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
405 if (cur.inset().asInsetMath()->name() == font) {
406 recordUndoInset(cur, Undo::ATOMIC);
407 cur.handleFont(to_utf8(font));
409 recordUndo(cur, Undo::ATOMIC);
410 cur.handleNest(createInsetMath(font));
416 void InsetMathNest::handleFont2(LCursor & cur, docstring const & arg)
418 recordUndo(cur, Undo::ATOMIC);
421 bv_funcs::string2font(to_utf8(arg), font, b);
422 if (font.color() != LColor::inherit) {
423 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
424 cur.handleNest(at, 0);
429 void InsetMathNest::doDispatch(LCursor & cur, FuncRequest & cmd)
431 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
432 //CursorSlice sl = cur.current();
434 switch (cmd.action) {
438 cur.message(_("Paste"));
439 replaceSelection(cur);
441 istringstream is(to_utf8(cmd.argument()));
443 docstring const selection = cap::getSelection(cur.buffer(), n);
444 cur.niceInsert(selection);
445 cur.clearSelection(); // bug 393
446 cur.bv().switchKeyMap();
453 cutSelection(cur, true, true);
454 cur.message(_("Cut"));
455 // Prevent stale position >= size crash
456 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
463 cur.message(_("Copy"));
466 case LFUN_MOUSE_PRESS:
467 lfunMousePress(cur, cmd);
470 case LFUN_MOUSE_MOTION:
471 lfunMouseMotion(cur, cmd);
474 case LFUN_MOUSE_RELEASE:
475 lfunMouseRelease(cur, cmd);
478 case LFUN_FINISHED_LEFT:
479 cur.bv().cursor() = cur;
482 case LFUN_FINISHED_RIGHT:
484 cur.bv().cursor() = cur;
487 case LFUN_FINISHED_UP:
488 cur.bv().cursor() = cur;
491 case LFUN_FINISHED_DOWN:
493 cur.bv().cursor() = cur;
496 case LFUN_CHAR_FORWARD:
497 // FIXME: we have to enable full redraw here because of the
498 // visual box corners that define the inset. If we know for
499 // sure that we stay within the same cell we can optimize for
501 //cur.updateFlags(Update::FitCursor);
502 case LFUN_CHAR_FORWARD_SELECT:
503 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
504 cur.autocorrect() = false;
506 cur.macroModeClose();
507 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
508 cur.pushLeft(*cur.nextAtom().nucleus());
509 cur.inset().idxFirst(cur);
510 } else if (cur.posRight() || idxRight(cur)
511 || cur.popRight() || cur.selection())
514 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
519 case LFUN_CHAR_BACKWARD:
520 // FIXME: we have to enable full redraw here because of the
521 // visual box corners that define the inset. If we know for
522 // sure that we stay within the same cell we can optimize for
524 //cur.updateFlags(Update::FitCursor);
525 case LFUN_CHAR_BACKWARD_SELECT:
526 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
527 cur.autocorrect() = false;
529 cur.macroModeClose();
530 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
532 cur.push(*cur.nextAtom().nucleus());
533 cur.inset().idxLast(cur);
534 } else if (cur.posLeft() || idxLeft(cur)
535 || cur.popLeft() || cur.selection())
538 cmd = FuncRequest(LFUN_FINISHED_LEFT);
544 // FIXME: we have to enable full redraw here because of the
545 // visual box corners that define the inset. If we know for
546 // sure that we stay within the same cell we can optimize for
548 //cur.updateFlags(Update::FitCursor);
550 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
551 if (cur.inMacroMode()) {
553 cur.macroModeClose();
556 cur.selHandle(cmd.action == LFUN_UP_SELECT);
558 cmd = FuncRequest(LFUN_FINISHED_UP);
561 // fixes bug 1598. Please check!
566 // FIXME: we have to enable full redraw here because of the
567 // visual box corners that define the inset. If we know for
568 // sure that we stay within the same cell we can optimize for
570 //cur.updateFlags(Update::FitCursor);
571 case LFUN_DOWN_SELECT:
572 if (cur.inMacroMode()) {
573 cur.macroModeClose();
576 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
578 cmd = FuncRequest(LFUN_FINISHED_DOWN);
581 // fixes bug 1598. Please check!
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 // FIXME: we have to enable full redraw here because of the
599 // visual box corners that define the inset. If we know for
600 // sure that we stay within the same cell we can optimize for
602 //cur.updateFlags(Update::FitCursor);
603 case LFUN_PARAGRAPH_UP_SELECT:
604 case LFUN_PARAGRAPH_DOWN_SELECT:
607 case LFUN_LINE_BEGIN:
608 case LFUN_WORD_BACKWARD:
609 // FIXME: we have to enable full redraw here because of the
610 // visual box corners that define the inset. If we know for
611 // sure that we stay within the same cell we can optimize for
613 //cur.updateFlags(Update::FitCursor);
614 case LFUN_LINE_BEGIN_SELECT:
615 case LFUN_WORD_BACKWARD_SELECT:
616 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
617 cmd.action == LFUN_LINE_BEGIN_SELECT);
618 cur.macroModeClose();
619 if (cur.pos() != 0) {
621 } else if (cur.col() != 0) {
622 cur.idx() -= cur.col();
624 } else if (cur.idx() != 0) {
628 cmd = FuncRequest(LFUN_FINISHED_LEFT);
633 case LFUN_WORD_FORWARD:
635 // FIXME: we have to enable full redraw here because of the
636 // visual box corners that define the inset. If we know for
637 // sure that we stay within the same cell we can optimize for
639 //cur.updateFlags(Update::FitCursor);
640 case LFUN_WORD_FORWARD_SELECT:
641 case LFUN_LINE_END_SELECT:
642 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
643 cmd.action == LFUN_LINE_END_SELECT);
644 cur.macroModeClose();
646 if (cur.pos() != cur.lastpos()) {
647 cur.pos() = cur.lastpos();
648 } else if (ncols() && (cur.col() != cur.lastcol())) {
649 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
650 cur.pos() = cur.lastpos();
651 } else if (cur.idx() != cur.lastidx()) {
652 cur.idx() = cur.lastidx();
653 cur.pos() = cur.lastpos();
655 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
660 case LFUN_SCREEN_UP_SELECT:
662 cmd = FuncRequest(LFUN_FINISHED_LEFT);
666 case LFUN_SCREEN_DOWN_SELECT:
667 case LFUN_SCREEN_DOWN:
668 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
672 case LFUN_CELL_FORWARD:
673 // FIXME: we have to enable full redraw here because of the
674 // visual box corners that define the inset. If we know for
675 // sure that we stay within the same cell we can optimize for
677 //cur.updateFlags(Update::FitCursor);
678 cur.inset().idxNext(cur);
681 case LFUN_CELL_BACKWARD:
682 // FIXME: we have to enable full redraw here because of the
683 // visual box corners that define the inset. If we know for
684 // sure that we stay within the same cell we can optimize for
686 //cur.updateFlags(Update::FitCursor);
687 cur.inset().idxPrev(cur);
690 case LFUN_WORD_DELETE_BACKWARD:
691 case LFUN_CHAR_DELETE_BACKWARD:
693 // May affect external cell:
694 recordUndoInset(cur, Undo::ATOMIC);
696 recordUndo(cur, Undo::ATOMIC);
697 // if the inset can not be removed from within, delete it
698 if (!cur.backspace()) {
699 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
700 cur.bv().getLyXText()->dispatch(cur, cmd);
704 case LFUN_WORD_DELETE_FORWARD:
705 case LFUN_CHAR_DELETE_FORWARD:
706 if (cur.pos() == cur.lastpos())
707 // May affect external cell:
708 recordUndoInset(cur, Undo::ATOMIC);
710 recordUndo(cur, Undo::ATOMIC);
711 // if the inset can not be removed from within, delete it
713 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
714 cur.bv().getLyXText()->dispatch(cur, cmd);
720 cur.clearSelection();
722 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
727 case LFUN_INSET_TOGGLE:
733 case LFUN_SELF_INSERT:
734 if (cmd.argument().size() != 1) {
736 docstring const arg = cmd.argument();
737 if (!interpretString(cur, arg))
741 // Don't record undo steps if we are in macro mode and
742 // cmd.argument is the next character of the macro name.
743 // Otherwise we'll get an invalid cursor if we undo after
744 // the macro was finished and the macro is a known command,
745 // e.g. sqrt. LCursor::macroModeClose replaces in this case
746 // the InsetMathUnknown with name "frac" by an empty
747 // InsetMathFrac -> a pos value > 0 is invalid.
748 // A side effect is that an undo before the macro is finished
749 // undoes the complete macro, not only the last character.
750 if (!cur.inMacroMode())
753 // spacial handling of space. If we insert an inset
754 // via macro mode, we want to put the cursor inside it
755 // if relevant. Think typing "\frac<space>".
756 if (cmd.argument()[0] == ' '
757 && cur.inMacroMode() && cur.macroName() != "\\"
758 && cur.macroModeClose()) {
759 MathAtom const atom = cur.prevAtom();
760 if (atom->asNestInset() && atom->nargs() > 0) {
762 cur.pushLeft(*cur.nextInset());
764 } else if (!interpretChar(cur, cmd.argument()[0])) {
765 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
770 //case LFUN_SERVER_GET_XY:
771 // sprintf(dispatch_buffer, "%d %d",);
774 case LFUN_SERVER_SET_XY: {
775 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
778 istringstream is(to_utf8(cmd.argument()));
780 cur.setScreenPos(x, y);
784 // Special casing for superscript in case of LyX handling
786 case LFUN_ACCENT_CIRCUMFLEX:
787 if (cmd.argument().empty()) {
788 // do superscript if LyX handles
790 recordUndo(cur, Undo::ATOMIC);
791 script(cur, true, grabAndEraseSelection(cur));
795 case LFUN_ACCENT_UMLAUT:
796 case LFUN_ACCENT_ACUTE:
797 case LFUN_ACCENT_GRAVE:
798 case LFUN_ACCENT_BREVE:
799 case LFUN_ACCENT_DOT:
800 case LFUN_ACCENT_MACRON:
801 case LFUN_ACCENT_CARON:
802 case LFUN_ACCENT_TILDE:
803 case LFUN_ACCENT_CEDILLA:
804 case LFUN_ACCENT_CIRCLE:
805 case LFUN_ACCENT_UNDERDOT:
806 case LFUN_ACCENT_TIE:
807 case LFUN_ACCENT_OGONEK:
808 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
812 case LFUN_FONT_FREE_APPLY:
813 case LFUN_FONT_FREE_UPDATE:
814 handleFont2(cur, cmd.argument());
818 if (currentMode() == TEXT_MODE)
819 handleFont(cur, cmd.argument(), "textbf");
821 handleFont(cur, cmd.argument(), "mathbf");
824 if (currentMode() == TEXT_MODE)
825 handleFont(cur, cmd.argument(), "textsf");
827 handleFont(cur, cmd.argument(), "mathsf");
830 if (currentMode() == TEXT_MODE)
831 handleFont(cur, cmd.argument(), "emph");
833 handleFont(cur, cmd.argument(), "mathcal");
835 case LFUN_FONT_ROMAN:
836 if (currentMode() == TEXT_MODE)
837 handleFont(cur, cmd.argument(), "textrm");
839 handleFont(cur, cmd.argument(), "mathrm");
842 if (currentMode() == TEXT_MODE)
843 handleFont(cur, cmd.argument(), "texttt");
845 handleFont(cur, cmd.argument(), "mathtt");
848 handleFont(cur, cmd.argument(), "mathfrak");
851 if (currentMode() == TEXT_MODE)
852 handleFont(cur, cmd.argument(), "textit");
854 handleFont(cur, cmd.argument(), "mathit");
857 if (currentMode() == TEXT_MODE)
858 // FIXME: should be "noun"
859 handleFont(cur, cmd.argument(), "textsc");
861 handleFont(cur, cmd.argument(), "mathbb");
863 //case LFUN_FONT_FREE_APPLY:
864 handleFont(cur, cmd.argument(), "textrm");
866 case LFUN_FONT_DEFAULT:
867 handleFont(cur, cmd.argument(), "textnormal");
870 case LFUN_MATH_MODE: {
872 // ignore math-mode on when already in math mode
873 if (currentMode() == InsetBase::MATH_MODE && cmd.argument() == "on")
875 cur.macroModeClose();
876 docstring const save_selection = grabAndEraseSelection(cur);
878 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
879 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
881 cur.pushLeft(*cur.nextInset());
882 cur.niceInsert(save_selection);
884 if (currentMode() == InsetBase::TEXT_MODE) {
885 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
886 cur.message(_("create new math text environment ($...$)"));
888 handleFont(cur, cmd.argument(), "textrm");
889 cur.message(_("entered math text mode (textrm)"));
902 case LFUN_MATH_MATRIX: {
903 recordUndo(cur, Undo::ATOMIC);
908 idocstringstream is(cmd.argument());
909 is >> m >> n >> v_align >> h_align;
916 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
920 case LFUN_MATH_DELIM: {
922 docstring rs = support::split(cmd.argument(), ls, ' ');
923 // Reasonable default values
928 recordUndo(cur, Undo::ATOMIC);
929 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
933 case LFUN_MATH_BIGDELIM: {
934 docstring const lname = from_utf8(cmd.getArg(0));
935 docstring const ldelim = from_utf8(cmd.getArg(1));
936 docstring const rname = from_utf8(cmd.getArg(2));
937 docstring const rdelim = from_utf8(cmd.getArg(3));
938 latexkeys const * l = in_word_set(lname);
939 bool const have_l = l && l->inset == "big" &&
940 InsetMathBig::isBigInsetDelim(ldelim);
941 l = in_word_set(rname);
942 bool const have_r = l && l->inset == "big" &&
943 InsetMathBig::isBigInsetDelim(rdelim);
944 // We mimic LFUN_MATH_DELIM in case we have an empty left
945 // or right delimiter.
946 if (have_l || have_r) {
947 recordUndo(cur, Undo::ATOMIC);
948 docstring const selection = grabAndEraseSelection(cur);
951 cur.insert(MathAtom(new InsetMathBig(lname,
953 cur.niceInsert(selection);
955 cur.insert(MathAtom(new InsetMathBig(rname,
958 // Don't call cur.undispatched() if we did nothing, this would
959 // lead to infinite recursion via LyXText::dispatch().
963 case LFUN_SPACE_INSERT:
964 case LFUN_MATH_SPACE:
965 recordUndo(cur, Undo::ATOMIC);
966 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
969 case LFUN_ERT_INSERT:
970 // interpret this as if a backslash was typed
971 recordUndo(cur, Undo::ATOMIC);
972 interpretChar(cur, '\\');
975 case LFUN_MATH_SUBSCRIPT:
976 // interpret this as if a _ was typed
977 recordUndo(cur, Undo::ATOMIC);
978 interpretChar(cur, '_');
981 case LFUN_MATH_SUPERSCRIPT:
982 // interpret this as if a ^ was typed
983 recordUndo(cur, Undo::ATOMIC);
984 interpretChar(cur, '^');
987 case LFUN_QUOTE_INSERT:
988 // interpret this as if a straight " was typed
989 recordUndo(cur, Undo::ATOMIC);
990 interpretChar(cur, '\"');
993 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
994 // handling such that "self-insert" works on "arbitrary stuff" too, and
995 // math-insert only handles special math things like "matrix".
996 case LFUN_MATH_INSERT: {
997 recordUndo(cur, Undo::ATOMIC);
998 if (cmd.argument() == "^" || cmd.argument() == "_") {
999 interpretChar(cur, cmd.argument()[0]);
1001 cur.niceInsert(cmd.argument());
1005 case LFUN_DIALOG_SHOW_NEW_INSET: {
1006 docstring const & name = cmd.argument();
1008 if (name == "ref") {
1010 data = tmp.createDialogStr(to_utf8(name));
1012 cur.bv().showInsetDialog(to_utf8(name), data, 0);
1016 case LFUN_INSET_INSERT: {
1018 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1027 InsetMathDim::doDispatch(cur, cmd);
1033 bool InsetMathNest::getStatus(LCursor & cur, FuncRequest const & cmd,
1034 FuncStatus & flag) const
1036 // the font related toggles
1037 //string tc = "mathnormal";
1039 string const arg = to_utf8(cmd.argument());
1040 switch (cmd.action) {
1041 case LFUN_TABULAR_FEATURE:
1042 flag.enabled(false);
1045 case LFUN_TABULAR_FEATURE:
1046 // FIXME: check temporarily disabled
1048 char align = mathcursor::valign();
1049 if (align == '\0') {
1053 if (cmd.argument().empty()) {
1057 if (!contains("tcb", cmd.argument()[0])) {
1061 flag.setOnOff(cmd.argument()[0] == align);
1064 /// We have to handle them since 1.4 blocks all unhandled actions
1065 case LFUN_FONT_ITAL:
1066 case LFUN_FONT_BOLD:
1067 case LFUN_FONT_SANS:
1068 case LFUN_FONT_EMPH:
1069 case LFUN_FONT_CODE:
1070 case LFUN_FONT_NOUN:
1071 case LFUN_FONT_ROMAN:
1072 case LFUN_FONT_DEFAULT:
1075 case LFUN_MATH_MUTATE:
1076 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1077 flag.setOnOff(false);
1080 // we just need to be in math mode to enable that
1081 case LFUN_MATH_SIZE:
1082 case LFUN_MATH_SPACE:
1083 case LFUN_MATH_LIMITS:
1084 case LFUN_MATH_NONUMBER:
1085 case LFUN_MATH_NUMBER:
1086 case LFUN_MATH_EXTERN:
1090 case LFUN_FONT_FRAK:
1091 flag.enabled(currentMode() != TEXT_MODE);
1094 case LFUN_MATH_INSERT: {
1095 bool const textarg =
1096 arg == "\\textbf" || arg == "\\textsf" ||
1097 arg == "\\textrm" || arg == "\\textmd" ||
1098 arg == "\\textit" || arg == "\\textsc" ||
1099 arg == "\\textsl" || arg == "\\textup" ||
1100 arg == "\\texttt" || arg == "\\textbb" ||
1101 arg == "\\textnormal";
1102 flag.enabled(currentMode() != TEXT_MODE || textarg);
1106 case LFUN_MATH_MATRIX:
1107 flag.enabled(currentMode() == MATH_MODE);
1110 case LFUN_INSET_INSERT: {
1111 // Don't test createMathInset_fromDialogStr(), since
1112 // getStatus is not called with a valid reference and the
1113 // dialog would not be applyable.
1114 string const name = cmd.getArg(0);
1115 flag.enabled(name == "ref");
1119 case LFUN_MATH_DELIM:
1120 case LFUN_MATH_BIGDELIM:
1121 // Don't do this with multi-cell selections
1122 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1133 void InsetMathNest::edit(LCursor & cur, bool left)
1136 cur.idx() = left ? 0 : cur.lastidx();
1137 cur.pos() = left ? 0 : cur.lastpos();
1139 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1143 InsetBase * InsetMathNest::editXY(LCursor & cur, int x, int y)
1146 int dist_min = 1000000;
1147 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1148 int const d = cell(i).dist(cur.bv(), x, y);
1154 MathArray & ar = cell(idx_min);
1156 cur.idx() = idx_min;
1157 cur.pos() = ar.x2pos(x - ar.xo(cur.bv()));
1159 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1160 if (dist_min == 0) {
1162 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1163 if (ar[i]->covers(cur.bv(), x, y))
1164 return ar[i].nucleus()->editXY(cur, x, y);
1170 void InsetMathNest::lfunMousePress(LCursor & cur, FuncRequest & cmd)
1172 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1173 BufferView & bv = cur.bv();
1174 if (cmd.button() == mouse_button::button1) {
1175 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1176 bv.mouseSetCursor(cur);
1177 // FIXME: we have to enable full redraw here because of the
1178 // visual box corners that define the inset.
1180 } else if (cmd.button() == mouse_button::button2) {
1182 if (cur.selection())
1183 asArray(bv.cursor().selectionAsString(false), ar);
1185 asArray(theSelection().get(), ar);
1188 bv.mouseSetCursor(cur);
1193 void InsetMathNest::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
1195 // only select with button 1
1196 if (cmd.button() == mouse_button::button1) {
1197 LCursor & bvcur = cur.bv().cursor();
1198 if (bvcur.anchor_.hasPart(cur)) {
1199 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1200 bvcur.setCursor(cur);
1201 bvcur.selection() = true;
1202 //lyxerr << "MOTION " << bvcur << endl;
1211 void InsetMathNest::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
1213 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1215 if (cmd.button() == mouse_button::button1) {
1216 //theSelection().put(cur.grabSelection());
1217 if (!cur.selection())
1222 if (cmd.button() == mouse_button::button3) {
1223 // try to dispatch to enclosed insets first
1224 cur.bv().showDialog("mathpanel");
1232 bool InsetMathNest::interpretChar(LCursor & cur, char_type c)
1234 //lyxerr << "interpret 2: '" << c << "'" << endl;
1235 docstring save_selection;
1236 if (c == '^' || c == '_')
1237 save_selection = grabAndEraseSelection(cur);
1242 if (cur.inMacroMode()) {
1243 docstring name = cur.macroName();
1245 /// are we currently typing '#1' or '#2' or...?
1246 if (name == "\\#") {
1249 if (n >= 1 && n <= 9)
1250 cur.insert(new MathMacroArgument(n));
1255 cur.activeMacro()->setName(name + docstring(1, c));
1259 // handle 'special char' macros
1264 if (currentMode() == InsetMath::TEXT_MODE)
1265 cur.niceInsert(createInsetMath("textbackslash"));
1267 cur.niceInsert(createInsetMath("backslash"));
1268 } else if (c == '{') {
1270 cur.niceInsert(MathAtom(new InsetMathBrace));
1271 } else if (c == '%') {
1273 cur.niceInsert(MathAtom(new InsetMathComment));
1274 } else if (c == '#') {
1275 BOOST_ASSERT(cur.activeMacro());
1276 cur.activeMacro()->setName(name + docstring(1, c));
1279 cur.niceInsert(createInsetMath(docstring(1, c)));
1284 // One character big delimiters. The others are handled in
1285 // interpretString().
1286 latexkeys const * l = in_word_set(name.substr(1));
1287 if (name[0] == '\\' && l && l->inset == "big") {
1291 delim = from_ascii("\\{");
1294 delim = from_ascii("\\}");
1297 delim = docstring(1, c);
1300 if (InsetMathBig::isBigInsetDelim(delim)) {
1301 // name + delim ared a valid InsetMathBig.
1302 // We can't use cur.macroModeClose() because
1303 // it does not handle delim.
1304 InsetMathUnknown * p = cur.activeMacro();
1307 cur.cell().erase(cur.pos());
1308 cur.plainInsert(MathAtom(
1309 new InsetMathBig(name.substr(1), delim)));
1314 // leave macro mode and try again if necessary
1315 cur.macroModeClose();
1317 cur.niceInsert(MathAtom(new InsetMathBrace));
1319 interpretChar(cur, c);
1323 // This is annoying as one has to press <space> far too often.
1327 // leave autocorrect mode if necessary
1328 if (autocorrect() && c == ' ') {
1329 autocorrect() = false;
1334 // just clear selection on pressing the space bar
1335 if (cur.selection() && c == ' ') {
1336 cur.selection() = false;
1343 //lyxerr << "starting with macro" << endl;
1344 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), false)));
1349 if (currentMode() == InsetMath::TEXT_MODE)
1355 if (currentMode() == InsetMath::TEXT_MODE) {
1356 // insert spaces in text mode,
1357 // but suppress direct insertion of two spaces in a row
1358 // the still allows typing '<space>a<space>' and deleting the 'a', but
1359 // it is better than nothing...
1360 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1362 // FIXME: we have to enable full redraw here because of the
1363 // visual box corners that define the inset. If we know for
1364 // sure that we stay within the same cell we can optimize for
1366 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1370 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1371 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1372 // FIXME: we have to enable full redraw here because of the
1373 // visual box corners that define the inset. If we know for
1374 // sure that we stay within the same cell we can optimize for
1376 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1380 if (cur.popRight()) {
1381 // FIXME: we have to enable full redraw here because of the
1382 // visual box corners that define the inset. If we know for
1383 // sure that we stay within the same cell we can optimize for
1385 //cur.updateFlags(Update::FitCursor);
1389 // if we are at the very end, leave the formula
1390 return cur.pos() != cur.lastpos();
1393 // These shouldn't work in text mode:
1394 if (currentMode() != InsetMath::TEXT_MODE) {
1396 script(cur, false, save_selection);
1400 script(cur, true, save_selection);
1404 cur.niceInsert(createInsetMath("sim"));
1409 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1410 c == '%' || c == '_' || c == '^') {
1411 cur.niceInsert(createInsetMath(docstring(1, c)));
1416 // try auto-correction
1417 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1420 // no special circumstances, so insert the character without any fuss
1422 cur.autocorrect() = true;
1427 bool InsetMathNest::interpretString(LCursor & cur, docstring const & str)
1429 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1431 if (!cur.empty() && cur.pos() > 0 &&
1432 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1433 if (InsetMathBig::isBigInsetDelim(str)) {
1434 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1435 if (prev[0] == '\\') {
1436 prev = prev.substr(1);
1437 latexkeys const * l = in_word_set(prev);
1438 if (l && l->inset == "big") {
1439 cur.cell()[cur.pos() - 1] =
1440 MathAtom(new InsetMathBig(prev, str));
1450 bool InsetMathNest::script(LCursor & cur, bool up,
1451 docstring const & save_selection)
1453 // Hack to get \^ and \_ working
1454 //lyxerr << "handling script: up: " << up << endl;
1455 if (cur.inMacroMode() && cur.macroName() == "\\") {
1457 cur.niceInsert(createInsetMath("mathcircumflex"));
1459 interpretChar(cur, '_');
1463 cur.macroModeClose();
1464 if (asScriptInset() && cur.idx() == 0) {
1465 // we are in a nucleus of a script inset, move to _our_ script
1466 InsetMathScript * inset = asScriptInset();
1467 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1469 cur.idx() = inset->idxOfScript(up);
1471 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1473 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1476 cur.idx() = inset->idxOfScript(up);
1477 cur.pos() = cur.lastpos();
1479 // convert the thing to our left to a scriptinset or create a new
1480 // one if in the very first position of the array
1481 if (cur.pos() == 0) {
1482 //lyxerr << "new scriptinset" << endl;
1483 cur.insert(new InsetMathScript(up));
1485 //lyxerr << "converting prev atom " << endl;
1486 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1489 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1490 // See comment in MathParser.C for special handling of {}-bases
1496 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1497 cur.niceInsert(save_selection);
1499 //lyxerr << "inserting selection 2:\n" << save_selection << endl;