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 bv->lockedInsetStoreUndo(Undo::DELETE);
451 mathcursor->backspace();
452 bv->updateInset(this, true);
456 bv->lockedInsetStoreUndo(Undo::DELETE);
458 bv->updateInset(this, true);
462 // sprintf(dispatch_buffer, "%d %d",);
463 // dispatch_result = dispatch_buffer;
467 lyxerr << "LFUN_SETXY broken!\n";
472 istringstream is(arg.c_str());
474 par()->getXY(x1, y1);
475 mathcursor->setPos(x1 + x, y1 + y);
476 updateLocal(bv, false);
483 mathcursor->macroModeClose();
484 bv->lockedInsetStoreUndo(Undo::INSERT);
485 mathcursor->selPaste();
486 updateLocal(bv, true);
490 bv->lockedInsetStoreUndo(Undo::DELETE);
491 mathcursor->selCut();
492 updateLocal(bv, true);
496 mathcursor->selCopy();
501 case LFUN_WORDRIGHTSEL:
502 case LFUN_WORDLEFTSEL:
505 // --- accented characters ------------------------------
507 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
508 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
509 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
510 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
511 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
512 case LFUN_MACRON: handleAccent(bv, "bar"); break;
513 case LFUN_DOT: handleAccent(bv, "dot"); break;
514 case LFUN_CARON: handleAccent(bv, "check"); break;
515 case LFUN_BREVE: handleAccent(bv, "breve"); break;
516 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
520 if (!greek_kb_flag) {
522 bv->owner()->message(_("Math greek mode on"));
528 case LFUN_GREEK_TOGGLE:
529 greek_kb_flag = greek_kb_flag ? 0 : 2;
531 bv->owner()->message(_("Math greek keyboard on"));
533 bv->owner()->message(_("Math greek keyboard off"));
537 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
538 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
539 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
540 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
541 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
542 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
545 handleFont(bv, LM_TC_TEXTRM);
546 //bv->owner()->message(_("math text mode toggled"));
549 case LFUN_MATH_LIMITS:
550 bv->lockedInsetStoreUndo(Undo::INSERT);
551 if (mathcursor->toggleLimits())
552 updateLocal(bv, true);
557 bv->lockedInsetStoreUndo(Undo::INSERT);
558 latexkeys const * l = in_word_set(arg);
559 mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
560 updateLocal(bv, true);
564 case LFUN_INSERT_MATRIX:
566 bv->lockedInsetStoreUndo(Undo::INSERT);
567 mathcursor->interpret("matrix " + arg);
568 updateLocal(bv, true);
572 case LFUN_INSERT_MATH:
574 bv->lockedInsetStoreUndo(Undo::INSERT);
575 mathcursor->interpret(arg);
576 updateLocal(bv, true);
580 case LFUN_MATH_SPACE:
582 bv->lockedInsetStoreUndo(Undo::EDIT);
583 MathSpaceInset * p = mathcursor->prevSpaceInset();
587 mathcursor->insert(new MathSpaceInset(1));
588 updateLocal(bv, true);
592 case LFUN_MATH_DELIM:
594 bv->lockedInsetStoreUndo(Undo::INSERT);
595 static const string vdelim("(){}[]./|");
596 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
601 // try to read integers first
604 istringstream is(arg.c_str());
607 if (!is) { // ok, the beasties are no integers... try something else
611 istringstream is(arg.c_str());
615 //lyxerr << "formulabase::LFUN_MATH_DELIM, lt: '" << lt << "'\n";
616 //lyxerr << "formulabase::LFUN_MATH_DELIM, rt: '" << rt << "'\n";
619 latexkeys const * l = in_word_set(lt);
622 } else if (vdelim.find(lt[0]) != string::npos)
626 latexkeys const * l = in_word_set(rt);
629 } else if (vdelim.find(rt[0]) != string::npos)
633 handleDelim(bv, ilt, irt);
634 updateLocal(bv, true);
638 case LFUN_PROTECTEDSPACE:
639 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
640 bv->lockedInsetStoreUndo(Undo::INSERT);
641 mathcursor->insert(new MathSpaceInset(1));
642 updateLocal(bv, true);
646 bv->owner()->message(_("Invalid action in math mode!"));
650 case LFUN_MATH_HALIGN:
652 bv->lockedInsetStoreUndo(Undo::INSERT);
653 lyxerr << "handling halign '" << arg << "'\n";
655 MathArrayInset * p = matrixpar(idx);
658 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
659 updateLocal(bv, true);
663 case LFUN_MATH_VALIGN:
665 bv->lockedInsetStoreUndo(Undo::INSERT);
666 lyxerr << "handling valign '" << arg << "'\n";
668 MathArrayInset * p = matrixpar(idx);
671 p->valign(arg.size() ? arg[0] : 'c');
672 updateLocal(bv, true);
676 case LFUN_MATH_ROW_INSERT:
678 bv->lockedInsetStoreUndo(Undo::INSERT);
680 MathArrayInset * p = matrixpar(idx);
681 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
684 p->addRow(p->row(idx));
685 updateLocal(bv, true);
689 case LFUN_MATH_ROW_DELETE:
691 bv->lockedInsetStoreUndo(Undo::INSERT);
693 MathArrayInset * p = matrixpar(idx);
694 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
697 p->delRow(p->row(idx));
698 updateLocal(bv, true);
702 case LFUN_MATH_COLUMN_INSERT:
704 bv->lockedInsetStoreUndo(Undo::INSERT);
706 MathArrayInset * p = matrixpar(idx);
709 p->addCol(p->col(idx));
710 updateLocal(bv, true);
714 case LFUN_MATH_COLUMN_DELETE:
716 bv->lockedInsetStoreUndo(Undo::INSERT);
718 MathArrayInset * p = matrixpar(idx);
721 p->delCol(p->col(idx));
722 updateLocal(bv, true);
726 case LFUN_EXEC_COMMAND:
727 result = UNDISPATCHED;
731 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
732 unsigned char c = arg[0];
734 lyxerr << "Action: " << action << endl;
736 lyxerr << "char: '" << c << "' int: " << int(c) << endl;
737 //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);
738 //lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
739 bv->lockedInsetStoreUndo(Undo::INSERT);
741 if (c == 0) { // Dead key, do nothing
742 //lyxerr << "deadkey" << endl;
747 if (mathcursor->getLastCode() == LM_TC_TEX) {
748 mathcursor->macroModeOpen();
749 mathcursor->clearLastCode();
751 } else if (!varcode) {
752 MathTextCodes f = mathcursor->getLastCode() ?
753 mathcursor->getLastCode() :
754 mathcursor->nextCode();
755 varcode = MathIsAlphaFont(f) ?
756 static_cast<MathTextCodes>(f) :
760 // lyxerr << "Varcode << vardoce;
761 MathTextCodes char_code = varcode;
764 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
765 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
766 0, 0, 0, 0, 0 , 'Z' };
768 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
769 char_code = LM_TC_RM;
772 char_code = LM_TC_SYMB;
775 mathcursor->insert(c, char_code);
777 if (greek_kb_flag && char_code == LM_TC_RM)
778 mathcursor->setLastCode(LM_TC_VAR);
782 if (greek_kb_flag < 2)
785 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
786 mathcursor->insert(c, LM_TC_TEX);
788 mathcursor->insert('}', LM_TC_TEX);
791 mathcursor->clearLastCode();
792 // varcode = LM_TC_MIN;
793 } else if (c == '_' && varcode == LM_TC_TEX) {
794 mathcursor->insert(c, LM_TC_SPECIAL);
795 mathcursor->clearLastCode();
796 // varcode = LM_TC_MIN;
797 } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
798 mathcursor->macroModeOpen();
799 mathcursor->clearLastCode();
800 mathcursor->insert(c, LM_TC_MIN);
801 } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
802 MathTextCodes code = mathcursor->getLastCode();
803 if (code != LM_TC_TEXTRM)
805 mathcursor->insert(c, code);
806 } else if (strchr("+/-*<>=", c)) {
807 MathTextCodes code = mathcursor->getLastCode();
808 if (code != LM_TC_TEXTRM)
810 mathcursor->insert(c, code);
811 } else if (strchr(latex_special_chars, c) && c != '_') {
812 MathTextCodes code = mathcursor->getLastCode();
813 if (code != LM_TC_TEXTRM)
814 code = LM_TC_SPECIAL;
815 mathcursor->insert(c, code);
816 } else if (c == '_' || c == '^') {
820 mathcursor->interpret(s);
821 } else if (c == ' ') {
823 MathTextCodes f = (mathcursor->getLastCode()) ?
824 mathcursor->getLastCode() :
825 mathcursor->nextCode();
826 varcode = MathIsAlphaFont(f) ? f : LM_TC_VAR;
829 if (varcode == LM_TC_TEXTRM)
830 mathcursor->insert(c, LM_TC_TEXTRM);
832 mathcursor->macroModeClose();
833 else if (mathcursor->popRight())
836 // this would not work if the inset is in an table!
837 //bv->text->cursorRight(bv, true);
840 } else if (c == '\'' || c == '@') {
841 mathcursor->insert(c, LM_TC_VAR);
842 } else if (c == '\\') {
844 mathcursor->macroModeClose();
845 bv->owner()->message(_("TeX mode"));
846 mathcursor->setLastCode(LM_TC_TEX);
848 updateLocal(bv, true);
849 } else if (action == LFUN_MATH_PANEL) {
850 result = UNDISPATCHED;
852 lyxerr << "Closed by action " << action << endl;
857 mathcursor->normalize();
859 if (was_macro != mathcursor->inMacroMode()
860 && action >= 0 && action != LFUN_BACKSPACE)
861 updateLocal(bv, true);
863 if (mathcursor->selection() || was_selection)
864 toggleInsetSelection(bv);
866 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
867 result == UNDISPATCHED)
870 bv->unlockInset(this);
872 return result; // original version
876 Inset::Code InsetFormulaBase::lyxCode() const
878 return Inset::MATH_CODE;
882 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
884 if (bv->available()) {
885 // Feature "Read math inset from selection" disabled.
886 // // use selection if available..
888 // if (action == LFUN_MATH_IMPORT_SELECTION)
891 // sel = bv->getLyXText()->selectionAsString(bv->buffer());
894 // if (sel.empty()) {
895 f = new InsetFormula;
896 if (openNewInset(bv, f)) {
897 // don't do that also for LFUN_MATH_MODE unless you want end up with
898 // always changing to mathrm when opening an inlined inset
899 // -- I really hate "LyXfunc overloading"...
901 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
902 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
905 // f = new InsetFormula(sel);
906 // bv->getLyXText()->cutSelection(bv);
907 // openNewInset(bv, f);
910 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
913 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
915 mathDispatchCreation(bv, arg, true);
918 void mathDispatchMathMode(BufferView * bv, string const & arg)
920 mathDispatchCreation(bv, arg, false);
923 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
925 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));
942 void mathDispatchMathDelim(BufferView * bv, string const & arg)
944 if (bv->available()) {
945 if (openNewInset(bv, new InsetFormula))
946 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
951 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
953 if (bv->available()) {
954 if (openNewInset(bv, new InsetFormula))
955 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
959 void mathDispatchInsertMath(BufferView * bv, string const & arg)
961 if (bv->available()) {
962 if (arg.size() && arg[0] == '\\')
963 openNewInset(bv, new InsetFormula(arg));
965 mathDispatchMathMode(bv, arg);