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"
30 #include "minibuffer.h"
31 #include "BufferView.h"
34 #include "LaTeXFeatures.h"
36 #include "lyx_gui_misc.h"
37 #include "support/LOstream.h"
41 extern char * mathed_label;
43 extern char const * latex_special_chars;
45 short greek_kb_flag = 0;
47 LyXFont * Math_Fonts = 0; // this is only used by Whichfont and mathed_init_fonts (Lgb)
49 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
52 static int sel_x, sel_y;
54 MathedCursor * InsetFormula::mathcursor = 0;
57 int MathedInset::df_asc;
58 int MathedInset::df_des;
59 int MathedInset::df_width;
62 inline bool IsMacro(short token, int id)
64 return (token != LM_TK_FRAC && token != LM_TK_SQRT &&
65 !((token == LM_TK_SYM || token == LM_TC_BSYM) && id < 255));
70 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
73 LyXFont WhichFont(short type, int size)
103 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
113 f.setSize(lfont_size);
117 if (type == LM_TC_BSYM) {
127 case LM_ST_SCRIPTSCRIPT:
132 lyxerr << "Mathed Error: wrong font size: " << size << endl;
136 if (type != LM_TC_TEXTRM)
137 f.setColor(LColor::math);
142 void mathed_init_fonts() //removed 'static' because DEC cxx does not
144 // Probably because this func is declared as a friend in math_defs.h
148 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
150 for (int i = 0 ; i < 8 ; ++i){
151 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
153 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
155 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
157 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
158 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
160 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
162 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
163 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
165 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
167 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
169 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
171 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
172 MathedInset::df_asc = f.maxAscent();
173 MathedInset::df_des = f.maxDescent();
174 MathedInset::df_width = f.width('I');
178 LyXFont mathed_get_font(short type, int size)
180 LyXFont f = WhichFont(type, size);
181 if (type == LM_TC_TEX) {
182 f.setLatex(LyXFont::ON);
188 int mathed_string_width(short type, int size, byte const * s, int ls)
190 LyXFont f = WhichFont(type, size);
193 if (MathIsBinary(type)) {
195 for (int i = 0; i < ls && i < 75; ++i) {
204 return f.textWidth(reinterpret_cast<char const *>(s), ls);
208 int mathed_char_width(short type, int size, byte c)
210 int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1):
211 WhichFont(type, size).width(c);
216 int mathed_string_height(short type, int size, byte const * s,
217 int ls, int & asc, int & des)
219 LyXFont font = WhichFont(type, size);
221 for (int i = 0; i < ls; ++i) {
222 if (font.descent(s[i]) > des)
223 des = font.descent(s[i]);
224 if (font.ascent(s[i]) > asc)
225 asc = font.ascent(s[i]);
231 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
233 LyXFont font = WhichFont(type, size);
235 des = font.descent(c);
236 asc = font.ascent(c);
241 // In a near future maybe we use a better fonts renderer
242 void MathedInset::drawStr(Painter & pain, short type, int size,
243 int x, int y, byte * s, int ls)
246 if (MathIsBinary(type)) {
247 for (int i = 0; i < ls; ++i) {
248 st += string(" ") + char(s[i]) + ' ';
251 st = string(reinterpret_cast<char*>(s), ls);
253 LyXFont mf = mathed_get_font(type, size);
254 pain.text(x, y, st, mf);
258 InsetFormula::InsetFormula(bool display)
260 par = new MathParInset; // this leaks
265 par->SetType(LM_OT_PAR);
266 par->SetStyle(LM_ST_DISPLAY);
271 InsetFormula::InsetFormula(MathParInset * p)
273 par = (p->GetType()>= LM_OT_MPAR) ?
274 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
278 disp_flag = (par->GetType()>0);
283 InsetFormula::~InsetFormula()
289 Inset * InsetFormula::Clone() const
291 InsetFormula * f = new InsetFormula(par);
297 void InsetFormula::Write(ostream & os) const
304 int InsetFormula::Latex(ostream & os, signed char fragile) const
307 //#warning Alejandro, the number of lines is not returned in this case
308 // This problem will disapear at 0.13.
310 InsetFormula::Latex(output, fragile);
316 int InsetFormula::Latex(string & file, signed char fragile) const
319 //#warning Alejandro, the number of lines is not returned in this case
320 // This problem will disapear at 0.13.
324 mathed_write(par, file, &ret, fragile, label.c_str());
329 int InsetFormula::Linuxdoc(string &/*file*/) const
335 int InsetFormula::DocBook(string &/*file*/) const
341 // Check if uses AMS macros
342 void InsetFormula::Validate(LaTeXFeatures & features) const
344 // Validation only necesary if not using an AMS Style
345 if (!features.amsstyle)
346 mathedValidate(features, par);
350 void InsetFormula::Read(LyXLex & lex)
352 istream & is = lex.getStream();
354 mathed_parser_file(is, lex.GetLineNo());
356 // Silly hack to read labels.
358 mathed_parse(0, 0, &par);
360 disp_flag = (par->GetType() > 0);
362 // Update line number
363 lex.setLineNo(mathed_parser_lineno());
366 label = mathed_label;
376 int InsetFormula::ascent(Painter &, LyXFont const &) const
378 return par->Ascent() + ((disp_flag) ? 8 : 1);
382 int InsetFormula::descent(Painter &, LyXFont const &) const
384 return par->Descent() + ((disp_flag) ? 8 : 1);
388 int InsetFormula::width(Painter &, LyXFont const & f) const
390 lfont_size = f.size();
392 return par->Width(); //+2;
396 void InsetFormula::draw(Painter & pain, LyXFont const &,
397 int baseline, float & x) const
399 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
401 lfont_size = font.size();
402 /// Let's try to wait a bit with this... (Lgb)
403 //UpdatableInset::draw(pain, font, baseline, x);
405 // otherwise a segfault could occur
406 // in some XDrawRectangles (i.e. matrix) (Matthias)
407 if (mathcursor && mathcursor->GetPar() == par) {
408 if (mathcursor->Selection()) {
412 mathcursor->SelGetArea(&xp, &yp, n);
413 pain.fillPolygon(xp, yp, n, LColor::selection);
415 mathcursor->draw(pain, int(x), baseline);
417 par->draw(pain, int(x), baseline);
419 x += float(width(pain, font));
421 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
422 LyXFont font = WhichFont(LM_TC_BF, par->size);
423 font.setLatex(LyXFont::OFF);
425 if (par->GetType() == LM_OT_PARN) {
428 str = string("(") + label + ")";
431 pain.text(int(x + 20), baseline, str, font);
432 } else if (par->GetType() == LM_OT_MPARN) {
433 MathMatrixInset * mt =
434 static_cast<MathMatrixInset*>(par);
436 MathedRowSt const * crow = mt->getRowSt();
438 y = baseline + crow->getBaseline();
439 if (crow->isNumbered()) {
441 if (crow->getLabel())
442 str = string("(") + crow->getLabel() + ")";
445 pain.text(int(x + 20), y, str, font);
447 crow = crow->getNext();
451 cursor_visible = false;
455 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
457 mathcursor = new MathedCursor(par);
460 bv->updateInset(this, false);
463 mathcursor->SetPos(x, y);
469 void InsetFormula::InsetUnlock(BufferView * bv)
472 if (mathcursor->InMacroMode()) {
473 mathcursor->MacroModeClose();
479 bv->updateInset(this, false);
483 // Now a symbol can be inserted only if the inset is locked
484 void InsetFormula::InsertSymbol(BufferView * bv, char const * s)
486 if (!s || !mathcursor) return;
487 mathcursor->Interpret(s);
492 void InsetFormula::GetCursorPos(int& x, int& y) const
494 mathcursor->GetPos(x, y);
499 void InsetFormula::ToggleInsetCursor(BufferView * bv)
505 mathcursor->GetPos(x, y);
508 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
509 int asc = font.maxAscent();
510 int desc = font.maxDescent();
513 bv->hideLockedInsetCursor();
515 bv->showLockedInsetCursor(x, y, asc, desc);
516 cursor_visible = !cursor_visible;
520 void InsetFormula::ShowInsetCursor(BufferView * bv)
522 if (!cursor_visible) {
525 mathcursor->GetPos(x, y);
528 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
529 int asc = font.maxAscent();
530 int desc = font.maxDescent();
531 bv->fitLockedInsetCursor(x, y, asc, desc);
533 ToggleInsetCursor(bv);
538 void InsetFormula::HideInsetCursor(BufferView * bv)
541 ToggleInsetCursor(bv);
545 void InsetFormula::ToggleInsetSelection(BufferView * bv)
553 //mathcursor->SelGetArea(n);
554 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
558 bv->updateInset(this, false);
563 void InsetFormula::display(bool dspf)
565 if (dspf != disp_flag) {
567 par->SetType(LM_OT_PAR);
568 par->SetStyle(LM_ST_DISPLAY);
570 if (par->GetType() >= LM_OT_MPAR) {
571 MathParInset * p = new MathParInset(par);
575 mathcursor->SetPar(par);
577 par->SetType(LM_OT_MIN);
578 par->SetStyle(LM_ST_TEXT);
579 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
588 int InsetFormula::GetNumberOfLabels() const
590 // This is dirty, I know. I'll clean it at 0.13
591 if (par->GetType() == LM_OT_MPARN) {
592 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
594 MathedRowSt const * crow = mt->getRowSt();
596 if (crow->getLabel()) ++nl;
597 crow = crow->getNext();
608 string InsetFormula::getLabel(int il) const
610 //#warning This is dirty, I know. Ill clean it at 0.11
611 // Correction, the only way to clean this is with a new kernel: 0.13.
612 if (par->GetType() == LM_OT_MPARN) {
614 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
616 MathedRowSt const * crow = mt->getRowSt();
618 if (crow->getLabel()) {
620 lab = crow->getLabel();
625 crow = crow->getNext();
633 void InsetFormula::UpdateLocal(BufferView * bv)
635 par->Metrics(); // To inform lyx kernel the exact size
636 // (there were problems with arrays).
637 bv->updateInset(this, true);
641 void InsetFormula::InsetButtonRelease(BufferView * bv,
642 int x, int y, int /*button*/)
647 mathcursor->SetPos(x, y);
652 bv->updateInset(this, false);
657 void InsetFormula::InsetButtonPress(BufferView * bv,
658 int x, int y, int /*button*/)
661 sel_x = x; sel_y = y;
662 if (mathcursor->Selection()) {
663 mathcursor->SelClear();
664 bv->updateInset(this, false);
669 void InsetFormula::InsetMotionNotify(BufferView * bv,
670 int x, int y, int /*button*/)
672 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
675 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
676 mathcursor->SelStart();
678 mathcursor->GetPos(sel_x, sel_y);
684 mathcursor->SetPos(x, y);
686 mathcursor->GetPos(x, y);
687 if (sel_x!= x || sel_y!= y)
688 bv->updateInset(this, false);
689 sel_x = x; sel_y = y;
694 void InsetFormula::InsetKeyPress(XKeyEvent *)
696 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
700 // Special Mathed functions
701 bool InsetFormula::SetNumber(bool numbf)
704 short type = par->GetType();
705 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
706 if (numbf && !oldf) ++type;
707 if (!numbf && oldf) --type;
715 UpdatableInset::RESULT InsetFormula::LocalDispatch(BufferView * bv,
716 int action, string arg)
718 // extern char *dispatch_result;
719 MathedTextCodes varcode = LM_TC_MIN;
720 bool was_macro = mathcursor->InMacroMode();
722 bool space_on = false;
723 bool was_selection = mathcursor->Selection();
724 RESULT result = DISPATCHED;
725 static MathSpaceInset * sp= 0;
729 if (mathcursor->getLastCode() == LM_TC_TEX) {
734 // --- Cursor Movements ---------------------------------------------
735 case LFUN_RIGHTSEL: sel = true;
738 result = DISPATCH_RESULT(mathcursor->Right(sel));
741 case LFUN_LEFTSEL: sel = true;
744 result = DISPATCH_RESULT(mathcursor->Left(sel));
747 case LFUN_UPSEL: sel = true;
749 result = DISPATCH_RESULT(mathcursor->Up(sel));
752 case LFUN_DOWNSEL: sel = true;
754 result = DISPATCH_RESULT(mathcursor->Down(sel));
762 case LFUN_DELETE_LINE_FORWARD:
763 //current_view->lockedInsetStoreUndo(Undo::INSERT);
764 bv->lockedInsetStoreUndo(Undo::DELETE);
765 mathcursor->DelLine();
769 bv->lockedInsetStoreUndo(Undo::INSERT);
770 mathcursor->Insert(' ', LM_TC_CR);
771 par = mathcursor->GetPar();
775 bv->lockedInsetStoreUndo(Undo::INSERT);
776 mathcursor->Insert(0, LM_TC_TAB);
780 bv->lockedInsetStoreUndo(Undo::INSERT);
781 mathcursor->Insert('T', LM_TC_TAB);
785 if (!mathcursor->Left())
788 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
789 bv->updateInset(this, true);
794 //current_view->lockedInsetStoreUndo(Undo::INSERT);
795 bv->lockedInsetStoreUndo(Undo::DELETE);
796 mathcursor->Delete();
797 bv->updateInset(this, true);
800 // sprintf(dispatch_buffer, "%d %d",);
801 // dispatch_result = dispatch_buffer;
806 sscanf(arg.c_str(), "%d %d", &x, &y);
808 mathcursor->SetPos(x1+x, y1+y);
812 /* cursor selection ---------------------------- */
816 mathcursor->MacroModeClose();
817 bv->lockedInsetStoreUndo(Undo::INSERT);
818 mathcursor->SelPaste(); UpdateLocal(bv); break;
820 bv->lockedInsetStoreUndo(Undo::DELETE);
821 mathcursor->SelCut(); UpdateLocal(bv); break;
822 case LFUN_COPY: mathcursor->SelCopy(); break;
825 case LFUN_WORDRIGHTSEL:
826 case LFUN_WORDLEFTSEL:
829 // --- accented characters ------------------------------
831 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
832 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
833 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
834 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
835 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
836 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
837 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
838 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
839 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
840 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
845 if (!greek_kb_flag) {
847 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
854 case LFUN_GREEK_TOGGLE:
856 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
858 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
860 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
865 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
866 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
867 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
868 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
869 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
870 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
873 // varcode = LM_TC_TEX;
874 mathcursor->setLastCode(LM_TC_TEX);
875 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
879 case LFUN_MATH_NUMBER:
881 bv->lockedInsetStoreUndo(Undo::INSERT);
883 short type = par->GetType();
884 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
887 if (!label.empty()) {
890 bv->owner()->getMiniBuffer()->Set(_("No number"));
893 bv->owner()->getMiniBuffer()->Set(_("Number"));
901 case LFUN_MATH_NONUMBER:
903 if (par->GetType() == LM_OT_MPARN) {
904 // MathMatrixInset *mt = (MathMatrixInset*)par;
906 // mt->SetNumbered(!mt->IsNumbered());
908 mathcursor->setNumbered();
914 case LFUN_MATH_LIMITS:
916 bv->lockedInsetStoreUndo(Undo::INSERT);
917 if (mathcursor->Limits())
923 latexkeys * l = in_word_set (arg.c_str(), strlen(arg.c_str()));
924 int sz = (l) ? l->id: -1;
925 mathcursor->SetSize(sz);
930 case LFUN_INSERT_MATH:
932 bv->lockedInsetStoreUndo(Undo::INSERT);
933 InsertSymbol(bv, arg.c_str());
937 case LFUN_INSERT_MATRIX:
939 bv->lockedInsetStoreUndo(Undo::INSERT);
941 char s[80], arg2[80];
942 // This is just so that too long args won't ooze out of s.
943 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
944 k = sscanf(arg2, "%d %d %s", &m, &n, s);
953 MathMatrixInset * p = new MathMatrixInset(m, n);
954 if (mathcursor && p) {
955 if (k > 2 && int(strlen(s)) > m)
956 p->SetAlign(s[0], &s[1]);
957 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
963 case LFUN_MATH_DELIM:
965 bv->lockedInsetStoreUndo(Undo::INSERT);
966 char lf[40], rg[40], arg2[40];
967 int ilf = '(', irg = '.';
969 string vdelim("(){}[]./|");
973 strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
974 int n = sscanf(arg2, "%s %s", lf, rg);
975 lf[39] = '\0'; rg[39] = '\0';
982 l = in_word_set(lf, strlen(lf));
983 // Long words will cause l == 0; so check.
986 if (vdelim.find(lf[0]) != string::npos)
994 l = in_word_set(rg, strlen(rg));
997 if (vdelim.find(rg[0]) != string::npos)
1002 MathDelimInset * p = new MathDelimInset(ilf, irg);
1003 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1008 case LFUN_PROTECTEDSPACE:
1010 bv->lockedInsetStoreUndo(Undo::INSERT);
1011 sp = new MathSpaceInset(1);
1012 mathcursor->Insert(sp);
1018 case LFUN_INSERT_LABEL:
1020 bv->lockedInsetStoreUndo(Undo::INSERT);
1021 if (par->GetType() < LM_OT_PAR) break;
1025 result = askForText(_("Enter new label to insert:"));
1030 if (!lb.empty() && lb[0] > ' ') {
1032 if (par->GetType() == LM_OT_MPARN) {
1033 mathcursor->setLabel(lb.c_str());
1034 // MathMatrixInset *mt = (MathMatrixInset*)par;
1035 // mt->SetLabel(lb);
1037 //if (label.notEmpty()) delete label;
1046 case LFUN_MATH_DISPLAY:
1047 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1048 bv->lockedInsetStoreUndo(Undo::EDIT);
1049 display(!disp_flag);
1053 // Invalid actions under math mode
1054 case LFUN_MATH_MODE:
1056 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1057 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1058 varcode = LM_TC_TEXTRM;
1060 varcode = LM_TC_VAR;
1062 mathcursor->setLastCode(varcode);
1066 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1069 //------- dummy actions
1070 case LFUN_EXEC_COMMAND:
1071 bv->owner()->getMiniBuffer()->ExecCommand();
1075 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1076 unsigned char c = arg[0];
1077 bv->lockedInsetStoreUndo(Undo::INSERT);
1079 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1081 mathcursor->setAccent(0);
1083 if (c == 0) { // Dead key, do nothing
1084 //lyxerr << "deadkey" << endl;
1088 if (mathcursor->getLastCode() == LM_TC_TEX) {
1089 mathcursor->MacroModeOpen();
1090 mathcursor->clearLastCode();
1091 varcode = LM_TC_MIN;
1094 short f = (mathcursor->getLastCode()) ?
1095 mathcursor->getLastCode() :
1096 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1097 varcode = MathIsAlphaFont(f) ?
1098 static_cast<MathedTextCodes>(f) :
1102 // lyxerr << "Varcode << vardoce;
1103 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1104 varcode = LM_TC_MIN;
1105 if (greek_kb_flag<2) greek_kb_flag = 0;
1107 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1108 mathcursor->Insert(c, LM_TC_TEX);
1110 mathcursor->Insert('}', LM_TC_TEX);
1113 mathcursor->clearLastCode();
1114 // varcode = LM_TC_MIN;
1116 if (c == '_' && varcode == LM_TC_TEX) {
1117 mathcursor->Insert(c, LM_TC_SPECIAL);
1118 mathcursor->clearLastCode();
1119 // varcode = LM_TC_MIN;
1121 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1122 mathcursor->MacroModeOpen();
1123 mathcursor->clearLastCode();
1124 mathcursor->Insert(c, LM_TC_MIN);
1127 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1128 mathcursor->Insert(c, LM_TC_CONST);
1130 if (strchr("+/-*<>=", c))
1131 mathcursor->Insert(c, LM_TC_BOP);
1133 if (strchr(latex_special_chars, c) && c!= '_')
1134 mathcursor->Insert(c, LM_TC_SPECIAL);
1136 if (c == '_' || c == '^') {
1140 mathcursor->Interpret (s);
1144 short f = (mathcursor->getLastCode()) ?
1145 mathcursor->getLastCode() :
1146 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1147 varcode = MathIsAlphaFont(f) ?
1148 static_cast<MathedTextCodes>(f) :
1151 if (varcode == LM_TC_TEXTRM) {
1152 mathcursor->Insert(c, LM_TC_TEXTRM);
1155 mathcursor->MacroModeClose();
1158 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1162 if (!mathcursor->Pop() && mathcursor->IsEnd())
1167 mathcursor->Insert (c, LM_TC_VAR);
1171 mathcursor->MacroModeClose();
1172 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1173 mathcursor->setLastCode(LM_TC_TEX);
1177 // lyxerr << "Closed by action " << action << endl;
1181 if (was_macro != mathcursor->InMacroMode()
1183 && action != LFUN_BACKSPACE)
1185 if (sp && !space_on) sp = 0;
1186 if (mathcursor->Selection() || was_selection)
1187 ToggleInsetSelection(bv);
1189 if (result == DISPATCHED)
1190 ShowInsetCursor(bv);
1192 bv->unlockInset(this);
1199 MathFuncInset::draw(Painter & pain, int x, int y)
1201 if (name && name[0] > ' ') {
1202 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1203 font.setLatex(LyXFont::ON);
1204 x += (font.textWidth("I", 1) + 3) / 4;
1205 pain.text(x, y, name, font);
1210 void MathFuncInset::Metrics()
1212 ln = (name) ? strlen(name): 0;
1213 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1214 font.setLatex(LyXFont::ON);
1215 width = font.textWidth(name, ln) + font.textWidth("I", 1)/2;
1216 mathed_string_height(LM_TC_TEXTRM, size,
1217 reinterpret_cast<unsigned char const *>(name),
1218 strlen(name), ascent, descent);
1223 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1225 MathedIter it(par->GetData());
1227 while (it.OK() && !(features.binom && features.boldsymbol)) {
1230 MathParInset * p = it.GetActiveInset();
1231 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1232 strcmp(p->GetName(), "binom") == 0) {
1233 features.binom = true;
1235 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1236 p->setArgumentIdx(i);
1237 mathedValidate(features, p);
1241 MathedInset* p = it.GetInset();
1242 if (!features.boldsymbol && p->GetName() &&
1243 strcmp(p->GetName(), "boldsymbol") == 0) {
1244 features.boldsymbol = true;