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