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()
83 // This is needed as long the math parser is not re-entrant
85 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
86 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
87 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
88 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
92 // simply scrap this function if you want
93 void InsetFormulaBase::mutateToText()
98 latex(NULL, os, false, false);
99 string str = os.str();
102 LyXText * lt = view_->getLyXText();
103 string::const_iterator cit = str.begin();
104 string::const_iterator end = str.end();
105 for (; cit != end; ++cit)
106 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
109 //view_->owner()->dispatch(LFUN_ESCAPE);
114 void InsetFormulaBase::handleFont
115 (BufferView * bv, string const & arg, string const & font)
117 // this whole function is a hack and won't work for incremental font
119 recordUndo(bv, Undo::ATOMIC);
121 if (mathcursor->inset()->name() == font)
122 mathcursor->handleFont(font);
124 mathcursor->handleNest(createMathInset(font));
125 mathcursor->insert(arg);
130 void InsetFormulaBase::handleFont2(BufferView * bv, string const & arg)
132 recordUndo(bv, Undo::ATOMIC);
135 bv_funcs::string2font(arg, font, b);
136 if (font.color() != LColor::inherit) {
137 MathAtom at = createMathInset("color");
138 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
139 mathcursor->handleNest(at, 1);
145 void InsetFormulaBase::validate(LaTeXFeatures &) const
149 string const InsetFormulaBase::editMessage() const
151 return _("Math editor mode");
155 void InsetFormulaBase::insetUnlock(BufferView * bv)
158 if (mathcursor->inMacroMode())
159 mathcursor->macroModeClose();
160 releaseMathCursor(bv);
163 generatePreview(*bv->buffer());
168 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
170 mathcursor->getPos(x, y);
174 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
176 // calling metrics here destroys the cached xo,yo positions e.g. in
177 // MathParboxinset. And it would be too expensive anyway...
180 lyxerr << "getCursorPos - should not happen";
184 mathcursor->getPos(x, y);
185 x = mathcursor->targetX();
188 lyxerr << "InsetFormulaBase::getCursorPos: " << x << ' ' << y << endl;
192 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
199 //math_font_max_dim(font_, asc, des);
200 getCursorPos(bv, x, y);
202 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
203 bv->fitLockedInsetCursor(x, y, asc, des);
207 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
214 DispatchResult InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
217 return DispatchResult(false);
219 BufferView * bv = cmd.view();
221 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
223 if (cmd.button() == mouse_button::button3) {
224 // try to dispatch to enclosed insets first
225 if (!mathcursor->dispatch(cmd).dispatched()) {
226 // launch math panel for right mouse button
227 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
228 bv->owner()->getDialogs().show("mathpanel");
230 return DispatchResult(true, true);
233 if (cmd.button() == mouse_button::button2) {
235 asArray(bv->getClipboard(), ar);
236 mathcursor->selClear();
237 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
238 mathcursor->insert(ar);
240 return DispatchResult(true, true);
243 if (cmd.button() == mouse_button::button1) {
244 // try to dispatch to enclosed insets first
245 mathcursor->dispatch(cmd);
246 cmd.view()->stuffClipboard(mathcursor->grabSelection());
247 // try to set the cursor
249 //mathcursor = new MathCursor(this, x == 0);
251 //mathcursor->setPos(x + xo_, y + yo_);
252 return DispatchResult(true, true);
255 return DispatchResult(false);
259 DispatchResult InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
261 BufferView * bv = cmd.view();
262 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
264 if (!mathcursor || mathcursor->formula() != this) {
265 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
266 releaseMathCursor(bv);
267 mathcursor = new MathCursor(this, cmd.x == 0);
269 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
272 if (cmd.button() == mouse_button::button3) {
273 mathcursor->dispatch(cmd);
274 return DispatchResult(true, true);
277 if (cmd.button() == mouse_button::button1) {
280 mathcursor->selClear();
281 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
282 mathcursor->dispatch(cmd);
283 return DispatchResult(true, true);
287 return DispatchResult(true, true);
291 DispatchResult InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
294 return DispatchResult(true, true);
296 if (mathcursor->dispatch(FuncRequest(cmd)).dispatched())
297 return DispatchResult(true, true);
299 // only select with button 1
300 if (cmd.button() != mouse_button::button1)
301 return DispatchResult(true, true);
303 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
304 return DispatchResult(true, true);
309 if (!mathcursor->selection())
310 mathcursor->selStart();
312 BufferView * bv = cmd.view();
313 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
315 return DispatchResult(true, true);
319 void InsetFormulaBase::edit(BufferView * bv, bool left)
321 lyxerr << "Called FormulaBase::edit" << endl;
322 releaseMathCursor(bv);
323 mathcursor = new MathCursor(this, left);
324 bv->cursor().push(this);
325 // if that is removed, we won't get the magenta box when entering an
326 // inset for the first time
331 void InsetFormulaBase::edit(BufferView * bv, int x, int y)
333 lyxerr << "Called FormulaBase::EDIT with '" << x << ' ' << y << "'" << endl;
334 releaseMathCursor(bv);
335 mathcursor = new MathCursor(this, true);
337 mathcursor->setPos(x + xo_, y + yo_);
338 bv->cursor().push(this);
339 // if that is removed, we won't get the magenta box when entering an
340 // inset for the first time
346 InsetFormulaBase::priv_dispatch(FuncRequest const & cmd,
347 idx_type &, pos_type &)
349 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
350 // << " arg: '" << cmd.argument
351 // << "' x: '" << cmd.x
352 // << " y: '" << cmd.y
353 // << "' button: " << cmd.button() << endl;
355 BufferView * bv = cmd.view();
357 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
358 bool remove_inset = false;
360 switch (cmd.action) {
361 case LFUN_MOUSE_PRESS:
362 //lyxerr << "Mouse single press" << endl;
363 return lfunMousePress(cmd);
364 case LFUN_MOUSE_MOTION:
365 //lyxerr << "Mouse motion" << endl;
366 return lfunMouseMotion(cmd);
367 case LFUN_MOUSE_RELEASE:
368 //lyxerr << "Mouse single release" << endl;
369 return lfunMouseRelease(cmd);
370 case LFUN_MOUSE_DOUBLE:
371 //lyxerr << "Mouse double" << endl;
372 return dispatch(FuncRequest(LFUN_WORDSEL));
378 return DispatchResult(false);
380 string argument = cmd.argument;
381 DispatchResult result(true);
383 bool was_macro = mathcursor->inMacroMode();
384 bool was_selection = mathcursor->selection();
386 mathcursor->normalize();
389 switch (cmd.action) {
391 case LFUN_MATH_MUTATE:
392 case LFUN_MATH_DISPLAY:
393 case LFUN_MATH_NUMBER:
394 case LFUN_MATH_NONUMBER:
395 case LFUN_CELL_SPLIT:
397 case LFUN_DELETE_LINE_FORWARD:
398 case LFUN_INSERT_LABEL:
399 case LFUN_MATH_EXTERN:
400 case LFUN_TABULAR_FEATURE:
401 case LFUN_PASTESELECTION:
402 case LFUN_MATH_LIMITS:
403 recordUndo(bv, Undo::ATOMIC);
404 mathcursor->dispatch(cmd);
408 sel = true; // fall through...
410 result = mathcursor->right(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
411 //lyxerr << "calling scroll 20" << endl;
413 // write something to the minibuffer
414 //bv->owner()->message(mathcursor->info());
418 sel = true; // fall through
420 result = mathcursor->left(sel) ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
424 sel = true; // fall through
426 result = mathcursor->up(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_UP);
430 sel = true; // fall through
432 result = mathcursor->down(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_DOWN);
436 mathcursor->home(false);
437 mathcursor->end(true);
440 case LFUN_UP_PARAGRAPHSEL:
441 case LFUN_UP_PARAGRAPH:
442 case LFUN_DOWN_PARAGRAPHSEL:
443 case LFUN_DOWN_PARAGRAPH:
444 result = DispatchResult(true, FINISHED);
448 case LFUN_WORDLEFTSEL:
449 sel = true; // fall through
452 result = mathcursor->home(sel) ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
456 case LFUN_WORDRIGHTSEL:
457 sel = true; // fall through
460 result = mathcursor->end(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
465 case LFUN_BEGINNINGBUFSEL:
466 case LFUN_BEGINNINGBUF:
467 result = DispatchResult(true, FINISHED);
474 result = DispatchResult(false, FINISHED_RIGHT);
477 case LFUN_CELL_FORWARD:
478 mathcursor->idxNext();
481 case LFUN_CELL_BACKWARD:
482 mathcursor->idxPrev();
485 case LFUN_DELETE_WORD_BACKWARD:
487 recordUndo(bv, Undo::ATOMIC);
488 if (!mathcursor->backspace()) {
489 result = DispatchResult(true, FINISHED);
494 case LFUN_DELETE_WORD_FORWARD:
496 recordUndo(bv, Undo::ATOMIC);
497 if (!mathcursor->erase()) {
498 result = DispatchResult(true, FINISHED);
504 // sprintf(dispatch_buffer, "%d %d",);
505 // DispatchResult= dispatch_buffer;
508 lyxerr << "LFUN_SETXY broken!" << endl;
511 istringstream is(cmd.argument.c_str());
513 mathcursor->setPos(x, y);
519 istringstream is(cmd.argument.c_str());
522 mathcursor->macroModeClose();
523 recordUndo(bv, Undo::ATOMIC);
524 mathcursor->selPaste(n);
529 recordUndo(bv, Undo::DELETE);
530 mathcursor->selCut();
534 mathcursor->selCopy();
538 // Special casing for superscript in case of LyX handling
540 case LFUN_CIRCUMFLEX:
541 if (cmd.argument.empty()) {
542 // do superscript if LyX handles
544 recordUndo(bv, Undo::ATOMIC);
545 mathcursor->script(true);
562 case LFUN_HUNG_UMLAUT:
566 case LFUN_FREEFONT_APPLY:
567 case LFUN_FREEFONT_UPDATE:
568 handleFont2(bv, cmd.argument);
571 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
572 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
573 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
574 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
575 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
576 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
577 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
578 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
579 //case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
580 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
583 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
584 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
586 handleFont(bv, cmd.argument, "textrm");
587 //bv->owner()->message(_("math text mode toggled"));
593 recordUndo(bv, Undo::ATOMIC);
594 mathcursor->setSize(arg);
599 case LFUN_INSERT_MATRIX: {
600 recordUndo(bv, Undo::ATOMIC);
605 istringstream is(argument);
606 is >> m >> n >> v_align >> h_align;
610 mathcursor->niceInsert(
611 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
615 case LFUN_SUPERSCRIPT:
618 recordUndo(bv, Undo::ATOMIC);
619 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
623 case LFUN_MATH_DELIM:
625 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
627 string rs = split(cmd.argument, ls, ' ');
628 // Reasonable default values
634 recordUndo(bv, Undo::ATOMIC);
635 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
639 case LFUN_SPACE_INSERT:
640 case LFUN_MATH_SPACE:
641 recordUndo(bv, Undo::ATOMIC);
642 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
646 bv->owner()->message(_("Invalid action in math mode!"));
650 case LFUN_EXEC_COMMAND:
651 result = DispatchResult(false);
655 // interpret this as if a backslash was typed
656 recordUndo(bv, Undo::ATOMIC);
657 mathcursor->interpret('\\');
660 case LFUN_BREAKPARAGRAPH:
661 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
662 case LFUN_BREAKPARAGRAPH_SKIP:
666 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
667 // handling such that "self-insert" works on "arbitrary stuff" too, and
668 // math-insert only handles special math things like "matrix".
669 case LFUN_INSERT_MATH:
670 recordUndo(bv, Undo::ATOMIC);
671 mathcursor->niceInsert(argument);
675 case LFUN_SELFINSERT:
676 if (!argument.empty()) {
677 recordUndo(bv, Undo::ATOMIC);
678 if (argument.size() == 1)
679 result = mathcursor->interpret(argument[0]) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
681 mathcursor->insert(argument);
686 if (mathcursor->selection())
687 mathcursor->selClear();
689 result = DispatchResult(false);
692 case LFUN_INSET_TOGGLE:
693 mathcursor->insetToggle();
696 case LFUN_DIALOG_SHOW:
697 result = DispatchResult(false);
700 case LFUN_DIALOG_SHOW_NEW_INSET: {
701 string const & name = argument;
705 data = tmp.createDialogStr(name);
709 result = DispatchResult(false);
711 bv->owner()->getDialogs().show(name, data, 0);
715 case LFUN_INSET_APPLY: {
716 string const name = cmd.getArg(0);
718 bv->owner()->getDialogs().getOpenInset(name);
721 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
722 result = base->dispatch(fr);
725 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
726 mathcursor->insert(ar);
727 result = DispatchResult(true, true);
729 result = DispatchResult(false);
736 result = DispatchResult(false);
739 if (result == DispatchResult(true, true))
742 mathcursor->normalize();
745 BOOST_ASSERT(mathcursor);
747 if (mathcursor->selection() || was_selection)
748 toggleInsetSelection(bv);
750 if (result.dispatched()) {
753 cmd.view()->stuffClipboard(mathcursor->grabSelection());
755 releaseMathCursor(bv);
757 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
760 return result; // original version
764 void InsetFormulaBase::revealCodes(BufferView * bv) const
768 bv->owner()->message(mathcursor->info());
771 // write something to the minibuffer
772 // translate to latex
773 mathcursor->markInsert();
776 string str = os.str();
777 mathcursor->markErase();
778 string::size_type pos = 0;
780 for (string::iterator it = str.begin(); it != str.end(); ++it) {
783 else if (*it == '\0') {
785 pos = it - str.begin();
791 res = res.substr(pos - 30);
793 res = res.substr(0, 60);
794 bv->owner()->message(res);
799 InsetOld::Code InsetFormulaBase::lyxCode() const
801 return InsetOld::MATH_CODE;
805 int InsetFormulaBase::ylow() const
807 return yo_ - dim_.asc;
811 int InsetFormulaBase::yhigh() const
813 return yo_ + dim_.des;
817 int InsetFormulaBase::xlow() const
823 int InsetFormulaBase::xhigh() const
825 return xo_ + dim_.wid;
829 /////////////////////////////////////////////////////////////////////
832 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
838 static InsetFormulaBase * lastformula = 0;
839 static MathIterator current = MathIterator(ibegin(par().nucleus()));
841 static string laststr;
843 if (lastformula != this || laststr != str) {
844 //lyxerr << "reset lastformula to " << this << endl;
847 current = ibegin(par().nucleus());
849 mathed_parse_cell(ar, str);
853 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
855 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
856 if (it.cell().matchpart(ar, it.back().pos_)) {
858 mathcursor = new MathCursor(this, true);
860 mathcursor->setSelection(it, ar.size());
868 //lyxerr << "not found!" << endl;
874 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
877 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
878 return searchForward(bv, what, a, b);
882 bool InsetFormulaBase::display() const
884 return par()->asHullInset() && par()->asHullInset()->display();
888 string InsetFormulaBase::selectionAsString() const
890 return mathcursor ? mathcursor->grabSelection() : string();
893 /////////////////////////////////////////////////////////////////////
896 void mathDispatchCreation(FuncRequest const & cmd, bool display)
898 BufferView * bv = cmd.view();
899 // use selection if available..
901 //if (action == LFUN_MATH_IMPORT_SELECTION)
905 string sel = bv->getLyXText()->selectionAsString(*bv->buffer(), false);
908 InsetFormula * f = new InsetFormula(bv);
909 if (openNewInset(bv, f)) {
910 bv->cursor().innerInset()->
911 dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
912 // don't do that also for LFUN_MATH_MODE unless you want end up with
913 // always changing to mathrm when opening an inlined inset
914 // -- I really hate "LyXfunc overloading"...
916 f->dispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
917 f->dispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
920 // create a macro if we see "\\newcommand" somewhere, and an ordinary
922 InsetFormulaBase * f;
923 if (sel.find("\\newcommand") == string::npos &&
924 sel.find("\\def") == string::npos)
925 f = new InsetFormula(sel);
927 f = new InsetFormulaMacro(sel);
928 bv->getLyXText()->cutSelection(true, false);
931 cmd.message(N_("Math editor mode"));
935 void mathDispatch(FuncRequest const & cmd)
937 BufferView * bv = cmd.view();
938 if (!bv->available())
941 switch (cmd.action) {
943 case LFUN_MATH_DISPLAY:
944 mathDispatchCreation(cmd, true);
948 mathDispatchCreation(cmd, false);
951 case LFUN_MATH_IMPORT_SELECTION:
952 mathDispatchCreation(cmd, false);
955 case LFUN_MATH_MACRO:
956 if (cmd.argument.empty())
957 cmd.errorMessage(N_("Missing argument"));
959 string s = cmd.argument;
960 string const s1 = token(s, ' ', 1);
961 int const nargs = s1.empty() ? 0 : atoi(s1);
962 string const s2 = token(s, ' ', 2);
963 string const type = s2.empty() ? "newcommand" : s2;
964 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
968 case LFUN_INSERT_MATH:
969 case LFUN_INSERT_MATRIX:
970 case LFUN_MATH_DELIM: {
971 InsetFormula * f = new InsetFormula(bv);
972 if (openNewInset(bv, f)) {
973 UpdatableInset * inset = bv->cursor().innerInset();
974 inset->dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
975 inset->dispatch(cmd);