2 * \file InsetMathNest.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetMathNest.h"
15 #include "InsetMathArray.h"
16 #include "InsetMathBig.h"
17 #include "InsetMathBox.h"
18 #include "InsetMathBrace.h"
19 #include "InsetMathColor.h"
20 #include "InsetMathComment.h"
22 #include "InsetMathDelim.h"
23 #include "MathFactory.h"
24 #include "InsetMathHull.h"
25 #include "MathMLStream.h"
26 #include "MathMacroArgument.h"
27 //#include "InsetMathMBox.h"
28 #include "MathParser.h"
29 #include "InsetMathScript.h"
30 #include "InsetMathSpace.h"
31 #include "InsetMathSymbol.h"
32 #include "MathSupport.h"
33 #include "InsetMathUnknown.h"
34 #include "InsetMathRef.h"
36 #include "BufferView.h"
37 #include "CutAndPaste.h"
38 #include "FuncStatus.h"
40 #include "bufferview_funcs.h"
41 #include "coordcache.h"
44 #include "dispatchresult.h"
45 #include "funcrequest.h"
47 #include "outputparams.h"
50 #include "support/lstrings.h"
52 #include "frontends/Dialogs.h"
53 #include "frontends/Gui.h"
54 #include "frontends/LyXView.h"
55 #include "frontends/Painter.h"
56 #include "frontends/Selection.h"
57 #include "frontends/nullpainter.h"
61 using lyx::cap::copySelection;
62 using lyx::cap::grabAndEraseSelection;
63 using lyx::cap::cutSelection;
64 using lyx::cap::replaceSelection;
65 using lyx::cap::selClearOrDel;
67 using lyx::frontend::Gui;
68 using lyx::frontend::Clipboard;
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 if (!theCoords.getArrays().has(&ar)) {
111 // this can (semi-)legally happen if we just created this cell
112 // and it never has been drawn before. So don't ASSERT.
113 //lyxerr << "no cached data for array " << &ar << endl;
118 Point const pt = theCoords.getArrays().xy(&ar);
119 if (!theCoords.getInsets().has(this)) {
121 //lyxerr << "no cached data for inset " << this << endl;
126 Point const pt2 = theCoords.getInsets().xy(this);
127 //lyxerr << "retrieving position cache for MathArray "
128 // << pt.x_ << ' ' << pt.y_ << std::endl;
129 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
131 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
132 // << " asc: " << ascent() << " des: " << descent()
133 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
134 // move cursor visually into empty cells ("blue rectangles");
140 void InsetMathNest::metrics(MetricsInfo const & mi) const
143 for (idx_type i = 0, n = nargs(); i != n; ++i)
148 bool InsetMathNest::idxNext(LCursor & cur) const
150 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
151 if (cur.idx() == cur.lastidx())
159 bool InsetMathNest::idxRight(LCursor & cur) const
165 bool InsetMathNest::idxPrev(LCursor & cur) const
167 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
171 cur.pos() = cur.lastpos();
176 bool InsetMathNest::idxLeft(LCursor & cur) const
182 bool InsetMathNest::idxFirst(LCursor & cur) const
184 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
193 bool InsetMathNest::idxLast(LCursor & cur) const
195 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
198 cur.idx() = cur.lastidx();
199 cur.pos() = cur.lastpos();
204 void InsetMathNest::dump() const
206 WriteStream os(lyxerr);
207 os << "---------------------------------------------\n";
210 for (idx_type i = 0, n = nargs(); i != n; ++i)
211 os << cell(i) << "\n";
212 os << "---------------------------------------------\n";
216 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
220 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
223 setPosCache(pi, x, y);
227 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
229 // this should use the x/y values given, not the cached values
230 LCursor & cur = pi.base.bv->cursor();
231 if (!cur.selection())
233 if (!ptr_cmp(&cur.inset(), this))
236 // FIXME: hack to get position cache warm
237 static lyx::frontend::NullPainter nop;
238 PainterInfo pinop(pi);
242 CursorSlice s1 = cur.selBegin();
243 CursorSlice s2 = cur.selEnd();
244 //lyxerr << "InsetMathNest::drawing selection: "
245 // << " s1: " << s1 << " s2: " << s2 << endl;
246 if (s1.idx() == s2.idx()) {
247 MathArray const & c = cell(s1.idx());
248 int x1 = c.xo() + c.pos2x(s1.pos());
249 int y1 = c.yo() - c.ascent();
250 int x2 = c.xo() + c.pos2x(s2.pos());
251 int y2 = c.yo() + c.descent();
252 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
253 //lyxerr << "InsetMathNest::drawing selection 3: "
254 // << " x1: " << x1 << " x2: " << x2
255 // << " y1: " << y1 << " y2: " << y2 << endl;
257 for (idx_type i = 0; i < nargs(); ++i) {
258 if (idxBetween(i, s1.idx(), s2.idx())) {
259 MathArray const & c = cell(i);
261 int y1 = c.yo() - c.ascent();
262 int x2 = c.xo() + c.width();
263 int y2 = c.yo() + c.descent();
264 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
271 void InsetMathNest::validate(LaTeXFeatures & features) const
273 for (idx_type i = 0; i < nargs(); ++i)
274 cell(i).validate(features);
278 void InsetMathNest::replace(ReplaceData & rep)
280 for (idx_type i = 0; i < nargs(); ++i)
281 cell(i).replace(rep);
285 bool InsetMathNest::contains(MathArray const & ar) const
287 for (idx_type i = 0; i < nargs(); ++i)
288 if (cell(i).contains(ar))
294 bool InsetMathNest::lock() const
300 void InsetMathNest::lock(bool l)
306 bool InsetMathNest::isActive() const
312 MathArray InsetMathNest::glue() const
315 for (size_t i = 0; i < nargs(); ++i)
321 void InsetMathNest::write(WriteStream & os) const
323 os << '\\' << name().c_str();
324 for (size_t i = 0; i < nargs(); ++i)
325 os << '{' << cell(i) << '}';
327 os.pendingSpace(true);
328 if (lock_ && !os.latex()) {
330 os.pendingSpace(true);
335 void InsetMathNest::normalize(NormalStream & os) const
337 os << '[' << name().c_str();
338 for (size_t i = 0; i < nargs(); ++i)
339 os << ' ' << cell(i);
344 int InsetMathNest::latex(Buffer const &, std::ostream & os,
345 OutputParams const & runparams) const
347 WriteStream wi(os, runparams.moving_arg, true);
353 bool InsetMathNest::notifyCursorLeaves(LCursor & /*cur*/)
359 MathArray & ar = cur.cell();
360 // remove base-only "scripts"
361 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
362 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
363 if (p && p->nargs() == 1) {
364 MathArray ar = p->nuc();
367 cur.adjust(i, ar.size() - 1);
371 // glue adjacent font insets of the same kind
372 for (pos_type i = 0; i + 1 < size(); ++i) {
373 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
374 InsetMathFont const * q = operator[](i + 1)->asFontInset();
375 if (p && q && p->name() == q->name()) {
376 p->cell(0).append(q->cell(0));
386 void InsetMathNest::handleFont
387 (LCursor & cur, string const & arg, string const & font)
389 // this whole function is a hack and won't work for incremental font
391 recordUndo(cur, Undo::ATOMIC);
393 if (cur.inset().asInsetMath()->name() == font)
394 cur.handleFont(font);
396 cur.handleNest(createInsetMath(font));
402 void InsetMathNest::handleFont2(LCursor & cur, string const & arg)
404 recordUndo(cur, Undo::ATOMIC);
407 bv_funcs::string2font(arg, font, b);
408 if (font.color() != LColor::inherit) {
409 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
410 cur.handleNest(at, 0);
415 void InsetMathNest::doDispatch(LCursor & cur, FuncRequest & cmd)
417 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
418 //CursorSlice sl = cur.current();
420 switch (cmd.action) {
424 cur.message(_("Paste"));
425 replaceSelection(cur);
427 istringstream is(lyx::to_utf8(cmd.argument()));
429 string const selection = lyx::cap::getSelection(cur.buffer(), n);
430 cur.niceInsert(selection);
431 cur.clearSelection(); // bug 393
432 cur.bv().switchKeyMap();
439 cutSelection(cur, true, true);
440 cur.message(_("Cut"));
441 // Prevent stale position >= size crash
442 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
449 cur.message(_("Copy"));
452 case LFUN_MOUSE_PRESS:
453 lfunMousePress(cur, cmd);
456 case LFUN_MOUSE_MOTION:
457 lfunMouseMotion(cur, cmd);
460 case LFUN_MOUSE_RELEASE:
461 lfunMouseRelease(cur, cmd);
464 case LFUN_FINISHED_LEFT:
465 cur.bv().cursor() = cur;
468 case LFUN_FINISHED_RIGHT:
470 cur.bv().cursor() = cur;
473 case LFUN_FINISHED_UP:
474 cur.bv().cursor() = cur;
477 case LFUN_FINISHED_DOWN:
479 cur.bv().cursor() = cur;
482 case LFUN_CHAR_FORWARD_SELECT:
483 case LFUN_CHAR_FORWARD:
484 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
485 cur.autocorrect() = false;
487 cur.macroModeClose();
488 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
489 cur.pushLeft(*cur.nextAtom().nucleus());
490 cur.inset().idxFirst(cur);
491 } else if (cur.posRight() || idxRight(cur)
492 || cur.popRight() || cur.selection())
495 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
500 case LFUN_CHAR_BACKWARD_SELECT:
501 case LFUN_CHAR_BACKWARD:
502 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
503 cur.autocorrect() = false;
505 cur.macroModeClose();
506 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
508 cur.push(*cur.nextAtom().nucleus());
509 cur.inset().idxLast(cur);
510 } else if (cur.posLeft() || idxLeft(cur)
511 || cur.popLeft() || cur.selection())
514 cmd = FuncRequest(LFUN_FINISHED_LEFT);
521 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
522 if (cur.inMacroMode()) {
524 cur.macroModeClose();
527 cur.selHandle(cmd.action == LFUN_UP_SELECT);
529 cmd = FuncRequest(LFUN_FINISHED_UP);
532 // fixes bug 1598. Please check!
536 case LFUN_DOWN_SELECT:
538 if (cur.inMacroMode()) {
539 cur.macroModeClose();
542 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
544 cmd = FuncRequest(LFUN_FINISHED_DOWN);
547 // fixes bug 1598. Please check!
551 case LFUN_MOUSE_DOUBLE:
552 case LFUN_MOUSE_TRIPLE:
553 case LFUN_WORD_SELECT:
557 cur.selection() = true;
558 cur.pos() = cur.lastpos();
559 cur.idx() = cur.lastidx();
562 case LFUN_PARAGRAPH_UP_SELECT:
563 case LFUN_PARAGRAPH_UP:
564 case LFUN_PARAGRAPH_DOWN_SELECT:
565 case LFUN_PARAGRAPH_DOWN:
568 case LFUN_LINE_BEGIN_SELECT:
569 case LFUN_LINE_BEGIN:
570 case LFUN_WORD_BACKWARD_SELECT:
571 case LFUN_WORD_BACKWARD:
572 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
573 cmd.action == LFUN_LINE_BEGIN_SELECT);
574 cur.macroModeClose();
575 if (cur.pos() != 0) {
577 } else if (cur.col() != 0) {
578 cur.idx() -= cur.col();
580 } else if (cur.idx() != 0) {
584 cmd = FuncRequest(LFUN_FINISHED_LEFT);
589 case LFUN_WORD_FORWARD_SELECT:
590 case LFUN_WORD_FORWARD:
591 case LFUN_LINE_END_SELECT:
593 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
594 cmd.action == LFUN_LINE_END_SELECT);
595 cur.macroModeClose();
597 if (cur.pos() != cur.lastpos()) {
598 cur.pos() = cur.lastpos();
599 } else if (ncols() && (cur.col() != cur.lastcol())) {
600 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
601 cur.pos() = cur.lastpos();
602 } else if (cur.idx() != cur.lastidx()) {
603 cur.idx() = cur.lastidx();
604 cur.pos() = cur.lastpos();
606 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
611 case LFUN_SCREEN_UP_SELECT:
613 cmd = FuncRequest(LFUN_FINISHED_LEFT);
617 case LFUN_SCREEN_DOWN_SELECT:
618 case LFUN_SCREEN_DOWN:
619 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
623 case LFUN_CELL_FORWARD:
624 cur.inset().idxNext(cur);
627 case LFUN_CELL_BACKWARD:
628 cur.inset().idxPrev(cur);
631 case LFUN_WORD_DELETE_BACKWARD:
632 case LFUN_CHAR_DELETE_BACKWARD:
634 // May affect external cell:
635 recordUndoInset(cur, Undo::ATOMIC);
637 recordUndo(cur, Undo::ATOMIC);
641 case LFUN_WORD_DELETE_FORWARD:
642 case LFUN_CHAR_DELETE_FORWARD:
643 if (cur.pos() == cur.lastpos())
644 // May affect external cell:
645 recordUndoInset(cur, Undo::ATOMIC);
647 recordUndo(cur, Undo::ATOMIC);
653 cur.clearSelection();
655 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
660 case LFUN_INSET_TOGGLE:
666 case LFUN_SELF_INSERT:
667 if (cmd.argument().size() != 1) {
669 string const arg = lyx::to_utf8(cmd.argument());
670 if (!interpret(cur, arg))
674 // Don't record undo steps if we are in macro mode and
675 // cmd.argument is the next character of the macro name.
676 // Otherwise we'll get an invalid cursor if we undo after
677 // the macro was finished and the macro is a known command,
678 // e.g. sqrt. LCursor::macroModeClose replaces in this case
679 // the InsetMathUnknown with name "frac" by an empty
680 // InsetMathFrac -> a pos value > 0 is invalid.
681 // A side effect is that an undo before the macro is finished
682 // undoes the complete macro, not only the last character.
683 if (!cur.inMacroMode())
686 // spacial handling of space. If we insert an inset
687 // via macro mode, we want to put the cursor inside it
688 // if relevant. Think typing "\frac<space>".
689 if (cmd.argument()[0] == ' '
690 && cur.inMacroMode() && cur.macroName() != "\\"
691 && cur.macroModeClose()) {
692 MathAtom const atom = cur.prevAtom();
693 if (atom->asNestInset() && atom->nargs() > 0) {
695 cur.pushLeft(*cur.nextInset());
698 // } else if (!interpret(cur, cmd.argument()[0])) {
699 // when interpret accepts UCS4 characters
700 } else if (!interpret(cur, lyx::to_utf8(cmd.argument()))) {
701 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
706 //case LFUN_SERVER_GET_XY:
707 // sprintf(dispatch_buffer, "%d %d",);
710 case LFUN_SERVER_SET_XY: {
711 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
714 istringstream is(lyx::to_utf8(cmd.argument()));
716 cur.setScreenPos(x, y);
720 // Special casing for superscript in case of LyX handling
722 case LFUN_ACCENT_CIRCUMFLEX:
723 if (cmd.argument().empty()) {
724 // do superscript if LyX handles
726 recordUndo(cur, Undo::ATOMIC);
727 script(cur, true, grabAndEraseSelection(cur));
731 case LFUN_ACCENT_UMLAUT:
732 case LFUN_ACCENT_ACUTE:
733 case LFUN_ACCENT_GRAVE:
734 case LFUN_ACCENT_BREVE:
735 case LFUN_ACCENT_DOT:
736 case LFUN_ACCENT_MACRON:
737 case LFUN_ACCENT_CARON:
738 case LFUN_ACCENT_TILDE:
739 case LFUN_ACCENT_CEDILLA:
740 case LFUN_ACCENT_CIRCLE:
741 case LFUN_ACCENT_UNDERDOT:
742 case LFUN_ACCENT_TIE:
743 case LFUN_ACCENT_OGONEK:
744 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
748 case LFUN_FONT_FREE_APPLY:
749 case LFUN_FONT_FREE_UPDATE:
750 handleFont2(cur, lyx::to_utf8(cmd.argument()));
754 if (currentMode() == TEXT_MODE)
755 handleFont(cur, lyx::to_utf8(cmd.argument()), "textbf");
757 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathbf");
760 if (currentMode() == TEXT_MODE)
761 handleFont(cur, lyx::to_utf8(cmd.argument()), "textsf");
763 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathsf");
766 if (currentMode() == TEXT_MODE)
767 handleFont(cur, lyx::to_utf8(cmd.argument()), "emph");
769 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathcal");
771 case LFUN_FONT_ROMAN:
772 if (currentMode() == TEXT_MODE)
773 handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
775 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathrm");
778 if (currentMode() == TEXT_MODE)
779 handleFont(cur, lyx::to_utf8(cmd.argument()), "texttt");
781 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathtt");
784 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathfrak");
787 if (currentMode() == TEXT_MODE)
788 handleFont(cur, lyx::to_utf8(cmd.argument()), "textit");
790 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathit");
793 if (currentMode() == TEXT_MODE)
794 // FIXME: should be "noun"
795 handleFont(cur, lyx::to_utf8(cmd.argument()), "textsc");
797 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathbb");
799 //case LFUN_FONT_FREE_APPLY:
800 handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
802 case LFUN_FONT_DEFAULT:
803 handleFont(cur, lyx::to_utf8(cmd.argument()), "textnormal");
806 case LFUN_MATH_MODE: {
808 // ignore math-mode on when already in math mode
809 if (currentMode() == InsetBase::MATH_MODE && cmd.argument() == "on")
811 cur.macroModeClose();
812 string const save_selection = grabAndEraseSelection(cur);
814 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
815 cur.plainInsert(MathAtom(new InsetMathBox("mbox")));
817 cur.pushLeft(*cur.nextInset());
818 cur.niceInsert(save_selection);
820 if (currentMode() == InsetBase::TEXT_MODE) {
821 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
822 cur.message(_("create new math text environment ($...$)"));
824 handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
825 cur.message(_("entered math text mode (textrm)"));
838 case LFUN_MATH_MATRIX: {
839 recordUndo(cur, Undo::ATOMIC);
844 istringstream is(lyx::to_utf8(cmd.argument()));
845 is >> m >> n >> v_align >> h_align;
852 MathAtom(new InsetMathArray("array", m, n, v_align[0], h_align)));
856 case LFUN_MATH_DELIM: {
858 string rs = lyx::support::split(lyx::to_utf8(cmd.argument()), ls, ' ');
859 // Reasonable default values
864 recordUndo(cur, Undo::ATOMIC);
865 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
869 case LFUN_MATH_BIGDELIM: {
870 string const lname = cmd.getArg(0);
871 string const ldelim = cmd.getArg(1);
872 string const rname = cmd.getArg(2);
873 string const rdelim = cmd.getArg(3);
874 latexkeys const * l = in_word_set(lname);
875 bool const have_l = l && l->inset == "big" &&
876 InsetMathBig::isBigInsetDelim(ldelim);
877 l = in_word_set(rname);
878 bool const have_r = l && l->inset == "big" &&
879 InsetMathBig::isBigInsetDelim(rdelim);
880 // We mimic LFUN_MATH_DELIM in case we have an empty left
881 // or right delimiter.
882 if (have_l || have_r) {
883 recordUndo(cur, Undo::ATOMIC);
884 string const selection = grabAndEraseSelection(cur);
887 cur.insert(MathAtom(new InsetMathBig(lname,
889 cur.niceInsert(selection);
891 cur.insert(MathAtom(new InsetMathBig(rname,
894 // Don't call cur.undispatched() if we did nothing, this would
895 // lead to infinite recursion via LyXText::dispatch().
899 case LFUN_SPACE_INSERT:
900 case LFUN_MATH_SPACE:
901 recordUndo(cur, Undo::ATOMIC);
902 cur.insert(MathAtom(new InsetMathSpace(",")));
905 case LFUN_ERT_INSERT:
906 // interpret this as if a backslash was typed
907 recordUndo(cur, Undo::ATOMIC);
908 interpret(cur, '\\');
911 case LFUN_MATH_SUBSCRIPT:
912 // interpret this as if a _ was typed
913 recordUndo(cur, Undo::ATOMIC);
917 case LFUN_MATH_SUPERSCRIPT:
918 // interpret this as if a ^ was typed
919 recordUndo(cur, Undo::ATOMIC);
923 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
924 // handling such that "self-insert" works on "arbitrary stuff" too, and
925 // math-insert only handles special math things like "matrix".
926 case LFUN_MATH_INSERT: {
927 recordUndo(cur, Undo::ATOMIC);
928 if (cmd.argument() == "^" || cmd.argument() == "_")
929 interpret(cur, cmd.argument()[0]);
931 cur.niceInsert(lyx::to_utf8(cmd.argument()));
935 case LFUN_DIALOG_SHOW_NEW_INSET: {
936 string const & name = lyx::to_utf8(cmd.argument());
940 data = tmp.createDialogStr(name);
942 cur.bv().owner()->getDialogs().show(name, data, 0);
947 InsetMathDim::doDispatch(cur, cmd);
953 bool InsetMathNest::getStatus(LCursor & cur, FuncRequest const & cmd,
954 FuncStatus & flag) const
956 // the font related toggles
957 //string tc = "mathnormal";
959 string const arg = lyx::to_utf8(cmd.argument());
960 switch (cmd.action) {
961 case LFUN_TABULAR_FEATURE:
965 case LFUN_TABULAR_FEATURE:
966 // FIXME: check temporarily disabled
968 char align = mathcursor::valign();
973 if (cmd.argument().empty()) {
977 if (!contains("tcb", cmd.argument()[0])) {
981 flag.setOnOff(cmd.argument()[0] == align);
984 /// We have to handle them since 1.4 blocks all unhandled actions
991 case LFUN_FONT_ROMAN:
992 case LFUN_FONT_DEFAULT:
995 case LFUN_MATH_MUTATE:
996 //flag.setOnOff(mathcursor::formula()->hullType() == lyx::to_utf8(cmd.argument()));
997 flag.setOnOff(false);
1000 // we just need to be in math mode to enable that
1001 case LFUN_MATH_SIZE:
1002 case LFUN_MATH_SPACE:
1003 case LFUN_MATH_LIMITS:
1004 case LFUN_MATH_NONUMBER:
1005 case LFUN_MATH_NUMBER:
1006 case LFUN_MATH_EXTERN:
1010 case LFUN_FONT_FRAK:
1011 flag.enabled(currentMode() != TEXT_MODE);
1014 case LFUN_MATH_INSERT: {
1015 bool const textarg =
1016 arg == "\\textbf" || arg == "\\textsf" ||
1017 arg == "\\textrm" || arg == "\\textmd" ||
1018 arg == "\\textit" || arg == "\\textsc" ||
1019 arg == "\\textsl" || arg == "\\textup" ||
1020 arg == "\\texttt" || arg == "\\textbb" ||
1021 arg == "\\textnormal";
1022 flag.enabled(currentMode() != TEXT_MODE || textarg);
1026 case LFUN_MATH_MATRIX:
1027 flag.enabled(currentMode() == MATH_MODE);
1030 case LFUN_MATH_DELIM:
1031 case LFUN_MATH_BIGDELIM:
1032 // Don't do this with multi-cell selections
1033 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1044 void InsetMathNest::edit(LCursor & cur, bool left)
1047 cur.idx() = left ? 0 : cur.lastidx();
1048 cur.pos() = left ? 0 : cur.lastpos();
1050 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1054 InsetBase * InsetMathNest::editXY(LCursor & cur, int x, int y)
1057 int dist_min = 1000000;
1058 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1059 int const d = cell(i).dist(x, y);
1065 MathArray & ar = cell(idx_min);
1067 cur.idx() = idx_min;
1068 cur.pos() = ar.x2pos(x - ar.xo());
1069 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1070 if (dist_min == 0) {
1072 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1073 if (ar[i]->covers(x, y))
1074 return ar[i].nucleus()->editXY(cur, x, y);
1080 void InsetMathNest::lfunMousePress(LCursor & cur, FuncRequest & cmd)
1082 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1083 BufferView & bv = cur.bv();
1084 if (cmd.button() == mouse_button::button1) {
1085 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1086 bv.mouseSetCursor(cur);
1087 } else if (cmd.button() == mouse_button::button2) {
1089 if (cur.selection())
1090 asArray(lyx::to_utf8(bv.cursor().selectionAsString(false)), ar);
1092 asArray(lyx::to_utf8(bv.owner()->gui().selection().get()), ar);
1095 bv.mouseSetCursor(cur);
1100 void InsetMathNest::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
1102 // only select with button 1
1103 if (cmd.button() == mouse_button::button1) {
1104 LCursor & bvcur = cur.bv().cursor();
1105 if (bvcur.anchor_.hasPart(cur)) {
1106 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1107 bvcur.setCursor(cur);
1108 bvcur.selection() = true;
1109 //lyxerr << "MOTION " << bvcur << endl;
1118 void InsetMathNest::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
1120 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1122 if (cmd.button() == mouse_button::button1) {
1123 //cur.bv().owner()->gui().selection().put(cur.grabSelection());
1127 if (cmd.button() == mouse_button::button3) {
1128 // try to dispatch to enclosed insets first
1129 cur.bv().owner()->getDialogs().show("mathpanel");
1137 bool InsetMathNest::interpret(LCursor & cur, char c)
1139 //lyxerr << "interpret 2: '" << c << "'" << endl;
1140 string save_selection;
1141 if (c == '^' || c == '_')
1142 save_selection = grabAndEraseSelection(cur);
1147 if (cur.inMacroMode()) {
1148 string name = cur.macroName();
1150 /// are we currently typing '#1' or '#2' or...?
1151 if (name == "\\#") {
1154 if (n >= 1 && n <= 9)
1155 cur.insert(new MathMacroArgument(n));
1160 cur.activeMacro()->setName(name + c);
1164 // handle 'special char' macros
1169 if (currentMode() == InsetMath::TEXT_MODE)
1170 cur.niceInsert(createInsetMath("textbackslash"));
1172 cur.niceInsert(createInsetMath("backslash"));
1173 } else if (c == '{') {
1175 cur.niceInsert(MathAtom(new InsetMathBrace));
1176 } else if (c == '%') {
1178 cur.niceInsert(MathAtom(new InsetMathComment));
1179 } else if (c == '#') {
1180 BOOST_ASSERT(cur.activeMacro());
1181 cur.activeMacro()->setName(name + c);
1184 cur.niceInsert(createInsetMath(string(1, c)));
1189 // One character big delimiters. The others are handled in
1190 // the other interpret() method.
1191 latexkeys const * l = in_word_set(name.substr(1));
1192 if (name[0] == '\\' && l && l->inset == "big") {
1202 delim = string(1, c);
1205 if (InsetMathBig::isBigInsetDelim(delim)) {
1206 // name + delim ared a valid InsetMathBig.
1207 // We can't use cur.macroModeClose() because
1208 // it does not handle delim.
1209 InsetMathUnknown * p = cur.activeMacro();
1212 cur.cell().erase(cur.pos());
1213 cur.plainInsert(MathAtom(
1214 new InsetMathBig(name.substr(1), delim)));
1219 // leave macro mode and try again if necessary
1220 cur.macroModeClose();
1222 cur.niceInsert(MathAtom(new InsetMathBrace));
1228 // This is annoying as one has to press <space> far too often.
1232 // leave autocorrect mode if necessary
1233 if (autocorrect() && c == ' ') {
1234 autocorrect() = false;
1239 // just clear selection on pressing the space bar
1240 if (cur.selection() && c == ' ') {
1241 cur.selection() = false;
1248 //lyxerr << "starting with macro" << endl;
1249 cur.insert(MathAtom(new InsetMathUnknown("\\", false)));
1254 if (currentMode() == InsetMath::TEXT_MODE)
1260 if (currentMode() == InsetMath::TEXT_MODE) {
1261 // insert spaces in text mode,
1262 // but suppress direct insertion of two spaces in a row
1263 // the still allows typing '<space>a<space>' and deleting the 'a', but
1264 // it is better than nothing...
1265 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
1269 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1270 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1275 // if are at the very end, leave the formula
1276 return cur.pos() != cur.lastpos();
1279 // These shouldn't work in text mode:
1280 if (currentMode() != InsetMath::TEXT_MODE) {
1282 script(cur, false, save_selection);
1286 script(cur, true, save_selection);
1290 cur.niceInsert(createInsetMath("sim"));
1295 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1296 c == '%' || c == '_' || c == '^') {
1297 cur.niceInsert(createInsetMath(string(1, c)));
1302 // try auto-correction
1303 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1306 // no special circumstances, so insert the character without any fuss
1308 cur.autocorrect() = true;
1313 bool InsetMathNest::interpret(LCursor & cur, string const & str)
1315 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1317 if (!cur.empty() && cur.pos() > 0 &&
1318 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1319 if (InsetMathBig::isBigInsetDelim(str)) {
1320 string prev = asString(cur.cell()[cur.pos() - 1]);
1321 if (prev[0] == '\\') {
1322 prev = prev.substr(1);
1323 latexkeys const * l = in_word_set(prev);
1324 if (l && l->inset == "big") {
1325 cur.cell()[cur.pos() - 1] =
1326 MathAtom(new InsetMathBig(prev, str));
1336 bool InsetMathNest::script(LCursor & cur, bool up, string const &
1339 // Hack to get \^ and \_ working
1340 //lyxerr << "handling script: up: " << up << endl;
1341 if (cur.inMacroMode() && cur.macroName() == "\\") {
1343 cur.niceInsert(createInsetMath("mathcircumflex"));
1345 interpret(cur, '_');
1349 cur.macroModeClose();
1350 if (asScriptInset() && cur.idx() == 0) {
1351 // we are in a nucleus of a script inset, move to _our_ script
1352 InsetMathScript * inset = asScriptInset();
1353 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1355 cur.idx() = inset->idxOfScript(up);
1357 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1359 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1362 cur.idx() = inset->idxOfScript(up);
1363 cur.pos() = cur.lastpos();
1365 // convert the thing to our left to a scriptinset or create a new
1366 // one if in the very first position of the array
1367 if (cur.pos() == 0) {
1368 //lyxerr << "new scriptinset" << endl;
1369 cur.insert(new InsetMathScript(up));
1371 //lyxerr << "converting prev atom " << endl;
1372 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1375 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1376 // special handling of {}-bases
1377 // is this always correct?
1378 if (inset->nuc().size() == 1
1379 && inset->nuc().back()->asBraceInset())
1380 inset->nuc() = inset->nuc().back()->asNestInset()->cell(0);
1386 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1387 cur.niceInsert(save_selection);
1389 //lyxerr << "inserting selection 2:\n" << save_selection << endl;