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 "InsetMathEnsureMath.h"
24 #include "InsetMathHull.h"
25 #include "InsetMathRef.h"
26 #include "InsetMathScript.h"
27 #include "InsetMathSpace.h"
28 #include "InsetMathSymbol.h"
29 #include "InsetMathUnknown.h"
30 #include "MathAutoCorrect.h"
31 #include "MathCompletionList.h"
33 #include "MathFactory.h"
34 #include "MathMacro.h"
35 #include "MathMacroArgument.h"
36 #include "MathParser.h"
37 #include "MathStream.h"
38 #include "MathSupport.h"
42 #include "BufferView.h"
43 #include "CoordCache.h"
45 #include "CutAndPaste.h"
46 #include "DispatchResult.h"
48 #include "FuncRequest.h"
49 #include "FuncStatus.h"
52 #include "OutputParams.h"
55 #include "frontends/Clipboard.h"
56 #include "frontends/Painter.h"
57 #include "frontends/Selection.h"
59 #include "support/lassert.h"
60 #include "support/debug.h"
61 #include "support/gettext.h"
62 #include "support/lstrings.h"
63 #include "support/textutils.h"
64 #include "support/docstream.h"
70 using namespace lyx::support;
74 using cap::copySelection;
75 using cap::grabAndEraseSelection;
76 using cap::cutSelection;
77 using cap::replaceSelection;
78 using cap::selClearOrDel;
81 InsetMathNest::InsetMathNest(idx_type nargs)
82 : cells_(nargs), lock_(false), mouse_hover_(false)
86 InsetMathNest::InsetMathNest(InsetMathNest const & inset)
87 : InsetMath(inset), cells_(inset.cells_), lock_(inset.lock_),
92 InsetMathNest & InsetMathNest::operator=(InsetMathNest const & inset)
94 cells_ = inset.cells_;
97 InsetMath::operator=(inset);
102 InsetMath::idx_type InsetMathNest::nargs() const
104 return cells_.size();
108 void InsetMathNest::cursorPos(BufferView const & bv,
109 CursorSlice const & sl, bool /*boundary*/,
110 int & x, int & y) const
112 // FIXME: This is a hack. Ideally, the coord cache should not store
113 // absolute positions, but relative ones. This would mean to call
114 // setXY() not in MathData::draw(), but in the parent insets' draw()
115 // with the correctly adjusted x,y values. But this means that we'd have
116 // to touch all (math)inset's draw() methods. Right now, we'll store
117 // absolute value, and make them here relative, only to make them
118 // absolute again when actually drawing the cursor. What a mess.
119 LASSERT(&sl.inset() == this, /**/);
120 MathData const & ar = sl.cell();
121 CoordCache const & coord_cache = bv.coordCache();
122 if (!coord_cache.getArrays().has(&ar)) {
123 // this can (semi-)legally happen if we just created this cell
124 // and it never has been drawn before. So don't ASSERT.
125 //lyxerr << "no cached data for array " << &ar << endl;
130 Point const pt = coord_cache.getArrays().xy(&ar);
131 if (!coord_cache.getInsets().has(this)) {
133 //lyxerr << "no cached data for inset " << this << endl;
138 Point const pt2 = coord_cache.getInsets().xy(this);
139 //lyxerr << "retrieving position cache for MathData "
140 // << pt.x_ << ' ' << pt.y_ << endl;
141 x = pt.x_ - pt2.x_ + ar.pos2x(&bv, sl.pos());
143 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
144 // << " asc: " << ascent() << " des: " << descent()
145 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
146 // move cursor visually into empty cells ("blue rectangles");
152 void InsetMathNest::metrics(MetricsInfo const & mi) const
155 for (idx_type i = 0, n = nargs(); i != n; ++i) {
157 cell(i).metrics(m, dim);
162 bool InsetMathNest::idxNext(Cursor & cur) const
164 LASSERT(&cur.inset() == this, /**/);
165 if (cur.idx() == cur.lastidx())
173 bool InsetMathNest::idxForward(Cursor & cur) const
179 bool InsetMathNest::idxPrev(Cursor & cur) const
181 LASSERT(&cur.inset() == this, /**/);
185 cur.pos() = cur.lastpos();
190 bool InsetMathNest::idxBackward(Cursor & cur) const
196 bool InsetMathNest::idxFirst(Cursor & cur) const
198 LASSERT(&cur.inset() == this, /**/);
207 bool InsetMathNest::idxLast(Cursor & cur) const
209 LASSERT(&cur.inset() == this, /**/);
212 cur.idx() = cur.lastidx();
213 cur.pos() = cur.lastpos();
218 void InsetMathNest::dump() const
220 odocstringstream oss;
222 os << "---------------------------------------------\n";
225 for (idx_type i = 0, n = nargs(); i != n; ++i)
226 os << cell(i) << "\n";
227 os << "---------------------------------------------\n";
228 lyxerr << to_utf8(oss.str());
232 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
236 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
239 setPosCache(pi, x, y);
243 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
245 BufferView & bv = *pi.base.bv;
246 // this should use the x/y values given, not the cached values
247 Cursor & cur = bv.cursor();
248 if (!cur.selection())
250 if (&cur.inset() != this)
253 // FIXME: hack to get position cache warm
254 bool const original_drawing_state = pi.pain.isDrawingEnabled();
255 pi.pain.setDrawingEnabled(false);
257 pi.pain.setDrawingEnabled(original_drawing_state);
259 CursorSlice s1 = cur.selBegin();
260 CursorSlice s2 = cur.selEnd();
262 //lyxerr << "InsetMathNest::drawing selection: "
263 // << " s1: " << s1 << " s2: " << s2 << endl;
264 if (s1.idx() == s2.idx()) {
265 MathData const & c = cell(s1.idx());
266 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
267 int x1 = g.pos.x_ + c.pos2x(pi.base.bv, s1.pos());
268 int y1 = g.pos.y_ - g.dim.ascent();
269 int x2 = g.pos.x_ + c.pos2x(pi.base.bv, s2.pos());
270 int y2 = g.pos.y_ + g.dim.descent();
271 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
272 //lyxerr << "InsetMathNest::drawing selection 3: "
273 // << " x1: " << x1 << " x2: " << x2
274 // << " y1: " << y1 << " y2: " << y2 << endl;
276 for (idx_type i = 0; i < nargs(); ++i) {
277 if (idxBetween(i, s1.idx(), s2.idx())) {
278 MathData const & c = cell(i);
279 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
281 int y1 = g.pos.y_ - g.dim.ascent();
282 int x2 = g.pos.x_ + g.dim.width();
283 int y2 = g.pos.y_ + g.dim.descent();
284 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
291 void InsetMathNest::validate(LaTeXFeatures & features) const
293 for (idx_type i = 0; i < nargs(); ++i)
294 cell(i).validate(features);
298 void InsetMathNest::replace(ReplaceData & rep)
300 for (idx_type i = 0; i < nargs(); ++i)
301 cell(i).replace(rep);
305 bool InsetMathNest::contains(MathData const & ar) const
307 for (idx_type i = 0; i < nargs(); ++i)
308 if (cell(i).contains(ar))
314 bool InsetMathNest::lock() const
320 void InsetMathNest::lock(bool l)
326 bool InsetMathNest::isActive() const
332 MathData InsetMathNest::glue() const
335 for (size_t i = 0; i < nargs(); ++i)
341 void InsetMathNest::write(WriteStream & os) const
343 ModeSpecifier specifier(os, currentMode());
344 docstring const latex_name = name();
345 os << '\\' << latex_name;
346 for (size_t i = 0; i < nargs(); ++i)
347 os << '{' << cell(i) << '}';
349 os.pendingSpace(true);
350 if (lock_ && !os.latex()) {
352 os.pendingSpace(true);
357 void InsetMathNest::normalize(NormalStream & os) const
360 for (size_t i = 0; i < nargs(); ++i)
361 os << ' ' << cell(i);
366 int InsetMathNest::latex(odocstream & os, OutputParams const & runparams) const
368 WriteStream wi(os, runparams.moving_arg, true, runparams.dryrun,
375 bool InsetMathNest::setMouseHover(bool mouse_hover)
377 mouse_hover_ = mouse_hover;
382 bool InsetMathNest::notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
386 MathData & ar = cur.cell();
387 // remove base-only "scripts"
388 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
389 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
390 if (p && p->nargs() == 1) {
391 MathData ar = p->nuc();
394 cur.adjust(i, ar.size() - 1);
398 // glue adjacent font insets of the same kind
399 for (pos_type i = 0; i + 1 < size(); ++i) {
400 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
401 InsetMathFont const * q = operator[](i + 1)->asFontInset();
402 if (p && q && p->name() == q->name()) {
403 p->cell(0).append(q->cell(0));
413 void InsetMathNest::handleFont
414 (Cursor & cur, docstring const & arg, char const * const font)
416 handleFont(cur, arg, from_ascii(font));
420 void InsetMathNest::handleFont(Cursor & cur, docstring const & arg,
421 docstring const & font)
423 cur.recordUndoSelection();
425 // this whole function is a hack and won't work for incremental font
427 if (cur.inset().asInsetMath()->name() == font)
428 cur.handleFont(to_utf8(font));
430 handleNest(cur, createInsetMath(font), arg);
434 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest)
436 handleNest(cur, nest, docstring());
440 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest,
441 docstring const & arg)
443 CursorSlice i1 = cur.selBegin();
444 CursorSlice i2 = cur.selEnd();
445 if (!i1.inset().asInsetMath())
447 if (i1.idx() == i2.idx()) {
448 // the easy case where only one cell is selected
449 cur.handleNest(nest);
454 // multiple selected cells in a simple non-grid inset
455 if (i1.asInsetMath()->nrows() == 0 || i1.asInsetMath()->ncols() == 0) {
456 for (idx_type i = i1.idx(); i <= i2.idx(); ++i) {
461 cur.pos() = cur.lastpos();
464 // change font of cell
465 cur.handleNest(nest);
468 // cur is in the font inset now. If the loop continues,
469 // we need to get outside again for the next cell
470 if (i + 1 <= i2.idx())
476 // the complicated case with multiple selected cells in a grid
479 cap::region(i1, i2, r1, r2, c1, c2);
480 for (row_type row = r1; row <= r2; ++row) {
481 for (col_type col = c1; col <= c2; ++col) {
483 cur.idx() = i1.asInsetMath()->index(row, col);
486 cur.pos() = cur.lastpos();
490 cur.handleNest(nest);
493 // cur is in the font inset now. If the loop continues,
494 // we need to get outside again for the next cell
495 if (col + 1 <= c2 || row + 1 <= r2)
502 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
504 cur.recordUndoSelection();
507 font.fromString(to_utf8(arg), b);
508 if (font.fontInfo().color() != Color_inherit &&
509 font.fontInfo().color() != Color_ignore)
510 handleNest(cur, MathAtom(new InsetMathColor(true, font.fontInfo().color())));
512 // FIXME: support other font changes here as well?
516 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
518 //lyxerr << "InsetMathNest: request: " << cmd << endl;
520 Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
522 switch (cmd.action) {
524 case LFUN_CLIPBOARD_PASTE:
525 parseflg |= Parse::VERBATIM;
528 if (cur.currentMode() == TEXT_MODE)
529 parseflg |= Parse::TEXTMODE;
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, parseflg);
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);
636 cur.macroModeClose();
637 // try moving forward or backwards as necessary...
638 if (!(forward ? cursorMathForward(cur) : cursorMathBackward(cur))) {
639 // ... and if movement failed, then finish forward or backwards
641 cmd = FuncRequest(finish_lfun);
649 cur.updateFlags(Update::Decoration | Update::FitCursor);
650 case LFUN_DOWN_SELECT:
651 case LFUN_UP_SELECT: {
652 // close active macro
653 if (cur.inMacroMode()) {
654 cur.macroModeClose();
658 // stop/start the selection
659 bool select = cmd.action == LFUN_DOWN_SELECT ||
660 cmd.action == LFUN_UP_SELECT;
661 cur.selHandle(select);
664 bool up = cmd.action == LFUN_UP || cmd.action == LFUN_UP_SELECT;
665 bool successful = cur.upDownInMath(up);
669 if (cur.fixIfBroken())
670 // FIXME: Something bad happened. We pass the corrected Cursor
671 // instead of letting things go worse.
674 // We did not manage to move the cursor.
679 case LFUN_MOUSE_DOUBLE:
680 case LFUN_MOUSE_TRIPLE:
681 case LFUN_WORD_SELECT:
685 cur.setSelection(true);
686 cur.pos() = cur.lastpos();
687 cur.idx() = cur.lastidx();
690 case LFUN_PARAGRAPH_UP:
691 case LFUN_PARAGRAPH_DOWN:
692 cur.updateFlags(Update::Decoration | Update::FitCursor);
693 case LFUN_PARAGRAPH_UP_SELECT:
694 case LFUN_PARAGRAPH_DOWN_SELECT:
697 case LFUN_LINE_BEGIN:
698 case LFUN_WORD_BACKWARD:
700 cur.updateFlags(Update::Decoration | Update::FitCursor);
701 case LFUN_LINE_BEGIN_SELECT:
702 case LFUN_WORD_BACKWARD_SELECT:
703 case LFUN_WORD_LEFT_SELECT:
704 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
705 cmd.action == LFUN_WORD_LEFT_SELECT ||
706 cmd.action == LFUN_LINE_BEGIN_SELECT);
707 cur.macroModeClose();
708 if (cur.pos() != 0) {
710 } else if (cur.col() != 0) {
711 cur.idx() -= cur.col();
713 } else if (cur.idx() != 0) {
717 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
722 case LFUN_WORD_FORWARD:
723 case LFUN_WORD_RIGHT:
725 cur.updateFlags(Update::Decoration | Update::FitCursor);
726 case LFUN_WORD_FORWARD_SELECT:
727 case LFUN_WORD_RIGHT_SELECT:
728 case LFUN_LINE_END_SELECT:
729 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
730 cmd.action == LFUN_WORD_RIGHT_SELECT ||
731 cmd.action == LFUN_LINE_END_SELECT);
732 cur.macroModeClose();
734 if (cur.pos() != cur.lastpos()) {
735 cur.pos() = cur.lastpos();
736 } else if (ncols() && (cur.col() != cur.lastcol())) {
737 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
738 cur.pos() = cur.lastpos();
739 } else if (cur.idx() != cur.lastidx()) {
740 cur.idx() = cur.lastidx();
741 cur.pos() = cur.lastpos();
743 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
748 case LFUN_CELL_FORWARD:
749 cur.updateFlags(Update::Decoration | Update::FitCursor);
750 cur.inset().idxNext(cur);
753 case LFUN_CELL_BACKWARD:
754 cur.updateFlags(Update::Decoration | Update::FitCursor);
755 cur.inset().idxPrev(cur);
758 case LFUN_WORD_DELETE_BACKWARD:
759 case LFUN_CHAR_DELETE_BACKWARD:
761 // May affect external cell:
762 cur.recordUndoInset();
764 cur.recordUndoSelection();
765 // if the inset can not be removed from within, delete it
766 if (!cur.backspace()) {
767 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
768 cur.innerText()->dispatch(cur, cmd);
772 case LFUN_WORD_DELETE_FORWARD:
773 case LFUN_CHAR_DELETE_FORWARD:
774 if (cur.pos() == cur.lastpos())
775 // May affect external cell:
776 cur.recordUndoInset();
778 cur.recordUndoSelection();
779 // if the inset can not be removed from within, delete it
781 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
782 cur.innerText()->dispatch(cur, cmd);
788 cur.clearSelection();
790 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
795 // 'Locks' the math inset. A 'locked' math inset behaves as a unit
796 // that is traversed by a single <CursorLeft>/<CursorRight>.
797 case LFUN_INSET_TOGGLE:
803 case LFUN_SELF_INSERT:
804 if (cmd.argument().size() != 1) {
805 cur.recordUndoSelection();
806 docstring const arg = cmd.argument();
807 if (!interpretString(cur, arg))
811 // Don't record undo steps if we are in macro mode and
812 // cmd.argument is the next character of the macro name.
813 // Otherwise we'll get an invalid cursor if we undo after
814 // the macro was finished and the macro is a known command,
815 // e.g. sqrt. Cursor::macroModeClose replaces in this case
816 // the InsetMathUnknown with name "frac" by an empty
817 // InsetMathFrac -> a pos value > 0 is invalid.
818 // A side effect is that an undo before the macro is finished
819 // undoes the complete macro, not only the last character.
820 if (!cur.inMacroMode()) {
821 MathMacro const * macro = 0;
822 if (cur.pos() > 0 && cmd.argument() != "\\")
823 macro = cur.inset().asInsetMath()->asMacro();
826 cur.recordUndoSelection();
829 // spacial handling of space. If we insert an inset
830 // via macro mode, we want to put the cursor inside it
831 // if relevant. Think typing "\frac<space>".
832 if (cmd.argument()[0] == ' '
833 && cur.inMacroMode() && cur.macroName() != "\\"
834 && cur.macroModeClose()) {
835 MathAtom const atom = cur.prevAtom();
836 if (atom->asNestInset() && atom->isActive()) {
838 cur.pushBackward(*cur.nextInset());
840 } else if (!interpretChar(cur, cmd.argument()[0])) {
841 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
846 //case LFUN_SERVER_GET_XY:
849 case LFUN_SERVER_SET_XY: {
850 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
853 istringstream is(to_utf8(cmd.argument()));
855 cur.setScreenPos(x, y);
859 // Special casing for superscript in case of LyX handling
861 case LFUN_ACCENT_CIRCUMFLEX:
862 if (cmd.argument().empty()) {
863 // do superscript if LyX handles
865 cur.recordUndoSelection();
866 script(cur, true, grabAndEraseSelection(cur));
870 case LFUN_ACCENT_UMLAUT:
871 case LFUN_ACCENT_ACUTE:
872 case LFUN_ACCENT_GRAVE:
873 case LFUN_ACCENT_BREVE:
874 case LFUN_ACCENT_DOT:
875 case LFUN_ACCENT_MACRON:
876 case LFUN_ACCENT_CARON:
877 case LFUN_ACCENT_TILDE:
878 case LFUN_ACCENT_CEDILLA:
879 case LFUN_ACCENT_CIRCLE:
880 case LFUN_ACCENT_UNDERDOT:
881 case LFUN_ACCENT_TIE:
882 case LFUN_ACCENT_OGONEK:
883 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
887 case LFUN_TEXTSTYLE_APPLY:
888 case LFUN_TEXTSTYLE_UPDATE:
889 handleFont2(cur, cmd.argument());
893 if (currentMode() == TEXT_MODE)
894 handleFont(cur, cmd.argument(), "textbf");
896 handleFont(cur, cmd.argument(), "mathbf");
898 case LFUN_FONT_BOLDSYMBOL:
899 if (currentMode() == TEXT_MODE)
900 handleFont(cur, cmd.argument(), "textbf");
902 handleFont(cur, cmd.argument(), "boldsymbol");
905 if (currentMode() == TEXT_MODE)
906 handleFont(cur, cmd.argument(), "textsf");
908 handleFont(cur, cmd.argument(), "mathsf");
911 if (currentMode() == TEXT_MODE)
912 handleFont(cur, cmd.argument(), "emph");
914 handleFont(cur, cmd.argument(), "mathcal");
916 case LFUN_FONT_ROMAN:
917 if (currentMode() == TEXT_MODE)
918 handleFont(cur, cmd.argument(), "textrm");
920 handleFont(cur, cmd.argument(), "mathrm");
922 case LFUN_FONT_TYPEWRITER:
923 if (currentMode() == TEXT_MODE)
924 handleFont(cur, cmd.argument(), "texttt");
926 handleFont(cur, cmd.argument(), "mathtt");
929 handleFont(cur, cmd.argument(), "mathfrak");
932 if (currentMode() == TEXT_MODE)
933 handleFont(cur, cmd.argument(), "textit");
935 handleFont(cur, cmd.argument(), "mathit");
938 if (currentMode() == TEXT_MODE)
939 // FIXME: should be "noun"
940 handleFont(cur, cmd.argument(), "textsc");
942 handleFont(cur, cmd.argument(), "mathbb");
944 case LFUN_FONT_DEFAULT:
945 handleFont(cur, cmd.argument(), "textnormal");
948 case LFUN_MATH_MODE: {
950 // ignore math-mode on when already in math mode
951 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
953 cur.recordUndoSelection();
954 cur.macroModeClose();
955 docstring const save_selection = grabAndEraseSelection(cur);
957 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
958 if (currentMode() == Inset::TEXT_MODE)
959 cur.plainInsert(MathAtom(new InsetMathEnsureMath));
961 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
963 cur.pushBackward(*cur.nextInset());
964 cur.niceInsert(save_selection);
966 if (currentMode() == Inset::TEXT_MODE) {
967 cur.recordUndoSelection();
968 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
969 cur.message(_("create new math text environment ($...$)"));
971 handleFont(cur, cmd.argument(), "textrm");
972 cur.message(_("entered math text mode (textrm)"));
978 case LFUN_REGEXP_MODE: {
979 InsetMathHull * i = dynamic_cast<InsetMathHull *>(cur.inset().asInsetMath());
980 if (i && i->getType() == hullRegexp) {
981 cur.message(_("Already in regexp mode"));
984 cur.macroModeClose();
985 docstring const save_selection = grabAndEraseSelection(cur);
987 cur.plainInsert(MathAtom(new InsetMathHull(hullRegexp)));
989 cur.pushBackward(*cur.nextInset());
990 cur.niceInsert(save_selection);
991 cur.message(_("Regexp editor mode"));
995 case LFUN_MATH_FONT_STYLE: {
996 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, '\\' + cmd.argument());
1001 case LFUN_MATH_SIZE: {
1002 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, cmd.argument());
1003 doDispatch(cur, fr);
1007 case LFUN_MATH_MATRIX: {
1013 idocstringstream is(cmd.argument());
1014 is >> m >> n >> v_align >> h_align;
1021 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
1025 case LFUN_MATH_DELIM: {
1027 docstring rs = split(cmd.argument(), ls, ' ');
1028 // Reasonable default values
1034 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
1038 case LFUN_MATH_BIGDELIM: {
1039 docstring const lname = from_utf8(cmd.getArg(0));
1040 docstring const ldelim = from_utf8(cmd.getArg(1));
1041 docstring const rname = from_utf8(cmd.getArg(2));
1042 docstring const rdelim = from_utf8(cmd.getArg(3));
1043 latexkeys const * l = in_word_set(lname);
1044 bool const have_l = l && l->inset == "big" &&
1045 InsetMathBig::isBigInsetDelim(ldelim);
1046 l = in_word_set(rname);
1047 bool const have_r = l && l->inset == "big" &&
1048 InsetMathBig::isBigInsetDelim(rdelim);
1049 // We mimic LFUN_MATH_DELIM in case we have an empty left
1050 // or right delimiter.
1051 if (have_l || have_r) {
1053 docstring const selection = grabAndEraseSelection(cur);
1056 cur.insert(MathAtom(new InsetMathBig(lname,
1058 cur.niceInsert(selection);
1060 cur.insert(MathAtom(new InsetMathBig(rname,
1063 // Don't call cur.undispatched() if we did nothing, this would
1064 // lead to infinite recursion via Text::dispatch().
1068 case LFUN_SPACE_INSERT:
1069 cur.recordUndoSelection();
1070 cur.insert(MathAtom(new InsetMathSpace));
1073 case LFUN_MATH_SPACE:
1074 cur.recordUndoSelection();
1075 if (cmd.argument().empty())
1076 cur.insert(MathAtom(new InsetMathSpace));
1078 string const name = cmd.getArg(0);
1079 string const len = cmd.getArg(1);
1080 cur.insert(MathAtom(new InsetMathSpace(name, len)));
1084 case LFUN_ERT_INSERT:
1085 // interpret this as if a backslash was typed
1087 interpretChar(cur, '\\');
1090 case LFUN_MATH_SUBSCRIPT:
1091 // interpret this as if a _ was typed
1092 cur.recordUndoSelection();
1093 interpretChar(cur, '_');
1096 case LFUN_MATH_SUPERSCRIPT:
1097 // interpret this as if a ^ was typed
1098 cur.recordUndoSelection();
1099 interpretChar(cur, '^');
1102 case LFUN_MATH_MACRO_FOLD:
1103 case LFUN_MATH_MACRO_UNFOLD: {
1105 bool fold = cmd.action == LFUN_MATH_MACRO_FOLD;
1106 bool found = findMacroToFoldUnfold(it, fold);
1108 MathMacro * macro = it.nextInset()->asInsetMath()->asMacro();
1109 cur.recordUndoInset();
1118 case LFUN_QUOTE_INSERT:
1119 // interpret this as if a straight " was typed
1120 cur.recordUndoSelection();
1121 interpretChar(cur, '\"');
1124 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
1125 // handling such that "self-insert" works on "arbitrary stuff" too, and
1126 // math-insert only handles special math things like "matrix".
1127 case LFUN_MATH_INSERT: {
1128 cur.recordUndoSelection();
1129 if (cmd.argument() == "^" || cmd.argument() == "_")
1130 interpretChar(cur, cmd.argument()[0]);
1133 asArray(cmd.argument(), ar);
1134 if (ar.size() == 1 && ar[0]->asNestInset()
1135 && ar[0]->asNestInset()->nargs() > 1)
1136 handleNest(cur, ar[0]);
1138 cur.niceInsert(cmd.argument());
1143 case LFUN_DIALOG_SHOW_NEW_INSET: {
1144 docstring const & name = cmd.argument();
1146 if (name == "ref") {
1147 InsetMathRef tmp(name);
1148 data = tmp.createDialogStr(to_utf8(name));
1149 } else if (name == "mathspace") {
1151 data = tmp.createDialogStr();
1153 cur.bv().showDialog(to_utf8(name), data);
1157 case LFUN_INSET_INSERT: {
1159 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1160 cur.recordUndoSelection();
1166 case LFUN_INSET_DISSOLVE:
1167 if (!asHullInset()) {
1168 cur.recordUndoInset();
1174 InsetMath::doDispatch(cur, cmd);
1180 bool InsetMathNest::findMacroToFoldUnfold(Cursor & it, bool fold) const {
1181 // look for macro to open/close, but stay in mathed
1182 for (; !it.empty(); it.pop_back()) {
1184 // go backward through the current cell
1185 Inset * inset = it.nextInset();
1186 while (inset && inset->asInsetMath()) {
1187 MathMacro * macro = inset->asInsetMath()->asMacro();
1189 // found the an macro to open/close?
1190 if (macro->folded() != fold)
1193 // Wrong folding state.
1194 // If this was the first we see in this slice, look further left,
1196 if (inset != it.nextInset())
1200 // go up if this was the left most position
1206 inset = it.nextInset();
1214 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1215 FuncStatus & flag) const
1217 // the font related toggles
1218 //string tc = "mathnormal";
1220 string const arg = to_utf8(cmd.argument());
1221 switch (cmd.action) {
1222 case LFUN_TABULAR_FEATURE:
1223 flag.setEnabled(false);
1226 case LFUN_TABULAR_FEATURE:
1227 // FIXME: check temporarily disabled
1229 char align = mathcursor::valign();
1230 if (align == '\0') {
1234 if (cmd.argument().empty()) {
1238 if (!contains("tcb", cmd.argument()[0])) {
1242 flag.setOnOff(cmd.argument()[0] == align);
1245 /// We have to handle them since 1.4 blocks all unhandled actions
1246 case LFUN_FONT_ITAL:
1247 case LFUN_FONT_BOLD:
1248 case LFUN_FONT_BOLDSYMBOL:
1249 case LFUN_FONT_SANS:
1250 case LFUN_FONT_EMPH:
1251 case LFUN_FONT_TYPEWRITER:
1252 case LFUN_FONT_NOUN:
1253 case LFUN_FONT_ROMAN:
1254 case LFUN_FONT_DEFAULT:
1255 flag.setEnabled(true);
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)));
1571 if (cur.pos() > 0) {
1572 MathMacro const * macro = cur.inset().asInsetMath()->asMacro();
1574 // resume macro_mode
1575 docstring const & s = macro->name();
1576 cur.leaveInset(*macro);
1578 docstring safe = grabAndEraseSelection(cur);
1579 cur.insert(MathAtom(new InsetMathUnknown("\\" + s + c, safe, false)));
1587 if (currentMode() == InsetMath::TEXT_MODE)
1593 if (currentMode() == InsetMath::TEXT_MODE) {
1594 // insert spaces in text mode,
1595 // but suppress direct insertion of two spaces in a row
1596 // the still allows typing '<space>a<space>' and deleting the 'a', but
1597 // it is better than nothing...
1598 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1600 // FIXME: we have to enable full redraw here because of the
1601 // visual box corners that define the inset. If we know for
1602 // sure that we stay within the same cell we can optimize for
1604 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1608 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1609 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1610 // FIXME: we have to enable full redraw here because of the
1611 // visual box corners that define the inset. If we know for
1612 // sure that we stay within the same cell we can optimize for
1614 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1618 if (cur.popForward()) {
1619 // FIXME: we have to enable full redraw here because of the
1620 // visual box corners that define the inset. If we know for
1621 // sure that we stay within the same cell we can optimize for
1623 //cur.updateFlags(Update::FitCursor);
1627 // if we are at the very end, leave the formula
1628 return cur.pos() != cur.lastpos();
1631 // These should be treated differently when not in text mode:
1632 if (currentMode() != InsetMath::TEXT_MODE) {
1634 script(cur, false, save_selection);
1638 script(cur, true, save_selection);
1642 cur.niceInsert(createInsetMath("sim"));
1645 if (!isAsciiOrMathAlpha(c)) {
1646 MathAtom at = createInsetMath("text");
1647 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
1654 cur.niceInsert(createInsetMath("textasciicircum"));
1658 cur.niceInsert(createInsetMath("textasciitilde"));
1663 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1664 c == '%' || c == '_') {
1665 cur.niceInsert(createInsetMath(docstring(1, c)));
1670 // try auto-correction
1671 if (lyxrc.autocorrection_math && cur.autocorrect() && cur.pos() != 0
1672 && math_autocorrect(cur.prevAtom(), c))
1675 // no special circumstances, so insert the character without any fuss
1677 if (lyxrc.autocorrection_math) {
1678 if (!cur.autocorrect())
1679 cur.message(_("Autocorrect Off ('!' to enter)"));
1681 cur.message(_("Autocorrect On (<space> to exit)"));
1687 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1689 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1691 if (!cur.empty() && cur.pos() > 0 &&
1692 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1693 if (InsetMathBig::isBigInsetDelim(str)) {
1694 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1695 if (prev[0] == '\\') {
1696 prev = prev.substr(1);
1697 latexkeys const * l = in_word_set(prev);
1698 if (l && l->inset == "big") {
1699 cur.cell()[cur.pos() - 1] =
1700 MathAtom(new InsetMathBig(prev, str));
1710 bool InsetMathNest::script(Cursor & cur, bool up)
1712 return script(cur, up, docstring());
1716 bool InsetMathNest::script(Cursor & cur, bool up,
1717 docstring const & save_selection)
1719 // Hack to get \^ and \_ working
1720 //lyxerr << "handling script: up: " << up << endl;
1721 if (cur.inMacroMode() && cur.macroName() == "\\") {
1723 cur.niceInsert(createInsetMath("mathcircumflex"));
1725 interpretChar(cur, '_');
1729 cur.macroModeClose();
1730 if (asScriptInset() && cur.idx() == 0) {
1731 // we are in a nucleus of a script inset, move to _our_ script
1732 InsetMathScript * inset = asScriptInset();
1733 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1735 cur.idx() = inset->idxOfScript(up);
1737 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1739 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1742 cur.idx() = inset->idxOfScript(up);
1743 cur.pos() = cur.lastpos();
1745 // convert the thing to our left to a scriptinset or create a new
1746 // one if in the very first position of the array
1747 if (cur.pos() == 0) {
1748 //lyxerr << "new scriptinset" << endl;
1749 cur.insert(new InsetMathScript(up));
1751 //lyxerr << "converting prev atom " << endl;
1752 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1755 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1756 // See comment in MathParser.cpp for special handling of {}-bases
1762 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1763 cur.niceInsert(save_selection);
1765 //lyxerr << "inserting selection 2:\n" << save_selection << endl;
1770 bool InsetMathNest::completionSupported(Cursor const & cur) const
1772 return cur.inMacroMode();
1776 bool InsetMathNest::inlineCompletionSupported(Cursor const & cur) const
1778 return cur.inMacroMode();
1782 bool InsetMathNest::automaticInlineCompletion() const
1784 return lyxrc.completion_inline_math;
1788 bool InsetMathNest::automaticPopupCompletion() const
1790 return lyxrc.completion_popup_math;
1794 CompletionList const *
1795 InsetMathNest::createCompletionList(Cursor const & cur) const
1797 if (!cur.inMacroMode())
1800 return new MathCompletionList(cur);
1804 docstring InsetMathNest::completionPrefix(Cursor const & cur) const
1806 if (!cur.inMacroMode())
1809 return cur.activeMacro()->name();
1813 bool InsetMathNest::insertCompletion(Cursor & cur, docstring const & s,
1816 if (!cur.inMacroMode())
1819 // append completion to active macro
1820 InsetMathUnknown * inset = cur.activeMacro();
1821 inset->setName(inset->name() + s);
1826 // FIXME: this creates duplicates in the completion popup
1827 // which looks ugly. Moreover the changes the list lengths
1829 confuse the popup as well.
1830 MathCompletionList::addToFavorites(inset->name());
1832 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, " "));
1839 void InsetMathNest::completionPosAndDim(Cursor const & cur, int & x, int & y,
1840 Dimension & dim) const
1842 Inset const * inset = cur.activeMacro();
1846 // get inset dimensions
1847 dim = cur.bv().coordCache().insets().dim(inset);
1848 // FIXME: these 3 are no accurate, but should depend on the font.
1849 // Now the popup jumps down if you enter a char with descent > 0.
1855 = cur.bv().coordCache().insets().xy(inset);
1861 bool InsetMathNest::cursorMathForward(Cursor & cur)
1863 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
1864 cur.pushBackward(*cur.nextAtom().nucleus());
1865 cur.inset().idxFirst(cur);
1868 if (cur.posForward() || idxForward(cur))
1870 // try to pop forwards --- but don't pop out of math! leave that to
1872 int s = cur.depth() - 2;
1873 if (s >= 0 && cur[s].inset().asInsetMath())
1874 return cur.popForward();
1879 bool InsetMathNest::cursorMathBackward(Cursor & cur)
1881 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
1883 cur.push(*cur.nextAtom().nucleus());
1884 cur.inset().idxLast(cur);
1887 if (cur.posBackward() || idxBackward(cur))
1889 // try to pop backwards --- but don't pop out of math! leave that to
1891 int s = cur.depth() - 2;
1892 if (s >= 0 && cur[s].inset().asInsetMath())
1893 return cur.popBackward();
1898 ////////////////////////////////////////////////////////////////////
1900 MathCompletionList::MathCompletionList(Cursor const & cur)
1902 // fill it with macros from the buffer
1903 MacroNameSet macros;
1904 cur.buffer()->listMacroNames(macros);
1905 MacroNameSet::const_iterator it;
1906 for (it = macros.begin(); it != macros.end(); ++it) {
1907 if (cur.buffer()->getMacro(*it, cur, false))
1908 locals.push_back("\\" + *it);
1910 sort(locals.begin(), locals.end());
1912 if (globals.size() > 0)
1915 // fill in global macros
1917 MacroTable::globalMacros().getMacroNames(macros);
1918 //lyxerr << "Globals completion macros: ";
1919 for (it = macros.begin(); it != macros.end(); ++it) {
1920 //lyxerr << "\\" + *it << " ";
1921 globals.push_back("\\" + *it);
1923 //lyxerr << std::endl;
1925 // fill in global commands
1926 globals.push_back(from_ascii("\\boxed"));
1927 globals.push_back(from_ascii("\\fbox"));
1928 globals.push_back(from_ascii("\\framebox"));
1929 globals.push_back(from_ascii("\\makebox"));
1930 globals.push_back(from_ascii("\\kern"));
1931 globals.push_back(from_ascii("\\xrightarrow"));
1932 globals.push_back(from_ascii("\\xleftarrow"));
1933 globals.push_back(from_ascii("\\split"));
1934 globals.push_back(from_ascii("\\gathered"));
1935 globals.push_back(from_ascii("\\aligned"));
1936 globals.push_back(from_ascii("\\alignedat"));
1937 globals.push_back(from_ascii("\\cases"));
1938 globals.push_back(from_ascii("\\substack"));
1939 globals.push_back(from_ascii("\\xymatrix"));
1940 globals.push_back(from_ascii("\\subarray"));
1941 globals.push_back(from_ascii("\\array"));
1942 globals.push_back(from_ascii("\\sqrt"));
1943 globals.push_back(from_ascii("\\root"));
1944 globals.push_back(from_ascii("\\tabular"));
1945 globals.push_back(from_ascii("\\stackrel"));
1946 globals.push_back(from_ascii("\\binom"));
1947 globals.push_back(from_ascii("\\choose"));
1948 globals.push_back(from_ascii("\\brace"));
1949 globals.push_back(from_ascii("\\brack"));
1950 globals.push_back(from_ascii("\\frac"));
1951 globals.push_back(from_ascii("\\over"));
1952 globals.push_back(from_ascii("\\nicefrac"));
1953 globals.push_back(from_ascii("\\unitfrac"));
1954 globals.push_back(from_ascii("\\unitfracthree"));
1955 globals.push_back(from_ascii("\\unitone"));
1956 globals.push_back(from_ascii("\\unittwo"));
1957 globals.push_back(from_ascii("\\infer"));
1958 globals.push_back(from_ascii("\\atop"));
1959 globals.push_back(from_ascii("\\lefteqn"));
1960 globals.push_back(from_ascii("\\boldsymbol"));
1961 globals.push_back(from_ascii("\\bm"));
1962 globals.push_back(from_ascii("\\color"));
1963 globals.push_back(from_ascii("\\normalcolor"));
1964 globals.push_back(from_ascii("\\textcolor"));
1965 globals.push_back(from_ascii("\\cfrac"));
1966 globals.push_back(from_ascii("\\cfracleft"));
1967 globals.push_back(from_ascii("\\cfracright"));
1968 globals.push_back(from_ascii("\\dfrac"));
1969 globals.push_back(from_ascii("\\tfrac"));
1970 globals.push_back(from_ascii("\\dbinom"));
1971 globals.push_back(from_ascii("\\tbinom"));
1972 globals.push_back(from_ascii("\\hphantom"));
1973 globals.push_back(from_ascii("\\phantom"));
1974 globals.push_back(from_ascii("\\vphantom"));
1975 MathWordList const & words = mathedWordList();
1976 MathWordList::const_iterator it2;
1977 //lyxerr << "Globals completion commands: ";
1978 for (it2 = words.begin(); it2 != words.end(); ++it2) {
1979 globals.push_back("\\" + (*it2).first);
1980 //lyxerr << "\\" + (*it2).first << " ";
1982 //lyxerr << std::endl;
1983 sort(globals.begin(), globals.end());
1987 MathCompletionList::~MathCompletionList()
1992 size_type MathCompletionList::size() const
1994 return locals.size() + globals.size();
1998 docstring const & MathCompletionList::data(size_t idx) const
2000 size_t lsize = locals.size();
2002 return globals[idx - lsize];
2008 std::string MathCompletionList::icon(size_t idx) const
2010 // get the latex command
2012 size_t lsize = locals.size();
2014 cmd = globals[idx - lsize];
2018 // get the icon resource name by stripping the backslash
2019 return "images/math/" + to_utf8(cmd.substr(1)) + ".png";
2022 std::vector<docstring> MathCompletionList::globals;