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"
31 #include "minibuffer.h"
32 #include "BufferView.h"
33 #include "lyxscreen.h"
37 #include "LaTeXFeatures.h"
39 #include "lyx_gui_misc.h"
40 #include "support/LOstream.h"
43 extern void UpdateInset(Inset * inset, bool mark_dirty = true);
45 extern GC canvasGC, mathGC, mathLineGC, latexGC, cursorGC, mathFrameGC;
46 extern char * mathed_label;
48 extern int mono_video;
49 extern int fast_selection;
51 extern BufferView * current_view;
52 extern char const * latex_special_chars;
54 short greek_kb_flag = 0;
56 LyXFont * Math_Fonts = 0; // this is only used by Whichfont and mathed_init_fonts (Lgb)
58 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
61 static int sel_x, sel_y;
63 MathedCursor * InsetFormula::mathcursor = 0;
66 int MathedInset::df_asc;
67 int MathedInset::df_des;
68 int MathedInset::df_width;
71 inline bool IsMacro(short token, int id)
73 return (token != LM_TK_FRAC && token != LM_TK_SQRT &&
74 !((token == LM_TK_SYM || token == LM_TC_BSYM) && id < 255));
79 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
82 LyXFont WhichFont(short type, int size)
112 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
122 f.setSize(lfont_size);
126 if (type == LM_TC_BSYM) {
136 case LM_ST_SCRIPTSCRIPT:
141 lyxerr << "Mathed Error: wrong font size: " << size << endl;
145 if (type != LM_TC_TEXTRM)
146 f.setColor(LyXFont::MATH);
151 void mathed_init_fonts() //removed 'static' because DEC cxx does not
153 // Probably because this func is declared as a friend in math_defs.h
157 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
159 for (int i = 0 ; i < 8 ; ++i){
160 Math_Fonts[i] = LyXFont::ALL_SANE;
162 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
164 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
166 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
167 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
169 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
171 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
172 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
174 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
176 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
178 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
180 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
181 MathedInset::df_asc = f.maxAscent();
182 MathedInset::df_des = f.maxDescent();
183 MathedInset::df_width = f.width('I');
187 void mathed_set_font(short type, int size)
190 cursorGC = getGC(gc_thin_on_off_line);
191 canvasGC = getGC(gc_lighted);
192 latexGC = getGC(gc_latex);
193 mathLineGC = getGC(gc_math);
194 mathFrameGC = getGC(gc_math_frame);
196 LyXFont f = WhichFont(type, size);
197 if (type == LM_TC_TEX) {
198 f.setLatex(LyXFont::ON);
205 int mathed_string_width(short type, int size, byte const * s, int ls)
207 LyXFont f = WhichFont(type, size);
210 if (MathIsBinary(type)) {
212 for (int i = 0; i < ls && i < 75; ++i) {
221 return f.textWidth(reinterpret_cast<char const *>(s), ls);
225 int mathed_char_width(short type, int size, byte c)
227 int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1):
228 WhichFont(type, size).width(c);
233 int mathed_string_height(short type, int size, byte const * s,
234 int ls, int & asc, int & des)
236 LyXFont font = WhichFont(type, size);
238 for (int i = 0; i < ls; ++i) {
239 if (font.descent(s[i]) > des)
240 des = font.descent(s[i]);
241 if (font.ascent(s[i]) > asc)
242 asc = font.ascent(s[i]);
248 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
250 LyXFont font = WhichFont(type, size);
252 des = font.descent(c);
253 asc = font.ascent(c);
258 // In a near future maybe we use a better fonts renderer
259 void MathedInset::drawStr(short type, int siz, int x, int y, byte * s, int ls)
261 mathed_set_font(type, siz);
263 if (MathIsBinary(type)) {
265 for (int i = 0; i < ls && i < 75; ++i) {
274 GC gc = (type == LM_TC_TEX) ? latexGC: mathGC;
275 XDrawString(fl_display, pm, gc, x, y, reinterpret_cast<char*>(s), ls);
280 InsetFormula::InsetFormula(bool display)
282 par = new MathParInset; // this leaks
287 par->SetType(LM_OT_PAR);
288 par->SetStyle(LM_ST_DISPLAY);
293 InsetFormula::InsetFormula(MathParInset * p)
295 par = (p->GetType()>= LM_OT_MPAR) ?
296 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
300 disp_flag = (par->GetType()>0);
305 InsetFormula::~InsetFormula()
311 Inset * InsetFormula::Clone() const
313 InsetFormula * f = new InsetFormula(par);
319 void InsetFormula::Write(ostream & os)
326 int InsetFormula::Latex(ostream & os, signed char fragile)
329 //#warning Alejandro, the number of lines is not returned in this case
330 // This problem will disapear at 0.13.
332 InsetFormula::Latex(output, fragile);
338 int InsetFormula::Latex(string & file, signed char fragile)
341 //#warning Alejandro, the number of lines is not returned in this case
342 // This problem will disapear at 0.13.
346 mathed_write(par, file, &ret, fragile, label.c_str());
351 int InsetFormula::Linuxdoc(string &/*file*/)
357 int InsetFormula::DocBook(string &/*file*/)
363 // Check if uses AMS macros
364 void InsetFormula::Validate(LaTeXFeatures & features) const
366 // Validation only necesary if not using an AMS Style
367 if (!features.amsstyle)
368 mathedValidate(features, par);
372 void InsetFormula::Read(LyXLex & lex)
374 istream & is = lex.getStream();
376 mathed_parser_file(is, lex.GetLineNo());
378 // Silly hack to read labels.
380 mathed_parse(0, 0, &par);
382 disp_flag = (par->GetType() > 0);
384 // Update line number
385 lex.setLineNo(mathed_parser_lineno());
388 label = mathed_label;
398 int InsetFormula::Ascent(LyXFont const &) const
400 return par->Ascent() + ((disp_flag) ? 8 : 1);
404 int InsetFormula::Descent(LyXFont const &) const
406 return par->Descent() + ((disp_flag) ? 8 : 1);
410 int InsetFormula::Width(LyXFont const & f) const
412 lfont_size = f.size();
414 return par->Width(); //+2;
418 void InsetFormula::Draw(LyXFont f, LyXScreen & scr, int baseline, float & x)
420 // This is Alejandros domain so I'll use this
421 unsigned long pm = scr.getForeground();
423 lfont_size = f.size();
424 mathed_set_font(LM_TC_TEXTRM, LM_ST_TEXT); // otherwise a segfault could occur
425 // in some XDrawRectangles (i.e. matrix) (Matthias)
426 if (mathcursor && mathcursor->GetPar() == par) {
427 if (mathcursor->Selection()) {
429 XPoint * p = mathcursor->SelGetArea(n);
430 XFillPolygon(fl_display, pm, getGC(gc_selection),
431 p, n, Nonconvex, CoordModeOrigin);
433 mathcursor->Draw(pm, int(x), baseline);
436 par->setDrawable(pm);
437 par->Draw(int(x), baseline);
439 x += float(Width(f));
441 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
443 LyXFont font = WhichFont(LM_TC_BF, par->size);
444 font.setLatex(LyXFont::OFF);
446 if (par->GetType() == LM_OT_PARN) {
448 sprintf(s, "(%s)", label.c_str());
451 font.drawString(s, pm, baseline, int(x+20));
453 if (par->GetType() == LM_OT_MPARN) {
454 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
456 MathedRowSt const* crow = mt->getRowSt();
458 y = baseline + crow->getBaseline();
459 if (crow->isNumbered()) {
460 if (crow->getLabel())
461 sprintf(s, "(%s)", crow->getLabel());
464 font.drawString(s, pm, y, int(x+20));
466 crow = crow->getNext();
470 cursor_visible = false;
474 void InsetFormula::Edit(int x, int y)
476 mathcursor = new MathedCursor(par);
477 current_view->lockInset(this);
479 UpdateInset(this, false);
482 mathcursor->SetPos(x, y);
488 void InsetFormula::InsetUnlock()
491 if (mathcursor->InMacroMode()) {
492 mathcursor->MacroModeClose();
498 UpdateInset(this, false);
502 // Now a symbol can be inserted only if the inset is locked
503 void InsetFormula::InsertSymbol(char const * s)
505 if (!s || !mathcursor) return;
506 mathcursor->Interpret(s);
511 void InsetFormula::GetCursorPos(int& x, int& y) const
513 mathcursor->GetPos(x, y);
518 void InsetFormula::ToggleInsetCursor()
524 mathcursor->GetPos(x, y);
527 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
528 asc = font.maxAscent();
529 desc = font.maxDescent();
532 current_view->hideLockedInsetCursor(x, y, asc, desc);
534 current_view->showLockedInsetCursor(x, y, asc, desc);
535 cursor_visible = !cursor_visible;
539 void InsetFormula::ShowInsetCursor()
541 if (!cursor_visible) {
544 mathcursor->GetPos(x, y);
547 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
548 asc = font.maxAscent();
549 desc = font.maxDescent();
550 current_view->fitLockedInsetCursor(x, y, asc, desc);
557 void InsetFormula::HideInsetCursor()
564 void InsetFormula::ToggleInsetSelection()
572 //mathcursor->SelGetArea(n);
573 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
577 UpdateInset(this, false);
582 void InsetFormula::display(bool dspf)
584 if (dspf != disp_flag) {
586 par->SetType(LM_OT_PAR);
587 par->SetStyle(LM_ST_DISPLAY);
589 if (par->GetType() >= LM_OT_MPAR) {
590 MathParInset * p = new MathParInset(par);
594 mathcursor->SetPar(par);
596 par->SetType(LM_OT_MIN);
597 par->SetStyle(LM_ST_TEXT);
598 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
607 int InsetFormula::GetNumberOfLabels() const
609 // This is dirty, I know. I'll clean it at 0.13
610 if (par->GetType() == LM_OT_MPARN) {
611 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
613 MathedRowSt const * crow = mt->getRowSt();
615 if (crow->getLabel()) ++nl;
616 crow = crow->getNext();
627 string InsetFormula::getLabel(int il) const
629 //#warning This is dirty, I know. Ill clean it at 0.11
630 // Correction, the only way to clean this is with a new kernel: 0.13.
631 if (par->GetType() == LM_OT_MPARN) {
633 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
635 MathedRowSt const * crow = mt->getRowSt();
637 if (crow->getLabel()) {
639 lab = crow->getLabel();
644 crow = crow->getNext();
652 void InsetFormula::UpdateLocal()
654 par->Metrics(); // To inform lyx kernel the exact size
655 // (there were problems with arrays).
660 void InsetFormula::InsetButtonRelease(int x, int y, int /*button*/)
665 mathcursor->SetPos(x, y);
670 UpdateInset(this, false);
675 void InsetFormula::InsetButtonPress(int x, int y, int /*button*/)
678 sel_x = x; sel_y = y;
679 if (mathcursor->Selection()) {
680 mathcursor->SelClear();
681 UpdateInset(this, false);
686 void InsetFormula::InsetMotionNotify(int x, int y, int /*button*/)
688 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
691 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
692 mathcursor->SelStart();
694 mathcursor->GetPos(sel_x, sel_y);
700 mathcursor->SetPos(x, y);
702 mathcursor->GetPos(x, y);
703 if (sel_x!= x || sel_y!= y)
704 UpdateInset(this, false);
705 sel_x = x; sel_y = y;
710 void InsetFormula::InsetKeyPress(XKeyEvent *)
712 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
716 // Special Mathed functions
717 bool InsetFormula::SetNumber(bool numbf)
720 short type = par->GetType();
721 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
722 if (numbf && !oldf) ++type;
723 if (!numbf && oldf) --type;
731 bool InsetFormula::LocalDispatch(int action, char const * arg)
733 // extern char *dispatch_result;
734 MathedTextCodes varcode = LM_TC_MIN;
735 bool was_macro = mathcursor->InMacroMode();
737 bool space_on = false;
738 bool was_selection = mathcursor->Selection();
740 static MathSpaceInset * sp= 0;
743 if (mathcursor->Selection() && (fast_selection || mono_video))
744 ToggleInsetSelection();
746 if (mathcursor->getLastCode() == LM_TC_TEX) {
751 // --- Cursor Movements ---------------------------------------------
752 case LFUN_RIGHTSEL: sel = true;
755 result = mathcursor->Right(sel);
758 case LFUN_LEFTSEL: sel = true;
761 result = mathcursor->Left(sel);
764 case LFUN_UPSEL: sel = true;
766 result = mathcursor->Up(sel);
769 case LFUN_DOWNSEL: sel = true;
771 result = mathcursor->Down(sel);
779 case LFUN_DELETE_LINE_FORWARD:
780 //current_view->lockedInsetStoreUndo(Undo::INSERT);
781 current_view->lockedInsetStoreUndo(Undo::DELETE);
782 mathcursor->DelLine();
786 current_view->lockedInsetStoreUndo(Undo::INSERT);
787 mathcursor->Insert(' ', LM_TC_CR);
788 par = mathcursor->GetPar();
792 current_view->lockedInsetStoreUndo(Undo::INSERT);
793 mathcursor->Insert(0, LM_TC_TAB);
797 current_view->lockedInsetStoreUndo(Undo::INSERT);
798 mathcursor->Insert('T', LM_TC_TAB);
802 if (!mathcursor->Left())
805 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
811 //current_view->lockedInsetStoreUndo(Undo::INSERT);
812 current_view->lockedInsetStoreUndo(Undo::DELETE);
813 mathcursor->Delete();
817 // sprintf(dispatch_buffer, "%d %d",);
818 // dispatch_result = dispatch_buffer;
823 sscanf(arg, "%d %d", &x, &y);
825 mathcursor->SetPos(x1+x, y1+y);
829 /* cursor selection ---------------------------- */
833 mathcursor->MacroModeClose();
834 current_view->lockedInsetStoreUndo(Undo::INSERT);
835 mathcursor->SelPaste(); UpdateLocal(); break;
837 current_view->lockedInsetStoreUndo(Undo::DELETE);
838 mathcursor->SelCut(); UpdateLocal(); break;
839 case LFUN_COPY: mathcursor->SelCopy(); break;
842 case LFUN_WORDRIGHTSEL:
843 case LFUN_WORDLEFTSEL:
846 // --- accented characters ------------------------------
848 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
849 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
850 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
851 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
852 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
853 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
854 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
855 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
856 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
857 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
862 if (!greek_kb_flag) {
864 current_view->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
871 case LFUN_GREEK_TOGGLE:
873 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
875 current_view->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
877 current_view->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
882 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
883 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
884 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
885 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
886 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
887 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
890 // varcode = LM_TC_TEX;
891 mathcursor->setLastCode(LM_TC_TEX);
892 current_view->owner()->getMiniBuffer()->Set(_("TeX mode"));
896 case LFUN_MATH_NUMBER:
898 current_view->lockedInsetStoreUndo(Undo::INSERT);
900 short type = par->GetType();
901 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
904 if (!label.empty()) {
907 current_view->owner()->getMiniBuffer()->Set(_("No number"));
910 current_view->owner()->getMiniBuffer()->Set(_("Number"));
918 case LFUN_MATH_NONUMBER:
920 if (par->GetType() == LM_OT_MPARN) {
921 // MathMatrixInset *mt = (MathMatrixInset*)par;
923 // mt->SetNumbered(!mt->IsNumbered());
925 mathcursor->setNumbered();
931 case LFUN_MATH_LIMITS:
933 current_view->lockedInsetStoreUndo(Undo::INSERT);
934 if (mathcursor->Limits())
940 latexkeys * l = in_word_set (arg, strlen(arg));
941 int sz = (l) ? l->id: -1;
942 mathcursor->SetSize(sz);
947 case LFUN_INSERT_MATH:
949 current_view->lockedInsetStoreUndo(Undo::INSERT);
954 case LFUN_INSERT_MATRIX:
956 current_view->lockedInsetStoreUndo(Undo::INSERT);
958 char s[80], arg2[80];
959 // This is just so that too long args won't ooze out of s.
960 strncpy(arg2, arg, 80); arg2[79]= '\0';
961 k = sscanf(arg2, "%d %d %s", &m, &n, s);
970 MathMatrixInset * p = new MathMatrixInset(m, n);
971 if (mathcursor && p) {
972 if (k > 2 && int(strlen(s)) > m)
973 p->SetAlign(s[0], &s[1]);
974 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
980 case LFUN_MATH_DELIM:
982 current_view->lockedInsetStoreUndo(Undo::INSERT);
983 char lf[40], rg[40], arg2[40];
984 int ilf = '(', irg = '.';
986 string vdelim("(){}[]./|");
989 strncpy(arg2, arg, 40); arg2[39]= '\0';
990 int n = sscanf(arg2, "%s %s", lf, rg);
991 lf[39] = '\0'; rg[39] = '\0';
998 l = in_word_set(lf, strlen(lf));
999 // Long words will cause l == 0; so check.
1002 if (vdelim.find(lf[0]) != string::npos)
1010 l = in_word_set(rg, strlen(rg));
1013 if (vdelim.find(rg[0]) != string::npos)
1018 MathDelimInset * p = new MathDelimInset(ilf, irg);
1019 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1024 case LFUN_PROTECTEDSPACE:
1026 current_view->lockedInsetStoreUndo(Undo::INSERT);
1027 sp = new MathSpaceInset(1);
1028 mathcursor->Insert(sp);
1034 case LFUN_INSERT_LABEL:
1036 current_view->lockedInsetStoreUndo(Undo::INSERT);
1037 if (par->GetType() < LM_OT_PAR) break;
1041 result = askForText(_("Enter new label to insert:"));
1046 if (!lb.empty() && lb[0] > ' ') {
1048 if (par->GetType() == LM_OT_MPARN) {
1049 mathcursor->setLabel(lb.c_str());
1050 // MathMatrixInset *mt = (MathMatrixInset*)par;
1051 // mt->SetLabel(lb);
1053 //if (label.notEmpty()) delete label;
1062 case LFUN_MATH_DISPLAY:
1063 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1064 current_view->lockedInsetStoreUndo(Undo::EDIT);
1065 display(!disp_flag);
1069 // Invalid actions under math mode
1070 case LFUN_MATH_MODE:
1072 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1073 current_view->owner()->getMiniBuffer()->Set(_("math text mode"));
1074 varcode = LM_TC_TEXTRM;
1076 varcode = LM_TC_VAR;
1078 mathcursor->setLastCode(varcode);
1082 current_view->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1085 //------- dummy actions
1086 case LFUN_EXEC_COMMAND:
1087 current_view->owner()->getMiniBuffer()->ExecCommand();
1091 if ((action == -1 || action == LFUN_SELFINSERT) && arg) {
1092 unsigned char c = arg[0];
1093 current_view->lockedInsetStoreUndo(Undo::INSERT);
1095 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1097 mathcursor->setAccent(0);
1099 if (c == 0) { // Dead key, do nothing
1100 //lyxerr << "deadkey" << endl;
1104 if (mathcursor->getLastCode() == LM_TC_TEX) {
1105 mathcursor->MacroModeOpen();
1106 mathcursor->clearLastCode();
1107 varcode = LM_TC_MIN;
1110 short f = (mathcursor->getLastCode()) ?
1111 mathcursor->getLastCode() :
1112 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1113 varcode = MathIsAlphaFont(f) ?
1114 static_cast<MathedTextCodes>(f) :
1118 // lyxerr << "Varcode << vardoce;
1119 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1120 varcode = LM_TC_MIN;
1121 if (greek_kb_flag<2) greek_kb_flag = 0;
1123 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1124 mathcursor->Insert(c, LM_TC_TEX);
1126 mathcursor->Insert('}', LM_TC_TEX);
1129 mathcursor->clearLastCode();
1130 // varcode = LM_TC_MIN;
1132 if (c == '_' && varcode == LM_TC_TEX) {
1133 mathcursor->Insert(c, LM_TC_SPECIAL);
1134 mathcursor->clearLastCode();
1135 // varcode = LM_TC_MIN;
1137 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1138 mathcursor->MacroModeOpen();
1139 mathcursor->clearLastCode();
1140 mathcursor->Insert(c, LM_TC_MIN);
1143 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1144 mathcursor->Insert(c, LM_TC_CONST);
1146 if (strchr("+/-*<>=", c))
1147 mathcursor->Insert(c, LM_TC_BOP);
1149 if (strchr(latex_special_chars, c) && c!= '_')
1150 mathcursor->Insert(c, LM_TC_SPECIAL);
1152 if (c == '_' || c == '^') {
1156 mathcursor->Interpret (s);
1160 short f = (mathcursor->getLastCode()) ?
1161 mathcursor->getLastCode() :
1162 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1163 varcode = MathIsAlphaFont(f) ?
1164 static_cast<MathedTextCodes>(f) :
1167 if (varcode == LM_TC_TEXTRM) {
1168 mathcursor->Insert(c, LM_TC_TEXTRM);
1171 mathcursor->MacroModeClose();
1174 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1178 if (!mathcursor->Pop() && mathcursor->IsEnd())
1183 mathcursor->Insert (c, LM_TC_VAR);
1187 mathcursor->MacroModeClose();
1188 current_view->owner()->getMiniBuffer()->Set(_("TeX mode"));
1189 mathcursor->setLastCode(LM_TC_TEX);
1193 // lyxerr << "Closed by action " << action << endl;
1197 if (was_macro != mathcursor->InMacroMode()
1199 && action != LFUN_BACKSPACE)
1201 if (sp && !space_on) sp = 0;
1202 if (mathcursor->Selection() || (was_selection && !(fast_selection || mono_video)))
1203 ToggleInsetSelection();
1208 current_view->unlockInset(this);
1215 MathFuncInset::Draw(int x, int y)
1217 if (name && name[0] > ' ') {
1218 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1219 font.setLatex(LyXFont::ON);
1220 x += (font.textWidth("I", 1)+3)/4;
1222 int a = font.maxAscent(), d = font.maxDescent();
1223 XFillRectangle (fl_display, pm, getGC(gc_copy),
1225 font.textWidth(name, strlen(name)), a+d);
1228 font.drawString(name, pm, y, x);
1233 void MathFuncInset::Metrics()
1235 ln = (name) ? strlen(name): 0;
1236 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1237 font.setLatex(LyXFont::ON);
1238 width = font.textWidth(name, ln) + font.textWidth("I", 1)/2;
1239 mathed_string_height(LM_TC_TEXTRM, size,
1240 reinterpret_cast<unsigned char const *>(name),
1241 strlen(name), ascent, descent);
1246 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1248 MathedIter it(par->GetData());
1250 while (it.OK() && !(features.binom && features.boldsymbol)) {
1253 MathParInset * p = it.GetActiveInset();
1254 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1255 strcmp(p->GetName(), "binom") == 0) {
1256 features.binom = true;
1258 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1259 p->setArgumentIdx(i);
1260 mathedValidate(features, p);
1264 MathedInset* p = it.GetInset();
1265 if (!features.boldsymbol && p->GetName() &&
1266 strcmp(p->GetName(), "boldsymbol") == 0) {
1267 features.boldsymbol = true;