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;
69 LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
76 int mathed_write(MathParInset *, std::ostream &, bool fragile,
77 string const & label = string());
79 void mathed_init_fonts();
81 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
86 MathedCursor * InsetFormula::mathcursor = 0;
89 LyXFont WhichFont(short type, int size)
126 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
137 f.setSize(lfont_size);
141 if (type == LM_TC_BSYM) {
154 case LM_ST_SCRIPTSCRIPT:
160 lyxerr << "Mathed Error: wrong font size: " << size << endl;
164 if (type != LM_TC_TEXTRM)
165 f.setColor(LColor::math);
173 void mathed_init_fonts() //removed 'static' because DEC cxx does not
175 // Probably because this func is declared as a friend in math_defs.h
178 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
181 for (int i = 0 ; i < 8 ; ++i) {
182 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
185 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
187 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
189 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
190 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
192 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
194 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
195 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
197 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
199 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
201 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
203 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
204 MathedInset::defaultAscent(lyxfont::maxAscent(f));
205 MathedInset::defaultDescent(lyxfont::maxDescent(f));
206 MathedInset::defaultWidth(lyxfont::width('I', f));
212 // quite a hack i know. Should be done with return values...
213 int number_of_newlines = 0;
216 InsetFormula::InsetFormula(bool display)
218 par = new MathParInset; // this leaks
220 disp_flag_ = display;
222 par->SetType(LM_OT_PAR);
223 par->SetStyle(LM_ST_DISPLAY);
228 InsetFormula::InsetFormula(MathParInset * p)
230 if (is_matrix_type(p->GetType()))
231 lyxerr << "InsetFormula::InsetFormula: This shouldn't happen" << endl;
233 par = is_multiline(p->GetType()) ?
234 new MathMatrixInset(*static_cast<MathMatrixInset*>(p)):
235 new MathParInset(*p);
238 disp_flag_ = (par->GetType()>0);
242 InsetFormula::~InsetFormula()
248 Inset * InsetFormula::Clone(Buffer const &) const
250 InsetFormula * f = new InsetFormula(par);
256 void InsetFormula::Write(Buffer const * buf, ostream & os) const
259 Latex(buf, os, false, false);
263 int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
265 //#warning Alejandro, the number of lines is not returned in this case
266 // This problem will disapear at 0.13.
267 return mathed_write(par, os, fragile, label_);
271 int InsetFormula::Ascii(Buffer const *, ostream & os, int) const
273 par->Write(os, false);
278 int InsetFormula::Linuxdoc(Buffer const * buf, ostream & os) const
280 return Ascii(buf, os, 0);
284 int InsetFormula::DocBook(Buffer const * buf, ostream & os) const
286 return Ascii(buf, os, 0);
290 // Check if uses AMS macros
291 void InsetFormula::Validate(LaTeXFeatures & features) const
293 if (is_ams(par->GetType()))
294 features.amsstyle = true;
296 // Validation is necessary only if not using AMS math.
297 // To be safe, we will always run mathedValidate.
298 //if (!features.amsstyle)
299 mathedValidate(features, par);
303 void InsetFormula::Read(Buffer const *buffer, LyXLex & lex)
305 istream & is = lex.getStream();
307 mathed_parser_file(is, lex.GetLineNo());
309 // Silly hack to read labels.
310 mathed_label.erase();
313 mathed_parse(ar, 0, &par);
315 disp_flag_ = (par->GetType() > 0);
317 // Update line number
318 lex.setLineNo(mathed_parser_lineno());
320 if (!mathed_label.empty()) {
321 label_ = mathed_label;
322 mathed_label.erase();
325 // reading of end_inset in the inset!!!
328 if (lex.GetString() == "\\end_inset")
330 lyxerr << "InsetFormula::Read: Garbage before \\end_inset,"
331 " or missing \\end_inset!" << endl;
334 if (lyxerr.debugging(Debug::MATHED))
335 Write(buffer, lyxerr);
339 int InsetFormula::ascent(BufferView *, LyXFont const &) const
341 return par->Ascent() + (disp_flag_ ? 8 : 1);
345 int InsetFormula::descent(BufferView *, LyXFont const &) const
347 return par->Descent() + (disp_flag_ ? 8 : 1);
351 int InsetFormula::width(BufferView * bv, LyXFont const & f) const
353 MathedInset::workWidth = bv->workWidth();
354 lfont_size = f.size();
356 return par->Width(); //+2;
360 void InsetFormula::draw(BufferView * bv, LyXFont const & f,
361 int baseline, float & x, bool) const
363 MathedInset::workWidth = bv->workWidth();
364 Painter & pain = bv->painter();
365 // Seems commenting out solves a problem.
366 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
367 font.setSize(f.size());
368 lfont_size = font.size();
369 /// Let's try to wait a bit with this... (Lgb)
370 //UpdatableInset::draw(pain, font, baseline, x);
372 // otherwise a segfault could occur
373 // in some XDrawRectangles (i.e. matrix) (Matthias)
374 if (mathcursor && mathcursor->GetPar() == par) {
375 if (mathcursor->Selection()) {
379 mathcursor->SelGetArea(&xp, &yp, n);
380 pain.fillPolygon(xp, yp, n, LColor::selection);
382 mathcursor->draw(pain, int(x), baseline);
384 par->draw(pain, int(x), baseline);
386 x += float(width(bv, font));
388 if (is_numbered(par->GetType())) {
389 LyXFont wfont = WhichFont(LM_TC_BF, par->size());
390 wfont.setLatex(LyXFont::OFF);
392 if (is_singlely_numbered(par->GetType())) {
395 str = string("(") + label_ + ")";
398 pain.text(int(x + 20), baseline, str, wfont);
400 MathMatrixInset * mt =
401 static_cast<MathMatrixInset*>(par);
402 MathedRowContainer::iterator crow = mt->getRowSt().begin();
404 int const y = baseline + crow->getBaseline();
405 if (crow->isNumbered()) {
407 if (!crow->getLabel().empty())
408 str = string("(") + crow->getLabel() + ")";
411 pain.text(int(x + 20), y, str, wfont);
417 cursor_visible = false;
421 string const InsetFormula::EditMessage() const
423 return _("Math editor mode");
427 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
429 mathcursor = new MathedCursor(par);
431 if (!bv->lockInset(this))
432 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
435 bv->updateInset(this, false);
438 mathcursor->SetPos(x, y);
445 void InsetFormula::InsetUnlock(BufferView * bv)
448 if (mathcursor->InMacroMode()) {
449 mathcursor->MacroModeClose();
455 bv->updateInset(this, false);
459 // Now a symbol can be inserted only if the inset is locked
460 void InsetFormula::InsertSymbol(BufferView * bv, string const & s)
462 if (s.empty() || !mathcursor)
464 mathcursor->Interpret(s);
469 void InsetFormula::GetCursorPos(BufferView *, int & x, int & y) const
471 mathcursor->GetPos(x, y);
477 void InsetFormula::ToggleInsetCursor(BufferView * bv)
484 mathcursor->GetPos(x, y);
487 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
488 int asc = lyxfont::maxAscent(font);
489 int desc = lyxfont::maxDescent(font);
492 bv->hideLockedInsetCursor();
494 bv->showLockedInsetCursor(x, y, asc, desc);
496 cursor_visible = !cursor_visible;
500 void InsetFormula::ShowInsetCursor(BufferView * bv, bool)
502 if (!cursor_visible) {
506 mathcursor->GetPos(x, y);
509 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
510 int asc = lyxfont::maxAscent(font);
511 int desc = lyxfont::maxDescent(font);
512 bv->fitLockedInsetCursor(x, y, asc, desc);
514 ToggleInsetCursor(bv);
519 void InsetFormula::HideInsetCursor(BufferView * bv)
522 ToggleInsetCursor(bv);
526 void InsetFormula::ToggleInsetSelection(BufferView * bv)
531 bv->updateInset(this, false);
535 void InsetFormula::display(bool dspf)
537 if (dspf != disp_flag_) {
539 par->SetType(LM_OT_PAR);
540 par->SetStyle(LM_ST_DISPLAY);
542 if (is_multiline(par->GetType())) {
544 mathcursor->SetPar(par);
546 par->SetType(LM_OT_MIN);
547 par->SetStyle(LM_ST_TEXT);
548 if (!label_.empty()) {
557 vector<string> const InsetFormula::getLabelList() const
559 //#warning This is dirty, I know. Ill clean it at 0.11
560 // Correction, the only way to clean this is with a new kernel: 0.13.
562 vector<string> label_list;
564 if (is_multi_numbered(par->GetType())) {
565 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
566 MathedRowContainer::iterator crow = mt->getRowSt().begin();
568 if (!crow->getLabel().empty())
569 label_list.push_back(crow->getLabel());
572 } else if (!label_.empty())
573 label_list.push_back(label_);
579 void InsetFormula::UpdateLocal(BufferView * bv)
581 par->Metrics(); // To inform lyx kernel the exact size
582 // (there were problems with arrays).
583 bv->updateInset(this, true);
587 void InsetFormula::InsetButtonRelease(BufferView * bv,
588 int x, int y, int /*button*/)
594 mathcursor->SetPos(x, y);
599 bv->updateInset(this, false);
605 void InsetFormula::InsetButtonPress(BufferView * bv, int x, int y,
611 if (mathcursor && mathcursor->Selection()) {
612 mathcursor->SelClear();
613 bv->updateInset(this, false);
618 void InsetFormula::InsetMotionNotify(BufferView * bv,
619 int x, int y, int /*button*/)
621 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
624 mathcursor->SetPos(sel_x + par->xo(), sel_y + par->yo());
625 mathcursor->SelStart();
627 mathcursor->GetPos(sel_x, sel_y);
628 } else if (sel_flag) {
632 mathcursor->SetPos(x, y);
634 mathcursor->GetPos(x, y);
635 if (sel_x != x || sel_y != y)
636 bv->updateInset(this, false);
643 void InsetFormula::InsetKeyPress(XKeyEvent *)
645 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
649 // Special Mathed functions
650 bool InsetFormula::SetNumber(bool numbf)
653 short type = par->GetType();
654 bool const oldf = is_numbered(type);
666 UpdatableInset::RESULT
667 InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
670 // extern char *dispatch_result;
671 MathedTextCodes varcode = LM_TC_MIN;
672 bool was_macro = mathcursor->InMacroMode();
674 bool space_on = false;
675 bool was_selection = mathcursor->Selection();
676 RESULT result = DISPATCHED;
677 static MathSpaceInset * sp= 0;
681 if (mathcursor->getLastCode() == LM_TC_TEX) {
686 // --- Cursor Movements ---------------------------------------------
688 sel = true; // fall through...
691 result = DISPATCH_RESULT(mathcursor->Right(sel));
692 if (!sel && (result == DISPATCHED))
693 result = DISPATCHED_NOUPDATE;
698 sel = true; // fall through
701 result = DISPATCH_RESULT(mathcursor->Left(sel));
702 if (!sel && (result == DISPATCHED))
703 result = DISPATCHED_NOUPDATE;
711 result = DISPATCH_RESULT(mathcursor->Up(sel));
712 if (!sel && (result == DISPATCHED))
713 result = DISPATCHED_NOUPDATE;
721 result = DISPATCH_RESULT(mathcursor->Down(sel));
722 if (!sel && (result == DISPATCHED))
723 result = DISPATCHED_NOUPDATE;
729 result = DISPATCHED_NOUPDATE;
734 result = DISPATCHED_NOUPDATE;
737 case LFUN_DELETE_LINE_FORWARD:
738 bv->lockedInsetStoreUndo(Undo::DELETE);
739 mathcursor->DelLine();
745 bv->lockedInsetStoreUndo(Undo::INSERT);
746 byte c = arg.empty() ? '1' : arg[0];
747 mathcursor->Insert(c, LM_TC_CR);
748 if (!label_.empty()) {
749 mathcursor->setLabel(label_);
752 par = mathcursor->GetPar();
758 bv->lockedInsetStoreUndo(Undo::INSERT);
759 mathcursor->Insert(0, LM_TC_TAB);
764 bv->lockedInsetStoreUndo(Undo::INSERT);
765 mathcursor->Insert('T', LM_TC_TAB);
770 if (!mathcursor->Left())
773 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
774 bv->updateInset(this, true);
780 bv->lockedInsetStoreUndo(Undo::DELETE);
781 mathcursor->Delete();
782 bv->updateInset(this, true);
786 // sprintf(dispatch_buffer, "%d %d",);
787 // dispatch_result = dispatch_buffer;
795 istringstream ist(arg.c_str());
798 mathcursor->SetPos(x1 + x, y1 + y);
802 /* cursor selection ---------------------------- */
806 mathcursor->MacroModeClose();
807 bv->lockedInsetStoreUndo(Undo::INSERT);
808 mathcursor->SelPaste();
813 bv->lockedInsetStoreUndo(Undo::DELETE);
814 mathcursor->SelCut();
819 mathcursor->SelCopy();
824 case LFUN_WORDRIGHTSEL:
825 case LFUN_WORDLEFTSEL:
828 // --- accented characters ------------------------------
830 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
831 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
832 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
833 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
834 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
835 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
836 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
837 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
838 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
839 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
843 if (!greek_kb_flag) {
845 bv->owner()->getMiniBuffer()->Set(_("Math greek mode on"));
851 case LFUN_GREEK_TOGGLE:
852 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
854 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
856 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard off"));
860 case LFUN_BOLD: mathcursor->toggleLastCode(LM_TC_BF); break;
861 case LFUN_SANS: mathcursor->toggleLastCode(LM_TC_SF); break;
862 case LFUN_EMPH: mathcursor->toggleLastCode(LM_TC_CAL); break;
863 case LFUN_ROMAN: mathcursor->toggleLastCode(LM_TC_RM); break;
864 case LFUN_CODE: mathcursor->toggleLastCode(LM_TC_TT); break;
865 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR); break;
868 // varcode = LM_TC_TEX;
869 mathcursor->setLastCode(LM_TC_TEX);
870 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
873 case LFUN_MATH_NUMBER:
874 bv->lockedInsetStoreUndo(Undo::INSERT);
876 short type = par->GetType();
877 if (is_numbered(type)) {
879 if (!label_.empty()) {
882 bv->owner()->getMiniBuffer()->Set(_("No number"));
885 bv->owner()->getMiniBuffer()->Set(_("Number"));
892 case LFUN_MATH_NONUMBER:
893 if (is_multi_numbered(par->GetType())) {
894 // MathMatrixInset *mt = (MathMatrixInset*)par;
896 // mt->SetNumbered(!mt->IsNumbered());
898 #warning This is a terrible hack! We should find a better solution.
899 while (mathcursor->getLabel() == MathedXIter::error_label) {
900 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
903 mathcursor->setNumbered();
908 case LFUN_MATH_LIMITS:
909 bv->lockedInsetStoreUndo(Undo::INSERT);
910 if (mathcursor->Limits())
916 latexkeys const * l = in_word_set(arg);
917 int const sz = (l) ? l->id: -1;
918 mathcursor->SetSize(sz);
922 // possible fall through?
924 case LFUN_INSERT_MATH:
925 bv->lockedInsetStoreUndo(Undo::INSERT);
926 InsertSymbol(bv, arg);
930 case LFUN_INSERT_MATRIX:
932 bv->lockedInsetStoreUndo(Undo::INSERT);
935 // This is just so that too long args won't ooze out of s.
936 strncpy(arg2, arg.c_str(), 80);
940 int const k = sscanf(arg2, "%d %d %s", &m, &n, s);
949 MathMatrixInset * p = new MathMatrixInset(m, n);
950 if (mathcursor && p) {
951 if (k > 2 && int(strlen(s)) > m)
952 p->SetAlign(s[0], &s[1]);
953 mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
959 case LFUN_MATH_DELIM:
961 bv->lockedInsetStoreUndo(Undo::INSERT);
968 string vdelim("(){}[]./|");
972 ::strncpy(arg2, arg.c_str(), 40);
974 int const n = sscanf(arg2, "%s %s", lf, rg);
983 l = in_word_set(lf, strlen(lf));
984 // Long words will cause l == 0; so check.
987 } else if (vdelim.find(lf[0]) != string::npos)
995 l = in_word_set(rg, strlen(rg));
998 } else if (vdelim.find(rg[0]) != string::npos)
1003 MathDelimInset * p = new MathDelimInset(ilf, irg);
1004 mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
1009 case LFUN_PROTECTEDSPACE:
1010 bv->lockedInsetStoreUndo(Undo::INSERT);
1011 sp = new MathSpaceInset(1);
1012 mathcursor->insertInset(sp, LM_TC_INSET);
1017 case LFUN_INSERT_LABEL:
1019 bv->lockedInsetStoreUndo(Undo::INSERT);
1020 if (par->GetType() < LM_OT_PAR)
1023 string old_label = is_multiline(par->GetType())
1024 ? mathcursor->getLabel() : label_;
1026 #warning This is a terrible hack! We should find a better solution.
1027 // This is needed because in some positions
1028 // mathcursor->cursor->crow is equal to 0, and therefore
1029 // the label cannot be inserted.
1030 // So we move the cursor left until
1031 // mathcursor->cursor->crow != 0.
1032 while (old_label == MathedXIter::error_label) {
1033 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
1035 old_label = mathcursor->getLabel();
1038 string new_label = arg;
1039 if (new_label.empty()) {
1040 string default_label = (lyxrc.label_init_length >= 0) ? "eq:" : "";
1041 pair<bool, string> res = old_label.empty()
1042 ? askForText(_("Enter new label to insert:"), default_label)
1043 : askForText(_("Enter label:"), old_label);
1046 new_label = frontStrip(strip(res.second));
1049 if (new_label == old_label)
1050 break; // Nothing to do
1052 if (!new_label.empty())
1055 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
1058 if (is_multi_numbered(par->GetType())) {
1059 mathcursor->setLabel(new_label);
1060 // MathMatrixInset *mt = (MathMatrixInset*)par;
1061 // mt->SetLabel(new_label);
1069 case LFUN_MATH_DISPLAY:
1070 bv->lockedInsetStoreUndo(Undo::EDIT);
1071 display(!disp_flag_);
1075 // Invalid actions under math mode
1076 case LFUN_MATH_MODE:
1077 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1078 bv->owner()->getMiniBuffer()->Set(_("math text mode"));
1079 varcode = LM_TC_TEXTRM;
1081 varcode = LM_TC_VAR;
1083 mathcursor->setLastCode(varcode);
1087 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1090 //------- dummy actions
1091 case LFUN_EXEC_COMMAND:
1092 bv->owner()->getMiniBuffer()->PrepareForCommand();
1096 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
1097 unsigned char c = arg[0];
1098 bv->lockedInsetStoreUndo(Undo::INSERT);
1100 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1102 mathcursor->setAccent(0);
1105 if (c == 0) { // Dead key, do nothing
1106 //lyxerr << "deadkey" << endl;
1111 if (mathcursor->getLastCode() == LM_TC_TEX) {
1112 mathcursor->MacroModeOpen();
1113 mathcursor->clearLastCode();
1114 varcode = LM_TC_MIN;
1115 } else if (!varcode) {
1116 short f = (mathcursor->getLastCode()) ?
1117 mathcursor->getLastCode() :
1118 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1119 varcode = MathIsAlphaFont(f) ?
1120 static_cast<MathedTextCodes>(f) :
1124 // lyxerr << "Varcode << vardoce;
1125 MathedTextCodes char_code = varcode;
1126 if (greek_kb_flag) {
1128 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1129 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1130 'Y', 0, 0, 0, 0 , 'Z' };
1132 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1133 char_code = LM_TC_RM;
1136 char_code = LM_TC_SYMB;
1139 mathcursor->Insert(c, char_code);
1141 if (greek_kb_flag && char_code == LM_TC_RM )
1142 mathcursor->setLastCode(LM_TC_VAR);
1144 varcode = LM_TC_MIN;
1146 if (greek_kb_flag < 2)
1149 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1150 mathcursor->Insert(c, LM_TC_TEX);
1152 mathcursor->Insert('}', LM_TC_TEX);
1155 mathcursor->clearLastCode();
1156 // varcode = LM_TC_MIN;
1157 } else if (c == '_' && varcode == LM_TC_TEX) {
1158 mathcursor->Insert(c, LM_TC_SPECIAL);
1159 mathcursor->clearLastCode();
1160 // varcode = LM_TC_MIN;
1161 } else if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1162 mathcursor->MacroModeOpen();
1163 mathcursor->clearLastCode();
1164 mathcursor->Insert(c, LM_TC_MIN);
1165 } else if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c)) {
1166 mathcursor->Insert(c, LM_TC_CONST);
1167 } else if (strchr("+/-*<>=", c)) {
1168 mathcursor->Insert(c, LM_TC_BOP);
1169 } else if (strchr(latex_special_chars, c) && c!= '_') {
1170 mathcursor->Insert(c, LM_TC_SPECIAL);
1171 } else if (c == '_' || c == '^') {
1175 mathcursor->Interpret(s);
1176 } else if (c == ' ') {
1178 short f = (mathcursor->getLastCode()) ?
1179 mathcursor->getLastCode() :
1180 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1181 varcode = MathIsAlphaFont(f) ?
1182 static_cast<MathedTextCodes>(f) :
1186 if (varcode == LM_TC_TEXTRM) {
1187 mathcursor->Insert(c, LM_TC_TEXTRM);
1188 } else if (was_macro) {
1189 mathcursor->MacroModeClose();
1191 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1194 } else if (!mathcursor->Pop() && mathcursor->IsEnd())
1196 } else if (c == '\'' || c == '@') {
1197 mathcursor->Insert (c, LM_TC_VAR);
1198 } else if (c == '\\') {
1200 mathcursor->MacroModeClose();
1201 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1202 mathcursor->setLastCode(LM_TC_TEX);
1205 } else if (action == LFUN_MATH_PANEL) {
1206 result = UNDISPATCHED;
1208 // lyxerr << "Closed by action " << action << endl;
1213 if (was_macro != mathcursor->InMacroMode()
1215 && action != LFUN_BACKSPACE)
1218 if (sp && !space_on)
1221 if (mathcursor->Selection() || was_selection)
1222 ToggleInsetSelection(bv);
1224 if ((result == DISPATCHED) || (result == DISPATCHED_NOUPDATE) ||
1225 (result == UNDISPATCHED))
1226 ShowInsetCursor(bv);
1228 bv->unlockInset(this);
1236 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1238 MathedIter it(&par->GetData());
1240 while (it.OK() && !(features.binom && features.boldsymbol)) {
1242 if (it.IsActive()) {
1243 MathParInset * p = it.GetActiveInset();
1244 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1245 p->GetName() == "binom") {
1246 features.binom = true;
1248 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1249 p->setArgumentIdx(i);
1250 mathedValidate(features, p);
1254 MathedInset * p = it.GetInset();
1255 if (!features.boldsymbol && p->GetName() == "boldsymbol") {
1256 features.boldsymbol = true;
1265 int mathed_write(MathParInset * p, ostream & os,
1266 bool fragile, string const & label)
1268 number_of_newlines = 0;
1269 short mathed_env = p->GetType();
1271 if (mathed_env == LM_OT_MIN) {
1272 if (fragile) os << "\\protect";
1273 os << "\\( "; // changed from " \\( " (Albrecht Dress)
1275 if (mathed_env == LM_OT_PAR){
1279 << latex_mathenv[mathed_env]
1281 if (is_multicolumn(mathed_env)) {
1282 if (mathed_env != LM_OT_ALIGNAT
1283 && mathed_env != LM_OT_ALIGNATN)
1285 os << "{" << p->GetColumns()/2 << "}";
1289 ++number_of_newlines;
1292 if (!label.empty() && label[0] > ' '
1293 && is_singlely_numbered(mathed_env)) {
1297 ++number_of_newlines;
1300 p->Write(os, fragile);
1302 if (mathed_env == LM_OT_MIN){
1303 if (fragile) os << "\\protect";
1305 } else if (mathed_env == LM_OT_PAR) {
1307 ++number_of_newlines;
1310 << latex_mathenv[mathed_env]
1312 number_of_newlines += 2;
1314 return number_of_newlines;
1320 /* FIXME: math-greek-toggle seems to work OK, but math-greek doesn't turn
1322 bool math_insert_greek(BufferView * bv, char c)
1324 if (bv->available() &&
1325 (('A' <= c && c <= 'Z') ||
1326 ('a'<= c && c<= 'z'))) {
1329 if (!bv->theLockingInset() || bv->theLockingInset()->IsTextInset()) {
1330 int greek_kb_flag_save = greek_kb_flag;
1331 InsetFormula * new_inset = new InsetFormula();
1332 bv->beforeChange(bv->text);
1333 if (!bv->insertInset(new_inset)) {
1338 new_inset->Edit(bv, 0, 0, 0);
1339 new_inset->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
1340 if (greek_kb_flag_save < 2) {
1341 bv->unlockInset(new_inset); // bv->theLockingInset());
1342 bv->text->CursorRight(bv, true);
1345 if (bv->theLockingInset()->LyxCode() == Inset::MATH_CODE ||
1346 bv->theLockingInset()->LyxCode() == Inset::MATHMACRO_CODE)
1347 static_cast<InsetFormula*>(bv->theLockingInset())->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
1349 lyxerr << "Math error: attempt to write on a wrong "
1350 "class of inset." << endl;