2 * \file InsetMathNest.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetMathNest.h"
15 #include "InsetMathArray.h"
16 #include "InsetMathBig.h"
17 #include "InsetMathBox.h"
18 #include "InsetMathBrace.h"
19 #include "InsetMathColor.h"
20 #include "InsetMathComment.h"
21 #include "InsetMathDelim.h"
22 #include "InsetMathHull.h"
23 //#include "InsetMathMBox.h"
24 #include "InsetMathRef.h"
25 #include "InsetMathScript.h"
26 #include "InsetMathSpace.h"
27 #include "InsetMathSymbol.h"
28 #include "InsetMathUnknown.h"
30 #include "MathFactory.h"
31 #include "MathMacroArgument.h"
32 #include "MathParser.h"
33 #include "MathStream.h"
34 #include "MathSupport.h"
36 #include "bufferview_funcs.h"
37 #include "BufferView.h"
39 #include "CoordCache.h"
41 #include "CutAndPaste.h"
43 #include "DispatchResult.h"
44 #include "FuncRequest.h"
45 #include "FuncStatus.h"
48 #include "OutputParams.h"
51 #include "support/lstrings.h"
52 #include "support/textutils.h"
54 #include "frontends/Clipboard.h"
55 #include "frontends/Painter.h"
56 #include "frontends/Selection.h"
58 #include "FuncRequest.h"
65 using cap::copySelection;
66 using cap::grabAndEraseSelection;
67 using cap::cutSelection;
68 using cap::replaceSelection;
69 using cap::selClearOrDel;
73 using std::istringstream;
76 InsetMathNest::InsetMathNest(idx_type nargs)
77 : cells_(nargs), lock_(false)
81 InsetMath::idx_type InsetMathNest::nargs() const
87 void InsetMathNest::cursorPos(BufferView const & bv,
88 CursorSlice const & sl, bool /*boundary*/,
89 int & x, int & y) const
91 // FIXME: This is a hack. Ideally, the coord cache should not store
92 // absolute positions, but relative ones. This would mean to call
93 // setXY() not in MathData::draw(), but in the parent insets' draw()
94 // with the correctly adjusted x,y values. But this means that we'd have
95 // to touch all (math)inset's draw() methods. Right now, we'll store
96 // absolute value, and make them here relative, only to make them
97 // absolute again when actually drawing the cursor. What a mess.
98 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
99 MathData const & ar = sl.cell();
100 CoordCache const & coord_cache = bv.coordCache();
101 if (!coord_cache.getArrays().has(&ar)) {
102 // this can (semi-)legally happen if we just created this cell
103 // and it never has been drawn before. So don't ASSERT.
104 //lyxerr << "no cached data for array " << &ar << endl;
109 Point const pt = coord_cache.getArrays().xy(&ar);
110 if (!coord_cache.getInsets().has(this)) {
112 //lyxerr << "no cached data for inset " << this << endl;
117 Point const pt2 = coord_cache.getInsets().xy(this);
118 //lyxerr << "retrieving position cache for MathData "
119 // << pt.x_ << ' ' << pt.y_ << std::endl;
120 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
122 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
123 // << " asc: " << ascent() << " des: " << descent()
124 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
125 // move cursor visually into empty cells ("blue rectangles");
131 void InsetMathNest::metrics(MetricsInfo const & mi) const
134 for (idx_type i = 0, n = nargs(); i != n; ++i)
139 bool InsetMathNest::idxNext(Cursor & cur) const
141 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
142 if (cur.idx() == cur.lastidx())
150 bool InsetMathNest::idxRight(Cursor & cur) const
156 bool InsetMathNest::idxPrev(Cursor & cur) const
158 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
162 cur.pos() = cur.lastpos();
167 bool InsetMathNest::idxLeft(Cursor & cur) const
173 bool InsetMathNest::idxFirst(Cursor & cur) const
175 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
184 bool InsetMathNest::idxLast(Cursor & cur) const
186 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
189 cur.idx() = cur.lastidx();
190 cur.pos() = cur.lastpos();
195 void InsetMathNest::dump() const
197 odocstringstream oss;
199 os << "---------------------------------------------\n";
202 for (idx_type i = 0, n = nargs(); i != n; ++i)
203 os << cell(i) << "\n";
204 os << "---------------------------------------------\n";
205 lyxerr << to_utf8(oss.str());
209 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
213 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
216 setPosCache(pi, x, y);
220 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
222 BufferView & bv = *pi.base.bv;
223 // this should use the x/y values given, not the cached values
224 Cursor & cur = bv.cursor();
225 if (!cur.selection())
227 if (!ptr_cmp(&cur.inset(), this))
230 // FIXME: hack to get position cache warm
231 pi.pain.setDrawingEnabled(false);
233 pi.pain.setDrawingEnabled(true);
235 CursorSlice s1 = cur.selBegin();
236 CursorSlice s2 = cur.selEnd();
238 //lyxerr << "InsetMathNest::drawing selection: "
239 // << " s1: " << s1 << " s2: " << s2 << endl;
240 if (s1.idx() == s2.idx()) {
241 MathData const & c = cell(s1.idx());
242 int x1 = c.xo(bv) + c.pos2x(s1.pos());
243 int y1 = c.yo(bv) - c.ascent();
244 int x2 = c.xo(bv) + c.pos2x(s2.pos());
245 int y2 = c.yo(bv) + c.descent();
246 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
247 //lyxerr << "InsetMathNest::drawing selection 3: "
248 // << " x1: " << x1 << " x2: " << x2
249 // << " y1: " << y1 << " y2: " << y2 << endl;
251 for (idx_type i = 0; i < nargs(); ++i) {
252 if (idxBetween(i, s1.idx(), s2.idx())) {
253 MathData const & c = cell(i);
255 int y1 = c.yo(bv) - c.ascent();
256 int x2 = c.xo(bv) + c.width();
257 int y2 = c.yo(bv) + c.descent();
258 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
265 void InsetMathNest::validate(LaTeXFeatures & features) const
267 for (idx_type i = 0; i < nargs(); ++i)
268 cell(i).validate(features);
272 void InsetMathNest::replace(ReplaceData & rep)
274 for (idx_type i = 0; i < nargs(); ++i)
275 cell(i).replace(rep);
279 bool InsetMathNest::contains(MathData const & ar) const
281 for (idx_type i = 0; i < nargs(); ++i)
282 if (cell(i).contains(ar))
288 bool InsetMathNest::lock() const
294 void InsetMathNest::lock(bool l)
300 bool InsetMathNest::isActive() const
306 MathData InsetMathNest::glue() const
309 for (size_t i = 0; i < nargs(); ++i)
315 void InsetMathNest::write(WriteStream & os) const
317 os << '\\' << name().c_str();
318 for (size_t i = 0; i < nargs(); ++i)
319 os << '{' << cell(i) << '}';
321 os.pendingSpace(true);
322 if (lock_ && !os.latex()) {
324 os.pendingSpace(true);
329 void InsetMathNest::normalize(NormalStream & os) const
331 os << '[' << name().c_str();
332 for (size_t i = 0; i < nargs(); ++i)
333 os << ' ' << cell(i);
338 int InsetMathNest::latex(Buffer const &, odocstream & os,
339 OutputParams const & runparams) const
341 WriteStream wi(os, runparams.moving_arg, true);
347 bool InsetMathNest::notifyCursorLeaves(Cursor & /*cur*/)
353 MathData & ar = cur.cell();
354 // remove base-only "scripts"
355 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
356 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
357 if (p && p->nargs() == 1) {
358 MathData ar = p->nuc();
361 cur.adjust(i, ar.size() - 1);
365 // glue adjacent font insets of the same kind
366 for (pos_type i = 0; i + 1 < size(); ++i) {
367 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
368 InsetMathFont const * q = operator[](i + 1)->asFontInset();
369 if (p && q && p->name() == q->name()) {
370 p->cell(0).append(q->cell(0));
380 void InsetMathNest::handleFont
381 (Cursor & cur, docstring const & arg, char const * const font)
383 handleFont(cur, arg, from_ascii(font));
387 void InsetMathNest::handleFont
388 (Cursor & cur, docstring const & arg, docstring const & font)
390 // this whole function is a hack and won't work for incremental font
393 if (cur.inset().asInsetMath()->name() == font) {
394 recordUndoInset(cur, Undo::ATOMIC);
395 cur.handleFont(to_utf8(font));
397 recordUndo(cur, Undo::ATOMIC);
398 cur.handleNest(createInsetMath(font));
404 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
406 recordUndo(cur, Undo::ATOMIC);
409 bv_funcs::string2font(to_utf8(arg), font, b);
410 if (font.color() != Color::inherit) {
411 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
412 cur.handleNest(at, 0);
417 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
419 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
420 //CursorSlice sl = cur.current();
422 switch (cmd.action) {
426 cur.message(_("Paste"));
427 replaceSelection(cur);
429 if (cmd.argument().empty() && !theClipboard().isInternal())
430 topaste = theClipboard().getAsText();
433 idocstringstream is(cmd.argument());
435 topaste = cap::getSelection(cur.buffer(), n);
437 cur.niceInsert(topaste);
438 cur.clearSelection(); // bug 393
439 cur.bv().switchKeyMap();
446 cutSelection(cur, true, true);
447 cur.message(_("Cut"));
448 // Prevent stale position >= size crash
449 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
455 cur.message(_("Copy"));
458 case LFUN_MOUSE_PRESS:
459 lfunMousePress(cur, cmd);
462 case LFUN_MOUSE_MOTION:
463 lfunMouseMotion(cur, cmd);
466 case LFUN_MOUSE_RELEASE:
467 lfunMouseRelease(cur, cmd);
470 case LFUN_FINISHED_LEFT:
471 cur.bv().cursor() = cur;
474 case LFUN_FINISHED_RIGHT:
476 cur.bv().cursor() = cur;
479 case LFUN_FINISHED_UP:
480 cur.bv().cursor() = cur;
483 case LFUN_FINISHED_DOWN:
485 cur.bv().cursor() = cur;
488 case LFUN_CHAR_FORWARD:
489 cur.updateFlags(Update::Decoration | Update::FitCursor);
490 case LFUN_CHAR_FORWARD_SELECT:
491 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
492 cur.autocorrect() = false;
494 cur.macroModeClose();
495 if (reverseDirectionNeeded(cur))
496 goto goto_char_backwards;
499 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
500 cur.pushLeft(*cur.nextAtom().nucleus());
501 cur.inset().idxFirst(cur);
502 } else if (cur.posRight() || idxRight(cur)
503 || cur.popRight() || cur.selection())
506 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
511 case LFUN_CHAR_BACKWARD:
512 cur.updateFlags(Update::Decoration | Update::FitCursor);
513 case LFUN_CHAR_BACKWARD_SELECT:
514 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
515 cur.autocorrect() = false;
517 cur.macroModeClose();
518 if (reverseDirectionNeeded(cur))
519 goto goto_char_forwards;
522 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
524 cur.push(*cur.nextAtom().nucleus());
525 cur.inset().idxLast(cur);
526 } else if (cur.posLeft() || idxLeft(cur)
527 || cur.popLeft() || cur.selection())
530 cmd = FuncRequest(LFUN_FINISHED_LEFT);
536 cur.updateFlags(Update::Decoration | Update::FitCursor);
538 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
539 if (cur.inMacroMode()) {
541 cur.macroModeClose();
544 cur.selHandle(cmd.action == LFUN_UP_SELECT);
546 cmd = FuncRequest(LFUN_FINISHED_UP);
549 // fixes bug 1598. Please check!
554 cur.updateFlags(Update::Decoration | Update::FitCursor);
555 case LFUN_DOWN_SELECT:
556 if (cur.inMacroMode()) {
557 cur.macroModeClose();
560 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
562 cmd = FuncRequest(LFUN_FINISHED_DOWN);
565 // fixes bug 1598. Please check!
569 case LFUN_MOUSE_DOUBLE:
570 case LFUN_MOUSE_TRIPLE:
571 case LFUN_WORD_SELECT:
575 cur.selection() = true;
576 cur.pos() = cur.lastpos();
577 cur.idx() = cur.lastidx();
578 cap::saveSelection(cur);
581 case LFUN_PARAGRAPH_UP:
582 case LFUN_PARAGRAPH_DOWN:
583 cur.updateFlags(Update::Decoration | Update::FitCursor);
584 case LFUN_PARAGRAPH_UP_SELECT:
585 case LFUN_PARAGRAPH_DOWN_SELECT:
588 case LFUN_LINE_BEGIN:
589 case LFUN_WORD_BACKWARD:
590 cur.updateFlags(Update::Decoration | Update::FitCursor);
591 case LFUN_LINE_BEGIN_SELECT:
592 case LFUN_WORD_BACKWARD_SELECT:
593 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
594 cmd.action == LFUN_LINE_BEGIN_SELECT);
595 cur.macroModeClose();
596 if (cur.pos() != 0) {
598 } else if (cur.col() != 0) {
599 cur.idx() -= cur.col();
601 } else if (cur.idx() != 0) {
605 cmd = FuncRequest(LFUN_FINISHED_LEFT);
610 case LFUN_WORD_FORWARD:
612 cur.updateFlags(Update::Decoration | Update::FitCursor);
613 case LFUN_WORD_FORWARD_SELECT:
614 case LFUN_LINE_END_SELECT:
615 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
616 cmd.action == LFUN_LINE_END_SELECT);
617 cur.macroModeClose();
619 if (cur.pos() != cur.lastpos()) {
620 cur.pos() = cur.lastpos();
621 } else if (ncols() && (cur.col() != cur.lastcol())) {
622 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
623 cur.pos() = cur.lastpos();
624 } else if (cur.idx() != cur.lastidx()) {
625 cur.idx() = cur.lastidx();
626 cur.pos() = cur.lastpos();
628 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
633 case LFUN_SCREEN_UP_SELECT:
635 cmd = FuncRequest(LFUN_FINISHED_LEFT);
639 case LFUN_SCREEN_DOWN_SELECT:
640 case LFUN_SCREEN_DOWN:
641 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
645 case LFUN_CELL_FORWARD:
646 cur.updateFlags(Update::Decoration | Update::FitCursor);
647 cur.inset().idxNext(cur);
650 case LFUN_CELL_BACKWARD:
651 cur.updateFlags(Update::Decoration | Update::FitCursor);
652 cur.inset().idxPrev(cur);
655 case LFUN_WORD_DELETE_BACKWARD:
656 case LFUN_CHAR_DELETE_BACKWARD:
658 // May affect external cell:
659 recordUndoInset(cur, Undo::ATOMIC);
661 recordUndo(cur, Undo::ATOMIC);
662 // if the inset can not be removed from within, delete it
663 if (!cur.backspace()) {
664 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
665 cur.innerText()->dispatch(cur, cmd);
669 case LFUN_WORD_DELETE_FORWARD:
670 case LFUN_CHAR_DELETE_FORWARD:
671 if (cur.pos() == cur.lastpos())
672 // May affect external cell:
673 recordUndoInset(cur, Undo::ATOMIC);
675 recordUndo(cur, Undo::ATOMIC);
676 // if the inset can not be removed from within, delete it
678 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
679 cur.innerText()->dispatch(cur, cmd);
685 cur.clearSelection();
687 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
692 case LFUN_INSET_TOGGLE:
698 case LFUN_SELF_INSERT:
699 if (cmd.argument().size() != 1) {
701 docstring const arg = cmd.argument();
702 if (!interpretString(cur, arg))
706 // Don't record undo steps if we are in macro mode and
707 // cmd.argument is the next character of the macro name.
708 // Otherwise we'll get an invalid cursor if we undo after
709 // the macro was finished and the macro is a known command,
710 // e.g. sqrt. Cursor::macroModeClose replaces in this case
711 // the InsetMathUnknown with name "frac" by an empty
712 // InsetMathFrac -> a pos value > 0 is invalid.
713 // A side effect is that an undo before the macro is finished
714 // undoes the complete macro, not only the last character.
715 if (!cur.inMacroMode())
718 // spacial handling of space. If we insert an inset
719 // via macro mode, we want to put the cursor inside it
720 // if relevant. Think typing "\frac<space>".
721 if (cmd.argument()[0] == ' '
722 && cur.inMacroMode() && cur.macroName() != "\\"
723 && cur.macroModeClose()) {
724 MathAtom const atom = cur.prevAtom();
725 if (atom->asNestInset() && atom->nargs() > 0) {
727 cur.pushLeft(*cur.nextInset());
729 } else if (!interpretChar(cur, cmd.argument()[0])) {
730 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
735 //case LFUN_SERVER_GET_XY:
736 // sprintf(dispatch_buffer, "%d %d",);
739 case LFUN_SERVER_SET_XY: {
740 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
743 istringstream is(to_utf8(cmd.argument()));
745 cur.setScreenPos(x, y);
749 // Special casing for superscript in case of LyX handling
751 case LFUN_ACCENT_CIRCUMFLEX:
752 if (cmd.argument().empty()) {
753 // do superscript if LyX handles
755 recordUndo(cur, Undo::ATOMIC);
756 script(cur, true, grabAndEraseSelection(cur));
760 case LFUN_ACCENT_UMLAUT:
761 case LFUN_ACCENT_ACUTE:
762 case LFUN_ACCENT_GRAVE:
763 case LFUN_ACCENT_BREVE:
764 case LFUN_ACCENT_DOT:
765 case LFUN_ACCENT_MACRON:
766 case LFUN_ACCENT_CARON:
767 case LFUN_ACCENT_TILDE:
768 case LFUN_ACCENT_CEDILLA:
769 case LFUN_ACCENT_CIRCLE:
770 case LFUN_ACCENT_UNDERDOT:
771 case LFUN_ACCENT_TIE:
772 case LFUN_ACCENT_OGONEK:
773 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
777 case LFUN_FONT_FREE_APPLY:
778 case LFUN_FONT_FREE_UPDATE:
779 handleFont2(cur, cmd.argument());
783 if (currentMode() == TEXT_MODE)
784 handleFont(cur, cmd.argument(), "textbf");
786 handleFont(cur, cmd.argument(), "mathbf");
789 if (currentMode() == TEXT_MODE)
790 handleFont(cur, cmd.argument(), "textsf");
792 handleFont(cur, cmd.argument(), "mathsf");
795 if (currentMode() == TEXT_MODE)
796 handleFont(cur, cmd.argument(), "emph");
798 handleFont(cur, cmd.argument(), "mathcal");
800 case LFUN_FONT_ROMAN:
801 if (currentMode() == TEXT_MODE)
802 handleFont(cur, cmd.argument(), "textrm");
804 handleFont(cur, cmd.argument(), "mathrm");
807 if (currentMode() == TEXT_MODE)
808 handleFont(cur, cmd.argument(), "texttt");
810 handleFont(cur, cmd.argument(), "mathtt");
813 handleFont(cur, cmd.argument(), "mathfrak");
816 if (currentMode() == TEXT_MODE)
817 handleFont(cur, cmd.argument(), "textit");
819 handleFont(cur, cmd.argument(), "mathit");
822 if (currentMode() == TEXT_MODE)
823 // FIXME: should be "noun"
824 handleFont(cur, cmd.argument(), "textsc");
826 handleFont(cur, cmd.argument(), "mathbb");
829 case LFUN_FONT_FREE_APPLY:
830 handleFont(cur, cmd.argument(), "textrm");
833 case LFUN_FONT_DEFAULT:
834 handleFont(cur, cmd.argument(), "textnormal");
837 case LFUN_MATH_MODE: {
839 // ignore math-mode on when already in math mode
840 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
842 cur.macroModeClose();
843 docstring const save_selection = grabAndEraseSelection(cur);
845 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
846 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
848 cur.pushLeft(*cur.nextInset());
849 cur.niceInsert(save_selection);
851 if (currentMode() == Inset::TEXT_MODE) {
852 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
853 cur.message(_("create new math text environment ($...$)"));
855 handleFont(cur, cmd.argument(), "textrm");
856 cur.message(_("entered math text mode (textrm)"));
869 case LFUN_MATH_MATRIX: {
870 recordUndo(cur, Undo::ATOMIC);
875 idocstringstream is(cmd.argument());
876 is >> m >> n >> v_align >> h_align;
883 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
887 case LFUN_MATH_DELIM: {
889 docstring rs = support::split(cmd.argument(), ls, ' ');
890 // Reasonable default values
895 recordUndo(cur, Undo::ATOMIC);
896 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
900 case LFUN_MATH_BIGDELIM: {
901 docstring const lname = from_utf8(cmd.getArg(0));
902 docstring const ldelim = from_utf8(cmd.getArg(1));
903 docstring const rname = from_utf8(cmd.getArg(2));
904 docstring const rdelim = from_utf8(cmd.getArg(3));
905 latexkeys const * l = in_word_set(lname);
906 bool const have_l = l && l->inset == "big" &&
907 InsetMathBig::isBigInsetDelim(ldelim);
908 l = in_word_set(rname);
909 bool const have_r = l && l->inset == "big" &&
910 InsetMathBig::isBigInsetDelim(rdelim);
911 // We mimic LFUN_MATH_DELIM in case we have an empty left
912 // or right delimiter.
913 if (have_l || have_r) {
914 recordUndo(cur, Undo::ATOMIC);
915 docstring const selection = grabAndEraseSelection(cur);
918 cur.insert(MathAtom(new InsetMathBig(lname,
920 cur.niceInsert(selection);
922 cur.insert(MathAtom(new InsetMathBig(rname,
925 // Don't call cur.undispatched() if we did nothing, this would
926 // lead to infinite recursion via Text::dispatch().
930 case LFUN_SPACE_INSERT:
931 case LFUN_MATH_SPACE:
932 recordUndo(cur, Undo::ATOMIC);
933 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
936 case LFUN_ERT_INSERT:
937 // interpret this as if a backslash was typed
938 recordUndo(cur, Undo::ATOMIC);
939 interpretChar(cur, '\\');
942 case LFUN_MATH_SUBSCRIPT:
943 // interpret this as if a _ was typed
944 recordUndo(cur, Undo::ATOMIC);
945 interpretChar(cur, '_');
948 case LFUN_MATH_SUPERSCRIPT:
949 // interpret this as if a ^ was typed
950 recordUndo(cur, Undo::ATOMIC);
951 interpretChar(cur, '^');
954 case LFUN_QUOTE_INSERT:
955 // interpret this as if a straight " was typed
956 recordUndo(cur, Undo::ATOMIC);
957 interpretChar(cur, '\"');
960 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
961 // handling such that "self-insert" works on "arbitrary stuff" too, and
962 // math-insert only handles special math things like "matrix".
963 case LFUN_MATH_INSERT: {
964 recordUndo(cur, Undo::ATOMIC);
965 if (cmd.argument() == "^" || cmd.argument() == "_") {
966 interpretChar(cur, cmd.argument()[0]);
968 cur.niceInsert(cmd.argument());
972 case LFUN_DIALOG_SHOW_NEW_INSET: {
973 docstring const & name = cmd.argument();
976 InsetMathRef tmp(name);
977 data = tmp.createDialogStr(to_utf8(name));
979 cur.bv().showInsetDialog(to_utf8(name), data, 0);
983 case LFUN_INSET_INSERT: {
985 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
994 InsetMath::doDispatch(cur, cmd);
1000 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1001 FuncStatus & flag) const
1003 // the font related toggles
1004 //string tc = "mathnormal";
1006 string const arg = to_utf8(cmd.argument());
1007 switch (cmd.action) {
1008 case LFUN_TABULAR_FEATURE:
1009 flag.enabled(false);
1012 case LFUN_TABULAR_FEATURE:
1013 // FIXME: check temporarily disabled
1015 char align = mathcursor::valign();
1016 if (align == '\0') {
1020 if (cmd.argument().empty()) {
1024 if (!contains("tcb", cmd.argument()[0])) {
1028 flag.setOnOff(cmd.argument()[0] == align);
1031 /// We have to handle them since 1.4 blocks all unhandled actions
1032 case LFUN_FONT_ITAL:
1033 case LFUN_FONT_BOLD:
1034 case LFUN_FONT_SANS:
1035 case LFUN_FONT_EMPH:
1036 case LFUN_FONT_CODE:
1037 case LFUN_FONT_NOUN:
1038 case LFUN_FONT_ROMAN:
1039 case LFUN_FONT_DEFAULT:
1042 case LFUN_MATH_MUTATE:
1043 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1044 flag.setOnOff(false);
1047 // we just need to be in math mode to enable that
1048 case LFUN_MATH_SIZE:
1049 case LFUN_MATH_SPACE:
1050 case LFUN_MATH_LIMITS:
1051 case LFUN_MATH_NONUMBER:
1052 case LFUN_MATH_NUMBER:
1053 case LFUN_MATH_EXTERN:
1057 case LFUN_FONT_FRAK:
1058 flag.enabled(currentMode() != TEXT_MODE);
1061 case LFUN_MATH_INSERT: {
1062 bool const textarg =
1063 arg == "\\textbf" || arg == "\\textsf" ||
1064 arg == "\\textrm" || arg == "\\textmd" ||
1065 arg == "\\textit" || arg == "\\textsc" ||
1066 arg == "\\textsl" || arg == "\\textup" ||
1067 arg == "\\texttt" || arg == "\\textbb" ||
1068 arg == "\\textnormal";
1069 flag.enabled(currentMode() != TEXT_MODE || textarg);
1073 case LFUN_MATH_MATRIX:
1074 flag.enabled(currentMode() == MATH_MODE);
1077 case LFUN_INSET_INSERT: {
1078 // Don't test createMathInset_fromDialogStr(), since
1079 // getStatus is not called with a valid reference and the
1080 // dialog would not be applyable.
1081 string const name = cmd.getArg(0);
1082 flag.enabled(name == "ref");
1086 case LFUN_MATH_DELIM:
1087 case LFUN_MATH_BIGDELIM:
1088 // Don't do this with multi-cell selections
1089 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1100 void InsetMathNest::edit(Cursor & cur, bool left)
1103 cur.idx() = left ? 0 : cur.lastidx();
1104 cur.pos() = left ? 0 : cur.lastpos();
1106 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1110 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1113 int dist_min = 1000000;
1114 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1115 int const d = cell(i).dist(cur.bv(), x, y);
1121 MathData & ar = cell(idx_min);
1123 cur.idx() = idx_min;
1124 cur.pos() = ar.x2pos(x - ar.xo(cur.bv()));
1126 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1127 if (dist_min == 0) {
1129 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1130 if (ar[i]->covers(cur.bv(), x, y))
1131 return ar[i].nucleus()->editXY(cur, x, y);
1137 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1139 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1140 BufferView & bv = cur.bv();
1141 if (cmd.button() == mouse_button::button1) {
1142 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1143 bv.mouseSetCursor(cur);
1144 // Update the cursor update flags as needed:
1146 // Update::Decoration: tells to update the decoration (visual box
1147 // corners that define the inset)/
1148 // Update::FitCursor: adjust the screen to the cursor position if
1150 // cur.result().update(): don't overwrite previously set flags.
1151 cur.updateFlags(Update::Decoration | Update::FitCursor | cur.result().update());
1152 } else if (cmd.button() == mouse_button::button2) {
1154 if (cap::selection()) {
1155 // See comment in Text::dispatch why we do this
1156 cap::copySelectionToStack();
1157 cmd = FuncRequest(LFUN_PASTE, "0");
1158 doDispatch(cur, cmd);
1160 asArray(theSelection().get(), ar);
1163 bv.mouseSetCursor(cur);
1168 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1170 // only select with button 1
1171 if (cmd.button() == mouse_button::button1) {
1172 Cursor & bvcur = cur.bv().cursor();
1173 if (bvcur.anchor_.hasPart(cur)) {
1174 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1175 bvcur.setCursor(cur);
1176 bvcur.selection() = true;
1177 //lyxerr << "MOTION " << bvcur << endl;
1184 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1186 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1188 if (cmd.button() == mouse_button::button1) {
1189 if (!cur.selection())
1192 Cursor & bvcur = cur.bv().cursor();
1193 bvcur.selection() = true;
1194 cap::saveSelection(bvcur);
1203 bool InsetMathNest::interpretChar(Cursor & cur, char_type c)
1205 //lyxerr << "interpret 2: '" << c << "'" << endl;
1206 docstring save_selection;
1207 if (c == '^' || c == '_')
1208 save_selection = grabAndEraseSelection(cur);
1213 if (cur.inMacroMode()) {
1214 docstring name = cur.macroName();
1216 /// are we currently typing '#1' or '#2' or...?
1217 if (name == "\\#") {
1220 if (n >= 1 && n <= 9)
1221 cur.insert(new MathMacroArgument(n));
1225 if (isAlphaASCII(c)) {
1226 cur.activeMacro()->setName(name + docstring(1, c));
1230 // handle 'special char' macros
1235 if (currentMode() == InsetMath::TEXT_MODE)
1236 cur.niceInsert(createInsetMath("textbackslash"));
1238 cur.niceInsert(createInsetMath("backslash"));
1239 } else if (c == '{') {
1241 cur.niceInsert(MathAtom(new InsetMathBrace));
1242 } else if (c == '%') {
1244 cur.niceInsert(MathAtom(new InsetMathComment));
1245 } else if (c == '#') {
1246 BOOST_ASSERT(cur.activeMacro());
1247 cur.activeMacro()->setName(name + docstring(1, c));
1250 cur.niceInsert(createInsetMath(docstring(1, c)));
1255 // One character big delimiters. The others are handled in
1256 // interpretString().
1257 latexkeys const * l = in_word_set(name.substr(1));
1258 if (name[0] == '\\' && l && l->inset == "big") {
1262 delim = from_ascii("\\{");
1265 delim = from_ascii("\\}");
1268 delim = docstring(1, c);
1271 if (InsetMathBig::isBigInsetDelim(delim)) {
1272 // name + delim ared a valid InsetMathBig.
1273 // We can't use cur.macroModeClose() because
1274 // it does not handle delim.
1275 InsetMathUnknown * p = cur.activeMacro();
1278 cur.cell().erase(cur.pos());
1279 cur.plainInsert(MathAtom(
1280 new InsetMathBig(name.substr(1), delim)));
1285 // leave macro mode and try again if necessary
1286 cur.macroModeClose();
1288 cur.niceInsert(MathAtom(new InsetMathBrace));
1290 interpretChar(cur, c);
1294 // This is annoying as one has to press <space> far too often.
1298 // leave autocorrect mode if necessary
1299 if (autocorrect() && c == ' ') {
1300 autocorrect() = false;
1305 // just clear selection on pressing the space bar
1306 if (cur.selection() && c == ' ') {
1307 cur.selection() = false;
1314 //lyxerr << "starting with macro" << endl;
1315 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), false)));
1320 if (currentMode() == InsetMath::TEXT_MODE)
1326 if (currentMode() == InsetMath::TEXT_MODE) {
1327 // insert spaces in text mode,
1328 // but suppress direct insertion of two spaces in a row
1329 // the still allows typing '<space>a<space>' and deleting the 'a', but
1330 // it is better than nothing...
1331 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1333 // FIXME: we have to enable full redraw here because of the
1334 // visual box corners that define the inset. If we know for
1335 // sure that we stay within the same cell we can optimize for
1337 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1341 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1342 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1343 // FIXME: we have to enable full redraw here because of the
1344 // visual box corners that define the inset. If we know for
1345 // sure that we stay within the same cell we can optimize for
1347 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1351 if (cur.popRight()) {
1352 // FIXME: we have to enable full redraw here because of the
1353 // visual box corners that define the inset. If we know for
1354 // sure that we stay within the same cell we can optimize for
1356 //cur.updateFlags(Update::FitCursor);
1360 // if we are at the very end, leave the formula
1361 return cur.pos() != cur.lastpos();
1364 // These shouldn't work in text mode:
1365 if (currentMode() != InsetMath::TEXT_MODE) {
1367 script(cur, false, save_selection);
1371 script(cur, true, save_selection);
1375 cur.niceInsert(createInsetMath("sim"));
1380 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1381 c == '%' || c == '_' || c == '^') {
1382 cur.niceInsert(createInsetMath(docstring(1, c)));
1387 // try auto-correction
1388 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1391 // no special circumstances, so insert the character without any fuss
1393 cur.autocorrect() = true;
1398 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1400 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1402 if (!cur.empty() && cur.pos() > 0 &&
1403 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1404 if (InsetMathBig::isBigInsetDelim(str)) {
1405 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1406 if (prev[0] == '\\') {
1407 prev = prev.substr(1);
1408 latexkeys const * l = in_word_set(prev);
1409 if (l && l->inset == "big") {
1410 cur.cell()[cur.pos() - 1] =
1411 MathAtom(new InsetMathBig(prev, str));
1421 bool InsetMathNest::script(Cursor & cur, bool up,
1422 docstring const & save_selection)
1424 // Hack to get \^ and \_ working
1425 //lyxerr << "handling script: up: " << up << endl;
1426 if (cur.inMacroMode() && cur.macroName() == "\\") {
1428 cur.niceInsert(createInsetMath("mathcircumflex"));
1430 interpretChar(cur, '_');
1434 cur.macroModeClose();
1435 if (asScriptInset() && cur.idx() == 0) {
1436 // we are in a nucleus of a script inset, move to _our_ script
1437 InsetMathScript * inset = asScriptInset();
1438 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1440 cur.idx() = inset->idxOfScript(up);
1442 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1444 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1447 cur.idx() = inset->idxOfScript(up);
1448 cur.pos() = cur.lastpos();
1450 // convert the thing to our left to a scriptinset or create a new
1451 // one if in the very first position of the array
1452 if (cur.pos() == 0) {
1453 //lyxerr << "new scriptinset" << endl;
1454 cur.insert(new InsetMathScript(up));
1456 //lyxerr << "converting prev atom " << endl;
1457 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1460 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1461 // See comment in MathParser.cpp for special handling of {}-bases
1467 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1468 cur.niceInsert(save_selection);
1470 //lyxerr << "inserting selection 2:\n" << save_selection << endl;