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