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"
30 #include "funcrequest.h"
36 #include "frontends/LyXView.h"
37 #include "frontends/Dialogs.h"
39 #include "support/std_sstream.h"
40 #include "support/lstrings.h"
41 #include "support/lyxlib.h"
43 using lyx::support::atoi;
44 using lyx::support::split;
45 using lyx::support::token;
52 using std::istringstream;
55 MathCursor * mathcursor = 0;
63 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
65 if (!bv->insertInset(new_inset)) {
69 new_inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
78 InsetFormulaBase::InsetFormulaBase()
81 // This is needed as long the math parser is not re-entrant
83 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
84 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
85 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
86 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
90 // simply scrap this function if you want
91 void InsetFormulaBase::mutateToText()
96 latex(NULL, os, false, false);
97 string str = os.str();
100 LyXText * lt = view_->getLyXText();
101 string::const_iterator cit = str.begin();
102 string::const_iterator end = str.end();
103 for (; cit != end; ++cit)
104 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
107 //view_->owner()->dispatch(LFUN_ESCAPE);
112 void InsetFormulaBase::handleFont
113 (BufferView * bv, string const & arg, string const & font)
115 // this whole function is a hack and won't work for incremental font
117 recordUndo(bv, Undo::ATOMIC);
119 if (mathcursor->inset()->name() == font)
120 mathcursor->handleFont(font);
122 mathcursor->handleNest(createMathInset(font));
123 mathcursor->insert(arg);
128 void InsetFormulaBase::handleFont2(BufferView * bv, string const & arg)
130 recordUndo(bv, Undo::ATOMIC);
133 bv_funcs::string2font(arg, font, b);
134 if (font.color() != LColor::inherit) {
135 MathAtom at = createMathInset("color");
136 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
137 mathcursor->handleNest(at, 1);
143 void InsetFormulaBase::validate(LaTeXFeatures &) const
147 string const InsetFormulaBase::editMessage() const
149 return _("Math editor mode");
153 void InsetFormulaBase::insetUnlock(BufferView * bv)
156 if (mathcursor->inMacroMode()) {
157 mathcursor->macroModeClose();
158 bv->updateInset(this);
160 releaseMathCursor(bv);
163 generatePreview(*bv->buffer());
164 bv->updateInset(this);
168 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
170 mathcursor->getPos(x, y);
174 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
176 // calling metrics here destroys the cached xo,yo positions e.g. in
177 // MathParboxinset. And it would be too expensive anyway...
180 lyxerr << "getCursorPos - should not happen";
184 mathcursor->getPos(x, y);
185 x = mathcursor->targetX();
188 //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
192 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
199 //math_font_max_dim(font_, asc, des);
200 getCursorPos(bv, x, y);
202 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
203 bv->fitLockedInsetCursor(x, y, asc, des);
207 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
210 bv->updateInset(this);
214 dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
219 BufferView * bv = cmd.view();
220 bv->updateInset(this);
221 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
223 if (cmd.button() == mouse_button::button3) {
224 // try to dispatch to enclosed insets first
225 if (mathcursor->dispatch(cmd) == UNDISPATCHED) {
226 // launch math panel for right mouse button
227 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
228 bv->owner()->getDialogs().show("mathpanel");
233 if (cmd.button() == mouse_button::button2) {
235 asArray(bv->getClipboard(), ar);
236 mathcursor->selClear();
237 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
238 mathcursor->insert(ar);
239 bv->updateInset(this);
243 if (cmd.button() == mouse_button::button1) {
244 // try to dispatch to enclosed insets first
245 mathcursor->dispatch(cmd);
246 cmd.view()->stuffClipboard(mathcursor->grabSelection());
247 // try to set the cursor
249 //mathcursor = new MathCursor(this, x == 0);
251 //mathcursor->setPos(x + xo_, y + yo_);
259 dispatch_result InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
261 BufferView * bv = cmd.view();
262 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
264 if (!mathcursor || mathcursor->formula() != this) {
265 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
266 releaseMathCursor(bv);
267 mathcursor = new MathCursor(this, cmd.x == 0);
269 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
272 if (cmd.button() == mouse_button::button3) {
273 mathcursor->dispatch(cmd);
277 if (cmd.button() == mouse_button::button1) {
280 mathcursor->selClear();
281 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
282 mathcursor->dispatch(cmd);
286 bv->updateInset(this);
291 dispatch_result InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
296 if (mathcursor->dispatch(FuncRequest(cmd)) != UNDISPATCHED)
299 // only select with button 1
300 if (cmd.button() != mouse_button::button1)
303 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
309 if (!mathcursor->selection())
310 mathcursor->selStart();
312 BufferView * bv = cmd.view();
313 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
314 bv->updateInset(this);
319 dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
321 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
322 // << " arg: '" << cmd.argument
323 // << "' x: '" << cmd.x
324 // << " y: '" << cmd.y
325 // << "' button: " << cmd.button() << endl;
327 BufferView * bv = cmd.view();
329 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
330 bool remove_inset = false;
332 switch (cmd.action) {
333 case LFUN_INSET_EDIT:
334 lyxerr << "Called EDIT with '" << cmd.argument << "'" << endl;
335 if (!bv->lockInset(this))
336 lyxerr << "Cannot lock math inset in edit call!" << endl;
337 releaseMathCursor(bv);
338 if (!cmd.argument.empty()) {
339 mathcursor = new MathCursor(this, cmd.argument == "left");
342 mathcursor = new MathCursor(this, true);
344 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
346 // if that is removed, we won't get the magenta box when entering an
347 // inset for the first time
348 bv->updateInset(this);
351 case LFUN_MOUSE_PRESS:
352 //lyxerr << "Mouse single press" << endl;
353 return lfunMousePress(cmd);
354 case LFUN_MOUSE_MOTION:
355 //lyxerr << "Mouse motion" << endl;
356 return lfunMouseMotion(cmd);
357 case LFUN_MOUSE_RELEASE:
358 //lyxerr << "Mouse single release" << endl;
359 return lfunMouseRelease(cmd);
360 case LFUN_MOUSE_DOUBLE:
361 //lyxerr << "Mouse double" << endl;
362 return localDispatch(FuncRequest(LFUN_WORDSEL));
370 string argument = cmd.argument;
371 dispatch_result result = DISPATCHED;
373 bool was_macro = mathcursor->inMacroMode();
374 bool was_selection = mathcursor->selection();
376 mathcursor->normalize();
379 switch (cmd.action) {
381 case LFUN_MATH_MUTATE:
382 case LFUN_MATH_DISPLAY:
383 case LFUN_MATH_NUMBER:
384 case LFUN_MATH_NONUMBER:
385 case LFUN_CELL_SPLIT:
387 case LFUN_DELETE_LINE_FORWARD:
388 case LFUN_INSERT_LABEL:
389 case LFUN_MATH_EXTERN:
390 case LFUN_TABULAR_FEATURE:
391 case LFUN_PASTESELECTION:
392 case LFUN_MATH_LIMITS:
393 recordUndo(bv, Undo::ATOMIC);
394 mathcursor->dispatch(cmd);
398 sel = true; // fall through...
400 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
401 //lyxerr << "calling scroll 20" << endl;
403 // write something to the minibuffer
404 //bv->owner()->message(mathcursor->info());
408 sel = true; // fall through
410 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
414 sel = true; // fall through
416 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
420 sel = true; // fall through
422 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
426 mathcursor->home(false);
427 mathcursor->end(true);
430 case LFUN_UP_PARAGRAPHSEL:
431 case LFUN_UP_PARAGRAPH:
432 case LFUN_DOWN_PARAGRAPHSEL:
433 case LFUN_DOWN_PARAGRAPH:
438 case LFUN_WORDLEFTSEL:
439 sel = true; // fall through
442 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
446 case LFUN_WORDRIGHTSEL:
447 sel = true; // fall through
450 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
455 case LFUN_BEGINNINGBUFSEL:
456 case LFUN_BEGINNINGBUF:
464 result = FINISHED_RIGHT;
467 case LFUN_CELL_FORWARD:
468 mathcursor->idxNext();
471 case LFUN_CELL_BACKWARD:
472 mathcursor->idxPrev();
475 case LFUN_DELETE_WORD_BACKWARD:
477 recordUndo(bv, Undo::ATOMIC);
478 if (!mathcursor->backspace()) {
484 case LFUN_DELETE_WORD_FORWARD:
486 recordUndo(bv, Undo::ATOMIC);
487 if (!mathcursor->erase()) {
494 // sprintf(dispatch_buffer, "%d %d",);
495 // dispatch_result = dispatch_buffer;
498 lyxerr << "LFUN_SETXY broken!" << endl;
501 istringstream is(cmd.argument.c_str());
503 mathcursor->setPos(x, y);
509 istringstream is(cmd.argument.c_str());
512 mathcursor->macroModeClose();
513 recordUndo(bv, Undo::ATOMIC);
514 mathcursor->selPaste(n);
519 recordUndo(bv, Undo::DELETE);
520 mathcursor->selCut();
524 mathcursor->selCopy();
528 // Special casing for superscript in case of LyX handling
530 case LFUN_CIRCUMFLEX:
531 if (cmd.argument.empty()) {
532 // do superscript if LyX handles
534 recordUndo(bv, Undo::ATOMIC);
535 mathcursor->script(true);
552 case LFUN_HUNG_UMLAUT:
556 case LFUN_FREEFONT_APPLY:
557 case LFUN_FREEFONT_UPDATE:
558 handleFont2(bv, cmd.argument);
561 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
562 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
563 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
564 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
565 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
566 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
567 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
568 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
569 //case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
570 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
573 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
574 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
576 handleFont(bv, cmd.argument, "textrm");
577 //bv->owner()->message(_("math text mode toggled"));
583 recordUndo(bv, Undo::ATOMIC);
584 mathcursor->setSize(arg);
589 case LFUN_INSERT_MATRIX: {
590 recordUndo(bv, Undo::ATOMIC);
595 istringstream is(argument);
596 is >> m >> n >> v_align >> h_align;
600 mathcursor->niceInsert(
601 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
605 case LFUN_SUPERSCRIPT:
608 recordUndo(bv, Undo::ATOMIC);
609 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
613 case LFUN_MATH_DELIM:
615 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
617 string rs = split(cmd.argument, ls, ' ');
618 // Reasonable default values
624 recordUndo(bv, Undo::ATOMIC);
625 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
629 case LFUN_SPACE_INSERT:
630 case LFUN_MATH_SPACE:
631 recordUndo(bv, Undo::ATOMIC);
632 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
636 bv->owner()->message(_("Invalid action in math mode!"));
640 case LFUN_EXEC_COMMAND:
641 result = UNDISPATCHED;
645 // interpret this as if a backslash was typed
646 recordUndo(bv, Undo::ATOMIC);
647 mathcursor->interpret('\\');
650 case LFUN_BREAKPARAGRAPH:
651 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
652 case LFUN_BREAKPARAGRAPH_SKIP:
656 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
657 // handling such that "self-insert" works on "arbitrary stuff" too, and
658 // math-insert only handles special math things like "matrix".
659 case LFUN_INSERT_MATH:
660 recordUndo(bv, Undo::ATOMIC);
661 mathcursor->niceInsert(argument);
665 case LFUN_SELFINSERT:
666 if (!argument.empty()) {
667 recordUndo(bv, Undo::ATOMIC);
668 if (argument.size() == 1)
669 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
671 mathcursor->insert(argument);
676 if (mathcursor->selection())
677 mathcursor->selClear();
679 result = UNDISPATCHED;
682 case LFUN_INSET_TOGGLE:
683 mathcursor->insetToggle();
686 case LFUN_DIALOG_SHOW:
687 result = UNDISPATCHED;
690 case LFUN_DIALOG_SHOW_NEW_INSET: {
691 string const & name = argument;
695 data = tmp.createDialogStr(name);
699 result = UNDISPATCHED;
701 bv->owner()->getDialogs().show(name, data, 0);
706 case LFUN_INSET_APPLY: {
707 string const name = cmd.getArg(0);
709 bv->owner()->getDialogs().getOpenInset(name);
712 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
713 result = base->localDispatch(fr);
716 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
717 mathcursor->insert(ar);
720 result = UNDISPATCHED;
727 result = UNDISPATCHED;
730 if (result == DISPATCHED)
731 bv->updateInset(this);
733 mathcursor->normalize();
736 BOOST_ASSERT(mathcursor);
738 if (mathcursor->selection() || was_selection)
739 toggleInsetSelection(bv);
741 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
742 result == UNDISPATCHED) {
745 cmd.view()->stuffClipboard(mathcursor->grabSelection());
747 releaseMathCursor(bv);
748 bv->unlockInset(this);
750 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
753 return result; // original version
757 void InsetFormulaBase::revealCodes(BufferView * bv) const
761 bv->owner()->message(mathcursor->info());
764 // write something to the minibuffer
765 // translate to latex
766 mathcursor->markInsert();
769 string str = os.str();
770 mathcursor->markErase();
771 string::size_type pos = 0;
773 for (string::iterator it = str.begin(); it != str.end(); ++it) {
776 else if (*it == '\0') {
778 pos = it - str.begin();
784 res = res.substr(pos - 30);
786 res = res.substr(0, 60);
787 bv->owner()->message(res);
792 InsetOld::Code InsetFormulaBase::lyxCode() const
794 return InsetOld::MATH_CODE;
798 int InsetFormulaBase::ylow() const
800 return yo_ - dim_.asc;
804 int InsetFormulaBase::yhigh() const
806 return yo_ + dim_.des;
810 int InsetFormulaBase::xlow() const
816 int InsetFormulaBase::xhigh() const
818 return xo_ + dim_.wid;
822 /////////////////////////////////////////////////////////////////////
825 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
831 static InsetFormulaBase * lastformula = 0;
832 static MathIterator current = MathIterator(ibegin(par().nucleus()));
834 static string laststr;
836 if (lastformula != this || laststr != str) {
837 //lyxerr << "reset lastformula to " << this << endl;
840 current = ibegin(par().nucleus());
842 mathed_parse_cell(ar, str);
846 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
848 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
849 if (it.cell().matchpart(ar, it.back().pos_)) {
850 bv->unlockInset(bv->theLockingInset());
851 if (!bv->lockInset(this)) {
852 lyxerr << "Cannot lock inset" << endl;
856 mathcursor = new MathCursor(this, true);
858 mathcursor->setSelection(it, ar.size());
861 bv->updateInset(this);
866 //lyxerr << "not found!" << endl;
872 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
875 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
876 return searchForward(bv, what, a, b);
880 bool InsetFormulaBase::display() const
882 return par()->asHullInset() && par()->asHullInset()->display();
886 string InsetFormulaBase::selectionAsString() const
888 return mathcursor ? mathcursor->grabSelection() : string();
891 /////////////////////////////////////////////////////////////////////
894 void mathDispatchCreation(FuncRequest const & cmd, bool display)
896 BufferView * bv = cmd.view();
897 // use selection if available..
899 //if (action == LFUN_MATH_IMPORT_SELECTION)
903 string sel = bv->getLyXText()->selectionAsString(*bv->buffer(), false);
906 InsetFormula * f = new InsetFormula(bv);
907 if (openNewInset(bv, f)) {
908 bv->theLockingInset()->
909 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
910 // don't do that also for LFUN_MATH_MODE unless you want end up with
911 // always changing to mathrm when opening an inlined inset
912 // -- I really hate "LyXfunc overloading"...
914 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
915 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
918 // create a macro if we see "\\newcommand" somewhere, and an ordinary
920 InsetFormulaBase * f;
921 if (sel.find("\\newcommand") == string::npos &&
922 sel.find("\\def") == string::npos)
923 f = new InsetFormula(sel);
925 f = new InsetFormulaMacro(sel);
926 bv->getLyXText()->cutSelection(true, false);
929 cmd.message(N_("Math editor mode"));
933 void mathDispatch(FuncRequest const & cmd)
935 BufferView * bv = cmd.view();
936 if (!bv->available())
939 switch (cmd.action) {
941 case LFUN_MATH_DISPLAY:
942 mathDispatchCreation(cmd, true);
946 mathDispatchCreation(cmd, false);
949 case LFUN_MATH_IMPORT_SELECTION:
950 mathDispatchCreation(cmd, false);
953 case LFUN_MATH_MACRO:
954 if (cmd.argument.empty())
955 cmd.errorMessage(N_("Missing argument"));
957 string s = cmd.argument;
958 string const s1 = token(s, ' ', 1);
959 int const nargs = s1.empty() ? 0 : atoi(s1);
960 string const s2 = token(s, ' ', 2);
961 string const type = s2.empty() ? "newcommand" : s2;
962 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
966 case LFUN_INSERT_MATH:
967 case LFUN_INSERT_MATRIX:
968 case LFUN_MATH_DELIM: {
969 InsetFormula * f = new InsetFormula(bv);
970 if (openNewInset(bv, f)) {
971 bv->theLockingInset()->
972 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
973 bv->theLockingInset()->localDispatch(cmd);