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