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, MathData(buffer_)), 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
324 MathData ar(buffer_);
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 InsetMathFont const * f = asFontInset();
525 // Return if nothing has been set
526 if (!font.fromString(to_utf8(arg), b))
529 if (support::contains(arg, from_ascii("family"))) {
530 switch(font.fontInfo().family()) {
532 if (!f || (f->name() != "textrm" && f->name() != "mathrm"))
533 im = currentMode() != MATH_MODE ? from_ascii("textrm")
534 : from_ascii("mathrm");
537 if (!f || (f->name() != "textsf" && f->name() != "mathsf"))
538 im = currentMode() != MATH_MODE ? from_ascii("textsf")
539 : from_ascii("mathsf");
541 case TYPEWRITER_FAMILY:
542 if (!f || (f->name() != "texttt" && f->name() != "mathtt"))
543 im = currentMode() != MATH_MODE ? from_ascii("texttt")
544 : from_ascii("mathtt");
564 handleNest(cur, createInsetMath(im, cur.buffer()));
566 include_previous_change = true;
570 if (support::contains(arg, from_ascii("series"))) {
571 switch(font.fontInfo().series()) {
573 if (!f || (f->name() != "textmd" && f->name() != "mathrm"))
574 im = currentMode() != MATH_MODE ? from_ascii("textmd")
575 : from_ascii("mathrm");
578 if (!f || (f->name() != "textbf" && f->name() != "mathbf"))
579 im = currentMode() != MATH_MODE ? from_ascii("textbf")
580 : from_ascii("boldsymbol");
587 if (multiple_cells) {
588 cur.setCursor(sel_begin);
592 cur.setCursor(sel_end);
593 cur.pos() = cur.lastpos();
595 } else if (include_previous_change && selection) {
598 handleNest(cur, createInsetMath(im, cur.buffer()));
600 include_previous_change = true;
604 if (support::contains(arg, from_ascii("shape"))) {
605 switch(font.fontInfo().shape()) {
607 if (!f || (f->name() != "textup" && f->name() != "mathrm"))
608 im = currentMode() != MATH_MODE ? from_ascii("textup")
609 : from_ascii("mathrm");
612 if (!f || (f->name() != "textit" && f->name() != "mathit"))
613 im = currentMode() != MATH_MODE ? from_ascii("textit")
614 : from_ascii("mathit");
617 if (!f || f->name() != "textsl")
618 im = currentMode() != MATH_MODE ? from_ascii("textsl")
621 case SMALLCAPS_SHAPE:
622 if (!f || f->name() != "textsc")
623 im = currentMode() != MATH_MODE ? from_ascii("textsc")
631 if (multiple_cells) {
632 cur.setCursor(sel_begin);
636 cur.setCursor(sel_end);
637 cur.pos() = cur.lastpos();
639 } else if (include_previous_change && selection) {
642 handleNest(cur, createInsetMath(im, cur.buffer()));
644 include_previous_change = true;
648 if (support::contains(arg, from_ascii("size"))) {
649 switch(font.fontInfo().size()) {
651 im = from_ascii("tiny");
654 im = from_ascii("scriptsize");
657 im = from_ascii("footnotesize");
660 im = from_ascii("small");
663 im = from_ascii("normalsize");
666 im = from_ascii("large");
669 im = from_ascii("Large");
672 im = from_ascii("LARGE");
675 im = from_ascii("huge");
678 im = from_ascii("Huge");
687 if (multiple_cells) {
688 cur.setCursor(sel_begin);
692 cur.setCursor(sel_end);
693 cur.pos() = cur.lastpos();
695 } else if (include_previous_change && selection) {
698 handleNest(cur, createInsetMath(im, cur.buffer()));
700 include_previous_change = true;
704 InsetMathDecoration const * d = multiple_cells
705 ? nullptr : asDecorationInset();
706 docstring const name = d ? d->name() : docstring();
708 if ((font.fontInfo().underbar() == FONT_OFF && name == "uline") ||
709 (font.fontInfo().uuline() == FONT_OFF && name == "uuline") ||
710 (font.fontInfo().uwave() == FONT_OFF && name == "uwave")) {
711 if (include_previous_change) {
716 docstring const beg = '\\' + name + '{';
717 docstring const end = from_ascii("}");
718 docstring const sel2 = cur.selectionAsString(false);
719 cutSelection(cur, false);
722 docstring const sel1 = cur.selectionAsString(false);
723 cur.pos() = cur.lastpos();
725 docstring const sel3 = cur.selectionAsString(false);
726 cur.mathForward(false);
728 cutSelection(cur, false);
729 MathData ar(buffer_);
731 mathed_parse_cell(ar, beg + sel1 + end);
736 mathed_parse_cell(ar, sel2);
740 pos_type pos = cur.pos();
741 mathed_parse_cell(ar, beg + sel3 + end);
746 sel_begin = cur.selectionBegin();
747 sel_end = cur.selectionEnd();
748 include_previous_change = false;
751 if (font.fontInfo().underbar() == FONT_ON) {
752 if (!d || name != "uline")
753 im = from_ascii("uline");
754 } else if (font.fontInfo().uuline() == FONT_ON) {
755 if (!d || name != "uuline")
756 im = from_ascii("uuline");
757 } else if (font.fontInfo().uwave() == FONT_ON) {
758 if (!d || name != "uwave")
759 im = from_ascii("uwave");
763 if (multiple_cells) {
764 cur.setCursor(sel_begin);
768 cur.setCursor(sel_end);
769 cur.pos() = cur.lastpos();
771 } else if (include_previous_change && selection) {
774 handleNest(cur, createInsetMath(im, cur.buffer()));
776 include_previous_change = true;
779 if (support::contains(arg, from_ascii("color"))) {
780 if (font.fontInfo().color() != Color_inherit &&
781 font.fontInfo().color() != Color_ignore) {
782 if (multiple_cells) {
783 cur.setCursor(sel_begin);
787 cur.setCursor(sel_end);
788 cur.pos() = cur.lastpos();
790 } else if (include_previous_change && selection) {
793 handleNest(cur, MathAtom(new InsetMathColor(buffer_, true, font.fontInfo().color())));
794 include_previous_change = true;
799 if (multiple_cells) {
800 cur.setCursor(sel_begin);
804 cur.setCursor(sel_end);
805 cur.pos() = cur.lastpos();
808 if (include_previous_change) {
812 cur.setCursor(sel_begin);
815 cur.setCursor(sel_end);
820 // FIXME: support other font changes here as well?
823 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
825 //LYXERR0("InsetMathNest: request: " << cmd);
827 Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
829 FuncCode const act = cmd.action();
832 case LFUN_CLIPBOARD_PASTE:
833 parseflg |= Parse::VERBATIM;
836 if (cur.currentMode() != MATH_MODE)
837 parseflg |= Parse::TEXTMODE;
838 cur.recordUndoSelection();
839 cur.message(_("Paste"));
840 replaceSelection(cur);
842 if (cmd.argument().empty() && !theClipboard().isInternal())
843 topaste = theClipboard().getAsText(frontend::Clipboard::PlainTextType);
846 idocstringstream is(cmd.argument());
848 topaste = cap::selection(n, make_pair(buffer().params().documentClassPtr(),
849 buffer().params().authors()));
851 InsetMath const * im = cur.inset().asInsetMath();
852 InsetMathMacro const * macro = im ? im->asMacro() : nullptr;
853 // do not allow pasting a backslash in the name of a macro
855 && macro->displayMode() == InsetMathMacro::DISPLAY_UNFOLDED
856 && support::contains(topaste, char_type('\\'))) {
857 LYXERR0("Removing backslash from pasted string");
858 topaste = subst(topaste, from_ascii("\\"), docstring());
860 cur.niceInsert(topaste, parseflg, false);
861 cur.clearSelection(); // bug 393
862 cur.forceBufferUpdate();
869 cutSelection(cur, true);
870 cur.message(_("Cut"));
871 // Prevent stale position >= size crash
872 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
874 cur.forceBufferUpdate();
877 case LFUN_MOUSE_PRESS:
878 lfunMousePress(cur, cmd);
881 case LFUN_MOUSE_MOTION:
882 lfunMouseMotion(cur, cmd);
885 case LFUN_MOUSE_RELEASE:
886 lfunMouseRelease(cur, cmd);
889 case LFUN_FINISHED_LEFT: // in math, left is backwards
890 case LFUN_FINISHED_BACKWARD:
891 cur.bv().cursor() = cur;
894 case LFUN_FINISHED_RIGHT: // in math, right is forward
895 case LFUN_FINISHED_FORWARD:
897 cur.bv().cursor() = cur;
900 case LFUN_WORD_RIGHT:
902 case LFUN_WORD_BACKWARD:
903 case LFUN_WORD_FORWARD:
904 case LFUN_CHAR_RIGHT:
906 case LFUN_CHAR_BACKWARD:
907 case LFUN_CHAR_FORWARD:
908 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
910 case LFUN_WORD_RIGHT_SELECT:
911 case LFUN_WORD_LEFT_SELECT:
912 case LFUN_WORD_BACKWARD_SELECT:
913 case LFUN_WORD_FORWARD_SELECT:
914 case LFUN_CHAR_RIGHT_SELECT:
915 case LFUN_CHAR_LEFT_SELECT:
916 case LFUN_CHAR_BACKWARD_SELECT:
917 case LFUN_CHAR_FORWARD_SELECT: {
918 // are we in a selection?
919 bool select = (act == LFUN_WORD_RIGHT_SELECT
920 || act == LFUN_WORD_LEFT_SELECT
921 || act == LFUN_WORD_BACKWARD_SELECT
922 || act == LFUN_WORD_FORWARD_SELECT
923 || act == LFUN_CHAR_RIGHT_SELECT
924 || act == LFUN_CHAR_LEFT_SELECT
925 || act == LFUN_CHAR_BACKWARD_SELECT
926 || act == LFUN_CHAR_FORWARD_SELECT);
928 bool word = (act == LFUN_WORD_RIGHT_SELECT
929 || act == LFUN_WORD_LEFT_SELECT
930 || act == LFUN_WORD_BACKWARD_SELECT
931 || act == LFUN_WORD_FORWARD_SELECT
932 || act == LFUN_WORD_RIGHT
933 || act == LFUN_WORD_LEFT
934 || act == LFUN_WORD_BACKWARD
935 || act == LFUN_WORD_FORWARD);
936 // are we moving forward or backwards?
937 // If the command was RIGHT or LEFT, then whether we're moving forward
938 // or backwards depends on the cursor movement mode (logical or visual):
939 // * in visual mode, since math is always LTR, right -> forward,
941 // * in logical mode, the mapping is determined by the
942 // reverseDirectionNeeded() function
945 FuncCode finish_lfun;
947 if (act == LFUN_CHAR_FORWARD
948 || act == LFUN_CHAR_FORWARD_SELECT
949 || act == LFUN_WORD_FORWARD
950 || act == LFUN_WORD_FORWARD_SELECT) {
952 finish_lfun = LFUN_FINISHED_FORWARD;
954 else if (act == LFUN_CHAR_BACKWARD
955 || act == LFUN_CHAR_BACKWARD_SELECT
956 || act == LFUN_WORD_BACKWARD
957 || act == LFUN_WORD_BACKWARD_SELECT) {
959 finish_lfun = LFUN_FINISHED_BACKWARD;
962 bool right = (act == LFUN_CHAR_RIGHT_SELECT
963 || act == LFUN_CHAR_RIGHT
964 || act == LFUN_WORD_RIGHT_SELECT
965 || act == LFUN_WORD_RIGHT);
966 if (lyxrc.visual_cursor || !cur.reverseDirectionNeeded())
972 finish_lfun = LFUN_FINISHED_RIGHT;
974 finish_lfun = LFUN_FINISHED_LEFT;
976 // Now that we know exactly what we want to do, let's do it!
977 cur.selHandle(select);
979 cur.macroModeClose();
980 // try moving forward or backwards as necessary...
981 if (!(forward ? cur.mathForward(word) : cur.mathBackward(word))) {
982 // ... and if movement failed, then finish forward or backwards
984 cmd = FuncRequest(finish_lfun);
992 case LFUN_PARAGRAPH_UP:
993 case LFUN_PARAGRAPH_DOWN:
994 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
996 case LFUN_DOWN_SELECT:
998 case LFUN_PARAGRAPH_UP_SELECT:
999 case LFUN_PARAGRAPH_DOWN_SELECT: {
1000 // close active macro
1001 if (cur.inMacroMode()) {
1002 cur.macroModeClose();
1006 // stop/start the selection
1007 bool select = act == LFUN_DOWN_SELECT
1008 || act == LFUN_UP_SELECT
1009 || act == LFUN_PARAGRAPH_DOWN_SELECT
1010 || act == LFUN_PARAGRAPH_UP_SELECT;
1011 cur.selHandle(select);
1014 bool up = act == LFUN_UP || act == LFUN_UP_SELECT
1015 || act == LFUN_PARAGRAPH_UP || act == LFUN_PARAGRAPH_UP_SELECT;
1016 bool successful = cur.upDownInMath(up);
1020 if (cur.fixIfBroken())
1021 // FIXME: Something bad happened. We pass the corrected Cursor
1022 // instead of letting things go worse.
1025 // We did not manage to move the cursor.
1030 case LFUN_MOUSE_DOUBLE:
1031 case LFUN_WORD_SELECT:
1033 cur.bv().mouseSetCursor(cur);
1034 cur.pos() = cur.lastpos();
1035 cur.bv().mouseSetCursor(cur, true);
1038 case LFUN_MOUSE_TRIPLE:
1041 cur.bv().mouseSetCursor(cur);
1042 cur.idx() = cur.lastidx();
1043 cur.pos() = cur.lastpos();
1044 cur.bv().mouseSetCursor(cur, true);
1047 case LFUN_LINE_BEGIN:
1048 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
1050 case LFUN_LINE_BEGIN_SELECT:
1051 cur.selHandle(act == LFUN_WORD_BACKWARD_SELECT ||
1052 act == LFUN_WORD_LEFT_SELECT ||
1053 act == LFUN_LINE_BEGIN_SELECT);
1054 cur.macroModeClose();
1055 if (cur.pos() != 0) {
1057 } else if (cur.col() != 0) {
1058 cur.idx() -= cur.col();
1060 } else if (cur.idx() != 0) {
1064 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1070 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
1072 case LFUN_LINE_END_SELECT:
1073 cur.selHandle(act == LFUN_WORD_FORWARD_SELECT ||
1074 act == LFUN_WORD_RIGHT_SELECT ||
1075 act == LFUN_LINE_END_SELECT);
1076 cur.macroModeClose();
1078 if (cur.pos() != cur.lastpos()) {
1079 cur.pos() = cur.lastpos();
1080 } else if (ncols() && (cur.col() != cur.lastcol())) {
1081 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
1082 cur.pos() = cur.lastpos();
1083 } else if (cur.idx() != cur.lastidx()) {
1084 cur.idx() = cur.lastidx();
1085 cur.pos() = cur.lastpos();
1087 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1092 case LFUN_CELL_FORWARD:
1093 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
1094 cur.selHandle(false);
1096 cur.macroModeClose();
1097 if (!cur.inset().idxNext(cur)) {
1098 if (cur.lastidx() == 0)
1101 cur.idx() = firstIdx();
1107 case LFUN_CELL_BACKWARD:
1108 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor);
1109 cur.selHandle(false);
1111 cur.macroModeClose();
1112 if (!cur.inset().idxPrev(cur)) {
1113 if (cur.lastidx() == 0)
1116 cur.idx() = cur.lastidx();
1117 cur.pos() = lyxrc.mac_like_cursor_movement ? cur.lastpos() : 0;
1122 case LFUN_WORD_DELETE_BACKWARD:
1123 case LFUN_CHAR_DELETE_BACKWARD:
1125 // May affect external cell:
1126 cur.recordUndoInset();
1127 else if (!cur.inMacroMode())
1128 cur.recordUndoSelection();
1129 // if the inset can not be removed from within, delete it
1130 if (!cur.backspace(cmd.getArg(0) != "confirm")) {
1131 FuncRequest newcmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
1132 cur.innerText()->dispatch(cur, newcmd);
1136 case LFUN_WORD_DELETE_FORWARD:
1137 case LFUN_CHAR_DELETE_FORWARD:
1138 if (cur.pos() == cur.lastpos())
1139 // May affect external cell:
1140 cur.recordUndoInset();
1142 cur.recordUndoSelection();
1143 // if the inset can not be removed from within, delete it
1144 if (!cur.erase(cmd.getArg(0) != "confirm")) {
1145 FuncRequest newcmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
1146 cur.innerText()->dispatch(cur, newcmd);
1151 if (cur.selection())
1152 cur.clearSelection();
1154 if (cur.inMacroMode())
1155 cur.macroModeClose(true);
1157 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1164 // FIXME: resurrect this later
1165 // 'Locks' the math inset. A 'locked' math inset behaves as a unit
1166 // that is traversed by a single <CursorLeft>/<CursorRight>.
1167 case LFUN_INSET_TOGGLE:
1174 case LFUN_SELF_INSERT:
1175 // special case first for big delimiters
1176 if (cmd.argument().size() != 1 && interpretString(cur, cmd.argument()))
1179 for (char_type c : cmd.argument()) {
1180 // Don't record undo steps if we are in macro mode and thus
1181 // cmd.argument is the next character of the macro name.
1182 // Otherwise we'll get an invalid cursor if we undo after
1183 // the macro was finished and the macro is a known command,
1184 // e.g. sqrt. Cursor::macroModeClose replaces in this case
1185 // the InsetMathUnknown with name "frac" by an empty
1186 // InsetMathFrac -> a pos value > 0 is invalid.
1187 // A side effect is that an undo before the macro is finished
1188 // undoes the complete macro, not only the last character.
1189 // At the time we hit '\' we are not in macro mode, still.
1190 if (!cur.inMacroMode())
1191 cur.recordUndoSelection();
1193 // special handling of space. If we insert an inset
1194 // via macro mode, we want to put the cursor inside it
1195 // if relevant. Think typing "\frac<space>".
1197 && cur.inMacroMode() && cur.macroName() != "\\"
1198 && cur.macroModeClose() && cur.pos() > 0)
1199 cur.editInsertedInset();
1200 else if (!interpretChar(cur, c)) {
1201 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1203 // FIXME: can we avoid skipping the end of the string?
1209 //case LFUN_SERVER_GET_XY:
1212 case LFUN_SERVER_SET_XY: {
1213 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
1216 istringstream is(to_utf8(cmd.argument()));
1222 // Special casing for superscript in case of LyX handling
1224 case LFUN_ACCENT_CIRCUMFLEX:
1225 if (cmd.argument().empty()) {
1226 // do superscript if LyX handles
1228 cur.recordUndoInset();
1229 script(cur, true, grabAndEraseSelection(cur));
1233 case LFUN_ACCENT_UMLAUT:
1234 case LFUN_ACCENT_ACUTE:
1235 case LFUN_ACCENT_GRAVE:
1236 case LFUN_ACCENT_BREVE:
1237 case LFUN_ACCENT_DOT:
1238 case LFUN_ACCENT_MACRON:
1239 case LFUN_ACCENT_CARON:
1240 case LFUN_ACCENT_TILDE:
1241 case LFUN_ACCENT_CEDILLA:
1242 case LFUN_ACCENT_CIRCLE:
1243 case LFUN_ACCENT_UNDERDOT:
1244 case LFUN_ACCENT_TIE:
1245 case LFUN_ACCENT_OGONEK:
1246 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1250 case LFUN_TEXTSTYLE_APPLY:
1251 case LFUN_TEXTSTYLE_UPDATE:
1252 handleFont2(cur, cmd.argument());
1255 case LFUN_FONT_BOLD:
1256 if (currentMode() != MATH_MODE)
1257 handleFont(cur, cmd.argument(), "textbf");
1259 handleFont(cur, cmd.argument(), "mathbf");
1261 case LFUN_FONT_BOLDSYMBOL:
1262 if (currentMode() != MATH_MODE)
1263 handleFont(cur, cmd.argument(), "textbf");
1265 handleFont(cur, cmd.argument(), "boldsymbol");
1267 case LFUN_FONT_SANS:
1268 if (currentMode() != MATH_MODE)
1269 handleFont(cur, cmd.argument(), "textsf");
1271 handleFont(cur, cmd.argument(), "mathsf");
1273 case LFUN_FONT_EMPH:
1274 if (currentMode() != MATH_MODE)
1275 handleFont(cur, cmd.argument(), "emph");
1277 handleFont(cur, cmd.argument(), "mathcal");
1279 case LFUN_FONT_ROMAN:
1280 if (currentMode() != MATH_MODE)
1281 handleFont(cur, cmd.argument(), "textrm");
1283 handleFont(cur, cmd.argument(), "mathrm");
1285 case LFUN_FONT_TYPEWRITER:
1286 if (currentMode() != MATH_MODE)
1287 handleFont(cur, cmd.argument(), "texttt");
1289 handleFont(cur, cmd.argument(), "mathtt");
1291 case LFUN_FONT_FRAK:
1292 handleFont(cur, cmd.argument(), "mathfrak");
1294 case LFUN_FONT_ITAL:
1295 if (currentMode() != MATH_MODE)
1296 handleFont(cur, cmd.argument(), "textit");
1298 handleFont(cur, cmd.argument(), "mathit");
1300 case LFUN_FONT_NOUN:
1301 if (currentMode() != MATH_MODE)
1302 // FIXME: should be "noun"
1303 handleFont(cur, cmd.argument(), "textsc");
1305 handleFont(cur, cmd.argument(), "mathbb");
1307 case LFUN_FONT_DEFAULT:
1308 handleFont(cur, cmd.argument(), "textnormal");
1310 case LFUN_FONT_UNDERLINE:
1312 cur.handleNest(createInsetMath("underline", cur.buffer()));
1315 case LFUN_MATH_MODE: {
1317 // ignore math-mode on when already in math mode
1318 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
1320 cur.recordUndoSelection();
1321 cur.macroModeClose();
1322 docstring const save_selection = grabAndEraseSelection(cur);
1324 if (currentMode() != Inset::MATH_MODE)
1325 cur.plainInsert(MathAtom(new InsetMathEnsureMath(buffer_)));
1327 cur.plainInsert(createInsetMath("text", buffer_));
1329 cur.pushBackward(*cur.nextInset());
1330 cur.niceInsert(save_selection);
1331 cur.forceBufferUpdate();
1333 if (currentMode() == Inset::TEXT_MODE) {
1334 cur.recordUndoSelection();
1335 cur.niceInsert(MathAtom(new InsetMathHull("simple", cur.buffer())));
1336 cur.message(_("create new math text environment ($...$)"));
1338 handleFont(cur, cmd.argument(), "textrm");
1339 cur.message(_("entered math text mode (textrm)"));
1345 case LFUN_REGEXP_MODE: {
1346 InsetMath * im = cur.inset().asInsetMath();
1348 InsetMathHull * i = im->asHullInset();
1349 if (i && i->getType() == hullRegexp) {
1350 cur.message(_("Already in regular expression mode"));
1354 cur.macroModeClose();
1355 docstring const save_selection = grabAndEraseSelection(cur);
1357 cur.plainInsert(MathAtom(new InsetMathHull(buffer_, hullRegexp)));
1359 cur.pushBackward(*cur.nextInset());
1360 cur.niceInsert(save_selection);
1361 cur.message(_("Regular expression editor mode"));
1365 case LFUN_MATH_FONT_STYLE: {
1366 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, '\\' + cmd.argument());
1367 doDispatch(cur, fr);
1371 case LFUN_MATH_SIZE: {
1372 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, cmd.argument());
1373 doDispatch(cur, fr);
1377 case LFUN_MATH_MATRIX: {
1383 idocstringstream is(cmd.argument());
1384 is >> m >> n >> v_align >> h_align;
1390 cur.niceInsert(MathAtom(new InsetMathArray(buffer_,
1391 from_ascii("array"), m, n, (char)v_align[0], h_align)));
1395 case LFUN_MATH_AMS_MATRIX: {
1399 docstring name = from_ascii("matrix");
1400 idocstringstream is(cmd.argument());
1401 is >> m >> n >> name;
1406 // check if we have a valid decoration
1407 if (name != "pmatrix" && name != "bmatrix"
1408 && name != "Bmatrix" && name != "vmatrix"
1409 && name != "Vmatrix" && name != "matrix"
1410 && name != "smallmatrix")
1411 name = from_ascii("matrix");
1414 MathAtom(new InsetMathAMSArray(buffer_, name, m, n)));
1418 case LFUN_MATH_DELIM: {
1420 docstring rs = split(cmd.argument(), ls, ' ');
1421 // Reasonable default values
1427 cur.handleNest(MathAtom(new InsetMathDelim(buffer_, ls, rs)));
1431 case LFUN_MATH_BIGDELIM: {
1432 docstring const lname = from_utf8(cmd.getArg(0));
1433 docstring const ldelim = from_utf8(cmd.getArg(1));
1434 docstring const rname = from_utf8(cmd.getArg(2));
1435 docstring const rdelim = from_utf8(cmd.getArg(3));
1436 latexkeys const * l = in_word_set(lname);
1437 bool const have_l = l && l->inset == "big" &&
1438 InsetMathBig::isBigInsetDelim(ldelim);
1439 l = in_word_set(rname);
1440 bool const have_r = l && l->inset == "big" &&
1441 InsetMathBig::isBigInsetDelim(rdelim);
1442 // We mimic LFUN_MATH_DELIM in case we have an empty left
1443 // or right delimiter.
1444 if (have_l || have_r) {
1446 docstring const selection = grabAndEraseSelection(cur);
1449 cur.insert(MathAtom(new InsetMathBig(buffer_, lname, ldelim)));
1450 // first insert the right delimiter and then go back
1451 // and re-insert the selection (bug 7088)
1453 cur.insert(MathAtom(new InsetMathBig(buffer_, rname, rdelim)));
1456 cur.niceInsert(selection);
1458 // Don't call cur.undispatched() if we did nothing, this would
1459 // lead to infinite recursion via Text::dispatch().
1463 case LFUN_SPACE_INSERT: {
1464 cur.recordUndoSelection();
1465 string const name = cmd.getArg(0);
1466 if (name == "normal")
1467 cur.insert(MathAtom(new InsetMathSpace(buffer_, " ", "")));
1468 else if (name == "protected")
1469 cur.insert(MathAtom(new InsetMathSpace(buffer_, "~", "")));
1470 else if (name == "thin" || name == "med" || name == "thick")
1471 cur.insert(MathAtom(new InsetMathSpace(buffer_, name + "space", "")));
1472 else if (name == "hfill*")
1473 cur.insert(MathAtom(new InsetMathSpace(buffer_, "hspace*{\\fill}", "")));
1474 else if (name == "quad" || name == "qquad" ||
1475 name == "enspace" || name == "enskip" ||
1476 name == "negthinspace" || name == "negmedspace" ||
1477 name == "negthickspace" || name == "hfill")
1478 cur.insert(MathAtom(new InsetMathSpace(buffer_, name, "")));
1479 else if (name == "hspace" || name == "hspace*") {
1480 string const len = cmd.getArg(1);
1481 if (len.empty() || !isValidLength(len)) {
1482 lyxerr << "LyX function 'space-insert " << name << "' "
1483 "needs a valid length argument." << endl;
1486 cur.insert(MathAtom(new InsetMathSpace(buffer_, name, len)));
1488 cur.insert(MathAtom(new InsetMathSpace(buffer_)));
1492 case LFUN_MATH_SPACE:
1493 cur.recordUndoSelection();
1494 if (cmd.argument().empty())
1495 cur.insert(MathAtom(new InsetMathSpace(buffer_)));
1497 string const name = cmd.getArg(0);
1498 string const len = cmd.getArg(1);
1499 cur.insert(MathAtom(new InsetMathSpace(buffer_, name, len)));
1503 case LFUN_ERT_INSERT:
1504 // interpret this as if a backslash was typed
1506 interpretChar(cur, '\\');
1509 case LFUN_MATH_SUBSCRIPT:
1510 // interpret this as if a _ was typed
1511 cur.recordUndoSelection();
1512 interpretChar(cur, '_');
1515 case LFUN_MATH_SUPERSCRIPT:
1516 // interpret this as if a ^ was typed
1517 cur.recordUndoSelection();
1518 interpretChar(cur, '^');
1521 case LFUN_MATH_MACRO_FOLD:
1522 case LFUN_MATH_MACRO_UNFOLD: {
1524 bool fold = act == LFUN_MATH_MACRO_FOLD;
1525 bool found = findMacroToFoldUnfold(it, fold);
1527 InsetMathMacro * macro = it.nextInset()->asInsetMath()->asMacro();
1528 cur.recordUndoInset();
1537 case LFUN_QUOTE_INSERT:
1538 // interpret this as if a straight " was typed
1539 cur.recordUndoSelection();
1540 interpretChar(cur, '\"');
1543 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
1544 // handling such that "self-insert" works on "arbitrary stuff" too, and
1545 // math-insert only handles special math things like "matrix".
1546 case LFUN_MATH_INSERT: {
1547 cur.recordUndoSelection();
1548 if (cmd.argument() == "^" || cmd.argument() == "_")
1549 interpretChar(cur, cmd.argument()[0]);
1550 else if (!cur.selection())
1551 cur.niceInsert(cmd.argument());
1553 MathData ar(cur.buffer());
1554 asArray(cmd.argument(), ar);
1556 && ar[0]->asNestInset()
1557 && ar[0]->asNestInset()->nargs() > 1)
1558 handleNest(cur, ar[0]);
1560 cur.niceInsert(cmd.argument());
1565 case LFUN_DIALOG_SHOW_NEW_INSET: {
1566 docstring const & name = cmd.argument();
1568 if (name == "ref") {
1569 InsetMathRef tmp(buffer_, name);
1570 data = tmp.createDialogStr();
1571 cur.bv().showDialog(to_utf8(name), data);
1572 } else if (name == "mathspace") {
1573 cur.bv().showDialog(to_utf8(name));
1578 case LFUN_INSET_INSERT: {
1579 MathData ar(buffer_);
1580 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1581 cur.recordUndoSelection();
1583 cur.forceBufferUpdate();
1588 case LFUN_INSET_DISSOLVE:
1589 if (cmd.argument().empty() && !asHullInset() && !asMacroTemplate()) {
1590 // we have been triggered via the AtPoint mechanism
1591 if (cur.nextInset() == this)
1593 cur.recordUndoInset();
1598 case LFUN_MATH_LIMITS: {
1600 if (cur.pos() < cur.lastpos() && cur.nextMath().allowsLimitsChange())
1601 in = &cur.nextMath();
1602 else if (cur.pos() > 0 && cur.prevMath().allowsLimitsChange())
1603 in = &cur.prevMath();
1604 else if (cur.lastpos() > 0 && cur.cell().back()->allowsLimitsChange())
1605 in = cur.cell().back().nucleus();
1606 // only when nucleus allows this
1609 cur.recordUndoInset();
1610 if (!cmd.argument().empty()) {
1611 if (cmd.argument() == "limits")
1613 else if (cmd.argument() == "nolimits")
1614 in->limits(NO_LIMITS);
1616 in->limits(AUTO_LIMITS);
1617 } else if (in->limits() != AUTO_LIMITS)
1618 in->limits(AUTO_LIMITS);
1619 else if (in->defaultLimits(cur.cell().displayStyle()) == LIMITS)
1620 in->limits(NO_LIMITS);
1626 case LFUN_PHANTOM_INSERT: {
1627 docstring const & arg = cmd.argument();
1629 if (arg == "Phantom")
1630 newarg = from_ascii("\\phantom");
1631 else if (arg == "HPhantom")
1632 newarg = from_ascii("\\hphantom");
1633 else if (arg == "VPhantom")
1634 newarg = from_ascii("\\vphantom");
1636 LYXERR0("Unknown phantom type " + newarg);
1638 FuncRequest const newfunc(LFUN_MATH_INSERT, newarg);
1639 lyx::dispatch(newfunc);
1645 InsetMath::doDispatch(cur, cmd);
1651 bool InsetMathNest::findMacroToFoldUnfold(Cursor & it, bool fold) const {
1652 // look for macro to open/close, but stay in mathed
1653 for (; !it.empty(); it.pop_back()) {
1655 // go backward through the current cell
1656 Inset * inset = it.nextInset();
1657 while (inset && inset->asInsetMath()) {
1658 InsetMathMacro * macro = inset->asInsetMath()->asMacro();
1660 // found the an macro to open/close?
1661 if (macro->folded() != fold)
1664 // Wrong folding state.
1665 // If this was the first we see in this slice, look further left,
1667 if (inset != it.nextInset())
1671 // go up if this was the left most position
1677 inset = it.nextInset();
1685 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1686 FuncStatus & flag) const
1688 // the font related toggles
1689 //string tc = "mathnormal";
1691 string const arg = to_utf8(cmd.argument());
1692 switch (cmd.action()) {
1694 case LFUN_INSET_MODIFY:
1695 // FIXME: check temporarily disabled
1697 char align = mathcursor::valign();
1698 if (align == '\0') {
1702 if (cmd.argument().empty()) {
1706 if (!contains("tcb", cmd.argument()[0])) {
1710 flag.setOnOff(cmd.argument()[0] == align);
1713 /// We have to handle them since 1.4 blocks all unhandled actions
1714 case LFUN_FONT_ITAL:
1715 case LFUN_FONT_BOLD:
1716 case LFUN_FONT_BOLDSYMBOL:
1717 case LFUN_FONT_SANS:
1718 case LFUN_FONT_EMPH:
1719 case LFUN_FONT_TYPEWRITER:
1720 case LFUN_FONT_NOUN:
1721 case LFUN_FONT_ROMAN:
1722 case LFUN_FONT_DEFAULT:
1723 flag.setEnabled(true);
1726 // we just need to be in math mode to enable that
1727 case LFUN_MATH_SIZE:
1728 case LFUN_MATH_SPACE:
1729 case LFUN_MATH_EXTERN:
1730 flag.setEnabled(true);
1733 case LFUN_FONT_UNDERLINE:
1734 case LFUN_FONT_FRAK:
1735 flag.setEnabled(currentMode() != TEXT_MODE);
1738 case LFUN_MATH_FONT_STYLE: {
1739 bool const textarg =
1740 arg == "textbf" || arg == "textsf" ||
1741 arg == "textrm" || arg == "textmd" ||
1742 arg == "textit" || arg == "textsc" ||
1743 arg == "textsl" || arg == "textup" ||
1744 arg == "texttt" || arg == "textbb" ||
1745 arg == "textnormal";
1746 flag.setEnabled(currentMode() != TEXT_MODE || textarg);
1750 case LFUN_MATH_MODE:
1751 // forbid "math-mode on" in math mode to prevent irritating
1752 // behaviour of menu entries (bug 6709)
1753 flag.setEnabled(currentMode() == TEXT_MODE || arg != "on");
1756 case LFUN_MATH_INSERT:
1757 flag.setEnabled(currentMode() != TEXT_MODE);
1760 case LFUN_MATH_AMS_MATRIX:
1761 case LFUN_MATH_MATRIX:
1762 flag.setEnabled(currentMode() == MATH_MODE);
1765 case LFUN_INSET_INSERT: {
1766 // Don't test createMathInset_fromDialogStr(), since
1767 // getStatus is not called with a valid reference and the
1768 // dialog would not be applicable.
1769 string const name = cmd.getArg(0);
1770 flag.setEnabled(name == "ref" || name == "mathspace");
1774 case LFUN_DIALOG_SHOW_NEW_INSET: {
1775 docstring const & name = cmd.argument();
1776 flag.setEnabled(name == "ref" || name == "mathspace");
1781 case LFUN_MATH_DELIM:
1782 case LFUN_MATH_BIGDELIM:
1783 // Don't do this with multi-cell selections
1784 flag.setEnabled(cur.selBegin().idx() == cur.selEnd().idx());
1787 case LFUN_MATH_MACRO_FOLD:
1788 case LFUN_MATH_MACRO_UNFOLD: {
1790 bool found = findMacroToFoldUnfold(it, cmd.action() == LFUN_MATH_MACRO_FOLD);
1791 flag.setEnabled(found);
1795 case LFUN_SPECIALCHAR_INSERT:
1796 case LFUN_SCRIPT_INSERT:
1797 // FIXME: These would probably make sense in math-text mode
1798 flag.setEnabled(false);
1801 // these are nonfunctional in math
1802 case LFUN_BOX_INSERT:
1803 case LFUN_BRANCH_INSERT:
1804 case LFUN_BRANCH_ADD_INSERT:
1805 case LFUN_CAPTION_INSERT:
1806 case LFUN_FLEX_INSERT:
1807 case LFUN_FLOAT_INSERT:
1808 case LFUN_FLOAT_LIST_INSERT:
1809 case LFUN_FOOTNOTE_INSERT:
1810 case LFUN_HREF_INSERT:
1811 case LFUN_INDEX_INSERT:
1812 case LFUN_INDEX_PRINT:
1813 case LFUN_INFO_INSERT:
1814 case LFUN_IPA_INSERT:
1815 case LFUN_LISTING_INSERT:
1816 case LFUN_MARGINALNOTE_INSERT:
1817 case LFUN_NEWPAGE_INSERT:
1818 case LFUN_NOMENCL_INSERT:
1819 case LFUN_NOMENCL_PRINT:
1820 case LFUN_NOTE_INSERT:
1821 case LFUN_PREVIEW_INSERT:
1822 case LFUN_TABULAR_INSERT:
1823 case LFUN_WRAP_INSERT:
1824 flag.setEnabled(false);
1827 case LFUN_SPACE_INSERT: {
1828 docstring const & name = cmd.argument();
1829 if (name == "visible")
1830 flag.setEnabled(false);
1834 case LFUN_INSET_DISSOLVE:
1835 flag.setEnabled(cmd.argument().empty() && !asHullInset() && !asMacroTemplate());
1839 docstring const & name = cmd.argument();
1840 if (name == "html" || name == "latex")
1841 flag.setEnabled(false);
1845 case LFUN_MATH_LIMITS: {
1847 if (cur.pos() < cur.lastpos() && cur.nextMath().allowsLimitsChange())
1848 in = &cur.nextMath();
1849 else if (cur.pos() > 0 && cur.prevMath().allowsLimitsChange())
1850 in = &cur.prevMath();
1851 else if (cur.lastpos() > 0 && cur.cell().back()->allowsLimitsChange())
1852 in = cur.cell().back().nucleus();
1854 if (!cmd.argument().empty()) {
1855 if (cmd.argument() == "limits")
1856 flag.setOnOff(in->limits() == LIMITS);
1857 else if (cmd.argument() == "nolimits")
1858 flag.setOnOff(in->limits() == NO_LIMITS);
1860 flag.setOnOff(in->limits() == AUTO_LIMITS);
1862 flag.setEnabled(true);
1864 flag.setEnabled(false);
1876 void InsetMathNest::edit(Cursor & cur, bool front, EntryDirection entry_from)
1879 bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_LEFT ||
1880 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1881 enter_front ? idxFirst(cur) : idxLast(cur);
1883 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1887 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1890 int dist_min = 1000000;
1891 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1892 int const d = cell(i).dist(cur.bv(), x, y);
1901 MathData & ar = cell(idx_min);
1903 cur.idx() = idx_min;
1904 cur.pos() = ar.x2pos(&cur.bv(), x - ar.xo(cur.bv()));
1906 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1907 if (dist_min == 0) {
1909 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1910 if (ar[i]->covers(cur.bv(), x, y))
1911 return ar[i].nucleus()->editXY(cur, x, y);
1917 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1919 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1920 BufferView & bv = cur.bv();
1921 if (cmd.button() == mouse_button::button3) {
1922 // Don't do anything if we right-click a
1923 // selection, a context menu will popup.
1924 if (bv.cursor().selection() && cur >= bv.cursor().selectionBegin()
1925 && cur < bv.cursor().selectionEnd()) {
1926 cur.noScreenUpdate();
1931 // set cursor after the inset if x is nearer to that position (bug 9748)
1932 cur.moveToClosestEdge(cmd.x(), true);
1934 bool do_selection = cmd.button() == mouse_button::button1
1935 && cmd.modifier() == ShiftModifier;
1936 bv.mouseSetCursor(cur, do_selection);
1937 if (cmd.button() == mouse_button::button1) {
1938 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1939 // Update the cursor update flags as needed:
1941 // Update::Decoration: tells to update the decoration
1942 // (visual box corners that define
1944 // Update::FitCursor: adjust the screen to the cursor
1945 // position if needed
1946 // cur.result().update(): don't overwrite previously set flags.
1947 cur.screenUpdateFlags(Update::Decoration | Update::FitCursor
1948 | cur.result().screenUpdate());
1949 } else if (cmd.button() == mouse_button::button2 && lyxrc.mouse_middlebutton_paste) {
1950 if (cap::selection()) {
1951 // See comment in Text::dispatch why we do this
1952 cap::copySelectionToStack();
1953 cmd = FuncRequest(LFUN_PASTE, "0");
1954 doDispatch(bv.cursor(), cmd);
1956 MathData ar(buffer_);
1957 asArray(theSelection().get(), ar);
1958 bv.cursor().insert(ar);
1964 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1966 // only select with button 1
1967 if (cmd.button() != mouse_button::button1)
1970 Cursor & bvcur = cur.bv().cursor();
1972 // ignore motions deeper nested than the real anchor
1973 if (!bvcur.realAnchor().hasPart(cur)) {
1978 // set cursor after the inset if x is nearer to that position (bug 9748)
1979 cur.moveToClosestEdge(cmd.x());
1981 CursorSlice old = bvcur.top();
1983 // We continue with our existing selection or start a new one, so don't
1984 // reset the anchor.
1985 bvcur.setCursor(cur);
1986 // Did we actually move?
1987 if (cur.top() == old)
1988 // We didn't move one iota, so no need to change selection status
1989 // or update the screen.
1990 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1992 bvcur.setSelection();
1996 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1998 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
2000 if (cmd.button() == mouse_button::button1) {
2001 if (!cur.selection())
2002 cur.noScreenUpdate();
2004 Cursor & bvcur = cur.bv().cursor();
2005 bvcur.selection(true);
2014 bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
2016 // try auto-correction
2017 if (lyxrc.autocorrection_math && cur.pos() != 0 && !cur.selection()
2018 && math_autocorrect(cur, c))
2021 //lyxerr << "interpret 2: '" << c << "'" << endl;
2022 docstring save_selection;
2023 if (c == '^' || c == '_')
2024 save_selection = grabAndEraseSelection(cur);
2027 Buffer * buf = cur.buffer();
2030 if (cur.inMacroMode()) {
2031 docstring name = cur.macroName();
2033 /// are we currently typing '#1' or '#2' or...?
2034 if (name == "\\#") {
2037 if (n >= 1 && n <= 9)
2038 cur.insert(new InsetMathMacroArgument(buffer_, n));
2042 // do not finish macro for known * commands
2043 bool star_macro = c == '*'
2044 && (in_word_set(name.substr(1) + '*')
2045 || cur.buffer()->getMacro(name.substr(1) + "*", cur, true));
2046 if (isAlphaASCII(c) || star_macro) {
2047 cur.activeMacro()->setName(name + docstring(1, c));
2051 // handle 'special char' macros
2056 if (currentMode() != InsetMath::MATH_MODE)
2057 cur.niceInsert(createInsetMath("textbackslash", buf));
2059 cur.niceInsert(createInsetMath("backslash", buf));
2060 } else if (c == '^' && currentMode() == InsetMath::MATH_MODE) {
2062 cur.niceInsert(createInsetMath("mathcircumflex", buf));
2063 } else if (c == '{' || c == '%') {
2064 //using the saved selection as argument
2065 InsetMathUnknown * p = cur.activeMacro();
2067 MathData sel(cur.buffer());
2068 asArray(p->selection(), sel);
2071 cur.niceInsert(MathAtom(new InsetMathBrace(buffer_, sel)));
2073 cur.niceInsert(MathAtom(new InsetMathComment(sel)));
2074 } else if (c == '#') {
2075 LASSERT(cur.activeMacro(), return false);
2076 cur.activeMacro()->setName(name + docstring(1, c));
2079 cur.niceInsert(createInsetMath(docstring(1, c), buf));
2084 // One character big delimiters. The others are handled in
2085 // interpretString().
2086 latexkeys const * l = in_word_set(name.substr(1));
2087 if (name[0] == '\\' && l && l->inset == "big") {
2091 delim = from_ascii("\\{");
2094 delim = from_ascii("\\}");
2097 delim = docstring(1, c);
2100 if (InsetMathBig::isBigInsetDelim(delim)) {
2101 // name + delim ared a valid InsetMathBig.
2102 // We can't use cur.macroModeClose() because
2103 // it does not handle delim.
2104 InsetMathUnknown * p = cur.activeMacro();
2107 cur.cell().erase(cur.pos());
2108 cur.plainInsert(MathAtom(
2109 new InsetMathBig(buffer_, name.substr(1), delim)));
2112 } else if (name == "\\smash" && c == '[') {
2113 // We can't use cur.macroModeClose() because
2114 // it would create an InsetMathPhantom
2115 InsetMathUnknown * p = cur.activeMacro();
2117 interpretChar(cur, c);
2121 // leave macro mode and try again if necessary
2122 if (cur.macroModeClose()) {
2123 MathAtom const atom = cur.prevAtom();
2124 if (atom->asNestInset() && atom->isActive()) {
2126 cur.nextInset()->edit(cur, true);
2130 cur.niceInsert(MathAtom(new InsetMathBrace(buf)));
2132 interpretChar(cur, c);
2137 // just clear selection on pressing the space bar
2138 if (cur.selection() && c == ' ') {
2139 cur.selection(false);
2144 //lyxerr << "starting with macro" << endl;
2145 bool reduced = cap::reduceSelectionToOneCell(cur);
2146 if (reduced || !cur.selection()) {
2147 InsetMath const * im = cur.inset().asInsetMath();
2148 InsetMathMacro const * macro = im ? im->asMacro()
2150 bool in_macro_name = macro
2151 && macro->displayMode() ==
2152 InsetMathMacro::DISPLAY_UNFOLDED;
2153 cur.recordUndoInset();
2154 docstring const safe = cap::grabAndEraseSelection(cur);
2155 if (!cur.inRegexped() && !in_macro_name)
2156 cur.insert(MathAtom(new InsetMathUnknown(buffer_, from_ascii("\\"), safe, false)));
2158 cur.niceInsert(createInsetMath("backslash", buf));
2166 if (currentMode() != InsetMath::MATH_MODE)
2172 if (currentMode() != InsetMath::MATH_MODE) {
2173 // insert spaces in text or undecided mode,
2174 // but suppress direct insertion of two spaces in a row
2175 // the still allows typing '<space>a<space>' and deleting the 'a', but
2176 // it is better than nothing...
2177 pos_type const pos = cur.pos();
2178 pos_type const lastpos = cur.lastpos();
2179 if ((pos == 0 && lastpos == 0)
2180 || (pos == 0 && cur.nextAtom()->getChar() != ' ')
2181 || (pos == lastpos && cur.prevAtom()->getChar() != ' ')
2182 || (pos > 0 && cur.prevAtom()->getChar() != ' '
2183 && cur.nextAtom()->getChar() != ' ')) {
2185 // FIXME: we have to enable full redraw here because of the
2186 // visual box corners that define the inset. If we know for
2187 // sure that we stay within the same cell we can optimize for
2189 //cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2193 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
2194 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
2195 // FIXME: we have to enable full redraw here because of the
2196 // visual box corners that define the inset. If we know for
2197 // sure that we stay within the same cell we can optimize for
2199 //cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2203 if (cur.popForward()) {
2204 // FIXME: we have to enable full redraw here because of the
2205 // visual box corners that define the inset. If we know for
2206 // sure that we stay within the same cell we can optimize for
2208 //cur.screenUpdateFlags(Update::FitCursor);
2212 // if we are at the very end, leave the formula
2213 return cur.pos() != cur.lastpos();
2216 // These should be treated differently when not in text mode:
2217 if (cur.inRegexped()) {
2220 cur.niceInsert(createInsetMath("mathcircumflex", buf));
2227 cur.niceInsert(createInsetMath(docstring(1, c), buf));
2230 cur.niceInsert(createInsetMath("sim", buf));
2236 } else if (currentMode() != InsetMath::TEXT_MODE) {
2238 cur.recordUndoInset();
2239 script(cur, false, save_selection);
2243 cur.recordUndoInset();
2244 script(cur, true, save_selection);
2248 cur.niceInsert(createInsetMath("sim", buf));
2251 if (currentMode() == InsetMath::MATH_MODE && Encodings::isUnicodeTextOnly(c)) {
2252 MathAtom const atom(new InsetMathChar(buffer_, c));
2253 if (cur.prevInset() && cur.prevInset()->asInsetMath()->name() == "text") {
2254 // reuse existing \text inset
2255 cur.prevInset()->asInsetMath()->cell(0).push_back(atom);
2257 MathAtom at = createInsetMath("text", buf);
2258 at.nucleus()->cell(0).push_back(atom);
2266 cur.niceInsert(createInsetMath("textasciicircum", buf));
2270 cur.niceInsert(createInsetMath("textasciitilde", buf));
2275 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
2276 c == '%' || c == '_') {
2277 cur.niceInsert(createInsetMath(docstring(1, c), buf));
2281 // no special circumstances, so insert the character without any fuss
2287 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
2289 if (str == "\\limits" || str == "\\nolimits") {
2290 if (cur.pos() > 0 && cur.prevMath().allowsLimitsChange()) {
2291 cur.prevMath().limits(str == "\\limits" ? LIMITS : NO_LIMITS);
2294 cur.message(bformat(_("Cannot apply %1$s here."), str));
2298 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
2300 if (!cur.empty() && cur.pos() > 0 &&
2301 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
2302 if (InsetMathBig::isBigInsetDelim(str)) {
2303 docstring prev = asString(cur.cell()[cur.pos() - 1]);
2304 if (prev[0] == '\\') {
2305 prev = prev.substr(1);
2306 latexkeys const * l = in_word_set(prev);
2307 if (l && l->inset == "big") {
2308 cur.recordUndoSelection();
2309 cur.cell()[cur.pos() - 1] =
2310 MathAtom(new InsetMathBig(buffer_, prev, str));
2320 bool InsetMathNest::script(Cursor & cur, bool up)
2322 return script(cur, up, docstring());
2326 bool InsetMathNest::script(Cursor & cur, bool up,
2327 docstring const & save_selection)
2329 // Hack to get \^ and \_ working
2330 //lyxerr << "handling script: up: " << up << endl;
2331 if (cur.inMacroMode() && cur.macroName() == "\\") {
2333 cur.niceInsert(createInsetMath("mathcircumflex", cur.buffer()));
2335 interpretChar(cur, '_');
2339 cur.macroModeClose();
2340 if (asScriptInset() && cur.idx() == 0) {
2341 // we are in a nucleus of a script inset, move to _our_ script
2342 InsetMathScript * inset = asScriptInset();
2343 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
2345 cur.idx() = inset->idxOfScript(up);
2347 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
2349 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
2352 cur.idx() = inset->idxOfScript(up);
2353 cur.pos() = cur.lastpos();
2355 // convert the thing to our left to a scriptinset or create a new
2356 // one if in the very first position of the array
2357 if (cur.pos() == 0) {
2358 //lyxerr << "new scriptinset" << endl;
2359 cur.insert(new InsetMathScript(buffer_, up));
2361 //lyxerr << "converting prev atom " << endl;
2362 cur.prevAtom() = MathAtom(new InsetMathScript(buffer_, cur.prevAtom(), up));
2365 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
2366 // See comment in MathParser.cpp for special handling of {}-bases
2372 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
2373 cur.niceInsert(save_selection);
2375 //lyxerr << "inserting selection 2:\n" << save_selection << endl;
2380 bool InsetMathNest::completionSupported(Cursor const & cur) const
2382 return cur.inMacroMode();
2386 bool InsetMathNest::inlineCompletionSupported(Cursor const & cur) const
2388 return cur.inMacroMode();
2392 bool InsetMathNest::automaticInlineCompletion() const
2394 return lyxrc.completion_inline_math;
2398 bool InsetMathNest::automaticPopupCompletion() const
2400 return lyxrc.completion_popup_math;
2404 CompletionList const *
2405 InsetMathNest::createCompletionList(Cursor const & cur) const
2407 if (!cur.inMacroMode())
2410 return new MathCompletionList(cur);
2414 docstring InsetMathNest::completionPrefix(Cursor const & cur) const
2416 if (!cur.inMacroMode())
2419 return cur.activeMacro()->name();
2423 bool InsetMathNest::insertCompletion(Cursor & cur, docstring const & s, bool finished)
2425 if (cur.buffer()->isReadonly() || !cur.inMacroMode())
2428 // Contrary to Text, the whole inset should be recorded (#12581).
2429 cur.recordUndoInset();
2431 // append completion to active macro
2432 InsetMathUnknown * inset = cur.activeMacro();
2433 inset->setName(inset->name() + s);
2438 // FIXME: this creates duplicates in the completion popup
2439 // which looks ugly. Moreover the changes the list lengths
2440 // which seems to confuse the popup as well.
2441 MathCompletionList::addToFavorites(inset->name());
2443 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, " "));
2445 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2451 void InsetMathNest::completionPosAndDim(Cursor const & cur, int & x, int & y,
2452 Dimension & dim) const
2454 Inset const * inset = cur.activeMacro();
2458 // get inset dimensions
2459 dim = cur.bv().coordCache().insets().dim(inset);
2460 // FIXME: these 3 are no accurate, but should depend on the font.
2461 // Now the popup jumps down if you enter a char with descent > 0.
2466 Point xy = cur.bv().coordCache().insets().xy(inset);
2472 ////////////////////////////////////////////////////////////////////
2474 MathCompletionList::MathCompletionList(Cursor const & cur)
2476 // fill it with macros from the buffer
2477 MacroNameSet macros;
2478 cur.buffer()->listMacroNames(macros);
2479 MacroNameSet::const_iterator it;
2480 for (it = macros.begin(); it != macros.end(); ++it) {
2481 if (cur.buffer()->getMacro(*it, cur, false))
2482 locals.push_back("\\" + *it);
2484 sort(locals.begin(), locals.end());
2486 if (!globals.empty())
2489 // fill in global macros
2491 MacroTable::globalMacros().getMacroNames(macros, false);
2492 //lyxerr << "Globals completion macros: ";
2493 for (it = macros.begin(); it != macros.end(); ++it) {
2494 //lyxerr << "\\" + *it << " ";
2495 globals.push_back("\\" + *it);
2497 //lyxerr << std::endl;
2499 // fill in global commands
2500 globals.push_back(from_ascii("\\boxed"));
2501 globals.push_back(from_ascii("\\fbox"));
2502 globals.push_back(from_ascii("\\framebox"));
2503 globals.push_back(from_ascii("\\makebox"));
2504 globals.push_back(from_ascii("\\kern"));
2505 globals.push_back(from_ascii("\\xhookrightarrow"));
2506 globals.push_back(from_ascii("\\xhookleftarrow"));
2507 globals.push_back(from_ascii("\\xrightarrow"));
2508 globals.push_back(from_ascii("\\xRightarrow"));
2509 globals.push_back(from_ascii("\\xrightharpoondown"));
2510 globals.push_back(from_ascii("\\xrightharpoonup"));
2511 globals.push_back(from_ascii("\\xrightleftharpoons"));
2512 globals.push_back(from_ascii("\\xleftarrow"));
2513 globals.push_back(from_ascii("\\xLeftarrow"));
2514 globals.push_back(from_ascii("\\xleftharpoondown"));
2515 globals.push_back(from_ascii("\\xleftharpoonup"));
2516 globals.push_back(from_ascii("\\xleftrightarrow"));
2517 globals.push_back(from_ascii("\\xLeftrightarrow"));
2518 globals.push_back(from_ascii("\\xleftrightharpoons"));
2519 globals.push_back(from_ascii("\\xmapsto"));
2520 globals.push_back(from_ascii("\\split"));
2521 globals.push_back(from_ascii("\\gathered"));
2522 globals.push_back(from_ascii("\\aligned"));
2523 globals.push_back(from_ascii("\\alignedat"));
2524 globals.push_back(from_ascii("\\cases"));
2525 globals.push_back(from_ascii("\\substack"));
2526 globals.push_back(from_ascii("\\xymatrix"));
2527 globals.push_back(from_ascii("\\Diagram"));
2528 globals.push_back(from_ascii("\\subarray"));
2529 globals.push_back(from_ascii("\\array"));
2530 globals.push_back(from_ascii("\\sqrt"));
2531 globals.push_back(from_ascii("\\root"));
2532 globals.push_back(from_ascii("\\tabular"));
2533 globals.push_back(from_ascii("\\sideset"));
2534 globals.push_back(from_ascii("\\stackrel"));
2535 globals.push_back(from_ascii("\\stackrelthree"));
2536 globals.push_back(from_ascii("\\binom"));
2537 globals.push_back(from_ascii("\\choose"));
2538 globals.push_back(from_ascii("\\brace"));
2539 globals.push_back(from_ascii("\\brack"));
2540 globals.push_back(from_ascii("\\frac"));
2541 globals.push_back(from_ascii("\\over"));
2542 globals.push_back(from_ascii("\\nicefrac"));
2543 globals.push_back(from_ascii("\\unitfrac"));
2544 globals.push_back(from_ascii("\\unitfracthree"));
2545 globals.push_back(from_ascii("\\unitone"));
2546 globals.push_back(from_ascii("\\unittwo"));
2547 globals.push_back(from_ascii("\\infer"));
2548 globals.push_back(from_ascii("\\atop"));
2549 globals.push_back(from_ascii("\\lefteqn"));
2550 globals.push_back(from_ascii("\\boldsymbol"));
2551 globals.push_back(from_ascii("\\bm"));
2552 globals.push_back(from_ascii("\\color"));
2553 globals.push_back(from_ascii("\\normalcolor"));
2554 globals.push_back(from_ascii("\\textcolor"));
2555 globals.push_back(from_ascii("\\cfrac"));
2556 globals.push_back(from_ascii("\\cfracleft"));
2557 globals.push_back(from_ascii("\\cfracright"));
2558 globals.push_back(from_ascii("\\dfrac"));
2559 globals.push_back(from_ascii("\\tfrac"));
2560 globals.push_back(from_ascii("\\dbinom"));
2561 globals.push_back(from_ascii("\\tbinom"));
2562 globals.push_back(from_ascii("\\hphantom"));
2563 globals.push_back(from_ascii("\\phantom"));
2564 globals.push_back(from_ascii("\\vphantom"));
2565 globals.push_back(from_ascii("\\cancel"));
2566 globals.push_back(from_ascii("\\bcancel"));
2567 globals.push_back(from_ascii("\\xcancel"));
2568 globals.push_back(from_ascii("\\cancelto"));
2569 globals.push_back(from_ascii("\\smash"));
2570 globals.push_back(from_ascii("\\mathclap"));
2571 globals.push_back(from_ascii("\\mathllap"));
2572 globals.push_back(from_ascii("\\mathrlap"));
2573 globals.push_back(from_ascii("\\ensuremath"));
2574 MathWordList const & words = mathedWordList();
2575 MathWordList::const_iterator it2;
2576 //lyxerr << "Globals completion commands: ";
2577 for (it2 = words.begin(); it2 != words.end(); ++it2) {
2578 if (it2->second.inset != "macro" && !it2->second.hidden) {
2579 // macros are already read from MacroTable::globalMacros()
2580 globals.push_back('\\' + it2->first);
2581 //lyxerr << '\\' + it2->first << ' ';
2584 //lyxerr << std::endl;
2585 sort(globals.begin(), globals.end());
2589 MathCompletionList::~MathCompletionList()
2594 size_type MathCompletionList::size() const
2596 return locals.size() + globals.size();
2600 docstring const & MathCompletionList::data(size_t idx) const
2602 size_t lsize = locals.size();
2604 return globals[idx - lsize];
2610 std::string MathCompletionList::icon(size_t idx) const
2612 // get the latex command
2614 size_t lsize = locals.size();
2616 cmd = globals[idx - lsize];
2620 // get the icon name by stripping the backslash
2621 docstring icon_name = frontend::Application::mathIcon(cmd.substr(1));
2622 if (icon_name.empty())
2623 return std::string();
2624 return "math/" + to_utf8(icon_name);
2627 std::vector<docstring> MathCompletionList::globals;