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"
36 #include "bufferview_funcs.h"
37 #include "BufferView.h"
39 #include "CoordCache.h"
41 #include "CutAndPaste.h"
43 #include "DispatchResult.h"
44 #include "FuncRequest.h"
45 #include "FuncStatus.h"
48 #include "OutputParams.h"
51 #include "support/lstrings.h"
52 #include "support/textutils.h"
54 #include "frontends/Clipboard.h"
55 #include "frontends/Painter.h"
56 #include "frontends/Selection.h"
58 #include "FuncRequest.h"
65 using cap::copySelection;
66 using cap::grabAndEraseSelection;
67 using cap::cutSelection;
68 using cap::replaceSelection;
69 using cap::selClearOrDel;
73 using std::istringstream;
76 InsetMathNest::InsetMathNest(idx_type nargs)
77 : cells_(nargs), lock_(false)
81 InsetMathNest::InsetMathNest(InsetMathNest const & inset)
82 : InsetMath(inset), cells_(inset.cells_), lock_(inset.lock_)
86 InsetMathNest & InsetMathNest::operator=(InsetMathNest const & inset)
88 cells_ = inset.cells_;
90 InsetMath::operator=(inset);
95 InsetMath::idx_type InsetMathNest::nargs() const
101 void InsetMathNest::cursorPos(BufferView const & bv,
102 CursorSlice const & sl, bool /*boundary*/,
103 int & x, int & y) const
105 // FIXME: This is a hack. Ideally, the coord cache should not store
106 // absolute positions, but relative ones. This would mean to call
107 // setXY() not in MathData::draw(), but in the parent insets' draw()
108 // with the correctly adjusted x,y values. But this means that we'd have
109 // to touch all (math)inset's draw() methods. Right now, we'll store
110 // absolute value, and make them here relative, only to make them
111 // absolute again when actually drawing the cursor. What a mess.
112 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
113 MathData const & ar = sl.cell();
114 CoordCache const & coord_cache = bv.coordCache();
115 if (!coord_cache.getArrays().has(&ar)) {
116 // this can (semi-)legally happen if we just created this cell
117 // and it never has been drawn before. So don't ASSERT.
118 //lyxerr << "no cached data for array " << &ar << endl;
123 Point const pt = coord_cache.getArrays().xy(&ar);
124 if (!coord_cache.getInsets().has(this)) {
126 //lyxerr << "no cached data for inset " << this << endl;
131 Point const pt2 = coord_cache.getInsets().xy(this);
132 //lyxerr << "retrieving position cache for MathData "
133 // << pt.x_ << ' ' << pt.y_ << std::endl;
134 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
136 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
137 // << " asc: " << ascent() << " des: " << descent()
138 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
139 // move cursor visually into empty cells ("blue rectangles");
145 void InsetMathNest::metrics(MetricsInfo const & mi) const
148 for (idx_type i = 0, n = nargs(); i != n; ++i)
153 bool InsetMathNest::idxNext(Cursor & cur) const
155 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
156 if (cur.idx() == cur.lastidx())
164 bool InsetMathNest::idxRight(Cursor & cur) const
170 bool InsetMathNest::idxPrev(Cursor & cur) const
172 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
176 cur.pos() = cur.lastpos();
181 bool InsetMathNest::idxLeft(Cursor & cur) const
187 bool InsetMathNest::idxFirst(Cursor & cur) const
189 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
198 bool InsetMathNest::idxLast(Cursor & cur) const
200 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
203 cur.idx() = cur.lastidx();
204 cur.pos() = cur.lastpos();
209 void InsetMathNest::dump() const
211 odocstringstream oss;
213 os << "---------------------------------------------\n";
216 for (idx_type i = 0, n = nargs(); i != n; ++i)
217 os << cell(i) << "\n";
218 os << "---------------------------------------------\n";
219 lyxerr << to_utf8(oss.str());
223 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
227 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
230 setPosCache(pi, x, y);
234 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
236 BufferView & bv = *pi.base.bv;
237 // this should use the x/y values given, not the cached values
238 Cursor & cur = bv.cursor();
239 if (!cur.selection())
241 if (!ptr_cmp(&cur.inset(), this))
244 // FIXME: hack to get position cache warm
245 pi.pain.setDrawingEnabled(false);
247 pi.pain.setDrawingEnabled(true);
249 CursorSlice s1 = cur.selBegin();
250 CursorSlice s2 = cur.selEnd();
252 //lyxerr << "InsetMathNest::drawing selection: "
253 // << " s1: " << s1 << " s2: " << s2 << endl;
254 if (s1.idx() == s2.idx()) {
255 MathData const & c = cell(s1.idx());
256 int x1 = c.xo(bv) + c.pos2x(s1.pos());
257 int y1 = c.yo(bv) - c.ascent();
258 int x2 = c.xo(bv) + c.pos2x(s2.pos());
259 int y2 = c.yo(bv) + c.descent();
260 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
261 //lyxerr << "InsetMathNest::drawing selection 3: "
262 // << " x1: " << x1 << " x2: " << x2
263 // << " y1: " << y1 << " y2: " << y2 << endl;
265 for (idx_type i = 0; i < nargs(); ++i) {
266 if (idxBetween(i, s1.idx(), s2.idx())) {
267 MathData const & c = cell(i);
269 int y1 = c.yo(bv) - c.ascent();
270 int x2 = c.xo(bv) + c.width();
271 int y2 = c.yo(bv) + c.descent();
272 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
279 void InsetMathNest::validate(LaTeXFeatures & features) const
281 for (idx_type i = 0; i < nargs(); ++i)
282 cell(i).validate(features);
286 void InsetMathNest::replace(ReplaceData & rep)
288 for (idx_type i = 0; i < nargs(); ++i)
289 cell(i).replace(rep);
293 bool InsetMathNest::contains(MathData const & ar) const
295 for (idx_type i = 0; i < nargs(); ++i)
296 if (cell(i).contains(ar))
302 bool InsetMathNest::lock() const
308 void InsetMathNest::lock(bool l)
314 bool InsetMathNest::isActive() const
320 MathData InsetMathNest::glue() const
323 for (size_t i = 0; i < nargs(); ++i)
329 void InsetMathNest::write(WriteStream & os) const
331 os << '\\' << name().c_str();
332 for (size_t i = 0; i < nargs(); ++i)
333 os << '{' << cell(i) << '}';
335 os.pendingSpace(true);
336 if (lock_ && !os.latex()) {
338 os.pendingSpace(true);
343 void InsetMathNest::normalize(NormalStream & os) const
345 os << '[' << name().c_str();
346 for (size_t i = 0; i < nargs(); ++i)
347 os << ' ' << cell(i);
352 int InsetMathNest::latex(Buffer const &, odocstream & os,
353 OutputParams const & runparams) const
355 WriteStream wi(os, runparams.moving_arg, true);
361 bool InsetMathNest::notifyCursorLeaves(Cursor & /*cur*/)
367 MathData & ar = cur.cell();
368 // remove base-only "scripts"
369 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
370 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
371 if (p && p->nargs() == 1) {
372 MathData ar = p->nuc();
375 cur.adjust(i, ar.size() - 1);
379 // glue adjacent font insets of the same kind
380 for (pos_type i = 0; i + 1 < size(); ++i) {
381 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
382 InsetMathFont const * q = operator[](i + 1)->asFontInset();
383 if (p && q && p->name() == q->name()) {
384 p->cell(0).append(q->cell(0));
394 void InsetMathNest::handleFont
395 (Cursor & cur, docstring const & arg, char const * const font)
397 handleFont(cur, arg, from_ascii(font));
401 void InsetMathNest::handleFont
402 (Cursor & cur, docstring const & arg, docstring const & font)
404 // this whole function is a hack and won't work for incremental font
407 if (cur.inset().asInsetMath()->name() == font) {
408 recordUndoInset(cur, Undo::ATOMIC);
409 cur.handleFont(to_utf8(font));
411 recordUndo(cur, Undo::ATOMIC);
412 cur.handleNest(createInsetMath(font));
418 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
420 recordUndo(cur, Undo::ATOMIC);
423 bv_funcs::string2font(to_utf8(arg), font, b);
424 if (font.color() != Color::inherit) {
425 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
426 cur.handleNest(at, 0);
431 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
433 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
434 //CursorSlice sl = cur.current();
436 switch (cmd.action) {
440 cur.message(_("Paste"));
441 replaceSelection(cur);
443 if (cmd.argument().empty() && !theClipboard().isInternal())
444 topaste = theClipboard().getAsText();
447 idocstringstream is(cmd.argument());
449 topaste = cap::getSelection(cur.buffer(), n);
451 cur.niceInsert(topaste);
452 cur.clearSelection(); // bug 393
453 cur.bv().switchKeyMap();
460 cutSelection(cur, true, true);
461 cur.message(_("Cut"));
462 // Prevent stale position >= size crash
463 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
469 cur.message(_("Copy"));
472 case LFUN_MOUSE_PRESS:
473 lfunMousePress(cur, cmd);
476 case LFUN_MOUSE_MOTION:
477 lfunMouseMotion(cur, cmd);
480 case LFUN_MOUSE_RELEASE:
481 lfunMouseRelease(cur, cmd);
484 case LFUN_FINISHED_LEFT:
485 cur.bv().cursor() = cur;
488 case LFUN_FINISHED_RIGHT:
490 cur.bv().cursor() = cur;
493 case LFUN_CHAR_FORWARD:
494 cur.updateFlags(Update::Decoration | Update::FitCursor);
495 case LFUN_CHAR_FORWARD_SELECT:
496 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
497 cur.autocorrect() = false;
499 cur.macroModeClose();
500 if (reverseDirectionNeeded(cur))
501 goto goto_char_backwards;
504 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
505 cur.pushLeft(*cur.nextAtom().nucleus());
506 cur.inset().idxFirst(cur);
507 } else if (cur.posRight() || idxRight(cur)
508 || cur.popRight() || cur.selection())
511 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
516 case LFUN_CHAR_BACKWARD:
517 cur.updateFlags(Update::Decoration | Update::FitCursor);
518 case LFUN_CHAR_BACKWARD_SELECT:
519 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
520 cur.autocorrect() = false;
522 cur.macroModeClose();
523 if (reverseDirectionNeeded(cur))
524 goto goto_char_forwards;
527 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
529 cur.push(*cur.nextAtom().nucleus());
530 cur.inset().idxLast(cur);
531 } else if (cur.posLeft() || idxLeft(cur)
532 || cur.popLeft() || cur.selection())
535 cmd = FuncRequest(LFUN_FINISHED_LEFT);
541 cur.updateFlags(Update::Decoration | Update::FitCursor);
543 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
544 if (cur.inMacroMode()) {
546 cur.macroModeClose();
549 cur.selHandle(cmd.action == LFUN_UP_SELECT);
550 if (!cur.upDownInMath(true))
552 // fixes bug 1598. Please check!
557 cur.updateFlags(Update::Decoration | Update::FitCursor);
558 case LFUN_DOWN_SELECT:
559 if (cur.inMacroMode()) {
560 cur.macroModeClose();
563 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
564 if (!cur.upDownInMath(false))
566 // fixes bug 1598. Please check!
570 case LFUN_MOUSE_DOUBLE:
571 case LFUN_MOUSE_TRIPLE:
572 case LFUN_WORD_SELECT:
576 cur.selection() = true;
577 cur.pos() = cur.lastpos();
578 cur.idx() = cur.lastidx();
579 cap::saveSelection(cur);
582 case LFUN_PARAGRAPH_UP:
583 case LFUN_PARAGRAPH_DOWN:
584 cur.updateFlags(Update::Decoration | Update::FitCursor);
585 case LFUN_PARAGRAPH_UP_SELECT:
586 case LFUN_PARAGRAPH_DOWN_SELECT:
589 case LFUN_LINE_BEGIN:
590 case LFUN_WORD_BACKWARD:
591 cur.updateFlags(Update::Decoration | Update::FitCursor);
592 case LFUN_LINE_BEGIN_SELECT:
593 case LFUN_WORD_BACKWARD_SELECT:
594 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
595 cmd.action == LFUN_LINE_BEGIN_SELECT);
596 cur.macroModeClose();
597 if (cur.pos() != 0) {
599 } else if (cur.col() != 0) {
600 cur.idx() -= cur.col();
602 } else if (cur.idx() != 0) {
606 cmd = FuncRequest(LFUN_FINISHED_LEFT);
611 case LFUN_WORD_FORWARD:
613 cur.updateFlags(Update::Decoration | Update::FitCursor);
614 case LFUN_WORD_FORWARD_SELECT:
615 case LFUN_LINE_END_SELECT:
616 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
617 cmd.action == LFUN_LINE_END_SELECT);
618 cur.macroModeClose();
620 if (cur.pos() != cur.lastpos()) {
621 cur.pos() = cur.lastpos();
622 } else if (ncols() && (cur.col() != cur.lastcol())) {
623 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
624 cur.pos() = cur.lastpos();
625 } else if (cur.idx() != cur.lastidx()) {
626 cur.idx() = cur.lastidx();
627 cur.pos() = cur.lastpos();
629 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
634 case LFUN_SCREEN_UP_SELECT:
636 cmd = FuncRequest(LFUN_FINISHED_LEFT);
640 case LFUN_SCREEN_DOWN_SELECT:
641 case LFUN_SCREEN_DOWN:
642 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
646 case LFUN_CELL_FORWARD:
647 cur.updateFlags(Update::Decoration | Update::FitCursor);
648 cur.inset().idxNext(cur);
651 case LFUN_CELL_BACKWARD:
652 cur.updateFlags(Update::Decoration | Update::FitCursor);
653 cur.inset().idxPrev(cur);
656 case LFUN_WORD_DELETE_BACKWARD:
657 case LFUN_CHAR_DELETE_BACKWARD:
659 // May affect external cell:
660 recordUndoInset(cur, Undo::ATOMIC);
662 recordUndo(cur, Undo::ATOMIC);
663 // if the inset can not be removed from within, delete it
664 if (!cur.backspace()) {
665 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
666 cur.innerText()->dispatch(cur, cmd);
670 case LFUN_WORD_DELETE_FORWARD:
671 case LFUN_CHAR_DELETE_FORWARD:
672 if (cur.pos() == cur.lastpos())
673 // May affect external cell:
674 recordUndoInset(cur, Undo::ATOMIC);
676 recordUndo(cur, Undo::ATOMIC);
677 // if the inset can not be removed from within, delete it
679 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
680 cur.innerText()->dispatch(cur, cmd);
686 cur.clearSelection();
688 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
693 case LFUN_INSET_TOGGLE:
699 case LFUN_SELF_INSERT:
700 if (cmd.argument().size() != 1) {
702 docstring const arg = cmd.argument();
703 if (!interpretString(cur, arg))
707 // Don't record undo steps if we are in macro mode and
708 // cmd.argument is the next character of the macro name.
709 // Otherwise we'll get an invalid cursor if we undo after
710 // the macro was finished and the macro is a known command,
711 // e.g. sqrt. Cursor::macroModeClose replaces in this case
712 // the InsetMathUnknown with name "frac" by an empty
713 // InsetMathFrac -> a pos value > 0 is invalid.
714 // A side effect is that an undo before the macro is finished
715 // undoes the complete macro, not only the last character.
716 if (!cur.inMacroMode())
719 // spacial handling of space. If we insert an inset
720 // via macro mode, we want to put the cursor inside it
721 // if relevant. Think typing "\frac<space>".
722 if (cmd.argument()[0] == ' '
723 && cur.inMacroMode() && cur.macroName() != "\\"
724 && cur.macroModeClose()) {
725 MathAtom const atom = cur.prevAtom();
726 if (atom->asNestInset() && atom->nargs() > 0) {
728 cur.pushLeft(*cur.nextInset());
730 } else if (!interpretChar(cur, cmd.argument()[0])) {
731 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
736 //case LFUN_SERVER_GET_XY:
737 // sprintf(dispatch_buffer, "%d %d",);
740 case LFUN_SERVER_SET_XY: {
741 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
744 istringstream is(to_utf8(cmd.argument()));
746 cur.setScreenPos(x, y);
750 // Special casing for superscript in case of LyX handling
752 case LFUN_ACCENT_CIRCUMFLEX:
753 if (cmd.argument().empty()) {
754 // do superscript if LyX handles
756 recordUndo(cur, Undo::ATOMIC);
757 script(cur, true, grabAndEraseSelection(cur));
761 case LFUN_ACCENT_UMLAUT:
762 case LFUN_ACCENT_ACUTE:
763 case LFUN_ACCENT_GRAVE:
764 case LFUN_ACCENT_BREVE:
765 case LFUN_ACCENT_DOT:
766 case LFUN_ACCENT_MACRON:
767 case LFUN_ACCENT_CARON:
768 case LFUN_ACCENT_TILDE:
769 case LFUN_ACCENT_CEDILLA:
770 case LFUN_ACCENT_CIRCLE:
771 case LFUN_ACCENT_UNDERDOT:
772 case LFUN_ACCENT_TIE:
773 case LFUN_ACCENT_OGONEK:
774 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
778 case LFUN_FONT_FREE_APPLY:
779 case LFUN_FONT_FREE_UPDATE:
780 handleFont2(cur, cmd.argument());
784 if (currentMode() == TEXT_MODE)
785 handleFont(cur, cmd.argument(), "textbf");
787 handleFont(cur, cmd.argument(), "mathbf");
790 if (currentMode() == TEXT_MODE)
791 handleFont(cur, cmd.argument(), "textsf");
793 handleFont(cur, cmd.argument(), "mathsf");
796 if (currentMode() == TEXT_MODE)
797 handleFont(cur, cmd.argument(), "emph");
799 handleFont(cur, cmd.argument(), "mathcal");
801 case LFUN_FONT_ROMAN:
802 if (currentMode() == TEXT_MODE)
803 handleFont(cur, cmd.argument(), "textrm");
805 handleFont(cur, cmd.argument(), "mathrm");
808 if (currentMode() == TEXT_MODE)
809 handleFont(cur, cmd.argument(), "texttt");
811 handleFont(cur, cmd.argument(), "mathtt");
814 handleFont(cur, cmd.argument(), "mathfrak");
817 if (currentMode() == TEXT_MODE)
818 handleFont(cur, cmd.argument(), "textit");
820 handleFont(cur, cmd.argument(), "mathit");
823 if (currentMode() == TEXT_MODE)
824 // FIXME: should be "noun"
825 handleFont(cur, cmd.argument(), "textsc");
827 handleFont(cur, cmd.argument(), "mathbb");
830 case LFUN_FONT_FREE_APPLY:
831 handleFont(cur, cmd.argument(), "textrm");
834 case LFUN_FONT_DEFAULT:
835 handleFont(cur, cmd.argument(), "textnormal");
838 case LFUN_MATH_MODE: {
840 // ignore math-mode on when already in math mode
841 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
843 cur.macroModeClose();
844 docstring const save_selection = grabAndEraseSelection(cur);
846 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
847 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
849 cur.pushLeft(*cur.nextInset());
850 cur.niceInsert(save_selection);
852 if (currentMode() == Inset::TEXT_MODE) {
853 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
854 cur.message(_("create new math text environment ($...$)"));
856 handleFont(cur, cmd.argument(), "textrm");
857 cur.message(_("entered math text mode (textrm)"));
870 case LFUN_MATH_MATRIX: {
871 recordUndo(cur, Undo::ATOMIC);
876 idocstringstream is(cmd.argument());
877 is >> m >> n >> v_align >> h_align;
884 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
888 case LFUN_MATH_DELIM: {
890 docstring rs = support::split(cmd.argument(), ls, ' ');
891 // Reasonable default values
896 recordUndo(cur, Undo::ATOMIC);
897 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
901 case LFUN_MATH_BIGDELIM: {
902 docstring const lname = from_utf8(cmd.getArg(0));
903 docstring const ldelim = from_utf8(cmd.getArg(1));
904 docstring const rname = from_utf8(cmd.getArg(2));
905 docstring const rdelim = from_utf8(cmd.getArg(3));
906 latexkeys const * l = in_word_set(lname);
907 bool const have_l = l && l->inset == "big" &&
908 InsetMathBig::isBigInsetDelim(ldelim);
909 l = in_word_set(rname);
910 bool const have_r = l && l->inset == "big" &&
911 InsetMathBig::isBigInsetDelim(rdelim);
912 // We mimic LFUN_MATH_DELIM in case we have an empty left
913 // or right delimiter.
914 if (have_l || have_r) {
915 recordUndo(cur, Undo::ATOMIC);
916 docstring const selection = grabAndEraseSelection(cur);
919 cur.insert(MathAtom(new InsetMathBig(lname,
921 cur.niceInsert(selection);
923 cur.insert(MathAtom(new InsetMathBig(rname,
926 // Don't call cur.undispatched() if we did nothing, this would
927 // lead to infinite recursion via Text::dispatch().
931 case LFUN_SPACE_INSERT:
932 case LFUN_MATH_SPACE:
933 recordUndo(cur, Undo::ATOMIC);
934 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
937 case LFUN_ERT_INSERT:
938 // interpret this as if a backslash was typed
939 recordUndo(cur, Undo::ATOMIC);
940 interpretChar(cur, '\\');
943 case LFUN_MATH_SUBSCRIPT:
944 // interpret this as if a _ was typed
945 recordUndo(cur, Undo::ATOMIC);
946 interpretChar(cur, '_');
949 case LFUN_MATH_SUPERSCRIPT:
950 // interpret this as if a ^ was typed
951 recordUndo(cur, Undo::ATOMIC);
952 interpretChar(cur, '^');
955 case LFUN_QUOTE_INSERT:
956 // interpret this as if a straight " was typed
957 recordUndo(cur, Undo::ATOMIC);
958 interpretChar(cur, '\"');
961 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
962 // handling such that "self-insert" works on "arbitrary stuff" too, and
963 // math-insert only handles special math things like "matrix".
964 case LFUN_MATH_INSERT: {
965 recordUndo(cur, Undo::ATOMIC);
966 if (cmd.argument() == "^" || cmd.argument() == "_") {
967 interpretChar(cur, cmd.argument()[0]);
969 cur.niceInsert(cmd.argument());
973 case LFUN_DIALOG_SHOW_NEW_INSET: {
974 docstring const & name = cmd.argument();
977 InsetMathRef tmp(name);
978 data = tmp.createDialogStr(to_utf8(name));
980 cur.bv().showInsetDialog(to_utf8(name), data, 0);
984 case LFUN_INSET_INSERT: {
986 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
995 InsetMath::doDispatch(cur, cmd);
1001 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1002 FuncStatus & flag) const
1004 // the font related toggles
1005 //string tc = "mathnormal";
1007 string const arg = to_utf8(cmd.argument());
1008 switch (cmd.action) {
1009 case LFUN_TABULAR_FEATURE:
1010 flag.enabled(false);
1013 case LFUN_TABULAR_FEATURE:
1014 // FIXME: check temporarily disabled
1016 char align = mathcursor::valign();
1017 if (align == '\0') {
1021 if (cmd.argument().empty()) {
1025 if (!contains("tcb", cmd.argument()[0])) {
1029 flag.setOnOff(cmd.argument()[0] == align);
1032 /// We have to handle them since 1.4 blocks all unhandled actions
1033 case LFUN_FONT_ITAL:
1034 case LFUN_FONT_BOLD:
1035 case LFUN_FONT_SANS:
1036 case LFUN_FONT_EMPH:
1037 case LFUN_FONT_CODE:
1038 case LFUN_FONT_NOUN:
1039 case LFUN_FONT_ROMAN:
1040 case LFUN_FONT_DEFAULT:
1043 case LFUN_MATH_MUTATE:
1044 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1045 flag.setOnOff(false);
1048 // we just need to be in math mode to enable that
1049 case LFUN_MATH_SIZE:
1050 case LFUN_MATH_SPACE:
1051 case LFUN_MATH_LIMITS:
1052 case LFUN_MATH_NONUMBER:
1053 case LFUN_MATH_NUMBER:
1054 case LFUN_MATH_EXTERN:
1058 case LFUN_FONT_FRAK:
1059 flag.enabled(currentMode() != TEXT_MODE);
1062 case LFUN_MATH_INSERT: {
1063 bool const textarg =
1064 arg == "\\textbf" || arg == "\\textsf" ||
1065 arg == "\\textrm" || arg == "\\textmd" ||
1066 arg == "\\textit" || arg == "\\textsc" ||
1067 arg == "\\textsl" || arg == "\\textup" ||
1068 arg == "\\texttt" || arg == "\\textbb" ||
1069 arg == "\\textnormal";
1070 flag.enabled(currentMode() != TEXT_MODE || textarg);
1074 case LFUN_MATH_MATRIX:
1075 flag.enabled(currentMode() == MATH_MODE);
1078 case LFUN_INSET_INSERT: {
1079 // Don't test createMathInset_fromDialogStr(), since
1080 // getStatus is not called with a valid reference and the
1081 // dialog would not be applyable.
1082 string const name = cmd.getArg(0);
1083 flag.enabled(name == "ref");
1087 case LFUN_MATH_DELIM:
1088 case LFUN_MATH_BIGDELIM:
1089 // Don't do this with multi-cell selections
1090 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1093 case LFUN_HYPHENATION_POINT_INSERT:
1094 case LFUN_LIGATURE_BREAK_INSERT:
1095 case LFUN_MENU_SEPARATOR_INSERT:
1096 case LFUN_DOTS_INSERT:
1097 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1098 // FIXME: These would probably make sense in math-text mode
1099 flag.enabled(false);
1110 void InsetMathNest::edit(Cursor & cur, bool left)
1113 cur.idx() = left ? 0 : cur.lastidx();
1114 cur.pos() = left ? 0 : cur.lastpos();
1116 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1120 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1123 int dist_min = 1000000;
1124 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1125 int const d = cell(i).dist(cur.bv(), x, y);
1131 MathData & ar = cell(idx_min);
1133 cur.idx() = idx_min;
1134 cur.pos() = ar.x2pos(x - ar.xo(cur.bv()));
1136 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1137 if (dist_min == 0) {
1139 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1140 if (ar[i]->covers(cur.bv(), x, y))
1141 return ar[i].nucleus()->editXY(cur, x, y);
1147 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1149 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1150 BufferView & bv = cur.bv();
1151 if (cmd.button() == mouse_button::button1) {
1152 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1153 bv.mouseSetCursor(cur);
1154 // Update the cursor update flags as needed:
1156 // Update::Decoration: tells to update the decoration (visual box
1157 // corners that define the inset)/
1158 // Update::FitCursor: adjust the screen to the cursor position if
1160 // cur.result().update(): don't overwrite previously set flags.
1161 cur.updateFlags(Update::Decoration | Update::FitCursor | cur.result().update());
1162 } else if (cmd.button() == mouse_button::button2) {
1164 if (cap::selection()) {
1165 // See comment in Text::dispatch why we do this
1166 cap::copySelectionToStack();
1167 cmd = FuncRequest(LFUN_PASTE, "0");
1168 doDispatch(cur, cmd);
1170 asArray(theSelection().get(), ar);
1173 bv.mouseSetCursor(cur);
1178 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1180 // only select with button 1
1181 if (cmd.button() == mouse_button::button1) {
1182 Cursor & bvcur = cur.bv().cursor();
1183 if (bvcur.anchor_.hasPart(cur)) {
1184 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1185 bvcur.setCursor(cur);
1186 bvcur.selection() = true;
1187 //lyxerr << "MOTION " << bvcur << endl;
1194 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1196 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1198 if (cmd.button() == mouse_button::button1) {
1199 if (!cur.selection())
1202 Cursor & bvcur = cur.bv().cursor();
1203 bvcur.selection() = true;
1204 cap::saveSelection(bvcur);
1213 bool InsetMathNest::interpretChar(Cursor & cur, char_type c)
1215 //lyxerr << "interpret 2: '" << c << "'" << endl;
1216 docstring save_selection;
1217 if (c == '^' || c == '_')
1218 save_selection = grabAndEraseSelection(cur);
1223 if (cur.inMacroMode()) {
1224 docstring name = cur.macroName();
1226 /// are we currently typing '#1' or '#2' or...?
1227 if (name == "\\#") {
1230 if (n >= 1 && n <= 9)
1231 cur.insert(new MathMacroArgument(n));
1235 if (isAlphaASCII(c)) {
1236 cur.activeMacro()->setName(name + docstring(1, c));
1240 // handle 'special char' macros
1245 if (currentMode() == InsetMath::TEXT_MODE)
1246 cur.niceInsert(createInsetMath("textbackslash"));
1248 cur.niceInsert(createInsetMath("backslash"));
1249 } else if (c == '{') {
1251 cur.niceInsert(MathAtom(new InsetMathBrace));
1252 } else if (c == '%') {
1254 cur.niceInsert(MathAtom(new InsetMathComment));
1255 } else if (c == '#') {
1256 BOOST_ASSERT(cur.activeMacro());
1257 cur.activeMacro()->setName(name + docstring(1, c));
1260 cur.niceInsert(createInsetMath(docstring(1, c)));
1265 // One character big delimiters. The others are handled in
1266 // interpretString().
1267 latexkeys const * l = in_word_set(name.substr(1));
1268 if (name[0] == '\\' && l && l->inset == "big") {
1272 delim = from_ascii("\\{");
1275 delim = from_ascii("\\}");
1278 delim = docstring(1, c);
1281 if (InsetMathBig::isBigInsetDelim(delim)) {
1282 // name + delim ared a valid InsetMathBig.
1283 // We can't use cur.macroModeClose() because
1284 // it does not handle delim.
1285 InsetMathUnknown * p = cur.activeMacro();
1288 cur.cell().erase(cur.pos());
1289 cur.plainInsert(MathAtom(
1290 new InsetMathBig(name.substr(1), delim)));
1295 // leave macro mode and try again if necessary
1296 cur.macroModeClose();
1298 cur.niceInsert(MathAtom(new InsetMathBrace));
1300 interpretChar(cur, c);
1304 // This is annoying as one has to press <space> far too often.
1308 // leave autocorrect mode if necessary
1309 if (autocorrect() && c == ' ') {
1310 autocorrect() = false;
1315 // just clear selection on pressing the space bar
1316 if (cur.selection() && c == ' ') {
1317 cur.selection() = false;
1324 //lyxerr << "starting with macro" << endl;
1325 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), false)));
1330 if (currentMode() == InsetMath::TEXT_MODE)
1336 if (currentMode() == InsetMath::TEXT_MODE) {
1337 // insert spaces in text mode,
1338 // but suppress direct insertion of two spaces in a row
1339 // the still allows typing '<space>a<space>' and deleting the 'a', but
1340 // it is better than nothing...
1341 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1343 // FIXME: we have to enable full redraw here because of the
1344 // visual box corners that define the inset. If we know for
1345 // sure that we stay within the same cell we can optimize for
1347 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1351 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1352 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
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.popRight()) {
1362 // FIXME: we have to enable full redraw here because of the
1363 // visual box corners that define the inset. If we know for
1364 // sure that we stay within the same cell we can optimize for
1366 //cur.updateFlags(Update::FitCursor);
1370 // if we are at the very end, leave the formula
1371 return cur.pos() != cur.lastpos();
1374 // These shouldn't work in text mode:
1375 if (currentMode() != InsetMath::TEXT_MODE) {
1377 script(cur, false, save_selection);
1381 script(cur, true, save_selection);
1385 cur.niceInsert(createInsetMath("sim"));
1390 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1391 c == '%' || c == '_' || c == '^') {
1392 cur.niceInsert(createInsetMath(docstring(1, c)));
1397 // try auto-correction
1398 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1401 // no special circumstances, so insert the character without any fuss
1403 cur.autocorrect() = true;
1408 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1410 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1412 if (!cur.empty() && cur.pos() > 0 &&
1413 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1414 if (InsetMathBig::isBigInsetDelim(str)) {
1415 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1416 if (prev[0] == '\\') {
1417 prev = prev.substr(1);
1418 latexkeys const * l = in_word_set(prev);
1419 if (l && l->inset == "big") {
1420 cur.cell()[cur.pos() - 1] =
1421 MathAtom(new InsetMathBig(prev, str));
1431 bool InsetMathNest::script(Cursor & cur, bool up,
1432 docstring const & save_selection)
1434 // Hack to get \^ and \_ working
1435 //lyxerr << "handling script: up: " << up << endl;
1436 if (cur.inMacroMode() && cur.macroName() == "\\") {
1438 cur.niceInsert(createInsetMath("mathcircumflex"));
1440 interpretChar(cur, '_');
1444 cur.macroModeClose();
1445 if (asScriptInset() && cur.idx() == 0) {
1446 // we are in a nucleus of a script inset, move to _our_ script
1447 InsetMathScript * inset = asScriptInset();
1448 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1450 cur.idx() = inset->idxOfScript(up);
1452 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1454 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1457 cur.idx() = inset->idxOfScript(up);
1458 cur.pos() = cur.lastpos();
1460 // convert the thing to our left to a scriptinset or create a new
1461 // one if in the very first position of the array
1462 if (cur.pos() == 0) {
1463 //lyxerr << "new scriptinset" << endl;
1464 cur.insert(new InsetMathScript(up));
1466 //lyxerr << "converting prev atom " << endl;
1467 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1470 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1471 // See comment in MathParser.cpp for special handling of {}-bases
1477 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1478 cur.niceInsert(save_selection);
1480 //lyxerr << "inserting selection 2:\n" << save_selection << endl;