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), 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)
155 bool InsetMathNest::idxNext(Cursor & cur) const
157 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
158 if (cur.idx() == cur.lastidx())
166 bool InsetMathNest::idxRight(Cursor & cur) const
172 bool InsetMathNest::idxPrev(Cursor & cur) const
174 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
178 cur.pos() = cur.lastpos();
183 bool InsetMathNest::idxLeft(Cursor & cur) const
189 bool InsetMathNest::idxFirst(Cursor & cur) const
191 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
200 bool InsetMathNest::idxLast(Cursor & cur) const
202 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
205 cur.idx() = cur.lastidx();
206 cur.pos() = cur.lastpos();
211 void InsetMathNest::dump() const
213 odocstringstream oss;
215 os << "---------------------------------------------\n";
218 for (idx_type i = 0, n = nargs(); i != n; ++i)
219 os << cell(i) << "\n";
220 os << "---------------------------------------------\n";
221 lyxerr << to_utf8(oss.str());
225 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
229 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
232 setPosCache(pi, x, y);
236 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
238 BufferView & bv = *pi.base.bv;
239 // this should use the x/y values given, not the cached values
240 Cursor & cur = bv.cursor();
241 if (!cur.selection())
243 if (!ptr_cmp(&cur.inset(), this))
246 // FIXME: hack to get position cache warm
247 pi.pain.setDrawingEnabled(false);
249 pi.pain.setDrawingEnabled(true);
251 CursorSlice s1 = cur.selBegin();
252 CursorSlice s2 = cur.selEnd();
254 //lyxerr << "InsetMathNest::drawing selection: "
255 // << " s1: " << s1 << " s2: " << s2 << endl;
256 if (s1.idx() == s2.idx()) {
257 MathData const & c = cell(s1.idx());
258 int x1 = c.xo(bv) + c.pos2x(s1.pos());
259 int y1 = c.yo(bv) - c.ascent();
260 int x2 = c.xo(bv) + c.pos2x(s2.pos());
261 int y2 = c.yo(bv) + c.descent();
262 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
263 //lyxerr << "InsetMathNest::drawing selection 3: "
264 // << " x1: " << x1 << " x2: " << x2
265 // << " y1: " << y1 << " y2: " << y2 << endl;
267 for (idx_type i = 0; i < nargs(); ++i) {
268 if (idxBetween(i, s1.idx(), s2.idx())) {
269 MathData const & c = cell(i);
271 int y1 = c.yo(bv) - c.ascent();
272 int x2 = c.xo(bv) + c.width();
273 int y2 = c.yo(bv) + c.descent();
274 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
281 void InsetMathNest::validate(LaTeXFeatures & features) const
283 for (idx_type i = 0; i < nargs(); ++i)
284 cell(i).validate(features);
288 void InsetMathNest::replace(ReplaceData & rep)
290 for (idx_type i = 0; i < nargs(); ++i)
291 cell(i).replace(rep);
295 bool InsetMathNest::contains(MathData const & ar) const
297 for (idx_type i = 0; i < nargs(); ++i)
298 if (cell(i).contains(ar))
304 bool InsetMathNest::lock() const
310 void InsetMathNest::lock(bool l)
316 bool InsetMathNest::isActive() const
322 MathData InsetMathNest::glue() const
325 for (size_t i = 0; i < nargs(); ++i)
331 void InsetMathNest::write(WriteStream & os) const
333 os << '\\' << name().c_str();
334 for (size_t i = 0; i < nargs(); ++i)
335 os << '{' << cell(i) << '}';
337 os.pendingSpace(true);
338 if (lock_ && !os.latex()) {
340 os.pendingSpace(true);
345 void InsetMathNest::normalize(NormalStream & os) const
347 os << '[' << name().c_str();
348 for (size_t i = 0; i < nargs(); ++i)
349 os << ' ' << cell(i);
354 int InsetMathNest::latex(Buffer const &, odocstream & os,
355 OutputParams const & runparams) const
357 WriteStream wi(os, runparams.moving_arg, true);
363 bool InsetMathNest::setMouseHover(bool mouse_hover)
365 mouse_hover_ = mouse_hover;
370 bool InsetMathNest::notifyCursorLeaves(Cursor & /*cur*/)
374 MathData & ar = cur.cell();
375 // remove base-only "scripts"
376 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
377 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
378 if (p && p->nargs() == 1) {
379 MathData ar = p->nuc();
382 cur.adjust(i, ar.size() - 1);
386 // glue adjacent font insets of the same kind
387 for (pos_type i = 0; i + 1 < size(); ++i) {
388 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
389 InsetMathFont const * q = operator[](i + 1)->asFontInset();
390 if (p && q && p->name() == q->name()) {
391 p->cell(0).append(q->cell(0));
401 void InsetMathNest::handleFont
402 (Cursor & cur, docstring const & arg, char const * const font)
404 handleFont(cur, arg, from_ascii(font));
408 void InsetMathNest::handleFont
409 (Cursor & cur, docstring const & arg, docstring const & font)
411 // this whole function is a hack and won't work for incremental font
414 if (cur.inset().asInsetMath()->name() == font) {
415 recordUndoInset(cur, Undo::ATOMIC);
416 cur.handleFont(to_utf8(font));
418 recordUndo(cur, Undo::ATOMIC);
419 cur.handleNest(createInsetMath(font));
425 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
427 recordUndo(cur, Undo::ATOMIC);
430 bv_funcs::string2font(to_utf8(arg), font, b);
431 if (font.color() != Color::inherit) {
432 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
433 cur.handleNest(at, 0);
438 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
440 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
441 //CursorSlice sl = cur.current();
443 switch (cmd.action) {
447 cur.message(_("Paste"));
448 replaceSelection(cur);
450 if (cmd.argument().empty() && !theClipboard().isInternal())
451 topaste = theClipboard().getAsText();
454 idocstringstream is(cmd.argument());
456 topaste = cap::getSelection(cur.buffer(), n);
458 cur.niceInsert(topaste);
459 cur.clearSelection(); // bug 393
466 cutSelection(cur, true, true);
467 cur.message(_("Cut"));
468 // Prevent stale position >= size crash
469 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
475 cur.message(_("Copy"));
478 case LFUN_MOUSE_PRESS:
479 lfunMousePress(cur, cmd);
482 case LFUN_MOUSE_MOTION:
483 lfunMouseMotion(cur, cmd);
486 case LFUN_MOUSE_RELEASE:
487 lfunMouseRelease(cur, cmd);
490 case LFUN_FINISHED_LEFT:
491 cur.bv().cursor() = cur;
494 case LFUN_FINISHED_RIGHT:
496 cur.bv().cursor() = cur;
499 case LFUN_CHAR_FORWARD:
500 cur.updateFlags(Update::Decoration | Update::FitCursor);
501 case LFUN_CHAR_FORWARD_SELECT:
502 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
503 cur.autocorrect() = false;
505 cur.macroModeClose();
506 if (reverseDirectionNeeded(cur))
507 goto goto_char_backwards;
510 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
511 cur.pushLeft(*cur.nextAtom().nucleus());
512 cur.inset().idxFirst(cur);
513 } else if (cur.posRight() || idxRight(cur)
514 || cur.popRight() || cur.selection())
517 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
522 case LFUN_CHAR_BACKWARD:
523 cur.updateFlags(Update::Decoration | Update::FitCursor);
524 case LFUN_CHAR_BACKWARD_SELECT:
525 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
526 cur.autocorrect() = false;
528 cur.macroModeClose();
529 if (reverseDirectionNeeded(cur))
530 goto goto_char_forwards;
533 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
535 cur.push(*cur.nextAtom().nucleus());
536 cur.inset().idxLast(cur);
537 } else if (cur.posLeft() || idxLeft(cur)
538 || cur.popLeft() || cur.selection())
541 cmd = FuncRequest(LFUN_FINISHED_LEFT);
548 cur.updateFlags(Update::Decoration | Update::FitCursor);
549 case LFUN_DOWN_SELECT:
550 case LFUN_UP_SELECT: {
551 // close active macro
552 if (cur.inMacroMode()) {
553 cur.macroModeClose();
557 // stop/start the selection
558 bool select = cmd.action == LFUN_DOWN_SELECT ||
559 cmd.action == LFUN_UP_SELECT;
560 cur.selHandle(select);
563 bool up = cmd.action == LFUN_UP || cmd.action == LFUN_UP_SELECT;
564 bool successful = cur.upDownInMath(up);
566 // notify left insets and give them chance to set update flags
567 lyx::notifyCursorLeaves(cur.beforeDispatchCursor(), cur);
574 case LFUN_MOUSE_DOUBLE:
575 case LFUN_MOUSE_TRIPLE:
576 case LFUN_WORD_SELECT:
580 cur.selection() = true;
581 cur.pos() = cur.lastpos();
582 cur.idx() = cur.lastidx();
585 case LFUN_PARAGRAPH_UP:
586 case LFUN_PARAGRAPH_DOWN:
587 cur.updateFlags(Update::Decoration | Update::FitCursor);
588 case LFUN_PARAGRAPH_UP_SELECT:
589 case LFUN_PARAGRAPH_DOWN_SELECT:
592 case LFUN_LINE_BEGIN:
593 case LFUN_WORD_BACKWARD:
594 cur.updateFlags(Update::Decoration | Update::FitCursor);
595 case LFUN_LINE_BEGIN_SELECT:
596 case LFUN_WORD_BACKWARD_SELECT:
597 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
598 cmd.action == LFUN_LINE_BEGIN_SELECT);
599 cur.macroModeClose();
600 if (cur.pos() != 0) {
602 } else if (cur.col() != 0) {
603 cur.idx() -= cur.col();
605 } else if (cur.idx() != 0) {
609 cmd = FuncRequest(LFUN_FINISHED_LEFT);
614 case LFUN_WORD_FORWARD:
616 cur.updateFlags(Update::Decoration | Update::FitCursor);
617 case LFUN_WORD_FORWARD_SELECT:
618 case LFUN_LINE_END_SELECT:
619 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
620 cmd.action == LFUN_LINE_END_SELECT);
621 cur.macroModeClose();
623 if (cur.pos() != cur.lastpos()) {
624 cur.pos() = cur.lastpos();
625 } else if (ncols() && (cur.col() != cur.lastcol())) {
626 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
627 cur.pos() = cur.lastpos();
628 } else if (cur.idx() != cur.lastidx()) {
629 cur.idx() = cur.lastidx();
630 cur.pos() = cur.lastpos();
632 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
637 case LFUN_SCREEN_UP_SELECT:
639 cmd = FuncRequest(LFUN_FINISHED_LEFT);
643 case LFUN_SCREEN_DOWN_SELECT:
644 case LFUN_SCREEN_DOWN:
645 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
649 case LFUN_CELL_FORWARD:
650 cur.updateFlags(Update::Decoration | Update::FitCursor);
651 cur.inset().idxNext(cur);
654 case LFUN_CELL_BACKWARD:
655 cur.updateFlags(Update::Decoration | Update::FitCursor);
656 cur.inset().idxPrev(cur);
659 case LFUN_WORD_DELETE_BACKWARD:
660 case LFUN_CHAR_DELETE_BACKWARD:
662 // May affect external cell:
663 recordUndoInset(cur, Undo::ATOMIC);
665 recordUndo(cur, Undo::ATOMIC);
666 // if the inset can not be removed from within, delete it
667 if (!cur.backspace()) {
668 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
669 cur.innerText()->dispatch(cur, cmd);
673 case LFUN_WORD_DELETE_FORWARD:
674 case LFUN_CHAR_DELETE_FORWARD:
675 if (cur.pos() == cur.lastpos())
676 // May affect external cell:
677 recordUndoInset(cur, Undo::ATOMIC);
679 recordUndo(cur, Undo::ATOMIC);
680 // if the inset can not be removed from within, delete it
682 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
683 cur.innerText()->dispatch(cur, cmd);
689 cur.clearSelection();
691 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
696 case LFUN_INSET_TOGGLE:
702 case LFUN_SELF_INSERT:
703 if (cmd.argument().size() != 1) {
705 docstring const arg = cmd.argument();
706 if (!interpretString(cur, arg))
710 // Don't record undo steps if we are in macro mode and
711 // cmd.argument is the next character of the macro name.
712 // Otherwise we'll get an invalid cursor if we undo after
713 // the macro was finished and the macro is a known command,
714 // e.g. sqrt. Cursor::macroModeClose replaces in this case
715 // the InsetMathUnknown with name "frac" by an empty
716 // InsetMathFrac -> a pos value > 0 is invalid.
717 // A side effect is that an undo before the macro is finished
718 // undoes the complete macro, not only the last character.
719 if (!cur.inMacroMode())
722 // spacial handling of space. If we insert an inset
723 // via macro mode, we want to put the cursor inside it
724 // if relevant. Think typing "\frac<space>".
725 if (cmd.argument()[0] == ' '
726 && cur.inMacroMode() && cur.macroName() != "\\"
727 && cur.macroModeClose()) {
728 MathAtom const atom = cur.prevAtom();
729 if (atom->asNestInset() && atom->isActive()) {
731 cur.pushLeft(*cur.nextInset());
733 } else if (!interpretChar(cur, cmd.argument()[0])) {
734 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
739 //case LFUN_SERVER_GET_XY:
740 // sprintf(dispatch_buffer, "%d %d",);
743 case LFUN_SERVER_SET_XY: {
744 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
747 istringstream is(to_utf8(cmd.argument()));
749 cur.setScreenPos(x, y);
753 // Special casing for superscript in case of LyX handling
755 case LFUN_ACCENT_CIRCUMFLEX:
756 if (cmd.argument().empty()) {
757 // do superscript if LyX handles
759 recordUndo(cur, Undo::ATOMIC);
760 script(cur, true, grabAndEraseSelection(cur));
764 case LFUN_ACCENT_UMLAUT:
765 case LFUN_ACCENT_ACUTE:
766 case LFUN_ACCENT_GRAVE:
767 case LFUN_ACCENT_BREVE:
768 case LFUN_ACCENT_DOT:
769 case LFUN_ACCENT_MACRON:
770 case LFUN_ACCENT_CARON:
771 case LFUN_ACCENT_TILDE:
772 case LFUN_ACCENT_CEDILLA:
773 case LFUN_ACCENT_CIRCLE:
774 case LFUN_ACCENT_UNDERDOT:
775 case LFUN_ACCENT_TIE:
776 case LFUN_ACCENT_OGONEK:
777 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
781 case LFUN_FONT_FREE_APPLY:
782 case LFUN_FONT_FREE_UPDATE:
783 handleFont2(cur, cmd.argument());
787 if (currentMode() == TEXT_MODE)
788 handleFont(cur, cmd.argument(), "textbf");
790 handleFont(cur, cmd.argument(), "mathbf");
793 if (currentMode() == TEXT_MODE)
794 handleFont(cur, cmd.argument(), "textsf");
796 handleFont(cur, cmd.argument(), "mathsf");
799 if (currentMode() == TEXT_MODE)
800 handleFont(cur, cmd.argument(), "emph");
802 handleFont(cur, cmd.argument(), "mathcal");
804 case LFUN_FONT_ROMAN:
805 if (currentMode() == TEXT_MODE)
806 handleFont(cur, cmd.argument(), "textrm");
808 handleFont(cur, cmd.argument(), "mathrm");
811 if (currentMode() == TEXT_MODE)
812 handleFont(cur, cmd.argument(), "texttt");
814 handleFont(cur, cmd.argument(), "mathtt");
817 handleFont(cur, cmd.argument(), "mathfrak");
820 if (currentMode() == TEXT_MODE)
821 handleFont(cur, cmd.argument(), "textit");
823 handleFont(cur, cmd.argument(), "mathit");
826 if (currentMode() == TEXT_MODE)
827 // FIXME: should be "noun"
828 handleFont(cur, cmd.argument(), "textsc");
830 handleFont(cur, cmd.argument(), "mathbb");
833 case LFUN_FONT_FREE_APPLY:
834 handleFont(cur, cmd.argument(), "textrm");
837 case LFUN_FONT_DEFAULT:
838 handleFont(cur, cmd.argument(), "textnormal");
841 case LFUN_MATH_MODE: {
843 // ignore math-mode on when already in math mode
844 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
846 cur.macroModeClose();
847 docstring const save_selection = grabAndEraseSelection(cur);
849 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
850 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
852 cur.pushLeft(*cur.nextInset());
853 cur.niceInsert(save_selection);
855 if (currentMode() == Inset::TEXT_MODE) {
856 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
857 cur.message(_("create new math text environment ($...$)"));
859 handleFont(cur, cmd.argument(), "textrm");
860 cur.message(_("entered math text mode (textrm)"));
873 case LFUN_MATH_MATRIX: {
874 recordUndo(cur, Undo::ATOMIC);
879 idocstringstream is(cmd.argument());
880 is >> m >> n >> v_align >> h_align;
887 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
891 case LFUN_MATH_DELIM: {
893 docstring rs = support::split(cmd.argument(), ls, ' ');
894 // Reasonable default values
899 recordUndo(cur, Undo::ATOMIC);
900 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
904 case LFUN_MATH_BIGDELIM: {
905 docstring const lname = from_utf8(cmd.getArg(0));
906 docstring const ldelim = from_utf8(cmd.getArg(1));
907 docstring const rname = from_utf8(cmd.getArg(2));
908 docstring const rdelim = from_utf8(cmd.getArg(3));
909 latexkeys const * l = in_word_set(lname);
910 bool const have_l = l && l->inset == "big" &&
911 InsetMathBig::isBigInsetDelim(ldelim);
912 l = in_word_set(rname);
913 bool const have_r = l && l->inset == "big" &&
914 InsetMathBig::isBigInsetDelim(rdelim);
915 // We mimic LFUN_MATH_DELIM in case we have an empty left
916 // or right delimiter.
917 if (have_l || have_r) {
918 recordUndo(cur, Undo::ATOMIC);
919 docstring const selection = grabAndEraseSelection(cur);
922 cur.insert(MathAtom(new InsetMathBig(lname,
924 cur.niceInsert(selection);
926 cur.insert(MathAtom(new InsetMathBig(rname,
929 // Don't call cur.undispatched() if we did nothing, this would
930 // lead to infinite recursion via Text::dispatch().
934 case LFUN_SPACE_INSERT:
935 case LFUN_MATH_SPACE:
936 recordUndo(cur, Undo::ATOMIC);
937 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
940 case LFUN_ERT_INSERT:
941 // interpret this as if a backslash was typed
942 recordUndo(cur, Undo::ATOMIC);
943 interpretChar(cur, '\\');
946 case LFUN_MATH_SUBSCRIPT:
947 // interpret this as if a _ was typed
948 recordUndo(cur, Undo::ATOMIC);
949 interpretChar(cur, '_');
952 case LFUN_MATH_SUPERSCRIPT:
953 // interpret this as if a ^ was typed
954 recordUndo(cur, Undo::ATOMIC);
955 interpretChar(cur, '^');
958 case LFUN_QUOTE_INSERT:
959 // interpret this as if a straight " was typed
960 recordUndo(cur, Undo::ATOMIC);
961 interpretChar(cur, '\"');
964 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
965 // handling such that "self-insert" works on "arbitrary stuff" too, and
966 // math-insert only handles special math things like "matrix".
967 case LFUN_MATH_INSERT: {
968 recordUndo(cur, Undo::ATOMIC);
969 if (cmd.argument() == "^" || cmd.argument() == "_") {
970 interpretChar(cur, cmd.argument()[0]);
972 cur.niceInsert(cmd.argument());
976 case LFUN_DIALOG_SHOW_NEW_INSET: {
977 docstring const & name = cmd.argument();
980 InsetMathRef tmp(name);
981 data = tmp.createDialogStr(to_utf8(name));
983 cur.bv().showInsetDialog(to_utf8(name), data, 0);
987 case LFUN_INSET_INSERT: {
989 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
998 InsetMath::doDispatch(cur, cmd);
1004 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1005 FuncStatus & flag) const
1007 // the font related toggles
1008 //string tc = "mathnormal";
1010 string const arg = to_utf8(cmd.argument());
1011 switch (cmd.action) {
1012 case LFUN_TABULAR_FEATURE:
1013 flag.enabled(false);
1016 case LFUN_TABULAR_FEATURE:
1017 // FIXME: check temporarily disabled
1019 char align = mathcursor::valign();
1020 if (align == '\0') {
1024 if (cmd.argument().empty()) {
1028 if (!contains("tcb", cmd.argument()[0])) {
1032 flag.setOnOff(cmd.argument()[0] == align);
1035 /// We have to handle them since 1.4 blocks all unhandled actions
1036 case LFUN_FONT_ITAL:
1037 case LFUN_FONT_BOLD:
1038 case LFUN_FONT_SANS:
1039 case LFUN_FONT_EMPH:
1040 case LFUN_FONT_CODE:
1041 case LFUN_FONT_NOUN:
1042 case LFUN_FONT_ROMAN:
1043 case LFUN_FONT_DEFAULT:
1046 case LFUN_MATH_MUTATE:
1047 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1048 flag.setOnOff(false);
1051 // we just need to be in math mode to enable that
1052 case LFUN_MATH_SIZE:
1053 case LFUN_MATH_SPACE:
1054 case LFUN_MATH_LIMITS:
1055 case LFUN_MATH_NONUMBER:
1056 case LFUN_MATH_NUMBER:
1057 case LFUN_MATH_EXTERN:
1061 case LFUN_FONT_FRAK:
1062 flag.enabled(currentMode() != TEXT_MODE);
1065 case LFUN_MATH_INSERT: {
1066 bool const textarg =
1067 arg == "\\textbf" || arg == "\\textsf" ||
1068 arg == "\\textrm" || arg == "\\textmd" ||
1069 arg == "\\textit" || arg == "\\textsc" ||
1070 arg == "\\textsl" || arg == "\\textup" ||
1071 arg == "\\texttt" || arg == "\\textbb" ||
1072 arg == "\\textnormal";
1073 flag.enabled(currentMode() != TEXT_MODE || textarg);
1077 case LFUN_MATH_MATRIX:
1078 flag.enabled(currentMode() == MATH_MODE);
1081 case LFUN_INSET_INSERT: {
1082 // Don't test createMathInset_fromDialogStr(), since
1083 // getStatus is not called with a valid reference and the
1084 // dialog would not be applyable.
1085 string const name = cmd.getArg(0);
1086 flag.enabled(name == "ref");
1090 case LFUN_MATH_DELIM:
1091 case LFUN_MATH_BIGDELIM:
1092 // Don't do this with multi-cell selections
1093 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1096 case LFUN_HYPHENATION_POINT_INSERT:
1097 case LFUN_LIGATURE_BREAK_INSERT:
1098 case LFUN_MENU_SEPARATOR_INSERT:
1099 case LFUN_DOTS_INSERT:
1100 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1101 // FIXME: These would probably make sense in math-text mode
1102 flag.enabled(false);
1113 void InsetMathNest::edit(Cursor & cur, bool left)
1116 cur.idx() = left ? 0 : cur.lastidx();
1117 cur.pos() = left ? 0 : cur.lastpos();
1119 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1123 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1126 int dist_min = 1000000;
1127 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1128 int const d = cell(i).dist(cur.bv(), x, y);
1134 MathData & ar = cell(idx_min);
1136 cur.idx() = idx_min;
1137 cur.pos() = ar.x2pos(x - ar.xo(cur.bv()));
1139 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1140 if (dist_min == 0) {
1142 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1143 if (ar[i]->covers(cur.bv(), x, y))
1144 return ar[i].nucleus()->editXY(cur, x, y);
1150 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1152 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1153 BufferView & bv = cur.bv();
1154 if (cmd.button() == mouse_button::button1) {
1155 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1156 bv.mouseSetCursor(cur);
1157 // Update the cursor update flags as needed:
1159 // Update::Decoration: tells to update the decoration (visual box
1160 // corners that define the inset)/
1161 // Update::FitCursor: adjust the screen to the cursor position if
1163 // cur.result().update(): don't overwrite previously set flags.
1164 cur.updateFlags(Update::Decoration | Update::FitCursor | cur.result().update());
1165 } else if (cmd.button() == mouse_button::button2) {
1167 if (cap::selection()) {
1168 // See comment in Text::dispatch why we do this
1169 cap::copySelectionToStack();
1170 cmd = FuncRequest(LFUN_PASTE, "0");
1171 doDispatch(cur, cmd);
1173 asArray(theSelection().get(), ar);
1176 bv.mouseSetCursor(cur);
1181 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1183 // only select with button 1
1184 if (cmd.button() == mouse_button::button1) {
1185 Cursor & bvcur = cur.bv().cursor();
1186 if (bvcur.anchor_.hasPart(cur)) {
1187 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1188 bvcur.setCursor(cur);
1189 bvcur.selection() = true;
1190 //lyxerr << "MOTION " << bvcur << endl;
1197 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1199 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1201 if (cmd.button() == mouse_button::button1) {
1202 if (!cur.selection())
1205 Cursor & bvcur = cur.bv().cursor();
1206 bvcur.selection() = true;
1215 bool InsetMathNest::interpretChar(Cursor & cur, char_type c)
1217 //lyxerr << "interpret 2: '" << c << "'" << endl;
1218 docstring save_selection;
1219 if (c == '^' || c == '_')
1220 save_selection = grabAndEraseSelection(cur);
1225 if (cur.inMacroMode()) {
1226 docstring name = cur.macroName();
1228 /// are we currently typing '#1' or '#2' or...?
1229 if (name == "\\#") {
1232 if (n >= 1 && n <= 9)
1233 cur.insert(new MathMacroArgument(n));
1237 if (isAlphaASCII(c)) {
1238 cur.activeMacro()->setName(name + docstring(1, c));
1242 // handle 'special char' macros
1247 if (currentMode() == InsetMath::TEXT_MODE)
1248 cur.niceInsert(createInsetMath("textbackslash"));
1250 cur.niceInsert(createInsetMath("backslash"));
1251 } else if (c == '{') {
1253 cur.niceInsert(MathAtom(new InsetMathBrace));
1254 } else if (c == '%') {
1256 cur.niceInsert(MathAtom(new InsetMathComment));
1257 } else if (c == '#') {
1258 BOOST_ASSERT(cur.activeMacro());
1259 cur.activeMacro()->setName(name + docstring(1, c));
1262 cur.niceInsert(createInsetMath(docstring(1, c)));
1267 // One character big delimiters. The others are handled in
1268 // interpretString().
1269 latexkeys const * l = in_word_set(name.substr(1));
1270 if (name[0] == '\\' && l && l->inset == "big") {
1274 delim = from_ascii("\\{");
1277 delim = from_ascii("\\}");
1280 delim = docstring(1, c);
1283 if (InsetMathBig::isBigInsetDelim(delim)) {
1284 // name + delim ared a valid InsetMathBig.
1285 // We can't use cur.macroModeClose() because
1286 // it does not handle delim.
1287 InsetMathUnknown * p = cur.activeMacro();
1290 cur.cell().erase(cur.pos());
1291 cur.plainInsert(MathAtom(
1292 new InsetMathBig(name.substr(1), delim)));
1297 // leave macro mode and try again if necessary
1298 cur.macroModeClose();
1300 cur.niceInsert(MathAtom(new InsetMathBrace));
1302 interpretChar(cur, c);
1306 // This is annoying as one has to press <space> far too often.
1310 // leave autocorrect mode if necessary
1311 if (autocorrect() && c == ' ') {
1312 autocorrect() = false;
1317 // just clear selection on pressing the space bar
1318 if (cur.selection() && c == ' ') {
1319 cur.selection() = false;
1326 //lyxerr << "starting with macro" << endl;
1327 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), false)));
1332 if (currentMode() == InsetMath::TEXT_MODE)
1338 if (currentMode() == InsetMath::TEXT_MODE) {
1339 // insert spaces in text mode,
1340 // but suppress direct insertion of two spaces in a row
1341 // the still allows typing '<space>a<space>' and deleting the 'a', but
1342 // it is better than nothing...
1343 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1345 // FIXME: we have to enable full redraw here because of the
1346 // visual box corners that define the inset. If we know for
1347 // sure that we stay within the same cell we can optimize for
1349 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1353 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1354 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1355 // FIXME: we have to enable full redraw here because of the
1356 // visual box corners that define the inset. If we know for
1357 // sure that we stay within the same cell we can optimize for
1359 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1363 if (cur.popRight()) {
1364 // FIXME: we have to enable full redraw here because of the
1365 // visual box corners that define the inset. If we know for
1366 // sure that we stay within the same cell we can optimize for
1368 //cur.updateFlags(Update::FitCursor);
1372 // if we are at the very end, leave the formula
1373 return cur.pos() != cur.lastpos();
1376 // These shouldn't work in text mode:
1377 if (currentMode() != InsetMath::TEXT_MODE) {
1379 script(cur, false, save_selection);
1383 script(cur, true, save_selection);
1387 cur.niceInsert(createInsetMath("sim"));
1392 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1393 c == '%' || c == '_' || c == '^') {
1394 cur.niceInsert(createInsetMath(docstring(1, c)));
1399 // try auto-correction
1400 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1403 // no special circumstances, so insert the character without any fuss
1405 cur.autocorrect() = true;
1410 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1412 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1414 if (!cur.empty() && cur.pos() > 0 &&
1415 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1416 if (InsetMathBig::isBigInsetDelim(str)) {
1417 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1418 if (prev[0] == '\\') {
1419 prev = prev.substr(1);
1420 latexkeys const * l = in_word_set(prev);
1421 if (l && l->inset == "big") {
1422 cur.cell()[cur.pos() - 1] =
1423 MathAtom(new InsetMathBig(prev, str));
1433 bool InsetMathNest::script(Cursor & cur, bool up,
1434 docstring const & save_selection)
1436 // Hack to get \^ and \_ working
1437 //lyxerr << "handling script: up: " << up << endl;
1438 if (cur.inMacroMode() && cur.macroName() == "\\") {
1440 cur.niceInsert(createInsetMath("mathcircumflex"));
1442 interpretChar(cur, '_');
1446 cur.macroModeClose();
1447 if (asScriptInset() && cur.idx() == 0) {
1448 // we are in a nucleus of a script inset, move to _our_ script
1449 InsetMathScript * inset = asScriptInset();
1450 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1452 cur.idx() = inset->idxOfScript(up);
1454 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1456 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1459 cur.idx() = inset->idxOfScript(up);
1460 cur.pos() = cur.lastpos();
1462 // convert the thing to our left to a scriptinset or create a new
1463 // one if in the very first position of the array
1464 if (cur.pos() == 0) {
1465 //lyxerr << "new scriptinset" << endl;
1466 cur.insert(new InsetMathScript(up));
1468 //lyxerr << "converting prev atom " << endl;
1469 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1472 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1473 // See comment in MathParser.cpp for special handling of {}-bases
1479 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1480 cur.niceInsert(save_selection);
1482 //lyxerr << "inserting selection 2:\n" << save_selection << endl;