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()
139 #warning leak this for a while...
145 void InsetFormulaBase::read(Buffer const *, LyXLex & lex)
150 void InsetFormulaBase::write(Buffer const *, ostream & os) const
155 int InsetFormulaBase::latex(Buffer const *, ostream & os,
156 bool fragile, bool spacing) const
158 return latex(os, fragile, spacing);
161 int InsetFormulaBase::ascii(Buffer const *, ostream & os, int spacing) const
163 return ascii(os, spacing);
166 int InsetFormulaBase::linuxdoc(Buffer const *, ostream & os) const
171 int InsetFormulaBase::docBook(Buffer const *, ostream & os) const
178 // Check if uses AMS macros
179 void InsetFormulaBase::validate(LaTeXFeatures &) const
183 string const InsetFormulaBase::editMessage() const
185 return _("Math editor mode");
189 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
191 mathcursor = new MathCursor(this);
193 if (!bv->lockInset(this))
194 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
197 //bv->updateInset(this, false);
208 void InsetFormulaBase::edit(BufferView * bv, bool front)
210 #warning Please have a look if this is right (Jug)
211 edit(bv, front ? 0 : 1, 0, 0);
215 void InsetFormulaBase::insetUnlock(BufferView * bv)
218 if (mathcursor->InMacroMode()) {
219 mathcursor->MacroModeClose();
220 updateLocal(bv, true);
225 bv->updateInset(this, false);
229 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
231 mathcursor->GetPos(x, y);
237 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
242 if (isCursorVisible())
243 bv->hideLockedInsetCursor();
247 mathcursor->GetPos(x, y);
252 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
253 bv->showLockedInsetCursor(x, y, asc, desc);
256 toggleCursorVisible();
260 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
262 if (!isCursorVisible()) {
266 mathcursor->GetPos(x, y);
271 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
272 bv->fitLockedInsetCursor(x, y, asc, desc);
274 toggleInsetCursor(bv);
279 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
281 if (isCursorVisible())
282 toggleInsetCursor(bv);
286 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
289 bv->updateInset(this, false);
293 vector<string> const InsetFormulaBase::getLabelList() const
295 return std::vector<string>();
299 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
302 bv->updateInset(this, dirty);
306 void InsetFormulaBase::Metrics() const
308 const_cast<MathInset *>(par_)->Metrics(LM_ST_TEXT);
312 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
313 int x, int y, int /*button*/)
319 mathcursor->SetPos(x, y);
326 bv->updateInset(this, false);
331 void InsetFormulaBase::insetButtonPress(BufferView * bv,
332 int x, int y, int /*button*/)
337 if (mathcursor && mathcursor->Selection()) {
338 mathcursor->SelClear();
339 bv->updateInset(this, false);
344 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
345 int x, int y, int /*button*/)
347 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
350 mathcursor->SetPos(sel_x + par_->xo(), sel_y + par_->yo());
351 mathcursor->SelStart();
353 mathcursor->GetPos(sel_x, sel_y);
354 } else if (sel_flag) {
358 mathcursor->SetPos(x, y);
360 mathcursor->GetPos(x, y);
361 if (sel_x != x || sel_y != y)
362 bv->updateInset(this, false);
369 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
371 lyxerr[Debug::MATHED]
372 << "Used InsetFormulaBase::InsetKeyPress." << endl;
377 UpdatableInset::RESULT
378 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
381 //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
382 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
387 MathTextCodes varcode = LM_TC_MIN;
388 bool was_macro = mathcursor->InMacroMode();
390 bool was_selection = mathcursor->Selection();
391 RESULT result = DISPATCHED;
395 if (mathcursor->getLastCode() == LM_TC_TEX)
398 mathcursor->normalize();
402 // --- Cursor Movements ---------------------------------------------
405 sel = true; // fall through...
408 result = DISPATCH_RESULT(mathcursor->Right(sel));
409 updateLocal(bv, false);
414 sel = true; // fall through
417 result = DISPATCH_RESULT(mathcursor->Left(sel));
418 updateLocal(bv, false);
426 result = DISPATCH_RESULT(mathcursor->Up(sel));
427 updateLocal(bv, false);
435 result = DISPATCH_RESULT(mathcursor->Down(sel));
436 updateLocal(bv, false);
441 updateLocal(bv, false);
446 updateLocal(bv, false);
449 case LFUN_DELETE_LINE_FORWARD:
450 bv->lockedInsetStoreUndo(Undo::DELETE);
451 mathcursor->DelLine();
452 updateLocal(bv, true);
456 mathcursor->idxNext();
457 updateLocal(bv, false);
461 mathcursor->idxPrev();
462 updateLocal(bv, false);
466 bv->lockedInsetStoreUndo(Undo::EDIT);
467 mathcursor->splitCell();
468 updateLocal(bv, true);
472 // if (!mathcursor->InMacroMode() && mathcursor->pos() == 0)
473 if (mathcursor->pos() == 0) {
474 bv->lockedInsetStoreUndo(Undo::DELETE);
475 mathcursor->pullArg(false);
476 bv->updateInset(this, true);
479 if (mathcursor->InMacroMode())
482 mathcursor->plainLeft();
486 bv->lockedInsetStoreUndo(Undo::DELETE);
487 if (mathcursor->pos() == mathcursor->array().size())
488 mathcursor->pullArg(true);
490 mathcursor->Delete();
491 bv->updateInset(this, true);
495 // sprintf(dispatch_buffer, "%d %d",);
496 // dispatch_result = dispatch_buffer;
500 lyxerr << "LFUN_SETXY broken!\n";
505 istringstream is(arg.c_str());
508 mathcursor->SetPos(x1 + x, y1 + y);
509 updateLocal(bv, false);
513 // cursor selection ----------------------------
517 mathcursor->MacroModeClose();
518 bv->lockedInsetStoreUndo(Undo::INSERT);
519 mathcursor->SelPaste();
520 updateLocal(bv, true);
524 bv->lockedInsetStoreUndo(Undo::DELETE);
525 mathcursor->SelCut();
526 updateLocal(bv, true);
530 mathcursor->SelCopy();
535 case LFUN_WORDRIGHTSEL:
536 case LFUN_WORDLEFTSEL:
539 // --- accented characters ------------------------------
541 case LFUN_UMLAUT: handleAccent(bv, "ddot", LM_ddot); break;
542 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat", LM_hat); break;
543 case LFUN_GRAVE: handleAccent(bv, "grave", LM_grave); break;
544 case LFUN_ACUTE: handleAccent(bv, "acute", LM_acute); break;
545 case LFUN_TILDE: handleAccent(bv, "tilde", LM_tilde); break;
546 case LFUN_MACRON: handleAccent(bv, "bar", LM_bar); break;
547 case LFUN_DOT: handleAccent(bv, "dot", LM_dot); break;
548 case LFUN_CARON: handleAccent(bv, "check", LM_check); break;
549 case LFUN_BREVE: handleAccent(bv, "breve", LM_breve); break;
550 case LFUN_VECTOR: handleAccent(bv, "vec", LM_vec); break;
554 if (!greek_kb_flag) {
556 bv->owner()->message(_("Math greek mode on"));
562 case LFUN_GREEK_TOGGLE:
563 greek_kb_flag = greek_kb_flag ? 0 : 2;
565 bv->owner()->message(_("Math greek keyboard on"));
567 bv->owner()->message(_("Math greek keyboard off"));
571 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
572 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
573 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
574 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
575 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
576 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
579 handleFont(bv, LM_TC_TEXTRM);
580 //bv->owner()->message(_("math text mode toggled"));
585 if (!mathcursor->Selection()) {
586 mathcursor->handleFont(LM_TC_TEX);
587 //bv->owner()->message(_("TeX mode toggled"));
592 case LFUN_MATH_LIMITS:
593 bv->lockedInsetStoreUndo(Undo::INSERT);
594 if (mathcursor->toggleLimits())
595 updateLocal(bv, true);
600 bv->lockedInsetStoreUndo(Undo::INSERT);
601 latexkeys const * l = in_word_set(arg);
602 mathcursor->SetSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
603 updateLocal(bv, true);
607 case LFUN_INSERT_MATRIX:
609 bv->lockedInsetStoreUndo(Undo::INSERT);
610 mathcursor->Interpret("matrix " + arg);
611 updateLocal(bv, true);
615 case LFUN_INSERT_MATH:
617 bv->lockedInsetStoreUndo(Undo::INSERT);
618 mathcursor->Interpret(arg);
619 updateLocal(bv, true);
623 case LFUN_MATH_DELIM:
625 bv->lockedInsetStoreUndo(Undo::INSERT);
626 static const string vdelim("(){}[]./|");
627 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
632 // try to read integers first
635 istringstream is(arg.c_str());
638 if (!is) { // ok, the beasties are no integers... try something else
642 istringstream is(arg.c_str());
646 //lyxerr << "formulabase::LFUN_MATH_DELIM, lt: '" << lt << "'\n";
647 //lyxerr << "formulabase::LFUN_MATH_DELIM, rt: '" << rt << "'\n";
650 latexkeys const * l = in_word_set(lt);
653 } else if (vdelim.find(lt[0]) != string::npos)
657 latexkeys const * l = in_word_set(rt);
660 } else if (vdelim.find(rt[0]) != string::npos)
664 handleDelim(bv, ilt, irt);
665 updateLocal(bv, true);
669 case LFUN_PROTECTEDSPACE:
670 bv->lockedInsetStoreUndo(Undo::INSERT);
671 mathcursor->insert(new MathSpaceInset(1));
672 updateLocal(bv, true);
676 bv->owner()->message(_("Invalid action in math mode!"));
680 case LFUN_MATH_HALIGN:
682 bv->lockedInsetStoreUndo(Undo::INSERT);
683 lyxerr << "handling halign '" << arg << "'\n";
685 MathArrayInset * p = matrixpar(idx);
688 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
689 updateLocal(bv, true);
693 case LFUN_MATH_VALIGN:
695 bv->lockedInsetStoreUndo(Undo::INSERT);
696 lyxerr << "handling valign '" << arg << "'\n";
698 MathArrayInset * p = matrixpar(idx);
701 p->valign(arg.size() ? arg[0] : 'c');
702 updateLocal(bv, true);
706 case LFUN_MATH_ROW_INSERT:
708 bv->lockedInsetStoreUndo(Undo::INSERT);
710 MathArrayInset * p = matrixpar(idx);
711 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
714 p->addRow(p->row(idx));
715 updateLocal(bv, true);
719 case LFUN_MATH_ROW_DELETE:
721 bv->lockedInsetStoreUndo(Undo::INSERT);
723 MathArrayInset * p = matrixpar(idx);
724 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
727 p->delRow(p->row(idx));
728 updateLocal(bv, true);
732 case LFUN_MATH_COLUMN_INSERT:
734 bv->lockedInsetStoreUndo(Undo::INSERT);
736 MathArrayInset * p = matrixpar(idx);
739 p->addCol(p->col(idx));
740 updateLocal(bv, true);
744 case LFUN_MATH_COLUMN_DELETE:
746 bv->lockedInsetStoreUndo(Undo::INSERT);
748 MathArrayInset * p = matrixpar(idx);
751 p->delCol(p->col(idx));
752 updateLocal(bv, true);
756 case LFUN_EXEC_COMMAND:
757 result = UNDISPATCHED;
761 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
762 unsigned char c = arg[0];
764 lyxerr << "Action: " << action << endl;
766 lyxerr << "char: '" << c << "' int: " << int(c) << endl;
767 //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt);
768 //lyxerr << "trans: '" << c << "' int: " << int(c) << endl;
769 bv->lockedInsetStoreUndo(Undo::INSERT);
771 if (c == 0) { // Dead key, do nothing
772 //lyxerr << "deadkey" << endl;
777 if (mathcursor->getLastCode() == LM_TC_TEX) {
778 mathcursor->MacroModeOpen();
779 mathcursor->clearLastCode();
781 } else if (!varcode) {
782 MathTextCodes f = mathcursor->getLastCode() ?
783 mathcursor->getLastCode() :
784 mathcursor->nextCode();
785 varcode = MathIsAlphaFont(f) ?
786 static_cast<MathTextCodes>(f) :
790 // lyxerr << "Varcode << vardoce;
791 MathTextCodes char_code = varcode;
794 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
795 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
796 'Y', 0, 0, 0, 0 , 'Z' };
798 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
799 char_code = LM_TC_RM;
802 char_code = LM_TC_SYMB;
805 mathcursor->insert(c, char_code);
807 if (greek_kb_flag && char_code == LM_TC_RM )
808 mathcursor->setLastCode(LM_TC_VAR);
812 if (greek_kb_flag < 2)
815 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
816 mathcursor->insert(c, LM_TC_TEX);
818 mathcursor->insert('}', LM_TC_TEX);
821 mathcursor->clearLastCode();
822 // varcode = LM_TC_MIN;
823 } else if (c == '_' && varcode == LM_TC_TEX) {
824 mathcursor->insert(c, LM_TC_SPECIAL);
825 mathcursor->clearLastCode();
826 // varcode = LM_TC_MIN;
827 } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
828 mathcursor->MacroModeOpen();
829 mathcursor->clearLastCode();
830 mathcursor->insert(c, LM_TC_MIN);
831 } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
832 MathTextCodes code = mathcursor->getLastCode();
833 if (code != LM_TC_TEXTRM)
835 mathcursor->insert(c, code);
836 } else if (strchr("+/-*<>=", c)) {
837 MathTextCodes code = mathcursor->getLastCode();
838 if (code != LM_TC_TEXTRM)
840 mathcursor->insert(c, code);
841 } else if (strchr(latex_special_chars, c) && c != '_') {
842 MathTextCodes code = mathcursor->getLastCode();
843 if (code != LM_TC_TEXTRM)
844 code = LM_TC_SPECIAL;
845 mathcursor->insert(c, code);
846 } else if (c == '_' || c == '^') {
850 mathcursor->Interpret(s);
851 } else if (c == ' ') {
853 MathTextCodes f = (mathcursor->getLastCode()) ?
854 mathcursor->getLastCode() :
855 mathcursor->nextCode();
856 varcode = MathIsAlphaFont(f) ? f : LM_TC_VAR;
859 if (varcode == LM_TC_TEXTRM)
860 mathcursor->insert(c, LM_TC_TEXTRM);
862 mathcursor->MacroModeClose();
863 else if (mathcursor->pop())
864 mathcursor->plainRight();
866 // this would not work if the inset is in an table!
867 //bv->text->cursorRight(bv, true);
870 } else if (c == '\'' || c == '@') {
871 mathcursor->insert(c, LM_TC_VAR);
872 } else if (c == '\\') {
874 mathcursor->MacroModeClose();
875 bv->owner()->message(_("TeX mode"));
876 mathcursor->setLastCode(LM_TC_TEX);
878 updateLocal(bv, true);
879 } else if (action == LFUN_MATH_PANEL) {
880 result = UNDISPATCHED;
882 lyxerr << "Closed by action " << action << endl;
887 mathcursor->normalize();
889 if (was_macro != mathcursor->InMacroMode()
890 && action >= 0 && action != LFUN_BACKSPACE)
891 updateLocal(bv, true);
893 if (mathcursor->Selection() || was_selection)
894 toggleInsetSelection(bv);
896 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
897 result == UNDISPATCHED)
900 bv->unlockInset(this);
902 return result; // original version
907 /* FIXME: math-greek-toggle seems to work OK, but math-greek doesn't turn
909 bool math_insert_greek(BufferView * bv, char c)
911 if (!bv->available())
919 if (!bv->theLockingInset() || bv->theLockingInset()->isTextInset()) {
920 int greek_kb_flag_save = greek_kb_flag;
921 InsetFormula * new_inset = new InsetFormula();
922 bv->beforeChange(bv->text);
923 if (!bv->insertInset(new_inset)) {
928 new_inset->edit(bv, 0, 0, 0);
929 new_inset->localDispatch(bv, LFUN_SELFINSERT, tmp);
930 if (greek_kb_flag_save < 2) {
931 bv->unlockInset(new_inset); // bv->theLockingInset());
932 bv->text->cursorRight(bv, true);
935 if (bv->theLockingInset()->lyxCode() == Inset::MATH_CODE ||
936 bv->theLockingInset()->lyxCode() == Inset::MATHMACRO_CODE)
937 static_cast<InsetFormula*>(bv->theLockingInset())->localDispatch(bv, LFUN_SELFINSERT, tmp);
939 lyxerr << "Math error: attempt to write on a wrong "
940 "class of inset." << endl;
946 Inset::Code InsetFormulaBase::lyxCode() const
948 return Inset::MATH_CODE;
952 LyXFont const InsetFormulaBase::convertFont(LyXFont const & f) const
954 // We have already discussed what was here
957 font.setLatex(LyXFont::OFF);
962 MathInset * InsetFormulaBase::par() const
968 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
970 if (bv->available()) {
971 // Feature "Read math inset from selection" disabled.
972 // // use selection if available..
974 // if (action == LFUN_MATH_IMPORT_SELECTION)
977 // sel = bv->getLyXText()->selectionAsString(bv->buffer());
980 // if (sel.empty()) {
981 f = new InsetFormula;
982 if (openNewInset(bv, f)) {
983 // don't do that also for LFUN_MATH_MODE unless you want end up with
984 // always changing to mathrm when opening an inlined inset
985 // -- I really hate "LyXfunc overloading"...
987 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
988 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
991 // f = new InsetFormula(sel);
992 // bv->getLyXText()->cutSelection(bv);
993 // openNewInset(bv, f);
996 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
999 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
1001 mathDispatchCreation(bv, arg, true);
1004 void mathDispatchMathMode(BufferView * bv, string const & arg)
1006 mathDispatchCreation(bv, arg, false);
1009 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
1011 mathDispatchCreation(bv, arg, true);
1014 void mathDispatchMathMacro(BufferView * bv, string const & arg)
1016 if (bv->available()) {
1018 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
1021 string const s1 = token(s, ' ', 1);
1022 int const na = s1.empty() ? 0 : lyx::atoi(s1);
1023 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
1028 void mathDispatchMathDelim(BufferView * bv, string const & arg)
1030 if (bv->available()) {
1031 if (openNewInset(bv, new InsetFormula))
1032 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
1037 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
1039 if (bv->available()) {
1040 if (openNewInset(bv, new InsetFormula))
1041 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
1045 void mathDispatchInsertMath(BufferView * bv, string const & arg)
1047 if (bv->available()) {
1048 if (arg.size() && arg[0] == '\\')
1049 openNewInset(bv, new InsetFormula(arg));
1051 mathDispatchMathMode(bv, arg);