2 * \file InsetMathNest.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetMathNest.h"
15 #include "InsetMathArray.h"
16 #include "InsetMathAMSArray.h"
17 #include "InsetMathBig.h"
18 #include "InsetMathBox.h"
19 #include "InsetMathBrace.h"
20 #include "InsetMathChar.h"
21 #include "InsetMathColor.h"
22 #include "InsetMathComment.h"
23 #include "InsetMathDecoration.h"
24 #include "InsetMathDelim.h"
25 #include "InsetMathEnsureMath.h"
26 #include "InsetMathFont.h"
27 #include "InsetMathHull.h"
28 #include "InsetMathRef.h"
29 #include "InsetMathScript.h"
30 #include "InsetMathSpace.h"
31 #include "InsetMathSymbol.h"
32 #include "InsetMathUnknown.h"
33 #include "MathAutoCorrect.h"
34 #include "MathCompletionList.h"
35 #include "MathFactory.h"
36 #include "InsetMathMacro.h"
37 #include "InsetMathMacroArgument.h"
38 #include "MathParser.h"
39 #include "MathStream.h"
40 #include "MathSupport.h"
43 #include "BufferParams.h"
44 #include "BufferView.h"
45 #include "CoordCache.h"
47 #include "CutAndPaste.h"
48 #include "DispatchResult.h"
50 #include "FuncRequest.h"
51 #include "FuncStatus.h"
52 #include "LaTeXFeatures.h"
55 #include "MetricsInfo.h"
59 #include "frontends/Application.h"
60 #include "frontends/Clipboard.h"
61 #include "frontends/Painter.h"
62 #include "frontends/Selection.h"
64 #include "support/debug.h"
65 #include "support/docstream.h"
66 #include "support/gettext.h"
67 #include "support/lassert.h"
68 #include "support/lstrings.h"
69 #include "support/textutils.h"
75 using namespace lyx::support;
79 using cap::copySelection;
80 using cap::grabAndEraseSelection;
81 using cap::cutSelection;
82 using cap::replaceSelection;
83 using cap::selClearOrDel;
86 InsetMathNest::InsetMathNest(Buffer * buf, idx_type nargs)
87 : InsetMath(buf), cells_(nargs), lock_(false)
89 // FIXME This should not really be necessary, but when we are
90 // initializing the table of global macros, we create macros
91 // with no associated Buffer.
97 InsetMathNest::InsetMathNest(InsetMathNest const & inset)
98 : InsetMath(inset), cells_(inset.cells_), lock_(inset.lock_)
102 InsetMathNest::~InsetMathNest()
104 map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
105 map<BufferView const *, bool>::iterator end = mouse_hover_.end();
106 for (; it != end; ++it)
108 it->first->clearLastInset(this);
112 InsetMathNest & InsetMathNest::operator=(InsetMathNest const & inset)
114 cells_ = inset.cells_;
116 mouse_hover_.clear();
117 InsetMath::operator=(inset);
122 void InsetMathNest::setBuffer(Buffer & buffer)
124 InsetMath::setBuffer(buffer);
125 for (MathData & data : cells_)
126 data.setBuffer(buffer);
130 idx_type InsetMathNest::nargs() const
132 return cells_.size();
136 void InsetMathNest::cursorPos(BufferView const & bv,
137 CursorSlice const & sl, bool /*boundary*/,
138 int & x, int & y) const
140 // FIXME: This is a hack. Ideally, the coord cache should not store
141 // absolute positions, but relative ones. This would mean to call
142 // setXY() not in MathData::draw(), but in the parent insets' draw()
143 // with the correctly adjusted x,y values. But this means that we'd have
144 // to touch all (math)inset's draw() methods. Right now, we'll store
145 // absolute value, and make them here relative, only to make them
146 // absolute again when actually drawing the cursor. What a mess.
147 LASSERT(&sl.inset() == this, return);
148 MathData const & ar = sl.cell();
149 CoordCache const & coord_cache = bv.coordCache();
150 if (!coord_cache.getArrays().has(&ar)) {
151 // this can (semi-)legally happen if we just created this cell
152 // and it never has been drawn before. So don't ASSERT.
153 //lyxerr << "no cached data for array " << &ar << endl;
158 Point const pt = coord_cache.getArrays().xy(&ar);
159 if (!coord_cache.getInsets().has(this)) {
161 //lyxerr << "no cached data for inset " << this << endl;
166 Point const pt2 = coord_cache.getInsets().xy(this);
167 //lyxerr << "retrieving position cache for MathData "
168 // << pt.x_ << ' ' << pt.y_ << endl;
169 x = pt.x_ - pt2.x_ + ar.pos2x(&bv, sl.pos());
171 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
172 // << " asc: " << ascent() << " des: " << descent()
173 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
174 // move cursor visually into empty cells ("blue rectangles");
176 Dimension const dim = coord_cache.getArrays().dim(&ar);
182 void InsetMathNest::cellsMetrics(MetricsInfo const & mi) const
185 for (auto const & cell : cells_) {
187 cell.metrics(m, dim);
192 void InsetMathNest::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
194 for (idx_type i = 0, n = nargs(); i != n; ++i)
195 cell(i).updateBuffer(it, utype, deleted);
200 bool InsetMathNest::idxNext(Cursor & cur) const
202 LASSERT(&cur.inset() == this, return false);
203 if (cur.idx() == cur.lastidx())
211 bool InsetMathNest::idxForward(Cursor & cur) const
217 bool InsetMathNest::idxPrev(Cursor & cur) const
219 LASSERT(&cur.inset() == this, return false);
223 cur.pos() = lyxrc.mac_like_cursor_movement ? cur.lastpos() : 0;
228 bool InsetMathNest::idxBackward(Cursor & cur) const
234 bool InsetMathNest::idxFirst(Cursor & cur) const
236 LASSERT(&cur.inset() == this, return false);
239 cur.idx() = firstIdx();
245 bool InsetMathNest::idxLast(Cursor & cur) const
247 LASSERT(&cur.inset() == this, return false);
250 cur.idx() = lastIdx();
251 cur.pos() = cur.lastpos();
256 void InsetMathNest::dump() const
258 odocstringstream oss;
259 otexrowstream ots(oss);
260 TeXMathStream os(ots);
261 os << "---------------------------------------------\n";
264 for (idx_type i = 0, n = nargs(); i != n; ++i)
265 os << cell(i) << "\n";
266 os << "---------------------------------------------\n";
267 lyxerr << to_utf8(oss.str());
271 void InsetMathNest::draw(PainterInfo &, int, int) const
275 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
281 void InsetMathNest::validate(LaTeXFeatures & features) const
283 for (idx_type i = 0; i < nargs(); ++i)
284 cell(i).validate(features);
288 void InsetMathNest::replace(ReplaceData & rep)
290 for (idx_type i = 0; i < nargs(); ++i)
291 cell(i).replace(rep);
295 bool InsetMathNest::contains(MathData const & ar) const
297 for (idx_type i = 0; i < nargs(); ++i)
298 if (cell(i).contains(ar))
304 bool InsetMathNest::lock() const
310 void InsetMathNest::lock(bool l)
316 bool InsetMathNest::isActive() const
322 MathData InsetMathNest::glue() const
325 for (size_t i = 0; i < nargs(); ++i)
331 void InsetMathNest::write(TeXMathStream & os) const
333 MathEnsurer ensurer(os, currentMode() == MATH_MODE);
334 ModeSpecifier specifier(os, currentMode(), lockedMode());
335 docstring const latex_name = name();
336 os << '\\' << latex_name;
337 os.inMathClass(asClassInset());
338 for (size_t i = 0; i < nargs(); ++i) {
339 Changer dummy = os.changeRowEntry(TexRow::mathEntry(id(),i));
340 os << '{' << cell(i) << '}';
343 os.pendingSpace(true);
344 if (lock_ && !os.latex()) {
346 os.pendingSpace(true);
348 os.inMathClass(false);
352 void InsetMathNest::normalize(NormalStream & os) const
355 for (size_t i = 0; i < nargs(); ++i)
356 os << ' ' << cell(i);
361 void InsetMathNest::latex(otexstream & os, OutputParams const & runparams) const
363 TeXMathStream::OutputType ot;
364 if (runparams.find_effective())
365 ot = TeXMathStream::wsSearchAdv;
366 else if (runparams.dryrun)
367 ot = TeXMathStream::wsDryrun;
369 ot = TeXMathStream::wsDefault;
370 TeXMathStream wi(os, runparams.moving_arg, true, ot,
372 wi.strikeoutMath(runparams.inDeletedInset);
373 if (runparams.inulemcmd) {
374 wi.ulemCmd(TeXMathStream::UNDERLINE);
375 if (runparams.local_font) {
376 FontInfo f = runparams.local_font->fontInfo();
377 if (f.strikeout() == FONT_ON)
378 wi.ulemCmd(TeXMathStream::STRIKEOUT);
381 wi.canBreakLine(os.canBreakLine());
382 Changer dummy = wi.changeRowEntry(TexRow::textEntry(runparams.lastid,
385 // Reset parbreak and command termination status after a math inset.
387 os.canBreakLine(wi.canBreakLine());
388 os.terminateCommand(false);
392 bool InsetMathNest::setMouseHover(BufferView const * bv, bool mouse_hover)
395 mouse_hover_[bv] = mouse_hover;
400 bool InsetMathNest::notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
404 MathData & ar = cur.cell();
405 // remove base-only "scripts"
406 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
407 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
408 if (p && p->nargs() == 1) {
409 MathData ar = p->nuc();
412 cur.adjust(i, ar.size() - 1);
416 // glue adjacent font insets of the same kind
417 for (pos_type i = 0; i + 1 < size(); ++i) {
418 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
419 InsetMathFont const * q = operator[](i + 1)->asFontInset();
420 if (p && q && p->name() == q->name()) {
421 p->cell(0).append(q->cell(0));
431 void InsetMathNest::handleFont
432 (Cursor & cur, docstring const & arg, char const * const font)
434 handleFont(cur, arg, from_ascii(font));
438 void InsetMathNest::handleFont(Cursor & cur, docstring const & arg,
439 docstring const & font)
441 cur.recordUndoSelection();
443 // this whole function is a hack and won't work for incremental font
445 if (cur.inset().asInsetMath()->name() == font)
446 cur.handleFont(to_utf8(font));
448 handleNest(cur, createInsetMath(font, cur.buffer()), arg);
452 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest)
454 handleNest(cur, nest, docstring());
458 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest,
459 docstring const & arg)
461 DocIterator const i1 = cur.selectionBegin();
462 DocIterator const i2 = cur.selectionEnd();
463 if (!i1.inset().asInsetMath())
465 if (i1.idx() == i2.idx()) {
466 // the easy case where only one cell is selected
467 cur.handleNest(nest);
472 // multiple selected cells in a simple non-grid inset
473 if (i1.inset().nrows() == 0 || i1.inset().ncols() == 0) {
474 for (idx_type i = i1.idx(); i <= i2.idx(); ++i) {
480 cur.pos() = cur.lastpos();
484 cur.handleNest(nest);
490 // the complicated case with multiple selected cells in a grid
493 cap::region(i1.top(), i2.top(), r1, r2, c1, c2);
494 for (row_type row = r1; row <= r2; ++row) {
495 for (col_type col = c1; col <= c2; ++col) {
498 cur.idx() = i1.inset().index(row, col);
501 cur.pos() = cur.lastpos();
505 cur.handleNest(nest);
512 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
514 cur.recordUndoSelection();
515 bool include_previous_change = false;
516 bool selection = cur.selection();
517 DocIterator sel_begin = cur.selectionBegin();
518 DocIterator sel_end = cur.selectionEnd();
519 bool multiple_cells = sel_begin.idx() != sel_end.idx();
522 font.fromString(to_utf8(arg), b);
524 InsetMathFont const * f = asFontInset();
526 switch(font.fontInfo().family()) {
528 if (!f || (f->name() != "textrm" && f->name() != "mathrm"))
529 im = currentMode() != MATH_MODE ? from_ascii("textrm")
530 : from_ascii("mathrm");
533 if (!f || (f->name() != "textsf" && f->name() != "mathsf"))
534 im = currentMode() != MATH_MODE ? from_ascii("textsf")
535 : from_ascii("mathsf");
537 case TYPEWRITER_FAMILY:
538 if (!f || (f->name() != "texttt" && f->name() != "mathtt"))
539 im = currentMode() != MATH_MODE ? from_ascii("texttt")
540 : from_ascii("mathtt");
560 handleNest(cur, createInsetMath(im, cur.buffer()));
562 include_previous_change = true;
565 switch(font.fontInfo().series()) {
567 if (!f || (f->name() != "textmd" && f->name() != "mathrm"))
568 im = currentMode() != MATH_MODE ? from_ascii("textmd")
569 : from_ascii("mathrm");
572 if (!f || (f->name() != "textbf" && f->name() != "mathbf"))
573 im = currentMode() != MATH_MODE ? from_ascii("textbf")
574 : from_ascii("boldsymbol");
581 if (multiple_cells) {
582 cur.setCursor(sel_begin);
586 cur.setCursor(sel_end);
587 cur.pos() = cur.lastpos();
589 } else if (include_previous_change && selection) {
592 handleNest(cur, createInsetMath(im, cur.buffer()));
594 include_previous_change = true;
597 switch(font.fontInfo().shape()) {
599 if (!f || (f->name() != "textup" && f->name() != "mathrm"))
600 im = currentMode() != MATH_MODE ? from_ascii("textup")
601 : from_ascii("mathrm");
604 if (!f || (f->name() != "textit" && f->name() != "mathit"))
605 im = currentMode() != MATH_MODE ? from_ascii("textit")
606 : from_ascii("mathit");
609 if (!f || f->name() != "textsl")
610 im = currentMode() != MATH_MODE ? from_ascii("textsl")
613 case SMALLCAPS_SHAPE:
614 if (!f || f->name() != "textsc")
615 im = currentMode() != MATH_MODE ? from_ascii("textsc")
623 if (multiple_cells) {
624 cur.setCursor(sel_begin);
628 cur.setCursor(sel_end);
629 cur.pos() = cur.lastpos();
631 } else if (include_previous_change && selection) {
634 handleNest(cur, createInsetMath(im, cur.buffer()));
636 include_previous_change = true;
639 switch(font.fontInfo().size()) {
641 im = from_ascii("tiny");
644 im = from_ascii("scriptsize");
647 im = from_ascii("footnotesize");
650 im = from_ascii("small");
653 im = from_ascii("normalsize");
656 im = from_ascii("large");
659 im = from_ascii("Large");
662 im = from_ascii("LARGE");
665 im = from_ascii("huge");
668 im = from_ascii("Huge");
677 if (multiple_cells) {
678 cur.setCursor(sel_begin);
682 cur.setCursor(sel_end);
683 cur.pos() = cur.lastpos();
685 } else if (include_previous_change && selection) {
688 handleNest(cur, createInsetMath(im, cur.buffer()));
690 include_previous_change = true;
693 InsetMathDecoration const * d = multiple_cells
694 ? nullptr : asDecorationInset();
695 docstring const name = d ? d->name() : docstring();
697 if ((font.fontInfo().underbar() == FONT_OFF && name == "uline") ||
698 (font.fontInfo().uuline() == FONT_OFF && name == "uuline") ||
699 (font.fontInfo().uwave() == FONT_OFF && name == "uwave")) {
700 if (include_previous_change) {
705 docstring const beg = '\\' + name + '{';
706 docstring const end = from_ascii("}");
707 docstring const sel2 = cur.selectionAsString(false);
708 cutSelection(cur, false);
711 docstring const sel1 = cur.selectionAsString(false);
712 cur.pos() = cur.lastpos();
714 docstring const sel3 = cur.selectionAsString(false);
715 cur.mathForward(false);
717 cutSelection(cur, false);
720 mathed_parse_cell(ar, beg + sel1 + end);
725 mathed_parse_cell(ar, sel2);
729 pos_type pos = cur.pos();
730 mathed_parse_cell(ar, beg + sel3 + end);
735 sel_begin = cur.selectionBegin();
736 sel_end = cur.selectionEnd();
737 include_previous_change = false;
740 if (font.fontInfo().underbar() == FONT_ON) {
741 if (!d || name != "uline")
742 im = from_ascii("uline");
743 } else if (font.fontInfo().uuline() == FONT_ON) {
744 if (!d || name != "uuline")
745 im = from_ascii("uuline");
746 } else if (font.fontInfo().uwave() == FONT_ON) {
747 if (!d || name != "uwave")
748 im = from_ascii("uwave");
752 if (multiple_cells) {
753 cur.setCursor(sel_begin);
757 cur.setCursor(sel_end);
758 cur.pos() = cur.lastpos();
760 } else if (include_previous_change && selection) {
763 handleNest(cur, createInsetMath(im, cur.buffer()));
765 include_previous_change = true;
768 if (font.fontInfo().color() != Color_inherit &&
769 font.fontInfo().color() != Color_ignore) {
770 if (multiple_cells) {
771 cur.setCursor(sel_begin);
775 cur.setCursor(sel_end);
776 cur.pos() = cur.lastpos();
778 } else if (include_previous_change && selection) {
781 handleNest(cur, MathAtom(new InsetMathColor(buffer_, true, font.fontInfo().color())));
782 include_previous_change = true;
786 if (multiple_cells) {
787 cur.setCursor(sel_begin);
791 cur.setCursor(sel_end);
792 cur.pos() = cur.lastpos();
795 if (include_previous_change) {
799 cur.setCursor(sel_begin);
802 cur.setCursor(sel_end);
807 // FIXME: support other font changes here as well?
810 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
812 //LYXERR0("InsetMathNest: request: " << cmd);
814 Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
816 FuncCode const act = cmd.action();
819 case LFUN_CLIPBOARD_PASTE:
820 parseflg |= Parse::VERBATIM;
823 if (cur.currentMode() != MATH_MODE)
824 parseflg |= Parse::TEXTMODE;
825 cur.recordUndoSelection();
826 cur.message(_("Paste"));
827 replaceSelection(cur);
829 if (cmd.argument().empty() && !theClipboard().isInternal())
830 topaste = theClipboard().getAsText(frontend::Clipboard::PlainTextType);
833 idocstringstream is(cmd.argument());
835 topaste = cap::selection(n, make_pair(buffer().params().documentClassPtr(),
836 buffer().params().authors()));
838 InsetMath const * im = cur.inset().asInsetMath();
839 InsetMathMacro const * macro = im ? im->asMacro() : nullptr;
840 // do not allow pasting a backslash in the name of a macro
842 && macro->displayMode() == InsetMathMacro::DISPLAY_UNFOLDED
843 && support::contains(topaste, char_type('\\'))) {
844 LYXERR0("Removing backslash from pasted string");
845 topaste = subst(topaste, from_ascii("\\"), docstring());
847 cur.niceInsert(topaste, parseflg, false);
848 cur.clearSelection(); // bug 393
849 cur.forceBufferUpdate();
856 cutSelection(cur, true);
857 cur.message(_("Cut"));
858 // Prevent stale position >= size crash
859 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
861 cur.forceBufferUpdate();
864 case LFUN_MOUSE_PRESS:
865 lfunMousePress(cur, cmd);
868 case LFUN_MOUSE_MOTION:
869 lfunMouseMotion(cur, cmd);
872 case LFUN_MOUSE_RELEASE:
873 lfunMouseRelease(cur, cmd);
876 case LFUN_FINISHED_LEFT: // in math, left is backwards
877 case LFUN_FINISHED_BACKWARD:
878 cur.bv().cursor() = cur;
881 case LFUN_FINISHED_RIGHT: // in math, right is forward
882 case LFUN_FINISHED_FORWARD:
884 cur.bv().cursor() = cur;
887 case LFUN_WORD_RIGHT:
889 case LFUN_WORD_BACKWARD:
890 case LFUN_WORD_FORWARD:
891 case LFUN_CHAR_RIGHT:
893 case LFUN_CHAR_BACKWARD:
894 case LFUN_CHAR_FORWARD:
895 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
897 case LFUN_WORD_RIGHT_SELECT:
898 case LFUN_WORD_LEFT_SELECT:
899 case LFUN_WORD_BACKWARD_SELECT:
900 case LFUN_WORD_FORWARD_SELECT:
901 case LFUN_CHAR_RIGHT_SELECT:
902 case LFUN_CHAR_LEFT_SELECT:
903 case LFUN_CHAR_BACKWARD_SELECT:
904 case LFUN_CHAR_FORWARD_SELECT: {
905 // are we in a selection?
906 bool select = (act == LFUN_WORD_RIGHT_SELECT
907 || act == LFUN_WORD_LEFT_SELECT
908 || act == LFUN_WORD_BACKWARD_SELECT
909 || act == LFUN_WORD_FORWARD_SELECT
910 || act == LFUN_CHAR_RIGHT_SELECT
911 || act == LFUN_CHAR_LEFT_SELECT
912 || act == LFUN_CHAR_BACKWARD_SELECT
913 || act == LFUN_CHAR_FORWARD_SELECT);
915 bool word = (act == LFUN_WORD_RIGHT_SELECT
916 || act == LFUN_WORD_LEFT_SELECT
917 || act == LFUN_WORD_BACKWARD_SELECT
918 || act == LFUN_WORD_FORWARD_SELECT
919 || act == LFUN_WORD_RIGHT
920 || act == LFUN_WORD_LEFT
921 || act == LFUN_WORD_BACKWARD
922 || act == LFUN_WORD_FORWARD);
923 // are we moving forward or backwards?
924 // If the command was RIGHT or LEFT, then whether we're moving forward
925 // or backwards depends on the cursor movement mode (logical or visual):
926 // * in visual mode, since math is always LTR, right -> forward,
928 // * in logical mode, the mapping is determined by the
929 // reverseDirectionNeeded() function
932 FuncCode finish_lfun;
934 if (act == LFUN_CHAR_FORWARD
935 || act == LFUN_CHAR_FORWARD_SELECT
936 || act == LFUN_WORD_FORWARD
937 || act == LFUN_WORD_FORWARD_SELECT) {
939 finish_lfun = LFUN_FINISHED_FORWARD;
941 else if (act == LFUN_CHAR_BACKWARD
942 || act == LFUN_CHAR_BACKWARD_SELECT
943 || act == LFUN_WORD_BACKWARD
944 || act == LFUN_WORD_BACKWARD_SELECT) {
946 finish_lfun = LFUN_FINISHED_BACKWARD;
949 bool right = (act == LFUN_CHAR_RIGHT_SELECT
950 || act == LFUN_CHAR_RIGHT
951 || act == LFUN_WORD_RIGHT_SELECT
952 || act == LFUN_WORD_RIGHT);
953 if (lyxrc.visual_cursor || !cur.reverseDirectionNeeded())
959 finish_lfun = LFUN_FINISHED_RIGHT;
961 finish_lfun = LFUN_FINISHED_LEFT;
963 // Now that we know exactly what we want to do, let's do it!
964 cur.selHandle(select);
966 cur.macroModeClose();
967 // try moving forward or backwards as necessary...
968 if (!(forward ? cur.mathForward(word) : cur.mathBackward(word))) {
969 // ... and if movement failed, then finish forward or backwards
971 cmd = FuncRequest(finish_lfun);
979 case LFUN_PARAGRAPH_UP:
980 case LFUN_PARAGRAPH_DOWN:
981 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
983 case LFUN_DOWN_SELECT:
985 case LFUN_PARAGRAPH_UP_SELECT:
986 case LFUN_PARAGRAPH_DOWN_SELECT: {
987 // close active macro
988 if (cur.inMacroMode()) {
989 cur.macroModeClose();
993 // stop/start the selection
994 bool select = act == LFUN_DOWN_SELECT
995 || act == LFUN_UP_SELECT
996 || act == LFUN_PARAGRAPH_DOWN_SELECT
997 || act == LFUN_PARAGRAPH_UP_SELECT;
998 cur.selHandle(select);
1001 bool up = act == LFUN_UP || act == LFUN_UP_SELECT
1002 || act == LFUN_PARAGRAPH_UP || act == LFUN_PARAGRAPH_UP_SELECT;
1003 bool successful = cur.upDownInMath(up);
1007 if (cur.fixIfBroken())
1008 // FIXME: Something bad happened. We pass the corrected Cursor
1009 // instead of letting things go worse.
1012 // We did not manage to move the cursor.
1017 case LFUN_MOUSE_DOUBLE:
1018 case LFUN_WORD_SELECT:
1020 cur.bv().mouseSetCursor(cur);
1021 cur.pos() = cur.lastpos();
1022 cur.bv().mouseSetCursor(cur, true);
1025 case LFUN_MOUSE_TRIPLE:
1028 cur.bv().mouseSetCursor(cur);
1029 cur.idx() = cur.lastidx();
1030 cur.pos() = cur.lastpos();
1031 cur.bv().mouseSetCursor(cur, true);
1034 case LFUN_LINE_BEGIN:
1035 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
1037 case LFUN_LINE_BEGIN_SELECT:
1038 cur.selHandle(act == LFUN_WORD_BACKWARD_SELECT ||
1039 act == LFUN_WORD_LEFT_SELECT ||
1040 act == LFUN_LINE_BEGIN_SELECT);
1041 cur.macroModeClose();
1042 if (cur.pos() != 0) {
1044 } else if (cur.col() != 0) {
1045 cur.idx() -= cur.col();
1047 } else if (cur.idx() != 0) {
1051 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1057 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
1059 case LFUN_LINE_END_SELECT:
1060 cur.selHandle(act == LFUN_WORD_FORWARD_SELECT ||
1061 act == LFUN_WORD_RIGHT_SELECT ||
1062 act == LFUN_LINE_END_SELECT);
1063 cur.macroModeClose();
1065 if (cur.pos() != cur.lastpos()) {
1066 cur.pos() = cur.lastpos();
1067 } else if (ncols() && (cur.col() != cur.lastcol())) {
1068 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
1069 cur.pos() = cur.lastpos();
1070 } else if (cur.idx() != cur.lastidx()) {
1071 cur.idx() = cur.lastidx();
1072 cur.pos() = cur.lastpos();
1074 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1079 case LFUN_CELL_FORWARD:
1080 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
1081 cur.selHandle(false);
1083 cur.macroModeClose();
1084 if (!cur.inset().idxNext(cur)) {
1085 if (cur.lastidx() == 0)
1088 cur.idx() = firstIdx();
1094 case LFUN_CELL_BACKWARD:
1095 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
1096 cur.selHandle(false);
1098 cur.macroModeClose();
1099 if (!cur.inset().idxPrev(cur)) {
1100 if (cur.lastidx() == 0)
1103 cur.idx() = cur.lastidx();
1104 cur.pos() = lyxrc.mac_like_cursor_movement ? cur.lastpos() : 0;
1109 case LFUN_WORD_DELETE_BACKWARD:
1110 case LFUN_CHAR_DELETE_BACKWARD:
1112 // May affect external cell:
1113 cur.recordUndoInset();
1114 else if (!cur.inMacroMode())
1115 cur.recordUndoSelection();
1116 // if the inset can not be removed from within, delete it
1117 if (!cur.backspace(cmd.getArg(0) != "confirm")) {
1118 FuncRequest newcmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
1119 cur.innerText()->dispatch(cur, newcmd);
1123 case LFUN_WORD_DELETE_FORWARD:
1124 case LFUN_CHAR_DELETE_FORWARD:
1125 if (cur.pos() == cur.lastpos())
1126 // May affect external cell:
1127 cur.recordUndoInset();
1129 cur.recordUndoSelection();
1130 // if the inset can not be removed from within, delete it
1131 if (!cur.erase(cmd.getArg(0) != "confirm")) {
1132 FuncRequest newcmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
1133 cur.innerText()->dispatch(cur, newcmd);
1138 if (cur.selection())
1139 cur.clearSelection();
1141 if (cur.inMacroMode())
1142 cur.macroModeClose(true);
1144 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1151 // FIXME: resurrect this later
1152 // 'Locks' the math inset. A 'locked' math inset behaves as a unit
1153 // that is traversed by a single <CursorLeft>/<CursorRight>.
1154 case LFUN_INSET_TOGGLE:
1161 case LFUN_SELF_INSERT:
1162 // special case first for big delimiters
1163 if (cmd.argument().size() != 1 && interpretString(cur, cmd.argument()))
1166 for (char_type c : cmd.argument()) {
1167 // Don't record undo steps if we are in macro mode and thus
1168 // cmd.argument is the next character of the macro name.
1169 // Otherwise we'll get an invalid cursor if we undo after
1170 // the macro was finished and the macro is a known command,
1171 // e.g. sqrt. Cursor::macroModeClose replaces in this case
1172 // the InsetMathUnknown with name "frac" by an empty
1173 // InsetMathFrac -> a pos value > 0 is invalid.
1174 // A side effect is that an undo before the macro is finished
1175 // undoes the complete macro, not only the last character.
1176 // At the time we hit '\' we are not in macro mode, still.
1177 if (!cur.inMacroMode())
1178 cur.recordUndoSelection();
1180 // special handling of space. If we insert an inset
1181 // via macro mode, we want to put the cursor inside it
1182 // if relevant. Think typing "\frac<space>".
1184 && cur.inMacroMode() && cur.macroName() != "\\"
1185 && cur.macroModeClose() && cur.pos() > 0)
1186 cur.editInsertedInset();
1187 else if (!interpretChar(cur, c)) {
1188 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1190 // FIXME: can we avoid skipping the end of the string?
1196 //case LFUN_SERVER_GET_XY:
1199 case LFUN_SERVER_SET_XY: {
1200 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
1203 istringstream is(to_utf8(cmd.argument()));
1209 // Special casing for superscript in case of LyX handling
1211 case LFUN_ACCENT_CIRCUMFLEX:
1212 if (cmd.argument().empty()) {
1213 // do superscript if LyX handles
1215 cur.recordUndoInset();
1216 script(cur, true, grabAndEraseSelection(cur));
1220 case LFUN_ACCENT_UMLAUT:
1221 case LFUN_ACCENT_ACUTE:
1222 case LFUN_ACCENT_GRAVE:
1223 case LFUN_ACCENT_BREVE:
1224 case LFUN_ACCENT_DOT:
1225 case LFUN_ACCENT_MACRON:
1226 case LFUN_ACCENT_CARON:
1227 case LFUN_ACCENT_TILDE:
1228 case LFUN_ACCENT_CEDILLA:
1229 case LFUN_ACCENT_CIRCLE:
1230 case LFUN_ACCENT_UNDERDOT:
1231 case LFUN_ACCENT_TIE:
1232 case LFUN_ACCENT_OGONEK:
1233 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1237 case LFUN_TEXTSTYLE_APPLY:
1238 case LFUN_TEXTSTYLE_UPDATE:
1239 handleFont2(cur, cmd.argument());
1242 case LFUN_FONT_BOLD:
1243 if (currentMode() != MATH_MODE)
1244 handleFont(cur, cmd.argument(), "textbf");
1246 handleFont(cur, cmd.argument(), "mathbf");
1248 case LFUN_FONT_BOLDSYMBOL:
1249 if (currentMode() != MATH_MODE)
1250 handleFont(cur, cmd.argument(), "textbf");
1252 handleFont(cur, cmd.argument(), "boldsymbol");
1254 case LFUN_FONT_SANS:
1255 if (currentMode() != MATH_MODE)
1256 handleFont(cur, cmd.argument(), "textsf");
1258 handleFont(cur, cmd.argument(), "mathsf");
1260 case LFUN_FONT_EMPH:
1261 if (currentMode() != MATH_MODE)
1262 handleFont(cur, cmd.argument(), "emph");
1264 handleFont(cur, cmd.argument(), "mathcal");
1266 case LFUN_FONT_ROMAN:
1267 if (currentMode() != MATH_MODE)
1268 handleFont(cur, cmd.argument(), "textrm");
1270 handleFont(cur, cmd.argument(), "mathrm");
1272 case LFUN_FONT_TYPEWRITER:
1273 if (currentMode() != MATH_MODE)
1274 handleFont(cur, cmd.argument(), "texttt");
1276 handleFont(cur, cmd.argument(), "mathtt");
1278 case LFUN_FONT_FRAK:
1279 handleFont(cur, cmd.argument(), "mathfrak");
1281 case LFUN_FONT_ITAL:
1282 if (currentMode() != MATH_MODE)
1283 handleFont(cur, cmd.argument(), "textit");
1285 handleFont(cur, cmd.argument(), "mathit");
1287 case LFUN_FONT_NOUN:
1288 if (currentMode() != MATH_MODE)
1289 // FIXME: should be "noun"
1290 handleFont(cur, cmd.argument(), "textsc");
1292 handleFont(cur, cmd.argument(), "mathbb");
1294 case LFUN_FONT_DEFAULT:
1295 handleFont(cur, cmd.argument(), "textnormal");
1297 case LFUN_FONT_UNDERLINE:
1299 cur.handleNest(createInsetMath("underline", cur.buffer()));
1302 case LFUN_MATH_MODE: {
1304 // ignore math-mode on when already in math mode
1305 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
1307 cur.recordUndoSelection();
1308 cur.macroModeClose();
1309 docstring const save_selection = grabAndEraseSelection(cur);
1311 if (currentMode() != Inset::MATH_MODE)
1312 cur.plainInsert(MathAtom(new InsetMathEnsureMath(buffer_)));
1314 cur.plainInsert(createInsetMath("text", buffer_));
1316 cur.pushBackward(*cur.nextInset());
1317 cur.niceInsert(save_selection);
1318 cur.forceBufferUpdate();
1320 if (currentMode() == Inset::TEXT_MODE) {
1321 cur.recordUndoSelection();
1322 cur.niceInsert(MathAtom(new InsetMathHull("simple", cur.buffer())));
1323 cur.message(_("create new math text environment ($...$)"));
1325 handleFont(cur, cmd.argument(), "textrm");
1326 cur.message(_("entered math text mode (textrm)"));
1332 case LFUN_REGEXP_MODE: {
1333 InsetMath * im = cur.inset().asInsetMath();
1335 InsetMathHull * i = im->asHullInset();
1336 if (i && i->getType() == hullRegexp) {
1337 cur.message(_("Already in regular expression mode"));
1341 cur.macroModeClose();
1342 docstring const save_selection = grabAndEraseSelection(cur);
1344 cur.plainInsert(MathAtom(new InsetMathHull(buffer_, hullRegexp)));
1346 cur.pushBackward(*cur.nextInset());
1347 cur.niceInsert(save_selection);
1348 cur.message(_("Regular expression editor mode"));
1352 case LFUN_MATH_FONT_STYLE: {
1353 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, '\\' + cmd.argument());
1354 doDispatch(cur, fr);
1358 case LFUN_MATH_SIZE: {
1359 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, cmd.argument());
1360 doDispatch(cur, fr);
1364 case LFUN_MATH_MATRIX: {
1370 idocstringstream is(cmd.argument());
1371 is >> m >> n >> v_align >> h_align;
1377 cur.niceInsert(MathAtom(new InsetMathArray(buffer_,
1378 from_ascii("array"), m, n, (char)v_align[0], h_align)));
1382 case LFUN_MATH_AMS_MATRIX: {
1386 docstring name = from_ascii("matrix");
1387 idocstringstream is(cmd.argument());
1388 is >> m >> n >> name;
1393 // check if we have a valid decoration
1394 if (name != "pmatrix" && name != "bmatrix"
1395 && name != "Bmatrix" && name != "vmatrix"
1396 && name != "Vmatrix" && name != "matrix"
1397 && name != "smallmatrix")
1398 name = from_ascii("matrix");
1401 MathAtom(new InsetMathAMSArray(buffer_, name, m, n)));
1405 case LFUN_MATH_DELIM: {
1407 docstring rs = split(cmd.argument(), ls, ' ');
1408 // Reasonable default values
1414 cur.handleNest(MathAtom(new InsetMathDelim(buffer_, ls, rs)));
1418 case LFUN_MATH_BIGDELIM: {
1419 docstring const lname = from_utf8(cmd.getArg(0));
1420 docstring const ldelim = from_utf8(cmd.getArg(1));
1421 docstring const rname = from_utf8(cmd.getArg(2));
1422 docstring const rdelim = from_utf8(cmd.getArg(3));
1423 latexkeys const * l = in_word_set(lname);
1424 bool const have_l = l && l->inset == "big" &&
1425 InsetMathBig::isBigInsetDelim(ldelim);
1426 l = in_word_set(rname);
1427 bool const have_r = l && l->inset == "big" &&
1428 InsetMathBig::isBigInsetDelim(rdelim);
1429 // We mimic LFUN_MATH_DELIM in case we have an empty left
1430 // or right delimiter.
1431 if (have_l || have_r) {
1433 docstring const selection = grabAndEraseSelection(cur);
1436 cur.insert(MathAtom(new InsetMathBig(lname,
1438 // first insert the right delimiter and then go back
1439 // and re-insert the selection (bug 7088)
1441 cur.insert(MathAtom(new InsetMathBig(rname,
1445 cur.niceInsert(selection);
1447 // Don't call cur.undispatched() if we did nothing, this would
1448 // lead to infinite recursion via Text::dispatch().
1452 case LFUN_SPACE_INSERT: {
1453 cur.recordUndoSelection();
1454 string const name = cmd.getArg(0);
1455 if (name == "normal")
1456 cur.insert(MathAtom(new InsetMathSpace(" ", "")));
1457 else if (name == "protected")
1458 cur.insert(MathAtom(new InsetMathSpace("~", "")));
1459 else if (name == "thin" || name == "med" || name == "thick")
1460 cur.insert(MathAtom(new InsetMathSpace(name + "space", "")));
1461 else if (name == "hfill*")
1462 cur.insert(MathAtom(new InsetMathSpace("hspace*{\\fill}", "")));
1463 else if (name == "quad" || name == "qquad" ||
1464 name == "enspace" || name == "enskip" ||
1465 name == "negthinspace" || name == "negmedspace" ||
1466 name == "negthickspace" || name == "hfill")
1467 cur.insert(MathAtom(new InsetMathSpace(name, "")));
1468 else if (name == "hspace" || name == "hspace*") {
1469 string const len = cmd.getArg(1);
1470 if (len.empty() || !isValidLength(len)) {
1471 lyxerr << "LyX function 'space-insert " << name << "' "
1472 "needs a valid length argument." << endl;
1475 cur.insert(MathAtom(new InsetMathSpace(name, len)));
1477 cur.insert(MathAtom(new InsetMathSpace));
1481 case LFUN_MATH_SPACE:
1482 cur.recordUndoSelection();
1483 if (cmd.argument().empty())
1484 cur.insert(MathAtom(new InsetMathSpace));
1486 string const name = cmd.getArg(0);
1487 string const len = cmd.getArg(1);
1488 cur.insert(MathAtom(new InsetMathSpace(name, len)));
1492 case LFUN_ERT_INSERT:
1493 // interpret this as if a backslash was typed
1495 interpretChar(cur, '\\');
1498 case LFUN_MATH_SUBSCRIPT:
1499 // interpret this as if a _ was typed
1500 cur.recordUndoSelection();
1501 interpretChar(cur, '_');
1504 case LFUN_MATH_SUPERSCRIPT:
1505 // interpret this as if a ^ was typed
1506 cur.recordUndoSelection();
1507 interpretChar(cur, '^');
1510 case LFUN_MATH_MACRO_FOLD:
1511 case LFUN_MATH_MACRO_UNFOLD: {
1513 bool fold = act == LFUN_MATH_MACRO_FOLD;
1514 bool found = findMacroToFoldUnfold(it, fold);
1516 InsetMathMacro * macro = it.nextInset()->asInsetMath()->asMacro();
1517 cur.recordUndoInset();
1526 case LFUN_QUOTE_INSERT:
1527 // interpret this as if a straight " was typed
1528 cur.recordUndoSelection();
1529 interpretChar(cur, '\"');
1532 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
1533 // handling such that "self-insert" works on "arbitrary stuff" too, and
1534 // math-insert only handles special math things like "matrix".
1535 case LFUN_MATH_INSERT: {
1536 cur.recordUndoSelection();
1537 if (cmd.argument() == "^" || cmd.argument() == "_")
1538 interpretChar(cur, cmd.argument()[0]);
1539 else if (!cur.selection())
1540 cur.niceInsert(cmd.argument());
1542 MathData ar(cur.buffer());
1543 asArray(cmd.argument(), ar);
1545 && ar[0]->asNestInset()
1546 && ar[0]->asNestInset()->nargs() > 1)
1547 handleNest(cur, ar[0]);
1549 cur.niceInsert(cmd.argument());
1554 case LFUN_DIALOG_SHOW_NEW_INSET: {
1555 docstring const & name = cmd.argument();
1557 if (name == "ref") {
1558 InsetMathRef tmp(buffer_, name);
1559 data = tmp.createDialogStr();
1560 cur.bv().showDialog(to_utf8(name), data);
1561 } else if (name == "mathspace") {
1562 cur.bv().showDialog(to_utf8(name));
1567 case LFUN_INSET_INSERT: {
1569 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1570 cur.recordUndoSelection();
1572 cur.forceBufferUpdate();
1577 case LFUN_INSET_DISSOLVE:
1578 if (cmd.argument().empty() && !asHullInset() && !asMacroTemplate()) {
1579 // we have been triggered via the AtPoint mechanism
1580 if (cur.nextInset() == this)
1582 cur.recordUndoInset();
1587 case LFUN_MATH_LIMITS: {
1589 if (cur.pos() < cur.lastpos() && cur.nextMath().allowsLimitsChange())
1590 in = &cur.nextMath();
1591 else if (cur.pos() > 0 && cur.prevMath().allowsLimitsChange())
1592 in = &cur.prevMath();
1593 else if (cur.lastpos() > 0 && cur.cell().back()->allowsLimitsChange())
1594 in = cur.cell().back().nucleus();
1595 // only when nucleus allows this
1598 cur.recordUndoInset();
1599 if (!cmd.argument().empty()) {
1600 if (cmd.argument() == "limits")
1602 else if (cmd.argument() == "nolimits")
1603 in->limits(NO_LIMITS);
1605 in->limits(AUTO_LIMITS);
1606 } else if (in->limits() != AUTO_LIMITS)
1607 in->limits(AUTO_LIMITS);
1608 else if (in->defaultLimits(cur.cell().displayStyle()) == LIMITS)
1609 in->limits(NO_LIMITS);
1615 case LFUN_PHANTOM_INSERT: {
1616 docstring const & arg = cmd.argument();
1618 if (arg == "Phantom")
1619 newarg = from_ascii("\\phantom");
1620 else if (arg == "HPhantom")
1621 newarg = from_ascii("\\hphantom");
1622 else if (arg == "VPhantom")
1623 newarg = from_ascii("\\vphantom");
1625 LYXERR0("Unknown phantom type " + newarg);
1627 FuncRequest const newfunc(LFUN_MATH_INSERT, newarg);
1628 lyx::dispatch(newfunc);
1634 InsetMath::doDispatch(cur, cmd);
1640 bool InsetMathNest::findMacroToFoldUnfold(Cursor & it, bool fold) const {
1641 // look for macro to open/close, but stay in mathed
1642 for (; !it.empty(); it.pop_back()) {
1644 // go backward through the current cell
1645 Inset * inset = it.nextInset();
1646 while (inset && inset->asInsetMath()) {
1647 InsetMathMacro * macro = inset->asInsetMath()->asMacro();
1649 // found the an macro to open/close?
1650 if (macro->folded() != fold)
1653 // Wrong folding state.
1654 // If this was the first we see in this slice, look further left,
1656 if (inset != it.nextInset())
1660 // go up if this was the left most position
1666 inset = it.nextInset();
1674 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1675 FuncStatus & flag) const
1677 // the font related toggles
1678 //string tc = "mathnormal";
1680 string const arg = to_utf8(cmd.argument());
1681 switch (cmd.action()) {
1683 case LFUN_INSET_MODIFY:
1684 // FIXME: check temporarily disabled
1686 char align = mathcursor::valign();
1687 if (align == '\0') {
1691 if (cmd.argument().empty()) {
1695 if (!contains("tcb", cmd.argument()[0])) {
1699 flag.setOnOff(cmd.argument()[0] == align);
1702 /// We have to handle them since 1.4 blocks all unhandled actions
1703 case LFUN_FONT_ITAL:
1704 case LFUN_FONT_BOLD:
1705 case LFUN_FONT_BOLDSYMBOL:
1706 case LFUN_FONT_SANS:
1707 case LFUN_FONT_EMPH:
1708 case LFUN_FONT_TYPEWRITER:
1709 case LFUN_FONT_NOUN:
1710 case LFUN_FONT_ROMAN:
1711 case LFUN_FONT_DEFAULT:
1712 flag.setEnabled(true);
1715 // we just need to be in math mode to enable that
1716 case LFUN_MATH_SIZE:
1717 case LFUN_MATH_SPACE:
1718 case LFUN_MATH_EXTERN:
1719 flag.setEnabled(true);
1722 case LFUN_FONT_UNDERLINE:
1723 case LFUN_FONT_FRAK:
1724 flag.setEnabled(currentMode() != TEXT_MODE);
1727 case LFUN_MATH_FONT_STYLE: {
1728 bool const textarg =
1729 arg == "textbf" || arg == "textsf" ||
1730 arg == "textrm" || arg == "textmd" ||
1731 arg == "textit" || arg == "textsc" ||
1732 arg == "textsl" || arg == "textup" ||
1733 arg == "texttt" || arg == "textbb" ||
1734 arg == "textnormal";
1735 flag.setEnabled(currentMode() != TEXT_MODE || textarg);
1739 case LFUN_MATH_MODE:
1740 // forbid "math-mode on" in math mode to prevent irritating
1741 // behaviour of menu entries (bug 6709)
1742 flag.setEnabled(currentMode() == TEXT_MODE || arg != "on");
1745 case LFUN_MATH_INSERT:
1746 flag.setEnabled(currentMode() != TEXT_MODE);
1749 case LFUN_MATH_AMS_MATRIX:
1750 case LFUN_MATH_MATRIX:
1751 flag.setEnabled(currentMode() == MATH_MODE);
1754 case LFUN_INSET_INSERT: {
1755 // Don't test createMathInset_fromDialogStr(), since
1756 // getStatus is not called with a valid reference and the
1757 // dialog would not be applicable.
1758 string const name = cmd.getArg(0);
1759 flag.setEnabled(name == "ref" || name == "mathspace");
1763 case LFUN_DIALOG_SHOW_NEW_INSET: {
1764 docstring const & name = cmd.argument();
1765 flag.setEnabled(name == "ref" || name == "mathspace");
1770 case LFUN_MATH_DELIM:
1771 case LFUN_MATH_BIGDELIM:
1772 // Don't do this with multi-cell selections
1773 flag.setEnabled(cur.selBegin().idx() == cur.selEnd().idx());
1776 case LFUN_MATH_MACRO_FOLD:
1777 case LFUN_MATH_MACRO_UNFOLD: {
1779 bool found = findMacroToFoldUnfold(it, cmd.action() == LFUN_MATH_MACRO_FOLD);
1780 flag.setEnabled(found);
1784 case LFUN_SPECIALCHAR_INSERT:
1785 case LFUN_SCRIPT_INSERT:
1786 // FIXME: These would probably make sense in math-text mode
1787 flag.setEnabled(false);
1790 // these are nonfunctional in math
1791 case LFUN_BOX_INSERT:
1792 case LFUN_BRANCH_INSERT:
1793 case LFUN_BRANCH_ADD_INSERT:
1794 case LFUN_CAPTION_INSERT:
1795 case LFUN_FLEX_INSERT:
1796 case LFUN_FLOAT_INSERT:
1797 case LFUN_FLOAT_LIST_INSERT:
1798 case LFUN_FOOTNOTE_INSERT:
1799 case LFUN_HREF_INSERT:
1800 case LFUN_INDEX_INSERT:
1801 case LFUN_INDEX_PRINT:
1802 case LFUN_INFO_INSERT:
1803 case LFUN_IPA_INSERT:
1804 case LFUN_LISTING_INSERT:
1805 case LFUN_MARGINALNOTE_INSERT:
1806 case LFUN_NEWPAGE_INSERT:
1807 case LFUN_NOMENCL_INSERT:
1808 case LFUN_NOMENCL_PRINT:
1809 case LFUN_NOTE_INSERT:
1810 case LFUN_PREVIEW_INSERT:
1811 case LFUN_TABULAR_INSERT:
1812 case LFUN_WRAP_INSERT:
1813 flag.setEnabled(false);
1816 case LFUN_SPACE_INSERT: {
1817 docstring const & name = cmd.argument();
1818 if (name == "visible")
1819 flag.setEnabled(false);
1823 case LFUN_INSET_DISSOLVE:
1824 flag.setEnabled(cmd.argument().empty() && !asHullInset() && !asMacroTemplate());
1828 docstring const & name = cmd.argument();
1829 if (name == "html" || name == "latex")
1830 flag.setEnabled(false);
1834 case LFUN_MATH_LIMITS: {
1836 if (cur.pos() < cur.lastpos() && cur.nextMath().allowsLimitsChange())
1837 in = &cur.nextMath();
1838 else if (cur.pos() > 0 && cur.prevMath().allowsLimitsChange())
1839 in = &cur.prevMath();
1840 else if (cur.lastpos() > 0 && cur.cell().back()->allowsLimitsChange())
1841 in = cur.cell().back().nucleus();
1843 if (!cmd.argument().empty()) {
1844 if (cmd.argument() == "limits")
1845 flag.setOnOff(in->limits() == LIMITS);
1846 else if (cmd.argument() == "nolimits")
1847 flag.setOnOff(in->limits() == NO_LIMITS);
1849 flag.setOnOff(in->limits() == AUTO_LIMITS);
1851 flag.setEnabled(true);
1853 flag.setEnabled(false);
1865 void InsetMathNest::edit(Cursor & cur, bool front, EntryDirection entry_from)
1868 bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_LEFT ||
1869 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1870 enter_front ? idxFirst(cur) : idxLast(cur);
1872 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1876 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1879 int dist_min = 1000000;
1880 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1881 int const d = cell(i).dist(cur.bv(), x, y);
1890 MathData & ar = cell(idx_min);
1892 cur.idx() = idx_min;
1893 cur.pos() = ar.x2pos(&cur.bv(), x - ar.xo(cur.bv()));
1895 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1896 if (dist_min == 0) {
1898 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1899 if (ar[i]->covers(cur.bv(), x, y))
1900 return ar[i].nucleus()->editXY(cur, x, y);
1906 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1908 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1909 BufferView & bv = cur.bv();
1910 if (cmd.button() == mouse_button::button3) {
1911 // Don't do anything if we right-click a
1912 // selection, a context menu will popup.
1913 if (bv.cursor().selection() && cur >= bv.cursor().selectionBegin()
1914 && cur < bv.cursor().selectionEnd()) {
1915 cur.noScreenUpdate();
1920 // set cursor after the inset if x is nearer to that position (bug 9748)
1921 cur.moveToClosestEdge(cmd.x(), true);
1923 bool do_selection = cmd.button() == mouse_button::button1
1924 && cmd.modifier() == ShiftModifier;
1925 bv.mouseSetCursor(cur, do_selection);
1926 if (cmd.button() == mouse_button::button1) {
1927 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1928 // Update the cursor update flags as needed:
1930 // Update::Decoration: tells to update the decoration
1931 // (visual box corners that define
1933 // Update::FitCursor: adjust the screen to the cursor
1934 // position if needed
1935 // cur.result().update(): don't overwrite previously set flags.
1936 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor
1937 | cur.result().screenUpdate());
1938 } else if (cmd.button() == mouse_button::button2 && lyxrc.mouse_middlebutton_paste) {
1939 if (cap::selection()) {
1940 // See comment in Text::dispatch why we do this
1941 cap::copySelectionToStack();
1942 cmd = FuncRequest(LFUN_PASTE, "0");
1943 doDispatch(bv.cursor(), cmd);
1946 asArray(theSelection().get(), ar);
1947 bv.cursor().insert(ar);
1953 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1955 // only select with button 1
1956 if (cmd.button() != mouse_button::button1)
1959 Cursor & bvcur = cur.bv().cursor();
1961 // ignore motions deeper nested than the real anchor
1962 if (!bvcur.realAnchor().hasPart(cur)) {
1967 // set cursor after the inset if x is nearer to that position (bug 9748)
1968 cur.moveToClosestEdge(cmd.x());
1970 CursorSlice old = bvcur.top();
1972 // We continue with our existing selection or start a new one, so don't
1973 // reset the anchor.
1974 bvcur.setCursor(cur);
1975 // Did we actually move?
1976 if (cur.top() == old)
1977 // We didn't move one iota, so no need to change selection status
1978 // or update the screen.
1979 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1981 bvcur.setSelection();
1985 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1987 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1989 if (cmd.button() == mouse_button::button1) {
1990 if (!cur.selection())
1991 cur.noScreenUpdate();
1993 Cursor & bvcur = cur.bv().cursor();
1994 bvcur.selection(true);
2003 bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
2005 // try auto-correction
2006 if (lyxrc.autocorrection_math && cur.pos() != 0 && !cur.selection()
2007 && math_autocorrect(cur, c))
2010 //lyxerr << "interpret 2: '" << c << "'" << endl;
2011 docstring save_selection;
2012 if (c == '^' || c == '_')
2013 save_selection = grabAndEraseSelection(cur);
2016 Buffer * buf = cur.buffer();
2019 if (cur.inMacroMode()) {
2020 docstring name = cur.macroName();
2022 /// are we currently typing '#1' or '#2' or...?
2023 if (name == "\\#") {
2026 if (n >= 1 && n <= 9)
2027 cur.insert(new InsetMathMacroArgument(n));
2031 // do not finish macro for known * commands
2032 bool star_macro = c == '*'
2033 && (in_word_set(name.substr(1) + '*')
2034 || cur.buffer()->getMacro(name.substr(1) + "*", cur, true));
2035 if (isAlphaASCII(c) || star_macro) {
2036 cur.activeMacro()->setName(name + docstring(1, c));
2040 // handle 'special char' macros
2045 if (currentMode() != InsetMath::MATH_MODE)
2046 cur.niceInsert(createInsetMath("textbackslash", buf));
2048 cur.niceInsert(createInsetMath("backslash", buf));
2049 } else if (c == '^' && currentMode() == InsetMath::MATH_MODE) {
2051 cur.niceInsert(createInsetMath("mathcircumflex", buf));
2052 } else if (c == '{' || c == '%') {
2053 //using the saved selection as argument
2054 InsetMathUnknown * p = cur.activeMacro();
2056 MathData sel(cur.buffer());
2057 asArray(p->selection(), sel);
2060 cur.niceInsert(MathAtom(new InsetMathBrace(sel)));
2062 cur.niceInsert(MathAtom(new InsetMathComment(sel)));
2063 } else if (c == '#') {
2064 LASSERT(cur.activeMacro(), return false);
2065 cur.activeMacro()->setName(name + docstring(1, c));
2068 cur.niceInsert(createInsetMath(docstring(1, c), buf));
2073 // One character big delimiters. The others are handled in
2074 // interpretString().
2075 latexkeys const * l = in_word_set(name.substr(1));
2076 if (name[0] == '\\' && l && l->inset == "big") {
2080 delim = from_ascii("\\{");
2083 delim = from_ascii("\\}");
2086 delim = docstring(1, c);
2089 if (InsetMathBig::isBigInsetDelim(delim)) {
2090 // name + delim ared a valid InsetMathBig.
2091 // We can't use cur.macroModeClose() because
2092 // it does not handle delim.
2093 InsetMathUnknown * p = cur.activeMacro();
2096 cur.cell().erase(cur.pos());
2097 cur.plainInsert(MathAtom(
2098 new InsetMathBig(name.substr(1), delim)));
2101 } else if (name == "\\smash" && c == '[') {
2102 // We can't use cur.macroModeClose() because
2103 // it would create an InsetMathPhantom
2104 InsetMathUnknown * p = cur.activeMacro();
2106 interpretChar(cur, c);
2110 // leave macro mode and try again if necessary
2111 if (cur.macroModeClose()) {
2112 MathAtom const atom = cur.prevAtom();
2113 if (atom->asNestInset() && atom->isActive()) {
2115 cur.nextInset()->edit(cur, true);
2119 cur.niceInsert(MathAtom(new InsetMathBrace(buf)));
2121 interpretChar(cur, c);
2126 // just clear selection on pressing the space bar
2127 if (cur.selection() && c == ' ') {
2128 cur.selection(false);
2133 //lyxerr << "starting with macro" << endl;
2134 bool reduced = cap::reduceSelectionToOneCell(cur);
2135 if (reduced || !cur.selection()) {
2136 InsetMath const * im = cur.inset().asInsetMath();
2137 InsetMathMacro const * macro = im ? im->asMacro()
2139 bool in_macro_name = macro
2140 && macro->displayMode() ==
2141 InsetMathMacro::DISPLAY_UNFOLDED;
2142 cur.recordUndoInset();
2143 docstring const safe = cap::grabAndEraseSelection(cur);
2144 if (!cur.inRegexped() && !in_macro_name)
2145 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), safe, false)));
2147 cur.niceInsert(createInsetMath("backslash", buf));
2155 if (currentMode() != InsetMath::MATH_MODE)
2161 if (currentMode() != InsetMath::MATH_MODE) {
2162 // insert spaces in text or undecided mode,
2163 // but suppress direct insertion of two spaces in a row
2164 // the still allows typing '<space>a<space>' and deleting the 'a', but
2165 // it is better than nothing...
2166 pos_type const pos = cur.pos();
2167 pos_type const lastpos = cur.lastpos();
2168 if ((pos == 0 && lastpos == 0)
2169 || (pos == 0 && cur.nextAtom()->getChar() != ' ')
2170 || (pos == lastpos && cur.prevAtom()->getChar() != ' ')
2171 || (pos > 0 && cur.prevAtom()->getChar() != ' '
2172 && cur.nextAtom()->getChar() != ' ')) {
2174 // FIXME: we have to enable full redraw here because of the
2175 // visual box corners that define the inset. If we know for
2176 // sure that we stay within the same cell we can optimize for
2178 //cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2182 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
2183 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
2184 // FIXME: we have to enable full redraw here because of the
2185 // visual box corners that define the inset. If we know for
2186 // sure that we stay within the same cell we can optimize for
2188 //cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2192 if (cur.popForward()) {
2193 // FIXME: we have to enable full redraw here because of the
2194 // visual box corners that define the inset. If we know for
2195 // sure that we stay within the same cell we can optimize for
2197 //cur.screenUpdateFlags(Update::FitCursor);
2201 // if we are at the very end, leave the formula
2202 return cur.pos() != cur.lastpos();
2205 // These should be treated differently when not in text mode:
2206 if (cur.inRegexped()) {
2209 cur.niceInsert(createInsetMath("mathcircumflex", buf));
2216 cur.niceInsert(createInsetMath(docstring(1, c), buf));
2219 cur.niceInsert(createInsetMath("sim", buf));
2225 } else if (currentMode() != InsetMath::TEXT_MODE) {
2227 cur.recordUndoInset();
2228 script(cur, false, save_selection);
2232 cur.recordUndoInset();
2233 script(cur, true, save_selection);
2237 cur.niceInsert(createInsetMath("sim", buf));
2240 if (currentMode() == InsetMath::MATH_MODE && Encodings::isUnicodeTextOnly(c)) {
2241 MathAtom const atom(new InsetMathChar(c));
2242 if (cur.prevInset() && cur.prevInset()->asInsetMath()->name() == "text") {
2243 // reuse existing \text inset
2244 cur.prevInset()->asInsetMath()->cell(0).push_back(atom);
2246 MathAtom at = createInsetMath("text", buf);
2247 at.nucleus()->cell(0).push_back(atom);
2255 cur.niceInsert(createInsetMath("textasciicircum", buf));
2259 cur.niceInsert(createInsetMath("textasciitilde", buf));
2264 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
2265 c == '%' || c == '_') {
2266 cur.niceInsert(createInsetMath(docstring(1, c), buf));
2270 // no special circumstances, so insert the character without any fuss
2276 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
2278 if (str == "\\limits" || str == "\\nolimits") {
2279 if (cur.pos() > 0 && cur.prevMath().allowsLimitsChange()) {
2280 cur.prevMath().limits(str == "\\limits" ? LIMITS : NO_LIMITS);
2283 cur.message(bformat(_("Cannot apply %1$s here."), str));
2287 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
2289 if (!cur.empty() && cur.pos() > 0 &&
2290 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
2291 if (InsetMathBig::isBigInsetDelim(str)) {
2292 docstring prev = asString(cur.cell()[cur.pos() - 1]);
2293 if (prev[0] == '\\') {
2294 prev = prev.substr(1);
2295 latexkeys const * l = in_word_set(prev);
2296 if (l && l->inset == "big") {
2297 cur.recordUndoSelection();
2298 cur.cell()[cur.pos() - 1] =
2299 MathAtom(new InsetMathBig(prev, str));
2309 bool InsetMathNest::script(Cursor & cur, bool up)
2311 return script(cur, up, docstring());
2315 bool InsetMathNest::script(Cursor & cur, bool up,
2316 docstring const & save_selection)
2318 // Hack to get \^ and \_ working
2319 //lyxerr << "handling script: up: " << up << endl;
2320 if (cur.inMacroMode() && cur.macroName() == "\\") {
2322 cur.niceInsert(createInsetMath("mathcircumflex", cur.buffer()));
2324 interpretChar(cur, '_');
2328 cur.macroModeClose();
2329 if (asScriptInset() && cur.idx() == 0) {
2330 // we are in a nucleus of a script inset, move to _our_ script
2331 InsetMathScript * inset = asScriptInset();
2332 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
2334 cur.idx() = inset->idxOfScript(up);
2336 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
2338 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
2341 cur.idx() = inset->idxOfScript(up);
2342 cur.pos() = cur.lastpos();
2344 // convert the thing to our left to a scriptinset or create a new
2345 // one if in the very first position of the array
2346 if (cur.pos() == 0) {
2347 //lyxerr << "new scriptinset" << endl;
2348 cur.insert(new InsetMathScript(buffer_, up));
2350 //lyxerr << "converting prev atom " << endl;
2351 cur.prevAtom() = MathAtom(new InsetMathScript(buffer_, cur.prevAtom(), up));
2354 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
2355 // See comment in MathParser.cpp for special handling of {}-bases
2361 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
2362 cur.niceInsert(save_selection);
2364 //lyxerr << "inserting selection 2:\n" << save_selection << endl;
2369 bool InsetMathNest::completionSupported(Cursor const & cur) const
2371 return cur.inMacroMode();
2375 bool InsetMathNest::inlineCompletionSupported(Cursor const & cur) const
2377 return cur.inMacroMode();
2381 bool InsetMathNest::automaticInlineCompletion() const
2383 return lyxrc.completion_inline_math;
2387 bool InsetMathNest::automaticPopupCompletion() const
2389 return lyxrc.completion_popup_math;
2393 CompletionList const *
2394 InsetMathNest::createCompletionList(Cursor const & cur) const
2396 if (!cur.inMacroMode())
2399 return new MathCompletionList(cur);
2403 docstring InsetMathNest::completionPrefix(Cursor const & cur) const
2405 if (!cur.inMacroMode())
2408 return cur.activeMacro()->name();
2412 bool InsetMathNest::insertCompletion(Cursor & cur, docstring const & s, bool finished)
2414 if (cur.buffer()->isReadonly() || !cur.inMacroMode())
2417 // Contrary to Text, the whole inset should be recorded (#12581).
2418 cur.recordUndoInset();
2420 // append completion to active macro
2421 InsetMathUnknown * inset = cur.activeMacro();
2422 inset->setName(inset->name() + s);
2427 // FIXME: this creates duplicates in the completion popup
2428 // which looks ugly. Moreover the changes the list lengths
2429 // which seems to confuse the popup as well.
2430 MathCompletionList::addToFavorites(inset->name());
2432 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, " "));
2434 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2440 void InsetMathNest::completionPosAndDim(Cursor const & cur, int & x, int & y,
2441 Dimension & dim) const
2443 Inset const * inset = cur.activeMacro();
2447 // get inset dimensions
2448 dim = cur.bv().coordCache().insets().dim(inset);
2449 // FIXME: these 3 are no accurate, but should depend on the font.
2450 // Now the popup jumps down if you enter a char with descent > 0.
2455 Point xy = cur.bv().coordCache().insets().xy(inset);
2461 ////////////////////////////////////////////////////////////////////
2463 MathCompletionList::MathCompletionList(Cursor const & cur)
2465 // fill it with macros from the buffer
2466 MacroNameSet macros;
2467 cur.buffer()->listMacroNames(macros);
2468 MacroNameSet::const_iterator it;
2469 for (it = macros.begin(); it != macros.end(); ++it) {
2470 if (cur.buffer()->getMacro(*it, cur, false))
2471 locals.push_back("\\" + *it);
2473 sort(locals.begin(), locals.end());
2475 if (!globals.empty())
2478 // fill in global macros
2480 MacroTable::globalMacros().getMacroNames(macros, false);
2481 //lyxerr << "Globals completion macros: ";
2482 for (it = macros.begin(); it != macros.end(); ++it) {
2483 //lyxerr << "\\" + *it << " ";
2484 globals.push_back("\\" + *it);
2486 //lyxerr << std::endl;
2488 // fill in global commands
2489 globals.push_back(from_ascii("\\boxed"));
2490 globals.push_back(from_ascii("\\fbox"));
2491 globals.push_back(from_ascii("\\framebox"));
2492 globals.push_back(from_ascii("\\makebox"));
2493 globals.push_back(from_ascii("\\kern"));
2494 globals.push_back(from_ascii("\\xhookrightarrow"));
2495 globals.push_back(from_ascii("\\xhookleftarrow"));
2496 globals.push_back(from_ascii("\\xrightarrow"));
2497 globals.push_back(from_ascii("\\xRightarrow"));
2498 globals.push_back(from_ascii("\\xrightharpoondown"));
2499 globals.push_back(from_ascii("\\xrightharpoonup"));
2500 globals.push_back(from_ascii("\\xrightleftharpoons"));
2501 globals.push_back(from_ascii("\\xleftarrow"));
2502 globals.push_back(from_ascii("\\xLeftarrow"));
2503 globals.push_back(from_ascii("\\xleftharpoondown"));
2504 globals.push_back(from_ascii("\\xleftharpoonup"));
2505 globals.push_back(from_ascii("\\xleftrightarrow"));
2506 globals.push_back(from_ascii("\\xLeftrightarrow"));
2507 globals.push_back(from_ascii("\\xleftrightharpoons"));
2508 globals.push_back(from_ascii("\\xmapsto"));
2509 globals.push_back(from_ascii("\\split"));
2510 globals.push_back(from_ascii("\\gathered"));
2511 globals.push_back(from_ascii("\\aligned"));
2512 globals.push_back(from_ascii("\\alignedat"));
2513 globals.push_back(from_ascii("\\cases"));
2514 globals.push_back(from_ascii("\\substack"));
2515 globals.push_back(from_ascii("\\xymatrix"));
2516 globals.push_back(from_ascii("\\Diagram"));
2517 globals.push_back(from_ascii("\\subarray"));
2518 globals.push_back(from_ascii("\\array"));
2519 globals.push_back(from_ascii("\\sqrt"));
2520 globals.push_back(from_ascii("\\root"));
2521 globals.push_back(from_ascii("\\tabular"));
2522 globals.push_back(from_ascii("\\sideset"));
2523 globals.push_back(from_ascii("\\stackrel"));
2524 globals.push_back(from_ascii("\\stackrelthree"));
2525 globals.push_back(from_ascii("\\binom"));
2526 globals.push_back(from_ascii("\\choose"));
2527 globals.push_back(from_ascii("\\brace"));
2528 globals.push_back(from_ascii("\\brack"));
2529 globals.push_back(from_ascii("\\frac"));
2530 globals.push_back(from_ascii("\\over"));
2531 globals.push_back(from_ascii("\\nicefrac"));
2532 globals.push_back(from_ascii("\\unitfrac"));
2533 globals.push_back(from_ascii("\\unitfracthree"));
2534 globals.push_back(from_ascii("\\unitone"));
2535 globals.push_back(from_ascii("\\unittwo"));
2536 globals.push_back(from_ascii("\\infer"));
2537 globals.push_back(from_ascii("\\atop"));
2538 globals.push_back(from_ascii("\\lefteqn"));
2539 globals.push_back(from_ascii("\\boldsymbol"));
2540 globals.push_back(from_ascii("\\bm"));
2541 globals.push_back(from_ascii("\\color"));
2542 globals.push_back(from_ascii("\\normalcolor"));
2543 globals.push_back(from_ascii("\\textcolor"));
2544 globals.push_back(from_ascii("\\cfrac"));
2545 globals.push_back(from_ascii("\\cfracleft"));
2546 globals.push_back(from_ascii("\\cfracright"));
2547 globals.push_back(from_ascii("\\dfrac"));
2548 globals.push_back(from_ascii("\\tfrac"));
2549 globals.push_back(from_ascii("\\dbinom"));
2550 globals.push_back(from_ascii("\\tbinom"));
2551 globals.push_back(from_ascii("\\hphantom"));
2552 globals.push_back(from_ascii("\\phantom"));
2553 globals.push_back(from_ascii("\\vphantom"));
2554 globals.push_back(from_ascii("\\cancel"));
2555 globals.push_back(from_ascii("\\bcancel"));
2556 globals.push_back(from_ascii("\\xcancel"));
2557 globals.push_back(from_ascii("\\cancelto"));
2558 globals.push_back(from_ascii("\\smash"));
2559 globals.push_back(from_ascii("\\mathclap"));
2560 globals.push_back(from_ascii("\\mathllap"));
2561 globals.push_back(from_ascii("\\mathrlap"));
2562 globals.push_back(from_ascii("\\ensuremath"));
2563 MathWordList const & words = mathedWordList();
2564 MathWordList::const_iterator it2;
2565 //lyxerr << "Globals completion commands: ";
2566 for (it2 = words.begin(); it2 != words.end(); ++it2) {
2567 if (it2->second.inset != "macro" && !it2->second.hidden) {
2568 // macros are already read from MacroTable::globalMacros()
2569 globals.push_back('\\' + it2->first);
2570 //lyxerr << '\\' + it2->first << ' ';
2573 //lyxerr << std::endl;
2574 sort(globals.begin(), globals.end());
2578 MathCompletionList::~MathCompletionList()
2583 size_type MathCompletionList::size() const
2585 return locals.size() + globals.size();
2589 docstring const & MathCompletionList::data(size_t idx) const
2591 size_t lsize = locals.size();
2593 return globals[idx - lsize];
2599 std::string MathCompletionList::icon(size_t idx) const
2601 // get the latex command
2603 size_t lsize = locals.size();
2605 cmd = globals[idx - lsize];
2609 // get the icon name by stripping the backslash
2610 docstring icon_name = frontend::Application::mathIcon(cmd.substr(1));
2611 if (icon_name.empty())
2612 return std::string();
2613 return "math/" + to_utf8(icon_name);
2616 std::vector<docstring> MathCompletionList::globals;