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, int action, string const & arg)
666 // extern char *dispatch_result;
667 MathedTextCodes varcode = LM_TC_MIN;
668 bool was_macro = mathcursor->InMacroMode();
670 bool space_on = false;
671 bool was_selection = mathcursor->Selection();
672 RESULT result = DISPATCHED;
673 static MathSpaceInset * sp= 0;
677 if (mathcursor->getLastCode() == LM_TC_TEX) {
682 // --- Cursor Movements ---------------------------------------------
684 sel = true; // fall through...
687 result = DISPATCH_RESULT(mathcursor->Right(sel));
688 if (!sel && (result == DISPATCHED))
689 result = DISPATCHED_NOUPDATE;
694 sel = true; // fall through
697 result = DISPATCH_RESULT(mathcursor->Left(sel));
698 if (!sel && (result == DISPATCHED))
699 result = DISPATCHED_NOUPDATE;
707 result = DISPATCH_RESULT(mathcursor->Up(sel));
708 if (!sel && (result == DISPATCHED))
709 result = DISPATCHED_NOUPDATE;
717 result = DISPATCH_RESULT(mathcursor->Down(sel));
718 if (!sel && (result == DISPATCHED))
719 result = DISPATCHED_NOUPDATE;
725 result = DISPATCHED_NOUPDATE;
730 result = DISPATCHED_NOUPDATE;
733 case LFUN_DELETE_LINE_FORWARD:
734 bv->lockedInsetStoreUndo(Undo::DELETE);
735 mathcursor->DelLine();
741 bv->lockedInsetStoreUndo(Undo::INSERT);
742 byte c = arg.empty() ? '1' : arg[0];
743 mathcursor->Insert(c, LM_TC_CR);
744 if (!label.empty()) {
745 mathcursor->setLabel(label);
748 par = mathcursor->GetPar();
754 bv->lockedInsetStoreUndo(Undo::INSERT);
755 mathcursor->Insert(0, LM_TC_TAB);
760 bv->lockedInsetStoreUndo(Undo::INSERT);
761 mathcursor->Insert('T', LM_TC_TAB);
766 if (!mathcursor->Left())
769 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
770 bv->updateInset(this, true);
776 bv->lockedInsetStoreUndo(Undo::DELETE);
777 mathcursor->Delete();
778 bv->updateInset(this, true);
782 // sprintf(dispatch_buffer, "%d %d",);
783 // dispatch_result = dispatch_buffer;
791 istringstream ist(arg.c_str());
794 mathcursor->SetPos(x1 + x, y1 + y);
798 /* cursor selection ---------------------------- */
802 mathcursor->MacroModeClose();
803 bv->lockedInsetStoreUndo(Undo::INSERT);
804 mathcursor->SelPaste();
809 bv->lockedInsetStoreUndo(Undo::DELETE);
810 mathcursor->SelCut();
815 mathcursor->SelCopy();
820 case LFUN_WORDRIGHTSEL:
821 case LFUN_WORDLEFTSEL:
824 // --- accented characters ------------------------------
826 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
827 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
828 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
829 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
830 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
831 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
832 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
833 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
834 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
835 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
839 if (!greek_kb_flag) {
841 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
847 case LFUN_GREEK_TOGGLE:
848 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
850 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
852 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
856 case LFUN_BOLD: mathcursor->toggleLastCode(LM_TC_BF); break;
857 case LFUN_SANS: mathcursor->toggleLastCode(LM_TC_SF); break;
858 case LFUN_EMPH: mathcursor->toggleLastCode(LM_TC_CAL); break;
859 case LFUN_ROMAN: mathcursor->toggleLastCode(LM_TC_RM); break;
860 case LFUN_CODE: mathcursor->toggleLastCode(LM_TC_TT); break;
861 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR); break;
864 // varcode = LM_TC_TEX;
865 mathcursor->setLastCode(LM_TC_TEX);
866 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
869 case LFUN_MATH_NUMBER:
870 bv->lockedInsetStoreUndo(Undo::INSERT);
872 short type = par->GetType();
873 if (is_numbered(type)) {
875 if (!label.empty()) {
878 bv->owner()->getMiniBuffer()->Set(_("No number"));
881 bv->owner()->getMiniBuffer()->Set(_("Number"));
888 case LFUN_MATH_NONUMBER:
889 if (is_multi_numbered(par->GetType())) {
890 // MathMatrixInset *mt = (MathMatrixInset*)par;
892 // mt->SetNumbered(!mt->IsNumbered());
894 #warning This is a terrible hack! We should find a better solution.
895 while (mathcursor->getLabel() == MathedXIter::error_label) {
896 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
899 mathcursor->setNumbered();
904 case LFUN_MATH_LIMITS:
905 bv->lockedInsetStoreUndo(Undo::INSERT);
906 if (mathcursor->Limits())
912 latexkeys const * l = in_word_set(arg);
913 int sz = (l) ? l->id: -1;
914 mathcursor->SetSize(sz);
918 // possible fall through?
920 case LFUN_INSERT_MATH:
921 bv->lockedInsetStoreUndo(Undo::INSERT);
922 InsertSymbol(bv, arg);
926 case LFUN_INSERT_MATRIX:
928 bv->lockedInsetStoreUndo(Undo::INSERT);
930 char s[80], arg2[80];
931 // This is just so that too long args won't ooze out of s.
932 strncpy(arg2, arg.c_str(), 80); arg2[79]= '\0';
933 k = sscanf(arg2, "%d %d %s", &m, &n, s);
942 MathMatrixInset * p = new MathMatrixInset(m, n);
943 if (mathcursor && p) {
944 if (k > 2 && int(strlen(s)) > m)
945 p->SetAlign(s[0], &s[1]);
946 mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
952 case LFUN_MATH_DELIM:
954 bv->lockedInsetStoreUndo(Undo::INSERT);
955 char lf[40], rg[40], arg2[40];
956 int ilf = '(', irg = '.';
958 string vdelim("(){}[]./|");
962 ::strncpy(arg2, arg.c_str(), 40);
964 int n = sscanf(arg2, "%s %s", lf, rg);
973 l = in_word_set(lf, strlen(lf));
974 // Long words will cause l == 0; so check.
977 } else if (vdelim.find(lf[0]) != string::npos)
985 l = in_word_set(rg, strlen(rg));
988 } else if (vdelim.find(rg[0]) != string::npos)
993 MathDelimInset * p = new MathDelimInset(ilf, irg);
994 mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
999 case LFUN_PROTECTEDSPACE:
1000 bv->lockedInsetStoreUndo(Undo::INSERT);
1001 sp = new MathSpaceInset(1);
1002 mathcursor->insertInset(sp, LM_TC_INSET);
1007 case LFUN_INSERT_LABEL:
1009 bv->lockedInsetStoreUndo(Undo::INSERT);
1010 if (par->GetType() < LM_OT_PAR)
1013 string old_label = is_multiline(par->GetType())
1014 ? mathcursor->getLabel() : label;
1016 #warning This is a terrible hack! We should find a better solution.
1017 // This is needed because in some positions
1018 // mathcursor->cursor->crow is equal to 0, and therefore
1019 // the label cannot be inserted.
1020 // So we move the cursor left until
1021 // mathcursor->cursor->crow != 0.
1022 while (old_label == MathedXIter::error_label) {
1023 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
1025 old_label = mathcursor->getLabel();
1028 string new_label = arg;
1029 if (new_label.empty()) {
1030 string default_label = (lyxrc.label_init_length >= 0) ? "eq:" : "";
1031 pair<bool, string> res = old_label.empty()
1032 ? askForText(_("Enter new label to insert:"), default_label)
1033 : askForText(_("Enter label:"), old_label);
1036 new_label = frontStrip(strip(res.second));
1039 if (new_label == old_label)
1040 break; // Nothing to do
1042 if (!new_label.empty())
1045 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
1048 if (is_multi_numbered(par->GetType())) {
1049 mathcursor->setLabel(new_label);
1050 // MathMatrixInset *mt = (MathMatrixInset*)par;
1051 // mt->SetLabel(new_label);
1059 case LFUN_MATH_DISPLAY:
1060 bv->lockedInsetStoreUndo(Undo::EDIT);
1061 display(!disp_flag);
1065 // Invalid actions under math mode
1066 case LFUN_MATH_MODE:
1067 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1068 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1069 varcode = LM_TC_TEXTRM;
1071 varcode = LM_TC_VAR;
1073 mathcursor->setLastCode(varcode);
1077 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1080 //------- dummy actions
1081 case LFUN_EXEC_COMMAND:
1082 bv->owner()->getMiniBuffer()->PrepareForCommand();
1086 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1087 unsigned char c = arg[0];
1088 bv->lockedInsetStoreUndo(Undo::INSERT);
1090 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1092 mathcursor->setAccent(0);
1095 if (c == 0) { // Dead key, do nothing
1096 //lyxerr << "deadkey" << endl;
1101 if (mathcursor->getLastCode() == LM_TC_TEX) {
1102 mathcursor->MacroModeOpen();
1103 mathcursor->clearLastCode();
1104 varcode = LM_TC_MIN;
1105 } else if (!varcode) {
1106 short f = (mathcursor->getLastCode()) ?
1107 mathcursor->getLastCode() :
1108 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1109 varcode = MathIsAlphaFont(f) ?
1110 static_cast<MathedTextCodes>(f) :
1114 // lyxerr << "Varcode << vardoce;
1115 MathedTextCodes char_code = varcode;
1116 if (greek_kb_flag) {
1118 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1119 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1120 'Y', 0, 0, 0, 0 , 'Z' };
1122 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1123 char_code = LM_TC_RM;
1126 char_code = LM_TC_SYMB;
1129 mathcursor->Insert(c, char_code);
1131 if (greek_kb_flag && char_code == LM_TC_RM )
1132 mathcursor->setLastCode(LM_TC_VAR);
1134 varcode = LM_TC_MIN;
1136 if (greek_kb_flag<2)
1139 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1140 mathcursor->Insert(c, LM_TC_TEX);
1142 mathcursor->Insert('}', LM_TC_TEX);
1145 mathcursor->clearLastCode();
1146 // varcode = LM_TC_MIN;
1147 } else if (c == '_' && varcode == LM_TC_TEX) {
1148 mathcursor->Insert(c, LM_TC_SPECIAL);
1149 mathcursor->clearLastCode();
1150 // varcode = LM_TC_MIN;
1151 } else if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1152 mathcursor->MacroModeOpen();
1153 mathcursor->clearLastCode();
1154 mathcursor->Insert(c, LM_TC_MIN);
1155 } else if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c)) {
1156 mathcursor->Insert(c, LM_TC_CONST);
1157 } else if (strchr("+/-*<>=", c)) {
1158 mathcursor->Insert(c, LM_TC_BOP);
1159 } else if (strchr(latex_special_chars, c) && c!= '_') {
1160 mathcursor->Insert(c, LM_TC_SPECIAL);
1161 } else if (c == '_' || c == '^') {
1165 mathcursor->Interpret(s);
1166 } else if (c == ' ') {
1168 short f = (mathcursor->getLastCode()) ?
1169 mathcursor->getLastCode() :
1170 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1171 varcode = MathIsAlphaFont(f) ?
1172 static_cast<MathedTextCodes>(f) :
1176 if (varcode == LM_TC_TEXTRM) {
1177 mathcursor->Insert(c, LM_TC_TEXTRM);
1178 } else if (was_macro) {
1179 mathcursor->MacroModeClose();
1181 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1184 } else if (!mathcursor->Pop() && mathcursor->IsEnd())
1186 } else if (c == '\'' || c == '@') {
1187 mathcursor->Insert (c, LM_TC_VAR);
1188 } else if (c == '\\') {
1190 mathcursor->MacroModeClose();
1191 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1192 mathcursor->setLastCode(LM_TC_TEX);
1195 } else if (action == LFUN_MATH_PANEL) {
1196 result = UNDISPATCHED;
1198 // lyxerr << "Closed by action " << action << endl;
1203 if (was_macro != mathcursor->InMacroMode()
1205 && action != LFUN_BACKSPACE)
1208 if (sp && !space_on)
1211 if (mathcursor->Selection() || was_selection)
1212 ToggleInsetSelection(bv);
1214 if ((result == DISPATCHED) || (result == DISPATCHED_NOUPDATE) ||
1215 (result == UNDISPATCHED))
1216 ShowInsetCursor(bv);
1218 bv->unlockInset(this);
1226 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1228 MathedIter it(&par->GetData());
1230 while (it.OK() && !(features.binom && features.boldsymbol)) {
1232 if (it.IsActive()) {
1233 MathParInset * p = it.GetActiveInset();
1234 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1235 p->GetName() == "binom") {
1236 features.binom = true;
1238 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1239 p->setArgumentIdx(i);
1240 mathedValidate(features, p);
1244 MathedInset * p = it.GetInset();
1245 if (!features.boldsymbol && p->GetName() == "boldsymbol") {
1246 features.boldsymbol = true;
1256 int mathed_write(MathParInset * p, ostream & os,
1257 bool fragile, string const & label)
1259 number_of_newlines = 0;
1260 short mathed_env = p->GetType();
1262 if (mathed_env == LM_OT_MIN) {
1263 if (fragile) os << "\\protect";
1264 os << "\\( "; // changed from " \\( " (Albrecht Dress)
1266 if (mathed_env == LM_OT_PAR){
1270 << latex_mathenv[mathed_env]
1272 if (is_multicolumn(mathed_env)) {
1273 if (mathed_env != LM_OT_ALIGNAT
1274 && mathed_env != LM_OT_ALIGNATN)
1276 os << "{" << p->GetColumns()/2 << "}";
1280 ++number_of_newlines;
1283 if (!label.empty() && label[0] > ' '
1284 && is_singlely_numbered(mathed_env)) {
1288 ++number_of_newlines;
1291 p->Write(os, fragile);
1293 if (mathed_env == LM_OT_MIN){
1294 if (fragile) os << "\\protect";
1296 } else if (mathed_env == LM_OT_PAR) {
1298 ++number_of_newlines;
1301 << latex_mathenv[mathed_env]
1303 number_of_newlines += 2;
1305 return number_of_newlines;