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"
15 #include "support/std_sstream.h"
17 #include "formulamacro.h"
18 #include "funcrequest.h"
19 #include "BufferView.h"
23 #include "math_support.h"
24 #include "support/lstrings.h"
25 #include "support/lyxlib.h"
26 #include "frontends/LyXView.h"
27 #include "math_arrayinset.h"
28 #include "math_deliminset.h"
29 #include "math_cursor.h"
30 #include "math_factory.h"
31 #include "math_hullinset.h"
32 #include "math_parser.h"
33 #include "math_spaceinset.h"
34 #include "undo_funcs.h"
35 #include "frontends/Dialogs.h"
36 #include "ref_inset.h"
38 using lyx::support::atoi;
39 using lyx::support::split;
40 using lyx::support::token;
46 using std::istringstream;
49 MathCursor * mathcursor = 0;
57 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
59 if (!bv->insertInset(new_inset)) {
63 new_inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
72 InsetFormulaBase::InsetFormulaBase()
73 : view_(0), xo_(0), yo_(0)
75 // This is needed as long the math parser is not re-entrant
77 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
78 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
79 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
80 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
84 // simply scrap this function if you want
85 void InsetFormulaBase::mutateToText()
90 latex(NULL, os, false, false);
91 string str = os.str();
94 LyXText * lt = view_->getLyXText();
95 string::const_iterator cit = str.begin();
96 string::const_iterator end = str.end();
97 for (; cit != end; ++cit)
98 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
101 //view_->owner()->dispatch(LFUN_ESCAPE);
106 void InsetFormulaBase::handleFont
107 (BufferView * bv, string const & arg, string const & font)
109 // this whole function is a hack and won't work for incremental font
111 recordUndo(bv, Undo::ATOMIC);
113 if (mathcursor->inset()->name() == font)
114 mathcursor->handleFont(font);
116 mathcursor->handleNest(createMathInset(font));
117 mathcursor->insert(arg);
122 BufferView * InsetFormulaBase::view() const
128 void InsetFormulaBase::validate(LaTeXFeatures &) const
132 string const InsetFormulaBase::editMessage() const
134 return _("Math editor mode");
138 void InsetFormulaBase::insetUnlock(BufferView * bv)
141 if (mathcursor->inMacroMode()) {
142 mathcursor->macroModeClose();
143 bv->updateInset(this);
145 releaseMathCursor(bv);
148 generatePreview(*bv->buffer());
149 bv->updateInset(this);
153 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
155 mathcursor->getPos(x, y);
159 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
161 // calling metrics here destroys the cached xo,yo positions e.g. in
162 // MathParboxinset. And it would be too expensive anyway...
165 lyxerr << "getCursorPos - should not happen";
169 mathcursor->getPos(x, y);
170 x = mathcursor->targetX();
173 //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
177 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
184 //math_font_max_dim(font_, asc, des);
185 getCursorPos(bv, x, y);
187 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
188 bv->fitLockedInsetCursor(x, y, asc, des);
192 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
195 bv->updateInset(this);
199 dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
204 BufferView * bv = cmd.view();
205 bv->updateInset(this);
206 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
208 if (cmd.button() == mouse_button::button3) {
209 // try to dispatch to enclosed insets first
210 if (mathcursor->dispatch(cmd) == UNDISPATCHED) {
211 // launch math panel for right mouse button
212 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
213 bv->owner()->getDialogs().show("mathpanel");
218 if (cmd.button() == mouse_button::button2) {
220 asArray(bv->getClipboard(), ar);
221 mathcursor->selClear();
222 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
223 mathcursor->insert(ar);
224 bv->updateInset(this);
228 if (cmd.button() == mouse_button::button1) {
229 // try to dispatch to enclosed insets first
230 mathcursor->dispatch(cmd);
231 cmd.view()->stuffClipboard(mathcursor->grabSelection());
232 // try to set the cursor
234 //mathcursor = new MathCursor(this, x == 0);
236 //mathcursor->setPos(x + xo_, y + yo_);
244 dispatch_result InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
246 BufferView * bv = cmd.view();
247 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
249 if (!mathcursor || mathcursor->formula() != this) {
250 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
251 releaseMathCursor(bv);
252 mathcursor = new MathCursor(this, cmd.x == 0);
254 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
257 if (cmd.button() == mouse_button::button3) {
258 mathcursor->dispatch(cmd);
262 if (cmd.button() == mouse_button::button1) {
265 mathcursor->selClear();
266 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
267 mathcursor->dispatch(cmd);
271 bv->updateInset(this);
276 dispatch_result InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
281 if (mathcursor->dispatch(FuncRequest(cmd)) != UNDISPATCHED)
284 // only select with button 1
285 if (cmd.button() != mouse_button::button1)
288 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
294 if (!mathcursor->selection())
295 mathcursor->selStart();
297 BufferView * bv = cmd.view();
298 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
299 bv->updateInset(this);
304 dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
306 lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
307 << " arg: '" << cmd.argument
310 << "' button: " << cmd.button() << endl;
312 BufferView * bv = cmd.view();
314 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
315 bool remove_inset = false;
317 switch (cmd.action) {
318 case LFUN_INSET_EDIT:
319 lyxerr << "Called EDIT with '" << cmd.argument << "'" << endl;
320 if (!bv->lockInset(this))
321 lyxerr << "Cannot lock math inset in edit call!" << endl;
322 releaseMathCursor(bv);
323 if (!cmd.argument.empty()) {
324 mathcursor = new MathCursor(this, cmd.argument == "left");
327 mathcursor = new MathCursor(this, true);
329 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
331 // if that is removed, we won't get the magenta box when entering an
332 // inset for the first time
333 bv->updateInset(this);
336 case LFUN_MOUSE_PRESS:
337 //lyxerr << "Mouse single press" << endl;
338 return lfunMousePress(cmd);
339 case LFUN_MOUSE_MOTION:
340 //lyxerr << "Mouse motion" << endl;
341 return lfunMouseMotion(cmd);
342 case LFUN_MOUSE_RELEASE:
343 //lyxerr << "Mouse single release" << endl;
344 return lfunMouseRelease(cmd);
345 case LFUN_MOUSE_DOUBLE:
346 //lyxerr << "Mouse double" << endl;
347 return localDispatch(FuncRequest(LFUN_WORDSEL));
355 string argument = cmd.argument;
356 RESULT result = DISPATCHED;
358 bool was_macro = mathcursor->inMacroMode();
359 bool was_selection = mathcursor->selection();
361 mathcursor->normalize();
364 switch (cmd.action) {
366 case LFUN_MATH_MUTATE:
367 case LFUN_MATH_DISPLAY:
368 case LFUN_MATH_NUMBER:
369 case LFUN_MATH_NONUMBER:
370 case LFUN_CELL_SPLIT:
372 case LFUN_DELETE_LINE_FORWARD:
373 case LFUN_INSERT_LABEL:
374 case LFUN_MATH_EXTERN:
375 case LFUN_TABULAR_FEATURE:
376 case LFUN_PASTESELECTION:
377 case LFUN_MATH_LIMITS:
378 recordUndo(bv, Undo::ATOMIC);
379 mathcursor->dispatch(cmd);
383 sel = true; // fall through...
385 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
386 //lyxerr << "calling scroll 20" << endl;
388 // write something to the minibuffer
389 //bv->owner()->message(mathcursor->info());
393 sel = true; // fall through
395 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
399 sel = true; // fall through
401 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
405 sel = true; // fall through
407 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
411 mathcursor->home(false);
412 mathcursor->end(true);
415 case LFUN_UP_PARAGRAPHSEL:
416 case LFUN_UP_PARAGRAPH:
417 case LFUN_DOWN_PARAGRAPHSEL:
418 case LFUN_DOWN_PARAGRAPH:
423 case LFUN_WORDLEFTSEL:
424 sel = true; // fall through
427 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
431 case LFUN_WORDRIGHTSEL:
432 sel = true; // fall through
435 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
440 case LFUN_BEGINNINGBUFSEL:
441 case LFUN_BEGINNINGBUF:
449 result = FINISHED_RIGHT;
452 case LFUN_CELL_FORWARD:
453 mathcursor->idxNext();
456 case LFUN_CELL_BACKWARD:
457 mathcursor->idxPrev();
460 case LFUN_DELETE_WORD_BACKWARD:
462 recordUndo(bv, Undo::ATOMIC);
463 if (!mathcursor->backspace()) {
469 case LFUN_DELETE_WORD_FORWARD:
471 recordUndo(bv, Undo::ATOMIC);
472 if (!mathcursor->erase()) {
479 // sprintf(dispatch_buffer, "%d %d",);
480 // dispatch_result = dispatch_buffer;
483 lyxerr << "LFUN_SETXY broken!" << endl;
486 istringstream is(cmd.argument.c_str());
488 mathcursor->setPos(x, y);
494 istringstream is(cmd.argument.c_str());
497 mathcursor->macroModeClose();
498 recordUndo(bv, Undo::ATOMIC);
499 mathcursor->selPaste(n);
504 recordUndo(bv, Undo::DELETE);
505 mathcursor->selCut();
509 mathcursor->selCopy();
513 // Special casing for superscript in case of LyX handling
515 case LFUN_CIRCUMFLEX:
516 if (cmd.argument.empty()) {
517 // do superscript if LyX handles
519 recordUndo(bv, Undo::ATOMIC);
520 mathcursor->script(true);
537 case LFUN_HUNG_UMLAUT:
541 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
542 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
543 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
544 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
545 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
546 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
547 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
548 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
549 case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
550 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
553 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
554 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
556 handleFont(bv, cmd.argument, "textrm");
557 //bv->owner()->message(_("math text mode toggled"));
563 recordUndo(bv, Undo::ATOMIC);
564 mathcursor->setSize(arg);
569 case LFUN_INSERT_MATRIX: {
570 recordUndo(bv, Undo::ATOMIC);
575 istringstream is(argument);
576 is >> m >> n >> v_align >> h_align;
580 mathcursor->niceInsert(
581 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
585 case LFUN_SUPERSCRIPT:
588 recordUndo(bv, Undo::ATOMIC);
589 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
593 case LFUN_MATH_DELIM:
595 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
597 string rs = split(cmd.argument, ls, ' ');
598 // Reasonable default values
604 recordUndo(bv, Undo::ATOMIC);
605 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
609 case LFUN_SPACE_INSERT:
610 case LFUN_MATH_SPACE:
611 recordUndo(bv, Undo::ATOMIC);
612 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
616 bv->owner()->message(_("Invalid action in math mode!"));
620 case LFUN_EXEC_COMMAND:
621 result = UNDISPATCHED;
625 // interpret this as if a backslash was typed
626 recordUndo(bv, Undo::ATOMIC);
627 mathcursor->interpret('\\');
630 case LFUN_BREAKPARAGRAPH:
631 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
632 case LFUN_BREAKPARAGRAPH_SKIP:
636 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
637 // handling such that "self-insert" works on "arbitrary stuff" too, and
638 // math-insert only handles special math things like "matrix".
639 case LFUN_INSERT_MATH:
640 recordUndo(bv, Undo::ATOMIC);
641 mathcursor->niceInsert(argument);
645 case LFUN_SELFINSERT:
646 if (!argument.empty()) {
647 recordUndo(bv, Undo::ATOMIC);
648 if (argument.size() == 1)
649 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
651 mathcursor->insert(argument);
656 if (mathcursor->selection())
657 mathcursor->selClear();
659 result = UNDISPATCHED;
662 case LFUN_INSET_TOGGLE:
663 mathcursor->insetToggle();
666 case LFUN_DIALOG_SHOW:
667 result = UNDISPATCHED;
670 case LFUN_DIALOG_SHOW_NEW_INSET: {
671 string const & name = argument;
675 data = tmp.createDialogStr(name);
679 result = UNDISPATCHED;
681 bv->owner()->getDialogs().show(name, data, 0);
686 case LFUN_INSET_APPLY: {
687 string const name = cmd.getArg(0);
689 bv->owner()->getDialogs().getOpenInset(name);
692 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
693 result = base->localDispatch(fr);
696 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
697 mathcursor->insert(ar);
700 result = UNDISPATCHED;
707 result = UNDISPATCHED;
710 if (result == DISPATCHED)
711 bv->updateInset(this);
713 mathcursor->normalize();
716 BOOST_ASSERT(mathcursor);
718 if (mathcursor->selection() || was_selection)
719 toggleInsetSelection(bv);
721 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
722 result == UNDISPATCHED) {
725 cmd.view()->stuffClipboard(mathcursor->grabSelection());
727 releaseMathCursor(bv);
728 bv->unlockInset(this);
730 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
733 return result; // original version
737 void InsetFormulaBase::revealCodes(BufferView * bv) const
741 bv->owner()->message(mathcursor->info());
744 // write something to the minibuffer
745 // translate to latex
746 mathcursor->markInsert();
749 string str = os.str();
750 mathcursor->markErase();
751 string::size_type pos = 0;
753 for (string::iterator it = str.begin(); it != str.end(); ++it) {
756 else if (*it == '\0') {
758 pos = it - str.begin();
764 res = res.substr(pos - 30);
766 res = res.substr(0, 60);
767 bv->owner()->message(res);
772 InsetOld::Code InsetFormulaBase::lyxCode() const
774 return InsetOld::MATH_CODE;
778 int InsetFormulaBase::ylow() const
780 return yo_ - dim_.asc;
784 int InsetFormulaBase::yhigh() const
786 return yo_ + dim_.des;
790 int InsetFormulaBase::xlow() const
796 int InsetFormulaBase::xhigh() const
798 return xo_ + dim_.wid;
802 /////////////////////////////////////////////////////////////////////
805 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
811 static InsetFormulaBase * lastformula = 0;
812 static MathIterator current = MathIterator(ibegin(par().nucleus()));
814 static string laststr;
816 if (lastformula != this || laststr != str) {
817 //lyxerr << "reset lastformula to " << this << endl;
820 current = ibegin(par().nucleus());
822 mathed_parse_cell(ar, str);
826 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
828 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
829 if (it.cell().matchpart(ar, it.back().pos_)) {
830 bv->unlockInset(bv->theLockingInset());
831 if (!bv->lockInset(this)) {
832 lyxerr << "Cannot lock inset" << endl;
836 mathcursor = new MathCursor(this, true);
838 mathcursor->setSelection(it, ar.size());
841 bv->updateInset(this);
846 //lyxerr << "not found!" << endl;
852 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
855 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
856 return searchForward(bv, what, a, b);
860 bool InsetFormulaBase::display() const
862 return par()->asHullInset() && par()->asHullInset()->display();
866 string InsetFormulaBase::selectionAsString() const
868 return mathcursor ? mathcursor->grabSelection() : string();
871 /////////////////////////////////////////////////////////////////////
874 void mathDispatchCreation(FuncRequest const & cmd, bool display)
876 BufferView * bv = cmd.view();
877 // use selection if available..
879 //if (action == LFUN_MATH_IMPORT_SELECTION)
883 string sel = bv->getLyXText()->selectionAsString(*bv->buffer(), false);
886 InsetFormula * f = new InsetFormula(bv);
887 if (openNewInset(bv, f)) {
888 bv->theLockingInset()->
889 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
890 // don't do that also for LFUN_MATH_MODE unless you want end up with
891 // always changing to mathrm when opening an inlined inset
892 // -- I really hate "LyXfunc overloading"...
894 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
895 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
898 // create a macro if we see "\\newcommand" somewhere, and an ordinary
900 InsetFormulaBase * f;
901 if (sel.find("\\newcommand") == string::npos &&
902 sel.find("\\def") == string::npos)
903 f = new InsetFormula(sel);
905 f = new InsetFormulaMacro(sel);
906 bv->getLyXText()->cutSelection(true, false);
909 cmd.message(N_("Math editor mode"));
913 void mathDispatch(FuncRequest const & cmd)
915 BufferView * bv = cmd.view();
916 if (!bv->available())
919 switch (cmd.action) {
921 case LFUN_MATH_DISPLAY:
922 mathDispatchCreation(cmd, true);
926 mathDispatchCreation(cmd, false);
929 case LFUN_MATH_IMPORT_SELECTION:
930 mathDispatchCreation(cmd, false);
933 case LFUN_MATH_MACRO:
934 if (cmd.argument.empty())
935 cmd.errorMessage(N_("Missing argument"));
937 string s = cmd.argument;
938 string const s1 = token(s, ' ', 1);
939 int const nargs = s1.empty() ? 0 : atoi(s1);
940 string const s2 = token(s, ' ', 2);
941 string const type = s2.empty() ? "newcommand" : s2;
942 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
946 case LFUN_INSERT_MATH:
947 case LFUN_INSERT_MATRIX:
948 case LFUN_MATH_DELIM: {
949 InsetFormula * f = new InsetFormula(bv);
950 if (openNewInset(bv, f)) {
951 bv->theLockingInset()->
952 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
953 bv->theLockingInset()->localDispatch(cmd);