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(Buffer const * buf, ostream & os) const
312 Latex(buf, os, false, false);
316 int InsetFormula::Latex(Buffer const *, 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(Buffer const *, ostream & os) const
328 par->Write(os, false);
333 int InsetFormula::Linuxdoc(Buffer const * buf, ostream & os) const
335 return Ascii(buf, os);
339 int InsetFormula::DocBook(Buffer const * buf, ostream & os) const
341 return Ascii(buf, os);
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(Buffer const *, 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(BufferView * bv, LyXFont const & f,
407 int baseline, float & x) const
409 Painter & pain = bv->painter();
410 // Seems commenting out solves a problem.
411 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
412 font.setSize(f.size());
413 lfont_size = font.size();
414 /// Let's try to wait a bit with this... (Lgb)
415 //UpdatableInset::draw(pain, font, baseline, x);
417 // otherwise a segfault could occur
418 // in some XDrawRectangles (i.e. matrix) (Matthias)
419 if (mathcursor && mathcursor->GetPar() == par) {
420 if (mathcursor->Selection()) {
424 mathcursor->SelGetArea(&xp, &yp, n);
425 pain.fillPolygon(xp, yp, n, LColor::selection);
427 mathcursor->draw(pain, int(x), baseline);
429 par->draw(pain, int(x), baseline);
431 x += float(width(pain, font));
433 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
434 LyXFont wfont = WhichFont(LM_TC_BF, par->size);
435 wfont.setLatex(LyXFont::OFF);
437 if (par->GetType() == LM_OT_PARN) {
440 str = string("(") + label + ")";
443 pain.text(int(x + 20), baseline, str, wfont);
444 } else if (par->GetType() == LM_OT_MPARN) {
445 MathMatrixInset * mt =
446 static_cast<MathMatrixInset*>(par);
448 MathedRowSt const * crow = mt->getRowSt();
450 y = baseline + crow->getBaseline();
451 if (crow->isNumbered()) {
453 if (crow->getLabel())
454 str = string("(") + crow->getLabel() + ")";
457 pain.text(int(x + 20), y, str, wfont);
459 crow = crow->getNext();
463 cursor_visible = false;
467 char const * InsetFormula::EditMessage() const
469 return _("Math editor mode");
473 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
475 mathcursor = new MathedCursor(par);
476 if (!bv->lockInset(this))
477 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
479 bv->updateInset(this, false);
482 mathcursor->SetPos(x, y);
488 void InsetFormula::InsetUnlock(BufferView * bv)
491 if (mathcursor->InMacroMode()) {
492 mathcursor->MacroModeClose();
498 bv->updateInset(this, false);
502 // Now a symbol can be inserted only if the inset is locked
503 void InsetFormula::InsertSymbol(BufferView * bv, char const * s)
505 if (!s || !mathcursor) return;
506 mathcursor->Interpret(s);
511 void InsetFormula::GetCursorPos(BufferView *, int& x, int& y) const
513 mathcursor->GetPos(x, y);
518 void InsetFormula::ToggleInsetCursor(BufferView * bv)
524 mathcursor->GetPos(x, y);
527 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
528 int asc = lyxfont::maxAscent(font);
529 int desc = lyxfont::maxDescent(font);
532 bv->hideLockedInsetCursor();
534 bv->showLockedInsetCursor(x, y, asc, desc);
535 cursor_visible = !cursor_visible;
539 void InsetFormula::ShowInsetCursor(BufferView * bv)
541 if (!cursor_visible) {
544 mathcursor->GetPos(x, y);
547 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
548 int asc = lyxfont::maxAscent(font);
549 int desc = lyxfont::maxDescent(font);
550 bv->fitLockedInsetCursor(x, y, asc, desc);
552 ToggleInsetCursor(bv);
557 void InsetFormula::HideInsetCursor(BufferView * bv)
560 ToggleInsetCursor(bv);
564 void InsetFormula::ToggleInsetSelection(BufferView * bv)
572 //mathcursor->SelGetArea(n);
573 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
577 bv->updateInset(this, false);
582 void InsetFormula::display(bool dspf)
584 if (dspf != disp_flag) {
586 par->SetType(LM_OT_PAR);
587 par->SetStyle(LM_ST_DISPLAY);
589 if (par->GetType() >= LM_OT_MPAR) {
590 MathParInset * p = new MathParInset(par);
594 mathcursor->SetPar(par);
596 par->SetType(LM_OT_MIN);
597 par->SetStyle(LM_ST_TEXT);
598 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
607 vector<string> InsetFormula::getLabelList() const
609 //#warning This is dirty, I know. Ill clean it at 0.11
610 // Correction, the only way to clean this is with a new kernel: 0.13.
612 vector<string> label_list;
614 if (par->GetType() == LM_OT_MPARN) {
615 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
616 MathedRowSt const * crow = mt->getRowSt();
618 if (crow->getLabel())
619 label_list.push_back(crow->getLabel());
620 crow = crow->getNext();
622 } else if (!label.empty())
623 label_list.push_back(label);
629 void InsetFormula::UpdateLocal(BufferView * bv)
631 par->Metrics(); // To inform lyx kernel the exact size
632 // (there were problems with arrays).
633 bv->updateInset(this, true);
637 void InsetFormula::InsetButtonRelease(BufferView * bv,
638 int x, int y, int /*button*/)
644 mathcursor->SetPos(x, y);
649 bv->updateInset(this, false);
655 void InsetFormula::InsetButtonPress(BufferView * bv,
656 int x, int y, int /*button*/)
659 sel_x = x; sel_y = y;
660 if (mathcursor && mathcursor->Selection()) {
661 mathcursor->SelClear();
662 bv->updateInset(this, false);
667 void InsetFormula::InsetMotionNotify(BufferView * bv,
668 int x, int y, int /*button*/)
670 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
673 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
674 mathcursor->SelStart();
676 mathcursor->GetPos(sel_x, sel_y);
682 mathcursor->SetPos(x, y);
684 mathcursor->GetPos(x, y);
685 if (sel_x!= x || sel_y!= y)
686 bv->updateInset(this, false);
687 sel_x = x; sel_y = y;
692 void InsetFormula::InsetKeyPress(XKeyEvent *)
694 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
698 // Special Mathed functions
699 bool InsetFormula::SetNumber(bool numbf)
702 short type = par->GetType();
703 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
704 if (numbf && !oldf) ++type;
705 if (!numbf && oldf) --type;
713 UpdatableInset::RESULT
714 InsetFormula::LocalDispatch(BufferView * bv,
715 int action, string const & arg)
717 // extern char *dispatch_result;
718 MathedTextCodes varcode = LM_TC_MIN;
719 bool was_macro = mathcursor->InMacroMode();
721 bool space_on = false;
722 bool was_selection = mathcursor->Selection();
723 RESULT result = DISPATCHED;
724 static MathSpaceInset * sp= 0;
728 if (mathcursor->getLastCode() == LM_TC_TEX) {
733 // --- Cursor Movements ---------------------------------------------
734 case LFUN_RIGHTSEL: sel = true;
737 result = DISPATCH_RESULT(mathcursor->Right(sel));
740 case LFUN_LEFTSEL: sel = true;
743 result = DISPATCH_RESULT(mathcursor->Left(sel));
746 case LFUN_UPSEL: sel = true;
748 result = DISPATCH_RESULT(mathcursor->Up(sel));
751 case LFUN_DOWNSEL: sel = true;
753 result = DISPATCH_RESULT(mathcursor->Down(sel));
761 case LFUN_DELETE_LINE_FORWARD:
762 //current_view->lockedInsetStoreUndo(Undo::INSERT);
763 bv->lockedInsetStoreUndo(Undo::DELETE);
764 mathcursor->DelLine();
768 bv->lockedInsetStoreUndo(Undo::INSERT);
769 mathcursor->Insert(' ', LM_TC_CR);
770 par = mathcursor->GetPar();
774 bv->lockedInsetStoreUndo(Undo::INSERT);
775 mathcursor->Insert(0, LM_TC_TAB);
779 bv->lockedInsetStoreUndo(Undo::INSERT);
780 mathcursor->Insert('T', LM_TC_TAB);
784 if (!mathcursor->Left())
787 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
788 bv->updateInset(this, true);
793 //current_view->lockedInsetStoreUndo(Undo::INSERT);
794 bv->lockedInsetStoreUndo(Undo::DELETE);
795 mathcursor->Delete();
796 bv->updateInset(this, true);
799 // sprintf(dispatch_buffer, "%d %d",);
800 // dispatch_result = dispatch_buffer;
806 istringstream ist(arg.c_str());
808 istrstream ist(arg.c_str());
812 mathcursor->SetPos(x1 + x, y1 + y);
816 /* cursor selection ---------------------------- */
820 mathcursor->MacroModeClose();
821 bv->lockedInsetStoreUndo(Undo::INSERT);
822 mathcursor->SelPaste(); UpdateLocal(bv); break;
824 bv->lockedInsetStoreUndo(Undo::DELETE);
825 mathcursor->SelCut(); UpdateLocal(bv); break;
826 case LFUN_COPY: mathcursor->SelCopy(); break;
829 case LFUN_WORDRIGHTSEL:
830 case LFUN_WORDLEFTSEL:
833 // --- accented characters ------------------------------
835 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
836 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
837 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
838 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
839 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
840 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
841 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
842 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
843 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
844 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
849 if (!greek_kb_flag) {
851 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
858 case LFUN_GREEK_TOGGLE:
860 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
862 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
864 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
869 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
870 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
871 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
872 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
873 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
874 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
877 // varcode = LM_TC_TEX;
878 mathcursor->setLastCode(LM_TC_TEX);
879 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
883 case LFUN_MATH_NUMBER:
885 bv->lockedInsetStoreUndo(Undo::INSERT);
887 short type = par->GetType();
888 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
891 if (!label.empty()) {
894 bv->owner()->getMiniBuffer()->Set(_("No number"));
897 bv->owner()->getMiniBuffer()->Set(_("Number"));
905 case LFUN_MATH_NONUMBER:
907 if (par->GetType() == LM_OT_MPARN) {
908 // MathMatrixInset *mt = (MathMatrixInset*)par;
910 // mt->SetNumbered(!mt->IsNumbered());
912 mathcursor->setNumbered();
918 case LFUN_MATH_LIMITS:
920 bv->lockedInsetStoreUndo(Undo::INSERT);
921 if (mathcursor->Limits())
927 latexkeys * l = in_word_set (arg.c_str(), strlen(arg.c_str()));
928 int sz = (l) ? l->id: -1;
929 mathcursor->SetSize(sz);
934 case LFUN_INSERT_MATH:
936 bv->lockedInsetStoreUndo(Undo::INSERT);
937 InsertSymbol(bv, arg.c_str());
941 case LFUN_INSERT_MATRIX:
943 bv->lockedInsetStoreUndo(Undo::INSERT);
945 char s[80], arg2[80];
946 // This is just so that too long args won't ooze out of s.
947 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
948 k = sscanf(arg2, "%d %d %s", &m, &n, s);
957 MathMatrixInset * p = new MathMatrixInset(m, n);
958 if (mathcursor && p) {
959 if (k > 2 && int(strlen(s)) > m)
960 p->SetAlign(s[0], &s[1]);
961 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
967 case LFUN_MATH_DELIM:
969 bv->lockedInsetStoreUndo(Undo::INSERT);
970 char lf[40], rg[40], arg2[40];
971 int ilf = '(', irg = '.';
973 string vdelim("(){}[]./|");
977 strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
978 int n = sscanf(arg2, "%s %s", lf, rg);
979 lf[39] = '\0'; rg[39] = '\0';
986 l = in_word_set(lf, strlen(lf));
987 // Long words will cause l == 0; so check.
990 if (vdelim.find(lf[0]) != string::npos)
998 l = in_word_set(rg, strlen(rg));
1001 if (vdelim.find(rg[0]) != string::npos)
1006 MathDelimInset * p = new MathDelimInset(ilf, irg);
1007 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1012 case LFUN_PROTECTEDSPACE:
1014 bv->lockedInsetStoreUndo(Undo::INSERT);
1015 sp = new MathSpaceInset(1);
1016 mathcursor->Insert(sp);
1022 case LFUN_INSERT_LABEL:
1024 bv->lockedInsetStoreUndo(Undo::INSERT);
1025 if (par->GetType() < LM_OT_PAR) break;
1029 res = askForText(_("Enter new label to insert:"));
1034 if (!lb.empty() && lb[0] > ' ') {
1036 if (par->GetType() == LM_OT_MPARN) {
1037 mathcursor->setLabel(lb.c_str());
1038 // MathMatrixInset *mt = (MathMatrixInset*)par;
1039 // mt->SetLabel(lb);
1041 //if (label.notEmpty()) delete label;
1050 case LFUN_MATH_DISPLAY:
1051 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1052 bv->lockedInsetStoreUndo(Undo::EDIT);
1053 display(!disp_flag);
1057 // Invalid actions under math mode
1058 case LFUN_MATH_MODE:
1060 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1061 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1062 varcode = LM_TC_TEXTRM;
1064 varcode = LM_TC_VAR;
1066 mathcursor->setLastCode(varcode);
1070 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1073 //------- dummy actions
1074 case LFUN_EXEC_COMMAND:
1075 bv->owner()->getMiniBuffer()->ExecCommand();
1079 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1080 unsigned char c = arg[0];
1081 bv->lockedInsetStoreUndo(Undo::INSERT);
1083 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1085 mathcursor->setAccent(0);
1087 if (c == 0) { // Dead key, do nothing
1088 //lyxerr << "deadkey" << endl;
1092 if (mathcursor->getLastCode() == LM_TC_TEX) {
1093 mathcursor->MacroModeOpen();
1094 mathcursor->clearLastCode();
1095 varcode = LM_TC_MIN;
1098 short f = (mathcursor->getLastCode()) ?
1099 mathcursor->getLastCode() :
1100 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1101 varcode = MathIsAlphaFont(f) ?
1102 static_cast<MathedTextCodes>(f) :
1106 // lyxerr << "Varcode << vardoce;
1107 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1108 varcode = LM_TC_MIN;
1109 if (greek_kb_flag<2) greek_kb_flag = 0;
1111 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1112 mathcursor->Insert(c, LM_TC_TEX);
1114 mathcursor->Insert('}', LM_TC_TEX);
1117 mathcursor->clearLastCode();
1118 // varcode = LM_TC_MIN;
1120 if (c == '_' && varcode == LM_TC_TEX) {
1121 mathcursor->Insert(c, LM_TC_SPECIAL);
1122 mathcursor->clearLastCode();
1123 // varcode = LM_TC_MIN;
1125 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1126 mathcursor->MacroModeOpen();
1127 mathcursor->clearLastCode();
1128 mathcursor->Insert(c, LM_TC_MIN);
1131 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1132 mathcursor->Insert(c, LM_TC_CONST);
1134 if (strchr("+/-*<>=", c))
1135 mathcursor->Insert(c, LM_TC_BOP);
1137 if (strchr(latex_special_chars, c) && c!= '_')
1138 mathcursor->Insert(c, LM_TC_SPECIAL);
1140 if (c == '_' || c == '^') {
1144 mathcursor->Interpret (s);
1148 short f = (mathcursor->getLastCode()) ?
1149 mathcursor->getLastCode() :
1150 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1151 varcode = MathIsAlphaFont(f) ?
1152 static_cast<MathedTextCodes>(f) :
1155 if (varcode == LM_TC_TEXTRM) {
1156 mathcursor->Insert(c, LM_TC_TEXTRM);
1159 mathcursor->MacroModeClose();
1162 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1166 if (!mathcursor->Pop() && mathcursor->IsEnd())
1171 mathcursor->Insert (c, LM_TC_VAR);
1175 mathcursor->MacroModeClose();
1176 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1177 mathcursor->setLastCode(LM_TC_TEX);
1181 // lyxerr << "Closed by action " << action << endl;
1185 if (was_macro != mathcursor->InMacroMode()
1187 && action != LFUN_BACKSPACE)
1189 if (sp && !space_on) sp = 0;
1190 if (mathcursor->Selection() || was_selection)
1191 ToggleInsetSelection(bv);
1193 if (result == DISPATCHED)
1194 ShowInsetCursor(bv);
1196 bv->unlockInset(this);
1203 MathFuncInset::draw(Painter & pain, int x, int y)
1205 if (name && name[0] > ' ') {
1206 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1207 font.setLatex(LyXFont::ON);
1208 x += (lyxfont::width('I', font) + 3) / 4;
1209 pain.text(x, y, name, font);
1214 void MathFuncInset::Metrics()
1216 ln = (name) ? strlen(name): 0;
1217 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1218 font.setLatex(LyXFont::ON);
1219 width = lyxfont::width(name, ln, font)
1220 + lyxfont::width('I', font) / 2;
1221 mathed_string_height(LM_TC_TEXTRM, size,
1222 reinterpret_cast<unsigned char const *>(name),
1223 strlen(name), ascent, descent);
1228 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1230 MathedIter it(par->GetData());
1232 while (it.OK() && !(features.binom && features.boldsymbol)) {
1235 MathParInset * p = it.GetActiveInset();
1236 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1237 strcmp(p->GetName(), "binom") == 0) {
1238 features.binom = true;
1240 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1241 p->setArgumentIdx(i);
1242 mathedValidate(features, p);
1246 MathedInset* p = it.GetInset();
1247 if (!features.boldsymbol && p->GetName() &&
1248 strcmp(p->GetName(), "boldsymbol") == 0) {
1249 features.boldsymbol = true;