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 "InsetMathChar.h"
20 #include "InsetMathColor.h"
21 #include "InsetMathComment.h"
22 #include "InsetMathDelim.h"
23 #include "InsetMathHull.h"
24 #include "InsetMathRef.h"
25 #include "InsetMathScript.h"
26 #include "InsetMathSpace.h"
27 #include "InsetMathSymbol.h"
28 #include "InsetMathUnknown.h"
29 #include "MathAutoCorrect.h"
30 #include "MathCompletionList.h"
32 #include "MathFactory.h"
33 #include "MathMacro.h"
34 #include "MathMacroArgument.h"
35 #include "MathParser.h"
36 #include "MathStream.h"
37 #include "MathSupport.h"
41 #include "BufferView.h"
42 #include "CoordCache.h"
44 #include "CutAndPaste.h"
45 #include "DispatchResult.h"
47 #include "FuncRequest.h"
48 #include "FuncStatus.h"
51 #include "OutputParams.h"
54 #include "frontends/Clipboard.h"
55 #include "frontends/Painter.h"
56 #include "frontends/Selection.h"
58 #include "support/lassert.h"
59 #include "support/debug.h"
60 #include "support/gettext.h"
61 #include "support/lstrings.h"
62 #include "support/textutils.h"
63 #include "support/docstream.h"
69 using namespace lyx::support;
73 using cap::copySelection;
74 using cap::grabAndEraseSelection;
75 using cap::cutSelection;
76 using cap::replaceSelection;
77 using cap::selClearOrDel;
80 InsetMathNest::InsetMathNest(idx_type nargs)
81 : cells_(nargs), lock_(false), mouse_hover_(false)
85 InsetMathNest::InsetMathNest(InsetMathNest const & inset)
86 : InsetMath(inset), cells_(inset.cells_), lock_(inset.lock_),
91 InsetMathNest & InsetMathNest::operator=(InsetMathNest const & inset)
93 cells_ = inset.cells_;
96 InsetMath::operator=(inset);
101 InsetMath::idx_type InsetMathNest::nargs() const
103 return cells_.size();
107 void InsetMathNest::cursorPos(BufferView const & bv,
108 CursorSlice const & sl, bool /*boundary*/,
109 int & x, int & y) const
111 // FIXME: This is a hack. Ideally, the coord cache should not store
112 // absolute positions, but relative ones. This would mean to call
113 // setXY() not in MathData::draw(), but in the parent insets' draw()
114 // with the correctly adjusted x,y values. But this means that we'd have
115 // to touch all (math)inset's draw() methods. Right now, we'll store
116 // absolute value, and make them here relative, only to make them
117 // absolute again when actually drawing the cursor. What a mess.
118 LASSERT(&sl.inset() == this, /**/);
119 MathData const & ar = sl.cell();
120 CoordCache const & coord_cache = bv.coordCache();
121 if (!coord_cache.getArrays().has(&ar)) {
122 // this can (semi-)legally happen if we just created this cell
123 // and it never has been drawn before. So don't ASSERT.
124 //lyxerr << "no cached data for array " << &ar << endl;
129 Point const pt = coord_cache.getArrays().xy(&ar);
130 if (!coord_cache.getInsets().has(this)) {
132 //lyxerr << "no cached data for inset " << this << endl;
137 Point const pt2 = coord_cache.getInsets().xy(this);
138 //lyxerr << "retrieving position cache for MathData "
139 // << pt.x_ << ' ' << pt.y_ << endl;
140 x = pt.x_ - pt2.x_ + ar.pos2x(&bv, sl.pos());
142 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
143 // << " asc: " << ascent() << " des: " << descent()
144 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
145 // move cursor visually into empty cells ("blue rectangles");
151 void InsetMathNest::metrics(MetricsInfo const & mi) const
154 for (idx_type i = 0, n = nargs(); i != n; ++i) {
156 cell(i).metrics(m, dim);
161 bool InsetMathNest::idxNext(Cursor & cur) const
163 LASSERT(&cur.inset() == this, /**/);
164 if (cur.idx() == cur.lastidx())
172 bool InsetMathNest::idxForward(Cursor & cur) const
178 bool InsetMathNest::idxPrev(Cursor & cur) const
180 LASSERT(&cur.inset() == this, /**/);
184 cur.pos() = cur.lastpos();
189 bool InsetMathNest::idxBackward(Cursor & cur) const
195 bool InsetMathNest::idxFirst(Cursor & cur) const
197 LASSERT(&cur.inset() == this, /**/);
206 bool InsetMathNest::idxLast(Cursor & cur) const
208 LASSERT(&cur.inset() == this, /**/);
211 cur.idx() = cur.lastidx();
212 cur.pos() = cur.lastpos();
217 void InsetMathNest::dump() const
219 odocstringstream oss;
221 os << "---------------------------------------------\n";
224 for (idx_type i = 0, n = nargs(); i != n; ++i)
225 os << cell(i) << "\n";
226 os << "---------------------------------------------\n";
227 lyxerr << to_utf8(oss.str());
231 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
235 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
238 setPosCache(pi, x, y);
242 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
244 BufferView & bv = *pi.base.bv;
245 // this should use the x/y values given, not the cached values
246 Cursor & cur = bv.cursor();
247 if (!cur.selection())
249 if (&cur.inset() != this)
252 // FIXME: hack to get position cache warm
253 bool const original_drawing_state = pi.pain.isDrawingEnabled();
254 pi.pain.setDrawingEnabled(false);
256 pi.pain.setDrawingEnabled(original_drawing_state);
258 CursorSlice s1 = cur.selBegin();
259 CursorSlice s2 = cur.selEnd();
261 //lyxerr << "InsetMathNest::drawing selection: "
262 // << " s1: " << s1 << " s2: " << s2 << endl;
263 if (s1.idx() == s2.idx()) {
264 MathData const & c = cell(s1.idx());
265 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
266 int x1 = g.pos.x_ + c.pos2x(pi.base.bv, s1.pos());
267 int y1 = g.pos.y_ - g.dim.ascent();
268 int x2 = g.pos.x_ + c.pos2x(pi.base.bv, s2.pos());
269 int y2 = g.pos.y_ + g.dim.descent();
270 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
271 //lyxerr << "InsetMathNest::drawing selection 3: "
272 // << " x1: " << x1 << " x2: " << x2
273 // << " y1: " << y1 << " y2: " << y2 << endl;
275 for (idx_type i = 0; i < nargs(); ++i) {
276 if (idxBetween(i, s1.idx(), s2.idx())) {
277 MathData const & c = cell(i);
278 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
280 int y1 = g.pos.y_ - g.dim.ascent();
281 int x2 = g.pos.x_ + g.dim.width();
282 int y2 = g.pos.y_ + g.dim.descent();
283 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
290 void InsetMathNest::validate(LaTeXFeatures & features) const
292 for (idx_type i = 0; i < nargs(); ++i)
293 cell(i).validate(features);
297 void InsetMathNest::replace(ReplaceData & rep)
299 for (idx_type i = 0; i < nargs(); ++i)
300 cell(i).replace(rep);
304 bool InsetMathNest::contains(MathData const & ar) const
306 for (idx_type i = 0; i < nargs(); ++i)
307 if (cell(i).contains(ar))
313 bool InsetMathNest::lock() const
319 void InsetMathNest::lock(bool l)
325 bool InsetMathNest::isActive() const
331 MathData InsetMathNest::glue() const
334 for (size_t i = 0; i < nargs(); ++i)
340 void InsetMathNest::write(WriteStream & os) const
342 ModeSpecifier specifier(os, currentMode());
343 docstring const latex_name = name();
344 os << '\\' << latex_name;
345 for (size_t i = 0; i < nargs(); ++i)
346 os << '{' << cell(i) << '}';
348 os.pendingSpace(true);
349 if (lock_ && !os.latex()) {
351 os.pendingSpace(true);
356 void InsetMathNest::normalize(NormalStream & os) const
359 for (size_t i = 0; i < nargs(); ++i)
360 os << ' ' << cell(i);
365 int InsetMathNest::latex(odocstream & os, OutputParams const & runparams) const
367 WriteStream wi(os, runparams.moving_arg, true, runparams.dryrun,
374 bool InsetMathNest::setMouseHover(bool mouse_hover)
376 mouse_hover_ = mouse_hover;
381 bool InsetMathNest::notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
385 MathData & ar = cur.cell();
386 // remove base-only "scripts"
387 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
388 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
389 if (p && p->nargs() == 1) {
390 MathData ar = p->nuc();
393 cur.adjust(i, ar.size() - 1);
397 // glue adjacent font insets of the same kind
398 for (pos_type i = 0; i + 1 < size(); ++i) {
399 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
400 InsetMathFont const * q = operator[](i + 1)->asFontInset();
401 if (p && q && p->name() == q->name()) {
402 p->cell(0).append(q->cell(0));
412 void InsetMathNest::handleFont
413 (Cursor & cur, docstring const & arg, char const * const font)
415 handleFont(cur, arg, from_ascii(font));
419 void InsetMathNest::handleFont(Cursor & cur, docstring const & arg,
420 docstring const & font)
422 cur.recordUndoSelection();
424 // this whole function is a hack and won't work for incremental font
426 if (cur.inset().asInsetMath()->name() == font)
427 cur.handleFont(to_utf8(font));
429 handleNest(cur, createInsetMath(font), arg);
433 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest)
435 handleNest(cur, nest, docstring());
439 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest,
440 docstring const & arg)
442 CursorSlice i1 = cur.selBegin();
443 CursorSlice i2 = cur.selEnd();
444 if (!i1.inset().asInsetMath())
446 if (i1.idx() == i2.idx()) {
447 // the easy case where only one cell is selected
448 cur.handleNest(nest);
453 // multiple selected cells in a simple non-grid inset
454 if (i1.asInsetMath()->nrows() == 0 || i1.asInsetMath()->ncols() == 0) {
455 for (idx_type i = i1.idx(); i <= i2.idx(); ++i) {
460 cur.pos() = cur.lastpos();
463 // change font of cell
464 cur.handleNest(nest);
467 // cur is in the font inset now. If the loop continues,
468 // we need to get outside again for the next cell
469 if (i + 1 <= i2.idx())
475 // the complicated case with multiple selected cells in a grid
478 cap::region(i1, i2, r1, r2, c1, c2);
479 for (row_type row = r1; row <= r2; ++row) {
480 for (col_type col = c1; col <= c2; ++col) {
482 cur.idx() = i1.asInsetMath()->index(row, col);
485 cur.pos() = cur.lastpos();
489 cur.handleNest(nest);
492 // cur is in the font inset now. If the loop continues,
493 // we need to get outside again for the next cell
494 if (col + 1 <= c2 || row + 1 <= r2)
501 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
503 cur.recordUndoSelection();
506 font.fromString(to_utf8(arg), b);
507 if (font.fontInfo().color() != Color_inherit &&
508 font.fontInfo().color() != Color_ignore)
509 handleNest(cur, MathAtom(new InsetMathColor(true, font.fontInfo().color())));
511 // FIXME: support other font changes here as well?
515 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
517 //lyxerr << "InsetMathNest: request: " << cmd << endl;
519 Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
521 switch (cmd.action) {
523 case LFUN_CLIPBOARD_PASTE:
524 parseflg |= Parse::VERBATIM;
527 if (cur.currentMode() == TEXT_MODE)
528 parseflg |= Parse::TEXTMODE;
529 cur.recordUndoSelection();
530 cur.message(_("Paste"));
531 replaceSelection(cur);
533 if (cmd.argument().empty() && !theClipboard().isInternal())
534 topaste = theClipboard().getAsText();
537 idocstringstream is(cmd.argument());
539 topaste = cap::selection(n);
541 cur.niceInsert(topaste, parseflg);
542 cur.clearSelection(); // bug 393
549 cutSelection(cur, true, true);
550 cur.message(_("Cut"));
551 // Prevent stale position >= size crash
552 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
558 cur.message(_("Copy"));
561 case LFUN_MOUSE_PRESS:
562 lfunMousePress(cur, cmd);
565 case LFUN_MOUSE_MOTION:
566 lfunMouseMotion(cur, cmd);
569 case LFUN_MOUSE_RELEASE:
570 lfunMouseRelease(cur, cmd);
573 case LFUN_FINISHED_LEFT: // in math, left is backwards
574 case LFUN_FINISHED_BACKWARD:
575 cur.bv().cursor() = cur;
578 case LFUN_FINISHED_RIGHT: // in math, right is forward
579 case LFUN_FINISHED_FORWARD:
581 cur.bv().cursor() = cur;
584 case LFUN_CHAR_RIGHT:
586 case LFUN_CHAR_BACKWARD:
587 case LFUN_CHAR_FORWARD:
588 cur.updateFlags(Update::Decoration | Update::FitCursor);
589 case LFUN_CHAR_RIGHT_SELECT:
590 case LFUN_CHAR_LEFT_SELECT:
591 case LFUN_CHAR_BACKWARD_SELECT:
592 case LFUN_CHAR_FORWARD_SELECT: {
593 // are we in a selection?
594 bool select = (cmd.action == LFUN_CHAR_RIGHT_SELECT
595 || cmd.action == LFUN_CHAR_LEFT_SELECT
596 || cmd.action == LFUN_CHAR_BACKWARD_SELECT
597 || cmd.action == LFUN_CHAR_FORWARD_SELECT);
598 // are we moving forward or backwards?
599 // If the command was RIGHT or LEFT, then whether we're moving forward
600 // or backwards depends on the cursor movement mode (logical or visual):
601 // * in visual mode, since math is always LTR, right -> forward,
603 // * in logical mode, the mapping is determined by the
604 // reverseDirectionNeeded() function
607 FuncCode finish_lfun;
609 if (cmd.action == LFUN_CHAR_FORWARD
610 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
612 finish_lfun = LFUN_FINISHED_FORWARD;
614 else if (cmd.action == LFUN_CHAR_BACKWARD
615 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
617 finish_lfun = LFUN_FINISHED_BACKWARD;
620 bool right = (cmd.action == LFUN_CHAR_RIGHT_SELECT
621 || cmd.action == LFUN_CHAR_RIGHT);
622 if (lyxrc.visual_cursor || !reverseDirectionNeeded(cur))
628 finish_lfun = LFUN_FINISHED_RIGHT;
630 finish_lfun = LFUN_FINISHED_LEFT;
632 // Now that we know exactly what we want to do, let's do it!
633 cur.selHandle(select);
635 cur.macroModeClose();
636 // try moving forward or backwards as necessary...
637 if (!(forward ? cursorMathForward(cur) : cursorMathBackward(cur))) {
638 // ... and if movement failed, then finish forward or backwards
640 cmd = FuncRequest(finish_lfun);
648 cur.updateFlags(Update::Decoration | Update::FitCursor);
649 case LFUN_DOWN_SELECT:
650 case LFUN_UP_SELECT: {
651 // close active macro
652 if (cur.inMacroMode()) {
653 cur.macroModeClose();
657 // stop/start the selection
658 bool select = cmd.action == LFUN_DOWN_SELECT ||
659 cmd.action == LFUN_UP_SELECT;
660 cur.selHandle(select);
663 bool up = cmd.action == LFUN_UP || cmd.action == LFUN_UP_SELECT;
664 bool successful = cur.upDownInMath(up);
668 if (cur.fixIfBroken())
669 // FIXME: Something bad happened. We pass the corrected Cursor
670 // instead of letting things go worse.
673 // We did not manage to move the cursor.
678 case LFUN_MOUSE_DOUBLE:
679 case LFUN_MOUSE_TRIPLE:
680 case LFUN_WORD_SELECT:
684 cur.setSelection(true);
685 cur.pos() = cur.lastpos();
686 cur.idx() = cur.lastidx();
689 case LFUN_PARAGRAPH_UP:
690 case LFUN_PARAGRAPH_DOWN:
691 cur.updateFlags(Update::Decoration | Update::FitCursor);
692 case LFUN_PARAGRAPH_UP_SELECT:
693 case LFUN_PARAGRAPH_DOWN_SELECT:
696 case LFUN_LINE_BEGIN:
697 case LFUN_WORD_BACKWARD:
699 cur.updateFlags(Update::Decoration | Update::FitCursor);
700 case LFUN_LINE_BEGIN_SELECT:
701 case LFUN_WORD_BACKWARD_SELECT:
702 case LFUN_WORD_LEFT_SELECT:
703 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
704 cmd.action == LFUN_WORD_LEFT_SELECT ||
705 cmd.action == LFUN_LINE_BEGIN_SELECT);
706 cur.macroModeClose();
707 if (cur.pos() != 0) {
709 } else if (cur.col() != 0) {
710 cur.idx() -= cur.col();
712 } else if (cur.idx() != 0) {
716 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
721 case LFUN_WORD_FORWARD:
722 case LFUN_WORD_RIGHT:
724 cur.updateFlags(Update::Decoration | Update::FitCursor);
725 case LFUN_WORD_FORWARD_SELECT:
726 case LFUN_WORD_RIGHT_SELECT:
727 case LFUN_LINE_END_SELECT:
728 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
729 cmd.action == LFUN_WORD_RIGHT_SELECT ||
730 cmd.action == LFUN_LINE_END_SELECT);
731 cur.macroModeClose();
733 if (cur.pos() != cur.lastpos()) {
734 cur.pos() = cur.lastpos();
735 } else if (ncols() && (cur.col() != cur.lastcol())) {
736 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
737 cur.pos() = cur.lastpos();
738 } else if (cur.idx() != cur.lastidx()) {
739 cur.idx() = cur.lastidx();
740 cur.pos() = cur.lastpos();
742 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
747 case LFUN_CELL_FORWARD:
748 cur.updateFlags(Update::Decoration | Update::FitCursor);
749 cur.inset().idxNext(cur);
752 case LFUN_CELL_BACKWARD:
753 cur.updateFlags(Update::Decoration | Update::FitCursor);
754 cur.inset().idxPrev(cur);
757 case LFUN_WORD_DELETE_BACKWARD:
758 case LFUN_CHAR_DELETE_BACKWARD:
760 // May affect external cell:
761 cur.recordUndoInset();
763 cur.recordUndoSelection();
764 // if the inset can not be removed from within, delete it
765 if (!cur.backspace()) {
766 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
767 cur.innerText()->dispatch(cur, cmd);
771 case LFUN_WORD_DELETE_FORWARD:
772 case LFUN_CHAR_DELETE_FORWARD:
773 if (cur.pos() == cur.lastpos())
774 // May affect external cell:
775 cur.recordUndoInset();
777 cur.recordUndoSelection();
778 // if the inset can not be removed from within, delete it
780 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
781 cur.innerText()->dispatch(cur, cmd);
787 cur.clearSelection();
789 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
794 // 'Locks' the math inset. A 'locked' math inset behaves as a unit
795 // that is traversed by a single <CursorLeft>/<CursorRight>.
796 case LFUN_INSET_TOGGLE:
802 case LFUN_SELF_INSERT:
803 if (cmd.argument().size() != 1) {
804 cur.recordUndoSelection();
805 docstring const arg = cmd.argument();
806 if (!interpretString(cur, arg))
810 // Don't record undo steps if we are in macro mode and
811 // cmd.argument is the next character of the macro name.
812 // Otherwise we'll get an invalid cursor if we undo after
813 // the macro was finished and the macro is a known command,
814 // e.g. sqrt. Cursor::macroModeClose replaces in this case
815 // the InsetMathUnknown with name "frac" by an empty
816 // InsetMathFrac -> a pos value > 0 is invalid.
817 // A side effect is that an undo before the macro is finished
818 // undoes the complete macro, not only the last character.
819 if (!cur.inMacroMode()) {
820 MathMacro const * macro = 0;
821 if (cur.pos() > 0 && cmd.argument() != "\\")
822 macro = cur.inset().asInsetMath()->asMacro();
825 cur.recordUndoSelection();
828 // spacial handling of space. If we insert an inset
829 // via macro mode, we want to put the cursor inside it
830 // if relevant. Think typing "\frac<space>".
831 if (cmd.argument()[0] == ' '
832 && cur.inMacroMode() && cur.macroName() != "\\"
833 && cur.macroModeClose()) {
834 MathAtom const atom = cur.prevAtom();
835 if (atom->asNestInset() && atom->isActive()) {
837 cur.pushBackward(*cur.nextInset());
839 } else if (!interpretChar(cur, cmd.argument()[0])) {
840 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
845 //case LFUN_SERVER_GET_XY:
848 case LFUN_SERVER_SET_XY: {
849 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
852 istringstream is(to_utf8(cmd.argument()));
854 cur.setScreenPos(x, y);
858 // Special casing for superscript in case of LyX handling
860 case LFUN_ACCENT_CIRCUMFLEX:
861 if (cmd.argument().empty()) {
862 // do superscript if LyX handles
864 cur.recordUndoSelection();
865 script(cur, true, grabAndEraseSelection(cur));
869 case LFUN_ACCENT_UMLAUT:
870 case LFUN_ACCENT_ACUTE:
871 case LFUN_ACCENT_GRAVE:
872 case LFUN_ACCENT_BREVE:
873 case LFUN_ACCENT_DOT:
874 case LFUN_ACCENT_MACRON:
875 case LFUN_ACCENT_CARON:
876 case LFUN_ACCENT_TILDE:
877 case LFUN_ACCENT_CEDILLA:
878 case LFUN_ACCENT_CIRCLE:
879 case LFUN_ACCENT_UNDERDOT:
880 case LFUN_ACCENT_TIE:
881 case LFUN_ACCENT_OGONEK:
882 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
886 case LFUN_TEXTSTYLE_APPLY:
887 case LFUN_TEXTSTYLE_UPDATE:
888 handleFont2(cur, cmd.argument());
892 if (currentMode() == TEXT_MODE)
893 handleFont(cur, cmd.argument(), "textbf");
895 handleFont(cur, cmd.argument(), "mathbf");
897 case LFUN_FONT_BOLDSYMBOL:
898 if (currentMode() == TEXT_MODE)
899 handleFont(cur, cmd.argument(), "textbf");
901 handleFont(cur, cmd.argument(), "boldsymbol");
904 if (currentMode() == TEXT_MODE)
905 handleFont(cur, cmd.argument(), "textsf");
907 handleFont(cur, cmd.argument(), "mathsf");
910 if (currentMode() == TEXT_MODE)
911 handleFont(cur, cmd.argument(), "emph");
913 handleFont(cur, cmd.argument(), "mathcal");
915 case LFUN_FONT_ROMAN:
916 if (currentMode() == TEXT_MODE)
917 handleFont(cur, cmd.argument(), "textrm");
919 handleFont(cur, cmd.argument(), "mathrm");
921 case LFUN_FONT_TYPEWRITER:
922 if (currentMode() == TEXT_MODE)
923 handleFont(cur, cmd.argument(), "texttt");
925 handleFont(cur, cmd.argument(), "mathtt");
928 handleFont(cur, cmd.argument(), "mathfrak");
931 if (currentMode() == TEXT_MODE)
932 handleFont(cur, cmd.argument(), "textit");
934 handleFont(cur, cmd.argument(), "mathit");
937 if (currentMode() == TEXT_MODE)
938 // FIXME: should be "noun"
939 handleFont(cur, cmd.argument(), "textsc");
941 handleFont(cur, cmd.argument(), "mathbb");
943 case LFUN_FONT_DEFAULT:
944 handleFont(cur, cmd.argument(), "textnormal");
947 case LFUN_MATH_MODE: {
949 // ignore math-mode on when already in math mode
950 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
952 cur.recordUndoSelection();
953 cur.macroModeClose();
954 docstring const save_selection = grabAndEraseSelection(cur);
956 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
957 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
959 cur.pushBackward(*cur.nextInset());
960 cur.niceInsert(save_selection);
962 if (currentMode() == Inset::TEXT_MODE) {
963 cur.recordUndoSelection();
964 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
965 cur.message(_("create new math text environment ($...$)"));
967 handleFont(cur, cmd.argument(), "textrm");
968 cur.message(_("entered math text mode (textrm)"));
974 case LFUN_REGEXP_MODE: {
975 InsetMathHull * i = dynamic_cast<InsetMathHull *>(cur.inset().asInsetMath());
976 if (i && i->getType() == hullRegexp) {
977 cur.message(_("Already in regexp mode"));
980 cur.macroModeClose();
981 docstring const save_selection = grabAndEraseSelection(cur);
983 cur.plainInsert(MathAtom(new InsetMathHull(hullRegexp)));
985 cur.pushBackward(*cur.nextInset());
986 cur.niceInsert(save_selection);
987 cur.message(_("Regexp editor mode"));
991 case LFUN_MATH_FONT_STYLE: {
992 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, '\\' + cmd.argument());
997 case LFUN_MATH_SIZE: {
998 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, cmd.argument());
1003 case LFUN_MATH_MATRIX: {
1009 idocstringstream is(cmd.argument());
1010 is >> m >> n >> v_align >> h_align;
1017 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
1021 case LFUN_MATH_DELIM: {
1023 docstring rs = split(cmd.argument(), ls, ' ');
1024 // Reasonable default values
1030 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
1034 case LFUN_MATH_BIGDELIM: {
1035 docstring const lname = from_utf8(cmd.getArg(0));
1036 docstring const ldelim = from_utf8(cmd.getArg(1));
1037 docstring const rname = from_utf8(cmd.getArg(2));
1038 docstring const rdelim = from_utf8(cmd.getArg(3));
1039 latexkeys const * l = in_word_set(lname);
1040 bool const have_l = l && l->inset == "big" &&
1041 InsetMathBig::isBigInsetDelim(ldelim);
1042 l = in_word_set(rname);
1043 bool const have_r = l && l->inset == "big" &&
1044 InsetMathBig::isBigInsetDelim(rdelim);
1045 // We mimic LFUN_MATH_DELIM in case we have an empty left
1046 // or right delimiter.
1047 if (have_l || have_r) {
1049 docstring const selection = grabAndEraseSelection(cur);
1052 cur.insert(MathAtom(new InsetMathBig(lname,
1054 cur.niceInsert(selection);
1056 cur.insert(MathAtom(new InsetMathBig(rname,
1059 // Don't call cur.undispatched() if we did nothing, this would
1060 // lead to infinite recursion via Text::dispatch().
1064 case LFUN_SPACE_INSERT:
1065 cur.recordUndoSelection();
1066 cur.insert(MathAtom(new InsetMathSpace));
1069 case LFUN_MATH_SPACE:
1070 cur.recordUndoSelection();
1071 if (cmd.argument().empty())
1072 cur.insert(MathAtom(new InsetMathSpace));
1074 string const name = cmd.getArg(0);
1075 string const len = cmd.getArg(1);
1076 cur.insert(MathAtom(new InsetMathSpace(name, len)));
1080 case LFUN_ERT_INSERT:
1081 // interpret this as if a backslash was typed
1083 interpretChar(cur, '\\');
1086 case LFUN_MATH_SUBSCRIPT:
1087 // interpret this as if a _ was typed
1088 cur.recordUndoSelection();
1089 interpretChar(cur, '_');
1092 case LFUN_MATH_SUPERSCRIPT:
1093 // interpret this as if a ^ was typed
1094 cur.recordUndoSelection();
1095 interpretChar(cur, '^');
1098 case LFUN_MATH_MACRO_FOLD:
1099 case LFUN_MATH_MACRO_UNFOLD: {
1101 bool fold = cmd.action == LFUN_MATH_MACRO_FOLD;
1102 bool found = findMacroToFoldUnfold(it, fold);
1104 MathMacro * macro = it.nextInset()->asInsetMath()->asMacro();
1105 cur.recordUndoInset();
1114 case LFUN_QUOTE_INSERT:
1115 // interpret this as if a straight " was typed
1116 cur.recordUndoSelection();
1117 interpretChar(cur, '\"');
1120 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
1121 // handling such that "self-insert" works on "arbitrary stuff" too, and
1122 // math-insert only handles special math things like "matrix".
1123 case LFUN_MATH_INSERT: {
1124 cur.recordUndoSelection();
1125 if (cmd.argument() == "^" || cmd.argument() == "_")
1126 interpretChar(cur, cmd.argument()[0]);
1129 asArray(cmd.argument(), ar);
1130 if (ar.size() == 1 && ar[0]->asNestInset()
1131 && ar[0]->asNestInset()->nargs() > 1)
1132 handleNest(cur, ar[0]);
1134 cur.niceInsert(cmd.argument());
1139 case LFUN_DIALOG_SHOW_NEW_INSET: {
1140 docstring const & name = cmd.argument();
1142 if (name == "ref") {
1143 InsetMathRef tmp(name);
1144 data = tmp.createDialogStr(to_utf8(name));
1145 } else if (name == "mathspace") {
1147 data = tmp.createDialogStr();
1149 cur.bv().showDialog(to_utf8(name), data);
1153 case LFUN_INSET_INSERT: {
1155 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1156 cur.recordUndoSelection();
1162 case LFUN_INSET_DISSOLVE:
1163 if (!asHullInset()) {
1164 cur.recordUndoInset();
1170 InsetMath::doDispatch(cur, cmd);
1176 bool InsetMathNest::findMacroToFoldUnfold(Cursor & it, bool fold) const {
1177 // look for macro to open/close, but stay in mathed
1178 for (; !it.empty(); it.pop_back()) {
1180 // go backward through the current cell
1181 Inset * inset = it.nextInset();
1182 while (inset && inset->asInsetMath()) {
1183 MathMacro * macro = inset->asInsetMath()->asMacro();
1185 // found the an macro to open/close?
1186 if (macro->folded() != fold)
1189 // Wrong folding state.
1190 // If this was the first we see in this slice, look further left,
1192 if (inset != it.nextInset())
1196 // go up if this was the left most position
1202 inset = it.nextInset();
1210 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1211 FuncStatus & flag) const
1213 // the font related toggles
1214 //string tc = "mathnormal";
1216 string const arg = to_utf8(cmd.argument());
1217 switch (cmd.action) {
1218 case LFUN_TABULAR_FEATURE:
1219 flag.setEnabled(false);
1222 case LFUN_TABULAR_FEATURE:
1223 // FIXME: check temporarily disabled
1225 char align = mathcursor::valign();
1226 if (align == '\0') {
1230 if (cmd.argument().empty()) {
1234 if (!contains("tcb", cmd.argument()[0])) {
1238 flag.setOnOff(cmd.argument()[0] == align);
1241 /// We have to handle them since 1.4 blocks all unhandled actions
1242 case LFUN_FONT_ITAL:
1243 case LFUN_FONT_BOLD:
1244 case LFUN_FONT_BOLDSYMBOL:
1245 case LFUN_FONT_SANS:
1246 case LFUN_FONT_EMPH:
1247 case LFUN_FONT_TYPEWRITER:
1248 case LFUN_FONT_NOUN:
1249 case LFUN_FONT_ROMAN:
1250 case LFUN_FONT_DEFAULT:
1251 flag.setEnabled(true);
1253 case LFUN_MATH_MUTATE:
1254 //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
1255 flag.setOnOff(false);
1258 // we just need to be in math mode to enable that
1259 case LFUN_MATH_SIZE:
1260 case LFUN_MATH_SPACE:
1261 case LFUN_MATH_LIMITS:
1262 case LFUN_MATH_EXTERN:
1263 flag.setEnabled(true);
1266 case LFUN_FONT_FRAK:
1267 flag.setEnabled(currentMode() != TEXT_MODE);
1270 case LFUN_MATH_FONT_STYLE: {
1271 bool const textarg =
1272 arg == "textbf" || arg == "textsf" ||
1273 arg == "textrm" || arg == "textmd" ||
1274 arg == "textit" || arg == "textsc" ||
1275 arg == "textsl" || arg == "textup" ||
1276 arg == "texttt" || arg == "textbb" ||
1277 arg == "textnormal";
1278 flag.setEnabled(currentMode() != TEXT_MODE || textarg);
1282 case LFUN_MATH_INSERT:
1283 flag.setEnabled(currentMode() != TEXT_MODE);
1286 case LFUN_MATH_MATRIX:
1287 flag.setEnabled(currentMode() == MATH_MODE);
1290 case LFUN_INSET_INSERT: {
1291 // Don't test createMathInset_fromDialogStr(), since
1292 // getStatus is not called with a valid reference and the
1293 // dialog would not be applyable.
1294 string const name = cmd.getArg(0);
1295 flag.setEnabled(name == "ref" || name == "mathspace");
1299 case LFUN_MATH_DELIM:
1300 case LFUN_MATH_BIGDELIM:
1301 // Don't do this with multi-cell selections
1302 flag.setEnabled(cur.selBegin().idx() == cur.selEnd().idx());
1305 case LFUN_MATH_MACRO_FOLD:
1306 case LFUN_MATH_MACRO_UNFOLD: {
1308 bool found = findMacroToFoldUnfold(it, cmd.action == LFUN_MATH_MACRO_FOLD);
1309 flag.setEnabled(found);
1313 case LFUN_SPECIALCHAR_INSERT:
1314 // FIXME: These would probably make sense in math-text mode
1315 flag.setEnabled(false);
1318 case LFUN_INSET_DISSOLVE:
1319 flag.setEnabled(!asHullInset());
1330 void InsetMathNest::edit(Cursor & cur, bool front, EntryDirection entry_from)
1333 bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_RIGHT ||
1334 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1335 cur.idx() = enter_front ? 0 : cur.lastidx();
1336 cur.pos() = enter_front ? 0 : cur.lastpos();
1338 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1342 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1345 int dist_min = 1000000;
1346 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1347 int const d = cell(i).dist(cur.bv(), x, y);
1353 MathData & ar = cell(idx_min);
1355 cur.idx() = idx_min;
1356 cur.pos() = ar.x2pos(&cur.bv(), x - ar.xo(cur.bv()));
1358 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1359 if (dist_min == 0) {
1361 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1362 if (ar[i]->covers(cur.bv(), x, y))
1363 return ar[i].nucleus()->editXY(cur, x, y);
1369 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1371 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1372 BufferView & bv = cur.bv();
1373 bool do_selection = cmd.button() == mouse_button::button1
1374 && cmd.argument() == "region-select";
1375 bv.mouseSetCursor(cur, do_selection);
1376 if (cmd.button() == mouse_button::button1) {
1377 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1378 // Update the cursor update flags as needed:
1380 // Update::Decoration: tells to update the decoration
1381 // (visual box corners that define
1383 // Update::FitCursor: adjust the screen to the cursor
1384 // position if needed
1385 // cur.result().update(): don't overwrite previously set flags.
1386 cur.updateFlags(Update::Decoration | Update::FitCursor
1387 | cur.result().update());
1388 } else if (cmd.button() == mouse_button::button2) {
1389 if (cap::selection()) {
1390 // See comment in Text::dispatch why we do this
1391 cap::copySelectionToStack();
1392 cmd = FuncRequest(LFUN_PASTE, "0");
1393 doDispatch(bv.cursor(), cmd);
1396 asArray(theSelection().get(), ar);
1397 bv.cursor().insert(ar);
1403 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1405 // only select with button 1
1406 if (cmd.button() == mouse_button::button1) {
1407 Cursor & bvcur = cur.bv().cursor();
1408 if (bvcur.anchor_.hasPart(cur)) {
1409 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1410 bvcur.setCursor(cur);
1411 bvcur.setSelection(true);
1412 //lyxerr << "MOTION " << bvcur << endl;
1419 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1421 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1423 if (cmd.button() == mouse_button::button1) {
1424 if (!cur.selection())
1427 Cursor & bvcur = cur.bv().cursor();
1428 bvcur.setSelection(true);
1437 bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
1439 //lyxerr << "interpret 2: '" << c << "'" << endl;
1440 docstring save_selection;
1441 if (c == '^' || c == '_')
1442 save_selection = grabAndEraseSelection(cur);
1447 if (cur.inMacroMode()) {
1448 docstring name = cur.macroName();
1450 /// are we currently typing '#1' or '#2' or...?
1451 if (name == "\\#") {
1454 if (n >= 1 && n <= 9)
1455 cur.insert(new MathMacroArgument(n));
1459 // do not finish macro for known * commands
1460 MathWordList const & mwl = mathedWordList();
1461 bool star_macro = c == '*'
1462 && (mwl.find(name.substr(1) + "*") != mwl.end()
1463 || cur.buffer()->getMacro(name.substr(1) + "*", cur, true));
1464 if (isAlphaASCII(c) || star_macro) {
1465 cur.activeMacro()->setName(name + docstring(1, c));
1469 // handle 'special char' macros
1474 if (currentMode() == InsetMath::TEXT_MODE)
1475 cur.niceInsert(createInsetMath("textbackslash"));
1477 cur.niceInsert(createInsetMath("backslash"));
1478 } else if (c == '^' && currentMode() == InsetMath::MATH_MODE) {
1480 cur.niceInsert(createInsetMath("mathcircumflex"));
1481 } else if (c == '{') {
1483 cur.niceInsert(MathAtom(new InsetMathBrace));
1484 } else if (c == '%') {
1486 cur.niceInsert(MathAtom(new InsetMathComment));
1487 } else if (c == '#') {
1488 LASSERT(cur.activeMacro(), /**/);
1489 cur.activeMacro()->setName(name + docstring(1, c));
1492 cur.niceInsert(createInsetMath(docstring(1, c)));
1497 // One character big delimiters. The others are handled in
1498 // interpretString().
1499 latexkeys const * l = in_word_set(name.substr(1));
1500 if (name[0] == '\\' && l && l->inset == "big") {
1504 delim = from_ascii("\\{");
1507 delim = from_ascii("\\}");
1510 delim = docstring(1, c);
1513 if (InsetMathBig::isBigInsetDelim(delim)) {
1514 // name + delim ared a valid InsetMathBig.
1515 // We can't use cur.macroModeClose() because
1516 // it does not handle delim.
1517 InsetMathUnknown * p = cur.activeMacro();
1520 cur.cell().erase(cur.pos());
1521 cur.plainInsert(MathAtom(
1522 new InsetMathBig(name.substr(1), delim)));
1527 // leave macro mode and try again if necessary
1528 if (cur.macroModeClose()) {
1529 MathAtom const atom = cur.prevAtom();
1530 if (atom->asNestInset() && atom->isActive()) {
1532 cur.pushBackward(*cur.nextInset());
1536 cur.niceInsert(MathAtom(new InsetMathBrace));
1538 interpretChar(cur, c);
1543 // leave autocorrect mode if necessary
1544 if (lyxrc.autocorrection_math && c == ' ' && cur.autocorrect()) {
1545 cur.autocorrect() = false;
1546 cur.message(_("Autocorrect Off ('!' to enter)"));
1549 if (lyxrc.autocorrection_math && c == '!' && !cur.autocorrect()) {
1550 cur.autocorrect() = true;
1551 cur.message(_("Autocorrect On (<space> to exit)"));
1555 // just clear selection on pressing the space bar
1556 if (cur.selection() && c == ' ') {
1557 cur.setSelection(false);
1562 //lyxerr << "starting with macro" << endl;
1563 bool reduced = cap::reduceSelectionToOneCell(cur);
1564 if (reduced || !cur.selection()) {
1565 docstring const safe = cap::grabAndEraseSelection(cur);
1566 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), safe, false)));
1574 if (currentMode() == InsetMath::TEXT_MODE)
1580 if (currentMode() == InsetMath::TEXT_MODE) {
1581 // insert spaces in text mode,
1582 // but suppress direct insertion of two spaces in a row
1583 // the still allows typing '<space>a<space>' and deleting the 'a', but
1584 // it is better than nothing...
1585 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1587 // FIXME: we have to enable full redraw here because of the
1588 // visual box corners that define the inset. If we know for
1589 // sure that we stay within the same cell we can optimize for
1591 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1595 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1596 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1597 // FIXME: we have to enable full redraw here because of the
1598 // visual box corners that define the inset. If we know for
1599 // sure that we stay within the same cell we can optimize for
1601 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1605 if (cur.popForward()) {
1606 // FIXME: we have to enable full redraw here because of the
1607 // visual box corners that define the inset. If we know for
1608 // sure that we stay within the same cell we can optimize for
1610 //cur.updateFlags(Update::FitCursor);
1614 // if we are at the very end, leave the formula
1615 return cur.pos() != cur.lastpos();
1618 // These should be treated differently when not in text mode:
1619 if (currentMode() != InsetMath::TEXT_MODE) {
1621 script(cur, false, save_selection);
1625 script(cur, true, save_selection);
1629 cur.niceInsert(createInsetMath("sim"));
1632 if (!isAsciiOrMathAlpha(c)) {
1633 MathAtom at = createInsetMath("text");
1634 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
1641 cur.niceInsert(createInsetMath("textasciicircum"));
1645 cur.niceInsert(createInsetMath("textasciitilde"));
1650 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1651 c == '%' || c == '_') {
1652 cur.niceInsert(createInsetMath(docstring(1, c)));
1657 // try auto-correction
1658 if (lyxrc.autocorrection_math && cur.autocorrect() && cur.pos() != 0
1659 && math_autocorrect(cur.prevAtom(), c))
1662 // no special circumstances, so insert the character without any fuss
1664 if (lyxrc.autocorrection_math) {
1665 if (!cur.autocorrect())
1666 cur.message(_("Autocorrect Off ('!' to enter)"));
1668 cur.message(_("Autocorrect On (<space> to exit)"));
1674 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1676 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1678 if (!cur.empty() && cur.pos() > 0 &&
1679 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1680 if (InsetMathBig::isBigInsetDelim(str)) {
1681 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1682 if (prev[0] == '\\') {
1683 prev = prev.substr(1);
1684 latexkeys const * l = in_word_set(prev);
1685 if (l && l->inset == "big") {
1686 cur.cell()[cur.pos() - 1] =
1687 MathAtom(new InsetMathBig(prev, str));
1697 bool InsetMathNest::script(Cursor & cur, bool up)
1699 return script(cur, up, docstring());
1703 bool InsetMathNest::script(Cursor & cur, bool up,
1704 docstring const & save_selection)
1706 // Hack to get \^ and \_ working
1707 //lyxerr << "handling script: up: " << up << endl;
1708 if (cur.inMacroMode() && cur.macroName() == "\\") {
1710 cur.niceInsert(createInsetMath("mathcircumflex"));
1712 interpretChar(cur, '_');
1716 cur.macroModeClose();
1717 if (asScriptInset() && cur.idx() == 0) {
1718 // we are in a nucleus of a script inset, move to _our_ script
1719 InsetMathScript * inset = asScriptInset();
1720 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1722 cur.idx() = inset->idxOfScript(up);
1724 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1726 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1729 cur.idx() = inset->idxOfScript(up);
1730 cur.pos() = cur.lastpos();
1732 // convert the thing to our left to a scriptinset or create a new
1733 // one if in the very first position of the array
1734 if (cur.pos() == 0) {
1735 //lyxerr << "new scriptinset" << endl;
1736 cur.insert(new InsetMathScript(up));
1738 //lyxerr << "converting prev atom " << endl;
1739 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1742 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1743 // See comment in MathParser.cpp for special handling of {}-bases
1749 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1750 cur.niceInsert(save_selection);
1752 //lyxerr << "inserting selection 2:\n" << save_selection << endl;
1757 bool InsetMathNest::completionSupported(Cursor const & cur) const
1759 return cur.inMacroMode();
1763 bool InsetMathNest::inlineCompletionSupported(Cursor const & cur) const
1765 return cur.inMacroMode();
1769 bool InsetMathNest::automaticInlineCompletion() const
1771 return lyxrc.completion_inline_math;
1775 bool InsetMathNest::automaticPopupCompletion() const
1777 return lyxrc.completion_popup_math;
1781 CompletionList const *
1782 InsetMathNest::createCompletionList(Cursor const & cur) const
1784 if (!cur.inMacroMode())
1787 return new MathCompletionList(cur);
1791 docstring InsetMathNest::completionPrefix(Cursor const & cur) const
1793 if (!cur.inMacroMode())
1796 return cur.activeMacro()->name();
1800 bool InsetMathNest::insertCompletion(Cursor & cur, docstring const & s,
1803 if (!cur.inMacroMode())
1806 // append completion to active macro
1807 InsetMathUnknown * inset = cur.activeMacro();
1808 inset->setName(inset->name() + s);
1813 // FIXME: this creates duplicates in the completion popup
1814 // which looks ugly. Moreover the changes the list lengths
1816 confuse the popup as well.
1817 MathCompletionList::addToFavorites(inset->name());
1819 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, " "));
1826 void InsetMathNest::completionPosAndDim(Cursor const & cur, int & x, int & y,
1827 Dimension & dim) const
1829 Inset const * inset = cur.activeMacro();
1833 // get inset dimensions
1834 dim = cur.bv().coordCache().insets().dim(inset);
1835 // FIXME: these 3 are no accurate, but should depend on the font.
1836 // Now the popup jumps down if you enter a char with descent > 0.
1842 = cur.bv().coordCache().insets().xy(inset);
1848 bool InsetMathNest::cursorMathForward(Cursor & cur)
1850 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
1851 cur.pushBackward(*cur.nextAtom().nucleus());
1852 cur.inset().idxFirst(cur);
1855 if (cur.posForward() || idxForward(cur))
1857 // try to pop forwards --- but don't pop out of math! leave that to
1859 int s = cur.depth() - 2;
1860 if (s >= 0 && cur[s].inset().asInsetMath())
1861 return cur.popForward();
1866 bool InsetMathNest::cursorMathBackward(Cursor & cur)
1868 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
1870 cur.push(*cur.nextAtom().nucleus());
1871 cur.inset().idxLast(cur);
1874 if (cur.posBackward() || idxBackward(cur))
1876 // try to pop backwards --- but don't pop out of math! leave that to
1878 int s = cur.depth() - 2;
1879 if (s >= 0 && cur[s].inset().asInsetMath())
1880 return cur.popBackward();
1885 ////////////////////////////////////////////////////////////////////
1887 MathCompletionList::MathCompletionList(Cursor const & cur)
1889 // fill it with macros from the buffer
1890 MacroNameSet macros;
1891 cur.buffer()->listMacroNames(macros);
1892 MacroNameSet::const_iterator it;
1893 for (it = macros.begin(); it != macros.end(); ++it) {
1894 if (cur.buffer()->getMacro(*it, cur, false))
1895 locals.push_back("\\" + *it);
1897 sort(locals.begin(), locals.end());
1899 if (globals.size() > 0)
1902 // fill in global macros
1904 MacroTable::globalMacros().getMacroNames(macros);
1905 //lyxerr << "Globals completion macros: ";
1906 for (it = macros.begin(); it != macros.end(); ++it) {
1907 //lyxerr << "\\" + *it << " ";
1908 globals.push_back("\\" + *it);
1910 //lyxerr << std::endl;
1912 // fill in global commands
1913 globals.push_back(from_ascii("\\boxed"));
1914 globals.push_back(from_ascii("\\fbox"));
1915 globals.push_back(from_ascii("\\framebox"));
1916 globals.push_back(from_ascii("\\makebox"));
1917 globals.push_back(from_ascii("\\kern"));
1918 globals.push_back(from_ascii("\\xrightarrow"));
1919 globals.push_back(from_ascii("\\xleftarrow"));
1920 globals.push_back(from_ascii("\\split"));
1921 globals.push_back(from_ascii("\\gathered"));
1922 globals.push_back(from_ascii("\\aligned"));
1923 globals.push_back(from_ascii("\\alignedat"));
1924 globals.push_back(from_ascii("\\cases"));
1925 globals.push_back(from_ascii("\\substack"));
1926 globals.push_back(from_ascii("\\xymatrix"));
1927 globals.push_back(from_ascii("\\subarray"));
1928 globals.push_back(from_ascii("\\array"));
1929 globals.push_back(from_ascii("\\sqrt"));
1930 globals.push_back(from_ascii("\\root"));
1931 globals.push_back(from_ascii("\\tabular"));
1932 globals.push_back(from_ascii("\\stackrel"));
1933 globals.push_back(from_ascii("\\binom"));
1934 globals.push_back(from_ascii("\\choose"));
1935 globals.push_back(from_ascii("\\brace"));
1936 globals.push_back(from_ascii("\\brack"));
1937 globals.push_back(from_ascii("\\frac"));
1938 globals.push_back(from_ascii("\\over"));
1939 globals.push_back(from_ascii("\\nicefrac"));
1940 globals.push_back(from_ascii("\\unitfrac"));
1941 globals.push_back(from_ascii("\\unitfracthree"));
1942 globals.push_back(from_ascii("\\unitone"));
1943 globals.push_back(from_ascii("\\unittwo"));
1944 globals.push_back(from_ascii("\\infer"));
1945 globals.push_back(from_ascii("\\atop"));
1946 globals.push_back(from_ascii("\\lefteqn"));
1947 globals.push_back(from_ascii("\\boldsymbol"));
1948 globals.push_back(from_ascii("\\bm"));
1949 globals.push_back(from_ascii("\\color"));
1950 globals.push_back(from_ascii("\\normalcolor"));
1951 globals.push_back(from_ascii("\\textcolor"));
1952 globals.push_back(from_ascii("\\cfrac"));
1953 globals.push_back(from_ascii("\\cfracleft"));
1954 globals.push_back(from_ascii("\\cfracright"));
1955 globals.push_back(from_ascii("\\dfrac"));
1956 globals.push_back(from_ascii("\\tfrac"));
1957 globals.push_back(from_ascii("\\dbinom"));
1958 globals.push_back(from_ascii("\\tbinom"));
1959 globals.push_back(from_ascii("\\hphantom"));
1960 globals.push_back(from_ascii("\\phantom"));
1961 globals.push_back(from_ascii("\\vphantom"));
1962 MathWordList const & words = mathedWordList();
1963 MathWordList::const_iterator it2;
1964 //lyxerr << "Globals completion commands: ";
1965 for (it2 = words.begin(); it2 != words.end(); ++it2) {
1966 globals.push_back("\\" + (*it2).first);
1967 //lyxerr << "\\" + (*it2).first << " ";
1969 //lyxerr << std::endl;
1970 sort(globals.begin(), globals.end());
1974 MathCompletionList::~MathCompletionList()
1979 size_type MathCompletionList::size() const
1981 return locals.size() + globals.size();
1985 docstring const & MathCompletionList::data(size_t idx) const
1987 size_t lsize = locals.size();
1989 return globals[idx - lsize];
1995 std::string MathCompletionList::icon(size_t idx) const
1997 // get the latex command
1999 size_t lsize = locals.size();
2001 cmd = globals[idx - lsize];
2005 // get the icon resource name by stripping the backslash
2006 return "images/math/" + to_utf8(cmd.substr(1)) + ".png";
2009 std::vector<docstring> MathCompletionList::globals;