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