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.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), mouse_hover_(false)
81 InsetMathNest::InsetMathNest(InsetMathNest const & inset)
82 : InsetMath(inset), cells_(inset.cells_), lock_(inset.lock_),
87 InsetMathNest & InsetMathNest::operator=(InsetMathNest const & inset)
89 cells_ = inset.cells_;
92 InsetMath::operator=(inset);
97 InsetMath::idx_type InsetMathNest::nargs() const
103 void InsetMathNest::cursorPos(BufferView const & bv,
104 CursorSlice const & sl, bool /*boundary*/,
105 int & x, int & y) const
107 // FIXME: This is a hack. Ideally, the coord cache should not store
108 // absolute positions, but relative ones. This would mean to call
109 // setXY() not in MathData::draw(), but in the parent insets' draw()
110 // with the correctly adjusted x,y values. But this means that we'd have
111 // to touch all (math)inset's draw() methods. Right now, we'll store
112 // absolute value, and make them here relative, only to make them
113 // absolute again when actually drawing the cursor. What a mess.
114 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
115 MathData const & ar = sl.cell();
116 CoordCache const & coord_cache = bv.coordCache();
117 if (!coord_cache.getArrays().has(&ar)) {
118 // this can (semi-)legally happen if we just created this cell
119 // and it never has been drawn before. So don't ASSERT.
120 //lyxerr << "no cached data for array " << &ar << endl;
125 Point const pt = coord_cache.getArrays().xy(&ar);
126 if (!coord_cache.getInsets().has(this)) {
128 //lyxerr << "no cached data for inset " << this << endl;
133 Point const pt2 = coord_cache.getInsets().xy(this);
134 //lyxerr << "retrieving position cache for MathData "
135 // << pt.x_ << ' ' << pt.y_ << std::endl;
136 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
138 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
139 // << " asc: " << ascent() << " des: " << descent()
140 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
141 // move cursor visually into empty cells ("blue rectangles");
147 void InsetMathNest::metrics(MetricsInfo const & mi) const
150 for (idx_type i = 0, n = nargs(); i != n; ++i) {
152 cell(i).metrics(m, dim);
157 bool InsetMathNest::idxNext(Cursor & cur) const
159 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
160 if (cur.idx() == cur.lastidx())
168 bool InsetMathNest::idxRight(Cursor & cur) const
174 bool InsetMathNest::idxPrev(Cursor & cur) const
176 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
180 cur.pos() = cur.lastpos();
185 bool InsetMathNest::idxLeft(Cursor & cur) const
191 bool InsetMathNest::idxFirst(Cursor & cur) const
193 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
202 bool InsetMathNest::idxLast(Cursor & cur) const
204 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
207 cur.idx() = cur.lastidx();
208 cur.pos() = cur.lastpos();
213 void InsetMathNest::dump() const
215 odocstringstream oss;
217 os << "---------------------------------------------\n";
220 for (idx_type i = 0, n = nargs(); i != n; ++i)
221 os << cell(i) << "\n";
222 os << "---------------------------------------------\n";
223 lyxerr << to_utf8(oss.str());
227 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
231 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
234 setPosCache(pi, x, y);
238 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
240 BufferView & bv = *pi.base.bv;
241 // this should use the x/y values given, not the cached values
242 Cursor & cur = bv.cursor();
243 if (!cur.selection())
245 if (!ptr_cmp(&cur.inset(), this))
248 // FIXME: hack to get position cache warm
249 pi.pain.setDrawingEnabled(false);
251 pi.pain.setDrawingEnabled(true);
253 CursorSlice s1 = cur.selBegin();
254 CursorSlice s2 = cur.selEnd();
256 //lyxerr << "InsetMathNest::drawing selection: "
257 // << " s1: " << s1 << " s2: " << s2 << endl;
258 if (s1.idx() == s2.idx()) {
259 MathData const & c = cell(s1.idx());
260 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
261 int x1 = g.pos.x_ + c.pos2x(s1.pos());
262 int y1 = g.pos.y_ - g.dim.ascent();
263 int x2 = g.pos.x_ + c.pos2x(s2.pos());
264 int y2 = g.pos.y_ + g.dim.descent();
265 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
266 //lyxerr << "InsetMathNest::drawing selection 3: "
267 // << " x1: " << x1 << " x2: " << x2
268 // << " y1: " << y1 << " y2: " << y2 << endl;
270 for (idx_type i = 0; i < nargs(); ++i) {
271 if (idxBetween(i, s1.idx(), s2.idx())) {
272 MathData const & c = cell(i);
273 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
275 int y1 = g.pos.y_ - g.dim.ascent();
276 int x2 = g.pos.x_ + g.dim.width();
277 int y2 = g.pos.y_ + g.dim.descent();
278 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
285 void InsetMathNest::validate(LaTeXFeatures & features) const
287 for (idx_type i = 0; i < nargs(); ++i)
288 cell(i).validate(features);
292 void InsetMathNest::replace(ReplaceData & rep)
294 for (idx_type i = 0; i < nargs(); ++i)
295 cell(i).replace(rep);
299 bool InsetMathNest::contains(MathData const & ar) const
301 for (idx_type i = 0; i < nargs(); ++i)
302 if (cell(i).contains(ar))
308 bool InsetMathNest::lock() const
314 void InsetMathNest::lock(bool l)
320 bool InsetMathNest::isActive() const
326 MathData InsetMathNest::glue() const
329 for (size_t i = 0; i < nargs(); ++i)
335 void InsetMathNest::write(WriteStream & os) const
337 os << '\\' << name().c_str();
338 for (size_t i = 0; i < nargs(); ++i)
339 os << '{' << cell(i) << '}';
341 os.pendingSpace(true);
342 if (lock_ && !os.latex()) {
344 os.pendingSpace(true);
349 void InsetMathNest::normalize(NormalStream & os) const
351 os << '[' << name().c_str();
352 for (size_t i = 0; i < nargs(); ++i)
353 os << ' ' << cell(i);
358 int InsetMathNest::latex(Buffer const &, odocstream & os,
359 OutputParams const & runparams) const
361 WriteStream wi(os, runparams.moving_arg, true);
367 bool InsetMathNest::setMouseHover(bool mouse_hover)
369 mouse_hover_ = mouse_hover;
374 bool InsetMathNest::notifyCursorLeaves(Cursor & /*cur*/)
378 MathData & ar = cur.cell();
379 // remove base-only "scripts"
380 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
381 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
382 if (p && p->nargs() == 1) {
383 MathData ar = p->nuc();
386 cur.adjust(i, ar.size() - 1);
390 // glue adjacent font insets of the same kind
391 for (pos_type i = 0; i + 1 < size(); ++i) {
392 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
393 InsetMathFont const * q = operator[](i + 1)->asFontInset();
394 if (p && q && p->name() == q->name()) {
395 p->cell(0).append(q->cell(0));
405 void InsetMathNest::handleFont
406 (Cursor & cur, docstring const & arg, char const * const font)
408 handleFont(cur, arg, from_ascii(font));
412 void InsetMathNest::handleFont
413 (Cursor & cur, docstring const & arg, docstring const & font)
415 // this whole function is a hack and won't work for incremental font
418 if (cur.inset().asInsetMath()->name() == font) {
419 recordUndoInset(cur, Undo::ATOMIC);
420 cur.handleFont(to_utf8(font));
422 recordUndo(cur, Undo::ATOMIC);
423 cur.handleNest(createInsetMath(font));
429 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
431 recordUndo(cur, Undo::ATOMIC);
434 font.fromString(to_utf8(arg), b);
435 if (font.color() != Color::inherit) {
436 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
437 cur.handleNest(at, 0);
442 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
444 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
445 //CursorSlice sl = cur.current();
447 switch (cmd.action) {
451 cur.message(_("Paste"));
452 replaceSelection(cur);
454 if (cmd.argument().empty() && !theClipboard().isInternal())
455 topaste = theClipboard().getAsText();
458 idocstringstream is(cmd.argument());
460 topaste = cap::getSelection(cur.buffer(), n);
462 cur.niceInsert(topaste);
463 cur.clearSelection(); // bug 393
470 cutSelection(cur, true, true);
471 cur.message(_("Cut"));
472 // Prevent stale position >= size crash
473 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
479 cur.message(_("Copy"));
482 case LFUN_MOUSE_PRESS:
483 lfunMousePress(cur, cmd);
486 case LFUN_MOUSE_MOTION:
487 lfunMouseMotion(cur, cmd);
490 case LFUN_MOUSE_RELEASE:
491 lfunMouseRelease(cur, cmd);
494 case LFUN_FINISHED_LEFT:
495 cur.bv().cursor() = cur;
498 case LFUN_FINISHED_RIGHT:
500 cur.bv().cursor() = cur;
503 case LFUN_CHAR_FORWARD:
504 cur.updateFlags(Update::Decoration | Update::FitCursor);
505 case LFUN_CHAR_FORWARD_SELECT:
506 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
507 cur.autocorrect() = false;
509 cur.macroModeClose();
510 if (reverseDirectionNeeded(cur))
511 goto goto_char_backwards;
514 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
515 cur.pushLeft(*cur.nextAtom().nucleus());
516 cur.inset().idxFirst(cur);
517 } else if (cur.posRight() || idxRight(cur)
518 || cur.popRight() || cur.selection())
521 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
526 case LFUN_CHAR_BACKWARD:
527 cur.updateFlags(Update::Decoration | Update::FitCursor);
528 case LFUN_CHAR_BACKWARD_SELECT:
529 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
530 cur.autocorrect() = false;
532 cur.macroModeClose();
533 if (reverseDirectionNeeded(cur))
534 goto goto_char_forwards;
537 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
539 cur.push(*cur.nextAtom().nucleus());
540 cur.inset().idxLast(cur);
541 } else if (cur.posLeft() || idxLeft(cur)
542 || cur.popLeft() || cur.selection())
545 cmd = FuncRequest(LFUN_FINISHED_LEFT);
552 cur.updateFlags(Update::Decoration | Update::FitCursor);
553 case LFUN_DOWN_SELECT:
554 case LFUN_UP_SELECT: {
555 // close active macro
556 if (cur.inMacroMode()) {
557 cur.macroModeClose();
561 // stop/start the selection
562 bool select = cmd.action == LFUN_DOWN_SELECT ||
563 cmd.action == LFUN_UP_SELECT;
564 cur.selHandle(select);
567 bool up = cmd.action == LFUN_UP || cmd.action == LFUN_UP_SELECT;
568 bool successful = cur.upDownInMath(up);
570 // notify left insets and give them chance to set update flags
571 lyx::notifyCursorLeaves(cur.beforeDispatchCursor(), cur);
576 if (cur.fixIfBroken())
577 // FIXME: Something bad happened. We pass the corrected Cursor
578 // instead of letting things go worse.
581 // We did not manage to move the cursor.
586 case LFUN_MOUSE_DOUBLE:
587 case LFUN_MOUSE_TRIPLE:
588 case LFUN_WORD_SELECT:
592 cur.selection() = true;
593 cur.pos() = cur.lastpos();
594 cur.idx() = cur.lastidx();
597 case LFUN_PARAGRAPH_UP:
598 case LFUN_PARAGRAPH_DOWN:
599 cur.updateFlags(Update::Decoration | Update::FitCursor);
600 case LFUN_PARAGRAPH_UP_SELECT:
601 case LFUN_PARAGRAPH_DOWN_SELECT:
604 case LFUN_LINE_BEGIN:
605 case LFUN_WORD_BACKWARD:
606 cur.updateFlags(Update::Decoration | Update::FitCursor);
607 case LFUN_LINE_BEGIN_SELECT:
608 case LFUN_WORD_BACKWARD_SELECT:
609 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
610 cmd.action == LFUN_LINE_BEGIN_SELECT);
611 cur.macroModeClose();
612 if (cur.pos() != 0) {
614 } else if (cur.col() != 0) {
615 cur.idx() -= cur.col();
617 } else if (cur.idx() != 0) {
621 cmd = FuncRequest(LFUN_FINISHED_LEFT);
626 case LFUN_WORD_FORWARD:
628 cur.updateFlags(Update::Decoration | Update::FitCursor);
629 case LFUN_WORD_FORWARD_SELECT:
630 case LFUN_LINE_END_SELECT:
631 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
632 cmd.action == LFUN_LINE_END_SELECT);
633 cur.macroModeClose();
635 if (cur.pos() != cur.lastpos()) {
636 cur.pos() = cur.lastpos();
637 } else if (ncols() && (cur.col() != cur.lastcol())) {
638 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
639 cur.pos() = cur.lastpos();
640 } else if (cur.idx() != cur.lastidx()) {
641 cur.idx() = cur.lastidx();
642 cur.pos() = cur.lastpos();
644 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
649 case LFUN_SCREEN_UP_SELECT:
650 cmd = FuncRequest(LFUN_FINISHED_LEFT);
654 case LFUN_SCREEN_DOWN_SELECT:
655 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
659 case LFUN_CELL_FORWARD:
660 cur.updateFlags(Update::Decoration | Update::FitCursor);
661 cur.inset().idxNext(cur);
664 case LFUN_CELL_BACKWARD:
665 cur.updateFlags(Update::Decoration | Update::FitCursor);
666 cur.inset().idxPrev(cur);
669 case LFUN_WORD_DELETE_BACKWARD:
670 case LFUN_CHAR_DELETE_BACKWARD:
672 // May affect external cell:
673 recordUndoInset(cur, Undo::ATOMIC);
675 recordUndo(cur, Undo::ATOMIC);
676 // if the inset can not be removed from within, delete it
677 if (!cur.backspace()) {
678 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
679 cur.innerText()->dispatch(cur, cmd);
683 case LFUN_WORD_DELETE_FORWARD:
684 case LFUN_CHAR_DELETE_FORWARD:
685 if (cur.pos() == cur.lastpos())
686 // May affect external cell:
687 recordUndoInset(cur, Undo::ATOMIC);
689 recordUndo(cur, Undo::ATOMIC);
690 // if the inset can not be removed from within, delete it
692 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
693 cur.innerText()->dispatch(cur, cmd);
699 cur.clearSelection();
701 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
706 case LFUN_INSET_TOGGLE:
712 case LFUN_SELF_INSERT:
713 if (cmd.argument().size() != 1) {
715 docstring const arg = cmd.argument();
716 if (!interpretString(cur, arg))
720 // Don't record undo steps if we are in macro mode and
721 // cmd.argument is the next character of the macro name.
722 // Otherwise we'll get an invalid cursor if we undo after
723 // the macro was finished and the macro is a known command,
724 // e.g. sqrt. Cursor::macroModeClose replaces in this case
725 // the InsetMathUnknown with name "frac" by an empty
726 // InsetMathFrac -> a pos value > 0 is invalid.
727 // A side effect is that an undo before the macro is finished
728 // undoes the complete macro, not only the last character.
729 if (!cur.inMacroMode())
732 // spacial handling of space. If we insert an inset
733 // via macro mode, we want to put the cursor inside it
734 // if relevant. Think typing "\frac<space>".
735 if (cmd.argument()[0] == ' '
736 && cur.inMacroMode() && cur.macroName() != "\\"
737 && cur.macroModeClose()) {
738 MathAtom const atom = cur.prevAtom();
739 if (atom->asNestInset() && atom->isActive()) {
741 cur.pushLeft(*cur.nextInset());
743 } else if (!interpretChar(cur, cmd.argument()[0])) {
744 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
749 //case LFUN_SERVER_GET_XY:
750 // sprintf(dispatch_buffer, "%d %d",);
753 case LFUN_SERVER_SET_XY: {
754 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
757 istringstream is(to_utf8(cmd.argument()));
759 cur.setScreenPos(x, y);
763 // Special casing for superscript in case of LyX handling
765 case LFUN_ACCENT_CIRCUMFLEX:
766 if (cmd.argument().empty()) {
767 // do superscript if LyX handles
769 recordUndo(cur, Undo::ATOMIC);
770 script(cur, true, grabAndEraseSelection(cur));
774 case LFUN_ACCENT_UMLAUT:
775 case LFUN_ACCENT_ACUTE:
776 case LFUN_ACCENT_GRAVE:
777 case LFUN_ACCENT_BREVE:
778 case LFUN_ACCENT_DOT:
779 case LFUN_ACCENT_MACRON:
780 case LFUN_ACCENT_CARON:
781 case LFUN_ACCENT_TILDE:
782 case LFUN_ACCENT_CEDILLA:
783 case LFUN_ACCENT_CIRCLE:
784 case LFUN_ACCENT_UNDERDOT:
785 case LFUN_ACCENT_TIE:
786 case LFUN_ACCENT_OGONEK:
787 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
791 case LFUN_FONT_FREE_APPLY:
792 case LFUN_FONT_FREE_UPDATE:
793 handleFont2(cur, cmd.argument());
797 if (currentMode() == TEXT_MODE)
798 handleFont(cur, cmd.argument(), "textbf");
800 handleFont(cur, cmd.argument(), "mathbf");
803 if (currentMode() == TEXT_MODE)
804 handleFont(cur, cmd.argument(), "textsf");
806 handleFont(cur, cmd.argument(), "mathsf");
809 if (currentMode() == TEXT_MODE)
810 handleFont(cur, cmd.argument(), "emph");
812 handleFont(cur, cmd.argument(), "mathcal");
814 case LFUN_FONT_ROMAN:
815 if (currentMode() == TEXT_MODE)
816 handleFont(cur, cmd.argument(), "textrm");
818 handleFont(cur, cmd.argument(), "mathrm");
820 case LFUN_FONT_TYPEWRITER:
821 if (currentMode() == TEXT_MODE)
822 handleFont(cur, cmd.argument(), "texttt");
824 handleFont(cur, cmd.argument(), "mathtt");
827 handleFont(cur, cmd.argument(), "mathfrak");
830 if (currentMode() == TEXT_MODE)
831 handleFont(cur, cmd.argument(), "textit");
833 handleFont(cur, cmd.argument(), "mathit");
836 if (currentMode() == TEXT_MODE)
837 // FIXME: should be "noun"
838 handleFont(cur, cmd.argument(), "textsc");
840 handleFont(cur, cmd.argument(), "mathbb");
843 case LFUN_FONT_FREE_APPLY:
844 handleFont(cur, cmd.argument(), "textrm");
847 case LFUN_FONT_DEFAULT:
848 handleFont(cur, cmd.argument(), "textnormal");
851 case LFUN_MATH_MODE: {
853 // ignore math-mode on when already in math mode
854 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
856 cur.macroModeClose();
857 docstring const save_selection = grabAndEraseSelection(cur);
859 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
860 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
862 cur.pushLeft(*cur.nextInset());
863 cur.niceInsert(save_selection);
865 if (currentMode() == Inset::TEXT_MODE) {
866 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
867 cur.message(_("create new math text environment ($...$)"));
869 handleFont(cur, cmd.argument(), "textrm");
870 cur.message(_("entered math text mode (textrm)"));
883 case LFUN_MATH_MATRIX: {
884 recordUndo(cur, Undo::ATOMIC);
889 idocstringstream is(cmd.argument());
890 is >> m >> n >> v_align >> h_align;
897 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
901 case LFUN_MATH_DELIM: {
903 docstring rs = support::split(cmd.argument(), ls, ' ');
904 // Reasonable default values
909 recordUndo(cur, Undo::ATOMIC);
910 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
914 case LFUN_MATH_BIGDELIM: {
915 docstring const lname = from_utf8(cmd.getArg(0));
916 docstring const ldelim = from_utf8(cmd.getArg(1));
917 docstring const rname = from_utf8(cmd.getArg(2));
918 docstring const rdelim = from_utf8(cmd.getArg(3));
919 latexkeys const * l = in_word_set(lname);
920 bool const have_l = l && l->inset == "big" &&
921 InsetMathBig::isBigInsetDelim(ldelim);
922 l = in_word_set(rname);
923 bool const have_r = l && l->inset == "big" &&
924 InsetMathBig::isBigInsetDelim(rdelim);
925 // We mimic LFUN_MATH_DELIM in case we have an empty left
926 // or right delimiter.
927 if (have_l || have_r) {
928 recordUndo(cur, Undo::ATOMIC);
929 docstring const selection = grabAndEraseSelection(cur);
932 cur.insert(MathAtom(new InsetMathBig(lname,
934 cur.niceInsert(selection);
936 cur.insert(MathAtom(new InsetMathBig(rname,
939 // Don't call cur.undispatched() if we did nothing, this would
940 // lead to infinite recursion via Text::dispatch().
944 case LFUN_SPACE_INSERT:
945 case LFUN_MATH_SPACE:
946 recordUndo(cur, Undo::ATOMIC);
947 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
950 case LFUN_ERT_INSERT:
951 // interpret this as if a backslash was typed
952 recordUndo(cur, Undo::ATOMIC);
953 interpretChar(cur, '\\');
956 case LFUN_MATH_SUBSCRIPT:
957 // interpret this as if a _ was typed
958 recordUndo(cur, Undo::ATOMIC);
959 interpretChar(cur, '_');
962 case LFUN_MATH_SUPERSCRIPT:
963 // interpret this as if a ^ was typed
964 recordUndo(cur, Undo::ATOMIC);
965 interpretChar(cur, '^');
968 case LFUN_QUOTE_INSERT:
969 // interpret this as if a straight " was typed
970 recordUndo(cur, Undo::ATOMIC);
971 interpretChar(cur, '\"');
974 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
975 // handling such that "self-insert" works on "arbitrary stuff" too, and
976 // math-insert only handles special math things like "matrix".
977 case LFUN_MATH_INSERT: {
978 recordUndo(cur, Undo::ATOMIC);
979 if (cmd.argument() == "^" || cmd.argument() == "_") {
980 interpretChar(cur, cmd.argument()[0]);
982 cur.niceInsert(cmd.argument());
986 case LFUN_DIALOG_SHOW_NEW_INSET: {
987 docstring const & name = cmd.argument();
990 InsetMathRef tmp(name);
991 data = tmp.createDialogStr(to_utf8(name));
993 cur.bv().showInsetDialog(to_utf8(name), data, 0);
997 case LFUN_INSET_INSERT: {
999 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1006 case LFUN_INSET_DISSOLVE:
1007 if (!asHullInset()) {
1008 recordUndoInset(cur, Undo::ATOMIC);
1014 InsetMath::doDispatch(cur, cmd);
1020 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1021 FuncStatus & flag) const
1023 // the font related toggles
1024 //string tc = "mathnormal";
1026 string const arg = to_utf8(cmd.argument());
1027 switch (cmd.action) {
1028 case LFUN_TABULAR_FEATURE:
1029 flag.enabled(false);
1032 case LFUN_TABULAR_FEATURE:
1033 // FIXME: check temporarily disabled
1035 char align = mathcursor::valign();
1036 if (align == '\0') {
1040 if (cmd.argument().empty()) {
1044 if (!contains("tcb", cmd.argument()[0])) {
1048 flag.setOnOff(cmd.argument()[0] == align);
1051 /// We have to handle them since 1.4 blocks all unhandled actions
1052 case LFUN_FONT_ITAL:
1053 case LFUN_FONT_BOLD:
1054 case LFUN_FONT_SANS:
1055 case LFUN_FONT_EMPH:
1056 case LFUN_FONT_TYPEWRITER:
1057 case LFUN_FONT_NOUN:
1058 case LFUN_FONT_ROMAN:
1059 case LFUN_FONT_DEFAULT:
1062 case LFUN_MATH_MUTATE:
1063 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1064 flag.setOnOff(false);
1067 // we just need to be in math mode to enable that
1068 case LFUN_MATH_SIZE:
1069 case LFUN_MATH_SPACE:
1070 case LFUN_MATH_LIMITS:
1071 case LFUN_MATH_NONUMBER:
1072 case LFUN_MATH_NUMBER:
1073 case LFUN_MATH_EXTERN:
1077 case LFUN_FONT_FRAK:
1078 flag.enabled(currentMode() != TEXT_MODE);
1081 case LFUN_MATH_INSERT: {
1082 bool const textarg =
1083 arg == "\\textbf" || arg == "\\textsf" ||
1084 arg == "\\textrm" || arg == "\\textmd" ||
1085 arg == "\\textit" || arg == "\\textsc" ||
1086 arg == "\\textsl" || arg == "\\textup" ||
1087 arg == "\\texttt" || arg == "\\textbb" ||
1088 arg == "\\textnormal";
1089 flag.enabled(currentMode() != TEXT_MODE || textarg);
1093 case LFUN_MATH_MATRIX:
1094 flag.enabled(currentMode() == MATH_MODE);
1097 case LFUN_INSET_INSERT: {
1098 // Don't test createMathInset_fromDialogStr(), since
1099 // getStatus is not called with a valid reference and the
1100 // dialog would not be applyable.
1101 string const name = cmd.getArg(0);
1102 flag.enabled(name == "ref");
1106 case LFUN_MATH_DELIM:
1107 case LFUN_MATH_BIGDELIM:
1108 // Don't do this with multi-cell selections
1109 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1112 case LFUN_HYPHENATION_POINT_INSERT:
1113 case LFUN_LIGATURE_BREAK_INSERT:
1114 case LFUN_MENU_SEPARATOR_INSERT:
1115 case LFUN_DOTS_INSERT:
1116 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1117 // FIXME: These would probably make sense in math-text mode
1118 flag.enabled(false);
1121 case LFUN_INSET_DISSOLVE:
1122 flag.enabled(!asHullInset());
1133 void InsetMathNest::edit(Cursor & cur, bool left)
1136 cur.idx() = left ? 0 : cur.lastidx();
1137 cur.pos() = left ? 0 : cur.lastpos();
1139 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1143 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1146 int dist_min = 1000000;
1147 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1148 int const d = cell(i).dist(cur.bv(), x, y);
1154 MathData & ar = cell(idx_min);
1156 cur.idx() = idx_min;
1157 cur.pos() = ar.x2pos(x - ar.xo(cur.bv()));
1159 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1160 if (dist_min == 0) {
1162 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1163 if (ar[i]->covers(cur.bv(), x, y))
1164 return ar[i].nucleus()->editXY(cur, x, y);
1170 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1172 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1173 BufferView & bv = cur.bv();
1174 bv.mouseSetCursor(cur);
1175 if (cmd.button() == mouse_button::button1) {
1176 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1177 // Update the cursor update flags as needed:
1179 // Update::Decoration: tells to update the decoration
1180 // (visual box corners that define
1182 // Update::FitCursor: adjust the screen to the cursor
1183 // position if needed
1184 // cur.result().update(): don't overwrite previously set flags.
1185 cur.updateFlags(Update::Decoration | Update::FitCursor
1186 | cur.result().update());
1187 } else if (cmd.button() == mouse_button::button2) {
1188 if (cap::selection()) {
1189 // See comment in Text::dispatch why we do this
1190 cap::copySelectionToStack();
1191 cmd = FuncRequest(LFUN_PASTE, "0");
1192 doDispatch(bv.cursor(), cmd);
1195 asArray(theSelection().get(), ar);
1196 bv.cursor().insert(ar);
1202 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1204 // only select with button 1
1205 if (cmd.button() == mouse_button::button1) {
1206 Cursor & bvcur = cur.bv().cursor();
1207 if (bvcur.anchor_.hasPart(cur)) {
1208 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1209 bvcur.setCursor(cur);
1210 bvcur.selection() = true;
1211 //lyxerr << "MOTION " << bvcur << endl;
1218 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1220 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1222 if (cmd.button() == mouse_button::button1) {
1223 if (!cur.selection())
1226 Cursor & bvcur = cur.bv().cursor();
1227 bvcur.selection() = true;
1236 bool InsetMathNest::interpretChar(Cursor & cur, char_type c)
1238 //lyxerr << "interpret 2: '" << c << "'" << endl;
1239 docstring save_selection;
1240 if (c == '^' || c == '_')
1241 save_selection = grabAndEraseSelection(cur);
1246 if (cur.inMacroMode()) {
1247 docstring name = cur.macroName();
1249 /// are we currently typing '#1' or '#2' or...?
1250 if (name == "\\#") {
1253 if (n >= 1 && n <= 9)
1254 cur.insert(new MathMacroArgument(n));
1258 if (isAlphaASCII(c)) {
1259 cur.activeMacro()->setName(name + docstring(1, c));
1263 // handle 'special char' macros
1268 if (currentMode() == InsetMath::TEXT_MODE)
1269 cur.niceInsert(createInsetMath("textbackslash"));
1271 cur.niceInsert(createInsetMath("backslash"));
1272 } else if (c == '{') {
1274 cur.niceInsert(MathAtom(new InsetMathBrace));
1275 } else if (c == '%') {
1277 cur.niceInsert(MathAtom(new InsetMathComment));
1278 } else if (c == '#') {
1279 BOOST_ASSERT(cur.activeMacro());
1280 cur.activeMacro()->setName(name + docstring(1, c));
1283 cur.niceInsert(createInsetMath(docstring(1, c)));
1288 // One character big delimiters. The others are handled in
1289 // interpretString().
1290 latexkeys const * l = in_word_set(name.substr(1));
1291 if (name[0] == '\\' && l && l->inset == "big") {
1295 delim = from_ascii("\\{");
1298 delim = from_ascii("\\}");
1301 delim = docstring(1, c);
1304 if (InsetMathBig::isBigInsetDelim(delim)) {
1305 // name + delim ared a valid InsetMathBig.
1306 // We can't use cur.macroModeClose() because
1307 // it does not handle delim.
1308 InsetMathUnknown * p = cur.activeMacro();
1311 cur.cell().erase(cur.pos());
1312 cur.plainInsert(MathAtom(
1313 new InsetMathBig(name.substr(1), delim)));
1318 // leave macro mode and try again if necessary
1319 cur.macroModeClose();
1321 cur.niceInsert(MathAtom(new InsetMathBrace));
1323 interpretChar(cur, c);
1327 // This is annoying as one has to press <space> far too often.
1331 // leave autocorrect mode if necessary
1332 if (autocorrect() && c == ' ') {
1333 autocorrect() = false;
1338 // just clear selection on pressing the space bar
1339 if (cur.selection() && c == ' ') {
1340 cur.selection() = false;
1347 //lyxerr << "starting with macro" << endl;
1348 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), false)));
1353 if (currentMode() == InsetMath::TEXT_MODE)
1359 if (currentMode() == InsetMath::TEXT_MODE) {
1360 // insert spaces in text mode,
1361 // but suppress direct insertion of two spaces in a row
1362 // the still allows typing '<space>a<space>' and deleting the 'a', but
1363 // it is better than nothing...
1364 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1366 // FIXME: we have to enable full redraw here because of the
1367 // visual box corners that define the inset. If we know for
1368 // sure that we stay within the same cell we can optimize for
1370 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1374 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1375 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1376 // FIXME: we have to enable full redraw here because of the
1377 // visual box corners that define the inset. If we know for
1378 // sure that we stay within the same cell we can optimize for
1380 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1384 if (cur.popRight()) {
1385 // FIXME: we have to enable full redraw here because of the
1386 // visual box corners that define the inset. If we know for
1387 // sure that we stay within the same cell we can optimize for
1389 //cur.updateFlags(Update::FitCursor);
1393 // if we are at the very end, leave the formula
1394 return cur.pos() != cur.lastpos();
1397 // These shouldn't work in text mode:
1398 if (currentMode() != InsetMath::TEXT_MODE) {
1400 script(cur, false, save_selection);
1404 script(cur, true, save_selection);
1408 cur.niceInsert(createInsetMath("sim"));
1413 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1414 c == '%' || c == '_' || c == '^') {
1415 cur.niceInsert(createInsetMath(docstring(1, c)));
1420 // try auto-correction
1421 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1424 // no special circumstances, so insert the character without any fuss
1426 cur.autocorrect() = true;
1431 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1433 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1435 if (!cur.empty() && cur.pos() > 0 &&
1436 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1437 if (InsetMathBig::isBigInsetDelim(str)) {
1438 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1439 if (prev[0] == '\\') {
1440 prev = prev.substr(1);
1441 latexkeys const * l = in_word_set(prev);
1442 if (l && l->inset == "big") {
1443 cur.cell()[cur.pos() - 1] =
1444 MathAtom(new InsetMathBig(prev, str));
1454 bool InsetMathNest::script(Cursor & cur, bool up,
1455 docstring const & save_selection)
1457 // Hack to get \^ and \_ working
1458 //lyxerr << "handling script: up: " << up << endl;
1459 if (cur.inMacroMode() && cur.macroName() == "\\") {
1461 cur.niceInsert(createInsetMath("mathcircumflex"));
1463 interpretChar(cur, '_');
1467 cur.macroModeClose();
1468 if (asScriptInset() && cur.idx() == 0) {
1469 // we are in a nucleus of a script inset, move to _our_ script
1470 InsetMathScript * inset = asScriptInset();
1471 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1473 cur.idx() = inset->idxOfScript(up);
1475 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1477 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1480 cur.idx() = inset->idxOfScript(up);
1481 cur.pos() = cur.lastpos();
1483 // convert the thing to our left to a scriptinset or create a new
1484 // one if in the very first position of the array
1485 if (cur.pos() == 0) {
1486 //lyxerr << "new scriptinset" << endl;
1487 cur.insert(new InsetMathScript(up));
1489 //lyxerr << "converting prev atom " << endl;
1490 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1493 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1494 // See comment in MathParser.cpp for special handling of {}-bases
1500 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1501 cur.niceInsert(save_selection);
1503 //lyxerr << "inserting selection 2:\n" << save_selection << endl;