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()
78 : view_(0), xo_(0), yo_(0)
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 BufferView * InsetFormulaBase::view() const
148 void InsetFormulaBase::validate(LaTeXFeatures &) const
152 string const InsetFormulaBase::editMessage() const
154 return _("Math editor mode");
158 void InsetFormulaBase::insetUnlock(BufferView * bv)
161 if (mathcursor->inMacroMode()) {
162 mathcursor->macroModeClose();
163 bv->updateInset(this);
165 releaseMathCursor(bv);
168 generatePreview(*bv->buffer());
169 bv->updateInset(this);
173 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
175 mathcursor->getPos(x, y);
179 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
181 // calling metrics here destroys the cached xo,yo positions e.g. in
182 // MathParboxinset. And it would be too expensive anyway...
185 lyxerr << "getCursorPos - should not happen";
189 mathcursor->getPos(x, y);
190 x = mathcursor->targetX();
193 //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
197 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
204 //math_font_max_dim(font_, asc, des);
205 getCursorPos(bv, x, y);
207 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
208 bv->fitLockedInsetCursor(x, y, asc, des);
212 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
215 bv->updateInset(this);
219 dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
224 BufferView * bv = cmd.view();
225 bv->updateInset(this);
226 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
228 if (cmd.button() == mouse_button::button3) {
229 // try to dispatch to enclosed insets first
230 if (mathcursor->dispatch(cmd) == UNDISPATCHED) {
231 // launch math panel for right mouse button
232 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
233 bv->owner()->getDialogs().show("mathpanel");
238 if (cmd.button() == mouse_button::button2) {
240 asArray(bv->getClipboard(), ar);
241 mathcursor->selClear();
242 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
243 mathcursor->insert(ar);
244 bv->updateInset(this);
248 if (cmd.button() == mouse_button::button1) {
249 // try to dispatch to enclosed insets first
250 mathcursor->dispatch(cmd);
251 cmd.view()->stuffClipboard(mathcursor->grabSelection());
252 // try to set the cursor
254 //mathcursor = new MathCursor(this, x == 0);
256 //mathcursor->setPos(x + xo_, y + yo_);
264 dispatch_result InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
266 BufferView * bv = cmd.view();
267 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
269 if (!mathcursor || mathcursor->formula() != this) {
270 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
271 releaseMathCursor(bv);
272 mathcursor = new MathCursor(this, cmd.x == 0);
274 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
277 if (cmd.button() == mouse_button::button3) {
278 mathcursor->dispatch(cmd);
282 if (cmd.button() == mouse_button::button1) {
285 mathcursor->selClear();
286 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
287 mathcursor->dispatch(cmd);
291 bv->updateInset(this);
296 dispatch_result InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
301 if (mathcursor->dispatch(FuncRequest(cmd)) != UNDISPATCHED)
304 // only select with button 1
305 if (cmd.button() != mouse_button::button1)
308 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
314 if (!mathcursor->selection())
315 mathcursor->selStart();
317 BufferView * bv = cmd.view();
318 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
319 bv->updateInset(this);
324 dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
326 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
327 // << " arg: '" << cmd.argument
328 // << "' x: '" << cmd.x
329 // << " y: '" << cmd.y
330 // << "' button: " << cmd.button() << endl;
332 BufferView * bv = cmd.view();
334 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
335 bool remove_inset = false;
337 switch (cmd.action) {
338 case LFUN_INSET_EDIT:
339 lyxerr << "Called EDIT with '" << cmd.argument << "'" << endl;
340 if (!bv->lockInset(this))
341 lyxerr << "Cannot lock math inset in edit call!" << endl;
342 releaseMathCursor(bv);
343 if (!cmd.argument.empty()) {
344 mathcursor = new MathCursor(this, cmd.argument == "left");
347 mathcursor = new MathCursor(this, true);
349 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
351 // if that is removed, we won't get the magenta box when entering an
352 // inset for the first time
353 bv->updateInset(this);
356 case LFUN_MOUSE_PRESS:
357 //lyxerr << "Mouse single press" << endl;
358 return lfunMousePress(cmd);
359 case LFUN_MOUSE_MOTION:
360 //lyxerr << "Mouse motion" << endl;
361 return lfunMouseMotion(cmd);
362 case LFUN_MOUSE_RELEASE:
363 //lyxerr << "Mouse single release" << endl;
364 return lfunMouseRelease(cmd);
365 case LFUN_MOUSE_DOUBLE:
366 //lyxerr << "Mouse double" << endl;
367 return localDispatch(FuncRequest(LFUN_WORDSEL));
375 string argument = cmd.argument;
376 RESULT result = DISPATCHED;
378 bool was_macro = mathcursor->inMacroMode();
379 bool was_selection = mathcursor->selection();
381 mathcursor->normalize();
384 switch (cmd.action) {
386 case LFUN_MATH_MUTATE:
387 case LFUN_MATH_DISPLAY:
388 case LFUN_MATH_NUMBER:
389 case LFUN_MATH_NONUMBER:
390 case LFUN_CELL_SPLIT:
392 case LFUN_DELETE_LINE_FORWARD:
393 case LFUN_INSERT_LABEL:
394 case LFUN_MATH_EXTERN:
395 case LFUN_TABULAR_FEATURE:
396 case LFUN_PASTESELECTION:
397 case LFUN_MATH_LIMITS:
398 recordUndo(bv, Undo::ATOMIC);
399 mathcursor->dispatch(cmd);
403 sel = true; // fall through...
405 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
406 //lyxerr << "calling scroll 20" << endl;
408 // write something to the minibuffer
409 //bv->owner()->message(mathcursor->info());
413 sel = true; // fall through
415 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
419 sel = true; // fall through
421 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
425 sel = true; // fall through
427 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
431 mathcursor->home(false);
432 mathcursor->end(true);
435 case LFUN_UP_PARAGRAPHSEL:
436 case LFUN_UP_PARAGRAPH:
437 case LFUN_DOWN_PARAGRAPHSEL:
438 case LFUN_DOWN_PARAGRAPH:
443 case LFUN_WORDLEFTSEL:
444 sel = true; // fall through
447 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
451 case LFUN_WORDRIGHTSEL:
452 sel = true; // fall through
455 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
460 case LFUN_BEGINNINGBUFSEL:
461 case LFUN_BEGINNINGBUF:
469 result = FINISHED_RIGHT;
472 case LFUN_CELL_FORWARD:
473 mathcursor->idxNext();
476 case LFUN_CELL_BACKWARD:
477 mathcursor->idxPrev();
480 case LFUN_DELETE_WORD_BACKWARD:
482 recordUndo(bv, Undo::ATOMIC);
483 if (!mathcursor->backspace()) {
489 case LFUN_DELETE_WORD_FORWARD:
491 recordUndo(bv, Undo::ATOMIC);
492 if (!mathcursor->erase()) {
499 // sprintf(dispatch_buffer, "%d %d",);
500 // dispatch_result = dispatch_buffer;
503 lyxerr << "LFUN_SETXY broken!" << endl;
506 istringstream is(cmd.argument.c_str());
508 mathcursor->setPos(x, y);
514 istringstream is(cmd.argument.c_str());
517 mathcursor->macroModeClose();
518 recordUndo(bv, Undo::ATOMIC);
519 mathcursor->selPaste(n);
524 recordUndo(bv, Undo::DELETE);
525 mathcursor->selCut();
529 mathcursor->selCopy();
533 // Special casing for superscript in case of LyX handling
535 case LFUN_CIRCUMFLEX:
536 if (cmd.argument.empty()) {
537 // do superscript if LyX handles
539 recordUndo(bv, Undo::ATOMIC);
540 mathcursor->script(true);
557 case LFUN_HUNG_UMLAUT:
561 case LFUN_FREEFONT_APPLY:
562 case LFUN_FREEFONT_UPDATE:
563 handleFont2(bv, cmd.argument);
566 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
567 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
568 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
569 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
570 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
571 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
572 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
573 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
574 //case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
575 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
578 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
579 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
581 handleFont(bv, cmd.argument, "textrm");
582 //bv->owner()->message(_("math text mode toggled"));
588 recordUndo(bv, Undo::ATOMIC);
589 mathcursor->setSize(arg);
594 case LFUN_INSERT_MATRIX: {
595 recordUndo(bv, Undo::ATOMIC);
600 istringstream is(argument);
601 is >> m >> n >> v_align >> h_align;
605 mathcursor->niceInsert(
606 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
610 case LFUN_SUPERSCRIPT:
613 recordUndo(bv, Undo::ATOMIC);
614 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
618 case LFUN_MATH_DELIM:
620 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
622 string rs = split(cmd.argument, ls, ' ');
623 // Reasonable default values
629 recordUndo(bv, Undo::ATOMIC);
630 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
634 case LFUN_SPACE_INSERT:
635 case LFUN_MATH_SPACE:
636 recordUndo(bv, Undo::ATOMIC);
637 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
641 bv->owner()->message(_("Invalid action in math mode!"));
645 case LFUN_EXEC_COMMAND:
646 result = UNDISPATCHED;
650 // interpret this as if a backslash was typed
651 recordUndo(bv, Undo::ATOMIC);
652 mathcursor->interpret('\\');
655 case LFUN_BREAKPARAGRAPH:
656 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
657 case LFUN_BREAKPARAGRAPH_SKIP:
661 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
662 // handling such that "self-insert" works on "arbitrary stuff" too, and
663 // math-insert only handles special math things like "matrix".
664 case LFUN_INSERT_MATH:
665 recordUndo(bv, Undo::ATOMIC);
666 mathcursor->niceInsert(argument);
670 case LFUN_SELFINSERT:
671 if (!argument.empty()) {
672 recordUndo(bv, Undo::ATOMIC);
673 if (argument.size() == 1)
674 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
676 mathcursor->insert(argument);
681 if (mathcursor->selection())
682 mathcursor->selClear();
684 result = UNDISPATCHED;
687 case LFUN_INSET_TOGGLE:
688 mathcursor->insetToggle();
691 case LFUN_DIALOG_SHOW:
692 result = UNDISPATCHED;
695 case LFUN_DIALOG_SHOW_NEW_INSET: {
696 string const & name = argument;
700 data = tmp.createDialogStr(name);
704 result = UNDISPATCHED;
706 bv->owner()->getDialogs().show(name, data, 0);
711 case LFUN_INSET_APPLY: {
712 string const name = cmd.getArg(0);
714 bv->owner()->getDialogs().getOpenInset(name);
717 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
718 result = base->localDispatch(fr);
721 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
722 mathcursor->insert(ar);
725 result = UNDISPATCHED;
732 result = UNDISPATCHED;
735 if (result == DISPATCHED)
736 bv->updateInset(this);
738 mathcursor->normalize();
741 BOOST_ASSERT(mathcursor);
743 if (mathcursor->selection() || was_selection)
744 toggleInsetSelection(bv);
746 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
747 result == UNDISPATCHED) {
750 cmd.view()->stuffClipboard(mathcursor->grabSelection());
752 releaseMathCursor(bv);
753 bv->unlockInset(this);
755 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
758 return result; // original version
762 void InsetFormulaBase::revealCodes(BufferView * bv) const
766 bv->owner()->message(mathcursor->info());
769 // write something to the minibuffer
770 // translate to latex
771 mathcursor->markInsert();
774 string str = os.str();
775 mathcursor->markErase();
776 string::size_type pos = 0;
778 for (string::iterator it = str.begin(); it != str.end(); ++it) {
781 else if (*it == '\0') {
783 pos = it - str.begin();
789 res = res.substr(pos - 30);
791 res = res.substr(0, 60);
792 bv->owner()->message(res);
797 InsetOld::Code InsetFormulaBase::lyxCode() const
799 return InsetOld::MATH_CODE;
803 int InsetFormulaBase::ylow() const
805 return yo_ - dim_.asc;
809 int InsetFormulaBase::yhigh() const
811 return yo_ + dim_.des;
815 int InsetFormulaBase::xlow() const
821 int InsetFormulaBase::xhigh() const
823 return xo_ + dim_.wid;
827 /////////////////////////////////////////////////////////////////////
830 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
836 static InsetFormulaBase * lastformula = 0;
837 static MathIterator current = MathIterator(ibegin(par().nucleus()));
839 static string laststr;
841 if (lastformula != this || laststr != str) {
842 //lyxerr << "reset lastformula to " << this << endl;
845 current = ibegin(par().nucleus());
847 mathed_parse_cell(ar, str);
851 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
853 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
854 if (it.cell().matchpart(ar, it.back().pos_)) {
855 bv->unlockInset(bv->theLockingInset());
856 if (!bv->lockInset(this)) {
857 lyxerr << "Cannot lock inset" << endl;
861 mathcursor = new MathCursor(this, true);
863 mathcursor->setSelection(it, ar.size());
866 bv->updateInset(this);
871 //lyxerr << "not found!" << endl;
877 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
880 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
881 return searchForward(bv, what, a, b);
885 bool InsetFormulaBase::display() const
887 return par()->asHullInset() && par()->asHullInset()->display();
891 string InsetFormulaBase::selectionAsString() const
893 return mathcursor ? mathcursor->grabSelection() : string();
896 /////////////////////////////////////////////////////////////////////
899 void mathDispatchCreation(FuncRequest const & cmd, bool display)
901 BufferView * bv = cmd.view();
902 // use selection if available..
904 //if (action == LFUN_MATH_IMPORT_SELECTION)
908 string sel = bv->getLyXText()->selectionAsString(*bv->buffer(), false);
911 InsetFormula * f = new InsetFormula(bv);
912 if (openNewInset(bv, f)) {
913 bv->theLockingInset()->
914 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
915 // don't do that also for LFUN_MATH_MODE unless you want end up with
916 // always changing to mathrm when opening an inlined inset
917 // -- I really hate "LyXfunc overloading"...
919 f->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
920 f->localDispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
923 // create a macro if we see "\\newcommand" somewhere, and an ordinary
925 InsetFormulaBase * f;
926 if (sel.find("\\newcommand") == string::npos &&
927 sel.find("\\def") == string::npos)
928 f = new InsetFormula(sel);
930 f = new InsetFormulaMacro(sel);
931 bv->getLyXText()->cutSelection(true, false);
934 cmd.message(N_("Math editor mode"));
938 void mathDispatch(FuncRequest const & cmd)
940 BufferView * bv = cmd.view();
941 if (!bv->available())
944 switch (cmd.action) {
946 case LFUN_MATH_DISPLAY:
947 mathDispatchCreation(cmd, true);
951 mathDispatchCreation(cmd, false);
954 case LFUN_MATH_IMPORT_SELECTION:
955 mathDispatchCreation(cmd, false);
958 case LFUN_MATH_MACRO:
959 if (cmd.argument.empty())
960 cmd.errorMessage(N_("Missing argument"));
962 string s = cmd.argument;
963 string const s1 = token(s, ' ', 1);
964 int const nargs = s1.empty() ? 0 : atoi(s1);
965 string const s2 = token(s, ' ', 2);
966 string const type = s2.empty() ? "newcommand" : s2;
967 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
971 case LFUN_INSERT_MATH:
972 case LFUN_INSERT_MATRIX:
973 case LFUN_MATH_DELIM: {
974 InsetFormula * f = new InsetFormula(bv);
975 if (openNewInset(bv, f)) {
976 bv->theLockingInset()->
977 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
978 bv->theLockingInset()->localDispatch(cmd);