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 "support/LAssert.h"
17 #include "formulamacro.h"
19 #include "funcrequest.h"
20 #include "BufferView.h"
23 #include "LaTeXFeatures.h"
25 #include "math_support.h"
26 #include "metricsinfo.h"
27 #include "math_data.h"
28 #include "support/lstrings.h"
29 #include "support/lyxlib.h"
30 #include "frontends/LyXView.h"
31 #include "frontends/font_metrics.h"
32 #include "frontends/mouse_state.h"
33 #include "math_arrayinset.h"
34 #include "math_charinset.h"
35 #include "math_deliminset.h"
36 #include "math_cursor.h"
37 #include "math_factory.h"
38 #include "math_fontinset.h"
39 #include "math_hullinset.h"
40 #include "math_iterator.h"
41 #include "math_macrotable.h"
42 #include "math_parser.h"
44 #include "math_spaceinset.h"
45 #include "undo_funcs.h"
46 #include "textpainter.h"
47 #include "frontends/Dialogs.h"
49 #include "ref_inset.h"
53 using namespace lyx::support;
62 MathCursor * mathcursor = 0;
70 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
72 if (!bv->insertInset(new_inset)) {
76 new_inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
85 InsetFormulaBase::InsetFormulaBase()
86 : view_(0), xo_(0), yo_(0)
88 // This is needed as long the math parser is not re-entrant
90 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
91 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
92 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
93 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
97 // simply scrap this function if you want
98 void InsetFormulaBase::mutateToText()
101 // translate to latex
103 latex(NULL, os, false, false);
104 string str = os.str();
107 LyXText * lt = view_->getLyXText();
108 string::const_iterator cit = str.begin();
109 string::const_iterator end = str.end();
110 for (; cit != end; ++cit)
111 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
114 //view_->owner()->dispatch(LFUN_ESCAPE);
119 void InsetFormulaBase::handleFont
120 (BufferView * bv, string const & arg, string const & font)
122 // this whole function is a hack and won't work for incremental font
124 recordUndo(bv, Undo::ATOMIC);
126 if (mathcursor->inset()->name() == font)
127 mathcursor->handleFont(font);
129 mathcursor->handleNest(createMathInset(font));
130 mathcursor->insert(arg);
135 BufferView * InsetFormulaBase::view() const
141 void InsetFormulaBase::validate(LaTeXFeatures &) const
145 string const InsetFormulaBase::editMessage() const
147 return _("Math editor mode");
151 void InsetFormulaBase::insetUnlock(BufferView * bv)
154 if (mathcursor->inMacroMode()) {
155 mathcursor->macroModeClose();
156 bv->updateInset(this);
158 releaseMathCursor(bv);
161 bv->updateInset(this);
165 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
167 mathcursor->getPos(x, y);
171 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
173 // calling metrics here destroys the cached xo,yo positions e.g. in
174 // MathParboxinset. And it would be too expensive anyway...
177 lyxerr << "getCursorPos - should not happen";
181 mathcursor->getPos(x, y);
182 x = mathcursor->targetX();
185 //lyxerr << "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)
207 bv->updateInset(this);
211 dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
216 BufferView * bv = cmd.view();
217 bv->updateInset(this);
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) == UNDISPATCHED) {
223 // launch math panel for right mouse button
224 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
225 bv->owner()->getDialogs().show("mathpanel");
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);
236 bv->updateInset(this);
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_);
256 dispatch_result 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);
274 if (cmd.button() == mouse_button::button1) {
277 mathcursor->selClear();
278 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
279 mathcursor->dispatch(cmd);
283 bv->updateInset(this);
288 dispatch_result InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
293 if (mathcursor->dispatch(FuncRequest(cmd)) != UNDISPATCHED)
296 // only select with button 1
297 if (cmd.button() != mouse_button::button1)
300 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
306 if (!mathcursor->selection())
307 mathcursor->selStart();
309 BufferView * bv = cmd.view();
310 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
311 bv->updateInset(this);
316 dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
318 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
319 // << " arg: '" << cmd.argument
320 // << " x: '" << cmd.x
321 // << " y: '" << cmd.y
322 // << "' button: " << cmd.button() << endl;
324 BufferView * bv = cmd.view();
326 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
327 bool remove_inset = false;
329 switch (cmd.action) {
330 case LFUN_INSET_EDIT:
331 lyxerr << "Called EDIT with '" << cmd.argument << "'" << endl;
332 if (!bv->lockInset(this))
333 lyxerr << "Cannot lock math inset in edit call!" << endl;
334 releaseMathCursor(bv);
335 if (!cmd.argument.empty()) {
336 mathcursor = new MathCursor(this, cmd.argument == "left");
339 mathcursor = new MathCursor(this, true);
341 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
343 // if that is removed, we won't get the magenta box when entering an
344 // inset for the first time
345 bv->updateInset(this);
348 case LFUN_MOUSE_PRESS:
349 //lyxerr << "Mouse single press" << endl;
350 return lfunMousePress(cmd);
351 case LFUN_MOUSE_MOTION:
352 //lyxerr << "Mouse motion" << endl;
353 return lfunMouseMotion(cmd);
354 case LFUN_MOUSE_RELEASE:
355 //lyxerr << "Mouse single release" << endl;
356 return lfunMouseRelease(cmd);
357 case LFUN_MOUSE_DOUBLE:
358 //lyxerr << "Mouse double" << endl;
359 return localDispatch(FuncRequest(LFUN_WORDSEL));
367 string argument = cmd.argument;
368 RESULT result = DISPATCHED;
370 bool was_macro = mathcursor->inMacroMode();
371 bool was_selection = mathcursor->selection();
373 mathcursor->normalize();
376 switch (cmd.action) {
378 case LFUN_MATH_MUTATE:
379 case LFUN_MATH_DISPLAY:
380 case LFUN_MATH_NUMBER:
381 case LFUN_MATH_NONUMBER:
382 case LFUN_CELL_SPLIT:
384 case LFUN_DELETE_LINE_FORWARD:
385 case LFUN_INSERT_LABEL:
386 case LFUN_MATH_EXTERN:
387 case LFUN_TABULAR_FEATURE:
388 case LFUN_PASTESELECTION:
389 case LFUN_MATH_LIMITS:
390 recordUndo(bv, Undo::ATOMIC);
391 mathcursor->dispatch(cmd);
395 sel = true; // fall through...
397 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
398 //lyxerr << "calling scroll 20" << endl;
400 // write something to the minibuffer
401 //bv->owner()->message(mathcursor->info());
405 sel = true; // fall through
407 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
411 sel = true; // fall through
413 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
417 sel = true; // fall through
419 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
423 mathcursor->home(false);
424 mathcursor->end(true);
427 case LFUN_UP_PARAGRAPHSEL:
428 case LFUN_UP_PARAGRAPH:
429 case LFUN_DOWN_PARAGRAPHSEL:
430 case LFUN_DOWN_PARAGRAPH:
435 case LFUN_WORDLEFTSEL:
436 sel = true; // fall through
439 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
443 case LFUN_WORDRIGHTSEL:
444 sel = true; // fall through
447 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
452 case LFUN_BEGINNINGBUFSEL:
453 case LFUN_BEGINNINGBUF:
461 result = FINISHED_RIGHT;
464 case LFUN_CELL_FORWARD:
465 mathcursor->idxNext();
468 case LFUN_CELL_BACKWARD:
469 mathcursor->idxPrev();
472 case LFUN_DELETE_WORD_BACKWARD:
474 recordUndo(bv, Undo::ATOMIC);
475 if (!mathcursor->backspace()) {
481 case LFUN_DELETE_WORD_FORWARD:
483 recordUndo(bv, Undo::ATOMIC);
484 if (!mathcursor->erase()) {
491 // sprintf(dispatch_buffer, "%d %d",);
492 // dispatch_result = dispatch_buffer;
495 lyxerr << "LFUN_SETXY broken!" << endl;
498 istringstream is(cmd.argument.c_str());
500 mathcursor->setPos(x, y);
506 istringstream is(cmd.argument.c_str());
509 mathcursor->macroModeClose();
510 recordUndo(bv, Undo::ATOMIC);
511 mathcursor->selPaste(n);
516 recordUndo(bv, Undo::DELETE);
517 mathcursor->selCut();
521 mathcursor->selCopy();
525 // Special casing for superscript in case of LyX handling
527 case LFUN_CIRCUMFLEX:
528 if (cmd.argument.empty()) {
529 // do superscript if LyX handles
531 recordUndo(bv, Undo::ATOMIC);
532 mathcursor->script(true);
549 case LFUN_HUNG_UMLAUT:
553 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
554 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
555 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
556 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
557 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
558 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
559 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
560 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
561 case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
562 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
565 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
566 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
568 handleFont(bv, cmd.argument, "textrm");
569 //bv->owner()->message(_("math text mode toggled"));
575 recordUndo(bv, Undo::ATOMIC);
576 mathcursor->setSize(arg);
581 case LFUN_INSERT_MATRIX: {
582 recordUndo(bv, Undo::ATOMIC);
587 istringstream is(STRCONV(argument));
588 is >> m >> n >> v_align >> h_align;
592 mathcursor->niceInsert(
593 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
597 case LFUN_SUPERSCRIPT:
600 recordUndo(bv, Undo::ATOMIC);
601 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
605 case LFUN_MATH_DELIM:
607 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
609 string rs = split(cmd.argument, ls, ' ');
610 // Reasonable default values
616 recordUndo(bv, Undo::ATOMIC);
617 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
621 case LFUN_SPACE_INSERT:
622 case LFUN_MATH_SPACE:
623 recordUndo(bv, Undo::ATOMIC);
624 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
628 bv->owner()->message(_("Invalid action in math mode!"));
632 case LFUN_EXEC_COMMAND:
633 result = UNDISPATCHED;
637 // interpret this as if a backslash was typed
638 recordUndo(bv, Undo::ATOMIC);
639 mathcursor->interpret('\\');
642 case LFUN_BREAKPARAGRAPH:
643 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
644 case LFUN_BREAKPARAGRAPH_SKIP:
648 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
649 // handling such that "self-insert" works on "arbitrary stuff" too, and
650 // math-insert only handles special math things like "matrix".
651 case LFUN_INSERT_MATH:
652 recordUndo(bv, Undo::ATOMIC);
653 mathcursor->niceInsert(argument);
657 case LFUN_SELFINSERT:
658 if (!argument.empty()) {
659 recordUndo(bv, Undo::ATOMIC);
660 if (argument.size() == 1)
661 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
663 mathcursor->insert(argument);
668 if (mathcursor->selection())
669 mathcursor->selClear();
671 result = UNDISPATCHED;
674 case LFUN_INSET_TOGGLE:
675 mathcursor->insetToggle();
678 case LFUN_DIALOG_SHOW:
679 result = UNDISPATCHED;
682 case LFUN_DIALOG_SHOW_NEW_INSET: {
683 string const & name = argument;
687 data = tmp.createDialogStr(name);
691 result = UNDISPATCHED;
693 bv->owner()->getDialogs().show(name, data, 0);
698 case LFUN_INSET_APPLY: {
699 string const name = cmd.getArg(0);
701 bv->owner()->getDialogs().getOpenInset(name);
704 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
705 result = base->localDispatch(fr);
708 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
709 mathcursor->insert(ar);
712 result = UNDISPATCHED;
719 result = UNDISPATCHED;
722 if (result == DISPATCHED)
723 bv->updateInset(this);
725 mathcursor->normalize();
730 if (mathcursor->selection() || was_selection)
731 toggleInsetSelection(bv);
733 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
734 result == UNDISPATCHED) {
737 cmd.view()->stuffClipboard(mathcursor->grabSelection());
739 releaseMathCursor(bv);
740 bv->unlockInset(this);
742 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
745 return result; // original version
749 void InsetFormulaBase::revealCodes(BufferView * bv) const
753 bv->owner()->message(mathcursor->info());
756 // write something to the minibuffer
757 // translate to latex
758 mathcursor->markInsert();
761 string str = os.str();
762 mathcursor->markErase();
763 string::size_type pos = 0;
765 for (string::iterator it = str.begin(); it != str.end(); ++it) {
768 else if (*it == '\0') {
770 pos = it - str.begin();
776 res = res.substr(pos - 30);
778 res = res.substr(0, 60);
779 bv->owner()->message(res);
784 InsetOld::Code InsetFormulaBase::lyxCode() const
786 return InsetOld::MATH_CODE;
790 int InsetFormulaBase::ylow() const
792 return yo_ - dim_.asc;
796 int InsetFormulaBase::yhigh() const
798 return yo_ + dim_.des;
802 int InsetFormulaBase::xlow() const
808 int InsetFormulaBase::xhigh() const
810 return xo_ + dim_.wid;
814 /////////////////////////////////////////////////////////////////////
817 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
823 static InsetFormulaBase * lastformula = 0;
824 static MathIterator current = MathIterator(ibegin(par().nucleus()));
826 static string laststr;
828 if (lastformula != this || laststr != str) {
829 //lyxerr << "reset lastformula to " << this << endl;
832 current = ibegin(par().nucleus());
834 mathed_parse_cell(ar, str);
838 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
840 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
841 if (it.cell().matchpart(ar, it.back().pos_)) {
842 bv->unlockInset(bv->theLockingInset());
843 if (!bv->lockInset(this)) {
844 lyxerr << "Cannot lock inset" << endl;
848 mathcursor = new MathCursor(this, true);
850 mathcursor->setSelection(it, ar.size());
853 bv->updateInset(this);
858 //lyxerr << "not found!" << endl;
864 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
867 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
868 return searchForward(bv, what, a, b);
872 bool InsetFormulaBase::display() const
874 return par()->asHullInset() && par()->asHullInset()->display();
878 string InsetFormulaBase::selectionAsString() const
880 return mathcursor ? mathcursor->grabSelection() : string();
883 /////////////////////////////////////////////////////////////////////
886 void mathDispatchCreation(FuncRequest const & cmd, bool display)
888 BufferView * bv = cmd.view();
889 // use selection if available..
891 //if (action == LFUN_MATH_IMPORT_SELECTION)
895 string sel = bv->getLyXText()->selectionAsString(*bv->buffer(), false);
898 InsetFormula * f = new InsetFormula(bv);
899 if (openNewInset(bv, f)) {
900 bv->theLockingInset()->
901 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
902 // don't do that also for LFUN_MATH_MODE unless you want end up with
903 // always changing to mathrm when opening an inlined inset
904 // -- I really hate "LyXfunc overloading"...
906 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
907 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
910 // create a macro if we see "\\newcommand" somewhere, and an ordinary
912 InsetFormulaBase * f;
913 if (sel.find("\\newcommand") == string::npos &&
914 sel.find("\\def") == string::npos)
915 f = new InsetFormula(sel);
917 f = new InsetFormulaMacro(sel);
918 bv->getLyXText()->cutSelection(true, false);
921 cmd.message(N_("Math editor mode"));
925 void mathDispatch(FuncRequest const & cmd)
927 BufferView * bv = cmd.view();
928 if (!bv->available())
931 switch (cmd.action) {
933 case LFUN_MATH_DISPLAY:
934 mathDispatchCreation(cmd, true);
938 mathDispatchCreation(cmd, false);
941 case LFUN_MATH_IMPORT_SELECTION:
942 mathDispatchCreation(cmd, false);
945 case LFUN_MATH_MACRO:
946 if (cmd.argument.empty())
947 cmd.errorMessage(N_("Missing argument"));
949 string s = cmd.argument;
950 string const s1 = token(s, ' ', 1);
951 int const nargs = s1.empty() ? 0 : atoi(s1);
952 string const s2 = token(s, ' ', 2);
953 string const type = s2.empty() ? "newcommand" : s2;
954 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
958 case LFUN_INSERT_MATH:
959 case LFUN_INSERT_MATRIX:
960 case LFUN_MATH_DELIM: {
961 InsetFormula * f = new InsetFormula(bv);
962 if (openNewInset(bv, f)) {
963 bv->theLockingInset()->
964 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
965 bv->theLockingInset()->localDispatch(cmd);