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