2 * \file InsetMathNest.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetMathNest.h"
15 #include "InsetMathArray.h"
16 #include "InsetMathBig.h"
17 #include "InsetMathBox.h"
18 #include "InsetMathBrace.h"
19 #include "InsetMathColor.h"
20 #include "InsetMathComment.h"
21 #include "InsetMathDelim.h"
22 #include "InsetMathHull.h"
23 //#include "InsetMathMBox.h"
24 #include "InsetMathRef.h"
25 #include "InsetMathScript.h"
26 #include "InsetMathSpace.h"
27 #include "InsetMathSymbol.h"
28 #include "InsetMathUnknown.h"
30 #include "MathFactory.h"
31 #include "MathMacroArgument.h"
32 #include "MathParser.h"
33 #include "MathStream.h"
34 #include "MathSupport.h"
37 #include "bufferview_funcs.h"
38 #include "BufferView.h"
40 #include "CoordCache.h"
42 #include "CutAndPaste.h"
44 #include "DispatchResult.h"
45 #include "FuncRequest.h"
46 #include "FuncStatus.h"
49 #include "OutputParams.h"
52 #include "support/lstrings.h"
53 #include "support/textutils.h"
55 #include "frontends/Clipboard.h"
56 #include "frontends/Painter.h"
57 #include "frontends/Selection.h"
59 #include "FuncRequest.h"
66 using cap::copySelection;
67 using cap::grabAndEraseSelection;
68 using cap::cutSelection;
69 using cap::replaceSelection;
70 using cap::selClearOrDel;
74 using std::istringstream;
77 InsetMathNest::InsetMathNest(idx_type nargs)
78 : cells_(nargs), lock_(false), mouse_hover_(false)
82 InsetMathNest::InsetMathNest(InsetMathNest const & inset)
83 : InsetMath(inset), cells_(inset.cells_), lock_(inset.lock_),
88 InsetMathNest & InsetMathNest::operator=(InsetMathNest const & inset)
90 cells_ = inset.cells_;
93 InsetMath::operator=(inset);
98 InsetMath::idx_type InsetMathNest::nargs() const
100 return cells_.size();
104 void InsetMathNest::cursorPos(BufferView const & bv,
105 CursorSlice const & sl, bool /*boundary*/,
106 int & x, int & y) const
108 // FIXME: This is a hack. Ideally, the coord cache should not store
109 // absolute positions, but relative ones. This would mean to call
110 // setXY() not in MathData::draw(), but in the parent insets' draw()
111 // with the correctly adjusted x,y values. But this means that we'd have
112 // to touch all (math)inset's draw() methods. Right now, we'll store
113 // absolute value, and make them here relative, only to make them
114 // absolute again when actually drawing the cursor. What a mess.
115 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
116 MathData const & ar = sl.cell();
117 CoordCache const & coord_cache = bv.coordCache();
118 if (!coord_cache.getArrays().has(&ar)) {
119 // this can (semi-)legally happen if we just created this cell
120 // and it never has been drawn before. So don't ASSERT.
121 //lyxerr << "no cached data for array " << &ar << endl;
126 Point const pt = coord_cache.getArrays().xy(&ar);
127 if (!coord_cache.getInsets().has(this)) {
129 //lyxerr << "no cached data for inset " << this << endl;
134 Point const pt2 = coord_cache.getInsets().xy(this);
135 //lyxerr << "retrieving position cache for MathData "
136 // << pt.x_ << ' ' << pt.y_ << std::endl;
137 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
139 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
140 // << " asc: " << ascent() << " des: " << descent()
141 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
142 // move cursor visually into empty cells ("blue rectangles");
148 void InsetMathNest::metrics(MetricsInfo const & mi) const
151 for (idx_type i = 0, n = nargs(); i != n; ++i)
156 bool InsetMathNest::idxNext(Cursor & cur) const
158 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
159 if (cur.idx() == cur.lastidx())
167 bool InsetMathNest::idxRight(Cursor & cur) const
173 bool InsetMathNest::idxPrev(Cursor & cur) const
175 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
179 cur.pos() = cur.lastpos();
184 bool InsetMathNest::idxLeft(Cursor & cur) const
190 bool InsetMathNest::idxFirst(Cursor & cur) const
192 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
201 bool InsetMathNest::idxLast(Cursor & cur) const
203 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
206 cur.idx() = cur.lastidx();
207 cur.pos() = cur.lastpos();
212 void InsetMathNest::dump() const
214 odocstringstream oss;
216 os << "---------------------------------------------\n";
219 for (idx_type i = 0, n = nargs(); i != n; ++i)
220 os << cell(i) << "\n";
221 os << "---------------------------------------------\n";
222 lyxerr << to_utf8(oss.str());
226 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
230 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
233 setPosCache(pi, x, y);
237 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
239 BufferView & bv = *pi.base.bv;
240 // this should use the x/y values given, not the cached values
241 Cursor & cur = bv.cursor();
242 if (!cur.selection())
244 if (!ptr_cmp(&cur.inset(), this))
247 // FIXME: hack to get position cache warm
248 pi.pain.setDrawingEnabled(false);
250 pi.pain.setDrawingEnabled(true);
252 CursorSlice s1 = cur.selBegin();
253 CursorSlice s2 = cur.selEnd();
255 //lyxerr << "InsetMathNest::drawing selection: "
256 // << " s1: " << s1 << " s2: " << s2 << endl;
257 if (s1.idx() == s2.idx()) {
258 MathData const & c = cell(s1.idx());
259 int x1 = c.xo(bv) + c.pos2x(s1.pos());
260 int y1 = c.yo(bv) - c.ascent();
261 int x2 = c.xo(bv) + c.pos2x(s2.pos());
262 int y2 = c.yo(bv) + c.descent();
263 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
264 //lyxerr << "InsetMathNest::drawing selection 3: "
265 // << " x1: " << x1 << " x2: " << x2
266 // << " y1: " << y1 << " y2: " << y2 << endl;
268 for (idx_type i = 0; i < nargs(); ++i) {
269 if (idxBetween(i, s1.idx(), s2.idx())) {
270 MathData const & c = cell(i);
272 int y1 = c.yo(bv) - c.ascent();
273 int x2 = c.xo(bv) + c.width();
274 int y2 = c.yo(bv) + c.descent();
275 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
282 void InsetMathNest::validate(LaTeXFeatures & features) const
284 for (idx_type i = 0; i < nargs(); ++i)
285 cell(i).validate(features);
289 void InsetMathNest::replace(ReplaceData & rep)
291 for (idx_type i = 0; i < nargs(); ++i)
292 cell(i).replace(rep);
296 bool InsetMathNest::contains(MathData const & ar) const
298 for (idx_type i = 0; i < nargs(); ++i)
299 if (cell(i).contains(ar))
305 bool InsetMathNest::lock() const
311 void InsetMathNest::lock(bool l)
317 bool InsetMathNest::isActive() const
323 MathData InsetMathNest::glue() const
326 for (size_t i = 0; i < nargs(); ++i)
332 void InsetMathNest::write(WriteStream & os) const
334 os << '\\' << name().c_str();
335 for (size_t i = 0; i < nargs(); ++i)
336 os << '{' << cell(i) << '}';
338 os.pendingSpace(true);
339 if (lock_ && !os.latex()) {
341 os.pendingSpace(true);
346 void InsetMathNest::normalize(NormalStream & os) const
348 os << '[' << name().c_str();
349 for (size_t i = 0; i < nargs(); ++i)
350 os << ' ' << cell(i);
355 int InsetMathNest::latex(Buffer const &, odocstream & os,
356 OutputParams const & runparams) const
358 WriteStream wi(os, runparams.moving_arg, true);
364 bool InsetMathNest::setMouseHover(bool mouse_hover)
366 mouse_hover_ = mouse_hover;
371 bool InsetMathNest::notifyCursorLeaves(Cursor & /*cur*/)
375 MathData & ar = cur.cell();
376 // remove base-only "scripts"
377 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
378 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
379 if (p && p->nargs() == 1) {
380 MathData ar = p->nuc();
383 cur.adjust(i, ar.size() - 1);
387 // glue adjacent font insets of the same kind
388 for (pos_type i = 0; i + 1 < size(); ++i) {
389 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
390 InsetMathFont const * q = operator[](i + 1)->asFontInset();
391 if (p && q && p->name() == q->name()) {
392 p->cell(0).append(q->cell(0));
402 void InsetMathNest::handleFont
403 (Cursor & cur, docstring const & arg, char const * const font)
405 handleFont(cur, arg, from_ascii(font));
409 void InsetMathNest::handleFont
410 (Cursor & cur, docstring const & arg, docstring const & font)
412 // this whole function is a hack and won't work for incremental font
415 if (cur.inset().asInsetMath()->name() == font) {
416 recordUndoInset(cur, Undo::ATOMIC);
417 cur.handleFont(to_utf8(font));
419 recordUndo(cur, Undo::ATOMIC);
420 cur.handleNest(createInsetMath(font));
426 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
428 recordUndo(cur, Undo::ATOMIC);
431 bv_funcs::string2font(to_utf8(arg), font, b);
432 if (font.color() != Color::inherit) {
433 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
434 cur.handleNest(at, 0);
439 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
441 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
442 //CursorSlice sl = cur.current();
444 switch (cmd.action) {
448 cur.message(_("Paste"));
449 replaceSelection(cur);
451 if (cmd.argument().empty() && !theClipboard().isInternal())
452 topaste = theClipboard().getAsText();
455 idocstringstream is(cmd.argument());
457 topaste = cap::getSelection(cur.buffer(), n);
459 cur.niceInsert(topaste);
460 cur.clearSelection(); // bug 393
467 cutSelection(cur, true, true);
468 cur.message(_("Cut"));
469 // Prevent stale position >= size crash
470 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
476 cur.message(_("Copy"));
479 case LFUN_MOUSE_PRESS:
480 lfunMousePress(cur, cmd);
483 case LFUN_MOUSE_MOTION:
484 lfunMouseMotion(cur, cmd);
487 case LFUN_MOUSE_RELEASE:
488 lfunMouseRelease(cur, cmd);
491 case LFUN_FINISHED_LEFT:
492 cur.bv().cursor() = cur;
495 case LFUN_FINISHED_RIGHT:
497 cur.bv().cursor() = cur;
500 case LFUN_CHAR_FORWARD:
501 cur.updateFlags(Update::Decoration | Update::FitCursor);
502 case LFUN_CHAR_FORWARD_SELECT:
503 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
504 cur.autocorrect() = false;
506 cur.macroModeClose();
507 if (reverseDirectionNeeded(cur))
508 goto goto_char_backwards;
511 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
512 cur.pushLeft(*cur.nextAtom().nucleus());
513 cur.inset().idxFirst(cur);
514 } else if (cur.posRight() || idxRight(cur)
515 || cur.popRight() || cur.selection())
518 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
523 case LFUN_CHAR_BACKWARD:
524 cur.updateFlags(Update::Decoration | Update::FitCursor);
525 case LFUN_CHAR_BACKWARD_SELECT:
526 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
527 cur.autocorrect() = false;
529 cur.macroModeClose();
530 if (reverseDirectionNeeded(cur))
531 goto goto_char_forwards;
534 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
536 cur.push(*cur.nextAtom().nucleus());
537 cur.inset().idxLast(cur);
538 } else if (cur.posLeft() || idxLeft(cur)
539 || cur.popLeft() || cur.selection())
542 cmd = FuncRequest(LFUN_FINISHED_LEFT);
549 cur.updateFlags(Update::Decoration | Update::FitCursor);
550 case LFUN_DOWN_SELECT:
551 case LFUN_UP_SELECT: {
552 // close active macro
553 if (cur.inMacroMode()) {
554 cur.macroModeClose();
558 // stop/start the selection
559 bool select = cmd.action == LFUN_DOWN_SELECT ||
560 cmd.action == LFUN_UP_SELECT;
561 cur.selHandle(select);
564 bool up = cmd.action == LFUN_UP || cmd.action == LFUN_UP_SELECT;
565 bool successful = cur.upDownInMath(up);
567 // notify left insets and give them chance to set update flags
568 lyx::notifyCursorLeaves(cur.beforeDispatchCursor(), cur);
573 if (cur.fixIfBroken())
574 // FIXME: Something bad happened. We pass the corrected Cursor
575 // instead of letting things go worse.
578 // We did not manage to move the cursor.
583 case LFUN_MOUSE_DOUBLE:
584 case LFUN_MOUSE_TRIPLE:
585 case LFUN_WORD_SELECT:
589 cur.selection() = true;
590 cur.pos() = cur.lastpos();
591 cur.idx() = cur.lastidx();
594 case LFUN_PARAGRAPH_UP:
595 case LFUN_PARAGRAPH_DOWN:
596 cur.updateFlags(Update::Decoration | Update::FitCursor);
597 case LFUN_PARAGRAPH_UP_SELECT:
598 case LFUN_PARAGRAPH_DOWN_SELECT:
601 case LFUN_LINE_BEGIN:
602 case LFUN_WORD_BACKWARD:
603 cur.updateFlags(Update::Decoration | Update::FitCursor);
604 case LFUN_LINE_BEGIN_SELECT:
605 case LFUN_WORD_BACKWARD_SELECT:
606 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
607 cmd.action == LFUN_LINE_BEGIN_SELECT);
608 cur.macroModeClose();
609 if (cur.pos() != 0) {
611 } else if (cur.col() != 0) {
612 cur.idx() -= cur.col();
614 } else if (cur.idx() != 0) {
618 cmd = FuncRequest(LFUN_FINISHED_LEFT);
623 case LFUN_WORD_FORWARD:
625 cur.updateFlags(Update::Decoration | Update::FitCursor);
626 case LFUN_WORD_FORWARD_SELECT:
627 case LFUN_LINE_END_SELECT:
628 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
629 cmd.action == LFUN_LINE_END_SELECT);
630 cur.macroModeClose();
632 if (cur.pos() != cur.lastpos()) {
633 cur.pos() = cur.lastpos();
634 } else if (ncols() && (cur.col() != cur.lastcol())) {
635 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
636 cur.pos() = cur.lastpos();
637 } else if (cur.idx() != cur.lastidx()) {
638 cur.idx() = cur.lastidx();
639 cur.pos() = cur.lastpos();
641 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
646 case LFUN_SCREEN_UP_SELECT:
647 cmd = FuncRequest(LFUN_FINISHED_LEFT);
651 case LFUN_SCREEN_DOWN_SELECT:
652 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
656 case LFUN_CELL_FORWARD:
657 cur.updateFlags(Update::Decoration | Update::FitCursor);
658 cur.inset().idxNext(cur);
661 case LFUN_CELL_BACKWARD:
662 cur.updateFlags(Update::Decoration | Update::FitCursor);
663 cur.inset().idxPrev(cur);
666 case LFUN_WORD_DELETE_BACKWARD:
667 case LFUN_CHAR_DELETE_BACKWARD:
669 // May affect external cell:
670 recordUndoInset(cur, Undo::ATOMIC);
672 recordUndo(cur, Undo::ATOMIC);
673 // if the inset can not be removed from within, delete it
674 if (!cur.backspace()) {
675 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
676 cur.innerText()->dispatch(cur, cmd);
680 case LFUN_WORD_DELETE_FORWARD:
681 case LFUN_CHAR_DELETE_FORWARD:
682 if (cur.pos() == cur.lastpos())
683 // May affect external cell:
684 recordUndoInset(cur, Undo::ATOMIC);
686 recordUndo(cur, Undo::ATOMIC);
687 // if the inset can not be removed from within, delete it
689 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
690 cur.innerText()->dispatch(cur, cmd);
696 cur.clearSelection();
698 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
703 case LFUN_INSET_TOGGLE:
709 case LFUN_SELF_INSERT:
710 if (cmd.argument().size() != 1) {
712 docstring const arg = cmd.argument();
713 if (!interpretString(cur, arg))
717 // Don't record undo steps if we are in macro mode and
718 // cmd.argument is the next character of the macro name.
719 // Otherwise we'll get an invalid cursor if we undo after
720 // the macro was finished and the macro is a known command,
721 // e.g. sqrt. Cursor::macroModeClose replaces in this case
722 // the InsetMathUnknown with name "frac" by an empty
723 // InsetMathFrac -> a pos value > 0 is invalid.
724 // A side effect is that an undo before the macro is finished
725 // undoes the complete macro, not only the last character.
726 if (!cur.inMacroMode())
729 // spacial handling of space. If we insert an inset
730 // via macro mode, we want to put the cursor inside it
731 // if relevant. Think typing "\frac<space>".
732 if (cmd.argument()[0] == ' '
733 && cur.inMacroMode() && cur.macroName() != "\\"
734 && cur.macroModeClose()) {
735 MathAtom const atom = cur.prevAtom();
736 if (atom->asNestInset() && atom->isActive()) {
738 cur.pushLeft(*cur.nextInset());
740 } else if (!interpretChar(cur, cmd.argument()[0])) {
741 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
746 //case LFUN_SERVER_GET_XY:
747 // sprintf(dispatch_buffer, "%d %d",);
750 case LFUN_SERVER_SET_XY: {
751 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
754 istringstream is(to_utf8(cmd.argument()));
756 cur.setScreenPos(x, y);
760 // Special casing for superscript in case of LyX handling
762 case LFUN_ACCENT_CIRCUMFLEX:
763 if (cmd.argument().empty()) {
764 // do superscript if LyX handles
766 recordUndo(cur, Undo::ATOMIC);
767 script(cur, true, grabAndEraseSelection(cur));
771 case LFUN_ACCENT_UMLAUT:
772 case LFUN_ACCENT_ACUTE:
773 case LFUN_ACCENT_GRAVE:
774 case LFUN_ACCENT_BREVE:
775 case LFUN_ACCENT_DOT:
776 case LFUN_ACCENT_MACRON:
777 case LFUN_ACCENT_CARON:
778 case LFUN_ACCENT_TILDE:
779 case LFUN_ACCENT_CEDILLA:
780 case LFUN_ACCENT_CIRCLE:
781 case LFUN_ACCENT_UNDERDOT:
782 case LFUN_ACCENT_TIE:
783 case LFUN_ACCENT_OGONEK:
784 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
788 case LFUN_FONT_FREE_APPLY:
789 case LFUN_FONT_FREE_UPDATE:
790 handleFont2(cur, cmd.argument());
794 if (currentMode() == TEXT_MODE)
795 handleFont(cur, cmd.argument(), "textbf");
797 handleFont(cur, cmd.argument(), "mathbf");
800 if (currentMode() == TEXT_MODE)
801 handleFont(cur, cmd.argument(), "textsf");
803 handleFont(cur, cmd.argument(), "mathsf");
806 if (currentMode() == TEXT_MODE)
807 handleFont(cur, cmd.argument(), "emph");
809 handleFont(cur, cmd.argument(), "mathcal");
811 case LFUN_FONT_ROMAN:
812 if (currentMode() == TEXT_MODE)
813 handleFont(cur, cmd.argument(), "textrm");
815 handleFont(cur, cmd.argument(), "mathrm");
817 case LFUN_FONT_TYPEWRITER:
818 if (currentMode() == TEXT_MODE)
819 handleFont(cur, cmd.argument(), "texttt");
821 handleFont(cur, cmd.argument(), "mathtt");
824 handleFont(cur, cmd.argument(), "mathfrak");
827 if (currentMode() == TEXT_MODE)
828 handleFont(cur, cmd.argument(), "textit");
830 handleFont(cur, cmd.argument(), "mathit");
833 if (currentMode() == TEXT_MODE)
834 // FIXME: should be "noun"
835 handleFont(cur, cmd.argument(), "textsc");
837 handleFont(cur, cmd.argument(), "mathbb");
840 case LFUN_FONT_FREE_APPLY:
841 handleFont(cur, cmd.argument(), "textrm");
844 case LFUN_FONT_DEFAULT:
845 handleFont(cur, cmd.argument(), "textnormal");
848 case LFUN_MATH_MODE: {
850 // ignore math-mode on when already in math mode
851 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
853 cur.macroModeClose();
854 docstring const save_selection = grabAndEraseSelection(cur);
856 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
857 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
859 cur.pushLeft(*cur.nextInset());
860 cur.niceInsert(save_selection);
862 if (currentMode() == Inset::TEXT_MODE) {
863 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
864 cur.message(_("create new math text environment ($...$)"));
866 handleFont(cur, cmd.argument(), "textrm");
867 cur.message(_("entered math text mode (textrm)"));
880 case LFUN_MATH_MATRIX: {
881 recordUndo(cur, Undo::ATOMIC);
886 idocstringstream is(cmd.argument());
887 is >> m >> n >> v_align >> h_align;
894 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
898 case LFUN_MATH_DELIM: {
900 docstring rs = support::split(cmd.argument(), ls, ' ');
901 // Reasonable default values
906 recordUndo(cur, Undo::ATOMIC);
907 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
911 case LFUN_MATH_BIGDELIM: {
912 docstring const lname = from_utf8(cmd.getArg(0));
913 docstring const ldelim = from_utf8(cmd.getArg(1));
914 docstring const rname = from_utf8(cmd.getArg(2));
915 docstring const rdelim = from_utf8(cmd.getArg(3));
916 latexkeys const * l = in_word_set(lname);
917 bool const have_l = l && l->inset == "big" &&
918 InsetMathBig::isBigInsetDelim(ldelim);
919 l = in_word_set(rname);
920 bool const have_r = l && l->inset == "big" &&
921 InsetMathBig::isBigInsetDelim(rdelim);
922 // We mimic LFUN_MATH_DELIM in case we have an empty left
923 // or right delimiter.
924 if (have_l || have_r) {
925 recordUndo(cur, Undo::ATOMIC);
926 docstring const selection = grabAndEraseSelection(cur);
929 cur.insert(MathAtom(new InsetMathBig(lname,
931 cur.niceInsert(selection);
933 cur.insert(MathAtom(new InsetMathBig(rname,
936 // Don't call cur.undispatched() if we did nothing, this would
937 // lead to infinite recursion via Text::dispatch().
941 case LFUN_SPACE_INSERT:
942 case LFUN_MATH_SPACE:
943 recordUndo(cur, Undo::ATOMIC);
944 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
947 case LFUN_ERT_INSERT:
948 // interpret this as if a backslash was typed
949 recordUndo(cur, Undo::ATOMIC);
950 interpretChar(cur, '\\');
953 case LFUN_MATH_SUBSCRIPT:
954 // interpret this as if a _ was typed
955 recordUndo(cur, Undo::ATOMIC);
956 interpretChar(cur, '_');
959 case LFUN_MATH_SUPERSCRIPT:
960 // interpret this as if a ^ was typed
961 recordUndo(cur, Undo::ATOMIC);
962 interpretChar(cur, '^');
965 case LFUN_QUOTE_INSERT:
966 // interpret this as if a straight " was typed
967 recordUndo(cur, Undo::ATOMIC);
968 interpretChar(cur, '\"');
971 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
972 // handling such that "self-insert" works on "arbitrary stuff" too, and
973 // math-insert only handles special math things like "matrix".
974 case LFUN_MATH_INSERT: {
975 recordUndo(cur, Undo::ATOMIC);
976 if (cmd.argument() == "^" || cmd.argument() == "_") {
977 interpretChar(cur, cmd.argument()[0]);
979 cur.niceInsert(cmd.argument());
983 case LFUN_DIALOG_SHOW_NEW_INSET: {
984 docstring const & name = cmd.argument();
987 InsetMathRef tmp(name);
988 data = tmp.createDialogStr(to_utf8(name));
990 cur.bv().showInsetDialog(to_utf8(name), data, 0);
994 case LFUN_INSET_INSERT: {
996 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1005 InsetMath::doDispatch(cur, cmd);
1011 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1012 FuncStatus & flag) const
1014 // the font related toggles
1015 //string tc = "mathnormal";
1017 string const arg = to_utf8(cmd.argument());
1018 switch (cmd.action) {
1019 case LFUN_TABULAR_FEATURE:
1020 flag.enabled(false);
1023 case LFUN_TABULAR_FEATURE:
1024 // FIXME: check temporarily disabled
1026 char align = mathcursor::valign();
1027 if (align == '\0') {
1031 if (cmd.argument().empty()) {
1035 if (!contains("tcb", cmd.argument()[0])) {
1039 flag.setOnOff(cmd.argument()[0] == align);
1042 /// We have to handle them since 1.4 blocks all unhandled actions
1043 case LFUN_FONT_ITAL:
1044 case LFUN_FONT_BOLD:
1045 case LFUN_FONT_SANS:
1046 case LFUN_FONT_EMPH:
1047 case LFUN_FONT_TYPEWRITER:
1048 case LFUN_FONT_NOUN:
1049 case LFUN_FONT_ROMAN:
1050 case LFUN_FONT_DEFAULT:
1053 case LFUN_MATH_MUTATE:
1054 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1055 flag.setOnOff(false);
1058 // we just need to be in math mode to enable that
1059 case LFUN_MATH_SIZE:
1060 case LFUN_MATH_SPACE:
1061 case LFUN_MATH_LIMITS:
1062 case LFUN_MATH_NONUMBER:
1063 case LFUN_MATH_NUMBER:
1064 case LFUN_MATH_EXTERN:
1068 case LFUN_FONT_FRAK:
1069 flag.enabled(currentMode() != TEXT_MODE);
1072 case LFUN_MATH_INSERT: {
1073 bool const textarg =
1074 arg == "\\textbf" || arg == "\\textsf" ||
1075 arg == "\\textrm" || arg == "\\textmd" ||
1076 arg == "\\textit" || arg == "\\textsc" ||
1077 arg == "\\textsl" || arg == "\\textup" ||
1078 arg == "\\texttt" || arg == "\\textbb" ||
1079 arg == "\\textnormal";
1080 flag.enabled(currentMode() != TEXT_MODE || textarg);
1084 case LFUN_MATH_MATRIX:
1085 flag.enabled(currentMode() == MATH_MODE);
1088 case LFUN_INSET_INSERT: {
1089 // Don't test createMathInset_fromDialogStr(), since
1090 // getStatus is not called with a valid reference and the
1091 // dialog would not be applyable.
1092 string const name = cmd.getArg(0);
1093 flag.enabled(name == "ref");
1097 case LFUN_MATH_DELIM:
1098 case LFUN_MATH_BIGDELIM:
1099 // Don't do this with multi-cell selections
1100 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1103 case LFUN_HYPHENATION_POINT_INSERT:
1104 case LFUN_LIGATURE_BREAK_INSERT:
1105 case LFUN_MENU_SEPARATOR_INSERT:
1106 case LFUN_DOTS_INSERT:
1107 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1108 // FIXME: These would probably make sense in math-text mode
1109 flag.enabled(false);
1120 void InsetMathNest::edit(Cursor & cur, bool left)
1123 cur.idx() = left ? 0 : cur.lastidx();
1124 cur.pos() = left ? 0 : cur.lastpos();
1126 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1130 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1133 int dist_min = 1000000;
1134 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1135 int const d = cell(i).dist(cur.bv(), x, y);
1141 MathData & ar = cell(idx_min);
1143 cur.idx() = idx_min;
1144 cur.pos() = ar.x2pos(x - ar.xo(cur.bv()));
1146 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1147 if (dist_min == 0) {
1149 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1150 if (ar[i]->covers(cur.bv(), x, y))
1151 return ar[i].nucleus()->editXY(cur, x, y);
1157 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1159 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1160 BufferView & bv = cur.bv();
1161 bv.mouseSetCursor(cur);
1162 if (cmd.button() == mouse_button::button1) {
1163 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1164 // Update the cursor update flags as needed:
1166 // Update::Decoration: tells to update the decoration
1167 // (visual box corners that define
1169 // Update::FitCursor: adjust the screen to the cursor
1170 // position if needed
1171 // cur.result().update(): don't overwrite previously set flags.
1172 cur.updateFlags(Update::Decoration | Update::FitCursor
1173 | cur.result().update());
1174 } else if (cmd.button() == mouse_button::button2) {
1175 if (cap::selection()) {
1176 // See comment in Text::dispatch why we do this
1177 cap::copySelectionToStack();
1178 cmd = FuncRequest(LFUN_PASTE, "0");
1179 doDispatch(bv.cursor(), cmd);
1182 asArray(theSelection().get(), ar);
1183 bv.cursor().insert(ar);
1189 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1191 // only select with button 1
1192 if (cmd.button() == mouse_button::button1) {
1193 Cursor & bvcur = cur.bv().cursor();
1194 if (bvcur.anchor_.hasPart(cur)) {
1195 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1196 bvcur.setCursor(cur);
1197 bvcur.selection() = true;
1198 //lyxerr << "MOTION " << bvcur << endl;
1205 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1207 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1209 if (cmd.button() == mouse_button::button1) {
1210 if (!cur.selection())
1213 Cursor & bvcur = cur.bv().cursor();
1214 bvcur.selection() = true;
1223 bool InsetMathNest::interpretChar(Cursor & cur, char_type c)
1225 //lyxerr << "interpret 2: '" << c << "'" << endl;
1226 docstring save_selection;
1227 if (c == '^' || c == '_')
1228 save_selection = grabAndEraseSelection(cur);
1233 if (cur.inMacroMode()) {
1234 docstring name = cur.macroName();
1236 /// are we currently typing '#1' or '#2' or...?
1237 if (name == "\\#") {
1240 if (n >= 1 && n <= 9)
1241 cur.insert(new MathMacroArgument(n));
1245 if (isAlphaASCII(c)) {
1246 cur.activeMacro()->setName(name + docstring(1, c));
1250 // handle 'special char' macros
1255 if (currentMode() == InsetMath::TEXT_MODE)
1256 cur.niceInsert(createInsetMath("textbackslash"));
1258 cur.niceInsert(createInsetMath("backslash"));
1259 } else if (c == '{') {
1261 cur.niceInsert(MathAtom(new InsetMathBrace));
1262 } else if (c == '%') {
1264 cur.niceInsert(MathAtom(new InsetMathComment));
1265 } else if (c == '#') {
1266 BOOST_ASSERT(cur.activeMacro());
1267 cur.activeMacro()->setName(name + docstring(1, c));
1270 cur.niceInsert(createInsetMath(docstring(1, c)));
1275 // One character big delimiters. The others are handled in
1276 // interpretString().
1277 latexkeys const * l = in_word_set(name.substr(1));
1278 if (name[0] == '\\' && l && l->inset == "big") {
1282 delim = from_ascii("\\{");
1285 delim = from_ascii("\\}");
1288 delim = docstring(1, c);
1291 if (InsetMathBig::isBigInsetDelim(delim)) {
1292 // name + delim ared a valid InsetMathBig.
1293 // We can't use cur.macroModeClose() because
1294 // it does not handle delim.
1295 InsetMathUnknown * p = cur.activeMacro();
1298 cur.cell().erase(cur.pos());
1299 cur.plainInsert(MathAtom(
1300 new InsetMathBig(name.substr(1), delim)));
1305 // leave macro mode and try again if necessary
1306 cur.macroModeClose();
1308 cur.niceInsert(MathAtom(new InsetMathBrace));
1310 interpretChar(cur, c);
1314 // This is annoying as one has to press <space> far too often.
1318 // leave autocorrect mode if necessary
1319 if (autocorrect() && c == ' ') {
1320 autocorrect() = false;
1325 // just clear selection on pressing the space bar
1326 if (cur.selection() && c == ' ') {
1327 cur.selection() = false;
1334 //lyxerr << "starting with macro" << endl;
1335 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), false)));
1340 if (currentMode() == InsetMath::TEXT_MODE)
1346 if (currentMode() == InsetMath::TEXT_MODE) {
1347 // insert spaces in text mode,
1348 // but suppress direct insertion of two spaces in a row
1349 // the still allows typing '<space>a<space>' and deleting the 'a', but
1350 // it is better than nothing...
1351 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1353 // FIXME: we have to enable full redraw here because of the
1354 // visual box corners that define the inset. If we know for
1355 // sure that we stay within the same cell we can optimize for
1357 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1361 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1362 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1363 // FIXME: we have to enable full redraw here because of the
1364 // visual box corners that define the inset. If we know for
1365 // sure that we stay within the same cell we can optimize for
1367 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1371 if (cur.popRight()) {
1372 // FIXME: we have to enable full redraw here because of the
1373 // visual box corners that define the inset. If we know for
1374 // sure that we stay within the same cell we can optimize for
1376 //cur.updateFlags(Update::FitCursor);
1380 // if we are at the very end, leave the formula
1381 return cur.pos() != cur.lastpos();
1384 // These shouldn't work in text mode:
1385 if (currentMode() != InsetMath::TEXT_MODE) {
1387 script(cur, false, save_selection);
1391 script(cur, true, save_selection);
1395 cur.niceInsert(createInsetMath("sim"));
1400 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1401 c == '%' || c == '_' || c == '^') {
1402 cur.niceInsert(createInsetMath(docstring(1, c)));
1407 // try auto-correction
1408 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1411 // no special circumstances, so insert the character without any fuss
1413 cur.autocorrect() = true;
1418 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1420 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1422 if (!cur.empty() && cur.pos() > 0 &&
1423 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1424 if (InsetMathBig::isBigInsetDelim(str)) {
1425 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1426 if (prev[0] == '\\') {
1427 prev = prev.substr(1);
1428 latexkeys const * l = in_word_set(prev);
1429 if (l && l->inset == "big") {
1430 cur.cell()[cur.pos() - 1] =
1431 MathAtom(new InsetMathBig(prev, str));
1441 bool InsetMathNest::script(Cursor & cur, bool up,
1442 docstring const & save_selection)
1444 // Hack to get \^ and \_ working
1445 //lyxerr << "handling script: up: " << up << endl;
1446 if (cur.inMacroMode() && cur.macroName() == "\\") {
1448 cur.niceInsert(createInsetMath("mathcircumflex"));
1450 interpretChar(cur, '_');
1454 cur.macroModeClose();
1455 if (asScriptInset() && cur.idx() == 0) {
1456 // we are in a nucleus of a script inset, move to _our_ script
1457 InsetMathScript * inset = asScriptInset();
1458 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1460 cur.idx() = inset->idxOfScript(up);
1462 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1464 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1467 cur.idx() = inset->idxOfScript(up);
1468 cur.pos() = cur.lastpos();
1470 // convert the thing to our left to a scriptinset or create a new
1471 // one if in the very first position of the array
1472 if (cur.pos() == 0) {
1473 //lyxerr << "new scriptinset" << endl;
1474 cur.insert(new InsetMathScript(up));
1476 //lyxerr << "converting prev atom " << endl;
1477 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1480 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1481 // See comment in MathParser.cpp for special handling of {}-bases
1487 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1488 cur.niceInsert(save_selection);
1490 //lyxerr << "inserting selection 2:\n" << save_selection << endl;