2 * \file InsetMathNest.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetMathNest.h"
15 #include "InsetMathArray.h"
16 #include "InsetMathBig.h"
17 #include "InsetMathBox.h"
18 #include "InsetMathBrace.h"
19 #include "InsetMathColor.h"
20 #include "InsetMathComment.h"
22 #include "InsetMathDelim.h"
23 #include "MathFactory.h"
24 #include "InsetMathHull.h"
25 #include "MathStream.h"
26 #include "MathMacroArgument.h"
27 //#include "InsetMathMBox.h"
28 #include "MathParser.h"
29 #include "InsetMathScript.h"
30 #include "InsetMathSpace.h"
31 #include "InsetMathSymbol.h"
32 #include "MathSupport.h"
33 #include "InsetMathUnknown.h"
34 #include "InsetMathRef.h"
36 #include "BufferView.h"
37 #include "CutAndPaste.h"
38 #include "FuncStatus.h"
40 #include "bufferview_funcs.h"
41 #include "coordcache.h"
44 #include "dispatchresult.h"
45 #include "funcrequest.h"
48 #include "outputparams.h"
51 #include "support/lstrings.h"
53 #include "frontends/Painter.h"
54 #include "frontends/Selection.h"
56 #include "funcrequest.h"
57 #include "lyxserver.h"
58 #include "lyxsocket.h"
65 using cap::copySelection;
66 using cap::grabAndEraseSelection;
67 using cap::cutSelection;
68 using cap::replaceSelection;
69 using cap::selClearOrDel;
73 using std::istringstream;
76 InsetMathNest::InsetMathNest(idx_type nargs)
77 : cells_(nargs), lock_(false)
81 InsetMath::idx_type InsetMathNest::nargs() const
87 MathArray & InsetMathNest::cell(idx_type i)
93 MathArray const & InsetMathNest::cell(idx_type i) const
99 void InsetMathNest::cursorPos(BufferView const & bv,
100 CursorSlice const & sl, bool /*boundary*/,
101 int & x, int & y) const
103 // FIXME: This is a hack. Ideally, the coord cache should not store
104 // absolute positions, but relative ones. This would mean to call
105 // setXY() not in MathArray::draw(), but in the parent insets' draw()
106 // with the correctly adjusted x,y values. But this means that we'd have
107 // to touch all (math)inset's draw() methods. Right now, we'll store
108 // absolute value, and make them here relative, only to make them
109 // absolute again when actually drawing the cursor. What a mess.
110 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
111 MathArray const & ar = sl.cell();
112 CoordCache const & coord_cache = bv.coordCache();
113 if (!coord_cache.getArrays().has(&ar)) {
114 // this can (semi-)legally happen if we just created this cell
115 // and it never has been drawn before. So don't ASSERT.
116 //lyxerr << "no cached data for array " << &ar << endl;
121 Point const pt = coord_cache.getArrays().xy(&ar);
122 if (!coord_cache.getInsets().has(this)) {
124 //lyxerr << "no cached data for inset " << this << endl;
129 Point const pt2 = coord_cache.getInsets().xy(this);
130 //lyxerr << "retrieving position cache for MathArray "
131 // << pt.x_ << ' ' << pt.y_ << std::endl;
132 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
134 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
135 // << " asc: " << ascent() << " des: " << descent()
136 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
137 // move cursor visually into empty cells ("blue rectangles");
143 void InsetMathNest::metrics(MetricsInfo const & mi) const
146 for (idx_type i = 0, n = nargs(); i != n; ++i)
151 bool InsetMathNest::idxNext(LCursor & cur) const
153 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
154 if (cur.idx() == cur.lastidx())
162 bool InsetMathNest::idxRight(LCursor & cur) const
168 bool InsetMathNest::idxPrev(LCursor & cur) const
170 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
174 cur.pos() = cur.lastpos();
179 bool InsetMathNest::idxLeft(LCursor & cur) const
185 bool InsetMathNest::idxFirst(LCursor & cur) const
187 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
196 bool InsetMathNest::idxLast(LCursor & cur) const
198 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
201 cur.idx() = cur.lastidx();
202 cur.pos() = cur.lastpos();
207 void InsetMathNest::dump() const
209 odocstringstream oss;
211 os << "---------------------------------------------\n";
214 for (idx_type i = 0, n = nargs(); i != n; ++i)
215 os << cell(i) << "\n";
216 os << "---------------------------------------------\n";
217 lyxerr << to_utf8(oss.str());
221 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
225 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
228 setPosCache(pi, x, y);
232 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
234 BufferView & bv = *pi.base.bv;
235 // this should use the x/y values given, not the cached values
236 LCursor & cur = bv.cursor();
237 if (!cur.selection())
239 if (!ptr_cmp(&cur.inset(), this))
242 // FIXME: hack to get position cache warm
243 pi.pain.setDrawingEnabled(false);
245 pi.pain.setDrawingEnabled(true);
247 CursorSlice s1 = cur.selBegin();
248 CursorSlice s2 = cur.selEnd();
250 //lyxerr << "InsetMathNest::drawing selection: "
251 // << " s1: " << s1 << " s2: " << s2 << endl;
252 if (s1.idx() == s2.idx()) {
253 MathArray const & c = cell(s1.idx());
254 int x1 = c.xo(bv) + c.pos2x(s1.pos());
255 int y1 = c.yo(bv) - c.ascent();
256 int x2 = c.xo(bv) + c.pos2x(s2.pos());
257 int y2 = c.yo(bv) + c.descent();
258 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
259 //lyxerr << "InsetMathNest::drawing selection 3: "
260 // << " x1: " << x1 << " x2: " << x2
261 // << " y1: " << y1 << " y2: " << y2 << endl;
263 for (idx_type i = 0; i < nargs(); ++i) {
264 if (idxBetween(i, s1.idx(), s2.idx())) {
265 MathArray const & c = cell(i);
267 int y1 = c.yo(bv) - c.ascent();
268 int x2 = c.xo(bv) + c.width();
269 int y2 = c.yo(bv) + c.descent();
270 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
277 void InsetMathNest::validate(LaTeXFeatures & features) const
279 for (idx_type i = 0; i < nargs(); ++i)
280 cell(i).validate(features);
284 void InsetMathNest::replace(ReplaceData & rep)
286 for (idx_type i = 0; i < nargs(); ++i)
287 cell(i).replace(rep);
291 bool InsetMathNest::contains(MathArray const & ar) const
293 for (idx_type i = 0; i < nargs(); ++i)
294 if (cell(i).contains(ar))
300 bool InsetMathNest::lock() const
306 void InsetMathNest::lock(bool l)
312 bool InsetMathNest::isActive() const
318 MathArray InsetMathNest::glue() const
321 for (size_t i = 0; i < nargs(); ++i)
327 void InsetMathNest::write(WriteStream & os) const
329 os << '\\' << name().c_str();
330 for (size_t i = 0; i < nargs(); ++i)
331 os << '{' << cell(i) << '}';
333 os.pendingSpace(true);
334 if (lock_ && !os.latex()) {
336 os.pendingSpace(true);
341 void InsetMathNest::normalize(NormalStream & os) const
343 os << '[' << name().c_str();
344 for (size_t i = 0; i < nargs(); ++i)
345 os << ' ' << cell(i);
350 int InsetMathNest::latex(Buffer const &, odocstream & os,
351 OutputParams const & runparams) const
353 WriteStream wi(os, runparams.moving_arg, true);
359 bool InsetMathNest::notifyCursorLeaves(LCursor & /*cur*/)
365 MathArray & ar = cur.cell();
366 // remove base-only "scripts"
367 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
368 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
369 if (p && p->nargs() == 1) {
370 MathArray ar = p->nuc();
373 cur.adjust(i, ar.size() - 1);
377 // glue adjacent font insets of the same kind
378 for (pos_type i = 0; i + 1 < size(); ++i) {
379 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
380 InsetMathFont const * q = operator[](i + 1)->asFontInset();
381 if (p && q && p->name() == q->name()) {
382 p->cell(0).append(q->cell(0));
392 void InsetMathNest::handleFont
393 (LCursor & cur, docstring const & arg, char const * const font)
395 handleFont(cur, arg, from_ascii(font));
399 void InsetMathNest::handleFont
400 (LCursor & cur, docstring const & arg, docstring const & font)
402 // this whole function is a hack and won't work for incremental font
404 recordUndo(cur, Undo::ATOMIC);
406 if (cur.inset().asInsetMath()->name() == font)
407 cur.handleFont(to_utf8(font));
409 cur.handleNest(createInsetMath(font));
415 void InsetMathNest::handleFont2(LCursor & cur, docstring const & arg)
417 recordUndo(cur, Undo::ATOMIC);
420 bv_funcs::string2font(to_utf8(arg), font, b);
421 if (font.color() != LColor::inherit) {
422 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
423 cur.handleNest(at, 0);
428 void InsetMathNest::doDispatch(LCursor & cur, FuncRequest & cmd)
430 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
431 //CursorSlice sl = cur.current();
433 switch (cmd.action) {
437 cur.message(_("Paste"));
438 replaceSelection(cur);
440 istringstream is(to_utf8(cmd.argument()));
442 docstring const selection = cap::getSelection(cur.buffer(), n);
443 cur.niceInsert(selection);
444 cur.clearSelection(); // bug 393
445 cur.bv().switchKeyMap();
452 cutSelection(cur, true, true);
453 cur.message(_("Cut"));
454 // Prevent stale position >= size crash
455 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
462 cur.message(_("Copy"));
465 case LFUN_MOUSE_PRESS:
466 lfunMousePress(cur, cmd);
469 case LFUN_MOUSE_MOTION:
470 lfunMouseMotion(cur, cmd);
473 case LFUN_MOUSE_RELEASE:
474 lfunMouseRelease(cur, cmd);
477 case LFUN_FINISHED_LEFT:
478 cur.bv().cursor() = cur;
481 case LFUN_FINISHED_RIGHT:
483 cur.bv().cursor() = cur;
486 case LFUN_FINISHED_UP:
487 cur.bv().cursor() = cur;
490 case LFUN_FINISHED_DOWN:
492 cur.bv().cursor() = cur;
495 case LFUN_CHAR_FORWARD_SELECT:
496 case LFUN_CHAR_FORWARD:
497 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
498 cur.autocorrect() = false;
500 cur.macroModeClose();
501 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
502 cur.pushLeft(*cur.nextAtom().nucleus());
503 cur.inset().idxFirst(cur);
504 } else if (cur.posRight() || idxRight(cur)
505 || cur.popRight() || cur.selection())
508 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
513 case LFUN_CHAR_BACKWARD_SELECT:
514 case LFUN_CHAR_BACKWARD:
515 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
516 cur.autocorrect() = false;
518 cur.macroModeClose();
519 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
521 cur.push(*cur.nextAtom().nucleus());
522 cur.inset().idxLast(cur);
523 } else if (cur.posLeft() || idxLeft(cur)
524 || cur.popLeft() || cur.selection())
527 cmd = FuncRequest(LFUN_FINISHED_LEFT);
534 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
535 if (cur.inMacroMode()) {
537 cur.macroModeClose();
540 cur.selHandle(cmd.action == LFUN_UP_SELECT);
542 cmd = FuncRequest(LFUN_FINISHED_UP);
545 // fixes bug 1598. Please check!
549 case LFUN_DOWN_SELECT:
551 if (cur.inMacroMode()) {
552 cur.macroModeClose();
555 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
557 cmd = FuncRequest(LFUN_FINISHED_DOWN);
560 // fixes bug 1598. Please check!
564 case LFUN_MOUSE_DOUBLE:
565 case LFUN_MOUSE_TRIPLE:
566 case LFUN_WORD_SELECT:
570 cur.selection() = true;
571 cur.pos() = cur.lastpos();
572 cur.idx() = cur.lastidx();
575 case LFUN_PARAGRAPH_UP_SELECT:
576 case LFUN_PARAGRAPH_UP:
577 case LFUN_PARAGRAPH_DOWN_SELECT:
578 case LFUN_PARAGRAPH_DOWN:
581 case LFUN_LINE_BEGIN_SELECT:
582 case LFUN_LINE_BEGIN:
583 case LFUN_WORD_BACKWARD_SELECT:
584 case LFUN_WORD_BACKWARD:
585 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
586 cmd.action == LFUN_LINE_BEGIN_SELECT);
587 cur.macroModeClose();
588 if (cur.pos() != 0) {
590 } else if (cur.col() != 0) {
591 cur.idx() -= cur.col();
593 } else if (cur.idx() != 0) {
597 cmd = FuncRequest(LFUN_FINISHED_LEFT);
602 case LFUN_WORD_FORWARD_SELECT:
603 case LFUN_WORD_FORWARD:
604 case LFUN_LINE_END_SELECT:
606 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
607 cmd.action == LFUN_LINE_END_SELECT);
608 cur.macroModeClose();
610 if (cur.pos() != cur.lastpos()) {
611 cur.pos() = cur.lastpos();
612 } else if (ncols() && (cur.col() != cur.lastcol())) {
613 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
614 cur.pos() = cur.lastpos();
615 } else if (cur.idx() != cur.lastidx()) {
616 cur.idx() = cur.lastidx();
617 cur.pos() = cur.lastpos();
619 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
624 case LFUN_SCREEN_UP_SELECT:
626 cmd = FuncRequest(LFUN_FINISHED_LEFT);
630 case LFUN_SCREEN_DOWN_SELECT:
631 case LFUN_SCREEN_DOWN:
632 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
636 case LFUN_CELL_FORWARD:
637 cur.inset().idxNext(cur);
640 case LFUN_CELL_BACKWARD:
641 cur.inset().idxPrev(cur);
644 case LFUN_WORD_DELETE_BACKWARD:
645 case LFUN_CHAR_DELETE_BACKWARD:
647 // May affect external cell:
648 recordUndoInset(cur, Undo::ATOMIC);
650 recordUndo(cur, Undo::ATOMIC);
654 case LFUN_WORD_DELETE_FORWARD:
655 case LFUN_CHAR_DELETE_FORWARD:
656 if (cur.pos() == cur.lastpos())
657 // May affect external cell:
658 recordUndoInset(cur, Undo::ATOMIC);
660 recordUndo(cur, Undo::ATOMIC);
666 cur.clearSelection();
668 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
673 case LFUN_INSET_TOGGLE:
679 case LFUN_SELF_INSERT:
680 if (cmd.argument().size() != 1) {
682 docstring const arg = cmd.argument();
683 if (!interpretString(cur, arg))
687 // Don't record undo steps if we are in macro mode and
688 // cmd.argument is the next character of the macro name.
689 // Otherwise we'll get an invalid cursor if we undo after
690 // the macro was finished and the macro is a known command,
691 // e.g. sqrt. LCursor::macroModeClose replaces in this case
692 // the InsetMathUnknown with name "frac" by an empty
693 // InsetMathFrac -> a pos value > 0 is invalid.
694 // A side effect is that an undo before the macro is finished
695 // undoes the complete macro, not only the last character.
696 if (!cur.inMacroMode())
699 // spacial handling of space. If we insert an inset
700 // via macro mode, we want to put the cursor inside it
701 // if relevant. Think typing "\frac<space>".
702 if (cmd.argument()[0] == ' '
703 && cur.inMacroMode() && cur.macroName() != "\\"
704 && cur.macroModeClose()) {
705 MathAtom const atom = cur.prevAtom();
706 if (atom->asNestInset() && atom->nargs() > 0) {
708 cur.pushLeft(*cur.nextInset());
710 } else if (!interpretChar(cur, cmd.argument()[0])) {
711 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
716 //case LFUN_SERVER_GET_XY:
717 // sprintf(dispatch_buffer, "%d %d",);
720 case LFUN_SERVER_SET_XY: {
721 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
724 istringstream is(to_utf8(cmd.argument()));
726 cur.setScreenPos(x, y);
730 // Special casing for superscript in case of LyX handling
732 case LFUN_ACCENT_CIRCUMFLEX:
733 if (cmd.argument().empty()) {
734 // do superscript if LyX handles
736 recordUndo(cur, Undo::ATOMIC);
737 script(cur, true, grabAndEraseSelection(cur));
741 case LFUN_ACCENT_UMLAUT:
742 case LFUN_ACCENT_ACUTE:
743 case LFUN_ACCENT_GRAVE:
744 case LFUN_ACCENT_BREVE:
745 case LFUN_ACCENT_DOT:
746 case LFUN_ACCENT_MACRON:
747 case LFUN_ACCENT_CARON:
748 case LFUN_ACCENT_TILDE:
749 case LFUN_ACCENT_CEDILLA:
750 case LFUN_ACCENT_CIRCLE:
751 case LFUN_ACCENT_UNDERDOT:
752 case LFUN_ACCENT_TIE:
753 case LFUN_ACCENT_OGONEK:
754 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
758 case LFUN_FONT_FREE_APPLY:
759 case LFUN_FONT_FREE_UPDATE:
760 handleFont2(cur, cmd.argument());
764 if (currentMode() == TEXT_MODE)
765 handleFont(cur, cmd.argument(), "textbf");
767 handleFont(cur, cmd.argument(), "mathbf");
770 if (currentMode() == TEXT_MODE)
771 handleFont(cur, cmd.argument(), "textsf");
773 handleFont(cur, cmd.argument(), "mathsf");
776 if (currentMode() == TEXT_MODE)
777 handleFont(cur, cmd.argument(), "emph");
779 handleFont(cur, cmd.argument(), "mathcal");
781 case LFUN_FONT_ROMAN:
782 if (currentMode() == TEXT_MODE)
783 handleFont(cur, cmd.argument(), "textrm");
785 handleFont(cur, cmd.argument(), "mathrm");
788 if (currentMode() == TEXT_MODE)
789 handleFont(cur, cmd.argument(), "texttt");
791 handleFont(cur, cmd.argument(), "mathtt");
794 handleFont(cur, cmd.argument(), "mathfrak");
797 if (currentMode() == TEXT_MODE)
798 handleFont(cur, cmd.argument(), "textit");
800 handleFont(cur, cmd.argument(), "mathit");
803 if (currentMode() == TEXT_MODE)
804 // FIXME: should be "noun"
805 handleFont(cur, cmd.argument(), "textsc");
807 handleFont(cur, cmd.argument(), "mathbb");
809 //case LFUN_FONT_FREE_APPLY:
810 handleFont(cur, cmd.argument(), "textrm");
812 case LFUN_FONT_DEFAULT:
813 handleFont(cur, cmd.argument(), "textnormal");
816 case LFUN_MATH_MODE: {
818 // ignore math-mode on when already in math mode
819 if (currentMode() == InsetBase::MATH_MODE && cmd.argument() == "on")
821 cur.macroModeClose();
822 docstring const save_selection = grabAndEraseSelection(cur);
824 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
825 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
827 cur.pushLeft(*cur.nextInset());
828 cur.niceInsert(save_selection);
830 if (currentMode() == InsetBase::TEXT_MODE) {
831 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
832 cur.message(_("create new math text environment ($...$)"));
834 handleFont(cur, cmd.argument(), "textrm");
835 cur.message(_("entered math text mode (textrm)"));
848 case LFUN_MATH_MATRIX: {
849 recordUndo(cur, Undo::ATOMIC);
854 idocstringstream is(cmd.argument());
855 is >> m >> n >> v_align >> h_align;
862 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
866 case LFUN_MATH_DELIM: {
868 docstring rs = support::split(cmd.argument(), ls, ' ');
869 // Reasonable default values
874 recordUndo(cur, Undo::ATOMIC);
875 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
879 case LFUN_MATH_BIGDELIM: {
880 docstring const lname = from_utf8(cmd.getArg(0));
881 docstring const ldelim = from_utf8(cmd.getArg(1));
882 docstring const rname = from_utf8(cmd.getArg(2));
883 docstring const rdelim = from_utf8(cmd.getArg(3));
884 latexkeys const * l = in_word_set(lname);
885 bool const have_l = l && l->inset == "big" &&
886 InsetMathBig::isBigInsetDelim(ldelim);
887 l = in_word_set(rname);
888 bool const have_r = l && l->inset == "big" &&
889 InsetMathBig::isBigInsetDelim(rdelim);
890 // We mimic LFUN_MATH_DELIM in case we have an empty left
891 // or right delimiter.
892 if (have_l || have_r) {
893 recordUndo(cur, Undo::ATOMIC);
894 docstring const selection = grabAndEraseSelection(cur);
897 cur.insert(MathAtom(new InsetMathBig(lname,
899 cur.niceInsert(selection);
901 cur.insert(MathAtom(new InsetMathBig(rname,
904 // Don't call cur.undispatched() if we did nothing, this would
905 // lead to infinite recursion via LyXText::dispatch().
909 case LFUN_SPACE_INSERT:
910 case LFUN_MATH_SPACE:
911 recordUndo(cur, Undo::ATOMIC);
912 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
915 case LFUN_ERT_INSERT:
916 // interpret this as if a backslash was typed
917 recordUndo(cur, Undo::ATOMIC);
918 interpretChar(cur, '\\');
921 case LFUN_MATH_SUBSCRIPT:
922 // interpret this as if a _ was typed
923 recordUndo(cur, Undo::ATOMIC);
924 interpretChar(cur, '_');
927 case LFUN_MATH_SUPERSCRIPT:
928 // interpret this as if a ^ was typed
929 recordUndo(cur, Undo::ATOMIC);
930 interpretChar(cur, '^');
933 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
934 // handling such that "self-insert" works on "arbitrary stuff" too, and
935 // math-insert only handles special math things like "matrix".
936 case LFUN_MATH_INSERT: {
937 recordUndo(cur, Undo::ATOMIC);
938 if (cmd.argument() == "^" || cmd.argument() == "_") {
939 interpretChar(cur, cmd.argument()[0]);
941 cur.niceInsert(cmd.argument());
945 case LFUN_DIALOG_SHOW_NEW_INSET: {
946 docstring const & name = cmd.argument();
950 data = tmp.createDialogStr(to_utf8(name));
952 cur.bv().showInsetDialog(to_utf8(name), data, 0);
956 case LFUN_INSET_INSERT: {
958 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
967 InsetMathDim::doDispatch(cur, cmd);
973 bool InsetMathNest::getStatus(LCursor & cur, FuncRequest const & cmd,
974 FuncStatus & flag) const
976 // the font related toggles
977 //string tc = "mathnormal";
979 string const arg = to_utf8(cmd.argument());
980 switch (cmd.action) {
981 case LFUN_TABULAR_FEATURE:
985 case LFUN_TABULAR_FEATURE:
986 // FIXME: check temporarily disabled
988 char align = mathcursor::valign();
993 if (cmd.argument().empty()) {
997 if (!contains("tcb", cmd.argument()[0])) {
1001 flag.setOnOff(cmd.argument()[0] == align);
1004 /// We have to handle them since 1.4 blocks all unhandled actions
1005 case LFUN_FONT_ITAL:
1006 case LFUN_FONT_BOLD:
1007 case LFUN_FONT_SANS:
1008 case LFUN_FONT_EMPH:
1009 case LFUN_FONT_CODE:
1010 case LFUN_FONT_NOUN:
1011 case LFUN_FONT_ROMAN:
1012 case LFUN_FONT_DEFAULT:
1015 case LFUN_MATH_MUTATE:
1016 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1017 flag.setOnOff(false);
1020 // we just need to be in math mode to enable that
1021 case LFUN_MATH_SIZE:
1022 case LFUN_MATH_SPACE:
1023 case LFUN_MATH_LIMITS:
1024 case LFUN_MATH_NONUMBER:
1025 case LFUN_MATH_NUMBER:
1026 case LFUN_MATH_EXTERN:
1030 case LFUN_FONT_FRAK:
1031 flag.enabled(currentMode() != TEXT_MODE);
1034 case LFUN_MATH_INSERT: {
1035 bool const textarg =
1036 arg == "\\textbf" || arg == "\\textsf" ||
1037 arg == "\\textrm" || arg == "\\textmd" ||
1038 arg == "\\textit" || arg == "\\textsc" ||
1039 arg == "\\textsl" || arg == "\\textup" ||
1040 arg == "\\texttt" || arg == "\\textbb" ||
1041 arg == "\\textnormal";
1042 flag.enabled(currentMode() != TEXT_MODE || textarg);
1046 case LFUN_MATH_MATRIX:
1047 flag.enabled(currentMode() == MATH_MODE);
1050 case LFUN_INSET_INSERT: {
1051 // Don't test createMathInset_fromDialogStr(), since
1052 // getStatus is not called with a valid reference and the
1053 // dialog would not be applyable.
1054 string const name = cmd.getArg(0);
1055 flag.enabled(name == "ref");
1059 case LFUN_MATH_DELIM:
1060 case LFUN_MATH_BIGDELIM:
1061 // Don't do this with multi-cell selections
1062 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1073 void InsetMathNest::edit(LCursor & cur, bool left)
1076 cur.idx() = left ? 0 : cur.lastidx();
1077 cur.pos() = left ? 0 : cur.lastpos();
1079 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1083 InsetBase * InsetMathNest::editXY(LCursor & cur, int x, int y)
1086 int dist_min = 1000000;
1087 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1088 int const d = cell(i).dist(cur.bv(), x, y);
1094 MathArray & ar = cell(idx_min);
1096 cur.idx() = idx_min;
1097 cur.pos() = ar.x2pos(x - ar.xo(cur.bv()));
1099 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1100 if (dist_min == 0) {
1102 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1103 if (ar[i]->covers(cur.bv(), x, y))
1104 return ar[i].nucleus()->editXY(cur, x, y);
1110 void InsetMathNest::lfunMousePress(LCursor & cur, FuncRequest & cmd)
1112 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1113 BufferView & bv = cur.bv();
1114 if (cmd.button() == mouse_button::button1) {
1115 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1116 bv.mouseSetCursor(cur);
1117 } else if (cmd.button() == mouse_button::button2) {
1119 if (cur.selection())
1120 asArray(bv.cursor().selectionAsString(false), ar);
1122 asArray(theSelection().get(), ar);
1125 bv.mouseSetCursor(cur);
1130 void InsetMathNest::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
1132 // only select with button 1
1133 if (cmd.button() == mouse_button::button1) {
1134 LCursor & bvcur = cur.bv().cursor();
1135 if (bvcur.anchor_.hasPart(cur)) {
1136 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1137 bvcur.setCursor(cur);
1138 bvcur.selection() = true;
1139 //lyxerr << "MOTION " << bvcur << endl;
1148 void InsetMathNest::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
1150 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1152 if (cmd.button() == mouse_button::button1) {
1153 //theSelection().put(cur.grabSelection());
1157 if (cmd.button() == mouse_button::button3) {
1158 // try to dispatch to enclosed insets first
1159 cur.bv().showDialog("mathpanel");
1167 bool InsetMathNest::interpretChar(LCursor & cur, char_type c)
1169 //lyxerr << "interpret 2: '" << c << "'" << endl;
1170 docstring save_selection;
1171 if (c == '^' || c == '_')
1172 save_selection = grabAndEraseSelection(cur);
1177 if (cur.inMacroMode()) {
1178 docstring name = cur.macroName();
1180 /// are we currently typing '#1' or '#2' or...?
1181 if (name == "\\#") {
1184 if (n >= 1 && n <= 9)
1185 cur.insert(new MathMacroArgument(n));
1190 cur.activeMacro()->setName(name + docstring(1, c));
1194 // handle 'special char' macros
1199 if (currentMode() == InsetMath::TEXT_MODE)
1200 cur.niceInsert(createInsetMath("textbackslash"));
1202 cur.niceInsert(createInsetMath("backslash"));
1203 } else if (c == '{') {
1205 cur.niceInsert(MathAtom(new InsetMathBrace));
1206 } else if (c == '%') {
1208 cur.niceInsert(MathAtom(new InsetMathComment));
1209 } else if (c == '#') {
1210 BOOST_ASSERT(cur.activeMacro());
1211 cur.activeMacro()->setName(name + docstring(1, c));
1214 cur.niceInsert(createInsetMath(docstring(1, c)));
1219 // One character big delimiters. The others are handled in
1220 // interpretString().
1221 latexkeys const * l = in_word_set(name.substr(1));
1222 if (name[0] == '\\' && l && l->inset == "big") {
1226 delim = from_ascii("\\{");
1229 delim = from_ascii("\\}");
1232 delim = docstring(1, c);
1235 if (InsetMathBig::isBigInsetDelim(delim)) {
1236 // name + delim ared a valid InsetMathBig.
1237 // We can't use cur.macroModeClose() because
1238 // it does not handle delim.
1239 InsetMathUnknown * p = cur.activeMacro();
1242 cur.cell().erase(cur.pos());
1243 cur.plainInsert(MathAtom(
1244 new InsetMathBig(name.substr(1), delim)));
1249 // leave macro mode and try again if necessary
1250 cur.macroModeClose();
1252 cur.niceInsert(MathAtom(new InsetMathBrace));
1254 interpretChar(cur, c);
1258 // This is annoying as one has to press <space> far too often.
1262 // leave autocorrect mode if necessary
1263 if (autocorrect() && c == ' ') {
1264 autocorrect() = false;
1269 // just clear selection on pressing the space bar
1270 if (cur.selection() && c == ' ') {
1271 cur.selection() = false;
1278 //lyxerr << "starting with macro" << endl;
1279 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), false)));
1284 if (currentMode() == InsetMath::TEXT_MODE)
1290 if (currentMode() == InsetMath::TEXT_MODE) {
1291 // insert spaces in text mode,
1292 // but suppress direct insertion of two spaces in a row
1293 // the still allows typing '<space>a<space>' and deleting the 'a', but
1294 // it is better than nothing...
1295 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
1299 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1300 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1305 // if are at the very end, leave the formula
1306 return cur.pos() != cur.lastpos();
1309 // These shouldn't work in text mode:
1310 if (currentMode() != InsetMath::TEXT_MODE) {
1312 script(cur, false, save_selection);
1316 script(cur, true, save_selection);
1320 cur.niceInsert(createInsetMath("sim"));
1325 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1326 c == '%' || c == '_' || c == '^') {
1327 cur.niceInsert(createInsetMath(docstring(1, c)));
1332 // try auto-correction
1333 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1336 // no special circumstances, so insert the character without any fuss
1338 cur.autocorrect() = true;
1343 bool InsetMathNest::interpretString(LCursor & cur, docstring const & str)
1345 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1347 if (!cur.empty() && cur.pos() > 0 &&
1348 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1349 if (InsetMathBig::isBigInsetDelim(str)) {
1350 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1351 if (prev[0] == '\\') {
1352 prev = prev.substr(1);
1353 latexkeys const * l = in_word_set(prev);
1354 if (l && l->inset == "big") {
1355 cur.cell()[cur.pos() - 1] =
1356 MathAtom(new InsetMathBig(prev, str));
1366 bool InsetMathNest::script(LCursor & cur, bool up,
1367 docstring const & save_selection)
1369 // Hack to get \^ and \_ working
1370 //lyxerr << "handling script: up: " << up << endl;
1371 if (cur.inMacroMode() && cur.macroName() == "\\") {
1373 cur.niceInsert(createInsetMath("mathcircumflex"));
1375 interpretChar(cur, '_');
1379 cur.macroModeClose();
1380 if (asScriptInset() && cur.idx() == 0) {
1381 // we are in a nucleus of a script inset, move to _our_ script
1382 InsetMathScript * inset = asScriptInset();
1383 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1385 cur.idx() = inset->idxOfScript(up);
1387 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1389 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1392 cur.idx() = inset->idxOfScript(up);
1393 cur.pos() = cur.lastpos();
1395 // convert the thing to our left to a scriptinset or create a new
1396 // one if in the very first position of the array
1397 if (cur.pos() == 0) {
1398 //lyxerr << "new scriptinset" << endl;
1399 cur.insert(new InsetMathScript(up));
1401 //lyxerr << "converting prev atom " << endl;
1402 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1405 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1406 // special handling of {}-bases
1407 // is this always correct?
1408 if (inset->nuc().size() == 1
1409 && inset->nuc().back()->asBraceInset())
1410 inset->nuc() = inset->nuc().back()->asNestInset()->cell(0);
1416 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1417 cur.niceInsert(save_selection);
1419 //lyxerr << "inserting selection 2:\n" << save_selection << endl;