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"
52 extern char * mathed_label;
54 extern char const * latex_special_chars;
56 short greek_kb_flag = 0;
58 LyXFont * Math_Fonts = 0; // this is only used by Whichfont and mathed_init_fonts (Lgb)
60 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
63 static int sel_x, sel_y;
65 MathedCursor * InsetFormula::mathcursor = 0;
68 int MathedInset::df_asc;
69 int MathedInset::df_des;
70 int MathedInset::df_width;
74 bool IsMacro(short token, int id)
76 return (token != LM_TK_FRAC && token != LM_TK_SQRT &&
77 !((token == LM_TK_SYM || token == LM_TC_BSYM) && id < 255));
82 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
85 LyXFont WhichFont(short type, int size)
115 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
125 f.setSize(lfont_size);
129 if (type == LM_TC_BSYM) {
139 case LM_ST_SCRIPTSCRIPT:
144 lyxerr << "Mathed Error: wrong font size: " << size << endl;
148 if (type != LM_TC_TEXTRM)
149 f.setColor(LColor::math);
154 void mathed_init_fonts() //removed 'static' because DEC cxx does not
156 // Probably because this func is declared as a friend in math_defs.h
160 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
162 for (int i = 0 ; i < 8 ; ++i) {
163 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
165 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
167 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
169 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
170 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
172 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
174 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
175 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
177 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
179 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
181 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
183 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
184 MathedInset::df_asc = lyxfont::maxAscent(f);
185 MathedInset::df_des = lyxfont::maxDescent(f);
186 MathedInset::df_width = lyxfont::width('I', f);
190 LyXFont mathed_get_font(short type, int size)
192 LyXFont f = WhichFont(type, size);
193 if (type == LM_TC_TEX) {
194 f.setLatex(LyXFont::ON);
200 int mathed_string_width(short type, int size, byte const * s, int ls)
202 LyXFont f = WhichFont(type, size);
205 if (MathIsBinary(type)) {
207 for (int i = 0; i < ls && i < 75; ++i) {
216 return lyxfont::width(reinterpret_cast<char const *>(s), ls, f);
220 int mathed_char_width(short type, int size, byte c)
222 int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1) :
223 lyxfont::width(c, WhichFont(type, size));
228 int mathed_string_height(short type, int size, byte const * s,
229 int ls, int & asc, int & des)
231 LyXFont font = WhichFont(type, size);
233 for (int i = 0; i < ls; ++i) {
234 if (lyxfont::descent(s[i], font) > des)
235 des = lyxfont::descent(s[i], font);
236 if (lyxfont::ascent(s[i], font) > asc)
237 asc = lyxfont::ascent(s[i], font);
243 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
245 LyXFont font = WhichFont(type, size);
247 des = lyxfont::descent(c, font);
248 asc = lyxfont::ascent(c, font);
253 // In a near future maybe we use a better fonts renderer
254 void MathedInset::drawStr(Painter & pain, short type, int siz,
255 int x, int y, byte const * s, int ls)
258 if (MathIsBinary(type)) {
259 for (int i = 0; i < ls; ++i) {
260 st += string(" ") + char(s[i]) + ' ';
263 st = string(reinterpret_cast<char const *>(s), ls);
265 LyXFont mf = mathed_get_font(type, siz);
266 pain.text(x, y, st, mf);
270 InsetFormula::InsetFormula(bool display)
272 par = new MathParInset; // this leaks
277 par->SetType(LM_OT_PAR);
278 par->SetStyle(LM_ST_DISPLAY);
283 InsetFormula::InsetFormula(MathParInset * p)
285 par = (p->GetType()>= LM_OT_MPAR) ?
286 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
290 disp_flag = (par->GetType()>0);
295 InsetFormula::~InsetFormula()
301 Inset * InsetFormula::Clone() const
303 InsetFormula * f = new InsetFormula(par);
309 void InsetFormula::Write(ostream & os) const
312 Latex(os, false, false);
316 int InsetFormula::Latex(ostream & os, bool fragile, bool) const
319 //#warning Alejandro, the number of lines is not returned in this case
320 // This problem will disapear at 0.13.
321 mathed_write(par, os, &ret, fragile, label.c_str());
326 int InsetFormula::Ascii(ostream & os) const
328 par->Write(os, false);
333 int InsetFormula::Linuxdoc(ostream &) const
339 int InsetFormula::DocBook(ostream&) const
345 // Check if uses AMS macros
346 void InsetFormula::Validate(LaTeXFeatures & features) const
348 // Validation only necesary if not using an AMS Style
349 if (!features.amsstyle)
350 mathedValidate(features, par);
354 void InsetFormula::Read(LyXLex & lex)
356 istream & is = lex.getStream();
358 mathed_parser_file(is, lex.GetLineNo());
360 // Silly hack to read labels.
362 mathed_parse(0, 0, &par);
364 disp_flag = (par->GetType() > 0);
366 // Update line number
367 lex.setLineNo(mathed_parser_lineno());
370 label = mathed_label;
373 // reading of end_inset in the inset!!!
376 if (lex.GetString() == "\\end_inset")
386 int InsetFormula::ascent(Painter &, LyXFont const &) const
388 return par->Ascent() + ((disp_flag) ? 8 : 1);
392 int InsetFormula::descent(Painter &, LyXFont const &) const
394 return par->Descent() + ((disp_flag) ? 8 : 1);
398 int InsetFormula::width(Painter &, LyXFont const & f) const
400 lfont_size = f.size();
402 return par->Width(); //+2;
406 void InsetFormula::draw(Painter & pain, LyXFont const & f,
407 int baseline, float & x) const
409 // Seems commenting out solves a problem.
410 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
411 font.setSize(f.size());
412 lfont_size = font.size();
413 /// Let's try to wait a bit with this... (Lgb)
414 //UpdatableInset::draw(pain, font, baseline, x);
416 // otherwise a segfault could occur
417 // in some XDrawRectangles (i.e. matrix) (Matthias)
418 if (mathcursor && mathcursor->GetPar() == par) {
419 if (mathcursor->Selection()) {
423 mathcursor->SelGetArea(&xp, &yp, n);
424 pain.fillPolygon(xp, yp, n, LColor::selection);
426 mathcursor->draw(pain, int(x), baseline);
428 par->draw(pain, int(x), baseline);
430 x += float(width(pain, font));
432 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
433 LyXFont font = WhichFont(LM_TC_BF, par->size);
434 font.setLatex(LyXFont::OFF);
436 if (par->GetType() == LM_OT_PARN) {
439 str = string("(") + label + ")";
442 pain.text(int(x + 20), baseline, str, font);
443 } else if (par->GetType() == LM_OT_MPARN) {
444 MathMatrixInset * mt =
445 static_cast<MathMatrixInset*>(par);
447 MathedRowSt const * crow = mt->getRowSt();
449 y = baseline + crow->getBaseline();
450 if (crow->isNumbered()) {
452 if (crow->getLabel())
453 str = string("(") + crow->getLabel() + ")";
456 pain.text(int(x + 20), y, str, font);
458 crow = crow->getNext();
462 cursor_visible = false;
466 char const * InsetFormula::EditMessage() const
468 return _("Math editor mode");
472 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
474 mathcursor = new MathedCursor(par);
475 if (!bv->lockInset(this))
476 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
478 bv->updateInset(this, false);
481 mathcursor->SetPos(x, y);
487 void InsetFormula::InsetUnlock(BufferView * bv)
490 if (mathcursor->InMacroMode()) {
491 mathcursor->MacroModeClose();
497 bv->updateInset(this, false);
501 // Now a symbol can be inserted only if the inset is locked
502 void InsetFormula::InsertSymbol(BufferView * bv, char const * s)
504 if (!s || !mathcursor) return;
505 mathcursor->Interpret(s);
510 void InsetFormula::GetCursorPos(int& x, int& y) const
512 mathcursor->GetPos(x, y);
517 void InsetFormula::ToggleInsetCursor(BufferView * bv)
523 mathcursor->GetPos(x, y);
526 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
527 int asc = lyxfont::maxAscent(font);
528 int desc = lyxfont::maxDescent(font);
531 bv->hideLockedInsetCursor();
533 bv->showLockedInsetCursor(x, y, asc, desc);
534 cursor_visible = !cursor_visible;
538 void InsetFormula::ShowInsetCursor(BufferView * bv)
540 if (!cursor_visible) {
543 mathcursor->GetPos(x, y);
546 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
547 int asc = lyxfont::maxAscent(font);
548 int desc = lyxfont::maxDescent(font);
549 bv->fitLockedInsetCursor(x, y, asc, desc);
551 ToggleInsetCursor(bv);
556 void InsetFormula::HideInsetCursor(BufferView * bv)
559 ToggleInsetCursor(bv);
563 void InsetFormula::ToggleInsetSelection(BufferView * bv)
571 //mathcursor->SelGetArea(n);
572 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
576 bv->updateInset(this, false);
581 void InsetFormula::display(bool dspf)
583 if (dspf != disp_flag) {
585 par->SetType(LM_OT_PAR);
586 par->SetStyle(LM_ST_DISPLAY);
588 if (par->GetType() >= LM_OT_MPAR) {
589 MathParInset * p = new MathParInset(par);
593 mathcursor->SetPar(par);
595 par->SetType(LM_OT_MIN);
596 par->SetStyle(LM_ST_TEXT);
597 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
606 vector<string> InsetFormula::getLabelList() const
608 //#warning This is dirty, I know. Ill clean it at 0.11
609 // Correction, the only way to clean this is with a new kernel: 0.13.
611 vector<string> label_list;
613 if (par->GetType() == LM_OT_MPARN) {
614 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
615 MathedRowSt const * crow = mt->getRowSt();
617 if (crow->getLabel())
618 label_list.push_back(crow->getLabel());
619 crow = crow->getNext();
621 } else if (!label.empty())
622 label_list.push_back(label);
628 void InsetFormula::UpdateLocal(BufferView * bv)
630 par->Metrics(); // To inform lyx kernel the exact size
631 // (there were problems with arrays).
632 bv->updateInset(this, true);
636 void InsetFormula::InsetButtonRelease(BufferView * bv,
637 int x, int y, int /*button*/)
643 mathcursor->SetPos(x, y);
648 bv->updateInset(this, false);
654 void InsetFormula::InsetButtonPress(BufferView * bv,
655 int x, int y, int /*button*/)
658 sel_x = x; sel_y = y;
659 if (mathcursor && mathcursor->Selection()) {
660 mathcursor->SelClear();
661 bv->updateInset(this, false);
666 void InsetFormula::InsetMotionNotify(BufferView * bv,
667 int x, int y, int /*button*/)
669 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
672 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
673 mathcursor->SelStart();
675 mathcursor->GetPos(sel_x, sel_y);
681 mathcursor->SetPos(x, y);
683 mathcursor->GetPos(x, y);
684 if (sel_x!= x || sel_y!= y)
685 bv->updateInset(this, false);
686 sel_x = x; sel_y = y;
691 void InsetFormula::InsetKeyPress(XKeyEvent *)
693 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
697 // Special Mathed functions
698 bool InsetFormula::SetNumber(bool numbf)
701 short type = par->GetType();
702 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
703 if (numbf && !oldf) ++type;
704 if (!numbf && oldf) --type;
712 UpdatableInset::RESULT
713 InsetFormula::LocalDispatch(BufferView * bv,
714 int action, string const & arg)
716 // extern char *dispatch_result;
717 MathedTextCodes varcode = LM_TC_MIN;
718 bool was_macro = mathcursor->InMacroMode();
720 bool space_on = false;
721 bool was_selection = mathcursor->Selection();
722 RESULT result = DISPATCHED;
723 static MathSpaceInset * sp= 0;
727 if (mathcursor->getLastCode() == LM_TC_TEX) {
732 // --- Cursor Movements ---------------------------------------------
733 case LFUN_RIGHTSEL: sel = true;
736 result = DISPATCH_RESULT(mathcursor->Right(sel));
739 case LFUN_LEFTSEL: sel = true;
742 result = DISPATCH_RESULT(mathcursor->Left(sel));
745 case LFUN_UPSEL: sel = true;
747 result = DISPATCH_RESULT(mathcursor->Up(sel));
750 case LFUN_DOWNSEL: sel = true;
752 result = DISPATCH_RESULT(mathcursor->Down(sel));
760 case LFUN_DELETE_LINE_FORWARD:
761 //current_view->lockedInsetStoreUndo(Undo::INSERT);
762 bv->lockedInsetStoreUndo(Undo::DELETE);
763 mathcursor->DelLine();
767 bv->lockedInsetStoreUndo(Undo::INSERT);
768 mathcursor->Insert(' ', LM_TC_CR);
769 par = mathcursor->GetPar();
773 bv->lockedInsetStoreUndo(Undo::INSERT);
774 mathcursor->Insert(0, LM_TC_TAB);
778 bv->lockedInsetStoreUndo(Undo::INSERT);
779 mathcursor->Insert('T', LM_TC_TAB);
783 if (!mathcursor->Left())
786 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
787 bv->updateInset(this, true);
792 //current_view->lockedInsetStoreUndo(Undo::INSERT);
793 bv->lockedInsetStoreUndo(Undo::DELETE);
794 mathcursor->Delete();
795 bv->updateInset(this, true);
798 // sprintf(dispatch_buffer, "%d %d",);
799 // dispatch_result = dispatch_buffer;
805 istringstream ist(arg.c_str());
807 istrstream ist(arg.c_str());
811 mathcursor->SetPos(x1 + x, y1 + y);
815 /* cursor selection ---------------------------- */
819 mathcursor->MacroModeClose();
820 bv->lockedInsetStoreUndo(Undo::INSERT);
821 mathcursor->SelPaste(); UpdateLocal(bv); break;
823 bv->lockedInsetStoreUndo(Undo::DELETE);
824 mathcursor->SelCut(); UpdateLocal(bv); break;
825 case LFUN_COPY: mathcursor->SelCopy(); break;
828 case LFUN_WORDRIGHTSEL:
829 case LFUN_WORDLEFTSEL:
832 // --- accented characters ------------------------------
834 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
835 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
836 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
837 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
838 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
839 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
840 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
841 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
842 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
843 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
848 if (!greek_kb_flag) {
850 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
857 case LFUN_GREEK_TOGGLE:
859 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
861 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
863 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
868 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
869 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
870 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
871 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
872 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
873 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
876 // varcode = LM_TC_TEX;
877 mathcursor->setLastCode(LM_TC_TEX);
878 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
882 case LFUN_MATH_NUMBER:
884 bv->lockedInsetStoreUndo(Undo::INSERT);
886 short type = par->GetType();
887 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
890 if (!label.empty()) {
893 bv->owner()->getMiniBuffer()->Set(_("No number"));
896 bv->owner()->getMiniBuffer()->Set(_("Number"));
904 case LFUN_MATH_NONUMBER:
906 if (par->GetType() == LM_OT_MPARN) {
907 // MathMatrixInset *mt = (MathMatrixInset*)par;
909 // mt->SetNumbered(!mt->IsNumbered());
911 mathcursor->setNumbered();
917 case LFUN_MATH_LIMITS:
919 bv->lockedInsetStoreUndo(Undo::INSERT);
920 if (mathcursor->Limits())
926 latexkeys * l = in_word_set (arg.c_str(), strlen(arg.c_str()));
927 int sz = (l) ? l->id: -1;
928 mathcursor->SetSize(sz);
933 case LFUN_INSERT_MATH:
935 bv->lockedInsetStoreUndo(Undo::INSERT);
936 InsertSymbol(bv, arg.c_str());
940 case LFUN_INSERT_MATRIX:
942 bv->lockedInsetStoreUndo(Undo::INSERT);
944 char s[80], arg2[80];
945 // This is just so that too long args won't ooze out of s.
946 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
947 k = sscanf(arg2, "%d %d %s", &m, &n, s);
956 MathMatrixInset * p = new MathMatrixInset(m, n);
957 if (mathcursor && p) {
958 if (k > 2 && int(strlen(s)) > m)
959 p->SetAlign(s[0], &s[1]);
960 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
966 case LFUN_MATH_DELIM:
968 bv->lockedInsetStoreUndo(Undo::INSERT);
969 char lf[40], rg[40], arg2[40];
970 int ilf = '(', irg = '.';
972 string vdelim("(){}[]./|");
976 strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
977 int n = sscanf(arg2, "%s %s", lf, rg);
978 lf[39] = '\0'; rg[39] = '\0';
985 l = in_word_set(lf, strlen(lf));
986 // Long words will cause l == 0; so check.
989 if (vdelim.find(lf[0]) != string::npos)
997 l = in_word_set(rg, strlen(rg));
1000 if (vdelim.find(rg[0]) != string::npos)
1005 MathDelimInset * p = new MathDelimInset(ilf, irg);
1006 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1011 case LFUN_PROTECTEDSPACE:
1013 bv->lockedInsetStoreUndo(Undo::INSERT);
1014 sp = new MathSpaceInset(1);
1015 mathcursor->Insert(sp);
1021 case LFUN_INSERT_LABEL:
1023 bv->lockedInsetStoreUndo(Undo::INSERT);
1024 if (par->GetType() < LM_OT_PAR) break;
1028 result = askForText(_("Enter new label to insert:"));
1033 if (!lb.empty() && lb[0] > ' ') {
1035 if (par->GetType() == LM_OT_MPARN) {
1036 mathcursor->setLabel(lb.c_str());
1037 // MathMatrixInset *mt = (MathMatrixInset*)par;
1038 // mt->SetLabel(lb);
1040 //if (label.notEmpty()) delete label;
1049 case LFUN_MATH_DISPLAY:
1050 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1051 bv->lockedInsetStoreUndo(Undo::EDIT);
1052 display(!disp_flag);
1056 // Invalid actions under math mode
1057 case LFUN_MATH_MODE:
1059 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1060 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1061 varcode = LM_TC_TEXTRM;
1063 varcode = LM_TC_VAR;
1065 mathcursor->setLastCode(varcode);
1069 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1072 //------- dummy actions
1073 case LFUN_EXEC_COMMAND:
1074 bv->owner()->getMiniBuffer()->ExecCommand();
1078 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1079 unsigned char c = arg[0];
1080 bv->lockedInsetStoreUndo(Undo::INSERT);
1082 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1084 mathcursor->setAccent(0);
1086 if (c == 0) { // Dead key, do nothing
1087 //lyxerr << "deadkey" << endl;
1091 if (mathcursor->getLastCode() == LM_TC_TEX) {
1092 mathcursor->MacroModeOpen();
1093 mathcursor->clearLastCode();
1094 varcode = LM_TC_MIN;
1097 short f = (mathcursor->getLastCode()) ?
1098 mathcursor->getLastCode() :
1099 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1100 varcode = MathIsAlphaFont(f) ?
1101 static_cast<MathedTextCodes>(f) :
1105 // lyxerr << "Varcode << vardoce;
1106 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1107 varcode = LM_TC_MIN;
1108 if (greek_kb_flag<2) greek_kb_flag = 0;
1110 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1111 mathcursor->Insert(c, LM_TC_TEX);
1113 mathcursor->Insert('}', LM_TC_TEX);
1116 mathcursor->clearLastCode();
1117 // varcode = LM_TC_MIN;
1119 if (c == '_' && varcode == LM_TC_TEX) {
1120 mathcursor->Insert(c, LM_TC_SPECIAL);
1121 mathcursor->clearLastCode();
1122 // varcode = LM_TC_MIN;
1124 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1125 mathcursor->MacroModeOpen();
1126 mathcursor->clearLastCode();
1127 mathcursor->Insert(c, LM_TC_MIN);
1130 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1131 mathcursor->Insert(c, LM_TC_CONST);
1133 if (strchr("+/-*<>=", c))
1134 mathcursor->Insert(c, LM_TC_BOP);
1136 if (strchr(latex_special_chars, c) && c!= '_')
1137 mathcursor->Insert(c, LM_TC_SPECIAL);
1139 if (c == '_' || c == '^') {
1143 mathcursor->Interpret (s);
1147 short f = (mathcursor->getLastCode()) ?
1148 mathcursor->getLastCode() :
1149 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1150 varcode = MathIsAlphaFont(f) ?
1151 static_cast<MathedTextCodes>(f) :
1154 if (varcode == LM_TC_TEXTRM) {
1155 mathcursor->Insert(c, LM_TC_TEXTRM);
1158 mathcursor->MacroModeClose();
1161 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1165 if (!mathcursor->Pop() && mathcursor->IsEnd())
1170 mathcursor->Insert (c, LM_TC_VAR);
1174 mathcursor->MacroModeClose();
1175 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1176 mathcursor->setLastCode(LM_TC_TEX);
1180 // lyxerr << "Closed by action " << action << endl;
1184 if (was_macro != mathcursor->InMacroMode()
1186 && action != LFUN_BACKSPACE)
1188 if (sp && !space_on) sp = 0;
1189 if (mathcursor->Selection() || was_selection)
1190 ToggleInsetSelection(bv);
1192 if (result == DISPATCHED)
1193 ShowInsetCursor(bv);
1195 bv->unlockInset(this);
1202 MathFuncInset::draw(Painter & pain, int x, int y)
1204 if (name && name[0] > ' ') {
1205 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1206 font.setLatex(LyXFont::ON);
1207 x += (lyxfont::width('I', font) + 3) / 4;
1208 pain.text(x, y, name, font);
1213 void MathFuncInset::Metrics()
1215 ln = (name) ? strlen(name): 0;
1216 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1217 font.setLatex(LyXFont::ON);
1218 width = lyxfont::width(name, ln, font)
1219 + lyxfont::width('I', font) / 2;
1220 mathed_string_height(LM_TC_TEXTRM, size,
1221 reinterpret_cast<unsigned char const *>(name),
1222 strlen(name), ascent, descent);
1227 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1229 MathedIter it(par->GetData());
1231 while (it.OK() && !(features.binom && features.boldsymbol)) {
1234 MathParInset * p = it.GetActiveInset();
1235 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1236 strcmp(p->GetName(), "binom") == 0) {
1237 features.binom = true;
1239 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1240 p->setArgumentIdx(i);
1241 mathedValidate(features, p);
1245 MathedInset* p = it.GetInset();
1246 if (!features.boldsymbol && p->GetName() &&
1247 strcmp(p->GetName(), "boldsymbol") == 0) {
1248 features.boldsymbol = true;