]> git.lyx.org Git - lyx.git/blob - src/mathed/MacroTable.h
Add doxy.
[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 MacroNameSet : public std::set<docstring> {};
40         
41 ///
42 class MacroData {
43 public:
44         /// Constructor to make STL containers happy
45         MacroData();
46         /// Create lazy MacroData which only queries the macro template when needed
47         MacroData(Buffer const & buf, DocIterator const & pos);
48         /// Create non-lazy MacroData which directly queries the macro template
49         MacroData(MathMacroTemplate const & macro);
50
51         ///
52         docstring const & definition() const { updateData(); return definition_; }
53         ///
54         docstring const & display() const { updateData(); return display_; }
55         /// arity including optional arguments (if there is any)
56         size_t numargs() const { updateData(); return numargs_; }
57         /// replace #1,#2,... by given MathAtom 0,1,.., _including_ the possible
58         /// optional argument
59         void expand(std::vector<MathData> const & from, MathData & to) const;
60         /// number of optional arguments
61         size_t optionals() const;
62         ///
63         std::vector<docstring> const & defaults() const;
64         ///
65         std::string const & requires() const { updateData(); return requires_; }
66         ///
67         std::string & requires() { updateData(); return requires_; }
68         
69         /// lock while being drawn to avoid recursions
70         int lock() const { return ++lockCount_; }
71         /// is it being drawn?
72         bool locked() const { return lockCount_ != 0; }
73         ///
74         void unlock() const;
75         
76         ///
77         bool redefinition() const { return redefinition_; }
78         ///
79         void setRedefinition(bool redefined) { redefinition_ = redefined; }
80
81         ///
82         MacroType type() const { return type_; }
83         ///
84         MacroType & type() { return type_; }
85         
86         /// output as TeX macro, only works for lazy MacroData!!!
87         void 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                         && requires_ == x.requires_
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         mutable std::string requires_;
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 /// A lookup table of macro definitions.
143 /**
144  * This contains a table of "global" macros that are always accessible,
145  * either because they implement a feature of standard LaTeX or some
146  * hack to display certain contents nicely.
147  *
148  **/
149 class MacroTable : public std::map<docstring, MacroData>
150 {
151 public:
152         /// Parse full "\\def..." or "\\newcommand..." or ...
153         void insert(docstring const & definition, std::string const &);
154         /// Insert pre-digested macro definition
155         void insert(docstring const & name, MacroData const & data);
156         ///
157         MacroData const * get(docstring const & name) const;
158         ///
159         void dump();
160         ///
161         void getMacroNames(std::set<docstring> & names) const;
162
163         /// the global list
164         static MacroTable & globalMacros();
165 };
166
167
168 /// A context to lookup macros at a certain position in a buffer.
169 /**
170  * The MacroContext is used during metrics calculation to resolve
171  * macro instances according to the position of them in the buffer
172  * document. Only macro definition in front of the macro instance
173  * are visible and are resolved.
174  *
175  **/
176 class MacroContext {
177 public:
178         /// construct context for the insets at pos
179         MacroContext(Buffer const & buf, DocIterator const & pos);
180         
181         /// Lookup macro
182         MacroData const * get(docstring const & name) const;
183         
184 private:
185         ///
186         Buffer const & buf_;
187         ///
188         DocIterator const & pos_;
189 };
190
191 } // namespace lyx
192
193 #endif