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