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