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;
76 char const * text_commands[] =
77 { "text", "textrm", "textsf", "texttt", "textmd", "textbf", "textup", "textit",
79 int const num_text_commands = sizeof(text_commands) / sizeof(*text_commands);
82 InsetMathNest::InsetMathNest(idx_type nargs)
83 : cells_(nargs), lock_(false), mouse_hover_(false)
87 InsetMathNest::InsetMathNest(InsetMathNest const & inset)
88 : InsetMath(inset), cells_(inset.cells_), lock_(inset.lock_),
93 InsetMathNest & InsetMathNest::operator=(InsetMathNest const & inset)
95 cells_ = inset.cells_;
98 InsetMath::operator=(inset);
103 InsetMath::idx_type InsetMathNest::nargs() const
105 return cells_.size();
109 void InsetMathNest::cursorPos(BufferView const & bv,
110 CursorSlice const & sl, bool /*boundary*/,
111 int & x, int & y) const
113 // FIXME: This is a hack. Ideally, the coord cache should not store
114 // absolute positions, but relative ones. This would mean to call
115 // setXY() not in MathData::draw(), but in the parent insets' draw()
116 // with the correctly adjusted x,y values. But this means that we'd have
117 // to touch all (math)inset's draw() methods. Right now, we'll store
118 // absolute value, and make them here relative, only to make them
119 // absolute again when actually drawing the cursor. What a mess.
120 LASSERT(&sl.inset() == this, /**/);
121 MathData const & ar = sl.cell();
122 CoordCache const & coord_cache = bv.coordCache();
123 if (!coord_cache.getArrays().has(&ar)) {
124 // this can (semi-)legally happen if we just created this cell
125 // and it never has been drawn before. So don't ASSERT.
126 //lyxerr << "no cached data for array " << &ar << endl;
131 Point const pt = coord_cache.getArrays().xy(&ar);
132 if (!coord_cache.getInsets().has(this)) {
134 //lyxerr << "no cached data for inset " << this << endl;
139 Point const pt2 = coord_cache.getInsets().xy(this);
140 //lyxerr << "retrieving position cache for MathData "
141 // << pt.x_ << ' ' << pt.y_ << endl;
142 x = pt.x_ - pt2.x_ + ar.pos2x(&bv, sl.pos());
144 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
145 // << " asc: " << ascent() << " des: " << descent()
146 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
147 // move cursor visually into empty cells ("blue rectangles");
153 void InsetMathNest::metrics(MetricsInfo const & mi) const
156 for (idx_type i = 0, n = nargs(); i != n; ++i) {
158 cell(i).metrics(m, dim);
163 bool InsetMathNest::idxNext(Cursor & cur) const
165 LASSERT(&cur.inset() == this, /**/);
166 if (cur.idx() == cur.lastidx())
174 bool InsetMathNest::idxForward(Cursor & cur) const
180 bool InsetMathNest::idxPrev(Cursor & cur) const
182 LASSERT(&cur.inset() == this, /**/);
186 cur.pos() = cur.lastpos();
191 bool InsetMathNest::idxBackward(Cursor & cur) const
197 bool InsetMathNest::idxFirst(Cursor & cur) const
199 LASSERT(&cur.inset() == this, /**/);
208 bool InsetMathNest::idxLast(Cursor & cur) const
210 LASSERT(&cur.inset() == this, /**/);
213 cur.idx() = cur.lastidx();
214 cur.pos() = cur.lastpos();
219 void InsetMathNest::dump() const
221 odocstringstream oss;
223 os << "---------------------------------------------\n";
226 for (idx_type i = 0, n = nargs(); i != n; ++i)
227 os << cell(i) << "\n";
228 os << "---------------------------------------------\n";
229 lyxerr << to_utf8(oss.str());
233 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
237 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
240 setPosCache(pi, x, y);
244 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
246 BufferView & bv = *pi.base.bv;
247 // this should use the x/y values given, not the cached values
248 Cursor & cur = bv.cursor();
249 if (!cur.selection())
251 if (&cur.inset() != this)
254 // FIXME: hack to get position cache warm
255 bool const original_drawing_state = pi.pain.isDrawingEnabled();
256 pi.pain.setDrawingEnabled(false);
258 pi.pain.setDrawingEnabled(original_drawing_state);
260 CursorSlice s1 = cur.selBegin();
261 CursorSlice s2 = cur.selEnd();
263 //lyxerr << "InsetMathNest::drawing selection: "
264 // << " s1: " << s1 << " s2: " << s2 << endl;
265 if (s1.idx() == s2.idx()) {
266 MathData const & c = cell(s1.idx());
267 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
268 int x1 = g.pos.x_ + c.pos2x(pi.base.bv, s1.pos());
269 int y1 = g.pos.y_ - g.dim.ascent();
270 int x2 = g.pos.x_ + c.pos2x(pi.base.bv, s2.pos());
271 int y2 = g.pos.y_ + g.dim.descent();
272 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
273 //lyxerr << "InsetMathNest::drawing selection 3: "
274 // << " x1: " << x1 << " x2: " << x2
275 // << " y1: " << y1 << " y2: " << y2 << endl;
277 for (idx_type i = 0; i < nargs(); ++i) {
278 if (idxBetween(i, s1.idx(), s2.idx())) {
279 MathData const & c = cell(i);
280 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
282 int y1 = g.pos.y_ - g.dim.ascent();
283 int x2 = g.pos.x_ + g.dim.width();
284 int y2 = g.pos.y_ + g.dim.descent();
285 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
292 void InsetMathNest::validate(LaTeXFeatures & features) const
294 for (idx_type i = 0; i < nargs(); ++i)
295 cell(i).validate(features);
299 void InsetMathNest::replace(ReplaceData & rep)
301 for (idx_type i = 0; i < nargs(); ++i)
302 cell(i).replace(rep);
306 bool InsetMathNest::contains(MathData const & ar) const
308 for (idx_type i = 0; i < nargs(); ++i)
309 if (cell(i).contains(ar))
315 bool InsetMathNest::lock() const
321 void InsetMathNest::lock(bool l)
327 bool InsetMathNest::isActive() const
333 MathData InsetMathNest::glue() const
336 for (size_t i = 0; i < nargs(); ++i)
342 void InsetMathNest::write(WriteStream & os) const
344 bool oldmode = os.textMode();
345 docstring const latex_name = name().c_str();
346 for (int i = 0; i < num_text_commands; ++i) {
347 if (latex_name == from_ascii(text_commands[i])) {
352 os << '\\' << latex_name;
353 for (size_t i = 0; i < nargs(); ++i)
354 os << '{' << cell(i) << '}';
356 os.pendingSpace(true);
357 if (lock_ && !os.latex()) {
359 os.pendingSpace(true);
361 os.textMode(oldmode);
365 void InsetMathNest::normalize(NormalStream & os) const
367 os << '[' << name().c_str();
368 for (size_t i = 0; i < nargs(); ++i)
369 os << ' ' << cell(i);
374 int InsetMathNest::latex(odocstream & os, OutputParams const & runparams) const
376 WriteStream wi(os, runparams.moving_arg, true, runparams.dryrun);
382 bool InsetMathNest::setMouseHover(bool mouse_hover)
384 mouse_hover_ = mouse_hover;
389 bool InsetMathNest::notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
393 MathData & ar = cur.cell();
394 // remove base-only "scripts"
395 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
396 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
397 if (p && p->nargs() == 1) {
398 MathData ar = p->nuc();
401 cur.adjust(i, ar.size() - 1);
405 // glue adjacent font insets of the same kind
406 for (pos_type i = 0; i + 1 < size(); ++i) {
407 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
408 InsetMathFont const * q = operator[](i + 1)->asFontInset();
409 if (p && q && p->name() == q->name()) {
410 p->cell(0).append(q->cell(0));
420 void InsetMathNest::handleFont
421 (Cursor & cur, docstring const & arg, char const * const font)
423 handleFont(cur, arg, from_ascii(font));
427 void InsetMathNest::handleFont(Cursor & cur, docstring const & arg,
428 docstring const & font)
430 cur.recordUndoSelection();
432 // this whole function is a hack and won't work for incremental font
434 if (cur.inset().asInsetMath()->name() == font)
435 cur.handleFont(to_utf8(font));
437 handleNest(cur, createInsetMath(font), arg);
441 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest)
443 handleNest(cur, nest, docstring());
447 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest,
448 docstring const & arg)
450 CursorSlice i1 = cur.selBegin();
451 CursorSlice i2 = cur.selEnd();
452 if (!i1.inset().asInsetMath())
454 if (i1.idx() == i2.idx()) {
455 // the easy case where only one cell is selected
456 cur.handleNest(nest);
461 // multiple selected cells in a simple non-grid inset
462 if (i1.asInsetMath()->nrows() == 0 || i1.asInsetMath()->ncols() == 0) {
463 for (idx_type i = i1.idx(); i <= i2.idx(); ++i) {
468 cur.pos() = cur.lastpos();
471 // change font of cell
472 cur.handleNest(nest);
475 // cur is in the font inset now. If the loop continues,
476 // we need to get outside again for the next cell
477 if (i + 1 <= i2.idx())
483 // the complicated case with multiple selected cells in a grid
486 cap::region(i1, i2, r1, r2, c1, c2);
487 for (row_type row = r1; row <= r2; ++row) {
488 for (col_type col = c1; col <= c2; ++col) {
490 cur.idx() = i1.asInsetMath()->index(row, col);
493 cur.pos() = cur.lastpos();
497 cur.handleNest(nest);
500 // cur is in the font inset now. If the loop continues,
501 // we need to get outside again for the next cell
502 if (col + 1 <= c2 || row + 1 <= r2)
509 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
511 cur.recordUndoSelection();
514 font.fromString(to_utf8(arg), b);
515 if (font.fontInfo().color() != Color_inherit &&
516 font.fontInfo().color() != Color_ignore)
517 handleNest(cur, MathAtom(new InsetMathColor(true, font.fontInfo().color())));
519 // FIXME: support other font changes here as well?
523 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
525 //lyxerr << "InsetMathNest: request: " << cmd << endl;
527 switch (cmd.action) {
530 cur.recordUndoSelection();
531 cur.message(_("Paste"));
532 replaceSelection(cur);
534 if (cmd.argument().empty() && !theClipboard().isInternal())
535 topaste = theClipboard().getAsText();
538 idocstringstream is(cmd.argument());
540 topaste = cap::selection(n);
542 cur.niceInsert(topaste);
543 cur.clearSelection(); // bug 393
550 cutSelection(cur, true, true);
551 cur.message(_("Cut"));
552 // Prevent stale position >= size crash
553 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
559 cur.message(_("Copy"));
562 case LFUN_MOUSE_PRESS:
563 lfunMousePress(cur, cmd);
566 case LFUN_MOUSE_MOTION:
567 lfunMouseMotion(cur, cmd);
570 case LFUN_MOUSE_RELEASE:
571 lfunMouseRelease(cur, cmd);
574 case LFUN_FINISHED_LEFT: // in math, left is backwards
575 case LFUN_FINISHED_BACKWARD:
576 cur.bv().cursor() = cur;
579 case LFUN_FINISHED_RIGHT: // in math, right is forward
580 case LFUN_FINISHED_FORWARD:
582 cur.bv().cursor() = cur;
585 case LFUN_CHAR_RIGHT:
587 case LFUN_CHAR_BACKWARD:
588 case LFUN_CHAR_FORWARD:
589 cur.updateFlags(Update::Decoration | Update::FitCursor);
590 case LFUN_CHAR_RIGHT_SELECT:
591 case LFUN_CHAR_LEFT_SELECT:
592 case LFUN_CHAR_BACKWARD_SELECT:
593 case LFUN_CHAR_FORWARD_SELECT: {
594 // are we in a selection?
595 bool select = (cmd.action == LFUN_CHAR_RIGHT_SELECT
596 || cmd.action == LFUN_CHAR_LEFT_SELECT
597 || cmd.action == LFUN_CHAR_BACKWARD_SELECT
598 || cmd.action == LFUN_CHAR_FORWARD_SELECT);
599 // are we moving forward or backwards?
600 // If the command was RIGHT or LEFT, then whether we're moving forward
601 // or backwards depends on the cursor movement mode (logical or visual):
602 // * in visual mode, since math is always LTR, right -> forward,
604 // * in logical mode, the mapping is determined by the
605 // reverseDirectionNeeded() function
608 FuncCode finish_lfun;
610 if (cmd.action == LFUN_CHAR_FORWARD
611 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
613 finish_lfun = LFUN_FINISHED_FORWARD;
615 else if (cmd.action == LFUN_CHAR_BACKWARD
616 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
618 finish_lfun = LFUN_FINISHED_BACKWARD;
621 bool right = (cmd.action == LFUN_CHAR_RIGHT_SELECT
622 || cmd.action == LFUN_CHAR_RIGHT);
623 if (lyxrc.visual_cursor || !reverseDirectionNeeded(cur))
629 finish_lfun = LFUN_FINISHED_RIGHT;
631 finish_lfun = LFUN_FINISHED_LEFT;
633 // Now that we know exactly what we want to do, let's do it!
634 cur.selHandle(select);
635 cur.autocorrect() = false;
637 cur.macroModeClose();
638 // try moving forward or backwards as necessary...
639 if (!(forward ? cursorMathForward(cur) : cursorMathBackward(cur))) {
640 // ... and if movement failed, then finish forward or backwards
642 cmd = FuncRequest(finish_lfun);
650 cur.updateFlags(Update::Decoration | Update::FitCursor);
651 case LFUN_DOWN_SELECT:
652 case LFUN_UP_SELECT: {
653 // close active macro
654 if (cur.inMacroMode()) {
655 cur.macroModeClose();
659 // stop/start the selection
660 bool select = cmd.action == LFUN_DOWN_SELECT ||
661 cmd.action == LFUN_UP_SELECT;
662 cur.selHandle(select);
665 bool up = cmd.action == LFUN_UP || cmd.action == LFUN_UP_SELECT;
666 bool successful = cur.upDownInMath(up);
670 if (cur.fixIfBroken())
671 // FIXME: Something bad happened. We pass the corrected Cursor
672 // instead of letting things go worse.
675 // We did not manage to move the cursor.
680 case LFUN_MOUSE_DOUBLE:
681 case LFUN_MOUSE_TRIPLE:
682 case LFUN_WORD_SELECT:
686 cur.selection() = true;
687 cur.pos() = cur.lastpos();
688 cur.idx() = cur.lastidx();
691 case LFUN_PARAGRAPH_UP:
692 case LFUN_PARAGRAPH_DOWN:
693 cur.updateFlags(Update::Decoration | Update::FitCursor);
694 case LFUN_PARAGRAPH_UP_SELECT:
695 case LFUN_PARAGRAPH_DOWN_SELECT:
698 case LFUN_LINE_BEGIN:
699 case LFUN_WORD_BACKWARD:
701 cur.updateFlags(Update::Decoration | Update::FitCursor);
702 case LFUN_LINE_BEGIN_SELECT:
703 case LFUN_WORD_BACKWARD_SELECT:
704 case LFUN_WORD_LEFT_SELECT:
705 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
706 cmd.action == LFUN_WORD_LEFT_SELECT ||
707 cmd.action == LFUN_LINE_BEGIN_SELECT);
708 cur.macroModeClose();
709 if (cur.pos() != 0) {
711 } else if (cur.col() != 0) {
712 cur.idx() -= cur.col();
714 } else if (cur.idx() != 0) {
718 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
723 case LFUN_WORD_FORWARD:
724 case LFUN_WORD_RIGHT:
726 cur.updateFlags(Update::Decoration | Update::FitCursor);
727 case LFUN_WORD_FORWARD_SELECT:
728 case LFUN_WORD_RIGHT_SELECT:
729 case LFUN_LINE_END_SELECT:
730 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
731 cmd.action == LFUN_WORD_RIGHT_SELECT ||
732 cmd.action == LFUN_LINE_END_SELECT);
733 cur.macroModeClose();
735 if (cur.pos() != cur.lastpos()) {
736 cur.pos() = cur.lastpos();
737 } else if (ncols() && (cur.col() != cur.lastcol())) {
738 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
739 cur.pos() = cur.lastpos();
740 } else if (cur.idx() != cur.lastidx()) {
741 cur.idx() = cur.lastidx();
742 cur.pos() = cur.lastpos();
744 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
749 case LFUN_CELL_FORWARD:
750 cur.updateFlags(Update::Decoration | Update::FitCursor);
751 cur.inset().idxNext(cur);
754 case LFUN_CELL_BACKWARD:
755 cur.updateFlags(Update::Decoration | Update::FitCursor);
756 cur.inset().idxPrev(cur);
759 case LFUN_WORD_DELETE_BACKWARD:
760 case LFUN_CHAR_DELETE_BACKWARD:
762 // May affect external cell:
763 cur.recordUndoInset();
765 cur.recordUndoSelection();
766 // if the inset can not be removed from within, delete it
767 if (!cur.backspace()) {
768 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
769 cur.innerText()->dispatch(cur, cmd);
773 case LFUN_WORD_DELETE_FORWARD:
774 case LFUN_CHAR_DELETE_FORWARD:
775 if (cur.pos() == cur.lastpos())
776 // May affect external cell:
777 cur.recordUndoInset();
779 cur.recordUndoSelection();
780 // if the inset can not be removed from within, delete it
782 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
783 cur.innerText()->dispatch(cur, cmd);
789 cur.clearSelection();
791 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
796 // 'Locks' the math inset. A 'locked' math inset behaves as a unit
797 // that is traversed by a single <CursorLeft>/<CursorRight>.
798 case LFUN_INSET_TOGGLE:
804 case LFUN_SELF_INSERT:
805 if (cmd.argument().size() != 1) {
806 cur.recordUndoSelection();
807 docstring const arg = cmd.argument();
808 if (!interpretString(cur, arg))
812 // Don't record undo steps if we are in macro mode and
813 // cmd.argument is the next character of the macro name.
814 // Otherwise we'll get an invalid cursor if we undo after
815 // the macro was finished and the macro is a known command,
816 // e.g. sqrt. Cursor::macroModeClose replaces in this case
817 // the InsetMathUnknown with name "frac" by an empty
818 // InsetMathFrac -> a pos value > 0 is invalid.
819 // A side effect is that an undo before the macro is finished
820 // undoes the complete macro, not only the last character.
821 if (!cur.inMacroMode())
822 cur.recordUndoSelection();
824 // spacial handling of space. If we insert an inset
825 // via macro mode, we want to put the cursor inside it
826 // if relevant. Think typing "\frac<space>".
827 if (cmd.argument()[0] == ' '
828 && cur.inMacroMode() && cur.macroName() != "\\"
829 && cur.macroModeClose()) {
830 MathAtom const atom = cur.prevAtom();
831 if (atom->asNestInset() && atom->isActive()) {
833 cur.pushBackward(*cur.nextInset());
835 } else if (!interpretChar(cur, cmd.argument()[0])) {
836 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
841 //case LFUN_SERVER_GET_XY:
842 // sprintf(dispatch_buffer, "%d %d",);
845 case LFUN_SERVER_SET_XY: {
846 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
849 istringstream is(to_utf8(cmd.argument()));
851 cur.setScreenPos(x, y);
855 // Special casing for superscript in case of LyX handling
857 case LFUN_ACCENT_CIRCUMFLEX:
858 if (cmd.argument().empty()) {
859 // do superscript if LyX handles
861 cur.recordUndoSelection();
862 script(cur, true, grabAndEraseSelection(cur));
866 case LFUN_ACCENT_UMLAUT:
867 case LFUN_ACCENT_ACUTE:
868 case LFUN_ACCENT_GRAVE:
869 case LFUN_ACCENT_BREVE:
870 case LFUN_ACCENT_DOT:
871 case LFUN_ACCENT_MACRON:
872 case LFUN_ACCENT_CARON:
873 case LFUN_ACCENT_TILDE:
874 case LFUN_ACCENT_CEDILLA:
875 case LFUN_ACCENT_CIRCLE:
876 case LFUN_ACCENT_UNDERDOT:
877 case LFUN_ACCENT_TIE:
878 case LFUN_ACCENT_OGONEK:
879 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
883 case LFUN_FONT_FREE_APPLY:
884 case LFUN_FONT_FREE_UPDATE:
885 handleFont2(cur, cmd.argument());
889 if (currentMode() == TEXT_MODE)
890 handleFont(cur, cmd.argument(), "textbf");
892 handleFont(cur, cmd.argument(), "boldsymbol");
895 if (currentMode() == TEXT_MODE)
896 handleFont(cur, cmd.argument(), "textsf");
898 handleFont(cur, cmd.argument(), "mathsf");
901 if (currentMode() == TEXT_MODE)
902 handleFont(cur, cmd.argument(), "emph");
904 handleFont(cur, cmd.argument(), "mathcal");
906 case LFUN_FONT_ROMAN:
907 if (currentMode() == TEXT_MODE)
908 handleFont(cur, cmd.argument(), "textrm");
910 handleFont(cur, cmd.argument(), "mathrm");
912 case LFUN_FONT_TYPEWRITER:
913 if (currentMode() == TEXT_MODE)
914 handleFont(cur, cmd.argument(), "texttt");
916 handleFont(cur, cmd.argument(), "mathtt");
919 handleFont(cur, cmd.argument(), "mathfrak");
922 if (currentMode() == TEXT_MODE)
923 handleFont(cur, cmd.argument(), "textit");
925 handleFont(cur, cmd.argument(), "mathit");
928 if (currentMode() == TEXT_MODE)
929 // FIXME: should be "noun"
930 handleFont(cur, cmd.argument(), "textsc");
932 handleFont(cur, cmd.argument(), "mathbb");
935 case LFUN_FONT_FREE_APPLY:
936 handleFont(cur, cmd.argument(), "textrm");
939 case LFUN_FONT_DEFAULT:
940 handleFont(cur, cmd.argument(), "textnormal");
943 case LFUN_MATH_MODE: {
945 // ignore math-mode on when already in math mode
946 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
948 cur.macroModeClose();
949 docstring const save_selection = grabAndEraseSelection(cur);
951 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
952 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
954 cur.pushBackward(*cur.nextInset());
955 cur.niceInsert(save_selection);
957 if (currentMode() == Inset::TEXT_MODE) {
958 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
959 cur.message(_("create new math text environment ($...$)"));
961 handleFont(cur, cmd.argument(), "textrm");
962 cur.message(_("entered math text mode (textrm)"));
970 cur.recordUndoSelection();
975 case LFUN_MATH_MATRIX: {
981 idocstringstream is(cmd.argument());
982 is >> m >> n >> v_align >> h_align;
989 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
993 case LFUN_MATH_DELIM: {
995 docstring rs = split(cmd.argument(), ls, ' ');
996 // Reasonable default values
1002 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
1006 case LFUN_MATH_BIGDELIM: {
1007 docstring const lname = from_utf8(cmd.getArg(0));
1008 docstring const ldelim = from_utf8(cmd.getArg(1));
1009 docstring const rname = from_utf8(cmd.getArg(2));
1010 docstring const rdelim = from_utf8(cmd.getArg(3));
1011 latexkeys const * l = in_word_set(lname);
1012 bool const have_l = l && l->inset == "big" &&
1013 InsetMathBig::isBigInsetDelim(ldelim);
1014 l = in_word_set(rname);
1015 bool const have_r = l && l->inset == "big" &&
1016 InsetMathBig::isBigInsetDelim(rdelim);
1017 // We mimic LFUN_MATH_DELIM in case we have an empty left
1018 // or right delimiter.
1019 if (have_l || have_r) {
1021 docstring const selection = grabAndEraseSelection(cur);
1024 cur.insert(MathAtom(new InsetMathBig(lname,
1026 cur.niceInsert(selection);
1028 cur.insert(MathAtom(new InsetMathBig(rname,
1031 // Don't call cur.undispatched() if we did nothing, this would
1032 // lead to infinite recursion via Text::dispatch().
1036 case LFUN_SPACE_INSERT:
1037 case LFUN_MATH_SPACE:
1038 cur.recordUndoSelection();
1039 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
1042 case LFUN_ERT_INSERT:
1043 // interpret this as if a backslash was typed
1045 interpretChar(cur, '\\');
1048 case LFUN_MATH_SUBSCRIPT:
1049 // interpret this as if a _ was typed
1050 cur.recordUndoSelection();
1051 interpretChar(cur, '_');
1054 case LFUN_MATH_SUPERSCRIPT:
1055 // interpret this as if a ^ was typed
1056 cur.recordUndoSelection();
1057 interpretChar(cur, '^');
1060 case LFUN_MATH_MACRO_FOLD:
1061 case LFUN_MATH_MACRO_UNFOLD: {
1063 bool fold = cmd.action == LFUN_MATH_MACRO_FOLD;
1064 bool found = findMacroToFoldUnfold(it, fold);
1066 MathMacro * macro = it.nextInset()->asInsetMath()->asMacro();
1067 cur.recordUndoInset();
1076 case LFUN_QUOTE_INSERT:
1077 // interpret this as if a straight " was typed
1078 cur.recordUndoSelection();
1079 interpretChar(cur, '\"');
1082 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
1083 // handling such that "self-insert" works on "arbitrary stuff" too, and
1084 // math-insert only handles special math things like "matrix".
1085 case LFUN_MATH_INSERT: {
1086 cur.recordUndoSelection();
1087 if (cmd.argument() == "^" || cmd.argument() == "_")
1088 interpretChar(cur, cmd.argument()[0]);
1091 asArray(cmd.argument(), ar);
1092 if (ar.size() == 1 && ar[0]->asNestInset()
1093 && ar[0]->asNestInset()->nargs() > 1)
1094 handleNest(cur, ar[0]);
1096 cur.niceInsert(cmd.argument());
1101 case LFUN_DIALOG_SHOW_NEW_INSET: {
1102 docstring const & name = cmd.argument();
1104 if (name == "ref") {
1105 InsetMathRef tmp(name);
1106 data = tmp.createDialogStr(to_utf8(name));
1108 cur.bv().showDialog(to_utf8(name), data);
1112 case LFUN_INSET_INSERT: {
1114 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1115 cur.recordUndoSelection();
1121 case LFUN_INSET_DISSOLVE:
1122 if (!asHullInset()) {
1123 cur.recordUndoInset();
1129 InsetMath::doDispatch(cur, cmd);
1135 bool InsetMathNest::findMacroToFoldUnfold(Cursor & it, bool fold) const {
1136 // look for macro to open/close, but stay in mathed
1137 for (; !it.empty(); it.pop_back()) {
1139 // go backward through the current cell
1140 Inset * inset = it.nextInset();
1141 while (inset && inset->asInsetMath()) {
1142 MathMacro * macro = inset->asInsetMath()->asMacro();
1144 // found the an macro to open/close?
1145 if (macro->folded() != fold)
1148 // Wrong folding state.
1149 // If this was the first we see in this slice, look further left,
1151 if (inset != it.nextInset())
1155 // go up if this was the left most position
1161 inset = it.nextInset();
1169 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1170 FuncStatus & flag) const
1172 // the font related toggles
1173 //string tc = "mathnormal";
1175 string const arg = to_utf8(cmd.argument());
1176 switch (cmd.action) {
1177 case LFUN_TABULAR_FEATURE:
1178 flag.setEnabled(false);
1181 case LFUN_TABULAR_FEATURE:
1182 // FIXME: check temporarily disabled
1184 char align = mathcursor::valign();
1185 if (align == '\0') {
1189 if (cmd.argument().empty()) {
1193 if (!contains("tcb", cmd.argument()[0])) {
1197 flag.setOnOff(cmd.argument()[0] == align);
1200 /// We have to handle them since 1.4 blocks all unhandled actions
1201 case LFUN_FONT_ITAL:
1202 case LFUN_FONT_BOLD:
1203 case LFUN_FONT_SANS:
1204 case LFUN_FONT_EMPH:
1205 case LFUN_FONT_TYPEWRITER:
1206 case LFUN_FONT_NOUN:
1207 case LFUN_FONT_ROMAN:
1208 case LFUN_FONT_DEFAULT:
1209 flag.setEnabled(true);
1211 case LFUN_MATH_MUTATE:
1212 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1213 flag.setOnOff(false);
1216 // we just need to be in math mode to enable that
1217 case LFUN_MATH_SIZE:
1218 case LFUN_MATH_SPACE:
1219 case LFUN_MATH_LIMITS:
1220 case LFUN_MATH_EXTERN:
1221 flag.setEnabled(true);
1224 case LFUN_FONT_FRAK:
1225 flag.setEnabled(currentMode() != TEXT_MODE);
1228 case LFUN_MATH_INSERT: {
1229 bool const textarg =
1230 arg == "\\textbf" || arg == "\\textsf" ||
1231 arg == "\\textrm" || arg == "\\textmd" ||
1232 arg == "\\textit" || arg == "\\textsc" ||
1233 arg == "\\textsl" || arg == "\\textup" ||
1234 arg == "\\texttt" || arg == "\\textbb" ||
1235 arg == "\\textnormal";
1236 flag.setEnabled(currentMode() != TEXT_MODE || textarg);
1240 case LFUN_MATH_MATRIX:
1241 flag.setEnabled(currentMode() == MATH_MODE);
1244 case LFUN_INSET_INSERT: {
1245 // Don't test createMathInset_fromDialogStr(), since
1246 // getStatus is not called with a valid reference and the
1247 // dialog would not be applyable.
1248 string const name = cmd.getArg(0);
1249 flag.setEnabled(name == "ref");
1253 case LFUN_MATH_DELIM:
1254 case LFUN_MATH_BIGDELIM:
1255 // Don't do this with multi-cell selections
1256 flag.setEnabled(cur.selBegin().idx() == cur.selEnd().idx());
1259 case LFUN_MATH_MACRO_FOLD:
1260 case LFUN_MATH_MACRO_UNFOLD: {
1262 bool found = findMacroToFoldUnfold(it, cmd.action == LFUN_MATH_MACRO_FOLD);
1263 flag.setEnabled(found);
1267 case LFUN_SPECIALCHAR_INSERT:
1268 // FIXME: These would probably make sense in math-text mode
1269 flag.setEnabled(false);
1272 case LFUN_INSET_DISSOLVE:
1273 flag.setEnabled(!asHullInset());
1284 void InsetMathNest::edit(Cursor & cur, bool front, EntryDirection entry_from)
1287 bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_RIGHT ||
1288 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1289 cur.idx() = enter_front ? 0 : cur.lastidx();
1290 cur.pos() = enter_front ? 0 : cur.lastpos();
1292 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1296 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1299 int dist_min = 1000000;
1300 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1301 int const d = cell(i).dist(cur.bv(), x, y);
1307 MathData & ar = cell(idx_min);
1309 cur.idx() = idx_min;
1310 cur.pos() = ar.x2pos(&cur.bv(), x - ar.xo(cur.bv()));
1312 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1313 if (dist_min == 0) {
1315 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1316 if (ar[i]->covers(cur.bv(), x, y))
1317 return ar[i].nucleus()->editXY(cur, x, y);
1323 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1325 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1326 BufferView & bv = cur.bv();
1327 bool do_selection = cmd.button() == mouse_button::button1
1328 && cmd.argument() == "region-select";
1329 bv.mouseSetCursor(cur, do_selection);
1330 if (cmd.button() == mouse_button::button1) {
1331 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1332 // Update the cursor update flags as needed:
1334 // Update::Decoration: tells to update the decoration
1335 // (visual box corners that define
1337 // Update::FitCursor: adjust the screen to the cursor
1338 // position if needed
1339 // cur.result().update(): don't overwrite previously set flags.
1340 cur.updateFlags(Update::Decoration | Update::FitCursor
1341 | cur.result().update());
1342 } else if (cmd.button() == mouse_button::button2) {
1343 if (cap::selection()) {
1344 // See comment in Text::dispatch why we do this
1345 cap::copySelectionToStack();
1346 cmd = FuncRequest(LFUN_PASTE, "0");
1347 doDispatch(bv.cursor(), cmd);
1350 asArray(theSelection().get(), ar);
1351 bv.cursor().insert(ar);
1357 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1359 // only select with button 1
1360 if (cmd.button() == mouse_button::button1) {
1361 Cursor & bvcur = cur.bv().cursor();
1362 if (bvcur.anchor_.hasPart(cur)) {
1363 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1364 bvcur.setCursor(cur);
1365 bvcur.selection() = true;
1366 //lyxerr << "MOTION " << bvcur << endl;
1373 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1375 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1377 if (cmd.button() == mouse_button::button1) {
1378 if (!cur.selection())
1381 Cursor & bvcur = cur.bv().cursor();
1382 bvcur.selection() = true;
1391 bool InsetMathNest::interpretChar(Cursor & cur, char_type c)
1393 //lyxerr << "interpret 2: '" << c << "'" << endl;
1394 docstring save_selection;
1395 if (c == '^' || c == '_')
1396 save_selection = grabAndEraseSelection(cur);
1401 if (cur.inMacroMode()) {
1402 docstring name = cur.macroName();
1404 /// are we currently typing '#1' or '#2' or...?
1405 if (name == "\\#") {
1408 if (n >= 1 && n <= 9)
1409 cur.insert(new MathMacroArgument(n));
1413 // do not finish macro for known * commands
1414 MathWordList const & mwl = mathedWordList();
1415 bool star_macro = c == '*'
1416 && (mwl.find(name.substr(1) + "*") != mwl.end()
1417 || cur.buffer().getMacro(name.substr(1) + "*", cur, true));
1418 if (isAlphaASCII(c) || star_macro) {
1419 cur.activeMacro()->setName(name + docstring(1, c));
1423 // handle 'special char' macros
1428 if (currentMode() == InsetMath::TEXT_MODE)
1429 cur.niceInsert(createInsetMath("textbackslash"));
1431 cur.niceInsert(createInsetMath("backslash"));
1432 } else if (c == '{') {
1434 cur.niceInsert(MathAtom(new InsetMathBrace));
1435 } else if (c == '%') {
1437 cur.niceInsert(MathAtom(new InsetMathComment));
1438 } else if (c == '#') {
1439 LASSERT(cur.activeMacro(), /**/);
1440 cur.activeMacro()->setName(name + docstring(1, c));
1443 cur.niceInsert(createInsetMath(docstring(1, c)));
1448 // One character big delimiters. The others are handled in
1449 // interpretString().
1450 latexkeys const * l = in_word_set(name.substr(1));
1451 if (name[0] == '\\' && l && l->inset == "big") {
1455 delim = from_ascii("\\{");
1458 delim = from_ascii("\\}");
1461 delim = docstring(1, c);
1464 if (InsetMathBig::isBigInsetDelim(delim)) {
1465 // name + delim ared a valid InsetMathBig.
1466 // We can't use cur.macroModeClose() because
1467 // it does not handle delim.
1468 InsetMathUnknown * p = cur.activeMacro();
1471 cur.cell().erase(cur.pos());
1472 cur.plainInsert(MathAtom(
1473 new InsetMathBig(name.substr(1), delim)));
1478 // leave macro mode and try again if necessary
1479 cur.macroModeClose();
1481 cur.niceInsert(MathAtom(new InsetMathBrace));
1483 interpretChar(cur, c);
1487 // This is annoying as one has to press <space> far too often.
1491 // leave autocorrect mode if necessary
1492 if (autocorrect() && c == ' ') {
1493 autocorrect() = false;
1498 // just clear selection on pressing the space bar
1499 if (cur.selection() && c == ' ') {
1500 cur.selection() = false;
1505 //lyxerr << "starting with macro" << endl;
1506 bool reduced = cap::reduceSelectionToOneCell(cur);
1507 if (reduced || !cur.selection()) {
1508 docstring const safe = cap::grabAndEraseSelection(cur);
1509 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), safe, false)));
1517 if (currentMode() == InsetMath::TEXT_MODE)
1523 if (currentMode() == InsetMath::TEXT_MODE) {
1524 // insert spaces in text mode,
1525 // but suppress direct insertion of two spaces in a row
1526 // the still allows typing '<space>a<space>' and deleting the 'a', but
1527 // it is better than nothing...
1528 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1530 // FIXME: we have to enable full redraw here because of the
1531 // visual box corners that define the inset. If we know for
1532 // sure that we stay within the same cell we can optimize for
1534 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1538 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1539 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1540 // FIXME: we have to enable full redraw here because of the
1541 // visual box corners that define the inset. If we know for
1542 // sure that we stay within the same cell we can optimize for
1544 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1548 if (cur.popForward()) {
1549 // FIXME: we have to enable full redraw here because of the
1550 // visual box corners that define the inset. If we know for
1551 // sure that we stay within the same cell we can optimize for
1553 //cur.updateFlags(Update::FitCursor);
1557 // if we are at the very end, leave the formula
1558 return cur.pos() != cur.lastpos();
1561 // These shouldn't work in text mode:
1562 if (currentMode() != InsetMath::TEXT_MODE) {
1564 script(cur, false, save_selection);
1568 script(cur, true, save_selection);
1572 cur.niceInsert(createInsetMath("sim"));
1577 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1578 c == '%' || c == '_' || c == '^') {
1579 cur.niceInsert(createInsetMath(docstring(1, c)));
1584 // try auto-correction
1585 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1588 // no special circumstances, so insert the character without any fuss
1590 cur.autocorrect() = true;
1595 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1597 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1599 if (!cur.empty() && cur.pos() > 0 &&
1600 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1601 if (InsetMathBig::isBigInsetDelim(str)) {
1602 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1603 if (prev[0] == '\\') {
1604 prev = prev.substr(1);
1605 latexkeys const * l = in_word_set(prev);
1606 if (l && l->inset == "big") {
1607 cur.cell()[cur.pos() - 1] =
1608 MathAtom(new InsetMathBig(prev, str));
1618 bool InsetMathNest::script(Cursor & cur, bool up)
1620 return script(cur, up, docstring());
1624 bool InsetMathNest::script(Cursor & cur, bool up,
1625 docstring const & save_selection)
1627 // Hack to get \^ and \_ working
1628 //lyxerr << "handling script: up: " << up << endl;
1629 if (cur.inMacroMode() && cur.macroName() == "\\") {
1631 cur.niceInsert(createInsetMath("mathcircumflex"));
1633 interpretChar(cur, '_');
1637 cur.macroModeClose();
1638 if (asScriptInset() && cur.idx() == 0) {
1639 // we are in a nucleus of a script inset, move to _our_ script
1640 InsetMathScript * inset = asScriptInset();
1641 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1643 cur.idx() = inset->idxOfScript(up);
1645 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1647 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1650 cur.idx() = inset->idxOfScript(up);
1651 cur.pos() = cur.lastpos();
1653 // convert the thing to our left to a scriptinset or create a new
1654 // one if in the very first position of the array
1655 if (cur.pos() == 0) {
1656 //lyxerr << "new scriptinset" << endl;
1657 cur.insert(new InsetMathScript(up));
1659 //lyxerr << "converting prev atom " << endl;
1660 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1663 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1664 // See comment in MathParser.cpp for special handling of {}-bases
1670 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1671 cur.niceInsert(save_selection);
1673 //lyxerr << "inserting selection 2:\n" << save_selection << endl;
1678 bool InsetMathNest::completionSupported(Cursor const & cur) const
1680 return cur.inMacroMode();
1684 bool InsetMathNest::inlineCompletionSupported(Cursor const & cur) const
1686 return cur.inMacroMode();
1690 bool InsetMathNest::automaticInlineCompletion() const
1692 return lyxrc.completion_inline_math;
1696 bool InsetMathNest::automaticPopupCompletion() const
1698 return lyxrc.completion_popup_math;
1702 CompletionList const *
1703 InsetMathNest::createCompletionList(Cursor const & cur) const
1705 if (!cur.inMacroMode())
1708 return new MathCompletionList(cur);
1712 docstring InsetMathNest::completionPrefix(Cursor const & cur) const
1714 if (!cur.inMacroMode())
1717 return cur.activeMacro()->name();
1721 bool InsetMathNest::insertCompletion(Cursor & cur, docstring const & s,
1724 if (!cur.inMacroMode())
1727 // append completion to active macro
1728 InsetMathUnknown * inset = cur.activeMacro();
1729 inset->setName(inset->name() + s);
1734 // FIXME: this creates duplicates in the completion popup
1735 // which looks ugly. Moreover the changes the list lengths
1737 confuse the popup as well.
1738 MathCompletionList::addToFavorites(inset->name());
1740 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, " "));
1747 void InsetMathNest::completionPosAndDim(Cursor const & cur, int & x, int & y,
1748 Dimension & dim) const
1750 Inset const * inset = cur.activeMacro();
1754 // get inset dimensions
1755 dim = cur.bv().coordCache().insets().dim(inset);
1756 // FIXME: these 3 are no accurate, but should depend on the font.
1757 // Now the popup jumps down if you enter a char with descent > 0.
1763 = cur.bv().coordCache().insets().xy(inset);
1769 bool InsetMathNest::cursorMathForward(Cursor & cur)
1771 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
1772 cur.pushBackward(*cur.nextAtom().nucleus());
1773 cur.inset().idxFirst(cur);
1776 if (cur.posForward() || idxForward(cur))
1778 // try to pop forwards --- but don't pop out of math! leave that to
1780 int s = cur.depth() - 2;
1781 if (s >= 0 && cur[s].inset().asInsetMath())
1782 return cur.popForward();
1787 bool InsetMathNest::cursorMathBackward(Cursor & cur)
1789 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
1791 cur.push(*cur.nextAtom().nucleus());
1792 cur.inset().idxLast(cur);
1795 if (cur.posBackward() || idxBackward(cur))
1797 // try to pop backwards --- but don't pop out of math! leave that to
1799 int s = cur.depth() - 2;
1800 if (s >= 0 && cur[s].inset().asInsetMath())
1801 return cur.popBackward();
1806 ////////////////////////////////////////////////////////////////////
1808 MathCompletionList::MathCompletionList(Cursor const & cur)
1810 // fill it with macros from the buffer
1811 MacroNameSet macros;
1812 cur.buffer().listMacroNames(macros);
1813 MacroNameSet::const_iterator it;
1814 for (it = macros.begin(); it != macros.end(); ++it) {
1815 if (cur.buffer().getMacro(*it, cur, false))
1816 locals.push_back("\\" + *it);
1818 sort(locals.begin(), locals.end());
1820 if (globals.size() > 0)
1823 // fill in global macros
1825 MacroTable::globalMacros().getMacroNames(macros);
1826 lyxerr << "Globals completion macros: ";
1827 for (it = macros.begin(); it != macros.end(); ++it) {
1828 lyxerr << "\\" + *it << " ";
1829 globals.push_back("\\" + *it);
1831 lyxerr << std::endl;
1833 // fill in global commands
1834 globals.push_back(from_ascii("\\boxed"));
1835 globals.push_back(from_ascii("\\fbox"));
1836 globals.push_back(from_ascii("\\framebox"));
1837 globals.push_back(from_ascii("\\makebox"));
1838 globals.push_back(from_ascii("\\kern"));
1839 globals.push_back(from_ascii("\\xrightarrow"));
1840 globals.push_back(from_ascii("\\xleftarrow"));
1841 globals.push_back(from_ascii("\\split"));
1842 globals.push_back(from_ascii("\\gathered"));
1843 globals.push_back(from_ascii("\\aligned"));
1844 globals.push_back(from_ascii("\\alignedat"));
1845 globals.push_back(from_ascii("\\cases"));
1846 globals.push_back(from_ascii("\\substack"));
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;