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: 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.
20 using std::istringstream;
26 #pragma implementation "formula.h"
30 #include "commandtags.h"
31 #include "math_cursor.h"
32 #include "math_parser.h"
34 #include "minibuffer.h"
35 #include "BufferView.h"
38 #include "LaTeXFeatures.h"
40 #include "lyx_gui_misc.h"
41 #include "support/LOstream.h"
51 extern char * mathed_label;
53 extern char const * latex_special_chars;
55 short greek_kb_flag = 0;
57 LyXFont * Math_Fonts = 0; // this is only used by Whichfont and mathed_init_fonts (Lgb)
59 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
62 static int sel_x, sel_y;
64 MathedCursor * InsetFormula::mathcursor = 0;
67 int MathedInset::df_asc;
68 int MathedInset::df_des;
69 int MathedInset::df_width;
73 bool IsMacro(short token, int id)
75 return (token != LM_TK_FRAC && token != LM_TK_SQRT &&
76 !((token == LM_TK_SYM || token == LM_TC_BSYM) && id < 255));
81 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
84 LyXFont WhichFont(short type, int size)
114 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
124 f.setSize(lfont_size);
128 if (type == LM_TC_BSYM) {
138 case LM_ST_SCRIPTSCRIPT:
143 lyxerr << "Mathed Error: wrong font size: " << size << endl;
147 if (type != LM_TC_TEXTRM)
148 f.setColor(LColor::math);
153 void mathed_init_fonts() //removed 'static' because DEC cxx does not
155 // Probably because this func is declared as a friend in math_defs.h
159 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
161 for (int i = 0 ; i < 8 ; ++i) {
162 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
164 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
166 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
168 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
169 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
171 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
173 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
174 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
176 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
178 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
180 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
182 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
183 MathedInset::df_asc = lyxfont::maxAscent(f);
184 MathedInset::df_des = lyxfont::maxDescent(f);
185 MathedInset::df_width = lyxfont::width('I', f);
189 LyXFont mathed_get_font(short type, int size)
191 LyXFont f = WhichFont(type, size);
192 if (type == LM_TC_TEX) {
193 f.setLatex(LyXFont::ON);
199 int mathed_string_width(short type, int size, byte const * s, int ls)
201 LyXFont f = WhichFont(type, size);
204 if (MathIsBinary(type)) {
206 for (int i = 0; i < ls && i < 75; ++i) {
215 return lyxfont::width(reinterpret_cast<char const *>(s), ls, f);
219 int mathed_char_width(short type, int size, byte c)
221 int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1) :
222 lyxfont::width(c, WhichFont(type, size));
227 int mathed_string_height(short type, int size, byte const * s,
228 int ls, int & asc, int & des)
230 LyXFont font = WhichFont(type, size);
232 for (int i = 0; i < ls; ++i) {
233 if (lyxfont::descent(s[i], font) > des)
234 des = lyxfont::descent(s[i], font);
235 if (lyxfont::ascent(s[i], font) > asc)
236 asc = lyxfont::ascent(s[i], font);
242 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
244 LyXFont font = WhichFont(type, size);
246 des = lyxfont::descent(c, font);
247 asc = lyxfont::ascent(c, font);
252 // In a near future maybe we use a better fonts renderer
253 void MathedInset::drawStr(Painter & pain, short type, int siz,
254 int x, int y, byte const * s, int ls)
257 if (MathIsBinary(type)) {
258 for (int i = 0; i < ls; ++i) {
259 st += string(" ") + char(s[i]) + ' ';
262 st = string(reinterpret_cast<char const *>(s), ls);
264 LyXFont mf = mathed_get_font(type, siz);
265 pain.text(x, y, st, mf);
269 InsetFormula::InsetFormula(bool display)
271 par = new MathParInset; // this leaks
276 par->SetType(LM_OT_PAR);
277 par->SetStyle(LM_ST_DISPLAY);
282 InsetFormula::InsetFormula(MathParInset * p)
284 par = (p->GetType()>= LM_OT_MPAR) ?
285 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
289 disp_flag = (par->GetType()>0);
294 InsetFormula::~InsetFormula()
300 Inset * InsetFormula::Clone() const
302 InsetFormula * f = new InsetFormula(par);
308 void InsetFormula::Write(ostream & os) const
311 Latex(os, false, false);
315 int InsetFormula::Latex(ostream & os, bool fragile, bool) const
318 //#warning Alejandro, the number of lines is not returned in this case
319 // This problem will disapear at 0.13.
320 mathed_write(par, os, &ret, fragile, label.c_str());
325 int InsetFormula::Ascii(ostream & os) const
327 par->Write(os, false);
332 int InsetFormula::Linuxdoc(ostream &) const
338 int InsetFormula::DocBook(ostream&) const
344 // Check if uses AMS macros
345 void InsetFormula::Validate(LaTeXFeatures & features) const
347 // Validation only necesary if not using an AMS Style
348 if (!features.amsstyle)
349 mathedValidate(features, par);
353 void InsetFormula::Read(LyXLex & lex)
355 istream & is = lex.getStream();
357 mathed_parser_file(is, lex.GetLineNo());
359 // Silly hack to read labels.
361 mathed_parse(0, 0, &par);
363 disp_flag = (par->GetType() > 0);
365 // Update line number
366 lex.setLineNo(mathed_parser_lineno());
369 label = mathed_label;
372 // reading of end_inset in the inset!!!
375 if (lex.GetString() == "\\end_inset")
385 int InsetFormula::ascent(Painter &, LyXFont const &) const
387 return par->Ascent() + ((disp_flag) ? 8 : 1);
391 int InsetFormula::descent(Painter &, LyXFont const &) const
393 return par->Descent() + ((disp_flag) ? 8 : 1);
397 int InsetFormula::width(Painter &, LyXFont const & f) const
399 lfont_size = f.size();
401 return par->Width(); //+2;
405 void InsetFormula::draw(Painter & pain, LyXFont const & f,
406 int baseline, float & x) const
408 // Seems commenting out solves a problem.
409 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
410 font.setSize(f.size());
411 lfont_size = font.size();
412 /// Let's try to wait a bit with this... (Lgb)
413 //UpdatableInset::draw(pain, font, baseline, x);
415 // otherwise a segfault could occur
416 // in some XDrawRectangles (i.e. matrix) (Matthias)
417 if (mathcursor && mathcursor->GetPar() == par) {
418 if (mathcursor->Selection()) {
422 mathcursor->SelGetArea(&xp, &yp, n);
423 pain.fillPolygon(xp, yp, n, LColor::selection);
425 mathcursor->draw(pain, int(x), baseline);
427 par->draw(pain, int(x), baseline);
429 x += float(width(pain, font));
431 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
432 LyXFont font = WhichFont(LM_TC_BF, par->size);
433 font.setLatex(LyXFont::OFF);
435 if (par->GetType() == LM_OT_PARN) {
438 str = string("(") + label + ")";
441 pain.text(int(x + 20), baseline, str, font);
442 } else if (par->GetType() == LM_OT_MPARN) {
443 MathMatrixInset * mt =
444 static_cast<MathMatrixInset*>(par);
446 MathedRowSt const * crow = mt->getRowSt();
448 y = baseline + crow->getBaseline();
449 if (crow->isNumbered()) {
451 if (crow->getLabel())
452 str = string("(") + crow->getLabel() + ")";
455 pain.text(int(x + 20), y, str, font);
457 crow = crow->getNext();
461 cursor_visible = false;
465 char const * InsetFormula::EditMessage() const
467 return _("Math editor mode");
471 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
473 mathcursor = new MathedCursor(par);
474 if (!bv->lockInset(this))
475 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
477 bv->updateInset(this, false);
480 mathcursor->SetPos(x, y);
486 void InsetFormula::InsetUnlock(BufferView * bv)
489 if (mathcursor->InMacroMode()) {
490 mathcursor->MacroModeClose();
496 bv->updateInset(this, false);
500 // Now a symbol can be inserted only if the inset is locked
501 void InsetFormula::InsertSymbol(BufferView * bv, char const * s)
503 if (!s || !mathcursor) return;
504 mathcursor->Interpret(s);
509 void InsetFormula::GetCursorPos(int& x, int& y) const
511 mathcursor->GetPos(x, y);
516 void InsetFormula::ToggleInsetCursor(BufferView * bv)
522 mathcursor->GetPos(x, y);
525 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
526 int asc = lyxfont::maxAscent(font);
527 int desc = lyxfont::maxDescent(font);
530 bv->hideLockedInsetCursor();
532 bv->showLockedInsetCursor(x, y, asc, desc);
533 cursor_visible = !cursor_visible;
537 void InsetFormula::ShowInsetCursor(BufferView * bv)
539 if (!cursor_visible) {
542 mathcursor->GetPos(x, y);
545 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
546 int asc = lyxfont::maxAscent(font);
547 int desc = lyxfont::maxDescent(font);
548 bv->fitLockedInsetCursor(x, y, asc, desc);
550 ToggleInsetCursor(bv);
555 void InsetFormula::HideInsetCursor(BufferView * bv)
558 ToggleInsetCursor(bv);
562 void InsetFormula::ToggleInsetSelection(BufferView * bv)
570 //mathcursor->SelGetArea(n);
571 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
575 bv->updateInset(this, false);
580 void InsetFormula::display(bool dspf)
582 if (dspf != disp_flag) {
584 par->SetType(LM_OT_PAR);
585 par->SetStyle(LM_ST_DISPLAY);
587 if (par->GetType() >= LM_OT_MPAR) {
588 MathParInset * p = new MathParInset(par);
592 mathcursor->SetPar(par);
594 par->SetType(LM_OT_MIN);
595 par->SetStyle(LM_ST_TEXT);
596 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
605 int InsetFormula::GetNumberOfLabels() const
607 // This is dirty, I know. I'll clean it at 0.13
608 if (par->GetType() == LM_OT_MPARN) {
609 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
611 MathedRowSt const * crow = mt->getRowSt();
613 if (crow->getLabel()) ++nl;
614 crow = crow->getNext();
625 string InsetFormula::getLabel(int il) const
627 //#warning This is dirty, I know. Ill clean it at 0.11
628 // Correction, the only way to clean this is with a new kernel: 0.13.
629 if (par->GetType() == LM_OT_MPARN) {
631 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
633 MathedRowSt const * crow = mt->getRowSt();
635 if (crow->getLabel()) {
637 lab = crow->getLabel();
642 crow = crow->getNext();
650 void InsetFormula::UpdateLocal(BufferView * bv)
652 par->Metrics(); // To inform lyx kernel the exact size
653 // (there were problems with arrays).
654 bv->updateInset(this, true);
658 void InsetFormula::InsetButtonRelease(BufferView * bv,
659 int x, int y, int /*button*/)
665 mathcursor->SetPos(x, y);
670 bv->updateInset(this, false);
676 void InsetFormula::InsetButtonPress(BufferView * bv,
677 int x, int y, int /*button*/)
680 sel_x = x; sel_y = y;
681 if (mathcursor && mathcursor->Selection()) {
682 mathcursor->SelClear();
683 bv->updateInset(this, false);
688 void InsetFormula::InsetMotionNotify(BufferView * bv,
689 int x, int y, int /*button*/)
691 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
694 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
695 mathcursor->SelStart();
697 mathcursor->GetPos(sel_x, sel_y);
703 mathcursor->SetPos(x, y);
705 mathcursor->GetPos(x, y);
706 if (sel_x!= x || sel_y!= y)
707 bv->updateInset(this, false);
708 sel_x = x; sel_y = y;
713 void InsetFormula::InsetKeyPress(XKeyEvent *)
715 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
719 // Special Mathed functions
720 bool InsetFormula::SetNumber(bool numbf)
723 short type = par->GetType();
724 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
725 if (numbf && !oldf) ++type;
726 if (!numbf && oldf) --type;
734 UpdatableInset::RESULT
735 InsetFormula::LocalDispatch(BufferView * bv,
736 int action, string const & arg)
738 // extern char *dispatch_result;
739 MathedTextCodes varcode = LM_TC_MIN;
740 bool was_macro = mathcursor->InMacroMode();
742 bool space_on = false;
743 bool was_selection = mathcursor->Selection();
744 RESULT result = DISPATCHED;
745 static MathSpaceInset * sp= 0;
749 if (mathcursor->getLastCode() == LM_TC_TEX) {
754 // --- Cursor Movements ---------------------------------------------
755 case LFUN_RIGHTSEL: sel = true;
758 result = DISPATCH_RESULT(mathcursor->Right(sel));
761 case LFUN_LEFTSEL: sel = true;
764 result = DISPATCH_RESULT(mathcursor->Left(sel));
767 case LFUN_UPSEL: sel = true;
769 result = DISPATCH_RESULT(mathcursor->Up(sel));
772 case LFUN_DOWNSEL: sel = true;
774 result = DISPATCH_RESULT(mathcursor->Down(sel));
782 case LFUN_DELETE_LINE_FORWARD:
783 //current_view->lockedInsetStoreUndo(Undo::INSERT);
784 bv->lockedInsetStoreUndo(Undo::DELETE);
785 mathcursor->DelLine();
789 bv->lockedInsetStoreUndo(Undo::INSERT);
790 mathcursor->Insert(' ', LM_TC_CR);
791 par = mathcursor->GetPar();
795 bv->lockedInsetStoreUndo(Undo::INSERT);
796 mathcursor->Insert(0, LM_TC_TAB);
800 bv->lockedInsetStoreUndo(Undo::INSERT);
801 mathcursor->Insert('T', LM_TC_TAB);
805 if (!mathcursor->Left())
808 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
809 bv->updateInset(this, true);
814 //current_view->lockedInsetStoreUndo(Undo::INSERT);
815 bv->lockedInsetStoreUndo(Undo::DELETE);
816 mathcursor->Delete();
817 bv->updateInset(this, true);
820 // sprintf(dispatch_buffer, "%d %d",);
821 // dispatch_result = dispatch_buffer;
827 istringstream ist(arg.c_str());
829 istrstream ist(arg.c_str());
833 mathcursor->SetPos(x1 + x, y1 + y);
837 /* cursor selection ---------------------------- */
841 mathcursor->MacroModeClose();
842 bv->lockedInsetStoreUndo(Undo::INSERT);
843 mathcursor->SelPaste(); UpdateLocal(bv); break;
845 bv->lockedInsetStoreUndo(Undo::DELETE);
846 mathcursor->SelCut(); UpdateLocal(bv); break;
847 case LFUN_COPY: mathcursor->SelCopy(); break;
850 case LFUN_WORDRIGHTSEL:
851 case LFUN_WORDLEFTSEL:
854 // --- accented characters ------------------------------
856 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
857 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
858 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
859 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
860 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
861 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
862 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
863 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
864 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
865 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
870 if (!greek_kb_flag) {
872 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
879 case LFUN_GREEK_TOGGLE:
881 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
883 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
885 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
890 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
891 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
892 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
893 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
894 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
895 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
898 // varcode = LM_TC_TEX;
899 mathcursor->setLastCode(LM_TC_TEX);
900 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
904 case LFUN_MATH_NUMBER:
906 bv->lockedInsetStoreUndo(Undo::INSERT);
908 short type = par->GetType();
909 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
912 if (!label.empty()) {
915 bv->owner()->getMiniBuffer()->Set(_("No number"));
918 bv->owner()->getMiniBuffer()->Set(_("Number"));
926 case LFUN_MATH_NONUMBER:
928 if (par->GetType() == LM_OT_MPARN) {
929 // MathMatrixInset *mt = (MathMatrixInset*)par;
931 // mt->SetNumbered(!mt->IsNumbered());
933 mathcursor->setNumbered();
939 case LFUN_MATH_LIMITS:
941 bv->lockedInsetStoreUndo(Undo::INSERT);
942 if (mathcursor->Limits())
948 latexkeys * l = in_word_set (arg.c_str(), strlen(arg.c_str()));
949 int sz = (l) ? l->id: -1;
950 mathcursor->SetSize(sz);
955 case LFUN_INSERT_MATH:
957 bv->lockedInsetStoreUndo(Undo::INSERT);
958 InsertSymbol(bv, arg.c_str());
962 case LFUN_INSERT_MATRIX:
964 bv->lockedInsetStoreUndo(Undo::INSERT);
966 char s[80], arg2[80];
967 // This is just so that too long args won't ooze out of s.
968 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
969 k = sscanf(arg2, "%d %d %s", &m, &n, s);
978 MathMatrixInset * p = new MathMatrixInset(m, n);
979 if (mathcursor && p) {
980 if (k > 2 && int(strlen(s)) > m)
981 p->SetAlign(s[0], &s[1]);
982 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
988 case LFUN_MATH_DELIM:
990 bv->lockedInsetStoreUndo(Undo::INSERT);
991 char lf[40], rg[40], arg2[40];
992 int ilf = '(', irg = '.';
994 string vdelim("(){}[]./|");
998 strncpy(arg2, arg.c_str(), 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 bv->lockedInsetStoreUndo(Undo::INSERT);
1036 sp = new MathSpaceInset(1);
1037 mathcursor->Insert(sp);
1043 case LFUN_INSERT_LABEL:
1045 bv->lockedInsetStoreUndo(Undo::INSERT);
1046 if (par->GetType() < LM_OT_PAR) break;
1050 result = askForText(_("Enter new label to insert:"));
1055 if (!lb.empty() && lb[0] > ' ') {
1057 if (par->GetType() == LM_OT_MPARN) {
1058 mathcursor->setLabel(lb.c_str());
1059 // MathMatrixInset *mt = (MathMatrixInset*)par;
1060 // mt->SetLabel(lb);
1062 //if (label.notEmpty()) delete label;
1071 case LFUN_MATH_DISPLAY:
1072 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1073 bv->lockedInsetStoreUndo(Undo::EDIT);
1074 display(!disp_flag);
1078 // Invalid actions under math mode
1079 case LFUN_MATH_MODE:
1081 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1082 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1083 varcode = LM_TC_TEXTRM;
1085 varcode = LM_TC_VAR;
1087 mathcursor->setLastCode(varcode);
1091 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1094 //------- dummy actions
1095 case LFUN_EXEC_COMMAND:
1096 bv->owner()->getMiniBuffer()->ExecCommand();
1100 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1101 unsigned char c = arg[0];
1102 bv->lockedInsetStoreUndo(Undo::INSERT);
1104 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1106 mathcursor->setAccent(0);
1108 if (c == 0) { // Dead key, do nothing
1109 //lyxerr << "deadkey" << endl;
1113 if (mathcursor->getLastCode() == LM_TC_TEX) {
1114 mathcursor->MacroModeOpen();
1115 mathcursor->clearLastCode();
1116 varcode = LM_TC_MIN;
1119 short f = (mathcursor->getLastCode()) ?
1120 mathcursor->getLastCode() :
1121 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1122 varcode = MathIsAlphaFont(f) ?
1123 static_cast<MathedTextCodes>(f) :
1127 // lyxerr << "Varcode << vardoce;
1128 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1129 varcode = LM_TC_MIN;
1130 if (greek_kb_flag<2) greek_kb_flag = 0;
1132 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1133 mathcursor->Insert(c, LM_TC_TEX);
1135 mathcursor->Insert('}', LM_TC_TEX);
1138 mathcursor->clearLastCode();
1139 // varcode = LM_TC_MIN;
1141 if (c == '_' && varcode == LM_TC_TEX) {
1142 mathcursor->Insert(c, LM_TC_SPECIAL);
1143 mathcursor->clearLastCode();
1144 // varcode = LM_TC_MIN;
1146 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1147 mathcursor->MacroModeOpen();
1148 mathcursor->clearLastCode();
1149 mathcursor->Insert(c, LM_TC_MIN);
1152 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1153 mathcursor->Insert(c, LM_TC_CONST);
1155 if (strchr("+/-*<>=", c))
1156 mathcursor->Insert(c, LM_TC_BOP);
1158 if (strchr(latex_special_chars, c) && c!= '_')
1159 mathcursor->Insert(c, LM_TC_SPECIAL);
1161 if (c == '_' || c == '^') {
1165 mathcursor->Interpret (s);
1169 short f = (mathcursor->getLastCode()) ?
1170 mathcursor->getLastCode() :
1171 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1172 varcode = MathIsAlphaFont(f) ?
1173 static_cast<MathedTextCodes>(f) :
1176 if (varcode == LM_TC_TEXTRM) {
1177 mathcursor->Insert(c, LM_TC_TEXTRM);
1180 mathcursor->MacroModeClose();
1183 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1187 if (!mathcursor->Pop() && mathcursor->IsEnd())
1192 mathcursor->Insert (c, LM_TC_VAR);
1196 mathcursor->MacroModeClose();
1197 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1198 mathcursor->setLastCode(LM_TC_TEX);
1202 // lyxerr << "Closed by action " << action << endl;
1206 if (was_macro != mathcursor->InMacroMode()
1208 && action != LFUN_BACKSPACE)
1210 if (sp && !space_on) sp = 0;
1211 if (mathcursor->Selection() || was_selection)
1212 ToggleInsetSelection(bv);
1214 if (result == DISPATCHED)
1215 ShowInsetCursor(bv);
1217 bv->unlockInset(this);
1224 MathFuncInset::draw(Painter & pain, int x, int y)
1226 if (name && name[0] > ' ') {
1227 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1228 font.setLatex(LyXFont::ON);
1229 x += (lyxfont::width('I', font) + 3) / 4;
1230 pain.text(x, y, name, font);
1235 void MathFuncInset::Metrics()
1237 ln = (name) ? strlen(name): 0;
1238 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1239 font.setLatex(LyXFont::ON);
1240 width = lyxfont::width(name, ln, font)
1241 + lyxfont::width('I', font) / 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;