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 "formulabase.h"
17 #include "formulamacro.h"
18 #include "math_support.h"
19 #include "math_arrayinset.h"
20 #include "math_deliminset.h"
21 #include "math_cursor.h"
22 #include "math_factory.h"
23 #include "math_hullinset.h"
24 #include "math_parser.h"
25 #include "math_spaceinset.h"
26 #include "ref_inset.h"
28 #include "BufferView.h"
29 #include "bufferview_funcs.h"
30 #include "dispatchresult.h"
32 #include "funcrequest.h"
38 #include "frontends/LyXView.h"
39 #include "frontends/Dialogs.h"
41 #include "support/std_sstream.h"
42 #include "support/lstrings.h"
43 #include "support/lyxlib.h"
45 using lyx::support::atoi;
46 using lyx::support::split;
47 using lyx::support::token;
54 using std::istringstream;
57 MathCursor * mathcursor = 0;
65 bool openNewInset(BufferView * bv, UpdatableInset * inset)
67 if (!bv->insertInset(inset)) {
71 inset->edit(bv, true);
80 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 releaseMathCursor(bv);
162 generatePreview(*bv->buffer());
167 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
169 mathcursor->getPos(x, y);
173 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
176 lyxerr << "getCursorPos - should not happen";
181 mathcursor->getPos(x, y);
182 x = mathcursor->targetX();
185 lyxerr << "InsetFormulaBase::getCursorPos: " << x << ' ' << y << endl;
189 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
196 //math_font_max_dim(font_, asc, des);
197 getCursorPos(bv, x, y);
199 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
200 bv->fitLockedInsetCursor(x, y, asc, des);
204 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
211 DispatchResult InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
214 return DispatchResult(false);
216 BufferView * bv = cmd.view();
218 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
220 if (cmd.button() == mouse_button::button3) {
221 // try to dispatch to enclosed insets first
222 if (!mathcursor->dispatch(cmd).dispatched()) {
223 // launch math panel for right mouse button
224 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
225 bv->owner()->getDialogs().show("mathpanel");
227 return DispatchResult(true, true);
230 if (cmd.button() == mouse_button::button2) {
232 asArray(bv->getClipboard(), ar);
233 mathcursor->selClear();
234 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
235 mathcursor->insert(ar);
237 return DispatchResult(true, true);
240 if (cmd.button() == mouse_button::button1) {
241 // try to dispatch to enclosed insets first
242 mathcursor->dispatch(cmd);
243 cmd.view()->stuffClipboard(mathcursor->grabSelection());
244 // try to set the cursor
246 //mathcursor = new MathCursor(this, x == 0);
248 //mathcursor->setPos(x + xo_, y + yo_);
249 return DispatchResult(true, true);
252 return DispatchResult(false);
256 DispatchResult InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
258 BufferView * bv = cmd.view();
259 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
261 if (!mathcursor || mathcursor->formula() != this) {
262 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
263 releaseMathCursor(bv);
264 mathcursor = new MathCursor(this, cmd.x == 0);
266 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
269 if (cmd.button() == mouse_button::button3) {
270 mathcursor->dispatch(cmd);
271 return DispatchResult(true, true);
274 if (cmd.button() == mouse_button::button1) {
277 mathcursor->selClear();
278 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
279 mathcursor->dispatch(cmd);
280 return DispatchResult(true, true);
284 return DispatchResult(true, true);
288 DispatchResult InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
291 return DispatchResult(true, true);
293 if (mathcursor->dispatch(FuncRequest(cmd)).dispatched())
294 return DispatchResult(true, true);
296 // only select with button 1
297 if (cmd.button() != mouse_button::button1)
298 return DispatchResult(true, true);
300 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
301 return DispatchResult(true, true);
306 if (!mathcursor->selection())
307 mathcursor->selStart();
309 BufferView * bv = cmd.view();
310 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
312 return DispatchResult(true, true);
316 void InsetFormulaBase::edit(BufferView * bv, bool left)
318 lyxerr << "Called FormulaBase::edit" << endl;
319 releaseMathCursor(bv);
320 mathcursor = new MathCursor(this, left);
321 bv->cursor().push(this);
322 // if that is removed, we won't get the magenta box when entering an
323 // inset for the first time
328 void InsetFormulaBase::edit(BufferView * bv, int x, int y)
330 lyxerr << "Called FormulaBase::EDIT with '" << x << ' ' << y << "'" << endl;
331 releaseMathCursor(bv);
332 mathcursor = new MathCursor(this, true);
334 mathcursor->setPos(x + xo_, y + yo_);
335 bv->cursor().push(this);
336 // if that is removed, we won't get the magenta box when entering an
337 // inset for the first time
343 InsetFormulaBase::priv_dispatch(FuncRequest const & cmd,
344 idx_type &, pos_type &)
346 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
347 // << " arg: '" << cmd.argument
348 // << "' x: '" << cmd.x
349 // << " y: '" << cmd.y
350 // << "' button: " << cmd.button() << endl;
352 BufferView * bv = cmd.view();
354 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
355 bool remove_inset = false;
357 switch (cmd.action) {
358 case LFUN_MOUSE_PRESS:
359 //lyxerr << "Mouse single press" << endl;
360 return lfunMousePress(cmd);
361 case LFUN_MOUSE_MOTION:
362 //lyxerr << "Mouse motion" << endl;
363 return lfunMouseMotion(cmd);
364 case LFUN_MOUSE_RELEASE:
365 //lyxerr << "Mouse single release" << endl;
366 return lfunMouseRelease(cmd);
367 case LFUN_MOUSE_DOUBLE:
368 //lyxerr << "Mouse double" << endl;
369 return dispatch(FuncRequest(LFUN_WORDSEL));
375 return DispatchResult(false);
377 string argument = cmd.argument;
378 DispatchResult result(true);
380 bool was_macro = mathcursor->inMacroMode();
381 bool was_selection = mathcursor->selection();
383 mathcursor->normalize();
386 switch (cmd.action) {
388 case LFUN_MATH_MUTATE:
389 case LFUN_MATH_DISPLAY:
390 case LFUN_MATH_NUMBER:
391 case LFUN_MATH_NONUMBER:
392 case LFUN_CELL_SPLIT:
394 case LFUN_DELETE_LINE_FORWARD:
395 case LFUN_INSERT_LABEL:
396 case LFUN_MATH_EXTERN:
397 case LFUN_TABULAR_FEATURE:
398 case LFUN_PASTESELECTION:
399 case LFUN_MATH_LIMITS:
400 recordUndo(bv, Undo::ATOMIC);
401 mathcursor->dispatch(cmd);
405 sel = true; // fall through...
407 result = mathcursor->right(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
408 //lyxerr << "calling scroll 20" << endl;
410 // write something to the minibuffer
411 //bv->owner()->message(mathcursor->info());
415 sel = true; // fall through
417 result = mathcursor->left(sel) ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
421 sel = true; // fall through
423 result = mathcursor->up(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_UP);
427 sel = true; // fall through
429 result = mathcursor->down(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_DOWN);
433 mathcursor->home(false);
434 mathcursor->end(true);
437 case LFUN_UP_PARAGRAPHSEL:
438 case LFUN_UP_PARAGRAPH:
439 case LFUN_DOWN_PARAGRAPHSEL:
440 case LFUN_DOWN_PARAGRAPH:
441 result = DispatchResult(true, FINISHED);
445 case LFUN_WORDLEFTSEL:
446 sel = true; // fall through
449 result = mathcursor->home(sel) ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
453 case LFUN_WORDRIGHTSEL:
454 sel = true; // fall through
457 result = mathcursor->end(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
462 case LFUN_BEGINNINGBUFSEL:
463 case LFUN_BEGINNINGBUF:
464 result = DispatchResult(true, FINISHED);
471 result = DispatchResult(false, FINISHED_RIGHT);
474 case LFUN_CELL_FORWARD:
475 mathcursor->idxNext();
478 case LFUN_CELL_BACKWARD:
479 mathcursor->idxPrev();
482 case LFUN_DELETE_WORD_BACKWARD:
484 recordUndo(bv, Undo::ATOMIC);
485 if (!mathcursor->backspace()) {
486 result = DispatchResult(true, FINISHED);
491 case LFUN_DELETE_WORD_FORWARD:
493 recordUndo(bv, Undo::ATOMIC);
494 if (!mathcursor->erase()) {
495 result = DispatchResult(true, FINISHED);
501 // sprintf(dispatch_buffer, "%d %d",);
502 // DispatchResult= dispatch_buffer;
505 lyxerr << "LFUN_SETXY broken!" << endl;
508 istringstream is(cmd.argument.c_str());
510 mathcursor->setPos(x, y);
516 istringstream is(cmd.argument.c_str());
519 mathcursor->macroModeClose();
520 recordUndo(bv, Undo::ATOMIC);
521 mathcursor->selPaste(n);
526 recordUndo(bv, Undo::DELETE);
527 mathcursor->selCut();
531 mathcursor->selCopy();
535 // Special casing for superscript in case of LyX handling
537 case LFUN_CIRCUMFLEX:
538 if (cmd.argument.empty()) {
539 // do superscript if LyX handles
541 recordUndo(bv, Undo::ATOMIC);
542 mathcursor->script(true);
559 case LFUN_HUNG_UMLAUT:
563 case LFUN_FREEFONT_APPLY:
564 case LFUN_FREEFONT_UPDATE:
565 handleFont2(bv, cmd.argument);
568 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
569 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
570 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
571 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
572 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
573 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
574 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
575 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
576 //case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
577 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
580 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
581 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
583 handleFont(bv, cmd.argument, "textrm");
584 //bv->owner()->message(_("math text mode toggled"));
590 recordUndo(bv, Undo::ATOMIC);
591 mathcursor->setSize(arg);
596 case LFUN_INSERT_MATRIX: {
597 recordUndo(bv, Undo::ATOMIC);
602 istringstream is(argument);
603 is >> m >> n >> v_align >> h_align;
607 mathcursor->niceInsert(
608 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
612 case LFUN_SUPERSCRIPT:
615 recordUndo(bv, Undo::ATOMIC);
616 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
620 case LFUN_MATH_DELIM:
622 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
624 string rs = split(cmd.argument, ls, ' ');
625 // Reasonable default values
631 recordUndo(bv, Undo::ATOMIC);
632 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
636 case LFUN_SPACE_INSERT:
637 case LFUN_MATH_SPACE:
638 recordUndo(bv, Undo::ATOMIC);
639 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
643 bv->owner()->message(_("Invalid action in math mode!"));
647 case LFUN_EXEC_COMMAND:
648 result = DispatchResult(false);
652 // interpret this as if a backslash was typed
653 recordUndo(bv, Undo::ATOMIC);
654 mathcursor->interpret('\\');
657 case LFUN_BREAKPARAGRAPH:
658 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
659 case LFUN_BREAKPARAGRAPH_SKIP:
663 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
664 // handling such that "self-insert" works on "arbitrary stuff" too, and
665 // math-insert only handles special math things like "matrix".
666 case LFUN_INSERT_MATH:
667 recordUndo(bv, Undo::ATOMIC);
668 mathcursor->niceInsert(argument);
672 case LFUN_SELFINSERT:
673 if (!argument.empty()) {
674 recordUndo(bv, Undo::ATOMIC);
675 if (argument.size() == 1)
676 result = mathcursor->interpret(argument[0]) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
678 mathcursor->insert(argument);
683 if (mathcursor->selection())
684 mathcursor->selClear();
686 result = DispatchResult(false);
689 case LFUN_INSET_TOGGLE:
690 mathcursor->insetToggle();
693 case LFUN_DIALOG_SHOW:
694 result = DispatchResult(false);
697 case LFUN_DIALOG_SHOW_NEW_INSET: {
698 string const & name = argument;
702 data = tmp.createDialogStr(name);
706 result = DispatchResult(false);
708 bv->owner()->getDialogs().show(name, data, 0);
712 case LFUN_INSET_APPLY: {
713 string const name = cmd.getArg(0);
715 bv->owner()->getDialogs().getOpenInset(name);
718 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
719 result = base->dispatch(fr);
722 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
723 mathcursor->insert(ar);
724 result = DispatchResult(true, true);
726 result = DispatchResult(false);
733 result = DispatchResult(false);
736 if (result == DispatchResult(true, true))
739 mathcursor->normalize();
742 BOOST_ASSERT(mathcursor);
744 if (mathcursor->selection() || was_selection)
745 toggleInsetSelection(bv);
747 if (result.dispatched()) {
750 cmd.view()->stuffClipboard(mathcursor->grabSelection());
752 releaseMathCursor(bv);
754 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
757 return result; // original version
761 void InsetFormulaBase::revealCodes(BufferView * bv) const
765 bv->owner()->message(mathcursor->info());
768 // write something to the minibuffer
769 // translate to latex
770 mathcursor->markInsert();
773 string str = os.str();
774 mathcursor->markErase();
775 string::size_type pos = 0;
777 for (string::iterator it = str.begin(); it != str.end(); ++it) {
780 else if (*it == '\0') {
782 pos = it - str.begin();
788 res = res.substr(pos - 30);
790 res = res.substr(0, 60);
791 bv->owner()->message(res);
796 InsetOld::Code InsetFormulaBase::lyxCode() const
798 return InsetOld::MATH_CODE;
802 int InsetFormulaBase::ylow() const
804 return yo_ - dim_.asc;
808 int InsetFormulaBase::yhigh() const
810 return yo_ + dim_.des;
814 int InsetFormulaBase::xlow() const
820 int InsetFormulaBase::xhigh() const
822 return xo_ + dim_.wid;
826 /////////////////////////////////////////////////////////////////////
829 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
835 static InsetFormulaBase * lastformula = 0;
836 static MathIterator current = MathIterator(ibegin(par().nucleus()));
838 static string laststr;
840 if (lastformula != this || laststr != str) {
841 //lyxerr << "reset lastformula to " << this << endl;
844 current = ibegin(par().nucleus());
846 mathed_parse_cell(ar, str);
850 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
852 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
853 if (it.cell().matchpart(ar, it.back().pos_)) {
855 mathcursor = new MathCursor(this, true);
857 mathcursor->setSelection(it, ar.size());
865 //lyxerr << "not found!" << endl;
871 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
874 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
875 return searchForward(bv, what, a, b);
879 bool InsetFormulaBase::display() const
881 return par()->asHullInset() && par()->asHullInset()->display();
885 string InsetFormulaBase::selectionAsString() const
887 return mathcursor ? mathcursor->grabSelection() : string();
890 /////////////////////////////////////////////////////////////////////
893 void mathDispatchCreation(FuncRequest const & cmd, bool display)
895 BufferView * bv = cmd.view();
896 // use selection if available..
898 //if (action == LFUN_MATH_IMPORT_SELECTION)
902 string sel = bv->getLyXText()->selectionAsString(*bv->buffer(), false);
905 InsetFormula * f = new InsetFormula(bv);
906 if (openNewInset(bv, f)) {
907 bv->cursor().innerInset()->
908 dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
909 // don't do that also for LFUN_MATH_MODE unless you want end up with
910 // always changing to mathrm when opening an inlined inset
911 // -- I really hate "LyXfunc overloading"...
913 f->dispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
914 f->dispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
917 // create a macro if we see "\\newcommand" somewhere, and an ordinary
919 InsetFormulaBase * f;
920 if (sel.find("\\newcommand") == string::npos &&
921 sel.find("\\def") == string::npos)
922 f = new InsetFormula(sel);
924 f = new InsetFormulaMacro(sel);
925 bv->getLyXText()->cutSelection(true, false);
928 cmd.message(N_("Math editor mode"));
932 void mathDispatch(FuncRequest const & cmd)
934 BufferView * bv = cmd.view();
935 if (!bv->available())
938 switch (cmd.action) {
940 case LFUN_MATH_DISPLAY:
941 mathDispatchCreation(cmd, true);
945 mathDispatchCreation(cmd, false);
948 case LFUN_MATH_IMPORT_SELECTION:
949 mathDispatchCreation(cmd, false);
952 case LFUN_MATH_MACRO:
953 if (cmd.argument.empty())
954 cmd.errorMessage(N_("Missing argument"));
956 string s = cmd.argument;
957 string const s1 = token(s, ' ', 1);
958 int const nargs = s1.empty() ? 0 : atoi(s1);
959 string const s2 = token(s, ' ', 2);
960 string const type = s2.empty() ? "newcommand" : s2;
961 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
965 case LFUN_INSERT_MATH:
966 case LFUN_INSERT_MATRIX:
967 case LFUN_MATH_DELIM: {
968 InsetFormula * f = new InsetFormula(bv);
969 if (openNewInset(bv, f)) {
970 UpdatableInset * inset = bv->cursor().innerInset();
971 inset->dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
972 inset->dispatch(cmd);