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"
22 #include "InsetMathDelim.h"
23 #include "MathFactory.h"
24 #include "InsetMathHull.h"
25 #include "MathStream.h"
26 #include "MathMacroArgument.h"
27 //#include "InsetMathMBox.h"
28 #include "MathParser.h"
29 #include "InsetMathScript.h"
30 #include "InsetMathSpace.h"
31 #include "InsetMathSymbol.h"
32 #include "MathSupport.h"
33 #include "InsetMathUnknown.h"
34 #include "InsetMathRef.h"
36 #include "BufferView.h"
37 #include "CutAndPaste.h"
38 #include "FuncStatus.h"
40 #include "bufferview_funcs.h"
41 #include "CoordCache.h"
44 #include "DispatchResult.h"
45 #include "FuncRequest.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"
59 #include "LyXServer.h"
60 #include "LyXServerSocket.h"
67 using cap::copySelection;
68 using cap::grabAndEraseSelection;
69 using cap::cutSelection;
70 using cap::replaceSelection;
71 using cap::selClearOrDel;
75 using std::istringstream;
78 InsetMathNest::InsetMathNest(idx_type nargs)
79 : cells_(nargs), lock_(false)
83 InsetMath::idx_type InsetMathNest::nargs() const
89 void InsetMathNest::cursorPos(BufferView const & bv,
90 CursorSlice const & sl, bool /*boundary*/,
91 int & x, int & y) const
93 // FIXME: This is a hack. Ideally, the coord cache should not store
94 // absolute positions, but relative ones. This would mean to call
95 // setXY() not in MathData::draw(), but in the parent insets' draw()
96 // with the correctly adjusted x,y values. But this means that we'd have
97 // to touch all (math)inset's draw() methods. Right now, we'll store
98 // absolute value, and make them here relative, only to make them
99 // absolute again when actually drawing the cursor. What a mess.
100 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
101 MathData const & ar = sl.cell();
102 CoordCache const & coord_cache = bv.coordCache();
103 if (!coord_cache.getArrays().has(&ar)) {
104 // this can (semi-)legally happen if we just created this cell
105 // and it never has been drawn before. So don't ASSERT.
106 //lyxerr << "no cached data for array " << &ar << endl;
111 Point const pt = coord_cache.getArrays().xy(&ar);
112 if (!coord_cache.getInsets().has(this)) {
114 //lyxerr << "no cached data for inset " << this << endl;
119 Point const pt2 = coord_cache.getInsets().xy(this);
120 //lyxerr << "retrieving position cache for MathData "
121 // << pt.x_ << ' ' << pt.y_ << std::endl;
122 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
124 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
125 // << " asc: " << ascent() << " des: " << descent()
126 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
127 // move cursor visually into empty cells ("blue rectangles");
133 void InsetMathNest::metrics(MetricsInfo const & mi) const
136 for (idx_type i = 0, n = nargs(); i != n; ++i)
141 bool InsetMathNest::idxNext(Cursor & cur) const
143 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
144 if (cur.idx() == cur.lastidx())
152 bool InsetMathNest::idxRight(Cursor & cur) const
158 bool InsetMathNest::idxPrev(Cursor & cur) const
160 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
164 cur.pos() = cur.lastpos();
169 bool InsetMathNest::idxLeft(Cursor & cur) const
175 bool InsetMathNest::idxFirst(Cursor & cur) const
177 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
186 bool InsetMathNest::idxLast(Cursor & cur) const
188 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
191 cur.idx() = cur.lastidx();
192 cur.pos() = cur.lastpos();
197 void InsetMathNest::dump() const
199 odocstringstream oss;
201 os << "---------------------------------------------\n";
204 for (idx_type i = 0, n = nargs(); i != n; ++i)
205 os << cell(i) << "\n";
206 os << "---------------------------------------------\n";
207 lyxerr << to_utf8(oss.str());
211 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
215 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
218 setPosCache(pi, x, y);
222 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
224 BufferView & bv = *pi.base.bv;
225 // this should use the x/y values given, not the cached values
226 Cursor & cur = bv.cursor();
227 if (!cur.selection())
229 if (!ptr_cmp(&cur.inset(), this))
232 // FIXME: hack to get position cache warm
233 pi.pain.setDrawingEnabled(false);
235 pi.pain.setDrawingEnabled(true);
237 CursorSlice s1 = cur.selBegin();
238 CursorSlice s2 = cur.selEnd();
240 //lyxerr << "InsetMathNest::drawing selection: "
241 // << " s1: " << s1 << " s2: " << s2 << endl;
242 if (s1.idx() == s2.idx()) {
243 MathData const & c = cell(s1.idx());
244 int x1 = c.xo(bv) + c.pos2x(s1.pos());
245 int y1 = c.yo(bv) - c.ascent();
246 int x2 = c.xo(bv) + c.pos2x(s2.pos());
247 int y2 = c.yo(bv) + c.descent();
248 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
249 //lyxerr << "InsetMathNest::drawing selection 3: "
250 // << " x1: " << x1 << " x2: " << x2
251 // << " y1: " << y1 << " y2: " << y2 << endl;
253 for (idx_type i = 0; i < nargs(); ++i) {
254 if (idxBetween(i, s1.idx(), s2.idx())) {
255 MathData const & c = cell(i);
257 int y1 = c.yo(bv) - c.ascent();
258 int x2 = c.xo(bv) + c.width();
259 int y2 = c.yo(bv) + c.descent();
260 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
267 void InsetMathNest::validate(LaTeXFeatures & features) const
269 for (idx_type i = 0; i < nargs(); ++i)
270 cell(i).validate(features);
274 void InsetMathNest::replace(ReplaceData & rep)
276 for (idx_type i = 0; i < nargs(); ++i)
277 cell(i).replace(rep);
281 bool InsetMathNest::contains(MathData const & ar) const
283 for (idx_type i = 0; i < nargs(); ++i)
284 if (cell(i).contains(ar))
290 bool InsetMathNest::lock() const
296 void InsetMathNest::lock(bool l)
302 bool InsetMathNest::isActive() const
308 MathData InsetMathNest::glue() const
311 for (size_t i = 0; i < nargs(); ++i)
317 void InsetMathNest::write(WriteStream & os) const
319 os << '\\' << name().c_str();
320 for (size_t i = 0; i < nargs(); ++i)
321 os << '{' << cell(i) << '}';
323 os.pendingSpace(true);
324 if (lock_ && !os.latex()) {
326 os.pendingSpace(true);
331 void InsetMathNest::normalize(NormalStream & os) const
333 os << '[' << name().c_str();
334 for (size_t i = 0; i < nargs(); ++i)
335 os << ' ' << cell(i);
340 int InsetMathNest::latex(Buffer const &, odocstream & os,
341 OutputParams const & runparams) const
343 WriteStream wi(os, runparams.moving_arg, true);
349 bool InsetMathNest::notifyCursorLeaves(Cursor & /*cur*/)
355 MathData & ar = cur.cell();
356 // remove base-only "scripts"
357 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
358 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
359 if (p && p->nargs() == 1) {
360 MathData ar = p->nuc();
363 cur.adjust(i, ar.size() - 1);
367 // glue adjacent font insets of the same kind
368 for (pos_type i = 0; i + 1 < size(); ++i) {
369 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
370 InsetMathFont const * q = operator[](i + 1)->asFontInset();
371 if (p && q && p->name() == q->name()) {
372 p->cell(0).append(q->cell(0));
382 void InsetMathNest::handleFont
383 (Cursor & cur, docstring const & arg, char const * const font)
385 handleFont(cur, arg, from_ascii(font));
389 void InsetMathNest::handleFont
390 (Cursor & cur, docstring const & arg, docstring const & font)
392 // this whole function is a hack and won't work for incremental font
395 if (cur.inset().asInsetMath()->name() == font) {
396 recordUndoInset(cur, Undo::ATOMIC);
397 cur.handleFont(to_utf8(font));
399 recordUndo(cur, Undo::ATOMIC);
400 cur.handleNest(createInsetMath(font));
406 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
408 recordUndo(cur, Undo::ATOMIC);
411 bv_funcs::string2font(to_utf8(arg), font, b);
412 if (font.color() != Color::inherit) {
413 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
414 cur.handleNest(at, 0);
419 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
421 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
422 //CursorSlice sl = cur.current();
424 switch (cmd.action) {
428 cur.message(_("Paste"));
429 replaceSelection(cur);
431 if (cmd.argument().empty() && !theClipboard().isInternal())
432 topaste = theClipboard().getAsText();
435 idocstringstream is(cmd.argument());
437 topaste = cap::getSelection(cur.buffer(), n);
439 cur.niceInsert(topaste);
440 cur.clearSelection(); // bug 393
441 cur.bv().switchKeyMap();
448 cutSelection(cur, true, true);
449 cur.message(_("Cut"));
450 // Prevent stale position >= size crash
451 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
457 cur.message(_("Copy"));
460 case LFUN_MOUSE_PRESS:
461 lfunMousePress(cur, cmd);
464 case LFUN_MOUSE_MOTION:
465 lfunMouseMotion(cur, cmd);
468 case LFUN_MOUSE_RELEASE:
469 lfunMouseRelease(cur, cmd);
472 case LFUN_FINISHED_LEFT:
473 cur.bv().cursor() = cur;
476 case LFUN_FINISHED_RIGHT:
478 cur.bv().cursor() = cur;
481 case LFUN_FINISHED_UP:
482 cur.bv().cursor() = cur;
485 case LFUN_FINISHED_DOWN:
487 cur.bv().cursor() = cur;
490 case LFUN_CHAR_FORWARD:
491 cur.updateFlags(Update::Decoration | Update::FitCursor);
492 case LFUN_CHAR_FORWARD_SELECT:
493 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
494 cur.autocorrect() = false;
496 cur.macroModeClose();
497 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
498 cur.pushLeft(*cur.nextAtom().nucleus());
499 cur.inset().idxFirst(cur);
500 } else if (cur.posRight() || idxRight(cur)
501 || cur.popRight() || cur.selection())
504 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
509 case LFUN_CHAR_BACKWARD:
510 cur.updateFlags(Update::Decoration | Update::FitCursor);
511 case LFUN_CHAR_BACKWARD_SELECT:
512 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
513 cur.autocorrect() = false;
515 cur.macroModeClose();
516 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
518 cur.push(*cur.nextAtom().nucleus());
519 cur.inset().idxLast(cur);
520 } else if (cur.posLeft() || idxLeft(cur)
521 || cur.popLeft() || cur.selection())
524 cmd = FuncRequest(LFUN_FINISHED_LEFT);
530 cur.updateFlags(Update::Decoration | Update::FitCursor);
532 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
533 if (cur.inMacroMode()) {
535 cur.macroModeClose();
538 cur.selHandle(cmd.action == LFUN_UP_SELECT);
540 cmd = FuncRequest(LFUN_FINISHED_UP);
543 // fixes bug 1598. Please check!
548 cur.updateFlags(Update::Decoration | Update::FitCursor);
549 case LFUN_DOWN_SELECT:
550 if (cur.inMacroMode()) {
551 cur.macroModeClose();
554 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
556 cmd = FuncRequest(LFUN_FINISHED_DOWN);
559 // fixes bug 1598. Please check!
563 case LFUN_MOUSE_DOUBLE:
564 case LFUN_MOUSE_TRIPLE:
565 case LFUN_WORD_SELECT:
569 cur.selection() = true;
570 cur.pos() = cur.lastpos();
571 cur.idx() = cur.lastidx();
572 cap::saveSelection(cur);
575 case LFUN_PARAGRAPH_UP:
576 case LFUN_PARAGRAPH_DOWN:
577 cur.updateFlags(Update::Decoration | Update::FitCursor);
578 case LFUN_PARAGRAPH_UP_SELECT:
579 case LFUN_PARAGRAPH_DOWN_SELECT:
582 case LFUN_LINE_BEGIN:
583 case LFUN_WORD_BACKWARD:
584 cur.updateFlags(Update::Decoration | Update::FitCursor);
585 case LFUN_LINE_BEGIN_SELECT:
586 case LFUN_WORD_BACKWARD_SELECT:
587 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
588 cmd.action == LFUN_LINE_BEGIN_SELECT);
589 cur.macroModeClose();
590 if (cur.pos() != 0) {
592 } else if (cur.col() != 0) {
593 cur.idx() -= cur.col();
595 } else if (cur.idx() != 0) {
599 cmd = FuncRequest(LFUN_FINISHED_LEFT);
604 case LFUN_WORD_FORWARD:
606 cur.updateFlags(Update::Decoration | Update::FitCursor);
607 case LFUN_WORD_FORWARD_SELECT:
608 case LFUN_LINE_END_SELECT:
609 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
610 cmd.action == LFUN_LINE_END_SELECT);
611 cur.macroModeClose();
613 if (cur.pos() != cur.lastpos()) {
614 cur.pos() = cur.lastpos();
615 } else if (ncols() && (cur.col() != cur.lastcol())) {
616 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
617 cur.pos() = cur.lastpos();
618 } else if (cur.idx() != cur.lastidx()) {
619 cur.idx() = cur.lastidx();
620 cur.pos() = cur.lastpos();
622 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
627 case LFUN_SCREEN_UP_SELECT:
629 cmd = FuncRequest(LFUN_FINISHED_LEFT);
633 case LFUN_SCREEN_DOWN_SELECT:
634 case LFUN_SCREEN_DOWN:
635 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
639 case LFUN_CELL_FORWARD:
640 cur.updateFlags(Update::Decoration | Update::FitCursor);
641 cur.inset().idxNext(cur);
644 case LFUN_CELL_BACKWARD:
645 cur.updateFlags(Update::Decoration | Update::FitCursor);
646 cur.inset().idxPrev(cur);
649 case LFUN_WORD_DELETE_BACKWARD:
650 case LFUN_CHAR_DELETE_BACKWARD:
652 // May affect external cell:
653 recordUndoInset(cur, Undo::ATOMIC);
655 recordUndo(cur, Undo::ATOMIC);
656 // if the inset can not be removed from within, delete it
657 if (!cur.backspace()) {
658 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
659 cur.innerText()->dispatch(cur, cmd);
663 case LFUN_WORD_DELETE_FORWARD:
664 case LFUN_CHAR_DELETE_FORWARD:
665 if (cur.pos() == cur.lastpos())
666 // May affect external cell:
667 recordUndoInset(cur, Undo::ATOMIC);
669 recordUndo(cur, Undo::ATOMIC);
670 // if the inset can not be removed from within, delete it
672 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
673 cur.innerText()->dispatch(cur, cmd);
679 cur.clearSelection();
681 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
686 case LFUN_INSET_TOGGLE:
692 case LFUN_SELF_INSERT:
693 if (cmd.argument().size() != 1) {
695 docstring const arg = cmd.argument();
696 if (!interpretString(cur, arg))
700 // Don't record undo steps if we are in macro mode and
701 // cmd.argument is the next character of the macro name.
702 // Otherwise we'll get an invalid cursor if we undo after
703 // the macro was finished and the macro is a known command,
704 // e.g. sqrt. Cursor::macroModeClose replaces in this case
705 // the InsetMathUnknown with name "frac" by an empty
706 // InsetMathFrac -> a pos value > 0 is invalid.
707 // A side effect is that an undo before the macro is finished
708 // undoes the complete macro, not only the last character.
709 if (!cur.inMacroMode())
712 // spacial handling of space. If we insert an inset
713 // via macro mode, we want to put the cursor inside it
714 // if relevant. Think typing "\frac<space>".
715 if (cmd.argument()[0] == ' '
716 && cur.inMacroMode() && cur.macroName() != "\\"
717 && cur.macroModeClose()) {
718 MathAtom const atom = cur.prevAtom();
719 if (atom->asNestInset() && atom->nargs() > 0) {
721 cur.pushLeft(*cur.nextInset());
723 } else if (!interpretChar(cur, cmd.argument()[0])) {
724 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
729 //case LFUN_SERVER_GET_XY:
730 // sprintf(dispatch_buffer, "%d %d",);
733 case LFUN_SERVER_SET_XY: {
734 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
737 istringstream is(to_utf8(cmd.argument()));
739 cur.setScreenPos(x, y);
743 // Special casing for superscript in case of LyX handling
745 case LFUN_ACCENT_CIRCUMFLEX:
746 if (cmd.argument().empty()) {
747 // do superscript if LyX handles
749 recordUndo(cur, Undo::ATOMIC);
750 script(cur, true, grabAndEraseSelection(cur));
754 case LFUN_ACCENT_UMLAUT:
755 case LFUN_ACCENT_ACUTE:
756 case LFUN_ACCENT_GRAVE:
757 case LFUN_ACCENT_BREVE:
758 case LFUN_ACCENT_DOT:
759 case LFUN_ACCENT_MACRON:
760 case LFUN_ACCENT_CARON:
761 case LFUN_ACCENT_TILDE:
762 case LFUN_ACCENT_CEDILLA:
763 case LFUN_ACCENT_CIRCLE:
764 case LFUN_ACCENT_UNDERDOT:
765 case LFUN_ACCENT_TIE:
766 case LFUN_ACCENT_OGONEK:
767 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
771 case LFUN_FONT_FREE_APPLY:
772 case LFUN_FONT_FREE_UPDATE:
773 handleFont2(cur, cmd.argument());
777 if (currentMode() == TEXT_MODE)
778 handleFont(cur, cmd.argument(), "textbf");
780 handleFont(cur, cmd.argument(), "mathbf");
783 if (currentMode() == TEXT_MODE)
784 handleFont(cur, cmd.argument(), "textsf");
786 handleFont(cur, cmd.argument(), "mathsf");
789 if (currentMode() == TEXT_MODE)
790 handleFont(cur, cmd.argument(), "emph");
792 handleFont(cur, cmd.argument(), "mathcal");
794 case LFUN_FONT_ROMAN:
795 if (currentMode() == TEXT_MODE)
796 handleFont(cur, cmd.argument(), "textrm");
798 handleFont(cur, cmd.argument(), "mathrm");
801 if (currentMode() == TEXT_MODE)
802 handleFont(cur, cmd.argument(), "texttt");
804 handleFont(cur, cmd.argument(), "mathtt");
807 handleFont(cur, cmd.argument(), "mathfrak");
810 if (currentMode() == TEXT_MODE)
811 handleFont(cur, cmd.argument(), "textit");
813 handleFont(cur, cmd.argument(), "mathit");
816 if (currentMode() == TEXT_MODE)
817 // FIXME: should be "noun"
818 handleFont(cur, cmd.argument(), "textsc");
820 handleFont(cur, cmd.argument(), "mathbb");
823 case LFUN_FONT_FREE_APPLY:
824 handleFont(cur, cmd.argument(), "textrm");
827 case LFUN_FONT_DEFAULT:
828 handleFont(cur, cmd.argument(), "textnormal");
831 case LFUN_MATH_MODE: {
833 // ignore math-mode on when already in math mode
834 if (currentMode() == InsetBase::MATH_MODE && cmd.argument() == "on")
836 cur.macroModeClose();
837 docstring const save_selection = grabAndEraseSelection(cur);
839 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
840 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
842 cur.pushLeft(*cur.nextInset());
843 cur.niceInsert(save_selection);
845 if (currentMode() == InsetBase::TEXT_MODE) {
846 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
847 cur.message(_("create new math text environment ($...$)"));
849 handleFont(cur, cmd.argument(), "textrm");
850 cur.message(_("entered math text mode (textrm)"));
863 case LFUN_MATH_MATRIX: {
864 recordUndo(cur, Undo::ATOMIC);
869 idocstringstream is(cmd.argument());
870 is >> m >> n >> v_align >> h_align;
877 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
881 case LFUN_MATH_DELIM: {
883 docstring rs = support::split(cmd.argument(), ls, ' ');
884 // Reasonable default values
889 recordUndo(cur, Undo::ATOMIC);
890 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
894 case LFUN_MATH_BIGDELIM: {
895 docstring const lname = from_utf8(cmd.getArg(0));
896 docstring const ldelim = from_utf8(cmd.getArg(1));
897 docstring const rname = from_utf8(cmd.getArg(2));
898 docstring const rdelim = from_utf8(cmd.getArg(3));
899 latexkeys const * l = in_word_set(lname);
900 bool const have_l = l && l->inset == "big" &&
901 InsetMathBig::isBigInsetDelim(ldelim);
902 l = in_word_set(rname);
903 bool const have_r = l && l->inset == "big" &&
904 InsetMathBig::isBigInsetDelim(rdelim);
905 // We mimic LFUN_MATH_DELIM in case we have an empty left
906 // or right delimiter.
907 if (have_l || have_r) {
908 recordUndo(cur, Undo::ATOMIC);
909 docstring const selection = grabAndEraseSelection(cur);
912 cur.insert(MathAtom(new InsetMathBig(lname,
914 cur.niceInsert(selection);
916 cur.insert(MathAtom(new InsetMathBig(rname,
919 // Don't call cur.undispatched() if we did nothing, this would
920 // lead to infinite recursion via LyXText::dispatch().
924 case LFUN_SPACE_INSERT:
925 case LFUN_MATH_SPACE:
926 recordUndo(cur, Undo::ATOMIC);
927 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
930 case LFUN_ERT_INSERT:
931 // interpret this as if a backslash was typed
932 recordUndo(cur, Undo::ATOMIC);
933 interpretChar(cur, '\\');
936 case LFUN_MATH_SUBSCRIPT:
937 // interpret this as if a _ was typed
938 recordUndo(cur, Undo::ATOMIC);
939 interpretChar(cur, '_');
942 case LFUN_MATH_SUPERSCRIPT:
943 // interpret this as if a ^ was typed
944 recordUndo(cur, Undo::ATOMIC);
945 interpretChar(cur, '^');
948 case LFUN_QUOTE_INSERT:
949 // interpret this as if a straight " was typed
950 recordUndo(cur, Undo::ATOMIC);
951 interpretChar(cur, '\"');
954 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
955 // handling such that "self-insert" works on "arbitrary stuff" too, and
956 // math-insert only handles special math things like "matrix".
957 case LFUN_MATH_INSERT: {
958 recordUndo(cur, Undo::ATOMIC);
959 if (cmd.argument() == "^" || cmd.argument() == "_") {
960 interpretChar(cur, cmd.argument()[0]);
962 cur.niceInsert(cmd.argument());
966 case LFUN_DIALOG_SHOW_NEW_INSET: {
967 docstring const & name = cmd.argument();
970 InsetMathRef tmp(name);
971 data = tmp.createDialogStr(to_utf8(name));
973 cur.bv().showInsetDialog(to_utf8(name), data, 0);
977 case LFUN_INSET_INSERT: {
979 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
988 InsetMathDim::doDispatch(cur, cmd);
994 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
995 FuncStatus & flag) const
997 // the font related toggles
998 //string tc = "mathnormal";
1000 string const arg = to_utf8(cmd.argument());
1001 switch (cmd.action) {
1002 case LFUN_TABULAR_FEATURE:
1003 flag.enabled(false);
1006 case LFUN_TABULAR_FEATURE:
1007 // FIXME: check temporarily disabled
1009 char align = mathcursor::valign();
1010 if (align == '\0') {
1014 if (cmd.argument().empty()) {
1018 if (!contains("tcb", cmd.argument()[0])) {
1022 flag.setOnOff(cmd.argument()[0] == align);
1025 /// We have to handle them since 1.4 blocks all unhandled actions
1026 case LFUN_FONT_ITAL:
1027 case LFUN_FONT_BOLD:
1028 case LFUN_FONT_SANS:
1029 case LFUN_FONT_EMPH:
1030 case LFUN_FONT_CODE:
1031 case LFUN_FONT_NOUN:
1032 case LFUN_FONT_ROMAN:
1033 case LFUN_FONT_DEFAULT:
1036 case LFUN_MATH_MUTATE:
1037 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1038 flag.setOnOff(false);
1041 // we just need to be in math mode to enable that
1042 case LFUN_MATH_SIZE:
1043 case LFUN_MATH_SPACE:
1044 case LFUN_MATH_LIMITS:
1045 case LFUN_MATH_NONUMBER:
1046 case LFUN_MATH_NUMBER:
1047 case LFUN_MATH_EXTERN:
1051 case LFUN_FONT_FRAK:
1052 flag.enabled(currentMode() != TEXT_MODE);
1055 case LFUN_MATH_INSERT: {
1056 bool const textarg =
1057 arg == "\\textbf" || arg == "\\textsf" ||
1058 arg == "\\textrm" || arg == "\\textmd" ||
1059 arg == "\\textit" || arg == "\\textsc" ||
1060 arg == "\\textsl" || arg == "\\textup" ||
1061 arg == "\\texttt" || arg == "\\textbb" ||
1062 arg == "\\textnormal";
1063 flag.enabled(currentMode() != TEXT_MODE || textarg);
1067 case LFUN_MATH_MATRIX:
1068 flag.enabled(currentMode() == MATH_MODE);
1071 case LFUN_INSET_INSERT: {
1072 // Don't test createMathInset_fromDialogStr(), since
1073 // getStatus is not called with a valid reference and the
1074 // dialog would not be applyable.
1075 string const name = cmd.getArg(0);
1076 flag.enabled(name == "ref");
1080 case LFUN_MATH_DELIM:
1081 case LFUN_MATH_BIGDELIM:
1082 // Don't do this with multi-cell selections
1083 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1094 void InsetMathNest::edit(Cursor & cur, bool left)
1097 cur.idx() = left ? 0 : cur.lastidx();
1098 cur.pos() = left ? 0 : cur.lastpos();
1100 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1104 InsetBase * InsetMathNest::editXY(Cursor & cur, int x, int y)
1107 int dist_min = 1000000;
1108 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1109 int const d = cell(i).dist(cur.bv(), x, y);
1115 MathData & ar = cell(idx_min);
1117 cur.idx() = idx_min;
1118 cur.pos() = ar.x2pos(x - ar.xo(cur.bv()));
1120 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1121 if (dist_min == 0) {
1123 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1124 if (ar[i]->covers(cur.bv(), x, y))
1125 return ar[i].nucleus()->editXY(cur, x, y);
1131 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1133 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1134 BufferView & bv = cur.bv();
1135 if (cmd.button() == mouse_button::button1) {
1136 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1137 bv.mouseSetCursor(cur);
1138 // Update the cursor update flags as needed:
1140 // Update::Decoration: tells to update the decoration (visual box
1141 // corners that define the inset)/
1142 // Update::FitCursor: adjust the screen to the cursor position if
1144 // cur.result().update(): don't overwrite previously set flags.
1145 cur.updateFlags(Update::Decoration | Update::FitCursor | cur.result().update());
1146 } else if (cmd.button() == mouse_button::button2) {
1148 if (cap::selection()) {
1149 // See comment in LyXText::dispatch why we do this
1150 cap::copySelectionToStack();
1151 cmd = FuncRequest(LFUN_PASTE, "0");
1152 doDispatch(cur, cmd);
1154 asArray(theSelection().get(), ar);
1157 bv.mouseSetCursor(cur);
1162 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1164 // only select with button 1
1165 if (cmd.button() == mouse_button::button1) {
1166 Cursor & bvcur = cur.bv().cursor();
1167 if (bvcur.anchor_.hasPart(cur)) {
1168 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1169 bvcur.setCursor(cur);
1170 bvcur.selection() = true;
1171 //lyxerr << "MOTION " << bvcur << endl;
1178 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1180 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1182 if (cmd.button() == mouse_button::button1) {
1183 if (!cur.selection())
1186 Cursor & bvcur = cur.bv().cursor();
1187 bvcur.selection() = true;
1188 cap::saveSelection(bvcur);
1197 bool InsetMathNest::interpretChar(Cursor & cur, char_type c)
1199 //lyxerr << "interpret 2: '" << c << "'" << endl;
1200 docstring save_selection;
1201 if (c == '^' || c == '_')
1202 save_selection = grabAndEraseSelection(cur);
1207 if (cur.inMacroMode()) {
1208 docstring name = cur.macroName();
1210 /// are we currently typing '#1' or '#2' or...?
1211 if (name == "\\#") {
1214 if (n >= 1 && n <= 9)
1215 cur.insert(new MathMacroArgument(n));
1219 if (isAlphaASCII(c)) {
1220 cur.activeMacro()->setName(name + docstring(1, c));
1224 // handle 'special char' macros
1229 if (currentMode() == InsetMath::TEXT_MODE)
1230 cur.niceInsert(createInsetMath("textbackslash"));
1232 cur.niceInsert(createInsetMath("backslash"));
1233 } else if (c == '{') {
1235 cur.niceInsert(MathAtom(new InsetMathBrace));
1236 } else if (c == '%') {
1238 cur.niceInsert(MathAtom(new InsetMathComment));
1239 } else if (c == '#') {
1240 BOOST_ASSERT(cur.activeMacro());
1241 cur.activeMacro()->setName(name + docstring(1, c));
1244 cur.niceInsert(createInsetMath(docstring(1, c)));
1249 // One character big delimiters. The others are handled in
1250 // interpretString().
1251 latexkeys const * l = in_word_set(name.substr(1));
1252 if (name[0] == '\\' && l && l->inset == "big") {
1256 delim = from_ascii("\\{");
1259 delim = from_ascii("\\}");
1262 delim = docstring(1, c);
1265 if (InsetMathBig::isBigInsetDelim(delim)) {
1266 // name + delim ared a valid InsetMathBig.
1267 // We can't use cur.macroModeClose() because
1268 // it does not handle delim.
1269 InsetMathUnknown * p = cur.activeMacro();
1272 cur.cell().erase(cur.pos());
1273 cur.plainInsert(MathAtom(
1274 new InsetMathBig(name.substr(1), delim)));
1279 // leave macro mode and try again if necessary
1280 cur.macroModeClose();
1282 cur.niceInsert(MathAtom(new InsetMathBrace));
1284 interpretChar(cur, c);
1288 // This is annoying as one has to press <space> far too often.
1292 // leave autocorrect mode if necessary
1293 if (autocorrect() && c == ' ') {
1294 autocorrect() = false;
1299 // just clear selection on pressing the space bar
1300 if (cur.selection() && c == ' ') {
1301 cur.selection() = false;
1308 //lyxerr << "starting with macro" << endl;
1309 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), false)));
1314 if (currentMode() == InsetMath::TEXT_MODE)
1320 if (currentMode() == InsetMath::TEXT_MODE) {
1321 // insert spaces in text mode,
1322 // but suppress direct insertion of two spaces in a row
1323 // the still allows typing '<space>a<space>' and deleting the 'a', but
1324 // it is better than nothing...
1325 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1327 // FIXME: we have to enable full redraw here because of the
1328 // visual box corners that define the inset. If we know for
1329 // sure that we stay within the same cell we can optimize for
1331 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1335 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1336 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1337 // FIXME: we have to enable full redraw here because of the
1338 // visual box corners that define the inset. If we know for
1339 // sure that we stay within the same cell we can optimize for
1341 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1345 if (cur.popRight()) {
1346 // FIXME: we have to enable full redraw here because of the
1347 // visual box corners that define the inset. If we know for
1348 // sure that we stay within the same cell we can optimize for
1350 //cur.updateFlags(Update::FitCursor);
1354 // if we are at the very end, leave the formula
1355 return cur.pos() != cur.lastpos();
1358 // These shouldn't work in text mode:
1359 if (currentMode() != InsetMath::TEXT_MODE) {
1361 script(cur, false, save_selection);
1365 script(cur, true, save_selection);
1369 cur.niceInsert(createInsetMath("sim"));
1374 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1375 c == '%' || c == '_' || c == '^') {
1376 cur.niceInsert(createInsetMath(docstring(1, c)));
1381 // try auto-correction
1382 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1385 // no special circumstances, so insert the character without any fuss
1387 cur.autocorrect() = true;
1392 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1394 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1396 if (!cur.empty() && cur.pos() > 0 &&
1397 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1398 if (InsetMathBig::isBigInsetDelim(str)) {
1399 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1400 if (prev[0] == '\\') {
1401 prev = prev.substr(1);
1402 latexkeys const * l = in_word_set(prev);
1403 if (l && l->inset == "big") {
1404 cur.cell()[cur.pos() - 1] =
1405 MathAtom(new InsetMathBig(prev, str));
1415 bool InsetMathNest::script(Cursor & cur, bool up,
1416 docstring const & save_selection)
1418 // Hack to get \^ and \_ working
1419 //lyxerr << "handling script: up: " << up << endl;
1420 if (cur.inMacroMode() && cur.macroName() == "\\") {
1422 cur.niceInsert(createInsetMath("mathcircumflex"));
1424 interpretChar(cur, '_');
1428 cur.macroModeClose();
1429 if (asScriptInset() && cur.idx() == 0) {
1430 // we are in a nucleus of a script inset, move to _our_ script
1431 InsetMathScript * inset = asScriptInset();
1432 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1434 cur.idx() = inset->idxOfScript(up);
1436 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1438 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1441 cur.idx() = inset->idxOfScript(up);
1442 cur.pos() = cur.lastpos();
1444 // convert the thing to our left to a scriptinset or create a new
1445 // one if in the very first position of the array
1446 if (cur.pos() == 0) {
1447 //lyxerr << "new scriptinset" << endl;
1448 cur.insert(new InsetMathScript(up));
1450 //lyxerr << "converting prev atom " << endl;
1451 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1454 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1455 // See comment in MathParser.cpp for special handling of {}-bases
1461 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1462 cur.niceInsert(save_selection);
1464 //lyxerr << "inserting selection 2:\n" << save_selection << endl;