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 void InsetFormulaBase::read(Buffer const *, LyXLex & lex)
119 void InsetFormulaBase::write(Buffer const *, ostream & os) const
125 int InsetFormulaBase::latex(Buffer const *, ostream & os,
126 bool fragile, bool spacing) const
128 return latex(os, fragile, spacing);
132 int InsetFormulaBase::ascii(Buffer const *, ostream & os, int spacing) const
134 return ascii(os, spacing);
138 int InsetFormulaBase::linuxdoc(Buffer const *, ostream & os) const
144 int InsetFormulaBase::docbook(Buffer const *, ostream & os) const
151 // Check if uses AMS macros
152 void InsetFormulaBase::validate(LaTeXFeatures &) const
156 string const InsetFormulaBase::editMessage() const
158 return _("Math editor mode");
162 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
164 mathcursor = new MathCursor(this);
166 if (!bv->lockInset(this))
167 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
170 // if that is removed, we won't get the magenta box when entering an
171 // inset for the first time
172 bv->updateInset(this, false);
183 void InsetFormulaBase::edit(BufferView * bv, bool front)
185 // looks hackish but seems to work
186 edit(bv, front ? 0 : 1, 0, 0);
190 void InsetFormulaBase::insetUnlock(BufferView * bv)
193 if (mathcursor->inMacroMode()) {
194 mathcursor->macroModeClose();
195 updateLocal(bv, true);
200 bv->updateInset(this, false);
204 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
206 mathcursor->getPos(x, y);
213 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
218 if (isCursorVisible())
219 bv->hideLockedInsetCursor();
223 mathcursor->getPos(x, y);
229 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
230 bv->showLockedInsetCursor(x, y, asc, des);
233 toggleCursorVisible();
237 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
239 if (!isCursorVisible()) {
243 mathcursor->getPos(x, y);
248 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
249 bv->fitLockedInsetCursor(x, y, asc, des);
251 toggleInsetCursor(bv);
256 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
258 if (isCursorVisible())
259 toggleInsetCursor(bv);
263 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
266 bv->updateInset(this, false);
270 vector<string> const InsetFormulaBase::getLabelList() const
272 return std::vector<string>();
276 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
279 bv->updateInset(this, dirty);
283 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
284 int x, int y, int /*button*/)
290 mathcursor->setPos(x, y);
297 bv->updateInset(this, false);
302 void InsetFormulaBase::insetButtonPress(BufferView * bv,
303 int x, int y, int /*button*/)
308 if (mathcursor && mathcursor->selection()) {
309 mathcursor->selClear();
310 bv->updateInset(this, false);
315 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
316 int x, int y, int /*button*/)
318 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
321 mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo());
322 mathcursor->selStart();
324 mathcursor->getPos(sel_x, sel_y);
325 } else if (sel_flag) {
329 mathcursor->setPos(x, y);
331 mathcursor->getPos(x, y);
332 if (sel_x != x || sel_y != y)
333 bv->updateInset(this, false);
340 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
342 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
346 int greek_kb_flag = 0;
348 UpdatableInset::RESULT
349 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
352 //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
353 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
359 RESULT result = DISPATCHED;
361 bool was_macro = mathcursor->inMacroMode();
362 bool was_selection = mathcursor->selection();
366 mathcursor->normalize();
370 // --- Cursor Movements ---------------------------------------------
373 sel = true; // fall through...
376 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
377 updateLocal(bv, false);
382 sel = true; // fall through
385 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
386 updateLocal(bv, false);
394 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
395 updateLocal(bv, false);
403 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
404 updateLocal(bv, false);
411 mathcursor->home(sel);
412 updateLocal(bv, false);
419 mathcursor->end(sel);
420 updateLocal(bv, false);
423 case LFUN_DELETE_LINE_FORWARD:
424 bv->lockedInsetStoreUndo(Undo::DELETE);
425 mathcursor->delLine();
426 updateLocal(bv, true);
430 mathcursor->idxNext();
431 updateLocal(bv, false);
435 mathcursor->idxPrev();
436 updateLocal(bv, false);
440 bv->lockedInsetStoreUndo(Undo::EDIT);
441 mathcursor->splitCell();
442 updateLocal(bv, true);
446 bv->lockedInsetStoreUndo(Undo::DELETE);
447 mathcursor->backspace();
448 bv->updateInset(this, true);
452 bv->lockedInsetStoreUndo(Undo::DELETE);
454 bv->updateInset(this, true);
458 // sprintf(dispatch_buffer, "%d %d",);
459 // dispatch_result = dispatch_buffer;
462 lyxerr << "LFUN_SETXY broken!\n";
467 istringstream is(arg.c_str());
469 par()->getXY(x1, y1);
470 mathcursor->setPos(x1 + x, y1 + y);
471 updateLocal(bv, false);
477 mathcursor->macroModeClose();
478 bv->lockedInsetStoreUndo(Undo::INSERT);
479 mathcursor->selPaste();
480 updateLocal(bv, true);
484 bv->lockedInsetStoreUndo(Undo::DELETE);
485 mathcursor->selCut();
486 updateLocal(bv, true);
490 mathcursor->selCopy();
493 case LFUN_WORDRIGHTSEL:
494 case LFUN_WORDLEFTSEL:
497 // --- accented characters ------------------------------
499 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
500 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
501 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
502 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
503 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
504 case LFUN_MACRON: handleAccent(bv, "bar"); break;
505 case LFUN_DOT: handleAccent(bv, "dot"); break;
506 case LFUN_CARON: handleAccent(bv, "check"); break;
507 case LFUN_BREVE: handleAccent(bv, "breve"); break;
508 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
511 case LFUN_GREEK: handleFont(bv, LM_TC_GREEK1); break;
512 case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
513 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
514 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
515 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
516 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
517 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
518 case LFUN_NOUN: handleFont(bv, LM_TC_BB); break;
519 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
522 handleFont(bv, LM_TC_TEXTRM);
523 //bv->owner()->message(_("math text mode toggled"));
526 case LFUN_MATH_LIMITS:
527 bv->lockedInsetStoreUndo(Undo::INSERT);
528 if (mathcursor->toggleLimits())
529 updateLocal(bv, true);
534 bv->lockedInsetStoreUndo(Undo::INSERT);
535 latexkeys const * l = in_word_set(arg);
536 mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
537 updateLocal(bv, true);
541 case LFUN_INSERT_MATRIX:
543 bv->lockedInsetStoreUndo(Undo::INSERT);
544 mathcursor->interpret("matrix " + arg);
545 updateLocal(bv, true);
549 case LFUN_MATH_SPACE:
551 bv->lockedInsetStoreUndo(Undo::EDIT);
552 //MathSpaceInset * p = mathcursor->prevSpaceInset();
556 // mathcursor->insert(new MathSpaceInset(1));
557 mathcursor->insert(new MathSpaceInset(1));
558 updateLocal(bv, true);
562 case LFUN_MATH_DELIM:
564 bv->lockedInsetStoreUndo(Undo::INSERT);
565 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
568 istringstream is(arg.c_str());
571 lyxerr << "can't parse delimeters from '" << arg << "'\n";
574 bv->lockedInsetStoreUndo(Undo::EDIT);
575 mathcursor->handleDelim(ls, rs);
576 updateLocal(bv, true);
580 case LFUN_PROTECTEDSPACE:
581 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
582 bv->lockedInsetStoreUndo(Undo::INSERT);
583 mathcursor->insert(new MathSpaceInset(1));
584 updateLocal(bv, true);
588 bv->owner()->message(_("Invalid action in math mode!"));
592 case LFUN_MATH_HALIGN:
594 bv->lockedInsetStoreUndo(Undo::INSERT);
595 lyxerr << "handling halign '" << arg << "'\n";
597 MathArrayInset * p = matrixpar(idx);
600 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
601 updateLocal(bv, true);
605 case LFUN_MATH_VALIGN:
607 bv->lockedInsetStoreUndo(Undo::INSERT);
608 lyxerr << "handling valign '" << arg << "'\n";
610 MathArrayInset * p = matrixpar(idx);
613 p->valign(arg.size() ? arg[0] : 'c');
614 updateLocal(bv, true);
618 case LFUN_MATH_ROW_INSERT:
620 bv->lockedInsetStoreUndo(Undo::INSERT);
622 MathArrayInset * p = matrixpar(idx);
623 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
626 p->addRow(p->row(idx));
627 updateLocal(bv, true);
631 case LFUN_MATH_ROW_DELETE:
633 bv->lockedInsetStoreUndo(Undo::INSERT);
635 MathArrayInset * p = matrixpar(idx);
636 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
639 p->delRow(p->row(idx));
640 updateLocal(bv, true);
644 case LFUN_MATH_COLUMN_INSERT:
646 bv->lockedInsetStoreUndo(Undo::INSERT);
648 MathArrayInset * p = matrixpar(idx);
651 p->addCol(p->col(idx));
652 updateLocal(bv, true);
656 case LFUN_MATH_COLUMN_DELETE:
658 bv->lockedInsetStoreUndo(Undo::INSERT);
660 MathArrayInset * p = matrixpar(idx);
663 p->delCol(p->col(idx));
664 updateLocal(bv, true);
668 case LFUN_EXEC_COMMAND:
669 result = UNDISPATCHED;
673 case LFUN_INSERT_MATH:
674 case LFUN_SELFINSERT:
676 bv->lockedInsetStoreUndo(Undo::INSERT);
677 mathcursor->interpret(arg);
678 updateLocal(bv, true);
682 case LFUN_MATH_PANEL:
683 result = UNDISPATCHED;
687 result = UNDISPATCHED;
690 //mathcursor->normalize();
692 if (was_macro != mathcursor->inMacroMode()
693 && action >= 0 && action != LFUN_BACKSPACE)
694 updateLocal(bv, true);
696 if (mathcursor->selection() || was_selection)
697 toggleInsetSelection(bv);
699 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
700 result == UNDISPATCHED)
703 bv->unlockInset(this);
705 return result; // original version
709 Inset::Code InsetFormulaBase::lyxCode() const
711 return Inset::MATH_CODE;
715 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
717 if (bv->available()) {
718 // use selection if available..
720 //if (action == LFUN_MATH_IMPORT_SELECTION)
724 string sel = bv->getLyXText()->selectionAsString(bv->buffer(),
727 InsetFormulaBase * f;
729 f = new InsetFormula;
730 if (openNewInset(bv, f)) {
731 // don't do that also for LFUN_MATH_MODE unless you want end up with
732 // always changing to mathrm when opening an inlined inset
733 // -- I really hate "LyXfunc overloading"...
735 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
736 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
739 // create a macro if we see "\\newcommand" somewhere, and an ordinary
741 if (sel.find("\\newcommand") == string::npos)
742 f = new InsetFormula(sel);
744 f = new InsetFormulaMacro(sel);
745 bv->getLyXText()->cutSelection(bv);
749 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
753 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
755 mathDispatchCreation(bv, arg, true);
759 void mathDispatchMathMode(BufferView * bv, string const & arg)
761 mathDispatchCreation(bv, arg, false);
765 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
767 mathDispatchCreation(bv, arg, true);
771 void mathDispatchMathMacro(BufferView * bv, string const & arg)
773 if (bv->available()) {
775 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
778 string const s1 = token(s, ' ', 1);
779 int const na = s1.empty() ? 0 : lyx::atoi(s1);
780 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
786 void mathDispatchMathDelim(BufferView * bv, string const & arg)
788 if (bv->available()) {
789 if (openNewInset(bv, new InsetFormula))
790 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
795 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
797 if (bv->available()) {
798 if (openNewInset(bv, new InsetFormula))
799 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
804 void mathDispatchInsertMath(BufferView * bv, string const & arg)
806 if (bv->available()) {
807 if (arg.size() && arg[0] == '\\')
808 openNewInset(bv, new InsetFormula(arg));
810 mathDispatchMathMode(bv, arg);
815 void mathDispatchGreek(BufferView * bv, string const & arg)
817 if (bv->available()) {
818 InsetFormula * f = new InsetFormula;
819 if (openNewInset(bv, f)) {
820 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, string());
821 bv->theLockingInset()->localDispatch(bv, LFUN_SELFINSERT, arg);