3 * Purpose: Implementation of common parts of the LyX math insets
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
7 * Copyright: 1996-1998 Alejandro Aguilar Sierra
9 * Version: 0.4, Lyx project.
11 * You are free to use and modify this code under the terms of
12 * the GNU General Public Licence version 2 or later.
21 #pragma implementation
25 #include "formulamacro.h"
26 #include "commandtags.h"
27 #include "math_cursor.h"
28 #include "math_parser.h"
29 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
36 #include "support/lstrings.h"
40 #include "math_arrayinset.h"
41 #include "math_spaceinset.h"
42 #include "math_macrotable.h"
43 #include "math_factory.h"
44 #include "support/lyxlib.h"
45 #include "undo_funcs.h"
51 extern char const * latex_mathenv[];
52 MathCursor * mathcursor = 0;
63 void mathed_init_fonts();
65 string nicelabel(string const & label)
67 return "(" + (label.empty() ? "#" : label) + ")";
70 void handleFont(BufferView * bv, MathTextCodes t)
72 if (mathcursor->selection())
73 bv->lockedInsetStoreUndo(Undo::EDIT);
74 mathcursor->handleFont(t);
78 void handleAccent(BufferView * bv, string const & name)
80 bv->lockedInsetStoreUndo(Undo::EDIT);
81 mathcursor->insert(createMathInset(name));
85 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
87 LyXText * lt = bv->getLyXText();
91 if (!bv->insertInset(new_inset)) {
95 new_inset->edit(bv, 0, 0, 0);
100 // returns the nearest enclosing grid
101 MathArrayInset * matrixpar(int & idx)
104 return (mathcursor ? mathcursor->enclosingArray(idx) : 0);
112 InsetFormulaBase::InsetFormulaBase()
114 // This is needed as long the math parser is not re-entrant
115 MathMacroTable::builtinMacros();
116 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
120 void InsetFormulaBase::read(Buffer const *, LyXLex & lex)
126 void InsetFormulaBase::write(Buffer const *, ostream & os) const
132 int InsetFormulaBase::latex(Buffer const *, ostream & os,
133 bool fragile, bool spacing) const
135 return latex(os, fragile, spacing);
139 int InsetFormulaBase::ascii(Buffer const *, ostream & os, int spacing) const
141 return ascii(os, spacing);
145 int InsetFormulaBase::linuxdoc(Buffer const *, ostream & os) const
151 int InsetFormulaBase::docbook(Buffer const *, ostream & os) const
158 // Check if uses AMS macros
159 void InsetFormulaBase::validate(LaTeXFeatures &) const
163 string const InsetFormulaBase::editMessage() const
165 return _("Math editor mode");
169 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
171 mathcursor = new MathCursor(this);
173 if (!bv->lockInset(this))
174 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
177 // if that is removed, we won't get the magenta box when entering an
178 // inset for the first time
179 bv->updateInset(this, false);
190 void InsetFormulaBase::edit(BufferView * bv, bool front)
192 // looks hackish but seems to work
193 edit(bv, front ? 0 : 1, 0, 0);
197 void InsetFormulaBase::insetUnlock(BufferView * bv)
200 if (mathcursor->inMacroMode()) {
201 mathcursor->macroModeClose();
202 updateLocal(bv, true);
207 bv->updateInset(this, false);
211 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
213 mathcursor->getPos(x, y);
220 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
225 if (isCursorVisible())
226 bv->hideLockedInsetCursor();
230 mathcursor->getPos(x, y);
236 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
237 bv->showLockedInsetCursor(x, y, asc, des);
240 toggleCursorVisible();
244 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
246 if (!isCursorVisible()) {
250 mathcursor->getPos(x, y);
255 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
256 bv->fitLockedInsetCursor(x, y, asc, des);
258 toggleInsetCursor(bv);
263 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
265 if (isCursorVisible())
266 toggleInsetCursor(bv);
270 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
273 bv->updateInset(this, false);
277 vector<string> const InsetFormulaBase::getLabelList() const
279 return std::vector<string>();
283 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
286 bv->updateInset(this, dirty);
290 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
291 int x, int y, int /*button*/)
297 mathcursor->setPos(x, y);
304 bv->updateInset(this, false);
309 void InsetFormulaBase::insetButtonPress(BufferView * bv,
310 int x, int y, int /*button*/)
315 if (mathcursor && mathcursor->selection()) {
316 mathcursor->selClear();
317 bv->updateInset(this, false);
322 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
323 int x, int y, int /*button*/)
325 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
328 mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo());
329 mathcursor->selStart();
331 mathcursor->getPos(sel_x, sel_y);
332 } else if (sel_flag) {
336 mathcursor->setPos(x, y);
338 mathcursor->getPos(x, y);
339 if (sel_x != x || sel_y != y)
340 bv->updateInset(this, false);
347 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
349 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
353 int greek_kb_flag = 0;
355 UpdatableInset::RESULT
356 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
359 //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
360 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
366 RESULT result = DISPATCHED;
368 bool was_macro = mathcursor->inMacroMode();
369 bool was_selection = mathcursor->selection();
373 mathcursor->normalize();
377 // --- Cursor Movements ---------------------------------------------
380 sel = true; // fall through...
383 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
384 updateLocal(bv, false);
389 sel = true; // fall through
392 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
393 updateLocal(bv, false);
401 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
402 updateLocal(bv, false);
410 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
411 updateLocal(bv, false);
418 mathcursor->home(sel);
419 updateLocal(bv, false);
426 mathcursor->end(sel);
427 updateLocal(bv, false);
430 case LFUN_DELETE_LINE_FORWARD:
431 bv->lockedInsetStoreUndo(Undo::DELETE);
432 mathcursor->delLine();
433 updateLocal(bv, true);
437 mathcursor->idxNext();
438 updateLocal(bv, false);
442 mathcursor->idxPrev();
443 updateLocal(bv, false);
447 bv->lockedInsetStoreUndo(Undo::EDIT);
448 mathcursor->splitCell();
449 updateLocal(bv, true);
453 bv->lockedInsetStoreUndo(Undo::DELETE);
454 mathcursor->backspace();
455 bv->updateInset(this, true);
459 bv->lockedInsetStoreUndo(Undo::DELETE);
461 bv->updateInset(this, true);
465 // sprintf(dispatch_buffer, "%d %d",);
466 // dispatch_result = dispatch_buffer;
469 lyxerr << "LFUN_SETXY broken!\n";
474 istringstream is(arg.c_str());
476 par()->getXY(x1, y1);
477 mathcursor->setPos(x1 + x, y1 + y);
478 updateLocal(bv, false);
484 mathcursor->macroModeClose();
485 bv->lockedInsetStoreUndo(Undo::INSERT);
486 mathcursor->selPaste();
487 updateLocal(bv, true);
491 bv->lockedInsetStoreUndo(Undo::DELETE);
492 mathcursor->selCut();
493 updateLocal(bv, true);
497 mathcursor->selCopy();
500 case LFUN_WORDRIGHTSEL:
501 case LFUN_WORDLEFTSEL:
504 // --- accented characters ------------------------------
506 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
507 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
508 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
509 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
510 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
511 case LFUN_MACRON: handleAccent(bv, "bar"); break;
512 case LFUN_DOT: handleAccent(bv, "dot"); break;
513 case LFUN_CARON: handleAccent(bv, "check"); break;
514 case LFUN_BREVE: handleAccent(bv, "breve"); break;
515 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
518 case LFUN_GREEK: handleFont(bv, LM_TC_GREEK1); break;
519 case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
520 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
521 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
522 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
523 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
524 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
525 case LFUN_NOUN: handleFont(bv, LM_TC_BB); break;
526 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
529 handleFont(bv, LM_TC_TEXTRM);
530 //bv->owner()->message(_("math text mode toggled"));
533 case LFUN_MATH_LIMITS:
534 bv->lockedInsetStoreUndo(Undo::INSERT);
535 if (mathcursor->toggleLimits())
536 updateLocal(bv, true);
541 bv->lockedInsetStoreUndo(Undo::INSERT);
542 latexkeys const * l = in_word_set(arg);
543 mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
544 updateLocal(bv, true);
548 case LFUN_INSERT_MATRIX:
550 bv->lockedInsetStoreUndo(Undo::INSERT);
551 mathcursor->interpret("matrix " + arg);
552 updateLocal(bv, true);
556 case LFUN_MATH_SPACE:
558 bv->lockedInsetStoreUndo(Undo::EDIT);
559 //MathSpaceInset * p = mathcursor->prevSpaceInset();
563 // mathcursor->insert(new MathSpaceInset(1));
564 mathcursor->insert(new MathSpaceInset(1));
565 updateLocal(bv, true);
569 case LFUN_MATH_DELIM:
571 bv->lockedInsetStoreUndo(Undo::INSERT);
572 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
575 istringstream is(arg.c_str());
578 lyxerr << "can't parse delimeters from '" << arg << "'\n";
581 bv->lockedInsetStoreUndo(Undo::EDIT);
582 mathcursor->handleDelim(ls, rs);
583 updateLocal(bv, true);
587 case LFUN_PROTECTEDSPACE:
588 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
589 bv->lockedInsetStoreUndo(Undo::INSERT);
590 mathcursor->insert(new MathSpaceInset(1));
591 updateLocal(bv, true);
595 bv->owner()->message(_("Invalid action in math mode!"));
599 case LFUN_MATH_HALIGN:
601 bv->lockedInsetStoreUndo(Undo::INSERT);
602 lyxerr << "handling halign '" << arg << "'\n";
604 MathArrayInset * p = matrixpar(idx);
607 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
608 updateLocal(bv, true);
612 case LFUN_MATH_VALIGN:
614 bv->lockedInsetStoreUndo(Undo::INSERT);
615 lyxerr << "handling valign '" << arg << "'\n";
617 MathArrayInset * p = matrixpar(idx);
620 p->valign(arg.size() ? arg[0] : 'c');
621 updateLocal(bv, true);
625 case LFUN_MATH_ROW_INSERT:
627 bv->lockedInsetStoreUndo(Undo::INSERT);
629 MathArrayInset * p = matrixpar(idx);
630 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
633 p->addRow(p->row(idx));
634 updateLocal(bv, true);
638 case LFUN_MATH_ROW_DELETE:
640 bv->lockedInsetStoreUndo(Undo::INSERT);
642 MathArrayInset * p = matrixpar(idx);
643 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
646 p->delRow(p->row(idx));
647 updateLocal(bv, true);
651 case LFUN_MATH_COLUMN_INSERT:
653 bv->lockedInsetStoreUndo(Undo::INSERT);
655 MathArrayInset * p = matrixpar(idx);
658 p->addCol(p->col(idx));
659 updateLocal(bv, true);
663 case LFUN_MATH_COLUMN_DELETE:
665 bv->lockedInsetStoreUndo(Undo::INSERT);
667 MathArrayInset * p = matrixpar(idx);
670 p->delCol(p->col(idx));
671 updateLocal(bv, true);
675 case LFUN_EXEC_COMMAND:
676 result = UNDISPATCHED;
680 case LFUN_INSERT_MATH:
681 case LFUN_SELFINSERT:
683 bv->lockedInsetStoreUndo(Undo::INSERT);
684 mathcursor->interpret(arg);
685 updateLocal(bv, true);
689 case LFUN_MATH_PANEL:
690 result = UNDISPATCHED;
694 result = UNDISPATCHED;
697 //mathcursor->normalize();
699 if (was_macro != mathcursor->inMacroMode()
700 && action >= 0 && action != LFUN_BACKSPACE)
701 updateLocal(bv, true);
703 if (mathcursor->selection() || was_selection)
704 toggleInsetSelection(bv);
706 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
707 result == UNDISPATCHED)
710 bv->unlockInset(this);
712 return result; // original version
716 Inset::Code InsetFormulaBase::lyxCode() const
718 return Inset::MATH_CODE;
722 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
724 if (bv->available()) {
725 // use selection if available..
727 //if (action == LFUN_MATH_IMPORT_SELECTION)
731 string sel = bv->getLyXText()->selectionAsString(bv->buffer(),
734 InsetFormulaBase * f;
736 f = new InsetFormula;
737 if (openNewInset(bv, f)) {
738 // don't do that also for LFUN_MATH_MODE unless you want end up with
739 // always changing to mathrm when opening an inlined inset
740 // -- I really hate "LyXfunc overloading"...
742 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
743 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
746 // create a macro if we see "\\newcommand" somewhere, and an ordinary
748 if (sel.find("\\newcommand") == string::npos)
749 f = new InsetFormula(sel);
751 f = new InsetFormulaMacro(sel);
752 bv->getLyXText()->cutSelection(bv);
756 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
760 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
762 mathDispatchCreation(bv, arg, true);
766 void mathDispatchMathMode(BufferView * bv, string const & arg)
768 mathDispatchCreation(bv, arg, false);
772 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
774 mathDispatchCreation(bv, arg, true);
778 void mathDispatchMathMacro(BufferView * bv, string const & arg)
780 if (bv->available()) {
782 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
785 string const s1 = token(s, ' ', 1);
786 int const na = s1.empty() ? 0 : lyx::atoi(s1);
787 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
793 void mathDispatchMathDelim(BufferView * bv, string const & arg)
795 if (bv->available()) {
796 if (openNewInset(bv, new InsetFormula))
797 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
802 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
804 if (bv->available()) {
805 if (openNewInset(bv, new InsetFormula))
806 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
811 void mathDispatchInsertMath(BufferView * bv, string const & arg)
813 if (bv->available()) {
814 if (arg.size() && arg[0] == '\\')
815 openNewInset(bv, new InsetFormula(arg));
817 mathDispatchMathMode(bv, arg);
822 void mathDispatchGreek(BufferView * bv, string const & arg)
824 if (bv->available()) {
825 InsetFormula * f = new InsetFormula;
826 if (openNewInset(bv, f)) {
827 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, string());
828 bv->theLockingInset()->localDispatch(bv, LFUN_SELFINSERT, arg);