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;
53 using std::istringstream;
54 using std::ostringstream;
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 (LCursor & cur, string const & arg, string const & font)
116 // this whole function is a hack and won't work for incremental font
118 recordUndo(cur.bv(), Undo::ATOMIC);
120 if (cur.inset()->asMathInset()->name() == font)
121 mathcursor->handleFont(cur, font);
123 mathcursor->handleNest(cur, createMathInset(font));
124 mathcursor->insert(cur, arg);
129 void InsetFormulaBase::handleFont2(LCursor & cur, string const & arg)
131 recordUndo(cur.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(cur, 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(bv.fullCursor()))
158 mathcursor->macroModeClose(bv.fullCursor());
159 releaseMathCursor(bv);
162 generatePreview(*bv.buffer());
167 void InsetFormulaBase::getCursorPos(BufferView & bv, int & x, int & y) const
170 mathcursor->getScreenPos(bv.fullCursor(), x, y);
171 x = mathcursor->targetX(bv.fullCursor());
174 lyxerr << "InsetFormulaBase::getCursorPos: " << x << ' ' << y << endl;
178 lyxerr << "getCursorPos - should not happen";
183 void InsetFormulaBase::getCursorDim(int & asc, int & desc) const
188 //math_font_max_dim(font_, asc, des);
194 InsetFormulaBase::lfunMouseRelease(BufferView & bv, FuncRequest const & cmd)
197 return DispatchResult(false);
199 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
201 if (cmd.button() == mouse_button::button3) {
202 // try to dispatch to enclosed insets first
203 if (!mathcursor->dispatch(bv.fullCursor(), cmd).dispatched()) {
204 // launch math panel for right mouse button
205 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
206 bv.owner()->getDialogs().show("mathpanel");
208 return DispatchResult(true, true);
211 if (cmd.button() == mouse_button::button2) {
213 asArray(bv.getClipboard(), ar);
214 mathcursor->selClear(bv.fullCursor());
215 mathcursor->setScreenPos(bv.fullCursor(), cmd.x + xo_, cmd.y + yo_);
216 mathcursor->insert(bv.fullCursor(), ar);
218 return DispatchResult(true, true);
221 if (cmd.button() == mouse_button::button1) {
222 // try to dispatch to enclosed insets first
223 mathcursor->dispatch(bv.fullCursor(), cmd);
224 bv.stuffClipboard(mathcursor->grabSelection(bv.fullCursor()));
225 // try to set the cursor
227 //mathcursor = new MathCursor(bv, this, x == 0);
229 //mathcursor->setScreenPos(x + xo_, y + yo_);
230 return DispatchResult(true, true);
233 return DispatchResult(false);
238 InsetFormulaBase::lfunMousePress(BufferView & bv, FuncRequest const & cmd)
240 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
242 if (!mathcursor || mathcursor->formula() != this) {
243 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
244 releaseMathCursor(bv);
245 mathcursor = new MathCursor(&bv, this, cmd.x == 0);
247 mathcursor->setScreenPos(bv.fullCursor(), cmd.x + xo_, cmd.y + yo_);
250 if (cmd.button() == mouse_button::button3) {
251 mathcursor->dispatch(bv.fullCursor(), cmd);
252 return DispatchResult(true, true);
255 if (cmd.button() == mouse_button::button1) {
258 mathcursor->selClear(bv.fullCursor());
259 mathcursor->setScreenPos(bv.fullCursor(), cmd.x + xo_, cmd.y + yo_);
260 mathcursor->dispatch(bv.fullCursor(), cmd);
261 return DispatchResult(true, true);
265 return DispatchResult(true, true);
270 InsetFormulaBase::lfunMouseMotion(BufferView & bv, FuncRequest const & cmd)
273 return DispatchResult(true, true);
275 if (mathcursor->dispatch(bv.fullCursor(), FuncRequest(cmd)).dispatched())
276 return DispatchResult(true, true);
278 // only select with button 1
279 if (cmd.button() != mouse_button::button1)
280 return DispatchResult(true, true);
282 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
283 return DispatchResult(true, true);
288 if (!mathcursor->selection())
289 mathcursor->selStart(bv.fullCursor());
291 mathcursor->setScreenPos(bv.fullCursor(), cmd.x + xo_, cmd.y + yo_);
293 return DispatchResult(true, true);
297 void InsetFormulaBase::edit(BufferView * bv, bool left)
299 lyxerr << "Called FormulaBase::edit" << endl;
300 releaseMathCursor(*bv);
301 mathcursor = new MathCursor(bv, this, left);
302 bv->fullCursor().push(this);
303 // if that is removed, we won't get the magenta box when entering an
304 // inset for the first time
309 void InsetFormulaBase::edit(BufferView * bv, int x, int y)
311 lyxerr << "Called FormulaBase::EDIT with '" << x << ' ' << y << "'" << endl;
312 releaseMathCursor(*bv);
313 mathcursor = new MathCursor(bv, this, true);
315 mathcursor->setScreenPos(bv->fullCursor(), x + xo_, y + yo_);
316 bv->fullCursor().push(this);
317 // if that is removed, we won't get the magenta box when entering an
318 // inset for the first time
324 InsetFormulaBase::priv_dispatch(BufferView & bv, 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 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
333 bool remove_inset = false;
334 LCursor & cur = bv.fullCursor();
336 switch (cmd.action) {
337 case LFUN_MOUSE_PRESS:
338 //lyxerr << "Mouse single press" << endl;
339 return lfunMousePress(bv, cmd);
340 case LFUN_MOUSE_MOTION:
341 //lyxerr << "Mouse motion" << endl;
342 return lfunMouseMotion(bv, cmd);
343 case LFUN_MOUSE_RELEASE:
344 //lyxerr << "Mouse single release" << endl;
345 return lfunMouseRelease(bv, cmd);
346 case LFUN_MOUSE_DOUBLE:
347 //lyxerr << "Mouse double" << endl;
348 return dispatch(bv, FuncRequest(LFUN_WORDSEL));
354 return DispatchResult(false);
356 DispatchResult result(true);
357 string argument = cmd.argument;
359 bool was_macro = mathcursor->inMacroMode(cur);
361 mathcursor->normalize(cur);
364 switch (cmd.action) {
366 case LFUN_MATH_MUTATE:
367 case LFUN_MATH_DISPLAY:
368 case LFUN_MATH_NUMBER:
369 case LFUN_MATH_NONUMBER:
370 case LFUN_CELL_SPLIT:
372 case LFUN_DELETE_LINE_FORWARD:
373 case LFUN_INSERT_LABEL:
374 case LFUN_MATH_EXTERN:
375 case LFUN_TABULAR_FEATURE:
376 case LFUN_PASTESELECTION:
377 case LFUN_MATH_LIMITS:
378 recordUndo(bv, Undo::ATOMIC);
379 mathcursor->dispatch(cur, cmd);
383 sel = true; // fall through...
385 result = mathcursor->right(cur, sel) ?
386 DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
387 //lyxerr << "calling scroll 20" << endl;
389 // write something to the minibuffer
390 //bv.owner()->message(mathcursor->info());
394 sel = true; // fall through
396 result = mathcursor->left(cur, sel) ?
397 DispatchResult(true, true) : DispatchResult(false, FINISHED);
401 sel = true; // fall through
403 result = mathcursor->up(cur, sel) ?
404 DispatchResult(true, true) : DispatchResult(false, FINISHED_UP);
408 sel = true; // fall through
410 result = mathcursor->down(cur, sel) ?
411 DispatchResult(true, true) : DispatchResult(false, FINISHED_DOWN);
415 mathcursor->home(cur, false);
416 mathcursor->end(cur, true);
419 case LFUN_UP_PARAGRAPHSEL:
420 case LFUN_UP_PARAGRAPH:
421 case LFUN_DOWN_PARAGRAPHSEL:
422 case LFUN_DOWN_PARAGRAPH:
423 result = DispatchResult(true, FINISHED);
427 case LFUN_WORDLEFTSEL:
428 sel = true; // fall through
431 result = mathcursor->home(cur, sel)
432 ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
436 case LFUN_WORDRIGHTSEL:
437 sel = true; // fall through
440 result = mathcursor->end(cur, sel)
441 ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
446 case LFUN_BEGINNINGBUFSEL:
447 case LFUN_BEGINNINGBUF:
448 result = DispatchResult(true, FINISHED);
455 result = DispatchResult(false, FINISHED_RIGHT);
458 case LFUN_CELL_FORWARD:
459 mathcursor->idxNext(cur);
462 case LFUN_CELL_BACKWARD:
463 mathcursor->idxPrev(cur);
466 case LFUN_DELETE_WORD_BACKWARD:
468 recordUndo(bv, Undo::ATOMIC);
469 if (!mathcursor->backspace(cur)) {
470 result = DispatchResult(true, FINISHED);
475 case LFUN_DELETE_WORD_FORWARD:
477 recordUndo(bv, Undo::ATOMIC);
478 if (!mathcursor->erase(cur)) {
479 result = DispatchResult(true, FINISHED);
485 // sprintf(dispatch_buffer, "%d %d",);
486 // DispatchResult= dispatch_buffer;
489 lyxerr << "LFUN_SETXY broken!" << endl;
492 istringstream is(cmd.argument.c_str());
494 mathcursor->setScreenPos(cur, x, y);
500 istringstream is(cmd.argument.c_str());
503 mathcursor->macroModeClose(cur);
504 recordUndo(bv, Undo::ATOMIC);
505 mathcursor->selPaste(cur, n);
510 recordUndo(bv, Undo::DELETE);
511 mathcursor->selCut(cur);
515 mathcursor->selCopy(cur);
519 // Special casing for superscript in case of LyX handling
521 case LFUN_CIRCUMFLEX:
522 if (cmd.argument.empty()) {
523 // do superscript if LyX handles
525 recordUndo(bv, Undo::ATOMIC);
526 mathcursor->script(cur, true);
543 case LFUN_HUNG_UMLAUT:
547 case LFUN_FREEFONT_APPLY:
548 case LFUN_FREEFONT_UPDATE:
549 handleFont2(cur, cmd.argument);
552 case LFUN_BOLD: handleFont(cur, cmd.argument, "mathbf"); break;
553 case LFUN_SANS: handleFont(cur, cmd.argument, "mathsf"); break;
554 case LFUN_EMPH: handleFont(cur, cmd.argument, "mathcal"); break;
555 case LFUN_ROMAN: handleFont(cur, cmd.argument, "mathrm"); break;
556 case LFUN_CODE: handleFont(cur, cmd.argument, "texttt"); break;
557 case LFUN_FRAK: handleFont(cur, cmd.argument, "mathfrak"); break;
558 case LFUN_ITAL: handleFont(cur, cmd.argument, "mathit"); break;
559 case LFUN_NOUN: handleFont(cur, cmd.argument, "mathbb"); break;
560 //case LFUN_FREEFONT_APPLY: handleFont(cur, cmd.argument, "textrm"); break;
561 case LFUN_DEFAULT: handleFont(cur, cmd.argument, "textnormal"); break;
564 if (mathcursor->currentMode(cur) == MathInset::TEXT_MODE)
565 mathcursor->niceInsert(cur, MathAtom(new MathHullInset("simple")));
567 handleFont(cur, cmd.argument, "textrm");
568 //cur.owner()->message(_("math text mode toggled"));
574 recordUndo(bv, Undo::ATOMIC);
575 mathcursor->setSize(arg);
580 case LFUN_INSERT_MATRIX: {
581 recordUndo(bv, Undo::ATOMIC);
586 istringstream is(argument);
587 is >> m >> n >> v_align >> h_align;
591 mathcursor->niceInsert(cur,
592 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
596 case LFUN_MATH_DELIM: {
597 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
599 string rs = split(cmd.argument, ls, ' ');
600 // Reasonable default values
605 recordUndo(bv, Undo::ATOMIC);
606 mathcursor->handleNest(cur, MathAtom(new MathDelimInset(ls, rs)));
610 case LFUN_SPACE_INSERT:
611 case LFUN_MATH_SPACE:
612 recordUndo(bv, Undo::ATOMIC);
613 mathcursor->insert(cur, MathAtom(new MathSpaceInset(",")));
617 bv.owner()->message(_("Invalid action in math mode!"));
621 case LFUN_EXEC_COMMAND:
622 result = DispatchResult(false);
626 // interpret this as if a backslash was typed
627 recordUndo(bv, Undo::ATOMIC);
628 mathcursor->interpret(cur, '\\');
631 case LFUN_BREAKPARAGRAPH:
632 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
633 case LFUN_BREAKPARAGRAPH_SKIP:
637 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
638 // handling such that "self-insert" works on "arbitrary stuff" too, and
639 // math-insert only handles special math things like "matrix".
640 case LFUN_INSERT_MATH:
641 recordUndo(bv, Undo::ATOMIC);
642 mathcursor->niceInsert(cur, argument);
646 case LFUN_SELFINSERT:
647 if (!argument.empty()) {
648 recordUndo(bv, Undo::ATOMIC);
649 if (argument.size() == 1)
650 result = mathcursor->interpret(cur, argument[0])
651 ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
653 mathcursor->insert(cur, argument);
658 if (mathcursor->selection())
659 mathcursor->selClear(cur);
661 result = DispatchResult(false);
664 case LFUN_INSET_TOGGLE:
665 mathcursor->insetToggle(cur);
668 case LFUN_DIALOG_SHOW:
669 result = DispatchResult(false);
672 case LFUN_DIALOG_SHOW_NEW_INSET: {
673 string const & name = argument;
677 data = tmp.createDialogStr(name);
681 result = DispatchResult(false);
683 bv.owner()->getDialogs().show(name, data, 0);
687 case LFUN_INSET_APPLY: {
688 string const name = cmd.getArg(0);
689 InsetBase * base = bv.owner()->getDialogs().getOpenInset(name);
692 FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
693 result = base->dispatch(bv, fr);
696 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
697 mathcursor->insert(cur, ar);
698 result = DispatchResult(true, true);
700 result = DispatchResult(false);
706 case LFUN_WORD_REPLACE:
707 case LFUN_WORD_FIND: {
709 searchForward(&bv, cmd.getArg(0), false, false)
710 ? DispatchResult(true, true) : DispatchResult(false);
715 result = DispatchResult(false);
718 if (result == DispatchResult(true, true))
721 mathcursor->normalize(cur);
724 BOOST_ASSERT(mathcursor);
726 if (result.dispatched()) {
728 bv.stuffClipboard(mathcursor->grabSelection(cur));
730 releaseMathCursor(bv);
732 bv.owner()->dispatch(FuncRequest(LFUN_DELETE));
735 return result; // original version
739 void InsetFormulaBase::revealCodes(BufferView & bv) const
743 bv.owner()->message(mathcursor->info(bv.fullCursor()));
745 // write something to the minibuffer
746 // translate to latex
747 mathcursor->markInsert(bv);
750 string str = os.str();
751 mathcursor->markErase(bv);
752 string::size_type pos = 0;
754 for (string::iterator it = str.begin(); it != str.end(); ++it) {
757 else if (*it == '\0') {
759 pos = it - str.begin();
765 res = res.substr(pos - 30);
767 res = res.substr(0, 60);
768 bv.owner()->message(res);
773 InsetOld::Code InsetFormulaBase::lyxCode() const
775 return InsetOld::MATH_CODE;
779 int InsetFormulaBase::ylow() const
781 return yo_ - dim_.asc;
785 int InsetFormulaBase::yhigh() const
787 return yo_ + dim_.des;
791 int InsetFormulaBase::xlow() const
797 int InsetFormulaBase::xhigh() const
799 return xo_ + dim_.wid;
803 /////////////////////////////////////////////////////////////////////
806 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
812 static InsetFormulaBase * lastformula = 0;
813 static CursorBase current = CursorBase(ibegin(par().nucleus()));
815 static string laststr;
817 if (lastformula != this || laststr != str) {
818 //lyxerr << "reset lastformula to " << this << endl;
821 current = ibegin(par().nucleus());
823 mathed_parse_cell(ar, str);
827 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
829 for (CursorBase it = current; it != iend(par().nucleus()); increment(it)) {
830 CursorSlice & top = it.back();
831 MathArray const & a = top.asMathInset()->cell(top.idx_);
832 if (a.matchpart(ar, top.pos_)) {
834 mathcursor = new MathCursor(bv, this, true);
836 mathcursor->setSelection(bv->fullCursor(), it, ar.size());
838 top.pos_ += ar.size();
844 //lyxerr << "not found!" << endl;
850 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
853 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
854 return searchForward(bv, what, a, b);
858 bool InsetFormulaBase::display() const
860 return par()->asHullInset() && par()->asHullInset()->display();
864 string InsetFormulaBase::selectionAsString(BufferView & bv) const
866 return mathcursor ? mathcursor->grabSelection(bv.fullCursor()) : string();
869 /////////////////////////////////////////////////////////////////////
872 void mathDispatchCreation(BufferView & bv, FuncRequest const & cmd,
875 // use selection if available..
877 //if (action == LFUN_MATH_IMPORT_SELECTION)
881 string sel = bv.getLyXText()->selectionAsString(*bv.buffer(), false);
884 InsetFormula * f = new InsetFormula(&bv);
885 if (openNewInset(bv, f)) {
886 bv.fullCursor().innerInset()->
887 dispatch(bv, FuncRequest(LFUN_MATH_MUTATE, "simple"));
888 // don't do that also for LFUN_MATH_MODE unless you want end up with
889 // always changing to mathrm when opening an inlined inset
890 // -- I really hate "LyXfunc overloading"...
892 f->dispatch(bv, FuncRequest(LFUN_MATH_DISPLAY));
893 f->dispatch(bv, FuncRequest(LFUN_INSERT_MATH, cmd.argument));
896 // create a macro if we see "\\newcommand" somewhere, and an ordinary
898 InsetFormulaBase * f;
899 if (sel.find("\\newcommand") == string::npos &&
900 sel.find("\\def") == string::npos)
901 f = new InsetFormula(sel);
903 f = new InsetFormulaMacro(sel);
904 bv.getLyXText()->cutSelection(true, false);
907 cmd.message(N_("Math editor mode"));
911 void mathDispatch(BufferView & bv, FuncRequest const & cmd)
916 switch (cmd.action) {
918 case LFUN_MATH_DISPLAY:
919 mathDispatchCreation(bv, cmd, true);
923 mathDispatchCreation(bv, cmd, false);
926 case LFUN_MATH_IMPORT_SELECTION:
927 mathDispatchCreation(bv, cmd, false);
930 case LFUN_MATH_MACRO:
931 if (cmd.argument.empty())
932 cmd.errorMessage(N_("Missing argument"));
934 string s = cmd.argument;
935 string const s1 = token(s, ' ', 1);
936 int const nargs = s1.empty() ? 0 : atoi(s1);
937 string const s2 = token(s, ' ', 2);
938 string const type = s2.empty() ? "newcommand" : s2;
939 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
943 case LFUN_INSERT_MATH:
944 case LFUN_INSERT_MATRIX:
945 case LFUN_MATH_DELIM: {
946 InsetFormula * f = new InsetFormula(&bv);
947 if (openNewInset(bv, f)) {
948 UpdatableInset * inset = bv.fullCursor().innerInset();
949 inset->dispatch(bv, FuncRequest(LFUN_MATH_MUTATE, "simple"));
950 inset->dispatch(bv, cmd);