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 } else if (c == '_' && varcode == LM_TC_TEX) {
793 mathcursor->insert(c, LM_TC_SPECIAL);
794 mathcursor->clearLastCode();
795 } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
796 mathcursor->macroModeOpen();
797 mathcursor->clearLastCode();
798 mathcursor->insert(c, LM_TC_MIN);
799 } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
800 MathTextCodes code = mathcursor->getLastCode();
801 if (code != LM_TC_TEXTRM)
803 mathcursor->insert(c, code);
804 } else if (strchr("+/-*<>=", c)) {
805 MathTextCodes code = mathcursor->getLastCode();
806 if (code != LM_TC_TEXTRM)
808 mathcursor->insert(c, code);
809 } else if (strchr(latex_special_chars, c) && c != '_') {
810 MathTextCodes code = mathcursor->getLastCode();
811 if (code != LM_TC_TEXTRM)
812 code = LM_TC_SPECIAL;
813 mathcursor->insert(c, code);
814 } else if (c == '_' || c == '^') {
818 mathcursor->interpret(s);
819 } else if (c == ' ') {
821 MathTextCodes f = (mathcursor->getLastCode()) ?
822 mathcursor->getLastCode() :
823 mathcursor->nextCode();
824 varcode = MathIsAlphaFont(f) ? f : LM_TC_VAR;
827 if (varcode == LM_TC_TEXTRM)
828 mathcursor->insert(c, LM_TC_TEXTRM);
830 mathcursor->macroModeClose();
831 else if (mathcursor->popRight())
834 // this would not work if the inset is in an table!
835 //bv->text->cursorRight(bv, true);
838 } else if (c == '\'' || c == '@') {
839 mathcursor->insert(c, LM_TC_VAR);
840 } else if (c == '\\') {
842 mathcursor->macroModeClose();
843 bv->owner()->message(_("TeX mode"));
844 mathcursor->setLastCode(LM_TC_TEX);
846 updateLocal(bv, true);
847 } else if (action == LFUN_MATH_PANEL) {
848 result = UNDISPATCHED;
850 lyxerr << "Closed by action " << action << endl;
855 mathcursor->normalize();
857 if (was_macro != mathcursor->inMacroMode()
858 && action >= 0 && action != LFUN_BACKSPACE)
859 updateLocal(bv, true);
861 if (mathcursor->selection() || was_selection)
862 toggleInsetSelection(bv);
864 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
865 result == UNDISPATCHED)
868 bv->unlockInset(this);
870 return result; // original version
874 Inset::Code InsetFormulaBase::lyxCode() const
876 return Inset::MATH_CODE;
880 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
882 if (bv->available()) {
883 // Feature "Read math inset from selection" disabled.
884 // // use selection if available..
886 // if (action == LFUN_MATH_IMPORT_SELECTION)
889 // sel = bv->getLyXText()->selectionAsString(bv->buffer());
892 // if (sel.empty()) {
893 f = new InsetFormula;
894 if (openNewInset(bv, f)) {
895 // don't do that also for LFUN_MATH_MODE unless you want end up with
896 // always changing to mathrm when opening an inlined inset
897 // -- I really hate "LyXfunc overloading"...
899 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
900 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
903 // f = new InsetFormula(sel);
904 // bv->getLyXText()->cutSelection(bv);
905 // openNewInset(bv, f);
908 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
911 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
913 mathDispatchCreation(bv, arg, true);
916 void mathDispatchMathMode(BufferView * bv, string const & arg)
918 mathDispatchCreation(bv, arg, false);
921 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
923 mathDispatchCreation(bv, arg, true);
926 void mathDispatchMathMacro(BufferView * bv, string const & arg)
928 if (bv->available()) {
930 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
933 string const s1 = token(s, ' ', 1);
934 int const na = s1.empty() ? 0 : lyx::atoi(s1);
935 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
940 void mathDispatchMathDelim(BufferView * bv, string const & arg)
942 if (bv->available()) {
943 if (openNewInset(bv, new InsetFormula))
944 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
949 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
951 if (bv->available()) {
952 if (openNewInset(bv, new InsetFormula))
953 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
957 void mathDispatchInsertMath(BufferView * bv, string const & arg)
959 if (bv->available()) {
960 if (arg.size() && arg[0] == '\\')
961 openNewInset(bv, new InsetFormula(arg));
963 mathDispatchMathMode(bv, arg);