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"
63 using lyx::cap::copySelection;
64 using lyx::cap::grabAndEraseSelection;
65 using lyx::cap::cutSelection;
66 using lyx::cap::replaceSelection;
67 using lyx::cap::selClearOrDel;
71 using std::istringstream;
74 InsetMathNest::InsetMathNest(idx_type nargs)
75 : cells_(nargs), lock_(false)
79 InsetMath::idx_type InsetMathNest::nargs() const
85 MathArray & InsetMathNest::cell(idx_type i)
91 MathArray const & InsetMathNest::cell(idx_type i) const
97 void InsetMathNest::cursorPos(CursorSlice const & sl, bool /*boundary*/,
98 int & x, int & y) const
100 // FIXME: This is a hack. Ideally, the coord cache should not store
101 // absolute positions, but relative ones. This would mean to call
102 // setXY() not in MathArray::draw(), but in the parent insets' draw()
103 // with the correctly adjusted x,y values. But this means that we'd have
104 // to touch all (math)inset's draw() methods. Right now, we'll store
105 // absolute value, and make them here relative, only to make them
106 // absolute again when actually drawing the cursor. What a mess.
107 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
108 MathArray const & ar = sl.cell();
109 if (!theCoords.getArrays().has(&ar)) {
110 // this can (semi-)legally happen if we just created this cell
111 // and it never has been drawn before. So don't ASSERT.
112 //lyxerr << "no cached data for array " << &ar << endl;
117 Point const pt = theCoords.getArrays().xy(&ar);
118 if (!theCoords.getInsets().has(this)) {
120 //lyxerr << "no cached data for inset " << this << endl;
125 Point const pt2 = theCoords.getInsets().xy(this);
126 //lyxerr << "retrieving position cache for MathArray "
127 // << pt.x_ << ' ' << pt.y_ << std::endl;
128 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
130 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
131 // << " asc: " << ascent() << " des: " << descent()
132 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
133 // move cursor visually into empty cells ("blue rectangles");
139 void InsetMathNest::metrics(MetricsInfo const & mi) const
142 for (idx_type i = 0, n = nargs(); i != n; ++i)
147 bool InsetMathNest::idxNext(LCursor & cur) const
149 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
150 if (cur.idx() == cur.lastidx())
158 bool InsetMathNest::idxRight(LCursor & cur) const
164 bool InsetMathNest::idxPrev(LCursor & cur) const
166 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
170 cur.pos() = cur.lastpos();
175 bool InsetMathNest::idxLeft(LCursor & cur) const
181 bool InsetMathNest::idxFirst(LCursor & cur) const
183 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
192 bool InsetMathNest::idxLast(LCursor & cur) const
194 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
197 cur.idx() = cur.lastidx();
198 cur.pos() = cur.lastpos();
203 void InsetMathNest::dump() const
205 WriteStream os(lyxerr);
206 os << "---------------------------------------------\n";
209 for (idx_type i = 0, n = nargs(); i != n; ++i)
210 os << cell(i) << "\n";
211 os << "---------------------------------------------\n";
215 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
219 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
222 setPosCache(pi, x, y);
226 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
228 // this should use the x/y values given, not the cached values
229 LCursor & cur = pi.base.bv->cursor();
230 if (!cur.selection())
232 if (!ptr_cmp(&cur.inset(), this))
235 // FIXME: hack to get position cache warm
236 static lyx::frontend::NullPainter nop;
237 PainterInfo pinop(pi);
241 CursorSlice s1 = cur.selBegin();
242 CursorSlice s2 = cur.selEnd();
243 //lyxerr << "InsetMathNest::drawing selection: "
244 // << " s1: " << s1 << " s2: " << s2 << endl;
245 if (s1.idx() == s2.idx()) {
246 MathArray const & c = cell(s1.idx());
247 int x1 = c.xo() + c.pos2x(s1.pos());
248 int y1 = c.yo() - c.ascent();
249 int x2 = c.xo() + c.pos2x(s2.pos());
250 int y2 = c.yo() + c.descent();
251 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
252 //lyxerr << "InsetMathNest::drawing selection 3: "
253 // << " x1: " << x1 << " x2: " << x2
254 // << " y1: " << y1 << " y2: " << y2 << endl;
256 for (idx_type i = 0; i < nargs(); ++i) {
257 if (idxBetween(i, s1.idx(), s2.idx())) {
258 MathArray const & c = cell(i);
260 int y1 = c.yo() - c.ascent();
261 int x2 = c.xo() + c.width();
262 int y2 = c.yo() + c.descent();
263 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
270 void InsetMathNest::validate(LaTeXFeatures & features) const
272 for (idx_type i = 0; i < nargs(); ++i)
273 cell(i).validate(features);
277 void InsetMathNest::replace(ReplaceData & rep)
279 for (idx_type i = 0; i < nargs(); ++i)
280 cell(i).replace(rep);
284 bool InsetMathNest::contains(MathArray const & ar) const
286 for (idx_type i = 0; i < nargs(); ++i)
287 if (cell(i).contains(ar))
293 bool InsetMathNest::lock() const
299 void InsetMathNest::lock(bool l)
305 bool InsetMathNest::isActive() const
311 MathArray InsetMathNest::glue() const
314 for (size_t i = 0; i < nargs(); ++i)
320 void InsetMathNest::write(WriteStream & os) const
322 os << '\\' << name().c_str();
323 for (size_t i = 0; i < nargs(); ++i)
324 os << '{' << cell(i) << '}';
326 os.pendingSpace(true);
327 if (lock_ && !os.latex()) {
329 os.pendingSpace(true);
334 void InsetMathNest::normalize(NormalStream & os) const
336 os << '[' << name().c_str();
337 for (size_t i = 0; i < nargs(); ++i)
338 os << ' ' << cell(i);
343 int InsetMathNest::latex(Buffer const &, std::ostream & os,
344 OutputParams const & runparams) const
346 WriteStream wi(os, runparams.moving_arg, true);
352 bool InsetMathNest::notifyCursorLeaves(LCursor & /*cur*/)
358 MathArray & ar = cur.cell();
359 // remove base-only "scripts"
360 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
361 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
362 if (p && p->nargs() == 1) {
363 MathArray ar = p->nuc();
366 cur.adjust(i, ar.size() - 1);
370 // glue adjacent font insets of the same kind
371 for (pos_type i = 0; i + 1 < size(); ++i) {
372 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
373 InsetMathFont const * q = operator[](i + 1)->asFontInset();
374 if (p && q && p->name() == q->name()) {
375 p->cell(0).append(q->cell(0));
385 void InsetMathNest::handleFont
386 (LCursor & cur, string const & arg, string const & font)
388 // this whole function is a hack and won't work for incremental font
390 recordUndo(cur, Undo::ATOMIC);
392 if (cur.inset().asInsetMath()->name() == font)
393 cur.handleFont(font);
395 cur.handleNest(createInsetMath(font));
401 void InsetMathNest::handleFont2(LCursor & cur, string const & arg)
403 recordUndo(cur, Undo::ATOMIC);
406 bv_funcs::string2font(arg, font, b);
407 if (font.color() != LColor::inherit) {
408 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
409 cur.handleNest(at, 0);
414 void InsetMathNest::doDispatch(LCursor & cur, FuncRequest & cmd)
416 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
417 //CursorSlice sl = cur.current();
419 switch (cmd.action) {
423 cur.message(_("Paste"));
424 replaceSelection(cur);
426 istringstream is(lyx::to_utf8(cmd.argument()));
428 docstring const selection = lyx::cap::getSelection(cur.buffer(), n);
430 cur.niceInsert(lyx::to_utf8(selection));
431 cur.clearSelection(); // bug 393
432 cur.bv().switchKeyMap();
439 cutSelection(cur, true, true);
440 cur.message(_("Cut"));
441 // Prevent stale position >= size crash
442 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
449 cur.message(_("Copy"));
452 case LFUN_MOUSE_PRESS:
453 lfunMousePress(cur, cmd);
456 case LFUN_MOUSE_MOTION:
457 lfunMouseMotion(cur, cmd);
460 case LFUN_MOUSE_RELEASE:
461 lfunMouseRelease(cur, cmd);
464 case LFUN_FINISHED_LEFT:
465 cur.bv().cursor() = cur;
468 case LFUN_FINISHED_RIGHT:
470 cur.bv().cursor() = cur;
473 case LFUN_FINISHED_UP:
474 cur.bv().cursor() = cur;
477 case LFUN_FINISHED_DOWN:
479 cur.bv().cursor() = cur;
482 case LFUN_CHAR_FORWARD_SELECT:
483 case LFUN_CHAR_FORWARD:
484 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
485 cur.autocorrect() = false;
487 cur.macroModeClose();
488 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
489 cur.pushLeft(*cur.nextAtom().nucleus());
490 cur.inset().idxFirst(cur);
491 } else if (cur.posRight() || idxRight(cur)
492 || cur.popRight() || cur.selection())
495 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
500 case LFUN_CHAR_BACKWARD_SELECT:
501 case LFUN_CHAR_BACKWARD:
502 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
503 cur.autocorrect() = false;
505 cur.macroModeClose();
506 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
508 cur.push(*cur.nextAtom().nucleus());
509 cur.inset().idxLast(cur);
510 } else if (cur.posLeft() || idxLeft(cur)
511 || cur.popLeft() || cur.selection())
514 cmd = FuncRequest(LFUN_FINISHED_LEFT);
521 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
522 if (cur.inMacroMode()) {
524 cur.macroModeClose();
527 cur.selHandle(cmd.action == LFUN_UP_SELECT);
529 cmd = FuncRequest(LFUN_FINISHED_UP);
532 // fixes bug 1598. Please check!
536 case LFUN_DOWN_SELECT:
538 if (cur.inMacroMode()) {
539 cur.macroModeClose();
542 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
544 cmd = FuncRequest(LFUN_FINISHED_DOWN);
547 // fixes bug 1598. Please check!
551 case LFUN_MOUSE_DOUBLE:
552 case LFUN_MOUSE_TRIPLE:
553 case LFUN_WORD_SELECT:
557 cur.selection() = true;
558 cur.pos() = cur.lastpos();
559 cur.idx() = cur.lastidx();
562 case LFUN_PARAGRAPH_UP_SELECT:
563 case LFUN_PARAGRAPH_UP:
564 case LFUN_PARAGRAPH_DOWN_SELECT:
565 case LFUN_PARAGRAPH_DOWN:
568 case LFUN_LINE_BEGIN_SELECT:
569 case LFUN_LINE_BEGIN:
570 case LFUN_WORD_BACKWARD_SELECT:
571 case LFUN_WORD_BACKWARD:
572 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
573 cmd.action == LFUN_LINE_BEGIN_SELECT);
574 cur.macroModeClose();
575 if (cur.pos() != 0) {
577 } else if (cur.col() != 0) {
578 cur.idx() -= cur.col();
580 } else if (cur.idx() != 0) {
584 cmd = FuncRequest(LFUN_FINISHED_LEFT);
589 case LFUN_WORD_FORWARD_SELECT:
590 case LFUN_WORD_FORWARD:
591 case LFUN_LINE_END_SELECT:
593 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
594 cmd.action == LFUN_LINE_END_SELECT);
595 cur.macroModeClose();
597 if (cur.pos() != cur.lastpos()) {
598 cur.pos() = cur.lastpos();
599 } else if (ncols() && (cur.col() != cur.lastcol())) {
600 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
601 cur.pos() = cur.lastpos();
602 } else if (cur.idx() != cur.lastidx()) {
603 cur.idx() = cur.lastidx();
604 cur.pos() = cur.lastpos();
606 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
611 case LFUN_SCREEN_UP_SELECT:
613 cmd = FuncRequest(LFUN_FINISHED_LEFT);
617 case LFUN_SCREEN_DOWN_SELECT:
618 case LFUN_SCREEN_DOWN:
619 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
623 case LFUN_CELL_FORWARD:
624 cur.inset().idxNext(cur);
627 case LFUN_CELL_BACKWARD:
628 cur.inset().idxPrev(cur);
631 case LFUN_WORD_DELETE_BACKWARD:
632 case LFUN_CHAR_DELETE_BACKWARD:
634 // May affect external cell:
635 recordUndoInset(cur, Undo::ATOMIC);
637 recordUndo(cur, Undo::ATOMIC);
641 case LFUN_WORD_DELETE_FORWARD:
642 case LFUN_CHAR_DELETE_FORWARD:
643 if (cur.pos() == cur.lastpos())
644 // May affect external cell:
645 recordUndoInset(cur, Undo::ATOMIC);
647 recordUndo(cur, Undo::ATOMIC);
653 cur.clearSelection();
655 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
660 case LFUN_INSET_TOGGLE:
666 case LFUN_SELF_INSERT:
667 if (cmd.argument().size() != 1) {
669 string const arg = lyx::to_utf8(cmd.argument());
670 if (!interpret(cur, arg))
674 // Don't record undo steps if we are in macro mode and
675 // cmd.argument is the next character of the macro name.
676 // Otherwise we'll get an invalid cursor if we undo after
677 // the macro was finished and the macro is a known command,
678 // e.g. sqrt. LCursor::macroModeClose replaces in this case
679 // the InsetMathUnknown with name "frac" by an empty
680 // InsetMathFrac -> a pos value > 0 is invalid.
681 // A side effect is that an undo before the macro is finished
682 // undoes the complete macro, not only the last character.
683 if (!cur.inMacroMode())
686 // spacial handling of space. If we insert an inset
687 // via macro mode, we want to put the cursor inside it
688 // if relevant. Think typing "\frac<space>".
689 if (cmd.argument()[0] == ' '
690 && cur.inMacroMode() && cur.macroName() != "\\"
691 && cur.macroModeClose()) {
692 MathAtom const atom = cur.prevAtom();
693 if (atom->asNestInset() && atom->nargs() > 0) {
695 cur.pushLeft(*cur.nextInset());
698 // } else if (!interpret(cur, cmd.argument()[0])) {
699 // when interpret accepts UCS4 characters
700 } else if (!interpret(cur, lyx::to_utf8(cmd.argument()))) {
701 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
706 //case LFUN_SERVER_GET_XY:
707 // sprintf(dispatch_buffer, "%d %d",);
710 case LFUN_SERVER_SET_XY: {
711 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
714 istringstream is(lyx::to_utf8(cmd.argument()));
716 cur.setScreenPos(x, y);
720 // Special casing for superscript in case of LyX handling
722 case LFUN_ACCENT_CIRCUMFLEX:
723 if (cmd.argument().empty()) {
724 // do superscript if LyX handles
726 recordUndo(cur, Undo::ATOMIC);
727 script(cur, true, grabAndEraseSelection(cur));
731 case LFUN_ACCENT_UMLAUT:
732 case LFUN_ACCENT_ACUTE:
733 case LFUN_ACCENT_GRAVE:
734 case LFUN_ACCENT_BREVE:
735 case LFUN_ACCENT_DOT:
736 case LFUN_ACCENT_MACRON:
737 case LFUN_ACCENT_CARON:
738 case LFUN_ACCENT_TILDE:
739 case LFUN_ACCENT_CEDILLA:
740 case LFUN_ACCENT_CIRCLE:
741 case LFUN_ACCENT_UNDERDOT:
742 case LFUN_ACCENT_TIE:
743 case LFUN_ACCENT_OGONEK:
744 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
748 case LFUN_FONT_FREE_APPLY:
749 case LFUN_FONT_FREE_UPDATE:
750 handleFont2(cur, lyx::to_utf8(cmd.argument()));
754 if (currentMode() == TEXT_MODE)
755 handleFont(cur, lyx::to_utf8(cmd.argument()), "textbf");
757 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathbf");
760 if (currentMode() == TEXT_MODE)
761 handleFont(cur, lyx::to_utf8(cmd.argument()), "textsf");
763 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathsf");
766 if (currentMode() == TEXT_MODE)
767 handleFont(cur, lyx::to_utf8(cmd.argument()), "emph");
769 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathcal");
771 case LFUN_FONT_ROMAN:
772 if (currentMode() == TEXT_MODE)
773 handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
775 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathrm");
778 if (currentMode() == TEXT_MODE)
779 handleFont(cur, lyx::to_utf8(cmd.argument()), "texttt");
781 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathtt");
784 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathfrak");
787 if (currentMode() == TEXT_MODE)
788 handleFont(cur, lyx::to_utf8(cmd.argument()), "textit");
790 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathit");
793 if (currentMode() == TEXT_MODE)
794 // FIXME: should be "noun"
795 handleFont(cur, lyx::to_utf8(cmd.argument()), "textsc");
797 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathbb");
799 //case LFUN_FONT_FREE_APPLY:
800 handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
802 case LFUN_FONT_DEFAULT:
803 handleFont(cur, lyx::to_utf8(cmd.argument()), "textnormal");
806 case LFUN_MATH_MODE: {
808 // ignore math-mode on when already in math mode
809 if (currentMode() == InsetBase::MATH_MODE && cmd.argument() == "on")
811 cur.macroModeClose();
812 string const save_selection = grabAndEraseSelection(cur);
814 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
815 cur.plainInsert(MathAtom(new InsetMathBox("mbox")));
817 cur.pushLeft(*cur.nextInset());
818 cur.niceInsert(save_selection);
820 if (currentMode() == InsetBase::TEXT_MODE) {
821 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
822 cur.message(_("create new math text environment ($...$)"));
824 handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
825 cur.message(_("entered math text mode (textrm)"));
838 case LFUN_MATH_MATRIX: {
839 recordUndo(cur, Undo::ATOMIC);
844 istringstream is(lyx::to_utf8(cmd.argument()));
845 is >> m >> n >> v_align >> h_align;
852 MathAtom(new InsetMathArray("array", m, n, v_align[0], h_align)));
856 case LFUN_MATH_DELIM: {
858 string rs = lyx::support::split(lyx::to_utf8(cmd.argument()), ls, ' ');
859 // Reasonable default values
864 recordUndo(cur, Undo::ATOMIC);
865 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
869 case LFUN_MATH_BIGDELIM: {
870 string const lname = cmd.getArg(0);
871 string const ldelim = cmd.getArg(1);
872 string const rname = cmd.getArg(2);
873 string const rdelim = cmd.getArg(3);
874 latexkeys const * l = in_word_set(lname);
875 bool const have_l = l && l->inset == "big" &&
876 InsetMathBig::isBigInsetDelim(ldelim);
877 l = in_word_set(rname);
878 bool const have_r = l && l->inset == "big" &&
879 InsetMathBig::isBigInsetDelim(rdelim);
880 // We mimic LFUN_MATH_DELIM in case we have an empty left
881 // or right delimiter.
882 if (have_l || have_r) {
883 recordUndo(cur, Undo::ATOMIC);
884 string const selection = grabAndEraseSelection(cur);
887 cur.insert(MathAtom(new InsetMathBig(lname,
889 cur.niceInsert(selection);
891 cur.insert(MathAtom(new InsetMathBig(rname,
894 // Don't call cur.undispatched() if we did nothing, this would
895 // lead to infinite recursion via LyXText::dispatch().
899 case LFUN_SPACE_INSERT:
900 case LFUN_MATH_SPACE:
901 recordUndo(cur, Undo::ATOMIC);
902 cur.insert(MathAtom(new InsetMathSpace(",")));
905 case LFUN_ERT_INSERT:
906 // interpret this as if a backslash was typed
907 recordUndo(cur, Undo::ATOMIC);
908 interpret(cur, '\\');
911 case LFUN_MATH_SUBSCRIPT:
912 // interpret this as if a _ was typed
913 recordUndo(cur, Undo::ATOMIC);
917 case LFUN_MATH_SUPERSCRIPT:
918 // interpret this as if a ^ was typed
919 recordUndo(cur, Undo::ATOMIC);
923 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
924 // handling such that "self-insert" works on "arbitrary stuff" too, and
925 // math-insert only handles special math things like "matrix".
926 case LFUN_MATH_INSERT: {
927 recordUndo(cur, Undo::ATOMIC);
928 if (cmd.argument() == "^" || cmd.argument() == "_")
929 interpret(cur, cmd.argument()[0]);
931 cur.niceInsert(lyx::to_utf8(cmd.argument()));
935 case LFUN_DIALOG_SHOW_NEW_INSET: {
936 string const & name = lyx::to_utf8(cmd.argument());
940 data = tmp.createDialogStr(name);
942 cur.bv().showInsetDialog(name, data, 0);
946 case LFUN_INSET_INSERT: {
948 if (createInsetMath_fromDialogStr(lyx::to_utf8(cmd.argument()), ar)) {
957 InsetMathDim::doDispatch(cur, cmd);
963 bool InsetMathNest::getStatus(LCursor & cur, FuncRequest const & cmd,
964 FuncStatus & flag) const
966 // the font related toggles
967 //string tc = "mathnormal";
969 string const arg = lyx::to_utf8(cmd.argument());
970 switch (cmd.action) {
971 case LFUN_TABULAR_FEATURE:
975 case LFUN_TABULAR_FEATURE:
976 // FIXME: check temporarily disabled
978 char align = mathcursor::valign();
983 if (cmd.argument().empty()) {
987 if (!contains("tcb", cmd.argument()[0])) {
991 flag.setOnOff(cmd.argument()[0] == align);
994 /// We have to handle them since 1.4 blocks all unhandled actions
1000 case LFUN_FONT_NOUN:
1001 case LFUN_FONT_ROMAN:
1002 case LFUN_FONT_DEFAULT:
1005 case LFUN_MATH_MUTATE:
1006 //flag.setOnOff(mathcursor::formula()->hullType() == lyx::to_utf8(cmd.argument()));
1007 flag.setOnOff(false);
1010 // we just need to be in math mode to enable that
1011 case LFUN_MATH_SIZE:
1012 case LFUN_MATH_SPACE:
1013 case LFUN_MATH_LIMITS:
1014 case LFUN_MATH_NONUMBER:
1015 case LFUN_MATH_NUMBER:
1016 case LFUN_MATH_EXTERN:
1020 case LFUN_FONT_FRAK:
1021 flag.enabled(currentMode() != TEXT_MODE);
1024 case LFUN_MATH_INSERT: {
1025 bool const textarg =
1026 arg == "\\textbf" || arg == "\\textsf" ||
1027 arg == "\\textrm" || arg == "\\textmd" ||
1028 arg == "\\textit" || arg == "\\textsc" ||
1029 arg == "\\textsl" || arg == "\\textup" ||
1030 arg == "\\texttt" || arg == "\\textbb" ||
1031 arg == "\\textnormal";
1032 flag.enabled(currentMode() != TEXT_MODE || textarg);
1036 case LFUN_MATH_MATRIX:
1037 flag.enabled(currentMode() == MATH_MODE);
1040 case LFUN_INSET_INSERT: {
1041 // Don't test createMathInset_fromDialogStr(), since
1042 // getStatus is not called with a valid reference and the
1043 // dialog would not be applyable.
1044 string const name = cmd.getArg(0);
1045 flag.enabled(name == "ref");
1049 case LFUN_MATH_DELIM:
1050 case LFUN_MATH_BIGDELIM:
1051 // Don't do this with multi-cell selections
1052 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1063 void InsetMathNest::edit(LCursor & cur, bool left)
1066 cur.idx() = left ? 0 : cur.lastidx();
1067 cur.pos() = left ? 0 : cur.lastpos();
1069 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1073 InsetBase * InsetMathNest::editXY(LCursor & cur, int x, int y)
1076 int dist_min = 1000000;
1077 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1078 int const d = cell(i).dist(x, y);
1084 MathArray & ar = cell(idx_min);
1086 cur.idx() = idx_min;
1087 cur.pos() = ar.x2pos(x - ar.xo());
1088 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1089 if (dist_min == 0) {
1091 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1092 if (ar[i]->covers(x, y))
1093 return ar[i].nucleus()->editXY(cur, x, y);
1099 void InsetMathNest::lfunMousePress(LCursor & cur, FuncRequest & cmd)
1101 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1102 BufferView & bv = cur.bv();
1103 if (cmd.button() == mouse_button::button1) {
1104 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1105 bv.mouseSetCursor(cur);
1106 } else if (cmd.button() == mouse_button::button2) {
1108 if (cur.selection())
1109 asArray(lyx::to_utf8(bv.cursor().selectionAsString(false)), ar);
1111 asArray(lyx::to_utf8(theSelection().get()), ar);
1114 bv.mouseSetCursor(cur);
1119 void InsetMathNest::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
1121 // only select with button 1
1122 if (cmd.button() == mouse_button::button1) {
1123 LCursor & bvcur = cur.bv().cursor();
1124 if (bvcur.anchor_.hasPart(cur)) {
1125 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1126 bvcur.setCursor(cur);
1127 bvcur.selection() = true;
1128 //lyxerr << "MOTION " << bvcur << endl;
1137 void InsetMathNest::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
1139 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1141 if (cmd.button() == mouse_button::button1) {
1142 //theSelection().put(cur.grabSelection());
1146 if (cmd.button() == mouse_button::button3) {
1147 // try to dispatch to enclosed insets first
1148 cur.bv().showDialog("mathpanel");
1156 bool InsetMathNest::interpret(LCursor & cur, char c)
1158 //lyxerr << "interpret 2: '" << c << "'" << endl;
1159 string save_selection;
1160 if (c == '^' || c == '_')
1161 save_selection = grabAndEraseSelection(cur);
1166 if (cur.inMacroMode()) {
1167 string name = cur.macroName();
1169 /// are we currently typing '#1' or '#2' or...?
1170 if (name == "\\#") {
1173 if (n >= 1 && n <= 9)
1174 cur.insert(new MathMacroArgument(n));
1179 cur.activeMacro()->setName(name + c);
1183 // handle 'special char' macros
1188 if (currentMode() == InsetMath::TEXT_MODE)
1189 cur.niceInsert(createInsetMath("textbackslash"));
1191 cur.niceInsert(createInsetMath("backslash"));
1192 } else if (c == '{') {
1194 cur.niceInsert(MathAtom(new InsetMathBrace));
1195 } else if (c == '%') {
1197 cur.niceInsert(MathAtom(new InsetMathComment));
1198 } else if (c == '#') {
1199 BOOST_ASSERT(cur.activeMacro());
1200 cur.activeMacro()->setName(name + c);
1203 cur.niceInsert(createInsetMath(string(1, c)));
1208 // One character big delimiters. The others are handled in
1209 // the other interpret() method.
1210 latexkeys const * l = in_word_set(name.substr(1));
1211 if (name[0] == '\\' && l && l->inset == "big") {
1221 delim = string(1, c);
1224 if (InsetMathBig::isBigInsetDelim(delim)) {
1225 // name + delim ared a valid InsetMathBig.
1226 // We can't use cur.macroModeClose() because
1227 // it does not handle delim.
1228 InsetMathUnknown * p = cur.activeMacro();
1231 cur.cell().erase(cur.pos());
1232 cur.plainInsert(MathAtom(
1233 new InsetMathBig(name.substr(1), delim)));
1238 // leave macro mode and try again if necessary
1239 cur.macroModeClose();
1241 cur.niceInsert(MathAtom(new InsetMathBrace));
1247 // This is annoying as one has to press <space> far too often.
1251 // leave autocorrect mode if necessary
1252 if (autocorrect() && c == ' ') {
1253 autocorrect() = false;
1258 // just clear selection on pressing the space bar
1259 if (cur.selection() && c == ' ') {
1260 cur.selection() = false;
1267 //lyxerr << "starting with macro" << endl;
1268 cur.insert(MathAtom(new InsetMathUnknown("\\", false)));
1273 if (currentMode() == InsetMath::TEXT_MODE)
1279 if (currentMode() == InsetMath::TEXT_MODE) {
1280 // insert spaces in text mode,
1281 // but suppress direct insertion of two spaces in a row
1282 // the still allows typing '<space>a<space>' and deleting the 'a', but
1283 // it is better than nothing...
1284 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
1288 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1289 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1294 // if are at the very end, leave the formula
1295 return cur.pos() != cur.lastpos();
1298 // These shouldn't work in text mode:
1299 if (currentMode() != InsetMath::TEXT_MODE) {
1301 script(cur, false, save_selection);
1305 script(cur, true, save_selection);
1309 cur.niceInsert(createInsetMath("sim"));
1314 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1315 c == '%' || c == '_' || c == '^') {
1316 cur.niceInsert(createInsetMath(string(1, c)));
1321 // try auto-correction
1322 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1325 // no special circumstances, so insert the character without any fuss
1327 cur.autocorrect() = true;
1332 bool InsetMathNest::interpret(LCursor & cur, string const & str)
1334 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1336 if (!cur.empty() && cur.pos() > 0 &&
1337 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1338 if (InsetMathBig::isBigInsetDelim(str)) {
1339 string prev = asString(cur.cell()[cur.pos() - 1]);
1340 if (prev[0] == '\\') {
1341 prev = prev.substr(1);
1342 latexkeys const * l = in_word_set(prev);
1343 if (l && l->inset == "big") {
1344 cur.cell()[cur.pos() - 1] =
1345 MathAtom(new InsetMathBig(prev, str));
1355 bool InsetMathNest::script(LCursor & cur, bool up, string const &
1358 // Hack to get \^ and \_ working
1359 //lyxerr << "handling script: up: " << up << endl;
1360 if (cur.inMacroMode() && cur.macroName() == "\\") {
1362 cur.niceInsert(createInsetMath("mathcircumflex"));
1364 interpret(cur, '_');
1368 cur.macroModeClose();
1369 if (asScriptInset() && cur.idx() == 0) {
1370 // we are in a nucleus of a script inset, move to _our_ script
1371 InsetMathScript * inset = asScriptInset();
1372 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1374 cur.idx() = inset->idxOfScript(up);
1376 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1378 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1381 cur.idx() = inset->idxOfScript(up);
1382 cur.pos() = cur.lastpos();
1384 // convert the thing to our left to a scriptinset or create a new
1385 // one if in the very first position of the array
1386 if (cur.pos() == 0) {
1387 //lyxerr << "new scriptinset" << endl;
1388 cur.insert(new InsetMathScript(up));
1390 //lyxerr << "converting prev atom " << endl;
1391 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1394 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1395 // special handling of {}-bases
1396 // is this always correct?
1397 if (inset->nuc().size() == 1
1398 && inset->nuc().back()->asBraceInset())
1399 inset->nuc() = inset->nuc().back()->asNestInset()->cell(0);
1405 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1406 cur.niceInsert(save_selection);
1408 //lyxerr << "inserting selection 2:\n" << save_selection << endl;