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.
21 #pragma implementation "formula.h"
25 #include "commandtags.h"
26 #include "math_cursor.h"
27 #include "math_parser.h"
29 #include "minibuffer.h"
30 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
35 #include "lyx_gui_misc.h"
36 #include "support/LOstream.h"
48 //extern char * mathed_label;
49 extern string mathed_label;
51 extern char const * latex_special_chars;
53 int greek_kb_flag = 0;
55 LyXFont * Math_Fonts = 0; // this is only used by Whichfont and mathed_init_fonts (Lgb)
57 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
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);
218 int mathed_string_width(short type, int size, string const & str)
220 return mathed_string_width(type, size, reinterpret_cast<unsigned char const *>(str.c_str()), str.length());
224 int mathed_char_width(short type, int size, byte c)
226 int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1) :
227 lyxfont::width(c, WhichFont(type, size));
232 int mathed_string_height(short type, int size, byte const * s,
233 int ls, int & asc, int & des)
235 LyXFont font = WhichFont(type, size);
237 for (int i = 0; i < ls; ++i) {
238 des = max(des, lyxfont::descent(s[i], font));
239 asc = max(asc, lyxfont::ascent(s[i], font));
245 int mathed_string_height(short type, int size, string const & str,
246 int & asc, int & des)
248 return mathed_string_height(type, size,
249 reinterpret_cast<unsigned char const *>(str.c_str()), str.length(),
254 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
256 LyXFont font = WhichFont(type, size);
257 des = lyxfont::descent(c, font);
258 asc = lyxfont::ascent(c, font);
263 // In a near future maybe we use a better fonts renderer
264 void MathedInset::drawStr(Painter & pain, short type, int siz,
265 int x, int y, byte const * s, int ls)
268 if (MathIsBinary(type)) {
269 for (int i = 0; i < ls; ++i) {
270 st += string(" ") + char(s[i]) + ' ';
273 st = string(reinterpret_cast<char const *>(s), ls);
275 LyXFont const mf = mathed_get_font(type, siz);
276 pain.text(x, y, st, mf);
280 InsetFormula::InsetFormula(bool display)
282 par = new MathParInset; // this leaks
287 par->SetType(LM_OT_PAR);
288 par->SetStyle(LM_ST_DISPLAY);
293 InsetFormula::InsetFormula(MathParInset * p)
295 par = (p->GetType()>= LM_OT_MPAR) ?
296 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
300 disp_flag = (par->GetType()>0);
305 InsetFormula::~InsetFormula()
311 Inset * InsetFormula::Clone() const
313 InsetFormula * f = new InsetFormula(par);
319 void InsetFormula::Write(Buffer const * buf, ostream & os) const
322 Latex(buf, os, false, false);
326 int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
329 //#warning Alejandro, the number of lines is not returned in this case
330 // This problem will disapear at 0.13.
331 mathed_write(par, os, &ret, fragile, label);
336 int InsetFormula::Ascii(Buffer const *, ostream & os, int) const
338 par->Write(os, false);
343 int InsetFormula::Linuxdoc(Buffer const * buf, ostream & os) const
345 return Ascii(buf, os, 0);
349 int InsetFormula::DocBook(Buffer const * buf, ostream & os) const
351 return Ascii(buf, os, 0);
355 // Check if uses AMS macros
356 void InsetFormula::Validate(LaTeXFeatures & features) const
358 // Validation only necesary if not using an AMS Style
359 if (!features.amsstyle)
360 mathedValidate(features, par);
364 void InsetFormula::Read(Buffer const *, LyXLex & lex)
366 istream & is = lex.getStream();
368 mathed_parser_file(is, lex.GetLineNo());
370 // Silly hack to read labels.
372 mathed_label.erase();
374 mathed_parse(0, 0, &par);
376 disp_flag = (par->GetType() > 0);
378 // Update line number
379 lex.setLineNo(mathed_parser_lineno());
381 //if (mathed_label) {
382 if (!mathed_label.empty()) {
383 label = mathed_label;
385 mathed_label.erase();
388 // reading of end_inset in the inset!!!
391 if (lex.GetString() == "\\end_inset")
393 lyxerr << "InsetFormula::Read: Garbage before \\end_inset,"
394 " or missing \\end_inset!" << endl;
403 int InsetFormula::ascent(BufferView *, LyXFont const &) const
405 return par->Ascent() + ((disp_flag) ? 8 : 1);
409 int InsetFormula::descent(BufferView *, LyXFont const &) const
411 return par->Descent() + ((disp_flag) ? 8 : 1);
415 int InsetFormula::width(BufferView *, LyXFont const & f) const
417 lfont_size = f.size();
419 return par->Width(); //+2;
423 void InsetFormula::draw(BufferView * bv, LyXFont const & f,
424 int baseline, float & x, bool) const
426 Painter & pain = bv->painter();
427 // Seems commenting out solves a problem.
428 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
429 font.setSize(f.size());
430 lfont_size = font.size();
431 /// Let's try to wait a bit with this... (Lgb)
432 //UpdatableInset::draw(pain, font, baseline, x);
434 // otherwise a segfault could occur
435 // in some XDrawRectangles (i.e. matrix) (Matthias)
436 if (mathcursor && mathcursor->GetPar() == par) {
437 if (mathcursor->Selection()) {
441 mathcursor->SelGetArea(&xp, &yp, n);
442 pain.fillPolygon(xp, yp, n, LColor::selection);
444 mathcursor->draw(pain, int(x), baseline);
446 par->draw(pain, int(x), baseline);
448 x += float(width(bv, font));
450 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
451 LyXFont wfont = WhichFont(LM_TC_BF, par->size);
452 wfont.setLatex(LyXFont::OFF);
454 if (par->GetType() == LM_OT_PARN) {
457 str = string("(") + label + ")";
460 pain.text(int(x + 20), baseline, str, wfont);
461 } else if (par->GetType() == LM_OT_MPARN) {
462 MathMatrixInset * mt =
463 static_cast<MathMatrixInset*>(par);
465 MathedRowSt const * crow = mt->getRowSt();
467 y = baseline + crow->getBaseline();
468 if (crow->isNumbered()) {
470 if (!crow->getLabel().empty())
471 str = string("(") + crow->getLabel() + ")";
474 pain.text(int(x + 20), y, str, wfont);
476 crow = crow->getNext();
480 cursor_visible = false;
484 string const InsetFormula::EditMessage() const
486 return _("Math editor mode");
490 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
492 mathcursor = new MathedCursor(par);
493 if (!bv->lockInset(this))
494 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
496 bv->updateInset(this, false);
499 mathcursor->SetPos(x, y);
505 void InsetFormula::InsetUnlock(BufferView * bv)
508 if (mathcursor->InMacroMode()) {
509 mathcursor->MacroModeClose();
515 bv->updateInset(this, false);
519 // Now a symbol can be inserted only if the inset is locked
520 void InsetFormula::InsertSymbol(BufferView * bv, string const & s)
522 if (s.empty() || !mathcursor) return;
523 mathcursor->Interpret(s);
528 void InsetFormula::GetCursorPos(BufferView *, int & x, int & y) const
530 mathcursor->GetPos(x, y);
536 void InsetFormula::ToggleInsetCursor(BufferView * bv)
543 mathcursor->GetPos(x, y);
546 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
547 int asc = lyxfont::maxAscent(font);
548 int desc = lyxfont::maxDescent(font);
551 bv->hideLockedInsetCursor();
553 bv->showLockedInsetCursor(x, y, asc, desc);
554 cursor_visible = !cursor_visible;
558 void InsetFormula::ShowInsetCursor(BufferView * bv)
560 if (!cursor_visible) {
564 mathcursor->GetPos(x, y);
567 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
568 int asc = lyxfont::maxAscent(font);
569 int desc = lyxfont::maxDescent(font);
570 bv->fitLockedInsetCursor(x, y, asc, desc);
572 ToggleInsetCursor(bv);
577 void InsetFormula::HideInsetCursor(BufferView * bv)
580 ToggleInsetCursor(bv);
584 void InsetFormula::ToggleInsetSelection(BufferView * bv)
592 //mathcursor->SelGetArea(n);
593 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
597 bv->updateInset(this, false);
602 void InsetFormula::display(bool dspf)
604 if (dspf != disp_flag) {
606 par->SetType(LM_OT_PAR);
607 par->SetStyle(LM_ST_DISPLAY);
609 if (par->GetType() >= LM_OT_MPAR) {
610 MathParInset * p = new MathParInset(par);
614 mathcursor->SetPar(par);
616 par->SetType(LM_OT_MIN);
617 par->SetStyle(LM_ST_TEXT);
618 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
627 vector<string> const InsetFormula::getLabelList() const
629 //#warning This is dirty, I know. Ill clean it at 0.11
630 // Correction, the only way to clean this is with a new kernel: 0.13.
632 vector<string> label_list;
634 if (par->GetType() == LM_OT_MPARN) {
635 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
636 MathedRowSt const * crow = mt->getRowSt();
638 if (!crow->getLabel().empty())
639 label_list.push_back(crow->getLabel());
640 crow = crow->getNext();
642 } else if (!label.empty())
643 label_list.push_back(label);
649 void InsetFormula::UpdateLocal(BufferView * bv)
651 par->Metrics(); // To inform lyx kernel the exact size
652 // (there were problems with arrays).
653 bv->updateInset(this, true);
657 void InsetFormula::InsetButtonRelease(BufferView * bv,
658 int x, int y, int /*button*/)
664 mathcursor->SetPos(x, y);
669 bv->updateInset(this, false);
675 void InsetFormula::InsetButtonPress(BufferView * bv,
676 int x, int y, int /*button*/)
679 sel_x = x; sel_y = y;
680 if (mathcursor && mathcursor->Selection()) {
681 mathcursor->SelClear();
682 bv->updateInset(this, false);
687 void InsetFormula::InsetMotionNotify(BufferView * bv,
688 int x, int y, int /*button*/)
690 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
693 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
694 mathcursor->SelStart();
696 mathcursor->GetPos(sel_x, sel_y);
697 } else if (sel_flag) {
701 mathcursor->SetPos(x, y);
703 mathcursor->GetPos(x, y);
704 if (sel_x!= x || sel_y!= y)
705 bv->updateInset(this, false);
706 sel_x = x; sel_y = y;
711 void InsetFormula::InsetKeyPress(XKeyEvent *)
713 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
717 // Special Mathed functions
718 bool InsetFormula::SetNumber(bool numbf)
721 short type = par->GetType();
722 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
723 if (numbf && !oldf) ++type;
724 if (!numbf && oldf) --type;
732 UpdatableInset::RESULT
733 InsetFormula::LocalDispatch(BufferView * bv,
734 int action, string const & arg)
736 // extern char *dispatch_result;
737 MathedTextCodes varcode = LM_TC_MIN;
738 bool was_macro = mathcursor->InMacroMode();
740 bool space_on = false;
741 bool was_selection = mathcursor->Selection();
742 RESULT result = DISPATCHED;
743 static MathSpaceInset * sp= 0;
747 if (mathcursor->getLastCode() == LM_TC_TEX) {
752 // --- Cursor Movements ---------------------------------------------
753 case LFUN_RIGHTSEL: sel = true;
756 result = DISPATCH_RESULT(mathcursor->Right(sel));
757 if (!sel && (result == DISPATCHED))
758 result = DISPATCHED_NOUPDATE;
761 case LFUN_LEFTSEL: sel = true;
764 result = DISPATCH_RESULT(mathcursor->Left(sel));
765 if (!sel && (result == DISPATCHED))
766 result = DISPATCHED_NOUPDATE;
769 case LFUN_UPSEL: sel = true;
771 result = DISPATCH_RESULT(mathcursor->Up(sel));
772 if (!sel && (result == DISPATCHED))
773 result = DISPATCHED_NOUPDATE;
776 case LFUN_DOWNSEL: sel = true;
778 result = DISPATCH_RESULT(mathcursor->Down(sel));
779 if (!sel && (result == DISPATCHED))
780 result = DISPATCHED_NOUPDATE;
784 result = DISPATCHED_NOUPDATE;
788 result = DISPATCHED_NOUPDATE;
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;
837 istringstream ist(arg.c_str());
840 mathcursor->SetPos(x1 + x, y1 + y);
844 /* cursor selection ---------------------------- */
848 mathcursor->MacroModeClose();
849 bv->lockedInsetStoreUndo(Undo::INSERT);
850 mathcursor->SelPaste(); UpdateLocal(bv); break;
852 bv->lockedInsetStoreUndo(Undo::DELETE);
853 mathcursor->SelCut(); UpdateLocal(bv); break;
854 case LFUN_COPY: mathcursor->SelCopy(); break;
857 case LFUN_WORDRIGHTSEL:
858 case LFUN_WORDLEFTSEL:
861 // --- accented characters ------------------------------
863 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
864 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
865 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
866 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
867 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
868 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
869 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
870 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
871 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
872 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
877 if (!greek_kb_flag) {
879 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
886 case LFUN_GREEK_TOGGLE:
888 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
890 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
892 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
897 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
898 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
899 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
900 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
901 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
902 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
905 // varcode = LM_TC_TEX;
906 mathcursor->setLastCode(LM_TC_TEX);
907 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
911 case LFUN_MATH_NUMBER:
913 bv->lockedInsetStoreUndo(Undo::INSERT);
915 short type = par->GetType();
916 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
919 if (!label.empty()) {
922 bv->owner()->getMiniBuffer()->Set(_("No number"));
925 bv->owner()->getMiniBuffer()->Set(_("Number"));
933 case LFUN_MATH_NONUMBER:
935 if (par->GetType() == LM_OT_MPARN) {
936 // MathMatrixInset *mt = (MathMatrixInset*)par;
938 // mt->SetNumbered(!mt->IsNumbered());
940 mathcursor->setNumbered();
946 case LFUN_MATH_LIMITS:
948 bv->lockedInsetStoreUndo(Undo::INSERT);
949 if (mathcursor->Limits())
955 latexkeys * l = in_word_set (arg);
956 int sz = (l) ? l->id: -1;
957 mathcursor->SetSize(sz);
962 case LFUN_INSERT_MATH:
964 bv->lockedInsetStoreUndo(Undo::INSERT);
965 InsertSymbol(bv, arg);
969 case LFUN_INSERT_MATRIX:
971 bv->lockedInsetStoreUndo(Undo::INSERT);
973 char s[80], arg2[80];
974 // This is just so that too long args won't ooze out of s.
975 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
976 k = sscanf(arg2, "%d %d %s", &m, &n, s);
985 MathMatrixInset * p = new MathMatrixInset(m, n);
986 if (mathcursor && p) {
987 if (k > 2 && int(strlen(s)) > m)
988 p->SetAlign(s[0], &s[1]);
989 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
995 case LFUN_MATH_DELIM:
997 bv->lockedInsetStoreUndo(Undo::INSERT);
998 char lf[40], rg[40], arg2[40];
999 int ilf = '(', irg = '.';
1001 string vdelim("(){}[]./|");
1005 strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
1006 int n = sscanf(arg2, "%s %s", lf, rg);
1007 lf[39] = '\0'; rg[39] = '\0';
1011 ilf = lyx::atoi(lf);
1014 l = in_word_set(lf, strlen(lf));
1015 // Long words will cause l == 0; so check.
1018 if (vdelim.find(lf[0]) != string::npos)
1023 irg = lyx::atoi(rg);
1026 l = in_word_set(rg, strlen(rg));
1029 if (vdelim.find(rg[0]) != string::npos)
1034 MathDelimInset * p = new MathDelimInset(ilf, irg);
1035 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1040 case LFUN_PROTECTEDSPACE:
1042 bv->lockedInsetStoreUndo(Undo::INSERT);
1043 sp = new MathSpaceInset(1);
1044 mathcursor->Insert(sp);
1050 case LFUN_INSERT_LABEL:
1052 bv->lockedInsetStoreUndo(Undo::INSERT);
1053 if (par->GetType() < LM_OT_PAR) break;
1057 res = askForText(_("Enter new label to insert:"));
1062 if (!lb.empty() && lb[0] > ' ') {
1064 if (par->GetType() == LM_OT_MPARN) {
1065 mathcursor->setLabel(lb);
1066 // MathMatrixInset *mt = (MathMatrixInset*)par;
1067 // mt->SetLabel(lb);
1069 //if (label.notEmpty()) delete label;
1078 case LFUN_MATH_DISPLAY:
1079 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1080 bv->lockedInsetStoreUndo(Undo::EDIT);
1081 display(!disp_flag);
1085 // Invalid actions under math mode
1086 case LFUN_MATH_MODE:
1088 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1089 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1090 varcode = LM_TC_TEXTRM;
1092 varcode = LM_TC_VAR;
1094 mathcursor->setLastCode(varcode);
1098 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1101 //------- dummy actions
1102 case LFUN_EXEC_COMMAND:
1103 bv->owner()->getMiniBuffer()->ExecCommand();
1107 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1108 unsigned char c = arg[0];
1109 bv->lockedInsetStoreUndo(Undo::INSERT);
1111 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1113 mathcursor->setAccent(0);
1115 if (c == 0) { // Dead key, do nothing
1116 //lyxerr << "deadkey" << endl;
1120 if (mathcursor->getLastCode() == LM_TC_TEX) {
1121 mathcursor->MacroModeOpen();
1122 mathcursor->clearLastCode();
1123 varcode = LM_TC_MIN;
1126 short f = (mathcursor->getLastCode()) ?
1127 mathcursor->getLastCode() :
1128 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1129 varcode = MathIsAlphaFont(f) ?
1130 static_cast<MathedTextCodes>(f) :
1134 // lyxerr << "Varcode << vardoce;
1135 MathedTextCodes char_code = varcode;
1136 if (greek_kb_flag) {
1138 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1139 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1140 'Y', 0, 0, 0, 0 , 'Z' };
1141 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1142 char_code = LM_TC_RM;
1145 char_code = LM_TC_SYMB;
1147 mathcursor->Insert(c, char_code);
1148 if (greek_kb_flag && char_code == LM_TC_RM )
1149 mathcursor->setLastCode(LM_TC_VAR);
1150 varcode = LM_TC_MIN;
1151 if (greek_kb_flag<2) greek_kb_flag = 0;
1153 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1154 mathcursor->Insert(c, LM_TC_TEX);
1156 mathcursor->Insert('}', LM_TC_TEX);
1159 mathcursor->clearLastCode();
1160 // varcode = LM_TC_MIN;
1162 if (c == '_' && varcode == LM_TC_TEX) {
1163 mathcursor->Insert(c, LM_TC_SPECIAL);
1164 mathcursor->clearLastCode();
1165 // varcode = LM_TC_MIN;
1167 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1168 mathcursor->MacroModeOpen();
1169 mathcursor->clearLastCode();
1170 mathcursor->Insert(c, LM_TC_MIN);
1173 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1174 mathcursor->Insert(c, LM_TC_CONST);
1176 if (strchr("+/-*<>=", c))
1177 mathcursor->Insert(c, LM_TC_BOP);
1179 if (strchr(latex_special_chars, c) && c!= '_')
1180 mathcursor->Insert(c, LM_TC_SPECIAL);
1182 if (c == '_' || c == '^') {
1186 mathcursor->Interpret (s);
1190 short f = (mathcursor->getLastCode()) ?
1191 mathcursor->getLastCode() :
1192 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1193 varcode = MathIsAlphaFont(f) ?
1194 static_cast<MathedTextCodes>(f) :
1197 if (varcode == LM_TC_TEXTRM) {
1198 mathcursor->Insert(c, LM_TC_TEXTRM);
1201 mathcursor->MacroModeClose();
1204 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1208 if (!mathcursor->Pop() && mathcursor->IsEnd())
1213 mathcursor->Insert (c, LM_TC_VAR);
1217 mathcursor->MacroModeClose();
1218 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1219 mathcursor->setLastCode(LM_TC_TEX);
1223 // lyxerr << "Closed by action " << action << endl;
1227 if (was_macro != mathcursor->InMacroMode()
1229 && action != LFUN_BACKSPACE)
1231 if (sp && !space_on) sp = 0;
1232 if (mathcursor->Selection() || was_selection)
1233 ToggleInsetSelection(bv);
1235 if ((result == DISPATCHED) || (result == DISPATCHED_NOUPDATE))
1236 ShowInsetCursor(bv);
1238 bv->unlockInset(this);
1245 MathFuncInset::draw(Painter & pain, int x, int y)
1247 if (!name.empty() && name[0] > ' ') {
1248 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1249 font.setLatex(LyXFont::ON);
1250 x += (lyxfont::width('I', font) + 3) / 4;
1251 pain.text(x, y, name, font);
1256 void MathFuncInset::Metrics()
1258 //ln = (name) ? strlen(name): 0;
1259 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1260 font.setLatex(LyXFont::ON);
1266 width = lyxfont::width(name, font)
1267 + lyxfont::width('I', font) / 2;
1268 mathed_string_height(LM_TC_TEXTRM, size, name, ascent, descent);
1274 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1276 MathedIter it(par->GetData());
1278 while (it.OK() && !(features.binom && features.boldsymbol)) {
1281 MathParInset * p = it.GetActiveInset();
1282 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1283 p->GetName() == "binom") {
1284 features.binom = true;
1286 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1287 p->setArgumentIdx(i);
1288 mathedValidate(features, p);
1292 MathedInset* p = it.GetInset();
1293 if (!features.boldsymbol &&
1294 p->GetName() == "boldsymbol") {
1295 features.boldsymbol = true;