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/LOstream.h"
40 #include "math_arrayinset.h"
41 #include "math_spaceinset.h"
42 #include "support/lyxlib.h"
43 #include "mathed/support.h"
44 #include "undo_funcs.h"
50 extern char const * latex_special_chars;
52 int greek_kb_flag = 0;
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, int code)
81 bv->lockedInsetStoreUndo(Undo::EDIT);
82 mathcursor->handleAccent(name, code);
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);
113 // returns the nearest enclosing matrix
114 MathArrayInset * matrixpar(int & idx)
118 static_cast<MathArrayInset *>
119 (mathcursor ? mathcursor->enclosing(LM_OT_MATRIX, idx) : 0);
126 InsetFormulaBase::InsetFormulaBase(MathInset * par)
131 InsetFormulaBase::InsetFormulaBase(InsetFormulaBase const & f)
132 : UpdatableInset(f), par_(static_cast<MathInset *>(f.par_->clone()))
136 InsetFormulaBase::~InsetFormulaBase()
142 void InsetFormulaBase::read(Buffer const *, LyXLex & lex)
147 void InsetFormulaBase::write(Buffer const *, ostream & os) const
152 int InsetFormulaBase::latex(Buffer const *, ostream & os,
153 bool fragile, bool spacing) const
155 return latex(os, fragile, spacing);
158 int InsetFormulaBase::ascii(Buffer const *, ostream & os, int spacing) const
160 return ascii(os, spacing);
163 int InsetFormulaBase::linuxdoc(Buffer const *, ostream & os) const
168 int InsetFormulaBase::docBook(Buffer const *, ostream & os) const
175 // Check if uses AMS macros
176 void InsetFormulaBase::validate(LaTeXFeatures &) const
180 string const InsetFormulaBase::editMessage() const
182 return _("Math editor mode");
186 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
188 mathcursor = new MathCursor(this);
190 if (!bv->lockInset(this))
191 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
194 //bv->updateInset(this, false);
205 void InsetFormulaBase::edit(BufferView * bv, bool front)
207 #warning Please have a look if this is right (Jug)
208 edit(bv, front ? 0 : 1, 0, 0);
212 void InsetFormulaBase::insetUnlock(BufferView * bv)
215 if (mathcursor->InMacroMode()) {
216 mathcursor->MacroModeClose();
217 updateLocal(bv, true);
222 bv->updateInset(this, false);
226 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
228 mathcursor->GetPos(x, y);
234 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
239 if (isCursorVisible())
240 bv->hideLockedInsetCursor();
244 mathcursor->GetPos(x, y);
249 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
250 bv->showLockedInsetCursor(x, y, asc, desc);
253 toggleCursorVisible();
257 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
259 if (!isCursorVisible()) {
263 mathcursor->GetPos(x, y);
268 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
269 bv->fitLockedInsetCursor(x, y, asc, desc);
271 toggleInsetCursor(bv);
276 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
278 if (isCursorVisible())
279 toggleInsetCursor(bv);
283 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
286 bv->updateInset(this, false);
290 vector<string> const InsetFormulaBase::getLabelList() const
292 return std::vector<string>();
296 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
299 bv->updateInset(this, dirty);
303 void InsetFormulaBase::Metrics() const
305 const_cast<MathInset *>(par_)->Metrics(LM_ST_TEXT);
309 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
310 int x, int y, int /*button*/)
316 mathcursor->SetPos(x, y);
323 bv->updateInset(this, false);
328 void InsetFormulaBase::insetButtonPress(BufferView * bv,
329 int x, int y, int /*button*/)
334 if (mathcursor && mathcursor->Selection()) {
335 mathcursor->SelClear();
336 bv->updateInset(this, false);
341 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
342 int x, int y, int /*button*/)
344 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
347 mathcursor->SetPos(sel_x + par_->xo(), sel_y + par_->yo());
348 mathcursor->SelStart();
350 mathcursor->GetPos(sel_x, sel_y);
351 } else if (sel_flag) {
355 mathcursor->SetPos(x, y);
357 mathcursor->GetPos(x, y);
358 if (sel_x != x || sel_y != y)
359 bv->updateInset(this, false);
366 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
368 lyxerr[Debug::MATHED]
369 << "Used InsetFormulaBase::InsetKeyPress." << endl;
374 UpdatableInset::RESULT
375 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
378 //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
379 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
384 MathTextCodes varcode = LM_TC_MIN;
385 bool was_macro = mathcursor->InMacroMode();
387 bool was_selection = mathcursor->Selection();
388 RESULT result = DISPATCHED;
392 if (mathcursor->getLastCode() == LM_TC_TEX)
395 mathcursor->normalize();
399 // --- Cursor Movements ---------------------------------------------
402 sel = true; // fall through...
405 result = DISPATCH_RESULT(mathcursor->Right(sel));
406 updateLocal(bv, false);
411 sel = true; // fall through
414 result = DISPATCH_RESULT(mathcursor->Left(sel));
415 updateLocal(bv, false);
423 result = DISPATCH_RESULT(mathcursor->Up(sel));
424 updateLocal(bv, false);
432 result = DISPATCH_RESULT(mathcursor->Down(sel));
433 updateLocal(bv, false);
438 updateLocal(bv, false);
443 updateLocal(bv, false);
446 case LFUN_DELETE_LINE_FORWARD:
447 bv->lockedInsetStoreUndo(Undo::DELETE);
448 mathcursor->DelLine();
449 updateLocal(bv, true);
453 mathcursor->idxNext();
454 updateLocal(bv, false);
458 mathcursor->idxPrev();
459 updateLocal(bv, false);
463 bv->lockedInsetStoreUndo(Undo::EDIT);
464 mathcursor->splitCell();
465 updateLocal(bv, true);
469 // if (!mathcursor->InMacroMode() && mathcursor->pos() == 0)
470 if (mathcursor->pos() == 0) {
471 bv->lockedInsetStoreUndo(Undo::DELETE);
472 mathcursor->pullArg(false);
473 bv->updateInset(this, true);
476 if (mathcursor->InMacroMode())
479 mathcursor->plainLeft();
483 bv->lockedInsetStoreUndo(Undo::DELETE);
484 if (mathcursor->pos() == mathcursor->array().size())
485 mathcursor->pullArg(true);
487 mathcursor->Delete();
488 bv->updateInset(this, true);
492 // sprintf(dispatch_buffer, "%d %d",);
493 // dispatch_result = dispatch_buffer;
497 lyxerr << "LFUN_SETXY broken!\n";
502 istringstream is(arg.c_str());
505 mathcursor->SetPos(x1 + x, y1 + y);
506 updateLocal(bv, false);
510 // cursor selection ----------------------------
514 mathcursor->MacroModeClose();
515 bv->lockedInsetStoreUndo(Undo::INSERT);
516 mathcursor->SelPaste();
517 updateLocal(bv, true);
521 bv->lockedInsetStoreUndo(Undo::DELETE);
522 mathcursor->SelCut();
523 updateLocal(bv, true);
527 mathcursor->SelCopy();
532 case LFUN_WORDRIGHTSEL:
533 case LFUN_WORDLEFTSEL:
536 // --- accented characters ------------------------------
538 case LFUN_UMLAUT: handleAccent(bv, "ddot", LM_ddot); break;
539 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat", LM_hat); break;
540 case LFUN_GRAVE: handleAccent(bv, "grave", LM_grave); break;
541 case LFUN_ACUTE: handleAccent(bv, "acute", LM_acute); break;
542 case LFUN_TILDE: handleAccent(bv, "tilde", LM_tilde); break;
543 case LFUN_MACRON: handleAccent(bv, "bar", LM_bar); break;
544 case LFUN_DOT: handleAccent(bv, "dot", LM_dot); break;
545 case LFUN_CARON: handleAccent(bv, "check", LM_check); break;
546 case LFUN_BREVE: handleAccent(bv, "breve", LM_breve); break;
547 case LFUN_VECTOR: handleAccent(bv, "vec", LM_vec); break;
551 if (!greek_kb_flag) {
553 bv->owner()->message(_("Math greek mode on"));
559 case LFUN_GREEK_TOGGLE:
560 greek_kb_flag = greek_kb_flag ? 0 : 2;
562 bv->owner()->message(_("Math greek keyboard on"));
564 bv->owner()->message(_("Math greek keyboard off"));
568 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
569 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
570 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
571 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
572 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
573 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
576 handleFont(bv, LM_TC_TEXTRM);
577 //bv->owner()->message(_("math text mode toggled"));
582 if (!mathcursor->Selection()) {
583 mathcursor->handleFont(LM_TC_TEX);
584 //bv->owner()->message(_("TeX mode toggled"));
589 case LFUN_MATH_LIMITS:
590 bv->lockedInsetStoreUndo(Undo::INSERT);
591 if (mathcursor->toggleLimits())
592 updateLocal(bv, true);
597 bv->lockedInsetStoreUndo(Undo::INSERT);
598 latexkeys const * l = in_word_set(arg);
599 mathcursor->SetSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
600 updateLocal(bv, true);
604 case LFUN_INSERT_MATRIX:
606 bv->lockedInsetStoreUndo(Undo::INSERT);
607 mathcursor->Interpret("matrix " + arg);
608 updateLocal(bv, true);
612 case LFUN_INSERT_MATH:
614 bv->lockedInsetStoreUndo(Undo::INSERT);
615 mathcursor->Interpret(arg);
616 updateLocal(bv, true);
620 case LFUN_MATH_DELIM:
622 bv->lockedInsetStoreUndo(Undo::INSERT);
623 static const string vdelim("(){}[]./|");
624 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
629 // try to read integers first
632 istringstream is(arg.c_str());
635 if (!is) { // ok, the beasties are no integers... try something else
639 istringstream is(arg.c_str());
643 //lyxerr << "formulabase::LFUN_MATH_DELIM, lt: '" << lt << "'\n";
644 //lyxerr << "formulabase::LFUN_MATH_DELIM, rt: '" << rt << "'\n";
647 latexkeys const * l = in_word_set(lt);
650 } else if (vdelim.find(lt[0]) != string::npos)
654 latexkeys const * l = in_word_set(rt);
657 } else if (vdelim.find(rt[0]) != string::npos)
661 handleDelim(bv, ilt, irt);
662 updateLocal(bv, true);
666 case LFUN_PROTECTEDSPACE:
667 bv->lockedInsetStoreUndo(Undo::INSERT);
668 mathcursor->insert(new MathSpaceInset(1));
669 updateLocal(bv, true);
673 bv->owner()->message(_("Invalid action in math mode!"));
677 case LFUN_MATH_HALIGN:
679 bv->lockedInsetStoreUndo(Undo::INSERT);
680 lyxerr << "handling halign '" << arg << "'\n";
682 MathArrayInset * p = matrixpar(idx);
685 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
686 updateLocal(bv, true);
690 case LFUN_MATH_VALIGN:
692 bv->lockedInsetStoreUndo(Undo::INSERT);
693 lyxerr << "handling valign '" << arg << "'\n";
695 MathArrayInset * p = matrixpar(idx);
698 p->valign(arg.size() ? arg[0] : 'c');
699 updateLocal(bv, true);
703 case LFUN_MATH_ROW_INSERT:
705 bv->lockedInsetStoreUndo(Undo::INSERT);
707 MathArrayInset * p = matrixpar(idx);
708 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
711 p->addRow(p->row(idx));
712 updateLocal(bv, true);
716 case LFUN_MATH_ROW_DELETE:
718 bv->lockedInsetStoreUndo(Undo::INSERT);
720 MathArrayInset * p = matrixpar(idx);
721 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
724 p->delRow(p->row(idx));
725 updateLocal(bv, true);
729 case LFUN_MATH_COLUMN_INSERT:
731 bv->lockedInsetStoreUndo(Undo::INSERT);
733 MathArrayInset * p = matrixpar(idx);
736 p->addCol(p->col(idx));
737 updateLocal(bv, true);
741 case LFUN_MATH_COLUMN_DELETE:
743 bv->lockedInsetStoreUndo(Undo::INSERT);
745 MathArrayInset * p = matrixpar(idx);
748 p->delCol(p->col(idx));
749 updateLocal(bv, true);
753 case LFUN_EXEC_COMMAND:
754 result = UNDISPATCHED;
758 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
759 unsigned char c = arg[0];
761 lyxerr << "Action: " << action << endl;
763 lyxerr << "char: '" << c << "' int: " << int(c) << endl;
764 //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);
765 //lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
766 bv->lockedInsetStoreUndo(Undo::INSERT);
768 if (c == 0) { // Dead key, do nothing
769 //lyxerr << "deadkey" << endl;
774 if (mathcursor->getLastCode() == LM_TC_TEX) {
775 mathcursor->MacroModeOpen();
776 mathcursor->clearLastCode();
778 } else if (!varcode) {
779 MathTextCodes f = mathcursor->getLastCode() ?
780 mathcursor->getLastCode() :
781 mathcursor->nextCode();
782 varcode = MathIsAlphaFont(f) ?
783 static_cast<MathTextCodes>(f) :
787 // lyxerr << "Varcode << vardoce;
788 MathTextCodes char_code = varcode;
791 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
792 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
793 'Y', 0, 0, 0, 0 , 'Z' };
795 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
796 char_code = LM_TC_RM;
799 char_code = LM_TC_SYMB;
802 mathcursor->insert(c, char_code);
804 if (greek_kb_flag && char_code == LM_TC_RM )
805 mathcursor->setLastCode(LM_TC_VAR);
809 if (greek_kb_flag < 2)
812 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
813 mathcursor->insert(c, LM_TC_TEX);
815 mathcursor->insert('}', LM_TC_TEX);
818 mathcursor->clearLastCode();
819 // varcode = LM_TC_MIN;
820 } else if (c == '_' && varcode == LM_TC_TEX) {
821 mathcursor->insert(c, LM_TC_SPECIAL);
822 mathcursor->clearLastCode();
823 // varcode = LM_TC_MIN;
824 } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
825 mathcursor->MacroModeOpen();
826 mathcursor->clearLastCode();
827 mathcursor->insert(c, LM_TC_MIN);
828 } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
829 MathTextCodes code = mathcursor->getLastCode();
830 if (code != LM_TC_TEXTRM)
832 mathcursor->insert(c, code);
833 } else if (strchr("+/-*<>=", c)) {
834 MathTextCodes code = mathcursor->getLastCode();
835 if (code != LM_TC_TEXTRM)
837 mathcursor->insert(c, code);
838 } else if (strchr(latex_special_chars, c) && c != '_') {
839 MathTextCodes code = mathcursor->getLastCode();
840 if (code != LM_TC_TEXTRM)
841 code = LM_TC_SPECIAL;
842 mathcursor->insert(c, code);
843 } else if (c == '_' || c == '^') {
847 mathcursor->Interpret(s);
848 } else if (c == ' ') {
850 MathTextCodes f = (mathcursor->getLastCode()) ?
851 mathcursor->getLastCode() :
852 mathcursor->nextCode();
853 varcode = MathIsAlphaFont(f) ? f : LM_TC_VAR;
856 if (varcode == LM_TC_TEXTRM)
857 mathcursor->insert(c, LM_TC_TEXTRM);
859 mathcursor->MacroModeClose();
860 else if (mathcursor->pop())
861 mathcursor->plainRight();
863 // this would not work if the inset is in an table!
864 //bv->text->cursorRight(bv, true);
867 } else if (c == '\'' || c == '@') {
868 mathcursor->insert(c, LM_TC_VAR);
869 } else if (c == '\\') {
871 mathcursor->MacroModeClose();
872 bv->owner()->message(_("TeX mode"));
873 mathcursor->setLastCode(LM_TC_TEX);
875 updateLocal(bv, true);
876 } else if (action == LFUN_MATH_PANEL) {
877 result = UNDISPATCHED;
879 lyxerr << "Closed by action " << action << endl;
884 mathcursor->normalize();
886 if (was_macro != mathcursor->InMacroMode()
887 && action >= 0 && action != LFUN_BACKSPACE)
888 updateLocal(bv, true);
890 if (mathcursor->Selection() || was_selection)
891 toggleInsetSelection(bv);
893 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
894 result == UNDISPATCHED)
897 bv->unlockInset(this);
899 return result; // original version
904 /* FIXME: math-greek-toggle seems to work OK, but math-greek doesn't turn
906 bool math_insert_greek(BufferView * bv, char c)
908 if (!bv->available())
916 if (!bv->theLockingInset() || bv->theLockingInset()->isTextInset()) {
917 int greek_kb_flag_save = greek_kb_flag;
918 InsetFormula * new_inset = new InsetFormula();
919 bv->beforeChange(bv->text);
920 if (!bv->insertInset(new_inset)) {
925 new_inset->edit(bv, 0, 0, 0);
926 new_inset->localDispatch(bv, LFUN_SELFINSERT, tmp);
927 if (greek_kb_flag_save < 2) {
928 bv->unlockInset(new_inset); // bv->theLockingInset());
929 bv->text->cursorRight(bv, true);
932 if (bv->theLockingInset()->lyxCode() == Inset::MATH_CODE ||
933 bv->theLockingInset()->lyxCode() == Inset::MATHMACRO_CODE)
934 static_cast<InsetFormula*>(bv->theLockingInset())->localDispatch(bv, LFUN_SELFINSERT, tmp);
936 lyxerr << "Math error: attempt to write on a wrong "
937 "class of inset." << endl;
943 Inset::Code InsetFormulaBase::lyxCode() const
945 return Inset::MATH_CODE;
949 LyXFont const InsetFormulaBase::convertFont(LyXFont const & f) const
951 // We have already discussed what was here
954 font.setLatex(LyXFont::OFF);
959 MathInset * InsetFormulaBase::par() const
965 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
967 if (bv->available()) {
968 // Feature "Read math inset from selection" disabled.
969 // // use selection if available..
971 // if (action == LFUN_MATH_IMPORT_SELECTION)
974 // sel = bv->getLyXText()->selectionAsString(bv->buffer());
977 // if (sel.empty()) {
978 f = new InsetFormula;
979 if (openNewInset(bv, f)) {
980 // don't do that also for LFUN_MATH_MODE unless you want end up with
981 // always changing to mathrm when opening an inlined inset
982 // -- I really hate "LyXfunc overloading"...
984 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
985 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
988 // f = new InsetFormula(sel);
989 // bv->getLyXText()->cutSelection(bv);
990 // openNewInset(bv, f);
993 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
996 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
998 mathDispatchCreation(bv, arg, true);
1001 void mathDispatchMathMode(BufferView * bv, string const & arg)
1003 mathDispatchCreation(bv, arg, false);
1006 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
1008 mathDispatchCreation(bv, arg, true);
1011 void mathDispatchMathMacro(BufferView * bv, string const & arg)
1013 if (bv->available()) {
1015 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
1018 string const s1 = token(s, ' ', 1);
1019 int const na = s1.empty() ? 0 : lyx::atoi(s1);
1020 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1025 void mathDispatchMathDelim(BufferView * bv, string const & arg)
1027 if (bv->available()) {
1028 if (openNewInset(bv, new InsetFormula))
1029 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
1034 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1036 if (bv->available()) {
1037 if (openNewInset(bv, new InsetFormula))
1038 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
1042 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1044 if (bv->available()) {
1045 if (arg.size() && arg[0] == '\\')
1046 openNewInset(bv, new InsetFormula(arg));
1048 mathDispatchMathMode(bv, arg);