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_commentinset.h"
19 #include "math_data.h"
20 #include "math_deliminset.h"
21 #include "math_factory.h"
22 #include "math_hullinset.h"
23 #include "math_mathmlstream.h"
24 #include "math_macroarg.h"
25 //#include "math_mboxinset.h"
26 #include "math_parser.h"
27 #include "math_scriptinset.h"
28 #include "math_spaceinset.h"
29 #include "math_symbolinset.h"
30 #include "math_support.h"
31 #include "math_unknowninset.h"
33 #include "BufferView.h"
34 #include "CutAndPaste.h"
35 #include "FuncStatus.h"
37 #include "bufferview_funcs.h"
38 #include "coordcache.h"
41 #include "dispatchresult.h"
42 #include "funcrequest.h"
44 #include "outputparams.h"
47 #include "support/lstrings.h"
49 #include "frontends/Dialogs.h"
50 #include "frontends/LyXView.h"
51 #include "frontends/Painter.h"
55 using lyx::cap::copySelection;
56 using lyx::cap::grabAndEraseSelection;
57 using lyx::cap::cutSelection;
58 using lyx::cap::pasteSelection;
59 using lyx::cap::replaceSelection;
60 using lyx::cap::selClearOrDel;
64 using std::istringstream;
67 MathNestInset::MathNestInset(idx_type nargs)
68 : cells_(nargs), lock_(false)
72 MathInset::idx_type MathNestInset::nargs() const
78 MathArray & MathNestInset::cell(idx_type i)
84 MathArray const & MathNestInset::cell(idx_type i) const
90 void MathNestInset::getCursorPos(CursorSlice const & sl,
91 int & x, int & y) const
93 // FIXME: This is a hack. Ideally, the coord cache should not store
94 // absolute positions, but relative ones. This would mean to call
95 // setXY() not in MathArray::draw(), but in the parent insets' draw()
96 // with the correctly adjusted x,y values. But this means that we'd have
97 // to touch all (math)inset's draw() methods. Right now, we'll store
98 // absolute value, and make them here relative, only to make them
99 // absolute again when actually drawing the cursor. What a mess.
100 BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
101 MathArray const & ar = sl.cell();
102 if (!theCoords.getArrays().has(&ar)) {
103 // this can (semi-)legally happen if we just created this cell
104 // and it never has been drawn before. So don't ASSERT.
105 //lyxerr << "no cached data for array " << &ar << endl;
110 Point const pt = theCoords.getArrays().xy(&ar);
111 if (!theCoords.getInsets().has(this)) {
113 //lyxerr << "no cached data for inset " << this << endl;
118 Point const pt2 = theCoords.getInsets().xy(this);
119 //lyxerr << "retrieving position cache for MathArray "
120 // << pt.x_ << ' ' << pt.y_ << std::endl;
121 x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
123 // lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
124 // << " asc: " << ascent() << " des: " << descent()
125 // << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
126 // move cursor visually into empty cells ("blue rectangles");
131 void MathNestInset::metrics(MetricsInfo const & mi) const
134 for (idx_type i = 0, n = nargs(); i != n; ++i)
139 bool MathNestInset::idxNext(LCursor & cur) const
141 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
142 if (cur.idx() == cur.lastidx())
150 bool MathNestInset::idxRight(LCursor & cur) const
156 bool MathNestInset::idxPrev(LCursor & cur) const
158 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
162 cur.pos() = cur.lastpos();
167 bool MathNestInset::idxLeft(LCursor & cur) const
173 bool MathNestInset::idxFirst(LCursor & cur) const
175 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
184 bool MathNestInset::idxLast(LCursor & cur) const
186 BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
189 cur.idx() = cur.lastidx();
190 cur.pos() = cur.lastpos();
195 void MathNestInset::dump() const
197 WriteStream os(lyxerr);
198 os << "---------------------------------------------\n";
201 for (idx_type i = 0, n = nargs(); i != n; ++i)
202 os << cell(i) << "\n";
203 os << "---------------------------------------------\n";
207 void MathNestInset::draw(PainterInfo & pi, int x, int y) const
211 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
214 setPosCache(pi, x, y);
218 void MathNestInset::drawSelection(PainterInfo & pi, int x, int y) const
220 // FIXME: hack to get position cache warm
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 CursorSlice s1 = cur.selBegin();
231 CursorSlice s2 = cur.selEnd();
232 //lyxerr << "MathNestInset::drawing selection: "
233 // << " s1: " << s1 << " s2: " << s2 << endl;
234 if (s1.idx() == s2.idx()) {
235 MathArray const & c = cell(s1.idx());
236 int x1 = c.xo() + c.pos2x(s1.pos());
237 int y1 = c.yo() - c.ascent();
238 int x2 = c.xo() + c.pos2x(s2.pos());
239 int y2 = c.yo() + c.descent();
240 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
241 //lyxerr << "MathNestInset::drawing selection 3: "
242 // << " x1: " << x1 << " x2: " << x2
243 // << " y1: " << y1 << " y2: " << y2 << endl;
245 for (idx_type i = 0; i < nargs(); ++i) {
246 if (idxBetween(i, s1.idx(), s2.idx())) {
247 MathArray const & c = cell(i);
249 int y1 = c.yo() - c.ascent();
250 int x2 = c.xo() + c.width();
251 int y2 = c.yo() + c.descent();
252 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
259 void MathNestInset::validate(LaTeXFeatures & features) const
261 for (idx_type i = 0; i < nargs(); ++i)
262 cell(i).validate(features);
266 void MathNestInset::replace(ReplaceData & rep)
268 for (idx_type i = 0; i < nargs(); ++i)
269 cell(i).replace(rep);
273 bool MathNestInset::contains(MathArray const & ar) const
275 for (idx_type i = 0; i < nargs(); ++i)
276 if (cell(i).contains(ar))
282 bool MathNestInset::lock() const
288 void MathNestInset::lock(bool l)
294 bool MathNestInset::isActive() const
300 MathArray MathNestInset::glue() const
303 for (size_t i = 0; i < nargs(); ++i)
309 void MathNestInset::write(WriteStream & os) const
311 os << '\\' << name().c_str();
312 for (size_t i = 0; i < nargs(); ++i)
313 os << '{' << cell(i) << '}';
315 os.pendingSpace(true);
316 if (lock_ && !os.latex()) {
318 os.pendingSpace(true);
323 void MathNestInset::normalize(NormalStream & os) const
325 os << '[' << name().c_str();
326 for (size_t i = 0; i < nargs(); ++i)
327 os << ' ' << cell(i);
332 int MathNestInset::latex(Buffer const &, std::ostream & os,
333 OutputParams const & runparams) const
335 WriteStream wi(os, runparams.moving_arg, true);
341 void MathNestInset::notifyCursorLeaves(LCursor & /*cur*/)
347 MathArray & ar = cur.cell();
348 // remove base-only "scripts"
349 for (pos_type i = 0; i + 1 < ar.size(); ++i) {
350 MathScriptInset * p = operator[](i).nucleus()->asScriptInset();
351 if (p && p->nargs() == 1) {
352 MathArray ar = p->nuc();
355 cur.adjust(i, ar.size() - 1);
359 // glue adjacent font insets of the same kind
360 for (pos_type i = 0; i + 1 < size(); ++i) {
361 MathFontInset * p = operator[](i).nucleus()->asFontInset();
362 MathFontInset const * q = operator[](i + 1)->asFontInset();
363 if (p && q && p->name() == q->name()) {
364 p->cell(0).append(q->cell(0));
373 void MathNestInset::handleFont
374 (LCursor & cur, string const & arg, string const & font)
376 // this whole function is a hack and won't work for incremental font
378 recordUndo(cur, Undo::ATOMIC);
380 if (cur.inset().asMathInset()->name() == font)
381 cur.handleFont(font);
383 cur.handleNest(createMathInset(font));
389 void MathNestInset::handleFont2(LCursor & cur, string const & arg)
391 recordUndo(cur, Undo::ATOMIC);
394 bv_funcs::string2font(arg, font, b);
395 if (font.color() != LColor::inherit) {
396 MathAtom at = createMathInset("color");
397 asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
398 cur.handleNest(at, 1);
403 void MathNestInset::doDispatch(LCursor & cur, FuncRequest & cmd)
405 //lyxerr << "MathNestInset: request: " << cmd << std::endl;
406 //CursorSlice sl = cur.current();
408 switch (cmd.action) {
412 cur.message(_("Paste"));
413 replaceSelection(cur);
415 istringstream is(cmd.argument);
417 pasteSelection(cur, n);
418 cur.clearSelection(); // bug 393
419 cur.bv().switchKeyMap();
425 cutSelection(cur, true, true);
426 cur.message(_("Cut"));
431 cur.message(_("Copy"));
434 case LFUN_MOUSE_PRESS:
435 lfunMousePress(cur, cmd);
438 case LFUN_MOUSE_MOTION:
439 lfunMouseMotion(cur, cmd);
442 case LFUN_MOUSE_RELEASE:
443 lfunMouseRelease(cur, cmd);
446 case LFUN_FINISHED_LEFT:
447 cur.bv().cursor() = cur;
450 case LFUN_FINISHED_RIGHT:
452 cur.bv().cursor() = cur;
455 case LFUN_FINISHED_UP:
456 cur.bv().cursor() = cur;
459 case LFUN_FINISHED_DOWN:
460 cur.bv().cursor() = cur;
465 cur.selHandle(cmd.action == LFUN_RIGHTSEL);
466 cur.autocorrect() = false;
468 cur.macroModeClose();
469 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
470 cur.pushLeft(*cur.nextAtom().nucleus());
471 cur.inset().idxFirst(cur);
472 } else if (cur.posRight() || idxRight(cur)
473 || cur.popRight() || cur.selection())
476 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
481 cur.selHandle(cmd.action == LFUN_LEFTSEL);
482 cur.autocorrect() = false;
484 cur.macroModeClose();
485 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
487 cur.push(*cur.nextAtom().nucleus());
488 cur.inset().idxLast(cur);
489 } else if (cur.posLeft() || idxLeft(cur)
490 || cur.popLeft() || cur.selection())
493 cmd = FuncRequest(LFUN_FINISHED_LEFT);
498 cur.selHandle(cmd.action == LFUN_UPSEL);
500 cmd = FuncRequest(LFUN_FINISHED_UP);
501 // fixes bug 1598. Please check!
507 cur.selHandle(cmd.action == LFUN_DOWNSEL);
509 cmd = FuncRequest(LFUN_FINISHED_DOWN);
510 // fixes bug 1598. Please check!
514 case LFUN_MOUSE_DOUBLE:
515 case LFUN_MOUSE_TRIPLE:
520 cur.selection() = true;
521 cur.pos() = cur.lastpos();
522 cur.idx() = cur.lastidx();
525 case LFUN_UP_PARAGRAPHSEL:
526 case LFUN_UP_PARAGRAPH:
527 case LFUN_DOWN_PARAGRAPHSEL:
528 case LFUN_DOWN_PARAGRAPH:
533 case LFUN_WORDLEFTSEL:
535 cur.selHandle(cmd.action == LFUN_WORDLEFTSEL || cmd.action == LFUN_HOMESEL);
536 cur.macroModeClose();
537 if (cur.pos() != 0) {
539 } else if (cur.col() != 0) {
540 cur.idx() -= cur.col();
542 } else if (cur.idx() != 0) {
546 cmd = FuncRequest(LFUN_FINISHED_LEFT);
550 case LFUN_WORDRIGHTSEL:
554 cur.selHandle(cmd.action == LFUN_WORDRIGHTSEL || cmd.action == LFUN_ENDSEL);
555 cur.macroModeClose();
557 if (cur.pos() != cur.lastpos()) {
558 cur.pos() = cur.lastpos();
559 } else if (cur.col() != cur.lastcol()) {
560 cur.idx() = cur.idx() - cur.col() + cur.lastcol();
561 cur.pos() = cur.lastpos();
562 } else if (cur.idx() != cur.lastidx()) {
563 cur.idx() = cur.lastidx();
564 cur.pos() = cur.lastpos();
566 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
572 cmd = FuncRequest(LFUN_FINISHED_LEFT);
577 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
580 case LFUN_CELL_FORWARD:
581 cur.inset().idxNext(cur);
584 case LFUN_CELL_BACKWARD:
585 cur.inset().idxPrev(cur);
588 case LFUN_DELETE_WORD_BACKWARD:
590 recordUndo(cur, Undo::ATOMIC);
594 case LFUN_DELETE_WORD_FORWARD:
598 cmd = FuncRequest(LFUN_FINISHED_LEFT);
603 cur.clearSelection();
605 cmd = FuncRequest(LFUN_FINISHED_LEFT);
608 case LFUN_INSET_TOGGLE:
611 if (cur.pos() != cur.lastpos()) {
612 // toggle previous inset ...
613 cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock());
614 } else if (cur.popLeft() && cur.pos() != cur.lastpos()) {
615 // ... or enclosing inset if we are in the last inset position
616 cur.nextAtom().nucleus()->lock(!cur.nextAtom()->lock());
621 case LFUN_SELFINSERT:
623 if (cmd.argument.size() != 1) {
624 cur.insert(cmd.argument);
627 if (!interpret(cur, cmd.argument[0]))
628 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
632 // sprintf(dispatch_buffer, "%d %d",);
636 lyxerr << "LFUN_SETXY broken!" << endl;
639 istringstream is(cmd.argument);
641 cur.setScreenPos(x, y);
645 // Special casing for superscript in case of LyX handling
647 case LFUN_CIRCUMFLEX:
648 if (cmd.argument.empty()) {
649 // do superscript if LyX handles
651 recordUndo(cur, Undo::ATOMIC);
669 case LFUN_HUNG_UMLAUT:
673 case LFUN_FREEFONT_APPLY:
674 case LFUN_FREEFONT_UPDATE:
675 handleFont2(cur, cmd.argument);
679 handleFont(cur, cmd.argument, "mathbf");
682 handleFont(cur, cmd.argument, "mathsf");
685 handleFont(cur, cmd.argument, "mathcal");
688 handleFont(cur, cmd.argument, "mathrm");
691 handleFont(cur, cmd.argument, "texttt");
694 handleFont(cur, cmd.argument, "mathfrak");
697 handleFont(cur, cmd.argument, "mathit");
700 handleFont(cur, cmd.argument, "mathbb");
702 //case LFUN_FREEFONT_APPLY:
703 handleFont(cur, cmd.argument, "textrm");
706 handleFont(cur, cmd.argument, "textnormal");
711 // ignore math-mode on when already in math mode
712 if (currentMode() == InsetBase::MATH_MODE && cmd.argument == "on")
714 cur.macroModeClose();
716 //cur.plainInsert(MathAtom(new MathMBoxInset(cur.bv())));
717 cur.plainInsert(MathAtom(new MathBoxInset("mbox")));
719 cur.pushLeft(*cur.nextInset());
721 if (currentMode() == InsetBase::TEXT_MODE) {
722 cur.niceInsert(MathAtom(new MathHullInset("simple")));
723 cur.message(_("create new math text environment ($...$)"));
725 handleFont(cur, cmd.argument, "textrm");
726 cur.message(_("entered math text mode (textrm)"));
738 case LFUN_INSERT_MATRIX: {
739 recordUndo(cur, Undo::ATOMIC);
744 istringstream is(cmd.argument);
745 is >> m >> n >> v_align >> h_align;
752 MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
756 case LFUN_MATH_DELIM: {
757 lyxerr << "MathNestInset::LFUN_MATH_DELIM" << endl;
759 string rs = lyx::support::split(cmd.argument, ls, ' ');
760 // Reasonable default values
765 recordUndo(cur, Undo::ATOMIC);
766 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
770 case LFUN_SPACE_INSERT:
771 case LFUN_MATH_SPACE:
772 recordUndo(cur, Undo::ATOMIC);
773 cur.insert(MathAtom(new MathSpaceInset(",")));
777 // interpret this as if a backslash was typed
778 recordUndo(cur, Undo::ATOMIC);
779 interpret(cur, '\\');
783 // interpret this as if a _ was typed
784 recordUndo(cur, Undo::ATOMIC);
788 case LFUN_SUPERSCRIPT:
789 // interpret this as if a ^ was typed
790 recordUndo(cur, Undo::ATOMIC);
794 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
795 // handling such that "self-insert" works on "arbitrary stuff" too, and
796 // math-insert only handles special math things like "matrix".
797 case LFUN_INSERT_MATH:
798 recordUndo(cur, Undo::ATOMIC);
799 cur.niceInsert(cmd.argument);
802 case LFUN_DIALOG_SHOW_NEW_INSET: {
803 string const & name = cmd.argument;
808 data = tmp.createDialogStr(name);
811 cur.bv().owner()->getDialogs().show(name, data, 0);
815 case LFUN_INSET_APPLY: {
816 string const name = cmd.getArg(0);
817 InsetBase * base = cur.bv().owner()->getDialogs().getOpenInset(name);
820 FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
821 base->dispatch(cur, fr);
825 if (createMathInset_fromDialogStr(cmd.argument, ar)) {
834 MathDimInset::doDispatch(cur, cmd);
840 bool MathNestInset::getStatus(LCursor & /*cur*/, FuncRequest const & cmd,
841 FuncStatus & flag) const
843 // the font related toggles
844 //string tc = "mathnormal";
846 switch (cmd.action) {
848 case LFUN_TABULAR_FEATURE:
849 // FIXME: check temporarily disabled
851 char align = mathcursor::valign();
856 if (cmd.argument.empty()) {
860 if (!contains("tcb", cmd.argument[0])) {
864 flag.setOnOff(cmd.argument[0] == align);
867 flag.setOnOff(tc == "mathbf");
870 flag.setOnOff(tc == "mathsf");
873 flag.setOnOff(tc == "mathcal");
876 flag.setOnOff(tc == "mathrm");
879 flag.setOnOff(tc == "mathtt");
882 flag.setOnOff(tc == "mathbb");
885 flag.setOnOff(tc == "mathnormal");
888 case LFUN_MATH_MUTATE:
889 //flag.setOnOff(mathcursor::formula()->hullType() == cmd.argument);
890 flag.setOnOff(false);
893 // we just need to be in math mode to enable that
895 case LFUN_MATH_SPACE:
896 case LFUN_MATH_LIMITS:
897 case LFUN_MATH_NONUMBER:
898 case LFUN_MATH_NUMBER:
899 case LFUN_MATH_EXTERN:
911 void MathNestInset::edit(LCursor & cur, bool left)
914 cur.idx() = left ? 0 : cur.lastidx();
915 cur.pos() = left ? 0 : cur.lastpos();
917 lyxerr << "MathNestInset::edit, cur:\n" << cur << endl;
921 InsetBase * MathNestInset::editXY(LCursor & cur, int x, int y) const
924 int dist_min = 1000000;
925 for (idx_type i = 0; i < nargs(); ++i) {
926 int d = cell(i).dist(x, y);
932 MathArray const & ar = cell(idx_min);
933 cur.push(const_cast<MathNestInset&>(*this));
935 cur.pos() = ar.x2pos(x - ar.xo());
936 lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
939 for (pos_type i = 0, n = ar.size(); i < n; ++i)
940 if (ar[i]->covers(x, y))
941 return ar[i].nucleus()->editXY(cur, x, y);
943 return const_cast<MathNestInset*>(this);
947 void MathNestInset::lfunMousePress(LCursor & cur, FuncRequest & cmd)
949 //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
950 if (cmd.button() == mouse_button::button1) {
951 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
953 cur.bv().cursor() = cur;
956 if (cmd.button() == mouse_button::button2) {
957 cur.dispatch(FuncRequest(LFUN_PASTESELECTION));
962 void MathNestInset::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
964 // only select with button 1
965 if (cmd.button() == mouse_button::button1) {
966 LCursor & bvcur = cur.bv().cursor();
967 if (bvcur.anchor_.hasPart(cur)) {
968 //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
969 bvcur.setCursor(cur);
970 bvcur.selection() = true;
971 //lyxerr << "MOTION " << bvcur << endl;
980 void MathNestInset::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
982 //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
984 if (cmd.button() == mouse_button::button1) {
985 //cur.bv().stuffClipboard(cur.grabSelection());
989 if (cmd.button() == mouse_button::button2) {
991 asArray(cur.bv().getClipboard(), ar);
992 cur.clearSelection();
993 cur.setScreenPos(cmd.x, cmd.y);
999 if (cmd.button() == mouse_button::button3) {
1000 // try to dispatch to enclosed insets first
1001 cur.bv().owner()->getDialogs().show("mathpanel");
1009 bool MathNestInset::interpret(LCursor & cur, char c)
1011 lyxerr << "interpret 2: '" << c << "'" << endl;
1015 if (cur.inMacroMode()) {
1016 string name = cur.macroName();
1018 /// are we currently typing '#1' or '#2' or...?
1019 if (name == "\\#") {
1022 if (n >= 1 && n <= 9)
1023 cur.insert(new MathMacroArgument(n));
1028 cur.activeMacro()->setName(name + c);
1032 // handle 'special char' macros
1037 if (currentMode() == MathInset::TEXT_MODE)
1038 cur.niceInsert(createMathInset("textbackslash"));
1040 cur.niceInsert(createMathInset("backslash"));
1041 } else if (c == '{') {
1043 cur.niceInsert(MathAtom(new MathBraceInset));
1044 } else if (c == '%') {
1046 cur.niceInsert(MathAtom(new MathCommentInset));
1047 } else if (c == '#') {
1048 BOOST_ASSERT(cur.activeMacro());
1049 cur.activeMacro()->setName(name + c);
1052 cur.niceInsert(createMathInset(string(1, c)));
1057 // leave macro mode and try again if necessary
1058 cur.macroModeClose();
1060 cur.niceInsert(MathAtom(new MathBraceInset));
1066 // This is annoying as one has to press <space> far too often.
1070 // leave autocorrect mode if necessary
1071 if (autocorrect() && c == ' ') {
1072 autocorrect() = false;
1077 // just clear selection on pressing the space bar
1078 if (cur.selection() && c == ' ') {
1079 cur.selection() = false;
1086 //lyxerr << "starting with macro" << endl;
1087 cur.insert(MathAtom(new MathUnknownInset("\\", false)));
1092 if (currentMode() == MathInset::TEXT_MODE)
1098 if (currentMode() == MathInset::TEXT_MODE) {
1099 // insert spaces in text mode,
1100 // but suppress direct insertion of two spaces in a row
1101 // the still allows typing '<space>a<space>' and deleting the 'a', but
1102 // it is better than nothing...
1103 if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
1107 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1108 cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1113 // if are at the very end, leave the formula
1114 return cur.pos() != cur.lastpos();
1127 if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' || c == '%') {
1128 cur.niceInsert(createMathInset(string(1, c)));
1133 cur.niceInsert(createMathInset("sim"));
1137 // try auto-correction
1138 //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1141 // no special circumstances, so insert the character without any fuss
1143 cur.autocorrect() = true;
1148 bool MathNestInset::script(LCursor & cur, bool up)
1150 // Hack to get \^ and \_ working
1151 lyxerr << "handling script: up: " << up << endl;
1152 if (cur.inMacroMode() && cur.macroName() == "\\") {
1154 cur.niceInsert(createMathInset("mathcircumflex"));
1156 interpret(cur, '_');
1160 cur.macroModeClose();
1161 string safe = grabAndEraseSelection(cur);
1162 if (asScriptInset() && cur.idx() == 0) {
1163 // we are in a nucleus of a script inset, move to _our_ script
1164 MathScriptInset * inset = asScriptInset();
1165 lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1167 cur.idx() = inset->idxOfScript(up);
1169 } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1171 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1173 cur.idx() = inset->idxOfScript(up);
1174 cur.pos() = cur.lastpos();
1176 // convert the thing to our left to a scriptinset or create a new
1177 // one if in the very first position of the array
1178 if (cur.pos() == 0) {
1179 lyxerr << "new scriptinset" << endl;
1180 cur.insert(new MathScriptInset(up));
1182 lyxerr << "converting prev atom " << endl;
1183 cur.prevAtom() = MathAtom(new MathScriptInset(cur.prevAtom(), up));
1186 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1191 lyxerr << "pasting 1: safe:\n" << safe << endl;
1194 lyxerr << "pasting 2: safe:\n" << safe << endl;