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;
41 /// Constructor to make STL containers happy
42 explicit MacroData(Buffer * buf = 0);
43 /// Create lazy MacroData which only queries the macro template when needed
44 MacroData(Buffer * buf, DocIterator const & pos);
45 /// Create non-lazy MacroData which directly queries the macro template
46 MacroData(Buffer * buf, InsetMathMacroTemplate const & macro);
49 docstring const & definition() const { updateData(); return definition_; }
51 docstring const & display() const { updateData(); return display_; }
52 /// arity including optional arguments (if there is any)
53 size_t numargs() const { updateData(); return numargs_; }
54 /// replace #1,#2,... by given MathAtom 0,1,.., _including_ the possible
56 /// \return whether anything was expanded
57 bool expand(std::vector<MathData> const & from, MathData & to) const;
58 /// number of optional arguments
59 size_t optionals() const;
61 std::vector<docstring> const & defaults() const;
63 std::string const required() const;
67 docstring const htmlname() const;
69 docstring const xmlname() const;
71 char const * MathMLtype() const;
73 latexkeys const * symbol() const { return sym_; }
75 void setSymbol(latexkeys const * sym) { sym_ = sym; }
77 DocIterator const & pos() { return pos_; }
79 /// lock while being drawn to avoid recursions
80 int lock() const { return ++lockCount_; }
81 /// is it being drawn?
82 bool locked() const { return lockCount_ != 0; }
87 bool redefinition() const { updateData(); return redefinition_; }
90 MacroType type() const { updateData(); return type_; }
92 /// output as TeX macro, only works for lazy MacroData!!!
93 int write(odocstream & os, bool overwriteRedefinition) const;
96 bool operator==(MacroData const & x) const {
99 return definition_ == x.definition_
100 && numargs_ == x.numargs_
101 && display_ == x.display_
103 && optionals_ == x.optionals_
104 && defaults_ == x.defaults_;
107 bool operator!=(MacroData const & x) const { return !operator==(x); }
111 void queryData(InsetMathMacroTemplate const & macro) const;
113 void updateData() const;
115 Buffer const * buffer_;
116 /// The position of the definition in the buffer.
117 /// There is no guarantee it stays valid if the buffer
118 /// changes. But it (normally) exists only until the
119 /// next Buffer::updateMacros call where new MacroData
120 /// objects are created for each macro definition.
121 /// In the worst case, it is invalidated and the MacroData
122 /// returns its defaults values and the user sees unfolded
124 mutable DocIterator pos_;
126 mutable bool queried_;
128 mutable docstring definition_;
130 mutable size_t numargs_;
132 mutable docstring display_;
134 latexkeys const * sym_;
136 mutable size_t optionals_;
138 mutable std::vector<docstring> defaults_;
140 mutable int lockCount_;
142 mutable bool redefinition_;
144 mutable MacroType type_;
149 class MacroNameSet : public std::set<docstring> {};
151 class MacroSet : public std::set<MacroData const *> {};
154 /// A lookup table of macro definitions.
156 * This contains a table of "global" macros that are always accessible,
157 * either because they implement a feature of standard LaTeX or some
158 * hack to display certain contents nicely.
161 class MacroTable : public std::map<docstring, MacroData>
164 /// Parse full "\\def..." or "\\newcommand..." or ...
165 iterator insert(Buffer * buf, docstring const & definition);
166 /// Insert pre-digested macro definition
167 iterator insert(docstring const & name, MacroData const & data);
169 MacroData const * get(docstring const & name) const;
173 void getMacroNames(std::set<docstring> & names, bool gethidden) const;
176 static MacroTable & globalMacros();
180 /// A context to lookup macros at a certain position in a buffer.
182 * The MacroContext is used during metrics calculation to resolve
183 * macro instances according to the position of them in the buffer
184 * document. Only macro definition in front of the macro instance
185 * are visible and are resolved.
190 /// construct context for the insets at pos
191 MacroContext(Buffer const * buf, DocIterator const & pos);
194 MacroData const * get(docstring const & name) const;
200 DocIterator const & pos_;