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.
19 #pragma implementation "formula.h"
23 #include "commandtags.h"
24 #include "math_cursor.h"
25 #include "math_parser.h"
27 #include "minibuffer.h"
28 #include "BufferView.h"
31 #include "LaTeXFeatures.h"
33 #include "lyx_gui_misc.h"
34 #include "support/LOstream.h"
38 extern char * mathed_label;
40 extern char const * latex_special_chars;
42 short greek_kb_flag = 0;
44 LyXFont * Math_Fonts = 0; // this is only used by Whichfont and mathed_init_fonts (Lgb)
46 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
49 static int sel_x, sel_y;
51 MathedCursor * InsetFormula::mathcursor = 0;
54 int MathedInset::df_asc;
55 int MathedInset::df_des;
56 int MathedInset::df_width;
60 bool IsMacro(short token, int id)
62 return (token != LM_TK_FRAC && token != LM_TK_SQRT &&
63 !((token == LM_TK_SYM || token == LM_TC_BSYM) && id < 255));
68 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
71 LyXFont WhichFont(short type, int size)
101 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
111 f.setSize(lfont_size);
115 if (type == LM_TC_BSYM) {
125 case LM_ST_SCRIPTSCRIPT:
130 lyxerr << "Mathed Error: wrong font size: " << size << endl;
134 if (type != LM_TC_TEXTRM)
135 f.setColor(LColor::math);
140 void mathed_init_fonts() //removed 'static' because DEC cxx does not
142 // Probably because this func is declared as a friend in math_defs.h
146 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
148 for (int i = 0 ; i < 8 ; ++i) {
149 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
151 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
153 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
155 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
156 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
158 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
160 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
161 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
163 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
165 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
167 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
169 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
170 MathedInset::df_asc = f.maxAscent();
171 MathedInset::df_des = f.maxDescent();
172 MathedInset::df_width = f.width('I');
176 LyXFont mathed_get_font(short type, int size)
178 LyXFont f = WhichFont(type, size);
179 if (type == LM_TC_TEX) {
180 f.setLatex(LyXFont::ON);
186 int mathed_string_width(short type, int size, byte const * s, int ls)
188 LyXFont f = WhichFont(type, size);
191 if (MathIsBinary(type)) {
193 for (int i = 0; i < ls && i < 75; ++i) {
202 return f.textWidth(reinterpret_cast<char const *>(s), ls);
206 int mathed_char_width(short type, int size, byte c)
208 int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1):
209 WhichFont(type, size).width(c);
214 int mathed_string_height(short type, int size, byte const * s,
215 int ls, int & asc, int & des)
217 LyXFont font = WhichFont(type, size);
219 for (int i = 0; i < ls; ++i) {
220 if (font.descent(s[i]) > des)
221 des = font.descent(s[i]);
222 if (font.ascent(s[i]) > asc)
223 asc = font.ascent(s[i]);
229 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
231 LyXFont font = WhichFont(type, size);
233 des = font.descent(c);
234 asc = font.ascent(c);
239 // In a near future maybe we use a better fonts renderer
240 void MathedInset::drawStr(Painter & pain, short type, int size,
241 int x, int y, byte const * s, int ls)
244 if (MathIsBinary(type)) {
245 for (int i = 0; i < ls; ++i) {
246 st += string(" ") + char(s[i]) + ' ';
249 st = string(reinterpret_cast<char const *>(s), ls);
251 LyXFont mf = mathed_get_font(type, size);
252 pain.text(x, y, st, mf);
256 InsetFormula::InsetFormula(bool display)
258 par = new MathParInset; // this leaks
263 par->SetType(LM_OT_PAR);
264 par->SetStyle(LM_ST_DISPLAY);
269 InsetFormula::InsetFormula(MathParInset * p)
271 par = (p->GetType()>= LM_OT_MPAR) ?
272 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
276 disp_flag = (par->GetType()>0);
281 InsetFormula::~InsetFormula()
287 Inset * InsetFormula::Clone() const
289 InsetFormula * f = new InsetFormula(par);
295 void InsetFormula::Write(ostream & os) const
302 int InsetFormula::Latex(ostream & os, signed char fragile,
303 #ifdef USE_OSTREAM_ONLY
310 //#warning Alejandro, the number of lines is not returned in this case
311 // This problem will disapear at 0.13.
312 #ifdef USE_OSTREAM_ONLY
316 mathed_write(par, os, &ret, fragile, label.c_str());
320 InsetFormula::Latex(output, fragile, free_spc);
327 #ifndef USE_OSTREAM_ONLY
328 int InsetFormula::Latex(string & file, signed char fragile) const
331 //#warning Alejandro, the number of lines is not returned in this case
332 // This problem will disapear at 0.13.
336 mathed_write(par, file, &ret, fragile, label.c_str());
341 int InsetFormula::Linuxdoc(string &/*file*/) const
347 int InsetFormula::DocBook(string &/*file*/) const
354 int InsetFormula::Linuxdoc(ostream &) const
360 int InsetFormula::DocBook(ostream&) const
367 // Check if uses AMS macros
368 void InsetFormula::Validate(LaTeXFeatures & features) const
370 // Validation only necesary if not using an AMS Style
371 if (!features.amsstyle)
372 mathedValidate(features, par);
376 void InsetFormula::Read(LyXLex & lex)
378 istream & is = lex.getStream();
380 mathed_parser_file(is, lex.GetLineNo());
382 // Silly hack to read labels.
384 mathed_parse(0, 0, &par);
386 disp_flag = (par->GetType() > 0);
388 // Update line number
389 lex.setLineNo(mathed_parser_lineno());
392 label = mathed_label;
402 int InsetFormula::ascent(Painter &, LyXFont const &) const
404 return par->Ascent() + ((disp_flag) ? 8 : 1);
408 int InsetFormula::descent(Painter &, LyXFont const &) const
410 return par->Descent() + ((disp_flag) ? 8 : 1);
414 int InsetFormula::width(Painter &, LyXFont const & f) const
416 lfont_size = f.size();
418 return par->Width(); //+2;
422 void InsetFormula::draw(Painter & pain, LyXFont const & f,
423 int baseline, float & x) const
425 // Seems commenting out solves a problem.
426 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
427 font.setSize(f.size());
428 lfont_size = font.size();
429 /// Let's try to wait a bit with this... (Lgb)
430 //UpdatableInset::draw(pain, font, baseline, x);
432 // otherwise a segfault could occur
433 // in some XDrawRectangles (i.e. matrix) (Matthias)
434 if (mathcursor && mathcursor->GetPar() == par) {
435 if (mathcursor->Selection()) {
439 mathcursor->SelGetArea(&xp, &yp, n);
440 pain.fillPolygon(xp, yp, n, LColor::selection);
442 mathcursor->draw(pain, int(x), baseline);
444 par->draw(pain, int(x), baseline);
446 x += float(width(pain, font));
448 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
449 LyXFont font = WhichFont(LM_TC_BF, par->size);
450 font.setLatex(LyXFont::OFF);
452 if (par->GetType() == LM_OT_PARN) {
455 str = string("(") + label + ")";
458 pain.text(int(x + 20), baseline, str, font);
459 } else if (par->GetType() == LM_OT_MPARN) {
460 MathMatrixInset * mt =
461 static_cast<MathMatrixInset*>(par);
463 MathedRowSt const * crow = mt->getRowSt();
465 y = baseline + crow->getBaseline();
466 if (crow->isNumbered()) {
468 if (crow->getLabel())
469 str = string("(") + crow->getLabel() + ")";
472 pain.text(int(x + 20), y, str, font);
474 crow = crow->getNext();
478 cursor_visible = false;
482 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
484 mathcursor = new MathedCursor(par);
487 bv->updateInset(this, false);
490 mathcursor->SetPos(x, y);
496 void InsetFormula::InsetUnlock(BufferView * bv)
499 if (mathcursor->InMacroMode()) {
500 mathcursor->MacroModeClose();
506 bv->updateInset(this, false);
510 // Now a symbol can be inserted only if the inset is locked
511 void InsetFormula::InsertSymbol(BufferView * bv, char const * s)
513 if (!s || !mathcursor) return;
514 mathcursor->Interpret(s);
519 void InsetFormula::GetCursorPos(int& x, int& y) const
521 mathcursor->GetPos(x, y);
526 void InsetFormula::ToggleInsetCursor(BufferView * bv)
532 mathcursor->GetPos(x, y);
535 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
536 int asc = font.maxAscent();
537 int desc = font.maxDescent();
540 bv->hideLockedInsetCursor();
542 bv->showLockedInsetCursor(x, y, asc, desc);
543 cursor_visible = !cursor_visible;
547 void InsetFormula::ShowInsetCursor(BufferView * bv)
549 if (!cursor_visible) {
552 mathcursor->GetPos(x, y);
555 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
556 int asc = font.maxAscent();
557 int desc = font.maxDescent();
558 bv->fitLockedInsetCursor(x, y, asc, desc);
560 ToggleInsetCursor(bv);
565 void InsetFormula::HideInsetCursor(BufferView * bv)
568 ToggleInsetCursor(bv);
572 void InsetFormula::ToggleInsetSelection(BufferView * bv)
580 //mathcursor->SelGetArea(n);
581 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
585 bv->updateInset(this, false);
590 void InsetFormula::display(bool dspf)
592 if (dspf != disp_flag) {
594 par->SetType(LM_OT_PAR);
595 par->SetStyle(LM_ST_DISPLAY);
597 if (par->GetType() >= LM_OT_MPAR) {
598 MathParInset * p = new MathParInset(par);
602 mathcursor->SetPar(par);
604 par->SetType(LM_OT_MIN);
605 par->SetStyle(LM_ST_TEXT);
606 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
615 int InsetFormula::GetNumberOfLabels() const
617 // This is dirty, I know. I'll clean it at 0.13
618 if (par->GetType() == LM_OT_MPARN) {
619 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
621 MathedRowSt const * crow = mt->getRowSt();
623 if (crow->getLabel()) ++nl;
624 crow = crow->getNext();
635 string InsetFormula::getLabel(int il) const
637 //#warning This is dirty, I know. Ill clean it at 0.11
638 // Correction, the only way to clean this is with a new kernel: 0.13.
639 if (par->GetType() == LM_OT_MPARN) {
641 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
643 MathedRowSt const * crow = mt->getRowSt();
645 if (crow->getLabel()) {
647 lab = crow->getLabel();
652 crow = crow->getNext();
660 void InsetFormula::UpdateLocal(BufferView * bv)
662 par->Metrics(); // To inform lyx kernel the exact size
663 // (there were problems with arrays).
664 bv->updateInset(this, true);
668 void InsetFormula::InsetButtonRelease(BufferView * bv,
669 int x, int y, int /*button*/)
674 mathcursor->SetPos(x, y);
679 bv->updateInset(this, false);
684 void InsetFormula::InsetButtonPress(BufferView * bv,
685 int x, int y, int /*button*/)
688 sel_x = x; sel_y = y;
689 if (mathcursor->Selection()) {
690 mathcursor->SelClear();
691 bv->updateInset(this, false);
696 void InsetFormula::InsetMotionNotify(BufferView * bv,
697 int x, int y, int /*button*/)
699 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
702 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
703 mathcursor->SelStart();
705 mathcursor->GetPos(sel_x, sel_y);
711 mathcursor->SetPos(x, y);
713 mathcursor->GetPos(x, y);
714 if (sel_x!= x || sel_y!= y)
715 bv->updateInset(this, false);
716 sel_x = x; sel_y = y;
721 void InsetFormula::InsetKeyPress(XKeyEvent *)
723 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
727 // Special Mathed functions
728 bool InsetFormula::SetNumber(bool numbf)
731 short type = par->GetType();
732 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
733 if (numbf && !oldf) ++type;
734 if (!numbf && oldf) --type;
742 UpdatableInset::RESULT
743 InsetFormula::LocalDispatch(BufferView * bv,
744 int action, string const & arg)
746 // extern char *dispatch_result;
747 MathedTextCodes varcode = LM_TC_MIN;
748 bool was_macro = mathcursor->InMacroMode();
750 bool space_on = false;
751 bool was_selection = mathcursor->Selection();
752 RESULT result = DISPATCHED;
753 static MathSpaceInset * sp= 0;
757 if (mathcursor->getLastCode() == LM_TC_TEX) {
762 // --- Cursor Movements ---------------------------------------------
763 case LFUN_RIGHTSEL: sel = true;
766 result = DISPATCH_RESULT(mathcursor->Right(sel));
769 case LFUN_LEFTSEL: sel = true;
772 result = DISPATCH_RESULT(mathcursor->Left(sel));
775 case LFUN_UPSEL: sel = true;
777 result = DISPATCH_RESULT(mathcursor->Up(sel));
780 case LFUN_DOWNSEL: sel = true;
782 result = DISPATCH_RESULT(mathcursor->Down(sel));
790 case LFUN_DELETE_LINE_FORWARD:
791 //current_view->lockedInsetStoreUndo(Undo::INSERT);
792 bv->lockedInsetStoreUndo(Undo::DELETE);
793 mathcursor->DelLine();
797 bv->lockedInsetStoreUndo(Undo::INSERT);
798 mathcursor->Insert(' ', LM_TC_CR);
799 par = mathcursor->GetPar();
803 bv->lockedInsetStoreUndo(Undo::INSERT);
804 mathcursor->Insert(0, LM_TC_TAB);
808 bv->lockedInsetStoreUndo(Undo::INSERT);
809 mathcursor->Insert('T', LM_TC_TAB);
813 if (!mathcursor->Left())
816 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
817 bv->updateInset(this, true);
822 //current_view->lockedInsetStoreUndo(Undo::INSERT);
823 bv->lockedInsetStoreUndo(Undo::DELETE);
824 mathcursor->Delete();
825 bv->updateInset(this, true);
828 // sprintf(dispatch_buffer, "%d %d",);
829 // dispatch_result = dispatch_buffer;
835 istringstream ist(arg.c_str());
837 istrstream ist(arg.c_str());
841 mathcursor->SetPos(x1 + x, y1 + y);
845 /* cursor selection ---------------------------- */
849 mathcursor->MacroModeClose();
850 bv->lockedInsetStoreUndo(Undo::INSERT);
851 mathcursor->SelPaste(); UpdateLocal(bv); break;
853 bv->lockedInsetStoreUndo(Undo::DELETE);
854 mathcursor->SelCut(); UpdateLocal(bv); break;
855 case LFUN_COPY: mathcursor->SelCopy(); break;
858 case LFUN_WORDRIGHTSEL:
859 case LFUN_WORDLEFTSEL:
862 // --- accented characters ------------------------------
864 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
865 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
866 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
867 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
868 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
869 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
870 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
871 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
872 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
873 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
878 if (!greek_kb_flag) {
880 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
887 case LFUN_GREEK_TOGGLE:
889 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
891 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
893 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
898 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
899 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
900 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
901 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
902 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
903 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
906 // varcode = LM_TC_TEX;
907 mathcursor->setLastCode(LM_TC_TEX);
908 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
912 case LFUN_MATH_NUMBER:
914 bv->lockedInsetStoreUndo(Undo::INSERT);
916 short type = par->GetType();
917 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
920 if (!label.empty()) {
923 bv->owner()->getMiniBuffer()->Set(_("No number"));
926 bv->owner()->getMiniBuffer()->Set(_("Number"));
934 case LFUN_MATH_NONUMBER:
936 if (par->GetType() == LM_OT_MPARN) {
937 // MathMatrixInset *mt = (MathMatrixInset*)par;
939 // mt->SetNumbered(!mt->IsNumbered());
941 mathcursor->setNumbered();
947 case LFUN_MATH_LIMITS:
949 bv->lockedInsetStoreUndo(Undo::INSERT);
950 if (mathcursor->Limits())
956 latexkeys * l = in_word_set (arg.c_str(), strlen(arg.c_str()));
957 int sz = (l) ? l->id: -1;
958 mathcursor->SetSize(sz);
963 case LFUN_INSERT_MATH:
965 bv->lockedInsetStoreUndo(Undo::INSERT);
966 InsertSymbol(bv, arg.c_str());
970 case LFUN_INSERT_MATRIX:
972 bv->lockedInsetStoreUndo(Undo::INSERT);
974 char s[80], arg2[80];
975 // This is just so that too long args won't ooze out of s.
976 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
977 k = sscanf(arg2, "%d %d %s", &m, &n, s);
986 MathMatrixInset * p = new MathMatrixInset(m, n);
987 if (mathcursor && p) {
988 if (k > 2 && int(strlen(s)) > m)
989 p->SetAlign(s[0], &s[1]);
990 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
996 case LFUN_MATH_DELIM:
998 bv->lockedInsetStoreUndo(Undo::INSERT);
999 char lf[40], rg[40], arg2[40];
1000 int ilf = '(', irg = '.';
1002 string vdelim("(){}[]./|");
1006 strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
1007 int n = sscanf(arg2, "%s %s", lf, rg);
1008 lf[39] = '\0'; rg[39] = '\0';
1015 l = in_word_set(lf, strlen(lf));
1016 // Long words will cause l == 0; so check.
1019 if (vdelim.find(lf[0]) != string::npos)
1027 l = in_word_set(rg, strlen(rg));
1030 if (vdelim.find(rg[0]) != string::npos)
1035 MathDelimInset * p = new MathDelimInset(ilf, irg);
1036 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1041 case LFUN_PROTECTEDSPACE:
1043 bv->lockedInsetStoreUndo(Undo::INSERT);
1044 sp = new MathSpaceInset(1);
1045 mathcursor->Insert(sp);
1051 case LFUN_INSERT_LABEL:
1053 bv->lockedInsetStoreUndo(Undo::INSERT);
1054 if (par->GetType() < LM_OT_PAR) break;
1058 result = askForText(_("Enter new label to insert:"));
1063 if (!lb.empty() && lb[0] > ' ') {
1065 if (par->GetType() == LM_OT_MPARN) {
1066 mathcursor->setLabel(lb.c_str());
1067 // MathMatrixInset *mt = (MathMatrixInset*)par;
1068 // mt->SetLabel(lb);
1070 //if (label.notEmpty()) delete label;
1079 case LFUN_MATH_DISPLAY:
1080 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1081 bv->lockedInsetStoreUndo(Undo::EDIT);
1082 display(!disp_flag);
1086 // Invalid actions under math mode
1087 case LFUN_MATH_MODE:
1089 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1090 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1091 varcode = LM_TC_TEXTRM;
1093 varcode = LM_TC_VAR;
1095 mathcursor->setLastCode(varcode);
1099 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1102 //------- dummy actions
1103 case LFUN_EXEC_COMMAND:
1104 bv->owner()->getMiniBuffer()->ExecCommand();
1108 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1109 unsigned char c = arg[0];
1110 bv->lockedInsetStoreUndo(Undo::INSERT);
1112 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1114 mathcursor->setAccent(0);
1116 if (c == 0) { // Dead key, do nothing
1117 //lyxerr << "deadkey" << endl;
1121 if (mathcursor->getLastCode() == LM_TC_TEX) {
1122 mathcursor->MacroModeOpen();
1123 mathcursor->clearLastCode();
1124 varcode = LM_TC_MIN;
1127 short f = (mathcursor->getLastCode()) ?
1128 mathcursor->getLastCode() :
1129 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1130 varcode = MathIsAlphaFont(f) ?
1131 static_cast<MathedTextCodes>(f) :
1135 // lyxerr << "Varcode << vardoce;
1136 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1137 varcode = LM_TC_MIN;
1138 if (greek_kb_flag<2) greek_kb_flag = 0;
1140 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1141 mathcursor->Insert(c, LM_TC_TEX);
1143 mathcursor->Insert('}', LM_TC_TEX);
1146 mathcursor->clearLastCode();
1147 // varcode = LM_TC_MIN;
1149 if (c == '_' && varcode == LM_TC_TEX) {
1150 mathcursor->Insert(c, LM_TC_SPECIAL);
1151 mathcursor->clearLastCode();
1152 // varcode = LM_TC_MIN;
1154 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1155 mathcursor->MacroModeOpen();
1156 mathcursor->clearLastCode();
1157 mathcursor->Insert(c, LM_TC_MIN);
1160 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1161 mathcursor->Insert(c, LM_TC_CONST);
1163 if (strchr("+/-*<>=", c))
1164 mathcursor->Insert(c, LM_TC_BOP);
1166 if (strchr(latex_special_chars, c) && c!= '_')
1167 mathcursor->Insert(c, LM_TC_SPECIAL);
1169 if (c == '_' || c == '^') {
1173 mathcursor->Interpret (s);
1177 short f = (mathcursor->getLastCode()) ?
1178 mathcursor->getLastCode() :
1179 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1180 varcode = MathIsAlphaFont(f) ?
1181 static_cast<MathedTextCodes>(f) :
1184 if (varcode == LM_TC_TEXTRM) {
1185 mathcursor->Insert(c, LM_TC_TEXTRM);
1188 mathcursor->MacroModeClose();
1191 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1195 if (!mathcursor->Pop() && mathcursor->IsEnd())
1200 mathcursor->Insert (c, LM_TC_VAR);
1204 mathcursor->MacroModeClose();
1205 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1206 mathcursor->setLastCode(LM_TC_TEX);
1210 // lyxerr << "Closed by action " << action << endl;
1214 if (was_macro != mathcursor->InMacroMode()
1216 && action != LFUN_BACKSPACE)
1218 if (sp && !space_on) sp = 0;
1219 if (mathcursor->Selection() || was_selection)
1220 ToggleInsetSelection(bv);
1222 if (result == DISPATCHED)
1223 ShowInsetCursor(bv);
1225 bv->unlockInset(this);
1232 MathFuncInset::draw(Painter & pain, int x, int y)
1234 if (name && name[0] > ' ') {
1235 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1236 font.setLatex(LyXFont::ON);
1237 x += (font.textWidth("I", 1) + 3) / 4;
1238 pain.text(x, y, name, font);
1243 void MathFuncInset::Metrics()
1245 ln = (name) ? strlen(name): 0;
1246 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1247 font.setLatex(LyXFont::ON);
1248 width = font.textWidth(name, ln) + font.textWidth("I", 1)/2;
1249 mathed_string_height(LM_TC_TEXTRM, size,
1250 reinterpret_cast<unsigned char const *>(name),
1251 strlen(name), ascent, descent);
1256 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1258 MathedIter it(par->GetData());
1260 while (it.OK() && !(features.binom && features.boldsymbol)) {
1263 MathParInset * p = it.GetActiveInset();
1264 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1265 strcmp(p->GetName(), "binom") == 0) {
1266 features.binom = true;
1268 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1269 p->setArgumentIdx(i);
1270 mathedValidate(features, p);
1274 MathedInset* p = it.GetInset();
1275 if (!features.boldsymbol && p->GetName() &&
1276 strcmp(p->GetName(), "boldsymbol") == 0) {
1277 features.boldsymbol = true;