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 "InsetMathAMSArray.h"
17 #include "InsetMathBig.h"
18 #include "InsetMathBox.h"
19 #include "InsetMathBrace.h"
20 #include "InsetMathChar.h"
21 #include "InsetMathColor.h"
22 #include "InsetMathComment.h"
23 #include "InsetMathDelim.h"
24 #include "InsetMathEnsureMath.h"
25 #include "InsetMathHull.h"
26 #include "InsetMathRef.h"
27 #include "InsetMathScript.h"
28 #include "InsetMathSpace.h"
29 #include "InsetMathSymbol.h"
30 #include "InsetMathUnknown.h"
31 #include "MathAutoCorrect.h"
32 #include "MathCompletionList.h"
34 #include "MathFactory.h"
35 #include "MathMacro.h"
36 #include "MathMacroArgument.h"
37 #include "MathParser.h"
38 #include "MathStream.h"
39 #include "MathSupport.h"
43 #include "BufferView.h"
44 #include "CoordCache.h"
46 #include "CutAndPaste.h"
47 #include "DispatchResult.h"
49 #include "FuncRequest.h"
50 #include "FuncStatus.h"
53 #include "OutputParams.h"
56 #include "frontends/Clipboard.h"
57 #include "frontends/Painter.h"
58 #include "frontends/Selection.h"
60 #include "support/lassert.h"
61 #include "support/debug.h"
62 #include "support/gettext.h"
63 #include "support/lstrings.h"
64 #include "support/textutils.h"
65 #include "support/docstream.h"
71 using namespace lyx::support;
75 using cap::copySelection;
76 using cap::grabAndEraseSelection;
77 using cap::cutSelection;
78 using cap::replaceSelection;
79 using cap::selClearOrDel;
82 InsetMathNest::InsetMathNest(idx_type nargs)
83 : cells_(nargs), lock_(false), mouse_hover_(false)
87 InsetMathNest::InsetMathNest(InsetMathNest const & inset)
88 : InsetMath(inset), cells_(inset.cells_), lock_(inset.lock_),
93 InsetMathNest & InsetMathNest::operator=(InsetMathNest const & inset)
95 cells_ = inset.cells_;
98 InsetMath::operator=(inset);
103 void InsetMathNest::setBuffer(Buffer & buffer)
105 InsetMath::setBuffer(buffer);
106 for (idx_type i = 0, n = nargs(); i != n; ++i) {
107 MathData & data = cell(i);
108 for (size_t j = 0; j != data.size(); ++j)
109 data[j].nucleus()->setBuffer(buffer);
114 InsetMath::idx_type InsetMathNest::nargs() const
116 return cells_.size();
120 void InsetMathNest::cursorPos(BufferView const & bv,
121 CursorSlice const & sl, bool /*boundary*/,
122 int & x, int & y) const
124 // FIXME: This is a hack. Ideally, the coord cache should not store
125 // absolute positions, but relative ones. This would mean to call
126 // setXY() not in MathData::draw(), but in the parent insets' draw()
127 // with the correctly adjusted x,y values. But this means that we'd have
128 // to touch all (math)inset's draw() methods. Right now, we'll store
129 // absolute value, and make them here relative, only to make them
130 // absolute again when actually drawing the cursor. What a mess.
131 LASSERT(&sl.inset() == this, /**/);
132 MathData const & ar = sl.cell();
133 CoordCache const & coord_cache = bv.coordCache();
134 if (!coord_cache.getArrays().has(&ar)) {
135 // this can (semi-)legally happen if we just created this cell
136 // and it never has been drawn before. So don't ASSERT.
137 //lyxerr << "no cached data for array " << &ar << endl;
142 Point const pt = coord_cache.getArrays().xy(&ar);
143 if (!coord_cache.getInsets().has(this)) {
145 //lyxerr << "no cached data for inset " << this << endl;
150 Point const pt2 = coord_cache.getInsets().xy(this);
151 //lyxerr << "retrieving position cache for MathData "
152 // << pt.x_ << ' ' << pt.y_ << endl;
153 x = pt.x_ - pt2.x_ + ar.pos2x(&bv, sl.pos());
155 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
156 // << " asc: " << ascent() << " des: " << descent()
157 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
158 // move cursor visually into empty cells ("blue rectangles");
164 void InsetMathNest::metrics(MetricsInfo const & mi) const
167 for (idx_type i = 0, n = nargs(); i != n; ++i) {
169 cell(i).metrics(m, dim);
174 bool InsetMathNest::idxNext(Cursor & cur) const
176 LASSERT(&cur.inset() == this, /**/);
177 if (cur.idx() == cur.lastidx())
185 bool InsetMathNest::idxForward(Cursor & cur) const
191 bool InsetMathNest::idxPrev(Cursor & cur) const
193 LASSERT(&cur.inset() == this, /**/);
197 cur.pos() = cur.lastpos();
202 bool InsetMathNest::idxBackward(Cursor & cur) const
208 bool InsetMathNest::idxFirst(Cursor & cur) const
210 LASSERT(&cur.inset() == this, /**/);
219 bool InsetMathNest::idxLast(Cursor & cur) const
221 LASSERT(&cur.inset() == this, /**/);
224 cur.idx() = cur.lastidx();
225 cur.pos() = cur.lastpos();
230 void InsetMathNest::dump() const
232 odocstringstream oss;
234 os << "---------------------------------------------\n";
237 for (idx_type i = 0, n = nargs(); i != n; ++i)
238 os << cell(i) << "\n";
239 os << "---------------------------------------------\n";
240 lyxerr << to_utf8(oss.str());
244 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
248 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
251 setPosCache(pi, x, y);
255 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
257 BufferView & bv = *pi.base.bv;
258 // this should use the x/y values given, not the cached values
259 Cursor & cur = bv.cursor();
260 if (!cur.selection())
262 if (&cur.inset() != this)
265 // FIXME: hack to get position cache warm
266 bool const original_drawing_state = pi.pain.isDrawingEnabled();
267 pi.pain.setDrawingEnabled(false);
269 pi.pain.setDrawingEnabled(original_drawing_state);
271 CursorSlice s1 = cur.selBegin();
272 CursorSlice s2 = cur.selEnd();
274 //lyxerr << "InsetMathNest::drawing selection: "
275 // << " s1: " << s1 << " s2: " << s2 << endl;
276 if (s1.idx() == s2.idx()) {
277 MathData const & c = cell(s1.idx());
278 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
279 int x1 = g.pos.x_ + c.pos2x(pi.base.bv, s1.pos());
280 int y1 = g.pos.y_ - g.dim.ascent();
281 int x2 = g.pos.x_ + c.pos2x(pi.base.bv, s2.pos());
282 int y2 = g.pos.y_ + g.dim.descent();
283 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
284 //lyxerr << "InsetMathNest::drawing selection 3: "
285 // << " x1: " << x1 << " x2: " << x2
286 // << " y1: " << y1 << " y2: " << y2 << endl;
288 for (idx_type i = 0; i < nargs(); ++i) {
289 if (idxBetween(i, s1.idx(), s2.idx())) {
290 MathData const & c = cell(i);
291 Geometry const & g = bv.coordCache().getArrays().geometry(&c);
293 int y1 = g.pos.y_ - g.dim.ascent();
294 int x2 = g.pos.x_ + g.dim.width();
295 int y2 = g.pos.y_ + g.dim.descent();
296 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
303 void InsetMathNest::validate(LaTeXFeatures & features) const
305 for (idx_type i = 0; i < nargs(); ++i)
306 cell(i).validate(features);
310 void InsetMathNest::replace(ReplaceData & rep)
312 for (idx_type i = 0; i < nargs(); ++i)
313 cell(i).replace(rep);
317 bool InsetMathNest::contains(MathData const & ar) const
319 for (idx_type i = 0; i < nargs(); ++i)
320 if (cell(i).contains(ar))
326 bool InsetMathNest::lock() const
332 void InsetMathNest::lock(bool l)
338 bool InsetMathNest::isActive() const
344 MathData InsetMathNest::glue() const
347 for (size_t i = 0; i < nargs(); ++i)
353 void InsetMathNest::write(WriteStream & os) const
355 ModeSpecifier specifier(os, currentMode(), lockedMode());
356 docstring const latex_name = name();
357 os << '\\' << latex_name;
358 for (size_t i = 0; i < nargs(); ++i)
359 os << '{' << cell(i) << '}';
361 os.pendingSpace(true);
362 if (lock_ && !os.latex()) {
364 os.pendingSpace(true);
369 void InsetMathNest::normalize(NormalStream & os) const
372 for (size_t i = 0; i < nargs(); ++i)
373 os << ' ' << cell(i);
378 int InsetMathNest::latex(odocstream & os, OutputParams const & runparams) const
380 WriteStream wi(os, runparams.moving_arg, true,
381 runparams.dryrun ? WriteStream::wsDryrun : WriteStream::wsDefault,
388 bool InsetMathNest::setMouseHover(bool mouse_hover)
390 mouse_hover_ = mouse_hover;
395 bool InsetMathNest::notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
399 MathData & ar = cur.cell();
400 // remove base-only "scripts"
401 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
402 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
403 if (p && p->nargs() == 1) {
404 MathData ar = p->nuc();
407 cur.adjust(i, ar.size() - 1);
411 // glue adjacent font insets of the same kind
412 for (pos_type i = 0; i + 1 < size(); ++i) {
413 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
414 InsetMathFont const * q = operator[](i + 1)->asFontInset();
415 if (p && q && p->name() == q->name()) {
416 p->cell(0).append(q->cell(0));
426 void InsetMathNest::handleFont
427 (Cursor & cur, docstring const & arg, char const * const font)
429 handleFont(cur, arg, from_ascii(font));
433 void InsetMathNest::handleFont(Cursor & cur, docstring const & arg,
434 docstring const & font)
436 cur.recordUndoSelection();
438 // this whole function is a hack and won't work for incremental font
440 if (cur.inset().asInsetMath()->name() == font)
441 cur.handleFont(to_utf8(font));
443 handleNest(cur, createInsetMath(font), arg);
447 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest)
449 handleNest(cur, nest, docstring());
453 void InsetMathNest::handleNest(Cursor & cur, MathAtom const & nest,
454 docstring const & arg)
456 CursorSlice i1 = cur.selBegin();
457 CursorSlice i2 = cur.selEnd();
458 if (!i1.inset().asInsetMath())
460 if (i1.idx() == i2.idx()) {
461 // the easy case where only one cell is selected
462 cur.handleNest(nest);
467 // multiple selected cells in a simple non-grid inset
468 if (i1.asInsetMath()->nrows() == 0 || i1.asInsetMath()->ncols() == 0) {
469 for (idx_type i = i1.idx(); i <= i2.idx(); ++i) {
474 cur.pos() = cur.lastpos();
477 // change font of cell
478 cur.handleNest(nest);
481 // cur is in the font inset now. If the loop continues,
482 // we need to get outside again for the next cell
483 if (i + 1 <= i2.idx())
489 // the complicated case with multiple selected cells in a grid
492 cap::region(i1, i2, r1, r2, c1, c2);
493 for (row_type row = r1; row <= r2; ++row) {
494 for (col_type col = c1; col <= c2; ++col) {
496 cur.idx() = i1.asInsetMath()->index(row, col);
499 cur.pos() = cur.lastpos();
503 cur.handleNest(nest);
506 // cur is in the font inset now. If the loop continues,
507 // we need to get outside again for the next cell
508 if (col + 1 <= c2 || row + 1 <= r2)
515 void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
517 cur.recordUndoSelection();
520 font.fromString(to_utf8(arg), b);
521 if (font.fontInfo().color() != Color_inherit &&
522 font.fontInfo().color() != Color_ignore)
523 handleNest(cur, MathAtom(new InsetMathColor(true, font.fontInfo().color())));
525 // FIXME: support other font changes here as well?
529 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
531 //lyxerr << "InsetMathNest: request: " << cmd << endl;
533 Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
535 switch (cmd.action) {
537 case LFUN_CLIPBOARD_PASTE:
538 parseflg |= Parse::VERBATIM;
541 if (cur.currentMode() <= TEXT_MODE)
542 parseflg |= Parse::TEXTMODE;
543 cur.recordUndoSelection();
544 cur.message(_("Paste"));
545 replaceSelection(cur);
547 if (cmd.argument().empty() && !theClipboard().isInternal())
548 topaste = theClipboard().getAsText();
551 idocstringstream is(cmd.argument());
553 topaste = cap::selection(n);
555 cur.niceInsert(topaste, parseflg);
556 cur.clearSelection(); // bug 393
563 cutSelection(cur, true, true);
564 cur.message(_("Cut"));
565 // Prevent stale position >= size crash
566 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
572 cur.message(_("Copy"));
575 case LFUN_MOUSE_PRESS:
576 lfunMousePress(cur, cmd);
579 case LFUN_MOUSE_MOTION:
580 lfunMouseMotion(cur, cmd);
583 case LFUN_MOUSE_RELEASE:
584 lfunMouseRelease(cur, cmd);
587 case LFUN_FINISHED_LEFT: // in math, left is backwards
588 case LFUN_FINISHED_BACKWARD:
589 cur.bv().cursor() = cur;
592 case LFUN_FINISHED_RIGHT: // in math, right is forward
593 case LFUN_FINISHED_FORWARD:
595 cur.bv().cursor() = cur;
598 case LFUN_CHAR_RIGHT:
600 case LFUN_CHAR_BACKWARD:
601 case LFUN_CHAR_FORWARD:
602 cur.updateFlags(Update::Decoration | Update::FitCursor);
603 case LFUN_CHAR_RIGHT_SELECT:
604 case LFUN_CHAR_LEFT_SELECT:
605 case LFUN_CHAR_BACKWARD_SELECT:
606 case LFUN_CHAR_FORWARD_SELECT: {
607 // are we in a selection?
608 bool select = (cmd.action == LFUN_CHAR_RIGHT_SELECT
609 || cmd.action == LFUN_CHAR_LEFT_SELECT
610 || cmd.action == LFUN_CHAR_BACKWARD_SELECT
611 || cmd.action == LFUN_CHAR_FORWARD_SELECT);
612 // are we moving forward or backwards?
613 // If the command was RIGHT or LEFT, then whether we're moving forward
614 // or backwards depends on the cursor movement mode (logical or visual):
615 // * in visual mode, since math is always LTR, right -> forward,
617 // * in logical mode, the mapping is determined by the
618 // reverseDirectionNeeded() function
621 FuncCode finish_lfun;
623 if (cmd.action == LFUN_CHAR_FORWARD
624 || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
626 finish_lfun = LFUN_FINISHED_FORWARD;
628 else if (cmd.action == LFUN_CHAR_BACKWARD
629 || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
631 finish_lfun = LFUN_FINISHED_BACKWARD;
634 bool right = (cmd.action == LFUN_CHAR_RIGHT_SELECT
635 || cmd.action == LFUN_CHAR_RIGHT);
636 if (lyxrc.visual_cursor || !reverseDirectionNeeded(cur))
642 finish_lfun = LFUN_FINISHED_RIGHT;
644 finish_lfun = LFUN_FINISHED_LEFT;
646 // Now that we know exactly what we want to do, let's do it!
647 cur.selHandle(select);
649 cur.macroModeClose();
650 // try moving forward or backwards as necessary...
651 if (!(forward ? cursorMathForward(cur) : cursorMathBackward(cur))) {
652 // ... and if movement failed, then finish forward or backwards
654 cmd = FuncRequest(finish_lfun);
662 cur.updateFlags(Update::Decoration | Update::FitCursor);
663 case LFUN_DOWN_SELECT:
664 case LFUN_UP_SELECT: {
665 // close active macro
666 if (cur.inMacroMode()) {
667 cur.macroModeClose();
671 // stop/start the selection
672 bool select = cmd.action == LFUN_DOWN_SELECT ||
673 cmd.action == LFUN_UP_SELECT;
674 cur.selHandle(select);
677 bool up = cmd.action == LFUN_UP || cmd.action == LFUN_UP_SELECT;
678 bool successful = cur.upDownInMath(up);
682 if (cur.fixIfBroken())
683 // FIXME: Something bad happened. We pass the corrected Cursor
684 // instead of letting things go worse.
687 // We did not manage to move the cursor.
692 case LFUN_MOUSE_DOUBLE:
693 case LFUN_MOUSE_TRIPLE:
694 case LFUN_WORD_SELECT:
698 cur.setSelection(true);
699 cur.pos() = cur.lastpos();
700 cur.idx() = cur.lastidx();
703 case LFUN_PARAGRAPH_UP:
704 case LFUN_PARAGRAPH_DOWN:
705 cur.updateFlags(Update::Decoration | Update::FitCursor);
706 case LFUN_PARAGRAPH_UP_SELECT:
707 case LFUN_PARAGRAPH_DOWN_SELECT:
710 case LFUN_LINE_BEGIN:
711 case LFUN_WORD_BACKWARD:
713 cur.updateFlags(Update::Decoration | Update::FitCursor);
714 case LFUN_LINE_BEGIN_SELECT:
715 case LFUN_WORD_BACKWARD_SELECT:
716 case LFUN_WORD_LEFT_SELECT:
717 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
718 cmd.action == LFUN_WORD_LEFT_SELECT ||
719 cmd.action == LFUN_LINE_BEGIN_SELECT);
720 cur.macroModeClose();
721 if (cur.pos() != 0) {
723 } else if (cur.col() != 0) {
724 cur.idx() -= cur.col();
726 } else if (cur.idx() != 0) {
730 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
735 case LFUN_WORD_FORWARD:
736 case LFUN_WORD_RIGHT:
738 cur.updateFlags(Update::Decoration | Update::FitCursor);
739 case LFUN_WORD_FORWARD_SELECT:
740 case LFUN_WORD_RIGHT_SELECT:
741 case LFUN_LINE_END_SELECT:
742 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
743 cmd.action == LFUN_WORD_RIGHT_SELECT ||
744 cmd.action == LFUN_LINE_END_SELECT);
745 cur.macroModeClose();
747 if (cur.pos() != cur.lastpos()) {
748 cur.pos() = cur.lastpos();
749 } else if (ncols() && (cur.col() != cur.lastcol())) {
750 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
751 cur.pos() = cur.lastpos();
752 } else if (cur.idx() != cur.lastidx()) {
753 cur.idx() = cur.lastidx();
754 cur.pos() = cur.lastpos();
756 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
761 case LFUN_CELL_FORWARD:
762 cur.updateFlags(Update::Decoration | Update::FitCursor);
763 cur.inset().idxNext(cur);
766 case LFUN_CELL_BACKWARD:
767 cur.updateFlags(Update::Decoration | Update::FitCursor);
768 cur.inset().idxPrev(cur);
771 case LFUN_WORD_DELETE_BACKWARD:
772 case LFUN_CHAR_DELETE_BACKWARD:
774 // May affect external cell:
775 cur.recordUndoInset();
777 cur.recordUndoSelection();
778 // if the inset can not be removed from within, delete it
779 if (!cur.backspace()) {
780 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
781 cur.innerText()->dispatch(cur, cmd);
785 case LFUN_WORD_DELETE_FORWARD:
786 case LFUN_CHAR_DELETE_FORWARD:
787 if (cur.pos() == cur.lastpos())
788 // May affect external cell:
789 cur.recordUndoInset();
791 cur.recordUndoSelection();
792 // if the inset can not be removed from within, delete it
794 FuncRequest cmd = FuncRequest(LFUN_CHAR_DELETE_FORWARD);
795 cur.innerText()->dispatch(cur, cmd);
801 cur.clearSelection();
803 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
808 // 'Locks' the math inset. A 'locked' math inset behaves as a unit
809 // that is traversed by a single <CursorLeft>/<CursorRight>.
810 case LFUN_INSET_TOGGLE:
816 case LFUN_SELF_INSERT:
817 if (cmd.argument().size() != 1) {
818 cur.recordUndoSelection();
819 docstring const arg = cmd.argument();
820 if (!interpretString(cur, arg))
824 // Don't record undo steps if we are in macro mode and thus
825 // cmd.argument is the next character of the macro name.
826 // Otherwise we'll get an invalid cursor if we undo after
827 // the macro was finished and the macro is a known command,
828 // e.g. sqrt. Cursor::macroModeClose replaces in this case
829 // the InsetMathUnknown with name "frac" by an empty
830 // InsetMathFrac -> a pos value > 0 is invalid.
831 // A side effect is that an undo before the macro is finished
832 // undoes the complete macro, not only the last character.
833 // At the time we hit '\' we are not in macro mode, still.
834 if (!cur.inMacroMode())
835 cur.recordUndoSelection();
837 // spacial handling of space. If we insert an inset
838 // via macro mode, we want to put the cursor inside it
839 // if relevant. Think typing "\frac<space>".
840 if (cmd.argument()[0] == ' '
841 && cur.inMacroMode() && cur.macroName() != "\\"
842 && cur.macroModeClose()) {
843 MathAtom const atom = cur.prevAtom();
844 if (atom->asNestInset() && atom->isActive()) {
846 cur.pushBackward(*cur.nextInset());
848 } else if (!interpretChar(cur, cmd.argument()[0])) {
849 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
854 //case LFUN_SERVER_GET_XY:
857 case LFUN_SERVER_SET_XY: {
858 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
861 istringstream is(to_utf8(cmd.argument()));
863 cur.setScreenPos(x, y);
867 // Special casing for superscript in case of LyX handling
869 case LFUN_ACCENT_CIRCUMFLEX:
870 if (cmd.argument().empty()) {
871 // do superscript if LyX handles
873 cur.recordUndoSelection();
874 script(cur, true, grabAndEraseSelection(cur));
878 case LFUN_ACCENT_UMLAUT:
879 case LFUN_ACCENT_ACUTE:
880 case LFUN_ACCENT_GRAVE:
881 case LFUN_ACCENT_BREVE:
882 case LFUN_ACCENT_DOT:
883 case LFUN_ACCENT_MACRON:
884 case LFUN_ACCENT_CARON:
885 case LFUN_ACCENT_TILDE:
886 case LFUN_ACCENT_CEDILLA:
887 case LFUN_ACCENT_CIRCLE:
888 case LFUN_ACCENT_UNDERDOT:
889 case LFUN_ACCENT_TIE:
890 case LFUN_ACCENT_OGONEK:
891 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
895 case LFUN_TEXTSTYLE_APPLY:
896 case LFUN_TEXTSTYLE_UPDATE:
897 handleFont2(cur, cmd.argument());
901 if (currentMode() <= TEXT_MODE)
902 handleFont(cur, cmd.argument(), "textbf");
904 handleFont(cur, cmd.argument(), "mathbf");
906 case LFUN_FONT_BOLDSYMBOL:
907 if (currentMode() <= TEXT_MODE)
908 handleFont(cur, cmd.argument(), "textbf");
910 handleFont(cur, cmd.argument(), "boldsymbol");
913 if (currentMode() <= TEXT_MODE)
914 handleFont(cur, cmd.argument(), "textsf");
916 handleFont(cur, cmd.argument(), "mathsf");
919 if (currentMode() <= TEXT_MODE)
920 handleFont(cur, cmd.argument(), "emph");
922 handleFont(cur, cmd.argument(), "mathcal");
924 case LFUN_FONT_ROMAN:
925 if (currentMode() <= TEXT_MODE)
926 handleFont(cur, cmd.argument(), "textrm");
928 handleFont(cur, cmd.argument(), "mathrm");
930 case LFUN_FONT_TYPEWRITER:
931 if (currentMode() <= TEXT_MODE)
932 handleFont(cur, cmd.argument(), "texttt");
934 handleFont(cur, cmd.argument(), "mathtt");
937 handleFont(cur, cmd.argument(), "mathfrak");
940 if (currentMode() <= TEXT_MODE)
941 handleFont(cur, cmd.argument(), "textit");
943 handleFont(cur, cmd.argument(), "mathit");
946 if (currentMode() <= TEXT_MODE)
947 // FIXME: should be "noun"
948 handleFont(cur, cmd.argument(), "textsc");
950 handleFont(cur, cmd.argument(), "mathbb");
952 case LFUN_FONT_DEFAULT:
953 handleFont(cur, cmd.argument(), "textnormal");
956 case LFUN_MATH_MODE: {
958 // ignore math-mode on when already in math mode
959 if (currentMode() == Inset::MATH_MODE && cmd.argument() == "on")
961 cur.recordUndoSelection();
962 cur.macroModeClose();
963 docstring const save_selection = grabAndEraseSelection(cur);
965 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
966 if (currentMode() <= Inset::TEXT_MODE)
967 cur.plainInsert(MathAtom(new InsetMathEnsureMath));
969 cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
971 cur.pushBackward(*cur.nextInset());
972 cur.niceInsert(save_selection);
974 if (currentMode() == Inset::TEXT_MODE) {
975 cur.recordUndoSelection();
976 cur.niceInsert(MathAtom(new InsetMathHull("simple")));
977 cur.message(_("create new math text environment ($...$)"));
979 handleFont(cur, cmd.argument(), "textrm");
980 cur.message(_("entered math text mode (textrm)"));
986 case LFUN_REGEXP_MODE: {
987 InsetMathHull * i = dynamic_cast<InsetMathHull *>(cur.inset().asInsetMath());
988 if (i && i->getType() == hullRegexp) {
989 cur.message(_("Already in regexp mode"));
992 cur.macroModeClose();
993 docstring const save_selection = grabAndEraseSelection(cur);
995 cur.plainInsert(MathAtom(new InsetMathHull(hullRegexp)));
997 cur.pushBackward(*cur.nextInset());
998 cur.niceInsert(save_selection);
999 cur.message(_("Regexp editor mode"));
1003 case LFUN_MATH_FONT_STYLE: {
1004 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, '\\' + cmd.argument());
1005 doDispatch(cur, fr);
1009 case LFUN_MATH_SIZE: {
1010 FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, cmd.argument());
1011 doDispatch(cur, fr);
1015 case LFUN_MATH_MATRIX: {
1021 idocstringstream is(cmd.argument());
1022 is >> m >> n >> v_align >> h_align;
1029 MathAtom(new InsetMathArray(from_ascii("array"), m, n, (char)v_align[0], h_align)));
1033 case LFUN_MATH_AMS_MATRIX: {
1038 idocstringstream is(cmd.argument());
1039 is >> m >> n >> name;
1045 MathAtom(new InsetMathAMSArray(name, m, n)));
1049 case LFUN_MATH_DELIM: {
1051 docstring rs = split(cmd.argument(), ls, ' ');
1052 // Reasonable default values
1058 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
1062 case LFUN_MATH_BIGDELIM: {
1063 docstring const lname = from_utf8(cmd.getArg(0));
1064 docstring const ldelim = from_utf8(cmd.getArg(1));
1065 docstring const rname = from_utf8(cmd.getArg(2));
1066 docstring const rdelim = from_utf8(cmd.getArg(3));
1067 latexkeys const * l = in_word_set(lname);
1068 bool const have_l = l && l->inset == "big" &&
1069 InsetMathBig::isBigInsetDelim(ldelim);
1070 l = in_word_set(rname);
1071 bool const have_r = l && l->inset == "big" &&
1072 InsetMathBig::isBigInsetDelim(rdelim);
1073 // We mimic LFUN_MATH_DELIM in case we have an empty left
1074 // or right delimiter.
1075 if (have_l || have_r) {
1077 docstring const selection = grabAndEraseSelection(cur);
1080 cur.insert(MathAtom(new InsetMathBig(lname,
1082 cur.niceInsert(selection);
1084 cur.insert(MathAtom(new InsetMathBig(rname,
1087 // Don't call cur.undispatched() if we did nothing, this would
1088 // lead to infinite recursion via Text::dispatch().
1092 case LFUN_SPACE_INSERT:
1093 cur.recordUndoSelection();
1094 cur.insert(MathAtom(new InsetMathSpace));
1097 case LFUN_MATH_SPACE:
1098 cur.recordUndoSelection();
1099 if (cmd.argument().empty())
1100 cur.insert(MathAtom(new InsetMathSpace));
1102 string const name = cmd.getArg(0);
1103 string const len = cmd.getArg(1);
1104 cur.insert(MathAtom(new InsetMathSpace(name, len)));
1108 case LFUN_ERT_INSERT:
1109 // interpret this as if a backslash was typed
1111 interpretChar(cur, '\\');
1114 case LFUN_MATH_SUBSCRIPT:
1115 // interpret this as if a _ was typed
1116 cur.recordUndoSelection();
1117 interpretChar(cur, '_');
1120 case LFUN_MATH_SUPERSCRIPT:
1121 // interpret this as if a ^ was typed
1122 cur.recordUndoSelection();
1123 interpretChar(cur, '^');
1126 case LFUN_MATH_MACRO_FOLD:
1127 case LFUN_MATH_MACRO_UNFOLD: {
1129 bool fold = cmd.action == LFUN_MATH_MACRO_FOLD;
1130 bool found = findMacroToFoldUnfold(it, fold);
1132 MathMacro * macro = it.nextInset()->asInsetMath()->asMacro();
1133 cur.recordUndoInset();
1142 case LFUN_QUOTE_INSERT:
1143 // interpret this as if a straight " was typed
1144 cur.recordUndoSelection();
1145 interpretChar(cur, '\"');
1148 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
1149 // handling such that "self-insert" works on "arbitrary stuff" too, and
1150 // math-insert only handles special math things like "matrix".
1151 case LFUN_MATH_INSERT: {
1152 cur.recordUndoSelection();
1153 if (cmd.argument() == "^" || cmd.argument() == "_")
1154 interpretChar(cur, cmd.argument()[0]);
1157 asArray(cmd.argument(), ar);
1158 if (cur.selection() && ar.size() == 1
1159 && ar[0]->asNestInset()
1160 && ar[0]->asNestInset()->nargs() > 1)
1161 handleNest(cur, ar[0]);
1163 cur.niceInsert(cmd.argument());
1168 case LFUN_DIALOG_SHOW_NEW_INSET: {
1169 docstring const & name = cmd.argument();
1171 if (name == "ref") {
1172 InsetMathRef tmp(name);
1173 data = tmp.createDialogStr(to_utf8(name));
1174 } else if (name == "mathspace") {
1176 data = tmp.createDialogStr();
1178 cur.bv().showDialog(to_utf8(name), data);
1182 case LFUN_INSET_INSERT: {
1184 if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
1185 cur.recordUndoSelection();
1191 case LFUN_INSET_DISSOLVE:
1192 if (!asHullInset()) {
1193 cur.recordUndoInset();
1199 InsetMath::doDispatch(cur, cmd);
1205 bool InsetMathNest::findMacroToFoldUnfold(Cursor & it, bool fold) const {
1206 // look for macro to open/close, but stay in mathed
1207 for (; !it.empty(); it.pop_back()) {
1209 // go backward through the current cell
1210 Inset * inset = it.nextInset();
1211 while (inset && inset->asInsetMath()) {
1212 MathMacro * macro = inset->asInsetMath()->asMacro();
1214 // found the an macro to open/close?
1215 if (macro->folded() != fold)
1218 // Wrong folding state.
1219 // If this was the first we see in this slice, look further left,
1221 if (inset != it.nextInset())
1225 // go up if this was the left most position
1231 inset = it.nextInset();
1239 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
1240 FuncStatus & flag) const
1242 // the font related toggles
1243 //string tc = "mathnormal";
1245 string const arg = to_utf8(cmd.argument());
1246 switch (cmd.action) {
1247 case LFUN_TABULAR_FEATURE:
1248 flag.setEnabled(false);
1251 case LFUN_TABULAR_FEATURE:
1252 // FIXME: check temporarily disabled
1254 char align = mathcursor::valign();
1255 if (align == '\0') {
1259 if (cmd.argument().empty()) {
1263 if (!contains("tcb", cmd.argument()[0])) {
1267 flag.setOnOff(cmd.argument()[0] == align);
1270 /// We have to handle them since 1.4 blocks all unhandled actions
1271 case LFUN_FONT_ITAL:
1272 case LFUN_FONT_BOLD:
1273 case LFUN_FONT_BOLDSYMBOL:
1274 case LFUN_FONT_SANS:
1275 case LFUN_FONT_EMPH:
1276 case LFUN_FONT_TYPEWRITER:
1277 case LFUN_FONT_NOUN:
1278 case LFUN_FONT_ROMAN:
1279 case LFUN_FONT_DEFAULT:
1280 flag.setEnabled(true);
1283 // we just need to be in math mode to enable that
1284 case LFUN_MATH_SIZE:
1285 case LFUN_MATH_SPACE:
1286 case LFUN_MATH_LIMITS:
1287 case LFUN_MATH_EXTERN:
1288 flag.setEnabled(true);
1291 case LFUN_FONT_FRAK:
1292 flag.setEnabled(currentMode() != TEXT_MODE);
1295 case LFUN_MATH_FONT_STYLE: {
1296 bool const textarg =
1297 arg == "textbf" || arg == "textsf" ||
1298 arg == "textrm" || arg == "textmd" ||
1299 arg == "textit" || arg == "textsc" ||
1300 arg == "textsl" || arg == "textup" ||
1301 arg == "texttt" || arg == "textbb" ||
1302 arg == "textnormal";
1303 flag.setEnabled(currentMode() != TEXT_MODE || textarg);
1307 case LFUN_MATH_INSERT:
1308 flag.setEnabled(currentMode() != TEXT_MODE);
1311 case LFUN_MATH_AMS_MATRIX:
1312 case LFUN_MATH_MATRIX:
1313 flag.setEnabled(currentMode() == MATH_MODE);
1316 case LFUN_INSET_INSERT: {
1317 // Don't test createMathInset_fromDialogStr(), since
1318 // getStatus is not called with a valid reference and the
1319 // dialog would not be applyable.
1320 string const name = cmd.getArg(0);
1321 flag.setEnabled(name == "ref" || name == "mathspace");
1325 case LFUN_MATH_DELIM:
1326 case LFUN_MATH_BIGDELIM:
1327 // Don't do this with multi-cell selections
1328 flag.setEnabled(cur.selBegin().idx() == cur.selEnd().idx());
1331 case LFUN_MATH_MACRO_FOLD:
1332 case LFUN_MATH_MACRO_UNFOLD: {
1334 bool found = findMacroToFoldUnfold(it, cmd.action == LFUN_MATH_MACRO_FOLD);
1335 flag.setEnabled(found);
1339 case LFUN_SPECIALCHAR_INSERT:
1340 // FIXME: These would probably make sense in math-text mode
1341 flag.setEnabled(false);
1344 case LFUN_INSET_DISSOLVE:
1345 flag.setEnabled(!asHullInset());
1356 void InsetMathNest::edit(Cursor & cur, bool front, EntryDirection entry_from)
1359 bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_RIGHT ||
1360 (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
1361 cur.idx() = enter_front ? 0 : cur.lastidx();
1362 cur.pos() = enter_front ? 0 : cur.lastpos();
1364 //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1368 Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
1371 int dist_min = 1000000;
1372 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1373 int const d = cell(i).dist(cur.bv(), x, y);
1379 MathData & ar = cell(idx_min);
1381 cur.idx() = idx_min;
1382 cur.pos() = ar.x2pos(&cur.bv(), x - ar.xo(cur.bv()));
1384 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1385 if (dist_min == 0) {
1387 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1388 if (ar[i]->covers(cur.bv(), x, y))
1389 return ar[i].nucleus()->editXY(cur, x, y);
1395 void InsetMathNest::lfunMousePress(Cursor & cur, FuncRequest & cmd)
1397 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1398 BufferView & bv = cur.bv();
1399 bool do_selection = cmd.button() == mouse_button::button1
1400 && cmd.argument() == "region-select";
1401 bv.mouseSetCursor(cur, do_selection);
1402 if (cmd.button() == mouse_button::button1) {
1403 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1404 // Update the cursor update flags as needed:
1406 // Update::Decoration: tells to update the decoration
1407 // (visual box corners that define
1409 // Update::FitCursor: adjust the screen to the cursor
1410 // position if needed
1411 // cur.result().update(): don't overwrite previously set flags.
1412 cur.updateFlags(Update::Decoration | Update::FitCursor
1413 | cur.result().update());
1414 } else if (cmd.button() == mouse_button::button2) {
1415 if (cap::selection()) {
1416 // See comment in Text::dispatch why we do this
1417 cap::copySelectionToStack();
1418 cmd = FuncRequest(LFUN_PASTE, "0");
1419 doDispatch(bv.cursor(), cmd);
1422 asArray(theSelection().get(), ar);
1423 bv.cursor().insert(ar);
1429 void InsetMathNest::lfunMouseMotion(Cursor & cur, FuncRequest & cmd)
1431 // only select with button 1
1432 if (cmd.button() == mouse_button::button1) {
1433 Cursor & bvcur = cur.bv().cursor();
1434 if (bvcur.anchor_.hasPart(cur)) {
1435 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1436 bvcur.setCursor(cur);
1437 bvcur.setSelection(true);
1438 //lyxerr << "MOTION " << bvcur << endl;
1445 void InsetMathNest::lfunMouseRelease(Cursor & cur, FuncRequest & cmd)
1447 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1449 if (cmd.button() == mouse_button::button1) {
1450 if (!cur.selection())
1453 Cursor & bvcur = cur.bv().cursor();
1454 bvcur.setSelection(true);
1463 bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
1465 //lyxerr << "interpret 2: '" << c << "'" << endl;
1466 docstring save_selection;
1467 if (c == '^' || c == '_')
1468 save_selection = grabAndEraseSelection(cur);
1473 if (cur.inMacroMode()) {
1474 docstring name = cur.macroName();
1476 /// are we currently typing '#1' or '#2' or...?
1477 if (name == "\\#") {
1480 if (n >= 1 && n <= 9)
1481 cur.insert(new MathMacroArgument(n));
1485 // do not finish macro for known * commands
1486 MathWordList const & mwl = mathedWordList();
1487 bool star_macro = c == '*'
1488 && (mwl.find(name.substr(1) + "*") != mwl.end()
1489 || cur.buffer()->getMacro(name.substr(1) + "*", cur, true));
1490 if (isAlphaASCII(c) || star_macro) {
1491 cur.activeMacro()->setName(name + docstring(1, c));
1495 // handle 'special char' macros
1500 if (currentMode() <= InsetMath::TEXT_MODE)
1501 cur.niceInsert(createInsetMath("textbackslash"));
1503 cur.niceInsert(createInsetMath("backslash"));
1504 } else if (c == '^' && currentMode() == InsetMath::MATH_MODE) {
1506 cur.niceInsert(createInsetMath("mathcircumflex"));
1507 } else if (c == '{') {
1509 cur.niceInsert(MathAtom(new InsetMathBrace));
1510 } else if (c == '%') {
1512 cur.niceInsert(MathAtom(new InsetMathComment));
1513 } else if (c == '#') {
1514 LASSERT(cur.activeMacro(), /**/);
1515 cur.activeMacro()->setName(name + docstring(1, c));
1518 cur.niceInsert(createInsetMath(docstring(1, c)));
1523 // One character big delimiters. The others are handled in
1524 // interpretString().
1525 latexkeys const * l = in_word_set(name.substr(1));
1526 if (name[0] == '\\' && l && l->inset == "big") {
1530 delim = from_ascii("\\{");
1533 delim = from_ascii("\\}");
1536 delim = docstring(1, c);
1539 if (InsetMathBig::isBigInsetDelim(delim)) {
1540 // name + delim ared a valid InsetMathBig.
1541 // We can't use cur.macroModeClose() because
1542 // it does not handle delim.
1543 InsetMathUnknown * p = cur.activeMacro();
1546 cur.cell().erase(cur.pos());
1547 cur.plainInsert(MathAtom(
1548 new InsetMathBig(name.substr(1), delim)));
1553 // leave macro mode and try again if necessary
1554 if (cur.macroModeClose()) {
1555 MathAtom const atom = cur.prevAtom();
1556 if (atom->asNestInset() && atom->isActive()) {
1558 cur.pushBackward(*cur.nextInset());
1562 cur.niceInsert(MathAtom(new InsetMathBrace));
1564 interpretChar(cur, c);
1569 // leave autocorrect mode if necessary
1570 if (lyxrc.autocorrection_math && c == ' ' && cur.autocorrect()) {
1571 cur.autocorrect() = false;
1572 cur.message(_("Autocorrect Off ('!' to enter)"));
1575 if (lyxrc.autocorrection_math && c == '!' && !cur.autocorrect()) {
1576 cur.autocorrect() = true;
1577 cur.message(_("Autocorrect On (<space> to exit)"));
1581 // just clear selection on pressing the space bar
1582 if (cur.selection() && c == ' ') {
1583 cur.setSelection(false);
1588 //lyxerr << "starting with macro" << endl;
1589 bool reduced = cap::reduceSelectionToOneCell(cur);
1590 if (reduced || !cur.selection()) {
1591 docstring const safe = cap::grabAndEraseSelection(cur);
1592 cur.insert(MathAtom(new InsetMathUnknown(from_ascii("\\"), safe, false)));
1600 if (currentMode() <= InsetMath::TEXT_MODE)
1606 if (currentMode() <= InsetMath::TEXT_MODE) {
1607 // insert spaces in text or undecided mode,
1608 // but suppress direct insertion of two spaces in a row
1609 // the still allows typing '<space>a<space>' and deleting the 'a', but
1610 // it is better than nothing...
1611 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ') {
1613 // FIXME: we have to enable full redraw here because of the
1614 // visual box corners that define the inset. If we know for
1615 // sure that we stay within the same cell we can optimize for
1617 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1621 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1622 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1623 // FIXME: we have to enable full redraw here because of the
1624 // visual box corners that define the inset. If we know for
1625 // sure that we stay within the same cell we can optimize for
1627 //cur.updateFlags(Update::SinglePar | Update::FitCursor);
1631 if (cur.popForward()) {
1632 // FIXME: we have to enable full redraw here because of the
1633 // visual box corners that define the inset. If we know for
1634 // sure that we stay within the same cell we can optimize for
1636 //cur.updateFlags(Update::FitCursor);
1640 // if we are at the very end, leave the formula
1641 return cur.pos() != cur.lastpos();
1644 // These should be treated differently when not in text mode:
1645 if (currentMode() != InsetMath::TEXT_MODE) {
1647 script(cur, false, save_selection);
1651 script(cur, true, save_selection);
1655 cur.niceInsert(createInsetMath("sim"));
1658 if (currentMode() == InsetMath::MATH_MODE && !isAsciiOrMathAlpha(c)) {
1659 MathAtom at = createInsetMath("text");
1660 at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
1667 cur.niceInsert(createInsetMath("textasciicircum"));
1671 cur.niceInsert(createInsetMath("textasciitilde"));
1676 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1677 c == '%' || c == '_') {
1678 cur.niceInsert(createInsetMath(docstring(1, c)));
1683 // try auto-correction
1684 if (lyxrc.autocorrection_math && cur.autocorrect() && cur.pos() != 0
1685 && math_autocorrect(cur.prevAtom(), c))
1688 // no special circumstances, so insert the character without any fuss
1690 if (lyxrc.autocorrection_math) {
1691 if (!cur.autocorrect())
1692 cur.message(_("Autocorrect Off ('!' to enter)"));
1694 cur.message(_("Autocorrect On (<space> to exit)"));
1700 bool InsetMathNest::interpretString(Cursor & cur, docstring const & str)
1702 // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1704 if (!cur.empty() && cur.pos() > 0 &&
1705 cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1706 if (InsetMathBig::isBigInsetDelim(str)) {
1707 docstring prev = asString(cur.cell()[cur.pos() - 1]);
1708 if (prev[0] == '\\') {
1709 prev = prev.substr(1);
1710 latexkeys const * l = in_word_set(prev);
1711 if (l && l->inset == "big") {
1712 cur.cell()[cur.pos() - 1] =
1713 MathAtom(new InsetMathBig(prev, str));
1723 bool InsetMathNest::script(Cursor & cur, bool up)
1725 return script(cur, up, docstring());
1729 bool InsetMathNest::script(Cursor & cur, bool up,
1730 docstring const & save_selection)
1732 // Hack to get \^ and \_ working
1733 //lyxerr << "handling script: up: " << up << endl;
1734 if (cur.inMacroMode() && cur.macroName() == "\\") {
1736 cur.niceInsert(createInsetMath("mathcircumflex"));
1738 interpretChar(cur, '_');
1742 cur.macroModeClose();
1743 if (asScriptInset() && cur.idx() == 0) {
1744 // we are in a nucleus of a script inset, move to _our_ script
1745 InsetMathScript * inset = asScriptInset();
1746 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1748 cur.idx() = inset->idxOfScript(up);
1750 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1752 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1755 cur.idx() = inset->idxOfScript(up);
1756 cur.pos() = cur.lastpos();
1758 // convert the thing to our left to a scriptinset or create a new
1759 // one if in the very first position of the array
1760 if (cur.pos() == 0) {
1761 //lyxerr << "new scriptinset" << endl;
1762 cur.insert(new InsetMathScript(up));
1764 //lyxerr << "converting prev atom " << endl;
1765 cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1768 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1769 // See comment in MathParser.cpp for special handling of {}-bases
1775 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1776 cur.niceInsert(save_selection);
1778 //lyxerr << "inserting selection 2:\n" << save_selection << endl;
1783 bool InsetMathNest::completionSupported(Cursor const & cur) const
1785 return cur.inMacroMode();
1789 bool InsetMathNest::inlineCompletionSupported(Cursor const & cur) const
1791 return cur.inMacroMode();
1795 bool InsetMathNest::automaticInlineCompletion() const
1797 return lyxrc.completion_inline_math;
1801 bool InsetMathNest::automaticPopupCompletion() const
1803 return lyxrc.completion_popup_math;
1807 CompletionList const *
1808 InsetMathNest::createCompletionList(Cursor const & cur) const
1810 if (!cur.inMacroMode())
1813 return new MathCompletionList(cur);
1817 docstring InsetMathNest::completionPrefix(Cursor const & cur) const
1819 if (!cur.inMacroMode())
1822 return cur.activeMacro()->name();
1826 bool InsetMathNest::insertCompletion(Cursor & cur, docstring const & s,
1829 if (!cur.inMacroMode())
1832 // append completion to active macro
1833 InsetMathUnknown * inset = cur.activeMacro();
1834 inset->setName(inset->name() + s);
1839 // FIXME: this creates duplicates in the completion popup
1840 // which looks ugly. Moreover the changes the list lengths
1842 confuse the popup as well.
1843 MathCompletionList::addToFavorites(inset->name());
1845 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, " "));
1852 void InsetMathNest::completionPosAndDim(Cursor const & cur, int & x, int & y,
1853 Dimension & dim) const
1855 Inset const * inset = cur.activeMacro();
1859 // get inset dimensions
1860 dim = cur.bv().coordCache().insets().dim(inset);
1861 // FIXME: these 3 are no accurate, but should depend on the font.
1862 // Now the popup jumps down if you enter a char with descent > 0.
1868 = cur.bv().coordCache().insets().xy(inset);
1874 bool InsetMathNest::cursorMathForward(Cursor & cur)
1876 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
1877 cur.pushBackward(*cur.nextAtom().nucleus());
1878 cur.inset().idxFirst(cur);
1881 if (cur.posForward() || idxForward(cur))
1883 // try to pop forwards --- but don't pop out of math! leave that to
1885 int s = cur.depth() - 2;
1886 if (s >= 0 && cur[s].inset().asInsetMath())
1887 return cur.popForward();
1892 bool InsetMathNest::cursorMathBackward(Cursor & cur)
1894 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
1896 cur.push(*cur.nextAtom().nucleus());
1897 cur.inset().idxLast(cur);
1900 if (cur.posBackward() || idxBackward(cur))
1902 // try to pop backwards --- but don't pop out of math! leave that to
1904 int s = cur.depth() - 2;
1905 if (s >= 0 && cur[s].inset().asInsetMath())
1906 return cur.popBackward();
1911 ////////////////////////////////////////////////////////////////////
1913 MathCompletionList::MathCompletionList(Cursor const & cur)
1915 // fill it with macros from the buffer
1916 MacroNameSet macros;
1917 cur.buffer()->listMacroNames(macros);
1918 MacroNameSet::const_iterator it;
1919 for (it = macros.begin(); it != macros.end(); ++it) {
1920 if (cur.buffer()->getMacro(*it, cur, false))
1921 locals.push_back("\\" + *it);
1923 sort(locals.begin(), locals.end());
1925 if (globals.size() > 0)
1928 // fill in global macros
1930 MacroTable::globalMacros().getMacroNames(macros);
1931 //lyxerr << "Globals completion macros: ";
1932 for (it = macros.begin(); it != macros.end(); ++it) {
1933 //lyxerr << "\\" + *it << " ";
1934 globals.push_back("\\" + *it);
1936 //lyxerr << std::endl;
1938 // fill in global commands
1939 globals.push_back(from_ascii("\\boxed"));
1940 globals.push_back(from_ascii("\\fbox"));
1941 globals.push_back(from_ascii("\\framebox"));
1942 globals.push_back(from_ascii("\\makebox"));
1943 globals.push_back(from_ascii("\\kern"));
1944 globals.push_back(from_ascii("\\xrightarrow"));
1945 globals.push_back(from_ascii("\\xleftarrow"));
1946 globals.push_back(from_ascii("\\split"));
1947 globals.push_back(from_ascii("\\gathered"));
1948 globals.push_back(from_ascii("\\aligned"));
1949 globals.push_back(from_ascii("\\alignedat"));
1950 globals.push_back(from_ascii("\\cases"));
1951 globals.push_back(from_ascii("\\substack"));
1952 globals.push_back(from_ascii("\\xymatrix"));
1953 globals.push_back(from_ascii("\\subarray"));
1954 globals.push_back(from_ascii("\\array"));
1955 globals.push_back(from_ascii("\\sqrt"));
1956 globals.push_back(from_ascii("\\root"));
1957 globals.push_back(from_ascii("\\tabular"));
1958 globals.push_back(from_ascii("\\stackrel"));
1959 globals.push_back(from_ascii("\\binom"));
1960 globals.push_back(from_ascii("\\choose"));
1961 globals.push_back(from_ascii("\\brace"));
1962 globals.push_back(from_ascii("\\brack"));
1963 globals.push_back(from_ascii("\\frac"));
1964 globals.push_back(from_ascii("\\over"));
1965 globals.push_back(from_ascii("\\nicefrac"));
1966 globals.push_back(from_ascii("\\unitfrac"));
1967 globals.push_back(from_ascii("\\unitfracthree"));
1968 globals.push_back(from_ascii("\\unitone"));
1969 globals.push_back(from_ascii("\\unittwo"));
1970 globals.push_back(from_ascii("\\infer"));
1971 globals.push_back(from_ascii("\\atop"));
1972 globals.push_back(from_ascii("\\lefteqn"));
1973 globals.push_back(from_ascii("\\boldsymbol"));
1974 globals.push_back(from_ascii("\\bm"));
1975 globals.push_back(from_ascii("\\color"));
1976 globals.push_back(from_ascii("\\normalcolor"));
1977 globals.push_back(from_ascii("\\textcolor"));
1978 globals.push_back(from_ascii("\\cfrac"));
1979 globals.push_back(from_ascii("\\cfracleft"));
1980 globals.push_back(from_ascii("\\cfracright"));
1981 globals.push_back(from_ascii("\\dfrac"));
1982 globals.push_back(from_ascii("\\tfrac"));
1983 globals.push_back(from_ascii("\\dbinom"));
1984 globals.push_back(from_ascii("\\tbinom"));
1985 globals.push_back(from_ascii("\\hphantom"));
1986 globals.push_back(from_ascii("\\phantom"));
1987 globals.push_back(from_ascii("\\vphantom"));
1988 MathWordList const & words = mathedWordList();
1989 MathWordList::const_iterator it2;
1990 //lyxerr << "Globals completion commands: ";
1991 for (it2 = words.begin(); it2 != words.end(); ++it2) {
1992 globals.push_back("\\" + (*it2).first);
1993 //lyxerr << "\\" + (*it2).first << " ";
1995 //lyxerr << std::endl;
1996 sort(globals.begin(), globals.end());
2000 MathCompletionList::~MathCompletionList()
2005 size_type MathCompletionList::size() const
2007 return locals.size() + globals.size();
2011 docstring const & MathCompletionList::data(size_t idx) const
2013 size_t lsize = locals.size();
2015 return globals[idx - lsize];
2021 std::string MathCompletionList::icon(size_t idx) const
2023 // get the latex command
2025 size_t lsize = locals.size();
2027 cmd = globals[idx - lsize];
2031 // get the icon resource name by stripping the backslash
2032 return "images/math/" + to_utf8(cmd.substr(1)) + ".png";
2035 std::vector<docstring> MathCompletionList::globals;