]> git.lyx.org Git - lyx.git/blob - src/mathed/MacroTable.h
Add missing revert routine to lyx_2_0.py
[lyx.git] / src / mathed / MacroTable.h
1 // -*- C++ -*-
2 /**
3  * \file MacroTable.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author André Pönitz
8  * \author Stefan Schimanski
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef MATH_MACROTABLE_H
14 #define MATH_MACROTABLE_H
15
16 #include "DocIterator.h"
17
18 #include "support/docstring.h"
19
20 #include <map>
21 #include <set>
22 #include <vector>
23
24 namespace lyx {
25
26 class Buffer;
27 class MathData;
28 class InsetMathMacroTemplate;
29 class latexkeys;
30
31 enum MacroType {
32         MacroTypeNewcommand,
33         MacroTypeNewcommandx,
34         MacroTypeDef
35 };
36
37 ///
38 class MacroData {
39 public:
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);
46
47         ///
48         docstring const & definition() const { updateData(); return definition_; }
49         ///
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
54         /// optional argument
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;
59         ///
60         std::vector<docstring> const & defaults() const;
61         ///
62         std::string const required() const;
63         ///
64         bool hidden() const;
65         ///
66         docstring const xmlname() const;
67         ///
68         char const * MathMLtype() const;
69         ///
70         latexkeys const * symbol() const { return sym_; }
71         ///
72         void setSymbol(latexkeys const * sym) { sym_ = sym; }
73         ///
74         DocIterator const & pos() const { return pos_; }
75
76         /// lock while being drawn to avoid recursions
77         int lock() const { return ++lockCount_; }
78         /// is it being drawn?
79         bool locked() const { return lockCount_ != 0; }
80         ///
81         void unlock() const;
82
83         ///
84         bool redefinition() const { updateData(); return redefinition_; }
85
86         ///
87         MacroType type() const { updateData(); return type_; }
88
89         /// output as TeX macro, only works for lazy MacroData!!!
90         int write(odocstream & os, bool overwriteRedefinition) const;
91
92         ///
93         bool operator==(MacroData const & x) const {
94                 updateData();
95                 x.updateData();
96                 return definition_ == x.definition_
97                         && numargs_ == x.numargs_
98                         && display_ == x.display_
99                         && sym_ == x.sym_
100                         && optionals_ == x.optionals_
101                         && defaults_ == x.defaults_;
102         }
103         ///
104         bool operator!=(MacroData const & x) const { return !operator==(x); }
105
106 private:
107         ///
108         void queryData(InsetMathMacroTemplate const & macro) const;
109         ///
110         void updateData() const;
111         ///
112         Buffer const * buffer_;
113         /// The position of the definition in the buffer.
114         /// There is no guarantee it stays valid if the buffer
115         /// changes. But it (normally) exists only until the
116         /// next Buffer::updateMacros call where new MacroData
117         /// objects are created for each macro definition.
118         /// In the worst case, it is invalidated and the MacroData
119         /// returns its defaults values and the user sees unfolded
120         /// macros.
121         mutable DocIterator pos_;
122         ///
123         mutable bool queried_ = false;
124         ///
125         mutable docstring definition_;
126         ///
127         mutable size_t numargs_ = 0;
128         ///
129         mutable docstring display_;
130         ///
131         latexkeys const * sym_ = nullptr;
132         ///
133         mutable size_t optionals_ = 0;
134         ///
135         mutable std::vector<docstring> defaults_;
136         ///
137         mutable int lockCount_ = 0;
138         ///
139         mutable bool redefinition_ = false;
140         ///
141         mutable MacroType type_ = MacroTypeNewcommand;
142 };
143
144
145 ///
146 class MacroNameSet : public std::set<docstring> {};
147 ///
148 class MacroSet : public std::set<MacroData const *> {};
149
150
151 /// A lookup table of macro definitions.
152 /**
153  * This contains a table of "global" macros that are always accessible,
154  * either because they implement a feature of standard LaTeX or some
155  * hack to display certain contents nicely.
156  *
157  **/
158 class MacroTable : public std::map<docstring, MacroData>
159 {
160 public:
161         /// Parse full "\\def..." or "\\newcommand..." or ...
162         iterator insert(Buffer * buf, docstring const & definition);
163         /// Insert pre-digested macro definition
164         iterator insert(docstring const & name, MacroData const & data);
165         ///
166         MacroData const * get(docstring const & name) const;
167         ///
168         void dump();
169         ///
170         void getMacroNames(std::set<docstring> & names, bool gethidden) const;
171
172         /// the global list
173         static MacroTable & globalMacros();
174 };
175
176
177 /// A context to lookup macros at a certain position in a buffer.
178 /**
179  * The MacroContext is used during metrics calculation to resolve
180  * macro instances according to the position of them in the buffer
181  * document. Only macro definition in front of the macro instance
182  * are visible and are resolved.
183  *
184  **/
185 class MacroContext {
186 public:
187         /// construct context for the insets at pos
188         MacroContext(Buffer const * buf, DocIterator const & pos);
189
190         /// Lookup macro
191         MacroData const * get(docstring const & name) const;
192
193 private:
194         ///
195         Buffer const * buf_;
196         ///
197         DocIterator const & pos_;
198 };
199
200 } // namespace lyx
201
202 #endif