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();
309 mathed_parse(ar, 0, &par);
311 disp_flag = (par->GetType() > 0);
313 // Update line number
314 lex.setLineNo(mathed_parser_lineno());
316 if (!mathed_label.empty()) {
317 label = mathed_label;
318 mathed_label.erase();
321 // reading of end_inset in the inset!!!
324 if (lex.GetString() == "\\end_inset")
326 lyxerr << "InsetFormula::Read: Garbage before \\end_inset,"
327 " or missing \\end_inset!" << endl;
336 int InsetFormula::ascent(BufferView *, LyXFont const &) const
338 return par->Ascent() + (disp_flag ? 8 : 1);
342 int InsetFormula::descent(BufferView *, LyXFont const &) const
344 return par->Descent() + (disp_flag ? 8 : 1);
348 int InsetFormula::width(BufferView * bv, LyXFont const & f) const
350 MathedInset::workWidth = bv->workWidth();
351 lfont_size = f.size();
353 return par->Width(); //+2;
357 void InsetFormula::draw(BufferView * bv, LyXFont const & f,
358 int baseline, float & x, bool) const
360 MathedInset::workWidth = bv->workWidth();
361 Painter & pain = bv->painter();
362 // Seems commenting out solves a problem.
363 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
364 font.setSize(f.size());
365 lfont_size = font.size();
366 /// Let's try to wait a bit with this... (Lgb)
367 //UpdatableInset::draw(pain, font, baseline, x);
369 // otherwise a segfault could occur
370 // in some XDrawRectangles (i.e. matrix) (Matthias)
371 if (mathcursor && mathcursor->GetPar() == par) {
372 if (mathcursor->Selection()) {
376 mathcursor->SelGetArea(&xp, &yp, n);
377 pain.fillPolygon(xp, yp, n, LColor::selection);
379 mathcursor->draw(pain, int(x), baseline);
381 par->draw(pain, int(x), baseline);
383 x += float(width(bv, font));
385 if (is_numbered(par->GetType())) {
386 LyXFont wfont = WhichFont(LM_TC_BF, par->size());
387 wfont.setLatex(LyXFont::OFF);
389 if (is_singlely_numbered(par->GetType())) {
392 str = string("(") + label + ")";
395 pain.text(int(x + 20), baseline, str, wfont);
397 MathMatrixInset * mt =
398 static_cast<MathMatrixInset*>(par);
400 MathedRowSt const * crow = mt->getRowSt();
402 y = baseline + crow->getBaseline();
403 if (crow->isNumbered()) {
405 if (!crow->getLabel().empty())
406 str = string("(") + crow->getLabel() + ")";
409 pain.text(int(x + 20), y, str, wfont);
411 crow = crow->getNext();
415 cursor_visible = false;
419 string const InsetFormula::EditMessage() const
421 return _("Math editor mode");
425 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
427 mathcursor = new MathedCursor(par);
429 if (!bv->lockInset(this))
430 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
433 bv->updateInset(this, false);
436 mathcursor->SetPos(x, y);
443 void InsetFormula::InsetUnlock(BufferView * bv)
446 if (mathcursor->InMacroMode()) {
447 mathcursor->MacroModeClose();
453 bv->updateInset(this, false);
457 // Now a symbol can be inserted only if the inset is locked
458 void InsetFormula::InsertSymbol(BufferView * bv, string const & s)
460 if (s.empty() || !mathcursor)
462 mathcursor->Interpret(s);
467 void InsetFormula::GetCursorPos(BufferView *, int & x, int & y) const
469 mathcursor->GetPos(x, y);
475 void InsetFormula::ToggleInsetCursor(BufferView * bv)
482 mathcursor->GetPos(x, y);
485 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
486 int asc = lyxfont::maxAscent(font);
487 int desc = lyxfont::maxDescent(font);
490 bv->hideLockedInsetCursor();
492 bv->showLockedInsetCursor(x, y, asc, desc);
494 cursor_visible = !cursor_visible;
498 void InsetFormula::ShowInsetCursor(BufferView * bv, bool)
500 if (!cursor_visible) {
504 mathcursor->GetPos(x, y);
507 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
508 int asc = lyxfont::maxAscent(font);
509 int desc = lyxfont::maxDescent(font);
510 bv->fitLockedInsetCursor(x, y, asc, desc);
512 ToggleInsetCursor(bv);
517 void InsetFormula::HideInsetCursor(BufferView * bv)
520 ToggleInsetCursor(bv);
524 void InsetFormula::ToggleInsetSelection(BufferView * bv)
529 bv->updateInset(this, false);
533 void InsetFormula::display(bool dspf)
535 if (dspf != disp_flag) {
537 par->SetType(LM_OT_PAR);
538 par->SetStyle(LM_ST_DISPLAY);
540 if (is_multiline(par->GetType())) {
542 mathcursor->SetPar(par);
544 par->SetType(LM_OT_MIN);
545 par->SetStyle(LM_ST_TEXT);
546 if (!label.empty()) {
555 vector<string> const InsetFormula::getLabelList() const
557 //#warning This is dirty, I know. Ill clean it at 0.11
558 // Correction, the only way to clean this is with a new kernel: 0.13.
560 vector<string> label_list;
562 if (is_multi_numbered(par->GetType())) {
563 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
564 MathedRowSt const * crow = mt->getRowSt();
566 if (!crow->getLabel().empty())
567 label_list.push_back(crow->getLabel());
568 crow = crow->getNext();
570 } else if (!label.empty())
571 label_list.push_back(label);
577 void InsetFormula::UpdateLocal(BufferView * bv)
579 par->Metrics(); // To inform lyx kernel the exact size
580 // (there were problems with arrays).
581 bv->updateInset(this, true);
585 void InsetFormula::InsetButtonRelease(BufferView * bv,
586 int x, int y, int /*button*/)
592 mathcursor->SetPos(x, y);
597 bv->updateInset(this, false);
603 void InsetFormula::InsetButtonPress(BufferView * bv, int x, int y,
607 sel_x = x; sel_y = y;
608 if (mathcursor && mathcursor->Selection()) {
609 mathcursor->SelClear();
610 bv->updateInset(this, false);
615 void InsetFormula::InsetMotionNotify(BufferView * bv,
616 int x, int y, int /*button*/)
618 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
621 mathcursor->SetPos(sel_x + par->xo(), sel_y + par->yo());
622 mathcursor->SelStart();
624 mathcursor->GetPos(sel_x, sel_y);
625 } else if (sel_flag) {
629 mathcursor->SetPos(x, y);
631 mathcursor->GetPos(x, y);
632 if (sel_x != x || sel_y != y)
633 bv->updateInset(this, false);
640 void InsetFormula::InsetKeyPress(XKeyEvent *)
642 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
646 // Special Mathed functions
647 bool InsetFormula::SetNumber(bool numbf)
650 short type = par->GetType();
651 bool const oldf = is_numbered(type);
663 UpdatableInset::RESULT
664 InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
667 // extern char *dispatch_result;
668 MathedTextCodes varcode = LM_TC_MIN;
669 bool was_macro = mathcursor->InMacroMode();
671 bool space_on = false;
672 bool was_selection = mathcursor->Selection();
673 RESULT result = DISPATCHED;
674 static MathSpaceInset * sp= 0;
678 if (mathcursor->getLastCode() == LM_TC_TEX) {
683 // --- Cursor Movements ---------------------------------------------
685 sel = true; // fall through...
688 result = DISPATCH_RESULT(mathcursor->Right(sel));
689 if (!sel && (result == DISPATCHED))
690 result = DISPATCHED_NOUPDATE;
695 sel = true; // fall through
698 result = DISPATCH_RESULT(mathcursor->Left(sel));
699 if (!sel && (result == DISPATCHED))
700 result = DISPATCHED_NOUPDATE;
708 result = DISPATCH_RESULT(mathcursor->Up(sel));
709 if (!sel && (result == DISPATCHED))
710 result = DISPATCHED_NOUPDATE;
718 result = DISPATCH_RESULT(mathcursor->Down(sel));
719 if (!sel && (result == DISPATCHED))
720 result = DISPATCHED_NOUPDATE;
726 result = DISPATCHED_NOUPDATE;
731 result = DISPATCHED_NOUPDATE;
734 case LFUN_DELETE_LINE_FORWARD:
735 bv->lockedInsetStoreUndo(Undo::DELETE);
736 mathcursor->DelLine();
742 bv->lockedInsetStoreUndo(Undo::INSERT);
743 byte c = arg.empty() ? '1' : arg[0];
744 mathcursor->Insert(c, LM_TC_CR);
745 if (!label.empty()) {
746 mathcursor->setLabel(label);
749 par = mathcursor->GetPar();
755 bv->lockedInsetStoreUndo(Undo::INSERT);
756 mathcursor->Insert(0, LM_TC_TAB);
761 bv->lockedInsetStoreUndo(Undo::INSERT);
762 mathcursor->Insert('T', LM_TC_TAB);
767 if (!mathcursor->Left())
770 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
771 bv->updateInset(this, true);
777 bv->lockedInsetStoreUndo(Undo::DELETE);
778 mathcursor->Delete();
779 bv->updateInset(this, true);
783 // sprintf(dispatch_buffer, "%d %d",);
784 // dispatch_result = dispatch_buffer;
792 istringstream ist(arg.c_str());
795 mathcursor->SetPos(x1 + x, y1 + y);
799 /* cursor selection ---------------------------- */
803 mathcursor->MacroModeClose();
804 bv->lockedInsetStoreUndo(Undo::INSERT);
805 mathcursor->SelPaste();
810 bv->lockedInsetStoreUndo(Undo::DELETE);
811 mathcursor->SelCut();
816 mathcursor->SelCopy();
821 case LFUN_WORDRIGHTSEL:
822 case LFUN_WORDLEFTSEL:
825 // --- accented characters ------------------------------
827 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
828 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
829 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
830 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
831 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
832 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
833 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
834 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
835 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
836 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
840 if (!greek_kb_flag) {
842 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
848 case LFUN_GREEK_TOGGLE:
849 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
851 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
853 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
857 case LFUN_BOLD: mathcursor->toggleLastCode(LM_TC_BF); break;
858 case LFUN_SANS: mathcursor->toggleLastCode(LM_TC_SF); break;
859 case LFUN_EMPH: mathcursor->toggleLastCode(LM_TC_CAL); break;
860 case LFUN_ROMAN: mathcursor->toggleLastCode(LM_TC_RM); break;
861 case LFUN_CODE: mathcursor->toggleLastCode(LM_TC_TT); break;
862 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR); break;
865 // varcode = LM_TC_TEX;
866 mathcursor->setLastCode(LM_TC_TEX);
867 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
870 case LFUN_MATH_NUMBER:
871 bv->lockedInsetStoreUndo(Undo::INSERT);
873 short type = par->GetType();
874 if (is_numbered(type)) {
876 if (!label.empty()) {
879 bv->owner()->getMiniBuffer()->Set(_("No number"));
882 bv->owner()->getMiniBuffer()->Set(_("Number"));
889 case LFUN_MATH_NONUMBER:
890 if (is_multi_numbered(par->GetType())) {
891 // MathMatrixInset *mt = (MathMatrixInset*)par;
893 // mt->SetNumbered(!mt->IsNumbered());
895 #warning This is a terrible hack! We should find a better solution.
896 while (mathcursor->getLabel() == MathedXIter::error_label) {
897 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
900 mathcursor->setNumbered();
905 case LFUN_MATH_LIMITS:
906 bv->lockedInsetStoreUndo(Undo::INSERT);
907 if (mathcursor->Limits())
913 latexkeys const * l = in_word_set(arg);
914 int sz = (l) ? l->id: -1;
915 mathcursor->SetSize(sz);
919 // possible fall through?
921 case LFUN_INSERT_MATH:
922 bv->lockedInsetStoreUndo(Undo::INSERT);
923 InsertSymbol(bv, arg);
927 case LFUN_INSERT_MATRIX:
929 bv->lockedInsetStoreUndo(Undo::INSERT);
931 char s[80], arg2[80];
932 // This is just so that too long args won't ooze out of s.
933 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
934 k = sscanf(arg2, "%d %d %s", &m, &n, s);
943 MathMatrixInset * p = new MathMatrixInset(m, n);
944 if (mathcursor && p) {
945 if (k > 2 && int(strlen(s)) > m)
946 p->SetAlign(s[0], &s[1]);
947 mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
953 case LFUN_MATH_DELIM:
955 bv->lockedInsetStoreUndo(Undo::INSERT);
956 char lf[40], rg[40], arg2[40];
957 int ilf = '(', irg = '.';
959 string vdelim("(){}[]./|");
963 ::strncpy(arg2, arg.c_str(), 40);
965 int n = sscanf(arg2, "%s %s", lf, rg);
974 l = in_word_set(lf, strlen(lf));
975 // Long words will cause l == 0; so check.
978 } else if (vdelim.find(lf[0]) != string::npos)
986 l = in_word_set(rg, strlen(rg));
989 } else if (vdelim.find(rg[0]) != string::npos)
994 MathDelimInset * p = new MathDelimInset(ilf, irg);
995 mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
1000 case LFUN_PROTECTEDSPACE:
1001 bv->lockedInsetStoreUndo(Undo::INSERT);
1002 sp = new MathSpaceInset(1);
1003 mathcursor->insertInset(sp, LM_TC_INSET);
1008 case LFUN_INSERT_LABEL:
1010 bv->lockedInsetStoreUndo(Undo::INSERT);
1011 if (par->GetType() < LM_OT_PAR)
1014 string old_label = is_multiline(par->GetType())
1015 ? mathcursor->getLabel() : label;
1017 #warning This is a terrible hack! We should find a better solution.
1018 // This is needed because in some positions
1019 // mathcursor->cursor->crow is equal to 0, and therefore
1020 // the label cannot be inserted.
1021 // So we move the cursor left until
1022 // mathcursor->cursor->crow != 0.
1023 while (old_label == MathedXIter::error_label) {
1024 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
1026 old_label = mathcursor->getLabel();
1029 string new_label = arg;
1030 if (new_label.empty()) {
1031 string default_label = (lyxrc.label_init_length >= 0) ? "eq:" : "";
1032 pair<bool, string> res = old_label.empty()
1033 ? askForText(_("Enter new label to insert:"), default_label)
1034 : askForText(_("Enter label:"), old_label);
1037 new_label = frontStrip(strip(res.second));
1040 if (new_label == old_label)
1041 break; // Nothing to do
1043 if (!new_label.empty())
1046 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
1049 if (is_multi_numbered(par->GetType())) {
1050 mathcursor->setLabel(new_label);
1051 // MathMatrixInset *mt = (MathMatrixInset*)par;
1052 // mt->SetLabel(new_label);
1060 case LFUN_MATH_DISPLAY:
1061 bv->lockedInsetStoreUndo(Undo::EDIT);
1062 display(!disp_flag);
1066 // Invalid actions under math mode
1067 case LFUN_MATH_MODE:
1068 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1069 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1070 varcode = LM_TC_TEXTRM;
1072 varcode = LM_TC_VAR;
1074 mathcursor->setLastCode(varcode);
1078 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1081 //------- dummy actions
1082 case LFUN_EXEC_COMMAND:
1083 bv->owner()->getMiniBuffer()->PrepareForCommand();
1087 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1088 unsigned char c = arg[0];
1089 bv->lockedInsetStoreUndo(Undo::INSERT);
1091 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1093 mathcursor->setAccent(0);
1096 if (c == 0) { // Dead key, do nothing
1097 //lyxerr << "deadkey" << endl;
1102 if (mathcursor->getLastCode() == LM_TC_TEX) {
1103 mathcursor->MacroModeOpen();
1104 mathcursor->clearLastCode();
1105 varcode = LM_TC_MIN;
1106 } else if (!varcode) {
1107 short f = (mathcursor->getLastCode()) ?
1108 mathcursor->getLastCode() :
1109 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1110 varcode = MathIsAlphaFont(f) ?
1111 static_cast<MathedTextCodes>(f) :
1115 // lyxerr << "Varcode << vardoce;
1116 MathedTextCodes char_code = varcode;
1117 if (greek_kb_flag) {
1119 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1120 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1121 'Y', 0, 0, 0, 0 , 'Z' };
1123 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1124 char_code = LM_TC_RM;
1127 char_code = LM_TC_SYMB;
1130 mathcursor->Insert(c, char_code);
1132 if (greek_kb_flag && char_code == LM_TC_RM )
1133 mathcursor->setLastCode(LM_TC_VAR);
1135 varcode = LM_TC_MIN;
1137 if (greek_kb_flag<2)
1140 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1141 mathcursor->Insert(c, LM_TC_TEX);
1143 mathcursor->Insert('}', LM_TC_TEX);
1146 mathcursor->clearLastCode();
1147 // varcode = LM_TC_MIN;
1148 } else if (c == '_' && varcode == LM_TC_TEX) {
1149 mathcursor->Insert(c, LM_TC_SPECIAL);
1150 mathcursor->clearLastCode();
1151 // varcode = LM_TC_MIN;
1152 } else if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1153 mathcursor->MacroModeOpen();
1154 mathcursor->clearLastCode();
1155 mathcursor->Insert(c, LM_TC_MIN);
1156 } else if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c)) {
1157 mathcursor->Insert(c, LM_TC_CONST);
1158 } else if (strchr("+/-*<>=", c)) {
1159 mathcursor->Insert(c, LM_TC_BOP);
1160 } else if (strchr(latex_special_chars, c) && c!= '_') {
1161 mathcursor->Insert(c, LM_TC_SPECIAL);
1162 } else if (c == '_' || c == '^') {
1166 mathcursor->Interpret(s);
1167 } else if (c == ' ') {
1169 short f = (mathcursor->getLastCode()) ?
1170 mathcursor->getLastCode() :
1171 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1172 varcode = MathIsAlphaFont(f) ?
1173 static_cast<MathedTextCodes>(f) :
1177 if (varcode == LM_TC_TEXTRM) {
1178 mathcursor->Insert(c, LM_TC_TEXTRM);
1179 } else if (was_macro) {
1180 mathcursor->MacroModeClose();
1182 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1185 } else if (!mathcursor->Pop() && mathcursor->IsEnd())
1187 } else if (c == '\'' || c == '@') {
1188 mathcursor->Insert (c, LM_TC_VAR);
1189 } else if (c == '\\') {
1191 mathcursor->MacroModeClose();
1192 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1193 mathcursor->setLastCode(LM_TC_TEX);
1196 } else if (action == LFUN_MATH_PANEL) {
1197 result = UNDISPATCHED;
1199 // lyxerr << "Closed by action " << action << endl;
1204 if (was_macro != mathcursor->InMacroMode()
1206 && action != LFUN_BACKSPACE)
1209 if (sp && !space_on)
1212 if (mathcursor->Selection() || was_selection)
1213 ToggleInsetSelection(bv);
1215 if ((result == DISPATCHED) || (result == DISPATCHED_NOUPDATE) ||
1216 (result == UNDISPATCHED))
1217 ShowInsetCursor(bv);
1219 bv->unlockInset(this);
1227 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1229 MathedIter it(&par->GetData());
1231 while (it.OK() && !(features.binom && features.boldsymbol)) {
1233 if (it.IsActive()) {
1234 MathParInset * p = it.GetActiveInset();
1235 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1236 p->GetName() == "binom") {
1237 features.binom = true;
1239 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1240 p->setArgumentIdx(i);
1241 mathedValidate(features, p);
1245 MathedInset * p = it.GetInset();
1246 if (!features.boldsymbol && p->GetName() == "boldsymbol") {
1247 features.boldsymbol = true;
1257 int mathed_write(MathParInset * p, ostream & os,
1258 bool fragile, string const & label)
1260 number_of_newlines = 0;
1261 short mathed_env = p->GetType();
1263 if (mathed_env == LM_OT_MIN) {
1264 if (fragile) os << "\\protect";
1265 os << "\\( "; // changed from " \\( " (Albrecht Dress)
1267 if (mathed_env == LM_OT_PAR){
1271 << latex_mathenv[mathed_env]
1273 if (is_multicolumn(mathed_env)) {
1274 if (mathed_env != LM_OT_ALIGNAT
1275 && mathed_env != LM_OT_ALIGNATN)
1277 os << "{" << p->GetColumns()/2 << "}";
1281 ++number_of_newlines;
1284 if (!label.empty() && label[0] > ' '
1285 && is_singlely_numbered(mathed_env)) {
1289 ++number_of_newlines;
1292 p->Write(os, fragile);
1294 if (mathed_env == LM_OT_MIN){
1295 if (fragile) os << "\\protect";
1297 } else if (mathed_env == LM_OT_PAR) {
1299 ++number_of_newlines;
1302 << latex_mathenv[mathed_env]
1304 number_of_newlines += 2;
1306 return number_of_newlines;