]> git.lyx.org Git - lyx.git/blob - src/mathed/MacroTable.h
709fd3b61fcc35a80f09ae7b0994d97b2286bf78
[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 "support/docstring.h"
17
18 #include <map>
19 #include <vector>
20
21 namespace lyx {
22
23 class Buffer;
24 class MathData;
25 class Paragraph;
26
27 ///
28 class MacroData {
29 public:
30         ///
31         MacroData();
32         ///
33         MacroData(docstring const & definition,
34                 std::vector<docstring> const & defaults, int nargs, int optionals, 
35                 docstring const & display, std::string const & requires);
36         ///
37         docstring const & definition() const { return definition_; }
38         ///
39         docstring const & display() const { return display_; }
40         /// arity including optional arguments (if there is any)
41         size_t numargs() const { return numargs_; }
42         /// replace #1,#2,... by given MathAtom 0,1,.., _including_ the possible
43         /// optional argument
44         void expand(std::vector<MathData> const & from, MathData & to) const;
45         /// number of optional arguments
46         size_t optionals() const;
47         ///
48         std::vector<docstring> const & defaults() const;
49         ///
50         std::string requires() const { return requires_; }
51         ///
52         std::string & requires() { return requires_; }
53         
54         /// lock while being drawn to avoid recursions
55         int lock() const { return ++lockCount_; }
56         /// is it being drawn?
57         bool locked() const { return lockCount_ != 0; }
58         ///
59         void unlock() const;
60         
61         ///
62         bool redefinition() const { return redefinition_; }
63         ///
64         void setRedefinition(bool redefined) { redefinition_ = redefined; }
65         
66         ///
67         bool operator==(MacroData const & x) const {
68                 return definition_ == x.definition_ 
69                         && numargs_ == x.numargs_
70                         && display_ == x.display_
71                         && requires_ == x.requires_
72                         && optionals_ == x.optionals_
73                         && defaults_ == x.defaults_;
74         }
75         ///
76         bool operator!=(MacroData const & x) const { return !operator==(x); }
77
78 private:
79         ///
80         docstring definition_;
81         ///
82         size_t numargs_;
83         ///
84         docstring display_;
85         ///
86         std::string requires_;
87         ///
88         mutable int lockCount_;
89         ///
90         bool redefinition_;
91         ///
92         size_t optionals_;
93         ///
94         std::vector<docstring> defaults_;
95 };
96
97
98 /// A lookup table of macro definitions.
99 /**
100  * This contains a table of "global" macros that are always accessible,
101  * either because they implement a feature of standard LaTeX or some
102  * hack to display certain contents nicely.
103  *
104  **/
105 class MacroTable : public std::map<docstring, MacroData>
106 {
107 public:
108         /// Parse full "\\def..." or "\\newcommand..." or ...
109         void insert(docstring const & definition, std::string const &);
110         /// Insert pre-digested macro definition
111         void insert(docstring const & name, MacroData const & data);
112         /// Do we have a macro by that name?
113         bool has(docstring const & name) const;
114         ///
115         MacroData const & get(docstring const & name) const;
116         ///
117         void dump();
118
119         /// the global list
120         static MacroTable & globalMacros();
121 };
122
123
124 /// A context to lookup macros at a certain position in a buffer.
125 /**
126  * The MacroContext is used during metrics calculation to resolve
127  * macro instances according to the position of them in the buffer
128  * document. Only macro definition in front of the macro instance
129  * are visible and are resolved.
130  *
131  **/
132 class MacroContext {
133 public:
134         /// construct context for insets in par (not including the ones
135         /// defined in par itself)
136         MacroContext(Buffer const & buf, Paragraph const & par);
137         
138         /// Look for macro
139         bool has(docstring const & name) const;
140         /// Lookup macro
141         MacroData const & get(docstring const & name) const;
142         
143         /// Insert pre-digested macro definition
144         void insert(docstring const & name, MacroData const & data);
145         
146 private:
147         /// context local macros
148         MacroTable macros_;
149         ///
150         Buffer const & buf_;
151         ///
152         Paragraph const & par_;
153 };
154
155 } // namespace lyx
156
157 #endif