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 "minibuffer.h"
31 #include "BufferView.h"
34 #include "LaTeXFeatures.h"
36 #include "lyx_gui_misc.h"
37 #include "support/LOstream.h"
41 extern char * mathed_label;
43 extern char const * latex_special_chars;
45 short greek_kb_flag = 0;
47 LyXFont * Math_Fonts = 0; // this is only used by Whichfont and mathed_init_fonts (Lgb)
49 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
52 static int sel_x, sel_y;
54 MathedCursor * InsetFormula::mathcursor = 0;
57 int MathedInset::df_asc;
58 int MathedInset::df_des;
59 int MathedInset::df_width;
63 bool IsMacro(short token, int id)
65 return (token != LM_TK_FRAC && token != LM_TK_SQRT &&
66 !((token == LM_TK_SYM || token == LM_TC_BSYM) && id < 255));
71 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
74 LyXFont WhichFont(short type, int size)
104 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
114 f.setSize(lfont_size);
118 if (type == LM_TC_BSYM) {
128 case LM_ST_SCRIPTSCRIPT:
133 lyxerr << "Mathed Error: wrong font size: " << size << endl;
137 if (type != LM_TC_TEXTRM)
138 f.setColor(LColor::math);
143 void mathed_init_fonts() //removed 'static' because DEC cxx does not
145 // Probably because this func is declared as a friend in math_defs.h
149 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
151 for (int i = 0 ; i < 8 ; ++i) {
152 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
154 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
156 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
158 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
159 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
161 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
163 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
164 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
166 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
168 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
170 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
172 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
173 MathedInset::df_asc = f.maxAscent();
174 MathedInset::df_des = f.maxDescent();
175 MathedInset::df_width = f.width('I');
179 LyXFont mathed_get_font(short type, int size)
181 LyXFont f = WhichFont(type, size);
182 if (type == LM_TC_TEX) {
183 f.setLatex(LyXFont::ON);
189 int mathed_string_width(short type, int size, byte const * s, int ls)
191 LyXFont f = WhichFont(type, size);
194 if (MathIsBinary(type)) {
196 for (int i = 0; i < ls && i < 75; ++i) {
205 return f.textWidth(reinterpret_cast<char const *>(s), ls);
209 int mathed_char_width(short type, int size, byte c)
211 int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1):
212 WhichFont(type, size).width(c);
217 int mathed_string_height(short type, int size, byte const * s,
218 int ls, int & asc, int & des)
220 LyXFont font = WhichFont(type, size);
222 for (int i = 0; i < ls; ++i) {
223 if (font.descent(s[i]) > des)
224 des = font.descent(s[i]);
225 if (font.ascent(s[i]) > asc)
226 asc = font.ascent(s[i]);
232 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
234 LyXFont font = WhichFont(type, size);
236 des = font.descent(c);
237 asc = font.ascent(c);
242 // In a near future maybe we use a better fonts renderer
243 void MathedInset::drawStr(Painter & pain, short type, int size,
244 int x, int y, byte * s, int ls)
247 if (MathIsBinary(type)) {
248 for (int i = 0; i < ls; ++i) {
249 st += string(" ") + char(s[i]) + ' ';
252 st = string(reinterpret_cast<char*>(s), ls);
254 LyXFont mf = mathed_get_font(type, size);
255 pain.text(x, y, st, mf);
259 InsetFormula::InsetFormula(bool display)
261 par = new MathParInset; // this leaks
266 par->SetType(LM_OT_PAR);
267 par->SetStyle(LM_ST_DISPLAY);
272 InsetFormula::InsetFormula(MathParInset * p)
274 par = (p->GetType()>= LM_OT_MPAR) ?
275 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
279 disp_flag = (par->GetType()>0);
284 InsetFormula::~InsetFormula()
290 Inset * InsetFormula::Clone() const
292 InsetFormula * f = new InsetFormula(par);
298 void InsetFormula::Write(ostream & os) const
305 int InsetFormula::Latex(ostream & os, signed char fragile) const
308 //#warning Alejandro, the number of lines is not returned in this case
309 // This problem will disapear at 0.13.
310 #ifdef USE_OSTREAM_ONLY
314 mathed_write(par, os, &ret, fragile, label.c_str());
318 InsetFormula::Latex(output, fragile);
325 #ifndef USE_OSTREAM_ONLY
326 int InsetFormula::Latex(string & file, signed char fragile) const
329 //#warning Alejandro, the number of lines is not returned in this case
330 // This problem will disapear at 0.13.
334 mathed_write(par, file, &ret, fragile, label.c_str());
339 int InsetFormula::Linuxdoc(string &/*file*/) const
345 int InsetFormula::DocBook(string &/*file*/) const
352 int InsetFormula::Linuxdoc(ostream &) const
358 int InsetFormula::DocBook(ostream&) const
365 // Check if uses AMS macros
366 void InsetFormula::Validate(LaTeXFeatures & features) const
368 // Validation only necesary if not using an AMS Style
369 if (!features.amsstyle)
370 mathedValidate(features, par);
374 void InsetFormula::Read(LyXLex & lex)
376 istream & is = lex.getStream();
378 mathed_parser_file(is, lex.GetLineNo());
380 // Silly hack to read labels.
382 mathed_parse(0, 0, &par);
384 disp_flag = (par->GetType() > 0);
386 // Update line number
387 lex.setLineNo(mathed_parser_lineno());
390 label = mathed_label;
400 int InsetFormula::ascent(Painter &, LyXFont const &) const
402 return par->Ascent() + ((disp_flag) ? 8 : 1);
406 int InsetFormula::descent(Painter &, LyXFont const &) const
408 return par->Descent() + ((disp_flag) ? 8 : 1);
412 int InsetFormula::width(Painter &, LyXFont const & f) const
414 lfont_size = f.size();
416 return par->Width(); //+2;
420 void InsetFormula::draw(Painter & pain, LyXFont const & f,
421 int baseline, float & x) const
423 // Seems commenting out solves a problem.
424 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
425 font.setSize(f.size());
426 lfont_size = font.size();
427 /// Let's try to wait a bit with this... (Lgb)
428 //UpdatableInset::draw(pain, font, baseline, x);
430 // otherwise a segfault could occur
431 // in some XDrawRectangles (i.e. matrix) (Matthias)
432 if (mathcursor && mathcursor->GetPar() == par) {
433 if (mathcursor->Selection()) {
437 mathcursor->SelGetArea(&xp, &yp, n);
438 pain.fillPolygon(xp, yp, n, LColor::selection);
440 mathcursor->draw(pain, int(x), baseline);
442 par->draw(pain, int(x), baseline);
444 x += float(width(pain, font));
446 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
447 LyXFont font = WhichFont(LM_TC_BF, par->size);
448 font.setLatex(LyXFont::OFF);
450 if (par->GetType() == LM_OT_PARN) {
453 str = string("(") + label + ")";
456 pain.text(int(x + 20), baseline, str, font);
457 } else if (par->GetType() == LM_OT_MPARN) {
458 MathMatrixInset * mt =
459 static_cast<MathMatrixInset*>(par);
461 MathedRowSt const * crow = mt->getRowSt();
463 y = baseline + crow->getBaseline();
464 if (crow->isNumbered()) {
466 if (crow->getLabel())
467 str = string("(") + crow->getLabel() + ")";
470 pain.text(int(x + 20), y, str, font);
472 crow = crow->getNext();
476 cursor_visible = false;
480 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
482 mathcursor = new MathedCursor(par);
485 bv->updateInset(this, false);
488 mathcursor->SetPos(x, y);
494 void InsetFormula::InsetUnlock(BufferView * bv)
497 if (mathcursor->InMacroMode()) {
498 mathcursor->MacroModeClose();
504 bv->updateInset(this, false);
508 // Now a symbol can be inserted only if the inset is locked
509 void InsetFormula::InsertSymbol(BufferView * bv, char const * s)
511 if (!s || !mathcursor) return;
512 mathcursor->Interpret(s);
517 void InsetFormula::GetCursorPos(int& x, int& y) const
519 mathcursor->GetPos(x, y);
524 void InsetFormula::ToggleInsetCursor(BufferView * bv)
530 mathcursor->GetPos(x, y);
533 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
534 int asc = font.maxAscent();
535 int desc = font.maxDescent();
538 bv->hideLockedInsetCursor();
540 bv->showLockedInsetCursor(x, y, asc, desc);
541 cursor_visible = !cursor_visible;
545 void InsetFormula::ShowInsetCursor(BufferView * bv)
547 if (!cursor_visible) {
550 mathcursor->GetPos(x, y);
553 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
554 int asc = font.maxAscent();
555 int desc = font.maxDescent();
556 bv->fitLockedInsetCursor(x, y, asc, desc);
558 ToggleInsetCursor(bv);
563 void InsetFormula::HideInsetCursor(BufferView * bv)
566 ToggleInsetCursor(bv);
570 void InsetFormula::ToggleInsetSelection(BufferView * bv)
578 //mathcursor->SelGetArea(n);
579 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
583 bv->updateInset(this, false);
588 void InsetFormula::display(bool dspf)
590 if (dspf != disp_flag) {
592 par->SetType(LM_OT_PAR);
593 par->SetStyle(LM_ST_DISPLAY);
595 if (par->GetType() >= LM_OT_MPAR) {
596 MathParInset * p = new MathParInset(par);
600 mathcursor->SetPar(par);
602 par->SetType(LM_OT_MIN);
603 par->SetStyle(LM_ST_TEXT);
604 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
613 int InsetFormula::GetNumberOfLabels() const
615 // This is dirty, I know. I'll clean it at 0.13
616 if (par->GetType() == LM_OT_MPARN) {
617 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
619 MathedRowSt const * crow = mt->getRowSt();
621 if (crow->getLabel()) ++nl;
622 crow = crow->getNext();
633 string InsetFormula::getLabel(int il) const
635 //#warning This is dirty, I know. Ill clean it at 0.11
636 // Correction, the only way to clean this is with a new kernel: 0.13.
637 if (par->GetType() == LM_OT_MPARN) {
639 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
641 MathedRowSt const * crow = mt->getRowSt();
643 if (crow->getLabel()) {
645 lab = crow->getLabel();
650 crow = crow->getNext();
658 void InsetFormula::UpdateLocal(BufferView * bv)
660 par->Metrics(); // To inform lyx kernel the exact size
661 // (there were problems with arrays).
662 bv->updateInset(this, true);
666 void InsetFormula::InsetButtonRelease(BufferView * bv,
667 int x, int y, int /*button*/)
672 mathcursor->SetPos(x, y);
677 bv->updateInset(this, false);
682 void InsetFormula::InsetButtonPress(BufferView * bv,
683 int x, int y, int /*button*/)
686 sel_x = x; sel_y = y;
687 if (mathcursor->Selection()) {
688 mathcursor->SelClear();
689 bv->updateInset(this, false);
694 void InsetFormula::InsetMotionNotify(BufferView * bv,
695 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 bv->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 UpdatableInset::RESULT
741 InsetFormula::LocalDispatch(BufferView * bv,
742 int action, string const & arg)
744 // extern char *dispatch_result;
745 MathedTextCodes varcode = LM_TC_MIN;
746 bool was_macro = mathcursor->InMacroMode();
748 bool space_on = false;
749 bool was_selection = mathcursor->Selection();
750 RESULT result = DISPATCHED;
751 static MathSpaceInset * sp= 0;
755 if (mathcursor->getLastCode() == LM_TC_TEX) {
760 // --- Cursor Movements ---------------------------------------------
761 case LFUN_RIGHTSEL: sel = true;
764 result = DISPATCH_RESULT(mathcursor->Right(sel));
767 case LFUN_LEFTSEL: sel = true;
770 result = DISPATCH_RESULT(mathcursor->Left(sel));
773 case LFUN_UPSEL: sel = true;
775 result = DISPATCH_RESULT(mathcursor->Up(sel));
778 case LFUN_DOWNSEL: sel = true;
780 result = DISPATCH_RESULT(mathcursor->Down(sel));
788 case LFUN_DELETE_LINE_FORWARD:
789 //current_view->lockedInsetStoreUndo(Undo::INSERT);
790 bv->lockedInsetStoreUndo(Undo::DELETE);
791 mathcursor->DelLine();
795 bv->lockedInsetStoreUndo(Undo::INSERT);
796 mathcursor->Insert(' ', LM_TC_CR);
797 par = mathcursor->GetPar();
801 bv->lockedInsetStoreUndo(Undo::INSERT);
802 mathcursor->Insert(0, LM_TC_TAB);
806 bv->lockedInsetStoreUndo(Undo::INSERT);
807 mathcursor->Insert('T', LM_TC_TAB);
811 if (!mathcursor->Left())
814 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
815 bv->updateInset(this, true);
820 //current_view->lockedInsetStoreUndo(Undo::INSERT);
821 bv->lockedInsetStoreUndo(Undo::DELETE);
822 mathcursor->Delete();
823 bv->updateInset(this, true);
826 // sprintf(dispatch_buffer, "%d %d",);
827 // dispatch_result = dispatch_buffer;
832 sscanf(arg.c_str(), "%d %d", &x, &y);
834 mathcursor->SetPos(x1+x, y1+y);
838 /* cursor selection ---------------------------- */
842 mathcursor->MacroModeClose();
843 bv->lockedInsetStoreUndo(Undo::INSERT);
844 mathcursor->SelPaste(); UpdateLocal(bv); break;
846 bv->lockedInsetStoreUndo(Undo::DELETE);
847 mathcursor->SelCut(); UpdateLocal(bv); 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 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
880 case LFUN_GREEK_TOGGLE:
882 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
884 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
886 bv->owner()->getMiniBuffer()->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 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
905 case LFUN_MATH_NUMBER:
907 bv->lockedInsetStoreUndo(Undo::INSERT);
909 short type = par->GetType();
910 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
913 if (!label.empty()) {
916 bv->owner()->getMiniBuffer()->Set(_("No number"));
919 bv->owner()->getMiniBuffer()->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 bv->lockedInsetStoreUndo(Undo::INSERT);
943 if (mathcursor->Limits())
949 latexkeys * l = in_word_set (arg.c_str(), strlen(arg.c_str()));
950 int sz = (l) ? l->id: -1;
951 mathcursor->SetSize(sz);
956 case LFUN_INSERT_MATH:
958 bv->lockedInsetStoreUndo(Undo::INSERT);
959 InsertSymbol(bv, arg.c_str());
963 case LFUN_INSERT_MATRIX:
965 bv->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.c_str(), 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 bv->lockedInsetStoreUndo(Undo::INSERT);
992 char lf[40], rg[40], arg2[40];
993 int ilf = '(', irg = '.';
995 string vdelim("(){}[]./|");
999 strncpy(arg2, arg.c_str(), 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 bv->lockedInsetStoreUndo(Undo::INSERT);
1037 sp = new MathSpaceInset(1);
1038 mathcursor->Insert(sp);
1044 case LFUN_INSERT_LABEL:
1046 bv->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 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1074 bv->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 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1084 varcode = LM_TC_TEXTRM;
1086 varcode = LM_TC_VAR;
1088 mathcursor->setLastCode(varcode);
1092 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1095 //------- dummy actions
1096 case LFUN_EXEC_COMMAND:
1097 bv->owner()->getMiniBuffer()->ExecCommand();
1101 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1102 unsigned char c = arg[0];
1103 bv->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 bv->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)
1213 ToggleInsetSelection(bv);
1215 if (result == DISPATCHED)
1216 ShowInsetCursor(bv);
1218 bv->unlockInset(this);
1225 MathFuncInset::draw(Painter & pain, 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;
1231 pain.text(x, y, name, font);
1236 void MathFuncInset::Metrics()
1238 ln = (name) ? strlen(name): 0;
1239 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1240 font.setLatex(LyXFont::ON);
1241 width = font.textWidth(name, ln) + font.textWidth("I", 1)/2;
1242 mathed_string_height(LM_TC_TEXTRM, size,
1243 reinterpret_cast<unsigned char const *>(name),
1244 strlen(name), ascent, descent);
1249 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1251 MathedIter it(par->GetData());
1253 while (it.OK() && !(features.binom && features.boldsymbol)) {
1256 MathParInset * p = it.GetActiveInset();
1257 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1258 strcmp(p->GetName(), "binom") == 0) {
1259 features.binom = true;
1261 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1262 p->setArgumentIdx(i);
1263 mathedValidate(features, p);
1267 MathedInset* p = it.GetInset();
1268 if (!features.boldsymbol && p->GetName() &&
1269 strcmp(p->GetName(), "boldsymbol") == 0) {
1270 features.boldsymbol = true;