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