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_special_chars;
53 extern char const * latex_mathenv[];
54 MathCursor * mathcursor = 0;
65 void mathed_init_fonts();
67 string nicelabel(string const & label)
69 return "(" + (label.empty() ? "#" : label) + ")";
72 void handleFont(BufferView * bv, MathTextCodes t)
74 if (mathcursor->selection())
75 bv->lockedInsetStoreUndo(Undo::EDIT);
76 mathcursor->handleFont(t);
79 void handleAccent(BufferView * bv, string const & name)
81 bv->lockedInsetStoreUndo(Undo::EDIT);
82 mathcursor->handleAccent(name);
85 void handleDelim(BufferView * bv, int l, int r)
87 bv->lockedInsetStoreUndo(Undo::EDIT);
88 mathcursor->handleDelim(l, r);
91 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
93 LyXText * lt = bv->getLyXText();
97 if (!bv->insertInset(new_inset)) {
101 new_inset->edit(bv, 0, 0, 0);
106 // returns the nearest enclosing grid
107 MathArrayInset * matrixpar(int & idx)
110 return (mathcursor ? mathcursor->enclosingArray(idx) : 0);
118 InsetFormulaBase::InsetFormulaBase()
121 #warning This is needed as long the math parser is not re-entrant
123 MathMacroTable::builtinMacros();
124 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
128 void InsetFormulaBase::read(Buffer const *, LyXLex & lex)
134 void InsetFormulaBase::write(Buffer const *, ostream & os) const
139 int InsetFormulaBase::latex(Buffer const *, ostream & os,
140 bool fragile, bool spacing) const
142 return latex(os, fragile, spacing);
145 int InsetFormulaBase::ascii(Buffer const *, ostream & os, int spacing) const
147 return ascii(os, spacing);
150 int InsetFormulaBase::linuxdoc(Buffer const *, ostream & os) const
155 int InsetFormulaBase::docBook(Buffer const *, ostream & os) const
162 // Check if uses AMS macros
163 void InsetFormulaBase::validate(LaTeXFeatures &) const
167 string const InsetFormulaBase::editMessage() const
169 return _("Math editor mode");
173 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
175 mathcursor = new MathCursor(this);
177 if (!bv->lockInset(this))
178 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
181 //bv->updateInset(this, false);
192 void InsetFormulaBase::edit(BufferView * bv, bool front)
194 edit(bv, front ? 0 : 1, 0, 0);
198 void InsetFormulaBase::insetUnlock(BufferView * bv)
201 if (mathcursor->inMacroMode()) {
202 mathcursor->macroModeClose();
203 updateLocal(bv, true);
208 bv->updateInset(this, false);
212 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
214 mathcursor->getPos(x, y);
220 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
225 if (isCursorVisible())
226 bv->hideLockedInsetCursor();
230 mathcursor->getPos(x, y);
235 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
236 bv->showLockedInsetCursor(x, y, asc, desc);
239 toggleCursorVisible();
243 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
245 if (!isCursorVisible()) {
249 mathcursor->getPos(x, y);
254 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
255 bv->fitLockedInsetCursor(x, y, asc, desc);
257 toggleInsetCursor(bv);
262 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
264 if (isCursorVisible())
265 toggleInsetCursor(bv);
269 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
272 bv->updateInset(this, false);
276 vector<string> const InsetFormulaBase::getLabelList() const
278 return std::vector<string>();
282 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
285 bv->updateInset(this, dirty);
289 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
290 int x, int y, int /*button*/)
296 mathcursor->setPos(x, y);
303 bv->updateInset(this, false);
308 void InsetFormulaBase::insetButtonPress(BufferView * bv,
309 int x, int y, int /*button*/)
314 if (mathcursor && mathcursor->selection()) {
315 mathcursor->selClear();
316 bv->updateInset(this, false);
321 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
322 int x, int y, int /*button*/)
324 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
327 mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo());
328 mathcursor->selStart();
330 mathcursor->getPos(sel_x, sel_y);
331 } else if (sel_flag) {
335 mathcursor->setPos(x, y);
337 mathcursor->getPos(x, y);
338 if (sel_x != x || sel_y != y)
339 bv->updateInset(this, false);
346 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
348 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
353 UpdatableInset::RESULT
354 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
357 //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
358 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
360 static int greek_kb_flag = 0;
365 MathTextCodes varcode = LM_TC_MIN;
366 bool was_macro = mathcursor->inMacroMode();
368 bool was_selection = mathcursor->selection();
369 RESULT result = DISPATCHED;
373 if (mathcursor->getLastCode() == LM_TC_TEX)
376 mathcursor->normalize();
380 // --- Cursor Movements ---------------------------------------------
383 sel = true; // fall through...
386 result = DISPATCH_RESULT(mathcursor->right(sel));
387 updateLocal(bv, false);
392 sel = true; // fall through
395 result = DISPATCH_RESULT(mathcursor->left(sel));
396 updateLocal(bv, false);
404 result = DISPATCH_RESULT(mathcursor->up(sel));
405 updateLocal(bv, false);
413 result = DISPATCH_RESULT(mathcursor->down(sel));
414 updateLocal(bv, false);
419 updateLocal(bv, false);
424 updateLocal(bv, false);
427 case LFUN_DELETE_LINE_FORWARD:
428 bv->lockedInsetStoreUndo(Undo::DELETE);
429 mathcursor->delLine();
430 updateLocal(bv, true);
434 mathcursor->idxNext();
435 updateLocal(bv, false);
439 mathcursor->idxPrev();
440 updateLocal(bv, false);
444 bv->lockedInsetStoreUndo(Undo::EDIT);
445 mathcursor->splitCell();
446 updateLocal(bv, true);
450 // if (!mathcursor->inMacroMode() && mathcursor->pos() == 0)
451 if (mathcursor->pos() == 0) {
452 bv->lockedInsetStoreUndo(Undo::DELETE);
453 mathcursor->pullArg(false);
454 bv->updateInset(this, true);
457 if (mathcursor->inMacroMode())
460 mathcursor->plainLeft();
464 bv->lockedInsetStoreUndo(Undo::DELETE);
465 if (mathcursor->pos() == mathcursor->array().size())
466 mathcursor->pullArg(true);
469 bv->updateInset(this, true);
473 // sprintf(dispatch_buffer, "%d %d",);
474 // dispatch_result = dispatch_buffer;
478 lyxerr << "LFUN_SETXY broken!\n";
483 istringstream is(arg.c_str());
485 par()->getXY(x1, y1);
486 mathcursor->setPos(x1 + x, y1 + y);
487 updateLocal(bv, false);
494 mathcursor->macroModeClose();
495 bv->lockedInsetStoreUndo(Undo::INSERT);
496 mathcursor->selPaste();
497 updateLocal(bv, true);
501 bv->lockedInsetStoreUndo(Undo::DELETE);
502 mathcursor->selCut();
503 updateLocal(bv, true);
507 mathcursor->selCopy();
512 case LFUN_WORDRIGHTSEL:
513 case LFUN_WORDLEFTSEL:
516 // --- accented characters ------------------------------
518 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
519 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
520 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
521 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
522 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
523 case LFUN_MACRON: handleAccent(bv, "bar"); break;
524 case LFUN_DOT: handleAccent(bv, "dot"); break;
525 case LFUN_CARON: handleAccent(bv, "check"); break;
526 case LFUN_BREVE: handleAccent(bv, "breve"); break;
527 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
531 if (!greek_kb_flag) {
533 bv->owner()->message(_("Math greek mode on"));
539 case LFUN_GREEK_TOGGLE:
540 greek_kb_flag = greek_kb_flag ? 0 : 2;
542 bv->owner()->message(_("Math greek keyboard on"));
544 bv->owner()->message(_("Math greek keyboard off"));
548 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
549 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
550 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
551 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
552 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
553 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
556 handleFont(bv, LM_TC_TEXTRM);
557 //bv->owner()->message(_("math text mode toggled"));
560 case LFUN_MATH_LIMITS:
561 bv->lockedInsetStoreUndo(Undo::INSERT);
562 if (mathcursor->toggleLimits())
563 updateLocal(bv, true);
568 bv->lockedInsetStoreUndo(Undo::INSERT);
569 latexkeys const * l = in_word_set(arg);
570 mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
571 updateLocal(bv, true);
575 case LFUN_INSERT_MATRIX:
577 bv->lockedInsetStoreUndo(Undo::INSERT);
578 mathcursor->interpret("matrix " + arg);
579 updateLocal(bv, true);
583 case LFUN_INSERT_MATH:
585 bv->lockedInsetStoreUndo(Undo::INSERT);
586 mathcursor->interpret(arg);
587 updateLocal(bv, true);
591 case LFUN_MATH_SPACE:
593 bv->lockedInsetStoreUndo(Undo::EDIT);
594 MathSpaceInset * p = mathcursor->prevSpaceInset();
598 mathcursor->insert(new MathSpaceInset(1));
599 updateLocal(bv, true);
603 case LFUN_MATH_DELIM:
605 bv->lockedInsetStoreUndo(Undo::INSERT);
606 static const string vdelim("(){}[]./|");
607 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
612 // try to read integers first
615 istringstream is(arg.c_str());
618 if (!is) { // ok, the beasties are no integers... try something else
622 istringstream is(arg.c_str());
626 //lyxerr << "formulabase::LFUN_MATH_DELIM, lt: '" << lt << "'\n";
627 //lyxerr << "formulabase::LFUN_MATH_DELIM, rt: '" << rt << "'\n";
630 latexkeys const * l = in_word_set(lt);
633 } else if (vdelim.find(lt[0]) != string::npos)
637 latexkeys const * l = in_word_set(rt);
640 } else if (vdelim.find(rt[0]) != string::npos)
644 handleDelim(bv, ilt, irt);
645 updateLocal(bv, true);
649 case LFUN_PROTECTEDSPACE:
650 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
651 bv->lockedInsetStoreUndo(Undo::INSERT);
652 mathcursor->insert(new MathSpaceInset(1));
653 updateLocal(bv, true);
657 bv->owner()->message(_("Invalid action in math mode!"));
661 case LFUN_MATH_HALIGN:
663 bv->lockedInsetStoreUndo(Undo::INSERT);
664 lyxerr << "handling halign '" << arg << "'\n";
666 MathArrayInset * p = matrixpar(idx);
669 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
670 updateLocal(bv, true);
674 case LFUN_MATH_VALIGN:
676 bv->lockedInsetStoreUndo(Undo::INSERT);
677 lyxerr << "handling valign '" << arg << "'\n";
679 MathArrayInset * p = matrixpar(idx);
682 p->valign(arg.size() ? arg[0] : 'c');
683 updateLocal(bv, true);
687 case LFUN_MATH_ROW_INSERT:
689 bv->lockedInsetStoreUndo(Undo::INSERT);
691 MathArrayInset * p = matrixpar(idx);
692 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
695 p->addRow(p->row(idx));
696 updateLocal(bv, true);
700 case LFUN_MATH_ROW_DELETE:
702 bv->lockedInsetStoreUndo(Undo::INSERT);
704 MathArrayInset * p = matrixpar(idx);
705 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
708 p->delRow(p->row(idx));
709 updateLocal(bv, true);
713 case LFUN_MATH_COLUMN_INSERT:
715 bv->lockedInsetStoreUndo(Undo::INSERT);
717 MathArrayInset * p = matrixpar(idx);
720 p->addCol(p->col(idx));
721 updateLocal(bv, true);
725 case LFUN_MATH_COLUMN_DELETE:
727 bv->lockedInsetStoreUndo(Undo::INSERT);
729 MathArrayInset * p = matrixpar(idx);
732 p->delCol(p->col(idx));
733 updateLocal(bv, true);
737 case LFUN_EXEC_COMMAND:
738 result = UNDISPATCHED;
742 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
743 unsigned char c = arg[0];
745 lyxerr << "Action: " << action << endl;
747 lyxerr << "char: '" << c << "' int: " << int(c) << endl;
748 //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);
749 //lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
750 bv->lockedInsetStoreUndo(Undo::INSERT);
752 if (c == 0) { // Dead key, do nothing
753 //lyxerr << "deadkey" << endl;
758 if (mathcursor->getLastCode() == LM_TC_TEX) {
759 mathcursor->macroModeOpen();
760 mathcursor->clearLastCode();
762 } else if (!varcode) {
763 MathTextCodes f = mathcursor->getLastCode() ?
764 mathcursor->getLastCode() :
765 mathcursor->nextCode();
766 varcode = MathIsAlphaFont(f) ?
767 static_cast<MathTextCodes>(f) :
771 // lyxerr << "Varcode << vardoce;
772 MathTextCodes char_code = varcode;
775 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
776 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
777 0, 0, 0, 0, 0 , 'Z' };
779 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
780 char_code = LM_TC_RM;
783 char_code = LM_TC_SYMB;
786 mathcursor->insert(c, char_code);
788 if (greek_kb_flag && char_code == LM_TC_RM)
789 mathcursor->setLastCode(LM_TC_VAR);
793 if (greek_kb_flag < 2)
796 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
797 mathcursor->insert(c, LM_TC_TEX);
799 mathcursor->insert('}', LM_TC_TEX);
802 mathcursor->clearLastCode();
803 // varcode = LM_TC_MIN;
804 } else if (c == '_' && varcode == LM_TC_TEX) {
805 mathcursor->insert(c, LM_TC_SPECIAL);
806 mathcursor->clearLastCode();
807 // varcode = LM_TC_MIN;
808 } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
809 mathcursor->macroModeOpen();
810 mathcursor->clearLastCode();
811 mathcursor->insert(c, LM_TC_MIN);
812 } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
813 MathTextCodes code = mathcursor->getLastCode();
814 if (code != LM_TC_TEXTRM)
816 mathcursor->insert(c, code);
817 } else if (strchr("+/-*<>=", c)) {
818 MathTextCodes code = mathcursor->getLastCode();
819 if (code != LM_TC_TEXTRM)
821 mathcursor->insert(c, code);
822 } else if (strchr(latex_special_chars, c) && c != '_') {
823 MathTextCodes code = mathcursor->getLastCode();
824 if (code != LM_TC_TEXTRM)
825 code = LM_TC_SPECIAL;
826 mathcursor->insert(c, code);
827 } else if (c == '_' || c == '^') {
831 mathcursor->interpret(s);
832 } else if (c == ' ') {
834 MathTextCodes f = (mathcursor->getLastCode()) ?
835 mathcursor->getLastCode() :
836 mathcursor->nextCode();
837 varcode = MathIsAlphaFont(f) ? f : LM_TC_VAR;
840 if (varcode == LM_TC_TEXTRM)
841 mathcursor->insert(c, LM_TC_TEXTRM);
843 mathcursor->macroModeClose();
844 else if (mathcursor->pop())
845 mathcursor->plainRight();
847 // this would not work if the inset is in an table!
848 //bv->text->cursorRight(bv, true);
851 } else if (c == '\'' || c == '@') {
852 mathcursor->insert(c, LM_TC_VAR);
853 } else if (c == '\\') {
855 mathcursor->macroModeClose();
856 bv->owner()->message(_("TeX mode"));
857 mathcursor->setLastCode(LM_TC_TEX);
859 updateLocal(bv, true);
860 } else if (action == LFUN_MATH_PANEL) {
861 result = UNDISPATCHED;
863 lyxerr << "Closed by action " << action << endl;
868 mathcursor->normalize();
870 if (was_macro != mathcursor->inMacroMode()
871 && action >= 0 && action != LFUN_BACKSPACE)
872 updateLocal(bv, true);
874 if (mathcursor->selection() || was_selection)
875 toggleInsetSelection(bv);
877 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
878 result == UNDISPATCHED)
881 bv->unlockInset(this);
883 return result; // original version
887 Inset::Code InsetFormulaBase::lyxCode() const
889 return Inset::MATH_CODE;
893 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
895 if (bv->available()) {
896 // Feature "Read math inset from selection" disabled.
897 // // use selection if available..
899 // if (action == LFUN_MATH_IMPORT_SELECTION)
902 // sel = bv->getLyXText()->selectionAsString(bv->buffer());
905 // if (sel.empty()) {
906 f = new InsetFormula;
907 if (openNewInset(bv, f)) {
908 // don't do that also for LFUN_MATH_MODE unless you want end up with
909 // always changing to mathrm when opening an inlined inset
910 // -- I really hate "LyXfunc overloading"...
912 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
913 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
916 // f = new InsetFormula(sel);
917 // bv->getLyXText()->cutSelection(bv);
918 // openNewInset(bv, f);
921 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
924 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
926 mathDispatchCreation(bv, arg, true);
929 void mathDispatchMathMode(BufferView * bv, string const & arg)
931 mathDispatchCreation(bv, arg, false);
934 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
936 mathDispatchCreation(bv, arg, true);
939 void mathDispatchMathMacro(BufferView * bv, string const & arg)
941 if (bv->available()) {
943 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
946 string const s1 = token(s, ' ', 1);
947 int const na = s1.empty() ? 0 : lyx::atoi(s1);
948 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
953 void mathDispatchMathDelim(BufferView * bv, string const & arg)
955 if (bv->available()) {
956 if (openNewInset(bv, new InsetFormula))
957 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
962 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
964 if (bv->available()) {
965 if (openNewInset(bv, new InsetFormula))
966 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
970 void mathDispatchInsertMath(BufferView * bv, string const & arg)
972 if (bv->available()) {
973 if (arg.size() && arg[0] == '\\')
974 openNewInset(bv, new InsetFormula(arg));
976 mathDispatchMathMode(bv, arg);