]> git.lyx.org Git - lyx.git/blob - src/mathed/MathMacroTable.C
move everything into namespace lyx
[lyx.git] / src / mathed / MathMacroTable.C
1 /**
2  * \file MathMacroTable.C
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)
40 {}
41
42
43 MacroData::MacroData(string const & def, int numargs, string const & disp)
44         : def_(def), numargs_(numargs), disp_(disp)
45 {}
46
47
48 void MacroData::expand(vector<MathArray> const & args, MathArray & to) const
49 {
50         InsetMathSqrt inset; // Hack. Any inset with a cell would do.
51         // FIXME UNICODE
52         asArray(from_utf8(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 // The local table.
82 //MacroTable & MacroTable::localMacros()
83 //{
84 //      static MacroTable theLocalMacros;
85 //      return theLocalMacros;
86 //}
87
88
89 bool MacroTable::has(string const & name) const
90 {
91         return find(name) != end();
92 }
93
94
95 MacroData const & MacroTable::get(string const & name) const
96 {
97         const_iterator it = find(name);
98         BOOST_ASSERT(it != end());
99         return it->second;
100 }
101
102
103 void MacroTable::insert(string const & name, MacroData const & data)
104 {
105         //lyxerr << "MacroTable::insert: " << name << endl;
106         operator[](name) = data;
107 }
108
109
110 void MacroTable::insert(string const & def)
111 {
112         //lyxerr << "MacroTable::insert, def: " << def << endl;
113         istringstream is(def);
114         MathMacroTemplate mac(is);
115         insert(mac.name(), mac.asMacroData());
116 }
117
118
119 void MacroTable::dump()
120 {
121         lyxerr << "\n------------------------------------------" << endl;
122         for (const_iterator it = begin(); it != end(); ++it)
123                 lyxerr << it->first
124                         << " [" << it->second.def() << "] : "
125                         << " [" << it->second.disp() << "] : "
126                         << endl;
127         lyxerr << "------------------------------------------" << endl;
128 }
129
130
131 } // namespace lyx