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"
54 #include "frontends/nullpainter.h"
58 using lyx::cap::copySelection;
59 using lyx::cap::grabAndEraseSelection;
60 using lyx::cap::cutSelection;
61 using lyx::cap::replaceSelection;
62 using lyx::cap::selClearOrDel;
66 using std::istringstream;
69 MathNestInset::MathNestInset(idx_type nargs)
70 : cells_(nargs), lock_(false)
74 MathInset::idx_type MathNestInset::nargs() const
80 MathArray & MathNestInset::cell(idx_type i)
86 MathArray const & MathNestInset::cell(idx_type i) const
92 void MathNestInset::cursorPos(CursorSlice const & sl, bool /*boundary*/,
93 int & x, int & y) const
95 // FIXME: This is a hack. Ideally, the coord cache should not store
96 // absolute positions, but relative ones. This would mean to call
97 // setXY() not in MathArray::draw(), but in the parent insets' draw()
98 // with the correctly adjusted x,y values. But this means that we'd have
99 // to touch all (math)inset's draw() methods. Right now, we'll store
100 // absolute value, and make them here relative, only to make them
101 // absolute again when actually drawing the cursor. What a mess.
102 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
103 MathArray const & ar = sl.cell();
104 if (!theCoords.getArrays().has(&ar)) {
105 // this can (semi-)legally happen if we just created this cell
106 // and it never has been drawn before. So don't ASSERT.
107 //lyxerr << "no cached data for array " << &ar << endl;
112 Point const pt = theCoords.getArrays().xy(&ar);
113 if (!theCoords.getInsets().has(this)) {
115 //lyxerr << "no cached data for inset " << this << endl;
120 Point const pt2 = theCoords.getInsets().xy(this);
121 //lyxerr << "retrieving position cache for MathArray "
122 // << pt.x_ << ' ' << pt.y_ << std::endl;
123 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
125 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
126 // << " asc: " << ascent() << " des: " << descent()
127 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
128 // move cursor visually into empty cells ("blue rectangles");
134 void MathNestInset::metrics(MetricsInfo const & mi) const
137 for (idx_type i = 0, n = nargs(); i != n; ++i)
142 bool MathNestInset::idxNext(LCursor & cur) const
144 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
145 if (cur.idx() == cur.lastidx())
153 bool MathNestInset::idxRight(LCursor & cur) const
159 bool MathNestInset::idxPrev(LCursor & cur) const
161 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
165 cur.pos() = cur.lastpos();
170 bool MathNestInset::idxLeft(LCursor & cur) const
176 bool MathNestInset::idxFirst(LCursor & cur) const
178 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
187 bool MathNestInset::idxLast(LCursor & cur) const
189 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
192 cur.idx() = cur.lastidx();
193 cur.pos() = cur.lastpos();
198 void MathNestInset::dump() const
200 WriteStream os(lyxerr);
201 os << "---------------------------------------------\n";
204 for (idx_type i = 0, n = nargs(); i != n; ++i)
205 os << cell(i) << "\n";
206 os << "---------------------------------------------\n";
210 void MathNestInset::draw(PainterInfo & pi, int x, int y) const
214 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
217 setPosCache(pi, x, y);
221 void MathNestInset::drawSelection(PainterInfo & pi, int x, int y) const
223 // this should use the x/y values given, not the cached values
224 LCursor & cur = pi.base.bv->cursor();
225 if (!cur.selection())
227 if (!ptr_cmp(&cur.inset(), this))
230 // FIXME: hack to get position cache warm
231 static NullPainter nop;
232 PainterInfo pinop(pi);
236 CursorSlice s1 = cur.selBegin();
237 CursorSlice s2 = cur.selEnd();
238 //lyxerr << "MathNestInset::drawing selection: "
239 // << " s1: " << s1 << " s2: " << s2 << endl;
240 if (s1.idx() == s2.idx()) {
241 MathArray const & c = cell(s1.idx());
242 int x1 = c.xo() + c.pos2x(s1.pos());
243 int y1 = c.yo() - c.ascent();
244 int x2 = c.xo() + c.pos2x(s2.pos());
245 int y2 = c.yo() + c.descent();
246 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
247 //lyxerr << "MathNestInset::drawing selection 3: "
248 // << " x1: " << x1 << " x2: " << x2
249 // << " y1: " << y1 << " y2: " << y2 << endl;
251 for (idx_type i = 0; i < nargs(); ++i) {
252 if (idxBetween(i, s1.idx(), s2.idx())) {
253 MathArray const & c = cell(i);
255 int y1 = c.yo() - c.ascent();
256 int x2 = c.xo() + c.width();
257 int y2 = c.yo() + c.descent();
258 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
265 void MathNestInset::validate(LaTeXFeatures & features) const
267 for (idx_type i = 0; i < nargs(); ++i)
268 cell(i).validate(features);
272 void MathNestInset::replace(ReplaceData & rep)
274 for (idx_type i = 0; i < nargs(); ++i)
275 cell(i).replace(rep);
279 bool MathNestInset::contains(MathArray const & ar) const
281 for (idx_type i = 0; i < nargs(); ++i)
282 if (cell(i).contains(ar))
288 bool MathNestInset::lock() const
294 void MathNestInset::lock(bool l)
300 bool MathNestInset::isActive() const
306 MathArray MathNestInset::glue() const
309 for (size_t i = 0; i < nargs(); ++i)
315 void MathNestInset::write(WriteStream & os) const
317 os << '\\' << name().c_str();
318 for (size_t i = 0; i < nargs(); ++i)
319 os << '{' << cell(i) << '}';
321 os.pendingSpace(true);
322 if (lock_ && !os.latex()) {
324 os.pendingSpace(true);
329 void MathNestInset::normalize(NormalStream & os) const
331 os << '[' << name().c_str();
332 for (size_t i = 0; i < nargs(); ++i)
333 os << ' ' << cell(i);
338 int MathNestInset::latex(Buffer const &, std::ostream & os,
339 OutputParams const & runparams) const
341 WriteStream wi(os, runparams.moving_arg, true);
347 void MathNestInset::notifyCursorLeaves(LCursor & cur)
353 MathArray & ar = cur.cell();
354 // remove base-only "scripts"
355 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
356 MathScriptInset * p = operator[](i).nucleus()->asScriptInset();
357 if (p && p->nargs() == 1) {
358 MathArray ar = p->nuc();
361 cur.adjust(i, ar.size() - 1);
365 // glue adjacent font insets of the same kind
366 for (pos_type i = 0; i + 1 < size(); ++i) {
367 MathFontInset * p = operator[](i).nucleus()->asFontInset();
368 MathFontInset const * q = operator[](i + 1)->asFontInset();
369 if (p && q && p->name() == q->name()) {
370 p->cell(0).append(q->cell(0));
379 void MathNestInset::handleFont
380 (LCursor & cur, string const & arg, string const & font)
382 // this whole function is a hack and won't work for incremental font
384 recordUndo(cur, Undo::ATOMIC);
386 if (cur.inset().asMathInset()->name() == font)
387 cur.handleFont(font);
389 cur.handleNest(createMathInset(font));
395 void MathNestInset::handleFont2(LCursor & cur, string const & arg)
397 recordUndo(cur, Undo::ATOMIC);
400 bv_funcs::string2font(arg, font, b);
401 if (font.color() != LColor::inherit) {
402 MathAtom at = MathAtom(new MathColorInset(true, font.color()));
403 cur.handleNest(at, 0);
408 void MathNestInset::doDispatch(LCursor & cur, FuncRequest & cmd)
410 //lyxerr << "MathNestInset: request: " << cmd << std::endl;
411 //CursorSlice sl = cur.current();
413 switch (cmd.action) {
417 cur.message(_("Paste"));
418 replaceSelection(cur);
420 istringstream is(cmd.argument);
422 string const selection = lyx::cap::getSelection(cur.buffer(), n);
423 cur.niceInsert(selection);
424 cur.clearSelection(); // bug 393
425 cur.bv().switchKeyMap();
432 cutSelection(cur, true, true);
433 cur.message(_("Cut"));
434 // Prevent stale position >= size crash
435 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
441 cur.message(_("Copy"));
444 case LFUN_MOUSE_PRESS:
445 lfunMousePress(cur, cmd);
448 case LFUN_MOUSE_MOTION:
449 lfunMouseMotion(cur, cmd);
452 case LFUN_MOUSE_RELEASE:
453 lfunMouseRelease(cur, cmd);
456 case LFUN_FINISHED_LEFT:
457 cur.bv().cursor() = cur;
460 case LFUN_FINISHED_RIGHT:
462 cur.bv().cursor() = cur;
465 case LFUN_FINISHED_UP:
466 cur.bv().cursor() = cur;
469 case LFUN_FINISHED_DOWN:
471 cur.bv().cursor() = cur;
476 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
477 cur.autocorrect() = false;
479 cur.macroModeClose();
480 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
481 cur.pushLeft(*cur.nextAtom().nucleus());
482 cur.inset().idxFirst(cur);
483 } else if (cur.posRight() || idxRight(cur)
484 || cur.popRight() || cur.selection())
487 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
494 cur.selHandle(cmd.action == LFUN_LEFTSEL);
495 cur.autocorrect() = false;
497 cur.macroModeClose();
498 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
500 cur.push(*cur.nextAtom().nucleus());
501 cur.inset().idxLast(cur);
502 } else if (cur.posLeft() || idxLeft(cur)
503 || cur.popLeft() || cur.selection())
506 cmd = FuncRequest(LFUN_FINISHED_LEFT);
513 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
514 if (cur.inMacroMode()) {
516 cur.macroModeClose();
519 cur.selHandle(cmd.action == LFUN_UPSEL);
521 cmd = FuncRequest(LFUN_FINISHED_UP);
524 // fixes bug 1598. Please check!
530 if (cur.inMacroMode()) {
531 cur.macroModeClose();
534 cur.selHandle(cmd.action == LFUN_DOWNSEL);
536 cmd = FuncRequest(LFUN_FINISHED_DOWN);
539 // fixes bug 1598. Please check!
543 case LFUN_MOUSE_DOUBLE:
544 case LFUN_MOUSE_TRIPLE:
549 cur.selection() = true;
550 cur.pos() = cur.lastpos();
551 cur.idx() = cur.lastidx();
554 case LFUN_UP_PARAGRAPHSEL:
555 case LFUN_UP_PARAGRAPH:
556 case LFUN_DOWN_PARAGRAPHSEL:
557 case LFUN_DOWN_PARAGRAPH:
562 case LFUN_WORDLEFTSEL:
564 cur.selHandle(cmd.action == LFUN_WORDLEFTSEL || cmd.action == LFUN_HOMESEL);
565 cur.macroModeClose();
566 if (cur.pos() != 0) {
568 } else if (cur.col() != 0) {
569 cur.idx() -= cur.col();
571 } else if (cur.idx() != 0) {
575 cmd = FuncRequest(LFUN_FINISHED_LEFT);
580 case LFUN_WORDRIGHTSEL:
584 cur.selHandle(cmd.action == LFUN_WORDRIGHTSEL || cmd.action == LFUN_ENDSEL);
585 cur.macroModeClose();
587 if (cur.pos() != cur.lastpos()) {
588 cur.pos() = cur.lastpos();
589 } else if (ncols() && (cur.col() != cur.lastcol())) {
590 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
591 cur.pos() = cur.lastpos();
592 } else if (cur.idx() != cur.lastidx()) {
593 cur.idx() = cur.lastidx();
594 cur.pos() = cur.lastpos();
596 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
603 cmd = FuncRequest(LFUN_FINISHED_LEFT);
609 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
613 case LFUN_CELL_FORWARD:
614 cur.inset().idxNext(cur);
617 case LFUN_CELL_BACKWARD:
618 cur.inset().idxPrev(cur);
621 case LFUN_DELETE_WORD_BACKWARD:
625 recordUndoInset(cur, Undo::ATOMIC);
627 recordUndo(cur, Undo::ATOMIC);
631 case LFUN_DELETE_WORD_FORWARD:
639 cur.clearSelection();
641 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
646 case LFUN_INSET_TOGGLE:
649 if (cur.pos() != cur.lastpos()) {
650 // toggle previous inset ...
651 cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock());
652 } else if (cur.popLeft() && cur.pos() != cur.lastpos()) {
653 // ... or enclosing inset if we are in the last inset position
654 cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock());
659 case LFUN_SELFINSERT:
660 if (cmd.argument.size() != 1) {
662 cur.insert(cmd.argument);
665 // Don't record undo steps if we are in macro mode and
666 // cmd.argument is the next character of the macro name.
667 // Otherwise we'll get an invalid cursor if we undo after
668 // the macro was finished and the macro is a known command,
669 // e.g. sqrt. LCursor::macroModeClose replaces in this case
670 // the MathUnknownInset with name "frac" by an empty
671 // MathFracInset -> a pos value > 0 is invalid.
672 // A side effect is that an undo before the macro is finished
673 // undoes the complete macro, not only the last character.
674 if (!cur.inMacroMode())
677 // spacial handling of space. If we insert an inset
678 // via macro mode, we want to put the cursor inside it
679 // if relevant. Think typing "\frac<space>".
680 if (cmd.argument[0] == ' '
681 && cur.inMacroMode() && cur.macroName() != "\\"
682 && cur.macroModeClose()) {
683 MathAtom const atom = cur.prevAtom();
684 if (atom->asNestInset() && atom->nargs() > 0) {
686 cur.pushLeft(*cur.nextInset());
688 } else if (!interpret(cur, cmd.argument[0])) {
689 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
695 // sprintf(dispatch_buffer, "%d %d",);
699 lyxerr << "LFUN_SETXY broken!" << endl;
702 istringstream is(cmd.argument);
704 cur.setScreenPos(x, y);
708 // Special casing for superscript in case of LyX handling
710 case LFUN_CIRCUMFLEX:
711 if (cmd.argument.empty()) {
712 // do superscript if LyX handles
714 recordUndo(cur, Undo::ATOMIC);
732 case LFUN_HUNG_UMLAUT:
736 case LFUN_FREEFONT_APPLY:
737 case LFUN_FREEFONT_UPDATE:
738 handleFont2(cur, cmd.argument);
742 if (currentMode() == TEXT_MODE)
743 handleFont(cur, cmd.argument, "textbf");
745 handleFont(cur, cmd.argument, "mathbf");
748 if (currentMode() == TEXT_MODE)
749 handleFont(cur, cmd.argument, "textsf");
751 handleFont(cur, cmd.argument, "mathsf");
754 if (currentMode() == TEXT_MODE)
755 handleFont(cur, cmd.argument, "emph");
757 handleFont(cur, cmd.argument, "mathcal");
760 if (currentMode() == TEXT_MODE)
761 handleFont(cur, cmd.argument, "textrm");
763 handleFont(cur, cmd.argument, "mathrm");
766 if (currentMode() == TEXT_MODE)
767 handleFont(cur, cmd.argument, "texttt");
769 handleFont(cur, cmd.argument, "mathtt");
772 handleFont(cur, cmd.argument, "mathfrak");
775 if (currentMode() == TEXT_MODE)
776 handleFont(cur, cmd.argument, "textit");
778 handleFont(cur, cmd.argument, "mathit");
781 if (currentMode() == TEXT_MODE)
782 // FIXME: should be "noun"
783 handleFont(cur, cmd.argument, "textsc");
785 handleFont(cur, cmd.argument, "mathbb");
787 //case LFUN_FREEFONT_APPLY:
788 handleFont(cur, cmd.argument, "textrm");
791 handleFont(cur, cmd.argument, "textnormal");
796 // ignore math-mode on when already in math mode
797 if (currentMode() == InsetBase::MATH_MODE && cmd.argument == "on")
799 cur.macroModeClose();
801 //cur.plainInsert(MathAtom(new MathMBoxInset(cur.bv())));
802 cur.plainInsert(MathAtom(new MathBoxInset("mbox")));
804 cur.pushLeft(*cur.nextInset());
806 if (currentMode() == InsetBase::TEXT_MODE) {
807 cur.niceInsert(MathAtom(new MathHullInset("simple")));
808 cur.message(_("create new math text environment ($...$)"));
810 handleFont(cur, cmd.argument, "textrm");
811 cur.message(_("entered math text mode (textrm)"));
823 case LFUN_INSERT_MATRIX: {
824 recordUndo(cur, Undo::ATOMIC);
829 istringstream is(cmd.argument);
830 is >> m >> n >> v_align >> h_align;
837 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
841 case LFUN_MATH_DELIM: {
842 lyxerr << "MathNestInset::LFUN_MATH_DELIM" << endl;
844 string rs = lyx::support::split(cmd.argument, ls, ' ');
845 // Reasonable default values
850 recordUndo(cur, Undo::ATOMIC);
851 // Don't do this with multi-cell selections
852 if (cur.selBegin().idx() == cur.selEnd().idx())
853 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
857 case LFUN_SPACE_INSERT:
858 case LFUN_MATH_SPACE:
859 recordUndo(cur, Undo::ATOMIC);
860 cur.insert(MathAtom(new MathSpaceInset(",")));
864 // interpret this as if a backslash was typed
865 recordUndo(cur, Undo::ATOMIC);
866 interpret(cur, '\\');
870 // interpret this as if a _ was typed
871 recordUndo(cur, Undo::ATOMIC);
875 case LFUN_SUPERSCRIPT:
876 // interpret this as if a ^ was typed
877 recordUndo(cur, Undo::ATOMIC);
881 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
882 // handling such that "self-insert" works on "arbitrary stuff" too, and
883 // math-insert only handles special math things like "matrix".
884 case LFUN_INSERT_MATH: {
885 recordUndo(cur, Undo::ATOMIC);
887 asArray(cmd.argument, ar);
889 if (cmd.argument == "\\root")
891 cur.niceInsert(cmd.argument);
895 case LFUN_DIALOG_SHOW_NEW_INSET: {
896 string const & name = cmd.argument;
900 data = tmp.createDialogStr(name);
902 cur.bv().owner()->getDialogs().show(name, data, 0);
907 MathDimInset::doDispatch(cur, cmd);
913 bool MathNestInset::getStatus(LCursor & /*cur*/, FuncRequest const & cmd,
914 FuncStatus & flag) const
916 // the font related toggles
917 //string tc = "mathnormal";
919 string const arg = cmd.argument;
920 switch (cmd.action) {
921 case LFUN_TABULAR_FEATURE:
925 case LFUN_TABULAR_FEATURE:
926 // FIXME: check temporarily disabled
928 char align = mathcursor::valign();
933 if (cmd.argument.empty()) {
937 if (!contains("tcb", cmd.argument[0])) {
941 flag.setOnOff(cmd.argument[0] == align);
944 /// We have to handle them since 1.4 blocks all unhandled actions
955 case LFUN_MATH_MUTATE:
956 //flag.setOnOff(mathcursor::formula()->hullType() == cmd.argument);
957 flag.setOnOff(false);
960 // we just need to be in math mode to enable that
962 case LFUN_MATH_SPACE:
963 case LFUN_MATH_LIMITS:
964 case LFUN_MATH_NONUMBER:
965 case LFUN_MATH_NUMBER:
966 case LFUN_MATH_EXTERN:
971 flag.enabled(currentMode() != TEXT_MODE);
974 case LFUN_INSERT_MATH: {
976 arg == "\\textbf" || arg == "\\textsf" ||
977 arg == "\\textrm" || arg == "\\textmd" ||
978 arg == "\\textit" || arg == "\\textsc" ||
979 arg == "\\textsl" || arg == "\\textup" ||
980 arg == "\\texttt" || arg == "\\textbb" ||
981 arg == "\\textnormal";
982 flag.enabled(currentMode() != TEXT_MODE || textarg);
986 case LFUN_INSERT_MATRIX:
987 flag.enabled(currentMode() == MATH_MODE);
997 void MathNestInset::edit(LCursor & cur, bool left)
1000 cur.idx() = left ? 0 : cur.lastidx();
1001 cur.pos() = left ? 0 : cur.lastpos();
1003 //lyxerr << "MathNestInset::edit, cur:\n" << cur << endl;
1007 InsetBase * MathNestInset::editXY(LCursor & cur, int x, int y)
1010 int dist_min = 1000000;
1011 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1012 int const d = cell(i).dist(x, y);
1018 MathArray & ar = cell(idx_min);
1020 cur.idx() = idx_min;
1021 cur.pos() = ar.x2pos(x - ar.xo());
1022 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1023 if (dist_min == 0) {
1025 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1026 if (ar[i]->covers(x, y))
1027 return ar[i].nucleus()->editXY(cur, x, y);
1033 void MathNestInset::lfunMousePress(LCursor & cur, FuncRequest & cmd)
1035 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1036 if (cmd.button() == mouse_button::button1) {
1037 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1038 cur.bv().mouseSetCursor(cur);
1041 if (cmd.button() == mouse_button::button2) {
1042 cur.dispatch(FuncRequest(LFUN_PASTESELECTION));
1047 void MathNestInset::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
1049 // only select with button 1
1050 if (cmd.button() == mouse_button::button1) {
1051 LCursor & bvcur = cur.bv().cursor();
1052 if (bvcur.anchor_.hasPart(cur)) {
1053 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1054 bvcur.setCursor(cur);
1055 bvcur.selection() = true;
1056 //lyxerr << "MOTION " << bvcur << endl;
1065 void MathNestInset::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
1067 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1069 if (cmd.button() == mouse_button::button1) {
1070 //cur.bv().stuffClipboard(cur.grabSelection());
1074 if (cmd.button() == mouse_button::button2) {
1076 asArray(cur.bv().getClipboard(), ar);
1077 cur.clearSelection();
1078 cur.setScreenPos(cmd.x, cmd.y);
1084 if (cmd.button() == mouse_button::button3) {
1085 // try to dispatch to enclosed insets first
1086 cur.bv().owner()->getDialogs().show("mathpanel");
1094 bool MathNestInset::interpret(LCursor & cur, char c)
1096 //lyxerr << "interpret 2: '" << c << "'" << endl;
1100 if (cur.inMacroMode()) {
1101 string name = cur.macroName();
1103 /// are we currently typing '#1' or '#2' or...?
1104 if (name == "\\#") {
1107 if (n >= 1 && n <= 9)
1108 cur.insert(new MathMacroArgument(n));
1113 cur.activeMacro()->setName(name + c);
1117 // handle 'special char' macros
1122 if (currentMode() == MathInset::TEXT_MODE)
1123 cur.niceInsert(createMathInset("textbackslash"));
1125 cur.niceInsert(createMathInset("backslash"));
1126 } else if (c == '{') {
1128 cur.niceInsert(MathAtom(new MathBraceInset));
1129 } else if (c == '%') {
1131 cur.niceInsert(MathAtom(new MathCommentInset));
1132 } else if (c == '#') {
1133 BOOST_ASSERT(cur.activeMacro());
1134 cur.activeMacro()->setName(name + c);
1137 cur.niceInsert(createMathInset(string(1, c)));
1142 // leave macro mode and try again if necessary
1143 cur.macroModeClose();
1145 cur.niceInsert(MathAtom(new MathBraceInset));
1151 // This is annoying as one has to press <space> far too often.
1155 // leave autocorrect mode if necessary
1156 if (autocorrect() && c == ' ') {
1157 autocorrect() = false;
1162 // just clear selection on pressing the space bar
1163 if (cur.selection() && c == ' ') {
1164 cur.selection() = false;
1171 //lyxerr << "starting with macro" << endl;
1172 cur.insert(MathAtom(new MathUnknownInset("\\", false)));
1177 if (currentMode() == MathInset::TEXT_MODE)
1183 if (currentMode() == MathInset::TEXT_MODE) {
1184 // insert spaces in text mode,
1185 // but suppress direct insertion of two spaces in a row
1186 // the still allows typing '<space>a<space>' and deleting the 'a', but
1187 // it is better than nothing...
1188 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
1192 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1193 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1198 // if are at the very end, leave the formula
1199 return cur.pos() != cur.lastpos();
1202 // These shouldn't work in text mode:
1203 if (currentMode() != MathInset::TEXT_MODE) {
1213 cur.niceInsert(createMathInset("sim"));
1218 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' || c == '%'
1219 || c == '_' || c == '^') {
1220 cur.niceInsert(createMathInset(string(1, c)));
1225 // try auto-correction
1226 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1229 // no special circumstances, so insert the character without any fuss
1231 cur.autocorrect() = true;
1236 bool MathNestInset::script(LCursor & cur, bool up)
1238 // Hack to get \^ and \_ working
1239 lyxerr << "handling script: up: " << up << endl;
1240 if (cur.inMacroMode() && cur.macroName() == "\\") {
1242 cur.niceInsert(createMathInset("mathcircumflex"));
1244 interpret(cur, '_');
1248 cur.macroModeClose();
1249 string safe = grabAndEraseSelection(cur);
1250 if (asScriptInset() && cur.idx() == 0) {
1251 // we are in a nucleus of a script inset, move to _our_ script
1252 MathScriptInset * inset = asScriptInset();
1253 lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1255 cur.idx() = inset->idxOfScript(up);
1257 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1259 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1262 cur.idx() = inset->idxOfScript(up);
1263 cur.pos() = cur.lastpos();
1265 // convert the thing to our left to a scriptinset or create a new
1266 // one if in the very first position of the array
1267 if (cur.pos() == 0) {
1268 //lyxerr << "new scriptinset" << endl;
1269 cur.insert(new MathScriptInset(up));
1271 //lyxerr << "converting prev atom " << endl;
1272 cur.prevAtom() = MathAtom(new MathScriptInset(cur.prevAtom(), up));
1275 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1280 //lyxerr << "pasting 1: safe:\n" << safe << endl;
1283 //lyxerr << "pasting 2: safe:\n" << safe << endl;