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->getScreenPos(x, y);
173 void InsetFormulaBase::getCursorPos(int, int & x, int & y) const
176 mathcursor->getScreenPos(x, y);
177 x = mathcursor->targetX();
180 lyxerr << "InsetFormulaBase::getCursorPos: " << x << ' ' << y << endl;
184 lyxerr << "getCursorPos - should not happen";
189 void InsetFormulaBase::getCursorDim(int & asc, int & desc) const
194 //math_font_max_dim(font_, asc, des);
199 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
206 DispatchResult InsetFormulaBase::lfunMouseRelease(
207 BufferView & bv, FuncRequest const & cmd)
210 return DispatchResult(false);
212 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
214 if (cmd.button() == mouse_button::button3) {
215 // try to dispatch to enclosed insets first
216 if (!mathcursor->dispatch(cmd).dispatched()) {
217 // launch math panel for right mouse button
218 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
219 bv.owner()->getDialogs().show("mathpanel");
221 return DispatchResult(true, true);
224 if (cmd.button() == mouse_button::button2) {
226 asArray(bv.getClipboard(), ar);
227 mathcursor->selClear();
228 mathcursor->setScreenPos(cmd.x + xo_, cmd.y + yo_);
229 mathcursor->insert(ar);
231 return DispatchResult(true, true);
234 if (cmd.button() == mouse_button::button1) {
235 // try to dispatch to enclosed insets first
236 mathcursor->dispatch(cmd);
237 bv.stuffClipboard(mathcursor->grabSelection());
238 // try to set the cursor
240 //mathcursor = new MathCursor(bv, this, x == 0);
242 //mathcursor->setScreenPos(x + xo_, y + yo_);
243 return DispatchResult(true, true);
246 return DispatchResult(false);
250 DispatchResult InsetFormulaBase::lfunMousePress(
251 BufferView & bv, FuncRequest const & cmd)
253 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
255 if (!mathcursor || mathcursor->formula() != this) {
256 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
257 releaseMathCursor(bv);
258 mathcursor = new MathCursor(&bv, this, cmd.x == 0);
260 mathcursor->setScreenPos(cmd.x + xo_, cmd.y + yo_);
263 if (cmd.button() == mouse_button::button3) {
264 mathcursor->dispatch(cmd);
265 return DispatchResult(true, true);
268 if (cmd.button() == mouse_button::button1) {
271 mathcursor->selClear();
272 mathcursor->setScreenPos(cmd.x + xo_, cmd.y + yo_);
273 mathcursor->dispatch(cmd);
274 return DispatchResult(true, true);
278 return DispatchResult(true, true);
282 DispatchResult InsetFormulaBase::lfunMouseMotion(
283 BufferView & bv, 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 mathcursor->setScreenPos(cmd.x + xo_, cmd.y + yo_);
306 return DispatchResult(true, true);
310 void InsetFormulaBase::edit(BufferView * bv, bool left)
312 lyxerr << "Called FormulaBase::edit" << endl;
313 releaseMathCursor(*bv);
314 mathcursor = new MathCursor(bv, this, left);
315 bv->fullCursor().push(this);
316 // if that is removed, we won't get the magenta box when entering an
317 // inset for the first time
322 void InsetFormulaBase::edit(BufferView * bv, int x, int y)
324 lyxerr << "Called FormulaBase::EDIT with '" << x << ' ' << y << "'" << endl;
325 releaseMathCursor(*bv);
326 mathcursor = new MathCursor(bv, this, true);
328 mathcursor->setScreenPos(x + xo_, y + yo_);
329 bv->fullCursor().push(this);
330 // if that is removed, we won't get the magenta box when entering an
331 // inset for the first time
337 InsetFormulaBase::priv_dispatch(BufferView & bv, FuncRequest const & cmd)
339 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
340 // << " arg: '" << cmd.argument
341 // << "' x: '" << cmd.x
342 // << " y: '" << cmd.y
343 // << "' button: " << cmd.button() << endl;
345 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
346 bool remove_inset = false;
348 switch (cmd.action) {
349 case LFUN_MOUSE_PRESS:
350 //lyxerr << "Mouse single press" << endl;
351 return lfunMousePress(bv, cmd);
352 case LFUN_MOUSE_MOTION:
353 //lyxerr << "Mouse motion" << endl;
354 return lfunMouseMotion(bv, cmd);
355 case LFUN_MOUSE_RELEASE:
356 //lyxerr << "Mouse single release" << endl;
357 return lfunMouseRelease(bv, cmd);
358 case LFUN_MOUSE_DOUBLE:
359 //lyxerr << "Mouse double" << endl;
360 return dispatch(bv, FuncRequest(LFUN_WORDSEL));
366 return DispatchResult(false);
368 string argument = cmd.argument;
369 DispatchResult result(true);
371 bool was_macro = mathcursor->inMacroMode();
372 bool was_selection = mathcursor->selection();
374 mathcursor->normalize();
377 switch (cmd.action) {
379 case LFUN_MATH_MUTATE:
380 case LFUN_MATH_DISPLAY:
381 case LFUN_MATH_NUMBER:
382 case LFUN_MATH_NONUMBER:
383 case LFUN_CELL_SPLIT:
385 case LFUN_DELETE_LINE_FORWARD:
386 case LFUN_INSERT_LABEL:
387 case LFUN_MATH_EXTERN:
388 case LFUN_TABULAR_FEATURE:
389 case LFUN_PASTESELECTION:
390 case LFUN_MATH_LIMITS:
391 recordUndo(bv, Undo::ATOMIC);
392 mathcursor->dispatch(cmd);
396 sel = true; // fall through...
398 result = mathcursor->right(sel) ?
399 DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
400 //lyxerr << "calling scroll 20" << endl;
402 // write something to the minibuffer
403 //bv.owner()->message(mathcursor->info());
407 sel = true; // fall through
409 result = mathcursor->left(sel) ?
410 DispatchResult(true, true) : DispatchResult(false, FINISHED);
414 sel = true; // fall through
416 result = mathcursor->up(sel) ?
417 DispatchResult(true, true) : DispatchResult(false, FINISHED_UP);
421 sel = true; // fall through
423 result = mathcursor->down(sel) ?
424 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->setScreenPos(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_MATH_DELIM: {
608 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
610 string rs = split(cmd.argument, ls, ' ');
611 // Reasonable default values
617 recordUndo(bv, Undo::ATOMIC);
618 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
622 case LFUN_SPACE_INSERT:
623 case LFUN_MATH_SPACE:
624 recordUndo(bv, Undo::ATOMIC);
625 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
629 bv.owner()->message(_("Invalid action in math mode!"));
633 case LFUN_EXEC_COMMAND:
634 result = DispatchResult(false);
638 // interpret this as if a backslash was typed
639 recordUndo(bv, Undo::ATOMIC);
640 mathcursor->interpret('\\');
643 case LFUN_BREAKPARAGRAPH:
644 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
645 case LFUN_BREAKPARAGRAPH_SKIP:
649 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
650 // handling such that "self-insert" works on "arbitrary stuff" too, and
651 // math-insert only handles special math things like "matrix".
652 case LFUN_INSERT_MATH:
653 recordUndo(bv, Undo::ATOMIC);
654 mathcursor->niceInsert(argument);
658 case LFUN_SELFINSERT:
659 if (!argument.empty()) {
660 recordUndo(bv, Undo::ATOMIC);
661 if (argument.size() == 1)
662 result = mathcursor->interpret(argument[0]) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
664 mathcursor->insert(argument);
669 if (mathcursor->selection())
670 mathcursor->selClear();
672 result = DispatchResult(false);
675 case LFUN_INSET_TOGGLE:
676 mathcursor->insetToggle();
679 case LFUN_DIALOG_SHOW:
680 result = DispatchResult(false);
683 case LFUN_DIALOG_SHOW_NEW_INSET: {
684 string const & name = argument;
688 data = tmp.createDialogStr(name);
692 result = DispatchResult(false);
694 bv.owner()->getDialogs().show(name, data, 0);
698 case LFUN_INSET_APPLY: {
699 string const name = cmd.getArg(0);
700 InsetBase * base = bv.owner()->getDialogs().getOpenInset(name);
703 FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
704 result = base->dispatch(bv, fr);
707 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
708 mathcursor->insert(ar);
709 result = DispatchResult(true, true);
711 result = DispatchResult(false);
717 case LFUN_WORD_REPLACE:
718 case LFUN_WORD_FIND: {
720 searchForward(&bv, cmd.getArg(0), false, false)
721 ? DispatchResult(true, true) : DispatchResult(false);
726 result = DispatchResult(false);
729 if (result == DispatchResult(true, true))
732 mathcursor->normalize();
735 BOOST_ASSERT(mathcursor);
737 if (mathcursor->selection() || was_selection)
738 toggleInsetSelection(&bv);
740 if (result.dispatched()) {
742 bv.stuffClipboard(mathcursor->grabSelection());
744 releaseMathCursor(bv);
746 bv.owner()->dispatch(FuncRequest(LFUN_DELETE));
749 return result; // original version
753 void InsetFormulaBase::revealCodes(BufferView & bv) const
757 bv.owner()->message(mathcursor->info());
760 // write something to the minibuffer
761 // translate to latex
762 mathcursor->markInsert();
765 string str = os.str();
766 mathcursor->markErase();
767 string::size_type pos = 0;
769 for (string::iterator it = str.begin(); it != str.end(); ++it) {
772 else if (*it == '\0') {
774 pos = it - str.begin();
780 res = res.substr(pos - 30);
782 res = res.substr(0, 60);
783 bv.owner()->message(res);
788 InsetOld::Code InsetFormulaBase::lyxCode() const
790 return InsetOld::MATH_CODE;
794 int InsetFormulaBase::ylow() const
796 return yo_ - dim_.asc;
800 int InsetFormulaBase::yhigh() const
802 return yo_ + dim_.des;
806 int InsetFormulaBase::xlow() const
812 int InsetFormulaBase::xhigh() const
814 return xo_ + dim_.wid;
818 /////////////////////////////////////////////////////////////////////
821 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
827 static InsetFormulaBase * lastformula = 0;
828 static CursorBase current = CursorBase(ibegin(par().nucleus()));
830 static string laststr;
832 if (lastformula != this || laststr != str) {
833 //lyxerr << "reset lastformula to " << this << endl;
836 current = ibegin(par().nucleus());
838 mathed_parse_cell(ar, str);
842 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
844 for (CursorBase it = current; it != iend(par().nucleus()); increment(it)) {
845 CursorSlice & top = it.back();
846 MathArray const & a = top.asMathInset()->cell(top.idx_);
847 if (a.matchpart(ar, top.pos_)) {
849 mathcursor = new MathCursor(bv, this, true);
851 mathcursor->setSelection(it, ar.size());
853 top.pos_ += 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(BufferView & bv, FuncRequest const & cmd,
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.fullCursor().innerInset()->
902 dispatch(bv, FuncRequest(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(bv, FuncRequest(LFUN_MATH_DISPLAY));
908 f->dispatch(bv, FuncRequest(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(BufferView & bv, FuncRequest const & cmd)
931 switch (cmd.action) {
933 case LFUN_MATH_DISPLAY:
934 mathDispatchCreation(bv, cmd, true);
938 mathDispatchCreation(bv, cmd, false);
941 case LFUN_MATH_IMPORT_SELECTION:
942 mathDispatchCreation(bv, cmd, false);
945 case LFUN_MATH_MACRO:
946 if (cmd.argument.empty())
947 cmd.errorMessage(N_("Missing argument"));
949 string s = cmd.argument;
950 string const s1 = token(s, ' ', 1);
951 int const nargs = s1.empty() ? 0 : atoi(s1);
952 string const s2 = token(s, ' ', 2);
953 string const type = s2.empty() ? "newcommand" : s2;
954 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
958 case LFUN_INSERT_MATH:
959 case LFUN_INSERT_MATRIX:
960 case LFUN_MATH_DELIM: {
961 InsetFormula * f = new InsetFormula(&bv);
962 if (openNewInset(bv, f)) {
963 UpdatableInset * inset = bv.fullCursor().innerInset();
964 inset->dispatch(bv, FuncRequest(LFUN_MATH_MUTATE, "simple"));
965 inset->dispatch(bv, cmd);