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"
48 #include "outputparams.h"
51 #include "support/lstrings.h"
53 #include "frontends/Painter.h"
54 #include "frontends/Selection.h"
55 #include "frontends/nullpainter.h"
57 #include "funcrequest.h"
58 #include "lyxserver.h"
59 #include "lyxsocket.h"
63 using lyx::CoordCache;
65 using lyx::odocstream;
68 using lyx::cap::copySelection;
69 using lyx::cap::grabAndEraseSelection;
70 using lyx::cap::cutSelection;
71 using lyx::cap::replaceSelection;
72 using lyx::cap::selClearOrDel;
76 using std::istringstream;
79 InsetMathNest::InsetMathNest(idx_type nargs)
80 : cells_(nargs), lock_(false)
84 InsetMath::idx_type InsetMathNest::nargs() const
90 MathArray & InsetMathNest::cell(idx_type i)
96 MathArray const & InsetMathNest::cell(idx_type i) const
102 void InsetMathNest::cursorPos(BufferView const & bv,
103 CursorSlice const & sl, bool /*boundary*/,
104 int & x, int & y) const
106 // FIXME: This is a hack. Ideally, the coord cache should not store
107 // absolute positions, but relative ones. This would mean to call
108 // setXY() not in MathArray::draw(), but in the parent insets' draw()
109 // with the correctly adjusted x,y values. But this means that we'd have
110 // to touch all (math)inset's draw() methods. Right now, we'll store
111 // absolute value, and make them here relative, only to make them
112 // absolute again when actually drawing the cursor. What a mess.
113 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
114 MathArray const & ar = sl.cell();
115 CoordCache const & coord_cache = bv.coordCache();
116 if (!coord_cache.getArrays().has(&ar)) {
117 // this can (semi-)legally happen if we just created this cell
118 // and it never has been drawn before. So don't ASSERT.
119 //lyxerr << "no cached data for array " << &ar << endl;
124 Point const pt = coord_cache.getArrays().xy(&ar);
125 if (!coord_cache.getInsets().has(this)) {
127 //lyxerr << "no cached data for inset " << this << endl;
132 Point const pt2 = coord_cache.getInsets().xy(this);
133 //lyxerr << "retrieving position cache for MathArray "
134 // << pt.x_ << ' ' << pt.y_ << std::endl;
135 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
137 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
138 // << " asc: " << ascent() << " des: " << descent()
139 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
140 // move cursor visually into empty cells ("blue rectangles");
146 void InsetMathNest::metrics(MetricsInfo const & mi) const
149 for (idx_type i = 0, n = nargs(); i != n; ++i)
154 bool InsetMathNest::idxNext(LCursor & cur) const
156 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
157 if (cur.idx() == cur.lastidx())
165 bool InsetMathNest::idxRight(LCursor & cur) const
171 bool InsetMathNest::idxPrev(LCursor & cur) const
173 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
177 cur.pos() = cur.lastpos();
182 bool InsetMathNest::idxLeft(LCursor & cur) const
188 bool InsetMathNest::idxFirst(LCursor & cur) const
190 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
199 bool InsetMathNest::idxLast(LCursor & cur) const
201 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
204 cur.idx() = cur.lastidx();
205 cur.pos() = cur.lastpos();
210 void InsetMathNest::dump() const
212 lyx::odocstringstream oss;
214 os << "---------------------------------------------\n";
217 for (idx_type i = 0, n = nargs(); i != n; ++i)
218 os << cell(i) << "\n";
219 os << "---------------------------------------------\n";
220 lyxerr << lyx::to_utf8(oss.str());
224 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
228 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
231 setPosCache(pi, x, y);
235 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
237 BufferView & bv = *pi.base.bv;
238 // this should use the x/y values given, not the cached values
239 LCursor & cur = bv.cursor();
240 if (!cur.selection())
242 if (!ptr_cmp(&cur.inset(), this))
245 // FIXME: hack to get position cache warm
246 static lyx::frontend::NullPainter nop;
247 PainterInfo pinop(pi);
251 CursorSlice s1 = cur.selBegin();
252 CursorSlice s2 = cur.selEnd();
254 //lyxerr << "InsetMathNest::drawing selection: "
255 // << " s1: " << s1 << " s2: " << s2 << endl;
256 if (s1.idx() == s2.idx()) {
257 MathArray const & c = cell(s1.idx());
258 int x1 = c.xo(bv) + c.pos2x(s1.pos());
259 int y1 = c.yo(bv) - c.ascent();
260 int x2 = c.xo(bv) + c.pos2x(s2.pos());
261 int y2 = c.yo(bv) + c.descent();
262 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
263 //lyxerr << "InsetMathNest::drawing selection 3: "
264 // << " x1: " << x1 << " x2: " << x2
265 // << " y1: " << y1 << " y2: " << y2 << endl;
267 for (idx_type i = 0; i < nargs(); ++i) {
268 if (idxBetween(i, s1.idx(), s2.idx())) {
269 MathArray const & c = cell(i);
271 int y1 = c.yo(bv) - c.ascent();
272 int x2 = c.xo(bv) + c.width();
273 int y2 = c.yo(bv) + c.descent();
274 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
281 void InsetMathNest::validate(LaTeXFeatures & features) const
283 for (idx_type i = 0; i < nargs(); ++i)
284 cell(i).validate(features);
288 void InsetMathNest::replace(ReplaceData & rep)
290 for (idx_type i = 0; i < nargs(); ++i)
291 cell(i).replace(rep);
295 bool InsetMathNest::contains(MathArray const & ar) const
297 for (idx_type i = 0; i < nargs(); ++i)
298 if (cell(i).contains(ar))
304 bool InsetMathNest::lock() const
310 void InsetMathNest::lock(bool l)
316 bool InsetMathNest::isActive() const
322 MathArray InsetMathNest::glue() const
325 for (size_t i = 0; i < nargs(); ++i)
331 void InsetMathNest::write(WriteStream & os) const
333 os << '\\' << name().c_str();
334 for (size_t i = 0; i < nargs(); ++i)
335 os << '{' << cell(i) << '}';
337 os.pendingSpace(true);
338 if (lock_ && !os.latex()) {
340 os.pendingSpace(true);
345 void InsetMathNest::normalize(NormalStream & os) const
347 os << '[' << name().c_str();
348 for (size_t i = 0; i < nargs(); ++i)
349 os << ' ' << cell(i);
354 int InsetMathNest::latex(Buffer const &, odocstream & os,
355 OutputParams const & runparams) const
357 WriteStream wi(os, runparams.moving_arg, true);
363 bool InsetMathNest::notifyCursorLeaves(LCursor & /*cur*/)
369 MathArray & ar = cur.cell();
370 // remove base-only "scripts"
371 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
372 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
373 if (p && p->nargs() == 1) {
374 MathArray ar = p->nuc();
377 cur.adjust(i, ar.size() - 1);
381 // glue adjacent font insets of the same kind
382 for (pos_type i = 0; i + 1 < size(); ++i) {
383 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
384 InsetMathFont const * q = operator[](i + 1)->asFontInset();
385 if (p && q && p->name() == q->name()) {
386 p->cell(0).append(q->cell(0));
396 void InsetMathNest::handleFont
397 (LCursor & cur, string const & arg, string const & font)
399 // this whole function is a hack and won't work for incremental font
401 recordUndo(cur, Undo::ATOMIC);
403 if (cur.inset().asInsetMath()->name() == font)
404 cur.handleFont(font);
406 cur.handleNest(createInsetMath(font));
412 void InsetMathNest::handleFont2(LCursor & cur, string const & arg)
414 recordUndo(cur, Undo::ATOMIC);
417 bv_funcs::string2font(arg, font, b);
418 if (font.color() != LColor::inherit) {
419 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
420 cur.handleNest(at, 0);
425 void InsetMathNest::doDispatch(LCursor & cur, FuncRequest & cmd)
427 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
428 //CursorSlice sl = cur.current();
430 switch (cmd.action) {
434 cur.message(_("Paste"));
435 replaceSelection(cur);
437 istringstream is(lyx::to_utf8(cmd.argument()));
439 docstring const selection = lyx::cap::getSelection(cur.buffer(), n);
441 cur.niceInsert(lyx::to_utf8(selection));
442 cur.clearSelection(); // bug 393
443 cur.bv().switchKeyMap();
450 cutSelection(cur, true, true);
451 cur.message(_("Cut"));
452 // Prevent stale position >= size crash
453 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
460 cur.message(_("Copy"));
463 case LFUN_MOUSE_PRESS:
464 lfunMousePress(cur, cmd);
467 case LFUN_MOUSE_MOTION:
468 lfunMouseMotion(cur, cmd);
471 case LFUN_MOUSE_RELEASE:
472 lfunMouseRelease(cur, cmd);
475 case LFUN_FINISHED_LEFT:
476 cur.bv().cursor() = cur;
479 case LFUN_FINISHED_RIGHT:
481 cur.bv().cursor() = cur;
484 case LFUN_FINISHED_UP:
485 cur.bv().cursor() = cur;
488 case LFUN_FINISHED_DOWN:
490 cur.bv().cursor() = cur;
493 case LFUN_CHAR_FORWARD_SELECT:
494 case LFUN_CHAR_FORWARD:
495 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
496 cur.autocorrect() = false;
498 cur.macroModeClose();
499 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
500 cur.pushLeft(*cur.nextAtom().nucleus());
501 cur.inset().idxFirst(cur);
502 } else if (cur.posRight() || idxRight(cur)
503 || cur.popRight() || cur.selection())
506 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
511 case LFUN_CHAR_BACKWARD_SELECT:
512 case LFUN_CHAR_BACKWARD:
513 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
514 cur.autocorrect() = false;
516 cur.macroModeClose();
517 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
519 cur.push(*cur.nextAtom().nucleus());
520 cur.inset().idxLast(cur);
521 } else if (cur.posLeft() || idxLeft(cur)
522 || cur.popLeft() || cur.selection())
525 cmd = FuncRequest(LFUN_FINISHED_LEFT);
532 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
533 if (cur.inMacroMode()) {
535 cur.macroModeClose();
538 cur.selHandle(cmd.action == LFUN_UP_SELECT);
540 cmd = FuncRequest(LFUN_FINISHED_UP);
543 // fixes bug 1598. Please check!
547 case LFUN_DOWN_SELECT:
549 if (cur.inMacroMode()) {
550 cur.macroModeClose();
553 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
555 cmd = FuncRequest(LFUN_FINISHED_DOWN);
558 // fixes bug 1598. Please check!
562 case LFUN_MOUSE_DOUBLE:
563 case LFUN_MOUSE_TRIPLE:
564 case LFUN_WORD_SELECT:
568 cur.selection() = true;
569 cur.pos() = cur.lastpos();
570 cur.idx() = cur.lastidx();
573 case LFUN_PARAGRAPH_UP_SELECT:
574 case LFUN_PARAGRAPH_UP:
575 case LFUN_PARAGRAPH_DOWN_SELECT:
576 case LFUN_PARAGRAPH_DOWN:
579 case LFUN_LINE_BEGIN_SELECT:
580 case LFUN_LINE_BEGIN:
581 case LFUN_WORD_BACKWARD_SELECT:
582 case LFUN_WORD_BACKWARD:
583 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
584 cmd.action == LFUN_LINE_BEGIN_SELECT);
585 cur.macroModeClose();
586 if (cur.pos() != 0) {
588 } else if (cur.col() != 0) {
589 cur.idx() -= cur.col();
591 } else if (cur.idx() != 0) {
595 cmd = FuncRequest(LFUN_FINISHED_LEFT);
600 case LFUN_WORD_FORWARD_SELECT:
601 case LFUN_WORD_FORWARD:
602 case LFUN_LINE_END_SELECT:
604 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
605 cmd.action == LFUN_LINE_END_SELECT);
606 cur.macroModeClose();
608 if (cur.pos() != cur.lastpos()) {
609 cur.pos() = cur.lastpos();
610 } else if (ncols() && (cur.col() != cur.lastcol())) {
611 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
612 cur.pos() = cur.lastpos();
613 } else if (cur.idx() != cur.lastidx()) {
614 cur.idx() = cur.lastidx();
615 cur.pos() = cur.lastpos();
617 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
622 case LFUN_SCREEN_UP_SELECT:
624 cmd = FuncRequest(LFUN_FINISHED_LEFT);
628 case LFUN_SCREEN_DOWN_SELECT:
629 case LFUN_SCREEN_DOWN:
630 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
634 case LFUN_CELL_FORWARD:
635 cur.inset().idxNext(cur);
638 case LFUN_CELL_BACKWARD:
639 cur.inset().idxPrev(cur);
642 case LFUN_WORD_DELETE_BACKWARD:
643 case LFUN_CHAR_DELETE_BACKWARD:
645 // May affect external cell:
646 recordUndoInset(cur, Undo::ATOMIC);
648 recordUndo(cur, Undo::ATOMIC);
652 case LFUN_WORD_DELETE_FORWARD:
653 case LFUN_CHAR_DELETE_FORWARD:
654 if (cur.pos() == cur.lastpos())
655 // May affect external cell:
656 recordUndoInset(cur, Undo::ATOMIC);
658 recordUndo(cur, Undo::ATOMIC);
664 cur.clearSelection();
666 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
671 case LFUN_INSET_TOGGLE:
677 case LFUN_SELF_INSERT:
678 if (cmd.argument().size() != 1) {
680 string const arg = lyx::to_utf8(cmd.argument());
681 if (!interpretString(cur, arg))
685 // Don't record undo steps if we are in macro mode and
686 // cmd.argument is the next character of the macro name.
687 // Otherwise we'll get an invalid cursor if we undo after
688 // the macro was finished and the macro is a known command,
689 // e.g. sqrt. LCursor::macroModeClose replaces in this case
690 // the InsetMathUnknown with name "frac" by an empty
691 // InsetMathFrac -> a pos value > 0 is invalid.
692 // A side effect is that an undo before the macro is finished
693 // undoes the complete macro, not only the last character.
694 if (!cur.inMacroMode())
697 // spacial handling of space. If we insert an inset
698 // via macro mode, we want to put the cursor inside it
699 // if relevant. Think typing "\frac<space>".
700 if (cmd.argument()[0] == ' '
701 && cur.inMacroMode() && cur.macroName() != "\\"
702 && cur.macroModeClose()) {
703 MathAtom const atom = cur.prevAtom();
704 if (atom->asNestInset() && atom->nargs() > 0) {
706 cur.pushLeft(*cur.nextInset());
708 } else if (!interpretChar(cur, cmd.argument()[0])) {
709 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
714 //case LFUN_SERVER_GET_XY:
715 // sprintf(dispatch_buffer, "%d %d",);
718 case LFUN_SERVER_SET_XY: {
719 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
722 istringstream is(lyx::to_utf8(cmd.argument()));
724 cur.setScreenPos(x, y);
728 // Special casing for superscript in case of LyX handling
730 case LFUN_ACCENT_CIRCUMFLEX:
731 if (cmd.argument().empty()) {
732 // do superscript if LyX handles
734 recordUndo(cur, Undo::ATOMIC);
735 script(cur, true, grabAndEraseSelection(cur));
739 case LFUN_ACCENT_UMLAUT:
740 case LFUN_ACCENT_ACUTE:
741 case LFUN_ACCENT_GRAVE:
742 case LFUN_ACCENT_BREVE:
743 case LFUN_ACCENT_DOT:
744 case LFUN_ACCENT_MACRON:
745 case LFUN_ACCENT_CARON:
746 case LFUN_ACCENT_TILDE:
747 case LFUN_ACCENT_CEDILLA:
748 case LFUN_ACCENT_CIRCLE:
749 case LFUN_ACCENT_UNDERDOT:
750 case LFUN_ACCENT_TIE:
751 case LFUN_ACCENT_OGONEK:
752 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
756 case LFUN_FONT_FREE_APPLY:
757 case LFUN_FONT_FREE_UPDATE:
758 handleFont2(cur, lyx::to_utf8(cmd.argument()));
762 if (currentMode() == TEXT_MODE)
763 handleFont(cur, lyx::to_utf8(cmd.argument()), "textbf");
765 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathbf");
768 if (currentMode() == TEXT_MODE)
769 handleFont(cur, lyx::to_utf8(cmd.argument()), "textsf");
771 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathsf");
774 if (currentMode() == TEXT_MODE)
775 handleFont(cur, lyx::to_utf8(cmd.argument()), "emph");
777 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathcal");
779 case LFUN_FONT_ROMAN:
780 if (currentMode() == TEXT_MODE)
781 handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
783 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathrm");
786 if (currentMode() == TEXT_MODE)
787 handleFont(cur, lyx::to_utf8(cmd.argument()), "texttt");
789 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathtt");
792 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathfrak");
795 if (currentMode() == TEXT_MODE)
796 handleFont(cur, lyx::to_utf8(cmd.argument()), "textit");
798 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathit");
801 if (currentMode() == TEXT_MODE)
802 // FIXME: should be "noun"
803 handleFont(cur, lyx::to_utf8(cmd.argument()), "textsc");
805 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathbb");
807 //case LFUN_FONT_FREE_APPLY:
808 handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
810 case LFUN_FONT_DEFAULT:
811 handleFont(cur, lyx::to_utf8(cmd.argument()), "textnormal");
814 case LFUN_MATH_MODE: {
816 // ignore math-mode on when already in math mode
817 if (currentMode() == InsetBase::MATH_MODE && cmd.argument() == "on")
819 cur.macroModeClose();
820 string const save_selection = grabAndEraseSelection(cur);
822 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
823 cur.plainInsert(MathAtom(new InsetMathBox("mbox")));
825 cur.pushLeft(*cur.nextInset());
826 cur.niceInsert(save_selection);
828 if (currentMode() == InsetBase::TEXT_MODE) {
829 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
830 cur.message(_("create new math text environment ($...$)"));
832 handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
833 cur.message(_("entered math text mode (textrm)"));
846 case LFUN_MATH_MATRIX: {
847 recordUndo(cur, Undo::ATOMIC);
852 istringstream is(lyx::to_utf8(cmd.argument()));
853 is >> m >> n >> v_align >> h_align;
860 MathAtom(new InsetMathArray("array", m, n, v_align[0], h_align)));
864 case LFUN_MATH_DELIM: {
866 string rs = lyx::support::split(lyx::to_utf8(cmd.argument()), ls, ' ');
867 // Reasonable default values
872 recordUndo(cur, Undo::ATOMIC);
873 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
877 case LFUN_MATH_BIGDELIM: {
878 string const lname = cmd.getArg(0);
879 string const ldelim = cmd.getArg(1);
880 string const rname = cmd.getArg(2);
881 string const rdelim = cmd.getArg(3);
882 latexkeys const * l = in_word_set(lname);
883 bool const have_l = l && l->inset == "big" &&
884 InsetMathBig::isBigInsetDelim(ldelim);
885 l = in_word_set(rname);
886 bool const have_r = l && l->inset == "big" &&
887 InsetMathBig::isBigInsetDelim(rdelim);
888 // We mimic LFUN_MATH_DELIM in case we have an empty left
889 // or right delimiter.
890 if (have_l || have_r) {
891 recordUndo(cur, Undo::ATOMIC);
892 string const selection = grabAndEraseSelection(cur);
895 cur.insert(MathAtom(new InsetMathBig(lname,
897 cur.niceInsert(selection);
899 cur.insert(MathAtom(new InsetMathBig(rname,
902 // Don't call cur.undispatched() if we did nothing, this would
903 // lead to infinite recursion via LyXText::dispatch().
907 case LFUN_SPACE_INSERT:
908 case LFUN_MATH_SPACE:
909 recordUndo(cur, Undo::ATOMIC);
910 cur.insert(MathAtom(new InsetMathSpace(",")));
913 case LFUN_ERT_INSERT:
914 // interpret this as if a backslash was typed
915 recordUndo(cur, Undo::ATOMIC);
916 interpretChar(cur, '\\');
919 case LFUN_MATH_SUBSCRIPT:
920 // interpret this as if a _ was typed
921 recordUndo(cur, Undo::ATOMIC);
922 interpretChar(cur, '_');
925 case LFUN_MATH_SUPERSCRIPT:
926 // interpret this as if a ^ was typed
927 recordUndo(cur, Undo::ATOMIC);
928 interpretChar(cur, '^');
931 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
932 // handling such that "self-insert" works on "arbitrary stuff" too, and
933 // math-insert only handles special math things like "matrix".
934 case LFUN_MATH_INSERT: {
935 recordUndo(cur, Undo::ATOMIC);
936 if (cmd.argument() == "^" || cmd.argument() == "_") {
937 interpretChar(cur, cmd.argument()[0]);
939 cur.niceInsert(lyx::to_utf8(cmd.argument()));
943 case LFUN_DIALOG_SHOW_NEW_INSET: {
944 string const & name = lyx::to_utf8(cmd.argument());
948 data = tmp.createDialogStr(name);
950 cur.bv().showInsetDialog(name, data, 0);
954 case LFUN_INSET_INSERT: {
956 if (createInsetMath_fromDialogStr(lyx::to_utf8(cmd.argument()), ar)) {
965 InsetMathDim::doDispatch(cur, cmd);
971 bool InsetMathNest::getStatus(LCursor & cur, FuncRequest const & cmd,
972 FuncStatus & flag) const
974 // the font related toggles
975 //string tc = "mathnormal";
977 string const arg = lyx::to_utf8(cmd.argument());
978 switch (cmd.action) {
979 case LFUN_TABULAR_FEATURE:
983 case LFUN_TABULAR_FEATURE:
984 // FIXME: check temporarily disabled
986 char align = mathcursor::valign();
991 if (cmd.argument().empty()) {
995 if (!contains("tcb", cmd.argument()[0])) {
999 flag.setOnOff(cmd.argument()[0] == align);
1002 /// We have to handle them since 1.4 blocks all unhandled actions
1003 case LFUN_FONT_ITAL:
1004 case LFUN_FONT_BOLD:
1005 case LFUN_FONT_SANS:
1006 case LFUN_FONT_EMPH:
1007 case LFUN_FONT_CODE:
1008 case LFUN_FONT_NOUN:
1009 case LFUN_FONT_ROMAN:
1010 case LFUN_FONT_DEFAULT:
1013 case LFUN_MATH_MUTATE:
1014 //flag.setOnOff(mathcursor::formula()->hullType() == lyx::to_utf8(cmd.argument()));
1015 flag.setOnOff(false);
1018 // we just need to be in math mode to enable that
1019 case LFUN_MATH_SIZE:
1020 case LFUN_MATH_SPACE:
1021 case LFUN_MATH_LIMITS:
1022 case LFUN_MATH_NONUMBER:
1023 case LFUN_MATH_NUMBER:
1024 case LFUN_MATH_EXTERN:
1028 case LFUN_FONT_FRAK:
1029 flag.enabled(currentMode() != TEXT_MODE);
1032 case LFUN_MATH_INSERT: {
1033 bool const textarg =
1034 arg == "\\textbf" || arg == "\\textsf" ||
1035 arg == "\\textrm" || arg == "\\textmd" ||
1036 arg == "\\textit" || arg == "\\textsc" ||
1037 arg == "\\textsl" || arg == "\\textup" ||
1038 arg == "\\texttt" || arg == "\\textbb" ||
1039 arg == "\\textnormal";
1040 flag.enabled(currentMode() != TEXT_MODE || textarg);
1044 case LFUN_MATH_MATRIX:
1045 flag.enabled(currentMode() == MATH_MODE);
1048 case LFUN_INSET_INSERT: {
1049 // Don't test createMathInset_fromDialogStr(), since
1050 // getStatus is not called with a valid reference and the
1051 // dialog would not be applyable.
1052 string const name = cmd.getArg(0);
1053 flag.enabled(name == "ref");
1057 case LFUN_MATH_DELIM:
1058 case LFUN_MATH_BIGDELIM:
1059 // Don't do this with multi-cell selections
1060 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1071 void InsetMathNest::edit(LCursor & cur, bool left)
1074 cur.idx() = left ? 0 : cur.lastidx();
1075 cur.pos() = left ? 0 : cur.lastpos();
1077 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1081 InsetBase * InsetMathNest::editXY(LCursor & cur, int x, int y)
1084 int dist_min = 1000000;
1085 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1086 int const d = cell(i).dist(cur.bv(), x, y);
1092 MathArray & ar = cell(idx_min);
1094 cur.idx() = idx_min;
1095 cur.pos() = ar.x2pos(x - ar.xo(cur.bv()));
1097 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1098 if (dist_min == 0) {
1100 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1101 if (ar[i]->covers(cur.bv(), x, y))
1102 return ar[i].nucleus()->editXY(cur, x, y);
1108 void InsetMathNest::lfunMousePress(LCursor & cur, FuncRequest & cmd)
1110 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1111 BufferView & bv = cur.bv();
1112 if (cmd.button() == mouse_button::button1) {
1113 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1114 bv.mouseSetCursor(cur);
1115 } else if (cmd.button() == mouse_button::button2) {
1117 if (cur.selection())
1118 asArray(bv.cursor().selectionAsString(false), ar);
1120 asArray(theSelection().get(), ar);
1123 bv.mouseSetCursor(cur);
1128 void InsetMathNest::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
1130 // only select with button 1
1131 if (cmd.button() == mouse_button::button1) {
1132 LCursor & bvcur = cur.bv().cursor();
1133 if (bvcur.anchor_.hasPart(cur)) {
1134 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1135 bvcur.setCursor(cur);
1136 bvcur.selection() = true;
1137 //lyxerr << "MOTION " << bvcur << endl;
1146 void InsetMathNest::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
1148 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1150 if (cmd.button() == mouse_button::button1) {
1151 //theSelection().put(cur.grabSelection());
1155 if (cmd.button() == mouse_button::button3) {
1156 // try to dispatch to enclosed insets first
1157 cur.bv().showDialog("mathpanel");
1165 bool InsetMathNest::interpretChar(LCursor & cur, lyx::char_type c)
1167 //lyxerr << "interpret 2: '" << c << "'" << endl;
1168 string save_selection;
1169 if (c == '^' || c == '_')
1170 save_selection = grabAndEraseSelection(cur);
1175 if (cur.inMacroMode()) {
1176 string name = cur.macroName();
1178 /// are we currently typing '#1' or '#2' or...?
1179 if (name == "\\#") {
1182 if (n >= 1 && n <= 9)
1183 cur.insert(new MathMacroArgument(n));
1188 cur.activeMacro()->setName(name + lyx::to_ascii(docstring(1, c)));
1192 // handle 'special char' macros
1197 if (currentMode() == InsetMath::TEXT_MODE)
1198 cur.niceInsert(createInsetMath("textbackslash"));
1200 cur.niceInsert(createInsetMath("backslash"));
1201 } else if (c == '{') {
1203 cur.niceInsert(MathAtom(new InsetMathBrace));
1204 } else if (c == '%') {
1206 cur.niceInsert(MathAtom(new InsetMathComment));
1207 } else if (c == '#') {
1208 BOOST_ASSERT(cur.activeMacro());
1209 cur.activeMacro()->setName(name + lyx::to_ascii(docstring(1, c)));
1212 cur.niceInsert(createInsetMath(lyx::to_ascii(docstring(1, c))));
1217 // One character big delimiters. The others are handled in
1218 // interpretString().
1219 latexkeys const * l = in_word_set(name.substr(1));
1220 if (name[0] == '\\' && l && l->inset == "big") {
1230 delim = lyx::to_ascii(docstring(1, c));
1233 if (InsetMathBig::isBigInsetDelim(delim)) {
1234 // name + delim ared a valid InsetMathBig.
1235 // We can't use cur.macroModeClose() because
1236 // it does not handle delim.
1237 InsetMathUnknown * p = cur.activeMacro();
1240 cur.cell().erase(cur.pos());
1241 cur.plainInsert(MathAtom(
1242 new InsetMathBig(name.substr(1), delim)));
1247 // leave macro mode and try again if necessary
1248 cur.macroModeClose();
1250 cur.niceInsert(MathAtom(new InsetMathBrace));
1252 interpretChar(cur, c);
1256 // This is annoying as one has to press <space> far too often.
1260 // leave autocorrect mode if necessary
1261 if (autocorrect() && c == ' ') {
1262 autocorrect() = false;
1267 // just clear selection on pressing the space bar
1268 if (cur.selection() && c == ' ') {
1269 cur.selection() = false;
1276 //lyxerr << "starting with macro" << endl;
1277 cur.insert(MathAtom(new InsetMathUnknown("\\", false)));
1282 if (currentMode() == InsetMath::TEXT_MODE)
1288 if (currentMode() == InsetMath::TEXT_MODE) {
1289 // insert spaces in text mode,
1290 // but suppress direct insertion of two spaces in a row
1291 // the still allows typing '<space>a<space>' and deleting the 'a', but
1292 // it is better than nothing...
1293 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
1297 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1298 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1303 // if are at the very end, leave the formula
1304 return cur.pos() != cur.lastpos();
1307 // These shouldn't work in text mode:
1308 if (currentMode() != InsetMath::TEXT_MODE) {
1310 script(cur, false, save_selection);
1314 script(cur, true, save_selection);
1318 cur.niceInsert(createInsetMath("sim"));
1323 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1324 c == '%' || c == '_' || c == '^') {
1325 cur.niceInsert(createInsetMath(lyx::to_ascii(docstring(1, c))));
1330 // try auto-correction
1331 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1334 // no special circumstances, so insert the character without any fuss
1336 cur.autocorrect() = true;
1341 bool InsetMathNest::interpretString(LCursor & cur, string const & str)
1343 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1345 if (!cur.empty() && cur.pos() > 0 &&
1346 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1347 if (InsetMathBig::isBigInsetDelim(str)) {
1348 string prev = asString(cur.cell()[cur.pos() - 1]);
1349 if (prev[0] == '\\') {
1350 prev = prev.substr(1);
1351 latexkeys const * l = in_word_set(prev);
1352 if (l && l->inset == "big") {
1353 cur.cell()[cur.pos() - 1] =
1354 MathAtom(new InsetMathBig(prev, str));
1364 bool InsetMathNest::script(LCursor & cur, bool up, string const &
1367 // Hack to get \^ and \_ working
1368 //lyxerr << "handling script: up: " << up << endl;
1369 if (cur.inMacroMode() && cur.macroName() == "\\") {
1371 cur.niceInsert(createInsetMath("mathcircumflex"));
1373 interpretChar(cur, '_');
1377 cur.macroModeClose();
1378 if (asScriptInset() && cur.idx() == 0) {
1379 // we are in a nucleus of a script inset, move to _our_ script
1380 InsetMathScript * inset = asScriptInset();
1381 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1383 cur.idx() = inset->idxOfScript(up);
1385 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1387 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1390 cur.idx() = inset->idxOfScript(up);
1391 cur.pos() = cur.lastpos();
1393 // convert the thing to our left to a scriptinset or create a new
1394 // one if in the very first position of the array
1395 if (cur.pos() == 0) {
1396 //lyxerr << "new scriptinset" << endl;
1397 cur.insert(new InsetMathScript(up));
1399 //lyxerr << "converting prev atom " << endl;
1400 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1403 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1404 // special handling of {}-bases
1405 // is this always correct?
1406 if (inset->nuc().size() == 1
1407 && inset->nuc().back()->asBraceInset())
1408 inset->nuc() = inset->nuc().back()->asNestInset()->cell(0);
1414 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1415 cur.niceInsert(save_selection);
1417 //lyxerr << "inserting selection 2:\n" << save_selection << endl;