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