2 * \file InsetMathNest.cpp
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"
21 #include "InsetMathDelim.h"
22 #include "InsetMathHull.h"
23 //#include "InsetMathMBox.h"
24 #include "InsetMathRef.h"
25 #include "InsetMathScript.h"
26 #include "InsetMathSpace.h"
27 #include "InsetMathSymbol.h"
28 #include "InsetMathUnknown.h"
30 #include "MathFactory.h"
31 #include "MathMacroArgument.h"
32 #include "MathParser.h"
33 #include "MathStream.h"
34 #include "MathSupport.h"
37 #include "BufferView.h"
38 #include "CoordCache.h"
40 #include "CutAndPaste.h"
42 #include "DispatchResult.h"
43 #include "FuncRequest.h"
44 #include "FuncStatus.h"
48 #include "OutputParams.h"
50 #include "support/lstrings.h"
51 #include "support/textutils.h"
53 #include "frontends/Clipboard.h"
54 #include "frontends/Painter.h"
55 #include "frontends/Selection.h"
57 #include "FuncRequest.h"
64 using cap::copySelection;
65 using cap::grabAndEraseSelection;
66 using cap::cutSelection;
67 using cap::replaceSelection;
68 using cap::selClearOrDel;
72 using std::istringstream;
75 InsetMathNest::InsetMathNest(idx_type nargs)
76 : cells_(nargs), lock_(false), mouse_hover_(false)
80 InsetMathNest::InsetMathNest(InsetMathNest const & inset)
81 : InsetMath(inset), cells_(inset.cells_), lock_(inset.lock_),
86 InsetMathNest & InsetMathNest::operator=(InsetMathNest const & inset)
88 cells_ = inset.cells_;
91 InsetMath::operator=(inset);
96 InsetMath::idx_type InsetMathNest::nargs() 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 MathData::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 MathData 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 MathData "
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) {
151 cell(i).metrics(m, dim);
156 bool InsetMathNest::idxNext(Cursor & cur) const
158 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
159 if (cur.idx() == cur.lastidx())
167 bool InsetMathNest::idxRight(Cursor & cur) const
173 bool InsetMathNest::idxPrev(Cursor & cur) const
175 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
179 cur.pos() = cur.lastpos();
184 bool InsetMathNest::idxLeft(Cursor & cur) const
190 bool InsetMathNest::idxFirst(Cursor & cur) const
192 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
201 bool InsetMathNest::idxLast(Cursor & cur) const
203 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
206 cur.idx() = cur.lastidx();
207 cur.pos() = cur.lastpos();
212 void InsetMathNest::dump() const
214 odocstringstream oss;
216 os << "---------------------------------------------\n";
219 for (idx_type i = 0, n = nargs(); i != n; ++i)
220 os << cell(i) << "\n";
221 os << "---------------------------------------------\n";
222 lyxerr << to_utf8(oss.str());
226 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
230 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
233 setPosCache(pi, x, y);
237 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
239 BufferView & bv = *pi.base.bv;
240 // this should use the x/y values given, not the cached values
241 Cursor & cur = bv.cursor();
242 if (!cur.selection())
244 if (!ptr_cmp(&cur.inset(), this))
247 // FIXME: hack to get position cache warm
248 pi.pain.setDrawingEnabled(false);
250 pi.pain.setDrawingEnabled(true);
252 CursorSlice s1 = cur.selBegin();
253 CursorSlice s2 = cur.selEnd();
255 //lyxerr << "InsetMathNest::drawing selection: "
256 // << " s1: " << s1 << " s2: " << s2 << endl;
257 if (s1.idx() == s2.idx()) {
258 MathData const & c = cell(s1.idx());
259 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
260 int x1 = g.pos.x_ + c.pos2x(s1.pos());
261 int y1 = g.pos.y_ - g.dim.ascent();
262 int x2 = g.pos.x_ + c.pos2x(s2.pos());
263 int y2 = g.pos.y_ + g.dim.descent();
264 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
265 //lyxerr << "InsetMathNest::drawing selection 3: "
266 // << " x1: " << x1 << " x2: " << x2
267 // << " y1: " << y1 << " y2: " << y2 << endl;
269 for (idx_type i = 0; i < nargs(); ++i) {
270 if (idxBetween(i, s1.idx(), s2.idx())) {
271 MathData const & c = cell(i);
272 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
274 int y1 = g.pos.y_ - g.dim.ascent();
275 int x2 = g.pos.x_ + g.dim.width();
276 int y2 = g.pos.y_ + g.dim.descent();
277 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
284 void InsetMathNest::validate(LaTeXFeatures & features) const
286 for (idx_type i = 0; i < nargs(); ++i)
287 cell(i).validate(features);
291 void InsetMathNest::replace(ReplaceData & rep)
293 for (idx_type i = 0; i < nargs(); ++i)
294 cell(i).replace(rep);
298 bool InsetMathNest::contains(MathData const & ar) const
300 for (idx_type i = 0; i < nargs(); ++i)
301 if (cell(i).contains(ar))
307 bool InsetMathNest::lock() const
313 void InsetMathNest::lock(bool l)
319 bool InsetMathNest::isActive() const
325 MathData InsetMathNest::glue() const
328 for (size_t i = 0; i < nargs(); ++i)
334 void InsetMathNest::write(WriteStream & os) const
336 os << '\\' << name().c_str();
337 for (size_t i = 0; i < nargs(); ++i)
338 os << '{' << cell(i) << '}';
340 os.pendingSpace(true);
341 if (lock_ && !os.latex()) {
343 os.pendingSpace(true);
348 void InsetMathNest::normalize(NormalStream & os) const
350 os << '[' << name().c_str();
351 for (size_t i = 0; i < nargs(); ++i)
352 os << ' ' << cell(i);
357 int InsetMathNest::latex(Buffer const &, odocstream & os,
358 OutputParams const & runparams) const
360 WriteStream wi(os, runparams.moving_arg, true);
366 bool InsetMathNest::setMouseHover(bool mouse_hover)
368 mouse_hover_ = mouse_hover;
373 bool InsetMathNest::notifyCursorLeaves(Cursor & /*cur*/)
377 MathData & ar = cur.cell();
378 // remove base-only "scripts"
379 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
380 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
381 if (p && p->nargs() == 1) {
382 MathData ar = p->nuc();
385 cur.adjust(i, ar.size() - 1);
389 // glue adjacent font insets of the same kind
390 for (pos_type i = 0; i + 1 < size(); ++i) {
391 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
392 InsetMathFont const * q = operator[](i + 1)->asFontInset();
393 if (p && q && p->name() == q->name()) {
394 p->cell(0).append(q->cell(0));
404 void InsetMathNest::handleFont
405 (Cursor & cur, docstring const & arg, char const * const font)
407 handleFont(cur, arg, from_ascii(font));
411 void InsetMathNest::handleFont
412 (Cursor & cur, docstring const & arg, docstring const & font)
414 // this whole function is a hack and won't work for incremental font
417 if (cur.inset().asInsetMath()->name() == font) {
418 cur.recordUndoInset();
419 cur.handleFont(to_utf8(font));
422 cur.handleNest(createInsetMath(font));
428 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
433 font.fromString(to_utf8(arg), b);
434 if (font.color() != Color_inherit) {
435 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
436 cur.handleNest(at, 0);
441 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
443 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
444 //CursorSlice sl = cur.current();
446 switch (cmd.action) {
450 cur.message(_("Paste"));
451 replaceSelection(cur);
453 if (cmd.argument().empty() && !theClipboard().isInternal())
454 topaste = theClipboard().getAsText();
457 idocstringstream is(cmd.argument());
459 topaste = cap::getSelection(cur.buffer(), n);
461 cur.niceInsert(topaste);
462 cur.clearSelection(); // bug 393
469 cutSelection(cur, true, true);
470 cur.message(_("Cut"));
471 // Prevent stale position >= size crash
472 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
478 cur.message(_("Copy"));
481 case LFUN_MOUSE_PRESS:
482 lfunMousePress(cur, cmd);
485 case LFUN_MOUSE_MOTION:
486 lfunMouseMotion(cur, cmd);
489 case LFUN_MOUSE_RELEASE:
490 lfunMouseRelease(cur, cmd);
493 case LFUN_FINISHED_BACKWARD:
494 cur.bv().cursor() = cur;
497 case LFUN_FINISHED_FORWARD:
499 cur.bv().cursor() = cur;
502 case LFUN_CHAR_FORWARD:
503 cur.updateFlags(Update::Decoration | Update::FitCursor);
504 case LFUN_CHAR_FORWARD_SELECT:
505 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
506 cur.autocorrect() = false;
508 cur.macroModeClose();
509 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
510 cur.pushLeft(*cur.nextAtom().nucleus());
511 cur.inset().idxFirst(cur);
512 } else if (cur.posRight() || idxRight(cur)
513 || cur.popRight() || cur.selection())
516 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
521 case LFUN_CHAR_BACKWARD:
522 cur.updateFlags(Update::Decoration | Update::FitCursor);
523 case LFUN_CHAR_BACKWARD_SELECT:
524 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
525 cur.autocorrect() = false;
527 cur.macroModeClose();
528 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
530 cur.push(*cur.nextAtom().nucleus());
531 cur.inset().idxLast(cur);
532 } else if (cur.posLeft() || idxLeft(cur)
533 || cur.popLeft() || cur.selection())
536 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
541 case LFUN_CHAR_RIGHT:
542 case LFUN_CHAR_RIGHT_SELECT:
543 //FIXME: for visual cursor, really move right
544 if (reverseDirectionNeeded(cur))
545 lyx::dispatch(FuncRequest(
546 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
547 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
549 lyx::dispatch(FuncRequest(
550 cmd.action == LFUN_CHAR_RIGHT_SELECT ?
551 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
555 case LFUN_CHAR_LEFT_SELECT:
556 //FIXME: for visual cursor, really move left
557 if (reverseDirectionNeeded(cur))
558 lyx::dispatch(FuncRequest(
559 cmd.action == LFUN_CHAR_LEFT_SELECT ?
560 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
562 lyx::dispatch(FuncRequest(
563 cmd.action == LFUN_CHAR_LEFT_SELECT ?
564 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
569 cur.updateFlags(Update::Decoration | Update::FitCursor);
570 case LFUN_DOWN_SELECT:
571 case LFUN_UP_SELECT: {
572 // close active macro
573 if (cur.inMacroMode()) {
574 cur.macroModeClose();
578 // stop/start the selection
579 bool select = cmd.action == LFUN_DOWN_SELECT ||
580 cmd.action == LFUN_UP_SELECT;
581 cur.selHandle(select);
584 bool up = cmd.action == LFUN_UP || cmd.action == LFUN_UP_SELECT;
585 bool successful = cur.upDownInMath(up);
587 // notify left insets and give them chance to set update flags
588 lyx::notifyCursorLeaves(cur.beforeDispatchCursor(), cur);
593 if (cur.fixIfBroken())
594 // FIXME: Something bad happened. We pass the corrected Cursor
595 // instead of letting things go worse.
598 // We did not manage to move the cursor.
603 case LFUN_MOUSE_DOUBLE:
604 case LFUN_MOUSE_TRIPLE:
605 case LFUN_WORD_SELECT:
609 cur.selection() = true;
610 cur.pos() = cur.lastpos();
611 cur.idx() = cur.lastidx();
614 case LFUN_PARAGRAPH_UP:
615 case LFUN_PARAGRAPH_DOWN:
616 cur.updateFlags(Update::Decoration | Update::FitCursor);
617 case LFUN_PARAGRAPH_UP_SELECT:
618 case LFUN_PARAGRAPH_DOWN_SELECT:
621 case LFUN_LINE_BEGIN:
622 case LFUN_WORD_BACKWARD:
623 cur.updateFlags(Update::Decoration | Update::FitCursor);
624 case LFUN_LINE_BEGIN_SELECT:
625 case LFUN_WORD_BACKWARD_SELECT:
626 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
627 cmd.action == LFUN_LINE_BEGIN_SELECT);
628 cur.macroModeClose();
629 if (cur.pos() != 0) {
631 } else if (cur.col() != 0) {
632 cur.idx() -= cur.col();
634 } else if (cur.idx() != 0) {
638 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
643 case LFUN_WORD_FORWARD:
645 cur.updateFlags(Update::Decoration | Update::FitCursor);
646 case LFUN_WORD_FORWARD_SELECT:
647 case LFUN_LINE_END_SELECT:
648 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
649 cmd.action == LFUN_LINE_END_SELECT);
650 cur.macroModeClose();
652 if (cur.pos() != cur.lastpos()) {
653 cur.pos() = cur.lastpos();
654 } else if (ncols() && (cur.col() != cur.lastcol())) {
655 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
656 cur.pos() = cur.lastpos();
657 } else if (cur.idx() != cur.lastidx()) {
658 cur.idx() = cur.lastidx();
659 cur.pos() = cur.lastpos();
661 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
666 case LFUN_SCREEN_UP_SELECT:
667 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
671 case LFUN_SCREEN_DOWN_SELECT:
672 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
676 case LFUN_CELL_FORWARD:
677 cur.updateFlags(Update::Decoration | Update::FitCursor);
678 cur.inset().idxNext(cur);
681 case LFUN_CELL_BACKWARD:
682 cur.updateFlags(Update::Decoration | Update::FitCursor);
683 cur.inset().idxPrev(cur);
686 case LFUN_WORD_DELETE_BACKWARD:
687 case LFUN_CHAR_DELETE_BACKWARD:
689 // May affect external cell:
690 cur.recordUndoInset();
693 // if the inset can not be removed from within, delete it
694 if (!cur.backspace()) {
695 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
696 cur.innerText()->dispatch(cur, cmd);
700 case LFUN_WORD_DELETE_FORWARD:
701 case LFUN_CHAR_DELETE_FORWARD:
702 if (cur.pos() == cur.lastpos())
703 // May affect external cell:
704 cur.recordUndoInset();
707 // if the inset can not be removed from within, delete it
709 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
710 cur.innerText()->dispatch(cur, cmd);
716 cur.clearSelection();
718 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
723 case LFUN_INSET_TOGGLE:
729 case LFUN_SELF_INSERT:
730 if (cmd.argument().size() != 1) {
732 docstring const arg = cmd.argument();
733 if (!interpretString(cur, arg))
737 // Don't record undo steps if we are in macro mode and
738 // cmd.argument is the next character of the macro name.
739 // Otherwise we'll get an invalid cursor if we undo after
740 // the macro was finished and the macro is a known command,
741 // e.g. sqrt. Cursor::macroModeClose replaces in this case
742 // the InsetMathUnknown with name "frac" by an empty
743 // InsetMathFrac -> a pos value > 0 is invalid.
744 // A side effect is that an undo before the macro is finished
745 // undoes the complete macro, not only the last character.
746 if (!cur.inMacroMode())
749 // spacial handling of space. If we insert an inset
750 // via macro mode, we want to put the cursor inside it
751 // if relevant. Think typing "\frac<space>".
752 if (cmd.argument()[0] == ' '
753 && cur.inMacroMode() && cur.macroName() != "\\"
754 && cur.macroModeClose()) {
755 MathAtom const atom = cur.prevAtom();
756 if (atom->asNestInset() && atom->isActive()) {
758 cur.pushLeft(*cur.nextInset());
760 } else if (!interpretChar(cur, cmd.argument()[0])) {
761 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
766 //case LFUN_SERVER_GET_XY:
767 // sprintf(dispatch_buffer, "%d %d",);
770 case LFUN_SERVER_SET_XY: {
771 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
774 istringstream is(to_utf8(cmd.argument()));
776 cur.setScreenPos(x, y);
780 // Special casing for superscript in case of LyX handling
782 case LFUN_ACCENT_CIRCUMFLEX:
783 if (cmd.argument().empty()) {
784 // do superscript if LyX handles
787 script(cur, true, grabAndEraseSelection(cur));
791 case LFUN_ACCENT_UMLAUT:
792 case LFUN_ACCENT_ACUTE:
793 case LFUN_ACCENT_GRAVE:
794 case LFUN_ACCENT_BREVE:
795 case LFUN_ACCENT_DOT:
796 case LFUN_ACCENT_MACRON:
797 case LFUN_ACCENT_CARON:
798 case LFUN_ACCENT_TILDE:
799 case LFUN_ACCENT_CEDILLA:
800 case LFUN_ACCENT_CIRCLE:
801 case LFUN_ACCENT_UNDERDOT:
802 case LFUN_ACCENT_TIE:
803 case LFUN_ACCENT_OGONEK:
804 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
808 case LFUN_FONT_FREE_APPLY:
809 case LFUN_FONT_FREE_UPDATE:
810 handleFont2(cur, cmd.argument());
814 if (currentMode() == TEXT_MODE)
815 handleFont(cur, cmd.argument(), "textbf");
817 handleFont(cur, cmd.argument(), "mathbf");
820 if (currentMode() == TEXT_MODE)
821 handleFont(cur, cmd.argument(), "textsf");
823 handleFont(cur, cmd.argument(), "mathsf");
826 if (currentMode() == TEXT_MODE)
827 handleFont(cur, cmd.argument(), "emph");
829 handleFont(cur, cmd.argument(), "mathcal");
831 case LFUN_FONT_ROMAN:
832 if (currentMode() == TEXT_MODE)
833 handleFont(cur, cmd.argument(), "textrm");
835 handleFont(cur, cmd.argument(), "mathrm");
837 case LFUN_FONT_TYPEWRITER:
838 if (currentMode() == TEXT_MODE)
839 handleFont(cur, cmd.argument(), "texttt");
841 handleFont(cur, cmd.argument(), "mathtt");
844 handleFont(cur, cmd.argument(), "mathfrak");
847 if (currentMode() == TEXT_MODE)
848 handleFont(cur, cmd.argument(), "textit");
850 handleFont(cur, cmd.argument(), "mathit");
853 if (currentMode() == TEXT_MODE)
854 // FIXME: should be "noun"
855 handleFont(cur, cmd.argument(), "textsc");
857 handleFont(cur, cmd.argument(), "mathbb");
860 case LFUN_FONT_FREE_APPLY:
861 handleFont(cur, cmd.argument(), "textrm");
864 case LFUN_FONT_DEFAULT:
865 handleFont(cur, cmd.argument(), "textnormal");
868 case LFUN_MATH_MODE: {
870 // ignore math-mode on when already in math mode
871 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
873 cur.macroModeClose();
874 docstring const save_selection = grabAndEraseSelection(cur);
876 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
877 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
879 cur.pushLeft(*cur.nextInset());
880 cur.niceInsert(save_selection);
882 if (currentMode() == Inset::TEXT_MODE) {
883 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
884 cur.message(_("create new math text environment ($...$)"));
886 handleFont(cur, cmd.argument(), "textrm");
887 cur.message(_("entered math text mode (textrm)"));
900 case LFUN_MATH_MATRIX: {
906 idocstringstream is(cmd.argument());
907 is >> m >> n >> v_align >> h_align;
914 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
918 case LFUN_MATH_DELIM: {
920 docstring rs = support::split(cmd.argument(), ls, ' ');
921 // Reasonable default values
927 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
931 case LFUN_MATH_BIGDELIM: {
932 docstring const lname = from_utf8(cmd.getArg(0));
933 docstring const ldelim = from_utf8(cmd.getArg(1));
934 docstring const rname = from_utf8(cmd.getArg(2));
935 docstring const rdelim = from_utf8(cmd.getArg(3));
936 latexkeys const * l = in_word_set(lname);
937 bool const have_l = l && l->inset == "big" &&
938 InsetMathBig::isBigInsetDelim(ldelim);
939 l = in_word_set(rname);
940 bool const have_r = l && l->inset == "big" &&
941 InsetMathBig::isBigInsetDelim(rdelim);
942 // We mimic LFUN_MATH_DELIM in case we have an empty left
943 // or right delimiter.
944 if (have_l || have_r) {
946 docstring const selection = grabAndEraseSelection(cur);
949 cur.insert(MathAtom(new InsetMathBig(lname,
951 cur.niceInsert(selection);
953 cur.insert(MathAtom(new InsetMathBig(rname,
956 // Don't call cur.undispatched() if we did nothing, this would
957 // lead to infinite recursion via Text::dispatch().
961 case LFUN_SPACE_INSERT:
962 case LFUN_MATH_SPACE:
964 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
967 case LFUN_ERT_INSERT:
968 // interpret this as if a backslash was typed
970 interpretChar(cur, '\\');
973 case LFUN_MATH_SUBSCRIPT:
974 // interpret this as if a _ was typed
976 interpretChar(cur, '_');
979 case LFUN_MATH_SUPERSCRIPT:
980 // interpret this as if a ^ was typed
982 interpretChar(cur, '^');
985 case LFUN_QUOTE_INSERT:
986 // interpret this as if a straight " was typed
988 interpretChar(cur, '\"');
991 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
992 // handling such that "self-insert" works on "arbitrary stuff" too, and
993 // math-insert only handles special math things like "matrix".
994 case LFUN_MATH_INSERT: {
996 if (cmd.argument() == "^" || cmd.argument() == "_") {
997 interpretChar(cur, cmd.argument()[0]);
999 cur.niceInsert(cmd.argument());
1003 case LFUN_DIALOG_SHOW_NEW_INSET: {
1004 docstring const & name = cmd.argument();
1006 if (name == "ref") {
1007 InsetMathRef tmp(name);
1008 data = tmp.createDialogStr(to_utf8(name));
1010 cur.bv().showInsetDialog(to_utf8(name), data, 0);
1014 case LFUN_INSET_INSERT: {
1016 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1023 case LFUN_INSET_DISSOLVE:
1024 if (!asHullInset()) {
1025 cur.recordUndoInset();
1031 InsetMath::doDispatch(cur, cmd);
1037 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1038 FuncStatus & flag) const
1040 // the font related toggles
1041 //string tc = "mathnormal";
1043 string const arg = to_utf8(cmd.argument());
1044 switch (cmd.action) {
1045 case LFUN_TABULAR_FEATURE:
1046 flag.enabled(false);
1049 case LFUN_TABULAR_FEATURE:
1050 // FIXME: check temporarily disabled
1052 char align = mathcursor::valign();
1053 if (align == '\0') {
1057 if (cmd.argument().empty()) {
1061 if (!contains("tcb", cmd.argument()[0])) {
1065 flag.setOnOff(cmd.argument()[0] == align);
1068 /// We have to handle them since 1.4 blocks all unhandled actions
1069 case LFUN_FONT_ITAL:
1070 case LFUN_FONT_BOLD:
1071 case LFUN_FONT_SANS:
1072 case LFUN_FONT_EMPH:
1073 case LFUN_FONT_TYPEWRITER:
1074 case LFUN_FONT_NOUN:
1075 case LFUN_FONT_ROMAN:
1076 case LFUN_FONT_DEFAULT:
1079 case LFUN_MATH_MUTATE:
1080 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1081 flag.setOnOff(false);
1084 // we just need to be in math mode to enable that
1085 case LFUN_MATH_SIZE:
1086 case LFUN_MATH_SPACE:
1087 case LFUN_MATH_LIMITS:
1088 case LFUN_MATH_NONUMBER:
1089 case LFUN_MATH_NUMBER:
1090 case LFUN_MATH_EXTERN:
1094 case LFUN_FONT_FRAK:
1095 flag.enabled(currentMode() != TEXT_MODE);
1098 case LFUN_MATH_INSERT: {
1099 bool const textarg =
1100 arg == "\\textbf" || arg == "\\textsf" ||
1101 arg == "\\textrm" || arg == "\\textmd" ||
1102 arg == "\\textit" || arg == "\\textsc" ||
1103 arg == "\\textsl" || arg == "\\textup" ||
1104 arg == "\\texttt" || arg == "\\textbb" ||
1105 arg == "\\textnormal";
1106 flag.enabled(currentMode() != TEXT_MODE || textarg);
1110 case LFUN_MATH_MATRIX:
1111 flag.enabled(currentMode() == MATH_MODE);
1114 case LFUN_INSET_INSERT: {
1115 // Don't test createMathInset_fromDialogStr(), since
1116 // getStatus is not called with a valid reference and the
1117 // dialog would not be applyable.
1118 string const name = cmd.getArg(0);
1119 flag.enabled(name == "ref");
1123 case LFUN_MATH_DELIM:
1124 case LFUN_MATH_BIGDELIM:
1125 // Don't do this with multi-cell selections
1126 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1129 case LFUN_HYPHENATION_POINT_INSERT:
1130 case LFUN_LIGATURE_BREAK_INSERT:
1131 case LFUN_MENU_SEPARATOR_INSERT:
1132 case LFUN_DOTS_INSERT:
1133 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1134 // FIXME: These would probably make sense in math-text mode
1135 flag.enabled(false);
1138 case LFUN_INSET_DISSOLVE:
1139 flag.enabled(!asHullInset());
1150 void InsetMathNest::edit(Cursor & cur, bool left)
1153 cur.idx() = left ? 0 : cur.lastidx();
1154 cur.pos() = left ? 0 : cur.lastpos();
1156 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1160 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1163 int dist_min = 1000000;
1164 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1165 int const d = cell(i).dist(cur.bv(), x, y);
1171 MathData & ar = cell(idx_min);
1173 cur.idx() = idx_min;
1174 cur.pos() = ar.x2pos(x - ar.xo(cur.bv()));
1176 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1177 if (dist_min == 0) {
1179 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1180 if (ar[i]->covers(cur.bv(), x, y))
1181 return ar[i].nucleus()->editXY(cur, x, y);
1187 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1189 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1190 BufferView & bv = cur.bv();
1191 bool do_selection = cmd.button() == mouse_button::button1
1192 && cmd.argument() == "region-select";
1193 bv.mouseSetCursor(cur, do_selection);
1194 if (cmd.button() == mouse_button::button1) {
1195 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1196 // Update the cursor update flags as needed:
1198 // Update::Decoration: tells to update the decoration
1199 // (visual box corners that define
1201 // Update::FitCursor: adjust the screen to the cursor
1202 // position if needed
1203 // cur.result().update(): don't overwrite previously set flags.
1204 cur.updateFlags(Update::Decoration | Update::FitCursor
1205 | cur.result().update());
1206 } else if (cmd.button() == mouse_button::button2) {
1207 if (cap::selection()) {
1208 // See comment in Text::dispatch why we do this
1209 cap::copySelectionToStack();
1210 cmd = FuncRequest(LFUN_PASTE, "0");
1211 doDispatch(bv.cursor(), cmd);
1214 asArray(theSelection().get(), ar);
1215 bv.cursor().insert(ar);
1221 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1223 // only select with button 1
1224 if (cmd.button() == mouse_button::button1) {
1225 Cursor & bvcur = cur.bv().cursor();
1226 if (bvcur.anchor_.hasPart(cur)) {
1227 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1228 bvcur.setCursor(cur);
1229 bvcur.selection() = true;
1230 //lyxerr << "MOTION " << bvcur << endl;
1237 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1239 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1241 if (cmd.button() == mouse_button::button1) {
1242 if (!cur.selection())
1245 Cursor & bvcur = cur.bv().cursor();
1246 bvcur.selection() = true;
1255 bool InsetMathNest::interpretChar(Cursor & cur, char_type c)
1257 //lyxerr << "interpret 2: '" << c << "'" << endl;
1258 docstring save_selection;
1259 if (c == '^' || c == '_')
1260 save_selection = grabAndEraseSelection(cur);
1265 if (cur.inMacroMode()) {
1266 docstring name = cur.macroName();
1268 /// are we currently typing '#1' or '#2' or...?
1269 if (name == "\\#") {
1272 if (n >= 1 && n <= 9)
1273 cur.insert(new MathMacroArgument(n));
1277 if (isAlphaASCII(c)) {
1278 cur.activeMacro()->setName(name + docstring(1, c));
1282 // handle 'special char' macros
1287 if (currentMode() == InsetMath::TEXT_MODE)
1288 cur.niceInsert(createInsetMath("textbackslash"));
1290 cur.niceInsert(createInsetMath("backslash"));
1291 } else if (c == '{') {
1293 cur.niceInsert(MathAtom(new InsetMathBrace));
1294 } else if (c == '%') {
1296 cur.niceInsert(MathAtom(new InsetMathComment));
1297 } else if (c == '#') {
1298 BOOST_ASSERT(cur.activeMacro());
1299 cur.activeMacro()->setName(name + docstring(1, c));
1302 cur.niceInsert(createInsetMath(docstring(1, c)));
1307 // One character big delimiters. The others are handled in
1308 // interpretString().
1309 latexkeys const * l = in_word_set(name.substr(1));
1310 if (name[0] == '\\' && l && l->inset == "big") {
1314 delim = from_ascii("\\{");
1317 delim = from_ascii("\\}");
1320 delim = docstring(1, c);
1323 if (InsetMathBig::isBigInsetDelim(delim)) {
1324 // name + delim ared a valid InsetMathBig.
1325 // We can't use cur.macroModeClose() because
1326 // it does not handle delim.
1327 InsetMathUnknown * p = cur.activeMacro();
1330 cur.cell().erase(cur.pos());
1331 cur.plainInsert(MathAtom(
1332 new InsetMathBig(name.substr(1), delim)));
1337 // leave macro mode and try again if necessary
1338 cur.macroModeClose();
1340 cur.niceInsert(MathAtom(new InsetMathBrace));
1342 interpretChar(cur, c);
1346 // This is annoying as one has to press <space> far too often.
1350 // leave autocorrect mode if necessary
1351 if (autocorrect() && c == ' ') {
1352 autocorrect() = false;
1357 // just clear selection on pressing the space bar
1358 if (cur.selection() && c == ' ') {
1359 cur.selection() = false;
1366 //lyxerr << "starting with macro" << endl;
1367 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), false)));
1372 if (currentMode() == InsetMath::TEXT_MODE)
1378 if (currentMode() == InsetMath::TEXT_MODE) {
1379 // insert spaces in text mode,
1380 // but suppress direct insertion of two spaces in a row
1381 // the still allows typing '<space>a<space>' and deleting the 'a', but
1382 // it is better than nothing...
1383 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1385 // FIXME: we have to enable full redraw here because of the
1386 // visual box corners that define the inset. If we know for
1387 // sure that we stay within the same cell we can optimize for
1389 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1393 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1394 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1395 // FIXME: we have to enable full redraw here because of the
1396 // visual box corners that define the inset. If we know for
1397 // sure that we stay within the same cell we can optimize for
1399 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1403 if (cur.popRight()) {
1404 // FIXME: we have to enable full redraw here because of the
1405 // visual box corners that define the inset. If we know for
1406 // sure that we stay within the same cell we can optimize for
1408 //cur.updateFlags(Update::FitCursor);
1412 // if we are at the very end, leave the formula
1413 return cur.pos() != cur.lastpos();
1416 // These shouldn't work in text mode:
1417 if (currentMode() != InsetMath::TEXT_MODE) {
1419 script(cur, false, save_selection);
1423 script(cur, true, save_selection);
1427 cur.niceInsert(createInsetMath("sim"));
1432 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1433 c == '%' || c == '_' || c == '^') {
1434 cur.niceInsert(createInsetMath(docstring(1, c)));
1439 // try auto-correction
1440 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1443 // no special circumstances, so insert the character without any fuss
1445 cur.autocorrect() = true;
1450 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1452 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1454 if (!cur.empty() && cur.pos() > 0 &&
1455 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1456 if (InsetMathBig::isBigInsetDelim(str)) {
1457 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1458 if (prev[0] == '\\') {
1459 prev = prev.substr(1);
1460 latexkeys const * l = in_word_set(prev);
1461 if (l && l->inset == "big") {
1462 cur.cell()[cur.pos() - 1] =
1463 MathAtom(new InsetMathBig(prev, str));
1473 bool InsetMathNest::script(Cursor & cur, bool up,
1474 docstring const & save_selection)
1476 // Hack to get \^ and \_ working
1477 //lyxerr << "handling script: up: " << up << endl;
1478 if (cur.inMacroMode() && cur.macroName() == "\\") {
1480 cur.niceInsert(createInsetMath("mathcircumflex"));
1482 interpretChar(cur, '_');
1486 cur.macroModeClose();
1487 if (asScriptInset() && cur.idx() == 0) {
1488 // we are in a nucleus of a script inset, move to _our_ script
1489 InsetMathScript * inset = asScriptInset();
1490 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1492 cur.idx() = inset->idxOfScript(up);
1494 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1496 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1499 cur.idx() = inset->idxOfScript(up);
1500 cur.pos() = cur.lastpos();
1502 // convert the thing to our left to a scriptinset or create a new
1503 // one if in the very first position of the array
1504 if (cur.pos() == 0) {
1505 //lyxerr << "new scriptinset" << endl;
1506 cur.insert(new InsetMathScript(up));
1508 //lyxerr << "converting prev atom " << endl;
1509 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1512 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1513 // See comment in MathParser.cpp for special handling of {}-bases
1519 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1520 cur.niceInsert(save_selection);
1522 //lyxerr << "inserting selection 2:\n" << save_selection << endl;