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;
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 bv->updateInset(this);
152 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
154 mathcursor->getPos(x, y);
158 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
160 // calling metrics here destroys the cached xo,yo positions e.g. in
161 // MathParboxinset. And it would be too expensive anyway...
164 lyxerr << "getCursorPos - should not happen";
168 mathcursor->getPos(x, y);
169 x = mathcursor->targetX();
172 //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
176 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
183 //math_font_max_dim(font_, asc, des);
184 getCursorPos(bv, x, y);
186 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
187 bv->fitLockedInsetCursor(x, y, asc, des);
191 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
194 bv->updateInset(this);
198 dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
203 BufferView * bv = cmd.view();
204 bv->updateInset(this);
205 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
207 if (cmd.button() == mouse_button::button3) {
208 // try to dispatch to enclosed insets first
209 if (mathcursor->dispatch(cmd) == UNDISPATCHED) {
210 // launch math panel for right mouse button
211 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
212 bv->owner()->getDialogs().show("mathpanel");
217 if (cmd.button() == mouse_button::button2) {
219 asArray(bv->getClipboard(), ar);
220 mathcursor->selClear();
221 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
222 mathcursor->insert(ar);
223 bv->updateInset(this);
227 if (cmd.button() == mouse_button::button1) {
228 // try to dispatch to enclosed insets first
229 mathcursor->dispatch(cmd);
230 cmd.view()->stuffClipboard(mathcursor->grabSelection());
231 // try to set the cursor
233 //mathcursor = new MathCursor(this, x == 0);
235 //mathcursor->setPos(x + xo_, y + yo_);
243 dispatch_result InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
245 BufferView * bv = cmd.view();
246 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
248 if (!mathcursor || mathcursor->formula() != this) {
249 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
250 releaseMathCursor(bv);
251 mathcursor = new MathCursor(this, cmd.x == 0);
253 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
256 if (cmd.button() == mouse_button::button3) {
257 mathcursor->dispatch(cmd);
261 if (cmd.button() == mouse_button::button1) {
264 mathcursor->selClear();
265 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
266 mathcursor->dispatch(cmd);
270 bv->updateInset(this);
275 dispatch_result InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
280 if (mathcursor->dispatch(FuncRequest(cmd)) != UNDISPATCHED)
283 // only select with button 1
284 if (cmd.button() != mouse_button::button1)
287 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
293 if (!mathcursor->selection())
294 mathcursor->selStart();
296 BufferView * bv = cmd.view();
297 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
298 bv->updateInset(this);
303 dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
305 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
306 // << " arg: '" << cmd.argument
307 // << " x: '" << cmd.x
308 // << " y: '" << cmd.y
309 // << "' button: " << cmd.button() << endl;
311 BufferView * bv = cmd.view();
313 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
314 bool remove_inset = false;
316 switch (cmd.action) {
317 case LFUN_INSET_EDIT:
318 lyxerr << "Called EDIT with '" << cmd.argument << "'" << endl;
319 if (!bv->lockInset(this))
320 lyxerr << "Cannot lock math inset in edit call!" << endl;
321 releaseMathCursor(bv);
322 if (!cmd.argument.empty()) {
323 mathcursor = new MathCursor(this, cmd.argument == "left");
326 mathcursor = new MathCursor(this, true);
328 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
330 // if that is removed, we won't get the magenta box when entering an
331 // inset for the first time
332 bv->updateInset(this);
335 case LFUN_MOUSE_PRESS:
336 //lyxerr << "Mouse single press" << endl;
337 return lfunMousePress(cmd);
338 case LFUN_MOUSE_MOTION:
339 //lyxerr << "Mouse motion" << endl;
340 return lfunMouseMotion(cmd);
341 case LFUN_MOUSE_RELEASE:
342 //lyxerr << "Mouse single release" << endl;
343 return lfunMouseRelease(cmd);
344 case LFUN_MOUSE_DOUBLE:
345 //lyxerr << "Mouse double" << endl;
346 return localDispatch(FuncRequest(LFUN_WORDSEL));
354 string argument = cmd.argument;
355 RESULT result = DISPATCHED;
357 bool was_macro = mathcursor->inMacroMode();
358 bool was_selection = mathcursor->selection();
360 mathcursor->normalize();
363 switch (cmd.action) {
365 case LFUN_MATH_MUTATE:
366 case LFUN_MATH_DISPLAY:
367 case LFUN_MATH_NUMBER:
368 case LFUN_MATH_NONUMBER:
369 case LFUN_CELL_SPLIT:
371 case LFUN_DELETE_LINE_FORWARD:
372 case LFUN_INSERT_LABEL:
373 case LFUN_MATH_EXTERN:
374 case LFUN_TABULAR_FEATURE:
375 case LFUN_PASTESELECTION:
376 case LFUN_MATH_LIMITS:
377 recordUndo(bv, Undo::ATOMIC);
378 mathcursor->dispatch(cmd);
382 sel = true; // fall through...
384 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
385 //lyxerr << "calling scroll 20" << endl;
387 // write something to the minibuffer
388 //bv->owner()->message(mathcursor->info());
392 sel = true; // fall through
394 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
398 sel = true; // fall through
400 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
404 sel = true; // fall through
406 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
410 mathcursor->home(false);
411 mathcursor->end(true);
414 case LFUN_UP_PARAGRAPHSEL:
415 case LFUN_UP_PARAGRAPH:
416 case LFUN_DOWN_PARAGRAPHSEL:
417 case LFUN_DOWN_PARAGRAPH:
422 case LFUN_WORDLEFTSEL:
423 sel = true; // fall through
426 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
430 case LFUN_WORDRIGHTSEL:
431 sel = true; // fall through
434 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
439 case LFUN_BEGINNINGBUFSEL:
440 case LFUN_BEGINNINGBUF:
448 result = FINISHED_RIGHT;
451 case LFUN_CELL_FORWARD:
452 mathcursor->idxNext();
455 case LFUN_CELL_BACKWARD:
456 mathcursor->idxPrev();
459 case LFUN_DELETE_WORD_BACKWARD:
461 recordUndo(bv, Undo::ATOMIC);
462 if (!mathcursor->backspace()) {
468 case LFUN_DELETE_WORD_FORWARD:
470 recordUndo(bv, Undo::ATOMIC);
471 if (!mathcursor->erase()) {
478 // sprintf(dispatch_buffer, "%d %d",);
479 // dispatch_result = dispatch_buffer;
482 lyxerr << "LFUN_SETXY broken!" << endl;
485 istringstream is(cmd.argument.c_str());
487 mathcursor->setPos(x, y);
493 istringstream is(cmd.argument.c_str());
496 mathcursor->macroModeClose();
497 recordUndo(bv, Undo::ATOMIC);
498 mathcursor->selPaste(n);
503 recordUndo(bv, Undo::DELETE);
504 mathcursor->selCut();
508 mathcursor->selCopy();
512 // Special casing for superscript in case of LyX handling
514 case LFUN_CIRCUMFLEX:
515 if (cmd.argument.empty()) {
516 // do superscript if LyX handles
518 recordUndo(bv, Undo::ATOMIC);
519 mathcursor->script(true);
536 case LFUN_HUNG_UMLAUT:
540 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
541 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
542 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
543 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
544 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
545 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
546 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
547 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
548 case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
549 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
552 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
553 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
555 handleFont(bv, cmd.argument, "textrm");
556 //bv->owner()->message(_("math text mode toggled"));
562 recordUndo(bv, Undo::ATOMIC);
563 mathcursor->setSize(arg);
568 case LFUN_INSERT_MATRIX: {
569 recordUndo(bv, Undo::ATOMIC);
574 istringstream is(STRCONV(argument));
575 is >> m >> n >> v_align >> h_align;
579 mathcursor->niceInsert(
580 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
584 case LFUN_SUPERSCRIPT:
587 recordUndo(bv, Undo::ATOMIC);
588 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
592 case LFUN_MATH_DELIM:
594 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
596 string rs = split(cmd.argument, ls, ' ');
597 // Reasonable default values
603 recordUndo(bv, Undo::ATOMIC);
604 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
608 case LFUN_SPACE_INSERT:
609 case LFUN_MATH_SPACE:
610 recordUndo(bv, Undo::ATOMIC);
611 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
615 bv->owner()->message(_("Invalid action in math mode!"));
619 case LFUN_EXEC_COMMAND:
620 result = UNDISPATCHED;
624 // interpret this as if a backslash was typed
625 recordUndo(bv, Undo::ATOMIC);
626 mathcursor->interpret('\\');
629 case LFUN_BREAKPARAGRAPH:
630 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
631 case LFUN_BREAKPARAGRAPH_SKIP:
635 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
636 // handling such that "self-insert" works on "arbitrary stuff" too, and
637 // math-insert only handles special math things like "matrix".
638 case LFUN_INSERT_MATH:
639 recordUndo(bv, Undo::ATOMIC);
640 mathcursor->niceInsert(argument);
644 case LFUN_SELFINSERT:
645 if (!argument.empty()) {
646 recordUndo(bv, Undo::ATOMIC);
647 if (argument.size() == 1)
648 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
650 mathcursor->insert(argument);
655 if (mathcursor->selection())
656 mathcursor->selClear();
658 result = UNDISPATCHED;
661 case LFUN_INSET_TOGGLE:
662 mathcursor->insetToggle();
665 case LFUN_DIALOG_SHOW:
666 result = UNDISPATCHED;
669 case LFUN_DIALOG_SHOW_NEW_INSET: {
670 string const & name = argument;
674 data = tmp.createDialogStr(name);
678 result = UNDISPATCHED;
680 bv->owner()->getDialogs().show(name, data, 0);
685 case LFUN_INSET_APPLY: {
686 string const name = cmd.getArg(0);
688 bv->owner()->getDialogs().getOpenInset(name);
691 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
692 result = base->localDispatch(fr);
695 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
696 mathcursor->insert(ar);
699 result = UNDISPATCHED;
706 result = UNDISPATCHED;
709 if (result == DISPATCHED)
710 bv->updateInset(this);
712 mathcursor->normalize();
717 if (mathcursor->selection() || was_selection)
718 toggleInsetSelection(bv);
720 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
721 result == UNDISPATCHED) {
724 cmd.view()->stuffClipboard(mathcursor->grabSelection());
726 releaseMathCursor(bv);
727 bv->unlockInset(this);
729 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
732 return result; // original version
736 void InsetFormulaBase::revealCodes(BufferView * bv) const
740 bv->owner()->message(mathcursor->info());
743 // write something to the minibuffer
744 // translate to latex
745 mathcursor->markInsert();
748 string str = os.str();
749 mathcursor->markErase();
750 string::size_type pos = 0;
752 for (string::iterator it = str.begin(); it != str.end(); ++it) {
755 else if (*it == '\0') {
757 pos = it - str.begin();
763 res = res.substr(pos - 30);
765 res = res.substr(0, 60);
766 bv->owner()->message(res);
771 InsetOld::Code InsetFormulaBase::lyxCode() const
773 return InsetOld::MATH_CODE;
777 int InsetFormulaBase::ylow() const
779 return yo_ - dim_.asc;
783 int InsetFormulaBase::yhigh() const
785 return yo_ + dim_.des;
789 int InsetFormulaBase::xlow() const
795 int InsetFormulaBase::xhigh() const
797 return xo_ + dim_.wid;
801 /////////////////////////////////////////////////////////////////////
804 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
810 static InsetFormulaBase * lastformula = 0;
811 static MathIterator current = MathIterator(ibegin(par().nucleus()));
813 static string laststr;
815 if (lastformula != this || laststr != str) {
816 //lyxerr << "reset lastformula to " << this << endl;
819 current = ibegin(par().nucleus());
821 mathed_parse_cell(ar, str);
825 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
827 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
828 if (it.cell().matchpart(ar, it.back().pos_)) {
829 bv->unlockInset(bv->theLockingInset());
830 if (!bv->lockInset(this)) {
831 lyxerr << "Cannot lock inset" << endl;
835 mathcursor = new MathCursor(this, true);
837 mathcursor->setSelection(it, ar.size());
840 bv->updateInset(this);
845 //lyxerr << "not found!" << endl;
851 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
854 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
855 return searchForward(bv, what, a, b);
859 bool InsetFormulaBase::display() const
861 return par()->asHullInset() && par()->asHullInset()->display();
865 string InsetFormulaBase::selectionAsString() const
867 return mathcursor ? mathcursor->grabSelection() : string();
870 /////////////////////////////////////////////////////////////////////
873 void mathDispatchCreation(FuncRequest const & cmd, bool display)
875 BufferView * bv = cmd.view();
876 // use selection if available..
878 //if (action == LFUN_MATH_IMPORT_SELECTION)
882 string sel = bv->getLyXText()->selectionAsString(*bv->buffer(), false);
885 InsetFormula * f = new InsetFormula(bv);
886 if (openNewInset(bv, f)) {
887 bv->theLockingInset()->
888 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
889 // don't do that also for LFUN_MATH_MODE unless you want end up with
890 // always changing to mathrm when opening an inlined inset
891 // -- I really hate "LyXfunc overloading"...
893 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
894 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
897 // create a macro if we see "\\newcommand" somewhere, and an ordinary
899 InsetFormulaBase * f;
900 if (sel.find("\\newcommand") == string::npos &&
901 sel.find("\\def") == string::npos)
902 f = new InsetFormula(sel);
904 f = new InsetFormulaMacro(sel);
905 bv->getLyXText()->cutSelection(true, false);
908 cmd.message(N_("Math editor mode"));
912 void mathDispatch(FuncRequest const & cmd)
914 BufferView * bv = cmd.view();
915 if (!bv->available())
918 switch (cmd.action) {
920 case LFUN_MATH_DISPLAY:
921 mathDispatchCreation(cmd, true);
925 mathDispatchCreation(cmd, false);
928 case LFUN_MATH_IMPORT_SELECTION:
929 mathDispatchCreation(cmd, false);
932 case LFUN_MATH_MACRO:
933 if (cmd.argument.empty())
934 cmd.errorMessage(N_("Missing argument"));
936 string s = cmd.argument;
937 string const s1 = token(s, ' ', 1);
938 int const nargs = s1.empty() ? 0 : atoi(s1);
939 string const s2 = token(s, ' ', 2);
940 string const type = s2.empty() ? "newcommand" : s2;
941 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
945 case LFUN_INSERT_MATH:
946 case LFUN_INSERT_MATRIX:
947 case LFUN_MATH_DELIM: {
948 InsetFormula * f = new InsetFormula(bv);
949 if (openNewInset(bv, f)) {
950 bv->theLockingInset()->
951 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
952 bv->theLockingInset()->localDispatch(cmd);