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 "BufferView.h"
32 #include "LaTeXFeatures.h"
34 #include "lyx_gui_misc.h"
35 #include "support/LOstream.h"
39 #include "support/lyxlib.h"
41 #include "math_inset.h"
42 #include "math_parinset.h"
43 #include "math_matrixinset.h"
44 #include "math_rowst.h"
45 #include "math_spaceinset.h"
46 #include "math_deliminset.h"
47 #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;
68 MathedCursor * mathcursor = 0;
73 LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
80 int mathed_write(MathParInset *, std::ostream &, bool fragile,
81 string const & label = string());
83 void mathed_init_fonts();
85 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
91 LyXFont WhichFont(short type, int size)
128 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
139 f.setSize(lfont_size);
143 if (type == LM_TC_BSYM) {
156 case LM_ST_SCRIPTSCRIPT:
162 lyxerr << "Mathed Error: wrong font size: " << size << endl;
166 if (type != LM_TC_TEXTRM)
167 f.setColor(LColor::math);
175 void mathed_init_fonts() //removed 'static' because DEC cxx does not
177 // Probably because this func is declared as a friend in math_defs.h
180 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
183 for (int i = 0 ; i < 8 ; ++i) {
184 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
187 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
189 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
191 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
192 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
194 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
196 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
197 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
199 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
201 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
203 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
205 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
206 MathedInset::defaultAscent(lyxfont::maxAscent(f));
207 MathedInset::defaultDescent(lyxfont::maxDescent(f));
208 MathedInset::defaultWidth(lyxfont::width('I', f));
214 // quite a hack i know. Should be done with return values...
215 int number_of_newlines = 0;
218 InsetFormula::InsetFormula(bool display)
220 par = new MathParInset; // this leaks
222 disp_flag_ = display;
224 par->SetType(LM_OT_PAR);
225 par->SetStyle(LM_ST_DISPLAY);
230 InsetFormula::InsetFormula(MathParInset * p)
232 if (is_matrix_type(p->GetType()))
233 lyxerr << "InsetFormula::InsetFormula: This shouldn't happen" << endl;
235 par = is_multiline(p->GetType()) ?
236 new MathMatrixInset(*static_cast<MathMatrixInset*>(p)):
237 new MathParInset(*p);
240 disp_flag_ = par->GetType() > 0;
244 InsetFormula::~InsetFormula()
247 #warning leak this for a while...
253 Inset * InsetFormula::Clone(Buffer const &) const
255 return new InsetFormula(par);
259 void InsetFormula::Write(Buffer const * buf, ostream & os) const
262 Latex(buf, os, false, false);
266 int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
268 return mathed_write(par, os, fragile, par->label());
272 int InsetFormula::Ascii(Buffer const *, ostream & os, int) const
274 par->Write(os, false);
279 int InsetFormula::Linuxdoc(Buffer const * buf, ostream & os) const
281 return Ascii(buf, os, 0);
285 int InsetFormula::DocBook(Buffer const * buf, ostream & os) const
287 return Ascii(buf, os, 0);
291 // Check if uses AMS macros
292 void InsetFormula::Validate(LaTeXFeatures & features) const
294 if (is_ams(par->GetType()))
295 features.amsstyle = true;
297 // Validation is necessary only if not using AMS math.
298 // To be safe, we will always run mathedValidate.
299 //if (!features.amsstyle)
300 mathedValidate(features, par);
304 void InsetFormula::Read(Buffer const *buffer, LyXLex & lex)
306 istream & is = lex.getStream();
308 mathed_parser_file(is, lex.GetLineNo());
311 mathed_parse(ar, par, 0);
312 if (par->isMatrix()) {
313 //lyxerr << "################## InsetFormula::Read: IsMatrix\n";
314 static_cast<MathMatrixInset*>(par)->setData(ar);
318 //lyxerr << "################## InsetFormula::Read: keine Matrix\n";
321 //lyxerr << "InsetFormula::Read: par: " << par << " " << par->GetData() << endl;
324 disp_flag_ = (par->GetType() > 0);
326 // Update line number
327 lex.setLineNo(mathed_parser_lineno());
329 // reading of end_inset in the inset!!!
332 if (lex.GetString() == "\\end_inset")
334 lyxerr << "InsetFormula::Read: Garbage before \\end_inset,"
335 " or missing \\end_inset!" << endl;
338 if (lyxerr.debugging(Debug::MATHED))
339 Write(buffer, lyxerr);
343 int InsetFormula::ascent(BufferView *, LyXFont const &) const
345 return par->Ascent() + (disp_flag_ ? 8 : 1);
349 int InsetFormula::descent(BufferView *, LyXFont const &) const
351 return par->Descent() + (disp_flag_ ? 8 : 1);
355 int InsetFormula::width(BufferView * bv, LyXFont const & f) const
357 MathedInset::workWidth = bv->workWidth();
358 lfont_size = f.size();
360 return par->Width(); //+2;
364 void InsetFormula::draw(BufferView * bv, LyXFont const & f,
365 int baseline, float & x, bool) const
367 MathedInset::workWidth = bv->workWidth();
368 Painter & pain = bv->painter();
369 // Seems commenting out solves a problem.
370 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
371 font.setSize(f.size());
372 lfont_size = font.size();
373 /// Let's try to wait a bit with this... (Lgb)
374 //UpdatableInset::draw(pain, font, baseline, x);
376 // otherwise a segfault could occur
377 // in some XDrawRectangles (i.e. matrix) (Matthias)
378 if (mathcursor && mathcursor->GetPar() == par) {
379 if (mathcursor->Selection()) {
383 mathcursor->SelGetArea(&xp, &yp, n);
384 pain.fillPolygon(xp, yp, n, LColor::selection);
386 mathcursor->draw(pain, int(x), baseline);
388 par->draw(pain, int(x), baseline);
390 x += float(width(bv, font));
392 if (is_numbered(par->GetType())) {
393 LyXFont wfont = WhichFont(LM_TC_BF, par->size());
394 wfont.setLatex(LyXFont::OFF);
396 if (is_singlely_numbered(par->GetType())) {
398 if (!par->label().empty())
399 str = string("(") + par->label() + ")";
402 pain.text(int(x + 20), baseline, str, wfont);
404 MathedRowContainer::iterator crow = par->getRowSt().begin();
406 int const y = baseline + crow->getBaseline();
407 if (crow->isNumbered()) {
409 if (!crow->getLabel().empty())
410 str = string("(") + crow->getLabel() + ")";
413 pain.text(int(x + 20), y, str, wfont);
419 setCursorVisible(false);
423 string const InsetFormula::EditMessage() const
425 return _("Math editor mode");
429 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
431 mathcursor = new MathedCursor(par);
433 if (!bv->lockInset(this))
434 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
437 bv->updateInset(this, false);
440 mathcursor->SetPos(x, y);
447 void InsetFormula::InsetUnlock(BufferView * bv)
450 if (mathcursor->InMacroMode()) {
451 mathcursor->MacroModeClose();
457 bv->updateInset(this, false);
461 // Now a symbol can be inserted only if the inset is locked
462 void InsetFormula::InsertSymbol(BufferView *, string const & s)
464 if (s.empty() || !mathcursor)
466 mathcursor->Interpret(s);
467 // Andre: UpdateLocal(bv);
471 void InsetFormula::GetCursorPos(BufferView *, int & x, int & y) const
473 mathcursor->GetPos(x, y);
479 void InsetFormula::ToggleInsetCursor(BufferView * bv)
486 mathcursor->GetPos(x, y);
489 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
490 int const asc = lyxfont::maxAscent(font);
491 int const desc = lyxfont::maxDescent(font);
493 if (isCursorVisible())
494 bv->hideLockedInsetCursor();
496 bv->showLockedInsetCursor(x, y, asc, desc);
498 toggleCursorVisible();
502 void InsetFormula::ShowInsetCursor(BufferView * bv, bool)
504 if (!isCursorVisible()) {
508 mathcursor->GetPos(x, y);
511 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
512 int const asc = lyxfont::maxAscent(font);
513 int const desc = lyxfont::maxDescent(font);
514 bv->fitLockedInsetCursor(x, y, asc, desc);
516 ToggleInsetCursor(bv);
521 void InsetFormula::HideInsetCursor(BufferView * bv)
523 if (isCursorVisible())
524 ToggleInsetCursor(bv);
528 void InsetFormula::ToggleInsetSelection(BufferView * bv)
533 bv->updateInset(this, false);
537 void InsetFormula::display(bool dspf)
539 if (dspf != disp_flag_) {
541 par->SetType(LM_OT_PAR);
542 par->SetStyle(LM_ST_DISPLAY);
544 if (is_multiline(par->GetType())) {
546 mathcursor->SetPar(par);
548 par->SetType(LM_OT_MIN);
549 par->SetStyle(LM_ST_TEXT);
554 if (!label_.empty()) {
564 vector<string> const InsetFormula::getLabelList() const
566 vector<string> label_list;
567 MathedRowContainer::iterator crow = par->getRowSt().begin();
568 for ( ; crow; ++crow)
569 label_list.push_back(crow->getLabel());
575 void InsetFormula::UpdateLocal(BufferView * bv)
577 par->Metrics(); // To inform lyx kernel the exact size
578 // (there were problems with arrays).
579 bv->updateInset(this, true);
583 void InsetFormula::InsetButtonRelease(BufferView * bv,
584 int x, int y, int /*button*/)
590 mathcursor->SetPos(x, y);
595 bv->updateInset(this, false);
601 void InsetFormula::InsetButtonPress(BufferView * bv, int x, int y,
607 if (mathcursor && mathcursor->Selection()) {
608 mathcursor->SelClear();
609 bv->updateInset(this, false);
614 void InsetFormula::InsetMotionNotify(BufferView * bv,
615 int x, int y, int /*button*/)
617 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
620 mathcursor->SetPos(sel_x + par->xo(), sel_y + par->yo());
621 mathcursor->SelStart();
623 mathcursor->GetPos(sel_x, sel_y);
624 } else if (sel_flag) {
628 mathcursor->SetPos(x, y);
630 mathcursor->GetPos(x, y);
631 if (sel_x != x || sel_y != y)
632 bv->updateInset(this, false);
639 void InsetFormula::InsetKeyPress(XKeyEvent *)
641 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
645 // Special Mathed functions
646 bool InsetFormula::SetNumber(bool numbf)
649 short type = par->GetType();
650 bool const oldf = is_numbered(type);
662 UpdatableInset::RESULT
663 InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
666 //lyxerr << "InsetFormula::LocalDispatch: act: " << action
667 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
668 // extern char *dispatch_result;
674 MathedTextCodes varcode = LM_TC_MIN;
675 bool was_macro = mathcursor->InMacroMode();
677 bool space_on = false;
678 bool was_selection = mathcursor->Selection();
679 RESULT result = DISPATCHED;
680 static MathSpaceInset * sp= 0;
684 if (mathcursor->getLastCode() == LM_TC_TEX) {
689 // --- Cursor Movements ---------------------------------------------
691 sel = true; // fall through...
694 result = DISPATCH_RESULT(mathcursor->Right(sel));
695 if (!sel && (result == DISPATCHED))
696 result = DISPATCHED_NOUPDATE;
702 sel = true; // fall through
705 result = DISPATCH_RESULT(mathcursor->Left(sel));
706 if (!sel && (result == DISPATCHED))
707 result = DISPATCHED_NOUPDATE;
716 result = DISPATCH_RESULT(mathcursor->Up(sel));
717 if (!sel && (result == DISPATCHED))
718 result = DISPATCHED_NOUPDATE;
727 result = DISPATCH_RESULT(mathcursor->Down(sel));
728 if (!sel && (result == DISPATCHED))
729 result = DISPATCHED_NOUPDATE;
736 result = DISPATCHED_NOUPDATE;
741 result = DISPATCHED_NOUPDATE;
744 case LFUN_DELETE_LINE_FORWARD:
745 bv->lockedInsetStoreUndo(Undo::DELETE);
746 mathcursor->DelLine();
753 bv->lockedInsetStoreUndo(Undo::INSERT);
754 byte c = arg.empty() ? '1' : arg[0];
755 mathcursor->Insert(c, LM_TC_CR);
760 if (!par->label().empty()) {
761 mathcursor->setLabel(label_);
765 par = mathcursor->GetPar();
772 bv->lockedInsetStoreUndo(Undo::INSERT);
773 mathcursor->Insert(0, LM_TC_TAB);
778 bv->lockedInsetStoreUndo(Undo::INSERT);
779 mathcursor->Insert('T', LM_TC_TAB);
785 if (!mathcursor->Left())
788 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
789 bv->updateInset(this, true);
795 bv->lockedInsetStoreUndo(Undo::DELETE);
796 mathcursor->Delete();
797 bv->updateInset(this, true);
801 // sprintf(dispatch_buffer, "%d %d",);
802 // dispatch_result = dispatch_buffer;
810 istringstream ist(arg.c_str());
813 mathcursor->SetPos(x1 + x, y1 + y);
817 /* cursor selection ---------------------------- */
821 mathcursor->MacroModeClose();
822 bv->lockedInsetStoreUndo(Undo::INSERT);
823 mathcursor->SelPaste();
829 bv->lockedInsetStoreUndo(Undo::DELETE);
830 mathcursor->SelCut();
836 mathcursor->SelCopy();
841 case LFUN_WORDRIGHTSEL:
842 case LFUN_WORDLEFTSEL:
845 // --- accented characters ------------------------------
847 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
848 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
849 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
850 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
851 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
852 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
853 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
854 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
855 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
856 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
860 if (!greek_kb_flag) {
862 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("Math greek mode on"));
868 case LFUN_GREEK_TOGGLE:
869 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
871 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("Math greek keyboard on"));
873 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("Math greek keyboard off"));
877 case LFUN_BOLD: mathcursor->toggleLastCode(LM_TC_BF); break;
878 case LFUN_SANS: mathcursor->toggleLastCode(LM_TC_SF); break;
879 case LFUN_EMPH: mathcursor->toggleLastCode(LM_TC_CAL); break;
880 case LFUN_ROMAN: mathcursor->toggleLastCode(LM_TC_RM); break;
881 case LFUN_CODE: mathcursor->toggleLastCode(LM_TC_TT); break;
882 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR); break;
885 // varcode = LM_TC_TEX;
886 mathcursor->setLastCode(LM_TC_TEX);
887 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("TeX mode"));
890 case LFUN_MATH_NUMBER:
891 bv->lockedInsetStoreUndo(Undo::INSERT);
893 short type = par->GetType();
894 if (is_numbered(type)) {
900 if (!label_.empty()) {
904 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("No number"));
907 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("Number"));
915 case LFUN_MATH_NONUMBER:
916 if (is_multi_numbered(par->GetType())) {
918 // par->SetNumbered(!par->IsNumbered());
921 #warning This is a terrible hack! We should find a better solution.
923 while (mathcursor->getLabel() == MathedXIter::error_label) {
924 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
927 mathcursor->setNumbered();
933 case LFUN_MATH_LIMITS:
934 bv->lockedInsetStoreUndo(Undo::INSERT);
935 if (mathcursor->Limits())
942 latexkeys const * l = in_word_set(arg);
943 int const sz = l ? l->id : -1;
944 mathcursor->SetSize(sz);
949 // possible fall through?
951 case LFUN_INSERT_MATH:
952 bv->lockedInsetStoreUndo(Undo::INSERT);
953 InsertSymbol(bv, arg);
957 case LFUN_INSERT_MATRIX:
959 bv->lockedInsetStoreUndo(Undo::INSERT);
962 // This is just so that too long args won't ooze out of s.
963 strncpy(arg2, arg.c_str(), 80);
967 int const k = sscanf(arg2, "%d %d %s", &m, &n, s);
977 MathMatrixInset * p = new MathMatrixInset(m, n);
978 if (k > 2 && int(strlen(s)) > m)
979 p->SetAlign(s[0], &s[1]);
980 mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
987 case LFUN_MATH_DELIM:
989 bv->lockedInsetStoreUndo(Undo::INSERT);
996 string vdelim("(){}[]./|");
1000 ::strncpy(arg2, arg.c_str(), 40);
1002 int const n = sscanf(arg2, "%s %s", lf, rg);
1008 ilf = lyx::atoi(lf);
1011 l = in_word_set(lf, strlen(lf));
1012 // Long words will cause l == 0; so check.
1015 } else if (vdelim.find(lf[0]) != string::npos)
1020 irg = lyx::atoi(rg);
1023 l = in_word_set(rg, strlen(rg));
1026 } else if (vdelim.find(rg[0]) != string::npos)
1031 MathDelimInset * p = new MathDelimInset(ilf, irg);
1032 mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
1038 case LFUN_PROTECTEDSPACE:
1039 bv->lockedInsetStoreUndo(Undo::INSERT);
1040 sp = new MathSpaceInset(1);
1041 mathcursor->insertInset(sp, LM_TC_INSET);
1047 case LFUN_INSERT_LABEL:
1049 bv->lockedInsetStoreUndo(Undo::INSERT);
1050 if (par->GetType() < LM_OT_PAR)
1053 string old_label = is_multiline(par->GetType())
1054 ? mathcursor->getLabel() : par->label();
1056 #ifdef WITH_WARNINGS
1057 #warning This is a terrible hack! We should find a better solution.
1059 // This is needed because in some positions
1060 // mathcursor->cursor->crow is equal to 0, and therefore
1061 // the label cannot be inserted.
1062 // So we move the cursor left until
1063 // mathcursor->cursor->crow != 0.
1064 while (old_label == MathedXIter::error_label) {
1065 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
1067 old_label = mathcursor->getLabel();
1070 string new_label = arg;
1071 if (new_label.empty()) {
1072 string const default_label =
1073 (lyxrc.label_init_length >= 0) ? "eq:" : "";
1074 pair<bool, string> const res = old_label.empty()
1075 ? askForText(_("Enter new label to insert:"),
1077 : askForText(_("Enter label:"), old_label);
1080 new_label = frontStrip(strip(res.second));
1083 if (new_label == old_label)
1084 break; // Nothing to do
1086 if (!new_label.empty())
1089 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
1092 #ifdef WITH_WARNINGS
1096 if (is_multi_numbered(par->GetType())) {
1097 mathcursor->setLabel(new_label);
1098 // par->SetLabel(new_label);
1108 case LFUN_MATH_DISPLAY:
1109 bv->lockedInsetStoreUndo(Undo::EDIT);
1110 display(!disp_flag_);
1115 // Invalid actions under math mode
1116 case LFUN_MATH_MODE:
1117 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1118 bv->owner()->getLyXFunc()
1119 ->Dispatch(LFUN_MESSAGE, _("math text mode"));
1120 varcode = LM_TC_TEXTRM;
1122 varcode = LM_TC_VAR;
1124 mathcursor->setLastCode(varcode);
1128 bv->owner()->getLyXFunc()
1129 ->Dispatch(LFUN_MESSAGE,
1130 _("Invalid action in math mode!"));
1133 //------- dummy actions
1134 #ifdef WITH_WARNINGS
1135 #warning Is this needed here? Shouldnt the main dispatch handle this? (Lgb)
1137 //case LFUN_EXEC_COMMAND:
1138 //bv->owner()->getMiniBuffer()->PrepareForCommand();
1142 if ((action == -1 || action == LFUN_SELFINSERT)
1144 unsigned char c = arg[0];
1145 bv->lockedInsetStoreUndo(Undo::INSERT);
1147 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1149 mathcursor->setAccent(0);
1152 if (c == 0) { // Dead key, do nothing
1153 //lyxerr << "deadkey" << endl;
1158 if (mathcursor->getLastCode() == LM_TC_TEX) {
1159 mathcursor->MacroModeOpen();
1160 mathcursor->clearLastCode();
1161 varcode = LM_TC_MIN;
1162 } else if (!varcode) {
1163 short f = (mathcursor->getLastCode()) ?
1164 mathcursor->getLastCode() :
1165 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1166 varcode = MathIsAlphaFont(f) ?
1167 static_cast<MathedTextCodes>(f) :
1171 // lyxerr << "Varcode << vardoce;
1172 MathedTextCodes char_code = varcode;
1173 if (greek_kb_flag) {
1175 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1176 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1177 'Y', 0, 0, 0, 0 , 'Z' };
1179 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1180 char_code = LM_TC_RM;
1183 char_code = LM_TC_SYMB;
1186 mathcursor->Insert(c, char_code);
1188 if (greek_kb_flag && char_code == LM_TC_RM )
1189 mathcursor->setLastCode(LM_TC_VAR);
1191 varcode = LM_TC_MIN;
1193 if (greek_kb_flag < 2)
1196 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1197 mathcursor->Insert(c, LM_TC_TEX);
1199 mathcursor->Insert('}', LM_TC_TEX);
1202 mathcursor->clearLastCode();
1203 // varcode = LM_TC_MIN;
1204 } else if (c == '_' && varcode == LM_TC_TEX) {
1205 mathcursor->Insert(c, LM_TC_SPECIAL);
1206 mathcursor->clearLastCode();
1207 // varcode = LM_TC_MIN;
1208 } else if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1209 mathcursor->MacroModeOpen();
1210 mathcursor->clearLastCode();
1211 mathcursor->Insert(c, LM_TC_MIN);
1212 } else if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c)) {
1213 mathcursor->Insert(c, LM_TC_CONST);
1214 } else if (strchr("+/-*<>=", c)) {
1215 mathcursor->Insert(c, LM_TC_BOP);
1216 } else if (strchr(latex_special_chars, c) && c!= '_') {
1217 mathcursor->Insert(c, LM_TC_SPECIAL);
1218 } else if (c == '_' || c == '^') {
1222 mathcursor->Interpret(s);
1223 } else if (c == ' ') {
1225 short f = (mathcursor->getLastCode()) ?
1226 mathcursor->getLastCode() :
1227 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1228 varcode = MathIsAlphaFont(f) ?
1229 static_cast<MathedTextCodes>(f) :
1233 if (varcode == LM_TC_TEXTRM) {
1234 mathcursor->Insert(c, LM_TC_TEXTRM);
1235 } else if (was_macro) {
1236 mathcursor->MacroModeClose();
1238 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1241 } else if (!mathcursor->Pop() && mathcursor->IsEnd())
1243 } else if (c == '\'' || c == '@') {
1244 mathcursor->Insert (c, LM_TC_VAR);
1245 } else if (c == '\\') {
1247 mathcursor->MacroModeClose();
1248 // This line nukes the mathcursor. Why?
1249 //bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("TeX mode"));
1250 mathcursor->setLastCode(LM_TC_TEX);
1254 } else if (action == LFUN_MATH_PANEL) {
1255 result = UNDISPATCHED;
1257 // lyxerr << "Closed by action " << action << endl;
1265 if ((mathcursor && was_macro != mathcursor->InMacroMode())
1267 && action != LFUN_BACKSPACE)
1270 if (sp && !space_on)
1273 if (mathcursor && (mathcursor->Selection() || was_selection))
1274 ToggleInsetSelection(bv);
1276 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
1277 result == UNDISPATCHED)
1278 ShowInsetCursor(bv);
1280 bv->unlockInset(this);
1282 return result; // original version
1288 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1290 MathedIter it(&par->GetData());
1292 while (it.OK() && !(features.binom && features.boldsymbol)) {
1294 if (it.IsActive()) {
1295 MathParInset * p = it.GetActiveInset();
1296 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1297 p->GetName() == "binom") {
1298 features.binom = true;
1300 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1301 p->setArgumentIdx(i);
1302 mathedValidate(features, p);
1306 MathedInset * p = it.GetInset();
1307 if (!features.boldsymbol && p->GetName() == "boldsymbol") {
1308 features.boldsymbol = true;
1317 int mathed_write(MathParInset * p, ostream & os,
1318 bool fragile, string const & label)
1320 number_of_newlines = 0;
1321 short mathed_env = p->GetType();
1323 if (mathed_env == LM_OT_MIN) {
1324 if (fragile) os << "\\protect";
1325 os << "\\( "; // changed from " \\( " (Albrecht Dress)
1327 if (mathed_env == LM_OT_PAR){
1331 << latex_mathenv[mathed_env]
1333 if (is_multicolumn(mathed_env)) {
1334 if (mathed_env != LM_OT_ALIGNAT
1335 && mathed_env != LM_OT_ALIGNATN)
1337 os << "{" << p->GetColumns()/2 << "}";
1341 ++number_of_newlines;
1344 if (!label.empty() && label[0] > ' '
1345 && is_singlely_numbered(mathed_env)) {
1349 ++number_of_newlines;
1352 p->Write(os, fragile);
1354 if (mathed_env == LM_OT_MIN){
1355 if (fragile) os << "\\protect";
1357 } else if (mathed_env == LM_OT_PAR) {
1359 ++number_of_newlines;
1362 << latex_mathenv[mathed_env]
1364 number_of_newlines += 2;
1366 return number_of_newlines;
1372 /* FIXME: math-greek-toggle seems to work OK, but math-greek doesn't turn
1374 bool math_insert_greek(BufferView * bv, char c)
1376 if (bv->available() &&
1377 (('A' <= c && c <= 'Z') ||
1378 ('a'<= c && c<= 'z'))) {
1381 if (!bv->theLockingInset() || bv->theLockingInset()->IsTextInset()) {
1382 int greek_kb_flag_save = greek_kb_flag;
1383 InsetFormula * new_inset = new InsetFormula();
1384 bv->beforeChange(bv->text);
1385 if (!bv->insertInset(new_inset)) {
1390 new_inset->Edit(bv, 0, 0, 0);
1391 new_inset->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
1392 if (greek_kb_flag_save < 2) {
1393 bv->unlockInset(new_inset); // bv->theLockingInset());
1394 bv->text->CursorRight(bv, true);
1397 if (bv->theLockingInset()->LyxCode() == Inset::MATH_CODE ||
1398 bv->theLockingInset()->LyxCode() == Inset::MATHMACRO_CODE)
1399 static_cast<InsetFormula*>(bv->theLockingInset())->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
1401 lyxerr << "Math error: attempt to write on a wrong "
1402 "class of inset." << endl;