2 * \file InsetMathNest.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetMathNest.h"
15 #include "InsetMathArray.h"
16 #include "InsetMathBig.h"
17 #include "InsetMathBox.h"
18 #include "InsetMathBrace.h"
19 #include "InsetMathColor.h"
20 #include "InsetMathComment.h"
21 #include "InsetMathDelim.h"
22 #include "InsetMathHull.h"
23 #include "InsetMathRef.h"
24 #include "InsetMathScript.h"
25 #include "InsetMathSpace.h"
26 #include "InsetMathSymbol.h"
27 #include "InsetMathUnknown.h"
28 #include "MathAutoCorrect.h"
29 #include "MathCompletionList.h"
31 #include "MathFactory.h"
32 #include "MathMacro.h"
33 #include "MathMacroArgument.h"
34 #include "MathParser.h"
35 #include "MathStream.h"
36 #include "MathSupport.h"
40 #include "BufferView.h"
41 #include "CoordCache.h"
43 #include "CutAndPaste.h"
44 #include "DispatchResult.h"
45 #include "FuncRequest.h"
46 #include "FuncStatus.h"
49 #include "OutputParams.h"
52 #include "frontends/Clipboard.h"
53 #include "frontends/Painter.h"
54 #include "frontends/Selection.h"
56 #include "support/lassert.h"
57 #include "support/debug.h"
58 #include "support/gettext.h"
59 #include "support/lstrings.h"
60 #include "support/textutils.h"
61 #include "support/docstream.h"
67 using namespace lyx::support;
71 using cap::copySelection;
72 using cap::grabAndEraseSelection;
73 using cap::cutSelection;
74 using cap::replaceSelection;
75 using cap::selClearOrDel;
78 InsetMathNest::InsetMathNest(idx_type nargs)
79 : cells_(nargs), lock_(false), mouse_hover_(false)
83 InsetMathNest::InsetMathNest(InsetMathNest const & inset)
84 : InsetMath(inset), cells_(inset.cells_), lock_(inset.lock_),
89 InsetMathNest & InsetMathNest::operator=(InsetMathNest const & inset)
91 cells_ = inset.cells_;
94 InsetMath::operator=(inset);
99 InsetMath::idx_type InsetMathNest::nargs() const
101 return cells_.size();
105 void InsetMathNest::cursorPos(BufferView const & bv,
106 CursorSlice const & sl, bool /*boundary*/,
107 int & x, int & y) const
109 // FIXME: This is a hack. Ideally, the coord cache should not store
110 // absolute positions, but relative ones. This would mean to call
111 // setXY() not in MathData::draw(), but in the parent insets' draw()
112 // with the correctly adjusted x,y values. But this means that we'd have
113 // to touch all (math)inset's draw() methods. Right now, we'll store
114 // absolute value, and make them here relative, only to make them
115 // absolute again when actually drawing the cursor. What a mess.
116 LASSERT(&sl.inset() == this, /**/);
117 MathData const & ar = sl.cell();
118 CoordCache const & coord_cache = bv.coordCache();
119 if (!coord_cache.getArrays().has(&ar)) {
120 // this can (semi-)legally happen if we just created this cell
121 // and it never has been drawn before. So don't ASSERT.
122 //lyxerr << "no cached data for array " << &ar << endl;
127 Point const pt = coord_cache.getArrays().xy(&ar);
128 if (!coord_cache.getInsets().has(this)) {
130 //lyxerr << "no cached data for inset " << this << endl;
135 Point const pt2 = coord_cache.getInsets().xy(this);
136 //lyxerr << "retrieving position cache for MathData "
137 // << pt.x_ << ' ' << pt.y_ << endl;
138 x = pt.x_ - pt2.x_ + ar.pos2x(&bv, sl.pos());
140 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
141 // << " asc: " << ascent() << " des: " << descent()
142 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
143 // move cursor visually into empty cells ("blue rectangles");
149 void InsetMathNest::metrics(MetricsInfo const & mi) const
152 for (idx_type i = 0, n = nargs(); i != n; ++i) {
154 cell(i).metrics(m, dim);
159 bool InsetMathNest::idxNext(Cursor & cur) const
161 LASSERT(&cur.inset() == this, /**/);
162 if (cur.idx() == cur.lastidx())
170 bool InsetMathNest::idxForward(Cursor & cur) const
176 bool InsetMathNest::idxPrev(Cursor & cur) const
178 LASSERT(&cur.inset() == this, /**/);
182 cur.pos() = cur.lastpos();
187 bool InsetMathNest::idxBackward(Cursor & cur) const
193 bool InsetMathNest::idxFirst(Cursor & cur) const
195 LASSERT(&cur.inset() == this, /**/);
204 bool InsetMathNest::idxLast(Cursor & cur) const
206 LASSERT(&cur.inset() == this, /**/);
209 cur.idx() = cur.lastidx();
210 cur.pos() = cur.lastpos();
215 void InsetMathNest::dump() const
217 odocstringstream oss;
219 os << "---------------------------------------------\n";
222 for (idx_type i = 0, n = nargs(); i != n; ++i)
223 os << cell(i) << "\n";
224 os << "---------------------------------------------\n";
225 lyxerr << to_utf8(oss.str());
229 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
233 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
236 setPosCache(pi, x, y);
240 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
242 BufferView & bv = *pi.base.bv;
243 // this should use the x/y values given, not the cached values
244 Cursor & cur = bv.cursor();
245 if (!cur.selection())
247 if (&cur.inset() != this)
250 // FIXME: hack to get position cache warm
251 bool const original_drawing_state = pi.pain.isDrawingEnabled();
252 pi.pain.setDrawingEnabled(false);
254 pi.pain.setDrawingEnabled(original_drawing_state);
256 CursorSlice s1 = cur.selBegin();
257 CursorSlice s2 = cur.selEnd();
259 //lyxerr << "InsetMathNest::drawing selection: "
260 // << " s1: " << s1 << " s2: " << s2 << endl;
261 if (s1.idx() == s2.idx()) {
262 MathData const & c = cell(s1.idx());
263 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
264 int x1 = g.pos.x_ + c.pos2x(pi.base.bv, s1.pos());
265 int y1 = g.pos.y_ - g.dim.ascent();
266 int x2 = g.pos.x_ + c.pos2x(pi.base.bv, s2.pos());
267 int y2 = g.pos.y_ + g.dim.descent();
268 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
269 //lyxerr << "InsetMathNest::drawing selection 3: "
270 // << " x1: " << x1 << " x2: " << x2
271 // << " y1: " << y1 << " y2: " << y2 << endl;
273 for (idx_type i = 0; i < nargs(); ++i) {
274 if (idxBetween(i, s1.idx(), s2.idx())) {
275 MathData const & c = cell(i);
276 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
278 int y1 = g.pos.y_ - g.dim.ascent();
279 int x2 = g.pos.x_ + g.dim.width();
280 int y2 = g.pos.y_ + g.dim.descent();
281 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
288 void InsetMathNest::validate(LaTeXFeatures & features) const
290 for (idx_type i = 0; i < nargs(); ++i)
291 cell(i).validate(features);
295 void InsetMathNest::replace(ReplaceData & rep)
297 for (idx_type i = 0; i < nargs(); ++i)
298 cell(i).replace(rep);
302 bool InsetMathNest::contains(MathData const & ar) const
304 for (idx_type i = 0; i < nargs(); ++i)
305 if (cell(i).contains(ar))
311 bool InsetMathNest::lock() const
317 void InsetMathNest::lock(bool l)
323 bool InsetMathNest::isActive() const
329 MathData InsetMathNest::glue() const
332 for (size_t i = 0; i < nargs(); ++i)
338 void InsetMathNest::write(WriteStream & os) const
340 ModeSpecifier specifier(os, currentMode());
341 docstring const latex_name = name();
342 os << '\\' << latex_name;
343 for (size_t i = 0; i < nargs(); ++i)
344 os << '{' << cell(i) << '}';
346 os.pendingSpace(true);
347 if (lock_ && !os.latex()) {
349 os.pendingSpace(true);
354 void InsetMathNest::normalize(NormalStream & os) const
357 for (size_t i = 0; i < nargs(); ++i)
358 os << ' ' << cell(i);
363 int InsetMathNest::latex(odocstream & os, OutputParams const & runparams) const
365 WriteStream wi(os, runparams.moving_arg, true, runparams.dryrun,
372 bool InsetMathNest::setMouseHover(bool mouse_hover)
374 mouse_hover_ = mouse_hover;
379 bool InsetMathNest::notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
383 MathData & ar = cur.cell();
384 // remove base-only "scripts"
385 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
386 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
387 if (p && p->nargs() == 1) {
388 MathData ar = p->nuc();
391 cur.adjust(i, ar.size() - 1);
395 // glue adjacent font insets of the same kind
396 for (pos_type i = 0; i + 1 < size(); ++i) {
397 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
398 InsetMathFont const * q = operator[](i + 1)->asFontInset();
399 if (p && q && p->name() == q->name()) {
400 p->cell(0).append(q->cell(0));
410 void InsetMathNest::handleFont
411 (Cursor & cur, docstring const & arg, char const * const font)
413 handleFont(cur, arg, from_ascii(font));
417 void InsetMathNest::handleFont(Cursor & cur, docstring const & arg,
418 docstring const & font)
420 cur.recordUndoSelection();
422 // this whole function is a hack and won't work for incremental font
424 if (cur.inset().asInsetMath()->name() == font)
425 cur.handleFont(to_utf8(font));
427 handleNest(cur, createInsetMath(font), arg);
431 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest)
433 handleNest(cur, nest, docstring());
437 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest,
438 docstring const & arg)
440 CursorSlice i1 = cur.selBegin();
441 CursorSlice i2 = cur.selEnd();
442 if (!i1.inset().asInsetMath())
444 if (i1.idx() == i2.idx()) {
445 // the easy case where only one cell is selected
446 cur.handleNest(nest);
451 // multiple selected cells in a simple non-grid inset
452 if (i1.asInsetMath()->nrows() == 0 || i1.asInsetMath()->ncols() == 0) {
453 for (idx_type i = i1.idx(); i <= i2.idx(); ++i) {
458 cur.pos() = cur.lastpos();
461 // change font of cell
462 cur.handleNest(nest);
465 // cur is in the font inset now. If the loop continues,
466 // we need to get outside again for the next cell
467 if (i + 1 <= i2.idx())
473 // the complicated case with multiple selected cells in a grid
476 cap::region(i1, i2, r1, r2, c1, c2);
477 for (row_type row = r1; row <= r2; ++row) {
478 for (col_type col = c1; col <= c2; ++col) {
480 cur.idx() = i1.asInsetMath()->index(row, col);
483 cur.pos() = cur.lastpos();
487 cur.handleNest(nest);
490 // cur is in the font inset now. If the loop continues,
491 // we need to get outside again for the next cell
492 if (col + 1 <= c2 || row + 1 <= r2)
499 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
501 cur.recordUndoSelection();
504 font.fromString(to_utf8(arg), b);
505 if (font.fontInfo().color() != Color_inherit &&
506 font.fontInfo().color() != Color_ignore)
507 handleNest(cur, MathAtom(new InsetMathColor(true, font.fontInfo().color())));
509 // FIXME: support other font changes here as well?
513 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
515 //lyxerr << "InsetMathNest: request: " << cmd << endl;
517 Parse::flags parseflg = Parse::QUIET;
519 switch (cmd.action) {
521 case LFUN_CLIPBOARD_PASTE:
522 parseflg |= Parse::VERBATIM;
525 if (cur.currentMode() == TEXT_MODE)
526 parseflg |= Parse::TEXTMODE;
527 cur.recordUndoSelection();
528 cur.message(_("Paste"));
529 replaceSelection(cur);
531 if (cmd.argument().empty() && !theClipboard().isInternal())
532 topaste = theClipboard().getAsText();
535 idocstringstream is(cmd.argument());
537 topaste = cap::selection(n);
539 cur.niceInsert(topaste, parseflg);
540 cur.clearSelection(); // bug 393
547 cutSelection(cur, true, true);
548 cur.message(_("Cut"));
549 // Prevent stale position >= size crash
550 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
556 cur.message(_("Copy"));
559 case LFUN_MOUSE_PRESS:
560 lfunMousePress(cur, cmd);
563 case LFUN_MOUSE_MOTION:
564 lfunMouseMotion(cur, cmd);
567 case LFUN_MOUSE_RELEASE:
568 lfunMouseRelease(cur, cmd);
571 case LFUN_FINISHED_LEFT: // in math, left is backwards
572 case LFUN_FINISHED_BACKWARD:
573 cur.bv().cursor() = cur;
576 case LFUN_FINISHED_RIGHT: // in math, right is forward
577 case LFUN_FINISHED_FORWARD:
579 cur.bv().cursor() = cur;
582 case LFUN_CHAR_RIGHT:
584 case LFUN_CHAR_BACKWARD:
585 case LFUN_CHAR_FORWARD:
586 cur.updateFlags(Update::Decoration | Update::FitCursor);
587 case LFUN_CHAR_RIGHT_SELECT:
588 case LFUN_CHAR_LEFT_SELECT:
589 case LFUN_CHAR_BACKWARD_SELECT:
590 case LFUN_CHAR_FORWARD_SELECT: {
591 // are we in a selection?
592 bool select = (cmd.action == LFUN_CHAR_RIGHT_SELECT
593 || cmd.action == LFUN_CHAR_LEFT_SELECT
594 || cmd.action == LFUN_CHAR_BACKWARD_SELECT
595 || cmd.action == LFUN_CHAR_FORWARD_SELECT);
596 // are we moving forward or backwards?
597 // If the command was RIGHT or LEFT, then whether we're moving forward
598 // or backwards depends on the cursor movement mode (logical or visual):
599 // * in visual mode, since math is always LTR, right -> forward,
601 // * in logical mode, the mapping is determined by the
602 // reverseDirectionNeeded() function
605 FuncCode finish_lfun;
607 if (cmd.action == LFUN_CHAR_FORWARD
608 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
610 finish_lfun = LFUN_FINISHED_FORWARD;
612 else if (cmd.action == LFUN_CHAR_BACKWARD
613 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
615 finish_lfun = LFUN_FINISHED_BACKWARD;
618 bool right = (cmd.action == LFUN_CHAR_RIGHT_SELECT
619 || cmd.action == LFUN_CHAR_RIGHT);
620 if (lyxrc.visual_cursor || !reverseDirectionNeeded(cur))
626 finish_lfun = LFUN_FINISHED_RIGHT;
628 finish_lfun = LFUN_FINISHED_LEFT;
630 // Now that we know exactly what we want to do, let's do it!
631 cur.selHandle(select);
633 cur.macroModeClose();
634 // try moving forward or backwards as necessary...
635 if (!(forward ? cursorMathForward(cur) : cursorMathBackward(cur))) {
636 // ... and if movement failed, then finish forward or backwards
638 cmd = FuncRequest(finish_lfun);
646 cur.updateFlags(Update::Decoration | Update::FitCursor);
647 case LFUN_DOWN_SELECT:
648 case LFUN_UP_SELECT: {
649 // close active macro
650 if (cur.inMacroMode()) {
651 cur.macroModeClose();
655 // stop/start the selection
656 bool select = cmd.action == LFUN_DOWN_SELECT ||
657 cmd.action == LFUN_UP_SELECT;
658 cur.selHandle(select);
661 bool up = cmd.action == LFUN_UP || cmd.action == LFUN_UP_SELECT;
662 bool successful = cur.upDownInMath(up);
666 if (cur.fixIfBroken())
667 // FIXME: Something bad happened. We pass the corrected Cursor
668 // instead of letting things go worse.
671 // We did not manage to move the cursor.
676 case LFUN_MOUSE_DOUBLE:
677 case LFUN_MOUSE_TRIPLE:
678 case LFUN_WORD_SELECT:
682 cur.setSelection(true);
683 cur.pos() = cur.lastpos();
684 cur.idx() = cur.lastidx();
687 case LFUN_PARAGRAPH_UP:
688 case LFUN_PARAGRAPH_DOWN:
689 cur.updateFlags(Update::Decoration | Update::FitCursor);
690 case LFUN_PARAGRAPH_UP_SELECT:
691 case LFUN_PARAGRAPH_DOWN_SELECT:
694 case LFUN_LINE_BEGIN:
695 case LFUN_WORD_BACKWARD:
697 cur.updateFlags(Update::Decoration | Update::FitCursor);
698 case LFUN_LINE_BEGIN_SELECT:
699 case LFUN_WORD_BACKWARD_SELECT:
700 case LFUN_WORD_LEFT_SELECT:
701 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
702 cmd.action == LFUN_WORD_LEFT_SELECT ||
703 cmd.action == LFUN_LINE_BEGIN_SELECT);
704 cur.macroModeClose();
705 if (cur.pos() != 0) {
707 } else if (cur.col() != 0) {
708 cur.idx() -= cur.col();
710 } else if (cur.idx() != 0) {
714 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
719 case LFUN_WORD_FORWARD:
720 case LFUN_WORD_RIGHT:
722 cur.updateFlags(Update::Decoration | Update::FitCursor);
723 case LFUN_WORD_FORWARD_SELECT:
724 case LFUN_WORD_RIGHT_SELECT:
725 case LFUN_LINE_END_SELECT:
726 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
727 cmd.action == LFUN_WORD_RIGHT_SELECT ||
728 cmd.action == LFUN_LINE_END_SELECT);
729 cur.macroModeClose();
731 if (cur.pos() != cur.lastpos()) {
732 cur.pos() = cur.lastpos();
733 } else if (ncols() && (cur.col() != cur.lastcol())) {
734 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
735 cur.pos() = cur.lastpos();
736 } else if (cur.idx() != cur.lastidx()) {
737 cur.idx() = cur.lastidx();
738 cur.pos() = cur.lastpos();
740 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
745 case LFUN_CELL_FORWARD:
746 cur.updateFlags(Update::Decoration | Update::FitCursor);
747 cur.inset().idxNext(cur);
750 case LFUN_CELL_BACKWARD:
751 cur.updateFlags(Update::Decoration | Update::FitCursor);
752 cur.inset().idxPrev(cur);
755 case LFUN_WORD_DELETE_BACKWARD:
756 case LFUN_CHAR_DELETE_BACKWARD:
758 // May affect external cell:
759 cur.recordUndoInset();
761 cur.recordUndoSelection();
762 // if the inset can not be removed from within, delete it
763 if (!cur.backspace()) {
764 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
765 cur.innerText()->dispatch(cur, cmd);
769 case LFUN_WORD_DELETE_FORWARD:
770 case LFUN_CHAR_DELETE_FORWARD:
771 if (cur.pos() == cur.lastpos())
772 // May affect external cell:
773 cur.recordUndoInset();
775 cur.recordUndoSelection();
776 // if the inset can not be removed from within, delete it
778 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
779 cur.innerText()->dispatch(cur, cmd);
785 cur.clearSelection();
787 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
792 // 'Locks' the math inset. A 'locked' math inset behaves as a unit
793 // that is traversed by a single <CursorLeft>/<CursorRight>.
794 case LFUN_INSET_TOGGLE:
800 case LFUN_SELF_INSERT:
801 if (cmd.argument().size() != 1) {
802 cur.recordUndoSelection();
803 docstring const arg = cmd.argument();
804 if (!interpretString(cur, arg))
808 // Don't record undo steps if we are in macro mode and
809 // cmd.argument is the next character of the macro name.
810 // Otherwise we'll get an invalid cursor if we undo after
811 // the macro was finished and the macro is a known command,
812 // e.g. sqrt. Cursor::macroModeClose replaces in this case
813 // the InsetMathUnknown with name "frac" by an empty
814 // InsetMathFrac -> a pos value > 0 is invalid.
815 // A side effect is that an undo before the macro is finished
816 // undoes the complete macro, not only the last character.
817 if (!cur.inMacroMode())
818 cur.recordUndoSelection();
820 // spacial handling of space. If we insert an inset
821 // via macro mode, we want to put the cursor inside it
822 // if relevant. Think typing "\frac<space>".
823 if (cmd.argument()[0] == ' '
824 && cur.inMacroMode() && cur.macroName() != "\\"
825 && cur.macroModeClose()) {
826 MathAtom const atom = cur.prevAtom();
827 if (atom->asNestInset() && atom->isActive()) {
829 cur.pushBackward(*cur.nextInset());
831 } else if (!interpretChar(cur, cmd.argument()[0])) {
832 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
837 //case LFUN_SERVER_GET_XY:
840 case LFUN_SERVER_SET_XY: {
841 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
844 istringstream is(to_utf8(cmd.argument()));
846 cur.setScreenPos(x, y);
850 // Special casing for superscript in case of LyX handling
852 case LFUN_ACCENT_CIRCUMFLEX:
853 if (cmd.argument().empty()) {
854 // do superscript if LyX handles
856 cur.recordUndoSelection();
857 script(cur, true, grabAndEraseSelection(cur));
861 case LFUN_ACCENT_UMLAUT:
862 case LFUN_ACCENT_ACUTE:
863 case LFUN_ACCENT_GRAVE:
864 case LFUN_ACCENT_BREVE:
865 case LFUN_ACCENT_DOT:
866 case LFUN_ACCENT_MACRON:
867 case LFUN_ACCENT_CARON:
868 case LFUN_ACCENT_TILDE:
869 case LFUN_ACCENT_CEDILLA:
870 case LFUN_ACCENT_CIRCLE:
871 case LFUN_ACCENT_UNDERDOT:
872 case LFUN_ACCENT_TIE:
873 case LFUN_ACCENT_OGONEK:
874 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
878 case LFUN_TEXTSTYLE_APPLY:
879 case LFUN_TEXTSTYLE_UPDATE:
880 handleFont2(cur, cmd.argument());
884 if (currentMode() == TEXT_MODE)
885 handleFont(cur, cmd.argument(), "textbf");
887 handleFont(cur, cmd.argument(), "mathbf");
889 case LFUN_FONT_BOLDSYMBOL:
890 if (currentMode() == TEXT_MODE)
891 handleFont(cur, cmd.argument(), "textbf");
893 handleFont(cur, cmd.argument(), "boldsymbol");
896 if (currentMode() == TEXT_MODE)
897 handleFont(cur, cmd.argument(), "textsf");
899 handleFont(cur, cmd.argument(), "mathsf");
902 if (currentMode() == TEXT_MODE)
903 handleFont(cur, cmd.argument(), "emph");
905 handleFont(cur, cmd.argument(), "mathcal");
907 case LFUN_FONT_ROMAN:
908 if (currentMode() == TEXT_MODE)
909 handleFont(cur, cmd.argument(), "textrm");
911 handleFont(cur, cmd.argument(), "mathrm");
913 case LFUN_FONT_TYPEWRITER:
914 if (currentMode() == TEXT_MODE)
915 handleFont(cur, cmd.argument(), "texttt");
917 handleFont(cur, cmd.argument(), "mathtt");
920 handleFont(cur, cmd.argument(), "mathfrak");
923 if (currentMode() == TEXT_MODE)
924 handleFont(cur, cmd.argument(), "textit");
926 handleFont(cur, cmd.argument(), "mathit");
929 if (currentMode() == TEXT_MODE)
930 // FIXME: should be "noun"
931 handleFont(cur, cmd.argument(), "textsc");
933 handleFont(cur, cmd.argument(), "mathbb");
935 case LFUN_FONT_DEFAULT:
936 handleFont(cur, cmd.argument(), "textnormal");
939 case LFUN_MATH_MODE: {
941 // ignore math-mode on when already in math mode
942 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
944 cur.recordUndoSelection();
945 cur.macroModeClose();
946 docstring const save_selection = grabAndEraseSelection(cur);
948 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
949 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
951 cur.pushBackward(*cur.nextInset());
952 cur.niceInsert(save_selection);
954 if (currentMode() == Inset::TEXT_MODE) {
955 cur.recordUndoSelection();
956 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
957 cur.message(_("create new math text environment ($...$)"));
959 handleFont(cur, cmd.argument(), "textrm");
960 cur.message(_("entered math text mode (textrm)"));
966 case LFUN_REGEXP_MODE: {
967 InsetMathHull * i = dynamic_cast<InsetMathHull *>(cur.inset().asInsetMath());
968 if (i && i->getType() == hullRegexp) {
969 cur.message(_("Already in regexp mode"));
972 cur.macroModeClose();
973 docstring const save_selection = grabAndEraseSelection(cur);
975 cur.plainInsert(MathAtom(new InsetMathHull(hullRegexp)));
977 cur.pushBackward(*cur.nextInset());
978 cur.niceInsert(save_selection);
979 cur.message(_("Regexp editor mode"));
983 case LFUN_MATH_SIZE: {
984 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, cmd.argument());
989 case LFUN_MATH_MATRIX: {
995 idocstringstream is(cmd.argument());
996 is >> m >> n >> v_align >> h_align;
1003 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
1007 case LFUN_MATH_DELIM: {
1009 docstring rs = split(cmd.argument(), ls, ' ');
1010 // Reasonable default values
1016 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
1020 case LFUN_MATH_BIGDELIM: {
1021 docstring const lname = from_utf8(cmd.getArg(0));
1022 docstring const ldelim = from_utf8(cmd.getArg(1));
1023 docstring const rname = from_utf8(cmd.getArg(2));
1024 docstring const rdelim = from_utf8(cmd.getArg(3));
1025 latexkeys const * l = in_word_set(lname);
1026 bool const have_l = l && l->inset == "big" &&
1027 InsetMathBig::isBigInsetDelim(ldelim);
1028 l = in_word_set(rname);
1029 bool const have_r = l && l->inset == "big" &&
1030 InsetMathBig::isBigInsetDelim(rdelim);
1031 // We mimic LFUN_MATH_DELIM in case we have an empty left
1032 // or right delimiter.
1033 if (have_l || have_r) {
1035 docstring const selection = grabAndEraseSelection(cur);
1038 cur.insert(MathAtom(new InsetMathBig(lname,
1040 cur.niceInsert(selection);
1042 cur.insert(MathAtom(new InsetMathBig(rname,
1045 // Don't call cur.undispatched() if we did nothing, this would
1046 // lead to infinite recursion via Text::dispatch().
1050 case LFUN_SPACE_INSERT:
1051 cur.recordUndoSelection();
1052 cur.insert(MathAtom(new InsetMathSpace));
1055 case LFUN_MATH_SPACE:
1056 cur.recordUndoSelection();
1057 if (cmd.argument().empty())
1058 cur.insert(MathAtom(new InsetMathSpace));
1060 string const name = cmd.getArg(0);
1061 string const len = cmd.getArg(1);
1062 cur.insert(MathAtom(new InsetMathSpace(name, len)));
1066 case LFUN_ERT_INSERT:
1067 // interpret this as if a backslash was typed
1069 interpretChar(cur, '\\');
1072 case LFUN_MATH_SUBSCRIPT:
1073 // interpret this as if a _ was typed
1074 cur.recordUndoSelection();
1075 interpretChar(cur, '_');
1078 case LFUN_MATH_SUPERSCRIPT:
1079 // interpret this as if a ^ was typed
1080 cur.recordUndoSelection();
1081 interpretChar(cur, '^');
1084 case LFUN_MATH_MACRO_FOLD:
1085 case LFUN_MATH_MACRO_UNFOLD: {
1087 bool fold = cmd.action == LFUN_MATH_MACRO_FOLD;
1088 bool found = findMacroToFoldUnfold(it, fold);
1090 MathMacro * macro = it.nextInset()->asInsetMath()->asMacro();
1091 cur.recordUndoInset();
1100 case LFUN_QUOTE_INSERT:
1101 // interpret this as if a straight " was typed
1102 cur.recordUndoSelection();
1103 interpretChar(cur, '\"');
1106 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
1107 // handling such that "self-insert" works on "arbitrary stuff" too, and
1108 // math-insert only handles special math things like "matrix".
1109 case LFUN_MATH_INSERT: {
1110 cur.recordUndoSelection();
1111 if (cmd.argument() == "^" || cmd.argument() == "_")
1112 interpretChar(cur, cmd.argument()[0]);
1115 asArray(cmd.argument(), ar);
1116 if (ar.size() == 1 && ar[0]->asNestInset()
1117 && ar[0]->asNestInset()->nargs() > 1)
1118 handleNest(cur, ar[0]);
1120 cur.niceInsert(cmd.argument());
1125 case LFUN_DIALOG_SHOW_NEW_INSET: {
1126 docstring const & name = cmd.argument();
1128 if (name == "ref") {
1129 InsetMathRef tmp(name);
1130 data = tmp.createDialogStr(to_utf8(name));
1131 } else if (name == "mathspace") {
1133 data = tmp.createDialogStr();
1135 cur.bv().showDialog(to_utf8(name), data);
1139 case LFUN_INSET_INSERT: {
1141 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1142 cur.recordUndoSelection();
1148 case LFUN_INSET_DISSOLVE:
1149 if (!asHullInset()) {
1150 cur.recordUndoInset();
1156 InsetMath::doDispatch(cur, cmd);
1162 bool InsetMathNest::findMacroToFoldUnfold(Cursor & it, bool fold) const {
1163 // look for macro to open/close, but stay in mathed
1164 for (; !it.empty(); it.pop_back()) {
1166 // go backward through the current cell
1167 Inset * inset = it.nextInset();
1168 while (inset && inset->asInsetMath()) {
1169 MathMacro * macro = inset->asInsetMath()->asMacro();
1171 // found the an macro to open/close?
1172 if (macro->folded() != fold)
1175 // Wrong folding state.
1176 // If this was the first we see in this slice, look further left,
1178 if (inset != it.nextInset())
1182 // go up if this was the left most position
1188 inset = it.nextInset();
1196 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1197 FuncStatus & flag) const
1199 // the font related toggles
1200 //string tc = "mathnormal";
1202 string const arg = to_utf8(cmd.argument());
1203 switch (cmd.action) {
1204 case LFUN_TABULAR_FEATURE:
1205 flag.setEnabled(false);
1208 case LFUN_TABULAR_FEATURE:
1209 // FIXME: check temporarily disabled
1211 char align = mathcursor::valign();
1212 if (align == '\0') {
1216 if (cmd.argument().empty()) {
1220 if (!contains("tcb", cmd.argument()[0])) {
1224 flag.setOnOff(cmd.argument()[0] == align);
1227 /// We have to handle them since 1.4 blocks all unhandled actions
1228 case LFUN_FONT_ITAL:
1229 case LFUN_FONT_BOLD:
1230 case LFUN_FONT_BOLDSYMBOL:
1231 case LFUN_FONT_SANS:
1232 case LFUN_FONT_EMPH:
1233 case LFUN_FONT_TYPEWRITER:
1234 case LFUN_FONT_NOUN:
1235 case LFUN_FONT_ROMAN:
1236 case LFUN_FONT_DEFAULT:
1237 flag.setEnabled(true);
1239 case LFUN_MATH_MUTATE:
1240 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1241 flag.setOnOff(false);
1244 // we just need to be in math mode to enable that
1245 case LFUN_MATH_SIZE:
1246 case LFUN_MATH_SPACE:
1247 case LFUN_MATH_LIMITS:
1248 case LFUN_MATH_EXTERN:
1249 flag.setEnabled(true);
1252 case LFUN_FONT_FRAK:
1253 flag.setEnabled(currentMode() != TEXT_MODE);
1256 case LFUN_MATH_INSERT: {
1257 bool const textarg =
1258 arg == "\\textbf" || arg == "\\textsf" ||
1259 arg == "\\textrm" || arg == "\\textmd" ||
1260 arg == "\\textit" || arg == "\\textsc" ||
1261 arg == "\\textsl" || arg == "\\textup" ||
1262 arg == "\\texttt" || arg == "\\textbb" ||
1263 arg == "\\textnormal";
1264 flag.setEnabled(currentMode() != TEXT_MODE || textarg);
1268 case LFUN_MATH_MATRIX:
1269 flag.setEnabled(currentMode() == MATH_MODE);
1272 case LFUN_INSET_INSERT: {
1273 // Don't test createMathInset_fromDialogStr(), since
1274 // getStatus is not called with a valid reference and the
1275 // dialog would not be applyable.
1276 string const name = cmd.getArg(0);
1277 flag.setEnabled(name == "ref" || name == "mathspace");
1281 case LFUN_MATH_DELIM:
1282 case LFUN_MATH_BIGDELIM:
1283 // Don't do this with multi-cell selections
1284 flag.setEnabled(cur.selBegin().idx() == cur.selEnd().idx());
1287 case LFUN_MATH_MACRO_FOLD:
1288 case LFUN_MATH_MACRO_UNFOLD: {
1290 bool found = findMacroToFoldUnfold(it, cmd.action == LFUN_MATH_MACRO_FOLD);
1291 flag.setEnabled(found);
1295 case LFUN_SPECIALCHAR_INSERT:
1296 // FIXME: These would probably make sense in math-text mode
1297 flag.setEnabled(false);
1300 case LFUN_INSET_DISSOLVE:
1301 flag.setEnabled(!asHullInset());
1312 void InsetMathNest::edit(Cursor & cur, bool front, EntryDirection entry_from)
1315 bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_RIGHT ||
1316 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1317 cur.idx() = enter_front ? 0 : cur.lastidx();
1318 cur.pos() = enter_front ? 0 : cur.lastpos();
1320 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1324 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1327 int dist_min = 1000000;
1328 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1329 int const d = cell(i).dist(cur.bv(), x, y);
1335 MathData & ar = cell(idx_min);
1337 cur.idx() = idx_min;
1338 cur.pos() = ar.x2pos(&cur.bv(), x - ar.xo(cur.bv()));
1340 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1341 if (dist_min == 0) {
1343 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1344 if (ar[i]->covers(cur.bv(), x, y))
1345 return ar[i].nucleus()->editXY(cur, x, y);
1351 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1353 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1354 BufferView & bv = cur.bv();
1355 bool do_selection = cmd.button() == mouse_button::button1
1356 && cmd.argument() == "region-select";
1357 bv.mouseSetCursor(cur, do_selection);
1358 if (cmd.button() == mouse_button::button1) {
1359 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1360 // Update the cursor update flags as needed:
1362 // Update::Decoration: tells to update the decoration
1363 // (visual box corners that define
1365 // Update::FitCursor: adjust the screen to the cursor
1366 // position if needed
1367 // cur.result().update(): don't overwrite previously set flags.
1368 cur.updateFlags(Update::Decoration | Update::FitCursor
1369 | cur.result().update());
1370 } else if (cmd.button() == mouse_button::button2) {
1371 if (cap::selection()) {
1372 // See comment in Text::dispatch why we do this
1373 cap::copySelectionToStack();
1374 cmd = FuncRequest(LFUN_PASTE, "0");
1375 doDispatch(bv.cursor(), cmd);
1378 asArray(theSelection().get(), ar);
1379 bv.cursor().insert(ar);
1385 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1387 // only select with button 1
1388 if (cmd.button() == mouse_button::button1) {
1389 Cursor & bvcur = cur.bv().cursor();
1390 if (bvcur.anchor_.hasPart(cur)) {
1391 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1392 bvcur.setCursor(cur);
1393 bvcur.setSelection(true);
1394 //lyxerr << "MOTION " << bvcur << endl;
1401 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1403 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1405 if (cmd.button() == mouse_button::button1) {
1406 if (!cur.selection())
1409 Cursor & bvcur = cur.bv().cursor();
1410 bvcur.setSelection(true);
1419 bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
1421 //lyxerr << "interpret 2: '" << c << "'" << endl;
1422 docstring save_selection;
1423 if (c == '^' || c == '_')
1424 save_selection = grabAndEraseSelection(cur);
1429 if (cur.inMacroMode()) {
1430 docstring name = cur.macroName();
1432 /// are we currently typing '#1' or '#2' or...?
1433 if (name == "\\#") {
1436 if (n >= 1 && n <= 9)
1437 cur.insert(new MathMacroArgument(n));
1441 // do not finish macro for known * commands
1442 MathWordList const & mwl = mathedWordList();
1443 bool star_macro = c == '*'
1444 && (mwl.find(name.substr(1) + "*") != mwl.end()
1445 || cur.buffer()->getMacro(name.substr(1) + "*", cur, true));
1446 if (isAlphaASCII(c) || star_macro) {
1447 cur.activeMacro()->setName(name + docstring(1, c));
1451 // handle 'special char' macros
1456 if (currentMode() == InsetMath::TEXT_MODE)
1457 cur.niceInsert(createInsetMath("textbackslash"));
1459 cur.niceInsert(createInsetMath("backslash"));
1460 } else if (c == '^' && currentMode() == InsetMath::MATH_MODE) {
1462 cur.niceInsert(createInsetMath("mathcircumflex"));
1463 } else if (c == '{') {
1465 cur.niceInsert(MathAtom(new InsetMathBrace));
1466 } else if (c == '%') {
1468 cur.niceInsert(MathAtom(new InsetMathComment));
1469 } else if (c == '#') {
1470 LASSERT(cur.activeMacro(), /**/);
1471 cur.activeMacro()->setName(name + docstring(1, c));
1474 cur.niceInsert(createInsetMath(docstring(1, c)));
1479 // One character big delimiters. The others are handled in
1480 // interpretString().
1481 latexkeys const * l = in_word_set(name.substr(1));
1482 if (name[0] == '\\' && l && l->inset == "big") {
1486 delim = from_ascii("\\{");
1489 delim = from_ascii("\\}");
1492 delim = docstring(1, c);
1495 if (InsetMathBig::isBigInsetDelim(delim)) {
1496 // name + delim ared a valid InsetMathBig.
1497 // We can't use cur.macroModeClose() because
1498 // it does not handle delim.
1499 InsetMathUnknown * p = cur.activeMacro();
1502 cur.cell().erase(cur.pos());
1503 cur.plainInsert(MathAtom(
1504 new InsetMathBig(name.substr(1), delim)));
1509 // leave macro mode and try again if necessary
1510 cur.macroModeClose();
1512 cur.niceInsert(MathAtom(new InsetMathBrace));
1514 interpretChar(cur, c);
1519 // leave autocorrect mode if necessary
1520 if (lyxrc.autocorrection_math && c == ' ' && cur.autocorrect()) {
1521 cur.autocorrect() = false;
1522 cur.message(_("Autocorrect Off ('!' to enter)"));
1525 if (lyxrc.autocorrection_math && c == '!' && !cur.autocorrect()) {
1526 cur.autocorrect() = true;
1527 cur.message(_("Autocorrect On (<space> to exit)"));
1531 // just clear selection on pressing the space bar
1532 if (cur.selection() && c == ' ') {
1533 cur.setSelection(false);
1538 //lyxerr << "starting with macro" << endl;
1539 bool reduced = cap::reduceSelectionToOneCell(cur);
1540 if (reduced || !cur.selection()) {
1541 docstring const safe = cap::grabAndEraseSelection(cur);
1542 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), safe, false)));
1550 if (currentMode() == InsetMath::TEXT_MODE)
1556 if (currentMode() == InsetMath::TEXT_MODE) {
1557 // insert spaces in text mode,
1558 // but suppress direct insertion of two spaces in a row
1559 // the still allows typing '<space>a<space>' and deleting the 'a', but
1560 // it is better than nothing...
1561 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1563 // FIXME: we have to enable full redraw here because of the
1564 // visual box corners that define the inset. If we know for
1565 // sure that we stay within the same cell we can optimize for
1567 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1571 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1572 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1573 // FIXME: we have to enable full redraw here because of the
1574 // visual box corners that define the inset. If we know for
1575 // sure that we stay within the same cell we can optimize for
1577 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1581 if (cur.popForward()) {
1582 // FIXME: we have to enable full redraw here because of the
1583 // visual box corners that define the inset. If we know for
1584 // sure that we stay within the same cell we can optimize for
1586 //cur.updateFlags(Update::FitCursor);
1590 // if we are at the very end, leave the formula
1591 return cur.pos() != cur.lastpos();
1594 // These should be treated differently when not in text mode:
1595 if (currentMode() != InsetMath::TEXT_MODE) {
1597 script(cur, false, save_selection);
1601 script(cur, true, save_selection);
1605 cur.niceInsert(createInsetMath("sim"));
1610 cur.niceInsert(createInsetMath("textasciicircum"));
1614 cur.niceInsert(createInsetMath("textasciitilde"));
1619 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1620 c == '%' || c == '_') {
1621 cur.niceInsert(createInsetMath(docstring(1, c)));
1626 // try auto-correction
1627 if (lyxrc.autocorrection_math && cur.autocorrect() && cur.pos() != 0 && math_autocorrect(cur.prevAtom(), c))
1630 // no special circumstances, so insert the character without any fuss
1632 if (lyxrc.autocorrection_math) {
1633 if (!cur.autocorrect())
1634 cur.message(_("Autocorrect Off ('!' to enter)"));
1636 cur.message(_("Autocorrect On (<space> to exit)"));
1642 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1644 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1646 if (!cur.empty() && cur.pos() > 0 &&
1647 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1648 if (InsetMathBig::isBigInsetDelim(str)) {
1649 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1650 if (prev[0] == '\\') {
1651 prev = prev.substr(1);
1652 latexkeys const * l = in_word_set(prev);
1653 if (l && l->inset == "big") {
1654 cur.cell()[cur.pos() - 1] =
1655 MathAtom(new InsetMathBig(prev, str));
1665 bool InsetMathNest::script(Cursor & cur, bool up)
1667 return script(cur, up, docstring());
1671 bool InsetMathNest::script(Cursor & cur, bool up,
1672 docstring const & save_selection)
1674 // Hack to get \^ and \_ working
1675 //lyxerr << "handling script: up: " << up << endl;
1676 if (cur.inMacroMode() && cur.macroName() == "\\") {
1678 cur.niceInsert(createInsetMath("mathcircumflex"));
1680 interpretChar(cur, '_');
1684 cur.macroModeClose();
1685 if (asScriptInset() && cur.idx() == 0) {
1686 // we are in a nucleus of a script inset, move to _our_ script
1687 InsetMathScript * inset = asScriptInset();
1688 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1690 cur.idx() = inset->idxOfScript(up);
1692 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1694 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1697 cur.idx() = inset->idxOfScript(up);
1698 cur.pos() = cur.lastpos();
1700 // convert the thing to our left to a scriptinset or create a new
1701 // one if in the very first position of the array
1702 if (cur.pos() == 0) {
1703 //lyxerr << "new scriptinset" << endl;
1704 cur.insert(new InsetMathScript(up));
1706 //lyxerr << "converting prev atom " << endl;
1707 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1710 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1711 // See comment in MathParser.cpp for special handling of {}-bases
1717 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1718 cur.niceInsert(save_selection);
1720 //lyxerr << "inserting selection 2:\n" << save_selection << endl;
1725 bool InsetMathNest::completionSupported(Cursor const & cur) const
1727 return cur.inMacroMode();
1731 bool InsetMathNest::inlineCompletionSupported(Cursor const & cur) const
1733 return cur.inMacroMode();
1737 bool InsetMathNest::automaticInlineCompletion() const
1739 return lyxrc.completion_inline_math;
1743 bool InsetMathNest::automaticPopupCompletion() const
1745 return lyxrc.completion_popup_math;
1749 CompletionList const *
1750 InsetMathNest::createCompletionList(Cursor const & cur) const
1752 if (!cur.inMacroMode())
1755 return new MathCompletionList(cur);
1759 docstring InsetMathNest::completionPrefix(Cursor const & cur) const
1761 if (!cur.inMacroMode())
1764 return cur.activeMacro()->name();
1768 bool InsetMathNest::insertCompletion(Cursor & cur, docstring const & s,
1771 if (!cur.inMacroMode())
1774 // append completion to active macro
1775 InsetMathUnknown * inset = cur.activeMacro();
1776 inset->setName(inset->name() + s);
1781 // FIXME: this creates duplicates in the completion popup
1782 // which looks ugly. Moreover the changes the list lengths
1784 confuse the popup as well.
1785 MathCompletionList::addToFavorites(inset->name());
1787 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, " "));
1794 void InsetMathNest::completionPosAndDim(Cursor const & cur, int & x, int & y,
1795 Dimension & dim) const
1797 Inset const * inset = cur.activeMacro();
1801 // get inset dimensions
1802 dim = cur.bv().coordCache().insets().dim(inset);
1803 // FIXME: these 3 are no accurate, but should depend on the font.
1804 // Now the popup jumps down if you enter a char with descent > 0.
1810 = cur.bv().coordCache().insets().xy(inset);
1816 bool InsetMathNest::cursorMathForward(Cursor & cur)
1818 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
1819 cur.pushBackward(*cur.nextAtom().nucleus());
1820 cur.inset().idxFirst(cur);
1823 if (cur.posForward() || idxForward(cur))
1825 // try to pop forwards --- but don't pop out of math! leave that to
1827 int s = cur.depth() - 2;
1828 if (s >= 0 && cur[s].inset().asInsetMath())
1829 return cur.popForward();
1834 bool InsetMathNest::cursorMathBackward(Cursor & cur)
1836 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
1838 cur.push(*cur.nextAtom().nucleus());
1839 cur.inset().idxLast(cur);
1842 if (cur.posBackward() || idxBackward(cur))
1844 // try to pop backwards --- but don't pop out of math! leave that to
1846 int s = cur.depth() - 2;
1847 if (s >= 0 && cur[s].inset().asInsetMath())
1848 return cur.popBackward();
1853 ////////////////////////////////////////////////////////////////////
1855 MathCompletionList::MathCompletionList(Cursor const & cur)
1857 // fill it with macros from the buffer
1858 MacroNameSet macros;
1859 cur.buffer()->listMacroNames(macros);
1860 MacroNameSet::const_iterator it;
1861 for (it = macros.begin(); it != macros.end(); ++it) {
1862 if (cur.buffer()->getMacro(*it, cur, false))
1863 locals.push_back("\\" + *it);
1865 sort(locals.begin(), locals.end());
1867 if (globals.size() > 0)
1870 // fill in global macros
1872 MacroTable::globalMacros().getMacroNames(macros);
1873 //lyxerr << "Globals completion macros: ";
1874 for (it = macros.begin(); it != macros.end(); ++it) {
1875 //lyxerr << "\\" + *it << " ";
1876 globals.push_back("\\" + *it);
1878 //lyxerr << std::endl;
1880 // fill in global commands
1881 globals.push_back(from_ascii("\\boxed"));
1882 globals.push_back(from_ascii("\\fbox"));
1883 globals.push_back(from_ascii("\\framebox"));
1884 globals.push_back(from_ascii("\\makebox"));
1885 globals.push_back(from_ascii("\\kern"));
1886 globals.push_back(from_ascii("\\xrightarrow"));
1887 globals.push_back(from_ascii("\\xleftarrow"));
1888 globals.push_back(from_ascii("\\split"));
1889 globals.push_back(from_ascii("\\gathered"));
1890 globals.push_back(from_ascii("\\aligned"));
1891 globals.push_back(from_ascii("\\alignedat"));
1892 globals.push_back(from_ascii("\\cases"));
1893 globals.push_back(from_ascii("\\substack"));
1894 globals.push_back(from_ascii("\\xymatrix"));
1895 globals.push_back(from_ascii("\\subarray"));
1896 globals.push_back(from_ascii("\\array"));
1897 globals.push_back(from_ascii("\\sqrt"));
1898 globals.push_back(from_ascii("\\root"));
1899 globals.push_back(from_ascii("\\tabular"));
1900 globals.push_back(from_ascii("\\stackrel"));
1901 globals.push_back(from_ascii("\\binom"));
1902 globals.push_back(from_ascii("\\choose"));
1903 globals.push_back(from_ascii("\\brace"));
1904 globals.push_back(from_ascii("\\brack"));
1905 globals.push_back(from_ascii("\\frac"));
1906 globals.push_back(from_ascii("\\over"));
1907 globals.push_back(from_ascii("\\nicefrac"));
1908 globals.push_back(from_ascii("\\unitfrac"));
1909 globals.push_back(from_ascii("\\unitfracthree"));
1910 globals.push_back(from_ascii("\\unitone"));
1911 globals.push_back(from_ascii("\\unittwo"));
1912 globals.push_back(from_ascii("\\infer"));
1913 globals.push_back(from_ascii("\\atop"));
1914 globals.push_back(from_ascii("\\lefteqn"));
1915 globals.push_back(from_ascii("\\boldsymbol"));
1916 globals.push_back(from_ascii("\\bm"));
1917 globals.push_back(from_ascii("\\color"));
1918 globals.push_back(from_ascii("\\normalcolor"));
1919 globals.push_back(from_ascii("\\textcolor"));
1920 globals.push_back(from_ascii("\\dfrac"));
1921 globals.push_back(from_ascii("\\tfrac"));
1922 globals.push_back(from_ascii("\\dbinom"));
1923 globals.push_back(from_ascii("\\tbinom"));
1924 globals.push_back(from_ascii("\\hphantom"));
1925 globals.push_back(from_ascii("\\phantom"));
1926 globals.push_back(from_ascii("\\vphantom"));
1927 MathWordList const & words = mathedWordList();
1928 MathWordList::const_iterator it2;
1929 //lyxerr << "Globals completion commands: ";
1930 for (it2 = words.begin(); it2 != words.end(); ++it2) {
1931 globals.push_back("\\" + (*it2).first);
1932 //lyxerr << "\\" + (*it2).first << " ";
1934 //lyxerr << std::endl;
1935 sort(globals.begin(), globals.end());
1939 MathCompletionList::~MathCompletionList()
1944 size_type MathCompletionList::size() const
1946 return locals.size() + globals.size();
1950 docstring const & MathCompletionList::data(size_t idx) const
1952 size_t lsize = locals.size();
1954 return globals[idx - lsize];
1960 std::string MathCompletionList::icon(size_t idx) const
1962 // get the latex command
1964 size_t lsize = locals.size();
1966 cmd = globals[idx - lsize];
1970 // get the icon resource name by stripping the backslash
1971 return "images/math/" + to_utf8(cmd.substr(1)) + ".png";
1974 std::vector<docstring> MathCompletionList::globals;