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