lyx_mirror/src/mathed/MacroTable.h
Stefan Schimanski 36dbec4506 * Lazy MathData to avoid unneeded interpretation of macro definitions
* DocIterator as MacroPosition
* Iterative search for macro in scope until a visible one is found.
This include the ability to resolve macro inside nested text insets.
* Speed up macro lookups by factor 2: only getMacro(name) call, no
further hasMacro(name) call before
* Both way child/master support
* Correct macro scope for multi-paragraph environments
* Correct macro scope for multi-depth-paragraphs
* Buffer::updateMacros made const
* Update macros when loaded (of master and child)
* Do not remove too many braces when unfolding a macro. This could
lead to a data loss because the relationship between arguments of
macros can be mixed up if nested macros are unfold at once.
* Reduce dependencies to MetricsInfo in MathMacro   
* Update macros when needed. Normally it's done just before doing
metrics. But in cases without a brace around some constructs (like
\left(bla\right)) there is some help needed.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22241 a592a061-630c-0410-9148-cb99ea01b6c8
2007-12-21 20:42:46 +00:00

189 lines
4.6 KiB
C++

// -*- C++ -*-
/**
* \file MacroTable.h
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author André Pönitz
* \author Stefan Schimanski
*
* Full author contact details are available in file CREDITS.
*/
#ifndef MATH_MACROTABLE_H
#define MATH_MACROTABLE_H
#include "DocIterator.h"
#include "support/docstring.h"
#include "support/types.h"
#include <map>
#include <vector>
namespace lyx {
class Buffer;
class MathData;
class MathMacroTemplate;
class Paragraph;
enum MacroType {
MacroTypeNewcommand,
MacroTypeDef
};
///
class MacroData {
public:
/// Constructor to make STL containers happy
MacroData();
/// Create lazy MacroData which only queries the macro template when needed
MacroData(Buffer const & buf, DocIterator const & pos);
/// Create non-lazy MacroData which directly queries the macro template
MacroData(MathMacroTemplate const & macro);
///
docstring const & definition() const { updateData(); return definition_; }
///
docstring const & display() const { updateData(); return display_; }
/// arity including optional arguments (if there is any)
size_t numargs() const { updateData(); return numargs_; }
/// replace #1,#2,... by given MathAtom 0,1,.., _including_ the possible
/// optional argument
void expand(std::vector<MathData> const & from, MathData & to) const;
/// number of optional arguments
size_t optionals() const;
///
std::vector<docstring> const & defaults() const;
///
std::string const & requires() const { updateData(); return requires_; }
///
std::string & requires() { updateData(); return requires_; }
/// lock while being drawn to avoid recursions
int lock() const { return ++lockCount_; }
/// is it being drawn?
bool locked() const { return lockCount_ != 0; }
///
void unlock() const;
///
bool redefinition() const { return redefinition_; }
///
void setRedefinition(bool redefined) { redefinition_ = redefined; }
///
MacroType type() const { return type_; }
///
MacroType & type() { return type_; }
/// output as TeX macro, only works for lazy MacroData!!!
void write(odocstream & os, bool overwriteRedefinition) const;
///
bool operator==(MacroData const & x) const {
updateData();
x.updateData();
return definition_ == x.definition_
&& numargs_ == x.numargs_
&& display_ == x.display_
&& requires_ == x.requires_
&& optionals_ == x.optionals_
&& defaults_ == x.defaults_;
}
///
bool operator!=(MacroData const & x) const { return !operator==(x); }
private:
///
void queryData(MathMacroTemplate const & macro) const;
///
void updateData() const;
///
Buffer const * buffer_;
/// The position of the definition in the buffer.
/// There is no guarantee it stays valid if the buffer
/// changes. But it (normally) exists only until the
/// next Buffer::updateMacros call where new MacroData
/// objects are created for each macro definition.
/// In the worst case, it is invalidated and the MacroData
/// returns its defaults values and the user sees unfolded
/// macros.
mutable DocIterator pos_;
///
mutable bool queried_;
///
mutable docstring definition_;
///
mutable size_t numargs_;
///
mutable docstring display_;
///
mutable std::string requires_;
///
mutable size_t optionals_;
///
mutable std::vector<docstring> defaults_;
///
mutable int lockCount_;
///
mutable bool redefinition_;
///
mutable MacroType type_;
};
/// A lookup table of macro definitions.
/**
* This contains a table of "global" macros that are always accessible,
* either because they implement a feature of standard LaTeX or some
* hack to display certain contents nicely.
*
**/
class MacroTable : public std::map<docstring, MacroData>
{
public:
/// Parse full "\\def..." or "\\newcommand..." or ...
void insert(docstring const & definition, std::string const &);
/// Insert pre-digested macro definition
void insert(docstring const & name, MacroData const & data);
///
MacroData const * get(docstring const & name) const;
///
void dump();
/// the global list
static MacroTable & globalMacros();
};
/// A context to lookup macros at a certain position in a buffer.
/**
* The MacroContext is used during metrics calculation to resolve
* macro instances according to the position of them in the buffer
* document. Only macro definition in front of the macro instance
* are visible and are resolved.
*
**/
class MacroContext {
public:
/// construct context for the insets at pos
MacroContext(Buffer const & buf, DocIterator const & pos);
/// Lookup macro
MacroData const * get(docstring const & name) const;
private:
///
Buffer const & buf_;
///
DocIterator const & pos_;
};
} // namespace lyx
#endif