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.
|
|
|
|
*
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author André Pönitz
|
2003-08-19 13:00:56 +00:00
|
|
|
*
|
|
|
|
* 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"
|
2014-12-29 20:13:42 +00:00
|
|
|
#include "MathParser.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"
|
2013-04-25 21:27:10 +00:00
|
|
|
#include "support/gettext.h"
|
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
|
|
|
|
//
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
2009-11-08 11:45:46 +00:00
|
|
|
MacroData::MacroData(Buffer * buf)
|
2014-12-29 20:13:42 +00:00
|
|
|
: buffer_(buf), queried_(true), numargs_(0), sym_(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
|
|
|
|
2013-10-11 18:38:05 +00:00
|
|
|
|
2009-11-08 11:45:46 +00:00
|
|
|
MacroData::MacroData(Buffer * buf, DocIterator const & pos)
|
2014-12-29 20:13:42 +00:00
|
|
|
: buffer_(buf), pos_(pos), queried_(false), numargs_(0), sym_(0),
|
2007-12-21 20:42:46 +00:00
|
|
|
optionals_(0), lockCount_(0), redefinition_(false),
|
|
|
|
type_(MacroTypeNewcommand)
|
2007-11-01 11:13:07 +00:00
|
|
|
{
|
|
|
|
}
|
2013-10-11 18:38:05 +00:00
|
|
|
|
|
|
|
|
2009-11-08 11:45:46 +00:00
|
|
|
MacroData::MacroData(Buffer * buf, MathMacroTemplate const & macro)
|
2014-12-29 20:13:42 +00:00
|
|
|
: buffer_(buf), queried_(false), numargs_(0), sym_(0), optionals_(0),
|
|
|
|
lockCount_(0), redefinition_(false), type_(MacroTypeNewcommand)
|
2007-12-21 20:42:46 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2014-11-14 20:30:42 +00:00
|
|
|
bool 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();
|
|
|
|
|
2008-11-17 11:46:07 +00:00
|
|
|
// Hack. Any inset with a cell would do.
|
2015-04-13 19:07:05 +00:00
|
|
|
InsetMathSqrt inset(const_cast<Buffer *>(buffer_));
|
2008-11-17 11:46:07 +00:00
|
|
|
|
2014-11-14 20:30:42 +00:00
|
|
|
docstring const & definition(display_.empty() ? definition_ : display_);
|
|
|
|
asArray(definition, inset.cell(0));
|
2004-04-13 06:27:29 +00:00
|
|
|
//lyxerr << "MathData::expand: args: " << args << endl;
|
2014-11-14 20:30:42 +00:00
|
|
|
//LYXERR0("MathData::expand: ar: " << inset.cell(0));
|
2009-08-06 10:25:48 +00:00
|
|
|
for (DocIterator it = doc_iterator_begin(buffer_, &inset); it; it.forwardChar()) {
|
2004-04-13 06:27:29 +00:00
|
|
|
if (!it.nextInset())
|
|
|
|
continue;
|
2009-07-16 19:00:24 +00:00
|
|
|
if (it.nextInset()->lyxCode() != MATH_MACROARG_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
|
|
|
}
|
|
|
|
}
|
2014-11-14 20:30:42 +00:00
|
|
|
//LYXERR0("MathData::expand: res: " << inset.cell(0));
|
2004-04-13 06:27:29 +00:00
|
|
|
to = inset.cell(0);
|
2014-11-14 20:30:42 +00:00
|
|
|
// If the result is equal to the definition then we either have a
|
|
|
|
// recursive loop, or the definition did not contain any macro in the
|
|
|
|
// first place.
|
|
|
|
return asString(to) != definition;
|
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_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-11 18:38:05 +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_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-29 20:13:42 +00:00
|
|
|
string const MacroData::requires() const
|
|
|
|
{
|
|
|
|
if (sym_)
|
2014-12-29 20:46:30 +00:00
|
|
|
return sym_->requires;
|
2014-12-29 20:13:42 +00:00
|
|
|
return string();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-10 19:53:56 +00:00
|
|
|
bool MacroData::hidden() const
|
|
|
|
{
|
|
|
|
if (sym_)
|
|
|
|
return sym_->hidden;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-30 12:30:40 +00:00
|
|
|
docstring const MacroData::xmlname() const
|
|
|
|
{
|
|
|
|
if (sym_)
|
|
|
|
return sym_->xmlname;
|
|
|
|
return docstring();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char const * MacroData::MathMLtype() const
|
|
|
|
{
|
|
|
|
return sym_ ? sym_->MathMLtype() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-06 20:38:35 +00:00
|
|
|
void MacroData::unlock() const
|
|
|
|
{
|
|
|
|
--lockCount_;
|
2013-04-25 21:27:10 +00:00
|
|
|
LASSERT(lockCount_ >= 0, 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();
|
2013-10-11 18:38:05 +00:00
|
|
|
|
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;
|
|
|
|
|
2013-04-27 21:52:55 +00:00
|
|
|
LBUFERR(buffer_);
|
2013-10-11 18:38:05 +00:00
|
|
|
|
2007-12-21 20:42:46 +00:00
|
|
|
// Try to fix position DocIterator. Should not do anything in theory.
|
|
|
|
pos_.fixIfBroken();
|
2013-10-11 18:38:05 +00:00
|
|
|
|
2007-12-21 20:42:46 +00:00
|
|
|
// find macro template
|
|
|
|
Inset * inset = pos_.nextInset();
|
|
|
|
if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
|
|
|
|
lyxerr << "BUG: No macro template found by MacroData" << endl;
|
|
|
|
return;
|
|
|
|
}
|
2013-10-11 18:38:05 +00:00
|
|
|
|
2007-12-21 20:42:46 +00:00
|
|
|
// query the data from the macro template
|
2013-10-11 18:38:05 +00:00
|
|
|
queryData(static_cast<MathMacroTemplate const &>(*inset));
|
2007-12-21 20:42:46 +00:00
|
|
|
}
|
2013-10-11 18:38:05 +00:00
|
|
|
|
2007-12-21 20:42:46 +00:00
|
|
|
|
2010-02-08 17:39:55 +00:00
|
|
|
int MacroData::write(odocstream & os, bool overwriteRedefinition) const
|
2007-12-21 20:42:46 +00:00
|
|
|
{
|
|
|
|
updateData();
|
|
|
|
|
|
|
|
// find macro template
|
|
|
|
Inset * inset = pos_.nextInset();
|
|
|
|
if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
|
|
|
|
lyxerr << "BUG: No macro template found by MacroData" << endl;
|
2010-02-08 17:39:55 +00:00
|
|
|
return 0;
|
2007-12-21 20:42:46 +00:00
|
|
|
}
|
2013-10-11 18:38:05 +00:00
|
|
|
|
2007-12-21 20:42:46 +00:00
|
|
|
// output template
|
2008-05-31 20:54:50 +00:00
|
|
|
MathMacroTemplate const & tmpl =
|
|
|
|
static_cast<MathMacroTemplate const &>(*inset);
|
2015-10-07 03:13:21 +00:00
|
|
|
TexRow texrow(false);
|
|
|
|
otexrowstream ots(os,texrow);
|
|
|
|
WriteStream wi(ots, false, true, WriteStream::wsDefault);
|
2010-02-08 17:39:55 +00:00
|
|
|
return tmpl.write(wi, overwriteRedefinition);
|
2007-12-21 20:42:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-17 20:38:40 +00:00
|
|
|
MacroTable::iterator
|
|
|
|
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;
|
2012-12-17 20:38:40 +00:00
|
|
|
iterator it = find(name);
|
|
|
|
if (it == end())
|
|
|
|
it = map<docstring, MacroData>::insert(
|
|
|
|
make_pair(name, data)).first;
|
|
|
|
else
|
|
|
|
it->second = data;
|
|
|
|
return it;
|
2002-03-27 18:04:36 +00:00
|
|
|
}
|
|
|
|
|
2001-10-12 12:02:49 +00:00
|
|
|
|
2012-12-17 20:38:40 +00:00
|
|
|
MacroTable::iterator
|
2014-12-29 20:13:42 +00:00
|
|
|
MacroTable::insert(Buffer * buf, docstring const & def)
|
2001-04-24 16:13:38 +00:00
|
|
|
{
|
2006-10-22 12:07:01 +00:00
|
|
|
//lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
|
2015-08-01 07:05:33 +00:00
|
|
|
MathMacroTemplate mac(buf);
|
|
|
|
mac.fromString(def);
|
2009-11-08 11:45:46 +00:00
|
|
|
MacroData data(buf, mac);
|
2012-12-17 20:38:40 +00:00
|
|
|
return insert(mac.name(), data);
|
2004-04-13 06:27:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-10 19:53:56 +00:00
|
|
|
void MacroTable::getMacroNames(std::set<docstring> & names, bool gethidden) const
|
2008-02-21 19:42:34 +00:00
|
|
|
{
|
|
|
|
for (const_iterator it = begin(); it != end(); ++it)
|
2015-03-10 19:53:56 +00:00
|
|
|
if (gethidden || !it->second.hidden())
|
|
|
|
names.insert(it->first);
|
2008-02-21 19:42:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2009-11-08 11:45:46 +00:00
|
|
|
MacroContext::MacroContext(Buffer const * buf, DocIterator const & pos)
|
2007-12-21 20:42:46 +00:00
|
|
|
: 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
|
|
|
{
|
2009-11-08 11:45: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
|