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.
22 #pragma implementation "formula.h"
26 #include "commandtags.h"
27 #include "math_cursor.h"
28 #include "math_parser.h"
31 #include "minibuffer.h"
32 #include "BufferView.h"
35 #include "LaTeXFeatures.h"
37 #include "lyx_gui_misc.h"
38 #include "support/LOstream.h"
42 extern void UpdateInset(BufferView *, Inset * inset, bool mark_dirty = true);
44 extern char * mathed_label;
46 extern char const * latex_special_chars;
48 short greek_kb_flag = 0;
50 LyXFont * Math_Fonts = 0; // this is only used by Whichfont and mathed_init_fonts (Lgb)
52 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
55 static int sel_x, sel_y;
57 MathedCursor * InsetFormula::mathcursor = 0;
60 int MathedInset::df_asc;
61 int MathedInset::df_des;
62 int MathedInset::df_width;
65 inline bool IsMacro(short token, int id)
67 return (token != LM_TK_FRAC && token != LM_TK_SQRT &&
68 !((token == LM_TK_SYM || token == LM_TC_BSYM) && id < 255));
73 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
76 LyXFont WhichFont(short type, int size)
106 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
116 f.setSize(lfont_size);
120 if (type == LM_TC_BSYM) {
130 case LM_ST_SCRIPTSCRIPT:
135 lyxerr << "Mathed Error: wrong font size: " << size << endl;
139 if (type != LM_TC_TEXTRM)
140 f.setColor(LColor::math);
145 void mathed_init_fonts() //removed 'static' because DEC cxx does not
147 // Probably because this func is declared as a friend in math_defs.h
151 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
153 for (int i = 0 ; i < 8 ; ++i){
154 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
156 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
158 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
160 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
161 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
163 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
165 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
166 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
168 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
170 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
172 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
174 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
175 MathedInset::df_asc = f.maxAscent();
176 MathedInset::df_des = f.maxDescent();
177 MathedInset::df_width = f.width('I');
181 LyXFont mathed_get_font(short type, int size)
183 LyXFont f = WhichFont(type, size);
184 if (type == LM_TC_TEX) {
185 f.setLatex(LyXFont::ON);
191 int mathed_string_width(short type, int size, byte const * s, int ls)
193 LyXFont f = WhichFont(type, size);
196 if (MathIsBinary(type)) {
198 for (int i = 0; i < ls && i < 75; ++i) {
207 return f.textWidth(reinterpret_cast<char const *>(s), ls);
211 int mathed_char_width(short type, int size, byte c)
213 int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1):
214 WhichFont(type, size).width(c);
219 int mathed_string_height(short type, int size, byte const * s,
220 int ls, int & asc, int & des)
222 LyXFont font = WhichFont(type, size);
224 for (int i = 0; i < ls; ++i) {
225 if (font.descent(s[i]) > des)
226 des = font.descent(s[i]);
227 if (font.ascent(s[i]) > asc)
228 asc = font.ascent(s[i]);
234 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
236 LyXFont font = WhichFont(type, size);
238 des = font.descent(c);
239 asc = font.ascent(c);
244 // In a near future maybe we use a better fonts renderer
245 void MathedInset::drawStr(Painter & pain, short type, int size,
246 int x, int y, byte * s, int ls)
249 if (MathIsBinary(type)) {
250 for (int i = 0; i < ls; ++i) {
251 st += string(" ") + char(s[i]) + ' ';
254 st = string(reinterpret_cast<char*>(s), ls);
256 LyXFont mf = mathed_get_font(type, size);
257 pain.text(x, y, st, mf);
261 InsetFormula::InsetFormula(bool display)
263 par = new MathParInset; // this leaks
268 par->SetType(LM_OT_PAR);
269 par->SetStyle(LM_ST_DISPLAY);
274 InsetFormula::InsetFormula(MathParInset * p)
276 par = (p->GetType()>= LM_OT_MPAR) ?
277 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
281 disp_flag = (par->GetType()>0);
286 InsetFormula::~InsetFormula()
292 Inset * InsetFormula::Clone() const
294 InsetFormula * f = new InsetFormula(par);
300 void InsetFormula::Write(ostream & os) const
307 int InsetFormula::Latex(ostream & os, signed char fragile) const
310 //#warning Alejandro, the number of lines is not returned in this case
311 // This problem will disapear at 0.13.
313 InsetFormula::Latex(output, fragile);
319 int InsetFormula::Latex(string & file, signed char fragile) const
322 //#warning Alejandro, the number of lines is not returned in this case
323 // This problem will disapear at 0.13.
327 mathed_write(par, file, &ret, fragile, label.c_str());
332 int InsetFormula::Linuxdoc(string &/*file*/) const
338 int InsetFormula::DocBook(string &/*file*/) const
344 // Check if uses AMS macros
345 void InsetFormula::Validate(LaTeXFeatures & features) const
347 // Validation only necesary if not using an AMS Style
348 if (!features.amsstyle)
349 mathedValidate(features, par);
353 void InsetFormula::Read(LyXLex & lex)
355 istream & is = lex.getStream();
357 mathed_parser_file(is, lex.GetLineNo());
359 // Silly hack to read labels.
361 mathed_parse(0, 0, &par);
363 disp_flag = (par->GetType() > 0);
365 // Update line number
366 lex.setLineNo(mathed_parser_lineno());
369 label = mathed_label;
379 int InsetFormula::ascent(Painter &, LyXFont const &) const
381 return par->Ascent() + ((disp_flag) ? 8 : 1);
385 int InsetFormula::descent(Painter &, LyXFont const &) const
387 return par->Descent() + ((disp_flag) ? 8 : 1);
391 int InsetFormula::width(Painter &, LyXFont const & f) const
393 lfont_size = f.size();
395 return par->Width(); //+2;
399 void InsetFormula::draw(Painter & pain, LyXFont const &,
400 int baseline, float & x) const
402 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
404 lfont_size = font.size();
405 /// Let's try to wait a bit with this... (Lgb)
406 //UpdatableInset::draw(pain, font, baseline, x);
408 // otherwise a segfault could occur
409 // in some XDrawRectangles (i.e. matrix) (Matthias)
410 if (mathcursor && mathcursor->GetPar() == par) {
411 if (mathcursor->Selection()) {
415 mathcursor->SelGetArea(&xp, &yp, n);
416 pain.fillPolygon(xp, yp, n, LColor::selection);
418 mathcursor->draw(pain, int(x), baseline);
420 par->draw(pain, int(x), baseline);
422 x += float(width(pain, font));
424 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
425 LyXFont font = WhichFont(LM_TC_BF, par->size);
426 font.setLatex(LyXFont::OFF);
428 if (par->GetType() == LM_OT_PARN) {
431 str = string("(") + label + ")";
434 pain.text(int(x + 20), baseline, str, font);
435 } else if (par->GetType() == LM_OT_MPARN) {
436 MathMatrixInset * mt =
437 static_cast<MathMatrixInset*>(par);
439 MathedRowSt const * crow = mt->getRowSt();
441 y = baseline + crow->getBaseline();
442 if (crow->isNumbered()) {
444 if (crow->getLabel())
445 str = string("(") + crow->getLabel() + ")";
448 pain.text(int(x + 20), y, str, font);
450 crow = crow->getNext();
454 cursor_visible = false;
458 void InsetFormula::Edit(BufferView * bv, int x, int y)
460 mathcursor = new MathedCursor(par);
463 UpdateInset(bv, this, false);
466 mathcursor->SetPos(x, y);
472 void InsetFormula::InsetUnlock(BufferView * bv)
475 if (mathcursor->InMacroMode()) {
476 mathcursor->MacroModeClose();
482 UpdateInset(bv, this, false);
486 // Now a symbol can be inserted only if the inset is locked
487 void InsetFormula::InsertSymbol(BufferView * bv, char const * s)
489 if (!s || !mathcursor) return;
490 mathcursor->Interpret(s);
495 void InsetFormula::GetCursorPos(int& x, int& y) const
497 mathcursor->GetPos(x, y);
502 void InsetFormula::ToggleInsetCursor(BufferView * bv)
508 mathcursor->GetPos(x, y);
511 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
512 int asc = font.maxAscent();
513 int desc = font.maxDescent();
516 bv->hideLockedInsetCursor();
518 bv->showLockedInsetCursor(x, y, asc, desc);
519 cursor_visible = !cursor_visible;
523 void InsetFormula::ShowInsetCursor(BufferView * bv)
525 if (!cursor_visible) {
528 mathcursor->GetPos(x, y);
531 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
532 int asc = font.maxAscent();
533 int desc = font.maxDescent();
534 bv->fitLockedInsetCursor(x, y, asc, desc);
536 ToggleInsetCursor(bv);
541 void InsetFormula::HideInsetCursor(BufferView * bv)
544 ToggleInsetCursor(bv);
548 void InsetFormula::ToggleInsetSelection(BufferView * bv)
556 //mathcursor->SelGetArea(n);
557 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
561 UpdateInset(bv, this, false);
566 void InsetFormula::display(bool dspf)
568 if (dspf != disp_flag) {
570 par->SetType(LM_OT_PAR);
571 par->SetStyle(LM_ST_DISPLAY);
573 if (par->GetType() >= LM_OT_MPAR) {
574 MathParInset * p = new MathParInset(par);
578 mathcursor->SetPar(par);
580 par->SetType(LM_OT_MIN);
581 par->SetStyle(LM_ST_TEXT);
582 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
591 int InsetFormula::GetNumberOfLabels() const
593 // This is dirty, I know. I'll clean it at 0.13
594 if (par->GetType() == LM_OT_MPARN) {
595 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
597 MathedRowSt const * crow = mt->getRowSt();
599 if (crow->getLabel()) ++nl;
600 crow = crow->getNext();
611 string InsetFormula::getLabel(int il) const
613 //#warning This is dirty, I know. Ill clean it at 0.11
614 // Correction, the only way to clean this is with a new kernel: 0.13.
615 if (par->GetType() == LM_OT_MPARN) {
617 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
619 MathedRowSt const * crow = mt->getRowSt();
621 if (crow->getLabel()) {
623 lab = crow->getLabel();
628 crow = crow->getNext();
636 void InsetFormula::UpdateLocal(BufferView * bv)
638 par->Metrics(); // To inform lyx kernel the exact size
639 // (there were problems with arrays).
640 UpdateInset(bv, this);
644 void InsetFormula::InsetButtonRelease(BufferView * bv,
645 int x, int y, int /*button*/)
650 mathcursor->SetPos(x, y);
655 UpdateInset(bv, this, false);
660 void InsetFormula::InsetButtonPress(BufferView * bv,
661 int x, int y, int /*button*/)
664 sel_x = x; sel_y = y;
665 if (mathcursor->Selection()) {
666 mathcursor->SelClear();
667 UpdateInset(bv, this, false);
672 void InsetFormula::InsetMotionNotify(BufferView * bv,
673 int x, int y, int /*button*/)
675 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
678 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
679 mathcursor->SelStart();
681 mathcursor->GetPos(sel_x, sel_y);
687 mathcursor->SetPos(x, y);
689 mathcursor->GetPos(x, y);
690 if (sel_x!= x || sel_y!= y)
691 UpdateInset(bv, this, false);
692 sel_x = x; sel_y = y;
697 void InsetFormula::InsetKeyPress(XKeyEvent *)
699 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
703 // Special Mathed functions
704 bool InsetFormula::SetNumber(bool numbf)
707 short type = par->GetType();
708 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
709 if (numbf && !oldf) ++type;
710 if (!numbf && oldf) --type;
718 bool InsetFormula::LocalDispatch(BufferView * bv, int action, char const * arg)
720 // extern char *dispatch_result;
721 MathedTextCodes varcode = LM_TC_MIN;
722 bool was_macro = mathcursor->InMacroMode();
724 bool space_on = false;
725 bool was_selection = mathcursor->Selection();
727 static MathSpaceInset * sp= 0;
731 if (mathcursor->getLastCode() == LM_TC_TEX) {
736 // --- Cursor Movements ---------------------------------------------
737 case LFUN_RIGHTSEL: sel = true;
740 result = mathcursor->Right(sel);
743 case LFUN_LEFTSEL: sel = true;
746 result = mathcursor->Left(sel);
749 case LFUN_UPSEL: sel = true;
751 result = mathcursor->Up(sel);
754 case LFUN_DOWNSEL: sel = true;
756 result = mathcursor->Down(sel);
764 case LFUN_DELETE_LINE_FORWARD:
765 //current_view->lockedInsetStoreUndo(Undo::INSERT);
766 bv->lockedInsetStoreUndo(Undo::DELETE);
767 mathcursor->DelLine();
771 bv->lockedInsetStoreUndo(Undo::INSERT);
772 mathcursor->Insert(' ', LM_TC_CR);
773 par = mathcursor->GetPar();
777 bv->lockedInsetStoreUndo(Undo::INSERT);
778 mathcursor->Insert(0, LM_TC_TAB);
782 bv->lockedInsetStoreUndo(Undo::INSERT);
783 mathcursor->Insert('T', LM_TC_TAB);
787 if (!mathcursor->Left())
790 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
791 UpdateInset(bv, this);
796 //current_view->lockedInsetStoreUndo(Undo::INSERT);
797 bv->lockedInsetStoreUndo(Undo::DELETE);
798 mathcursor->Delete();
799 UpdateInset(bv, this);
802 // sprintf(dispatch_buffer, "%d %d",);
803 // dispatch_result = dispatch_buffer;
808 sscanf(arg, "%d %d", &x, &y);
810 mathcursor->SetPos(x1+x, y1+y);
814 /* cursor selection ---------------------------- */
818 mathcursor->MacroModeClose();
819 bv->lockedInsetStoreUndo(Undo::INSERT);
820 mathcursor->SelPaste(); UpdateLocal(bv); break;
822 bv->lockedInsetStoreUndo(Undo::DELETE);
823 mathcursor->SelCut(); UpdateLocal(bv); break;
824 case LFUN_COPY: mathcursor->SelCopy(); break;
827 case LFUN_WORDRIGHTSEL:
828 case LFUN_WORDLEFTSEL:
831 // --- accented characters ------------------------------
833 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
834 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
835 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
836 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
837 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
838 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
839 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
840 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
841 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
842 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
847 if (!greek_kb_flag) {
849 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
856 case LFUN_GREEK_TOGGLE:
858 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
860 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
862 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
867 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
868 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
869 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
870 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
871 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
872 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
875 // varcode = LM_TC_TEX;
876 mathcursor->setLastCode(LM_TC_TEX);
877 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
881 case LFUN_MATH_NUMBER:
883 bv->lockedInsetStoreUndo(Undo::INSERT);
885 short type = par->GetType();
886 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
889 if (!label.empty()) {
892 bv->owner()->getMiniBuffer()->Set(_("No number"));
895 bv->owner()->getMiniBuffer()->Set(_("Number"));
903 case LFUN_MATH_NONUMBER:
905 if (par->GetType() == LM_OT_MPARN) {
906 // MathMatrixInset *mt = (MathMatrixInset*)par;
908 // mt->SetNumbered(!mt->IsNumbered());
910 mathcursor->setNumbered();
916 case LFUN_MATH_LIMITS:
918 bv->lockedInsetStoreUndo(Undo::INSERT);
919 if (mathcursor->Limits())
925 latexkeys * l = in_word_set (arg, strlen(arg));
926 int sz = (l) ? l->id: -1;
927 mathcursor->SetSize(sz);
932 case LFUN_INSERT_MATH:
934 bv->lockedInsetStoreUndo(Undo::INSERT);
935 InsertSymbol(bv, arg);
939 case LFUN_INSERT_MATRIX:
941 bv->lockedInsetStoreUndo(Undo::INSERT);
943 char s[80], arg2[80];
944 // This is just so that too long args won't ooze out of s.
945 strncpy(arg2, arg, 80); arg2[79]= '\0';
946 k = sscanf(arg2, "%d %d %s", &m, &n, s);
955 MathMatrixInset * p = new MathMatrixInset(m, n);
956 if (mathcursor && p) {
957 if (k > 2 && int(strlen(s)) > m)
958 p->SetAlign(s[0], &s[1]);
959 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
965 case LFUN_MATH_DELIM:
967 bv->lockedInsetStoreUndo(Undo::INSERT);
968 char lf[40], rg[40], arg2[40];
969 int ilf = '(', irg = '.';
971 string vdelim("(){}[]./|");
974 strncpy(arg2, arg, 40); arg2[39]= '\0';
975 int n = sscanf(arg2, "%s %s", lf, rg);
976 lf[39] = '\0'; rg[39] = '\0';
983 l = in_word_set(lf, strlen(lf));
984 // Long words will cause l == 0; so check.
987 if (vdelim.find(lf[0]) != string::npos)
995 l = in_word_set(rg, strlen(rg));
998 if (vdelim.find(rg[0]) != string::npos)
1003 MathDelimInset * p = new MathDelimInset(ilf, irg);
1004 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1009 case LFUN_PROTECTEDSPACE:
1011 bv->lockedInsetStoreUndo(Undo::INSERT);
1012 sp = new MathSpaceInset(1);
1013 mathcursor->Insert(sp);
1019 case LFUN_INSERT_LABEL:
1021 bv->lockedInsetStoreUndo(Undo::INSERT);
1022 if (par->GetType() < LM_OT_PAR) break;
1026 result = askForText(_("Enter new label to insert:"));
1031 if (!lb.empty() && lb[0] > ' ') {
1033 if (par->GetType() == LM_OT_MPARN) {
1034 mathcursor->setLabel(lb.c_str());
1035 // MathMatrixInset *mt = (MathMatrixInset*)par;
1036 // mt->SetLabel(lb);
1038 //if (label.notEmpty()) delete label;
1047 case LFUN_MATH_DISPLAY:
1048 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1049 bv->lockedInsetStoreUndo(Undo::EDIT);
1050 display(!disp_flag);
1054 // Invalid actions under math mode
1055 case LFUN_MATH_MODE:
1057 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1058 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1059 varcode = LM_TC_TEXTRM;
1061 varcode = LM_TC_VAR;
1063 mathcursor->setLastCode(varcode);
1067 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1070 //------- dummy actions
1071 case LFUN_EXEC_COMMAND:
1072 bv->owner()->getMiniBuffer()->ExecCommand();
1076 if ((action == -1 || action == LFUN_SELFINSERT) && arg) {
1077 unsigned char c = arg[0];
1078 bv->lockedInsetStoreUndo(Undo::INSERT);
1080 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1082 mathcursor->setAccent(0);
1084 if (c == 0) { // Dead key, do nothing
1085 //lyxerr << "deadkey" << endl;
1089 if (mathcursor->getLastCode() == LM_TC_TEX) {
1090 mathcursor->MacroModeOpen();
1091 mathcursor->clearLastCode();
1092 varcode = LM_TC_MIN;
1095 short f = (mathcursor->getLastCode()) ?
1096 mathcursor->getLastCode() :
1097 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1098 varcode = MathIsAlphaFont(f) ?
1099 static_cast<MathedTextCodes>(f) :
1103 // lyxerr << "Varcode << vardoce;
1104 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1105 varcode = LM_TC_MIN;
1106 if (greek_kb_flag<2) greek_kb_flag = 0;
1108 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1109 mathcursor->Insert(c, LM_TC_TEX);
1111 mathcursor->Insert('}', LM_TC_TEX);
1114 mathcursor->clearLastCode();
1115 // varcode = LM_TC_MIN;
1117 if (c == '_' && varcode == LM_TC_TEX) {
1118 mathcursor->Insert(c, LM_TC_SPECIAL);
1119 mathcursor->clearLastCode();
1120 // varcode = LM_TC_MIN;
1122 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1123 mathcursor->MacroModeOpen();
1124 mathcursor->clearLastCode();
1125 mathcursor->Insert(c, LM_TC_MIN);
1128 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1129 mathcursor->Insert(c, LM_TC_CONST);
1131 if (strchr("+/-*<>=", c))
1132 mathcursor->Insert(c, LM_TC_BOP);
1134 if (strchr(latex_special_chars, c) && c!= '_')
1135 mathcursor->Insert(c, LM_TC_SPECIAL);
1137 if (c == '_' || c == '^') {
1141 mathcursor->Interpret (s);
1145 short f = (mathcursor->getLastCode()) ?
1146 mathcursor->getLastCode() :
1147 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1148 varcode = MathIsAlphaFont(f) ?
1149 static_cast<MathedTextCodes>(f) :
1152 if (varcode == LM_TC_TEXTRM) {
1153 mathcursor->Insert(c, LM_TC_TEXTRM);
1156 mathcursor->MacroModeClose();
1159 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1163 if (!mathcursor->Pop() && mathcursor->IsEnd())
1168 mathcursor->Insert (c, LM_TC_VAR);
1172 mathcursor->MacroModeClose();
1173 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1174 mathcursor->setLastCode(LM_TC_TEX);
1178 // lyxerr << "Closed by action " << action << endl;
1182 if (was_macro != mathcursor->InMacroMode()
1184 && action != LFUN_BACKSPACE)
1186 if (sp && !space_on) sp = 0;
1187 if (mathcursor->Selection() || was_selection)
1188 ToggleInsetSelection(bv);
1191 ShowInsetCursor(bv);
1193 bv->unlockInset(this);
1200 MathFuncInset::draw(Painter & pain, int x, int y)
1202 if (name && name[0] > ' ') {
1203 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1204 font.setLatex(LyXFont::ON);
1205 x += (font.textWidth("I", 1) + 3) / 4;
1206 pain.text(x, y, name, font);
1211 void MathFuncInset::Metrics()
1213 ln = (name) ? strlen(name): 0;
1214 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1215 font.setLatex(LyXFont::ON);
1216 width = font.textWidth(name, ln) + font.textWidth("I", 1)/2;
1217 mathed_string_height(LM_TC_TEXTRM, size,
1218 reinterpret_cast<unsigned char const *>(name),
1219 strlen(name), ascent, descent);
1224 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1226 MathedIter it(par->GetData());
1228 while (it.OK() && !(features.binom && features.boldsymbol)) {
1231 MathParInset * p = it.GetActiveInset();
1232 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1233 strcmp(p->GetName(), "binom") == 0) {
1234 features.binom = true;
1236 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1237 p->setArgumentIdx(i);
1238 mathedValidate(features, p);
1242 MathedInset* p = it.GetInset();
1243 if (!features.boldsymbol && p->GetName() &&
1244 strcmp(p->GetName(), "boldsymbol") == 0) {
1245 features.boldsymbol = true;