]> git.lyx.org Git - lyx.git/blob - src/mathed/math_macrotable.C
macro rework
[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 "support/std_sstream.h"
23
24 #include <boost/assert.hpp>
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
33
34 MacroData::MacroData() 
35         : numargs_(0)
36 {}
37
38
39 MacroData::MacroData(string const & def, int numargs, string const & disp)
40         : def_(def), numargs_(numargs), disp_(disp)
41 {}
42
43
44 void MacroData::expand(MathArray const & args, MathArray & to) const
45 {
46         MathSqrtInset inset; // Hack. Any inset with a cell would do.
47         asArray(disp_.empty() ? def_ : disp_, inset.cell(0));
48         //lyxerr << "MathData::expand: args: " << args << endl;
49         //lyxerr << "MathData::expand: ar: " << inset.cell(0) << endl;
50         for (DocIterator it = doc_iterator_begin(inset); it; it.forwardChar()) {
51                 if (!it.nextInset())
52                         continue;
53                 if (it.nextInset()->lyxCode() != InsetBase::MATHMACROARG_CODE)
54                         continue;
55                 //it.cell().erase(it.pos());
56                 //it.cell().insert(it.pos(), it.nextInset()->asMathInset()
57                 int n = static_cast<MathMacroArgument*>(it.nextInset())->number();
58                 if (n <= args.size()) {
59                         if (args[n - 1]->asBraceInset()) {
60                                 it.cell().erase(it.pos());
61                                 it.cell().insert(it.pos(), args[n - 1]->cell(0));
62                         } else {
63                                 it.cell()[it.pos()] = args[n - 1];
64                         }
65                 }
66         }
67         //lyxerr << "MathData::expand: res: " << inset.cell(0) << endl;
68         to = inset.cell(0);
69 }
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(string const & name) const
82 {
83         return macros_.find(name) != macros_.end();
84 }
85
86
87 MacroData const & MacroTable::get(string const & name) const
88 {
89         table_type::const_iterator it = macros_.find(name);
90         BOOST_ASSERT(it != macros_.end());
91         return it->second;
92 }
93
94
95 void MacroTable::insert(string const & name, MacroData const & data)
96 {
97         lyxerr << "MathMacroTable::insert: " << name << endl;
98         macros_[name] = data;
99 }
100
101
102 void MacroTable::insert(string const & def)
103 {
104         //lyxerr << "MathMacroTable::insert, def: " << def << endl;
105         istringstream is(def);
106         MathMacroTemplate mac(is);
107         insert(mac.name(), mac.asMacroData());
108 }
109
110
111 void MacroTable::dump()
112 {
113         lyxerr << "\n------------------------------------------" << endl;
114         table_type::const_iterator it;
115         for (it = macros_.begin(); it != macros_.end(); ++it)
116                 lyxerr << it->first
117                         << " [" << it->second.def() << "] : "
118                         << " [" << it->second.disp() << "] : "
119                         << endl;
120         lyxerr << "------------------------------------------" << endl;
121 }