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
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;
62 extern char const * latex_mathenv[];
64 // this is only used by Whichfont and mathed_init_fonts (Lgb)
65 LyXFont * Math_Fonts = 0;
67 static LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
74 // quite a hack i know. Should be done with return values...
75 int number_of_newlines = 0;
78 int mathed_write(MathParInset *, std::ostream &, bool fragile,
79 string const & label = string());
81 void mathed_init_fonts();
84 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
87 MathedCursor * InsetFormula::mathcursor = 0;
90 LyXFont WhichFont(short type, int size)
127 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
138 f.setSize(lfont_size);
142 if (type == LM_TC_BSYM) {
155 case LM_ST_SCRIPTSCRIPT:
161 lyxerr << "Mathed Error: wrong font size: " << size << endl;
165 if (type != LM_TC_TEXTRM)
166 f.setColor(LColor::math);
172 void mathed_init_fonts() //removed 'static' because DEC cxx does not
174 // Probably because this func is declared as a friend in math_defs.h
177 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
180 for (int i = 0 ; i < 8 ; ++i) {
181 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
184 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
186 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
188 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
189 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
191 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
193 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
194 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
196 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
198 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
200 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
202 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
203 MathedInset::defaultAscent(lyxfont::maxAscent(f));
204 MathedInset::defaultDescent(lyxfont::maxDescent(f));
205 MathedInset::defaultWidth(lyxfont::width('I', f));
210 InsetFormula::InsetFormula(bool display)
212 par = new MathParInset; // this leaks
217 par->SetType(LM_OT_PAR);
218 par->SetStyle(LM_ST_DISPLAY);
223 InsetFormula::InsetFormula(MathParInset * p)
225 if (is_matrix_type(p->GetType()))
226 lyxerr << "InsetFormula::InsetFormula: This shouldn't happen" << endl;
228 par = is_multiline(p->GetType()) ?
229 new MathMatrixInset(static_cast<MathMatrixInset*>(p)):
233 disp_flag = (par->GetType()>0);
238 InsetFormula::~InsetFormula()
244 Inset * InsetFormula::Clone(Buffer const &) const
246 InsetFormula * f = new InsetFormula(par);
252 void InsetFormula::Write(Buffer const * buf, ostream & os) const
255 Latex(buf, os, false, false);
259 int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
261 //#warning Alejandro, the number of lines is not returned in this case
262 // This problem will disapear at 0.13.
263 return mathed_write(par, os, fragile, label);
267 int InsetFormula::Ascii(Buffer const *, ostream & os, int) const
269 par->Write(os, false);
274 int InsetFormula::Linuxdoc(Buffer const * buf, ostream & os) const
276 return Ascii(buf, os, 0);
280 int InsetFormula::DocBook(Buffer const * buf, ostream & os) const
282 return Ascii(buf, os, 0);
286 // Check if uses AMS macros
287 void InsetFormula::Validate(LaTeXFeatures & features) const
289 if (is_ams(par->GetType()))
290 features.amsstyle = true;
292 // Validation is necessary only if not using AMS math.
293 // To be safe, we will always run mathedValidate.
294 //if (!features.amsstyle)
295 mathedValidate(features, par);
299 void InsetFormula::Read(Buffer const *, LyXLex & lex)
301 istream & is = lex.getStream();
303 mathed_parser_file(is, lex.GetLineNo());
305 // Silly hack to read labels.
306 mathed_label.erase();
308 mathed_parse(0, 0, &par);
310 disp_flag = (par->GetType() > 0);
312 // Update line number
313 lex.setLineNo(mathed_parser_lineno());
315 if (!mathed_label.empty()) {
316 label = mathed_label;
317 mathed_label.erase();
320 // reading of end_inset in the inset!!!
323 if (lex.GetString() == "\\end_inset")
325 lyxerr << "InsetFormula::Read: Garbage before \\end_inset,"
326 " or missing \\end_inset!" << endl;
335 int InsetFormula::ascent(BufferView *, LyXFont const &) const
337 return par->Ascent() + (disp_flag ? 8 : 1);
341 int InsetFormula::descent(BufferView *, LyXFont const &) const
343 return par->Descent() + (disp_flag ? 8 : 1);
347 int InsetFormula::width(BufferView * bv, LyXFont const & f) const
349 MathedInset::workWidth = bv->workWidth();
350 lfont_size = f.size();
352 return par->Width(); //+2;
356 void InsetFormula::draw(BufferView * bv, LyXFont const & f,
357 int baseline, float & x, bool) const
359 MathedInset::workWidth = bv->workWidth();
360 Painter & pain = bv->painter();
361 // Seems commenting out solves a problem.
362 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
363 font.setSize(f.size());
364 lfont_size = font.size();
365 /// Let's try to wait a bit with this... (Lgb)
366 //UpdatableInset::draw(pain, font, baseline, x);
368 // otherwise a segfault could occur
369 // in some XDrawRectangles (i.e. matrix) (Matthias)
370 if (mathcursor && mathcursor->GetPar() == par) {
371 if (mathcursor->Selection()) {
375 mathcursor->SelGetArea(&xp, &yp, n);
376 pain.fillPolygon(xp, yp, n, LColor::selection);
378 mathcursor->draw(pain, int(x), baseline);
380 par->draw(pain, int(x), baseline);
382 x += float(width(bv, font));
384 if (is_numbered(par->GetType())) {
385 LyXFont wfont = WhichFont(LM_TC_BF, par->size());
386 wfont.setLatex(LyXFont::OFF);
388 if (is_singlely_numbered(par->GetType())) {
391 str = string("(") + label + ")";
394 pain.text(int(x + 20), baseline, str, wfont);
396 MathMatrixInset * mt =
397 static_cast<MathMatrixInset*>(par);
399 MathedRowSt const * crow = mt->getRowSt();
401 y = baseline + crow->getBaseline();
402 if (crow->isNumbered()) {
404 if (!crow->getLabel().empty())
405 str = string("(") + crow->getLabel() + ")";
408 pain.text(int(x + 20), y, str, wfont);
410 crow = crow->getNext();
414 cursor_visible = false;
418 string const InsetFormula::EditMessage() const
420 return _("Math editor mode");
424 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
426 mathcursor = new MathedCursor(par);
428 if (!bv->lockInset(this))
429 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
432 bv->updateInset(this, false);
435 mathcursor->SetPos(x, y);
442 void InsetFormula::InsetUnlock(BufferView * bv)
445 if (mathcursor->InMacroMode()) {
446 mathcursor->MacroModeClose();
452 bv->updateInset(this, false);
456 // Now a symbol can be inserted only if the inset is locked
457 void InsetFormula::InsertSymbol(BufferView * bv, string const & s)
459 if (s.empty() || !mathcursor)
461 mathcursor->Interpret(s);
466 void InsetFormula::GetCursorPos(BufferView *, int & x, int & y) const
468 mathcursor->GetPos(x, y);
474 void InsetFormula::ToggleInsetCursor(BufferView * bv)
481 mathcursor->GetPos(x, y);
484 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
485 int asc = lyxfont::maxAscent(font);
486 int desc = lyxfont::maxDescent(font);
489 bv->hideLockedInsetCursor();
491 bv->showLockedInsetCursor(x, y, asc, desc);
493 cursor_visible = !cursor_visible;
497 void InsetFormula::ShowInsetCursor(BufferView * bv, bool)
499 if (!cursor_visible) {
503 mathcursor->GetPos(x, y);
506 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
507 int asc = lyxfont::maxAscent(font);
508 int desc = lyxfont::maxDescent(font);
509 bv->fitLockedInsetCursor(x, y, asc, desc);
511 ToggleInsetCursor(bv);
516 void InsetFormula::HideInsetCursor(BufferView * bv)
519 ToggleInsetCursor(bv);
523 void InsetFormula::ToggleInsetSelection(BufferView * bv)
528 bv->updateInset(this, false);
532 void InsetFormula::display(bool dspf)
534 if (dspf != disp_flag) {
536 par->SetType(LM_OT_PAR);
537 par->SetStyle(LM_ST_DISPLAY);
539 if (is_multiline(par->GetType())) {
540 MathParInset * p = new MathParInset(par);
544 mathcursor->SetPar(par);
546 par->SetType(LM_OT_MIN);
547 par->SetStyle(LM_ST_TEXT);
548 if (!label.empty()) {
557 vector<string> const InsetFormula::getLabelList() const
559 //#warning This is dirty, I know. Ill clean it at 0.11
560 // Correction, the only way to clean this is with a new kernel: 0.13.
562 vector<string> label_list;
564 if (is_multi_numbered(par->GetType())) {
565 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
566 MathedRowSt const * crow = mt->getRowSt();
568 if (!crow->getLabel().empty())
569 label_list.push_back(crow->getLabel());
570 crow = crow->getNext();
572 } else if (!label.empty())
573 label_list.push_back(label);
579 void InsetFormula::UpdateLocal(BufferView * bv)
581 par->Metrics(); // To inform lyx kernel the exact size
582 // (there were problems with arrays).
583 bv->updateInset(this, true);
587 void InsetFormula::InsetButtonRelease(BufferView * bv,
588 int x, int y, int /*button*/)
594 mathcursor->SetPos(x, y);
599 bv->updateInset(this, false);
605 void InsetFormula::InsetButtonPress(BufferView * bv, int x, int y,
609 sel_x = x; sel_y = y;
610 if (mathcursor && mathcursor->Selection()) {
611 mathcursor->SelClear();
612 bv->updateInset(this, false);
617 void InsetFormula::InsetMotionNotify(BufferView * bv,
618 int x, int y, int /*button*/)
620 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
623 mathcursor->SetPos(sel_x + par->xo(), sel_y + par->yo());
624 mathcursor->SelStart();
626 mathcursor->GetPos(sel_x, sel_y);
627 } else if (sel_flag) {
631 mathcursor->SetPos(x, y);
633 mathcursor->GetPos(x, y);
634 if (sel_x != x || sel_y != y)
635 bv->updateInset(this, false);
642 void InsetFormula::InsetKeyPress(XKeyEvent *)
644 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
648 // Special Mathed functions
649 bool InsetFormula::SetNumber(bool numbf)
652 short type = par->GetType();
653 bool const oldf = is_numbered(type);
665 UpdatableInset::RESULT
666 InsetFormula::LocalDispatch(BufferView * bv, int action, string const & arg)
668 // extern char *dispatch_result;
669 MathedTextCodes varcode = LM_TC_MIN;
670 bool was_macro = mathcursor->InMacroMode();
672 bool space_on = false;
673 bool was_selection = mathcursor->Selection();
674 RESULT result = DISPATCHED;
675 static MathSpaceInset * sp= 0;
679 if (mathcursor->getLastCode() == LM_TC_TEX) {
684 // --- Cursor Movements ---------------------------------------------
686 sel = true; // fall through...
689 result = DISPATCH_RESULT(mathcursor->Right(sel));
690 if (!sel && (result == DISPATCHED))
691 result = DISPATCHED_NOUPDATE;
696 sel = true; // fall through
699 result = DISPATCH_RESULT(mathcursor->Left(sel));
700 if (!sel && (result == DISPATCHED))
701 result = DISPATCHED_NOUPDATE;
709 result = DISPATCH_RESULT(mathcursor->Up(sel));
710 if (!sel && (result == DISPATCHED))
711 result = DISPATCHED_NOUPDATE;
719 result = DISPATCH_RESULT(mathcursor->Down(sel));
720 if (!sel && (result == DISPATCHED))
721 result = DISPATCHED_NOUPDATE;
727 result = DISPATCHED_NOUPDATE;
732 result = DISPATCHED_NOUPDATE;
735 case LFUN_DELETE_LINE_FORWARD:
736 bv->lockedInsetStoreUndo(Undo::DELETE);
737 mathcursor->DelLine();
743 bv->lockedInsetStoreUndo(Undo::INSERT);
744 byte c = arg.empty() ? '1' : arg[0];
745 mathcursor->Insert(c, LM_TC_CR);
746 if (!label.empty()) {
747 mathcursor->setLabel(label);
750 par = mathcursor->GetPar();
756 bv->lockedInsetStoreUndo(Undo::INSERT);
757 mathcursor->Insert(0, LM_TC_TAB);
762 bv->lockedInsetStoreUndo(Undo::INSERT);
763 mathcursor->Insert('T', LM_TC_TAB);
768 if (!mathcursor->Left())
771 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
772 bv->updateInset(this, true);
778 bv->lockedInsetStoreUndo(Undo::DELETE);
779 mathcursor->Delete();
780 bv->updateInset(this, true);
784 // sprintf(dispatch_buffer, "%d %d",);
785 // dispatch_result = dispatch_buffer;
793 istringstream ist(arg.c_str());
796 mathcursor->SetPos(x1 + x, y1 + y);
800 /* cursor selection ---------------------------- */
804 mathcursor->MacroModeClose();
805 bv->lockedInsetStoreUndo(Undo::INSERT);
806 mathcursor->SelPaste();
811 bv->lockedInsetStoreUndo(Undo::DELETE);
812 mathcursor->SelCut();
817 mathcursor->SelCopy();
822 case LFUN_WORDRIGHTSEL:
823 case LFUN_WORDLEFTSEL:
826 // --- accented characters ------------------------------
828 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
829 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
830 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
831 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
832 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
833 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
834 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
835 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
836 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
837 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
841 if (!greek_kb_flag) {
843 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
849 case LFUN_GREEK_TOGGLE:
850 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
852 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
854 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
858 case LFUN_BOLD: mathcursor->toggleLastCode(LM_TC_BF); break;
859 case LFUN_SANS: mathcursor->toggleLastCode(LM_TC_SF); break;
860 case LFUN_EMPH: mathcursor->toggleLastCode(LM_TC_CAL); break;
861 case LFUN_ROMAN: mathcursor->toggleLastCode(LM_TC_RM); break;
862 case LFUN_CODE: mathcursor->toggleLastCode(LM_TC_TT); break;
863 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR); break;
866 // varcode = LM_TC_TEX;
867 mathcursor->setLastCode(LM_TC_TEX);
868 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
871 case LFUN_MATH_NUMBER:
872 bv->lockedInsetStoreUndo(Undo::INSERT);
874 short type = par->GetType();
875 if (is_numbered(type)) {
877 if (!label.empty()) {
880 bv->owner()->getMiniBuffer()->Set(_("No number"));
883 bv->owner()->getMiniBuffer()->Set(_("Number"));
890 case LFUN_MATH_NONUMBER:
891 if (is_multi_numbered(par->GetType())) {
892 // MathMatrixInset *mt = (MathMatrixInset*)par;
894 // mt->SetNumbered(!mt->IsNumbered());
896 #warning This is a terrible hack! We should find a better solution.
897 while (mathcursor->getLabel() == MathedXIter::error_label) {
898 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
901 mathcursor->setNumbered();
906 case LFUN_MATH_LIMITS:
907 bv->lockedInsetStoreUndo(Undo::INSERT);
908 if (mathcursor->Limits())
914 latexkeys const * l = in_word_set(arg);
915 int sz = (l) ? l->id: -1;
916 mathcursor->SetSize(sz);
920 // possible fall through?
922 case LFUN_INSERT_MATH:
923 bv->lockedInsetStoreUndo(Undo::INSERT);
924 InsertSymbol(bv, arg);
928 case LFUN_INSERT_MATRIX:
930 bv->lockedInsetStoreUndo(Undo::INSERT);
932 char s[80], arg2[80];
933 // This is just so that too long args won't ooze out of s.
934 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
935 k = sscanf(arg2, "%d %d %s", &m, &n, s);
944 MathMatrixInset * p = new MathMatrixInset(m, n);
945 if (mathcursor && p) {
946 if (k > 2 && int(strlen(s)) > m)
947 p->SetAlign(s[0], &s[1]);
948 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
954 case LFUN_MATH_DELIM:
956 bv->lockedInsetStoreUndo(Undo::INSERT);
957 char lf[40], rg[40], arg2[40];
958 int ilf = '(', irg = '.';
960 string vdelim("(){}[]./|");
964 ::strncpy(arg2, arg.c_str(), 40);
966 int n = sscanf(arg2, "%s %s", lf, rg);
975 l = in_word_set(lf, strlen(lf));
976 // Long words will cause l == 0; so check.
979 } else if (vdelim.find(lf[0]) != string::npos)
987 l = in_word_set(rg, strlen(rg));
990 } else if (vdelim.find(rg[0]) != string::npos)
995 MathDelimInset * p = new MathDelimInset(ilf, irg);
996 mathcursor->Insert(p, LM_TC_ACTIVE_INSET);
1001 case LFUN_PROTECTEDSPACE:
1002 bv->lockedInsetStoreUndo(Undo::INSERT);
1003 sp = new MathSpaceInset(1);
1004 mathcursor->Insert(sp);
1009 case LFUN_INSERT_LABEL:
1011 bv->lockedInsetStoreUndo(Undo::INSERT);
1012 if (par->GetType() < LM_OT_PAR)
1015 string old_label = is_multiline(par->GetType())
1016 ? mathcursor->getLabel() : label;
1018 #warning This is a terrible hack! We should find a better solution.
1019 // This is needed because in some positions
1020 // mathcursor->cursor->crow is equal to 0, and therefore
1021 // the label cannot be inserted.
1022 // So we move the cursor left until
1023 // mathcursor->cursor->crow != 0.
1024 while (old_label == MathedXIter::error_label) {
1025 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
1027 old_label = mathcursor->getLabel();
1030 string new_label = arg;
1031 if (new_label.empty()) {
1032 string default_label = (lyxrc.label_init_length >= 0) ? "eq:" : "";
1033 pair<bool, string> res = old_label.empty()
1034 ? askForText(_("Enter new label to insert:"), default_label)
1035 : askForText(_("Enter label:"), old_label);
1038 new_label = frontStrip(strip(res.second));
1041 if (new_label == old_label)
1042 break; // Nothing to do
1044 if (!new_label.empty())
1047 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
1050 if (is_multi_numbered(par->GetType())) {
1051 mathcursor->setLabel(new_label);
1052 // MathMatrixInset *mt = (MathMatrixInset*)par;
1053 // mt->SetLabel(new_label);
1061 case LFUN_MATH_DISPLAY:
1062 bv->lockedInsetStoreUndo(Undo::EDIT);
1063 display(!disp_flag);
1067 // Invalid actions under math mode
1068 case LFUN_MATH_MODE:
1069 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1070 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1071 varcode = LM_TC_TEXTRM;
1073 varcode = LM_TC_VAR;
1075 mathcursor->setLastCode(varcode);
1079 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1082 //------- dummy actions
1083 case LFUN_EXEC_COMMAND:
1084 bv->owner()->getMiniBuffer()->PrepareForCommand();
1088 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1089 unsigned char c = arg[0];
1090 bv->lockedInsetStoreUndo(Undo::INSERT);
1092 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1094 mathcursor->setAccent(0);
1097 if (c == 0) { // Dead key, do nothing
1098 //lyxerr << "deadkey" << endl;
1103 if (mathcursor->getLastCode() == LM_TC_TEX) {
1104 mathcursor->MacroModeOpen();
1105 mathcursor->clearLastCode();
1106 varcode = LM_TC_MIN;
1107 } else if (!varcode) {
1108 short f = (mathcursor->getLastCode()) ?
1109 mathcursor->getLastCode() :
1110 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1111 varcode = MathIsAlphaFont(f) ?
1112 static_cast<MathedTextCodes>(f) :
1116 // lyxerr << "Varcode << vardoce;
1117 MathedTextCodes char_code = varcode;
1118 if (greek_kb_flag) {
1120 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1121 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1122 'Y', 0, 0, 0, 0 , 'Z' };
1124 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1125 char_code = LM_TC_RM;
1128 char_code = LM_TC_SYMB;
1131 mathcursor->Insert(c, char_code);
1133 if (greek_kb_flag && char_code == LM_TC_RM )
1134 mathcursor->setLastCode(LM_TC_VAR);
1136 varcode = LM_TC_MIN;
1138 if (greek_kb_flag<2)
1141 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1142 mathcursor->Insert(c, LM_TC_TEX);
1144 mathcursor->Insert('}', LM_TC_TEX);
1147 mathcursor->clearLastCode();
1148 // varcode = LM_TC_MIN;
1149 } else if (c == '_' && varcode == LM_TC_TEX) {
1150 mathcursor->Insert(c, LM_TC_SPECIAL);
1151 mathcursor->clearLastCode();
1152 // varcode = LM_TC_MIN;
1153 } else if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1154 mathcursor->MacroModeOpen();
1155 mathcursor->clearLastCode();
1156 mathcursor->Insert(c, LM_TC_MIN);
1157 } else if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c)) {
1158 mathcursor->Insert(c, LM_TC_CONST);
1159 } else if (strchr("+/-*<>=", c)) {
1160 mathcursor->Insert(c, LM_TC_BOP);
1161 } else if (strchr(latex_special_chars, c) && c!= '_') {
1162 mathcursor->Insert(c, LM_TC_SPECIAL);
1163 } else if (c == '_' || c == '^') {
1167 mathcursor->Interpret(s);
1168 } else if (c == ' ') {
1170 short f = (mathcursor->getLastCode()) ?
1171 mathcursor->getLastCode() :
1172 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1173 varcode = MathIsAlphaFont(f) ?
1174 static_cast<MathedTextCodes>(f) :
1178 if (varcode == LM_TC_TEXTRM) {
1179 mathcursor->Insert(c, LM_TC_TEXTRM);
1180 } else if (was_macro) {
1181 mathcursor->MacroModeClose();
1183 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1186 } else if (!mathcursor->Pop() && mathcursor->IsEnd())
1188 } else if (c == '\'' || c == '@') {
1189 mathcursor->Insert (c, LM_TC_VAR);
1190 } else if (c == '\\') {
1192 mathcursor->MacroModeClose();
1193 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1194 mathcursor->setLastCode(LM_TC_TEX);
1197 } else if (action == LFUN_MATH_PANEL) {
1198 result = UNDISPATCHED;
1200 // lyxerr << "Closed by action " << action << endl;
1205 if (was_macro != mathcursor->InMacroMode()
1207 && action != LFUN_BACKSPACE)
1210 if (sp && !space_on)
1213 if (mathcursor->Selection() || was_selection)
1214 ToggleInsetSelection(bv);
1216 if ((result == DISPATCHED) || (result == DISPATCHED_NOUPDATE) ||
1217 (result == UNDISPATCHED))
1218 ShowInsetCursor(bv);
1220 bv->unlockInset(this);
1228 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1230 MathedIter it(par->GetData());
1232 while (it.OK() && !(features.binom && features.boldsymbol)) {
1234 if (it.IsActive()) {
1235 MathParInset * p = it.GetActiveInset();
1236 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1237 p->GetName() == "binom") {
1238 features.binom = true;
1240 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1241 p->setArgumentIdx(i);
1242 mathedValidate(features, p);
1246 MathedInset * p = it.GetInset();
1247 if (!features.boldsymbol && p->GetName() == "boldsymbol") {
1248 features.boldsymbol = true;
1258 int mathed_write(MathParInset * p, ostream & os,
1259 bool fragile, string const & label)
1261 number_of_newlines = 0;
1262 short mathed_env = p->GetType();
1264 if (mathed_env == LM_OT_MIN) {
1265 if (fragile) os << "\\protect";
1266 os << "\\( "; // changed from " \\( " (Albrecht Dress)
1268 if (mathed_env == LM_OT_PAR){
1272 << latex_mathenv[mathed_env]
1274 if (is_multicolumn(mathed_env)) {
1275 if (mathed_env != LM_OT_ALIGNAT
1276 && mathed_env != LM_OT_ALIGNATN)
1278 os << "{" << p->GetColumns()/2 << "}";
1282 ++number_of_newlines;
1285 if (!label.empty() && label[0] > ' '
1286 && is_singlely_numbered(mathed_env)) {
1290 ++number_of_newlines;
1293 p->Write(os, fragile);
1295 if (mathed_env == LM_OT_MIN){
1296 if (fragile) os << "\\protect";
1298 } else if (mathed_env == LM_OT_PAR) {
1300 ++number_of_newlines;
1303 << latex_mathenv[mathed_env]
1305 number_of_newlines += 2;
1307 return number_of_newlines;