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 handleFont(BufferView * bv, MathTextCodes t)
65 if (mathcursor->selection())
66 bv->lockedInsetStoreUndo(Undo::EDIT);
67 mathcursor->handleFont(t);
71 void handleAccent(BufferView * bv, string const & name)
73 bv->lockedInsetStoreUndo(Undo::EDIT);
74 mathcursor->insert(createMathInset(name));
78 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
80 LyXText * lt = bv->getLyXText();
84 if (!bv->insertInset(new_inset)) {
88 new_inset->edit(bv, 0, 0, 0);
93 // returns the nearest enclosing grid
94 MathArrayInset * matrixpar(MathInset::idx_type & idx)
97 return (mathcursor ? mathcursor->enclosingArray(idx) : 0);
105 InsetFormulaBase::InsetFormulaBase()
107 // This is needed as long the math parser is not re-entrant
108 MathMacroTable::builtinMacros();
109 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
113 // Check if uses AMS macros
114 void InsetFormulaBase::validate(LaTeXFeatures &) const
118 string const InsetFormulaBase::editMessage() const
120 return _("Math editor mode");
124 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
126 if (!bv->lockInset(this))
127 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
129 mathcursor = new MathCursor(this, x == 0);
131 // if that is removed, we won't get the magenta box when entering an
132 // inset for the first time
133 bv->updateInset(this, false);
140 void InsetFormulaBase::edit(BufferView * bv, bool front)
142 // looks hackish but seems to work
143 edit(bv, front ? 0 : 1, 0, 0);
147 void InsetFormulaBase::insetUnlock(BufferView * bv)
150 if (mathcursor->inMacroMode()) {
151 mathcursor->macroModeClose();
152 updateLocal(bv, true);
157 bv->updateInset(this, false);
161 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
163 mathcursor->getPos(x, y);
170 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
175 if (isCursorVisible())
176 bv->hideLockedInsetCursor();
180 mathcursor->getPos(x, y);
186 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
187 bv->showLockedInsetCursor(x, y, asc, des);
190 toggleCursorVisible();
194 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
196 if (!isCursorVisible()) {
200 mathcursor->getPos(x, y);
205 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
206 bv->fitLockedInsetCursor(x, y, asc, des);
208 toggleInsetCursor(bv);
213 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
215 if (isCursorVisible())
216 toggleInsetCursor(bv);
220 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
223 bv->updateInset(this, false);
227 vector<string> const InsetFormulaBase::getLabelList() const
229 return std::vector<string>();
233 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
236 bv->updateInset(this, dirty);
240 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
241 int x, int y, int /*button*/)
247 mathcursor->setPos(x, y);
254 bv->updateInset(this, false);
259 void InsetFormulaBase::insetButtonPress(BufferView * bv,
260 int x, int y, int /*button*/)
265 if (mathcursor && mathcursor->selection()) {
266 mathcursor->selClear();
267 bv->updateInset(this, false);
272 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
273 int x, int y, int /*button*/)
275 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
278 mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo());
279 mathcursor->selStart();
281 mathcursor->getPos(sel_x, sel_y);
282 } else if (sel_flag) {
286 mathcursor->setPos(x, y);
288 mathcursor->getPos(x, y);
289 if (sel_x != x || sel_y != y)
290 bv->updateInset(this, false);
297 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
299 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
303 UpdatableInset::RESULT
304 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
307 //lyxerr << "InsetFormulaBase::localDispatch: act: " << action
308 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
313 RESULT result = DISPATCHED;
315 bool was_macro = mathcursor->inMacroMode();
316 bool was_selection = mathcursor->selection();
320 mathcursor->normalize();
324 // --- Cursor Movements ---------------------------------------------
327 sel = true; // fall through...
330 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
331 updateLocal(bv, false);
336 sel = true; // fall through
339 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
340 updateLocal(bv, false);
348 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
349 updateLocal(bv, false);
357 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
358 updateLocal(bv, false);
365 mathcursor->home(sel);
366 updateLocal(bv, false);
373 mathcursor->end(sel);
374 updateLocal(bv, false);
377 case LFUN_DELETE_LINE_FORWARD:
378 bv->lockedInsetStoreUndo(Undo::DELETE);
379 mathcursor->delLine();
380 updateLocal(bv, true);
384 mathcursor->idxNext();
385 updateLocal(bv, false);
389 mathcursor->idxPrev();
390 updateLocal(bv, false);
394 bv->lockedInsetStoreUndo(Undo::EDIT);
395 mathcursor->splitCell();
396 updateLocal(bv, true);
400 bv->lockedInsetStoreUndo(Undo::DELETE);
401 mathcursor->backspace();
402 bv->updateInset(this, true);
406 bv->lockedInsetStoreUndo(Undo::DELETE);
408 bv->updateInset(this, true);
412 // sprintf(dispatch_buffer, "%d %d",);
413 // dispatch_result = dispatch_buffer;
416 lyxerr << "LFUN_SETXY broken!\n";
421 istringstream is(arg.c_str());
423 par()->getXY(x1, y1);
424 mathcursor->setPos(x1 + x, y1 + y);
425 updateLocal(bv, false);
431 mathcursor->macroModeClose();
432 bv->lockedInsetStoreUndo(Undo::INSERT);
433 mathcursor->selPaste();
434 updateLocal(bv, true);
438 bv->lockedInsetStoreUndo(Undo::DELETE);
439 mathcursor->selCut();
440 updateLocal(bv, true);
444 mathcursor->selCopy();
447 case LFUN_WORDRIGHTSEL:
448 case LFUN_WORDLEFTSEL:
451 // --- accented characters ------------------------------
453 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
454 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
455 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
456 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
457 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
458 case LFUN_MACRON: handleAccent(bv, "bar"); break;
459 case LFUN_DOT: handleAccent(bv, "dot"); break;
460 case LFUN_CARON: handleAccent(bv, "check"); break;
461 case LFUN_BREVE: handleAccent(bv, "breve"); break;
462 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
465 case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
466 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
467 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
468 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
469 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
470 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
471 case LFUN_NOUN: handleFont(bv, LM_TC_BB); break;
472 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
475 handleFont(bv, LM_TC_GREEK1);
477 mathcursor->interpret(arg[0]);
481 handleFont(bv, LM_TC_TEXTRM);
482 //bv->owner()->message(_("math text mode toggled"));
485 case LFUN_MATH_LIMITS:
486 bv->lockedInsetStoreUndo(Undo::INSERT);
487 if (mathcursor->toggleLimits())
488 updateLocal(bv, true);
493 bv->lockedInsetStoreUndo(Undo::INSERT);
494 latexkeys const * l = in_word_set(arg);
495 mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
496 updateLocal(bv, true);
500 case LFUN_INSERT_MATRIX:
502 bv->lockedInsetStoreUndo(Undo::INSERT);
503 mathcursor->interpret("matrix " + arg);
504 updateLocal(bv, true);
508 case LFUN_MATH_SPACE:
510 bv->lockedInsetStoreUndo(Undo::EDIT);
511 //MathSpaceInset * p = mathcursor->prevSpaceInset();
515 // mathcursor->insert(new MathSpaceInset(1));
516 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
517 updateLocal(bv, true);
521 case LFUN_MATH_DELIM:
523 bv->lockedInsetStoreUndo(Undo::INSERT);
524 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
527 istringstream is(arg.c_str());
530 lyxerr << "can't parse delimeters from '" << arg << "'\n";
533 bv->lockedInsetStoreUndo(Undo::EDIT);
534 mathcursor->handleDelim(ls, rs);
535 updateLocal(bv, true);
539 case LFUN_PROTECTEDSPACE:
540 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
541 bv->lockedInsetStoreUndo(Undo::INSERT);
542 mathcursor->insert(MathAtom(new MathSpaceInset(1)));
543 updateLocal(bv, true);
547 bv->owner()->message(_("Invalid action in math mode!"));
551 case LFUN_MATH_HALIGN:
553 bv->lockedInsetStoreUndo(Undo::INSERT);
554 lyxerr << "handling halign '" << arg << "'\n";
555 MathInset::idx_type idx;
556 MathArrayInset * p = matrixpar(idx);
559 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
560 updateLocal(bv, true);
564 case LFUN_MATH_VALIGN:
566 bv->lockedInsetStoreUndo(Undo::INSERT);
567 lyxerr << "handling valign '" << arg << "'\n";
568 MathInset::idx_type idx;
569 MathArrayInset * p = matrixpar(idx);
572 p->valign(arg.size() ? arg[0] : 'c');
573 updateLocal(bv, true);
577 case LFUN_MATH_ROW_INSERT:
579 bv->lockedInsetStoreUndo(Undo::INSERT);
580 MathInset::idx_type idx;
581 MathArrayInset * p = matrixpar(idx);
582 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
585 p->addRow(p->row(idx));
586 updateLocal(bv, true);
590 case LFUN_MATH_ROW_DELETE:
592 bv->lockedInsetStoreUndo(Undo::INSERT);
593 MathInset::idx_type idx;
594 MathArrayInset * p = matrixpar(idx);
595 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
598 p->delRow(p->row(idx));
599 updateLocal(bv, true);
603 case LFUN_MATH_COLUMN_INSERT:
605 bv->lockedInsetStoreUndo(Undo::INSERT);
606 MathInset::idx_type idx;
607 MathArrayInset * p = matrixpar(idx);
610 p->addCol(p->col(idx));
611 updateLocal(bv, true);
615 case LFUN_MATH_COLUMN_DELETE:
617 bv->lockedInsetStoreUndo(Undo::INSERT);
618 MathInset::idx_type idx;
619 MathArrayInset * p = matrixpar(idx);
622 p->delCol(p->col(idx));
623 updateLocal(bv, true);
627 case LFUN_EXEC_COMMAND:
628 result = UNDISPATCHED;
632 case LFUN_INSERT_MATH:
633 case LFUN_SELFINSERT:
635 bv->lockedInsetStoreUndo(Undo::INSERT);
636 mathcursor->interpret(arg);
637 updateLocal(bv, true);
641 case LFUN_MATH_PANEL:
642 result = UNDISPATCHED;
646 result = UNDISPATCHED;
649 //mathcursor->normalize();
651 if (was_macro != mathcursor->inMacroMode()
652 && action >= 0 && action != LFUN_BACKSPACE)
653 updateLocal(bv, true);
655 if (mathcursor->selection() || was_selection)
656 toggleInsetSelection(bv);
658 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
659 result == UNDISPATCHED)
662 bv->unlockInset(this);
664 return result; // original version
668 Inset::Code InsetFormulaBase::lyxCode() const
670 return Inset::MATH_CODE;
674 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
676 if (bv->available()) {
677 // use selection if available..
679 //if (action == LFUN_MATH_IMPORT_SELECTION)
683 string sel = bv->getLyXText()->selectionAsString(bv->buffer(),
686 InsetFormulaBase * f;
688 f = new InsetFormula;
689 if (openNewInset(bv, f)) {
690 // don't do that also for LFUN_MATH_MODE unless you want end up with
691 // always changing to mathrm when opening an inlined inset
692 // -- I really hate "LyXfunc overloading"...
694 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
695 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
698 // create a macro if we see "\\newcommand" somewhere, and an ordinary
700 if (sel.find("\\newcommand") == string::npos)
701 f = new InsetFormula(sel);
703 f = new InsetFormulaMacro(sel);
704 bv->getLyXText()->cutSelection(bv);
708 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
712 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
714 mathDispatchCreation(bv, arg, true);
718 void mathDispatchMathMode(BufferView * bv, string const & arg)
720 mathDispatchCreation(bv, arg, false);
724 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
726 mathDispatchCreation(bv, arg, true);
730 void mathDispatchMathMacro(BufferView * bv, string const & arg)
732 if (bv->available()) {
734 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
737 string const s1 = token(s, ' ', 1);
738 int const na = s1.empty() ? 0 : lyx::atoi(s1);
739 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
745 void mathDispatchMathDelim(BufferView * bv, string const & arg)
747 if (bv->available()) {
748 if (openNewInset(bv, new InsetFormula))
749 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
754 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
756 if (bv->available()) {
757 if (openNewInset(bv, new InsetFormula))
758 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
763 void mathDispatchInsertMath(BufferView * bv, string const & arg)
765 if (bv->available()) {
766 if (arg.size() && arg[0] == '\\')
767 openNewInset(bv, new InsetFormula(arg));
769 mathDispatchMathMode(bv, arg);
774 void mathDispatchGreek(BufferView * bv, string const & arg)
776 if (bv->available()) {
777 InsetFormula * f = new InsetFormula;
778 if (openNewInset(bv, f)) {
779 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, arg);