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.
20 using std::istringstream;
26 #pragma implementation "formula.h"
30 #include "commandtags.h"
31 #include "math_cursor.h"
32 #include "math_parser.h"
34 #include "minibuffer.h"
35 #include "BufferView.h"
38 #include "LaTeXFeatures.h"
40 #include "lyx_gui_misc.h"
41 #include "support/LOstream.h"
51 extern char * mathed_label;
53 extern char const * latex_special_chars;
55 short greek_kb_flag = 0;
57 LyXFont * Math_Fonts = 0; // this is only used by Whichfont and mathed_init_fonts (Lgb)
59 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
62 static int sel_x, sel_y;
64 MathedCursor * InsetFormula::mathcursor = 0;
67 int MathedInset::df_asc;
68 int MathedInset::df_des;
69 int MathedInset::df_width;
73 bool IsMacro(short token, int id)
75 return (token != LM_TK_FRAC && token != LM_TK_SQRT &&
76 !((token == LM_TK_SYM || token == LM_TC_BSYM) && id < 255));
81 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
84 LyXFont WhichFont(short type, int size)
114 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
124 f.setSize(lfont_size);
128 if (type == LM_TC_BSYM) {
138 case LM_ST_SCRIPTSCRIPT:
143 lyxerr << "Mathed Error: wrong font size: " << size << endl;
147 if (type != LM_TC_TEXTRM)
148 f.setColor(LColor::math);
153 void mathed_init_fonts() //removed 'static' because DEC cxx does not
155 // Probably because this func is declared as a friend in math_defs.h
159 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
161 for (int i = 0 ; i < 8 ; ++i) {
162 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
164 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
166 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
168 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
169 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
171 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
173 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
174 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
176 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
178 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
180 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
182 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
183 MathedInset::df_asc = lyxfont::maxAscent(f);
184 MathedInset::df_des = lyxfont::maxDescent(f);
185 MathedInset::df_width = lyxfont::width('I', f);
189 LyXFont mathed_get_font(short type, int size)
191 LyXFont f = WhichFont(type, size);
192 if (type == LM_TC_TEX) {
193 f.setLatex(LyXFont::ON);
199 int mathed_string_width(short type, int size, byte const * s, int ls)
201 LyXFont f = WhichFont(type, size);
204 if (MathIsBinary(type)) {
206 for (int i = 0; i < ls && i < 75; ++i) {
215 return lyxfont::width(reinterpret_cast<char const *>(s), ls, f);
219 int mathed_char_width(short type, int size, byte c)
221 int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1) :
222 lyxfont::width(c, WhichFont(type, size));
227 int mathed_string_height(short type, int size, byte const * s,
228 int ls, int & asc, int & des)
230 LyXFont font = WhichFont(type, size);
232 for (int i = 0; i < ls; ++i) {
233 if (lyxfont::descent(s[i], font) > des)
234 des = lyxfont::descent(s[i], font);
235 if (lyxfont::ascent(s[i], font) > asc)
236 asc = lyxfont::ascent(s[i], font);
242 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
244 LyXFont font = WhichFont(type, size);
246 des = lyxfont::descent(c, font);
247 asc = lyxfont::ascent(c, font);
252 // In a near future maybe we use a better fonts renderer
253 void MathedInset::drawStr(Painter & pain, short type, int size,
254 int x, int y, byte const * s, int ls)
257 if (MathIsBinary(type)) {
258 for (int i = 0; i < ls; ++i) {
259 st += string(" ") + char(s[i]) + ' ';
262 st = string(reinterpret_cast<char const *>(s), ls);
264 LyXFont mf = mathed_get_font(type, size);
265 pain.text(x, y, st, mf);
269 InsetFormula::InsetFormula(bool display)
271 par = new MathParInset; // this leaks
276 par->SetType(LM_OT_PAR);
277 par->SetStyle(LM_ST_DISPLAY);
282 InsetFormula::InsetFormula(MathParInset * p)
284 par = (p->GetType()>= LM_OT_MPAR) ?
285 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
289 disp_flag = (par->GetType()>0);
294 InsetFormula::~InsetFormula()
300 Inset * InsetFormula::Clone() const
302 InsetFormula * f = new InsetFormula(par);
308 void InsetFormula::Write(ostream & os) const
315 int InsetFormula::Latex(ostream & os, signed char fragile, bool) const
318 //#warning Alejandro, the number of lines is not returned in this case
319 // This problem will disapear at 0.13.
323 mathed_write(par, os, &ret, fragile, label.c_str());
328 int InsetFormula::Linuxdoc(ostream &) const
334 int InsetFormula::DocBook(ostream&) const
340 // Check if uses AMS macros
341 void InsetFormula::Validate(LaTeXFeatures & features) const
343 // Validation only necesary if not using an AMS Style
344 if (!features.amsstyle)
345 mathedValidate(features, par);
349 void InsetFormula::Read(LyXLex & lex)
351 istream & is = lex.getStream();
353 mathed_parser_file(is, lex.GetLineNo());
355 // Silly hack to read labels.
357 mathed_parse(0, 0, &par);
359 disp_flag = (par->GetType() > 0);
361 // Update line number
362 lex.setLineNo(mathed_parser_lineno());
365 label = mathed_label;
375 int InsetFormula::ascent(Painter &, LyXFont const &) const
377 return par->Ascent() + ((disp_flag) ? 8 : 1);
381 int InsetFormula::descent(Painter &, LyXFont const &) const
383 return par->Descent() + ((disp_flag) ? 8 : 1);
387 int InsetFormula::width(Painter &, LyXFont const & f) const
389 lfont_size = f.size();
391 return par->Width(); //+2;
395 void InsetFormula::draw(Painter & pain, LyXFont const & f,
396 int baseline, float & x) const
398 // Seems commenting out solves a problem.
399 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
400 font.setSize(f.size());
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 char const * InsetFormula::EditMessage() const
457 return _("Math editor mode");
461 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
463 mathcursor = new MathedCursor(par);
466 bv->updateInset(this, false);
469 mathcursor->SetPos(x, y);
475 void InsetFormula::InsetUnlock(BufferView * bv)
478 if (mathcursor->InMacroMode()) {
479 mathcursor->MacroModeClose();
485 bv->updateInset(this, false);
489 // Now a symbol can be inserted only if the inset is locked
490 void InsetFormula::InsertSymbol(BufferView * bv, char const * s)
492 if (!s || !mathcursor) return;
493 mathcursor->Interpret(s);
498 void InsetFormula::GetCursorPos(int& x, int& y) const
500 mathcursor->GetPos(x, y);
505 void InsetFormula::ToggleInsetCursor(BufferView * bv)
511 mathcursor->GetPos(x, y);
514 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
515 int asc = lyxfont::maxAscent(font);
516 int desc = lyxfont::maxDescent(font);
519 bv->hideLockedInsetCursor();
521 bv->showLockedInsetCursor(x, y, asc, desc);
522 cursor_visible = !cursor_visible;
526 void InsetFormula::ShowInsetCursor(BufferView * bv)
528 if (!cursor_visible) {
531 mathcursor->GetPos(x, y);
534 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
535 int asc = lyxfont::maxAscent(font);
536 int desc = lyxfont::maxDescent(font);
537 bv->fitLockedInsetCursor(x, y, asc, desc);
539 ToggleInsetCursor(bv);
544 void InsetFormula::HideInsetCursor(BufferView * bv)
547 ToggleInsetCursor(bv);
551 void InsetFormula::ToggleInsetSelection(BufferView * bv)
559 //mathcursor->SelGetArea(n);
560 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
564 bv->updateInset(this, false);
569 void InsetFormula::display(bool dspf)
571 if (dspf != disp_flag) {
573 par->SetType(LM_OT_PAR);
574 par->SetStyle(LM_ST_DISPLAY);
576 if (par->GetType() >= LM_OT_MPAR) {
577 MathParInset * p = new MathParInset(par);
581 mathcursor->SetPar(par);
583 par->SetType(LM_OT_MIN);
584 par->SetStyle(LM_ST_TEXT);
585 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
594 int InsetFormula::GetNumberOfLabels() const
596 // This is dirty, I know. I'll clean it at 0.13
597 if (par->GetType() == LM_OT_MPARN) {
598 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
600 MathedRowSt const * crow = mt->getRowSt();
602 if (crow->getLabel()) ++nl;
603 crow = crow->getNext();
614 string InsetFormula::getLabel(int il) const
616 //#warning This is dirty, I know. Ill clean it at 0.11
617 // Correction, the only way to clean this is with a new kernel: 0.13.
618 if (par->GetType() == LM_OT_MPARN) {
620 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
622 MathedRowSt const * crow = mt->getRowSt();
624 if (crow->getLabel()) {
626 lab = crow->getLabel();
631 crow = crow->getNext();
639 void InsetFormula::UpdateLocal(BufferView * bv)
641 par->Metrics(); // To inform lyx kernel the exact size
642 // (there were problems with arrays).
643 bv->updateInset(this, true);
647 void InsetFormula::InsetButtonRelease(BufferView * bv,
648 int x, int y, int /*button*/)
654 mathcursor->SetPos(x, y);
659 bv->updateInset(this, false);
665 void InsetFormula::InsetButtonPress(BufferView * bv,
666 int x, int y, int /*button*/)
669 sel_x = x; sel_y = y;
670 if (mathcursor && mathcursor->Selection()) {
671 mathcursor->SelClear();
672 bv->updateInset(this, false);
677 void InsetFormula::InsetMotionNotify(BufferView * bv,
678 int x, int y, int /*button*/)
680 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
683 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
684 mathcursor->SelStart();
686 mathcursor->GetPos(sel_x, sel_y);
692 mathcursor->SetPos(x, y);
694 mathcursor->GetPos(x, y);
695 if (sel_x!= x || sel_y!= y)
696 bv->updateInset(this, false);
697 sel_x = x; sel_y = y;
702 void InsetFormula::InsetKeyPress(XKeyEvent *)
704 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
708 // Special Mathed functions
709 bool InsetFormula::SetNumber(bool numbf)
712 short type = par->GetType();
713 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
714 if (numbf && !oldf) ++type;
715 if (!numbf && oldf) --type;
723 UpdatableInset::RESULT
724 InsetFormula::LocalDispatch(BufferView * bv,
725 int action, string const & arg)
727 // extern char *dispatch_result;
728 MathedTextCodes varcode = LM_TC_MIN;
729 bool was_macro = mathcursor->InMacroMode();
731 bool space_on = false;
732 bool was_selection = mathcursor->Selection();
733 RESULT result = DISPATCHED;
734 static MathSpaceInset * sp= 0;
738 if (mathcursor->getLastCode() == LM_TC_TEX) {
743 // --- Cursor Movements ---------------------------------------------
744 case LFUN_RIGHTSEL: sel = true;
747 result = DISPATCH_RESULT(mathcursor->Right(sel));
750 case LFUN_LEFTSEL: sel = true;
753 result = DISPATCH_RESULT(mathcursor->Left(sel));
756 case LFUN_UPSEL: sel = true;
758 result = DISPATCH_RESULT(mathcursor->Up(sel));
761 case LFUN_DOWNSEL: sel = true;
763 result = DISPATCH_RESULT(mathcursor->Down(sel));
771 case LFUN_DELETE_LINE_FORWARD:
772 //current_view->lockedInsetStoreUndo(Undo::INSERT);
773 bv->lockedInsetStoreUndo(Undo::DELETE);
774 mathcursor->DelLine();
778 bv->lockedInsetStoreUndo(Undo::INSERT);
779 mathcursor->Insert(' ', LM_TC_CR);
780 par = mathcursor->GetPar();
784 bv->lockedInsetStoreUndo(Undo::INSERT);
785 mathcursor->Insert(0, LM_TC_TAB);
789 bv->lockedInsetStoreUndo(Undo::INSERT);
790 mathcursor->Insert('T', LM_TC_TAB);
794 if (!mathcursor->Left())
797 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
798 bv->updateInset(this, true);
803 //current_view->lockedInsetStoreUndo(Undo::INSERT);
804 bv->lockedInsetStoreUndo(Undo::DELETE);
805 mathcursor->Delete();
806 bv->updateInset(this, true);
809 // sprintf(dispatch_buffer, "%d %d",);
810 // dispatch_result = dispatch_buffer;
816 istringstream ist(arg.c_str());
818 istrstream ist(arg.c_str());
822 mathcursor->SetPos(x1 + x, y1 + y);
826 /* cursor selection ---------------------------- */
830 mathcursor->MacroModeClose();
831 bv->lockedInsetStoreUndo(Undo::INSERT);
832 mathcursor->SelPaste(); UpdateLocal(bv); break;
834 bv->lockedInsetStoreUndo(Undo::DELETE);
835 mathcursor->SelCut(); UpdateLocal(bv); break;
836 case LFUN_COPY: mathcursor->SelCopy(); break;
839 case LFUN_WORDRIGHTSEL:
840 case LFUN_WORDLEFTSEL:
843 // --- accented characters ------------------------------
845 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
846 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
847 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
848 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
849 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
850 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
851 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
852 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
853 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
854 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
859 if (!greek_kb_flag) {
861 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
868 case LFUN_GREEK_TOGGLE:
870 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
872 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
874 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
879 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
880 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
881 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
882 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
883 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
884 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
887 // varcode = LM_TC_TEX;
888 mathcursor->setLastCode(LM_TC_TEX);
889 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
893 case LFUN_MATH_NUMBER:
895 bv->lockedInsetStoreUndo(Undo::INSERT);
897 short type = par->GetType();
898 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
901 if (!label.empty()) {
904 bv->owner()->getMiniBuffer()->Set(_("No number"));
907 bv->owner()->getMiniBuffer()->Set(_("Number"));
915 case LFUN_MATH_NONUMBER:
917 if (par->GetType() == LM_OT_MPARN) {
918 // MathMatrixInset *mt = (MathMatrixInset*)par;
920 // mt->SetNumbered(!mt->IsNumbered());
922 mathcursor->setNumbered();
928 case LFUN_MATH_LIMITS:
930 bv->lockedInsetStoreUndo(Undo::INSERT);
931 if (mathcursor->Limits())
937 latexkeys * l = in_word_set (arg.c_str(), strlen(arg.c_str()));
938 int sz = (l) ? l->id: -1;
939 mathcursor->SetSize(sz);
944 case LFUN_INSERT_MATH:
946 bv->lockedInsetStoreUndo(Undo::INSERT);
947 InsertSymbol(bv, arg.c_str());
951 case LFUN_INSERT_MATRIX:
953 bv->lockedInsetStoreUndo(Undo::INSERT);
955 char s[80], arg2[80];
956 // This is just so that too long args won't ooze out of s.
957 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
958 k = sscanf(arg2, "%d %d %s", &m, &n, s);
967 MathMatrixInset * p = new MathMatrixInset(m, n);
968 if (mathcursor && p) {
969 if (k > 2 && int(strlen(s)) > m)
970 p->SetAlign(s[0], &s[1]);
971 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
977 case LFUN_MATH_DELIM:
979 bv->lockedInsetStoreUndo(Undo::INSERT);
980 char lf[40], rg[40], arg2[40];
981 int ilf = '(', irg = '.';
983 string vdelim("(){}[]./|");
987 strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
988 int n = sscanf(arg2, "%s %s", lf, rg);
989 lf[39] = '\0'; rg[39] = '\0';
996 l = in_word_set(lf, strlen(lf));
997 // Long words will cause l == 0; so check.
1000 if (vdelim.find(lf[0]) != string::npos)
1008 l = in_word_set(rg, strlen(rg));
1011 if (vdelim.find(rg[0]) != string::npos)
1016 MathDelimInset * p = new MathDelimInset(ilf, irg);
1017 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1022 case LFUN_PROTECTEDSPACE:
1024 bv->lockedInsetStoreUndo(Undo::INSERT);
1025 sp = new MathSpaceInset(1);
1026 mathcursor->Insert(sp);
1032 case LFUN_INSERT_LABEL:
1034 bv->lockedInsetStoreUndo(Undo::INSERT);
1035 if (par->GetType() < LM_OT_PAR) break;
1039 result = askForText(_("Enter new label to insert:"));
1044 if (!lb.empty() && lb[0] > ' ') {
1046 if (par->GetType() == LM_OT_MPARN) {
1047 mathcursor->setLabel(lb.c_str());
1048 // MathMatrixInset *mt = (MathMatrixInset*)par;
1049 // mt->SetLabel(lb);
1051 //if (label.notEmpty()) delete label;
1060 case LFUN_MATH_DISPLAY:
1061 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1062 bv->lockedInsetStoreUndo(Undo::EDIT);
1063 display(!disp_flag);
1067 // Invalid actions under math mode
1068 case LFUN_MATH_MODE:
1070 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1071 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1072 varcode = LM_TC_TEXTRM;
1074 varcode = LM_TC_VAR;
1076 mathcursor->setLastCode(varcode);
1080 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1083 //------- dummy actions
1084 case LFUN_EXEC_COMMAND:
1085 bv->owner()->getMiniBuffer()->ExecCommand();
1089 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1090 unsigned char c = arg[0];
1091 bv->lockedInsetStoreUndo(Undo::INSERT);
1093 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1095 mathcursor->setAccent(0);
1097 if (c == 0) { // Dead key, do nothing
1098 //lyxerr << "deadkey" << endl;
1102 if (mathcursor->getLastCode() == LM_TC_TEX) {
1103 mathcursor->MacroModeOpen();
1104 mathcursor->clearLastCode();
1105 varcode = LM_TC_MIN;
1108 short f = (mathcursor->getLastCode()) ?
1109 mathcursor->getLastCode() :
1110 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1111 varcode = MathIsAlphaFont(f) ?
1112 static_cast<MathedTextCodes>(f) :
1116 // lyxerr << "Varcode << vardoce;
1117 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1118 varcode = LM_TC_MIN;
1119 if (greek_kb_flag<2) greek_kb_flag = 0;
1121 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1122 mathcursor->Insert(c, LM_TC_TEX);
1124 mathcursor->Insert('}', LM_TC_TEX);
1127 mathcursor->clearLastCode();
1128 // varcode = LM_TC_MIN;
1130 if (c == '_' && varcode == LM_TC_TEX) {
1131 mathcursor->Insert(c, LM_TC_SPECIAL);
1132 mathcursor->clearLastCode();
1133 // varcode = LM_TC_MIN;
1135 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1136 mathcursor->MacroModeOpen();
1137 mathcursor->clearLastCode();
1138 mathcursor->Insert(c, LM_TC_MIN);
1141 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1142 mathcursor->Insert(c, LM_TC_CONST);
1144 if (strchr("+/-*<>=", c))
1145 mathcursor->Insert(c, LM_TC_BOP);
1147 if (strchr(latex_special_chars, c) && c!= '_')
1148 mathcursor->Insert(c, LM_TC_SPECIAL);
1150 if (c == '_' || c == '^') {
1154 mathcursor->Interpret (s);
1158 short f = (mathcursor->getLastCode()) ?
1159 mathcursor->getLastCode() :
1160 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1161 varcode = MathIsAlphaFont(f) ?
1162 static_cast<MathedTextCodes>(f) :
1165 if (varcode == LM_TC_TEXTRM) {
1166 mathcursor->Insert(c, LM_TC_TEXTRM);
1169 mathcursor->MacroModeClose();
1172 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1176 if (!mathcursor->Pop() && mathcursor->IsEnd())
1181 mathcursor->Insert (c, LM_TC_VAR);
1185 mathcursor->MacroModeClose();
1186 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1187 mathcursor->setLastCode(LM_TC_TEX);
1191 // lyxerr << "Closed by action " << action << endl;
1195 if (was_macro != mathcursor->InMacroMode()
1197 && action != LFUN_BACKSPACE)
1199 if (sp && !space_on) sp = 0;
1200 if (mathcursor->Selection() || was_selection)
1201 ToggleInsetSelection(bv);
1203 if (result == DISPATCHED)
1204 ShowInsetCursor(bv);
1206 bv->unlockInset(this);
1213 MathFuncInset::draw(Painter & pain, int x, int y)
1215 if (name && name[0] > ' ') {
1216 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1217 font.setLatex(LyXFont::ON);
1218 x += (lyxfont::width('I', font) + 3) / 4;
1219 pain.text(x, y, name, font);
1224 void MathFuncInset::Metrics()
1226 ln = (name) ? strlen(name): 0;
1227 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1228 font.setLatex(LyXFont::ON);
1229 width = lyxfont::width(name, ln, font)
1230 + lyxfont::width('I', font) / 2;
1231 mathed_string_height(LM_TC_TEXTRM, size,
1232 reinterpret_cast<unsigned char const *>(name),
1233 strlen(name), ascent, descent);
1238 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1240 MathedIter it(par->GetData());
1242 while (it.OK() && !(features.binom && features.boldsymbol)) {
1245 MathParInset * p = it.GetActiveInset();
1246 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1247 strcmp(p->GetName(), "binom") == 0) {
1248 features.binom = true;
1250 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1251 p->setArgumentIdx(i);
1252 mathedValidate(features, p);
1256 MathedInset* p = it.GetInset();
1257 if (!features.boldsymbol && p->GetName() &&
1258 strcmp(p->GetName(), "boldsymbol") == 0) {
1259 features.boldsymbol = true;