3 * Purpose: Implementation of formula inset
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
6 * Description: Allows the edition of math paragraphs inside Lyx.
8 * Copyright: 1996-1998 Alejandro Aguilar Sierra
10 * Version: 0.4, Lyx project.
12 * You are free to use and modify this code under the terms of
13 * the GNU General Public Licence version 2 or later.
17 #pragma implementation
23 #include "commandtags.h"
24 #include "math_cursor.h"
25 #include "math_parser.h"
26 #include "math_charinset.h"
28 #include "BufferView.h"
31 #include "lyx_gui_misc.h"
32 #include "support/LOstream.h"
33 #include "support/LAssert.h"
34 #include "support/lyxlib.h"
35 #include "support/syscall.h"
36 #include "support/lstrings.h"
37 #include "support/filetools.h" // LibFileSearch
41 #include "math_matrixinset.h"
42 #include "mathed/support.h"
54 void stripFromLastEqualSign(MathArray & ar)
56 // find position of last '=' in the array
57 MathArray::size_type pos = ar.size();
58 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
59 if ((*it)->getChar() == '=')
60 pos = it - ar.begin();
62 // delete everything behind this position
63 ar.erase(pos, ar.size());
67 string captureOutput(string const & cmd, string const & data)
69 string outfile = lyx::tempName(string(), "mathextern");
70 string full = "echo '" + data + "' | " + cmd + " > " + outfile;
71 lyxerr << "calling: " << full << "\n";
72 Systemcalls dummy(Systemcalls::System, full, 0);
73 string out = GetFileContents(outfile);
74 lyxerr << "result: " << out << "\n";
79 string pipeThroughMaple(string const & extra, MathArray const & ar)
83 "`latex/csname_font` := ``:\n"
84 "`latex/latex/*` := subs(`\\,`=`\\cdot `,eval(`latex/latex/*`)):\n";
85 //"#`latex/csname_font` := `\\it `:"
86 //"#`latex/latex/symbol` "
87 // " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
89 string trailer = "quit;";
90 string expr = ar.maplize();
92 for (int i = 0; i < 100; ++i) { // at most 100 attempts
93 // try to fix missing '*' the hard way by using mint
95 // ... > echo "1A;" | mint -i 1 -S -s -q
98 // Probably missing an operator such as * p
100 lyxerr << "checking expr: '" << expr << "'\n";
101 string out = captureOutput("mint -i 1 -S -s -q -q", expr + ";");
103 break; // expression syntax is ok
104 istringstream is(out);
107 if (line.find("on line") != 0)
108 break; // error message not identified
110 string::size_type pos = line.find('^');
111 if (pos == string::npos || pos < 15)
112 break; // caret position not found
113 expr.insert(pos - 15, "*");
116 string full = "latex(" + extra + '(' + expr + "));";
117 string res = captureOutput("maple -q", header + full + trailer);
125 MathArray pipeThroughExtern(string const & arg, MathArray const & ar)
129 istringstream iss(arg.c_str());
130 iss >> lang >> extra;
136 if (lang == "octave") {
138 string out = captureOutput("octave -q", ar.octavize());
141 mathed_parse_cell(res, out);
143 } else if (lang == "maple") {
145 mathed_parse_cell(res, pipeThroughMaple(extra, ar));
149 // create normalized expression
151 os << "[" << extra << ' ';
154 string data = os.str().c_str();
156 // search external script
157 string file = LibFileSearch("mathed", "extern_" + lang);
159 lyxerr << "converter to '" << lang << "' not found\n";
163 // run external sript
164 string out = captureOutput(file, data);
165 mathed_parse_cell(res, out);
174 InsetFormula::InsetFormula()
175 : par_(MathAtom(new MathMatrixInset))
179 InsetFormula::InsetFormula(MathInsetTypes t)
180 : par_(MathAtom(new MathMatrixInset(t)))
184 InsetFormula::InsetFormula(string const & s)
187 bool res = mathed_parse_normal(par_, s);
190 res = mathed_parse_normal(par_, "$" + s + "$");
193 lyxerr << "cannot interpret '" << s << "' as math\n";
194 par_ = MathAtom(new MathMatrixInset(LM_OT_SIMPLE));
201 Inset * InsetFormula::clone(Buffer const &, bool) const
203 return new InsetFormula(*this);
207 void InsetFormula::write(Buffer const * buf, ostream & os) const
210 latex(buf, os, false, false);
214 int InsetFormula::latex(Buffer const * buf, ostream & os, bool fragil, bool)
217 MathWriteInfo wi(buf, os, fragil);
223 int InsetFormula::ascii(Buffer const * buf, ostream & os, int) const
225 MathWriteInfo wi(buf, os, false);
231 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
233 return ascii(buf, os, 0);
237 int InsetFormula::docbook(Buffer const * buf, ostream & os) const
239 return ascii(buf, os, 0);
243 void InsetFormula::read(Buffer const *, LyXLex & lex)
245 mathed_parse_normal(par_, lex);
250 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
251 int y, float & xx, bool) const
256 Painter & pain = bv->painter();
259 int w = par_->width();
260 int h = par_->height();
261 int a = par_->ascent();
262 pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
264 if (mathcursor && mathcursor->formula() == this) {
265 mathcursor->drawSelection(pain);
266 pain.rectangle(x, y - a, w, h, LColor::mathframe);
269 par_->draw(pain, x, y);
274 setCursorVisible(false);
278 vector<string> const InsetFormula::getLabelList() const
280 return mat()->getLabelList();
284 UpdatableInset::RESULT
285 InsetFormula::localDispatch(BufferView * bv, kb_action action,
288 RESULT result = DISPATCHED;
293 bv->lockedInsetStoreUndo(Undo::INSERT);
294 mathcursor->breakLine();
295 mathcursor->normalize();
296 updateLocal(bv, true);
299 case LFUN_MATH_NUMBER:
301 //lyxerr << "toggling all numbers\n";
303 bv->lockedInsetStoreUndo(Undo::INSERT);
304 bool old = mat()->numberedType();
305 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
306 mat()->numbered(row, !old);
307 bv->owner()->message(old ? _("No number") : _("Number"));
308 updateLocal(bv, true);
313 case LFUN_MATH_NONUMBER:
315 //lyxerr << "toggling line number\n";
317 bv->lockedInsetStoreUndo(Undo::INSERT);
318 MathCursor::row_type row = mathcursor->row();
319 bool old = mat()->numbered(row);
320 bv->owner()->message(old ? _("No number") : _("Number"));
321 mat()->numbered(row, !old);
322 updateLocal(bv, true);
327 case LFUN_INSERT_LABEL:
329 bv->lockedInsetStoreUndo(Undo::INSERT);
331 MathCursor::row_type row = mathcursor->row();
332 string old_label = mat()->label(row);
333 string new_label = arg;
335 if (new_label.empty()) {
336 string const default_label =
337 (lyxrc.label_init_length >= 0) ? "eq:" : "";
338 pair<bool, string> const res = old_label.empty()
339 ? askForText(_("Enter new label to insert:"), default_label)
340 : askForText(_("Enter label:"), old_label);
342 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
345 new_label = frontStrip(strip(res.second));
348 //if (new_label == old_label)
349 // break; // Nothing to do
351 if (!new_label.empty()) {
352 lyxerr << "setting label to '" << new_label << "'\n";
353 mat()->numbered(row, true);
356 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
359 mat()->label(row, new_label);
361 updateLocal(bv, true);
365 case LFUN_MATH_MUTATE:
367 bv->lockedInsetStoreUndo(Undo::EDIT);
370 mathcursor->getPos(x, y);
372 mathcursor->setPos(x, y);
373 mathcursor->normalize();
374 updateLocal(bv, true);
378 case LFUN_MATH_EXTERN:
380 bv->lockedInsetStoreUndo(Undo::EDIT);
382 // re-compute inset dimension
384 updateLocal(bv, true);
388 case LFUN_MATH_DISPLAY:
392 mathcursor->getPos(x, y);
393 if (mat()->getType() == LM_OT_SIMPLE)
394 mat()->mutate(LM_OT_EQUATION);
396 mat()->mutate(LM_OT_SIMPLE);
397 mathcursor->setPos(x, y);
398 mathcursor->normalize();
399 updateLocal(bv, true);
403 case LFUN_PASTESELECTION:
405 string const clip = bv->getClipboard();
407 mathed_parse_normal(par_, clip);
411 case LFUN_MATH_COLUMN_INSERT:
413 if (mat()->getType() == LM_OT_ALIGN)
414 mat()->mutate(LM_OT_ALIGNAT);
415 mat()->addCol(mat()->ncols());
416 mathcursor->normalize();
417 updateLocal(bv, true);
421 result = InsetFormulaBase::localDispatch(bv, action, arg);
428 void InsetFormula::handleExtern(const string & arg)
434 bool selected = mathcursor->selection();
438 mathcursor->selGet(ar);
439 lyxerr << "use selection: " << ar << "\n";
442 ar = mathcursor->cursor().cell();
443 stripFromLastEqualSign(ar);
444 mathcursor->insert(MathAtom(new MathCharInset('=', LM_TC_VAR)));
445 //lyxerr << "use whole cell: " << ar << "\n";
448 mathcursor->insert(pipeThroughExtern(arg, ar));
452 bool InsetFormula::display() const
454 return mat()->getType() != LM_OT_SIMPLE;
458 MathMatrixInset const * InsetFormula::mat() const
460 lyx::Assert(par_->asMatrixInset());
461 return par_->asMatrixInset();
465 MathMatrixInset * InsetFormula::mat()
467 lyx::Assert(par_->asMatrixInset());
468 return par_->asMatrixInset();
472 Inset::Code InsetFormula::lyxCode() const
474 return Inset::MATH_CODE;
478 void InsetFormula::validate(LaTeXFeatures & features) const
480 par_->validate(features);
484 bool InsetFormula::insetAllowed(Inset::Code code) const
486 return code == Inset::LABEL_CODE && display();
490 int InsetFormula::ascent(BufferView *, LyXFont const &) const
492 return par_->ascent() + 2;
496 int InsetFormula::descent(BufferView *, LyXFont const &) const
498 return par_->descent() - 2;
502 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
505 return par_->width();
509 MathInsetTypes InsetFormula::getType() const
511 return mat()->getType();