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;
51 using std::istringstream;
54 MathCursor * mathcursor = 0;
62 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
64 if (!bv->insertInset(new_inset)) {
68 new_inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
77 InsetFormulaBase::InsetFormulaBase()
80 // This is needed as long the math parser is not re-entrant
82 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
83 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
84 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
85 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
89 // simply scrap this function if you want
90 void InsetFormulaBase::mutateToText()
95 latex(NULL, os, false, false);
96 string str = os.str();
99 LyXText * lt = view_->getLyXText();
100 string::const_iterator cit = str.begin();
101 string::const_iterator end = str.end();
102 for (; cit != end; ++cit)
103 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
106 //view_->owner()->dispatch(LFUN_ESCAPE);
111 void InsetFormulaBase::handleFont
112 (BufferView * bv, string const & arg, string const & font)
114 // this whole function is a hack and won't work for incremental font
116 recordUndo(bv, Undo::ATOMIC);
118 if (mathcursor->inset()->name() == font)
119 mathcursor->handleFont(font);
121 mathcursor->handleNest(createMathInset(font));
122 mathcursor->insert(arg);
127 void InsetFormulaBase::handleFont2(BufferView * bv, string const & arg)
129 recordUndo(bv, Undo::ATOMIC);
132 bv_funcs::string2font(arg, font, b);
133 if (font.color() != LColor::inherit) {
134 MathAtom at = createMathInset("color");
135 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
136 mathcursor->handleNest(at, 1);
142 void InsetFormulaBase::cache(BufferView * view) const
145 view_ = view->owner()->view();
148 BufferView * InsetFormulaBase::view() const
150 return view_.lock().get();
154 void InsetFormulaBase::validate(LaTeXFeatures &) const
158 string const InsetFormulaBase::editMessage() const
160 return _("Math editor mode");
164 void InsetFormulaBase::insetUnlock(BufferView * bv)
167 if (mathcursor->inMacroMode()) {
168 mathcursor->macroModeClose();
169 bv->updateInset(this);
171 releaseMathCursor(bv);
174 generatePreview(*bv->buffer());
175 bv->updateInset(this);
179 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
181 mathcursor->getPos(x, y);
185 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
187 // calling metrics here destroys the cached xo,yo positions e.g. in
188 // MathParboxinset. And it would be too expensive anyway...
191 lyxerr << "getCursorPos - should not happen";
195 mathcursor->getPos(x, y);
196 x = mathcursor->targetX();
199 //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
203 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
210 //math_font_max_dim(font_, asc, des);
211 getCursorPos(bv, x, y);
213 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
214 bv->fitLockedInsetCursor(x, y, asc, des);
218 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
221 bv->updateInset(this);
225 dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
230 BufferView * bv = cmd.view();
231 bv->updateInset(this);
232 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
234 if (cmd.button() == mouse_button::button3) {
235 // try to dispatch to enclosed insets first
236 if (mathcursor->dispatch(cmd) == UNDISPATCHED) {
237 // launch math panel for right mouse button
238 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
239 bv->owner()->getDialogs().show("mathpanel");
244 if (cmd.button() == mouse_button::button2) {
246 asArray(bv->getClipboard(), ar);
247 mathcursor->selClear();
248 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
249 mathcursor->insert(ar);
250 bv->updateInset(this);
254 if (cmd.button() == mouse_button::button1) {
255 // try to dispatch to enclosed insets first
256 mathcursor->dispatch(cmd);
257 cmd.view()->stuffClipboard(mathcursor->grabSelection());
258 // try to set the cursor
260 //mathcursor = new MathCursor(this, x == 0);
262 //mathcursor->setPos(x + xo_, y + yo_);
270 dispatch_result InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
272 BufferView * bv = cmd.view();
273 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
275 if (!mathcursor || mathcursor->formula() != this) {
276 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
277 releaseMathCursor(bv);
278 mathcursor = new MathCursor(this, cmd.x == 0);
280 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
283 if (cmd.button() == mouse_button::button3) {
284 mathcursor->dispatch(cmd);
288 if (cmd.button() == mouse_button::button1) {
291 mathcursor->selClear();
292 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
293 mathcursor->dispatch(cmd);
297 bv->updateInset(this);
302 dispatch_result InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
307 if (mathcursor->dispatch(FuncRequest(cmd)) != UNDISPATCHED)
310 // only select with button 1
311 if (cmd.button() != mouse_button::button1)
314 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
320 if (!mathcursor->selection())
321 mathcursor->selStart();
323 BufferView * bv = cmd.view();
324 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
325 bv->updateInset(this);
330 dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
332 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
333 // << " arg: '" << cmd.argument
334 // << "' x: '" << cmd.x
335 // << " y: '" << cmd.y
336 // << "' button: " << cmd.button() << endl;
338 BufferView * bv = cmd.view();
340 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
341 bool remove_inset = false;
343 switch (cmd.action) {
344 case LFUN_INSET_EDIT:
345 lyxerr << "Called EDIT with '" << cmd.argument << "'" << endl;
346 if (!bv->lockInset(this))
347 lyxerr << "Cannot lock math inset in edit call!" << endl;
348 releaseMathCursor(bv);
349 if (!cmd.argument.empty()) {
350 mathcursor = new MathCursor(this, cmd.argument == "left");
353 mathcursor = new MathCursor(this, true);
355 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
357 // if that is removed, we won't get the magenta box when entering an
358 // inset for the first time
359 bv->updateInset(this);
362 case LFUN_MOUSE_PRESS:
363 //lyxerr << "Mouse single press" << endl;
364 return lfunMousePress(cmd);
365 case LFUN_MOUSE_MOTION:
366 //lyxerr << "Mouse motion" << endl;
367 return lfunMouseMotion(cmd);
368 case LFUN_MOUSE_RELEASE:
369 //lyxerr << "Mouse single release" << endl;
370 return lfunMouseRelease(cmd);
371 case LFUN_MOUSE_DOUBLE:
372 //lyxerr << "Mouse double" << endl;
373 return localDispatch(FuncRequest(LFUN_WORDSEL));
381 string argument = cmd.argument;
382 dispatch_result result = DISPATCHED;
384 bool was_macro = mathcursor->inMacroMode();
385 bool was_selection = mathcursor->selection();
387 mathcursor->normalize();
390 switch (cmd.action) {
392 case LFUN_MATH_MUTATE:
393 case LFUN_MATH_DISPLAY:
394 case LFUN_MATH_NUMBER:
395 case LFUN_MATH_NONUMBER:
396 case LFUN_CELL_SPLIT:
398 case LFUN_DELETE_LINE_FORWARD:
399 case LFUN_INSERT_LABEL:
400 case LFUN_MATH_EXTERN:
401 case LFUN_TABULAR_FEATURE:
402 case LFUN_PASTESELECTION:
403 case LFUN_MATH_LIMITS:
404 recordUndo(bv, Undo::ATOMIC);
405 mathcursor->dispatch(cmd);
409 sel = true; // fall through...
411 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
412 //lyxerr << "calling scroll 20" << endl;
414 // write something to the minibuffer
415 //bv->owner()->message(mathcursor->info());
419 sel = true; // fall through
421 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
425 sel = true; // fall through
427 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
431 sel = true; // fall through
433 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
437 mathcursor->home(false);
438 mathcursor->end(true);
441 case LFUN_UP_PARAGRAPHSEL:
442 case LFUN_UP_PARAGRAPH:
443 case LFUN_DOWN_PARAGRAPHSEL:
444 case LFUN_DOWN_PARAGRAPH:
449 case LFUN_WORDLEFTSEL:
450 sel = true; // fall through
453 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
457 case LFUN_WORDRIGHTSEL:
458 sel = true; // fall through
461 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
466 case LFUN_BEGINNINGBUFSEL:
467 case LFUN_BEGINNINGBUF:
475 result = FINISHED_RIGHT;
478 case LFUN_CELL_FORWARD:
479 mathcursor->idxNext();
482 case LFUN_CELL_BACKWARD:
483 mathcursor->idxPrev();
486 case LFUN_DELETE_WORD_BACKWARD:
488 recordUndo(bv, Undo::ATOMIC);
489 if (!mathcursor->backspace()) {
495 case LFUN_DELETE_WORD_FORWARD:
497 recordUndo(bv, Undo::ATOMIC);
498 if (!mathcursor->erase()) {
505 // sprintf(dispatch_buffer, "%d %d",);
506 // dispatch_result = dispatch_buffer;
509 lyxerr << "LFUN_SETXY broken!" << endl;
512 istringstream is(cmd.argument.c_str());
514 mathcursor->setPos(x, y);
520 istringstream is(cmd.argument.c_str());
523 mathcursor->macroModeClose();
524 recordUndo(bv, Undo::ATOMIC);
525 mathcursor->selPaste(n);
530 recordUndo(bv, Undo::DELETE);
531 mathcursor->selCut();
535 mathcursor->selCopy();
539 // Special casing for superscript in case of LyX handling
541 case LFUN_CIRCUMFLEX:
542 if (cmd.argument.empty()) {
543 // do superscript if LyX handles
545 recordUndo(bv, Undo::ATOMIC);
546 mathcursor->script(true);
563 case LFUN_HUNG_UMLAUT:
567 case LFUN_FREEFONT_APPLY:
568 case LFUN_FREEFONT_UPDATE:
569 handleFont2(bv, cmd.argument);
572 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
573 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
574 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
575 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
576 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
577 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
578 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
579 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
580 //case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
581 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
584 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
585 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
587 handleFont(bv, cmd.argument, "textrm");
588 //bv->owner()->message(_("math text mode toggled"));
594 recordUndo(bv, Undo::ATOMIC);
595 mathcursor->setSize(arg);
600 case LFUN_INSERT_MATRIX: {
601 recordUndo(bv, Undo::ATOMIC);
606 istringstream is(argument);
607 is >> m >> n >> v_align >> h_align;
611 mathcursor->niceInsert(
612 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
616 case LFUN_SUPERSCRIPT:
619 recordUndo(bv, Undo::ATOMIC);
620 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
624 case LFUN_MATH_DELIM:
626 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
628 string rs = split(cmd.argument, ls, ' ');
629 // Reasonable default values
635 recordUndo(bv, Undo::ATOMIC);
636 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
640 case LFUN_SPACE_INSERT:
641 case LFUN_MATH_SPACE:
642 recordUndo(bv, Undo::ATOMIC);
643 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
647 bv->owner()->message(_("Invalid action in math mode!"));
651 case LFUN_EXEC_COMMAND:
652 result = UNDISPATCHED;
656 // interpret this as if a backslash was typed
657 recordUndo(bv, Undo::ATOMIC);
658 mathcursor->interpret('\\');
661 case LFUN_BREAKPARAGRAPH:
662 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
663 case LFUN_BREAKPARAGRAPH_SKIP:
667 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
668 // handling such that "self-insert" works on "arbitrary stuff" too, and
669 // math-insert only handles special math things like "matrix".
670 case LFUN_INSERT_MATH:
671 recordUndo(bv, Undo::ATOMIC);
672 mathcursor->niceInsert(argument);
676 case LFUN_SELFINSERT:
677 if (!argument.empty()) {
678 recordUndo(bv, Undo::ATOMIC);
679 if (argument.size() == 1)
680 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
682 mathcursor->insert(argument);
687 if (mathcursor->selection())
688 mathcursor->selClear();
690 result = UNDISPATCHED;
693 case LFUN_INSET_TOGGLE:
694 mathcursor->insetToggle();
697 case LFUN_DIALOG_SHOW:
698 result = UNDISPATCHED;
701 case LFUN_DIALOG_SHOW_NEW_INSET: {
702 string const & name = argument;
706 data = tmp.createDialogStr(name);
710 result = UNDISPATCHED;
712 bv->owner()->getDialogs().show(name, data, 0);
717 case LFUN_INSET_APPLY: {
718 string const name = cmd.getArg(0);
720 bv->owner()->getDialogs().getOpenInset(name);
723 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
724 result = base->localDispatch(fr);
727 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
728 mathcursor->insert(ar);
731 result = UNDISPATCHED;
738 result = UNDISPATCHED;
741 if (result == DISPATCHED)
742 bv->updateInset(this);
744 mathcursor->normalize();
747 BOOST_ASSERT(mathcursor);
749 if (mathcursor->selection() || was_selection)
750 toggleInsetSelection(bv);
752 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
753 result == UNDISPATCHED) {
756 cmd.view()->stuffClipboard(mathcursor->grabSelection());
758 releaseMathCursor(bv);
759 bv->unlockInset(this);
761 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
764 return result; // original version
768 void InsetFormulaBase::revealCodes(BufferView * bv) const
772 bv->owner()->message(mathcursor->info());
775 // write something to the minibuffer
776 // translate to latex
777 mathcursor->markInsert();
780 string str = os.str();
781 mathcursor->markErase();
782 string::size_type pos = 0;
784 for (string::iterator it = str.begin(); it != str.end(); ++it) {
787 else if (*it == '\0') {
789 pos = it - str.begin();
795 res = res.substr(pos - 30);
797 res = res.substr(0, 60);
798 bv->owner()->message(res);
803 InsetOld::Code InsetFormulaBase::lyxCode() const
805 return InsetOld::MATH_CODE;
809 int InsetFormulaBase::ylow() const
811 return yo_ - dim_.asc;
815 int InsetFormulaBase::yhigh() const
817 return yo_ + dim_.des;
821 int InsetFormulaBase::xlow() const
827 int InsetFormulaBase::xhigh() const
829 return xo_ + dim_.wid;
833 /////////////////////////////////////////////////////////////////////
836 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
842 static InsetFormulaBase * lastformula = 0;
843 static MathIterator current = MathIterator(ibegin(par().nucleus()));
845 static string laststr;
847 if (lastformula != this || laststr != str) {
848 //lyxerr << "reset lastformula to " << this << endl;
851 current = ibegin(par().nucleus());
853 mathed_parse_cell(ar, str);
857 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
859 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
860 if (it.cell().matchpart(ar, it.back().pos_)) {
861 bv->unlockInset(bv->theLockingInset());
862 if (!bv->lockInset(this)) {
863 lyxerr << "Cannot lock inset" << endl;
867 mathcursor = new MathCursor(this, true);
869 mathcursor->setSelection(it, ar.size());
872 bv->updateInset(this);
877 //lyxerr << "not found!" << endl;
883 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
886 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
887 return searchForward(bv, what, a, b);
891 bool InsetFormulaBase::display() const
893 return par()->asHullInset() && par()->asHullInset()->display();
897 string InsetFormulaBase::selectionAsString() const
899 return mathcursor ? mathcursor->grabSelection() : string();
902 /////////////////////////////////////////////////////////////////////
905 void mathDispatchCreation(FuncRequest const & cmd, bool display)
907 BufferView * bv = cmd.view();
908 // use selection if available..
910 //if (action == LFUN_MATH_IMPORT_SELECTION)
914 string sel = bv->getLyXText()->selectionAsString(*bv->buffer(), false);
917 InsetFormula * f = new InsetFormula(bv);
918 if (openNewInset(bv, f)) {
919 bv->theLockingInset()->
920 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
921 // don't do that also for LFUN_MATH_MODE unless you want end up with
922 // always changing to mathrm when opening an inlined inset
923 // -- I really hate "LyXfunc overloading"...
925 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
926 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
929 // create a macro if we see "\\newcommand" somewhere, and an ordinary
931 InsetFormulaBase * f;
932 if (sel.find("\\newcommand") == string::npos &&
933 sel.find("\\def") == string::npos)
934 f = new InsetFormula(sel);
936 f = new InsetFormulaMacro(sel);
937 bv->getLyXText()->cutSelection(true, false);
940 cmd.message(N_("Math editor mode"));
944 void mathDispatch(FuncRequest const & cmd)
946 BufferView * bv = cmd.view();
947 if (!bv->available())
950 switch (cmd.action) {
952 case LFUN_MATH_DISPLAY:
953 mathDispatchCreation(cmd, true);
957 mathDispatchCreation(cmd, false);
960 case LFUN_MATH_IMPORT_SELECTION:
961 mathDispatchCreation(cmd, false);
964 case LFUN_MATH_MACRO:
965 if (cmd.argument.empty())
966 cmd.errorMessage(N_("Missing argument"));
968 string s = cmd.argument;
969 string const s1 = token(s, ' ', 1);
970 int const nargs = s1.empty() ? 0 : atoi(s1);
971 string const s2 = token(s, ' ', 2);
972 string const type = s2.empty() ? "newcommand" : s2;
973 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
977 case LFUN_INSERT_MATH:
978 case LFUN_INSERT_MATRIX:
979 case LFUN_MATH_DELIM: {
980 InsetFormula * f = new InsetFormula(bv);
981 if (openNewInset(bv, f)) {
982 bv->theLockingInset()->
983 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
984 bv->theLockingInset()->localDispatch(cmd);