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 "funcrequest.h"
18 #include "BufferView.h"
19 #include "bufferview_funcs.h"
23 #include "math_support.h"
24 #include "math_arrayinset.h"
25 #include "math_deliminset.h"
26 #include "math_cursor.h"
27 #include "math_factory.h"
28 #include "math_hullinset.h"
29 #include "math_parser.h"
30 #include "math_spaceinset.h"
31 #include "undo_funcs.h"
32 #include "ref_inset.h"
35 #include "support/std_sstream.h"
36 #include "support/lstrings.h"
37 #include "support/lyxlib.h"
39 #include "frontends/LyXView.h"
40 #include "frontends/Dialogs.h"
42 using lyx::support::atoi;
43 using lyx::support::split;
44 using lyx::support::token;
50 using std::istringstream;
53 MathCursor * mathcursor = 0;
61 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
63 if (!bv->insertInset(new_inset)) {
67 new_inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
76 InsetFormulaBase::InsetFormulaBase()
77 : view_(0), xo_(0), yo_(0)
79 // This is needed as long the math parser is not re-entrant
81 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
82 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
83 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
84 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
88 // simply scrap this function if you want
89 void InsetFormulaBase::mutateToText()
94 latex(NULL, os, false, false);
95 string str = os.str();
98 LyXText * lt = view_->getLyXText();
99 string::const_iterator cit = str.begin();
100 string::const_iterator end = str.end();
101 for (; cit != end; ++cit)
102 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
105 //view_->owner()->dispatch(LFUN_ESCAPE);
110 void InsetFormulaBase::handleFont
111 (BufferView * bv, string const & arg, string const & font)
113 // this whole function is a hack and won't work for incremental font
115 recordUndo(bv, Undo::ATOMIC);
117 if (mathcursor->inset()->name() == font)
118 mathcursor->handleFont(font);
120 mathcursor->handleNest(createMathInset(font));
121 mathcursor->insert(arg);
126 void InsetFormulaBase::handleFont2(BufferView * bv, string const & arg)
128 recordUndo(bv, Undo::ATOMIC);
131 bv_funcs::string2font(arg, font, b);
132 if (font.color() != LColor::inherit) {
133 MathAtom at = createMathInset("color");
134 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
135 mathcursor->handleNest(at, 1);
141 BufferView * InsetFormulaBase::view() const
147 void InsetFormulaBase::validate(LaTeXFeatures &) const
151 string const InsetFormulaBase::editMessage() const
153 return _("Math editor mode");
157 void InsetFormulaBase::insetUnlock(BufferView * bv)
160 if (mathcursor->inMacroMode()) {
161 mathcursor->macroModeClose();
162 bv->updateInset(this);
164 releaseMathCursor(bv);
167 generatePreview(*bv->buffer());
168 bv->updateInset(this);
172 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
174 mathcursor->getPos(x, y);
178 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
180 // calling metrics here destroys the cached xo,yo positions e.g. in
181 // MathParboxinset. And it would be too expensive anyway...
184 lyxerr << "getCursorPos - should not happen";
188 mathcursor->getPos(x, y);
189 x = mathcursor->targetX();
192 //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
196 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
203 //math_font_max_dim(font_, asc, des);
204 getCursorPos(bv, x, y);
206 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
207 bv->fitLockedInsetCursor(x, y, asc, des);
211 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
214 bv->updateInset(this);
218 dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
223 BufferView * bv = cmd.view();
224 bv->updateInset(this);
225 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
227 if (cmd.button() == mouse_button::button3) {
228 // try to dispatch to enclosed insets first
229 if (mathcursor->dispatch(cmd) == UNDISPATCHED) {
230 // launch math panel for right mouse button
231 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
232 bv->owner()->getDialogs().show("mathpanel");
237 if (cmd.button() == mouse_button::button2) {
239 asArray(bv->getClipboard(), ar);
240 mathcursor->selClear();
241 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
242 mathcursor->insert(ar);
243 bv->updateInset(this);
247 if (cmd.button() == mouse_button::button1) {
248 // try to dispatch to enclosed insets first
249 mathcursor->dispatch(cmd);
250 cmd.view()->stuffClipboard(mathcursor->grabSelection());
251 // try to set the cursor
253 //mathcursor = new MathCursor(this, x == 0);
255 //mathcursor->setPos(x + xo_, y + yo_);
263 dispatch_result InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
265 BufferView * bv = cmd.view();
266 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
268 if (!mathcursor || mathcursor->formula() != this) {
269 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
270 releaseMathCursor(bv);
271 mathcursor = new MathCursor(this, cmd.x == 0);
273 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
276 if (cmd.button() == mouse_button::button3) {
277 mathcursor->dispatch(cmd);
281 if (cmd.button() == mouse_button::button1) {
284 mathcursor->selClear();
285 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
286 mathcursor->dispatch(cmd);
290 bv->updateInset(this);
295 dispatch_result InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
300 if (mathcursor->dispatch(FuncRequest(cmd)) != UNDISPATCHED)
303 // only select with button 1
304 if (cmd.button() != mouse_button::button1)
307 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
313 if (!mathcursor->selection())
314 mathcursor->selStart();
316 BufferView * bv = cmd.view();
317 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
318 bv->updateInset(this);
323 dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
325 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
326 // << " arg: '" << cmd.argument
327 // << "' x: '" << cmd.x
328 // << " y: '" << cmd.y
329 // << "' button: " << cmd.button() << endl;
331 BufferView * bv = cmd.view();
333 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
334 bool remove_inset = false;
336 switch (cmd.action) {
337 case LFUN_INSET_EDIT:
338 lyxerr << "Called EDIT with '" << cmd.argument << "'" << endl;
339 if (!bv->lockInset(this))
340 lyxerr << "Cannot lock math inset in edit call!" << endl;
341 releaseMathCursor(bv);
342 if (!cmd.argument.empty()) {
343 mathcursor = new MathCursor(this, cmd.argument == "left");
346 mathcursor = new MathCursor(this, true);
348 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
350 // if that is removed, we won't get the magenta box when entering an
351 // inset for the first time
352 bv->updateInset(this);
355 case LFUN_MOUSE_PRESS:
356 //lyxerr << "Mouse single press" << endl;
357 return lfunMousePress(cmd);
358 case LFUN_MOUSE_MOTION:
359 //lyxerr << "Mouse motion" << endl;
360 return lfunMouseMotion(cmd);
361 case LFUN_MOUSE_RELEASE:
362 //lyxerr << "Mouse single release" << endl;
363 return lfunMouseRelease(cmd);
364 case LFUN_MOUSE_DOUBLE:
365 //lyxerr << "Mouse double" << endl;
366 return localDispatch(FuncRequest(LFUN_WORDSEL));
374 string argument = cmd.argument;
375 RESULT result = DISPATCHED;
377 bool was_macro = mathcursor->inMacroMode();
378 bool was_selection = mathcursor->selection();
380 mathcursor->normalize();
383 switch (cmd.action) {
385 case LFUN_MATH_MUTATE:
386 case LFUN_MATH_DISPLAY:
387 case LFUN_MATH_NUMBER:
388 case LFUN_MATH_NONUMBER:
389 case LFUN_CELL_SPLIT:
391 case LFUN_DELETE_LINE_FORWARD:
392 case LFUN_INSERT_LABEL:
393 case LFUN_MATH_EXTERN:
394 case LFUN_TABULAR_FEATURE:
395 case LFUN_PASTESELECTION:
396 case LFUN_MATH_LIMITS:
397 recordUndo(bv, Undo::ATOMIC);
398 mathcursor->dispatch(cmd);
402 sel = true; // fall through...
404 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
405 //lyxerr << "calling scroll 20" << endl;
407 // write something to the minibuffer
408 //bv->owner()->message(mathcursor->info());
412 sel = true; // fall through
414 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
418 sel = true; // fall through
420 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
424 sel = true; // fall through
426 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
430 mathcursor->home(false);
431 mathcursor->end(true);
434 case LFUN_UP_PARAGRAPHSEL:
435 case LFUN_UP_PARAGRAPH:
436 case LFUN_DOWN_PARAGRAPHSEL:
437 case LFUN_DOWN_PARAGRAPH:
442 case LFUN_WORDLEFTSEL:
443 sel = true; // fall through
446 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
450 case LFUN_WORDRIGHTSEL:
451 sel = true; // fall through
454 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
459 case LFUN_BEGINNINGBUFSEL:
460 case LFUN_BEGINNINGBUF:
468 result = FINISHED_RIGHT;
471 case LFUN_CELL_FORWARD:
472 mathcursor->idxNext();
475 case LFUN_CELL_BACKWARD:
476 mathcursor->idxPrev();
479 case LFUN_DELETE_WORD_BACKWARD:
481 recordUndo(bv, Undo::ATOMIC);
482 if (!mathcursor->backspace()) {
488 case LFUN_DELETE_WORD_FORWARD:
490 recordUndo(bv, Undo::ATOMIC);
491 if (!mathcursor->erase()) {
498 // sprintf(dispatch_buffer, "%d %d",);
499 // dispatch_result = dispatch_buffer;
502 lyxerr << "LFUN_SETXY broken!" << endl;
505 istringstream is(cmd.argument.c_str());
507 mathcursor->setPos(x, y);
513 istringstream is(cmd.argument.c_str());
516 mathcursor->macroModeClose();
517 recordUndo(bv, Undo::ATOMIC);
518 mathcursor->selPaste(n);
523 recordUndo(bv, Undo::DELETE);
524 mathcursor->selCut();
528 mathcursor->selCopy();
532 // Special casing for superscript in case of LyX handling
534 case LFUN_CIRCUMFLEX:
535 if (cmd.argument.empty()) {
536 // do superscript if LyX handles
538 recordUndo(bv, Undo::ATOMIC);
539 mathcursor->script(true);
556 case LFUN_HUNG_UMLAUT:
560 case LFUN_FREEFONT_APPLY:
561 case LFUN_FREEFONT_UPDATE:
562 handleFont2(bv, cmd.argument);
565 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
566 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
567 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
568 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
569 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
570 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
571 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
572 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
573 //case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
574 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
577 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
578 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
580 handleFont(bv, cmd.argument, "textrm");
581 //bv->owner()->message(_("math text mode toggled"));
587 recordUndo(bv, Undo::ATOMIC);
588 mathcursor->setSize(arg);
593 case LFUN_INSERT_MATRIX: {
594 recordUndo(bv, Undo::ATOMIC);
599 istringstream is(argument);
600 is >> m >> n >> v_align >> h_align;
604 mathcursor->niceInsert(
605 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
609 case LFUN_SUPERSCRIPT:
612 recordUndo(bv, Undo::ATOMIC);
613 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
617 case LFUN_MATH_DELIM:
619 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
621 string rs = split(cmd.argument, ls, ' ');
622 // Reasonable default values
628 recordUndo(bv, Undo::ATOMIC);
629 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
633 case LFUN_SPACE_INSERT:
634 case LFUN_MATH_SPACE:
635 recordUndo(bv, Undo::ATOMIC);
636 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
640 bv->owner()->message(_("Invalid action in math mode!"));
644 case LFUN_EXEC_COMMAND:
645 result = UNDISPATCHED;
649 // interpret this as if a backslash was typed
650 recordUndo(bv, Undo::ATOMIC);
651 mathcursor->interpret('\\');
654 case LFUN_BREAKPARAGRAPH:
655 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
656 case LFUN_BREAKPARAGRAPH_SKIP:
660 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
661 // handling such that "self-insert" works on "arbitrary stuff" too, and
662 // math-insert only handles special math things like "matrix".
663 case LFUN_INSERT_MATH:
664 recordUndo(bv, Undo::ATOMIC);
665 mathcursor->niceInsert(argument);
669 case LFUN_SELFINSERT:
670 if (!argument.empty()) {
671 recordUndo(bv, Undo::ATOMIC);
672 if (argument.size() == 1)
673 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
675 mathcursor->insert(argument);
680 if (mathcursor->selection())
681 mathcursor->selClear();
683 result = UNDISPATCHED;
686 case LFUN_INSET_TOGGLE:
687 mathcursor->insetToggle();
690 case LFUN_DIALOG_SHOW:
691 result = UNDISPATCHED;
694 case LFUN_DIALOG_SHOW_NEW_INSET: {
695 string const & name = argument;
699 data = tmp.createDialogStr(name);
703 result = UNDISPATCHED;
705 bv->owner()->getDialogs().show(name, data, 0);
710 case LFUN_INSET_APPLY: {
711 string const name = cmd.getArg(0);
713 bv->owner()->getDialogs().getOpenInset(name);
716 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
717 result = base->localDispatch(fr);
720 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
721 mathcursor->insert(ar);
724 result = UNDISPATCHED;
731 result = UNDISPATCHED;
734 if (result == DISPATCHED)
735 bv->updateInset(this);
737 mathcursor->normalize();
740 BOOST_ASSERT(mathcursor);
742 if (mathcursor->selection() || was_selection)
743 toggleInsetSelection(bv);
745 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
746 result == UNDISPATCHED) {
749 cmd.view()->stuffClipboard(mathcursor->grabSelection());
751 releaseMathCursor(bv);
752 bv->unlockInset(this);
754 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
757 return result; // original version
761 void InsetFormulaBase::revealCodes(BufferView * bv) const
765 bv->owner()->message(mathcursor->info());
768 // write something to the minibuffer
769 // translate to latex
770 mathcursor->markInsert();
773 string str = os.str();
774 mathcursor->markErase();
775 string::size_type pos = 0;
777 for (string::iterator it = str.begin(); it != str.end(); ++it) {
780 else if (*it == '\0') {
782 pos = it - str.begin();
788 res = res.substr(pos - 30);
790 res = res.substr(0, 60);
791 bv->owner()->message(res);
796 InsetOld::Code InsetFormulaBase::lyxCode() const
798 return InsetOld::MATH_CODE;
802 int InsetFormulaBase::ylow() const
804 return yo_ - dim_.asc;
808 int InsetFormulaBase::yhigh() const
810 return yo_ + dim_.des;
814 int InsetFormulaBase::xlow() const
820 int InsetFormulaBase::xhigh() const
822 return xo_ + dim_.wid;
826 /////////////////////////////////////////////////////////////////////
829 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
835 static InsetFormulaBase * lastformula = 0;
836 static MathIterator current = MathIterator(ibegin(par().nucleus()));
838 static string laststr;
840 if (lastformula != this || laststr != str) {
841 //lyxerr << "reset lastformula to " << this << endl;
844 current = ibegin(par().nucleus());
846 mathed_parse_cell(ar, str);
850 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
852 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
853 if (it.cell().matchpart(ar, it.back().pos_)) {
854 bv->unlockInset(bv->theLockingInset());
855 if (!bv->lockInset(this)) {
856 lyxerr << "Cannot lock inset" << endl;
860 mathcursor = new MathCursor(this, true);
862 mathcursor->setSelection(it, ar.size());
865 bv->updateInset(this);
870 //lyxerr << "not found!" << endl;
876 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
879 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
880 return searchForward(bv, what, a, b);
884 bool InsetFormulaBase::display() const
886 return par()->asHullInset() && par()->asHullInset()->display();
890 string InsetFormulaBase::selectionAsString() const
892 return mathcursor ? mathcursor->grabSelection() : string();
895 /////////////////////////////////////////////////////////////////////
898 void mathDispatchCreation(FuncRequest const & cmd, bool display)
900 BufferView * bv = cmd.view();
901 // use selection if available..
903 //if (action == LFUN_MATH_IMPORT_SELECTION)
907 string sel = bv->getLyXText()->selectionAsString(*bv->buffer(), false);
910 InsetFormula * f = new InsetFormula(bv);
911 if (openNewInset(bv, f)) {
912 bv->theLockingInset()->
913 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
914 // don't do that also for LFUN_MATH_MODE unless you want end up with
915 // always changing to mathrm when opening an inlined inset
916 // -- I really hate "LyXfunc overloading"...
918 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
919 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
922 // create a macro if we see "\\newcommand" somewhere, and an ordinary
924 InsetFormulaBase * f;
925 if (sel.find("\\newcommand") == string::npos &&
926 sel.find("\\def") == string::npos)
927 f = new InsetFormula(sel);
929 f = new InsetFormulaMacro(sel);
930 bv->getLyXText()->cutSelection(true, false);
933 cmd.message(N_("Math editor mode"));
937 void mathDispatch(FuncRequest const & cmd)
939 BufferView * bv = cmd.view();
940 if (!bv->available())
943 switch (cmd.action) {
945 case LFUN_MATH_DISPLAY:
946 mathDispatchCreation(cmd, true);
950 mathDispatchCreation(cmd, false);
953 case LFUN_MATH_IMPORT_SELECTION:
954 mathDispatchCreation(cmd, false);
957 case LFUN_MATH_MACRO:
958 if (cmd.argument.empty())
959 cmd.errorMessage(N_("Missing argument"));
961 string s = cmd.argument;
962 string const s1 = token(s, ' ', 1);
963 int const nargs = s1.empty() ? 0 : atoi(s1);
964 string const s2 = token(s, ' ', 2);
965 string const type = s2.empty() ? "newcommand" : s2;
966 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
970 case LFUN_INSERT_MATH:
971 case LFUN_INSERT_MATRIX:
972 case LFUN_MATH_DELIM: {
973 InsetFormula * f = new InsetFormula(bv);
974 if (openNewInset(bv, f)) {
975 bv->theLockingInset()->
976 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
977 bv->theLockingInset()->localDispatch(cmd);