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*/)
634 mathcursor->SetPos(x, y);
639 bv->updateInset(this, false);
644 void InsetFormula::InsetButtonPress(BufferView * bv,
645 int x, int y, int /*button*/)
648 sel_x = x; sel_y = y;
649 if (mathcursor->Selection()) {
650 mathcursor->SelClear();
651 bv->updateInset(this, false);
656 void InsetFormula::InsetMotionNotify(BufferView * bv,
657 int x, int y, int /*button*/)
659 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
662 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
663 mathcursor->SelStart();
665 mathcursor->GetPos(sel_x, sel_y);
671 mathcursor->SetPos(x, y);
673 mathcursor->GetPos(x, y);
674 if (sel_x!= x || sel_y!= y)
675 bv->updateInset(this, false);
676 sel_x = x; sel_y = y;
681 void InsetFormula::InsetKeyPress(XKeyEvent *)
683 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
687 // Special Mathed functions
688 bool InsetFormula::SetNumber(bool numbf)
691 short type = par->GetType();
692 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
693 if (numbf && !oldf) ++type;
694 if (!numbf && oldf) --type;
702 UpdatableInset::RESULT
703 InsetFormula::LocalDispatch(BufferView * bv,
704 int action, string const & arg)
706 // extern char *dispatch_result;
707 MathedTextCodes varcode = LM_TC_MIN;
708 bool was_macro = mathcursor->InMacroMode();
710 bool space_on = false;
711 bool was_selection = mathcursor->Selection();
712 RESULT result = DISPATCHED;
713 static MathSpaceInset * sp= 0;
717 if (mathcursor->getLastCode() == LM_TC_TEX) {
722 // --- Cursor Movements ---------------------------------------------
723 case LFUN_RIGHTSEL: sel = true;
726 result = DISPATCH_RESULT(mathcursor->Right(sel));
729 case LFUN_LEFTSEL: sel = true;
732 result = DISPATCH_RESULT(mathcursor->Left(sel));
735 case LFUN_UPSEL: sel = true;
737 result = DISPATCH_RESULT(mathcursor->Up(sel));
740 case LFUN_DOWNSEL: sel = true;
742 result = DISPATCH_RESULT(mathcursor->Down(sel));
750 case LFUN_DELETE_LINE_FORWARD:
751 //current_view->lockedInsetStoreUndo(Undo::INSERT);
752 bv->lockedInsetStoreUndo(Undo::DELETE);
753 mathcursor->DelLine();
757 bv->lockedInsetStoreUndo(Undo::INSERT);
758 mathcursor->Insert(' ', LM_TC_CR);
759 par = mathcursor->GetPar();
763 bv->lockedInsetStoreUndo(Undo::INSERT);
764 mathcursor->Insert(0, LM_TC_TAB);
768 bv->lockedInsetStoreUndo(Undo::INSERT);
769 mathcursor->Insert('T', LM_TC_TAB);
773 if (!mathcursor->Left())
776 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
777 bv->updateInset(this, true);
782 //current_view->lockedInsetStoreUndo(Undo::INSERT);
783 bv->lockedInsetStoreUndo(Undo::DELETE);
784 mathcursor->Delete();
785 bv->updateInset(this, true);
788 // sprintf(dispatch_buffer, "%d %d",);
789 // dispatch_result = dispatch_buffer;
795 istringstream ist(arg.c_str());
797 istrstream ist(arg.c_str());
801 mathcursor->SetPos(x1 + x, y1 + y);
805 /* cursor selection ---------------------------- */
809 mathcursor->MacroModeClose();
810 bv->lockedInsetStoreUndo(Undo::INSERT);
811 mathcursor->SelPaste(); UpdateLocal(bv); break;
813 bv->lockedInsetStoreUndo(Undo::DELETE);
814 mathcursor->SelCut(); UpdateLocal(bv); break;
815 case LFUN_COPY: mathcursor->SelCopy(); break;
818 case LFUN_WORDRIGHTSEL:
819 case LFUN_WORDLEFTSEL:
822 // --- accented characters ------------------------------
824 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
825 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
826 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
827 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
828 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
829 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
830 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
831 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
832 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
833 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
838 if (!greek_kb_flag) {
840 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
847 case LFUN_GREEK_TOGGLE:
849 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
851 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
853 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
858 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
859 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
860 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
861 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
862 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
863 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
866 // varcode = LM_TC_TEX;
867 mathcursor->setLastCode(LM_TC_TEX);
868 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
872 case LFUN_MATH_NUMBER:
874 bv->lockedInsetStoreUndo(Undo::INSERT);
876 short type = par->GetType();
877 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
880 if (!label.empty()) {
883 bv->owner()->getMiniBuffer()->Set(_("No number"));
886 bv->owner()->getMiniBuffer()->Set(_("Number"));
894 case LFUN_MATH_NONUMBER:
896 if (par->GetType() == LM_OT_MPARN) {
897 // MathMatrixInset *mt = (MathMatrixInset*)par;
899 // mt->SetNumbered(!mt->IsNumbered());
901 mathcursor->setNumbered();
907 case LFUN_MATH_LIMITS:
909 bv->lockedInsetStoreUndo(Undo::INSERT);
910 if (mathcursor->Limits())
916 latexkeys * l = in_word_set (arg.c_str(), strlen(arg.c_str()));
917 int sz = (l) ? l->id: -1;
918 mathcursor->SetSize(sz);
923 case LFUN_INSERT_MATH:
925 bv->lockedInsetStoreUndo(Undo::INSERT);
926 InsertSymbol(bv, arg.c_str());
930 case LFUN_INSERT_MATRIX:
932 bv->lockedInsetStoreUndo(Undo::INSERT);
934 char s[80], arg2[80];
935 // This is just so that too long args won't ooze out of s.
936 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
937 k = sscanf(arg2, "%d %d %s", &m, &n, s);
946 MathMatrixInset * p = new MathMatrixInset(m, n);
947 if (mathcursor && p) {
948 if (k > 2 && int(strlen(s)) > m)
949 p->SetAlign(s[0], &s[1]);
950 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
956 case LFUN_MATH_DELIM:
958 bv->lockedInsetStoreUndo(Undo::INSERT);
959 char lf[40], rg[40], arg2[40];
960 int ilf = '(', irg = '.';
962 string vdelim("(){}[]./|");
966 strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
967 int n = sscanf(arg2, "%s %s", lf, rg);
968 lf[39] = '\0'; rg[39] = '\0';
975 l = in_word_set(lf, strlen(lf));
976 // Long words will cause l == 0; so check.
979 if (vdelim.find(lf[0]) != string::npos)
987 l = in_word_set(rg, strlen(rg));
990 if (vdelim.find(rg[0]) != string::npos)
995 MathDelimInset * p = new MathDelimInset(ilf, irg);
996 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1001 case LFUN_PROTECTEDSPACE:
1003 bv->lockedInsetStoreUndo(Undo::INSERT);
1004 sp = new MathSpaceInset(1);
1005 mathcursor->Insert(sp);
1011 case LFUN_INSERT_LABEL:
1013 bv->lockedInsetStoreUndo(Undo::INSERT);
1014 if (par->GetType() < LM_OT_PAR) break;
1018 result = askForText(_("Enter new label to insert:"));
1023 if (!lb.empty() && lb[0] > ' ') {
1025 if (par->GetType() == LM_OT_MPARN) {
1026 mathcursor->setLabel(lb.c_str());
1027 // MathMatrixInset *mt = (MathMatrixInset*)par;
1028 // mt->SetLabel(lb);
1030 //if (label.notEmpty()) delete label;
1039 case LFUN_MATH_DISPLAY:
1040 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1041 bv->lockedInsetStoreUndo(Undo::EDIT);
1042 display(!disp_flag);
1046 // Invalid actions under math mode
1047 case LFUN_MATH_MODE:
1049 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1050 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1051 varcode = LM_TC_TEXTRM;
1053 varcode = LM_TC_VAR;
1055 mathcursor->setLastCode(varcode);
1059 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1062 //------- dummy actions
1063 case LFUN_EXEC_COMMAND:
1064 bv->owner()->getMiniBuffer()->ExecCommand();
1068 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1069 unsigned char c = arg[0];
1070 bv->lockedInsetStoreUndo(Undo::INSERT);
1072 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1074 mathcursor->setAccent(0);
1076 if (c == 0) { // Dead key, do nothing
1077 //lyxerr << "deadkey" << endl;
1081 if (mathcursor->getLastCode() == LM_TC_TEX) {
1082 mathcursor->MacroModeOpen();
1083 mathcursor->clearLastCode();
1084 varcode = LM_TC_MIN;
1087 short f = (mathcursor->getLastCode()) ?
1088 mathcursor->getLastCode() :
1089 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1090 varcode = MathIsAlphaFont(f) ?
1091 static_cast<MathedTextCodes>(f) :
1095 // lyxerr << "Varcode << vardoce;
1096 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1097 varcode = LM_TC_MIN;
1098 if (greek_kb_flag<2) greek_kb_flag = 0;
1100 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1101 mathcursor->Insert(c, LM_TC_TEX);
1103 mathcursor->Insert('}', LM_TC_TEX);
1106 mathcursor->clearLastCode();
1107 // varcode = LM_TC_MIN;
1109 if (c == '_' && varcode == LM_TC_TEX) {
1110 mathcursor->Insert(c, LM_TC_SPECIAL);
1111 mathcursor->clearLastCode();
1112 // varcode = LM_TC_MIN;
1114 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1115 mathcursor->MacroModeOpen();
1116 mathcursor->clearLastCode();
1117 mathcursor->Insert(c, LM_TC_MIN);
1120 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1121 mathcursor->Insert(c, LM_TC_CONST);
1123 if (strchr("+/-*<>=", c))
1124 mathcursor->Insert(c, LM_TC_BOP);
1126 if (strchr(latex_special_chars, c) && c!= '_')
1127 mathcursor->Insert(c, LM_TC_SPECIAL);
1129 if (c == '_' || c == '^') {
1133 mathcursor->Interpret (s);
1137 short f = (mathcursor->getLastCode()) ?
1138 mathcursor->getLastCode() :
1139 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1140 varcode = MathIsAlphaFont(f) ?
1141 static_cast<MathedTextCodes>(f) :
1144 if (varcode == LM_TC_TEXTRM) {
1145 mathcursor->Insert(c, LM_TC_TEXTRM);
1148 mathcursor->MacroModeClose();
1151 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1155 if (!mathcursor->Pop() && mathcursor->IsEnd())
1160 mathcursor->Insert (c, LM_TC_VAR);
1164 mathcursor->MacroModeClose();
1165 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1166 mathcursor->setLastCode(LM_TC_TEX);
1170 // lyxerr << "Closed by action " << action << endl;
1174 if (was_macro != mathcursor->InMacroMode()
1176 && action != LFUN_BACKSPACE)
1178 if (sp && !space_on) sp = 0;
1179 if (mathcursor->Selection() || was_selection)
1180 ToggleInsetSelection(bv);
1182 if (result == DISPATCHED)
1183 ShowInsetCursor(bv);
1185 bv->unlockInset(this);
1192 MathFuncInset::draw(Painter & pain, int x, int y)
1194 if (name && name[0] > ' ') {
1195 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1196 font.setLatex(LyXFont::ON);
1197 x += (font.textWidth("I", 1) + 3) / 4;
1198 pain.text(x, y, name, font);
1203 void MathFuncInset::Metrics()
1205 ln = (name) ? strlen(name): 0;
1206 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1207 font.setLatex(LyXFont::ON);
1208 width = font.textWidth(name, ln) + font.textWidth("I", 1)/2;
1209 mathed_string_height(LM_TC_TEXTRM, size,
1210 reinterpret_cast<unsigned char const *>(name),
1211 strlen(name), ascent, descent);
1216 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1218 MathedIter it(par->GetData());
1220 while (it.OK() && !(features.binom && features.boldsymbol)) {
1223 MathParInset * p = it.GetActiveInset();
1224 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1225 strcmp(p->GetName(), "binom") == 0) {
1226 features.binom = true;
1228 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1229 p->setArgumentIdx(i);
1230 mathedValidate(features, p);
1234 MathedInset* p = it.GetInset();
1235 if (!features.boldsymbol && p->GetName() &&
1236 strcmp(p->GetName(), "boldsymbol") == 0) {
1237 features.boldsymbol = true;