2003-08-19 13:00:56 +00:00
|
|
|
|
/**
|
2007-04-28 20:44:46 +00:00
|
|
|
|
* \file MacroTable.cpp
|
2003-08-19 13:00:56 +00:00
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
|
*
|
|
|
|
|
* \author Andr<EFBFBD> P<EFBFBD>nitz
|
|
|
|
|
*
|
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
|
*/
|
|
|
|
|
|
2001-02-13 17:08:51 +00:00
|
|
|
|
#include <config.h>
|
|
|
|
|
|
2007-04-28 20:44:46 +00:00
|
|
|
|
#include "MacroTable.h"
|
2006-09-17 09:14:18 +00:00
|
|
|
|
#include "MathMacroTemplate.h"
|
|
|
|
|
#include "MathMacroArgument.h"
|
|
|
|
|
#include "MathSupport.h"
|
|
|
|
|
#include "InsetMathSqrt.h"
|
2004-04-13 06:27:29 +00:00
|
|
|
|
|
2007-11-01 11:13:07 +00:00
|
|
|
|
#include "InsetMathNest.h"
|
2007-11-01 14:30:48 +00:00
|
|
|
|
#include "Buffer.h"
|
2007-11-01 11:13:07 +00:00
|
|
|
|
|
2001-02-13 19:10:18 +00:00
|
|
|
|
#include "debug.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
|
#include "DocIterator.h"
|
2001-02-13 17:08:51 +00:00
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
#include <boost/assert.hpp>
|
2003-08-02 11:30:30 +00:00
|
|
|
|
|
2004-07-24 10:55:30 +00:00
|
|
|
|
#include <sstream>
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
2003-08-02 11:30:30 +00:00
|
|
|
|
using std::endl;
|
2004-04-13 06:27:29 +00:00
|
|
|
|
using std::istringstream;
|
|
|
|
|
using std::map;
|
|
|
|
|
using std::pair;
|
|
|
|
|
using std::string;
|
|
|
|
|
using std::vector;
|
2004-07-23 22:31:14 +00:00
|
|
|
|
using std::size_t;
|
2004-04-13 06:27:29 +00:00
|
|
|
|
|
2003-08-02 11:30:30 +00:00
|
|
|
|
|
2004-04-20 08:51:15 +00:00
|
|
|
|
MacroData::MacroData()
|
2007-11-01 11:13:07 +00:00
|
|
|
|
: numargs_(0), lockCount_(0), redefinition_(false)
|
2004-04-13 06:27:29 +00:00
|
|
|
|
{}
|
2001-07-26 16:14:23 +00:00
|
|
|
|
|
2001-02-13 17:08:51 +00:00
|
|
|
|
|
2007-11-01 11:13:07 +00:00
|
|
|
|
MacroData::MacroData(docstring const & definition, std::vector<docstring> const & defaults,
|
|
|
|
|
int numargs, int optionals, docstring const & display, string const & requires)
|
|
|
|
|
: definition_(definition), numargs_(numargs), display_(display),
|
|
|
|
|
requires_(requires), lockCount_(0), redefinition_(false), optionals_(optionals),
|
|
|
|
|
defaults_(defaults)
|
|
|
|
|
{
|
|
|
|
|
defaults_.resize(optionals);
|
|
|
|
|
}
|
2001-02-13 17:08:51 +00:00
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
|
2007-04-26 16:05:57 +00:00
|
|
|
|
void MacroData::expand(vector<MathData> const & args, MathData & to) const
|
2001-02-13 17:08:51 +00:00
|
|
|
|
{
|
2006-09-16 18:11:38 +00:00
|
|
|
|
InsetMathSqrt inset; // Hack. Any inset with a cell would do.
|
2006-10-17 14:46:45 +00:00
|
|
|
|
// FIXME UNICODE
|
2007-11-01 11:13:07 +00:00
|
|
|
|
asArray(display_.empty() ? definition_ : display_, inset.cell(0));
|
2004-04-13 06:27:29 +00:00
|
|
|
|
//lyxerr << "MathData::expand: args: " << args << endl;
|
|
|
|
|
//lyxerr << "MathData::expand: ar: " << inset.cell(0) << endl;
|
|
|
|
|
for (DocIterator it = doc_iterator_begin(inset); it; it.forwardChar()) {
|
|
|
|
|
if (!it.nextInset())
|
|
|
|
|
continue;
|
2007-10-13 09:04:52 +00:00
|
|
|
|
if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
|
2004-04-13 06:27:29 +00:00
|
|
|
|
continue;
|
|
|
|
|
//it.cell().erase(it.pos());
|
2006-09-16 18:11:38 +00:00
|
|
|
|
//it.cell().insert(it.pos(), it.nextInset()->asInsetMath()
|
2004-07-23 22:31:14 +00:00
|
|
|
|
size_t n = static_cast<MathMacroArgument*>(it.nextInset())->number();
|
2004-04-13 06:27:29 +00:00
|
|
|
|
if (n <= args.size()) {
|
2004-04-13 13:54:58 +00:00
|
|
|
|
it.cell().erase(it.pos());
|
|
|
|
|
it.cell().insert(it.pos(), args[n - 1]);
|
2004-04-13 06:27:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//lyxerr << "MathData::expand: res: " << inset.cell(0) << endl;
|
|
|
|
|
to = inset.cell(0);
|
2001-02-13 17:08:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-03-01 14:07:43 +00:00
|
|
|
|
|
2007-11-01 11:13:07 +00:00
|
|
|
|
int MacroData::optionals() const
|
|
|
|
|
{
|
|
|
|
|
return optionals_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<docstring> const & MacroData::defaults() const
|
|
|
|
|
{
|
|
|
|
|
return defaults_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
// The global table.
|
|
|
|
|
MacroTable & MacroTable::globalMacros()
|
2001-06-25 00:06:33 +00:00
|
|
|
|
{
|
2004-04-13 06:27:29 +00:00
|
|
|
|
static MacroTable theGlobalMacros;
|
|
|
|
|
return theGlobalMacros;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
|
bool MacroTable::has(docstring const & name) const
|
2004-04-13 06:27:29 +00:00
|
|
|
|
{
|
2004-04-13 13:54:58 +00:00
|
|
|
|
return find(name) != end();
|
2004-04-13 06:27:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
|
MacroData const & MacroTable::get(docstring const & name) const
|
2004-04-13 06:27:29 +00:00
|
|
|
|
{
|
2004-04-13 13:54:58 +00:00
|
|
|
|
const_iterator it = find(name);
|
|
|
|
|
BOOST_ASSERT(it != end());
|
2004-04-13 06:27:29 +00:00
|
|
|
|
return it->second;
|
2001-04-24 16:13:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
|
void MacroTable::insert(docstring const & name, MacroData const & data)
|
2001-04-24 16:13:38 +00:00
|
|
|
|
{
|
2006-10-22 12:07:01 +00:00
|
|
|
|
//lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
|
2004-04-13 13:54:58 +00:00
|
|
|
|
operator[](name) = data;
|
2002-03-27 18:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
|
|
2006-11-13 17:35:18 +00:00
|
|
|
|
void MacroTable::insert(docstring const & def, string const & requires)
|
2001-04-24 16:13:38 +00:00
|
|
|
|
{
|
2006-10-22 12:07:01 +00:00
|
|
|
|
//lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
|
2006-10-22 14:37:32 +00:00
|
|
|
|
MathMacroTemplate mac(def);
|
2006-11-13 17:35:18 +00:00
|
|
|
|
MacroData data = mac.asMacroData();
|
|
|
|
|
data.requires() = requires;
|
|
|
|
|
insert(mac.name(), data);
|
2004-04-13 06:27:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MacroTable::dump()
|
|
|
|
|
{
|
|
|
|
|
lyxerr << "\n------------------------------------------" << endl;
|
2004-04-13 13:54:58 +00:00
|
|
|
|
for (const_iterator it = begin(); it != end(); ++it)
|
2006-10-22 10:15:23 +00:00
|
|
|
|
lyxerr << to_utf8(it->first)
|
2007-11-01 11:13:07 +00:00
|
|
|
|
<< " [" << to_utf8(it->second.definition()) << "] : "
|
|
|
|
|
<< " [" << to_utf8(it->second.display()) << "] : "
|
2004-04-13 06:27:29 +00:00
|
|
|
|
<< endl;
|
|
|
|
|
lyxerr << "------------------------------------------" << endl;
|
2001-02-13 17:08:51 +00:00
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
2007-11-01 11:13:07 +00:00
|
|
|
|
MacroContext::MacroContext(Buffer const & buf, Paragraph const & par)
|
|
|
|
|
: buf_(buf), par_(par)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool MacroContext::has(docstring const & name) const
|
|
|
|
|
{
|
|
|
|
|
// check if it's a local macro
|
|
|
|
|
if (macros_.has(name))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
// otherwise ask the buffer
|
|
|
|
|
return buf_.hasMacro(name, par_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MacroData const & MacroContext::get(docstring const & name) const
|
|
|
|
|
{
|
|
|
|
|
// check if it's a local macro
|
|
|
|
|
if (macros_.has(name))
|
|
|
|
|
return macros_.get(name);
|
|
|
|
|
|
|
|
|
|
// ask the buffer for its macros
|
|
|
|
|
return buf_.getMacro(name, par_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MacroContext::insert(docstring const & name, MacroData const & data)
|
|
|
|
|
{
|
|
|
|
|
macros_.insert(name, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
} // namespace lyx
|