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(BufferView *, 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(BufferView * bv,
190 int & asc, int & desc) const
196 //math_font_max_dim(font_, asc, des);
200 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
207 DispatchResult InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
210 return DispatchResult(false);
212 BufferView * bv = cmd.view();
214 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
216 if (cmd.button() == mouse_button::button3) {
217 // try to dispatch to enclosed insets first
218 if (!mathcursor->dispatch(cmd).dispatched()) {
219 // launch math panel for right mouse button
220 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
221 bv->owner()->getDialogs().show("mathpanel");
223 return DispatchResult(true, true);
226 if (cmd.button() == mouse_button::button2) {
228 asArray(bv->getClipboard(), ar);
229 mathcursor->selClear();
230 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
231 mathcursor->insert(ar);
233 return DispatchResult(true, true);
236 if (cmd.button() == mouse_button::button1) {
237 // try to dispatch to enclosed insets first
238 mathcursor->dispatch(cmd);
239 cmd.view()->stuffClipboard(mathcursor->grabSelection());
240 // try to set the cursor
242 //mathcursor = new MathCursor(this, x == 0);
244 //mathcursor->setPos(x + xo_, y + yo_);
245 return DispatchResult(true, true);
248 return DispatchResult(false);
252 DispatchResult InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
254 BufferView * bv = cmd.view();
255 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
257 if (!mathcursor || mathcursor->formula() != this) {
258 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
259 releaseMathCursor(bv);
260 mathcursor = new MathCursor(this, cmd.x == 0);
262 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
265 if (cmd.button() == mouse_button::button3) {
266 mathcursor->dispatch(cmd);
267 return DispatchResult(true, true);
270 if (cmd.button() == mouse_button::button1) {
273 mathcursor->selClear();
274 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
275 mathcursor->dispatch(cmd);
276 return DispatchResult(true, true);
280 return DispatchResult(true, true);
284 DispatchResult InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
287 return DispatchResult(true, true);
289 if (mathcursor->dispatch(FuncRequest(cmd)).dispatched())
290 return DispatchResult(true, true);
292 // only select with button 1
293 if (cmd.button() != mouse_button::button1)
294 return DispatchResult(true, true);
296 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
297 return DispatchResult(true, true);
302 if (!mathcursor->selection())
303 mathcursor->selStart();
305 BufferView * bv = cmd.view();
306 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
308 return DispatchResult(true, true);
312 void InsetFormulaBase::edit(BufferView * bv, bool left)
314 lyxerr << "Called FormulaBase::edit" << endl;
315 releaseMathCursor(bv);
316 mathcursor = new MathCursor(this, left);
317 bv->cursor().push(this);
318 // if that is removed, we won't get the magenta box when entering an
319 // inset for the first time
324 void InsetFormulaBase::edit(BufferView * bv, int x, int y)
326 lyxerr << "Called FormulaBase::EDIT with '" << x << ' ' << y << "'" << endl;
327 releaseMathCursor(bv);
328 mathcursor = new MathCursor(this, true);
330 mathcursor->setPos(x + xo_, y + yo_);
331 bv->cursor().push(this);
332 // if that is removed, we won't get the magenta box when entering an
333 // inset for the first time
339 InsetFormulaBase::priv_dispatch(FuncRequest const & cmd,
340 idx_type &, pos_type &)
342 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
343 // << " arg: '" << cmd.argument
344 // << "' x: '" << cmd.x
345 // << " y: '" << cmd.y
346 // << "' button: " << cmd.button() << endl;
348 BufferView * bv = cmd.view();
350 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
351 bool remove_inset = false;
353 switch (cmd.action) {
354 case LFUN_MOUSE_PRESS:
355 //lyxerr << "Mouse single press" << endl;
356 return lfunMousePress(cmd);
357 case LFUN_MOUSE_MOTION:
358 //lyxerr << "Mouse motion" << endl;
359 return lfunMouseMotion(cmd);
360 case LFUN_MOUSE_RELEASE:
361 //lyxerr << "Mouse single release" << endl;
362 return lfunMouseRelease(cmd);
363 case LFUN_MOUSE_DOUBLE:
364 //lyxerr << "Mouse double" << endl;
365 return dispatch(FuncRequest(LFUN_WORDSEL));
371 return DispatchResult(false);
373 string argument = cmd.argument;
374 DispatchResult result(true);
376 bool was_macro = mathcursor->inMacroMode();
377 bool was_selection = mathcursor->selection();
379 mathcursor->normalize();
382 switch (cmd.action) {
384 case LFUN_MATH_MUTATE:
385 case LFUN_MATH_DISPLAY:
386 case LFUN_MATH_NUMBER:
387 case LFUN_MATH_NONUMBER:
388 case LFUN_CELL_SPLIT:
390 case LFUN_DELETE_LINE_FORWARD:
391 case LFUN_INSERT_LABEL:
392 case LFUN_MATH_EXTERN:
393 case LFUN_TABULAR_FEATURE:
394 case LFUN_PASTESELECTION:
395 case LFUN_MATH_LIMITS:
396 recordUndo(bv, Undo::ATOMIC);
397 mathcursor->dispatch(cmd);
401 sel = true; // fall through...
403 result = mathcursor->right(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
404 //lyxerr << "calling scroll 20" << endl;
406 // write something to the minibuffer
407 //bv->owner()->message(mathcursor->info());
411 sel = true; // fall through
413 result = mathcursor->left(sel) ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
417 sel = true; // fall through
419 result = mathcursor->up(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_UP);
423 sel = true; // fall through
425 result = mathcursor->down(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_DOWN);
429 mathcursor->home(false);
430 mathcursor->end(true);
433 case LFUN_UP_PARAGRAPHSEL:
434 case LFUN_UP_PARAGRAPH:
435 case LFUN_DOWN_PARAGRAPHSEL:
436 case LFUN_DOWN_PARAGRAPH:
437 result = DispatchResult(true, FINISHED);
441 case LFUN_WORDLEFTSEL:
442 sel = true; // fall through
445 result = mathcursor->home(sel) ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
449 case LFUN_WORDRIGHTSEL:
450 sel = true; // fall through
453 result = mathcursor->end(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
458 case LFUN_BEGINNINGBUFSEL:
459 case LFUN_BEGINNINGBUF:
460 result = DispatchResult(true, FINISHED);
467 result = DispatchResult(false, FINISHED_RIGHT);
470 case LFUN_CELL_FORWARD:
471 mathcursor->idxNext();
474 case LFUN_CELL_BACKWARD:
475 mathcursor->idxPrev();
478 case LFUN_DELETE_WORD_BACKWARD:
480 recordUndo(bv, Undo::ATOMIC);
481 if (!mathcursor->backspace()) {
482 result = DispatchResult(true, FINISHED);
487 case LFUN_DELETE_WORD_FORWARD:
489 recordUndo(bv, Undo::ATOMIC);
490 if (!mathcursor->erase()) {
491 result = DispatchResult(true, FINISHED);
497 // sprintf(dispatch_buffer, "%d %d",);
498 // DispatchResult= dispatch_buffer;
501 lyxerr << "LFUN_SETXY broken!" << endl;
504 istringstream is(cmd.argument.c_str());
506 mathcursor->setPos(x, y);
512 istringstream is(cmd.argument.c_str());
515 mathcursor->macroModeClose();
516 recordUndo(bv, Undo::ATOMIC);
517 mathcursor->selPaste(n);
522 recordUndo(bv, Undo::DELETE);
523 mathcursor->selCut();
527 mathcursor->selCopy();
531 // Special casing for superscript in case of LyX handling
533 case LFUN_CIRCUMFLEX:
534 if (cmd.argument.empty()) {
535 // do superscript if LyX handles
537 recordUndo(bv, Undo::ATOMIC);
538 mathcursor->script(true);
555 case LFUN_HUNG_UMLAUT:
559 case LFUN_FREEFONT_APPLY:
560 case LFUN_FREEFONT_UPDATE:
561 handleFont2(bv, cmd.argument);
564 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
565 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
566 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
567 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
568 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
569 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
570 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
571 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
572 //case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
573 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
576 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
577 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
579 handleFont(bv, cmd.argument, "textrm");
580 //bv->owner()->message(_("math text mode toggled"));
586 recordUndo(bv, Undo::ATOMIC);
587 mathcursor->setSize(arg);
592 case LFUN_INSERT_MATRIX: {
593 recordUndo(bv, Undo::ATOMIC);
598 istringstream is(argument);
599 is >> m >> n >> v_align >> h_align;
603 mathcursor->niceInsert(
604 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
608 case LFUN_SUPERSCRIPT:
611 recordUndo(bv, Undo::ATOMIC);
612 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
616 case LFUN_MATH_DELIM:
618 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
620 string rs = split(cmd.argument, ls, ' ');
621 // Reasonable default values
627 recordUndo(bv, Undo::ATOMIC);
628 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
632 case LFUN_SPACE_INSERT:
633 case LFUN_MATH_SPACE:
634 recordUndo(bv, Undo::ATOMIC);
635 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
639 bv->owner()->message(_("Invalid action in math mode!"));
643 case LFUN_EXEC_COMMAND:
644 result = DispatchResult(false);
648 // interpret this as if a backslash was typed
649 recordUndo(bv, Undo::ATOMIC);
650 mathcursor->interpret('\\');
653 case LFUN_BREAKPARAGRAPH:
654 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
655 case LFUN_BREAKPARAGRAPH_SKIP:
659 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
660 // handling such that "self-insert" works on "arbitrary stuff" too, and
661 // math-insert only handles special math things like "matrix".
662 case LFUN_INSERT_MATH:
663 recordUndo(bv, Undo::ATOMIC);
664 mathcursor->niceInsert(argument);
668 case LFUN_SELFINSERT:
669 if (!argument.empty()) {
670 recordUndo(bv, Undo::ATOMIC);
671 if (argument.size() == 1)
672 result = mathcursor->interpret(argument[0]) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
674 mathcursor->insert(argument);
679 if (mathcursor->selection())
680 mathcursor->selClear();
682 result = DispatchResult(false);
685 case LFUN_INSET_TOGGLE:
686 mathcursor->insetToggle();
689 case LFUN_DIALOG_SHOW:
690 result = DispatchResult(false);
693 case LFUN_DIALOG_SHOW_NEW_INSET: {
694 string const & name = argument;
698 data = tmp.createDialogStr(name);
702 result = DispatchResult(false);
704 bv->owner()->getDialogs().show(name, data, 0);
708 case LFUN_INSET_APPLY: {
709 string const name = cmd.getArg(0);
711 bv->owner()->getDialogs().getOpenInset(name);
714 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
715 result = base->dispatch(fr);
718 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
719 mathcursor->insert(ar);
720 result = DispatchResult(true, true);
722 result = DispatchResult(false);
729 result = DispatchResult(false);
732 if (result == DispatchResult(true, true))
735 mathcursor->normalize();
738 BOOST_ASSERT(mathcursor);
740 if (mathcursor->selection() || was_selection)
741 toggleInsetSelection(bv);
743 if (result.dispatched()) {
745 cmd.view()->stuffClipboard(mathcursor->grabSelection());
747 releaseMathCursor(bv);
749 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
752 return result; // original version
756 void InsetFormulaBase::revealCodes(BufferView * bv) const
760 bv->owner()->message(mathcursor->info());
763 // write something to the minibuffer
764 // translate to latex
765 mathcursor->markInsert();
768 string str = os.str();
769 mathcursor->markErase();
770 string::size_type pos = 0;
772 for (string::iterator it = str.begin(); it != str.end(); ++it) {
775 else if (*it == '\0') {
777 pos = it - str.begin();
783 res = res.substr(pos - 30);
785 res = res.substr(0, 60);
786 bv->owner()->message(res);
791 InsetOld::Code InsetFormulaBase::lyxCode() const
793 return InsetOld::MATH_CODE;
797 int InsetFormulaBase::ylow() const
799 return yo_ - dim_.asc;
803 int InsetFormulaBase::yhigh() const
805 return yo_ + dim_.des;
809 int InsetFormulaBase::xlow() const
815 int InsetFormulaBase::xhigh() const
817 return xo_ + dim_.wid;
821 /////////////////////////////////////////////////////////////////////
824 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
830 static InsetFormulaBase * lastformula = 0;
831 static MathIterator current = MathIterator(ibegin(par().nucleus()));
833 static string laststr;
835 if (lastformula != this || laststr != str) {
836 //lyxerr << "reset lastformula to " << this << endl;
839 current = ibegin(par().nucleus());
841 mathed_parse_cell(ar, str);
845 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
847 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
848 if (it.cell().matchpart(ar, it.back().pos_)) {
850 mathcursor = new MathCursor(this, true);
852 mathcursor->setSelection(it, ar.size());
860 //lyxerr << "not found!" << endl;
866 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
869 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
870 return searchForward(bv, what, a, b);
874 bool InsetFormulaBase::display() const
876 return par()->asHullInset() && par()->asHullInset()->display();
880 string InsetFormulaBase::selectionAsString() const
882 return mathcursor ? mathcursor->grabSelection() : string();
885 /////////////////////////////////////////////////////////////////////
888 void mathDispatchCreation(FuncRequest const & cmd, bool display)
890 BufferView * bv = cmd.view();
891 // use selection if available..
893 //if (action == LFUN_MATH_IMPORT_SELECTION)
897 string sel = bv->getLyXText()->selectionAsString(*bv->buffer(), false);
900 InsetFormula * f = new InsetFormula(bv);
901 if (openNewInset(bv, f)) {
902 bv->cursor().innerInset()->
903 dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
904 // don't do that also for LFUN_MATH_MODE unless you want end up with
905 // always changing to mathrm when opening an inlined inset
906 // -- I really hate "LyXfunc overloading"...
908 f->dispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
909 f->dispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
912 // create a macro if we see "\\newcommand" somewhere, and an ordinary
914 InsetFormulaBase * f;
915 if (sel.find("\\newcommand") == string::npos &&
916 sel.find("\\def") == string::npos)
917 f = new InsetFormula(sel);
919 f = new InsetFormulaMacro(sel);
920 bv->getLyXText()->cutSelection(true, false);
923 cmd.message(N_("Math editor mode"));
927 void mathDispatch(FuncRequest const & cmd)
929 BufferView * bv = cmd.view();
930 if (!bv->available())
933 switch (cmd.action) {
935 case LFUN_MATH_DISPLAY:
936 mathDispatchCreation(cmd, true);
940 mathDispatchCreation(cmd, false);
943 case LFUN_MATH_IMPORT_SELECTION:
944 mathDispatchCreation(cmd, false);
947 case LFUN_MATH_MACRO:
948 if (cmd.argument.empty())
949 cmd.errorMessage(N_("Missing argument"));
951 string s = cmd.argument;
952 string const s1 = token(s, ' ', 1);
953 int const nargs = s1.empty() ? 0 : atoi(s1);
954 string const s2 = token(s, ' ', 2);
955 string const type = s2.empty() ? "newcommand" : s2;
956 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
960 case LFUN_INSERT_MATH:
961 case LFUN_INSERT_MATRIX:
962 case LFUN_MATH_DELIM: {
963 InsetFormula * f = new InsetFormula(bv);
964 if (openNewInset(bv, f)) {
965 UpdatableInset * inset = bv->cursor().innerInset();
966 inset->dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
967 inset->dispatch(cmd);