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);
550 cur.updateFlags(Update::Decoration | Update::FitCursor);
551 case LFUN_DOWN_SELECT:
552 case LFUN_UP_SELECT: {
553 // close active macro
554 if (cur.inMacroMode()) {
555 cur.macroModeClose();
559 // stop/start the selection
560 bool select = cmd.action == LFUN_DOWN_SELECT ||
561 cmd.action == LFUN_UP_SELECT;
562 cur.selHandle(select);
565 bool up = cmd.action == LFUN_UP || cmd.action == LFUN_UP_SELECT;
566 bool successful = cur.upDownInMath(up);
568 // notify left insets and give them chance to set update flags
569 lyx::notifyCursorLeaves(cur.beforeDispatchCursor(), cur);
576 case LFUN_MOUSE_DOUBLE:
577 case LFUN_MOUSE_TRIPLE:
578 case LFUN_WORD_SELECT:
582 cur.selection() = true;
583 cur.pos() = cur.lastpos();
584 cur.idx() = cur.lastidx();
587 case LFUN_PARAGRAPH_UP:
588 case LFUN_PARAGRAPH_DOWN:
589 cur.updateFlags(Update::Decoration | Update::FitCursor);
590 case LFUN_PARAGRAPH_UP_SELECT:
591 case LFUN_PARAGRAPH_DOWN_SELECT:
594 case LFUN_LINE_BEGIN:
595 case LFUN_WORD_BACKWARD:
596 cur.updateFlags(Update::Decoration | Update::FitCursor);
597 case LFUN_LINE_BEGIN_SELECT:
598 case LFUN_WORD_BACKWARD_SELECT:
599 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
600 cmd.action == LFUN_LINE_BEGIN_SELECT);
601 cur.macroModeClose();
602 if (cur.pos() != 0) {
604 } else if (cur.col() != 0) {
605 cur.idx() -= cur.col();
607 } else if (cur.idx() != 0) {
611 cmd = FuncRequest(LFUN_FINISHED_LEFT);
616 case LFUN_WORD_FORWARD:
618 cur.updateFlags(Update::Decoration | Update::FitCursor);
619 case LFUN_WORD_FORWARD_SELECT:
620 case LFUN_LINE_END_SELECT:
621 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
622 cmd.action == LFUN_LINE_END_SELECT);
623 cur.macroModeClose();
625 if (cur.pos() != cur.lastpos()) {
626 cur.pos() = cur.lastpos();
627 } else if (ncols() && (cur.col() != cur.lastcol())) {
628 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
629 cur.pos() = cur.lastpos();
630 } else if (cur.idx() != cur.lastidx()) {
631 cur.idx() = cur.lastidx();
632 cur.pos() = cur.lastpos();
634 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
639 case LFUN_SCREEN_UP_SELECT:
641 cmd = FuncRequest(LFUN_FINISHED_LEFT);
645 case LFUN_SCREEN_DOWN_SELECT:
646 case LFUN_SCREEN_DOWN:
647 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
651 case LFUN_CELL_FORWARD:
652 cur.updateFlags(Update::Decoration | Update::FitCursor);
653 cur.inset().idxNext(cur);
656 case LFUN_CELL_BACKWARD:
657 cur.updateFlags(Update::Decoration | Update::FitCursor);
658 cur.inset().idxPrev(cur);
661 case LFUN_WORD_DELETE_BACKWARD:
662 case LFUN_CHAR_DELETE_BACKWARD:
664 // May affect external cell:
665 recordUndoInset(cur, Undo::ATOMIC);
667 recordUndo(cur, Undo::ATOMIC);
668 // if the inset can not be removed from within, delete it
669 if (!cur.backspace()) {
670 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
671 cur.innerText()->dispatch(cur, cmd);
675 case LFUN_WORD_DELETE_FORWARD:
676 case LFUN_CHAR_DELETE_FORWARD:
677 if (cur.pos() == cur.lastpos())
678 // May affect external cell:
679 recordUndoInset(cur, Undo::ATOMIC);
681 recordUndo(cur, Undo::ATOMIC);
682 // if the inset can not be removed from within, delete it
684 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
685 cur.innerText()->dispatch(cur, cmd);
691 cur.clearSelection();
693 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
698 case LFUN_INSET_TOGGLE:
704 case LFUN_SELF_INSERT:
705 if (cmd.argument().size() != 1) {
707 docstring const arg = cmd.argument();
708 if (!interpretString(cur, arg))
712 // Don't record undo steps if we are in macro mode and
713 // cmd.argument is the next character of the macro name.
714 // Otherwise we'll get an invalid cursor if we undo after
715 // the macro was finished and the macro is a known command,
716 // e.g. sqrt. Cursor::macroModeClose replaces in this case
717 // the InsetMathUnknown with name "frac" by an empty
718 // InsetMathFrac -> a pos value > 0 is invalid.
719 // A side effect is that an undo before the macro is finished
720 // undoes the complete macro, not only the last character.
721 if (!cur.inMacroMode())
724 // spacial handling of space. If we insert an inset
725 // via macro mode, we want to put the cursor inside it
726 // if relevant. Think typing "\frac<space>".
727 if (cmd.argument()[0] == ' '
728 && cur.inMacroMode() && cur.macroName() != "\\"
729 && cur.macroModeClose()) {
730 MathAtom const atom = cur.prevAtom();
731 if (atom->asNestInset() && atom->isActive()) {
733 cur.pushLeft(*cur.nextInset());
735 } else if (!interpretChar(cur, cmd.argument()[0])) {
736 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
741 //case LFUN_SERVER_GET_XY:
742 // sprintf(dispatch_buffer, "%d %d",);
745 case LFUN_SERVER_SET_XY: {
746 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
749 istringstream is(to_utf8(cmd.argument()));
751 cur.setScreenPos(x, y);
755 // Special casing for superscript in case of LyX handling
757 case LFUN_ACCENT_CIRCUMFLEX:
758 if (cmd.argument().empty()) {
759 // do superscript if LyX handles
761 recordUndo(cur, Undo::ATOMIC);
762 script(cur, true, grabAndEraseSelection(cur));
766 case LFUN_ACCENT_UMLAUT:
767 case LFUN_ACCENT_ACUTE:
768 case LFUN_ACCENT_GRAVE:
769 case LFUN_ACCENT_BREVE:
770 case LFUN_ACCENT_DOT:
771 case LFUN_ACCENT_MACRON:
772 case LFUN_ACCENT_CARON:
773 case LFUN_ACCENT_TILDE:
774 case LFUN_ACCENT_CEDILLA:
775 case LFUN_ACCENT_CIRCLE:
776 case LFUN_ACCENT_UNDERDOT:
777 case LFUN_ACCENT_TIE:
778 case LFUN_ACCENT_OGONEK:
779 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
783 case LFUN_FONT_FREE_APPLY:
784 case LFUN_FONT_FREE_UPDATE:
785 handleFont2(cur, cmd.argument());
789 if (currentMode() == TEXT_MODE)
790 handleFont(cur, cmd.argument(), "textbf");
792 handleFont(cur, cmd.argument(), "mathbf");
795 if (currentMode() == TEXT_MODE)
796 handleFont(cur, cmd.argument(), "textsf");
798 handleFont(cur, cmd.argument(), "mathsf");
801 if (currentMode() == TEXT_MODE)
802 handleFont(cur, cmd.argument(), "emph");
804 handleFont(cur, cmd.argument(), "mathcal");
806 case LFUN_FONT_ROMAN:
807 if (currentMode() == TEXT_MODE)
808 handleFont(cur, cmd.argument(), "textrm");
810 handleFont(cur, cmd.argument(), "mathrm");
813 if (currentMode() == TEXT_MODE)
814 handleFont(cur, cmd.argument(), "texttt");
816 handleFont(cur, cmd.argument(), "mathtt");
819 handleFont(cur, cmd.argument(), "mathfrak");
822 if (currentMode() == TEXT_MODE)
823 handleFont(cur, cmd.argument(), "textit");
825 handleFont(cur, cmd.argument(), "mathit");
828 if (currentMode() == TEXT_MODE)
829 // FIXME: should be "noun"
830 handleFont(cur, cmd.argument(), "textsc");
832 handleFont(cur, cmd.argument(), "mathbb");
835 case LFUN_FONT_FREE_APPLY:
836 handleFont(cur, cmd.argument(), "textrm");
839 case LFUN_FONT_DEFAULT:
840 handleFont(cur, cmd.argument(), "textnormal");
843 case LFUN_MATH_MODE: {
845 // ignore math-mode on when already in math mode
846 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
848 cur.macroModeClose();
849 docstring const save_selection = grabAndEraseSelection(cur);
851 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
852 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
854 cur.pushLeft(*cur.nextInset());
855 cur.niceInsert(save_selection);
857 if (currentMode() == Inset::TEXT_MODE) {
858 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
859 cur.message(_("create new math text environment ($...$)"));
861 handleFont(cur, cmd.argument(), "textrm");
862 cur.message(_("entered math text mode (textrm)"));
875 case LFUN_MATH_MATRIX: {
876 recordUndo(cur, Undo::ATOMIC);
881 idocstringstream is(cmd.argument());
882 is >> m >> n >> v_align >> h_align;
889 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
893 case LFUN_MATH_DELIM: {
895 docstring rs = support::split(cmd.argument(), ls, ' ');
896 // Reasonable default values
901 recordUndo(cur, Undo::ATOMIC);
902 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
906 case LFUN_MATH_BIGDELIM: {
907 docstring const lname = from_utf8(cmd.getArg(0));
908 docstring const ldelim = from_utf8(cmd.getArg(1));
909 docstring const rname = from_utf8(cmd.getArg(2));
910 docstring const rdelim = from_utf8(cmd.getArg(3));
911 latexkeys const * l = in_word_set(lname);
912 bool const have_l = l && l->inset == "big" &&
913 InsetMathBig::isBigInsetDelim(ldelim);
914 l = in_word_set(rname);
915 bool const have_r = l && l->inset == "big" &&
916 InsetMathBig::isBigInsetDelim(rdelim);
917 // We mimic LFUN_MATH_DELIM in case we have an empty left
918 // or right delimiter.
919 if (have_l || have_r) {
920 recordUndo(cur, Undo::ATOMIC);
921 docstring const selection = grabAndEraseSelection(cur);
924 cur.insert(MathAtom(new InsetMathBig(lname,
926 cur.niceInsert(selection);
928 cur.insert(MathAtom(new InsetMathBig(rname,
931 // Don't call cur.undispatched() if we did nothing, this would
932 // lead to infinite recursion via Text::dispatch().
936 case LFUN_SPACE_INSERT:
937 case LFUN_MATH_SPACE:
938 recordUndo(cur, Undo::ATOMIC);
939 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
942 case LFUN_ERT_INSERT:
943 // interpret this as if a backslash was typed
944 recordUndo(cur, Undo::ATOMIC);
945 interpretChar(cur, '\\');
948 case LFUN_MATH_SUBSCRIPT:
949 // interpret this as if a _ was typed
950 recordUndo(cur, Undo::ATOMIC);
951 interpretChar(cur, '_');
954 case LFUN_MATH_SUPERSCRIPT:
955 // interpret this as if a ^ was typed
956 recordUndo(cur, Undo::ATOMIC);
957 interpretChar(cur, '^');
960 case LFUN_QUOTE_INSERT:
961 // interpret this as if a straight " was typed
962 recordUndo(cur, Undo::ATOMIC);
963 interpretChar(cur, '\"');
966 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
967 // handling such that "self-insert" works on "arbitrary stuff" too, and
968 // math-insert only handles special math things like "matrix".
969 case LFUN_MATH_INSERT: {
970 recordUndo(cur, Undo::ATOMIC);
971 if (cmd.argument() == "^" || cmd.argument() == "_") {
972 interpretChar(cur, cmd.argument()[0]);
974 cur.niceInsert(cmd.argument());
978 case LFUN_DIALOG_SHOW_NEW_INSET: {
979 docstring const & name = cmd.argument();
982 InsetMathRef tmp(name);
983 data = tmp.createDialogStr(to_utf8(name));
985 cur.bv().showInsetDialog(to_utf8(name), data, 0);
989 case LFUN_INSET_INSERT: {
991 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1000 InsetMath::doDispatch(cur, cmd);
1006 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1007 FuncStatus & flag) const
1009 // the font related toggles
1010 //string tc = "mathnormal";
1012 string const arg = to_utf8(cmd.argument());
1013 switch (cmd.action) {
1014 case LFUN_TABULAR_FEATURE:
1015 flag.enabled(false);
1018 case LFUN_TABULAR_FEATURE:
1019 // FIXME: check temporarily disabled
1021 char align = mathcursor::valign();
1022 if (align == '\0') {
1026 if (cmd.argument().empty()) {
1030 if (!contains("tcb", cmd.argument()[0])) {
1034 flag.setOnOff(cmd.argument()[0] == align);
1037 /// We have to handle them since 1.4 blocks all unhandled actions
1038 case LFUN_FONT_ITAL:
1039 case LFUN_FONT_BOLD:
1040 case LFUN_FONT_SANS:
1041 case LFUN_FONT_EMPH:
1042 case LFUN_FONT_CODE:
1043 case LFUN_FONT_NOUN:
1044 case LFUN_FONT_ROMAN:
1045 case LFUN_FONT_DEFAULT:
1048 case LFUN_MATH_MUTATE:
1049 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1050 flag.setOnOff(false);
1053 // we just need to be in math mode to enable that
1054 case LFUN_MATH_SIZE:
1055 case LFUN_MATH_SPACE:
1056 case LFUN_MATH_LIMITS:
1057 case LFUN_MATH_NONUMBER:
1058 case LFUN_MATH_NUMBER:
1059 case LFUN_MATH_EXTERN:
1063 case LFUN_FONT_FRAK:
1064 flag.enabled(currentMode() != TEXT_MODE);
1067 case LFUN_MATH_INSERT: {
1068 bool const textarg =
1069 arg == "\\textbf" || arg == "\\textsf" ||
1070 arg == "\\textrm" || arg == "\\textmd" ||
1071 arg == "\\textit" || arg == "\\textsc" ||
1072 arg == "\\textsl" || arg == "\\textup" ||
1073 arg == "\\texttt" || arg == "\\textbb" ||
1074 arg == "\\textnormal";
1075 flag.enabled(currentMode() != TEXT_MODE || textarg);
1079 case LFUN_MATH_MATRIX:
1080 flag.enabled(currentMode() == MATH_MODE);
1083 case LFUN_INSET_INSERT: {
1084 // Don't test createMathInset_fromDialogStr(), since
1085 // getStatus is not called with a valid reference and the
1086 // dialog would not be applyable.
1087 string const name = cmd.getArg(0);
1088 flag.enabled(name == "ref");
1092 case LFUN_MATH_DELIM:
1093 case LFUN_MATH_BIGDELIM:
1094 // Don't do this with multi-cell selections
1095 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1098 case LFUN_HYPHENATION_POINT_INSERT:
1099 case LFUN_LIGATURE_BREAK_INSERT:
1100 case LFUN_MENU_SEPARATOR_INSERT:
1101 case LFUN_DOTS_INSERT:
1102 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1103 // FIXME: These would probably make sense in math-text mode
1104 flag.enabled(false);
1115 void InsetMathNest::edit(Cursor & cur, bool left)
1118 cur.idx() = left ? 0 : cur.lastidx();
1119 cur.pos() = left ? 0 : cur.lastpos();
1121 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1125 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1128 int dist_min = 1000000;
1129 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1130 int const d = cell(i).dist(cur.bv(), x, y);
1136 MathData & ar = cell(idx_min);
1138 cur.idx() = idx_min;
1139 cur.pos() = ar.x2pos(x - ar.xo(cur.bv()));
1141 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1142 if (dist_min == 0) {
1144 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1145 if (ar[i]->covers(cur.bv(), x, y))
1146 return ar[i].nucleus()->editXY(cur, x, y);
1152 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1154 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1155 BufferView & bv = cur.bv();
1156 if (cmd.button() == mouse_button::button1) {
1157 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1158 bv.mouseSetCursor(cur);
1159 // Update the cursor update flags as needed:
1161 // Update::Decoration: tells to update the decoration (visual box
1162 // corners that define the inset)/
1163 // Update::FitCursor: adjust the screen to the cursor position if
1165 // cur.result().update(): don't overwrite previously set flags.
1166 cur.updateFlags(Update::Decoration | Update::FitCursor | cur.result().update());
1167 } else if (cmd.button() == mouse_button::button2) {
1169 if (cap::selection()) {
1170 // See comment in Text::dispatch why we do this
1171 cap::copySelectionToStack();
1172 cmd = FuncRequest(LFUN_PASTE, "0");
1173 doDispatch(cur, cmd);
1175 asArray(theSelection().get(), ar);
1178 bv.mouseSetCursor(cur);
1183 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1185 // only select with button 1
1186 if (cmd.button() == mouse_button::button1) {
1187 Cursor & bvcur = cur.bv().cursor();
1188 if (bvcur.anchor_.hasPart(cur)) {
1189 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1190 bvcur.setCursor(cur);
1191 bvcur.selection() = true;
1192 //lyxerr << "MOTION " << bvcur << endl;
1199 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1201 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1203 if (cmd.button() == mouse_button::button1) {
1204 if (!cur.selection())
1207 Cursor & bvcur = cur.bv().cursor();
1208 bvcur.selection() = true;
1217 bool InsetMathNest::interpretChar(Cursor & cur, char_type c)
1219 //lyxerr << "interpret 2: '" << c << "'" << endl;
1220 docstring save_selection;
1221 if (c == '^' || c == '_')
1222 save_selection = grabAndEraseSelection(cur);
1227 if (cur.inMacroMode()) {
1228 docstring name = cur.macroName();
1230 /// are we currently typing '#1' or '#2' or...?
1231 if (name == "\\#") {
1234 if (n >= 1 && n <= 9)
1235 cur.insert(new MathMacroArgument(n));
1239 if (isAlphaASCII(c)) {
1240 cur.activeMacro()->setName(name + docstring(1, c));
1244 // handle 'special char' macros
1249 if (currentMode() == InsetMath::TEXT_MODE)
1250 cur.niceInsert(createInsetMath("textbackslash"));
1252 cur.niceInsert(createInsetMath("backslash"));
1253 } else if (c == '{') {
1255 cur.niceInsert(MathAtom(new InsetMathBrace));
1256 } else if (c == '%') {
1258 cur.niceInsert(MathAtom(new InsetMathComment));
1259 } else if (c == '#') {
1260 BOOST_ASSERT(cur.activeMacro());
1261 cur.activeMacro()->setName(name + docstring(1, c));
1264 cur.niceInsert(createInsetMath(docstring(1, c)));
1269 // One character big delimiters. The others are handled in
1270 // interpretString().
1271 latexkeys const * l = in_word_set(name.substr(1));
1272 if (name[0] == '\\' && l && l->inset == "big") {
1276 delim = from_ascii("\\{");
1279 delim = from_ascii("\\}");
1282 delim = docstring(1, c);
1285 if (InsetMathBig::isBigInsetDelim(delim)) {
1286 // name + delim ared a valid InsetMathBig.
1287 // We can't use cur.macroModeClose() because
1288 // it does not handle delim.
1289 InsetMathUnknown * p = cur.activeMacro();
1292 cur.cell().erase(cur.pos());
1293 cur.plainInsert(MathAtom(
1294 new InsetMathBig(name.substr(1), delim)));
1299 // leave macro mode and try again if necessary
1300 cur.macroModeClose();
1302 cur.niceInsert(MathAtom(new InsetMathBrace));
1304 interpretChar(cur, c);
1308 // This is annoying as one has to press <space> far too often.
1312 // leave autocorrect mode if necessary
1313 if (autocorrect() && c == ' ') {
1314 autocorrect() = false;
1319 // just clear selection on pressing the space bar
1320 if (cur.selection() && c == ' ') {
1321 cur.selection() = false;
1328 //lyxerr << "starting with macro" << endl;
1329 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), false)));
1334 if (currentMode() == InsetMath::TEXT_MODE)
1340 if (currentMode() == InsetMath::TEXT_MODE) {
1341 // insert spaces in text mode,
1342 // but suppress direct insertion of two spaces in a row
1343 // the still allows typing '<space>a<space>' and deleting the 'a', but
1344 // it is better than nothing...
1345 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1347 // FIXME: we have to enable full redraw here because of the
1348 // visual box corners that define the inset. If we know for
1349 // sure that we stay within the same cell we can optimize for
1351 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1355 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1356 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1357 // FIXME: we have to enable full redraw here because of the
1358 // visual box corners that define the inset. If we know for
1359 // sure that we stay within the same cell we can optimize for
1361 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1365 if (cur.popRight()) {
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::FitCursor);
1374 // if we are at the very end, leave the formula
1375 return cur.pos() != cur.lastpos();
1378 // These shouldn't work in text mode:
1379 if (currentMode() != InsetMath::TEXT_MODE) {
1381 script(cur, false, save_selection);
1385 script(cur, true, save_selection);
1389 cur.niceInsert(createInsetMath("sim"));
1394 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1395 c == '%' || c == '_' || c == '^') {
1396 cur.niceInsert(createInsetMath(docstring(1, c)));
1401 // try auto-correction
1402 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1405 // no special circumstances, so insert the character without any fuss
1407 cur.autocorrect() = true;
1412 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1414 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1416 if (!cur.empty() && cur.pos() > 0 &&
1417 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1418 if (InsetMathBig::isBigInsetDelim(str)) {
1419 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1420 if (prev[0] == '\\') {
1421 prev = prev.substr(1);
1422 latexkeys const * l = in_word_set(prev);
1423 if (l && l->inset == "big") {
1424 cur.cell()[cur.pos() - 1] =
1425 MathAtom(new InsetMathBig(prev, str));
1435 bool InsetMathNest::script(Cursor & cur, bool up,
1436 docstring const & save_selection)
1438 // Hack to get \^ and \_ working
1439 //lyxerr << "handling script: up: " << up << endl;
1440 if (cur.inMacroMode() && cur.macroName() == "\\") {
1442 cur.niceInsert(createInsetMath("mathcircumflex"));
1444 interpretChar(cur, '_');
1448 cur.macroModeClose();
1449 if (asScriptInset() && cur.idx() == 0) {
1450 // we are in a nucleus of a script inset, move to _our_ script
1451 InsetMathScript * inset = asScriptInset();
1452 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1454 cur.idx() = inset->idxOfScript(up);
1456 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1458 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1461 cur.idx() = inset->idxOfScript(up);
1462 cur.pos() = cur.lastpos();
1464 // convert the thing to our left to a scriptinset or create a new
1465 // one if in the very first position of the array
1466 if (cur.pos() == 0) {
1467 //lyxerr << "new scriptinset" << endl;
1468 cur.insert(new InsetMathScript(up));
1470 //lyxerr << "converting prev atom " << endl;
1471 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1474 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1475 // See comment in MathParser.cpp for special handling of {}-bases
1481 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1482 cur.niceInsert(save_selection);
1484 //lyxerr << "inserting selection 2:\n" << save_selection << endl;