]> git.lyx.org Git - features.git/blob - src/mathed/MacroTable.cpp
'using namespace std' instead of 'using std::xxx'
[features.git] / src / mathed / MacroTable.cpp
1 /**
2  * \file MacroTable.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "MacroTable.h"
14 #include "MathMacroTemplate.h"
15 #include "MathMacroArgument.h"
16 #include "MathSupport.h"
17 #include "InsetMathSqrt.h"
18
19 #include "InsetMathNest.h"
20 #include "Buffer.h"
21
22 #include "support/debug.h"
23 #include "DocIterator.h"
24
25 #include <boost/assert.hpp>
26
27 #include <sstream>
28
29 using namespace std;
30
31 namespace lyx {
32
33
34 /////////////////////////////////////////////////////////////////////
35 //
36 // MacroData
37 //
38 /////////////////////////////////////////////////////////////////////
39
40 MacroData::MacroData()
41         : numargs_(0), lockCount_(0), redefinition_(false)
42 {}
43
44
45 MacroData::MacroData(docstring const & definition,
46                 std::vector<docstring> const & defaults, 
47                 int numargs, int optionals, docstring const & display,
48                 string const & requires)
49         : definition_(definition), numargs_(numargs), display_(display),
50                 requires_(requires), lockCount_(0), redefinition_(false),
51                 optionals_(optionals), defaults_(defaults)
52 {
53         defaults_.resize(optionals);
54 }
55
56
57 void MacroData::expand(vector<MathData> const & args, MathData & to) const
58 {
59         InsetMathSqrt inset; // Hack. Any inset with a cell would do.
60         // FIXME UNICODE
61         asArray(display_.empty() ? definition_ : display_, inset.cell(0));
62         //lyxerr << "MathData::expand: args: " << args << endl;
63         //lyxerr << "MathData::expand: ar: " << inset.cell(0) << endl;
64         for (DocIterator it = doc_iterator_begin(inset); it; it.forwardChar()) {
65                 if (!it.nextInset())
66                         continue;
67                 if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
68                         continue;
69                 //it.cell().erase(it.pos());
70                 //it.cell().insert(it.pos(), it.nextInset()->asInsetMath()
71                 size_t n = static_cast<MathMacroArgument*>(it.nextInset())->number();
72                 if (n <= args.size()) {
73                         it.cell().erase(it.pos());
74                         it.cell().insert(it.pos(), args[n - 1]);
75                 }
76         }
77         //lyxerr << "MathData::expand: res: " << inset.cell(0) << endl;
78         to = inset.cell(0);
79 }
80
81
82 size_t MacroData::optionals() const
83 {
84         return optionals_;
85 }
86
87
88 std::vector<docstring> const &  MacroData::defaults() const
89 {
90         return defaults_;
91 }
92
93
94 void MacroData::unlock() const
95 {
96         --lockCount_;
97         BOOST_ASSERT(lockCount_ >= 0);
98 }
99
100
101 /////////////////////////////////////////////////////////////////////
102 //
103 // The global table of macros
104 //
105 /////////////////////////////////////////////////////////////////////
106
107 MacroTable & MacroTable::globalMacros()
108 {
109         static MacroTable theGlobalMacros;
110         return theGlobalMacros;
111 }
112
113
114 bool MacroTable::has(docstring const & name) const
115 {
116         return find(name) != end();
117 }
118
119
120 MacroData const & MacroTable::get(docstring const & name) const
121 {
122         const_iterator it = find(name);
123         BOOST_ASSERT(it != end());
124         return it->second;
125 }
126
127
128 void MacroTable::insert(docstring const & name, MacroData const & data)
129 {
130         //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
131         operator[](name) = data;
132 }
133
134
135 void MacroTable::insert(docstring const & def, string const & requires)
136 {
137         //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
138         MathMacroTemplate mac(def);
139         MacroData data = mac.asMacroData();
140         data.requires() = requires;
141         insert(mac.name(), data);
142 }
143
144
145 void MacroTable::dump()
146 {
147         lyxerr << "\n------------------------------------------" << endl;
148         for (const_iterator it = begin(); it != end(); ++it)
149                 lyxerr << to_utf8(it->first)
150                         << " [" << to_utf8(it->second.definition()) << "] : "
151                         << " [" << to_utf8(it->second.display()) << "] : "
152                         << endl;
153         lyxerr << "------------------------------------------" << endl;
154 }
155
156
157 MacroContext::MacroContext(Buffer const & buf, Paragraph const & par)
158         : buf_(buf), par_(par)
159 {
160 }
161
162
163 bool MacroContext::has(docstring const & name) const
164 {
165         // check if it's a local macro
166         if (macros_.has(name))
167                 return true;
168         
169         // otherwise ask the buffer
170         return buf_.hasMacro(name, par_);
171 }
172
173
174 MacroData const & MacroContext::get(docstring const & name) const
175 {
176         // check if it's a local macro
177         if (macros_.has(name))
178                 return macros_.get(name);
179         
180         // ask the buffer for its macros
181         return buf_.getMacro(name, par_);
182 }
183
184
185 void MacroContext::insert(docstring const & name, MacroData const & data)
186 {
187         macros_.insert(name, data);
188 }
189
190
191 } // namespace lyx