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: (c) 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.
19 #pragma implementation "formula.h"
23 #include "commandtags.h"
24 #include "math_cursor.h"
25 #include "math_parser.h"
27 #include "minibuffer.h"
28 #include "BufferView.h"
31 #include "LaTeXFeatures.h"
33 #include "lyx_gui_misc.h"
34 #include "support/LOstream.h"
38 extern char * mathed_label;
40 extern char const * latex_special_chars;
42 short greek_kb_flag = 0;
44 LyXFont * Math_Fonts = 0; // this is only used by Whichfont and mathed_init_fonts (Lgb)
46 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
49 static int sel_x, sel_y;
51 MathedCursor * InsetFormula::mathcursor = 0;
54 int MathedInset::df_asc;
55 int MathedInset::df_des;
56 int MathedInset::df_width;
60 bool IsMacro(short token, int id)
62 return (token != LM_TK_FRAC && token != LM_TK_SQRT &&
63 !((token == LM_TK_SYM || token == LM_TC_BSYM) && id < 255));
68 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
71 LyXFont WhichFont(short type, int size)
101 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
111 f.setSize(lfont_size);
115 if (type == LM_TC_BSYM) {
125 case LM_ST_SCRIPTSCRIPT:
130 lyxerr << "Mathed Error: wrong font size: " << size << endl;
134 if (type != LM_TC_TEXTRM)
135 f.setColor(LColor::math);
140 void mathed_init_fonts() //removed 'static' because DEC cxx does not
142 // Probably because this func is declared as a friend in math_defs.h
146 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
148 for (int i = 0 ; i < 8 ; ++i) {
149 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
151 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
153 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
155 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
156 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
158 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
160 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
161 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
163 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
165 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
167 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
169 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
170 MathedInset::df_asc = f.maxAscent();
171 MathedInset::df_des = f.maxDescent();
172 MathedInset::df_width = f.width('I');
176 LyXFont mathed_get_font(short type, int size)
178 LyXFont f = WhichFont(type, size);
179 if (type == LM_TC_TEX) {
180 f.setLatex(LyXFont::ON);
186 int mathed_string_width(short type, int size, byte const * s, int ls)
188 LyXFont f = WhichFont(type, size);
191 if (MathIsBinary(type)) {
193 for (int i = 0; i < ls && i < 75; ++i) {
202 return f.textWidth(reinterpret_cast<char const *>(s), ls);
206 int mathed_char_width(short type, int size, byte c)
208 int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1):
209 WhichFont(type, size).width(c);
214 int mathed_string_height(short type, int size, byte const * s,
215 int ls, int & asc, int & des)
217 LyXFont font = WhichFont(type, size);
219 for (int i = 0; i < ls; ++i) {
220 if (font.descent(s[i]) > des)
221 des = font.descent(s[i]);
222 if (font.ascent(s[i]) > asc)
223 asc = font.ascent(s[i]);
229 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
231 LyXFont font = WhichFont(type, size);
233 des = font.descent(c);
234 asc = font.ascent(c);
239 // In a near future maybe we use a better fonts renderer
240 void MathedInset::drawStr(Painter & pain, short type, int size,
241 int x, int y, byte * s, int ls)
244 if (MathIsBinary(type)) {
245 for (int i = 0; i < ls; ++i) {
246 st += string(" ") + char(s[i]) + ' ';
249 st = string(reinterpret_cast<char*>(s), ls);
251 LyXFont mf = mathed_get_font(type, size);
252 pain.text(x, y, st, mf);
256 InsetFormula::InsetFormula(bool display)
258 par = new MathParInset; // this leaks
263 par->SetType(LM_OT_PAR);
264 par->SetStyle(LM_ST_DISPLAY);
269 InsetFormula::InsetFormula(MathParInset * p)
271 par = (p->GetType()>= LM_OT_MPAR) ?
272 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
276 disp_flag = (par->GetType()>0);
281 InsetFormula::~InsetFormula()
287 Inset * InsetFormula::Clone() const
289 InsetFormula * f = new InsetFormula(par);
295 void InsetFormula::Write(ostream & os) const
302 int InsetFormula::Latex(ostream & os, signed char fragile) const
305 //#warning Alejandro, the number of lines is not returned in this case
306 // This problem will disapear at 0.13.
307 #ifdef USE_OSTREAM_ONLY
311 mathed_write(par, os, &ret, fragile, label.c_str());
315 InsetFormula::Latex(output, fragile);
322 #ifndef USE_OSTREAM_ONLY
323 int InsetFormula::Latex(string & file, signed char fragile) const
326 //#warning Alejandro, the number of lines is not returned in this case
327 // This problem will disapear at 0.13.
331 mathed_write(par, file, &ret, fragile, label.c_str());
336 int InsetFormula::Linuxdoc(string &/*file*/) const
342 int InsetFormula::DocBook(string &/*file*/) const
349 int InsetFormula::Linuxdoc(ostream &) const
355 int InsetFormula::DocBook(ostream&) const
362 // Check if uses AMS macros
363 void InsetFormula::Validate(LaTeXFeatures & features) const
365 // Validation only necesary if not using an AMS Style
366 if (!features.amsstyle)
367 mathedValidate(features, par);
371 void InsetFormula::Read(LyXLex & lex)
373 istream & is = lex.getStream();
375 mathed_parser_file(is, lex.GetLineNo());
377 // Silly hack to read labels.
379 mathed_parse(0, 0, &par);
381 disp_flag = (par->GetType() > 0);
383 // Update line number
384 lex.setLineNo(mathed_parser_lineno());
387 label = mathed_label;
397 int InsetFormula::ascent(Painter &, LyXFont const &) const
399 return par->Ascent() + ((disp_flag) ? 8 : 1);
403 int InsetFormula::descent(Painter &, LyXFont const &) const
405 return par->Descent() + ((disp_flag) ? 8 : 1);
409 int InsetFormula::width(Painter &, LyXFont const & f) const
411 lfont_size = f.size();
413 return par->Width(); //+2;
417 void InsetFormula::draw(Painter & pain, LyXFont const & f,
418 int baseline, float & x) const
420 // Seems commenting out solves a problem.
421 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
422 font.setSize(f.size());
423 lfont_size = font.size();
424 /// Let's try to wait a bit with this... (Lgb)
425 //UpdatableInset::draw(pain, font, baseline, x);
427 // otherwise a segfault could occur
428 // in some XDrawRectangles (i.e. matrix) (Matthias)
429 if (mathcursor && mathcursor->GetPar() == par) {
430 if (mathcursor->Selection()) {
434 mathcursor->SelGetArea(&xp, &yp, n);
435 pain.fillPolygon(xp, yp, n, LColor::selection);
437 mathcursor->draw(pain, int(x), baseline);
439 par->draw(pain, int(x), baseline);
441 x += float(width(pain, font));
443 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
444 LyXFont font = WhichFont(LM_TC_BF, par->size);
445 font.setLatex(LyXFont::OFF);
447 if (par->GetType() == LM_OT_PARN) {
450 str = string("(") + label + ")";
453 pain.text(int(x + 20), baseline, str, font);
454 } else if (par->GetType() == LM_OT_MPARN) {
455 MathMatrixInset * mt =
456 static_cast<MathMatrixInset*>(par);
458 MathedRowSt const * crow = mt->getRowSt();
460 y = baseline + crow->getBaseline();
461 if (crow->isNumbered()) {
463 if (crow->getLabel())
464 str = string("(") + crow->getLabel() + ")";
467 pain.text(int(x + 20), y, str, font);
469 crow = crow->getNext();
473 cursor_visible = false;
477 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
479 mathcursor = new MathedCursor(par);
482 bv->updateInset(this, false);
485 mathcursor->SetPos(x, y);
491 void InsetFormula::InsetUnlock(BufferView * bv)
494 if (mathcursor->InMacroMode()) {
495 mathcursor->MacroModeClose();
501 bv->updateInset(this, false);
505 // Now a symbol can be inserted only if the inset is locked
506 void InsetFormula::InsertSymbol(BufferView * bv, char const * s)
508 if (!s || !mathcursor) return;
509 mathcursor->Interpret(s);
514 void InsetFormula::GetCursorPos(int& x, int& y) const
516 mathcursor->GetPos(x, y);
521 void InsetFormula::ToggleInsetCursor(BufferView * bv)
527 mathcursor->GetPos(x, y);
530 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
531 int asc = font.maxAscent();
532 int desc = font.maxDescent();
535 bv->hideLockedInsetCursor();
537 bv->showLockedInsetCursor(x, y, asc, desc);
538 cursor_visible = !cursor_visible;
542 void InsetFormula::ShowInsetCursor(BufferView * bv)
544 if (!cursor_visible) {
547 mathcursor->GetPos(x, y);
550 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
551 int asc = font.maxAscent();
552 int desc = font.maxDescent();
553 bv->fitLockedInsetCursor(x, y, asc, desc);
555 ToggleInsetCursor(bv);
560 void InsetFormula::HideInsetCursor(BufferView * bv)
563 ToggleInsetCursor(bv);
567 void InsetFormula::ToggleInsetSelection(BufferView * bv)
575 //mathcursor->SelGetArea(n);
576 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
580 bv->updateInset(this, false);
585 void InsetFormula::display(bool dspf)
587 if (dspf != disp_flag) {
589 par->SetType(LM_OT_PAR);
590 par->SetStyle(LM_ST_DISPLAY);
592 if (par->GetType() >= LM_OT_MPAR) {
593 MathParInset * p = new MathParInset(par);
597 mathcursor->SetPar(par);
599 par->SetType(LM_OT_MIN);
600 par->SetStyle(LM_ST_TEXT);
601 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
610 int InsetFormula::GetNumberOfLabels() const
612 // This is dirty, I know. I'll clean it at 0.13
613 if (par->GetType() == LM_OT_MPARN) {
614 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
616 MathedRowSt const * crow = mt->getRowSt();
618 if (crow->getLabel()) ++nl;
619 crow = crow->getNext();
630 string InsetFormula::getLabel(int il) const
632 //#warning This is dirty, I know. Ill clean it at 0.11
633 // Correction, the only way to clean this is with a new kernel: 0.13.
634 if (par->GetType() == LM_OT_MPARN) {
636 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
638 MathedRowSt const * crow = mt->getRowSt();
640 if (crow->getLabel()) {
642 lab = crow->getLabel();
647 crow = crow->getNext();
655 void InsetFormula::UpdateLocal(BufferView * bv)
657 par->Metrics(); // To inform lyx kernel the exact size
658 // (there were problems with arrays).
659 bv->updateInset(this, true);
663 void InsetFormula::InsetButtonRelease(BufferView * bv,
664 int x, int y, int /*button*/)
669 mathcursor->SetPos(x, y);
674 bv->updateInset(this, false);
679 void InsetFormula::InsetButtonPress(BufferView * bv,
680 int x, int y, int /*button*/)
683 sel_x = x; sel_y = y;
684 if (mathcursor->Selection()) {
685 mathcursor->SelClear();
686 bv->updateInset(this, false);
691 void InsetFormula::InsetMotionNotify(BufferView * bv,
692 int x, int y, int /*button*/)
694 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
697 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
698 mathcursor->SelStart();
700 mathcursor->GetPos(sel_x, sel_y);
706 mathcursor->SetPos(x, y);
708 mathcursor->GetPos(x, y);
709 if (sel_x!= x || sel_y!= y)
710 bv->updateInset(this, false);
711 sel_x = x; sel_y = y;
716 void InsetFormula::InsetKeyPress(XKeyEvent *)
718 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
722 // Special Mathed functions
723 bool InsetFormula::SetNumber(bool numbf)
726 short type = par->GetType();
727 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
728 if (numbf && !oldf) ++type;
729 if (!numbf && oldf) --type;
737 UpdatableInset::RESULT
738 InsetFormula::LocalDispatch(BufferView * bv,
739 int action, string const & arg)
741 // extern char *dispatch_result;
742 MathedTextCodes varcode = LM_TC_MIN;
743 bool was_macro = mathcursor->InMacroMode();
745 bool space_on = false;
746 bool was_selection = mathcursor->Selection();
747 RESULT result = DISPATCHED;
748 static MathSpaceInset * sp= 0;
752 if (mathcursor->getLastCode() == LM_TC_TEX) {
757 // --- Cursor Movements ---------------------------------------------
758 case LFUN_RIGHTSEL: sel = true;
761 result = DISPATCH_RESULT(mathcursor->Right(sel));
764 case LFUN_LEFTSEL: sel = true;
767 result = DISPATCH_RESULT(mathcursor->Left(sel));
770 case LFUN_UPSEL: sel = true;
772 result = DISPATCH_RESULT(mathcursor->Up(sel));
775 case LFUN_DOWNSEL: sel = true;
777 result = DISPATCH_RESULT(mathcursor->Down(sel));
785 case LFUN_DELETE_LINE_FORWARD:
786 //current_view->lockedInsetStoreUndo(Undo::INSERT);
787 bv->lockedInsetStoreUndo(Undo::DELETE);
788 mathcursor->DelLine();
792 bv->lockedInsetStoreUndo(Undo::INSERT);
793 mathcursor->Insert(' ', LM_TC_CR);
794 par = mathcursor->GetPar();
798 bv->lockedInsetStoreUndo(Undo::INSERT);
799 mathcursor->Insert(0, LM_TC_TAB);
803 bv->lockedInsetStoreUndo(Undo::INSERT);
804 mathcursor->Insert('T', LM_TC_TAB);
808 if (!mathcursor->Left())
811 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
812 bv->updateInset(this, true);
817 //current_view->lockedInsetStoreUndo(Undo::INSERT);
818 bv->lockedInsetStoreUndo(Undo::DELETE);
819 mathcursor->Delete();
820 bv->updateInset(this, true);
823 // sprintf(dispatch_buffer, "%d %d",);
824 // dispatch_result = dispatch_buffer;
830 istringstream ist(arg.c_str());
832 istrstream ist(arg.c_str());
836 mathcursor->SetPos(x1 + x, y1 + y);
840 /* cursor selection ---------------------------- */
844 mathcursor->MacroModeClose();
845 bv->lockedInsetStoreUndo(Undo::INSERT);
846 mathcursor->SelPaste(); UpdateLocal(bv); break;
848 bv->lockedInsetStoreUndo(Undo::DELETE);
849 mathcursor->SelCut(); UpdateLocal(bv); break;
850 case LFUN_COPY: mathcursor->SelCopy(); break;
853 case LFUN_WORDRIGHTSEL:
854 case LFUN_WORDLEFTSEL:
857 // --- accented characters ------------------------------
859 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
860 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
861 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
862 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
863 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
864 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
865 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
866 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
867 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
868 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
873 if (!greek_kb_flag) {
875 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
882 case LFUN_GREEK_TOGGLE:
884 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
886 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
888 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
893 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
894 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
895 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
896 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
897 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
898 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
901 // varcode = LM_TC_TEX;
902 mathcursor->setLastCode(LM_TC_TEX);
903 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
907 case LFUN_MATH_NUMBER:
909 bv->lockedInsetStoreUndo(Undo::INSERT);
911 short type = par->GetType();
912 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
915 if (!label.empty()) {
918 bv->owner()->getMiniBuffer()->Set(_("No number"));
921 bv->owner()->getMiniBuffer()->Set(_("Number"));
929 case LFUN_MATH_NONUMBER:
931 if (par->GetType() == LM_OT_MPARN) {
932 // MathMatrixInset *mt = (MathMatrixInset*)par;
934 // mt->SetNumbered(!mt->IsNumbered());
936 mathcursor->setNumbered();
942 case LFUN_MATH_LIMITS:
944 bv->lockedInsetStoreUndo(Undo::INSERT);
945 if (mathcursor->Limits())
951 latexkeys * l = in_word_set (arg.c_str(), strlen(arg.c_str()));
952 int sz = (l) ? l->id: -1;
953 mathcursor->SetSize(sz);
958 case LFUN_INSERT_MATH:
960 bv->lockedInsetStoreUndo(Undo::INSERT);
961 InsertSymbol(bv, arg.c_str());
965 case LFUN_INSERT_MATRIX:
967 bv->lockedInsetStoreUndo(Undo::INSERT);
969 char s[80], arg2[80];
970 // This is just so that too long args won't ooze out of s.
971 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
972 k = sscanf(arg2, "%d %d %s", &m, &n, s);
981 MathMatrixInset * p = new MathMatrixInset(m, n);
982 if (mathcursor && p) {
983 if (k > 2 && int(strlen(s)) > m)
984 p->SetAlign(s[0], &s[1]);
985 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
991 case LFUN_MATH_DELIM:
993 bv->lockedInsetStoreUndo(Undo::INSERT);
994 char lf[40], rg[40], arg2[40];
995 int ilf = '(', irg = '.';
997 string vdelim("(){}[]./|");
1001 strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
1002 int n = sscanf(arg2, "%s %s", lf, rg);
1003 lf[39] = '\0'; rg[39] = '\0';
1010 l = in_word_set(lf, strlen(lf));
1011 // Long words will cause l == 0; so check.
1014 if (vdelim.find(lf[0]) != string::npos)
1022 l = in_word_set(rg, strlen(rg));
1025 if (vdelim.find(rg[0]) != string::npos)
1030 MathDelimInset * p = new MathDelimInset(ilf, irg);
1031 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1036 case LFUN_PROTECTEDSPACE:
1038 bv->lockedInsetStoreUndo(Undo::INSERT);
1039 sp = new MathSpaceInset(1);
1040 mathcursor->Insert(sp);
1046 case LFUN_INSERT_LABEL:
1048 bv->lockedInsetStoreUndo(Undo::INSERT);
1049 if (par->GetType() < LM_OT_PAR) break;
1053 result = askForText(_("Enter new label to insert:"));
1058 if (!lb.empty() && lb[0] > ' ') {
1060 if (par->GetType() == LM_OT_MPARN) {
1061 mathcursor->setLabel(lb.c_str());
1062 // MathMatrixInset *mt = (MathMatrixInset*)par;
1063 // mt->SetLabel(lb);
1065 //if (label.notEmpty()) delete label;
1074 case LFUN_MATH_DISPLAY:
1075 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1076 bv->lockedInsetStoreUndo(Undo::EDIT);
1077 display(!disp_flag);
1081 // Invalid actions under math mode
1082 case LFUN_MATH_MODE:
1084 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1085 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1086 varcode = LM_TC_TEXTRM;
1088 varcode = LM_TC_VAR;
1090 mathcursor->setLastCode(varcode);
1094 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1097 //------- dummy actions
1098 case LFUN_EXEC_COMMAND:
1099 bv->owner()->getMiniBuffer()->ExecCommand();
1103 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1104 unsigned char c = arg[0];
1105 bv->lockedInsetStoreUndo(Undo::INSERT);
1107 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1109 mathcursor->setAccent(0);
1111 if (c == 0) { // Dead key, do nothing
1112 //lyxerr << "deadkey" << endl;
1116 if (mathcursor->getLastCode() == LM_TC_TEX) {
1117 mathcursor->MacroModeOpen();
1118 mathcursor->clearLastCode();
1119 varcode = LM_TC_MIN;
1122 short f = (mathcursor->getLastCode()) ?
1123 mathcursor->getLastCode() :
1124 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1125 varcode = MathIsAlphaFont(f) ?
1126 static_cast<MathedTextCodes>(f) :
1130 // lyxerr << "Varcode << vardoce;
1131 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1132 varcode = LM_TC_MIN;
1133 if (greek_kb_flag<2) greek_kb_flag = 0;
1135 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1136 mathcursor->Insert(c, LM_TC_TEX);
1138 mathcursor->Insert('}', LM_TC_TEX);
1141 mathcursor->clearLastCode();
1142 // varcode = LM_TC_MIN;
1144 if (c == '_' && varcode == LM_TC_TEX) {
1145 mathcursor->Insert(c, LM_TC_SPECIAL);
1146 mathcursor->clearLastCode();
1147 // varcode = LM_TC_MIN;
1149 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1150 mathcursor->MacroModeOpen();
1151 mathcursor->clearLastCode();
1152 mathcursor->Insert(c, LM_TC_MIN);
1155 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1156 mathcursor->Insert(c, LM_TC_CONST);
1158 if (strchr("+/-*<>=", c))
1159 mathcursor->Insert(c, LM_TC_BOP);
1161 if (strchr(latex_special_chars, c) && c!= '_')
1162 mathcursor->Insert(c, LM_TC_SPECIAL);
1164 if (c == '_' || c == '^') {
1168 mathcursor->Interpret (s);
1172 short f = (mathcursor->getLastCode()) ?
1173 mathcursor->getLastCode() :
1174 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1175 varcode = MathIsAlphaFont(f) ?
1176 static_cast<MathedTextCodes>(f) :
1179 if (varcode == LM_TC_TEXTRM) {
1180 mathcursor->Insert(c, LM_TC_TEXTRM);
1183 mathcursor->MacroModeClose();
1186 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1190 if (!mathcursor->Pop() && mathcursor->IsEnd())
1195 mathcursor->Insert (c, LM_TC_VAR);
1199 mathcursor->MacroModeClose();
1200 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1201 mathcursor->setLastCode(LM_TC_TEX);
1205 // lyxerr << "Closed by action " << action << endl;
1209 if (was_macro != mathcursor->InMacroMode()
1211 && action != LFUN_BACKSPACE)
1213 if (sp && !space_on) sp = 0;
1214 if (mathcursor->Selection() || was_selection)
1215 ToggleInsetSelection(bv);
1217 if (result == DISPATCHED)
1218 ShowInsetCursor(bv);
1220 bv->unlockInset(this);
1227 MathFuncInset::draw(Painter & pain, int x, int y)
1229 if (name && name[0] > ' ') {
1230 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1231 font.setLatex(LyXFont::ON);
1232 x += (font.textWidth("I", 1) + 3) / 4;
1233 pain.text(x, y, name, font);
1238 void MathFuncInset::Metrics()
1240 ln = (name) ? strlen(name): 0;
1241 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1242 font.setLatex(LyXFont::ON);
1243 width = font.textWidth(name, ln) + font.textWidth("I", 1)/2;
1244 mathed_string_height(LM_TC_TEXTRM, size,
1245 reinterpret_cast<unsigned char const *>(name),
1246 strlen(name), ascent, descent);
1251 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1253 MathedIter it(par->GetData());
1255 while (it.OK() && !(features.binom && features.boldsymbol)) {
1258 MathParInset * p = it.GetActiveInset();
1259 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1260 strcmp(p->GetName(), "binom") == 0) {
1261 features.binom = true;
1263 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1264 p->setArgumentIdx(i);
1265 mathedValidate(features, p);
1269 MathedInset* p = it.GetInset();
1270 if (!features.boldsymbol && p->GetName() &&
1271 strcmp(p->GetName(), "boldsymbol") == 0) {
1272 features.boldsymbol = true;