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