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 size,
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, size);
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;
379 int InsetFormula::ascent(Painter &, LyXFont const &) const
381 return par->Ascent() + ((disp_flag) ? 8 : 1);
385 int InsetFormula::descent(Painter &, LyXFont const &) const
387 return par->Descent() + ((disp_flag) ? 8 : 1);
391 int InsetFormula::width(Painter &, LyXFont const & f) const
393 lfont_size = f.size();
395 return par->Width(); //+2;
399 void InsetFormula::draw(Painter & pain, LyXFont const & f,
400 int baseline, float & x) const
402 // Seems commenting out solves a problem.
403 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
404 font.setSize(f.size());
405 lfont_size = font.size();
406 /// Let's try to wait a bit with this... (Lgb)
407 //UpdatableInset::draw(pain, font, baseline, x);
409 // otherwise a segfault could occur
410 // in some XDrawRectangles (i.e. matrix) (Matthias)
411 if (mathcursor && mathcursor->GetPar() == par) {
412 if (mathcursor->Selection()) {
416 mathcursor->SelGetArea(&xp, &yp, n);
417 pain.fillPolygon(xp, yp, n, LColor::selection);
419 mathcursor->draw(pain, int(x), baseline);
421 par->draw(pain, int(x), baseline);
423 x += float(width(pain, font));
425 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
426 LyXFont font = WhichFont(LM_TC_BF, par->size);
427 font.setLatex(LyXFont::OFF);
429 if (par->GetType() == LM_OT_PARN) {
432 str = string("(") + label + ")";
435 pain.text(int(x + 20), baseline, str, font);
436 } else if (par->GetType() == LM_OT_MPARN) {
437 MathMatrixInset * mt =
438 static_cast<MathMatrixInset*>(par);
440 MathedRowSt const * crow = mt->getRowSt();
442 y = baseline + crow->getBaseline();
443 if (crow->isNumbered()) {
445 if (crow->getLabel())
446 str = string("(") + crow->getLabel() + ")";
449 pain.text(int(x + 20), y, str, font);
451 crow = crow->getNext();
455 cursor_visible = false;
459 char const * InsetFormula::EditMessage() const
461 return _("Math editor mode");
465 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
467 mathcursor = new MathedCursor(par);
468 if (!bv->lockInset(this))
469 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
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 = lyxfont::maxAscent(font);
521 int desc = lyxfont::maxDescent(font);
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 = lyxfont::maxAscent(font);
541 int desc = lyxfont::maxDescent(font);
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*/)
659 mathcursor->SetPos(x, y);
664 bv->updateInset(this, false);
670 void InsetFormula::InsetButtonPress(BufferView * bv,
671 int x, int y, int /*button*/)
674 sel_x = x; sel_y = y;
675 if (mathcursor && mathcursor->Selection()) {
676 mathcursor->SelClear();
677 bv->updateInset(this, false);
682 void InsetFormula::InsetMotionNotify(BufferView * bv,
683 int x, int y, int /*button*/)
685 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
688 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
689 mathcursor->SelStart();
691 mathcursor->GetPos(sel_x, sel_y);
697 mathcursor->SetPos(x, y);
699 mathcursor->GetPos(x, y);
700 if (sel_x!= x || sel_y!= y)
701 bv->updateInset(this, false);
702 sel_x = x; sel_y = y;
707 void InsetFormula::InsetKeyPress(XKeyEvent *)
709 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
713 // Special Mathed functions
714 bool InsetFormula::SetNumber(bool numbf)
717 short type = par->GetType();
718 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
719 if (numbf && !oldf) ++type;
720 if (!numbf && oldf) --type;
728 UpdatableInset::RESULT
729 InsetFormula::LocalDispatch(BufferView * bv,
730 int action, string const & arg)
732 // extern char *dispatch_result;
733 MathedTextCodes varcode = LM_TC_MIN;
734 bool was_macro = mathcursor->InMacroMode();
736 bool space_on = false;
737 bool was_selection = mathcursor->Selection();
738 RESULT result = DISPATCHED;
739 static MathSpaceInset * sp= 0;
743 if (mathcursor->getLastCode() == LM_TC_TEX) {
748 // --- Cursor Movements ---------------------------------------------
749 case LFUN_RIGHTSEL: sel = true;
752 result = DISPATCH_RESULT(mathcursor->Right(sel));
755 case LFUN_LEFTSEL: sel = true;
758 result = DISPATCH_RESULT(mathcursor->Left(sel));
761 case LFUN_UPSEL: sel = true;
763 result = DISPATCH_RESULT(mathcursor->Up(sel));
766 case LFUN_DOWNSEL: sel = true;
768 result = DISPATCH_RESULT(mathcursor->Down(sel));
776 case LFUN_DELETE_LINE_FORWARD:
777 //current_view->lockedInsetStoreUndo(Undo::INSERT);
778 bv->lockedInsetStoreUndo(Undo::DELETE);
779 mathcursor->DelLine();
783 bv->lockedInsetStoreUndo(Undo::INSERT);
784 mathcursor->Insert(' ', LM_TC_CR);
785 par = mathcursor->GetPar();
789 bv->lockedInsetStoreUndo(Undo::INSERT);
790 mathcursor->Insert(0, LM_TC_TAB);
794 bv->lockedInsetStoreUndo(Undo::INSERT);
795 mathcursor->Insert('T', LM_TC_TAB);
799 if (!mathcursor->Left())
802 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
803 bv->updateInset(this, true);
808 //current_view->lockedInsetStoreUndo(Undo::INSERT);
809 bv->lockedInsetStoreUndo(Undo::DELETE);
810 mathcursor->Delete();
811 bv->updateInset(this, true);
814 // sprintf(dispatch_buffer, "%d %d",);
815 // dispatch_result = dispatch_buffer;
821 istringstream ist(arg.c_str());
823 istrstream ist(arg.c_str());
827 mathcursor->SetPos(x1 + x, y1 + y);
831 /* cursor selection ---------------------------- */
835 mathcursor->MacroModeClose();
836 bv->lockedInsetStoreUndo(Undo::INSERT);
837 mathcursor->SelPaste(); UpdateLocal(bv); break;
839 bv->lockedInsetStoreUndo(Undo::DELETE);
840 mathcursor->SelCut(); UpdateLocal(bv); break;
841 case LFUN_COPY: mathcursor->SelCopy(); break;
844 case LFUN_WORDRIGHTSEL:
845 case LFUN_WORDLEFTSEL:
848 // --- accented characters ------------------------------
850 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
851 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
852 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
853 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
854 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
855 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
856 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
857 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
858 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
859 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
864 if (!greek_kb_flag) {
866 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
873 case LFUN_GREEK_TOGGLE:
875 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
877 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
879 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
884 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
885 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
886 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
887 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
888 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
889 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
892 // varcode = LM_TC_TEX;
893 mathcursor->setLastCode(LM_TC_TEX);
894 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
898 case LFUN_MATH_NUMBER:
900 bv->lockedInsetStoreUndo(Undo::INSERT);
902 short type = par->GetType();
903 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
906 if (!label.empty()) {
909 bv->owner()->getMiniBuffer()->Set(_("No number"));
912 bv->owner()->getMiniBuffer()->Set(_("Number"));
920 case LFUN_MATH_NONUMBER:
922 if (par->GetType() == LM_OT_MPARN) {
923 // MathMatrixInset *mt = (MathMatrixInset*)par;
925 // mt->SetNumbered(!mt->IsNumbered());
927 mathcursor->setNumbered();
933 case LFUN_MATH_LIMITS:
935 bv->lockedInsetStoreUndo(Undo::INSERT);
936 if (mathcursor->Limits())
942 latexkeys * l = in_word_set (arg.c_str(), strlen(arg.c_str()));
943 int sz = (l) ? l->id: -1;
944 mathcursor->SetSize(sz);
949 case LFUN_INSERT_MATH:
951 bv->lockedInsetStoreUndo(Undo::INSERT);
952 InsertSymbol(bv, arg.c_str());
956 case LFUN_INSERT_MATRIX:
958 bv->lockedInsetStoreUndo(Undo::INSERT);
960 char s[80], arg2[80];
961 // This is just so that too long args won't ooze out of s.
962 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
963 k = sscanf(arg2, "%d %d %s", &m, &n, s);
972 MathMatrixInset * p = new MathMatrixInset(m, n);
973 if (mathcursor && p) {
974 if (k > 2 && int(strlen(s)) > m)
975 p->SetAlign(s[0], &s[1]);
976 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
982 case LFUN_MATH_DELIM:
984 bv->lockedInsetStoreUndo(Undo::INSERT);
985 char lf[40], rg[40], arg2[40];
986 int ilf = '(', irg = '.';
988 string vdelim("(){}[]./|");
992 strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
993 int n = sscanf(arg2, "%s %s", lf, rg);
994 lf[39] = '\0'; rg[39] = '\0';
1001 l = in_word_set(lf, strlen(lf));
1002 // Long words will cause l == 0; so check.
1005 if (vdelim.find(lf[0]) != string::npos)
1013 l = in_word_set(rg, strlen(rg));
1016 if (vdelim.find(rg[0]) != string::npos)
1021 MathDelimInset * p = new MathDelimInset(ilf, irg);
1022 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1027 case LFUN_PROTECTEDSPACE:
1029 bv->lockedInsetStoreUndo(Undo::INSERT);
1030 sp = new MathSpaceInset(1);
1031 mathcursor->Insert(sp);
1037 case LFUN_INSERT_LABEL:
1039 bv->lockedInsetStoreUndo(Undo::INSERT);
1040 if (par->GetType() < LM_OT_PAR) break;
1044 result = askForText(_("Enter new label to insert:"));
1049 if (!lb.empty() && lb[0] > ' ') {
1051 if (par->GetType() == LM_OT_MPARN) {
1052 mathcursor->setLabel(lb.c_str());
1053 // MathMatrixInset *mt = (MathMatrixInset*)par;
1054 // mt->SetLabel(lb);
1056 //if (label.notEmpty()) delete label;
1065 case LFUN_MATH_DISPLAY:
1066 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1067 bv->lockedInsetStoreUndo(Undo::EDIT);
1068 display(!disp_flag);
1072 // Invalid actions under math mode
1073 case LFUN_MATH_MODE:
1075 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1076 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1077 varcode = LM_TC_TEXTRM;
1079 varcode = LM_TC_VAR;
1081 mathcursor->setLastCode(varcode);
1085 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1088 //------- dummy actions
1089 case LFUN_EXEC_COMMAND:
1090 bv->owner()->getMiniBuffer()->ExecCommand();
1094 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1095 unsigned char c = arg[0];
1096 bv->lockedInsetStoreUndo(Undo::INSERT);
1098 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1100 mathcursor->setAccent(0);
1102 if (c == 0) { // Dead key, do nothing
1103 //lyxerr << "deadkey" << endl;
1107 if (mathcursor->getLastCode() == LM_TC_TEX) {
1108 mathcursor->MacroModeOpen();
1109 mathcursor->clearLastCode();
1110 varcode = LM_TC_MIN;
1113 short f = (mathcursor->getLastCode()) ?
1114 mathcursor->getLastCode() :
1115 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1116 varcode = MathIsAlphaFont(f) ?
1117 static_cast<MathedTextCodes>(f) :
1121 // lyxerr << "Varcode << vardoce;
1122 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1123 varcode = LM_TC_MIN;
1124 if (greek_kb_flag<2) greek_kb_flag = 0;
1126 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1127 mathcursor->Insert(c, LM_TC_TEX);
1129 mathcursor->Insert('}', LM_TC_TEX);
1132 mathcursor->clearLastCode();
1133 // varcode = LM_TC_MIN;
1135 if (c == '_' && varcode == LM_TC_TEX) {
1136 mathcursor->Insert(c, LM_TC_SPECIAL);
1137 mathcursor->clearLastCode();
1138 // varcode = LM_TC_MIN;
1140 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1141 mathcursor->MacroModeOpen();
1142 mathcursor->clearLastCode();
1143 mathcursor->Insert(c, LM_TC_MIN);
1146 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1147 mathcursor->Insert(c, LM_TC_CONST);
1149 if (strchr("+/-*<>=", c))
1150 mathcursor->Insert(c, LM_TC_BOP);
1152 if (strchr(latex_special_chars, c) && c!= '_')
1153 mathcursor->Insert(c, LM_TC_SPECIAL);
1155 if (c == '_' || c == '^') {
1159 mathcursor->Interpret (s);
1163 short f = (mathcursor->getLastCode()) ?
1164 mathcursor->getLastCode() :
1165 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1166 varcode = MathIsAlphaFont(f) ?
1167 static_cast<MathedTextCodes>(f) :
1170 if (varcode == LM_TC_TEXTRM) {
1171 mathcursor->Insert(c, LM_TC_TEXTRM);
1174 mathcursor->MacroModeClose();
1177 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1181 if (!mathcursor->Pop() && mathcursor->IsEnd())
1186 mathcursor->Insert (c, LM_TC_VAR);
1190 mathcursor->MacroModeClose();
1191 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1192 mathcursor->setLastCode(LM_TC_TEX);
1196 // lyxerr << "Closed by action " << action << endl;
1200 if (was_macro != mathcursor->InMacroMode()
1202 && action != LFUN_BACKSPACE)
1204 if (sp && !space_on) sp = 0;
1205 if (mathcursor->Selection() || was_selection)
1206 ToggleInsetSelection(bv);
1208 if (result == DISPATCHED)
1209 ShowInsetCursor(bv);
1211 bv->unlockInset(this);
1218 MathFuncInset::draw(Painter & pain, int x, int y)
1220 if (name && name[0] > ' ') {
1221 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1222 font.setLatex(LyXFont::ON);
1223 x += (lyxfont::width('I', font) + 3) / 4;
1224 pain.text(x, y, name, font);
1229 void MathFuncInset::Metrics()
1231 ln = (name) ? strlen(name): 0;
1232 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1233 font.setLatex(LyXFont::ON);
1234 width = lyxfont::width(name, ln, font)
1235 + lyxfont::width('I', font) / 2;
1236 mathed_string_height(LM_TC_TEXTRM, size,
1237 reinterpret_cast<unsigned char const *>(name),
1238 strlen(name), ascent, descent);
1243 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1245 MathedIter it(par->GetData());
1247 while (it.OK() && !(features.binom && features.boldsymbol)) {
1250 MathParInset * p = it.GetActiveInset();
1251 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1252 strcmp(p->GetName(), "binom") == 0) {
1253 features.binom = true;
1255 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1256 p->setArgumentIdx(i);
1257 mathedValidate(features, p);
1261 MathedInset* p = it.GetInset();
1262 if (!features.boldsymbol && p->GetName() &&
1263 strcmp(p->GetName(), "boldsymbol") == 0) {
1264 features.boldsymbol = true;