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 "commandtags.h"
27 #include "math_cursor.h"
28 #include "math_parser.h"
29 #include "BufferView.h"
32 #include "LaTeXFeatures.h"
34 #include "support/LOstream.h"
38 #include "math_arrayinset.h"
39 #include "math_spaceinset.h"
40 #include "math_deliminset.h"
41 #include "support/lyxlib.h"
42 #include "mathed/support.h"
48 extern char const * latex_special_chars;
50 int greek_kb_flag = 0;
51 extern char const * latex_mathenv[];
52 LyXFont * Math_Fonts = 0;
53 MathCursor * mathcursor = 0;
64 void mathed_init_fonts();
66 string nicelabel(string const & label)
68 return label.empty() ? string("(#)") : "(" + label + ")";
75 LyXFont WhichFont(short type, int size)
112 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
125 if (type == LM_TC_BSYM) {
138 case LM_ST_SCRIPTSCRIPT:
144 lyxerr << "Math Error: wrong font size: " << size << endl;
148 if (type != LM_TC_TEXTRM)
149 f.setColor(LColor::math);
157 void mathed_init_fonts()
159 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
162 for (int i = 0 ; i < 8 ; ++i) {
163 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
166 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
168 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
170 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
171 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
173 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
175 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
176 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
178 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
180 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
182 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
186 // returns the nearest enclosing matrix
187 MathArrayInset * matrixpar(int & idx)
191 static_cast<MathArrayInset *>
192 (mathcursor ? mathcursor->enclosing(LM_OT_MATRIX, idx) : 0);
199 InsetFormulaBase::InsetFormulaBase(MathInset * par)
204 InsetFormulaBase::InsetFormulaBase(InsetFormulaBase const & f)
205 : UpdatableInset(f), par_(static_cast<MathInset *>(f.par_->clone()))
209 InsetFormulaBase::~InsetFormulaBase()
212 #warning leak this for a while...
218 void InsetFormulaBase::write(Buffer const * buf, ostream & os) const
221 latex(buf, os, false, false);
225 int InsetFormulaBase::ascii(Buffer const *, ostream & os, int) const
227 par_->Write(os, false);
232 int InsetFormulaBase::linuxdoc(Buffer const * buf, ostream & os) const
234 return ascii(buf, os, 0);
238 int InsetFormulaBase::docBook(Buffer const * buf, ostream & os) const
240 return ascii(buf, os, 0);
244 // Check if uses AMS macros
245 void InsetFormulaBase::validate(LaTeXFeatures &) const
249 string const InsetFormulaBase::editMessage() const
251 return _("Math editor mode");
255 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
257 mathcursor = new MathCursor(this);
259 if (!bv->lockInset(this))
260 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
262 par_->Metrics(LM_ST_TEXT);
263 bv->updateInset(this, false);
275 void InsetFormulaBase::insetUnlock(BufferView * bv)
278 if (mathcursor->InMacroMode()) {
279 mathcursor->MacroModeClose();
285 bv->updateInset(this, false);
289 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
291 mathcursor->GetPos(x, y);
297 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
302 if (isCursorVisible())
303 bv->hideLockedInsetCursor();
307 mathcursor->GetPos(x, y);
311 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
312 int const asc = lyxfont::maxAscent(font);
313 int const desc = lyxfont::maxDescent(font);
315 bv->showLockedInsetCursor(x, y, asc, desc);
318 toggleCursorVisible();
322 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
324 if (!isCursorVisible()) {
328 mathcursor->GetPos(x, y);
331 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
332 int const asc = lyxfont::maxAscent(font);
333 int const desc = lyxfont::maxDescent(font);
334 bv->fitLockedInsetCursor(x, y, asc, desc);
336 toggleInsetCursor(bv);
341 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
343 if (isCursorVisible())
344 toggleInsetCursor(bv);
348 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
353 bv->updateInset(this, false);
357 vector<string> const InsetFormulaBase::getLabelList() const
359 return std::vector<string>();
363 void InsetFormulaBase::updateLocal(BufferView * bv)
365 par_->Metrics(LM_ST_TEXT);
366 bv->updateInset(this, true);
370 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
371 int x, int y, int /*button*/)
377 mathcursor->SetPos(x, y);
383 bv->updateInset(this, false);
389 void InsetFormulaBase::insetButtonPress(BufferView * bv,
390 int x, int y, int /*button*/)
395 if (mathcursor && mathcursor->Selection()) {
396 mathcursor->SelClear();
397 bv->updateInset(this, false);
402 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
403 int x, int y, int /*button*/)
405 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
408 mathcursor->SetPos(sel_x + par_->xo(), sel_y + par_->yo());
409 mathcursor->SelStart();
411 mathcursor->GetPos(sel_x, sel_y);
412 } else if (sel_flag) {
416 mathcursor->SetPos(x, y);
418 mathcursor->GetPos(x, y);
419 if (sel_x != x || sel_y != y)
420 bv->updateInset(this, false);
427 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
429 lyxerr[Debug::MATHED]
430 << "Used InsetFormulaBase::InsetKeyPress." << endl;
434 UpdatableInset::RESULT
435 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
438 //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
439 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
440 // extern char *dispatch_result;
445 MathTextCodes varcode = LM_TC_MIN;
446 bool was_macro = mathcursor->InMacroMode();
448 bool space_on = false;
449 bool was_selection = mathcursor->Selection();
450 RESULT result = DISPATCHED;
451 static MathSpaceInset * sp = 0;
455 if (mathcursor->getLastCode() == LM_TC_TEX)
458 mathcursor->normalize();
462 // --- Cursor Movements ---------------------------------------------
465 sel = true; // fall through...
468 result = DISPATCH_RESULT(mathcursor->Right(sel));
474 sel = true; // fall through
477 result = DISPATCH_RESULT(mathcursor->Left(sel));
486 result = DISPATCH_RESULT(mathcursor->Up(sel));
495 result = DISPATCH_RESULT(mathcursor->Down(sel));
502 result = DISPATCHED_NOUPDATE;
507 result = DISPATCHED_NOUPDATE;
510 case LFUN_DELETE_LINE_FORWARD:
511 bv->lockedInsetStoreUndo(Undo::DELETE);
512 mathcursor->DelLine();
517 bv->lockedInsetStoreUndo(Undo::INSERT);
518 mathcursor->idxRight();
523 bv->lockedInsetStoreUndo(Undo::INSERT);
524 mathcursor->idxRight();
529 if (!mathcursor->InMacroMode() && mathcursor->pos() == 0) {
530 bv->lockedInsetStoreUndo(Undo::DELETE);
531 mathcursor->pullArg();
532 bv->updateInset(this, true);
535 if (!mathcursor->Left())
540 bv->lockedInsetStoreUndo(Undo::DELETE);
541 mathcursor->Delete();
542 bv->updateInset(this, true);
546 // sprintf(dispatch_buffer, "%d %d",);
547 // dispatch_result = dispatch_buffer;
551 lyxerr << "LFUN_SETXY broken!\n";
556 istringstream is(arg);
558 lyxerr << "LFUN_SETXY: x: " << x << " y: " << y << "\n";
560 mathcursor->SetPos(x1 + x, y1 + y);
564 // cursor selection ----------------------------
568 mathcursor->MacroModeClose();
569 bv->lockedInsetStoreUndo(Undo::INSERT);
570 mathcursor->SelPaste();
575 bv->lockedInsetStoreUndo(Undo::DELETE);
576 mathcursor->SelCut();
581 mathcursor->SelCopy();
586 case LFUN_WORDRIGHTSEL:
587 case LFUN_WORDLEFTSEL:
590 // --- accented characters ------------------------------
592 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
593 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
594 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
595 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
596 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
597 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
598 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
599 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
600 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
601 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
605 if (!greek_kb_flag) {
607 bv->owner()->message(_("Math greek mode on"));
613 case LFUN_GREEK_TOGGLE:
614 greek_kb_flag = greek_kb_flag ? 0 : 2;
616 bv->owner()->message(_("Math greek keyboard on"));
618 bv->owner()->message(_("Math greek keyboard off"));
622 case LFUN_BOLD: mathcursor->toggleLastCode(LM_TC_BF); break;
623 case LFUN_SANS: mathcursor->toggleLastCode(LM_TC_SF); break;
624 case LFUN_EMPH: mathcursor->toggleLastCode(LM_TC_CAL); break;
625 case LFUN_ROMAN: mathcursor->toggleLastCode(LM_TC_RM); break;
626 case LFUN_CODE: mathcursor->toggleLastCode(LM_TC_TT); break;
627 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR); break;
631 #warning This needs a fix.
632 // Can we use the ERT inset here? (Lgb)
635 // varcode = LM_TC_TEX;
636 mathcursor->setLastCode(LM_TC_TEX);
637 bv->owner()->message(_("TeX mode"));
640 case LFUN_MATH_LIMITS:
641 bv->lockedInsetStoreUndo(Undo::INSERT);
642 if (mathcursor->toggleLimits())
648 bv->lockedInsetStoreUndo(Undo::INSERT);
649 latexkeys const * l = in_word_set(arg);
650 mathcursor->SetSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
655 case LFUN_INSERT_MATH:
657 bv->lockedInsetStoreUndo(Undo::INSERT);
658 mathcursor->Interpret(arg);
663 case LFUN_INSERT_MATRIX:
665 bv->lockedInsetStoreUndo(Undo::INSERT);
670 istringstream is(arg);
671 is >> m >> n >> v_align >> h_align;
672 MathArrayInset * p = new MathArrayInset(m, n);
673 p->valign(v_align[0]);
675 mathcursor->insert(p);
680 case LFUN_MATH_DELIM:
682 bv->lockedInsetStoreUndo(Undo::INSERT);
685 static const string vdelim("(){}[]./|");
686 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
691 istringstream is(arg);
695 lyxerr << "formulabase::LFUN_MATH_DELIM, lt: '" << lt << "'\n";
696 lyxerr << "formulabase::LFUN_MATH_DELIM, rt: '" << rt << "'\n";
699 latexkeys const * l = in_word_set(lt);
702 } else if (vdelim.find(lt[0]) != string::npos)
706 latexkeys const * l = in_word_set(rt);
709 } else if (vdelim.find(rt[0]) != string::npos)
712 if (mathcursor->selection) {
713 MathDelimInset * p = new MathDelimInset(ilt, irt);
715 mathcursor->selArray(ar);
716 lyxerr << "selarray: " << ar << "\n";
718 mathcursor->insert(p);
720 mathcursor->insert(new MathDelimInset(ilt, irt));
726 case LFUN_PROTECTEDSPACE:
727 bv->lockedInsetStoreUndo(Undo::INSERT);
728 mathcursor->insert(new MathSpaceInset(1));
733 // Invalid actions under math mode
735 if (mathcursor->getLastCode() != LM_TC_TEXTRM) {
736 bv->owner()->message(_("math text mode"));
737 varcode = LM_TC_TEXTRM;
740 mathcursor->setLastCode(varcode);
744 bv->owner()->message(_("Invalid action in math mode!"));
748 case LFUN_MATH_HALIGN:
750 bv->lockedInsetStoreUndo(Undo::INSERT);
751 lyxerr << "handling halign '" << arg << "'\n";
753 MathArrayInset * p = matrixpar(idx);
756 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
761 case LFUN_MATH_VALIGN:
763 bv->lockedInsetStoreUndo(Undo::INSERT);
764 lyxerr << "handling valign '" << arg << "'\n";
766 MathArrayInset * p = matrixpar(idx);
769 p->valign(arg.size() ? arg[0] : 'c');
774 case LFUN_MATH_ROW_INSERT:
776 bv->lockedInsetStoreUndo(Undo::INSERT);
778 MathArrayInset * p = matrixpar(idx);
779 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
782 p->addRow(p->row(idx));
787 case LFUN_MATH_ROW_DELETE:
789 bv->lockedInsetStoreUndo(Undo::INSERT);
791 MathArrayInset * p = matrixpar(idx);
792 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
795 p->delRow(p->row(idx));
800 case LFUN_MATH_COLUMN_INSERT:
802 bv->lockedInsetStoreUndo(Undo::INSERT);
804 MathArrayInset * p = matrixpar(idx);
807 p->addCol(p->col(idx));
812 case LFUN_MATH_COLUMN_DELETE:
814 bv->lockedInsetStoreUndo(Undo::INSERT);
816 MathArrayInset * p = matrixpar(idx);
819 p->delCol(p->col(idx));
824 case LFUN_EXEC_COMMAND:
825 result = UNDISPATCHED;
829 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
830 unsigned char c = arg[0];
831 bv->lockedInsetStoreUndo(Undo::INSERT);
833 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
835 mathcursor->setAccent(0);
838 if (c == 0) { // Dead key, do nothing
839 //lyxerr << "deadkey" << endl;
844 if (mathcursor->getLastCode() == LM_TC_TEX) {
845 mathcursor->MacroModeOpen();
846 mathcursor->clearLastCode();
848 } else if (!varcode) {
849 short f = mathcursor->getLastCode() ?
850 mathcursor->getLastCode() :
851 mathcursor->nextCode();
852 varcode = MathIsAlphaFont(f) ?
853 static_cast<MathTextCodes>(f) :
857 // lyxerr << "Varcode << vardoce;
858 MathTextCodes char_code = varcode;
861 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
862 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
863 'Y', 0, 0, 0, 0 , 'Z' };
865 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
866 char_code = LM_TC_RM;
869 char_code = LM_TC_SYMB;
872 mathcursor->insert(c, char_code);
874 if (greek_kb_flag && char_code == LM_TC_RM )
875 mathcursor->setLastCode(LM_TC_VAR);
879 if (greek_kb_flag < 2)
882 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
883 mathcursor->insert(c, LM_TC_TEX);
885 mathcursor->insert('}', LM_TC_TEX);
888 mathcursor->clearLastCode();
889 // varcode = LM_TC_MIN;
890 } else if (c == '_' && varcode == LM_TC_TEX) {
891 mathcursor->insert(c, LM_TC_SPECIAL);
892 mathcursor->clearLastCode();
893 // varcode = LM_TC_MIN;
894 } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
895 mathcursor->MacroModeOpen();
896 mathcursor->clearLastCode();
897 mathcursor->insert(c, LM_TC_MIN);
898 } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
899 MathTextCodes code = mathcursor->getLastCode();
900 if (code != LM_TC_TEXTRM)
902 mathcursor->insert(c, code);
903 } else if (strchr("+/-*<>=", c)) {
904 MathTextCodes code = mathcursor->getLastCode();
905 if (code != LM_TC_TEXTRM)
907 mathcursor->insert(c, code);
908 } else if (strchr(latex_special_chars, c) && c!= '_') {
909 MathTextCodes code = mathcursor->getLastCode();
910 if (code != LM_TC_TEXTRM)
911 code = LM_TC_SPECIAL;
912 mathcursor->insert(c, code);
913 } else if (c == '_' || c == '^') {
917 mathcursor->Interpret(s);
918 } else if (c == ' ') {
920 short f = (mathcursor->getLastCode()) ?
921 mathcursor->getLastCode() :
922 mathcursor->nextCode();
923 varcode = MathIsAlphaFont(f) ?
924 static_cast<MathTextCodes>(f) :
928 if (varcode == LM_TC_TEXTRM) {
929 mathcursor->insert(c, LM_TC_TEXTRM);
930 } else if (was_macro) {
931 mathcursor->MacroModeClose();
933 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
937 if (!mathcursor->pop())
939 mathcursor->plainRight();
941 } else if (c == '\'' || c == '@') {
942 mathcursor->insert (c, LM_TC_VAR);
943 } else if (c == '\\') {
945 mathcursor->MacroModeClose();
946 bv->owner()->message(_("TeX mode"));
947 mathcursor->setLastCode(LM_TC_TEX);
950 } else if (action == LFUN_MATH_PANEL) {
951 result = UNDISPATCHED;
953 lyxerr << "Closed by action " << action << endl;
959 mathcursor->normalize();
961 if (mathcursor && was_macro != mathcursor->InMacroMode()
963 && action != LFUN_BACKSPACE)
972 if (mathcursor && (mathcursor->Selection() || was_selection))
973 toggleInsetSelection(bv);
975 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
976 result == UNDISPATCHED)
979 bv->unlockInset(this);
981 return result; // original version
986 /* FIXME: math-greek-toggle seems to work OK, but math-greek doesn't turn
988 bool math_insert_greek(BufferView * bv, char c)
990 if (!bv->available())
998 if (!bv->theLockingInset() || bv->theLockingInset()->isTextInset()) {
999 int greek_kb_flag_save = greek_kb_flag;
1000 InsetFormula * new_inset = new InsetFormula();
1001 bv->beforeChange(bv->text);
1002 if (!bv->insertInset(new_inset)) {
1007 new_inset->edit(bv, 0, 0, 0);
1008 new_inset->localDispatch(bv, LFUN_SELFINSERT, tmp);
1009 if (greek_kb_flag_save < 2) {
1010 bv->unlockInset(new_inset); // bv->theLockingInset());
1011 bv->text->cursorRight(bv, true);
1014 if (bv->theLockingInset()->lyxCode() == Inset::MATH_CODE ||
1015 bv->theLockingInset()->lyxCode() == Inset::MATHMACRO_CODE)
1016 static_cast<InsetFormula*>(bv->theLockingInset())->localDispatch(bv, LFUN_SELFINSERT, tmp);
1018 lyxerr << "Math error: attempt to write on a wrong "
1019 "class of inset." << endl;
1025 Inset::Code InsetFormulaBase::lyxCode() const
1027 return Inset::MATH_CODE;
1031 LyXFont const InsetFormulaBase::convertFont(LyXFont const & f) const
1033 // We have already discussed what was here
1036 font.setLatex(LyXFont::OFF);
1041 MathInset * InsetFormulaBase::par() const