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*/)
376 MathData & ar = cur.cell();
377 // remove base-only "scripts"
378 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
379 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
380 if (p && p->nargs() == 1) {
381 MathData ar = p->nuc();
384 cur.adjust(i, ar.size() - 1);
388 // glue adjacent font insets of the same kind
389 for (pos_type i = 0; i + 1 < size(); ++i) {
390 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
391 InsetMathFont const * q = operator[](i + 1)->asFontInset();
392 if (p && q && p->name() == q->name()) {
393 p->cell(0).append(q->cell(0));
403 void InsetMathNest::handleFont
404 (Cursor & cur, docstring const & arg, char const * const font)
406 handleFont(cur, arg, from_ascii(font));
410 void InsetMathNest::handleFont
411 (Cursor & cur, docstring const & arg, docstring const & font)
413 // this whole function is a hack and won't work for incremental font
416 if (cur.inset().asInsetMath()->name() == font) {
417 recordUndoInset(cur, Undo::ATOMIC);
418 cur.handleFont(to_utf8(font));
420 recordUndo(cur, Undo::ATOMIC);
421 cur.handleNest(createInsetMath(font));
427 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
429 recordUndo(cur, Undo::ATOMIC);
432 bv_funcs::string2font(to_utf8(arg), font, b);
433 if (font.color() != Color::inherit) {
434 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
435 cur.handleNest(at, 0);
440 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
442 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
443 //CursorSlice sl = cur.current();
445 switch (cmd.action) {
449 cur.message(_("Paste"));
450 replaceSelection(cur);
452 if (cmd.argument().empty() && !theClipboard().isInternal())
453 topaste = theClipboard().getAsText();
456 idocstringstream is(cmd.argument());
458 topaste = cap::getSelection(cur.buffer(), n);
460 cur.niceInsert(topaste);
461 cur.clearSelection(); // bug 393
468 cutSelection(cur, true, true);
469 cur.message(_("Cut"));
470 // Prevent stale position >= size crash
471 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
477 cur.message(_("Copy"));
480 case LFUN_MOUSE_PRESS:
481 lfunMousePress(cur, cmd);
484 case LFUN_MOUSE_MOTION:
485 lfunMouseMotion(cur, cmd);
488 case LFUN_MOUSE_RELEASE:
489 lfunMouseRelease(cur, cmd);
492 case LFUN_FINISHED_LEFT:
493 cur.bv().cursor() = cur;
496 case LFUN_FINISHED_RIGHT:
498 cur.bv().cursor() = cur;
501 case LFUN_CHAR_FORWARD:
502 cur.updateFlags(Update::Decoration | Update::FitCursor);
503 case LFUN_CHAR_FORWARD_SELECT:
504 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
505 cur.autocorrect() = false;
507 cur.macroModeClose();
508 if (reverseDirectionNeeded(cur))
509 goto goto_char_backwards;
512 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
513 cur.pushLeft(*cur.nextAtom().nucleus());
514 cur.inset().idxFirst(cur);
515 } else if (cur.posRight() || idxRight(cur)
516 || cur.popRight() || cur.selection())
519 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
524 case LFUN_CHAR_BACKWARD:
525 cur.updateFlags(Update::Decoration | Update::FitCursor);
526 case LFUN_CHAR_BACKWARD_SELECT:
527 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
528 cur.autocorrect() = false;
530 cur.macroModeClose();
531 if (reverseDirectionNeeded(cur))
532 goto goto_char_forwards;
535 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
537 cur.push(*cur.nextAtom().nucleus());
538 cur.inset().idxLast(cur);
539 } else if (cur.posLeft() || idxLeft(cur)
540 || cur.popLeft() || cur.selection())
543 cmd = FuncRequest(LFUN_FINISHED_LEFT);
549 cur.updateFlags(Update::Decoration | Update::FitCursor);
551 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
552 if (cur.inMacroMode()) {
554 cur.macroModeClose();
557 cur.selHandle(cmd.action == LFUN_UP_SELECT);
558 if (!cur.upDownInMath(true))
560 // fixes bug 1598. Please check!
565 cur.updateFlags(Update::Decoration | Update::FitCursor);
566 case LFUN_DOWN_SELECT:
567 if (cur.inMacroMode()) {
568 cur.macroModeClose();
571 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
572 if (!cur.upDownInMath(false))
574 // fixes bug 1598. Please check!
578 case LFUN_MOUSE_DOUBLE:
579 case LFUN_MOUSE_TRIPLE:
580 case LFUN_WORD_SELECT:
584 cur.selection() = true;
585 cur.pos() = cur.lastpos();
586 cur.idx() = cur.lastidx();
587 cap::saveSelection(cur);
590 case LFUN_PARAGRAPH_UP:
591 case LFUN_PARAGRAPH_DOWN:
592 cur.updateFlags(Update::Decoration | Update::FitCursor);
593 case LFUN_PARAGRAPH_UP_SELECT:
594 case LFUN_PARAGRAPH_DOWN_SELECT:
597 case LFUN_LINE_BEGIN:
598 case LFUN_WORD_BACKWARD:
599 cur.updateFlags(Update::Decoration | Update::FitCursor);
600 case LFUN_LINE_BEGIN_SELECT:
601 case LFUN_WORD_BACKWARD_SELECT:
602 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
603 cmd.action == LFUN_LINE_BEGIN_SELECT);
604 cur.macroModeClose();
605 if (cur.pos() != 0) {
607 } else if (cur.col() != 0) {
608 cur.idx() -= cur.col();
610 } else if (cur.idx() != 0) {
614 cmd = FuncRequest(LFUN_FINISHED_LEFT);
619 case LFUN_WORD_FORWARD:
621 cur.updateFlags(Update::Decoration | Update::FitCursor);
622 case LFUN_WORD_FORWARD_SELECT:
623 case LFUN_LINE_END_SELECT:
624 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
625 cmd.action == LFUN_LINE_END_SELECT);
626 cur.macroModeClose();
628 if (cur.pos() != cur.lastpos()) {
629 cur.pos() = cur.lastpos();
630 } else if (ncols() && (cur.col() != cur.lastcol())) {
631 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
632 cur.pos() = cur.lastpos();
633 } else if (cur.idx() != cur.lastidx()) {
634 cur.idx() = cur.lastidx();
635 cur.pos() = cur.lastpos();
637 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
642 case LFUN_SCREEN_UP_SELECT:
644 cmd = FuncRequest(LFUN_FINISHED_LEFT);
648 case LFUN_SCREEN_DOWN_SELECT:
649 case LFUN_SCREEN_DOWN:
650 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
654 case LFUN_CELL_FORWARD:
655 cur.updateFlags(Update::Decoration | Update::FitCursor);
656 cur.inset().idxNext(cur);
659 case LFUN_CELL_BACKWARD:
660 cur.updateFlags(Update::Decoration | Update::FitCursor);
661 cur.inset().idxPrev(cur);
664 case LFUN_WORD_DELETE_BACKWARD:
665 case LFUN_CHAR_DELETE_BACKWARD:
667 // May affect external cell:
668 recordUndoInset(cur, Undo::ATOMIC);
670 recordUndo(cur, Undo::ATOMIC);
671 // if the inset can not be removed from within, delete it
672 if (!cur.backspace()) {
673 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
674 cur.innerText()->dispatch(cur, cmd);
678 case LFUN_WORD_DELETE_FORWARD:
679 case LFUN_CHAR_DELETE_FORWARD:
680 if (cur.pos() == cur.lastpos())
681 // May affect external cell:
682 recordUndoInset(cur, Undo::ATOMIC);
684 recordUndo(cur, Undo::ATOMIC);
685 // if the inset can not be removed from within, delete it
687 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
688 cur.innerText()->dispatch(cur, cmd);
694 cur.clearSelection();
696 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
701 case LFUN_INSET_TOGGLE:
707 case LFUN_SELF_INSERT:
708 if (cmd.argument().size() != 1) {
710 docstring const arg = cmd.argument();
711 if (!interpretString(cur, arg))
715 // Don't record undo steps if we are in macro mode and
716 // cmd.argument is the next character of the macro name.
717 // Otherwise we'll get an invalid cursor if we undo after
718 // the macro was finished and the macro is a known command,
719 // e.g. sqrt. Cursor::macroModeClose replaces in this case
720 // the InsetMathUnknown with name "frac" by an empty
721 // InsetMathFrac -> a pos value > 0 is invalid.
722 // A side effect is that an undo before the macro is finished
723 // undoes the complete macro, not only the last character.
724 if (!cur.inMacroMode())
727 // spacial handling of space. If we insert an inset
728 // via macro mode, we want to put the cursor inside it
729 // if relevant. Think typing "\frac<space>".
730 if (cmd.argument()[0] == ' '
731 && cur.inMacroMode() && cur.macroName() != "\\"
732 && cur.macroModeClose()) {
733 MathAtom const atom = cur.prevAtom();
734 if (atom->asNestInset() && atom->isActive()) {
736 cur.pushLeft(*cur.nextInset());
738 } else if (!interpretChar(cur, cmd.argument()[0])) {
739 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
744 //case LFUN_SERVER_GET_XY:
745 // sprintf(dispatch_buffer, "%d %d",);
748 case LFUN_SERVER_SET_XY: {
749 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
752 istringstream is(to_utf8(cmd.argument()));
754 cur.setScreenPos(x, y);
758 // Special casing for superscript in case of LyX handling
760 case LFUN_ACCENT_CIRCUMFLEX:
761 if (cmd.argument().empty()) {
762 // do superscript if LyX handles
764 recordUndo(cur, Undo::ATOMIC);
765 script(cur, true, grabAndEraseSelection(cur));
769 case LFUN_ACCENT_UMLAUT:
770 case LFUN_ACCENT_ACUTE:
771 case LFUN_ACCENT_GRAVE:
772 case LFUN_ACCENT_BREVE:
773 case LFUN_ACCENT_DOT:
774 case LFUN_ACCENT_MACRON:
775 case LFUN_ACCENT_CARON:
776 case LFUN_ACCENT_TILDE:
777 case LFUN_ACCENT_CEDILLA:
778 case LFUN_ACCENT_CIRCLE:
779 case LFUN_ACCENT_UNDERDOT:
780 case LFUN_ACCENT_TIE:
781 case LFUN_ACCENT_OGONEK:
782 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
786 case LFUN_FONT_FREE_APPLY:
787 case LFUN_FONT_FREE_UPDATE:
788 handleFont2(cur, cmd.argument());
792 if (currentMode() == TEXT_MODE)
793 handleFont(cur, cmd.argument(), "textbf");
795 handleFont(cur, cmd.argument(), "mathbf");
798 if (currentMode() == TEXT_MODE)
799 handleFont(cur, cmd.argument(), "textsf");
801 handleFont(cur, cmd.argument(), "mathsf");
804 if (currentMode() == TEXT_MODE)
805 handleFont(cur, cmd.argument(), "emph");
807 handleFont(cur, cmd.argument(), "mathcal");
809 case LFUN_FONT_ROMAN:
810 if (currentMode() == TEXT_MODE)
811 handleFont(cur, cmd.argument(), "textrm");
813 handleFont(cur, cmd.argument(), "mathrm");
816 if (currentMode() == TEXT_MODE)
817 handleFont(cur, cmd.argument(), "texttt");
819 handleFont(cur, cmd.argument(), "mathtt");
822 handleFont(cur, cmd.argument(), "mathfrak");
825 if (currentMode() == TEXT_MODE)
826 handleFont(cur, cmd.argument(), "textit");
828 handleFont(cur, cmd.argument(), "mathit");
831 if (currentMode() == TEXT_MODE)
832 // FIXME: should be "noun"
833 handleFont(cur, cmd.argument(), "textsc");
835 handleFont(cur, cmd.argument(), "mathbb");
838 case LFUN_FONT_FREE_APPLY:
839 handleFont(cur, cmd.argument(), "textrm");
842 case LFUN_FONT_DEFAULT:
843 handleFont(cur, cmd.argument(), "textnormal");
846 case LFUN_MATH_MODE: {
848 // ignore math-mode on when already in math mode
849 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
851 cur.macroModeClose();
852 docstring const save_selection = grabAndEraseSelection(cur);
854 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
855 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
857 cur.pushLeft(*cur.nextInset());
858 cur.niceInsert(save_selection);
860 if (currentMode() == Inset::TEXT_MODE) {
861 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
862 cur.message(_("create new math text environment ($...$)"));
864 handleFont(cur, cmd.argument(), "textrm");
865 cur.message(_("entered math text mode (textrm)"));
878 case LFUN_MATH_MATRIX: {
879 recordUndo(cur, Undo::ATOMIC);
884 idocstringstream is(cmd.argument());
885 is >> m >> n >> v_align >> h_align;
892 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
896 case LFUN_MATH_DELIM: {
898 docstring rs = support::split(cmd.argument(), ls, ' ');
899 // Reasonable default values
904 recordUndo(cur, Undo::ATOMIC);
905 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
909 case LFUN_MATH_BIGDELIM: {
910 docstring const lname = from_utf8(cmd.getArg(0));
911 docstring const ldelim = from_utf8(cmd.getArg(1));
912 docstring const rname = from_utf8(cmd.getArg(2));
913 docstring const rdelim = from_utf8(cmd.getArg(3));
914 latexkeys const * l = in_word_set(lname);
915 bool const have_l = l && l->inset == "big" &&
916 InsetMathBig::isBigInsetDelim(ldelim);
917 l = in_word_set(rname);
918 bool const have_r = l && l->inset == "big" &&
919 InsetMathBig::isBigInsetDelim(rdelim);
920 // We mimic LFUN_MATH_DELIM in case we have an empty left
921 // or right delimiter.
922 if (have_l || have_r) {
923 recordUndo(cur, Undo::ATOMIC);
924 docstring const selection = grabAndEraseSelection(cur);
927 cur.insert(MathAtom(new InsetMathBig(lname,
929 cur.niceInsert(selection);
931 cur.insert(MathAtom(new InsetMathBig(rname,
934 // Don't call cur.undispatched() if we did nothing, this would
935 // lead to infinite recursion via Text::dispatch().
939 case LFUN_SPACE_INSERT:
940 case LFUN_MATH_SPACE:
941 recordUndo(cur, Undo::ATOMIC);
942 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
945 case LFUN_ERT_INSERT:
946 // interpret this as if a backslash was typed
947 recordUndo(cur, Undo::ATOMIC);
948 interpretChar(cur, '\\');
951 case LFUN_MATH_SUBSCRIPT:
952 // interpret this as if a _ was typed
953 recordUndo(cur, Undo::ATOMIC);
954 interpretChar(cur, '_');
957 case LFUN_MATH_SUPERSCRIPT:
958 // interpret this as if a ^ was typed
959 recordUndo(cur, Undo::ATOMIC);
960 interpretChar(cur, '^');
963 case LFUN_QUOTE_INSERT:
964 // interpret this as if a straight " was typed
965 recordUndo(cur, Undo::ATOMIC);
966 interpretChar(cur, '\"');
969 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
970 // handling such that "self-insert" works on "arbitrary stuff" too, and
971 // math-insert only handles special math things like "matrix".
972 case LFUN_MATH_INSERT: {
973 recordUndo(cur, Undo::ATOMIC);
974 if (cmd.argument() == "^" || cmd.argument() == "_") {
975 interpretChar(cur, cmd.argument()[0]);
977 cur.niceInsert(cmd.argument());
981 case LFUN_DIALOG_SHOW_NEW_INSET: {
982 docstring const & name = cmd.argument();
985 InsetMathRef tmp(name);
986 data = tmp.createDialogStr(to_utf8(name));
988 cur.bv().showInsetDialog(to_utf8(name), data, 0);
992 case LFUN_INSET_INSERT: {
994 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1003 InsetMath::doDispatch(cur, cmd);
1009 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1010 FuncStatus & flag) const
1012 // the font related toggles
1013 //string tc = "mathnormal";
1015 string const arg = to_utf8(cmd.argument());
1016 switch (cmd.action) {
1017 case LFUN_TABULAR_FEATURE:
1018 flag.enabled(false);
1021 case LFUN_TABULAR_FEATURE:
1022 // FIXME: check temporarily disabled
1024 char align = mathcursor::valign();
1025 if (align == '\0') {
1029 if (cmd.argument().empty()) {
1033 if (!contains("tcb", cmd.argument()[0])) {
1037 flag.setOnOff(cmd.argument()[0] == align);
1040 /// We have to handle them since 1.4 blocks all unhandled actions
1041 case LFUN_FONT_ITAL:
1042 case LFUN_FONT_BOLD:
1043 case LFUN_FONT_SANS:
1044 case LFUN_FONT_EMPH:
1045 case LFUN_FONT_CODE:
1046 case LFUN_FONT_NOUN:
1047 case LFUN_FONT_ROMAN:
1048 case LFUN_FONT_DEFAULT:
1051 case LFUN_MATH_MUTATE:
1052 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1053 flag.setOnOff(false);
1056 // we just need to be in math mode to enable that
1057 case LFUN_MATH_SIZE:
1058 case LFUN_MATH_SPACE:
1059 case LFUN_MATH_LIMITS:
1060 case LFUN_MATH_NONUMBER:
1061 case LFUN_MATH_NUMBER:
1062 case LFUN_MATH_EXTERN:
1066 case LFUN_FONT_FRAK:
1067 flag.enabled(currentMode() != TEXT_MODE);
1070 case LFUN_MATH_INSERT: {
1071 bool const textarg =
1072 arg == "\\textbf" || arg == "\\textsf" ||
1073 arg == "\\textrm" || arg == "\\textmd" ||
1074 arg == "\\textit" || arg == "\\textsc" ||
1075 arg == "\\textsl" || arg == "\\textup" ||
1076 arg == "\\texttt" || arg == "\\textbb" ||
1077 arg == "\\textnormal";
1078 flag.enabled(currentMode() != TEXT_MODE || textarg);
1082 case LFUN_MATH_MATRIX:
1083 flag.enabled(currentMode() == MATH_MODE);
1086 case LFUN_INSET_INSERT: {
1087 // Don't test createMathInset_fromDialogStr(), since
1088 // getStatus is not called with a valid reference and the
1089 // dialog would not be applyable.
1090 string const name = cmd.getArg(0);
1091 flag.enabled(name == "ref");
1095 case LFUN_MATH_DELIM:
1096 case LFUN_MATH_BIGDELIM:
1097 // Don't do this with multi-cell selections
1098 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1101 case LFUN_HYPHENATION_POINT_INSERT:
1102 case LFUN_LIGATURE_BREAK_INSERT:
1103 case LFUN_MENU_SEPARATOR_INSERT:
1104 case LFUN_DOTS_INSERT:
1105 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1106 // FIXME: These would probably make sense in math-text mode
1107 flag.enabled(false);
1118 void InsetMathNest::edit(Cursor & cur, bool left)
1121 cur.idx() = left ? 0 : cur.lastidx();
1122 cur.pos() = left ? 0 : cur.lastpos();
1124 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1128 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1131 int dist_min = 1000000;
1132 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1133 int const d = cell(i).dist(cur.bv(), x, y);
1139 MathData & ar = cell(idx_min);
1141 cur.idx() = idx_min;
1142 cur.pos() = ar.x2pos(x - ar.xo(cur.bv()));
1144 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1145 if (dist_min == 0) {
1147 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1148 if (ar[i]->covers(cur.bv(), x, y))
1149 return ar[i].nucleus()->editXY(cur, x, y);
1155 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1157 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1158 BufferView & bv = cur.bv();
1159 if (cmd.button() == mouse_button::button1) {
1160 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1161 bv.mouseSetCursor(cur);
1162 // Update the cursor update flags as needed:
1164 // Update::Decoration: tells to update the decoration (visual box
1165 // corners that define the inset)/
1166 // Update::FitCursor: adjust the screen to the cursor position if
1168 // cur.result().update(): don't overwrite previously set flags.
1169 cur.updateFlags(Update::Decoration | Update::FitCursor | cur.result().update());
1170 } else if (cmd.button() == mouse_button::button2) {
1172 if (cap::selection()) {
1173 // See comment in Text::dispatch why we do this
1174 cap::copySelectionToStack();
1175 cmd = FuncRequest(LFUN_PASTE, "0");
1176 doDispatch(cur, cmd);
1178 asArray(theSelection().get(), ar);
1181 bv.mouseSetCursor(cur);
1186 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1188 // only select with button 1
1189 if (cmd.button() == mouse_button::button1) {
1190 Cursor & bvcur = cur.bv().cursor();
1191 if (bvcur.anchor_.hasPart(cur)) {
1192 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1193 bvcur.setCursor(cur);
1194 bvcur.selection() = true;
1195 //lyxerr << "MOTION " << bvcur << endl;
1202 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1204 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1206 if (cmd.button() == mouse_button::button1) {
1207 if (!cur.selection())
1210 Cursor & bvcur = cur.bv().cursor();
1211 bvcur.selection() = true;
1212 cap::saveSelection(bvcur);
1221 bool InsetMathNest::interpretChar(Cursor & cur, char_type c)
1223 //lyxerr << "interpret 2: '" << c << "'" << endl;
1224 docstring save_selection;
1225 if (c == '^' || c == '_')
1226 save_selection = grabAndEraseSelection(cur);
1231 if (cur.inMacroMode()) {
1232 docstring name = cur.macroName();
1234 /// are we currently typing '#1' or '#2' or...?
1235 if (name == "\\#") {
1238 if (n >= 1 && n <= 9)
1239 cur.insert(new MathMacroArgument(n));
1243 if (isAlphaASCII(c)) {
1244 cur.activeMacro()->setName(name + docstring(1, c));
1248 // handle 'special char' macros
1253 if (currentMode() == InsetMath::TEXT_MODE)
1254 cur.niceInsert(createInsetMath("textbackslash"));
1256 cur.niceInsert(createInsetMath("backslash"));
1257 } else if (c == '{') {
1259 cur.niceInsert(MathAtom(new InsetMathBrace));
1260 } else if (c == '%') {
1262 cur.niceInsert(MathAtom(new InsetMathComment));
1263 } else if (c == '#') {
1264 BOOST_ASSERT(cur.activeMacro());
1265 cur.activeMacro()->setName(name + docstring(1, c));
1268 cur.niceInsert(createInsetMath(docstring(1, c)));
1273 // One character big delimiters. The others are handled in
1274 // interpretString().
1275 latexkeys const * l = in_word_set(name.substr(1));
1276 if (name[0] == '\\' && l && l->inset == "big") {
1280 delim = from_ascii("\\{");
1283 delim = from_ascii("\\}");
1286 delim = docstring(1, c);
1289 if (InsetMathBig::isBigInsetDelim(delim)) {
1290 // name + delim ared a valid InsetMathBig.
1291 // We can't use cur.macroModeClose() because
1292 // it does not handle delim.
1293 InsetMathUnknown * p = cur.activeMacro();
1296 cur.cell().erase(cur.pos());
1297 cur.plainInsert(MathAtom(
1298 new InsetMathBig(name.substr(1), delim)));
1303 // leave macro mode and try again if necessary
1304 cur.macroModeClose();
1306 cur.niceInsert(MathAtom(new InsetMathBrace));
1308 interpretChar(cur, c);
1312 // This is annoying as one has to press <space> far too often.
1316 // leave autocorrect mode if necessary
1317 if (autocorrect() && c == ' ') {
1318 autocorrect() = false;
1323 // just clear selection on pressing the space bar
1324 if (cur.selection() && c == ' ') {
1325 cur.selection() = false;
1332 //lyxerr << "starting with macro" << endl;
1333 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), false)));
1338 if (currentMode() == InsetMath::TEXT_MODE)
1344 if (currentMode() == InsetMath::TEXT_MODE) {
1345 // insert spaces in text mode,
1346 // but suppress direct insertion of two spaces in a row
1347 // the still allows typing '<space>a<space>' and deleting the 'a', but
1348 // it is better than nothing...
1349 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1351 // FIXME: we have to enable full redraw here because of the
1352 // visual box corners that define the inset. If we know for
1353 // sure that we stay within the same cell we can optimize for
1355 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1359 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1360 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1361 // FIXME: we have to enable full redraw here because of the
1362 // visual box corners that define the inset. If we know for
1363 // sure that we stay within the same cell we can optimize for
1365 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1369 if (cur.popRight()) {
1370 // FIXME: we have to enable full redraw here because of the
1371 // visual box corners that define the inset. If we know for
1372 // sure that we stay within the same cell we can optimize for
1374 //cur.updateFlags(Update::FitCursor);
1378 // if we are at the very end, leave the formula
1379 return cur.pos() != cur.lastpos();
1382 // These shouldn't work in text mode:
1383 if (currentMode() != InsetMath::TEXT_MODE) {
1385 script(cur, false, save_selection);
1389 script(cur, true, save_selection);
1393 cur.niceInsert(createInsetMath("sim"));
1398 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1399 c == '%' || c == '_' || c == '^') {
1400 cur.niceInsert(createInsetMath(docstring(1, c)));
1405 // try auto-correction
1406 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1409 // no special circumstances, so insert the character without any fuss
1411 cur.autocorrect() = true;
1416 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1418 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1420 if (!cur.empty() && cur.pos() > 0 &&
1421 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1422 if (InsetMathBig::isBigInsetDelim(str)) {
1423 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1424 if (prev[0] == '\\') {
1425 prev = prev.substr(1);
1426 latexkeys const * l = in_word_set(prev);
1427 if (l && l->inset == "big") {
1428 cur.cell()[cur.pos() - 1] =
1429 MathAtom(new InsetMathBig(prev, str));
1439 bool InsetMathNest::script(Cursor & cur, bool up,
1440 docstring const & save_selection)
1442 // Hack to get \^ and \_ working
1443 //lyxerr << "handling script: up: " << up << endl;
1444 if (cur.inMacroMode() && cur.macroName() == "\\") {
1446 cur.niceInsert(createInsetMath("mathcircumflex"));
1448 interpretChar(cur, '_');
1452 cur.macroModeClose();
1453 if (asScriptInset() && cur.idx() == 0) {
1454 // we are in a nucleus of a script inset, move to _our_ script
1455 InsetMathScript * inset = asScriptInset();
1456 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1458 cur.idx() = inset->idxOfScript(up);
1460 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1462 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1465 cur.idx() = inset->idxOfScript(up);
1466 cur.pos() = cur.lastpos();
1468 // convert the thing to our left to a scriptinset or create a new
1469 // one if in the very first position of the array
1470 if (cur.pos() == 0) {
1471 //lyxerr << "new scriptinset" << endl;
1472 cur.insert(new InsetMathScript(up));
1474 //lyxerr << "converting prev atom " << endl;
1475 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1478 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1479 // See comment in MathParser.cpp for special handling of {}-bases
1485 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1486 cur.niceInsert(save_selection);
1488 //lyxerr << "inserting selection 2:\n" << save_selection << endl;