3 * Purpose: Implementation of common parts of the LyX math insets
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
7 * Copyright: 1996-1998 Alejandro Aguilar Sierra
9 * Version: 0.4, Lyx project.
11 * You are free to use and modify this code under the terms of
12 * the GNU General Public Licence version 2 or later.
19 #include "support/LAssert.h"
22 #pragma implementation
26 #include "formulamacro.h"
27 #include "commandtags.h"
28 #include "BufferView.h"
32 #include "LaTeXFeatures.h"
34 #include "math_support.h"
35 #include "support/lstrings.h"
40 #include "math_arrayinset.h"
41 #include "math_cursor.h"
42 #include "math_factory.h"
43 #include "math_hullinset.h"
44 #include "math_iterator.h"
45 #include "math_macrotable.h"
46 #include "math_parser.h"
48 #include "math_spaceinset.h"
49 #include "undo_funcs.h"
50 #include "frontends/Dialogs.h"
57 MathCursor * mathcursor = 0;
71 void handleFont(BufferView * bv, string const & arg, MathTextCodes t)
73 if (mathcursor->selection())
74 bv->lockedInsetStoreUndo(Undo::EDIT);
75 mathcursor->handleFont(t);
76 for (string::const_iterator it = arg.begin(); it != arg.end(); ++it)
77 mathcursor->insert(*it);
81 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
83 if (!bv->insertInset(new_inset)) {
87 new_inset->edit(bv, 0, 0, 0);
96 InsetFormulaBase::InsetFormulaBase()
97 : view_(0), font_(), xo_(0), yo_(0)
99 // This is needed as long the math parser is not re-entrant
100 MathMacroTable::builtinMacros();
101 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
105 // simply scrap this function if you want
106 void InsetFormulaBase::mutateToText()
109 // translate to latex
111 latex(NULL, os, false, false);
112 string str = os.str();
115 LyXText * lt = view_->getLyXText();
116 string::const_iterator cit = str.begin();
117 string::const_iterator end = str.end();
118 for (; cit != end; ++cit)
119 view_->owner()->getIntl()->getTrans().TranslateAndInsert(*cit, lt);
122 //view_->owner()->getLyXFunc()->dispatch(LFUN_ESCAPE);
127 // Check if uses AMS macros
128 void InsetFormulaBase::validate(LaTeXFeatures &) const
132 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
139 void InsetFormulaBase::metrics(BufferView * bv) const
143 MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
148 string const InsetFormulaBase::editMessage() const
150 return _("Math editor mode");
154 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
156 if (!bv->lockInset(this))
157 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
159 //lyxerr << "edit: " << x << " " << y << " button: " << button << "\n";
161 mathcursor = new MathCursor(this, x == 0);
163 // handle ignored click
164 if (hack_x || hack_y) {
165 insetButtonPress(bv, hack_x, hack_y, hack_button);
170 // if that is removed, we won't get the magenta box when entering an
171 // inset for the first time
172 bv->updateInset(this, false);
176 void InsetFormulaBase::edit(BufferView * bv, bool front)
178 // looks hackish but seems to work
179 edit(bv, front ? 0 : 1, 0, 0);
183 void InsetFormulaBase::insetUnlock(BufferView * bv)
186 if (mathcursor->inMacroMode()) {
187 mathcursor->macroModeClose();
188 updateLocal(bv, true);
193 bv->updateInset(this, false);
197 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
199 mathcursor->getPos(x, y);
202 //lyxerr << "getCursorPos: " << x << " " << y << "\n";
206 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
211 if (isCursorVisible())
212 bv->hideLockedInsetCursor();
217 mathcursor->getPos(x, y);
221 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
222 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
223 bv->showLockedInsetCursor(x, y, asc, des);
224 //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
227 toggleCursorVisible();
231 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
233 if (isCursorVisible())
239 mathcursor->getPos(x, y);
243 MathMetricsInfo mi(bv, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
244 math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
245 bv->fitLockedInsetCursor(x, y, asc, des);
246 //lyxerr << "showInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << "\n";
248 toggleInsetCursor(bv);
252 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
254 if (isCursorVisible())
255 toggleInsetCursor(bv);
259 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
262 bv->updateInset(this, false);
266 vector<string> const InsetFormulaBase::getLabelList() const
268 return vector<string>();
272 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
275 bv->updateInset(this, dirty);
279 bool InsetFormulaBase::insetButtonRelease(BufferView * bv,
280 int /*x*/, int /*y*/, int /*button*/)
284 //lyxerr << "insetButtonRelease: " << x << " " << y << "\n";
287 bv->updateInset(this, false);
292 void InsetFormulaBase::insetButtonPress(BufferView * bv,
293 int x, int y, int button)
295 // hack to cope with mouseclick that comes before the call to edit()
299 hack_button = button;
303 lyxerr << "insetButtonPress: " << x + xo_ << " " << y + yo_
304 << " but: " << button << "\n";
312 mathcursor->selClear();
313 mathcursor->setPos(x + xo_, y + yo_);
318 lyxerr << "insetButtonPress: 2\n";
321 bv->lockedInsetStoreUndo(Undo::EDIT);
323 mathcursor->selGet(ar);
324 mathcursor->setPos(x + xo_, y + yo_);
326 bv->getLyXText()->selectionAsString(bv->buffer(), false);
327 mathed_parse_cell(ar, sel);
328 mathcursor->insert(ar);
333 // launch math panel for right mouse button
334 bv->owner()->getDialogs()->showMathPanel();
337 bv->updateInset(this, false);
341 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
342 int x, int y, int /*button*/)
347 if (abs(x - first_x) < 2 && abs(y - first_y) < 2) {
348 //lyxerr << "insetMotionNotify: ignored\n";
354 if (!mathcursor->selection())
355 mathcursor->selStart();
357 //lyxerr << "insetMotionNotify: " << x + xo_ << ' ' << y + yo_
358 // << ' ' << button << "\n";
360 mathcursor->setPos(x + xo_, y + yo_);
362 bv->updateInset(this, false);
366 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
368 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
372 UpdatableInset::RESULT
373 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
376 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
377 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
382 if (mathcursor->asHyperActiveInset()) {
383 lyxerr << " uurr.... getting dificult now\n";
384 return mathcursor->asHyperActiveInset()->localDispatch(bv, action, arg);
387 RESULT result = DISPATCHED;
389 bool was_macro = mathcursor->inMacroMode();
390 bool was_selection = mathcursor->selection();
394 mathcursor->normalize();
399 // --- Cursor Movements ---------------------------------------------
402 sel = true; // fall through...
405 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
406 //lyxerr << "calling scroll 20\n";
408 updateLocal(bv, false);
409 // write something to the minibuffer
410 //bv->owner()->message(mathcursor->info());
415 sel = true; // fall through
418 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
419 updateLocal(bv, false);
427 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
428 updateLocal(bv, false);
436 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
437 updateLocal(bv, false);
444 mathcursor->home(sel);
445 updateLocal(bv, false);
452 mathcursor->end(sel);
453 updateLocal(bv, false);
456 case LFUN_DELETE_LINE_FORWARD:
457 bv->lockedInsetStoreUndo(Undo::DELETE);
458 mathcursor->delLine();
459 updateLocal(bv, true);
463 mathcursor->idxNext();
464 updateLocal(bv, false);
468 mathcursor->idxPrev();
469 updateLocal(bv, false);
473 bv->lockedInsetStoreUndo(Undo::EDIT);
474 mathcursor->splitCell();
475 updateLocal(bv, true);
478 case LFUN_DELETE_WORD_BACKWARD:
480 bv->lockedInsetStoreUndo(Undo::DELETE);
481 mathcursor->backspace();
482 bv->updateInset(this, true);
485 case LFUN_DELETE_WORD_FORWARD:
487 bv->lockedInsetStoreUndo(Undo::DELETE);
489 bv->updateInset(this, true);
493 // sprintf(dispatch_buffer, "%d %d",);
494 // dispatch_result = dispatch_buffer;
497 lyxerr << "LFUN_SETXY broken!\n";
500 istringstream is(arg.c_str());
502 mathcursor->setPos(x, y);
503 updateLocal(bv, false);
509 mathcursor->macroModeClose();
510 bv->lockedInsetStoreUndo(Undo::EDIT);
511 mathcursor->selPaste();
512 updateLocal(bv, true);
516 bv->lockedInsetStoreUndo(Undo::DELETE);
517 mathcursor->selCut();
518 updateLocal(bv, true);
522 mathcursor->selCopy();
525 case LFUN_WORDRIGHTSEL:
526 case LFUN_WORDLEFTSEL:
529 // Special casing for superscript in case of LyX handling
531 case LFUN_CIRCUMFLEX:
533 // do superscript if LyX handles
535 bv->lockedInsetStoreUndo(Undo::EDIT);
536 mathcursor->script(true);
537 updateLocal(bv, true);
553 case LFUN_HUNG_UMLAUT:
557 case LFUN_GREEK_TOGGLE: handleFont(bv, arg, LM_TC_GREEK); break;
558 case LFUN_BOLD: handleFont(bv, arg, LM_TC_BF); break;
559 case LFUN_SANS: handleFont(bv, arg, LM_TC_SF); break;
560 case LFUN_EMPH: handleFont(bv, arg, LM_TC_CAL); break;
561 case LFUN_ROMAN: handleFont(bv, arg, LM_TC_RM); break;
562 case LFUN_CODE: handleFont(bv, arg, LM_TC_TT); break;
563 case LFUN_FRAK: handleFont(bv, arg, LM_TC_EUFRAK); break;
564 case LFUN_ITAL: handleFont(bv, arg, LM_TC_IT); break;
565 case LFUN_NOUN: handleFont(bv, arg, LM_TC_BB); break;
566 case LFUN_DEFAULT: handleFont(bv, arg, LM_TC_VAR); break;
567 case LFUN_FREE: handleFont(bv, arg, LM_TC_TEXTRM); break;
570 handleFont(bv, arg, LM_TC_GREEK1);
572 mathcursor->interpret(arg);
577 handleFont(bv, arg, LM_TC_TEXTRM);
581 mathcursor->niceInsert(MathAtom(new MathHullInset(LM_OT_SIMPLE)));
582 updateLocal(bv, true);
585 //bv->owner()->message(_("math text mode toggled"));
588 case LFUN_MATH_LIMITS:
589 bv->lockedInsetStoreUndo(Undo::EDIT);
590 if (mathcursor->toggleLimits())
591 updateLocal(bv, true);
597 bv->lockedInsetStoreUndo(Undo::EDIT);
598 mathcursor->setSize(arg);
599 updateLocal(bv, true);
604 case LFUN_INSERT_MATRIX:
606 bv->lockedInsetStoreUndo(Undo::EDIT);
607 mathcursor->interpret("matrix " + arg);
608 updateLocal(bv, true);
612 case LFUN_MATH_SPACE:
614 bv->lockedInsetStoreUndo(Undo::EDIT);
615 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
616 updateLocal(bv, true);
620 case LFUN_SUPERSCRIPT:
623 bv->lockedInsetStoreUndo(Undo::EDIT);
624 mathcursor->script((action == LFUN_SUPERSCRIPT));
625 updateLocal(bv, true);
629 case LFUN_MATH_DELIM:
631 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
634 istringstream is(arg.c_str());
637 lyxerr << "can't parse delimeters from '" << arg << "'\n";
640 bv->lockedInsetStoreUndo(Undo::EDIT);
641 mathcursor->handleDelim(ls, rs);
642 updateLocal(bv, true);
646 case LFUN_PROTECTEDSPACE:
647 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
648 bv->lockedInsetStoreUndo(Undo::EDIT);
649 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
650 updateLocal(bv, true);
654 bv->owner()->message(_("Invalid action in math mode!"));
658 case LFUN_MATH_HALIGN:
659 case LFUN_MATH_VALIGN:
660 case LFUN_MATH_ROW_INSERT:
661 case LFUN_MATH_ROW_DELETE:
662 case LFUN_MATH_COLUMN_INSERT:
663 case LFUN_MATH_COLUMN_DELETE:
665 MathInset::idx_type idx = 0;
666 MathGridInset * p = mathcursor ? mathcursor->enclosingGrid(idx) : 0;
668 bv->lockedInsetStoreUndo(Undo::EDIT);
669 char al = arg.size() ? arg[0] : 'c';
671 case LFUN_MATH_HALIGN: p->halign(al, p->col(idx)); break;
672 case LFUN_MATH_VALIGN: p->valign(al); break;
673 case LFUN_MATH_ROW_INSERT: p->addRow(p->row(idx)); break;
674 case LFUN_MATH_ROW_DELETE: p->delRow(p->row(idx)); break;
675 case LFUN_MATH_COLUMN_INSERT: p->addCol(p->col(idx)); break;
676 case LFUN_MATH_COLUMN_DELETE: p->delCol(p->col(idx)); break;
679 updateLocal(bv, true);
684 case LFUN_EXEC_COMMAND:
685 result = UNDISPATCHED;
688 case LFUN_BREAKPARAGRAPH:
689 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
690 //lyxerr << "LFUN ignored\n";
694 // interpret this as if a backslash was typed
695 bv->lockedInsetStoreUndo(Undo::EDIT);
696 mathcursor->interpret("\\");
697 updateLocal(bv, true);
701 case LFUN_INSERT_MATH:
702 case LFUN_SELFINSERT:
704 bv->lockedInsetStoreUndo(Undo::EDIT);
705 result = mathcursor->interpret(arg) ? DISPATCHED : FINISHED_RIGHT;
706 updateLocal(bv, true);
710 case LFUN_MATH_PANEL:
711 result = UNDISPATCHED;
715 if (mathcursor->selection())
716 mathcursor->selClear();
718 result = UNDISPATCHED;
722 result = UNDISPATCHED;
725 mathcursor->normalize();
728 lyx::Assert(mathcursor);
730 if (mathcursor->selection() || was_selection)
731 toggleInsetSelection(bv);
733 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
734 result == UNDISPATCHED)
737 bv->unlockInset(this);
741 return result; // original version
745 void InsetFormulaBase::revealCodes(BufferView * /*bv*/) const
750 // write something to the minibuffer
751 // translate to latex
752 mathcursor->markInsert();
755 string str = os.str();
756 mathcursor->markErase();
757 string::size_type pos = 0;
759 for (string::iterator it = str.begin(); it != str.end(); ++it) {
762 else if (*it == '\0') {
764 pos = it - str.begin();
770 res = res.substr(pos - 30);
772 res = res.substr(0, 60);
773 bv->owner()->message(res);
778 Inset::Code InsetFormulaBase::lyxCode() const
780 return Inset::MATH_CODE;
784 int InsetFormulaBase::ylow() const
786 return yo_ - ascent(view_, font_);
790 int InsetFormulaBase::yhigh() const
792 return yo_ + descent(view_, font_);
796 int InsetFormulaBase::xlow() const
802 int InsetFormulaBase::xhigh() const
804 return xo_ + width(view_, font_);
808 /////////////////////////////////////////////////////////////////////
811 bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
817 static InsetFormulaBase * lastformula = 0;
818 static MathIterator current = MathIterator(ibegin(par().nucleus()));
820 static string laststr;
822 if (lastformula != this || laststr != str) {
823 //lyxerr << "reset lastformula to " << this << "\n";
826 current = ibegin(par().nucleus());
828 mathed_parse_cell(ar, str);
832 //lyxerr << "searching '" << str << "' in " << this << ar << endl;
834 for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
835 if (it.cell().matchpart(ar, it.position().pos_)) {
836 mathcursor->setSelection(it.cursor(), ar.size());
839 // I guess some of the following can go
840 bv->toggleSelection(true);
842 updateLocal(bv, true);
849 //lyxerr << "not found!\n";
851 // we have to unlock ourself in this function by default!
852 // don't ask me why...
853 bv->unlockInset(this);
858 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
861 lyxerr << "searching backward not implemented in mathed" << endl;
862 return searchForward(bv, what, a, b);
866 /////////////////////////////////////////////////////////////////////
869 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
871 if (bv->available()) {
872 // use selection if available..
874 //if (action == LFUN_MATH_IMPORT_SELECTION)
878 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
880 InsetFormulaBase * f;
882 f = new InsetFormula;
883 if (openNewInset(bv, f)) {
884 // don't do that also for LFUN_MATH_MODE unless you want end up with
885 // always changing to mathrm when opening an inlined inset
886 // -- I really hate "LyXfunc overloading"...
888 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
889 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
892 // create a macro if we see "\\newcommand" somewhere, and an ordinary
894 if (sel.find("\\newcommand") == string::npos)
895 f = new InsetFormula(sel);
898 if (!mathed_parse_macro(name, sel))
900 f = new InsetFormulaMacro(sel);
902 bv->getLyXText()->cutSelection(bv);
906 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
910 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
912 mathDispatchCreation(bv, arg, true);
916 void mathDispatchMathMode(BufferView * bv, string const & arg)
918 mathDispatchCreation(bv, arg, false);
922 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
924 mathDispatchCreation(bv, arg, true);
928 void mathDispatchMathMacro(BufferView * bv, string const & arg)
930 if (bv->available()) {
932 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
935 string const s1 = token(s, ' ', 1);
936 int const na = s1.empty() ? 0 : lyx::atoi(s1);
937 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
943 void mathDispatchMathDelim(BufferView * bv, string const & arg)
945 if (bv->available()) {
946 if (openNewInset(bv, new InsetFormula))
947 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
952 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
954 if (bv->available()) {
955 if (openNewInset(bv, new InsetFormula))
956 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
961 void mathDispatchInsertMath(BufferView * bv, string const & arg)
963 if (bv->available()) {
964 if (arg.size() && arg[0] == '\\') {
965 InsetFormula * f = new InsetFormula(arg);
966 if (!bv->insertInset(f))
969 mathDispatchMathMode(bv, arg);
974 void mathDispatchGreek(BufferView * bv, string const & arg)
976 if (bv->available()) {
977 InsetFormula * f = new InsetFormula;
978 if (openNewInset(bv, f)) {
979 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);
986 void mathDispatch(BufferView *, kb_action, string const &)