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"
27 #include "math_arrayinset.h"
28 #include "math_deliminset.h"
30 #include "BufferView.h"
33 #include "lyx_gui_misc.h"
34 #include "support/LOstream.h"
35 #include "support/LAssert.h"
36 #include "support/lyxlib.h"
37 #include "support/syscall.h"
38 #include "support/lstrings.h"
39 #include "support/filetools.h" // LibFileSearch
43 #include "math_matrixinset.h"
44 #include "mathed/support.h"
56 string captureOutput(string const & cmd, string const & data)
58 string outfile = lyx::tempName(string(), "mathextern");
59 string full = "echo '" + data + "' | " + cmd + " > " + outfile;
60 lyxerr << "calling: " << full << "\n";
61 Systemcalls dummy(Systemcalls::System, full, 0);
62 string out = GetFileContents(outfile);
63 lyxerr << "result: " << out << "\n";
68 MathArray pipeThroughMaple(string const & extra, MathArray const & ar)
72 "`latex/csname_font` := ``:\n"
73 "`latex/latex/*` := subs(`\\,`=`\\cdot `,eval(`latex/latex/*`)):\n";
74 //"#`latex/csname_font` := `\\it `:"
75 //"#`latex/latex/symbol` "
76 // " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
78 string trailer = "quit;";
79 string expr = ar.maplize();
81 for (int i = 0; i < 100; ++i) { // at most 100 attempts
82 // try to fix missing '*' the hard way by using mint
84 // ... > echo "1A;" | mint -i 1 -S -s -q
87 // Probably missing an operator such as * p
89 lyxerr << "checking expr: '" << expr << "'\n";
90 string out = captureOutput("mint -i 1 -S -s -q -q", expr + ";");
92 break; // expression syntax is ok
93 istringstream is(out);
96 if (line.find("on line") != 0)
97 break; // error message not identified
99 string::size_type pos = line.find('^');
100 if (pos == string::npos || pos < 15)
101 break; // caret position not found
102 expr.insert(pos - 15, "*");
105 string full = "latex(" + extra + '(' + expr + "));";
106 string out = captureOutput("maple -q", header + full + trailer);
112 mathed_parse_cell(res, out);
117 MathArray pipeThroughOctave(string const &, MathArray const & ar)
119 string out = captureOutput("octave -q", ar.octavize());
120 if (out.size() > 6) // remove 'ans = '
123 // parse output as matrix or single number
124 MathAtom at(new MathArrayInset(out));
125 MathArrayInset const * mat = at.nucleus()->asArrayInset();
127 if (mat->ncols() == 1 && mat->nrows() == 1)
128 res.push_back(mat->cell(0));
130 res.push_back(MathAtom(new MathDelimInset("(", ")")));
131 res.back()->cell(0).push_back(at);
137 MathArray pipeThroughExtern(string const & arg, MathArray const & ar)
141 istringstream iss(arg.c_str());
142 iss >> lang >> extra;
146 if (lang == "octave")
147 return pipeThroughOctave(extra, ar);
150 return pipeThroughMaple(extra, ar);
152 // create normalized expression
154 os << "[" << extra << ' ';
157 string data = os.str().c_str();
159 // search external script
160 string file = LibFileSearch("mathed", "extern_" + lang);
162 lyxerr << "converter to '" << lang << "' not found\n";
166 // run external sript
167 string out = captureOutput(file, data);
169 mathed_parse_cell(res, out);
176 InsetFormula::InsetFormula()
177 : par_(MathAtom(new MathMatrixInset))
181 InsetFormula::InsetFormula(MathInsetTypes t)
182 : par_(MathAtom(new MathMatrixInset(t)))
186 InsetFormula::InsetFormula(string const & s)
189 bool res = mathed_parse_normal(par_, s);
192 res = mathed_parse_normal(par_, "$" + s + "$");
195 lyxerr << "cannot interpret '" << s << "' as math\n";
196 par_ = MathAtom(new MathMatrixInset(LM_OT_SIMPLE));
203 Inset * InsetFormula::clone(Buffer const &, bool) const
205 return new InsetFormula(*this);
209 void InsetFormula::write(Buffer const * buf, ostream & os) const
212 latex(buf, os, false, false);
216 int InsetFormula::latex(Buffer const * buf, ostream & os, bool fragil, bool)
219 MathWriteInfo wi(buf, os, fragil);
225 int InsetFormula::ascii(Buffer const * buf, ostream & os, int) const
227 MathWriteInfo wi(buf, os, false);
233 int InsetFormula::linuxdoc(Buffer const * buf, ostream & os) const
235 return ascii(buf, os, 0);
239 int InsetFormula::docbook(Buffer const * buf, ostream & os) const
241 return ascii(buf, os, 0);
245 void InsetFormula::read(Buffer const *, LyXLex & lex)
247 mathed_parse_normal(par_, lex);
252 void InsetFormula::draw(BufferView * bv, LyXFont const & font,
253 int y, float & xx, bool) const
258 Painter & pain = bv->painter();
261 int w = par_->width();
262 int h = par_->height();
263 int a = par_->ascent();
264 pain.fillRectangle(x, y - a, w, h, LColor::mathbg);
266 if (mathcursor && mathcursor->formula() == this) {
267 mathcursor->drawSelection(pain);
268 pain.rectangle(x, y - a, w, h, LColor::mathframe);
271 par_->draw(pain, x, y);
276 setCursorVisible(false);
280 vector<string> const InsetFormula::getLabelList() const
282 return mat()->getLabelList();
286 UpdatableInset::RESULT
287 InsetFormula::localDispatch(BufferView * bv, kb_action action,
290 RESULT result = DISPATCHED;
295 bv->lockedInsetStoreUndo(Undo::INSERT);
296 mathcursor->breakLine();
297 mathcursor->normalize();
298 updateLocal(bv, true);
301 case LFUN_MATH_NUMBER:
303 //lyxerr << "toggling all numbers\n";
305 bv->lockedInsetStoreUndo(Undo::INSERT);
306 bool old = mat()->numberedType();
307 for (MathInset::row_type row = 0; row < par_->nrows(); ++row)
308 mat()->numbered(row, !old);
309 bv->owner()->message(old ? _("No number") : _("Number"));
310 updateLocal(bv, true);
315 case LFUN_MATH_NONUMBER:
317 //lyxerr << "toggling line number\n";
319 bv->lockedInsetStoreUndo(Undo::INSERT);
320 MathCursor::row_type row = mathcursor->row();
321 bool old = mat()->numbered(row);
322 bv->owner()->message(old ? _("No number") : _("Number"));
323 mat()->numbered(row, !old);
324 updateLocal(bv, true);
329 case LFUN_INSERT_LABEL:
331 bv->lockedInsetStoreUndo(Undo::INSERT);
333 MathCursor::row_type row = mathcursor->row();
334 string old_label = mat()->label(row);
335 string new_label = arg;
337 if (new_label.empty()) {
338 string const default_label =
339 (lyxrc.label_init_length >= 0) ? "eq:" : "";
340 pair<bool, string> const res = old_label.empty()
341 ? askForText(_("Enter new label to insert:"), default_label)
342 : askForText(_("Enter label:"), old_label);
344 lyxerr << "res: " << res.first << " - '" << res.second << "'\n";
347 new_label = frontStrip(strip(res.second));
350 //if (new_label == old_label)
351 // break; // Nothing to do
353 if (!new_label.empty()) {
354 lyxerr << "setting label to '" << new_label << "'\n";
355 mat()->numbered(row, true);
358 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
361 mat()->label(row, new_label);
363 updateLocal(bv, true);
367 case LFUN_MATH_MUTATE:
369 bv->lockedInsetStoreUndo(Undo::EDIT);
372 mathcursor->getPos(x, y);
374 mathcursor->setPos(x, y);
375 mathcursor->normalize();
376 updateLocal(bv, true);
380 case LFUN_MATH_EXTERN:
382 bv->lockedInsetStoreUndo(Undo::EDIT);
384 // re-compute inset dimension
386 updateLocal(bv, true);
390 case LFUN_MATH_DISPLAY:
394 mathcursor->getPos(x, y);
395 if (mat()->getType() == LM_OT_SIMPLE)
396 mat()->mutate(LM_OT_EQUATION);
398 mat()->mutate(LM_OT_SIMPLE);
399 mathcursor->setPos(x, y);
400 mathcursor->normalize();
401 updateLocal(bv, true);
405 case LFUN_PASTESELECTION:
407 string const clip = bv->getClipboard();
409 mathed_parse_normal(par_, clip);
413 case LFUN_MATH_COLUMN_INSERT:
415 if (mat()->getType() == LM_OT_ALIGN)
416 mat()->mutate(LM_OT_ALIGNAT);
417 mat()->addCol(mat()->ncols());
418 mathcursor->normalize();
419 updateLocal(bv, true);
423 result = InsetFormulaBase::localDispatch(bv, action, arg);
430 void InsetFormula::handleExtern(const string & arg)
436 bool selected = mathcursor->selection();
440 mathcursor->selGet(ar);
441 lyxerr << "use selection: " << ar << "\n";
444 mathcursor->stripFromLastEqualSign();
445 ar = mathcursor->cursor().cell();
446 mathcursor->insert(MathAtom(new MathCharInset('=', LM_TC_VAR)));
447 //lyxerr << "use whole cell: " << ar << "\n";
450 mathcursor->insert(pipeThroughExtern(arg, ar));
454 bool InsetFormula::display() const
456 return mat()->getType() != LM_OT_SIMPLE;
460 MathMatrixInset const * InsetFormula::mat() const
462 lyx::Assert(par_->asMatrixInset());
463 return par_->asMatrixInset();
467 MathMatrixInset * InsetFormula::mat()
469 lyx::Assert(par_->asMatrixInset());
470 return par_->asMatrixInset();
474 Inset::Code InsetFormula::lyxCode() const
476 return Inset::MATH_CODE;
480 void InsetFormula::validate(LaTeXFeatures & features) const
482 par_->validate(features);
486 bool InsetFormula::insetAllowed(Inset::Code code) const
488 return code == Inset::LABEL_CODE && display();
492 int InsetFormula::ascent(BufferView *, LyXFont const &) const
494 return par_->ascent() + 2;
498 int InsetFormula::descent(BufferView *, LyXFont const &) const
500 return par_->descent() - 2;
504 int InsetFormula::width(BufferView * bv, LyXFont const & font) const
507 return par_->width();
511 MathInsetTypes InsetFormula::getType() const
513 return mat()->getType();