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"
42 #include "math_inset.h"
43 #include "math_parinset.h"
44 #include "math_matrixinset.h"
45 #include "math_rowst.h"
46 #include "math_spaceinset.h"
47 #include "math_deliminset.h"
48 #include "mathed/support.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;
74 void mathed_init_fonts();
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);
196 InsetFormula::InsetFormula(bool display)
198 par = new MathParInset; // this leaks
203 par->SetType(LM_OT_PAR);
204 par->SetStyle(LM_ST_DISPLAY);
209 InsetFormula::InsetFormula(MathParInset * p)
211 if (is_matrix_type(p->GetType()))
212 lyxerr << "InsetFormula::InsetFormula: This shouldn't happen" << endl;
213 par = is_multiline(p->GetType()) ?
214 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
218 disp_flag = (par->GetType()>0);
223 InsetFormula::~InsetFormula()
229 Inset * InsetFormula::Clone(Buffer const &) const
231 InsetFormula * f = new InsetFormula(par);
237 void InsetFormula::Write(Buffer const * buf, ostream & os) const
240 Latex(buf, os, false, false);
244 int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
247 //#warning Alejandro, the number of lines is not returned in this case
248 // This problem will disapear at 0.13.
249 mathed_write(par, os, &ret, fragile, label);
254 int InsetFormula::Ascii(Buffer const *, ostream & os, int) const
256 par->Write(os, false);
261 int InsetFormula::Linuxdoc(Buffer const * buf, ostream & os) const
263 return Ascii(buf, os, 0);
267 int InsetFormula::DocBook(Buffer const * buf, ostream & os) const
269 return Ascii(buf, os, 0);
273 // Check if uses AMS macros
274 void InsetFormula::Validate(LaTeXFeatures & features) const
276 if (is_ams(par->GetType()))
277 features.amsstyle = true;
279 // Validation is necessary only if not using AMS math.
280 // To be safe, we will always run mathedValidate.
281 //if (!features.amsstyle)
282 mathedValidate(features, par);
286 void InsetFormula::Read(Buffer const *, LyXLex & lex)
288 istream & is = lex.getStream();
290 mathed_parser_file(is, lex.GetLineNo());
292 // Silly hack to read labels.
294 mathed_label.erase();
296 mathed_parse(0, 0, &par);
298 disp_flag = (par->GetType() > 0);
300 // Update line number
301 lex.setLineNo(mathed_parser_lineno());
303 //if (mathed_label) {
304 if (!mathed_label.empty()) {
305 label = mathed_label;
307 mathed_label.erase();
310 // reading of end_inset in the inset!!!
313 if (lex.GetString() == "\\end_inset")
315 lyxerr << "InsetFormula::Read: Garbage before \\end_inset,"
316 " or missing \\end_inset!" << endl;
325 int InsetFormula::ascent(BufferView *, LyXFont const &) const
327 return par->Ascent() + ((disp_flag) ? 8 : 1);
331 int InsetFormula::descent(BufferView *, LyXFont const &) const
333 return par->Descent() + ((disp_flag) ? 8 : 1);
337 int InsetFormula::width(BufferView * bv, LyXFont const & f) const
339 MathedInset::workWidth = bv->workWidth();
340 lfont_size = f.size();
342 return par->Width(); //+2;
346 void InsetFormula::draw(BufferView * bv, LyXFont const & f,
347 int baseline, float & x, bool) const
349 MathedInset::workWidth = bv->workWidth();
350 Painter & pain = bv->painter();
351 // Seems commenting out solves a problem.
352 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
353 font.setSize(f.size());
354 lfont_size = font.size();
355 /// Let's try to wait a bit with this... (Lgb)
356 //UpdatableInset::draw(pain, font, baseline, x);
358 // otherwise a segfault could occur
359 // in some XDrawRectangles (i.e. matrix) (Matthias)
360 if (mathcursor && mathcursor->GetPar() == par) {
361 if (mathcursor->Selection()) {
365 mathcursor->SelGetArea(&xp, &yp, n);
366 pain.fillPolygon(xp, yp, n, LColor::selection);
368 mathcursor->draw(pain, int(x), baseline);
370 par->draw(pain, int(x), baseline);
372 x += float(width(bv, font));
374 if (is_numbered(par->GetType())) {
375 LyXFont wfont = WhichFont(LM_TC_BF, par->size);
376 wfont.setLatex(LyXFont::OFF);
378 if (is_singlely_numbered(par->GetType())) {
381 str = string("(") + label + ")";
384 pain.text(int(x + 20), baseline, str, wfont);
386 MathMatrixInset * mt =
387 static_cast<MathMatrixInset*>(par);
389 MathedRowSt const * crow = mt->getRowSt();
391 y = baseline + crow->getBaseline();
392 if (crow->isNumbered()) {
394 if (!crow->getLabel().empty())
395 str = string("(") + crow->getLabel() + ")";
398 pain.text(int(x + 20), y, str, wfont);
400 crow = crow->getNext();
404 cursor_visible = false;
408 string const InsetFormula::EditMessage() const
410 return _("Math editor mode");
414 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
416 mathcursor = new MathedCursor(par);
417 if (!bv->lockInset(this))
418 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
420 bv->updateInset(this, false);
423 mathcursor->SetPos(x, y);
429 void InsetFormula::InsetUnlock(BufferView * bv)
432 if (mathcursor->InMacroMode()) {
433 mathcursor->MacroModeClose();
439 bv->updateInset(this, false);
443 // Now a symbol can be inserted only if the inset is locked
444 void InsetFormula::InsertSymbol(BufferView * bv, string const & s)
446 if (s.empty() || !mathcursor) return;
447 mathcursor->Interpret(s);
452 void InsetFormula::GetCursorPos(BufferView *, int & x, int & y) const
454 mathcursor->GetPos(x, y);
460 void InsetFormula::ToggleInsetCursor(BufferView * bv)
467 mathcursor->GetPos(x, y);
470 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
471 int asc = lyxfont::maxAscent(font);
472 int desc = lyxfont::maxDescent(font);
475 bv->hideLockedInsetCursor();
477 bv->showLockedInsetCursor(x, y, asc, desc);
478 cursor_visible = !cursor_visible;
482 void InsetFormula::ShowInsetCursor(BufferView * bv, bool)
484 if (!cursor_visible) {
488 mathcursor->GetPos(x, y);
491 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
492 int asc = lyxfont::maxAscent(font);
493 int desc = lyxfont::maxDescent(font);
494 bv->fitLockedInsetCursor(x, y, asc, desc);
496 ToggleInsetCursor(bv);
501 void InsetFormula::HideInsetCursor(BufferView * bv)
504 ToggleInsetCursor(bv);
508 void InsetFormula::ToggleInsetSelection(BufferView * bv)
516 //mathcursor->SelGetArea(n);
517 // XFillPolygon(fl_get_display(), pm, LyXGetSelectionGC(), p, n, Nonconvex, CoordModeOrigin);
521 bv->updateInset(this, false);
526 void InsetFormula::display(bool dspf)
528 if (dspf != disp_flag) {
530 par->SetType(LM_OT_PAR);
531 par->SetStyle(LM_ST_DISPLAY);
533 if (is_multiline(par->GetType())) {
534 MathParInset * p = new MathParInset(par);
538 mathcursor->SetPar(par);
540 par->SetType(LM_OT_MIN);
541 par->SetStyle(LM_ST_TEXT);
542 if (!label.empty()) {
551 vector<string> const InsetFormula::getLabelList() const
553 //#warning This is dirty, I know. Ill clean it at 0.11
554 // Correction, the only way to clean this is with a new kernel: 0.13.
556 vector<string> label_list;
558 if (is_multi_numbered(par->GetType())) {
559 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
560 MathedRowSt const * crow = mt->getRowSt();
562 if (!crow->getLabel().empty())
563 label_list.push_back(crow->getLabel());
564 crow = crow->getNext();
566 } else if (!label.empty())
567 label_list.push_back(label);
573 void InsetFormula::UpdateLocal(BufferView * bv)
575 par->Metrics(); // To inform lyx kernel the exact size
576 // (there were problems with arrays).
577 bv->updateInset(this, true);
581 void InsetFormula::InsetButtonRelease(BufferView * bv,
582 int x, int y, int /*button*/)
588 mathcursor->SetPos(x, y);
593 bv->updateInset(this, false);
599 void InsetFormula::InsetButtonPress(BufferView * bv,
600 int x, int y, int /*button*/)
603 sel_x = x; sel_y = y;
604 if (mathcursor && mathcursor->Selection()) {
605 mathcursor->SelClear();
606 bv->updateInset(this, false);
611 void InsetFormula::InsetMotionNotify(BufferView * bv,
612 int x, int y, int /*button*/)
614 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
617 mathcursor->SetPos(sel_x + par->xo, sel_y + par->yo);
618 mathcursor->SelStart();
620 mathcursor->GetPos(sel_x, sel_y);
621 } else if (sel_flag) {
625 mathcursor->SetPos(x, y);
627 mathcursor->GetPos(x, y);
628 if (sel_x!= x || sel_y!= y)
629 bv->updateInset(this, false);
630 sel_x = x; sel_y = y;
635 void InsetFormula::InsetKeyPress(XKeyEvent *)
637 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
641 // Special Mathed functions
642 bool InsetFormula::SetNumber(bool numbf)
645 short type = par->GetType();
646 bool oldf = is_numbered(type);
647 if (numbf && !oldf) ++type;
648 if (!numbf && oldf) --type;
656 UpdatableInset::RESULT
657 InsetFormula::LocalDispatch(BufferView * bv,
658 int action, string const & arg)
660 // extern char *dispatch_result;
661 MathedTextCodes varcode = LM_TC_MIN;
662 bool was_macro = mathcursor->InMacroMode();
664 bool space_on = false;
665 bool was_selection = mathcursor->Selection();
666 RESULT result = DISPATCHED;
667 static MathSpaceInset * sp= 0;
671 if (mathcursor->getLastCode() == LM_TC_TEX) {
676 // --- Cursor Movements ---------------------------------------------
677 case LFUN_RIGHTSEL: sel = true;
680 result = DISPATCH_RESULT(mathcursor->Right(sel));
681 if (!sel && (result == DISPATCHED))
682 result = DISPATCHED_NOUPDATE;
685 case LFUN_LEFTSEL: sel = true;
688 result = DISPATCH_RESULT(mathcursor->Left(sel));
689 if (!sel && (result == DISPATCHED))
690 result = DISPATCHED_NOUPDATE;
693 case LFUN_UPSEL: sel = true;
695 result = DISPATCH_RESULT(mathcursor->Up(sel));
696 if (!sel && (result == DISPATCHED))
697 result = DISPATCHED_NOUPDATE;
700 case LFUN_DOWNSEL: sel = true;
702 result = DISPATCH_RESULT(mathcursor->Down(sel));
703 if (!sel && (result == DISPATCHED))
704 result = DISPATCHED_NOUPDATE;
708 result = DISPATCHED_NOUPDATE;
712 result = DISPATCHED_NOUPDATE;
714 case LFUN_DELETE_LINE_FORWARD:
715 bv->lockedInsetStoreUndo(Undo::DELETE);
716 mathcursor->DelLine();
721 bv->lockedInsetStoreUndo(Undo::INSERT);
722 byte c = arg.empty() ? '1' : arg[0];
723 mathcursor->Insert(c, LM_TC_CR);
724 if (!label.empty()) {
725 mathcursor->setLabel(label);
728 par = mathcursor->GetPar();
733 bv->lockedInsetStoreUndo(Undo::INSERT);
734 mathcursor->Insert(0, LM_TC_TAB);
738 bv->lockedInsetStoreUndo(Undo::INSERT);
739 mathcursor->Insert('T', LM_TC_TAB);
743 if (!mathcursor->Left())
746 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
747 bv->updateInset(this, true);
752 bv->lockedInsetStoreUndo(Undo::DELETE);
753 mathcursor->Delete();
754 bv->updateInset(this, true);
757 // sprintf(dispatch_buffer, "%d %d",);
758 // dispatch_result = dispatch_buffer;
766 istringstream ist(arg.c_str());
769 mathcursor->SetPos(x1 + x, y1 + y);
773 /* cursor selection ---------------------------- */
777 mathcursor->MacroModeClose();
778 bv->lockedInsetStoreUndo(Undo::INSERT);
779 mathcursor->SelPaste(); UpdateLocal(bv); break;
781 bv->lockedInsetStoreUndo(Undo::DELETE);
782 mathcursor->SelCut(); UpdateLocal(bv); break;
783 case LFUN_COPY: mathcursor->SelCopy(); break;
786 case LFUN_WORDRIGHTSEL:
787 case LFUN_WORDLEFTSEL:
790 // --- accented characters ------------------------------
792 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
793 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
794 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
795 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
796 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
797 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
798 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
799 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
800 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
801 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
806 if (!greek_kb_flag) {
808 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
815 case LFUN_GREEK_TOGGLE:
817 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
819 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
821 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
826 case LFUN_BOLD: mathcursor->toggleLastCode(LM_TC_BF); break;
827 case LFUN_SANS: mathcursor->toggleLastCode(LM_TC_SF); break;
828 case LFUN_EMPH: mathcursor->toggleLastCode(LM_TC_CAL); break;
829 case LFUN_ROMAN: mathcursor->toggleLastCode(LM_TC_RM); break;
830 case LFUN_CODE: mathcursor->toggleLastCode(LM_TC_TT); break;
831 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR); break;
834 // varcode = LM_TC_TEX;
835 mathcursor->setLastCode(LM_TC_TEX);
836 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
840 case LFUN_MATH_NUMBER:
842 bv->lockedInsetStoreUndo(Undo::INSERT);
844 short type = par->GetType();
845 if (is_numbered(type)) {
847 if (!label.empty()) {
850 bv->owner()->getMiniBuffer()->Set(_("No number"));
853 bv->owner()->getMiniBuffer()->Set(_("Number"));
861 case LFUN_MATH_NONUMBER:
863 if (is_multi_numbered(par->GetType())) {
864 // MathMatrixInset *mt = (MathMatrixInset*)par;
866 // mt->SetNumbered(!mt->IsNumbered());
868 #warning This is a terrible hack! We should find a better solution.
869 while (mathcursor->getLabel() == MathedXIter::error_label) {
870 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
873 mathcursor->setNumbered();
879 case LFUN_MATH_LIMITS:
881 bv->lockedInsetStoreUndo(Undo::INSERT);
882 if (mathcursor->Limits())
888 latexkeys * l = in_word_set (arg);
889 int sz = (l) ? l->id: -1;
890 mathcursor->SetSize(sz);
895 case LFUN_INSERT_MATH:
897 bv->lockedInsetStoreUndo(Undo::INSERT);
898 InsertSymbol(bv, arg);
902 case LFUN_INSERT_MATRIX:
904 bv->lockedInsetStoreUndo(Undo::INSERT);
906 char s[80], arg2[80];
907 // This is just so that too long args won't ooze out of s.
908 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
909 k = sscanf(arg2, "%d %d %s", &m, &n, s);
918 MathMatrixInset * p = new MathMatrixInset(m, n);
919 if (mathcursor && p) {
920 if (k > 2 && int(strlen(s)) > m)
921 p->SetAlign(s[0], &s[1]);
922 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
928 case LFUN_MATH_DELIM:
930 bv->lockedInsetStoreUndo(Undo::INSERT);
931 char lf[40], rg[40], arg2[40];
932 int ilf = '(', irg = '.';
934 string vdelim("(){}[]./|");
938 ::strncpy(arg2, arg.c_str(), 40); arg2[39]= '\0';
939 int n = sscanf(arg2, "%s %s", lf, rg);
940 lf[39] = '\0'; rg[39] = '\0';
947 l = in_word_set(lf, strlen(lf));
948 // Long words will cause l == 0; so check.
951 if (vdelim.find(lf[0]) != string::npos)
959 l = in_word_set(rg, strlen(rg));
962 if (vdelim.find(rg[0]) != string::npos)
967 MathDelimInset * p = new MathDelimInset(ilf, irg);
968 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
973 case LFUN_PROTECTEDSPACE:
975 bv->lockedInsetStoreUndo(Undo::INSERT);
976 sp = new MathSpaceInset(1);
977 mathcursor->Insert(sp);
983 case LFUN_INSERT_LABEL:
985 bv->lockedInsetStoreUndo(Undo::INSERT);
986 if (par->GetType() < LM_OT_PAR)
989 string old_label = is_multiline(par->GetType())
990 ? mathcursor->getLabel() : label;
992 #warning This is a terrible hack! We should find a better solution.
993 /// This is needed becuase in some positions mathcursor->cursor->crow
994 /// is equal to 0, and therefore the label cannot be inserted.
995 /// So we move the cursor left until mathcursor->cursor->crow != 0.
996 while (old_label == MathedXIter::error_label) {
997 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
999 old_label = mathcursor->getLabel();
1002 string new_label = arg;
1003 if (new_label.empty()) {
1004 string default_label = (lyxrc.label_init_length >= 0) ? "eq:" : "";
1005 pair<bool, string> res = old_label.empty()
1006 ? askForText(_("Enter new label to insert:"), default_label)
1007 : askForText(_("Enter label:"), old_label);
1010 new_label = frontStrip(strip(res.second));
1013 if (new_label == old_label)
1014 break; // Nothing to do
1016 if (!new_label.empty())
1019 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
1022 if (is_multi_numbered(par->GetType()))
1023 mathcursor->setLabel(new_label);
1024 // MathMatrixInset *mt = (MathMatrixInset*)par;
1025 // mt->SetLabel(new_label);
1032 case LFUN_MATH_DISPLAY:
1033 bv->lockedInsetStoreUndo(Undo::EDIT);
1034 display(!disp_flag);
1038 // Invalid actions under math mode
1039 case LFUN_MATH_MODE:
1041 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1042 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1043 varcode = LM_TC_TEXTRM;
1045 varcode = LM_TC_VAR;
1047 mathcursor->setLastCode(varcode);
1051 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1054 //------- dummy actions
1055 case LFUN_EXEC_COMMAND:
1056 bv->owner()->getMiniBuffer()->PrepareForCommand();
1060 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1061 unsigned char c = arg[0];
1062 bv->lockedInsetStoreUndo(Undo::INSERT);
1064 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1066 mathcursor->setAccent(0);
1068 if (c == 0) { // Dead key, do nothing
1069 //lyxerr << "deadkey" << endl;
1073 if (mathcursor->getLastCode() == LM_TC_TEX) {
1074 mathcursor->MacroModeOpen();
1075 mathcursor->clearLastCode();
1076 varcode = LM_TC_MIN;
1079 short f = (mathcursor->getLastCode()) ?
1080 mathcursor->getLastCode() :
1081 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1082 varcode = MathIsAlphaFont(f) ?
1083 static_cast<MathedTextCodes>(f) :
1087 // lyxerr << "Varcode << vardoce;
1088 MathedTextCodes char_code = varcode;
1089 if (greek_kb_flag) {
1091 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1092 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1093 'Y', 0, 0, 0, 0 , 'Z' };
1094 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1095 char_code = LM_TC_RM;
1098 char_code = LM_TC_SYMB;
1100 mathcursor->Insert(c, char_code);
1101 if (greek_kb_flag && char_code == LM_TC_RM )
1102 mathcursor->setLastCode(LM_TC_VAR);
1103 varcode = LM_TC_MIN;
1104 if (greek_kb_flag<2) greek_kb_flag = 0;
1106 if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1107 mathcursor->Insert(c, LM_TC_TEX);
1109 mathcursor->Insert('}', LM_TC_TEX);
1112 mathcursor->clearLastCode();
1113 // varcode = LM_TC_MIN;
1115 if (c == '_' && varcode == LM_TC_TEX) {
1116 mathcursor->Insert(c, LM_TC_SPECIAL);
1117 mathcursor->clearLastCode();
1118 // varcode = LM_TC_MIN;
1120 if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1121 mathcursor->MacroModeOpen();
1122 mathcursor->clearLastCode();
1123 mathcursor->Insert(c, LM_TC_MIN);
1126 if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c))
1127 mathcursor->Insert(c, LM_TC_CONST);
1129 if (strchr("+/-*<>=", c))
1130 mathcursor->Insert(c, LM_TC_BOP);
1132 if (strchr(latex_special_chars, c) && c!= '_')
1133 mathcursor->Insert(c, LM_TC_SPECIAL);
1135 if (c == '_' || c == '^') {
1139 mathcursor->Interpret (s);
1143 short f = (mathcursor->getLastCode()) ?
1144 mathcursor->getLastCode() :
1145 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1146 varcode = MathIsAlphaFont(f) ?
1147 static_cast<MathedTextCodes>(f) :
1150 if (varcode == LM_TC_TEXTRM) {
1151 mathcursor->Insert(c, LM_TC_TEXTRM);
1154 mathcursor->MacroModeClose();
1157 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1161 if (!mathcursor->Pop() && mathcursor->IsEnd())
1165 if (c == '\'' || c == '@') {
1166 mathcursor->Insert (c, LM_TC_VAR);
1170 mathcursor->MacroModeClose();
1171 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1172 mathcursor->setLastCode(LM_TC_TEX);
1175 } else if (action == LFUN_MATH_PANEL) {
1176 result = UNDISPATCHED;
1178 // lyxerr << "Closed by action " << action << endl;
1182 if (was_macro != mathcursor->InMacroMode()
1184 && action != LFUN_BACKSPACE)
1186 if (sp && !space_on) sp = 0;
1187 if (mathcursor->Selection() || was_selection)
1188 ToggleInsetSelection(bv);
1190 if ((result == DISPATCHED) || (result == DISPATCHED_NOUPDATE) ||
1191 (result == UNDISPATCHED))
1192 ShowInsetCursor(bv);
1194 bv->unlockInset(this);
1202 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1204 MathedIter it(par->GetData());
1206 while (it.OK() && !(features.binom && features.boldsymbol)) {
1208 if (it.IsActive()) {
1209 MathParInset * p = it.GetActiveInset();
1210 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1211 p->GetName() == "binom") {
1212 features.binom = true;
1214 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1215 p->setArgumentIdx(i);
1216 mathedValidate(features, p);
1220 MathedInset* p = it.GetInset();
1221 if (!features.boldsymbol &&
1222 p->GetName() == "boldsymbol") {
1223 features.boldsymbol = true;