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 extern string mathed_label;
62 extern char const * latex_special_chars;
64 int greek_kb_flag = 0;
65 extern char const * latex_mathenv[];
67 // this is only used by Whichfont and mathed_init_fonts (Lgb)
68 LyXFont * Math_Fonts = 0;
71 MathedCursor * mathcursor = 0;
76 LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
83 int mathed_write(MathParInset *, std::ostream &, bool fragile,
84 string const & label = string());
86 void mathed_init_fonts();
88 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
94 LyXFont WhichFont(short type, int size)
131 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
142 f.setSize(lfont_size);
146 if (type == LM_TC_BSYM) {
159 case LM_ST_SCRIPTSCRIPT:
165 lyxerr << "Mathed Error: wrong font size: " << size << endl;
169 if (type != LM_TC_TEXTRM)
170 f.setColor(LColor::math);
178 void mathed_init_fonts() //removed 'static' because DEC cxx does not
180 // Probably because this func is declared as a friend in math_defs.h
183 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
186 for (int i = 0 ; i < 8 ; ++i) {
187 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
190 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
192 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
194 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
195 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
197 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
199 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
200 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
202 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
204 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
206 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
208 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
209 MathedInset::defaultAscent(lyxfont::maxAscent(f));
210 MathedInset::defaultDescent(lyxfont::maxDescent(f));
211 MathedInset::defaultWidth(lyxfont::width('I', f));
217 // quite a hack i know. Should be done with return values...
218 int number_of_newlines = 0;
221 InsetFormula::InsetFormula(bool display)
223 par = new MathParInset; // this leaks
225 disp_flag_ = display;
227 par->SetType(LM_OT_PAR);
228 par->SetStyle(LM_ST_DISPLAY);
233 InsetFormula::InsetFormula(MathParInset * p)
235 if (is_matrix_type(p->GetType()))
236 lyxerr << "InsetFormula::InsetFormula: This shouldn't happen" << endl;
238 par = is_multiline(p->GetType()) ?
239 new MathMatrixInset(*static_cast<MathMatrixInset*>(p)):
240 new MathParInset(*p);
243 disp_flag_ = par->GetType() > 0;
247 InsetFormula::~InsetFormula()
250 #warning leak this for a while...
256 Inset * InsetFormula::Clone(Buffer const &) const
258 return new InsetFormula(par);
262 void InsetFormula::Write(Buffer const * buf, ostream & os) const
265 Latex(buf, os, false, false);
269 int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
271 return mathed_write(par, os, fragile, par->label());
275 int InsetFormula::Ascii(Buffer const *, ostream & os, int) const
277 par->Write(os, false);
282 int InsetFormula::Linuxdoc(Buffer const * buf, ostream & os) const
284 return Ascii(buf, os, 0);
288 int InsetFormula::DocBook(Buffer const * buf, ostream & os) const
290 return Ascii(buf, os, 0);
294 // Check if uses AMS macros
295 void InsetFormula::Validate(LaTeXFeatures & features) const
297 if (is_ams(par->GetType()))
298 features.amsstyle = true;
300 // Validation is necessary only if not using AMS math.
301 // To be safe, we will always run mathedValidate.
302 //if (!features.amsstyle)
303 mathedValidate(features, par);
307 void InsetFormula::Read(Buffer const *buffer, LyXLex & lex)
309 istream & is = lex.getStream();
311 mathed_parser_file(is, lex.GetLineNo());
314 mathed_parse(ar, par, 0);
315 if (par->isMatrix()) {
316 //lyxerr << "################## InsetFormula::Read: IsMatrix\n";
317 static_cast<MathMatrixInset*>(par)->setData(ar);
321 //lyxerr << "################## InsetFormula::Read: keine Matrix\n";
324 //lyxerr << "InsetFormula::Read: par: " << par << " " << par->GetData() << endl;
327 disp_flag_ = (par->GetType() > 0);
329 // Update line number
330 lex.setLineNo(mathed_parser_lineno());
332 // reading of end_inset in the inset!!!
335 if (lex.GetString() == "\\end_inset")
337 lyxerr << "InsetFormula::Read: Garbage before \\end_inset,"
338 " or missing \\end_inset!" << endl;
341 if (lyxerr.debugging(Debug::MATHED))
342 Write(buffer, lyxerr);
346 int InsetFormula::ascent(BufferView *, LyXFont const &) const
348 return par->Ascent() + (disp_flag_ ? 8 : 1);
352 int InsetFormula::descent(BufferView *, LyXFont const &) const
354 return par->Descent() + (disp_flag_ ? 8 : 1);
358 int InsetFormula::width(BufferView * bv, LyXFont const & f) const
360 MathedInset::workWidth = bv->workWidth();
361 lfont_size = f.size();
363 return par->Width(); //+2;
367 void InsetFormula::draw(BufferView * bv, LyXFont const & f,
368 int baseline, float & x, bool) const
370 MathedInset::workWidth = bv->workWidth();
371 Painter & pain = bv->painter();
372 // Seems commenting out solves a problem.
373 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
374 font.setSize(f.size());
375 lfont_size = font.size();
376 /// Let's try to wait a bit with this... (Lgb)
377 //UpdatableInset::draw(pain, font, baseline, x);
379 // otherwise a segfault could occur
380 // in some XDrawRectangles (i.e. matrix) (Matthias)
381 if (mathcursor && mathcursor->GetPar() == par) {
382 if (mathcursor->Selection()) {
386 mathcursor->SelGetArea(&xp, &yp, n);
387 pain.fillPolygon(xp, yp, n, LColor::selection);
389 mathcursor->draw(pain, int(x), baseline);
391 par->draw(pain, int(x), baseline);
393 x += float(width(bv, font));
395 if (is_numbered(par->GetType())) {
396 LyXFont wfont = WhichFont(LM_TC_BF, par->size());
397 wfont.setLatex(LyXFont::OFF);
399 if (is_singlely_numbered(par->GetType())) {
401 if (!par->label().empty())
402 str = string("(") + par->label() + ")";
405 pain.text(int(x + 20), baseline, str, wfont);
407 MathedRowContainer::iterator crow = par->getRowSt().begin();
409 int const y = baseline + crow->getBaseline();
410 if (crow->isNumbered()) {
412 if (!crow->getLabel().empty())
413 str = string("(") + crow->getLabel() + ")";
416 pain.text(int(x + 20), y, str, wfont);
422 setCursorVisible(false);
426 string const InsetFormula::EditMessage() const
428 return _("Math editor mode");
432 void InsetFormula::Edit(BufferView * bv, int x, int y, unsigned int)
434 mathcursor = new MathedCursor(par);
436 if (!bv->lockInset(this))
437 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
440 bv->updateInset(this, false);
443 mathcursor->SetPos(x, y);
450 void InsetFormula::InsetUnlock(BufferView * bv)
453 if (mathcursor->InMacroMode()) {
454 mathcursor->MacroModeClose();
460 bv->updateInset(this, false);
464 // Now a symbol can be inserted only if the inset is locked
465 void InsetFormula::InsertSymbol(BufferView *, string const & s)
467 if (s.empty() || !mathcursor)
469 mathcursor->Interpret(s);
470 // Andre: UpdateLocal(bv);
474 void InsetFormula::GetCursorPos(BufferView *, int & x, int & y) const
476 mathcursor->GetPos(x, y);
482 void InsetFormula::ToggleInsetCursor(BufferView * bv)
489 mathcursor->GetPos(x, y);
492 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
493 int const asc = lyxfont::maxAscent(font);
494 int const desc = lyxfont::maxDescent(font);
496 if (isCursorVisible())
497 bv->hideLockedInsetCursor();
499 bv->showLockedInsetCursor(x, y, asc, desc);
501 toggleCursorVisible();
505 void InsetFormula::ShowInsetCursor(BufferView * bv, bool)
507 if (!isCursorVisible()) {
511 mathcursor->GetPos(x, y);
514 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
515 int const asc = lyxfont::maxAscent(font);
516 int const desc = lyxfont::maxDescent(font);
517 bv->fitLockedInsetCursor(x, y, asc, desc);
519 ToggleInsetCursor(bv);
524 void InsetFormula::HideInsetCursor(BufferView * bv)
526 if (isCursorVisible())
527 ToggleInsetCursor(bv);
531 void InsetFormula::ToggleInsetSelection(BufferView * bv)
536 bv->updateInset(this, false);
540 void InsetFormula::display(bool dspf)
542 if (dspf != disp_flag_) {
544 par->SetType(LM_OT_PAR);
545 par->SetStyle(LM_ST_DISPLAY);
547 if (is_multiline(par->GetType())) {
549 mathcursor->SetPar(par);
551 par->SetType(LM_OT_MIN);
552 par->SetStyle(LM_ST_TEXT);
557 if (!label_.empty()) {
567 vector<string> const InsetFormula::getLabelList() const
569 vector<string> label_list;
570 MathedRowContainer::iterator crow = par->getRowSt().begin();
571 for ( ; crow; ++crow)
572 label_list.push_back(crow->getLabel());
578 void InsetFormula::UpdateLocal(BufferView * bv)
580 par->Metrics(); // To inform lyx kernel the exact size
581 // (there were problems with arrays).
582 bv->updateInset(this, true);
586 void InsetFormula::InsetButtonRelease(BufferView * bv,
587 int x, int y, int /*button*/)
593 mathcursor->SetPos(x, y);
598 bv->updateInset(this, false);
604 void InsetFormula::InsetButtonPress(BufferView * bv, int x, int y,
610 if (mathcursor && mathcursor->Selection()) {
611 mathcursor->SelClear();
612 bv->updateInset(this, false);
617 void InsetFormula::InsetMotionNotify(BufferView * bv,
618 int x, int y, int /*button*/)
620 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
623 mathcursor->SetPos(sel_x + par->xo(), sel_y + par->yo());
624 mathcursor->SelStart();
626 mathcursor->GetPos(sel_x, sel_y);
627 } else if (sel_flag) {
631 mathcursor->SetPos(x, y);
633 mathcursor->GetPos(x, y);
634 if (sel_x != x || sel_y != y)
635 bv->updateInset(this, false);
642 void InsetFormula::InsetKeyPress(XKeyEvent *)
644 lyxerr[Debug::MATHED] << "Used InsetFormula::InsetKeyPress." << endl;
648 // Special Mathed functions
649 bool InsetFormula::SetNumber(bool numbf)
652 short type = par->GetType();
653 bool const oldf = is_numbered(type);
665 UpdatableInset::RESULT
666 InsetFormula::LocalDispatch(BufferView * bv, kb_action action,
669 //lyxerr << "InsetFormula::LocalDispatch: act: " << action
670 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
671 // extern char *dispatch_result;
677 MathedTextCodes varcode = LM_TC_MIN;
678 bool was_macro = mathcursor->InMacroMode();
680 bool space_on = false;
681 bool was_selection = mathcursor->Selection();
682 RESULT result = DISPATCHED;
683 static MathSpaceInset * sp= 0;
687 if (mathcursor->getLastCode() == LM_TC_TEX) {
692 // --- Cursor Movements ---------------------------------------------
694 sel = true; // fall through...
697 result = DISPATCH_RESULT(mathcursor->Right(sel));
698 if (!sel && (result == DISPATCHED))
699 result = DISPATCHED_NOUPDATE;
705 sel = true; // fall through
708 result = DISPATCH_RESULT(mathcursor->Left(sel));
709 if (!sel && (result == DISPATCHED))
710 result = DISPATCHED_NOUPDATE;
719 result = DISPATCH_RESULT(mathcursor->Up(sel));
720 if (!sel && (result == DISPATCHED))
721 result = DISPATCHED_NOUPDATE;
730 result = DISPATCH_RESULT(mathcursor->Down(sel));
731 if (!sel && (result == DISPATCHED))
732 result = DISPATCHED_NOUPDATE;
739 result = DISPATCHED_NOUPDATE;
744 result = DISPATCHED_NOUPDATE;
747 case LFUN_DELETE_LINE_FORWARD:
748 bv->lockedInsetStoreUndo(Undo::DELETE);
749 mathcursor->DelLine();
756 bv->lockedInsetStoreUndo(Undo::INSERT);
757 byte c = arg.empty() ? '1' : arg[0];
758 mathcursor->Insert(c, LM_TC_CR);
763 if (!par->label().empty()) {
764 mathcursor->setLabel(label_);
768 par = mathcursor->GetPar();
775 bv->lockedInsetStoreUndo(Undo::INSERT);
776 mathcursor->Insert(0, LM_TC_TAB);
781 bv->lockedInsetStoreUndo(Undo::INSERT);
782 mathcursor->Insert('T', LM_TC_TAB);
788 if (!mathcursor->Left())
791 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
792 bv->updateInset(this, true);
798 bv->lockedInsetStoreUndo(Undo::DELETE);
799 mathcursor->Delete();
800 bv->updateInset(this, true);
804 // sprintf(dispatch_buffer, "%d %d",);
805 // dispatch_result = dispatch_buffer;
813 istringstream ist(arg.c_str());
816 mathcursor->SetPos(x1 + x, y1 + y);
820 /* cursor selection ---------------------------- */
824 mathcursor->MacroModeClose();
825 bv->lockedInsetStoreUndo(Undo::INSERT);
826 mathcursor->SelPaste();
832 bv->lockedInsetStoreUndo(Undo::DELETE);
833 mathcursor->SelCut();
839 mathcursor->SelCopy();
844 case LFUN_WORDRIGHTSEL:
845 case LFUN_WORDLEFTSEL:
848 // --- accented characters ------------------------------
850 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
851 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
852 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
853 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
854 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
855 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
856 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
857 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
858 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
859 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
863 if (!greek_kb_flag) {
865 bv->owner()->message(_("Math greek mode on"));
871 case LFUN_GREEK_TOGGLE:
872 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
874 bv->owner()->message(_("Math greek keyboard on"));
876 bv->owner()->message(_("Math greek keyboard off"));
880 case LFUN_BOLD: mathcursor->toggleLastCode(LM_TC_BF); break;
881 case LFUN_SANS: mathcursor->toggleLastCode(LM_TC_SF); break;
882 case LFUN_EMPH: mathcursor->toggleLastCode(LM_TC_CAL); break;
883 case LFUN_ROMAN: mathcursor->toggleLastCode(LM_TC_RM); break;
884 case LFUN_CODE: mathcursor->toggleLastCode(LM_TC_TT); break;
885 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR); break;
888 // varcode = LM_TC_TEX;
889 mathcursor->setLastCode(LM_TC_TEX);
890 bv->owner()->message(_("TeX mode"));
893 case LFUN_MATH_NUMBER:
894 bv->lockedInsetStoreUndo(Undo::INSERT);
896 short type = par->GetType();
897 if (is_numbered(type)) {
903 if (!label_.empty()) {
907 bv->owner()->message(_("No number"));
910 bv->owner()->message(_("Number"));
918 case LFUN_MATH_NONUMBER:
919 if (is_multi_numbered(par->GetType())) {
921 // par->SetNumbered(!par->IsNumbered());
924 #warning This is a terrible hack! We should find a better solution.
926 while (mathcursor->getLabel() == MathedXIter::error_label) {
927 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
930 mathcursor->setNumbered();
936 case LFUN_MATH_LIMITS:
937 bv->lockedInsetStoreUndo(Undo::INSERT);
938 if (mathcursor->Limits())
945 latexkeys const * l = in_word_set(arg);
946 int const sz = l ? l->id : -1;
947 mathcursor->SetSize(sz);
952 // possible fall through?
954 case LFUN_INSERT_MATH:
955 bv->lockedInsetStoreUndo(Undo::INSERT);
956 InsertSymbol(bv, arg);
961 case LFUN_INSERT_MATRIX:
963 bv->lockedInsetStoreUndo(Undo::INSERT);
966 // This is just so that too long args won't ooze out of s.
967 strncpy(arg2, arg.c_str(), 80);
971 int const k = sscanf(arg2, "%d %d %s", &m, &n, s);
981 MathMatrixInset * p = new MathMatrixInset(m, n);
982 if (k > 2 && int(strlen(s)) > m)
983 p->SetAlign(s[0], &s[1]);
984 mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
991 case LFUN_MATH_DELIM:
993 bv->lockedInsetStoreUndo(Undo::INSERT);
1000 string vdelim("(){}[]./|");
1004 ::strncpy(arg2, arg.c_str(), 40);
1006 int const n = sscanf(arg2, "%s %s", lf, rg);
1012 ilf = lyx::atoi(lf);
1015 l = in_word_set(lf);
1016 // Long words will cause l == 0; so check.
1019 } else if (vdelim.find(lf[0]) != string::npos)
1024 irg = lyx::atoi(rg);
1027 l = in_word_set(rg);
1030 } else if (vdelim.find(rg[0]) != string::npos)
1035 MathDelimInset * p = new MathDelimInset(ilf, irg);
1036 mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
1042 case LFUN_PROTECTEDSPACE:
1043 bv->lockedInsetStoreUndo(Undo::INSERT);
1044 sp = new MathSpaceInset(1);
1045 mathcursor->insertInset(sp, LM_TC_INSET);
1051 case LFUN_INSERT_LABEL:
1053 bv->lockedInsetStoreUndo(Undo::INSERT);
1054 if (par->GetType() < LM_OT_PAR)
1057 string old_label = is_multiline(par->GetType())
1058 ? mathcursor->getLabel() : par->label();
1060 #ifdef WITH_WARNINGS
1061 #warning This is a terrible hack! We should find a better solution.
1063 // This is needed because in some positions
1064 // mathcursor->cursor->crow is equal to 0, and therefore
1065 // the label cannot be inserted.
1066 // So we move the cursor left until
1067 // mathcursor->cursor->crow != 0.
1068 while (old_label == MathedXIter::error_label) {
1069 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
1071 old_label = mathcursor->getLabel();
1074 string new_label = arg;
1075 if (new_label.empty()) {
1076 string const default_label =
1077 (lyxrc.label_init_length >= 0) ? "eq:" : "";
1078 pair<bool, string> const res = old_label.empty()
1079 ? askForText(_("Enter new label to insert:"),
1081 : askForText(_("Enter label:"), old_label);
1084 new_label = frontStrip(strip(res.second));
1087 if (new_label == old_label)
1088 break; // Nothing to do
1090 if (!new_label.empty())
1093 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
1096 #ifdef WITH_WARNINGS
1100 if (is_multi_numbered(par->GetType())) {
1101 mathcursor->setLabel(new_label);
1102 // par->SetLabel(new_label);
1112 case LFUN_MATH_DISPLAY:
1113 bv->lockedInsetStoreUndo(Undo::EDIT);
1114 display(!disp_flag_);
1119 // Invalid actions under math mode
1120 case LFUN_MATH_MODE:
1121 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1122 bv->owner()->message(_("math text mode"));
1123 varcode = LM_TC_TEXTRM;
1125 varcode = LM_TC_VAR;
1127 mathcursor->setLastCode(varcode);
1131 bv->owner()->message(_("Invalid action in math mode!"));
1134 case LFUN_MATH_EXTERN:
1135 HandleExtern(arg, bv);
1138 //------- dummy actions
1139 #ifdef WITH_WARNINGS
1140 #warning Is this needed here? Shouldnt the main dispatch handle this? (Lgb)
1142 //case LFUN_EXEC_COMMAND:
1143 //bv->owner()->getMiniBuffer()->PrepareForCommand();
1147 if ((action == -1 || action == LFUN_SELFINSERT)
1149 unsigned char c = arg[0];
1150 bv->lockedInsetStoreUndo(Undo::INSERT);
1152 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1154 mathcursor->setAccent(0);
1157 if (c == 0) { // Dead key, do nothing
1158 //lyxerr << "deadkey" << endl;
1163 if (mathcursor->getLastCode() == LM_TC_TEX) {
1164 mathcursor->MacroModeOpen();
1165 mathcursor->clearLastCode();
1166 varcode = LM_TC_MIN;
1167 } else if (!varcode) {
1168 short f = (mathcursor->getLastCode()) ?
1169 mathcursor->getLastCode() :
1170 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1171 varcode = MathIsAlphaFont(f) ?
1172 static_cast<MathedTextCodes>(f) :
1176 // lyxerr << "Varcode << vardoce;
1177 MathedTextCodes char_code = varcode;
1178 if (greek_kb_flag) {
1180 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1181 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1182 'Y', 0, 0, 0, 0 , 'Z' };
1184 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1185 char_code = LM_TC_RM;
1188 char_code = LM_TC_SYMB;
1191 mathcursor->Insert(c, char_code);
1193 if (greek_kb_flag && char_code == LM_TC_RM )
1194 mathcursor->setLastCode(LM_TC_VAR);
1196 varcode = LM_TC_MIN;
1198 if (greek_kb_flag < 2)
1201 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1202 mathcursor->Insert(c, LM_TC_TEX);
1204 mathcursor->Insert('}', LM_TC_TEX);
1207 mathcursor->clearLastCode();
1208 // varcode = LM_TC_MIN;
1209 } else if (c == '_' && varcode == LM_TC_TEX) {
1210 mathcursor->Insert(c, LM_TC_SPECIAL);
1211 mathcursor->clearLastCode();
1212 // varcode = LM_TC_MIN;
1213 } else if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1214 mathcursor->MacroModeOpen();
1215 mathcursor->clearLastCode();
1216 mathcursor->Insert(c, LM_TC_MIN);
1217 } else if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c)) {
1218 mathcursor->Insert(c, LM_TC_CONST);
1219 } else if (strchr("+/-*<>=", c)) {
1220 mathcursor->Insert(c, LM_TC_BOP);
1221 } else if (strchr(latex_special_chars, c) && c!= '_') {
1222 mathcursor->Insert(c, LM_TC_SPECIAL);
1223 } else if (c == '_' || c == '^') {
1227 mathcursor->Interpret(s);
1228 } else if (c == ' ') {
1230 short f = (mathcursor->getLastCode()) ?
1231 mathcursor->getLastCode() :
1232 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1233 varcode = MathIsAlphaFont(f) ?
1234 static_cast<MathedTextCodes>(f) :
1238 if (varcode == LM_TC_TEXTRM) {
1239 mathcursor->Insert(c, LM_TC_TEXTRM);
1240 } else if (was_macro) {
1241 mathcursor->MacroModeClose();
1243 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1246 } else if (!mathcursor->Pop() && mathcursor->IsEnd())
1248 } else if (c == '\'' || c == '@') {
1249 mathcursor->Insert (c, LM_TC_VAR);
1250 } else if (c == '\\') {
1252 mathcursor->MacroModeClose();
1253 // This line nukes the mathcursor. Why?
1254 #ifdef WITH_WARNINGS
1255 #warning Still? (Lgb)
1257 bv->owner()->message(_("TeX mode"));
1258 mathcursor->setLastCode(LM_TC_TEX);
1262 } else if (action == LFUN_MATH_PANEL) {
1263 result = UNDISPATCHED;
1265 // lyxerr << "Closed by action " << action << endl;
1273 if ((mathcursor && was_macro != mathcursor->InMacroMode())
1275 && action != LFUN_BACKSPACE)
1278 if (sp && !space_on)
1281 if (mathcursor && (mathcursor->Selection() || was_selection))
1282 ToggleInsetSelection(bv);
1284 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
1285 result == UNDISPATCHED)
1286 ShowInsetCursor(bv);
1288 bv->unlockInset(this);
1290 return result; // original version
1296 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1298 MathedIter it(&par->GetData());
1300 while (it.OK() && !(features.binom && features.boldsymbol)) {
1302 if (it.IsActive()) {
1303 MathParInset * p = it.GetActiveInset();
1304 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1305 p->GetName() == "binom") {
1306 features.binom = true;
1308 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1309 p->setArgumentIdx(i);
1310 mathedValidate(features, p);
1314 MathedInset * p = it.GetInset();
1315 if (!features.boldsymbol && p->GetName() == "boldsymbol") {
1316 features.boldsymbol = true;
1325 int mathed_write(MathParInset * p, ostream & os,
1326 bool fragile, string const & label)
1328 number_of_newlines = 0;
1329 short mathed_env = p->GetType();
1331 if (mathed_env == LM_OT_MIN) {
1332 if (fragile) os << "\\protect";
1333 os << "\\( "; // changed from " \\( " (Albrecht Dress)
1335 if (mathed_env == LM_OT_PAR){
1339 << latex_mathenv[mathed_env]
1341 if (is_multicolumn(mathed_env)) {
1342 if (mathed_env != LM_OT_ALIGNAT
1343 && mathed_env != LM_OT_ALIGNATN)
1345 os << "{" << p->GetColumns()/2 << "}";
1349 ++number_of_newlines;
1352 if (!label.empty() && label[0] > ' '
1353 && is_singlely_numbered(mathed_env)) {
1357 ++number_of_newlines;
1360 p->Write(os, fragile);
1362 if (mathed_env == LM_OT_MIN){
1363 if (fragile) os << "\\protect";
1365 } else if (mathed_env == LM_OT_PAR) {
1367 ++number_of_newlines;
1370 << latex_mathenv[mathed_env]
1372 number_of_newlines += 2;
1374 return number_of_newlines;
1380 /* FIXME: math-greek-toggle seems to work OK, but math-greek doesn't turn
1382 bool math_insert_greek(BufferView * bv, char c)
1384 if (bv->available() &&
1385 (('A' <= c && c <= 'Z') ||
1386 ('a'<= c && c<= 'z'))) {
1389 if (!bv->theLockingInset() || bv->theLockingInset()->IsTextInset()) {
1390 int greek_kb_flag_save = greek_kb_flag;
1391 InsetFormula * new_inset = new InsetFormula();
1392 bv->beforeChange(bv->text);
1393 if (!bv->insertInset(new_inset)) {
1398 new_inset->Edit(bv, 0, 0, 0);
1399 new_inset->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
1400 if (greek_kb_flag_save < 2) {
1401 bv->unlockInset(new_inset); // bv->theLockingInset());
1402 bv->text->CursorRight(bv, true);
1405 if (bv->theLockingInset()->LyxCode() == Inset::MATH_CODE ||
1406 bv->theLockingInset()->LyxCode() == Inset::MATHMACRO_CODE)
1407 static_cast<InsetFormula*>(bv->theLockingInset())->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
1409 lyxerr << "Math error: attempt to write on a wrong "
1410 "class of inset." << endl;
1416 void InsetFormula::HandleExtern(const string & arg, BufferView * bv)
1418 //string outfile = lyx::tempName("maple.out");
1419 string outfile = "/tmp/lyx2" + arg + ".out";
1421 par->WriteNormal(os);
1422 string code = os.str().c_str();
1423 string script = "lyx2" + arg + " '" + code + "' " + outfile;
1424 lyxerr << "calling: " << script << endl;
1425 Systemcalls cmd(Systemcalls::System, script, 0);
1427 ifstream is(outfile.c_str());
1429 mathed_parser_file(is, 0);
1430 mathed_parse(ar, par, 0);