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