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"));
562 case LFUN_MATH_LIMITS:
563 bv->lockedInsetStoreUndo(Undo::INSERT);
564 if (mathcursor->toggleLimits())
565 updateLocal(bv, true);
570 bv->lockedInsetStoreUndo(Undo::INSERT);
571 latexkeys const * l = in_word_set(arg);
572 mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
573 updateLocal(bv, true);
577 case LFUN_INSERT_MATRIX:
579 bv->lockedInsetStoreUndo(Undo::INSERT);
580 mathcursor->interpret("matrix " + arg);
581 updateLocal(bv, true);
585 case LFUN_INSERT_MATH:
587 bv->lockedInsetStoreUndo(Undo::INSERT);
588 mathcursor->interpret(arg);
589 updateLocal(bv, true);
593 case LFUN_MATH_SPACE:
595 bv->lockedInsetStoreUndo(Undo::EDIT);
596 MathSpaceInset * p = mathcursor->prevSpaceInset();
600 mathcursor->insert(new MathSpaceInset(1));
601 updateLocal(bv, true);
605 case LFUN_MATH_DELIM:
607 bv->lockedInsetStoreUndo(Undo::INSERT);
608 static const string vdelim("(){}[]./|");
609 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
614 // try to read integers first
617 istringstream is(arg.c_str());
620 if (!is) { // ok, the beasties are no integers... try something else
624 istringstream is(arg.c_str());
628 //lyxerr << "formulabase::LFUN_MATH_DELIM, lt: '" << lt << "'\n";
629 //lyxerr << "formulabase::LFUN_MATH_DELIM, rt: '" << rt << "'\n";
632 latexkeys const * l = in_word_set(lt);
635 } else if (vdelim.find(lt[0]) != string::npos)
639 latexkeys const * l = in_word_set(rt);
642 } else if (vdelim.find(rt[0]) != string::npos)
646 handleDelim(bv, ilt, irt);
647 updateLocal(bv, true);
651 case LFUN_PROTECTEDSPACE:
652 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
653 bv->lockedInsetStoreUndo(Undo::INSERT);
654 mathcursor->insert(new MathSpaceInset(1));
655 updateLocal(bv, true);
659 bv->owner()->message(_("Invalid action in math mode!"));
663 case LFUN_MATH_HALIGN:
665 bv->lockedInsetStoreUndo(Undo::INSERT);
666 lyxerr << "handling halign '" << arg << "'\n";
668 MathArrayInset * p = matrixpar(idx);
671 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
672 updateLocal(bv, true);
676 case LFUN_MATH_VALIGN:
678 bv->lockedInsetStoreUndo(Undo::INSERT);
679 lyxerr << "handling valign '" << arg << "'\n";
681 MathArrayInset * p = matrixpar(idx);
684 p->valign(arg.size() ? arg[0] : 'c');
685 updateLocal(bv, true);
689 case LFUN_MATH_ROW_INSERT:
691 bv->lockedInsetStoreUndo(Undo::INSERT);
693 MathArrayInset * p = matrixpar(idx);
694 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
697 p->addRow(p->row(idx));
698 updateLocal(bv, true);
702 case LFUN_MATH_ROW_DELETE:
704 bv->lockedInsetStoreUndo(Undo::INSERT);
706 MathArrayInset * p = matrixpar(idx);
707 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
710 p->delRow(p->row(idx));
711 updateLocal(bv, true);
715 case LFUN_MATH_COLUMN_INSERT:
717 bv->lockedInsetStoreUndo(Undo::INSERT);
719 MathArrayInset * p = matrixpar(idx);
722 p->addCol(p->col(idx));
723 updateLocal(bv, true);
727 case LFUN_MATH_COLUMN_DELETE:
729 bv->lockedInsetStoreUndo(Undo::INSERT);
731 MathArrayInset * p = matrixpar(idx);
734 p->delCol(p->col(idx));
735 updateLocal(bv, true);
739 case LFUN_EXEC_COMMAND:
740 result = UNDISPATCHED;
744 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
745 unsigned char c = arg[0];
747 lyxerr << "Action: " << action << endl;
749 lyxerr << "char: '" << c << "' int: " << int(c) << endl;
750 //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);
751 //lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
752 bv->lockedInsetStoreUndo(Undo::INSERT);
754 if (c == 0) { // Dead key, do nothing
755 //lyxerr << "deadkey" << endl;
760 if (mathcursor->getLastCode() == LM_TC_TEX) {
761 mathcursor->macroModeOpen();
762 mathcursor->clearLastCode();
764 } else if (!varcode) {
765 MathTextCodes f = mathcursor->getLastCode() ?
766 mathcursor->getLastCode() :
767 mathcursor->nextCode();
768 varcode = MathIsAlphaFont(f) ?
769 static_cast<MathTextCodes>(f) :
773 // lyxerr << "Varcode << vardoce;
774 MathTextCodes char_code = varcode;
777 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
778 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
779 0, 0, 0, 0, 0 , 'Z' };
781 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
782 char_code = LM_TC_RM;
785 char_code = LM_TC_SYMB;
788 mathcursor->insert(c, char_code);
790 if (greek_kb_flag && char_code == LM_TC_RM)
791 mathcursor->setLastCode(LM_TC_VAR);
795 if (greek_kb_flag < 2)
798 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
799 mathcursor->insert(c, LM_TC_TEX);
801 mathcursor->insert('}', LM_TC_TEX);
804 mathcursor->clearLastCode();
805 // varcode = LM_TC_MIN;
806 } else if (c == '_' && varcode == LM_TC_TEX) {
807 mathcursor->insert(c, LM_TC_SPECIAL);
808 mathcursor->clearLastCode();
809 // varcode = LM_TC_MIN;
810 } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
811 mathcursor->macroModeOpen();
812 mathcursor->clearLastCode();
813 mathcursor->insert(c, LM_TC_MIN);
814 } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
815 MathTextCodes code = mathcursor->getLastCode();
816 if (code != LM_TC_TEXTRM)
818 mathcursor->insert(c, code);
819 } else if (strchr("+/-*<>=", c)) {
820 MathTextCodes code = mathcursor->getLastCode();
821 if (code != LM_TC_TEXTRM)
823 mathcursor->insert(c, code);
824 } else if (strchr(latex_special_chars, c) && c != '_') {
825 MathTextCodes code = mathcursor->getLastCode();
826 if (code != LM_TC_TEXTRM)
827 code = LM_TC_SPECIAL;
828 mathcursor->insert(c, code);
829 } else if (c == '_' || c == '^') {
833 mathcursor->interpret(s);
834 } else if (c == ' ') {
836 MathTextCodes f = (mathcursor->getLastCode()) ?
837 mathcursor->getLastCode() :
838 mathcursor->nextCode();
839 varcode = MathIsAlphaFont(f) ? f : LM_TC_VAR;
842 if (varcode == LM_TC_TEXTRM)
843 mathcursor->insert(c, LM_TC_TEXTRM);
845 mathcursor->macroModeClose();
846 else if (mathcursor->pop())
847 mathcursor->plainRight();
849 // this would not work if the inset is in an table!
850 //bv->text->cursorRight(bv, true);
853 } else if (c == '\'' || c == '@') {
854 mathcursor->insert(c, LM_TC_VAR);
855 } else if (c == '\\') {
857 mathcursor->macroModeClose();
858 bv->owner()->message(_("TeX mode"));
859 mathcursor->setLastCode(LM_TC_TEX);
861 updateLocal(bv, true);
862 } else if (action == LFUN_MATH_PANEL) {
863 result = UNDISPATCHED;
865 lyxerr << "Closed by action " << action << endl;
870 mathcursor->normalize();
872 if (was_macro != mathcursor->inMacroMode()
873 && action >= 0 && action != LFUN_BACKSPACE)
874 updateLocal(bv, true);
876 if (mathcursor->selection() || was_selection)
877 toggleInsetSelection(bv);
879 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
880 result == UNDISPATCHED)
883 bv->unlockInset(this);
885 return result; // original version
889 Inset::Code InsetFormulaBase::lyxCode() const
891 return Inset::MATH_CODE;
896 LyXFont const InsetFormulaBase::convertFont(LyXFont const & f) const
898 #warning Andre, you should be able to remove this now?
905 // We have already discussed what was here
913 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
915 if (bv->available()) {
916 // Feature "Read math inset from selection" disabled.
917 // // use selection if available..
919 // if (action == LFUN_MATH_IMPORT_SELECTION)
922 // sel = bv->getLyXText()->selectionAsString(bv->buffer());
925 // if (sel.empty()) {
926 f = new InsetFormula;
927 if (openNewInset(bv, f)) {
928 // don't do that also for LFUN_MATH_MODE unless you want end up with
929 // always changing to mathrm when opening an inlined inset
930 // -- I really hate "LyXfunc overloading"...
932 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
933 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
936 // f = new InsetFormula(sel);
937 // bv->getLyXText()->cutSelection(bv);
938 // openNewInset(bv, f);
941 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
944 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
946 mathDispatchCreation(bv, arg, true);
949 void mathDispatchMathMode(BufferView * bv, string const & arg)
951 mathDispatchCreation(bv, arg, false);
954 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
956 mathDispatchCreation(bv, arg, true);
959 void mathDispatchMathMacro(BufferView * bv, string const & arg)
961 if (bv->available()) {
963 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
966 string const s1 = token(s, ' ', 1);
967 int const na = s1.empty() ? 0 : lyx::atoi(s1);
968 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
973 void mathDispatchMathDelim(BufferView * bv, string const & arg)
975 if (bv->available()) {
976 if (openNewInset(bv, new InsetFormula))
977 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
982 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
984 if (bv->available()) {
985 if (openNewInset(bv, new InsetFormula))
986 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
990 void mathDispatchInsertMath(BufferView * bv, string const & arg)
992 if (bv->available()) {
993 if (arg.size() && arg[0] == '\\')
994 openNewInset(bv, new InsetFormula(arg));
996 mathDispatchMathMode(bv, arg);