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"
30 #include "bufferlist.h"
32 #include "minibuffer.h"
33 #include "BufferView.h"
34 #include "lyxscreen.h"
38 #include "LaTeXFeatures.h"
40 #include "lyx_gui_misc.h"
42 extern void UpdateInset(Inset* inset, bool mark_dirty = true);
43 extern void LockedInsetStoreUndo(Undo::undo_kind);
44 extern MiniBuffer *minibuffer;
45 extern void ShowLockedInsetCursor(long, long, int, int);
46 extern void HideLockedInsetCursor(long, long, int, int);
47 extern void FitLockedInsetCursor(long, long, int, int);
48 extern int LockInset(UpdatableInset*);
49 extern int UnlockInset(UpdatableInset*);
52 extern GC canvasGC, mathGC, mathLineGC, latexGC, cursorGC, mathFrameGC;
53 extern char *mathed_label;
55 extern int mono_video;
56 extern int fast_selection;
58 extern BufferView *current_view;
59 extern BufferList bufferlist;
60 extern char const *latex_special_chars;
62 short greek_kb_flag = 0;
64 LyXFont *Math_Fonts = 0; // this is only used by Whichfont and mathed_init_fonts (Lgb)
66 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
69 static int sel_x, sel_y;
71 MathedCursor* InsetFormula::mathcursor = 0;
74 int MathedInset::df_asc;
75 int MathedInset::df_des;
76 int MathedInset::df_width;
78 // wrong name on this one should be called "IsAscii"
79 inline bool IsAlpha(char c)
81 return ('A' <= c && c<= 'Z' || 'a' <= c && c<= 'z');
84 inline bool IsDigit(char c)
86 return ('0' <= c && c <= '9');
89 inline bool IsMacro(short token, int id)
91 return (token!= LM_TK_FRAC && token!= LM_TK_SQRT &&
92 !((token == LM_TK_SYM || token == LM_TC_BSYM) && id<255));
95 void mathedValidate(LaTeXFeatures &features, MathParInset *par);
97 LyXFont WhichFont(short type, int size)
127 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
137 f.setSize(lfont_size);
141 if (type == LM_TC_BSYM) {
151 case LM_ST_SCRIPTSCRIPT:
156 lyxerr << "Mathed Error: wrong font size: " << size << endl;
160 if (type!= LM_TC_TEXTRM)
161 f.setColor(LyXFont::MATH);
166 void mathed_init_fonts() //removed 'static' because DEC cxx does not
170 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
172 for (int i= 0 ; i<8 ; i++){
173 Math_Fonts[i] = LyXFont::ALL_SANE;
175 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
177 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
179 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
180 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
182 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
184 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
185 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
187 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
189 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
191 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
193 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
194 MathedInset::df_asc = f.maxAscent();
195 MathedInset::df_des = f.maxDescent();
196 MathedInset::df_width = f.width('I');
200 void mathed_set_font(short type, int size)
203 cursorGC = getGC(gc_thin_on_off_line);
204 canvasGC = getGC(gc_lighted);
205 latexGC = getGC(gc_latex);
206 mathLineGC = getGC(gc_math);
207 mathFrameGC = getGC(gc_math_frame);
209 LyXFont f = WhichFont(type, size);
210 if (type == LM_TC_TEX) {
211 f.setLatex(LyXFont::ON);
218 int mathed_string_width(short type, int size, byte const* s, int ls)
220 LyXFont f = WhichFont(type, size);
223 if (MathIsBinary(type)) {
225 for (int i= 0; i<ls && i<75; i++) {
234 return f.textWidth((const char*)s, ls);;
237 int mathed_char_width(short type, int size, byte c)
239 int t= (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1):
240 WhichFont(type, size).width(c);
244 int mathed_string_height(short type, int size, byte const * s, int ls, int& asc, int& des)
246 LyXFont font = WhichFont(type, size);
248 for (int i= 0; i<ls; i++) {
249 if (font.descent(s[i]) > des)
250 des = font.descent(s[i]);
251 if (font.ascent(s[i]) > asc)
252 asc = font.ascent(s[i]);
257 int mathed_char_height(short type, int size, byte c, int& asc, int& des)
259 LyXFont font = WhichFont(type, size);
261 des = font.descent(c);
262 asc = font.ascent(c);
267 // In a near future maybe we use a better fonts renderer
268 void MathedInset::drawStr(short type, int siz, int x, int y, byte* s, int ls)
270 mathed_set_font(type, siz);
272 if (MathIsBinary(type)) {
274 for (int i= 0; i<ls && i < 75; i++) {
283 GC gc = (type == LM_TC_TEX) ? latexGC: mathGC;
284 XDrawString(fl_display, pm, gc, x, y, reinterpret_cast<char*>(s), ls);
289 InsetFormula::InsetFormula(bool display)
291 par = new MathParInset; // this leaks
296 par->SetType(LM_OT_PAR);
297 par->SetStyle(LM_ST_DISPLAY);
301 InsetFormula::InsetFormula(MathParInset *p)
303 par = (p->GetType()>= LM_OT_MPAR) ?
304 new MathMatrixInset((MathMatrixInset*)p):
308 disp_flag = (par->GetType()>0);
312 InsetFormula::~InsetFormula()
315 //if (label) delete label;
318 InsetFormula * InsetFormula::Clone() const
320 InsetFormula * f = new InsetFormula(par);
325 void InsetFormula::Write(FILE *file)
327 fprintf(file, "Formula ");
331 int InsetFormula::Latex(FILE *file, signed char fragile)
334 //#warning Alejandro, the number of lines is not returned in this case
335 // This problem will disapear at 0.13.
337 InsetFormula::Latex(output, fragile);
338 fprintf(file, "%s", output.c_str());
342 int InsetFormula::Latex(string &file, signed char fragile)
345 //#warning Alejandro, the number of lines is not returned in this case
346 // This problem will disapear at 0.13.
350 mathed_write(par, file, &ret, fragile, label.c_str());
355 int InsetFormula::Linuxdoc(string &/*file*/)
361 int InsetFormula::DocBook(string &/*file*/)
367 // Check if uses AMS macros
368 void InsetFormula::Validate(LaTeXFeatures &features) const
370 // Validation only necesary if not using an AMS Style
371 if (!features.amsstyle)
372 mathedValidate(features, par);
376 void InsetFormula::Read(LyXLex &lex)
378 FILE *file = lex.getFile();
380 mathed_parser_file(file, lex.GetLineNo());
382 // Silly hack to read labels.
384 mathed_parse(0, 0, &par);
386 disp_flag = (par->GetType()>0);
388 // Update line number
389 lex.setLineNo(mathed_parser_lineno());
392 label = mathed_label;
402 int InsetFormula::Ascent(LyXFont const &) const
404 return par->Ascent() + ((disp_flag) ? 8: 1);
407 int InsetFormula::Descent(LyXFont const &) const
409 return par->Descent() + ((disp_flag) ? 8: 1);
412 int InsetFormula::Width(LyXFont const &f) const
414 lfont_size = f.size();
416 return par->Width(); //+2;
419 void InsetFormula::Draw(LyXFont f, LyXScreen &scr, int baseline, float &x)
421 // This is Alejandros domain so I'll use this
422 unsigned long pm = scr.getForeground();
424 lfont_size = f.size();
425 mathed_set_font(LM_TC_TEXTRM, LM_ST_TEXT); // otherwise a segfault could occur
426 // in some XDrawRectangles (i.e. matrix) (Matthias)
427 if (mathcursor && mathcursor->GetPar() == par) {
428 if (mathcursor->Selection()) {
430 XPoint * p = mathcursor->SelGetArea(n);
431 XFillPolygon(fl_display, pm, getGC(gc_selection), //LyXGetSelectionGC(),
432 p, n, Nonconvex, CoordModeOrigin);
434 mathcursor->Draw(pm, (int)x, baseline);
437 par->setDrawable(pm);
438 par->Draw((int)x, baseline);
440 x += (float)Width(f);
442 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
444 LyXFont font = WhichFont(LM_TC_BF, par->size);
445 font.setLatex(LyXFont::OFF);
447 if (par->GetType() == LM_OT_PARN) {
449 sprintf(s, "(%s)", label.c_str());
452 font.drawString(s, pm, baseline, int(x+20));
454 if (par->GetType() == LM_OT_MPARN) {
455 MathMatrixInset *mt = (MathMatrixInset*)par;
458 MathedRowSt const* crow = mt->getRowSt();
460 y = baseline + crow->getBaseline();
461 if (crow->isNumbered()) {
462 if (crow->getLabel())
463 sprintf(s, "(%s)", crow->getLabel());
466 font.drawString(s, pm, y, int(x+20));
468 crow = crow->getNext();
472 cursor_visible = false;
476 void InsetFormula::Edit(int x, int y)
478 mathcursor = new MathedCursor(par);
481 UpdateInset(this, false);
484 mathcursor->SetPos(x, y);
489 void InsetFormula::InsetUnlock()
492 if (mathcursor->InMacroMode()) {
493 mathcursor->MacroModeClose();
499 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);
510 void InsetFormula::GetCursorPos(int& x, int& y)
512 mathcursor->GetPos(x, y);
517 void InsetFormula::ToggleInsetCursor()
523 mathcursor->GetPos(x, y);
526 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
527 asc = font.maxAscent();
528 desc = font.maxDescent();
531 HideLockedInsetCursor(x, y, asc, desc);
533 ShowLockedInsetCursor(x, y, asc, desc);
534 cursor_visible = !cursor_visible;
537 void InsetFormula::ShowInsetCursor(){
538 if (!cursor_visible){
541 mathcursor->GetPos(x, y);
544 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
545 asc = font.maxAscent();
546 desc = font.maxDescent();
547 FitLockedInsetCursor(x, y, asc, desc);
553 void InsetFormula::HideInsetCursor(){
558 void InsetFormula::ToggleInsetSelection()
566 //mathcursor->SelGetArea(n);
567 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
571 UpdateInset(this, false);
575 void InsetFormula::display(bool dspf)
577 if (dspf!= disp_flag) {
579 par->SetType(LM_OT_PAR);
580 par->SetStyle(LM_ST_DISPLAY);
582 if (par->GetType()>= LM_OT_MPAR) {
583 MathParInset * p = new MathParInset(par);
587 mathcursor->SetPar(par);
589 par->SetType(LM_OT_MIN);
590 par->SetStyle(LM_ST_TEXT);
591 if (!label.empty() && par->GetType()!= LM_OT_MPARN) {
601 int InsetFormula::GetNumberOfLabels() const
603 // This is dirty, I know. I'll clean it at 0.13
604 if (par->GetType() == LM_OT_MPARN) {
605 MathMatrixInset * mt = (MathMatrixInset*)par;
607 MathedRowSt const * crow = mt->getRowSt();
609 if (crow->getLabel()) nl++;
610 crow = crow->getNext();
621 string InsetFormula::getLabel(int il) const
623 //#warning This is dirty, I know. Ill clean it at 0.11
624 // Correction, the only way to clean this is with a new kernel: 0.13.
625 if (par->GetType() == LM_OT_MPARN) {
627 MathMatrixInset * mt = (MathMatrixInset*)par;
629 MathedRowSt const * crow = mt->getRowSt();
631 if (crow->getLabel()) {
633 lab = crow->getLabel();
638 crow = crow->getNext();
645 void InsetFormula::UpdateLocal()
647 par->Metrics(); // To inform lyx kernel the exact size
648 // (there were problems with arrays).
654 void InsetFormula::InsetButtonRelease(int x, int y, int /*button*/)
659 mathcursor->SetPos(x, y);
664 UpdateInset(this, false);
668 void InsetFormula::InsetButtonPress(int x, int y, int /*button*/)
671 sel_x = x; sel_y = y;
672 if (mathcursor->Selection()) {
673 mathcursor->SelClear();
674 UpdateInset(this, false);
678 void InsetFormula::InsetMotionNotify(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 UpdateInset(this, false);
697 sel_x = x; sel_y = y;
701 void InsetFormula::InsetKeyPress(XKeyEvent *)
703 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
706 // Special Mathed functions
707 bool InsetFormula::SetNumber(bool numbf)
710 short type = par->GetType();
711 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
712 if (numbf && !oldf) type++;
713 if (!numbf && oldf) type--;
720 bool InsetFormula::LocalDispatch(int action, char const *arg)
722 // extern char *dispatch_result;
723 MathedTextCodes varcode = LM_TC_MIN;
724 bool was_macro = mathcursor->InMacroMode();
726 bool space_on = false;
727 bool was_selection = mathcursor->Selection();
729 static MathSpaceInset* sp= 0;
732 if (mathcursor->Selection() && (fast_selection || mono_video)) ToggleInsetSelection();
734 if (mathcursor->getLastCode() == LM_TC_TEX) {
739 // --- Cursor Movements ---------------------------------------------
740 case LFUN_RIGHTSEL: sel = true;
743 result = mathcursor->Right(sel);
746 case LFUN_LEFTSEL: sel = true;
749 result = mathcursor->Left(sel);
752 case LFUN_UPSEL: sel = true;
754 result = mathcursor->Up(sel);
757 case LFUN_DOWNSEL: sel = true;
759 result = mathcursor->Down(sel);
767 case LFUN_DELETE_LINE_FORWARD:
768 //LockedInsetStoreUndo(Undo::INSERT);
769 LockedInsetStoreUndo(Undo::DELETE);
770 mathcursor->DelLine();
774 LockedInsetStoreUndo(Undo::INSERT);
775 mathcursor->Insert(' ', LM_TC_CR);
776 par = mathcursor->GetPar();
780 LockedInsetStoreUndo(Undo::INSERT);
781 mathcursor->Insert(0, LM_TC_TAB);
785 LockedInsetStoreUndo(Undo::INSERT);
786 mathcursor->Insert('T', LM_TC_TAB);
790 if (!mathcursor->Left())
793 if (!mathcursor-> InMacroMode() && mathcursor->pullArg()) {
799 //LockedInsetStoreUndo(Undo::INSERT);
800 LockedInsetStoreUndo(Undo::DELETE);
801 mathcursor->Delete();
805 // sprintf(dispatch_buffer, "%d %d",);
806 // dispatch_result = dispatch_buffer;
811 sscanf(arg, "%d %d", &x, &y);
813 mathcursor->SetPos(x1+x, y1+y);
817 /* cursor selection ---------------------------- */
821 mathcursor->MacroModeClose();
822 LockedInsetStoreUndo(Undo::INSERT);
823 mathcursor->SelPaste(); UpdateLocal(); break;
825 LockedInsetStoreUndo(Undo::DELETE);
826 mathcursor->SelCut(); UpdateLocal(); 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 minibuffer->Set(_("Math greek mode on"));
859 case LFUN_GREEK_TOGGLE:
861 greek_kb_flag = (greek_kb_flag) ? 0: 2;
863 minibuffer->Set(_("Math greek keyboard on"));
865 minibuffer->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 minibuffer->Set(_("TeX mode"));
884 case LFUN_MATH_NUMBER:
886 LockedInsetStoreUndo(Undo::INSERT);
888 short type = par->GetType();
889 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
892 if (!label.empty()) {
895 minibuffer->Set(_("No number"));
898 minibuffer->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 LockedInsetStoreUndo(Undo::INSERT);
922 if (mathcursor->Limits())
928 latexkeys *l = in_word_set (arg, strlen(arg));
929 int sz = (l) ? l->id: -1;
930 mathcursor->SetSize(sz);
935 case LFUN_INSERT_MATH:
937 LockedInsetStoreUndo(Undo::INSERT);
942 case LFUN_INSERT_MATRIX:
944 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, 80); arg2[79]= (char)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 LockedInsetStoreUndo(Undo::INSERT);
971 char lf[40], rg[40], arg2[40];
972 int ilf = '(', irg = '.';
974 string vdelim("(){}[]./|");
977 strncpy(arg2, arg, 40); arg2[39]= (char)0;
978 int n = sscanf(arg2, "%s %s", lf, rg);
979 lf[39] = (char)0; rg[39] = (char)0;
986 l = in_word_set(lf, strlen(lf));
987 // Long words will cause l == 0; so check.
990 if (vdelim.find(lf[0]) != string::npos)
998 l = in_word_set(rg, strlen(rg));
1001 if (vdelim.find(rg[0]) != string::npos)
1006 MathDelimInset* p = new MathDelimInset(ilf, irg);
1007 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1012 case LFUN_PROTECTEDSPACE:
1014 LockedInsetStoreUndo(Undo::INSERT);
1015 sp = new MathSpaceInset(1);
1016 mathcursor->Insert(sp);
1022 case LFUN_INSERT_LABEL:
1024 LockedInsetStoreUndo(Undo::INSERT);
1025 if (par->GetType()<LM_OT_PAR) break;
1028 lb = string(askForText(_("Enter new label to insert:")));
1029 if (!lb.empty() && lb[0]> ' ') {
1031 if (par->GetType() == LM_OT_MPARN) {
1032 mathcursor->setLabel(lb.c_str());
1033 // MathMatrixInset *mt = (MathMatrixInset*)par;
1034 // mt->SetLabel(lb);
1036 //if (label.notEmpty()) delete label;
1046 case LFUN_MATH_DISPLAY:
1047 //LockedInsetStoreUndo(Undo::INSERT);
1048 LockedInsetStoreUndo(Undo::EDIT);
1049 display(!disp_flag);
1053 // Invalid actions under math mode
1054 case LFUN_MATH_MODE:
1056 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1057 minibuffer->Set(_("math text mode"));
1058 varcode = LM_TC_TEXTRM;
1060 varcode = LM_TC_VAR;
1062 mathcursor->setLastCode(varcode);
1066 minibuffer->Set(_("Invalid action in math mode!"));
1069 //------- dummy actions
1070 case LFUN_EXEC_COMMAND:
1071 minibuffer->ExecCommand();
1075 if ((action == -1 || action == LFUN_SELFINSERT) && arg) {
1076 unsigned char c = arg[0];
1077 LockedInsetStoreUndo(Undo::INSERT);
1079 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1081 mathcursor->setAccent(0);
1083 if (c == 0) { // Dead key, do nothing
1084 //lyxerr << "deadkey" << endl;
1088 if (mathcursor->getLastCode() == LM_TC_TEX) {
1089 mathcursor->MacroModeOpen();
1090 mathcursor->clearLastCode();
1091 varcode = LM_TC_MIN;
1094 short f = (mathcursor->getLastCode()) ?
1095 mathcursor->getLastCode():
1096 (MathedTextCodes)mathcursor->GetFCode();
1097 varcode = MathIsAlphaFont(f) ? (MathedTextCodes)f:LM_TC_VAR;
1100 // lyxerr << "Varcode << vardoce;
1101 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1102 varcode = LM_TC_MIN;
1103 if (greek_kb_flag<2) greek_kb_flag = 0;
1105 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1106 mathcursor->Insert(c, LM_TC_TEX);
1108 mathcursor->Insert('}', LM_TC_TEX);
1111 mathcursor->clearLastCode();
1112 // varcode = LM_TC_MIN;
1114 if (c == '_' && varcode == LM_TC_TEX) {
1115 mathcursor->Insert(c, LM_TC_SPECIAL);
1116 mathcursor->clearLastCode();
1117 // varcode = LM_TC_MIN;
1119 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1120 mathcursor->MacroModeOpen();
1121 mathcursor->clearLastCode();
1122 mathcursor->Insert(c, LM_TC_MIN);
1125 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1126 mathcursor->Insert(c, LM_TC_CONST);
1128 if (strchr("+/-*<>= ", c))
1129 mathcursor->Insert(c, LM_TC_BOP);
1131 if (strchr(latex_special_chars, c) && c!= '_')
1132 mathcursor->Insert(c, LM_TC_SPECIAL);
1134 if (c == '_' || c == '^') {
1138 mathcursor->Interpret (s);
1142 short f = (mathcursor->getLastCode()) ?
1143 mathcursor->getLastCode():
1144 (MathedTextCodes)mathcursor->GetFCode();
1145 varcode = MathIsAlphaFont(f) ? (MathedTextCodes)f:LM_TC_VAR;
1147 if (varcode == LM_TC_TEXTRM) {
1148 mathcursor->Insert(c, LM_TC_TEXTRM);
1151 mathcursor->MacroModeClose();
1154 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1158 if (!mathcursor->Pop() && mathcursor->IsEnd())
1163 mathcursor->Insert (c, LM_TC_VAR);
1167 mathcursor->MacroModeClose();
1168 minibuffer->Set(_("TeX mode"));
1169 mathcursor->setLastCode(LM_TC_TEX);
1173 // lyxerr << "Closed by action " << action << endl;
1177 if (was_macro!= mathcursor->InMacroMode()&&action>= 0&&action!= LFUN_BACKSPACE)
1179 if (sp && !space_on) sp = 0;
1180 if (mathcursor->Selection() || (was_selection && !(fast_selection || mono_video)))
1181 ToggleInsetSelection();
1193 MathFuncInset::Draw(int x, int y)
1195 if (name && name[0]>' ') {
1196 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1197 font.setLatex(LyXFont::ON);
1198 x += (font.textWidth("I", 1)+3)/4;
1200 int a= font.maxAscent(), d= font.maxDescent();
1201 XFillRectangle (fl_display, pm, getGC(gc_copy),
1203 font.textWidth(name, strlen(name)), a+d);
1206 font.drawString(name, pm, y, x);
1211 void MathFuncInset::Metrics()
1213 ln = (name) ? strlen(name): 0;
1214 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1215 font.setLatex(LyXFont::ON);
1216 width = font.textWidth(name, ln) + font.textWidth("I", 1)/2;
1217 mathed_string_height(LM_TC_TEXTRM, size, (byte const *) name,
1218 strlen(name), ascent, descent);
1222 void mathedValidate(LaTeXFeatures &features, MathParInset *par)
1224 MathedIter it(par->GetData());
1226 while (it.OK() && !(features.binom && features.boldsymbol)) {
1229 MathParInset *p = it.GetActiveInset();
1230 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1231 strcmp(p->GetName(), "binom") == 0) {
1232 features.binom = true;
1234 for (int i= 0; i<= p->getMaxArgumentIdx(); i++) {
1235 p->setArgumentIdx(i);
1236 mathedValidate(features, p);
1240 MathedInset* p = it.GetInset();
1241 if (!features.boldsymbol && p->GetName() &&
1242 strcmp(p->GetName(), "boldsymbol") == 0) {
1243 features.boldsymbol = true;