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.
14 #include "formulabase.h"
16 #include "formulamacro.h"
17 #include "math_support.h"
18 #include "math_arrayinset.h"
19 #include "math_deliminset.h"
20 #include "math_cursor.h"
21 #include "math_factory.h"
22 #include "math_hullinset.h"
23 #include "math_parser.h"
24 #include "math_spaceinset.h"
25 #include "ref_inset.h"
27 #include "BufferView.h"
28 #include "bufferview_funcs.h"
29 #include "dispatchresult.h"
31 #include "funcrequest.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/Dialogs.h"
40 #include "support/std_sstream.h"
41 #include "support/lstrings.h"
42 #include "support/lyxlib.h"
44 using lyx::support::atoi;
45 using lyx::support::split;
46 using lyx::support::token;
53 using std::istringstream;
56 MathCursor * mathcursor = 0;
64 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
66 if (!bv->insertInset(new_inset)) {
70 new_inset->edit(bv, true);
79 InsetFormulaBase::InsetFormulaBase()
82 // This is needed as long the math parser is not re-entrant
84 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
85 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
86 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
87 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
91 // simply scrap this function if you want
92 void InsetFormulaBase::mutateToText()
97 latex(NULL, os, false, false);
98 string str = os.str();
101 LyXText * lt = view_->getLyXText();
102 string::const_iterator cit = str.begin();
103 string::const_iterator end = str.end();
104 for (; cit != end; ++cit)
105 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
108 //view_->owner()->dispatch(LFUN_ESCAPE);
113 void InsetFormulaBase::handleFont
114 (BufferView * bv, string const & arg, string const & font)
116 // this whole function is a hack and won't work for incremental font
118 recordUndo(bv, Undo::ATOMIC);
120 if (mathcursor->inset()->name() == font)
121 mathcursor->handleFont(font);
123 mathcursor->handleNest(createMathInset(font));
124 mathcursor->insert(arg);
129 void InsetFormulaBase::handleFont2(BufferView * bv, string const & arg)
131 recordUndo(bv, Undo::ATOMIC);
134 bv_funcs::string2font(arg, font, b);
135 if (font.color() != LColor::inherit) {
136 MathAtom at = createMathInset("color");
137 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
138 mathcursor->handleNest(at, 1);
144 void InsetFormulaBase::validate(LaTeXFeatures &) const
148 string const InsetFormulaBase::editMessage() const
150 return _("Math editor mode");
154 void InsetFormulaBase::insetUnlock(BufferView * bv)
157 if (mathcursor->inMacroMode()) {
158 mathcursor->macroModeClose();
159 bv->updateInset(this);
161 releaseMathCursor(bv);
164 generatePreview(*bv->buffer());
165 bv->updateInset(this);
169 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
171 mathcursor->getPos(x, y);
175 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
177 // calling metrics here destroys the cached xo,yo positions e.g. in
178 // MathParboxinset. And it would be too expensive anyway...
181 lyxerr << "getCursorPos - should not happen";
185 mathcursor->getPos(x, y);
186 x = mathcursor->targetX();
189 //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
193 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
200 //math_font_max_dim(font_, asc, des);
201 getCursorPos(bv, x, y);
203 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
204 bv->fitLockedInsetCursor(x, y, asc, des);
208 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
211 bv->updateInset(this);
215 DispatchResult InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
218 return DispatchResult(false);
220 BufferView * bv = cmd.view();
221 bv->updateInset(this);
222 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
224 if (cmd.button() == mouse_button::button3) {
225 // try to dispatch to enclosed insets first
226 if (!mathcursor->dispatch(cmd).dispatched()) {
227 // launch math panel for right mouse button
228 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
229 bv->owner()->getDialogs().show("mathpanel");
231 return DispatchResult(true, true);
234 if (cmd.button() == mouse_button::button2) {
236 asArray(bv->getClipboard(), ar);
237 mathcursor->selClear();
238 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
239 mathcursor->insert(ar);
240 bv->updateInset(this);
241 return DispatchResult(true, true);
244 if (cmd.button() == mouse_button::button1) {
245 // try to dispatch to enclosed insets first
246 mathcursor->dispatch(cmd);
247 cmd.view()->stuffClipboard(mathcursor->grabSelection());
248 // try to set the cursor
250 //mathcursor = new MathCursor(this, x == 0);
252 //mathcursor->setPos(x + xo_, y + yo_);
253 return DispatchResult(true, true);
256 return DispatchResult(false);
260 DispatchResult InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
262 BufferView * bv = cmd.view();
263 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
265 if (!mathcursor || mathcursor->formula() != this) {
266 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
267 releaseMathCursor(bv);
268 mathcursor = new MathCursor(this, cmd.x == 0);
270 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
273 if (cmd.button() == mouse_button::button3) {
274 mathcursor->dispatch(cmd);
275 return DispatchResult(true, true);
278 if (cmd.button() == mouse_button::button1) {
281 mathcursor->selClear();
282 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
283 mathcursor->dispatch(cmd);
284 return DispatchResult(true, true);
287 bv->updateInset(this);
288 return DispatchResult(true, true);
292 DispatchResult InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
295 return DispatchResult(true, true);
297 if (mathcursor->dispatch(FuncRequest(cmd)).dispatched())
298 return DispatchResult(true, true);
300 // only select with button 1
301 if (cmd.button() != mouse_button::button1)
302 return DispatchResult(true, true);
304 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
305 return DispatchResult(true, true);
310 if (!mathcursor->selection())
311 mathcursor->selStart();
313 BufferView * bv = cmd.view();
314 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
315 bv->updateInset(this);
316 return DispatchResult(true, true);
320 void InsetFormulaBase::edit(BufferView * bv, bool left)
322 lyxerr << "Called FormulaBase::edit" << endl;
323 if (!bv->lockInset(this))
324 lyxerr << "Cannot lock math inset in edit call!" << endl;
325 releaseMathCursor(bv);
326 mathcursor = new MathCursor(this, left);
327 // if that is removed, we won't get the magenta box when entering an
328 // inset for the first time
329 bv->updateInset(this);
333 void InsetFormulaBase::edit(BufferView * bv, int x, int y)
335 lyxerr << "Called FormulaBase::EDIT with '" << x << ' ' << y << "'" << endl;
336 if (!bv->lockInset(this))
337 lyxerr << "Cannot lock math inset in edit call!" << endl;
338 releaseMathCursor(bv);
339 mathcursor = new MathCursor(this, true);
341 mathcursor->setPos(x + xo_, y + yo_);
342 // if that is removed, we won't get the magenta box when entering an
343 // inset for the first time
344 bv->updateInset(this);
349 InsetFormulaBase::priv_dispatch(FuncRequest const & cmd,
350 idx_type &, pos_type &)
352 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
353 // << " arg: '" << cmd.argument
354 // << "' x: '" << cmd.x
355 // << " y: '" << cmd.y
356 // << "' button: " << cmd.button() << endl;
358 BufferView * bv = cmd.view();
360 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
361 bool remove_inset = false;
363 switch (cmd.action) {
364 case LFUN_MOUSE_PRESS:
365 //lyxerr << "Mouse single press" << endl;
366 return lfunMousePress(cmd);
367 case LFUN_MOUSE_MOTION:
368 //lyxerr << "Mouse motion" << endl;
369 return lfunMouseMotion(cmd);
370 case LFUN_MOUSE_RELEASE:
371 //lyxerr << "Mouse single release" << endl;
372 return lfunMouseRelease(cmd);
373 case LFUN_MOUSE_DOUBLE:
374 //lyxerr << "Mouse double" << endl;
375 return dispatch(FuncRequest(LFUN_WORDSEL));
381 return DispatchResult(false);
383 string argument = cmd.argument;
384 DispatchResult result(true);
386 bool was_macro = mathcursor->inMacroMode();
387 bool was_selection = mathcursor->selection();
389 mathcursor->normalize();
392 switch (cmd.action) {
394 case LFUN_MATH_MUTATE:
395 case LFUN_MATH_DISPLAY:
396 case LFUN_MATH_NUMBER:
397 case LFUN_MATH_NONUMBER:
398 case LFUN_CELL_SPLIT:
400 case LFUN_DELETE_LINE_FORWARD:
401 case LFUN_INSERT_LABEL:
402 case LFUN_MATH_EXTERN:
403 case LFUN_TABULAR_FEATURE:
404 case LFUN_PASTESELECTION:
405 case LFUN_MATH_LIMITS:
406 recordUndo(bv, Undo::ATOMIC);
407 mathcursor->dispatch(cmd);
411 sel = true; // fall through...
413 result = mathcursor->right(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
414 //lyxerr << "calling scroll 20" << endl;
416 // write something to the minibuffer
417 //bv->owner()->message(mathcursor->info());
421 sel = true; // fall through
423 result = mathcursor->left(sel) ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
427 sel = true; // fall through
429 result = mathcursor->up(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_UP);
433 sel = true; // fall through
435 result = mathcursor->down(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_DOWN);
439 mathcursor->home(false);
440 mathcursor->end(true);
443 case LFUN_UP_PARAGRAPHSEL:
444 case LFUN_UP_PARAGRAPH:
445 case LFUN_DOWN_PARAGRAPHSEL:
446 case LFUN_DOWN_PARAGRAPH:
447 result = DispatchResult(true, FINISHED);
451 case LFUN_WORDLEFTSEL:
452 sel = true; // fall through
455 result = mathcursor->home(sel) ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
459 case LFUN_WORDRIGHTSEL:
460 sel = true; // fall through
463 result = mathcursor->end(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
468 case LFUN_BEGINNINGBUFSEL:
469 case LFUN_BEGINNINGBUF:
470 result = DispatchResult(true, FINISHED);
477 result = DispatchResult(false, FINISHED_RIGHT);
480 case LFUN_CELL_FORWARD:
481 mathcursor->idxNext();
484 case LFUN_CELL_BACKWARD:
485 mathcursor->idxPrev();
488 case LFUN_DELETE_WORD_BACKWARD:
490 recordUndo(bv, Undo::ATOMIC);
491 if (!mathcursor->backspace()) {
492 result = DispatchResult(true, FINISHED);
497 case LFUN_DELETE_WORD_FORWARD:
499 recordUndo(bv, Undo::ATOMIC);
500 if (!mathcursor->erase()) {
501 result = DispatchResult(true, FINISHED);
507 // sprintf(dispatch_buffer, "%d %d",);
508 // DispatchResult= dispatch_buffer;
511 lyxerr << "LFUN_SETXY broken!" << endl;
514 istringstream is(cmd.argument.c_str());
516 mathcursor->setPos(x, y);
522 istringstream is(cmd.argument.c_str());
525 mathcursor->macroModeClose();
526 recordUndo(bv, Undo::ATOMIC);
527 mathcursor->selPaste(n);
532 recordUndo(bv, Undo::DELETE);
533 mathcursor->selCut();
537 mathcursor->selCopy();
541 // Special casing for superscript in case of LyX handling
543 case LFUN_CIRCUMFLEX:
544 if (cmd.argument.empty()) {
545 // do superscript if LyX handles
547 recordUndo(bv, Undo::ATOMIC);
548 mathcursor->script(true);
565 case LFUN_HUNG_UMLAUT:
569 case LFUN_FREEFONT_APPLY:
570 case LFUN_FREEFONT_UPDATE:
571 handleFont2(bv, cmd.argument);
574 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
575 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
576 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
577 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
578 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
579 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
580 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
581 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
582 //case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
583 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
586 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
587 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
589 handleFont(bv, cmd.argument, "textrm");
590 //bv->owner()->message(_("math text mode toggled"));
596 recordUndo(bv, Undo::ATOMIC);
597 mathcursor->setSize(arg);
602 case LFUN_INSERT_MATRIX: {
603 recordUndo(bv, Undo::ATOMIC);
608 istringstream is(argument);
609 is >> m >> n >> v_align >> h_align;
613 mathcursor->niceInsert(
614 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
618 case LFUN_SUPERSCRIPT:
621 recordUndo(bv, Undo::ATOMIC);
622 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
626 case LFUN_MATH_DELIM:
628 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
630 string rs = split(cmd.argument, ls, ' ');
631 // Reasonable default values
637 recordUndo(bv, Undo::ATOMIC);
638 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
642 case LFUN_SPACE_INSERT:
643 case LFUN_MATH_SPACE:
644 recordUndo(bv, Undo::ATOMIC);
645 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
649 bv->owner()->message(_("Invalid action in math mode!"));
653 case LFUN_EXEC_COMMAND:
654 result = DispatchResult(false);
658 // interpret this as if a backslash was typed
659 recordUndo(bv, Undo::ATOMIC);
660 mathcursor->interpret('\\');
663 case LFUN_BREAKPARAGRAPH:
664 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
665 case LFUN_BREAKPARAGRAPH_SKIP:
669 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
670 // handling such that "self-insert" works on "arbitrary stuff" too, and
671 // math-insert only handles special math things like "matrix".
672 case LFUN_INSERT_MATH:
673 recordUndo(bv, Undo::ATOMIC);
674 mathcursor->niceInsert(argument);
678 case LFUN_SELFINSERT:
679 if (!argument.empty()) {
680 recordUndo(bv, Undo::ATOMIC);
681 if (argument.size() == 1)
682 result = mathcursor->interpret(argument[0]) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
684 mathcursor->insert(argument);
689 if (mathcursor->selection())
690 mathcursor->selClear();
692 result = DispatchResult(false);
695 case LFUN_INSET_TOGGLE:
696 mathcursor->insetToggle();
699 case LFUN_DIALOG_SHOW:
700 result = DispatchResult(false);
703 case LFUN_DIALOG_SHOW_NEW_INSET: {
704 string const & name = argument;
708 data = tmp.createDialogStr(name);
712 result = DispatchResult(false);
714 bv->owner()->getDialogs().show(name, data, 0);
719 case LFUN_INSET_APPLY: {
720 string const name = cmd.getArg(0);
722 bv->owner()->getDialogs().getOpenInset(name);
725 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
726 result = base->dispatch(fr);
729 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
730 mathcursor->insert(ar);
731 result = DispatchResult(true, true);
733 result = DispatchResult(false);
740 result = DispatchResult(false);
743 if (result == DispatchResult(true, true))
744 bv->updateInset(this);
746 mathcursor->normalize();
749 BOOST_ASSERT(mathcursor);
751 if (mathcursor->selection() || was_selection)
752 toggleInsetSelection(bv);
754 if (result.dispatched()) {
757 cmd.view()->stuffClipboard(mathcursor->grabSelection());
759 releaseMathCursor(bv);
760 bv->unlockInset(this);
762 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
765 return result; // original version
769 void InsetFormulaBase::revealCodes(BufferView * bv) const
773 bv->owner()->message(mathcursor->info());
776 // write something to the minibuffer
777 // translate to latex
778 mathcursor->markInsert();
781 string str = os.str();
782 mathcursor->markErase();
783 string::size_type pos = 0;
785 for (string::iterator it = str.begin(); it != str.end(); ++it) {
788 else if (*it == '\0') {
790 pos = it - str.begin();
796 res = res.substr(pos - 30);
798 res = res.substr(0, 60);
799 bv->owner()->message(res);
804 InsetOld::Code InsetFormulaBase::lyxCode() const
806 return InsetOld::MATH_CODE;
810 int InsetFormulaBase::ylow() const
812 return yo_ - dim_.asc;
816 int InsetFormulaBase::yhigh() const
818 return yo_ + dim_.des;
822 int InsetFormulaBase::xlow() const
828 int InsetFormulaBase::xhigh() const
830 return xo_ + dim_.wid;
834 /////////////////////////////////////////////////////////////////////
837 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
843 static InsetFormulaBase * lastformula = 0;
844 static MathIterator current = MathIterator(ibegin(par().nucleus()));
846 static string laststr;
848 if (lastformula != this || laststr != str) {
849 //lyxerr << "reset lastformula to " << this << endl;
852 current = ibegin(par().nucleus());
854 mathed_parse_cell(ar, str);
858 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
860 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
861 if (it.cell().matchpart(ar, it.back().pos_)) {
862 bv->unlockInset(bv->theLockingInset());
863 if (!bv->lockInset(this)) {
864 lyxerr << "Cannot lock inset" << endl;
868 mathcursor = new MathCursor(this, true);
870 mathcursor->setSelection(it, ar.size());
873 bv->updateInset(this);
878 //lyxerr << "not found!" << endl;
884 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
887 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
888 return searchForward(bv, what, a, b);
892 bool InsetFormulaBase::display() const
894 return par()->asHullInset() && par()->asHullInset()->display();
898 string InsetFormulaBase::selectionAsString() const
900 return mathcursor ? mathcursor->grabSelection() : string();
903 /////////////////////////////////////////////////////////////////////
906 void mathDispatchCreation(FuncRequest const & cmd, bool display)
908 BufferView * bv = cmd.view();
909 // use selection if available..
911 //if (action == LFUN_MATH_IMPORT_SELECTION)
915 string sel = bv->getLyXText()->selectionAsString(*bv->buffer(), false);
918 InsetFormula * f = new InsetFormula(bv);
919 if (openNewInset(bv, f)) {
920 bv->theLockingInset()->
921 dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
922 // don't do that also for LFUN_MATH_MODE unless you want end up with
923 // always changing to mathrm when opening an inlined inset
924 // -- I really hate "LyXfunc overloading"...
926 f->dispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
927 f->dispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
930 // create a macro if we see "\\newcommand" somewhere, and an ordinary
932 InsetFormulaBase * f;
933 if (sel.find("\\newcommand") == string::npos &&
934 sel.find("\\def") == string::npos)
935 f = new InsetFormula(sel);
937 f = new InsetFormulaMacro(sel);
938 bv->getLyXText()->cutSelection(true, false);
941 cmd.message(N_("Math editor mode"));
945 void mathDispatch(FuncRequest const & cmd)
947 BufferView * bv = cmd.view();
948 if (!bv->available())
951 switch (cmd.action) {
953 case LFUN_MATH_DISPLAY:
954 mathDispatchCreation(cmd, true);
958 mathDispatchCreation(cmd, false);
961 case LFUN_MATH_IMPORT_SELECTION:
962 mathDispatchCreation(cmd, false);
965 case LFUN_MATH_MACRO:
966 if (cmd.argument.empty())
967 cmd.errorMessage(N_("Missing argument"));
969 string s = cmd.argument;
970 string const s1 = token(s, ' ', 1);
971 int const nargs = s1.empty() ? 0 : atoi(s1);
972 string const s2 = token(s, ' ', 2);
973 string const type = s2.empty() ? "newcommand" : s2;
974 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
978 case LFUN_INSERT_MATH:
979 case LFUN_INSERT_MATRIX:
980 case LFUN_MATH_DELIM: {
981 InsetFormula * f = new InsetFormula(bv);
982 if (openNewInset(bv, f)) {
983 bv->theLockingInset()->
984 dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
985 bv->theLockingInset()->dispatch(cmd);