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_scopeinset.h"
43 #include "math_macrotable.h"
44 #include "support/lyxlib.h"
45 #include "mathed/support.h"
46 #include "undo_funcs.h"
52 extern char const * latex_mathenv[];
53 MathCursor * mathcursor = 0;
64 void mathed_init_fonts();
66 string nicelabel(string const & label)
68 return "(" + (label.empty() ? "#" : label) + ")";
71 void handleFont(BufferView * bv, MathTextCodes t)
73 if (mathcursor->selection())
74 bv->lockedInsetStoreUndo(Undo::EDIT);
75 mathcursor->handleFont(t);
79 void handleAccent(BufferView * bv, string const & name)
81 bv->lockedInsetStoreUndo(Undo::EDIT);
82 mathcursor->handleAccent(name);
86 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
88 LyXText * lt = bv->getLyXText();
92 if (!bv->insertInset(new_inset)) {
96 new_inset->edit(bv, 0, 0, 0);
101 // returns the nearest enclosing grid
102 MathArrayInset * matrixpar(int & idx)
105 return (mathcursor ? mathcursor->enclosingArray(idx) : 0);
113 InsetFormulaBase::InsetFormulaBase()
116 #warning This is needed as long the math parser is not re-entrant
118 MathMacroTable::builtinMacros();
119 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
123 void InsetFormulaBase::read(Buffer const *, LyXLex & lex)
129 void InsetFormulaBase::write(Buffer const *, ostream & os) const
134 int InsetFormulaBase::latex(Buffer const *, ostream & os,
135 bool fragile, bool spacing) const
137 return latex(os, fragile, spacing);
140 int InsetFormulaBase::ascii(Buffer const *, ostream & os, int spacing) const
142 return ascii(os, spacing);
145 int InsetFormulaBase::linuxdoc(Buffer const *, ostream & os) const
150 int InsetFormulaBase::docBook(Buffer const *, ostream & os) const
157 // Check if uses AMS macros
158 void InsetFormulaBase::validate(LaTeXFeatures &) const
162 string const InsetFormulaBase::editMessage() const
164 return _("Math editor mode");
168 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
170 mathcursor = new MathCursor(this);
172 if (!bv->lockInset(this))
173 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
176 //bv->updateInset(this, false);
187 void InsetFormulaBase::edit(BufferView * bv, bool front)
189 edit(bv, front ? 0 : 1, 0, 0);
193 void InsetFormulaBase::insetUnlock(BufferView * bv)
196 if (mathcursor->inMacroMode()) {
197 mathcursor->macroModeClose();
198 updateLocal(bv, true);
203 bv->updateInset(this, false);
207 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
209 mathcursor->getPos(x, y);
215 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
220 if (isCursorVisible())
221 bv->hideLockedInsetCursor();
225 mathcursor->getPos(x, y);
230 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
231 bv->showLockedInsetCursor(x, y, asc, desc);
234 toggleCursorVisible();
238 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
240 if (!isCursorVisible()) {
244 mathcursor->getPos(x, y);
249 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
250 bv->fitLockedInsetCursor(x, y, asc, desc);
252 toggleInsetCursor(bv);
257 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
259 if (isCursorVisible())
260 toggleInsetCursor(bv);
264 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
267 bv->updateInset(this, false);
271 vector<string> const InsetFormulaBase::getLabelList() const
273 return std::vector<string>();
277 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
280 bv->updateInset(this, dirty);
284 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
285 int x, int y, int /*button*/)
291 mathcursor->setPos(x, y);
298 bv->updateInset(this, false);
303 void InsetFormulaBase::insetButtonPress(BufferView * bv,
304 int x, int y, int /*button*/)
309 if (mathcursor && mathcursor->selection()) {
310 mathcursor->selClear();
311 bv->updateInset(this, false);
316 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
317 int x, int y, int /*button*/)
319 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
322 mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo());
323 mathcursor->selStart();
325 mathcursor->getPos(sel_x, sel_y);
326 } else if (sel_flag) {
330 mathcursor->setPos(x, y);
332 mathcursor->getPos(x, y);
333 if (sel_x != x || sel_y != y)
334 bv->updateInset(this, false);
341 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
343 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
348 UpdatableInset::RESULT
349 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
352 //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
353 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
355 static int greek_kb_flag = 0;
360 MathTextCodes varcode = LM_TC_MIN;
361 bool was_macro = mathcursor->inMacroMode();
363 bool was_selection = mathcursor->selection();
364 RESULT result = DISPATCHED;
368 if (mathcursor->getLastCode() == LM_TC_TEX)
371 mathcursor->normalize();
375 // --- Cursor Movements ---------------------------------------------
378 sel = true; // fall through...
381 result = DISPATCH_RESULT(mathcursor->right(sel));
382 updateLocal(bv, false);
387 sel = true; // fall through
390 result = DISPATCH_RESULT(mathcursor->left(sel));
391 updateLocal(bv, false);
399 result = DISPATCH_RESULT(mathcursor->up(sel));
400 updateLocal(bv, false);
408 result = DISPATCH_RESULT(mathcursor->down(sel));
409 updateLocal(bv, false);
414 updateLocal(bv, false);
419 updateLocal(bv, false);
422 case LFUN_DELETE_LINE_FORWARD:
423 bv->lockedInsetStoreUndo(Undo::DELETE);
424 mathcursor->delLine();
425 updateLocal(bv, true);
429 mathcursor->idxNext();
430 updateLocal(bv, false);
434 mathcursor->idxPrev();
435 updateLocal(bv, false);
439 bv->lockedInsetStoreUndo(Undo::EDIT);
440 mathcursor->splitCell();
441 updateLocal(bv, true);
445 bv->lockedInsetStoreUndo(Undo::DELETE);
446 mathcursor->backspace();
447 bv->updateInset(this, true);
451 bv->lockedInsetStoreUndo(Undo::DELETE);
453 bv->updateInset(this, true);
457 // sprintf(dispatch_buffer, "%d %d",);
458 // dispatch_result = dispatch_buffer;
462 lyxerr << "LFUN_SETXY broken!\n";
467 istringstream is(arg.c_str());
469 par()->getXY(x1, y1);
470 mathcursor->setPos(x1 + x, y1 + y);
471 updateLocal(bv, false);
478 mathcursor->macroModeClose();
479 bv->lockedInsetStoreUndo(Undo::INSERT);
480 mathcursor->selPaste();
481 updateLocal(bv, true);
485 bv->lockedInsetStoreUndo(Undo::DELETE);
486 mathcursor->selCut();
487 updateLocal(bv, true);
491 mathcursor->selCopy();
496 case LFUN_WORDRIGHTSEL:
497 case LFUN_WORDLEFTSEL:
500 // --- accented characters ------------------------------
502 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
503 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
504 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
505 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
506 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
507 case LFUN_MACRON: handleAccent(bv, "bar"); break;
508 case LFUN_DOT: handleAccent(bv, "dot"); break;
509 case LFUN_CARON: handleAccent(bv, "check"); break;
510 case LFUN_BREVE: handleAccent(bv, "breve"); break;
511 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
515 if (!greek_kb_flag) {
517 bv->owner()->message(_("Math greek mode on"));
523 case LFUN_GREEK_TOGGLE:
524 greek_kb_flag = greek_kb_flag ? 0 : 2;
526 bv->owner()->message(_("Math greek keyboard on"));
528 bv->owner()->message(_("Math greek keyboard off"));
532 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
533 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
534 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
535 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
536 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
537 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
540 handleFont(bv, LM_TC_TEXTRM);
541 //bv->owner()->message(_("math text mode toggled"));
544 case LFUN_MATH_LIMITS:
545 bv->lockedInsetStoreUndo(Undo::INSERT);
546 if (mathcursor->toggleLimits())
547 updateLocal(bv, true);
552 bv->lockedInsetStoreUndo(Undo::INSERT);
553 latexkeys const * l = in_word_set(arg);
554 mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
555 updateLocal(bv, true);
559 case LFUN_INSERT_MATRIX:
561 bv->lockedInsetStoreUndo(Undo::INSERT);
562 mathcursor->interpret("matrix " + arg);
563 updateLocal(bv, true);
567 case LFUN_INSERT_MATH:
569 bv->lockedInsetStoreUndo(Undo::INSERT);
570 mathcursor->interpret(arg);
571 updateLocal(bv, true);
575 case LFUN_MATH_SPACE:
577 bv->lockedInsetStoreUndo(Undo::EDIT);
578 MathSpaceInset * p = mathcursor->prevSpaceInset();
582 mathcursor->insert(new MathSpaceInset(1));
583 updateLocal(bv, true);
587 case LFUN_MATH_DELIM:
589 bv->lockedInsetStoreUndo(Undo::INSERT);
590 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
593 istringstream is(arg.c_str());
595 latexkeys const * l = in_word_set(ls);
596 latexkeys const * r = in_word_set(rs);
597 if (!is || !l || !r) {
598 lyxerr << "can't parse delimeters from '" << arg << "'\n";
601 bv->lockedInsetStoreUndo(Undo::EDIT);
602 mathcursor->handleDelim(l, r);
603 updateLocal(bv, true);
607 case LFUN_PROTECTEDSPACE:
608 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
609 bv->lockedInsetStoreUndo(Undo::INSERT);
610 mathcursor->insert(new MathSpaceInset(1));
611 updateLocal(bv, true);
615 bv->owner()->message(_("Invalid action in math mode!"));
619 case LFUN_MATH_HALIGN:
621 bv->lockedInsetStoreUndo(Undo::INSERT);
622 lyxerr << "handling halign '" << arg << "'\n";
624 MathArrayInset * p = matrixpar(idx);
627 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
628 updateLocal(bv, true);
632 case LFUN_MATH_VALIGN:
634 bv->lockedInsetStoreUndo(Undo::INSERT);
635 lyxerr << "handling valign '" << arg << "'\n";
637 MathArrayInset * p = matrixpar(idx);
640 p->valign(arg.size() ? arg[0] : 'c');
641 updateLocal(bv, true);
645 case LFUN_MATH_ROW_INSERT:
647 bv->lockedInsetStoreUndo(Undo::INSERT);
649 MathArrayInset * p = matrixpar(idx);
650 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
653 p->addRow(p->row(idx));
654 updateLocal(bv, true);
658 case LFUN_MATH_ROW_DELETE:
660 bv->lockedInsetStoreUndo(Undo::INSERT);
662 MathArrayInset * p = matrixpar(idx);
663 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
666 p->delRow(p->row(idx));
667 updateLocal(bv, true);
671 case LFUN_MATH_COLUMN_INSERT:
673 bv->lockedInsetStoreUndo(Undo::INSERT);
675 MathArrayInset * p = matrixpar(idx);
678 p->addCol(p->col(idx));
679 updateLocal(bv, true);
683 case LFUN_MATH_COLUMN_DELETE:
685 bv->lockedInsetStoreUndo(Undo::INSERT);
687 MathArrayInset * p = matrixpar(idx);
690 p->delCol(p->col(idx));
691 updateLocal(bv, true);
695 case LFUN_EXEC_COMMAND:
696 result = UNDISPATCHED;
700 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
701 unsigned char c = arg[0];
703 lyxerr << "Action: " << action << endl;
705 lyxerr << "char: '" << c << "' int: " << int(c) << endl;
706 //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);
707 //lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
708 bv->lockedInsetStoreUndo(Undo::INSERT);
710 if (c == 0) { // Dead key, do nothing
711 //lyxerr << "deadkey" << endl;
716 if (mathcursor->getLastCode() == LM_TC_TEX) {
717 mathcursor->macroModeOpen();
718 mathcursor->clearLastCode();
720 } else if (!varcode) {
721 MathTextCodes f = mathcursor->getLastCode() ?
722 mathcursor->getLastCode() :
723 mathcursor->nextCode();
724 varcode = MathIsAlphaFont(f) ?
725 static_cast<MathTextCodes>(f) :
729 // lyxerr << "Varcode << vardoce;
730 MathTextCodes char_code = varcode;
733 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
734 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
735 0, 0, 0, 0, 0 , 'Z' };
737 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
738 char_code = LM_TC_RM;
741 char_code = LM_TC_SYMB;
744 mathcursor->insert(c, char_code);
746 if (greek_kb_flag && char_code == LM_TC_RM)
747 mathcursor->setLastCode(LM_TC_VAR);
751 if (greek_kb_flag < 2)
754 } else if (c == '{') {
755 mathcursor->insert(new MathScopeInset);
757 mathcursor->clearLastCode();
758 } else if (strchr("!,:;", c) && (varcode == LM_TC_TEX||was_macro)) {
759 mathcursor->insert(c, LM_TC_TEX);
760 mathcursor->clearLastCode();
761 } else if (c == '_' && varcode == LM_TC_TEX) {
762 mathcursor->insert(c, LM_TC_SPECIAL);
763 mathcursor->clearLastCode();
764 } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
765 mathcursor->macroModeOpen();
766 mathcursor->clearLastCode();
767 mathcursor->insert(c, LM_TC_MIN);
768 } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
769 MathTextCodes code = mathcursor->getLastCode();
770 if (code != LM_TC_TEXTRM)
772 mathcursor->insert(c, code);
773 } else if (strchr("+/-*<>=", c)) {
774 MathTextCodes code = mathcursor->getLastCode();
775 if (code != LM_TC_TEXTRM)
777 mathcursor->insert(c, code);
778 } else if (strchr("#$%{|}", c)) {
779 MathTextCodes code = mathcursor->getLastCode();
780 if (code != LM_TC_TEXTRM)
781 code = LM_TC_SPECIAL;
782 mathcursor->insert(c, code);
783 } else if (c == '_' || c == '^') {
787 mathcursor->interpret(s);
788 } else if (c == ' ') {
790 MathTextCodes f = (mathcursor->getLastCode()) ?
791 mathcursor->getLastCode() :
792 mathcursor->nextCode();
793 varcode = MathIsAlphaFont(f) ? f : LM_TC_VAR;
796 if (varcode == LM_TC_TEXTRM)
797 mathcursor->insert(c, LM_TC_TEXTRM);
799 mathcursor->macroModeClose();
800 else if (mathcursor->popRight())
803 // this would not work if the inset is in an table!
804 //bv->text->cursorRight(bv, true);
807 } else if (c == '\'' || c == '@') {
808 mathcursor->insert(c, LM_TC_VAR);
809 } else if (c == '\\') {
811 mathcursor->macroModeClose();
812 bv->owner()->message(_("TeX mode"));
813 mathcursor->setLastCode(LM_TC_TEX);
815 updateLocal(bv, true);
816 } else if (action == LFUN_MATH_PANEL) {
817 result = UNDISPATCHED;
819 lyxerr << "Closed by action " << action << endl;
824 mathcursor->normalize();
826 if (was_macro != mathcursor->inMacroMode()
827 && action >= 0 && action != LFUN_BACKSPACE)
828 updateLocal(bv, true);
830 if (mathcursor->selection() || was_selection)
831 toggleInsetSelection(bv);
833 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
834 result == UNDISPATCHED)
837 bv->unlockInset(this);
839 return result; // original version
843 Inset::Code InsetFormulaBase::lyxCode() const
845 return Inset::MATH_CODE;
849 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
851 if (bv->available()) {
852 // Feature "Read math inset from selection" disabled.
853 // // use selection if available..
855 // if (action == LFUN_MATH_IMPORT_SELECTION)
858 // sel = bv->getLyXText()->selectionAsString(bv->buffer());
861 // if (sel.empty()) {
862 f = new InsetFormula;
863 if (openNewInset(bv, f)) {
864 // don't do that also for LFUN_MATH_MODE unless you want end up with
865 // always changing to mathrm when opening an inlined inset
866 // -- I really hate "LyXfunc overloading"...
868 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
869 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
872 // f = new InsetFormula(sel);
873 // bv->getLyXText()->cutSelection(bv);
874 // openNewInset(bv, f);
877 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
880 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
882 mathDispatchCreation(bv, arg, true);
885 void mathDispatchMathMode(BufferView * bv, string const & arg)
887 mathDispatchCreation(bv, arg, false);
890 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
892 mathDispatchCreation(bv, arg, true);
895 void mathDispatchMathMacro(BufferView * bv, string const & arg)
897 if (bv->available()) {
899 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
902 string const s1 = token(s, ' ', 1);
903 int const na = s1.empty() ? 0 : lyx::atoi(s1);
904 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
909 void mathDispatchMathDelim(BufferView * bv, string const & arg)
911 if (bv->available()) {
912 if (openNewInset(bv, new InsetFormula))
913 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
918 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
920 if (bv->available()) {
921 if (openNewInset(bv, new InsetFormula))
922 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
926 void mathDispatchInsertMath(BufferView * bv, string const & arg)
928 if (bv->available()) {
929 if (arg.size() && arg[0] == '\\')
930 openNewInset(bv, new InsetFormula(arg));
932 mathDispatchMathMode(bv, arg);