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_braceinset.h"
17 #include "math_commentinset.h"
18 #include "math_data.h"
19 #include "math_deliminset.h"
20 #include "math_factory.h"
21 #include "math_hullinset.h"
22 #include "math_mathmlstream.h"
23 #include "math_macroarg.h"
24 #include "math_mboxinset.h"
25 #include "math_parser.h"
26 #include "math_scriptinset.h"
27 #include "math_spaceinset.h"
28 #include "math_symbolinset.h"
29 #include "math_support.h"
30 #include "math_unknowninset.h"
32 #include "BufferView.h"
33 #include "CutAndPaste.h"
34 #include "FuncStatus.h"
36 #include "bufferview_funcs.h"
37 #include "coordcache.h"
40 #include "dispatchresult.h"
41 #include "funcrequest.h"
43 #include "outputparams.h"
46 #include "support/lstrings.h"
48 #include "frontends/Dialogs.h"
49 #include "frontends/LyXView.h"
50 #include "frontends/Painter.h"
54 using lyx::cap::copySelection;
55 using lyx::cap::grabAndEraseSelection;
56 using lyx::cap::cutSelection;
57 using lyx::cap::pasteSelection;
58 using lyx::cap::replaceSelection;
59 using lyx::cap::selClearOrDel;
63 using std::istringstream;
78 MathNestInset::MathNestInset(idx_type nargs)
79 : cells_(nargs), lock_(false)
83 MathInset::idx_type MathNestInset::nargs() const
89 MathArray & MathNestInset::cell(idx_type i)
95 MathArray const & MathNestInset::cell(idx_type i) const
101 void MathNestInset::getCursorPos(CursorSlice const & sl,
102 int & x, int & y) const
104 // FIXME: This is a hack. Ideally, the coord cache should not store
105 // absolute positions, but relative ones. This would mean to call
106 // setXY() not in MathArray::draw(), but in the parent insets' draw()
107 // with the correctly adjusted x,y values. But this means that we'd have
108 // to touch all (math)inset's draw() methods. Right now, we'll store
109 // absolute value, and make them here relative, only to make them
110 // absolute again when actually drawing the cursor. What a mess.
111 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
112 MathArray const & ar = sl.cell();
113 if (!theCoords.getArrays().has(&ar)) {
114 // this can (semi-)legally happen if we just created this cell
115 // and it never has been drawn before. So don't ASSERT.
116 //lyxerr << "no cached data for array " << &ar << endl;
121 Point const pt = theCoords.getArrays().xy(&ar);
122 if (!theCoords.getInsets().has(this)) {
124 //lyxerr << "no cached data for inset " << this << endl;
129 Point const pt2 = theCoords.getInsets().xy(this);
130 //lyxerr << "retrieving position cache for MathArray "
131 // << pt.x_ << ' ' << pt.y_ << std::endl;
132 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
134 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
135 // << " asc: " << ascent() << " des: " << descent()
136 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
137 // move cursor visually into empty cells ("blue rectangles");
142 void MathNestInset::metrics(MetricsInfo const & mi) const
145 for (idx_type i = 0, n = nargs(); i != n; ++i)
150 bool MathNestInset::idxNext(LCursor & cur) const
152 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
153 if (cur.idx() == cur.lastidx())
161 bool MathNestInset::idxRight(LCursor & cur) const
167 bool MathNestInset::idxPrev(LCursor & cur) const
169 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
173 cur.pos() = cur.lastpos();
178 bool MathNestInset::idxLeft(LCursor & cur) const
184 bool MathNestInset::idxFirst(LCursor & cur) const
186 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
195 bool MathNestInset::idxLast(LCursor & cur) const
197 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
200 cur.idx() = cur.lastidx();
201 cur.pos() = cur.lastpos();
206 void MathNestInset::dump() const
208 WriteStream os(lyxerr);
209 os << "---------------------------------------------\n";
212 for (idx_type i = 0, n = nargs(); i != n; ++i)
213 os << cell(i) << "\n";
214 os << "---------------------------------------------\n";
218 void MathNestInset::draw(PainterInfo & pi, int x, int y) const
222 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
225 setPosCache(pi, x, y);
229 void MathNestInset::drawSelection(PainterInfo & pi, int x, int y) const
231 // FIXME: hack to get position cache warm
234 // this should use the x/y values given, not the cached values
235 LCursor & cur = pi.base.bv->cursor();
236 if (!cur.selection())
238 if (!ptr_cmp(&cur.inset(), this))
241 CursorSlice s1 = cur.selBegin();
242 CursorSlice s2 = cur.selEnd();
243 //lyxerr << "MathNestInset::drawing selection: "
244 // << " s1: " << s1 << " s2: " << s2 << endl;
245 if (s1.idx() == s2.idx()) {
246 MathArray const & c = cell(s1.idx());
247 int x1 = c.xo() + c.pos2x(s1.pos());
248 int y1 = c.yo() - c.ascent();
249 int x2 = c.xo() + c.pos2x(s2.pos());
250 int y2 = c.yo() + c.descent();
251 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
252 //lyxerr << "MathNestInset::drawing selection 3: "
253 // << " x1: " << x1 << " x2: " << x2
254 // << " y1: " << y1 << " y2: " << y2 << endl;
256 for (idx_type i = 0; i < nargs(); ++i) {
257 if (idxBetween(i, s1.idx(), s2.idx())) {
258 MathArray const & c = cell(i);
260 int y1 = c.yo() - c.ascent();
261 int x2 = c.xo() + c.width();
262 int y2 = c.yo() + c.descent();
263 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
270 void MathNestInset::validate(LaTeXFeatures & features) const
272 for (idx_type i = 0; i < nargs(); ++i)
273 cell(i).validate(features);
277 void MathNestInset::replace(ReplaceData & rep)
279 for (idx_type i = 0; i < nargs(); ++i)
280 cell(i).replace(rep);
284 bool MathNestInset::contains(MathArray const & ar) const
286 for (idx_type i = 0; i < nargs(); ++i)
287 if (cell(i).contains(ar))
293 bool MathNestInset::lock() const
299 void MathNestInset::lock(bool l)
305 bool MathNestInset::isActive() const
311 MathArray MathNestInset::glue() const
314 for (size_t i = 0; i < nargs(); ++i)
320 void MathNestInset::write(WriteStream & os) const
322 os << '\\' << name().c_str();
323 for (size_t i = 0; i < nargs(); ++i)
324 os << '{' << cell(i) << '}';
326 os.pendingSpace(true);
327 if (lock_ && !os.latex()) {
329 os.pendingSpace(true);
334 void MathNestInset::normalize(NormalStream & os) const
336 os << '[' << name().c_str();
337 for (size_t i = 0; i < nargs(); ++i)
338 os << ' ' << cell(i);
343 int MathNestInset::latex(Buffer const &, std::ostream & os,
344 OutputParams const & runparams) const
346 WriteStream wi(os, runparams.moving_arg, true);
352 void MathNestInset::notifyCursorLeaves(LCursor & /*cur*/)
358 MathArray & ar = cur.cell();
359 // remove base-only "scripts"
360 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
361 MathScriptInset * p = operator[](i).nucleus()->asScriptInset();
362 if (p && p->nargs() == 1) {
363 MathArray ar = p->nuc();
366 cur.adjust(i, ar.size() - 1);
370 // glue adjacent font insets of the same kind
371 for (pos_type i = 0; i + 1 < size(); ++i) {
372 MathFontInset * p = operator[](i).nucleus()->asFontInset();
373 MathFontInset const * q = operator[](i + 1)->asFontInset();
374 if (p && q && p->name() == q->name()) {
375 p->cell(0).append(q->cell(0));
384 void MathNestInset::handleFont
385 (LCursor & cur, string const & arg, string const & font)
387 // this whole function is a hack and won't work for incremental font
389 recordUndo(cur, Undo::ATOMIC);
391 if (cur.inset().asMathInset()->name() == font)
392 cur.handleFont(font);
394 cur.handleNest(createMathInset(font));
400 void MathNestInset::handleFont2(LCursor & cur, string const & arg)
402 recordUndo(cur, Undo::ATOMIC);
405 bv_funcs::string2font(arg, font, b);
406 if (font.color() != LColor::inherit) {
407 MathAtom at = createMathInset("color");
408 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
409 cur.handleNest(at, 1);
414 void MathNestInset::doDispatch(LCursor & cur, FuncRequest & cmd)
416 //lyxerr << "MathNestInset: request: " << cmd << std::endl;
417 //CursorSlice sl = cur.current();
419 switch (cmd.action) {
423 cur.message(_("Paste"));
424 replaceSelection(cur);
426 istringstream is(cmd.argument);
428 pasteSelection(cur, n);
429 cur.clearSelection(); // bug 393
430 cur.bv().switchKeyMap();
436 cutSelection(cur, true, true);
437 cur.message(_("Cut"));
442 cur.message(_("Copy"));
445 case LFUN_MOUSE_PRESS:
446 lfunMousePress(cur, cmd);
449 case LFUN_MOUSE_MOTION:
450 lfunMouseMotion(cur, cmd);
453 case LFUN_MOUSE_RELEASE:
454 lfunMouseRelease(cur, cmd);
457 case LFUN_FINISHED_LEFT:
458 cur.bv().cursor() = cur;
461 case LFUN_FINISHED_RIGHT:
463 cur.bv().cursor() = cur;
466 case LFUN_FINISHED_UP:
467 cur.bv().cursor() = cur;
470 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);
492 cur.selHandle(cmd.action == LFUN_LEFTSEL);
493 cur.autocorrect() = false;
495 cur.macroModeClose();
496 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
498 cur.push(*cur.nextAtom().nucleus());
499 cur.inset().idxLast(cur);
500 } else if (cur.posLeft() || idxLeft(cur)
501 || cur.popLeft() || cur.selection())
504 cmd = FuncRequest(LFUN_FINISHED_LEFT);
509 cur.selHandle(cmd.action == LFUN_UPSEL);
511 cmd = FuncRequest(LFUN_FINISHED_UP);
512 // fixes bug 1598. Please check!
518 cur.selHandle(cmd.action == LFUN_DOWNSEL);
520 cmd = FuncRequest(LFUN_FINISHED_DOWN);
521 // fixes bug 1598. Please check!
525 case LFUN_MOUSE_DOUBLE:
526 case LFUN_MOUSE_TRIPLE:
531 cur.selection() = true;
532 cur.pos() = cur.lastpos();
533 cur.idx() = cur.lastidx();
536 case LFUN_UP_PARAGRAPHSEL:
537 case LFUN_UP_PARAGRAPH:
538 case LFUN_DOWN_PARAGRAPHSEL:
539 case LFUN_DOWN_PARAGRAPH:
544 case LFUN_WORDLEFTSEL:
546 cur.selHandle(cmd.action == LFUN_WORDLEFTSEL || cmd.action == LFUN_HOMESEL);
547 cur.macroModeClose();
548 if (cur.pos() != 0) {
550 } else if (cur.col() != 0) {
551 cur.idx() -= cur.col();
553 } else if (cur.idx() != 0) {
557 cmd = FuncRequest(LFUN_FINISHED_LEFT);
561 case LFUN_WORDRIGHTSEL:
565 cur.selHandle(cmd.action == LFUN_WORDRIGHTSEL || cmd.action == LFUN_ENDSEL);
566 cur.macroModeClose();
568 if (cur.pos() != cur.lastpos()) {
569 cur.pos() = cur.lastpos();
570 } else if (cur.col() != cur.lastcol()) {
571 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
572 cur.pos() = cur.lastpos();
573 } else if (cur.idx() != cur.lastidx()) {
574 cur.idx() = cur.lastidx();
575 cur.pos() = cur.lastpos();
577 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
583 cmd = FuncRequest(LFUN_FINISHED_LEFT);
588 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
591 case LFUN_CELL_FORWARD:
592 cur.inset().idxNext(cur);
595 case LFUN_CELL_BACKWARD:
596 cur.inset().idxPrev(cur);
599 case LFUN_DELETE_WORD_BACKWARD:
601 recordUndo(cur, Undo::ATOMIC);
605 case LFUN_DELETE_WORD_FORWARD:
609 cmd = FuncRequest(LFUN_FINISHED_LEFT);
614 cur.clearSelection();
616 cmd = FuncRequest(LFUN_FINISHED_LEFT);
619 case LFUN_INSET_TOGGLE:
622 if (cur.pos() != cur.lastpos()) {
623 // toggle previous inset ...
624 cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock());
625 } else if (cur.popLeft() && cur.pos() != cur.lastpos()) {
626 // ... or enclosing inset if we are in the last inset position
627 cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock());
632 case LFUN_SELFINSERT:
634 if (cmd.argument.size() != 1) {
635 cur.insert(cmd.argument);
638 if (!interpret(cur, cmd.argument[0]))
639 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
643 // sprintf(dispatch_buffer, "%d %d",);
647 lyxerr << "LFUN_SETXY broken!" << endl;
650 istringstream is(cmd.argument);
652 cur.setScreenPos(x, y);
656 // Special casing for superscript in case of LyX handling
658 case LFUN_CIRCUMFLEX:
659 if (cmd.argument.empty()) {
660 // do superscript if LyX handles
662 recordUndo(cur, Undo::ATOMIC);
680 case LFUN_HUNG_UMLAUT:
684 case LFUN_FREEFONT_APPLY:
685 case LFUN_FREEFONT_UPDATE:
686 handleFont2(cur, cmd.argument);
690 handleFont(cur, cmd.argument, "mathbf");
693 handleFont(cur, cmd.argument, "mathsf");
696 handleFont(cur, cmd.argument, "mathcal");
699 handleFont(cur, cmd.argument, "mathrm");
702 handleFont(cur, cmd.argument, "texttt");
705 handleFont(cur, cmd.argument, "mathfrak");
708 handleFont(cur, cmd.argument, "mathit");
711 handleFont(cur, cmd.argument, "mathbb");
713 //case LFUN_FREEFONT_APPLY:
714 handleFont(cur, cmd.argument, "textrm");
717 handleFont(cur, cmd.argument, "textnormal");
722 // ignore math-mode on when already in math mode
723 if (currentMode() == InsetBase::MATH_MODE && cmd.argument == "on")
725 cur.macroModeClose();
727 cur.plainInsert(MathAtom(new MathMBoxInset(cur.bv())));
729 cur.pushLeft(*cur.nextInset());
731 if (currentMode() == InsetBase::TEXT_MODE) {
732 cur.niceInsert(MathAtom(new MathHullInset("simple")));
733 cur.message(_("create new math text environment ($...$)"));
735 handleFont(cur, cmd.argument, "textrm");
736 cur.message(_("entered math text mode (textrm)"));
748 case LFUN_INSERT_MATRIX: {
749 recordUndo(cur, Undo::ATOMIC);
754 istringstream is(cmd.argument);
755 is >> m >> n >> v_align >> h_align;
762 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
766 case LFUN_MATH_DELIM: {
767 lyxerr << "MathNestInset::LFUN_MATH_DELIM" << endl;
769 string rs = lyx::support::split(cmd.argument, ls, ' ');
770 // Reasonable default values
775 recordUndo(cur, Undo::ATOMIC);
776 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
780 case LFUN_SPACE_INSERT:
781 case LFUN_MATH_SPACE:
782 recordUndo(cur, Undo::ATOMIC);
783 cur.insert(MathAtom(new MathSpaceInset(",")));
787 // interpret this as if a backslash was typed
788 recordUndo(cur, Undo::ATOMIC);
789 interpret(cur, '\\');
793 // interpret this as if a _ was typed
794 recordUndo(cur, Undo::ATOMIC);
798 case LFUN_SUPERSCRIPT:
799 // interpret this as if a ^ was typed
800 recordUndo(cur, Undo::ATOMIC);
804 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
805 // handling such that "self-insert" works on "arbitrary stuff" too, and
806 // math-insert only handles special math things like "matrix".
807 case LFUN_INSERT_MATH:
808 recordUndo(cur, Undo::ATOMIC);
809 cur.niceInsert(cmd.argument);
812 case LFUN_DIALOG_SHOW_NEW_INSET: {
813 string const & name = cmd.argument;
818 data = tmp.createDialogStr(name);
821 cur.bv().owner()->getDialogs().show(name, data, 0);
825 case LFUN_INSET_APPLY: {
826 string const name = cmd.getArg(0);
827 InsetBase * base = cur.bv().owner()->getDialogs().getOpenInset(name);
830 FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
831 base->dispatch(cur, fr);
835 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
844 MathDimInset::doDispatch(cur, cmd);
850 bool MathNestInset::getStatus(LCursor & /*cur*/, FuncRequest const & cmd,
851 FuncStatus & flag) const
853 // the font related toggles
854 //string tc = "mathnormal";
856 switch (cmd.action) {
858 case LFUN_TABULAR_FEATURE:
859 // FIXME: check temporarily disabled
861 char align = mathcursor::valign();
866 if (cmd.argument.empty()) {
870 if (!contains("tcb", cmd.argument[0])) {
874 flag.setOnOff(cmd.argument[0] == align);
877 flag.setOnOff(tc == "mathbf");
880 flag.setOnOff(tc == "mathsf");
883 flag.setOnOff(tc == "mathcal");
886 flag.setOnOff(tc == "mathrm");
889 flag.setOnOff(tc == "mathtt");
892 flag.setOnOff(tc == "mathbb");
895 flag.setOnOff(tc == "mathnormal");
898 case LFUN_MATH_MUTATE:
899 //flag.setOnOff(mathcursor::formula()->hullType() == cmd.argument);
900 flag.setOnOff(false);
903 // we just need to be in math mode to enable that
905 case LFUN_MATH_SPACE:
906 case LFUN_MATH_LIMITS:
907 case LFUN_MATH_NONUMBER:
908 case LFUN_MATH_NUMBER:
909 case LFUN_MATH_EXTERN:
921 void MathNestInset::edit(LCursor & cur, bool left)
924 cur.idx() = left ? 0 : cur.lastidx();
925 cur.pos() = left ? 0 : cur.lastpos();
927 lyxerr << "MathNestInset::edit, cur:\n" << cur << endl;
931 InsetBase * MathNestInset::editXY(LCursor & cur, int x, int y) const
934 int dist_min = 1000000;
935 for (idx_type i = 0; i < nargs(); ++i) {
936 int d = cell(i).dist(x, y);
942 MathArray const & ar = cell(idx_min);
943 cur.push(const_cast<MathNestInset&>(*this));
945 cur.pos() = ar.x2pos(x - ar.xo());
946 lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
949 for (pos_type i = 0, n = ar.size(); i < n; ++i)
950 if (ar[i]->covers(x, y))
951 return ar[i].nucleus()->editXY(cur, x, y);
953 return const_cast<MathNestInset*>(this);
957 void MathNestInset::lfunMousePress(LCursor & cur, FuncRequest & cmd)
959 lyxerr << "lfunMousePress: buttons: " << cmd.button() << endl;
960 if (cmd.button() == mouse_button::button1) {
963 lyxerr << "lfunMousePress: setting cursor to: " << cur << endl;
965 cur.bv().cursor() = cur;
968 if (cmd.button() == mouse_button::button2) {
969 cur.dispatch(FuncRequest(LFUN_PASTESELECTION));
974 void MathNestInset::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
976 // only select with button 1
977 if (cmd.button() == mouse_button::button1) {
978 LCursor & bvcur = cur.bv().cursor();
979 if (abs(cmd.x - first_x) + abs(cmd.y - first_y) > 4
980 && bvcur.anchor_.hasPart(cur)) {
984 bvcur.setCursor(cur);
985 bvcur.selection() = true;
992 void MathNestInset::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
994 lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
996 if (cmd.button() == mouse_button::button1) {
997 //cur.bv().stuffClipboard(cur.grabSelection());
1001 if (cmd.button() == mouse_button::button2) {
1003 asArray(cur.bv().getClipboard(), ar);
1004 cur.clearSelection();
1005 cur.setScreenPos(cmd.x, cmd.y);
1011 if (cmd.button() == mouse_button::button3) {
1012 // try to dispatch to enclosed insets first
1013 cur.bv().owner()->getDialogs().show("mathpanel");
1021 bool MathNestInset::interpret(LCursor & cur, char c)
1023 lyxerr << "interpret 2: '" << c << "'" << endl;
1027 if (cur.inMacroMode()) {
1028 string name = cur.macroName();
1030 /// are we currently typing '#1' or '#2' or...?
1031 if (name == "\\#") {
1034 if (n >= 1 && n <= 9)
1035 cur.insert(new MathMacroArgument(n));
1040 cur.activeMacro()->setName(name + c);
1044 // handle 'special char' macros
1049 if (currentMode() == MathInset::TEXT_MODE)
1050 cur.niceInsert(createMathInset("textbackslash"));
1052 cur.niceInsert(createMathInset("backslash"));
1053 } else if (c == '{') {
1055 cur.niceInsert(MathAtom(new MathBraceInset));
1056 } else if (c == '%') {
1058 cur.niceInsert(MathAtom(new MathCommentInset));
1059 } else if (c == '#') {
1060 BOOST_ASSERT(cur.activeMacro());
1061 cur.activeMacro()->setName(name + c);
1064 cur.niceInsert(createMathInset(string(1, c)));
1069 // leave macro mode and try again if necessary
1070 cur.macroModeClose();
1072 cur.niceInsert(MathAtom(new MathBraceInset));
1078 // This is annoying as one has to press <space> far too often.
1082 // leave autocorrect mode if necessary
1083 if (autocorrect() && c == ' ') {
1084 autocorrect() = false;
1089 // just clear selection on pressing the space bar
1090 if (cur.selection() && c == ' ') {
1091 cur.selection() = false;
1098 //lyxerr << "starting with macro" << endl;
1099 cur.insert(MathAtom(new MathUnknownInset("\\", false)));
1104 if (currentMode() == MathInset::TEXT_MODE)
1110 if (currentMode() == MathInset::TEXT_MODE) {
1111 // insert spaces in text mode,
1112 // but suppress direct insertion of two spaces in a row
1113 // the still allows typing '<space>a<space>' and deleting the 'a', but
1114 // it is better than nothing...
1115 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
1119 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1120 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1125 // if are at the very end, leave the formula
1126 return cur.pos() != cur.lastpos();
1139 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' || c == '%') {
1140 cur.niceInsert(createMathInset(string(1, c)));
1145 cur.niceInsert(createMathInset("sim"));
1149 // try auto-correction
1150 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1153 // no special circumstances, so insert the character without any fuss
1155 cur.autocorrect() = true;
1160 bool MathNestInset::script(LCursor & cur, bool up)
1162 // Hack to get \^ and \_ working
1163 lyxerr << "handling script: up: " << up << endl;
1164 if (cur.inMacroMode() && cur.macroName() == "\\") {
1166 cur.niceInsert(createMathInset("mathcircumflex"));
1168 interpret(cur, '_');
1172 cur.macroModeClose();
1173 string safe = grabAndEraseSelection(cur);
1174 if (asScriptInset() && cur.idx() == 0) {
1175 // we are in a nucleus of a script inset, move to _our_ script
1176 MathScriptInset * inset = asScriptInset();
1177 lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1179 cur.idx() = inset->idxOfScript(up);
1181 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1183 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1185 cur.idx() = inset->idxOfScript(up);
1186 cur.pos() = cur.lastpos();
1188 // convert the thing to our left to a scriptinset or create a new
1189 // one if in the very first position of the array
1190 if (cur.pos() == 0) {
1191 lyxerr << "new scriptinset" << endl;
1192 cur.insert(new MathScriptInset(up));
1194 lyxerr << "converting prev atom " << endl;
1195 cur.prevAtom() = MathAtom(new MathScriptInset(cur.prevAtom(), up));
1198 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1203 lyxerr << "pasting 1: safe:\n" << safe << endl;
1206 lyxerr << "pasting 2: safe:\n" << safe << endl;