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 #warning Please have a look if this is right (Jug)
195 #warning Does not look wrong... although I do not know what it is supposed to do (Andre)
196 edit(bv, front ? 0 : 1, 0, 0);
200 void InsetFormulaBase::insetUnlock(BufferView * bv)
203 if (mathcursor->inMacroMode()) {
204 mathcursor->macroModeClose();
205 updateLocal(bv, true);
210 bv->updateInset(this, false);
214 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
216 mathcursor->getPos(x, y);
222 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
227 if (isCursorVisible())
228 bv->hideLockedInsetCursor();
232 mathcursor->getPos(x, y);
237 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
238 bv->showLockedInsetCursor(x, y, asc, desc);
241 toggleCursorVisible();
245 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
247 if (!isCursorVisible()) {
251 mathcursor->getPos(x, y);
256 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
257 bv->fitLockedInsetCursor(x, y, asc, desc);
259 toggleInsetCursor(bv);
264 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
266 if (isCursorVisible())
267 toggleInsetCursor(bv);
271 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
274 bv->updateInset(this, false);
278 vector<string> const InsetFormulaBase::getLabelList() const
280 return std::vector<string>();
284 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
287 bv->updateInset(this, dirty);
291 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
292 int x, int y, int /*button*/)
298 mathcursor->setPos(x, y);
305 bv->updateInset(this, false);
310 void InsetFormulaBase::insetButtonPress(BufferView * bv,
311 int x, int y, int /*button*/)
316 if (mathcursor && mathcursor->selection()) {
317 mathcursor->selClear();
318 bv->updateInset(this, false);
323 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
324 int x, int y, int /*button*/)
326 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
329 mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo());
330 mathcursor->selStart();
332 mathcursor->getPos(sel_x, sel_y);
333 } else if (sel_flag) {
337 mathcursor->setPos(x, y);
339 mathcursor->getPos(x, y);
340 if (sel_x != x || sel_y != y)
341 bv->updateInset(this, false);
348 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
350 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
355 UpdatableInset::RESULT
356 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
359 //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
360 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
362 static int greek_kb_flag = 0;
367 MathTextCodes varcode = LM_TC_MIN;
368 bool was_macro = mathcursor->inMacroMode();
370 bool was_selection = mathcursor->selection();
371 RESULT result = DISPATCHED;
375 if (mathcursor->getLastCode() == LM_TC_TEX)
378 mathcursor->normalize();
382 // --- Cursor Movements ---------------------------------------------
385 sel = true; // fall through...
388 result = DISPATCH_RESULT(mathcursor->right(sel));
389 updateLocal(bv, false);
394 sel = true; // fall through
397 result = DISPATCH_RESULT(mathcursor->left(sel));
398 updateLocal(bv, false);
406 result = DISPATCH_RESULT(mathcursor->up(sel));
407 updateLocal(bv, false);
415 result = DISPATCH_RESULT(mathcursor->down(sel));
416 updateLocal(bv, false);
421 updateLocal(bv, false);
426 updateLocal(bv, false);
429 case LFUN_DELETE_LINE_FORWARD:
430 bv->lockedInsetStoreUndo(Undo::DELETE);
431 mathcursor->delLine();
432 updateLocal(bv, true);
436 mathcursor->idxNext();
437 updateLocal(bv, false);
441 mathcursor->idxPrev();
442 updateLocal(bv, false);
446 bv->lockedInsetStoreUndo(Undo::EDIT);
447 mathcursor->splitCell();
448 updateLocal(bv, true);
452 // if (!mathcursor->inMacroMode() && mathcursor->pos() == 0)
453 if (mathcursor->pos() == 0) {
454 bv->lockedInsetStoreUndo(Undo::DELETE);
455 mathcursor->pullArg(false);
456 bv->updateInset(this, true);
459 if (mathcursor->inMacroMode())
462 mathcursor->plainLeft();
466 bv->lockedInsetStoreUndo(Undo::DELETE);
467 if (mathcursor->pos() == mathcursor->array().size())
468 mathcursor->pullArg(true);
471 bv->updateInset(this, true);
475 // sprintf(dispatch_buffer, "%d %d",);
476 // dispatch_result = dispatch_buffer;
480 lyxerr << "LFUN_SETXY broken!\n";
485 istringstream is(arg.c_str());
487 par()->getXY(x1, y1);
488 mathcursor->setPos(x1 + x, y1 + y);
489 updateLocal(bv, false);
496 mathcursor->macroModeClose();
497 bv->lockedInsetStoreUndo(Undo::INSERT);
498 mathcursor->selPaste();
499 updateLocal(bv, true);
503 bv->lockedInsetStoreUndo(Undo::DELETE);
504 mathcursor->selCut();
505 updateLocal(bv, true);
509 mathcursor->selCopy();
514 case LFUN_WORDRIGHTSEL:
515 case LFUN_WORDLEFTSEL:
518 // --- accented characters ------------------------------
520 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
521 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
522 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
523 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
524 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
525 case LFUN_MACRON: handleAccent(bv, "bar"); break;
526 case LFUN_DOT: handleAccent(bv, "dot"); break;
527 case LFUN_CARON: handleAccent(bv, "check"); break;
528 case LFUN_BREVE: handleAccent(bv, "breve"); break;
529 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
533 if (!greek_kb_flag) {
535 bv->owner()->message(_("Math greek mode on"));
541 case LFUN_GREEK_TOGGLE:
542 greek_kb_flag = greek_kb_flag ? 0 : 2;
544 bv->owner()->message(_("Math greek keyboard on"));
546 bv->owner()->message(_("Math greek keyboard off"));
550 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
551 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
552 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
553 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
554 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
555 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
558 handleFont(bv, LM_TC_TEXTRM);
559 //bv->owner()->message(_("math text mode toggled"));
564 if (!mathcursor->selection()) {
565 mathcursor->handleFont(LM_TC_TEX);
566 //bv->owner()->message(_("TeX mode toggled"));
571 case LFUN_MATH_LIMITS:
572 bv->lockedInsetStoreUndo(Undo::INSERT);
573 if (mathcursor->toggleLimits())
574 updateLocal(bv, true);
579 bv->lockedInsetStoreUndo(Undo::INSERT);
580 latexkeys const * l = in_word_set(arg);
581 mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
582 updateLocal(bv, true);
586 case LFUN_INSERT_MATRIX:
588 bv->lockedInsetStoreUndo(Undo::INSERT);
589 mathcursor->interpret("matrix " + arg);
590 updateLocal(bv, true);
594 case LFUN_INSERT_MATH:
596 bv->lockedInsetStoreUndo(Undo::INSERT);
597 mathcursor->interpret(arg);
598 updateLocal(bv, true);
602 case LFUN_MATH_SPACE:
604 bv->lockedInsetStoreUndo(Undo::EDIT);
605 MathSpaceInset * p = mathcursor->prevSpaceInset();
609 mathcursor->insert(new MathSpaceInset(1));
610 updateLocal(bv, true);
614 case LFUN_MATH_DELIM:
616 bv->lockedInsetStoreUndo(Undo::INSERT);
617 static const string vdelim("(){}[]./|");
618 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
623 // try to read integers first
626 istringstream is(arg.c_str());
629 if (!is) { // ok, the beasties are no integers... try something else
633 istringstream is(arg.c_str());
637 //lyxerr << "formulabase::LFUN_MATH_DELIM, lt: '" << lt << "'\n";
638 //lyxerr << "formulabase::LFUN_MATH_DELIM, rt: '" << rt << "'\n";
641 latexkeys const * l = in_word_set(lt);
644 } else if (vdelim.find(lt[0]) != string::npos)
648 latexkeys const * l = in_word_set(rt);
651 } else if (vdelim.find(rt[0]) != string::npos)
655 handleDelim(bv, ilt, irt);
656 updateLocal(bv, true);
660 case LFUN_PROTECTEDSPACE:
661 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
662 bv->lockedInsetStoreUndo(Undo::INSERT);
663 mathcursor->insert(new MathSpaceInset(1));
664 updateLocal(bv, true);
668 bv->owner()->message(_("Invalid action in math mode!"));
672 case LFUN_MATH_HALIGN:
674 bv->lockedInsetStoreUndo(Undo::INSERT);
675 lyxerr << "handling halign '" << arg << "'\n";
677 MathArrayInset * p = matrixpar(idx);
680 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
681 updateLocal(bv, true);
685 case LFUN_MATH_VALIGN:
687 bv->lockedInsetStoreUndo(Undo::INSERT);
688 lyxerr << "handling valign '" << arg << "'\n";
690 MathArrayInset * p = matrixpar(idx);
693 p->valign(arg.size() ? arg[0] : 'c');
694 updateLocal(bv, true);
698 case LFUN_MATH_ROW_INSERT:
700 bv->lockedInsetStoreUndo(Undo::INSERT);
702 MathArrayInset * p = matrixpar(idx);
703 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
706 p->addRow(p->row(idx));
707 updateLocal(bv, true);
711 case LFUN_MATH_ROW_DELETE:
713 bv->lockedInsetStoreUndo(Undo::INSERT);
715 MathArrayInset * p = matrixpar(idx);
716 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
719 p->delRow(p->row(idx));
720 updateLocal(bv, true);
724 case LFUN_MATH_COLUMN_INSERT:
726 bv->lockedInsetStoreUndo(Undo::INSERT);
728 MathArrayInset * p = matrixpar(idx);
731 p->addCol(p->col(idx));
732 updateLocal(bv, true);
736 case LFUN_MATH_COLUMN_DELETE:
738 bv->lockedInsetStoreUndo(Undo::INSERT);
740 MathArrayInset * p = matrixpar(idx);
743 p->delCol(p->col(idx));
744 updateLocal(bv, true);
748 case LFUN_EXEC_COMMAND:
749 result = UNDISPATCHED;
753 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
754 unsigned char c = arg[0];
756 lyxerr << "Action: " << action << endl;
758 lyxerr << "char: '" << c << "' int: " << int(c) << endl;
759 //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);
760 //lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
761 bv->lockedInsetStoreUndo(Undo::INSERT);
763 if (c == 0) { // Dead key, do nothing
764 //lyxerr << "deadkey" << endl;
769 if (mathcursor->getLastCode() == LM_TC_TEX) {
770 mathcursor->macroModeOpen();
771 mathcursor->clearLastCode();
773 } else if (!varcode) {
774 MathTextCodes f = mathcursor->getLastCode() ?
775 mathcursor->getLastCode() :
776 mathcursor->nextCode();
777 varcode = MathIsAlphaFont(f) ?
778 static_cast<MathTextCodes>(f) :
782 // lyxerr << "Varcode << vardoce;
783 MathTextCodes char_code = varcode;
786 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
787 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
788 0, 0, 0, 0, 0 , 'Z' };
790 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
791 char_code = LM_TC_RM;
794 char_code = LM_TC_SYMB;
797 mathcursor->insert(c, char_code);
799 if (greek_kb_flag && char_code == LM_TC_RM)
800 mathcursor->setLastCode(LM_TC_VAR);
804 if (greek_kb_flag < 2)
807 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
808 mathcursor->insert(c, LM_TC_TEX);
810 mathcursor->insert('}', LM_TC_TEX);
813 mathcursor->clearLastCode();
814 // varcode = LM_TC_MIN;
815 } else if (c == '_' && varcode == LM_TC_TEX) {
816 mathcursor->insert(c, LM_TC_SPECIAL);
817 mathcursor->clearLastCode();
818 // varcode = LM_TC_MIN;
819 } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
820 mathcursor->macroModeOpen();
821 mathcursor->clearLastCode();
822 mathcursor->insert(c, LM_TC_MIN);
823 } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
824 MathTextCodes code = mathcursor->getLastCode();
825 if (code != LM_TC_TEXTRM)
827 mathcursor->insert(c, code);
828 } else if (strchr("+/-*<>=", c)) {
829 MathTextCodes code = mathcursor->getLastCode();
830 if (code != LM_TC_TEXTRM)
832 mathcursor->insert(c, code);
833 } else if (strchr(latex_special_chars, c) && c != '_') {
834 MathTextCodes code = mathcursor->getLastCode();
835 if (code != LM_TC_TEXTRM)
836 code = LM_TC_SPECIAL;
837 mathcursor->insert(c, code);
838 } else if (c == '_' || c == '^') {
842 mathcursor->interpret(s);
843 } else if (c == ' ') {
845 MathTextCodes f = (mathcursor->getLastCode()) ?
846 mathcursor->getLastCode() :
847 mathcursor->nextCode();
848 varcode = MathIsAlphaFont(f) ? f : LM_TC_VAR;
851 if (varcode == LM_TC_TEXTRM)
852 mathcursor->insert(c, LM_TC_TEXTRM);
854 mathcursor->macroModeClose();
855 else if (mathcursor->pop())
856 mathcursor->plainRight();
858 // this would not work if the inset is in an table!
859 //bv->text->cursorRight(bv, true);
862 } else if (c == '\'' || c == '@') {
863 mathcursor->insert(c, LM_TC_VAR);
864 } else if (c == '\\') {
866 mathcursor->macroModeClose();
867 bv->owner()->message(_("TeX mode"));
868 mathcursor->setLastCode(LM_TC_TEX);
870 updateLocal(bv, true);
871 } else if (action == LFUN_MATH_PANEL) {
872 result = UNDISPATCHED;
874 lyxerr << "Closed by action " << action << endl;
879 mathcursor->normalize();
881 if (was_macro != mathcursor->inMacroMode()
882 && action >= 0 && action != LFUN_BACKSPACE)
883 updateLocal(bv, true);
885 if (mathcursor->selection() || was_selection)
886 toggleInsetSelection(bv);
888 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
889 result == UNDISPATCHED)
892 bv->unlockInset(this);
894 return result; // original version
898 Inset::Code InsetFormulaBase::lyxCode() const
900 return Inset::MATH_CODE;
904 LyXFont const InsetFormulaBase::convertFont(LyXFont const & f) const
906 // We have already discussed what was here
909 font.setLatex(LyXFont::OFF);
915 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
917 if (bv->available()) {
918 // Feature "Read math inset from selection" disabled.
919 // // use selection if available..
921 // if (action == LFUN_MATH_IMPORT_SELECTION)
924 // sel = bv->getLyXText()->selectionAsString(bv->buffer());
927 // if (sel.empty()) {
928 f = new InsetFormula;
929 if (openNewInset(bv, f)) {
930 // don't do that also for LFUN_MATH_MODE unless you want end up with
931 // always changing to mathrm when opening an inlined inset
932 // -- I really hate "LyXfunc overloading"...
934 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
935 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
938 // f = new InsetFormula(sel);
939 // bv->getLyXText()->cutSelection(bv);
940 // openNewInset(bv, f);
943 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
946 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
948 mathDispatchCreation(bv, arg, true);
951 void mathDispatchMathMode(BufferView * bv, string const & arg)
953 mathDispatchCreation(bv, arg, false);
956 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
958 mathDispatchCreation(bv, arg, true);
961 void mathDispatchMathMacro(BufferView * bv, string const & arg)
963 if (bv->available()) {
965 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
968 string const s1 = token(s, ' ', 1);
969 int const na = s1.empty() ? 0 : lyx::atoi(s1);
970 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
975 void mathDispatchMathDelim(BufferView * bv, string const & arg)
977 if (bv->available()) {
978 if (openNewInset(bv, new InsetFormula))
979 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
984 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
986 if (bv->available()) {
987 if (openNewInset(bv, new InsetFormula))
988 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
992 void mathDispatchInsertMath(BufferView * bv, string const & arg)
994 if (bv->available()) {
995 if (arg.size() && arg[0] == '\\')
996 openNewInset(bv, new InsetFormula(arg));
998 mathDispatchMathMode(bv, arg);