3 * Purpose: Implementation of formula inset
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
6 * Description: Allows the edition of math paragraphs inside Lyx.
8 * Copyright: 1996-1998 Alejandro Aguilar Sierra
10 * Version: 0.4, Lyx project.
12 * You are free to use and modify this code under the terms of
13 * the GNU General Public Licence version 2 or later.
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 mathed_init_fonts();
66 string nicelabel(string const & label)
68 return "(" + (label.empty() ? "#" : label) + ")";
71 void handleFont(BufferView * bv, MathTextCodes t)
73 if (mathcursor->selection())
74 bv->lockedInsetStoreUndo(Undo::EDIT);
75 mathcursor->handleFont(t);
79 void handleAccent(BufferView * bv, string const & name)
81 bv->lockedInsetStoreUndo(Undo::EDIT);
82 mathcursor->insert(createMathInset(name));
86 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
88 LyXText * lt = bv->getLyXText();
92 if (!bv->insertInset(new_inset)) {
96 new_inset->edit(bv, 0, 0, 0);
101 // returns the nearest enclosing grid
102 MathArrayInset * matrixpar(int & idx)
105 return (mathcursor ? mathcursor->enclosingArray(idx) : 0);
113 InsetFormulaBase::InsetFormulaBase()
115 // This is needed as long the math parser is not re-entrant
116 MathMacroTable::builtinMacros();
117 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
121 void InsetFormulaBase::read(Buffer const *, LyXLex & lex)
127 void InsetFormulaBase::write(Buffer const *, ostream & os) const
133 int InsetFormulaBase::latex(Buffer const *, ostream & os,
134 bool fragile, bool spacing) const
136 return latex(os, fragile, spacing);
140 int InsetFormulaBase::ascii(Buffer const *, ostream & os, int spacing) const
142 return ascii(os, spacing);
146 int InsetFormulaBase::linuxdoc(Buffer const *, ostream & os) const
152 int InsetFormulaBase::docbook(Buffer const *, ostream & os) const
159 // Check if uses AMS macros
160 void InsetFormulaBase::validate(LaTeXFeatures &) const
164 string const InsetFormulaBase::editMessage() const
166 return _("Math editor mode");
170 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
172 mathcursor = new MathCursor(this);
174 if (!bv->lockInset(this))
175 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
178 // if that is removed, we won't get the magenta box when entering an
179 // inset for the first time
180 bv->updateInset(this, false);
191 void InsetFormulaBase::edit(BufferView * bv, bool front)
193 // looks hackish but seems to work
194 edit(bv, front ? 0 : 1, 0, 0);
198 void InsetFormulaBase::insetUnlock(BufferView * bv)
201 if (mathcursor->inMacroMode()) {
202 mathcursor->macroModeClose();
203 updateLocal(bv, true);
208 bv->updateInset(this, false);
212 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
214 mathcursor->getPos(x, y);
221 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
226 if (isCursorVisible())
227 bv->hideLockedInsetCursor();
231 mathcursor->getPos(x, y);
237 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
238 bv->showLockedInsetCursor(x, y, asc, des);
241 toggleCursorVisible();
245 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
247 if (!isCursorVisible()) {
251 mathcursor->getPos(x, y);
256 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
257 bv->fitLockedInsetCursor(x, y, asc, des);
259 toggleInsetCursor(bv);
264 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
266 if (isCursorVisible())
267 toggleInsetCursor(bv);
271 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
274 bv->updateInset(this, false);
278 vector<string> const InsetFormulaBase::getLabelList() const
280 return std::vector<string>();
284 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
287 bv->updateInset(this, dirty);
291 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
292 int x, int y, int /*button*/)
298 mathcursor->setPos(x, y);
305 bv->updateInset(this, false);
310 void InsetFormulaBase::insetButtonPress(BufferView * bv,
311 int x, int y, int /*button*/)
316 if (mathcursor && mathcursor->selection()) {
317 mathcursor->selClear();
318 bv->updateInset(this, false);
323 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
324 int x, int y, int /*button*/)
326 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
329 mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo());
330 mathcursor->selStart();
332 mathcursor->getPos(sel_x, sel_y);
333 } else if (sel_flag) {
337 mathcursor->setPos(x, y);
339 mathcursor->getPos(x, y);
340 if (sel_x != x || sel_y != y)
341 bv->updateInset(this, false);
348 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
350 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
354 int greek_kb_flag = 0;
356 UpdatableInset::RESULT
357 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
360 //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
361 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
367 RESULT result = DISPATCHED;
369 bool was_macro = mathcursor->inMacroMode();
370 bool was_selection = mathcursor->selection();
374 mathcursor->normalize();
378 // --- Cursor Movements ---------------------------------------------
381 sel = true; // fall through...
384 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
385 updateLocal(bv, false);
390 sel = true; // fall through
393 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
394 updateLocal(bv, false);
402 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
403 updateLocal(bv, false);
411 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
412 updateLocal(bv, false);
419 mathcursor->home(sel);
420 updateLocal(bv, false);
427 mathcursor->end(sel);
428 updateLocal(bv, false);
431 case LFUN_DELETE_LINE_FORWARD:
432 bv->lockedInsetStoreUndo(Undo::DELETE);
433 mathcursor->delLine();
434 updateLocal(bv, true);
438 mathcursor->idxNext();
439 updateLocal(bv, false);
443 mathcursor->idxPrev();
444 updateLocal(bv, false);
448 bv->lockedInsetStoreUndo(Undo::EDIT);
449 mathcursor->splitCell();
450 updateLocal(bv, true);
454 bv->lockedInsetStoreUndo(Undo::DELETE);
455 mathcursor->backspace();
456 bv->updateInset(this, true);
460 bv->lockedInsetStoreUndo(Undo::DELETE);
462 bv->updateInset(this, true);
466 // sprintf(dispatch_buffer, "%d %d",);
467 // dispatch_result = dispatch_buffer;
470 lyxerr << "LFUN_SETXY broken!\n";
475 istringstream is(arg.c_str());
477 par()->getXY(x1, y1);
478 mathcursor->setPos(x1 + x, y1 + y);
479 updateLocal(bv, false);
485 mathcursor->macroModeClose();
486 bv->lockedInsetStoreUndo(Undo::INSERT);
487 mathcursor->selPaste();
488 updateLocal(bv, true);
492 bv->lockedInsetStoreUndo(Undo::DELETE);
493 mathcursor->selCut();
494 updateLocal(bv, true);
498 mathcursor->selCopy();
501 case LFUN_WORDRIGHTSEL:
502 case LFUN_WORDLEFTSEL:
505 // --- accented characters ------------------------------
507 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
508 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
509 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
510 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
511 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
512 case LFUN_MACRON: handleAccent(bv, "bar"); break;
513 case LFUN_DOT: handleAccent(bv, "dot"); break;
514 case LFUN_CARON: handleAccent(bv, "check"); break;
515 case LFUN_BREVE: handleAccent(bv, "breve"); break;
516 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
519 case LFUN_GREEK: handleFont(bv, LM_TC_GREEK1); break;
520 case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
521 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
522 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
523 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
524 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
525 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
526 case LFUN_NOUN: handleFont(bv, LM_TC_BB); break;
527 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
530 handleFont(bv, LM_TC_TEXTRM);
531 //bv->owner()->message(_("math text mode toggled"));
534 case LFUN_MATH_LIMITS:
535 bv->lockedInsetStoreUndo(Undo::INSERT);
536 if (mathcursor->toggleLimits())
537 updateLocal(bv, true);
542 bv->lockedInsetStoreUndo(Undo::INSERT);
543 latexkeys const * l = in_word_set(arg);
544 mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
545 updateLocal(bv, true);
549 case LFUN_INSERT_MATRIX:
551 bv->lockedInsetStoreUndo(Undo::INSERT);
552 mathcursor->interpret("matrix " + arg);
553 updateLocal(bv, true);
557 case LFUN_MATH_SPACE:
559 bv->lockedInsetStoreUndo(Undo::EDIT);
560 //MathSpaceInset * p = mathcursor->prevSpaceInset();
564 // mathcursor->insert(new MathSpaceInset(1));
565 mathcursor->insert(new MathSpaceInset(1));
566 updateLocal(bv, true);
570 case LFUN_MATH_DELIM:
572 bv->lockedInsetStoreUndo(Undo::INSERT);
573 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
576 istringstream is(arg.c_str());
579 lyxerr << "can't parse delimeters from '" << arg << "'\n";
582 bv->lockedInsetStoreUndo(Undo::EDIT);
583 mathcursor->handleDelim(ls, rs);
584 updateLocal(bv, true);
588 case LFUN_PROTECTEDSPACE:
589 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
590 bv->lockedInsetStoreUndo(Undo::INSERT);
591 mathcursor->insert(new MathSpaceInset(1));
592 updateLocal(bv, true);
596 bv->owner()->message(_("Invalid action in math mode!"));
600 case LFUN_MATH_HALIGN:
602 bv->lockedInsetStoreUndo(Undo::INSERT);
603 lyxerr << "handling halign '" << arg << "'\n";
605 MathArrayInset * p = matrixpar(idx);
608 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
609 updateLocal(bv, true);
613 case LFUN_MATH_VALIGN:
615 bv->lockedInsetStoreUndo(Undo::INSERT);
616 lyxerr << "handling valign '" << arg << "'\n";
618 MathArrayInset * p = matrixpar(idx);
621 p->valign(arg.size() ? arg[0] : 'c');
622 updateLocal(bv, true);
626 case LFUN_MATH_ROW_INSERT:
628 bv->lockedInsetStoreUndo(Undo::INSERT);
630 MathArrayInset * p = matrixpar(idx);
631 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
634 p->addRow(p->row(idx));
635 updateLocal(bv, true);
639 case LFUN_MATH_ROW_DELETE:
641 bv->lockedInsetStoreUndo(Undo::INSERT);
643 MathArrayInset * p = matrixpar(idx);
644 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
647 p->delRow(p->row(idx));
648 updateLocal(bv, true);
652 case LFUN_MATH_COLUMN_INSERT:
654 bv->lockedInsetStoreUndo(Undo::INSERT);
656 MathArrayInset * p = matrixpar(idx);
659 p->addCol(p->col(idx));
660 updateLocal(bv, true);
664 case LFUN_MATH_COLUMN_DELETE:
666 bv->lockedInsetStoreUndo(Undo::INSERT);
668 MathArrayInset * p = matrixpar(idx);
671 p->delCol(p->col(idx));
672 updateLocal(bv, true);
676 case LFUN_EXEC_COMMAND:
677 result = UNDISPATCHED;
681 case LFUN_INSERT_MATH:
682 case LFUN_SELFINSERT:
684 bv->lockedInsetStoreUndo(Undo::INSERT);
685 mathcursor->interpret(arg);
686 updateLocal(bv, true);
690 case LFUN_MATH_PANEL:
691 result = UNDISPATCHED;
695 lyxerr << "Closed by action " << action << endl;
696 result = FINISHED_RIGHT;
699 //mathcursor->normalize();
701 if (was_macro != mathcursor->inMacroMode()
702 && action >= 0 && action != LFUN_BACKSPACE)
703 updateLocal(bv, true);
705 if (mathcursor->selection() || was_selection)
706 toggleInsetSelection(bv);
708 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
709 result == UNDISPATCHED)
712 bv->unlockInset(this);
714 return result; // original version
718 Inset::Code InsetFormulaBase::lyxCode() const
720 return Inset::MATH_CODE;
724 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
726 if (bv->available()) {
727 // use selection if available..
729 //if (action == LFUN_MATH_IMPORT_SELECTION)
733 string sel = bv->getLyXText()->selectionAsString(bv->buffer(),
736 InsetFormulaBase * f;
738 f = new InsetFormula;
739 if (openNewInset(bv, f)) {
740 // don't do that also for LFUN_MATH_MODE unless you want end up with
741 // always changing to mathrm when opening an inlined inset
742 // -- I really hate "LyXfunc overloading"...
744 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
745 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
748 // create a macro if we see "\\newcommand" somewhere, and an ordinary
750 if (sel.find("\\newcommand") == string::npos)
751 f = new InsetFormula(sel);
753 f = new InsetFormulaMacro(sel);
754 bv->getLyXText()->cutSelection(bv);
758 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
762 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
764 mathDispatchCreation(bv, arg, true);
768 void mathDispatchMathMode(BufferView * bv, string const & arg)
770 mathDispatchCreation(bv, arg, false);
774 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
776 mathDispatchCreation(bv, arg, true);
780 void mathDispatchMathMacro(BufferView * bv, string const & arg)
782 if (bv->available()) {
784 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
787 string const s1 = token(s, ' ', 1);
788 int const na = s1.empty() ? 0 : lyx::atoi(s1);
789 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
795 void mathDispatchMathDelim(BufferView * bv, string const & arg)
797 if (bv->available()) {
798 if (openNewInset(bv, new InsetFormula))
799 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
804 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
806 if (bv->available()) {
807 if (openNewInset(bv, new InsetFormula))
808 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
813 void mathDispatchInsertMath(BufferView * bv, string const & arg)
815 if (bv->available()) {
816 if (arg.size() && arg[0] == '\\')
817 openNewInset(bv, new InsetFormula(arg));
819 mathDispatchMathMode(bv, arg);
824 void mathDispatchGreek(BufferView * bv, string const & arg)
826 if (bv->available()) {
827 InsetFormula * f = new InsetFormula;
828 if (openNewInset(bv, f)) {
829 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, string());
830 bv->theLockingInset()->localDispatch(bv, LFUN_SELFINSERT, arg);