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 void InsetMathNest::latex(otexstream & os, OutputParams const & runparams) const
399 WriteStream wi(os.os(), runparams.moving_arg, true,
400 runparams.dryrun ? WriteStream::wsDryrun : WriteStream::wsDefault,
402 wi.canBreakLine(os.canBreakLine());
404 os.canBreakLine(wi.canBreakLine());
407 if (lf > 0 && runparams.lastid != -1) {
409 os.texrow().newline();
410 os.texrow().start(runparams.lastid, runparams.lastpos);
412 os.texrow().newlines(lf);
416 bool InsetMathNest::setMouseHover(BufferView const * bv, bool mouse_hover)
419 mouse_hover_[bv] = mouse_hover;
424 bool InsetMathNest::notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
428 MathData & ar = cur.cell();
429 // remove base-only "scripts"
430 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
431 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
432 if (p && p->nargs() == 1) {
433 MathData ar = p->nuc();
436 cur.adjust(i, ar.size() - 1);
440 // glue adjacent font insets of the same kind
441 for (pos_type i = 0; i + 1 < size(); ++i) {
442 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
443 InsetMathFont const * q = operator[](i + 1)->asFontInset();
444 if (p && q && p->name() == q->name()) {
445 p->cell(0).append(q->cell(0));
455 void InsetMathNest::handleFont
456 (Cursor & cur, docstring const & arg, char const * const font)
458 handleFont(cur, arg, from_ascii(font));
462 void InsetMathNest::handleFont(Cursor & cur, docstring const & arg,
463 docstring const & font)
465 cur.recordUndoSelection();
467 // this whole function is a hack and won't work for incremental font
469 if (cur.inset().asInsetMath()->name() == font)
470 cur.handleFont(to_utf8(font));
472 handleNest(cur, createInsetMath(font, cur.buffer()), arg);
476 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest)
478 handleNest(cur, nest, docstring());
482 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest,
483 docstring const & arg)
485 CursorSlice i1 = cur.selBegin();
486 CursorSlice i2 = cur.selEnd();
487 if (!i1.inset().asInsetMath())
489 if (i1.idx() == i2.idx()) {
490 // the easy case where only one cell is selected
491 cur.handleNest(nest);
496 // multiple selected cells in a simple non-grid inset
497 if (i1.asInsetMath()->nrows() == 0 || i1.asInsetMath()->ncols() == 0) {
498 for (idx_type i = i1.idx(); i <= i2.idx(); ++i) {
503 cur.pos() = cur.lastpos();
506 // change font of cell
507 cur.handleNest(nest);
510 // cur is in the font inset now. If the loop continues,
511 // we need to get outside again for the next cell
512 if (i + 1 <= i2.idx())
518 // the complicated case with multiple selected cells in a grid
521 cap::region(i1, i2, r1, r2, c1, c2);
522 for (row_type row = r1; row <= r2; ++row) {
523 for (col_type col = c1; col <= c2; ++col) {
525 cur.idx() = i1.asInsetMath()->index(row, col);
528 cur.pos() = cur.lastpos();
532 cur.handleNest(nest);
535 // cur is in the font inset now. If the loop continues,
536 // we need to get outside again for the next cell
537 if (col + 1 <= c2 || row + 1 <= r2)
544 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
546 cur.recordUndoSelection();
549 font.fromString(to_utf8(arg), b);
550 if (font.fontInfo().color() != Color_inherit &&
551 font.fontInfo().color() != Color_ignore)
552 handleNest(cur, MathAtom(new InsetMathColor(buffer_, true, font.fontInfo().color())));
554 // FIXME: support other font changes here as well?
558 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
560 //LYXERR0("InsetMathNest: request: " << cmd);
562 Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
564 FuncCode const act = cmd.action();
567 case LFUN_CLIPBOARD_PASTE:
568 parseflg |= Parse::VERBATIM;
571 if (cur.currentMode() <= TEXT_MODE)
572 parseflg |= Parse::TEXTMODE;
573 cur.recordUndoSelection();
574 cur.message(_("Paste"));
575 replaceSelection(cur);
577 if (cmd.argument().empty() && !theClipboard().isInternal())
578 topaste = theClipboard().getAsText();
581 idocstringstream is(cmd.argument());
583 topaste = cap::selection(n);
585 cur.niceInsert(topaste, parseflg, false);
586 cur.clearSelection(); // bug 393
587 cur.forceBufferUpdate();
594 cutSelection(cur, true, true);
595 cur.message(_("Cut"));
596 // Prevent stale position >= size crash
597 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
599 cur.forceBufferUpdate();
604 cur.message(_("Copy"));
607 case LFUN_MOUSE_PRESS:
608 lfunMousePress(cur, cmd);
611 case LFUN_MOUSE_MOTION:
612 lfunMouseMotion(cur, cmd);
615 case LFUN_MOUSE_RELEASE:
616 lfunMouseRelease(cur, cmd);
619 case LFUN_FINISHED_LEFT: // in math, left is backwards
620 case LFUN_FINISHED_BACKWARD:
621 cur.bv().cursor() = cur;
624 case LFUN_FINISHED_RIGHT: // in math, right is forward
625 case LFUN_FINISHED_FORWARD:
627 cur.bv().cursor() = cur;
630 case LFUN_CHAR_RIGHT:
632 case LFUN_CHAR_BACKWARD:
633 case LFUN_CHAR_FORWARD:
634 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
635 case LFUN_CHAR_RIGHT_SELECT:
636 case LFUN_CHAR_LEFT_SELECT:
637 case LFUN_CHAR_BACKWARD_SELECT:
638 case LFUN_CHAR_FORWARD_SELECT: {
639 // are we in a selection?
640 bool select = (act == LFUN_CHAR_RIGHT_SELECT
641 || act == LFUN_CHAR_LEFT_SELECT
642 || act == LFUN_CHAR_BACKWARD_SELECT
643 || act == LFUN_CHAR_FORWARD_SELECT);
644 // are we moving forward or backwards?
645 // If the command was RIGHT or LEFT, then whether we're moving forward
646 // or backwards depends on the cursor movement mode (logical or visual):
647 // * in visual mode, since math is always LTR, right -> forward,
649 // * in logical mode, the mapping is determined by the
650 // reverseDirectionNeeded() function
653 FuncCode finish_lfun;
655 if (act == LFUN_CHAR_FORWARD
656 || act == LFUN_CHAR_FORWARD_SELECT) {
658 finish_lfun = LFUN_FINISHED_FORWARD;
660 else if (act == LFUN_CHAR_BACKWARD
661 || act == LFUN_CHAR_BACKWARD_SELECT) {
663 finish_lfun = LFUN_FINISHED_BACKWARD;
666 bool right = (act == LFUN_CHAR_RIGHT_SELECT
667 || act == LFUN_CHAR_RIGHT);
668 if (lyxrc.visual_cursor || !reverseDirectionNeeded(cur))
674 finish_lfun = LFUN_FINISHED_RIGHT;
676 finish_lfun = LFUN_FINISHED_LEFT;
678 // Now that we know exactly what we want to do, let's do it!
679 cur.selHandle(select);
681 cur.macroModeClose();
682 // try moving forward or backwards as necessary...
683 if (!(forward ? cursorMathForward(cur) : cursorMathBackward(cur))) {
684 // ... and if movement failed, then finish forward or backwards
686 cmd = FuncRequest(finish_lfun);
694 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
695 case LFUN_DOWN_SELECT:
696 case LFUN_UP_SELECT: {
697 // close active macro
698 if (cur.inMacroMode()) {
699 cur.macroModeClose();
703 // stop/start the selection
704 bool select = act == LFUN_DOWN_SELECT ||
705 act == LFUN_UP_SELECT;
706 cur.selHandle(select);
709 bool up = act == LFUN_UP || act == LFUN_UP_SELECT;
710 bool successful = cur.upDownInMath(up);
714 if (cur.fixIfBroken())
715 // FIXME: Something bad happened. We pass the corrected Cursor
716 // instead of letting things go worse.
719 // We did not manage to move the cursor.
724 case LFUN_MOUSE_DOUBLE:
725 case LFUN_MOUSE_TRIPLE:
726 case LFUN_WORD_SELECT:
730 cur.setSelection(true);
731 cur.pos() = cur.lastpos();
732 cur.idx() = cur.lastidx();
735 case LFUN_PARAGRAPH_UP:
736 case LFUN_PARAGRAPH_DOWN:
737 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
738 case LFUN_PARAGRAPH_UP_SELECT:
739 case LFUN_PARAGRAPH_DOWN_SELECT:
742 case LFUN_LINE_BEGIN:
743 case LFUN_WORD_BACKWARD:
745 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
746 case LFUN_LINE_BEGIN_SELECT:
747 case LFUN_WORD_BACKWARD_SELECT:
748 case LFUN_WORD_LEFT_SELECT:
749 cur.selHandle(act == LFUN_WORD_BACKWARD_SELECT ||
750 act == LFUN_WORD_LEFT_SELECT ||
751 act == LFUN_LINE_BEGIN_SELECT);
752 cur.macroModeClose();
753 if (cur.pos() != 0) {
755 } else if (cur.col() != 0) {
756 cur.idx() -= cur.col();
758 } else if (cur.idx() != 0) {
762 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
767 case LFUN_WORD_FORWARD:
768 case LFUN_WORD_RIGHT:
770 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
771 case LFUN_WORD_FORWARD_SELECT:
772 case LFUN_WORD_RIGHT_SELECT:
773 case LFUN_LINE_END_SELECT:
774 cur.selHandle(act == LFUN_WORD_FORWARD_SELECT ||
775 act == LFUN_WORD_RIGHT_SELECT ||
776 act == LFUN_LINE_END_SELECT);
777 cur.macroModeClose();
779 if (cur.pos() != cur.lastpos()) {
780 cur.pos() = cur.lastpos();
781 } else if (ncols() && (cur.col() != cur.lastcol())) {
782 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
783 cur.pos() = cur.lastpos();
784 } else if (cur.idx() != cur.lastidx()) {
785 cur.idx() = cur.lastidx();
786 cur.pos() = cur.lastpos();
788 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
793 case LFUN_CELL_FORWARD:
794 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
795 cur.inset().idxNext(cur);
798 case LFUN_CELL_BACKWARD:
799 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
800 cur.inset().idxPrev(cur);
803 case LFUN_WORD_DELETE_BACKWARD:
804 case LFUN_CHAR_DELETE_BACKWARD:
806 // May affect external cell:
807 cur.recordUndoInset();
809 cur.recordUndoSelection();
810 // if the inset can not be removed from within, delete it
811 if (!cur.backspace()) {
812 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
813 cur.innerText()->dispatch(cur, cmd);
817 case LFUN_WORD_DELETE_FORWARD:
818 case LFUN_CHAR_DELETE_FORWARD:
819 if (cur.pos() == cur.lastpos())
820 // May affect external cell:
821 cur.recordUndoInset();
823 cur.recordUndoSelection();
824 // if the inset can not be removed from within, delete it
826 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
827 cur.innerText()->dispatch(cur, cmd);
833 cur.clearSelection();
835 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
840 // 'Locks' the math inset. A 'locked' math inset behaves as a unit
841 // that is traversed by a single <CursorLeft>/<CursorRight>.
842 case LFUN_INSET_TOGGLE:
848 case LFUN_SELF_INSERT:
849 if (cmd.argument().size() != 1) {
850 cur.recordUndoSelection();
851 docstring const arg = cmd.argument();
852 if (!interpretString(cur, arg))
856 // Don't record undo steps if we are in macro mode and thus
857 // cmd.argument is the next character of the macro name.
858 // Otherwise we'll get an invalid cursor if we undo after
859 // the macro was finished and the macro is a known command,
860 // e.g. sqrt. Cursor::macroModeClose replaces in this case
861 // the InsetMathUnknown with name "frac" by an empty
862 // InsetMathFrac -> a pos value > 0 is invalid.
863 // A side effect is that an undo before the macro is finished
864 // undoes the complete macro, not only the last character.
865 // At the time we hit '\' we are not in macro mode, still.
866 if (!cur.inMacroMode())
867 cur.recordUndoSelection();
869 // spacial handling of space. If we insert an inset
870 // via macro mode, we want to put the cursor inside it
871 // if relevant. Think typing "\frac<space>".
872 if (cmd.argument()[0] == ' '
873 && cur.inMacroMode() && cur.macroName() != "\\"
874 && cur.macroModeClose()) {
875 MathAtom const atom = cur.prevAtom();
876 if (atom->asNestInset() && atom->isActive()) {
878 cur.pushBackward(*cur.nextInset());
880 } else if (!interpretChar(cur, cmd.argument()[0])) {
881 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
886 //case LFUN_SERVER_GET_XY:
889 case LFUN_SERVER_SET_XY: {
890 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
893 istringstream is(to_utf8(cmd.argument()));
895 cur.setScreenPos(x, y);
899 // Special casing for superscript in case of LyX handling
901 case LFUN_ACCENT_CIRCUMFLEX:
902 if (cmd.argument().empty()) {
903 // do superscript if LyX handles
905 cur.recordUndoSelection();
906 script(cur, true, grabAndEraseSelection(cur));
910 case LFUN_ACCENT_UMLAUT:
911 case LFUN_ACCENT_ACUTE:
912 case LFUN_ACCENT_GRAVE:
913 case LFUN_ACCENT_BREVE:
914 case LFUN_ACCENT_DOT:
915 case LFUN_ACCENT_MACRON:
916 case LFUN_ACCENT_CARON:
917 case LFUN_ACCENT_TILDE:
918 case LFUN_ACCENT_CEDILLA:
919 case LFUN_ACCENT_CIRCLE:
920 case LFUN_ACCENT_UNDERDOT:
921 case LFUN_ACCENT_TIE:
922 case LFUN_ACCENT_OGONEK:
923 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
927 case LFUN_TEXTSTYLE_APPLY:
928 case LFUN_TEXTSTYLE_UPDATE:
929 handleFont2(cur, cmd.argument());
933 if (currentMode() <= TEXT_MODE)
934 handleFont(cur, cmd.argument(), "textbf");
936 handleFont(cur, cmd.argument(), "mathbf");
938 case LFUN_FONT_BOLDSYMBOL:
939 if (currentMode() <= TEXT_MODE)
940 handleFont(cur, cmd.argument(), "textbf");
942 handleFont(cur, cmd.argument(), "boldsymbol");
945 if (currentMode() <= TEXT_MODE)
946 handleFont(cur, cmd.argument(), "textsf");
948 handleFont(cur, cmd.argument(), "mathsf");
951 if (currentMode() <= TEXT_MODE)
952 handleFont(cur, cmd.argument(), "emph");
954 handleFont(cur, cmd.argument(), "mathcal");
956 case LFUN_FONT_ROMAN:
957 if (currentMode() <= TEXT_MODE)
958 handleFont(cur, cmd.argument(), "textrm");
960 handleFont(cur, cmd.argument(), "mathrm");
962 case LFUN_FONT_TYPEWRITER:
963 if (currentMode() <= TEXT_MODE)
964 handleFont(cur, cmd.argument(), "texttt");
966 handleFont(cur, cmd.argument(), "mathtt");
969 handleFont(cur, cmd.argument(), "mathfrak");
972 if (currentMode() <= TEXT_MODE)
973 handleFont(cur, cmd.argument(), "textit");
975 handleFont(cur, cmd.argument(), "mathit");
978 if (currentMode() <= TEXT_MODE)
979 // FIXME: should be "noun"
980 handleFont(cur, cmd.argument(), "textsc");
982 handleFont(cur, cmd.argument(), "mathbb");
984 case LFUN_FONT_DEFAULT:
985 handleFont(cur, cmd.argument(), "textnormal");
988 case LFUN_FONT_UNDERLINE:
990 cur.handleNest(createInsetMath("underline", cur.buffer()));
992 case LFUN_MATH_MODE: {
994 // ignore math-mode on when already in math mode
995 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
997 cur.recordUndoSelection();
998 cur.macroModeClose();
999 docstring const save_selection = grabAndEraseSelection(cur);
1001 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
1002 if (currentMode() <= Inset::TEXT_MODE)
1003 cur.plainInsert(MathAtom(new InsetMathEnsureMath(buffer_)));
1005 cur.plainInsert(MathAtom(new InsetMathBox(buffer_, from_ascii("mbox"))));
1007 cur.pushBackward(*cur.nextInset());
1008 cur.niceInsert(save_selection);
1009 cur.forceBufferUpdate();
1011 if (currentMode() == Inset::TEXT_MODE) {
1012 cur.recordUndoSelection();
1013 cur.niceInsert(MathAtom(new InsetMathHull("simple", cur.buffer())));
1014 cur.message(_("create new math text environment ($...$)"));
1016 handleFont(cur, cmd.argument(), "textrm");
1017 cur.message(_("entered math text mode (textrm)"));
1023 case LFUN_REGEXP_MODE: {
1024 InsetMath * im = cur.inset().asInsetMath();
1026 InsetMathHull * i = im->asHullInset();
1027 if (i && i->getType() == hullRegexp) {
1028 cur.message(_("Already in regular expression mode"));
1032 cur.macroModeClose();
1033 docstring const save_selection = grabAndEraseSelection(cur);
1035 cur.plainInsert(MathAtom(new InsetMathHull(buffer_, hullRegexp)));
1037 cur.pushBackward(*cur.nextInset());
1038 cur.niceInsert(save_selection);
1039 cur.message(_("Regular expression editor mode"));
1043 case LFUN_MATH_FONT_STYLE: {
1044 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, '\\' + cmd.argument());
1045 doDispatch(cur, fr);
1049 case LFUN_MATH_SIZE: {
1050 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, cmd.argument());
1051 doDispatch(cur, fr);
1055 case LFUN_MATH_MATRIX: {
1061 idocstringstream is(cmd.argument());
1062 is >> m >> n >> v_align >> h_align;
1068 cur.niceInsert(MathAtom(new InsetMathArray(buffer_,
1069 from_ascii("array"), m, n, (char)v_align[0], h_align)));
1073 case LFUN_MATH_AMS_MATRIX: {
1078 idocstringstream is(cmd.argument());
1079 is >> m >> n >> name;
1085 MathAtom(new InsetMathAMSArray(buffer_, name, m, n)));
1089 case LFUN_MATH_DELIM: {
1091 docstring rs = split(cmd.argument(), ls, ' ');
1092 // Reasonable default values
1098 cur.handleNest(MathAtom(new InsetMathDelim(buffer_, ls, rs)));
1102 case LFUN_MATH_BIGDELIM: {
1103 docstring const lname = from_utf8(cmd.getArg(0));
1104 docstring const ldelim = from_utf8(cmd.getArg(1));
1105 docstring const rname = from_utf8(cmd.getArg(2));
1106 docstring const rdelim = from_utf8(cmd.getArg(3));
1107 latexkeys const * l = in_word_set(lname);
1108 bool const have_l = l && l->inset == "big" &&
1109 InsetMathBig::isBigInsetDelim(ldelim);
1110 l = in_word_set(rname);
1111 bool const have_r = l && l->inset == "big" &&
1112 InsetMathBig::isBigInsetDelim(rdelim);
1113 // We mimic LFUN_MATH_DELIM in case we have an empty left
1114 // or right delimiter.
1115 if (have_l || have_r) {
1117 docstring const selection = grabAndEraseSelection(cur);
1120 cur.insert(MathAtom(new InsetMathBig(lname,
1122 // first insert the right delimiter and then go back
1123 // and re-insert the selection (bug 7088)
1125 cur.insert(MathAtom(new InsetMathBig(rname,
1129 cur.niceInsert(selection);
1131 // Don't call cur.undispatched() if we did nothing, this would
1132 // lead to infinite recursion via Text::dispatch().
1136 case LFUN_SPACE_INSERT:
1137 cur.recordUndoSelection();
1138 cur.insert(MathAtom(new InsetMathSpace));
1141 case LFUN_MATH_SPACE:
1142 cur.recordUndoSelection();
1143 if (cmd.argument().empty())
1144 cur.insert(MathAtom(new InsetMathSpace));
1146 string const name = cmd.getArg(0);
1147 string const len = cmd.getArg(1);
1148 cur.insert(MathAtom(new InsetMathSpace(name, len)));
1152 case LFUN_ERT_INSERT:
1153 // interpret this as if a backslash was typed
1155 interpretChar(cur, '\\');
1158 case LFUN_MATH_SUBSCRIPT:
1159 // interpret this as if a _ was typed
1160 cur.recordUndoSelection();
1161 interpretChar(cur, '_');
1164 case LFUN_MATH_SUPERSCRIPT:
1165 // interpret this as if a ^ was typed
1166 cur.recordUndoSelection();
1167 interpretChar(cur, '^');
1170 case LFUN_MATH_MACRO_FOLD:
1171 case LFUN_MATH_MACRO_UNFOLD: {
1173 bool fold = act == LFUN_MATH_MACRO_FOLD;
1174 bool found = findMacroToFoldUnfold(it, fold);
1176 MathMacro * macro = it.nextInset()->asInsetMath()->asMacro();
1177 cur.recordUndoInset();
1186 case LFUN_QUOTE_INSERT:
1187 // interpret this as if a straight " was typed
1188 cur.recordUndoSelection();
1189 interpretChar(cur, '\"');
1192 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
1193 // handling such that "self-insert" works on "arbitrary stuff" too, and
1194 // math-insert only handles special math things like "matrix".
1195 case LFUN_MATH_INSERT: {
1196 cur.recordUndoSelection();
1197 if (cmd.argument() == "^" || cmd.argument() == "_")
1198 interpretChar(cur, cmd.argument()[0]);
1201 asArray(cmd.argument(), ar);
1202 if (cur.selection() && ar.size() == 1
1203 && ar[0]->asNestInset()
1204 && ar[0]->asNestInset()->nargs() > 1)
1205 handleNest(cur, ar[0]);
1207 cur.niceInsert(cmd.argument());
1212 case LFUN_UNICODE_INSERT: {
1213 if (cmd.argument().empty())
1215 docstring hexstring = cmd.argument();
1216 if (isHex(hexstring)) {
1217 char_type c = hexToInt(hexstring);
1218 if (c >= 32 && c < 0x10ffff) {
1219 docstring s = docstring(1, c);
1220 FuncCode code = currentMode() == MATH_MODE ?
1221 LFUN_MATH_INSERT : LFUN_SELF_INSERT;
1222 lyx::dispatch(FuncRequest(code, s));
1228 case LFUN_DIALOG_SHOW_NEW_INSET: {
1229 docstring const & name = cmd.argument();
1231 if (name == "ref") {
1232 InsetMathRef tmp(buffer_, name);
1233 data = tmp.createDialogStr();
1234 cur.bv().showDialog(to_utf8(name), data);
1235 } else if (name == "mathspace") {
1236 cur.bv().showDialog(to_utf8(name));
1241 case LFUN_INSET_INSERT: {
1243 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1244 cur.recordUndoSelection();
1246 cur.forceBufferUpdate();
1251 case LFUN_INSET_DISSOLVE:
1252 if (!asHullInset()) {
1253 cur.recordUndoInset();
1259 InsetMath::doDispatch(cur, cmd);
1265 bool InsetMathNest::findMacroToFoldUnfold(Cursor & it, bool fold) const {
1266 // look for macro to open/close, but stay in mathed
1267 for (; !it.empty(); it.pop_back()) {
1269 // go backward through the current cell
1270 Inset * inset = it.nextInset();
1271 while (inset && inset->asInsetMath()) {
1272 MathMacro * macro = inset->asInsetMath()->asMacro();
1274 // found the an macro to open/close?
1275 if (macro->folded() != fold)
1278 // Wrong folding state.
1279 // If this was the first we see in this slice, look further left,
1281 if (inset != it.nextInset())
1285 // go up if this was the left most position
1291 inset = it.nextInset();
1299 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1300 FuncStatus & flag) const
1302 // the font related toggles
1303 //string tc = "mathnormal";
1305 string const arg = to_utf8(cmd.argument());
1306 switch (cmd.action()) {
1307 case LFUN_INSET_MODIFY:
1308 flag.setEnabled(false);
1311 case LFUN_INSET_MODIFY:
1312 // FIXME: check temporarily disabled
1314 char align = mathcursor::valign();
1315 if (align == '\0') {
1319 if (cmd.argument().empty()) {
1323 if (!contains("tcb", cmd.argument()[0])) {
1327 flag.setOnOff(cmd.argument()[0] == align);
1330 /// We have to handle them since 1.4 blocks all unhandled actions
1331 case LFUN_FONT_ITAL:
1332 case LFUN_FONT_BOLD:
1333 case LFUN_FONT_BOLDSYMBOL:
1334 case LFUN_FONT_SANS:
1335 case LFUN_FONT_EMPH:
1336 case LFUN_FONT_TYPEWRITER:
1337 case LFUN_FONT_NOUN:
1338 case LFUN_FONT_ROMAN:
1339 case LFUN_FONT_DEFAULT:
1340 flag.setEnabled(true);
1343 // we just need to be in math mode to enable that
1344 case LFUN_MATH_SIZE:
1345 case LFUN_MATH_SPACE:
1346 case LFUN_MATH_LIMITS:
1347 case LFUN_MATH_EXTERN:
1348 flag.setEnabled(true);
1351 case LFUN_FONT_UNDERLINE:
1352 case LFUN_FONT_FRAK:
1353 flag.setEnabled(currentMode() != TEXT_MODE);
1356 case LFUN_MATH_FONT_STYLE: {
1357 bool const textarg =
1358 arg == "textbf" || arg == "textsf" ||
1359 arg == "textrm" || arg == "textmd" ||
1360 arg == "textit" || arg == "textsc" ||
1361 arg == "textsl" || arg == "textup" ||
1362 arg == "texttt" || arg == "textbb" ||
1363 arg == "textnormal";
1364 flag.setEnabled(currentMode() != TEXT_MODE || textarg);
1368 case LFUN_MATH_MODE:
1369 // forbid "math-mode on" in math mode to prevent irritating
1370 // behaviour of menu entries (bug 6709)
1371 flag.setEnabled(currentMode() == TEXT_MODE || arg != "on");
1374 case LFUN_MATH_INSERT:
1375 flag.setEnabled(currentMode() != TEXT_MODE);
1378 case LFUN_MATH_AMS_MATRIX:
1379 case LFUN_MATH_MATRIX:
1380 flag.setEnabled(currentMode() == MATH_MODE);
1383 case LFUN_INSET_INSERT: {
1384 // Don't test createMathInset_fromDialogStr(), since
1385 // getStatus is not called with a valid reference and the
1386 // dialog would not be applyable.
1387 string const name = cmd.getArg(0);
1388 flag.setEnabled(name == "ref" || name == "mathspace");
1392 case LFUN_MATH_DELIM:
1393 case LFUN_MATH_BIGDELIM:
1394 // Don't do this with multi-cell selections
1395 flag.setEnabled(cur.selBegin().idx() == cur.selEnd().idx());
1398 case LFUN_MATH_MACRO_FOLD:
1399 case LFUN_MATH_MACRO_UNFOLD: {
1401 bool found = findMacroToFoldUnfold(it, cmd.action() == LFUN_MATH_MACRO_FOLD);
1402 flag.setEnabled(found);
1406 case LFUN_SPECIALCHAR_INSERT:
1407 case LFUN_SCRIPT_INSERT:
1408 // FIXME: These would probably make sense in math-text mode
1409 flag.setEnabled(false);
1412 case LFUN_INSET_DISSOLVE:
1413 flag.setEnabled(!asHullInset());
1424 void InsetMathNest::edit(Cursor & cur, bool front, EntryDirection entry_from)
1427 bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_RIGHT ||
1428 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1429 cur.idx() = enter_front ? 0 : cur.lastidx();
1430 cur.pos() = enter_front ? 0 : cur.lastpos();
1432 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1436 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1439 int dist_min = 1000000;
1440 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1441 int const d = cell(i).dist(cur.bv(), x, y);
1447 MathData & ar = cell(idx_min);
1449 cur.idx() = idx_min;
1450 cur.pos() = ar.x2pos(&cur.bv(), x - ar.xo(cur.bv()));
1452 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1453 if (dist_min == 0) {
1455 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1456 if (ar[i]->covers(cur.bv(), x, y))
1457 return ar[i].nucleus()->editXY(cur, x, y);
1463 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1465 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1466 BufferView & bv = cur.bv();
1467 if (cmd.button() == mouse_button::button3) {
1468 // Don't do anything if we right-click a
1469 // selection, a context menu will popup.
1470 if (bv.cursor().selection() && cur >= bv.cursor().selectionBegin()
1471 && cur < bv.cursor().selectionEnd()) {
1472 cur.noScreenUpdate();
1476 bool do_selection = cmd.button() == mouse_button::button1
1477 && cmd.argument() == "region-select";
1478 bv.mouseSetCursor(cur, do_selection);
1479 if (cmd.button() == mouse_button::button1) {
1480 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1481 // Update the cursor update flags as needed:
1483 // Update::Decoration: tells to update the decoration
1484 // (visual box corners that define
1486 // Update::FitCursor: adjust the screen to the cursor
1487 // position if needed
1488 // cur.result().update(): don't overwrite previously set flags.
1489 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor
1490 | cur.result().screenUpdate());
1491 } else if (cmd.button() == mouse_button::button2) {
1492 if (cap::selection()) {
1493 // See comment in Text::dispatch why we do this
1494 cap::copySelectionToStack();
1495 cmd = FuncRequest(LFUN_PASTE, "0");
1496 doDispatch(bv.cursor(), cmd);
1499 asArray(theSelection().get(), ar);
1500 bv.cursor().insert(ar);
1506 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1508 // only select with button 1
1509 if (cmd.button() == mouse_button::button1) {
1510 Cursor & bvcur = cur.bv().cursor();
1511 if (bvcur.realAnchor().hasPart(cur)) {
1512 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1513 bvcur.setCursor(cur);
1514 bvcur.setSelection(true);
1515 //lyxerr << "MOTION " << bvcur << endl;
1522 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1524 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1526 if (cmd.button() == mouse_button::button1) {
1527 if (!cur.selection())
1528 cur.noScreenUpdate();
1530 Cursor & bvcur = cur.bv().cursor();
1531 bvcur.setSelection(true);
1540 bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
1542 //lyxerr << "interpret 2: '" << c << "'" << endl;
1543 docstring save_selection;
1544 if (c == '^' || c == '_')
1545 save_selection = grabAndEraseSelection(cur);
1548 Buffer * buf = cur.buffer();
1551 if (cur.inMacroMode()) {
1552 docstring name = cur.macroName();
1554 /// are we currently typing '#1' or '#2' or...?
1555 if (name == "\\#") {
1558 if (n >= 1 && n <= 9)
1559 cur.insert(new MathMacroArgument(n));
1563 // do not finish macro for known * commands
1564 MathWordList const & mwl = mathedWordList();
1565 bool star_macro = c == '*'
1566 && (mwl.find(name.substr(1) + "*") != mwl.end()
1567 || cur.buffer()->getMacro(name.substr(1) + "*", cur, true));
1568 if (isAlphaASCII(c) || star_macro) {
1569 cur.activeMacro()->setName(name + docstring(1, c));
1573 // handle 'special char' macros
1578 if (currentMode() <= InsetMath::TEXT_MODE)
1579 cur.niceInsert(createInsetMath("textbackslash", buf));
1581 cur.niceInsert(createInsetMath("backslash", buf));
1582 } else if (c == '^' && currentMode() == InsetMath::MATH_MODE) {
1584 cur.niceInsert(createInsetMath("mathcircumflex", buf));
1585 } else if (c == '{') {
1587 cur.niceInsert(MathAtom(new InsetMathBrace(buf)));
1588 } else if (c == '%') {
1590 cur.niceInsert(MathAtom(new InsetMathComment(buf)));
1591 } else if (c == '#') {
1592 LASSERT(cur.activeMacro(), /**/);
1593 cur.activeMacro()->setName(name + docstring(1, c));
1596 cur.niceInsert(createInsetMath(docstring(1, c), buf));
1601 // One character big delimiters. The others are handled in
1602 // interpretString().
1603 latexkeys const * l = in_word_set(name.substr(1));
1604 if (name[0] == '\\' && l && l->inset == "big") {
1608 delim = from_ascii("\\{");
1611 delim = from_ascii("\\}");
1614 delim = docstring(1, c);
1617 if (InsetMathBig::isBigInsetDelim(delim)) {
1618 // name + delim ared a valid InsetMathBig.
1619 // We can't use cur.macroModeClose() because
1620 // it does not handle delim.
1621 InsetMathUnknown * p = cur.activeMacro();
1624 cur.cell().erase(cur.pos());
1625 cur.plainInsert(MathAtom(
1626 new InsetMathBig(name.substr(1), delim)));
1631 // leave macro mode and try again if necessary
1632 if (cur.macroModeClose()) {
1633 MathAtom const atom = cur.prevAtom();
1634 if (atom->asNestInset() && atom->isActive()) {
1636 cur.pushBackward(*cur.nextInset());
1640 cur.niceInsert(MathAtom(new InsetMathBrace(buf)));
1642 interpretChar(cur, c);
1647 // leave autocorrect mode if necessary
1648 if (lyxrc.autocorrection_math && c == ' ' && cur.autocorrect()) {
1649 cur.autocorrect() = false;
1650 cur.message(_("Autocorrect Off ('!' to enter)"));
1653 if (lyxrc.autocorrection_math && c == '!' && !cur.autocorrect()) {
1654 cur.autocorrect() = true;
1655 cur.message(_("Autocorrect On (<space> to exit)"));
1659 // just clear selection on pressing the space bar
1660 if (cur.selection() && c == ' ') {
1661 cur.setSelection(false);
1666 //lyxerr << "starting with macro" << endl;
1667 bool reduced = cap::reduceSelectionToOneCell(cur);
1668 if (reduced || !cur.selection()) {
1669 docstring const safe = cap::grabAndEraseSelection(cur);
1670 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), safe, false)));
1678 if (currentMode() <= InsetMath::TEXT_MODE)
1684 if (currentMode() <= InsetMath::TEXT_MODE) {
1685 // insert spaces in text or undecided mode,
1686 // but suppress direct insertion of two spaces in a row
1687 // the still allows typing '<space>a<space>' and deleting the 'a', but
1688 // it is better than nothing...
1689 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1691 // FIXME: we have to enable full redraw here because of the
1692 // visual box corners that define the inset. If we know for
1693 // sure that we stay within the same cell we can optimize for
1695 //cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1699 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1700 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1701 // FIXME: we have to enable full redraw here because of the
1702 // visual box corners that define the inset. If we know for
1703 // sure that we stay within the same cell we can optimize for
1705 //cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1709 if (cur.popForward()) {
1710 // FIXME: we have to enable full redraw here because of the
1711 // visual box corners that define the inset. If we know for
1712 // sure that we stay within the same cell we can optimize for
1714 //cur.screenUpdateFlags(Update::FitCursor);
1718 // if we are at the very end, leave the formula
1719 return cur.pos() != cur.lastpos();
1722 // These should be treated differently when not in text mode:
1723 if (currentMode() != InsetMath::TEXT_MODE) {
1725 script(cur, false, save_selection);
1729 script(cur, true, save_selection);
1733 cur.niceInsert(createInsetMath("sim", buf));
1736 if (currentMode() == InsetMath::MATH_MODE && !isAsciiOrMathAlpha(c)) {
1737 MathAtom at = createInsetMath("text", buf);
1738 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
1745 cur.niceInsert(createInsetMath("textasciicircum", buf));
1749 cur.niceInsert(createInsetMath("textasciitilde", buf));
1754 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1755 c == '%' || c == '_') {
1756 cur.niceInsert(createInsetMath(docstring(1, c), buf));
1761 // try auto-correction
1762 if (lyxrc.autocorrection_math && cur.autocorrect() && cur.pos() != 0
1763 && math_autocorrect(cur.prevAtom(), c))
1766 // no special circumstances, so insert the character without any fuss
1768 if (lyxrc.autocorrection_math) {
1769 if (!cur.autocorrect())
1770 cur.message(_("Autocorrect Off ('!' to enter)"));
1772 cur.message(_("Autocorrect On (<space> to exit)"));
1778 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1780 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1782 if (!cur.empty() && cur.pos() > 0 &&
1783 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1784 if (InsetMathBig::isBigInsetDelim(str)) {
1785 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1786 if (prev[0] == '\\') {
1787 prev = prev.substr(1);
1788 latexkeys const * l = in_word_set(prev);
1789 if (l && l->inset == "big") {
1790 cur.cell()[cur.pos() - 1] =
1791 MathAtom(new InsetMathBig(prev, str));
1801 bool InsetMathNest::script(Cursor & cur, bool up)
1803 return script(cur, up, docstring());
1807 bool InsetMathNest::script(Cursor & cur, bool up,
1808 docstring const & save_selection)
1810 // Hack to get \^ and \_ working
1811 //lyxerr << "handling script: up: " << up << endl;
1812 if (cur.inMacroMode() && cur.macroName() == "\\") {
1814 cur.niceInsert(createInsetMath("mathcircumflex", cur.buffer()));
1816 interpretChar(cur, '_');
1820 cur.macroModeClose();
1821 if (asScriptInset() && cur.idx() == 0) {
1822 // we are in a nucleus of a script inset, move to _our_ script
1823 InsetMathScript * inset = asScriptInset();
1824 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1826 cur.idx() = inset->idxOfScript(up);
1828 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1830 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1833 cur.idx() = inset->idxOfScript(up);
1834 cur.pos() = cur.lastpos();
1836 // convert the thing to our left to a scriptinset or create a new
1837 // one if in the very first position of the array
1838 if (cur.pos() == 0) {
1839 //lyxerr << "new scriptinset" << endl;
1840 cur.insert(new InsetMathScript(buffer_, up));
1842 //lyxerr << "converting prev atom " << endl;
1843 cur.prevAtom() = MathAtom(new InsetMathScript(buffer_, cur.prevAtom(), up));
1846 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1847 // See comment in MathParser.cpp for special handling of {}-bases
1853 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1854 cur.niceInsert(save_selection);
1856 //lyxerr << "inserting selection 2:\n" << save_selection << endl;
1861 bool InsetMathNest::completionSupported(Cursor const & cur) const
1863 return cur.inMacroMode();
1867 bool InsetMathNest::inlineCompletionSupported(Cursor const & cur) const
1869 return cur.inMacroMode();
1873 bool InsetMathNest::automaticInlineCompletion() const
1875 return lyxrc.completion_inline_math;
1879 bool InsetMathNest::automaticPopupCompletion() const
1881 return lyxrc.completion_popup_math;
1885 CompletionList const *
1886 InsetMathNest::createCompletionList(Cursor const & cur) const
1888 if (!cur.inMacroMode())
1891 return new MathCompletionList(cur);
1895 docstring InsetMathNest::completionPrefix(Cursor const & cur) const
1897 if (!cur.inMacroMode())
1900 return cur.activeMacro()->name();
1904 bool InsetMathNest::insertCompletion(Cursor & cur, docstring const & s,
1907 if (!cur.inMacroMode())
1910 // append completion to active macro
1911 InsetMathUnknown * inset = cur.activeMacro();
1912 inset->setName(inset->name() + s);
1917 // FIXME: this creates duplicates in the completion popup
1918 // which looks ugly. Moreover the changes the list lengths
1920 confuse the popup as well.
1921 MathCompletionList::addToFavorites(inset->name());
1923 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, " "));
1930 void InsetMathNest::completionPosAndDim(Cursor const & cur, int & x, int & y,
1931 Dimension & dim) const
1933 Inset const * inset = cur.activeMacro();
1937 // get inset dimensions
1938 dim = cur.bv().coordCache().insets().dim(inset);
1939 // FIXME: these 3 are no accurate, but should depend on the font.
1940 // Now the popup jumps down if you enter a char with descent > 0.
1945 Point xy = cur.bv().coordCache().insets().xy(inset);
1951 bool InsetMathNest::cursorMathForward(Cursor & cur)
1953 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
1954 cur.pushBackward(*cur.nextAtom().nucleus());
1955 cur.inset().idxFirst(cur);
1958 if (cur.posForward() || idxForward(cur))
1960 // try to pop forwards --- but don't pop out of math! leave that to
1962 int s = cur.depth() - 2;
1963 if (s >= 0 && cur[s].inset().asInsetMath())
1964 return cur.popForward();
1969 bool InsetMathNest::cursorMathBackward(Cursor & cur)
1971 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
1973 cur.push(*cur.nextAtom().nucleus());
1974 cur.inset().idxLast(cur);
1977 if (cur.posBackward() || idxBackward(cur))
1979 // try to pop backwards --- but don't pop out of math! leave that to
1981 int s = cur.depth() - 2;
1982 if (s >= 0 && cur[s].inset().asInsetMath())
1983 return cur.popBackward();
1988 ////////////////////////////////////////////////////////////////////
1990 MathCompletionList::MathCompletionList(Cursor const & cur)
1992 // fill it with macros from the buffer
1993 MacroNameSet macros;
1994 cur.buffer()->listMacroNames(macros);
1995 MacroNameSet::const_iterator it;
1996 for (it = macros.begin(); it != macros.end(); ++it) {
1997 if (cur.buffer()->getMacro(*it, cur, false))
1998 locals.push_back("\\" + *it);
2000 sort(locals.begin(), locals.end());
2002 if (globals.size() > 0)
2005 // fill in global macros
2007 MacroTable::globalMacros().getMacroNames(macros);
2008 //lyxerr << "Globals completion macros: ";
2009 for (it = macros.begin(); it != macros.end(); ++it) {
2010 //lyxerr << "\\" + *it << " ";
2011 globals.push_back("\\" + *it);
2013 //lyxerr << std::endl;
2015 // fill in global commands
2016 globals.push_back(from_ascii("\\boxed"));
2017 globals.push_back(from_ascii("\\fbox"));
2018 globals.push_back(from_ascii("\\framebox"));
2019 globals.push_back(from_ascii("\\makebox"));
2020 globals.push_back(from_ascii("\\kern"));
2021 globals.push_back(from_ascii("\\xrightarrow"));
2022 globals.push_back(from_ascii("\\xleftarrow"));
2023 globals.push_back(from_ascii("\\split"));
2024 globals.push_back(from_ascii("\\gathered"));
2025 globals.push_back(from_ascii("\\aligned"));
2026 globals.push_back(from_ascii("\\alignedat"));
2027 globals.push_back(from_ascii("\\cases"));
2028 globals.push_back(from_ascii("\\substack"));
2029 globals.push_back(from_ascii("\\xymatrix"));
2030 globals.push_back(from_ascii("\\Diagram"));
2031 globals.push_back(from_ascii("\\subarray"));
2032 globals.push_back(from_ascii("\\array"));
2033 globals.push_back(from_ascii("\\sqrt"));
2034 globals.push_back(from_ascii("\\root"));
2035 globals.push_back(from_ascii("\\tabular"));
2036 globals.push_back(from_ascii("\\stackrel"));
2037 globals.push_back(from_ascii("\\binom"));
2038 globals.push_back(from_ascii("\\choose"));
2039 globals.push_back(from_ascii("\\brace"));
2040 globals.push_back(from_ascii("\\brack"));
2041 globals.push_back(from_ascii("\\frac"));
2042 globals.push_back(from_ascii("\\over"));
2043 globals.push_back(from_ascii("\\nicefrac"));
2044 globals.push_back(from_ascii("\\unitfrac"));
2045 globals.push_back(from_ascii("\\unitfracthree"));
2046 globals.push_back(from_ascii("\\unitone"));
2047 globals.push_back(from_ascii("\\unittwo"));
2048 globals.push_back(from_ascii("\\infer"));
2049 globals.push_back(from_ascii("\\atop"));
2050 globals.push_back(from_ascii("\\lefteqn"));
2051 globals.push_back(from_ascii("\\boldsymbol"));
2052 globals.push_back(from_ascii("\\bm"));
2053 globals.push_back(from_ascii("\\color"));
2054 globals.push_back(from_ascii("\\normalcolor"));
2055 globals.push_back(from_ascii("\\textcolor"));
2056 globals.push_back(from_ascii("\\cfrac"));
2057 globals.push_back(from_ascii("\\cfracleft"));
2058 globals.push_back(from_ascii("\\cfracright"));
2059 globals.push_back(from_ascii("\\dfrac"));
2060 globals.push_back(from_ascii("\\tfrac"));
2061 globals.push_back(from_ascii("\\dbinom"));
2062 globals.push_back(from_ascii("\\tbinom"));
2063 globals.push_back(from_ascii("\\hphantom"));
2064 globals.push_back(from_ascii("\\phantom"));
2065 globals.push_back(from_ascii("\\vphantom"));
2066 MathWordList const & words = mathedWordList();
2067 MathWordList::const_iterator it2;
2068 //lyxerr << "Globals completion commands: ";
2069 for (it2 = words.begin(); it2 != words.end(); ++it2) {
2070 globals.push_back("\\" + (*it2).first);
2071 //lyxerr << "\\" + (*it2).first << " ";
2073 //lyxerr << std::endl;
2074 sort(globals.begin(), globals.end());
2078 MathCompletionList::~MathCompletionList()
2083 size_type MathCompletionList::size() const
2085 return locals.size() + globals.size();
2089 docstring const & MathCompletionList::data(size_t idx) const
2091 size_t lsize = locals.size();
2093 return globals[idx - lsize];
2099 std::string MathCompletionList::icon(size_t idx) const
2101 // get the latex command
2103 size_t lsize = locals.size();
2105 cmd = globals[idx - lsize];
2109 // get the icon resource name by stripping the backslash
2110 return "images/math/" + to_utf8(cmd.substr(1)) + ".png";
2113 std::vector<docstring> MathCompletionList::globals;