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.
14 #include "formulabase.h"
16 #include "formulamacro.h"
17 #include "math_support.h"
18 #include "math_arrayinset.h"
19 #include "math_deliminset.h"
20 #include "math_cursor.h"
21 #include "math_factory.h"
22 #include "math_hullinset.h"
23 #include "math_parser.h"
24 #include "math_spaceinset.h"
25 #include "ref_inset.h"
27 #include "BufferView.h"
28 #include "bufferview_funcs.h"
29 #include "dispatchresult.h"
31 #include "funcrequest.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/Dialogs.h"
40 #include "support/std_sstream.h"
41 #include "support/lstrings.h"
42 #include "support/lyxlib.h"
44 using lyx::support::atoi;
45 using lyx::support::split;
46 using lyx::support::token;
53 using std::istringstream;
56 MathCursor * mathcursor = 0;
64 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
66 if (!bv->insertInset(new_inset)) {
70 new_inset->dispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
79 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 bv->updateInset(this);
161 releaseMathCursor(bv);
164 generatePreview(*bv->buffer());
165 bv->updateInset(this);
169 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
171 mathcursor->getPos(x, y);
175 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
177 // calling metrics here destroys the cached xo,yo positions e.g. in
178 // MathParboxinset. And it would be too expensive anyway...
181 lyxerr << "getCursorPos - should not happen";
185 mathcursor->getPos(x, y);
186 x = mathcursor->targetX();
189 //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
193 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
200 //math_font_max_dim(font_, asc, des);
201 getCursorPos(bv, x, y);
203 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
204 bv->fitLockedInsetCursor(x, y, asc, des);
208 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
211 bv->updateInset(this);
215 DispatchResult InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
220 BufferView * bv = cmd.view();
221 bv->updateInset(this);
222 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
224 if (cmd.button() == mouse_button::button3) {
225 // try to dispatch to enclosed insets first
226 if (mathcursor->dispatch(cmd) == UNDISPATCHED) {
227 // launch math panel for right mouse button
228 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
229 bv->owner()->getDialogs().show("mathpanel");
234 if (cmd.button() == mouse_button::button2) {
236 asArray(bv->getClipboard(), ar);
237 mathcursor->selClear();
238 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
239 mathcursor->insert(ar);
240 bv->updateInset(this);
244 if (cmd.button() == mouse_button::button1) {
245 // try to dispatch to enclosed insets first
246 mathcursor->dispatch(cmd);
247 cmd.view()->stuffClipboard(mathcursor->grabSelection());
248 // try to set the cursor
250 //mathcursor = new MathCursor(this, x == 0);
252 //mathcursor->setPos(x + xo_, y + yo_);
260 DispatchResult InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
262 BufferView * bv = cmd.view();
263 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
265 if (!mathcursor || mathcursor->formula() != this) {
266 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
267 releaseMathCursor(bv);
268 mathcursor = new MathCursor(this, cmd.x == 0);
270 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
273 if (cmd.button() == mouse_button::button3) {
274 mathcursor->dispatch(cmd);
278 if (cmd.button() == mouse_button::button1) {
281 mathcursor->selClear();
282 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
283 mathcursor->dispatch(cmd);
287 bv->updateInset(this);
292 DispatchResult InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
297 if (mathcursor->dispatch(FuncRequest(cmd)) != UNDISPATCHED)
300 // only select with button 1
301 if (cmd.button() != mouse_button::button1)
304 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
310 if (!mathcursor->selection())
311 mathcursor->selStart();
313 BufferView * bv = cmd.view();
314 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
315 bv->updateInset(this);
321 InsetFormulaBase::priv_dispatch(FuncRequest const & cmd,
322 idx_type &, pos_type &)
324 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
325 // << " arg: '" << cmd.argument
326 // << "' x: '" << cmd.x
327 // << " y: '" << cmd.y
328 // << "' button: " << cmd.button() << endl;
330 BufferView * bv = cmd.view();
332 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
333 bool remove_inset = false;
335 switch (cmd.action) {
336 case LFUN_INSET_EDIT:
337 lyxerr << "Called EDIT with '" << cmd.argument << "'" << endl;
338 if (!bv->lockInset(this))
339 lyxerr << "Cannot lock math inset in edit call!" << endl;
340 releaseMathCursor(bv);
341 if (!cmd.argument.empty()) {
342 mathcursor = new MathCursor(this, cmd.argument == "left");
345 mathcursor = new MathCursor(this, true);
347 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
349 // if that is removed, we won't get the magenta box when entering an
350 // inset for the first time
351 bv->updateInset(this);
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));
373 string argument = cmd.argument;
374 DispatchResult result = DISPATCHED;
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) ? DISPATCHED : 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) ? DISPATCHED : FINISHED;
417 sel = true; // fall through
419 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
423 sel = true; // fall through
425 result = mathcursor->down(sel) ? DISPATCHED : 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:
441 case LFUN_WORDLEFTSEL:
442 sel = true; // fall through
445 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
449 case LFUN_WORDRIGHTSEL:
450 sel = true; // fall through
453 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
458 case LFUN_BEGINNINGBUFSEL:
459 case LFUN_BEGINNINGBUF:
467 result = 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()) {
487 case LFUN_DELETE_WORD_FORWARD:
489 recordUndo(bv, Undo::ATOMIC);
490 if (!mathcursor->erase()) {
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 = UNDISPATCHED;
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]) ? DISPATCHED : FINISHED_RIGHT;
674 mathcursor->insert(argument);
679 if (mathcursor->selection())
680 mathcursor->selClear();
682 result = UNDISPATCHED;
685 case LFUN_INSET_TOGGLE:
686 mathcursor->insetToggle();
689 case LFUN_DIALOG_SHOW:
690 result = UNDISPATCHED;
693 case LFUN_DIALOG_SHOW_NEW_INSET: {
694 string const & name = argument;
698 data = tmp.createDialogStr(name);
702 result = UNDISPATCHED;
704 bv->owner()->getDialogs().show(name, data, 0);
709 case LFUN_INSET_APPLY: {
710 string const name = cmd.getArg(0);
712 bv->owner()->getDialogs().getOpenInset(name);
715 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
716 result = base->dispatch(fr);
719 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
720 mathcursor->insert(ar);
723 result = UNDISPATCHED;
730 result = UNDISPATCHED;
733 if (result == DISPATCHED)
734 bv->updateInset(this);
736 mathcursor->normalize();
739 BOOST_ASSERT(mathcursor);
741 if (mathcursor->selection() || was_selection)
742 toggleInsetSelection(bv);
744 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
745 result == UNDISPATCHED) {
748 cmd.view()->stuffClipboard(mathcursor->grabSelection());
750 releaseMathCursor(bv);
751 bv->unlockInset(this);
753 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
756 return result; // original version
760 void InsetFormulaBase::revealCodes(BufferView * bv) const
764 bv->owner()->message(mathcursor->info());
767 // write something to the minibuffer
768 // translate to latex
769 mathcursor->markInsert();
772 string str = os.str();
773 mathcursor->markErase();
774 string::size_type pos = 0;
776 for (string::iterator it = str.begin(); it != str.end(); ++it) {
779 else if (*it == '\0') {
781 pos = it - str.begin();
787 res = res.substr(pos - 30);
789 res = res.substr(0, 60);
790 bv->owner()->message(res);
795 InsetOld::Code InsetFormulaBase::lyxCode() const
797 return InsetOld::MATH_CODE;
801 int InsetFormulaBase::ylow() const
803 return yo_ - dim_.asc;
807 int InsetFormulaBase::yhigh() const
809 return yo_ + dim_.des;
813 int InsetFormulaBase::xlow() const
819 int InsetFormulaBase::xhigh() const
821 return xo_ + dim_.wid;
825 /////////////////////////////////////////////////////////////////////
828 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
834 static InsetFormulaBase * lastformula = 0;
835 static MathIterator current = MathIterator(ibegin(par().nucleus()));
837 static string laststr;
839 if (lastformula != this || laststr != str) {
840 //lyxerr << "reset lastformula to " << this << endl;
843 current = ibegin(par().nucleus());
845 mathed_parse_cell(ar, str);
849 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
851 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
852 if (it.cell().matchpart(ar, it.back().pos_)) {
853 bv->unlockInset(bv->theLockingInset());
854 if (!bv->lockInset(this)) {
855 lyxerr << "Cannot lock inset" << endl;
859 mathcursor = new MathCursor(this, true);
861 mathcursor->setSelection(it, ar.size());
864 bv->updateInset(this);
869 //lyxerr << "not found!" << endl;
875 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
878 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
879 return searchForward(bv, what, a, b);
883 bool InsetFormulaBase::display() const
885 return par()->asHullInset() && par()->asHullInset()->display();
889 string InsetFormulaBase::selectionAsString() const
891 return mathcursor ? mathcursor->grabSelection() : string();
894 /////////////////////////////////////////////////////////////////////
897 void mathDispatchCreation(FuncRequest const & cmd, bool display)
899 BufferView * bv = cmd.view();
900 // use selection if available..
902 //if (action == LFUN_MATH_IMPORT_SELECTION)
906 string sel = bv->getLyXText()->selectionAsString(*bv->buffer(), false);
909 InsetFormula * f = new InsetFormula(bv);
910 if (openNewInset(bv, f)) {
911 bv->theLockingInset()->
912 dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
913 // don't do that also for LFUN_MATH_MODE unless you want end up with
914 // always changing to mathrm when opening an inlined inset
915 // -- I really hate "LyXfunc overloading"...
917 f->dispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
918 f->dispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
921 // create a macro if we see "\\newcommand" somewhere, and an ordinary
923 InsetFormulaBase * f;
924 if (sel.find("\\newcommand") == string::npos &&
925 sel.find("\\def") == string::npos)
926 f = new InsetFormula(sel);
928 f = new InsetFormulaMacro(sel);
929 bv->getLyXText()->cutSelection(true, false);
932 cmd.message(N_("Math editor mode"));
936 void mathDispatch(FuncRequest const & cmd)
938 BufferView * bv = cmd.view();
939 if (!bv->available())
942 switch (cmd.action) {
944 case LFUN_MATH_DISPLAY:
945 mathDispatchCreation(cmd, true);
949 mathDispatchCreation(cmd, false);
952 case LFUN_MATH_IMPORT_SELECTION:
953 mathDispatchCreation(cmd, false);
956 case LFUN_MATH_MACRO:
957 if (cmd.argument.empty())
958 cmd.errorMessage(N_("Missing argument"));
960 string s = cmd.argument;
961 string const s1 = token(s, ' ', 1);
962 int const nargs = s1.empty() ? 0 : atoi(s1);
963 string const s2 = token(s, ' ', 2);
964 string const type = s2.empty() ? "newcommand" : s2;
965 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
969 case LFUN_INSERT_MATH:
970 case LFUN_INSERT_MATRIX:
971 case LFUN_MATH_DELIM: {
972 InsetFormula * f = new InsetFormula(bv);
973 if (openNewInset(bv, f)) {
974 bv->theLockingInset()->
975 dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
976 bv->theLockingInset()->dispatch(cmd);