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 "InsetMathAMSArray.h"
17 #include "InsetMathBig.h"
18 #include "InsetMathBox.h"
19 #include "InsetMathBrace.h"
20 #include "InsetMathChar.h"
21 #include "InsetMathColor.h"
22 #include "InsetMathComment.h"
23 #include "InsetMathDelim.h"
24 #include "InsetMathEnsureMath.h"
25 #include "InsetMathHull.h"
26 #include "InsetMathRef.h"
27 #include "InsetMathScript.h"
28 #include "InsetMathSpace.h"
29 #include "InsetMathSymbol.h"
30 #include "InsetMathUnknown.h"
31 #include "MathAutoCorrect.h"
32 #include "MathCompletionList.h"
34 #include "MathFactory.h"
35 #include "MathMacro.h"
36 #include "MathMacroArgument.h"
37 #include "MathParser.h"
38 #include "MathStream.h"
39 #include "MathSupport.h"
43 #include "BufferView.h"
44 #include "CoordCache.h"
46 #include "CutAndPaste.h"
47 #include "DispatchResult.h"
49 #include "FuncRequest.h"
50 #include "FuncStatus.h"
53 #include "OutputParams.h"
56 #include "frontends/Clipboard.h"
57 #include "frontends/Painter.h"
58 #include "frontends/Selection.h"
60 #include "support/lassert.h"
61 #include "support/debug.h"
62 #include "support/gettext.h"
63 #include "support/lstrings.h"
64 #include "support/textutils.h"
65 #include "support/docstream.h"
71 using namespace lyx::support;
75 using cap::copySelection;
76 using cap::grabAndEraseSelection;
77 using cap::cutSelection;
78 using cap::replaceSelection;
79 using cap::selClearOrDel;
82 InsetMathNest::InsetMathNest(Buffer * buf, idx_type nargs)
83 : InsetMath(buf), cells_(nargs), lock_(false), mouse_hover_(false)
89 InsetMathNest::InsetMathNest(InsetMathNest const & inset)
90 : InsetMath(inset), cells_(inset.cells_), lock_(inset.lock_),
95 InsetMathNest & InsetMathNest::operator=(InsetMathNest const & inset)
97 cells_ = inset.cells_;
100 InsetMath::operator=(inset);
105 void InsetMathNest::setBuffer(Buffer & buffer)
107 InsetMath::setBuffer(buffer);
108 for (idx_type i = 0, n = nargs(); i != n; ++i) {
109 MathData & data = cell(i);
110 for (size_t j = 0; j != data.size(); ++j)
111 data[j].nucleus()->setBuffer(buffer);
116 InsetMath::idx_type InsetMathNest::nargs() const
118 return cells_.size();
122 void InsetMathNest::cursorPos(BufferView const & bv,
123 CursorSlice const & sl, bool /*boundary*/,
124 int & x, int & y) const
126 // FIXME: This is a hack. Ideally, the coord cache should not store
127 // absolute positions, but relative ones. This would mean to call
128 // setXY() not in MathData::draw(), but in the parent insets' draw()
129 // with the correctly adjusted x,y values. But this means that we'd have
130 // to touch all (math)inset's draw() methods. Right now, we'll store
131 // absolute value, and make them here relative, only to make them
132 // absolute again when actually drawing the cursor. What a mess.
133 LASSERT(&sl.inset() == this, /**/);
134 MathData const & ar = sl.cell();
135 CoordCache const & coord_cache = bv.coordCache();
136 if (!coord_cache.getArrays().has(&ar)) {
137 // this can (semi-)legally happen if we just created this cell
138 // and it never has been drawn before. So don't ASSERT.
139 //lyxerr << "no cached data for array " << &ar << endl;
144 Point const pt = coord_cache.getArrays().xy(&ar);
145 if (!coord_cache.getInsets().has(this)) {
147 //lyxerr << "no cached data for inset " << this << endl;
152 Point const pt2 = coord_cache.getInsets().xy(this);
153 //lyxerr << "retrieving position cache for MathData "
154 // << pt.x_ << ' ' << pt.y_ << endl;
155 x = pt.x_ - pt2.x_ + ar.pos2x(&bv, sl.pos());
157 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
158 // << " asc: " << ascent() << " des: " << descent()
159 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
160 // move cursor visually into empty cells ("blue rectangles");
166 void InsetMathNest::metrics(MetricsInfo const & mi) const
169 for (idx_type i = 0, n = nargs(); i != n; ++i) {
171 cell(i).metrics(m, dim);
176 void InsetMathNest::updateLabels(ParIterator const & it, UpdateType utype)
178 for (idx_type i = 0, n = nargs(); i != n; ++i)
179 cell(i).updateLabels(it, utype);
184 bool InsetMathNest::idxNext(Cursor & cur) const
186 LASSERT(&cur.inset() == this, /**/);
187 if (cur.idx() == cur.lastidx())
195 bool InsetMathNest::idxForward(Cursor & cur) const
201 bool InsetMathNest::idxPrev(Cursor & cur) const
203 LASSERT(&cur.inset() == this, /**/);
207 cur.pos() = cur.lastpos();
212 bool InsetMathNest::idxBackward(Cursor & cur) const
218 bool InsetMathNest::idxFirst(Cursor & cur) const
220 LASSERT(&cur.inset() == this, /**/);
229 bool InsetMathNest::idxLast(Cursor & cur) const
231 LASSERT(&cur.inset() == this, /**/);
234 cur.idx() = cur.lastidx();
235 cur.pos() = cur.lastpos();
240 void InsetMathNest::dump() const
242 odocstringstream oss;
244 os << "---------------------------------------------\n";
247 for (idx_type i = 0, n = nargs(); i != n; ++i)
248 os << cell(i) << "\n";
249 os << "---------------------------------------------\n";
250 lyxerr << to_utf8(oss.str());
254 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
258 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
261 setPosCache(pi, x, y);
265 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
267 BufferView & bv = *pi.base.bv;
268 // this should use the x/y values given, not the cached values
269 Cursor & cur = bv.cursor();
270 if (!cur.selection())
272 if (&cur.inset() != this)
275 // FIXME: hack to get position cache warm
276 bool const original_drawing_state = pi.pain.isDrawingEnabled();
277 pi.pain.setDrawingEnabled(false);
279 pi.pain.setDrawingEnabled(original_drawing_state);
281 CursorSlice s1 = cur.selBegin();
282 CursorSlice s2 = cur.selEnd();
284 //lyxerr << "InsetMathNest::drawing selection: "
285 // << " s1: " << s1 << " s2: " << s2 << endl;
286 if (s1.idx() == s2.idx()) {
287 MathData const & c = cell(s1.idx());
288 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
289 int x1 = g.pos.x_ + c.pos2x(pi.base.bv, s1.pos());
290 int y1 = g.pos.y_ - g.dim.ascent();
291 int x2 = g.pos.x_ + c.pos2x(pi.base.bv, s2.pos());
292 int y2 = g.pos.y_ + g.dim.descent();
293 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
294 //lyxerr << "InsetMathNest::drawing selection 3: "
295 // << " x1: " << x1 << " x2: " << x2
296 // << " y1: " << y1 << " y2: " << y2 << endl;
298 for (idx_type i = 0; i < nargs(); ++i) {
299 if (idxBetween(i, s1.idx(), s2.idx())) {
300 MathData const & c = cell(i);
301 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
303 int y1 = g.pos.y_ - g.dim.ascent();
304 int x2 = g.pos.x_ + g.dim.width();
305 int y2 = g.pos.y_ + g.dim.descent();
306 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
313 void InsetMathNest::validate(LaTeXFeatures & features) const
315 for (idx_type i = 0; i < nargs(); ++i)
316 cell(i).validate(features);
320 void InsetMathNest::replace(ReplaceData & rep)
322 for (idx_type i = 0; i < nargs(); ++i)
323 cell(i).replace(rep);
327 bool InsetMathNest::contains(MathData const & ar) const
329 for (idx_type i = 0; i < nargs(); ++i)
330 if (cell(i).contains(ar))
336 bool InsetMathNest::lock() const
342 void InsetMathNest::lock(bool l)
348 bool InsetMathNest::isActive() const
354 MathData InsetMathNest::glue() const
357 for (size_t i = 0; i < nargs(); ++i)
363 void InsetMathNest::write(WriteStream & os) const
365 ModeSpecifier specifier(os, currentMode(), lockedMode());
366 docstring const latex_name = name();
367 os << '\\' << latex_name;
368 for (size_t i = 0; i < nargs(); ++i)
369 os << '{' << cell(i) << '}';
371 os.pendingSpace(true);
372 if (lock_ && !os.latex()) {
374 os.pendingSpace(true);
379 void InsetMathNest::normalize(NormalStream & os) const
382 for (size_t i = 0; i < nargs(); ++i)
383 os << ' ' << cell(i);
388 int InsetMathNest::latex(odocstream & os, OutputParams const & runparams) const
390 WriteStream wi(os, runparams.moving_arg, true,
391 runparams.dryrun ? WriteStream::wsDryrun : WriteStream::wsDefault,
398 bool InsetMathNest::setMouseHover(bool mouse_hover)
400 mouse_hover_ = mouse_hover;
405 bool InsetMathNest::notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
409 MathData & ar = cur.cell();
410 // remove base-only "scripts"
411 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
412 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
413 if (p && p->nargs() == 1) {
414 MathData ar = p->nuc();
417 cur.adjust(i, ar.size() - 1);
421 // glue adjacent font insets of the same kind
422 for (pos_type i = 0; i + 1 < size(); ++i) {
423 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
424 InsetMathFont const * q = operator[](i + 1)->asFontInset();
425 if (p && q && p->name() == q->name()) {
426 p->cell(0).append(q->cell(0));
436 void InsetMathNest::handleFont
437 (Cursor & cur, docstring const & arg, char const * const font)
439 handleFont(cur, arg, from_ascii(font));
443 void InsetMathNest::handleFont(Cursor & cur, docstring const & arg,
444 docstring const & font)
446 cur.recordUndoSelection();
448 // this whole function is a hack and won't work for incremental font
450 if (cur.inset().asInsetMath()->name() == font)
451 cur.handleFont(to_utf8(font));
453 handleNest(cur, createInsetMath(font, cur.buffer()), arg);
457 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest)
459 handleNest(cur, nest, docstring());
463 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest,
464 docstring const & arg)
466 CursorSlice i1 = cur.selBegin();
467 CursorSlice i2 = cur.selEnd();
468 if (!i1.inset().asInsetMath())
470 if (i1.idx() == i2.idx()) {
471 // the easy case where only one cell is selected
472 cur.handleNest(nest);
477 // multiple selected cells in a simple non-grid inset
478 if (i1.asInsetMath()->nrows() == 0 || i1.asInsetMath()->ncols() == 0) {
479 for (idx_type i = i1.idx(); i <= i2.idx(); ++i) {
484 cur.pos() = cur.lastpos();
487 // change font of cell
488 cur.handleNest(nest);
491 // cur is in the font inset now. If the loop continues,
492 // we need to get outside again for the next cell
493 if (i + 1 <= i2.idx())
499 // the complicated case with multiple selected cells in a grid
502 cap::region(i1, i2, r1, r2, c1, c2);
503 for (row_type row = r1; row <= r2; ++row) {
504 for (col_type col = c1; col <= c2; ++col) {
506 cur.idx() = i1.asInsetMath()->index(row, col);
509 cur.pos() = cur.lastpos();
513 cur.handleNest(nest);
516 // cur is in the font inset now. If the loop continues,
517 // we need to get outside again for the next cell
518 if (col + 1 <= c2 || row + 1 <= r2)
525 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
527 cur.recordUndoSelection();
530 font.fromString(to_utf8(arg), b);
531 if (font.fontInfo().color() != Color_inherit &&
532 font.fontInfo().color() != Color_ignore)
533 handleNest(cur, MathAtom(new InsetMathColor(buffer_, true, font.fontInfo().color())));
535 // FIXME: support other font changes here as well?
539 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
541 //LYXERR0("InsetMathNest: request: " << cmd);
543 Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
545 switch (cmd.action) {
547 case LFUN_CLIPBOARD_PASTE:
548 parseflg |= Parse::VERBATIM;
551 if (cur.currentMode() <= TEXT_MODE)
552 parseflg |= Parse::TEXTMODE;
553 cur.recordUndoSelection();
554 cur.message(_("Paste"));
555 replaceSelection(cur);
557 if (cmd.argument().empty() && !theClipboard().isInternal())
558 topaste = theClipboard().getAsText();
561 idocstringstream is(cmd.argument());
563 topaste = cap::selection(n);
565 cur.niceInsert(topaste, parseflg, false);
566 cur.clearSelection(); // bug 393
567 // FIXME audit setBuffer/updateLabels calls
568 cur.buffer()->updateLabels();
575 cutSelection(cur, true, true);
576 cur.message(_("Cut"));
577 // Prevent stale position >= size crash
578 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
580 // FIXME audit setBuffer/updateLabels calls
581 cur.buffer()->updateLabels();
586 cur.message(_("Copy"));
589 case LFUN_MOUSE_PRESS:
590 lfunMousePress(cur, cmd);
593 case LFUN_MOUSE_MOTION:
594 lfunMouseMotion(cur, cmd);
597 case LFUN_MOUSE_RELEASE:
598 lfunMouseRelease(cur, cmd);
601 case LFUN_FINISHED_LEFT: // in math, left is backwards
602 case LFUN_FINISHED_BACKWARD:
603 cur.bv().cursor() = cur;
606 case LFUN_FINISHED_RIGHT: // in math, right is forward
607 case LFUN_FINISHED_FORWARD:
609 cur.bv().cursor() = cur;
612 case LFUN_CHAR_RIGHT:
614 case LFUN_CHAR_BACKWARD:
615 case LFUN_CHAR_FORWARD:
616 cur.updateFlags(Update::Decoration | Update::FitCursor);
617 case LFUN_CHAR_RIGHT_SELECT:
618 case LFUN_CHAR_LEFT_SELECT:
619 case LFUN_CHAR_BACKWARD_SELECT:
620 case LFUN_CHAR_FORWARD_SELECT: {
621 // are we in a selection?
622 bool select = (cmd.action == LFUN_CHAR_RIGHT_SELECT
623 || cmd.action == LFUN_CHAR_LEFT_SELECT
624 || cmd.action == LFUN_CHAR_BACKWARD_SELECT
625 || cmd.action == LFUN_CHAR_FORWARD_SELECT);
626 // are we moving forward or backwards?
627 // If the command was RIGHT or LEFT, then whether we're moving forward
628 // or backwards depends on the cursor movement mode (logical or visual):
629 // * in visual mode, since math is always LTR, right -> forward,
631 // * in logical mode, the mapping is determined by the
632 // reverseDirectionNeeded() function
635 FuncCode finish_lfun;
637 if (cmd.action == LFUN_CHAR_FORWARD
638 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
640 finish_lfun = LFUN_FINISHED_FORWARD;
642 else if (cmd.action == LFUN_CHAR_BACKWARD
643 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
645 finish_lfun = LFUN_FINISHED_BACKWARD;
648 bool right = (cmd.action == LFUN_CHAR_RIGHT_SELECT
649 || cmd.action == LFUN_CHAR_RIGHT);
650 if (lyxrc.visual_cursor || !reverseDirectionNeeded(cur))
656 finish_lfun = LFUN_FINISHED_RIGHT;
658 finish_lfun = LFUN_FINISHED_LEFT;
660 // Now that we know exactly what we want to do, let's do it!
661 cur.selHandle(select);
663 cur.macroModeClose();
664 // try moving forward or backwards as necessary...
665 if (!(forward ? cursorMathForward(cur) : cursorMathBackward(cur))) {
666 // ... and if movement failed, then finish forward or backwards
668 cmd = FuncRequest(finish_lfun);
676 cur.updateFlags(Update::Decoration | Update::FitCursor);
677 case LFUN_DOWN_SELECT:
678 case LFUN_UP_SELECT: {
679 // close active macro
680 if (cur.inMacroMode()) {
681 cur.macroModeClose();
685 // stop/start the selection
686 bool select = cmd.action == LFUN_DOWN_SELECT ||
687 cmd.action == LFUN_UP_SELECT;
688 cur.selHandle(select);
691 bool up = cmd.action == LFUN_UP || cmd.action == LFUN_UP_SELECT;
692 bool successful = cur.upDownInMath(up);
696 if (cur.fixIfBroken())
697 // FIXME: Something bad happened. We pass the corrected Cursor
698 // instead of letting things go worse.
701 // We did not manage to move the cursor.
706 case LFUN_MOUSE_DOUBLE:
707 case LFUN_MOUSE_TRIPLE:
708 case LFUN_WORD_SELECT:
712 cur.setSelection(true);
713 cur.pos() = cur.lastpos();
714 cur.idx() = cur.lastidx();
717 case LFUN_PARAGRAPH_UP:
718 case LFUN_PARAGRAPH_DOWN:
719 cur.updateFlags(Update::Decoration | Update::FitCursor);
720 case LFUN_PARAGRAPH_UP_SELECT:
721 case LFUN_PARAGRAPH_DOWN_SELECT:
724 case LFUN_LINE_BEGIN:
725 case LFUN_WORD_BACKWARD:
727 cur.updateFlags(Update::Decoration | Update::FitCursor);
728 case LFUN_LINE_BEGIN_SELECT:
729 case LFUN_WORD_BACKWARD_SELECT:
730 case LFUN_WORD_LEFT_SELECT:
731 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
732 cmd.action == LFUN_WORD_LEFT_SELECT ||
733 cmd.action == LFUN_LINE_BEGIN_SELECT);
734 cur.macroModeClose();
735 if (cur.pos() != 0) {
737 } else if (cur.col() != 0) {
738 cur.idx() -= cur.col();
740 } else if (cur.idx() != 0) {
744 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
749 case LFUN_WORD_FORWARD:
750 case LFUN_WORD_RIGHT:
752 cur.updateFlags(Update::Decoration | Update::FitCursor);
753 case LFUN_WORD_FORWARD_SELECT:
754 case LFUN_WORD_RIGHT_SELECT:
755 case LFUN_LINE_END_SELECT:
756 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
757 cmd.action == LFUN_WORD_RIGHT_SELECT ||
758 cmd.action == LFUN_LINE_END_SELECT);
759 cur.macroModeClose();
761 if (cur.pos() != cur.lastpos()) {
762 cur.pos() = cur.lastpos();
763 } else if (ncols() && (cur.col() != cur.lastcol())) {
764 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
765 cur.pos() = cur.lastpos();
766 } else if (cur.idx() != cur.lastidx()) {
767 cur.idx() = cur.lastidx();
768 cur.pos() = cur.lastpos();
770 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
775 case LFUN_CELL_FORWARD:
776 cur.updateFlags(Update::Decoration | Update::FitCursor);
777 cur.inset().idxNext(cur);
780 case LFUN_CELL_BACKWARD:
781 cur.updateFlags(Update::Decoration | Update::FitCursor);
782 cur.inset().idxPrev(cur);
785 case LFUN_WORD_DELETE_BACKWARD:
786 case LFUN_CHAR_DELETE_BACKWARD:
788 // May affect external cell:
789 cur.recordUndoInset();
791 cur.recordUndoSelection();
792 // if the inset can not be removed from within, delete it
793 if (!cur.backspace()) {
794 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
795 cur.innerText()->dispatch(cur, cmd);
799 case LFUN_WORD_DELETE_FORWARD:
800 case LFUN_CHAR_DELETE_FORWARD:
801 if (cur.pos() == cur.lastpos())
802 // May affect external cell:
803 cur.recordUndoInset();
805 cur.recordUndoSelection();
806 // if the inset can not be removed from within, delete it
808 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
809 cur.innerText()->dispatch(cur, cmd);
815 cur.clearSelection();
817 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
822 // 'Locks' the math inset. A 'locked' math inset behaves as a unit
823 // that is traversed by a single <CursorLeft>/<CursorRight>.
824 case LFUN_INSET_TOGGLE:
830 case LFUN_SELF_INSERT:
831 if (cmd.argument().size() != 1) {
832 cur.recordUndoSelection();
833 docstring const arg = cmd.argument();
834 if (!interpretString(cur, arg))
838 // Don't record undo steps if we are in macro mode and thus
839 // cmd.argument is the next character of the macro name.
840 // Otherwise we'll get an invalid cursor if we undo after
841 // the macro was finished and the macro is a known command,
842 // e.g. sqrt. Cursor::macroModeClose replaces in this case
843 // the InsetMathUnknown with name "frac" by an empty
844 // InsetMathFrac -> a pos value > 0 is invalid.
845 // A side effect is that an undo before the macro is finished
846 // undoes the complete macro, not only the last character.
847 // At the time we hit '\' we are not in macro mode, still.
848 if (!cur.inMacroMode())
849 cur.recordUndoSelection();
851 // spacial handling of space. If we insert an inset
852 // via macro mode, we want to put the cursor inside it
853 // if relevant. Think typing "\frac<space>".
854 if (cmd.argument()[0] == ' '
855 && cur.inMacroMode() && cur.macroName() != "\\"
856 && cur.macroModeClose()) {
857 MathAtom const atom = cur.prevAtom();
858 if (atom->asNestInset() && atom->isActive()) {
860 cur.pushBackward(*cur.nextInset());
862 } else if (!interpretChar(cur, cmd.argument()[0])) {
863 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
868 //case LFUN_SERVER_GET_XY:
871 case LFUN_SERVER_SET_XY: {
872 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
875 istringstream is(to_utf8(cmd.argument()));
877 cur.setScreenPos(x, y);
881 // Special casing for superscript in case of LyX handling
883 case LFUN_ACCENT_CIRCUMFLEX:
884 if (cmd.argument().empty()) {
885 // do superscript if LyX handles
887 cur.recordUndoSelection();
888 script(cur, true, grabAndEraseSelection(cur));
892 case LFUN_ACCENT_UMLAUT:
893 case LFUN_ACCENT_ACUTE:
894 case LFUN_ACCENT_GRAVE:
895 case LFUN_ACCENT_BREVE:
896 case LFUN_ACCENT_DOT:
897 case LFUN_ACCENT_MACRON:
898 case LFUN_ACCENT_CARON:
899 case LFUN_ACCENT_TILDE:
900 case LFUN_ACCENT_CEDILLA:
901 case LFUN_ACCENT_CIRCLE:
902 case LFUN_ACCENT_UNDERDOT:
903 case LFUN_ACCENT_TIE:
904 case LFUN_ACCENT_OGONEK:
905 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
909 case LFUN_TEXTSTYLE_APPLY:
910 case LFUN_TEXTSTYLE_UPDATE:
911 handleFont2(cur, cmd.argument());
915 if (currentMode() <= TEXT_MODE)
916 handleFont(cur, cmd.argument(), "textbf");
918 handleFont(cur, cmd.argument(), "mathbf");
920 case LFUN_FONT_BOLDSYMBOL:
921 if (currentMode() <= TEXT_MODE)
922 handleFont(cur, cmd.argument(), "textbf");
924 handleFont(cur, cmd.argument(), "boldsymbol");
927 if (currentMode() <= TEXT_MODE)
928 handleFont(cur, cmd.argument(), "textsf");
930 handleFont(cur, cmd.argument(), "mathsf");
933 if (currentMode() <= TEXT_MODE)
934 handleFont(cur, cmd.argument(), "emph");
936 handleFont(cur, cmd.argument(), "mathcal");
938 case LFUN_FONT_ROMAN:
939 if (currentMode() <= TEXT_MODE)
940 handleFont(cur, cmd.argument(), "textrm");
942 handleFont(cur, cmd.argument(), "mathrm");
944 case LFUN_FONT_TYPEWRITER:
945 if (currentMode() <= TEXT_MODE)
946 handleFont(cur, cmd.argument(), "texttt");
948 handleFont(cur, cmd.argument(), "mathtt");
951 handleFont(cur, cmd.argument(), "mathfrak");
954 if (currentMode() <= TEXT_MODE)
955 handleFont(cur, cmd.argument(), "textit");
957 handleFont(cur, cmd.argument(), "mathit");
960 if (currentMode() <= TEXT_MODE)
961 // FIXME: should be "noun"
962 handleFont(cur, cmd.argument(), "textsc");
964 handleFont(cur, cmd.argument(), "mathbb");
966 case LFUN_FONT_DEFAULT:
967 handleFont(cur, cmd.argument(), "textnormal");
970 case LFUN_FONT_UNDERLINE:
972 cur.handleNest(createInsetMath("underline", cur.buffer()));
974 case LFUN_MATH_MODE: {
976 // ignore math-mode on when already in math mode
977 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
979 cur.recordUndoSelection();
980 cur.macroModeClose();
981 docstring const save_selection = grabAndEraseSelection(cur);
983 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
984 if (currentMode() <= Inset::TEXT_MODE)
985 cur.plainInsert(MathAtom(new InsetMathEnsureMath(buffer_)));
987 cur.plainInsert(MathAtom(new InsetMathBox(buffer_, from_ascii("mbox"))));
989 cur.pushBackward(*cur.nextInset());
990 cur.niceInsert(save_selection);
991 // FIXME audit setBuffer/updateLabels calls
992 cur.buffer()->updateLabels();
994 if (currentMode() == Inset::TEXT_MODE) {
995 cur.recordUndoSelection();
996 cur.niceInsert(MathAtom(new InsetMathHull("simple", cur.buffer())));
997 cur.message(_("create new math text environment ($...$)"));
999 handleFont(cur, cmd.argument(), "textrm");
1000 cur.message(_("entered math text mode (textrm)"));
1006 case LFUN_REGEXP_MODE: {
1007 InsetMathHull * i = dynamic_cast<InsetMathHull *>(cur.inset().asInsetMath());
1008 if (i && i->getType() == hullRegexp) {
1009 cur.message(_("Already in regular expression mode"));
1012 cur.macroModeClose();
1013 docstring const save_selection = grabAndEraseSelection(cur);
1015 cur.plainInsert(MathAtom(new InsetMathHull(buffer_, hullRegexp)));
1017 cur.pushBackward(*cur.nextInset());
1018 cur.niceInsert(save_selection);
1019 cur.message(_("Regular expression editor mode"));
1023 case LFUN_MATH_FONT_STYLE: {
1024 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, '\\' + cmd.argument());
1025 doDispatch(cur, fr);
1029 case LFUN_MATH_SIZE: {
1030 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, cmd.argument());
1031 doDispatch(cur, fr);
1035 case LFUN_MATH_MATRIX: {
1041 idocstringstream is(cmd.argument());
1042 is >> m >> n >> v_align >> h_align;
1048 cur.niceInsert(MathAtom(new InsetMathArray(buffer_,
1049 from_ascii("array"), m, n, (char)v_align[0], h_align)));
1053 case LFUN_MATH_AMS_MATRIX: {
1058 idocstringstream is(cmd.argument());
1059 is >> m >> n >> name;
1065 MathAtom(new InsetMathAMSArray(buffer_, name, m, n)));
1069 case LFUN_MATH_DELIM: {
1071 docstring rs = split(cmd.argument(), ls, ' ');
1072 // Reasonable default values
1078 cur.handleNest(MathAtom(new InsetMathDelim(buffer_, ls, rs)));
1082 case LFUN_MATH_BIGDELIM: {
1083 docstring const lname = from_utf8(cmd.getArg(0));
1084 docstring const ldelim = from_utf8(cmd.getArg(1));
1085 docstring const rname = from_utf8(cmd.getArg(2));
1086 docstring const rdelim = from_utf8(cmd.getArg(3));
1087 latexkeys const * l = in_word_set(lname);
1088 bool const have_l = l && l->inset == "big" &&
1089 InsetMathBig::isBigInsetDelim(ldelim);
1090 l = in_word_set(rname);
1091 bool const have_r = l && l->inset == "big" &&
1092 InsetMathBig::isBigInsetDelim(rdelim);
1093 // We mimic LFUN_MATH_DELIM in case we have an empty left
1094 // or right delimiter.
1095 if (have_l || have_r) {
1097 docstring const selection = grabAndEraseSelection(cur);
1100 cur.insert(MathAtom(new InsetMathBig(lname,
1102 cur.niceInsert(selection);
1104 cur.insert(MathAtom(new InsetMathBig(rname,
1107 // Don't call cur.undispatched() if we did nothing, this would
1108 // lead to infinite recursion via Text::dispatch().
1112 case LFUN_SPACE_INSERT:
1113 cur.recordUndoSelection();
1114 cur.insert(MathAtom(new InsetMathSpace));
1117 case LFUN_MATH_SPACE:
1118 cur.recordUndoSelection();
1119 if (cmd.argument().empty())
1120 cur.insert(MathAtom(new InsetMathSpace));
1122 string const name = cmd.getArg(0);
1123 string const len = cmd.getArg(1);
1124 cur.insert(MathAtom(new InsetMathSpace(name, len)));
1128 case LFUN_ERT_INSERT:
1129 // interpret this as if a backslash was typed
1131 interpretChar(cur, '\\');
1134 case LFUN_MATH_SUBSCRIPT:
1135 // interpret this as if a _ was typed
1136 cur.recordUndoSelection();
1137 interpretChar(cur, '_');
1140 case LFUN_MATH_SUPERSCRIPT:
1141 // interpret this as if a ^ was typed
1142 cur.recordUndoSelection();
1143 interpretChar(cur, '^');
1146 case LFUN_MATH_MACRO_FOLD:
1147 case LFUN_MATH_MACRO_UNFOLD: {
1149 bool fold = cmd.action == LFUN_MATH_MACRO_FOLD;
1150 bool found = findMacroToFoldUnfold(it, fold);
1152 MathMacro * macro = it.nextInset()->asInsetMath()->asMacro();
1153 cur.recordUndoInset();
1162 case LFUN_QUOTE_INSERT:
1163 // interpret this as if a straight " was typed
1164 cur.recordUndoSelection();
1165 interpretChar(cur, '\"');
1168 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
1169 // handling such that "self-insert" works on "arbitrary stuff" too, and
1170 // math-insert only handles special math things like "matrix".
1171 case LFUN_MATH_INSERT: {
1172 cur.recordUndoSelection();
1173 if (cmd.argument() == "^" || cmd.argument() == "_")
1174 interpretChar(cur, cmd.argument()[0]);
1177 asArray(cmd.argument(), ar);
1178 if (cur.selection() && ar.size() == 1
1179 && ar[0]->asNestInset()
1180 && ar[0]->asNestInset()->nargs() > 1)
1181 handleNest(cur, ar[0]);
1183 cur.niceInsert(cmd.argument());
1188 case LFUN_DIALOG_SHOW_NEW_INSET: {
1189 docstring const & name = cmd.argument();
1191 if (name == "ref") {
1192 InsetMathRef tmp(buffer_, name);
1193 data = tmp.createDialogStr(to_utf8(name));
1194 } else if (name == "mathspace") {
1196 data = tmp.createDialogStr();
1198 cur.bv().showDialog(to_utf8(name), data);
1202 case LFUN_INSET_INSERT: {
1204 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1205 cur.recordUndoSelection();
1207 // FIXME audit setBuffer/updateLabels calls
1208 cur.buffer()->updateLabels();
1213 case LFUN_INSET_DISSOLVE:
1214 if (!asHullInset()) {
1215 cur.recordUndoInset();
1221 InsetMath::doDispatch(cur, cmd);
1227 bool InsetMathNest::findMacroToFoldUnfold(Cursor & it, bool fold) const {
1228 // look for macro to open/close, but stay in mathed
1229 for (; !it.empty(); it.pop_back()) {
1231 // go backward through the current cell
1232 Inset * inset = it.nextInset();
1233 while (inset && inset->asInsetMath()) {
1234 MathMacro * macro = inset->asInsetMath()->asMacro();
1236 // found the an macro to open/close?
1237 if (macro->folded() != fold)
1240 // Wrong folding state.
1241 // If this was the first we see in this slice, look further left,
1243 if (inset != it.nextInset())
1247 // go up if this was the left most position
1253 inset = it.nextInset();
1261 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1262 FuncStatus & flag) const
1264 // the font related toggles
1265 //string tc = "mathnormal";
1267 string const arg = to_utf8(cmd.argument());
1268 switch (cmd.action) {
1269 case LFUN_TABULAR_FEATURE:
1270 flag.setEnabled(false);
1273 case LFUN_TABULAR_FEATURE:
1274 // FIXME: check temporarily disabled
1276 char align = mathcursor::valign();
1277 if (align == '\0') {
1281 if (cmd.argument().empty()) {
1285 if (!contains("tcb", cmd.argument()[0])) {
1289 flag.setOnOff(cmd.argument()[0] == align);
1292 /// We have to handle them since 1.4 blocks all unhandled actions
1293 case LFUN_FONT_ITAL:
1294 case LFUN_FONT_BOLD:
1295 case LFUN_FONT_BOLDSYMBOL:
1296 case LFUN_FONT_SANS:
1297 case LFUN_FONT_EMPH:
1298 case LFUN_FONT_TYPEWRITER:
1299 case LFUN_FONT_NOUN:
1300 case LFUN_FONT_ROMAN:
1301 case LFUN_FONT_DEFAULT:
1302 flag.setEnabled(true);
1305 // we just need to be in math mode to enable that
1306 case LFUN_MATH_SIZE:
1307 case LFUN_MATH_SPACE:
1308 case LFUN_MATH_LIMITS:
1309 case LFUN_MATH_EXTERN:
1310 flag.setEnabled(true);
1313 case LFUN_FONT_UNDERLINE:
1314 case LFUN_FONT_FRAK:
1315 flag.setEnabled(currentMode() != TEXT_MODE);
1318 case LFUN_MATH_FONT_STYLE: {
1319 bool const textarg =
1320 arg == "textbf" || arg == "textsf" ||
1321 arg == "textrm" || arg == "textmd" ||
1322 arg == "textit" || arg == "textsc" ||
1323 arg == "textsl" || arg == "textup" ||
1324 arg == "texttt" || arg == "textbb" ||
1325 arg == "textnormal";
1326 flag.setEnabled(currentMode() != TEXT_MODE || textarg);
1330 case LFUN_MATH_INSERT:
1331 flag.setEnabled(currentMode() != TEXT_MODE);
1334 case LFUN_MATH_AMS_MATRIX:
1335 case LFUN_MATH_MATRIX:
1336 flag.setEnabled(currentMode() == MATH_MODE);
1339 case LFUN_INSET_INSERT: {
1340 // Don't test createMathInset_fromDialogStr(), since
1341 // getStatus is not called with a valid reference and the
1342 // dialog would not be applyable.
1343 string const name = cmd.getArg(0);
1344 flag.setEnabled(name == "ref" || name == "mathspace");
1348 case LFUN_MATH_DELIM:
1349 case LFUN_MATH_BIGDELIM:
1350 // Don't do this with multi-cell selections
1351 flag.setEnabled(cur.selBegin().idx() == cur.selEnd().idx());
1354 case LFUN_MATH_MACRO_FOLD:
1355 case LFUN_MATH_MACRO_UNFOLD: {
1357 bool found = findMacroToFoldUnfold(it, cmd.action == LFUN_MATH_MACRO_FOLD);
1358 flag.setEnabled(found);
1362 case LFUN_SPECIALCHAR_INSERT:
1363 // FIXME: These would probably make sense in math-text mode
1364 flag.setEnabled(false);
1367 case LFUN_INSET_DISSOLVE:
1368 flag.setEnabled(!asHullInset());
1379 void InsetMathNest::edit(Cursor & cur, bool front, EntryDirection entry_from)
1382 bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_RIGHT ||
1383 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1384 cur.idx() = enter_front ? 0 : cur.lastidx();
1385 cur.pos() = enter_front ? 0 : cur.lastpos();
1387 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1391 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1394 int dist_min = 1000000;
1395 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1396 int const d = cell(i).dist(cur.bv(), x, y);
1402 MathData & ar = cell(idx_min);
1404 cur.idx() = idx_min;
1405 cur.pos() = ar.x2pos(&cur.bv(), x - ar.xo(cur.bv()));
1407 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1408 if (dist_min == 0) {
1410 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1411 if (ar[i]->covers(cur.bv(), x, y))
1412 return ar[i].nucleus()->editXY(cur, x, y);
1418 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1420 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1421 BufferView & bv = cur.bv();
1422 bool do_selection = cmd.button() == mouse_button::button1
1423 && cmd.argument() == "region-select";
1424 bv.mouseSetCursor(cur, do_selection);
1425 if (cmd.button() == mouse_button::button1) {
1426 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1427 // Update the cursor update flags as needed:
1429 // Update::Decoration: tells to update the decoration
1430 // (visual box corners that define
1432 // Update::FitCursor: adjust the screen to the cursor
1433 // position if needed
1434 // cur.result().update(): don't overwrite previously set flags.
1435 cur.updateFlags(Update::Decoration | Update::FitCursor
1436 | cur.result().update());
1437 } else if (cmd.button() == mouse_button::button2) {
1438 if (cap::selection()) {
1439 // See comment in Text::dispatch why we do this
1440 cap::copySelectionToStack();
1441 cmd = FuncRequest(LFUN_PASTE, "0");
1442 doDispatch(bv.cursor(), cmd);
1445 asArray(theSelection().get(), ar);
1446 bv.cursor().insert(ar);
1452 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1454 // only select with button 1
1455 if (cmd.button() == mouse_button::button1) {
1456 Cursor & bvcur = cur.bv().cursor();
1457 if (bvcur.anchor_.hasPart(cur)) {
1458 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1459 bvcur.setCursor(cur);
1460 bvcur.setSelection(true);
1461 //lyxerr << "MOTION " << bvcur << endl;
1468 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1470 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1472 if (cmd.button() == mouse_button::button1) {
1473 if (!cur.selection())
1476 Cursor & bvcur = cur.bv().cursor();
1477 bvcur.setSelection(true);
1486 bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
1488 //lyxerr << "interpret 2: '" << c << "'" << endl;
1489 docstring save_selection;
1490 if (c == '^' || c == '_')
1491 save_selection = grabAndEraseSelection(cur);
1494 Buffer * buf = cur.buffer();
1497 if (cur.inMacroMode()) {
1498 docstring name = cur.macroName();
1500 /// are we currently typing '#1' or '#2' or...?
1501 if (name == "\\#") {
1504 if (n >= 1 && n <= 9)
1505 cur.insert(new MathMacroArgument(n));
1509 // do not finish macro for known * commands
1510 MathWordList const & mwl = mathedWordList();
1511 bool star_macro = c == '*'
1512 && (mwl.find(name.substr(1) + "*") != mwl.end()
1513 || cur.buffer()->getMacro(name.substr(1) + "*", cur, true));
1514 if (isAlphaASCII(c) || star_macro) {
1515 cur.activeMacro()->setName(name + docstring(1, c));
1519 // handle 'special char' macros
1524 if (currentMode() <= InsetMath::TEXT_MODE)
1525 cur.niceInsert(createInsetMath("textbackslash", buf));
1527 cur.niceInsert(createInsetMath("backslash", buf));
1528 } else if (c == '^' && currentMode() == InsetMath::MATH_MODE) {
1530 cur.niceInsert(createInsetMath("mathcircumflex", buf));
1531 } else if (c == '{') {
1533 cur.niceInsert(MathAtom(new InsetMathBrace(buf)));
1534 } else if (c == '%') {
1536 cur.niceInsert(MathAtom(new InsetMathComment(buf)));
1537 } else if (c == '#') {
1538 LASSERT(cur.activeMacro(), /**/);
1539 cur.activeMacro()->setName(name + docstring(1, c));
1542 cur.niceInsert(createInsetMath(docstring(1, c), buf));
1547 // One character big delimiters. The others are handled in
1548 // interpretString().
1549 latexkeys const * l = in_word_set(name.substr(1));
1550 if (name[0] == '\\' && l && l->inset == "big") {
1554 delim = from_ascii("\\{");
1557 delim = from_ascii("\\}");
1560 delim = docstring(1, c);
1563 if (InsetMathBig::isBigInsetDelim(delim)) {
1564 // name + delim ared a valid InsetMathBig.
1565 // We can't use cur.macroModeClose() because
1566 // it does not handle delim.
1567 InsetMathUnknown * p = cur.activeMacro();
1570 cur.cell().erase(cur.pos());
1571 cur.plainInsert(MathAtom(
1572 new InsetMathBig(name.substr(1), delim)));
1577 // leave macro mode and try again if necessary
1578 if (cur.macroModeClose()) {
1579 MathAtom const atom = cur.prevAtom();
1580 if (atom->asNestInset() && atom->isActive()) {
1582 cur.pushBackward(*cur.nextInset());
1586 cur.niceInsert(MathAtom(new InsetMathBrace(buf)));
1588 interpretChar(cur, c);
1593 // leave autocorrect mode if necessary
1594 if (lyxrc.autocorrection_math && c == ' ' && cur.autocorrect()) {
1595 cur.autocorrect() = false;
1596 cur.message(_("Autocorrect Off ('!' to enter)"));
1599 if (lyxrc.autocorrection_math && c == '!' && !cur.autocorrect()) {
1600 cur.autocorrect() = true;
1601 cur.message(_("Autocorrect On (<space> to exit)"));
1605 // just clear selection on pressing the space bar
1606 if (cur.selection() && c == ' ') {
1607 cur.setSelection(false);
1612 //lyxerr << "starting with macro" << endl;
1613 bool reduced = cap::reduceSelectionToOneCell(cur);
1614 if (reduced || !cur.selection()) {
1615 docstring const safe = cap::grabAndEraseSelection(cur);
1616 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), safe, false)));
1624 if (currentMode() <= InsetMath::TEXT_MODE)
1630 if (currentMode() <= InsetMath::TEXT_MODE) {
1631 // insert spaces in text or undecided mode,
1632 // but suppress direct insertion of two spaces in a row
1633 // the still allows typing '<space>a<space>' and deleting the 'a', but
1634 // it is better than nothing...
1635 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1637 // FIXME: we have to enable full redraw here because of the
1638 // visual box corners that define the inset. If we know for
1639 // sure that we stay within the same cell we can optimize for
1641 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1645 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1646 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1647 // FIXME: we have to enable full redraw here because of the
1648 // visual box corners that define the inset. If we know for
1649 // sure that we stay within the same cell we can optimize for
1651 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1655 if (cur.popForward()) {
1656 // FIXME: we have to enable full redraw here because of the
1657 // visual box corners that define the inset. If we know for
1658 // sure that we stay within the same cell we can optimize for
1660 //cur.updateFlags(Update::FitCursor);
1664 // if we are at the very end, leave the formula
1665 return cur.pos() != cur.lastpos();
1668 // These should be treated differently when not in text mode:
1669 if (currentMode() != InsetMath::TEXT_MODE) {
1671 script(cur, false, save_selection);
1675 script(cur, true, save_selection);
1679 cur.niceInsert(createInsetMath("sim", buf));
1682 if (currentMode() == InsetMath::MATH_MODE && !isAsciiOrMathAlpha(c)) {
1683 MathAtom at = createInsetMath("text", buf);
1684 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
1691 cur.niceInsert(createInsetMath("textasciicircum", buf));
1695 cur.niceInsert(createInsetMath("textasciitilde", buf));
1700 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1701 c == '%' || c == '_') {
1702 cur.niceInsert(createInsetMath(docstring(1, c), buf));
1707 // try auto-correction
1708 if (lyxrc.autocorrection_math && cur.autocorrect() && cur.pos() != 0
1709 && math_autocorrect(cur.prevAtom(), c))
1712 // no special circumstances, so insert the character without any fuss
1714 if (lyxrc.autocorrection_math) {
1715 if (!cur.autocorrect())
1716 cur.message(_("Autocorrect Off ('!' to enter)"));
1718 cur.message(_("Autocorrect On (<space> to exit)"));
1724 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1726 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1728 if (!cur.empty() && cur.pos() > 0 &&
1729 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1730 if (InsetMathBig::isBigInsetDelim(str)) {
1731 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1732 if (prev[0] == '\\') {
1733 prev = prev.substr(1);
1734 latexkeys const * l = in_word_set(prev);
1735 if (l && l->inset == "big") {
1736 cur.cell()[cur.pos() - 1] =
1737 MathAtom(new InsetMathBig(prev, str));
1747 bool InsetMathNest::script(Cursor & cur, bool up)
1749 return script(cur, up, docstring());
1753 bool InsetMathNest::script(Cursor & cur, bool up,
1754 docstring const & save_selection)
1756 // Hack to get \^ and \_ working
1757 //lyxerr << "handling script: up: " << up << endl;
1758 if (cur.inMacroMode() && cur.macroName() == "\\") {
1760 cur.niceInsert(createInsetMath("mathcircumflex", cur.buffer()));
1762 interpretChar(cur, '_');
1766 cur.macroModeClose();
1767 if (asScriptInset() && cur.idx() == 0) {
1768 // we are in a nucleus of a script inset, move to _our_ script
1769 InsetMathScript * inset = asScriptInset();
1770 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1772 cur.idx() = inset->idxOfScript(up);
1774 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1776 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1779 cur.idx() = inset->idxOfScript(up);
1780 cur.pos() = cur.lastpos();
1782 // convert the thing to our left to a scriptinset or create a new
1783 // one if in the very first position of the array
1784 if (cur.pos() == 0) {
1785 //lyxerr << "new scriptinset" << endl;
1786 cur.insert(new InsetMathScript(buffer_, up));
1788 //lyxerr << "converting prev atom " << endl;
1789 cur.prevAtom() = MathAtom(new InsetMathScript(buffer_, cur.prevAtom(), up));
1792 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1793 // See comment in MathParser.cpp for special handling of {}-bases
1799 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1800 cur.niceInsert(save_selection);
1802 //lyxerr << "inserting selection 2:\n" << save_selection << endl;
1807 bool InsetMathNest::completionSupported(Cursor const & cur) const
1809 return cur.inMacroMode();
1813 bool InsetMathNest::inlineCompletionSupported(Cursor const & cur) const
1815 return cur.inMacroMode();
1819 bool InsetMathNest::automaticInlineCompletion() const
1821 return lyxrc.completion_inline_math;
1825 bool InsetMathNest::automaticPopupCompletion() const
1827 return lyxrc.completion_popup_math;
1831 CompletionList const *
1832 InsetMathNest::createCompletionList(Cursor const & cur) const
1834 if (!cur.inMacroMode())
1837 return new MathCompletionList(cur);
1841 docstring InsetMathNest::completionPrefix(Cursor const & cur) const
1843 if (!cur.inMacroMode())
1846 return cur.activeMacro()->name();
1850 bool InsetMathNest::insertCompletion(Cursor & cur, docstring const & s,
1853 if (!cur.inMacroMode())
1856 // append completion to active macro
1857 InsetMathUnknown * inset = cur.activeMacro();
1858 inset->setName(inset->name() + s);
1863 // FIXME: this creates duplicates in the completion popup
1864 // which looks ugly. Moreover the changes the list lengths
1866 confuse the popup as well.
1867 MathCompletionList::addToFavorites(inset->name());
1869 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, " "));
1876 void InsetMathNest::completionPosAndDim(Cursor const & cur, int & x, int & y,
1877 Dimension & dim) const
1879 Inset const * inset = cur.activeMacro();
1883 // get inset dimensions
1884 dim = cur.bv().coordCache().insets().dim(inset);
1885 // FIXME: these 3 are no accurate, but should depend on the font.
1886 // Now the popup jumps down if you enter a char with descent > 0.
1892 = cur.bv().coordCache().insets().xy(inset);
1898 bool InsetMathNest::cursorMathForward(Cursor & cur)
1900 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
1901 cur.pushBackward(*cur.nextAtom().nucleus());
1902 cur.inset().idxFirst(cur);
1905 if (cur.posForward() || idxForward(cur))
1907 // try to pop forwards --- but don't pop out of math! leave that to
1909 int s = cur.depth() - 2;
1910 if (s >= 0 && cur[s].inset().asInsetMath())
1911 return cur.popForward();
1916 bool InsetMathNest::cursorMathBackward(Cursor & cur)
1918 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
1920 cur.push(*cur.nextAtom().nucleus());
1921 cur.inset().idxLast(cur);
1924 if (cur.posBackward() || idxBackward(cur))
1926 // try to pop backwards --- but don't pop out of math! leave that to
1928 int s = cur.depth() - 2;
1929 if (s >= 0 && cur[s].inset().asInsetMath())
1930 return cur.popBackward();
1935 ////////////////////////////////////////////////////////////////////
1937 MathCompletionList::MathCompletionList(Cursor const & cur)
1939 // fill it with macros from the buffer
1940 MacroNameSet macros;
1941 cur.buffer()->listMacroNames(macros);
1942 MacroNameSet::const_iterator it;
1943 for (it = macros.begin(); it != macros.end(); ++it) {
1944 if (cur.buffer()->getMacro(*it, cur, false))
1945 locals.push_back("\\" + *it);
1947 sort(locals.begin(), locals.end());
1949 if (globals.size() > 0)
1952 // fill in global macros
1954 MacroTable::globalMacros().getMacroNames(macros);
1955 //lyxerr << "Globals completion macros: ";
1956 for (it = macros.begin(); it != macros.end(); ++it) {
1957 //lyxerr << "\\" + *it << " ";
1958 globals.push_back("\\" + *it);
1960 //lyxerr << std::endl;
1962 // fill in global commands
1963 globals.push_back(from_ascii("\\boxed"));
1964 globals.push_back(from_ascii("\\fbox"));
1965 globals.push_back(from_ascii("\\framebox"));
1966 globals.push_back(from_ascii("\\makebox"));
1967 globals.push_back(from_ascii("\\kern"));
1968 globals.push_back(from_ascii("\\xrightarrow"));
1969 globals.push_back(from_ascii("\\xleftarrow"));
1970 globals.push_back(from_ascii("\\split"));
1971 globals.push_back(from_ascii("\\gathered"));
1972 globals.push_back(from_ascii("\\aligned"));
1973 globals.push_back(from_ascii("\\alignedat"));
1974 globals.push_back(from_ascii("\\cases"));
1975 globals.push_back(from_ascii("\\substack"));
1976 globals.push_back(from_ascii("\\xymatrix"));
1977 globals.push_back(from_ascii("\\subarray"));
1978 globals.push_back(from_ascii("\\array"));
1979 globals.push_back(from_ascii("\\sqrt"));
1980 globals.push_back(from_ascii("\\root"));
1981 globals.push_back(from_ascii("\\tabular"));
1982 globals.push_back(from_ascii("\\stackrel"));
1983 globals.push_back(from_ascii("\\binom"));
1984 globals.push_back(from_ascii("\\choose"));
1985 globals.push_back(from_ascii("\\brace"));
1986 globals.push_back(from_ascii("\\brack"));
1987 globals.push_back(from_ascii("\\frac"));
1988 globals.push_back(from_ascii("\\over"));
1989 globals.push_back(from_ascii("\\nicefrac"));
1990 globals.push_back(from_ascii("\\unitfrac"));
1991 globals.push_back(from_ascii("\\unitfracthree"));
1992 globals.push_back(from_ascii("\\unitone"));
1993 globals.push_back(from_ascii("\\unittwo"));
1994 globals.push_back(from_ascii("\\infer"));
1995 globals.push_back(from_ascii("\\atop"));
1996 globals.push_back(from_ascii("\\lefteqn"));
1997 globals.push_back(from_ascii("\\boldsymbol"));
1998 globals.push_back(from_ascii("\\bm"));
1999 globals.push_back(from_ascii("\\color"));
2000 globals.push_back(from_ascii("\\normalcolor"));
2001 globals.push_back(from_ascii("\\textcolor"));
2002 globals.push_back(from_ascii("\\cfrac"));
2003 globals.push_back(from_ascii("\\cfracleft"));
2004 globals.push_back(from_ascii("\\cfracright"));
2005 globals.push_back(from_ascii("\\dfrac"));
2006 globals.push_back(from_ascii("\\tfrac"));
2007 globals.push_back(from_ascii("\\dbinom"));
2008 globals.push_back(from_ascii("\\tbinom"));
2009 globals.push_back(from_ascii("\\hphantom"));
2010 globals.push_back(from_ascii("\\phantom"));
2011 globals.push_back(from_ascii("\\vphantom"));
2012 MathWordList const & words = mathedWordList();
2013 MathWordList::const_iterator it2;
2014 //lyxerr << "Globals completion commands: ";
2015 for (it2 = words.begin(); it2 != words.end(); ++it2) {
2016 globals.push_back("\\" + (*it2).first);
2017 //lyxerr << "\\" + (*it2).first << " ";
2019 //lyxerr << std::endl;
2020 sort(globals.begin(), globals.end());
2024 MathCompletionList::~MathCompletionList()
2029 size_type MathCompletionList::size() const
2031 return locals.size() + globals.size();
2035 docstring const & MathCompletionList::data(size_t idx) const
2037 size_t lsize = locals.size();
2039 return globals[idx - lsize];
2045 std::string MathCompletionList::icon(size_t idx) const
2047 // get the latex command
2049 size_t lsize = locals.size();
2051 cmd = globals[idx - lsize];
2055 // get the icon resource name by stripping the backslash
2056 return "images/math/" + to_utf8(cmd.substr(1)) + ".png";
2059 std::vector<docstring> MathCompletionList::globals;