]> git.lyx.org Git - lyx.git/blob - src/mathed/math_nestinset.C
Change _() to return a docstring. Fixup callers with the help of lyx::to_utf8.
[lyx.git] / src / mathed / math_nestinset.C
1 /**
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.
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 "math_nestinset.h"
14
15 #include "math_arrayinset.h"
16 #include "math_biginset.h"
17 #include "math_boxinset.h"
18 #include "math_braceinset.h"
19 #include "math_colorinset.h"
20 #include "math_commentinset.h"
21 #include "math_data.h"
22 #include "math_deliminset.h"
23 #include "math_factory.h"
24 #include "math_hullinset.h"
25 #include "math_mathmlstream.h"
26 #include "math_macroarg.h"
27 //#include "math_mboxinset.h"
28 #include "math_parser.h"
29 #include "math_scriptinset.h"
30 #include "math_spaceinset.h"
31 #include "math_symbolinset.h"
32 #include "math_support.h"
33 #include "math_unknowninset.h"
34 #include "ref_inset.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 MathNestInset::MathNestInset(idx_type nargs)
76         : cells_(nargs), lock_(false)
77 {}
78
79
80 MathInset::idx_type MathNestInset::nargs() const
81 {
82         return cells_.size();
83 }
84
85
86 MathArray & MathNestInset::cell(idx_type i)
87 {
88         return cells_[i];
89 }
90
91
92 MathArray const & MathNestInset::cell(idx_type i) const
93 {
94         return cells_[i];
95 }
96
97
98 void MathNestInset::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 MathNestInset::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 MathNestInset::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 MathNestInset::idxRight(LCursor & cur) const
160 {
161         return idxNext(cur);
162 }
163
164
165 bool MathNestInset::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 MathNestInset::idxLeft(LCursor & cur) const
177 {
178         return idxPrev(cur);
179 }
180
181
182 bool MathNestInset::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 MathNestInset::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 MathNestInset::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 MathNestInset::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 MathNestInset::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 << "MathNestInset::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 << "MathNestInset::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 MathNestInset::validate(LaTeXFeatures & features) const
272 {
273         for (idx_type i = 0; i < nargs(); ++i)
274                 cell(i).validate(features);
275 }
276
277
278 void MathNestInset::replace(ReplaceData & rep)
279 {
280         for (idx_type i = 0; i < nargs(); ++i)
281                 cell(i).replace(rep);
282 }
283
284
285 bool MathNestInset::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 MathNestInset::lock() const
295 {
296         return lock_;
297 }
298
299
300 void MathNestInset::lock(bool l)
301 {
302         lock_ = l;
303 }
304
305
306 bool MathNestInset::isActive() const
307 {
308         return nargs() > 0;
309 }
310
311
312 MathArray MathNestInset::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 MathNestInset::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 MathNestInset::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 MathNestInset::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 MathNestInset::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                 MathScriptInset * 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                 MathFontInset * p = operator[](i).nucleus()->asFontInset();
374                 MathFontInset 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 MathNestInset::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().asMathInset()->name() == font)
394                 cur.handleFont(font);
395         else {
396                 cur.handleNest(createMathInset(font));
397                 cur.insert(arg);
398         }
399 }
400
401
402 void MathNestInset::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 MathColorInset(true, font.color()));
410                 cur.handleNest(at, 0);
411         }
412 }
413
414
415 void MathNestInset::doDispatch(LCursor & cur, FuncRequest & cmd)
416 {
417         //lyxerr << "MathNestInset: request: " << cmd << std::endl;
418         //CursorSlice sl = cur.current();
419
420         switch (cmd.action) {
421
422         case LFUN_PASTE: {
423                 recordUndo(cur);
424                 // FIXME UNICODE
425                 cur.message(lyx::to_utf8(_("Paste")));
426                 replaceSelection(cur);
427                 size_t n = 0;
428                 istringstream is(lyx::to_utf8(cmd.argument()));
429                 is >> n;
430                 string const selection = lyx::cap::getSelection(cur.buffer(), n);
431                 cur.niceInsert(selection);
432                 cur.clearSelection(); // bug 393
433                 cur.bv().switchKeyMap();
434                 finishUndo();
435                 break;
436         }
437
438         case LFUN_CUT:
439                 recordUndo(cur);
440                 cutSelection(cur, true, true);
441                 // FIXME UNICODE
442                 cur.message(lyx::to_utf8(_("Cut")));
443                 // Prevent stale position >= size crash
444                 // Probably not necessary anymore, see eraseSelection (gb 2005-10-09)
445                 cur.normalize();
446                 break;
447
448         case LFUN_COPY:
449                 copySelection(cur);
450                 // FIXME UNICODE
451                 cur.message(lyx::to_utf8(_("Copy")));
452                 break;
453
454         case LFUN_MOUSE_PRESS:
455                 lfunMousePress(cur, cmd);
456                 break;
457
458         case LFUN_MOUSE_MOTION:
459                 lfunMouseMotion(cur, cmd);
460                 break;
461
462         case LFUN_MOUSE_RELEASE:
463                 lfunMouseRelease(cur, cmd);
464                 break;
465
466         case LFUN_FINISHED_LEFT:
467                 cur.bv().cursor() = cur;
468                 break;
469
470         case LFUN_FINISHED_RIGHT:
471                 ++cur.pos();
472                 cur.bv().cursor() = cur;
473                 break;
474
475         case LFUN_FINISHED_UP:
476                 cur.bv().cursor() = cur;
477                 break;
478
479         case LFUN_FINISHED_DOWN:
480                 ++cur.pos();
481                 cur.bv().cursor() = cur;
482                 break;
483
484         case LFUN_CHAR_FORWARD_SELECT:
485         case LFUN_CHAR_FORWARD:
486                 cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
487                 cur.autocorrect() = false;
488                 cur.clearTargetX();
489                 cur.macroModeClose();
490                 if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
491                         cur.pushLeft(*cur.nextAtom().nucleus());
492                         cur.inset().idxFirst(cur);
493                 } else if (cur.posRight() || idxRight(cur)
494                         || cur.popRight() || cur.selection())
495                         ;
496                 else {
497                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
498                         cur.undispatched();
499                 }
500                 break;
501
502         case LFUN_CHAR_BACKWARD_SELECT:
503         case LFUN_CHAR_BACKWARD:
504                 cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
505                 cur.autocorrect() = false;
506                 cur.clearTargetX();
507                 cur.macroModeClose();
508                 if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
509                         cur.posLeft();
510                         cur.push(*cur.nextAtom().nucleus());
511                         cur.inset().idxLast(cur);
512                 } else if (cur.posLeft() || idxLeft(cur)
513                         || cur.popLeft() || cur.selection())
514                         ;
515                 else {
516                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
517                         cur.undispatched();
518                 }
519                 break;
520
521         case LFUN_UP_SELECT:
522         case LFUN_UP:
523                 // FIXME Tried to use clearTargetX and macroModeClose, crashed on cur.up()
524                 if (cur.inMacroMode()) {
525                         // Make Helge happy
526                         cur.macroModeClose();
527                         break;
528                 }
529                 cur.selHandle(cmd.action == LFUN_UP_SELECT);
530                 if (!cur.up()) {
531                         cmd = FuncRequest(LFUN_FINISHED_UP);
532                         cur.undispatched();
533                 }
534                 // fixes bug 1598. Please check!
535                 cur.normalize();
536                 break;
537
538         case LFUN_DOWN_SELECT:
539         case LFUN_DOWN:
540                 if (cur.inMacroMode()) {
541                         cur.macroModeClose();
542                         break;
543                 }
544                 cur.selHandle(cmd.action == LFUN_DOWN_SELECT);
545                 if (!cur.down()) {
546                         cmd = FuncRequest(LFUN_FINISHED_DOWN);
547                         cur.undispatched();
548                 }
549                 // fixes bug 1598. Please check!
550                 cur.normalize();
551                 break;
552
553         case LFUN_MOUSE_DOUBLE:
554         case LFUN_MOUSE_TRIPLE:
555         case LFUN_WORD_SELECT:
556                 cur.pos() = 0;
557                 cur.idx() = 0;
558                 cur.resetAnchor();
559                 cur.selection() = true;
560                 cur.pos() = cur.lastpos();
561                 cur.idx() = cur.lastidx();
562                 break;
563
564         case LFUN_PARAGRAPH_UP_SELECT:
565         case LFUN_PARAGRAPH_UP:
566         case LFUN_PARAGRAPH_DOWN_SELECT:
567         case LFUN_PARAGRAPH_DOWN:
568                 break;
569
570         case LFUN_LINE_BEGIN_SELECT:
571         case LFUN_LINE_BEGIN:
572         case LFUN_WORD_BACKWARD_SELECT:
573         case LFUN_WORD_BACKWARD:
574                 cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
575                                 cmd.action == LFUN_LINE_BEGIN_SELECT);
576                 cur.macroModeClose();
577                 if (cur.pos() != 0) {
578                         cur.pos() = 0;
579                 } else if (cur.col() != 0) {
580                         cur.idx() -= cur.col();
581                         cur.pos() = 0;
582                 } else if (cur.idx() != 0) {
583                         cur.idx() = 0;
584                         cur.pos() = 0;
585                 } else {
586                         cmd = FuncRequest(LFUN_FINISHED_LEFT);
587                         cur.undispatched();
588                 }
589                 break;
590
591         case LFUN_WORD_FORWARD_SELECT:
592         case LFUN_WORD_FORWARD:
593         case LFUN_LINE_END_SELECT:
594         case LFUN_LINE_END:
595                 cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
596                                 cmd.action == LFUN_LINE_END_SELECT);
597                 cur.macroModeClose();
598                 cur.clearTargetX();
599                 if (cur.pos() != cur.lastpos()) {
600                         cur.pos() = cur.lastpos();
601                 } else if (ncols() && (cur.col() != cur.lastcol())) {
602                         cur.idx() = cur.idx() - cur.col() + cur.lastcol();
603                         cur.pos() = cur.lastpos();
604                 } else if (cur.idx() != cur.lastidx()) {
605                         cur.idx() = cur.lastidx();
606                         cur.pos() = cur.lastpos();
607                 } else {
608                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
609                         cur.undispatched();
610                 }
611                 break;
612
613         case LFUN_SCREEN_UP_SELECT:
614         case LFUN_SCREEN_UP:
615                 cmd = FuncRequest(LFUN_FINISHED_LEFT);
616                 cur.undispatched();
617                 break;
618
619         case LFUN_SCREEN_DOWN_SELECT:
620         case LFUN_SCREEN_DOWN:
621                 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
622                 cur.undispatched();
623                 break;
624
625         case LFUN_CELL_FORWARD:
626                 cur.inset().idxNext(cur);
627                 break;
628
629         case LFUN_CELL_BACKWARD:
630                 cur.inset().idxPrev(cur);
631                 break;
632
633         case LFUN_WORD_DELETE_BACKWARD:
634         case LFUN_CHAR_DELETE_BACKWARD:
635                 if (cur.pos() == 0)
636                         // May affect external cell:
637                         recordUndoInset(cur, Undo::ATOMIC);
638                 else
639                         recordUndo(cur, Undo::ATOMIC);
640                 cur.backspace();
641                 break;
642
643         case LFUN_WORD_DELETE_FORWARD:
644         case LFUN_CHAR_DELETE_FORWARD:
645                 if (cur.pos() == cur.lastpos())
646                         // May affect external cell:
647                         recordUndoInset(cur, Undo::ATOMIC);
648                 else
649                         recordUndo(cur, Undo::ATOMIC);
650                 cur.erase();
651                 break;
652
653         case LFUN_ESCAPE:
654                 if (cur.selection())
655                         cur.clearSelection();
656                 else  {
657                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
658                         cur.undispatched();
659                 }
660                 break;
661
662         case LFUN_INSET_TOGGLE:
663                 recordUndo(cur);
664                 lock(!lock());
665                 cur.popRight();
666                 break;
667
668         case LFUN_SELF_INSERT:
669                 if (cmd.argument().size() != 1) {
670                         recordUndo(cur);
671                         string const arg = lyx::to_utf8(cmd.argument());
672                         if (!interpret(cur, arg))
673                                 cur.insert(arg);
674                         break;
675                 }
676                 // Don't record undo steps if we are in macro mode and
677                 // cmd.argument is the next character of the macro name.
678                 // Otherwise we'll get an invalid cursor if we undo after
679                 // the macro was finished and the macro is a known command,
680                 // e.g. sqrt. LCursor::macroModeClose replaces in this case
681                 // the MathUnknownInset with name "frac" by an empty
682                 // MathFracInset -> a pos value > 0 is invalid.
683                 // A side effect is that an undo before the macro is finished
684                 // undoes the complete macro, not only the last character.
685                 if (!cur.inMacroMode())
686                         recordUndo(cur);
687
688                 // spacial handling of space. If we insert an inset
689                 // via macro mode, we want to put the cursor inside it
690                 // if relevant. Think typing "\frac<space>".
691                 if (cmd.argument()[0] == ' '
692                     && cur.inMacroMode() && cur.macroName() != "\\"
693                     && cur.macroModeClose()) {
694                         MathAtom const atom = cur.prevAtom();
695                         if (atom->asNestInset() && atom->nargs() > 0) {
696                                 cur.posLeft();
697                                 cur.pushLeft(*cur.nextInset());
698                         }
699                 // FIXME: Change to
700                 // } else if (!interpret(cur, cmd.argument()[0])) {
701                 // when interpret accepts UCS4 characters
702                 } else if (!interpret(cur, lyx::to_utf8(cmd.argument()))) {
703                         cmd = FuncRequest(LFUN_FINISHED_RIGHT);
704                         cur.undispatched();
705                 }
706                 break;
707
708         //case LFUN_SERVER_GET_XY:
709         //      sprintf(dispatch_buffer, "%d %d",);
710         //      break;
711
712         case LFUN_SERVER_SET_XY: {
713                 lyxerr << "LFUN_SERVER_SET_XY broken!" << endl;
714                 int x = 0;
715                 int y = 0;
716                 istringstream is(lyx::to_utf8(cmd.argument()));
717                 is >> x >> y;
718                 cur.setScreenPos(x, y);
719                 break;
720         }
721
722         // Special casing for superscript in case of LyX handling
723         // dead-keys:
724         case LFUN_ACCENT_CIRCUMFLEX:
725                 if (cmd.argument().empty()) {
726                         // do superscript if LyX handles
727                         // deadkeys
728                         recordUndo(cur, Undo::ATOMIC);
729                         script(cur, true, grabAndEraseSelection(cur));
730                 }
731                 break;
732
733         case LFUN_ACCENT_UMLAUT:
734         case LFUN_ACCENT_ACUTE:
735         case LFUN_ACCENT_GRAVE:
736         case LFUN_ACCENT_BREVE:
737         case LFUN_ACCENT_DOT:
738         case LFUN_ACCENT_MACRON:
739         case LFUN_ACCENT_CARON:
740         case LFUN_ACCENT_TILDE:
741         case LFUN_ACCENT_CEDILLA:
742         case LFUN_ACCENT_CIRCLE:
743         case LFUN_ACCENT_UNDERDOT:
744         case LFUN_ACCENT_TIE:
745         case LFUN_ACCENT_OGONEK:
746         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
747                 break;
748
749         //  Math fonts
750         case LFUN_FONT_FREE_APPLY:
751         case LFUN_FONT_FREE_UPDATE:
752                 handleFont2(cur, lyx::to_utf8(cmd.argument()));
753                 break;
754
755         case LFUN_FONT_BOLD:
756                 if (currentMode() == TEXT_MODE)
757                         handleFont(cur, lyx::to_utf8(cmd.argument()), "textbf");
758                 else
759                         handleFont(cur, lyx::to_utf8(cmd.argument()), "mathbf");
760                 break;
761         case LFUN_FONT_SANS:
762                 if (currentMode() == TEXT_MODE)
763                         handleFont(cur, lyx::to_utf8(cmd.argument()), "textsf");
764                 else
765                         handleFont(cur, lyx::to_utf8(cmd.argument()), "mathsf");
766                 break;
767         case LFUN_FONT_EMPH:
768                 if (currentMode() == TEXT_MODE)
769                         handleFont(cur, lyx::to_utf8(cmd.argument()), "emph");
770                 else
771                         handleFont(cur, lyx::to_utf8(cmd.argument()), "mathcal");
772                 break;
773         case LFUN_FONT_ROMAN:
774                 if (currentMode() == TEXT_MODE)
775                         handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
776                 else
777                         handleFont(cur, lyx::to_utf8(cmd.argument()), "mathrm");
778                 break;
779         case LFUN_FONT_CODE:
780                 if (currentMode() == TEXT_MODE)
781                         handleFont(cur, lyx::to_utf8(cmd.argument()), "texttt");
782                 else
783                         handleFont(cur, lyx::to_utf8(cmd.argument()), "mathtt");
784                 break;
785         case LFUN_FONT_FRAK:
786                 handleFont(cur, lyx::to_utf8(cmd.argument()), "mathfrak");
787                 break;
788         case LFUN_FONT_ITAL:
789                 if (currentMode() == TEXT_MODE)
790                         handleFont(cur, lyx::to_utf8(cmd.argument()), "textit");
791                 else
792                         handleFont(cur, lyx::to_utf8(cmd.argument()), "mathit");
793                 break;
794         case LFUN_FONT_NOUN:
795                 if (currentMode() == TEXT_MODE)
796                         // FIXME: should be "noun"
797                         handleFont(cur, lyx::to_utf8(cmd.argument()), "textsc");
798                 else
799                         handleFont(cur, lyx::to_utf8(cmd.argument()), "mathbb");
800                 break;
801         //case LFUN_FONT_FREE_APPLY:
802                 handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
803                 break;
804         case LFUN_FONT_DEFAULT:
805                 handleFont(cur, lyx::to_utf8(cmd.argument()), "textnormal");
806                 break;
807
808         case LFUN_MATH_MODE: {
809 #if 1
810                 // ignore math-mode on when already in math mode
811                 if (currentMode() == InsetBase::MATH_MODE && cmd.argument() == "on")
812                         break;
813                 cur.macroModeClose();
814                 string const save_selection = grabAndEraseSelection(cur);
815                 selClearOrDel(cur);
816                 //cur.plainInsert(MathAtom(new MathMBoxInset(cur.bv())));
817                 cur.plainInsert(MathAtom(new MathBoxInset("mbox")));
818                 cur.posLeft();
819                 cur.pushLeft(*cur.nextInset());
820                 cur.niceInsert(save_selection);
821 #else
822                 if (currentMode() == InsetBase::TEXT_MODE) {
823                         cur.niceInsert(MathAtom(new MathHullInset("simple")));
824                         cur.message(_("create new math text environment ($...$)"));
825                 } else {
826                         handleFont(cur, lyx::to_utf8(cmd.argument()), "textrm");
827                         cur.message(_("entered math text mode (textrm)"));
828                 }
829 #endif
830                 break;
831         }
832
833         case LFUN_MATH_SIZE:
834 #if 0
835                 recordUndo(cur);
836                 cur.setSize(arg);
837 #endif
838                 break;
839
840         case LFUN_MATH_MATRIX: {
841                 recordUndo(cur, Undo::ATOMIC);
842                 unsigned int m = 1;
843                 unsigned int n = 1;
844                 string v_align;
845                 string h_align;
846                 istringstream is(lyx::to_utf8(cmd.argument()));
847                 is >> m >> n >> v_align >> h_align;
848                 if (m < 1)
849                         m = 1;
850                 if (n < 1)
851                         n = 1;
852                 v_align += 'c';
853                 cur.niceInsert(
854                         MathAtom(new MathArrayInset("array", m, n, v_align[0], h_align)));
855                 break;
856         }
857
858         case LFUN_MATH_DELIM: {
859                 string ls;
860                 string rs = lyx::support::split(lyx::to_utf8(cmd.argument()), ls, ' ');
861                 // Reasonable default values
862                 if (ls.empty())
863                         ls = '(';
864                 if (rs.empty())
865                         rs = ')';
866                 recordUndo(cur, Undo::ATOMIC);
867                 cur.handleNest(MathAtom(new MathDelimInset(ls, rs)));
868                 break;
869         }
870
871         case LFUN_MATH_BIGDELIM: {
872                 string const lname = cmd.getArg(0);
873                 string const ldelim = cmd.getArg(1);
874                 string const rname = cmd.getArg(2);
875                 string const rdelim = cmd.getArg(3);
876                 latexkeys const * l = in_word_set(lname);
877                 bool const have_l = l && l->inset == "big" &&
878                                     MathBigInset::isBigInsetDelim(ldelim);
879                 l = in_word_set(rname);
880                 bool const have_r = l && l->inset == "big" &&
881                                     MathBigInset::isBigInsetDelim(rdelim);
882                 // We mimic LFUN_MATH_DELIM in case we have an empty left
883                 // or right delimiter.
884                 if (have_l || have_r) {
885                         recordUndo(cur, Undo::ATOMIC);
886                         string const selection = grabAndEraseSelection(cur);
887                         selClearOrDel(cur);
888                         if (have_l)
889                                 cur.insert(MathAtom(new MathBigInset(lname,
890                                                                 ldelim)));
891                         cur.niceInsert(selection);
892                         if (have_r)
893                                 cur.insert(MathAtom(new MathBigInset(rname,
894                                                                 rdelim)));
895                 }
896                 // Don't call cur.undispatched() if we did nothing, this would
897                 // lead to infinite recursion via LyXText::dispatch().
898                 break;
899         }
900
901         case LFUN_SPACE_INSERT:
902         case LFUN_MATH_SPACE:
903                 recordUndo(cur, Undo::ATOMIC);
904                 cur.insert(MathAtom(new MathSpaceInset(",")));
905                 break;
906
907         case LFUN_ERT_INSERT:
908                 // interpret this as if a backslash was typed
909                 recordUndo(cur, Undo::ATOMIC);
910                 interpret(cur, '\\');
911                 break;
912
913         case LFUN_MATH_SUBSCRIPT:
914                 // interpret this as if a _ was typed
915                 recordUndo(cur, Undo::ATOMIC);
916                 interpret(cur, '_');
917                 break;
918
919         case LFUN_MATH_SUPERSCRIPT:
920                 // interpret this as if a ^ was typed
921                 recordUndo(cur, Undo::ATOMIC);
922                 interpret(cur, '^');
923                 break;
924
925 // FIXME: We probably should swap parts of "math-insert" and "self-insert"
926 // handling such that "self-insert" works on "arbitrary stuff" too, and
927 // math-insert only handles special math things like "matrix".
928         case LFUN_MATH_INSERT: {
929                 recordUndo(cur, Undo::ATOMIC);
930                 if (cmd.argument() == "^" || cmd.argument() == "_")
931                         interpret(cur, cmd.argument()[0]);
932                 else
933                         cur.niceInsert(lyx::to_utf8(cmd.argument()));
934                 break;
935                 }
936
937         case LFUN_DIALOG_SHOW_NEW_INSET: {
938                 string const & name = lyx::to_utf8(cmd.argument());
939                 string data;
940                 if (name == "ref") {
941                         RefInset tmp(name);
942                         data = tmp.createDialogStr(name);
943                 }
944                 cur.bv().owner()->getDialogs().show(name, data, 0);
945                 break;
946         }
947
948         default:
949                 MathDimInset::doDispatch(cur, cmd);
950                 break;
951         }
952 }
953
954
955 bool MathNestInset::getStatus(LCursor & cur, FuncRequest const & cmd,
956                 FuncStatus & flag) const
957 {
958         // the font related toggles
959         //string tc = "mathnormal";
960         bool ret = true;
961         string const arg = lyx::to_utf8(cmd.argument());
962         switch (cmd.action) {
963         case LFUN_TABULAR_FEATURE:
964                 flag.enabled(false);
965                 break;
966 #if 0
967         case LFUN_TABULAR_FEATURE:
968                 // FIXME: check temporarily disabled
969                 // valign code
970                 char align = mathcursor::valign();
971                 if (align == '\0') {
972                         enable = false;
973                         break;
974                 }
975                 if (cmd.argument().empty()) {
976                         flag.clear();
977                         break;
978                 }
979                 if (!contains("tcb", cmd.argument()[0])) {
980                         enable = false;
981                         break;
982                 }
983                 flag.setOnOff(cmd.argument()[0] == align);
984                 break;
985 #endif
986         /// We have to handle them since 1.4 blocks all unhandled actions
987         case LFUN_FONT_ITAL:
988         case LFUN_FONT_BOLD:
989         case LFUN_FONT_SANS:
990         case LFUN_FONT_EMPH:
991         case LFUN_FONT_CODE:
992         case LFUN_FONT_NOUN:
993         case LFUN_FONT_ROMAN:
994         case LFUN_FONT_DEFAULT:
995                 flag.enabled(true);
996                 break;
997         case LFUN_MATH_MUTATE:
998                 //flag.setOnOff(mathcursor::formula()->hullType() == lyx::to_utf8(cmd.argument()));
999                 flag.setOnOff(false);
1000                 break;
1001
1002         // we just need to be in math mode to enable that
1003         case LFUN_MATH_SIZE:
1004         case LFUN_MATH_SPACE:
1005         case LFUN_MATH_LIMITS:
1006         case LFUN_MATH_NONUMBER:
1007         case LFUN_MATH_NUMBER:
1008         case LFUN_MATH_EXTERN:
1009                 flag.enabled(true);
1010                 break;
1011
1012         case LFUN_FONT_FRAK:
1013                 flag.enabled(currentMode() != TEXT_MODE);
1014                 break;
1015
1016         case LFUN_MATH_INSERT: {
1017                 bool const textarg =
1018                         arg == "\\textbf"   || arg == "\\textsf" ||
1019                         arg == "\\textrm"   || arg == "\\textmd" ||
1020                         arg == "\\textit"   || arg == "\\textsc" ||
1021                         arg == "\\textsl"   || arg == "\\textup" ||
1022                         arg == "\\texttt"   || arg == "\\textbb" ||
1023                         arg == "\\textnormal";
1024                 flag.enabled(currentMode() != TEXT_MODE || textarg);
1025                 break;
1026         }
1027
1028         case LFUN_MATH_MATRIX:
1029                 flag.enabled(currentMode() == MATH_MODE);
1030                 break;
1031
1032         case LFUN_MATH_DELIM:
1033         case LFUN_MATH_BIGDELIM:
1034                 // Don't do this with multi-cell selections
1035                 flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
1036                 break;
1037
1038         default:
1039                 ret = false;
1040                 break;
1041         }
1042         return ret;
1043 }
1044
1045
1046 void MathNestInset::edit(LCursor & cur, bool left)
1047 {
1048         cur.push(*this);
1049         cur.idx() = left ? 0 : cur.lastidx();
1050         cur.pos() = left ? 0 : cur.lastpos();
1051         cur.resetAnchor();
1052         //lyxerr << "MathNestInset::edit, cur:\n" << cur << endl;
1053 }
1054
1055
1056 InsetBase * MathNestInset::editXY(LCursor & cur, int x, int y)
1057 {
1058         int idx_min = 0;
1059         int dist_min = 1000000;
1060         for (idx_type i = 0, n = nargs(); i != n; ++i) {
1061                 int const d = cell(i).dist(x, y);
1062                 if (d < dist_min) {
1063                         dist_min = d;
1064                         idx_min = i;
1065                 }
1066         }
1067         MathArray & ar = cell(idx_min);
1068         cur.push(*this);
1069         cur.idx() = idx_min;
1070         cur.pos() = ar.x2pos(x - ar.xo());
1071         //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
1072         if (dist_min == 0) {
1073                 // hit inside cell
1074                 for (pos_type i = 0, n = ar.size(); i < n; ++i)
1075                         if (ar[i]->covers(x, y))
1076                                 return ar[i].nucleus()->editXY(cur, x, y);
1077         }
1078         return this;
1079 }
1080
1081
1082 void MathNestInset::lfunMousePress(LCursor & cur, FuncRequest & cmd)
1083 {
1084         //lyxerr << "## lfunMousePress: buttons: " << cmd.button() << endl;
1085         BufferView & bv = cur.bv();
1086         if (cmd.button() == mouse_button::button1) {
1087                 //lyxerr << "## lfunMousePress: setting cursor to: " << cur << endl;
1088                 bv.mouseSetCursor(cur);
1089         } else if (cmd.button() == mouse_button::button2) {
1090                 MathArray ar;
1091                 if (cur.selection())
1092                         asArray(lyx::to_utf8(bv.cursor().selectionAsString(false)), ar);
1093                 else
1094                         asArray(lyx::to_utf8(bv.owner()->gui().selection().get()), ar);
1095
1096                 cur.insert(ar);
1097                 bv.mouseSetCursor(cur);
1098         }
1099 }
1100
1101
1102 void MathNestInset::lfunMouseMotion(LCursor & cur, FuncRequest & cmd)
1103 {
1104         // only select with button 1
1105         if (cmd.button() == mouse_button::button1) {
1106                 LCursor & bvcur = cur.bv().cursor();
1107                 if (bvcur.anchor_.hasPart(cur)) {
1108                         //lyxerr << "## lfunMouseMotion: cursor: " << cur << endl;
1109                         bvcur.setCursor(cur);
1110                         bvcur.selection() = true;
1111                         //lyxerr << "MOTION " << bvcur << endl;
1112                 }
1113                 else {
1114                         cur.undispatched();
1115                 }
1116         }
1117 }
1118
1119
1120 void MathNestInset::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
1121 {
1122         //lyxerr << "## lfunMouseRelease: buttons: " << cmd.button() << endl;
1123
1124         if (cmd.button() == mouse_button::button1) {
1125                 //cur.bv().owner()->gui().selection().put(cur.grabSelection());
1126                 return;
1127         }
1128
1129         if (cmd.button() == mouse_button::button3) {
1130                 // try to dispatch to enclosed insets first
1131                 cur.bv().owner()->getDialogs().show("mathpanel");
1132                 return;
1133         }
1134
1135         cur.undispatched();
1136 }
1137
1138
1139 bool MathNestInset::interpret(LCursor & cur, char c)
1140 {
1141         //lyxerr << "interpret 2: '" << c << "'" << endl;
1142         string save_selection;
1143         if (c == '^' || c == '_')
1144                 save_selection = grabAndEraseSelection(cur);
1145
1146         cur.clearTargetX();
1147
1148         // handle macroMode
1149         if (cur.inMacroMode()) {
1150                 string name = cur.macroName();
1151
1152                 /// are we currently typing '#1' or '#2' or...?
1153                 if (name == "\\#") {
1154                         cur.backspace();
1155                         int n = c - '0';
1156                         if (n >= 1 && n <= 9)
1157                                 cur.insert(new MathMacroArgument(n));
1158                         return true;
1159                 }
1160
1161                 if (isalpha(c)) {
1162                         cur.activeMacro()->setName(name + c);
1163                         return true;
1164                 }
1165
1166                 // handle 'special char' macros
1167                 if (name == "\\") {
1168                         // remove the '\\'
1169                         if (c == '\\') {
1170                                 cur.backspace();
1171                                 if (currentMode() == MathInset::TEXT_MODE)
1172                                         cur.niceInsert(createMathInset("textbackslash"));
1173                                 else
1174                                         cur.niceInsert(createMathInset("backslash"));
1175                         } else if (c == '{') {
1176                                 cur.backspace();
1177                                 cur.niceInsert(MathAtom(new MathBraceInset));
1178                         } else if (c == '%') {
1179                                 cur.backspace();
1180                                 cur.niceInsert(MathAtom(new MathCommentInset));
1181                         } else if (c == '#') {
1182                                 BOOST_ASSERT(cur.activeMacro());
1183                                 cur.activeMacro()->setName(name + c);
1184                         } else {
1185                                 cur.backspace();
1186                                 cur.niceInsert(createMathInset(string(1, c)));
1187                         }
1188                         return true;
1189                 }
1190
1191                 // One character big delimiters. The others are handled in
1192                 // the other interpret() method.
1193                 latexkeys const * l = in_word_set(name.substr(1));
1194                 if (name[0] == '\\' && l && l->inset == "big") {
1195                         string delim;
1196                         switch (c) {
1197                         case '{':
1198                                 delim = "\\{";
1199                                 break;
1200                         case '}':
1201                                 delim = "\\}";
1202                                 break;
1203                         default:
1204                                 delim = string(1, c);
1205                                 break;
1206                         }
1207                         if (MathBigInset::isBigInsetDelim(delim)) {
1208                                 // name + delim ared a valid MathBigInset.
1209                                 // We can't use cur.macroModeClose() because
1210                                 // it does not handle delim.
1211                                 MathUnknownInset * p = cur.activeMacro();
1212                                 p->finalize();
1213                                 --cur.pos();
1214                                 cur.cell().erase(cur.pos());
1215                                 cur.plainInsert(MathAtom(
1216                                         new MathBigInset(name.substr(1), delim)));
1217                                 return true;
1218                         }
1219                 }
1220
1221                 // leave macro mode and try again if necessary
1222                 cur.macroModeClose();
1223                 if (c == '{')
1224                         cur.niceInsert(MathAtom(new MathBraceInset));
1225                 else if (c != ' ')
1226                         interpret(cur, c);
1227                 return true;
1228         }
1229
1230         // This is annoying as one has to press <space> far too often.
1231         // Disable it.
1232
1233 #if 0
1234                 // leave autocorrect mode if necessary
1235                 if (autocorrect() && c == ' ') {
1236                         autocorrect() = false;
1237                         return true;
1238                 }
1239 #endif
1240
1241         // just clear selection on pressing the space bar
1242         if (cur.selection() && c == ' ') {
1243                 cur.selection() = false;
1244                 return true;
1245         }
1246
1247         selClearOrDel(cur);
1248
1249         if (c == '\\') {
1250                 //lyxerr << "starting with macro" << endl;
1251                 cur.insert(MathAtom(new MathUnknownInset("\\", false)));
1252                 return true;
1253         }
1254
1255         if (c == '\n') {
1256                 if (currentMode() == MathInset::TEXT_MODE)
1257                         cur.insert(c);
1258                 return true;
1259         }
1260
1261         if (c == ' ') {
1262                 if (currentMode() == MathInset::TEXT_MODE) {
1263                         // insert spaces in text mode,
1264                         // but suppress direct insertion of two spaces in a row
1265                         // the still allows typing  '<space>a<space>' and deleting the 'a', but
1266                         // it is better than nothing...
1267                         if (!cur.pos() != 0 || cur.prevAtom()->getChar() != ' ')
1268                                 cur.insert(c);
1269                         return true;
1270                 }
1271                 if (cur.pos() != 0 && cur.prevAtom()->asSpaceInset()) {
1272                         cur.prevAtom().nucleus()->asSpaceInset()->incSpace();
1273                         return true;
1274                 }
1275                 if (cur.popRight())
1276                         return true;
1277                 // if are at the very end, leave the formula
1278                 return cur.pos() != cur.lastpos();
1279         }
1280
1281         // These shouldn't work in text mode:
1282         if (currentMode() != MathInset::TEXT_MODE) {
1283                 if (c == '_') {
1284                         script(cur, false, save_selection);
1285                         return true;
1286                 }
1287                 if (c == '^') {
1288                         script(cur, true, save_selection);
1289                         return true;
1290                 }
1291                 if (c == '~') {
1292                         cur.niceInsert(createMathInset("sim"));
1293                         return true;
1294                 }
1295         }
1296
1297         if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' ||
1298             c == '%' || c == '_' || c == '^') {
1299                 cur.niceInsert(createMathInset(string(1, c)));
1300                 return true;
1301         }
1302
1303
1304         // try auto-correction
1305         //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1306         //      return true;
1307
1308         // no special circumstances, so insert the character without any fuss
1309         cur.insert(c);
1310         cur.autocorrect() = true;
1311         return true;
1312 }
1313
1314
1315 bool MathNestInset::interpret(LCursor & cur, string const & str)
1316 {
1317         // Create a MathBigInset from cur.cell()[cur.pos() - 1] and t if
1318         // possible
1319         if (!cur.empty() && cur.pos() > 0 &&
1320             cur.cell()[cur.pos() - 1]->asUnknownInset()) {
1321                 if (MathBigInset::isBigInsetDelim(str)) {
1322                         string prev = asString(cur.cell()[cur.pos() - 1]);
1323                         if (prev[0] == '\\') {
1324                                 prev = prev.substr(1);
1325                                 latexkeys const * l = in_word_set(prev);
1326                                 if (l && l->inset == "big") {
1327                                         cur.cell()[cur.pos() - 1] =
1328                                                 MathAtom(new MathBigInset(prev, str));
1329                                         return true;
1330                                 }
1331                         }
1332                 }
1333         }
1334         return false;
1335 }
1336
1337
1338 bool MathNestInset::script(LCursor & cur, bool up, string const &
1339                 save_selection)
1340 {
1341         // Hack to get \^ and \_ working
1342         //lyxerr << "handling script: up: " << up << endl;
1343         if (cur.inMacroMode() && cur.macroName() == "\\") {
1344                 if (up)
1345                         cur.niceInsert(createMathInset("mathcircumflex"));
1346                 else
1347                         interpret(cur, '_');
1348                 return true;
1349         }
1350
1351         cur.macroModeClose();
1352         if (asScriptInset() && cur.idx() == 0) {
1353                 // we are in a nucleus of a script inset, move to _our_ script
1354                 MathScriptInset * inset = asScriptInset();
1355                 //lyxerr << " going to cell " << inset->idxOfScript(up) << endl;
1356                 inset->ensure(up);
1357                 cur.idx() = inset->idxOfScript(up);
1358                 cur.pos() = 0;
1359         } else if (cur.pos() != 0 && cur.prevAtom()->asScriptInset()) {
1360                 --cur.pos();
1361                 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1362                 cur.push(*inset);
1363                 inset->ensure(up);
1364                 cur.idx() = inset->idxOfScript(up);
1365                 cur.pos() = cur.lastpos();
1366         } else {
1367                 // convert the thing to our left to a scriptinset or create a new
1368                 // one if in the very first position of the array
1369                 if (cur.pos() == 0) {
1370                         //lyxerr << "new scriptinset" << endl;
1371                         cur.insert(new MathScriptInset(up));
1372                 } else {
1373                         //lyxerr << "converting prev atom " << endl;
1374                         cur.prevAtom() = MathAtom(new MathScriptInset(cur.prevAtom(), up));
1375                 }
1376                 --cur.pos();
1377                 MathScriptInset * inset = cur.nextAtom().nucleus()->asScriptInset();
1378                 // special handling of {}-bases
1379                 // is this always correct?
1380                 if (inset->nuc().size() == 1
1381                     && inset->nuc().back()->asBraceInset())
1382                         inset->nuc() = inset->nuc().back()->asNestInset()->cell(0);
1383
1384                 cur.push(*inset);
1385                 cur.idx() = 1;
1386                 cur.pos() = 0;
1387         }
1388         //lyxerr << "inserting selection 1:\n" << save_selection << endl;
1389         cur.niceInsert(save_selection);
1390         cur.resetAnchor();
1391         //lyxerr << "inserting selection 2:\n" << save_selection << endl;
1392         return true;
1393 }