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"
29 #include "BufferView.h"
32 #include "LaTeXFeatures.h"
34 #include "support/LOstream.h"
38 #include "math_arrayinset.h"
39 #include "math_spaceinset.h"
40 #include "math_deliminset.h"
41 #include "support/lyxlib.h"
42 #include "mathed/support.h"
48 extern char const * latex_special_chars;
50 int greek_kb_flag = 0;
51 extern char const * latex_mathenv[];
52 LyXFont * Math_Fonts = 0;
53 MathCursor * mathcursor = 0;
64 void mathed_init_fonts();
66 string nicelabel(string const & label)
68 return label.empty() ? string("(#)") : "(" + label + ")";
75 LyXFont WhichFont(short type, int size)
112 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
125 if (type == LM_TC_BSYM) {
138 case LM_ST_SCRIPTSCRIPT:
144 lyxerr << "Math Error: wrong font size: " << size << endl;
148 if (type != LM_TC_TEXTRM)
149 f.setColor(LColor::math);
157 void mathed_init_fonts()
159 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
162 for (int i = 0 ; i < 8 ; ++i) {
163 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
166 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
168 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
170 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
171 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
173 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
175 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
176 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
178 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
180 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
182 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
186 // returns the nearest enclosing matrix
187 MathArrayInset * matrixpar(int & idx)
191 static_cast<MathArrayInset *>
192 (mathcursor ? mathcursor->enclosing(LM_OT_MATRIX, idx) : 0);
199 InsetFormulaBase::InsetFormulaBase(MathInset * par)
204 InsetFormulaBase::InsetFormulaBase(InsetFormulaBase const & f)
205 : UpdatableInset(f), par_(static_cast<MathInset *>(f.par_->clone()))
209 InsetFormulaBase::~InsetFormulaBase()
212 #warning leak this for a while...
218 void InsetFormulaBase::write(Buffer const * buf, ostream & os) const
221 latex(buf, os, false, false);
225 int InsetFormulaBase::ascii(Buffer const *, ostream & os, int) const
227 par_->Write(os, false);
232 int InsetFormulaBase::linuxdoc(Buffer const * buf, ostream & os) const
234 return ascii(buf, os, 0);
238 int InsetFormulaBase::docBook(Buffer const * buf, ostream & os) const
240 return ascii(buf, os, 0);
244 // Check if uses AMS macros
245 void InsetFormulaBase::validate(LaTeXFeatures &) const
249 string const InsetFormulaBase::editMessage() const
251 return _("Math editor mode");
255 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
257 mathcursor = new MathCursor(this);
259 if (!bv->lockInset(this))
260 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
262 par_->Metrics(LM_ST_TEXT);
263 bv->updateInset(this, false);
275 void InsetFormulaBase::insetUnlock(BufferView * bv)
278 if (mathcursor->InMacroMode()) {
279 mathcursor->MacroModeClose();
285 bv->updateInset(this, false);
289 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
291 mathcursor->GetPos(x, y);
297 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
302 if (isCursorVisible())
303 bv->hideLockedInsetCursor();
307 mathcursor->GetPos(x, y);
311 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
312 int const asc = lyxfont::maxAscent(font);
313 int const desc = lyxfont::maxDescent(font);
315 bv->showLockedInsetCursor(x, y, asc, desc);
318 toggleCursorVisible();
322 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
324 if (!isCursorVisible()) {
328 mathcursor->GetPos(x, y);
331 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
332 int const asc = lyxfont::maxAscent(font);
333 int const desc = lyxfont::maxDescent(font);
334 bv->fitLockedInsetCursor(x, y, asc, desc);
336 toggleInsetCursor(bv);
341 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
343 if (isCursorVisible())
344 toggleInsetCursor(bv);
348 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
353 bv->updateInset(this, false);
357 vector<string> const InsetFormulaBase::getLabelList() const
359 return std::vector<string>();
363 void InsetFormulaBase::updateLocal(BufferView * bv)
365 par_->Metrics(LM_ST_TEXT);
366 bv->updateInset(this, true);
370 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
371 int x, int y, int /*button*/)
377 mathcursor->SetPos(x, y);
383 bv->updateInset(this, false);
389 void InsetFormulaBase::insetButtonPress(BufferView * bv,
390 int x, int y, int /*button*/)
395 if (mathcursor && mathcursor->Selection()) {
396 mathcursor->SelClear();
397 bv->updateInset(this, false);
402 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
403 int x, int y, int /*button*/)
405 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
408 mathcursor->SetPos(sel_x + par_->xo(), sel_y + par_->yo());
409 mathcursor->SelStart();
411 mathcursor->GetPos(sel_x, sel_y);
412 } else if (sel_flag) {
416 mathcursor->SetPos(x, y);
418 mathcursor->GetPos(x, y);
419 if (sel_x != x || sel_y != y)
420 bv->updateInset(this, false);
427 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
429 lyxerr[Debug::MATHED]
430 << "Used InsetFormulaBase::InsetKeyPress." << endl;
434 UpdatableInset::RESULT
435 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
438 //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
439 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
440 // extern char *dispatch_result;
445 MathTextCodes varcode = LM_TC_MIN;
446 bool was_macro = mathcursor->InMacroMode();
448 bool space_on = false;
449 bool was_selection = mathcursor->Selection();
450 RESULT result = DISPATCHED;
451 static MathSpaceInset * sp = 0;
455 if (mathcursor->getLastCode() == LM_TC_TEX)
458 mathcursor->normalize();
462 // --- Cursor Movements ---------------------------------------------
465 sel = true; // fall through...
468 result = DISPATCH_RESULT(mathcursor->Right(sel));
474 sel = true; // fall through
477 result = DISPATCH_RESULT(mathcursor->Left(sel));
486 result = DISPATCH_RESULT(mathcursor->Up(sel));
495 result = DISPATCH_RESULT(mathcursor->Down(sel));
502 result = DISPATCHED_NOUPDATE;
507 result = DISPATCHED_NOUPDATE;
510 case LFUN_DELETE_LINE_FORWARD:
511 bv->lockedInsetStoreUndo(Undo::DELETE);
512 mathcursor->DelLine();
517 bv->lockedInsetStoreUndo(Undo::INSERT);
518 mathcursor->idxRight();
523 bv->lockedInsetStoreUndo(Undo::INSERT);
524 mathcursor->idxRight();
529 if (!mathcursor->InMacroMode() && mathcursor->pos() == 0) {
530 bv->lockedInsetStoreUndo(Undo::DELETE);
531 mathcursor->pullArg();
532 bv->updateInset(this, true);
535 if (!mathcursor->Left())
540 bv->lockedInsetStoreUndo(Undo::DELETE);
541 mathcursor->Delete();
542 bv->updateInset(this, true);
546 // sprintf(dispatch_buffer, "%d %d",);
547 // dispatch_result = dispatch_buffer;
551 lyxerr << "LFUN_SETXY broken!\n";
553 istringstream is(arg.c_str());
555 lyxerr << "LFUN_SETXY: x: " << x << " y: " << y << "\n";
557 mathcursor->SetPos(x1 + x, y1 + y);
561 // cursor selection ----------------------------
565 mathcursor->MacroModeClose();
566 bv->lockedInsetStoreUndo(Undo::INSERT);
567 mathcursor->SelPaste();
572 bv->lockedInsetStoreUndo(Undo::DELETE);
573 mathcursor->SelCut();
578 mathcursor->SelCopy();
583 case LFUN_WORDRIGHTSEL:
584 case LFUN_WORDLEFTSEL:
587 // --- accented characters ------------------------------
589 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
590 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
591 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
592 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
593 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
594 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
595 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
596 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
597 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
598 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
602 if (!greek_kb_flag) {
604 bv->owner()->message(_("Math greek mode on"));
610 case LFUN_GREEK_TOGGLE:
611 greek_kb_flag = greek_kb_flag ? 0 : 2;
613 bv->owner()->message(_("Math greek keyboard on"));
615 bv->owner()->message(_("Math greek keyboard off"));
619 case LFUN_BOLD: mathcursor->toggleLastCode(LM_TC_BF); break;
620 case LFUN_SANS: mathcursor->toggleLastCode(LM_TC_SF); break;
621 case LFUN_EMPH: mathcursor->toggleLastCode(LM_TC_CAL); break;
622 case LFUN_ROMAN: mathcursor->toggleLastCode(LM_TC_RM); break;
623 case LFUN_CODE: mathcursor->toggleLastCode(LM_TC_TT); break;
624 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR); break;
628 #warning This needs a fix.
629 // Can we use the ERT inset here? (Lgb)
632 // varcode = LM_TC_TEX;
633 mathcursor->setLastCode(LM_TC_TEX);
634 bv->owner()->message(_("TeX mode"));
637 case LFUN_MATH_LIMITS:
638 bv->lockedInsetStoreUndo(Undo::INSERT);
639 if (mathcursor->toggleLimits())
645 bv->lockedInsetStoreUndo(Undo::INSERT);
646 latexkeys const * l = in_word_set(arg);
647 mathcursor->SetSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
652 case LFUN_INSERT_MATH:
654 bv->lockedInsetStoreUndo(Undo::INSERT);
655 mathcursor->Interpret(arg);
660 case LFUN_INSERT_MATRIX:
662 bv->lockedInsetStoreUndo(Undo::INSERT);
665 string v_align, h_align;
666 istringstream is(arg.c_str());
667 is >> m >> n >> v_align >> h_align;
668 MathArrayInset * p = new MathArrayInset(m, n);
669 p->valign(v_align[0]);
671 mathcursor->insert(p);
676 case LFUN_MATH_DELIM:
678 bv->lockedInsetStoreUndo(Undo::INSERT);
681 static const string vdelim("(){}[]./|");
682 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
687 istringstream is(arg.c_str());
690 lyxerr << "formulabase::LFUN_MATH_DELIM, lt: '" << lt << "'\n";
691 lyxerr << "formulabase::LFUN_MATH_DELIM, rt: '" << rt << "'\n";
694 latexkeys const * l = in_word_set(lt);
697 } else if (vdelim.find(lt[0]) != string::npos)
701 latexkeys const * l = in_word_set(rt);
704 } else if (vdelim.find(rt[0]) != string::npos)
707 if (mathcursor->selection) {
708 MathDelimInset * p = new MathDelimInset(ilt, irt);
710 mathcursor->selArray(ar);
711 lyxerr << "selarray: " << ar << "\n";
713 mathcursor->insert(p);
715 mathcursor->insert(new MathDelimInset(ilt, irt));
721 case LFUN_PROTECTEDSPACE:
722 bv->lockedInsetStoreUndo(Undo::INSERT);
723 mathcursor->insert(new MathSpaceInset(1));
728 // Invalid actions under math mode
730 if (mathcursor->getLastCode() != LM_TC_TEXTRM) {
731 bv->owner()->message(_("math text mode"));
732 varcode = LM_TC_TEXTRM;
735 mathcursor->setLastCode(varcode);
739 bv->owner()->message(_("Invalid action in math mode!"));
743 case LFUN_MATH_HALIGN:
745 bv->lockedInsetStoreUndo(Undo::INSERT);
746 lyxerr << "handling halign '" << arg << "'\n";
748 MathArrayInset * p = matrixpar(idx);
751 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
756 case LFUN_MATH_VALIGN:
758 bv->lockedInsetStoreUndo(Undo::INSERT);
759 lyxerr << "handling valign '" << arg << "'\n";
761 MathArrayInset * p = matrixpar(idx);
764 p->valign(arg.size() ? arg[0] : 'c');
769 case LFUN_MATH_ROW_INSERT:
771 bv->lockedInsetStoreUndo(Undo::INSERT);
773 MathArrayInset * p = matrixpar(idx);
774 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
777 p->addRow(p->row(idx));
782 case LFUN_MATH_ROW_DELETE:
784 bv->lockedInsetStoreUndo(Undo::INSERT);
786 MathArrayInset * p = matrixpar(idx);
787 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
790 p->delRow(p->row(idx));
795 case LFUN_MATH_COLUMN_INSERT:
797 bv->lockedInsetStoreUndo(Undo::INSERT);
799 MathArrayInset * p = matrixpar(idx);
802 p->addCol(p->col(idx));
807 case LFUN_MATH_COLUMN_DELETE:
809 bv->lockedInsetStoreUndo(Undo::INSERT);
811 MathArrayInset * p = matrixpar(idx);
814 p->delCol(p->col(idx));
819 case LFUN_EXEC_COMMAND:
820 result = UNDISPATCHED;
824 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
825 unsigned char c = arg[0];
826 bv->lockedInsetStoreUndo(Undo::INSERT);
828 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
830 mathcursor->setAccent(0);
833 if (c == 0) { // Dead key, do nothing
834 //lyxerr << "deadkey" << endl;
839 if (mathcursor->getLastCode() == LM_TC_TEX) {
840 mathcursor->MacroModeOpen();
841 mathcursor->clearLastCode();
843 } else if (!varcode) {
844 short f = mathcursor->getLastCode() ?
845 mathcursor->getLastCode() :
846 mathcursor->nextCode();
847 varcode = MathIsAlphaFont(f) ?
848 static_cast<MathTextCodes>(f) :
852 // lyxerr << "Varcode << vardoce;
853 MathTextCodes char_code = varcode;
856 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
857 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
858 'Y', 0, 0, 0, 0 , 'Z' };
860 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
861 char_code = LM_TC_RM;
864 char_code = LM_TC_SYMB;
867 mathcursor->insert(c, char_code);
869 if (greek_kb_flag && char_code == LM_TC_RM )
870 mathcursor->setLastCode(LM_TC_VAR);
874 if (greek_kb_flag < 2)
877 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
878 mathcursor->insert(c, LM_TC_TEX);
880 mathcursor->insert('}', LM_TC_TEX);
883 mathcursor->clearLastCode();
884 // varcode = LM_TC_MIN;
885 } else if (c == '_' && varcode == LM_TC_TEX) {
886 mathcursor->insert(c, LM_TC_SPECIAL);
887 mathcursor->clearLastCode();
888 // varcode = LM_TC_MIN;
889 } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
890 mathcursor->MacroModeOpen();
891 mathcursor->clearLastCode();
892 mathcursor->insert(c, LM_TC_MIN);
893 } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
894 MathTextCodes code = mathcursor->getLastCode();
895 if (code != LM_TC_TEXTRM)
897 mathcursor->insert(c, code);
898 } else if (strchr("+/-*<>=", c)) {
899 MathTextCodes code = mathcursor->getLastCode();
900 if (code != LM_TC_TEXTRM)
902 mathcursor->insert(c, code);
903 } else if (strchr(latex_special_chars, c) && c!= '_') {
904 MathTextCodes code = mathcursor->getLastCode();
905 if (code != LM_TC_TEXTRM)
906 code = LM_TC_SPECIAL;
907 mathcursor->insert(c, code);
908 } else if (c == '_' || c == '^') {
912 mathcursor->Interpret(s);
913 } else if (c == ' ') {
915 short f = (mathcursor->getLastCode()) ?
916 mathcursor->getLastCode() :
917 mathcursor->nextCode();
918 varcode = MathIsAlphaFont(f) ?
919 static_cast<MathTextCodes>(f) :
923 if (varcode == LM_TC_TEXTRM) {
924 mathcursor->insert(c, LM_TC_TEXTRM);
925 } else if (was_macro) {
926 mathcursor->MacroModeClose();
928 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
932 if (!mathcursor->pop())
934 mathcursor->plainRight();
936 } else if (c == '\'' || c == '@') {
937 mathcursor->insert (c, LM_TC_VAR);
938 } else if (c == '\\') {
940 mathcursor->MacroModeClose();
941 bv->owner()->message(_("TeX mode"));
942 mathcursor->setLastCode(LM_TC_TEX);
945 } else if (action == LFUN_MATH_PANEL) {
946 result = UNDISPATCHED;
948 lyxerr << "Closed by action " << action << endl;
954 mathcursor->normalize();
956 if (mathcursor && was_macro != mathcursor->InMacroMode()
958 && action != LFUN_BACKSPACE)
967 if (mathcursor && (mathcursor->Selection() || was_selection))
968 toggleInsetSelection(bv);
970 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
971 result == UNDISPATCHED)
974 bv->unlockInset(this);
976 return result; // original version
981 /* FIXME: math-greek-toggle seems to work OK, but math-greek doesn't turn
983 bool math_insert_greek(BufferView * bv, char c)
985 if (!bv->available())
993 if (!bv->theLockingInset() || bv->theLockingInset()->isTextInset()) {
994 int greek_kb_flag_save = greek_kb_flag;
995 InsetFormula * new_inset = new InsetFormula();
996 bv->beforeChange(bv->text);
997 if (!bv->insertInset(new_inset)) {
1002 new_inset->edit(bv, 0, 0, 0);
1003 new_inset->localDispatch(bv, LFUN_SELFINSERT, tmp);
1004 if (greek_kb_flag_save < 2) {
1005 bv->unlockInset(new_inset); // bv->theLockingInset());
1006 bv->text->cursorRight(bv, true);
1009 if (bv->theLockingInset()->lyxCode() == Inset::MATH_CODE ||
1010 bv->theLockingInset()->lyxCode() == Inset::MATHMACRO_CODE)
1011 static_cast<InsetFormula*>(bv->theLockingInset())->localDispatch(bv, LFUN_SELFINSERT, tmp);
1013 lyxerr << "Math error: attempt to write on a wrong "
1014 "class of inset." << endl;
1020 Inset::Code InsetFormulaBase::lyxCode() const
1022 return Inset::MATH_CODE;
1026 LyXFont const InsetFormulaBase::convertFont(LyXFont const & f) const
1028 // We have already discussed what was here
1031 font.setLatex(LyXFont::OFF);
1036 MathInset * InsetFormulaBase::par() const