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"
48 extern char * mathed_label;
50 extern char const * latex_special_chars;
52 short greek_kb_flag = 0;
54 LyXFont * Math_Fonts = 0; // this is only used by Whichfont and mathed_init_fonts (Lgb)
56 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
59 static int sel_x, sel_y;
61 MathedCursor * InsetFormula::mathcursor = 0;
64 int MathedInset::df_asc;
65 int MathedInset::df_des;
66 int MathedInset::df_width;
70 bool IsMacro(short token, int id)
72 return (token != LM_TK_FRAC && token != LM_TK_SQRT &&
73 !((token == LM_TK_SYM || token == LM_TC_BSYM) && id < 255));
78 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
81 LyXFont WhichFont(short type, int size)
111 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
121 f.setSize(lfont_size);
125 if (type == LM_TC_BSYM) {
135 case LM_ST_SCRIPTSCRIPT:
140 lyxerr << "Mathed Error: wrong font size: " << size << endl;
144 if (type != LM_TC_TEXTRM)
145 f.setColor(LColor::math);
150 void mathed_init_fonts() //removed 'static' because DEC cxx does not
152 // Probably because this func is declared as a friend in math_defs.h
156 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
158 for (int i = 0 ; i < 8 ; ++i) {
159 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
161 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
163 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
165 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
166 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
168 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
170 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
171 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
173 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
175 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
177 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
179 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
180 MathedInset::df_asc = f.maxAscent();
181 MathedInset::df_des = f.maxDescent();
182 MathedInset::df_width = f.width('I');
186 LyXFont mathed_get_font(short type, int size)
188 LyXFont f = WhichFont(type, size);
189 if (type == LM_TC_TEX) {
190 f.setLatex(LyXFont::ON);
196 int mathed_string_width(short type, int size, byte const * s, int ls)
198 LyXFont f = WhichFont(type, size);
201 if (MathIsBinary(type)) {
203 for (int i = 0; i < ls && i < 75; ++i) {
212 return f.textWidth(reinterpret_cast<char const *>(s), ls);
216 int mathed_char_width(short type, int size, byte c)
218 int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1):
219 WhichFont(type, size).width(c);
224 int mathed_string_height(short type, int size, byte const * s,
225 int ls, int & asc, int & des)
227 LyXFont font = WhichFont(type, size);
229 for (int i = 0; i < ls; ++i) {
230 if (font.descent(s[i]) > des)
231 des = font.descent(s[i]);
232 if (font.ascent(s[i]) > asc)
233 asc = font.ascent(s[i]);
239 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
241 LyXFont font = WhichFont(type, size);
243 des = font.descent(c);
244 asc = font.ascent(c);
249 // In a near future maybe we use a better fonts renderer
250 void MathedInset::drawStr(Painter & pain, short type, int size,
251 int x, int y, byte const * s, int ls)
254 if (MathIsBinary(type)) {
255 for (int i = 0; i < ls; ++i) {
256 st += string(" ") + char(s[i]) + ' ';
259 st = string(reinterpret_cast<char const *>(s), ls);
261 LyXFont mf = mathed_get_font(type, size);
262 pain.text(x, y, st, mf);
266 InsetFormula::InsetFormula(bool display)
268 par = new MathParInset; // this leaks
273 par->SetType(LM_OT_PAR);
274 par->SetStyle(LM_ST_DISPLAY);
279 InsetFormula::InsetFormula(MathParInset * p)
281 par = (p->GetType()>= LM_OT_MPAR) ?
282 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
286 disp_flag = (par->GetType()>0);
291 InsetFormula::~InsetFormula()
297 Inset * InsetFormula::Clone() const
299 InsetFormula * f = new InsetFormula(par);
305 void InsetFormula::Write(ostream & os) const
312 int InsetFormula::Latex(ostream & os, signed char fragile, bool) const
315 //#warning Alejandro, the number of lines is not returned in this case
316 // This problem will disapear at 0.13.
320 mathed_write(par, os, &ret, fragile, label.c_str());
325 int InsetFormula::Linuxdoc(ostream &) const
331 int InsetFormula::DocBook(ostream&) const
337 // Check if uses AMS macros
338 void InsetFormula::Validate(LaTeXFeatures & features) const
340 // Validation only necesary if not using an AMS Style
341 if (!features.amsstyle)
342 mathedValidate(features, par);
346 void InsetFormula::Read(LyXLex & lex)
348 istream & is = lex.getStream();
350 mathed_parser_file(is, lex.GetLineNo());
352 // Silly hack to read labels.
354 mathed_parse(0, 0, &par);
356 disp_flag = (par->GetType() > 0);
358 // Update line number
359 lex.setLineNo(mathed_parser_lineno());
362 label = mathed_label;
372 int InsetFormula::ascent(Painter &, LyXFont const &) const
374 return par->Ascent() + ((disp_flag) ? 8 : 1);
378 int InsetFormula::descent(Painter &, LyXFont const &) const
380 return par->Descent() + ((disp_flag) ? 8 : 1);
384 int InsetFormula::width(Painter &, LyXFont const & f) const
386 lfont_size = f.size();
388 return par->Width(); //+2;
392 void InsetFormula::draw(Painter & pain, LyXFont const & f,
393 int baseline, float & x) const
395 // Seems commenting out solves a problem.
396 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
397 font.setSize(f.size());
398 lfont_size = font.size();
399 /// Let's try to wait a bit with this... (Lgb)
400 //UpdatableInset::draw(pain, font, baseline, x);
402 // otherwise a segfault could occur
403 // in some XDrawRectangles (i.e. matrix) (Matthias)
404 if (mathcursor && mathcursor->GetPar() == par) {
405 if (mathcursor->Selection()) {
409 mathcursor->SelGetArea(&xp, &yp, n);
410 pain.fillPolygon(xp, yp, n, LColor::selection);
412 mathcursor->draw(pain, int(x), baseline);
414 par->draw(pain, int(x), baseline);
416 x += float(width(pain, font));
418 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
419 LyXFont font = WhichFont(LM_TC_BF, par->size);
420 font.setLatex(LyXFont::OFF);
422 if (par->GetType() == LM_OT_PARN) {
425 str = string("(") + label + ")";
428 pain.text(int(x + 20), baseline, str, font);
429 } else if (par->GetType() == LM_OT_MPARN) {
430 MathMatrixInset * mt =
431 static_cast<MathMatrixInset*>(par);
433 MathedRowSt const * crow = mt->getRowSt();
435 y = baseline + crow->getBaseline();
436 if (crow->isNumbered()) {
438 if (crow->getLabel())
439 str = string("(") + crow->getLabel() + ")";
442 pain.text(int(x + 20), y, str, font);
444 crow = crow->getNext();
448 cursor_visible = false;
452 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
454 mathcursor = new MathedCursor(par);
457 bv->updateInset(this, false);
460 mathcursor->SetPos(x, y);
466 void InsetFormula::InsetUnlock(BufferView * bv)
469 if (mathcursor->InMacroMode()) {
470 mathcursor->MacroModeClose();
476 bv->updateInset(this, false);
480 // Now a symbol can be inserted only if the inset is locked
481 void InsetFormula::InsertSymbol(BufferView * bv, char const * s)
483 if (!s || !mathcursor) return;
484 mathcursor->Interpret(s);
489 void InsetFormula::GetCursorPos(int& x, int& y) const
491 mathcursor->GetPos(x, y);
496 void InsetFormula::ToggleInsetCursor(BufferView * bv)
502 mathcursor->GetPos(x, y);
505 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
506 int asc = font.maxAscent();
507 int desc = font.maxDescent();
510 bv->hideLockedInsetCursor();
512 bv->showLockedInsetCursor(x, y, asc, desc);
513 cursor_visible = !cursor_visible;
517 void InsetFormula::ShowInsetCursor(BufferView * bv)
519 if (!cursor_visible) {
522 mathcursor->GetPos(x, y);
525 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
526 int asc = font.maxAscent();
527 int desc = font.maxDescent();
528 bv->fitLockedInsetCursor(x, y, asc, desc);
530 ToggleInsetCursor(bv);
535 void InsetFormula::HideInsetCursor(BufferView * bv)
538 ToggleInsetCursor(bv);
542 void InsetFormula::ToggleInsetSelection(BufferView * bv)
550 //mathcursor->SelGetArea(n);
551 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
555 bv->updateInset(this, false);
560 void InsetFormula::display(bool dspf)
562 if (dspf != disp_flag) {
564 par->SetType(LM_OT_PAR);
565 par->SetStyle(LM_ST_DISPLAY);
567 if (par->GetType() >= LM_OT_MPAR) {
568 MathParInset * p = new MathParInset(par);
572 mathcursor->SetPar(par);
574 par->SetType(LM_OT_MIN);
575 par->SetStyle(LM_ST_TEXT);
576 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
585 int InsetFormula::GetNumberOfLabels() const
587 // This is dirty, I know. I'll clean it at 0.13
588 if (par->GetType() == LM_OT_MPARN) {
589 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
591 MathedRowSt const * crow = mt->getRowSt();
593 if (crow->getLabel()) ++nl;
594 crow = crow->getNext();
605 string InsetFormula::getLabel(int il) const
607 //#warning This is dirty, I know. Ill clean it at 0.11
608 // Correction, the only way to clean this is with a new kernel: 0.13.
609 if (par->GetType() == LM_OT_MPARN) {
611 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
613 MathedRowSt const * crow = mt->getRowSt();
615 if (crow->getLabel()) {
617 lab = crow->getLabel();
622 crow = crow->getNext();
630 void InsetFormula::UpdateLocal(BufferView * bv)
632 par->Metrics(); // To inform lyx kernel the exact size
633 // (there were problems with arrays).
634 bv->updateInset(this, true);
638 void InsetFormula::InsetButtonRelease(BufferView * bv,
639 int x, int y, int /*button*/)
645 mathcursor->SetPos(x, y);
650 bv->updateInset(this, false);
656 void InsetFormula::InsetButtonPress(BufferView * bv,
657 int x, int y, int /*button*/)
660 sel_x = x; sel_y = y;
661 if (mathcursor && mathcursor->Selection()) {
662 mathcursor->SelClear();
663 bv->updateInset(this, false);
668 void InsetFormula::InsetMotionNotify(BufferView * bv,
669 int x, int y, int /*button*/)
671 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
674 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
675 mathcursor->SelStart();
677 mathcursor->GetPos(sel_x, sel_y);
683 mathcursor->SetPos(x, y);
685 mathcursor->GetPos(x, y);
686 if (sel_x!= x || sel_y!= y)
687 bv->updateInset(this, false);
688 sel_x = x; sel_y = y;
693 void InsetFormula::InsetKeyPress(XKeyEvent *)
695 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
699 // Special Mathed functions
700 bool InsetFormula::SetNumber(bool numbf)
703 short type = par->GetType();
704 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
705 if (numbf && !oldf) ++type;
706 if (!numbf && oldf) --type;
714 UpdatableInset::RESULT
715 InsetFormula::LocalDispatch(BufferView * bv,
716 int action, string const & 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;
807 istringstream ist(arg.c_str());
809 istrstream ist(arg.c_str());
813 mathcursor->SetPos(x1 + x, y1 + y);
817 /* cursor selection ---------------------------- */
821 mathcursor->MacroModeClose();
822 bv->lockedInsetStoreUndo(Undo::INSERT);
823 mathcursor->SelPaste(); UpdateLocal(bv); break;
825 bv->lockedInsetStoreUndo(Undo::DELETE);
826 mathcursor->SelCut(); UpdateLocal(bv); break;
827 case LFUN_COPY: mathcursor->SelCopy(); break;
830 case LFUN_WORDRIGHTSEL:
831 case LFUN_WORDLEFTSEL:
834 // --- accented characters ------------------------------
836 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
837 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
838 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
839 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
840 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
841 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
842 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
843 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
844 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
845 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
850 if (!greek_kb_flag) {
852 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
859 case LFUN_GREEK_TOGGLE:
861 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
863 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
865 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
870 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
871 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
872 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
873 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
874 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
875 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
878 // varcode = LM_TC_TEX;
879 mathcursor->setLastCode(LM_TC_TEX);
880 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
884 case LFUN_MATH_NUMBER:
886 bv->lockedInsetStoreUndo(Undo::INSERT);
888 short type = par->GetType();
889 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
892 if (!label.empty()) {
895 bv->owner()->getMiniBuffer()->Set(_("No number"));
898 bv->owner()->getMiniBuffer()->Set(_("Number"));
906 case LFUN_MATH_NONUMBER:
908 if (par->GetType() == LM_OT_MPARN) {
909 // MathMatrixInset *mt = (MathMatrixInset*)par;
911 // mt->SetNumbered(!mt->IsNumbered());
913 mathcursor->setNumbered();
919 case LFUN_MATH_LIMITS:
921 bv->lockedInsetStoreUndo(Undo::INSERT);
922 if (mathcursor->Limits())
928 latexkeys * l = in_word_set (arg.c_str(), strlen(arg.c_str()));
929 int sz = (l) ? l->id: -1;
930 mathcursor->SetSize(sz);
935 case LFUN_INSERT_MATH:
937 bv->lockedInsetStoreUndo(Undo::INSERT);
938 InsertSymbol(bv, arg.c_str());
942 case LFUN_INSERT_MATRIX:
944 bv->lockedInsetStoreUndo(Undo::INSERT);
946 char s[80], arg2[80];
947 // This is just so that too long args won't ooze out of s.
948 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
949 k = sscanf(arg2, "%d %d %s", &m, &n, s);
958 MathMatrixInset * p = new MathMatrixInset(m, n);
959 if (mathcursor && p) {
960 if (k > 2 && int(strlen(s)) > m)
961 p->SetAlign(s[0], &s[1]);
962 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
968 case LFUN_MATH_DELIM:
970 bv->lockedInsetStoreUndo(Undo::INSERT);
971 char lf[40], rg[40], arg2[40];
972 int ilf = '(', irg = '.';
974 string vdelim("(){}[]./|");
978 strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
979 int n = sscanf(arg2, "%s %s", lf, rg);
980 lf[39] = '\0'; rg[39] = '\0';
987 l = in_word_set(lf, strlen(lf));
988 // Long words will cause l == 0; so check.
991 if (vdelim.find(lf[0]) != string::npos)
999 l = in_word_set(rg, strlen(rg));
1002 if (vdelim.find(rg[0]) != string::npos)
1007 MathDelimInset * p = new MathDelimInset(ilf, irg);
1008 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1013 case LFUN_PROTECTEDSPACE:
1015 bv->lockedInsetStoreUndo(Undo::INSERT);
1016 sp = new MathSpaceInset(1);
1017 mathcursor->Insert(sp);
1023 case LFUN_INSERT_LABEL:
1025 bv->lockedInsetStoreUndo(Undo::INSERT);
1026 if (par->GetType() < LM_OT_PAR) break;
1030 result = askForText(_("Enter new label to insert:"));
1035 if (!lb.empty() && lb[0] > ' ') {
1037 if (par->GetType() == LM_OT_MPARN) {
1038 mathcursor->setLabel(lb.c_str());
1039 // MathMatrixInset *mt = (MathMatrixInset*)par;
1040 // mt->SetLabel(lb);
1042 //if (label.notEmpty()) delete label;
1051 case LFUN_MATH_DISPLAY:
1052 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1053 bv->lockedInsetStoreUndo(Undo::EDIT);
1054 display(!disp_flag);
1058 // Invalid actions under math mode
1059 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()->ExecCommand();
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);
1088 if (c == 0) { // Dead key, do nothing
1089 //lyxerr << "deadkey" << endl;
1093 if (mathcursor->getLastCode() == LM_TC_TEX) {
1094 mathcursor->MacroModeOpen();
1095 mathcursor->clearLastCode();
1096 varcode = LM_TC_MIN;
1099 short f = (mathcursor->getLastCode()) ?
1100 mathcursor->getLastCode() :
1101 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1102 varcode = MathIsAlphaFont(f) ?
1103 static_cast<MathedTextCodes>(f) :
1107 // lyxerr << "Varcode << vardoce;
1108 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1109 varcode = LM_TC_MIN;
1110 if (greek_kb_flag<2) greek_kb_flag = 0;
1112 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1113 mathcursor->Insert(c, LM_TC_TEX);
1115 mathcursor->Insert('}', LM_TC_TEX);
1118 mathcursor->clearLastCode();
1119 // varcode = LM_TC_MIN;
1121 if (c == '_' && varcode == LM_TC_TEX) {
1122 mathcursor->Insert(c, LM_TC_SPECIAL);
1123 mathcursor->clearLastCode();
1124 // varcode = LM_TC_MIN;
1126 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1127 mathcursor->MacroModeOpen();
1128 mathcursor->clearLastCode();
1129 mathcursor->Insert(c, LM_TC_MIN);
1132 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1133 mathcursor->Insert(c, LM_TC_CONST);
1135 if (strchr("+/-*<>=", c))
1136 mathcursor->Insert(c, LM_TC_BOP);
1138 if (strchr(latex_special_chars, c) && c!= '_')
1139 mathcursor->Insert(c, LM_TC_SPECIAL);
1141 if (c == '_' || c == '^') {
1145 mathcursor->Interpret (s);
1149 short f = (mathcursor->getLastCode()) ?
1150 mathcursor->getLastCode() :
1151 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1152 varcode = MathIsAlphaFont(f) ?
1153 static_cast<MathedTextCodes>(f) :
1156 if (varcode == LM_TC_TEXTRM) {
1157 mathcursor->Insert(c, LM_TC_TEXTRM);
1160 mathcursor->MacroModeClose();
1163 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1167 if (!mathcursor->Pop() && mathcursor->IsEnd())
1172 mathcursor->Insert (c, LM_TC_VAR);
1176 mathcursor->MacroModeClose();
1177 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1178 mathcursor->setLastCode(LM_TC_TEX);
1182 // lyxerr << "Closed by action " << action << endl;
1186 if (was_macro != mathcursor->InMacroMode()
1188 && action != LFUN_BACKSPACE)
1190 if (sp && !space_on) sp = 0;
1191 if (mathcursor->Selection() || was_selection)
1192 ToggleInsetSelection(bv);
1194 if (result == DISPATCHED)
1195 ShowInsetCursor(bv);
1197 bv->unlockInset(this);
1204 MathFuncInset::draw(Painter & pain, int x, int y)
1206 if (name && name[0] > ' ') {
1207 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1208 font.setLatex(LyXFont::ON);
1209 x += (font.textWidth("I", 1) + 3) / 4;
1210 pain.text(x, y, name, font);
1215 void MathFuncInset::Metrics()
1217 ln = (name) ? strlen(name): 0;
1218 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1219 font.setLatex(LyXFont::ON);
1220 width = font.textWidth(name, ln) + font.textWidth("I", 1)/2;
1221 mathed_string_height(LM_TC_TEXTRM, size,
1222 reinterpret_cast<unsigned char const *>(name),
1223 strlen(name), ascent, descent);
1228 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1230 MathedIter it(par->GetData());
1232 while (it.OK() && !(features.binom && features.boldsymbol)) {
1235 MathParInset * p = it.GetActiveInset();
1236 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1237 strcmp(p->GetName(), "binom") == 0) {
1238 features.binom = true;
1240 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1241 p->setArgumentIdx(i);
1242 mathedValidate(features, p);
1246 MathedInset* p = it.GetInset();
1247 if (!features.boldsymbol && p->GetName() &&
1248 strcmp(p->GetName(), "boldsymbol") == 0) {
1249 features.boldsymbol = true;