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;
62 inline bool IsMacro(short token, int id)
64 return (token != LM_TK_FRAC && token != LM_TK_SQRT &&
65 !((token == LM_TK_SYM || token == LM_TC_BSYM) && id < 255));
70 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
73 LyXFont WhichFont(short type, int size)
103 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
113 f.setSize(lfont_size);
117 if (type == LM_TC_BSYM) {
127 case LM_ST_SCRIPTSCRIPT:
132 lyxerr << "Mathed Error: wrong font size: " << size << endl;
136 if (type != LM_TC_TEXTRM)
137 f.setColor(LColor::math);
142 void mathed_init_fonts() //removed 'static' because DEC cxx does not
144 // Probably because this func is declared as a friend in math_defs.h
148 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
150 for (int i = 0 ; i < 8 ; ++i){
151 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
153 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
155 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
157 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
158 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
160 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
162 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
163 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
165 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
167 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
169 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
171 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
172 MathedInset::df_asc = f.maxAscent();
173 MathedInset::df_des = f.maxDescent();
174 MathedInset::df_width = f.width('I');
178 LyXFont mathed_get_font(short type, int size)
180 LyXFont f = WhichFont(type, size);
181 if (type == LM_TC_TEX) {
182 f.setLatex(LyXFont::ON);
188 int mathed_string_width(short type, int size, byte const * s, int ls)
190 LyXFont f = WhichFont(type, size);
193 if (MathIsBinary(type)) {
195 for (int i = 0; i < ls && i < 75; ++i) {
204 return f.textWidth(reinterpret_cast<char const *>(s), ls);
208 int mathed_char_width(short type, int size, byte c)
210 int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1):
211 WhichFont(type, size).width(c);
216 int mathed_string_height(short type, int size, byte const * s,
217 int ls, int & asc, int & des)
219 LyXFont font = WhichFont(type, size);
221 for (int i = 0; i < ls; ++i) {
222 if (font.descent(s[i]) > des)
223 des = font.descent(s[i]);
224 if (font.ascent(s[i]) > asc)
225 asc = font.ascent(s[i]);
231 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
233 LyXFont font = WhichFont(type, size);
235 des = font.descent(c);
236 asc = font.ascent(c);
241 // In a near future maybe we use a better fonts renderer
242 void MathedInset::drawStr(Painter & pain, short type, int size,
243 int x, int y, byte * s, int ls)
246 if (MathIsBinary(type)) {
247 for (int i = 0; i < ls; ++i) {
248 st += string(" ") + char(s[i]) + ' ';
251 st = string(reinterpret_cast<char*>(s), ls);
253 LyXFont mf = mathed_get_font(type, size);
254 pain.text(x, y, st, mf);
258 InsetFormula::InsetFormula(bool display)
260 par = new MathParInset; // this leaks
265 par->SetType(LM_OT_PAR);
266 par->SetStyle(LM_ST_DISPLAY);
271 InsetFormula::InsetFormula(MathParInset * p)
273 par = (p->GetType()>= LM_OT_MPAR) ?
274 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
278 disp_flag = (par->GetType()>0);
283 InsetFormula::~InsetFormula()
289 Inset * InsetFormula::Clone() const
291 InsetFormula * f = new InsetFormula(par);
297 void InsetFormula::Write(ostream & os) const
304 int InsetFormula::Latex(ostream & os, signed char fragile) const
307 //#warning Alejandro, the number of lines is not returned in this case
308 // This problem will disapear at 0.13.
310 InsetFormula::Latex(output, fragile);
316 int InsetFormula::Latex(string & file, signed char fragile) const
319 //#warning Alejandro, the number of lines is not returned in this case
320 // This problem will disapear at 0.13.
324 mathed_write(par, file, &ret, fragile, label.c_str());
329 int InsetFormula::Linuxdoc(string &/*file*/) const
335 int InsetFormula::DocBook(string &/*file*/) const
341 // Check if uses AMS macros
342 void InsetFormula::Validate(LaTeXFeatures & features) const
344 // Validation only necesary if not using an AMS Style
345 if (!features.amsstyle)
346 mathedValidate(features, par);
350 void InsetFormula::Read(LyXLex & lex)
352 istream & is = lex.getStream();
354 mathed_parser_file(is, lex.GetLineNo());
356 // Silly hack to read labels.
358 mathed_parse(0, 0, &par);
360 disp_flag = (par->GetType() > 0);
362 // Update line number
363 lex.setLineNo(mathed_parser_lineno());
366 label = mathed_label;
376 int InsetFormula::ascent(Painter &, LyXFont const &) const
378 return par->Ascent() + ((disp_flag) ? 8 : 1);
382 int InsetFormula::descent(Painter &, LyXFont const &) const
384 return par->Descent() + ((disp_flag) ? 8 : 1);
388 int InsetFormula::width(Painter &, LyXFont const & f) const
390 lfont_size = f.size();
392 return par->Width(); //+2;
396 void InsetFormula::draw(Painter & pain, LyXFont const &,
397 int baseline, float & x) const
399 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
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 void InsetFormula::Edit(BufferView * bv, int x, int y)
457 mathcursor = new MathedCursor(par);
460 bv->updateInset(this, false);
463 mathcursor->SetPos(x, y);
469 void InsetFormula::InsetUnlock(BufferView * bv)
472 if (mathcursor->InMacroMode()) {
473 mathcursor->MacroModeClose();
479 bv->updateInset(this, false);
483 // Now a symbol can be inserted only if the inset is locked
484 void InsetFormula::InsertSymbol(BufferView * bv, char const * s)
486 if (!s || !mathcursor) return;
487 mathcursor->Interpret(s);
492 void InsetFormula::GetCursorPos(int& x, int& y) const
494 mathcursor->GetPos(x, y);
499 void InsetFormula::ToggleInsetCursor(BufferView * bv)
505 mathcursor->GetPos(x, y);
508 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
509 int asc = font.maxAscent();
510 int desc = font.maxDescent();
513 bv->hideLockedInsetCursor();
515 bv->showLockedInsetCursor(x, y, asc, desc);
516 cursor_visible = !cursor_visible;
520 void InsetFormula::ShowInsetCursor(BufferView * bv)
522 if (!cursor_visible) {
525 mathcursor->GetPos(x, y);
528 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
529 int asc = font.maxAscent();
530 int desc = font.maxDescent();
531 bv->fitLockedInsetCursor(x, y, asc, desc);
533 ToggleInsetCursor(bv);
538 void InsetFormula::HideInsetCursor(BufferView * bv)
541 ToggleInsetCursor(bv);
545 void InsetFormula::ToggleInsetSelection(BufferView * bv)
553 //mathcursor->SelGetArea(n);
554 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
558 bv->updateInset(this, false);
563 void InsetFormula::display(bool dspf)
565 if (dspf != disp_flag) {
567 par->SetType(LM_OT_PAR);
568 par->SetStyle(LM_ST_DISPLAY);
570 if (par->GetType() >= LM_OT_MPAR) {
571 MathParInset * p = new MathParInset(par);
575 mathcursor->SetPar(par);
577 par->SetType(LM_OT_MIN);
578 par->SetStyle(LM_ST_TEXT);
579 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
588 int InsetFormula::GetNumberOfLabels() const
590 // This is dirty, I know. I'll clean it at 0.13
591 if (par->GetType() == LM_OT_MPARN) {
592 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
594 MathedRowSt const * crow = mt->getRowSt();
596 if (crow->getLabel()) ++nl;
597 crow = crow->getNext();
608 string InsetFormula::getLabel(int il) const
610 //#warning This is dirty, I know. Ill clean it at 0.11
611 // Correction, the only way to clean this is with a new kernel: 0.13.
612 if (par->GetType() == LM_OT_MPARN) {
614 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
616 MathedRowSt const * crow = mt->getRowSt();
618 if (crow->getLabel()) {
620 lab = crow->getLabel();
625 crow = crow->getNext();
633 void InsetFormula::UpdateLocal(BufferView * bv)
635 par->Metrics(); // To inform lyx kernel the exact size
636 // (there were problems with arrays).
637 bv->updateInset(this, true);
641 void InsetFormula::InsetButtonRelease(BufferView * bv,
642 int x, int y, int /*button*/)
647 mathcursor->SetPos(x, y);
652 bv->updateInset(this, false);
657 void InsetFormula::InsetButtonPress(BufferView * bv,
658 int x, int y, int /*button*/)
661 sel_x = x; sel_y = y;
662 if (mathcursor->Selection()) {
663 mathcursor->SelClear();
664 bv->updateInset(this, false);
669 void InsetFormula::InsetMotionNotify(BufferView * bv,
670 int x, int y, int /*button*/)
672 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
675 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
676 mathcursor->SelStart();
678 mathcursor->GetPos(sel_x, sel_y);
684 mathcursor->SetPos(x, y);
686 mathcursor->GetPos(x, y);
687 if (sel_x!= x || sel_y!= y)
688 bv->updateInset(this, false);
689 sel_x = x; sel_y = y;
694 void InsetFormula::InsetKeyPress(XKeyEvent *)
696 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
700 // Special Mathed functions
701 bool InsetFormula::SetNumber(bool numbf)
704 short type = par->GetType();
705 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
706 if (numbf && !oldf) ++type;
707 if (!numbf && oldf) --type;
715 bool InsetFormula::LocalDispatch(BufferView * bv, int action, char 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();
724 static MathSpaceInset * sp= 0;
728 if (mathcursor->getLastCode() == LM_TC_TEX) {
733 // --- Cursor Movements ---------------------------------------------
734 case LFUN_RIGHTSEL: sel = true;
737 result = mathcursor->Right(sel);
740 case LFUN_LEFTSEL: sel = true;
743 result = mathcursor->Left(sel);
746 case LFUN_UPSEL: sel = true;
748 result = mathcursor->Up(sel);
751 case LFUN_DOWNSEL: sel = true;
753 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;
805 sscanf(arg, "%d %d", &x, &y);
807 mathcursor->SetPos(x1+x, y1+y);
811 /* cursor selection ---------------------------- */
815 mathcursor->MacroModeClose();
816 bv->lockedInsetStoreUndo(Undo::INSERT);
817 mathcursor->SelPaste(); UpdateLocal(bv); break;
819 bv->lockedInsetStoreUndo(Undo::DELETE);
820 mathcursor->SelCut(); UpdateLocal(bv); break;
821 case LFUN_COPY: mathcursor->SelCopy(); break;
824 case LFUN_WORDRIGHTSEL:
825 case LFUN_WORDLEFTSEL:
828 // --- accented characters ------------------------------
830 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
831 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
832 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
833 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
834 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
835 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
836 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
837 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
838 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
839 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
844 if (!greek_kb_flag) {
846 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
853 case LFUN_GREEK_TOGGLE:
855 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
857 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
859 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
864 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
865 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
866 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
867 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
868 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
869 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
872 // varcode = LM_TC_TEX;
873 mathcursor->setLastCode(LM_TC_TEX);
874 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
878 case LFUN_MATH_NUMBER:
880 bv->lockedInsetStoreUndo(Undo::INSERT);
882 short type = par->GetType();
883 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
886 if (!label.empty()) {
889 bv->owner()->getMiniBuffer()->Set(_("No number"));
892 bv->owner()->getMiniBuffer()->Set(_("Number"));
900 case LFUN_MATH_NONUMBER:
902 if (par->GetType() == LM_OT_MPARN) {
903 // MathMatrixInset *mt = (MathMatrixInset*)par;
905 // mt->SetNumbered(!mt->IsNumbered());
907 mathcursor->setNumbered();
913 case LFUN_MATH_LIMITS:
915 bv->lockedInsetStoreUndo(Undo::INSERT);
916 if (mathcursor->Limits())
922 latexkeys * l = in_word_set (arg, strlen(arg));
923 int sz = (l) ? l->id: -1;
924 mathcursor->SetSize(sz);
929 case LFUN_INSERT_MATH:
931 bv->lockedInsetStoreUndo(Undo::INSERT);
932 InsertSymbol(bv, arg);
936 case LFUN_INSERT_MATRIX:
938 bv->lockedInsetStoreUndo(Undo::INSERT);
940 char s[80], arg2[80];
941 // This is just so that too long args won't ooze out of s.
942 strncpy(arg2, arg, 80); arg2[79]= '\0';
943 k = sscanf(arg2, "%d %d %s", &m, &n, s);
952 MathMatrixInset * p = new MathMatrixInset(m, n);
953 if (mathcursor && p) {
954 if (k > 2 && int(strlen(s)) > m)
955 p->SetAlign(s[0], &s[1]);
956 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
962 case LFUN_MATH_DELIM:
964 bv->lockedInsetStoreUndo(Undo::INSERT);
965 char lf[40], rg[40], arg2[40];
966 int ilf = '(', irg = '.';
968 string vdelim("(){}[]./|");
971 strncpy(arg2, arg, 40); arg2[39]= '\0';
972 int n = sscanf(arg2, "%s %s", lf, rg);
973 lf[39] = '\0'; rg[39] = '\0';
980 l = in_word_set(lf, strlen(lf));
981 // Long words will cause l == 0; so check.
984 if (vdelim.find(lf[0]) != string::npos)
992 l = in_word_set(rg, strlen(rg));
995 if (vdelim.find(rg[0]) != string::npos)
1000 MathDelimInset * p = new MathDelimInset(ilf, irg);
1001 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1006 case LFUN_PROTECTEDSPACE:
1008 bv->lockedInsetStoreUndo(Undo::INSERT);
1009 sp = new MathSpaceInset(1);
1010 mathcursor->Insert(sp);
1016 case LFUN_INSERT_LABEL:
1018 bv->lockedInsetStoreUndo(Undo::INSERT);
1019 if (par->GetType() < LM_OT_PAR) break;
1023 result = askForText(_("Enter new label to insert:"));
1028 if (!lb.empty() && lb[0] > ' ') {
1030 if (par->GetType() == LM_OT_MPARN) {
1031 mathcursor->setLabel(lb.c_str());
1032 // MathMatrixInset *mt = (MathMatrixInset*)par;
1033 // mt->SetLabel(lb);
1035 //if (label.notEmpty()) delete label;
1044 case LFUN_MATH_DISPLAY:
1045 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1046 bv->lockedInsetStoreUndo(Undo::EDIT);
1047 display(!disp_flag);
1051 // Invalid actions under math mode
1052 case LFUN_MATH_MODE:
1054 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1055 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1056 varcode = LM_TC_TEXTRM;
1058 varcode = LM_TC_VAR;
1060 mathcursor->setLastCode(varcode);
1064 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1067 //------- dummy actions
1068 case LFUN_EXEC_COMMAND:
1069 bv->owner()->getMiniBuffer()->ExecCommand();
1073 if ((action == -1 || action == LFUN_SELFINSERT) && arg) {
1074 unsigned char c = arg[0];
1075 bv->lockedInsetStoreUndo(Undo::INSERT);
1077 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1079 mathcursor->setAccent(0);
1081 if (c == 0) { // Dead key, do nothing
1082 //lyxerr << "deadkey" << endl;
1086 if (mathcursor->getLastCode() == LM_TC_TEX) {
1087 mathcursor->MacroModeOpen();
1088 mathcursor->clearLastCode();
1089 varcode = LM_TC_MIN;
1092 short f = (mathcursor->getLastCode()) ?
1093 mathcursor->getLastCode() :
1094 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1095 varcode = MathIsAlphaFont(f) ?
1096 static_cast<MathedTextCodes>(f) :
1100 // lyxerr << "Varcode << vardoce;
1101 mathcursor->Insert(c, (greek_kb_flag) ? LM_TC_SYMB: varcode);
1102 varcode = LM_TC_MIN;
1103 if (greek_kb_flag<2) greek_kb_flag = 0;
1105 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1106 mathcursor->Insert(c, LM_TC_TEX);
1108 mathcursor->Insert('}', LM_TC_TEX);
1111 mathcursor->clearLastCode();
1112 // varcode = LM_TC_MIN;
1114 if (c == '_' && varcode == LM_TC_TEX) {
1115 mathcursor->Insert(c, LM_TC_SPECIAL);
1116 mathcursor->clearLastCode();
1117 // varcode = LM_TC_MIN;
1119 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1120 mathcursor->MacroModeOpen();
1121 mathcursor->clearLastCode();
1122 mathcursor->Insert(c, LM_TC_MIN);
1125 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1126 mathcursor->Insert(c, LM_TC_CONST);
1128 if (strchr("+/-*<>=", c))
1129 mathcursor->Insert(c, LM_TC_BOP);
1131 if (strchr(latex_special_chars, c) && c!= '_')
1132 mathcursor->Insert(c, LM_TC_SPECIAL);
1134 if (c == '_' || c == '^') {
1138 mathcursor->Interpret (s);
1142 short f = (mathcursor->getLastCode()) ?
1143 mathcursor->getLastCode() :
1144 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1145 varcode = MathIsAlphaFont(f) ?
1146 static_cast<MathedTextCodes>(f) :
1149 if (varcode == LM_TC_TEXTRM) {
1150 mathcursor->Insert(c, LM_TC_TEXTRM);
1153 mathcursor->MacroModeClose();
1156 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1160 if (!mathcursor->Pop() && mathcursor->IsEnd())
1165 mathcursor->Insert (c, LM_TC_VAR);
1169 mathcursor->MacroModeClose();
1170 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1171 mathcursor->setLastCode(LM_TC_TEX);
1175 // lyxerr << "Closed by action " << action << endl;
1179 if (was_macro != mathcursor->InMacroMode()
1181 && action != LFUN_BACKSPACE)
1183 if (sp && !space_on) sp = 0;
1184 if (mathcursor->Selection() || was_selection)
1185 ToggleInsetSelection(bv);
1188 ShowInsetCursor(bv);
1190 bv->unlockInset(this);
1197 MathFuncInset::draw(Painter & pain, int x, int y)
1199 if (name && name[0] > ' ') {
1200 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1201 font.setLatex(LyXFont::ON);
1202 x += (font.textWidth("I", 1) + 3) / 4;
1203 pain.text(x, y, name, font);
1208 void MathFuncInset::Metrics()
1210 ln = (name) ? strlen(name): 0;
1211 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1212 font.setLatex(LyXFont::ON);
1213 width = font.textWidth(name, ln) + font.textWidth("I", 1)/2;
1214 mathed_string_height(LM_TC_TEXTRM, size,
1215 reinterpret_cast<unsigned char const *>(name),
1216 strlen(name), ascent, descent);
1221 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1223 MathedIter it(par->GetData());
1225 while (it.OK() && !(features.binom && features.boldsymbol)) {
1228 MathParInset * p = it.GetActiveInset();
1229 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1230 strcmp(p->GetName(), "binom") == 0) {
1231 features.binom = true;
1233 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1234 p->setArgumentIdx(i);
1235 mathedValidate(features, p);
1239 MathedInset* p = it.GetInset();
1240 if (!features.boldsymbol && p->GetName() &&
1241 strcmp(p->GetName(), "boldsymbol") == 0) {
1242 features.boldsymbol = true;