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_mathenv[];
52 MathCursor * mathcursor = 0;
63 void mathed_init_fonts();
65 string nicelabel(string const & label)
67 return "(" + (label.empty() ? "#" : label) + ")";
70 void handleFont(BufferView * bv, MathTextCodes t)
72 if (mathcursor->selection())
73 bv->lockedInsetStoreUndo(Undo::EDIT);
74 mathcursor->handleFont(t);
78 void handleAccent(BufferView * bv, string const & name)
80 bv->lockedInsetStoreUndo(Undo::EDIT);
81 mathcursor->handleAccent(name);
85 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
87 LyXText * lt = bv->getLyXText();
91 if (!bv->insertInset(new_inset)) {
95 new_inset->edit(bv, 0, 0, 0);
100 // returns the nearest enclosing grid
101 MathArrayInset * matrixpar(int & idx)
104 return (mathcursor ? mathcursor->enclosingArray(idx) : 0);
112 InsetFormulaBase::InsetFormulaBase()
115 #warning This is needed as long the math parser is not re-entrant
117 MathMacroTable::builtinMacros();
118 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
122 void InsetFormulaBase::read(Buffer const *, LyXLex & lex)
128 void InsetFormulaBase::write(Buffer const *, ostream & os) const
133 int InsetFormulaBase::latex(Buffer const *, ostream & os,
134 bool fragile, bool spacing) const
136 return latex(os, fragile, spacing);
139 int InsetFormulaBase::ascii(Buffer const *, ostream & os, int spacing) const
141 return ascii(os, spacing);
144 int InsetFormulaBase::linuxdoc(Buffer const *, ostream & os) const
149 int InsetFormulaBase::docBook(Buffer const *, ostream & os) const
156 // Check if uses AMS macros
157 void InsetFormulaBase::validate(LaTeXFeatures &) const
161 string const InsetFormulaBase::editMessage() const
163 return _("Math editor mode");
167 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
169 mathcursor = new MathCursor(this);
171 if (!bv->lockInset(this))
172 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
175 //bv->updateInset(this, false);
186 void InsetFormulaBase::edit(BufferView * bv, bool front)
188 edit(bv, front ? 0 : 1, 0, 0);
192 void InsetFormulaBase::insetUnlock(BufferView * bv)
195 if (mathcursor->inMacroMode()) {
196 mathcursor->macroModeClose();
197 updateLocal(bv, true);
202 bv->updateInset(this, false);
206 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
208 mathcursor->getPos(x, y);
214 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
219 if (isCursorVisible())
220 bv->hideLockedInsetCursor();
224 mathcursor->getPos(x, y);
229 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
230 bv->showLockedInsetCursor(x, y, asc, desc);
233 toggleCursorVisible();
237 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
239 if (!isCursorVisible()) {
243 mathcursor->getPos(x, y);
248 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
249 bv->fitLockedInsetCursor(x, y, asc, desc);
251 toggleInsetCursor(bv);
256 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
258 if (isCursorVisible())
259 toggleInsetCursor(bv);
263 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
266 bv->updateInset(this, false);
270 vector<string> const InsetFormulaBase::getLabelList() const
272 return std::vector<string>();
276 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
279 bv->updateInset(this, dirty);
283 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
284 int x, int y, int /*button*/)
290 mathcursor->setPos(x, y);
297 bv->updateInset(this, false);
302 void InsetFormulaBase::insetButtonPress(BufferView * bv,
303 int x, int y, int /*button*/)
308 if (mathcursor && mathcursor->selection()) {
309 mathcursor->selClear();
310 bv->updateInset(this, false);
315 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
316 int x, int y, int /*button*/)
318 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
321 mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo());
322 mathcursor->selStart();
324 mathcursor->getPos(sel_x, sel_y);
325 } else if (sel_flag) {
329 mathcursor->setPos(x, y);
331 mathcursor->getPos(x, y);
332 if (sel_x != x || sel_y != y)
333 bv->updateInset(this, false);
340 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
342 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
347 UpdatableInset::RESULT
348 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
351 //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
352 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
354 static int greek_kb_flag = 0;
359 MathTextCodes varcode = LM_TC_MIN;
360 bool was_macro = mathcursor->inMacroMode();
362 bool was_selection = mathcursor->selection();
363 RESULT result = DISPATCHED;
367 if (mathcursor->getLastCode() == LM_TC_TEX)
370 mathcursor->normalize();
374 // --- Cursor Movements ---------------------------------------------
377 sel = true; // fall through...
380 result = DISPATCH_RESULT(mathcursor->right(sel));
381 updateLocal(bv, false);
386 sel = true; // fall through
389 result = DISPATCH_RESULT(mathcursor->left(sel));
390 updateLocal(bv, false);
398 result = DISPATCH_RESULT(mathcursor->up(sel));
399 updateLocal(bv, false);
407 result = DISPATCH_RESULT(mathcursor->down(sel));
408 updateLocal(bv, false);
413 updateLocal(bv, false);
418 updateLocal(bv, false);
421 case LFUN_DELETE_LINE_FORWARD:
422 bv->lockedInsetStoreUndo(Undo::DELETE);
423 mathcursor->delLine();
424 updateLocal(bv, true);
428 mathcursor->idxNext();
429 updateLocal(bv, false);
433 mathcursor->idxPrev();
434 updateLocal(bv, false);
438 bv->lockedInsetStoreUndo(Undo::EDIT);
439 mathcursor->splitCell();
440 updateLocal(bv, true);
444 bv->lockedInsetStoreUndo(Undo::DELETE);
445 mathcursor->backspace();
446 bv->updateInset(this, true);
450 bv->lockedInsetStoreUndo(Undo::DELETE);
452 bv->updateInset(this, true);
456 // sprintf(dispatch_buffer, "%d %d",);
457 // dispatch_result = dispatch_buffer;
461 lyxerr << "LFUN_SETXY broken!\n";
466 istringstream is(arg.c_str());
468 par()->getXY(x1, y1);
469 mathcursor->setPos(x1 + x, y1 + y);
470 updateLocal(bv, false);
477 mathcursor->macroModeClose();
478 bv->lockedInsetStoreUndo(Undo::INSERT);
479 mathcursor->selPaste();
480 updateLocal(bv, true);
484 bv->lockedInsetStoreUndo(Undo::DELETE);
485 mathcursor->selCut();
486 updateLocal(bv, true);
490 mathcursor->selCopy();
495 case LFUN_WORDRIGHTSEL:
496 case LFUN_WORDLEFTSEL:
499 // --- accented characters ------------------------------
501 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
502 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
503 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
504 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
505 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
506 case LFUN_MACRON: handleAccent(bv, "bar"); break;
507 case LFUN_DOT: handleAccent(bv, "dot"); break;
508 case LFUN_CARON: handleAccent(bv, "check"); break;
509 case LFUN_BREVE: handleAccent(bv, "breve"); break;
510 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
514 if (!greek_kb_flag) {
516 bv->owner()->message(_("Math greek mode on"));
522 case LFUN_GREEK_TOGGLE:
523 greek_kb_flag = greek_kb_flag ? 0 : 2;
525 bv->owner()->message(_("Math greek keyboard on"));
527 bv->owner()->message(_("Math greek keyboard off"));
531 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
532 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
533 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
534 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
535 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
536 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
539 handleFont(bv, LM_TC_TEXTRM);
540 //bv->owner()->message(_("math text mode toggled"));
543 case LFUN_MATH_LIMITS:
544 bv->lockedInsetStoreUndo(Undo::INSERT);
545 if (mathcursor->toggleLimits())
546 updateLocal(bv, true);
551 bv->lockedInsetStoreUndo(Undo::INSERT);
552 latexkeys const * l = in_word_set(arg);
553 mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
554 updateLocal(bv, true);
558 case LFUN_INSERT_MATRIX:
560 bv->lockedInsetStoreUndo(Undo::INSERT);
561 mathcursor->interpret("matrix " + arg);
562 updateLocal(bv, true);
566 case LFUN_INSERT_MATH:
568 bv->lockedInsetStoreUndo(Undo::INSERT);
569 mathcursor->interpret(arg);
570 updateLocal(bv, true);
574 case LFUN_MATH_SPACE:
576 bv->lockedInsetStoreUndo(Undo::EDIT);
577 MathSpaceInset * p = mathcursor->prevSpaceInset();
581 mathcursor->insert(new MathSpaceInset(1));
582 updateLocal(bv, true);
586 case LFUN_MATH_DELIM:
588 bv->lockedInsetStoreUndo(Undo::INSERT);
589 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
592 istringstream is(arg.c_str());
594 latexkeys const * l = in_word_set(ls);
595 latexkeys const * r = in_word_set(rs);
596 if (!is || !l || !r) {
597 lyxerr << "can't parse delimeters from '" << arg << "'\n";
600 bv->lockedInsetStoreUndo(Undo::EDIT);
601 mathcursor->handleDelim(l, r);
602 updateLocal(bv, true);
606 case LFUN_PROTECTEDSPACE:
607 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
608 bv->lockedInsetStoreUndo(Undo::INSERT);
609 mathcursor->insert(new MathSpaceInset(1));
610 updateLocal(bv, true);
614 bv->owner()->message(_("Invalid action in math mode!"));
618 case LFUN_MATH_HALIGN:
620 bv->lockedInsetStoreUndo(Undo::INSERT);
621 lyxerr << "handling halign '" << arg << "'\n";
623 MathArrayInset * p = matrixpar(idx);
626 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
627 updateLocal(bv, true);
631 case LFUN_MATH_VALIGN:
633 bv->lockedInsetStoreUndo(Undo::INSERT);
634 lyxerr << "handling valign '" << arg << "'\n";
636 MathArrayInset * p = matrixpar(idx);
639 p->valign(arg.size() ? arg[0] : 'c');
640 updateLocal(bv, true);
644 case LFUN_MATH_ROW_INSERT:
646 bv->lockedInsetStoreUndo(Undo::INSERT);
648 MathArrayInset * p = matrixpar(idx);
649 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
652 p->addRow(p->row(idx));
653 updateLocal(bv, true);
657 case LFUN_MATH_ROW_DELETE:
659 bv->lockedInsetStoreUndo(Undo::INSERT);
661 MathArrayInset * p = matrixpar(idx);
662 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
665 p->delRow(p->row(idx));
666 updateLocal(bv, true);
670 case LFUN_MATH_COLUMN_INSERT:
672 bv->lockedInsetStoreUndo(Undo::INSERT);
674 MathArrayInset * p = matrixpar(idx);
677 p->addCol(p->col(idx));
678 updateLocal(bv, true);
682 case LFUN_MATH_COLUMN_DELETE:
684 bv->lockedInsetStoreUndo(Undo::INSERT);
686 MathArrayInset * p = matrixpar(idx);
689 p->delCol(p->col(idx));
690 updateLocal(bv, true);
694 case LFUN_EXEC_COMMAND:
695 result = UNDISPATCHED;
699 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
700 unsigned char c = arg[0];
702 lyxerr << "Action: " << action << endl;
704 lyxerr << "char: '" << c << "' int: " << int(c) << endl;
705 //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);
706 //lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
707 bv->lockedInsetStoreUndo(Undo::INSERT);
709 if (c == 0) { // Dead key, do nothing
710 //lyxerr << "deadkey" << endl;
715 if (mathcursor->getLastCode() == LM_TC_TEX) {
716 mathcursor->macroModeOpen();
717 mathcursor->clearLastCode();
719 } else if (!varcode) {
720 MathTextCodes f = mathcursor->getLastCode() ?
721 mathcursor->getLastCode() :
722 mathcursor->nextCode();
723 varcode = MathIsAlphaFont(f) ?
724 static_cast<MathTextCodes>(f) :
728 // lyxerr << "Varcode << vardoce;
729 MathTextCodes char_code = varcode;
732 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
733 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
734 0, 0, 0, 0, 0 , 'Z' };
736 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
737 char_code = LM_TC_RM;
740 char_code = LM_TC_SYMB;
743 mathcursor->insert(c, char_code);
745 if (greek_kb_flag && char_code == LM_TC_RM)
746 mathcursor->setLastCode(LM_TC_VAR);
750 if (greek_kb_flag < 2)
753 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
754 mathcursor->insert(c, LM_TC_TEX);
756 mathcursor->insert('}', LM_TC_TEX);
759 mathcursor->clearLastCode();
760 } else if (c == '_' && varcode == LM_TC_TEX) {
761 mathcursor->insert(c, LM_TC_SPECIAL);
762 mathcursor->clearLastCode();
763 } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
764 mathcursor->macroModeOpen();
765 mathcursor->clearLastCode();
766 mathcursor->insert(c, LM_TC_MIN);
767 } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
768 MathTextCodes code = mathcursor->getLastCode();
769 if (code != LM_TC_TEXTRM)
771 mathcursor->insert(c, code);
772 } else if (strchr("+/-*<>=", c)) {
773 MathTextCodes code = mathcursor->getLastCode();
774 if (code != LM_TC_TEXTRM)
776 mathcursor->insert(c, code);
777 } else if (strchr("#$%{|}", c)) {
778 MathTextCodes code = mathcursor->getLastCode();
779 if (code != LM_TC_TEXTRM)
780 code = LM_TC_SPECIAL;
781 mathcursor->insert(c, code);
782 } else if (c == '_' || c == '^') {
786 mathcursor->interpret(s);
787 } else if (c == ' ') {
789 MathTextCodes f = (mathcursor->getLastCode()) ?
790 mathcursor->getLastCode() :
791 mathcursor->nextCode();
792 varcode = MathIsAlphaFont(f) ? f : LM_TC_VAR;
795 if (varcode == LM_TC_TEXTRM)
796 mathcursor->insert(c, LM_TC_TEXTRM);
798 mathcursor->macroModeClose();
799 else if (mathcursor->popRight())
802 // this would not work if the inset is in an table!
803 //bv->text->cursorRight(bv, true);
806 } else if (c == '\'' || c == '@') {
807 mathcursor->insert(c, LM_TC_VAR);
808 } else if (c == '\\') {
810 mathcursor->macroModeClose();
811 bv->owner()->message(_("TeX mode"));
812 mathcursor->setLastCode(LM_TC_TEX);
814 updateLocal(bv, true);
815 } else if (action == LFUN_MATH_PANEL) {
816 result = UNDISPATCHED;
818 lyxerr << "Closed by action " << action << endl;
823 mathcursor->normalize();
825 if (was_macro != mathcursor->inMacroMode()
826 && action >= 0 && action != LFUN_BACKSPACE)
827 updateLocal(bv, true);
829 if (mathcursor->selection() || was_selection)
830 toggleInsetSelection(bv);
832 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
833 result == UNDISPATCHED)
836 bv->unlockInset(this);
838 return result; // original version
842 Inset::Code InsetFormulaBase::lyxCode() const
844 return Inset::MATH_CODE;
848 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
850 if (bv->available()) {
851 // Feature "Read math inset from selection" disabled.
852 // // use selection if available..
854 // if (action == LFUN_MATH_IMPORT_SELECTION)
857 // sel = bv->getLyXText()->selectionAsString(bv->buffer());
860 // if (sel.empty()) {
861 f = new InsetFormula;
862 if (openNewInset(bv, f)) {
863 // don't do that also for LFUN_MATH_MODE unless you want end up with
864 // always changing to mathrm when opening an inlined inset
865 // -- I really hate "LyXfunc overloading"...
867 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
868 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
871 // f = new InsetFormula(sel);
872 // bv->getLyXText()->cutSelection(bv);
873 // openNewInset(bv, f);
876 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
879 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
881 mathDispatchCreation(bv, arg, true);
884 void mathDispatchMathMode(BufferView * bv, string const & arg)
886 mathDispatchCreation(bv, arg, false);
889 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
891 mathDispatchCreation(bv, arg, true);
894 void mathDispatchMathMacro(BufferView * bv, string const & arg)
896 if (bv->available()) {
898 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
901 string const s1 = token(s, ' ', 1);
902 int const na = s1.empty() ? 0 : lyx::atoi(s1);
903 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
908 void mathDispatchMathDelim(BufferView * bv, string const & arg)
910 if (bv->available()) {
911 if (openNewInset(bv, new InsetFormula))
912 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
917 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
919 if (bv->available()) {
920 if (openNewInset(bv, new InsetFormula))
921 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
925 void mathDispatchInsertMath(BufferView * bv, string const & arg)
927 if (bv->available()) {
928 if (arg.size() && arg[0] == '\\')
929 openNewInset(bv, new InsetFormula(arg));
931 mathDispatchMathMode(bv, arg);