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;
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 //#ifdef WITH_WARNINGS
266 //#warning Alejandro, the number of lines is not returned in this case
267 // This problem will disapear at 0.13.
269 return mathed_write(par, os, fragile, label_);
273 int InsetFormula::Ascii(Buffer const *, ostream & os, int) const
275 par->Write(os, false);
280 int InsetFormula::Linuxdoc(Buffer const * buf, ostream & os) const
282 return Ascii(buf, os, 0);
286 int InsetFormula::DocBook(Buffer const * buf, ostream & os) const
288 return Ascii(buf, os, 0);
292 // Check if uses AMS macros
293 void InsetFormula::Validate(LaTeXFeatures & features) const
295 if (is_ams(par->GetType()))
296 features.amsstyle = true;
298 // Validation is necessary only if not using AMS math.
299 // To be safe, we will always run mathedValidate.
300 //if (!features.amsstyle)
301 mathedValidate(features, par);
305 void InsetFormula::Read(Buffer const *buffer, LyXLex & lex)
307 istream & is = lex.getStream();
309 mathed_parser_file(is, lex.GetLineNo());
311 // Silly hack to read labels.
312 mathed_label.erase();
315 mathed_parse(ar, 0, &par);
317 disp_flag_ = (par->GetType() > 0);
319 // Update line number
320 lex.setLineNo(mathed_parser_lineno());
322 if (!mathed_label.empty()) {
323 label_ = mathed_label;
324 mathed_label.erase();
327 // reading of end_inset in the inset!!!
330 if (lex.GetString() == "\\end_inset")
332 lyxerr << "InsetFormula::Read: Garbage before \\end_inset,"
333 " or missing \\end_inset!" << endl;
336 if (lyxerr.debugging(Debug::MATHED))
337 Write(buffer, lyxerr);
341 int InsetFormula::ascent(BufferView *, LyXFont const &) const
343 return par->Ascent() + (disp_flag_ ? 8 : 1);
347 int InsetFormula::descent(BufferView *, LyXFont const &) const
349 return par->Descent() + (disp_flag_ ? 8 : 1);
353 int InsetFormula::width(BufferView * bv, LyXFont const & f) const
355 MathedInset::workWidth = bv->workWidth();
356 lfont_size = f.size();
358 return par->Width(); //+2;
362 void InsetFormula::draw(BufferView * bv, LyXFont const & f,
363 int baseline, float & x, bool) const
365 MathedInset::workWidth = bv->workWidth();
366 Painter & pain = bv->painter();
367 // Seems commenting out solves a problem.
368 LyXFont font = mathed_get_font(LM_TC_TEXTRM, LM_ST_TEXT);
369 font.setSize(f.size());
370 lfont_size = font.size();
371 /// Let's try to wait a bit with this... (Lgb)
372 //UpdatableInset::draw(pain, font, baseline, x);
374 // otherwise a segfault could occur
375 // in some XDrawRectangles (i.e. matrix) (Matthias)
376 if (mathcursor && mathcursor->GetPar() == par) {
377 if (mathcursor->Selection()) {
381 mathcursor->SelGetArea(&xp, &yp, n);
382 pain.fillPolygon(xp, yp, n, LColor::selection);
384 mathcursor->draw(pain, int(x), baseline);
386 par->draw(pain, int(x), baseline);
388 x += float(width(bv, font));
390 if (is_numbered(par->GetType())) {
391 LyXFont wfont = WhichFont(LM_TC_BF, par->size());
392 wfont.setLatex(LyXFont::OFF);
394 if (is_singlely_numbered(par->GetType())) {
397 str = string("(") + label_ + ")";
400 pain.text(int(x + 20), baseline, str, wfont);
402 MathMatrixInset * mt =
403 static_cast<MathMatrixInset*>(par);
404 MathedRowContainer::iterator crow = mt->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 * bv, string const & s)
464 if (s.empty() || !mathcursor)
466 mathcursor->Interpret(s);
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);
550 if (!label_.empty()) {
559 vector<string> const InsetFormula::getLabelList() const
561 //#ifdef WITH_WARNINGS
562 //#warning This is dirty, I know. Ill clean it at 0.11
563 // Correction, the only way to clean this is with a new kernel: 0.13.
566 vector<string> label_list;
568 if (is_multi_numbered(par->GetType())) {
569 MathMatrixInset * mt = static_cast<MathMatrixInset*>(par);
570 MathedRowContainer::iterator crow = mt->getRowSt().begin();
572 if (!crow->getLabel().empty())
573 label_list.push_back(crow->getLabel());
576 } else if (!label_.empty())
577 label_list.push_back(label_);
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 // extern char *dispatch_result;
675 MathedTextCodes varcode = LM_TC_MIN;
676 bool was_macro = mathcursor->InMacroMode();
678 bool space_on = false;
679 bool was_selection = mathcursor->Selection();
680 RESULT result = DISPATCHED;
681 static MathSpaceInset * sp= 0;
685 if (mathcursor->getLastCode() == LM_TC_TEX) {
690 // --- Cursor Movements ---------------------------------------------
692 sel = true; // fall through...
695 result = DISPATCH_RESULT(mathcursor->Right(sel));
696 if (!sel && (result == DISPATCHED))
697 result = DISPATCHED_NOUPDATE;
702 sel = true; // fall through
705 result = DISPATCH_RESULT(mathcursor->Left(sel));
706 if (!sel && (result == DISPATCHED))
707 result = DISPATCHED_NOUPDATE;
715 result = DISPATCH_RESULT(mathcursor->Up(sel));
716 if (!sel && (result == DISPATCHED))
717 result = DISPATCHED_NOUPDATE;
725 result = DISPATCH_RESULT(mathcursor->Down(sel));
726 if (!sel && (result == DISPATCHED))
727 result = DISPATCHED_NOUPDATE;
733 result = DISPATCHED_NOUPDATE;
738 result = DISPATCHED_NOUPDATE;
741 case LFUN_DELETE_LINE_FORWARD:
742 bv->lockedInsetStoreUndo(Undo::DELETE);
743 mathcursor->DelLine();
749 bv->lockedInsetStoreUndo(Undo::INSERT);
750 byte c = arg.empty() ? '1' : arg[0];
751 mathcursor->Insert(c, LM_TC_CR);
752 if (!label_.empty()) {
753 mathcursor->setLabel(label_);
756 par = mathcursor->GetPar();
762 bv->lockedInsetStoreUndo(Undo::INSERT);
763 mathcursor->Insert(0, LM_TC_TAB);
768 bv->lockedInsetStoreUndo(Undo::INSERT);
769 mathcursor->Insert('T', LM_TC_TAB);
774 if (!mathcursor->Left())
777 if (!mathcursor->InMacroMode() && mathcursor->pullArg()) {
778 bv->updateInset(this, true);
784 bv->lockedInsetStoreUndo(Undo::DELETE);
785 mathcursor->Delete();
786 bv->updateInset(this, true);
790 // sprintf(dispatch_buffer, "%d %d",);
791 // dispatch_result = dispatch_buffer;
799 istringstream ist(arg.c_str());
802 mathcursor->SetPos(x1 + x, y1 + y);
806 /* cursor selection ---------------------------- */
810 mathcursor->MacroModeClose();
811 bv->lockedInsetStoreUndo(Undo::INSERT);
812 mathcursor->SelPaste();
817 bv->lockedInsetStoreUndo(Undo::DELETE);
818 mathcursor->SelCut();
823 mathcursor->SelCopy();
828 case LFUN_WORDRIGHTSEL:
829 case LFUN_WORDLEFTSEL:
832 // --- accented characters ------------------------------
834 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
835 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
836 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
837 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
838 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
839 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
840 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
841 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
842 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
843 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
847 if (!greek_kb_flag) {
849 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("Math greek mode on"));
855 case LFUN_GREEK_TOGGLE:
856 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
858 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("Math greek keyboard on"));
860 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("Math greek keyboard off"));
864 case LFUN_BOLD: mathcursor->toggleLastCode(LM_TC_BF); break;
865 case LFUN_SANS: mathcursor->toggleLastCode(LM_TC_SF); break;
866 case LFUN_EMPH: mathcursor->toggleLastCode(LM_TC_CAL); break;
867 case LFUN_ROMAN: mathcursor->toggleLastCode(LM_TC_RM); break;
868 case LFUN_CODE: mathcursor->toggleLastCode(LM_TC_TT); break;
869 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR); break;
872 // varcode = LM_TC_TEX;
873 mathcursor->setLastCode(LM_TC_TEX);
874 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("TeX mode"));
877 case LFUN_MATH_NUMBER:
878 bv->lockedInsetStoreUndo(Undo::INSERT);
880 short type = par->GetType();
881 if (is_numbered(type)) {
883 if (!label_.empty()) {
886 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("No number"));
889 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("Number"));
896 case LFUN_MATH_NONUMBER:
897 if (is_multi_numbered(par->GetType())) {
898 // MathMatrixInset *mt = (MathMatrixInset*)par;
900 // mt->SetNumbered(!mt->IsNumbered());
903 #warning This is a terrible hack! We should find a better solution.
905 while (mathcursor->getLabel() == MathedXIter::error_label) {
906 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
909 mathcursor->setNumbered();
914 case LFUN_MATH_LIMITS:
915 bv->lockedInsetStoreUndo(Undo::INSERT);
916 if (mathcursor->Limits())
922 latexkeys const * l = in_word_set(arg);
923 int const sz = (l) ? l->id: -1;
924 mathcursor->SetSize(sz);
928 // possible fall through?
930 case LFUN_INSERT_MATH:
931 bv->lockedInsetStoreUndo(Undo::INSERT);
932 InsertSymbol(bv, arg);
936 case LFUN_INSERT_MATRIX:
938 bv->lockedInsetStoreUndo(Undo::INSERT);
941 // This is just so that too long args won't ooze out of s.
942 strncpy(arg2, arg.c_str(), 80);
946 int const k = sscanf(arg2, "%d %d %s", &m, &n, s);
955 MathMatrixInset * p = new MathMatrixInset(m, n);
956 if (mathcursor && p) {
957 if (k > 2 && int(strlen(s)) > m)
958 p->SetAlign(s[0], &s[1]);
959 mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
965 case LFUN_MATH_DELIM:
967 bv->lockedInsetStoreUndo(Undo::INSERT);
974 string vdelim("(){}[]./|");
978 ::strncpy(arg2, arg.c_str(), 40);
980 int const n = sscanf(arg2, "%s %s", lf, rg);
989 l = in_word_set(lf, strlen(lf));
990 // Long words will cause l == 0; so check.
993 } else if (vdelim.find(lf[0]) != string::npos)
1001 l = in_word_set(rg, strlen(rg));
1004 } else if (vdelim.find(rg[0]) != string::npos)
1009 MathDelimInset * p = new MathDelimInset(ilf, irg);
1010 mathcursor->insertInset(p, LM_TC_ACTIVE_INSET);
1015 case LFUN_PROTECTEDSPACE:
1016 bv->lockedInsetStoreUndo(Undo::INSERT);
1017 sp = new MathSpaceInset(1);
1018 mathcursor->insertInset(sp, LM_TC_INSET);
1023 case LFUN_INSERT_LABEL:
1025 bv->lockedInsetStoreUndo(Undo::INSERT);
1026 if (par->GetType() < LM_OT_PAR)
1029 string old_label = is_multiline(par->GetType())
1030 ? mathcursor->getLabel() : label_;
1032 #ifdef WITH_WARNINGS
1033 #warning This is a terrible hack! We should find a better solution.
1035 // This is needed because in some positions
1036 // mathcursor->cursor->crow is equal to 0, and therefore
1037 // the label cannot be inserted.
1038 // So we move the cursor left until
1039 // mathcursor->cursor->crow != 0.
1040 while (old_label == MathedXIter::error_label) {
1041 if (LocalDispatch(bv, LFUN_LEFT, string()) == FINISHED)
1043 old_label = mathcursor->getLabel();
1046 string new_label = arg;
1047 if (new_label.empty()) {
1048 string const default_label =
1049 (lyxrc.label_init_length >= 0) ? "eq:" : "";
1050 pair<bool, string> const res = old_label.empty()
1051 ? askForText(_("Enter new label to insert:"),
1053 : askForText(_("Enter label:"), old_label);
1056 new_label = frontStrip(strip(res.second));
1059 if (new_label == old_label)
1060 break; // Nothing to do
1062 if (!new_label.empty())
1065 if (!new_label.empty() && bv->ChangeRefsIfUnique(old_label, new_label))
1068 if (is_multi_numbered(par->GetType())) {
1069 mathcursor->setLabel(new_label);
1070 // MathMatrixInset *mt = (MathMatrixInset*)par;
1071 // mt->SetLabel(new_label);
1079 case LFUN_MATH_DISPLAY:
1080 bv->lockedInsetStoreUndo(Undo::EDIT);
1081 display(!disp_flag_);
1085 // Invalid actions under math mode
1086 case LFUN_MATH_MODE:
1087 if (mathcursor->getLastCode()!= LM_TC_TEXTRM) {
1088 bv->owner()->getLyXFunc()
1089 ->Dispatch(LFUN_MESSAGE, _("math text mode"));
1090 varcode = LM_TC_TEXTRM;
1092 varcode = LM_TC_VAR;
1094 mathcursor->setLastCode(varcode);
1098 bv->owner()->getLyXFunc()
1099 ->Dispatch(LFUN_MESSAGE,
1100 _("Invalid action in math mode!"));
1103 //------- dummy actions
1104 #ifdef WITH_WARNINGS
1105 #warning Is this needed here? Shouldnt the main dispatch handle this? (Lgb)
1107 //case LFUN_EXEC_COMMAND:
1108 //bv->owner()->getMiniBuffer()->PrepareForCommand();
1112 if ((action == -1 || action == LFUN_SELFINSERT)
1114 unsigned char c = arg[0];
1115 bv->lockedInsetStoreUndo(Undo::INSERT);
1117 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1119 mathcursor->setAccent(0);
1122 if (c == 0) { // Dead key, do nothing
1123 //lyxerr << "deadkey" << endl;
1128 if (mathcursor->getLastCode() == LM_TC_TEX) {
1129 mathcursor->MacroModeOpen();
1130 mathcursor->clearLastCode();
1131 varcode = LM_TC_MIN;
1132 } else if (!varcode) {
1133 short f = (mathcursor->getLastCode()) ?
1134 mathcursor->getLastCode() :
1135 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1136 varcode = MathIsAlphaFont(f) ?
1137 static_cast<MathedTextCodes>(f) :
1141 // lyxerr << "Varcode << vardoce;
1142 MathedTextCodes char_code = varcode;
1143 if (greek_kb_flag) {
1145 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1146 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1147 'Y', 0, 0, 0, 0 , 'Z' };
1149 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1150 char_code = LM_TC_RM;
1153 char_code = LM_TC_SYMB;
1156 mathcursor->Insert(c, char_code);
1158 if (greek_kb_flag && char_code == LM_TC_RM )
1159 mathcursor->setLastCode(LM_TC_VAR);
1161 varcode = LM_TC_MIN;
1163 if (greek_kb_flag < 2)
1166 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1167 mathcursor->Insert(c, LM_TC_TEX);
1169 mathcursor->Insert('}', LM_TC_TEX);
1172 mathcursor->clearLastCode();
1173 // varcode = LM_TC_MIN;
1174 } else if (c == '_' && varcode == LM_TC_TEX) {
1175 mathcursor->Insert(c, LM_TC_SPECIAL);
1176 mathcursor->clearLastCode();
1177 // varcode = LM_TC_MIN;
1178 } else if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1179 mathcursor->MacroModeOpen();
1180 mathcursor->clearLastCode();
1181 mathcursor->Insert(c, LM_TC_MIN);
1182 } else if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c)) {
1183 mathcursor->Insert(c, LM_TC_CONST);
1184 } else if (strchr("+/-*<>=", c)) {
1185 mathcursor->Insert(c, LM_TC_BOP);
1186 } else if (strchr(latex_special_chars, c) && c!= '_') {
1187 mathcursor->Insert(c, LM_TC_SPECIAL);
1188 } else if (c == '_' || c == '^') {
1192 mathcursor->Interpret(s);
1193 } else if (c == ' ') {
1195 short f = (mathcursor->getLastCode()) ?
1196 mathcursor->getLastCode() :
1197 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1198 varcode = MathIsAlphaFont(f) ?
1199 static_cast<MathedTextCodes>(f) :
1203 if (varcode == LM_TC_TEXTRM) {
1204 mathcursor->Insert(c, LM_TC_TEXTRM);
1205 } else if (was_macro) {
1206 mathcursor->MacroModeClose();
1208 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1211 } else if (!mathcursor->Pop() && mathcursor->IsEnd())
1213 } else if (c == '\'' || c == '@') {
1214 mathcursor->Insert (c, LM_TC_VAR);
1215 } else if (c == '\\') {
1217 mathcursor->MacroModeClose();
1218 bv->owner()->getLyXFunc()
1219 ->Dispatch(LFUN_MESSAGE,
1221 mathcursor->setLastCode(LM_TC_TEX);
1224 } else if (action == LFUN_MATH_PANEL) {
1225 result = UNDISPATCHED;
1227 // lyxerr << "Closed by action " << action << endl;
1232 if (was_macro != mathcursor->InMacroMode()
1234 && action != LFUN_BACKSPACE)
1237 if (sp && !space_on)
1240 if (mathcursor->Selection() || was_selection)
1241 ToggleInsetSelection(bv);
1243 if ((result == DISPATCHED) || (result == DISPATCHED_NOUPDATE) ||
1244 (result == UNDISPATCHED))
1245 ShowInsetCursor(bv);
1247 bv->unlockInset(this);
1255 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1257 MathedIter it(&par->GetData());
1259 while (it.OK() && !(features.binom && features.boldsymbol)) {
1261 if (it.IsActive()) {
1262 MathParInset * p = it.GetActiveInset();
1263 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1264 p->GetName() == "binom") {
1265 features.binom = true;
1267 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1268 p->setArgumentIdx(i);
1269 mathedValidate(features, p);
1273 MathedInset * p = it.GetInset();
1274 if (!features.boldsymbol && p->GetName() == "boldsymbol") {
1275 features.boldsymbol = true;
1284 int mathed_write(MathParInset * p, ostream & os,
1285 bool fragile, string const & label)
1287 number_of_newlines = 0;
1288 short mathed_env = p->GetType();
1290 if (mathed_env == LM_OT_MIN) {
1291 if (fragile) os << "\\protect";
1292 os << "\\( "; // changed from " \\( " (Albrecht Dress)
1294 if (mathed_env == LM_OT_PAR){
1298 << latex_mathenv[mathed_env]
1300 if (is_multicolumn(mathed_env)) {
1301 if (mathed_env != LM_OT_ALIGNAT
1302 && mathed_env != LM_OT_ALIGNATN)
1304 os << "{" << p->GetColumns()/2 << "}";
1308 ++number_of_newlines;
1311 if (!label.empty() && label[0] > ' '
1312 && is_singlely_numbered(mathed_env)) {
1316 ++number_of_newlines;
1319 p->Write(os, fragile);
1321 if (mathed_env == LM_OT_MIN){
1322 if (fragile) os << "\\protect";
1324 } else if (mathed_env == LM_OT_PAR) {
1326 ++number_of_newlines;
1329 << latex_mathenv[mathed_env]
1331 number_of_newlines += 2;
1333 return number_of_newlines;
1339 /* FIXME: math-greek-toggle seems to work OK, but math-greek doesn't turn
1341 bool math_insert_greek(BufferView * bv, char c)
1343 if (bv->available() &&
1344 (('A' <= c && c <= 'Z') ||
1345 ('a'<= c && c<= 'z'))) {
1348 if (!bv->theLockingInset() || bv->theLockingInset()->IsTextInset()) {
1349 int greek_kb_flag_save = greek_kb_flag;
1350 InsetFormula * new_inset = new InsetFormula();
1351 bv->beforeChange(bv->text);
1352 if (!bv->insertInset(new_inset)) {
1357 new_inset->Edit(bv, 0, 0, 0);
1358 new_inset->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
1359 if (greek_kb_flag_save < 2) {
1360 bv->unlockInset(new_inset); // bv->theLockingInset());
1361 bv->text->CursorRight(bv, true);
1364 if (bv->theLockingInset()->LyxCode() == Inset::MATH_CODE ||
1365 bv->theLockingInset()->LyxCode() == Inset::MATHMACRO_CODE)
1366 static_cast<InsetFormula*>(bv->theLockingInset())->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
1368 lyxerr << "Math error: attempt to write on a wrong "
1369 "class of inset." << endl;