2 * \file math_nestinset.C
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 "math_nestinset.h"
15 #include "math_arrayinset.h"
16 #include "math_boxinset.h"
17 #include "math_braceinset.h"
18 #include "math_colorinset.h"
19 #include "math_commentinset.h"
20 #include "math_data.h"
21 #include "math_deliminset.h"
22 #include "math_factory.h"
23 #include "math_hullinset.h"
24 #include "math_mathmlstream.h"
25 #include "math_macroarg.h"
26 //#include "math_mboxinset.h"
27 #include "math_parser.h"
28 #include "math_scriptinset.h"
29 #include "math_spaceinset.h"
30 #include "math_symbolinset.h"
31 #include "math_support.h"
32 #include "math_unknowninset.h"
33 #include "ref_inset.h"
35 #include "BufferView.h"
36 #include "CutAndPaste.h"
37 #include "FuncStatus.h"
39 #include "bufferview_funcs.h"
40 #include "coordcache.h"
43 #include "dispatchresult.h"
44 #include "funcrequest.h"
46 #include "outputparams.h"
49 #include "support/lstrings.h"
51 #include "frontends/Dialogs.h"
52 #include "frontends/LyXView.h"
53 #include "frontends/Painter.h"
57 using lyx::cap::copySelection;
58 using lyx::cap::grabAndEraseSelection;
59 using lyx::cap::cutSelection;
60 using lyx::cap::replaceSelection;
61 using lyx::cap::selClearOrDel;
65 using std::istringstream;
68 MathNestInset::MathNestInset(idx_type nargs)
69 : cells_(nargs), lock_(false)
73 MathInset::idx_type MathNestInset::nargs() const
79 MathArray & MathNestInset::cell(idx_type i)
85 MathArray const & MathNestInset::cell(idx_type i) const
91 void MathNestInset::cursorPos(CursorSlice const & sl, bool /*boundary*/,
92 int & x, int & y) const
94 // FIXME: This is a hack. Ideally, the coord cache should not store
95 // absolute positions, but relative ones. This would mean to call
96 // setXY() not in MathArray::draw(), but in the parent insets' draw()
97 // with the correctly adjusted x,y values. But this means that we'd have
98 // to touch all (math)inset's draw() methods. Right now, we'll store
99 // absolute value, and make them here relative, only to make them
100 // absolute again when actually drawing the cursor. What a mess.
101 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
102 MathArray const & ar = sl.cell();
103 if (!theCoords.getArrays().has(&ar)) {
104 // this can (semi-)legally happen if we just created this cell
105 // and it never has been drawn before. So don't ASSERT.
106 //lyxerr << "no cached data for array " << &ar << endl;
111 Point const pt = theCoords.getArrays().xy(&ar);
112 if (!theCoords.getInsets().has(this)) {
114 //lyxerr << "no cached data for inset " << this << endl;
119 Point const pt2 = theCoords.getInsets().xy(this);
120 //lyxerr << "retrieving position cache for MathArray "
121 // << pt.x_ << ' ' << pt.y_ << std::endl;
122 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
124 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
125 // << " asc: " << ascent() << " des: " << descent()
126 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
127 // move cursor visually into empty cells ("blue rectangles");
133 void MathNestInset::metrics(MetricsInfo const & mi) const
136 for (idx_type i = 0, n = nargs(); i != n; ++i)
141 bool MathNestInset::idxNext(LCursor & cur) const
143 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
144 if (cur.idx() == cur.lastidx())
152 bool MathNestInset::idxRight(LCursor & cur) const
158 bool MathNestInset::idxPrev(LCursor & cur) const
160 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
164 cur.pos() = cur.lastpos();
169 bool MathNestInset::idxLeft(LCursor & cur) const
175 bool MathNestInset::idxFirst(LCursor & cur) const
177 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
186 bool MathNestInset::idxLast(LCursor & cur) const
188 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
191 cur.idx() = cur.lastidx();
192 cur.pos() = cur.lastpos();
197 void MathNestInset::dump() const
199 WriteStream os(lyxerr);
200 os << "---------------------------------------------\n";
203 for (idx_type i = 0, n = nargs(); i != n; ++i)
204 os << cell(i) << "\n";
205 os << "---------------------------------------------\n";
209 void MathNestInset::draw(PainterInfo & pi, int x, int y) const
213 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
216 setPosCache(pi, x, y);
220 void MathNestInset::drawSelection(PainterInfo & pi, int x, int y) const
222 // FIXME: hack to get position cache warm
225 // this should use the x/y values given, not the cached values
226 LCursor & cur = pi.base.bv->cursor();
227 if (!cur.selection())
229 if (!ptr_cmp(&cur.inset(), this))
232 CursorSlice s1 = cur.selBegin();
233 CursorSlice s2 = cur.selEnd();
234 //lyxerr << "MathNestInset::drawing selection: "
235 // << " s1: " << s1 << " s2: " << s2 << endl;
236 if (s1.idx() == s2.idx()) {
237 MathArray const & c = cell(s1.idx());
238 int x1 = c.xo() + c.pos2x(s1.pos());
239 int y1 = c.yo() - c.ascent();
240 int x2 = c.xo() + c.pos2x(s2.pos());
241 int y2 = c.yo() + c.descent();
242 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
243 //lyxerr << "MathNestInset::drawing selection 3: "
244 // << " x1: " << x1 << " x2: " << x2
245 // << " y1: " << y1 << " y2: " << y2 << endl;
247 for (idx_type i = 0; i < nargs(); ++i) {
248 if (idxBetween(i, s1.idx(), s2.idx())) {
249 MathArray const & c = cell(i);
251 int y1 = c.yo() - c.ascent();
252 int x2 = c.xo() + c.width();
253 int y2 = c.yo() + c.descent();
254 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
261 void MathNestInset::validate(LaTeXFeatures & features) const
263 for (idx_type i = 0; i < nargs(); ++i)
264 cell(i).validate(features);
268 void MathNestInset::replace(ReplaceData & rep)
270 for (idx_type i = 0; i < nargs(); ++i)
271 cell(i).replace(rep);
275 bool MathNestInset::contains(MathArray const & ar) const
277 for (idx_type i = 0; i < nargs(); ++i)
278 if (cell(i).contains(ar))
284 bool MathNestInset::lock() const
290 void MathNestInset::lock(bool l)
296 bool MathNestInset::isActive() const
302 MathArray MathNestInset::glue() const
305 for (size_t i = 0; i < nargs(); ++i)
311 void MathNestInset::write(WriteStream & os) const
313 os << '\\' << name().c_str();
314 for (size_t i = 0; i < nargs(); ++i)
315 os << '{' << cell(i) << '}';
317 os.pendingSpace(true);
318 if (lock_ && !os.latex()) {
320 os.pendingSpace(true);
325 void MathNestInset::normalize(NormalStream & os) const
327 os << '[' << name().c_str();
328 for (size_t i = 0; i < nargs(); ++i)
329 os << ' ' << cell(i);
334 int MathNestInset::latex(Buffer const &, std::ostream & os,
335 OutputParams const & runparams) const
337 WriteStream wi(os, runparams.moving_arg, true);
343 void MathNestInset::notifyCursorLeaves(LCursor & cur)
349 MathArray & ar = cur.cell();
350 // remove base-only "scripts"
351 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
352 MathScriptInset * p = operator[](i).nucleus()->asScriptInset();
353 if (p && p->nargs() == 1) {
354 MathArray ar = p->nuc();
357 cur.adjust(i, ar.size() - 1);
361 // glue adjacent font insets of the same kind
362 for (pos_type i = 0; i + 1 < size(); ++i) {
363 MathFontInset * p = operator[](i).nucleus()->asFontInset();
364 MathFontInset const * q = operator[](i + 1)->asFontInset();
365 if (p && q && p->name() == q->name()) {
366 p->cell(0).append(q->cell(0));
375 void MathNestInset::handleFont
376 (LCursor & cur, string const & arg, string const & font)
378 // this whole function is a hack and won't work for incremental font
380 recordUndo(cur, Undo::ATOMIC);
382 if (cur.inset().asMathInset()->name() == font)
383 cur.handleFont(font);
385 cur.handleNest(createMathInset(font));
391 void MathNestInset::handleFont2(LCursor & cur, string const & arg)
393 recordUndo(cur, Undo::ATOMIC);
396 bv_funcs::string2font(arg, font, b);
397 if (font.color() != LColor::inherit) {
398 MathAtom at = MathAtom(new MathColorInset(true, font.color()));
399 cur.handleNest(at, 0);
404 void MathNestInset::doDispatch(LCursor & cur, FuncRequest & cmd)
406 //lyxerr << "MathNestInset: request: " << cmd << std::endl;
407 //CursorSlice sl = cur.current();
409 switch (cmd.action) {
413 cur.message(_("Paste"));
414 replaceSelection(cur);
416 istringstream is(cmd.argument);
418 string const selection = lyx::cap::getSelection(cur.buffer(), n);
419 cur.niceInsert(selection);
420 cur.clearSelection(); // bug 393
421 cur.bv().switchKeyMap();
428 cutSelection(cur, true, true);
429 cur.message(_("Cut"));
430 // Prevent stale position >= size crash
431 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
437 cur.message(_("Copy"));
440 case LFUN_MOUSE_PRESS:
441 lfunMousePress(cur, cmd);
444 case LFUN_MOUSE_MOTION:
445 lfunMouseMotion(cur, cmd);
448 case LFUN_MOUSE_RELEASE:
449 lfunMouseRelease(cur, cmd);
452 case LFUN_FINISHED_LEFT:
453 cur.bv().cursor() = cur;
456 case LFUN_FINISHED_RIGHT:
458 cur.bv().cursor() = cur;
461 case LFUN_FINISHED_UP:
462 cur.bv().cursor() = cur;
465 case LFUN_FINISHED_DOWN:
467 cur.bv().cursor() = cur;
472 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
473 cur.autocorrect() = false;
475 cur.macroModeClose();
476 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
477 cur.pushLeft(*cur.nextAtom().nucleus());
478 cur.inset().idxFirst(cur);
479 } else if (cur.posRight() || idxRight(cur)
480 || cur.popRight() || cur.selection())
483 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
490 cur.selHandle(cmd.action == LFUN_LEFTSEL);
491 cur.autocorrect() = false;
493 cur.macroModeClose();
494 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
496 cur.push(*cur.nextAtom().nucleus());
497 cur.inset().idxLast(cur);
498 } else if (cur.posLeft() || idxLeft(cur)
499 || cur.popLeft() || cur.selection())
502 cmd = FuncRequest(LFUN_FINISHED_LEFT);
509 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
510 if (cur.inMacroMode()) {
512 cur.macroModeClose();
515 cur.selHandle(cmd.action == LFUN_UPSEL);
517 cmd = FuncRequest(LFUN_FINISHED_UP);
520 // fixes bug 1598. Please check!
526 if (cur.inMacroMode()) {
527 cur.macroModeClose();
530 cur.selHandle(cmd.action == LFUN_DOWNSEL);
532 cmd = FuncRequest(LFUN_FINISHED_DOWN);
535 // fixes bug 1598. Please check!
539 case LFUN_MOUSE_DOUBLE:
540 case LFUN_MOUSE_TRIPLE:
545 cur.selection() = true;
546 cur.pos() = cur.lastpos();
547 cur.idx() = cur.lastidx();
550 case LFUN_UP_PARAGRAPHSEL:
551 case LFUN_UP_PARAGRAPH:
552 case LFUN_DOWN_PARAGRAPHSEL:
553 case LFUN_DOWN_PARAGRAPH:
558 case LFUN_WORDLEFTSEL:
560 cur.selHandle(cmd.action == LFUN_WORDLEFTSEL || cmd.action == LFUN_HOMESEL);
561 cur.macroModeClose();
562 if (cur.pos() != 0) {
564 } else if (cur.col() != 0) {
565 cur.idx() -= cur.col();
567 } else if (cur.idx() != 0) {
571 cmd = FuncRequest(LFUN_FINISHED_LEFT);
576 case LFUN_WORDRIGHTSEL:
580 cur.selHandle(cmd.action == LFUN_WORDRIGHTSEL || cmd.action == LFUN_ENDSEL);
581 cur.macroModeClose();
583 if (cur.pos() != cur.lastpos()) {
584 cur.pos() = cur.lastpos();
585 } else if (ncols() && (cur.col() != cur.lastcol())) {
586 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
587 cur.pos() = cur.lastpos();
588 } else if (cur.idx() != cur.lastidx()) {
589 cur.idx() = cur.lastidx();
590 cur.pos() = cur.lastpos();
592 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
599 cmd = FuncRequest(LFUN_FINISHED_LEFT);
605 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
609 case LFUN_CELL_FORWARD:
610 cur.inset().idxNext(cur);
613 case LFUN_CELL_BACKWARD:
614 cur.inset().idxPrev(cur);
617 case LFUN_DELETE_WORD_BACKWARD:
621 recordUndoInset(cur, Undo::ATOMIC);
623 recordUndo(cur, Undo::ATOMIC);
627 case LFUN_DELETE_WORD_FORWARD:
635 cur.clearSelection();
637 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
642 case LFUN_INSET_TOGGLE:
645 if (cur.pos() != cur.lastpos()) {
646 // toggle previous inset ...
647 cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock());
648 } else if (cur.popLeft() && cur.pos() != cur.lastpos()) {
649 // ... or enclosing inset if we are in the last inset position
650 cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock());
655 case LFUN_SELFINSERT:
656 if (cmd.argument.size() != 1) {
658 cur.insert(cmd.argument);
661 // Don't record undo steps if we are in macro mode and
662 // cmd.argument is the next character of the macro name.
663 // Otherwise we'll get an invalid cursor if we undo after
664 // the macro was finished and the macro is a known command,
665 // e.g. sqrt. LCursor::macroModeClose replaces in this case
666 // the MathUnknownInset with name "frac" by an empty
667 // MathFracInset -> a pos value > 0 is invalid.
668 // A side effect is that an undo before the macro is finished
669 // undoes the complete macro, not only the last character.
670 if (!cur.inMacroMode())
673 // spacial handling of space. If we insert an inset
674 // via macro mode, we want to put the cursor inside it
675 // if relevant. Think typing "\frac<space>".
676 if (cmd.argument[0] == ' '
677 && cur.inMacroMode() && cur.macroName() != "\\"
678 && cur.macroModeClose()) {
679 MathAtom const atom = cur.prevAtom();
680 if (atom->asNestInset() && atom->nargs() > 0) {
682 cur.pushLeft(*cur.nextInset());
684 } else if (!interpret(cur, cmd.argument[0])) {
685 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
691 // sprintf(dispatch_buffer, "%d %d",);
695 lyxerr << "LFUN_SETXY broken!" << endl;
698 istringstream is(cmd.argument);
700 cur.setScreenPos(x, y);
704 // Special casing for superscript in case of LyX handling
706 case LFUN_CIRCUMFLEX:
707 if (cmd.argument.empty()) {
708 // do superscript if LyX handles
710 recordUndo(cur, Undo::ATOMIC);
728 case LFUN_HUNG_UMLAUT:
732 case LFUN_FREEFONT_APPLY:
733 case LFUN_FREEFONT_UPDATE:
734 handleFont2(cur, cmd.argument);
738 if (currentMode() == TEXT_MODE)
739 handleFont(cur, cmd.argument, "textbf");
741 handleFont(cur, cmd.argument, "mathbf");
744 if (currentMode() == TEXT_MODE)
745 handleFont(cur, cmd.argument, "textsf");
747 handleFont(cur, cmd.argument, "mathsf");
750 if (currentMode() == TEXT_MODE)
751 handleFont(cur, cmd.argument, "emph");
753 handleFont(cur, cmd.argument, "mathcal");
756 if (currentMode() == TEXT_MODE)
757 handleFont(cur, cmd.argument, "textrm");
759 handleFont(cur, cmd.argument, "mathrm");
762 if (currentMode() == TEXT_MODE)
763 handleFont(cur, cmd.argument, "texttt");
765 handleFont(cur, cmd.argument, "mathtt");
768 handleFont(cur, cmd.argument, "mathfrak");
771 if (currentMode() == TEXT_MODE)
772 handleFont(cur, cmd.argument, "textit");
774 handleFont(cur, cmd.argument, "mathit");
777 if (currentMode() == TEXT_MODE)
778 // FIXME: should be "noun"
779 handleFont(cur, cmd.argument, "textsc");
781 handleFont(cur, cmd.argument, "mathbb");
783 //case LFUN_FREEFONT_APPLY:
784 handleFont(cur, cmd.argument, "textrm");
787 handleFont(cur, cmd.argument, "textnormal");
792 // ignore math-mode on when already in math mode
793 if (currentMode() == InsetBase::MATH_MODE && cmd.argument == "on")
795 cur.macroModeClose();
797 //cur.plainInsert(MathAtom(new MathMBoxInset(cur.bv())));
798 cur.plainInsert(MathAtom(new MathBoxInset("mbox")));
800 cur.pushLeft(*cur.nextInset());
802 if (currentMode() == InsetBase::TEXT_MODE) {
803 cur.niceInsert(MathAtom(new MathHullInset("simple")));
804 cur.message(_("create new math text environment ($...$)"));
806 handleFont(cur, cmd.argument, "textrm");
807 cur.message(_("entered math text mode (textrm)"));
819 case LFUN_INSERT_MATRIX: {
820 recordUndo(cur, Undo::ATOMIC);
825 istringstream is(cmd.argument);
826 is >> m >> n >> v_align >> h_align;
833 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
837 case LFUN_MATH_DELIM: {
838 lyxerr << "MathNestInset::LFUN_MATH_DELIM" << endl;
840 string rs = lyx::support::split(cmd.argument, ls, ' ');
841 // Reasonable default values
846 recordUndo(cur, Undo::ATOMIC);
847 // Don't do this with multi-cell selections
848 if (cur.selBegin().idx() == cur.selEnd().idx())
849 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
853 case LFUN_SPACE_INSERT:
854 case LFUN_MATH_SPACE:
855 recordUndo(cur, Undo::ATOMIC);
856 cur.insert(MathAtom(new MathSpaceInset(",")));
860 // interpret this as if a backslash was typed
861 recordUndo(cur, Undo::ATOMIC);
862 interpret(cur, '\\');
866 // interpret this as if a _ was typed
867 recordUndo(cur, Undo::ATOMIC);
871 case LFUN_SUPERSCRIPT:
872 // interpret this as if a ^ was typed
873 recordUndo(cur, Undo::ATOMIC);
877 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
878 // handling such that "self-insert" works on "arbitrary stuff" too, and
879 // math-insert only handles special math things like "matrix".
880 case LFUN_INSERT_MATH: {
881 recordUndo(cur, Undo::ATOMIC);
883 asArray(cmd.argument, ar);
885 if (cmd.argument == "\\root")
887 // math macros are nest insets and may have 0 cells.
888 // handleNest would crash in this case.
889 if (ar.size() == 1 && (ar[0].nucleus()->asNestInset()) &&
890 ar[0].nucleus()->nargs() > MathInset::idx_type(cell)) {
891 cur.handleNest(ar[0], cell);
893 cur.niceInsert(cmd.argument);
897 case LFUN_DIALOG_SHOW_NEW_INSET: {
898 string const & name = cmd.argument;
902 data = tmp.createDialogStr(name);
904 cur.bv().owner()->getDialogs().show(name, data, 0);
909 MathDimInset::doDispatch(cur, cmd);
915 bool MathNestInset::getStatus(LCursor & /*cur*/, FuncRequest const & cmd,
916 FuncStatus & flag) const
918 // the font related toggles
919 //string tc = "mathnormal";
921 string const arg = cmd.argument;
922 switch (cmd.action) {
923 case LFUN_TABULAR_FEATURE:
927 case LFUN_TABULAR_FEATURE:
928 // FIXME: check temporarily disabled
930 char align = mathcursor::valign();
935 if (cmd.argument.empty()) {
939 if (!contains("tcb", cmd.argument[0])) {
943 flag.setOnOff(cmd.argument[0] == align);
946 /// We have to handle them since 1.4 blocks all unhandled actions
957 case LFUN_MATH_MUTATE:
958 //flag.setOnOff(mathcursor::formula()->hullType() == cmd.argument);
959 flag.setOnOff(false);
962 // we just need to be in math mode to enable that
964 case LFUN_MATH_SPACE:
965 case LFUN_MATH_LIMITS:
966 case LFUN_MATH_NONUMBER:
967 case LFUN_MATH_NUMBER:
968 case LFUN_MATH_EXTERN:
973 flag.enabled(currentMode() != TEXT_MODE);
976 case LFUN_INSERT_MATH: {
978 arg == "\\textbf" || arg == "\\textsf" ||
979 arg == "\\textrm" || arg == "\\textmd" ||
980 arg == "\\textit" || arg == "\\textsc" ||
981 arg == "\\textsl" || arg == "\\textup" ||
982 arg == "\\texttt" || arg == "\\textbb" ||
983 arg == "\\textnormal";
984 flag.enabled(currentMode() != TEXT_MODE || textarg);
988 case LFUN_INSERT_MATRIX:
989 flag.enabled(currentMode() == MATH_MODE);
999 void MathNestInset::edit(LCursor & cur, bool left)
1002 cur.idx() = left ? 0 : cur.lastidx();
1003 cur.pos() = left ? 0 : cur.lastpos();
1005 //lyxerr << "MathNestInset::edit, cur:\n" << cur << endl;
1009 InsetBase * MathNestInset::editXY(LCursor & cur, int x, int y)
1012 int dist_min = 1000000;
1013 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1014 int const d = cell(i).dist(x, y);
1020 MathArray & ar = cell(idx_min);
1022 cur.idx() = idx_min;
1023 cur.pos() = ar.x2pos(x - ar.xo());
1024 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1025 if (dist_min == 0) {
1027 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1028 if (ar[i]->covers(x, y))
1029 return ar[i].nucleus()->editXY(cur, x, y);
1035 void MathNestInset::lfunMousePress(LCursor & cur, FuncRequest & cmd)
1037 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1038 if (cmd.button() == mouse_button::button1) {
1039 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1040 cur.bv().mouseSetCursor(cur);
1043 if (cmd.button() == mouse_button::button2) {
1044 cur.dispatch(FuncRequest(LFUN_PASTESELECTION));
1049 void MathNestInset::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
1051 // only select with button 1
1052 if (cmd.button() == mouse_button::button1) {
1053 LCursor & bvcur = cur.bv().cursor();
1054 if (bvcur.anchor_.hasPart(cur)) {
1055 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1056 bvcur.setCursor(cur);
1057 bvcur.selection() = true;
1058 //lyxerr << "MOTION " << bvcur << endl;
1067 void MathNestInset::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
1069 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1071 if (cmd.button() == mouse_button::button1) {
1072 //cur.bv().stuffClipboard(cur.grabSelection());
1076 if (cmd.button() == mouse_button::button2) {
1078 asArray(cur.bv().getClipboard(), ar);
1079 cur.clearSelection();
1080 cur.setScreenPos(cmd.x, cmd.y);
1086 if (cmd.button() == mouse_button::button3) {
1087 // try to dispatch to enclosed insets first
1088 cur.bv().owner()->getDialogs().show("mathpanel");
1096 bool MathNestInset::interpret(LCursor & cur, char c)
1098 //lyxerr << "interpret 2: '" << c << "'" << endl;
1102 if (cur.inMacroMode()) {
1103 string name = cur.macroName();
1105 /// are we currently typing '#1' or '#2' or...?
1106 if (name == "\\#") {
1109 if (n >= 1 && n <= 9)
1110 cur.insert(new MathMacroArgument(n));
1115 cur.activeMacro()->setName(name + c);
1119 // handle 'special char' macros
1124 if (currentMode() == MathInset::TEXT_MODE)
1125 cur.niceInsert(createMathInset("textbackslash"));
1127 cur.niceInsert(createMathInset("backslash"));
1128 } else if (c == '{') {
1130 cur.niceInsert(MathAtom(new MathBraceInset));
1131 } else if (c == '%') {
1133 cur.niceInsert(MathAtom(new MathCommentInset));
1134 } else if (c == '#') {
1135 BOOST_ASSERT(cur.activeMacro());
1136 cur.activeMacro()->setName(name + c);
1139 cur.niceInsert(createMathInset(string(1, c)));
1144 // leave macro mode and try again if necessary
1145 cur.macroModeClose();
1147 cur.niceInsert(MathAtom(new MathBraceInset));
1153 // This is annoying as one has to press <space> far too often.
1157 // leave autocorrect mode if necessary
1158 if (autocorrect() && c == ' ') {
1159 autocorrect() = false;
1164 // just clear selection on pressing the space bar
1165 if (cur.selection() && c == ' ') {
1166 cur.selection() = false;
1173 //lyxerr << "starting with macro" << endl;
1174 cur.insert(MathAtom(new MathUnknownInset("\\", false)));
1179 if (currentMode() == MathInset::TEXT_MODE)
1185 if (currentMode() == MathInset::TEXT_MODE) {
1186 // insert spaces in text mode,
1187 // but suppress direct insertion of two spaces in a row
1188 // the still allows typing '<space>a<space>' and deleting the 'a', but
1189 // it is better than nothing...
1190 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
1194 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1195 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1200 // if are at the very end, leave the formula
1201 return cur.pos() != cur.lastpos();
1204 // These shouldn't work in text mode:
1205 if (currentMode() != MathInset::TEXT_MODE) {
1215 cur.niceInsert(createMathInset("sim"));
1220 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' || c == '%'
1221 || c == '_' || c == '^') {
1222 cur.niceInsert(createMathInset(string(1, c)));
1227 // try auto-correction
1228 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1231 // no special circumstances, so insert the character without any fuss
1233 cur.autocorrect() = true;
1238 bool MathNestInset::script(LCursor & cur, bool up)
1240 // Hack to get \^ and \_ working
1241 lyxerr << "handling script: up: " << up << endl;
1242 if (cur.inMacroMode() && cur.macroName() == "\\") {
1244 cur.niceInsert(createMathInset("mathcircumflex"));
1246 interpret(cur, '_');
1250 cur.macroModeClose();
1251 string safe = grabAndEraseSelection(cur);
1252 if (asScriptInset() && cur.idx() == 0) {
1253 // we are in a nucleus of a script inset, move to _our_ script
1254 MathScriptInset * inset = asScriptInset();
1255 lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1257 cur.idx() = inset->idxOfScript(up);
1259 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1261 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1264 cur.idx() = inset->idxOfScript(up);
1265 cur.pos() = cur.lastpos();
1267 // convert the thing to our left to a scriptinset or create a new
1268 // one if in the very first position of the array
1269 if (cur.pos() == 0) {
1270 //lyxerr << "new scriptinset" << endl;
1271 cur.insert(new MathScriptInset(up));
1273 //lyxerr << "converting prev atom " << endl;
1274 cur.prevAtom() = MathAtom(new MathScriptInset(cur.prevAtom(), up));
1277 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1282 //lyxerr << "pasting 1: safe:\n" << safe << endl;
1285 //lyxerr << "pasting 2: safe:\n" << safe << endl;