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)):
230 new MathParInset(*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())) {
541 mathcursor->SetPar(par);
543 par->SetType(LM_OT_MIN);
544 par->SetStyle(LM_ST_TEXT);
545 if (!label.empty()) {
554 vector<string> const InsetFormula::getLabelList() const
556 //#warning This is dirty, I know. Ill clean it at 0.11
557 // Correction, the only way to clean this is with a new kernel: 0.13.
559 vector<string> label_list;
561 if (is_multi_numbered(par->GetType())) {
562 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
563 MathedRowSt const * crow = mt->getRowSt();
565 if (!crow->getLabel().empty())
566 label_list.push_back(crow->getLabel());
567 crow = crow->getNext();
569 } else if (!label.empty())
570 label_list.push_back(label);
576 void InsetFormula::UpdateLocal(BufferView * bv)
578 par->Metrics(); // To inform lyx kernel the exact size
579 // (there were problems with arrays).
580 bv->updateInset(this, true);
584 void InsetFormula::InsetButtonRelease(BufferView * bv,
585 int x, int y, int /*button*/)
591 mathcursor->SetPos(x, y);
596 bv->updateInset(this, false);
602 void InsetFormula::InsetButtonPress(BufferView * bv, int x, int y,
606 sel_x = x; sel_y = y;
607 if (mathcursor && mathcursor->Selection()) {
608 mathcursor->SelClear();
609 bv->updateInset(this, false);
614 void InsetFormula::InsetMotionNotify(BufferView * bv,
615 int x, int y, int /*button*/)
617 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
620 mathcursor->SetPos(sel_x + par->xo(), sel_y + par->yo());
621 mathcursor->SelStart();
623 mathcursor->GetPos(sel_x, sel_y);
624 } else if (sel_flag) {
628 mathcursor->SetPos(x, y);
630 mathcursor->GetPos(x, y);
631 if (sel_x != x || sel_y != y)
632 bv->updateInset(this, false);
639 void InsetFormula::InsetKeyPress(XKeyEvent *)
641 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
645 // Special Mathed functions
646 bool InsetFormula::SetNumber(bool numbf)
649 short type = par->GetType();
650 bool const oldf = is_numbered(type);
662 UpdatableInset::RESULT
663 InsetFormula::LocalDispatch(BufferView * bv, int action, string const & arg)
665 // extern char *dispatch_result;
666 MathedTextCodes varcode = LM_TC_MIN;
667 bool was_macro = mathcursor->InMacroMode();
669 bool space_on = false;
670 bool was_selection = mathcursor->Selection();
671 RESULT result = DISPATCHED;
672 static MathSpaceInset * sp= 0;
676 if (mathcursor->getLastCode() == LM_TC_TEX) {
681 // --- Cursor Movements ---------------------------------------------
683 sel = true; // fall through...
686 result = DISPATCH_RESULT(mathcursor->Right(sel));
687 if (!sel && (result == DISPATCHED))
688 result = DISPATCHED_NOUPDATE;
693 sel = true; // fall through
696 result = DISPATCH_RESULT(mathcursor->Left(sel));
697 if (!sel && (result == DISPATCHED))
698 result = DISPATCHED_NOUPDATE;
706 result = DISPATCH_RESULT(mathcursor->Up(sel));
707 if (!sel && (result == DISPATCHED))
708 result = DISPATCHED_NOUPDATE;
716 result = DISPATCH_RESULT(mathcursor->Down(sel));
717 if (!sel && (result == DISPATCHED))
718 result = DISPATCHED_NOUPDATE;
724 result = DISPATCHED_NOUPDATE;
729 result = DISPATCHED_NOUPDATE;
732 case LFUN_DELETE_LINE_FORWARD:
733 bv->lockedInsetStoreUndo(Undo::DELETE);
734 mathcursor->DelLine();
740 bv->lockedInsetStoreUndo(Undo::INSERT);
741 byte c = arg.empty() ? '1' : arg[0];
742 mathcursor->Insert(c, LM_TC_CR);
743 if (!label.empty()) {
744 mathcursor->setLabel(label);
747 par = mathcursor->GetPar();
753 bv->lockedInsetStoreUndo(Undo::INSERT);
754 mathcursor->Insert(0, LM_TC_TAB);
759 bv->lockedInsetStoreUndo(Undo::INSERT);
760 mathcursor->Insert('T', LM_TC_TAB);
765 if (!mathcursor->Left())
768 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
769 bv->updateInset(this, true);
775 bv->lockedInsetStoreUndo(Undo::DELETE);
776 mathcursor->Delete();
777 bv->updateInset(this, true);
781 // sprintf(dispatch_buffer, "%d %d",);
782 // dispatch_result = dispatch_buffer;
790 istringstream ist(arg.c_str());
793 mathcursor->SetPos(x1 + x, y1 + y);
797 /* cursor selection ---------------------------- */
801 mathcursor->MacroModeClose();
802 bv->lockedInsetStoreUndo(Undo::INSERT);
803 mathcursor->SelPaste();
808 bv->lockedInsetStoreUndo(Undo::DELETE);
809 mathcursor->SelCut();
814 mathcursor->SelCopy();
819 case LFUN_WORDRIGHTSEL:
820 case LFUN_WORDLEFTSEL:
823 // --- accented characters ------------------------------
825 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
826 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
827 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
828 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
829 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
830 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
831 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
832 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
833 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
834 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
838 if (!greek_kb_flag) {
840 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
846 case LFUN_GREEK_TOGGLE:
847 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
849 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
851 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
855 case LFUN_BOLD: mathcursor->toggleLastCode(LM_TC_BF); break;
856 case LFUN_SANS: mathcursor->toggleLastCode(LM_TC_SF); break;
857 case LFUN_EMPH: mathcursor->toggleLastCode(LM_TC_CAL); break;
858 case LFUN_ROMAN: mathcursor->toggleLastCode(LM_TC_RM); break;
859 case LFUN_CODE: mathcursor->toggleLastCode(LM_TC_TT); break;
860 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR); break;
863 // varcode = LM_TC_TEX;
864 mathcursor->setLastCode(LM_TC_TEX);
865 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
868 case LFUN_MATH_NUMBER:
869 bv->lockedInsetStoreUndo(Undo::INSERT);
871 short type = par->GetType();
872 if (is_numbered(type)) {
874 if (!label.empty()) {
877 bv->owner()->getMiniBuffer()->Set(_("No number"));
880 bv->owner()->getMiniBuffer()->Set(_("Number"));
887 case LFUN_MATH_NONUMBER:
888 if (is_multi_numbered(par->GetType())) {
889 // MathMatrixInset *mt = (MathMatrixInset*)par;
891 // mt->SetNumbered(!mt->IsNumbered());
893 #warning This is a terrible hack! We should find a better solution.
894 while (mathcursor->getLabel() == MathedXIter::error_label) {
895 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
898 mathcursor->setNumbered();
903 case LFUN_MATH_LIMITS:
904 bv->lockedInsetStoreUndo(Undo::INSERT);
905 if (mathcursor->Limits())
911 latexkeys const * l = in_word_set(arg);
912 int sz = (l) ? l->id: -1;
913 mathcursor->SetSize(sz);
917 // possible fall through?
919 case LFUN_INSERT_MATH:
920 bv->lockedInsetStoreUndo(Undo::INSERT);
921 InsertSymbol(bv, arg);
925 case LFUN_INSERT_MATRIX:
927 bv->lockedInsetStoreUndo(Undo::INSERT);
929 char s[80], arg2[80];
930 // This is just so that too long args won't ooze out of s.
931 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
932 k = sscanf(arg2, "%d %d %s", &m, &n, s);
941 MathMatrixInset * p = new MathMatrixInset(m, n);
942 if (mathcursor && p) {
943 if (k > 2 && int(strlen(s)) > m)
944 p->SetAlign(s[0], &s[1]);
945 mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
951 case LFUN_MATH_DELIM:
953 bv->lockedInsetStoreUndo(Undo::INSERT);
954 char lf[40], rg[40], arg2[40];
955 int ilf = '(', irg = '.';
957 string vdelim("(){}[]./|");
961 ::strncpy(arg2, arg.c_str(), 40);
963 int n = sscanf(arg2, "%s %s", lf, rg);
972 l = in_word_set(lf, strlen(lf));
973 // Long words will cause l == 0; so check.
976 } else if (vdelim.find(lf[0]) != string::npos)
984 l = in_word_set(rg, strlen(rg));
987 } else if (vdelim.find(rg[0]) != string::npos)
992 MathDelimInset * p = new MathDelimInset(ilf, irg);
993 mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
998 case LFUN_PROTECTEDSPACE:
999 bv->lockedInsetStoreUndo(Undo::INSERT);
1000 sp = new MathSpaceInset(1);
1001 mathcursor->insertInset(sp, LM_TC_INSET);
1006 case LFUN_INSERT_LABEL:
1008 bv->lockedInsetStoreUndo(Undo::INSERT);
1009 if (par->GetType() < LM_OT_PAR)
1012 string old_label = is_multiline(par->GetType())
1013 ? mathcursor->getLabel() : label;
1015 #warning This is a terrible hack! We should find a better solution.
1016 // This is needed because in some positions
1017 // mathcursor->cursor->crow is equal to 0, and therefore
1018 // the label cannot be inserted.
1019 // So we move the cursor left until
1020 // mathcursor->cursor->crow != 0.
1021 while (old_label == MathedXIter::error_label) {
1022 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
1024 old_label = mathcursor->getLabel();
1027 string new_label = arg;
1028 if (new_label.empty()) {
1029 string default_label = (lyxrc.label_init_length >= 0) ? "eq:" : "";
1030 pair<bool, string> res = old_label.empty()
1031 ? askForText(_("Enter new label to insert:"), default_label)
1032 : askForText(_("Enter label:"), old_label);
1035 new_label = frontStrip(strip(res.second));
1038 if (new_label == old_label)
1039 break; // Nothing to do
1041 if (!new_label.empty())
1044 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
1047 if (is_multi_numbered(par->GetType())) {
1048 mathcursor->setLabel(new_label);
1049 // MathMatrixInset *mt = (MathMatrixInset*)par;
1050 // mt->SetLabel(new_label);
1058 case LFUN_MATH_DISPLAY:
1059 bv->lockedInsetStoreUndo(Undo::EDIT);
1060 display(!disp_flag);
1064 // Invalid actions under math mode
1065 case LFUN_MATH_MODE:
1066 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1067 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1068 varcode = LM_TC_TEXTRM;
1070 varcode = LM_TC_VAR;
1072 mathcursor->setLastCode(varcode);
1076 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1079 //------- dummy actions
1080 case LFUN_EXEC_COMMAND:
1081 bv->owner()->getMiniBuffer()->PrepareForCommand();
1085 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1086 unsigned char c = arg[0];
1087 bv->lockedInsetStoreUndo(Undo::INSERT);
1089 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1091 mathcursor->setAccent(0);
1094 if (c == 0) { // Dead key, do nothing
1095 //lyxerr << "deadkey" << endl;
1100 if (mathcursor->getLastCode() == LM_TC_TEX) {
1101 mathcursor->MacroModeOpen();
1102 mathcursor->clearLastCode();
1103 varcode = LM_TC_MIN;
1104 } else if (!varcode) {
1105 short f = (mathcursor->getLastCode()) ?
1106 mathcursor->getLastCode() :
1107 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1108 varcode = MathIsAlphaFont(f) ?
1109 static_cast<MathedTextCodes>(f) :
1113 // lyxerr << "Varcode << vardoce;
1114 MathedTextCodes char_code = varcode;
1115 if (greek_kb_flag) {
1117 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1118 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1119 'Y', 0, 0, 0, 0 , 'Z' };
1121 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1122 char_code = LM_TC_RM;
1125 char_code = LM_TC_SYMB;
1128 mathcursor->Insert(c, char_code);
1130 if (greek_kb_flag && char_code == LM_TC_RM )
1131 mathcursor->setLastCode(LM_TC_VAR);
1133 varcode = LM_TC_MIN;
1135 if (greek_kb_flag<2)
1138 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1139 mathcursor->Insert(c, LM_TC_TEX);
1141 mathcursor->Insert('}', LM_TC_TEX);
1144 mathcursor->clearLastCode();
1145 // varcode = LM_TC_MIN;
1146 } else if (c == '_' && varcode == LM_TC_TEX) {
1147 mathcursor->Insert(c, LM_TC_SPECIAL);
1148 mathcursor->clearLastCode();
1149 // varcode = LM_TC_MIN;
1150 } else if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1151 mathcursor->MacroModeOpen();
1152 mathcursor->clearLastCode();
1153 mathcursor->Insert(c, LM_TC_MIN);
1154 } else if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c)) {
1155 mathcursor->Insert(c, LM_TC_CONST);
1156 } else if (strchr("+/-*<>=", c)) {
1157 mathcursor->Insert(c, LM_TC_BOP);
1158 } else if (strchr(latex_special_chars, c) && c!= '_') {
1159 mathcursor->Insert(c, LM_TC_SPECIAL);
1160 } else if (c == '_' || c == '^') {
1164 mathcursor->Interpret(s);
1165 } else if (c == ' ') {
1167 short f = (mathcursor->getLastCode()) ?
1168 mathcursor->getLastCode() :
1169 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1170 varcode = MathIsAlphaFont(f) ?
1171 static_cast<MathedTextCodes>(f) :
1175 if (varcode == LM_TC_TEXTRM) {
1176 mathcursor->Insert(c, LM_TC_TEXTRM);
1177 } else if (was_macro) {
1178 mathcursor->MacroModeClose();
1180 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1183 } else if (!mathcursor->Pop() && mathcursor->IsEnd())
1185 } else if (c == '\'' || c == '@') {
1186 mathcursor->Insert (c, LM_TC_VAR);
1187 } else if (c == '\\') {
1189 mathcursor->MacroModeClose();
1190 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1191 mathcursor->setLastCode(LM_TC_TEX);
1194 } else if (action == LFUN_MATH_PANEL) {
1195 result = UNDISPATCHED;
1197 // lyxerr << "Closed by action " << action << endl;
1202 if (was_macro != mathcursor->InMacroMode()
1204 && action != LFUN_BACKSPACE)
1207 if (sp && !space_on)
1210 if (mathcursor->Selection() || was_selection)
1211 ToggleInsetSelection(bv);
1213 if ((result == DISPATCHED) || (result == DISPATCHED_NOUPDATE) ||
1214 (result == UNDISPATCHED))
1215 ShowInsetCursor(bv);
1217 bv->unlockInset(this);
1225 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1227 MathedIter it(par->GetData());
1229 while (it.OK() && !(features.binom && features.boldsymbol)) {
1231 if (it.IsActive()) {
1232 MathParInset * p = it.GetActiveInset();
1233 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1234 p->GetName() == "binom") {
1235 features.binom = true;
1237 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1238 p->setArgumentIdx(i);
1239 mathedValidate(features, p);
1243 MathedInset * p = it.GetInset();
1244 if (!features.boldsymbol && p->GetName() == "boldsymbol") {
1245 features.boldsymbol = true;
1255 int mathed_write(MathParInset * p, ostream & os,
1256 bool fragile, string const & label)
1258 number_of_newlines = 0;
1259 short mathed_env = p->GetType();
1261 if (mathed_env == LM_OT_MIN) {
1262 if (fragile) os << "\\protect";
1263 os << "\\( "; // changed from " \\( " (Albrecht Dress)
1265 if (mathed_env == LM_OT_PAR){
1269 << latex_mathenv[mathed_env]
1271 if (is_multicolumn(mathed_env)) {
1272 if (mathed_env != LM_OT_ALIGNAT
1273 && mathed_env != LM_OT_ALIGNATN)
1275 os << "{" << p->GetColumns()/2 << "}";
1279 ++number_of_newlines;
1282 if (!label.empty() && label[0] > ' '
1283 && is_singlely_numbered(mathed_env)) {
1287 ++number_of_newlines;
1290 p->Write(os, fragile);
1292 if (mathed_env == LM_OT_MIN){
1293 if (fragile) os << "\\protect";
1295 } else if (mathed_env == LM_OT_PAR) {
1297 ++number_of_newlines;
1300 << latex_mathenv[mathed_env]
1302 number_of_newlines += 2;
1304 return number_of_newlines;