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 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);
107 // returns the nearest enclosing grid
108 MathArrayInset * matrixpar(int & idx)
111 return (mathcursor ? mathcursor->enclosingArray(idx) : 0);
119 InsetFormulaBase::InsetFormulaBase(MathInset * par)
123 #warning This is needed as long the math parser is not re-entrant
125 MathMacroTable::builtinMacros();
126 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
130 InsetFormulaBase::InsetFormulaBase(InsetFormulaBase const & f)
131 : UpdatableInset(f), par_(f.par_->clone())
135 InsetFormulaBase::~InsetFormulaBase()
141 void InsetFormulaBase::read(Buffer const *, LyXLex & lex)
146 void InsetFormulaBase::write(Buffer const *, ostream & os) const
151 int InsetFormulaBase::latex(Buffer const *, ostream & os,
152 bool fragile, bool spacing) const
154 return latex(os, fragile, spacing);
157 int InsetFormulaBase::ascii(Buffer const *, ostream & os, int spacing) const
159 return ascii(os, spacing);
162 int InsetFormulaBase::linuxdoc(Buffer const *, ostream & os) const
167 int InsetFormulaBase::docBook(Buffer const *, ostream & os) const
174 // Check if uses AMS macros
175 void InsetFormulaBase::validate(LaTeXFeatures &) const
179 string const InsetFormulaBase::editMessage() const
181 return _("Math editor mode");
185 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
187 mathcursor = new MathCursor(this);
189 if (!bv->lockInset(this))
190 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
193 //bv->updateInset(this, false);
204 void InsetFormulaBase::edit(BufferView * bv, bool front)
206 #warning Please have a look if this is right (Jug)
207 edit(bv, front ? 0 : 1, 0, 0);
211 void InsetFormulaBase::insetUnlock(BufferView * bv)
214 if (mathcursor->inMacroMode()) {
215 mathcursor->macroModeClose();
216 updateLocal(bv, true);
221 bv->updateInset(this, false);
225 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
227 mathcursor->getPos(x, y);
233 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
238 if (isCursorVisible())
239 bv->hideLockedInsetCursor();
243 mathcursor->getPos(x, y);
248 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
249 bv->showLockedInsetCursor(x, y, asc, desc);
252 toggleCursorVisible();
256 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
258 if (!isCursorVisible()) {
262 mathcursor->getPos(x, y);
267 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
268 bv->fitLockedInsetCursor(x, y, asc, desc);
270 toggleInsetCursor(bv);
275 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
277 if (isCursorVisible())
278 toggleInsetCursor(bv);
282 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
285 bv->updateInset(this, false);
289 vector<string> const InsetFormulaBase::getLabelList() const
291 return std::vector<string>();
295 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
298 bv->updateInset(this, dirty);
302 void InsetFormulaBase::metrics() const
304 const_cast<MathInset *>(par_)->metrics(LM_ST_TEXT);
308 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
309 int x, int y, int /*button*/)
315 mathcursor->setPos(x, y);
322 bv->updateInset(this, false);
327 void InsetFormulaBase::insetButtonPress(BufferView * bv,
328 int x, int y, int /*button*/)
333 if (mathcursor && mathcursor->selection()) {
334 mathcursor->selClear();
335 bv->updateInset(this, false);
340 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
341 int x, int y, int /*button*/)
343 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
346 mathcursor->setPos(sel_x + par_->xo(), sel_y + par_->yo());
347 mathcursor->selStart();
349 mathcursor->getPos(sel_x, sel_y);
350 } else if (sel_flag) {
354 mathcursor->setPos(x, y);
356 mathcursor->getPos(x, y);
357 if (sel_x != x || sel_y != y)
358 bv->updateInset(this, false);
365 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
367 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
372 UpdatableInset::RESULT
373 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
376 //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
377 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
382 MathTextCodes varcode = LM_TC_MIN;
383 bool was_macro = mathcursor->inMacroMode();
385 bool was_selection = mathcursor->selection();
386 RESULT result = DISPATCHED;
390 if (mathcursor->getLastCode() == LM_TC_TEX)
393 mathcursor->normalize();
397 // --- Cursor Movements ---------------------------------------------
400 sel = true; // fall through...
403 result = DISPATCH_RESULT(mathcursor->right(sel));
404 updateLocal(bv, false);
409 sel = true; // fall through
412 result = DISPATCH_RESULT(mathcursor->left(sel));
413 updateLocal(bv, false);
421 result = DISPATCH_RESULT(mathcursor->up(sel));
422 updateLocal(bv, false);
430 result = DISPATCH_RESULT(mathcursor->down(sel));
431 updateLocal(bv, false);
436 updateLocal(bv, false);
441 updateLocal(bv, false);
444 case LFUN_DELETE_LINE_FORWARD:
445 bv->lockedInsetStoreUndo(Undo::DELETE);
446 mathcursor->delLine();
447 updateLocal(bv, true);
451 mathcursor->idxNext();
452 updateLocal(bv, false);
456 mathcursor->idxPrev();
457 updateLocal(bv, false);
461 bv->lockedInsetStoreUndo(Undo::EDIT);
462 mathcursor->splitCell();
463 updateLocal(bv, true);
467 // if (!mathcursor->inMacroMode() && mathcursor->pos() == 0)
468 if (mathcursor->pos() == 0) {
469 bv->lockedInsetStoreUndo(Undo::DELETE);
470 mathcursor->pullArg(false);
471 bv->updateInset(this, true);
474 if (mathcursor->inMacroMode())
477 mathcursor->plainLeft();
481 bv->lockedInsetStoreUndo(Undo::DELETE);
482 if (mathcursor->pos() == mathcursor->array().size())
483 mathcursor->pullArg(true);
486 bv->updateInset(this, true);
490 // sprintf(dispatch_buffer, "%d %d",);
491 // dispatch_result = dispatch_buffer;
495 lyxerr << "LFUN_SETXY broken!\n";
500 istringstream is(arg.c_str());
503 mathcursor->setPos(x1 + x, y1 + y);
504 updateLocal(bv, false);
511 mathcursor->macroModeClose();
512 bv->lockedInsetStoreUndo(Undo::INSERT);
513 mathcursor->selPaste();
514 updateLocal(bv, true);
518 bv->lockedInsetStoreUndo(Undo::DELETE);
519 mathcursor->selCut();
520 updateLocal(bv, true);
524 mathcursor->selCopy();
529 case LFUN_WORDRIGHTSEL:
530 case LFUN_WORDLEFTSEL:
533 // --- accented characters ------------------------------
535 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
536 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
537 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
538 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
539 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
540 case LFUN_MACRON: handleAccent(bv, "bar"); break;
541 case LFUN_DOT: handleAccent(bv, "dot"); break;
542 case LFUN_CARON: handleAccent(bv, "check"); break;
543 case LFUN_BREVE: handleAccent(bv, "breve"); break;
544 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
548 if (!greek_kb_flag) {
550 bv->owner()->message(_("Math greek mode on"));
556 case LFUN_GREEK_TOGGLE:
557 greek_kb_flag = greek_kb_flag ? 0 : 2;
559 bv->owner()->message(_("Math greek keyboard on"));
561 bv->owner()->message(_("Math greek keyboard off"));
565 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
566 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
567 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
568 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
569 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
570 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
573 handleFont(bv, LM_TC_TEXTRM);
574 //bv->owner()->message(_("math text mode toggled"));
579 if (!mathcursor->selection()) {
580 mathcursor->handleFont(LM_TC_TEX);
581 //bv->owner()->message(_("TeX mode toggled"));
586 case LFUN_MATH_LIMITS:
587 bv->lockedInsetStoreUndo(Undo::INSERT);
588 if (mathcursor->toggleLimits())
589 updateLocal(bv, true);
594 bv->lockedInsetStoreUndo(Undo::INSERT);
595 latexkeys const * l = in_word_set(arg);
596 mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
597 updateLocal(bv, true);
601 case LFUN_INSERT_MATRIX:
603 bv->lockedInsetStoreUndo(Undo::INSERT);
604 mathcursor->interpret("matrix " + arg);
605 updateLocal(bv, true);
609 case LFUN_INSERT_MATH:
611 bv->lockedInsetStoreUndo(Undo::INSERT);
612 mathcursor->interpret(arg);
613 updateLocal(bv, true);
617 case LFUN_MATH_SPACE:
619 bv->lockedInsetStoreUndo(Undo::EDIT);
620 MathSpaceInset * p = mathcursor->prevSpaceInset();
624 mathcursor->insert(new MathSpaceInset(1));
625 updateLocal(bv, true);
629 case LFUN_MATH_DELIM:
631 bv->lockedInsetStoreUndo(Undo::INSERT);
632 static const string vdelim("(){}[]./|");
633 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
638 // try to read integers first
641 istringstream is(arg.c_str());
644 if (!is) { // ok, the beasties are no integers... try something else
648 istringstream is(arg.c_str());
652 //lyxerr << "formulabase::LFUN_MATH_DELIM, lt: '" << lt << "'\n";
653 //lyxerr << "formulabase::LFUN_MATH_DELIM, rt: '" << rt << "'\n";
656 latexkeys const * l = in_word_set(lt);
659 } else if (vdelim.find(lt[0]) != string::npos)
663 latexkeys const * l = in_word_set(rt);
666 } else if (vdelim.find(rt[0]) != string::npos)
670 handleDelim(bv, ilt, irt);
671 updateLocal(bv, true);
675 case LFUN_PROTECTEDSPACE:
676 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
677 bv->lockedInsetStoreUndo(Undo::INSERT);
678 mathcursor->insert(new MathSpaceInset(1));
679 updateLocal(bv, true);
683 bv->owner()->message(_("Invalid action in math mode!"));
687 case LFUN_MATH_HALIGN:
689 bv->lockedInsetStoreUndo(Undo::INSERT);
690 lyxerr << "handling halign '" << arg << "'\n";
692 MathArrayInset * p = matrixpar(idx);
695 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
696 updateLocal(bv, true);
700 case LFUN_MATH_VALIGN:
702 bv->lockedInsetStoreUndo(Undo::INSERT);
703 lyxerr << "handling valign '" << arg << "'\n";
705 MathArrayInset * p = matrixpar(idx);
708 p->valign(arg.size() ? arg[0] : 'c');
709 updateLocal(bv, true);
713 case LFUN_MATH_ROW_INSERT:
715 bv->lockedInsetStoreUndo(Undo::INSERT);
717 MathArrayInset * p = matrixpar(idx);
718 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
721 p->addRow(p->row(idx));
722 updateLocal(bv, true);
726 case LFUN_MATH_ROW_DELETE:
728 bv->lockedInsetStoreUndo(Undo::INSERT);
730 MathArrayInset * p = matrixpar(idx);
731 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
734 p->delRow(p->row(idx));
735 updateLocal(bv, true);
739 case LFUN_MATH_COLUMN_INSERT:
741 bv->lockedInsetStoreUndo(Undo::INSERT);
743 MathArrayInset * p = matrixpar(idx);
746 p->addCol(p->col(idx));
747 updateLocal(bv, true);
751 case LFUN_MATH_COLUMN_DELETE:
753 bv->lockedInsetStoreUndo(Undo::INSERT);
755 MathArrayInset * p = matrixpar(idx);
758 p->delCol(p->col(idx));
759 updateLocal(bv, true);
763 case LFUN_EXEC_COMMAND:
764 result = UNDISPATCHED;
768 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
769 unsigned char c = arg[0];
771 lyxerr << "Action: " << action << endl;
773 lyxerr << "char: '" << c << "' int: " << int(c) << endl;
774 //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);
775 //lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
776 bv->lockedInsetStoreUndo(Undo::INSERT);
778 if (c == 0) { // Dead key, do nothing
779 //lyxerr << "deadkey" << endl;
784 if (mathcursor->getLastCode() == LM_TC_TEX) {
785 mathcursor->macroModeOpen();
786 mathcursor->clearLastCode();
788 } else if (!varcode) {
789 MathTextCodes f = mathcursor->getLastCode() ?
790 mathcursor->getLastCode() :
791 mathcursor->nextCode();
792 varcode = MathIsAlphaFont(f) ?
793 static_cast<MathTextCodes>(f) :
797 // lyxerr << "Varcode << vardoce;
798 MathTextCodes char_code = varcode;
801 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
802 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
803 'Y', 0, 0, 0, 0 , 'Z' };
805 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
806 char_code = LM_TC_RM;
809 char_code = LM_TC_SYMB;
812 mathcursor->insert(c, char_code);
814 if (greek_kb_flag && char_code == LM_TC_RM )
815 mathcursor->setLastCode(LM_TC_VAR);
819 if (greek_kb_flag < 2)
822 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
823 mathcursor->insert(c, LM_TC_TEX);
825 mathcursor->insert('}', LM_TC_TEX);
828 mathcursor->clearLastCode();
829 // varcode = LM_TC_MIN;
830 } else if (c == '_' && varcode == LM_TC_TEX) {
831 mathcursor->insert(c, LM_TC_SPECIAL);
832 mathcursor->clearLastCode();
833 // varcode = LM_TC_MIN;
834 } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
835 mathcursor->macroModeOpen();
836 mathcursor->clearLastCode();
837 mathcursor->insert(c, LM_TC_MIN);
838 } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
839 MathTextCodes code = mathcursor->getLastCode();
840 if (code != LM_TC_TEXTRM)
842 mathcursor->insert(c, code);
843 } else if (strchr("+/-*<>=", c)) {
844 MathTextCodes code = mathcursor->getLastCode();
845 if (code != LM_TC_TEXTRM)
847 mathcursor->insert(c, code);
848 } else if (strchr(latex_special_chars, c) && c != '_') {
849 MathTextCodes code = mathcursor->getLastCode();
850 if (code != LM_TC_TEXTRM)
851 code = LM_TC_SPECIAL;
852 mathcursor->insert(c, code);
853 } else if (c == '_' || c == '^') {
857 mathcursor->interpret(s);
858 } else if (c == ' ') {
860 MathTextCodes f = (mathcursor->getLastCode()) ?
861 mathcursor->getLastCode() :
862 mathcursor->nextCode();
863 varcode = MathIsAlphaFont(f) ? f : LM_TC_VAR;
866 if (varcode == LM_TC_TEXTRM)
867 mathcursor->insert(c, LM_TC_TEXTRM);
869 mathcursor->macroModeClose();
870 else if (mathcursor->pop())
871 mathcursor->plainRight();
873 // this would not work if the inset is in an table!
874 //bv->text->cursorRight(bv, true);
877 } else if (c == '\'' || c == '@') {
878 mathcursor->insert(c, LM_TC_VAR);
879 } else if (c == '\\') {
881 mathcursor->macroModeClose();
882 bv->owner()->message(_("TeX mode"));
883 mathcursor->setLastCode(LM_TC_TEX);
885 updateLocal(bv, true);
886 } else if (action == LFUN_MATH_PANEL) {
887 result = UNDISPATCHED;
889 lyxerr << "Closed by action " << action << endl;
894 mathcursor->normalize();
896 if (was_macro != mathcursor->inMacroMode()
897 && action >= 0 && action != LFUN_BACKSPACE)
898 updateLocal(bv, true);
900 if (mathcursor->selection() || was_selection)
901 toggleInsetSelection(bv);
903 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
904 result == UNDISPATCHED)
907 bv->unlockInset(this);
909 return result; // original version
914 /* FIXME: math-greek-toggle seems to work OK, but math-greek doesn't turn
916 bool math_insert_greek(BufferView * bv, char c)
918 if (!bv->available())
926 if (!bv->theLockingInset() || bv->theLockingInset()->isTextInset()) {
927 int greek_kb_flag_save = greek_kb_flag;
928 InsetFormula * new_inset = new InsetFormula();
929 bv->beforeChange(bv->text);
930 if (!bv->insertInset(new_inset)) {
935 new_inset->edit(bv, 0, 0, 0);
936 new_inset->localDispatch(bv, LFUN_SELFINSERT, tmp);
937 if (greek_kb_flag_save < 2) {
938 bv->unlockInset(new_inset); // bv->theLockingInset());
939 bv->text->cursorRight(bv, true);
942 if (bv->theLockingInset()->lyxCode() == Inset::MATH_CODE ||
943 bv->theLockingInset()->lyxCode() == Inset::MATHMACRO_CODE)
944 static_cast<InsetFormula*>(bv->theLockingInset())->localDispatch(bv, LFUN_SELFINSERT, tmp);
946 lyxerr << "Math error: attempt to write on a wrong "
947 "class of inset." << endl;
953 Inset::Code InsetFormulaBase::lyxCode() const
955 return Inset::MATH_CODE;
959 LyXFont const InsetFormulaBase::convertFont(LyXFont const & f) const
961 // We have already discussed what was here
964 font.setLatex(LyXFont::OFF);
969 MathInset * InsetFormulaBase::par() const
975 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
977 if (bv->available()) {
978 // Feature "Read math inset from selection" disabled.
979 // // use selection if available..
981 // if (action == LFUN_MATH_IMPORT_SELECTION)
984 // sel = bv->getLyXText()->selectionAsString(bv->buffer());
987 // if (sel.empty()) {
988 f = new InsetFormula;
989 if (openNewInset(bv, f)) {
990 // don't do that also for LFUN_MATH_MODE unless you want end up with
991 // always changing to mathrm when opening an inlined inset
992 // -- I really hate "LyXfunc overloading"...
994 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
995 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
998 // f = new InsetFormula(sel);
999 // bv->getLyXText()->cutSelection(bv);
1000 // openNewInset(bv, f);
1003 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
1006 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
1008 mathDispatchCreation(bv, arg, true);
1011 void mathDispatchMathMode(BufferView * bv, string const & arg)
1013 mathDispatchCreation(bv, arg, false);
1016 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
1018 mathDispatchCreation(bv, arg, true);
1021 void mathDispatchMathMacro(BufferView * bv, string const & arg)
1023 if (bv->available()) {
1025 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
1028 string const s1 = token(s, ' ', 1);
1029 int const na = s1.empty() ? 0 : lyx::atoi(s1);
1030 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1035 void mathDispatchMathDelim(BufferView * bv, string const & arg)
1037 if (bv->available()) {
1038 if (openNewInset(bv, new InsetFormula))
1039 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
1044 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1046 if (bv->available()) {
1047 if (openNewInset(bv, new InsetFormula))
1048 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
1052 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1054 if (bv->available()) {
1055 if (arg.size() && arg[0] == '\\')
1056 openNewInset(bv, new InsetFormula(arg));
1058 mathDispatchMathMode(bv, arg);