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;
79 inline bool IsMacro(short token, int id)
81 return (token != LM_TK_FRAC && token != LM_TK_SQRT &&
82 !((token == LM_TK_SYM || token == LM_TC_BSYM) && id < 255));
87 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
90 LyXFont WhichFont(short type, int size)
120 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
130 f.setSize(lfont_size);
134 if (type == LM_TC_BSYM) {
144 case LM_ST_SCRIPTSCRIPT:
149 lyxerr << "Mathed Error: wrong font size: " << size << endl;
153 if (type != LM_TC_TEXTRM)
154 f.setColor(LyXFont::MATH);
159 void mathed_init_fonts() //removed 'static' because DEC cxx does not
161 // Probably because this func is declared as a friend in math_defs.h
165 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
167 for (int i = 0 ; i < 8 ; ++i){
168 Math_Fonts[i] = LyXFont::ALL_SANE;
170 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
172 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
174 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
175 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
177 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
179 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
180 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
182 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
184 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
186 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
188 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
189 MathedInset::df_asc = f.maxAscent();
190 MathedInset::df_des = f.maxDescent();
191 MathedInset::df_width = f.width('I');
195 void mathed_set_font(short type, int size)
198 cursorGC = getGC(gc_thin_on_off_line);
199 canvasGC = getGC(gc_lighted);
200 latexGC = getGC(gc_latex);
201 mathLineGC = getGC(gc_math);
202 mathFrameGC = getGC(gc_math_frame);
204 LyXFont f = WhichFont(type, size);
205 if (type == LM_TC_TEX) {
206 f.setLatex(LyXFont::ON);
213 int mathed_string_width(short type, int size, byte const * s, int ls)
215 LyXFont f = WhichFont(type, size);
218 if (MathIsBinary(type)) {
220 for (int i = 0; i < ls && i < 75; ++i) {
229 return f.textWidth(reinterpret_cast<char const *>(s), ls);
233 int mathed_char_width(short type, int size, byte c)
235 int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1):
236 WhichFont(type, size).width(c);
241 int mathed_string_height(short type, int size, byte const * s,
242 int ls, int & asc, int & des)
244 LyXFont font = WhichFont(type, size);
246 for (int i = 0; i < ls; ++i) {
247 if (font.descent(s[i]) > des)
248 des = font.descent(s[i]);
249 if (font.ascent(s[i]) > asc)
250 asc = font.ascent(s[i]);
256 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
258 LyXFont font = WhichFont(type, size);
260 des = font.descent(c);
261 asc = font.ascent(c);
266 // In a near future maybe we use a better fonts renderer
267 void MathedInset::drawStr(short type, int siz, int x, int y, byte * s, int ls)
269 mathed_set_font(type, siz);
271 if (MathIsBinary(type)) {
273 for (int i = 0; i < ls && i < 75; ++i) {
282 GC gc = (type == LM_TC_TEX) ? latexGC: mathGC;
283 XDrawString(fl_display, pm, gc, x, y, reinterpret_cast<char*>(s), ls);
288 InsetFormula::InsetFormula(bool display)
290 par = new MathParInset; // this leaks
295 par->SetType(LM_OT_PAR);
296 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);
313 InsetFormula::~InsetFormula()
319 Inset * InsetFormula::Clone() const
321 InsetFormula * f = new InsetFormula(par);
327 void InsetFormula::Write(FILE * file)
329 fprintf(file, "Formula ");
334 int InsetFormula::Latex(FILE * file, signed char fragile)
337 //#warning Alejandro, the number of lines is not returned in this case
338 // This problem will disapear at 0.13.
340 InsetFormula::Latex(output, fragile);
341 fprintf(file, "%s", output.c_str());
346 int InsetFormula::Latex(string & file, signed char fragile)
349 //#warning Alejandro, the number of lines is not returned in this case
350 // This problem will disapear at 0.13.
354 mathed_write(par, file, &ret, fragile, label.c_str());
359 int InsetFormula::Linuxdoc(string &/*file*/)
365 int InsetFormula::DocBook(string &/*file*/)
371 // Check if uses AMS macros
372 void InsetFormula::Validate(LaTeXFeatures & features) const
374 // Validation only necesary if not using an AMS Style
375 if (!features.amsstyle)
376 mathedValidate(features, par);
380 void InsetFormula::Read(LyXLex & lex)
382 FILE * file = lex.getFile();
384 mathed_parser_file(file, lex.GetLineNo());
386 // Silly hack to read labels.
388 mathed_parse(0, 0, &par);
390 disp_flag = (par->GetType() > 0);
392 // Update line number
393 lex.setLineNo(mathed_parser_lineno());
396 label = mathed_label;
407 int InsetFormula::Ascent(LyXFont const &) const
409 return par->Ascent() + ((disp_flag) ? 8 : 1);
413 int InsetFormula::Descent(LyXFont const &) const
415 return par->Descent() + ((disp_flag) ? 8 : 1);
419 int InsetFormula::Width(LyXFont const & f) const
421 lfont_size = f.size();
423 return par->Width(); //+2;
427 void InsetFormula::Draw(LyXFont f, LyXScreen & scr, int baseline, float & x)
429 // This is Alejandros domain so I'll use this
430 unsigned long pm = scr.getForeground();
432 lfont_size = f.size();
433 mathed_set_font(LM_TC_TEXTRM, LM_ST_TEXT); // otherwise a segfault could occur
434 // in some XDrawRectangles (i.e. matrix) (Matthias)
435 if (mathcursor && mathcursor->GetPar() == par) {
436 if (mathcursor->Selection()) {
438 XPoint * p = mathcursor->SelGetArea(n);
439 XFillPolygon(fl_display, pm, getGC(gc_selection),
440 p, n, Nonconvex, CoordModeOrigin);
442 mathcursor->Draw(pm, int(x), baseline);
445 par->setDrawable(pm);
446 par->Draw(int(x), baseline);
448 x += float(Width(f));
450 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
452 LyXFont font = WhichFont(LM_TC_BF, par->size);
453 font.setLatex(LyXFont::OFF);
455 if (par->GetType() == LM_OT_PARN) {
457 sprintf(s, "(%s)", label.c_str());
460 font.drawString(s, pm, baseline, int(x+20));
462 if (par->GetType() == LM_OT_MPARN) {
463 MathMatrixInset *mt = (MathMatrixInset*)par;
465 MathedRowSt const* crow = mt->getRowSt();
467 y = baseline + crow->getBaseline();
468 if (crow->isNumbered()) {
469 if (crow->getLabel())
470 sprintf(s, "(%s)", crow->getLabel());
473 font.drawString(s, pm, y, int(x+20));
475 crow = crow->getNext();
479 cursor_visible = false;
483 void InsetFormula::Edit(int x, int y)
485 mathcursor = new MathedCursor(par);
488 UpdateInset(this, false);
491 mathcursor->SetPos(x, y);
497 void InsetFormula::InsetUnlock()
500 if (mathcursor->InMacroMode()) {
501 mathcursor->MacroModeClose();
507 UpdateInset(this, false);
511 // Now a symbol can be inserted only if the inset is locked
512 void InsetFormula::InsertSymbol(char const * s)
514 if (!s || !mathcursor) return;
515 mathcursor->Interpret(s);
520 void InsetFormula::GetCursorPos(int& x, int& y) const
522 mathcursor->GetPos(x, y);
527 void InsetFormula::ToggleInsetCursor()
533 mathcursor->GetPos(x, y);
536 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
537 asc = font.maxAscent();
538 desc = font.maxDescent();
541 HideLockedInsetCursor(x, y, asc, desc);
543 ShowLockedInsetCursor(x, y, asc, desc);
544 cursor_visible = !cursor_visible;
548 void InsetFormula::ShowInsetCursor()
550 if (!cursor_visible) {
553 mathcursor->GetPos(x, y);
556 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
557 asc = font.maxAscent();
558 desc = font.maxDescent();
559 FitLockedInsetCursor(x, y, asc, desc);
566 void InsetFormula::HideInsetCursor()
573 void InsetFormula::ToggleInsetSelection()
581 //mathcursor->SelGetArea(n);
582 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
586 UpdateInset(this, false);
591 void InsetFormula::display(bool dspf)
593 if (dspf != disp_flag) {
595 par->SetType(LM_OT_PAR);
596 par->SetStyle(LM_ST_DISPLAY);
598 if (par->GetType() >= LM_OT_MPAR) {
599 MathParInset * p = new MathParInset(par);
603 mathcursor->SetPar(par);
605 par->SetType(LM_OT_MIN);
606 par->SetStyle(LM_ST_TEXT);
607 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
616 int InsetFormula::GetNumberOfLabels() const
618 // This is dirty, I know. I'll clean it at 0.13
619 if (par->GetType() == LM_OT_MPARN) {
620 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
622 MathedRowSt const * crow = mt->getRowSt();
624 if (crow->getLabel()) ++nl;
625 crow = crow->getNext();
636 string InsetFormula::getLabel(int il) const
638 //#warning This is dirty, I know. Ill clean it at 0.11
639 // Correction, the only way to clean this is with a new kernel: 0.13.
640 if (par->GetType() == LM_OT_MPARN) {
642 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
644 MathedRowSt const * crow = mt->getRowSt();
646 if (crow->getLabel()) {
648 lab = crow->getLabel();
653 crow = crow->getNext();
661 void InsetFormula::UpdateLocal()
663 par->Metrics(); // To inform lyx kernel the exact size
664 // (there were problems with arrays).
669 void InsetFormula::InsetButtonRelease(int x, int y, int /*button*/)
674 mathcursor->SetPos(x, y);
679 UpdateInset(this, false);
684 void InsetFormula::InsetButtonPress(int x, int y, int /*button*/)
687 sel_x = x; sel_y = y;
688 if (mathcursor->Selection()) {
689 mathcursor->SelClear();
690 UpdateInset(this, false);
695 void InsetFormula::InsetMotionNotify(int x, int y, int /*button*/)
697 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
700 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
701 mathcursor->SelStart();
703 mathcursor->GetPos(sel_x, sel_y);
709 mathcursor->SetPos(x, y);
711 mathcursor->GetPos(x, y);
712 if (sel_x!= x || sel_y!= y)
713 UpdateInset(this, false);
714 sel_x = x; sel_y = y;
719 void InsetFormula::InsetKeyPress(XKeyEvent *)
721 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
725 // Special Mathed functions
726 bool InsetFormula::SetNumber(bool numbf)
729 short type = par->GetType();
730 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
731 if (numbf && !oldf) ++type;
732 if (!numbf && oldf) --type;
740 bool InsetFormula::LocalDispatch(int action, char const * arg)
742 // extern char *dispatch_result;
743 MathedTextCodes varcode = LM_TC_MIN;
744 bool was_macro = mathcursor->InMacroMode();
746 bool space_on = false;
747 bool was_selection = mathcursor->Selection();
749 static MathSpaceInset * sp= 0;
752 if (mathcursor->Selection() && (fast_selection || mono_video))
753 ToggleInsetSelection();
755 if (mathcursor->getLastCode() == LM_TC_TEX) {
760 // --- Cursor Movements ---------------------------------------------
761 case LFUN_RIGHTSEL: sel = true;
764 result = mathcursor->Right(sel);
767 case LFUN_LEFTSEL: sel = true;
770 result = mathcursor->Left(sel);
773 case LFUN_UPSEL: sel = true;
775 result = mathcursor->Up(sel);
778 case LFUN_DOWNSEL: sel = true;
780 result = mathcursor->Down(sel);
788 case LFUN_DELETE_LINE_FORWARD:
789 //LockedInsetStoreUndo(Undo::INSERT);
790 LockedInsetStoreUndo(Undo::DELETE);
791 mathcursor->DelLine();
795 LockedInsetStoreUndo(Undo::INSERT);
796 mathcursor->Insert(' ', LM_TC_CR);
797 par = mathcursor->GetPar();
801 LockedInsetStoreUndo(Undo::INSERT);
802 mathcursor->Insert(0, LM_TC_TAB);
806 LockedInsetStoreUndo(Undo::INSERT);
807 mathcursor->Insert('T', LM_TC_TAB);
811 if (!mathcursor->Left())
814 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
820 //LockedInsetStoreUndo(Undo::INSERT);
821 LockedInsetStoreUndo(Undo::DELETE);
822 mathcursor->Delete();
826 // sprintf(dispatch_buffer, "%d %d",);
827 // dispatch_result = dispatch_buffer;
832 sscanf(arg, "%d %d", &x, &y);
834 mathcursor->SetPos(x1+x, y1+y);
838 /* cursor selection ---------------------------- */
842 mathcursor->MacroModeClose();
843 LockedInsetStoreUndo(Undo::INSERT);
844 mathcursor->SelPaste(); UpdateLocal(); break;
846 LockedInsetStoreUndo(Undo::DELETE);
847 mathcursor->SelCut(); UpdateLocal(); break;
848 case LFUN_COPY: mathcursor->SelCopy(); break;
851 case LFUN_WORDRIGHTSEL:
852 case LFUN_WORDLEFTSEL:
855 // --- accented characters ------------------------------
857 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
858 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
859 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
860 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
861 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
862 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
863 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
864 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
865 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
866 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
871 if (!greek_kb_flag) {
873 minibuffer->Set(_("Math greek mode on"));
880 case LFUN_GREEK_TOGGLE:
882 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
884 minibuffer->Set(_("Math greek keyboard on"));
886 minibuffer->Set(_("Math greek keyboard off"));
891 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
892 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
893 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
894 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
895 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
896 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
899 // varcode = LM_TC_TEX;
900 mathcursor->setLastCode(LM_TC_TEX);
901 minibuffer->Set(_("TeX mode"));
905 case LFUN_MATH_NUMBER:
907 LockedInsetStoreUndo(Undo::INSERT);
909 short type = par->GetType();
910 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
913 if (!label.empty()) {
916 minibuffer->Set(_("No number"));
919 minibuffer->Set(_("Number"));
927 case LFUN_MATH_NONUMBER:
929 if (par->GetType() == LM_OT_MPARN) {
930 // MathMatrixInset *mt = (MathMatrixInset*)par;
932 // mt->SetNumbered(!mt->IsNumbered());
934 mathcursor->setNumbered();
940 case LFUN_MATH_LIMITS:
942 LockedInsetStoreUndo(Undo::INSERT);
943 if (mathcursor->Limits())
949 latexkeys * l = in_word_set (arg, strlen(arg));
950 int sz = (l) ? l->id: -1;
951 mathcursor->SetSize(sz);
956 case LFUN_INSERT_MATH:
958 LockedInsetStoreUndo(Undo::INSERT);
963 case LFUN_INSERT_MATRIX:
965 LockedInsetStoreUndo(Undo::INSERT);
967 char s[80], arg2[80];
968 // This is just so that too long args won't ooze out of s.
969 strncpy(arg2, arg, 80); arg2[79]= '\0';
970 k = sscanf(arg2, "%d %d %s", &m, &n, s);
979 MathMatrixInset * p = new MathMatrixInset(m, n);
980 if (mathcursor && p) {
981 if (k > 2 && int(strlen(s)) > m)
982 p->SetAlign(s[0], &s[1]);
983 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
989 case LFUN_MATH_DELIM:
991 LockedInsetStoreUndo(Undo::INSERT);
992 char lf[40], rg[40], arg2[40];
993 int ilf = '(', irg = '.';
995 string vdelim("(){}[]./|");
998 strncpy(arg2, arg, 40); arg2[39]= '\0';
999 int n = sscanf(arg2, "%s %s", lf, rg);
1000 lf[39] = '\0'; rg[39] = '\0';
1007 l = in_word_set(lf, strlen(lf));
1008 // Long words will cause l == 0; so check.
1011 if (vdelim.find(lf[0]) != string::npos)
1019 l = in_word_set(rg, strlen(rg));
1022 if (vdelim.find(rg[0]) != string::npos)
1027 MathDelimInset * p = new MathDelimInset(ilf, irg);
1028 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1033 case LFUN_PROTECTEDSPACE:
1035 LockedInsetStoreUndo(Undo::INSERT);
1036 sp = new MathSpaceInset(1);
1037 mathcursor->Insert(sp);
1043 case LFUN_INSERT_LABEL:
1045 LockedInsetStoreUndo(Undo::INSERT);
1046 if (par->GetType() < LM_OT_PAR) break;
1049 lb = string(askForText(_("Enter new label to insert:")));
1050 if (!lb.empty() && lb[0] > ' ') {
1052 if (par->GetType() == LM_OT_MPARN) {
1053 mathcursor->setLabel(lb.c_str());
1054 // MathMatrixInset *mt = (MathMatrixInset*)par;
1055 // mt->SetLabel(lb);
1057 //if (label.notEmpty()) delete label;
1066 case LFUN_MATH_DISPLAY:
1067 //LockedInsetStoreUndo(Undo::INSERT);
1068 LockedInsetStoreUndo(Undo::EDIT);
1069 display(!disp_flag);
1073 // Invalid actions under math mode
1074 case LFUN_MATH_MODE:
1076 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1077 minibuffer->Set(_("math text mode"));
1078 varcode = LM_TC_TEXTRM;
1080 varcode = LM_TC_VAR;
1082 mathcursor->setLastCode(varcode);
1086 minibuffer->Set(_("Invalid action in math mode!"));
1089 //------- dummy actions
1090 case LFUN_EXEC_COMMAND:
1091 minibuffer->ExecCommand();
1095 if ((action == -1 || action == LFUN_SELFINSERT) && arg) {
1096 unsigned char c = arg[0];
1097 LockedInsetStoreUndo(Undo::INSERT);
1099 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1101 mathcursor->setAccent(0);
1103 if (c == 0) { // Dead key, do nothing
1104 //lyxerr << "deadkey" << endl;
1108 if (mathcursor->getLastCode() == LM_TC_TEX) {
1109 mathcursor->MacroModeOpen();
1110 mathcursor->clearLastCode();
1111 varcode = LM_TC_MIN;
1114 short f = (mathcursor->getLastCode()) ?
1115 mathcursor->getLastCode():
1116 (MathedTextCodes)mathcursor->GetFCode();
1117 varcode = MathIsAlphaFont(f) ? (MathedTextCodes)f:LM_TC_VAR;
1120 // lyxerr << "Varcode << vardoce;
1121 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1122 varcode = LM_TC_MIN;
1123 if (greek_kb_flag<2) greek_kb_flag = 0;
1125 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1126 mathcursor->Insert(c, LM_TC_TEX);
1128 mathcursor->Insert('}', LM_TC_TEX);
1131 mathcursor->clearLastCode();
1132 // varcode = LM_TC_MIN;
1134 if (c == '_' && varcode == LM_TC_TEX) {
1135 mathcursor->Insert(c, LM_TC_SPECIAL);
1136 mathcursor->clearLastCode();
1137 // varcode = LM_TC_MIN;
1139 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1140 mathcursor->MacroModeOpen();
1141 mathcursor->clearLastCode();
1142 mathcursor->Insert(c, LM_TC_MIN);
1145 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1146 mathcursor->Insert(c, LM_TC_CONST);
1148 if (strchr("+/-*<>= ", c))
1149 mathcursor->Insert(c, LM_TC_BOP);
1151 if (strchr(latex_special_chars, c) && c!= '_')
1152 mathcursor->Insert(c, LM_TC_SPECIAL);
1154 if (c == '_' || c == '^') {
1158 mathcursor->Interpret (s);
1162 short f = (mathcursor->getLastCode()) ?
1163 mathcursor->getLastCode():
1164 (MathedTextCodes)mathcursor->GetFCode();
1165 varcode = MathIsAlphaFont(f) ? (MathedTextCodes)f:LM_TC_VAR;
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 minibuffer->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();
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;