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"
19 #include "support/types.h"
29 class MathMacroTemplate;
39 class MacroNameSet : public std::set<docstring> {};
44 /// Constructor to make STL containers happy
46 /// Create lazy MacroData which only queries the macro template when needed
47 MacroData(Buffer const & buf, DocIterator const & pos);
48 /// Create non-lazy MacroData which directly queries the macro template
49 MacroData(MathMacroTemplate const & macro);
52 docstring const & definition() const { updateData(); return definition_; }
54 docstring const & display() const { updateData(); return display_; }
55 /// arity including optional arguments (if there is any)
56 size_t numargs() const { updateData(); return numargs_; }
57 /// replace #1,#2,... by given MathAtom 0,1,.., _including_ the possible
59 void expand(std::vector<MathData> const & from, MathData & to) const;
60 /// number of optional arguments
61 size_t optionals() const;
63 std::vector<docstring> const & defaults() const;
65 std::string const & requires() const { updateData(); return requires_; }
67 std::string & requires() { updateData(); return requires_; }
69 /// lock while being drawn to avoid recursions
70 int lock() const { return ++lockCount_; }
71 /// is it being drawn?
72 bool locked() const { return lockCount_ != 0; }
77 bool redefinition() const { return redefinition_; }
79 void setRedefinition(bool redefined) { redefinition_ = redefined; }
82 MacroType type() const { return type_; }
84 MacroType & type() { return type_; }
86 /// output as TeX macro, only works for lazy MacroData!!!
87 void write(odocstream & os, bool overwriteRedefinition) const;
90 bool operator==(MacroData const & x) const {
93 return definition_ == x.definition_
94 && numargs_ == x.numargs_
95 && display_ == x.display_
96 && requires_ == x.requires_
97 && optionals_ == x.optionals_
98 && defaults_ == x.defaults_;
101 bool operator!=(MacroData const & x) const { return !operator==(x); }
105 void queryData(MathMacroTemplate const & macro) const;
107 void updateData() const;
109 Buffer const * buffer_;
110 /// The position of the definition in the buffer.
111 /// There is no guarantee it stays valid if the buffer
112 /// changes. But it (normally) exists only until the
113 /// next Buffer::updateMacros call where new MacroData
114 /// objects are created for each macro definition.
115 /// In the worst case, it is invalidated and the MacroData
116 /// returns its defaults values and the user sees unfolded
118 mutable DocIterator pos_;
120 mutable bool queried_;
122 mutable docstring definition_;
124 mutable size_t numargs_;
126 mutable docstring display_;
128 mutable std::string requires_;
130 mutable size_t optionals_;
132 mutable std::vector<docstring> defaults_;
134 mutable int lockCount_;
136 mutable bool redefinition_;
138 mutable MacroType type_;
142 /// A lookup table of macro definitions.
144 * This contains a table of "global" macros that are always accessible,
145 * either because they implement a feature of standard LaTeX or some
146 * hack to display certain contents nicely.
149 class MacroTable : public std::map<docstring, MacroData>
152 /// Parse full "\\def..." or "\\newcommand..." or ...
153 void insert(docstring const & definition, std::string const &);
154 /// Insert pre-digested macro definition
155 void insert(docstring const & name, MacroData const & data);
157 MacroData const * get(docstring const & name) const;
161 void getMacroNames(std::set<docstring> & names) const;
164 static MacroTable & globalMacros();
168 /// A context to lookup macros at a certain position in a buffer.
170 * The MacroContext is used during metrics calculation to resolve
171 * macro instances according to the position of them in the buffer
172 * document. Only macro definition in front of the macro instance
173 * are visible and are resolved.
178 /// construct context for the insets at pos
179 MacroContext(Buffer const & buf, DocIterator const & pos);
182 MacroData const * get(docstring const & name) const;
188 DocIterator const & pos_;