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 bv->updateInset(this);
162 releaseMathCursor(bv);
165 generatePreview(*bv->buffer());
166 bv->updateInset(this);
170 void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
172 mathcursor->getPos(x, y);
176 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
178 // calling metrics here destroys the cached xo,yo positions e.g. in
179 // MathParboxinset. And it would be too expensive anyway...
182 lyxerr << "getCursorPos - should not happen";
186 mathcursor->getPos(x, y);
187 x = mathcursor->targetX();
190 lyxerr << "InsetFormulaBase::getCursorPos: " << x << ' ' << y << endl;
194 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
201 //math_font_max_dim(font_, asc, des);
202 getCursorPos(bv, x, y);
204 //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
205 bv->fitLockedInsetCursor(x, y, asc, des);
209 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
212 bv->updateInset(this);
216 DispatchResult InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
219 return DispatchResult(false);
221 BufferView * bv = cmd.view();
222 bv->updateInset(this);
223 //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
225 if (cmd.button() == mouse_button::button3) {
226 // try to dispatch to enclosed insets first
227 if (!mathcursor->dispatch(cmd).dispatched()) {
228 // launch math panel for right mouse button
229 lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
230 bv->owner()->getDialogs().show("mathpanel");
232 return DispatchResult(true, true);
235 if (cmd.button() == mouse_button::button2) {
237 asArray(bv->getClipboard(), ar);
238 mathcursor->selClear();
239 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
240 mathcursor->insert(ar);
241 bv->updateInset(this);
242 return DispatchResult(true, true);
245 if (cmd.button() == mouse_button::button1) {
246 // try to dispatch to enclosed insets first
247 mathcursor->dispatch(cmd);
248 cmd.view()->stuffClipboard(mathcursor->grabSelection());
249 // try to set the cursor
251 //mathcursor = new MathCursor(this, x == 0);
253 //mathcursor->setPos(x + xo_, y + yo_);
254 return DispatchResult(true, true);
257 return DispatchResult(false);
261 DispatchResult InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
263 BufferView * bv = cmd.view();
264 //lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
266 if (!mathcursor || mathcursor->formula() != this) {
267 lyxerr[Debug::MATHED] << "re-create cursor" << endl;
268 releaseMathCursor(bv);
269 mathcursor = new MathCursor(this, cmd.x == 0);
271 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
274 if (cmd.button() == mouse_button::button3) {
275 mathcursor->dispatch(cmd);
276 return DispatchResult(true, true);
279 if (cmd.button() == mouse_button::button1) {
282 mathcursor->selClear();
283 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
284 mathcursor->dispatch(cmd);
285 return DispatchResult(true, true);
288 bv->updateInset(this);
289 return DispatchResult(true, true);
293 DispatchResult InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
296 return DispatchResult(true, true);
298 if (mathcursor->dispatch(FuncRequest(cmd)).dispatched())
299 return DispatchResult(true, true);
301 // only select with button 1
302 if (cmd.button() != mouse_button::button1)
303 return DispatchResult(true, true);
305 if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
306 return DispatchResult(true, true);
311 if (!mathcursor->selection())
312 mathcursor->selStart();
314 BufferView * bv = cmd.view();
315 mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
316 bv->updateInset(this);
317 return DispatchResult(true, true);
321 void InsetFormulaBase::edit(BufferView * bv, bool left)
323 lyxerr << "Called FormulaBase::edit" << endl;
324 releaseMathCursor(bv);
325 mathcursor = new MathCursor(this, left);
326 bv->cursor().push(this);
327 // if that is removed, we won't get the magenta box when entering an
328 // inset for the first time
329 bv->updateInset(this);
333 void InsetFormulaBase::edit(BufferView * bv, int x, int y)
335 lyxerr << "Called FormulaBase::EDIT with '" << x << ' ' << y << "'" << endl;
336 releaseMathCursor(bv);
337 mathcursor = new MathCursor(this, true);
339 mathcursor->setPos(x + xo_, y + yo_);
340 bv->cursor().push(this);
341 // if that is removed, we won't get the magenta box when entering an
342 // inset for the first time
343 bv->updateInset(this);
348 InsetFormulaBase::priv_dispatch(FuncRequest const & cmd,
349 idx_type &, pos_type &)
351 //lyxerr << "InsetFormulaBase::localDispatch: act: " << cmd.action
352 // << " arg: '" << cmd.argument
353 // << "' x: '" << cmd.x
354 // << " y: '" << cmd.y
355 // << "' button: " << cmd.button() << endl;
357 BufferView * bv = cmd.view();
359 // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
360 bool remove_inset = false;
362 switch (cmd.action) {
363 case LFUN_MOUSE_PRESS:
364 //lyxerr << "Mouse single press" << endl;
365 return lfunMousePress(cmd);
366 case LFUN_MOUSE_MOTION:
367 //lyxerr << "Mouse motion" << endl;
368 return lfunMouseMotion(cmd);
369 case LFUN_MOUSE_RELEASE:
370 //lyxerr << "Mouse single release" << endl;
371 return lfunMouseRelease(cmd);
372 case LFUN_MOUSE_DOUBLE:
373 //lyxerr << "Mouse double" << endl;
374 return dispatch(FuncRequest(LFUN_WORDSEL));
380 return DispatchResult(false);
382 string argument = cmd.argument;
383 DispatchResult result(true);
385 bool was_macro = mathcursor->inMacroMode();
386 bool was_selection = mathcursor->selection();
388 mathcursor->normalize();
391 switch (cmd.action) {
393 case LFUN_MATH_MUTATE:
394 case LFUN_MATH_DISPLAY:
395 case LFUN_MATH_NUMBER:
396 case LFUN_MATH_NONUMBER:
397 case LFUN_CELL_SPLIT:
399 case LFUN_DELETE_LINE_FORWARD:
400 case LFUN_INSERT_LABEL:
401 case LFUN_MATH_EXTERN:
402 case LFUN_TABULAR_FEATURE:
403 case LFUN_PASTESELECTION:
404 case LFUN_MATH_LIMITS:
405 recordUndo(bv, Undo::ATOMIC);
406 mathcursor->dispatch(cmd);
410 sel = true; // fall through...
412 result = mathcursor->right(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
413 //lyxerr << "calling scroll 20" << endl;
415 // write something to the minibuffer
416 //bv->owner()->message(mathcursor->info());
420 sel = true; // fall through
422 result = mathcursor->left(sel) ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
426 sel = true; // fall through
428 result = mathcursor->up(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_UP);
432 sel = true; // fall through
434 result = mathcursor->down(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_DOWN);
438 mathcursor->home(false);
439 mathcursor->end(true);
442 case LFUN_UP_PARAGRAPHSEL:
443 case LFUN_UP_PARAGRAPH:
444 case LFUN_DOWN_PARAGRAPHSEL:
445 case LFUN_DOWN_PARAGRAPH:
446 result = DispatchResult(true, FINISHED);
450 case LFUN_WORDLEFTSEL:
451 sel = true; // fall through
454 result = mathcursor->home(sel) ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
458 case LFUN_WORDRIGHTSEL:
459 sel = true; // fall through
462 result = mathcursor->end(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
467 case LFUN_BEGINNINGBUFSEL:
468 case LFUN_BEGINNINGBUF:
469 result = DispatchResult(true, FINISHED);
476 result = DispatchResult(false, FINISHED_RIGHT);
479 case LFUN_CELL_FORWARD:
480 mathcursor->idxNext();
483 case LFUN_CELL_BACKWARD:
484 mathcursor->idxPrev();
487 case LFUN_DELETE_WORD_BACKWARD:
489 recordUndo(bv, Undo::ATOMIC);
490 if (!mathcursor->backspace()) {
491 result = DispatchResult(true, FINISHED);
496 case LFUN_DELETE_WORD_FORWARD:
498 recordUndo(bv, Undo::ATOMIC);
499 if (!mathcursor->erase()) {
500 result = DispatchResult(true, FINISHED);
506 // sprintf(dispatch_buffer, "%d %d",);
507 // DispatchResult= dispatch_buffer;
510 lyxerr << "LFUN_SETXY broken!" << endl;
513 istringstream is(cmd.argument.c_str());
515 mathcursor->setPos(x, y);
521 istringstream is(cmd.argument.c_str());
524 mathcursor->macroModeClose();
525 recordUndo(bv, Undo::ATOMIC);
526 mathcursor->selPaste(n);
531 recordUndo(bv, Undo::DELETE);
532 mathcursor->selCut();
536 mathcursor->selCopy();
540 // Special casing for superscript in case of LyX handling
542 case LFUN_CIRCUMFLEX:
543 if (cmd.argument.empty()) {
544 // do superscript if LyX handles
546 recordUndo(bv, Undo::ATOMIC);
547 mathcursor->script(true);
564 case LFUN_HUNG_UMLAUT:
568 case LFUN_FREEFONT_APPLY:
569 case LFUN_FREEFONT_UPDATE:
570 handleFont2(bv, cmd.argument);
573 case LFUN_BOLD: handleFont(bv, cmd.argument, "mathbf"); break;
574 case LFUN_SANS: handleFont(bv, cmd.argument, "mathsf"); break;
575 case LFUN_EMPH: handleFont(bv, cmd.argument, "mathcal"); break;
576 case LFUN_ROMAN: handleFont(bv, cmd.argument, "mathrm"); break;
577 case LFUN_CODE: handleFont(bv, cmd.argument, "texttt"); break;
578 case LFUN_FRAK: handleFont(bv, cmd.argument, "mathfrak"); break;
579 case LFUN_ITAL: handleFont(bv, cmd.argument, "mathit"); break;
580 case LFUN_NOUN: handleFont(bv, cmd.argument, "mathbb"); break;
581 //case LFUN_FREEFONT_APPLY: handleFont(bv, cmd.argument, "textrm"); break;
582 case LFUN_DEFAULT: handleFont(bv, cmd.argument, "textnormal"); break;
585 if (mathcursor->currentMode() == MathInset::TEXT_MODE)
586 mathcursor->niceInsert(MathAtom(new MathHullInset("simple")));
588 handleFont(bv, cmd.argument, "textrm");
589 //bv->owner()->message(_("math text mode toggled"));
595 recordUndo(bv, Undo::ATOMIC);
596 mathcursor->setSize(arg);
601 case LFUN_INSERT_MATRIX: {
602 recordUndo(bv, Undo::ATOMIC);
607 istringstream is(argument);
608 is >> m >> n >> v_align >> h_align;
612 mathcursor->niceInsert(
613 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
617 case LFUN_SUPERSCRIPT:
620 recordUndo(bv, Undo::ATOMIC);
621 mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
625 case LFUN_MATH_DELIM:
627 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
629 string rs = split(cmd.argument, ls, ' ');
630 // Reasonable default values
636 recordUndo(bv, Undo::ATOMIC);
637 mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
641 case LFUN_SPACE_INSERT:
642 case LFUN_MATH_SPACE:
643 recordUndo(bv, Undo::ATOMIC);
644 mathcursor->insert(MathAtom(new MathSpaceInset(",")));
648 bv->owner()->message(_("Invalid action in math mode!"));
652 case LFUN_EXEC_COMMAND:
653 result = DispatchResult(false);
657 // interpret this as if a backslash was typed
658 recordUndo(bv, Undo::ATOMIC);
659 mathcursor->interpret('\\');
662 case LFUN_BREAKPARAGRAPH:
663 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
664 case LFUN_BREAKPARAGRAPH_SKIP:
668 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
669 // handling such that "self-insert" works on "arbitrary stuff" too, and
670 // math-insert only handles special math things like "matrix".
671 case LFUN_INSERT_MATH:
672 recordUndo(bv, Undo::ATOMIC);
673 mathcursor->niceInsert(argument);
677 case LFUN_SELFINSERT:
678 if (!argument.empty()) {
679 recordUndo(bv, Undo::ATOMIC);
680 if (argument.size() == 1)
681 result = mathcursor->interpret(argument[0]) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
683 mathcursor->insert(argument);
688 if (mathcursor->selection())
689 mathcursor->selClear();
691 result = DispatchResult(false);
694 case LFUN_INSET_TOGGLE:
695 mathcursor->insetToggle();
698 case LFUN_DIALOG_SHOW:
699 result = DispatchResult(false);
702 case LFUN_DIALOG_SHOW_NEW_INSET: {
703 string const & name = argument;
707 data = tmp.createDialogStr(name);
711 result = DispatchResult(false);
713 bv->owner()->getDialogs().show(name, data, 0);
717 case LFUN_INSET_APPLY: {
718 string const name = cmd.getArg(0);
720 bv->owner()->getDialogs().getOpenInset(name);
723 FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument);
724 result = base->dispatch(fr);
727 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
728 mathcursor->insert(ar);
729 result = DispatchResult(true, true);
731 result = DispatchResult(false);
738 result = DispatchResult(false);
741 if (result == DispatchResult(true, true))
742 bv->updateInset(this);
744 mathcursor->normalize();
747 BOOST_ASSERT(mathcursor);
749 if (mathcursor->selection() || was_selection)
750 toggleInsetSelection(bv);
752 if (result.dispatched()) {
755 cmd.view()->stuffClipboard(mathcursor->grabSelection());
757 releaseMathCursor(bv);
759 bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
762 return result; // original version
766 void InsetFormulaBase::revealCodes(BufferView * bv) const
770 bv->owner()->message(mathcursor->info());
773 // write something to the minibuffer
774 // translate to latex
775 mathcursor->markInsert();
778 string str = os.str();
779 mathcursor->markErase();
780 string::size_type pos = 0;
782 for (string::iterator it = str.begin(); it != str.end(); ++it) {
785 else if (*it == '\0') {
787 pos = it - str.begin();
793 res = res.substr(pos - 30);
795 res = res.substr(0, 60);
796 bv->owner()->message(res);
801 InsetOld::Code InsetFormulaBase::lyxCode() const
803 return InsetOld::MATH_CODE;
807 int InsetFormulaBase::ylow() const
809 return yo_ - dim_.asc;
813 int InsetFormulaBase::yhigh() const
815 return yo_ + dim_.des;
819 int InsetFormulaBase::xlow() const
825 int InsetFormulaBase::xhigh() const
827 return xo_ + dim_.wid;
831 /////////////////////////////////////////////////////////////////////
834 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
840 static InsetFormulaBase * lastformula = 0;
841 static MathIterator current = MathIterator(ibegin(par().nucleus()));
843 static string laststr;
845 if (lastformula != this || laststr != str) {
846 //lyxerr << "reset lastformula to " << this << endl;
849 current = ibegin(par().nucleus());
851 mathed_parse_cell(ar, str);
855 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
857 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
858 if (it.cell().matchpart(ar, it.back().pos_)) {
860 mathcursor = new MathCursor(this, true);
862 mathcursor->setSelection(it, ar.size());
865 bv->updateInset(this);
870 //lyxerr << "not found!" << endl;
876 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
879 lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
880 return searchForward(bv, what, a, b);
884 bool InsetFormulaBase::display() const
886 return par()->asHullInset() && par()->asHullInset()->display();
890 string InsetFormulaBase::selectionAsString() const
892 return mathcursor ? mathcursor->grabSelection() : string();
895 /////////////////////////////////////////////////////////////////////
898 void mathDispatchCreation(FuncRequest const & cmd, bool display)
900 BufferView * bv = cmd.view();
901 // use selection if available..
903 //if (action == LFUN_MATH_IMPORT_SELECTION)
907 string sel = bv->getLyXText()->selectionAsString(*bv->buffer(), false);
910 InsetFormula * f = new InsetFormula(bv);
911 if (openNewInset(bv, f)) {
912 bv->cursor().innerInset()->
913 dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
914 // don't do that also for LFUN_MATH_MODE unless you want end up with
915 // always changing to mathrm when opening an inlined inset
916 // -- I really hate "LyXfunc overloading"...
918 f->dispatch(FuncRequest(bv, LFUN_MATH_DISPLAY));
919 f->dispatch(FuncRequest(bv, LFUN_INSERT_MATH, cmd.argument));
922 // create a macro if we see "\\newcommand" somewhere, and an ordinary
924 InsetFormulaBase * f;
925 if (sel.find("\\newcommand") == string::npos &&
926 sel.find("\\def") == string::npos)
927 f = new InsetFormula(sel);
929 f = new InsetFormulaMacro(sel);
930 bv->getLyXText()->cutSelection(true, false);
933 cmd.message(N_("Math editor mode"));
937 void mathDispatch(FuncRequest const & cmd)
939 BufferView * bv = cmd.view();
940 if (!bv->available())
943 switch (cmd.action) {
945 case LFUN_MATH_DISPLAY:
946 mathDispatchCreation(cmd, true);
950 mathDispatchCreation(cmd, false);
953 case LFUN_MATH_IMPORT_SELECTION:
954 mathDispatchCreation(cmd, false);
957 case LFUN_MATH_MACRO:
958 if (cmd.argument.empty())
959 cmd.errorMessage(N_("Missing argument"));
961 string s = cmd.argument;
962 string const s1 = token(s, ' ', 1);
963 int const nargs = s1.empty() ? 0 : atoi(s1);
964 string const s2 = token(s, ' ', 2);
965 string const type = s2.empty() ? "newcommand" : s2;
966 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
970 case LFUN_INSERT_MATH:
971 case LFUN_INSERT_MATRIX:
972 case LFUN_MATH_DELIM: {
973 InsetFormula * f = new InsetFormula(bv);
974 if (openNewInset(bv, f)) {
975 UpdatableInset * inset = bv->cursor().innerInset();
976 inset->dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
977 inset->dispatch(cmd);