2 * \file InsetMathNest.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetMathNest.h"
15 #include "InsetMathArray.h"
16 #include "InsetMathBig.h"
17 #include "InsetMathBox.h"
18 #include "InsetMathBrace.h"
19 #include "InsetMathColor.h"
20 #include "InsetMathComment.h"
21 #include "InsetMathDelim.h"
22 #include "InsetMathHull.h"
23 //#include "InsetMathMBox.h"
24 #include "InsetMathRef.h"
25 #include "InsetMathScript.h"
26 #include "InsetMathSpace.h"
27 #include "InsetMathSymbol.h"
28 #include "InsetMathUnknown.h"
30 #include "MathFactory.h"
31 #include "MathMacroArgument.h"
32 #include "MathParser.h"
33 #include "MathStream.h"
34 #include "MathSupport.h"
37 #include "BufferView.h"
39 #include "CoordCache.h"
41 #include "CutAndPaste.h"
43 #include "DispatchResult.h"
44 #include "FuncRequest.h"
45 #include "FuncStatus.h"
48 #include "OutputParams.h"
50 #include "support/lstrings.h"
51 #include "support/textutils.h"
53 #include "frontends/Clipboard.h"
54 #include "frontends/Painter.h"
55 #include "frontends/Selection.h"
57 #include "FuncRequest.h"
64 using cap::copySelection;
65 using cap::grabAndEraseSelection;
66 using cap::cutSelection;
67 using cap::replaceSelection;
68 using cap::selClearOrDel;
72 using std::istringstream;
75 InsetMathNest::InsetMathNest(idx_type nargs)
76 : cells_(nargs), lock_(false), mouse_hover_(false)
80 InsetMathNest::InsetMathNest(InsetMathNest const & inset)
81 : InsetMath(inset), cells_(inset.cells_), lock_(inset.lock_),
86 InsetMathNest & InsetMathNest::operator=(InsetMathNest const & inset)
88 cells_ = inset.cells_;
91 InsetMath::operator=(inset);
96 InsetMath::idx_type InsetMathNest::nargs() const
102 void InsetMathNest::cursorPos(BufferView const & bv,
103 CursorSlice const & sl, bool /*boundary*/,
104 int & x, int & y) const
106 // FIXME: This is a hack. Ideally, the coord cache should not store
107 // absolute positions, but relative ones. This would mean to call
108 // setXY() not in MathData::draw(), but in the parent insets' draw()
109 // with the correctly adjusted x,y values. But this means that we'd have
110 // to touch all (math)inset's draw() methods. Right now, we'll store
111 // absolute value, and make them here relative, only to make them
112 // absolute again when actually drawing the cursor. What a mess.
113 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
114 MathData const & ar = sl.cell();
115 CoordCache const & coord_cache = bv.coordCache();
116 if (!coord_cache.getArrays().has(&ar)) {
117 // this can (semi-)legally happen if we just created this cell
118 // and it never has been drawn before. So don't ASSERT.
119 //lyxerr << "no cached data for array " << &ar << endl;
124 Point const pt = coord_cache.getArrays().xy(&ar);
125 if (!coord_cache.getInsets().has(this)) {
127 //lyxerr << "no cached data for inset " << this << endl;
132 Point const pt2 = coord_cache.getInsets().xy(this);
133 //lyxerr << "retrieving position cache for MathData "
134 // << pt.x_ << ' ' << pt.y_ << std::endl;
135 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
137 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
138 // << " asc: " << ascent() << " des: " << descent()
139 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
140 // move cursor visually into empty cells ("blue rectangles");
146 void InsetMathNest::metrics(MetricsInfo const & mi) const
149 for (idx_type i = 0, n = nargs(); i != n; ++i) {
151 cell(i).metrics(m, dim);
156 bool InsetMathNest::idxNext(Cursor & cur) const
158 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
159 if (cur.idx() == cur.lastidx())
167 bool InsetMathNest::idxRight(Cursor & cur) const
173 bool InsetMathNest::idxPrev(Cursor & cur) const
175 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
179 cur.pos() = cur.lastpos();
184 bool InsetMathNest::idxLeft(Cursor & cur) const
190 bool InsetMathNest::idxFirst(Cursor & cur) const
192 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
201 bool InsetMathNest::idxLast(Cursor & cur) const
203 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
206 cur.idx() = cur.lastidx();
207 cur.pos() = cur.lastpos();
212 void InsetMathNest::dump() const
214 odocstringstream oss;
216 os << "---------------------------------------------\n";
219 for (idx_type i = 0, n = nargs(); i != n; ++i)
220 os << cell(i) << "\n";
221 os << "---------------------------------------------\n";
222 lyxerr << to_utf8(oss.str());
226 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
230 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
233 setPosCache(pi, x, y);
237 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
239 BufferView & bv = *pi.base.bv;
240 // this should use the x/y values given, not the cached values
241 Cursor & cur = bv.cursor();
242 if (!cur.selection())
244 if (!ptr_cmp(&cur.inset(), this))
247 // FIXME: hack to get position cache warm
248 pi.pain.setDrawingEnabled(false);
250 pi.pain.setDrawingEnabled(true);
252 CursorSlice s1 = cur.selBegin();
253 CursorSlice s2 = cur.selEnd();
255 //lyxerr << "InsetMathNest::drawing selection: "
256 // << " s1: " << s1 << " s2: " << s2 << endl;
257 if (s1.idx() == s2.idx()) {
258 MathData const & c = cell(s1.idx());
259 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
260 int x1 = g.pos.x_ + c.pos2x(s1.pos());
261 int y1 = g.pos.y_ - g.dim.ascent();
262 int x2 = g.pos.x_ + c.pos2x(s2.pos());
263 int y2 = g.pos.y_ + g.dim.descent();
264 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
265 //lyxerr << "InsetMathNest::drawing selection 3: "
266 // << " x1: " << x1 << " x2: " << x2
267 // << " y1: " << y1 << " y2: " << y2 << endl;
269 for (idx_type i = 0; i < nargs(); ++i) {
270 if (idxBetween(i, s1.idx(), s2.idx())) {
271 MathData const & c = cell(i);
272 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
274 int y1 = g.pos.y_ - g.dim.ascent();
275 int x2 = g.pos.x_ + g.dim.width();
276 int y2 = g.pos.y_ + g.dim.descent();
277 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
284 void InsetMathNest::validate(LaTeXFeatures & features) const
286 for (idx_type i = 0; i < nargs(); ++i)
287 cell(i).validate(features);
291 void InsetMathNest::replace(ReplaceData & rep)
293 for (idx_type i = 0; i < nargs(); ++i)
294 cell(i).replace(rep);
298 bool InsetMathNest::contains(MathData const & ar) const
300 for (idx_type i = 0; i < nargs(); ++i)
301 if (cell(i).contains(ar))
307 bool InsetMathNest::lock() const
313 void InsetMathNest::lock(bool l)
319 bool InsetMathNest::isActive() const
325 MathData InsetMathNest::glue() const
328 for (size_t i = 0; i < nargs(); ++i)
334 void InsetMathNest::write(WriteStream & os) const
336 os << '\\' << name().c_str();
337 for (size_t i = 0; i < nargs(); ++i)
338 os << '{' << cell(i) << '}';
340 os.pendingSpace(true);
341 if (lock_ && !os.latex()) {
343 os.pendingSpace(true);
348 void InsetMathNest::normalize(NormalStream & os) const
350 os << '[' << name().c_str();
351 for (size_t i = 0; i < nargs(); ++i)
352 os << ' ' << cell(i);
357 int InsetMathNest::latex(Buffer const &, odocstream & os,
358 OutputParams const & runparams) const
360 WriteStream wi(os, runparams.moving_arg, true);
366 bool InsetMathNest::setMouseHover(bool mouse_hover)
368 mouse_hover_ = mouse_hover;
373 bool InsetMathNest::notifyCursorLeaves(Cursor & /*cur*/)
377 MathData & ar = cur.cell();
378 // remove base-only "scripts"
379 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
380 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
381 if (p && p->nargs() == 1) {
382 MathData ar = p->nuc();
385 cur.adjust(i, ar.size() - 1);
389 // glue adjacent font insets of the same kind
390 for (pos_type i = 0; i + 1 < size(); ++i) {
391 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
392 InsetMathFont const * q = operator[](i + 1)->asFontInset();
393 if (p && q && p->name() == q->name()) {
394 p->cell(0).append(q->cell(0));
404 void InsetMathNest::handleFont
405 (Cursor & cur, docstring const & arg, char const * const font)
407 handleFont(cur, arg, from_ascii(font));
411 void InsetMathNest::handleFont
412 (Cursor & cur, docstring const & arg, docstring const & font)
414 // this whole function is a hack and won't work for incremental font
417 if (cur.inset().asInsetMath()->name() == font) {
418 cur.recordUndoInset();
419 cur.handleFont(to_utf8(font));
422 cur.handleNest(createInsetMath(font));
428 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
433 font.fromString(to_utf8(arg), b);
434 if (font.color() != Color::inherit) {
435 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
436 cur.handleNest(at, 0);
441 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
443 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
444 //CursorSlice sl = cur.current();
446 switch (cmd.action) {
450 cur.message(_("Paste"));
451 replaceSelection(cur);
453 if (cmd.argument().empty() && !theClipboard().isInternal())
454 topaste = theClipboard().getAsText();
457 idocstringstream is(cmd.argument());
459 topaste = cap::getSelection(cur.buffer(), n);
461 cur.niceInsert(topaste);
462 cur.clearSelection(); // bug 393
469 cutSelection(cur, true, true);
470 cur.message(_("Cut"));
471 // Prevent stale position >= size crash
472 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
478 cur.message(_("Copy"));
481 case LFUN_MOUSE_PRESS:
482 lfunMousePress(cur, cmd);
485 case LFUN_MOUSE_MOTION:
486 lfunMouseMotion(cur, cmd);
489 case LFUN_MOUSE_RELEASE:
490 lfunMouseRelease(cur, cmd);
493 case LFUN_FINISHED_LEFT:
494 cur.bv().cursor() = cur;
497 case LFUN_FINISHED_RIGHT:
499 cur.bv().cursor() = cur;
502 case LFUN_CHAR_FORWARD:
503 cur.updateFlags(Update::Decoration | Update::FitCursor);
504 case LFUN_CHAR_FORWARD_SELECT:
505 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
506 cur.autocorrect() = false;
508 cur.macroModeClose();
509 if (reverseDirectionNeeded(cur))
510 goto goto_char_backwards;
513 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
514 cur.pushLeft(*cur.nextAtom().nucleus());
515 cur.inset().idxFirst(cur);
516 } else if (cur.posRight() || idxRight(cur)
517 || cur.popRight() || cur.selection())
520 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
525 case LFUN_CHAR_BACKWARD:
526 cur.updateFlags(Update::Decoration | Update::FitCursor);
527 case LFUN_CHAR_BACKWARD_SELECT:
528 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
529 cur.autocorrect() = false;
531 cur.macroModeClose();
532 if (reverseDirectionNeeded(cur))
533 goto goto_char_forwards;
536 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
538 cur.push(*cur.nextAtom().nucleus());
539 cur.inset().idxLast(cur);
540 } else if (cur.posLeft() || idxLeft(cur)
541 || cur.popLeft() || cur.selection())
544 cmd = FuncRequest(LFUN_FINISHED_LEFT);
551 cur.updateFlags(Update::Decoration | Update::FitCursor);
552 case LFUN_DOWN_SELECT:
553 case LFUN_UP_SELECT: {
554 // close active macro
555 if (cur.inMacroMode()) {
556 cur.macroModeClose();
560 // stop/start the selection
561 bool select = cmd.action == LFUN_DOWN_SELECT ||
562 cmd.action == LFUN_UP_SELECT;
563 cur.selHandle(select);
566 bool up = cmd.action == LFUN_UP || cmd.action == LFUN_UP_SELECT;
567 bool successful = cur.upDownInMath(up);
569 // notify left insets and give them chance to set update flags
570 lyx::notifyCursorLeaves(cur.beforeDispatchCursor(), cur);
575 if (cur.fixIfBroken())
576 // FIXME: Something bad happened. We pass the corrected Cursor
577 // instead of letting things go worse.
580 // We did not manage to move the cursor.
585 case LFUN_MOUSE_DOUBLE:
586 case LFUN_MOUSE_TRIPLE:
587 case LFUN_WORD_SELECT:
591 cur.selection() = true;
592 cur.pos() = cur.lastpos();
593 cur.idx() = cur.lastidx();
596 case LFUN_PARAGRAPH_UP:
597 case LFUN_PARAGRAPH_DOWN:
598 cur.updateFlags(Update::Decoration | Update::FitCursor);
599 case LFUN_PARAGRAPH_UP_SELECT:
600 case LFUN_PARAGRAPH_DOWN_SELECT:
603 case LFUN_LINE_BEGIN:
604 case LFUN_WORD_BACKWARD:
605 cur.updateFlags(Update::Decoration | Update::FitCursor);
606 case LFUN_LINE_BEGIN_SELECT:
607 case LFUN_WORD_BACKWARD_SELECT:
608 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
609 cmd.action == LFUN_LINE_BEGIN_SELECT);
610 cur.macroModeClose();
611 if (cur.pos() != 0) {
613 } else if (cur.col() != 0) {
614 cur.idx() -= cur.col();
616 } else if (cur.idx() != 0) {
620 cmd = FuncRequest(LFUN_FINISHED_LEFT);
625 case LFUN_WORD_FORWARD:
627 cur.updateFlags(Update::Decoration | Update::FitCursor);
628 case LFUN_WORD_FORWARD_SELECT:
629 case LFUN_LINE_END_SELECT:
630 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
631 cmd.action == LFUN_LINE_END_SELECT);
632 cur.macroModeClose();
634 if (cur.pos() != cur.lastpos()) {
635 cur.pos() = cur.lastpos();
636 } else if (ncols() && (cur.col() != cur.lastcol())) {
637 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
638 cur.pos() = cur.lastpos();
639 } else if (cur.idx() != cur.lastidx()) {
640 cur.idx() = cur.lastidx();
641 cur.pos() = cur.lastpos();
643 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
648 case LFUN_SCREEN_UP_SELECT:
649 cmd = FuncRequest(LFUN_FINISHED_LEFT);
653 case LFUN_SCREEN_DOWN_SELECT:
654 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
658 case LFUN_CELL_FORWARD:
659 cur.updateFlags(Update::Decoration | Update::FitCursor);
660 cur.inset().idxNext(cur);
663 case LFUN_CELL_BACKWARD:
664 cur.updateFlags(Update::Decoration | Update::FitCursor);
665 cur.inset().idxPrev(cur);
668 case LFUN_WORD_DELETE_BACKWARD:
669 case LFUN_CHAR_DELETE_BACKWARD:
671 // May affect external cell:
672 cur.recordUndoInset();
675 // if the inset can not be removed from within, delete it
676 if (!cur.backspace()) {
677 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
678 cur.innerText()->dispatch(cur, cmd);
682 case LFUN_WORD_DELETE_FORWARD:
683 case LFUN_CHAR_DELETE_FORWARD:
684 if (cur.pos() == cur.lastpos())
685 // May affect external cell:
686 cur.recordUndoInset();
689 // if the inset can not be removed from within, delete it
691 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
692 cur.innerText()->dispatch(cur, cmd);
698 cur.clearSelection();
700 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
705 case LFUN_INSET_TOGGLE:
711 case LFUN_SELF_INSERT:
712 if (cmd.argument().size() != 1) {
714 docstring const arg = cmd.argument();
715 if (!interpretString(cur, arg))
719 // Don't record undo steps if we are in macro mode and
720 // cmd.argument is the next character of the macro name.
721 // Otherwise we'll get an invalid cursor if we undo after
722 // the macro was finished and the macro is a known command,
723 // e.g. sqrt. Cursor::macroModeClose replaces in this case
724 // the InsetMathUnknown with name "frac" by an empty
725 // InsetMathFrac -> a pos value > 0 is invalid.
726 // A side effect is that an undo before the macro is finished
727 // undoes the complete macro, not only the last character.
728 if (!cur.inMacroMode())
731 // spacial handling of space. If we insert an inset
732 // via macro mode, we want to put the cursor inside it
733 // if relevant. Think typing "\frac<space>".
734 if (cmd.argument()[0] == ' '
735 && cur.inMacroMode() && cur.macroName() != "\\"
736 && cur.macroModeClose()) {
737 MathAtom const atom = cur.prevAtom();
738 if (atom->asNestInset() && atom->isActive()) {
740 cur.pushLeft(*cur.nextInset());
742 } else if (!interpretChar(cur, cmd.argument()[0])) {
743 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
748 //case LFUN_SERVER_GET_XY:
749 // sprintf(dispatch_buffer, "%d %d",);
752 case LFUN_SERVER_SET_XY: {
753 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
756 istringstream is(to_utf8(cmd.argument()));
758 cur.setScreenPos(x, y);
762 // Special casing for superscript in case of LyX handling
764 case LFUN_ACCENT_CIRCUMFLEX:
765 if (cmd.argument().empty()) {
766 // do superscript if LyX handles
769 script(cur, true, grabAndEraseSelection(cur));
773 case LFUN_ACCENT_UMLAUT:
774 case LFUN_ACCENT_ACUTE:
775 case LFUN_ACCENT_GRAVE:
776 case LFUN_ACCENT_BREVE:
777 case LFUN_ACCENT_DOT:
778 case LFUN_ACCENT_MACRON:
779 case LFUN_ACCENT_CARON:
780 case LFUN_ACCENT_TILDE:
781 case LFUN_ACCENT_CEDILLA:
782 case LFUN_ACCENT_CIRCLE:
783 case LFUN_ACCENT_UNDERDOT:
784 case LFUN_ACCENT_TIE:
785 case LFUN_ACCENT_OGONEK:
786 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
790 case LFUN_FONT_FREE_APPLY:
791 case LFUN_FONT_FREE_UPDATE:
792 handleFont2(cur, cmd.argument());
796 if (currentMode() == TEXT_MODE)
797 handleFont(cur, cmd.argument(), "textbf");
799 handleFont(cur, cmd.argument(), "mathbf");
802 if (currentMode() == TEXT_MODE)
803 handleFont(cur, cmd.argument(), "textsf");
805 handleFont(cur, cmd.argument(), "mathsf");
808 if (currentMode() == TEXT_MODE)
809 handleFont(cur, cmd.argument(), "emph");
811 handleFont(cur, cmd.argument(), "mathcal");
813 case LFUN_FONT_ROMAN:
814 if (currentMode() == TEXT_MODE)
815 handleFont(cur, cmd.argument(), "textrm");
817 handleFont(cur, cmd.argument(), "mathrm");
819 case LFUN_FONT_TYPEWRITER:
820 if (currentMode() == TEXT_MODE)
821 handleFont(cur, cmd.argument(), "texttt");
823 handleFont(cur, cmd.argument(), "mathtt");
826 handleFont(cur, cmd.argument(), "mathfrak");
829 if (currentMode() == TEXT_MODE)
830 handleFont(cur, cmd.argument(), "textit");
832 handleFont(cur, cmd.argument(), "mathit");
835 if (currentMode() == TEXT_MODE)
836 // FIXME: should be "noun"
837 handleFont(cur, cmd.argument(), "textsc");
839 handleFont(cur, cmd.argument(), "mathbb");
842 case LFUN_FONT_FREE_APPLY:
843 handleFont(cur, cmd.argument(), "textrm");
846 case LFUN_FONT_DEFAULT:
847 handleFont(cur, cmd.argument(), "textnormal");
850 case LFUN_MATH_MODE: {
852 // ignore math-mode on when already in math mode
853 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
855 cur.macroModeClose();
856 docstring const save_selection = grabAndEraseSelection(cur);
858 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
859 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
861 cur.pushLeft(*cur.nextInset());
862 cur.niceInsert(save_selection);
864 if (currentMode() == Inset::TEXT_MODE) {
865 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
866 cur.message(_("create new math text environment ($...$)"));
868 handleFont(cur, cmd.argument(), "textrm");
869 cur.message(_("entered math text mode (textrm)"));
882 case LFUN_MATH_MATRIX: {
888 idocstringstream is(cmd.argument());
889 is >> m >> n >> v_align >> h_align;
896 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
900 case LFUN_MATH_DELIM: {
902 docstring rs = support::split(cmd.argument(), ls, ' ');
903 // Reasonable default values
909 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
913 case LFUN_MATH_BIGDELIM: {
914 docstring const lname = from_utf8(cmd.getArg(0));
915 docstring const ldelim = from_utf8(cmd.getArg(1));
916 docstring const rname = from_utf8(cmd.getArg(2));
917 docstring const rdelim = from_utf8(cmd.getArg(3));
918 latexkeys const * l = in_word_set(lname);
919 bool const have_l = l && l->inset == "big" &&
920 InsetMathBig::isBigInsetDelim(ldelim);
921 l = in_word_set(rname);
922 bool const have_r = l && l->inset == "big" &&
923 InsetMathBig::isBigInsetDelim(rdelim);
924 // We mimic LFUN_MATH_DELIM in case we have an empty left
925 // or right delimiter.
926 if (have_l || have_r) {
928 docstring const selection = grabAndEraseSelection(cur);
931 cur.insert(MathAtom(new InsetMathBig(lname,
933 cur.niceInsert(selection);
935 cur.insert(MathAtom(new InsetMathBig(rname,
938 // Don't call cur.undispatched() if we did nothing, this would
939 // lead to infinite recursion via Text::dispatch().
943 case LFUN_SPACE_INSERT:
944 case LFUN_MATH_SPACE:
946 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
949 case LFUN_ERT_INSERT:
950 // interpret this as if a backslash was typed
952 interpretChar(cur, '\\');
955 case LFUN_MATH_SUBSCRIPT:
956 // interpret this as if a _ was typed
958 interpretChar(cur, '_');
961 case LFUN_MATH_SUPERSCRIPT:
962 // interpret this as if a ^ was typed
964 interpretChar(cur, '^');
967 case LFUN_QUOTE_INSERT:
968 // interpret this as if a straight " was typed
970 interpretChar(cur, '\"');
973 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
974 // handling such that "self-insert" works on "arbitrary stuff" too, and
975 // math-insert only handles special math things like "matrix".
976 case LFUN_MATH_INSERT: {
978 if (cmd.argument() == "^" || cmd.argument() == "_") {
979 interpretChar(cur, cmd.argument()[0]);
981 cur.niceInsert(cmd.argument());
985 case LFUN_DIALOG_SHOW_NEW_INSET: {
986 docstring const & name = cmd.argument();
989 InsetMathRef tmp(name);
990 data = tmp.createDialogStr(to_utf8(name));
992 cur.bv().showInsetDialog(to_utf8(name), data, 0);
996 case LFUN_INSET_INSERT: {
998 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1005 case LFUN_INSET_DISSOLVE:
1006 if (!asHullInset()) {
1007 cur.recordUndoInset();
1013 InsetMath::doDispatch(cur, cmd);
1019 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1020 FuncStatus & flag) const
1022 // the font related toggles
1023 //string tc = "mathnormal";
1025 string const arg = to_utf8(cmd.argument());
1026 switch (cmd.action) {
1027 case LFUN_TABULAR_FEATURE:
1028 flag.enabled(false);
1031 case LFUN_TABULAR_FEATURE:
1032 // FIXME: check temporarily disabled
1034 char align = mathcursor::valign();
1035 if (align == '\0') {
1039 if (cmd.argument().empty()) {
1043 if (!contains("tcb", cmd.argument()[0])) {
1047 flag.setOnOff(cmd.argument()[0] == align);
1050 /// We have to handle them since 1.4 blocks all unhandled actions
1051 case LFUN_FONT_ITAL:
1052 case LFUN_FONT_BOLD:
1053 case LFUN_FONT_SANS:
1054 case LFUN_FONT_EMPH:
1055 case LFUN_FONT_TYPEWRITER:
1056 case LFUN_FONT_NOUN:
1057 case LFUN_FONT_ROMAN:
1058 case LFUN_FONT_DEFAULT:
1061 case LFUN_MATH_MUTATE:
1062 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1063 flag.setOnOff(false);
1066 // we just need to be in math mode to enable that
1067 case LFUN_MATH_SIZE:
1068 case LFUN_MATH_SPACE:
1069 case LFUN_MATH_LIMITS:
1070 case LFUN_MATH_NONUMBER:
1071 case LFUN_MATH_NUMBER:
1072 case LFUN_MATH_EXTERN:
1076 case LFUN_FONT_FRAK:
1077 flag.enabled(currentMode() != TEXT_MODE);
1080 case LFUN_MATH_INSERT: {
1081 bool const textarg =
1082 arg == "\\textbf" || arg == "\\textsf" ||
1083 arg == "\\textrm" || arg == "\\textmd" ||
1084 arg == "\\textit" || arg == "\\textsc" ||
1085 arg == "\\textsl" || arg == "\\textup" ||
1086 arg == "\\texttt" || arg == "\\textbb" ||
1087 arg == "\\textnormal";
1088 flag.enabled(currentMode() != TEXT_MODE || textarg);
1092 case LFUN_MATH_MATRIX:
1093 flag.enabled(currentMode() == MATH_MODE);
1096 case LFUN_INSET_INSERT: {
1097 // Don't test createMathInset_fromDialogStr(), since
1098 // getStatus is not called with a valid reference and the
1099 // dialog would not be applyable.
1100 string const name = cmd.getArg(0);
1101 flag.enabled(name == "ref");
1105 case LFUN_MATH_DELIM:
1106 case LFUN_MATH_BIGDELIM:
1107 // Don't do this with multi-cell selections
1108 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1111 case LFUN_HYPHENATION_POINT_INSERT:
1112 case LFUN_LIGATURE_BREAK_INSERT:
1113 case LFUN_MENU_SEPARATOR_INSERT:
1114 case LFUN_DOTS_INSERT:
1115 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1116 // FIXME: These would probably make sense in math-text mode
1117 flag.enabled(false);
1120 case LFUN_INSET_DISSOLVE:
1121 flag.enabled(!asHullInset());
1132 void InsetMathNest::edit(Cursor & cur, bool left)
1135 cur.idx() = left ? 0 : cur.lastidx();
1136 cur.pos() = left ? 0 : cur.lastpos();
1138 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1142 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1145 int dist_min = 1000000;
1146 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1147 int const d = cell(i).dist(cur.bv(), x, y);
1153 MathData & ar = cell(idx_min);
1155 cur.idx() = idx_min;
1156 cur.pos() = ar.x2pos(x - ar.xo(cur.bv()));
1158 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1159 if (dist_min == 0) {
1161 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1162 if (ar[i]->covers(cur.bv(), x, y))
1163 return ar[i].nucleus()->editXY(cur, x, y);
1169 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1171 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1172 BufferView & bv = cur.bv();
1173 bool do_selection = cmd.button() == mouse_button::button1
1174 && cmd.argument() == "region-select";
1175 bv.mouseSetCursor(cur, do_selection);
1176 if (cmd.button() == mouse_button::button1) {
1177 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1178 // Update the cursor update flags as needed:
1180 // Update::Decoration: tells to update the decoration
1181 // (visual box corners that define
1183 // Update::FitCursor: adjust the screen to the cursor
1184 // position if needed
1185 // cur.result().update(): don't overwrite previously set flags.
1186 cur.updateFlags(Update::Decoration | Update::FitCursor
1187 | cur.result().update());
1188 } else if (cmd.button() == mouse_button::button2) {
1189 if (cap::selection()) {
1190 // See comment in Text::dispatch why we do this
1191 cap::copySelectionToStack();
1192 cmd = FuncRequest(LFUN_PASTE, "0");
1193 doDispatch(bv.cursor(), cmd);
1196 asArray(theSelection().get(), ar);
1197 bv.cursor().insert(ar);
1203 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1205 // only select with button 1
1206 if (cmd.button() == mouse_button::button1) {
1207 Cursor & bvcur = cur.bv().cursor();
1208 if (bvcur.anchor_.hasPart(cur)) {
1209 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1210 bvcur.setCursor(cur);
1211 bvcur.selection() = true;
1212 //lyxerr << "MOTION " << bvcur << endl;
1219 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1221 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1223 if (cmd.button() == mouse_button::button1) {
1224 if (!cur.selection())
1227 Cursor & bvcur = cur.bv().cursor();
1228 bvcur.selection() = true;
1237 bool InsetMathNest::interpretChar(Cursor & cur, char_type c)
1239 //lyxerr << "interpret 2: '" << c << "'" << endl;
1240 docstring save_selection;
1241 if (c == '^' || c == '_')
1242 save_selection = grabAndEraseSelection(cur);
1247 if (cur.inMacroMode()) {
1248 docstring name = cur.macroName();
1250 /// are we currently typing '#1' or '#2' or...?
1251 if (name == "\\#") {
1254 if (n >= 1 && n <= 9)
1255 cur.insert(new MathMacroArgument(n));
1259 if (isAlphaASCII(c)) {
1260 cur.activeMacro()->setName(name + docstring(1, c));
1264 // handle 'special char' macros
1269 if (currentMode() == InsetMath::TEXT_MODE)
1270 cur.niceInsert(createInsetMath("textbackslash"));
1272 cur.niceInsert(createInsetMath("backslash"));
1273 } else if (c == '{') {
1275 cur.niceInsert(MathAtom(new InsetMathBrace));
1276 } else if (c == '%') {
1278 cur.niceInsert(MathAtom(new InsetMathComment));
1279 } else if (c == '#') {
1280 BOOST_ASSERT(cur.activeMacro());
1281 cur.activeMacro()->setName(name + docstring(1, c));
1284 cur.niceInsert(createInsetMath(docstring(1, c)));
1289 // One character big delimiters. The others are handled in
1290 // interpretString().
1291 latexkeys const * l = in_word_set(name.substr(1));
1292 if (name[0] == '\\' && l && l->inset == "big") {
1296 delim = from_ascii("\\{");
1299 delim = from_ascii("\\}");
1302 delim = docstring(1, c);
1305 if (InsetMathBig::isBigInsetDelim(delim)) {
1306 // name + delim ared a valid InsetMathBig.
1307 // We can't use cur.macroModeClose() because
1308 // it does not handle delim.
1309 InsetMathUnknown * p = cur.activeMacro();
1312 cur.cell().erase(cur.pos());
1313 cur.plainInsert(MathAtom(
1314 new InsetMathBig(name.substr(1), delim)));
1319 // leave macro mode and try again if necessary
1320 cur.macroModeClose();
1322 cur.niceInsert(MathAtom(new InsetMathBrace));
1324 interpretChar(cur, c);
1328 // This is annoying as one has to press <space> far too often.
1332 // leave autocorrect mode if necessary
1333 if (autocorrect() && c == ' ') {
1334 autocorrect() = false;
1339 // just clear selection on pressing the space bar
1340 if (cur.selection() && c == ' ') {
1341 cur.selection() = false;
1348 //lyxerr << "starting with macro" << endl;
1349 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), false)));
1354 if (currentMode() == InsetMath::TEXT_MODE)
1360 if (currentMode() == InsetMath::TEXT_MODE) {
1361 // insert spaces in text mode,
1362 // but suppress direct insertion of two spaces in a row
1363 // the still allows typing '<space>a<space>' and deleting the 'a', but
1364 // it is better than nothing...
1365 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1367 // FIXME: we have to enable full redraw here because of the
1368 // visual box corners that define the inset. If we know for
1369 // sure that we stay within the same cell we can optimize for
1371 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1375 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1376 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1377 // FIXME: we have to enable full redraw here because of the
1378 // visual box corners that define the inset. If we know for
1379 // sure that we stay within the same cell we can optimize for
1381 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1385 if (cur.popRight()) {
1386 // FIXME: we have to enable full redraw here because of the
1387 // visual box corners that define the inset. If we know for
1388 // sure that we stay within the same cell we can optimize for
1390 //cur.updateFlags(Update::FitCursor);
1394 // if we are at the very end, leave the formula
1395 return cur.pos() != cur.lastpos();
1398 // These shouldn't work in text mode:
1399 if (currentMode() != InsetMath::TEXT_MODE) {
1401 script(cur, false, save_selection);
1405 script(cur, true, save_selection);
1409 cur.niceInsert(createInsetMath("sim"));
1414 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1415 c == '%' || c == '_' || c == '^') {
1416 cur.niceInsert(createInsetMath(docstring(1, c)));
1421 // try auto-correction
1422 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1425 // no special circumstances, so insert the character without any fuss
1427 cur.autocorrect() = true;
1432 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1434 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1436 if (!cur.empty() && cur.pos() > 0 &&
1437 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1438 if (InsetMathBig::isBigInsetDelim(str)) {
1439 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1440 if (prev[0] == '\\') {
1441 prev = prev.substr(1);
1442 latexkeys const * l = in_word_set(prev);
1443 if (l && l->inset == "big") {
1444 cur.cell()[cur.pos() - 1] =
1445 MathAtom(new InsetMathBig(prev, str));
1455 bool InsetMathNest::script(Cursor & cur, bool up,
1456 docstring const & save_selection)
1458 // Hack to get \^ and \_ working
1459 //lyxerr << "handling script: up: " << up << endl;
1460 if (cur.inMacroMode() && cur.macroName() == "\\") {
1462 cur.niceInsert(createInsetMath("mathcircumflex"));
1464 interpretChar(cur, '_');
1468 cur.macroModeClose();
1469 if (asScriptInset() && cur.idx() == 0) {
1470 // we are in a nucleus of a script inset, move to _our_ script
1471 InsetMathScript * inset = asScriptInset();
1472 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1474 cur.idx() = inset->idxOfScript(up);
1476 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1478 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1481 cur.idx() = inset->idxOfScript(up);
1482 cur.pos() = cur.lastpos();
1484 // convert the thing to our left to a scriptinset or create a new
1485 // one if in the very first position of the array
1486 if (cur.pos() == 0) {
1487 //lyxerr << "new scriptinset" << endl;
1488 cur.insert(new InsetMathScript(up));
1490 //lyxerr << "converting prev atom " << endl;
1491 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1494 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1495 // See comment in MathParser.cpp for special handling of {}-bases
1501 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1502 cur.niceInsert(save_selection);
1504 //lyxerr << "inserting selection 2:\n" << save_selection << endl;