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/Painter.h"
53 #include "frontends/Selection.h"
54 #include "frontends/nullpainter.h"
56 #include "funcrequest.h"
57 #include "lyxserver.h"
58 #include "lyxsocket.h"
62 using lyx::cap::copySelection;
63 using lyx::cap::grabAndEraseSelection;
64 using lyx::cap::cutSelection;
65 using lyx::cap::replaceSelection;
66 using lyx::cap::selClearOrDel;
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);
944 case LFUN_INSET_INSERT: {
946 if (createInsetMath_fromDialogStr(lyx::to_utf8(cmd.argument()), ar)) {
955 InsetMathDim::doDispatch(cur, cmd);
961 bool InsetMathNest::getStatus(LCursor & cur, FuncRequest const & cmd,
962 FuncStatus & flag) const
964 // the font related toggles
965 //string tc = "mathnormal";
967 string const arg = lyx::to_utf8(cmd.argument());
968 switch (cmd.action) {
969 case LFUN_TABULAR_FEATURE:
973 case LFUN_TABULAR_FEATURE:
974 // FIXME: check temporarily disabled
976 char align = mathcursor::valign();
981 if (cmd.argument().empty()) {
985 if (!contains("tcb", cmd.argument()[0])) {
989 flag.setOnOff(cmd.argument()[0] == align);
992 /// We have to handle them since 1.4 blocks all unhandled actions
999 case LFUN_FONT_ROMAN:
1000 case LFUN_FONT_DEFAULT:
1003 case LFUN_MATH_MUTATE:
1004 //flag.setOnOff(mathcursor::formula()->hullType() == lyx::to_utf8(cmd.argument()));
1005 flag.setOnOff(false);
1008 // we just need to be in math mode to enable that
1009 case LFUN_MATH_SIZE:
1010 case LFUN_MATH_SPACE:
1011 case LFUN_MATH_LIMITS:
1012 case LFUN_MATH_NONUMBER:
1013 case LFUN_MATH_NUMBER:
1014 case LFUN_MATH_EXTERN:
1018 case LFUN_FONT_FRAK:
1019 flag.enabled(currentMode() != TEXT_MODE);
1022 case LFUN_MATH_INSERT: {
1023 bool const textarg =
1024 arg == "\\textbf" || arg == "\\textsf" ||
1025 arg == "\\textrm" || arg == "\\textmd" ||
1026 arg == "\\textit" || arg == "\\textsc" ||
1027 arg == "\\textsl" || arg == "\\textup" ||
1028 arg == "\\texttt" || arg == "\\textbb" ||
1029 arg == "\\textnormal";
1030 flag.enabled(currentMode() != TEXT_MODE || textarg);
1034 case LFUN_MATH_MATRIX:
1035 flag.enabled(currentMode() == MATH_MODE);
1038 case LFUN_INSET_INSERT: {
1039 // Don't test createMathInset_fromDialogStr(), since
1040 // getStatus is not called with a valid reference and the
1041 // dialog would not be applyable.
1042 string const name = cmd.getArg(0);
1043 flag.enabled(name == "ref");
1047 case LFUN_MATH_DELIM:
1048 case LFUN_MATH_BIGDELIM:
1049 // Don't do this with multi-cell selections
1050 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1061 void InsetMathNest::edit(LCursor & cur, bool left)
1064 cur.idx() = left ? 0 : cur.lastidx();
1065 cur.pos() = left ? 0 : cur.lastpos();
1067 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1071 InsetBase * InsetMathNest::editXY(LCursor & cur, int x, int y)
1074 int dist_min = 1000000;
1075 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1076 int const d = cell(i).dist(x, y);
1082 MathArray & ar = cell(idx_min);
1084 cur.idx() = idx_min;
1085 cur.pos() = ar.x2pos(x - ar.xo());
1086 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1087 if (dist_min == 0) {
1089 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1090 if (ar[i]->covers(x, y))
1091 return ar[i].nucleus()->editXY(cur, x, y);
1097 void InsetMathNest::lfunMousePress(LCursor & cur, FuncRequest & cmd)
1099 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1100 BufferView & bv = cur.bv();
1101 if (cmd.button() == mouse_button::button1) {
1102 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1103 bv.mouseSetCursor(cur);
1104 } else if (cmd.button() == mouse_button::button2) {
1106 if (cur.selection())
1107 asArray(lyx::to_utf8(bv.cursor().selectionAsString(false)), ar);
1109 asArray(lyx::to_utf8(theSelection().get()), ar);
1112 bv.mouseSetCursor(cur);
1117 void InsetMathNest::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
1119 // only select with button 1
1120 if (cmd.button() == mouse_button::button1) {
1121 LCursor & bvcur = cur.bv().cursor();
1122 if (bvcur.anchor_.hasPart(cur)) {
1123 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1124 bvcur.setCursor(cur);
1125 bvcur.selection() = true;
1126 //lyxerr << "MOTION " << bvcur << endl;
1135 void InsetMathNest::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
1137 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1139 if (cmd.button() == mouse_button::button1) {
1140 //theSelection().put(cur.grabSelection());
1144 if (cmd.button() == mouse_button::button3) {
1145 // try to dispatch to enclosed insets first
1146 cur.bv().showDialog("mathpanel");
1154 bool InsetMathNest::interpret(LCursor & cur, char c)
1156 //lyxerr << "interpret 2: '" << c << "'" << endl;
1157 string save_selection;
1158 if (c == '^' || c == '_')
1159 save_selection = grabAndEraseSelection(cur);
1164 if (cur.inMacroMode()) {
1165 string name = cur.macroName();
1167 /// are we currently typing '#1' or '#2' or...?
1168 if (name == "\\#") {
1171 if (n >= 1 && n <= 9)
1172 cur.insert(new MathMacroArgument(n));
1177 cur.activeMacro()->setName(name + c);
1181 // handle 'special char' macros
1186 if (currentMode() == InsetMath::TEXT_MODE)
1187 cur.niceInsert(createInsetMath("textbackslash"));
1189 cur.niceInsert(createInsetMath("backslash"));
1190 } else if (c == '{') {
1192 cur.niceInsert(MathAtom(new InsetMathBrace));
1193 } else if (c == '%') {
1195 cur.niceInsert(MathAtom(new InsetMathComment));
1196 } else if (c == '#') {
1197 BOOST_ASSERT(cur.activeMacro());
1198 cur.activeMacro()->setName(name + c);
1201 cur.niceInsert(createInsetMath(string(1, c)));
1206 // One character big delimiters. The others are handled in
1207 // the other interpret() method.
1208 latexkeys const * l = in_word_set(name.substr(1));
1209 if (name[0] == '\\' && l && l->inset == "big") {
1219 delim = string(1, c);
1222 if (InsetMathBig::isBigInsetDelim(delim)) {
1223 // name + delim ared a valid InsetMathBig.
1224 // We can't use cur.macroModeClose() because
1225 // it does not handle delim.
1226 InsetMathUnknown * p = cur.activeMacro();
1229 cur.cell().erase(cur.pos());
1230 cur.plainInsert(MathAtom(
1231 new InsetMathBig(name.substr(1), delim)));
1236 // leave macro mode and try again if necessary
1237 cur.macroModeClose();
1239 cur.niceInsert(MathAtom(new InsetMathBrace));
1245 // This is annoying as one has to press <space> far too often.
1249 // leave autocorrect mode if necessary
1250 if (autocorrect() && c == ' ') {
1251 autocorrect() = false;
1256 // just clear selection on pressing the space bar
1257 if (cur.selection() && c == ' ') {
1258 cur.selection() = false;
1265 //lyxerr << "starting with macro" << endl;
1266 cur.insert(MathAtom(new InsetMathUnknown("\\", false)));
1271 if (currentMode() == InsetMath::TEXT_MODE)
1277 if (currentMode() == InsetMath::TEXT_MODE) {
1278 // insert spaces in text mode,
1279 // but suppress direct insertion of two spaces in a row
1280 // the still allows typing '<space>a<space>' and deleting the 'a', but
1281 // it is better than nothing...
1282 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
1286 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1287 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1292 // if are at the very end, leave the formula
1293 return cur.pos() != cur.lastpos();
1296 // These shouldn't work in text mode:
1297 if (currentMode() != InsetMath::TEXT_MODE) {
1299 script(cur, false, save_selection);
1303 script(cur, true, save_selection);
1307 cur.niceInsert(createInsetMath("sim"));
1312 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1313 c == '%' || c == '_' || c == '^') {
1314 cur.niceInsert(createInsetMath(string(1, c)));
1319 // try auto-correction
1320 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1323 // no special circumstances, so insert the character without any fuss
1325 cur.autocorrect() = true;
1330 bool InsetMathNest::interpret(LCursor & cur, string const & str)
1332 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1334 if (!cur.empty() && cur.pos() > 0 &&
1335 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1336 if (InsetMathBig::isBigInsetDelim(str)) {
1337 string prev = asString(cur.cell()[cur.pos() - 1]);
1338 if (prev[0] == '\\') {
1339 prev = prev.substr(1);
1340 latexkeys const * l = in_word_set(prev);
1341 if (l && l->inset == "big") {
1342 cur.cell()[cur.pos() - 1] =
1343 MathAtom(new InsetMathBig(prev, str));
1353 bool InsetMathNest::script(LCursor & cur, bool up, string const &
1356 // Hack to get \^ and \_ working
1357 //lyxerr << "handling script: up: " << up << endl;
1358 if (cur.inMacroMode() && cur.macroName() == "\\") {
1360 cur.niceInsert(createInsetMath("mathcircumflex"));
1362 interpret(cur, '_');
1366 cur.macroModeClose();
1367 if (asScriptInset() && cur.idx() == 0) {
1368 // we are in a nucleus of a script inset, move to _our_ script
1369 InsetMathScript * inset = asScriptInset();
1370 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1372 cur.idx() = inset->idxOfScript(up);
1374 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1376 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1379 cur.idx() = inset->idxOfScript(up);
1380 cur.pos() = cur.lastpos();
1382 // convert the thing to our left to a scriptinset or create a new
1383 // one if in the very first position of the array
1384 if (cur.pos() == 0) {
1385 //lyxerr << "new scriptinset" << endl;
1386 cur.insert(new InsetMathScript(up));
1388 //lyxerr << "converting prev atom " << endl;
1389 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1392 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1393 // special handling of {}-bases
1394 // is this always correct?
1395 if (inset->nuc().size() == 1
1396 && inset->nuc().back()->asBraceInset())
1397 inset->nuc() = inset->nuc().back()->asNestInset()->cell(0);
1403 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1404 cur.niceInsert(save_selection);
1406 //lyxerr << "inserting selection 2:\n" << save_selection << endl;