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);
287 void InsetFormulaBase::GetCursorPos(BufferView *, int & x, int & y) const
289 mathcursor->GetPos(x, y);
295 void InsetFormulaBase::ToggleInsetCursor(BufferView * bv)
300 if (isCursorVisible())
301 bv->hideLockedInsetCursor();
305 mathcursor->GetPos(x, y);
309 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
310 int const asc = lyxfont::maxAscent(font);
311 int const desc = lyxfont::maxDescent(font);
313 bv->showLockedInsetCursor(x, y, asc, desc);
316 toggleCursorVisible();
320 void InsetFormulaBase::ShowInsetCursor(BufferView * bv, bool)
322 if (!isCursorVisible()) {
326 mathcursor->GetPos(x, y);
329 LyXFont font = WhichFont(LM_TC_TEXTRM, LM_ST_TEXT);
330 int const asc = lyxfont::maxAscent(font);
331 int const desc = lyxfont::maxDescent(font);
332 bv->fitLockedInsetCursor(x, y, asc, desc);
334 ToggleInsetCursor(bv);
339 void InsetFormulaBase::HideInsetCursor(BufferView * bv)
341 if (isCursorVisible())
342 ToggleInsetCursor(bv);
346 void InsetFormulaBase::ToggleInsetSelection(BufferView * bv)
351 bv->updateInset(this, false);
356 vector<string> const InsetFormulaBase::getLabelList() const
358 return std::vector<string>();
362 void InsetFormulaBase::UpdateLocal(BufferView * bv)
364 par_->Metrics(LM_ST_TEXT);
365 bv->updateInset(this, true);
369 void InsetFormulaBase::InsetButtonRelease(BufferView * bv,
370 int x, int y, int /*button*/)
376 mathcursor->SetPos(x, y);
382 bv->updateInset(this, false);
388 void InsetFormulaBase::InsetButtonPress(BufferView * bv,
389 int x, int y, int /*button*/)
394 if (mathcursor && mathcursor->Selection()) {
395 mathcursor->SelClear();
396 bv->updateInset(this, false);
401 void InsetFormulaBase::InsetMotionNotify(BufferView * bv,
402 int x, int y, int /*button*/)
404 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
407 mathcursor->SetPos(sel_x + par_->xo(), sel_y + par_->yo());
408 mathcursor->SelStart();
410 mathcursor->GetPos(sel_x, sel_y);
411 } else if (sel_flag) {
415 mathcursor->SetPos(x, y);
417 mathcursor->GetPos(x, y);
418 if (sel_x != x || sel_y != y)
419 bv->updateInset(this, false);
426 void InsetFormulaBase::InsetKeyPress(XKeyEvent *)
428 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
432 UpdatableInset::RESULT
433 InsetFormulaBase::LocalDispatch(BufferView * bv, kb_action action,
436 //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
437 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
438 // extern char *dispatch_result;
443 MathTextCodes varcode = LM_TC_MIN;
444 bool was_macro = mathcursor->InMacroMode();
446 bool space_on = false;
447 bool was_selection = mathcursor->Selection();
448 RESULT result = DISPATCHED;
449 static MathSpaceInset * sp = 0;
453 if (mathcursor->getLastCode() == LM_TC_TEX)
456 mathcursor->normalize();
460 // --- Cursor Movements ---------------------------------------------
463 sel = true; // fall through...
466 result = DISPATCH_RESULT(mathcursor->Right(sel));
472 sel = true; // fall through
475 result = DISPATCH_RESULT(mathcursor->Left(sel));
484 result = DISPATCH_RESULT(mathcursor->Up(sel));
493 result = DISPATCH_RESULT(mathcursor->Down(sel));
500 result = DISPATCHED_NOUPDATE;
505 result = DISPATCHED_NOUPDATE;
508 case LFUN_DELETE_LINE_FORWARD:
509 bv->lockedInsetStoreUndo(Undo::DELETE);
510 mathcursor->DelLine();
515 bv->lockedInsetStoreUndo(Undo::INSERT);
516 mathcursor->idxRight();
521 bv->lockedInsetStoreUndo(Undo::INSERT);
522 mathcursor->idxRight();
527 if (!mathcursor->InMacroMode() && mathcursor->pos() == 0) {
528 bv->lockedInsetStoreUndo(Undo::DELETE);
529 mathcursor->pullArg();
530 bv->updateInset(this, true);
533 if (!mathcursor->Left())
539 bv->lockedInsetStoreUndo(Undo::DELETE);
540 mathcursor->Delete();
541 bv->updateInset(this, true);
545 // sprintf(dispatch_buffer, "%d %d",);
546 // dispatch_result = dispatch_buffer;
550 lyxerr << "LFUN_SETXY broken!\n";
552 istringstream is(arg.c_str());
554 lyxerr << "LFUN_SETXY: x: " << x << " y: " << y << "\n";
556 mathcursor->SetPos(x1 + x, y1 + y);
560 // cursor selection ----------------------------
564 mathcursor->MacroModeClose();
565 bv->lockedInsetStoreUndo(Undo::INSERT);
566 mathcursor->SelPaste();
571 bv->lockedInsetStoreUndo(Undo::DELETE);
572 mathcursor->SelCut();
577 mathcursor->SelCopy();
582 case LFUN_WORDRIGHTSEL:
583 case LFUN_WORDLEFTSEL:
586 // --- accented characters ------------------------------
588 case LFUN_UMLAUT: mathcursor->setAccent(LM_ddot); break;
589 case LFUN_CIRCUMFLEX: mathcursor->setAccent(LM_hat); break;
590 case LFUN_GRAVE: mathcursor->setAccent(LM_grave); break;
591 case LFUN_ACUTE: mathcursor->setAccent(LM_acute); break;
592 case LFUN_TILDE: mathcursor->setAccent(LM_tilde); break;
593 case LFUN_MACRON: mathcursor->setAccent(LM_bar); break;
594 case LFUN_DOT: mathcursor->setAccent(LM_dot); break;
595 case LFUN_CARON: mathcursor->setAccent(LM_check); break;
596 case LFUN_BREVE: mathcursor->setAccent(LM_breve); break;
597 case LFUN_VECTOR: mathcursor->setAccent(LM_vec); break;
601 if (!greek_kb_flag) {
603 bv->owner()->message(_("Math greek mode on"));
609 case LFUN_GREEK_TOGGLE:
610 greek_kb_flag = greek_kb_flag ? 0 : 2;
612 bv->owner()->message(_("Math greek keyboard on"));
614 bv->owner()->message(_("Math greek keyboard off"));
618 case LFUN_BOLD: mathcursor->toggleLastCode(LM_TC_BF); break;
619 case LFUN_SANS: mathcursor->toggleLastCode(LM_TC_SF); break;
620 case LFUN_EMPH: mathcursor->toggleLastCode(LM_TC_CAL); break;
621 case LFUN_ROMAN: mathcursor->toggleLastCode(LM_TC_RM); break;
622 case LFUN_CODE: mathcursor->toggleLastCode(LM_TC_TT); break;
623 case LFUN_DEFAULT: mathcursor->setLastCode(LM_TC_VAR); break;
626 // varcode = LM_TC_TEX;
627 mathcursor->setLastCode(LM_TC_TEX);
628 bv->owner()->message(_("TeX mode"));
631 case LFUN_MATH_LIMITS:
632 bv->lockedInsetStoreUndo(Undo::INSERT);
633 if (mathcursor->toggleLimits())
639 bv->lockedInsetStoreUndo(Undo::INSERT);
640 latexkeys const * l = in_word_set(arg);
641 mathcursor->SetSize(MathStyles(l ? l->id : -1));
646 case LFUN_INSERT_MATH:
648 bv->lockedInsetStoreUndo(Undo::INSERT);
649 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 //------- dummy actions
821 #warning Is this needed here? Shouldnt the main dispatch handle this? (Lgb)
823 //case LFUN_EXEC_COMMAND:
824 //bv->owner()->getMiniBuffer()->PrepareForCommand();
828 if ((action == -1 || action == LFUN_SELFINSERT)
830 unsigned char c = arg[0];
831 bv->lockedInsetStoreUndo(Undo::INSERT);
833 if (c == ' ' && mathcursor->getAccent() == LM_hat) {
835 mathcursor->setAccent(0);
838 if (c == 0) { // Dead key, do nothing
839 //lyxerr << "deadkey" << endl;
844 if (mathcursor->getLastCode() == LM_TC_TEX) {
845 mathcursor->MacroModeOpen();
846 mathcursor->clearLastCode();
848 } else if (!varcode) {
849 short f = mathcursor->getLastCode() ?
850 mathcursor->getLastCode() :
851 mathcursor->nextCode();
852 varcode = MathIsAlphaFont(f) ?
853 static_cast<MathTextCodes>(f) :
857 // lyxerr << "Varcode << vardoce;
858 MathTextCodes char_code = varcode;
861 {'A', 'B', 'X', 0 , 'E', 0 , 0 , 'H', 'I', 0 ,
862 'K', 0 , 'M', 'N', 'O', 0 , 0 , 'P', 0 , 'T',
863 'Y', 0, 0, 0, 0 , 'Z' };
865 if ('A' <= c && c <= 'Z' && greek[c - 'A']) {
866 char_code = LM_TC_RM;
869 char_code = LM_TC_SYMB;
872 mathcursor->insert(c, char_code);
874 if (greek_kb_flag && char_code == LM_TC_RM )
875 mathcursor->setLastCode(LM_TC_VAR);
879 if (greek_kb_flag < 2)
882 } else if (strchr("!,:;{}", c) && (varcode == LM_TC_TEX||was_macro)) {
883 mathcursor->insert(c, LM_TC_TEX);
885 mathcursor->insert('}', LM_TC_TEX);
888 mathcursor->clearLastCode();
889 // varcode = LM_TC_MIN;
890 } else if (c == '_' && varcode == LM_TC_TEX) {
891 mathcursor->insert(c, LM_TC_SPECIAL);
892 mathcursor->clearLastCode();
893 // varcode = LM_TC_MIN;
894 } else if ('0' <= c && c <= '9' && (varcode == LM_TC_TEX||was_macro)) {
895 mathcursor->MacroModeOpen();
896 mathcursor->clearLastCode();
897 mathcursor->insert(c, LM_TC_MIN);
898 } else if (('0' <= c && c <= '9') || strchr(";:!|[]().,?", c)) {
899 mathcursor->insert(c, LM_TC_CONST);
900 } else if (strchr("+/-*<>=", c)) {
901 mathcursor->insert(c, LM_TC_BOP);
902 } else if (strchr(latex_special_chars, c) && c!= '_') {
903 mathcursor->insert(c, LM_TC_SPECIAL);
904 } else if (c == '_' || c == '^') {
908 mathcursor->Interpret(s);
909 } else if (c == ' ') {
911 short f = (mathcursor->getLastCode()) ?
912 mathcursor->getLastCode() :
913 mathcursor->nextCode();
914 varcode = MathIsAlphaFont(f) ?
915 static_cast<MathTextCodes>(f) :
919 if (varcode == LM_TC_TEXTRM) {
920 mathcursor->insert(c, LM_TC_TEXTRM);
921 } else if (was_macro) {
922 mathcursor->MacroModeClose();
924 int isp = (sp->GetSpace()<5) ? sp->GetSpace()+1: 0;
928 lyxerr << "look here!\n";
929 //if (!mathcursor->cursor.pop() && !mathcursor->cursor.OK())
932 } else if (c == '\'' || c == '@') {
933 mathcursor->insert (c, LM_TC_VAR);
934 } else if (c == '\\') {
936 mathcursor->MacroModeClose();
937 bv->owner()->message(_("TeX mode"));
938 mathcursor->setLastCode(LM_TC_TEX);
941 } else if (action == LFUN_MATH_PANEL) {
942 result = UNDISPATCHED;
944 lyxerr << "Closed by action " << action << endl;
950 mathcursor->normalize();
952 if (mathcursor && was_macro != mathcursor->InMacroMode()
954 && action != LFUN_BACKSPACE)
963 if (mathcursor && (mathcursor->Selection() || was_selection))
964 ToggleInsetSelection(bv);
966 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
967 result == UNDISPATCHED)
970 bv->unlockInset(this);
972 return result; // original version
977 /* FIXME: math-greek-toggle seems to work OK, but math-greek doesn't turn
979 bool math_insert_greek(BufferView * bv, char c)
981 if (!bv->available())
989 if (!bv->theLockingInset() || bv->theLockingInset()->IsTextInset()) {
990 int greek_kb_flag_save = greek_kb_flag;
991 InsetFormula * new_inset = new InsetFormula();
992 bv->beforeChange(bv->text);
993 if (!bv->insertInset(new_inset)) {
998 new_inset->Edit(bv, 0, 0, 0);
999 new_inset->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
1000 if (greek_kb_flag_save < 2) {
1001 bv->unlockInset(new_inset); // bv->theLockingInset());
1002 #warning someone broke this in bolzano
1003 //bv->text->cursorRight(bv, true);
1006 if (bv->theLockingInset()->LyxCode() == Inset::MATH_CODE ||
1007 bv->theLockingInset()->LyxCode() == Inset::MATHMACRO_CODE)
1008 static_cast<InsetFormula*>(bv->theLockingInset())->LocalDispatch(bv, LFUN_SELFINSERT, tmp);
1010 lyxerr << "Math error: attempt to write on a wrong "
1011 "class of inset." << endl;
1017 Inset::Code InsetFormulaBase::LyxCode() const
1019 return Inset::MATH_CODE;
1023 LyXFont const InsetFormulaBase::ConvertFont(LyXFont const & f) const
1025 // We have already discussed what was here
1027 font.setLatex(LyXFont::OFF);
1031 MathInset * InsetFormulaBase::par() const