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(int & 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 mathcursor = new MathCursor(this);
128 if (!bv->lockInset(this))
129 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
132 // if that is removed, we won't get the magenta box when entering an
133 // inset for the first time
134 bv->updateInset(this, false);
145 void InsetFormulaBase::edit(BufferView * bv, bool front)
147 // looks hackish but seems to work
148 edit(bv, front ? 0 : 1, 0, 0);
152 void InsetFormulaBase::insetUnlock(BufferView * bv)
155 if (mathcursor->inMacroMode()) {
156 mathcursor->macroModeClose();
157 updateLocal(bv, true);
162 bv->updateInset(this, false);
166 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
168 mathcursor->getPos(x, y);
175 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
180 if (isCursorVisible())
181 bv->hideLockedInsetCursor();
185 mathcursor->getPos(x, y);
191 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
192 bv->showLockedInsetCursor(x, y, asc, des);
195 toggleCursorVisible();
199 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
201 if (!isCursorVisible()) {
205 mathcursor->getPos(x, y);
210 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
211 bv->fitLockedInsetCursor(x, y, asc, des);
213 toggleInsetCursor(bv);
218 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
220 if (isCursorVisible())
221 toggleInsetCursor(bv);
225 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
228 bv->updateInset(this, false);
232 vector<string> const InsetFormulaBase::getLabelList() const
234 return std::vector<string>();
238 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
241 bv->updateInset(this, dirty);
245 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
246 int x, int y, int /*button*/)
252 mathcursor->setPos(x, y);
259 bv->updateInset(this, false);
264 void InsetFormulaBase::insetButtonPress(BufferView * bv,
265 int x, int y, int /*button*/)
270 if (mathcursor && mathcursor->selection()) {
271 mathcursor->selClear();
272 bv->updateInset(this, false);
277 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
278 int x, int y, int /*button*/)
280 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
283 mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo());
284 mathcursor->selStart();
286 mathcursor->getPos(sel_x, sel_y);
287 } else if (sel_flag) {
291 mathcursor->setPos(x, y);
293 mathcursor->getPos(x, y);
294 if (sel_x != x || sel_y != y)
295 bv->updateInset(this, false);
302 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
304 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
308 int greek_kb_flag = 0;
310 UpdatableInset::RESULT
311 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
314 //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
315 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
321 RESULT result = DISPATCHED;
323 bool was_macro = mathcursor->inMacroMode();
324 bool was_selection = mathcursor->selection();
328 mathcursor->normalize();
332 // --- Cursor Movements ---------------------------------------------
335 sel = true; // fall through...
338 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
339 updateLocal(bv, false);
344 sel = true; // fall through
347 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
348 updateLocal(bv, false);
356 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
357 updateLocal(bv, false);
365 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
366 updateLocal(bv, false);
373 mathcursor->home(sel);
374 updateLocal(bv, false);
381 mathcursor->end(sel);
382 updateLocal(bv, false);
385 case LFUN_DELETE_LINE_FORWARD:
386 bv->lockedInsetStoreUndo(Undo::DELETE);
387 mathcursor->delLine();
388 updateLocal(bv, true);
392 mathcursor->idxNext();
393 updateLocal(bv, false);
397 mathcursor->idxPrev();
398 updateLocal(bv, false);
402 bv->lockedInsetStoreUndo(Undo::EDIT);
403 mathcursor->splitCell();
404 updateLocal(bv, true);
408 bv->lockedInsetStoreUndo(Undo::DELETE);
409 mathcursor->backspace();
410 bv->updateInset(this, true);
414 bv->lockedInsetStoreUndo(Undo::DELETE);
416 bv->updateInset(this, true);
420 // sprintf(dispatch_buffer, "%d %d",);
421 // dispatch_result = dispatch_buffer;
424 lyxerr << "LFUN_SETXY broken!\n";
429 istringstream is(arg.c_str());
431 par()->getXY(x1, y1);
432 mathcursor->setPos(x1 + x, y1 + y);
433 updateLocal(bv, false);
439 mathcursor->macroModeClose();
440 bv->lockedInsetStoreUndo(Undo::INSERT);
441 mathcursor->selPaste();
442 updateLocal(bv, true);
446 bv->lockedInsetStoreUndo(Undo::DELETE);
447 mathcursor->selCut();
448 updateLocal(bv, true);
452 mathcursor->selCopy();
455 case LFUN_WORDRIGHTSEL:
456 case LFUN_WORDLEFTSEL:
459 // --- accented characters ------------------------------
461 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
462 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
463 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
464 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
465 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
466 case LFUN_MACRON: handleAccent(bv, "bar"); break;
467 case LFUN_DOT: handleAccent(bv, "dot"); break;
468 case LFUN_CARON: handleAccent(bv, "check"); break;
469 case LFUN_BREVE: handleAccent(bv, "breve"); break;
470 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
473 case LFUN_GREEK: handleFont(bv, LM_TC_GREEK1); break;
474 case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
475 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
476 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
477 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
478 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
479 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
480 case LFUN_NOUN: handleFont(bv, LM_TC_BB); break;
481 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
484 handleFont(bv, LM_TC_TEXTRM);
485 //bv->owner()->message(_("math text mode toggled"));
488 case LFUN_MATH_LIMITS:
489 bv->lockedInsetStoreUndo(Undo::INSERT);
490 if (mathcursor->toggleLimits())
491 updateLocal(bv, true);
496 bv->lockedInsetStoreUndo(Undo::INSERT);
497 latexkeys const * l = in_word_set(arg);
498 mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
499 updateLocal(bv, true);
503 case LFUN_INSERT_MATRIX:
505 bv->lockedInsetStoreUndo(Undo::INSERT);
506 mathcursor->interpret("matrix " + arg);
507 updateLocal(bv, true);
511 case LFUN_MATH_SPACE:
513 bv->lockedInsetStoreUndo(Undo::EDIT);
514 //MathSpaceInset * p = mathcursor->prevSpaceInset();
518 // mathcursor->insert(new MathSpaceInset(1));
519 mathcursor->insert(new MathSpaceInset(1));
520 updateLocal(bv, true);
524 case LFUN_MATH_DELIM:
526 bv->lockedInsetStoreUndo(Undo::INSERT);
527 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
530 istringstream is(arg.c_str());
533 lyxerr << "can't parse delimeters from '" << arg << "'\n";
536 bv->lockedInsetStoreUndo(Undo::EDIT);
537 mathcursor->handleDelim(ls, rs);
538 updateLocal(bv, true);
542 case LFUN_PROTECTEDSPACE:
543 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
544 bv->lockedInsetStoreUndo(Undo::INSERT);
545 mathcursor->insert(new MathSpaceInset(1));
546 updateLocal(bv, true);
550 bv->owner()->message(_("Invalid action in math mode!"));
554 case LFUN_MATH_HALIGN:
556 bv->lockedInsetStoreUndo(Undo::INSERT);
557 lyxerr << "handling halign '" << arg << "'\n";
559 MathArrayInset * p = matrixpar(idx);
562 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
563 updateLocal(bv, true);
567 case LFUN_MATH_VALIGN:
569 bv->lockedInsetStoreUndo(Undo::INSERT);
570 lyxerr << "handling valign '" << arg << "'\n";
572 MathArrayInset * p = matrixpar(idx);
575 p->valign(arg.size() ? arg[0] : 'c');
576 updateLocal(bv, true);
580 case LFUN_MATH_ROW_INSERT:
582 bv->lockedInsetStoreUndo(Undo::INSERT);
584 MathArrayInset * p = matrixpar(idx);
585 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
588 p->addRow(p->row(idx));
589 updateLocal(bv, true);
593 case LFUN_MATH_ROW_DELETE:
595 bv->lockedInsetStoreUndo(Undo::INSERT);
597 MathArrayInset * p = matrixpar(idx);
598 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
601 p->delRow(p->row(idx));
602 updateLocal(bv, true);
606 case LFUN_MATH_COLUMN_INSERT:
608 bv->lockedInsetStoreUndo(Undo::INSERT);
610 MathArrayInset * p = matrixpar(idx);
613 p->addCol(p->col(idx));
614 updateLocal(bv, true);
618 case LFUN_MATH_COLUMN_DELETE:
620 bv->lockedInsetStoreUndo(Undo::INSERT);
622 MathArrayInset * p = matrixpar(idx);
625 p->delCol(p->col(idx));
626 updateLocal(bv, true);
630 case LFUN_EXEC_COMMAND:
631 result = UNDISPATCHED;
635 case LFUN_INSERT_MATH:
636 case LFUN_SELFINSERT:
638 bv->lockedInsetStoreUndo(Undo::INSERT);
639 mathcursor->interpret(arg);
640 updateLocal(bv, true);
644 case LFUN_MATH_PANEL:
645 result = UNDISPATCHED;
649 result = UNDISPATCHED;
652 //mathcursor->normalize();
654 if (was_macro != mathcursor->inMacroMode()
655 && action >= 0 && action != LFUN_BACKSPACE)
656 updateLocal(bv, true);
658 if (mathcursor->selection() || was_selection)
659 toggleInsetSelection(bv);
661 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
662 result == UNDISPATCHED)
665 bv->unlockInset(this);
667 return result; // original version
671 Inset::Code InsetFormulaBase::lyxCode() const
673 return Inset::MATH_CODE;
677 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
679 if (bv->available()) {
680 // use selection if available..
682 //if (action == LFUN_MATH_IMPORT_SELECTION)
686 string sel = bv->getLyXText()->selectionAsString(bv->buffer(),
689 InsetFormulaBase * f;
691 f = new InsetFormula;
692 if (openNewInset(bv, f)) {
693 // don't do that also for LFUN_MATH_MODE unless you want end up with
694 // always changing to mathrm when opening an inlined inset
695 // -- I really hate "LyXfunc overloading"...
697 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
698 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
701 // create a macro if we see "\\newcommand" somewhere, and an ordinary
703 if (sel.find("\\newcommand") == string::npos)
704 f = new InsetFormula(sel);
706 f = new InsetFormulaMacro(sel);
707 bv->getLyXText()->cutSelection(bv);
711 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
715 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
717 mathDispatchCreation(bv, arg, true);
721 void mathDispatchMathMode(BufferView * bv, string const & arg)
723 mathDispatchCreation(bv, arg, false);
727 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
729 mathDispatchCreation(bv, arg, true);
733 void mathDispatchMathMacro(BufferView * bv, string const & arg)
735 if (bv->available()) {
737 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
740 string const s1 = token(s, ' ', 1);
741 int const na = s1.empty() ? 0 : lyx::atoi(s1);
742 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
748 void mathDispatchMathDelim(BufferView * bv, string const & arg)
750 if (bv->available()) {
751 if (openNewInset(bv, new InsetFormula))
752 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
757 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
759 if (bv->available()) {
760 if (openNewInset(bv, new InsetFormula))
761 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
766 void mathDispatchInsertMath(BufferView * bv, string const & arg)
768 if (bv->available()) {
769 if (arg.size() && arg[0] == '\\')
770 openNewInset(bv, new InsetFormula(arg));
772 mathDispatchMathMode(bv, arg);
777 void mathDispatchGreek(BufferView * bv, string const & arg)
779 if (bv->available()) {
780 InsetFormula * f = new InsetFormula;
781 if (openNewInset(bv, f)) {
782 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, string());
783 bv->theLockingInset()->localDispatch(bv, LFUN_SELFINSERT, arg);