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_scopeinset.h"
44 #include "math_macrotable.h"
45 #include "math_factory.h"
46 #include "support/lyxlib.h"
47 #include "undo_funcs.h"
53 extern char const * latex_mathenv[];
54 MathCursor * mathcursor = 0;
65 void mathed_init_fonts();
67 string nicelabel(string const & label)
69 return "(" + (label.empty() ? "#" : label) + ")";
72 void handleFont(BufferView * bv, MathTextCodes t)
74 if (mathcursor->selection())
75 bv->lockedInsetStoreUndo(Undo::EDIT);
76 mathcursor->handleFont(t);
80 void handleAccent(BufferView * bv, string const & name)
82 bv->lockedInsetStoreUndo(Undo::EDIT);
83 mathcursor->insert(createMathInset(name));
87 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
89 LyXText * lt = bv->getLyXText();
93 if (!bv->insertInset(new_inset)) {
97 new_inset->edit(bv, 0, 0, 0);
102 // returns the nearest enclosing grid
103 MathArrayInset * matrixpar(int & idx)
106 return (mathcursor ? mathcursor->enclosingArray(idx) : 0);
114 InsetFormulaBase::InsetFormulaBase()
117 #warning This is needed as long the math parser is not re-entrant
119 MathMacroTable::builtinMacros();
120 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
124 void InsetFormulaBase::read(Buffer const *, LyXLex & lex)
130 void InsetFormulaBase::write(Buffer const *, ostream & os) const
136 int InsetFormulaBase::latex(Buffer const *, ostream & os,
137 bool fragile, bool spacing) const
139 return latex(os, fragile, spacing);
143 int InsetFormulaBase::ascii(Buffer const *, ostream & os, int spacing) const
145 return ascii(os, spacing);
149 int InsetFormulaBase::linuxdoc(Buffer const *, ostream & os) const
155 int InsetFormulaBase::docBook(Buffer const *, ostream & os) const
162 // Check if uses AMS macros
163 void InsetFormulaBase::validate(LaTeXFeatures &) const
167 string const InsetFormulaBase::editMessage() const
169 return _("Math editor mode");
173 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
175 mathcursor = new MathCursor(this);
177 if (!bv->lockInset(this))
178 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
181 // if that is removed, we won't get the magenta box when entering an
182 // inset for the first time
183 bv->updateInset(this, false);
194 void InsetFormulaBase::edit(BufferView * bv, bool front)
196 // looks hackish but seems to work
197 edit(bv, front ? 0 : 1, 0, 0);
201 void InsetFormulaBase::insetUnlock(BufferView * bv)
204 if (mathcursor->inMacroMode()) {
205 mathcursor->macroModeClose();
206 updateLocal(bv, true);
211 bv->updateInset(this, false);
215 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
217 mathcursor->getPos(x, y);
223 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
228 if (isCursorVisible())
229 bv->hideLockedInsetCursor();
233 mathcursor->getPos(x, y);
238 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
239 bv->showLockedInsetCursor(x, y, asc, desc);
242 toggleCursorVisible();
246 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
248 if (!isCursorVisible()) {
252 mathcursor->getPos(x, y);
257 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
258 bv->fitLockedInsetCursor(x, y, asc, desc);
260 toggleInsetCursor(bv);
265 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
267 if (isCursorVisible())
268 toggleInsetCursor(bv);
272 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
275 bv->updateInset(this, false);
279 vector<string> const InsetFormulaBase::getLabelList() const
281 return std::vector<string>();
285 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
288 bv->updateInset(this, dirty);
292 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
293 int x, int y, int /*button*/)
299 mathcursor->setPos(x, y);
306 bv->updateInset(this, false);
311 void InsetFormulaBase::insetButtonPress(BufferView * bv,
312 int x, int y, int /*button*/)
317 if (mathcursor && mathcursor->selection()) {
318 mathcursor->selClear();
319 bv->updateInset(this, false);
324 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
325 int x, int y, int /*button*/)
327 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
330 mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo());
331 mathcursor->selStart();
333 mathcursor->getPos(sel_x, sel_y);
334 } else if (sel_flag) {
338 mathcursor->setPos(x, y);
340 mathcursor->getPos(x, y);
341 if (sel_x != x || sel_y != y)
342 bv->updateInset(this, false);
349 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
351 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
355 int greek_kb_flag = 0;
357 UpdatableInset::RESULT
358 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
361 //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
362 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
368 RESULT result = DISPATCHED;
370 bool was_macro = mathcursor->inMacroMode();
371 bool was_selection = mathcursor->selection();
375 mathcursor->normalize();
379 // --- Cursor Movements ---------------------------------------------
382 sel = true; // fall through...
385 result = DISPATCH_RESULT(mathcursor->right(sel));
386 updateLocal(bv, false);
391 sel = true; // fall through
394 result = DISPATCH_RESULT(mathcursor->left(sel));
395 updateLocal(bv, false);
403 result = DISPATCH_RESULT(mathcursor->up(sel));
404 updateLocal(bv, false);
412 result = DISPATCH_RESULT(mathcursor->down(sel));
413 updateLocal(bv, false);
418 updateLocal(bv, false);
423 updateLocal(bv, false);
426 case LFUN_DELETE_LINE_FORWARD:
427 bv->lockedInsetStoreUndo(Undo::DELETE);
428 mathcursor->delLine();
429 updateLocal(bv, true);
433 mathcursor->idxNext();
434 updateLocal(bv, false);
438 mathcursor->idxPrev();
439 updateLocal(bv, false);
443 bv->lockedInsetStoreUndo(Undo::EDIT);
444 mathcursor->splitCell();
445 updateLocal(bv, true);
449 bv->lockedInsetStoreUndo(Undo::DELETE);
450 mathcursor->backspace();
451 bv->updateInset(this, true);
455 bv->lockedInsetStoreUndo(Undo::DELETE);
457 bv->updateInset(this, true);
461 // sprintf(dispatch_buffer, "%d %d",);
462 // dispatch_result = dispatch_buffer;
465 lyxerr << "LFUN_SETXY broken!\n";
470 istringstream is(arg.c_str());
472 par()->getXY(x1, y1);
473 mathcursor->setPos(x1 + x, y1 + y);
474 updateLocal(bv, false);
480 mathcursor->macroModeClose();
481 bv->lockedInsetStoreUndo(Undo::INSERT);
482 mathcursor->selPaste();
483 updateLocal(bv, true);
487 bv->lockedInsetStoreUndo(Undo::DELETE);
488 mathcursor->selCut();
489 updateLocal(bv, true);
493 mathcursor->selCopy();
498 case LFUN_WORDRIGHTSEL:
499 case LFUN_WORDLEFTSEL:
502 // --- accented characters ------------------------------
504 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
505 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
506 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
507 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
508 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
509 case LFUN_MACRON: handleAccent(bv, "bar"); break;
510 case LFUN_DOT: handleAccent(bv, "dot"); break;
511 case LFUN_CARON: handleAccent(bv, "check"); break;
512 case LFUN_BREVE: handleAccent(bv, "breve"); break;
513 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
516 case LFUN_GREEK: handleFont(bv, LM_TC_GREEK1); break;
517 case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
518 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
519 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
520 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
521 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
522 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
523 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
526 handleFont(bv, LM_TC_TEXTRM);
527 //bv->owner()->message(_("math text mode toggled"));
530 case LFUN_MATH_LIMITS:
531 bv->lockedInsetStoreUndo(Undo::INSERT);
532 if (mathcursor->toggleLimits())
533 updateLocal(bv, true);
538 bv->lockedInsetStoreUndo(Undo::INSERT);
539 latexkeys const * l = in_word_set(arg);
540 mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
541 updateLocal(bv, true);
545 case LFUN_INSERT_MATRIX:
547 bv->lockedInsetStoreUndo(Undo::INSERT);
548 mathcursor->interpret("matrix " + arg);
549 updateLocal(bv, true);
553 case LFUN_MATH_SPACE:
555 bv->lockedInsetStoreUndo(Undo::EDIT);
556 MathSpaceInset * p = mathcursor->prevSpaceInset();
560 mathcursor->insert(new MathSpaceInset(1));
561 updateLocal(bv, true);
565 case LFUN_MATH_DELIM:
567 bv->lockedInsetStoreUndo(Undo::INSERT);
568 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
571 istringstream is(arg.c_str());
573 latexkeys const * l = in_word_set(ls);
574 latexkeys const * r = in_word_set(rs);
575 if (!is || !l || !r) {
576 lyxerr << "can't parse delimeters from '" << arg << "'\n";
579 bv->lockedInsetStoreUndo(Undo::EDIT);
580 mathcursor->handleDelim(l, r);
581 updateLocal(bv, true);
585 case LFUN_PROTECTEDSPACE:
586 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
587 bv->lockedInsetStoreUndo(Undo::INSERT);
588 mathcursor->insert(new MathSpaceInset(1));
589 updateLocal(bv, true);
593 bv->owner()->message(_("Invalid action in math mode!"));
597 case LFUN_MATH_HALIGN:
599 bv->lockedInsetStoreUndo(Undo::INSERT);
600 lyxerr << "handling halign '" << arg << "'\n";
602 MathArrayInset * p = matrixpar(idx);
605 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
606 updateLocal(bv, true);
610 case LFUN_MATH_VALIGN:
612 bv->lockedInsetStoreUndo(Undo::INSERT);
613 lyxerr << "handling valign '" << arg << "'\n";
615 MathArrayInset * p = matrixpar(idx);
618 p->valign(arg.size() ? arg[0] : 'c');
619 updateLocal(bv, true);
623 case LFUN_MATH_ROW_INSERT:
625 bv->lockedInsetStoreUndo(Undo::INSERT);
627 MathArrayInset * p = matrixpar(idx);
628 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
631 p->addRow(p->row(idx));
632 updateLocal(bv, true);
636 case LFUN_MATH_ROW_DELETE:
638 bv->lockedInsetStoreUndo(Undo::INSERT);
640 MathArrayInset * p = matrixpar(idx);
641 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
644 p->delRow(p->row(idx));
645 updateLocal(bv, true);
649 case LFUN_MATH_COLUMN_INSERT:
651 bv->lockedInsetStoreUndo(Undo::INSERT);
653 MathArrayInset * p = matrixpar(idx);
656 p->addCol(p->col(idx));
657 updateLocal(bv, true);
661 case LFUN_MATH_COLUMN_DELETE:
663 bv->lockedInsetStoreUndo(Undo::INSERT);
665 MathArrayInset * p = matrixpar(idx);
668 p->delCol(p->col(idx));
669 updateLocal(bv, true);
673 case LFUN_EXEC_COMMAND:
674 result = UNDISPATCHED;
678 case LFUN_INSERT_MATH:
679 case LFUN_SELFINSERT:
681 bv->lockedInsetStoreUndo(Undo::INSERT);
682 mathcursor->interpret(arg);
683 updateLocal(bv, true);
687 case LFUN_MATH_PANEL:
688 result = UNDISPATCHED;
692 lyxerr << "Closed by action " << action << endl;
696 mathcursor->normalize();
698 if (was_macro != mathcursor->inMacroMode()
699 && action >= 0 && action != LFUN_BACKSPACE)
700 updateLocal(bv, true);
702 if (mathcursor->selection() || was_selection)
703 toggleInsetSelection(bv);
705 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
706 result == UNDISPATCHED)
709 bv->unlockInset(this);
711 return result; // original version
715 Inset::Code InsetFormulaBase::lyxCode() const
717 return Inset::MATH_CODE;
721 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
723 if (bv->available()) {
724 // use selection if available..
726 //if (action == LFUN_MATH_IMPORT_SELECTION)
730 string sel = bv->getLyXText()->selectionAsString(bv->buffer());
732 InsetFormulaBase * f;
734 f = new InsetFormula;
735 if (openNewInset(bv, f)) {
736 // don't do that also for LFUN_MATH_MODE unless you want end up with
737 // always changing to mathrm when opening an inlined inset
738 // -- I really hate "LyXfunc overloading"...
740 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
741 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
744 // create a macro if we see "\\newcommand" somewhere, and an ordinary
746 if (sel.find("\\newcommand") == string::npos)
747 f = new InsetFormula(sel);
749 f = new InsetFormulaMacro(sel);
750 bv->getLyXText()->cutSelection(bv);
754 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
757 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
759 mathDispatchCreation(bv, arg, true);
763 void mathDispatchMathMode(BufferView * bv, string const & arg)
765 mathDispatchCreation(bv, arg, false);
769 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
771 mathDispatchCreation(bv, arg, true);
775 void mathDispatchMathMacro(BufferView * bv, string const & arg)
777 if (bv->available()) {
779 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
782 string const s1 = token(s, ' ', 1);
783 int const na = s1.empty() ? 0 : lyx::atoi(s1);
784 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
790 void mathDispatchMathDelim(BufferView * bv, string const & arg)
792 if (bv->available()) {
793 if (openNewInset(bv, new InsetFormula))
794 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
799 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
801 if (bv->available()) {
802 if (openNewInset(bv, new InsetFormula))
803 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
808 void mathDispatchInsertMath(BufferView * bv, string const & arg)
810 if (bv->available()) {
811 if (arg.size() && arg[0] == '\\')
812 openNewInset(bv, new InsetFormula(arg));
814 mathDispatchMathMode(bv, arg);