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"
46 extern char const * latex_special_chars;
48 int greek_kb_flag = 0;
49 extern char const * latex_mathenv[];
50 LyXFont * Math_Fonts = 0;
51 MathCursor * mathcursor = 0;
62 void mathed_init_fonts();
64 string nicelabel(string const & label)
66 return label.empty() ? string("(#)") : "(" + label + ")";
73 LyXFont WhichFont(short type, int size)
110 case LM_TC_SPECIAL: //f = Math_Fonts[0]; break;
123 if (type == LM_TC_BSYM) {
136 case LM_ST_SCRIPTSCRIPT:
142 lyxerr << "Math Error: wrong font size: " << size << endl;
146 if (type != LM_TC_TEXTRM)
147 f.setColor(LColor::math);
155 void mathed_init_fonts()
157 Math_Fonts = new LyXFont[8]; //DEC cxx cannot initialize all fonts
160 for (int i = 0 ; i < 8 ; ++i) {
161 Math_Fonts[i] = LyXFont(LyXFont::ALL_SANE);
164 Math_Fonts[0].setShape(LyXFont::ITALIC_SHAPE);
166 Math_Fonts[1].setFamily(LyXFont::SYMBOL_FAMILY);
168 Math_Fonts[2].setFamily(LyXFont::SYMBOL_FAMILY);
169 Math_Fonts[2].setShape(LyXFont::ITALIC_SHAPE);
171 Math_Fonts[3].setSeries(LyXFont::BOLD_SERIES);
173 Math_Fonts[4].setFamily(LyXFont::SANS_FAMILY);
174 Math_Fonts[4].setShape(LyXFont::ITALIC_SHAPE);
176 Math_Fonts[5].setFamily(LyXFont::TYPEWRITER_FAMILY);
178 Math_Fonts[6].setFamily(LyXFont::ROMAN_FAMILY);
180 Math_Fonts[7].setFamily(LyXFont::SANS_FAMILY);
184 // returns the nearest enclosing matrix
185 MathArrayInset * matrixpar(int & idx)
189 static_cast<MathArrayInset *>
190 (mathcursor ? mathcursor->enclosing(LM_OT_MATRIX, idx) : 0);
197 InsetFormulaBase::InsetFormulaBase(MathInset * par)
201 InsetFormulaBase::InsetFormulaBase(InsetFormulaBase const & f)
202 : UpdatableInset(f), par_(static_cast<MathInset *>(f.par_->Clone()))
205 InsetFormulaBase::~InsetFormulaBase()
208 #warning leak this for a while...
214 void InsetFormulaBase::Write(Buffer const * buf, ostream & os) const
217 Latex(buf, os, false, false);
221 int InsetFormulaBase::Ascii(Buffer const *, ostream & os, int) const
223 par_->Write(os, false);
228 int InsetFormulaBase::Linuxdoc(Buffer const * buf, ostream & os) const
230 return Ascii(buf, os, 0);
234 int InsetFormulaBase::DocBook(Buffer const * buf, ostream & os) const
236 return Ascii(buf, os, 0);
240 // Check if uses AMS macros
241 void InsetFormulaBase::Validate(LaTeXFeatures &) const
245 string const InsetFormulaBase::EditMessage() const
247 return _("Math editor mode");
251 void InsetFormulaBase::Edit(BufferView * bv, int x, int /*y*/, unsigned int)
253 mathcursor = new MathCursor(this);
255 if (!bv->lockInset(this))
256 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
258 par_->Metrics(LM_ST_TEXT);
259 bv->updateInset(this, false);
271 void InsetFormulaBase::InsetUnlock(BufferView * bv)
274 if (mathcursor->InMacroMode()) {
275 mathcursor->MacroModeClose();
281 bv->updateInset(this, false);
285 void InsetFormulaBase::GetCursorPos(BufferView *, int & x, int & y) const
287 mathcursor->GetPos(x, y);
293 void InsetFormulaBase::ToggleInsetCursor(BufferView * bv)
298 if (isCursorVisible())
299 bv->hideLockedInsetCursor();
303 mathcursor->GetPos(x, y);
307 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
308 int const asc = lyxfont::maxAscent(font);
309 int const desc = lyxfont::maxDescent(font);
311 bv->showLockedInsetCursor(x, y, asc, desc);
314 toggleCursorVisible();
318 void InsetFormulaBase::ShowInsetCursor(BufferView * bv, bool)
320 if (!isCursorVisible()) {
324 mathcursor->GetPos(x, y);
327 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
328 int const asc = lyxfont::maxAscent(font);
329 int const desc = lyxfont::maxDescent(font);
330 bv->fitLockedInsetCursor(x, y, asc, desc);
332 ToggleInsetCursor(bv);
337 void InsetFormulaBase::HideInsetCursor(BufferView * bv)
339 if (isCursorVisible())
340 ToggleInsetCursor(bv);
344 void InsetFormulaBase::ToggleInsetSelection(BufferView * bv)
349 bv->updateInset(this, false);
353 vector<string> const InsetFormulaBase::getLabelList() const
355 return std::vector<string>();
359 void InsetFormulaBase::UpdateLocal(BufferView * bv)
361 par_->Metrics(LM_ST_TEXT);
362 bv->updateInset(this, true);
366 void InsetFormulaBase::InsetButtonRelease(BufferView * bv,
367 int x, int y, int /*button*/)
373 mathcursor->SetPos(x, y);
379 bv->updateInset(this, false);
385 void InsetFormulaBase::InsetButtonPress(BufferView * bv,
386 int x, int y, int /*button*/)
391 if (mathcursor && mathcursor->Selection()) {
392 mathcursor->SelClear();
393 bv->updateInset(this, false);
398 void InsetFormulaBase::InsetMotionNotify(BufferView * bv,
399 int x, int y, int /*button*/)
401 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
404 mathcursor->SetPos(sel_x + par_->xo(), sel_y + par_->yo());
405 mathcursor->SelStart();
407 mathcursor->GetPos(sel_x, sel_y);
408 } else if (sel_flag) {
412 mathcursor->SetPos(x, y);
414 mathcursor->GetPos(x, y);
415 if (sel_x != x || sel_y != y)
416 bv->updateInset(this, false);
423 void InsetFormulaBase::InsetKeyPress(XKeyEvent *)
425 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
429 UpdatableInset::RESULT
430 InsetFormulaBase::LocalDispatch(BufferView * bv, kb_action action,
433 //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
434 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
435 // extern char *dispatch_result;
440 MathTextCodes varcode = LM_TC_MIN;
441 bool was_macro = mathcursor->InMacroMode();
443 bool space_on = false;
444 bool was_selection = mathcursor->Selection();
445 RESULT result = DISPATCHED;
446 static MathSpaceInset * sp = 0;
450 if (mathcursor->getLastCode() == LM_TC_TEX)
453 mathcursor->normalize();
457 // --- Cursor Movements ---------------------------------------------
460 sel = true; // fall through...
463 result = DISPATCH_RESULT(mathcursor->Right(sel));
469 sel = true; // fall through
472 result = DISPATCH_RESULT(mathcursor->Left(sel));
481 result = DISPATCH_RESULT(mathcursor->Up(sel));
490 result = DISPATCH_RESULT(mathcursor->Down(sel));
497 result = DISPATCHED_NOUPDATE;
502 result = DISPATCHED_NOUPDATE;
505 case LFUN_DELETE_LINE_FORWARD:
506 bv->lockedInsetStoreUndo(Undo::DELETE);
507 mathcursor->DelLine();
512 bv->lockedInsetStoreUndo(Undo::INSERT);
513 mathcursor->idxRight();
518 bv->lockedInsetStoreUndo(Undo::INSERT);
519 mathcursor->idxRight();
524 if (!mathcursor->InMacroMode() && mathcursor->pos() == 0) {
525 bv->lockedInsetStoreUndo(Undo::DELETE);
526 mathcursor->pullArg();
527 bv->updateInset(this, true);
530 if (!mathcursor->Left())
535 bv->lockedInsetStoreUndo(Undo::DELETE);
536 mathcursor->Delete();
537 bv->updateInset(this, true);
541 // sprintf(dispatch_buffer, "%d %d",);
542 // dispatch_result = dispatch_buffer;
546 lyxerr << "LFUN_SETXY broken!\n";
548 istringstream is(arg.c_str());
550 lyxerr << "LFUN_SETXY: x: " << x << " y: " << y << "\n";
552 mathcursor->SetPos(x1 + x, y1 + y);
556 // cursor selection ----------------------------
560 mathcursor->MacroModeClose();
561 bv->lockedInsetStoreUndo(Undo::INSERT);
562 mathcursor->SelPaste();
567 bv->lockedInsetStoreUndo(Undo::DELETE);
568 mathcursor->SelCut();
573 mathcursor->SelCopy();
578 case LFUN_WORDRIGHTSEL:
579 case LFUN_WORDLEFTSEL:
582 // --- accented characters ------------------------------
584 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
585 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
586 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
587 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
588 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
589 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
590 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
591 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
592 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
593 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
597 if (!greek_kb_flag) {
599 bv->owner()->message(_("Math greek mode on"));
605 case LFUN_GREEK_TOGGLE:
606 greek_kb_flag = greek_kb_flag ? 0 : 2;
608 bv->owner()->message(_("Math greek keyboard on"));
610 bv->owner()->message(_("Math greek keyboard off"));
614 case LFUN_BOLD: mathcursor->toggleLastCode(LM_TC_BF); break;
615 case LFUN_SANS: mathcursor->toggleLastCode(LM_TC_SF); break;
616 case LFUN_EMPH: mathcursor->toggleLastCode(LM_TC_CAL); break;
617 case LFUN_ROMAN: mathcursor->toggleLastCode(LM_TC_RM); break;
618 case LFUN_CODE: mathcursor->toggleLastCode(LM_TC_TT); break;
619 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR); break;
623 #warning This needs a fix.
624 // Can we use the ERT inset here? (Lgb)
627 // varcode = LM_TC_TEX;
628 mathcursor->setLastCode(LM_TC_TEX);
629 bv->owner()->message(_("TeX mode"));
632 case LFUN_MATH_LIMITS:
633 bv->lockedInsetStoreUndo(Undo::INSERT);
634 if (mathcursor->toggleLimits())
640 bv->lockedInsetStoreUndo(Undo::INSERT);
641 latexkeys const * l = in_word_set(arg);
642 mathcursor->SetSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
647 case LFUN_INSERT_MATH:
649 bv->lockedInsetStoreUndo(Undo::INSERT);
650 mathcursor->Interpret(arg);
655 case LFUN_INSERT_MATRIX:
657 bv->lockedInsetStoreUndo(Undo::INSERT);
660 string v_align, h_align;
661 istringstream is(arg.c_str());
662 is >> m >> n >> v_align >> h_align;
663 MathArrayInset * p = new MathArrayInset(m, n);
664 p->valign(v_align[0]);
666 mathcursor->insert(p);
671 case LFUN_MATH_DELIM:
673 bv->lockedInsetStoreUndo(Undo::INSERT);
676 static const string vdelim("(){}[]./|");
677 lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
682 istringstream is(arg.c_str());
685 lyxerr << "formulabase::LFUN_MATH_DELIM, lt: '" << lt << "'\n";
686 lyxerr << "formulabase::LFUN_MATH_DELIM, rt: '" << rt << "'\n";
689 latexkeys const * l = in_word_set(lt);
692 } else if (vdelim.find(lt[0]) != string::npos)
696 latexkeys const * l = in_word_set(rt);
699 } else if (vdelim.find(rt[0]) != string::npos)
702 if (mathcursor->selection) {
703 MathDelimInset * p = new MathDelimInset(ilt, irt);
705 mathcursor->selArray(ar);
706 lyxerr << "selarray: " << ar << "\n";
708 mathcursor->insert(p);
710 mathcursor->insert(new MathDelimInset(ilt, irt));
716 case LFUN_PROTECTEDSPACE:
717 bv->lockedInsetStoreUndo(Undo::INSERT);
718 mathcursor->insert(new MathSpaceInset(1));
723 // Invalid actions under math mode
725 if (mathcursor->getLastCode() != LM_TC_TEXTRM) {
726 bv->owner()->message(_("math text mode"));
727 varcode = LM_TC_TEXTRM;
730 mathcursor->setLastCode(varcode);
734 bv->owner()->message(_("Invalid action in math mode!"));
738 case LFUN_MATH_HALIGN:
740 bv->lockedInsetStoreUndo(Undo::INSERT);
741 lyxerr << "handling halign '" << arg << "'\n";
743 MathArrayInset * p = matrixpar(idx);
746 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
751 case LFUN_MATH_VALIGN:
753 bv->lockedInsetStoreUndo(Undo::INSERT);
754 lyxerr << "handling valign '" << arg << "'\n";
756 MathArrayInset * p = matrixpar(idx);
759 p->valign(arg.size() ? arg[0] : 'c');
764 case LFUN_MATH_ROW_INSERT:
766 bv->lockedInsetStoreUndo(Undo::INSERT);
768 MathArrayInset * p = matrixpar(idx);
769 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
772 p->addRow(p->row(idx));
777 case LFUN_MATH_ROW_DELETE:
779 bv->lockedInsetStoreUndo(Undo::INSERT);
781 MathArrayInset * p = matrixpar(idx);
782 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
785 p->delRow(p->row(idx));
790 case LFUN_MATH_COLUMN_INSERT:
792 bv->lockedInsetStoreUndo(Undo::INSERT);
794 MathArrayInset * p = matrixpar(idx);
797 p->addCol(p->col(idx));
802 case LFUN_MATH_COLUMN_DELETE:
804 bv->lockedInsetStoreUndo(Undo::INSERT);
806 MathArrayInset * p = matrixpar(idx);
809 p->delCol(p->col(idx));
814 case LFUN_EXEC_COMMAND:
815 result = UNDISPATCHED;
819 if ((action == -1 || action == LFUN_SELFINSERT) && !arg.empty()) {
820 unsigned char c = arg[0];
821 bv->lockedInsetStoreUndo(Undo::INSERT);
823 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
825 mathcursor->setAccent(0);
828 if (c == 0) { // Dead key, do nothing
829 //lyxerr << "deadkey" << endl;
834 if (mathcursor->getLastCode() == LM_TC_TEX) {
835 mathcursor->MacroModeOpen();
836 mathcursor->clearLastCode();
838 } else if (!varcode) {
839 short f = mathcursor->getLastCode() ?
840 mathcursor->getLastCode() :
841 mathcursor->nextCode();
842 varcode = MathIsAlphaFont(f) ?
843 static_cast<MathTextCodes>(f) :
847 // lyxerr << "Varcode << vardoce;
848 MathTextCodes char_code = varcode;
851 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
852 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
853 'Y', 0, 0, 0, 0 , 'Z' };
855 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
856 char_code = LM_TC_RM;
859 char_code = LM_TC_SYMB;
862 mathcursor->insert(c, char_code);
864 if (greek_kb_flag && char_code == LM_TC_RM )
865 mathcursor->setLastCode(LM_TC_VAR);
869 if (greek_kb_flag < 2)
872 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
873 mathcursor->insert(c, LM_TC_TEX);
875 mathcursor->insert('}', LM_TC_TEX);
878 mathcursor->clearLastCode();
879 // varcode = LM_TC_MIN;
880 } else if (c == '_' && varcode == LM_TC_TEX) {
881 mathcursor->insert(c, LM_TC_SPECIAL);
882 mathcursor->clearLastCode();
883 // varcode = LM_TC_MIN;
884 } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
885 mathcursor->MacroModeOpen();
886 mathcursor->clearLastCode();
887 mathcursor->insert(c, LM_TC_MIN);
888 } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
889 MathTextCodes code = mathcursor->getLastCode();
890 if (code != LM_TC_TEXTRM)
892 mathcursor->insert(c, code);
893 } else if (strchr("+/-*<>=", c)) {
894 MathTextCodes code = mathcursor->getLastCode();
895 if (code != LM_TC_TEXTRM)
897 mathcursor->insert(c, code);
898 } else if (strchr(latex_special_chars, c) && c!= '_') {
899 MathTextCodes code = mathcursor->getLastCode();
900 if (code != LM_TC_TEXTRM)
901 code = LM_TC_SPECIAL;
902 mathcursor->insert(c, code);
903 } else if (c == '_' || c == '^') {
907 mathcursor->Interpret(s);
908 } else if (c == ' ') {
910 short f = (mathcursor->getLastCode()) ?
911 mathcursor->getLastCode() :
912 mathcursor->nextCode();
913 varcode = MathIsAlphaFont(f) ?
914 static_cast<MathTextCodes>(f) :
918 if (varcode == LM_TC_TEXTRM) {
919 mathcursor->insert(c, LM_TC_TEXTRM);
920 } else if (was_macro) {
921 mathcursor->MacroModeClose();
923 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
927 if (!mathcursor->pop())
929 mathcursor->plainRight();
931 } else if (c == '\'' || c == '@') {
932 mathcursor->insert (c, LM_TC_VAR);
933 } else if (c == '\\') {
935 mathcursor->MacroModeClose();
936 bv->owner()->message(_("TeX mode"));
937 mathcursor->setLastCode(LM_TC_TEX);
940 } else if (action == LFUN_MATH_PANEL) {
941 result = UNDISPATCHED;
943 lyxerr << "Closed by action " << action << endl;
949 mathcursor->normalize();
951 if (mathcursor && was_macro != mathcursor->InMacroMode()
953 && action != LFUN_BACKSPACE)
962 if (mathcursor && (mathcursor->Selection() || was_selection))
963 ToggleInsetSelection(bv);
965 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
966 result == UNDISPATCHED)
969 bv->unlockInset(this);
971 return result; // original version
976 /* FIXME: math-greek-toggle seems to work OK, but math-greek doesn't turn
978 bool math_insert_greek(BufferView * bv, char c)
980 if (!bv->available())
988 if (!bv->theLockingInset() || bv->theLockingInset()->IsTextInset()) {
989 int greek_kb_flag_save = greek_kb_flag;
990 InsetFormula * new_inset = new InsetFormula();
991 bv->beforeChange(bv->text);
992 if (!bv->insertInset(new_inset)) {
997 new_inset->Edit(bv, 0, 0, 0);
998 new_inset->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
999 if (greek_kb_flag_save < 2) {
1000 bv->unlockInset(new_inset); // bv->theLockingInset());
1001 bv->text->cursorRight(bv, true);
1004 if (bv->theLockingInset()->LyxCode() == Inset::MATH_CODE ||
1005 bv->theLockingInset()->LyxCode() == Inset::MATHMACRO_CODE)
1006 static_cast<InsetFormula*>(bv->theLockingInset())->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
1008 lyxerr << "Math error: attempt to write on a wrong "
1009 "class of inset." << endl;
1015 Inset::Code InsetFormulaBase::LyxCode() const
1017 return Inset::MATH_CODE;
1021 LyXFont const InsetFormulaBase::ConvertFont(LyXFont const & f) const
1023 // We have already discussed what was here
1026 font.setLatex(LyXFont::OFF);
1031 MathInset * InsetFormulaBase::par() const