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()
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);
223 void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
228 if (isCursorVisible())
229 bv->hideLockedInsetCursor();
233 mathcursor->getPos(x, y);
239 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
240 bv->showLockedInsetCursor(x, y, asc, des);
243 toggleCursorVisible();
247 void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
249 if (!isCursorVisible()) {
253 mathcursor->getPos(x, y);
258 math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
259 bv->fitLockedInsetCursor(x, y, asc, des);
261 toggleInsetCursor(bv);
266 void InsetFormulaBase::hideInsetCursor(BufferView * bv)
268 if (isCursorVisible())
269 toggleInsetCursor(bv);
273 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
276 bv->updateInset(this, false);
280 vector<string> const InsetFormulaBase::getLabelList() const
282 return std::vector<string>();
286 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
289 bv->updateInset(this, dirty);
293 void InsetFormulaBase::insetButtonRelease(BufferView * bv,
294 int x, int y, int /*button*/)
300 mathcursor->setPos(x, y);
307 bv->updateInset(this, false);
312 void InsetFormulaBase::insetButtonPress(BufferView * bv,
313 int x, int y, int /*button*/)
318 if (mathcursor && mathcursor->selection()) {
319 mathcursor->selClear();
320 bv->updateInset(this, false);
325 void InsetFormulaBase::insetMotionNotify(BufferView * bv,
326 int x, int y, int /*button*/)
328 if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
331 mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo());
332 mathcursor->selStart();
334 mathcursor->getPos(sel_x, sel_y);
335 } else if (sel_flag) {
339 mathcursor->setPos(x, y);
341 mathcursor->getPos(x, y);
342 if (sel_x != x || sel_y != y)
343 bv->updateInset(this, false);
350 void InsetFormulaBase::insetKeyPress(XKeyEvent *)
352 lyxerr[Debug::MATHED] << "Used InsetFormulaBase::InsetKeyPress." << endl;
356 int greek_kb_flag = 0;
358 UpdatableInset::RESULT
359 InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
362 //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action
363 // << " arg: '" << arg << "' cursor: " << mathcursor << "\n";
369 RESULT result = DISPATCHED;
371 bool was_macro = mathcursor->inMacroMode();
372 bool was_selection = mathcursor->selection();
376 mathcursor->normalize();
380 // --- Cursor Movements ---------------------------------------------
383 sel = true; // fall through...
386 result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
387 updateLocal(bv, false);
392 sel = true; // fall through
395 result = mathcursor->left(sel) ? DISPATCHED : FINISHED;
396 updateLocal(bv, false);
404 result = mathcursor->up(sel) ? DISPATCHED : FINISHED_UP;
405 updateLocal(bv, false);
413 result = mathcursor->down(sel) ? DISPATCHED : FINISHED_DOWN;
414 updateLocal(bv, false);
421 mathcursor->home(sel);
422 updateLocal(bv, false);
429 mathcursor->end(sel);
430 updateLocal(bv, false);
433 case LFUN_DELETE_LINE_FORWARD:
434 bv->lockedInsetStoreUndo(Undo::DELETE);
435 mathcursor->delLine();
436 updateLocal(bv, true);
440 mathcursor->idxNext();
441 updateLocal(bv, false);
445 mathcursor->idxPrev();
446 updateLocal(bv, false);
450 bv->lockedInsetStoreUndo(Undo::EDIT);
451 mathcursor->splitCell();
452 updateLocal(bv, true);
456 bv->lockedInsetStoreUndo(Undo::DELETE);
457 mathcursor->backspace();
458 bv->updateInset(this, true);
462 bv->lockedInsetStoreUndo(Undo::DELETE);
464 bv->updateInset(this, true);
468 // sprintf(dispatch_buffer, "%d %d",);
469 // dispatch_result = dispatch_buffer;
472 lyxerr << "LFUN_SETXY broken!\n";
477 istringstream is(arg.c_str());
479 par()->getXY(x1, y1);
480 mathcursor->setPos(x1 + x, y1 + y);
481 updateLocal(bv, false);
487 mathcursor->macroModeClose();
488 bv->lockedInsetStoreUndo(Undo::INSERT);
489 mathcursor->selPaste();
490 updateLocal(bv, true);
494 bv->lockedInsetStoreUndo(Undo::DELETE);
495 mathcursor->selCut();
496 updateLocal(bv, true);
500 mathcursor->selCopy();
503 case LFUN_WORDRIGHTSEL:
504 case LFUN_WORDLEFTSEL:
507 // --- accented characters ------------------------------
509 case LFUN_UMLAUT: handleAccent(bv, "ddot"); break;
510 case LFUN_CIRCUMFLEX: handleAccent(bv, "hat"); break;
511 case LFUN_GRAVE: handleAccent(bv, "grave"); break;
512 case LFUN_ACUTE: handleAccent(bv, "acute"); break;
513 case LFUN_TILDE: handleAccent(bv, "tilde"); break;
514 case LFUN_MACRON: handleAccent(bv, "bar"); break;
515 case LFUN_DOT: handleAccent(bv, "dot"); break;
516 case LFUN_CARON: handleAccent(bv, "check"); break;
517 case LFUN_BREVE: handleAccent(bv, "breve"); break;
518 case LFUN_VECTOR: handleAccent(bv, "vec"); break;
521 case LFUN_GREEK: handleFont(bv, LM_TC_GREEK1); break;
522 case LFUN_GREEK_TOGGLE: handleFont(bv, LM_TC_GREEK); break;
523 case LFUN_BOLD: handleFont(bv, LM_TC_BF); break;
524 case LFUN_SANS: handleFont(bv, LM_TC_SF); break;
525 case LFUN_EMPH: handleFont(bv, LM_TC_CAL); break;
526 case LFUN_ROMAN: handleFont(bv, LM_TC_RM); break;
527 case LFUN_CODE: handleFont(bv, LM_TC_TT); break;
528 case LFUN_NOUN: handleFont(bv, LM_TC_BB); break;
529 case LFUN_DEFAULT: handleFont(bv, LM_TC_VAR); break;
532 handleFont(bv, LM_TC_TEXTRM);
533 //bv->owner()->message(_("math text mode toggled"));
536 case LFUN_MATH_LIMITS:
537 bv->lockedInsetStoreUndo(Undo::INSERT);
538 if (mathcursor->toggleLimits())
539 updateLocal(bv, true);
544 bv->lockedInsetStoreUndo(Undo::INSERT);
545 latexkeys const * l = in_word_set(arg);
546 mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
547 updateLocal(bv, true);
551 case LFUN_INSERT_MATRIX:
553 bv->lockedInsetStoreUndo(Undo::INSERT);
554 mathcursor->interpret("matrix " + arg);
555 updateLocal(bv, true);
559 case LFUN_MATH_SPACE:
561 bv->lockedInsetStoreUndo(Undo::EDIT);
562 //MathSpaceInset * p = mathcursor->prevSpaceInset();
566 // mathcursor->insert(new MathSpaceInset(1));
567 mathcursor->insert(new MathSpaceInset(1));
568 updateLocal(bv, true);
572 case LFUN_MATH_DELIM:
574 bv->lockedInsetStoreUndo(Undo::INSERT);
575 //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
578 istringstream is(arg.c_str());
580 latexkeys const * l = in_word_set(ls);
581 latexkeys const * r = in_word_set(rs);
582 if (!is || !l || !r) {
583 lyxerr << "can't parse delimeters from '" << arg << "'\n";
586 bv->lockedInsetStoreUndo(Undo::EDIT);
587 mathcursor->handleDelim(l, r);
588 updateLocal(bv, true);
592 case LFUN_PROTECTEDSPACE:
593 //lyxerr << " called LFUN_PROTECTEDSPACE\n";
594 bv->lockedInsetStoreUndo(Undo::INSERT);
595 mathcursor->insert(new MathSpaceInset(1));
596 updateLocal(bv, true);
600 bv->owner()->message(_("Invalid action in math mode!"));
604 case LFUN_MATH_HALIGN:
606 bv->lockedInsetStoreUndo(Undo::INSERT);
607 lyxerr << "handling halign '" << arg << "'\n";
609 MathArrayInset * p = matrixpar(idx);
612 p->halign(arg.size() ? arg[0] : 'c', p->col(idx));
613 updateLocal(bv, true);
617 case LFUN_MATH_VALIGN:
619 bv->lockedInsetStoreUndo(Undo::INSERT);
620 lyxerr << "handling valign '" << arg << "'\n";
622 MathArrayInset * p = matrixpar(idx);
625 p->valign(arg.size() ? arg[0] : 'c');
626 updateLocal(bv, true);
630 case LFUN_MATH_ROW_INSERT:
632 bv->lockedInsetStoreUndo(Undo::INSERT);
634 MathArrayInset * p = matrixpar(idx);
635 lyxerr << " calling LFUN_MATH_ROW_INSERT on " << p << endl;
638 p->addRow(p->row(idx));
639 updateLocal(bv, true);
643 case LFUN_MATH_ROW_DELETE:
645 bv->lockedInsetStoreUndo(Undo::INSERT);
647 MathArrayInset * p = matrixpar(idx);
648 lyxerr << " calling LFUN_MATH_ROW_DELETE on " << p << endl;
651 p->delRow(p->row(idx));
652 updateLocal(bv, true);
656 case LFUN_MATH_COLUMN_INSERT:
658 bv->lockedInsetStoreUndo(Undo::INSERT);
660 MathArrayInset * p = matrixpar(idx);
663 p->addCol(p->col(idx));
664 updateLocal(bv, true);
668 case LFUN_MATH_COLUMN_DELETE:
670 bv->lockedInsetStoreUndo(Undo::INSERT);
672 MathArrayInset * p = matrixpar(idx);
675 p->delCol(p->col(idx));
676 updateLocal(bv, true);
680 case LFUN_EXEC_COMMAND:
681 result = UNDISPATCHED;
685 case LFUN_INSERT_MATH:
686 case LFUN_SELFINSERT:
688 bv->lockedInsetStoreUndo(Undo::INSERT);
689 mathcursor->interpret(arg);
690 updateLocal(bv, true);
694 case LFUN_MATH_PANEL:
695 result = UNDISPATCHED;
699 lyxerr << "Closed by action " << action << endl;
700 result = FINISHED_RIGHT;
703 mathcursor->normalize();
705 if (was_macro != mathcursor->inMacroMode()
706 && action >= 0 && action != LFUN_BACKSPACE)
707 updateLocal(bv, true);
709 if (mathcursor->selection() || was_selection)
710 toggleInsetSelection(bv);
712 if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
713 result == UNDISPATCHED)
716 bv->unlockInset(this);
718 return result; // original version
722 Inset::Code InsetFormulaBase::lyxCode() const
724 return Inset::MATH_CODE;
728 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
730 if (bv->available()) {
731 // use selection if available..
733 //if (action == LFUN_MATH_IMPORT_SELECTION)
737 string sel = bv->getLyXText()->selectionAsString(bv->buffer());
739 InsetFormulaBase * f;
741 f = new InsetFormula;
742 if (openNewInset(bv, f)) {
743 // don't do that also for LFUN_MATH_MODE unless you want end up with
744 // always changing to mathrm when opening an inlined inset
745 // -- I really hate "LyXfunc overloading"...
747 f->localDispatch(bv, LFUN_MATH_DISPLAY, string());
748 f->localDispatch(bv, LFUN_INSERT_MATH, arg);
751 // create a macro if we see "\\newcommand" somewhere, and an ordinary
753 if (sel.find("\\newcommand") == string::npos)
754 f = new InsetFormula(sel);
756 f = new InsetFormulaMacro(sel);
757 bv->getLyXText()->cutSelection(bv);
761 bv->owner()->getLyXFunc()->setMessage(N_("Math editor mode"));
765 void mathDispatchMathDisplay(BufferView * bv, string const & arg)
767 mathDispatchCreation(bv, arg, true);
771 void mathDispatchMathMode(BufferView * bv, string const & arg)
773 mathDispatchCreation(bv, arg, false);
777 void mathDispatchMathImportSelection(BufferView * bv, string const & arg)
779 mathDispatchCreation(bv, arg, true);
783 void mathDispatchMathMacro(BufferView * bv, string const & arg)
785 if (bv->available()) {
787 bv->owner()->getLyXFunc()->setErrorMessage(N_("Missing argument"));
790 string const s1 = token(s, ' ', 1);
791 int const na = s1.empty() ? 0 : lyx::atoi(s1);
792 openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
798 void mathDispatchMathDelim(BufferView * bv, string const & arg)
800 if (bv->available()) {
801 if (openNewInset(bv, new InsetFormula))
802 bv->theLockingInset()->localDispatch(bv, LFUN_MATH_DELIM, arg);
807 void mathDispatchInsertMatrix(BufferView * bv, string const & arg)
809 if (bv->available()) {
810 if (openNewInset(bv, new InsetFormula))
811 bv->theLockingInset()->localDispatch(bv, LFUN_INSERT_MATRIX, arg);
816 void mathDispatchInsertMath(BufferView * bv, string const & arg)
818 if (bv->available()) {
819 if (arg.size() && arg[0] == '\\')
820 openNewInset(bv, new InsetFormula(arg));
822 mathDispatchMathMode(bv, arg);
827 void mathDispatchGreek(BufferView * bv, string const & arg)
829 if (bv->available()) {
830 InsetFormula * f = new InsetFormula;
831 if (openNewInset(bv, f)) {
832 bv->theLockingInset()->localDispatch(bv, LFUN_GREEK, string());
833 bv->theLockingInset()->localDispatch(bv, LFUN_SELFINSERT, arg);