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;
38 class MacroNameSet : public std::set<docstring> {};
43 /// Constructor to make STL containers happy
45 /// Create lazy MacroData which only queries the macro template when needed
46 MacroData(Buffer const & buf, DocIterator const & pos);
47 /// Create non-lazy MacroData which directly queries the macro template
48 MacroData(MathMacroTemplate const & macro);
51 docstring const & definition() const { updateData(); return definition_; }
53 docstring const & display() const { updateData(); return display_; }
54 /// arity including optional arguments (if there is any)
55 size_t numargs() const { updateData(); return numargs_; }
56 /// replace #1,#2,... by given MathAtom 0,1,.., _including_ the possible
58 void expand(std::vector<MathData> const & from, MathData & to) const;
59 /// number of optional arguments
60 size_t optionals() const;
62 std::vector<docstring> const & defaults() const;
64 std::string const & requires() const { updateData(); return requires_; }
66 std::string & requires() { updateData(); return requires_; }
68 /// lock while being drawn to avoid recursions
69 int lock() const { return ++lockCount_; }
70 /// is it being drawn?
71 bool locked() const { return lockCount_ != 0; }
76 bool redefinition() const { return redefinition_; }
78 void setRedefinition(bool redefined) { redefinition_ = redefined; }
81 MacroType type() const { return type_; }
83 MacroType & type() { return type_; }
85 /// output as TeX macro, only works for lazy MacroData!!!
86 void write(odocstream & os, bool overwriteRedefinition) const;
89 bool operator==(MacroData const & x) const {
92 return definition_ == x.definition_
93 && numargs_ == x.numargs_
94 && display_ == x.display_
95 && requires_ == x.requires_
96 && optionals_ == x.optionals_
97 && defaults_ == x.defaults_;
100 bool operator!=(MacroData const & x) const { return !operator==(x); }
104 void queryData(MathMacroTemplate const & macro) const;
106 void updateData() const;
108 Buffer const * buffer_;
109 /// The position of the definition in the buffer.
110 /// There is no guarantee it stays valid if the buffer
111 /// changes. But it (normally) exists only until the
112 /// next Buffer::updateMacros call where new MacroData
113 /// objects are created for each macro definition.
114 /// In the worst case, it is invalidated and the MacroData
115 /// returns its defaults values and the user sees unfolded
117 mutable DocIterator pos_;
119 mutable bool queried_;
121 mutable docstring definition_;
123 mutable size_t numargs_;
125 mutable docstring display_;
127 mutable std::string requires_;
129 mutable size_t optionals_;
131 mutable std::vector<docstring> defaults_;
133 mutable int lockCount_;
135 mutable bool redefinition_;
137 mutable MacroType type_;
141 /// A lookup table of macro definitions.
143 * This contains a table of "global" macros that are always accessible,
144 * either because they implement a feature of standard LaTeX or some
145 * hack to display certain contents nicely.
148 class MacroTable : public std::map<docstring, MacroData>
151 /// Parse full "\\def..." or "\\newcommand..." or ...
152 void insert(docstring const & definition, std::string const &);
153 /// Insert pre-digested macro definition
154 void insert(docstring const & name, MacroData const & data);
156 MacroData const * get(docstring const & name) const;
160 void getMacroNames(std::set<docstring> & names) const;
163 static MacroTable & globalMacros();
167 /// A context to lookup macros at a certain position in a buffer.
169 * The MacroContext is used during metrics calculation to resolve
170 * macro instances according to the position of them in the buffer
171 * document. Only macro definition in front of the macro instance
172 * are visible and are resolved.
177 /// construct context for the insets at pos
178 MacroContext(Buffer const & buf, DocIterator const & pos);
181 MacroData const * get(docstring const & name) const;
187 DocIterator const & pos_;