3 * Purpose: Implementation of formula inset
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
6 * Description: Allows the edition of math paragraphs inside Lyx.
8 * Copyright: 1996-1998 Alejandro Aguilar Sierra
10 * Version: 0.4, Lyx project.
12 * You are free to use and modify this code under the terms of
13 * the GNU General Public Licence version 2 or later.
21 #pragma implementation
25 #include "commandtags.h"
26 #include "math_cursor.h"
27 #include "math_parser.h"
29 #include "minibuffer.h"
30 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
35 #include "lyx_gui_misc.h"
36 #include "support/LOstream.h"
40 #include "support/lyxlib.h"
42 #include "math_inset.h"
43 #include "math_parinset.h"
44 #include "math_matrixinset.h"
45 #include "math_rowst.h"
46 #include "math_spaceinset.h"
47 #include "math_deliminset.h"
48 #include "mathed/support.h"
57 extern string mathed_label;
59 extern char const * latex_special_chars;
61 int greek_kb_flag = 0;
62 extern char const * latex_mathenv[];
64 // this is only used by Whichfont and mathed_init_fonts (Lgb)
65 LyXFont * Math_Fonts = 0;
69 LyXFont::FONT_SIZE lfont_size = LyXFont::SIZE_NORMAL;
76 int mathed_write(MathParInset *, std::ostream &, bool fragile,
77 string const & label = string());
79 void mathed_init_fonts();
81 void mathedValidate(LaTeXFeatures & features, MathParInset * par);
86 MathedCursor * InsetFormula::mathcursor = 0;
89 LyXFont WhichFont(short type, int size)
126 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
137 f.setSize(lfont_size);
141 if (type == LM_TC_BSYM) {
154 case LM_ST_SCRIPTSCRIPT:
160 lyxerr << "Mathed Error: wrong font size: " << size << endl;
164 if (type != LM_TC_TEXTRM)
165 f.setColor(LColor::math);
173 void mathed_init_fonts() //removed 'static' because DEC cxx does not
175 // Probably because this func is declared as a friend in math_defs.h
178 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
181 for (int i = 0 ; i < 8 ; ++i) {
182 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
185 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
187 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
189 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
190 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
192 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
194 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
195 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
197 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
199 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
201 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
203 LyXFont f = WhichFont(LM_TC_VAR, LM_ST_TEXT);
204 MathedInset::defaultAscent(lyxfont::maxAscent(f));
205 MathedInset::defaultDescent(lyxfont::maxDescent(f));
206 MathedInset::defaultWidth(lyxfont::width('I', f));
212 // quite a hack i know. Should be done with return values...
213 int number_of_newlines = 0;
216 InsetFormula::InsetFormula(bool display)
218 par = new MathParInset; // this leaks
220 disp_flag_ = display;
222 par->SetType(LM_OT_PAR);
223 par->SetStyle(LM_ST_DISPLAY);
228 InsetFormula::InsetFormula(MathParInset * p)
230 if (is_matrix_type(p->GetType()))
231 lyxerr << "InsetFormula::InsetFormula: This shouldn't happen" << endl;
233 par = is_multiline(p->GetType()) ?
234 new MathMatrixInset(*static_cast<MathMatrixInset*>(p)):
235 new MathParInset(*p);
238 disp_flag_ = (par->GetType()>0);
242 InsetFormula::~InsetFormula()
248 Inset * InsetFormula::Clone(Buffer const &) const
250 InsetFormula * f = new InsetFormula(par);
256 void InsetFormula::Write(Buffer const * buf, ostream & os) const
259 Latex(buf, os, false, false);
263 int InsetFormula::Latex(Buffer const *, ostream & os, bool fragile, bool) const
265 //#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()->getMiniBuffer()->Set(_("Math greek mode on"));
855 case LFUN_GREEK_TOGGLE:
856 greek_kb_flag = (greek_kb_flag) ? 0 : 2;
858 bv->owner()->getMiniBuffer()->Set(_("Math greek keyboard on"));
860 bv->owner()->getMiniBuffer()->Set(_("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()->getMiniBuffer()->Set(_("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()->getMiniBuffer()->Set(_("No number"));
889 bv->owner()->getMiniBuffer()->Set(_("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()->getMiniBuffer()->Set(_("math text mode"));
1089 varcode = LM_TC_TEXTRM;
1091 varcode = LM_TC_VAR;
1093 mathcursor->setLastCode(varcode);
1097 bv->owner()->getMiniBuffer()->Set(_("Invalid action in math mode!"));
1100 //------- dummy actions
1101 case LFUN_EXEC_COMMAND:
1102 bv->owner()->getMiniBuffer()->PrepareForCommand();
1106 if ((action == -1 || action == LFUN_SELFINSERT)
1108 unsigned char c = arg[0];
1109 bv->lockedInsetStoreUndo(Undo::INSERT);
1111 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
1113 mathcursor->setAccent(0);
1116 if (c == 0) { // Dead key, do nothing
1117 //lyxerr << "deadkey" << endl;
1122 if (mathcursor->getLastCode() == LM_TC_TEX) {
1123 mathcursor->MacroModeOpen();
1124 mathcursor->clearLastCode();
1125 varcode = LM_TC_MIN;
1126 } else if (!varcode) {
1127 short f = (mathcursor->getLastCode()) ?
1128 mathcursor->getLastCode() :
1129 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1130 varcode = MathIsAlphaFont(f) ?
1131 static_cast<MathedTextCodes>(f) :
1135 // lyxerr << "Varcode << vardoce;
1136 MathedTextCodes char_code = varcode;
1137 if (greek_kb_flag) {
1139 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
1140 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
1141 'Y', 0, 0, 0, 0 , 'Z' };
1143 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
1144 char_code = LM_TC_RM;
1147 char_code = LM_TC_SYMB;
1150 mathcursor->Insert(c, char_code);
1152 if (greek_kb_flag && char_code == LM_TC_RM )
1153 mathcursor->setLastCode(LM_TC_VAR);
1155 varcode = LM_TC_MIN;
1157 if (greek_kb_flag < 2)
1160 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
1161 mathcursor->Insert(c, LM_TC_TEX);
1163 mathcursor->Insert('}', LM_TC_TEX);
1166 mathcursor->clearLastCode();
1167 // varcode = LM_TC_MIN;
1168 } else if (c == '_' && varcode == LM_TC_TEX) {
1169 mathcursor->Insert(c, LM_TC_SPECIAL);
1170 mathcursor->clearLastCode();
1171 // varcode = LM_TC_MIN;
1172 } else if (('0'<= c && c<= '9') && (varcode == LM_TC_TEX||was_macro)) {
1173 mathcursor->MacroModeOpen();
1174 mathcursor->clearLastCode();
1175 mathcursor->Insert(c, LM_TC_MIN);
1176 } else if (('0'<= c && c<= '9') || strchr(";:!|[]().,?", c)) {
1177 mathcursor->Insert(c, LM_TC_CONST);
1178 } else if (strchr("+/-*<>=", c)) {
1179 mathcursor->Insert(c, LM_TC_BOP);
1180 } else if (strchr(latex_special_chars, c) && c!= '_') {
1181 mathcursor->Insert(c, LM_TC_SPECIAL);
1182 } else if (c == '_' || c == '^') {
1186 mathcursor->Interpret(s);
1187 } else if (c == ' ') {
1189 short f = (mathcursor->getLastCode()) ?
1190 mathcursor->getLastCode() :
1191 static_cast<MathedTextCodes>(mathcursor->GetFCode());
1192 varcode = MathIsAlphaFont(f) ?
1193 static_cast<MathedTextCodes>(f) :
1197 if (varcode == LM_TC_TEXTRM) {
1198 mathcursor->Insert(c, LM_TC_TEXTRM);
1199 } else if (was_macro) {
1200 mathcursor->MacroModeClose();
1202 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
1205 } else if (!mathcursor->Pop() && mathcursor->IsEnd())
1207 } else if (c == '\'' || c == '@') {
1208 mathcursor->Insert (c, LM_TC_VAR);
1209 } else if (c == '\\') {
1211 mathcursor->MacroModeClose();
1212 bv->owner()->getMiniBuffer()->Set(_("TeX mode"));
1213 mathcursor->setLastCode(LM_TC_TEX);
1216 } else if (action == LFUN_MATH_PANEL) {
1217 result = UNDISPATCHED;
1219 // lyxerr << "Closed by action " << action << endl;
1224 if (was_macro != mathcursor->InMacroMode()
1226 && action != LFUN_BACKSPACE)
1229 if (sp && !space_on)
1232 if (mathcursor->Selection() || was_selection)
1233 ToggleInsetSelection(bv);
1235 if ((result == DISPATCHED) || (result == DISPATCHED_NOUPDATE) ||
1236 (result == UNDISPATCHED))
1237 ShowInsetCursor(bv);
1239 bv->unlockInset(this);
1247 void mathedValidate(LaTeXFeatures & features, MathParInset * par)
1249 MathedIter it(&par->GetData());
1251 while (it.OK() && !(features.binom && features.boldsymbol)) {
1253 if (it.IsActive()) {
1254 MathParInset * p = it.GetActiveInset();
1255 if (!features.binom && p->GetType() == LM_OT_MACRO &&
1256 p->GetName() == "binom") {
1257 features.binom = true;
1259 for (int i = 0; i <= p->getMaxArgumentIdx(); ++i) {
1260 p->setArgumentIdx(i);
1261 mathedValidate(features, p);
1265 MathedInset * p = it.GetInset();
1266 if (!features.boldsymbol && p->GetName() == "boldsymbol") {
1267 features.boldsymbol = true;
1276 int mathed_write(MathParInset * p, ostream & os,
1277 bool fragile, string const & label)
1279 number_of_newlines = 0;
1280 short mathed_env = p->GetType();
1282 if (mathed_env == LM_OT_MIN) {
1283 if (fragile) os << "\\protect";
1284 os << "\\( "; // changed from " \\( " (Albrecht Dress)
1286 if (mathed_env == LM_OT_PAR){
1290 << latex_mathenv[mathed_env]
1292 if (is_multicolumn(mathed_env)) {
1293 if (mathed_env != LM_OT_ALIGNAT
1294 && mathed_env != LM_OT_ALIGNATN)
1296 os << "{" << p->GetColumns()/2 << "}";
1300 ++number_of_newlines;
1303 if (!label.empty() && label[0] > ' '
1304 && is_singlely_numbered(mathed_env)) {
1308 ++number_of_newlines;
1311 p->Write(os, fragile);
1313 if (mathed_env == LM_OT_MIN){
1314 if (fragile) os << "\\protect";
1316 } else if (mathed_env == LM_OT_PAR) {
1318 ++number_of_newlines;
1321 << latex_mathenv[mathed_env]
1323 number_of_newlines += 2;
1325 return number_of_newlines;
1331 /* FIXME: math-greek-toggle seems to work OK, but math-greek doesn't turn
1333 bool math_insert_greek(BufferView * bv, char c)
1335 if (bv->available() &&
1336 (('A' <= c && c <= 'Z') ||
1337 ('a'<= c && c<= 'z'))) {
1340 if (!bv->theLockingInset() || bv->theLockingInset()->IsTextInset()) {
1341 int greek_kb_flag_save = greek_kb_flag;
1342 InsetFormula * new_inset = new InsetFormula();
1343 bv->beforeChange(bv->text);
1344 if (!bv->insertInset(new_inset)) {
1349 new_inset->Edit(bv, 0, 0, 0);
1350 new_inset->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
1351 if (greek_kb_flag_save < 2) {
1352 bv->unlockInset(new_inset); // bv->theLockingInset());
1353 bv->text->CursorRight(bv, true);
1356 if (bv->theLockingInset()->LyxCode() == Inset::MATH_CODE ||
1357 bv->theLockingInset()->LyxCode() == Inset::MATHMACRO_CODE)
1358 static_cast<InsetFormula*>(bv->theLockingInset())->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
1360 lyxerr << "Math error: attempt to write on a wrong "
1361 "class of inset." << endl;