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 if (fragile) // this is where fragile != 0 was used (Lgb)
321 par->Write(os, fragile);
323 mathed_write(par, os, &ret, fragile, label.c_str());
328 int InsetFormula::Linuxdoc(ostream &) const
334 int InsetFormula::DocBook(ostream&) const
340 // Check if uses AMS macros
341 void InsetFormula::Validate(LaTeXFeatures & features) const
343 // Validation only necesary if not using an AMS Style
344 if (!features.amsstyle)
345 mathedValidate(features, par);
349 void InsetFormula::Read(LyXLex & lex)
351 istream & is = lex.getStream();
353 mathed_parser_file(is, lex.GetLineNo());
355 // Silly hack to read labels.
357 mathed_parse(0, 0, &par);
359 disp_flag = (par->GetType() > 0);
361 // Update line number
362 lex.setLineNo(mathed_parser_lineno());
365 label = mathed_label;
375 int InsetFormula::ascent(Painter &, LyXFont const &) const
377 return par->Ascent() + ((disp_flag) ? 8 : 1);
381 int InsetFormula::descent(Painter &, LyXFont const &) const
383 return par->Descent() + ((disp_flag) ? 8 : 1);
387 int InsetFormula::width(Painter &, LyXFont const & f) const
389 lfont_size = f.size();
391 return par->Width(); //+2;
395 void InsetFormula::draw(Painter & pain, LyXFont const & f,
396 int baseline, float & x) const
398 // Seems commenting out solves a problem.
399 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
400 font.setSize(f.size());
401 lfont_size = font.size();
402 /// Let's try to wait a bit with this... (Lgb)
403 //UpdatableInset::draw(pain, font, baseline, x);
405 // otherwise a segfault could occur
406 // in some XDrawRectangles (i.e. matrix) (Matthias)
407 if (mathcursor && mathcursor->GetPar() == par) {
408 if (mathcursor->Selection()) {
412 mathcursor->SelGetArea(&xp, &yp, n);
413 pain.fillPolygon(xp, yp, n, LColor::selection);
415 mathcursor->draw(pain, int(x), baseline);
417 par->draw(pain, int(x), baseline);
419 x += float(width(pain, font));
421 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
422 LyXFont font = WhichFont(LM_TC_BF, par->size);
423 font.setLatex(LyXFont::OFF);
425 if (par->GetType() == LM_OT_PARN) {
428 str = string("(") + label + ")";
431 pain.text(int(x + 20), baseline, str, font);
432 } else if (par->GetType() == LM_OT_MPARN) {
433 MathMatrixInset * mt =
434 static_cast<MathMatrixInset*>(par);
436 MathedRowSt const * crow = mt->getRowSt();
438 y = baseline + crow->getBaseline();
439 if (crow->isNumbered()) {
441 if (crow->getLabel())
442 str = string("(") + crow->getLabel() + ")";
445 pain.text(int(x + 20), y, str, font);
447 crow = crow->getNext();
451 cursor_visible = false;
455 char const * InsetFormula::EditMessage() const
457 return _("Math editor mode");
461 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
463 mathcursor = new MathedCursor(par);
464 if (!bv->lockInset(this))
465 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
467 bv->updateInset(this, false);
470 mathcursor->SetPos(x, y);
476 void InsetFormula::InsetUnlock(BufferView * bv)
479 if (mathcursor->InMacroMode()) {
480 mathcursor->MacroModeClose();
486 bv->updateInset(this, false);
490 // Now a symbol can be inserted only if the inset is locked
491 void InsetFormula::InsertSymbol(BufferView * bv, char const * s)
493 if (!s || !mathcursor) return;
494 mathcursor->Interpret(s);
499 void InsetFormula::GetCursorPos(int& x, int& y) const
501 mathcursor->GetPos(x, y);
506 void InsetFormula::ToggleInsetCursor(BufferView * bv)
512 mathcursor->GetPos(x, y);
515 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
516 int asc = lyxfont::maxAscent(font);
517 int desc = lyxfont::maxDescent(font);
520 bv->hideLockedInsetCursor();
522 bv->showLockedInsetCursor(x, y, asc, desc);
523 cursor_visible = !cursor_visible;
527 void InsetFormula::ShowInsetCursor(BufferView * bv)
529 if (!cursor_visible) {
532 mathcursor->GetPos(x, y);
535 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
536 int asc = lyxfont::maxAscent(font);
537 int desc = lyxfont::maxDescent(font);
538 bv->fitLockedInsetCursor(x, y, asc, desc);
540 ToggleInsetCursor(bv);
545 void InsetFormula::HideInsetCursor(BufferView * bv)
548 ToggleInsetCursor(bv);
552 void InsetFormula::ToggleInsetSelection(BufferView * bv)
560 //mathcursor->SelGetArea(n);
561 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
565 bv->updateInset(this, false);
570 void InsetFormula::display(bool dspf)
572 if (dspf != disp_flag) {
574 par->SetType(LM_OT_PAR);
575 par->SetStyle(LM_ST_DISPLAY);
577 if (par->GetType() >= LM_OT_MPAR) {
578 MathParInset * p = new MathParInset(par);
582 mathcursor->SetPar(par);
584 par->SetType(LM_OT_MIN);
585 par->SetStyle(LM_ST_TEXT);
586 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
595 int InsetFormula::GetNumberOfLabels() const
597 // This is dirty, I know. I'll clean it at 0.13
598 if (par->GetType() == LM_OT_MPARN) {
599 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
601 MathedRowSt const * crow = mt->getRowSt();
603 if (crow->getLabel()) ++nl;
604 crow = crow->getNext();
615 string InsetFormula::getLabel(int il) const
617 //#warning This is dirty, I know. Ill clean it at 0.11
618 // Correction, the only way to clean this is with a new kernel: 0.13.
619 if (par->GetType() == LM_OT_MPARN) {
621 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
623 MathedRowSt const * crow = mt->getRowSt();
625 if (crow->getLabel()) {
627 lab = crow->getLabel();
632 crow = crow->getNext();
640 void InsetFormula::UpdateLocal(BufferView * bv)
642 par->Metrics(); // To inform lyx kernel the exact size
643 // (there were problems with arrays).
644 bv->updateInset(this, true);
648 void InsetFormula::InsetButtonRelease(BufferView * bv,
649 int x, int y, int /*button*/)
655 mathcursor->SetPos(x, y);
660 bv->updateInset(this, false);
666 void InsetFormula::InsetButtonPress(BufferView * bv,
667 int x, int y, int /*button*/)
670 sel_x = x; sel_y = y;
671 if (mathcursor && mathcursor->Selection()) {
672 mathcursor->SelClear();
673 bv->updateInset(this, false);
678 void InsetFormula::InsetMotionNotify(BufferView * bv,
679 int x, int y, int /*button*/)
681 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
684 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
685 mathcursor->SelStart();
687 mathcursor->GetPos(sel_x, sel_y);
693 mathcursor->SetPos(x, y);
695 mathcursor->GetPos(x, y);
696 if (sel_x!= x || sel_y!= y)
697 bv->updateInset(this, false);
698 sel_x = x; sel_y = y;
703 void InsetFormula::InsetKeyPress(XKeyEvent *)
705 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
709 // Special Mathed functions
710 bool InsetFormula::SetNumber(bool numbf)
713 short type = par->GetType();
714 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
715 if (numbf && !oldf) ++type;
716 if (!numbf && oldf) --type;
724 UpdatableInset::RESULT
725 InsetFormula::LocalDispatch(BufferView * bv,
726 int action, string const & arg)
728 // extern char *dispatch_result;
729 MathedTextCodes varcode = LM_TC_MIN;
730 bool was_macro = mathcursor->InMacroMode();
732 bool space_on = false;
733 bool was_selection = mathcursor->Selection();
734 RESULT result = DISPATCHED;
735 static MathSpaceInset * sp= 0;
739 if (mathcursor->getLastCode() == LM_TC_TEX) {
744 // --- Cursor Movements ---------------------------------------------
745 case LFUN_RIGHTSEL: sel = true;
748 result = DISPATCH_RESULT(mathcursor->Right(sel));
751 case LFUN_LEFTSEL: sel = true;
754 result = DISPATCH_RESULT(mathcursor->Left(sel));
757 case LFUN_UPSEL: sel = true;
759 result = DISPATCH_RESULT(mathcursor->Up(sel));
762 case LFUN_DOWNSEL: sel = true;
764 result = DISPATCH_RESULT(mathcursor->Down(sel));
772 case LFUN_DELETE_LINE_FORWARD:
773 //current_view->lockedInsetStoreUndo(Undo::INSERT);
774 bv->lockedInsetStoreUndo(Undo::DELETE);
775 mathcursor->DelLine();
779 bv->lockedInsetStoreUndo(Undo::INSERT);
780 mathcursor->Insert(' ', LM_TC_CR);
781 par = mathcursor->GetPar();
785 bv->lockedInsetStoreUndo(Undo::INSERT);
786 mathcursor->Insert(0, LM_TC_TAB);
790 bv->lockedInsetStoreUndo(Undo::INSERT);
791 mathcursor->Insert('T', LM_TC_TAB);
795 if (!mathcursor->Left())
798 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
799 bv->updateInset(this, true);
804 //current_view->lockedInsetStoreUndo(Undo::INSERT);
805 bv->lockedInsetStoreUndo(Undo::DELETE);
806 mathcursor->Delete();
807 bv->updateInset(this, true);
810 // sprintf(dispatch_buffer, "%d %d",);
811 // dispatch_result = dispatch_buffer;
817 istringstream ist(arg.c_str());
819 istrstream ist(arg.c_str());
823 mathcursor->SetPos(x1 + x, y1 + y);
827 /* cursor selection ---------------------------- */
831 mathcursor->MacroModeClose();
832 bv->lockedInsetStoreUndo(Undo::INSERT);
833 mathcursor->SelPaste(); UpdateLocal(bv); break;
835 bv->lockedInsetStoreUndo(Undo::DELETE);
836 mathcursor->SelCut(); UpdateLocal(bv); break;
837 case LFUN_COPY: mathcursor->SelCopy(); break;
840 case LFUN_WORDRIGHTSEL:
841 case LFUN_WORDLEFTSEL:
844 // --- accented characters ------------------------------
846 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
847 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
848 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
849 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
850 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
851 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
852 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
853 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
854 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
855 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
860 if (!greek_kb_flag) {
862 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
869 case LFUN_GREEK_TOGGLE:
871 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
873 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
875 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
880 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
881 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
882 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
883 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
884 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
885 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
888 // varcode = LM_TC_TEX;
889 mathcursor->setLastCode(LM_TC_TEX);
890 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
894 case LFUN_MATH_NUMBER:
896 bv->lockedInsetStoreUndo(Undo::INSERT);
898 short type = par->GetType();
899 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
902 if (!label.empty()) {
905 bv->owner()->getMiniBuffer()->Set(_("No number"));
908 bv->owner()->getMiniBuffer()->Set(_("Number"));
916 case LFUN_MATH_NONUMBER:
918 if (par->GetType() == LM_OT_MPARN) {
919 // MathMatrixInset *mt = (MathMatrixInset*)par;
921 // mt->SetNumbered(!mt->IsNumbered());
923 mathcursor->setNumbered();
929 case LFUN_MATH_LIMITS:
931 bv->lockedInsetStoreUndo(Undo::INSERT);
932 if (mathcursor->Limits())
938 latexkeys * l = in_word_set (arg.c_str(), strlen(arg.c_str()));
939 int sz = (l) ? l->id: -1;
940 mathcursor->SetSize(sz);
945 case LFUN_INSERT_MATH:
947 bv->lockedInsetStoreUndo(Undo::INSERT);
948 InsertSymbol(bv, arg.c_str());
952 case LFUN_INSERT_MATRIX:
954 bv->lockedInsetStoreUndo(Undo::INSERT);
956 char s[80], arg2[80];
957 // This is just so that too long args won't ooze out of s.
958 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
959 k = sscanf(arg2, "%d %d %s", &m, &n, s);
968 MathMatrixInset * p = new MathMatrixInset(m, n);
969 if (mathcursor && p) {
970 if (k > 2 && int(strlen(s)) > m)
971 p->SetAlign(s[0], &s[1]);
972 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
978 case LFUN_MATH_DELIM:
980 bv->lockedInsetStoreUndo(Undo::INSERT);
981 char lf[40], rg[40], arg2[40];
982 int ilf = '(', irg = '.';
984 string vdelim("(){}[]./|");
988 strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
989 int n = sscanf(arg2, "%s %s", lf, rg);
990 lf[39] = '\0'; rg[39] = '\0';
997 l = in_word_set(lf, strlen(lf));
998 // Long words will cause l == 0; so check.
1001 if (vdelim.find(lf[0]) != string::npos)
1009 l = in_word_set(rg, strlen(rg));
1012 if (vdelim.find(rg[0]) != string::npos)
1017 MathDelimInset * p = new MathDelimInset(ilf, irg);
1018 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1023 case LFUN_PROTECTEDSPACE:
1025 bv->lockedInsetStoreUndo(Undo::INSERT);
1026 sp = new MathSpaceInset(1);
1027 mathcursor->Insert(sp);
1033 case LFUN_INSERT_LABEL:
1035 bv->lockedInsetStoreUndo(Undo::INSERT);
1036 if (par->GetType() < LM_OT_PAR) break;
1040 result = askForText(_("Enter new label to insert:"));
1045 if (!lb.empty() && lb[0] > ' ') {
1047 if (par->GetType() == LM_OT_MPARN) {
1048 mathcursor->setLabel(lb.c_str());
1049 // MathMatrixInset *mt = (MathMatrixInset*)par;
1050 // mt->SetLabel(lb);
1052 //if (label.notEmpty()) delete label;
1061 case LFUN_MATH_DISPLAY:
1062 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1063 bv->lockedInsetStoreUndo(Undo::EDIT);
1064 display(!disp_flag);
1068 // Invalid actions under math mode
1069 case LFUN_MATH_MODE:
1071 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1072 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1073 varcode = LM_TC_TEXTRM;
1075 varcode = LM_TC_VAR;
1077 mathcursor->setLastCode(varcode);
1081 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1084 //------- dummy actions
1085 case LFUN_EXEC_COMMAND:
1086 bv->owner()->getMiniBuffer()->ExecCommand();
1090 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1091 unsigned char c = arg[0];
1092 bv->lockedInsetStoreUndo(Undo::INSERT);
1094 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1096 mathcursor->setAccent(0);
1098 if (c == 0) { // Dead key, do nothing
1099 //lyxerr << "deadkey" << endl;
1103 if (mathcursor->getLastCode() == LM_TC_TEX) {
1104 mathcursor->MacroModeOpen();
1105 mathcursor->clearLastCode();
1106 varcode = LM_TC_MIN;
1109 short f = (mathcursor->getLastCode()) ?
1110 mathcursor->getLastCode() :
1111 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1112 varcode = MathIsAlphaFont(f) ?
1113 static_cast<MathedTextCodes>(f) :
1117 // lyxerr << "Varcode << vardoce;
1118 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1119 varcode = LM_TC_MIN;
1120 if (greek_kb_flag<2) greek_kb_flag = 0;
1122 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1123 mathcursor->Insert(c, LM_TC_TEX);
1125 mathcursor->Insert('}', LM_TC_TEX);
1128 mathcursor->clearLastCode();
1129 // varcode = LM_TC_MIN;
1131 if (c == '_' && varcode == LM_TC_TEX) {
1132 mathcursor->Insert(c, LM_TC_SPECIAL);
1133 mathcursor->clearLastCode();
1134 // varcode = LM_TC_MIN;
1136 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1137 mathcursor->MacroModeOpen();
1138 mathcursor->clearLastCode();
1139 mathcursor->Insert(c, LM_TC_MIN);
1142 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1143 mathcursor->Insert(c, LM_TC_CONST);
1145 if (strchr("+/-*<>=", c))
1146 mathcursor->Insert(c, LM_TC_BOP);
1148 if (strchr(latex_special_chars, c) && c!= '_')
1149 mathcursor->Insert(c, LM_TC_SPECIAL);
1151 if (c == '_' || c == '^') {
1155 mathcursor->Interpret (s);
1159 short f = (mathcursor->getLastCode()) ?
1160 mathcursor->getLastCode() :
1161 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1162 varcode = MathIsAlphaFont(f) ?
1163 static_cast<MathedTextCodes>(f) :
1166 if (varcode == LM_TC_TEXTRM) {
1167 mathcursor->Insert(c, LM_TC_TEXTRM);
1170 mathcursor->MacroModeClose();
1173 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1177 if (!mathcursor->Pop() && mathcursor->IsEnd())
1182 mathcursor->Insert (c, LM_TC_VAR);
1186 mathcursor->MacroModeClose();
1187 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1188 mathcursor->setLastCode(LM_TC_TEX);
1192 // lyxerr << "Closed by action " << action << endl;
1196 if (was_macro != mathcursor->InMacroMode()
1198 && action != LFUN_BACKSPACE)
1200 if (sp && !space_on) sp = 0;
1201 if (mathcursor->Selection() || was_selection)
1202 ToggleInsetSelection(bv);
1204 if (result == DISPATCHED)
1205 ShowInsetCursor(bv);
1207 bv->unlockInset(this);
1214 MathFuncInset::draw(Painter & pain, int x, int y)
1216 if (name && name[0] > ' ') {
1217 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1218 font.setLatex(LyXFont::ON);
1219 x += (lyxfont::width('I', font) + 3) / 4;
1220 pain.text(x, y, name, font);
1225 void MathFuncInset::Metrics()
1227 ln = (name) ? strlen(name): 0;
1228 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1229 font.setLatex(LyXFont::ON);
1230 width = lyxfont::width(name, ln, font)
1231 + lyxfont::width('I', font) / 2;
1232 mathed_string_height(LM_TC_TEXTRM, size,
1233 reinterpret_cast<unsigned char const *>(name),
1234 strlen(name), ascent, descent);
1239 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1241 MathedIter it(par->GetData());
1243 while (it.OK() && !(features.binom && features.boldsymbol)) {
1246 MathParInset * p = it.GetActiveInset();
1247 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1248 strcmp(p->GetName(), "binom") == 0) {
1249 features.binom = true;
1251 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1252 p->setArgumentIdx(i);
1253 mathedValidate(features, p);
1257 MathedInset* p = it.GetInset();
1258 if (!features.boldsymbol && p->GetName() &&
1259 strcmp(p->GetName(), "boldsymbol") == 0) {
1260 features.boldsymbol = true;