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.
311 #ifdef USE_OSTREAM_ONLY
315 mathed_write(par, output, &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());
340 int InsetFormula::Linuxdoc(string &/*file*/) const
346 int InsetFormula::DocBook(string &/*file*/) const
352 // Check if uses AMS macros
353 void InsetFormula::Validate(LaTeXFeatures & features) const
355 // Validation only necesary if not using an AMS Style
356 if (!features.amsstyle)
357 mathedValidate(features, par);
361 void InsetFormula::Read(LyXLex & lex)
363 istream & is = lex.getStream();
365 mathed_parser_file(is, lex.GetLineNo());
367 // Silly hack to read labels.
369 mathed_parse(0, 0, &par);
371 disp_flag = (par->GetType() > 0);
373 // Update line number
374 lex.setLineNo(mathed_parser_lineno());
377 label = mathed_label;
387 int InsetFormula::ascent(Painter &, LyXFont const &) const
389 return par->Ascent() + ((disp_flag) ? 8 : 1);
393 int InsetFormula::descent(Painter &, LyXFont const &) const
395 return par->Descent() + ((disp_flag) ? 8 : 1);
399 int InsetFormula::width(Painter &, LyXFont const & f) const
401 lfont_size = f.size();
403 return par->Width(); //+2;
407 void InsetFormula::draw(Painter & pain, LyXFont const &,
408 int baseline, float & x) const
410 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
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 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
468 mathcursor = new MathedCursor(par);
471 bv->updateInset(this, false);
474 mathcursor->SetPos(x, y);
480 void InsetFormula::InsetUnlock(BufferView * bv)
483 if (mathcursor->InMacroMode()) {
484 mathcursor->MacroModeClose();
490 bv->updateInset(this, false);
494 // Now a symbol can be inserted only if the inset is locked
495 void InsetFormula::InsertSymbol(BufferView * bv, char const * s)
497 if (!s || !mathcursor) return;
498 mathcursor->Interpret(s);
503 void InsetFormula::GetCursorPos(int& x, int& y) const
505 mathcursor->GetPos(x, y);
510 void InsetFormula::ToggleInsetCursor(BufferView * bv)
516 mathcursor->GetPos(x, y);
519 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
520 int asc = font.maxAscent();
521 int desc = font.maxDescent();
524 bv->hideLockedInsetCursor();
526 bv->showLockedInsetCursor(x, y, asc, desc);
527 cursor_visible = !cursor_visible;
531 void InsetFormula::ShowInsetCursor(BufferView * bv)
533 if (!cursor_visible) {
536 mathcursor->GetPos(x, y);
539 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
540 int asc = font.maxAscent();
541 int desc = font.maxDescent();
542 bv->fitLockedInsetCursor(x, y, asc, desc);
544 ToggleInsetCursor(bv);
549 void InsetFormula::HideInsetCursor(BufferView * bv)
552 ToggleInsetCursor(bv);
556 void InsetFormula::ToggleInsetSelection(BufferView * bv)
564 //mathcursor->SelGetArea(n);
565 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
569 bv->updateInset(this, false);
574 void InsetFormula::display(bool dspf)
576 if (dspf != disp_flag) {
578 par->SetType(LM_OT_PAR);
579 par->SetStyle(LM_ST_DISPLAY);
581 if (par->GetType() >= LM_OT_MPAR) {
582 MathParInset * p = new MathParInset(par);
586 mathcursor->SetPar(par);
588 par->SetType(LM_OT_MIN);
589 par->SetStyle(LM_ST_TEXT);
590 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
599 int InsetFormula::GetNumberOfLabels() const
601 // This is dirty, I know. I'll clean it at 0.13
602 if (par->GetType() == LM_OT_MPARN) {
603 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
605 MathedRowSt const * crow = mt->getRowSt();
607 if (crow->getLabel()) ++nl;
608 crow = crow->getNext();
619 string InsetFormula::getLabel(int il) const
621 //#warning This is dirty, I know. Ill clean it at 0.11
622 // Correction, the only way to clean this is with a new kernel: 0.13.
623 if (par->GetType() == LM_OT_MPARN) {
625 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
627 MathedRowSt const * crow = mt->getRowSt();
629 if (crow->getLabel()) {
631 lab = crow->getLabel();
636 crow = crow->getNext();
644 void InsetFormula::UpdateLocal(BufferView * bv)
646 par->Metrics(); // To inform lyx kernel the exact size
647 // (there were problems with arrays).
648 bv->updateInset(this, true);
652 void InsetFormula::InsetButtonRelease(BufferView * bv,
653 int x, int y, int /*button*/)
658 mathcursor->SetPos(x, y);
663 bv->updateInset(this, false);
668 void InsetFormula::InsetButtonPress(BufferView * bv,
669 int x, int y, int /*button*/)
672 sel_x = x; sel_y = y;
673 if (mathcursor->Selection()) {
674 mathcursor->SelClear();
675 bv->updateInset(this, false);
680 void InsetFormula::InsetMotionNotify(BufferView * bv,
681 int x, int y, int /*button*/)
683 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
686 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
687 mathcursor->SelStart();
689 mathcursor->GetPos(sel_x, sel_y);
695 mathcursor->SetPos(x, y);
697 mathcursor->GetPos(x, y);
698 if (sel_x!= x || sel_y!= y)
699 bv->updateInset(this, false);
700 sel_x = x; sel_y = y;
705 void InsetFormula::InsetKeyPress(XKeyEvent *)
707 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
711 // Special Mathed functions
712 bool InsetFormula::SetNumber(bool numbf)
715 short type = par->GetType();
716 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
717 if (numbf && !oldf) ++type;
718 if (!numbf && oldf) --type;
726 UpdatableInset::RESULT
727 InsetFormula::LocalDispatch(BufferView * bv,
728 int action, string const & arg)
730 // extern char *dispatch_result;
731 MathedTextCodes varcode = LM_TC_MIN;
732 bool was_macro = mathcursor->InMacroMode();
734 bool space_on = false;
735 bool was_selection = mathcursor->Selection();
736 RESULT result = DISPATCHED;
737 static MathSpaceInset * sp= 0;
741 if (mathcursor->getLastCode() == LM_TC_TEX) {
746 // --- Cursor Movements ---------------------------------------------
747 case LFUN_RIGHTSEL: sel = true;
750 result = DISPATCH_RESULT(mathcursor->Right(sel));
753 case LFUN_LEFTSEL: sel = true;
756 result = DISPATCH_RESULT(mathcursor->Left(sel));
759 case LFUN_UPSEL: sel = true;
761 result = DISPATCH_RESULT(mathcursor->Up(sel));
764 case LFUN_DOWNSEL: sel = true;
766 result = DISPATCH_RESULT(mathcursor->Down(sel));
774 case LFUN_DELETE_LINE_FORWARD:
775 //current_view->lockedInsetStoreUndo(Undo::INSERT);
776 bv->lockedInsetStoreUndo(Undo::DELETE);
777 mathcursor->DelLine();
781 bv->lockedInsetStoreUndo(Undo::INSERT);
782 mathcursor->Insert(' ', LM_TC_CR);
783 par = mathcursor->GetPar();
787 bv->lockedInsetStoreUndo(Undo::INSERT);
788 mathcursor->Insert(0, LM_TC_TAB);
792 bv->lockedInsetStoreUndo(Undo::INSERT);
793 mathcursor->Insert('T', LM_TC_TAB);
797 if (!mathcursor->Left())
800 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
801 bv->updateInset(this, true);
806 //current_view->lockedInsetStoreUndo(Undo::INSERT);
807 bv->lockedInsetStoreUndo(Undo::DELETE);
808 mathcursor->Delete();
809 bv->updateInset(this, true);
812 // sprintf(dispatch_buffer, "%d %d",);
813 // dispatch_result = dispatch_buffer;
818 sscanf(arg.c_str(), "%d %d", &x, &y);
820 mathcursor->SetPos(x1+x, y1+y);
824 /* cursor selection ---------------------------- */
828 mathcursor->MacroModeClose();
829 bv->lockedInsetStoreUndo(Undo::INSERT);
830 mathcursor->SelPaste(); UpdateLocal(bv); break;
832 bv->lockedInsetStoreUndo(Undo::DELETE);
833 mathcursor->SelCut(); UpdateLocal(bv); break;
834 case LFUN_COPY: mathcursor->SelCopy(); break;
837 case LFUN_WORDRIGHTSEL:
838 case LFUN_WORDLEFTSEL:
841 // --- accented characters ------------------------------
843 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
844 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
845 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
846 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
847 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
848 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
849 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
850 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
851 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
852 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
857 if (!greek_kb_flag) {
859 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
866 case LFUN_GREEK_TOGGLE:
868 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
870 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
872 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
877 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
878 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
879 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
880 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
881 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
882 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
885 // varcode = LM_TC_TEX;
886 mathcursor->setLastCode(LM_TC_TEX);
887 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
891 case LFUN_MATH_NUMBER:
893 bv->lockedInsetStoreUndo(Undo::INSERT);
895 short type = par->GetType();
896 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
899 if (!label.empty()) {
902 bv->owner()->getMiniBuffer()->Set(_("No number"));
905 bv->owner()->getMiniBuffer()->Set(_("Number"));
913 case LFUN_MATH_NONUMBER:
915 if (par->GetType() == LM_OT_MPARN) {
916 // MathMatrixInset *mt = (MathMatrixInset*)par;
918 // mt->SetNumbered(!mt->IsNumbered());
920 mathcursor->setNumbered();
926 case LFUN_MATH_LIMITS:
928 bv->lockedInsetStoreUndo(Undo::INSERT);
929 if (mathcursor->Limits())
935 latexkeys * l = in_word_set (arg.c_str(), strlen(arg.c_str()));
936 int sz = (l) ? l->id: -1;
937 mathcursor->SetSize(sz);
942 case LFUN_INSERT_MATH:
944 bv->lockedInsetStoreUndo(Undo::INSERT);
945 InsertSymbol(bv, arg.c_str());
949 case LFUN_INSERT_MATRIX:
951 bv->lockedInsetStoreUndo(Undo::INSERT);
953 char s[80], arg2[80];
954 // This is just so that too long args won't ooze out of s.
955 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
956 k = sscanf(arg2, "%d %d %s", &m, &n, s);
965 MathMatrixInset * p = new MathMatrixInset(m, n);
966 if (mathcursor && p) {
967 if (k > 2 && int(strlen(s)) > m)
968 p->SetAlign(s[0], &s[1]);
969 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
975 case LFUN_MATH_DELIM:
977 bv->lockedInsetStoreUndo(Undo::INSERT);
978 char lf[40], rg[40], arg2[40];
979 int ilf = '(', irg = '.';
981 string vdelim("(){}[]./|");
985 strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
986 int n = sscanf(arg2, "%s %s", lf, rg);
987 lf[39] = '\0'; rg[39] = '\0';
994 l = in_word_set(lf, strlen(lf));
995 // Long words will cause l == 0; so check.
998 if (vdelim.find(lf[0]) != string::npos)
1006 l = in_word_set(rg, strlen(rg));
1009 if (vdelim.find(rg[0]) != string::npos)
1014 MathDelimInset * p = new MathDelimInset(ilf, irg);
1015 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1020 case LFUN_PROTECTEDSPACE:
1022 bv->lockedInsetStoreUndo(Undo::INSERT);
1023 sp = new MathSpaceInset(1);
1024 mathcursor->Insert(sp);
1030 case LFUN_INSERT_LABEL:
1032 bv->lockedInsetStoreUndo(Undo::INSERT);
1033 if (par->GetType() < LM_OT_PAR) break;
1037 result = askForText(_("Enter new label to insert:"));
1042 if (!lb.empty() && lb[0] > ' ') {
1044 if (par->GetType() == LM_OT_MPARN) {
1045 mathcursor->setLabel(lb.c_str());
1046 // MathMatrixInset *mt = (MathMatrixInset*)par;
1047 // mt->SetLabel(lb);
1049 //if (label.notEmpty()) delete label;
1058 case LFUN_MATH_DISPLAY:
1059 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1060 bv->lockedInsetStoreUndo(Undo::EDIT);
1061 display(!disp_flag);
1065 // Invalid actions under math mode
1066 case LFUN_MATH_MODE:
1068 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1069 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1070 varcode = LM_TC_TEXTRM;
1072 varcode = LM_TC_VAR;
1074 mathcursor->setLastCode(varcode);
1078 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1081 //------- dummy actions
1082 case LFUN_EXEC_COMMAND:
1083 bv->owner()->getMiniBuffer()->ExecCommand();
1087 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1088 unsigned char c = arg[0];
1089 bv->lockedInsetStoreUndo(Undo::INSERT);
1091 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1093 mathcursor->setAccent(0);
1095 if (c == 0) { // Dead key, do nothing
1096 //lyxerr << "deadkey" << endl;
1100 if (mathcursor->getLastCode() == LM_TC_TEX) {
1101 mathcursor->MacroModeOpen();
1102 mathcursor->clearLastCode();
1103 varcode = LM_TC_MIN;
1106 short f = (mathcursor->getLastCode()) ?
1107 mathcursor->getLastCode() :
1108 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1109 varcode = MathIsAlphaFont(f) ?
1110 static_cast<MathedTextCodes>(f) :
1114 // lyxerr << "Varcode << vardoce;
1115 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1116 varcode = LM_TC_MIN;
1117 if (greek_kb_flag<2) greek_kb_flag = 0;
1119 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1120 mathcursor->Insert(c, LM_TC_TEX);
1122 mathcursor->Insert('}', LM_TC_TEX);
1125 mathcursor->clearLastCode();
1126 // varcode = LM_TC_MIN;
1128 if (c == '_' && varcode == LM_TC_TEX) {
1129 mathcursor->Insert(c, LM_TC_SPECIAL);
1130 mathcursor->clearLastCode();
1131 // varcode = LM_TC_MIN;
1133 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1134 mathcursor->MacroModeOpen();
1135 mathcursor->clearLastCode();
1136 mathcursor->Insert(c, LM_TC_MIN);
1139 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1140 mathcursor->Insert(c, LM_TC_CONST);
1142 if (strchr("+/-*<>=", c))
1143 mathcursor->Insert(c, LM_TC_BOP);
1145 if (strchr(latex_special_chars, c) && c!= '_')
1146 mathcursor->Insert(c, LM_TC_SPECIAL);
1148 if (c == '_' || c == '^') {
1152 mathcursor->Interpret (s);
1156 short f = (mathcursor->getLastCode()) ?
1157 mathcursor->getLastCode() :
1158 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1159 varcode = MathIsAlphaFont(f) ?
1160 static_cast<MathedTextCodes>(f) :
1163 if (varcode == LM_TC_TEXTRM) {
1164 mathcursor->Insert(c, LM_TC_TEXTRM);
1167 mathcursor->MacroModeClose();
1170 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1174 if (!mathcursor->Pop() && mathcursor->IsEnd())
1179 mathcursor->Insert (c, LM_TC_VAR);
1183 mathcursor->MacroModeClose();
1184 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1185 mathcursor->setLastCode(LM_TC_TEX);
1189 // lyxerr << "Closed by action " << action << endl;
1193 if (was_macro != mathcursor->InMacroMode()
1195 && action != LFUN_BACKSPACE)
1197 if (sp && !space_on) sp = 0;
1198 if (mathcursor->Selection() || was_selection)
1199 ToggleInsetSelection(bv);
1201 if (result == DISPATCHED)
1202 ShowInsetCursor(bv);
1204 bv->unlockInset(this);
1211 MathFuncInset::draw(Painter & pain, int x, int y)
1213 if (name && name[0] > ' ') {
1214 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1215 font.setLatex(LyXFont::ON);
1216 x += (font.textWidth("I", 1) + 3) / 4;
1217 pain.text(x, y, name, font);
1222 void MathFuncInset::Metrics()
1224 ln = (name) ? strlen(name): 0;
1225 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1226 font.setLatex(LyXFont::ON);
1227 width = font.textWidth(name, ln) + font.textWidth("I", 1)/2;
1228 mathed_string_height(LM_TC_TEXTRM, size,
1229 reinterpret_cast<unsigned char const *>(name),
1230 strlen(name), ascent, descent);
1235 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1237 MathedIter it(par->GetData());
1239 while (it.OK() && !(features.binom && features.boldsymbol)) {
1242 MathParInset * p = it.GetActiveInset();
1243 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1244 strcmp(p->GetName(), "binom") == 0) {
1245 features.binom = true;
1247 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1248 p->setArgumentIdx(i);
1249 mathedValidate(features, p);
1253 MathedInset* p = it.GetInset();
1254 if (!features.boldsymbol && p->GetName() &&
1255 strcmp(p->GetName(), "boldsymbol") == 0) {
1256 features.boldsymbol = true;