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 string captureOutput(string const & cmd, string const & data)
56 string outfile = lyx::tempName(string(), "mathextern");
57 string full = "echo '" + data + "' | " + cmd + " > " + outfile;
58 lyxerr << "calling: " << full << "\n";
59 Systemcalls dummy(Systemcalls::System, full, 0);
60 string out = GetFileContents(outfile);
61 lyxerr << "result: " << out << "\n";
66 string pipeThroughMaple(string const & extra, MathArray const & ar)
70 "`latex/csname_font` := ``:\n"
71 "`latex/latex/*` := subs(`\\,`=`\\cdot `,eval(`latex/latex/*`)):\n";
72 //"#`latex/csname_font` := `\\it `:"
73 //"#`latex/latex/symbol` "
74 // " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
76 string trailer = "quit;";
77 string expr = ar.maplize();
79 for (int i = 0; i < 100; ++i) { // at most 100 attempts
80 // try to fix missing '*' the hard way by using mint
82 // ... > echo "1A;" | mint -i 1 -S -s -q
85 // Probably missing an operator such as * p
87 lyxerr << "checking expr: '" << expr << "'\n";
88 string out = captureOutput("mint -i 1 -S -s -q -q", expr + ";");
90 break; // expression syntax is ok
91 istringstream is(out);
94 if (line.find("on line") != 0)
95 break; // error message not identified
97 string::size_type pos = line.find('^');
98 if (pos == string::npos || pos < 15)
99 break; // caret position not found
100 expr.insert(pos - 15, "*");
103 string full = "latex(" + extra + '(' + expr + "));";
104 string res = captureOutput("maple -q", header + full + trailer);
112 MathArray pipeThroughExtern(string const & arg, MathArray const & ar)
116 istringstream iss(arg.c_str());
117 iss >> lang >> extra;
123 if (lang == "octave") {
125 string out = captureOutput("octave -q", ar.octavize());
128 mathed_parse_cell(res, out);
130 } else if (lang == "maple") {
132 mathed_parse_cell(res, pipeThroughMaple(extra, ar));
136 // create normalized expression
138 os << "[" << extra << ' ';
141 string data = os.str().c_str();
143 // search external script
144 string file = LibFileSearch("mathed", "extern_" + lang);
146 lyxerr << "converter to '" << lang << "' not found\n";
150 // run external sript
151 string out = captureOutput(file, data);
152 mathed_parse_cell(res, out);
161 InsetFormula::InsetFormula()
162 : par_(MathAtom(new MathMatrixInset))
166 InsetFormula::InsetFormula(MathInsetTypes t)
167 : par_(MathAtom(new MathMatrixInset(t)))
171 InsetFormula::InsetFormula(string const & s)
174 bool res = mathed_parse_normal(par_, s);
177 res = mathed_parse_normal(par_, "$" + s + "$");
180 lyxerr << "cannot interpret '" << s << "' as math\n";
181 par_ = MathAtom(new MathMatrixInset(LM_OT_SIMPLE));
188 Inset * InsetFormula::clone(Buffer const &, bool) const
190 return new InsetFormula(*this);
194 void InsetFormula::write(Buffer const * buf, ostream & os) const
197 latex(buf, os, false, false);
201 int InsetFormula::latex(Buffer const * buf, ostream & os, bool fragil, bool)
204 MathWriteInfo wi(buf, os, fragil);
210 int InsetFormula::ascii(Buffer const * buf, ostream & os, int) const
212 MathWriteInfo wi(buf, os, false);
218 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
220 return ascii(buf, os, 0);
224 int InsetFormula::docbook(Buffer const * buf, ostream & os) const
226 return ascii(buf, os, 0);
230 void InsetFormula::read(Buffer const *, LyXLex & lex)
232 mathed_parse_normal(par_, lex);
237 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
238 int y, float & xx, bool) const
243 Painter & pain = bv->painter();
246 int w = par_->width();
247 int h = par_->height();
248 int a = par_->ascent();
249 pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
251 if (mathcursor && mathcursor->formula() == this) {
252 mathcursor->drawSelection(pain);
253 pain.rectangle(x, y - a, w, h, LColor::mathframe);
256 par_->draw(pain, x, y);
261 setCursorVisible(false);
265 vector<string> const InsetFormula::getLabelList() const
267 return mat()->getLabelList();
271 UpdatableInset::RESULT
272 InsetFormula::localDispatch(BufferView * bv, kb_action action,
275 RESULT result = DISPATCHED;
280 bv->lockedInsetStoreUndo(Undo::INSERT);
281 mathcursor->breakLine();
282 mathcursor->normalize();
283 updateLocal(bv, true);
286 case LFUN_MATH_NUMBER:
288 //lyxerr << "toggling all numbers\n";
290 bv->lockedInsetStoreUndo(Undo::INSERT);
291 bool old = mat()->numberedType();
292 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
293 mat()->numbered(row, !old);
294 bv->owner()->message(old ? _("No number") : _("Number"));
295 updateLocal(bv, true);
300 case LFUN_MATH_NONUMBER:
302 //lyxerr << "toggling line number\n";
304 bv->lockedInsetStoreUndo(Undo::INSERT);
305 MathCursor::row_type row = mathcursor->row();
306 bool old = mat()->numbered(row);
307 bv->owner()->message(old ? _("No number") : _("Number"));
308 mat()->numbered(row, !old);
309 updateLocal(bv, true);
314 case LFUN_INSERT_LABEL:
316 bv->lockedInsetStoreUndo(Undo::INSERT);
318 MathCursor::row_type row = mathcursor->row();
319 string old_label = mat()->label(row);
320 string new_label = arg;
322 if (new_label.empty()) {
323 string const default_label =
324 (lyxrc.label_init_length >= 0) ? "eq:" : "";
325 pair<bool, string> const res = old_label.empty()
326 ? askForText(_("Enter new label to insert:"), default_label)
327 : askForText(_("Enter label:"), old_label);
329 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
332 new_label = frontStrip(strip(res.second));
335 //if (new_label == old_label)
336 // break; // Nothing to do
338 if (!new_label.empty()) {
339 lyxerr << "setting label to '" << new_label << "'\n";
340 mat()->numbered(row, true);
343 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
346 mat()->label(row, new_label);
348 updateLocal(bv, true);
352 case LFUN_MATH_MUTATE:
354 bv->lockedInsetStoreUndo(Undo::EDIT);
357 mathcursor->getPos(x, y);
359 mathcursor->setPos(x, y);
360 mathcursor->normalize();
361 updateLocal(bv, true);
365 case LFUN_MATH_EXTERN:
367 bv->lockedInsetStoreUndo(Undo::EDIT);
369 // re-compute inset dimension
371 updateLocal(bv, true);
375 case LFUN_MATH_DISPLAY:
379 mathcursor->getPos(x, y);
380 if (mat()->getType() == LM_OT_SIMPLE)
381 mat()->mutate(LM_OT_EQUATION);
383 mat()->mutate(LM_OT_SIMPLE);
384 mathcursor->setPos(x, y);
385 mathcursor->normalize();
386 updateLocal(bv, true);
390 case LFUN_PASTESELECTION:
392 string const clip = bv->getClipboard();
394 mathed_parse_normal(par_, clip);
398 case LFUN_MATH_COLUMN_INSERT:
400 if (mat()->getType() == LM_OT_ALIGN)
401 mat()->mutate(LM_OT_ALIGNAT);
402 mat()->addCol(mat()->ncols());
403 mathcursor->normalize();
404 updateLocal(bv, true);
408 result = InsetFormulaBase::localDispatch(bv, action, arg);
415 void InsetFormula::handleExtern(const string & arg)
421 bool selected = mathcursor->selection();
425 mathcursor->selGet(ar);
426 lyxerr << "use selection: " << ar << "\n";
428 mathcursor->stripFromLastEqualSign();
429 ar = mathcursor->cursor().cell();
430 mathcursor->insert(MathAtom(new MathCharInset('=', LM_TC_VAR)));
431 //lyxerr << "use whole cell: " << ar << "\n";
434 mathcursor->insert(pipeThroughExtern(arg, ar));
438 bool InsetFormula::display() const
440 return mat()->getType() != LM_OT_SIMPLE;
444 MathMatrixInset const * InsetFormula::mat() const
446 lyx::Assert(par_->asMatrixInset());
447 return par_->asMatrixInset();
451 MathMatrixInset * InsetFormula::mat()
453 lyx::Assert(par_->asMatrixInset());
454 return par_->asMatrixInset();
458 Inset::Code InsetFormula::lyxCode() const
460 return Inset::MATH_CODE;
464 void InsetFormula::validate(LaTeXFeatures & features) const
466 par_->validate(features);
470 bool InsetFormula::insetAllowed(Inset::Code code) const
472 return code == Inset::LABEL_CODE && display();
476 int InsetFormula::ascent(BufferView *, LyXFont const &) const
478 return par_->ascent() + 2;
482 int InsetFormula::descent(BufferView *, LyXFont const &) const
484 return par_->descent() - 2;
488 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
491 return par_->width();
495 MathInsetTypes InsetFormula::getType() const
497 return mat()->getType();