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)
89 InsetMathNest::InsetMathNest(InsetMathNest const & inset)
90 : InsetMath(inset), cells_(inset.cells_), lock_(inset.lock_)
94 InsetMathNest::~InsetMathNest()
96 map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
97 map<BufferView const *, bool>::iterator end = mouse_hover_.end();
98 for (; it != end; ++it)
100 it->first->clearLastInset(this);
104 InsetMathNest & InsetMathNest::operator=(InsetMathNest const & inset)
106 cells_ = inset.cells_;
108 mouse_hover_.clear();
109 InsetMath::operator=(inset);
114 void InsetMathNest::setBuffer(Buffer & buffer)
116 InsetMath::setBuffer(buffer);
117 for (idx_type i = 0, n = nargs(); i != n; ++i) {
118 MathData & data = cell(i);
119 for (size_t j = 0; j != data.size(); ++j)
120 data[j].nucleus()->setBuffer(buffer);
125 InsetMath::idx_type InsetMathNest::nargs() const
127 return cells_.size();
131 void InsetMathNest::cursorPos(BufferView const & bv,
132 CursorSlice const & sl, bool /*boundary*/,
133 int & x, int & y) const
135 // FIXME: This is a hack. Ideally, the coord cache should not store
136 // absolute positions, but relative ones. This would mean to call
137 // setXY() not in MathData::draw(), but in the parent insets' draw()
138 // with the correctly adjusted x,y values. But this means that we'd have
139 // to touch all (math)inset's draw() methods. Right now, we'll store
140 // absolute value, and make them here relative, only to make them
141 // absolute again when actually drawing the cursor. What a mess.
142 LASSERT(&sl.inset() == this, /**/);
143 MathData const & ar = sl.cell();
144 CoordCache const & coord_cache = bv.coordCache();
145 if (!coord_cache.getArrays().has(&ar)) {
146 // this can (semi-)legally happen if we just created this cell
147 // and it never has been drawn before. So don't ASSERT.
148 //lyxerr << "no cached data for array " << &ar << endl;
153 Point const pt = coord_cache.getArrays().xy(&ar);
154 if (!coord_cache.getInsets().has(this)) {
156 //lyxerr << "no cached data for inset " << this << endl;
161 Point const pt2 = coord_cache.getInsets().xy(this);
162 //lyxerr << "retrieving position cache for MathData "
163 // << pt.x_ << ' ' << pt.y_ << endl;
164 x = pt.x_ - pt2.x_ + ar.pos2x(&bv, sl.pos());
166 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
167 // << " asc: " << ascent() << " des: " << descent()
168 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
169 // move cursor visually into empty cells ("blue rectangles");
175 void InsetMathNest::metrics(MetricsInfo const & mi) const
178 for (idx_type i = 0, n = nargs(); i != n; ++i) {
180 cell(i).metrics(m, dim);
185 void InsetMathNest::updateBuffer(ParIterator const & it, UpdateType utype)
187 for (idx_type i = 0, n = nargs(); i != n; ++i)
188 cell(i).updateBuffer(it, utype);
193 bool InsetMathNest::idxNext(Cursor & cur) const
195 LASSERT(&cur.inset() == this, /**/);
196 if (cur.idx() == cur.lastidx())
204 bool InsetMathNest::idxForward(Cursor & cur) const
210 bool InsetMathNest::idxPrev(Cursor & cur) const
212 LASSERT(&cur.inset() == this, /**/);
216 cur.pos() = cur.lastpos();
221 bool InsetMathNest::idxBackward(Cursor & cur) const
227 bool InsetMathNest::idxFirst(Cursor & cur) const
229 LASSERT(&cur.inset() == this, /**/);
238 bool InsetMathNest::idxLast(Cursor & cur) const
240 LASSERT(&cur.inset() == this, /**/);
243 cur.idx() = cur.lastidx();
244 cur.pos() = cur.lastpos();
249 void InsetMathNest::dump() const
251 odocstringstream oss;
253 os << "---------------------------------------------\n";
256 for (idx_type i = 0, n = nargs(); i != n; ++i)
257 os << cell(i) << "\n";
258 os << "---------------------------------------------\n";
259 lyxerr << to_utf8(oss.str());
263 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
267 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
270 setPosCache(pi, x, y);
274 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
276 BufferView & bv = *pi.base.bv;
277 // this should use the x/y values given, not the cached values
278 Cursor & cur = bv.cursor();
279 if (!cur.selection())
281 if (&cur.inset() != this)
284 // FIXME: hack to get position cache warm
285 bool const original_drawing_state = pi.pain.isDrawingEnabled();
286 pi.pain.setDrawingEnabled(false);
288 pi.pain.setDrawingEnabled(original_drawing_state);
290 CursorSlice s1 = cur.selBegin();
291 CursorSlice s2 = cur.selEnd();
293 //lyxerr << "InsetMathNest::drawing selection: "
294 // << " s1: " << s1 << " s2: " << s2 << endl;
295 if (s1.idx() == s2.idx()) {
296 MathData const & c = cell(s1.idx());
297 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
298 int x1 = g.pos.x_ + c.pos2x(pi.base.bv, s1.pos());
299 int y1 = g.pos.y_ - g.dim.ascent();
300 int x2 = g.pos.x_ + c.pos2x(pi.base.bv, s2.pos());
301 int y2 = g.pos.y_ + g.dim.descent();
302 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
303 //lyxerr << "InsetMathNest::drawing selection 3: "
304 // << " x1: " << x1 << " x2: " << x2
305 // << " y1: " << y1 << " y2: " << y2 << endl;
307 for (idx_type i = 0; i < nargs(); ++i) {
308 if (idxBetween(i, s1.idx(), s2.idx())) {
309 MathData const & c = cell(i);
310 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
312 int y1 = g.pos.y_ - g.dim.ascent();
313 int x2 = g.pos.x_ + g.dim.width();
314 int y2 = g.pos.y_ + g.dim.descent();
315 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
322 void InsetMathNest::validate(LaTeXFeatures & features) const
324 for (idx_type i = 0; i < nargs(); ++i)
325 cell(i).validate(features);
329 void InsetMathNest::replace(ReplaceData & rep)
331 for (idx_type i = 0; i < nargs(); ++i)
332 cell(i).replace(rep);
336 bool InsetMathNest::contains(MathData const & ar) const
338 for (idx_type i = 0; i < nargs(); ++i)
339 if (cell(i).contains(ar))
345 bool InsetMathNest::lock() const
351 void InsetMathNest::lock(bool l)
357 bool InsetMathNest::isActive() const
363 MathData InsetMathNest::glue() const
366 for (size_t i = 0; i < nargs(); ++i)
372 void InsetMathNest::write(WriteStream & os) const
374 ModeSpecifier specifier(os, currentMode(), lockedMode());
375 docstring const latex_name = name();
376 os << '\\' << latex_name;
377 for (size_t i = 0; i < nargs(); ++i)
378 os << '{' << cell(i) << '}';
380 os.pendingSpace(true);
381 if (lock_ && !os.latex()) {
383 os.pendingSpace(true);
388 void InsetMathNest::normalize(NormalStream & os) const
391 for (size_t i = 0; i < nargs(); ++i)
392 os << ' ' << cell(i);
397 int InsetMathNest::latex(odocstream & os, OutputParams const & runparams) const
399 WriteStream wi(os, runparams.moving_arg, true,
400 runparams.dryrun ? WriteStream::wsDryrun : WriteStream::wsDefault,
407 bool InsetMathNest::setMouseHover(BufferView const * bv, bool mouse_hover)
409 mouse_hover_[bv] = mouse_hover;
414 bool InsetMathNest::notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
418 MathData & ar = cur.cell();
419 // remove base-only "scripts"
420 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
421 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
422 if (p && p->nargs() == 1) {
423 MathData ar = p->nuc();
426 cur.adjust(i, ar.size() - 1);
430 // glue adjacent font insets of the same kind
431 for (pos_type i = 0; i + 1 < size(); ++i) {
432 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
433 InsetMathFont const * q = operator[](i + 1)->asFontInset();
434 if (p && q && p->name() == q->name()) {
435 p->cell(0).append(q->cell(0));
445 void InsetMathNest::handleFont
446 (Cursor & cur, docstring const & arg, char const * const font)
448 handleFont(cur, arg, from_ascii(font));
452 void InsetMathNest::handleFont(Cursor & cur, docstring const & arg,
453 docstring const & font)
455 cur.recordUndoSelection();
457 // this whole function is a hack and won't work for incremental font
459 if (cur.inset().asInsetMath()->name() == font)
460 cur.handleFont(to_utf8(font));
462 handleNest(cur, createInsetMath(font, cur.buffer()), arg);
466 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest)
468 handleNest(cur, nest, docstring());
472 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest,
473 docstring const & arg)
475 CursorSlice i1 = cur.selBegin();
476 CursorSlice i2 = cur.selEnd();
477 if (!i1.inset().asInsetMath())
479 if (i1.idx() == i2.idx()) {
480 // the easy case where only one cell is selected
481 cur.handleNest(nest);
486 // multiple selected cells in a simple non-grid inset
487 if (i1.asInsetMath()->nrows() == 0 || i1.asInsetMath()->ncols() == 0) {
488 for (idx_type i = i1.idx(); i <= i2.idx(); ++i) {
493 cur.pos() = cur.lastpos();
496 // change font of cell
497 cur.handleNest(nest);
500 // cur is in the font inset now. If the loop continues,
501 // we need to get outside again for the next cell
502 if (i + 1 <= i2.idx())
508 // the complicated case with multiple selected cells in a grid
511 cap::region(i1, i2, r1, r2, c1, c2);
512 for (row_type row = r1; row <= r2; ++row) {
513 for (col_type col = c1; col <= c2; ++col) {
515 cur.idx() = i1.asInsetMath()->index(row, col);
518 cur.pos() = cur.lastpos();
522 cur.handleNest(nest);
525 // cur is in the font inset now. If the loop continues,
526 // we need to get outside again for the next cell
527 if (col + 1 <= c2 || row + 1 <= r2)
534 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
536 cur.recordUndoSelection();
539 font.fromString(to_utf8(arg), b);
540 if (font.fontInfo().color() != Color_inherit &&
541 font.fontInfo().color() != Color_ignore)
542 handleNest(cur, MathAtom(new InsetMathColor(buffer_, true, font.fontInfo().color())));
544 // FIXME: support other font changes here as well?
548 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
550 //LYXERR0("InsetMathNest: request: " << cmd);
552 Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
554 FuncCode const act = cmd.action();
557 case LFUN_CLIPBOARD_PASTE:
558 parseflg |= Parse::VERBATIM;
561 if (cur.currentMode() <= TEXT_MODE)
562 parseflg |= Parse::TEXTMODE;
563 cur.recordUndoSelection();
564 cur.message(_("Paste"));
565 replaceSelection(cur);
567 if (cmd.argument().empty() && !theClipboard().isInternal())
568 topaste = theClipboard().getAsText();
571 idocstringstream is(cmd.argument());
573 topaste = cap::selection(n);
575 cur.niceInsert(topaste, parseflg, false);
576 cur.clearSelection(); // bug 393
577 cur.forceBufferUpdate();
584 cutSelection(cur, true, true);
585 cur.message(_("Cut"));
586 // Prevent stale position >= size crash
587 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
589 cur.forceBufferUpdate();
594 cur.message(_("Copy"));
597 case LFUN_MOUSE_PRESS:
598 lfunMousePress(cur, cmd);
601 case LFUN_MOUSE_MOTION:
602 lfunMouseMotion(cur, cmd);
605 case LFUN_MOUSE_RELEASE:
606 lfunMouseRelease(cur, cmd);
609 case LFUN_FINISHED_LEFT: // in math, left is backwards
610 case LFUN_FINISHED_BACKWARD:
611 cur.bv().cursor() = cur;
614 case LFUN_FINISHED_RIGHT: // in math, right is forward
615 case LFUN_FINISHED_FORWARD:
617 cur.bv().cursor() = cur;
620 case LFUN_CHAR_RIGHT:
622 case LFUN_CHAR_BACKWARD:
623 case LFUN_CHAR_FORWARD:
624 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
625 case LFUN_CHAR_RIGHT_SELECT:
626 case LFUN_CHAR_LEFT_SELECT:
627 case LFUN_CHAR_BACKWARD_SELECT:
628 case LFUN_CHAR_FORWARD_SELECT: {
629 // are we in a selection?
630 bool select = (act == LFUN_CHAR_RIGHT_SELECT
631 || act == LFUN_CHAR_LEFT_SELECT
632 || act == LFUN_CHAR_BACKWARD_SELECT
633 || act == LFUN_CHAR_FORWARD_SELECT);
634 // are we moving forward or backwards?
635 // If the command was RIGHT or LEFT, then whether we're moving forward
636 // or backwards depends on the cursor movement mode (logical or visual):
637 // * in visual mode, since math is always LTR, right -> forward,
639 // * in logical mode, the mapping is determined by the
640 // reverseDirectionNeeded() function
643 FuncCode finish_lfun;
645 if (act == LFUN_CHAR_FORWARD
646 || act == LFUN_CHAR_FORWARD_SELECT) {
648 finish_lfun = LFUN_FINISHED_FORWARD;
650 else if (act == LFUN_CHAR_BACKWARD
651 || act == LFUN_CHAR_BACKWARD_SELECT) {
653 finish_lfun = LFUN_FINISHED_BACKWARD;
656 bool right = (act == LFUN_CHAR_RIGHT_SELECT
657 || act == LFUN_CHAR_RIGHT);
658 if (lyxrc.visual_cursor || !reverseDirectionNeeded(cur))
664 finish_lfun = LFUN_FINISHED_RIGHT;
666 finish_lfun = LFUN_FINISHED_LEFT;
668 // Now that we know exactly what we want to do, let's do it!
669 cur.selHandle(select);
671 cur.macroModeClose();
672 // try moving forward or backwards as necessary...
673 if (!(forward ? cursorMathForward(cur) : cursorMathBackward(cur))) {
674 // ... and if movement failed, then finish forward or backwards
676 cmd = FuncRequest(finish_lfun);
684 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
685 case LFUN_DOWN_SELECT:
686 case LFUN_UP_SELECT: {
687 // close active macro
688 if (cur.inMacroMode()) {
689 cur.macroModeClose();
693 // stop/start the selection
694 bool select = act == LFUN_DOWN_SELECT ||
695 act == LFUN_UP_SELECT;
696 cur.selHandle(select);
699 bool up = act == LFUN_UP || act == LFUN_UP_SELECT;
700 bool successful = cur.upDownInMath(up);
704 if (cur.fixIfBroken())
705 // FIXME: Something bad happened. We pass the corrected Cursor
706 // instead of letting things go worse.
709 // We did not manage to move the cursor.
714 case LFUN_MOUSE_DOUBLE:
715 case LFUN_MOUSE_TRIPLE:
716 case LFUN_WORD_SELECT:
720 cur.setSelection(true);
721 cur.pos() = cur.lastpos();
722 cur.idx() = cur.lastidx();
725 case LFUN_PARAGRAPH_UP:
726 case LFUN_PARAGRAPH_DOWN:
727 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
728 case LFUN_PARAGRAPH_UP_SELECT:
729 case LFUN_PARAGRAPH_DOWN_SELECT:
732 case LFUN_LINE_BEGIN:
733 case LFUN_WORD_BACKWARD:
735 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
736 case LFUN_LINE_BEGIN_SELECT:
737 case LFUN_WORD_BACKWARD_SELECT:
738 case LFUN_WORD_LEFT_SELECT:
739 cur.selHandle(act == LFUN_WORD_BACKWARD_SELECT ||
740 act == LFUN_WORD_LEFT_SELECT ||
741 act == LFUN_LINE_BEGIN_SELECT);
742 cur.macroModeClose();
743 if (cur.pos() != 0) {
745 } else if (cur.col() != 0) {
746 cur.idx() -= cur.col();
748 } else if (cur.idx() != 0) {
752 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
757 case LFUN_WORD_FORWARD:
758 case LFUN_WORD_RIGHT:
760 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
761 case LFUN_WORD_FORWARD_SELECT:
762 case LFUN_WORD_RIGHT_SELECT:
763 case LFUN_LINE_END_SELECT:
764 cur.selHandle(act == LFUN_WORD_FORWARD_SELECT ||
765 act == LFUN_WORD_RIGHT_SELECT ||
766 act == LFUN_LINE_END_SELECT);
767 cur.macroModeClose();
769 if (cur.pos() != cur.lastpos()) {
770 cur.pos() = cur.lastpos();
771 } else if (ncols() && (cur.col() != cur.lastcol())) {
772 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
773 cur.pos() = cur.lastpos();
774 } else if (cur.idx() != cur.lastidx()) {
775 cur.idx() = cur.lastidx();
776 cur.pos() = cur.lastpos();
778 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
783 case LFUN_CELL_FORWARD:
784 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
785 cur.inset().idxNext(cur);
788 case LFUN_CELL_BACKWARD:
789 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
790 cur.inset().idxPrev(cur);
793 case LFUN_WORD_DELETE_BACKWARD:
794 case LFUN_CHAR_DELETE_BACKWARD:
796 // May affect external cell:
797 cur.recordUndoInset();
799 cur.recordUndoSelection();
800 // if the inset can not be removed from within, delete it
801 if (!cur.backspace()) {
802 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
803 cur.innerText()->dispatch(cur, cmd);
807 case LFUN_WORD_DELETE_FORWARD:
808 case LFUN_CHAR_DELETE_FORWARD:
809 if (cur.pos() == cur.lastpos())
810 // May affect external cell:
811 cur.recordUndoInset();
813 cur.recordUndoSelection();
814 // if the inset can not be removed from within, delete it
816 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
817 cur.innerText()->dispatch(cur, cmd);
823 cur.clearSelection();
825 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
830 // 'Locks' the math inset. A 'locked' math inset behaves as a unit
831 // that is traversed by a single <CursorLeft>/<CursorRight>.
832 case LFUN_INSET_TOGGLE:
838 case LFUN_SELF_INSERT:
839 if (cmd.argument().size() != 1) {
840 cur.recordUndoSelection();
841 docstring const arg = cmd.argument();
842 if (!interpretString(cur, arg))
846 // Don't record undo steps if we are in macro mode and thus
847 // cmd.argument is the next character of the macro name.
848 // Otherwise we'll get an invalid cursor if we undo after
849 // the macro was finished and the macro is a known command,
850 // e.g. sqrt. Cursor::macroModeClose replaces in this case
851 // the InsetMathUnknown with name "frac" by an empty
852 // InsetMathFrac -> a pos value > 0 is invalid.
853 // A side effect is that an undo before the macro is finished
854 // undoes the complete macro, not only the last character.
855 // At the time we hit '\' we are not in macro mode, still.
856 if (!cur.inMacroMode())
857 cur.recordUndoSelection();
859 // spacial handling of space. If we insert an inset
860 // via macro mode, we want to put the cursor inside it
861 // if relevant. Think typing "\frac<space>".
862 if (cmd.argument()[0] == ' '
863 && cur.inMacroMode() && cur.macroName() != "\\"
864 && cur.macroModeClose()) {
865 MathAtom const atom = cur.prevAtom();
866 if (atom->asNestInset() && atom->isActive()) {
868 cur.pushBackward(*cur.nextInset());
870 } else if (!interpretChar(cur, cmd.argument()[0])) {
871 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
876 //case LFUN_SERVER_GET_XY:
879 case LFUN_SERVER_SET_XY: {
880 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
883 istringstream is(to_utf8(cmd.argument()));
885 cur.setScreenPos(x, y);
889 // Special casing for superscript in case of LyX handling
891 case LFUN_ACCENT_CIRCUMFLEX:
892 if (cmd.argument().empty()) {
893 // do superscript if LyX handles
895 cur.recordUndoSelection();
896 script(cur, true, grabAndEraseSelection(cur));
900 case LFUN_ACCENT_UMLAUT:
901 case LFUN_ACCENT_ACUTE:
902 case LFUN_ACCENT_GRAVE:
903 case LFUN_ACCENT_BREVE:
904 case LFUN_ACCENT_DOT:
905 case LFUN_ACCENT_MACRON:
906 case LFUN_ACCENT_CARON:
907 case LFUN_ACCENT_TILDE:
908 case LFUN_ACCENT_CEDILLA:
909 case LFUN_ACCENT_CIRCLE:
910 case LFUN_ACCENT_UNDERDOT:
911 case LFUN_ACCENT_TIE:
912 case LFUN_ACCENT_OGONEK:
913 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
917 case LFUN_TEXTSTYLE_APPLY:
918 case LFUN_TEXTSTYLE_UPDATE:
919 handleFont2(cur, cmd.argument());
923 if (currentMode() <= TEXT_MODE)
924 handleFont(cur, cmd.argument(), "textbf");
926 handleFont(cur, cmd.argument(), "mathbf");
928 case LFUN_FONT_BOLDSYMBOL:
929 if (currentMode() <= TEXT_MODE)
930 handleFont(cur, cmd.argument(), "textbf");
932 handleFont(cur, cmd.argument(), "boldsymbol");
935 if (currentMode() <= TEXT_MODE)
936 handleFont(cur, cmd.argument(), "textsf");
938 handleFont(cur, cmd.argument(), "mathsf");
941 if (currentMode() <= TEXT_MODE)
942 handleFont(cur, cmd.argument(), "emph");
944 handleFont(cur, cmd.argument(), "mathcal");
946 case LFUN_FONT_ROMAN:
947 if (currentMode() <= TEXT_MODE)
948 handleFont(cur, cmd.argument(), "textrm");
950 handleFont(cur, cmd.argument(), "mathrm");
952 case LFUN_FONT_TYPEWRITER:
953 if (currentMode() <= TEXT_MODE)
954 handleFont(cur, cmd.argument(), "texttt");
956 handleFont(cur, cmd.argument(), "mathtt");
959 handleFont(cur, cmd.argument(), "mathfrak");
962 if (currentMode() <= TEXT_MODE)
963 handleFont(cur, cmd.argument(), "textit");
965 handleFont(cur, cmd.argument(), "mathit");
968 if (currentMode() <= TEXT_MODE)
969 // FIXME: should be "noun"
970 handleFont(cur, cmd.argument(), "textsc");
972 handleFont(cur, cmd.argument(), "mathbb");
974 case LFUN_FONT_DEFAULT:
975 handleFont(cur, cmd.argument(), "textnormal");
978 case LFUN_FONT_UNDERLINE:
980 cur.handleNest(createInsetMath("underline", cur.buffer()));
982 case LFUN_MATH_MODE: {
984 // ignore math-mode on when already in math mode
985 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
987 cur.recordUndoSelection();
988 cur.macroModeClose();
989 docstring const save_selection = grabAndEraseSelection(cur);
991 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
992 if (currentMode() <= Inset::TEXT_MODE)
993 cur.plainInsert(MathAtom(new InsetMathEnsureMath(buffer_)));
995 cur.plainInsert(MathAtom(new InsetMathBox(buffer_, from_ascii("mbox"))));
997 cur.pushBackward(*cur.nextInset());
998 cur.niceInsert(save_selection);
999 cur.forceBufferUpdate();
1001 if (currentMode() == Inset::TEXT_MODE) {
1002 cur.recordUndoSelection();
1003 cur.niceInsert(MathAtom(new InsetMathHull("simple", cur.buffer())));
1004 cur.message(_("create new math text environment ($...$)"));
1006 handleFont(cur, cmd.argument(), "textrm");
1007 cur.message(_("entered math text mode (textrm)"));
1013 case LFUN_REGEXP_MODE: {
1014 InsetMathHull * i = dynamic_cast<InsetMathHull *>(cur.inset().asInsetMath());
1015 if (i && i->getType() == hullRegexp) {
1016 cur.message(_("Already in regular expression mode"));
1019 cur.macroModeClose();
1020 docstring const save_selection = grabAndEraseSelection(cur);
1022 cur.plainInsert(MathAtom(new InsetMathHull(buffer_, hullRegexp)));
1024 cur.pushBackward(*cur.nextInset());
1025 cur.niceInsert(save_selection);
1026 cur.message(_("Regular expression editor mode"));
1030 case LFUN_MATH_FONT_STYLE: {
1031 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, '\\' + cmd.argument());
1032 doDispatch(cur, fr);
1036 case LFUN_MATH_SIZE: {
1037 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, cmd.argument());
1038 doDispatch(cur, fr);
1042 case LFUN_MATH_MATRIX: {
1048 idocstringstream is(cmd.argument());
1049 is >> m >> n >> v_align >> h_align;
1055 cur.niceInsert(MathAtom(new InsetMathArray(buffer_,
1056 from_ascii("array"), m, n, (char)v_align[0], h_align)));
1060 case LFUN_MATH_AMS_MATRIX: {
1065 idocstringstream is(cmd.argument());
1066 is >> m >> n >> name;
1072 MathAtom(new InsetMathAMSArray(buffer_, name, m, n)));
1076 case LFUN_MATH_DELIM: {
1078 docstring rs = split(cmd.argument(), ls, ' ');
1079 // Reasonable default values
1085 cur.handleNest(MathAtom(new InsetMathDelim(buffer_, ls, rs)));
1089 case LFUN_MATH_BIGDELIM: {
1090 docstring const lname = from_utf8(cmd.getArg(0));
1091 docstring const ldelim = from_utf8(cmd.getArg(1));
1092 docstring const rname = from_utf8(cmd.getArg(2));
1093 docstring const rdelim = from_utf8(cmd.getArg(3));
1094 latexkeys const * l = in_word_set(lname);
1095 bool const have_l = l && l->inset == "big" &&
1096 InsetMathBig::isBigInsetDelim(ldelim);
1097 l = in_word_set(rname);
1098 bool const have_r = l && l->inset == "big" &&
1099 InsetMathBig::isBigInsetDelim(rdelim);
1100 // We mimic LFUN_MATH_DELIM in case we have an empty left
1101 // or right delimiter.
1102 if (have_l || have_r) {
1104 docstring const selection = grabAndEraseSelection(cur);
1107 cur.insert(MathAtom(new InsetMathBig(lname,
1109 cur.niceInsert(selection);
1111 cur.insert(MathAtom(new InsetMathBig(rname,
1114 // Don't call cur.undispatched() if we did nothing, this would
1115 // lead to infinite recursion via Text::dispatch().
1119 case LFUN_SPACE_INSERT:
1120 cur.recordUndoSelection();
1121 cur.insert(MathAtom(new InsetMathSpace));
1124 case LFUN_MATH_SPACE:
1125 cur.recordUndoSelection();
1126 if (cmd.argument().empty())
1127 cur.insert(MathAtom(new InsetMathSpace));
1129 string const name = cmd.getArg(0);
1130 string const len = cmd.getArg(1);
1131 cur.insert(MathAtom(new InsetMathSpace(name, len)));
1135 case LFUN_ERT_INSERT:
1136 // interpret this as if a backslash was typed
1138 interpretChar(cur, '\\');
1141 case LFUN_MATH_SUBSCRIPT:
1142 // interpret this as if a _ was typed
1143 cur.recordUndoSelection();
1144 interpretChar(cur, '_');
1147 case LFUN_MATH_SUPERSCRIPT:
1148 // interpret this as if a ^ was typed
1149 cur.recordUndoSelection();
1150 interpretChar(cur, '^');
1153 case LFUN_MATH_MACRO_FOLD:
1154 case LFUN_MATH_MACRO_UNFOLD: {
1156 bool fold = act == LFUN_MATH_MACRO_FOLD;
1157 bool found = findMacroToFoldUnfold(it, fold);
1159 MathMacro * macro = it.nextInset()->asInsetMath()->asMacro();
1160 cur.recordUndoInset();
1169 case LFUN_QUOTE_INSERT:
1170 // interpret this as if a straight " was typed
1171 cur.recordUndoSelection();
1172 interpretChar(cur, '\"');
1175 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
1176 // handling such that "self-insert" works on "arbitrary stuff" too, and
1177 // math-insert only handles special math things like "matrix".
1178 case LFUN_MATH_INSERT: {
1179 cur.recordUndoSelection();
1180 if (cmd.argument() == "^" || cmd.argument() == "_")
1181 interpretChar(cur, cmd.argument()[0]);
1184 asArray(cmd.argument(), ar);
1185 if (cur.selection() && ar.size() == 1
1186 && ar[0]->asNestInset()
1187 && ar[0]->asNestInset()->nargs() > 1)
1188 handleNest(cur, ar[0]);
1190 cur.niceInsert(cmd.argument());
1195 case LFUN_UNICODE_INSERT: {
1196 if (cmd.argument().empty())
1198 docstring hexstring = cmd.argument();
1199 if (isHex(hexstring)) {
1200 char_type c = hexToInt(hexstring);
1201 if (c >= 32 && c < 0x10ffff) {
1202 docstring s = docstring(1, c);
1203 FuncCode code = currentMode() == MATH_MODE ?
1204 LFUN_MATH_INSERT : LFUN_SELF_INSERT;
1205 lyx::dispatch(FuncRequest(code, s));
1211 case LFUN_DIALOG_SHOW_NEW_INSET: {
1212 docstring const & name = cmd.argument();
1214 if (name == "ref") {
1215 InsetMathRef tmp(buffer_, name);
1216 data = tmp.createDialogStr(to_utf8(name));
1217 cur.bv().showDialog(to_utf8(name), data);
1218 } else if (name == "mathspace") {
1219 cur.bv().showDialog(to_utf8(name));
1224 case LFUN_INSET_INSERT: {
1226 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1227 cur.recordUndoSelection();
1229 cur.forceBufferUpdate();
1234 case LFUN_INSET_DISSOLVE:
1235 if (!asHullInset()) {
1236 cur.recordUndoInset();
1242 InsetMath::doDispatch(cur, cmd);
1248 bool InsetMathNest::findMacroToFoldUnfold(Cursor & it, bool fold) const {
1249 // look for macro to open/close, but stay in mathed
1250 for (; !it.empty(); it.pop_back()) {
1252 // go backward through the current cell
1253 Inset * inset = it.nextInset();
1254 while (inset && inset->asInsetMath()) {
1255 MathMacro * macro = inset->asInsetMath()->asMacro();
1257 // found the an macro to open/close?
1258 if (macro->folded() != fold)
1261 // Wrong folding state.
1262 // If this was the first we see in this slice, look further left,
1264 if (inset != it.nextInset())
1268 // go up if this was the left most position
1274 inset = it.nextInset();
1282 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1283 FuncStatus & flag) const
1285 // the font related toggles
1286 //string tc = "mathnormal";
1288 string const arg = to_utf8(cmd.argument());
1289 switch (cmd.action()) {
1290 case LFUN_INSET_MODIFY:
1291 flag.setEnabled(false);
1294 case LFUN_INSET_MODIFY:
1295 // FIXME: check temporarily disabled
1297 char align = mathcursor::valign();
1298 if (align == '\0') {
1302 if (cmd.argument().empty()) {
1306 if (!contains("tcb", cmd.argument()[0])) {
1310 flag.setOnOff(cmd.argument()[0] == align);
1313 /// We have to handle them since 1.4 blocks all unhandled actions
1314 case LFUN_FONT_ITAL:
1315 case LFUN_FONT_BOLD:
1316 case LFUN_FONT_BOLDSYMBOL:
1317 case LFUN_FONT_SANS:
1318 case LFUN_FONT_EMPH:
1319 case LFUN_FONT_TYPEWRITER:
1320 case LFUN_FONT_NOUN:
1321 case LFUN_FONT_ROMAN:
1322 case LFUN_FONT_DEFAULT:
1323 flag.setEnabled(true);
1326 // we just need to be in math mode to enable that
1327 case LFUN_MATH_SIZE:
1328 case LFUN_MATH_SPACE:
1329 case LFUN_MATH_LIMITS:
1330 case LFUN_MATH_EXTERN:
1331 flag.setEnabled(true);
1334 case LFUN_FONT_UNDERLINE:
1335 case LFUN_FONT_FRAK:
1336 flag.setEnabled(currentMode() != TEXT_MODE);
1339 case LFUN_MATH_FONT_STYLE: {
1340 bool const textarg =
1341 arg == "textbf" || arg == "textsf" ||
1342 arg == "textrm" || arg == "textmd" ||
1343 arg == "textit" || arg == "textsc" ||
1344 arg == "textsl" || arg == "textup" ||
1345 arg == "texttt" || arg == "textbb" ||
1346 arg == "textnormal";
1347 flag.setEnabled(currentMode() != TEXT_MODE || textarg);
1351 case LFUN_MATH_INSERT:
1352 flag.setEnabled(currentMode() != TEXT_MODE);
1355 case LFUN_MATH_AMS_MATRIX:
1356 case LFUN_MATH_MATRIX:
1357 flag.setEnabled(currentMode() == MATH_MODE);
1360 case LFUN_INSET_INSERT: {
1361 // Don't test createMathInset_fromDialogStr(), since
1362 // getStatus is not called with a valid reference and the
1363 // dialog would not be applyable.
1364 string const name = cmd.getArg(0);
1365 flag.setEnabled(name == "ref" || name == "mathspace");
1369 case LFUN_MATH_DELIM:
1370 case LFUN_MATH_BIGDELIM:
1371 // Don't do this with multi-cell selections
1372 flag.setEnabled(cur.selBegin().idx() == cur.selEnd().idx());
1375 case LFUN_MATH_MACRO_FOLD:
1376 case LFUN_MATH_MACRO_UNFOLD: {
1378 bool found = findMacroToFoldUnfold(it, cmd.action() == LFUN_MATH_MACRO_FOLD);
1379 flag.setEnabled(found);
1383 case LFUN_SPECIALCHAR_INSERT:
1384 // FIXME: These would probably make sense in math-text mode
1385 flag.setEnabled(false);
1388 case LFUN_INSET_DISSOLVE:
1389 flag.setEnabled(!asHullInset());
1400 void InsetMathNest::edit(Cursor & cur, bool front, EntryDirection entry_from)
1403 bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_RIGHT ||
1404 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1405 cur.idx() = enter_front ? 0 : cur.lastidx();
1406 cur.pos() = enter_front ? 0 : cur.lastpos();
1408 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1412 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1415 int dist_min = 1000000;
1416 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1417 int const d = cell(i).dist(cur.bv(), x, y);
1423 MathData & ar = cell(idx_min);
1425 cur.idx() = idx_min;
1426 cur.pos() = ar.x2pos(&cur.bv(), x - ar.xo(cur.bv()));
1428 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1429 if (dist_min == 0) {
1431 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1432 if (ar[i]->covers(cur.bv(), x, y))
1433 return ar[i].nucleus()->editXY(cur, x, y);
1439 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1441 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1442 BufferView & bv = cur.bv();
1443 bool do_selection = cmd.button() == mouse_button::button1
1444 && cmd.argument() == "region-select";
1445 bv.mouseSetCursor(cur, do_selection);
1446 if (cmd.button() == mouse_button::button1) {
1447 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1448 // Update the cursor update flags as needed:
1450 // Update::Decoration: tells to update the decoration
1451 // (visual box corners that define
1453 // Update::FitCursor: adjust the screen to the cursor
1454 // position if needed
1455 // cur.result().update(): don't overwrite previously set flags.
1456 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor
1457 | cur.result().update());
1458 } else if (cmd.button() == mouse_button::button2) {
1459 if (cap::selection()) {
1460 // See comment in Text::dispatch why we do this
1461 cap::copySelectionToStack();
1462 cmd = FuncRequest(LFUN_PASTE, "0");
1463 doDispatch(bv.cursor(), cmd);
1466 asArray(theSelection().get(), ar);
1467 bv.cursor().insert(ar);
1473 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1475 // only select with button 1
1476 if (cmd.button() == mouse_button::button1) {
1477 Cursor & bvcur = cur.bv().cursor();
1478 if (bvcur.realAnchor().hasPart(cur)) {
1479 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1480 bvcur.setCursor(cur);
1481 bvcur.setSelection(true);
1482 //lyxerr << "MOTION " << bvcur << endl;
1489 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1491 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1493 if (cmd.button() == mouse_button::button1) {
1494 if (!cur.selection())
1495 cur.noScreenUpdate();
1497 Cursor & bvcur = cur.bv().cursor();
1498 bvcur.setSelection(true);
1507 bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
1509 //lyxerr << "interpret 2: '" << c << "'" << endl;
1510 docstring save_selection;
1511 if (c == '^' || c == '_')
1512 save_selection = grabAndEraseSelection(cur);
1515 Buffer * buf = cur.buffer();
1518 if (cur.inMacroMode()) {
1519 docstring name = cur.macroName();
1521 /// are we currently typing '#1' or '#2' or...?
1522 if (name == "\\#") {
1525 if (n >= 1 && n <= 9)
1526 cur.insert(new MathMacroArgument(n));
1530 // do not finish macro for known * commands
1531 MathWordList const & mwl = mathedWordList();
1532 bool star_macro = c == '*'
1533 && (mwl.find(name.substr(1) + "*") != mwl.end()
1534 || cur.buffer()->getMacro(name.substr(1) + "*", cur, true));
1535 if (isAlphaASCII(c) || star_macro) {
1536 cur.activeMacro()->setName(name + docstring(1, c));
1540 // handle 'special char' macros
1545 if (currentMode() <= InsetMath::TEXT_MODE)
1546 cur.niceInsert(createInsetMath("textbackslash", buf));
1548 cur.niceInsert(createInsetMath("backslash", buf));
1549 } else if (c == '^' && currentMode() == InsetMath::MATH_MODE) {
1551 cur.niceInsert(createInsetMath("mathcircumflex", buf));
1552 } else if (c == '{') {
1554 cur.niceInsert(MathAtom(new InsetMathBrace(buf)));
1555 } else if (c == '%') {
1557 cur.niceInsert(MathAtom(new InsetMathComment(buf)));
1558 } else if (c == '#') {
1559 LASSERT(cur.activeMacro(), /**/);
1560 cur.activeMacro()->setName(name + docstring(1, c));
1563 cur.niceInsert(createInsetMath(docstring(1, c), buf));
1568 // One character big delimiters. The others are handled in
1569 // interpretString().
1570 latexkeys const * l = in_word_set(name.substr(1));
1571 if (name[0] == '\\' && l && l->inset == "big") {
1575 delim = from_ascii("\\{");
1578 delim = from_ascii("\\}");
1581 delim = docstring(1, c);
1584 if (InsetMathBig::isBigInsetDelim(delim)) {
1585 // name + delim ared a valid InsetMathBig.
1586 // We can't use cur.macroModeClose() because
1587 // it does not handle delim.
1588 InsetMathUnknown * p = cur.activeMacro();
1591 cur.cell().erase(cur.pos());
1592 cur.plainInsert(MathAtom(
1593 new InsetMathBig(name.substr(1), delim)));
1598 // leave macro mode and try again if necessary
1599 if (cur.macroModeClose()) {
1600 MathAtom const atom = cur.prevAtom();
1601 if (atom->asNestInset() && atom->isActive()) {
1603 cur.pushBackward(*cur.nextInset());
1607 cur.niceInsert(MathAtom(new InsetMathBrace(buf)));
1609 interpretChar(cur, c);
1614 // leave autocorrect mode if necessary
1615 if (lyxrc.autocorrection_math && c == ' ' && cur.autocorrect()) {
1616 cur.autocorrect() = false;
1617 cur.message(_("Autocorrect Off ('!' to enter)"));
1620 if (lyxrc.autocorrection_math && c == '!' && !cur.autocorrect()) {
1621 cur.autocorrect() = true;
1622 cur.message(_("Autocorrect On (<space> to exit)"));
1626 // just clear selection on pressing the space bar
1627 if (cur.selection() && c == ' ') {
1628 cur.setSelection(false);
1633 //lyxerr << "starting with macro" << endl;
1634 bool reduced = cap::reduceSelectionToOneCell(cur);
1635 if (reduced || !cur.selection()) {
1636 docstring const safe = cap::grabAndEraseSelection(cur);
1637 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), safe, false)));
1645 if (currentMode() <= InsetMath::TEXT_MODE)
1651 if (currentMode() <= InsetMath::TEXT_MODE) {
1652 // insert spaces in text or undecided mode,
1653 // but suppress direct insertion of two spaces in a row
1654 // the still allows typing '<space>a<space>' and deleting the 'a', but
1655 // it is better than nothing...
1656 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1658 // FIXME: we have to enable full redraw here because of the
1659 // visual box corners that define the inset. If we know for
1660 // sure that we stay within the same cell we can optimize for
1662 //cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1666 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1667 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1668 // FIXME: we have to enable full redraw here because of the
1669 // visual box corners that define the inset. If we know for
1670 // sure that we stay within the same cell we can optimize for
1672 //cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1676 if (cur.popForward()) {
1677 // FIXME: we have to enable full redraw here because of the
1678 // visual box corners that define the inset. If we know for
1679 // sure that we stay within the same cell we can optimize for
1681 //cur.screenUpdateFlags(Update::FitCursor);
1685 // if we are at the very end, leave the formula
1686 return cur.pos() != cur.lastpos();
1689 // These should be treated differently when not in text mode:
1690 if (currentMode() != InsetMath::TEXT_MODE) {
1692 script(cur, false, save_selection);
1696 script(cur, true, save_selection);
1700 cur.niceInsert(createInsetMath("sim", buf));
1703 if (currentMode() == InsetMath::MATH_MODE && !isAsciiOrMathAlpha(c)) {
1704 MathAtom at = createInsetMath("text", buf);
1705 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
1712 cur.niceInsert(createInsetMath("textasciicircum", buf));
1716 cur.niceInsert(createInsetMath("textasciitilde", buf));
1721 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1722 c == '%' || c == '_') {
1723 cur.niceInsert(createInsetMath(docstring(1, c), buf));
1728 // try auto-correction
1729 if (lyxrc.autocorrection_math && cur.autocorrect() && cur.pos() != 0
1730 && math_autocorrect(cur.prevAtom(), c))
1733 // no special circumstances, so insert the character without any fuss
1735 if (lyxrc.autocorrection_math) {
1736 if (!cur.autocorrect())
1737 cur.message(_("Autocorrect Off ('!' to enter)"));
1739 cur.message(_("Autocorrect On (<space> to exit)"));
1745 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1747 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1749 if (!cur.empty() && cur.pos() > 0 &&
1750 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1751 if (InsetMathBig::isBigInsetDelim(str)) {
1752 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1753 if (prev[0] == '\\') {
1754 prev = prev.substr(1);
1755 latexkeys const * l = in_word_set(prev);
1756 if (l && l->inset == "big") {
1757 cur.cell()[cur.pos() - 1] =
1758 MathAtom(new InsetMathBig(prev, str));
1768 bool InsetMathNest::script(Cursor & cur, bool up)
1770 return script(cur, up, docstring());
1774 bool InsetMathNest::script(Cursor & cur, bool up,
1775 docstring const & save_selection)
1777 // Hack to get \^ and \_ working
1778 //lyxerr << "handling script: up: " << up << endl;
1779 if (cur.inMacroMode() && cur.macroName() == "\\") {
1781 cur.niceInsert(createInsetMath("mathcircumflex", cur.buffer()));
1783 interpretChar(cur, '_');
1787 cur.macroModeClose();
1788 if (asScriptInset() && cur.idx() == 0) {
1789 // we are in a nucleus of a script inset, move to _our_ script
1790 InsetMathScript * inset = asScriptInset();
1791 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1793 cur.idx() = inset->idxOfScript(up);
1795 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1797 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1800 cur.idx() = inset->idxOfScript(up);
1801 cur.pos() = cur.lastpos();
1803 // convert the thing to our left to a scriptinset or create a new
1804 // one if in the very first position of the array
1805 if (cur.pos() == 0) {
1806 //lyxerr << "new scriptinset" << endl;
1807 cur.insert(new InsetMathScript(buffer_, up));
1809 //lyxerr << "converting prev atom " << endl;
1810 cur.prevAtom() = MathAtom(new InsetMathScript(buffer_, cur.prevAtom(), up));
1813 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1814 // See comment in MathParser.cpp for special handling of {}-bases
1820 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1821 cur.niceInsert(save_selection);
1823 //lyxerr << "inserting selection 2:\n" << save_selection << endl;
1828 bool InsetMathNest::completionSupported(Cursor const & cur) const
1830 return cur.inMacroMode();
1834 bool InsetMathNest::inlineCompletionSupported(Cursor const & cur) const
1836 return cur.inMacroMode();
1840 bool InsetMathNest::automaticInlineCompletion() const
1842 return lyxrc.completion_inline_math;
1846 bool InsetMathNest::automaticPopupCompletion() const
1848 return lyxrc.completion_popup_math;
1852 CompletionList const *
1853 InsetMathNest::createCompletionList(Cursor const & cur) const
1855 if (!cur.inMacroMode())
1858 return new MathCompletionList(cur);
1862 docstring InsetMathNest::completionPrefix(Cursor const & cur) const
1864 if (!cur.inMacroMode())
1867 return cur.activeMacro()->name();
1871 bool InsetMathNest::insertCompletion(Cursor & cur, docstring const & s,
1874 if (!cur.inMacroMode())
1877 // append completion to active macro
1878 InsetMathUnknown * inset = cur.activeMacro();
1879 inset->setName(inset->name() + s);
1884 // FIXME: this creates duplicates in the completion popup
1885 // which looks ugly. Moreover the changes the list lengths
1887 confuse the popup as well.
1888 MathCompletionList::addToFavorites(inset->name());
1890 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, " "));
1897 void InsetMathNest::completionPosAndDim(Cursor const & cur, int & x, int & y,
1898 Dimension & dim) const
1900 Inset const * inset = cur.activeMacro();
1904 // get inset dimensions
1905 dim = cur.bv().coordCache().insets().dim(inset);
1906 // FIXME: these 3 are no accurate, but should depend on the font.
1907 // Now the popup jumps down if you enter a char with descent > 0.
1912 Point xy = cur.bv().coordCache().insets().xy(inset);
1918 bool InsetMathNest::cursorMathForward(Cursor & cur)
1920 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
1921 cur.pushBackward(*cur.nextAtom().nucleus());
1922 cur.inset().idxFirst(cur);
1925 if (cur.posForward() || idxForward(cur))
1927 // try to pop forwards --- but don't pop out of math! leave that to
1929 int s = cur.depth() - 2;
1930 if (s >= 0 && cur[s].inset().asInsetMath())
1931 return cur.popForward();
1936 bool InsetMathNest::cursorMathBackward(Cursor & cur)
1938 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
1940 cur.push(*cur.nextAtom().nucleus());
1941 cur.inset().idxLast(cur);
1944 if (cur.posBackward() || idxBackward(cur))
1946 // try to pop backwards --- but don't pop out of math! leave that to
1948 int s = cur.depth() - 2;
1949 if (s >= 0 && cur[s].inset().asInsetMath())
1950 return cur.popBackward();
1955 ////////////////////////////////////////////////////////////////////
1957 MathCompletionList::MathCompletionList(Cursor const & cur)
1959 // fill it with macros from the buffer
1960 MacroNameSet macros;
1961 cur.buffer()->listMacroNames(macros);
1962 MacroNameSet::const_iterator it;
1963 for (it = macros.begin(); it != macros.end(); ++it) {
1964 if (cur.buffer()->getMacro(*it, cur, false))
1965 locals.push_back("\\" + *it);
1967 sort(locals.begin(), locals.end());
1969 if (globals.size() > 0)
1972 // fill in global macros
1974 MacroTable::globalMacros().getMacroNames(macros);
1975 //lyxerr << "Globals completion macros: ";
1976 for (it = macros.begin(); it != macros.end(); ++it) {
1977 //lyxerr << "\\" + *it << " ";
1978 globals.push_back("\\" + *it);
1980 //lyxerr << std::endl;
1982 // fill in global commands
1983 globals.push_back(from_ascii("\\boxed"));
1984 globals.push_back(from_ascii("\\fbox"));
1985 globals.push_back(from_ascii("\\framebox"));
1986 globals.push_back(from_ascii("\\makebox"));
1987 globals.push_back(from_ascii("\\kern"));
1988 globals.push_back(from_ascii("\\xrightarrow"));
1989 globals.push_back(from_ascii("\\xleftarrow"));
1990 globals.push_back(from_ascii("\\split"));
1991 globals.push_back(from_ascii("\\gathered"));
1992 globals.push_back(from_ascii("\\aligned"));
1993 globals.push_back(from_ascii("\\alignedat"));
1994 globals.push_back(from_ascii("\\cases"));
1995 globals.push_back(from_ascii("\\substack"));
1996 globals.push_back(from_ascii("\\xymatrix"));
1997 globals.push_back(from_ascii("\\Diagram"));
1998 globals.push_back(from_ascii("\\subarray"));
1999 globals.push_back(from_ascii("\\array"));
2000 globals.push_back(from_ascii("\\sqrt"));
2001 globals.push_back(from_ascii("\\root"));
2002 globals.push_back(from_ascii("\\tabular"));
2003 globals.push_back(from_ascii("\\stackrel"));
2004 globals.push_back(from_ascii("\\binom"));
2005 globals.push_back(from_ascii("\\choose"));
2006 globals.push_back(from_ascii("\\brace"));
2007 globals.push_back(from_ascii("\\brack"));
2008 globals.push_back(from_ascii("\\frac"));
2009 globals.push_back(from_ascii("\\over"));
2010 globals.push_back(from_ascii("\\nicefrac"));
2011 globals.push_back(from_ascii("\\unitfrac"));
2012 globals.push_back(from_ascii("\\unitfracthree"));
2013 globals.push_back(from_ascii("\\unitone"));
2014 globals.push_back(from_ascii("\\unittwo"));
2015 globals.push_back(from_ascii("\\infer"));
2016 globals.push_back(from_ascii("\\atop"));
2017 globals.push_back(from_ascii("\\lefteqn"));
2018 globals.push_back(from_ascii("\\boldsymbol"));
2019 globals.push_back(from_ascii("\\bm"));
2020 globals.push_back(from_ascii("\\color"));
2021 globals.push_back(from_ascii("\\normalcolor"));
2022 globals.push_back(from_ascii("\\textcolor"));
2023 globals.push_back(from_ascii("\\cfrac"));
2024 globals.push_back(from_ascii("\\cfracleft"));
2025 globals.push_back(from_ascii("\\cfracright"));
2026 globals.push_back(from_ascii("\\dfrac"));
2027 globals.push_back(from_ascii("\\tfrac"));
2028 globals.push_back(from_ascii("\\dbinom"));
2029 globals.push_back(from_ascii("\\tbinom"));
2030 globals.push_back(from_ascii("\\hphantom"));
2031 globals.push_back(from_ascii("\\phantom"));
2032 globals.push_back(from_ascii("\\vphantom"));
2033 MathWordList const & words = mathedWordList();
2034 MathWordList::const_iterator it2;
2035 //lyxerr << "Globals completion commands: ";
2036 for (it2 = words.begin(); it2 != words.end(); ++it2) {
2037 globals.push_back("\\" + (*it2).first);
2038 //lyxerr << "\\" + (*it2).first << " ";
2040 //lyxerr << std::endl;
2041 sort(globals.begin(), globals.end());
2045 MathCompletionList::~MathCompletionList()
2050 size_type MathCompletionList::size() const
2052 return locals.size() + globals.size();
2056 docstring const & MathCompletionList::data(size_t idx) const
2058 size_t lsize = locals.size();
2060 return globals[idx - lsize];
2066 std::string MathCompletionList::icon(size_t idx) const
2068 // get the latex command
2070 size_t lsize = locals.size();
2072 cmd = globals[idx - lsize];
2076 // get the icon resource name by stripping the backslash
2077 return "images/math/" + to_utf8(cmd.substr(1)) + ".png";
2080 std::vector<docstring> MathCompletionList::globals;