]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotable.C
Remove mixed_content from output parameters.
[lyx.git] / src / mathed / math_macrotable.C
1 /**
2  * \file math_macrotable.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 "math_macrotable.h"
14 #include "math_macrotemplate.h"
15 #include "math_macroarg.h"
16 #include "math_support.h"
17 #include "math_sqrtinset.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         MathSqrtInset inset; // Hack. Any inset with a cell would do.
48         asArray(disp_.empty() ? def_ : disp_, inset.cell(0));
49         //lyxerr << "MathData::expand: args: " << args << endl;
50         //lyxerr << "MathData::expand: ar: " << inset.cell(0) << endl;
51         for (DocIterator it = doc_iterator_begin(inset); it; it.forwardChar()) {
52                 if (!it.nextInset())
53                         continue;
54                 if (it.nextInset()->lyxCode() != InsetBase::MATHMACROARG_CODE)
55                         continue;
56                 //it.cell().erase(it.pos());
57                 //it.cell().insert(it.pos(), it.nextInset()->asMathInset()
58                 size_t n = static_cast<MathMacroArgument*>(it.nextInset())->number();
59                 if (n <= args.size()) {
60                         it.cell().erase(it.pos());
61                         it.cell().insert(it.pos(), args[n - 1]);
62                 }
63         }
64         //lyxerr << "MathData::expand: res: " << inset.cell(0) << endl;
65         to = inset.cell(0);
66 }
67
68
69 // The global table.
70 MacroTable & MacroTable::globalMacros()
71 {
72         static MacroTable theGlobalMacros;
73         return theGlobalMacros;
74 }
75
76
77 // The local table.
78 //MacroTable & MacroTable::localMacros()
79 //{
80 //      static MacroTable theLocalMacros;
81 //      return theLocalMacros;
82 //}
83
84
85 bool MacroTable::has(string const & name) const
86 {
87         return find(name) != end();
88 }
89
90
91 MacroData const & MacroTable::get(string const & name) const
92 {
93         const_iterator it = find(name);
94         BOOST_ASSERT(it != end());
95         return it->second;
96 }
97
98
99 void MacroTable::insert(string const & name, MacroData const & data)
100 {
101         //lyxerr << "MacroTable::insert: " << name << endl;
102         operator[](name) = data;
103 }
104
105
106 void MacroTable::insert(string const & def)
107 {
108         //lyxerr << "MacroTable::insert, def: " << def << endl;
109         istringstream is(def);
110         MathMacroTemplate mac(is);
111         insert(mac.name(), mac.asMacroData());
112 }
113
114
115 void MacroTable::dump()
116 {
117         lyxerr << "\n------------------------------------------" << endl;
118         for (const_iterator it = begin(); it != end(); ++it)
119                 lyxerr << it->first
120                         << " [" << it->second.def() << "] : "
121                         << " [" << it->second.disp() << "] : "
122                         << endl;
123         lyxerr << "------------------------------------------" << endl;
124 }