3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
9 * Full author contact details are available in file CREDITS.
15 #include "formulamacro.h"
16 #include "math_data.h"
17 #include "math_hullinset.h"
18 #include "math_mathmlstream.h"
19 #include "math_parser.h"
21 #include "BufferView.h"
23 #include "dispatchresult.h"
25 #include "funcrequest.h"
27 #include "LaTeXFeatures.h"
31 #include "textpainter.h"
33 #include "frontends/Alert.h"
35 #include "support/std_sstream.h"
37 using lyx::support::trim;
44 using std::istringstream;
45 using std::ostringstream;
51 bool openNewInset(LCursor & cur, InsetBase * inset)
53 if (!cur.bv().insertInset(inset)) {
57 inset->edit(cur, true);
67 std::auto_ptr<InsetBase> InsetFormula::clone() const
69 return auto_ptr<InsetBase>(new InsetFormula(*this));
73 void InsetFormula::write(Buffer const &, std::ostream & os) const
75 WriteStream wi(os, false, false);
76 os << fileInsetLabel() << ' ';
77 MathHullInset::write(wi);
81 void InsetFormula::read(Buffer const &, LyXLex & lex)
84 mathed_parse_normal(at, lex);
85 MathHullInset::operator=(*at->asHullInset());
90 /////////////////////////////////////////////
92 void mathDispatchCreation(LCursor & cur, FuncRequest const & cmd,
95 // use selection if available..
97 //if (action == LFUN_MATH_IMPORT_SELECTION)
102 cur.bv().getLyXText()->selectionAsString(*cur.bv().buffer(), false);
105 InsetBase * f = new MathHullInset;
106 if (openNewInset(cur, f)) {
107 cur.inset()->dispatch(cur, FuncRequest(LFUN_MATH_MUTATE, "simple"));
108 // don't do that also for LFUN_MATH_MODE unless you want end up with
109 // always changing to mathrm when opening an inlined inset
110 // -- I really hate "LyXfunc overloading"...
112 f->dispatch(cur, FuncRequest(LFUN_MATH_DISPLAY));
113 f->dispatch(cur, FuncRequest(LFUN_INSERT_MATH, cmd.argument));
116 // create a macro if we see "\\newcommand" somewhere, and an ordinary
119 if (sel.find("\\newcommand") == string::npos &&
120 sel.find("\\def") == string::npos)
121 f = new MathHullInset(sel);
123 f = new InsetFormulaMacro(sel);
124 cur.bv().getLyXText()->cutSelection(true, false);
125 openNewInset(cur, f);
127 cmd.message(N_("Math editor mode"));
131 void mathDispatch(LCursor & cur, FuncRequest const & cmd)
133 if (!cur.bv().available())
136 switch (cmd.action) {
138 case LFUN_MATH_DISPLAY:
139 mathDispatchCreation(cur, cmd, true);
143 mathDispatchCreation(cur, cmd, false);
146 case LFUN_MATH_IMPORT_SELECTION:
147 mathDispatchCreation(cur, cmd, false);
151 case LFUN_MATH_MACRO:
152 if (cmd.argument.empty())
153 cmd.errorMessage(N_("Missing argument"));
155 string s = cmd.argument;
156 string const s1 = token(s, ' ', 1);
157 int const nargs = s1.empty() ? 0 : atoi(s1);
158 string const s2 = token(s, ' ', 2);
159 string const type = s2.empty() ? "newcommand" : s2;
160 openNewInset(cur, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
164 case LFUN_INSERT_MATH:
165 case LFUN_INSERT_MATRIX:
166 case LFUN_MATH_DELIM: {
167 MathHullInset * f = new MathHullInset;
168 if (openNewInset(cur, f)) {
169 cur.inset()->dispatch(cur, FuncRequest(LFUN_MATH_MUTATE, "simple"));
170 cur.inset()->dispatch(cur, cmd);