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->dispatch(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);
320 InsetFormulaBase::priv_dispatch(FuncRequest const & cmd,
321 idx_type &, pos_type &)
323 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
324 // << " arg: '" << cmd.argument
325 // << "' x: '" << cmd.x
326 // << " y: '" << cmd.y
327 // << "' button: " << cmd.button() << endl;
329 BufferView * bv = cmd.view();
331 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
332 bool remove_inset = false;
334 switch (cmd.action) {
335 case LFUN_INSET_EDIT:
336 lyxerr << "Called EDIT with '" << cmd.argument << "'" << endl;
337 if (!bv->lockInset(this))
338 lyxerr << "Cannot lock math inset in edit call!" << endl;
339 releaseMathCursor(bv);
340 if (!cmd.argument.empty()) {
341 mathcursor = new MathCursor(this, cmd.argument == "left");
344 mathcursor = new MathCursor(this, true);
346 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
348 // if that is removed, we won't get the magenta box when entering an
349 // inset for the first time
350 bv->updateInset(this);
353 case LFUN_MOUSE_PRESS:
354 //lyxerr << "Mouse single press" << endl;
355 return lfunMousePress(cmd);
356 case LFUN_MOUSE_MOTION:
357 //lyxerr << "Mouse motion" << endl;
358 return lfunMouseMotion(cmd);
359 case LFUN_MOUSE_RELEASE:
360 //lyxerr << "Mouse single release" << endl;
361 return lfunMouseRelease(cmd);
362 case LFUN_MOUSE_DOUBLE:
363 //lyxerr << "Mouse double" << endl;
364 return dispatch(FuncRequest(LFUN_WORDSEL));
372 string argument = cmd.argument;
373 dispatch_result result = DISPATCHED;
375 bool was_macro = mathcursor->inMacroMode();
376 bool was_selection = mathcursor->selection();
378 mathcursor->normalize();
381 switch (cmd.action) {
383 case LFUN_MATH_MUTATE:
384 case LFUN_MATH_DISPLAY:
385 case LFUN_MATH_NUMBER:
386 case LFUN_MATH_NONUMBER:
387 case LFUN_CELL_SPLIT:
389 case LFUN_DELETE_LINE_FORWARD:
390 case LFUN_INSERT_LABEL:
391 case LFUN_MATH_EXTERN:
392 case LFUN_TABULAR_FEATURE:
393 case LFUN_PASTESELECTION:
394 case LFUN_MATH_LIMITS:
395 recordUndo(bv, Undo::ATOMIC);
396 mathcursor->dispatch(cmd);
400 sel = true; // fall through...
402 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
403 //lyxerr << "calling scroll 20" << endl;
405 // write something to the minibuffer
406 //bv->owner()->message(mathcursor->info());
410 sel = true; // fall through
412 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
416 sel = true; // fall through
418 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
422 sel = true; // fall through
424 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
428 mathcursor->home(false);
429 mathcursor->end(true);
432 case LFUN_UP_PARAGRAPHSEL:
433 case LFUN_UP_PARAGRAPH:
434 case LFUN_DOWN_PARAGRAPHSEL:
435 case LFUN_DOWN_PARAGRAPH:
440 case LFUN_WORDLEFTSEL:
441 sel = true; // fall through
444 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
448 case LFUN_WORDRIGHTSEL:
449 sel = true; // fall through
452 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
457 case LFUN_BEGINNINGBUFSEL:
458 case LFUN_BEGINNINGBUF:
466 result = FINISHED_RIGHT;
469 case LFUN_CELL_FORWARD:
470 mathcursor->idxNext();
473 case LFUN_CELL_BACKWARD:
474 mathcursor->idxPrev();
477 case LFUN_DELETE_WORD_BACKWARD:
479 recordUndo(bv, Undo::ATOMIC);
480 if (!mathcursor->backspace()) {
486 case LFUN_DELETE_WORD_FORWARD:
488 recordUndo(bv, Undo::ATOMIC);
489 if (!mathcursor->erase()) {
496 // sprintf(dispatch_buffer, "%d %d",);
497 // dispatch_result = dispatch_buffer;
500 lyxerr << "LFUN_SETXY broken!" << endl;
503 istringstream is(cmd.argument.c_str());
505 mathcursor->setPos(x, y);
511 istringstream is(cmd.argument.c_str());
514 mathcursor->macroModeClose();
515 recordUndo(bv, Undo::ATOMIC);
516 mathcursor->selPaste(n);
521 recordUndo(bv, Undo::DELETE);
522 mathcursor->selCut();
526 mathcursor->selCopy();
530 // Special casing for superscript in case of LyX handling
532 case LFUN_CIRCUMFLEX:
533 if (cmd.argument.empty()) {
534 // do superscript if LyX handles
536 recordUndo(bv, Undo::ATOMIC);
537 mathcursor->script(true);
554 case LFUN_HUNG_UMLAUT:
558 case LFUN_FREEFONT_APPLY:
559 case LFUN_FREEFONT_UPDATE:
560 handleFont2(bv, cmd.argument);
563 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
564 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
565 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
566 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
567 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
568 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
569 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
570 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
571 //case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
572 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
575 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
576 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
578 handleFont(bv, cmd.argument, "textrm");
579 //bv->owner()->message(_("math text mode toggled"));
585 recordUndo(bv, Undo::ATOMIC);
586 mathcursor->setSize(arg);
591 case LFUN_INSERT_MATRIX: {
592 recordUndo(bv, Undo::ATOMIC);
597 istringstream is(argument);
598 is >> m >> n >> v_align >> h_align;
602 mathcursor->niceInsert(
603 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
607 case LFUN_SUPERSCRIPT:
610 recordUndo(bv, Undo::ATOMIC);
611 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
615 case LFUN_MATH_DELIM:
617 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
619 string rs = split(cmd.argument, ls, ' ');
620 // Reasonable default values
626 recordUndo(bv, Undo::ATOMIC);
627 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
631 case LFUN_SPACE_INSERT:
632 case LFUN_MATH_SPACE:
633 recordUndo(bv, Undo::ATOMIC);
634 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
638 bv->owner()->message(_("Invalid action in math mode!"));
642 case LFUN_EXEC_COMMAND:
643 result = UNDISPATCHED;
647 // interpret this as if a backslash was typed
648 recordUndo(bv, Undo::ATOMIC);
649 mathcursor->interpret('\\');
652 case LFUN_BREAKPARAGRAPH:
653 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
654 case LFUN_BREAKPARAGRAPH_SKIP:
658 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
659 // handling such that "self-insert" works on "arbitrary stuff" too, and
660 // math-insert only handles special math things like "matrix".
661 case LFUN_INSERT_MATH:
662 recordUndo(bv, Undo::ATOMIC);
663 mathcursor->niceInsert(argument);
667 case LFUN_SELFINSERT:
668 if (!argument.empty()) {
669 recordUndo(bv, Undo::ATOMIC);
670 if (argument.size() == 1)
671 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
673 mathcursor->insert(argument);
678 if (mathcursor->selection())
679 mathcursor->selClear();
681 result = UNDISPATCHED;
684 case LFUN_INSET_TOGGLE:
685 mathcursor->insetToggle();
688 case LFUN_DIALOG_SHOW:
689 result = UNDISPATCHED;
692 case LFUN_DIALOG_SHOW_NEW_INSET: {
693 string const & name = argument;
697 data = tmp.createDialogStr(name);
701 result = UNDISPATCHED;
703 bv->owner()->getDialogs().show(name, data, 0);
708 case LFUN_INSET_APPLY: {
709 string const name = cmd.getArg(0);
711 bv->owner()->getDialogs().getOpenInset(name);
714 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
715 result = base->dispatch(fr);
718 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
719 mathcursor->insert(ar);
722 result = UNDISPATCHED;
729 result = UNDISPATCHED;
732 if (result == DISPATCHED)
733 bv->updateInset(this);
735 mathcursor->normalize();
738 BOOST_ASSERT(mathcursor);
740 if (mathcursor->selection() || was_selection)
741 toggleInsetSelection(bv);
743 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
744 result == UNDISPATCHED) {
747 cmd.view()->stuffClipboard(mathcursor->grabSelection());
749 releaseMathCursor(bv);
750 bv->unlockInset(this);
752 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
755 return result; // original version
759 void InsetFormulaBase::revealCodes(BufferView * bv) const
763 bv->owner()->message(mathcursor->info());
766 // write something to the minibuffer
767 // translate to latex
768 mathcursor->markInsert();
771 string str = os.str();
772 mathcursor->markErase();
773 string::size_type pos = 0;
775 for (string::iterator it = str.begin(); it != str.end(); ++it) {
778 else if (*it == '\0') {
780 pos = it - str.begin();
786 res = res.substr(pos - 30);
788 res = res.substr(0, 60);
789 bv->owner()->message(res);
794 InsetOld::Code InsetFormulaBase::lyxCode() const
796 return InsetOld::MATH_CODE;
800 int InsetFormulaBase::ylow() const
802 return yo_ - dim_.asc;
806 int InsetFormulaBase::yhigh() const
808 return yo_ + dim_.des;
812 int InsetFormulaBase::xlow() const
818 int InsetFormulaBase::xhigh() const
820 return xo_ + dim_.wid;
824 /////////////////////////////////////////////////////////////////////
827 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
833 static InsetFormulaBase * lastformula = 0;
834 static MathIterator current = MathIterator(ibegin(par().nucleus()));
836 static string laststr;
838 if (lastformula != this || laststr != str) {
839 //lyxerr << "reset lastformula to " << this << endl;
842 current = ibegin(par().nucleus());
844 mathed_parse_cell(ar, str);
848 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
850 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
851 if (it.cell().matchpart(ar, it.back().pos_)) {
852 bv->unlockInset(bv->theLockingInset());
853 if (!bv->lockInset(this)) {
854 lyxerr << "Cannot lock inset" << endl;
858 mathcursor = new MathCursor(this, true);
860 mathcursor->setSelection(it, ar.size());
863 bv->updateInset(this);
868 //lyxerr << "not found!" << endl;
874 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
877 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
878 return searchForward(bv, what, a, b);
882 bool InsetFormulaBase::display() const
884 return par()->asHullInset() && par()->asHullInset()->display();
888 string InsetFormulaBase::selectionAsString() const
890 return mathcursor ? mathcursor->grabSelection() : string();
893 /////////////////////////////////////////////////////////////////////
896 void mathDispatchCreation(FuncRequest const & cmd, bool display)
898 BufferView * bv = cmd.view();
899 // use selection if available..
901 //if (action == LFUN_MATH_IMPORT_SELECTION)
905 string sel = bv->getLyXText()->selectionAsString(*bv->buffer(), false);
908 InsetFormula * f = new InsetFormula(bv);
909 if (openNewInset(bv, f)) {
910 bv->theLockingInset()->
911 dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
912 // don't do that also for LFUN_MATH_MODE unless you want end up with
913 // always changing to mathrm when opening an inlined inset
914 // -- I really hate "LyXfunc overloading"...
916 f->dispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
917 f->dispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
920 // create a macro if we see "\\newcommand" somewhere, and an ordinary
922 InsetFormulaBase * f;
923 if (sel.find("\\newcommand") == string::npos &&
924 sel.find("\\def") == string::npos)
925 f = new InsetFormula(sel);
927 f = new InsetFormulaMacro(sel);
928 bv->getLyXText()->cutSelection(true, false);
931 cmd.message(N_("Math editor mode"));
935 void mathDispatch(FuncRequest const & cmd)
937 BufferView * bv = cmd.view();
938 if (!bv->available())
941 switch (cmd.action) {
943 case LFUN_MATH_DISPLAY:
944 mathDispatchCreation(cmd, true);
948 mathDispatchCreation(cmd, false);
951 case LFUN_MATH_IMPORT_SELECTION:
952 mathDispatchCreation(cmd, false);
955 case LFUN_MATH_MACRO:
956 if (cmd.argument.empty())
957 cmd.errorMessage(N_("Missing argument"));
959 string s = cmd.argument;
960 string const s1 = token(s, ' ', 1);
961 int const nargs = s1.empty() ? 0 : atoi(s1);
962 string const s2 = token(s, ' ', 2);
963 string const type = s2.empty() ? "newcommand" : s2;
964 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
968 case LFUN_INSERT_MATH:
969 case LFUN_INSERT_MATRIX:
970 case LFUN_MATH_DELIM: {
971 InsetFormula * f = new InsetFormula(bv);
972 if (openNewInset(bv, f)) {
973 bv->theLockingInset()->
974 dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
975 bv->theLockingInset()->dispatch(cmd);