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);
715 script(cur, true, grabAndEraseSelection(cur));
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");
794 case LFUN_MATH_MODE: {
796 // ignore math-mode on when already in math mode
797 if (currentMode() == InsetBase::MATH_MODE && cmd.argument == "on")
799 cur.macroModeClose();
800 string const save_selection = grabAndEraseSelection(cur);
802 //cur.plainInsert(MathAtom(new MathMBoxInset(cur.bv())));
803 cur.plainInsert(MathAtom(new MathBoxInset("mbox")));
805 cur.pushLeft(*cur.nextInset());
806 cur.niceInsert(save_selection);
808 if (currentMode() == InsetBase::TEXT_MODE) {
809 cur.niceInsert(MathAtom(new MathHullInset("simple")));
810 cur.message(_("create new math text environment ($...$)"));
812 handleFont(cur, cmd.argument, "textrm");
813 cur.message(_("entered math text mode (textrm)"));
826 case LFUN_INSERT_MATRIX: {
827 recordUndo(cur, Undo::ATOMIC);
832 istringstream is(cmd.argument);
833 is >> m >> n >> v_align >> h_align;
840 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
844 case LFUN_MATH_DELIM: {
845 lyxerr << "MathNestInset::LFUN_MATH_DELIM" << endl;
847 string rs = lyx::support::split(cmd.argument, ls, ' ');
848 // Reasonable default values
853 recordUndo(cur, Undo::ATOMIC);
854 // Don't do this with multi-cell selections
855 if (cur.selBegin().idx() == cur.selEnd().idx())
856 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
860 case LFUN_SPACE_INSERT:
861 case LFUN_MATH_SPACE:
862 recordUndo(cur, Undo::ATOMIC);
863 cur.insert(MathAtom(new MathSpaceInset(",")));
867 // interpret this as if a backslash was typed
868 recordUndo(cur, Undo::ATOMIC);
869 interpret(cur, '\\');
873 // interpret this as if a _ was typed
874 recordUndo(cur, Undo::ATOMIC);
878 case LFUN_SUPERSCRIPT:
879 // interpret this as if a ^ was typed
880 recordUndo(cur, Undo::ATOMIC);
884 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
885 // handling such that "self-insert" works on "arbitrary stuff" too, and
886 // math-insert only handles special math things like "matrix".
887 case LFUN_INSERT_MATH: {
888 recordUndo(cur, Undo::ATOMIC);
889 if (cmd.argument == "^" || cmd.argument == "_") {
890 interpret(cur, cmd.argument[0]);
892 cur.niceInsert(cmd.argument);
896 case LFUN_DIALOG_SHOW_NEW_INSET: {
897 string const & name = cmd.argument;
901 data = tmp.createDialogStr(name);
903 cur.bv().owner()->getDialogs().show(name, data, 0);
908 MathDimInset::doDispatch(cur, cmd);
914 bool MathNestInset::getStatus(LCursor & /*cur*/, FuncRequest const & cmd,
915 FuncStatus & flag) const
917 // the font related toggles
918 //string tc = "mathnormal";
920 string const arg = cmd.argument;
921 switch (cmd.action) {
922 case LFUN_TABULAR_FEATURE:
926 case LFUN_TABULAR_FEATURE:
927 // FIXME: check temporarily disabled
929 char align = mathcursor::valign();
934 if (cmd.argument.empty()) {
938 if (!contains("tcb", cmd.argument[0])) {
942 flag.setOnOff(cmd.argument[0] == align);
945 /// We have to handle them since 1.4 blocks all unhandled actions
956 case LFUN_MATH_MUTATE:
957 //flag.setOnOff(mathcursor::formula()->hullType() == cmd.argument);
958 flag.setOnOff(false);
961 // we just need to be in math mode to enable that
963 case LFUN_MATH_SPACE:
964 case LFUN_MATH_LIMITS:
965 case LFUN_MATH_NONUMBER:
966 case LFUN_MATH_NUMBER:
967 case LFUN_MATH_EXTERN:
972 flag.enabled(currentMode() != TEXT_MODE);
975 case LFUN_INSERT_MATH: {
977 arg == "\\textbf" || arg == "\\textsf" ||
978 arg == "\\textrm" || arg == "\\textmd" ||
979 arg == "\\textit" || arg == "\\textsc" ||
980 arg == "\\textsl" || arg == "\\textup" ||
981 arg == "\\texttt" || arg == "\\textbb" ||
982 arg == "\\textnormal";
983 flag.enabled(currentMode() != TEXT_MODE || textarg);
987 case LFUN_INSERT_MATRIX:
988 flag.enabled(currentMode() == MATH_MODE);
998 void MathNestInset::edit(LCursor & cur, bool left)
1001 cur.idx() = left ? 0 : cur.lastidx();
1002 cur.pos() = left ? 0 : cur.lastpos();
1004 //lyxerr << "MathNestInset::edit, cur:\n" << cur << endl;
1008 InsetBase * MathNestInset::editXY(LCursor & cur, int x, int y)
1011 int dist_min = 1000000;
1012 for (idx_type i = 0, n = nargs(); i != n; ++i) {
1013 int const d = cell(i).dist(x, y);
1019 MathArray & ar = cell(idx_min);
1021 cur.idx() = idx_min;
1022 cur.pos() = ar.x2pos(x - ar.xo());
1023 //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1024 if (dist_min == 0) {
1026 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1027 if (ar[i]->covers(x, y))
1028 return ar[i].nucleus()->editXY(cur, x, y);
1034 void MathNestInset::lfunMousePress(LCursor & cur, FuncRequest & cmd)
1036 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1037 if (cmd.button() == mouse_button::button1) {
1038 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1039 cur.bv().mouseSetCursor(cur);
1042 if (cmd.button() == mouse_button::button2) {
1044 asArray(cur.bv().getClipboard(), ar);
1045 cur.clearSelection();
1046 editXY(cur, cmd.x, cmd.y);
1054 void MathNestInset::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
1056 // only select with button 1
1057 if (cmd.button() == mouse_button::button1) {
1058 LCursor & bvcur = cur.bv().cursor();
1059 if (bvcur.anchor_.hasPart(cur)) {
1060 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1061 bvcur.setCursor(cur);
1062 bvcur.selection() = true;
1063 //lyxerr << "MOTION " << bvcur << endl;
1072 void MathNestInset::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
1074 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1076 if (cmd.button() == mouse_button::button1) {
1077 //cur.bv().stuffClipboard(cur.grabSelection());
1081 if (cmd.button() == mouse_button::button3) {
1082 // try to dispatch to enclosed insets first
1083 cur.bv().owner()->getDialogs().show("mathpanel");
1091 bool MathNestInset::interpret(LCursor & cur, char c)
1093 //lyxerr << "interpret 2: '" << c << "'" << endl;
1094 string save_selection;
1095 if (c == '^' || c == '_')
1096 save_selection = grabAndEraseSelection(cur);
1101 if (cur.inMacroMode()) {
1102 string name = cur.macroName();
1104 /// are we currently typing '#1' or '#2' or...?
1105 if (name == "\\#") {
1108 if (n >= 1 && n <= 9)
1109 cur.insert(new MathMacroArgument(n));
1114 cur.activeMacro()->setName(name + c);
1118 // handle 'special char' macros
1123 if (currentMode() == MathInset::TEXT_MODE)
1124 cur.niceInsert(createMathInset("textbackslash"));
1126 cur.niceInsert(createMathInset("backslash"));
1127 } else if (c == '{') {
1129 cur.niceInsert(MathAtom(new MathBraceInset));
1130 } else if (c == '%') {
1132 cur.niceInsert(MathAtom(new MathCommentInset));
1133 } else if (c == '#') {
1134 BOOST_ASSERT(cur.activeMacro());
1135 cur.activeMacro()->setName(name + c);
1138 cur.niceInsert(createMathInset(string(1, c)));
1143 // leave macro mode and try again if necessary
1144 cur.macroModeClose();
1146 cur.niceInsert(MathAtom(new MathBraceInset));
1152 // This is annoying as one has to press <space> far too often.
1156 // leave autocorrect mode if necessary
1157 if (autocorrect() && c == ' ') {
1158 autocorrect() = false;
1163 // just clear selection on pressing the space bar
1164 if (cur.selection() && c == ' ') {
1165 cur.selection() = false;
1172 //lyxerr << "starting with macro" << endl;
1173 cur.insert(MathAtom(new MathUnknownInset("\\", false)));
1178 if (currentMode() == MathInset::TEXT_MODE)
1184 if (currentMode() == MathInset::TEXT_MODE) {
1185 // insert spaces in text mode,
1186 // but suppress direct insertion of two spaces in a row
1187 // the still allows typing '<space>a<space>' and deleting the 'a', but
1188 // it is better than nothing...
1189 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
1193 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1194 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1199 // if are at the very end, leave the formula
1200 return cur.pos() != cur.lastpos();
1203 // These shouldn't work in text mode:
1204 if (currentMode() != MathInset::TEXT_MODE) {
1206 script(cur, false, save_selection);
1210 script(cur, true, save_selection);
1214 cur.niceInsert(createMathInset("sim"));
1219 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' || c == '%'
1220 || c == '_' || c == '^') {
1221 cur.niceInsert(createMathInset(string(1, c)));
1226 // try auto-correction
1227 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1230 // no special circumstances, so insert the character without any fuss
1232 cur.autocorrect() = true;
1237 bool MathNestInset::script(LCursor & cur, bool up, string const &
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 if (asScriptInset() && cur.idx() == 0) {
1252 // we are in a nucleus of a script inset, move to _our_ script
1253 MathScriptInset * inset = asScriptInset();
1254 lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1256 cur.idx() = inset->idxOfScript(up);
1258 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1260 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1263 cur.idx() = inset->idxOfScript(up);
1264 cur.pos() = cur.lastpos();
1266 // convert the thing to our left to a scriptinset or create a new
1267 // one if in the very first position of the array
1268 if (cur.pos() == 0) {
1269 //lyxerr << "new scriptinset" << endl;
1270 cur.insert(new MathScriptInset(up));
1272 //lyxerr << "converting prev atom " << endl;
1273 cur.prevAtom() = MathAtom(new MathScriptInset(cur.prevAtom(), up));
1276 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1281 //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1282 cur.niceInsert(save_selection);
1284 //lyxerr << "inserting selection 2:\n" << save_selection << endl;