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"
16 #include "support/LAssert.h"
18 #include "formulamacro.h"
19 #include "funcrequest.h"
20 #include "BufferView.h"
24 #include "math_support.h"
25 #include "support/lstrings.h"
26 #include "support/lyxlib.h"
27 #include "frontends/LyXView.h"
28 #include "math_arrayinset.h"
29 #include "math_deliminset.h"
30 #include "math_cursor.h"
31 #include "math_factory.h"
32 #include "math_hullinset.h"
33 #include "math_parser.h"
34 #include "math_spaceinset.h"
35 #include "undo_funcs.h"
36 #include "frontends/Dialogs.h"
37 #include "ref_inset.h"
40 using namespace lyx::support;
43 using std::istringstream;
45 using std::ostringstream;
51 MathCursor * mathcursor = 0;
59 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
61 if (!bv->insertInset(new_inset)) {
65 new_inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
74 InsetFormulaBase::InsetFormulaBase()
75 : view_(0), xo_(0), yo_(0)
77 // This is needed as long the math parser is not re-entrant
79 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
80 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
81 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
82 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
86 // simply scrap this function if you want
87 void InsetFormulaBase::mutateToText()
92 latex(NULL, os, false, false);
93 string str = os.str();
96 LyXText * lt = view_->getLyXText();
97 string::const_iterator cit = str.begin();
98 string::const_iterator end = str.end();
99 for (; cit != end; ++cit)
100 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
103 //view_->owner()->dispatch(LFUN_ESCAPE);
108 void InsetFormulaBase::handleFont
109 (BufferView * bv, string const & arg, string const & font)
111 // this whole function is a hack and won't work for incremental font
113 recordUndo(bv, Undo::ATOMIC);
115 if (mathcursor->inset()->name() == font)
116 mathcursor->handleFont(font);
118 mathcursor->handleNest(createMathInset(font));
119 mathcursor->insert(arg);
124 BufferView * InsetFormulaBase::view() const
130 void InsetFormulaBase::validate(LaTeXFeatures &) const
134 string const InsetFormulaBase::editMessage() const
136 return _("Math editor mode");
140 void InsetFormulaBase::insetUnlock(BufferView * bv)
143 if (mathcursor->inMacroMode()) {
144 mathcursor->macroModeClose();
145 bv->updateInset(this);
147 releaseMathCursor(bv);
150 bv->updateInset(this);
154 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
156 mathcursor->getPos(x, y);
160 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
162 // calling metrics here destroys the cached xo,yo positions e.g. in
163 // MathParboxinset. And it would be too expensive anyway...
166 lyxerr << "getCursorPos - should not happen";
170 mathcursor->getPos(x, y);
171 x = mathcursor->targetX();
174 //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
178 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
185 //math_font_max_dim(font_, asc, des);
186 getCursorPos(bv, x, y);
188 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
189 bv->fitLockedInsetCursor(x, y, asc, des);
193 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
196 bv->updateInset(this);
200 dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
205 BufferView * bv = cmd.view();
206 bv->updateInset(this);
207 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
209 if (cmd.button() == mouse_button::button3) {
210 // try to dispatch to enclosed insets first
211 if (mathcursor->dispatch(cmd) == UNDISPATCHED) {
212 // launch math panel for right mouse button
213 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
214 bv->owner()->getDialogs().show("mathpanel");
219 if (cmd.button() == mouse_button::button2) {
221 asArray(bv->getClipboard(), ar);
222 mathcursor->selClear();
223 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
224 mathcursor->insert(ar);
225 bv->updateInset(this);
229 if (cmd.button() == mouse_button::button1) {
230 // try to dispatch to enclosed insets first
231 mathcursor->dispatch(cmd);
232 cmd.view()->stuffClipboard(mathcursor->grabSelection());
233 // try to set the cursor
235 //mathcursor = new MathCursor(this, x == 0);
237 //mathcursor->setPos(x + xo_, y + yo_);
245 dispatch_result InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
247 BufferView * bv = cmd.view();
248 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
250 if (!mathcursor || mathcursor->formula() != this) {
251 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
252 releaseMathCursor(bv);
253 mathcursor = new MathCursor(this, cmd.x == 0);
255 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
258 if (cmd.button() == mouse_button::button3) {
259 mathcursor->dispatch(cmd);
263 if (cmd.button() == mouse_button::button1) {
266 mathcursor->selClear();
267 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
268 mathcursor->dispatch(cmd);
272 bv->updateInset(this);
277 dispatch_result InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
282 if (mathcursor->dispatch(FuncRequest(cmd)) != UNDISPATCHED)
285 // only select with button 1
286 if (cmd.button() != mouse_button::button1)
289 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
295 if (!mathcursor->selection())
296 mathcursor->selStart();
298 BufferView * bv = cmd.view();
299 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
300 bv->updateInset(this);
305 dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
307 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
308 // << " arg: '" << cmd.argument
309 // << " x: '" << cmd.x
310 // << " y: '" << cmd.y
311 // << "' button: " << cmd.button() << endl;
313 BufferView * bv = cmd.view();
315 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
316 bool remove_inset = false;
318 switch (cmd.action) {
319 case LFUN_INSET_EDIT:
320 lyxerr << "Called EDIT with '" << cmd.argument << "'" << endl;
321 if (!bv->lockInset(this))
322 lyxerr << "Cannot lock math inset in edit call!" << endl;
323 releaseMathCursor(bv);
324 if (!cmd.argument.empty()) {
325 mathcursor = new MathCursor(this, cmd.argument == "left");
328 mathcursor = new MathCursor(this, true);
330 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
332 // if that is removed, we won't get the magenta box when entering an
333 // inset for the first time
334 bv->updateInset(this);
337 case LFUN_MOUSE_PRESS:
338 //lyxerr << "Mouse single press" << endl;
339 return lfunMousePress(cmd);
340 case LFUN_MOUSE_MOTION:
341 //lyxerr << "Mouse motion" << endl;
342 return lfunMouseMotion(cmd);
343 case LFUN_MOUSE_RELEASE:
344 //lyxerr << "Mouse single release" << endl;
345 return lfunMouseRelease(cmd);
346 case LFUN_MOUSE_DOUBLE:
347 //lyxerr << "Mouse double" << endl;
348 return localDispatch(FuncRequest(LFUN_WORDSEL));
356 string argument = cmd.argument;
357 RESULT result = DISPATCHED;
359 bool was_macro = mathcursor->inMacroMode();
360 bool was_selection = mathcursor->selection();
362 mathcursor->normalize();
365 switch (cmd.action) {
367 case LFUN_MATH_MUTATE:
368 case LFUN_MATH_DISPLAY:
369 case LFUN_MATH_NUMBER:
370 case LFUN_MATH_NONUMBER:
371 case LFUN_CELL_SPLIT:
373 case LFUN_DELETE_LINE_FORWARD:
374 case LFUN_INSERT_LABEL:
375 case LFUN_MATH_EXTERN:
376 case LFUN_TABULAR_FEATURE:
377 case LFUN_PASTESELECTION:
378 case LFUN_MATH_LIMITS:
379 recordUndo(bv, Undo::ATOMIC);
380 mathcursor->dispatch(cmd);
384 sel = true; // fall through...
386 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
387 //lyxerr << "calling scroll 20" << endl;
389 // write something to the minibuffer
390 //bv->owner()->message(mathcursor->info());
394 sel = true; // fall through
396 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
400 sel = true; // fall through
402 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
406 sel = true; // fall through
408 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
412 mathcursor->home(false);
413 mathcursor->end(true);
416 case LFUN_UP_PARAGRAPHSEL:
417 case LFUN_UP_PARAGRAPH:
418 case LFUN_DOWN_PARAGRAPHSEL:
419 case LFUN_DOWN_PARAGRAPH:
424 case LFUN_WORDLEFTSEL:
425 sel = true; // fall through
428 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
432 case LFUN_WORDRIGHTSEL:
433 sel = true; // fall through
436 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
441 case LFUN_BEGINNINGBUFSEL:
442 case LFUN_BEGINNINGBUF:
450 result = FINISHED_RIGHT;
453 case LFUN_CELL_FORWARD:
454 mathcursor->idxNext();
457 case LFUN_CELL_BACKWARD:
458 mathcursor->idxPrev();
461 case LFUN_DELETE_WORD_BACKWARD:
463 recordUndo(bv, Undo::ATOMIC);
464 if (!mathcursor->backspace()) {
470 case LFUN_DELETE_WORD_FORWARD:
472 recordUndo(bv, Undo::ATOMIC);
473 if (!mathcursor->erase()) {
480 // sprintf(dispatch_buffer, "%d %d",);
481 // dispatch_result = dispatch_buffer;
484 lyxerr << "LFUN_SETXY broken!" << endl;
487 istringstream is(cmd.argument.c_str());
489 mathcursor->setPos(x, y);
495 istringstream is(cmd.argument.c_str());
498 mathcursor->macroModeClose();
499 recordUndo(bv, Undo::ATOMIC);
500 mathcursor->selPaste(n);
505 recordUndo(bv, Undo::DELETE);
506 mathcursor->selCut();
510 mathcursor->selCopy();
514 // Special casing for superscript in case of LyX handling
516 case LFUN_CIRCUMFLEX:
517 if (cmd.argument.empty()) {
518 // do superscript if LyX handles
520 recordUndo(bv, Undo::ATOMIC);
521 mathcursor->script(true);
538 case LFUN_HUNG_UMLAUT:
542 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
543 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
544 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
545 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
546 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
547 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
548 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
549 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
550 case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
551 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
554 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
555 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
557 handleFont(bv, cmd.argument, "textrm");
558 //bv->owner()->message(_("math text mode toggled"));
564 recordUndo(bv, Undo::ATOMIC);
565 mathcursor->setSize(arg);
570 case LFUN_INSERT_MATRIX: {
571 recordUndo(bv, Undo::ATOMIC);
576 istringstream is(STRCONV(argument));
577 is >> m >> n >> v_align >> h_align;
581 mathcursor->niceInsert(
582 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
586 case LFUN_SUPERSCRIPT:
589 recordUndo(bv, Undo::ATOMIC);
590 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
594 case LFUN_MATH_DELIM:
596 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
598 string rs = split(cmd.argument, ls, ' ');
599 // Reasonable default values
605 recordUndo(bv, Undo::ATOMIC);
606 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
610 case LFUN_SPACE_INSERT:
611 case LFUN_MATH_SPACE:
612 recordUndo(bv, Undo::ATOMIC);
613 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
617 bv->owner()->message(_("Invalid action in math mode!"));
621 case LFUN_EXEC_COMMAND:
622 result = UNDISPATCHED;
626 // interpret this as if a backslash was typed
627 recordUndo(bv, Undo::ATOMIC);
628 mathcursor->interpret('\\');
631 case LFUN_BREAKPARAGRAPH:
632 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
633 case LFUN_BREAKPARAGRAPH_SKIP:
637 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
638 // handling such that "self-insert" works on "arbitrary stuff" too, and
639 // math-insert only handles special math things like "matrix".
640 case LFUN_INSERT_MATH:
641 recordUndo(bv, Undo::ATOMIC);
642 mathcursor->niceInsert(argument);
646 case LFUN_SELFINSERT:
647 if (!argument.empty()) {
648 recordUndo(bv, Undo::ATOMIC);
649 if (argument.size() == 1)
650 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
652 mathcursor->insert(argument);
657 if (mathcursor->selection())
658 mathcursor->selClear();
660 result = UNDISPATCHED;
663 case LFUN_INSET_TOGGLE:
664 mathcursor->insetToggle();
667 case LFUN_DIALOG_SHOW:
668 result = UNDISPATCHED;
671 case LFUN_DIALOG_SHOW_NEW_INSET: {
672 string const & name = argument;
676 data = tmp.createDialogStr(name);
680 result = UNDISPATCHED;
682 bv->owner()->getDialogs().show(name, data, 0);
687 case LFUN_INSET_APPLY: {
688 string const name = cmd.getArg(0);
690 bv->owner()->getDialogs().getOpenInset(name);
693 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
694 result = base->localDispatch(fr);
697 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
698 mathcursor->insert(ar);
701 result = UNDISPATCHED;
708 result = UNDISPATCHED;
711 if (result == DISPATCHED)
712 bv->updateInset(this);
714 mathcursor->normalize();
719 if (mathcursor->selection() || was_selection)
720 toggleInsetSelection(bv);
722 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
723 result == UNDISPATCHED) {
726 cmd.view()->stuffClipboard(mathcursor->grabSelection());
728 releaseMathCursor(bv);
729 bv->unlockInset(this);
731 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
734 return result; // original version
738 void InsetFormulaBase::revealCodes(BufferView * bv) const
742 bv->owner()->message(mathcursor->info());
745 // write something to the minibuffer
746 // translate to latex
747 mathcursor->markInsert();
750 string str = os.str();
751 mathcursor->markErase();
752 string::size_type pos = 0;
754 for (string::iterator it = str.begin(); it != str.end(); ++it) {
757 else if (*it == '\0') {
759 pos = it - str.begin();
765 res = res.substr(pos - 30);
767 res = res.substr(0, 60);
768 bv->owner()->message(res);
773 InsetOld::Code InsetFormulaBase::lyxCode() const
775 return InsetOld::MATH_CODE;
779 int InsetFormulaBase::ylow() const
781 return yo_ - dim_.asc;
785 int InsetFormulaBase::yhigh() const
787 return yo_ + dim_.des;
791 int InsetFormulaBase::xlow() const
797 int InsetFormulaBase::xhigh() const
799 return xo_ + dim_.wid;
803 /////////////////////////////////////////////////////////////////////
806 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
812 static InsetFormulaBase * lastformula = 0;
813 static MathIterator current = MathIterator(ibegin(par().nucleus()));
815 static string laststr;
817 if (lastformula != this || laststr != str) {
818 //lyxerr << "reset lastformula to " << this << endl;
821 current = ibegin(par().nucleus());
823 mathed_parse_cell(ar, str);
827 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
829 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
830 if (it.cell().matchpart(ar, it.back().pos_)) {
831 bv->unlockInset(bv->theLockingInset());
832 if (!bv->lockInset(this)) {
833 lyxerr << "Cannot lock inset" << endl;
837 mathcursor = new MathCursor(this, true);
839 mathcursor->setSelection(it, ar.size());
842 bv->updateInset(this);
847 //lyxerr << "not found!" << endl;
853 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
856 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
857 return searchForward(bv, what, a, b);
861 bool InsetFormulaBase::display() const
863 return par()->asHullInset() && par()->asHullInset()->display();
867 string InsetFormulaBase::selectionAsString() const
869 return mathcursor ? mathcursor->grabSelection() : string();
872 /////////////////////////////////////////////////////////////////////
875 void mathDispatchCreation(FuncRequest const & cmd, bool display)
877 BufferView * bv = cmd.view();
878 // use selection if available..
880 //if (action == LFUN_MATH_IMPORT_SELECTION)
884 string sel = bv->getLyXText()->selectionAsString(*bv->buffer(), false);
887 InsetFormula * f = new InsetFormula(bv);
888 if (openNewInset(bv, f)) {
889 bv->theLockingInset()->
890 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
891 // don't do that also for LFUN_MATH_MODE unless you want end up with
892 // always changing to mathrm when opening an inlined inset
893 // -- I really hate "LyXfunc overloading"...
895 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
896 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
899 // create a macro if we see "\\newcommand" somewhere, and an ordinary
901 InsetFormulaBase * f;
902 if (sel.find("\\newcommand") == string::npos &&
903 sel.find("\\def") == string::npos)
904 f = new InsetFormula(sel);
906 f = new InsetFormulaMacro(sel);
907 bv->getLyXText()->cutSelection(true, false);
910 cmd.message(N_("Math editor mode"));
914 void mathDispatch(FuncRequest const & cmd)
916 BufferView * bv = cmd.view();
917 if (!bv->available())
920 switch (cmd.action) {
922 case LFUN_MATH_DISPLAY:
923 mathDispatchCreation(cmd, true);
927 mathDispatchCreation(cmd, false);
930 case LFUN_MATH_IMPORT_SELECTION:
931 mathDispatchCreation(cmd, false);
934 case LFUN_MATH_MACRO:
935 if (cmd.argument.empty())
936 cmd.errorMessage(N_("Missing argument"));
938 string s = cmd.argument;
939 string const s1 = token(s, ' ', 1);
940 int const nargs = s1.empty() ? 0 : atoi(s1);
941 string const s2 = token(s, ' ', 2);
942 string const type = s2.empty() ? "newcommand" : s2;
943 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
947 case LFUN_INSERT_MATH:
948 case LFUN_INSERT_MATRIX:
949 case LFUN_MATH_DELIM: {
950 InsetFormula * f = new InsetFormula(bv);
951 if (openNewInset(bv, f)) {
952 bv->theLockingInset()->
953 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
954 bv->theLockingInset()->localDispatch(cmd);