]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathNest.C
cleanup after svn hang-up, #undef CursorShape. Should be compilable ganin now.
[lyx.git] / src / mathed / InsetMathNest.C
1 /**
2  * \file InsetMathNest.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetMathNest.h"
14
15 #include "InsetMathArray.h"
16 #include "InsetMathBig.h"
17 #include "InsetMathBox.h"
18 #include "InsetMathBrace.h"
19 #include "InsetMathColor.h"
20 #include "InsetMathComment.h"
21 #include "MathData.h"
22 #include "InsetMathDelim.h"
23 #include "MathFactory.h"
24 #include "InsetMathHull.h"
25 #include "MathMLStream.h"
26 #include "MathMacroArgument.h"
27 //#include "InsetMathMBox.h"
28 #include "MathParser.h"
29 #include "InsetMathScript.h"
30 #include "InsetMathSpace.h"
31 #include "InsetMathSymbol.h"
32 #include "MathSupport.h"
33 #include "InsetMathUnknown.h"
34 #include "InsetMathRef.h"
35
36 #include "BufferView.h"
37 #include "CutAndPaste.h"
38 #include "FuncStatus.h"
39 #include "LColor.h"
40 #include "bufferview_funcs.h"
41 #include "coordcache.h"
42 #include "cursor.h"
43 #include "debug.h"
44 #include "dispatchresult.h"
45 #include "funcrequest.h"
46 #include "gettext.h"
47 #include "outputparams.h"
48 #include "undo.h"
49
50 #include "support/lstrings.h"
51
52 #include "frontends/Dialogs.h"
53 #include "frontends/Gui.h"
54 #include "frontends/LyXView.h"
55 #include "frontends/Painter.h"
56 #include "frontends/Selection.h"
57 #include "frontends/nullpainter.h"
58
59 #include <sstream>
60
61 using lyx::cap::copySelection;
62 using lyx::cap::grabAndEraseSelection;
63 using lyx::cap::cutSelection;
64 using lyx::cap::replaceSelection;
65 using lyx::cap::selClearOrDel;
66
67 using lyx::frontend::Gui;
68 using lyx::frontend::Clipboard;
69
70 using std::endl;
71 using std::string;
72 using std::istringstream;
73
74
75 InsetMathNest::InsetMathNest(idx_type nargs)
76         : cells_(nargs), lock_(false)
77 {}
78
79
80 InsetMath::idx_type InsetMathNest::nargs() const
81 {
82         return cells_.size();
83 }
84
85
86 MathArray & InsetMathNest::cell(idx_type i)
87 {
88         return cells_[i];
89 }
90
91
92 MathArray const & InsetMathNest::cell(idx_type i) const
93 {
94         return cells_[i];
95 }
96
97
98 void InsetMathNest::cursorPos(CursorSlice const & sl, bool /*boundary*/,
99         int & x, int & y) const
100 {
101 // FIXME: This is a hack. Ideally, the coord cache should not store
102 // absolute positions, but relative ones. This would mean to call
103 // setXY() not in MathArray::draw(), but in the parent insets' draw()
104 // with the correctly adjusted x,y values. But this means that we'd have
105 // to touch all (math)inset's draw() methods. Right now, we'll store
106 // absolute value, and make them here relative, only to make them
107 // absolute again when actually drawing the cursor. What a mess.
108         BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
109         MathArray const & ar = sl.cell();
110         if (!theCoords.getArrays().has(&ar)) {
111                 // this can (semi-)legally happen if we just created this cell
112                 // and it never has been drawn before. So don't ASSERT.
113                 //lyxerr << "no cached data for array " << &ar << endl;
114                 x = 0;
115                 y = 0;
116                 return;
117         }
118         Point const pt = theCoords.getArrays().xy(&ar);
119         if (!theCoords.getInsets().has(this)) {
120                 // same as above
121                 //lyxerr << "no cached data for inset " << this << endl;
122                 x = 0;
123                 y = 0;
124                 return;
125         }
126         Point const pt2 = theCoords.getInsets().xy(this);
127         //lyxerr << "retrieving position cache for MathArray "
128         //      << pt.x_ << ' ' << pt.y_ << std::endl;
129         x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
130         y = pt.y_ - pt2.y_;
131 //      lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
132 //              << " asc: " << ascent() << "  des: " << descent()
133 //              << " ar.asc: " << ar.ascent() << " ar.des: " << ar.descent() << endl;
134         // move cursor visually into empty cells ("blue rectangles");
135         if (ar.empty())
136                 x += 2;
137 }
138
139
140 void InsetMathNest::metrics(MetricsInfo const & mi) const
141 {
142         MetricsInfo m = mi;
143         for (idx_type i = 0, n = nargs(); i != n; ++i)
144                 cell(i).metrics(m);
145 }
146
147
148 bool InsetMathNest::idxNext(LCursor & cur) const
149 {
150         BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
151         if (cur.idx() == cur.lastidx())
152                 return false;
153         ++cur.idx();
154         cur.pos() = 0;
155         return true;
156 }
157
158
159 bool InsetMathNest::idxRight(LCursor & cur) const
160 {
161         return idxNext(cur);
162 }
163
164
165 bool InsetMathNest::idxPrev(LCursor & cur) const
166 {
167         BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
168         if (cur.idx() == 0)
169                 return false;
170         --cur.idx();
171         cur.pos() = cur.lastpos();
172         return true;
173 }
174
175
176 bool InsetMathNest::idxLeft(LCursor & cur) const
177 {
178         return idxPrev(cur);
179 }
180
181
182 bool InsetMathNest::idxFirst(LCursor & cur) const
183 {
184         BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
185         if (nargs() == 0)
186                 return false;
187         cur.idx() = 0;
188         cur.pos() = 0;
189         return true;
190 }
191
192
193 bool InsetMathNest::idxLast(LCursor & cur) const
194 {
195         BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
196         if (nargs() == 0)
197                 return false;
198         cur.idx() = cur.lastidx();
199         cur.pos() = cur.lastpos();
200         return true;
201 }
202
203
204 void InsetMathNest::dump() const
205 {
206         WriteStream os(lyxerr);
207         os << "---------------------------------------------\n";
208         write(os);
209         os << "\n";
210         for (idx_type i = 0, n = nargs(); i != n; ++i)
211                 os << cell(i) << "\n";
212         os << "---------------------------------------------\n";
213 }
214
215
216 void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
217 {
218 #if 0
219         if (lock_)
220                 pi.pain.fillRectangle(x, y - ascent(), width(), height(),
221                                         LColor::mathlockbg);
222 #endif
223         setPosCache(pi, x, y);
224 }
225
226
227 void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
228 {
229         // this should use the x/y values given, not the cached values
230         LCursor & cur = pi.base.bv->cursor();
231         if (!cur.selection())
232                 return;
233         if (!ptr_cmp(&cur.inset(), this))
234                 return;
235
236         // FIXME: hack to get position cache warm
237         static lyx::frontend::NullPainter nop;
238         PainterInfo pinop(pi);
239         pinop.pain = nop;
240         draw(pinop, x, y);
241
242         CursorSlice s1 = cur.selBegin();
243         CursorSlice s2 = cur.selEnd();
244         //lyxerr << "InsetMathNest::drawing selection: "
245         //      << " s1: " << s1 << " s2: " << s2 << endl;
246         if (s1.idx() == s2.idx()) {
247                 MathArray const & c = cell(s1.idx());
248                 int x1 = c.xo() + c.pos2x(s1.pos());
249                 int y1 = c.yo() - c.ascent();
250                 int x2 = c.xo() + c.pos2x(s2.pos());
251                 int y2 = c.yo() + c.descent();
252                 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
253         //lyxerr << "InsetMathNest::drawing selection 3: "
254         //      << " x1: " << x1 << " x2: " << x2
255         //      << " y1: " << y1 << " y2: " << y2 << endl;
256         } else {
257                 for (idx_type i = 0; i < nargs(); ++i) {
258                         if (idxBetween(i, s1.idx(), s2.idx())) {
259                                 MathArray const & c = cell(i);
260                                 int x1 = c.xo();
261                                 int y1 = c.yo() - c.ascent();
262                                 int x2 = c.xo() + c.width();
263                                 int y2 = c.yo() + c.descent();
264                                 pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, LColor::selection);
265                         }
266                 }
267         }
268 }
269
270
271 void InsetMathNest::validate(LaTeXFeatures & features) const
272 {
273         for (idx_type i = 0; i < nargs(); ++i)
274                 cell(i).validate(features);
275 }
276
277
278 void InsetMathNest::replace(ReplaceData & rep)
279 {
280         for (idx_type i = 0; i < nargs(); ++i)
281                 cell(i).replace(rep);
282 }
283
284
285 bool InsetMathNest::contains(MathArray const & ar) const
286 {
287         for (idx_type i = 0; i < nargs(); ++i)
288                 if (cell(i).contains(ar))
289                         return true;
290         return false;
291 }
292
293
294 bool InsetMathNest::lock() const
295 {
296         return lock_;
297 }
298
299
300 void InsetMathNest::lock(bool l)
301 {
302         lock_ = l;
303 }
304
305
306 bool InsetMathNest::isActive() const
307 {
308         return nargs() > 0;
309 }
310
311
312 MathArray InsetMathNest::glue() const
313 {
314         MathArray ar;
315         for (size_t i = 0; i < nargs(); ++i)
316                 ar.append(cell(i));
317         return ar;
318 }
319
320
321 void InsetMathNest::write(WriteStream & os) const
322 {
323         os << '\\' << name().c_str();
324         for (size_t i = 0; i < nargs(); ++i)
325                 os << '{' << cell(i) << '}';
326         if (nargs() == 0)
327                 os.pendingSpace(true);
328         if (lock_ && !os.latex()) {
329                 os << "\\lyxlock";
330                 os.pendingSpace(true);
331         }
332 }
333
334
335 void InsetMathNest::normalize(NormalStream & os) const
336 {
337         os << '[' << name().c_str();
338         for (size_t i = 0; i < nargs(); ++i)
339                 os << ' ' << cell(i);
340         os << ']';
341 }
342
343
344 int InsetMathNest::latex(Buffer const &, std::ostream & os,
345                         OutputParams const & runparams) const
346 {
347         WriteStream wi(os, runparams.moving_arg, true);
348         write(wi);
349         return wi.line();
350 }
351
352
353 bool InsetMathNest::notifyCursorLeaves(LCursor & /*cur*/)
354 {
355 #ifdef WITH_WARNINGS
356 #warning look here
357 #endif
358 #if 0
359         MathArray & ar = cur.cell();
360         // remove base-only "scripts"
361         for (pos_type i = 0; i + 1 < ar.size(); ++i) {
362                 InsetMathScript * p = operator[](i).nucleus()->asScriptInset();
363                 if (p && p->nargs() == 1) {
364                         MathArray ar = p->nuc();
365                         erase(i);
366                         insert(i, ar);
367                         cur.adjust(i, ar.size() - 1);
368                 }
369         }
370
371         // glue adjacent font insets of the same kind
372         for (pos_type i = 0; i + 1 < size(); ++i) {
373                 InsetMathFont * p = operator[](i).nucleus()->asFontInset();
374                 InsetMathFont const * q = operator[](i + 1)->asFontInset();
375                 if (p && q && p->name() == q->name()) {
376                         p->cell(0).append(q->cell(0));
377                         erase(i + 1);
378                         cur.adjust(i, -1);
379                 }
380         }
381 #endif
382         return false;
383 }
384
385
386 void InsetMathNest::handleFont
387         (LCursor & cur, string const & arg, string const & font)
388 {
389         // this whole function is a hack and won't work for incremental font
390         // changes...
391         recordUndo(cur, Undo::ATOMIC);
392
393         if (cur.inset().asInsetMath()->name() == font)
394                 cur.handleFont(font);
395         else {
396                 cur.handleNest(createInsetMath(font));
397                 cur.insert(arg);
398         }
399 }
400
401
402 void InsetMathNest::handleFont2(LCursor & cur, string const & arg)
403 {
404         recordUndo(cur, Undo::ATOMIC);
405         LyXFont font;
406         bool b;
407         bv_funcs::string2font(arg, font, b);
408         if (font.color() != LColor::inherit) {
409                 MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
410                 cur.handleNest(at, 0);
411         }
412 }
413
414
415 void InsetMathNest::doDispatch(LCursor & cur, FuncRequest & cmd)
416 {
417         //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
418         //CursorSlice sl = cur.current();
419
420         switch (cmd.action) {
421
422         case LFUN_PASTE: {
423                 recordUndo(cur);
424                 cur.message(_("Paste"));
425                 replaceSelection(cur);
426                 size_t n = 0;
427                 istringstream is(lyx::to_utf8(cmd.argument()));
428                 is >> n;
429                 string const selection = lyx::cap::getSelection(cur.buffer(), n);
430                 cur.niceInsert(selection);
431                 cur.clearSelection(); // bug 393
432                 cur.bv().switchKeyMap();
433                 finishUndo();
434                 break;
435         }
436
437         case LFUN_CUT:
438                 recordUndo(cur);
439                 cutSelection(cur, true, true);
440                 cur.message(_("Cut"));
441                 // Prevent stale position >= size crash
442                 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
443                 cur.normalize();
444                 break;
445
446         case LFUN_COPY:
447                 copySelection(cur);
448                 // FIXME UNICODE
449                 cur.message(_("Copy"));
450                 break;
451
452         case LFUN_MOUSE_PRESS:
453                 lfunMousePress(cur, cmd);
454                 break;
455
456         case LFUN_MOUSE_MOTION:
457                 lfunMouseMotion(cur, cmd);
458                 break;
459
460         case LFUN_MOUSE_RELEASE:
461                 lfunMouseRelease(cur, cmd);
462                 break;
463
464         case LFUN_FINISHED_LEFT:
465                 cur.bv().cursor() = cur;
466                 break;
467
468         case LFUN_FINISHED_RIGHT:
469                 ++cur.pos();
470                 cur.bv().cursor() = cur;
471                 break;
472
473         case LFUN_FINISHED_UP:
474                 cur.bv().cursor() = cur;
475                 break;
476
477         case LFUN_FINISHED_DOWN:
478                 ++cur.pos();
479                 cur.bv().cursor() = cur;
480                 break;
481
482         case LFUN_CHAR_FORWARD_SELECT:
483         case LFUN_CHAR_FORWARD:
484                 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
485                 cur.autocorrect() = false;
486                 cur.clearTargetX();
487                 cur.macroModeClose();
488                 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
489                         cur.pushLeft(*cur.nextAtom().nucleus());
490                         cur.inset().idxFirst(cur);
491                 } else if (cur.posRight() || idxRight(cur)
492                         || cur.popRight() || cur.selection())
493                         ;
494                 else {
495                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
496                         cur.undispatched();
497                 }
498                 break;
499
500         case LFUN_CHAR_BACKWARD_SELECT:
501         case LFUN_CHAR_BACKWARD:
502                 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
503                 cur.autocorrect() = false;
504                 cur.clearTargetX();
505                 cur.macroModeClose();
506                 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
507                         cur.posLeft();
508                         cur.push(*cur.nextAtom().nucleus());
509                         cur.inset().idxLast(cur);
510                 } else if (cur.posLeft() || idxLeft(cur)
511                         || cur.popLeft() || cur.selection())
512                         ;
513                 else {
514                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
515                         cur.undispatched();
516                 }
517                 break;
518
519         case LFUN_UP_SELECT:
520         case LFUN_UP:
521                 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
522                 if (cur.inMacroMode()) {
523                         // Make Helge happy
524                         cur.macroModeClose();
525                         break;
526                 }
527                 cur.selHandle(cmd.action == LFUN_UP_SELECT);
528                 if (!cur.up()) {
529                         cmd = FuncRequest(LFUN_FINISHED_UP);
530                         cur.undispatched();
531                 }
532                 // fixes bug 1598. Please check!
533                 cur.normalize();
534                 break;
535
536         case LFUN_DOWN_SELECT:
537         case LFUN_DOWN:
538                 if (cur.inMacroMode()) {
539                         cur.macroModeClose();
540                         break;
541                 }
542                 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
543                 if (!cur.down()) {
544                         cmd = FuncRequest(LFUN_FINISHED_DOWN);
545                         cur.undispatched();
546                 }
547                 // fixes bug 1598. Please check!
548                 cur.normalize();
549                 break;
550
551         case LFUN_MOUSE_DOUBLE:
552         case LFUN_MOUSE_TRIPLE:
553         case LFUN_WORD_SELECT:
554                 cur.pos() = 0;
555                 cur.idx() = 0;
556                 cur.resetAnchor();
557                 cur.selection() = true;
558                 cur.pos() = cur.lastpos();
559                 cur.idx() = cur.lastidx();
560                 break;
561
562         case LFUN_PARAGRAPH_UP_SELECT:
563         case LFUN_PARAGRAPH_UP:
564         case LFUN_PARAGRAPH_DOWN_SELECT:
565         case LFUN_PARAGRAPH_DOWN:
566                 break;
567
568         case LFUN_LINE_BEGIN_SELECT:
569         case LFUN_LINE_BEGIN:
570         case LFUN_WORD_BACKWARD_SELECT:
571         case LFUN_WORD_BACKWARD:
572                 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
573                                 cmd.action == LFUN_LINE_BEGIN_SELECT);
574                 cur.macroModeClose();
575                 if (cur.pos() != 0) {
576                         cur.pos() = 0;
577                 } else if (cur.col() != 0) {
578                         cur.idx() -= cur.col();
579                         cur.pos() = 0;
580                 } else if (cur.idx() != 0) {
581                         cur.idx() = 0;
582                         cur.pos() = 0;
583                 } else {
584                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
585                         cur.undispatched();
586                 }
587                 break;
588
589         case LFUN_WORD_FORWARD_SELECT:
590         case LFUN_WORD_FORWARD:
591         case LFUN_LINE_END_SELECT:
592         case LFUN_LINE_END:
593                 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
594                                 cmd.action == LFUN_LINE_END_SELECT);
595                 cur.macroModeClose();
596                 cur.clearTargetX();
597                 if (cur.pos() != cur.lastpos()) {
598                         cur.pos() = cur.lastpos();
599                 } else if (ncols() && (cur.col() != cur.lastcol())) {
600                         cur.idx() = cur.idx() - cur.col() + cur.lastcol();
601                         cur.pos() = cur.lastpos();
602                 } else if (cur.idx() != cur.lastidx()) {
603                         cur.idx() = cur.lastidx();
604                         cur.pos() = cur.lastpos();
605                 } else {
606                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
607                         cur.undispatched();
608                 }
609                 break;
610
611         case LFUN_SCREEN_UP_SELECT:
612         case LFUN_SCREEN_UP:
613                 cmd = FuncRequest(LFUN_FINISHED_LEFT);
614                 cur.undispatched();
615                 break;
616
617         case LFUN_SCREEN_DOWN_SELECT:
618         case LFUN_SCREEN_DOWN:
619                 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
620                 cur.undispatched();
621                 break;
622
623         case LFUN_CELL_FORWARD:
624                 cur.inset().idxNext(cur);
625                 break;
626
627         case LFUN_CELL_BACKWARD:
628                 cur.inset().idxPrev(cur);
629                 break;
630
631         case LFUN_WORD_DELETE_BACKWARD:
632         case LFUN_CHAR_DELETE_BACKWARD:
633                 if (cur.pos() == 0)
634                         // May affect external cell:
635                         recordUndoInset(cur, Undo::ATOMIC);
636                 else
637                         recordUndo(cur, Undo::ATOMIC);
638                 cur.backspace();
639                 break;
640
641         case LFUN_WORD_DELETE_FORWARD:
642         case LFUN_CHAR_DELETE_FORWARD:
643                 if (cur.pos() == cur.lastpos())
644                         // May affect external cell:
645                         recordUndoInset(cur, Undo::ATOMIC);
646                 else
647                         recordUndo(cur, Undo::ATOMIC);
648                 cur.erase();
649                 break;
650
651         case LFUN_ESCAPE:
652                 if (cur.selection())
653                         cur.clearSelection();
654                 else  {
655                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
656                         cur.undispatched();
657                 }
658                 break;
659
660         case LFUN_INSET_TOGGLE:
661                 recordUndo(cur);
662                 lock(!lock());
663                 cur.popRight();
664                 break;
665
666         case LFUN_SELF_INSERT:
667                 if (cmd.argument().size() != 1) {
668                         recordUndo(cur);
669                         string const arg = lyx::to_utf8(cmd.argument());
670                         if (!interpret(cur, arg))
671                                 cur.insert(arg);
672                         break;
673                 }
674                 // Don't record undo steps if we are in macro mode and
675                 // cmd.argument is the next character of the macro name.
676                 // Otherwise we'll get an invalid cursor if we undo after
677                 // the macro was finished and the macro is a known command,
678                 // e.g. sqrt. LCursor::macroModeClose replaces in this case
679                 // the InsetMathUnknown with name "frac" by an empty
680                 // InsetMathFrac -> a pos value > 0 is invalid.
681                 // A side effect is that an undo before the macro is finished
682                 // undoes the complete macro, not only the last character.
683                 if (!cur.inMacroMode())
684                         recordUndo(cur);
685
686                 // spacial handling of space. If we insert an inset
687                 // via macro mode, we want to put the cursor inside it
688                 // if relevant. Think typing "\frac<space>".
689                 if (cmd.argument()[0] == ' '
690                     && cur.inMacroMode() && cur.macroName() != "\\"
691                     && cur.macroModeClose()) {
692                         MathAtom const atom = cur.prevAtom();
693                         if (atom->asNestInset() && atom->nargs() > 0) {
694                                 cur.posLeft();
695                                 cur.pushLeft(*cur.nextInset());
696                         }
697                 // FIXME: Change to
698                 // } else if (!interpret(cur, cmd.argument()[0])) {
699                 // when interpret accepts UCS4 characters
700                 } else if (!interpret(cur, lyx::to_utf8(cmd.argument()))) {
701                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
702                         cur.undispatched();
703                 }
704                 break;
705
706         //case LFUN_SERVER_GET_XY:
707         //      sprintf(dispatch_buffer, "%d %d",);
708         //      break;
709
710         case LFUN_SERVER_SET_XY: {
711                 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
712                 int x = 0;
713                 int y = 0;
714                 istringstream is(lyx::to_utf8(cmd.argument()));
715                 is >> x >> y;
716                 cur.setScreenPos(x, y);
717                 break;
718         }
719
720         // Special casing for superscript in case of LyX handling
721         // dead-keys:
722         case LFUN_ACCENT_CIRCUMFLEX:
723                 if (cmd.argument().empty()) {
724                         // do superscript if LyX handles
725                         // deadkeys
726                         recordUndo(cur, Undo::ATOMIC);
727                         script(cur, true, grabAndEraseSelection(cur));
728                 }
729                 break;
730
731         case LFUN_ACCENT_UMLAUT:
732         case LFUN_ACCENT_ACUTE:
733         case LFUN_ACCENT_GRAVE:
734         case LFUN_ACCENT_BREVE:
735         case LFUN_ACCENT_DOT:
736         case LFUN_ACCENT_MACRON:
737         case LFUN_ACCENT_CARON:
738         case LFUN_ACCENT_TILDE:
739         case LFUN_ACCENT_CEDILLA:
740         case LFUN_ACCENT_CIRCLE:
741         case LFUN_ACCENT_UNDERDOT:
742         case LFUN_ACCENT_TIE:
743         case LFUN_ACCENT_OGONEK:
744         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
745                 break;
746
747         //  Math fonts
748         case LFUN_FONT_FREE_APPLY:
749         case LFUN_FONT_FREE_UPDATE:
750                 handleFont2(cur, lyx::to_utf8(cmd.argument()));
751                 break;
752
753         case LFUN_FONT_BOLD:
754                 if (currentMode() == TEXT_MODE)
755                         handleFont(cur, lyx::to_utf8(cmd.argument()), "textbf");
756                 else
757                         handleFont(cur, lyx::to_utf8(cmd.argument()), "mathbf");
758                 break;
759         case LFUN_FONT_SANS:
760                 if (currentMode() == TEXT_MODE)
761                         handleFont(cur, lyx::to_utf8(cmd.argument()), "textsf");
762                 else
763                         handleFont(cur, lyx::to_utf8(cmd.argument()), "mathsf");
764                 break;
765         case LFUN_FONT_EMPH:
766                 if (currentMode() == TEXT_MODE)
767                         handleFont(cur, lyx::to_utf8(cmd.argument()), "emph");
768                 else
769                         handleFont(cur, lyx::to_utf8(cmd.argument()), "mathcal");
770                 break;
771         case LFUN_FONT_ROMAN:
772                 if (currentMode() == TEXT_MODE)
773                         handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
774                 else
775                         handleFont(cur, lyx::to_utf8(cmd.argument()), "mathrm");
776                 break;
777         case LFUN_FONT_CODE:
778                 if (currentMode() == TEXT_MODE)
779                         handleFont(cur, lyx::to_utf8(cmd.argument()), "texttt");
780                 else
781                         handleFont(cur, lyx::to_utf8(cmd.argument()), "mathtt");
782                 break;
783         case LFUN_FONT_FRAK:
784                 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathfrak");
785                 break;
786         case LFUN_FONT_ITAL:
787                 if (currentMode() == TEXT_MODE)
788                         handleFont(cur, lyx::to_utf8(cmd.argument()), "textit");
789                 else
790                         handleFont(cur, lyx::to_utf8(cmd.argument()), "mathit");
791                 break;
792         case LFUN_FONT_NOUN:
793                 if (currentMode() == TEXT_MODE)
794                         // FIXME: should be "noun"
795                         handleFont(cur, lyx::to_utf8(cmd.argument()), "textsc");
796                 else
797                         handleFont(cur, lyx::to_utf8(cmd.argument()), "mathbb");
798                 break;
799         //case LFUN_FONT_FREE_APPLY:
800                 handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
801                 break;
802         case LFUN_FONT_DEFAULT:
803                 handleFont(cur, lyx::to_utf8(cmd.argument()), "textnormal");
804                 break;
805
806         case LFUN_MATH_MODE: {
807 #if 1
808                 // ignore math-mode on when already in math mode
809                 if (currentMode() == InsetBase::MATH_MODE && cmd.argument() == "on")
810                         break;
811                 cur.macroModeClose();
812                 string const save_selection = grabAndEraseSelection(cur);
813                 selClearOrDel(cur);
814                 //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
815                 cur.plainInsert(MathAtom(new InsetMathBox("mbox")));
816                 cur.posLeft();
817                 cur.pushLeft(*cur.nextInset());
818                 cur.niceInsert(save_selection);
819 #else
820                 if (currentMode() == InsetBase::TEXT_MODE) {
821                         cur.niceInsert(MathAtom(new InsetMathHull("simple")));
822                         cur.message(_("create new math text environment ($...$)"));
823                 } else {
824                         handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
825                         cur.message(_("entered math text mode (textrm)"));
826                 }
827 #endif
828                 break;
829         }
830
831         case LFUN_MATH_SIZE:
832 #if 0
833                 recordUndo(cur);
834                 cur.setSize(arg);
835 #endif
836                 break;
837
838         case LFUN_MATH_MATRIX: {
839                 recordUndo(cur, Undo::ATOMIC);
840                 unsigned int m = 1;
841                 unsigned int n = 1;
842                 string v_align;
843                 string h_align;
844                 istringstream is(lyx::to_utf8(cmd.argument()));
845                 is >> m >> n >> v_align >> h_align;
846                 if (m < 1)
847                         m = 1;
848                 if (n < 1)
849                         n = 1;
850                 v_align += 'c';
851                 cur.niceInsert(
852                         MathAtom(new InsetMathArray("array", m, n, v_align[0], h_align)));
853                 break;
854         }
855
856         case LFUN_MATH_DELIM: {
857                 string ls;
858                 string rs = lyx::support::split(lyx::to_utf8(cmd.argument()), ls, ' ');
859                 // Reasonable default values
860                 if (ls.empty())
861                         ls = '(';
862                 if (rs.empty())
863                         rs = ')';
864                 recordUndo(cur, Undo::ATOMIC);
865                 cur.handleNest(MathAtom(new InsetMathDelim(ls, rs)));
866                 break;
867         }
868
869         case LFUN_MATH_BIGDELIM: {
870                 string const lname = cmd.getArg(0);
871                 string const ldelim = cmd.getArg(1);
872                 string const rname = cmd.getArg(2);
873                 string const rdelim = cmd.getArg(3);
874                 latexkeys const * l = in_word_set(lname);
875                 bool const have_l = l && l->inset == "big" &&
876                                     InsetMathBig::isBigInsetDelim(ldelim);
877                 l = in_word_set(rname);
878                 bool const have_r = l && l->inset == "big" &&
879                                     InsetMathBig::isBigInsetDelim(rdelim);
880                 // We mimic LFUN_MATH_DELIM in case we have an empty left
881                 // or right delimiter.
882                 if (have_l || have_r) {
883                         recordUndo(cur, Undo::ATOMIC);
884                         string const selection = grabAndEraseSelection(cur);
885                         selClearOrDel(cur);
886                         if (have_l)
887                                 cur.insert(MathAtom(new InsetMathBig(lname,
888                                                                 ldelim)));
889                         cur.niceInsert(selection);
890                         if (have_r)
891                                 cur.insert(MathAtom(new InsetMathBig(rname,
892                                                                 rdelim)));
893                 }
894                 // Don't call cur.undispatched() if we did nothing, this would
895                 // lead to infinite recursion via LyXText::dispatch().
896                 break;
897         }
898
899         case LFUN_SPACE_INSERT:
900         case LFUN_MATH_SPACE:
901                 recordUndo(cur, Undo::ATOMIC);
902                 cur.insert(MathAtom(new InsetMathSpace(",")));
903                 break;
904
905         case LFUN_ERT_INSERT:
906                 // interpret this as if a backslash was typed
907                 recordUndo(cur, Undo::ATOMIC);
908                 interpret(cur, '\\');
909                 break;
910
911         case LFUN_MATH_SUBSCRIPT:
912                 // interpret this as if a _ was typed
913                 recordUndo(cur, Undo::ATOMIC);
914                 interpret(cur, '_');
915                 break;
916
917         case LFUN_MATH_SUPERSCRIPT:
918                 // interpret this as if a ^ was typed
919                 recordUndo(cur, Undo::ATOMIC);
920                 interpret(cur, '^');
921                 break;
922
923 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
924 // handling such that "self-insert" works on "arbitrary stuff" too, and
925 // math-insert only handles special math things like "matrix".
926         case LFUN_MATH_INSERT: {
927                 recordUndo(cur, Undo::ATOMIC);
928                 if (cmd.argument() == "^" || cmd.argument() == "_")
929                         interpret(cur, cmd.argument()[0]);
930                 else
931                         cur.niceInsert(lyx::to_utf8(cmd.argument()));
932                 break;
933                 }
934
935         case LFUN_DIALOG_SHOW_NEW_INSET: {
936                 string const & name = lyx::to_utf8(cmd.argument());
937                 string data;
938                 if (name == "ref") {
939                         RefInset tmp(name);
940                         data = tmp.createDialogStr(name);
941                 }
942                 cur.bv().owner()->getDialogs().show(name, data, 0);
943                 break;
944         }
945
946         default:
947                 InsetMathDim::doDispatch(cur, cmd);
948                 break;
949         }
950 }
951
952
953 bool InsetMathNest::getStatus(LCursor & cur, FuncRequest const & cmd,
954                 FuncStatus & flag) const
955 {
956         // the font related toggles
957         //string tc = "mathnormal";
958         bool ret = true;
959         string const arg = lyx::to_utf8(cmd.argument());
960         switch (cmd.action) {
961         case LFUN_TABULAR_FEATURE:
962                 flag.enabled(false);
963                 break;
964 #if 0
965         case LFUN_TABULAR_FEATURE:
966                 // FIXME: check temporarily disabled
967                 // valign code
968                 char align = mathcursor::valign();
969                 if (align == '\0') {
970                         enable = false;
971                         break;
972                 }
973                 if (cmd.argument().empty()) {
974                         flag.clear();
975                         break;
976                 }
977                 if (!contains("tcb", cmd.argument()[0])) {
978                         enable = false;
979                         break;
980                 }
981                 flag.setOnOff(cmd.argument()[0] == align);
982                 break;
983 #endif
984         /// We have to handle them since 1.4 blocks all unhandled actions
985         case LFUN_FONT_ITAL:
986         case LFUN_FONT_BOLD:
987         case LFUN_FONT_SANS:
988         case LFUN_FONT_EMPH:
989         case LFUN_FONT_CODE:
990         case LFUN_FONT_NOUN:
991         case LFUN_FONT_ROMAN:
992         case LFUN_FONT_DEFAULT:
993                 flag.enabled(true);
994                 break;
995         case LFUN_MATH_MUTATE:
996                 //flag.setOnOff(mathcursor::formula()->hullType() == lyx::to_utf8(cmd.argument()));
997                 flag.setOnOff(false);
998                 break;
999
1000         // we just need to be in math mode to enable that
1001         case LFUN_MATH_SIZE:
1002         case LFUN_MATH_SPACE:
1003         case LFUN_MATH_LIMITS:
1004         case LFUN_MATH_NONUMBER:
1005         case LFUN_MATH_NUMBER:
1006         case LFUN_MATH_EXTERN:
1007                 flag.enabled(true);
1008                 break;
1009
1010         case LFUN_FONT_FRAK:
1011                 flag.enabled(currentMode() != TEXT_MODE);
1012                 break;
1013
1014         case LFUN_MATH_INSERT: {
1015                 bool const textarg =
1016                         arg == "\\textbf"   || arg == "\\textsf" ||
1017                         arg == "\\textrm"   || arg == "\\textmd" ||
1018                         arg == "\\textit"   || arg == "\\textsc" ||
1019                         arg == "\\textsl"   || arg == "\\textup" ||
1020                         arg == "\\texttt"   || arg == "\\textbb" ||
1021                         arg == "\\textnormal";
1022                 flag.enabled(currentMode() != TEXT_MODE || textarg);
1023                 break;
1024         }
1025
1026         case LFUN_MATH_MATRIX:
1027                 flag.enabled(currentMode() == MATH_MODE);
1028                 break;
1029
1030         case LFUN_MATH_DELIM:
1031         case LFUN_MATH_BIGDELIM:
1032                 // Don't do this with multi-cell selections
1033                 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1034                 break;
1035
1036         default:
1037                 ret = false;
1038                 break;
1039         }
1040         return ret;
1041 }
1042
1043
1044 void InsetMathNest::edit(LCursor & cur, bool left)
1045 {
1046         cur.push(*this);
1047         cur.idx() = left ? 0 : cur.lastidx();
1048         cur.pos() = left ? 0 : cur.lastpos();
1049         cur.resetAnchor();
1050         //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
1051 }
1052
1053
1054 InsetBase * InsetMathNest::editXY(LCursor & cur, int x, int y)
1055 {
1056         int idx_min = 0;
1057         int dist_min = 1000000;
1058         for (idx_type i = 0, n = nargs(); i != n; ++i) {
1059                 int const d = cell(i).dist(x, y);
1060                 if (d < dist_min) {
1061                         dist_min = d;
1062                         idx_min = i;
1063                 }
1064         }
1065         MathArray & ar = cell(idx_min);
1066         cur.push(*this);
1067         cur.idx() = idx_min;
1068         cur.pos() = ar.x2pos(x - ar.xo());
1069         //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1070         if (dist_min == 0) {
1071                 // hit inside cell
1072                 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1073                         if (ar[i]->covers(x, y))
1074                                 return ar[i].nucleus()->editXY(cur, x, y);
1075         }
1076         return this;
1077 }
1078
1079
1080 void InsetMathNest::lfunMousePress(LCursor & cur, FuncRequest & cmd)
1081 {
1082         //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1083         BufferView & bv = cur.bv();
1084         if (cmd.button() == mouse_button::button1) {
1085                 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1086                 bv.mouseSetCursor(cur);
1087         } else if (cmd.button() == mouse_button::button2) {
1088                 MathArray ar;
1089                 if (cur.selection())
1090                         asArray(lyx::to_utf8(bv.cursor().selectionAsString(false)), ar);
1091                 else
1092                         asArray(lyx::to_utf8(bv.owner()->gui().selection().get()), ar);
1093
1094                 cur.insert(ar);
1095                 bv.mouseSetCursor(cur);
1096         }
1097 }
1098
1099
1100 void InsetMathNest::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
1101 {
1102         // only select with button 1
1103         if (cmd.button() == mouse_button::button1) {
1104                 LCursor & bvcur = cur.bv().cursor();
1105                 if (bvcur.anchor_.hasPart(cur)) {
1106                         //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1107                         bvcur.setCursor(cur);
1108                         bvcur.selection() = true;
1109                         //lyxerr << "MOTION " << bvcur << endl;
1110                 }
1111                 else {
1112                         cur.undispatched();
1113                 }
1114         }
1115 }
1116
1117
1118 void InsetMathNest::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
1119 {
1120         //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1121
1122         if (cmd.button() == mouse_button::button1) {
1123                 //cur.bv().owner()->gui().selection().put(cur.grabSelection());
1124                 return;
1125         }
1126
1127         if (cmd.button() == mouse_button::button3) {
1128                 // try to dispatch to enclosed insets first
1129                 cur.bv().owner()->getDialogs().show("mathpanel");
1130                 return;
1131         }
1132
1133         cur.undispatched();
1134 }
1135
1136
1137 bool InsetMathNest::interpret(LCursor & cur, char c)
1138 {
1139         //lyxerr << "interpret 2: '" << c << "'" << endl;
1140         string save_selection;
1141         if (c == '^' || c == '_')
1142                 save_selection = grabAndEraseSelection(cur);
1143
1144         cur.clearTargetX();
1145
1146         // handle macroMode
1147         if (cur.inMacroMode()) {
1148                 string name = cur.macroName();
1149
1150                 /// are we currently typing '#1' or '#2' or...?
1151                 if (name == "\\#") {
1152                         cur.backspace();
1153                         int n = c - '0';
1154                         if (n >= 1 && n <= 9)
1155                                 cur.insert(new MathMacroArgument(n));
1156                         return true;
1157                 }
1158
1159                 if (isalpha(c)) {
1160                         cur.activeMacro()->setName(name + c);
1161                         return true;
1162                 }
1163
1164                 // handle 'special char' macros
1165                 if (name == "\\") {
1166                         // remove the '\\'
1167                         if (c == '\\') {
1168                                 cur.backspace();
1169                                 if (currentMode() == InsetMath::TEXT_MODE)
1170                                         cur.niceInsert(createInsetMath("textbackslash"));
1171                                 else
1172                                         cur.niceInsert(createInsetMath("backslash"));
1173                         } else if (c == '{') {
1174                                 cur.backspace();
1175                                 cur.niceInsert(MathAtom(new InsetMathBrace));
1176                         } else if (c == '%') {
1177                                 cur.backspace();
1178                                 cur.niceInsert(MathAtom(new InsetMathComment));
1179                         } else if (c == '#') {
1180                                 BOOST_ASSERT(cur.activeMacro());
1181                                 cur.activeMacro()->setName(name + c);
1182                         } else {
1183                                 cur.backspace();
1184                                 cur.niceInsert(createInsetMath(string(1, c)));
1185                         }
1186                         return true;
1187                 }
1188
1189                 // One character big delimiters. The others are handled in
1190                 // the other interpret() method.
1191                 latexkeys const * l = in_word_set(name.substr(1));
1192                 if (name[0] == '\\' && l && l->inset == "big") {
1193                         string delim;
1194                         switch (c) {
1195                         case '{':
1196                                 delim = "\\{";
1197                                 break;
1198                         case '}':
1199                                 delim = "\\}";
1200                                 break;
1201                         default:
1202                                 delim = string(1, c);
1203                                 break;
1204                         }
1205                         if (InsetMathBig::isBigInsetDelim(delim)) {
1206                                 // name + delim ared a valid InsetMathBig.
1207                                 // We can't use cur.macroModeClose() because
1208                                 // it does not handle delim.
1209                                 InsetMathUnknown * p = cur.activeMacro();
1210                                 p->finalize();
1211                                 --cur.pos();
1212                                 cur.cell().erase(cur.pos());
1213                                 cur.plainInsert(MathAtom(
1214                                         new InsetMathBig(name.substr(1), delim)));
1215                                 return true;
1216                         }
1217                 }
1218
1219                 // leave macro mode and try again if necessary
1220                 cur.macroModeClose();
1221                 if (c == '{')
1222                         cur.niceInsert(MathAtom(new InsetMathBrace));
1223                 else if (c != ' ')
1224                         interpret(cur, c);
1225                 return true;
1226         }
1227
1228         // This is annoying as one has to press <space> far too often.
1229         // Disable it.
1230
1231 #if 0
1232                 // leave autocorrect mode if necessary
1233                 if (autocorrect() && c == ' ') {
1234                         autocorrect() = false;
1235                         return true;
1236                 }
1237 #endif
1238
1239         // just clear selection on pressing the space bar
1240         if (cur.selection() && c == ' ') {
1241                 cur.selection() = false;
1242                 return true;
1243         }
1244
1245         selClearOrDel(cur);
1246
1247         if (c == '\\') {
1248                 //lyxerr << "starting with macro" << endl;
1249                 cur.insert(MathAtom(new InsetMathUnknown("\\", false)));
1250                 return true;
1251         }
1252
1253         if (c == '\n') {
1254                 if (currentMode() == InsetMath::TEXT_MODE)
1255                         cur.insert(c);
1256                 return true;
1257         }
1258
1259         if (c == ' ') {
1260                 if (currentMode() == InsetMath::TEXT_MODE) {
1261                         // insert spaces in text mode,
1262                         // but suppress direct insertion of two spaces in a row
1263                         // the still allows typing  '<space>a<space>' and deleting the 'a', but
1264                         // it is better than nothing...
1265                         if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
1266                                 cur.insert(c);
1267                         return true;
1268                 }
1269                 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1270                         cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1271                         return true;
1272                 }
1273                 if (cur.popRight())
1274                         return true;
1275                 // if are at the very end, leave the formula
1276                 return cur.pos() != cur.lastpos();
1277         }
1278
1279         // These shouldn't work in text mode:
1280         if (currentMode() != InsetMath::TEXT_MODE) {
1281                 if (c == '_') {
1282                         script(cur, false, save_selection);
1283                         return true;
1284                 }
1285                 if (c == '^') {
1286                         script(cur, true, save_selection);
1287                         return true;
1288                 }
1289                 if (c == '~') {
1290                         cur.niceInsert(createInsetMath("sim"));
1291                         return true;
1292                 }
1293         }
1294
1295         if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1296             c == '%' || c == '_' || c == '^') {
1297                 cur.niceInsert(createInsetMath(string(1, c)));
1298                 return true;
1299         }
1300
1301
1302         // try auto-correction
1303         //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1304         //      return true;
1305
1306         // no special circumstances, so insert the character without any fuss
1307         cur.insert(c);
1308         cur.autocorrect() = true;
1309         return true;
1310 }
1311
1312
1313 bool InsetMathNest::interpret(LCursor & cur, string const & str)
1314 {
1315         // Create a InsetMathBig from cur.cell()[cur.pos() - 1] and t if
1316         // possible
1317         if (!cur.empty() && cur.pos() > 0 &&
1318             cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1319                 if (InsetMathBig::isBigInsetDelim(str)) {
1320                         string prev = asString(cur.cell()[cur.pos() - 1]);
1321                         if (prev[0] == '\\') {
1322                                 prev = prev.substr(1);
1323                                 latexkeys const * l = in_word_set(prev);
1324                                 if (l && l->inset == "big") {
1325                                         cur.cell()[cur.pos() - 1] =
1326                                                 MathAtom(new InsetMathBig(prev, str));
1327                                         return true;
1328                                 }
1329                         }
1330                 }
1331         }
1332         return false;
1333 }
1334
1335
1336 bool InsetMathNest::script(LCursor & cur, bool up, string const &
1337                 save_selection)
1338 {
1339         // Hack to get \^ and \_ working
1340         //lyxerr << "handling script: up: " << up << endl;
1341         if (cur.inMacroMode() && cur.macroName() == "\\") {
1342                 if (up)
1343                         cur.niceInsert(createInsetMath("mathcircumflex"));
1344                 else
1345                         interpret(cur, '_');
1346                 return true;
1347         }
1348
1349         cur.macroModeClose();
1350         if (asScriptInset() && cur.idx() == 0) {
1351                 // we are in a nucleus of a script inset, move to _our_ script
1352                 InsetMathScript * inset = asScriptInset();
1353                 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1354                 inset->ensure(up);
1355                 cur.idx() = inset->idxOfScript(up);
1356                 cur.pos() = 0;
1357         } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1358                 --cur.pos();
1359                 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1360                 cur.push(*inset);
1361                 inset->ensure(up);
1362                 cur.idx() = inset->idxOfScript(up);
1363                 cur.pos() = cur.lastpos();
1364         } else {
1365                 // convert the thing to our left to a scriptinset or create a new
1366                 // one if in the very first position of the array
1367                 if (cur.pos() == 0) {
1368                         //lyxerr << "new scriptinset" << endl;
1369                         cur.insert(new InsetMathScript(up));
1370                 } else {
1371                         //lyxerr << "converting prev atom " << endl;
1372                         cur.prevAtom() = MathAtom(new InsetMathScript(cur.prevAtom(), up));
1373                 }
1374                 --cur.pos();
1375                 InsetMathScript * inset = cur.nextAtom().nucleus()->asScriptInset();
1376                 // special handling of {}-bases
1377                 // is this always correct?
1378                 if (inset->nuc().size() == 1
1379                     && inset->nuc().back()->asBraceInset())
1380                         inset->nuc() = inset->nuc().back()->asNestInset()->cell(0);
1381
1382                 cur.push(*inset);
1383                 cur.idx() = 1;
1384                 cur.pos() = 0;
1385         }
1386         //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1387         cur.niceInsert(save_selection);
1388         cur.resetAnchor();
1389         //lyxerr << "inserting selection 2:\n" << save_selection << endl;
1390         return true;
1391 }