]> git.lyx.org Git - lyx.git/blob - src/mathed/MathMacroTable.C
cursor is no more damaging the background. L-shaped cursor is broken right now....
[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(docstring const & def, int numargs, docstring 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(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)
103 {
104         //lyxerr << "MacroTable::insert, def: " << to_utf8(def) << endl;
105         MathMacroTemplate mac(def);
106         insert(mac.name(), mac.asMacroData());
107 }
108
109
110 void MacroTable::dump()
111 {
112         lyxerr << "\n------------------------------------------" << endl;
113         for (const_iterator it = begin(); it != end(); ++it)
114                 lyxerr << to_utf8(it->first)
115                         << " [" << to_utf8(it->second.def()) << "] : "
116                         << " [" << to_utf8(it->second.disp()) << "] : "
117                         << endl;
118         lyxerr << "------------------------------------------" << endl;
119 }
120
121
122 } // namespace lyx