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.
15 #include "InsetMathNest.h"
17 #include "InsetMathArray.h"
18 #include "InsetMathBig.h"
19 #include "InsetMathBox.h"
20 #include "InsetMathBrace.h"
21 #include "InsetMathColor.h"
22 #include "InsetMathComment.h"
23 #include "InsetMathDelim.h"
24 #include "InsetMathHull.h"
25 #include "InsetMathRef.h"
26 #include "InsetMathScript.h"
27 #include "InsetMathSpace.h"
28 #include "InsetMathSymbol.h"
29 #include "InsetMathUnknown.h"
31 #include "MathAutoCorrect.h"
33 #include "MathCompletionList.h"
35 #include "MathFactory.h"
36 #include "MathMacro.h"
37 #include "MathMacroArgument.h"
38 #include "MathParser.h"
39 #include "MathStream.h"
40 #include "MathSupport.h"
44 #include "BufferView.h"
45 #include "CoordCache.h"
47 #include "CutAndPaste.h"
48 #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(idx_type nargs)
83 : cells_(nargs), lock_(false), mouse_hover_(false)
87 InsetMathNest::InsetMathNest(InsetMathNest const & inset)
88 : InsetMath(inset), cells_(inset.cells_), lock_(inset.lock_),
93 InsetMathNest & InsetMathNest::operator=(InsetMathNest const & inset)
95 cells_ = inset.cells_;
98 InsetMath::operator=(inset);
103 InsetMath::idx_type InsetMathNest::nargs() const
105 return cells_.size();
109 void InsetMathNest::cursorPos(BufferView const & bv,
110 CursorSlice const & sl, bool /*boundary*/,
111 int & x, int & y) const
113 // FIXME: This is a hack. Ideally, the coord cache should not store
114 // absolute positions, but relative ones. This would mean to call
115 // setXY() not in MathData::draw(), but in the parent insets' draw()
116 // with the correctly adjusted x,y values. But this means that we'd have
117 // to touch all (math)inset's draw() methods. Right now, we'll store
118 // absolute value, and make them here relative, only to make them
119 // absolute again when actually drawing the cursor. What a mess.
120 LASSERT(&sl.inset() == this, /**/);
121 MathData const & ar = sl.cell();
122 CoordCache const & coord_cache = bv.coordCache();
123 if (!coord_cache.getArrays().has(&ar)) {
124 // this can (semi-)legally happen if we just created this cell
125 // and it never has been drawn before. So don't ASSERT.
126 //lyxerr << "no cached data for array " << &ar << endl;
131 Point const pt = coord_cache.getArrays().xy(&ar);
132 if (!coord_cache.getInsets().has(this)) {
134 //lyxerr << "no cached data for inset " << this << endl;
139 Point const pt2 = coord_cache.getInsets().xy(this);
140 //lyxerr << "retrieving position cache for MathData "
141 // << pt.x_ << ' ' << pt.y_ << endl;
142 x = pt.x_ - pt2.x_ + ar.pos2x(&bv, sl.pos());
144 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
145 // << " asc: " << ascent() << " des: " << descent()
146 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
147 // move cursor visually into empty cells ("blue rectangles");
153 void InsetMathNest::metrics(MetricsInfo const & mi) const
156 for (idx_type i = 0, n = nargs(); i != n; ++i) {
158 cell(i).metrics(m, dim);
163 bool InsetMathNest::idxNext(Cursor & cur) const
165 LASSERT(&cur.inset() == this, /**/);
166 if (cur.idx() == cur.lastidx())
174 bool InsetMathNest::idxForward(Cursor & cur) const
180 bool InsetMathNest::idxPrev(Cursor & cur) const
182 LASSERT(&cur.inset() == this, /**/);
186 cur.pos() = cur.lastpos();
191 bool InsetMathNest::idxBackward(Cursor & cur) const
197 bool InsetMathNest::idxFirst(Cursor & cur) const
199 LASSERT(&cur.inset() == this, /**/);
208 bool InsetMathNest::idxLast(Cursor & cur) const
210 LASSERT(&cur.inset() == this, /**/);
213 cur.idx() = cur.lastidx();
214 cur.pos() = cur.lastpos();
219 void InsetMathNest::dump() const
221 odocstringstream oss;
223 os << "---------------------------------------------\n";
226 for (idx_type i = 0, n = nargs(); i != n; ++i)
227 os << cell(i) << "\n";
228 os << "---------------------------------------------\n";
229 lyxerr << to_utf8(oss.str());
233 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
237 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
240 setPosCache(pi, x, y);
244 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
246 BufferView & bv = *pi.base.bv;
247 // this should use the x/y values given, not the cached values
248 Cursor & cur = bv.cursor();
249 if (!cur.selection())
251 if (&cur.inset() != this)
254 // FIXME: hack to get position cache warm
255 bool const original_drawing_state = pi.pain.isDrawingEnabled();
256 pi.pain.setDrawingEnabled(false);
258 pi.pain.setDrawingEnabled(original_drawing_state);
260 CursorSlice s1 = cur.selBegin();
261 CursorSlice s2 = cur.selEnd();
263 //lyxerr << "InsetMathNest::drawing selection: "
264 // << " s1: " << s1 << " s2: " << s2 << endl;
265 if (s1.idx() == s2.idx()) {
266 MathData const & c = cell(s1.idx());
267 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
268 int x1 = g.pos.x_ + c.pos2x(pi.base.bv, s1.pos());
269 int y1 = g.pos.y_ - g.dim.ascent();
270 int x2 = g.pos.x_ + c.pos2x(pi.base.bv, s2.pos());
271 int y2 = g.pos.y_ + g.dim.descent();
272 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
273 //lyxerr << "InsetMathNest::drawing selection 3: "
274 // << " x1: " << x1 << " x2: " << x2
275 // << " y1: " << y1 << " y2: " << y2 << endl;
277 for (idx_type i = 0; i < nargs(); ++i) {
278 if (idxBetween(i, s1.idx(), s2.idx())) {
279 MathData const & c = cell(i);
280 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
282 int y1 = g.pos.y_ - g.dim.ascent();
283 int x2 = g.pos.x_ + g.dim.width();
284 int y2 = g.pos.y_ + g.dim.descent();
285 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
292 void InsetMathNest::validate(LaTeXFeatures & features) const
294 for (idx_type i = 0; i < nargs(); ++i)
295 cell(i).validate(features);
299 void InsetMathNest::replace(ReplaceData & rep)
301 for (idx_type i = 0; i < nargs(); ++i)
302 cell(i).replace(rep);
306 bool InsetMathNest::contains(MathData const & ar) const
308 for (idx_type i = 0; i < nargs(); ++i)
309 if (cell(i).contains(ar))
315 bool InsetMathNest::lock() const
321 void InsetMathNest::lock(bool l)
327 bool InsetMathNest::isActive() const
333 MathData InsetMathNest::glue() const
336 for (size_t i = 0; i < nargs(); ++i)
342 void InsetMathNest::write(WriteStream & os) const
344 ModeSpecifier specifier(os, currentMode());
345 docstring const latex_name = name();
346 os << '\\' << latex_name;
347 for (size_t i = 0; i < nargs(); ++i)
348 os << '{' << cell(i) << '}';
350 os.pendingSpace(true);
351 if (lock_ && !os.latex()) {
353 os.pendingSpace(true);
358 void InsetMathNest::normalize(NormalStream & os) const
361 for (size_t i = 0; i < nargs(); ++i)
362 os << ' ' << cell(i);
367 int InsetMathNest::latex(odocstream & os, OutputParams const & runparams) const
369 WriteStream wi(os, runparams.moving_arg, true, runparams.dryrun,
376 bool InsetMathNest::setMouseHover(bool mouse_hover)
378 mouse_hover_ = mouse_hover;
383 bool InsetMathNest::notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
387 MathData & ar = cur.cell();
388 // remove base-only "scripts"
389 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
390 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
391 if (p && p->nargs() == 1) {
392 MathData ar = p->nuc();
395 cur.adjust(i, ar.size() - 1);
399 // glue adjacent font insets of the same kind
400 for (pos_type i = 0; i + 1 < size(); ++i) {
401 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
402 InsetMathFont const * q = operator[](i + 1)->asFontInset();
403 if (p && q && p->name() == q->name()) {
404 p->cell(0).append(q->cell(0));
414 void InsetMathNest::handleFont
415 (Cursor & cur, docstring const & arg, char const * const font)
417 handleFont(cur, arg, from_ascii(font));
421 void InsetMathNest::handleFont(Cursor & cur, docstring const & arg,
422 docstring const & font)
424 cur.recordUndoSelection();
426 // this whole function is a hack and won't work for incremental font
428 if (cur.inset().asInsetMath()->name() == font)
429 cur.handleFont(to_utf8(font));
431 handleNest(cur, createInsetMath(font), arg);
435 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest)
437 handleNest(cur, nest, docstring());
441 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest,
442 docstring const & arg)
444 CursorSlice i1 = cur.selBegin();
445 CursorSlice i2 = cur.selEnd();
446 if (!i1.inset().asInsetMath())
448 if (i1.idx() == i2.idx()) {
449 // the easy case where only one cell is selected
450 cur.handleNest(nest);
455 // multiple selected cells in a simple non-grid inset
456 if (i1.asInsetMath()->nrows() == 0 || i1.asInsetMath()->ncols() == 0) {
457 for (idx_type i = i1.idx(); i <= i2.idx(); ++i) {
462 cur.pos() = cur.lastpos();
465 // change font of cell
466 cur.handleNest(nest);
469 // cur is in the font inset now. If the loop continues,
470 // we need to get outside again for the next cell
471 if (i + 1 <= i2.idx())
477 // the complicated case with multiple selected cells in a grid
480 cap::region(i1, i2, r1, r2, c1, c2);
481 for (row_type row = r1; row <= r2; ++row) {
482 for (col_type col = c1; col <= c2; ++col) {
484 cur.idx() = i1.asInsetMath()->index(row, col);
487 cur.pos() = cur.lastpos();
491 cur.handleNest(nest);
494 // cur is in the font inset now. If the loop continues,
495 // we need to get outside again for the next cell
496 if (col + 1 <= c2 || row + 1 <= r2)
503 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
505 cur.recordUndoSelection();
508 font.fromString(to_utf8(arg), b);
509 if (font.fontInfo().color() != Color_inherit &&
510 font.fontInfo().color() != Color_ignore)
511 handleNest(cur, MathAtom(new InsetMathColor(true, font.fontInfo().color())));
513 // FIXME: support other font changes here as well?
517 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
519 //lyxerr << "InsetMathNest: request: " << cmd << endl;
521 Parse::flags parseflg = Parse::QUIET;
523 switch (cmd.action) {
525 case LFUN_CLIPBOARD_PASTE:
526 parseflg |= Parse::VERBATIM;
529 if (cur.currentMode() == TEXT_MODE)
530 parseflg |= Parse::TEXTMODE;
531 cur.recordUndoSelection();
532 cur.message(_("Paste"));
533 replaceSelection(cur);
535 if (cmd.argument().empty() && !theClipboard().isInternal())
536 topaste = theClipboard().getAsText();
539 idocstringstream is(cmd.argument());
541 topaste = cap::selection(n);
543 cur.niceInsert(topaste, parseflg);
544 cur.clearSelection(); // bug 393
551 cutSelection(cur, true, true);
552 cur.message(_("Cut"));
553 // Prevent stale position >= size crash
554 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
560 cur.message(_("Copy"));
563 case LFUN_MOUSE_PRESS:
564 lfunMousePress(cur, cmd);
567 case LFUN_MOUSE_MOTION:
568 lfunMouseMotion(cur, cmd);
571 case LFUN_MOUSE_RELEASE:
572 lfunMouseRelease(cur, cmd);
575 case LFUN_FINISHED_LEFT: // in math, left is backwards
576 case LFUN_FINISHED_BACKWARD:
577 cur.bv().cursor() = cur;
580 case LFUN_FINISHED_RIGHT: // in math, right is forward
581 case LFUN_FINISHED_FORWARD:
583 cur.bv().cursor() = cur;
586 case LFUN_CHAR_RIGHT:
588 case LFUN_CHAR_BACKWARD:
589 case LFUN_CHAR_FORWARD:
590 cur.updateFlags(Update::Decoration | Update::FitCursor);
591 case LFUN_CHAR_RIGHT_SELECT:
592 case LFUN_CHAR_LEFT_SELECT:
593 case LFUN_CHAR_BACKWARD_SELECT:
594 case LFUN_CHAR_FORWARD_SELECT: {
595 // are we in a selection?
596 bool select = (cmd.action == LFUN_CHAR_RIGHT_SELECT
597 || cmd.action == LFUN_CHAR_LEFT_SELECT
598 || cmd.action == LFUN_CHAR_BACKWARD_SELECT
599 || cmd.action == LFUN_CHAR_FORWARD_SELECT);
600 // are we moving forward or backwards?
601 // If the command was RIGHT or LEFT, then whether we're moving forward
602 // or backwards depends on the cursor movement mode (logical or visual):
603 // * in visual mode, since math is always LTR, right -> forward,
605 // * in logical mode, the mapping is determined by the
606 // reverseDirectionNeeded() function
609 FuncCode finish_lfun;
611 if (cmd.action == LFUN_CHAR_FORWARD
612 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
614 finish_lfun = LFUN_FINISHED_FORWARD;
616 else if (cmd.action == LFUN_CHAR_BACKWARD
617 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
619 finish_lfun = LFUN_FINISHED_BACKWARD;
622 bool right = (cmd.action == LFUN_CHAR_RIGHT_SELECT
623 || cmd.action == LFUN_CHAR_RIGHT);
624 if (lyxrc.visual_cursor || !reverseDirectionNeeded(cur))
630 finish_lfun = LFUN_FINISHED_RIGHT;
632 finish_lfun = LFUN_FINISHED_LEFT;
634 // Now that we know exactly what we want to do, let's do it!
635 cur.selHandle(select);
636 cur.autocorrect() = false;
638 cur.macroModeClose();
639 // try moving forward or backwards as necessary...
640 if (!(forward ? cursorMathForward(cur) : cursorMathBackward(cur))) {
641 // ... and if movement failed, then finish forward or backwards
643 cmd = FuncRequest(finish_lfun);
651 cur.updateFlags(Update::Decoration | Update::FitCursor);
652 case LFUN_DOWN_SELECT:
653 case LFUN_UP_SELECT: {
654 // close active macro
655 if (cur.inMacroMode()) {
656 cur.macroModeClose();
660 // stop/start the selection
661 bool select = cmd.action == LFUN_DOWN_SELECT ||
662 cmd.action == LFUN_UP_SELECT;
663 cur.selHandle(select);
666 bool up = cmd.action == LFUN_UP || cmd.action == LFUN_UP_SELECT;
667 bool successful = cur.upDownInMath(up);
671 if (cur.fixIfBroken())
672 // FIXME: Something bad happened. We pass the corrected Cursor
673 // instead of letting things go worse.
676 // We did not manage to move the cursor.
681 case LFUN_MOUSE_DOUBLE:
682 case LFUN_MOUSE_TRIPLE:
683 case LFUN_WORD_SELECT:
687 cur.setSelection(true);
688 cur.pos() = cur.lastpos();
689 cur.idx() = cur.lastidx();
692 case LFUN_PARAGRAPH_UP:
693 case LFUN_PARAGRAPH_DOWN:
694 cur.updateFlags(Update::Decoration | Update::FitCursor);
695 case LFUN_PARAGRAPH_UP_SELECT:
696 case LFUN_PARAGRAPH_DOWN_SELECT:
699 case LFUN_LINE_BEGIN:
700 case LFUN_WORD_BACKWARD:
702 cur.updateFlags(Update::Decoration | Update::FitCursor);
703 case LFUN_LINE_BEGIN_SELECT:
704 case LFUN_WORD_BACKWARD_SELECT:
705 case LFUN_WORD_LEFT_SELECT:
706 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
707 cmd.action == LFUN_WORD_LEFT_SELECT ||
708 cmd.action == LFUN_LINE_BEGIN_SELECT);
709 cur.macroModeClose();
710 if (cur.pos() != 0) {
712 } else if (cur.col() != 0) {
713 cur.idx() -= cur.col();
715 } else if (cur.idx() != 0) {
719 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
724 case LFUN_WORD_FORWARD:
725 case LFUN_WORD_RIGHT:
727 cur.updateFlags(Update::Decoration | Update::FitCursor);
728 case LFUN_WORD_FORWARD_SELECT:
729 case LFUN_WORD_RIGHT_SELECT:
730 case LFUN_LINE_END_SELECT:
731 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
732 cmd.action == LFUN_WORD_RIGHT_SELECT ||
733 cmd.action == LFUN_LINE_END_SELECT);
734 cur.macroModeClose();
736 if (cur.pos() != cur.lastpos()) {
737 cur.pos() = cur.lastpos();
738 } else if (ncols() && (cur.col() != cur.lastcol())) {
739 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
740 cur.pos() = cur.lastpos();
741 } else if (cur.idx() != cur.lastidx()) {
742 cur.idx() = cur.lastidx();
743 cur.pos() = cur.lastpos();
745 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
750 case LFUN_CELL_FORWARD:
751 cur.updateFlags(Update::Decoration | Update::FitCursor);
752 cur.inset().idxNext(cur);
755 case LFUN_CELL_BACKWARD:
756 cur.updateFlags(Update::Decoration | Update::FitCursor);
757 cur.inset().idxPrev(cur);
760 case LFUN_WORD_DELETE_BACKWARD:
761 case LFUN_CHAR_DELETE_BACKWARD:
763 // May affect external cell:
764 cur.recordUndoInset();
766 cur.recordUndoSelection();
767 // if the inset can not be removed from within, delete it
768 if (!cur.backspace()) {
769 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
770 cur.innerText()->dispatch(cur, cmd);
774 case LFUN_WORD_DELETE_FORWARD:
775 case LFUN_CHAR_DELETE_FORWARD:
776 if (cur.pos() == cur.lastpos())
777 // May affect external cell:
778 cur.recordUndoInset();
780 cur.recordUndoSelection();
781 // if the inset can not be removed from within, delete it
783 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
784 cur.innerText()->dispatch(cur, cmd);
790 cur.clearSelection();
792 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
797 // 'Locks' the math inset. A 'locked' math inset behaves as a unit
798 // that is traversed by a single <CursorLeft>/<CursorRight>.
799 case LFUN_INSET_TOGGLE:
805 case LFUN_SELF_INSERT:
806 if (cmd.argument().size() != 1) {
807 cur.recordUndoSelection();
808 docstring const arg = cmd.argument();
809 if (!interpretString(cur, arg))
813 // Don't record undo steps if we are in macro mode and
814 // cmd.argument is the next character of the macro name.
815 // Otherwise we'll get an invalid cursor if we undo after
816 // the macro was finished and the macro is a known command,
817 // e.g. sqrt. Cursor::macroModeClose replaces in this case
818 // the InsetMathUnknown with name "frac" by an empty
819 // InsetMathFrac -> a pos value > 0 is invalid.
820 // A side effect is that an undo before the macro is finished
821 // undoes the complete macro, not only the last character.
822 if (!cur.inMacroMode())
823 cur.recordUndoSelection();
825 // spacial handling of space. If we insert an inset
826 // via macro mode, we want to put the cursor inside it
827 // if relevant. Think typing "\frac<space>".
828 if (cmd.argument()[0] == ' '
829 && cur.inMacroMode() && cur.macroName() != "\\"
830 && cur.macroModeClose()) {
831 MathAtom const atom = cur.prevAtom();
832 if (atom->asNestInset() && atom->isActive()) {
834 cur.pushBackward(*cur.nextInset());
836 } else if (!interpretChar(cur, cmd.argument()[0])) {
837 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
842 //case LFUN_SERVER_GET_XY:
845 case LFUN_SERVER_SET_XY: {
846 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
849 istringstream is(to_utf8(cmd.argument()));
851 cur.setScreenPos(x, y);
855 // Special casing for superscript in case of LyX handling
857 case LFUN_ACCENT_CIRCUMFLEX:
858 if (cmd.argument().empty()) {
859 // do superscript if LyX handles
861 cur.recordUndoSelection();
862 script(cur, true, grabAndEraseSelection(cur));
866 case LFUN_ACCENT_UMLAUT:
867 case LFUN_ACCENT_ACUTE:
868 case LFUN_ACCENT_GRAVE:
869 case LFUN_ACCENT_BREVE:
870 case LFUN_ACCENT_DOT:
871 case LFUN_ACCENT_MACRON:
872 case LFUN_ACCENT_CARON:
873 case LFUN_ACCENT_TILDE:
874 case LFUN_ACCENT_CEDILLA:
875 case LFUN_ACCENT_CIRCLE:
876 case LFUN_ACCENT_UNDERDOT:
877 case LFUN_ACCENT_TIE:
878 case LFUN_ACCENT_OGONEK:
879 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
883 case LFUN_TEXTSTYLE_APPLY:
884 case LFUN_TEXTSTYLE_UPDATE:
885 handleFont2(cur, cmd.argument());
889 if (currentMode() == TEXT_MODE)
890 handleFont(cur, cmd.argument(), "textbf");
892 handleFont(cur, cmd.argument(), "mathbf");
894 case LFUN_FONT_BOLDSYMBOL:
895 if (currentMode() == TEXT_MODE)
896 handleFont(cur, cmd.argument(), "textbf");
898 handleFont(cur, cmd.argument(), "boldsymbol");
901 if (currentMode() == TEXT_MODE)
902 handleFont(cur, cmd.argument(), "textsf");
904 handleFont(cur, cmd.argument(), "mathsf");
907 if (currentMode() == TEXT_MODE)
908 handleFont(cur, cmd.argument(), "emph");
910 handleFont(cur, cmd.argument(), "mathcal");
912 case LFUN_FONT_ROMAN:
913 if (currentMode() == TEXT_MODE)
914 handleFont(cur, cmd.argument(), "textrm");
916 handleFont(cur, cmd.argument(), "mathrm");
918 case LFUN_FONT_TYPEWRITER:
919 if (currentMode() == TEXT_MODE)
920 handleFont(cur, cmd.argument(), "texttt");
922 handleFont(cur, cmd.argument(), "mathtt");
925 handleFont(cur, cmd.argument(), "mathfrak");
928 if (currentMode() == TEXT_MODE)
929 handleFont(cur, cmd.argument(), "textit");
931 handleFont(cur, cmd.argument(), "mathit");
934 if (currentMode() == TEXT_MODE)
935 // FIXME: should be "noun"
936 handleFont(cur, cmd.argument(), "textsc");
938 handleFont(cur, cmd.argument(), "mathbb");
940 case LFUN_FONT_DEFAULT:
941 handleFont(cur, cmd.argument(), "textnormal");
944 case LFUN_MATH_MODE: {
946 // ignore math-mode on when already in math mode
947 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
949 cur.recordUndoSelection();
950 cur.macroModeClose();
951 docstring const save_selection = grabAndEraseSelection(cur);
953 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
954 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
956 cur.pushBackward(*cur.nextInset());
957 cur.niceInsert(save_selection);
959 if (currentMode() == Inset::TEXT_MODE) {
960 cur.recordUndoSelection();
961 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
962 cur.message(_("create new math text environment ($...$)"));
964 handleFont(cur, cmd.argument(), "textrm");
965 cur.message(_("entered math text mode (textrm)"));
971 case LFUN_REGEXP_MODE: {
972 InsetMathHull * i = dynamic_cast<InsetMathHull *>(cur.inset().asInsetMath());
973 if (i && i->getType() == hullRegexp) {
974 cur.message(_("Already in regexp mode"));
977 cur.macroModeClose();
978 docstring const save_selection = grabAndEraseSelection(cur);
980 cur.plainInsert(MathAtom(new InsetMathHull(hullRegexp)));
982 cur.pushBackward(*cur.nextInset());
983 cur.niceInsert(save_selection);
984 cur.message(_("Regexp editor mode"));
988 case LFUN_MATH_SIZE: {
989 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, cmd.argument());
994 case LFUN_MATH_MATRIX: {
1000 idocstringstream is(cmd.argument());
1001 is >> m >> n >> v_align >> h_align;
1008 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
1012 case LFUN_MATH_DELIM: {
1014 docstring rs = split(cmd.argument(), ls, ' ');
1015 // Reasonable default values
1021 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
1025 case LFUN_MATH_BIGDELIM: {
1026 docstring const lname = from_utf8(cmd.getArg(0));
1027 docstring const ldelim = from_utf8(cmd.getArg(1));
1028 docstring const rname = from_utf8(cmd.getArg(2));
1029 docstring const rdelim = from_utf8(cmd.getArg(3));
1030 latexkeys const * l = in_word_set(lname);
1031 bool const have_l = l && l->inset == "big" &&
1032 InsetMathBig::isBigInsetDelim(ldelim);
1033 l = in_word_set(rname);
1034 bool const have_r = l && l->inset == "big" &&
1035 InsetMathBig::isBigInsetDelim(rdelim);
1036 // We mimic LFUN_MATH_DELIM in case we have an empty left
1037 // or right delimiter.
1038 if (have_l || have_r) {
1040 docstring const selection = grabAndEraseSelection(cur);
1043 cur.insert(MathAtom(new InsetMathBig(lname,
1045 cur.niceInsert(selection);
1047 cur.insert(MathAtom(new InsetMathBig(rname,
1050 // Don't call cur.undispatched() if we did nothing, this would
1051 // lead to infinite recursion via Text::dispatch().
1055 case LFUN_SPACE_INSERT:
1056 cur.recordUndoSelection();
1057 cur.insert(MathAtom(new InsetMathSpace));
1060 case LFUN_MATH_SPACE:
1061 cur.recordUndoSelection();
1062 if (cmd.argument().empty())
1063 cur.insert(MathAtom(new InsetMathSpace));
1065 string const name = cmd.getArg(0);
1066 string const len = cmd.getArg(1);
1067 cur.insert(MathAtom(new InsetMathSpace(name, len)));
1071 case LFUN_ERT_INSERT:
1072 // interpret this as if a backslash was typed
1074 interpretChar(cur, '\\');
1077 case LFUN_MATH_SUBSCRIPT:
1078 // interpret this as if a _ was typed
1079 cur.recordUndoSelection();
1080 interpretChar(cur, '_');
1083 case LFUN_MATH_SUPERSCRIPT:
1084 // interpret this as if a ^ was typed
1085 cur.recordUndoSelection();
1086 interpretChar(cur, '^');
1089 case LFUN_MATH_MACRO_FOLD:
1090 case LFUN_MATH_MACRO_UNFOLD: {
1092 bool fold = cmd.action == LFUN_MATH_MACRO_FOLD;
1093 bool found = findMacroToFoldUnfold(it, fold);
1095 MathMacro * macro = it.nextInset()->asInsetMath()->asMacro();
1096 cur.recordUndoInset();
1105 case LFUN_QUOTE_INSERT:
1106 // interpret this as if a straight " was typed
1107 cur.recordUndoSelection();
1108 interpretChar(cur, '\"');
1111 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
1112 // handling such that "self-insert" works on "arbitrary stuff" too, and
1113 // math-insert only handles special math things like "matrix".
1114 case LFUN_MATH_INSERT: {
1115 cur.recordUndoSelection();
1116 if (cmd.argument() == "^" || cmd.argument() == "_")
1117 interpretChar(cur, cmd.argument()[0]);
1120 asArray(cmd.argument(), ar);
1121 if (ar.size() == 1 && ar[0]->asNestInset()
1122 && ar[0]->asNestInset()->nargs() > 1)
1123 handleNest(cur, ar[0]);
1125 cur.niceInsert(cmd.argument());
1130 case LFUN_DIALOG_SHOW_NEW_INSET: {
1131 docstring const & name = cmd.argument();
1133 if (name == "ref") {
1134 InsetMathRef tmp(name);
1135 data = tmp.createDialogStr(to_utf8(name));
1136 } else if (name == "mathspace") {
1138 data = tmp.createDialogStr();
1140 cur.bv().showDialog(to_utf8(name), data);
1144 case LFUN_INSET_INSERT: {
1146 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1147 cur.recordUndoSelection();
1153 case LFUN_INSET_DISSOLVE:
1154 if (!asHullInset()) {
1155 cur.recordUndoInset();
1161 InsetMath::doDispatch(cur, cmd);
1167 bool InsetMathNest::findMacroToFoldUnfold(Cursor & it, bool fold) const {
1168 // look for macro to open/close, but stay in mathed
1169 for (; !it.empty(); it.pop_back()) {
1171 // go backward through the current cell
1172 Inset * inset = it.nextInset();
1173 while (inset && inset->asInsetMath()) {
1174 MathMacro * macro = inset->asInsetMath()->asMacro();
1176 // found the an macro to open/close?
1177 if (macro->folded() != fold)
1180 // Wrong folding state.
1181 // If this was the first we see in this slice, look further left,
1183 if (inset != it.nextInset())
1187 // go up if this was the left most position
1193 inset = it.nextInset();
1201 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1202 FuncStatus & flag) const
1204 // the font related toggles
1205 //string tc = "mathnormal";
1207 string const arg = to_utf8(cmd.argument());
1208 switch (cmd.action) {
1209 case LFUN_TABULAR_FEATURE:
1210 flag.setEnabled(false);
1213 case LFUN_TABULAR_FEATURE:
1214 // FIXME: check temporarily disabled
1216 char align = mathcursor::valign();
1217 if (align == '\0') {
1221 if (cmd.argument().empty()) {
1225 if (!contains("tcb", cmd.argument()[0])) {
1229 flag.setOnOff(cmd.argument()[0] == align);
1232 /// We have to handle them since 1.4 blocks all unhandled actions
1233 case LFUN_FONT_ITAL:
1234 case LFUN_FONT_BOLD:
1235 case LFUN_FONT_BOLDSYMBOL:
1236 case LFUN_FONT_SANS:
1237 case LFUN_FONT_EMPH:
1238 case LFUN_FONT_TYPEWRITER:
1239 case LFUN_FONT_NOUN:
1240 case LFUN_FONT_ROMAN:
1241 case LFUN_FONT_DEFAULT:
1242 flag.setEnabled(true);
1244 case LFUN_MATH_MUTATE:
1245 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1246 flag.setOnOff(false);
1249 // we just need to be in math mode to enable that
1250 case LFUN_MATH_SIZE:
1251 case LFUN_MATH_SPACE:
1252 case LFUN_MATH_LIMITS:
1253 case LFUN_MATH_EXTERN:
1254 flag.setEnabled(true);
1257 case LFUN_FONT_FRAK:
1258 flag.setEnabled(currentMode() != TEXT_MODE);
1261 case LFUN_MATH_INSERT: {
1262 bool const textarg =
1263 arg == "\\textbf" || arg == "\\textsf" ||
1264 arg == "\\textrm" || arg == "\\textmd" ||
1265 arg == "\\textit" || arg == "\\textsc" ||
1266 arg == "\\textsl" || arg == "\\textup" ||
1267 arg == "\\texttt" || arg == "\\textbb" ||
1268 arg == "\\textnormal";
1269 flag.setEnabled(currentMode() != TEXT_MODE || textarg);
1273 case LFUN_MATH_MATRIX:
1274 flag.setEnabled(currentMode() == MATH_MODE);
1277 case LFUN_INSET_INSERT: {
1278 // Don't test createMathInset_fromDialogStr(), since
1279 // getStatus is not called with a valid reference and the
1280 // dialog would not be applyable.
1281 string const name = cmd.getArg(0);
1282 flag.setEnabled(name == "ref" || name == "mathspace");
1286 case LFUN_MATH_DELIM:
1287 case LFUN_MATH_BIGDELIM:
1288 // Don't do this with multi-cell selections
1289 flag.setEnabled(cur.selBegin().idx() == cur.selEnd().idx());
1292 case LFUN_MATH_MACRO_FOLD:
1293 case LFUN_MATH_MACRO_UNFOLD: {
1295 bool found = findMacroToFoldUnfold(it, cmd.action == LFUN_MATH_MACRO_FOLD);
1296 flag.setEnabled(found);
1300 case LFUN_SPECIALCHAR_INSERT:
1301 // FIXME: These would probably make sense in math-text mode
1302 flag.setEnabled(false);
1305 case LFUN_INSET_DISSOLVE:
1306 flag.setEnabled(!asHullInset());
1317 void InsetMathNest::edit(Cursor & cur, bool front, EntryDirection entry_from)
1320 bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_RIGHT ||
1321 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1322 cur.idx() = enter_front ? 0 : cur.lastidx();
1323 cur.pos() = enter_front ? 0 : cur.lastpos();
1325 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1329 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1332 int dist_min = 1000000;
1333 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1334 int const d = cell(i).dist(cur.bv(), x, y);
1340 MathData & ar = cell(idx_min);
1342 cur.idx() = idx_min;
1343 cur.pos() = ar.x2pos(&cur.bv(), x - ar.xo(cur.bv()));
1345 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1346 if (dist_min == 0) {
1348 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1349 if (ar[i]->covers(cur.bv(), x, y))
1350 return ar[i].nucleus()->editXY(cur, x, y);
1356 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1358 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1359 BufferView & bv = cur.bv();
1360 bool do_selection = cmd.button() == mouse_button::button1
1361 && cmd.argument() == "region-select";
1362 bv.mouseSetCursor(cur, do_selection);
1363 if (cmd.button() == mouse_button::button1) {
1364 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1365 // Update the cursor update flags as needed:
1367 // Update::Decoration: tells to update the decoration
1368 // (visual box corners that define
1370 // Update::FitCursor: adjust the screen to the cursor
1371 // position if needed
1372 // cur.result().update(): don't overwrite previously set flags.
1373 cur.updateFlags(Update::Decoration | Update::FitCursor
1374 | cur.result().update());
1375 } else if (cmd.button() == mouse_button::button2) {
1376 if (cap::selection()) {
1377 // See comment in Text::dispatch why we do this
1378 cap::copySelectionToStack();
1379 cmd = FuncRequest(LFUN_PASTE, "0");
1380 doDispatch(bv.cursor(), cmd);
1383 asArray(theSelection().get(), ar);
1384 bv.cursor().insert(ar);
1390 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1392 // only select with button 1
1393 if (cmd.button() == mouse_button::button1) {
1394 Cursor & bvcur = cur.bv().cursor();
1395 if (bvcur.anchor_.hasPart(cur)) {
1396 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1397 bvcur.setCursor(cur);
1398 bvcur.setSelection(true);
1399 //lyxerr << "MOTION " << bvcur << endl;
1406 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1408 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1410 if (cmd.button() == mouse_button::button1) {
1411 if (!cur.selection())
1414 Cursor & bvcur = cur.bv().cursor();
1415 bvcur.setSelection(true);
1424 bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
1426 //lyxerr << "interpret 2: '" << c << "'" << endl;
1427 docstring save_selection;
1428 if (c == '^' || c == '_')
1429 save_selection = grabAndEraseSelection(cur);
1434 if (cur.inMacroMode()) {
1435 docstring name = cur.macroName();
1437 /// are we currently typing '#1' or '#2' or...?
1438 if (name == "\\#") {
1441 if (n >= 1 && n <= 9)
1442 cur.insert(new MathMacroArgument(n));
1446 // do not finish macro for known * commands
1447 MathWordList const & mwl = mathedWordList();
1448 bool star_macro = c == '*'
1449 && (mwl.find(name.substr(1) + "*") != mwl.end()
1450 || cur.buffer()->getMacro(name.substr(1) + "*", cur, true));
1451 if (isAlphaASCII(c) || star_macro) {
1452 cur.activeMacro()->setName(name + docstring(1, c));
1456 // handle 'special char' macros
1461 if (currentMode() == InsetMath::TEXT_MODE)
1462 cur.niceInsert(createInsetMath("textbackslash"));
1464 cur.niceInsert(createInsetMath("backslash"));
1465 } else if (c == '^' && currentMode() == InsetMath::MATH_MODE) {
1467 cur.niceInsert(createInsetMath("mathcircumflex"));
1468 } else if (c == '{') {
1470 cur.niceInsert(MathAtom(new InsetMathBrace));
1471 } else if (c == '%') {
1473 cur.niceInsert(MathAtom(new InsetMathComment));
1474 } else if (c == '#') {
1475 LASSERT(cur.activeMacro(), /**/);
1476 cur.activeMacro()->setName(name + docstring(1, c));
1479 cur.niceInsert(createInsetMath(docstring(1, c)));
1484 // One character big delimiters. The others are handled in
1485 // interpretString().
1486 latexkeys const * l = in_word_set(name.substr(1));
1487 if (name[0] == '\\' && l && l->inset == "big") {
1491 delim = from_ascii("\\{");
1494 delim = from_ascii("\\}");
1497 delim = docstring(1, c);
1500 if (InsetMathBig::isBigInsetDelim(delim)) {
1501 // name + delim ared a valid InsetMathBig.
1502 // We can't use cur.macroModeClose() because
1503 // it does not handle delim.
1504 InsetMathUnknown * p = cur.activeMacro();
1507 cur.cell().erase(cur.pos());
1508 cur.plainInsert(MathAtom(
1509 new InsetMathBig(name.substr(1), delim)));
1514 // leave macro mode and try again if necessary
1515 cur.macroModeClose();
1517 cur.niceInsert(MathAtom(new InsetMathBrace));
1519 interpretChar(cur, c);
1523 // This is annoying as one has to press <space> far too often.
1527 // leave autocorrect mode if necessary
1528 if (cur.autocorrect() && c == ' ') {
1529 cur.autocorrect() = false;
1534 // just clear selection on pressing the space bar
1535 if (cur.selection() && c == ' ') {
1536 cur.setSelection(false);
1541 //lyxerr << "starting with macro" << endl;
1542 bool reduced = cap::reduceSelectionToOneCell(cur);
1543 if (reduced || !cur.selection()) {
1544 docstring const safe = cap::grabAndEraseSelection(cur);
1545 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), safe, false)));
1553 if (currentMode() == InsetMath::TEXT_MODE)
1559 if (currentMode() == InsetMath::TEXT_MODE) {
1560 // insert spaces in text mode,
1561 // but suppress direct insertion of two spaces in a row
1562 // the still allows typing '<space>a<space>' and deleting the 'a', but
1563 // it is better than nothing...
1564 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1566 // FIXME: we have to enable full redraw here because of the
1567 // visual box corners that define the inset. If we know for
1568 // sure that we stay within the same cell we can optimize for
1570 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1574 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1575 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1576 // FIXME: we have to enable full redraw here because of the
1577 // visual box corners that define the inset. If we know for
1578 // sure that we stay within the same cell we can optimize for
1580 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1584 if (cur.popForward()) {
1585 // FIXME: we have to enable full redraw here because of the
1586 // visual box corners that define the inset. If we know for
1587 // sure that we stay within the same cell we can optimize for
1589 //cur.updateFlags(Update::FitCursor);
1593 // if we are at the very end, leave the formula
1594 return cur.pos() != cur.lastpos();
1597 // These should be treated differently when not in text mode:
1598 if (currentMode() != InsetMath::TEXT_MODE) {
1600 script(cur, false, save_selection);
1604 script(cur, true, save_selection);
1608 cur.niceInsert(createInsetMath("sim"));
1613 cur.niceInsert(createInsetMath("textasciicircum"));
1617 cur.niceInsert(createInsetMath("textasciitilde"));
1622 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1623 c == '%' || c == '_') {
1624 cur.niceInsert(createInsetMath(docstring(1, c)));
1630 // try auto-correction
1631 if (cur.autocorrect() && cur.pos() != 0 && math_autocorrect(cur.prevAtom(), c))
1635 // no special circumstances, so insert the character without any fuss
1637 cur.autocorrect() = true;
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;