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"
36 #include "support/lstrings.h"
40 #include "math_arrayinset.h"
41 #include "math_spaceinset.h"
42 #include "math_scopeinset.h"
43 #include "math_macrotable.h"
44 #include "support/lyxlib.h"
45 #include "mathed/support.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->handleAccent(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()
116 #warning This is needed as long the math parser is not re-entrant
118 MathMacroTable::builtinMacros();
119 //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
123 void InsetFormulaBase::read(Buffer const *, LyXLex & lex)
129 void InsetFormulaBase::write(Buffer const *, ostream & os) const
135 int InsetFormulaBase::latex(Buffer const *, ostream & os,
136 bool fragile, bool spacing) const
138 return latex(os, fragile, spacing);
142 int InsetFormulaBase::ascii(Buffer const *, ostream & os, int spacing) const
144 return ascii(os, spacing);
148 int InsetFormulaBase::linuxdoc(Buffer const *, ostream & os) const
154 int InsetFormulaBase::docBook(Buffer const *, ostream & os) const
161 // Check if uses AMS macros
162 void InsetFormulaBase::validate(LaTeXFeatures &) const
166 string const InsetFormulaBase::editMessage() const
168 return _("Math editor mode");
172 void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
174 mathcursor = new MathCursor(this);
176 if (!bv->lockInset(this))
177 lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
180 // if that is removed, we won't get the magenta box when entering an
181 // inset for the first time
182 bv->updateInset(this, false);
193 void InsetFormulaBase::edit(BufferView * bv, bool front)
195 // looks hackish but seems to work
196 edit(bv, front ? 0 : 1, 0, 0);
200 void InsetFormulaBase::insetUnlock(BufferView * bv)
203 if (mathcursor->inMacroMode()) {
204 mathcursor->macroModeClose();
205 updateLocal(bv, true);
210 bv->updateInset(this, false);
214 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
216 mathcursor->getPos(x, y);
222 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
227 if (isCursorVisible())
228 bv->hideLockedInsetCursor();
232 mathcursor->getPos(x, y);
237 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, desc);
238 bv->showLockedInsetCursor(x, y, asc, desc);
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, desc);
257 bv->fitLockedInsetCursor(x, y, asc, desc);
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 = DISPATCH_RESULT(mathcursor->right(sel));
385 updateLocal(bv, false);
390 sel = true; // fall through
393 result = DISPATCH_RESULT(mathcursor->left(sel));
394 updateLocal(bv, false);
402 result = DISPATCH_RESULT(mathcursor->up(sel));
403 updateLocal(bv, false);
411 result = DISPATCH_RESULT(mathcursor->down(sel));
412 updateLocal(bv, false);
417 updateLocal(bv, false);
422 updateLocal(bv, false);
425 case LFUN_DELETE_LINE_FORWARD:
426 bv->lockedInsetStoreUndo(Undo::DELETE);
427 mathcursor->delLine();
428 updateLocal(bv, true);
432 mathcursor->idxNext();
433 updateLocal(bv, false);
437 mathcursor->idxPrev();
438 updateLocal(bv, false);
442 bv->lockedInsetStoreUndo(Undo::EDIT);
443 mathcursor->splitCell();
444 updateLocal(bv, true);
448 bv->lockedInsetStoreUndo(Undo::DELETE);
449 mathcursor->backspace();
450 bv->updateInset(this, true);
454 bv->lockedInsetStoreUndo(Undo::DELETE);
456 bv->updateInset(this, true);
460 // sprintf(dispatch_buffer, "%d %d",);
461 // dispatch_result = dispatch_buffer;
464 lyxerr << "LFUN_SETXY broken!\n";
469 istringstream is(arg.c_str());
471 par()->getXY(x1, y1);
472 mathcursor->setPos(x1 + x, y1 + y);
473 updateLocal(bv, false);
479 mathcursor->macroModeClose();
480 bv->lockedInsetStoreUndo(Undo::INSERT);
481 mathcursor->selPaste();
482 updateLocal(bv, true);
486 bv->lockedInsetStoreUndo(Undo::DELETE);
487 mathcursor->selCut();
488 updateLocal(bv, true);
492 mathcursor->selCopy();
497 case LFUN_WORDRIGHTSEL:
498 case LFUN_WORDLEFTSEL:
501 // --- accented characters ------------------------------
503 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
504 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
505 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
506 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
507 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
508 case LFUN_MACRON: handleAccent(bv, "bar"); break;
509 case LFUN_DOT: handleAccent(bv, "dot"); break;
510 case LFUN_CARON: handleAccent(bv, "check"); break;
511 case LFUN_BREVE: handleAccent(bv, "breve"); break;
512 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
515 case LFUN_GREEK: handleFont(bv, LM_TC_GREEK1); break;
516 case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
517 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
518 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
519 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
520 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
521 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
522 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
525 handleFont(bv, LM_TC_TEXTRM);
526 //bv->owner()->message(_("math text mode toggled"));
529 case LFUN_MATH_LIMITS:
530 bv->lockedInsetStoreUndo(Undo::INSERT);
531 if (mathcursor->toggleLimits())
532 updateLocal(bv, true);
537 bv->lockedInsetStoreUndo(Undo::INSERT);
538 latexkeys const * l = in_word_set(arg);
539 mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
540 updateLocal(bv, true);
544 case LFUN_INSERT_MATRIX:
546 bv->lockedInsetStoreUndo(Undo::INSERT);
547 mathcursor->interpret("matrix " + arg);
548 updateLocal(bv, true);
552 case LFUN_MATH_SPACE:
554 bv->lockedInsetStoreUndo(Undo::EDIT);
555 MathSpaceInset * p = mathcursor->prevSpaceInset();
559 mathcursor->insert(new MathSpaceInset(1));
560 updateLocal(bv, true);
564 case LFUN_MATH_DELIM:
566 bv->lockedInsetStoreUndo(Undo::INSERT);
567 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
570 istringstream is(arg.c_str());
572 latexkeys const * l = in_word_set(ls);
573 latexkeys const * r = in_word_set(rs);
574 if (!is || !l || !r) {
575 lyxerr << "can't parse delimeters from '" << arg << "'\n";
578 bv->lockedInsetStoreUndo(Undo::EDIT);
579 mathcursor->handleDelim(l, r);
580 updateLocal(bv, true);
584 case LFUN_PROTECTEDSPACE:
585 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
586 bv->lockedInsetStoreUndo(Undo::INSERT);
587 mathcursor->insert(new MathSpaceInset(1));
588 updateLocal(bv, true);
592 bv->owner()->message(_("Invalid action in math mode!"));
596 case LFUN_MATH_HALIGN:
598 bv->lockedInsetStoreUndo(Undo::INSERT);
599 lyxerr << "handling halign '" << arg << "'\n";
601 MathArrayInset * p = matrixpar(idx);
604 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
605 updateLocal(bv, true);
609 case LFUN_MATH_VALIGN:
611 bv->lockedInsetStoreUndo(Undo::INSERT);
612 lyxerr << "handling valign '" << arg << "'\n";
614 MathArrayInset * p = matrixpar(idx);
617 p->valign(arg.size() ? arg[0] : 'c');
618 updateLocal(bv, true);
622 case LFUN_MATH_ROW_INSERT:
624 bv->lockedInsetStoreUndo(Undo::INSERT);
626 MathArrayInset * p = matrixpar(idx);
627 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
630 p->addRow(p->row(idx));
631 updateLocal(bv, true);
635 case LFUN_MATH_ROW_DELETE:
637 bv->lockedInsetStoreUndo(Undo::INSERT);
639 MathArrayInset * p = matrixpar(idx);
640 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
643 p->delRow(p->row(idx));
644 updateLocal(bv, true);
648 case LFUN_MATH_COLUMN_INSERT:
650 bv->lockedInsetStoreUndo(Undo::INSERT);
652 MathArrayInset * p = matrixpar(idx);
655 p->addCol(p->col(idx));
656 updateLocal(bv, true);
660 case LFUN_MATH_COLUMN_DELETE:
662 bv->lockedInsetStoreUndo(Undo::INSERT);
664 MathArrayInset * p = matrixpar(idx);
667 p->delCol(p->col(idx));
668 updateLocal(bv, true);
672 case LFUN_EXEC_COMMAND:
673 result = UNDISPATCHED;
677 case LFUN_INSERT_MATH:
678 case LFUN_SELFINSERT:
680 bv->lockedInsetStoreUndo(Undo::INSERT);
681 mathcursor->interpret(arg);
682 updateLocal(bv, true);
686 case LFUN_MATH_PANEL:
687 result = UNDISPATCHED;
691 lyxerr << "Closed by action " << action << endl;
695 mathcursor->normalize();
697 if (was_macro != mathcursor->inMacroMode()
698 && action >= 0 && action != LFUN_BACKSPACE)
699 updateLocal(bv, true);
701 if (mathcursor->selection() || was_selection)
702 toggleInsetSelection(bv);
704 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
705 result == UNDISPATCHED)
708 bv->unlockInset(this);
710 return result; // original version
714 Inset::Code InsetFormulaBase::lyxCode() const
716 return Inset::MATH_CODE;
720 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
722 if (bv->available()) {
723 // use selection if available..
725 //if (action == LFUN_MATH_IMPORT_SELECTION)
729 string sel = bv->getLyXText()->selectionAsString(bv->buffer());
731 InsetFormulaBase * f;
733 f = new InsetFormula;
734 if (openNewInset(bv, f)) {
735 // don't do that also for LFUN_MATH_MODE unless you want end up with
736 // always changing to mathrm when opening an inlined inset
737 // -- I really hate "LyXfunc overloading"...
739 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
740 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
743 // create a macro if we see "\\newcommand" somewhere, and an ordinary
745 if (sel.find("\\newcommand") == string::npos)
746 f = new InsetFormula(sel);
748 f = new InsetFormulaMacro(sel);
749 bv->getLyXText()->cutSelection(bv);
753 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
756 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
758 mathDispatchCreation(bv, arg, true);
762 void mathDispatchMathMode(BufferView * bv, string const & arg)
764 mathDispatchCreation(bv, arg, false);
768 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
770 mathDispatchCreation(bv, arg, true);
774 void mathDispatchMathMacro(BufferView * bv, string const & arg)
776 if (bv->available()) {
778 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
781 string const s1 = token(s, ' ', 1);
782 int const na = s1.empty() ? 0 : lyx::atoi(s1);
783 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
789 void mathDispatchMathDelim(BufferView * bv, string const & arg)
791 if (bv->available()) {
792 if (openNewInset(bv, new InsetFormula))
793 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
798 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
800 if (bv->available()) {
801 if (openNewInset(bv, new InsetFormula))
802 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
807 void mathDispatchInsertMath(BufferView * bv, string const & arg)
809 if (bv->available()) {
810 if (arg.size() && arg[0] == '\\')
811 openNewInset(bv, new InsetFormula(arg));
813 mathDispatchMathMode(bv, arg);