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"
24 #include "funcrequest.h"
26 #include "LaTeXFeatures.h"
30 #include "textpainter.h"
32 #include "frontends/Alert.h"
34 #include "support/std_sstream.h"
36 using lyx::support::trim;
43 using std::istringstream;
44 using std::ostringstream;
50 bool openNewInset(LCursor & cur, InsetBase * inset)
52 if (!cur.bv().insertInset(inset)) {
56 inset->edit(cur, true);
66 std::auto_ptr<InsetBase> InsetFormula::clone() const
68 return auto_ptr<InsetBase>(new InsetFormula(*this));
72 void InsetFormula::write(Buffer const &, std::ostream & os) const
74 WriteStream wi(os, false, false);
75 os << fileInsetLabel() << ' ';
76 MathHullInset::write(wi);
80 void InsetFormula::read(Buffer const &, LyXLex & lex)
83 mathed_parse_normal(at, lex);
84 MathHullInset::operator=(*at->asHullInset());
89 /////////////////////////////////////////////
91 void mathDispatchCreation(LCursor & cur, FuncRequest const & cmd,
94 // use selection if available..
96 //if (action == LFUN_MATH_IMPORT_SELECTION)
101 cur.bv().getLyXText()->selectionAsString(*cur.bv().buffer(), false);
104 InsetBase * f = new MathHullInset;
105 if (openNewInset(cur, f)) {
106 cur.inset()->dispatch(cur, FuncRequest(LFUN_MATH_MUTATE, "simple"));
107 // don't do that also for LFUN_MATH_MODE unless you want end up with
108 // always changing to mathrm when opening an inlined inset
109 // -- I really hate "LyXfunc overloading"...
111 f->dispatch(cur, FuncRequest(LFUN_MATH_DISPLAY));
112 f->dispatch(cur, FuncRequest(LFUN_INSERT_MATH, cmd.argument));
115 // create a macro if we see "\\newcommand" somewhere, and an ordinary
118 if (sel.find("\\newcommand") == string::npos &&
119 sel.find("\\def") == string::npos)
120 f = new MathHullInset(sel);
122 f = new InsetFormulaMacro(sel);
123 cur.bv().getLyXText()->cutSelection(true, false);
124 openNewInset(cur, f);
126 cmd.message(N_("Math editor mode"));
130 void mathDispatch(LCursor & cur, FuncRequest const & cmd)
132 if (!cur.bv().available())
135 switch (cmd.action) {
137 case LFUN_MATH_DISPLAY:
138 mathDispatchCreation(cur, cmd, true);
142 mathDispatchCreation(cur, cmd, false);
145 case LFUN_MATH_IMPORT_SELECTION:
146 mathDispatchCreation(cur, cmd, false);
150 case LFUN_MATH_MACRO:
151 if (cmd.argument.empty())
152 cmd.errorMessage(N_("Missing argument"));
154 string s = cmd.argument;
155 string const s1 = token(s, ' ', 1);
156 int const nargs = s1.empty() ? 0 : atoi(s1);
157 string const s2 = token(s, ' ', 2);
158 string const type = s2.empty() ? "newcommand" : s2;
159 openNewInset(cur, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
163 case LFUN_INSERT_MATH:
164 case LFUN_INSERT_MATRIX:
165 case LFUN_MATH_DELIM: {
166 MathHullInset * f = new MathHullInset;
167 if (openNewInset(cur, f)) {
168 cur.inset()->dispatch(cur, FuncRequest(LFUN_MATH_MUTATE, "simple"));
169 cur.inset()->dispatch(cur, cmd);