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.
19 #include "support/LAssert.h"
22 #pragma implementation
26 #include "formulamacro.h"
27 #include "commandtags.h"
28 #include "math_cursor.h"
29 #include "math_parser.h"
30 #include "BufferView.h"
34 #include "LaTeXFeatures.h"
37 #include "support/lstrings.h"
41 #include "math_arrayinset.h"
42 #include "math_spaceinset.h"
43 #include "math_macrotable.h"
44 #include "math_factory.h"
45 #include "support/lyxlib.h"
46 #include "undo_funcs.h"
52 extern char const * latex_mathenv[];
53 MathCursor * mathcursor = 0;
64 void handleFont(BufferView * bv, MathTextCodes t)
66 if (mathcursor->selection())
67 bv->lockedInsetStoreUndo(Undo::EDIT);
68 mathcursor->handleFont(t);
72 void handleAccent(BufferView * bv, string const & name)
74 bv->lockedInsetStoreUndo(Undo::EDIT);
75 mathcursor->insert(createMathInset(name));
79 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
81 LyXText * lt = bv->getLyXText();
85 if (!bv->insertInset(new_inset)) {
89 new_inset->edit(bv, 0, 0, 0);
94 // returns the nearest enclosing grid
95 MathArrayInset * matrixpar(MathInset::idx_type & idx)
98 return (mathcursor ? mathcursor->enclosingArray(idx) : 0);
106 InsetFormulaBase::InsetFormulaBase()
109 // This is needed as long the math parser is not re-entrant
110 MathMacroTable::builtinMacros();
111 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
115 // Check if uses AMS macros
116 void InsetFormulaBase::validate(LaTeXFeatures &) const
120 void InsetFormulaBase::metrics(BufferView * bv, LyXFont const * f) const
126 MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
131 string const InsetFormulaBase::editMessage() const
133 return _("Math editor mode");
137 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
139 if (!bv->lockInset(this))
140 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
142 mathcursor = new MathCursor(this, x == 0);
144 // if that is removed, we won't get the magenta box when entering an
145 // inset for the first time
146 bv->updateInset(this, false);
153 void InsetFormulaBase::edit(BufferView * bv, bool front)
155 // looks hackish but seems to work
156 edit(bv, front ? 0 : 1, 0, 0);
160 void InsetFormulaBase::insetUnlock(BufferView * bv)
163 if (mathcursor->inMacroMode()) {
164 mathcursor->macroModeClose();
165 updateLocal(bv, true);
170 bv->updateInset(this, false);
174 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
176 mathcursor->getPos(x, y);
183 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
188 if (isCursorVisible())
189 bv->hideLockedInsetCursor();
193 mathcursor->getPos(x, y);
199 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
200 bv->showLockedInsetCursor(x, y, asc, des);
203 toggleCursorVisible();
207 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
209 if (!isCursorVisible()) {
213 mathcursor->getPos(x, y);
218 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
219 bv->fitLockedInsetCursor(x, y, asc, des);
221 toggleInsetCursor(bv);
226 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
228 if (isCursorVisible())
229 toggleInsetCursor(bv);
233 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
236 bv->updateInset(this, false);
240 vector<string> const InsetFormulaBase::getLabelList() const
242 return std::vector<string>();
246 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
249 bv->updateInset(this, dirty);
253 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
254 int x, int y, int /*button*/)
260 mathcursor->setPos(x, y);
267 bv->updateInset(this, false);
272 void InsetFormulaBase::insetButtonPress(BufferView * bv,
273 int x, int y, int /*button*/)
278 if (mathcursor && mathcursor->selection()) {
279 mathcursor->selClear();
280 bv->updateInset(this, false);
285 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
286 int x, int y, int /*button*/)
288 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
291 mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo());
292 mathcursor->selStart();
294 mathcursor->getPos(sel_x, sel_y);
295 } else if (sel_flag) {
299 mathcursor->setPos(x, y);
301 mathcursor->getPos(x, y);
302 if (sel_x != x || sel_y != y)
303 bv->updateInset(this, false);
310 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
312 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
316 UpdatableInset::RESULT
317 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
320 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
321 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
326 RESULT result = DISPATCHED;
328 bool was_macro = mathcursor->inMacroMode();
329 bool was_selection = mathcursor->selection();
333 mathcursor->normalize();
337 // --- Cursor Movements ---------------------------------------------
340 sel = true; // fall through...
343 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
344 updateLocal(bv, false);
349 sel = true; // fall through
352 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
353 updateLocal(bv, false);
361 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
362 updateLocal(bv, false);
370 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
371 updateLocal(bv, false);
378 mathcursor->home(sel);
379 updateLocal(bv, false);
386 mathcursor->end(sel);
387 updateLocal(bv, false);
390 case LFUN_DELETE_LINE_FORWARD:
391 bv->lockedInsetStoreUndo(Undo::DELETE);
392 mathcursor->delLine();
393 updateLocal(bv, true);
397 mathcursor->idxNext();
398 updateLocal(bv, false);
402 mathcursor->idxPrev();
403 updateLocal(bv, false);
407 bv->lockedInsetStoreUndo(Undo::EDIT);
408 mathcursor->splitCell();
409 updateLocal(bv, true);
413 bv->lockedInsetStoreUndo(Undo::DELETE);
414 mathcursor->backspace();
415 bv->updateInset(this, true);
419 bv->lockedInsetStoreUndo(Undo::DELETE);
421 bv->updateInset(this, true);
425 // sprintf(dispatch_buffer, "%d %d",);
426 // dispatch_result = dispatch_buffer;
429 lyxerr << "LFUN_SETXY broken!\n";
434 istringstream is(arg.c_str());
436 par()->getXY(x1, y1);
437 mathcursor->setPos(x1 + x, y1 + y);
438 updateLocal(bv, false);
444 mathcursor->macroModeClose();
445 bv->lockedInsetStoreUndo(Undo::INSERT);
446 mathcursor->selPaste();
447 updateLocal(bv, true);
451 bv->lockedInsetStoreUndo(Undo::DELETE);
452 mathcursor->selCut();
453 updateLocal(bv, true);
457 mathcursor->selCopy();
460 case LFUN_WORDRIGHTSEL:
461 case LFUN_WORDLEFTSEL:
464 // --- accented characters ------------------------------
466 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
467 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
468 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
469 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
470 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
471 case LFUN_MACRON: handleAccent(bv, "bar"); break;
472 case LFUN_DOT: handleAccent(bv, "dot"); break;
473 case LFUN_CARON: handleAccent(bv, "check"); break;
474 case LFUN_BREVE: handleAccent(bv, "breve"); break;
475 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
478 case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
479 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
480 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
481 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
482 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
483 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
484 case LFUN_NOUN: handleFont(bv, LM_TC_BB); break;
485 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
488 handleFont(bv, LM_TC_GREEK1);
490 mathcursor->interpret(arg[0]);
494 handleFont(bv, LM_TC_TEXTRM);
495 //bv->owner()->message(_("math text mode toggled"));
498 case LFUN_MATH_LIMITS:
499 bv->lockedInsetStoreUndo(Undo::INSERT);
500 if (mathcursor->toggleLimits())
501 updateLocal(bv, true);
506 bv->lockedInsetStoreUndo(Undo::INSERT);
507 latexkeys const * l = in_word_set(arg);
508 mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
509 updateLocal(bv, true);
513 case LFUN_INSERT_MATRIX:
515 bv->lockedInsetStoreUndo(Undo::INSERT);
516 mathcursor->interpret("matrix " + arg);
517 updateLocal(bv, true);
521 case LFUN_MATH_SPACE:
523 bv->lockedInsetStoreUndo(Undo::EDIT);
524 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
525 updateLocal(bv, true);
529 case LFUN_MATH_DELIM:
531 bv->lockedInsetStoreUndo(Undo::INSERT);
532 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
535 istringstream is(arg.c_str());
538 lyxerr << "can't parse delimeters from '" << arg << "'\n";
541 bv->lockedInsetStoreUndo(Undo::EDIT);
542 mathcursor->handleDelim(ls, rs);
543 updateLocal(bv, true);
547 case LFUN_PROTECTEDSPACE:
548 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
549 bv->lockedInsetStoreUndo(Undo::INSERT);
550 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
551 updateLocal(bv, true);
555 bv->owner()->message(_("Invalid action in math mode!"));
559 case LFUN_MATH_HALIGN:
561 bv->lockedInsetStoreUndo(Undo::INSERT);
562 lyxerr << "handling halign '" << arg << "'\n";
563 MathInset::idx_type idx;
564 MathArrayInset * p = matrixpar(idx);
567 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
568 updateLocal(bv, true);
572 case LFUN_MATH_VALIGN:
574 bv->lockedInsetStoreUndo(Undo::INSERT);
575 lyxerr << "handling valign '" << arg << "'\n";
576 MathInset::idx_type idx;
577 MathArrayInset * p = matrixpar(idx);
580 p->valign(arg.size() ? arg[0] : 'c');
581 updateLocal(bv, true);
585 case LFUN_MATH_ROW_INSERT:
587 bv->lockedInsetStoreUndo(Undo::INSERT);
588 MathInset::idx_type idx;
589 MathArrayInset * p = matrixpar(idx);
590 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
593 p->addRow(p->row(idx));
594 updateLocal(bv, true);
598 case LFUN_MATH_ROW_DELETE:
600 bv->lockedInsetStoreUndo(Undo::INSERT);
601 MathInset::idx_type idx;
602 MathArrayInset * p = matrixpar(idx);
603 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
606 p->delRow(p->row(idx));
607 updateLocal(bv, true);
611 case LFUN_MATH_COLUMN_INSERT:
613 bv->lockedInsetStoreUndo(Undo::INSERT);
614 MathInset::idx_type idx;
615 MathArrayInset * p = matrixpar(idx);
618 p->addCol(p->col(idx));
619 updateLocal(bv, true);
623 case LFUN_MATH_COLUMN_DELETE:
625 bv->lockedInsetStoreUndo(Undo::INSERT);
626 MathInset::idx_type idx;
627 MathArrayInset * p = matrixpar(idx);
630 p->delCol(p->col(idx));
631 updateLocal(bv, true);
635 case LFUN_EXEC_COMMAND:
636 result = UNDISPATCHED;
640 case LFUN_INSERT_MATH:
641 case LFUN_SELFINSERT:
643 bv->lockedInsetStoreUndo(Undo::INSERT);
644 mathcursor->interpret(arg);
645 updateLocal(bv, true);
649 case LFUN_MATH_PANEL:
650 result = UNDISPATCHED;
654 result = UNDISPATCHED;
657 lyx::Assert(mathcursor);
658 //mathcursor->normalize();
660 if (//was_macro != mathcursor->inMacroMode() &&
661 action >= 0 && action != LFUN_BACKSPACE)
662 updateLocal(bv, true);
664 if (mathcursor->selection() || was_selection)
665 toggleInsetSelection(bv);
667 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
668 result == UNDISPATCHED)
671 bv->unlockInset(this);
673 return result; // original version
677 Inset::Code InsetFormulaBase::lyxCode() const
679 return Inset::MATH_CODE;
683 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
685 if (bv->available()) {
686 // use selection if available..
688 //if (action == LFUN_MATH_IMPORT_SELECTION)
692 string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
694 InsetFormulaBase * f;
696 f = new InsetFormula;
697 if (openNewInset(bv, f)) {
698 // don't do that also for LFUN_MATH_MODE unless you want end up with
699 // always changing to mathrm when opening an inlined inset
700 // -- I really hate "LyXfunc overloading"...
702 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
703 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
706 // create a macro if we see "\\newcommand" somewhere, and an ordinary
708 if (sel.find("\\newcommand") == string::npos)
709 f = new InsetFormula(sel);
711 f = new InsetFormulaMacro(sel);
712 bv->getLyXText()->cutSelection(bv);
716 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
720 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
722 mathDispatchCreation(bv, arg, true);
726 void mathDispatchMathMode(BufferView * bv, string const & arg)
728 mathDispatchCreation(bv, arg, false);
732 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
734 mathDispatchCreation(bv, arg, true);
738 void mathDispatchMathMacro(BufferView * bv, string const & arg)
740 if (bv->available()) {
742 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
745 string const s1 = token(s, ' ', 1);
746 int const na = s1.empty() ? 0 : lyx::atoi(s1);
747 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
753 void mathDispatchMathDelim(BufferView * bv, string const & arg)
755 if (bv->available()) {
756 if (openNewInset(bv, new InsetFormula))
757 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
762 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
764 if (bv->available()) {
765 if (openNewInset(bv, new InsetFormula))
766 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
771 void mathDispatchInsertMath(BufferView * bv, string const & arg)
773 if (bv->available()) {
774 if (arg.size() && arg[0] == '\\') {
775 InsetFormula * f = new InsetFormula(arg);
776 if (!bv->insertInset(f))
779 mathDispatchMathMode(bv, arg);
784 void mathDispatchGreek(BufferView * bv, string const & arg)
786 if (bv->available()) {
787 InsetFormula * f = new InsetFormula;
788 if (openNewInset(bv, f)) {
789 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);