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 "MathMLStream.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"
47 #include "outputparams.h"
50 #include "support/lstrings.h"
52 #include "frontends/Dialogs.h"
53 #include "frontends/LyXView.h"
54 #include "frontends/Painter.h"
55 #include "frontends/Selection.h"
56 #include "frontends/nullpainter.h"
60 using lyx::cap::copySelection;
61 using lyx::cap::grabAndEraseSelection;
62 using lyx::cap::cutSelection;
63 using lyx::cap::replaceSelection;
64 using lyx::cap::selClearOrDel;
66 using lyx::frontend::Clipboard;
70 using std::istringstream;
73 InsetMathNest::InsetMathNest(idx_type nargs)
74 : cells_(nargs), lock_(false)
78 InsetMath::idx_type InsetMathNest::nargs() const
84 MathArray & InsetMathNest::cell(idx_type i)
90 MathArray const & InsetMathNest::cell(idx_type i) const
96 void InsetMathNest::cursorPos(CursorSlice const & sl, bool /*boundary*/,
97 int & x, int & y) const
99 // FIXME: This is a hack. Ideally, the coord cache should not store
100 // absolute positions, but relative ones. This would mean to call
101 // setXY() not in MathArray::draw(), but in the parent insets' draw()
102 // with the correctly adjusted x,y values. But this means that we'd have
103 // to touch all (math)inset's draw() methods. Right now, we'll store
104 // absolute value, and make them here relative, only to make them
105 // absolute again when actually drawing the cursor. What a mess.
106 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
107 MathArray const & ar = sl.cell();
108 if (!theCoords.getArrays().has(&ar)) {
109 // this can (semi-)legally happen if we just created this cell
110 // and it never has been drawn before. So don't ASSERT.
111 //lyxerr << "no cached data for array " << &ar << endl;
116 Point const pt = theCoords.getArrays().xy(&ar);
117 if (!theCoords.getInsets().has(this)) {
119 //lyxerr << "no cached data for inset " << this << endl;
124 Point const pt2 = theCoords.getInsets().xy(this);
125 //lyxerr << "retrieving position cache for MathArray "
126 // << pt.x_ << ' ' << pt.y_ << std::endl;
127 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
129 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
130 // << " asc: " << ascent() << " des: " << descent()
131 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
132 // move cursor visually into empty cells ("blue rectangles");
138 void InsetMathNest::metrics(MetricsInfo const & mi) const
141 for (idx_type i = 0, n = nargs(); i != n; ++i)
146 bool InsetMathNest::idxNext(LCursor & cur) const
148 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
149 if (cur.idx() == cur.lastidx())
157 bool InsetMathNest::idxRight(LCursor & cur) const
163 bool InsetMathNest::idxPrev(LCursor & cur) const
165 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
169 cur.pos() = cur.lastpos();
174 bool InsetMathNest::idxLeft(LCursor & cur) const
180 bool InsetMathNest::idxFirst(LCursor & cur) const
182 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
191 bool InsetMathNest::idxLast(LCursor & cur) const
193 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
196 cur.idx() = cur.lastidx();
197 cur.pos() = cur.lastpos();
202 void InsetMathNest::dump() const
204 WriteStream os(lyxerr);
205 os << "---------------------------------------------\n";
208 for (idx_type i = 0, n = nargs(); i != n; ++i)
209 os << cell(i) << "\n";
210 os << "---------------------------------------------\n";
214 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
218 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
221 setPosCache(pi, x, y);
225 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
227 // this should use the x/y values given, not the cached values
228 LCursor & cur = pi.base.bv->cursor();
229 if (!cur.selection())
231 if (!ptr_cmp(&cur.inset(), this))
234 // FIXME: hack to get position cache warm
235 static lyx::frontend::NullPainter nop;
236 PainterInfo pinop(pi);
240 CursorSlice s1 = cur.selBegin();
241 CursorSlice s2 = cur.selEnd();
242 //lyxerr << "InsetMathNest::drawing selection: "
243 // << " s1: " << s1 << " s2: " << s2 << endl;
244 if (s1.idx() == s2.idx()) {
245 MathArray const & c = cell(s1.idx());
246 int x1 = c.xo() + c.pos2x(s1.pos());
247 int y1 = c.yo() - c.ascent();
248 int x2 = c.xo() + c.pos2x(s2.pos());
249 int y2 = c.yo() + c.descent();
250 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
251 //lyxerr << "InsetMathNest::drawing selection 3: "
252 // << " x1: " << x1 << " x2: " << x2
253 // << " y1: " << y1 << " y2: " << y2 << endl;
255 for (idx_type i = 0; i < nargs(); ++i) {
256 if (idxBetween(i, s1.idx(), s2.idx())) {
257 MathArray const & c = cell(i);
259 int y1 = c.yo() - c.ascent();
260 int x2 = c.xo() + c.width();
261 int y2 = c.yo() + c.descent();
262 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
269 void InsetMathNest::validate(LaTeXFeatures & features) const
271 for (idx_type i = 0; i < nargs(); ++i)
272 cell(i).validate(features);
276 void InsetMathNest::replace(ReplaceData & rep)
278 for (idx_type i = 0; i < nargs(); ++i)
279 cell(i).replace(rep);
283 bool InsetMathNest::contains(MathArray const & ar) const
285 for (idx_type i = 0; i < nargs(); ++i)
286 if (cell(i).contains(ar))
292 bool InsetMathNest::lock() const
298 void InsetMathNest::lock(bool l)
304 bool InsetMathNest::isActive() const
310 MathArray InsetMathNest::glue() const
313 for (size_t i = 0; i < nargs(); ++i)
319 void InsetMathNest::write(WriteStream & os) const
321 os << '\\' << name().c_str();
322 for (size_t i = 0; i < nargs(); ++i)
323 os << '{' << cell(i) << '}';
325 os.pendingSpace(true);
326 if (lock_ && !os.latex()) {
328 os.pendingSpace(true);
333 void InsetMathNest::normalize(NormalStream & os) const
335 os << '[' << name().c_str();
336 for (size_t i = 0; i < nargs(); ++i)
337 os << ' ' << cell(i);
342 int InsetMathNest::latex(Buffer const &, std::ostream & os,
343 OutputParams const & runparams) const
345 WriteStream wi(os, runparams.moving_arg, true);
351 bool InsetMathNest::notifyCursorLeaves(LCursor & /*cur*/)
357 MathArray & ar = cur.cell();
358 // remove base-only "scripts"
359 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
360 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
361 if (p && p->nargs() == 1) {
362 MathArray ar = p->nuc();
365 cur.adjust(i, ar.size() - 1);
369 // glue adjacent font insets of the same kind
370 for (pos_type i = 0; i + 1 < size(); ++i) {
371 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
372 InsetMathFont const * q = operator[](i + 1)->asFontInset();
373 if (p && q && p->name() == q->name()) {
374 p->cell(0).append(q->cell(0));
384 void InsetMathNest::handleFont
385 (LCursor & cur, string const & arg, string const & font)
387 // this whole function is a hack and won't work for incremental font
389 recordUndo(cur, Undo::ATOMIC);
391 if (cur.inset().asInsetMath()->name() == font)
392 cur.handleFont(font);
394 cur.handleNest(createInsetMath(font));
400 void InsetMathNest::handleFont2(LCursor & cur, string const & arg)
402 recordUndo(cur, Undo::ATOMIC);
405 bv_funcs::string2font(arg, font, b);
406 if (font.color() != LColor::inherit) {
407 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
408 cur.handleNest(at, 0);
413 void InsetMathNest::doDispatch(LCursor & cur, FuncRequest & cmd)
415 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
416 //CursorSlice sl = cur.current();
418 switch (cmd.action) {
422 cur.message(_("Paste"));
423 replaceSelection(cur);
425 istringstream is(lyx::to_utf8(cmd.argument()));
427 string const selection = lyx::cap::getSelection(cur.buffer(), n);
428 cur.niceInsert(selection);
429 cur.clearSelection(); // bug 393
430 cur.bv().switchKeyMap();
437 cutSelection(cur, true, true);
438 cur.message(_("Cut"));
439 // Prevent stale position >= size crash
440 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
447 cur.message(_("Copy"));
450 case LFUN_MOUSE_PRESS:
451 lfunMousePress(cur, cmd);
454 case LFUN_MOUSE_MOTION:
455 lfunMouseMotion(cur, cmd);
458 case LFUN_MOUSE_RELEASE:
459 lfunMouseRelease(cur, cmd);
462 case LFUN_FINISHED_LEFT:
463 cur.bv().cursor() = cur;
466 case LFUN_FINISHED_RIGHT:
468 cur.bv().cursor() = cur;
471 case LFUN_FINISHED_UP:
472 cur.bv().cursor() = cur;
475 case LFUN_FINISHED_DOWN:
477 cur.bv().cursor() = cur;
480 case LFUN_CHAR_FORWARD_SELECT:
481 case LFUN_CHAR_FORWARD:
482 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
483 cur.autocorrect() = false;
485 cur.macroModeClose();
486 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
487 cur.pushLeft(*cur.nextAtom().nucleus());
488 cur.inset().idxFirst(cur);
489 } else if (cur.posRight() || idxRight(cur)
490 || cur.popRight() || cur.selection())
493 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
498 case LFUN_CHAR_BACKWARD_SELECT:
499 case LFUN_CHAR_BACKWARD:
500 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
501 cur.autocorrect() = false;
503 cur.macroModeClose();
504 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
506 cur.push(*cur.nextAtom().nucleus());
507 cur.inset().idxLast(cur);
508 } else if (cur.posLeft() || idxLeft(cur)
509 || cur.popLeft() || cur.selection())
512 cmd = FuncRequest(LFUN_FINISHED_LEFT);
519 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
520 if (cur.inMacroMode()) {
522 cur.macroModeClose();
525 cur.selHandle(cmd.action == LFUN_UP_SELECT);
527 cmd = FuncRequest(LFUN_FINISHED_UP);
530 // fixes bug 1598. Please check!
534 case LFUN_DOWN_SELECT:
536 if (cur.inMacroMode()) {
537 cur.macroModeClose();
540 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
542 cmd = FuncRequest(LFUN_FINISHED_DOWN);
545 // fixes bug 1598. Please check!
549 case LFUN_MOUSE_DOUBLE:
550 case LFUN_MOUSE_TRIPLE:
551 case LFUN_WORD_SELECT:
555 cur.selection() = true;
556 cur.pos() = cur.lastpos();
557 cur.idx() = cur.lastidx();
560 case LFUN_PARAGRAPH_UP_SELECT:
561 case LFUN_PARAGRAPH_UP:
562 case LFUN_PARAGRAPH_DOWN_SELECT:
563 case LFUN_PARAGRAPH_DOWN:
566 case LFUN_LINE_BEGIN_SELECT:
567 case LFUN_LINE_BEGIN:
568 case LFUN_WORD_BACKWARD_SELECT:
569 case LFUN_WORD_BACKWARD:
570 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
571 cmd.action == LFUN_LINE_BEGIN_SELECT);
572 cur.macroModeClose();
573 if (cur.pos() != 0) {
575 } else if (cur.col() != 0) {
576 cur.idx() -= cur.col();
578 } else if (cur.idx() != 0) {
582 cmd = FuncRequest(LFUN_FINISHED_LEFT);
587 case LFUN_WORD_FORWARD_SELECT:
588 case LFUN_WORD_FORWARD:
589 case LFUN_LINE_END_SELECT:
591 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
592 cmd.action == LFUN_LINE_END_SELECT);
593 cur.macroModeClose();
595 if (cur.pos() != cur.lastpos()) {
596 cur.pos() = cur.lastpos();
597 } else if (ncols() && (cur.col() != cur.lastcol())) {
598 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
599 cur.pos() = cur.lastpos();
600 } else if (cur.idx() != cur.lastidx()) {
601 cur.idx() = cur.lastidx();
602 cur.pos() = cur.lastpos();
604 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
609 case LFUN_SCREEN_UP_SELECT:
611 cmd = FuncRequest(LFUN_FINISHED_LEFT);
615 case LFUN_SCREEN_DOWN_SELECT:
616 case LFUN_SCREEN_DOWN:
617 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
621 case LFUN_CELL_FORWARD:
622 cur.inset().idxNext(cur);
625 case LFUN_CELL_BACKWARD:
626 cur.inset().idxPrev(cur);
629 case LFUN_WORD_DELETE_BACKWARD:
630 case LFUN_CHAR_DELETE_BACKWARD:
632 // May affect external cell:
633 recordUndoInset(cur, Undo::ATOMIC);
635 recordUndo(cur, Undo::ATOMIC);
639 case LFUN_WORD_DELETE_FORWARD:
640 case LFUN_CHAR_DELETE_FORWARD:
641 if (cur.pos() == cur.lastpos())
642 // May affect external cell:
643 recordUndoInset(cur, Undo::ATOMIC);
645 recordUndo(cur, Undo::ATOMIC);
651 cur.clearSelection();
653 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
658 case LFUN_INSET_TOGGLE:
664 case LFUN_SELF_INSERT:
665 if (cmd.argument().size() != 1) {
667 string const arg = lyx::to_utf8(cmd.argument());
668 if (!interpret(cur, arg))
672 // Don't record undo steps if we are in macro mode and
673 // cmd.argument is the next character of the macro name.
674 // Otherwise we'll get an invalid cursor if we undo after
675 // the macro was finished and the macro is a known command,
676 // e.g. sqrt. LCursor::macroModeClose replaces in this case
677 // the InsetMathUnknown with name "frac" by an empty
678 // InsetMathFrac -> a pos value > 0 is invalid.
679 // A side effect is that an undo before the macro is finished
680 // undoes the complete macro, not only the last character.
681 if (!cur.inMacroMode())
684 // spacial handling of space. If we insert an inset
685 // via macro mode, we want to put the cursor inside it
686 // if relevant. Think typing "\frac<space>".
687 if (cmd.argument()[0] == ' '
688 && cur.inMacroMode() && cur.macroName() != "\\"
689 && cur.macroModeClose()) {
690 MathAtom const atom = cur.prevAtom();
691 if (atom->asNestInset() && atom->nargs() > 0) {
693 cur.pushLeft(*cur.nextInset());
696 // } else if (!interpret(cur, cmd.argument()[0])) {
697 // when interpret accepts UCS4 characters
698 } else if (!interpret(cur, lyx::to_utf8(cmd.argument()))) {
699 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
704 //case LFUN_SERVER_GET_XY:
705 // sprintf(dispatch_buffer, "%d %d",);
708 case LFUN_SERVER_SET_XY: {
709 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
712 istringstream is(lyx::to_utf8(cmd.argument()));
714 cur.setScreenPos(x, y);
718 // Special casing for superscript in case of LyX handling
720 case LFUN_ACCENT_CIRCUMFLEX:
721 if (cmd.argument().empty()) {
722 // do superscript if LyX handles
724 recordUndo(cur, Undo::ATOMIC);
725 script(cur, true, grabAndEraseSelection(cur));
729 case LFUN_ACCENT_UMLAUT:
730 case LFUN_ACCENT_ACUTE:
731 case LFUN_ACCENT_GRAVE:
732 case LFUN_ACCENT_BREVE:
733 case LFUN_ACCENT_DOT:
734 case LFUN_ACCENT_MACRON:
735 case LFUN_ACCENT_CARON:
736 case LFUN_ACCENT_TILDE:
737 case LFUN_ACCENT_CEDILLA:
738 case LFUN_ACCENT_CIRCLE:
739 case LFUN_ACCENT_UNDERDOT:
740 case LFUN_ACCENT_TIE:
741 case LFUN_ACCENT_OGONEK:
742 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
746 case LFUN_FONT_FREE_APPLY:
747 case LFUN_FONT_FREE_UPDATE:
748 handleFont2(cur, lyx::to_utf8(cmd.argument()));
752 if (currentMode() == TEXT_MODE)
753 handleFont(cur, lyx::to_utf8(cmd.argument()), "textbf");
755 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathbf");
758 if (currentMode() == TEXT_MODE)
759 handleFont(cur, lyx::to_utf8(cmd.argument()), "textsf");
761 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathsf");
764 if (currentMode() == TEXT_MODE)
765 handleFont(cur, lyx::to_utf8(cmd.argument()), "emph");
767 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathcal");
769 case LFUN_FONT_ROMAN:
770 if (currentMode() == TEXT_MODE)
771 handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
773 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathrm");
776 if (currentMode() == TEXT_MODE)
777 handleFont(cur, lyx::to_utf8(cmd.argument()), "texttt");
779 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathtt");
782 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathfrak");
785 if (currentMode() == TEXT_MODE)
786 handleFont(cur, lyx::to_utf8(cmd.argument()), "textit");
788 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathit");
791 if (currentMode() == TEXT_MODE)
792 // FIXME: should be "noun"
793 handleFont(cur, lyx::to_utf8(cmd.argument()), "textsc");
795 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathbb");
797 //case LFUN_FONT_FREE_APPLY:
798 handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
800 case LFUN_FONT_DEFAULT:
801 handleFont(cur, lyx::to_utf8(cmd.argument()), "textnormal");
804 case LFUN_MATH_MODE: {
806 // ignore math-mode on when already in math mode
807 if (currentMode() == InsetBase::MATH_MODE && cmd.argument() == "on")
809 cur.macroModeClose();
810 string const save_selection = grabAndEraseSelection(cur);
812 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
813 cur.plainInsert(MathAtom(new InsetMathBox("mbox")));
815 cur.pushLeft(*cur.nextInset());
816 cur.niceInsert(save_selection);
818 if (currentMode() == InsetBase::TEXT_MODE) {
819 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
820 cur.message(_("create new math text environment ($...$)"));
822 handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
823 cur.message(_("entered math text mode (textrm)"));
836 case LFUN_MATH_MATRIX: {
837 recordUndo(cur, Undo::ATOMIC);
842 istringstream is(lyx::to_utf8(cmd.argument()));
843 is >> m >> n >> v_align >> h_align;
850 MathAtom(new InsetMathArray("array", m, n, v_align[0], h_align)));
854 case LFUN_MATH_DELIM: {
856 string rs = lyx::support::split(lyx::to_utf8(cmd.argument()), ls, ' ');
857 // Reasonable default values
862 recordUndo(cur, Undo::ATOMIC);
863 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
867 case LFUN_MATH_BIGDELIM: {
868 string const lname = cmd.getArg(0);
869 string const ldelim = cmd.getArg(1);
870 string const rname = cmd.getArg(2);
871 string const rdelim = cmd.getArg(3);
872 latexkeys const * l = in_word_set(lname);
873 bool const have_l = l && l->inset == "big" &&
874 InsetMathBig::isBigInsetDelim(ldelim);
875 l = in_word_set(rname);
876 bool const have_r = l && l->inset == "big" &&
877 InsetMathBig::isBigInsetDelim(rdelim);
878 // We mimic LFUN_MATH_DELIM in case we have an empty left
879 // or right delimiter.
880 if (have_l || have_r) {
881 recordUndo(cur, Undo::ATOMIC);
882 string const selection = grabAndEraseSelection(cur);
885 cur.insert(MathAtom(new InsetMathBig(lname,
887 cur.niceInsert(selection);
889 cur.insert(MathAtom(new InsetMathBig(rname,
892 // Don't call cur.undispatched() if we did nothing, this would
893 // lead to infinite recursion via LyXText::dispatch().
897 case LFUN_SPACE_INSERT:
898 case LFUN_MATH_SPACE:
899 recordUndo(cur, Undo::ATOMIC);
900 cur.insert(MathAtom(new InsetMathSpace(",")));
903 case LFUN_ERT_INSERT:
904 // interpret this as if a backslash was typed
905 recordUndo(cur, Undo::ATOMIC);
906 interpret(cur, '\\');
909 case LFUN_MATH_SUBSCRIPT:
910 // interpret this as if a _ was typed
911 recordUndo(cur, Undo::ATOMIC);
915 case LFUN_MATH_SUPERSCRIPT:
916 // interpret this as if a ^ was typed
917 recordUndo(cur, Undo::ATOMIC);
921 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
922 // handling such that "self-insert" works on "arbitrary stuff" too, and
923 // math-insert only handles special math things like "matrix".
924 case LFUN_MATH_INSERT: {
925 recordUndo(cur, Undo::ATOMIC);
926 if (cmd.argument() == "^" || cmd.argument() == "_")
927 interpret(cur, cmd.argument()[0]);
929 cur.niceInsert(lyx::to_utf8(cmd.argument()));
933 case LFUN_DIALOG_SHOW_NEW_INSET: {
934 string const & name = lyx::to_utf8(cmd.argument());
938 data = tmp.createDialogStr(name);
940 cur.bv().showInsetDialog(name, data, 0);
945 InsetMathDim::doDispatch(cur, cmd);
951 bool InsetMathNest::getStatus(LCursor & cur, FuncRequest const & cmd,
952 FuncStatus & flag) const
954 // the font related toggles
955 //string tc = "mathnormal";
957 string const arg = lyx::to_utf8(cmd.argument());
958 switch (cmd.action) {
959 case LFUN_TABULAR_FEATURE:
963 case LFUN_TABULAR_FEATURE:
964 // FIXME: check temporarily disabled
966 char align = mathcursor::valign();
971 if (cmd.argument().empty()) {
975 if (!contains("tcb", cmd.argument()[0])) {
979 flag.setOnOff(cmd.argument()[0] == align);
982 /// We have to handle them since 1.4 blocks all unhandled actions
989 case LFUN_FONT_ROMAN:
990 case LFUN_FONT_DEFAULT:
993 case LFUN_MATH_MUTATE:
994 //flag.setOnOff(mathcursor::formula()->hullType() == lyx::to_utf8(cmd.argument()));
995 flag.setOnOff(false);
998 // we just need to be in math mode to enable that
1000 case LFUN_MATH_SPACE:
1001 case LFUN_MATH_LIMITS:
1002 case LFUN_MATH_NONUMBER:
1003 case LFUN_MATH_NUMBER:
1004 case LFUN_MATH_EXTERN:
1008 case LFUN_FONT_FRAK:
1009 flag.enabled(currentMode() != TEXT_MODE);
1012 case LFUN_MATH_INSERT: {
1013 bool const textarg =
1014 arg == "\\textbf" || arg == "\\textsf" ||
1015 arg == "\\textrm" || arg == "\\textmd" ||
1016 arg == "\\textit" || arg == "\\textsc" ||
1017 arg == "\\textsl" || arg == "\\textup" ||
1018 arg == "\\texttt" || arg == "\\textbb" ||
1019 arg == "\\textnormal";
1020 flag.enabled(currentMode() != TEXT_MODE || textarg);
1024 case LFUN_MATH_MATRIX:
1025 flag.enabled(currentMode() == MATH_MODE);
1028 case LFUN_MATH_DELIM:
1029 case LFUN_MATH_BIGDELIM:
1030 // Don't do this with multi-cell selections
1031 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1042 void InsetMathNest::edit(LCursor & cur, bool left)
1045 cur.idx() = left ? 0 : cur.lastidx();
1046 cur.pos() = left ? 0 : cur.lastpos();
1048 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1052 InsetBase * InsetMathNest::editXY(LCursor & cur, int x, int y)
1055 int dist_min = 1000000;
1056 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1057 int const d = cell(i).dist(x, y);
1063 MathArray & ar = cell(idx_min);
1065 cur.idx() = idx_min;
1066 cur.pos() = ar.x2pos(x - ar.xo());
1067 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1068 if (dist_min == 0) {
1070 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1071 if (ar[i]->covers(x, y))
1072 return ar[i].nucleus()->editXY(cur, x, y);
1078 void InsetMathNest::lfunMousePress(LCursor & cur, FuncRequest & cmd)
1080 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1081 BufferView & bv = cur.bv();
1082 if (cmd.button() == mouse_button::button1) {
1083 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1084 bv.mouseSetCursor(cur);
1085 } else if (cmd.button() == mouse_button::button2) {
1087 if (cur.selection())
1088 asArray(lyx::to_utf8(bv.cursor().selectionAsString(false)), ar);
1090 asArray(lyx::to_utf8(theApp->selection().get()), ar);
1093 bv.mouseSetCursor(cur);
1098 void InsetMathNest::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
1100 // only select with button 1
1101 if (cmd.button() == mouse_button::button1) {
1102 LCursor & bvcur = cur.bv().cursor();
1103 if (bvcur.anchor_.hasPart(cur)) {
1104 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1105 bvcur.setCursor(cur);
1106 bvcur.selection() = true;
1107 //lyxerr << "MOTION " << bvcur << endl;
1116 void InsetMathNest::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
1118 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1120 if (cmd.button() == mouse_button::button1) {
1121 //theApp->selection().put(cur.grabSelection());
1125 if (cmd.button() == mouse_button::button3) {
1126 // try to dispatch to enclosed insets first
1127 cur.bv().showDialog("mathpanel");
1135 bool InsetMathNest::interpret(LCursor & cur, char c)
1137 //lyxerr << "interpret 2: '" << c << "'" << endl;
1138 string save_selection;
1139 if (c == '^' || c == '_')
1140 save_selection = grabAndEraseSelection(cur);
1145 if (cur.inMacroMode()) {
1146 string name = cur.macroName();
1148 /// are we currently typing '#1' or '#2' or...?
1149 if (name == "\\#") {
1152 if (n >= 1 && n <= 9)
1153 cur.insert(new MathMacroArgument(n));
1158 cur.activeMacro()->setName(name + c);
1162 // handle 'special char' macros
1167 if (currentMode() == InsetMath::TEXT_MODE)
1168 cur.niceInsert(createInsetMath("textbackslash"));
1170 cur.niceInsert(createInsetMath("backslash"));
1171 } else if (c == '{') {
1173 cur.niceInsert(MathAtom(new InsetMathBrace));
1174 } else if (c == '%') {
1176 cur.niceInsert(MathAtom(new InsetMathComment));
1177 } else if (c == '#') {
1178 BOOST_ASSERT(cur.activeMacro());
1179 cur.activeMacro()->setName(name + c);
1182 cur.niceInsert(createInsetMath(string(1, c)));
1187 // One character big delimiters. The others are handled in
1188 // the other interpret() method.
1189 latexkeys const * l = in_word_set(name.substr(1));
1190 if (name[0] == '\\' && l && l->inset == "big") {
1200 delim = string(1, c);
1203 if (InsetMathBig::isBigInsetDelim(delim)) {
1204 // name + delim ared a valid InsetMathBig.
1205 // We can't use cur.macroModeClose() because
1206 // it does not handle delim.
1207 InsetMathUnknown * p = cur.activeMacro();
1210 cur.cell().erase(cur.pos());
1211 cur.plainInsert(MathAtom(
1212 new InsetMathBig(name.substr(1), delim)));
1217 // leave macro mode and try again if necessary
1218 cur.macroModeClose();
1220 cur.niceInsert(MathAtom(new InsetMathBrace));
1226 // This is annoying as one has to press <space> far too often.
1230 // leave autocorrect mode if necessary
1231 if (autocorrect() && c == ' ') {
1232 autocorrect() = false;
1237 // just clear selection on pressing the space bar
1238 if (cur.selection() && c == ' ') {
1239 cur.selection() = false;
1246 //lyxerr << "starting with macro" << endl;
1247 cur.insert(MathAtom(new InsetMathUnknown("\\", false)));
1252 if (currentMode() == InsetMath::TEXT_MODE)
1258 if (currentMode() == InsetMath::TEXT_MODE) {
1259 // insert spaces in text mode,
1260 // but suppress direct insertion of two spaces in a row
1261 // the still allows typing '<space>a<space>' and deleting the 'a', but
1262 // it is better than nothing...
1263 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
1267 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1268 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1273 // if are at the very end, leave the formula
1274 return cur.pos() != cur.lastpos();
1277 // These shouldn't work in text mode:
1278 if (currentMode() != InsetMath::TEXT_MODE) {
1280 script(cur, false, save_selection);
1284 script(cur, true, save_selection);
1288 cur.niceInsert(createInsetMath("sim"));
1293 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1294 c == '%' || c == '_' || c == '^') {
1295 cur.niceInsert(createInsetMath(string(1, c)));
1300 // try auto-correction
1301 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1304 // no special circumstances, so insert the character without any fuss
1306 cur.autocorrect() = true;
1311 bool InsetMathNest::interpret(LCursor & cur, string const & str)
1313 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1315 if (!cur.empty() && cur.pos() > 0 &&
1316 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1317 if (InsetMathBig::isBigInsetDelim(str)) {
1318 string prev = asString(cur.cell()[cur.pos() - 1]);
1319 if (prev[0] == '\\') {
1320 prev = prev.substr(1);
1321 latexkeys const * l = in_word_set(prev);
1322 if (l && l->inset == "big") {
1323 cur.cell()[cur.pos() - 1] =
1324 MathAtom(new InsetMathBig(prev, str));
1334 bool InsetMathNest::script(LCursor & cur, bool up, string const &
1337 // Hack to get \^ and \_ working
1338 //lyxerr << "handling script: up: " << up << endl;
1339 if (cur.inMacroMode() && cur.macroName() == "\\") {
1341 cur.niceInsert(createInsetMath("mathcircumflex"));
1343 interpret(cur, '_');
1347 cur.macroModeClose();
1348 if (asScriptInset() && cur.idx() == 0) {
1349 // we are in a nucleus of a script inset, move to _our_ script
1350 InsetMathScript * inset = asScriptInset();
1351 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1353 cur.idx() = inset->idxOfScript(up);
1355 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1357 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1360 cur.idx() = inset->idxOfScript(up);
1361 cur.pos() = cur.lastpos();
1363 // convert the thing to our left to a scriptinset or create a new
1364 // one if in the very first position of the array
1365 if (cur.pos() == 0) {
1366 //lyxerr << "new scriptinset" << endl;
1367 cur.insert(new InsetMathScript(up));
1369 //lyxerr << "converting prev atom " << endl;
1370 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1373 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1374 // special handling of {}-bases
1375 // is this always correct?
1376 if (inset->nuc().size() == 1
1377 && inset->nuc().back()->asBraceInset())
1378 inset->nuc() = inset->nuc().back()->asNestInset()->cell(0);
1384 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1385 cur.niceInsert(save_selection);
1387 //lyxerr << "inserting selection 2:\n" << save_selection << endl;