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 bool textmode = os.textMode();
340 os.textMode(currentMode() == TEXT_MODE);
341 docstring const latex_name = name().c_str();
342 os << '\\' << latex_name.c_str();
343 for (size_t i = 0; i < nargs(); ++i)
344 os << '{' << cell(i) << '}';
346 os.pendingSpace(true);
347 if (lock_ && !os.latex()) {
349 os.pendingSpace(true);
351 os.textMode(textmode);
355 void InsetMathNest::normalize(NormalStream & os) const
357 os << '[' << name().c_str();
358 for (size_t i = 0; i < nargs(); ++i)
359 os << ' ' << cell(i);
364 int InsetMathNest::latex(odocstream & os, OutputParams const & runparams) const
366 WriteStream wi(os, runparams.moving_arg, true, runparams.dryrun);
372 bool InsetMathNest::setMouseHover(bool mouse_hover)
374 mouse_hover_ = mouse_hover;
379 bool InsetMathNest::notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
383 MathData & ar = cur.cell();
384 // remove base-only "scripts"
385 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
386 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
387 if (p && p->nargs() == 1) {
388 MathData ar = p->nuc();
391 cur.adjust(i, ar.size() - 1);
395 // glue adjacent font insets of the same kind
396 for (pos_type i = 0; i + 1 < size(); ++i) {
397 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
398 InsetMathFont const * q = operator[](i + 1)->asFontInset();
399 if (p && q && p->name() == q->name()) {
400 p->cell(0).append(q->cell(0));
410 void InsetMathNest::handleFont
411 (Cursor & cur, docstring const & arg, char const * const font)
413 handleFont(cur, arg, from_ascii(font));
417 void InsetMathNest::handleFont(Cursor & cur, docstring const & arg,
418 docstring const & font)
420 cur.recordUndoSelection();
422 // this whole function is a hack and won't work for incremental font
424 if (cur.inset().asInsetMath()->name() == font)
425 cur.handleFont(to_utf8(font));
427 handleNest(cur, createInsetMath(font), arg);
431 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest)
433 handleNest(cur, nest, docstring());
437 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest,
438 docstring const & arg)
440 CursorSlice i1 = cur.selBegin();
441 CursorSlice i2 = cur.selEnd();
442 if (!i1.inset().asInsetMath())
444 if (i1.idx() == i2.idx()) {
445 // the easy case where only one cell is selected
446 cur.handleNest(nest);
451 // multiple selected cells in a simple non-grid inset
452 if (i1.asInsetMath()->nrows() == 0 || i1.asInsetMath()->ncols() == 0) {
453 for (idx_type i = i1.idx(); i <= i2.idx(); ++i) {
458 cur.pos() = cur.lastpos();
461 // change font of cell
462 cur.handleNest(nest);
465 // cur is in the font inset now. If the loop continues,
466 // we need to get outside again for the next cell
467 if (i + 1 <= i2.idx())
473 // the complicated case with multiple selected cells in a grid
476 cap::region(i1, i2, r1, r2, c1, c2);
477 for (row_type row = r1; row <= r2; ++row) {
478 for (col_type col = c1; col <= c2; ++col) {
480 cur.idx() = i1.asInsetMath()->index(row, col);
483 cur.pos() = cur.lastpos();
487 cur.handleNest(nest);
490 // cur is in the font inset now. If the loop continues,
491 // we need to get outside again for the next cell
492 if (col + 1 <= c2 || row + 1 <= r2)
499 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
501 cur.recordUndoSelection();
504 font.fromString(to_utf8(arg), b);
505 if (font.fontInfo().color() != Color_inherit &&
506 font.fontInfo().color() != Color_ignore)
507 handleNest(cur, MathAtom(new InsetMathColor(true, font.fontInfo().color())));
509 // FIXME: support other font changes here as well?
513 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
515 //lyxerr << "InsetMathNest: request: " << cmd << endl;
517 switch (cmd.action) {
520 cur.recordUndoSelection();
521 cur.message(_("Paste"));
522 replaceSelection(cur);
524 if (cmd.argument().empty() && !theClipboard().isInternal())
525 topaste = theClipboard().getAsText();
528 idocstringstream is(cmd.argument());
530 topaste = cap::selection(n);
532 cur.niceInsert(topaste);
533 cur.clearSelection(); // bug 393
540 cutSelection(cur, true, true);
541 cur.message(_("Cut"));
542 // Prevent stale position >= size crash
543 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
549 cur.message(_("Copy"));
552 case LFUN_MOUSE_PRESS:
553 lfunMousePress(cur, cmd);
556 case LFUN_MOUSE_MOTION:
557 lfunMouseMotion(cur, cmd);
560 case LFUN_MOUSE_RELEASE:
561 lfunMouseRelease(cur, cmd);
564 case LFUN_FINISHED_LEFT: // in math, left is backwards
565 case LFUN_FINISHED_BACKWARD:
566 cur.bv().cursor() = cur;
569 case LFUN_FINISHED_RIGHT: // in math, right is forward
570 case LFUN_FINISHED_FORWARD:
572 cur.bv().cursor() = cur;
575 case LFUN_CHAR_RIGHT:
577 case LFUN_CHAR_BACKWARD:
578 case LFUN_CHAR_FORWARD:
579 cur.updateFlags(Update::Decoration | Update::FitCursor);
580 case LFUN_CHAR_RIGHT_SELECT:
581 case LFUN_CHAR_LEFT_SELECT:
582 case LFUN_CHAR_BACKWARD_SELECT:
583 case LFUN_CHAR_FORWARD_SELECT: {
584 // are we in a selection?
585 bool select = (cmd.action == LFUN_CHAR_RIGHT_SELECT
586 || cmd.action == LFUN_CHAR_LEFT_SELECT
587 || cmd.action == LFUN_CHAR_BACKWARD_SELECT
588 || cmd.action == LFUN_CHAR_FORWARD_SELECT);
589 // are we moving forward or backwards?
590 // If the command was RIGHT or LEFT, then whether we're moving forward
591 // or backwards depends on the cursor movement mode (logical or visual):
592 // * in visual mode, since math is always LTR, right -> forward,
594 // * in logical mode, the mapping is determined by the
595 // reverseDirectionNeeded() function
598 FuncCode finish_lfun;
600 if (cmd.action == LFUN_CHAR_FORWARD
601 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
603 finish_lfun = LFUN_FINISHED_FORWARD;
605 else if (cmd.action == LFUN_CHAR_BACKWARD
606 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
608 finish_lfun = LFUN_FINISHED_BACKWARD;
611 bool right = (cmd.action == LFUN_CHAR_RIGHT_SELECT
612 || cmd.action == LFUN_CHAR_RIGHT);
613 if (lyxrc.visual_cursor || !reverseDirectionNeeded(cur))
619 finish_lfun = LFUN_FINISHED_RIGHT;
621 finish_lfun = LFUN_FINISHED_LEFT;
623 // Now that we know exactly what we want to do, let's do it!
624 cur.selHandle(select);
625 cur.autocorrect() = false;
627 cur.macroModeClose();
628 // try moving forward or backwards as necessary...
629 if (!(forward ? cursorMathForward(cur) : cursorMathBackward(cur))) {
630 // ... and if movement failed, then finish forward or backwards
632 cmd = FuncRequest(finish_lfun);
640 cur.updateFlags(Update::Decoration | Update::FitCursor);
641 case LFUN_DOWN_SELECT:
642 case LFUN_UP_SELECT: {
643 // close active macro
644 if (cur.inMacroMode()) {
645 cur.macroModeClose();
649 // stop/start the selection
650 bool select = cmd.action == LFUN_DOWN_SELECT ||
651 cmd.action == LFUN_UP_SELECT;
652 cur.selHandle(select);
655 bool up = cmd.action == LFUN_UP || cmd.action == LFUN_UP_SELECT;
656 bool successful = cur.upDownInMath(up);
660 if (cur.fixIfBroken())
661 // FIXME: Something bad happened. We pass the corrected Cursor
662 // instead of letting things go worse.
665 // We did not manage to move the cursor.
670 case LFUN_MOUSE_DOUBLE:
671 case LFUN_MOUSE_TRIPLE:
672 case LFUN_WORD_SELECT:
676 cur.selection() = true;
677 cur.pos() = cur.lastpos();
678 cur.idx() = cur.lastidx();
681 case LFUN_PARAGRAPH_UP:
682 case LFUN_PARAGRAPH_DOWN:
683 cur.updateFlags(Update::Decoration | Update::FitCursor);
684 case LFUN_PARAGRAPH_UP_SELECT:
685 case LFUN_PARAGRAPH_DOWN_SELECT:
688 case LFUN_LINE_BEGIN:
689 case LFUN_WORD_BACKWARD:
691 cur.updateFlags(Update::Decoration | Update::FitCursor);
692 case LFUN_LINE_BEGIN_SELECT:
693 case LFUN_WORD_BACKWARD_SELECT:
694 case LFUN_WORD_LEFT_SELECT:
695 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
696 cmd.action == LFUN_WORD_LEFT_SELECT ||
697 cmd.action == LFUN_LINE_BEGIN_SELECT);
698 cur.macroModeClose();
699 if (cur.pos() != 0) {
701 } else if (cur.col() != 0) {
702 cur.idx() -= cur.col();
704 } else if (cur.idx() != 0) {
708 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
713 case LFUN_WORD_FORWARD:
714 case LFUN_WORD_RIGHT:
716 cur.updateFlags(Update::Decoration | Update::FitCursor);
717 case LFUN_WORD_FORWARD_SELECT:
718 case LFUN_WORD_RIGHT_SELECT:
719 case LFUN_LINE_END_SELECT:
720 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
721 cmd.action == LFUN_WORD_RIGHT_SELECT ||
722 cmd.action == LFUN_LINE_END_SELECT);
723 cur.macroModeClose();
725 if (cur.pos() != cur.lastpos()) {
726 cur.pos() = cur.lastpos();
727 } else if (ncols() && (cur.col() != cur.lastcol())) {
728 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
729 cur.pos() = cur.lastpos();
730 } else if (cur.idx() != cur.lastidx()) {
731 cur.idx() = cur.lastidx();
732 cur.pos() = cur.lastpos();
734 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
739 case LFUN_CELL_FORWARD:
740 cur.updateFlags(Update::Decoration | Update::FitCursor);
741 cur.inset().idxNext(cur);
744 case LFUN_CELL_BACKWARD:
745 cur.updateFlags(Update::Decoration | Update::FitCursor);
746 cur.inset().idxPrev(cur);
749 case LFUN_WORD_DELETE_BACKWARD:
750 case LFUN_CHAR_DELETE_BACKWARD:
752 // May affect external cell:
753 cur.recordUndoInset();
755 cur.recordUndoSelection();
756 // if the inset can not be removed from within, delete it
757 if (!cur.backspace()) {
758 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
759 cur.innerText()->dispatch(cur, cmd);
763 case LFUN_WORD_DELETE_FORWARD:
764 case LFUN_CHAR_DELETE_FORWARD:
765 if (cur.pos() == cur.lastpos())
766 // May affect external cell:
767 cur.recordUndoInset();
769 cur.recordUndoSelection();
770 // if the inset can not be removed from within, delete it
772 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
773 cur.innerText()->dispatch(cur, cmd);
779 cur.clearSelection();
781 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
786 // 'Locks' the math inset. A 'locked' math inset behaves as a unit
787 // that is traversed by a single <CursorLeft>/<CursorRight>.
788 case LFUN_INSET_TOGGLE:
794 case LFUN_SELF_INSERT:
795 if (cmd.argument().size() != 1) {
796 cur.recordUndoSelection();
797 docstring const arg = cmd.argument();
798 if (!interpretString(cur, arg))
802 // Don't record undo steps if we are in macro mode and
803 // cmd.argument is the next character of the macro name.
804 // Otherwise we'll get an invalid cursor if we undo after
805 // the macro was finished and the macro is a known command,
806 // e.g. sqrt. Cursor::macroModeClose replaces in this case
807 // the InsetMathUnknown with name "frac" by an empty
808 // InsetMathFrac -> a pos value > 0 is invalid.
809 // A side effect is that an undo before the macro is finished
810 // undoes the complete macro, not only the last character.
811 if (!cur.inMacroMode())
812 cur.recordUndoSelection();
814 // spacial handling of space. If we insert an inset
815 // via macro mode, we want to put the cursor inside it
816 // if relevant. Think typing "\frac<space>".
817 if (cmd.argument()[0] == ' '
818 && cur.inMacroMode() && cur.macroName() != "\\"
819 && cur.macroModeClose()) {
820 MathAtom const atom = cur.prevAtom();
821 if (atom->asNestInset() && atom->isActive()) {
823 cur.pushBackward(*cur.nextInset());
825 } else if (!interpretChar(cur, cmd.argument()[0])) {
826 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
831 //case LFUN_SERVER_GET_XY:
832 // sprintf(dispatch_buffer, "%d %d",);
835 case LFUN_SERVER_SET_XY: {
836 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
839 istringstream is(to_utf8(cmd.argument()));
841 cur.setScreenPos(x, y);
845 // Special casing for superscript in case of LyX handling
847 case LFUN_ACCENT_CIRCUMFLEX:
848 if (cmd.argument().empty()) {
849 // do superscript if LyX handles
851 cur.recordUndoSelection();
852 script(cur, true, grabAndEraseSelection(cur));
856 case LFUN_ACCENT_UMLAUT:
857 case LFUN_ACCENT_ACUTE:
858 case LFUN_ACCENT_GRAVE:
859 case LFUN_ACCENT_BREVE:
860 case LFUN_ACCENT_DOT:
861 case LFUN_ACCENT_MACRON:
862 case LFUN_ACCENT_CARON:
863 case LFUN_ACCENT_TILDE:
864 case LFUN_ACCENT_CEDILLA:
865 case LFUN_ACCENT_CIRCLE:
866 case LFUN_ACCENT_UNDERDOT:
867 case LFUN_ACCENT_TIE:
868 case LFUN_ACCENT_OGONEK:
869 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
873 case LFUN_TEXTSTYLE_APPLY:
874 case LFUN_TEXTSTYLE_UPDATE:
875 handleFont2(cur, cmd.argument());
879 if (currentMode() == TEXT_MODE)
880 handleFont(cur, cmd.argument(), "textbf");
882 handleFont(cur, cmd.argument(), "boldsymbol");
885 if (currentMode() == TEXT_MODE)
886 handleFont(cur, cmd.argument(), "textsf");
888 handleFont(cur, cmd.argument(), "mathsf");
891 if (currentMode() == TEXT_MODE)
892 handleFont(cur, cmd.argument(), "emph");
894 handleFont(cur, cmd.argument(), "mathcal");
896 case LFUN_FONT_ROMAN:
897 if (currentMode() == TEXT_MODE)
898 handleFont(cur, cmd.argument(), "textrm");
900 handleFont(cur, cmd.argument(), "mathrm");
902 case LFUN_FONT_TYPEWRITER:
903 if (currentMode() == TEXT_MODE)
904 handleFont(cur, cmd.argument(), "texttt");
906 handleFont(cur, cmd.argument(), "mathtt");
909 handleFont(cur, cmd.argument(), "mathfrak");
912 if (currentMode() == TEXT_MODE)
913 handleFont(cur, cmd.argument(), "textit");
915 handleFont(cur, cmd.argument(), "mathit");
918 if (currentMode() == TEXT_MODE)
919 // FIXME: should be "noun"
920 handleFont(cur, cmd.argument(), "textsc");
922 handleFont(cur, cmd.argument(), "mathbb");
924 case LFUN_FONT_DEFAULT:
925 handleFont(cur, cmd.argument(), "textnormal");
928 case LFUN_MATH_MODE: {
930 // ignore math-mode on when already in math mode
931 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
933 cur.macroModeClose();
934 docstring const save_selection = grabAndEraseSelection(cur);
936 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
937 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
939 cur.pushBackward(*cur.nextInset());
940 cur.niceInsert(save_selection);
942 if (currentMode() == Inset::TEXT_MODE) {
943 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
944 cur.message(_("create new math text environment ($...$)"));
946 handleFont(cur, cmd.argument(), "textrm");
947 cur.message(_("entered math text mode (textrm)"));
953 case LFUN_MATH_SIZE: {
954 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, cmd.argument());
959 case LFUN_MATH_MATRIX: {
965 idocstringstream is(cmd.argument());
966 is >> m >> n >> v_align >> h_align;
973 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
977 case LFUN_MATH_DELIM: {
979 docstring rs = split(cmd.argument(), ls, ' ');
980 // Reasonable default values
986 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
990 case LFUN_MATH_BIGDELIM: {
991 docstring const lname = from_utf8(cmd.getArg(0));
992 docstring const ldelim = from_utf8(cmd.getArg(1));
993 docstring const rname = from_utf8(cmd.getArg(2));
994 docstring const rdelim = from_utf8(cmd.getArg(3));
995 latexkeys const * l = in_word_set(lname);
996 bool const have_l = l && l->inset == "big" &&
997 InsetMathBig::isBigInsetDelim(ldelim);
998 l = in_word_set(rname);
999 bool const have_r = l && l->inset == "big" &&
1000 InsetMathBig::isBigInsetDelim(rdelim);
1001 // We mimic LFUN_MATH_DELIM in case we have an empty left
1002 // or right delimiter.
1003 if (have_l || have_r) {
1005 docstring const selection = grabAndEraseSelection(cur);
1008 cur.insert(MathAtom(new InsetMathBig(lname,
1010 cur.niceInsert(selection);
1012 cur.insert(MathAtom(new InsetMathBig(rname,
1015 // Don't call cur.undispatched() if we did nothing, this would
1016 // lead to infinite recursion via Text::dispatch().
1020 case LFUN_SPACE_INSERT:
1021 cur.recordUndoSelection();
1022 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
1025 case LFUN_MATH_SPACE:
1026 cur.recordUndoSelection();
1027 if (cmd.argument().empty())
1028 cur.insert(MathAtom(new InsetMathSpace(from_ascii(","))));
1030 cur.insert(MathAtom(new InsetMathSpace(cmd.argument())));
1033 case LFUN_ERT_INSERT:
1034 // interpret this as if a backslash was typed
1036 interpretChar(cur, '\\');
1039 case LFUN_MATH_SUBSCRIPT:
1040 // interpret this as if a _ was typed
1041 cur.recordUndoSelection();
1042 interpretChar(cur, '_');
1045 case LFUN_MATH_SUPERSCRIPT:
1046 // interpret this as if a ^ was typed
1047 cur.recordUndoSelection();
1048 interpretChar(cur, '^');
1051 case LFUN_MATH_MACRO_FOLD:
1052 case LFUN_MATH_MACRO_UNFOLD: {
1054 bool fold = cmd.action == LFUN_MATH_MACRO_FOLD;
1055 bool found = findMacroToFoldUnfold(it, fold);
1057 MathMacro * macro = it.nextInset()->asInsetMath()->asMacro();
1058 cur.recordUndoInset();
1067 case LFUN_QUOTE_INSERT:
1068 // interpret this as if a straight " was typed
1069 cur.recordUndoSelection();
1070 interpretChar(cur, '\"');
1073 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
1074 // handling such that "self-insert" works on "arbitrary stuff" too, and
1075 // math-insert only handles special math things like "matrix".
1076 case LFUN_MATH_INSERT: {
1077 cur.recordUndoSelection();
1078 if (cmd.argument() == "^" || cmd.argument() == "_")
1079 interpretChar(cur, cmd.argument()[0]);
1082 asArray(cmd.argument(), ar);
1083 if (ar.size() == 1 && ar[0]->asNestInset()
1084 && ar[0]->asNestInset()->nargs() > 1)
1085 handleNest(cur, ar[0]);
1087 cur.niceInsert(cmd.argument());
1092 case LFUN_DIALOG_SHOW_NEW_INSET: {
1093 docstring const & name = cmd.argument();
1095 if (name == "ref") {
1096 InsetMathRef tmp(name);
1097 data = tmp.createDialogStr(to_utf8(name));
1099 cur.bv().showDialog(to_utf8(name), data);
1103 case LFUN_INSET_INSERT: {
1105 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1106 cur.recordUndoSelection();
1112 case LFUN_INSET_DISSOLVE:
1113 if (!asHullInset()) {
1114 cur.recordUndoInset();
1120 InsetMath::doDispatch(cur, cmd);
1126 bool InsetMathNest::findMacroToFoldUnfold(Cursor & it, bool fold) const {
1127 // look for macro to open/close, but stay in mathed
1128 for (; !it.empty(); it.pop_back()) {
1130 // go backward through the current cell
1131 Inset * inset = it.nextInset();
1132 while (inset && inset->asInsetMath()) {
1133 MathMacro * macro = inset->asInsetMath()->asMacro();
1135 // found the an macro to open/close?
1136 if (macro->folded() != fold)
1139 // Wrong folding state.
1140 // If this was the first we see in this slice, look further left,
1142 if (inset != it.nextInset())
1146 // go up if this was the left most position
1152 inset = it.nextInset();
1160 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1161 FuncStatus & flag) const
1163 // the font related toggles
1164 //string tc = "mathnormal";
1166 string const arg = to_utf8(cmd.argument());
1167 switch (cmd.action) {
1168 case LFUN_TABULAR_FEATURE:
1169 flag.setEnabled(false);
1172 case LFUN_TABULAR_FEATURE:
1173 // FIXME: check temporarily disabled
1175 char align = mathcursor::valign();
1176 if (align == '\0') {
1180 if (cmd.argument().empty()) {
1184 if (!contains("tcb", cmd.argument()[0])) {
1188 flag.setOnOff(cmd.argument()[0] == align);
1191 /// We have to handle them since 1.4 blocks all unhandled actions
1192 case LFUN_FONT_ITAL:
1193 case LFUN_FONT_BOLD:
1194 case LFUN_FONT_SANS:
1195 case LFUN_FONT_EMPH:
1196 case LFUN_FONT_TYPEWRITER:
1197 case LFUN_FONT_NOUN:
1198 case LFUN_FONT_ROMAN:
1199 case LFUN_FONT_DEFAULT:
1200 flag.setEnabled(true);
1202 case LFUN_MATH_MUTATE:
1203 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1204 flag.setOnOff(false);
1207 // we just need to be in math mode to enable that
1208 case LFUN_MATH_SIZE:
1209 case LFUN_MATH_SPACE:
1210 case LFUN_MATH_LIMITS:
1211 case LFUN_MATH_EXTERN:
1212 flag.setEnabled(true);
1215 case LFUN_FONT_FRAK:
1216 flag.setEnabled(currentMode() != TEXT_MODE);
1219 case LFUN_MATH_INSERT: {
1220 bool const textarg =
1221 arg == "\\textbf" || arg == "\\textsf" ||
1222 arg == "\\textrm" || arg == "\\textmd" ||
1223 arg == "\\textit" || arg == "\\textsc" ||
1224 arg == "\\textsl" || arg == "\\textup" ||
1225 arg == "\\texttt" || arg == "\\textbb" ||
1226 arg == "\\textnormal";
1227 flag.setEnabled(currentMode() != TEXT_MODE || textarg);
1231 case LFUN_MATH_MATRIX:
1232 flag.setEnabled(currentMode() == MATH_MODE);
1235 case LFUN_INSET_INSERT: {
1236 // Don't test createMathInset_fromDialogStr(), since
1237 // getStatus is not called with a valid reference and the
1238 // dialog would not be applyable.
1239 string const name = cmd.getArg(0);
1240 flag.setEnabled(name == "ref");
1244 case LFUN_MATH_DELIM:
1245 case LFUN_MATH_BIGDELIM:
1246 // Don't do this with multi-cell selections
1247 flag.setEnabled(cur.selBegin().idx() == cur.selEnd().idx());
1250 case LFUN_MATH_MACRO_FOLD:
1251 case LFUN_MATH_MACRO_UNFOLD: {
1253 bool found = findMacroToFoldUnfold(it, cmd.action == LFUN_MATH_MACRO_FOLD);
1254 flag.setEnabled(found);
1258 case LFUN_SPECIALCHAR_INSERT:
1259 // FIXME: These would probably make sense in math-text mode
1260 flag.setEnabled(false);
1263 case LFUN_INSET_DISSOLVE:
1264 flag.setEnabled(!asHullInset());
1275 void InsetMathNest::edit(Cursor & cur, bool front, EntryDirection entry_from)
1278 bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_RIGHT ||
1279 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1280 cur.idx() = enter_front ? 0 : cur.lastidx();
1281 cur.pos() = enter_front ? 0 : cur.lastpos();
1283 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1287 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1290 int dist_min = 1000000;
1291 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1292 int const d = cell(i).dist(cur.bv(), x, y);
1298 MathData & ar = cell(idx_min);
1300 cur.idx() = idx_min;
1301 cur.pos() = ar.x2pos(&cur.bv(), x - ar.xo(cur.bv()));
1303 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1304 if (dist_min == 0) {
1306 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1307 if (ar[i]->covers(cur.bv(), x, y))
1308 return ar[i].nucleus()->editXY(cur, x, y);
1314 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1316 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1317 BufferView & bv = cur.bv();
1318 bool do_selection = cmd.button() == mouse_button::button1
1319 && cmd.argument() == "region-select";
1320 bv.mouseSetCursor(cur, do_selection);
1321 if (cmd.button() == mouse_button::button1) {
1322 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1323 // Update the cursor update flags as needed:
1325 // Update::Decoration: tells to update the decoration
1326 // (visual box corners that define
1328 // Update::FitCursor: adjust the screen to the cursor
1329 // position if needed
1330 // cur.result().update(): don't overwrite previously set flags.
1331 cur.updateFlags(Update::Decoration | Update::FitCursor
1332 | cur.result().update());
1333 } else if (cmd.button() == mouse_button::button2) {
1334 if (cap::selection()) {
1335 // See comment in Text::dispatch why we do this
1336 cap::copySelectionToStack();
1337 cmd = FuncRequest(LFUN_PASTE, "0");
1338 doDispatch(bv.cursor(), cmd);
1341 asArray(theSelection().get(), ar);
1342 bv.cursor().insert(ar);
1348 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1350 // only select with button 1
1351 if (cmd.button() == mouse_button::button1) {
1352 Cursor & bvcur = cur.bv().cursor();
1353 if (bvcur.anchor_.hasPart(cur)) {
1354 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1355 bvcur.setCursor(cur);
1356 bvcur.selection() = true;
1357 //lyxerr << "MOTION " << bvcur << endl;
1364 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1366 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1368 if (cmd.button() == mouse_button::button1) {
1369 if (!cur.selection())
1372 Cursor & bvcur = cur.bv().cursor();
1373 bvcur.selection() = true;
1382 bool InsetMathNest::interpretChar(Cursor & cur, char_type c)
1384 //lyxerr << "interpret 2: '" << c << "'" << endl;
1385 docstring save_selection;
1386 if (c == '^' || c == '_')
1387 save_selection = grabAndEraseSelection(cur);
1392 if (cur.inMacroMode()) {
1393 docstring name = cur.macroName();
1395 /// are we currently typing '#1' or '#2' or...?
1396 if (name == "\\#") {
1399 if (n >= 1 && n <= 9)
1400 cur.insert(new MathMacroArgument(n));
1404 // do not finish macro for known * commands
1405 MathWordList const & mwl = mathedWordList();
1406 bool star_macro = c == '*'
1407 && (mwl.find(name.substr(1) + "*") != mwl.end()
1408 || cur.buffer().getMacro(name.substr(1) + "*", cur, true));
1409 if (isAlphaASCII(c) || star_macro) {
1410 cur.activeMacro()->setName(name + docstring(1, c));
1414 // handle 'special char' macros
1419 if (currentMode() == InsetMath::TEXT_MODE)
1420 cur.niceInsert(createInsetMath("textbackslash"));
1422 cur.niceInsert(createInsetMath("backslash"));
1423 } else if (c == '{') {
1425 cur.niceInsert(MathAtom(new InsetMathBrace));
1426 } else if (c == '%') {
1428 cur.niceInsert(MathAtom(new InsetMathComment));
1429 } else if (c == '#') {
1430 LASSERT(cur.activeMacro(), /**/);
1431 cur.activeMacro()->setName(name + docstring(1, c));
1434 cur.niceInsert(createInsetMath(docstring(1, c)));
1439 // One character big delimiters. The others are handled in
1440 // interpretString().
1441 latexkeys const * l = in_word_set(name.substr(1));
1442 if (name[0] == '\\' && l && l->inset == "big") {
1446 delim = from_ascii("\\{");
1449 delim = from_ascii("\\}");
1452 delim = docstring(1, c);
1455 if (InsetMathBig::isBigInsetDelim(delim)) {
1456 // name + delim ared a valid InsetMathBig.
1457 // We can't use cur.macroModeClose() because
1458 // it does not handle delim.
1459 InsetMathUnknown * p = cur.activeMacro();
1462 cur.cell().erase(cur.pos());
1463 cur.plainInsert(MathAtom(
1464 new InsetMathBig(name.substr(1), delim)));
1469 // leave macro mode and try again if necessary
1470 cur.macroModeClose();
1472 cur.niceInsert(MathAtom(new InsetMathBrace));
1474 interpretChar(cur, c);
1478 // This is annoying as one has to press <space> far too often.
1482 // leave autocorrect mode if necessary
1483 if (autocorrect() && c == ' ') {
1484 autocorrect() = false;
1489 // just clear selection on pressing the space bar
1490 if (cur.selection() && c == ' ') {
1491 cur.selection() = false;
1496 //lyxerr << "starting with macro" << endl;
1497 bool reduced = cap::reduceSelectionToOneCell(cur);
1498 if (reduced || !cur.selection()) {
1499 docstring const safe = cap::grabAndEraseSelection(cur);
1500 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), safe, false)));
1508 if (currentMode() == InsetMath::TEXT_MODE)
1514 if (currentMode() == InsetMath::TEXT_MODE) {
1515 // insert spaces in text mode,
1516 // but suppress direct insertion of two spaces in a row
1517 // the still allows typing '<space>a<space>' and deleting the 'a', but
1518 // it is better than nothing...
1519 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1521 // FIXME: we have to enable full redraw here because of the
1522 // visual box corners that define the inset. If we know for
1523 // sure that we stay within the same cell we can optimize for
1525 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1529 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1530 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1531 // FIXME: we have to enable full redraw here because of the
1532 // visual box corners that define the inset. If we know for
1533 // sure that we stay within the same cell we can optimize for
1535 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1539 if (cur.popForward()) {
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::FitCursor);
1548 // if we are at the very end, leave the formula
1549 return cur.pos() != cur.lastpos();
1552 // These shouldn't work in text mode:
1553 if (currentMode() != InsetMath::TEXT_MODE) {
1555 script(cur, false, save_selection);
1559 script(cur, true, save_selection);
1563 cur.niceInsert(createInsetMath("sim"));
1568 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1569 c == '%' || c == '_' || c == '^') {
1570 cur.niceInsert(createInsetMath(docstring(1, c)));
1575 // try auto-correction
1576 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1579 // no special circumstances, so insert the character without any fuss
1581 cur.autocorrect() = true;
1586 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1588 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1590 if (!cur.empty() && cur.pos() > 0 &&
1591 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1592 if (InsetMathBig::isBigInsetDelim(str)) {
1593 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1594 if (prev[0] == '\\') {
1595 prev = prev.substr(1);
1596 latexkeys const * l = in_word_set(prev);
1597 if (l && l->inset == "big") {
1598 cur.cell()[cur.pos() - 1] =
1599 MathAtom(new InsetMathBig(prev, str));
1609 bool InsetMathNest::script(Cursor & cur, bool up)
1611 return script(cur, up, docstring());
1615 bool InsetMathNest::script(Cursor & cur, bool up,
1616 docstring const & save_selection)
1618 // Hack to get \^ and \_ working
1619 //lyxerr << "handling script: up: " << up << endl;
1620 if (cur.inMacroMode() && cur.macroName() == "\\") {
1622 cur.niceInsert(createInsetMath("mathcircumflex"));
1624 interpretChar(cur, '_');
1628 cur.macroModeClose();
1629 if (asScriptInset() && cur.idx() == 0) {
1630 // we are in a nucleus of a script inset, move to _our_ script
1631 InsetMathScript * inset = asScriptInset();
1632 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1634 cur.idx() = inset->idxOfScript(up);
1636 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1638 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1641 cur.idx() = inset->idxOfScript(up);
1642 cur.pos() = cur.lastpos();
1644 // convert the thing to our left to a scriptinset or create a new
1645 // one if in the very first position of the array
1646 if (cur.pos() == 0) {
1647 //lyxerr << "new scriptinset" << endl;
1648 cur.insert(new InsetMathScript(up));
1650 //lyxerr << "converting prev atom " << endl;
1651 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1654 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1655 // See comment in MathParser.cpp for special handling of {}-bases
1661 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1662 cur.niceInsert(save_selection);
1664 //lyxerr << "inserting selection 2:\n" << save_selection << endl;
1669 bool InsetMathNest::completionSupported(Cursor const & cur) const
1671 return cur.inMacroMode();
1675 bool InsetMathNest::inlineCompletionSupported(Cursor const & cur) const
1677 return cur.inMacroMode();
1681 bool InsetMathNest::automaticInlineCompletion() const
1683 return lyxrc.completion_inline_math;
1687 bool InsetMathNest::automaticPopupCompletion() const
1689 return lyxrc.completion_popup_math;
1693 CompletionList const *
1694 InsetMathNest::createCompletionList(Cursor const & cur) const
1696 if (!cur.inMacroMode())
1699 return new MathCompletionList(cur);
1703 docstring InsetMathNest::completionPrefix(Cursor const & cur) const
1705 if (!cur.inMacroMode())
1708 return cur.activeMacro()->name();
1712 bool InsetMathNest::insertCompletion(Cursor & cur, docstring const & s,
1715 if (!cur.inMacroMode())
1718 // append completion to active macro
1719 InsetMathUnknown * inset = cur.activeMacro();
1720 inset->setName(inset->name() + s);
1725 // FIXME: this creates duplicates in the completion popup
1726 // which looks ugly. Moreover the changes the list lengths
1728 confuse the popup as well.
1729 MathCompletionList::addToFavorites(inset->name());
1731 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, " "));
1738 void InsetMathNest::completionPosAndDim(Cursor const & cur, int & x, int & y,
1739 Dimension & dim) const
1741 Inset const * inset = cur.activeMacro();
1745 // get inset dimensions
1746 dim = cur.bv().coordCache().insets().dim(inset);
1747 // FIXME: these 3 are no accurate, but should depend on the font.
1748 // Now the popup jumps down if you enter a char with descent > 0.
1754 = cur.bv().coordCache().insets().xy(inset);
1760 bool InsetMathNest::cursorMathForward(Cursor & cur)
1762 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
1763 cur.pushBackward(*cur.nextAtom().nucleus());
1764 cur.inset().idxFirst(cur);
1767 if (cur.posForward() || idxForward(cur))
1769 // try to pop forwards --- but don't pop out of math! leave that to
1771 int s = cur.depth() - 2;
1772 if (s >= 0 && cur[s].inset().asInsetMath())
1773 return cur.popForward();
1778 bool InsetMathNest::cursorMathBackward(Cursor & cur)
1780 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
1782 cur.push(*cur.nextAtom().nucleus());
1783 cur.inset().idxLast(cur);
1786 if (cur.posBackward() || idxBackward(cur))
1788 // try to pop backwards --- but don't pop out of math! leave that to
1790 int s = cur.depth() - 2;
1791 if (s >= 0 && cur[s].inset().asInsetMath())
1792 return cur.popBackward();
1797 ////////////////////////////////////////////////////////////////////
1799 MathCompletionList::MathCompletionList(Cursor const & cur)
1801 // fill it with macros from the buffer
1802 MacroNameSet macros;
1803 cur.buffer().listMacroNames(macros);
1804 MacroNameSet::const_iterator it;
1805 for (it = macros.begin(); it != macros.end(); ++it) {
1806 if (cur.buffer().getMacro(*it, cur, false))
1807 locals.push_back("\\" + *it);
1809 sort(locals.begin(), locals.end());
1811 if (globals.size() > 0)
1814 // fill in global macros
1816 MacroTable::globalMacros().getMacroNames(macros);
1817 lyxerr << "Globals completion macros: ";
1818 for (it = macros.begin(); it != macros.end(); ++it) {
1819 lyxerr << "\\" + *it << " ";
1820 globals.push_back("\\" + *it);
1822 lyxerr << std::endl;
1824 // fill in global commands
1825 globals.push_back(from_ascii("\\boxed"));
1826 globals.push_back(from_ascii("\\fbox"));
1827 globals.push_back(from_ascii("\\framebox"));
1828 globals.push_back(from_ascii("\\makebox"));
1829 globals.push_back(from_ascii("\\kern"));
1830 globals.push_back(from_ascii("\\xrightarrow"));
1831 globals.push_back(from_ascii("\\xleftarrow"));
1832 globals.push_back(from_ascii("\\split"));
1833 globals.push_back(from_ascii("\\gathered"));
1834 globals.push_back(from_ascii("\\aligned"));
1835 globals.push_back(from_ascii("\\alignedat"));
1836 globals.push_back(from_ascii("\\cases"));
1837 globals.push_back(from_ascii("\\substack"));
1838 globals.push_back(from_ascii("\\subarray"));
1839 globals.push_back(from_ascii("\\array"));
1840 globals.push_back(from_ascii("\\sqrt"));
1841 globals.push_back(from_ascii("\\root"));
1842 globals.push_back(from_ascii("\\tabular"));
1843 globals.push_back(from_ascii("\\stackrel"));
1844 globals.push_back(from_ascii("\\binom"));
1845 globals.push_back(from_ascii("\\choose"));
1846 globals.push_back(from_ascii("\\brace"));
1847 globals.push_back(from_ascii("\\brack"));
1848 globals.push_back(from_ascii("\\frac"));
1849 globals.push_back(from_ascii("\\over"));
1850 globals.push_back(from_ascii("\\nicefrac"));
1851 globals.push_back(from_ascii("\\unitfrac"));
1852 globals.push_back(from_ascii("\\unitfracthree"));
1853 globals.push_back(from_ascii("\\unitone"));
1854 globals.push_back(from_ascii("\\unittwo"));
1855 globals.push_back(from_ascii("\\infer"));
1856 globals.push_back(from_ascii("\\atop"));
1857 globals.push_back(from_ascii("\\lefteqn"));
1858 globals.push_back(from_ascii("\\boldsymbol"));
1859 globals.push_back(from_ascii("\\bm"));
1860 globals.push_back(from_ascii("\\color"));
1861 globals.push_back(from_ascii("\\normalcolor"));
1862 globals.push_back(from_ascii("\\textcolor"));
1863 globals.push_back(from_ascii("\\dfrac"));
1864 globals.push_back(from_ascii("\\tfrac"));
1865 globals.push_back(from_ascii("\\dbinom"));
1866 globals.push_back(from_ascii("\\tbinom"));
1867 globals.push_back(from_ascii("\\hphantom"));
1868 globals.push_back(from_ascii("\\phantom"));
1869 globals.push_back(from_ascii("\\vphantom"));
1870 MathWordList const & words = mathedWordList();
1871 MathWordList::const_iterator it2;
1872 lyxerr << "Globals completion commands: ";
1873 for (it2 = words.begin(); it2 != words.end(); ++it2) {
1874 globals.push_back("\\" + (*it2).first);
1875 lyxerr << "\\" + (*it2).first << " ";
1877 lyxerr << std::endl;
1878 sort(globals.begin(), globals.end());
1882 MathCompletionList::~MathCompletionList()
1887 size_type MathCompletionList::size() const
1889 return locals.size() + globals.size();
1893 docstring const & MathCompletionList::data(size_t idx) const
1895 size_t lsize = locals.size();
1897 return globals[idx - lsize];
1903 std::string MathCompletionList::icon(size_t idx) const
1905 // get the latex command
1907 size_t lsize = locals.size();
1909 cmd = globals[idx - lsize];
1913 // get the icon resource name by stripping the backslash
1914 return "images/math/" + to_utf8(cmd.substr(1)) + ".png";
1917 std::vector<docstring> MathCompletionList::globals;