2 * \file InsetMathNest.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetMathNest.h"
15 #include "InsetMathArray.h"
16 #include "InsetMathBig.h"
17 #include "InsetMathBox.h"
18 #include "InsetMathBrace.h"
19 #include "InsetMathColor.h"
20 #include "InsetMathComment.h"
22 #include "InsetMathDelim.h"
23 #include "MathFactory.h"
24 #include "InsetMathHull.h"
25 #include "MathStream.h"
26 #include "MathMacroArgument.h"
27 //#include "InsetMathMBox.h"
28 #include "MathParser.h"
29 #include "InsetMathScript.h"
30 #include "InsetMathSpace.h"
31 #include "InsetMathSymbol.h"
32 #include "MathSupport.h"
33 #include "InsetMathUnknown.h"
34 #include "InsetMathRef.h"
36 #include "BufferView.h"
37 #include "CutAndPaste.h"
38 #include "FuncStatus.h"
40 #include "bufferview_funcs.h"
41 #include "coordcache.h"
44 #include "dispatchresult.h"
45 #include "funcrequest.h"
48 #include "outputparams.h"
51 #include "support/lstrings.h"
52 #include "support/textutils.h"
54 #include "frontends/Clipboard.h"
55 #include "frontends/Painter.h"
56 #include "frontends/Selection.h"
58 #include "funcrequest.h"
59 #include "lyxserver.h"
60 #include "lyxsocket.h"
67 using cap::copySelection;
68 using cap::grabAndEraseSelection;
69 using cap::cutSelection;
70 using cap::replaceSelection;
71 using cap::selClearOrDel;
75 using std::istringstream;
78 InsetMathNest::InsetMathNest(idx_type nargs)
79 : cells_(nargs), lock_(false)
83 InsetMath::idx_type InsetMathNest::nargs() const
89 MathArray & InsetMathNest::cell(idx_type i)
95 MathArray const & InsetMathNest::cell(idx_type i) const
101 void InsetMathNest::cursorPos(BufferView const & bv,
102 CursorSlice const & sl, bool /*boundary*/,
103 int & x, int & y) const
105 // FIXME: This is a hack. Ideally, the coord cache should not store
106 // absolute positions, but relative ones. This would mean to call
107 // setXY() not in MathArray::draw(), but in the parent insets' draw()
108 // with the correctly adjusted x,y values. But this means that we'd have
109 // to touch all (math)inset's draw() methods. Right now, we'll store
110 // absolute value, and make them here relative, only to make them
111 // absolute again when actually drawing the cursor. What a mess.
112 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
113 MathArray const & ar = sl.cell();
114 CoordCache const & coord_cache = bv.coordCache();
115 if (!coord_cache.getArrays().has(&ar)) {
116 // this can (semi-)legally happen if we just created this cell
117 // and it never has been drawn before. So don't ASSERT.
118 //lyxerr << "no cached data for array " << &ar << endl;
123 Point const pt = coord_cache.getArrays().xy(&ar);
124 if (!coord_cache.getInsets().has(this)) {
126 //lyxerr << "no cached data for inset " << this << endl;
131 Point const pt2 = coord_cache.getInsets().xy(this);
132 //lyxerr << "retrieving position cache for MathArray "
133 // << pt.x_ << ' ' << pt.y_ << std::endl;
134 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
136 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
137 // << " asc: " << ascent() << " des: " << descent()
138 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
139 // move cursor visually into empty cells ("blue rectangles");
145 void InsetMathNest::metrics(MetricsInfo const & mi) const
148 for (idx_type i = 0, n = nargs(); i != n; ++i)
153 bool InsetMathNest::idxNext(LCursor & cur) const
155 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
156 if (cur.idx() == cur.lastidx())
164 bool InsetMathNest::idxRight(LCursor & cur) const
170 bool InsetMathNest::idxPrev(LCursor & cur) const
172 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
176 cur.pos() = cur.lastpos();
181 bool InsetMathNest::idxLeft(LCursor & cur) const
187 bool InsetMathNest::idxFirst(LCursor & cur) const
189 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
198 bool InsetMathNest::idxLast(LCursor & cur) const
200 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
203 cur.idx() = cur.lastidx();
204 cur.pos() = cur.lastpos();
209 void InsetMathNest::dump() const
211 odocstringstream oss;
213 os << "---------------------------------------------\n";
216 for (idx_type i = 0, n = nargs(); i != n; ++i)
217 os << cell(i) << "\n";
218 os << "---------------------------------------------\n";
219 lyxerr << to_utf8(oss.str());
223 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
227 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
230 setPosCache(pi, x, y);
234 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
236 BufferView & bv = *pi.base.bv;
237 // this should use the x/y values given, not the cached values
238 LCursor & cur = bv.cursor();
239 if (!cur.selection())
241 if (!ptr_cmp(&cur.inset(), this))
244 // FIXME: hack to get position cache warm
245 pi.pain.setDrawingEnabled(false);
247 pi.pain.setDrawingEnabled(true);
249 CursorSlice s1 = cur.selBegin();
250 CursorSlice s2 = cur.selEnd();
252 //lyxerr << "InsetMathNest::drawing selection: "
253 // << " s1: " << s1 << " s2: " << s2 << endl;
254 if (s1.idx() == s2.idx()) {
255 MathArray const & c = cell(s1.idx());
256 int x1 = c.xo(bv) + c.pos2x(s1.pos());
257 int y1 = c.yo(bv) - c.ascent();
258 int x2 = c.xo(bv) + c.pos2x(s2.pos());
259 int y2 = c.yo(bv) + c.descent();
260 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
261 //lyxerr << "InsetMathNest::drawing selection 3: "
262 // << " x1: " << x1 << " x2: " << x2
263 // << " y1: " << y1 << " y2: " << y2 << endl;
265 for (idx_type i = 0; i < nargs(); ++i) {
266 if (idxBetween(i, s1.idx(), s2.idx())) {
267 MathArray const & c = cell(i);
269 int y1 = c.yo(bv) - c.ascent();
270 int x2 = c.xo(bv) + c.width();
271 int y2 = c.yo(bv) + c.descent();
272 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
279 void InsetMathNest::validate(LaTeXFeatures & features) const
281 for (idx_type i = 0; i < nargs(); ++i)
282 cell(i).validate(features);
286 void InsetMathNest::replace(ReplaceData & rep)
288 for (idx_type i = 0; i < nargs(); ++i)
289 cell(i).replace(rep);
293 bool InsetMathNest::contains(MathArray const & ar) const
295 for (idx_type i = 0; i < nargs(); ++i)
296 if (cell(i).contains(ar))
302 bool InsetMathNest::lock() const
308 void InsetMathNest::lock(bool l)
314 bool InsetMathNest::isActive() const
320 MathArray InsetMathNest::glue() const
323 for (size_t i = 0; i < nargs(); ++i)
329 void InsetMathNest::write(WriteStream & os) const
331 os << '\\' << name().c_str();
332 for (size_t i = 0; i < nargs(); ++i)
333 os << '{' << cell(i) << '}';
335 os.pendingSpace(true);
336 if (lock_ && !os.latex()) {
338 os.pendingSpace(true);
343 void InsetMathNest::normalize(NormalStream & os) const
345 os << '[' << name().c_str();
346 for (size_t i = 0; i < nargs(); ++i)
347 os << ' ' << cell(i);
352 int InsetMathNest::latex(Buffer const &, odocstream & os,
353 OutputParams const & runparams) const
355 WriteStream wi(os, runparams.moving_arg, true);
361 bool InsetMathNest::notifyCursorLeaves(LCursor & /*cur*/)
367 MathArray & ar = cur.cell();
368 // remove base-only "scripts"
369 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
370 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
371 if (p && p->nargs() == 1) {
372 MathArray ar = p->nuc();
375 cur.adjust(i, ar.size() - 1);
379 // glue adjacent font insets of the same kind
380 for (pos_type i = 0; i + 1 < size(); ++i) {
381 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
382 InsetMathFont const * q = operator[](i + 1)->asFontInset();
383 if (p && q && p->name() == q->name()) {
384 p->cell(0).append(q->cell(0));
394 void InsetMathNest::handleFont
395 (LCursor & cur, docstring const & arg, char const * const font)
397 handleFont(cur, arg, from_ascii(font));
401 void InsetMathNest::handleFont
402 (LCursor & cur, docstring const & arg, docstring const & font)
404 // this whole function is a hack and won't work for incremental font
407 if (cur.inset().asInsetMath()->name() == font) {
408 recordUndoInset(cur, Undo::ATOMIC);
409 cur.handleFont(to_utf8(font));
411 recordUndo(cur, Undo::ATOMIC);
412 cur.handleNest(createInsetMath(font));
418 void InsetMathNest::handleFont2(LCursor & cur, docstring const & arg)
420 recordUndo(cur, Undo::ATOMIC);
423 bv_funcs::string2font(to_utf8(arg), font, b);
424 if (font.color() != LColor::inherit) {
425 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
426 cur.handleNest(at, 0);
431 void InsetMathNest::doDispatch(LCursor & cur, FuncRequest & cmd)
433 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
434 //CursorSlice sl = cur.current();
436 switch (cmd.action) {
440 cur.message(_("Paste"));
441 replaceSelection(cur);
443 if (cmd.argument().empty() && !theClipboard().isInternal())
444 topaste = theClipboard().getAsText();
447 idocstringstream is(cmd.argument());
449 topaste = cap::getSelection(cur.buffer(), n);
451 cur.niceInsert(topaste);
452 cur.clearSelection(); // bug 393
453 cur.bv().switchKeyMap();
460 cutSelection(cur, true, true);
461 cur.message(_("Cut"));
462 // Prevent stale position >= size crash
463 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
469 cur.message(_("Copy"));
472 case LFUN_MOUSE_PRESS:
473 lfunMousePress(cur, cmd);
476 case LFUN_MOUSE_MOTION:
477 lfunMouseMotion(cur, cmd);
480 case LFUN_MOUSE_RELEASE:
481 lfunMouseRelease(cur, cmd);
484 case LFUN_FINISHED_LEFT:
485 cur.bv().cursor() = cur;
488 case LFUN_FINISHED_RIGHT:
490 cur.bv().cursor() = cur;
493 case LFUN_FINISHED_UP:
494 cur.bv().cursor() = cur;
497 case LFUN_FINISHED_DOWN:
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_RIGHT);
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_LEFT);
542 cur.updateFlags(Update::Decoration | Update::FitCursor);
544 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
545 if (cur.inMacroMode()) {
547 cur.macroModeClose();
550 cur.selHandle(cmd.action == LFUN_UP_SELECT);
552 cmd = FuncRequest(LFUN_FINISHED_UP);
555 // fixes bug 1598. Please check!
560 cur.updateFlags(Update::Decoration | Update::FitCursor);
561 case LFUN_DOWN_SELECT:
562 if (cur.inMacroMode()) {
563 cur.macroModeClose();
566 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
568 cmd = FuncRequest(LFUN_FINISHED_DOWN);
571 // fixes bug 1598. Please check!
575 case LFUN_MOUSE_DOUBLE:
576 case LFUN_MOUSE_TRIPLE:
577 case LFUN_WORD_SELECT:
581 cur.selection() = true;
582 cur.pos() = cur.lastpos();
583 cur.idx() = cur.lastidx();
584 cap::saveSelection(cur);
587 case LFUN_PARAGRAPH_UP:
588 case LFUN_PARAGRAPH_DOWN:
589 cur.updateFlags(Update::Decoration | Update::FitCursor);
590 case LFUN_PARAGRAPH_UP_SELECT:
591 case LFUN_PARAGRAPH_DOWN_SELECT:
594 case LFUN_LINE_BEGIN:
595 case LFUN_WORD_BACKWARD:
596 cur.updateFlags(Update::Decoration | Update::FitCursor);
597 case LFUN_LINE_BEGIN_SELECT:
598 case LFUN_WORD_BACKWARD_SELECT:
599 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
600 cmd.action == LFUN_LINE_BEGIN_SELECT);
601 cur.macroModeClose();
602 if (cur.pos() != 0) {
604 } else if (cur.col() != 0) {
605 cur.idx() -= cur.col();
607 } else if (cur.idx() != 0) {
611 cmd = FuncRequest(LFUN_FINISHED_LEFT);
616 case LFUN_WORD_FORWARD:
618 cur.updateFlags(Update::Decoration | Update::FitCursor);
619 case LFUN_WORD_FORWARD_SELECT:
620 case LFUN_LINE_END_SELECT:
621 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
622 cmd.action == LFUN_LINE_END_SELECT);
623 cur.macroModeClose();
625 if (cur.pos() != cur.lastpos()) {
626 cur.pos() = cur.lastpos();
627 } else if (ncols() && (cur.col() != cur.lastcol())) {
628 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
629 cur.pos() = cur.lastpos();
630 } else if (cur.idx() != cur.lastidx()) {
631 cur.idx() = cur.lastidx();
632 cur.pos() = cur.lastpos();
634 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
639 case LFUN_SCREEN_UP_SELECT:
641 cmd = FuncRequest(LFUN_FINISHED_LEFT);
645 case LFUN_SCREEN_DOWN_SELECT:
646 case LFUN_SCREEN_DOWN:
647 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
651 case LFUN_CELL_FORWARD:
652 cur.updateFlags(Update::Decoration | Update::FitCursor);
653 cur.inset().idxNext(cur);
656 case LFUN_CELL_BACKWARD:
657 cur.updateFlags(Update::Decoration | Update::FitCursor);
658 cur.inset().idxPrev(cur);
661 case LFUN_WORD_DELETE_BACKWARD:
662 case LFUN_CHAR_DELETE_BACKWARD:
664 // May affect external cell:
665 recordUndoInset(cur, Undo::ATOMIC);
667 recordUndo(cur, Undo::ATOMIC);
668 // if the inset can not be removed from within, delete it
669 if (!cur.backspace()) {
670 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
671 cur.innerText()->dispatch(cur, cmd);
675 case LFUN_WORD_DELETE_FORWARD:
676 case LFUN_CHAR_DELETE_FORWARD:
677 if (cur.pos() == cur.lastpos())
678 // May affect external cell:
679 recordUndoInset(cur, Undo::ATOMIC);
681 recordUndo(cur, Undo::ATOMIC);
682 // if the inset can not be removed from within, delete it
684 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
685 cur.innerText()->dispatch(cur, cmd);
691 cur.clearSelection();
693 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
698 case LFUN_INSET_TOGGLE:
704 case LFUN_SELF_INSERT:
705 if (cmd.argument().size() != 1) {
707 docstring const arg = cmd.argument();
708 if (!interpretString(cur, arg))
712 // Don't record undo steps if we are in macro mode and
713 // cmd.argument is the next character of the macro name.
714 // Otherwise we'll get an invalid cursor if we undo after
715 // the macro was finished and the macro is a known command,
716 // e.g. sqrt. LCursor::macroModeClose replaces in this case
717 // the InsetMathUnknown with name "frac" by an empty
718 // InsetMathFrac -> a pos value > 0 is invalid.
719 // A side effect is that an undo before the macro is finished
720 // undoes the complete macro, not only the last character.
721 if (!cur.inMacroMode())
724 // spacial handling of space. If we insert an inset
725 // via macro mode, we want to put the cursor inside it
726 // if relevant. Think typing "\frac<space>".
727 if (cmd.argument()[0] == ' '
728 && cur.inMacroMode() && cur.macroName() != "\\"
729 && cur.macroModeClose()) {
730 MathAtom const atom = cur.prevAtom();
731 if (atom->asNestInset() && atom->nargs() > 0) {
733 cur.pushLeft(*cur.nextInset());
735 } else if (!interpretChar(cur, cmd.argument()[0])) {
736 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
741 //case LFUN_SERVER_GET_XY:
742 // sprintf(dispatch_buffer, "%d %d",);
745 case LFUN_SERVER_SET_XY: {
746 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
749 istringstream is(to_utf8(cmd.argument()));
751 cur.setScreenPos(x, y);
755 // Special casing for superscript in case of LyX handling
757 case LFUN_ACCENT_CIRCUMFLEX:
758 if (cmd.argument().empty()) {
759 // do superscript if LyX handles
761 recordUndo(cur, Undo::ATOMIC);
762 script(cur, true, grabAndEraseSelection(cur));
766 case LFUN_ACCENT_UMLAUT:
767 case LFUN_ACCENT_ACUTE:
768 case LFUN_ACCENT_GRAVE:
769 case LFUN_ACCENT_BREVE:
770 case LFUN_ACCENT_DOT:
771 case LFUN_ACCENT_MACRON:
772 case LFUN_ACCENT_CARON:
773 case LFUN_ACCENT_TILDE:
774 case LFUN_ACCENT_CEDILLA:
775 case LFUN_ACCENT_CIRCLE:
776 case LFUN_ACCENT_UNDERDOT:
777 case LFUN_ACCENT_TIE:
778 case LFUN_ACCENT_OGONEK:
779 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
783 case LFUN_FONT_FREE_APPLY:
784 case LFUN_FONT_FREE_UPDATE:
785 handleFont2(cur, cmd.argument());
789 if (currentMode() == TEXT_MODE)
790 handleFont(cur, cmd.argument(), "textbf");
792 handleFont(cur, cmd.argument(), "mathbf");
795 if (currentMode() == TEXT_MODE)
796 handleFont(cur, cmd.argument(), "textsf");
798 handleFont(cur, cmd.argument(), "mathsf");
801 if (currentMode() == TEXT_MODE)
802 handleFont(cur, cmd.argument(), "emph");
804 handleFont(cur, cmd.argument(), "mathcal");
806 case LFUN_FONT_ROMAN:
807 if (currentMode() == TEXT_MODE)
808 handleFont(cur, cmd.argument(), "textrm");
810 handleFont(cur, cmd.argument(), "mathrm");
813 if (currentMode() == TEXT_MODE)
814 handleFont(cur, cmd.argument(), "texttt");
816 handleFont(cur, cmd.argument(), "mathtt");
819 handleFont(cur, cmd.argument(), "mathfrak");
822 if (currentMode() == TEXT_MODE)
823 handleFont(cur, cmd.argument(), "textit");
825 handleFont(cur, cmd.argument(), "mathit");
828 if (currentMode() == TEXT_MODE)
829 // FIXME: should be "noun"
830 handleFont(cur, cmd.argument(), "textsc");
832 handleFont(cur, cmd.argument(), "mathbb");
835 case LFUN_FONT_FREE_APPLY:
836 handleFont(cur, cmd.argument(), "textrm");
839 case LFUN_FONT_DEFAULT:
840 handleFont(cur, cmd.argument(), "textnormal");
843 case LFUN_MATH_MODE: {
845 // ignore math-mode on when already in math mode
846 if (currentMode() == InsetBase::MATH_MODE && cmd.argument() == "on")
848 cur.macroModeClose();
849 docstring const save_selection = grabAndEraseSelection(cur);
851 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
852 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
854 cur.pushLeft(*cur.nextInset());
855 cur.niceInsert(save_selection);
857 if (currentMode() == InsetBase::TEXT_MODE) {
858 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
859 cur.message(_("create new math text environment ($...$)"));
861 handleFont(cur, cmd.argument(), "textrm");
862 cur.message(_("entered math text mode (textrm)"));
875 case LFUN_MATH_MATRIX: {
876 recordUndo(cur, Undo::ATOMIC);
881 idocstringstream is(cmd.argument());
882 is >> m >> n >> v_align >> h_align;
889 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
893 case LFUN_MATH_DELIM: {
895 docstring rs = support::split(cmd.argument(), ls, ' ');
896 // Reasonable default values
901 recordUndo(cur, Undo::ATOMIC);
902 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
906 case LFUN_MATH_BIGDELIM: {
907 docstring const lname = from_utf8(cmd.getArg(0));
908 docstring const ldelim = from_utf8(cmd.getArg(1));
909 docstring const rname = from_utf8(cmd.getArg(2));
910 docstring const rdelim = from_utf8(cmd.getArg(3));
911 latexkeys const * l = in_word_set(lname);
912 bool const have_l = l && l->inset == "big" &&
913 InsetMathBig::isBigInsetDelim(ldelim);
914 l = in_word_set(rname);
915 bool const have_r = l && l->inset == "big" &&
916 InsetMathBig::isBigInsetDelim(rdelim);
917 // We mimic LFUN_MATH_DELIM in case we have an empty left
918 // or right delimiter.
919 if (have_l || have_r) {
920 recordUndo(cur, Undo::ATOMIC);
921 docstring const selection = grabAndEraseSelection(cur);
924 cur.insert(MathAtom(new InsetMathBig(lname,
926 cur.niceInsert(selection);
928 cur.insert(MathAtom(new InsetMathBig(rname,
931 // Don't call cur.undispatched() if we did nothing, this would
932 // lead to infinite recursion via LyXText::dispatch().
936 case LFUN_SPACE_INSERT:
937 case LFUN_MATH_SPACE:
938 recordUndo(cur, Undo::ATOMIC);
939 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
942 case LFUN_ERT_INSERT:
943 // interpret this as if a backslash was typed
944 recordUndo(cur, Undo::ATOMIC);
945 interpretChar(cur, '\\');
948 case LFUN_MATH_SUBSCRIPT:
949 // interpret this as if a _ was typed
950 recordUndo(cur, Undo::ATOMIC);
951 interpretChar(cur, '_');
954 case LFUN_MATH_SUPERSCRIPT:
955 // interpret this as if a ^ was typed
956 recordUndo(cur, Undo::ATOMIC);
957 interpretChar(cur, '^');
960 case LFUN_QUOTE_INSERT:
961 // interpret this as if a straight " was typed
962 recordUndo(cur, Undo::ATOMIC);
963 interpretChar(cur, '\"');
966 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
967 // handling such that "self-insert" works on "arbitrary stuff" too, and
968 // math-insert only handles special math things like "matrix".
969 case LFUN_MATH_INSERT: {
970 recordUndo(cur, Undo::ATOMIC);
971 if (cmd.argument() == "^" || cmd.argument() == "_") {
972 interpretChar(cur, cmd.argument()[0]);
974 cur.niceInsert(cmd.argument());
978 case LFUN_DIALOG_SHOW_NEW_INSET: {
979 docstring const & name = cmd.argument();
983 data = tmp.createDialogStr(to_utf8(name));
985 cur.bv().showInsetDialog(to_utf8(name), data, 0);
989 case LFUN_INSET_INSERT: {
991 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1000 InsetMathDim::doDispatch(cur, cmd);
1006 bool InsetMathNest::getStatus(LCursor & cur, FuncRequest const & cmd,
1007 FuncStatus & flag) const
1009 // the font related toggles
1010 //string tc = "mathnormal";
1012 string const arg = to_utf8(cmd.argument());
1013 switch (cmd.action) {
1014 case LFUN_TABULAR_FEATURE:
1015 flag.enabled(false);
1018 case LFUN_TABULAR_FEATURE:
1019 // FIXME: check temporarily disabled
1021 char align = mathcursor::valign();
1022 if (align == '\0') {
1026 if (cmd.argument().empty()) {
1030 if (!contains("tcb", cmd.argument()[0])) {
1034 flag.setOnOff(cmd.argument()[0] == align);
1037 /// We have to handle them since 1.4 blocks all unhandled actions
1038 case LFUN_FONT_ITAL:
1039 case LFUN_FONT_BOLD:
1040 case LFUN_FONT_SANS:
1041 case LFUN_FONT_EMPH:
1042 case LFUN_FONT_CODE:
1043 case LFUN_FONT_NOUN:
1044 case LFUN_FONT_ROMAN:
1045 case LFUN_FONT_DEFAULT:
1048 case LFUN_MATH_MUTATE:
1049 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1050 flag.setOnOff(false);
1053 // we just need to be in math mode to enable that
1054 case LFUN_MATH_SIZE:
1055 case LFUN_MATH_SPACE:
1056 case LFUN_MATH_LIMITS:
1057 case LFUN_MATH_NONUMBER:
1058 case LFUN_MATH_NUMBER:
1059 case LFUN_MATH_EXTERN:
1063 case LFUN_FONT_FRAK:
1064 flag.enabled(currentMode() != TEXT_MODE);
1067 case LFUN_MATH_INSERT: {
1068 bool const textarg =
1069 arg == "\\textbf" || arg == "\\textsf" ||
1070 arg == "\\textrm" || arg == "\\textmd" ||
1071 arg == "\\textit" || arg == "\\textsc" ||
1072 arg == "\\textsl" || arg == "\\textup" ||
1073 arg == "\\texttt" || arg == "\\textbb" ||
1074 arg == "\\textnormal";
1075 flag.enabled(currentMode() != TEXT_MODE || textarg);
1079 case LFUN_MATH_MATRIX:
1080 flag.enabled(currentMode() == MATH_MODE);
1083 case LFUN_INSET_INSERT: {
1084 // Don't test createMathInset_fromDialogStr(), since
1085 // getStatus is not called with a valid reference and the
1086 // dialog would not be applyable.
1087 string const name = cmd.getArg(0);
1088 flag.enabled(name == "ref");
1092 case LFUN_MATH_DELIM:
1093 case LFUN_MATH_BIGDELIM:
1094 // Don't do this with multi-cell selections
1095 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1106 void InsetMathNest::edit(LCursor & cur, bool left)
1109 cur.idx() = left ? 0 : cur.lastidx();
1110 cur.pos() = left ? 0 : cur.lastpos();
1112 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1116 InsetBase * InsetMathNest::editXY(LCursor & cur, int x, int y)
1119 int dist_min = 1000000;
1120 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1121 int const d = cell(i).dist(cur.bv(), x, y);
1127 MathArray & ar = cell(idx_min);
1129 cur.idx() = idx_min;
1130 cur.pos() = ar.x2pos(x - ar.xo(cur.bv()));
1132 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1133 if (dist_min == 0) {
1135 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1136 if (ar[i]->covers(cur.bv(), x, y))
1137 return ar[i].nucleus()->editXY(cur, x, y);
1143 void InsetMathNest::lfunMousePress(LCursor & cur, FuncRequest & cmd)
1145 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1146 BufferView & bv = cur.bv();
1147 if (cmd.button() == mouse_button::button1) {
1148 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1149 bv.mouseSetCursor(cur);
1150 // FIXME: we have to enable full redraw here because of the
1151 // visual box corners that define the inset.
1152 cur.updateFlags(Update::Decoration | Update::FitCursor);
1153 } else if (cmd.button() == mouse_button::button2) {
1155 if (cap::selection()) {
1156 // See comment in LyXText::dispatch why we do this
1157 cap::copySelectionToStack();
1158 cmd = FuncRequest(LFUN_PASTE, "0");
1159 doDispatch(cur, cmd);
1161 asArray(theSelection().get(), ar);
1164 bv.mouseSetCursor(cur);
1169 void InsetMathNest::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
1171 // only select with button 1
1172 if (cmd.button() == mouse_button::button1) {
1173 LCursor & bvcur = cur.bv().cursor();
1174 if (bvcur.anchor_.hasPart(cur)) {
1175 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1176 bvcur.setCursor(cur);
1177 bvcur.selection() = true;
1178 //lyxerr << "MOTION " << bvcur << endl;
1185 void InsetMathNest::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
1187 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1189 if (cmd.button() == mouse_button::button1) {
1190 if (!cur.selection())
1193 LCursor & bvcur = cur.bv().cursor();
1194 bvcur.selection() = true;
1195 cap::saveSelection(bvcur);
1200 if (cmd.button() == mouse_button::button3) {
1201 // try to dispatch to enclosed insets first
1202 cur.bv().showDialog("mathpanel");
1210 bool InsetMathNest::interpretChar(LCursor & cur, char_type c)
1212 //lyxerr << "interpret 2: '" << c << "'" << endl;
1213 docstring save_selection;
1214 if (c == '^' || c == '_')
1215 save_selection = grabAndEraseSelection(cur);
1220 if (cur.inMacroMode()) {
1221 docstring name = cur.macroName();
1223 /// are we currently typing '#1' or '#2' or...?
1224 if (name == "\\#") {
1227 if (n >= 1 && n <= 9)
1228 cur.insert(new MathMacroArgument(n));
1232 if (isAlphaASCII(c)) {
1233 cur.activeMacro()->setName(name + docstring(1, c));
1237 // handle 'special char' macros
1242 if (currentMode() == InsetMath::TEXT_MODE)
1243 cur.niceInsert(createInsetMath("textbackslash"));
1245 cur.niceInsert(createInsetMath("backslash"));
1246 } else if (c == '{') {
1248 cur.niceInsert(MathAtom(new InsetMathBrace));
1249 } else if (c == '%') {
1251 cur.niceInsert(MathAtom(new InsetMathComment));
1252 } else if (c == '#') {
1253 BOOST_ASSERT(cur.activeMacro());
1254 cur.activeMacro()->setName(name + docstring(1, c));
1257 cur.niceInsert(createInsetMath(docstring(1, c)));
1262 // One character big delimiters. The others are handled in
1263 // interpretString().
1264 latexkeys const * l = in_word_set(name.substr(1));
1265 if (name[0] == '\\' && l && l->inset == "big") {
1269 delim = from_ascii("\\{");
1272 delim = from_ascii("\\}");
1275 delim = docstring(1, c);
1278 if (InsetMathBig::isBigInsetDelim(delim)) {
1279 // name + delim ared a valid InsetMathBig.
1280 // We can't use cur.macroModeClose() because
1281 // it does not handle delim.
1282 InsetMathUnknown * p = cur.activeMacro();
1285 cur.cell().erase(cur.pos());
1286 cur.plainInsert(MathAtom(
1287 new InsetMathBig(name.substr(1), delim)));
1292 // leave macro mode and try again if necessary
1293 cur.macroModeClose();
1295 cur.niceInsert(MathAtom(new InsetMathBrace));
1297 interpretChar(cur, c);
1301 // This is annoying as one has to press <space> far too often.
1305 // leave autocorrect mode if necessary
1306 if (autocorrect() && c == ' ') {
1307 autocorrect() = false;
1312 // just clear selection on pressing the space bar
1313 if (cur.selection() && c == ' ') {
1314 cur.selection() = false;
1321 //lyxerr << "starting with macro" << endl;
1322 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), false)));
1327 if (currentMode() == InsetMath::TEXT_MODE)
1333 if (currentMode() == InsetMath::TEXT_MODE) {
1334 // insert spaces in text mode,
1335 // but suppress direct insertion of two spaces in a row
1336 // the still allows typing '<space>a<space>' and deleting the 'a', but
1337 // it is better than nothing...
1338 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1340 // FIXME: we have to enable full redraw here because of the
1341 // visual box corners that define the inset. If we know for
1342 // sure that we stay within the same cell we can optimize for
1344 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1348 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1349 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1350 // FIXME: we have to enable full redraw here because of the
1351 // visual box corners that define the inset. If we know for
1352 // sure that we stay within the same cell we can optimize for
1354 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1358 if (cur.popRight()) {
1359 // FIXME: we have to enable full redraw here because of the
1360 // visual box corners that define the inset. If we know for
1361 // sure that we stay within the same cell we can optimize for
1363 //cur.updateFlags(Update::FitCursor);
1367 // if we are at the very end, leave the formula
1368 return cur.pos() != cur.lastpos();
1371 // These shouldn't work in text mode:
1372 if (currentMode() != InsetMath::TEXT_MODE) {
1374 script(cur, false, save_selection);
1378 script(cur, true, save_selection);
1382 cur.niceInsert(createInsetMath("sim"));
1387 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1388 c == '%' || c == '_' || c == '^') {
1389 cur.niceInsert(createInsetMath(docstring(1, c)));
1394 // try auto-correction
1395 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1398 // no special circumstances, so insert the character without any fuss
1400 cur.autocorrect() = true;
1405 bool InsetMathNest::interpretString(LCursor & cur, docstring const & str)
1407 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1409 if (!cur.empty() && cur.pos() > 0 &&
1410 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1411 if (InsetMathBig::isBigInsetDelim(str)) {
1412 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1413 if (prev[0] == '\\') {
1414 prev = prev.substr(1);
1415 latexkeys const * l = in_word_set(prev);
1416 if (l && l->inset == "big") {
1417 cur.cell()[cur.pos() - 1] =
1418 MathAtom(new InsetMathBig(prev, str));
1428 bool InsetMathNest::script(LCursor & cur, bool up,
1429 docstring const & save_selection)
1431 // Hack to get \^ and \_ working
1432 //lyxerr << "handling script: up: " << up << endl;
1433 if (cur.inMacroMode() && cur.macroName() == "\\") {
1435 cur.niceInsert(createInsetMath("mathcircumflex"));
1437 interpretChar(cur, '_');
1441 cur.macroModeClose();
1442 if (asScriptInset() && cur.idx() == 0) {
1443 // we are in a nucleus of a script inset, move to _our_ script
1444 InsetMathScript * inset = asScriptInset();
1445 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1447 cur.idx() = inset->idxOfScript(up);
1449 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1451 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1454 cur.idx() = inset->idxOfScript(up);
1455 cur.pos() = cur.lastpos();
1457 // convert the thing to our left to a scriptinset or create a new
1458 // one if in the very first position of the array
1459 if (cur.pos() == 0) {
1460 //lyxerr << "new scriptinset" << endl;
1461 cur.insert(new InsetMathScript(up));
1463 //lyxerr << "converting prev atom " << endl;
1464 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1467 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1468 // See comment in MathParser.C for special handling of {}-bases
1474 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1475 cur.niceInsert(save_selection);
1477 //lyxerr << "inserting selection 2:\n" << save_selection << endl;