2003-08-19 13:00:56 +00:00
|
|
|
|
/**
|
2006-09-17 09:14:18 +00:00
|
|
|
|
* \file MathMacroTable.C
|
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>
|
|
|
|
|
|
2006-09-17 09:14:18 +00:00
|
|
|
|
#include "MathMacroTable.h"
|
|
|
|
|
#include "MathMacroTemplate.h"
|
|
|
|
|
#include "MathMacroArgument.h"
|
|
|
|
|
#include "MathSupport.h"
|
|
|
|
|
#include "InsetMathSqrt.h"
|
2004-04-13 06:27:29 +00:00
|
|
|
|
|
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 <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()
|
2004-04-13 06:27:29 +00:00
|
|
|
|
: numargs_(0)
|
|
|
|
|
{}
|
2001-07-26 16:14:23 +00:00
|
|
|
|
|
2001-02-13 17:08:51 +00:00
|
|
|
|
|
2006-11-13 17:35:18 +00:00
|
|
|
|
MacroData::MacroData(docstring const & def, int numargs, docstring const & disp, string const & requires)
|
|
|
|
|
: def_(def), numargs_(numargs), disp_(disp), requires_(requires)
|
2004-04-13 06:27:29 +00:00
|
|
|
|
{}
|
2001-02-13 17:08:51 +00:00
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
|
2004-04-13 13:54:58 +00:00
|
|
|
|
void MacroData::expand(vector<MathArray> const & args, MathArray & 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
|
2006-10-22 10:15:23 +00:00
|
|
|
|
asArray(disp_.empty() ? def_ : disp_, 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;
|
|
|
|
|
if (it.nextInset()->lyxCode() != InsetBase::MATHMACROARG_CODE)
|
|
|
|
|
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
|
|
|
|
|
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)
|
|
|
|
|
<< " [" << to_utf8(it->second.def()) << "] : "
|
|
|
|
|
<< " [" << to_utf8(it->second.disp()) << "] : "
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|