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"
41 #include "support/LOstream.h"
44 extern void UpdateInset(Inset * inset, bool mark_dirty = true);
45 extern void LockedInsetStoreUndo(Undo::undo_kind);
46 extern void ShowLockedInsetCursor(long, long, int, int);
47 extern void HideLockedInsetCursor(long, long, int, int);
48 extern void FitLockedInsetCursor(long, long, int, int);
49 extern int LockInset(UpdatableInset *);
50 extern int UnlockInset(UpdatableInset *);
53 extern GC canvasGC, mathGC, mathLineGC, latexGC, cursorGC, mathFrameGC;
54 extern char * mathed_label;
56 extern int mono_video;
57 extern int fast_selection;
59 extern BufferView * current_view;
60 extern BufferList bufferlist;
61 extern char const * latex_special_chars;
63 short greek_kb_flag = 0;
65 LyXFont * Math_Fonts = 0; // this is only used by Whichfont and mathed_init_fonts (Lgb)
67 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
70 static int sel_x, sel_y;
72 MathedCursor * InsetFormula::mathcursor = 0;
75 int MathedInset::df_asc;
76 int MathedInset::df_des;
77 int MathedInset::df_width;
80 inline bool IsMacro(short token, int id)
82 return (token != LM_TK_FRAC && token != LM_TK_SQRT &&
83 !((token == LM_TK_SYM || token == LM_TC_BSYM) && id < 255));
88 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
91 LyXFont WhichFont(short type, int size)
121 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
131 f.setSize(lfont_size);
135 if (type == LM_TC_BSYM) {
145 case LM_ST_SCRIPTSCRIPT:
150 lyxerr << "Mathed Error: wrong font size: " << size << endl;
154 if (type != LM_TC_TEXTRM)
155 f.setColor(LyXFont::MATH);
160 void mathed_init_fonts() //removed 'static' because DEC cxx does not
162 // Probably because this func is declared as a friend in math_defs.h
166 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
168 for (int i = 0 ; i < 8 ; ++i){
169 Math_Fonts[i] = LyXFont::ALL_SANE;
171 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
173 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
175 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
176 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
178 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
180 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
181 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
183 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
185 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
187 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
189 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
190 MathedInset::df_asc = f.maxAscent();
191 MathedInset::df_des = f.maxDescent();
192 MathedInset::df_width = f.width('I');
196 void mathed_set_font(short type, int size)
199 cursorGC = getGC(gc_thin_on_off_line);
200 canvasGC = getGC(gc_lighted);
201 latexGC = getGC(gc_latex);
202 mathLineGC = getGC(gc_math);
203 mathFrameGC = getGC(gc_math_frame);
205 LyXFont f = WhichFont(type, size);
206 if (type == LM_TC_TEX) {
207 f.setLatex(LyXFont::ON);
214 int mathed_string_width(short type, int size, byte const * s, int ls)
216 LyXFont f = WhichFont(type, size);
219 if (MathIsBinary(type)) {
221 for (int i = 0; i < ls && i < 75; ++i) {
230 return f.textWidth(reinterpret_cast<char const *>(s), ls);
234 int mathed_char_width(short type, int size, byte c)
236 int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1):
237 WhichFont(type, size).width(c);
242 int mathed_string_height(short type, int size, byte const * s,
243 int ls, int & asc, int & des)
245 LyXFont font = WhichFont(type, size);
247 for (int i = 0; i < ls; ++i) {
248 if (font.descent(s[i]) > des)
249 des = font.descent(s[i]);
250 if (font.ascent(s[i]) > asc)
251 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);
302 InsetFormula::InsetFormula(MathParInset * p)
304 par = (p->GetType()>= LM_OT_MPAR) ?
305 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
309 disp_flag = (par->GetType()>0);
314 InsetFormula::~InsetFormula()
320 Inset * InsetFormula::Clone() const
322 InsetFormula * f = new InsetFormula(par);
328 void InsetFormula::Write(ostream & os)
335 int InsetFormula::Latex(ostream & os, signed char fragile)
338 //#warning Alejandro, the number of lines is not returned in this case
339 // This problem will disapear at 0.13.
341 InsetFormula::Latex(output, fragile);
347 int InsetFormula::Latex(string & file, signed char fragile)
350 //#warning Alejandro, the number of lines is not returned in this case
351 // This problem will disapear at 0.13.
355 mathed_write(par, file, &ret, fragile, label.c_str());
360 int InsetFormula::Linuxdoc(string &/*file*/)
366 int InsetFormula::DocBook(string &/*file*/)
372 // Check if uses AMS macros
373 void InsetFormula::Validate(LaTeXFeatures & features) const
375 // Validation only necesary if not using an AMS Style
376 if (!features.amsstyle)
377 mathedValidate(features, par);
381 void InsetFormula::Read(LyXLex & lex)
383 FILE * file = lex.getFile();
385 mathed_parser_file(file, lex.GetLineNo());
387 // Silly hack to read labels.
389 mathed_parse(0, 0, &par);
391 disp_flag = (par->GetType() > 0);
393 // Update line number
394 lex.setLineNo(mathed_parser_lineno());
397 label = mathed_label;
408 int InsetFormula::Ascent(LyXFont const &) const
410 return par->Ascent() + ((disp_flag) ? 8 : 1);
414 int InsetFormula::Descent(LyXFont const &) const
416 return par->Descent() + ((disp_flag) ? 8 : 1);
420 int InsetFormula::Width(LyXFont const & f) const
422 lfont_size = f.size();
424 return par->Width(); //+2;
428 void InsetFormula::Draw(LyXFont f, LyXScreen & scr, int baseline, float & x)
430 // This is Alejandros domain so I'll use this
431 unsigned long pm = scr.getForeground();
433 lfont_size = f.size();
434 mathed_set_font(LM_TC_TEXTRM, LM_ST_TEXT); // otherwise a segfault could occur
435 // in some XDrawRectangles (i.e. matrix) (Matthias)
436 if (mathcursor && mathcursor->GetPar() == par) {
437 if (mathcursor->Selection()) {
439 XPoint * p = mathcursor->SelGetArea(n);
440 XFillPolygon(fl_display, pm, getGC(gc_selection),
441 p, n, Nonconvex, CoordModeOrigin);
443 mathcursor->Draw(pm, int(x), baseline);
446 par->setDrawable(pm);
447 par->Draw(int(x), baseline);
449 x += float(Width(f));
451 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
453 LyXFont font = WhichFont(LM_TC_BF, par->size);
454 font.setLatex(LyXFont::OFF);
456 if (par->GetType() == LM_OT_PARN) {
458 sprintf(s, "(%s)", label.c_str());
461 font.drawString(s, pm, baseline, int(x+20));
463 if (par->GetType() == LM_OT_MPARN) {
464 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
466 MathedRowSt const* crow = mt->getRowSt();
468 y = baseline + crow->getBaseline();
469 if (crow->isNumbered()) {
470 if (crow->getLabel())
471 sprintf(s, "(%s)", crow->getLabel());
474 font.drawString(s, pm, y, int(x+20));
476 crow = crow->getNext();
480 cursor_visible = false;
484 void InsetFormula::Edit(int x, int y)
486 mathcursor = new MathedCursor(par);
489 UpdateInset(this, false);
492 mathcursor->SetPos(x, y);
498 void InsetFormula::InsetUnlock()
501 if (mathcursor->InMacroMode()) {
502 mathcursor->MacroModeClose();
508 UpdateInset(this, false);
512 // Now a symbol can be inserted only if the inset is locked
513 void InsetFormula::InsertSymbol(char const * s)
515 if (!s || !mathcursor) return;
516 mathcursor->Interpret(s);
521 void InsetFormula::GetCursorPos(int& x, int& y) const
523 mathcursor->GetPos(x, y);
528 void InsetFormula::ToggleInsetCursor()
534 mathcursor->GetPos(x, y);
537 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
538 asc = font.maxAscent();
539 desc = font.maxDescent();
542 HideLockedInsetCursor(x, y, asc, desc);
544 ShowLockedInsetCursor(x, y, asc, desc);
545 cursor_visible = !cursor_visible;
549 void InsetFormula::ShowInsetCursor()
551 if (!cursor_visible) {
554 mathcursor->GetPos(x, y);
557 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
558 asc = font.maxAscent();
559 desc = font.maxDescent();
560 FitLockedInsetCursor(x, y, asc, desc);
567 void InsetFormula::HideInsetCursor()
574 void InsetFormula::ToggleInsetSelection()
582 //mathcursor->SelGetArea(n);
583 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
587 UpdateInset(this, false);
592 void InsetFormula::display(bool dspf)
594 if (dspf != disp_flag) {
596 par->SetType(LM_OT_PAR);
597 par->SetStyle(LM_ST_DISPLAY);
599 if (par->GetType() >= LM_OT_MPAR) {
600 MathParInset * p = new MathParInset(par);
604 mathcursor->SetPar(par);
606 par->SetType(LM_OT_MIN);
607 par->SetStyle(LM_ST_TEXT);
608 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
617 int InsetFormula::GetNumberOfLabels() const
619 // This is dirty, I know. I'll clean it at 0.13
620 if (par->GetType() == LM_OT_MPARN) {
621 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
623 MathedRowSt const * crow = mt->getRowSt();
625 if (crow->getLabel()) ++nl;
626 crow = crow->getNext();
637 string InsetFormula::getLabel(int il) const
639 //#warning This is dirty, I know. Ill clean it at 0.11
640 // Correction, the only way to clean this is with a new kernel: 0.13.
641 if (par->GetType() == LM_OT_MPARN) {
643 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
645 MathedRowSt const * crow = mt->getRowSt();
647 if (crow->getLabel()) {
649 lab = crow->getLabel();
654 crow = crow->getNext();
662 void InsetFormula::UpdateLocal()
664 par->Metrics(); // To inform lyx kernel the exact size
665 // (there were problems with arrays).
670 void InsetFormula::InsetButtonRelease(int x, int y, int /*button*/)
675 mathcursor->SetPos(x, y);
680 UpdateInset(this, false);
685 void InsetFormula::InsetButtonPress(int x, int y, int /*button*/)
688 sel_x = x; sel_y = y;
689 if (mathcursor->Selection()) {
690 mathcursor->SelClear();
691 UpdateInset(this, false);
696 void InsetFormula::InsetMotionNotify(int x, int y, int /*button*/)
698 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
701 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
702 mathcursor->SelStart();
704 mathcursor->GetPos(sel_x, sel_y);
710 mathcursor->SetPos(x, y);
712 mathcursor->GetPos(x, y);
713 if (sel_x!= x || sel_y!= y)
714 UpdateInset(this, false);
715 sel_x = x; sel_y = y;
720 void InsetFormula::InsetKeyPress(XKeyEvent *)
722 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
726 // Special Mathed functions
727 bool InsetFormula::SetNumber(bool numbf)
730 short type = par->GetType();
731 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
732 if (numbf && !oldf) ++type;
733 if (!numbf && oldf) --type;
741 bool InsetFormula::LocalDispatch(int action, char const * arg)
743 // extern char *dispatch_result;
744 MathedTextCodes varcode = LM_TC_MIN;
745 bool was_macro = mathcursor->InMacroMode();
747 bool space_on = false;
748 bool was_selection = mathcursor->Selection();
750 static MathSpaceInset * sp= 0;
753 if (mathcursor->Selection() && (fast_selection || mono_video))
754 ToggleInsetSelection();
756 if (mathcursor->getLastCode() == LM_TC_TEX) {
761 // --- Cursor Movements ---------------------------------------------
762 case LFUN_RIGHTSEL: sel = true;
765 result = mathcursor->Right(sel);
768 case LFUN_LEFTSEL: sel = true;
771 result = mathcursor->Left(sel);
774 case LFUN_UPSEL: sel = true;
776 result = mathcursor->Up(sel);
779 case LFUN_DOWNSEL: sel = true;
781 result = mathcursor->Down(sel);
789 case LFUN_DELETE_LINE_FORWARD:
790 //LockedInsetStoreUndo(Undo::INSERT);
791 LockedInsetStoreUndo(Undo::DELETE);
792 mathcursor->DelLine();
796 LockedInsetStoreUndo(Undo::INSERT);
797 mathcursor->Insert(' ', LM_TC_CR);
798 par = mathcursor->GetPar();
802 LockedInsetStoreUndo(Undo::INSERT);
803 mathcursor->Insert(0, LM_TC_TAB);
807 LockedInsetStoreUndo(Undo::INSERT);
808 mathcursor->Insert('T', LM_TC_TAB);
812 if (!mathcursor->Left())
815 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
821 //LockedInsetStoreUndo(Undo::INSERT);
822 LockedInsetStoreUndo(Undo::DELETE);
823 mathcursor->Delete();
827 // sprintf(dispatch_buffer, "%d %d",);
828 // dispatch_result = dispatch_buffer;
833 sscanf(arg, "%d %d", &x, &y);
835 mathcursor->SetPos(x1+x, y1+y);
839 /* cursor selection ---------------------------- */
843 mathcursor->MacroModeClose();
844 LockedInsetStoreUndo(Undo::INSERT);
845 mathcursor->SelPaste(); UpdateLocal(); break;
847 LockedInsetStoreUndo(Undo::DELETE);
848 mathcursor->SelCut(); UpdateLocal(); break;
849 case LFUN_COPY: mathcursor->SelCopy(); break;
852 case LFUN_WORDRIGHTSEL:
853 case LFUN_WORDLEFTSEL:
856 // --- accented characters ------------------------------
858 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
859 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
860 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
861 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
862 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
863 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
864 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
865 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
866 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
867 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
872 if (!greek_kb_flag) {
874 current_view->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
881 case LFUN_GREEK_TOGGLE:
883 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
885 current_view->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
887 current_view->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
892 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
893 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
894 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
895 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
896 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
897 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
900 // varcode = LM_TC_TEX;
901 mathcursor->setLastCode(LM_TC_TEX);
902 current_view->owner()->getMiniBuffer()->Set(_("TeX mode"));
906 case LFUN_MATH_NUMBER:
908 LockedInsetStoreUndo(Undo::INSERT);
910 short type = par->GetType();
911 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
914 if (!label.empty()) {
917 current_view->owner()->getMiniBuffer()->Set(_("No number"));
920 current_view->owner()->getMiniBuffer()->Set(_("Number"));
928 case LFUN_MATH_NONUMBER:
930 if (par->GetType() == LM_OT_MPARN) {
931 // MathMatrixInset *mt = (MathMatrixInset*)par;
933 // mt->SetNumbered(!mt->IsNumbered());
935 mathcursor->setNumbered();
941 case LFUN_MATH_LIMITS:
943 LockedInsetStoreUndo(Undo::INSERT);
944 if (mathcursor->Limits())
950 latexkeys * l = in_word_set (arg, strlen(arg));
951 int sz = (l) ? l->id: -1;
952 mathcursor->SetSize(sz);
957 case LFUN_INSERT_MATH:
959 LockedInsetStoreUndo(Undo::INSERT);
964 case LFUN_INSERT_MATRIX:
966 LockedInsetStoreUndo(Undo::INSERT);
968 char s[80], arg2[80];
969 // This is just so that too long args won't ooze out of s.
970 strncpy(arg2, arg, 80); arg2[79]= '\0';
971 k = sscanf(arg2, "%d %d %s", &m, &n, s);
980 MathMatrixInset * p = new MathMatrixInset(m, n);
981 if (mathcursor && p) {
982 if (k > 2 && int(strlen(s)) > m)
983 p->SetAlign(s[0], &s[1]);
984 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
990 case LFUN_MATH_DELIM:
992 LockedInsetStoreUndo(Undo::INSERT);
993 char lf[40], rg[40], arg2[40];
994 int ilf = '(', irg = '.';
996 string vdelim("(){}[]./|");
999 strncpy(arg2, arg, 40); arg2[39]= '\0';
1000 int n = sscanf(arg2, "%s %s", lf, rg);
1001 lf[39] = '\0'; rg[39] = '\0';
1008 l = in_word_set(lf, strlen(lf));
1009 // Long words will cause l == 0; so check.
1012 if (vdelim.find(lf[0]) != string::npos)
1020 l = in_word_set(rg, strlen(rg));
1023 if (vdelim.find(rg[0]) != string::npos)
1028 MathDelimInset * p = new MathDelimInset(ilf, irg);
1029 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1034 case LFUN_PROTECTEDSPACE:
1036 LockedInsetStoreUndo(Undo::INSERT);
1037 sp = new MathSpaceInset(1);
1038 mathcursor->Insert(sp);
1044 case LFUN_INSERT_LABEL:
1046 LockedInsetStoreUndo(Undo::INSERT);
1047 if (par->GetType() < LM_OT_PAR) break;
1051 result = askForText(_("Enter new label to insert:"));
1056 if (!lb.empty() && lb[0] > ' ') {
1058 if (par->GetType() == LM_OT_MPARN) {
1059 mathcursor->setLabel(lb.c_str());
1060 // MathMatrixInset *mt = (MathMatrixInset*)par;
1061 // mt->SetLabel(lb);
1063 //if (label.notEmpty()) delete label;
1072 case LFUN_MATH_DISPLAY:
1073 //LockedInsetStoreUndo(Undo::INSERT);
1074 LockedInsetStoreUndo(Undo::EDIT);
1075 display(!disp_flag);
1079 // Invalid actions under math mode
1080 case LFUN_MATH_MODE:
1082 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1083 current_view->owner()->getMiniBuffer()->Set(_("math text mode"));
1084 varcode = LM_TC_TEXTRM;
1086 varcode = LM_TC_VAR;
1088 mathcursor->setLastCode(varcode);
1092 current_view->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1095 //------- dummy actions
1096 case LFUN_EXEC_COMMAND:
1097 current_view->owner()->getMiniBuffer()->ExecCommand();
1101 if ((action == -1 || action == LFUN_SELFINSERT) && arg) {
1102 unsigned char c = arg[0];
1103 LockedInsetStoreUndo(Undo::INSERT);
1105 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1107 mathcursor->setAccent(0);
1109 if (c == 0) { // Dead key, do nothing
1110 //lyxerr << "deadkey" << endl;
1114 if (mathcursor->getLastCode() == LM_TC_TEX) {
1115 mathcursor->MacroModeOpen();
1116 mathcursor->clearLastCode();
1117 varcode = LM_TC_MIN;
1120 short f = (mathcursor->getLastCode()) ?
1121 mathcursor->getLastCode() :
1122 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1123 varcode = MathIsAlphaFont(f) ?
1124 static_cast<MathedTextCodes>(f) :
1128 // lyxerr << "Varcode << vardoce;
1129 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1130 varcode = LM_TC_MIN;
1131 if (greek_kb_flag<2) greek_kb_flag = 0;
1133 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1134 mathcursor->Insert(c, LM_TC_TEX);
1136 mathcursor->Insert('}', LM_TC_TEX);
1139 mathcursor->clearLastCode();
1140 // varcode = LM_TC_MIN;
1142 if (c == '_' && varcode == LM_TC_TEX) {
1143 mathcursor->Insert(c, LM_TC_SPECIAL);
1144 mathcursor->clearLastCode();
1145 // varcode = LM_TC_MIN;
1147 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1148 mathcursor->MacroModeOpen();
1149 mathcursor->clearLastCode();
1150 mathcursor->Insert(c, LM_TC_MIN);
1153 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1154 mathcursor->Insert(c, LM_TC_CONST);
1156 if (strchr("+/-*<>=", c))
1157 mathcursor->Insert(c, LM_TC_BOP);
1159 if (strchr(latex_special_chars, c) && c!= '_')
1160 mathcursor->Insert(c, LM_TC_SPECIAL);
1162 if (c == '_' || c == '^') {
1166 mathcursor->Interpret (s);
1170 short f = (mathcursor->getLastCode()) ?
1171 mathcursor->getLastCode() :
1172 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1173 varcode = MathIsAlphaFont(f) ?
1174 static_cast<MathedTextCodes>(f) :
1177 if (varcode == LM_TC_TEXTRM) {
1178 mathcursor->Insert(c, LM_TC_TEXTRM);
1181 mathcursor->MacroModeClose();
1184 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1188 if (!mathcursor->Pop() && mathcursor->IsEnd())
1193 mathcursor->Insert (c, LM_TC_VAR);
1197 mathcursor->MacroModeClose();
1198 current_view->owner()->getMiniBuffer()->Set(_("TeX mode"));
1199 mathcursor->setLastCode(LM_TC_TEX);
1203 // lyxerr << "Closed by action " << action << endl;
1207 if (was_macro != mathcursor->InMacroMode()
1209 && action != LFUN_BACKSPACE)
1211 if (sp && !space_on) sp = 0;
1212 if (mathcursor->Selection() || (was_selection && !(fast_selection || mono_video)))
1213 ToggleInsetSelection();
1225 MathFuncInset::Draw(int x, int y)
1227 if (name && name[0] > ' ') {
1228 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1229 font.setLatex(LyXFont::ON);
1230 x += (font.textWidth("I", 1)+3)/4;
1232 int a = font.maxAscent(), d = font.maxDescent();
1233 XFillRectangle (fl_display, pm, getGC(gc_copy),
1235 font.textWidth(name, strlen(name)), a+d);
1238 font.drawString(name, pm, y, x);
1243 void MathFuncInset::Metrics()
1245 ln = (name) ? strlen(name): 0;
1246 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1247 font.setLatex(LyXFont::ON);
1248 width = font.textWidth(name, ln) + font.textWidth("I", 1)/2;
1249 mathed_string_height(LM_TC_TEXTRM, size,
1250 reinterpret_cast<unsigned char const *>(name),
1251 strlen(name), ascent, descent);
1256 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1258 MathedIter it(par->GetData());
1260 while (it.OK() && !(features.binom && features.boldsymbol)) {
1263 MathParInset * p = it.GetActiveInset();
1264 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1265 strcmp(p->GetName(), "binom") == 0) {
1266 features.binom = true;
1268 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1269 p->setArgumentIdx(i);
1270 mathedValidate(features, p);
1274 MathedInset* p = it.GetInset();
1275 if (!features.boldsymbol && p->GetName() &&
1276 strcmp(p->GetName(), "boldsymbol") == 0) {
1277 features.boldsymbol = true;