2003-08-19 13:00:56 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file math_macrotable.C
|
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
|
|
#include "math_macrotable.h"
|
|
|
|
|
#include "math_macrotemplate.h"
|
2004-04-13 06:27:29 +00:00
|
|
|
|
#include "math_macroarg.h"
|
|
|
|
|
#include "math_support.h"
|
|
|
|
|
#include "math_sqrtinset.h"
|
|
|
|
|
|
2001-02-13 19:10:18 +00:00
|
|
|
|
#include "debug.h"
|
2004-04-13 06:27:29 +00:00
|
|
|
|
#include "dociterator.h"
|
2001-02-13 17:08:51 +00:00
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
#include "support/std_sstream.h"
|
2004-03-27 12:46:30 +00:00
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
#include <boost/assert.hpp>
|
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;
|
|
|
|
|
|
2003-08-02 11:30:30 +00:00
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
MacroData::MacroData()
|
|
|
|
|
: numargs_(0)
|
|
|
|
|
{}
|
2001-07-26 16:14:23 +00:00
|
|
|
|
|
2001-02-13 17:08:51 +00:00
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
MacroData::MacroData(string const & def, int numargs, string const & disp)
|
|
|
|
|
: def_(def), numargs_(numargs), disp_(disp)
|
|
|
|
|
{}
|
2001-02-13 17:08:51 +00:00
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
|
|
|
|
|
void MacroData::expand(MathArray const & args, MathArray & to) const
|
2001-02-13 17:08:51 +00:00
|
|
|
|
{
|
2004-04-13 06:27:29 +00:00
|
|
|
|
MathSqrtInset inset; // Hack. Any inset with a cell would do.
|
|
|
|
|
asArray(disp_.empty() ? def_ : disp_, inset.cell(0));
|
|
|
|
|
//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;
|
|
|
|
|
if (it.nextInset()->lyxCode() != InsetBase::MATHMACROARG_CODE)
|
|
|
|
|
continue;
|
|
|
|
|
//it.cell().erase(it.pos());
|
|
|
|
|
//it.cell().insert(it.pos(), it.nextInset()->asMathInset()
|
|
|
|
|
int n = static_cast<MathMacroArgument*>(it.nextInset())->number();
|
|
|
|
|
if (n <= args.size()) {
|
|
|
|
|
if (args[n - 1]->asBraceInset()) {
|
|
|
|
|
it.cell().erase(it.pos());
|
|
|
|
|
it.cell().insert(it.pos(), args[n - 1]->cell(0));
|
|
|
|
|
} else {
|
|
|
|
|
it.cell()[it.pos()] = args[n - 1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//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
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool MacroTable::has(string const & name) const
|
|
|
|
|
{
|
|
|
|
|
return macros_.find(name) != macros_.end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MacroData const & MacroTable::get(string const & name) const
|
|
|
|
|
{
|
|
|
|
|
table_type::const_iterator it = macros_.find(name);
|
|
|
|
|
BOOST_ASSERT(it != macros_.end());
|
|
|
|
|
return it->second;
|
2001-04-24 16:13:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
void MacroTable::insert(string const & name, MacroData const & data)
|
2001-04-24 16:13:38 +00:00
|
|
|
|
{
|
2004-04-13 06:27:29 +00:00
|
|
|
|
lyxerr << "MathMacroTable::insert: " << name << endl;
|
|
|
|
|
macros_[name] = data;
|
2002-03-27 18:04:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
void MacroTable::insert(string const & def)
|
2001-04-24 16:13:38 +00:00
|
|
|
|
{
|
2004-04-13 06:27:29 +00:00
|
|
|
|
//lyxerr << "MathMacroTable::insert, def: " << def << endl;
|
|
|
|
|
istringstream is(def);
|
|
|
|
|
MathMacroTemplate mac(is);
|
|
|
|
|
insert(mac.name(), mac.asMacroData());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MacroTable::dump()
|
|
|
|
|
{
|
|
|
|
|
lyxerr << "\n------------------------------------------" << endl;
|
|
|
|
|
table_type::const_iterator it;
|
|
|
|
|
for (it = macros_.begin(); it != macros_.end(); ++it)
|
|
|
|
|
lyxerr << it->first
|
|
|
|
|
<< " [" << it->second.def() << "] : "
|
|
|
|
|
<< " [" << it->second.disp() << "] : "
|
|
|
|
|
<< endl;
|
|
|
|
|
lyxerr << "------------------------------------------" << endl;
|
2001-02-13 17:08:51 +00:00
|
|
|
|
}
|