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;
50 extern char const * latex_special_chars;
52 int greek_kb_flag = 0;
54 LyXFont * Math_Fonts = 0; // this is only used by Whichfont and mathed_init_fonts (Lgb)
56 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
59 static int sel_x, sel_y;
61 MathedCursor * InsetFormula::mathcursor = 0;
64 int MathedInset::df_asc;
65 int MathedInset::df_des;
66 int MathedInset::df_width;
70 bool IsMacro(short token, int id)
72 return (token != LM_TK_FRAC && token != LM_TK_SQRT &&
73 !((token == LM_TK_SYM || token == LM_TC_BSYM) && id < 255));
78 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
81 LyXFont WhichFont(short type, int size)
111 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
121 f.setSize(lfont_size);
125 if (type == LM_TC_BSYM) {
135 case LM_ST_SCRIPTSCRIPT:
140 lyxerr << "Mathed Error: wrong font size: " << size << endl;
144 if (type != LM_TC_TEXTRM)
145 f.setColor(LColor::math);
150 void mathed_init_fonts() //removed 'static' because DEC cxx does not
152 // Probably because this func is declared as a friend in math_defs.h
156 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
158 for (int i = 0 ; i < 8 ; ++i) {
159 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
161 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
163 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
165 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
166 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
168 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
170 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
171 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
173 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
175 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
177 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
179 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
180 MathedInset::df_asc = lyxfont::maxAscent(f);
181 MathedInset::df_des = lyxfont::maxDescent(f);
182 MathedInset::df_width = lyxfont::width('I', f);
186 LyXFont mathed_get_font(short type, int size)
188 LyXFont f = WhichFont(type, size);
189 if (type == LM_TC_TEX) {
190 f.setLatex(LyXFont::ON);
196 int mathed_string_width(short type, int size, byte const * s, int ls)
198 LyXFont f = WhichFont(type, size);
201 if (MathIsBinary(type)) {
203 for (int i = 0; i < ls && i < 75; ++i) {
212 return lyxfont::width(reinterpret_cast<char const *>(s), ls, 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) {
267 st += string(" ") + char(s[i]) + ' ';
270 st = string(reinterpret_cast<char const *>(s), ls);
272 LyXFont const mf = mathed_get_font(type, siz);
273 pain.text(x, y, st, mf);
277 InsetFormula::InsetFormula(bool display)
279 par = new MathParInset; // this leaks
284 par->SetType(LM_OT_PAR);
285 par->SetStyle(LM_ST_DISPLAY);
290 InsetFormula::InsetFormula(MathParInset * p)
292 par = (p->GetType()>= LM_OT_MPAR) ?
293 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
297 disp_flag = (par->GetType()>0);
302 InsetFormula::~InsetFormula()
308 Inset * InsetFormula::Clone() const
310 InsetFormula * f = new InsetFormula(par);
316 void InsetFormula::Write(Buffer const * buf, ostream & os) const
319 Latex(buf, os, false, false);
323 int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
326 //#warning Alejandro, the number of lines is not returned in this case
327 // This problem will disapear at 0.13.
328 mathed_write(par, os, &ret, fragile, label.c_str());
333 int InsetFormula::Ascii(Buffer const *, ostream & os) const
335 par->Write(os, false);
340 int InsetFormula::Linuxdoc(Buffer const * buf, ostream & os) const
342 return Ascii(buf, os);
346 int InsetFormula::DocBook(Buffer const * buf, ostream & os) const
348 return Ascii(buf, os);
352 // Check if uses AMS macros
353 void InsetFormula::Validate(LaTeXFeatures & features) const
355 // Validation only necesary if not using an AMS Style
356 if (!features.amsstyle)
357 mathedValidate(features, par);
361 void InsetFormula::Read(Buffer const *, LyXLex & lex)
363 istream & is = lex.getStream();
365 mathed_parser_file(is, lex.GetLineNo());
367 // Silly hack to read labels.
369 mathed_parse(0, 0, &par);
371 disp_flag = (par->GetType() > 0);
373 // Update line number
374 lex.setLineNo(mathed_parser_lineno());
377 label = mathed_label;
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)
535 mathcursor->GetPos(x, y);
538 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
539 int asc = lyxfont::maxAscent(font);
540 int desc = lyxfont::maxDescent(font);
543 bv->hideLockedInsetCursor();
545 bv->showLockedInsetCursor(x, y, asc, desc);
546 cursor_visible = !cursor_visible;
550 void InsetFormula::ShowInsetCursor(BufferView * bv)
552 if (!cursor_visible) {
555 mathcursor->GetPos(x, y);
558 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
559 int asc = lyxfont::maxAscent(font);
560 int desc = lyxfont::maxDescent(font);
561 bv->fitLockedInsetCursor(x, y, asc, desc);
563 ToggleInsetCursor(bv);
568 void InsetFormula::HideInsetCursor(BufferView * bv)
571 ToggleInsetCursor(bv);
575 void InsetFormula::ToggleInsetSelection(BufferView * bv)
583 //mathcursor->SelGetArea(n);
584 // XFillPolygon(fl_display, pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
588 bv->updateInset(this, false);
593 void InsetFormula::display(bool dspf)
595 if (dspf != disp_flag) {
597 par->SetType(LM_OT_PAR);
598 par->SetStyle(LM_ST_DISPLAY);
600 if (par->GetType() >= LM_OT_MPAR) {
601 MathParInset * p = new MathParInset(par);
605 mathcursor->SetPar(par);
607 par->SetType(LM_OT_MIN);
608 par->SetStyle(LM_ST_TEXT);
609 if (!label.empty() && par->GetType() != LM_OT_MPARN) {
618 vector<string> const InsetFormula::getLabelList() const
620 //#warning This is dirty, I know. Ill clean it at 0.11
621 // Correction, the only way to clean this is with a new kernel: 0.13.
623 vector<string> label_list;
625 if (par->GetType() == LM_OT_MPARN) {
626 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
627 MathedRowSt const * crow = mt->getRowSt();
629 if (!crow->getLabel().empty())
630 label_list.push_back(crow->getLabel());
631 crow = crow->getNext();
633 } else if (!label.empty())
634 label_list.push_back(label);
640 void InsetFormula::UpdateLocal(BufferView * bv)
642 par->Metrics(); // To inform lyx kernel the exact size
643 // (there were problems with arrays).
644 bv->updateInset(this, true);
648 void InsetFormula::InsetButtonRelease(BufferView * bv,
649 int x, int y, int /*button*/)
655 mathcursor->SetPos(x, y);
660 bv->updateInset(this, false);
666 void InsetFormula::InsetButtonPress(BufferView * bv,
667 int x, int y, int /*button*/)
670 sel_x = x; sel_y = y;
671 if (mathcursor && mathcursor->Selection()) {
672 mathcursor->SelClear();
673 bv->updateInset(this, false);
678 void InsetFormula::InsetMotionNotify(BufferView * bv,
679 int x, int y, int /*button*/)
681 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
684 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
685 mathcursor->SelStart();
687 mathcursor->GetPos(sel_x, sel_y);
693 mathcursor->SetPos(x, y);
695 mathcursor->GetPos(x, y);
696 if (sel_x!= x || sel_y!= y)
697 bv->updateInset(this, false);
698 sel_x = x; sel_y = y;
703 void InsetFormula::InsetKeyPress(XKeyEvent *)
705 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
709 // Special Mathed functions
710 bool InsetFormula::SetNumber(bool numbf)
713 short type = par->GetType();
714 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
715 if (numbf && !oldf) ++type;
716 if (!numbf && oldf) --type;
724 UpdatableInset::RESULT
725 InsetFormula::LocalDispatch(BufferView * bv,
726 int action, string const & arg)
728 // extern char *dispatch_result;
729 MathedTextCodes varcode = LM_TC_MIN;
730 bool was_macro = mathcursor->InMacroMode();
732 bool space_on = false;
733 bool was_selection = mathcursor->Selection();
734 RESULT result = DISPATCHED;
735 static MathSpaceInset * sp= 0;
739 if (mathcursor->getLastCode() == LM_TC_TEX) {
744 // --- Cursor Movements ---------------------------------------------
745 case LFUN_RIGHTSEL: sel = true;
748 result = DISPATCH_RESULT(mathcursor->Right(sel));
749 if (!sel && (result == DISPATCHED))
750 result = DISPATCHED_NOUPDATE;
753 case LFUN_LEFTSEL: sel = true;
756 result = DISPATCH_RESULT(mathcursor->Left(sel));
757 if (!sel && (result == DISPATCHED))
758 result = DISPATCHED_NOUPDATE;
761 case LFUN_UPSEL: sel = true;
763 result = DISPATCH_RESULT(mathcursor->Up(sel));
764 if (!sel && (result == DISPATCHED))
765 result = DISPATCHED_NOUPDATE;
768 case LFUN_DOWNSEL: sel = true;
770 result = DISPATCH_RESULT(mathcursor->Down(sel));
771 if (!sel && (result == DISPATCHED))
772 result = DISPATCHED_NOUPDATE;
776 result = DISPATCHED_NOUPDATE;
780 result = DISPATCHED_NOUPDATE;
782 case LFUN_DELETE_LINE_FORWARD:
783 //current_view->lockedInsetStoreUndo(Undo::INSERT);
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 //current_view->lockedInsetStoreUndo(Undo::INSERT);
815 bv->lockedInsetStoreUndo(Undo::DELETE);
816 mathcursor->Delete();
817 bv->updateInset(this, true);
820 // sprintf(dispatch_buffer, "%d %d",);
821 // dispatch_result = dispatch_buffer;
826 istringstream ist(arg.c_str());
829 mathcursor->SetPos(x1 + x, y1 + y);
833 /* cursor selection ---------------------------- */
837 mathcursor->MacroModeClose();
838 bv->lockedInsetStoreUndo(Undo::INSERT);
839 mathcursor->SelPaste(); UpdateLocal(bv); break;
841 bv->lockedInsetStoreUndo(Undo::DELETE);
842 mathcursor->SelCut(); UpdateLocal(bv); break;
843 case LFUN_COPY: mathcursor->SelCopy(); break;
846 case LFUN_WORDRIGHTSEL:
847 case LFUN_WORDLEFTSEL:
850 // --- accented characters ------------------------------
852 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
853 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
854 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
855 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
856 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
857 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
858 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
859 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
860 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
861 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
866 if (!greek_kb_flag) {
868 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
875 case LFUN_GREEK_TOGGLE:
877 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
879 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
881 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
886 case LFUN_BOLD: mathcursor->setLastCode(LM_TC_BF); break;
887 case LFUN_SANS: mathcursor->setLastCode( LM_TC_SF); break;
888 case LFUN_EMPH: mathcursor->setLastCode(LM_TC_CAL); break;
889 case LFUN_ROMAN: mathcursor->setLastCode(LM_TC_RM); break;
890 case LFUN_CODE: mathcursor->setLastCode(LM_TC_TT); break;
891 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR ) ; break;
894 // varcode = LM_TC_TEX;
895 mathcursor->setLastCode(LM_TC_TEX);
896 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
900 case LFUN_MATH_NUMBER:
902 bv->lockedInsetStoreUndo(Undo::INSERT);
904 short type = par->GetType();
905 bool oldf = (type == LM_OT_PARN || type == LM_OT_MPARN);
908 if (!label.empty()) {
911 bv->owner()->getMiniBuffer()->Set(_("No number"));
914 bv->owner()->getMiniBuffer()->Set(_("Number"));
922 case LFUN_MATH_NONUMBER:
924 if (par->GetType() == LM_OT_MPARN) {
925 // MathMatrixInset *mt = (MathMatrixInset*)par;
927 // mt->SetNumbered(!mt->IsNumbered());
929 mathcursor->setNumbered();
935 case LFUN_MATH_LIMITS:
937 bv->lockedInsetStoreUndo(Undo::INSERT);
938 if (mathcursor->Limits())
944 latexkeys * l = in_word_set (arg.c_str(), strlen(arg.c_str()));
945 int sz = (l) ? l->id: -1;
946 mathcursor->SetSize(sz);
951 case LFUN_INSERT_MATH:
953 bv->lockedInsetStoreUndo(Undo::INSERT);
954 InsertSymbol(bv, arg.c_str());
958 case LFUN_INSERT_MATRIX:
960 bv->lockedInsetStoreUndo(Undo::INSERT);
962 char s[80], arg2[80];
963 // This is just so that too long args won't ooze out of s.
964 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
965 k = sscanf(arg2, "%d %d %s", &m, &n, s);
974 MathMatrixInset * p = new MathMatrixInset(m, n);
975 if (mathcursor && p) {
976 if (k > 2 && int(strlen(s)) > m)
977 p->SetAlign(s[0], &s[1]);
978 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
984 case LFUN_MATH_DELIM:
986 bv->lockedInsetStoreUndo(Undo::INSERT);
987 char lf[40], rg[40], arg2[40];
988 int ilf = '(', irg = '.';
990 string vdelim("(){}[]./|");
994 strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
995 int n = sscanf(arg2, "%s %s", lf, rg);
996 lf[39] = '\0'; rg[39] = '\0';
1003 l = in_word_set(lf, strlen(lf));
1004 // Long words will cause l == 0; so check.
1007 if (vdelim.find(lf[0]) != string::npos)
1015 l = in_word_set(rg, strlen(rg));
1018 if (vdelim.find(rg[0]) != string::npos)
1023 MathDelimInset * p = new MathDelimInset(ilf, irg);
1024 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1029 case LFUN_PROTECTEDSPACE:
1031 bv->lockedInsetStoreUndo(Undo::INSERT);
1032 sp = new MathSpaceInset(1);
1033 mathcursor->Insert(sp);
1039 case LFUN_INSERT_LABEL:
1041 bv->lockedInsetStoreUndo(Undo::INSERT);
1042 if (par->GetType() < LM_OT_PAR) break;
1046 res = askForText(_("Enter new label to insert:"));
1051 if (!lb.empty() && lb[0] > ' ') {
1053 if (par->GetType() == LM_OT_MPARN) {
1054 mathcursor->setLabel(lb.c_str());
1055 // MathMatrixInset *mt = (MathMatrixInset*)par;
1056 // mt->SetLabel(lb);
1058 //if (label.notEmpty()) delete label;
1067 case LFUN_MATH_DISPLAY:
1068 //current_view->lockedInsetStoreUndo(Undo::INSERT);
1069 bv->lockedInsetStoreUndo(Undo::EDIT);
1070 display(!disp_flag);
1074 // Invalid actions under math mode
1075 case LFUN_MATH_MODE:
1077 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1078 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1079 varcode = LM_TC_TEXTRM;
1081 varcode = LM_TC_VAR;
1083 mathcursor->setLastCode(varcode);
1087 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1090 //------- dummy actions
1091 case LFUN_EXEC_COMMAND:
1092 bv->owner()->getMiniBuffer()->ExecCommand();
1096 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1097 unsigned char c = arg[0];
1098 bv->lockedInsetStoreUndo(Undo::INSERT);
1100 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1102 mathcursor->setAccent(0);
1104 if (c == 0) { // Dead key, do nothing
1105 //lyxerr << "deadkey" << endl;
1109 if (mathcursor->getLastCode() == LM_TC_TEX) {
1110 mathcursor->MacroModeOpen();
1111 mathcursor->clearLastCode();
1112 varcode = LM_TC_MIN;
1115 short f = (mathcursor->getLastCode()) ?
1116 mathcursor->getLastCode() :
1117 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1118 varcode = MathIsAlphaFont(f) ?
1119 static_cast<MathedTextCodes>(f) :
1123 // lyxerr << "Varcode << vardoce;
1124 MathedTextCodes char_code = varcode;
1125 if (greek_kb_flag) {
1127 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1128 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1129 'Y', 0, 0, 0, 0 , 'Z' };
1130 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1131 char_code = LM_TC_RM;
1134 char_code = LM_TC_SYMB;
1136 mathcursor->Insert(c, char_code);
1137 if (greek_kb_flag && char_code == LM_TC_RM )
1138 mathcursor->setLastCode(LM_TC_VAR);
1139 varcode = LM_TC_MIN;
1140 if (greek_kb_flag<2) greek_kb_flag = 0;
1142 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1143 mathcursor->Insert(c, LM_TC_TEX);
1145 mathcursor->Insert('}', LM_TC_TEX);
1148 mathcursor->clearLastCode();
1149 // varcode = LM_TC_MIN;
1151 if (c == '_' && varcode == LM_TC_TEX) {
1152 mathcursor->Insert(c, LM_TC_SPECIAL);
1153 mathcursor->clearLastCode();
1154 // varcode = LM_TC_MIN;
1156 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1157 mathcursor->MacroModeOpen();
1158 mathcursor->clearLastCode();
1159 mathcursor->Insert(c, LM_TC_MIN);
1162 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1163 mathcursor->Insert(c, LM_TC_CONST);
1165 if (strchr("+/-*<>=", c))
1166 mathcursor->Insert(c, LM_TC_BOP);
1168 if (strchr(latex_special_chars, c) && c!= '_')
1169 mathcursor->Insert(c, LM_TC_SPECIAL);
1171 if (c == '_' || c == '^') {
1175 mathcursor->Interpret (s);
1179 short f = (mathcursor->getLastCode()) ?
1180 mathcursor->getLastCode() :
1181 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1182 varcode = MathIsAlphaFont(f) ?
1183 static_cast<MathedTextCodes>(f) :
1186 if (varcode == LM_TC_TEXTRM) {
1187 mathcursor->Insert(c, LM_TC_TEXTRM);
1190 mathcursor->MacroModeClose();
1193 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1197 if (!mathcursor->Pop() && mathcursor->IsEnd())
1202 mathcursor->Insert (c, LM_TC_VAR);
1206 mathcursor->MacroModeClose();
1207 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1208 mathcursor->setLastCode(LM_TC_TEX);
1212 // lyxerr << "Closed by action " << action << endl;
1216 if (was_macro != mathcursor->InMacroMode()
1218 && action != LFUN_BACKSPACE)
1220 if (sp && !space_on) sp = 0;
1221 if (mathcursor->Selection() || was_selection)
1222 ToggleInsetSelection(bv);
1224 if ((result == DISPATCHED) || (result == DISPATCHED_NOUPDATE))
1225 ShowInsetCursor(bv);
1227 bv->unlockInset(this);
1234 MathFuncInset::draw(Painter & pain, int x, int y)
1236 if (!name.empty() && name[0] > ' ') {
1237 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1238 font.setLatex(LyXFont::ON);
1239 x += (lyxfont::width('I', font) + 3) / 4;
1240 pain.text(x, y, name, font);
1245 void MathFuncInset::Metrics()
1247 //ln = (name) ? strlen(name): 0;
1248 LyXFont font = WhichFont(LM_TC_TEXTRM, size);
1249 font.setLatex(LyXFont::ON);
1255 width = lyxfont::width(name, font)
1256 + lyxfont::width('I', font) / 2;
1257 mathed_string_height(LM_TC_TEXTRM, size, name, ascent, descent);
1263 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1265 MathedIter it(par->GetData());
1267 while (it.OK() && !(features.binom && features.boldsymbol)) {
1270 MathParInset * p = it.GetActiveInset();
1271 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1272 p->GetName() == "binom") {
1273 features.binom = true;
1275 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1276 p->setArgumentIdx(i);
1277 mathedValidate(features, p);
1281 MathedInset* p = it.GetInset();
1282 if (!features.boldsymbol &&
1283 p->GetName() == "boldsymbol") {
1284 features.boldsymbol = true;