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 InsetMathMacroTemplate;
40 /// Constructor to make STL containers happy
41 explicit MacroData(Buffer const * buf = 0);
42 /// Create lazy MacroData which only queries the macro template when needed
43 MacroData(Buffer const * buf, DocIterator const & pos);
44 /// Create non-lazy MacroData which directly queries the macro template
45 MacroData(Buffer const * buf, InsetMathMacroTemplate 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 /// \return whether anything was expanded
56 bool expand(std::vector<MathData> const & from, MathData & to) const;
57 /// number of optional arguments
58 size_t optionals() const;
60 std::vector<docstring> const & defaults() const;
62 std::string const required() const;
66 docstring const htmlname() const;
68 docstring const xmlname() const;
70 char const * MathMLtype() const;
72 latexkeys const * symbol() const { return sym_; }
74 void setSymbol(latexkeys const * sym) { sym_ = sym; }
76 DocIterator const & pos() const { return pos_; }
78 /// lock while being drawn to avoid recursions
79 int lock() const { return ++lockCount_; }
80 /// is it being drawn?
81 bool locked() const { return lockCount_ != 0; }
86 bool redefinition() const { updateData(); return redefinition_; }
89 MacroType type() const { updateData(); return type_; }
91 /// output as TeX macro, only works for lazy MacroData!!!
92 int write(odocstream & os, bool overwriteRedefinition) const;
95 bool operator==(MacroData const & x) const {
98 return definition_ == x.definition_
99 && numargs_ == x.numargs_
100 && display_ == x.display_
102 && optionals_ == x.optionals_
103 && defaults_ == x.defaults_;
106 bool operator!=(MacroData const & x) const { return !operator==(x); }
110 void queryData(InsetMathMacroTemplate const & macro) const;
112 void updateData() const;
114 Buffer const * buffer_;
115 /// The position of the definition in the buffer.
116 /// There is no guarantee it stays valid if the buffer
117 /// changes. But it (normally) exists only until the
118 /// next Buffer::updateMacros call where new MacroData
119 /// objects are created for each macro definition.
120 /// In the worst case, it is invalidated and the MacroData
121 /// returns its defaults values and the user sees unfolded
123 mutable DocIterator pos_;
125 mutable bool queried_ = false;
127 mutable docstring definition_;
129 mutable size_t numargs_ = 0;
131 mutable docstring display_;
133 latexkeys const * sym_ = nullptr;
135 mutable size_t optionals_ = 0;
137 mutable std::vector<docstring> defaults_;
139 mutable int lockCount_ = 0;
141 mutable bool redefinition_ = false;
143 mutable MacroType type_ = MacroTypeNewcommand;
148 class MacroNameSet : public std::set<docstring> {};
150 class MacroSet : public std::set<MacroData const *> {};
153 /// A lookup table of macro definitions.
155 * This contains a table of "global" macros that are always accessible,
156 * either because they implement a feature of standard LaTeX or some
157 * hack to display certain contents nicely.
160 class MacroTable : public std::map<docstring, MacroData>
163 /// Parse full "\\def..." or "\\newcommand..." or ...
164 iterator insert(Buffer * buf, docstring const & definition);
165 /// Insert pre-digested macro definition
166 iterator insert(docstring const & name, MacroData const & data);
168 MacroData const * get(docstring const & name) const;
172 void getMacroNames(std::set<docstring> & names, bool gethidden) const;
175 static MacroTable & globalMacros();
179 /// A context to lookup macros at a certain position in a buffer.
181 * The MacroContext is used during metrics calculation to resolve
182 * macro instances according to the position of them in the buffer
183 * document. Only macro definition in front of the macro instance
184 * are visible and are resolved.
189 /// construct context for the insets at pos
190 MacroContext(Buffer const * buf, DocIterator const & pos);
193 MacroData const * get(docstring const & name) const;
199 DocIterator const & pos_;