]> git.lyx.org Git - lyx.git/blob - src/mathed/MacroTable.cpp
add Buffer * member to DocIterator
[lyx.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 "InsetMathSqrt.h"
14 #include "MacroTable.h"
15 #include "MathMacroTemplate.h"
16 #include "MathMacroArgument.h"
17 #include "MathStream.h"
18 #include "MathSupport.h"
19 #include "InsetMathNest.h"
20
21 #include "Buffer.h"
22 #include "DocIterator.h"
23 #include "InsetList.h"
24 #include "Text.h"
25
26 #include "support/debug.h"
27
28 #include "support/lassert.h"
29
30 #include <sstream>
31
32 using namespace std;
33
34 namespace lyx {
35
36
37 /////////////////////////////////////////////////////////////////////
38 //
39 // MacroData
40 //
41 /////////////////////////////////////////////////////////////////////
42
43 MacroData::MacroData()
44         : queried_(true), numargs_(0), optionals_(0), lockCount_(0),
45           redefinition_(false), type_(MacroTypeNewcommand)
46 {}
47
48         
49         
50 MacroData::MacroData(Buffer const & buf, DocIterator const & pos)
51         : buffer_(&buf), pos_(pos), queried_(false), numargs_(0),
52           optionals_(0), lockCount_(0), redefinition_(false),
53           type_(MacroTypeNewcommand)
54 {
55 }
56         
57         
58 MacroData::MacroData(MathMacroTemplate const & macro)
59         : queried_(false), numargs_(0), optionals_(0), lockCount_(0),
60           redefinition_(false), type_(MacroTypeNewcommand)
61 {
62         queryData(macro);
63 }
64
65
66 void MacroData::expand(vector<MathData> const & args, MathData & to) const
67 {
68         updateData();
69
70         // Hack. Any inset with a cell would do.
71         static Buffer buffer("");
72         static InsetMathSqrt inset;
73
74         // FIXME UNICODE
75         asArray(display_.empty() ? definition_ : display_, inset.cell(0));
76         //lyxerr << "MathData::expand: args: " << args << endl;
77         //lyxerr << "MathData::expand: ar: " << inset.cell(0) << endl;
78         for (DocIterator it = doc_iterator_begin(&buffer, &inset); it; it.forwardChar()) {
79                 if (!it.nextInset())
80                         continue;
81                 if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
82                         continue;
83                 //it.cell().erase(it.pos());
84                 //it.cell().insert(it.pos(), it.nextInset()->asInsetMath()
85                 size_t n = static_cast<MathMacroArgument*>(it.nextInset())->number();
86                 if (n <= args.size()) {
87                         it.cell().erase(it.pos());
88                         it.cell().insert(it.pos(), args[n - 1]);
89                 }
90         }
91         //lyxerr << "MathData::expand: res: " << inset.cell(0) << endl;
92         to = inset.cell(0);
93 }
94
95
96 size_t MacroData::optionals() const
97 {
98         updateData();
99         return optionals_;
100 }
101
102
103 vector<docstring> const &  MacroData::defaults() const
104 {
105         updateData();
106         return defaults_;
107 }
108
109
110 void MacroData::unlock() const
111 {
112         --lockCount_;
113         LASSERT(lockCount_ >= 0, /**/);
114 }
115
116
117 void MacroData::queryData(MathMacroTemplate const & macro) const
118 {
119         if (queried_)
120                 return;
121
122         queried_ = true;
123         definition_ = macro.definition();
124         numargs_ = macro.numArgs();
125         display_ = macro.displayDefinition();
126         redefinition_ = macro.redefinition();
127         type_ = macro.type();
128         optionals_ = macro.numOptionals();
129         
130         macro.getDefaults(defaults_);
131 }
132
133
134 void MacroData::updateData() const
135 {
136         if (queried_)
137                 return;
138
139         LASSERT(buffer_ != 0, /**/);
140         
141         // Try to fix position DocIterator. Should not do anything in theory.
142         pos_.fixIfBroken();
143         
144         // find macro template
145         Inset * inset = pos_.nextInset();
146         if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
147                 lyxerr << "BUG: No macro template found by MacroData" << endl;
148                 return;
149         }
150         
151         // query the data from the macro template
152         queryData(static_cast<MathMacroTemplate const &>(*inset));      
153 }
154         
155
156 void MacroData::write(odocstream & os, bool overwriteRedefinition) const
157 {
158         updateData();
159
160         // find macro template
161         Inset * inset = pos_.nextInset();
162         if (inset == 0 || inset->lyxCode() != MATHMACRO_CODE) {
163                 lyxerr << "BUG: No macro template found by MacroData" << endl;
164                 return;
165         }
166                 
167         // output template
168         MathMacroTemplate const & tmpl =
169                 static_cast<MathMacroTemplate const &>(*inset);
170         WriteStream wi(os, false, true, false);
171         tmpl.write(wi, overwriteRedefinition);
172 }
173
174
175 /////////////////////////////////////////////////////////////////////
176 //
177 // The global table of macros
178 //
179 /////////////////////////////////////////////////////////////////////
180
181 MacroTable & MacroTable::globalMacros()
182 {
183         static MacroTable theGlobalMacros;
184         return theGlobalMacros;
185 }
186
187
188 MacroData const * MacroTable::get(docstring const & name) const
189 {
190         const_iterator it = find(name);
191         return it == end() ? 0 : &it->second;
192 }
193
194
195 void MacroTable::insert(docstring const & name, MacroData const & data)
196 {
197         //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
198         operator[](name) = data;
199 }
200
201
202 void MacroTable::insert(docstring const & def, string const & requires)
203 {
204         //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
205         MathMacroTemplate mac(def);
206         MacroData data(mac);
207         data.requires() = requires;
208         insert(mac.name(), data);
209 }
210
211
212 void MacroTable::getMacroNames(std::set<docstring> & names) const
213 {
214         for (const_iterator it = begin(); it != end(); ++it)
215                 names.insert(it->first);
216 }
217
218
219 void MacroTable::dump()
220 {
221         lyxerr << "\n------------------------------------------" << endl;
222         for (const_iterator it = begin(); it != end(); ++it)
223                 lyxerr << to_utf8(it->first)
224                         << " [" << to_utf8(it->second.definition()) << "] : "
225                         << " [" << to_utf8(it->second.display()) << "] : "
226                         << endl;
227         lyxerr << "------------------------------------------" << endl;
228 }
229
230
231 /////////////////////////////////////////////////////////////////////
232 //
233 // MacroContext
234 //
235 /////////////////////////////////////////////////////////////////////
236
237 MacroContext::MacroContext(Buffer const & buf, DocIterator const & pos)
238         : buf_(buf), pos_(pos)
239 {
240 }
241
242
243 MacroData const * MacroContext::get(docstring const & name) const
244 {
245         return buf_.getMacro(name, pos_);
246 }
247
248 } // namespace lyx