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, 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) ?
403 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) ?
414 DispatchResult(true, true) : DispatchResult(false, FINISHED);
418 sel = true; // fall through
420 result = mathcursor->up(sel) ?
421 DispatchResult(true, true) : DispatchResult(false, FINISHED_UP);
425 sel = true; // fall through
427 result = mathcursor->down(sel) ?
428 DispatchResult(true, true) : DispatchResult(false, FINISHED_DOWN);
432 mathcursor->home(false);
433 mathcursor->end(true);
436 case LFUN_UP_PARAGRAPHSEL:
437 case LFUN_UP_PARAGRAPH:
438 case LFUN_DOWN_PARAGRAPHSEL:
439 case LFUN_DOWN_PARAGRAPH:
440 result = DispatchResult(true, FINISHED);
444 case LFUN_WORDLEFTSEL:
445 sel = true; // fall through
448 result = mathcursor->home(sel) ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
452 case LFUN_WORDRIGHTSEL:
453 sel = true; // fall through
456 result = mathcursor->end(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
461 case LFUN_BEGINNINGBUFSEL:
462 case LFUN_BEGINNINGBUF:
463 result = DispatchResult(true, FINISHED);
470 result = DispatchResult(false, FINISHED_RIGHT);
473 case LFUN_CELL_FORWARD:
474 mathcursor->idxNext();
477 case LFUN_CELL_BACKWARD:
478 mathcursor->idxPrev();
481 case LFUN_DELETE_WORD_BACKWARD:
483 recordUndo(bv, Undo::ATOMIC);
484 if (!mathcursor->backspace()) {
485 result = DispatchResult(true, FINISHED);
490 case LFUN_DELETE_WORD_FORWARD:
492 recordUndo(bv, Undo::ATOMIC);
493 if (!mathcursor->erase()) {
494 result = DispatchResult(true, FINISHED);
500 // sprintf(dispatch_buffer, "%d %d",);
501 // DispatchResult= dispatch_buffer;
504 lyxerr << "LFUN_SETXY broken!" << endl;
507 istringstream is(cmd.argument.c_str());
509 mathcursor->setPos(x, y);
515 istringstream is(cmd.argument.c_str());
518 mathcursor->macroModeClose();
519 recordUndo(bv, Undo::ATOMIC);
520 mathcursor->selPaste(n);
525 recordUndo(bv, Undo::DELETE);
526 mathcursor->selCut();
530 mathcursor->selCopy();
534 // Special casing for superscript in case of LyX handling
536 case LFUN_CIRCUMFLEX:
537 if (cmd.argument.empty()) {
538 // do superscript if LyX handles
540 recordUndo(bv, Undo::ATOMIC);
541 mathcursor->script(true);
558 case LFUN_HUNG_UMLAUT:
562 case LFUN_FREEFONT_APPLY:
563 case LFUN_FREEFONT_UPDATE:
564 handleFont2(bv, cmd.argument);
567 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
568 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
569 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
570 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
571 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
572 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
573 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
574 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
575 //case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
576 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
579 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
580 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
582 handleFont(bv, cmd.argument, "textrm");
583 //bv->owner()->message(_("math text mode toggled"));
589 recordUndo(bv, Undo::ATOMIC);
590 mathcursor->setSize(arg);
595 case LFUN_INSERT_MATRIX: {
596 recordUndo(bv, Undo::ATOMIC);
601 istringstream is(argument);
602 is >> m >> n >> v_align >> h_align;
606 mathcursor->niceInsert(
607 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
611 case LFUN_MATH_DELIM: {
612 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
614 string rs = split(cmd.argument, ls, ' ');
615 // Reasonable default values
621 recordUndo(bv, Undo::ATOMIC);
622 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
626 case LFUN_SPACE_INSERT:
627 case LFUN_MATH_SPACE:
628 recordUndo(bv, Undo::ATOMIC);
629 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
633 bv->owner()->message(_("Invalid action in math mode!"));
637 case LFUN_EXEC_COMMAND:
638 result = DispatchResult(false);
642 // interpret this as if a backslash was typed
643 recordUndo(bv, Undo::ATOMIC);
644 mathcursor->interpret('\\');
647 case LFUN_BREAKPARAGRAPH:
648 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
649 case LFUN_BREAKPARAGRAPH_SKIP:
653 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
654 // handling such that "self-insert" works on "arbitrary stuff" too, and
655 // math-insert only handles special math things like "matrix".
656 case LFUN_INSERT_MATH:
657 recordUndo(bv, Undo::ATOMIC);
658 mathcursor->niceInsert(argument);
662 case LFUN_SELFINSERT:
663 if (!argument.empty()) {
664 recordUndo(bv, Undo::ATOMIC);
665 if (argument.size() == 1)
666 result = mathcursor->interpret(argument[0]) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
668 mathcursor->insert(argument);
673 if (mathcursor->selection())
674 mathcursor->selClear();
676 result = DispatchResult(false);
679 case LFUN_INSET_TOGGLE:
680 mathcursor->insetToggle();
683 case LFUN_DIALOG_SHOW:
684 result = DispatchResult(false);
687 case LFUN_DIALOG_SHOW_NEW_INSET: {
688 string const & name = argument;
692 data = tmp.createDialogStr(name);
696 result = DispatchResult(false);
698 bv->owner()->getDialogs().show(name, data, 0);
702 case LFUN_INSET_APPLY: {
703 string const name = cmd.getArg(0);
705 bv->owner()->getDialogs().getOpenInset(name);
708 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
709 result = base->dispatch(fr);
712 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
713 mathcursor->insert(ar);
714 result = DispatchResult(true, true);
716 result = DispatchResult(false);
722 case LFUN_WORD_REPLACE:
723 case LFUN_WORD_FIND: {
725 searchForward(cmd.view(), cmd.getArg(0), false, false)
726 ? DispatchResult(true, true) : DispatchResult(false);
731 result = DispatchResult(false);
734 if (result == DispatchResult(true, true))
737 mathcursor->normalize();
740 BOOST_ASSERT(mathcursor);
742 if (mathcursor->selection() || was_selection)
743 toggleInsetSelection(bv);
745 if (result.dispatched()) {
747 cmd.view()->stuffClipboard(mathcursor->grabSelection());
749 releaseMathCursor(bv);
751 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
754 return result; // original version
758 void InsetFormulaBase::revealCodes(BufferView * bv) const
762 bv->owner()->message(mathcursor->info());
765 // write something to the minibuffer
766 // translate to latex
767 mathcursor->markInsert();
770 string str = os.str();
771 mathcursor->markErase();
772 string::size_type pos = 0;
774 for (string::iterator it = str.begin(); it != str.end(); ++it) {
777 else if (*it == '\0') {
779 pos = it - str.begin();
785 res = res.substr(pos - 30);
787 res = res.substr(0, 60);
788 bv->owner()->message(res);
793 InsetOld::Code InsetFormulaBase::lyxCode() const
795 return InsetOld::MATH_CODE;
799 int InsetFormulaBase::ylow() const
801 return yo_ - dim_.asc;
805 int InsetFormulaBase::yhigh() const
807 return yo_ + dim_.des;
811 int InsetFormulaBase::xlow() const
817 int InsetFormulaBase::xhigh() const
819 return xo_ + dim_.wid;
823 /////////////////////////////////////////////////////////////////////
826 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
832 static InsetFormulaBase * lastformula = 0;
833 static MathIterator current = MathIterator(ibegin(par().nucleus()));
835 static string laststr;
837 if (lastformula != this || laststr != str) {
838 //lyxerr << "reset lastformula to " << this << endl;
841 current = ibegin(par().nucleus());
843 mathed_parse_cell(ar, str);
847 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
849 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
850 if (it.cell().matchpart(ar, it.back().pos_)) {
852 mathcursor = new MathCursor(this, true);
854 mathcursor->setSelection(it, ar.size());
862 //lyxerr << "not found!" << endl;
868 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
871 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
872 return searchForward(bv, what, a, b);
876 bool InsetFormulaBase::display() const
878 return par()->asHullInset() && par()->asHullInset()->display();
882 string InsetFormulaBase::selectionAsString() const
884 return mathcursor ? mathcursor->grabSelection() : string();
887 /////////////////////////////////////////////////////////////////////
890 void mathDispatchCreation(FuncRequest const & cmd, bool display)
892 BufferView * bv = cmd.view();
893 // use selection if available..
895 //if (action == LFUN_MATH_IMPORT_SELECTION)
899 string sel = bv->getLyXText()->selectionAsString(*bv->buffer(), false);
902 InsetFormula * f = new InsetFormula(bv);
903 if (openNewInset(bv, f)) {
904 bv->cursor().innerInset()->
905 dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
906 // don't do that also for LFUN_MATH_MODE unless you want end up with
907 // always changing to mathrm when opening an inlined inset
908 // -- I really hate "LyXfunc overloading"...
910 f->dispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
911 f->dispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
914 // create a macro if we see "\\newcommand" somewhere, and an ordinary
916 InsetFormulaBase * f;
917 if (sel.find("\\newcommand") == string::npos &&
918 sel.find("\\def") == string::npos)
919 f = new InsetFormula(sel);
921 f = new InsetFormulaMacro(sel);
922 bv->getLyXText()->cutSelection(true, false);
925 cmd.message(N_("Math editor mode"));
929 void mathDispatch(FuncRequest const & cmd)
931 BufferView * bv = cmd.view();
932 if (!bv->available())
935 switch (cmd.action) {
937 case LFUN_MATH_DISPLAY:
938 mathDispatchCreation(cmd, true);
942 mathDispatchCreation(cmd, false);
945 case LFUN_MATH_IMPORT_SELECTION:
946 mathDispatchCreation(cmd, false);
949 case LFUN_MATH_MACRO:
950 if (cmd.argument.empty())
951 cmd.errorMessage(N_("Missing argument"));
953 string s = cmd.argument;
954 string const s1 = token(s, ' ', 1);
955 int const nargs = s1.empty() ? 0 : atoi(s1);
956 string const s2 = token(s, ' ', 2);
957 string const type = s2.empty() ? "newcommand" : s2;
958 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
962 case LFUN_INSERT_MATH:
963 case LFUN_INSERT_MATRIX:
964 case LFUN_MATH_DELIM: {
965 InsetFormula * f = new InsetFormula(bv);
966 if (openNewInset(bv, f)) {
967 UpdatableInset * inset = bv->cursor().innerInset();
968 inset->dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
969 inset->dispatch(cmd);