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.
21 #pragma implementation "formula.h"
25 #include "commandtags.h"
26 #include "math_cursor.h"
27 #include "math_parser.h"
29 #include "minibuffer.h"
30 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
35 #include "lyx_gui_misc.h"
36 #include "support/LOstream.h"
40 #include "support/lyxlib.h"
42 #include "math_inset.h"
43 #include "math_parinset.h"
44 #include "math_matrixinset.h"
45 #include "math_rowst.h"
46 #include "math_spaceinset.h"
47 #include "math_deliminset.h"
48 #include "mathed/support.h"
57 extern string mathed_label;
59 extern char const * latex_special_chars;
61 int greek_kb_flag = 0;
63 // this is only used by Whichfont and mathed_init_fonts (Lgb)
64 LyXFont * Math_Fonts = 0;
66 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
73 MathedCursor * InsetFormula::mathcursor = 0;
75 void mathed_init_fonts();
79 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
82 LyXFont WhichFont(short type, int size)
119 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
130 f.setSize(lfont_size);
134 if (type == LM_TC_BSYM) {
147 case LM_ST_SCRIPTSCRIPT:
153 lyxerr << "Mathed Error: wrong font size: " << size << endl;
157 if (type != LM_TC_TEXTRM)
158 f.setColor(LColor::math);
164 void mathed_init_fonts() //removed 'static' because DEC cxx does not
166 // Probably because this func is declared as a friend in math_defs.h
169 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
172 for (int i = 0 ; i < 8 ; ++i) {
173 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
176 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
178 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
180 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
181 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
183 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
185 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
186 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
188 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
190 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
192 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
194 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
195 MathedInset::df_asc = lyxfont::maxAscent(f);
196 MathedInset::df_des = lyxfont::maxDescent(f);
197 MathedInset::df_width = lyxfont::width('I', f);
202 InsetFormula::InsetFormula(bool display)
204 par = new MathParInset; // this leaks
209 par->SetType(LM_OT_PAR);
210 par->SetStyle(LM_ST_DISPLAY);
215 InsetFormula::InsetFormula(MathParInset * p)
217 if (is_matrix_type(p->GetType()))
218 lyxerr << "InsetFormula::InsetFormula: This shouldn't happen" << endl;
220 par = is_multiline(p->GetType()) ?
221 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
225 disp_flag = (par->GetType()>0);
230 InsetFormula::~InsetFormula()
236 Inset * InsetFormula::Clone(Buffer const &) const
238 InsetFormula * f = new InsetFormula(par);
244 void InsetFormula::Write(Buffer const * buf, ostream & os) const
247 Latex(buf, os, false, false);
251 int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
254 //#warning Alejandro, the number of lines is not returned in this case
255 // This problem will disapear at 0.13.
256 mathed_write(par, os, &ret, fragile, label);
261 int InsetFormula::Ascii(Buffer const *, ostream & os, int) const
263 par->Write(os, false);
268 int InsetFormula::Linuxdoc(Buffer const * buf, ostream & os) const
270 return Ascii(buf, os, 0);
274 int InsetFormula::DocBook(Buffer const * buf, ostream & os) const
276 return Ascii(buf, os, 0);
280 // Check if uses AMS macros
281 void InsetFormula::Validate(LaTeXFeatures & features) const
283 if (is_ams(par->GetType()))
284 features.amsstyle = true;
286 // Validation is necessary only if not using AMS math.
287 // To be safe, we will always run mathedValidate.
288 //if (!features.amsstyle)
289 mathedValidate(features, par);
293 void InsetFormula::Read(Buffer const *, LyXLex & lex)
295 istream & is = lex.getStream();
297 mathed_parser_file(is, lex.GetLineNo());
299 // Silly hack to read labels.
300 mathed_label.erase();
302 mathed_parse(0, 0, &par);
304 disp_flag = (par->GetType() > 0);
306 // Update line number
307 lex.setLineNo(mathed_parser_lineno());
309 if (!mathed_label.empty()) {
310 label = mathed_label;
311 mathed_label.erase();
314 // reading of end_inset in the inset!!!
317 if (lex.GetString() == "\\end_inset")
319 lyxerr << "InsetFormula::Read: Garbage before \\end_inset,"
320 " or missing \\end_inset!" << endl;
329 int InsetFormula::ascent(BufferView *, LyXFont const &) const
331 return par->Ascent() + (disp_flag ? 8 : 1);
335 int InsetFormula::descent(BufferView *, LyXFont const &) const
337 return par->Descent() + (disp_flag ? 8 : 1);
341 int InsetFormula::width(BufferView * bv, LyXFont const & f) const
343 MathedInset::workWidth = bv->workWidth();
344 lfont_size = f.size();
346 return par->Width(); //+2;
350 void InsetFormula::draw(BufferView * bv, LyXFont const & f,
351 int baseline, float & x, bool) const
353 MathedInset::workWidth = bv->workWidth();
354 Painter & pain = bv->painter();
355 // Seems commenting out solves a problem.
356 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
357 font.setSize(f.size());
358 lfont_size = font.size();
359 /// Let's try to wait a bit with this... (Lgb)
360 //UpdatableInset::draw(pain, font, baseline, x);
362 // otherwise a segfault could occur
363 // in some XDrawRectangles (i.e. matrix) (Matthias)
364 if (mathcursor && mathcursor->GetPar() == par) {
365 if (mathcursor->Selection()) {
369 mathcursor->SelGetArea(&xp, &yp, n);
370 pain.fillPolygon(xp, yp, n, LColor::selection);
372 mathcursor->draw(pain, int(x), baseline);
374 par->draw(pain, int(x), baseline);
376 x += float(width(bv, font));
378 if (is_numbered(par->GetType())) {
379 LyXFont wfont = WhichFont(LM_TC_BF, par->size);
380 wfont.setLatex(LyXFont::OFF);
382 if (is_singlely_numbered(par->GetType())) {
385 str = string("(") + label + ")";
388 pain.text(int(x + 20), baseline, str, wfont);
390 MathMatrixInset * mt =
391 static_cast<MathMatrixInset*>(par);
393 MathedRowSt const * crow = mt->getRowSt();
395 y = baseline + crow->getBaseline();
396 if (crow->isNumbered()) {
398 if (!crow->getLabel().empty())
399 str = string("(") + crow->getLabel() + ")";
402 pain.text(int(x + 20), y, str, wfont);
404 crow = crow->getNext();
408 cursor_visible = false;
412 string const InsetFormula::EditMessage() const
414 return _("Math editor mode");
418 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
420 mathcursor = new MathedCursor(par);
422 if (!bv->lockInset(this))
423 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
426 bv->updateInset(this, false);
429 mathcursor->SetPos(x, y);
436 void InsetFormula::InsetUnlock(BufferView * bv)
439 if (mathcursor->InMacroMode()) {
440 mathcursor->MacroModeClose();
446 bv->updateInset(this, false);
450 // Now a symbol can be inserted only if the inset is locked
451 void InsetFormula::InsertSymbol(BufferView * bv, string const & s)
453 if (s.empty() || !mathcursor)
455 mathcursor->Interpret(s);
460 void InsetFormula::GetCursorPos(BufferView *, int & x, int & y) const
462 mathcursor->GetPos(x, y);
468 void InsetFormula::ToggleInsetCursor(BufferView * bv)
475 mathcursor->GetPos(x, y);
478 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
479 int asc = lyxfont::maxAscent(font);
480 int desc = lyxfont::maxDescent(font);
483 bv->hideLockedInsetCursor();
485 bv->showLockedInsetCursor(x, y, asc, desc);
487 cursor_visible = !cursor_visible;
491 void InsetFormula::ShowInsetCursor(BufferView * bv, bool)
493 if (!cursor_visible) {
497 mathcursor->GetPos(x, y);
500 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
501 int asc = lyxfont::maxAscent(font);
502 int desc = lyxfont::maxDescent(font);
503 bv->fitLockedInsetCursor(x, y, asc, desc);
505 ToggleInsetCursor(bv);
510 void InsetFormula::HideInsetCursor(BufferView * bv)
513 ToggleInsetCursor(bv);
517 void InsetFormula::ToggleInsetSelection(BufferView * bv)
522 bv->updateInset(this, false);
526 void InsetFormula::display(bool dspf)
528 if (dspf != disp_flag) {
530 par->SetType(LM_OT_PAR);
531 par->SetStyle(LM_ST_DISPLAY);
533 if (is_multiline(par->GetType())) {
534 MathParInset * p = new MathParInset(par);
538 mathcursor->SetPar(par);
540 par->SetType(LM_OT_MIN);
541 par->SetStyle(LM_ST_TEXT);
542 if (!label.empty()) {
551 vector<string> const InsetFormula::getLabelList() const
553 //#warning This is dirty, I know. Ill clean it at 0.11
554 // Correction, the only way to clean this is with a new kernel: 0.13.
556 vector<string> label_list;
558 if (is_multi_numbered(par->GetType())) {
559 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
560 MathedRowSt const * crow = mt->getRowSt();
562 if (!crow->getLabel().empty())
563 label_list.push_back(crow->getLabel());
564 crow = crow->getNext();
566 } else if (!label.empty())
567 label_list.push_back(label);
573 void InsetFormula::UpdateLocal(BufferView * bv)
575 par->Metrics(); // To inform lyx kernel the exact size
576 // (there were problems with arrays).
577 bv->updateInset(this, true);
581 void InsetFormula::InsetButtonRelease(BufferView * bv,
582 int x, int y, int /*button*/)
588 mathcursor->SetPos(x, y);
593 bv->updateInset(this, false);
599 void InsetFormula::InsetButtonPress(BufferView * bv, int x, int y,
603 sel_x = x; sel_y = y;
604 if (mathcursor && mathcursor->Selection()) {
605 mathcursor->SelClear();
606 bv->updateInset(this, false);
611 void InsetFormula::InsetMotionNotify(BufferView * bv,
612 int x, int y, int /*button*/)
614 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
617 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
618 mathcursor->SelStart();
620 mathcursor->GetPos(sel_x, sel_y);
621 } else if (sel_flag) {
625 mathcursor->SetPos(x, y);
627 mathcursor->GetPos(x, y);
628 if (sel_x != x || sel_y != y)
629 bv->updateInset(this, false);
636 void InsetFormula::InsetKeyPress(XKeyEvent *)
638 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
642 // Special Mathed functions
643 bool InsetFormula::SetNumber(bool numbf)
646 short type = par->GetType();
647 bool oldf = is_numbered(type);
659 UpdatableInset::RESULT
660 InsetFormula::LocalDispatch(BufferView * bv, int action, string const & arg)
662 // extern char *dispatch_result;
663 MathedTextCodes varcode = LM_TC_MIN;
664 bool was_macro = mathcursor->InMacroMode();
666 bool space_on = false;
667 bool was_selection = mathcursor->Selection();
668 RESULT result = DISPATCHED;
669 static MathSpaceInset * sp= 0;
673 if (mathcursor->getLastCode() == LM_TC_TEX) {
678 // --- Cursor Movements ---------------------------------------------
680 sel = true; // fall through...
683 result = DISPATCH_RESULT(mathcursor->Right(sel));
684 if (!sel && (result == DISPATCHED))
685 result = DISPATCHED_NOUPDATE;
690 sel = true; // fall through
693 result = DISPATCH_RESULT(mathcursor->Left(sel));
694 if (!sel && (result == DISPATCHED))
695 result = DISPATCHED_NOUPDATE;
703 result = DISPATCH_RESULT(mathcursor->Up(sel));
704 if (!sel && (result == DISPATCHED))
705 result = DISPATCHED_NOUPDATE;
713 result = DISPATCH_RESULT(mathcursor->Down(sel));
714 if (!sel && (result == DISPATCHED))
715 result = DISPATCHED_NOUPDATE;
721 result = DISPATCHED_NOUPDATE;
726 result = DISPATCHED_NOUPDATE;
729 case LFUN_DELETE_LINE_FORWARD:
730 bv->lockedInsetStoreUndo(Undo::DELETE);
731 mathcursor->DelLine();
737 bv->lockedInsetStoreUndo(Undo::INSERT);
738 byte c = arg.empty() ? '1' : arg[0];
739 mathcursor->Insert(c, LM_TC_CR);
740 if (!label.empty()) {
741 mathcursor->setLabel(label);
744 par = mathcursor->GetPar();
750 bv->lockedInsetStoreUndo(Undo::INSERT);
751 mathcursor->Insert(0, LM_TC_TAB);
756 bv->lockedInsetStoreUndo(Undo::INSERT);
757 mathcursor->Insert('T', LM_TC_TAB);
762 if (!mathcursor->Left())
765 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
766 bv->updateInset(this, true);
772 bv->lockedInsetStoreUndo(Undo::DELETE);
773 mathcursor->Delete();
774 bv->updateInset(this, true);
778 // sprintf(dispatch_buffer, "%d %d",);
779 // dispatch_result = dispatch_buffer;
787 istringstream ist(arg.c_str());
790 mathcursor->SetPos(x1 + x, y1 + y);
794 /* cursor selection ---------------------------- */
798 mathcursor->MacroModeClose();
799 bv->lockedInsetStoreUndo(Undo::INSERT);
800 mathcursor->SelPaste();
805 bv->lockedInsetStoreUndo(Undo::DELETE);
806 mathcursor->SelCut();
811 mathcursor->SelCopy();
816 case LFUN_WORDRIGHTSEL:
817 case LFUN_WORDLEFTSEL:
820 // --- accented characters ------------------------------
822 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
823 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
824 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
825 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
826 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
827 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
828 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
829 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
830 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
831 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
835 if (!greek_kb_flag) {
837 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
843 case LFUN_GREEK_TOGGLE:
844 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
846 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
848 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
852 case LFUN_BOLD: mathcursor->toggleLastCode(LM_TC_BF); break;
853 case LFUN_SANS: mathcursor->toggleLastCode(LM_TC_SF); break;
854 case LFUN_EMPH: mathcursor->toggleLastCode(LM_TC_CAL); break;
855 case LFUN_ROMAN: mathcursor->toggleLastCode(LM_TC_RM); break;
856 case LFUN_CODE: mathcursor->toggleLastCode(LM_TC_TT); break;
857 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR); break;
860 // varcode = LM_TC_TEX;
861 mathcursor->setLastCode(LM_TC_TEX);
862 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
865 case LFUN_MATH_NUMBER:
866 bv->lockedInsetStoreUndo(Undo::INSERT);
868 short type = par->GetType();
869 if (is_numbered(type)) {
871 if (!label.empty()) {
874 bv->owner()->getMiniBuffer()->Set(_("No number"));
877 bv->owner()->getMiniBuffer()->Set(_("Number"));
884 case LFUN_MATH_NONUMBER:
885 if (is_multi_numbered(par->GetType())) {
886 // MathMatrixInset *mt = (MathMatrixInset*)par;
888 // mt->SetNumbered(!mt->IsNumbered());
890 #warning This is a terrible hack! We should find a better solution.
891 while (mathcursor->getLabel() == MathedXIter::error_label) {
892 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
895 mathcursor->setNumbered();
900 case LFUN_MATH_LIMITS:
901 bv->lockedInsetStoreUndo(Undo::INSERT);
902 if (mathcursor->Limits())
908 latexkeys * l = in_word_set(arg);
909 int sz = (l) ? l->id: -1;
910 mathcursor->SetSize(sz);
914 // possible fall through?
916 case LFUN_INSERT_MATH:
917 bv->lockedInsetStoreUndo(Undo::INSERT);
918 InsertSymbol(bv, arg);
922 case LFUN_INSERT_MATRIX:
924 bv->lockedInsetStoreUndo(Undo::INSERT);
926 char s[80], arg2[80];
927 // This is just so that too long args won't ooze out of s.
928 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
929 k = sscanf(arg2, "%d %d %s", &m, &n, s);
938 MathMatrixInset * p = new MathMatrixInset(m, n);
939 if (mathcursor && p) {
940 if (k > 2 && int(strlen(s)) > m)
941 p->SetAlign(s[0], &s[1]);
942 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
948 case LFUN_MATH_DELIM:
950 bv->lockedInsetStoreUndo(Undo::INSERT);
951 char lf[40], rg[40], arg2[40];
952 int ilf = '(', irg = '.';
954 string vdelim("(){}[]./|");
958 ::strncpy(arg2, arg.c_str(), 40);
960 int n = sscanf(arg2, "%s %s", lf, rg);
969 l = in_word_set(lf, strlen(lf));
970 // Long words will cause l == 0; so check.
973 } else if (vdelim.find(lf[0]) != string::npos)
981 l = in_word_set(rg, strlen(rg));
984 } else if (vdelim.find(rg[0]) != string::npos)
989 MathDelimInset * p = new MathDelimInset(ilf, irg);
990 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
995 case LFUN_PROTECTEDSPACE:
996 bv->lockedInsetStoreUndo(Undo::INSERT);
997 sp = new MathSpaceInset(1);
998 mathcursor->Insert(sp);
1003 case LFUN_INSERT_LABEL:
1005 bv->lockedInsetStoreUndo(Undo::INSERT);
1006 if (par->GetType() < LM_OT_PAR)
1009 string old_label = is_multiline(par->GetType())
1010 ? mathcursor->getLabel() : label;
1012 #warning This is a terrible hack! We should find a better solution.
1013 /// This is needed because in some positions mathcursor->cursor->crow
1014 /// is equal to 0, and therefore the label cannot be inserted.
1015 /// So we move the cursor left until mathcursor->cursor->crow != 0.
1016 while (old_label == MathedXIter::error_label) {
1017 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
1019 old_label = mathcursor->getLabel();
1022 string new_label = arg;
1023 if (new_label.empty()) {
1024 string default_label = (lyxrc.label_init_length >= 0) ? "eq:" : "";
1025 pair<bool, string> res = old_label.empty()
1026 ? askForText(_("Enter new label to insert:"), default_label)
1027 : askForText(_("Enter label:"), old_label);
1030 new_label = frontStrip(strip(res.second));
1033 if (new_label == old_label)
1034 break; // Nothing to do
1036 if (!new_label.empty())
1039 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
1042 if (is_multi_numbered(par->GetType()))
1043 mathcursor->setLabel(new_label);
1044 // MathMatrixInset *mt = (MathMatrixInset*)par;
1045 // mt->SetLabel(new_label);
1053 case LFUN_MATH_DISPLAY:
1054 bv->lockedInsetStoreUndo(Undo::EDIT);
1055 display(!disp_flag);
1059 // Invalid actions under math mode
1060 case LFUN_MATH_MODE:
1061 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1062 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1063 varcode = LM_TC_TEXTRM;
1065 varcode = LM_TC_VAR;
1067 mathcursor->setLastCode(varcode);
1071 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1074 //------- dummy actions
1075 case LFUN_EXEC_COMMAND:
1076 bv->owner()->getMiniBuffer()->PrepareForCommand();
1080 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1081 unsigned char c = arg[0];
1082 bv->lockedInsetStoreUndo(Undo::INSERT);
1084 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1086 mathcursor->setAccent(0);
1089 if (c == 0) { // Dead key, do nothing
1090 //lyxerr << "deadkey" << endl;
1095 if (mathcursor->getLastCode() == LM_TC_TEX) {
1096 mathcursor->MacroModeOpen();
1097 mathcursor->clearLastCode();
1098 varcode = LM_TC_MIN;
1102 short f = (mathcursor->getLastCode()) ?
1103 mathcursor->getLastCode() :
1104 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1105 varcode = MathIsAlphaFont(f) ?
1106 static_cast<MathedTextCodes>(f) :
1110 // lyxerr << "Varcode << vardoce;
1111 MathedTextCodes char_code = varcode;
1112 if (greek_kb_flag) {
1114 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1115 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1116 'Y', 0, 0, 0, 0 , 'Z' };
1118 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1119 char_code = LM_TC_RM;
1122 char_code = LM_TC_SYMB;
1125 mathcursor->Insert(c, char_code);
1127 if (greek_kb_flag && char_code == LM_TC_RM )
1128 mathcursor->setLastCode(LM_TC_VAR);
1130 varcode = LM_TC_MIN;
1132 if (greek_kb_flag<2)
1137 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1138 mathcursor->Insert(c, LM_TC_TEX);
1140 mathcursor->Insert('}', LM_TC_TEX);
1143 mathcursor->clearLastCode();
1144 // varcode = LM_TC_MIN;
1147 if (c == '_' && varcode == LM_TC_TEX) {
1148 mathcursor->Insert(c, LM_TC_SPECIAL);
1149 mathcursor->clearLastCode();
1150 // varcode = LM_TC_MIN;
1153 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1154 mathcursor->MacroModeOpen();
1155 mathcursor->clearLastCode();
1156 mathcursor->Insert(c, LM_TC_MIN);
1159 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c)) {
1160 mathcursor->Insert(c, LM_TC_CONST);
1163 if (strchr("+/-*<>=", c)) {
1164 mathcursor->Insert(c, LM_TC_BOP);
1167 if (strchr(latex_special_chars, c) && c!= '_') {
1168 mathcursor->Insert(c, LM_TC_SPECIAL);
1171 if (c == '_' || c == '^') {
1175 mathcursor->Interpret(s);
1180 short f = (mathcursor->getLastCode()) ?
1181 mathcursor->getLastCode() :
1182 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1183 varcode = MathIsAlphaFont(f) ?
1184 static_cast<MathedTextCodes>(f) :
1188 if (varcode == LM_TC_TEXTRM) {
1189 mathcursor->Insert(c, LM_TC_TEXTRM);
1193 mathcursor->MacroModeClose();
1197 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1202 if (!mathcursor->Pop() && mathcursor->IsEnd())
1206 if (c == '\'' || c == '@') {
1207 mathcursor->Insert (c, LM_TC_VAR);
1212 mathcursor->MacroModeClose();
1213 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1214 mathcursor->setLastCode(LM_TC_TEX);
1217 } else if (action == LFUN_MATH_PANEL) {
1218 result = UNDISPATCHED;
1220 // lyxerr << "Closed by action " << action << endl;
1225 if (was_macro != mathcursor->InMacroMode()
1227 && action != LFUN_BACKSPACE)
1230 if (sp && !space_on)
1233 if (mathcursor->Selection() || was_selection)
1234 ToggleInsetSelection(bv);
1236 if ((result == DISPATCHED) || (result == DISPATCHED_NOUPDATE) ||
1237 (result == UNDISPATCHED))
1238 ShowInsetCursor(bv);
1240 bv->unlockInset(this);
1248 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1250 MathedIter it(par->GetData());
1252 while (it.OK() && !(features.binom && features.boldsymbol)) {
1254 if (it.IsActive()) {
1255 MathParInset * p = it.GetActiveInset();
1256 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1257 p->GetName() == "binom") {
1258 features.binom = true;
1260 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1261 p->setArgumentIdx(i);
1262 mathedValidate(features, p);
1266 MathedInset * p = it.GetInset();
1267 if (!features.boldsymbol && p->GetName() == "boldsymbol") {
1268 features.boldsymbol = true;