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"
64 using lyx::cap::copySelection;
65 using lyx::cap::grabAndEraseSelection;
66 using lyx::cap::cutSelection;
67 using lyx::cap::replaceSelection;
68 using lyx::cap::selClearOrDel;
72 using std::istringstream;
75 InsetMathNest::InsetMathNest(idx_type nargs)
76 : cells_(nargs), lock_(false)
80 InsetMath::idx_type InsetMathNest::nargs() const
86 MathArray & InsetMathNest::cell(idx_type i)
92 MathArray const & InsetMathNest::cell(idx_type i) const
98 void InsetMathNest::cursorPos(CursorSlice const & sl, bool /*boundary*/,
99 int & x, int & y) const
101 // FIXME: This is a hack. Ideally, the coord cache should not store
102 // absolute positions, but relative ones. This would mean to call
103 // setXY() not in MathArray::draw(), but in the parent insets' draw()
104 // with the correctly adjusted x,y values. But this means that we'd have
105 // to touch all (math)inset's draw() methods. Right now, we'll store
106 // absolute value, and make them here relative, only to make them
107 // absolute again when actually drawing the cursor. What a mess.
108 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
109 MathArray const & ar = sl.cell();
110 CoordCache & coord_cache = sl.text()->bv()->coordCache();
111 if (!coord_cache.getArrays().has(&ar)) {
112 // this can (semi-)legally happen if we just created this cell
113 // and it never has been drawn before. So don't ASSERT.
114 //lyxerr << "no cached data for array " << &ar << endl;
119 Point const pt = coord_cache.getArrays().xy(&ar);
120 if (!coord_cache.getInsets().has(this)) {
122 //lyxerr << "no cached data for inset " << this << endl;
127 Point const pt2 = coord_cache.getInsets().xy(this);
128 //lyxerr << "retrieving position cache for MathArray "
129 // << pt.x_ << ' ' << pt.y_ << std::endl;
130 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
132 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
133 // << " asc: " << ascent() << " des: " << descent()
134 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
135 // move cursor visually into empty cells ("blue rectangles");
141 void InsetMathNest::metrics(MetricsInfo const & mi) const
144 for (idx_type i = 0, n = nargs(); i != n; ++i)
149 bool InsetMathNest::idxNext(LCursor & cur) const
151 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
152 if (cur.idx() == cur.lastidx())
160 bool InsetMathNest::idxRight(LCursor & cur) const
166 bool InsetMathNest::idxPrev(LCursor & cur) const
168 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
172 cur.pos() = cur.lastpos();
177 bool InsetMathNest::idxLeft(LCursor & cur) const
183 bool InsetMathNest::idxFirst(LCursor & cur) const
185 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
194 bool InsetMathNest::idxLast(LCursor & cur) const
196 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
199 cur.idx() = cur.lastidx();
200 cur.pos() = cur.lastpos();
205 void InsetMathNest::dump() const
207 WriteStream os(lyxerr);
208 os << "---------------------------------------------\n";
211 for (idx_type i = 0, n = nargs(); i != n; ++i)
212 os << cell(i) << "\n";
213 os << "---------------------------------------------\n";
217 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
221 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
224 setPosCache(pi, x, y);
228 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
230 BufferView & bv = *pi.base.bv;
231 // this should use the x/y values given, not the cached values
232 LCursor & cur = bv.cursor();
233 if (!cur.selection())
235 if (!ptr_cmp(&cur.inset(), this))
238 // FIXME: hack to get position cache warm
239 static lyx::frontend::NullPainter nop;
240 PainterInfo pinop(pi);
244 CursorSlice s1 = cur.selBegin();
245 CursorSlice s2 = cur.selEnd();
247 //lyxerr << "InsetMathNest::drawing selection: "
248 // << " s1: " << s1 << " s2: " << s2 << endl;
249 if (s1.idx() == s2.idx()) {
250 MathArray const & c = cell(s1.idx());
251 int x1 = c.xo(bv) + c.pos2x(s1.pos());
252 int y1 = c.yo(bv) - c.ascent();
253 int x2 = c.xo(bv) + c.pos2x(s2.pos());
254 int y2 = c.yo(bv) + c.descent();
255 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
256 //lyxerr << "InsetMathNest::drawing selection 3: "
257 // << " x1: " << x1 << " x2: " << x2
258 // << " y1: " << y1 << " y2: " << y2 << endl;
260 for (idx_type i = 0; i < nargs(); ++i) {
261 if (idxBetween(i, s1.idx(), s2.idx())) {
262 MathArray const & c = cell(i);
264 int y1 = c.yo(bv) - c.ascent();
265 int x2 = c.xo(bv) + c.width();
266 int y2 = c.yo(bv) + c.descent();
267 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
274 void InsetMathNest::validate(LaTeXFeatures & features) const
276 for (idx_type i = 0; i < nargs(); ++i)
277 cell(i).validate(features);
281 void InsetMathNest::replace(ReplaceData & rep)
283 for (idx_type i = 0; i < nargs(); ++i)
284 cell(i).replace(rep);
288 bool InsetMathNest::contains(MathArray const & ar) const
290 for (idx_type i = 0; i < nargs(); ++i)
291 if (cell(i).contains(ar))
297 bool InsetMathNest::lock() const
303 void InsetMathNest::lock(bool l)
309 bool InsetMathNest::isActive() const
315 MathArray InsetMathNest::glue() const
318 for (size_t i = 0; i < nargs(); ++i)
324 void InsetMathNest::write(WriteStream & os) const
326 os << '\\' << name().c_str();
327 for (size_t i = 0; i < nargs(); ++i)
328 os << '{' << cell(i) << '}';
330 os.pendingSpace(true);
331 if (lock_ && !os.latex()) {
333 os.pendingSpace(true);
338 void InsetMathNest::normalize(NormalStream & os) const
340 os << '[' << name().c_str();
341 for (size_t i = 0; i < nargs(); ++i)
342 os << ' ' << cell(i);
347 int InsetMathNest::latex(Buffer const &, std::ostream & os,
348 OutputParams const & runparams) const
350 WriteStream wi(os, runparams.moving_arg, true);
356 bool InsetMathNest::notifyCursorLeaves(LCursor & /*cur*/)
362 MathArray & ar = cur.cell();
363 // remove base-only "scripts"
364 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
365 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
366 if (p && p->nargs() == 1) {
367 MathArray ar = p->nuc();
370 cur.adjust(i, ar.size() - 1);
374 // glue adjacent font insets of the same kind
375 for (pos_type i = 0; i + 1 < size(); ++i) {
376 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
377 InsetMathFont const * q = operator[](i + 1)->asFontInset();
378 if (p && q && p->name() == q->name()) {
379 p->cell(0).append(q->cell(0));
389 void InsetMathNest::handleFont
390 (LCursor & cur, string const & arg, string const & font)
392 // this whole function is a hack and won't work for incremental font
394 recordUndo(cur, Undo::ATOMIC);
396 if (cur.inset().asInsetMath()->name() == font)
397 cur.handleFont(font);
399 cur.handleNest(createInsetMath(font));
405 void InsetMathNest::handleFont2(LCursor & cur, string const & arg)
407 recordUndo(cur, Undo::ATOMIC);
410 bv_funcs::string2font(arg, font, b);
411 if (font.color() != LColor::inherit) {
412 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
413 cur.handleNest(at, 0);
418 void InsetMathNest::doDispatch(LCursor & cur, FuncRequest & cmd)
420 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
421 //CursorSlice sl = cur.current();
423 switch (cmd.action) {
427 cur.message(_("Paste"));
428 replaceSelection(cur);
430 istringstream is(lyx::to_utf8(cmd.argument()));
432 docstring const selection = lyx::cap::getSelection(cur.buffer(), n);
434 cur.niceInsert(lyx::to_utf8(selection));
435 cur.clearSelection(); // bug 393
436 cur.bv().switchKeyMap();
443 cutSelection(cur, true, true);
444 cur.message(_("Cut"));
445 // Prevent stale position >= size crash
446 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
453 cur.message(_("Copy"));
456 case LFUN_MOUSE_PRESS:
457 lfunMousePress(cur, cmd);
460 case LFUN_MOUSE_MOTION:
461 lfunMouseMotion(cur, cmd);
464 case LFUN_MOUSE_RELEASE:
465 lfunMouseRelease(cur, cmd);
468 case LFUN_FINISHED_LEFT:
469 cur.bv().cursor() = cur;
472 case LFUN_FINISHED_RIGHT:
474 cur.bv().cursor() = cur;
477 case LFUN_FINISHED_UP:
478 cur.bv().cursor() = cur;
481 case LFUN_FINISHED_DOWN:
483 cur.bv().cursor() = cur;
486 case LFUN_CHAR_FORWARD_SELECT:
487 case LFUN_CHAR_FORWARD:
488 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
489 cur.autocorrect() = false;
491 cur.macroModeClose();
492 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
493 cur.pushLeft(*cur.nextAtom().nucleus());
494 cur.inset().idxFirst(cur);
495 } else if (cur.posRight() || idxRight(cur)
496 || cur.popRight() || cur.selection())
499 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
504 case LFUN_CHAR_BACKWARD_SELECT:
505 case LFUN_CHAR_BACKWARD:
506 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
507 cur.autocorrect() = false;
509 cur.macroModeClose();
510 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
512 cur.push(*cur.nextAtom().nucleus());
513 cur.inset().idxLast(cur);
514 } else if (cur.posLeft() || idxLeft(cur)
515 || cur.popLeft() || cur.selection())
518 cmd = FuncRequest(LFUN_FINISHED_LEFT);
525 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
526 if (cur.inMacroMode()) {
528 cur.macroModeClose();
531 cur.selHandle(cmd.action == LFUN_UP_SELECT);
533 cmd = FuncRequest(LFUN_FINISHED_UP);
536 // fixes bug 1598. Please check!
540 case LFUN_DOWN_SELECT:
542 if (cur.inMacroMode()) {
543 cur.macroModeClose();
546 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
548 cmd = FuncRequest(LFUN_FINISHED_DOWN);
551 // fixes bug 1598. Please check!
555 case LFUN_MOUSE_DOUBLE:
556 case LFUN_MOUSE_TRIPLE:
557 case LFUN_WORD_SELECT:
561 cur.selection() = true;
562 cur.pos() = cur.lastpos();
563 cur.idx() = cur.lastidx();
566 case LFUN_PARAGRAPH_UP_SELECT:
567 case LFUN_PARAGRAPH_UP:
568 case LFUN_PARAGRAPH_DOWN_SELECT:
569 case LFUN_PARAGRAPH_DOWN:
572 case LFUN_LINE_BEGIN_SELECT:
573 case LFUN_LINE_BEGIN:
574 case LFUN_WORD_BACKWARD_SELECT:
575 case LFUN_WORD_BACKWARD:
576 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
577 cmd.action == LFUN_LINE_BEGIN_SELECT);
578 cur.macroModeClose();
579 if (cur.pos() != 0) {
581 } else if (cur.col() != 0) {
582 cur.idx() -= cur.col();
584 } else if (cur.idx() != 0) {
588 cmd = FuncRequest(LFUN_FINISHED_LEFT);
593 case LFUN_WORD_FORWARD_SELECT:
594 case LFUN_WORD_FORWARD:
595 case LFUN_LINE_END_SELECT:
597 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
598 cmd.action == LFUN_LINE_END_SELECT);
599 cur.macroModeClose();
601 if (cur.pos() != cur.lastpos()) {
602 cur.pos() = cur.lastpos();
603 } else if (ncols() && (cur.col() != cur.lastcol())) {
604 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
605 cur.pos() = cur.lastpos();
606 } else if (cur.idx() != cur.lastidx()) {
607 cur.idx() = cur.lastidx();
608 cur.pos() = cur.lastpos();
610 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
615 case LFUN_SCREEN_UP_SELECT:
617 cmd = FuncRequest(LFUN_FINISHED_LEFT);
621 case LFUN_SCREEN_DOWN_SELECT:
622 case LFUN_SCREEN_DOWN:
623 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
627 case LFUN_CELL_FORWARD:
628 cur.inset().idxNext(cur);
631 case LFUN_CELL_BACKWARD:
632 cur.inset().idxPrev(cur);
635 case LFUN_WORD_DELETE_BACKWARD:
636 case LFUN_CHAR_DELETE_BACKWARD:
638 // May affect external cell:
639 recordUndoInset(cur, Undo::ATOMIC);
641 recordUndo(cur, Undo::ATOMIC);
645 case LFUN_WORD_DELETE_FORWARD:
646 case LFUN_CHAR_DELETE_FORWARD:
647 if (cur.pos() == cur.lastpos())
648 // May affect external cell:
649 recordUndoInset(cur, Undo::ATOMIC);
651 recordUndo(cur, Undo::ATOMIC);
657 cur.clearSelection();
659 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
664 case LFUN_INSET_TOGGLE:
670 case LFUN_SELF_INSERT:
671 if (cmd.argument().size() != 1) {
673 string const arg = lyx::to_utf8(cmd.argument());
674 if (!interpret(cur, arg))
678 // Don't record undo steps if we are in macro mode and
679 // cmd.argument is the next character of the macro name.
680 // Otherwise we'll get an invalid cursor if we undo after
681 // the macro was finished and the macro is a known command,
682 // e.g. sqrt. LCursor::macroModeClose replaces in this case
683 // the InsetMathUnknown with name "frac" by an empty
684 // InsetMathFrac -> a pos value > 0 is invalid.
685 // A side effect is that an undo before the macro is finished
686 // undoes the complete macro, not only the last character.
687 if (!cur.inMacroMode())
690 // spacial handling of space. If we insert an inset
691 // via macro mode, we want to put the cursor inside it
692 // if relevant. Think typing "\frac<space>".
693 if (cmd.argument()[0] == ' '
694 && cur.inMacroMode() && cur.macroName() != "\\"
695 && cur.macroModeClose()) {
696 MathAtom const atom = cur.prevAtom();
697 if (atom->asNestInset() && atom->nargs() > 0) {
699 cur.pushLeft(*cur.nextInset());
702 // } else if (!interpret(cur, cmd.argument()[0])) {
703 // when interpret accepts UCS4 characters
704 } else if (!interpret(cur, lyx::to_utf8(cmd.argument()))) {
705 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
710 //case LFUN_SERVER_GET_XY:
711 // sprintf(dispatch_buffer, "%d %d",);
714 case LFUN_SERVER_SET_XY: {
715 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
718 istringstream is(lyx::to_utf8(cmd.argument()));
720 cur.setScreenPos(x, y);
724 // Special casing for superscript in case of LyX handling
726 case LFUN_ACCENT_CIRCUMFLEX:
727 if (cmd.argument().empty()) {
728 // do superscript if LyX handles
730 recordUndo(cur, Undo::ATOMIC);
731 script(cur, true, grabAndEraseSelection(cur));
735 case LFUN_ACCENT_UMLAUT:
736 case LFUN_ACCENT_ACUTE:
737 case LFUN_ACCENT_GRAVE:
738 case LFUN_ACCENT_BREVE:
739 case LFUN_ACCENT_DOT:
740 case LFUN_ACCENT_MACRON:
741 case LFUN_ACCENT_CARON:
742 case LFUN_ACCENT_TILDE:
743 case LFUN_ACCENT_CEDILLA:
744 case LFUN_ACCENT_CIRCLE:
745 case LFUN_ACCENT_UNDERDOT:
746 case LFUN_ACCENT_TIE:
747 case LFUN_ACCENT_OGONEK:
748 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
752 case LFUN_FONT_FREE_APPLY:
753 case LFUN_FONT_FREE_UPDATE:
754 handleFont2(cur, lyx::to_utf8(cmd.argument()));
758 if (currentMode() == TEXT_MODE)
759 handleFont(cur, lyx::to_utf8(cmd.argument()), "textbf");
761 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathbf");
764 if (currentMode() == TEXT_MODE)
765 handleFont(cur, lyx::to_utf8(cmd.argument()), "textsf");
767 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathsf");
770 if (currentMode() == TEXT_MODE)
771 handleFont(cur, lyx::to_utf8(cmd.argument()), "emph");
773 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathcal");
775 case LFUN_FONT_ROMAN:
776 if (currentMode() == TEXT_MODE)
777 handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
779 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathrm");
782 if (currentMode() == TEXT_MODE)
783 handleFont(cur, lyx::to_utf8(cmd.argument()), "texttt");
785 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathtt");
788 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathfrak");
791 if (currentMode() == TEXT_MODE)
792 handleFont(cur, lyx::to_utf8(cmd.argument()), "textit");
794 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathit");
797 if (currentMode() == TEXT_MODE)
798 // FIXME: should be "noun"
799 handleFont(cur, lyx::to_utf8(cmd.argument()), "textsc");
801 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathbb");
803 //case LFUN_FONT_FREE_APPLY:
804 handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
806 case LFUN_FONT_DEFAULT:
807 handleFont(cur, lyx::to_utf8(cmd.argument()), "textnormal");
810 case LFUN_MATH_MODE: {
812 // ignore math-mode on when already in math mode
813 if (currentMode() == InsetBase::MATH_MODE && cmd.argument() == "on")
815 cur.macroModeClose();
816 string const save_selection = grabAndEraseSelection(cur);
818 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
819 cur.plainInsert(MathAtom(new InsetMathBox("mbox")));
821 cur.pushLeft(*cur.nextInset());
822 cur.niceInsert(save_selection);
824 if (currentMode() == InsetBase::TEXT_MODE) {
825 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
826 cur.message(_("create new math text environment ($...$)"));
828 handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
829 cur.message(_("entered math text mode (textrm)"));
842 case LFUN_MATH_MATRIX: {
843 recordUndo(cur, Undo::ATOMIC);
848 istringstream is(lyx::to_utf8(cmd.argument()));
849 is >> m >> n >> v_align >> h_align;
856 MathAtom(new InsetMathArray("array", m, n, v_align[0], h_align)));
860 case LFUN_MATH_DELIM: {
862 string rs = lyx::support::split(lyx::to_utf8(cmd.argument()), ls, ' ');
863 // Reasonable default values
868 recordUndo(cur, Undo::ATOMIC);
869 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
873 case LFUN_MATH_BIGDELIM: {
874 string const lname = cmd.getArg(0);
875 string const ldelim = cmd.getArg(1);
876 string const rname = cmd.getArg(2);
877 string const rdelim = cmd.getArg(3);
878 latexkeys const * l = in_word_set(lname);
879 bool const have_l = l && l->inset == "big" &&
880 InsetMathBig::isBigInsetDelim(ldelim);
881 l = in_word_set(rname);
882 bool const have_r = l && l->inset == "big" &&
883 InsetMathBig::isBigInsetDelim(rdelim);
884 // We mimic LFUN_MATH_DELIM in case we have an empty left
885 // or right delimiter.
886 if (have_l || have_r) {
887 recordUndo(cur, Undo::ATOMIC);
888 string const selection = grabAndEraseSelection(cur);
891 cur.insert(MathAtom(new InsetMathBig(lname,
893 cur.niceInsert(selection);
895 cur.insert(MathAtom(new InsetMathBig(rname,
898 // Don't call cur.undispatched() if we did nothing, this would
899 // lead to infinite recursion via LyXText::dispatch().
903 case LFUN_SPACE_INSERT:
904 case LFUN_MATH_SPACE:
905 recordUndo(cur, Undo::ATOMIC);
906 cur.insert(MathAtom(new InsetMathSpace(",")));
909 case LFUN_ERT_INSERT:
910 // interpret this as if a backslash was typed
911 recordUndo(cur, Undo::ATOMIC);
912 interpret(cur, '\\');
915 case LFUN_MATH_SUBSCRIPT:
916 // interpret this as if a _ was typed
917 recordUndo(cur, Undo::ATOMIC);
921 case LFUN_MATH_SUPERSCRIPT:
922 // interpret this as if a ^ was typed
923 recordUndo(cur, Undo::ATOMIC);
927 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
928 // handling such that "self-insert" works on "arbitrary stuff" too, and
929 // math-insert only handles special math things like "matrix".
930 case LFUN_MATH_INSERT: {
931 recordUndo(cur, Undo::ATOMIC);
932 if (cmd.argument() == "^" || cmd.argument() == "_")
933 interpret(cur, cmd.argument()[0]);
935 cur.niceInsert(lyx::to_utf8(cmd.argument()));
939 case LFUN_DIALOG_SHOW_NEW_INSET: {
940 string const & name = lyx::to_utf8(cmd.argument());
944 data = tmp.createDialogStr(name);
946 cur.bv().showInsetDialog(name, data, 0);
950 case LFUN_INSET_INSERT: {
952 if (createInsetMath_fromDialogStr(lyx::to_utf8(cmd.argument()), ar)) {
961 InsetMathDim::doDispatch(cur, cmd);
967 bool InsetMathNest::getStatus(LCursor & cur, FuncRequest const & cmd,
968 FuncStatus & flag) const
970 // the font related toggles
971 //string tc = "mathnormal";
973 string const arg = lyx::to_utf8(cmd.argument());
974 switch (cmd.action) {
975 case LFUN_TABULAR_FEATURE:
979 case LFUN_TABULAR_FEATURE:
980 // FIXME: check temporarily disabled
982 char align = mathcursor::valign();
987 if (cmd.argument().empty()) {
991 if (!contains("tcb", cmd.argument()[0])) {
995 flag.setOnOff(cmd.argument()[0] == align);
998 /// We have to handle them since 1.4 blocks all unhandled actions
1000 case LFUN_FONT_BOLD:
1001 case LFUN_FONT_SANS:
1002 case LFUN_FONT_EMPH:
1003 case LFUN_FONT_CODE:
1004 case LFUN_FONT_NOUN:
1005 case LFUN_FONT_ROMAN:
1006 case LFUN_FONT_DEFAULT:
1009 case LFUN_MATH_MUTATE:
1010 //flag.setOnOff(mathcursor::formula()->hullType() == lyx::to_utf8(cmd.argument()));
1011 flag.setOnOff(false);
1014 // we just need to be in math mode to enable that
1015 case LFUN_MATH_SIZE:
1016 case LFUN_MATH_SPACE:
1017 case LFUN_MATH_LIMITS:
1018 case LFUN_MATH_NONUMBER:
1019 case LFUN_MATH_NUMBER:
1020 case LFUN_MATH_EXTERN:
1024 case LFUN_FONT_FRAK:
1025 flag.enabled(currentMode() != TEXT_MODE);
1028 case LFUN_MATH_INSERT: {
1029 bool const textarg =
1030 arg == "\\textbf" || arg == "\\textsf" ||
1031 arg == "\\textrm" || arg == "\\textmd" ||
1032 arg == "\\textit" || arg == "\\textsc" ||
1033 arg == "\\textsl" || arg == "\\textup" ||
1034 arg == "\\texttt" || arg == "\\textbb" ||
1035 arg == "\\textnormal";
1036 flag.enabled(currentMode() != TEXT_MODE || textarg);
1040 case LFUN_MATH_MATRIX:
1041 flag.enabled(currentMode() == MATH_MODE);
1044 case LFUN_INSET_INSERT: {
1045 // Don't test createMathInset_fromDialogStr(), since
1046 // getStatus is not called with a valid reference and the
1047 // dialog would not be applyable.
1048 string const name = cmd.getArg(0);
1049 flag.enabled(name == "ref");
1053 case LFUN_MATH_DELIM:
1054 case LFUN_MATH_BIGDELIM:
1055 // Don't do this with multi-cell selections
1056 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1067 void InsetMathNest::edit(LCursor & cur, bool left)
1070 cur.idx() = left ? 0 : cur.lastidx();
1071 cur.pos() = left ? 0 : cur.lastpos();
1073 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1077 InsetBase * InsetMathNest::editXY(LCursor & cur, int x, int y)
1080 int dist_min = 1000000;
1081 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1082 int const d = cell(i).dist(cur.bv(), x, y);
1088 MathArray & ar = cell(idx_min);
1090 cur.idx() = idx_min;
1091 cur.pos() = ar.x2pos(x - ar.xo(cur.bv()));
1093 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1094 if (dist_min == 0) {
1096 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1097 if (ar[i]->covers(cur.bv(), x, y))
1098 return ar[i].nucleus()->editXY(cur, x, y);
1104 void InsetMathNest::lfunMousePress(LCursor & cur, FuncRequest & cmd)
1106 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1107 BufferView & bv = cur.bv();
1108 if (cmd.button() == mouse_button::button1) {
1109 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1110 bv.mouseSetCursor(cur);
1111 } else if (cmd.button() == mouse_button::button2) {
1113 if (cur.selection())
1114 asArray(lyx::to_utf8(bv.cursor().selectionAsString(false)), ar);
1116 asArray(lyx::to_utf8(theSelection().get()), ar);
1119 bv.mouseSetCursor(cur);
1124 void InsetMathNest::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
1126 // only select with button 1
1127 if (cmd.button() == mouse_button::button1) {
1128 LCursor & bvcur = cur.bv().cursor();
1129 if (bvcur.anchor_.hasPart(cur)) {
1130 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1131 bvcur.setCursor(cur);
1132 bvcur.selection() = true;
1133 //lyxerr << "MOTION " << bvcur << endl;
1142 void InsetMathNest::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
1144 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1146 if (cmd.button() == mouse_button::button1) {
1147 //theSelection().put(cur.grabSelection());
1151 if (cmd.button() == mouse_button::button3) {
1152 // try to dispatch to enclosed insets first
1153 cur.bv().showDialog("mathpanel");
1161 bool InsetMathNest::interpret(LCursor & cur, char c)
1163 //lyxerr << "interpret 2: '" << c << "'" << endl;
1164 string save_selection;
1165 if (c == '^' || c == '_')
1166 save_selection = grabAndEraseSelection(cur);
1171 if (cur.inMacroMode()) {
1172 string name = cur.macroName();
1174 /// are we currently typing '#1' or '#2' or...?
1175 if (name == "\\#") {
1178 if (n >= 1 && n <= 9)
1179 cur.insert(new MathMacroArgument(n));
1184 cur.activeMacro()->setName(name + c);
1188 // handle 'special char' macros
1193 if (currentMode() == InsetMath::TEXT_MODE)
1194 cur.niceInsert(createInsetMath("textbackslash"));
1196 cur.niceInsert(createInsetMath("backslash"));
1197 } else if (c == '{') {
1199 cur.niceInsert(MathAtom(new InsetMathBrace));
1200 } else if (c == '%') {
1202 cur.niceInsert(MathAtom(new InsetMathComment));
1203 } else if (c == '#') {
1204 BOOST_ASSERT(cur.activeMacro());
1205 cur.activeMacro()->setName(name + c);
1208 cur.niceInsert(createInsetMath(string(1, c)));
1213 // One character big delimiters. The others are handled in
1214 // the other interpret() method.
1215 latexkeys const * l = in_word_set(name.substr(1));
1216 if (name[0] == '\\' && l && l->inset == "big") {
1226 delim = string(1, c);
1229 if (InsetMathBig::isBigInsetDelim(delim)) {
1230 // name + delim ared a valid InsetMathBig.
1231 // We can't use cur.macroModeClose() because
1232 // it does not handle delim.
1233 InsetMathUnknown * p = cur.activeMacro();
1236 cur.cell().erase(cur.pos());
1237 cur.plainInsert(MathAtom(
1238 new InsetMathBig(name.substr(1), delim)));
1243 // leave macro mode and try again if necessary
1244 cur.macroModeClose();
1246 cur.niceInsert(MathAtom(new InsetMathBrace));
1252 // This is annoying as one has to press <space> far too often.
1256 // leave autocorrect mode if necessary
1257 if (autocorrect() && c == ' ') {
1258 autocorrect() = false;
1263 // just clear selection on pressing the space bar
1264 if (cur.selection() && c == ' ') {
1265 cur.selection() = false;
1272 //lyxerr << "starting with macro" << endl;
1273 cur.insert(MathAtom(new InsetMathUnknown("\\", false)));
1278 if (currentMode() == InsetMath::TEXT_MODE)
1284 if (currentMode() == InsetMath::TEXT_MODE) {
1285 // insert spaces in text mode,
1286 // but suppress direct insertion of two spaces in a row
1287 // the still allows typing '<space>a<space>' and deleting the 'a', but
1288 // it is better than nothing...
1289 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
1293 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1294 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1299 // if are at the very end, leave the formula
1300 return cur.pos() != cur.lastpos();
1303 // These shouldn't work in text mode:
1304 if (currentMode() != InsetMath::TEXT_MODE) {
1306 script(cur, false, save_selection);
1310 script(cur, true, save_selection);
1314 cur.niceInsert(createInsetMath("sim"));
1319 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1320 c == '%' || c == '_' || c == '^') {
1321 cur.niceInsert(createInsetMath(string(1, c)));
1326 // try auto-correction
1327 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1330 // no special circumstances, so insert the character without any fuss
1332 cur.autocorrect() = true;
1337 bool InsetMathNest::interpret(LCursor & cur, string const & str)
1339 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1341 if (!cur.empty() && cur.pos() > 0 &&
1342 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1343 if (InsetMathBig::isBigInsetDelim(str)) {
1344 string prev = asString(cur.cell()[cur.pos() - 1]);
1345 if (prev[0] == '\\') {
1346 prev = prev.substr(1);
1347 latexkeys const * l = in_word_set(prev);
1348 if (l && l->inset == "big") {
1349 cur.cell()[cur.pos() - 1] =
1350 MathAtom(new InsetMathBig(prev, str));
1360 bool InsetMathNest::script(LCursor & cur, bool up, string const &
1363 // Hack to get \^ and \_ working
1364 //lyxerr << "handling script: up: " << up << endl;
1365 if (cur.inMacroMode() && cur.macroName() == "\\") {
1367 cur.niceInsert(createInsetMath("mathcircumflex"));
1369 interpret(cur, '_');
1373 cur.macroModeClose();
1374 if (asScriptInset() && cur.idx() == 0) {
1375 // we are in a nucleus of a script inset, move to _our_ script
1376 InsetMathScript * inset = asScriptInset();
1377 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1379 cur.idx() = inset->idxOfScript(up);
1381 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1383 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1386 cur.idx() = inset->idxOfScript(up);
1387 cur.pos() = cur.lastpos();
1389 // convert the thing to our left to a scriptinset or create a new
1390 // one if in the very first position of the array
1391 if (cur.pos() == 0) {
1392 //lyxerr << "new scriptinset" << endl;
1393 cur.insert(new InsetMathScript(up));
1395 //lyxerr << "converting prev atom " << endl;
1396 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1399 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1400 // special handling of {}-bases
1401 // is this always correct?
1402 if (inset->nuc().size() == 1
1403 && inset->nuc().back()->asBraceInset())
1404 inset->nuc() = inset->nuc().back()->asNestInset()->cell(0);
1410 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1411 cur.niceInsert(save_selection);
1413 //lyxerr << "inserting selection 2:\n" << save_selection << endl;