2 * \file InsetMathNest.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetMathNest.h"
15 #include "InsetMathArray.h"
16 #include "InsetMathBig.h"
17 #include "InsetMathBox.h"
18 #include "InsetMathBrace.h"
19 #include "InsetMathColor.h"
20 #include "InsetMathComment.h"
21 #include "InsetMathDelim.h"
22 #include "InsetMathHull.h"
23 #include "InsetMathRef.h"
24 #include "InsetMathScript.h"
25 #include "InsetMathSpace.h"
26 #include "InsetMathSymbol.h"
27 #include "InsetMathUnknown.h"
28 #include "MathCompletionList.h"
30 #include "MathFactory.h"
31 #include "MathMacro.h"
32 #include "MathMacroArgument.h"
33 #include "MathParser.h"
34 #include "MathStream.h"
35 #include "MathSupport.h"
39 #include "BufferView.h"
40 #include "CoordCache.h"
42 #include "CutAndPaste.h"
43 #include "DispatchResult.h"
44 #include "FuncRequest.h"
45 #include "FuncStatus.h"
48 #include "OutputParams.h"
51 #include "frontends/Clipboard.h"
52 #include "frontends/Painter.h"
53 #include "frontends/Selection.h"
55 #include "support/lassert.h"
56 #include "support/debug.h"
57 #include "support/gettext.h"
58 #include "support/lstrings.h"
59 #include "support/textutils.h"
60 #include "support/docstream.h"
66 using namespace lyx::support;
70 using cap::copySelection;
71 using cap::grabAndEraseSelection;
72 using cap::cutSelection;
73 using cap::replaceSelection;
74 using cap::selClearOrDel;
77 InsetMathNest::InsetMathNest(idx_type nargs)
78 : cells_(nargs), lock_(false), mouse_hover_(false)
82 InsetMathNest::InsetMathNest(InsetMathNest const & inset)
83 : InsetMath(inset), cells_(inset.cells_), lock_(inset.lock_),
88 InsetMathNest & InsetMathNest::operator=(InsetMathNest const & inset)
90 cells_ = inset.cells_;
93 InsetMath::operator=(inset);
98 InsetMath::idx_type InsetMathNest::nargs() const
100 return cells_.size();
104 void InsetMathNest::cursorPos(BufferView const & bv,
105 CursorSlice const & sl, bool /*boundary*/,
106 int & x, int & y) const
108 // FIXME: This is a hack. Ideally, the coord cache should not store
109 // absolute positions, but relative ones. This would mean to call
110 // setXY() not in MathData::draw(), but in the parent insets' draw()
111 // with the correctly adjusted x,y values. But this means that we'd have
112 // to touch all (math)inset's draw() methods. Right now, we'll store
113 // absolute value, and make them here relative, only to make them
114 // absolute again when actually drawing the cursor. What a mess.
115 LASSERT(&sl.inset() == this, /**/);
116 MathData const & ar = sl.cell();
117 CoordCache const & coord_cache = bv.coordCache();
118 if (!coord_cache.getArrays().has(&ar)) {
119 // this can (semi-)legally happen if we just created this cell
120 // and it never has been drawn before. So don't ASSERT.
121 //lyxerr << "no cached data for array " << &ar << endl;
126 Point const pt = coord_cache.getArrays().xy(&ar);
127 if (!coord_cache.getInsets().has(this)) {
129 //lyxerr << "no cached data for inset " << this << endl;
134 Point const pt2 = coord_cache.getInsets().xy(this);
135 //lyxerr << "retrieving position cache for MathData "
136 // << pt.x_ << ' ' << pt.y_ << endl;
137 x = pt.x_ - pt2.x_ + ar.pos2x(&bv, sl.pos());
139 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
140 // << " asc: " << ascent() << " des: " << descent()
141 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
142 // move cursor visually into empty cells ("blue rectangles");
148 void InsetMathNest::metrics(MetricsInfo const & mi) const
151 for (idx_type i = 0, n = nargs(); i != n; ++i) {
153 cell(i).metrics(m, dim);
158 bool InsetMathNest::idxNext(Cursor & cur) const
160 LASSERT(&cur.inset() == this, /**/);
161 if (cur.idx() == cur.lastidx())
169 bool InsetMathNest::idxForward(Cursor & cur) const
175 bool InsetMathNest::idxPrev(Cursor & cur) const
177 LASSERT(&cur.inset() == this, /**/);
181 cur.pos() = cur.lastpos();
186 bool InsetMathNest::idxBackward(Cursor & cur) const
192 bool InsetMathNest::idxFirst(Cursor & cur) const
194 LASSERT(&cur.inset() == this, /**/);
203 bool InsetMathNest::idxLast(Cursor & cur) const
205 LASSERT(&cur.inset() == this, /**/);
208 cur.idx() = cur.lastidx();
209 cur.pos() = cur.lastpos();
214 void InsetMathNest::dump() const
216 odocstringstream oss;
218 os << "---------------------------------------------\n";
221 for (idx_type i = 0, n = nargs(); i != n; ++i)
222 os << cell(i) << "\n";
223 os << "---------------------------------------------\n";
224 lyxerr << to_utf8(oss.str());
228 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
232 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
235 setPosCache(pi, x, y);
239 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
241 BufferView & bv = *pi.base.bv;
242 // this should use the x/y values given, not the cached values
243 Cursor & cur = bv.cursor();
244 if (!cur.selection())
246 if (&cur.inset() != this)
249 // FIXME: hack to get position cache warm
250 bool const original_drawing_state = pi.pain.isDrawingEnabled();
251 pi.pain.setDrawingEnabled(false);
253 pi.pain.setDrawingEnabled(original_drawing_state);
255 CursorSlice s1 = cur.selBegin();
256 CursorSlice s2 = cur.selEnd();
258 //lyxerr << "InsetMathNest::drawing selection: "
259 // << " s1: " << s1 << " s2: " << s2 << endl;
260 if (s1.idx() == s2.idx()) {
261 MathData const & c = cell(s1.idx());
262 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
263 int x1 = g.pos.x_ + c.pos2x(pi.base.bv, s1.pos());
264 int y1 = g.pos.y_ - g.dim.ascent();
265 int x2 = g.pos.x_ + c.pos2x(pi.base.bv, s2.pos());
266 int y2 = g.pos.y_ + g.dim.descent();
267 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
268 //lyxerr << "InsetMathNest::drawing selection 3: "
269 // << " x1: " << x1 << " x2: " << x2
270 // << " y1: " << y1 << " y2: " << y2 << endl;
272 for (idx_type i = 0; i < nargs(); ++i) {
273 if (idxBetween(i, s1.idx(), s2.idx())) {
274 MathData const & c = cell(i);
275 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
277 int y1 = g.pos.y_ - g.dim.ascent();
278 int x2 = g.pos.x_ + g.dim.width();
279 int y2 = g.pos.y_ + g.dim.descent();
280 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
287 void InsetMathNest::validate(LaTeXFeatures & features) const
289 for (idx_type i = 0; i < nargs(); ++i)
290 cell(i).validate(features);
294 void InsetMathNest::replace(ReplaceData & rep)
296 for (idx_type i = 0; i < nargs(); ++i)
297 cell(i).replace(rep);
301 bool InsetMathNest::contains(MathData const & ar) const
303 for (idx_type i = 0; i < nargs(); ++i)
304 if (cell(i).contains(ar))
310 bool InsetMathNest::lock() const
316 void InsetMathNest::lock(bool l)
322 bool InsetMathNest::isActive() const
328 MathData InsetMathNest::glue() const
331 for (size_t i = 0; i < nargs(); ++i)
337 void InsetMathNest::write(WriteStream & os) const
339 ModeSpecifier specifier(os, currentMode());
340 docstring const latex_name = name();
341 os << '\\' << latex_name;
342 for (size_t i = 0; i < nargs(); ++i)
343 os << '{' << cell(i) << '}';
345 os.pendingSpace(true);
346 if (lock_ && !os.latex()) {
348 os.pendingSpace(true);
353 void InsetMathNest::normalize(NormalStream & os) const
356 for (size_t i = 0; i < nargs(); ++i)
357 os << ' ' << cell(i);
362 int InsetMathNest::latex(odocstream & os, OutputParams const & runparams) const
364 WriteStream wi(os, runparams.moving_arg, true, runparams.dryrun,
371 bool InsetMathNest::setMouseHover(bool mouse_hover)
373 mouse_hover_ = mouse_hover;
378 bool InsetMathNest::notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
382 MathData & ar = cur.cell();
383 // remove base-only "scripts"
384 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
385 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
386 if (p && p->nargs() == 1) {
387 MathData ar = p->nuc();
390 cur.adjust(i, ar.size() - 1);
394 // glue adjacent font insets of the same kind
395 for (pos_type i = 0; i + 1 < size(); ++i) {
396 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
397 InsetMathFont const * q = operator[](i + 1)->asFontInset();
398 if (p && q && p->name() == q->name()) {
399 p->cell(0).append(q->cell(0));
409 void InsetMathNest::handleFont
410 (Cursor & cur, docstring const & arg, char const * const font)
412 handleFont(cur, arg, from_ascii(font));
416 void InsetMathNest::handleFont(Cursor & cur, docstring const & arg,
417 docstring const & font)
419 cur.recordUndoSelection();
421 // this whole function is a hack and won't work for incremental font
423 if (cur.inset().asInsetMath()->name() == font)
424 cur.handleFont(to_utf8(font));
426 handleNest(cur, createInsetMath(font), arg);
430 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest)
432 handleNest(cur, nest, docstring());
436 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest,
437 docstring const & arg)
439 CursorSlice i1 = cur.selBegin();
440 CursorSlice i2 = cur.selEnd();
441 if (!i1.inset().asInsetMath())
443 if (i1.idx() == i2.idx()) {
444 // the easy case where only one cell is selected
445 cur.handleNest(nest);
450 // multiple selected cells in a simple non-grid inset
451 if (i1.asInsetMath()->nrows() == 0 || i1.asInsetMath()->ncols() == 0) {
452 for (idx_type i = i1.idx(); i <= i2.idx(); ++i) {
457 cur.pos() = cur.lastpos();
460 // change font of cell
461 cur.handleNest(nest);
464 // cur is in the font inset now. If the loop continues,
465 // we need to get outside again for the next cell
466 if (i + 1 <= i2.idx())
472 // the complicated case with multiple selected cells in a grid
475 cap::region(i1, i2, r1, r2, c1, c2);
476 for (row_type row = r1; row <= r2; ++row) {
477 for (col_type col = c1; col <= c2; ++col) {
479 cur.idx() = i1.asInsetMath()->index(row, col);
482 cur.pos() = cur.lastpos();
486 cur.handleNest(nest);
489 // cur is in the font inset now. If the loop continues,
490 // we need to get outside again for the next cell
491 if (col + 1 <= c2 || row + 1 <= r2)
498 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
500 cur.recordUndoSelection();
503 font.fromString(to_utf8(arg), b);
504 if (font.fontInfo().color() != Color_inherit &&
505 font.fontInfo().color() != Color_ignore)
506 handleNest(cur, MathAtom(new InsetMathColor(true, font.fontInfo().color())));
508 // FIXME: support other font changes here as well?
512 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
514 //lyxerr << "InsetMathNest: request: " << cmd << endl;
516 switch (cmd.action) {
519 cur.recordUndoSelection();
520 cur.message(_("Paste"));
521 replaceSelection(cur);
523 if (cmd.argument().empty() && !theClipboard().isInternal())
524 topaste = theClipboard().getAsText();
527 idocstringstream is(cmd.argument());
529 topaste = cap::selection(n);
531 cur.niceInsert(topaste);
532 cur.clearSelection(); // bug 393
539 cutSelection(cur, true, true);
540 cur.message(_("Cut"));
541 // Prevent stale position >= size crash
542 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
548 cur.message(_("Copy"));
551 case LFUN_MOUSE_PRESS:
552 lfunMousePress(cur, cmd);
555 case LFUN_MOUSE_MOTION:
556 lfunMouseMotion(cur, cmd);
559 case LFUN_MOUSE_RELEASE:
560 lfunMouseRelease(cur, cmd);
563 case LFUN_FINISHED_LEFT: // in math, left is backwards
564 case LFUN_FINISHED_BACKWARD:
565 cur.bv().cursor() = cur;
568 case LFUN_FINISHED_RIGHT: // in math, right is forward
569 case LFUN_FINISHED_FORWARD:
571 cur.bv().cursor() = cur;
574 case LFUN_CHAR_RIGHT:
576 case LFUN_CHAR_BACKWARD:
577 case LFUN_CHAR_FORWARD:
578 cur.updateFlags(Update::Decoration | Update::FitCursor);
579 case LFUN_CHAR_RIGHT_SELECT:
580 case LFUN_CHAR_LEFT_SELECT:
581 case LFUN_CHAR_BACKWARD_SELECT:
582 case LFUN_CHAR_FORWARD_SELECT: {
583 // are we in a selection?
584 bool select = (cmd.action == LFUN_CHAR_RIGHT_SELECT
585 || cmd.action == LFUN_CHAR_LEFT_SELECT
586 || cmd.action == LFUN_CHAR_BACKWARD_SELECT
587 || cmd.action == LFUN_CHAR_FORWARD_SELECT);
588 // are we moving forward or backwards?
589 // If the command was RIGHT or LEFT, then whether we're moving forward
590 // or backwards depends on the cursor movement mode (logical or visual):
591 // * in visual mode, since math is always LTR, right -> forward,
593 // * in logical mode, the mapping is determined by the
594 // reverseDirectionNeeded() function
597 FuncCode finish_lfun;
599 if (cmd.action == LFUN_CHAR_FORWARD
600 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
602 finish_lfun = LFUN_FINISHED_FORWARD;
604 else if (cmd.action == LFUN_CHAR_BACKWARD
605 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
607 finish_lfun = LFUN_FINISHED_BACKWARD;
610 bool right = (cmd.action == LFUN_CHAR_RIGHT_SELECT
611 || cmd.action == LFUN_CHAR_RIGHT);
612 if (lyxrc.visual_cursor || !reverseDirectionNeeded(cur))
618 finish_lfun = LFUN_FINISHED_RIGHT;
620 finish_lfun = LFUN_FINISHED_LEFT;
622 // Now that we know exactly what we want to do, let's do it!
623 cur.selHandle(select);
624 cur.autocorrect() = false;
626 cur.macroModeClose();
627 // try moving forward or backwards as necessary...
628 if (!(forward ? cursorMathForward(cur) : cursorMathBackward(cur))) {
629 // ... and if movement failed, then finish forward or backwards
631 cmd = FuncRequest(finish_lfun);
639 cur.updateFlags(Update::Decoration | Update::FitCursor);
640 case LFUN_DOWN_SELECT:
641 case LFUN_UP_SELECT: {
642 // close active macro
643 if (cur.inMacroMode()) {
644 cur.macroModeClose();
648 // stop/start the selection
649 bool select = cmd.action == LFUN_DOWN_SELECT ||
650 cmd.action == LFUN_UP_SELECT;
651 cur.selHandle(select);
654 bool up = cmd.action == LFUN_UP || cmd.action == LFUN_UP_SELECT;
655 bool successful = cur.upDownInMath(up);
659 if (cur.fixIfBroken())
660 // FIXME: Something bad happened. We pass the corrected Cursor
661 // instead of letting things go worse.
664 // We did not manage to move the cursor.
669 case LFUN_MOUSE_DOUBLE:
670 case LFUN_MOUSE_TRIPLE:
671 case LFUN_WORD_SELECT:
675 cur.setSelection(true);
676 cur.pos() = cur.lastpos();
677 cur.idx() = cur.lastidx();
680 case LFUN_PARAGRAPH_UP:
681 case LFUN_PARAGRAPH_DOWN:
682 cur.updateFlags(Update::Decoration | Update::FitCursor);
683 case LFUN_PARAGRAPH_UP_SELECT:
684 case LFUN_PARAGRAPH_DOWN_SELECT:
687 case LFUN_LINE_BEGIN:
688 case LFUN_WORD_BACKWARD:
690 cur.updateFlags(Update::Decoration | Update::FitCursor);
691 case LFUN_LINE_BEGIN_SELECT:
692 case LFUN_WORD_BACKWARD_SELECT:
693 case LFUN_WORD_LEFT_SELECT:
694 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
695 cmd.action == LFUN_WORD_LEFT_SELECT ||
696 cmd.action == LFUN_LINE_BEGIN_SELECT);
697 cur.macroModeClose();
698 if (cur.pos() != 0) {
700 } else if (cur.col() != 0) {
701 cur.idx() -= cur.col();
703 } else if (cur.idx() != 0) {
707 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
712 case LFUN_WORD_FORWARD:
713 case LFUN_WORD_RIGHT:
715 cur.updateFlags(Update::Decoration | Update::FitCursor);
716 case LFUN_WORD_FORWARD_SELECT:
717 case LFUN_WORD_RIGHT_SELECT:
718 case LFUN_LINE_END_SELECT:
719 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
720 cmd.action == LFUN_WORD_RIGHT_SELECT ||
721 cmd.action == LFUN_LINE_END_SELECT);
722 cur.macroModeClose();
724 if (cur.pos() != cur.lastpos()) {
725 cur.pos() = cur.lastpos();
726 } else if (ncols() && (cur.col() != cur.lastcol())) {
727 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
728 cur.pos() = cur.lastpos();
729 } else if (cur.idx() != cur.lastidx()) {
730 cur.idx() = cur.lastidx();
731 cur.pos() = cur.lastpos();
733 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
738 case LFUN_CELL_FORWARD:
739 cur.updateFlags(Update::Decoration | Update::FitCursor);
740 cur.inset().idxNext(cur);
743 case LFUN_CELL_BACKWARD:
744 cur.updateFlags(Update::Decoration | Update::FitCursor);
745 cur.inset().idxPrev(cur);
748 case LFUN_WORD_DELETE_BACKWARD:
749 case LFUN_CHAR_DELETE_BACKWARD:
751 // May affect external cell:
752 cur.recordUndoInset();
754 cur.recordUndoSelection();
755 // if the inset can not be removed from within, delete it
756 if (!cur.backspace()) {
757 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
758 cur.innerText()->dispatch(cur, cmd);
762 case LFUN_WORD_DELETE_FORWARD:
763 case LFUN_CHAR_DELETE_FORWARD:
764 if (cur.pos() == cur.lastpos())
765 // May affect external cell:
766 cur.recordUndoInset();
768 cur.recordUndoSelection();
769 // if the inset can not be removed from within, delete it
771 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
772 cur.innerText()->dispatch(cur, cmd);
778 cur.clearSelection();
780 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
785 // 'Locks' the math inset. A 'locked' math inset behaves as a unit
786 // that is traversed by a single <CursorLeft>/<CursorRight>.
787 case LFUN_INSET_TOGGLE:
793 case LFUN_SELF_INSERT:
794 if (cmd.argument().size() != 1) {
795 cur.recordUndoSelection();
796 docstring const arg = cmd.argument();
797 if (!interpretString(cur, arg))
801 // Don't record undo steps if we are in macro mode and
802 // cmd.argument is the next character of the macro name.
803 // Otherwise we'll get an invalid cursor if we undo after
804 // the macro was finished and the macro is a known command,
805 // e.g. sqrt. Cursor::macroModeClose replaces in this case
806 // the InsetMathUnknown with name "frac" by an empty
807 // InsetMathFrac -> a pos value > 0 is invalid.
808 // A side effect is that an undo before the macro is finished
809 // undoes the complete macro, not only the last character.
810 if (!cur.inMacroMode())
811 cur.recordUndoSelection();
813 // spacial handling of space. If we insert an inset
814 // via macro mode, we want to put the cursor inside it
815 // if relevant. Think typing "\frac<space>".
816 if (cmd.argument()[0] == ' '
817 && cur.inMacroMode() && cur.macroName() != "\\"
818 && cur.macroModeClose()) {
819 MathAtom const atom = cur.prevAtom();
820 if (atom->asNestInset() && atom->isActive()) {
822 cur.pushBackward(*cur.nextInset());
824 } else if (!interpretChar(cur, cmd.argument()[0])) {
825 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
830 //case LFUN_SERVER_GET_XY:
831 // sprintf(dispatch_buffer, "%d %d",);
834 case LFUN_SERVER_SET_XY: {
835 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
838 istringstream is(to_utf8(cmd.argument()));
840 cur.setScreenPos(x, y);
844 // Special casing for superscript in case of LyX handling
846 case LFUN_ACCENT_CIRCUMFLEX:
847 if (cmd.argument().empty()) {
848 // do superscript if LyX handles
850 cur.recordUndoSelection();
851 script(cur, true, grabAndEraseSelection(cur));
855 case LFUN_ACCENT_UMLAUT:
856 case LFUN_ACCENT_ACUTE:
857 case LFUN_ACCENT_GRAVE:
858 case LFUN_ACCENT_BREVE:
859 case LFUN_ACCENT_DOT:
860 case LFUN_ACCENT_MACRON:
861 case LFUN_ACCENT_CARON:
862 case LFUN_ACCENT_TILDE:
863 case LFUN_ACCENT_CEDILLA:
864 case LFUN_ACCENT_CIRCLE:
865 case LFUN_ACCENT_UNDERDOT:
866 case LFUN_ACCENT_TIE:
867 case LFUN_ACCENT_OGONEK:
868 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
872 case LFUN_TEXTSTYLE_APPLY:
873 case LFUN_TEXTSTYLE_UPDATE:
874 handleFont2(cur, cmd.argument());
878 if (currentMode() == TEXT_MODE)
879 handleFont(cur, cmd.argument(), "textbf");
881 handleFont(cur, cmd.argument(), "mathbf");
883 case LFUN_FONT_BOLDSYMBOL:
884 if (currentMode() == TEXT_MODE)
885 handleFont(cur, cmd.argument(), "textbf");
887 handleFont(cur, cmd.argument(), "boldsymbol");
890 if (currentMode() == TEXT_MODE)
891 handleFont(cur, cmd.argument(), "textsf");
893 handleFont(cur, cmd.argument(), "mathsf");
896 if (currentMode() == TEXT_MODE)
897 handleFont(cur, cmd.argument(), "emph");
899 handleFont(cur, cmd.argument(), "mathcal");
901 case LFUN_FONT_ROMAN:
902 if (currentMode() == TEXT_MODE)
903 handleFont(cur, cmd.argument(), "textrm");
905 handleFont(cur, cmd.argument(), "mathrm");
907 case LFUN_FONT_TYPEWRITER:
908 if (currentMode() == TEXT_MODE)
909 handleFont(cur, cmd.argument(), "texttt");
911 handleFont(cur, cmd.argument(), "mathtt");
914 handleFont(cur, cmd.argument(), "mathfrak");
917 if (currentMode() == TEXT_MODE)
918 handleFont(cur, cmd.argument(), "textit");
920 handleFont(cur, cmd.argument(), "mathit");
923 if (currentMode() == TEXT_MODE)
924 // FIXME: should be "noun"
925 handleFont(cur, cmd.argument(), "textsc");
927 handleFont(cur, cmd.argument(), "mathbb");
929 case LFUN_FONT_DEFAULT:
930 handleFont(cur, cmd.argument(), "textnormal");
933 case LFUN_MATH_MODE: {
935 // ignore math-mode on when already in math mode
936 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
938 cur.recordUndoSelection();
939 cur.macroModeClose();
940 docstring const save_selection = grabAndEraseSelection(cur);
942 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
943 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
945 cur.pushBackward(*cur.nextInset());
946 cur.niceInsert(save_selection);
948 if (currentMode() == Inset::TEXT_MODE) {
949 cur.recordUndoSelection();
950 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
951 cur.message(_("create new math text environment ($...$)"));
953 handleFont(cur, cmd.argument(), "textrm");
954 cur.message(_("entered math text mode (textrm)"));
960 case LFUN_MATH_SIZE: {
961 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, cmd.argument());
966 case LFUN_MATH_MATRIX: {
972 idocstringstream is(cmd.argument());
973 is >> m >> n >> v_align >> h_align;
980 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
984 case LFUN_MATH_DELIM: {
986 docstring rs = split(cmd.argument(), ls, ' ');
987 // Reasonable default values
993 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
997 case LFUN_MATH_BIGDELIM: {
998 docstring const lname = from_utf8(cmd.getArg(0));
999 docstring const ldelim = from_utf8(cmd.getArg(1));
1000 docstring const rname = from_utf8(cmd.getArg(2));
1001 docstring const rdelim = from_utf8(cmd.getArg(3));
1002 latexkeys const * l = in_word_set(lname);
1003 bool const have_l = l && l->inset == "big" &&
1004 InsetMathBig::isBigInsetDelim(ldelim);
1005 l = in_word_set(rname);
1006 bool const have_r = l && l->inset == "big" &&
1007 InsetMathBig::isBigInsetDelim(rdelim);
1008 // We mimic LFUN_MATH_DELIM in case we have an empty left
1009 // or right delimiter.
1010 if (have_l || have_r) {
1012 docstring const selection = grabAndEraseSelection(cur);
1015 cur.insert(MathAtom(new InsetMathBig(lname,
1017 cur.niceInsert(selection);
1019 cur.insert(MathAtom(new InsetMathBig(rname,
1022 // Don't call cur.undispatched() if we did nothing, this would
1023 // lead to infinite recursion via Text::dispatch().
1027 case LFUN_SPACE_INSERT:
1028 cur.recordUndoSelection();
1029 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
1032 case LFUN_MATH_SPACE:
1033 cur.recordUndoSelection();
1034 if (cmd.argument().empty())
1035 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
1037 cur.insert(MathAtom(new InsetMathSpace(cmd.argument())));
1040 case LFUN_ERT_INSERT:
1041 // interpret this as if a backslash was typed
1043 interpretChar(cur, '\\');
1046 case LFUN_MATH_SUBSCRIPT:
1047 // interpret this as if a _ was typed
1048 cur.recordUndoSelection();
1049 interpretChar(cur, '_');
1052 case LFUN_MATH_SUPERSCRIPT:
1053 // interpret this as if a ^ was typed
1054 cur.recordUndoSelection();
1055 interpretChar(cur, '^');
1058 case LFUN_MATH_MACRO_FOLD:
1059 case LFUN_MATH_MACRO_UNFOLD: {
1061 bool fold = cmd.action == LFUN_MATH_MACRO_FOLD;
1062 bool found = findMacroToFoldUnfold(it, fold);
1064 MathMacro * macro = it.nextInset()->asInsetMath()->asMacro();
1065 cur.recordUndoInset();
1074 case LFUN_QUOTE_INSERT:
1075 // interpret this as if a straight " was typed
1076 cur.recordUndoSelection();
1077 interpretChar(cur, '\"');
1080 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
1081 // handling such that "self-insert" works on "arbitrary stuff" too, and
1082 // math-insert only handles special math things like "matrix".
1083 case LFUN_MATH_INSERT: {
1084 cur.recordUndoSelection();
1085 if (cmd.argument() == "^" || cmd.argument() == "_")
1086 interpretChar(cur, cmd.argument()[0]);
1089 asArray(cmd.argument(), ar);
1090 if (ar.size() == 1 && ar[0]->asNestInset()
1091 && ar[0]->asNestInset()->nargs() > 1)
1092 handleNest(cur, ar[0]);
1094 cur.niceInsert(cmd.argument());
1099 case LFUN_DIALOG_SHOW_NEW_INSET: {
1100 docstring const & name = cmd.argument();
1102 if (name == "ref") {
1103 InsetMathRef tmp(name);
1104 data = tmp.createDialogStr(to_utf8(name));
1106 cur.bv().showDialog(to_utf8(name), data);
1110 case LFUN_INSET_INSERT: {
1112 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1113 cur.recordUndoSelection();
1119 case LFUN_INSET_DISSOLVE:
1120 if (!asHullInset()) {
1121 cur.recordUndoInset();
1127 InsetMath::doDispatch(cur, cmd);
1133 bool InsetMathNest::findMacroToFoldUnfold(Cursor & it, bool fold) const {
1134 // look for macro to open/close, but stay in mathed
1135 for (; !it.empty(); it.pop_back()) {
1137 // go backward through the current cell
1138 Inset * inset = it.nextInset();
1139 while (inset && inset->asInsetMath()) {
1140 MathMacro * macro = inset->asInsetMath()->asMacro();
1142 // found the an macro to open/close?
1143 if (macro->folded() != fold)
1146 // Wrong folding state.
1147 // If this was the first we see in this slice, look further left,
1149 if (inset != it.nextInset())
1153 // go up if this was the left most position
1159 inset = it.nextInset();
1167 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1168 FuncStatus & flag) const
1170 // the font related toggles
1171 //string tc = "mathnormal";
1173 string const arg = to_utf8(cmd.argument());
1174 switch (cmd.action) {
1175 case LFUN_TABULAR_FEATURE:
1176 flag.setEnabled(false);
1179 case LFUN_TABULAR_FEATURE:
1180 // FIXME: check temporarily disabled
1182 char align = mathcursor::valign();
1183 if (align == '\0') {
1187 if (cmd.argument().empty()) {
1191 if (!contains("tcb", cmd.argument()[0])) {
1195 flag.setOnOff(cmd.argument()[0] == align);
1198 /// We have to handle them since 1.4 blocks all unhandled actions
1199 case LFUN_FONT_ITAL:
1200 case LFUN_FONT_BOLD:
1201 case LFUN_FONT_BOLDSYMBOL:
1202 case LFUN_FONT_SANS:
1203 case LFUN_FONT_EMPH:
1204 case LFUN_FONT_TYPEWRITER:
1205 case LFUN_FONT_NOUN:
1206 case LFUN_FONT_ROMAN:
1207 case LFUN_FONT_DEFAULT:
1208 flag.setEnabled(true);
1210 case LFUN_MATH_MUTATE:
1211 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1212 flag.setOnOff(false);
1215 // we just need to be in math mode to enable that
1216 case LFUN_MATH_SIZE:
1217 case LFUN_MATH_SPACE:
1218 case LFUN_MATH_LIMITS:
1219 case LFUN_MATH_EXTERN:
1220 flag.setEnabled(true);
1223 case LFUN_FONT_FRAK:
1224 flag.setEnabled(currentMode() != TEXT_MODE);
1227 case LFUN_MATH_INSERT: {
1228 bool const textarg =
1229 arg == "\\textbf" || arg == "\\textsf" ||
1230 arg == "\\textrm" || arg == "\\textmd" ||
1231 arg == "\\textit" || arg == "\\textsc" ||
1232 arg == "\\textsl" || arg == "\\textup" ||
1233 arg == "\\texttt" || arg == "\\textbb" ||
1234 arg == "\\textnormal";
1235 flag.setEnabled(currentMode() != TEXT_MODE || textarg);
1239 case LFUN_MATH_MATRIX:
1240 flag.setEnabled(currentMode() == MATH_MODE);
1243 case LFUN_INSET_INSERT: {
1244 // Don't test createMathInset_fromDialogStr(), since
1245 // getStatus is not called with a valid reference and the
1246 // dialog would not be applyable.
1247 string const name = cmd.getArg(0);
1248 flag.setEnabled(name == "ref");
1252 case LFUN_MATH_DELIM:
1253 case LFUN_MATH_BIGDELIM:
1254 // Don't do this with multi-cell selections
1255 flag.setEnabled(cur.selBegin().idx() == cur.selEnd().idx());
1258 case LFUN_MATH_MACRO_FOLD:
1259 case LFUN_MATH_MACRO_UNFOLD: {
1261 bool found = findMacroToFoldUnfold(it, cmd.action == LFUN_MATH_MACRO_FOLD);
1262 flag.setEnabled(found);
1266 case LFUN_SPECIALCHAR_INSERT:
1267 // FIXME: These would probably make sense in math-text mode
1268 flag.setEnabled(false);
1271 case LFUN_INSET_DISSOLVE:
1272 flag.setEnabled(!asHullInset());
1283 void InsetMathNest::edit(Cursor & cur, bool front, EntryDirection entry_from)
1286 bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_RIGHT ||
1287 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1288 cur.idx() = enter_front ? 0 : cur.lastidx();
1289 cur.pos() = enter_front ? 0 : cur.lastpos();
1291 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1295 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1298 int dist_min = 1000000;
1299 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1300 int const d = cell(i).dist(cur.bv(), x, y);
1306 MathData & ar = cell(idx_min);
1308 cur.idx() = idx_min;
1309 cur.pos() = ar.x2pos(&cur.bv(), x - ar.xo(cur.bv()));
1311 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1312 if (dist_min == 0) {
1314 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1315 if (ar[i]->covers(cur.bv(), x, y))
1316 return ar[i].nucleus()->editXY(cur, x, y);
1322 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1324 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1325 BufferView & bv = cur.bv();
1326 bool do_selection = cmd.button() == mouse_button::button1
1327 && cmd.argument() == "region-select";
1328 bv.mouseSetCursor(cur, do_selection);
1329 if (cmd.button() == mouse_button::button1) {
1330 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1331 // Update the cursor update flags as needed:
1333 // Update::Decoration: tells to update the decoration
1334 // (visual box corners that define
1336 // Update::FitCursor: adjust the screen to the cursor
1337 // position if needed
1338 // cur.result().update(): don't overwrite previously set flags.
1339 cur.updateFlags(Update::Decoration | Update::FitCursor
1340 | cur.result().update());
1341 } else if (cmd.button() == mouse_button::button2) {
1342 if (cap::selection()) {
1343 // See comment in Text::dispatch why we do this
1344 cap::copySelectionToStack();
1345 cmd = FuncRequest(LFUN_PASTE, "0");
1346 doDispatch(bv.cursor(), cmd);
1349 asArray(theSelection().get(), ar);
1350 bv.cursor().insert(ar);
1356 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1358 // only select with button 1
1359 if (cmd.button() == mouse_button::button1) {
1360 Cursor & bvcur = cur.bv().cursor();
1361 if (bvcur.anchor_.hasPart(cur)) {
1362 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1363 bvcur.setCursor(cur);
1364 bvcur.setSelection(true);
1365 //lyxerr << "MOTION " << bvcur << endl;
1372 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1374 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1376 if (cmd.button() == mouse_button::button1) {
1377 if (!cur.selection())
1380 Cursor & bvcur = cur.bv().cursor();
1381 bvcur.setSelection(true);
1390 bool InsetMathNest::interpretChar(Cursor & cur, char_type c)
1392 //lyxerr << "interpret 2: '" << c << "'" << endl;
1393 docstring save_selection;
1394 if (c == '^' || c == '_')
1395 save_selection = grabAndEraseSelection(cur);
1400 if (cur.inMacroMode()) {
1401 docstring name = cur.macroName();
1403 /// are we currently typing '#1' or '#2' or...?
1404 if (name == "\\#") {
1407 if (n >= 1 && n <= 9)
1408 cur.insert(new MathMacroArgument(n));
1412 // do not finish macro for known * commands
1413 MathWordList const & mwl = mathedWordList();
1414 bool star_macro = c == '*'
1415 && (mwl.find(name.substr(1) + "*") != mwl.end()
1416 || cur.buffer().getMacro(name.substr(1) + "*", cur, true));
1417 if (isAlphaASCII(c) || star_macro) {
1418 cur.activeMacro()->setName(name + docstring(1, c));
1422 // handle 'special char' macros
1427 if (currentMode() == InsetMath::TEXT_MODE)
1428 cur.niceInsert(createInsetMath("textbackslash"));
1430 cur.niceInsert(createInsetMath("backslash"));
1431 } else if (c == '{') {
1433 cur.niceInsert(MathAtom(new InsetMathBrace));
1434 } else if (c == '%') {
1436 cur.niceInsert(MathAtom(new InsetMathComment));
1437 } else if (c == '#') {
1438 LASSERT(cur.activeMacro(), /**/);
1439 cur.activeMacro()->setName(name + docstring(1, c));
1442 cur.niceInsert(createInsetMath(docstring(1, c)));
1447 // One character big delimiters. The others are handled in
1448 // interpretString().
1449 latexkeys const * l = in_word_set(name.substr(1));
1450 if (name[0] == '\\' && l && l->inset == "big") {
1454 delim = from_ascii("\\{");
1457 delim = from_ascii("\\}");
1460 delim = docstring(1, c);
1463 if (InsetMathBig::isBigInsetDelim(delim)) {
1464 // name + delim ared a valid InsetMathBig.
1465 // We can't use cur.macroModeClose() because
1466 // it does not handle delim.
1467 InsetMathUnknown * p = cur.activeMacro();
1470 cur.cell().erase(cur.pos());
1471 cur.plainInsert(MathAtom(
1472 new InsetMathBig(name.substr(1), delim)));
1477 // leave macro mode and try again if necessary
1478 cur.macroModeClose();
1480 cur.niceInsert(MathAtom(new InsetMathBrace));
1482 interpretChar(cur, c);
1486 // This is annoying as one has to press <space> far too often.
1490 // leave autocorrect mode if necessary
1491 if (autocorrect() && c == ' ') {
1492 autocorrect() = false;
1497 // just clear selection on pressing the space bar
1498 if (cur.selection() && c == ' ') {
1499 cur.setSelection(false);
1504 //lyxerr << "starting with macro" << endl;
1505 bool reduced = cap::reduceSelectionToOneCell(cur);
1506 if (reduced || !cur.selection()) {
1507 docstring const safe = cap::grabAndEraseSelection(cur);
1508 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), safe, false)));
1516 if (currentMode() == InsetMath::TEXT_MODE)
1522 if (currentMode() == InsetMath::TEXT_MODE) {
1523 // insert spaces in text mode,
1524 // but suppress direct insertion of two spaces in a row
1525 // the still allows typing '<space>a<space>' and deleting the 'a', but
1526 // it is better than nothing...
1527 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1529 // FIXME: we have to enable full redraw here because of the
1530 // visual box corners that define the inset. If we know for
1531 // sure that we stay within the same cell we can optimize for
1533 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1537 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1538 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1539 // FIXME: we have to enable full redraw here because of the
1540 // visual box corners that define the inset. If we know for
1541 // sure that we stay within the same cell we can optimize for
1543 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1547 if (cur.popForward()) {
1548 // FIXME: we have to enable full redraw here because of the
1549 // visual box corners that define the inset. If we know for
1550 // sure that we stay within the same cell we can optimize for
1552 //cur.updateFlags(Update::FitCursor);
1556 // if we are at the very end, leave the formula
1557 return cur.pos() != cur.lastpos();
1560 // These shouldn't work in text mode:
1561 if (currentMode() != InsetMath::TEXT_MODE) {
1563 script(cur, false, save_selection);
1567 script(cur, true, save_selection);
1571 cur.niceInsert(createInsetMath("sim"));
1576 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1577 c == '%' || c == '_' || c == '^') {
1578 cur.niceInsert(createInsetMath(docstring(1, c)));
1583 // try auto-correction
1584 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1587 // no special circumstances, so insert the character without any fuss
1589 cur.autocorrect() = true;
1594 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1596 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1598 if (!cur.empty() && cur.pos() > 0 &&
1599 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1600 if (InsetMathBig::isBigInsetDelim(str)) {
1601 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1602 if (prev[0] == '\\') {
1603 prev = prev.substr(1);
1604 latexkeys const * l = in_word_set(prev);
1605 if (l && l->inset == "big") {
1606 cur.cell()[cur.pos() - 1] =
1607 MathAtom(new InsetMathBig(prev, str));
1617 bool InsetMathNest::script(Cursor & cur, bool up)
1619 return script(cur, up, docstring());
1623 bool InsetMathNest::script(Cursor & cur, bool up,
1624 docstring const & save_selection)
1626 // Hack to get \^ and \_ working
1627 //lyxerr << "handling script: up: " << up << endl;
1628 if (cur.inMacroMode() && cur.macroName() == "\\") {
1630 cur.niceInsert(createInsetMath("mathcircumflex"));
1632 interpretChar(cur, '_');
1636 cur.macroModeClose();
1637 if (asScriptInset() && cur.idx() == 0) {
1638 // we are in a nucleus of a script inset, move to _our_ script
1639 InsetMathScript * inset = asScriptInset();
1640 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1642 cur.idx() = inset->idxOfScript(up);
1644 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1646 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1649 cur.idx() = inset->idxOfScript(up);
1650 cur.pos() = cur.lastpos();
1652 // convert the thing to our left to a scriptinset or create a new
1653 // one if in the very first position of the array
1654 if (cur.pos() == 0) {
1655 //lyxerr << "new scriptinset" << endl;
1656 cur.insert(new InsetMathScript(up));
1658 //lyxerr << "converting prev atom " << endl;
1659 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1662 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1663 // See comment in MathParser.cpp for special handling of {}-bases
1669 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1670 cur.niceInsert(save_selection);
1672 //lyxerr << "inserting selection 2:\n" << save_selection << endl;
1677 bool InsetMathNest::completionSupported(Cursor const & cur) const
1679 return cur.inMacroMode();
1683 bool InsetMathNest::inlineCompletionSupported(Cursor const & cur) const
1685 return cur.inMacroMode();
1689 bool InsetMathNest::automaticInlineCompletion() const
1691 return lyxrc.completion_inline_math;
1695 bool InsetMathNest::automaticPopupCompletion() const
1697 return lyxrc.completion_popup_math;
1701 CompletionList const *
1702 InsetMathNest::createCompletionList(Cursor const & cur) const
1704 if (!cur.inMacroMode())
1707 return new MathCompletionList(cur);
1711 docstring InsetMathNest::completionPrefix(Cursor const & cur) const
1713 if (!cur.inMacroMode())
1716 return cur.activeMacro()->name();
1720 bool InsetMathNest::insertCompletion(Cursor & cur, docstring const & s,
1723 if (!cur.inMacroMode())
1726 // append completion to active macro
1727 InsetMathUnknown * inset = cur.activeMacro();
1728 inset->setName(inset->name() + s);
1733 // FIXME: this creates duplicates in the completion popup
1734 // which looks ugly. Moreover the changes the list lengths
1736 confuse the popup as well.
1737 MathCompletionList::addToFavorites(inset->name());
1739 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, " "));
1746 void InsetMathNest::completionPosAndDim(Cursor const & cur, int & x, int & y,
1747 Dimension & dim) const
1749 Inset const * inset = cur.activeMacro();
1753 // get inset dimensions
1754 dim = cur.bv().coordCache().insets().dim(inset);
1755 // FIXME: these 3 are no accurate, but should depend on the font.
1756 // Now the popup jumps down if you enter a char with descent > 0.
1762 = cur.bv().coordCache().insets().xy(inset);
1768 bool InsetMathNest::cursorMathForward(Cursor & cur)
1770 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
1771 cur.pushBackward(*cur.nextAtom().nucleus());
1772 cur.inset().idxFirst(cur);
1775 if (cur.posForward() || idxForward(cur))
1777 // try to pop forwards --- but don't pop out of math! leave that to
1779 int s = cur.depth() - 2;
1780 if (s >= 0 && cur[s].inset().asInsetMath())
1781 return cur.popForward();
1786 bool InsetMathNest::cursorMathBackward(Cursor & cur)
1788 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
1790 cur.push(*cur.nextAtom().nucleus());
1791 cur.inset().idxLast(cur);
1794 if (cur.posBackward() || idxBackward(cur))
1796 // try to pop backwards --- but don't pop out of math! leave that to
1798 int s = cur.depth() - 2;
1799 if (s >= 0 && cur[s].inset().asInsetMath())
1800 return cur.popBackward();
1805 ////////////////////////////////////////////////////////////////////
1807 MathCompletionList::MathCompletionList(Cursor const & cur)
1809 // fill it with macros from the buffer
1810 MacroNameSet macros;
1811 cur.buffer().listMacroNames(macros);
1812 MacroNameSet::const_iterator it;
1813 for (it = macros.begin(); it != macros.end(); ++it) {
1814 if (cur.buffer().getMacro(*it, cur, false))
1815 locals.push_back("\\" + *it);
1817 sort(locals.begin(), locals.end());
1819 if (globals.size() > 0)
1822 // fill in global macros
1824 MacroTable::globalMacros().getMacroNames(macros);
1825 //lyxerr << "Globals completion macros: ";
1826 for (it = macros.begin(); it != macros.end(); ++it) {
1827 //lyxerr << "\\" + *it << " ";
1828 globals.push_back("\\" + *it);
1830 //lyxerr << std::endl;
1832 // fill in global commands
1833 globals.push_back(from_ascii("\\boxed"));
1834 globals.push_back(from_ascii("\\fbox"));
1835 globals.push_back(from_ascii("\\framebox"));
1836 globals.push_back(from_ascii("\\makebox"));
1837 globals.push_back(from_ascii("\\kern"));
1838 globals.push_back(from_ascii("\\xrightarrow"));
1839 globals.push_back(from_ascii("\\xleftarrow"));
1840 globals.push_back(from_ascii("\\split"));
1841 globals.push_back(from_ascii("\\gathered"));
1842 globals.push_back(from_ascii("\\aligned"));
1843 globals.push_back(from_ascii("\\alignedat"));
1844 globals.push_back(from_ascii("\\cases"));
1845 globals.push_back(from_ascii("\\substack"));
1846 globals.push_back(from_ascii("\\xymatrix"));
1847 globals.push_back(from_ascii("\\subarray"));
1848 globals.push_back(from_ascii("\\array"));
1849 globals.push_back(from_ascii("\\sqrt"));
1850 globals.push_back(from_ascii("\\root"));
1851 globals.push_back(from_ascii("\\tabular"));
1852 globals.push_back(from_ascii("\\stackrel"));
1853 globals.push_back(from_ascii("\\binom"));
1854 globals.push_back(from_ascii("\\choose"));
1855 globals.push_back(from_ascii("\\brace"));
1856 globals.push_back(from_ascii("\\brack"));
1857 globals.push_back(from_ascii("\\frac"));
1858 globals.push_back(from_ascii("\\over"));
1859 globals.push_back(from_ascii("\\nicefrac"));
1860 globals.push_back(from_ascii("\\unitfrac"));
1861 globals.push_back(from_ascii("\\unitfracthree"));
1862 globals.push_back(from_ascii("\\unitone"));
1863 globals.push_back(from_ascii("\\unittwo"));
1864 globals.push_back(from_ascii("\\infer"));
1865 globals.push_back(from_ascii("\\atop"));
1866 globals.push_back(from_ascii("\\lefteqn"));
1867 globals.push_back(from_ascii("\\boldsymbol"));
1868 globals.push_back(from_ascii("\\bm"));
1869 globals.push_back(from_ascii("\\color"));
1870 globals.push_back(from_ascii("\\normalcolor"));
1871 globals.push_back(from_ascii("\\textcolor"));
1872 globals.push_back(from_ascii("\\dfrac"));
1873 globals.push_back(from_ascii("\\tfrac"));
1874 globals.push_back(from_ascii("\\dbinom"));
1875 globals.push_back(from_ascii("\\tbinom"));
1876 globals.push_back(from_ascii("\\hphantom"));
1877 globals.push_back(from_ascii("\\phantom"));
1878 globals.push_back(from_ascii("\\vphantom"));
1879 MathWordList const & words = mathedWordList();
1880 MathWordList::const_iterator it2;
1881 //lyxerr << "Globals completion commands: ";
1882 for (it2 = words.begin(); it2 != words.end(); ++it2) {
1883 globals.push_back("\\" + (*it2).first);
1884 //lyxerr << "\\" + (*it2).first << " ";
1886 //lyxerr << std::endl;
1887 sort(globals.begin(), globals.end());
1891 MathCompletionList::~MathCompletionList()
1896 size_type MathCompletionList::size() const
1898 return locals.size() + globals.size();
1902 docstring const & MathCompletionList::data(size_t idx) const
1904 size_t lsize = locals.size();
1906 return globals[idx - lsize];
1912 std::string MathCompletionList::icon(size_t idx) const
1914 // get the latex command
1916 size_t lsize = locals.size();
1918 cmd = globals[idx - lsize];
1922 // get the icon resource name by stripping the backslash
1923 return "images/math/" + to_utf8(cmd.substr(1)) + ".png";
1926 std::vector<docstring> MathCompletionList::globals;