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 "support/LAssert.h"
17 #include "formulamacro.h"
18 #include "funcrequest.h"
19 #include "BufferView.h"
23 #include "math_support.h"
24 #include "support/lstrings.h"
25 #include "support/lyxlib.h"
26 #include "frontends/LyXView.h"
27 #include "math_arrayinset.h"
28 #include "math_deliminset.h"
29 #include "math_cursor.h"
30 #include "math_factory.h"
31 #include "math_hullinset.h"
32 #include "math_parser.h"
33 #include "math_spaceinset.h"
34 #include "undo_funcs.h"
35 #include "frontends/Dialogs.h"
36 #include "ref_inset.h"
39 using namespace lyx::support;
48 MathCursor * mathcursor = 0;
56 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
58 if (!bv->insertInset(new_inset)) {
62 new_inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
71 InsetFormulaBase::InsetFormulaBase()
72 : view_(0), xo_(0), yo_(0)
74 // This is needed as long the math parser is not re-entrant
76 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
77 //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
78 //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
79 //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
83 // simply scrap this function if you want
84 void InsetFormulaBase::mutateToText()
89 latex(NULL, os, false, false);
90 string str = os.str();
93 LyXText * lt = view_->getLyXText();
94 string::const_iterator cit = str.begin();
95 string::const_iterator end = str.end();
96 for (; cit != end; ++cit)
97 view_->owner()->getIntl()->getTransManager().TranslateAndInsert(*cit, lt);
100 //view_->owner()->dispatch(LFUN_ESCAPE);
105 void InsetFormulaBase::handleFont
106 (BufferView * bv, string const & arg, string const & font)
108 // this whole function is a hack and won't work for incremental font
110 recordUndo(bv, Undo::ATOMIC);
112 if (mathcursor->inset()->name() == font)
113 mathcursor->handleFont(font);
115 mathcursor->handleNest(createMathInset(font));
116 mathcursor->insert(arg);
121 BufferView * InsetFormulaBase::view() const
127 void InsetFormulaBase::validate(LaTeXFeatures &) const
131 string const InsetFormulaBase::editMessage() const
133 return _("Math editor mode");
137 void InsetFormulaBase::insetUnlock(BufferView * bv)
140 if (mathcursor->inMacroMode()) {
141 mathcursor->macroModeClose();
142 bv->updateInset(this);
144 releaseMathCursor(bv);
147 bv->updateInset(this);
151 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
153 mathcursor->getPos(x, y);
157 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
159 // calling metrics here destroys the cached xo,yo positions e.g. in
160 // MathParboxinset. And it would be too expensive anyway...
163 lyxerr << "getCursorPos - should not happen";
167 mathcursor->getPos(x, y);
168 x = mathcursor->targetX();
171 //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
175 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
182 //math_font_max_dim(font_, asc, des);
183 getCursorPos(bv, x, y);
185 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
186 bv->fitLockedInsetCursor(x, y, asc, des);
190 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
193 bv->updateInset(this);
197 dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
202 BufferView * bv = cmd.view();
203 bv->updateInset(this);
204 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
206 if (cmd.button() == mouse_button::button3) {
207 // try to dispatch to enclosed insets first
208 if (mathcursor->dispatch(cmd) == UNDISPATCHED) {
209 // launch math panel for right mouse button
210 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
211 bv->owner()->getDialogs().show("mathpanel");
216 if (cmd.button() == mouse_button::button2) {
218 asArray(bv->getClipboard(), ar);
219 mathcursor->selClear();
220 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
221 mathcursor->insert(ar);
222 bv->updateInset(this);
226 if (cmd.button() == mouse_button::button1) {
227 // try to dispatch to enclosed insets first
228 mathcursor->dispatch(cmd);
229 cmd.view()->stuffClipboard(mathcursor->grabSelection());
230 // try to set the cursor
232 //mathcursor = new MathCursor(this, x == 0);
234 //mathcursor->setPos(x + xo_, y + yo_);
242 dispatch_result InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
244 BufferView * bv = cmd.view();
245 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
247 if (!mathcursor || mathcursor->formula() != this) {
248 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
249 releaseMathCursor(bv);
250 mathcursor = new MathCursor(this, cmd.x == 0);
252 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
255 if (cmd.button() == mouse_button::button3) {
256 mathcursor->dispatch(cmd);
260 if (cmd.button() == mouse_button::button1) {
263 mathcursor->selClear();
264 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
265 mathcursor->dispatch(cmd);
269 bv->updateInset(this);
274 dispatch_result InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
279 if (mathcursor->dispatch(FuncRequest(cmd)) != UNDISPATCHED)
282 // only select with button 1
283 if (cmd.button() != mouse_button::button1)
286 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
292 if (!mathcursor->selection())
293 mathcursor->selStart();
295 BufferView * bv = cmd.view();
296 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
297 bv->updateInset(this);
302 dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
304 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
305 // << " arg: '" << cmd.argument
306 // << " x: '" << cmd.x
307 // << " y: '" << cmd.y
308 // << "' button: " << cmd.button() << endl;
310 BufferView * bv = cmd.view();
312 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
313 bool remove_inset = false;
315 switch (cmd.action) {
316 case LFUN_INSET_EDIT:
317 lyxerr << "Called EDIT with '" << cmd.argument << "'" << endl;
318 if (!bv->lockInset(this))
319 lyxerr << "Cannot lock math inset in edit call!" << endl;
320 releaseMathCursor(bv);
321 if (!cmd.argument.empty()) {
322 mathcursor = new MathCursor(this, cmd.argument == "left");
325 mathcursor = new MathCursor(this, true);
327 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
329 // if that is removed, we won't get the magenta box when entering an
330 // inset for the first time
331 bv->updateInset(this);
334 case LFUN_MOUSE_PRESS:
335 //lyxerr << "Mouse single press" << endl;
336 return lfunMousePress(cmd);
337 case LFUN_MOUSE_MOTION:
338 //lyxerr << "Mouse motion" << endl;
339 return lfunMouseMotion(cmd);
340 case LFUN_MOUSE_RELEASE:
341 //lyxerr << "Mouse single release" << endl;
342 return lfunMouseRelease(cmd);
343 case LFUN_MOUSE_DOUBLE:
344 //lyxerr << "Mouse double" << endl;
345 return localDispatch(FuncRequest(LFUN_WORDSEL));
353 string argument = cmd.argument;
354 RESULT result = DISPATCHED;
356 bool was_macro = mathcursor->inMacroMode();
357 bool was_selection = mathcursor->selection();
359 mathcursor->normalize();
362 switch (cmd.action) {
364 case LFUN_MATH_MUTATE:
365 case LFUN_MATH_DISPLAY:
366 case LFUN_MATH_NUMBER:
367 case LFUN_MATH_NONUMBER:
368 case LFUN_CELL_SPLIT:
370 case LFUN_DELETE_LINE_FORWARD:
371 case LFUN_INSERT_LABEL:
372 case LFUN_MATH_EXTERN:
373 case LFUN_TABULAR_FEATURE:
374 case LFUN_PASTESELECTION:
375 case LFUN_MATH_LIMITS:
376 recordUndo(bv, Undo::ATOMIC);
377 mathcursor->dispatch(cmd);
381 sel = true; // fall through...
383 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
384 //lyxerr << "calling scroll 20" << endl;
386 // write something to the minibuffer
387 //bv->owner()->message(mathcursor->info());
391 sel = true; // fall through
393 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
397 sel = true; // fall through
399 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
403 sel = true; // fall through
405 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
409 mathcursor->home(false);
410 mathcursor->end(true);
413 case LFUN_UP_PARAGRAPHSEL:
414 case LFUN_UP_PARAGRAPH:
415 case LFUN_DOWN_PARAGRAPHSEL:
416 case LFUN_DOWN_PARAGRAPH:
421 case LFUN_WORDLEFTSEL:
422 sel = true; // fall through
425 result = mathcursor->home(sel) ? DISPATCHED : FINISHED;
429 case LFUN_WORDRIGHTSEL:
430 sel = true; // fall through
433 result = mathcursor->end(sel) ? DISPATCHED : FINISHED_RIGHT;
438 case LFUN_BEGINNINGBUFSEL:
439 case LFUN_BEGINNINGBUF:
447 result = FINISHED_RIGHT;
450 case LFUN_CELL_FORWARD:
451 mathcursor->idxNext();
454 case LFUN_CELL_BACKWARD:
455 mathcursor->idxPrev();
458 case LFUN_DELETE_WORD_BACKWARD:
460 recordUndo(bv, Undo::ATOMIC);
461 if (!mathcursor->backspace()) {
467 case LFUN_DELETE_WORD_FORWARD:
469 recordUndo(bv, Undo::ATOMIC);
470 if (!mathcursor->erase()) {
477 // sprintf(dispatch_buffer, "%d %d",);
478 // dispatch_result = dispatch_buffer;
481 lyxerr << "LFUN_SETXY broken!" << endl;
484 istringstream is(cmd.argument.c_str());
486 mathcursor->setPos(x, y);
492 istringstream is(cmd.argument.c_str());
495 mathcursor->macroModeClose();
496 recordUndo(bv, Undo::ATOMIC);
497 mathcursor->selPaste(n);
502 recordUndo(bv, Undo::DELETE);
503 mathcursor->selCut();
507 mathcursor->selCopy();
511 // Special casing for superscript in case of LyX handling
513 case LFUN_CIRCUMFLEX:
514 if (cmd.argument.empty()) {
515 // do superscript if LyX handles
517 recordUndo(bv, Undo::ATOMIC);
518 mathcursor->script(true);
535 case LFUN_HUNG_UMLAUT:
539 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
540 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
541 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
542 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
543 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
544 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
545 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
546 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
547 case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
548 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
551 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
552 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
554 handleFont(bv, cmd.argument, "textrm");
555 //bv->owner()->message(_("math text mode toggled"));
561 recordUndo(bv, Undo::ATOMIC);
562 mathcursor->setSize(arg);
567 case LFUN_INSERT_MATRIX: {
568 recordUndo(bv, Undo::ATOMIC);
573 istringstream is(STRCONV(argument));
574 is >> m >> n >> v_align >> h_align;
578 mathcursor->niceInsert(
579 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
583 case LFUN_SUPERSCRIPT:
586 recordUndo(bv, Undo::ATOMIC);
587 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
591 case LFUN_MATH_DELIM:
593 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
595 string rs = split(cmd.argument, ls, ' ');
596 // Reasonable default values
602 recordUndo(bv, Undo::ATOMIC);
603 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
607 case LFUN_SPACE_INSERT:
608 case LFUN_MATH_SPACE:
609 recordUndo(bv, Undo::ATOMIC);
610 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
614 bv->owner()->message(_("Invalid action in math mode!"));
618 case LFUN_EXEC_COMMAND:
619 result = UNDISPATCHED;
623 // interpret this as if a backslash was typed
624 recordUndo(bv, Undo::ATOMIC);
625 mathcursor->interpret('\\');
628 case LFUN_BREAKPARAGRAPH:
629 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
630 case LFUN_BREAKPARAGRAPH_SKIP:
634 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
635 // handling such that "self-insert" works on "arbitrary stuff" too, and
636 // math-insert only handles special math things like "matrix".
637 case LFUN_INSERT_MATH:
638 recordUndo(bv, Undo::ATOMIC);
639 mathcursor->niceInsert(argument);
643 case LFUN_SELFINSERT:
644 if (!argument.empty()) {
645 recordUndo(bv, Undo::ATOMIC);
646 if (argument.size() == 1)
647 result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
649 mathcursor->insert(argument);
654 if (mathcursor->selection())
655 mathcursor->selClear();
657 result = UNDISPATCHED;
660 case LFUN_INSET_TOGGLE:
661 mathcursor->insetToggle();
664 case LFUN_DIALOG_SHOW:
665 result = UNDISPATCHED;
668 case LFUN_DIALOG_SHOW_NEW_INSET: {
669 string const & name = argument;
673 data = tmp.createDialogStr(name);
677 result = UNDISPATCHED;
679 bv->owner()->getDialogs().show(name, data, 0);
684 case LFUN_INSET_APPLY: {
685 string const name = cmd.getArg(0);
687 bv->owner()->getDialogs().getOpenInset(name);
690 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
691 result = base->localDispatch(fr);
694 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
695 mathcursor->insert(ar);
698 result = UNDISPATCHED;
705 result = UNDISPATCHED;
708 if (result == DISPATCHED)
709 bv->updateInset(this);
711 mathcursor->normalize();
716 if (mathcursor->selection() || was_selection)
717 toggleInsetSelection(bv);
719 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
720 result == UNDISPATCHED) {
723 cmd.view()->stuffClipboard(mathcursor->grabSelection());
725 releaseMathCursor(bv);
726 bv->unlockInset(this);
728 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
731 return result; // original version
735 void InsetFormulaBase::revealCodes(BufferView * bv) const
739 bv->owner()->message(mathcursor->info());
742 // write something to the minibuffer
743 // translate to latex
744 mathcursor->markInsert();
747 string str = os.str();
748 mathcursor->markErase();
749 string::size_type pos = 0;
751 for (string::iterator it = str.begin(); it != str.end(); ++it) {
754 else if (*it == '\0') {
756 pos = it - str.begin();
762 res = res.substr(pos - 30);
764 res = res.substr(0, 60);
765 bv->owner()->message(res);
770 InsetOld::Code InsetFormulaBase::lyxCode() const
772 return InsetOld::MATH_CODE;
776 int InsetFormulaBase::ylow() const
778 return yo_ - dim_.asc;
782 int InsetFormulaBase::yhigh() const
784 return yo_ + dim_.des;
788 int InsetFormulaBase::xlow() const
794 int InsetFormulaBase::xhigh() const
796 return xo_ + dim_.wid;
800 /////////////////////////////////////////////////////////////////////
803 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
809 static InsetFormulaBase * lastformula = 0;
810 static MathIterator current = MathIterator(ibegin(par().nucleus()));
812 static string laststr;
814 if (lastformula != this || laststr != str) {
815 //lyxerr << "reset lastformula to " << this << endl;
818 current = ibegin(par().nucleus());
820 mathed_parse_cell(ar, str);
824 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
826 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
827 if (it.cell().matchpart(ar, it.back().pos_)) {
828 bv->unlockInset(bv->theLockingInset());
829 if (!bv->lockInset(this)) {
830 lyxerr << "Cannot lock inset" << endl;
834 mathcursor = new MathCursor(this, true);
836 mathcursor->setSelection(it, ar.size());
839 bv->updateInset(this);
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() const
866 return mathcursor ? mathcursor->grabSelection() : string();
869 /////////////////////////////////////////////////////////////////////
872 void mathDispatchCreation(FuncRequest const & cmd, bool display)
874 BufferView * bv = cmd.view();
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->theLockingInset()->
887 localDispatch(FuncRequest(bv, 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->localDispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
893 f->localDispatch(FuncRequest(bv, 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(FuncRequest const & cmd)
913 BufferView * bv = cmd.view();
914 if (!bv->available())
917 switch (cmd.action) {
919 case LFUN_MATH_DISPLAY:
920 mathDispatchCreation(cmd, true);
924 mathDispatchCreation(cmd, false);
927 case LFUN_MATH_IMPORT_SELECTION:
928 mathDispatchCreation(cmd, false);
931 case LFUN_MATH_MACRO:
932 if (cmd.argument.empty())
933 cmd.errorMessage(N_("Missing argument"));
935 string s = cmd.argument;
936 string const s1 = token(s, ' ', 1);
937 int const nargs = s1.empty() ? 0 : atoi(s1);
938 string const s2 = token(s, ' ', 2);
939 string const type = s2.empty() ? "newcommand" : s2;
940 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
944 case LFUN_INSERT_MATH:
945 case LFUN_INSERT_MATRIX:
946 case LFUN_MATH_DELIM: {
947 InsetFormula * f = new InsetFormula(bv);
948 if (openNewInset(bv, f)) {
949 bv->theLockingInset()->
950 localDispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
951 bv->theLockingInset()->localDispatch(cmd);