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
214 disp_flag_ = display;
216 par->SetType(LM_OT_PAR);
217 par->SetStyle(LM_ST_DISPLAY);
222 InsetFormula::InsetFormula(MathParInset * p)
224 if (is_matrix_type(p->GetType()))
225 lyxerr << "InsetFormula::InsetFormula: This shouldn't happen" << endl;
227 par = is_multiline(p->GetType()) ?
228 new MathMatrixInset(*static_cast<MathMatrixInset*>(p)):
229 new MathParInset(*p);
232 disp_flag_ = (par->GetType()>0);
236 InsetFormula::~InsetFormula()
242 Inset * InsetFormula::Clone(Buffer const &) const
244 InsetFormula * f = new InsetFormula(par);
250 void InsetFormula::Write(Buffer const * buf, ostream & os) const
253 Latex(buf, os, false, false);
257 int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
259 //#warning Alejandro, the number of lines is not returned in this case
260 // This problem will disapear at 0.13.
261 return mathed_write(par, os, fragile, label_);
265 int InsetFormula::Ascii(Buffer const *, ostream & os, int) const
267 par->Write(os, false);
272 int InsetFormula::Linuxdoc(Buffer const * buf, ostream & os) const
274 return Ascii(buf, os, 0);
278 int InsetFormula::DocBook(Buffer const * buf, ostream & os) const
280 return Ascii(buf, os, 0);
284 // Check if uses AMS macros
285 void InsetFormula::Validate(LaTeXFeatures & features) const
287 if (is_ams(par->GetType()))
288 features.amsstyle = true;
290 // Validation is necessary only if not using AMS math.
291 // To be safe, we will always run mathedValidate.
292 //if (!features.amsstyle)
293 mathedValidate(features, par);
297 void InsetFormula::Read(Buffer const *buffer, LyXLex & lex)
299 istream & is = lex.getStream();
301 mathed_parser_file(is, lex.GetLineNo());
303 // Silly hack to read labels.
304 mathed_label.erase();
307 mathed_parse(ar, 0, &par);
309 disp_flag_ = (par->GetType() > 0);
311 // Update line number
312 lex.setLineNo(mathed_parser_lineno());
314 if (!mathed_label.empty()) {
315 label_ = mathed_label;
316 mathed_label.erase();
319 // reading of end_inset in the inset!!!
322 if (lex.GetString() == "\\end_inset")
324 lyxerr << "InsetFormula::Read: Garbage before \\end_inset,"
325 " or missing \\end_inset!" << endl;
328 if (lyxerr.debugging(Debug::MATHED))
329 Write(buffer, lyxerr);
333 int InsetFormula::ascent(BufferView *, LyXFont const &) const
335 return par->Ascent() + (disp_flag_ ? 8 : 1);
339 int InsetFormula::descent(BufferView *, LyXFont const &) const
341 return par->Descent() + (disp_flag_ ? 8 : 1);
345 int InsetFormula::width(BufferView * bv, LyXFont const & f) const
347 MathedInset::workWidth = bv->workWidth();
348 lfont_size = f.size();
350 return par->Width(); //+2;
354 void InsetFormula::draw(BufferView * bv, LyXFont const & f,
355 int baseline, float & x, bool) const
357 MathedInset::workWidth = bv->workWidth();
358 Painter & pain = bv->painter();
359 // Seems commenting out solves a problem.
360 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
361 font.setSize(f.size());
362 lfont_size = font.size();
363 /// Let's try to wait a bit with this... (Lgb)
364 //UpdatableInset::draw(pain, font, baseline, x);
366 // otherwise a segfault could occur
367 // in some XDrawRectangles (i.e. matrix) (Matthias)
368 if (mathcursor && mathcursor->GetPar() == par) {
369 if (mathcursor->Selection()) {
373 mathcursor->SelGetArea(&xp, &yp, n);
374 pain.fillPolygon(xp, yp, n, LColor::selection);
376 mathcursor->draw(pain, int(x), baseline);
378 par->draw(pain, int(x), baseline);
380 x += float(width(bv, font));
382 if (is_numbered(par->GetType())) {
383 LyXFont wfont = WhichFont(LM_TC_BF, par->size());
384 wfont.setLatex(LyXFont::OFF);
386 if (is_singlely_numbered(par->GetType())) {
389 str = string("(") + label_ + ")";
392 pain.text(int(x + 20), baseline, str, wfont);
394 MathMatrixInset * mt =
395 static_cast<MathMatrixInset*>(par);
396 MathedRowSt const * crow = mt->getRowSt();
398 int const y = baseline + crow->getBaseline();
399 if (crow->isNumbered()) {
401 if (!crow->getLabel().empty())
402 str = string("(") + crow->getLabel() + ")";
405 pain.text(int(x + 20), y, str, wfont);
407 crow = crow->getNext();
411 cursor_visible = false;
415 string const InsetFormula::EditMessage() const
417 return _("Math editor mode");
421 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
423 mathcursor = new MathedCursor(par);
425 if (!bv->lockInset(this))
426 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
429 bv->updateInset(this, false);
432 mathcursor->SetPos(x, y);
439 void InsetFormula::InsetUnlock(BufferView * bv)
442 if (mathcursor->InMacroMode()) {
443 mathcursor->MacroModeClose();
449 bv->updateInset(this, false);
453 // Now a symbol can be inserted only if the inset is locked
454 void InsetFormula::InsertSymbol(BufferView * bv, string const & s)
456 if (s.empty() || !mathcursor)
458 mathcursor->Interpret(s);
463 void InsetFormula::GetCursorPos(BufferView *, int & x, int & y) const
465 mathcursor->GetPos(x, y);
471 void InsetFormula::ToggleInsetCursor(BufferView * bv)
478 mathcursor->GetPos(x, y);
481 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
482 int asc = lyxfont::maxAscent(font);
483 int desc = lyxfont::maxDescent(font);
486 bv->hideLockedInsetCursor();
488 bv->showLockedInsetCursor(x, y, asc, desc);
490 cursor_visible = !cursor_visible;
494 void InsetFormula::ShowInsetCursor(BufferView * bv, bool)
496 if (!cursor_visible) {
500 mathcursor->GetPos(x, y);
503 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
504 int asc = lyxfont::maxAscent(font);
505 int desc = lyxfont::maxDescent(font);
506 bv->fitLockedInsetCursor(x, y, asc, desc);
508 ToggleInsetCursor(bv);
513 void InsetFormula::HideInsetCursor(BufferView * bv)
516 ToggleInsetCursor(bv);
520 void InsetFormula::ToggleInsetSelection(BufferView * bv)
525 bv->updateInset(this, false);
529 void InsetFormula::display(bool dspf)
531 if (dspf != disp_flag_) {
533 par->SetType(LM_OT_PAR);
534 par->SetStyle(LM_ST_DISPLAY);
536 if (is_multiline(par->GetType())) {
538 mathcursor->SetPar(par);
540 par->SetType(LM_OT_MIN);
541 par->SetStyle(LM_ST_TEXT);
542 if (!label_.empty()) {
551 vector<string> const InsetFormula::getLabelList() const
553 //#warning This is dirty, I know. Ill clean it at 0.11
554 // Correction, the only way to clean this is with a new kernel: 0.13.
556 vector<string> label_list;
558 if (is_multi_numbered(par->GetType())) {
559 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
560 MathedRowSt const * crow = mt->getRowSt();
562 if (!crow->getLabel().empty())
563 label_list.push_back(crow->getLabel());
564 crow = crow->getNext();
566 } else if (!label_.empty())
567 label_list.push_back(label_);
573 void InsetFormula::UpdateLocal(BufferView * bv)
575 par->Metrics(); // To inform lyx kernel the exact size
576 // (there were problems with arrays).
577 bv->updateInset(this, true);
581 void InsetFormula::InsetButtonRelease(BufferView * bv,
582 int x, int y, int /*button*/)
588 mathcursor->SetPos(x, y);
593 bv->updateInset(this, false);
599 void InsetFormula::InsetButtonPress(BufferView * bv, int x, int y,
605 if (mathcursor && mathcursor->Selection()) {
606 mathcursor->SelClear();
607 bv->updateInset(this, false);
612 void InsetFormula::InsetMotionNotify(BufferView * bv,
613 int x, int y, int /*button*/)
615 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
618 mathcursor->SetPos(sel_x + par->xo(), sel_y + par->yo());
619 mathcursor->SelStart();
621 mathcursor->GetPos(sel_x, sel_y);
622 } else if (sel_flag) {
626 mathcursor->SetPos(x, y);
628 mathcursor->GetPos(x, y);
629 if (sel_x != x || sel_y != y)
630 bv->updateInset(this, false);
637 void InsetFormula::InsetKeyPress(XKeyEvent *)
639 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
643 // Special Mathed functions
644 bool InsetFormula::SetNumber(bool numbf)
647 short type = par->GetType();
648 bool const oldf = is_numbered(type);
660 UpdatableInset::RESULT
661 InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
664 // extern char *dispatch_result;
665 MathedTextCodes varcode = LM_TC_MIN;
666 bool was_macro = mathcursor->InMacroMode();
668 bool space_on = false;
669 bool was_selection = mathcursor->Selection();
670 RESULT result = DISPATCHED;
671 static MathSpaceInset * sp= 0;
675 if (mathcursor->getLastCode() == LM_TC_TEX) {
680 // --- Cursor Movements ---------------------------------------------
682 sel = true; // fall through...
685 result = DISPATCH_RESULT(mathcursor->Right(sel));
686 if (!sel && (result == DISPATCHED))
687 result = DISPATCHED_NOUPDATE;
692 sel = true; // fall through
695 result = DISPATCH_RESULT(mathcursor->Left(sel));
696 if (!sel && (result == DISPATCHED))
697 result = DISPATCHED_NOUPDATE;
705 result = DISPATCH_RESULT(mathcursor->Up(sel));
706 if (!sel && (result == DISPATCHED))
707 result = DISPATCHED_NOUPDATE;
715 result = DISPATCH_RESULT(mathcursor->Down(sel));
716 if (!sel && (result == DISPATCHED))
717 result = DISPATCHED_NOUPDATE;
723 result = DISPATCHED_NOUPDATE;
728 result = DISPATCHED_NOUPDATE;
731 case LFUN_DELETE_LINE_FORWARD:
732 bv->lockedInsetStoreUndo(Undo::DELETE);
733 mathcursor->DelLine();
739 bv->lockedInsetStoreUndo(Undo::INSERT);
740 byte c = arg.empty() ? '1' : arg[0];
741 mathcursor->Insert(c, LM_TC_CR);
742 if (!label_.empty()) {
743 mathcursor->setLabel(label_);
746 par = mathcursor->GetPar();
752 bv->lockedInsetStoreUndo(Undo::INSERT);
753 mathcursor->Insert(0, LM_TC_TAB);
758 bv->lockedInsetStoreUndo(Undo::INSERT);
759 mathcursor->Insert('T', LM_TC_TAB);
764 if (!mathcursor->Left())
767 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
768 bv->updateInset(this, true);
774 bv->lockedInsetStoreUndo(Undo::DELETE);
775 mathcursor->Delete();
776 bv->updateInset(this, true);
780 // sprintf(dispatch_buffer, "%d %d",);
781 // dispatch_result = dispatch_buffer;
789 istringstream ist(arg.c_str());
792 mathcursor->SetPos(x1 + x, y1 + y);
796 /* cursor selection ---------------------------- */
800 mathcursor->MacroModeClose();
801 bv->lockedInsetStoreUndo(Undo::INSERT);
802 mathcursor->SelPaste();
807 bv->lockedInsetStoreUndo(Undo::DELETE);
808 mathcursor->SelCut();
813 mathcursor->SelCopy();
818 case LFUN_WORDRIGHTSEL:
819 case LFUN_WORDLEFTSEL:
822 // --- accented characters ------------------------------
824 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
825 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
826 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
827 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
828 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
829 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
830 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
831 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
832 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
833 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
837 if (!greek_kb_flag) {
839 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
845 case LFUN_GREEK_TOGGLE:
846 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
848 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
850 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
854 case LFUN_BOLD: mathcursor->toggleLastCode(LM_TC_BF); break;
855 case LFUN_SANS: mathcursor->toggleLastCode(LM_TC_SF); break;
856 case LFUN_EMPH: mathcursor->toggleLastCode(LM_TC_CAL); break;
857 case LFUN_ROMAN: mathcursor->toggleLastCode(LM_TC_RM); break;
858 case LFUN_CODE: mathcursor->toggleLastCode(LM_TC_TT); break;
859 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR); break;
862 // varcode = LM_TC_TEX;
863 mathcursor->setLastCode(LM_TC_TEX);
864 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
867 case LFUN_MATH_NUMBER:
868 bv->lockedInsetStoreUndo(Undo::INSERT);
870 short type = par->GetType();
871 if (is_numbered(type)) {
873 if (!label_.empty()) {
876 bv->owner()->getMiniBuffer()->Set(_("No number"));
879 bv->owner()->getMiniBuffer()->Set(_("Number"));
886 case LFUN_MATH_NONUMBER:
887 if (is_multi_numbered(par->GetType())) {
888 // MathMatrixInset *mt = (MathMatrixInset*)par;
890 // mt->SetNumbered(!mt->IsNumbered());
892 #warning This is a terrible hack! We should find a better solution.
893 while (mathcursor->getLabel() == MathedXIter::error_label) {
894 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
897 mathcursor->setNumbered();
902 case LFUN_MATH_LIMITS:
903 bv->lockedInsetStoreUndo(Undo::INSERT);
904 if (mathcursor->Limits())
910 latexkeys const * l = in_word_set(arg);
911 int const sz = (l) ? l->id: -1;
912 mathcursor->SetSize(sz);
916 // possible fall through?
918 case LFUN_INSERT_MATH:
919 bv->lockedInsetStoreUndo(Undo::INSERT);
920 InsertSymbol(bv, arg);
924 case LFUN_INSERT_MATRIX:
926 bv->lockedInsetStoreUndo(Undo::INSERT);
929 // This is just so that too long args won't ooze out of s.
930 strncpy(arg2, arg.c_str(), 80);
934 int const 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);
962 string vdelim("(){}[]./|");
966 ::strncpy(arg2, arg.c_str(), 40);
968 int const n = sscanf(arg2, "%s %s", lf, rg);
977 l = in_word_set(lf, strlen(lf));
978 // Long words will cause l == 0; so check.
981 } else if (vdelim.find(lf[0]) != string::npos)
989 l = in_word_set(rg, strlen(rg));
992 } else if (vdelim.find(rg[0]) != string::npos)
997 MathDelimInset * p = new MathDelimInset(ilf, irg);
998 mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
1003 case LFUN_PROTECTEDSPACE:
1004 bv->lockedInsetStoreUndo(Undo::INSERT);
1005 sp = new MathSpaceInset(1);
1006 mathcursor->insertInset(sp, LM_TC_INSET);
1011 case LFUN_INSERT_LABEL:
1013 bv->lockedInsetStoreUndo(Undo::INSERT);
1014 if (par->GetType() < LM_OT_PAR)
1017 string old_label = is_multiline(par->GetType())
1018 ? mathcursor->getLabel() : label_;
1020 #warning This is a terrible hack! We should find a better solution.
1021 // This is needed because in some positions
1022 // mathcursor->cursor->crow is equal to 0, and therefore
1023 // the label cannot be inserted.
1024 // So we move the cursor left until
1025 // mathcursor->cursor->crow != 0.
1026 while (old_label == MathedXIter::error_label) {
1027 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
1029 old_label = mathcursor->getLabel();
1032 string new_label = arg;
1033 if (new_label.empty()) {
1034 string default_label = (lyxrc.label_init_length >= 0) ? "eq:" : "";
1035 pair<bool, string> res = old_label.empty()
1036 ? askForText(_("Enter new label to insert:"), default_label)
1037 : askForText(_("Enter label:"), old_label);
1040 new_label = frontStrip(strip(res.second));
1043 if (new_label == old_label)
1044 break; // Nothing to do
1046 if (!new_label.empty())
1049 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
1052 if (is_multi_numbered(par->GetType())) {
1053 mathcursor->setLabel(new_label);
1054 // MathMatrixInset *mt = (MathMatrixInset*)par;
1055 // mt->SetLabel(new_label);
1063 case LFUN_MATH_DISPLAY:
1064 bv->lockedInsetStoreUndo(Undo::EDIT);
1065 display(!disp_flag_);
1069 // Invalid actions under math mode
1070 case LFUN_MATH_MODE:
1071 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1072 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1073 varcode = LM_TC_TEXTRM;
1075 varcode = LM_TC_VAR;
1077 mathcursor->setLastCode(varcode);
1081 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1084 //------- dummy actions
1085 case LFUN_EXEC_COMMAND:
1086 bv->owner()->getMiniBuffer()->PrepareForCommand();
1090 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1091 unsigned char c = arg[0];
1092 bv->lockedInsetStoreUndo(Undo::INSERT);
1094 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1096 mathcursor->setAccent(0);
1099 if (c == 0) { // Dead key, do nothing
1100 //lyxerr << "deadkey" << endl;
1105 if (mathcursor->getLastCode() == LM_TC_TEX) {
1106 mathcursor->MacroModeOpen();
1107 mathcursor->clearLastCode();
1108 varcode = LM_TC_MIN;
1109 } else if (!varcode) {
1110 short f = (mathcursor->getLastCode()) ?
1111 mathcursor->getLastCode() :
1112 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1113 varcode = MathIsAlphaFont(f) ?
1114 static_cast<MathedTextCodes>(f) :
1118 // lyxerr << "Varcode << vardoce;
1119 MathedTextCodes char_code = varcode;
1120 if (greek_kb_flag) {
1122 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1123 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1124 'Y', 0, 0, 0, 0 , 'Z' };
1126 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1127 char_code = LM_TC_RM;
1130 char_code = LM_TC_SYMB;
1133 mathcursor->Insert(c, char_code);
1135 if (greek_kb_flag && char_code == LM_TC_RM )
1136 mathcursor->setLastCode(LM_TC_VAR);
1138 varcode = LM_TC_MIN;
1140 if (greek_kb_flag < 2)
1143 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1144 mathcursor->Insert(c, LM_TC_TEX);
1146 mathcursor->Insert('}', LM_TC_TEX);
1149 mathcursor->clearLastCode();
1150 // varcode = LM_TC_MIN;
1151 } else if (c == '_' && varcode == LM_TC_TEX) {
1152 mathcursor->Insert(c, LM_TC_SPECIAL);
1153 mathcursor->clearLastCode();
1154 // varcode = LM_TC_MIN;
1155 } else if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1156 mathcursor->MacroModeOpen();
1157 mathcursor->clearLastCode();
1158 mathcursor->Insert(c, LM_TC_MIN);
1159 } else if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c)) {
1160 mathcursor->Insert(c, LM_TC_CONST);
1161 } else if (strchr("+/-*<>=", c)) {
1162 mathcursor->Insert(c, LM_TC_BOP);
1163 } else if (strchr(latex_special_chars, c) && c!= '_') {
1164 mathcursor->Insert(c, LM_TC_SPECIAL);
1165 } else if (c == '_' || c == '^') {
1169 mathcursor->Interpret(s);
1170 } else if (c == ' ') {
1172 short f = (mathcursor->getLastCode()) ?
1173 mathcursor->getLastCode() :
1174 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1175 varcode = MathIsAlphaFont(f) ?
1176 static_cast<MathedTextCodes>(f) :
1180 if (varcode == LM_TC_TEXTRM) {
1181 mathcursor->Insert(c, LM_TC_TEXTRM);
1182 } else if (was_macro) {
1183 mathcursor->MacroModeClose();
1185 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1188 } else if (!mathcursor->Pop() && mathcursor->IsEnd())
1190 } else if (c == '\'' || c == '@') {
1191 mathcursor->Insert (c, LM_TC_VAR);
1192 } else if (c == '\\') {
1194 mathcursor->MacroModeClose();
1195 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1196 mathcursor->setLastCode(LM_TC_TEX);
1199 } else if (action == LFUN_MATH_PANEL) {
1200 result = UNDISPATCHED;
1202 // lyxerr << "Closed by action " << action << endl;
1207 if (was_macro != mathcursor->InMacroMode()
1209 && action != LFUN_BACKSPACE)
1212 if (sp && !space_on)
1215 if (mathcursor->Selection() || was_selection)
1216 ToggleInsetSelection(bv);
1218 if ((result == DISPATCHED) || (result == DISPATCHED_NOUPDATE) ||
1219 (result == UNDISPATCHED))
1220 ShowInsetCursor(bv);
1222 bv->unlockInset(this);
1230 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1232 MathedIter it(&par->GetData());
1234 while (it.OK() && !(features.binom && features.boldsymbol)) {
1236 if (it.IsActive()) {
1237 MathParInset * p = it.GetActiveInset();
1238 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1239 p->GetName() == "binom") {
1240 features.binom = true;
1242 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1243 p->setArgumentIdx(i);
1244 mathedValidate(features, p);
1248 MathedInset * p = it.GetInset();
1249 if (!features.boldsymbol && p->GetName() == "boldsymbol") {
1250 features.boldsymbol = true;
1260 int mathed_write(MathParInset * p, ostream & os,
1261 bool fragile, string const & label)
1263 number_of_newlines = 0;
1264 short mathed_env = p->GetType();
1266 if (mathed_env == LM_OT_MIN) {
1267 if (fragile) os << "\\protect";
1268 os << "\\( "; // changed from " \\( " (Albrecht Dress)
1270 if (mathed_env == LM_OT_PAR){
1274 << latex_mathenv[mathed_env]
1276 if (is_multicolumn(mathed_env)) {
1277 if (mathed_env != LM_OT_ALIGNAT
1278 && mathed_env != LM_OT_ALIGNATN)
1280 os << "{" << p->GetColumns()/2 << "}";
1284 ++number_of_newlines;
1287 if (!label.empty() && label[0] > ' '
1288 && is_singlely_numbered(mathed_env)) {
1292 ++number_of_newlines;
1295 p->Write(os, fragile);
1297 if (mathed_env == LM_OT_MIN){
1298 if (fragile) os << "\\protect";
1300 } else if (mathed_env == LM_OT_PAR) {
1302 ++number_of_newlines;
1305 << latex_mathenv[mathed_env]
1307 number_of_newlines += 2;
1309 return number_of_newlines;