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.
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 const * 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 const *>(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, bool) const
305 //#warning Alejandro, the number of lines is not returned in this case
306 // This problem will disapear at 0.13.
310 mathed_write(par, os, &ret, fragile, label.c_str());
315 int InsetFormula::Linuxdoc(ostream &) const
321 int InsetFormula::DocBook(ostream&) const
327 // Check if uses AMS macros
328 void InsetFormula::Validate(LaTeXFeatures & features) const
330 // Validation only necesary if not using an AMS Style
331 if (!features.amsstyle)
332 mathedValidate(features, par);
336 void InsetFormula::Read(LyXLex & lex)
338 istream & is = lex.getStream();
340 mathed_parser_file(is, lex.GetLineNo());
342 // Silly hack to read labels.
344 mathed_parse(0, 0, &par);
346 disp_flag = (par->GetType() > 0);
348 // Update line number
349 lex.setLineNo(mathed_parser_lineno());
352 label = mathed_label;
362 int InsetFormula::ascent(Painter &, LyXFont const &) const
364 return par->Ascent() + ((disp_flag) ? 8 : 1);
368 int InsetFormula::descent(Painter &, LyXFont const &) const
370 return par->Descent() + ((disp_flag) ? 8 : 1);
374 int InsetFormula::width(Painter &, LyXFont const & f) const
376 lfont_size = f.size();
378 return par->Width(); //+2;
382 void InsetFormula::draw(Painter & pain, LyXFont const & f,
383 int baseline, float & x) const
385 // Seems commenting out solves a problem.
386 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
387 font.setSize(f.size());
388 lfont_size = font.size();
389 /// Let's try to wait a bit with this... (Lgb)
390 //UpdatableInset::draw(pain, font, baseline, x);
392 // otherwise a segfault could occur
393 // in some XDrawRectangles (i.e. matrix) (Matthias)
394 if (mathcursor && mathcursor->GetPar() == par) {
395 if (mathcursor->Selection()) {
399 mathcursor->SelGetArea(&xp, &yp, n);
400 pain.fillPolygon(xp, yp, n, LColor::selection);
402 mathcursor->draw(pain, int(x), baseline);
404 par->draw(pain, int(x), baseline);
406 x += float(width(pain, font));
408 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
409 LyXFont font = WhichFont(LM_TC_BF, par->size);
410 font.setLatex(LyXFont::OFF);
412 if (par->GetType() == LM_OT_PARN) {
415 str = string("(") + label + ")";
418 pain.text(int(x + 20), baseline, str, font);
419 } else if (par->GetType() == LM_OT_MPARN) {
420 MathMatrixInset * mt =
421 static_cast<MathMatrixInset*>(par);
423 MathedRowSt const * crow = mt->getRowSt();
425 y = baseline + crow->getBaseline();
426 if (crow->isNumbered()) {
428 if (crow->getLabel())
429 str = string("(") + crow->getLabel() + ")";
432 pain.text(int(x + 20), y, str, font);
434 crow = crow->getNext();
438 cursor_visible = false;
442 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
444 mathcursor = new MathedCursor(par);
447 bv->updateInset(this, false);
450 mathcursor->SetPos(x, y);
456 void InsetFormula::InsetUnlock(BufferView * bv)
459 if (mathcursor->InMacroMode()) {
460 mathcursor->MacroModeClose();
466 bv->updateInset(this, false);
470 // Now a symbol can be inserted only if the inset is locked
471 void InsetFormula::InsertSymbol(BufferView * bv, char const * s)
473 if (!s || !mathcursor) return;
474 mathcursor->Interpret(s);
479 void InsetFormula::GetCursorPos(int& x, int& y) const
481 mathcursor->GetPos(x, y);
486 void InsetFormula::ToggleInsetCursor(BufferView * bv)
492 mathcursor->GetPos(x, y);
495 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
496 int asc = font.maxAscent();
497 int desc = font.maxDescent();
500 bv->hideLockedInsetCursor();
502 bv->showLockedInsetCursor(x, y, asc, desc);
503 cursor_visible = !cursor_visible;
507 void InsetFormula::ShowInsetCursor(BufferView * bv)
509 if (!cursor_visible) {
512 mathcursor->GetPos(x, y);
515 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
516 int asc = font.maxAscent();
517 int desc = font.maxDescent();
518 bv->fitLockedInsetCursor(x, y, asc, desc);
520 ToggleInsetCursor(bv);
525 void InsetFormula::HideInsetCursor(BufferView * bv)
528 ToggleInsetCursor(bv);
532 void InsetFormula::ToggleInsetSelection(BufferView * bv)
540 //mathcursor->SelGetArea(n);
541 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
545 bv->updateInset(this, false);
550 void InsetFormula::display(bool dspf)
552 if (dspf != disp_flag) {
554 par->SetType(LM_OT_PAR);
555 par->SetStyle(LM_ST_DISPLAY);
557 if (par->GetType() >= LM_OT_MPAR) {
558 MathParInset * p = new MathParInset(par);
562 mathcursor->SetPar(par);
564 par->SetType(LM_OT_MIN);
565 par->SetStyle(LM_ST_TEXT);
566 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
575 int InsetFormula::GetNumberOfLabels() const
577 // This is dirty, I know. I'll clean it at 0.13
578 if (par->GetType() == LM_OT_MPARN) {
579 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
581 MathedRowSt const * crow = mt->getRowSt();
583 if (crow->getLabel()) ++nl;
584 crow = crow->getNext();
595 string InsetFormula::getLabel(int il) const
597 //#warning This is dirty, I know. Ill clean it at 0.11
598 // Correction, the only way to clean this is with a new kernel: 0.13.
599 if (par->GetType() == LM_OT_MPARN) {
601 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
603 MathedRowSt const * crow = mt->getRowSt();
605 if (crow->getLabel()) {
607 lab = crow->getLabel();
612 crow = crow->getNext();
620 void InsetFormula::UpdateLocal(BufferView * bv)
622 par->Metrics(); // To inform lyx kernel the exact size
623 // (there were problems with arrays).
624 bv->updateInset(this, true);
628 void InsetFormula::InsetButtonRelease(BufferView * bv,
629 int x, int y, int /*button*/)
635 mathcursor->SetPos(x, y);
640 bv->updateInset(this, false);
646 void InsetFormula::InsetButtonPress(BufferView * bv,
647 int x, int y, int /*button*/)
650 sel_x = x; sel_y = y;
651 if (mathcursor && mathcursor->Selection()) {
652 mathcursor->SelClear();
653 bv->updateInset(this, false);
658 void InsetFormula::InsetMotionNotify(BufferView * bv,
659 int x, int y, int /*button*/)
661 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
664 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
665 mathcursor->SelStart();
667 mathcursor->GetPos(sel_x, sel_y);
673 mathcursor->SetPos(x, y);
675 mathcursor->GetPos(x, y);
676 if (sel_x!= x || sel_y!= y)
677 bv->updateInset(this, false);
678 sel_x = x; sel_y = y;
683 void InsetFormula::InsetKeyPress(XKeyEvent *)
685 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
689 // Special Mathed functions
690 bool InsetFormula::SetNumber(bool numbf)
693 short type = par->GetType();
694 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
695 if (numbf && !oldf) ++type;
696 if (!numbf && oldf) --type;
704 UpdatableInset::RESULT
705 InsetFormula::LocalDispatch(BufferView * bv,
706 int action, string const & arg)
708 // extern char *dispatch_result;
709 MathedTextCodes varcode = LM_TC_MIN;
710 bool was_macro = mathcursor->InMacroMode();
712 bool space_on = false;
713 bool was_selection = mathcursor->Selection();
714 RESULT result = DISPATCHED;
715 static MathSpaceInset * sp= 0;
719 if (mathcursor->getLastCode() == LM_TC_TEX) {
724 // --- Cursor Movements ---------------------------------------------
725 case LFUN_RIGHTSEL: sel = true;
728 result = DISPATCH_RESULT(mathcursor->Right(sel));
731 case LFUN_LEFTSEL: sel = true;
734 result = DISPATCH_RESULT(mathcursor->Left(sel));
737 case LFUN_UPSEL: sel = true;
739 result = DISPATCH_RESULT(mathcursor->Up(sel));
742 case LFUN_DOWNSEL: sel = true;
744 result = DISPATCH_RESULT(mathcursor->Down(sel));
752 case LFUN_DELETE_LINE_FORWARD:
753 //current_view->lockedInsetStoreUndo(Undo::INSERT);
754 bv->lockedInsetStoreUndo(Undo::DELETE);
755 mathcursor->DelLine();
759 bv->lockedInsetStoreUndo(Undo::INSERT);
760 mathcursor->Insert(' ', LM_TC_CR);
761 par = mathcursor->GetPar();
765 bv->lockedInsetStoreUndo(Undo::INSERT);
766 mathcursor->Insert(0, LM_TC_TAB);
770 bv->lockedInsetStoreUndo(Undo::INSERT);
771 mathcursor->Insert('T', LM_TC_TAB);
775 if (!mathcursor->Left())
778 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
779 bv->updateInset(this, true);
784 //current_view->lockedInsetStoreUndo(Undo::INSERT);
785 bv->lockedInsetStoreUndo(Undo::DELETE);
786 mathcursor->Delete();
787 bv->updateInset(this, true);
790 // sprintf(dispatch_buffer, "%d %d",);
791 // dispatch_result = dispatch_buffer;
797 istringstream ist(arg.c_str());
799 istrstream ist(arg.c_str());
803 mathcursor->SetPos(x1 + x, y1 + y);
807 /* cursor selection ---------------------------- */
811 mathcursor->MacroModeClose();
812 bv->lockedInsetStoreUndo(Undo::INSERT);
813 mathcursor->SelPaste(); UpdateLocal(bv); break;
815 bv->lockedInsetStoreUndo(Undo::DELETE);
816 mathcursor->SelCut(); UpdateLocal(bv); break;
817 case LFUN_COPY: mathcursor->SelCopy(); break;
820 case LFUN_WORDRIGHTSEL:
821 case LFUN_WORDLEFTSEL:
824 // --- accented characters ------------------------------
826 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
827 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
828 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
829 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
830 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
831 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
832 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
833 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
834 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
835 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
840 if (!greek_kb_flag) {
842 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
849 case LFUN_GREEK_TOGGLE:
851 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
853 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
855 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
860 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
861 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
862 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
863 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
864 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
865 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
868 // varcode = LM_TC_TEX;
869 mathcursor->setLastCode(LM_TC_TEX);
870 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
874 case LFUN_MATH_NUMBER:
876 bv->lockedInsetStoreUndo(Undo::INSERT);
878 short type = par->GetType();
879 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
882 if (!label.empty()) {
885 bv->owner()->getMiniBuffer()->Set(_("No number"));
888 bv->owner()->getMiniBuffer()->Set(_("Number"));
896 case LFUN_MATH_NONUMBER:
898 if (par->GetType() == LM_OT_MPARN) {
899 // MathMatrixInset *mt = (MathMatrixInset*)par;
901 // mt->SetNumbered(!mt->IsNumbered());
903 mathcursor->setNumbered();
909 case LFUN_MATH_LIMITS:
911 bv->lockedInsetStoreUndo(Undo::INSERT);
912 if (mathcursor->Limits())
918 latexkeys * l = in_word_set (arg.c_str(), strlen(arg.c_str()));
919 int sz = (l) ? l->id: -1;
920 mathcursor->SetSize(sz);
925 case LFUN_INSERT_MATH:
927 bv->lockedInsetStoreUndo(Undo::INSERT);
928 InsertSymbol(bv, arg.c_str());
932 case LFUN_INSERT_MATRIX:
934 bv->lockedInsetStoreUndo(Undo::INSERT);
936 char s[80], arg2[80];
937 // This is just so that too long args won't ooze out of s.
938 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
939 k = sscanf(arg2, "%d %d %s", &m, &n, s);
948 MathMatrixInset * p = new MathMatrixInset(m, n);
949 if (mathcursor && p) {
950 if (k > 2 && int(strlen(s)) > m)
951 p->SetAlign(s[0], &s[1]);
952 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
958 case LFUN_MATH_DELIM:
960 bv->lockedInsetStoreUndo(Undo::INSERT);
961 char lf[40], rg[40], arg2[40];
962 int ilf = '(', irg = '.';
964 string vdelim("(){}[]./|");
968 strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
969 int n = sscanf(arg2, "%s %s", lf, rg);
970 lf[39] = '\0'; rg[39] = '\0';
977 l = in_word_set(lf, strlen(lf));
978 // Long words will cause l == 0; so check.
981 if (vdelim.find(lf[0]) != string::npos)
989 l = in_word_set(rg, strlen(rg));
992 if (vdelim.find(rg[0]) != string::npos)
997 MathDelimInset * p = new MathDelimInset(ilf, irg);
998 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1003 case LFUN_PROTECTEDSPACE:
1005 bv->lockedInsetStoreUndo(Undo::INSERT);
1006 sp = new MathSpaceInset(1);
1007 mathcursor->Insert(sp);
1013 case LFUN_INSERT_LABEL:
1015 bv->lockedInsetStoreUndo(Undo::INSERT);
1016 if (par->GetType() < LM_OT_PAR) break;
1020 result = askForText(_("Enter new label to insert:"));
1025 if (!lb.empty() && lb[0] > ' ') {
1027 if (par->GetType() == LM_OT_MPARN) {
1028 mathcursor->setLabel(lb.c_str());
1029 // MathMatrixInset *mt = (MathMatrixInset*)par;
1030 // mt->SetLabel(lb);
1032 //if (label.notEmpty()) delete label;
1041 case LFUN_MATH_DISPLAY:
1042 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1043 bv->lockedInsetStoreUndo(Undo::EDIT);
1044 display(!disp_flag);
1048 // Invalid actions under math mode
1049 case LFUN_MATH_MODE:
1051 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1052 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1053 varcode = LM_TC_TEXTRM;
1055 varcode = LM_TC_VAR;
1057 mathcursor->setLastCode(varcode);
1061 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1064 //------- dummy actions
1065 case LFUN_EXEC_COMMAND:
1066 bv->owner()->getMiniBuffer()->ExecCommand();
1070 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1071 unsigned char c = arg[0];
1072 bv->lockedInsetStoreUndo(Undo::INSERT);
1074 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1076 mathcursor->setAccent(0);
1078 if (c == 0) { // Dead key, do nothing
1079 //lyxerr << "deadkey" << endl;
1083 if (mathcursor->getLastCode() == LM_TC_TEX) {
1084 mathcursor->MacroModeOpen();
1085 mathcursor->clearLastCode();
1086 varcode = LM_TC_MIN;
1089 short f = (mathcursor->getLastCode()) ?
1090 mathcursor->getLastCode() :
1091 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1092 varcode = MathIsAlphaFont(f) ?
1093 static_cast<MathedTextCodes>(f) :
1097 // lyxerr << "Varcode << vardoce;
1098 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1099 varcode = LM_TC_MIN;
1100 if (greek_kb_flag<2) greek_kb_flag = 0;
1102 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1103 mathcursor->Insert(c, LM_TC_TEX);
1105 mathcursor->Insert('}', LM_TC_TEX);
1108 mathcursor->clearLastCode();
1109 // varcode = LM_TC_MIN;
1111 if (c == '_' && varcode == LM_TC_TEX) {
1112 mathcursor->Insert(c, LM_TC_SPECIAL);
1113 mathcursor->clearLastCode();
1114 // varcode = LM_TC_MIN;
1116 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1117 mathcursor->MacroModeOpen();
1118 mathcursor->clearLastCode();
1119 mathcursor->Insert(c, LM_TC_MIN);
1122 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1123 mathcursor->Insert(c, LM_TC_CONST);
1125 if (strchr("+/-*<>=", c))
1126 mathcursor->Insert(c, LM_TC_BOP);
1128 if (strchr(latex_special_chars, c) && c!= '_')
1129 mathcursor->Insert(c, LM_TC_SPECIAL);
1131 if (c == '_' || c == '^') {
1135 mathcursor->Interpret (s);
1139 short f = (mathcursor->getLastCode()) ?
1140 mathcursor->getLastCode() :
1141 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1142 varcode = MathIsAlphaFont(f) ?
1143 static_cast<MathedTextCodes>(f) :
1146 if (varcode == LM_TC_TEXTRM) {
1147 mathcursor->Insert(c, LM_TC_TEXTRM);
1150 mathcursor->MacroModeClose();
1153 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1157 if (!mathcursor->Pop() && mathcursor->IsEnd())
1162 mathcursor->Insert (c, LM_TC_VAR);
1166 mathcursor->MacroModeClose();
1167 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1168 mathcursor->setLastCode(LM_TC_TEX);
1172 // lyxerr << "Closed by action " << action << endl;
1176 if (was_macro != mathcursor->InMacroMode()
1178 && action != LFUN_BACKSPACE)
1180 if (sp && !space_on) sp = 0;
1181 if (mathcursor->Selection() || was_selection)
1182 ToggleInsetSelection(bv);
1184 if (result == DISPATCHED)
1185 ShowInsetCursor(bv);
1187 bv->unlockInset(this);
1194 MathFuncInset::draw(Painter & pain, int x, int y)
1196 if (name && name[0] > ' ') {
1197 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1198 font.setLatex(LyXFont::ON);
1199 x += (font.textWidth("I", 1) + 3) / 4;
1200 pain.text(x, y, name, font);
1205 void MathFuncInset::Metrics()
1207 ln = (name) ? strlen(name): 0;
1208 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1209 font.setLatex(LyXFont::ON);
1210 width = font.textWidth(name, ln) + font.textWidth("I", 1)/2;
1211 mathed_string_height(LM_TC_TEXTRM, size,
1212 reinterpret_cast<unsigned char const *>(name),
1213 strlen(name), ascent, descent);
1218 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1220 MathedIter it(par->GetData());
1222 while (it.OK() && !(features.binom && features.boldsymbol)) {
1225 MathParInset * p = it.GetActiveInset();
1226 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1227 strcmp(p->GetName(), "binom") == 0) {
1228 features.binom = true;
1230 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1231 p->setArgumentIdx(i);
1232 mathedValidate(features, p);
1236 MathedInset* p = it.GetInset();
1237 if (!features.boldsymbol && p->GetName() &&
1238 strcmp(p->GetName(), "boldsymbol") == 0) {
1239 features.boldsymbol = true;