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 "formulabase.h"
17 #include "formulamacro.h"
18 #include "math_support.h"
19 #include "math_arrayinset.h"
20 #include "math_deliminset.h"
21 #include "math_cursor.h"
22 #include "math_factory.h"
23 #include "math_hullinset.h"
24 #include "math_parser.h"
25 #include "math_spaceinset.h"
26 #include "ref_inset.h"
28 #include "BufferView.h"
29 #include "bufferview_funcs.h"
30 #include "dispatchresult.h"
32 #include "funcrequest.h"
38 #include "frontends/LyXView.h"
39 #include "frontends/Dialogs.h"
41 #include "support/std_sstream.h"
42 #include "support/lstrings.h"
43 #include "support/lyxlib.h"
45 using lyx::support::atoi;
46 using lyx::support::split;
47 using lyx::support::token;
54 using std::istringstream;
57 MathCursor * mathcursor = 0;
65 bool openNewInset(BufferView * bv, UpdatableInset * inset)
67 if (!bv->insertInset(inset)) {
71 inset->edit(bv, true);
80 InsetFormulaBase::InsetFormulaBase()
82 // This is needed as long the math parser is not re-entrant
84 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
85 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
86 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
87 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
91 // simply scrap this function if you want
92 void InsetFormulaBase::mutateToText()
97 latex(NULL, os, false, false);
98 string str = os.str();
101 LyXText * lt = view_->getLyXText();
102 string::const_iterator cit = str.begin();
103 string::const_iterator end = str.end();
104 for (; cit != end; ++cit)
105 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
108 //view_->owner()->dispatch(LFUN_ESCAPE);
113 void InsetFormulaBase::handleFont
114 (BufferView * bv, string const & arg, string const & font)
116 // this whole function is a hack and won't work for incremental font
118 recordUndo(bv, Undo::ATOMIC);
120 if (mathcursor->inset()->name() == font)
121 mathcursor->handleFont(font);
123 mathcursor->handleNest(createMathInset(font));
124 mathcursor->insert(arg);
129 void InsetFormulaBase::handleFont2(BufferView * bv, string const & arg)
131 recordUndo(bv, Undo::ATOMIC);
134 bv_funcs::string2font(arg, font, b);
135 if (font.color() != LColor::inherit) {
136 MathAtom at = createMathInset("color");
137 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
138 mathcursor->handleNest(at, 1);
144 void InsetFormulaBase::validate(LaTeXFeatures &) const
148 string const InsetFormulaBase::editMessage() const
150 return _("Math editor mode");
154 void InsetFormulaBase::insetUnlock(BufferView * bv)
157 if (mathcursor->inMacroMode())
158 mathcursor->macroModeClose();
159 releaseMathCursor(bv);
162 generatePreview(*bv->buffer());
167 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
169 mathcursor->getPos(x, y);
173 void InsetFormulaBase::getCursorPos(int & x, int & y) const
176 lyxerr << "getCursorPos - should not happen";
181 mathcursor->getPos(x, y);
182 x = mathcursor->targetX();
185 lyxerr << "InsetFormulaBase::getCursorPos: " << x << ' ' << y << endl;
189 void InsetFormulaBase::getCursorDim(int & asc, int & desc) const
195 //math_font_max_dim(font_, asc, des);
199 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
206 DispatchResult InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
209 return DispatchResult(false);
211 BufferView * bv = cmd.view();
213 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
215 if (cmd.button() == mouse_button::button3) {
216 // try to dispatch to enclosed insets first
217 if (!mathcursor->dispatch(cmd).dispatched()) {
218 // launch math panel for right mouse button
219 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
220 bv->owner()->getDialogs().show("mathpanel");
222 return DispatchResult(true, true);
225 if (cmd.button() == mouse_button::button2) {
227 asArray(bv->getClipboard(), ar);
228 mathcursor->selClear();
229 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
230 mathcursor->insert(ar);
232 return DispatchResult(true, true);
235 if (cmd.button() == mouse_button::button1) {
236 // try to dispatch to enclosed insets first
237 mathcursor->dispatch(cmd);
238 cmd.view()->stuffClipboard(mathcursor->grabSelection());
239 // try to set the cursor
241 //mathcursor = new MathCursor(this, x == 0);
243 //mathcursor->setPos(x + xo_, y + yo_);
244 return DispatchResult(true, true);
247 return DispatchResult(false);
251 DispatchResult InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
253 BufferView * bv = cmd.view();
254 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
256 if (!mathcursor || mathcursor->formula() != this) {
257 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
258 releaseMathCursor(bv);
259 mathcursor = new MathCursor(this, cmd.x == 0);
261 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
264 if (cmd.button() == mouse_button::button3) {
265 mathcursor->dispatch(cmd);
266 return DispatchResult(true, true);
269 if (cmd.button() == mouse_button::button1) {
272 mathcursor->selClear();
273 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
274 mathcursor->dispatch(cmd);
275 return DispatchResult(true, true);
279 return DispatchResult(true, true);
283 DispatchResult InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
286 return DispatchResult(true, true);
288 if (mathcursor->dispatch(FuncRequest(cmd)).dispatched())
289 return DispatchResult(true, true);
291 // only select with button 1
292 if (cmd.button() != mouse_button::button1)
293 return DispatchResult(true, true);
295 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
296 return DispatchResult(true, true);
301 if (!mathcursor->selection())
302 mathcursor->selStart();
304 BufferView * bv = cmd.view();
305 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
307 return DispatchResult(true, true);
311 void InsetFormulaBase::edit(BufferView * bv, bool left)
313 lyxerr << "Called FormulaBase::edit" << endl;
314 releaseMathCursor(bv);
315 mathcursor = new MathCursor(this, left);
316 bv->cursor().push(this);
317 // if that is removed, we won't get the magenta box when entering an
318 // inset for the first time
323 void InsetFormulaBase::edit(BufferView * bv, int x, int y)
325 lyxerr << "Called FormulaBase::EDIT with '" << x << ' ' << y << "'" << endl;
326 releaseMathCursor(bv);
327 mathcursor = new MathCursor(this, true);
329 mathcursor->setPos(x + xo_, y + yo_);
330 bv->cursor().push(this);
331 // if that is removed, we won't get the magenta box when entering an
332 // inset for the first time
338 InsetFormulaBase::priv_dispatch(FuncRequest const & cmd,
339 idx_type &, pos_type &)
341 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
342 // << " arg: '" << cmd.argument
343 // << "' x: '" << cmd.x
344 // << " y: '" << cmd.y
345 // << "' button: " << cmd.button() << endl;
347 BufferView * bv = cmd.view();
349 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
350 bool remove_inset = false;
352 switch (cmd.action) {
353 case LFUN_MOUSE_PRESS:
354 //lyxerr << "Mouse single press" << endl;
355 return lfunMousePress(cmd);
356 case LFUN_MOUSE_MOTION:
357 //lyxerr << "Mouse motion" << endl;
358 return lfunMouseMotion(cmd);
359 case LFUN_MOUSE_RELEASE:
360 //lyxerr << "Mouse single release" << endl;
361 return lfunMouseRelease(cmd);
362 case LFUN_MOUSE_DOUBLE:
363 //lyxerr << "Mouse double" << endl;
364 return dispatch(FuncRequest(LFUN_WORDSEL));
370 return DispatchResult(false);
372 string argument = cmd.argument;
373 DispatchResult result(true);
375 bool was_macro = mathcursor->inMacroMode();
376 bool was_selection = mathcursor->selection();
378 mathcursor->normalize();
381 switch (cmd.action) {
383 case LFUN_MATH_MUTATE:
384 case LFUN_MATH_DISPLAY:
385 case LFUN_MATH_NUMBER:
386 case LFUN_MATH_NONUMBER:
387 case LFUN_CELL_SPLIT:
389 case LFUN_DELETE_LINE_FORWARD:
390 case LFUN_INSERT_LABEL:
391 case LFUN_MATH_EXTERN:
392 case LFUN_TABULAR_FEATURE:
393 case LFUN_PASTESELECTION:
394 case LFUN_MATH_LIMITS:
395 recordUndo(bv, Undo::ATOMIC);
396 mathcursor->dispatch(cmd);
400 sel = true; // fall through...
402 result = mathcursor->right(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
403 //lyxerr << "calling scroll 20" << endl;
405 // write something to the minibuffer
406 //bv->owner()->message(mathcursor->info());
410 sel = true; // fall through
412 result = mathcursor->left(sel) ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
416 sel = true; // fall through
418 result = mathcursor->up(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_UP);
422 sel = true; // fall through
424 result = mathcursor->down(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_DOWN);
428 mathcursor->home(false);
429 mathcursor->end(true);
432 case LFUN_UP_PARAGRAPHSEL:
433 case LFUN_UP_PARAGRAPH:
434 case LFUN_DOWN_PARAGRAPHSEL:
435 case LFUN_DOWN_PARAGRAPH:
436 result = DispatchResult(true, FINISHED);
440 case LFUN_WORDLEFTSEL:
441 sel = true; // fall through
444 result = mathcursor->home(sel) ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
448 case LFUN_WORDRIGHTSEL:
449 sel = true; // fall through
452 result = mathcursor->end(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
457 case LFUN_BEGINNINGBUFSEL:
458 case LFUN_BEGINNINGBUF:
459 result = DispatchResult(true, FINISHED);
466 result = DispatchResult(false, FINISHED_RIGHT);
469 case LFUN_CELL_FORWARD:
470 mathcursor->idxNext();
473 case LFUN_CELL_BACKWARD:
474 mathcursor->idxPrev();
477 case LFUN_DELETE_WORD_BACKWARD:
479 recordUndo(bv, Undo::ATOMIC);
480 if (!mathcursor->backspace()) {
481 result = DispatchResult(true, FINISHED);
486 case LFUN_DELETE_WORD_FORWARD:
488 recordUndo(bv, Undo::ATOMIC);
489 if (!mathcursor->erase()) {
490 result = DispatchResult(true, FINISHED);
496 // sprintf(dispatch_buffer, "%d %d",);
497 // DispatchResult= dispatch_buffer;
500 lyxerr << "LFUN_SETXY broken!" << endl;
503 istringstream is(cmd.argument.c_str());
505 mathcursor->setPos(x, y);
511 istringstream is(cmd.argument.c_str());
514 mathcursor->macroModeClose();
515 recordUndo(bv, Undo::ATOMIC);
516 mathcursor->selPaste(n);
521 recordUndo(bv, Undo::DELETE);
522 mathcursor->selCut();
526 mathcursor->selCopy();
530 // Special casing for superscript in case of LyX handling
532 case LFUN_CIRCUMFLEX:
533 if (cmd.argument.empty()) {
534 // do superscript if LyX handles
536 recordUndo(bv, Undo::ATOMIC);
537 mathcursor->script(true);
554 case LFUN_HUNG_UMLAUT:
558 case LFUN_FREEFONT_APPLY:
559 case LFUN_FREEFONT_UPDATE:
560 handleFont2(bv, cmd.argument);
563 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
564 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
565 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
566 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
567 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
568 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
569 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
570 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
571 //case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
572 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
575 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
576 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
578 handleFont(bv, cmd.argument, "textrm");
579 //bv->owner()->message(_("math text mode toggled"));
585 recordUndo(bv, Undo::ATOMIC);
586 mathcursor->setSize(arg);
591 case LFUN_INSERT_MATRIX: {
592 recordUndo(bv, Undo::ATOMIC);
597 istringstream is(argument);
598 is >> m >> n >> v_align >> h_align;
602 mathcursor->niceInsert(
603 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
607 case LFUN_SUPERSCRIPT:
610 recordUndo(bv, Undo::ATOMIC);
611 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
615 case LFUN_MATH_DELIM:
617 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
619 string rs = split(cmd.argument, ls, ' ');
620 // Reasonable default values
626 recordUndo(bv, Undo::ATOMIC);
627 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
631 case LFUN_SPACE_INSERT:
632 case LFUN_MATH_SPACE:
633 recordUndo(bv, Undo::ATOMIC);
634 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
638 bv->owner()->message(_("Invalid action in math mode!"));
642 case LFUN_EXEC_COMMAND:
643 result = DispatchResult(false);
647 // interpret this as if a backslash was typed
648 recordUndo(bv, Undo::ATOMIC);
649 mathcursor->interpret('\\');
652 case LFUN_BREAKPARAGRAPH:
653 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
654 case LFUN_BREAKPARAGRAPH_SKIP:
658 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
659 // handling such that "self-insert" works on "arbitrary stuff" too, and
660 // math-insert only handles special math things like "matrix".
661 case LFUN_INSERT_MATH:
662 recordUndo(bv, Undo::ATOMIC);
663 mathcursor->niceInsert(argument);
667 case LFUN_SELFINSERT:
668 if (!argument.empty()) {
669 recordUndo(bv, Undo::ATOMIC);
670 if (argument.size() == 1)
671 result = mathcursor->interpret(argument[0]) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
673 mathcursor->insert(argument);
678 if (mathcursor->selection())
679 mathcursor->selClear();
681 result = DispatchResult(false);
684 case LFUN_INSET_TOGGLE:
685 mathcursor->insetToggle();
688 case LFUN_DIALOG_SHOW:
689 result = DispatchResult(false);
692 case LFUN_DIALOG_SHOW_NEW_INSET: {
693 string const & name = argument;
697 data = tmp.createDialogStr(name);
701 result = DispatchResult(false);
703 bv->owner()->getDialogs().show(name, data, 0);
707 case LFUN_INSET_APPLY: {
708 string const name = cmd.getArg(0);
710 bv->owner()->getDialogs().getOpenInset(name);
713 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
714 result = base->dispatch(fr);
717 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
718 mathcursor->insert(ar);
719 result = DispatchResult(true, true);
721 result = DispatchResult(false);
728 result = DispatchResult(false);
731 if (result == DispatchResult(true, true))
734 mathcursor->normalize();
737 BOOST_ASSERT(mathcursor);
739 if (mathcursor->selection() || was_selection)
740 toggleInsetSelection(bv);
742 if (result.dispatched()) {
744 cmd.view()->stuffClipboard(mathcursor->grabSelection());
746 releaseMathCursor(bv);
748 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
751 return result; // original version
755 void InsetFormulaBase::revealCodes(BufferView * bv) const
759 bv->owner()->message(mathcursor->info());
762 // write something to the minibuffer
763 // translate to latex
764 mathcursor->markInsert();
767 string str = os.str();
768 mathcursor->markErase();
769 string::size_type pos = 0;
771 for (string::iterator it = str.begin(); it != str.end(); ++it) {
774 else if (*it == '\0') {
776 pos = it - str.begin();
782 res = res.substr(pos - 30);
784 res = res.substr(0, 60);
785 bv->owner()->message(res);
790 InsetOld::Code InsetFormulaBase::lyxCode() const
792 return InsetOld::MATH_CODE;
796 int InsetFormulaBase::ylow() const
798 return yo_ - dim_.asc;
802 int InsetFormulaBase::yhigh() const
804 return yo_ + dim_.des;
808 int InsetFormulaBase::xlow() const
814 int InsetFormulaBase::xhigh() const
816 return xo_ + dim_.wid;
820 /////////////////////////////////////////////////////////////////////
823 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
829 static InsetFormulaBase * lastformula = 0;
830 static MathIterator current = MathIterator(ibegin(par().nucleus()));
832 static string laststr;
834 if (lastformula != this || laststr != str) {
835 //lyxerr << "reset lastformula to " << this << endl;
838 current = ibegin(par().nucleus());
840 mathed_parse_cell(ar, str);
844 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
846 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
847 if (it.cell().matchpart(ar, it.back().pos_)) {
849 mathcursor = new MathCursor(this, true);
851 mathcursor->setSelection(it, ar.size());
859 //lyxerr << "not found!" << endl;
865 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
868 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
869 return searchForward(bv, what, a, b);
873 bool InsetFormulaBase::display() const
875 return par()->asHullInset() && par()->asHullInset()->display();
879 string InsetFormulaBase::selectionAsString() const
881 return mathcursor ? mathcursor->grabSelection() : string();
884 /////////////////////////////////////////////////////////////////////
887 void mathDispatchCreation(FuncRequest const & cmd, bool display)
889 BufferView * bv = cmd.view();
890 // use selection if available..
892 //if (action == LFUN_MATH_IMPORT_SELECTION)
896 string sel = bv->getLyXText()->selectionAsString(*bv->buffer(), false);
899 InsetFormula * f = new InsetFormula(bv);
900 if (openNewInset(bv, f)) {
901 bv->cursor().innerInset()->
902 dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
903 // don't do that also for LFUN_MATH_MODE unless you want end up with
904 // always changing to mathrm when opening an inlined inset
905 // -- I really hate "LyXfunc overloading"...
907 f->dispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
908 f->dispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
911 // create a macro if we see "\\newcommand" somewhere, and an ordinary
913 InsetFormulaBase * f;
914 if (sel.find("\\newcommand") == string::npos &&
915 sel.find("\\def") == string::npos)
916 f = new InsetFormula(sel);
918 f = new InsetFormulaMacro(sel);
919 bv->getLyXText()->cutSelection(true, false);
922 cmd.message(N_("Math editor mode"));
926 void mathDispatch(FuncRequest const & cmd)
928 BufferView * bv = cmd.view();
929 if (!bv->available())
932 switch (cmd.action) {
934 case LFUN_MATH_DISPLAY:
935 mathDispatchCreation(cmd, true);
939 mathDispatchCreation(cmd, false);
942 case LFUN_MATH_IMPORT_SELECTION:
943 mathDispatchCreation(cmd, false);
946 case LFUN_MATH_MACRO:
947 if (cmd.argument.empty())
948 cmd.errorMessage(N_("Missing argument"));
950 string s = cmd.argument;
951 string const s1 = token(s, ' ', 1);
952 int const nargs = s1.empty() ? 0 : atoi(s1);
953 string const s2 = token(s, ' ', 2);
954 string const type = s2.empty() ? "newcommand" : s2;
955 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
959 case LFUN_INSERT_MATH:
960 case LFUN_INSERT_MATRIX:
961 case LFUN_MATH_DELIM: {
962 InsetFormula * f = new InsetFormula(bv);
963 if (openNewInset(bv, f)) {
964 UpdatableInset * inset = bv->cursor().innerInset();
965 inset->dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
966 inset->dispatch(cmd);