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/lstrings.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_mathenv[];
52 MathCursor * mathcursor = 0;
63 void mathed_init_fonts();
65 string nicelabel(string const & label)
67 return "(" + (label.empty() ? "#" : label) + ")";
70 void handleFont(BufferView * bv, MathTextCodes t)
72 if (mathcursor->selection())
73 bv->lockedInsetStoreUndo(Undo::EDIT);
74 mathcursor->handleFont(t);
77 void handleAccent(BufferView * bv, string const & name)
79 bv->lockedInsetStoreUndo(Undo::EDIT);
80 mathcursor->handleAccent(name);
83 void handleDelim(BufferView * bv, int l, int r)
85 bv->lockedInsetStoreUndo(Undo::EDIT);
86 mathcursor->handleDelim(l, r);
89 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
91 LyXText * lt = bv->getLyXText();
95 if (!bv->insertInset(new_inset)) {
99 new_inset->edit(bv, 0, 0, 0);
104 // returns the nearest enclosing grid
105 MathArrayInset * matrixpar(int & idx)
108 return (mathcursor ? mathcursor->enclosingArray(idx) : 0);
116 InsetFormulaBase::InsetFormulaBase()
119 #warning This is needed as long the math parser is not re-entrant
121 MathMacroTable::builtinMacros();
122 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
126 void InsetFormulaBase::read(Buffer const *, LyXLex & lex)
132 void InsetFormulaBase::write(Buffer const *, ostream & os) const
137 int InsetFormulaBase::latex(Buffer const *, ostream & os,
138 bool fragile, bool spacing) const
140 return latex(os, fragile, spacing);
143 int InsetFormulaBase::ascii(Buffer const *, ostream & os, int spacing) const
145 return ascii(os, spacing);
148 int InsetFormulaBase::linuxdoc(Buffer const *, ostream & os) const
153 int InsetFormulaBase::docBook(Buffer const *, ostream & os) const
160 // Check if uses AMS macros
161 void InsetFormulaBase::validate(LaTeXFeatures &) const
165 string const InsetFormulaBase::editMessage() const
167 return _("Math editor mode");
171 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
173 mathcursor = new MathCursor(this);
175 if (!bv->lockInset(this))
176 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
179 //bv->updateInset(this, false);
190 void InsetFormulaBase::edit(BufferView * bv, bool front)
192 edit(bv, front ? 0 : 1, 0, 0);
196 void InsetFormulaBase::insetUnlock(BufferView * bv)
199 if (mathcursor->inMacroMode()) {
200 mathcursor->macroModeClose();
201 updateLocal(bv, true);
206 bv->updateInset(this, false);
210 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
212 mathcursor->getPos(x, y);
218 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
223 if (isCursorVisible())
224 bv->hideLockedInsetCursor();
228 mathcursor->getPos(x, y);
233 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
234 bv->showLockedInsetCursor(x, y, asc, desc);
237 toggleCursorVisible();
241 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
243 if (!isCursorVisible()) {
247 mathcursor->getPos(x, y);
252 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
253 bv->fitLockedInsetCursor(x, y, asc, desc);
255 toggleInsetCursor(bv);
260 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
262 if (isCursorVisible())
263 toggleInsetCursor(bv);
267 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
270 bv->updateInset(this, false);
274 vector<string> const InsetFormulaBase::getLabelList() const
276 return std::vector<string>();
280 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
283 bv->updateInset(this, dirty);
287 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
288 int x, int y, int /*button*/)
294 mathcursor->setPos(x, y);
301 bv->updateInset(this, false);
306 void InsetFormulaBase::insetButtonPress(BufferView * bv,
307 int x, int y, int /*button*/)
312 if (mathcursor && mathcursor->selection()) {
313 mathcursor->selClear();
314 bv->updateInset(this, false);
319 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
320 int x, int y, int /*button*/)
322 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
325 mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo());
326 mathcursor->selStart();
328 mathcursor->getPos(sel_x, sel_y);
329 } else if (sel_flag) {
333 mathcursor->setPos(x, y);
335 mathcursor->getPos(x, y);
336 if (sel_x != x || sel_y != y)
337 bv->updateInset(this, false);
344 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
346 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
351 UpdatableInset::RESULT
352 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
355 //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
356 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
358 static int greek_kb_flag = 0;
363 MathTextCodes varcode = LM_TC_MIN;
364 bool was_macro = mathcursor->inMacroMode();
366 bool was_selection = mathcursor->selection();
367 RESULT result = DISPATCHED;
371 if (mathcursor->getLastCode() == LM_TC_TEX)
374 mathcursor->normalize();
378 // --- Cursor Movements ---------------------------------------------
381 sel = true; // fall through...
384 result = DISPATCH_RESULT(mathcursor->right(sel));
385 updateLocal(bv, false);
390 sel = true; // fall through
393 result = DISPATCH_RESULT(mathcursor->left(sel));
394 updateLocal(bv, false);
402 result = DISPATCH_RESULT(mathcursor->up(sel));
403 updateLocal(bv, false);
411 result = DISPATCH_RESULT(mathcursor->down(sel));
412 updateLocal(bv, false);
417 updateLocal(bv, false);
422 updateLocal(bv, false);
425 case LFUN_DELETE_LINE_FORWARD:
426 bv->lockedInsetStoreUndo(Undo::DELETE);
427 mathcursor->delLine();
428 updateLocal(bv, true);
432 mathcursor->idxNext();
433 updateLocal(bv, false);
437 mathcursor->idxPrev();
438 updateLocal(bv, false);
442 bv->lockedInsetStoreUndo(Undo::EDIT);
443 mathcursor->splitCell();
444 updateLocal(bv, true);
448 bv->lockedInsetStoreUndo(Undo::DELETE);
449 mathcursor->backspace();
450 bv->updateInset(this, true);
454 bv->lockedInsetStoreUndo(Undo::DELETE);
456 bv->updateInset(this, true);
460 // sprintf(dispatch_buffer, "%d %d",);
461 // dispatch_result = dispatch_buffer;
465 lyxerr << "LFUN_SETXY broken!\n";
470 istringstream is(arg.c_str());
472 par()->getXY(x1, y1);
473 mathcursor->setPos(x1 + x, y1 + y);
474 updateLocal(bv, false);
481 mathcursor->macroModeClose();
482 bv->lockedInsetStoreUndo(Undo::INSERT);
483 mathcursor->selPaste();
484 updateLocal(bv, true);
488 bv->lockedInsetStoreUndo(Undo::DELETE);
489 mathcursor->selCut();
490 updateLocal(bv, true);
494 mathcursor->selCopy();
499 case LFUN_WORDRIGHTSEL:
500 case LFUN_WORDLEFTSEL:
503 // --- accented characters ------------------------------
505 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
506 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
507 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
508 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
509 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
510 case LFUN_MACRON: handleAccent(bv, "bar"); break;
511 case LFUN_DOT: handleAccent(bv, "dot"); break;
512 case LFUN_CARON: handleAccent(bv, "check"); break;
513 case LFUN_BREVE: handleAccent(bv, "breve"); break;
514 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
518 if (!greek_kb_flag) {
520 bv->owner()->message(_("Math greek mode on"));
526 case LFUN_GREEK_TOGGLE:
527 greek_kb_flag = greek_kb_flag ? 0 : 2;
529 bv->owner()->message(_("Math greek keyboard on"));
531 bv->owner()->message(_("Math greek keyboard off"));
535 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
536 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
537 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
538 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
539 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
540 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
543 handleFont(bv, LM_TC_TEXTRM);
544 //bv->owner()->message(_("math text mode toggled"));
547 case LFUN_MATH_LIMITS:
548 bv->lockedInsetStoreUndo(Undo::INSERT);
549 if (mathcursor->toggleLimits())
550 updateLocal(bv, true);
555 bv->lockedInsetStoreUndo(Undo::INSERT);
556 latexkeys const * l = in_word_set(arg);
557 mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
558 updateLocal(bv, true);
562 case LFUN_INSERT_MATRIX:
564 bv->lockedInsetStoreUndo(Undo::INSERT);
565 mathcursor->interpret("matrix " + arg);
566 updateLocal(bv, true);
570 case LFUN_INSERT_MATH:
572 bv->lockedInsetStoreUndo(Undo::INSERT);
573 mathcursor->interpret(arg);
574 updateLocal(bv, true);
578 case LFUN_MATH_SPACE:
580 bv->lockedInsetStoreUndo(Undo::EDIT);
581 MathSpaceInset * p = mathcursor->prevSpaceInset();
585 mathcursor->insert(new MathSpaceInset(1));
586 updateLocal(bv, true);
590 case LFUN_MATH_DELIM:
592 bv->lockedInsetStoreUndo(Undo::INSERT);
593 static const string vdelim("(){}[]./|");
594 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
599 // try to read integers first
602 istringstream is(arg.c_str());
605 if (!is) { // ok, the beasties are no integers... try something else
609 istringstream is(arg.c_str());
613 //lyxerr << "formulabase::LFUN_MATH_DELIM, lt: '" << lt << "'\n";
614 //lyxerr << "formulabase::LFUN_MATH_DELIM, rt: '" << rt << "'\n";
617 latexkeys const * l = in_word_set(lt);
620 } else if (vdelim.find(lt[0]) != string::npos)
624 latexkeys const * l = in_word_set(rt);
627 } else if (vdelim.find(rt[0]) != string::npos)
631 handleDelim(bv, ilt, irt);
632 updateLocal(bv, true);
636 case LFUN_PROTECTEDSPACE:
637 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
638 bv->lockedInsetStoreUndo(Undo::INSERT);
639 mathcursor->insert(new MathSpaceInset(1));
640 updateLocal(bv, true);
644 bv->owner()->message(_("Invalid action in math mode!"));
648 case LFUN_MATH_HALIGN:
650 bv->lockedInsetStoreUndo(Undo::INSERT);
651 lyxerr << "handling halign '" << arg << "'\n";
653 MathArrayInset * p = matrixpar(idx);
656 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
657 updateLocal(bv, true);
661 case LFUN_MATH_VALIGN:
663 bv->lockedInsetStoreUndo(Undo::INSERT);
664 lyxerr << "handling valign '" << arg << "'\n";
666 MathArrayInset * p = matrixpar(idx);
669 p->valign(arg.size() ? arg[0] : 'c');
670 updateLocal(bv, true);
674 case LFUN_MATH_ROW_INSERT:
676 bv->lockedInsetStoreUndo(Undo::INSERT);
678 MathArrayInset * p = matrixpar(idx);
679 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
682 p->addRow(p->row(idx));
683 updateLocal(bv, true);
687 case LFUN_MATH_ROW_DELETE:
689 bv->lockedInsetStoreUndo(Undo::INSERT);
691 MathArrayInset * p = matrixpar(idx);
692 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
695 p->delRow(p->row(idx));
696 updateLocal(bv, true);
700 case LFUN_MATH_COLUMN_INSERT:
702 bv->lockedInsetStoreUndo(Undo::INSERT);
704 MathArrayInset * p = matrixpar(idx);
707 p->addCol(p->col(idx));
708 updateLocal(bv, true);
712 case LFUN_MATH_COLUMN_DELETE:
714 bv->lockedInsetStoreUndo(Undo::INSERT);
716 MathArrayInset * p = matrixpar(idx);
719 p->delCol(p->col(idx));
720 updateLocal(bv, true);
724 case LFUN_EXEC_COMMAND:
725 result = UNDISPATCHED;
729 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
730 unsigned char c = arg[0];
732 lyxerr << "Action: " << action << endl;
734 lyxerr << "char: '" << c << "' int: " << int(c) << endl;
735 //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);
736 //lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
737 bv->lockedInsetStoreUndo(Undo::INSERT);
739 if (c == 0) { // Dead key, do nothing
740 //lyxerr << "deadkey" << endl;
745 if (mathcursor->getLastCode() == LM_TC_TEX) {
746 mathcursor->macroModeOpen();
747 mathcursor->clearLastCode();
749 } else if (!varcode) {
750 MathTextCodes f = mathcursor->getLastCode() ?
751 mathcursor->getLastCode() :
752 mathcursor->nextCode();
753 varcode = MathIsAlphaFont(f) ?
754 static_cast<MathTextCodes>(f) :
758 // lyxerr << "Varcode << vardoce;
759 MathTextCodes char_code = varcode;
762 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
763 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
764 0, 0, 0, 0, 0 , 'Z' };
766 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
767 char_code = LM_TC_RM;
770 char_code = LM_TC_SYMB;
773 mathcursor->insert(c, char_code);
775 if (greek_kb_flag && char_code == LM_TC_RM)
776 mathcursor->setLastCode(LM_TC_VAR);
780 if (greek_kb_flag < 2)
783 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
784 mathcursor->insert(c, LM_TC_TEX);
786 mathcursor->insert('}', LM_TC_TEX);
789 mathcursor->clearLastCode();
790 } else if (c == '_' && varcode == LM_TC_TEX) {
791 mathcursor->insert(c, LM_TC_SPECIAL);
792 mathcursor->clearLastCode();
793 } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
794 mathcursor->macroModeOpen();
795 mathcursor->clearLastCode();
796 mathcursor->insert(c, LM_TC_MIN);
797 } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
798 MathTextCodes code = mathcursor->getLastCode();
799 if (code != LM_TC_TEXTRM)
801 mathcursor->insert(c, code);
802 } else if (strchr("+/-*<>=", c)) {
803 MathTextCodes code = mathcursor->getLastCode();
804 if (code != LM_TC_TEXTRM)
806 mathcursor->insert(c, code);
807 } else if (strchr("#$%^{|}", c)) {
808 MathTextCodes code = mathcursor->getLastCode();
809 if (code != LM_TC_TEXTRM)
810 code = LM_TC_SPECIAL;
811 mathcursor->insert(c, code);
812 } else if (c == '_' || c == '^') {
816 mathcursor->interpret(s);
817 } else if (c == ' ') {
819 MathTextCodes f = (mathcursor->getLastCode()) ?
820 mathcursor->getLastCode() :
821 mathcursor->nextCode();
822 varcode = MathIsAlphaFont(f) ? f : LM_TC_VAR;
825 if (varcode == LM_TC_TEXTRM)
826 mathcursor->insert(c, LM_TC_TEXTRM);
828 mathcursor->macroModeClose();
829 else if (mathcursor->popRight())
832 // this would not work if the inset is in an table!
833 //bv->text->cursorRight(bv, true);
836 } else if (c == '\'' || c == '@') {
837 mathcursor->insert(c, LM_TC_VAR);
838 } else if (c == '\\') {
840 mathcursor->macroModeClose();
841 bv->owner()->message(_("TeX mode"));
842 mathcursor->setLastCode(LM_TC_TEX);
844 updateLocal(bv, true);
845 } else if (action == LFUN_MATH_PANEL) {
846 result = UNDISPATCHED;
848 lyxerr << "Closed by action " << action << endl;
853 mathcursor->normalize();
855 if (was_macro != mathcursor->inMacroMode()
856 && action >= 0 && action != LFUN_BACKSPACE)
857 updateLocal(bv, true);
859 if (mathcursor->selection() || was_selection)
860 toggleInsetSelection(bv);
862 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
863 result == UNDISPATCHED)
866 bv->unlockInset(this);
868 return result; // original version
872 Inset::Code InsetFormulaBase::lyxCode() const
874 return Inset::MATH_CODE;
878 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
880 if (bv->available()) {
881 // Feature "Read math inset from selection" disabled.
882 // // use selection if available..
884 // if (action == LFUN_MATH_IMPORT_SELECTION)
887 // sel = bv->getLyXText()->selectionAsString(bv->buffer());
890 // if (sel.empty()) {
891 f = new InsetFormula;
892 if (openNewInset(bv, f)) {
893 // don't do that also for LFUN_MATH_MODE unless you want end up with
894 // always changing to mathrm when opening an inlined inset
895 // -- I really hate "LyXfunc overloading"...
897 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
898 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
901 // f = new InsetFormula(sel);
902 // bv->getLyXText()->cutSelection(bv);
903 // openNewInset(bv, f);
906 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
909 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
911 mathDispatchCreation(bv, arg, true);
914 void mathDispatchMathMode(BufferView * bv, string const & arg)
916 mathDispatchCreation(bv, arg, false);
919 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
921 mathDispatchCreation(bv, arg, true);
924 void mathDispatchMathMacro(BufferView * bv, string const & arg)
926 if (bv->available()) {
928 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
931 string const s1 = token(s, ' ', 1);
932 int const na = s1.empty() ? 0 : lyx::atoi(s1);
933 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
938 void mathDispatchMathDelim(BufferView * bv, string const & arg)
940 if (bv->available()) {
941 if (openNewInset(bv, new InsetFormula))
942 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
947 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
949 if (bv->available()) {
950 if (openNewInset(bv, new InsetFormula))
951 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
955 void mathDispatchInsertMath(BufferView * bv, string const & arg)
957 if (bv->available()) {
958 if (arg.size() && arg[0] == '\\')
959 openNewInset(bv, new InsetFormula(arg));
961 mathDispatchMathMode(bv, arg);