lyx_mirror/src/mathed/MacroTable.cpp

238 lines
5.3 KiB
C++
Raw Normal View History

/**
* \file MacroTable.cpp
* 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.
*/
#include <config.h>
#include "InsetMathSqrt.h"
#include "MacroTable.h"
#include "MathMacroTemplate.h"
#include "MathMacroArgument.h"
#include "MathStream.h"
#include "MathSupport.h"
#include "InsetMathNest.h"
#include "Buffer.h"
#include "InsetList.h"
#include "Text.h"
#include "support/debug.h"
#include "DocIterator.h"
#include <boost/assert.hpp>
#include <sstream>
using namespace std;
namespace lyx {
/////////////////////////////////////////////////////////////////////
//
// MacroData
//
/////////////////////////////////////////////////////////////////////
MacroData::MacroData()
: queried_(true), numargs_(0), optionals_(0), lockCount_(0),
redefinition_(false), type_(MacroTypeNewcommand)
{}
MacroData::MacroData(Buffer const & buf, DocIterator const & pos)
: buffer_(&buf), pos_(pos), queried_(false), numargs_(0),
optionals_(0), lockCount_(0), redefinition_(false),
type_(MacroTypeNewcommand)
{
}
MacroData::MacroData(MathMacroTemplate const & macro)
: queried_(false), numargs_(0), optionals_(0), lockCount_(0),
redefinition_(false), type_(MacroTypeNewcommand)
{
queryData(macro);
}
void MacroData::expand(vector<MathData> const & args, MathData & to) const
{
updateData();
InsetMathSqrt inset; // Hack. Any inset with a cell would do.
// FIXME UNICODE
asArray(display_.empty() ? definition_ : display_, 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() != MATHMACROARG_CODE)
continue;
//it.cell().erase(it.pos());
//it.cell().insert(it.pos(), it.nextInset()->asInsetMath()
size_t n = static_cast<MathMacroArgument*>(it.nextInset())->number();
if (n <= args.size()) {
it.cell().erase(it.pos());
it.cell().insert(it.pos(), args[n - 1]);
}
}
//lyxerr << "MathData::expand: res: " << inset.cell(0) << endl;
to = inset.cell(0);
}
size_t MacroData::optionals() const
{
updateData();
return optionals_;
}
vector<docstring> const & MacroData::defaults() const
{
updateData();
return defaults_;
}
void MacroData::unlock() const
{
--lockCount_;
BOOST_ASSERT(lockCount_ >= 0);
}
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();
macro.getDefaults(defaults_);
}
void MacroData::updateData() const
{
if (queried_)
return;
BOOST_ASSERT(buffer_ != 0);
// 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);
}
/////////////////////////////////////////////////////////////////////
//
// The global table of macros
//
/////////////////////////////////////////////////////////////////////
MacroTable & MacroTable::globalMacros()
{
static MacroTable theGlobalMacros;
return theGlobalMacros;
}
MacroData const * MacroTable::get(docstring const & name) const
{
const_iterator it = find(name);
return it == end() ? 0 : &it->second;
}
void MacroTable::insert(docstring const & name, MacroData const & data)
{
//lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
operator[](name) = data;
}
Add support for the esint package * src/LaTeXFeatures.C (LaTeXFeatures::getPackages): handle esint and wasysym * src/mathed/MathMacroTable.[Ch] (requires_): New member: tell the feature this macro requires (MacroTable::insert): take new requires arg * src/mathed/MathMacroTemplate.C (MathMacroTemplate::asMacroData): adjust to change above * src/mathed/MathSupport.C (fontinfos): add esint10 font * src/mathed/InsetMathHull.C (InsetMathHull::doDispatch): AMS_ON -> package_on * src/mathed/MathMacroTable.h * src/mathed/MathFactory.C (initSymbols): read and store requires field for symbols * src/mathed/InsetMathSymbol.C (InsetMathSymbol::metrics): handle esint (InsetMathSymbol::takesLimits): ditto * src/buffer.C (LYX_FORMAT): update format (Buffer::validate): handle esint, AMS_ON -> package_on * src/bufferparams.C: (AMSTranslator): Rename to PackageTranslator (BufferParams::readToken): Read \use_esint (BufferParams::writeFile): Write \use_esint * src/frontends/qt4/QDocumentDialog.C: handle esint * src/frontends/qt4/ui/MathsUi.ui : add esint checkboxes * src/frontends/qt4/GuiFontLoader.C (symbol_fonts: Add esint10 font (symbolFamily): handle esint10 font (isChosenFont): Add comment * src/frontends/controllers/ControlMath.C (latex_varsz): Add new integral symbols * src/support/fontutils.C (win_fonts_truetype): Add esint10 font * src/bufferparams.h (enum AMS): rename to enum Package (use_esint): new parameter * src/lyxfont.[Ch]: Add esint font * lib/symbols: Add new integral symbols * lib/lyx2lyx/LyX.py (format_relation): Update format * lib/lyx2lyx/lyx_1_5.py: handle new format * lib/chkconfig.ltx: Test esint package * lib/images/math/oiintop.xpm * lib/images/math/sqintop.xpm * lib/images/math/sqint.xpm * lib/images/math/ointctrclockwiseop.xpm * lib/images/math/ointctrclockwise.xpm * lib/images/math/iiintop.xpm * lib/images/math/iintop.xpm * lib/images/math/sqiint.xpm * lib/images/math/iiint.xpm * lib/images/math/ointclockwiseop.xpm * lib/images/math/oiint.xpm * lib/images/math/dotsintop.xpm * lib/images/math/sqiintop.xpm * lib/images/math/ointclockwise.xpm * lib/images/math/iiiintop.xpm * lib/images/math/dotsint.xpm * lib/images/math/iiiint.xpm * lib/images/math/iint.xpm: new icons * lib/doc/LaTeXConfig.lyx.in: Add docs for esint package * lib/doc/UserGuide.lyx: Add short documentation of integral symbols * lib/Makefile.am: Add new files * development/scons/scons_manifest.py: ditto git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@15907 a592a061-630c-0410-9148-cb99ea01b6c8
2006-11-13 17:35:18 +00:00
void MacroTable::insert(docstring const & def, string const & requires)
{
//lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
MathMacroTemplate mac(def);
MacroData data(mac);
Add support for the esint package * src/LaTeXFeatures.C (LaTeXFeatures::getPackages): handle esint and wasysym * src/mathed/MathMacroTable.[Ch] (requires_): New member: tell the feature this macro requires (MacroTable::insert): take new requires arg * src/mathed/MathMacroTemplate.C (MathMacroTemplate::asMacroData): adjust to change above * src/mathed/MathSupport.C (fontinfos): add esint10 font * src/mathed/InsetMathHull.C (InsetMathHull::doDispatch): AMS_ON -> package_on * src/mathed/MathMacroTable.h * src/mathed/MathFactory.C (initSymbols): read and store requires field for symbols * src/mathed/InsetMathSymbol.C (InsetMathSymbol::metrics): handle esint (InsetMathSymbol::takesLimits): ditto * src/buffer.C (LYX_FORMAT): update format (Buffer::validate): handle esint, AMS_ON -> package_on * src/bufferparams.C: (AMSTranslator): Rename to PackageTranslator (BufferParams::readToken): Read \use_esint (BufferParams::writeFile): Write \use_esint * src/frontends/qt4/QDocumentDialog.C: handle esint * src/frontends/qt4/ui/MathsUi.ui : add esint checkboxes * src/frontends/qt4/GuiFontLoader.C (symbol_fonts: Add esint10 font (symbolFamily): handle esint10 font (isChosenFont): Add comment * src/frontends/controllers/ControlMath.C (latex_varsz): Add new integral symbols * src/support/fontutils.C (win_fonts_truetype): Add esint10 font * src/bufferparams.h (enum AMS): rename to enum Package (use_esint): new parameter * src/lyxfont.[Ch]: Add esint font * lib/symbols: Add new integral symbols * lib/lyx2lyx/LyX.py (format_relation): Update format * lib/lyx2lyx/lyx_1_5.py: handle new format * lib/chkconfig.ltx: Test esint package * lib/images/math/oiintop.xpm * lib/images/math/sqintop.xpm * lib/images/math/sqint.xpm * lib/images/math/ointctrclockwiseop.xpm * lib/images/math/ointctrclockwise.xpm * lib/images/math/iiintop.xpm * lib/images/math/iintop.xpm * lib/images/math/sqiint.xpm * lib/images/math/iiint.xpm * lib/images/math/ointclockwiseop.xpm * lib/images/math/oiint.xpm * lib/images/math/dotsintop.xpm * lib/images/math/sqiintop.xpm * lib/images/math/ointclockwise.xpm * lib/images/math/iiiintop.xpm * lib/images/math/dotsint.xpm * lib/images/math/iiiint.xpm * lib/images/math/iint.xpm: new icons * lib/doc/LaTeXConfig.lyx.in: Add docs for esint package * lib/doc/UserGuide.lyx: Add short documentation of integral symbols * lib/Makefile.am: Add new files * development/scons/scons_manifest.py: ditto git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@15907 a592a061-630c-0410-9148-cb99ea01b6c8
2006-11-13 17:35:18 +00:00
data.requires() = requires;
insert(mac.name(), data);
}
void MacroTable::dump()
{
lyxerr << "\n------------------------------------------" << endl;
for (const_iterator it = begin(); it != end(); ++it)
lyxerr << to_utf8(it->first)
<< " [" << to_utf8(it->second.definition()) << "] : "
<< " [" << to_utf8(it->second.display()) << "] : "
<< endl;
lyxerr << "------------------------------------------" << endl;
}
/////////////////////////////////////////////////////////////////////
//
// MacroContext
//
/////////////////////////////////////////////////////////////////////
MacroContext::MacroContext(Buffer const & buf, DocIterator const & pos)
: buf_(buf), pos_(pos)
{
}
MacroData const * MacroContext::get(docstring const & name) const
{
return buf_.getMacro(name, pos_);
}
} // namespace lyx