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/Clipboard.h"
54 #include "frontends/Painter.h"
55 #include "frontends/Selection.h"
57 #include "funcrequest.h"
58 #include "lyxserver.h"
59 #include "lyxsocket.h"
66 using cap::copySelection;
67 using cap::grabAndEraseSelection;
68 using cap::cutSelection;
69 using cap::replaceSelection;
70 using cap::selClearOrDel;
74 using std::istringstream;
77 InsetMathNest::InsetMathNest(idx_type nargs)
78 : cells_(nargs), lock_(false)
82 InsetMath::idx_type InsetMathNest::nargs() const
88 MathArray & InsetMathNest::cell(idx_type i)
94 MathArray const & InsetMathNest::cell(idx_type i) const
100 void InsetMathNest::cursorPos(BufferView const & bv,
101 CursorSlice const & sl, bool /*boundary*/,
102 int & x, int & y) const
104 // FIXME: This is a hack. Ideally, the coord cache should not store
105 // absolute positions, but relative ones. This would mean to call
106 // setXY() not in MathArray::draw(), but in the parent insets' draw()
107 // with the correctly adjusted x,y values. But this means that we'd have
108 // to touch all (math)inset's draw() methods. Right now, we'll store
109 // absolute value, and make them here relative, only to make them
110 // absolute again when actually drawing the cursor. What a mess.
111 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
112 MathArray const & ar = sl.cell();
113 CoordCache const & coord_cache = bv.coordCache();
114 if (!coord_cache.getArrays().has(&ar)) {
115 // this can (semi-)legally happen if we just created this cell
116 // and it never has been drawn before. So don't ASSERT.
117 //lyxerr << "no cached data for array " << &ar << endl;
122 Point const pt = coord_cache.getArrays().xy(&ar);
123 if (!coord_cache.getInsets().has(this)) {
125 //lyxerr << "no cached data for inset " << this << endl;
130 Point const pt2 = coord_cache.getInsets().xy(this);
131 //lyxerr << "retrieving position cache for MathArray "
132 // << pt.x_ << ' ' << pt.y_ << std::endl;
133 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
135 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
136 // << " asc: " << ascent() << " des: " << descent()
137 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
138 // move cursor visually into empty cells ("blue rectangles");
144 void InsetMathNest::metrics(MetricsInfo const & mi) const
147 for (idx_type i = 0, n = nargs(); i != n; ++i)
152 bool InsetMathNest::idxNext(LCursor & cur) const
154 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
155 if (cur.idx() == cur.lastidx())
163 bool InsetMathNest::idxRight(LCursor & cur) const
169 bool InsetMathNest::idxPrev(LCursor & cur) const
171 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
175 cur.pos() = cur.lastpos();
180 bool InsetMathNest::idxLeft(LCursor & cur) const
186 bool InsetMathNest::idxFirst(LCursor & cur) const
188 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
197 bool InsetMathNest::idxLast(LCursor & cur) const
199 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
202 cur.idx() = cur.lastidx();
203 cur.pos() = cur.lastpos();
208 void InsetMathNest::dump() const
210 odocstringstream oss;
212 os << "---------------------------------------------\n";
215 for (idx_type i = 0, n = nargs(); i != n; ++i)
216 os << cell(i) << "\n";
217 os << "---------------------------------------------\n";
218 lyxerr << to_utf8(oss.str());
222 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
226 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
229 setPosCache(pi, x, y);
233 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
235 BufferView & bv = *pi.base.bv;
236 // this should use the x/y values given, not the cached values
237 LCursor & cur = bv.cursor();
238 if (!cur.selection())
240 if (!ptr_cmp(&cur.inset(), this))
243 // FIXME: hack to get position cache warm
244 pi.pain.setDrawingEnabled(false);
246 pi.pain.setDrawingEnabled(true);
248 CursorSlice s1 = cur.selBegin();
249 CursorSlice s2 = cur.selEnd();
251 //lyxerr << "InsetMathNest::drawing selection: "
252 // << " s1: " << s1 << " s2: " << s2 << endl;
253 if (s1.idx() == s2.idx()) {
254 MathArray const & c = cell(s1.idx());
255 int x1 = c.xo(bv) + c.pos2x(s1.pos());
256 int y1 = c.yo(bv) - c.ascent();
257 int x2 = c.xo(bv) + c.pos2x(s2.pos());
258 int y2 = c.yo(bv) + c.descent();
259 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
260 //lyxerr << "InsetMathNest::drawing selection 3: "
261 // << " x1: " << x1 << " x2: " << x2
262 // << " y1: " << y1 << " y2: " << y2 << endl;
264 for (idx_type i = 0; i < nargs(); ++i) {
265 if (idxBetween(i, s1.idx(), s2.idx())) {
266 MathArray const & c = cell(i);
268 int y1 = c.yo(bv) - c.ascent();
269 int x2 = c.xo(bv) + c.width();
270 int y2 = c.yo(bv) + c.descent();
271 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
278 void InsetMathNest::validate(LaTeXFeatures & features) const
280 for (idx_type i = 0; i < nargs(); ++i)
281 cell(i).validate(features);
285 void InsetMathNest::replace(ReplaceData & rep)
287 for (idx_type i = 0; i < nargs(); ++i)
288 cell(i).replace(rep);
292 bool InsetMathNest::contains(MathArray const & ar) const
294 for (idx_type i = 0; i < nargs(); ++i)
295 if (cell(i).contains(ar))
301 bool InsetMathNest::lock() const
307 void InsetMathNest::lock(bool l)
313 bool InsetMathNest::isActive() const
319 MathArray InsetMathNest::glue() const
322 for (size_t i = 0; i < nargs(); ++i)
328 void InsetMathNest::write(WriteStream & os) const
330 os << '\\' << name().c_str();
331 for (size_t i = 0; i < nargs(); ++i)
332 os << '{' << cell(i) << '}';
334 os.pendingSpace(true);
335 if (lock_ && !os.latex()) {
337 os.pendingSpace(true);
342 void InsetMathNest::normalize(NormalStream & os) const
344 os << '[' << name().c_str();
345 for (size_t i = 0; i < nargs(); ++i)
346 os << ' ' << cell(i);
351 int InsetMathNest::latex(Buffer const &, odocstream & os,
352 OutputParams const & runparams) const
354 WriteStream wi(os, runparams.moving_arg, true);
360 bool InsetMathNest::notifyCursorLeaves(LCursor & /*cur*/)
366 MathArray & ar = cur.cell();
367 // remove base-only "scripts"
368 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
369 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
370 if (p && p->nargs() == 1) {
371 MathArray ar = p->nuc();
374 cur.adjust(i, ar.size() - 1);
378 // glue adjacent font insets of the same kind
379 for (pos_type i = 0; i + 1 < size(); ++i) {
380 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
381 InsetMathFont const * q = operator[](i + 1)->asFontInset();
382 if (p && q && p->name() == q->name()) {
383 p->cell(0).append(q->cell(0));
393 void InsetMathNest::handleFont
394 (LCursor & cur, docstring const & arg, char const * const font)
396 handleFont(cur, arg, from_ascii(font));
400 void InsetMathNest::handleFont
401 (LCursor & cur, docstring const & arg, docstring const & font)
403 // this whole function is a hack and won't work for incremental font
406 if (cur.inset().asInsetMath()->name() == font) {
407 recordUndoInset(cur, Undo::ATOMIC);
408 cur.handleFont(to_utf8(font));
410 recordUndo(cur, Undo::ATOMIC);
411 cur.handleNest(createInsetMath(font));
417 void InsetMathNest::handleFont2(LCursor & cur, docstring const & arg)
419 recordUndo(cur, Undo::ATOMIC);
422 bv_funcs::string2font(to_utf8(arg), font, b);
423 if (font.color() != LColor::inherit) {
424 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
425 cur.handleNest(at, 0);
430 void InsetMathNest::doDispatch(LCursor & cur, FuncRequest & cmd)
432 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
433 //CursorSlice sl = cur.current();
435 switch (cmd.action) {
439 cur.message(_("Paste"));
440 replaceSelection(cur);
442 if (cmd.argument().empty() && !theClipboard().isInternal())
443 topaste = theClipboard().getAsText();
446 idocstringstream is(cmd.argument());
448 topaste = cap::getSelection(cur.buffer(), n);
450 cur.niceInsert(topaste);
451 cur.clearSelection(); // bug 393
452 cur.bv().switchKeyMap();
459 cutSelection(cur, true, true);
460 cur.message(_("Cut"));
461 // Prevent stale position >= size crash
462 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
468 cur.message(_("Copy"));
471 case LFUN_MOUSE_PRESS:
472 lfunMousePress(cur, cmd);
475 case LFUN_MOUSE_MOTION:
476 lfunMouseMotion(cur, cmd);
479 case LFUN_MOUSE_RELEASE:
480 lfunMouseRelease(cur, cmd);
483 case LFUN_FINISHED_LEFT:
484 cur.bv().cursor() = cur;
487 case LFUN_FINISHED_RIGHT:
489 cur.bv().cursor() = cur;
492 case LFUN_FINISHED_UP:
493 cur.bv().cursor() = cur;
496 case LFUN_FINISHED_DOWN:
498 cur.bv().cursor() = cur;
501 case LFUN_CHAR_FORWARD:
502 // FIXME: we have to enable full redraw here because of the
503 // visual box corners that define the inset. If we know for
504 // sure that we stay within the same cell we can optimize for
506 //cur.updateFlags(Update::FitCursor);
507 case LFUN_CHAR_FORWARD_SELECT:
508 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
509 cur.autocorrect() = false;
511 cur.macroModeClose();
512 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
513 cur.pushLeft(*cur.nextAtom().nucleus());
514 cur.inset().idxFirst(cur);
515 } else if (cur.posRight() || idxRight(cur)
516 || cur.popRight() || cur.selection())
519 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
524 case LFUN_CHAR_BACKWARD:
525 // FIXME: we have to enable full redraw here because of the
526 // visual box corners that define the inset. If we know for
527 // sure that we stay within the same cell we can optimize for
529 //cur.updateFlags(Update::FitCursor);
530 case LFUN_CHAR_BACKWARD_SELECT:
531 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
532 cur.autocorrect() = false;
534 cur.macroModeClose();
535 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
537 cur.push(*cur.nextAtom().nucleus());
538 cur.inset().idxLast(cur);
539 } else if (cur.posLeft() || idxLeft(cur)
540 || cur.popLeft() || cur.selection())
543 cmd = FuncRequest(LFUN_FINISHED_LEFT);
549 // FIXME: we have to enable full redraw here because of the
550 // visual box corners that define the inset. If we know for
551 // sure that we stay within the same cell we can optimize for
553 //cur.updateFlags(Update::FitCursor);
555 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
556 if (cur.inMacroMode()) {
558 cur.macroModeClose();
561 cur.selHandle(cmd.action == LFUN_UP_SELECT);
563 cmd = FuncRequest(LFUN_FINISHED_UP);
566 // fixes bug 1598. Please check!
571 // FIXME: we have to enable full redraw here because of the
572 // visual box corners that define the inset. If we know for
573 // sure that we stay within the same cell we can optimize for
575 //cur.updateFlags(Update::FitCursor);
576 case LFUN_DOWN_SELECT:
577 if (cur.inMacroMode()) {
578 cur.macroModeClose();
581 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
583 cmd = FuncRequest(LFUN_FINISHED_DOWN);
586 // fixes bug 1598. Please check!
590 case LFUN_MOUSE_DOUBLE:
591 case LFUN_MOUSE_TRIPLE:
592 case LFUN_WORD_SELECT:
596 cur.selection() = true;
597 cur.pos() = cur.lastpos();
598 cur.idx() = cur.lastidx();
599 theSelection().haveSelection(true);
602 case LFUN_PARAGRAPH_UP:
603 case LFUN_PARAGRAPH_DOWN:
604 // FIXME: we have to enable full redraw here because of the
605 // visual box corners that define the inset. If we know for
606 // sure that we stay within the same cell we can optimize for
608 //cur.updateFlags(Update::FitCursor);
609 case LFUN_PARAGRAPH_UP_SELECT:
610 case LFUN_PARAGRAPH_DOWN_SELECT:
613 case LFUN_LINE_BEGIN:
614 case LFUN_WORD_BACKWARD:
615 // FIXME: we have to enable full redraw here because of the
616 // visual box corners that define the inset. If we know for
617 // sure that we stay within the same cell we can optimize for
619 //cur.updateFlags(Update::FitCursor);
620 case LFUN_LINE_BEGIN_SELECT:
621 case LFUN_WORD_BACKWARD_SELECT:
622 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
623 cmd.action == LFUN_LINE_BEGIN_SELECT);
624 cur.macroModeClose();
625 if (cur.pos() != 0) {
627 } else if (cur.col() != 0) {
628 cur.idx() -= cur.col();
630 } else if (cur.idx() != 0) {
634 cmd = FuncRequest(LFUN_FINISHED_LEFT);
639 case LFUN_WORD_FORWARD:
641 // FIXME: we have to enable full redraw here because of the
642 // visual box corners that define the inset. If we know for
643 // sure that we stay within the same cell we can optimize for
645 //cur.updateFlags(Update::FitCursor);
646 case LFUN_WORD_FORWARD_SELECT:
647 case LFUN_LINE_END_SELECT:
648 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
649 cmd.action == LFUN_LINE_END_SELECT);
650 cur.macroModeClose();
652 if (cur.pos() != cur.lastpos()) {
653 cur.pos() = cur.lastpos();
654 } else if (ncols() && (cur.col() != cur.lastcol())) {
655 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
656 cur.pos() = cur.lastpos();
657 } else if (cur.idx() != cur.lastidx()) {
658 cur.idx() = cur.lastidx();
659 cur.pos() = cur.lastpos();
661 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
666 case LFUN_SCREEN_UP_SELECT:
668 cmd = FuncRequest(LFUN_FINISHED_LEFT);
672 case LFUN_SCREEN_DOWN_SELECT:
673 case LFUN_SCREEN_DOWN:
674 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
678 case LFUN_CELL_FORWARD:
679 // FIXME: we have to enable full redraw here because of the
680 // visual box corners that define the inset. If we know for
681 // sure that we stay within the same cell we can optimize for
683 //cur.updateFlags(Update::FitCursor);
684 cur.inset().idxNext(cur);
687 case LFUN_CELL_BACKWARD:
688 // FIXME: we have to enable full redraw here because of the
689 // visual box corners that define the inset. If we know for
690 // sure that we stay within the same cell we can optimize for
692 //cur.updateFlags(Update::FitCursor);
693 cur.inset().idxPrev(cur);
696 case LFUN_WORD_DELETE_BACKWARD:
697 case LFUN_CHAR_DELETE_BACKWARD:
699 // May affect external cell:
700 recordUndoInset(cur, Undo::ATOMIC);
702 recordUndo(cur, Undo::ATOMIC);
703 // if the inset can not be removed from within, delete it
704 if (!cur.backspace()) {
705 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
706 cur.bv().getLyXText()->dispatch(cur, cmd);
710 case LFUN_WORD_DELETE_FORWARD:
711 case LFUN_CHAR_DELETE_FORWARD:
712 if (cur.pos() == cur.lastpos())
713 // May affect external cell:
714 recordUndoInset(cur, Undo::ATOMIC);
716 recordUndo(cur, Undo::ATOMIC);
717 // if the inset can not be removed from within, delete it
719 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
720 cur.bv().getLyXText()->dispatch(cur, cmd);
726 cur.clearSelection();
728 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
733 case LFUN_INSET_TOGGLE:
739 case LFUN_SELF_INSERT:
740 if (cmd.argument().size() != 1) {
742 docstring const arg = cmd.argument();
743 if (!interpretString(cur, arg))
747 // Don't record undo steps if we are in macro mode and
748 // cmd.argument is the next character of the macro name.
749 // Otherwise we'll get an invalid cursor if we undo after
750 // the macro was finished and the macro is a known command,
751 // e.g. sqrt. LCursor::macroModeClose replaces in this case
752 // the InsetMathUnknown with name "frac" by an empty
753 // InsetMathFrac -> a pos value > 0 is invalid.
754 // A side effect is that an undo before the macro is finished
755 // undoes the complete macro, not only the last character.
756 if (!cur.inMacroMode())
759 // spacial handling of space. If we insert an inset
760 // via macro mode, we want to put the cursor inside it
761 // if relevant. Think typing "\frac<space>".
762 if (cmd.argument()[0] == ' '
763 && cur.inMacroMode() && cur.macroName() != "\\"
764 && cur.macroModeClose()) {
765 MathAtom const atom = cur.prevAtom();
766 if (atom->asNestInset() && atom->nargs() > 0) {
768 cur.pushLeft(*cur.nextInset());
770 } else if (!interpretChar(cur, cmd.argument()[0])) {
771 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
776 //case LFUN_SERVER_GET_XY:
777 // sprintf(dispatch_buffer, "%d %d",);
780 case LFUN_SERVER_SET_XY: {
781 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
784 istringstream is(to_utf8(cmd.argument()));
786 cur.setScreenPos(x, y);
790 // Special casing for superscript in case of LyX handling
792 case LFUN_ACCENT_CIRCUMFLEX:
793 if (cmd.argument().empty()) {
794 // do superscript if LyX handles
796 recordUndo(cur, Undo::ATOMIC);
797 script(cur, true, grabAndEraseSelection(cur));
801 case LFUN_ACCENT_UMLAUT:
802 case LFUN_ACCENT_ACUTE:
803 case LFUN_ACCENT_GRAVE:
804 case LFUN_ACCENT_BREVE:
805 case LFUN_ACCENT_DOT:
806 case LFUN_ACCENT_MACRON:
807 case LFUN_ACCENT_CARON:
808 case LFUN_ACCENT_TILDE:
809 case LFUN_ACCENT_CEDILLA:
810 case LFUN_ACCENT_CIRCLE:
811 case LFUN_ACCENT_UNDERDOT:
812 case LFUN_ACCENT_TIE:
813 case LFUN_ACCENT_OGONEK:
814 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
818 case LFUN_FONT_FREE_APPLY:
819 case LFUN_FONT_FREE_UPDATE:
820 handleFont2(cur, cmd.argument());
824 if (currentMode() == TEXT_MODE)
825 handleFont(cur, cmd.argument(), "textbf");
827 handleFont(cur, cmd.argument(), "mathbf");
830 if (currentMode() == TEXT_MODE)
831 handleFont(cur, cmd.argument(), "textsf");
833 handleFont(cur, cmd.argument(), "mathsf");
836 if (currentMode() == TEXT_MODE)
837 handleFont(cur, cmd.argument(), "emph");
839 handleFont(cur, cmd.argument(), "mathcal");
841 case LFUN_FONT_ROMAN:
842 if (currentMode() == TEXT_MODE)
843 handleFont(cur, cmd.argument(), "textrm");
845 handleFont(cur, cmd.argument(), "mathrm");
848 if (currentMode() == TEXT_MODE)
849 handleFont(cur, cmd.argument(), "texttt");
851 handleFont(cur, cmd.argument(), "mathtt");
854 handleFont(cur, cmd.argument(), "mathfrak");
857 if (currentMode() == TEXT_MODE)
858 handleFont(cur, cmd.argument(), "textit");
860 handleFont(cur, cmd.argument(), "mathit");
863 if (currentMode() == TEXT_MODE)
864 // FIXME: should be "noun"
865 handleFont(cur, cmd.argument(), "textsc");
867 handleFont(cur, cmd.argument(), "mathbb");
870 case LFUN_FONT_FREE_APPLY:
871 handleFont(cur, cmd.argument(), "textrm");
874 case LFUN_FONT_DEFAULT:
875 handleFont(cur, cmd.argument(), "textnormal");
878 case LFUN_MATH_MODE: {
880 // ignore math-mode on when already in math mode
881 if (currentMode() == InsetBase::MATH_MODE && cmd.argument() == "on")
883 cur.macroModeClose();
884 docstring const save_selection = grabAndEraseSelection(cur);
886 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
887 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
889 cur.pushLeft(*cur.nextInset());
890 cur.niceInsert(save_selection);
892 if (currentMode() == InsetBase::TEXT_MODE) {
893 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
894 cur.message(_("create new math text environment ($...$)"));
896 handleFont(cur, cmd.argument(), "textrm");
897 cur.message(_("entered math text mode (textrm)"));
910 case LFUN_MATH_MATRIX: {
911 recordUndo(cur, Undo::ATOMIC);
916 idocstringstream is(cmd.argument());
917 is >> m >> n >> v_align >> h_align;
924 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
928 case LFUN_MATH_DELIM: {
930 docstring rs = support::split(cmd.argument(), ls, ' ');
931 // Reasonable default values
936 recordUndo(cur, Undo::ATOMIC);
937 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
941 case LFUN_MATH_BIGDELIM: {
942 docstring const lname = from_utf8(cmd.getArg(0));
943 docstring const ldelim = from_utf8(cmd.getArg(1));
944 docstring const rname = from_utf8(cmd.getArg(2));
945 docstring const rdelim = from_utf8(cmd.getArg(3));
946 latexkeys const * l = in_word_set(lname);
947 bool const have_l = l && l->inset == "big" &&
948 InsetMathBig::isBigInsetDelim(ldelim);
949 l = in_word_set(rname);
950 bool const have_r = l && l->inset == "big" &&
951 InsetMathBig::isBigInsetDelim(rdelim);
952 // We mimic LFUN_MATH_DELIM in case we have an empty left
953 // or right delimiter.
954 if (have_l || have_r) {
955 recordUndo(cur, Undo::ATOMIC);
956 docstring const selection = grabAndEraseSelection(cur);
959 cur.insert(MathAtom(new InsetMathBig(lname,
961 cur.niceInsert(selection);
963 cur.insert(MathAtom(new InsetMathBig(rname,
966 // Don't call cur.undispatched() if we did nothing, this would
967 // lead to infinite recursion via LyXText::dispatch().
971 case LFUN_SPACE_INSERT:
972 case LFUN_MATH_SPACE:
973 recordUndo(cur, Undo::ATOMIC);
974 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
977 case LFUN_ERT_INSERT:
978 // interpret this as if a backslash was typed
979 recordUndo(cur, Undo::ATOMIC);
980 interpretChar(cur, '\\');
983 case LFUN_MATH_SUBSCRIPT:
984 // interpret this as if a _ was typed
985 recordUndo(cur, Undo::ATOMIC);
986 interpretChar(cur, '_');
989 case LFUN_MATH_SUPERSCRIPT:
990 // interpret this as if a ^ was typed
991 recordUndo(cur, Undo::ATOMIC);
992 interpretChar(cur, '^');
995 case LFUN_QUOTE_INSERT:
996 // interpret this as if a straight " was typed
997 recordUndo(cur, Undo::ATOMIC);
998 interpretChar(cur, '\"');
1001 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
1002 // handling such that "self-insert" works on "arbitrary stuff" too, and
1003 // math-insert only handles special math things like "matrix".
1004 case LFUN_MATH_INSERT: {
1005 recordUndo(cur, Undo::ATOMIC);
1006 if (cmd.argument() == "^" || cmd.argument() == "_") {
1007 interpretChar(cur, cmd.argument()[0]);
1009 cur.niceInsert(cmd.argument());
1013 case LFUN_DIALOG_SHOW_NEW_INSET: {
1014 docstring const & name = cmd.argument();
1016 if (name == "ref") {
1018 data = tmp.createDialogStr(to_utf8(name));
1020 cur.bv().showInsetDialog(to_utf8(name), data, 0);
1024 case LFUN_INSET_INSERT: {
1026 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1035 InsetMathDim::doDispatch(cur, cmd);
1041 bool InsetMathNest::getStatus(LCursor & cur, FuncRequest const & cmd,
1042 FuncStatus & flag) const
1044 // the font related toggles
1045 //string tc = "mathnormal";
1047 string const arg = to_utf8(cmd.argument());
1048 switch (cmd.action) {
1049 case LFUN_TABULAR_FEATURE:
1050 flag.enabled(false);
1053 case LFUN_TABULAR_FEATURE:
1054 // FIXME: check temporarily disabled
1056 char align = mathcursor::valign();
1057 if (align == '\0') {
1061 if (cmd.argument().empty()) {
1065 if (!contains("tcb", cmd.argument()[0])) {
1069 flag.setOnOff(cmd.argument()[0] == align);
1072 /// We have to handle them since 1.4 blocks all unhandled actions
1073 case LFUN_FONT_ITAL:
1074 case LFUN_FONT_BOLD:
1075 case LFUN_FONT_SANS:
1076 case LFUN_FONT_EMPH:
1077 case LFUN_FONT_CODE:
1078 case LFUN_FONT_NOUN:
1079 case LFUN_FONT_ROMAN:
1080 case LFUN_FONT_DEFAULT:
1083 case LFUN_MATH_MUTATE:
1084 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1085 flag.setOnOff(false);
1088 // we just need to be in math mode to enable that
1089 case LFUN_MATH_SIZE:
1090 case LFUN_MATH_SPACE:
1091 case LFUN_MATH_LIMITS:
1092 case LFUN_MATH_NONUMBER:
1093 case LFUN_MATH_NUMBER:
1094 case LFUN_MATH_EXTERN:
1098 case LFUN_FONT_FRAK:
1099 flag.enabled(currentMode() != TEXT_MODE);
1102 case LFUN_MATH_INSERT: {
1103 bool const textarg =
1104 arg == "\\textbf" || arg == "\\textsf" ||
1105 arg == "\\textrm" || arg == "\\textmd" ||
1106 arg == "\\textit" || arg == "\\textsc" ||
1107 arg == "\\textsl" || arg == "\\textup" ||
1108 arg == "\\texttt" || arg == "\\textbb" ||
1109 arg == "\\textnormal";
1110 flag.enabled(currentMode() != TEXT_MODE || textarg);
1114 case LFUN_MATH_MATRIX:
1115 flag.enabled(currentMode() == MATH_MODE);
1118 case LFUN_INSET_INSERT: {
1119 // Don't test createMathInset_fromDialogStr(), since
1120 // getStatus is not called with a valid reference and the
1121 // dialog would not be applyable.
1122 string const name = cmd.getArg(0);
1123 flag.enabled(name == "ref");
1127 case LFUN_MATH_DELIM:
1128 case LFUN_MATH_BIGDELIM:
1129 // Don't do this with multi-cell selections
1130 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1141 void InsetMathNest::edit(LCursor & cur, bool left)
1144 cur.idx() = left ? 0 : cur.lastidx();
1145 cur.pos() = left ? 0 : cur.lastpos();
1147 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1151 InsetBase * InsetMathNest::editXY(LCursor & cur, int x, int y)
1154 int dist_min = 1000000;
1155 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1156 int const d = cell(i).dist(cur.bv(), x, y);
1162 MathArray & ar = cell(idx_min);
1164 cur.idx() = idx_min;
1165 cur.pos() = ar.x2pos(x - ar.xo(cur.bv()));
1167 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1168 if (dist_min == 0) {
1170 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1171 if (ar[i]->covers(cur.bv(), x, y))
1172 return ar[i].nucleus()->editXY(cur, x, y);
1178 void InsetMathNest::lfunMousePress(LCursor & cur, FuncRequest & cmd)
1180 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1181 BufferView & bv = cur.bv();
1182 if (cmd.button() == mouse_button::button1) {
1183 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1184 bv.mouseSetCursor(cur);
1185 // FIXME: we have to enable full redraw here because of the
1186 // visual box corners that define the inset.
1188 } else if (cmd.button() == mouse_button::button2) {
1190 if (cur.selection()) {
1191 // See comment in LyXText::dispatch why we do this
1192 cap::copySelectionToStack(bv.cursor());
1193 asArray(bv.cursor().selectionAsString(false), ar);
1195 asArray(theSelection().get(), ar);
1198 bv.mouseSetCursor(cur);
1203 void InsetMathNest::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
1205 // only select with button 1
1206 if (cmd.button() == mouse_button::button1) {
1207 LCursor & bvcur = cur.bv().cursor();
1208 if (bvcur.anchor_.hasPart(cur)) {
1209 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1210 bvcur.setCursor(cur);
1211 bvcur.selection() = true;
1212 //lyxerr << "MOTION " << bvcur << endl;
1219 void InsetMathNest::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
1221 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1223 if (cmd.button() == mouse_button::button1) {
1224 if (cur.bv().cursor().selection())
1225 theSelection().haveSelection(true);
1226 if (!cur.selection())
1231 if (cmd.button() == mouse_button::button3) {
1232 // try to dispatch to enclosed insets first
1233 cur.bv().showDialog("mathpanel");
1241 bool InsetMathNest::interpretChar(LCursor & cur, char_type c)
1243 //lyxerr << "interpret 2: '" << c << "'" << endl;
1244 docstring save_selection;
1245 if (c == '^' || c == '_')
1246 save_selection = grabAndEraseSelection(cur);
1251 if (cur.inMacroMode()) {
1252 docstring name = cur.macroName();
1254 /// are we currently typing '#1' or '#2' or...?
1255 if (name == "\\#") {
1258 if (n >= 1 && n <= 9)
1259 cur.insert(new MathMacroArgument(n));
1264 cur.activeMacro()->setName(name + docstring(1, c));
1268 // handle 'special char' macros
1273 if (currentMode() == InsetMath::TEXT_MODE)
1274 cur.niceInsert(createInsetMath("textbackslash"));
1276 cur.niceInsert(createInsetMath("backslash"));
1277 } else if (c == '{') {
1279 cur.niceInsert(MathAtom(new InsetMathBrace));
1280 } else if (c == '%') {
1282 cur.niceInsert(MathAtom(new InsetMathComment));
1283 } else if (c == '#') {
1284 BOOST_ASSERT(cur.activeMacro());
1285 cur.activeMacro()->setName(name + docstring(1, c));
1288 cur.niceInsert(createInsetMath(docstring(1, c)));
1293 // One character big delimiters. The others are handled in
1294 // interpretString().
1295 latexkeys const * l = in_word_set(name.substr(1));
1296 if (name[0] == '\\' && l && l->inset == "big") {
1300 delim = from_ascii("\\{");
1303 delim = from_ascii("\\}");
1306 delim = docstring(1, c);
1309 if (InsetMathBig::isBigInsetDelim(delim)) {
1310 // name + delim ared a valid InsetMathBig.
1311 // We can't use cur.macroModeClose() because
1312 // it does not handle delim.
1313 InsetMathUnknown * p = cur.activeMacro();
1316 cur.cell().erase(cur.pos());
1317 cur.plainInsert(MathAtom(
1318 new InsetMathBig(name.substr(1), delim)));
1323 // leave macro mode and try again if necessary
1324 cur.macroModeClose();
1326 cur.niceInsert(MathAtom(new InsetMathBrace));
1328 interpretChar(cur, c);
1332 // This is annoying as one has to press <space> far too often.
1336 // leave autocorrect mode if necessary
1337 if (autocorrect() && c == ' ') {
1338 autocorrect() = false;
1343 // just clear selection on pressing the space bar
1344 if (cur.selection() && c == ' ') {
1345 cur.selection() = false;
1352 //lyxerr << "starting with macro" << endl;
1353 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), false)));
1358 if (currentMode() == InsetMath::TEXT_MODE)
1364 if (currentMode() == InsetMath::TEXT_MODE) {
1365 // insert spaces in text mode,
1366 // but suppress direct insertion of two spaces in a row
1367 // the still allows typing '<space>a<space>' and deleting the 'a', but
1368 // it is better than nothing...
1369 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
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.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1380 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
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::SinglePar | Update::FitCursor);
1389 if (cur.popRight()) {
1390 // FIXME: we have to enable full redraw here because of the
1391 // visual box corners that define the inset. If we know for
1392 // sure that we stay within the same cell we can optimize for
1394 //cur.updateFlags(Update::FitCursor);
1398 // if we are at the very end, leave the formula
1399 return cur.pos() != cur.lastpos();
1402 // These shouldn't work in text mode:
1403 if (currentMode() != InsetMath::TEXT_MODE) {
1405 script(cur, false, save_selection);
1409 script(cur, true, save_selection);
1413 cur.niceInsert(createInsetMath("sim"));
1418 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1419 c == '%' || c == '_' || c == '^') {
1420 cur.niceInsert(createInsetMath(docstring(1, c)));
1425 // try auto-correction
1426 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1429 // no special circumstances, so insert the character without any fuss
1431 cur.autocorrect() = true;
1436 bool InsetMathNest::interpretString(LCursor & cur, docstring const & str)
1438 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1440 if (!cur.empty() && cur.pos() > 0 &&
1441 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1442 if (InsetMathBig::isBigInsetDelim(str)) {
1443 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1444 if (prev[0] == '\\') {
1445 prev = prev.substr(1);
1446 latexkeys const * l = in_word_set(prev);
1447 if (l && l->inset == "big") {
1448 cur.cell()[cur.pos() - 1] =
1449 MathAtom(new InsetMathBig(prev, str));
1459 bool InsetMathNest::script(LCursor & cur, bool up,
1460 docstring const & save_selection)
1462 // Hack to get \^ and \_ working
1463 //lyxerr << "handling script: up: " << up << endl;
1464 if (cur.inMacroMode() && cur.macroName() == "\\") {
1466 cur.niceInsert(createInsetMath("mathcircumflex"));
1468 interpretChar(cur, '_');
1472 cur.macroModeClose();
1473 if (asScriptInset() && cur.idx() == 0) {
1474 // we are in a nucleus of a script inset, move to _our_ script
1475 InsetMathScript * inset = asScriptInset();
1476 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1478 cur.idx() = inset->idxOfScript(up);
1480 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1482 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1485 cur.idx() = inset->idxOfScript(up);
1486 cur.pos() = cur.lastpos();
1488 // convert the thing to our left to a scriptinset or create a new
1489 // one if in the very first position of the array
1490 if (cur.pos() == 0) {
1491 //lyxerr << "new scriptinset" << endl;
1492 cur.insert(new InsetMathScript(up));
1494 //lyxerr << "converting prev atom " << endl;
1495 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1498 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1499 // See comment in MathParser.C for special handling of {}-bases
1505 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1506 cur.niceInsert(save_selection);
1508 //lyxerr << "inserting selection 2:\n" << save_selection << endl;