4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
8 * \author Stefan Schimanski
10 * Full author contact details are available in file CREDITS.
13 #ifndef MATH_MACROTABLE_H
14 #define MATH_MACROTABLE_H
16 #include "DocIterator.h"
18 #include "support/docstring.h"
28 class MathMacroTemplate;
40 /// Constructor to make STL containers happy
41 MacroData(Buffer * buf = 0);
42 /// Create lazy MacroData which only queries the macro template when needed
43 MacroData(Buffer * buf, DocIterator const & pos);
44 /// Create non-lazy MacroData which directly queries the macro template
45 MacroData(Buffer * buf, MathMacroTemplate const & macro);
48 docstring const & definition() const { updateData(); return definition_; }
50 docstring const & display() const { updateData(); return display_; }
51 /// arity including optional arguments (if there is any)
52 size_t numargs() const { updateData(); return numargs_; }
53 /// replace #1,#2,... by given MathAtom 0,1,.., _including_ the possible
55 void expand(std::vector<MathData> const & from, MathData & to) const;
56 /// number of optional arguments
57 size_t optionals() const;
59 std::vector<docstring> const & defaults() const;
61 std::string const & requires() const { return requires_; }
63 std::string & requires() { return requires_; }
65 /// lock while being drawn to avoid recursions
66 int lock() const { return ++lockCount_; }
67 /// is it being drawn?
68 bool locked() const { return lockCount_ != 0; }
73 bool redefinition() const { updateData(); return redefinition_; }
76 MacroType type() const { updateData(); return type_; }
78 /// output as TeX macro, only works for lazy MacroData!!!
79 int write(odocstream & os, bool overwriteRedefinition) const;
82 bool operator==(MacroData const & x) const {
85 return definition_ == x.definition_
86 && numargs_ == x.numargs_
87 && display_ == x.display_
88 && requires_ == x.requires_
89 && optionals_ == x.optionals_
90 && defaults_ == x.defaults_;
93 bool operator!=(MacroData const & x) const { return !operator==(x); }
97 void queryData(MathMacroTemplate const & macro) const;
99 void updateData() const;
101 Buffer const * buffer_;
102 /// The position of the definition in the buffer.
103 /// There is no guarantee it stays valid if the buffer
104 /// changes. But it (normally) exists only until the
105 /// next Buffer::updateMacros call where new MacroData
106 /// objects are created for each macro definition.
107 /// In the worst case, it is invalidated and the MacroData
108 /// returns its defaults values and the user sees unfolded
110 mutable DocIterator pos_;
112 mutable bool queried_;
114 mutable docstring definition_;
116 mutable size_t numargs_;
118 mutable docstring display_;
120 std::string requires_;
122 mutable size_t optionals_;
124 mutable std::vector<docstring> defaults_;
126 mutable int lockCount_;
128 mutable bool redefinition_;
130 mutable MacroType type_;
135 class MacroNameSet : public std::set<docstring> {};
137 class MacroSet : public std::set<MacroData const *> {};
140 /// A lookup table of macro definitions.
142 * This contains a table of "global" macros that are always accessible,
143 * either because they implement a feature of standard LaTeX or some
144 * hack to display certain contents nicely.
147 class MacroTable : public std::map<docstring, MacroData>
150 /// Parse full "\\def..." or "\\newcommand..." or ...
151 iterator insert(Buffer * buf, docstring const & definition, std::string const &);
152 /// Insert pre-digested macro definition
153 iterator insert(docstring const & name, MacroData const & data);
155 MacroData const * get(docstring const & name) const;
159 void getMacroNames(std::set<docstring> & names) const;
162 static MacroTable & globalMacros();
166 /// A context to lookup macros at a certain position in a buffer.
168 * The MacroContext is used during metrics calculation to resolve
169 * macro instances according to the position of them in the buffer
170 * document. Only macro definition in front of the macro instance
171 * are visible and are resolved.
176 /// construct context for the insets at pos
177 MacroContext(Buffer const * buf, DocIterator const & pos);
180 MacroData const * get(docstring const & name) const;
186 DocIterator const & pos_;