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;
60 static int sel_x, sel_y;
62 MathedCursor * InsetFormula::mathcursor = 0;
65 int MathedInset::df_asc;
66 int MathedInset::df_des;
67 int MathedInset::df_width;
71 bool IsMacro(short token, int id)
73 return (token != LM_TK_FRAC && token != LM_TK_SQRT &&
74 !((token == LM_TK_SYM || token == LM_TC_BSYM) && id < 255));
79 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
82 LyXFont WhichFont(short type, int size)
112 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
122 f.setSize(lfont_size);
126 if (type == LM_TC_BSYM) {
136 case LM_ST_SCRIPTSCRIPT:
141 lyxerr << "Mathed Error: wrong font size: " << size << endl;
145 if (type != LM_TC_TEXTRM)
146 f.setColor(LColor::math);
151 void mathed_init_fonts() //removed 'static' because DEC cxx does not
153 // Probably because this func is declared as a friend in math_defs.h
157 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
159 for (int i = 0 ; i < 8 ; ++i) {
160 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
162 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
164 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
166 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
167 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
169 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
171 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
172 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
174 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
176 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
178 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
180 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
181 MathedInset::df_asc = lyxfont::maxAscent(f);
182 MathedInset::df_des = lyxfont::maxDescent(f);
183 MathedInset::df_width = lyxfont::width('I', f);
187 LyXFont mathed_get_font(short type, int size)
189 LyXFont f = WhichFont(type, size);
190 if (type == LM_TC_TEX) {
191 f.setLatex(LyXFont::ON);
197 int mathed_string_width(short type, int size, byte const * s, int ls)
199 LyXFont f = WhichFont(type, size);
202 if (MathIsBinary(type)) {
204 for (int i = 0; i < ls && i < 75; ++i) {
213 return lyxfont::width(reinterpret_cast<char const *>(s), ls, f);
216 int mathed_string_width(short type, int size, string const & str)
218 return mathed_string_width(type, size, reinterpret_cast<unsigned char const *>(str.c_str()), str.length());
222 int mathed_char_width(short type, int size, byte c)
224 int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1) :
225 lyxfont::width(c, WhichFont(type, size));
230 int mathed_string_height(short type, int size, byte const * s,
231 int ls, int & asc, int & des)
233 LyXFont font = WhichFont(type, size);
235 for (int i = 0; i < ls; ++i) {
236 des = max(des, lyxfont::descent(s[i], font));
237 asc = max(asc, lyxfont::ascent(s[i], font));
243 int mathed_string_height(short type, int size, string const & str,
244 int & asc, int & des)
246 return mathed_string_height(type, size,
247 reinterpret_cast<unsigned char const *>(str.c_str()), str.length(),
252 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
254 LyXFont font = WhichFont(type, size);
255 des = lyxfont::descent(c, font);
256 asc = lyxfont::ascent(c, font);
261 // In a near future maybe we use a better fonts renderer
262 void MathedInset::drawStr(Painter & pain, short type, int siz,
263 int x, int y, byte const * s, int ls)
266 if (MathIsBinary(type)) {
267 for (int i = 0; i < ls; ++i) {
268 st += string(" ") + char(s[i]) + ' ';
271 st = string(reinterpret_cast<char const *>(s), ls);
273 LyXFont const mf = mathed_get_font(type, siz);
274 pain.text(x, y, st, mf);
278 InsetFormula::InsetFormula(bool display)
280 par = new MathParInset; // this leaks
285 par->SetType(LM_OT_PAR);
286 par->SetStyle(LM_ST_DISPLAY);
291 InsetFormula::InsetFormula(MathParInset * p)
293 par = (p->GetType()>= LM_OT_MPAR) ?
294 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
298 disp_flag = (par->GetType()>0);
303 InsetFormula::~InsetFormula()
309 Inset * InsetFormula::Clone() const
311 InsetFormula * f = new InsetFormula(par);
317 void InsetFormula::Write(Buffer const * buf, ostream & os) const
320 Latex(buf, os, false, false);
324 int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
327 //#warning Alejandro, the number of lines is not returned in this case
328 // This problem will disapear at 0.13.
329 mathed_write(par, os, &ret, fragile, label);
334 int InsetFormula::Ascii(Buffer const *, ostream & os, int) const
336 par->Write(os, false);
341 int InsetFormula::Linuxdoc(Buffer const * buf, ostream & os) const
343 return Ascii(buf, os, 0);
347 int InsetFormula::DocBook(Buffer const * buf, ostream & os) const
349 return Ascii(buf, os, 0);
353 // Check if uses AMS macros
354 void InsetFormula::Validate(LaTeXFeatures & features) const
356 // Validation only necesary if not using an AMS Style
357 if (!features.amsstyle)
358 mathedValidate(features, par);
362 void InsetFormula::Read(Buffer const *, LyXLex & lex)
364 istream & is = lex.getStream();
366 mathed_parser_file(is, lex.GetLineNo());
368 // Silly hack to read labels.
370 mathed_label.erase();
372 mathed_parse(0, 0, &par);
374 disp_flag = (par->GetType() > 0);
376 // Update line number
377 lex.setLineNo(mathed_parser_lineno());
379 //if (mathed_label) {
380 if (!mathed_label.empty()) {
381 label = mathed_label;
383 mathed_label.erase();
386 // reading of end_inset in the inset!!!
389 if (lex.GetString() == "\\end_inset")
391 lyxerr << "InsetFormula::Read: Garbage before \\end_inset,"
392 " or missing \\end_inset!" << endl;
401 int InsetFormula::ascent(BufferView *, LyXFont const &) const
403 return par->Ascent() + ((disp_flag) ? 8 : 1);
407 int InsetFormula::descent(BufferView *, LyXFont const &) const
409 return par->Descent() + ((disp_flag) ? 8 : 1);
413 int InsetFormula::width(BufferView *, LyXFont const & f) const
415 lfont_size = f.size();
417 return par->Width(); //+2;
421 void InsetFormula::draw(BufferView * bv, LyXFont const & f,
422 int baseline, float & x, bool) const
424 Painter & pain = bv->painter();
425 // Seems commenting out solves a problem.
426 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
427 font.setSize(f.size());
428 lfont_size = font.size();
429 /// Let's try to wait a bit with this... (Lgb)
430 //UpdatableInset::draw(pain, font, baseline, x);
432 // otherwise a segfault could occur
433 // in some XDrawRectangles (i.e. matrix) (Matthias)
434 if (mathcursor && mathcursor->GetPar() == par) {
435 if (mathcursor->Selection()) {
439 mathcursor->SelGetArea(&xp, &yp, n);
440 pain.fillPolygon(xp, yp, n, LColor::selection);
442 mathcursor->draw(pain, int(x), baseline);
444 par->draw(pain, int(x), baseline);
446 x += float(width(bv, font));
448 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
449 LyXFont wfont = WhichFont(LM_TC_BF, par->size);
450 wfont.setLatex(LyXFont::OFF);
452 if (par->GetType() == LM_OT_PARN) {
455 str = string("(") + label + ")";
458 pain.text(int(x + 20), baseline, str, wfont);
459 } else if (par->GetType() == LM_OT_MPARN) {
460 MathMatrixInset * mt =
461 static_cast<MathMatrixInset*>(par);
463 MathedRowSt const * crow = mt->getRowSt();
465 y = baseline + crow->getBaseline();
466 if (crow->isNumbered()) {
468 if (!crow->getLabel().empty())
469 str = string("(") + crow->getLabel() + ")";
472 pain.text(int(x + 20), y, str, wfont);
474 crow = crow->getNext();
478 cursor_visible = false;
482 string const InsetFormula::EditMessage() const
484 return _("Math editor mode");
488 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
490 mathcursor = new MathedCursor(par);
491 if (!bv->lockInset(this))
492 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
494 bv->updateInset(this, false);
497 mathcursor->SetPos(x, y);
503 void InsetFormula::InsetUnlock(BufferView * bv)
506 if (mathcursor->InMacroMode()) {
507 mathcursor->MacroModeClose();
513 bv->updateInset(this, false);
517 // Now a symbol can be inserted only if the inset is locked
518 void InsetFormula::InsertSymbol(BufferView * bv, string const & s)
520 if (s.empty() || !mathcursor) return;
521 mathcursor->Interpret(s);
526 void InsetFormula::GetCursorPos(BufferView *, int & x, int & y) const
528 mathcursor->GetPos(x, y);
534 void InsetFormula::ToggleInsetCursor(BufferView * bv)
540 mathcursor->GetPos(x, y);
543 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
544 int asc = lyxfont::maxAscent(font);
545 int desc = lyxfont::maxDescent(font);
548 bv->hideLockedInsetCursor();
550 bv->showLockedInsetCursor(x, y, asc, desc);
551 cursor_visible = !cursor_visible;
555 void InsetFormula::ShowInsetCursor(BufferView * bv)
557 if (!cursor_visible) {
560 mathcursor->GetPos(x, y);
563 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
564 int asc = lyxfont::maxAscent(font);
565 int desc = lyxfont::maxDescent(font);
566 bv->fitLockedInsetCursor(x, y, asc, desc);
568 ToggleInsetCursor(bv);
573 void InsetFormula::HideInsetCursor(BufferView * bv)
576 ToggleInsetCursor(bv);
580 void InsetFormula::ToggleInsetSelection(BufferView * bv)
588 //mathcursor->SelGetArea(n);
589 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
593 bv->updateInset(this, false);
598 void InsetFormula::display(bool dspf)
600 if (dspf != disp_flag) {
602 par->SetType(LM_OT_PAR);
603 par->SetStyle(LM_ST_DISPLAY);
605 if (par->GetType() >= LM_OT_MPAR) {
606 MathParInset * p = new MathParInset(par);
610 mathcursor->SetPar(par);
612 par->SetType(LM_OT_MIN);
613 par->SetStyle(LM_ST_TEXT);
614 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
623 vector<string> const InsetFormula::getLabelList() const
625 //#warning This is dirty, I know. Ill clean it at 0.11
626 // Correction, the only way to clean this is with a new kernel: 0.13.
628 vector<string> label_list;
630 if (par->GetType() == LM_OT_MPARN) {
631 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
632 MathedRowSt const * crow = mt->getRowSt();
634 if (!crow->getLabel().empty())
635 label_list.push_back(crow->getLabel());
636 crow = crow->getNext();
638 } else if (!label.empty())
639 label_list.push_back(label);
645 void InsetFormula::UpdateLocal(BufferView * bv)
647 par->Metrics(); // To inform lyx kernel the exact size
648 // (there were problems with arrays).
649 bv->updateInset(this, true);
653 void InsetFormula::InsetButtonRelease(BufferView * bv,
654 int x, int y, int /*button*/)
660 mathcursor->SetPos(x, y);
665 bv->updateInset(this, false);
671 void InsetFormula::InsetButtonPress(BufferView * bv,
672 int x, int y, int /*button*/)
675 sel_x = x; sel_y = y;
676 if (mathcursor && mathcursor->Selection()) {
677 mathcursor->SelClear();
678 bv->updateInset(this, false);
683 void InsetFormula::InsetMotionNotify(BufferView * bv,
684 int x, int y, int /*button*/)
686 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
689 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
690 mathcursor->SelStart();
692 mathcursor->GetPos(sel_x, sel_y);
698 mathcursor->SetPos(x, y);
700 mathcursor->GetPos(x, y);
701 if (sel_x!= x || sel_y!= y)
702 bv->updateInset(this, false);
703 sel_x = x; sel_y = y;
708 void InsetFormula::InsetKeyPress(XKeyEvent *)
710 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
714 // Special Mathed functions
715 bool InsetFormula::SetNumber(bool numbf)
718 short type = par->GetType();
719 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
720 if (numbf && !oldf) ++type;
721 if (!numbf && oldf) --type;
729 UpdatableInset::RESULT
730 InsetFormula::LocalDispatch(BufferView * bv,
731 int action, string const & arg)
733 // extern char *dispatch_result;
734 MathedTextCodes varcode = LM_TC_MIN;
735 bool was_macro = mathcursor->InMacroMode();
737 bool space_on = false;
738 bool was_selection = mathcursor->Selection();
739 RESULT result = DISPATCHED;
740 static MathSpaceInset * sp= 0;
744 if (mathcursor->getLastCode() == LM_TC_TEX) {
749 // --- Cursor Movements ---------------------------------------------
750 case LFUN_RIGHTSEL: sel = true;
753 result = DISPATCH_RESULT(mathcursor->Right(sel));
754 if (!sel && (result == DISPATCHED))
755 result = DISPATCHED_NOUPDATE;
758 case LFUN_LEFTSEL: sel = true;
761 result = DISPATCH_RESULT(mathcursor->Left(sel));
762 if (!sel && (result == DISPATCHED))
763 result = DISPATCHED_NOUPDATE;
766 case LFUN_UPSEL: sel = true;
768 result = DISPATCH_RESULT(mathcursor->Up(sel));
769 if (!sel && (result == DISPATCHED))
770 result = DISPATCHED_NOUPDATE;
773 case LFUN_DOWNSEL: sel = true;
775 result = DISPATCH_RESULT(mathcursor->Down(sel));
776 if (!sel && (result == DISPATCHED))
777 result = DISPATCHED_NOUPDATE;
781 result = DISPATCHED_NOUPDATE;
785 result = DISPATCHED_NOUPDATE;
787 case LFUN_DELETE_LINE_FORWARD:
788 //current_view->lockedInsetStoreUndo(Undo::INSERT);
789 bv->lockedInsetStoreUndo(Undo::DELETE);
790 mathcursor->DelLine();
794 bv->lockedInsetStoreUndo(Undo::INSERT);
795 mathcursor->Insert(' ', LM_TC_CR);
796 par = mathcursor->GetPar();
800 bv->lockedInsetStoreUndo(Undo::INSERT);
801 mathcursor->Insert(0, LM_TC_TAB);
805 bv->lockedInsetStoreUndo(Undo::INSERT);
806 mathcursor->Insert('T', LM_TC_TAB);
810 if (!mathcursor->Left())
813 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
814 bv->updateInset(this, true);
819 //current_view->lockedInsetStoreUndo(Undo::INSERT);
820 bv->lockedInsetStoreUndo(Undo::DELETE);
821 mathcursor->Delete();
822 bv->updateInset(this, true);
825 // sprintf(dispatch_buffer, "%d %d",);
826 // dispatch_result = dispatch_buffer;
831 istringstream ist(arg.c_str());
834 mathcursor->SetPos(x1 + x, y1 + y);
838 /* cursor selection ---------------------------- */
842 mathcursor->MacroModeClose();
843 bv->lockedInsetStoreUndo(Undo::INSERT);
844 mathcursor->SelPaste(); UpdateLocal(bv); break;
846 bv->lockedInsetStoreUndo(Undo::DELETE);
847 mathcursor->SelCut(); UpdateLocal(bv); break;
848 case LFUN_COPY: mathcursor->SelCopy(); break;
851 case LFUN_WORDRIGHTSEL:
852 case LFUN_WORDLEFTSEL:
855 // --- accented characters ------------------------------
857 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
858 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
859 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
860 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
861 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
862 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
863 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
864 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
865 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
866 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
871 if (!greek_kb_flag) {
873 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
880 case LFUN_GREEK_TOGGLE:
882 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
884 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
886 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
891 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
892 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
893 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
894 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
895 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
896 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
899 // varcode = LM_TC_TEX;
900 mathcursor->setLastCode(LM_TC_TEX);
901 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
905 case LFUN_MATH_NUMBER:
907 bv->lockedInsetStoreUndo(Undo::INSERT);
909 short type = par->GetType();
910 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
913 if (!label.empty()) {
916 bv->owner()->getMiniBuffer()->Set(_("No number"));
919 bv->owner()->getMiniBuffer()->Set(_("Number"));
927 case LFUN_MATH_NONUMBER:
929 if (par->GetType() == LM_OT_MPARN) {
930 // MathMatrixInset *mt = (MathMatrixInset*)par;
932 // mt->SetNumbered(!mt->IsNumbered());
934 mathcursor->setNumbered();
940 case LFUN_MATH_LIMITS:
942 bv->lockedInsetStoreUndo(Undo::INSERT);
943 if (mathcursor->Limits())
949 latexkeys * l = in_word_set (arg);
950 int sz = (l) ? l->id: -1;
951 mathcursor->SetSize(sz);
956 case LFUN_INSERT_MATH:
958 bv->lockedInsetStoreUndo(Undo::INSERT);
959 InsertSymbol(bv, arg);
963 case LFUN_INSERT_MATRIX:
965 bv->lockedInsetStoreUndo(Undo::INSERT);
967 char s[80], arg2[80];
968 // This is just so that too long args won't ooze out of s.
969 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
970 k = sscanf(arg2, "%d %d %s", &m, &n, s);
979 MathMatrixInset * p = new MathMatrixInset(m, n);
980 if (mathcursor && p) {
981 if (k > 2 && int(strlen(s)) > m)
982 p->SetAlign(s[0], &s[1]);
983 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
989 case LFUN_MATH_DELIM:
991 bv->lockedInsetStoreUndo(Undo::INSERT);
992 char lf[40], rg[40], arg2[40];
993 int ilf = '(', irg = '.';
995 string vdelim("(){}[]./|");
999 strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
1000 int n = sscanf(arg2, "%s %s", lf, rg);
1001 lf[39] = '\0'; rg[39] = '\0';
1005 ilf = lyx::atoi(lf);
1008 l = in_word_set(lf, strlen(lf));
1009 // Long words will cause l == 0; so check.
1012 if (vdelim.find(lf[0]) != string::npos)
1017 irg = lyx::atoi(rg);
1020 l = in_word_set(rg, strlen(rg));
1023 if (vdelim.find(rg[0]) != string::npos)
1028 MathDelimInset * p = new MathDelimInset(ilf, irg);
1029 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1034 case LFUN_PROTECTEDSPACE:
1036 bv->lockedInsetStoreUndo(Undo::INSERT);
1037 sp = new MathSpaceInset(1);
1038 mathcursor->Insert(sp);
1044 case LFUN_INSERT_LABEL:
1046 bv->lockedInsetStoreUndo(Undo::INSERT);
1047 if (par->GetType() < LM_OT_PAR) break;
1051 res = askForText(_("Enter new label to insert:"));
1056 if (!lb.empty() && lb[0] > ' ') {
1058 if (par->GetType() == LM_OT_MPARN) {
1059 mathcursor->setLabel(lb);
1060 // MathMatrixInset *mt = (MathMatrixInset*)par;
1061 // mt->SetLabel(lb);
1063 //if (label.notEmpty()) delete label;
1072 case LFUN_MATH_DISPLAY:
1073 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1074 bv->lockedInsetStoreUndo(Undo::EDIT);
1075 display(!disp_flag);
1079 // Invalid actions under math mode
1080 case LFUN_MATH_MODE:
1082 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1083 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1084 varcode = LM_TC_TEXTRM;
1086 varcode = LM_TC_VAR;
1088 mathcursor->setLastCode(varcode);
1092 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1095 //------- dummy actions
1096 case LFUN_EXEC_COMMAND:
1097 bv->owner()->getMiniBuffer()->ExecCommand();
1101 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1102 unsigned char c = arg[0];
1103 bv->lockedInsetStoreUndo(Undo::INSERT);
1105 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1107 mathcursor->setAccent(0);
1109 if (c == 0) { // Dead key, do nothing
1110 //lyxerr << "deadkey" << endl;
1114 if (mathcursor->getLastCode() == LM_TC_TEX) {
1115 mathcursor->MacroModeOpen();
1116 mathcursor->clearLastCode();
1117 varcode = LM_TC_MIN;
1120 short f = (mathcursor->getLastCode()) ?
1121 mathcursor->getLastCode() :
1122 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1123 varcode = MathIsAlphaFont(f) ?
1124 static_cast<MathedTextCodes>(f) :
1128 // lyxerr << "Varcode << vardoce;
1129 MathedTextCodes char_code = varcode;
1130 if (greek_kb_flag) {
1132 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1133 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1134 'Y', 0, 0, 0, 0 , 'Z' };
1135 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1136 char_code = LM_TC_RM;
1139 char_code = LM_TC_SYMB;
1141 mathcursor->Insert(c, char_code);
1142 if (greek_kb_flag && char_code == LM_TC_RM )
1143 mathcursor->setLastCode(LM_TC_VAR);
1144 varcode = LM_TC_MIN;
1145 if (greek_kb_flag<2) greek_kb_flag = 0;
1147 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1148 mathcursor->Insert(c, LM_TC_TEX);
1150 mathcursor->Insert('}', LM_TC_TEX);
1153 mathcursor->clearLastCode();
1154 // varcode = LM_TC_MIN;
1156 if (c == '_' && varcode == LM_TC_TEX) {
1157 mathcursor->Insert(c, LM_TC_SPECIAL);
1158 mathcursor->clearLastCode();
1159 // varcode = LM_TC_MIN;
1161 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1162 mathcursor->MacroModeOpen();
1163 mathcursor->clearLastCode();
1164 mathcursor->Insert(c, LM_TC_MIN);
1167 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1168 mathcursor->Insert(c, LM_TC_CONST);
1170 if (strchr("+/-*<>=", c))
1171 mathcursor->Insert(c, LM_TC_BOP);
1173 if (strchr(latex_special_chars, c) && c!= '_')
1174 mathcursor->Insert(c, LM_TC_SPECIAL);
1176 if (c == '_' || c == '^') {
1180 mathcursor->Interpret (s);
1184 short f = (mathcursor->getLastCode()) ?
1185 mathcursor->getLastCode() :
1186 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1187 varcode = MathIsAlphaFont(f) ?
1188 static_cast<MathedTextCodes>(f) :
1191 if (varcode == LM_TC_TEXTRM) {
1192 mathcursor->Insert(c, LM_TC_TEXTRM);
1195 mathcursor->MacroModeClose();
1198 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1202 if (!mathcursor->Pop() && mathcursor->IsEnd())
1207 mathcursor->Insert (c, LM_TC_VAR);
1211 mathcursor->MacroModeClose();
1212 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1213 mathcursor->setLastCode(LM_TC_TEX);
1217 // lyxerr << "Closed by action " << action << endl;
1221 if (was_macro != mathcursor->InMacroMode()
1223 && action != LFUN_BACKSPACE)
1225 if (sp && !space_on) sp = 0;
1226 if (mathcursor->Selection() || was_selection)
1227 ToggleInsetSelection(bv);
1229 if ((result == DISPATCHED) || (result == DISPATCHED_NOUPDATE))
1230 ShowInsetCursor(bv);
1232 bv->unlockInset(this);
1239 MathFuncInset::draw(Painter & pain, int x, int y)
1241 if (!name.empty() && name[0] > ' ') {
1242 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1243 font.setLatex(LyXFont::ON);
1244 x += (lyxfont::width('I', font) + 3) / 4;
1245 pain.text(x, y, name, font);
1250 void MathFuncInset::Metrics()
1252 //ln = (name) ? strlen(name): 0;
1253 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1254 font.setLatex(LyXFont::ON);
1260 width = lyxfont::width(name, font)
1261 + lyxfont::width('I', font) / 2;
1262 mathed_string_height(LM_TC_TEXTRM, size, name, ascent, descent);
1268 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1270 MathedIter it(par->GetData());
1272 while (it.OK() && !(features.binom && features.boldsymbol)) {
1275 MathParInset * p = it.GetActiveInset();
1276 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1277 p->GetName() == "binom") {
1278 features.binom = true;
1280 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1281 p->setArgumentIdx(i);
1282 mathedValidate(features, p);
1286 MathedInset* p = it.GetInset();
1287 if (!features.boldsymbol &&
1288 p->GetName() == "boldsymbol") {
1289 features.boldsymbol = true;