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"
40 #include "support/lyxlib.h"
49 //extern char * mathed_label;
50 extern string mathed_label;
52 extern char const * latex_special_chars;
54 int greek_kb_flag = 0;
56 LyXFont * Math_Fonts = 0; // this is only used by Whichfont and mathed_init_fonts (Lgb)
58 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
65 MathedCursor * InsetFormula::mathcursor = 0;
68 int MathedInset::df_asc;
69 int MathedInset::df_des;
70 int MathedInset::df_width;
74 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
77 LyXFont WhichFont(short type, int size)
107 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
117 f.setSize(lfont_size);
121 if (type == LM_TC_BSYM) {
131 case LM_ST_SCRIPTSCRIPT:
136 lyxerr << "Mathed Error: wrong font size: " << size << endl;
140 if (type != LM_TC_TEXTRM)
141 f.setColor(LColor::math);
146 void mathed_init_fonts() //removed 'static' because DEC cxx does not
148 // Probably because this func is declared as a friend in math_defs.h
152 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
154 for (int i = 0 ; i < 8 ; ++i) {
155 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
157 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
159 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
161 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
162 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
164 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
166 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
167 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
169 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
171 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
173 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
175 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
176 MathedInset::df_asc = lyxfont::maxAscent(f);
177 MathedInset::df_des = lyxfont::maxDescent(f);
178 MathedInset::df_width = lyxfont::width('I', f);
182 LyXFont mathed_get_font(short type, int size)
184 LyXFont f = WhichFont(type, size);
185 if (type == LM_TC_TEX) {
186 f.setLatex(LyXFont::ON);
192 int mathed_string_width(short type, int size, byte const * s, int ls)
194 LyXFont f = WhichFont(type, size);
197 if (MathIsBinary(type)) {
199 for (int i = 0; i < ls && i < 75; ++i) {
208 return lyxfont::width(reinterpret_cast<char const *>(s), ls, f);
211 int mathed_string_width(short type, int size, string const & str)
213 return mathed_string_width(type, size, reinterpret_cast<unsigned char const *>(str.c_str()), str.length());
217 int mathed_char_width(short type, int size, byte c)
219 int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1) :
220 lyxfont::width(c, WhichFont(type, size));
225 int mathed_string_height(short type, int size, byte const * s,
226 int ls, int & asc, int & des)
228 LyXFont font = WhichFont(type, size);
230 for (int i = 0; i < ls; ++i) {
231 des = max(des, lyxfont::descent(s[i], font));
232 asc = max(asc, lyxfont::ascent(s[i], font));
238 int mathed_string_height(short type, int size, string const & str,
239 int & asc, int & des)
241 return mathed_string_height(type, size,
242 reinterpret_cast<unsigned char const *>(str.c_str()), str.length(),
247 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
249 LyXFont font = WhichFont(type, size);
250 des = lyxfont::descent(c, font);
251 asc = lyxfont::ascent(c, font);
256 // In a near future maybe we use a better fonts renderer
257 void MathedInset::drawStr(Painter & pain, short type, int siz,
258 int x, int y, byte const * s, int ls)
261 if (MathIsBinary(type)) {
262 for (int i = 0; i < ls; ++i) {
263 st += string(" ") + char(s[i]) + ' ';
266 st = string(reinterpret_cast<char const *>(s), ls);
268 LyXFont const mf = mathed_get_font(type, siz);
269 pain.text(x, y, st, mf);
273 InsetFormula::InsetFormula(bool display)
275 par = new MathParInset; // this leaks
280 par->SetType(LM_OT_PAR);
281 par->SetStyle(LM_ST_DISPLAY);
286 InsetFormula::InsetFormula(MathParInset * p)
288 par = (p->GetType()>= LM_OT_MPAR) ?
289 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
293 disp_flag = (par->GetType()>0);
298 InsetFormula::~InsetFormula()
304 Inset * InsetFormula::Clone(Buffer const &) const
306 InsetFormula * f = new InsetFormula(par);
312 void InsetFormula::Write(Buffer const * buf, ostream & os) const
315 Latex(buf, os, false, false);
319 int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
322 //#warning Alejandro, the number of lines is not returned in this case
323 // This problem will disapear at 0.13.
324 mathed_write(par, os, &ret, fragile, label);
329 int InsetFormula::Ascii(Buffer const *, ostream & os, int) const
331 par->Write(os, false);
336 int InsetFormula::Linuxdoc(Buffer const * buf, ostream & os) const
338 return Ascii(buf, os, 0);
342 int InsetFormula::DocBook(Buffer const * buf, ostream & os) const
344 return Ascii(buf, os, 0);
348 // Check if uses AMS macros
349 void InsetFormula::Validate(LaTeXFeatures & features) const
351 // Validation only necesary if not using an AMS Style
352 if (!features.amsstyle)
353 mathedValidate(features, par);
357 void InsetFormula::Read(Buffer const *, LyXLex & lex)
359 istream & is = lex.getStream();
361 mathed_parser_file(is, lex.GetLineNo());
363 // Silly hack to read labels.
365 mathed_label.erase();
367 mathed_parse(0, 0, &par);
369 disp_flag = (par->GetType() > 0);
371 // Update line number
372 lex.setLineNo(mathed_parser_lineno());
374 //if (mathed_label) {
375 if (!mathed_label.empty()) {
376 label = mathed_label;
378 mathed_label.erase();
381 // reading of end_inset in the inset!!!
384 if (lex.GetString() == "\\end_inset")
386 lyxerr << "InsetFormula::Read: Garbage before \\end_inset,"
387 " or missing \\end_inset!" << endl;
396 int InsetFormula::ascent(BufferView *, LyXFont const &) const
398 return par->Ascent() + ((disp_flag) ? 8 : 1);
402 int InsetFormula::descent(BufferView *, LyXFont const &) const
404 return par->Descent() + ((disp_flag) ? 8 : 1);
408 int InsetFormula::width(BufferView *, LyXFont const & f) const
410 lfont_size = f.size();
412 return par->Width(); //+2;
416 void InsetFormula::draw(BufferView * bv, LyXFont const & f,
417 int baseline, float & x, bool) const
419 Painter & pain = bv->painter();
420 // Seems commenting out solves a problem.
421 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
422 font.setSize(f.size());
423 lfont_size = font.size();
424 /// Let's try to wait a bit with this... (Lgb)
425 //UpdatableInset::draw(pain, font, baseline, x);
427 // otherwise a segfault could occur
428 // in some XDrawRectangles (i.e. matrix) (Matthias)
429 if (mathcursor && mathcursor->GetPar() == par) {
430 if (mathcursor->Selection()) {
434 mathcursor->SelGetArea(&xp, &yp, n);
435 pain.fillPolygon(xp, yp, n, LColor::selection);
437 mathcursor->draw(pain, int(x), baseline);
439 par->draw(pain, int(x), baseline);
441 x += float(width(bv, font));
443 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
444 LyXFont wfont = WhichFont(LM_TC_BF, par->size);
445 wfont.setLatex(LyXFont::OFF);
447 if (par->GetType() == LM_OT_PARN) {
450 str = string("(") + label + ")";
453 pain.text(int(x + 20), baseline, str, wfont);
454 } else if (par->GetType() == LM_OT_MPARN) {
455 MathMatrixInset * mt =
456 static_cast<MathMatrixInset*>(par);
458 MathedRowSt const * crow = mt->getRowSt();
460 y = baseline + crow->getBaseline();
461 if (crow->isNumbered()) {
463 if (!crow->getLabel().empty())
464 str = string("(") + crow->getLabel() + ")";
467 pain.text(int(x + 20), y, str, wfont);
469 crow = crow->getNext();
473 cursor_visible = false;
477 string const InsetFormula::EditMessage() const
479 return _("Math editor mode");
483 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
485 mathcursor = new MathedCursor(par);
486 if (!bv->lockInset(this))
487 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
489 bv->updateInset(this, false);
492 mathcursor->SetPos(x, y);
498 void InsetFormula::InsetUnlock(BufferView * bv)
501 if (mathcursor->InMacroMode()) {
502 mathcursor->MacroModeClose();
508 bv->updateInset(this, false);
512 // Now a symbol can be inserted only if the inset is locked
513 void InsetFormula::InsertSymbol(BufferView * bv, string const & s)
515 if (s.empty() || !mathcursor) return;
516 mathcursor->Interpret(s);
521 void InsetFormula::GetCursorPos(BufferView *, int & x, int & y) const
523 mathcursor->GetPos(x, y);
529 void InsetFormula::ToggleInsetCursor(BufferView * bv)
536 mathcursor->GetPos(x, y);
539 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
540 int asc = lyxfont::maxAscent(font);
541 int desc = lyxfont::maxDescent(font);
544 bv->hideLockedInsetCursor();
546 bv->showLockedInsetCursor(x, y, asc, desc);
547 cursor_visible = !cursor_visible;
551 void InsetFormula::ShowInsetCursor(BufferView * bv, bool)
553 if (!cursor_visible) {
557 mathcursor->GetPos(x, y);
560 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
561 int asc = lyxfont::maxAscent(font);
562 int desc = lyxfont::maxDescent(font);
563 bv->fitLockedInsetCursor(x, y, asc, desc);
565 ToggleInsetCursor(bv);
570 void InsetFormula::HideInsetCursor(BufferView * bv)
573 ToggleInsetCursor(bv);
577 void InsetFormula::ToggleInsetSelection(BufferView * bv)
585 //mathcursor->SelGetArea(n);
586 // XFillPolygon(fl_get_display(), pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
590 bv->updateInset(this, false);
595 void InsetFormula::display(bool dspf)
597 if (dspf != disp_flag) {
599 par->SetType(LM_OT_PAR);
600 par->SetStyle(LM_ST_DISPLAY);
602 if (par->GetType() >= LM_OT_MPAR) {
603 MathParInset * p = new MathParInset(par);
607 mathcursor->SetPar(par);
609 par->SetType(LM_OT_MIN);
610 par->SetStyle(LM_ST_TEXT);
611 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
620 vector<string> const InsetFormula::getLabelList() const
622 //#warning This is dirty, I know. Ill clean it at 0.11
623 // Correction, the only way to clean this is with a new kernel: 0.13.
625 vector<string> label_list;
627 if (par->GetType() == LM_OT_MPARN) {
628 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
629 MathedRowSt const * crow = mt->getRowSt();
631 if (!crow->getLabel().empty())
632 label_list.push_back(crow->getLabel());
633 crow = crow->getNext();
635 } else if (!label.empty())
636 label_list.push_back(label);
642 void InsetFormula::UpdateLocal(BufferView * bv)
644 par->Metrics(); // To inform lyx kernel the exact size
645 // (there were problems with arrays).
646 bv->updateInset(this, true);
650 void InsetFormula::InsetButtonRelease(BufferView * bv,
651 int x, int y, int /*button*/)
657 mathcursor->SetPos(x, y);
662 bv->updateInset(this, false);
668 void InsetFormula::InsetButtonPress(BufferView * bv,
669 int x, int y, int /*button*/)
672 sel_x = x; sel_y = y;
673 if (mathcursor && mathcursor->Selection()) {
674 mathcursor->SelClear();
675 bv->updateInset(this, false);
680 void InsetFormula::InsetMotionNotify(BufferView * bv,
681 int x, int y, int /*button*/)
683 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
686 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
687 mathcursor->SelStart();
689 mathcursor->GetPos(sel_x, sel_y);
690 } else if (sel_flag) {
694 mathcursor->SetPos(x, y);
696 mathcursor->GetPos(x, y);
697 if (sel_x!= x || sel_y!= y)
698 bv->updateInset(this, false);
699 sel_x = x; sel_y = y;
704 void InsetFormula::InsetKeyPress(XKeyEvent *)
706 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
710 // Special Mathed functions
711 bool InsetFormula::SetNumber(bool numbf)
714 short type = par->GetType();
715 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
716 if (numbf && !oldf) ++type;
717 if (!numbf && oldf) --type;
725 UpdatableInset::RESULT
726 InsetFormula::LocalDispatch(BufferView * bv,
727 int action, string const & arg)
729 // extern char *dispatch_result;
730 MathedTextCodes varcode = LM_TC_MIN;
731 bool was_macro = mathcursor->InMacroMode();
733 bool space_on = false;
734 bool was_selection = mathcursor->Selection();
735 RESULT result = DISPATCHED;
736 static MathSpaceInset * sp= 0;
740 if (mathcursor->getLastCode() == LM_TC_TEX) {
745 // --- Cursor Movements ---------------------------------------------
746 case LFUN_RIGHTSEL: sel = true;
749 result = DISPATCH_RESULT(mathcursor->Right(sel));
750 if (!sel && (result == DISPATCHED))
751 result = DISPATCHED_NOUPDATE;
754 case LFUN_LEFTSEL: sel = true;
757 result = DISPATCH_RESULT(mathcursor->Left(sel));
758 if (!sel && (result == DISPATCHED))
759 result = DISPATCHED_NOUPDATE;
762 case LFUN_UPSEL: sel = true;
764 result = DISPATCH_RESULT(mathcursor->Up(sel));
765 if (!sel && (result == DISPATCHED))
766 result = DISPATCHED_NOUPDATE;
769 case LFUN_DOWNSEL: sel = true;
771 result = DISPATCH_RESULT(mathcursor->Down(sel));
772 if (!sel && (result == DISPATCHED))
773 result = DISPATCHED_NOUPDATE;
777 result = DISPATCHED_NOUPDATE;
781 result = DISPATCHED_NOUPDATE;
783 case LFUN_DELETE_LINE_FORWARD:
784 bv->lockedInsetStoreUndo(Undo::DELETE);
785 mathcursor->DelLine();
789 bv->lockedInsetStoreUndo(Undo::INSERT);
790 mathcursor->Insert(' ', LM_TC_CR);
791 par = mathcursor->GetPar();
795 bv->lockedInsetStoreUndo(Undo::INSERT);
796 mathcursor->Insert(0, LM_TC_TAB);
800 bv->lockedInsetStoreUndo(Undo::INSERT);
801 mathcursor->Insert('T', LM_TC_TAB);
805 if (!mathcursor->Left())
808 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
809 bv->updateInset(this, true);
814 bv->lockedInsetStoreUndo(Undo::DELETE);
815 mathcursor->Delete();
816 bv->updateInset(this, true);
819 // sprintf(dispatch_buffer, "%d %d",);
820 // dispatch_result = dispatch_buffer;
828 istringstream ist(arg.c_str());
831 mathcursor->SetPos(x1 + x, y1 + y);
835 /* cursor selection ---------------------------- */
839 mathcursor->MacroModeClose();
840 bv->lockedInsetStoreUndo(Undo::INSERT);
841 mathcursor->SelPaste(); UpdateLocal(bv); break;
843 bv->lockedInsetStoreUndo(Undo::DELETE);
844 mathcursor->SelCut(); UpdateLocal(bv); break;
845 case LFUN_COPY: mathcursor->SelCopy(); break;
848 case LFUN_WORDRIGHTSEL:
849 case LFUN_WORDLEFTSEL:
852 // --- accented characters ------------------------------
854 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
855 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
856 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
857 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
858 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
859 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
860 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
861 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
862 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
863 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
868 if (!greek_kb_flag) {
870 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
877 case LFUN_GREEK_TOGGLE:
879 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
881 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
883 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
888 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
889 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
890 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
891 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
892 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
893 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
896 // varcode = LM_TC_TEX;
897 mathcursor->setLastCode(LM_TC_TEX);
898 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
902 case LFUN_MATH_NUMBER:
904 bv->lockedInsetStoreUndo(Undo::INSERT);
906 short type = par->GetType();
907 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
910 if (!label.empty()) {
913 bv->owner()->getMiniBuffer()->Set(_("No number"));
916 bv->owner()->getMiniBuffer()->Set(_("Number"));
924 case LFUN_MATH_NONUMBER:
926 if (par->GetType() == LM_OT_MPARN) {
927 // MathMatrixInset *mt = (MathMatrixInset*)par;
929 // mt->SetNumbered(!mt->IsNumbered());
931 mathcursor->setNumbered();
937 case LFUN_MATH_LIMITS:
939 bv->lockedInsetStoreUndo(Undo::INSERT);
940 if (mathcursor->Limits())
946 latexkeys * l = in_word_set (arg);
947 int sz = (l) ? l->id: -1;
948 mathcursor->SetSize(sz);
953 case LFUN_INSERT_MATH:
955 bv->lockedInsetStoreUndo(Undo::INSERT);
956 InsertSymbol(bv, arg);
960 case LFUN_INSERT_MATRIX:
962 bv->lockedInsetStoreUndo(Undo::INSERT);
964 char s[80], arg2[80];
965 // This is just so that too long args won't ooze out of s.
966 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
967 k = sscanf(arg2, "%d %d %s", &m, &n, s);
976 MathMatrixInset * p = new MathMatrixInset(m, n);
977 if (mathcursor && p) {
978 if (k > 2 && int(strlen(s)) > m)
979 p->SetAlign(s[0], &s[1]);
980 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
986 case LFUN_MATH_DELIM:
988 bv->lockedInsetStoreUndo(Undo::INSERT);
989 char lf[40], rg[40], arg2[40];
990 int ilf = '(', irg = '.';
992 string vdelim("(){}[]./|");
996 ::strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
997 int n = sscanf(arg2, "%s %s", lf, rg);
998 lf[39] = '\0'; rg[39] = '\0';
1002 ilf = lyx::atoi(lf);
1005 l = in_word_set(lf, strlen(lf));
1006 // Long words will cause l == 0; so check.
1009 if (vdelim.find(lf[0]) != string::npos)
1014 irg = lyx::atoi(rg);
1017 l = in_word_set(rg, strlen(rg));
1020 if (vdelim.find(rg[0]) != string::npos)
1025 MathDelimInset * p = new MathDelimInset(ilf, irg);
1026 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1031 case LFUN_PROTECTEDSPACE:
1033 bv->lockedInsetStoreUndo(Undo::INSERT);
1034 sp = new MathSpaceInset(1);
1035 mathcursor->Insert(sp);
1041 case LFUN_INSERT_LABEL:
1043 bv->lockedInsetStoreUndo(Undo::INSERT);
1044 if (par->GetType() < LM_OT_PAR) break;
1048 res = askForText(_("Enter new label to insert:"));
1053 if (!lb.empty() && lb[0] > ' ') {
1055 if (par->GetType() == LM_OT_MPARN) {
1056 mathcursor->setLabel(lb);
1057 // MathMatrixInset *mt = (MathMatrixInset*)par;
1058 // mt->SetLabel(lb);
1060 //if (label.notEmpty()) delete label;
1069 case LFUN_MATH_DISPLAY:
1070 bv->lockedInsetStoreUndo(Undo::EDIT);
1071 display(!disp_flag);
1075 // Invalid actions under math mode
1076 case LFUN_MATH_MODE:
1078 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1079 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1080 varcode = LM_TC_TEXTRM;
1082 varcode = LM_TC_VAR;
1084 mathcursor->setLastCode(varcode);
1088 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1091 //------- dummy actions
1092 case LFUN_EXEC_COMMAND:
1093 bv->owner()->getMiniBuffer()->PrepareForCommand();
1097 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1098 unsigned char c = arg[0];
1099 bv->lockedInsetStoreUndo(Undo::INSERT);
1101 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1103 mathcursor->setAccent(0);
1105 if (c == 0) { // Dead key, do nothing
1106 //lyxerr << "deadkey" << endl;
1110 if (mathcursor->getLastCode() == LM_TC_TEX) {
1111 mathcursor->MacroModeOpen();
1112 mathcursor->clearLastCode();
1113 varcode = LM_TC_MIN;
1116 short f = (mathcursor->getLastCode()) ?
1117 mathcursor->getLastCode() :
1118 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1119 varcode = MathIsAlphaFont(f) ?
1120 static_cast<MathedTextCodes>(f) :
1124 // lyxerr << "Varcode << vardoce;
1125 MathedTextCodes char_code = varcode;
1126 if (greek_kb_flag) {
1128 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1129 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1130 'Y', 0, 0, 0, 0 , 'Z' };
1131 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1132 char_code = LM_TC_RM;
1135 char_code = LM_TC_SYMB;
1137 mathcursor->Insert(c, char_code);
1138 if (greek_kb_flag && char_code == LM_TC_RM )
1139 mathcursor->setLastCode(LM_TC_VAR);
1140 varcode = LM_TC_MIN;
1141 if (greek_kb_flag<2) greek_kb_flag = 0;
1143 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1144 mathcursor->Insert(c, LM_TC_TEX);
1146 mathcursor->Insert('}', LM_TC_TEX);
1149 mathcursor->clearLastCode();
1150 // varcode = LM_TC_MIN;
1152 if (c == '_' && varcode == LM_TC_TEX) {
1153 mathcursor->Insert(c, LM_TC_SPECIAL);
1154 mathcursor->clearLastCode();
1155 // varcode = LM_TC_MIN;
1157 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1158 mathcursor->MacroModeOpen();
1159 mathcursor->clearLastCode();
1160 mathcursor->Insert(c, LM_TC_MIN);
1163 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1164 mathcursor->Insert(c, LM_TC_CONST);
1166 if (strchr("+/-*<>=", c))
1167 mathcursor->Insert(c, LM_TC_BOP);
1169 if (strchr(latex_special_chars, c) && c!= '_')
1170 mathcursor->Insert(c, LM_TC_SPECIAL);
1172 if (c == '_' || c == '^') {
1176 mathcursor->Interpret (s);
1180 short f = (mathcursor->getLastCode()) ?
1181 mathcursor->getLastCode() :
1182 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1183 varcode = MathIsAlphaFont(f) ?
1184 static_cast<MathedTextCodes>(f) :
1187 if (varcode == LM_TC_TEXTRM) {
1188 mathcursor->Insert(c, LM_TC_TEXTRM);
1191 mathcursor->MacroModeClose();
1194 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1198 if (!mathcursor->Pop() && mathcursor->IsEnd())
1202 if (c == '\'' || c == '@') {
1203 mathcursor->Insert (c, LM_TC_VAR);
1207 mathcursor->MacroModeClose();
1208 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1209 mathcursor->setLastCode(LM_TC_TEX);
1213 // lyxerr << "Closed by action " << action << endl;
1217 if (was_macro != mathcursor->InMacroMode()
1219 && action != LFUN_BACKSPACE)
1221 if (sp && !space_on) sp = 0;
1222 if (mathcursor->Selection() || was_selection)
1223 ToggleInsetSelection(bv);
1225 if ((result == DISPATCHED) || (result == DISPATCHED_NOUPDATE))
1226 ShowInsetCursor(bv);
1228 bv->unlockInset(this);
1235 MathFuncInset::draw(Painter & pain, int x, int y)
1237 if (!name.empty() && name[0] > ' ') {
1238 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1239 font.setLatex(LyXFont::ON);
1240 x += (lyxfont::width('I', font) + 3) / 4;
1241 pain.text(x, y, name, font);
1246 void MathFuncInset::Metrics()
1248 //ln = (name) ? strlen(name): 0;
1249 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1250 font.setLatex(LyXFont::ON);
1256 width = lyxfont::width(name, font)
1257 + lyxfont::width('I', font) / 2;
1258 mathed_string_height(LM_TC_TEXTRM, size, name, ascent, descent);
1264 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1266 MathedIter it(par->GetData());
1268 while (it.OK() && !(features.binom && features.boldsymbol)) {
1271 MathParInset * p = it.GetActiveInset();
1272 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1273 p->GetName() == "binom") {
1274 features.binom = true;
1276 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1277 p->setArgumentIdx(i);
1278 mathedValidate(features, p);
1282 MathedInset* p = it.GetInset();
1283 if (!features.boldsymbol &&
1284 p->GetName() == "boldsymbol") {
1285 features.boldsymbol = true;