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"
57 extern string mathed_label;
59 extern char const * latex_special_chars;
61 int greek_kb_flag = 0;
63 LyXFont * Math_Fonts = 0; // this is only used by Whichfont and mathed_init_fonts (Lgb)
65 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
72 MathedCursor * InsetFormula::mathcursor = 0;
75 int MathedInset::df_asc;
76 int MathedInset::df_des;
77 int MathedInset::df_width;
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)
202 if (MathIsBinary(type))
203 for (int i = 0; i < ls; ++i) {
209 st = string(reinterpret_cast<char const *>(s), ls);
211 LyXFont const f = WhichFont(type, size);
212 return lyxfont::width(st, f);
215 int mathed_string_width(short type, int size, string const & str)
217 return mathed_string_width(type, size, reinterpret_cast<unsigned char const *>(str.c_str()), str.length());
221 int mathed_char_width(short type, int size, byte c)
223 int t = (MathIsBinary(type)) ? mathed_string_width(type, size, &c, 1) :
224 lyxfont::width(c, WhichFont(type, size));
229 int mathed_string_height(short type, int size, byte const * s,
230 int ls, int & asc, int & des)
232 LyXFont font = WhichFont(type, size);
234 for (int i = 0; i < ls; ++i) {
235 des = max(des, lyxfont::descent(s[i], font));
236 asc = max(asc, lyxfont::ascent(s[i], font));
242 int mathed_string_height(short type, int size, string const & str,
243 int & asc, int & des)
245 return mathed_string_height(type, size,
246 reinterpret_cast<unsigned char const *>(str.c_str()), str.length(),
251 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
253 LyXFont font = WhichFont(type, size);
254 des = lyxfont::descent(c, font);
255 asc = lyxfont::ascent(c, font);
260 // In a near future maybe we use a better fonts renderer
261 void MathedInset::drawStr(Painter & pain, short type, int siz,
262 int x, int y, byte const * s, int ls)
265 if (MathIsBinary(type))
266 for (int i = 0; i < ls; ++i) {
272 st = string(reinterpret_cast<char const *>(s), ls);
274 LyXFont const mf = mathed_get_font(type, siz);
275 pain.text(x, y, st, mf);
279 InsetFormula::InsetFormula(bool display)
281 par = new MathParInset; // this leaks
286 par->SetType(LM_OT_PAR);
287 par->SetStyle(LM_ST_DISPLAY);
292 InsetFormula::InsetFormula(MathParInset * p)
294 par = (p->GetType()>= LM_OT_MPAR) ?
295 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
299 disp_flag = (par->GetType()>0);
304 InsetFormula::~InsetFormula()
310 Inset * InsetFormula::Clone(Buffer const &) const
312 InsetFormula * f = new InsetFormula(par);
318 void InsetFormula::Write(Buffer const * buf, ostream & os) const
321 Latex(buf, os, false, false);
325 int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
328 //#warning Alejandro, the number of lines is not returned in this case
329 // This problem will disapear at 0.13.
330 mathed_write(par, os, &ret, fragile, label);
335 int InsetFormula::Ascii(Buffer const *, ostream & os, int) const
337 par->Write(os, false);
342 int InsetFormula::Linuxdoc(Buffer const * buf, ostream & os) const
344 return Ascii(buf, os, 0);
348 int InsetFormula::DocBook(Buffer const * buf, ostream & os) const
350 return Ascii(buf, os, 0);
354 // Check if uses AMS macros
355 void InsetFormula::Validate(LaTeXFeatures & features) const
357 // Validation only necesary if not using an AMS Style
358 if (!features.amsstyle)
359 mathedValidate(features, par);
363 void InsetFormula::Read(Buffer const *, LyXLex & lex)
365 istream & is = lex.getStream();
367 mathed_parser_file(is, lex.GetLineNo());
369 // Silly hack to read labels.
371 mathed_label.erase();
373 mathed_parse(0, 0, &par);
375 disp_flag = (par->GetType() > 0);
377 // Update line number
378 lex.setLineNo(mathed_parser_lineno());
380 //if (mathed_label) {
381 if (!mathed_label.empty()) {
382 label = mathed_label;
384 mathed_label.erase();
387 // reading of end_inset in the inset!!!
390 if (lex.GetString() == "\\end_inset")
392 lyxerr << "InsetFormula::Read: Garbage before \\end_inset,"
393 " or missing \\end_inset!" << endl;
402 int InsetFormula::ascent(BufferView *, LyXFont const &) const
404 return par->Ascent() + ((disp_flag) ? 8 : 1);
408 int InsetFormula::descent(BufferView *, LyXFont const &) const
410 return par->Descent() + ((disp_flag) ? 8 : 1);
414 int InsetFormula::width(BufferView *, LyXFont const & f) const
416 lfont_size = f.size();
418 return par->Width(); //+2;
422 void InsetFormula::draw(BufferView * bv, LyXFont const & f,
423 int baseline, float & x, bool) const
425 Painter & pain = bv->painter();
426 // Seems commenting out solves a problem.
427 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
428 font.setSize(f.size());
429 lfont_size = font.size();
430 /// Let's try to wait a bit with this... (Lgb)
431 //UpdatableInset::draw(pain, font, baseline, x);
433 // otherwise a segfault could occur
434 // in some XDrawRectangles (i.e. matrix) (Matthias)
435 if (mathcursor && mathcursor->GetPar() == par) {
436 if (mathcursor->Selection()) {
440 mathcursor->SelGetArea(&xp, &yp, n);
441 pain.fillPolygon(xp, yp, n, LColor::selection);
443 mathcursor->draw(pain, int(x), baseline);
445 par->draw(pain, int(x), baseline);
447 x += float(width(bv, font));
449 if (par->GetType() == LM_OT_PARN || par->GetType() == LM_OT_MPARN) {
450 LyXFont wfont = WhichFont(LM_TC_BF, par->size);
451 wfont.setLatex(LyXFont::OFF);
453 if (par->GetType() == LM_OT_PARN) {
456 str = string("(") + label + ")";
459 pain.text(int(x + 20), baseline, str, wfont);
460 } else if (par->GetType() == LM_OT_MPARN) {
461 MathMatrixInset * mt =
462 static_cast<MathMatrixInset*>(par);
464 MathedRowSt const * crow = mt->getRowSt();
466 y = baseline + crow->getBaseline();
467 if (crow->isNumbered()) {
469 if (!crow->getLabel().empty())
470 str = string("(") + crow->getLabel() + ")";
473 pain.text(int(x + 20), y, str, wfont);
475 crow = crow->getNext();
479 cursor_visible = false;
483 string const InsetFormula::EditMessage() const
485 return _("Math editor mode");
489 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
491 mathcursor = new MathedCursor(par);
492 if (!bv->lockInset(this))
493 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
495 bv->updateInset(this, false);
498 mathcursor->SetPos(x, y);
504 void InsetFormula::InsetUnlock(BufferView * bv)
507 if (mathcursor->InMacroMode()) {
508 mathcursor->MacroModeClose();
514 bv->updateInset(this, false);
518 // Now a symbol can be inserted only if the inset is locked
519 void InsetFormula::InsertSymbol(BufferView * bv, string const & s)
521 if (s.empty() || !mathcursor) return;
522 mathcursor->Interpret(s);
527 void InsetFormula::GetCursorPos(BufferView *, int & x, int & y) const
529 mathcursor->GetPos(x, y);
535 void InsetFormula::ToggleInsetCursor(BufferView * bv)
542 mathcursor->GetPos(x, y);
545 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
546 int asc = lyxfont::maxAscent(font);
547 int desc = lyxfont::maxDescent(font);
550 bv->hideLockedInsetCursor();
552 bv->showLockedInsetCursor(x, y, asc, desc);
553 cursor_visible = !cursor_visible;
557 void InsetFormula::ShowInsetCursor(BufferView * bv, bool)
559 if (!cursor_visible) {
563 mathcursor->GetPos(x, y);
566 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
567 int asc = lyxfont::maxAscent(font);
568 int desc = lyxfont::maxDescent(font);
569 bv->fitLockedInsetCursor(x, y, asc, desc);
571 ToggleInsetCursor(bv);
576 void InsetFormula::HideInsetCursor(BufferView * bv)
579 ToggleInsetCursor(bv);
583 void InsetFormula::ToggleInsetSelection(BufferView * bv)
591 //mathcursor->SelGetArea(n);
592 // XFillPolygon(fl_get_display(), pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
596 bv->updateInset(this, false);
601 void InsetFormula::display(bool dspf)
603 if (dspf != disp_flag) {
605 par->SetType(LM_OT_PAR);
606 par->SetStyle(LM_ST_DISPLAY);
608 if (par->GetType() >= LM_OT_MPAR) {
609 MathParInset * p = new MathParInset(par);
613 mathcursor->SetPar(par);
615 par->SetType(LM_OT_MIN);
616 par->SetStyle(LM_ST_TEXT);
617 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
626 vector<string> const InsetFormula::getLabelList() const
628 //#warning This is dirty, I know. Ill clean it at 0.11
629 // Correction, the only way to clean this is with a new kernel: 0.13.
631 vector<string> label_list;
633 if (par->GetType() == LM_OT_MPARN) {
634 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
635 MathedRowSt const * crow = mt->getRowSt();
637 if (!crow->getLabel().empty())
638 label_list.push_back(crow->getLabel());
639 crow = crow->getNext();
641 } else if (!label.empty())
642 label_list.push_back(label);
648 void InsetFormula::UpdateLocal(BufferView * bv)
650 par->Metrics(); // To inform lyx kernel the exact size
651 // (there were problems with arrays).
652 bv->updateInset(this, true);
656 void InsetFormula::InsetButtonRelease(BufferView * bv,
657 int x, int y, int /*button*/)
663 mathcursor->SetPos(x, y);
668 bv->updateInset(this, false);
674 void InsetFormula::InsetButtonPress(BufferView * bv,
675 int x, int y, int /*button*/)
678 sel_x = x; sel_y = y;
679 if (mathcursor && mathcursor->Selection()) {
680 mathcursor->SelClear();
681 bv->updateInset(this, false);
686 void InsetFormula::InsetMotionNotify(BufferView * bv,
687 int x, int y, int /*button*/)
689 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
692 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
693 mathcursor->SelStart();
695 mathcursor->GetPos(sel_x, sel_y);
696 } else if (sel_flag) {
700 mathcursor->SetPos(x, y);
702 mathcursor->GetPos(x, y);
703 if (sel_x!= x || sel_y!= y)
704 bv->updateInset(this, false);
705 sel_x = x; sel_y = y;
710 void InsetFormula::InsetKeyPress(XKeyEvent *)
712 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
716 // Special Mathed functions
717 bool InsetFormula::SetNumber(bool numbf)
720 short type = par->GetType();
721 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
722 if (numbf && !oldf) ++type;
723 if (!numbf && oldf) --type;
731 UpdatableInset::RESULT
732 InsetFormula::LocalDispatch(BufferView * bv,
733 int action, string const & arg)
735 // extern char *dispatch_result;
736 MathedTextCodes varcode = LM_TC_MIN;
737 bool was_macro = mathcursor->InMacroMode();
739 bool space_on = false;
740 bool was_selection = mathcursor->Selection();
741 RESULT result = DISPATCHED;
742 static MathSpaceInset * sp= 0;
746 if (mathcursor->getLastCode() == LM_TC_TEX) {
751 // --- Cursor Movements ---------------------------------------------
752 case LFUN_RIGHTSEL: sel = true;
755 result = DISPATCH_RESULT(mathcursor->Right(sel));
756 if (!sel && (result == DISPATCHED))
757 result = DISPATCHED_NOUPDATE;
760 case LFUN_LEFTSEL: sel = true;
763 result = DISPATCH_RESULT(mathcursor->Left(sel));
764 if (!sel && (result == DISPATCHED))
765 result = DISPATCHED_NOUPDATE;
768 case LFUN_UPSEL: sel = true;
770 result = DISPATCH_RESULT(mathcursor->Up(sel));
771 if (!sel && (result == DISPATCHED))
772 result = DISPATCHED_NOUPDATE;
775 case LFUN_DOWNSEL: sel = true;
777 result = DISPATCH_RESULT(mathcursor->Down(sel));
778 if (!sel && (result == DISPATCHED))
779 result = DISPATCHED_NOUPDATE;
783 result = DISPATCHED_NOUPDATE;
787 result = DISPATCHED_NOUPDATE;
789 case LFUN_DELETE_LINE_FORWARD:
790 bv->lockedInsetStoreUndo(Undo::DELETE);
791 mathcursor->DelLine();
795 bv->lockedInsetStoreUndo(Undo::INSERT);
796 mathcursor->Insert(' ', LM_TC_CR);
797 par = mathcursor->GetPar();
801 bv->lockedInsetStoreUndo(Undo::INSERT);
802 mathcursor->Insert(0, LM_TC_TAB);
806 bv->lockedInsetStoreUndo(Undo::INSERT);
807 mathcursor->Insert('T', LM_TC_TAB);
811 if (!mathcursor->Left())
814 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
815 bv->updateInset(this, true);
820 bv->lockedInsetStoreUndo(Undo::DELETE);
821 mathcursor->Delete();
822 bv->updateInset(this, true);
825 // sprintf(dispatch_buffer, "%d %d",);
826 // dispatch_result = dispatch_buffer;
834 istringstream ist(arg.c_str());
837 mathcursor->SetPos(x1 + x, y1 + y);
841 /* cursor selection ---------------------------- */
845 mathcursor->MacroModeClose();
846 bv->lockedInsetStoreUndo(Undo::INSERT);
847 mathcursor->SelPaste(); UpdateLocal(bv); break;
849 bv->lockedInsetStoreUndo(Undo::DELETE);
850 mathcursor->SelCut(); UpdateLocal(bv); break;
851 case LFUN_COPY: mathcursor->SelCopy(); break;
854 case LFUN_WORDRIGHTSEL:
855 case LFUN_WORDLEFTSEL:
858 // --- accented characters ------------------------------
860 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
861 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
862 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
863 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
864 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
865 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
866 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
867 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
868 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
869 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
874 if (!greek_kb_flag) {
876 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
883 case LFUN_GREEK_TOGGLE:
885 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
887 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
889 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
894 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
895 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
896 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
897 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
898 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
899 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
902 // varcode = LM_TC_TEX;
903 mathcursor->setLastCode(LM_TC_TEX);
904 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
908 case LFUN_MATH_NUMBER:
910 bv->lockedInsetStoreUndo(Undo::INSERT);
912 short type = par->GetType();
913 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
916 if (!label.empty()) {
919 bv->owner()->getMiniBuffer()->Set(_("No number"));
922 bv->owner()->getMiniBuffer()->Set(_("Number"));
930 case LFUN_MATH_NONUMBER:
932 if (par->GetType() == LM_OT_MPARN) {
933 // MathMatrixInset *mt = (MathMatrixInset*)par;
935 // mt->SetNumbered(!mt->IsNumbered());
937 mathcursor->setNumbered();
943 case LFUN_MATH_LIMITS:
945 bv->lockedInsetStoreUndo(Undo::INSERT);
946 if (mathcursor->Limits())
952 latexkeys * l = in_word_set (arg);
953 int sz = (l) ? l->id: -1;
954 mathcursor->SetSize(sz);
959 case LFUN_INSERT_MATH:
961 bv->lockedInsetStoreUndo(Undo::INSERT);
962 InsertSymbol(bv, arg);
966 case LFUN_INSERT_MATRIX:
968 bv->lockedInsetStoreUndo(Undo::INSERT);
970 char s[80], arg2[80];
971 // This is just so that too long args won't ooze out of s.
972 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
973 k = sscanf(arg2, "%d %d %s", &m, &n, s);
982 MathMatrixInset * p = new MathMatrixInset(m, n);
983 if (mathcursor && p) {
984 if (k > 2 && int(strlen(s)) > m)
985 p->SetAlign(s[0], &s[1]);
986 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
992 case LFUN_MATH_DELIM:
994 bv->lockedInsetStoreUndo(Undo::INSERT);
995 char lf[40], rg[40], arg2[40];
996 int ilf = '(', irg = '.';
998 string vdelim("(){}[]./|");
1002 ::strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
1003 int n = sscanf(arg2, "%s %s", lf, rg);
1004 lf[39] = '\0'; rg[39] = '\0';
1008 ilf = lyx::atoi(lf);
1011 l = in_word_set(lf, strlen(lf));
1012 // Long words will cause l == 0; so check.
1015 if (vdelim.find(lf[0]) != string::npos)
1020 irg = lyx::atoi(rg);
1023 l = in_word_set(rg, strlen(rg));
1026 if (vdelim.find(rg[0]) != string::npos)
1031 MathDelimInset * p = new MathDelimInset(ilf, irg);
1032 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1037 case LFUN_PROTECTEDSPACE:
1039 bv->lockedInsetStoreUndo(Undo::INSERT);
1040 sp = new MathSpaceInset(1);
1041 mathcursor->Insert(sp);
1047 case LFUN_INSERT_LABEL:
1049 bv->lockedInsetStoreUndo(Undo::INSERT);
1050 if (par->GetType() < LM_OT_PAR) break;
1054 res = askForText(_("Enter new label to insert:"));
1059 if (!lb.empty() && lb[0] > ' ') {
1061 if (par->GetType() == LM_OT_MPARN) {
1062 mathcursor->setLabel(lb);
1063 // MathMatrixInset *mt = (MathMatrixInset*)par;
1064 // mt->SetLabel(lb);
1066 //if (label.notEmpty()) delete label;
1075 case LFUN_MATH_DISPLAY:
1076 bv->lockedInsetStoreUndo(Undo::EDIT);
1077 display(!disp_flag);
1081 // Invalid actions under math mode
1082 case LFUN_MATH_MODE:
1084 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1085 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1086 varcode = LM_TC_TEXTRM;
1088 varcode = LM_TC_VAR;
1090 mathcursor->setLastCode(varcode);
1094 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1097 //------- dummy actions
1098 case LFUN_EXEC_COMMAND:
1099 bv->owner()->getMiniBuffer()->PrepareForCommand();
1103 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1104 unsigned char c = arg[0];
1105 bv->lockedInsetStoreUndo(Undo::INSERT);
1107 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1109 mathcursor->setAccent(0);
1111 if (c == 0) { // Dead key, do nothing
1112 //lyxerr << "deadkey" << endl;
1116 if (mathcursor->getLastCode() == LM_TC_TEX) {
1117 mathcursor->MacroModeOpen();
1118 mathcursor->clearLastCode();
1119 varcode = LM_TC_MIN;
1122 short f = (mathcursor->getLastCode()) ?
1123 mathcursor->getLastCode() :
1124 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1125 varcode = MathIsAlphaFont(f) ?
1126 static_cast<MathedTextCodes>(f) :
1130 // lyxerr << "Varcode << vardoce;
1131 MathedTextCodes char_code = varcode;
1132 if (greek_kb_flag) {
1134 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1135 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1136 'Y', 0, 0, 0, 0 , 'Z' };
1137 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1138 char_code = LM_TC_RM;
1141 char_code = LM_TC_SYMB;
1143 mathcursor->Insert(c, char_code);
1144 if (greek_kb_flag && char_code == LM_TC_RM )
1145 mathcursor->setLastCode(LM_TC_VAR);
1146 varcode = LM_TC_MIN;
1147 if (greek_kb_flag<2) greek_kb_flag = 0;
1149 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1150 mathcursor->Insert(c, LM_TC_TEX);
1152 mathcursor->Insert('}', LM_TC_TEX);
1155 mathcursor->clearLastCode();
1156 // varcode = LM_TC_MIN;
1158 if (c == '_' && varcode == LM_TC_TEX) {
1159 mathcursor->Insert(c, LM_TC_SPECIAL);
1160 mathcursor->clearLastCode();
1161 // varcode = LM_TC_MIN;
1163 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1164 mathcursor->MacroModeOpen();
1165 mathcursor->clearLastCode();
1166 mathcursor->Insert(c, LM_TC_MIN);
1169 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1170 mathcursor->Insert(c, LM_TC_CONST);
1172 if (strchr("+/-*<>=", c))
1173 mathcursor->Insert(c, LM_TC_BOP);
1175 if (strchr(latex_special_chars, c) && c!= '_')
1176 mathcursor->Insert(c, LM_TC_SPECIAL);
1178 if (c == '_' || c == '^') {
1182 mathcursor->Interpret (s);
1186 short f = (mathcursor->getLastCode()) ?
1187 mathcursor->getLastCode() :
1188 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1189 varcode = MathIsAlphaFont(f) ?
1190 static_cast<MathedTextCodes>(f) :
1193 if (varcode == LM_TC_TEXTRM) {
1194 mathcursor->Insert(c, LM_TC_TEXTRM);
1197 mathcursor->MacroModeClose();
1200 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1204 if (!mathcursor->Pop() && mathcursor->IsEnd())
1208 if (c == '\'' || c == '@') {
1209 mathcursor->Insert (c, LM_TC_VAR);
1213 mathcursor->MacroModeClose();
1214 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1215 mathcursor->setLastCode(LM_TC_TEX);
1219 // lyxerr << "Closed by action " << action << endl;
1223 if (was_macro != mathcursor->InMacroMode()
1225 && action != LFUN_BACKSPACE)
1227 if (sp && !space_on) sp = 0;
1228 if (mathcursor->Selection() || was_selection)
1229 ToggleInsetSelection(bv);
1231 if ((result == DISPATCHED) || (result == DISPATCHED_NOUPDATE))
1232 ShowInsetCursor(bv);
1234 bv->unlockInset(this);
1241 MathFuncInset::draw(Painter & pain, int x, int y)
1243 if (!name.empty() && name[0] > ' ') {
1244 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1245 font.setLatex(LyXFont::ON);
1246 x += (lyxfont::width('I', font) + 3) / 4;
1247 pain.text(x, y, name, font);
1252 void MathFuncInset::Metrics()
1254 //ln = (name) ? strlen(name): 0;
1255 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1256 font.setLatex(LyXFont::ON);
1262 width = lyxfont::width(name, font)
1263 + lyxfont::width('I', font) / 2;
1264 mathed_string_height(LM_TC_TEXTRM, size, name, ascent, descent);
1270 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1272 MathedIter it(par->GetData());
1274 while (it.OK() && !(features.binom && features.boldsymbol)) {
1276 if (it.IsActive()) {
1277 MathParInset * p = it.GetActiveInset();
1278 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1279 p->GetName() == "binom") {
1280 features.binom = true;
1282 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1283 p->setArgumentIdx(i);
1284 mathedValidate(features, p);
1288 MathedInset* p = it.GetInset();
1289 if (!features.boldsymbol &&
1290 p->GetName() == "boldsymbol") {
1291 features.boldsymbol = true;