2 * \file InsetMathNest.C
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"
53 #include "frontends/Painter.h"
54 #include "frontends/Selection.h"
56 #include "funcrequest.h"
57 #include "lyxserver.h"
58 #include "lyxsocket.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 MathArray & InsetMathNest::cell(idx_type i)
93 MathArray const & InsetMathNest::cell(idx_type i) const
99 void InsetMathNest::cursorPos(BufferView const & bv,
100 CursorSlice const & sl, bool /*boundary*/,
101 int & x, int & y) const
103 // FIXME: This is a hack. Ideally, the coord cache should not store
104 // absolute positions, but relative ones. This would mean to call
105 // setXY() not in MathArray::draw(), but in the parent insets' draw()
106 // with the correctly adjusted x,y values. But this means that we'd have
107 // to touch all (math)inset's draw() methods. Right now, we'll store
108 // absolute value, and make them here relative, only to make them
109 // absolute again when actually drawing the cursor. What a mess.
110 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
111 MathArray const & ar = sl.cell();
112 CoordCache const & coord_cache = bv.coordCache();
113 if (!coord_cache.getArrays().has(&ar)) {
114 // this can (semi-)legally happen if we just created this cell
115 // and it never has been drawn before. So don't ASSERT.
116 //lyxerr << "no cached data for array " << &ar << endl;
121 Point const pt = coord_cache.getArrays().xy(&ar);
122 if (!coord_cache.getInsets().has(this)) {
124 //lyxerr << "no cached data for inset " << this << endl;
129 Point const pt2 = coord_cache.getInsets().xy(this);
130 //lyxerr << "retrieving position cache for MathArray "
131 // << pt.x_ << ' ' << pt.y_ << std::endl;
132 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
134 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
135 // << " asc: " << ascent() << " des: " << descent()
136 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
137 // move cursor visually into empty cells ("blue rectangles");
143 void InsetMathNest::metrics(MetricsInfo const & mi) const
146 for (idx_type i = 0, n = nargs(); i != n; ++i)
151 bool InsetMathNest::idxNext(LCursor & cur) const
153 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
154 if (cur.idx() == cur.lastidx())
162 bool InsetMathNest::idxRight(LCursor & cur) const
168 bool InsetMathNest::idxPrev(LCursor & cur) const
170 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
174 cur.pos() = cur.lastpos();
179 bool InsetMathNest::idxLeft(LCursor & cur) const
185 bool InsetMathNest::idxFirst(LCursor & cur) const
187 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
196 bool InsetMathNest::idxLast(LCursor & cur) const
198 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
201 cur.idx() = cur.lastidx();
202 cur.pos() = cur.lastpos();
207 void InsetMathNest::dump() const
209 odocstringstream oss;
211 os << "---------------------------------------------\n";
214 for (idx_type i = 0, n = nargs(); i != n; ++i)
215 os << cell(i) << "\n";
216 os << "---------------------------------------------\n";
217 lyxerr << to_utf8(oss.str());
221 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
225 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
228 setPosCache(pi, x, y);
232 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
234 BufferView & bv = *pi.base.bv;
235 // this should use the x/y values given, not the cached values
236 LCursor & cur = bv.cursor();
237 if (!cur.selection())
239 if (!ptr_cmp(&cur.inset(), this))
242 // FIXME: hack to get position cache warm
243 pi.pain.setDrawingEnabled(false);
245 pi.pain.setDrawingEnabled(true);
247 CursorSlice s1 = cur.selBegin();
248 CursorSlice s2 = cur.selEnd();
250 //lyxerr << "InsetMathNest::drawing selection: "
251 // << " s1: " << s1 << " s2: " << s2 << endl;
252 if (s1.idx() == s2.idx()) {
253 MathArray const & c = cell(s1.idx());
254 int x1 = c.xo(bv) + c.pos2x(s1.pos());
255 int y1 = c.yo(bv) - c.ascent();
256 int x2 = c.xo(bv) + c.pos2x(s2.pos());
257 int y2 = c.yo(bv) + c.descent();
258 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
259 //lyxerr << "InsetMathNest::drawing selection 3: "
260 // << " x1: " << x1 << " x2: " << x2
261 // << " y1: " << y1 << " y2: " << y2 << endl;
263 for (idx_type i = 0; i < nargs(); ++i) {
264 if (idxBetween(i, s1.idx(), s2.idx())) {
265 MathArray const & c = cell(i);
267 int y1 = c.yo(bv) - c.ascent();
268 int x2 = c.xo(bv) + c.width();
269 int y2 = c.yo(bv) + c.descent();
270 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
277 void InsetMathNest::validate(LaTeXFeatures & features) const
279 for (idx_type i = 0; i < nargs(); ++i)
280 cell(i).validate(features);
284 void InsetMathNest::replace(ReplaceData & rep)
286 for (idx_type i = 0; i < nargs(); ++i)
287 cell(i).replace(rep);
291 bool InsetMathNest::contains(MathArray const & ar) const
293 for (idx_type i = 0; i < nargs(); ++i)
294 if (cell(i).contains(ar))
300 bool InsetMathNest::lock() const
306 void InsetMathNest::lock(bool l)
312 bool InsetMathNest::isActive() const
318 MathArray InsetMathNest::glue() const
321 for (size_t i = 0; i < nargs(); ++i)
327 void InsetMathNest::write(WriteStream & os) const
329 os << '\\' << name().c_str();
330 for (size_t i = 0; i < nargs(); ++i)
331 os << '{' << cell(i) << '}';
333 os.pendingSpace(true);
334 if (lock_ && !os.latex()) {
336 os.pendingSpace(true);
341 void InsetMathNest::normalize(NormalStream & os) const
343 os << '[' << name().c_str();
344 for (size_t i = 0; i < nargs(); ++i)
345 os << ' ' << cell(i);
350 int InsetMathNest::latex(Buffer const &, odocstream & os,
351 OutputParams const & runparams) const
353 WriteStream wi(os, runparams.moving_arg, true);
359 bool InsetMathNest::notifyCursorLeaves(LCursor & /*cur*/)
365 MathArray & ar = cur.cell();
366 // remove base-only "scripts"
367 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
368 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
369 if (p && p->nargs() == 1) {
370 MathArray ar = p->nuc();
373 cur.adjust(i, ar.size() - 1);
377 // glue adjacent font insets of the same kind
378 for (pos_type i = 0; i + 1 < size(); ++i) {
379 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
380 InsetMathFont const * q = operator[](i + 1)->asFontInset();
381 if (p && q && p->name() == q->name()) {
382 p->cell(0).append(q->cell(0));
392 void InsetMathNest::handleFont
393 (LCursor & cur, docstring const & arg, char const * const font)
395 handleFont(cur, arg, from_ascii(font));
399 void InsetMathNest::handleFont
400 (LCursor & cur, docstring const & arg, docstring const & font)
402 // this whole function is a hack and won't work for incremental font
404 recordUndo(cur, Undo::ATOMIC);
406 if (cur.inset().asInsetMath()->name() == font)
407 cur.handleFont(to_utf8(font));
409 cur.handleNest(createInsetMath(font));
415 void InsetMathNest::handleFont2(LCursor & cur, docstring const & arg)
417 recordUndo(cur, Undo::ATOMIC);
420 bv_funcs::string2font(to_utf8(arg), font, b);
421 if (font.color() != LColor::inherit) {
422 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
423 cur.handleNest(at, 0);
428 void InsetMathNest::doDispatch(LCursor & cur, FuncRequest & cmd)
430 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
431 //CursorSlice sl = cur.current();
433 switch (cmd.action) {
437 cur.message(_("Paste"));
438 replaceSelection(cur);
440 istringstream is(to_utf8(cmd.argument()));
442 docstring const selection = cap::getSelection(cur.buffer(), n);
443 cur.niceInsert(selection);
444 cur.clearSelection(); // bug 393
445 cur.bv().switchKeyMap();
452 cutSelection(cur, true, true);
453 cur.message(_("Cut"));
454 // Prevent stale position >= size crash
455 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
462 cur.message(_("Copy"));
465 case LFUN_MOUSE_PRESS:
466 lfunMousePress(cur, cmd);
469 case LFUN_MOUSE_MOTION:
470 lfunMouseMotion(cur, cmd);
473 case LFUN_MOUSE_RELEASE:
474 lfunMouseRelease(cur, cmd);
477 case LFUN_FINISHED_LEFT:
478 cur.bv().cursor() = cur;
481 case LFUN_FINISHED_RIGHT:
483 cur.bv().cursor() = cur;
486 case LFUN_FINISHED_UP:
487 cur.bv().cursor() = cur;
490 case LFUN_FINISHED_DOWN:
492 cur.bv().cursor() = cur;
495 case LFUN_CHAR_FORWARD:
496 // FIXME: we have to enable full redraw here because of the
497 // visual box corners that define the inset. If we know for
498 // sure that we stay within the same cell we can optimize for
500 //cur.updateFlags(Update::FitCursor);
501 case LFUN_CHAR_FORWARD_SELECT:
502 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
503 cur.autocorrect() = false;
505 cur.macroModeClose();
506 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
507 cur.pushLeft(*cur.nextAtom().nucleus());
508 cur.inset().idxFirst(cur);
509 } else if (cur.posRight() || idxRight(cur)
510 || cur.popRight() || cur.selection())
513 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
518 case LFUN_CHAR_BACKWARD:
519 // FIXME: we have to enable full redraw here because of the
520 // visual box corners that define the inset. If we know for
521 // sure that we stay within the same cell we can optimize for
523 //cur.updateFlags(Update::FitCursor);
524 case LFUN_CHAR_BACKWARD_SELECT:
525 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
526 cur.autocorrect() = false;
528 cur.macroModeClose();
529 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
531 cur.push(*cur.nextAtom().nucleus());
532 cur.inset().idxLast(cur);
533 } else if (cur.posLeft() || idxLeft(cur)
534 || cur.popLeft() || cur.selection())
537 cmd = FuncRequest(LFUN_FINISHED_LEFT);
543 // FIXME: we have to enable full redraw here because of the
544 // visual box corners that define the inset. If we know for
545 // sure that we stay within the same cell we can optimize for
547 //cur.updateFlags(Update::FitCursor);
549 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
550 if (cur.inMacroMode()) {
552 cur.macroModeClose();
555 cur.selHandle(cmd.action == LFUN_UP_SELECT);
557 cmd = FuncRequest(LFUN_FINISHED_UP);
560 // fixes bug 1598. Please check!
565 // FIXME: we have to enable full redraw here because of the
566 // visual box corners that define the inset. If we know for
567 // sure that we stay within the same cell we can optimize for
569 //cur.updateFlags(Update::FitCursor);
570 case LFUN_DOWN_SELECT:
571 if (cur.inMacroMode()) {
572 cur.macroModeClose();
575 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
577 cmd = FuncRequest(LFUN_FINISHED_DOWN);
580 // fixes bug 1598. Please check!
584 case LFUN_MOUSE_DOUBLE:
585 case LFUN_MOUSE_TRIPLE:
586 case LFUN_WORD_SELECT:
590 cur.selection() = true;
591 cur.pos() = cur.lastpos();
592 cur.idx() = cur.lastidx();
595 case LFUN_PARAGRAPH_UP:
596 case LFUN_PARAGRAPH_DOWN:
597 // FIXME: we have to enable full redraw here because of the
598 // visual box corners that define the inset. If we know for
599 // sure that we stay within the same cell we can optimize for
601 //cur.updateFlags(Update::FitCursor);
602 case LFUN_PARAGRAPH_UP_SELECT:
603 case LFUN_PARAGRAPH_DOWN_SELECT:
606 case LFUN_LINE_BEGIN:
607 case LFUN_WORD_BACKWARD:
608 // FIXME: we have to enable full redraw here because of the
609 // visual box corners that define the inset. If we know for
610 // sure that we stay within the same cell we can optimize for
612 //cur.updateFlags(Update::FitCursor);
613 case LFUN_LINE_BEGIN_SELECT:
614 case LFUN_WORD_BACKWARD_SELECT:
615 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
616 cmd.action == LFUN_LINE_BEGIN_SELECT);
617 cur.macroModeClose();
618 if (cur.pos() != 0) {
620 } else if (cur.col() != 0) {
621 cur.idx() -= cur.col();
623 } else if (cur.idx() != 0) {
627 cmd = FuncRequest(LFUN_FINISHED_LEFT);
632 case LFUN_WORD_FORWARD:
634 // FIXME: we have to enable full redraw here because of the
635 // visual box corners that define the inset. If we know for
636 // sure that we stay within the same cell we can optimize for
638 //cur.updateFlags(Update::FitCursor);
639 case LFUN_WORD_FORWARD_SELECT:
640 case LFUN_LINE_END_SELECT:
641 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
642 cmd.action == LFUN_LINE_END_SELECT);
643 cur.macroModeClose();
645 if (cur.pos() != cur.lastpos()) {
646 cur.pos() = cur.lastpos();
647 } else if (ncols() && (cur.col() != cur.lastcol())) {
648 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
649 cur.pos() = cur.lastpos();
650 } else if (cur.idx() != cur.lastidx()) {
651 cur.idx() = cur.lastidx();
652 cur.pos() = cur.lastpos();
654 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
659 case LFUN_SCREEN_UP_SELECT:
661 cmd = FuncRequest(LFUN_FINISHED_LEFT);
665 case LFUN_SCREEN_DOWN_SELECT:
666 case LFUN_SCREEN_DOWN:
667 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
671 case LFUN_CELL_FORWARD:
672 // FIXME: we have to enable full redraw here because of the
673 // visual box corners that define the inset. If we know for
674 // sure that we stay within the same cell we can optimize for
676 //cur.updateFlags(Update::FitCursor);
677 cur.inset().idxNext(cur);
680 case LFUN_CELL_BACKWARD:
681 // FIXME: we have to enable full redraw here because of the
682 // visual box corners that define the inset. If we know for
683 // sure that we stay within the same cell we can optimize for
685 //cur.updateFlags(Update::FitCursor);
686 cur.inset().idxPrev(cur);
689 case LFUN_WORD_DELETE_BACKWARD:
690 case LFUN_CHAR_DELETE_BACKWARD:
692 // May affect external cell:
693 recordUndoInset(cur, Undo::ATOMIC);
695 recordUndo(cur, Undo::ATOMIC);
696 // if the inset can not be removed from within, delete it
697 if (!cur.backspace()) {
698 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
699 cur.bv().getLyXText()->dispatch(cur, cmd);
703 case LFUN_WORD_DELETE_FORWARD:
704 case LFUN_CHAR_DELETE_FORWARD:
705 if (cur.pos() == cur.lastpos())
706 // May affect external cell:
707 recordUndoInset(cur, Undo::ATOMIC);
709 recordUndo(cur, Undo::ATOMIC);
710 // if the inset can not be removed from within, delete it
712 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
713 cur.bv().getLyXText()->dispatch(cur, cmd);
719 cur.clearSelection();
721 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
726 case LFUN_INSET_TOGGLE:
732 case LFUN_SELF_INSERT:
733 if (cmd.argument().size() != 1) {
735 docstring const arg = cmd.argument();
736 if (!interpretString(cur, arg))
740 // Don't record undo steps if we are in macro mode and
741 // cmd.argument is the next character of the macro name.
742 // Otherwise we'll get an invalid cursor if we undo after
743 // the macro was finished and the macro is a known command,
744 // e.g. sqrt. LCursor::macroModeClose replaces in this case
745 // the InsetMathUnknown with name "frac" by an empty
746 // InsetMathFrac -> a pos value > 0 is invalid.
747 // A side effect is that an undo before the macro is finished
748 // undoes the complete macro, not only the last character.
749 if (!cur.inMacroMode())
752 // spacial handling of space. If we insert an inset
753 // via macro mode, we want to put the cursor inside it
754 // if relevant. Think typing "\frac<space>".
755 if (cmd.argument()[0] == ' '
756 && cur.inMacroMode() && cur.macroName() != "\\"
757 && cur.macroModeClose()) {
758 MathAtom const atom = cur.prevAtom();
759 if (atom->asNestInset() && atom->nargs() > 0) {
761 cur.pushLeft(*cur.nextInset());
763 } else if (!interpretChar(cur, cmd.argument()[0])) {
764 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
769 //case LFUN_SERVER_GET_XY:
770 // sprintf(dispatch_buffer, "%d %d",);
773 case LFUN_SERVER_SET_XY: {
774 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
777 istringstream is(to_utf8(cmd.argument()));
779 cur.setScreenPos(x, y);
783 // Special casing for superscript in case of LyX handling
785 case LFUN_ACCENT_CIRCUMFLEX:
786 if (cmd.argument().empty()) {
787 // do superscript if LyX handles
789 recordUndo(cur, Undo::ATOMIC);
790 script(cur, true, grabAndEraseSelection(cur));
794 case LFUN_ACCENT_UMLAUT:
795 case LFUN_ACCENT_ACUTE:
796 case LFUN_ACCENT_GRAVE:
797 case LFUN_ACCENT_BREVE:
798 case LFUN_ACCENT_DOT:
799 case LFUN_ACCENT_MACRON:
800 case LFUN_ACCENT_CARON:
801 case LFUN_ACCENT_TILDE:
802 case LFUN_ACCENT_CEDILLA:
803 case LFUN_ACCENT_CIRCLE:
804 case LFUN_ACCENT_UNDERDOT:
805 case LFUN_ACCENT_TIE:
806 case LFUN_ACCENT_OGONEK:
807 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
811 case LFUN_FONT_FREE_APPLY:
812 case LFUN_FONT_FREE_UPDATE:
813 handleFont2(cur, cmd.argument());
817 if (currentMode() == TEXT_MODE)
818 handleFont(cur, cmd.argument(), "textbf");
820 handleFont(cur, cmd.argument(), "mathbf");
823 if (currentMode() == TEXT_MODE)
824 handleFont(cur, cmd.argument(), "textsf");
826 handleFont(cur, cmd.argument(), "mathsf");
829 if (currentMode() == TEXT_MODE)
830 handleFont(cur, cmd.argument(), "emph");
832 handleFont(cur, cmd.argument(), "mathcal");
834 case LFUN_FONT_ROMAN:
835 if (currentMode() == TEXT_MODE)
836 handleFont(cur, cmd.argument(), "textrm");
838 handleFont(cur, cmd.argument(), "mathrm");
841 if (currentMode() == TEXT_MODE)
842 handleFont(cur, cmd.argument(), "texttt");
844 handleFont(cur, cmd.argument(), "mathtt");
847 handleFont(cur, cmd.argument(), "mathfrak");
850 if (currentMode() == TEXT_MODE)
851 handleFont(cur, cmd.argument(), "textit");
853 handleFont(cur, cmd.argument(), "mathit");
856 if (currentMode() == TEXT_MODE)
857 // FIXME: should be "noun"
858 handleFont(cur, cmd.argument(), "textsc");
860 handleFont(cur, cmd.argument(), "mathbb");
862 //case LFUN_FONT_FREE_APPLY:
863 handleFont(cur, cmd.argument(), "textrm");
865 case LFUN_FONT_DEFAULT:
866 handleFont(cur, cmd.argument(), "textnormal");
869 case LFUN_MATH_MODE: {
871 // ignore math-mode on when already in math mode
872 if (currentMode() == InsetBase::MATH_MODE && cmd.argument() == "on")
874 cur.macroModeClose();
875 docstring const save_selection = grabAndEraseSelection(cur);
877 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
878 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
880 cur.pushLeft(*cur.nextInset());
881 cur.niceInsert(save_selection);
883 if (currentMode() == InsetBase::TEXT_MODE) {
884 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
885 cur.message(_("create new math text environment ($...$)"));
887 handleFont(cur, cmd.argument(), "textrm");
888 cur.message(_("entered math text mode (textrm)"));
901 case LFUN_MATH_MATRIX: {
902 recordUndo(cur, Undo::ATOMIC);
907 idocstringstream is(cmd.argument());
908 is >> m >> n >> v_align >> h_align;
915 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
919 case LFUN_MATH_DELIM: {
921 docstring rs = support::split(cmd.argument(), ls, ' ');
922 // Reasonable default values
927 recordUndo(cur, Undo::ATOMIC);
928 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
932 case LFUN_MATH_BIGDELIM: {
933 docstring const lname = from_utf8(cmd.getArg(0));
934 docstring const ldelim = from_utf8(cmd.getArg(1));
935 docstring const rname = from_utf8(cmd.getArg(2));
936 docstring const rdelim = from_utf8(cmd.getArg(3));
937 latexkeys const * l = in_word_set(lname);
938 bool const have_l = l && l->inset == "big" &&
939 InsetMathBig::isBigInsetDelim(ldelim);
940 l = in_word_set(rname);
941 bool const have_r = l && l->inset == "big" &&
942 InsetMathBig::isBigInsetDelim(rdelim);
943 // We mimic LFUN_MATH_DELIM in case we have an empty left
944 // or right delimiter.
945 if (have_l || have_r) {
946 recordUndo(cur, Undo::ATOMIC);
947 docstring const selection = grabAndEraseSelection(cur);
950 cur.insert(MathAtom(new InsetMathBig(lname,
952 cur.niceInsert(selection);
954 cur.insert(MathAtom(new InsetMathBig(rname,
957 // Don't call cur.undispatched() if we did nothing, this would
958 // lead to infinite recursion via LyXText::dispatch().
962 case LFUN_SPACE_INSERT:
963 case LFUN_MATH_SPACE:
964 recordUndo(cur, Undo::ATOMIC);
965 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
968 case LFUN_ERT_INSERT:
969 // interpret this as if a backslash was typed
970 recordUndo(cur, Undo::ATOMIC);
971 interpretChar(cur, '\\');
974 case LFUN_MATH_SUBSCRIPT:
975 // interpret this as if a _ was typed
976 recordUndo(cur, Undo::ATOMIC);
977 interpretChar(cur, '_');
980 case LFUN_MATH_SUPERSCRIPT:
981 // interpret this as if a ^ was typed
982 recordUndo(cur, Undo::ATOMIC);
983 interpretChar(cur, '^');
986 case LFUN_QUOTE_INSERT:
987 // interpret this as if a straight " was typed
988 recordUndo(cur, Undo::ATOMIC);
989 interpretChar(cur, '\"');
992 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
993 // handling such that "self-insert" works on "arbitrary stuff" too, and
994 // math-insert only handles special math things like "matrix".
995 case LFUN_MATH_INSERT: {
996 recordUndo(cur, Undo::ATOMIC);
997 if (cmd.argument() == "^" || cmd.argument() == "_") {
998 interpretChar(cur, cmd.argument()[0]);
1000 cur.niceInsert(cmd.argument());
1004 case LFUN_DIALOG_SHOW_NEW_INSET: {
1005 docstring const & name = cmd.argument();
1007 if (name == "ref") {
1009 data = tmp.createDialogStr(to_utf8(name));
1011 cur.bv().showInsetDialog(to_utf8(name), data, 0);
1015 case LFUN_INSET_INSERT: {
1017 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1026 InsetMathDim::doDispatch(cur, cmd);
1032 bool InsetMathNest::getStatus(LCursor & cur, FuncRequest const & cmd,
1033 FuncStatus & flag) const
1035 // the font related toggles
1036 //string tc = "mathnormal";
1038 string const arg = to_utf8(cmd.argument());
1039 switch (cmd.action) {
1040 case LFUN_TABULAR_FEATURE:
1041 flag.enabled(false);
1044 case LFUN_TABULAR_FEATURE:
1045 // FIXME: check temporarily disabled
1047 char align = mathcursor::valign();
1048 if (align == '\0') {
1052 if (cmd.argument().empty()) {
1056 if (!contains("tcb", cmd.argument()[0])) {
1060 flag.setOnOff(cmd.argument()[0] == align);
1063 /// We have to handle them since 1.4 blocks all unhandled actions
1064 case LFUN_FONT_ITAL:
1065 case LFUN_FONT_BOLD:
1066 case LFUN_FONT_SANS:
1067 case LFUN_FONT_EMPH:
1068 case LFUN_FONT_CODE:
1069 case LFUN_FONT_NOUN:
1070 case LFUN_FONT_ROMAN:
1071 case LFUN_FONT_DEFAULT:
1074 case LFUN_MATH_MUTATE:
1075 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1076 flag.setOnOff(false);
1079 // we just need to be in math mode to enable that
1080 case LFUN_MATH_SIZE:
1081 case LFUN_MATH_SPACE:
1082 case LFUN_MATH_LIMITS:
1083 case LFUN_MATH_NONUMBER:
1084 case LFUN_MATH_NUMBER:
1085 case LFUN_MATH_EXTERN:
1089 case LFUN_FONT_FRAK:
1090 flag.enabled(currentMode() != TEXT_MODE);
1093 case LFUN_MATH_INSERT: {
1094 bool const textarg =
1095 arg == "\\textbf" || arg == "\\textsf" ||
1096 arg == "\\textrm" || arg == "\\textmd" ||
1097 arg == "\\textit" || arg == "\\textsc" ||
1098 arg == "\\textsl" || arg == "\\textup" ||
1099 arg == "\\texttt" || arg == "\\textbb" ||
1100 arg == "\\textnormal";
1101 flag.enabled(currentMode() != TEXT_MODE || textarg);
1105 case LFUN_MATH_MATRIX:
1106 flag.enabled(currentMode() == MATH_MODE);
1109 case LFUN_INSET_INSERT: {
1110 // Don't test createMathInset_fromDialogStr(), since
1111 // getStatus is not called with a valid reference and the
1112 // dialog would not be applyable.
1113 string const name = cmd.getArg(0);
1114 flag.enabled(name == "ref");
1118 case LFUN_MATH_DELIM:
1119 case LFUN_MATH_BIGDELIM:
1120 // Don't do this with multi-cell selections
1121 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1132 void InsetMathNest::edit(LCursor & 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 InsetBase * InsetMathNest::editXY(LCursor & 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 MathArray & 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(LCursor & cur, FuncRequest & cmd)
1171 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1172 BufferView & bv = cur.bv();
1173 if (cmd.button() == mouse_button::button1) {
1174 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1175 bv.mouseSetCursor(cur);
1176 // FIXME: we have to enable full redraw here because of the
1177 // visual box corners that define the inset.
1179 } else if (cmd.button() == mouse_button::button2) {
1181 if (cur.selection())
1182 asArray(bv.cursor().selectionAsString(false), ar);
1184 asArray(theSelection().get(), ar);
1187 bv.mouseSetCursor(cur);
1192 void InsetMathNest::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
1194 // only select with button 1
1195 if (cmd.button() == mouse_button::button1) {
1196 LCursor & bvcur = cur.bv().cursor();
1197 if (bvcur.anchor_.hasPart(cur)) {
1198 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1199 bvcur.setCursor(cur);
1200 bvcur.selection() = true;
1201 //lyxerr << "MOTION " << bvcur << endl;
1210 void InsetMathNest::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
1212 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1214 if (cmd.button() == mouse_button::button1) {
1215 //theSelection().put(cur.grabSelection());
1216 if (!cur.selection())
1221 if (cmd.button() == mouse_button::button3) {
1222 // try to dispatch to enclosed insets first
1223 cur.bv().showDialog("mathpanel");
1231 bool InsetMathNest::interpretChar(LCursor & cur, char_type c)
1233 //lyxerr << "interpret 2: '" << c << "'" << endl;
1234 docstring save_selection;
1235 if (c == '^' || c == '_')
1236 save_selection = grabAndEraseSelection(cur);
1241 if (cur.inMacroMode()) {
1242 docstring name = cur.macroName();
1244 /// are we currently typing '#1' or '#2' or...?
1245 if (name == "\\#") {
1248 if (n >= 1 && n <= 9)
1249 cur.insert(new MathMacroArgument(n));
1254 cur.activeMacro()->setName(name + docstring(1, c));
1258 // handle 'special char' macros
1263 if (currentMode() == InsetMath::TEXT_MODE)
1264 cur.niceInsert(createInsetMath("textbackslash"));
1266 cur.niceInsert(createInsetMath("backslash"));
1267 } else if (c == '{') {
1269 cur.niceInsert(MathAtom(new InsetMathBrace));
1270 } else if (c == '%') {
1272 cur.niceInsert(MathAtom(new InsetMathComment));
1273 } else if (c == '#') {
1274 BOOST_ASSERT(cur.activeMacro());
1275 cur.activeMacro()->setName(name + docstring(1, c));
1278 cur.niceInsert(createInsetMath(docstring(1, c)));
1283 // One character big delimiters. The others are handled in
1284 // interpretString().
1285 latexkeys const * l = in_word_set(name.substr(1));
1286 if (name[0] == '\\' && l && l->inset == "big") {
1290 delim = from_ascii("\\{");
1293 delim = from_ascii("\\}");
1296 delim = docstring(1, c);
1299 if (InsetMathBig::isBigInsetDelim(delim)) {
1300 // name + delim ared a valid InsetMathBig.
1301 // We can't use cur.macroModeClose() because
1302 // it does not handle delim.
1303 InsetMathUnknown * p = cur.activeMacro();
1306 cur.cell().erase(cur.pos());
1307 cur.plainInsert(MathAtom(
1308 new InsetMathBig(name.substr(1), delim)));
1313 // leave macro mode and try again if necessary
1314 cur.macroModeClose();
1316 cur.niceInsert(MathAtom(new InsetMathBrace));
1318 interpretChar(cur, c);
1322 // This is annoying as one has to press <space> far too often.
1326 // leave autocorrect mode if necessary
1327 if (autocorrect() && c == ' ') {
1328 autocorrect() = false;
1333 // just clear selection on pressing the space bar
1334 if (cur.selection() && c == ' ') {
1335 cur.selection() = false;
1342 //lyxerr << "starting with macro" << endl;
1343 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), false)));
1348 if (currentMode() == InsetMath::TEXT_MODE)
1354 if (currentMode() == InsetMath::TEXT_MODE) {
1355 // insert spaces in text mode,
1356 // but suppress direct insertion of two spaces in a row
1357 // the still allows typing '<space>a<space>' and deleting the 'a', but
1358 // it is better than nothing...
1359 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1361 // FIXME: we have to enable full redraw here because of the
1362 // visual box corners that define the inset. If we know for
1363 // sure that we stay within the same cell we can optimize for
1365 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1369 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1370 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1371 // FIXME: we have to enable full redraw here because of the
1372 // visual box corners that define the inset. If we know for
1373 // sure that we stay within the same cell we can optimize for
1375 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1379 if (cur.popRight()) {
1380 // FIXME: we have to enable full redraw here because of the
1381 // visual box corners that define the inset. If we know for
1382 // sure that we stay within the same cell we can optimize for
1384 //cur.updateFlags(Update::FitCursor);
1388 // if we are at the very end, leave the formula
1389 return cur.pos() != cur.lastpos();
1392 // These shouldn't work in text mode:
1393 if (currentMode() != InsetMath::TEXT_MODE) {
1395 script(cur, false, save_selection);
1399 script(cur, true, save_selection);
1403 cur.niceInsert(createInsetMath("sim"));
1408 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1409 c == '%' || c == '_' || c == '^') {
1410 cur.niceInsert(createInsetMath(docstring(1, c)));
1415 // try auto-correction
1416 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1419 // no special circumstances, so insert the character without any fuss
1421 cur.autocorrect() = true;
1426 bool InsetMathNest::interpretString(LCursor & cur, docstring const & str)
1428 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1430 if (!cur.empty() && cur.pos() > 0 &&
1431 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1432 if (InsetMathBig::isBigInsetDelim(str)) {
1433 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1434 if (prev[0] == '\\') {
1435 prev = prev.substr(1);
1436 latexkeys const * l = in_word_set(prev);
1437 if (l && l->inset == "big") {
1438 cur.cell()[cur.pos() - 1] =
1439 MathAtom(new InsetMathBig(prev, str));
1449 bool InsetMathNest::script(LCursor & cur, bool up,
1450 docstring const & save_selection)
1452 // Hack to get \^ and \_ working
1453 //lyxerr << "handling script: up: " << up << endl;
1454 if (cur.inMacroMode() && cur.macroName() == "\\") {
1456 cur.niceInsert(createInsetMath("mathcircumflex"));
1458 interpretChar(cur, '_');
1462 cur.macroModeClose();
1463 if (asScriptInset() && cur.idx() == 0) {
1464 // we are in a nucleus of a script inset, move to _our_ script
1465 InsetMathScript * inset = asScriptInset();
1466 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1468 cur.idx() = inset->idxOfScript(up);
1470 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1472 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1475 cur.idx() = inset->idxOfScript(up);
1476 cur.pos() = cur.lastpos();
1478 // convert the thing to our left to a scriptinset or create a new
1479 // one if in the very first position of the array
1480 if (cur.pos() == 0) {
1481 //lyxerr << "new scriptinset" << endl;
1482 cur.insert(new InsetMathScript(up));
1484 //lyxerr << "converting prev atom " << endl;
1485 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1488 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1489 // See comment in MathParser.C for special handling of {}-bases
1495 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1496 cur.niceInsert(save_selection);
1498 //lyxerr << "inserting selection 2:\n" << save_selection << endl;