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-12-21 20:42:46 +00:00
|
|
|
|
#include "InsetMathSqrt.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"
|
2007-12-21 20:42:46 +00:00
|
|
|
|
#include "MathStream.h"
|
2006-09-17 09:14:18 +00:00
|
|
|
|
#include "MathSupport.h"
|
2007-11-01 11:13:07 +00:00
|
|
|
|
#include "InsetMathNest.h"
|
2007-12-21 20:42:46 +00:00
|
|
|
|
|
2007-11-01 14:30:48 +00:00
|
|
|
|
#include "Buffer.h"
|
2008-02-18 07:14:42 +00:00
|
|
|
|
#include "DocIterator.h"
|
2007-12-21 20:42:46 +00:00
|
|
|
|
#include "InsetList.h"
|
|
|
|
|
#include "Text.h"
|
2007-11-01 11:13:07 +00:00
|
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
|
#include "support/debug.h"
|
2001-02-13 17:08:51 +00:00
|
|
|
|
|
2008-04-30 08:26:40 +00:00
|
|
|
|
#include "support/lassert.h"
|
2003-08-02 11:30:30 +00:00
|
|
|
|
|
2004-07-24 10:55:30 +00:00
|
|
|
|
#include <sstream>
|
|
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
|
using namespace std;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
2003-08-02 11:30:30 +00:00
|
|
|
|
|
2007-11-06 20:38:35 +00:00
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// MacroData
|
|
|
|
|
//
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2004-04-20 08:51:15 +00:00
|
|
|
|
MacroData::MacroData()
|
2007-12-21 20:42:46 +00:00
|
|
|
|
: queried_(true), numargs_(0), optionals_(0), lockCount_(0),
|
|
|
|
|
redefinition_(false), type_(MacroTypeNewcommand)
|
2004-04-13 06:27:29 +00:00
|
|
|
|
{}
|
2001-07-26 16:14:23 +00:00
|
|
|
|
|
2007-12-21 20:42:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MacroData::MacroData(Buffer const & buf, DocIterator const & pos)
|
|
|
|
|
: buffer_(&buf), pos_(pos), queried_(false), numargs_(0),
|
|
|
|
|
optionals_(0), lockCount_(0), redefinition_(false),
|
|
|
|
|
type_(MacroTypeNewcommand)
|
2007-11-01 11:13:07 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
2007-12-21 20:42:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MacroData::MacroData(MathMacroTemplate const & macro)
|
|
|
|
|
: queried_(false), numargs_(0), optionals_(0), lockCount_(0),
|
|
|
|
|
redefinition_(false), type_(MacroTypeNewcommand)
|
|
|
|
|
{
|
|
|
|
|
queryData(macro);
|
2008-03-26 12:55:36 +00:00
|
|
|
|
}
|
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
|
|
|
|
{
|
2007-12-21 20:42:46 +00:00
|
|
|
|
updateData();
|
|
|
|
|
|
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 15:36:27 +00:00
|
|
|
|
size_t MacroData::optionals() const
|
2007-11-01 11:13:07 +00:00
|
|
|
|
{
|
2007-12-21 20:42:46 +00:00
|
|
|
|
updateData();
|
2007-11-01 11:13:07 +00:00
|
|
|
|
return optionals_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
|
vector<docstring> const & MacroData::defaults() const
|
2007-11-01 11:13:07 +00:00
|
|
|
|
{
|
2007-12-21 20:42:46 +00:00
|
|
|
|
updateData();
|
2007-11-01 11:13:07 +00:00
|
|
|
|
return defaults_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-11-06 20:38:35 +00:00
|
|
|
|
void MacroData::unlock() const
|
|
|
|
|
{
|
|
|
|
|
--lockCount_;
|
2008-04-10 21:49:34 +00:00
|
|
|
|
LASSERT(lockCount_ >= 0, /**/);
|
2007-11-06 20:38:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-12-21 20:42:46 +00:00
|
|
|
|
void MacroData::queryData(MathMacroTemplate const & macro) const
|
|
|
|
|
{
|
|
|
|
|
if (queried_)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
queried_ = true;
|
|
|
|
|
definition_ = macro.definition();
|
|
|
|
|
numargs_ = macro.numArgs();
|
|
|
|
|
display_ = macro.displayDefinition();
|
|
|
|
|
redefinition_ = macro.redefinition();
|
|
|
|
|
type_ = macro.type();
|
|
|
|
|
optionals_ = macro.numOptionals();
|
2008-03-26 12:55:36 +00:00
|
|
|
|
|
|
|
|
|
macro.getDefaults(defaults_);
|
2007-12-21 20:42:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MacroData::updateData() const
|
|
|
|
|
{
|
|
|
|
|
if (queried_)
|
|
|
|
|
return;
|
|
|
|
|
|
2008-04-10 21:49:34 +00:00
|
|
|
|
LASSERT(buffer_ != 0, /**/);
|
2007-12-21 20:42:46 +00:00
|
|
|
|
|
|
|
|
|
// Try to fix position DocIterator. Should not do anything in theory.
|
|
|
|
|
pos_.fixIfBroken();
|
|
|
|
|
|
|
|
|
|
// find macro template
|
|
|
|
|
Inset * inset = pos_.nextInset();
|
|
|
|
|
if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
|
|
|
|
|
lyxerr << "BUG: No macro template found by MacroData" << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// query the data from the macro template
|
|
|
|
|
queryData(static_cast<MathMacroTemplate const &>(*inset));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MacroData::write(odocstream & os, bool overwriteRedefinition) const
|
|
|
|
|
{
|
|
|
|
|
updateData();
|
|
|
|
|
|
|
|
|
|
// find macro template
|
|
|
|
|
Inset * inset = pos_.nextInset();
|
|
|
|
|
if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
|
|
|
|
|
lyxerr << "BUG: No macro template found by MacroData" << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// output template
|
|
|
|
|
MathMacroTemplate const & tmpl
|
|
|
|
|
= static_cast<MathMacroTemplate const &>(*inset);
|
|
|
|
|
WriteStream wi(os, false, true);
|
|
|
|
|
tmpl.write(wi, overwriteRedefinition);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-11-06 20:38:35 +00:00
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// The global table of macros
|
|
|
|
|
//
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2004-04-13 06:27:29 +00:00
|
|
|
|
MacroTable & MacroTable::globalMacros()
|
2001-06-25 00:06:33 +00:00
|
|
|
|
{
|
2004-04-13 06:27:29 +00:00
|
|
|
|
static MacroTable theGlobalMacros;
|
|
|
|
|
return theGlobalMacros;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-12-21 20:42:46 +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);
|
2007-12-21 20:42:46 +00:00
|
|
|
|
return it == end() ? 0 : &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);
|
2007-12-21 20:42:46 +00:00
|
|
|
|
MacroData data(mac);
|
2006-11-13 17:35:18 +00:00
|
|
|
|
data.requires() = requires;
|
|
|
|
|
insert(mac.name(), data);
|
2004-04-13 06:27:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-02-21 19:42:34 +00:00
|
|
|
|
void MacroTable::getMacroNames(std::set<docstring> & names) const
|
|
|
|
|
{
|
|
|
|
|
for (const_iterator it = begin(); it != end(); ++it)
|
|
|
|
|
names.insert(it->first);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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-12-21 20:42:46 +00:00
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// MacroContext
|
|
|
|
|
//
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
2007-11-01 11:13:07 +00:00
|
|
|
|
|
2007-12-21 20:42:46 +00:00
|
|
|
|
MacroContext::MacroContext(Buffer const & buf, DocIterator const & pos)
|
|
|
|
|
: buf_(buf), pos_(pos)
|
2007-11-01 11:13:07 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-12-21 20:42:46 +00:00
|
|
|
|
MacroData const * MacroContext::get(docstring const & name) const
|
2007-11-01 11:13:07 +00:00
|
|
|
|
{
|
2007-12-21 20:42:46 +00:00
|
|
|
|
return buf_.getMacro(name, pos_);
|
2007-11-01 11:13:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
} // namespace lyx
|