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.
22 #pragma implementation
26 #include "commandtags.h"
27 #include "math_cursor.h"
28 #include "math_parser.h"
30 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
35 #include "lyx_gui_misc.h"
36 #include "support/LOstream.h"
37 #include "support/lyxlib.h"
38 #include "support/syscall.h"
43 #include "math_inset.h"
44 #include "math_parinset.h"
45 #include "math_matrixinset.h"
46 #include "math_rowst.h"
47 #include "math_spaceinset.h"
48 #include "math_deliminset.h"
49 #include "mathed/support.h"
51 using std::ostringstream;
60 #ifndef CXX_GLOBAL_CSTD
65 extern string mathed_label;
67 extern char const * latex_special_chars;
69 int greek_kb_flag = 0;
70 extern char const * latex_mathenv[];
72 // this is only used by Whichfont and mathed_init_fonts (Lgb)
73 LyXFont * Math_Fonts = 0;
76 MathedCursor * mathcursor = 0;
81 LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
88 int mathed_write(MathParInset *, std::ostream &, bool fragile,
89 string const & label = string());
91 void mathed_init_fonts();
93 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
99 LyXFont WhichFont(short type, int size)
136 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
147 f.setSize(lfont_size);
151 if (type == LM_TC_BSYM) {
164 case LM_ST_SCRIPTSCRIPT:
170 lyxerr << "Mathed Error: wrong font size: " << size << endl;
174 if (type != LM_TC_TEXTRM)
175 f.setColor(LColor::math);
183 void mathed_init_fonts() //removed 'static' because DEC cxx does not
185 // Probably because this func is declared as a friend in math_defs.h
188 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
191 for (int i = 0 ; i < 8 ; ++i) {
192 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
195 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
197 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
199 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
200 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
202 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
204 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
205 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
207 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
209 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
211 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
213 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
214 MathedInset::defaultAscent(lyxfont::maxAscent(f));
215 MathedInset::defaultDescent(lyxfont::maxDescent(f));
216 MathedInset::defaultWidth(lyxfont::width('I', f));
222 // quite a hack i know. Should be done with return values...
223 int number_of_newlines = 0;
226 InsetFormula::InsetFormula(bool display)
228 par = new MathParInset; // this leaks
230 disp_flag_ = display;
232 par->SetType(LM_OT_PAR);
233 par->SetStyle(LM_ST_DISPLAY);
238 InsetFormula::InsetFormula(MathParInset * p)
240 if (is_matrix_type(p->GetType()))
241 lyxerr << "InsetFormula::InsetFormula: This shouldn't happen" << endl;
243 par = is_multiline(p->GetType()) ?
244 new MathMatrixInset(*static_cast<MathMatrixInset*>(p)):
245 new MathParInset(*p);
248 disp_flag_ = par->GetType() > 0;
252 InsetFormula::~InsetFormula()
255 #warning leak this for a while...
261 Inset * InsetFormula::Clone(Buffer const &) const
263 return new InsetFormula(par);
267 void InsetFormula::Write(Buffer const * buf, ostream & os) const
270 Latex(buf, os, false, false);
274 int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
276 return mathed_write(par, os, fragile, par->label());
280 int InsetFormula::Ascii(Buffer const *, ostream & os, int) const
282 par->Write(os, false);
287 int InsetFormula::Linuxdoc(Buffer const * buf, ostream & os) const
289 return Ascii(buf, os, 0);
293 int InsetFormula::DocBook(Buffer const * buf, ostream & os) const
295 return Ascii(buf, os, 0);
299 // Check if uses AMS macros
300 void InsetFormula::Validate(LaTeXFeatures & features) const
302 if (is_ams(par->GetType()))
303 features.amsstyle = true;
305 // Validation is necessary only if not using AMS math.
306 // To be safe, we will always run mathedValidate.
307 //if (!features.amsstyle)
308 mathedValidate(features, par);
312 void InsetFormula::Read(Buffer const *buffer, LyXLex & lex)
314 istream & is = lex.getStream();
316 mathed_parser_file(is, lex.GetLineNo());
319 mathed_parse(ar, par, 0);
320 if (par->isMatrix()) {
321 //lyxerr << "################## InsetFormula::Read: IsMatrix\n";
322 static_cast<MathMatrixInset*>(par)->setData(ar);
326 //lyxerr << "################## InsetFormula::Read: keine Matrix\n";
329 //lyxerr << "InsetFormula::Read: par: " << par << " " << par->GetData() << endl;
332 disp_flag_ = (par->GetType() > 0);
334 // Update line number
335 lex.setLineNo(mathed_parser_lineno());
337 // reading of end_inset in the inset!!!
340 if (lex.GetString() == "\\end_inset")
342 lyxerr << "InsetFormula::Read: Garbage before \\end_inset,"
343 " or missing \\end_inset!" << endl;
346 if (lyxerr.debugging(Debug::MATHED))
347 Write(buffer, lyxerr);
351 int InsetFormula::ascent(BufferView *, LyXFont const &) const
353 return par->Ascent() + (disp_flag_ ? 8 : 1);
357 int InsetFormula::descent(BufferView *, LyXFont const &) const
359 return par->Descent() + (disp_flag_ ? 8 : 1);
363 int InsetFormula::width(BufferView * bv, LyXFont const & f) const
365 MathedInset::workWidth = bv->workWidth();
366 lfont_size = f.size();
368 return par->Width(); //+2;
372 void InsetFormula::draw(BufferView * bv, LyXFont const & f,
373 int baseline, float & x, bool) const
375 MathedInset::workWidth = bv->workWidth();
376 Painter & pain = bv->painter();
377 // Seems commenting out solves a problem.
378 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
379 font.setSize(f.size());
380 lfont_size = font.size();
381 /// Let's try to wait a bit with this... (Lgb)
382 //UpdatableInset::draw(pain, font, baseline, x);
384 // otherwise a segfault could occur
385 // in some XDrawRectangles (i.e. matrix) (Matthias)
386 if (mathcursor && mathcursor->GetPar() == par) {
387 if (mathcursor->Selection()) {
391 mathcursor->SelGetArea(&xp, &yp, n);
392 pain.fillPolygon(xp, yp, n, LColor::selection);
394 mathcursor->draw(pain, int(x), baseline);
396 par->draw(pain, int(x), baseline);
398 x += float(width(bv, font));
400 if (is_numbered(par->GetType())) {
401 LyXFont wfont = WhichFont(LM_TC_BF, par->size());
402 wfont.setLatex(LyXFont::OFF);
404 if (is_singlely_numbered(par->GetType())) {
406 if (!par->label().empty())
407 str = string("(") + par->label() + ")";
410 pain.text(int(x + 20), baseline, str, wfont);
412 MathedRowContainer::iterator crow = par->getRowSt().begin();
414 int const y = baseline + crow->getBaseline();
415 if (crow->isNumbered()) {
417 if (!crow->getLabel().empty())
418 str = string("(") + crow->getLabel() + ")";
421 pain.text(int(x + 20), y, str, wfont);
427 setCursorVisible(false);
431 string const InsetFormula::EditMessage() const
433 return _("Math editor mode");
437 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
439 mathcursor = new MathedCursor(par);
441 if (!bv->lockInset(this))
442 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
445 bv->updateInset(this, false);
448 mathcursor->SetPos(x, y);
455 void InsetFormula::InsetUnlock(BufferView * bv)
458 if (mathcursor->InMacroMode()) {
459 mathcursor->MacroModeClose();
465 bv->updateInset(this, false);
469 // Now a symbol can be inserted only if the inset is locked
470 void InsetFormula::InsertSymbol(BufferView *, string const & s)
472 if (s.empty() || !mathcursor)
474 mathcursor->Interpret(s);
475 // Andre: UpdateLocal(bv);
479 void InsetFormula::GetCursorPos(BufferView *, int & x, int & y) const
481 mathcursor->GetPos(x, y);
487 void InsetFormula::ToggleInsetCursor(BufferView * bv)
494 mathcursor->GetPos(x, y);
497 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
498 int const asc = lyxfont::maxAscent(font);
499 int const desc = lyxfont::maxDescent(font);
501 if (isCursorVisible())
502 bv->hideLockedInsetCursor();
504 bv->showLockedInsetCursor(x, y, asc, desc);
506 toggleCursorVisible();
510 void InsetFormula::ShowInsetCursor(BufferView * bv, bool)
512 if (!isCursorVisible()) {
516 mathcursor->GetPos(x, y);
519 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
520 int const asc = lyxfont::maxAscent(font);
521 int const desc = lyxfont::maxDescent(font);
522 bv->fitLockedInsetCursor(x, y, asc, desc);
524 ToggleInsetCursor(bv);
529 void InsetFormula::HideInsetCursor(BufferView * bv)
531 if (isCursorVisible())
532 ToggleInsetCursor(bv);
536 void InsetFormula::ToggleInsetSelection(BufferView * bv)
541 bv->updateInset(this, false);
545 void InsetFormula::display(bool dspf)
547 if (dspf != disp_flag_) {
549 par->SetType(LM_OT_PAR);
550 par->SetStyle(LM_ST_DISPLAY);
552 if (is_multiline(par->GetType())) {
554 mathcursor->SetPar(par);
556 par->SetType(LM_OT_MIN);
557 par->SetStyle(LM_ST_TEXT);
562 if (!label_.empty()) {
572 vector<string> const InsetFormula::getLabelList() const
574 vector<string> label_list;
575 MathedRowContainer::iterator crow = par->getRowSt().begin();
576 for ( ; crow; ++crow)
577 label_list.push_back(crow->getLabel());
583 void InsetFormula::UpdateLocal(BufferView * bv)
585 par->Metrics(); // To inform lyx kernel the exact size
586 // (there were problems with arrays).
587 bv->updateInset(this, true);
591 void InsetFormula::InsetButtonRelease(BufferView * bv,
592 int x, int y, int /*button*/)
598 mathcursor->SetPos(x, y);
603 bv->updateInset(this, false);
609 void InsetFormula::InsetButtonPress(BufferView * bv, int x, int y,
615 if (mathcursor && mathcursor->Selection()) {
616 mathcursor->SelClear();
617 bv->updateInset(this, false);
622 void InsetFormula::InsetMotionNotify(BufferView * bv,
623 int x, int y, int /*button*/)
625 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
628 mathcursor->SetPos(sel_x + par->xo(), sel_y + par->yo());
629 mathcursor->SelStart();
631 mathcursor->GetPos(sel_x, sel_y);
632 } else if (sel_flag) {
636 mathcursor->SetPos(x, y);
638 mathcursor->GetPos(x, y);
639 if (sel_x != x || sel_y != y)
640 bv->updateInset(this, false);
647 void InsetFormula::InsetKeyPress(XKeyEvent *)
649 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
653 // Special Mathed functions
654 bool InsetFormula::SetNumber(bool numbf)
657 short type = par->GetType();
658 bool const oldf = is_numbered(type);
670 UpdatableInset::RESULT
671 InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
674 //lyxerr << "InsetFormula::LocalDispatch: act: " << action
675 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
676 // extern char *dispatch_result;
682 MathedTextCodes varcode = LM_TC_MIN;
683 bool was_macro = mathcursor->InMacroMode();
685 bool space_on = false;
686 bool was_selection = mathcursor->Selection();
687 RESULT result = DISPATCHED;
688 static MathSpaceInset * sp= 0;
692 if (mathcursor->getLastCode() == LM_TC_TEX) {
697 // --- Cursor Movements ---------------------------------------------
699 sel = true; // fall through...
702 result = DISPATCH_RESULT(mathcursor->Right(sel));
703 if (!sel && (result == DISPATCHED))
704 result = DISPATCHED_NOUPDATE;
710 sel = true; // fall through
713 result = DISPATCH_RESULT(mathcursor->Left(sel));
714 if (!sel && (result == DISPATCHED))
715 result = DISPATCHED_NOUPDATE;
724 result = DISPATCH_RESULT(mathcursor->Up(sel));
725 if (!sel && (result == DISPATCHED))
726 result = DISPATCHED_NOUPDATE;
735 result = DISPATCH_RESULT(mathcursor->Down(sel));
736 if (!sel && (result == DISPATCHED))
737 result = DISPATCHED_NOUPDATE;
744 result = DISPATCHED_NOUPDATE;
749 result = DISPATCHED_NOUPDATE;
752 case LFUN_DELETE_LINE_FORWARD:
753 bv->lockedInsetStoreUndo(Undo::DELETE);
754 mathcursor->DelLine();
761 bv->lockedInsetStoreUndo(Undo::INSERT);
762 byte c = arg.empty() ? '1' : arg[0];
763 mathcursor->Insert(c, LM_TC_CR);
768 if (!par->label().empty()) {
769 mathcursor->setLabel(label_);
773 par = mathcursor->GetPar();
780 bv->lockedInsetStoreUndo(Undo::INSERT);
781 mathcursor->Insert(0, LM_TC_TAB);
786 bv->lockedInsetStoreUndo(Undo::INSERT);
787 mathcursor->Insert('T', LM_TC_TAB);
793 if (!mathcursor->Left())
796 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
797 bv->updateInset(this, true);
803 bv->lockedInsetStoreUndo(Undo::DELETE);
804 mathcursor->Delete();
805 bv->updateInset(this, true);
809 // sprintf(dispatch_buffer, "%d %d",);
810 // dispatch_result = dispatch_buffer;
818 istringstream ist(arg.c_str());
821 mathcursor->SetPos(x1 + x, y1 + y);
825 /* cursor selection ---------------------------- */
829 mathcursor->MacroModeClose();
830 bv->lockedInsetStoreUndo(Undo::INSERT);
831 mathcursor->SelPaste();
837 bv->lockedInsetStoreUndo(Undo::DELETE);
838 mathcursor->SelCut();
844 mathcursor->SelCopy();
849 case LFUN_WORDRIGHTSEL:
850 case LFUN_WORDLEFTSEL:
853 // --- accented characters ------------------------------
855 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
856 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
857 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
858 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
859 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
860 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
861 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
862 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
863 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
864 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
868 if (!greek_kb_flag) {
870 bv->owner()->message(_("Math greek mode on"));
876 case LFUN_GREEK_TOGGLE:
877 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
879 bv->owner()->message(_("Math greek keyboard on"));
881 bv->owner()->message(_("Math greek keyboard off"));
885 case LFUN_BOLD: mathcursor->toggleLastCode(LM_TC_BF); break;
886 case LFUN_SANS: mathcursor->toggleLastCode(LM_TC_SF); break;
887 case LFUN_EMPH: mathcursor->toggleLastCode(LM_TC_CAL); break;
888 case LFUN_ROMAN: mathcursor->toggleLastCode(LM_TC_RM); break;
889 case LFUN_CODE: mathcursor->toggleLastCode(LM_TC_TT); break;
890 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR); break;
893 // varcode = LM_TC_TEX;
894 mathcursor->setLastCode(LM_TC_TEX);
895 bv->owner()->message(_("TeX mode"));
898 case LFUN_MATH_NUMBER:
899 bv->lockedInsetStoreUndo(Undo::INSERT);
901 short type = par->GetType();
902 if (is_numbered(type)) {
908 if (!label_.empty()) {
912 bv->owner()->message(_("No number"));
915 bv->owner()->message(_("Number"));
923 case LFUN_MATH_NONUMBER:
924 if (is_multi_numbered(par->GetType())) {
926 // par->SetNumbered(!par->IsNumbered());
929 #warning This is a terrible hack! We should find a better solution.
931 while (mathcursor->getLabel() == MathedXIter::error_label) {
932 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
935 mathcursor->setNumbered();
941 case LFUN_MATH_LIMITS:
942 bv->lockedInsetStoreUndo(Undo::INSERT);
943 if (mathcursor->Limits())
950 latexkeys const * l = in_word_set(arg);
951 int const sz = l ? l->id : -1;
952 mathcursor->SetSize(sz);
957 // possible fall through?
959 case LFUN_INSERT_MATH:
960 bv->lockedInsetStoreUndo(Undo::INSERT);
961 InsertSymbol(bv, arg);
966 case LFUN_INSERT_MATRIX:
968 bv->lockedInsetStoreUndo(Undo::INSERT);
972 // This is just so that too long args won't ooze out of s.
973 //strncpy(arg2, arg.c_str(), 80);
975 string arg2(arg, 0, 80);
979 int const k = sscanf(arg2.c_str(), "%d %d %s", &m, &n, s);
989 MathMatrixInset * p = new MathMatrixInset(m, n);
990 if (k > 2 && int(strlen(s)) > m)
991 p->SetAlign(s[0], &s[1]);
992 mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
999 case LFUN_MATH_DELIM:
1001 bv->lockedInsetStoreUndo(Undo::INSERT);
1007 latexkeys const * l;
1008 string vdelim("(){}[]./|");
1012 //::strncpy(arg2, arg.c_str(), 40);
1014 string arg2(arg, 0, 40);
1016 int const n = sscanf(arg2.c_str(), "%s %s", lf, rg);
1022 ilf = lyx::atoi(lf);
1025 l = in_word_set(lf);
1026 // Long words will cause l == 0; so check.
1029 } else if (vdelim.find(lf[0]) != string::npos)
1034 irg = lyx::atoi(rg);
1037 l = in_word_set(rg);
1040 } else if (vdelim.find(rg[0]) != string::npos)
1045 MathDelimInset * p = new MathDelimInset(ilf, irg);
1046 mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
1052 case LFUN_PROTECTEDSPACE:
1053 bv->lockedInsetStoreUndo(Undo::INSERT);
1054 sp = new MathSpaceInset(1);
1055 mathcursor->insertInset(sp, LM_TC_INSET);
1061 case LFUN_INSERT_LABEL:
1063 bv->lockedInsetStoreUndo(Undo::INSERT);
1064 if (par->GetType() < LM_OT_PAR)
1067 string old_label = is_multiline(par->GetType())
1068 ? mathcursor->getLabel() : par->label();
1070 #ifdef WITH_WARNINGS
1071 #warning This is a terrible hack! We should find a better solution.
1073 // This is needed because in some positions
1074 // mathcursor->cursor->crow is equal to 0, and therefore
1075 // the label cannot be inserted.
1076 // So we move the cursor left until
1077 // mathcursor->cursor->crow != 0.
1078 while (old_label == MathedXIter::error_label) {
1079 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
1081 old_label = mathcursor->getLabel();
1084 string new_label = arg;
1085 if (new_label.empty()) {
1086 string const default_label =
1087 (lyxrc.label_init_length >= 0) ? "eq:" : "";
1088 pair<bool, string> const res = old_label.empty()
1089 ? askForText(_("Enter new label to insert:"),
1091 : askForText(_("Enter label:"), old_label);
1094 new_label = frontStrip(strip(res.second));
1097 if (new_label == old_label)
1098 break; // Nothing to do
1100 if (!new_label.empty())
1103 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
1106 #ifdef WITH_WARNINGS
1110 if (is_multi_numbered(par->GetType())) {
1111 mathcursor->setLabel(new_label);
1112 // par->SetLabel(new_label);
1122 case LFUN_MATH_DISPLAY:
1123 bv->lockedInsetStoreUndo(Undo::EDIT);
1124 display(!disp_flag_);
1129 // Invalid actions under math mode
1130 case LFUN_MATH_MODE:
1131 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1132 bv->owner()->message(_("math text mode"));
1133 varcode = LM_TC_TEXTRM;
1135 varcode = LM_TC_VAR;
1137 mathcursor->setLastCode(varcode);
1141 bv->owner()->message(_("Invalid action in math mode!"));
1144 case LFUN_MATH_EXTERN:
1145 HandleExtern(arg, bv);
1148 //------- dummy actions
1149 #ifdef WITH_WARNINGS
1150 #warning Is this needed here? Shouldnt the main dispatch handle this? (Lgb)
1152 //case LFUN_EXEC_COMMAND:
1153 //bv->owner()->getMiniBuffer()->PrepareForCommand();
1157 if ((action == -1 || action == LFUN_SELFINSERT)
1159 unsigned char c = arg[0];
1160 bv->lockedInsetStoreUndo(Undo::INSERT);
1162 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1164 mathcursor->setAccent(0);
1167 if (c == 0) { // Dead key, do nothing
1168 //lyxerr << "deadkey" << endl;
1173 if (mathcursor->getLastCode() == LM_TC_TEX) {
1174 mathcursor->MacroModeOpen();
1175 mathcursor->clearLastCode();
1176 varcode = LM_TC_MIN;
1177 } else if (!varcode) {
1178 short f = (mathcursor->getLastCode()) ?
1179 mathcursor->getLastCode() :
1180 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1181 varcode = MathIsAlphaFont(f) ?
1182 static_cast<MathedTextCodes>(f) :
1186 // lyxerr << "Varcode << vardoce;
1187 MathedTextCodes char_code = varcode;
1188 if (greek_kb_flag) {
1190 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1191 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1192 'Y', 0, 0, 0, 0 , 'Z' };
1194 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1195 char_code = LM_TC_RM;
1198 char_code = LM_TC_SYMB;
1201 mathcursor->Insert(c, char_code);
1203 if (greek_kb_flag && char_code == LM_TC_RM )
1204 mathcursor->setLastCode(LM_TC_VAR);
1206 varcode = LM_TC_MIN;
1208 if (greek_kb_flag < 2)
1211 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1213 } else if (contains("!,:;{}", c)
1214 && (varcode == LM_TC_TEX||was_macro)) {
1216 mathcursor->Insert(c, LM_TC_TEX);
1218 mathcursor->Insert('}', LM_TC_TEX);
1221 mathcursor->clearLastCode();
1222 // varcode = LM_TC_MIN;
1223 } else if (c == '_' && varcode == LM_TC_TEX) {
1224 mathcursor->Insert(c, LM_TC_SPECIAL);
1225 mathcursor->clearLastCode();
1226 // varcode = LM_TC_MIN;
1227 } else if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1228 mathcursor->MacroModeOpen();
1229 mathcursor->clearLastCode();
1230 mathcursor->Insert(c, LM_TC_MIN);
1231 } else if (('0'<= c && c<= '9') ||
1233 strchr(";:!|[]().,?", c)
1235 contains(";:!|[]().,?", c)
1238 mathcursor->Insert(c, LM_TC_CONST);
1241 strchr("+/-*<>=", c)
1243 contains("+/-*<>=", c)
1246 mathcursor->Insert(c, LM_TC_BOP);
1249 strchr(latex_special_chars, c)
1251 contains(latex_special_chars, c)
1254 mathcursor->Insert(c, LM_TC_SPECIAL);
1255 } else if (c == '_' || c == '^') {
1259 mathcursor->Interpret(s);
1260 } else if (c == ' ') {
1262 short f = (mathcursor->getLastCode()) ?
1263 mathcursor->getLastCode() :
1264 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1265 varcode = MathIsAlphaFont(f) ?
1266 static_cast<MathedTextCodes>(f) :
1270 if (varcode == LM_TC_TEXTRM) {
1271 mathcursor->Insert(c, LM_TC_TEXTRM);
1272 } else if (was_macro) {
1273 mathcursor->MacroModeClose();
1275 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1278 } else if (!mathcursor->Pop() && mathcursor->IsEnd())
1280 } else if (c == '\'' || c == '@') {
1281 mathcursor->Insert (c, LM_TC_VAR);
1282 } else if (c == '\\') {
1284 mathcursor->MacroModeClose();
1285 // This line nukes the mathcursor. Why?
1286 #ifdef WITH_WARNINGS
1287 #warning Still? (Lgb)
1289 bv->owner()->message(_("TeX mode"));
1290 mathcursor->setLastCode(LM_TC_TEX);
1294 } else if (action == LFUN_MATH_PANEL) {
1295 result = UNDISPATCHED;
1297 // lyxerr << "Closed by action " << action << endl;
1305 if ((mathcursor && was_macro != mathcursor->InMacroMode())
1307 && action != LFUN_BACKSPACE)
1310 if (sp && !space_on)
1313 if (mathcursor && (mathcursor->Selection() || was_selection))
1314 ToggleInsetSelection(bv);
1316 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
1317 result == UNDISPATCHED)
1318 ShowInsetCursor(bv);
1320 bv->unlockInset(this);
1322 return result; // original version
1328 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1330 MathedIter it(&par->GetData());
1332 while (it.OK() && !(features.binom && features.boldsymbol)) {
1334 if (it.IsActive()) {
1335 MathParInset * p = it.GetActiveInset();
1336 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1337 p->GetName() == "binom") {
1338 features.binom = true;
1340 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1341 p->setArgumentIdx(i);
1342 mathedValidate(features, p);
1346 MathedInset * p = it.GetInset();
1347 if (!features.boldsymbol && p->GetName() == "boldsymbol") {
1348 features.boldsymbol = true;
1357 int mathed_write(MathParInset * p, ostream & os,
1358 bool fragile, string const & label)
1360 number_of_newlines = 0;
1361 short mathed_env = p->GetType();
1363 if (mathed_env == LM_OT_MIN) {
1364 if (fragile) os << "\\protect";
1365 os << "\\( "; // changed from " \\( " (Albrecht Dress)
1367 if (mathed_env == LM_OT_PAR){
1371 << latex_mathenv[mathed_env]
1373 if (is_multicolumn(mathed_env)) {
1374 if (mathed_env != LM_OT_ALIGNAT
1375 && mathed_env != LM_OT_ALIGNATN)
1377 os << "{" << p->GetColumns()/2 << "}";
1381 ++number_of_newlines;
1384 if (!label.empty() && label[0] > ' '
1385 && is_singlely_numbered(mathed_env)) {
1389 ++number_of_newlines;
1392 p->Write(os, fragile);
1394 if (mathed_env == LM_OT_MIN){
1395 if (fragile) os << "\\protect";
1397 } else if (mathed_env == LM_OT_PAR) {
1399 ++number_of_newlines;
1402 << latex_mathenv[mathed_env]
1404 number_of_newlines += 2;
1406 return number_of_newlines;
1412 /* FIXME: math-greek-toggle seems to work OK, but math-greek doesn't turn
1414 bool math_insert_greek(BufferView * bv, char c)
1416 if (bv->available() &&
1417 (('A' <= c && c <= 'Z') ||
1418 ('a'<= c && c<= 'z'))) {
1421 if (!bv->theLockingInset() || bv->theLockingInset()->IsTextInset()) {
1422 int greek_kb_flag_save = greek_kb_flag;
1423 InsetFormula * new_inset = new InsetFormula();
1424 bv->beforeChange(bv->text);
1425 if (!bv->insertInset(new_inset)) {
1430 new_inset->Edit(bv, 0, 0, 0);
1431 new_inset->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
1432 if (greek_kb_flag_save < 2) {
1433 bv->unlockInset(new_inset); // bv->theLockingInset());
1434 bv->text->CursorRight(bv, true);
1437 if (bv->theLockingInset()->LyxCode() == Inset::MATH_CODE ||
1438 bv->theLockingInset()->LyxCode() == Inset::MATHMACRO_CODE)
1439 static_cast<InsetFormula*>(bv->theLockingInset())->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
1441 lyxerr << "Math error: attempt to write on a wrong "
1442 "class of inset." << endl;
1448 void InsetFormula::HandleExtern(const string & arg, BufferView * bv)
1450 //string outfile = lyx::tempName("maple.out");
1451 string outfile = "/tmp/lyx2" + arg + ".out";
1453 par->WriteNormal(os);
1454 string code = os.str().c_str();
1455 string script = "lyx2" + arg + " '" + code + "' " + outfile;
1456 lyxerr << "calling: " << script << endl;
1457 Systemcalls cmd(Systemcalls::System, script, 0);
1459 ifstream is(outfile.c_str());
1461 mathed_parser_file(is, 0);
1462 mathed_parse(ar, par, 0);