]> git.lyx.org Git - lyx.git/blob - src/mathed/MacroTable.h
1c9decc3e7bef8adb43a91db5fafcbb4173acd84
[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 MathMacroTemplate;
29 class Paragraph;
30 class latexkeys;
31
32 enum MacroType {
33         MacroTypeNewcommand,
34         MacroTypeNewcommandx,
35         MacroTypeDef
36 };
37
38 ///
39 class MacroData {
40 public:
41         /// Constructor to make STL containers happy
42         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, MathMacroTemplate const & macro);
47
48         ///
49         docstring const & definition() const { updateData(); return definition_; }
50         ///
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
55         /// optional argument
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;
60         ///
61         std::vector<docstring> const & defaults() const;
62         ///
63         std::string const requires() const;
64         ///
65         void setSymbol(latexkeys const * sym) { sym_ = sym; }
66
67         /// lock while being drawn to avoid recursions
68         int lock() const { return ++lockCount_; }
69         /// is it being drawn?
70         bool locked() const { return lockCount_ != 0; }
71         ///
72         void unlock() const;
73
74         ///
75         bool redefinition() const { updateData(); return redefinition_; }
76
77         ///
78         MacroType type() const { updateData(); return type_; }
79
80         /// output as TeX macro, only works for lazy MacroData!!!
81         int write(odocstream & os, bool overwriteRedefinition) const;
82
83         ///
84         bool operator==(MacroData const & x) const {
85                 updateData();
86                 x.updateData();
87                 return definition_ == x.definition_
88                         && numargs_ == x.numargs_
89                         && display_ == x.display_
90                         && sym_ == x.sym_
91                         && optionals_ == x.optionals_
92                         && defaults_ == x.defaults_;
93         }
94         ///
95         bool operator!=(MacroData const & x) const { return !operator==(x); }
96
97 private:
98         ///
99         void queryData(MathMacroTemplate const & macro) const;
100         ///
101         void updateData() const;
102         ///
103         Buffer const * buffer_;
104         /// The position of the definition in the buffer.
105         /// There is no guarantee it stays valid if the buffer
106         /// changes. But it (normally) exists only until the
107         /// next Buffer::updateMacros call where new MacroData
108         /// objects are created for each macro definition.
109         /// In the worst case, it is invalidated and the MacroData
110         /// returns its defaults values and the user sees unfolded
111         /// macros.
112         mutable DocIterator pos_;
113         ///
114         mutable bool queried_;
115         ///
116         mutable docstring definition_;
117         ///
118         mutable size_t numargs_;
119         ///
120         mutable docstring display_;
121         ///
122         latexkeys const * sym_;
123         ///
124         mutable size_t optionals_;
125         ///
126         mutable std::vector<docstring> defaults_;
127         ///
128         mutable int lockCount_;
129         ///
130         mutable bool redefinition_;
131         ///
132         mutable MacroType type_;
133 };
134
135
136 ///
137 class MacroNameSet : public std::set<docstring> {};
138 ///
139 class MacroSet : public std::set<MacroData const *> {};
140
141
142 /// A lookup table of macro definitions.
143 /**
144  * This contains a table of "global" macros that are always accessible,
145  * either because they implement a feature of standard LaTeX or some
146  * hack to display certain contents nicely.
147  *
148  **/
149 class MacroTable : public std::map<docstring, MacroData>
150 {
151 public:
152         /// Parse full "\\def..." or "\\newcommand..." or ...
153         iterator insert(Buffer * buf, docstring const & definition);
154         /// Insert pre-digested macro definition
155         iterator insert(docstring const & name, MacroData const & data);
156         ///
157         MacroData const * get(docstring const & name) const;
158         ///
159         void dump();
160         ///
161         void getMacroNames(std::set<docstring> & names) const;
162
163         /// the global list
164         static MacroTable & globalMacros();
165 };
166
167
168 /// A context to lookup macros at a certain position in a buffer.
169 /**
170  * The MacroContext is used during metrics calculation to resolve
171  * macro instances according to the position of them in the buffer
172  * document. Only macro definition in front of the macro instance
173  * are visible and are resolved.
174  *
175  **/
176 class MacroContext {
177 public:
178         /// construct context for the insets at pos
179         MacroContext(Buffer const * buf, DocIterator const & pos);
180
181         /// Lookup macro
182         MacroData const * get(docstring const & name) const;
183
184 private:
185         ///
186         Buffer const * buf_;
187         ///
188         DocIterator const & pos_;
189 };
190
191 } // namespace lyx
192
193 #endif