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 "support/docstring.h"
18 #include <boost/assert.hpp>
35 MacroData(docstring const & definition, std::vector<docstring> const & defaults,
36 int nargs, int optionals,
37 docstring const & display, std::string const & requires);
39 docstring const & definition() const { return definition_; }
41 docstring const & display() const { return display_; }
42 /// arity including optional arguments (if there is any)
43 int numargs() const { return numargs_; }
44 /// replace #1,#2,... by given MathAtom 0,1,.., _including_ the possible optional argument
45 void expand(std::vector<MathData> const & from, MathData & to) const;
46 /// number of optional arguments
47 int optionals() const;
49 std::vector<docstring> const & defaults() const;
51 std::string requires() const { return requires_; }
53 std::string & requires() { return requires_; }
55 /// lock while being drawn to avoid recursions
56 int lock() const { return ++lockCount_; }
57 /// is it being drawn?
58 bool locked() const { return lockCount_ != 0; }
60 void unlock() const { --lockCount_; BOOST_ASSERT(lockCount_ >= 0); }
63 bool redefinition() const { return redefinition_; }
65 void setRedefinition(bool redefined) { redefinition_ = redefined; }
68 bool operator==(MacroData const & x) const {
69 return definition_ == x.definition_
70 && numargs_ == x.numargs_
71 && display_ == x.display_
72 && requires_ == x.requires_
73 && optionals_ == x.optionals_
74 && defaults_ == x.defaults_;
77 bool operator!=(MacroData const & x) const { return !operator==(x); }
81 docstring definition_;
87 std::string requires_;
89 mutable int lockCount_;
95 std::vector<docstring> defaults_;
99 /// A lookup table of macro definitions.
101 * This contains a table of "global" macros that are always accessible,
102 * either because they implement a feature of standard LaTeX or some
103 * hack to display certain contents nicely.
106 class MacroTable : public std::map<docstring, MacroData>
109 /// Parse full "\\def..." or "\\newcommand..." or ...
110 void insert(docstring const & definition, std::string const &);
111 /// Insert pre-digested macro definition
112 void insert(docstring const & name, MacroData const & data);
113 /// Do we have a macro by that name?
114 bool has(docstring const & name) const;
116 MacroData const & get(docstring const & name) const;
121 static MacroTable & globalMacros();
125 /// A context to lookup macros at a certain position in a buffer.
127 * The MacroContext is used during metrics calculation to resolve
128 * macro instances according to the position of them in the buffer
129 * document. Only macro definition in front of the macro instance
130 * are visible and are resolved.
135 /// construct context for insets in par (not including the ones defined in par itself)
136 MacroContext(Buffer const & buf, Paragraph const & par);
139 bool has(docstring const & name) const;
141 MacroData const & get(docstring const & name) const;
143 /// Insert pre-digested macro definition
144 void insert(docstring const & name, MacroData const & data);
147 /// context local macros
152 Paragraph const & par_;