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_FONT_STYLE: {
984 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, '\\' + cmd.argument());
989 case LFUN_MATH_SIZE: {
990 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, cmd.argument());
995 case LFUN_MATH_MATRIX: {
1001 idocstringstream is(cmd.argument());
1002 is >> m >> n >> v_align >> h_align;
1009 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
1013 case LFUN_MATH_DELIM: {
1015 docstring rs = split(cmd.argument(), ls, ' ');
1016 // Reasonable default values
1022 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
1026 case LFUN_MATH_BIGDELIM: {
1027 docstring const lname = from_utf8(cmd.getArg(0));
1028 docstring const ldelim = from_utf8(cmd.getArg(1));
1029 docstring const rname = from_utf8(cmd.getArg(2));
1030 docstring const rdelim = from_utf8(cmd.getArg(3));
1031 latexkeys const * l = in_word_set(lname);
1032 bool const have_l = l && l->inset == "big" &&
1033 InsetMathBig::isBigInsetDelim(ldelim);
1034 l = in_word_set(rname);
1035 bool const have_r = l && l->inset == "big" &&
1036 InsetMathBig::isBigInsetDelim(rdelim);
1037 // We mimic LFUN_MATH_DELIM in case we have an empty left
1038 // or right delimiter.
1039 if (have_l || have_r) {
1041 docstring const selection = grabAndEraseSelection(cur);
1044 cur.insert(MathAtom(new InsetMathBig(lname,
1046 cur.niceInsert(selection);
1048 cur.insert(MathAtom(new InsetMathBig(rname,
1051 // Don't call cur.undispatched() if we did nothing, this would
1052 // lead to infinite recursion via Text::dispatch().
1056 case LFUN_SPACE_INSERT:
1057 cur.recordUndoSelection();
1058 cur.insert(MathAtom(new InsetMathSpace));
1061 case LFUN_MATH_SPACE:
1062 cur.recordUndoSelection();
1063 if (cmd.argument().empty())
1064 cur.insert(MathAtom(new InsetMathSpace));
1066 string const name = cmd.getArg(0);
1067 string const len = cmd.getArg(1);
1068 cur.insert(MathAtom(new InsetMathSpace(name, len)));
1072 case LFUN_ERT_INSERT:
1073 // interpret this as if a backslash was typed
1075 interpretChar(cur, '\\');
1078 case LFUN_MATH_SUBSCRIPT:
1079 // interpret this as if a _ was typed
1080 cur.recordUndoSelection();
1081 interpretChar(cur, '_');
1084 case LFUN_MATH_SUPERSCRIPT:
1085 // interpret this as if a ^ was typed
1086 cur.recordUndoSelection();
1087 interpretChar(cur, '^');
1090 case LFUN_MATH_MACRO_FOLD:
1091 case LFUN_MATH_MACRO_UNFOLD: {
1093 bool fold = cmd.action == LFUN_MATH_MACRO_FOLD;
1094 bool found = findMacroToFoldUnfold(it, fold);
1096 MathMacro * macro = it.nextInset()->asInsetMath()->asMacro();
1097 cur.recordUndoInset();
1106 case LFUN_QUOTE_INSERT:
1107 // interpret this as if a straight " was typed
1108 cur.recordUndoSelection();
1109 interpretChar(cur, '\"');
1112 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
1113 // handling such that "self-insert" works on "arbitrary stuff" too, and
1114 // math-insert only handles special math things like "matrix".
1115 case LFUN_MATH_INSERT: {
1116 cur.recordUndoSelection();
1117 if (cmd.argument() == "^" || cmd.argument() == "_")
1118 interpretChar(cur, cmd.argument()[0]);
1121 asArray(cmd.argument(), ar);
1122 if (ar.size() == 1 && ar[0]->asNestInset()
1123 && ar[0]->asNestInset()->nargs() > 1)
1124 handleNest(cur, ar[0]);
1126 cur.niceInsert(cmd.argument());
1131 case LFUN_DIALOG_SHOW_NEW_INSET: {
1132 docstring const & name = cmd.argument();
1134 if (name == "ref") {
1135 InsetMathRef tmp(name);
1136 data = tmp.createDialogStr(to_utf8(name));
1137 } else if (name == "mathspace") {
1139 data = tmp.createDialogStr();
1141 cur.bv().showDialog(to_utf8(name), data);
1145 case LFUN_INSET_INSERT: {
1147 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1148 cur.recordUndoSelection();
1154 case LFUN_INSET_DISSOLVE:
1155 if (!asHullInset()) {
1156 cur.recordUndoInset();
1162 InsetMath::doDispatch(cur, cmd);
1168 bool InsetMathNest::findMacroToFoldUnfold(Cursor & it, bool fold) const {
1169 // look for macro to open/close, but stay in mathed
1170 for (; !it.empty(); it.pop_back()) {
1172 // go backward through the current cell
1173 Inset * inset = it.nextInset();
1174 while (inset && inset->asInsetMath()) {
1175 MathMacro * macro = inset->asInsetMath()->asMacro();
1177 // found the an macro to open/close?
1178 if (macro->folded() != fold)
1181 // Wrong folding state.
1182 // If this was the first we see in this slice, look further left,
1184 if (inset != it.nextInset())
1188 // go up if this was the left most position
1194 inset = it.nextInset();
1202 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1203 FuncStatus & flag) const
1205 // the font related toggles
1206 //string tc = "mathnormal";
1208 string const arg = to_utf8(cmd.argument());
1209 switch (cmd.action) {
1210 case LFUN_TABULAR_FEATURE:
1211 flag.setEnabled(false);
1214 case LFUN_TABULAR_FEATURE:
1215 // FIXME: check temporarily disabled
1217 char align = mathcursor::valign();
1218 if (align == '\0') {
1222 if (cmd.argument().empty()) {
1226 if (!contains("tcb", cmd.argument()[0])) {
1230 flag.setOnOff(cmd.argument()[0] == align);
1233 /// We have to handle them since 1.4 blocks all unhandled actions
1234 case LFUN_FONT_ITAL:
1235 case LFUN_FONT_BOLD:
1236 case LFUN_FONT_BOLDSYMBOL:
1237 case LFUN_FONT_SANS:
1238 case LFUN_FONT_EMPH:
1239 case LFUN_FONT_TYPEWRITER:
1240 case LFUN_FONT_NOUN:
1241 case LFUN_FONT_ROMAN:
1242 case LFUN_FONT_DEFAULT:
1243 flag.setEnabled(true);
1245 case LFUN_MATH_MUTATE:
1246 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1247 flag.setOnOff(false);
1250 // we just need to be in math mode to enable that
1251 case LFUN_MATH_SIZE:
1252 case LFUN_MATH_SPACE:
1253 case LFUN_MATH_LIMITS:
1254 case LFUN_MATH_EXTERN:
1255 flag.setEnabled(true);
1258 case LFUN_FONT_FRAK:
1259 flag.setEnabled(currentMode() != TEXT_MODE);
1262 case LFUN_MATH_FONT_STYLE: {
1263 bool const textarg =
1264 arg == "textbf" || arg == "textsf" ||
1265 arg == "textrm" || arg == "textmd" ||
1266 arg == "textit" || arg == "textsc" ||
1267 arg == "textsl" || arg == "textup" ||
1268 arg == "texttt" || arg == "textbb" ||
1269 arg == "textnormal";
1270 flag.setEnabled(currentMode() != TEXT_MODE || textarg);
1274 case LFUN_MATH_INSERT:
1275 flag.setEnabled(currentMode() != TEXT_MODE);
1278 case LFUN_MATH_MATRIX:
1279 flag.setEnabled(currentMode() == MATH_MODE);
1282 case LFUN_INSET_INSERT: {
1283 // Don't test createMathInset_fromDialogStr(), since
1284 // getStatus is not called with a valid reference and the
1285 // dialog would not be applyable.
1286 string const name = cmd.getArg(0);
1287 flag.setEnabled(name == "ref" || name == "mathspace");
1291 case LFUN_MATH_DELIM:
1292 case LFUN_MATH_BIGDELIM:
1293 // Don't do this with multi-cell selections
1294 flag.setEnabled(cur.selBegin().idx() == cur.selEnd().idx());
1297 case LFUN_MATH_MACRO_FOLD:
1298 case LFUN_MATH_MACRO_UNFOLD: {
1300 bool found = findMacroToFoldUnfold(it, cmd.action == LFUN_MATH_MACRO_FOLD);
1301 flag.setEnabled(found);
1305 case LFUN_SPECIALCHAR_INSERT:
1306 // FIXME: These would probably make sense in math-text mode
1307 flag.setEnabled(false);
1310 case LFUN_INSET_DISSOLVE:
1311 flag.setEnabled(!asHullInset());
1322 void InsetMathNest::edit(Cursor & cur, bool front, EntryDirection entry_from)
1325 bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_RIGHT ||
1326 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1327 cur.idx() = enter_front ? 0 : cur.lastidx();
1328 cur.pos() = enter_front ? 0 : cur.lastpos();
1330 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1334 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1337 int dist_min = 1000000;
1338 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1339 int const d = cell(i).dist(cur.bv(), x, y);
1345 MathData & ar = cell(idx_min);
1347 cur.idx() = idx_min;
1348 cur.pos() = ar.x2pos(&cur.bv(), x - ar.xo(cur.bv()));
1350 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1351 if (dist_min == 0) {
1353 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1354 if (ar[i]->covers(cur.bv(), x, y))
1355 return ar[i].nucleus()->editXY(cur, x, y);
1361 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1363 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1364 BufferView & bv = cur.bv();
1365 bool do_selection = cmd.button() == mouse_button::button1
1366 && cmd.argument() == "region-select";
1367 bv.mouseSetCursor(cur, do_selection);
1368 if (cmd.button() == mouse_button::button1) {
1369 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1370 // Update the cursor update flags as needed:
1372 // Update::Decoration: tells to update the decoration
1373 // (visual box corners that define
1375 // Update::FitCursor: adjust the screen to the cursor
1376 // position if needed
1377 // cur.result().update(): don't overwrite previously set flags.
1378 cur.updateFlags(Update::Decoration | Update::FitCursor
1379 | cur.result().update());
1380 } else if (cmd.button() == mouse_button::button2) {
1381 if (cap::selection()) {
1382 // See comment in Text::dispatch why we do this
1383 cap::copySelectionToStack();
1384 cmd = FuncRequest(LFUN_PASTE, "0");
1385 doDispatch(bv.cursor(), cmd);
1388 asArray(theSelection().get(), ar);
1389 bv.cursor().insert(ar);
1395 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1397 // only select with button 1
1398 if (cmd.button() == mouse_button::button1) {
1399 Cursor & bvcur = cur.bv().cursor();
1400 if (bvcur.anchor_.hasPart(cur)) {
1401 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1402 bvcur.setCursor(cur);
1403 bvcur.setSelection(true);
1404 //lyxerr << "MOTION " << bvcur << endl;
1411 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1413 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1415 if (cmd.button() == mouse_button::button1) {
1416 if (!cur.selection())
1419 Cursor & bvcur = cur.bv().cursor();
1420 bvcur.setSelection(true);
1429 bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
1431 //lyxerr << "interpret 2: '" << c << "'" << endl;
1432 docstring save_selection;
1433 if (c == '^' || c == '_')
1434 save_selection = grabAndEraseSelection(cur);
1439 if (cur.inMacroMode()) {
1440 docstring name = cur.macroName();
1442 /// are we currently typing '#1' or '#2' or...?
1443 if (name == "\\#") {
1446 if (n >= 1 && n <= 9)
1447 cur.insert(new MathMacroArgument(n));
1451 // do not finish macro for known * commands
1452 MathWordList const & mwl = mathedWordList();
1453 bool star_macro = c == '*'
1454 && (mwl.find(name.substr(1) + "*") != mwl.end()
1455 || cur.buffer()->getMacro(name.substr(1) + "*", cur, true));
1456 if (isAlphaASCII(c) || star_macro) {
1457 cur.activeMacro()->setName(name + docstring(1, c));
1461 // handle 'special char' macros
1466 if (currentMode() == InsetMath::TEXT_MODE)
1467 cur.niceInsert(createInsetMath("textbackslash"));
1469 cur.niceInsert(createInsetMath("backslash"));
1470 } else if (c == '^' && currentMode() == InsetMath::MATH_MODE) {
1472 cur.niceInsert(createInsetMath("mathcircumflex"));
1473 } else if (c == '{') {
1475 cur.niceInsert(MathAtom(new InsetMathBrace));
1476 } else if (c == '%') {
1478 cur.niceInsert(MathAtom(new InsetMathComment));
1479 } else if (c == '#') {
1480 LASSERT(cur.activeMacro(), /**/);
1481 cur.activeMacro()->setName(name + docstring(1, c));
1484 cur.niceInsert(createInsetMath(docstring(1, c)));
1489 // One character big delimiters. The others are handled in
1490 // interpretString().
1491 latexkeys const * l = in_word_set(name.substr(1));
1492 if (name[0] == '\\' && l && l->inset == "big") {
1496 delim = from_ascii("\\{");
1499 delim = from_ascii("\\}");
1502 delim = docstring(1, c);
1505 if (InsetMathBig::isBigInsetDelim(delim)) {
1506 // name + delim ared a valid InsetMathBig.
1507 // We can't use cur.macroModeClose() because
1508 // it does not handle delim.
1509 InsetMathUnknown * p = cur.activeMacro();
1512 cur.cell().erase(cur.pos());
1513 cur.plainInsert(MathAtom(
1514 new InsetMathBig(name.substr(1), delim)));
1519 // leave macro mode and try again if necessary
1520 cur.macroModeClose();
1522 cur.niceInsert(MathAtom(new InsetMathBrace));
1524 interpretChar(cur, c);
1529 // leave autocorrect mode if necessary
1530 if (lyxrc.autocorrection_math && c == ' ' && cur.autocorrect()) {
1531 cur.autocorrect() = false;
1532 cur.message(_("Autocorrect Off ('!' to enter)"));
1535 if (lyxrc.autocorrection_math && c == '!' && !cur.autocorrect()) {
1536 cur.autocorrect() = true;
1537 cur.message(_("Autocorrect On (<space> to exit)"));
1541 // just clear selection on pressing the space bar
1542 if (cur.selection() && c == ' ') {
1543 cur.setSelection(false);
1548 //lyxerr << "starting with macro" << endl;
1549 bool reduced = cap::reduceSelectionToOneCell(cur);
1550 if (reduced || !cur.selection()) {
1551 docstring const safe = cap::grabAndEraseSelection(cur);
1552 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), safe, false)));
1560 if (currentMode() == InsetMath::TEXT_MODE)
1566 if (currentMode() == InsetMath::TEXT_MODE) {
1567 // insert spaces in text mode,
1568 // but suppress direct insertion of two spaces in a row
1569 // the still allows typing '<space>a<space>' and deleting the 'a', but
1570 // it is better than nothing...
1571 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
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.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1582 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1583 // FIXME: we have to enable full redraw here because of the
1584 // visual box corners that define the inset. If we know for
1585 // sure that we stay within the same cell we can optimize for
1587 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1591 if (cur.popForward()) {
1592 // FIXME: we have to enable full redraw here because of the
1593 // visual box corners that define the inset. If we know for
1594 // sure that we stay within the same cell we can optimize for
1596 //cur.updateFlags(Update::FitCursor);
1600 // if we are at the very end, leave the formula
1601 return cur.pos() != cur.lastpos();
1604 // These should be treated differently when not in text mode:
1605 if (currentMode() != InsetMath::TEXT_MODE) {
1607 script(cur, false, save_selection);
1611 script(cur, true, save_selection);
1615 cur.niceInsert(createInsetMath("sim"));
1620 cur.niceInsert(createInsetMath("textasciicircum"));
1624 cur.niceInsert(createInsetMath("textasciitilde"));
1629 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1630 c == '%' || c == '_') {
1631 cur.niceInsert(createInsetMath(docstring(1, c)));
1636 // try auto-correction
1637 if (lyxrc.autocorrection_math && cur.autocorrect() && cur.pos() != 0
1638 && math_autocorrect(cur.prevAtom(), c))
1641 // no special circumstances, so insert the character without any fuss
1643 if (lyxrc.autocorrection_math) {
1644 if (!cur.autocorrect())
1645 cur.message(_("Autocorrect Off ('!' to enter)"));
1647 cur.message(_("Autocorrect On (<space> to exit)"));
1653 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1655 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1657 if (!cur.empty() && cur.pos() > 0 &&
1658 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1659 if (InsetMathBig::isBigInsetDelim(str)) {
1660 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1661 if (prev[0] == '\\') {
1662 prev = prev.substr(1);
1663 latexkeys const * l = in_word_set(prev);
1664 if (l && l->inset == "big") {
1665 cur.cell()[cur.pos() - 1] =
1666 MathAtom(new InsetMathBig(prev, str));
1676 bool InsetMathNest::script(Cursor & cur, bool up)
1678 return script(cur, up, docstring());
1682 bool InsetMathNest::script(Cursor & cur, bool up,
1683 docstring const & save_selection)
1685 // Hack to get \^ and \_ working
1686 //lyxerr << "handling script: up: " << up << endl;
1687 if (cur.inMacroMode() && cur.macroName() == "\\") {
1689 cur.niceInsert(createInsetMath("mathcircumflex"));
1691 interpretChar(cur, '_');
1695 cur.macroModeClose();
1696 if (asScriptInset() && cur.idx() == 0) {
1697 // we are in a nucleus of a script inset, move to _our_ script
1698 InsetMathScript * inset = asScriptInset();
1699 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1701 cur.idx() = inset->idxOfScript(up);
1703 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1705 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1708 cur.idx() = inset->idxOfScript(up);
1709 cur.pos() = cur.lastpos();
1711 // convert the thing to our left to a scriptinset or create a new
1712 // one if in the very first position of the array
1713 if (cur.pos() == 0) {
1714 //lyxerr << "new scriptinset" << endl;
1715 cur.insert(new InsetMathScript(up));
1717 //lyxerr << "converting prev atom " << endl;
1718 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1721 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1722 // See comment in MathParser.cpp for special handling of {}-bases
1728 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1729 cur.niceInsert(save_selection);
1731 //lyxerr << "inserting selection 2:\n" << save_selection << endl;
1736 bool InsetMathNest::completionSupported(Cursor const & cur) const
1738 return cur.inMacroMode();
1742 bool InsetMathNest::inlineCompletionSupported(Cursor const & cur) const
1744 return cur.inMacroMode();
1748 bool InsetMathNest::automaticInlineCompletion() const
1750 return lyxrc.completion_inline_math;
1754 bool InsetMathNest::automaticPopupCompletion() const
1756 return lyxrc.completion_popup_math;
1760 CompletionList const *
1761 InsetMathNest::createCompletionList(Cursor const & cur) const
1763 if (!cur.inMacroMode())
1766 return new MathCompletionList(cur);
1770 docstring InsetMathNest::completionPrefix(Cursor const & cur) const
1772 if (!cur.inMacroMode())
1775 return cur.activeMacro()->name();
1779 bool InsetMathNest::insertCompletion(Cursor & cur, docstring const & s,
1782 if (!cur.inMacroMode())
1785 // append completion to active macro
1786 InsetMathUnknown * inset = cur.activeMacro();
1787 inset->setName(inset->name() + s);
1792 // FIXME: this creates duplicates in the completion popup
1793 // which looks ugly. Moreover the changes the list lengths
1795 confuse the popup as well.
1796 MathCompletionList::addToFavorites(inset->name());
1798 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, " "));
1805 void InsetMathNest::completionPosAndDim(Cursor const & cur, int & x, int & y,
1806 Dimension & dim) const
1808 Inset const * inset = cur.activeMacro();
1812 // get inset dimensions
1813 dim = cur.bv().coordCache().insets().dim(inset);
1814 // FIXME: these 3 are no accurate, but should depend on the font.
1815 // Now the popup jumps down if you enter a char with descent > 0.
1821 = cur.bv().coordCache().insets().xy(inset);
1827 bool InsetMathNest::cursorMathForward(Cursor & cur)
1829 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
1830 cur.pushBackward(*cur.nextAtom().nucleus());
1831 cur.inset().idxFirst(cur);
1834 if (cur.posForward() || idxForward(cur))
1836 // try to pop forwards --- but don't pop out of math! leave that to
1838 int s = cur.depth() - 2;
1839 if (s >= 0 && cur[s].inset().asInsetMath())
1840 return cur.popForward();
1845 bool InsetMathNest::cursorMathBackward(Cursor & cur)
1847 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
1849 cur.push(*cur.nextAtom().nucleus());
1850 cur.inset().idxLast(cur);
1853 if (cur.posBackward() || idxBackward(cur))
1855 // try to pop backwards --- but don't pop out of math! leave that to
1857 int s = cur.depth() - 2;
1858 if (s >= 0 && cur[s].inset().asInsetMath())
1859 return cur.popBackward();
1864 ////////////////////////////////////////////////////////////////////
1866 MathCompletionList::MathCompletionList(Cursor const & cur)
1868 // fill it with macros from the buffer
1869 MacroNameSet macros;
1870 cur.buffer()->listMacroNames(macros);
1871 MacroNameSet::const_iterator it;
1872 for (it = macros.begin(); it != macros.end(); ++it) {
1873 if (cur.buffer()->getMacro(*it, cur, false))
1874 locals.push_back("\\" + *it);
1876 sort(locals.begin(), locals.end());
1878 if (globals.size() > 0)
1881 // fill in global macros
1883 MacroTable::globalMacros().getMacroNames(macros);
1884 //lyxerr << "Globals completion macros: ";
1885 for (it = macros.begin(); it != macros.end(); ++it) {
1886 //lyxerr << "\\" + *it << " ";
1887 globals.push_back("\\" + *it);
1889 //lyxerr << std::endl;
1891 // fill in global commands
1892 globals.push_back(from_ascii("\\boxed"));
1893 globals.push_back(from_ascii("\\fbox"));
1894 globals.push_back(from_ascii("\\framebox"));
1895 globals.push_back(from_ascii("\\makebox"));
1896 globals.push_back(from_ascii("\\kern"));
1897 globals.push_back(from_ascii("\\xrightarrow"));
1898 globals.push_back(from_ascii("\\xleftarrow"));
1899 globals.push_back(from_ascii("\\split"));
1900 globals.push_back(from_ascii("\\gathered"));
1901 globals.push_back(from_ascii("\\aligned"));
1902 globals.push_back(from_ascii("\\alignedat"));
1903 globals.push_back(from_ascii("\\cases"));
1904 globals.push_back(from_ascii("\\substack"));
1905 globals.push_back(from_ascii("\\xymatrix"));
1906 globals.push_back(from_ascii("\\subarray"));
1907 globals.push_back(from_ascii("\\array"));
1908 globals.push_back(from_ascii("\\sqrt"));
1909 globals.push_back(from_ascii("\\root"));
1910 globals.push_back(from_ascii("\\tabular"));
1911 globals.push_back(from_ascii("\\stackrel"));
1912 globals.push_back(from_ascii("\\binom"));
1913 globals.push_back(from_ascii("\\choose"));
1914 globals.push_back(from_ascii("\\brace"));
1915 globals.push_back(from_ascii("\\brack"));
1916 globals.push_back(from_ascii("\\frac"));
1917 globals.push_back(from_ascii("\\over"));
1918 globals.push_back(from_ascii("\\nicefrac"));
1919 globals.push_back(from_ascii("\\unitfrac"));
1920 globals.push_back(from_ascii("\\unitfracthree"));
1921 globals.push_back(from_ascii("\\unitone"));
1922 globals.push_back(from_ascii("\\unittwo"));
1923 globals.push_back(from_ascii("\\infer"));
1924 globals.push_back(from_ascii("\\atop"));
1925 globals.push_back(from_ascii("\\lefteqn"));
1926 globals.push_back(from_ascii("\\boldsymbol"));
1927 globals.push_back(from_ascii("\\bm"));
1928 globals.push_back(from_ascii("\\color"));
1929 globals.push_back(from_ascii("\\normalcolor"));
1930 globals.push_back(from_ascii("\\textcolor"));
1931 globals.push_back(from_ascii("\\dfrac"));
1932 globals.push_back(from_ascii("\\tfrac"));
1933 globals.push_back(from_ascii("\\dbinom"));
1934 globals.push_back(from_ascii("\\tbinom"));
1935 globals.push_back(from_ascii("\\hphantom"));
1936 globals.push_back(from_ascii("\\phantom"));
1937 globals.push_back(from_ascii("\\vphantom"));
1938 MathWordList const & words = mathedWordList();
1939 MathWordList::const_iterator it2;
1940 //lyxerr << "Globals completion commands: ";
1941 for (it2 = words.begin(); it2 != words.end(); ++it2) {
1942 globals.push_back("\\" + (*it2).first);
1943 //lyxerr << "\\" + (*it2).first << " ";
1945 //lyxerr << std::endl;
1946 sort(globals.begin(), globals.end());
1950 MathCompletionList::~MathCompletionList()
1955 size_type MathCompletionList::size() const
1957 return locals.size() + globals.size();
1961 docstring const & MathCompletionList::data(size_t idx) const
1963 size_t lsize = locals.size();
1965 return globals[idx - lsize];
1971 std::string MathCompletionList::icon(size_t idx) const
1973 // get the latex command
1975 size_t lsize = locals.size();
1977 cmd = globals[idx - lsize];
1981 // get the icon resource name by stripping the backslash
1982 return "images/math/" + to_utf8(cmd.substr(1)) + ".png";
1985 std::vector<docstring> MathCompletionList::globals;