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"
36 #include "bufferview_funcs.h"
37 #include "BufferView.h"
39 #include "CoordCache.h"
41 #include "CutAndPaste.h"
43 #include "DispatchResult.h"
44 #include "FuncRequest.h"
45 #include "FuncStatus.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"
65 using cap::copySelection;
66 using cap::grabAndEraseSelection;
67 using cap::cutSelection;
68 using cap::replaceSelection;
69 using cap::selClearOrDel;
73 using std::istringstream;
76 InsetMathNest::InsetMathNest(idx_type nargs)
77 : cells_(nargs), lock_(false)
81 InsetMath::idx_type InsetMathNest::nargs() const
87 void InsetMathNest::cursorPos(BufferView const & bv,
88 CursorSlice const & sl, bool /*boundary*/,
89 int & x, int & y) const
91 // FIXME: This is a hack. Ideally, the coord cache should not store
92 // absolute positions, but relative ones. This would mean to call
93 // setXY() not in MathData::draw(), but in the parent insets' draw()
94 // with the correctly adjusted x,y values. But this means that we'd have
95 // to touch all (math)inset's draw() methods. Right now, we'll store
96 // absolute value, and make them here relative, only to make them
97 // absolute again when actually drawing the cursor. What a mess.
98 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
99 MathData const & ar = sl.cell();
100 CoordCache const & coord_cache = bv.coordCache();
101 if (!coord_cache.getArrays().has(&ar)) {
102 // this can (semi-)legally happen if we just created this cell
103 // and it never has been drawn before. So don't ASSERT.
104 //lyxerr << "no cached data for array " << &ar << endl;
109 Point const pt = coord_cache.getArrays().xy(&ar);
110 if (!coord_cache.getInsets().has(this)) {
112 //lyxerr << "no cached data for inset " << this << endl;
117 Point const pt2 = coord_cache.getInsets().xy(this);
118 //lyxerr << "retrieving position cache for MathData "
119 // << pt.x_ << ' ' << pt.y_ << std::endl;
120 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
122 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
123 // << " asc: " << ascent() << " des: " << descent()
124 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
125 // move cursor visually into empty cells ("blue rectangles");
131 void InsetMathNest::metrics(MetricsInfo const & mi) const
134 for (idx_type i = 0, n = nargs(); i != n; ++i)
139 bool InsetMathNest::idxNext(Cursor & cur) const
141 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
142 if (cur.idx() == cur.lastidx())
150 bool InsetMathNest::idxRight(Cursor & cur) const
156 bool InsetMathNest::idxPrev(Cursor & cur) const
158 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
162 cur.pos() = cur.lastpos();
167 bool InsetMathNest::idxLeft(Cursor & cur) const
173 bool InsetMathNest::idxFirst(Cursor & cur) const
175 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
184 bool InsetMathNest::idxLast(Cursor & cur) const
186 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
189 cur.idx() = cur.lastidx();
190 cur.pos() = cur.lastpos();
195 void InsetMathNest::dump() const
197 odocstringstream oss;
199 os << "---------------------------------------------\n";
202 for (idx_type i = 0, n = nargs(); i != n; ++i)
203 os << cell(i) << "\n";
204 os << "---------------------------------------------\n";
205 lyxerr << to_utf8(oss.str());
209 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
213 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
216 setPosCache(pi, x, y);
220 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
222 BufferView & bv = *pi.base.bv;
223 // this should use the x/y values given, not the cached values
224 Cursor & cur = bv.cursor();
225 if (!cur.selection())
227 if (!ptr_cmp(&cur.inset(), this))
230 // FIXME: hack to get position cache warm
231 pi.pain.setDrawingEnabled(false);
233 pi.pain.setDrawingEnabled(true);
235 CursorSlice s1 = cur.selBegin();
236 CursorSlice s2 = cur.selEnd();
238 //lyxerr << "InsetMathNest::drawing selection: "
239 // << " s1: " << s1 << " s2: " << s2 << endl;
240 if (s1.idx() == s2.idx()) {
241 MathData const & c = cell(s1.idx());
242 int x1 = c.xo(bv) + c.pos2x(s1.pos());
243 int y1 = c.yo(bv) - c.ascent();
244 int x2 = c.xo(bv) + c.pos2x(s2.pos());
245 int y2 = c.yo(bv) + c.descent();
246 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
247 //lyxerr << "InsetMathNest::drawing selection 3: "
248 // << " x1: " << x1 << " x2: " << x2
249 // << " y1: " << y1 << " y2: " << y2 << endl;
251 for (idx_type i = 0; i < nargs(); ++i) {
252 if (idxBetween(i, s1.idx(), s2.idx())) {
253 MathData const & c = cell(i);
255 int y1 = c.yo(bv) - c.ascent();
256 int x2 = c.xo(bv) + c.width();
257 int y2 = c.yo(bv) + c.descent();
258 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
265 void InsetMathNest::validate(LaTeXFeatures & features) const
267 for (idx_type i = 0; i < nargs(); ++i)
268 cell(i).validate(features);
272 void InsetMathNest::replace(ReplaceData & rep)
274 for (idx_type i = 0; i < nargs(); ++i)
275 cell(i).replace(rep);
279 bool InsetMathNest::contains(MathData const & ar) const
281 for (idx_type i = 0; i < nargs(); ++i)
282 if (cell(i).contains(ar))
288 bool InsetMathNest::lock() const
294 void InsetMathNest::lock(bool l)
300 bool InsetMathNest::isActive() const
306 MathData InsetMathNest::glue() const
309 for (size_t i = 0; i < nargs(); ++i)
315 void InsetMathNest::write(WriteStream & os) const
317 os << '\\' << name().c_str();
318 for (size_t i = 0; i < nargs(); ++i)
319 os << '{' << cell(i) << '}';
321 os.pendingSpace(true);
322 if (lock_ && !os.latex()) {
324 os.pendingSpace(true);
329 void InsetMathNest::normalize(NormalStream & os) const
331 os << '[' << name().c_str();
332 for (size_t i = 0; i < nargs(); ++i)
333 os << ' ' << cell(i);
338 int InsetMathNest::latex(Buffer const &, odocstream & os,
339 OutputParams const & runparams) const
341 WriteStream wi(os, runparams.moving_arg, true);
347 bool InsetMathNest::notifyCursorLeaves(Cursor & /*cur*/)
353 MathData & ar = cur.cell();
354 // remove base-only "scripts"
355 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
356 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
357 if (p && p->nargs() == 1) {
358 MathData ar = p->nuc();
361 cur.adjust(i, ar.size() - 1);
365 // glue adjacent font insets of the same kind
366 for (pos_type i = 0; i + 1 < size(); ++i) {
367 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
368 InsetMathFont const * q = operator[](i + 1)->asFontInset();
369 if (p && q && p->name() == q->name()) {
370 p->cell(0).append(q->cell(0));
380 void InsetMathNest::handleFont
381 (Cursor & cur, docstring const & arg, char const * const font)
383 handleFont(cur, arg, from_ascii(font));
387 void InsetMathNest::handleFont
388 (Cursor & cur, docstring const & arg, docstring const & font)
390 // this whole function is a hack and won't work for incremental font
393 if (cur.inset().asInsetMath()->name() == font) {
394 recordUndoInset(cur, Undo::ATOMIC);
395 cur.handleFont(to_utf8(font));
397 recordUndo(cur, Undo::ATOMIC);
398 cur.handleNest(createInsetMath(font));
404 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
406 recordUndo(cur, Undo::ATOMIC);
409 bv_funcs::string2font(to_utf8(arg), font, b);
410 if (font.color() != Color::inherit) {
411 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
412 cur.handleNest(at, 0);
417 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
419 //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
420 //CursorSlice sl = cur.current();
422 switch (cmd.action) {
426 cur.message(_("Paste"));
427 replaceSelection(cur);
429 if (cmd.argument().empty() && !theClipboard().isInternal())
430 topaste = theClipboard().getAsText();
433 idocstringstream is(cmd.argument());
435 topaste = cap::getSelection(cur.buffer(), n);
437 cur.niceInsert(topaste);
438 cur.clearSelection(); // bug 393
439 cur.bv().switchKeyMap();
446 cutSelection(cur, true, true);
447 cur.message(_("Cut"));
448 // Prevent stale position >= size crash
449 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
455 cur.message(_("Copy"));
458 case LFUN_MOUSE_PRESS:
459 lfunMousePress(cur, cmd);
462 case LFUN_MOUSE_MOTION:
463 lfunMouseMotion(cur, cmd);
466 case LFUN_MOUSE_RELEASE:
467 lfunMouseRelease(cur, cmd);
470 case LFUN_FINISHED_LEFT:
471 cur.bv().cursor() = cur;
474 case LFUN_FINISHED_RIGHT:
476 cur.bv().cursor() = cur;
479 case LFUN_CHAR_FORWARD:
480 cur.updateFlags(Update::Decoration | Update::FitCursor);
481 case LFUN_CHAR_FORWARD_SELECT:
482 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
483 cur.autocorrect() = false;
485 cur.macroModeClose();
486 if (reverseDirectionNeeded(cur))
487 goto goto_char_backwards;
490 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
491 cur.pushLeft(*cur.nextAtom().nucleus());
492 cur.inset().idxFirst(cur);
493 } else if (cur.posRight() || idxRight(cur)
494 || cur.popRight() || cur.selection())
497 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
502 case LFUN_CHAR_BACKWARD:
503 cur.updateFlags(Update::Decoration | Update::FitCursor);
504 case LFUN_CHAR_BACKWARD_SELECT:
505 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
506 cur.autocorrect() = false;
508 cur.macroModeClose();
509 if (reverseDirectionNeeded(cur))
510 goto goto_char_forwards;
513 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
515 cur.push(*cur.nextAtom().nucleus());
516 cur.inset().idxLast(cur);
517 } else if (cur.posLeft() || idxLeft(cur)
518 || cur.popLeft() || cur.selection())
521 cmd = FuncRequest(LFUN_FINISHED_LEFT);
527 cur.updateFlags(Update::Decoration | Update::FitCursor);
529 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
530 if (cur.inMacroMode()) {
532 cur.macroModeClose();
535 cur.selHandle(cmd.action == LFUN_UP_SELECT);
536 if (!cur.upDownInMath(true))
538 // fixes bug 1598. Please check!
543 cur.updateFlags(Update::Decoration | Update::FitCursor);
544 case LFUN_DOWN_SELECT:
545 if (cur.inMacroMode()) {
546 cur.macroModeClose();
549 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
550 if (!cur.upDownInMath(false))
552 // fixes bug 1598. Please check!
556 case LFUN_MOUSE_DOUBLE:
557 case LFUN_MOUSE_TRIPLE:
558 case LFUN_WORD_SELECT:
562 cur.selection() = true;
563 cur.pos() = cur.lastpos();
564 cur.idx() = cur.lastidx();
565 cap::saveSelection(cur);
568 case LFUN_PARAGRAPH_UP:
569 case LFUN_PARAGRAPH_DOWN:
570 cur.updateFlags(Update::Decoration | Update::FitCursor);
571 case LFUN_PARAGRAPH_UP_SELECT:
572 case LFUN_PARAGRAPH_DOWN_SELECT:
575 case LFUN_LINE_BEGIN:
576 case LFUN_WORD_BACKWARD:
577 cur.updateFlags(Update::Decoration | Update::FitCursor);
578 case LFUN_LINE_BEGIN_SELECT:
579 case LFUN_WORD_BACKWARD_SELECT:
580 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
581 cmd.action == LFUN_LINE_BEGIN_SELECT);
582 cur.macroModeClose();
583 if (cur.pos() != 0) {
585 } else if (cur.col() != 0) {
586 cur.idx() -= cur.col();
588 } else if (cur.idx() != 0) {
592 cmd = FuncRequest(LFUN_FINISHED_LEFT);
597 case LFUN_WORD_FORWARD:
599 cur.updateFlags(Update::Decoration | Update::FitCursor);
600 case LFUN_WORD_FORWARD_SELECT:
601 case LFUN_LINE_END_SELECT:
602 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
603 cmd.action == LFUN_LINE_END_SELECT);
604 cur.macroModeClose();
606 if (cur.pos() != cur.lastpos()) {
607 cur.pos() = cur.lastpos();
608 } else if (ncols() && (cur.col() != cur.lastcol())) {
609 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
610 cur.pos() = cur.lastpos();
611 } else if (cur.idx() != cur.lastidx()) {
612 cur.idx() = cur.lastidx();
613 cur.pos() = cur.lastpos();
615 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
620 case LFUN_SCREEN_UP_SELECT:
622 cmd = FuncRequest(LFUN_FINISHED_LEFT);
626 case LFUN_SCREEN_DOWN_SELECT:
627 case LFUN_SCREEN_DOWN:
628 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
632 case LFUN_CELL_FORWARD:
633 cur.updateFlags(Update::Decoration | Update::FitCursor);
634 cur.inset().idxNext(cur);
637 case LFUN_CELL_BACKWARD:
638 cur.updateFlags(Update::Decoration | Update::FitCursor);
639 cur.inset().idxPrev(cur);
642 case LFUN_WORD_DELETE_BACKWARD:
643 case LFUN_CHAR_DELETE_BACKWARD:
645 // May affect external cell:
646 recordUndoInset(cur, Undo::ATOMIC);
648 recordUndo(cur, Undo::ATOMIC);
649 // if the inset can not be removed from within, delete it
650 if (!cur.backspace()) {
651 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
652 cur.innerText()->dispatch(cur, cmd);
656 case LFUN_WORD_DELETE_FORWARD:
657 case LFUN_CHAR_DELETE_FORWARD:
658 if (cur.pos() == cur.lastpos())
659 // May affect external cell:
660 recordUndoInset(cur, Undo::ATOMIC);
662 recordUndo(cur, Undo::ATOMIC);
663 // if the inset can not be removed from within, delete it
665 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
666 cur.innerText()->dispatch(cur, cmd);
672 cur.clearSelection();
674 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
679 case LFUN_INSET_TOGGLE:
685 case LFUN_SELF_INSERT:
686 if (cmd.argument().size() != 1) {
688 docstring const arg = cmd.argument();
689 if (!interpretString(cur, arg))
693 // Don't record undo steps if we are in macro mode and
694 // cmd.argument is the next character of the macro name.
695 // Otherwise we'll get an invalid cursor if we undo after
696 // the macro was finished and the macro is a known command,
697 // e.g. sqrt. Cursor::macroModeClose replaces in this case
698 // the InsetMathUnknown with name "frac" by an empty
699 // InsetMathFrac -> a pos value > 0 is invalid.
700 // A side effect is that an undo before the macro is finished
701 // undoes the complete macro, not only the last character.
702 if (!cur.inMacroMode())
705 // spacial handling of space. If we insert an inset
706 // via macro mode, we want to put the cursor inside it
707 // if relevant. Think typing "\frac<space>".
708 if (cmd.argument()[0] == ' '
709 && cur.inMacroMode() && cur.macroName() != "\\"
710 && cur.macroModeClose()) {
711 MathAtom const atom = cur.prevAtom();
712 if (atom->asNestInset() && atom->nargs() > 0) {
714 cur.pushLeft(*cur.nextInset());
716 } else if (!interpretChar(cur, cmd.argument()[0])) {
717 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
722 //case LFUN_SERVER_GET_XY:
723 // sprintf(dispatch_buffer, "%d %d",);
726 case LFUN_SERVER_SET_XY: {
727 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
730 istringstream is(to_utf8(cmd.argument()));
732 cur.setScreenPos(x, y);
736 // Special casing for superscript in case of LyX handling
738 case LFUN_ACCENT_CIRCUMFLEX:
739 if (cmd.argument().empty()) {
740 // do superscript if LyX handles
742 recordUndo(cur, Undo::ATOMIC);
743 script(cur, true, grabAndEraseSelection(cur));
747 case LFUN_ACCENT_UMLAUT:
748 case LFUN_ACCENT_ACUTE:
749 case LFUN_ACCENT_GRAVE:
750 case LFUN_ACCENT_BREVE:
751 case LFUN_ACCENT_DOT:
752 case LFUN_ACCENT_MACRON:
753 case LFUN_ACCENT_CARON:
754 case LFUN_ACCENT_TILDE:
755 case LFUN_ACCENT_CEDILLA:
756 case LFUN_ACCENT_CIRCLE:
757 case LFUN_ACCENT_UNDERDOT:
758 case LFUN_ACCENT_TIE:
759 case LFUN_ACCENT_OGONEK:
760 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
764 case LFUN_FONT_FREE_APPLY:
765 case LFUN_FONT_FREE_UPDATE:
766 handleFont2(cur, cmd.argument());
770 if (currentMode() == TEXT_MODE)
771 handleFont(cur, cmd.argument(), "textbf");
773 handleFont(cur, cmd.argument(), "mathbf");
776 if (currentMode() == TEXT_MODE)
777 handleFont(cur, cmd.argument(), "textsf");
779 handleFont(cur, cmd.argument(), "mathsf");
782 if (currentMode() == TEXT_MODE)
783 handleFont(cur, cmd.argument(), "emph");
785 handleFont(cur, cmd.argument(), "mathcal");
787 case LFUN_FONT_ROMAN:
788 if (currentMode() == TEXT_MODE)
789 handleFont(cur, cmd.argument(), "textrm");
791 handleFont(cur, cmd.argument(), "mathrm");
794 if (currentMode() == TEXT_MODE)
795 handleFont(cur, cmd.argument(), "texttt");
797 handleFont(cur, cmd.argument(), "mathtt");
800 handleFont(cur, cmd.argument(), "mathfrak");
803 if (currentMode() == TEXT_MODE)
804 handleFont(cur, cmd.argument(), "textit");
806 handleFont(cur, cmd.argument(), "mathit");
809 if (currentMode() == TEXT_MODE)
810 // FIXME: should be "noun"
811 handleFont(cur, cmd.argument(), "textsc");
813 handleFont(cur, cmd.argument(), "mathbb");
816 case LFUN_FONT_FREE_APPLY:
817 handleFont(cur, cmd.argument(), "textrm");
820 case LFUN_FONT_DEFAULT:
821 handleFont(cur, cmd.argument(), "textnormal");
824 case LFUN_MATH_MODE: {
826 // ignore math-mode on when already in math mode
827 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
829 cur.macroModeClose();
830 docstring const save_selection = grabAndEraseSelection(cur);
832 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
833 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
835 cur.pushLeft(*cur.nextInset());
836 cur.niceInsert(save_selection);
838 if (currentMode() == Inset::TEXT_MODE) {
839 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
840 cur.message(_("create new math text environment ($...$)"));
842 handleFont(cur, cmd.argument(), "textrm");
843 cur.message(_("entered math text mode (textrm)"));
856 case LFUN_MATH_MATRIX: {
857 recordUndo(cur, Undo::ATOMIC);
862 idocstringstream is(cmd.argument());
863 is >> m >> n >> v_align >> h_align;
870 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
874 case LFUN_MATH_DELIM: {
876 docstring rs = support::split(cmd.argument(), ls, ' ');
877 // Reasonable default values
882 recordUndo(cur, Undo::ATOMIC);
883 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
887 case LFUN_MATH_BIGDELIM: {
888 docstring const lname = from_utf8(cmd.getArg(0));
889 docstring const ldelim = from_utf8(cmd.getArg(1));
890 docstring const rname = from_utf8(cmd.getArg(2));
891 docstring const rdelim = from_utf8(cmd.getArg(3));
892 latexkeys const * l = in_word_set(lname);
893 bool const have_l = l && l->inset == "big" &&
894 InsetMathBig::isBigInsetDelim(ldelim);
895 l = in_word_set(rname);
896 bool const have_r = l && l->inset == "big" &&
897 InsetMathBig::isBigInsetDelim(rdelim);
898 // We mimic LFUN_MATH_DELIM in case we have an empty left
899 // or right delimiter.
900 if (have_l || have_r) {
901 recordUndo(cur, Undo::ATOMIC);
902 docstring const selection = grabAndEraseSelection(cur);
905 cur.insert(MathAtom(new InsetMathBig(lname,
907 cur.niceInsert(selection);
909 cur.insert(MathAtom(new InsetMathBig(rname,
912 // Don't call cur.undispatched() if we did nothing, this would
913 // lead to infinite recursion via Text::dispatch().
917 case LFUN_SPACE_INSERT:
918 case LFUN_MATH_SPACE:
919 recordUndo(cur, Undo::ATOMIC);
920 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
923 case LFUN_ERT_INSERT:
924 // interpret this as if a backslash was typed
925 recordUndo(cur, Undo::ATOMIC);
926 interpretChar(cur, '\\');
929 case LFUN_MATH_SUBSCRIPT:
930 // interpret this as if a _ was typed
931 recordUndo(cur, Undo::ATOMIC);
932 interpretChar(cur, '_');
935 case LFUN_MATH_SUPERSCRIPT:
936 // interpret this as if a ^ was typed
937 recordUndo(cur, Undo::ATOMIC);
938 interpretChar(cur, '^');
941 case LFUN_QUOTE_INSERT:
942 // interpret this as if a straight " was typed
943 recordUndo(cur, Undo::ATOMIC);
944 interpretChar(cur, '\"');
947 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
948 // handling such that "self-insert" works on "arbitrary stuff" too, and
949 // math-insert only handles special math things like "matrix".
950 case LFUN_MATH_INSERT: {
951 recordUndo(cur, Undo::ATOMIC);
952 if (cmd.argument() == "^" || cmd.argument() == "_") {
953 interpretChar(cur, cmd.argument()[0]);
955 cur.niceInsert(cmd.argument());
959 case LFUN_DIALOG_SHOW_NEW_INSET: {
960 docstring const & name = cmd.argument();
963 InsetMathRef tmp(name);
964 data = tmp.createDialogStr(to_utf8(name));
966 cur.bv().showInsetDialog(to_utf8(name), data, 0);
970 case LFUN_INSET_INSERT: {
972 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
981 InsetMath::doDispatch(cur, cmd);
987 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
988 FuncStatus & flag) const
990 // the font related toggles
991 //string tc = "mathnormal";
993 string const arg = to_utf8(cmd.argument());
994 switch (cmd.action) {
995 case LFUN_TABULAR_FEATURE:
999 case LFUN_TABULAR_FEATURE:
1000 // FIXME: check temporarily disabled
1002 char align = mathcursor::valign();
1003 if (align == '\0') {
1007 if (cmd.argument().empty()) {
1011 if (!contains("tcb", cmd.argument()[0])) {
1015 flag.setOnOff(cmd.argument()[0] == align);
1018 /// We have to handle them since 1.4 blocks all unhandled actions
1019 case LFUN_FONT_ITAL:
1020 case LFUN_FONT_BOLD:
1021 case LFUN_FONT_SANS:
1022 case LFUN_FONT_EMPH:
1023 case LFUN_FONT_CODE:
1024 case LFUN_FONT_NOUN:
1025 case LFUN_FONT_ROMAN:
1026 case LFUN_FONT_DEFAULT:
1029 case LFUN_MATH_MUTATE:
1030 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1031 flag.setOnOff(false);
1034 // we just need to be in math mode to enable that
1035 case LFUN_MATH_SIZE:
1036 case LFUN_MATH_SPACE:
1037 case LFUN_MATH_LIMITS:
1038 case LFUN_MATH_NONUMBER:
1039 case LFUN_MATH_NUMBER:
1040 case LFUN_MATH_EXTERN:
1044 case LFUN_FONT_FRAK:
1045 flag.enabled(currentMode() != TEXT_MODE);
1048 case LFUN_MATH_INSERT: {
1049 bool const textarg =
1050 arg == "\\textbf" || arg == "\\textsf" ||
1051 arg == "\\textrm" || arg == "\\textmd" ||
1052 arg == "\\textit" || arg == "\\textsc" ||
1053 arg == "\\textsl" || arg == "\\textup" ||
1054 arg == "\\texttt" || arg == "\\textbb" ||
1055 arg == "\\textnormal";
1056 flag.enabled(currentMode() != TEXT_MODE || textarg);
1060 case LFUN_MATH_MATRIX:
1061 flag.enabled(currentMode() == MATH_MODE);
1064 case LFUN_INSET_INSERT: {
1065 // Don't test createMathInset_fromDialogStr(), since
1066 // getStatus is not called with a valid reference and the
1067 // dialog would not be applyable.
1068 string const name = cmd.getArg(0);
1069 flag.enabled(name == "ref");
1073 case LFUN_MATH_DELIM:
1074 case LFUN_MATH_BIGDELIM:
1075 // Don't do this with multi-cell selections
1076 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1079 case LFUN_HYPHENATION_POINT_INSERT:
1080 case LFUN_LIGATURE_BREAK_INSERT:
1081 case LFUN_MENU_SEPARATOR_INSERT:
1082 case LFUN_DOTS_INSERT:
1083 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
1084 // FIXME: These would probably make sense in math-text mode
1085 flag.enabled(false);
1096 void InsetMathNest::edit(Cursor & cur, bool left)
1099 cur.idx() = left ? 0 : cur.lastidx();
1100 cur.pos() = left ? 0 : cur.lastpos();
1102 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1106 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1109 int dist_min = 1000000;
1110 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1111 int const d = cell(i).dist(cur.bv(), x, y);
1117 MathData & ar = cell(idx_min);
1119 cur.idx() = idx_min;
1120 cur.pos() = ar.x2pos(x - ar.xo(cur.bv()));
1122 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1123 if (dist_min == 0) {
1125 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1126 if (ar[i]->covers(cur.bv(), x, y))
1127 return ar[i].nucleus()->editXY(cur, x, y);
1133 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1135 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1136 BufferView & bv = cur.bv();
1137 if (cmd.button() == mouse_button::button1) {
1138 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1139 bv.mouseSetCursor(cur);
1140 // Update the cursor update flags as needed:
1142 // Update::Decoration: tells to update the decoration (visual box
1143 // corners that define the inset)/
1144 // Update::FitCursor: adjust the screen to the cursor position if
1146 // cur.result().update(): don't overwrite previously set flags.
1147 cur.updateFlags(Update::Decoration | Update::FitCursor | cur.result().update());
1148 } else if (cmd.button() == mouse_button::button2) {
1150 if (cap::selection()) {
1151 // See comment in Text::dispatch why we do this
1152 cap::copySelectionToStack();
1153 cmd = FuncRequest(LFUN_PASTE, "0");
1154 doDispatch(cur, cmd);
1156 asArray(theSelection().get(), ar);
1159 bv.mouseSetCursor(cur);
1164 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1166 // only select with button 1
1167 if (cmd.button() == mouse_button::button1) {
1168 Cursor & bvcur = cur.bv().cursor();
1169 if (bvcur.anchor_.hasPart(cur)) {
1170 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1171 bvcur.setCursor(cur);
1172 bvcur.selection() = true;
1173 //lyxerr << "MOTION " << bvcur << endl;
1180 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1182 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1184 if (cmd.button() == mouse_button::button1) {
1185 if (!cur.selection())
1188 Cursor & bvcur = cur.bv().cursor();
1189 bvcur.selection() = true;
1190 cap::saveSelection(bvcur);
1199 bool InsetMathNest::interpretChar(Cursor & cur, char_type c)
1201 //lyxerr << "interpret 2: '" << c << "'" << endl;
1202 docstring save_selection;
1203 if (c == '^' || c == '_')
1204 save_selection = grabAndEraseSelection(cur);
1209 if (cur.inMacroMode()) {
1210 docstring name = cur.macroName();
1212 /// are we currently typing '#1' or '#2' or...?
1213 if (name == "\\#") {
1216 if (n >= 1 && n <= 9)
1217 cur.insert(new MathMacroArgument(n));
1221 if (isAlphaASCII(c)) {
1222 cur.activeMacro()->setName(name + docstring(1, c));
1226 // handle 'special char' macros
1231 if (currentMode() == InsetMath::TEXT_MODE)
1232 cur.niceInsert(createInsetMath("textbackslash"));
1234 cur.niceInsert(createInsetMath("backslash"));
1235 } else if (c == '{') {
1237 cur.niceInsert(MathAtom(new InsetMathBrace));
1238 } else if (c == '%') {
1240 cur.niceInsert(MathAtom(new InsetMathComment));
1241 } else if (c == '#') {
1242 BOOST_ASSERT(cur.activeMacro());
1243 cur.activeMacro()->setName(name + docstring(1, c));
1246 cur.niceInsert(createInsetMath(docstring(1, c)));
1251 // One character big delimiters. The others are handled in
1252 // interpretString().
1253 latexkeys const * l = in_word_set(name.substr(1));
1254 if (name[0] == '\\' && l && l->inset == "big") {
1258 delim = from_ascii("\\{");
1261 delim = from_ascii("\\}");
1264 delim = docstring(1, c);
1267 if (InsetMathBig::isBigInsetDelim(delim)) {
1268 // name + delim ared a valid InsetMathBig.
1269 // We can't use cur.macroModeClose() because
1270 // it does not handle delim.
1271 InsetMathUnknown * p = cur.activeMacro();
1274 cur.cell().erase(cur.pos());
1275 cur.plainInsert(MathAtom(
1276 new InsetMathBig(name.substr(1), delim)));
1281 // leave macro mode and try again if necessary
1282 cur.macroModeClose();
1284 cur.niceInsert(MathAtom(new InsetMathBrace));
1286 interpretChar(cur, c);
1290 // This is annoying as one has to press <space> far too often.
1294 // leave autocorrect mode if necessary
1295 if (autocorrect() && c == ' ') {
1296 autocorrect() = false;
1301 // just clear selection on pressing the space bar
1302 if (cur.selection() && c == ' ') {
1303 cur.selection() = false;
1310 //lyxerr << "starting with macro" << endl;
1311 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), false)));
1316 if (currentMode() == InsetMath::TEXT_MODE)
1322 if (currentMode() == InsetMath::TEXT_MODE) {
1323 // insert spaces in text mode,
1324 // but suppress direct insertion of two spaces in a row
1325 // the still allows typing '<space>a<space>' and deleting the 'a', but
1326 // it is better than nothing...
1327 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1329 // FIXME: we have to enable full redraw here because of the
1330 // visual box corners that define the inset. If we know for
1331 // sure that we stay within the same cell we can optimize for
1333 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1337 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1338 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1339 // FIXME: we have to enable full redraw here because of the
1340 // visual box corners that define the inset. If we know for
1341 // sure that we stay within the same cell we can optimize for
1343 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1347 if (cur.popRight()) {
1348 // FIXME: we have to enable full redraw here because of the
1349 // visual box corners that define the inset. If we know for
1350 // sure that we stay within the same cell we can optimize for
1352 //cur.updateFlags(Update::FitCursor);
1356 // if we are at the very end, leave the formula
1357 return cur.pos() != cur.lastpos();
1360 // These shouldn't work in text mode:
1361 if (currentMode() != InsetMath::TEXT_MODE) {
1363 script(cur, false, save_selection);
1367 script(cur, true, save_selection);
1371 cur.niceInsert(createInsetMath("sim"));
1376 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1377 c == '%' || c == '_' || c == '^') {
1378 cur.niceInsert(createInsetMath(docstring(1, c)));
1383 // try auto-correction
1384 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1387 // no special circumstances, so insert the character without any fuss
1389 cur.autocorrect() = true;
1394 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1396 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1398 if (!cur.empty() && cur.pos() > 0 &&
1399 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1400 if (InsetMathBig::isBigInsetDelim(str)) {
1401 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1402 if (prev[0] == '\\') {
1403 prev = prev.substr(1);
1404 latexkeys const * l = in_word_set(prev);
1405 if (l && l->inset == "big") {
1406 cur.cell()[cur.pos() - 1] =
1407 MathAtom(new InsetMathBig(prev, str));
1417 bool InsetMathNest::script(Cursor & cur, bool up,
1418 docstring const & save_selection)
1420 // Hack to get \^ and \_ working
1421 //lyxerr << "handling script: up: " << up << endl;
1422 if (cur.inMacroMode() && cur.macroName() == "\\") {
1424 cur.niceInsert(createInsetMath("mathcircumflex"));
1426 interpretChar(cur, '_');
1430 cur.macroModeClose();
1431 if (asScriptInset() && cur.idx() == 0) {
1432 // we are in a nucleus of a script inset, move to _our_ script
1433 InsetMathScript * inset = asScriptInset();
1434 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1436 cur.idx() = inset->idxOfScript(up);
1438 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1440 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1443 cur.idx() = inset->idxOfScript(up);
1444 cur.pos() = cur.lastpos();
1446 // convert the thing to our left to a scriptinset or create a new
1447 // one if in the very first position of the array
1448 if (cur.pos() == 0) {
1449 //lyxerr << "new scriptinset" << endl;
1450 cur.insert(new InsetMathScript(up));
1452 //lyxerr << "converting prev atom " << endl;
1453 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1456 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1457 // See comment in MathParser.cpp for special handling of {}-bases
1463 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1464 cur.niceInsert(save_selection);
1466 //lyxerr << "inserting selection 2:\n" << save_selection << endl;