3 * Purpose: Implementation of formula inset
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
6 * Description: Allows the edition of math paragraphs inside Lyx.
8 * Copyright: 1996-1998 Alejandro Aguilar Sierra
10 * Version: 0.4, Lyx project.
12 * You are free to use and modify this code under the terms of
13 * the GNU General Public Licence version 2 or later.
22 #pragma implementation
26 #include "formulamacro.h"
27 #include "commandtags.h"
28 #include "math_cursor.h"
29 #include "math_parser.h"
30 #include "BufferView.h"
34 #include "LaTeXFeatures.h"
36 #include "support/LOstream.h"
40 #include "math_arrayinset.h"
41 #include "math_spaceinset.h"
42 #include "math_macrotable.h"
43 #include "support/lyxlib.h"
44 #include "mathed/support.h"
45 #include "undo_funcs.h"
51 extern char const * latex_special_chars;
53 int greek_kb_flag = 0;
54 extern char const * latex_mathenv[];
55 MathCursor * mathcursor = 0;
66 void mathed_init_fonts();
68 string nicelabel(string const & label)
70 return "(" + (label.empty() ? "#" : label) + ")";
73 void handleFont(BufferView * bv, MathTextCodes t)
75 if (mathcursor->selection())
76 bv->lockedInsetStoreUndo(Undo::EDIT);
77 mathcursor->handleFont(t);
80 void handleAccent(BufferView * bv, string const & name)
82 bv->lockedInsetStoreUndo(Undo::EDIT);
83 mathcursor->handleAccent(name);
86 void handleDelim(BufferView * bv, int l, int r)
88 bv->lockedInsetStoreUndo(Undo::EDIT);
89 mathcursor->handleDelim(l, r);
92 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
94 LyXText * lt = bv->getLyXText();
98 if (!bv->insertInset(new_inset)) {
102 new_inset->edit(bv, 0, 0, 0);
114 // returns the nearest enclosing matrix
115 MathArrayInset * matrixpar(int & idx)
119 static_cast<MathArrayInset *>
120 (mathcursor ? mathcursor->enclosing(LM_OT_MATRIX, idx) : 0);
127 InsetFormulaBase::InsetFormulaBase(MathInset * par)
131 #warning This is needed as long the math parser is not re-entrant
133 MathMacroTable::builtinMacros();
137 InsetFormulaBase::InsetFormulaBase(InsetFormulaBase const & f)
138 : UpdatableInset(f), par_(static_cast<MathInset *>(f.par_->clone()))
142 InsetFormulaBase::~InsetFormulaBase()
148 void InsetFormulaBase::read(Buffer const *, LyXLex & lex)
153 void InsetFormulaBase::write(Buffer const *, ostream & os) const
158 int InsetFormulaBase::latex(Buffer const *, ostream & os,
159 bool fragile, bool spacing) const
161 return latex(os, fragile, spacing);
164 int InsetFormulaBase::ascii(Buffer const *, ostream & os, int spacing) const
166 return ascii(os, spacing);
169 int InsetFormulaBase::linuxdoc(Buffer const *, ostream & os) const
174 int InsetFormulaBase::docBook(Buffer const *, ostream & os) const
181 // Check if uses AMS macros
182 void InsetFormulaBase::validate(LaTeXFeatures &) const
186 string const InsetFormulaBase::editMessage() const
188 return _("Math editor mode");
192 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
194 mathcursor = new MathCursor(this);
196 if (!bv->lockInset(this))
197 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
200 //bv->updateInset(this, false);
211 void InsetFormulaBase::edit(BufferView * bv, bool front)
213 #warning Please have a look if this is right (Jug)
214 edit(bv, front ? 0 : 1, 0, 0);
218 void InsetFormulaBase::insetUnlock(BufferView * bv)
221 if (mathcursor->inMacroMode()) {
222 mathcursor->macroModeClose();
223 updateLocal(bv, true);
228 bv->updateInset(this, false);
232 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
234 mathcursor->getPos(x, y);
240 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
245 if (isCursorVisible())
246 bv->hideLockedInsetCursor();
250 mathcursor->getPos(x, y);
255 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
256 bv->showLockedInsetCursor(x, y, asc, desc);
259 toggleCursorVisible();
263 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
265 if (!isCursorVisible()) {
269 mathcursor->getPos(x, y);
274 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
275 bv->fitLockedInsetCursor(x, y, asc, desc);
277 toggleInsetCursor(bv);
282 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
284 if (isCursorVisible())
285 toggleInsetCursor(bv);
289 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
292 bv->updateInset(this, false);
296 vector<string> const InsetFormulaBase::getLabelList() const
298 return std::vector<string>();
302 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
305 bv->updateInset(this, dirty);
309 void InsetFormulaBase::metrics() const
311 const_cast<MathInset *>(par_)->metrics(LM_ST_TEXT);
315 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
316 int x, int y, int /*button*/)
322 mathcursor->setPos(x, y);
329 bv->updateInset(this, false);
334 void InsetFormulaBase::insetButtonPress(BufferView * bv,
335 int x, int y, int /*button*/)
340 if (mathcursor && mathcursor->selection()) {
341 mathcursor->selClear();
342 bv->updateInset(this, false);
347 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
348 int x, int y, int /*button*/)
350 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
353 mathcursor->setPos(sel_x + par_->xo(), sel_y + par_->yo());
354 mathcursor->selStart();
356 mathcursor->getPos(sel_x, sel_y);
357 } else if (sel_flag) {
361 mathcursor->setPos(x, y);
363 mathcursor->getPos(x, y);
364 if (sel_x != x || sel_y != y)
365 bv->updateInset(this, false);
372 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
374 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
379 UpdatableInset::RESULT
380 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
383 //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
384 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
389 MathTextCodes varcode = LM_TC_MIN;
390 bool was_macro = mathcursor->inMacroMode();
392 bool was_selection = mathcursor->selection();
393 RESULT result = DISPATCHED;
397 if (mathcursor->getLastCode() == LM_TC_TEX)
400 mathcursor->normalize();
404 // --- Cursor Movements ---------------------------------------------
407 sel = true; // fall through...
410 result = DISPATCH_RESULT(mathcursor->right(sel));
411 updateLocal(bv, false);
416 sel = true; // fall through
419 result = DISPATCH_RESULT(mathcursor->left(sel));
420 updateLocal(bv, false);
428 result = DISPATCH_RESULT(mathcursor->up(sel));
429 updateLocal(bv, false);
437 result = DISPATCH_RESULT(mathcursor->down(sel));
438 updateLocal(bv, false);
443 updateLocal(bv, false);
448 updateLocal(bv, false);
451 case LFUN_DELETE_LINE_FORWARD:
452 bv->lockedInsetStoreUndo(Undo::DELETE);
453 mathcursor->delLine();
454 updateLocal(bv, true);
458 mathcursor->idxNext();
459 updateLocal(bv, false);
463 mathcursor->idxPrev();
464 updateLocal(bv, false);
468 bv->lockedInsetStoreUndo(Undo::EDIT);
469 mathcursor->splitCell();
470 updateLocal(bv, true);
474 // if (!mathcursor->inMacroMode() && mathcursor->pos() == 0)
475 if (mathcursor->pos() == 0) {
476 bv->lockedInsetStoreUndo(Undo::DELETE);
477 mathcursor->pullArg(false);
478 bv->updateInset(this, true);
481 if (mathcursor->inMacroMode())
484 mathcursor->plainLeft();
488 bv->lockedInsetStoreUndo(Undo::DELETE);
489 if (mathcursor->pos() == mathcursor->array().size())
490 mathcursor->pullArg(true);
493 bv->updateInset(this, true);
497 // sprintf(dispatch_buffer, "%d %d",);
498 // dispatch_result = dispatch_buffer;
502 lyxerr << "LFUN_SETXY broken!\n";
507 istringstream is(arg.c_str());
510 mathcursor->setPos(x1 + x, y1 + y);
511 updateLocal(bv, false);
518 mathcursor->macroModeClose();
519 bv->lockedInsetStoreUndo(Undo::INSERT);
520 mathcursor->selPaste();
521 updateLocal(bv, true);
525 bv->lockedInsetStoreUndo(Undo::DELETE);
526 mathcursor->selCut();
527 updateLocal(bv, true);
531 mathcursor->selCopy();
536 case LFUN_WORDRIGHTSEL:
537 case LFUN_WORDLEFTSEL:
540 // --- accented characters ------------------------------
542 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
543 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
544 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
545 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
546 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
547 case LFUN_MACRON: handleAccent(bv, "bar"); break;
548 case LFUN_DOT: handleAccent(bv, "dot"); break;
549 case LFUN_CARON: handleAccent(bv, "check"); break;
550 case LFUN_BREVE: handleAccent(bv, "breve"); break;
551 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
555 if (!greek_kb_flag) {
557 bv->owner()->message(_("Math greek mode on"));
563 case LFUN_GREEK_TOGGLE:
564 greek_kb_flag = greek_kb_flag ? 0 : 2;
566 bv->owner()->message(_("Math greek keyboard on"));
568 bv->owner()->message(_("Math greek keyboard off"));
572 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
573 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
574 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
575 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
576 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
577 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
580 handleFont(bv, LM_TC_TEXTRM);
581 //bv->owner()->message(_("math text mode toggled"));
586 if (!mathcursor->selection()) {
587 mathcursor->handleFont(LM_TC_TEX);
588 //bv->owner()->message(_("TeX mode toggled"));
593 case LFUN_MATH_LIMITS:
594 bv->lockedInsetStoreUndo(Undo::INSERT);
595 if (mathcursor->toggleLimits())
596 updateLocal(bv, true);
601 bv->lockedInsetStoreUndo(Undo::INSERT);
602 latexkeys const * l = in_word_set(arg);
603 mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
604 updateLocal(bv, true);
608 case LFUN_INSERT_MATRIX:
610 bv->lockedInsetStoreUndo(Undo::INSERT);
611 mathcursor->interpret("matrix " + arg);
612 updateLocal(bv, true);
616 case LFUN_INSERT_MATH:
618 bv->lockedInsetStoreUndo(Undo::INSERT);
619 mathcursor->interpret(arg);
620 updateLocal(bv, true);
624 case LFUN_MATH_SPACE:
626 bv->lockedInsetStoreUndo(Undo::EDIT);
627 MathSpaceInset * p = mathcursor->prevSpaceInset();
631 mathcursor->insert(new MathSpaceInset(1));
632 updateLocal(bv, true);
636 case LFUN_MATH_DELIM:
638 bv->lockedInsetStoreUndo(Undo::INSERT);
639 static const string vdelim("(){}[]./|");
640 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
645 // try to read integers first
648 istringstream is(arg.c_str());
651 if (!is) { // ok, the beasties are no integers... try something else
655 istringstream is(arg.c_str());
659 //lyxerr << "formulabase::LFUN_MATH_DELIM, lt: '" << lt << "'\n";
660 //lyxerr << "formulabase::LFUN_MATH_DELIM, rt: '" << rt << "'\n";
663 latexkeys const * l = in_word_set(lt);
666 } else if (vdelim.find(lt[0]) != string::npos)
670 latexkeys const * l = in_word_set(rt);
673 } else if (vdelim.find(rt[0]) != string::npos)
677 handleDelim(bv, ilt, irt);
678 updateLocal(bv, true);
682 case LFUN_PROTECTEDSPACE:
683 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
684 bv->lockedInsetStoreUndo(Undo::INSERT);
685 mathcursor->insert(new MathSpaceInset(1));
686 updateLocal(bv, true);
690 bv->owner()->message(_("Invalid action in math mode!"));
694 case LFUN_MATH_HALIGN:
696 bv->lockedInsetStoreUndo(Undo::INSERT);
697 lyxerr << "handling halign '" << arg << "'\n";
699 MathArrayInset * p = matrixpar(idx);
702 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
703 updateLocal(bv, true);
707 case LFUN_MATH_VALIGN:
709 bv->lockedInsetStoreUndo(Undo::INSERT);
710 lyxerr << "handling valign '" << arg << "'\n";
712 MathArrayInset * p = matrixpar(idx);
715 p->valign(arg.size() ? arg[0] : 'c');
716 updateLocal(bv, true);
720 case LFUN_MATH_ROW_INSERT:
722 bv->lockedInsetStoreUndo(Undo::INSERT);
724 MathArrayInset * p = matrixpar(idx);
725 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
728 p->addRow(p->row(idx));
729 updateLocal(bv, true);
733 case LFUN_MATH_ROW_DELETE:
735 bv->lockedInsetStoreUndo(Undo::INSERT);
737 MathArrayInset * p = matrixpar(idx);
738 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
741 p->delRow(p->row(idx));
742 updateLocal(bv, true);
746 case LFUN_MATH_COLUMN_INSERT:
748 bv->lockedInsetStoreUndo(Undo::INSERT);
750 MathArrayInset * p = matrixpar(idx);
753 p->addCol(p->col(idx));
754 updateLocal(bv, true);
758 case LFUN_MATH_COLUMN_DELETE:
760 bv->lockedInsetStoreUndo(Undo::INSERT);
762 MathArrayInset * p = matrixpar(idx);
765 p->delCol(p->col(idx));
766 updateLocal(bv, true);
770 case LFUN_EXEC_COMMAND:
771 result = UNDISPATCHED;
775 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
776 unsigned char c = arg[0];
778 lyxerr << "Action: " << action << endl;
780 lyxerr << "char: '" << c << "' int: " << int(c) << endl;
781 //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);
782 //lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
783 bv->lockedInsetStoreUndo(Undo::INSERT);
785 if (c == 0) { // Dead key, do nothing
786 //lyxerr << "deadkey" << endl;
791 if (mathcursor->getLastCode() == LM_TC_TEX) {
792 mathcursor->macroModeOpen();
793 mathcursor->clearLastCode();
795 } else if (!varcode) {
796 MathTextCodes f = mathcursor->getLastCode() ?
797 mathcursor->getLastCode() :
798 mathcursor->nextCode();
799 varcode = MathIsAlphaFont(f) ?
800 static_cast<MathTextCodes>(f) :
804 // lyxerr << "Varcode << vardoce;
805 MathTextCodes char_code = varcode;
808 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
809 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
810 'Y', 0, 0, 0, 0 , 'Z' };
812 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
813 char_code = LM_TC_RM;
816 char_code = LM_TC_SYMB;
819 mathcursor->insert(c, char_code);
821 if (greek_kb_flag && char_code == LM_TC_RM )
822 mathcursor->setLastCode(LM_TC_VAR);
826 if (greek_kb_flag < 2)
829 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
830 mathcursor->insert(c, LM_TC_TEX);
832 mathcursor->insert('}', LM_TC_TEX);
835 mathcursor->clearLastCode();
836 // varcode = LM_TC_MIN;
837 } else if (c == '_' && varcode == LM_TC_TEX) {
838 mathcursor->insert(c, LM_TC_SPECIAL);
839 mathcursor->clearLastCode();
840 // varcode = LM_TC_MIN;
841 } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
842 mathcursor->macroModeOpen();
843 mathcursor->clearLastCode();
844 mathcursor->insert(c, LM_TC_MIN);
845 } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
846 MathTextCodes code = mathcursor->getLastCode();
847 if (code != LM_TC_TEXTRM)
849 mathcursor->insert(c, code);
850 } else if (strchr("+/-*<>=", c)) {
851 MathTextCodes code = mathcursor->getLastCode();
852 if (code != LM_TC_TEXTRM)
854 mathcursor->insert(c, code);
855 } else if (strchr(latex_special_chars, c) && c != '_') {
856 MathTextCodes code = mathcursor->getLastCode();
857 if (code != LM_TC_TEXTRM)
858 code = LM_TC_SPECIAL;
859 mathcursor->insert(c, code);
860 } else if (c == '_' || c == '^') {
864 mathcursor->interpret(s);
865 } else if (c == ' ') {
867 MathTextCodes f = (mathcursor->getLastCode()) ?
868 mathcursor->getLastCode() :
869 mathcursor->nextCode();
870 varcode = MathIsAlphaFont(f) ? f : LM_TC_VAR;
873 if (varcode == LM_TC_TEXTRM)
874 mathcursor->insert(c, LM_TC_TEXTRM);
876 mathcursor->macroModeClose();
877 else if (mathcursor->pop())
878 mathcursor->plainRight();
880 // this would not work if the inset is in an table!
881 //bv->text->cursorRight(bv, true);
884 } else if (c == '\'' || c == '@') {
885 mathcursor->insert(c, LM_TC_VAR);
886 } else if (c == '\\') {
888 mathcursor->macroModeClose();
889 bv->owner()->message(_("TeX mode"));
890 mathcursor->setLastCode(LM_TC_TEX);
892 updateLocal(bv, true);
893 } else if (action == LFUN_MATH_PANEL) {
894 result = UNDISPATCHED;
896 lyxerr << "Closed by action " << action << endl;
901 mathcursor->normalize();
903 if (was_macro != mathcursor->inMacroMode()
904 && action >= 0 && action != LFUN_BACKSPACE)
905 updateLocal(bv, true);
907 if (mathcursor->selection() || was_selection)
908 toggleInsetSelection(bv);
910 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
911 result == UNDISPATCHED)
914 bv->unlockInset(this);
916 return result; // original version
921 /* FIXME: math-greek-toggle seems to work OK, but math-greek doesn't turn
923 bool math_insert_greek(BufferView * bv, char c)
925 if (!bv->available())
933 if (!bv->theLockingInset() || bv->theLockingInset()->isTextInset()) {
934 int greek_kb_flag_save = greek_kb_flag;
935 InsetFormula * new_inset = new InsetFormula();
936 bv->beforeChange(bv->text);
937 if (!bv->insertInset(new_inset)) {
942 new_inset->edit(bv, 0, 0, 0);
943 new_inset->localDispatch(bv, LFUN_SELFINSERT, tmp);
944 if (greek_kb_flag_save < 2) {
945 bv->unlockInset(new_inset); // bv->theLockingInset());
946 bv->text->cursorRight(bv, true);
949 if (bv->theLockingInset()->lyxCode() == Inset::MATH_CODE ||
950 bv->theLockingInset()->lyxCode() == Inset::MATHMACRO_CODE)
951 static_cast<InsetFormula*>(bv->theLockingInset())->localDispatch(bv, LFUN_SELFINSERT, tmp);
953 lyxerr << "Math error: attempt to write on a wrong "
954 "class of inset." << endl;
960 Inset::Code InsetFormulaBase::lyxCode() const
962 return Inset::MATH_CODE;
966 LyXFont const InsetFormulaBase::convertFont(LyXFont const & f) const
968 // We have already discussed what was here
971 font.setLatex(LyXFont::OFF);
976 MathInset * InsetFormulaBase::par() const
982 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
984 if (bv->available()) {
985 // Feature "Read math inset from selection" disabled.
986 // // use selection if available..
988 // if (action == LFUN_MATH_IMPORT_SELECTION)
991 // sel = bv->getLyXText()->selectionAsString(bv->buffer());
994 // if (sel.empty()) {
995 f = new InsetFormula;
996 if (openNewInset(bv, f)) {
997 // don't do that also for LFUN_MATH_MODE unless you want end up with
998 // always changing to mathrm when opening an inlined inset
999 // -- I really hate "LyXfunc overloading"...
1001 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
1002 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
1005 // f = new InsetFormula(sel);
1006 // bv->getLyXText()->cutSelection(bv);
1007 // openNewInset(bv, f);
1010 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
1013 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
1015 mathDispatchCreation(bv, arg, true);
1018 void mathDispatchMathMode(BufferView * bv, string const & arg)
1020 mathDispatchCreation(bv, arg, false);
1023 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
1025 mathDispatchCreation(bv, arg, true);
1028 void mathDispatchMathMacro(BufferView * bv, string const & arg)
1030 if (bv->available()) {
1032 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
1035 string const s1 = token(s, ' ', 1);
1036 int const na = s1.empty() ? 0 : lyx::atoi(s1);
1037 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1042 void mathDispatchMathDelim(BufferView * bv, string const & arg)
1044 if (bv->available()) {
1045 if (openNewInset(bv, new InsetFormula))
1046 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
1051 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1053 if (bv->available()) {
1054 if (openNewInset(bv, new InsetFormula))
1055 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
1059 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1061 if (bv->available()) {
1062 if (arg.size() && arg[0] == '\\')
1063 openNewInset(bv, new InsetFormula(arg));
1065 mathDispatchMathMode(bv, arg);