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