]> git.lyx.org Git - lyx.git/blob - src/mathed/MathMacroTable.cpp
rename MathArray into MathData
[lyx.git] / src / mathed / MathMacroTable.cpp
1 /**
2  * \file MathMacroTable.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 "MathMacroTable.h"
14 #include "MathMacroTemplate.h"
15 #include "MathMacroArgument.h"
16 #include "MathSupport.h"
17 #include "InsetMathSqrt.h"
18
19 #include "debug.h"
20 #include "DocIterator.h"
21
22 #include <boost/assert.hpp>
23
24 #include <sstream>
25
26
27 namespace lyx {
28
29 using std::endl;
30 using std::istringstream;
31 using std::map;
32 using std::pair;
33 using std::string;
34 using std::vector;
35 using std::size_t;
36
37
38 MacroData::MacroData()
39         : numargs_(0), lockCount_(0)
40 {}
41
42
43 MacroData::MacroData(docstring const & def, int numargs, docstring const & disp, string const & requires)
44         : def_(def), numargs_(numargs), disp_(disp), requires_(requires), lockCount_(0)
45 {}
46
47
48 void MacroData::expand(vector<MathData> const & args, MathData & to) const
49 {
50         InsetMathSqrt inset; // Hack. Any inset with a cell would do.
51         // FIXME UNICODE
52         asArray(disp_.empty() ? def_ : disp_, inset.cell(0));
53         //lyxerr << "MathData::expand: args: " << args << endl;
54         //lyxerr << "MathData::expand: ar: " << inset.cell(0) << endl;
55         for (DocIterator it = doc_iterator_begin(inset); it; it.forwardChar()) {
56                 if (!it.nextInset())
57                         continue;
58                 if (it.nextInset()->lyxCode() != InsetBase::MATHMACROARG_CODE)
59                         continue;
60                 //it.cell().erase(it.pos());
61                 //it.cell().insert(it.pos(), it.nextInset()->asInsetMath()
62                 size_t n = static_cast<MathMacroArgument*>(it.nextInset())->number();
63                 if (n <= args.size()) {
64                         it.cell().erase(it.pos());
65                         it.cell().insert(it.pos(), args[n - 1]);
66                 }
67         }
68         //lyxerr << "MathData::expand: res: " << inset.cell(0) << endl;
69         to = inset.cell(0);
70 }
71
72
73 // The global table.
74 MacroTable & MacroTable::globalMacros()
75 {
76         static MacroTable theGlobalMacros;
77         return theGlobalMacros;
78 }
79
80
81 bool MacroTable::has(docstring const & name) const
82 {
83         return find(name) != end();
84 }
85
86
87 MacroData const & MacroTable::get(docstring const & name) const
88 {
89         const_iterator it = find(name);
90         BOOST_ASSERT(it != end());
91         return it->second;
92 }
93
94
95 void MacroTable::insert(docstring const & name, MacroData const & data)
96 {
97         //lyxerr << "MacroTable::insert: " << to_utf8(name) << endl;
98         operator[](name) = data;
99 }
100
101
102 void MacroTable::insert(docstring const & def, string const & requires)
103 {
104         //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
105         MathMacroTemplate mac(def);
106         MacroData data = mac.asMacroData();
107         data.requires() = requires;
108         insert(mac.name(), data);
109 }
110
111
112 void MacroTable::dump()
113 {
114         lyxerr << "\n------------------------------------------" << endl;
115         for (const_iterator it = begin(); it != end(); ++it)
116                 lyxerr << to_utf8(it->first)
117                         << " [" << to_utf8(it->second.def()) << "] : "
118                         << " [" << to_utf8(it->second.disp()) << "] : "
119                         << endl;
120         lyxerr << "------------------------------------------" << endl;
121 }
122
123
124 } // namespace lyx