]> git.lyx.org Git - features.git/blob - src/cursor.C
more IU
[features.git] / src / cursor.C
1 /**
2  * \file cursor.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author Alfredo Braunstein
8  * \author André Pönitz
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "buffer.h"
16 #include "BufferView.h"
17 #include "cursor.h"
18 #include "debug.h"
19 #include "dispatchresult.h"
20 #include "funcrequest.h"
21 #include "iterators.h"
22 #include "lfuns.h"
23 #include "lyxrc.h"
24 #include "lyxtext.h"
25 #include "lyxrow.h"
26 #include "paragraph.h"
27
28 #include "insets/updatableinset.h"
29 #include "insets/insettabular.h"
30 #include "insets/insettext.h"
31
32 #include "mathed/math_data.h"
33 #include "mathed/math_hullinset.h"
34 #include "mathed/math_support.h"
35
36 #include "support/limited_stack.h"
37
38 #include <boost/assert.hpp>
39
40 using std::string;
41 using std::vector;
42 using std::endl;
43 #ifndef CXX_GLOBAL_CSTD
44 using std::isalpha;
45 #endif
46 using std::min;
47 using std::swap;
48
49
50
51 // our own cut buffer
52 limited_stack<string> theCutBuffer;
53
54
55 LCursor::LCursor(BufferView & bv)
56         : cursor_(1), anchor_(1), bv_(&bv), current_(0),
57           cached_y_(0), x_target_(-1),
58           selection_(false), mark_(false)
59 {}
60
61
62 void LCursor::reset()
63 {
64         cursor_.clear();
65         anchor_.clear();
66         cursor_.push_back(CursorSlice());
67         anchor_.push_back(CursorSlice());
68         current_ = 0;
69         cached_y_ = 0;
70         clearTargetX();
71         selection_ = false;
72         mark_ = false;
73 }
74
75
76 DispatchResult LCursor::dispatch(FuncRequest const & cmd0)
77 {
78         //lyxerr << "\nLCursor::dispatch: cmd: " << cmd0 << endl << *this << endl;
79         FuncRequest cmd = cmd0;
80
81         for (int i = cursor_.size() - 1; i >= 1; --i) {
82                 current_ = i;
83                 CursorSlice const & citem = cursor_[i];
84                 lyxerr << "trying to dispatch to inset " << citem.inset_ << endl;
85                 DispatchResult res = inset()->dispatch(*this, cmd);
86                 if (res.dispatched()) {
87                         lyxerr << " successfully dispatched to inset " << citem.inset_ << endl;
88                         return DispatchResult(true, true);
89                 }
90                 // remove one level of cursor
91                 switch (res.val()) {
92                         case FINISHED:
93                                 pop(i);
94                                 cmd = FuncRequest(LFUN_FINISHED_LEFT);
95                                 break;
96                         case FINISHED_RIGHT:
97                                 pop(i);
98                                 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
99                                 break;
100                         case FINISHED_UP:
101                                 pop(i);
102                                 cmd = FuncRequest(LFUN_FINISHED_UP);
103                                 break;
104                         case FINISHED_DOWN:
105                                 pop(i);
106                                 cmd = FuncRequest(LFUN_FINISHED_DOWN);
107                                 break;
108                         default:
109                                 lyxerr << "not handled on level " << i << " val: " << res.val() << endl;
110                                 break;
111                 }
112         }
113         lyxerr << "trying to dispatch to main text " << bv_->text() << endl;
114         DispatchResult res = bv_->text()->dispatch(*this, cmd);
115         lyxerr << "   result: " << res.val() << endl;
116         return res;
117 }
118
119
120 void LCursor::push(InsetBase * inset)
121 {
122         lyxerr << "LCursor::push()  inset: " << inset << endl;
123         cursor_.push_back(CursorSlice(inset));
124         anchor_.push_back(CursorSlice(inset));
125         ++current_;
126         updatePos();
127 }
128
129
130 void LCursor::pop(int depth)
131 {
132         //lyxerr << "LCursor::pop() to depth " << depth << endl;
133         while (int(cursor_.size()) > depth)
134                 pop();
135 }
136
137
138 void LCursor::pop()
139 {
140         BOOST_ASSERT(!cursor_.empty());
141         //lyxerr << "LCursor::pop() a level" << endl;
142         if (cursor_.size() <= 1)
143                 lyxerr << "### TRYING TO POP FROM EMPTY CURSOR" << endl;
144         else {
145                 cursor_.pop_back();
146                 anchor_.pop_back();
147                 current_ = cursor_.size() - 1;
148         }
149         //lyxerr << "LCursor::pop() current now: " << current_ << endl;
150 }
151
152
153 void LCursor::pushLeft(InsetBase * p)
154 {
155         BOOST_ASSERT(!cursor_.empty());
156         //lyxerr << "Entering inset " << t << " left" << endl;
157         push(p);
158         p->idxFirst(*this);
159 }
160
161
162 bool LCursor::popLeft()
163 {
164         BOOST_ASSERT(!cursor_.empty());
165         //lyxerr << "Leaving inset to the left" << endl;
166         if (depth() <= 1) {
167                 if (depth() == 1)
168                         inset()->notifyCursorLeaves(idx());
169                 return false;
170         }
171         inset()->notifyCursorLeaves(idx());
172         pop();
173         return true;
174 }
175
176
177 bool LCursor::popRight()
178 {
179         BOOST_ASSERT(!cursor_.empty());
180         //lyxerr << "Leaving inset to the right" << endl;
181         if (depth() <= 1) {
182                 if (depth() == 1)
183                         inset()->notifyCursorLeaves(idx());
184                 return false;
185         }
186         inset()->notifyCursorLeaves(idx());
187         pop();
188         posRight();
189         return true;
190 }
191
192
193 CursorSlice & LCursor::current()
194 {
195         BOOST_ASSERT(!cursor_.empty());
196         //lyxerr << "accessing cursor slice " << current_
197         //      << ": " << cursor_[current_] << endl;
198         return cursor_[current_];
199 }
200
201
202 CursorSlice const & LCursor::current() const
203 {
204         //lyxerr << "accessing cursor slice " << current_
205         //      << ": " << cursor_[current_] << endl;
206         return cursor_[current_];
207 }
208
209
210 int LCursor::currentMode()
211 {
212         BOOST_ASSERT(!cursor_.empty());
213         for (int i = cursor_.size() - 1; i >= 1; --i) {
214                 int res = cursor_[i].inset()->currentMode();
215                 if (res != MathInset::UNDECIDED_MODE)
216                         return res;
217         }
218         return MathInset::TEXT_MODE;
219 }
220
221
222 LyXText * LCursor::innerText() const
223 {
224         BOOST_ASSERT(!cursor_.empty());
225         //lyxerr << "LCursor::innerText()  depth: " << cursor_.size() << endl;
226         if (cursor_.size() > 1) {
227                 // go up until first non-0 text is hit
228                 // (innermost text is 0 in mathed)
229                 for (int i = cursor_.size() - 1; i >= 1; --i)
230                         if (cursor_[i].text())
231                                 return cursor_[i].text();
232         }
233         return bv_->text();
234 }
235
236
237 CursorSlice const & LCursor::innerTextSlice() const
238 {
239         BOOST_ASSERT(!cursor_.empty());
240         //lyxerr << "LCursor::innerTextSlice()  depth: " << cursor_.size() << endl;
241         if (cursor_.size() > 1) {
242                 // go up until first non-0 text is hit
243                 // (innermost text is 0 in mathed)
244                 for (int i = cursor_.size() - 1; i >= 1; --i)
245                         if (cursor_[i].text())
246                                 return cursor_[i];
247         }
248         return cursor_[0];
249 }
250
251
252 void LCursor::updatePos()
253 {
254         BOOST_ASSERT(!cursor_.empty());
255         if (cursor_.size() > 1)
256                 cached_y_ = bv_->top_y() + cursor_.back().inset()->yo();
257                 //cached_y_ = cursor_.back().inset()->yo();
258 }
259
260
261 void LCursor::getDim(int & asc, int & des) const
262 {
263         BOOST_ASSERT(!cursor_.empty());
264         LyXText * text = innerText();
265 #warning crashes with text-in-math
266         if (0 && text) {
267                 RowList::iterator const rit = text->cursorRow();
268                 if (rit != text->endRow()) {
269                         asc = rit->baseline();
270                         des = rit->height() - asc;
271                 } else {
272                         asc = 10;
273                         des = 10;
274                 }
275         } else {
276                 asc = 10;
277                 des = 10;
278                 //innerInset()->getCursorDim(asc, des);
279         }
280 }
281
282
283 void LCursor::getPos(int & x, int & y) const
284 {
285         BOOST_ASSERT(!cursor_.empty());
286         x = 0;
287         y = 0;
288         if (cursor_.size() <= 1) {
289                 x = bv_->text()->cursorX(cursor_.front());
290                 y = bv_->text()->cursorY(cursor_.front());
291         } else {
292                 inset()->getCursorPos(cursor_.back(), x, y);
293                 // getCursorPos gives _screen_ coordinates. We need to add
294                 // top_y to get document coordinates. This is hidden in cached_y_.
295                 //y += cached_y_ - inset()->yo();
296                 // The rest is non-obvious. The reason we have to have these
297                 // extra computation is that the getCursorPos() calls rely
298                 // on the inset's own knowledge of its screen position.
299                 // If we scroll up or down in a big enough increment,
300                 // inset->draw() is not called: this doesn't update
301                 // inset.yo_, so getCursor() returns an old value.
302                 // Ugly as you like.
303         }
304         lyxerr << "#### LCursor::getPos: x: " << x << " y: " << y << endl;
305 }
306
307
308 void LCursor::paste(string const & data)
309 {
310         dispatch(FuncRequest(LFUN_PASTE, data));
311 }
312
313
314 InsetBase * LCursor::innerInsetOfType(int code) const
315 {
316         for (int i = cursor_.size() - 1; i >= 1; --i)
317                 if (cursor_[i].inset_->lyxCode() == code)
318                         return cursor_[i].inset_;
319         return 0;
320 }
321
322
323 InsetTabular * LCursor::innerInsetTabular() const
324 {
325         return static_cast<InsetTabular *>(innerInsetOfType(InsetBase::TABULAR_CODE));
326 }
327
328
329 void LCursor::resetAnchor()
330 {
331         anchor_ = cursor_;
332 }
333
334
335 BufferView & LCursor::bv() const
336 {
337         return *bv_;
338 }
339
340
341 MathAtom const & LCursor::prevAtom() const
342 {
343         BOOST_ASSERT(pos() > 0);
344         return cell()[pos() - 1];
345 }
346
347
348 MathAtom & LCursor::prevAtom()
349 {
350         BOOST_ASSERT(pos() > 0);
351         return cell()[pos() - 1];
352 }
353
354
355 MathAtom const & LCursor::nextAtom() const
356 {
357         BOOST_ASSERT(pos() < lastpos());
358         return cell()[pos()];
359 }
360
361
362 MathAtom & LCursor::nextAtom()
363 {
364         BOOST_ASSERT(pos() < lastpos());
365         return cell()[pos()];
366 }
367
368
369 bool LCursor::posLeft()
370 {
371         if (pos() == 0)
372                 return false;
373         --pos();
374         return true;
375 }
376
377
378 bool LCursor::posRight()
379 {
380         if (pos() == lastpos())
381                 return false;
382         ++pos();
383         return true;
384 }
385
386
387 CursorSlice & LCursor::anchor()
388 {
389         return anchor_.back();
390 }
391
392
393 CursorSlice const & LCursor::anchor() const
394 {
395         return anchor_.back();
396 }
397
398
399 CursorSlice const & LCursor::selBegin() const
400 {
401         if (!selection())
402                 return cursor_.back();
403         // can't use std::min as this creates a new object
404         return anchor() < cursor_.back() ? anchor() : cursor_.back();
405 }
406
407
408 CursorSlice & LCursor::selBegin()
409 {
410         if (!selection())
411                 return cursor_.back();
412         return anchor() < cursor_.back() ? anchor() : cursor_.back();
413 }
414
415
416 CursorSlice const & LCursor::selEnd() const
417 {
418         if (!selection())
419                 return cursor_.back();
420         return anchor() > cursor_.back() ? anchor() : cursor_.back();
421 }
422
423
424 CursorSlice & LCursor::selEnd()
425 {
426         if (selection())
427                 return cursor_.back();
428         return anchor() > cursor_.back() ? anchor() : cursor_.back();
429 }
430
431
432 void LCursor::setSelection()
433 {
434         selection() = true;
435         // a selection with no contents is not a selection
436         if (par() == anchor().par() && pos() == anchor().pos())
437                 selection() = false;
438 }
439
440
441 void LCursor::setSelection(CursorBase const & where, size_t n)
442 {
443         selection() = true;
444         cursor_ = where;
445         anchor_ = where;
446         pos() += n;
447 }
448
449
450 void LCursor::clearSelection()
451 {
452         selection() = false;
453         mark() = false;
454         resetAnchor();
455         bv().unsetXSel();
456 }
457
458
459 int & LCursor::x_target()
460 {
461         return x_target_;
462 }
463
464
465 int LCursor::x_target() const
466 {
467         return x_target_;
468 }
469
470
471 void LCursor::clearTargetX()
472 {
473         x_target_ = -1;
474 }
475
476
477 Paragraph & LCursor::paragraph()
478 {
479         return current_ ? current().paragraph() : *bv_->text()->getPar(par());
480 }
481
482
483 Paragraph const & LCursor::paragraph() const
484 {
485         return current_ ? current().paragraph() : *bv_->text()->getPar(par());
486 }
487
488
489 LCursor::pos_type LCursor::lastpos() const
490 {
491         InsetBase * inset = current().inset();
492         return inset && inset->asMathInset() ? cell().size() : paragraph().size();
493 }
494
495
496 size_t LCursor::nargs() const
497 {
498         // assume 1x1 grid for 'plain text'
499         return current_ ? current().nargs() : 1;
500 }
501
502
503 size_t LCursor::ncols() const
504 {
505         // assume 1x1 grid for 'plain text'
506         return current_ ? current().ncols() : 1;
507 }
508
509
510 size_t LCursor::nrows() const
511 {
512         // assume 1x1 grid for 'plain text'
513         return current_ ? current().nrows() : 1;
514 }
515
516
517 LCursor::row_type LCursor::row() const
518 {
519         BOOST_ASSERT(current_ > 0);
520         return current().row();
521 }
522
523
524 LCursor::col_type LCursor::col() const
525 {
526         BOOST_ASSERT(current_ > 0);
527         return current().col();
528 }
529
530
531 MathArray const & LCursor::cell() const
532 {
533         BOOST_ASSERT(current_ > 0);
534         return current().cell();
535 }
536
537
538 MathArray & LCursor::cell()
539 {
540         BOOST_ASSERT(current_ > 0);
541         return current().cell();
542 }
543
544
545 void LCursor::info(std::ostream & os)
546 {
547         for (int i = 1, n = depth(); i < n; ++i) {
548                 cursor_[i].inset()->infoize(os);
549                 os << "  ";
550         }
551 #warning FIXME
552         //if (pos() != 0)
553         //      prevAtom()->infoize2(os);
554         // overwite old message
555         os << "                    ";
556 }
557
558
559 namespace {
560
561 void region(CursorSlice const & i1, CursorSlice const & i2,
562         LCursor::row_type & r1, LCursor::row_type & r2,
563         LCursor::col_type & c1, LCursor::col_type & c2)
564 {
565         InsetBase * p = i1.inset();
566         c1 = p->col(i1.idx_);
567         c2 = p->col(i2.idx_);
568         if (c1 > c2)
569                 swap(c1, c2);
570         r1 = p->row(i1.idx_);
571         r2 = p->row(i2.idx_);
572         if (r1 > r2)
573                 swap(r1, r2);
574 }
575
576 }
577
578
579 string LCursor::grabSelection()
580 {
581         if (!selection())
582                 return string();
583
584         CursorSlice i1 = selBegin();
585         CursorSlice i2 = selEnd();
586
587         if (i1.idx_ == i2.idx_) {
588                 if (i1.inset()->asMathInset()) {
589                         MathArray::const_iterator it = i1.cell().begin();
590                         return asString(MathArray(it + i1.pos_, it + i2.pos_));
591                 } else {
592                         return "unknown selection 1";
593                 }
594         }
595
596         row_type r1, r2;
597         col_type c1, c2;
598         region(i1, i2, r1, r2, c1, c2);
599
600         string data;
601         if (i1.inset()->asMathInset()) {
602                 for (row_type row = r1; row <= r2; ++row) {
603                         if (row > r1)
604                                 data += "\\\\";
605                         for (col_type col = c1; col <= c2; ++col) {
606                                 if (col > c1)
607                                         data += '&';
608                                 data += asString(i1.asMathInset()->cell(i1.asMathInset()->index(row, col)));
609                         }
610                 }
611         } else {
612                 data = "unknown selection 2";
613         }
614         return data;
615 }
616
617
618 void LCursor::eraseSelection()
619 {
620         CursorSlice i1 = selBegin();
621         CursorSlice i2 = selEnd();
622 #warning FIXME
623         if (i1.inset()->asMathInset()) {
624                 if (i1.idx_ == i2.idx_) {
625                         i1.cell().erase(i1.pos_, i2.pos_);
626                 } else {
627                         MathInset * p = i1.asMathInset();
628                         row_type r1, r2;
629                         col_type c1, c2;
630                         region(i1, i2, r1, r2, c1, c2);
631                         for (row_type row = r1; row <= r2; ++row)
632                                 for (col_type col = c1; col <= c2; ++col)
633                                         p->cell(p->index(row, col)).clear();
634                 }
635                 current() = i1;
636         } else {
637                 lyxerr << "can't erase this selection 1" << endl;
638         }
639 }
640
641
642 string LCursor::grabAndEraseSelection()
643 {
644         if (!selection())
645                 return string();
646         string res = grabSelection();
647         eraseSelection();
648         selection() = false;
649         return res;
650 }
651
652
653 void LCursor::selClear()
654 {
655         resetAnchor();
656         clearSelection();
657 }
658
659
660 void LCursor::selCopy()
661 {
662         if (selection()) {
663                 theCutBuffer.push(grabSelection());
664                 selection() = false;
665         } else {
666                 //theCutBuffer.erase();
667         }
668 }
669
670
671 void LCursor::selCut()
672 {
673         theCutBuffer.push(grabAndEraseSelection());
674 }
675
676
677 void LCursor::selDel()
678 {
679         if (selection()) {
680                 eraseSelection();
681                 selection() = false;
682         }
683 }
684
685
686 void LCursor::selPaste(size_t n)
687 {
688         selClearOrDel();
689         if (n < theCutBuffer.size())
690                 paste(theCutBuffer[n]);
691         //grabSelection();
692         selection() = false;
693 }
694
695
696 void LCursor::selHandle(bool sel)
697 {
698         if (sel == selection())
699                 return;
700         resetAnchor();
701         selection() = sel;
702 }
703
704
705 void LCursor::selStart()
706 {
707         resetAnchor();
708         selection() = true;
709 }
710
711
712 void LCursor::selClearOrDel()
713 {
714         if (lyxrc.auto_region_delete)
715                 selDel();
716         else
717                 selection() = false;
718 }
719
720
721 std::ostream & operator<<(std::ostream & os, LCursor const & cur)
722 {
723         os << "\n";
724         for (size_t i = 0, n = cur.cursor_.size(); i != n; ++i)
725                 os << "  (" << cur.cursor_[i] << " | " << cur.anchor_[i] << "\n";
726         return os;
727 }
728
729
730
731
732 //
733 // CursorBase
734 //
735
736
737 void increment(CursorBase & it)
738 {
739         CursorSlice & top = it.back();
740         MathArray & ar = top.asMathInset()->cell(top.idx_);
741
742         // move into the current inset if possible
743         // it is impossible for pos() == size()!
744         MathInset * n = 0;
745         if (top.pos() != top.lastpos())
746                 n = (ar.begin() + top.pos_)->nucleus();
747         if (n && n->isActive()) {
748                 it.push_back(CursorSlice(n));
749                 return;
750         }
751
752         // otherwise move on one cell back if possible
753         if (top.pos() < top.lastpos()) {
754                 // pos() == lastpos() is valid!
755                 ++top.pos_;
756                 return;
757         }
758
759         // otherwise try to move on one cell if possible
760         while (top.idx() < top.lastidx()) {
761                 ++top.idx_;
762                 if (top.asMathInset()->validCell(top.idx_)) {
763                         top.pos_ = 0;
764                         return;
765                 }
766         }
767
768         // otherwise leave array, move on one back
769         // this might yield pos() == size(), but that's a ok.
770         it.pop_back();
771         // it certainly invalidates top
772         ++it.back().pos_;
773 }
774
775
776 CursorBase ibegin(InsetBase * p)
777 {
778         CursorBase it;
779         it.push_back(CursorSlice(p));
780         return it;
781 }
782
783
784 CursorBase iend(InsetBase * p)
785 {
786         CursorBase it;
787         it.push_back(CursorSlice(p));
788         CursorSlice & cur = it.back();
789         cur.idx() = cur.lastidx();
790         cur.pos() = cur.lastpos();
791         return it;
792 }
793
794
795
796
797 ///////////////////////////////////////////////////////////////////
798 //
799 // The part below is the non-integrated rest of the original math
800 // cursor. This should be either generalized for texted or moved
801 // back to the math insets.
802 //
803 ///////////////////////////////////////////////////////////////////
804
805 #include "mathed/math_braceinset.h"
806 #include "mathed/math_charinset.h"
807 #include "mathed/math_commentinset.h"
808 #include "mathed/math_factory.h"
809 #include "mathed/math_gridinset.h"
810 #include "mathed/math_macroarg.h"
811 #include "mathed/math_macrotemplate.h"
812 #include "mathed/math_mathmlstream.h"
813 #include "mathed/math_scriptinset.h"
814 #include "mathed/math_spaceinset.h"
815 #include "mathed/math_support.h"
816 #include "mathed/math_unknowninset.h"
817
818 //#define FILEDEBUG 1
819
820
821 bool LCursor::isInside(InsetBase const * p)
822 {
823         for (unsigned i = 0; i < depth(); ++i)
824                 if (cursor_[i].inset() == p)
825                         return true;
826         return false;
827 }
828
829
830 bool LCursor::openable(MathAtom const & t)
831 {
832         if (!t->isActive())
833                 return false;
834
835         if (t->lock())
836                 return false;
837
838         if (!selection())
839                 return true;
840
841         // we can't move into anything new during selection
842         if (depth() == anchor_.size())
843                 return false;
844         if (t.nucleus() != anchor_[depth()].inset())
845                 return false;
846
847         return true;
848 }
849
850
851 bool LCursor::inNucleus()
852 {
853         return inset()->asMathInset()->asScriptInset() && idx() == 2;
854 }
855
856
857 bool LCursor::left()
858 {
859         autocorrect() = false;
860         clearTargetX();
861         if (inMacroMode()) {
862                 macroModeClose();
863                 return true;
864         }
865
866         if (pos() != 0 && openable(prevAtom())) {
867                 posLeft();
868                 push(nextAtom().nucleus());
869                 inset()->idxLast(*this);
870                 return true;
871         }
872
873         return posLeft() || idxLeft() || popLeft() || selection();
874 }
875
876
877 bool LCursor::right()
878 {
879         autocorrect() = false;
880         clearTargetX();
881         if (inMacroMode()) {
882                 macroModeClose();
883                 return true;
884         }
885
886         if (pos() != lastpos() && openable(nextAtom())) {
887                 pushLeft(nextAtom().nucleus());
888                 inset()->idxFirst(*this);
889                 return true;
890         }
891
892         return posRight() || idxRight() || popRight() || selection();
893 }
894
895
896 bool positionable(CursorBase const & cursor, CursorBase const & anchor)
897 {
898         // avoid deeper nested insets when selecting
899         if (cursor.size() > anchor.size())
900                 return false;
901
902         // anchor might be deeper, should have same path then
903         for (size_t i = 0; i < cursor.size(); ++i)
904                 if (cursor[i].inset() != anchor[i].inset())
905                         return false;
906
907         // position should be ok.
908         return true;
909 }
910
911
912 void LCursor::setScreenPos(int x, int y)
913 {
914         bool res = bruteFind(x, y, formula()->xlow(), formula()->xhigh(),
915                 formula()->ylow(), formula()->yhigh());
916         if (!res) {
917                 // this can happen on creation of "math-display"
918                 idx() = 0;
919                 pos() = 0;
920         }
921         clearTargetX();
922 }
923
924
925
926 bool LCursor::home()
927 {
928         autocorrect() = false;
929         macroModeClose();
930         if (!inset()->idxHome(*this))
931                 return popLeft();
932         clearTargetX();
933         return true;
934 }
935
936
937 bool LCursor::end()
938 {
939         autocorrect() = false;
940         macroModeClose();
941         if (!inset()->idxEnd(*this))
942                 return popRight();
943         clearTargetX();
944         return true;
945 }
946
947
948 void LCursor::plainErase()
949 {
950         cell().erase(pos());
951 }
952
953
954 void LCursor::markInsert()
955 {
956         cell().insert(pos(), MathAtom(new MathCharInset(0)));
957 }
958
959
960 void LCursor::markErase()
961 {
962         cell().erase(pos());
963 }
964
965
966 void LCursor::plainInsert(MathAtom const & t)
967 {
968         cell().insert(pos(), t);
969         ++pos();
970 }
971
972
973 void LCursor::insert2(string const & str)
974 {
975         MathArray ar;
976         asArray(str, ar);
977         insert(ar);
978 }
979
980
981 void LCursor::insert(string const & str)
982 {
983         lyxerr << "inserting '" << str << "'" << endl;
984         selClearOrDel();
985         for (string::const_iterator it = str.begin(); it != str.end(); ++it)
986                 plainInsert(MathAtom(new MathCharInset(*it)));
987 }
988
989
990 void LCursor::insert(char c)
991 {
992         lyxerr << "inserting '" << c << "'" << endl;
993         selClearOrDel();
994         plainInsert(MathAtom(new MathCharInset(c)));
995 }
996
997
998 void LCursor::insert(MathAtom const & t)
999 {
1000         macroModeClose();
1001         selClearOrDel();
1002         plainInsert(t);
1003 }
1004
1005
1006 void LCursor::niceInsert(string const & t)
1007 {
1008         lyxerr << "*** LCursor::niceInsert 1: " << t << endl;
1009         MathArray ar;
1010         asArray(t, ar);
1011         if (ar.size() == 1)
1012                 niceInsert(ar[0]);
1013         else
1014                 insert(ar);
1015 }
1016
1017
1018 void LCursor::niceInsert(MathAtom const & t)
1019 {
1020         lyxerr << "*** LCursor::niceInsert 2: " << t << endl;
1021         macroModeClose();
1022         string safe = grabAndEraseSelection();
1023         plainInsert(t);
1024         // enter the new inset and move the contents of the selection if possible
1025         if (t->isActive()) {
1026                 posLeft();
1027                 pushLeft(nextAtom().nucleus());
1028                 paste(safe);
1029         }
1030         lyxerr << "*** LCursor::niceInsert 3: " << t << endl;
1031 }
1032
1033
1034 void LCursor::insert(MathArray const & ar)
1035 {
1036         macroModeClose();
1037         if (selection())
1038                 eraseSelection();
1039         cell().insert(pos(), ar);
1040         pos() += ar.size();
1041 }
1042
1043
1044 bool LCursor::backspace()
1045 {
1046         autocorrect() = false;
1047
1048         if (selection()) {
1049                 selDel();
1050                 return true;
1051         }
1052
1053         if (pos() == 0) {
1054                 if (inset()->nargs() == 1 && depth() == 1 && lastpos() == 0)
1055                         return false;
1056                 pullArg();
1057                 return true;
1058         }
1059
1060         if (inMacroMode()) {
1061                 MathUnknownInset * p = activeMacro();
1062                 if (p->name().size() > 1) {
1063                         p->setName(p->name().substr(0, p->name().size() - 1));
1064                         return true;
1065                 }
1066         }
1067
1068         if (pos() != 0 && prevAtom()->nargs() > 0) {
1069                 // let's require two backspaces for 'big stuff' and
1070                 // highlight on the first
1071                 selection() = true;
1072                 left();
1073         } else {
1074                 --pos();
1075                 plainErase();
1076         }
1077         return true;
1078 }
1079
1080
1081 bool LCursor::erase()
1082 {
1083         autocorrect() = false;
1084         if (inMacroMode())
1085                 return true;
1086
1087         if (selection()) {
1088                 selDel();
1089                 return true;
1090         }
1091
1092         // delete empty cells if possible
1093 #warning FIXME
1094         //if (cell().empty() && inset()->idxDelete(idx()))
1095         //              return true;
1096
1097         // special behaviour when in last position of cell
1098         if (pos() == lastpos()) {
1099                 bool one_cell = inset()->nargs() == 1;
1100                 if (one_cell && depth() == 1 && lastpos() == 0)
1101                         return false;
1102                 // remove markup
1103                 if (one_cell)
1104                         pullArg();
1105                 else
1106                         inset()->idxGlue(idx());
1107                 return true;
1108         }
1109
1110         if (pos() != lastpos() && inset()->nargs() > 0) {
1111                 selection() = true;
1112                 right();
1113         } else {
1114                 plainErase();
1115         }
1116
1117         return true;
1118 }
1119
1120
1121 bool LCursor::up()
1122 {
1123         macroModeClose();
1124         CursorBase save = cursor_;
1125         if (goUpDown(true))
1126                 return true;
1127         cursor_ = save;
1128         autocorrect() = false;
1129         return selection();
1130 }
1131
1132
1133 bool LCursor::down()
1134 {
1135         macroModeClose();
1136         CursorBase save = cursor_;
1137         if (goUpDown(false))
1138                 return true;
1139         cursor_ = save;
1140         autocorrect() = false;
1141         return selection();
1142 }
1143
1144
1145 void LCursor::macroModeClose()
1146 {
1147         if (!inMacroMode())
1148                 return;
1149         MathUnknownInset * p = activeMacro();
1150         p->finalize();
1151         string s = p->name();
1152         --pos();
1153         cell().erase(pos());
1154
1155         // do nothing if the macro name is empty
1156         if (s == "\\")
1157                 return;
1158
1159         string const name = s.substr(1);
1160
1161         // prevent entering of recursive macros
1162         if (formula()->lyxCode() == InsetOld::MATHMACRO_CODE
1163                         && formula()->getInsetName() == name)
1164                 lyxerr << "can't enter recursive macro" << endl;
1165
1166         niceInsert(createMathInset(name));
1167 }
1168
1169
1170 string LCursor::macroName()
1171 {
1172         return inMacroMode() ? activeMacro()->name() : string();
1173 }
1174
1175
1176 void LCursor::drawSelection(PainterInfo & pi)
1177 {
1178         if (!selection())
1179                 return;
1180         CursorSlice i1 = selBegin();
1181         CursorSlice i2 = selEnd();
1182         i1.asMathInset()->drawSelection(pi, i1.idx_, i1.pos_, i2.idx_, i2.pos_);
1183 }
1184
1185
1186 void LCursor::handleNest(MathAtom const & a, int c)
1187 {
1188         MathAtom t = a;
1189         asArray(grabAndEraseSelection(), t.nucleus()->cell(c));
1190         insert(t);
1191         posLeft();
1192         pushLeft(t.nucleus());
1193 }
1194
1195
1196 int LCursor::targetX() const
1197 {
1198         if (x_target() != -1)
1199                 return x_target();
1200         int x = 0;
1201         int y = 0;
1202         getPos(x, y);
1203         return x;
1204 }
1205
1206
1207 MathHullInset * LCursor::formula() const
1208 {
1209         for (int i = cursor_.size() - 1; i >= 1; --i)
1210                 if (cursor_[i].inset()->lyxCode() == InsetBase::MATH_CODE)
1211                         return static_cast<MathHullInset *>(cursor_[i].inset());
1212         return 0;
1213 }
1214
1215
1216 void LCursor::adjust(pos_type from, int diff)
1217 {
1218         if (pos() > from)
1219                 pos() += diff;
1220         if (anchor().pos_ > from)
1221                 anchor().pos_ += diff;
1222         // just to be on the safe side
1223         // theoretically unecessary
1224         normalize();
1225 }
1226
1227
1228 bool LCursor::inMacroMode() const
1229 {
1230         if (!pos() != 0)
1231                 return false;
1232         MathUnknownInset const * p = prevAtom()->asUnknownInset();
1233         return p && !p->final();
1234 }
1235
1236
1237 MathUnknownInset * LCursor::activeMacro()
1238 {
1239         return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1240 }
1241
1242
1243 bool LCursor::inMacroArgMode() const
1244 {
1245         return pos() > 0 && prevAtom()->getChar() == '#';
1246 }
1247
1248
1249 MathGridInset * LCursor::enclosingGrid(idx_type & idx) const
1250 {
1251         for (MathInset::difference_type i = depth() - 1; i >= 0; --i) {
1252                 MathInset * m = cursor_[i].inset()->asMathInset();
1253                 if (!m)
1254                         return 0;
1255                 MathGridInset * p = m->asGridInset();
1256                 if (p) {
1257                         idx = cursor_[i].idx_;
1258                         return p;
1259                 }
1260         }
1261         return 0;
1262 }
1263
1264
1265 void LCursor::pullArg()
1266 {
1267 #warning look here
1268 #if 0
1269         MathArray ar = cell();
1270         if (popLeft()) {
1271                 plainErase();
1272                 cell().insert(pos(), ar);
1273                 resetAnchor();
1274         } else {
1275                 formula()->mutateToText();
1276         }
1277 #endif
1278 }
1279
1280
1281 void LCursor::touch()
1282 {
1283 #warning look here
1284 #if 0
1285         CursorBase::const_iterator it = cursor_.begin();
1286         CursorBase::const_iterator et = cursor_.end();
1287         for ( ; it != et; ++it)
1288                 it->cell().touch();
1289 #endif
1290 }
1291
1292
1293 void LCursor::normalize()
1294 {
1295         if (idx() >= nargs()) {
1296                 lyxerr << "this should not really happen - 1: "
1297                        << idx() << ' ' << nargs()
1298                        << " in: " << inset() << endl;
1299         }
1300         idx() = min(idx(), nargs() - 1);
1301
1302         if (pos() > lastpos()) {
1303                 lyxerr << "this should not really happen - 2: "
1304                         << pos() << ' ' << lastpos() <<  " in idx: " << idx()
1305                        << " in atom: '";
1306                 WriteStream wi(lyxerr, false, true);
1307                 inset()->asMathInset()->write(wi);
1308                 lyxerr << endl;
1309         }
1310         pos() = min(pos(), lastpos());
1311 }
1312
1313
1314 char LCursor::valign()
1315 {
1316         idx_type idx;
1317         MathGridInset * p = enclosingGrid(idx);
1318         return p ? p->valign() : '\0';
1319 }
1320
1321
1322 char LCursor::halign()
1323 {
1324         idx_type idx;
1325         MathGridInset * p = enclosingGrid(idx);
1326         return p ? p->halign(idx % p->ncols()) : '\0';
1327 }
1328
1329
1330 bool LCursor::goUpDown(bool up)
1331 {
1332         // Be warned: The 'logic' implemented in this function is highly fragile.
1333         // A distance of one pixel or a '<' vs '<=' _really_ matters.
1334         // So fiddle around with it only if you know what you are doing!
1335   int xo = 0;
1336         int yo = 0;
1337         getPos(xo, yo);
1338
1339         // check if we had something else in mind, if not, this is the future goal
1340         if (x_target() == -1)
1341                 x_target() = xo;
1342         else
1343                 xo = x_target();
1344
1345         // try neigbouring script insets
1346         if (!selection()) {
1347                 // try left
1348                 if (pos() != 0) {
1349                         MathScriptInset const * p = prevAtom()->asScriptInset();
1350                         if (p && p->has(up)) {
1351                                 --pos();
1352                                 push(inset());
1353                                 idx() = up; // the superscript has index 1
1354                                 pos() = lastpos();
1355                                 //lyxerr << "updown: handled by scriptinset to the left" << endl;
1356                                 return true;
1357                         }
1358                 }
1359
1360                 // try right
1361                 if (pos() != lastpos()) {
1362                         MathScriptInset const * p = nextAtom()->asScriptInset();
1363                         if (p && p->has(up)) {
1364                                 push(inset());
1365                                 idx() = up;
1366                                 pos() = 0;
1367                                 //lyxerr << "updown: handled by scriptinset to the right" << endl;
1368                                 return true;
1369                         }
1370                 }
1371         }
1372
1373         // try current cell for e.g. text insets
1374         if (inset()->idxUpDown2(*this, up))
1375                 return true;
1376
1377         //xarray().boundingBox(xlow, xhigh, ylow, yhigh);
1378         //if (up)
1379         //      yhigh = yo - 4;
1380         //else
1381         //      ylow = yo + 4;
1382         //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) {
1383         //      lyxerr << "updown: handled by brute find in the same cell" << endl;
1384         //      return true;
1385         //}
1386
1387         // try to find an inset that knows better then we
1388         while (1) {
1389                 //lyxerr << "updown: We are in " << inset() << " idx: " << idx() << endl;
1390                 // ask inset first
1391                 if (inset()->idxUpDown(*this, up)) {
1392                         // try to find best position within this inset
1393                         if (!selection())
1394                                 bruteFind2(xo, yo);
1395                         return true;
1396                 }
1397
1398                 // no such inset found, just take something "above"
1399                 //lyxerr << "updown: handled by strange case" << endl;
1400                 if (!popLeft()) {
1401                         return
1402                                 bruteFind(xo, yo,
1403                                         formula()->xlow(),
1404                                         formula()->xhigh(),
1405                                         up ? formula()->ylow() : yo + 4,
1406                                         up ? yo - 4 : formula()->yhigh()
1407                                 );
1408                 }
1409
1410                 // any improvement so far?
1411                 int xnew, ynew;
1412                 getPos(xnew, ynew);
1413                 if (up ? ynew < yo : ynew > yo)
1414                         return true;
1415         }
1416 }
1417
1418
1419 bool LCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
1420 {
1421         CursorBase best_cursor;
1422         double best_dist = 1e10;
1423
1424         CursorBase it = ibegin(formula());
1425         CursorBase et = iend(formula());
1426         while (1) {
1427                 // avoid invalid nesting when selecting
1428                 if (!selection() || positionable(it, anchor_)) {
1429                         int xo, yo;
1430                         CursorSlice & cur = it.back();
1431                         cur.inset()->getCursorPos(cur, xo, yo);
1432                         if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
1433                                 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1434                                 //lyxerr << "x: " << x << " y: " << y << " d: " << endl;
1435                                 // '<=' in order to take the last possible position
1436                                 // this is important for clicking behind \sum in e.g. '\sum_i a'
1437                                 if (d <= best_dist) {
1438                                         best_dist   = d;
1439                                         best_cursor = it;
1440                                 }
1441                         }
1442                 }
1443
1444                 if (it == et)
1445                         break;
1446                 increment(it);
1447         }
1448
1449         if (best_dist < 1e10)
1450                 cursor_ = best_cursor;
1451         return best_dist < 1e10;
1452 }
1453
1454
1455 void LCursor::bruteFind2(int x, int y)
1456 {
1457         double best_dist = 1e10;
1458
1459         CursorBase it = cursor_;
1460         it.back().pos(0);
1461         CursorBase et = cursor_;
1462         int n = et.back().asMathInset()->cell(et.back().idx_).size();
1463         et.back().pos(n);
1464         for (int i = 0; ; ++i) {
1465                 int xo, yo;
1466                 CursorSlice & cur = it.back();
1467                 cur.inset()->getCursorPos(cur, xo, yo);
1468                 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
1469                 // '<=' in order to take the last possible position
1470                 // this is important for clicking behind \sum in e.g. '\sum_i a'
1471                 lyxerr << "i: " << i << " d: " << d << " best: " << best_dist << endl;
1472                 if (d <= best_dist) {
1473                         best_dist = d;
1474                         cursor_ = it;
1475                 }
1476                 if (it == et)
1477                         break;
1478                 increment(it);
1479         }
1480 }
1481
1482
1483 bool LCursor::idxLineLast()
1484 {
1485         idx() -= idx() % ncols();
1486         idx() += ncols() - 1;
1487         pos() = lastpos();
1488         return true;
1489 }
1490
1491
1492 bool LCursor::idxLeft()
1493 {
1494         return inset()->idxLeft(*this);
1495 }
1496
1497
1498 bool LCursor::idxRight()
1499 {
1500         return inset()->idxRight(*this);
1501 }
1502
1503
1504 bool LCursor::script(bool up)
1505 {
1506         // Hack to get \\^ and \\_ working
1507         if (inMacroMode() && macroName() == "\\") {
1508                 if (up)
1509                         niceInsert(createMathInset("mathcircumflex"));
1510                 else
1511                         interpret('_');
1512                 return true;
1513         }
1514
1515         macroModeClose();
1516         string safe = grabAndEraseSelection();
1517         if (inNucleus()) {
1518                 // we are in a nucleus of a script inset, move to _our_ script
1519                 inset()->asMathInset()->asScriptInset()->ensure(up);
1520                 idx() = up;
1521                 pos() = 0;
1522         } else if (pos() != 0 && prevAtom()->asScriptInset()) {
1523                 prevAtom().nucleus()->asScriptInset()->ensure(up);
1524                 posLeft();
1525                 push(inset());
1526                 idx() = up;
1527                 pos() = lastpos();
1528         } else if (pos() != 0) {
1529                 --pos();
1530                 cell()[pos()]
1531                         = MathAtom(new MathScriptInset(nextAtom(), up));
1532                 push(inset());
1533                 idx() = up;
1534                 pos() = 0;
1535         } else {
1536                 plainInsert(MathAtom(new MathScriptInset(up)));
1537                 posLeft();
1538                 nextAtom().nucleus()->asScriptInset()->ensure(up);
1539                 push(inset());
1540                 idx() = up;
1541                 pos() = 0;
1542         }
1543         paste(safe);
1544         return true;
1545 }
1546
1547
1548 bool LCursor::interpret(char c)
1549 {
1550         //lyxerr << "interpret 2: '" << c << "'" << endl;
1551         clearTargetX();
1552         if (inMacroArgMode()) {
1553                 posLeft();
1554                 plainErase();
1555 #warning FIXME
1556 #if 0
1557                 int n = c - '0';
1558                 MathMacroTemplate const * p = formula()->asMacroTemplate();
1559                 if (p && 1 <= n && n <= p->numargs())
1560                         insert(MathAtom(new MathMacroArgument(c - '0')));
1561                 else {
1562                         insert(createMathInset("#"));
1563                         interpret(c); // try again
1564                 }
1565 #endif
1566                 return true;
1567         }
1568
1569         // handle macroMode
1570         if (inMacroMode()) {
1571                 string name = macroName();
1572                 //lyxerr << "interpret name: '" << name << "'" << endl;
1573
1574                 if (isalpha(c)) {
1575                         activeMacro()->setName(activeMacro()->name() + c);
1576                         return true;
1577                 }
1578
1579                 // handle 'special char' macros
1580                 if (name == "\\") {
1581                         // remove the '\\'
1582                         backspace();
1583                         if (c == '\\') {
1584                                 if (currentMode() == MathInset::TEXT_MODE)
1585                                         niceInsert(createMathInset("textbackslash"));
1586                                 else
1587                                         niceInsert(createMathInset("backslash"));
1588                         } else if (c == '{') {
1589                                 niceInsert(MathAtom(new MathBraceInset));
1590                         } else {
1591                                 niceInsert(createMathInset(string(1, c)));
1592                         }
1593                         return true;
1594                 }
1595
1596                 // leave macro mode and try again if necessary
1597                 macroModeClose();
1598                 if (c == '{')
1599                         niceInsert(MathAtom(new MathBraceInset));
1600                 else if (c != ' ')
1601                         interpret(c);
1602                 return true;
1603         }
1604
1605         // This is annoying as one has to press <space> far too often.
1606         // Disable it.
1607
1608         if (0) {
1609                 // leave autocorrect mode if necessary
1610                 if (autocorrect() && c == ' ') {
1611                         autocorrect() = false;
1612                         return true;
1613                 }
1614         }
1615
1616         // just clear selection on pressing the space bar
1617         if (selection() && c == ' ') {
1618                 selection() = false;
1619                 return true;
1620         }
1621
1622         selClearOrDel();
1623
1624         if (c == '\\') {
1625                 //lyxerr << "starting with macro" << endl;
1626                 insert(MathAtom(new MathUnknownInset("\\", false)));
1627                 return true;
1628         }
1629
1630         if (c == '\n') {
1631                 if (currentMode() == MathInset::TEXT_MODE)
1632                         insert(c);
1633                 return true;
1634         }
1635
1636         if (c == ' ') {
1637                 if (currentMode() == MathInset::TEXT_MODE) {
1638                         // insert spaces in text mode,
1639                         // but suppress direct insertion of two spaces in a row
1640                         // the still allows typing  '<space>a<space>' and deleting the 'a', but
1641                         // it is better than nothing...
1642                         if (!pos() != 0 || prevAtom()->getChar() != ' ')
1643                                 insert(c);
1644                         return true;
1645                 }
1646                 if (pos() != 0 && prevAtom()->asSpaceInset()) {
1647                         prevAtom().nucleus()->asSpaceInset()->incSpace();
1648                         return true;
1649                 }
1650                 if (popRight())
1651                         return true;
1652                 // if are at the very end, leave the formula
1653                 return pos() != lastpos();
1654         }
1655
1656         if (c == '_') {
1657                 script(false);
1658                 return true;
1659         }
1660
1661         if (c == '^') {
1662                 script(true);
1663                 return true;
1664         }
1665
1666         if (c == '{' || c == '}' || c == '#' || c == '&' || c == '$') {
1667                 niceInsert(createMathInset(string(1, c)));
1668                 return true;
1669         }
1670
1671         if (c == '%') {
1672                 niceInsert(MathAtom(new MathCommentInset));
1673                 return true;
1674         }
1675
1676         // try auto-correction
1677         //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
1678         //      return true;
1679
1680         // no special circumstances, so insert the character without any fuss
1681         insert(c);
1682         autocorrect() = true;
1683         return true;
1684 }
1685
1686
1687 void LCursor::lockToggle()
1688 {
1689         if (pos() != lastpos()) {
1690                 // toggle previous inset ...
1691                 nextAtom().nucleus()->lock(!nextAtom()->lock());
1692         } else if (popLeft() && pos() != lastpos()) {
1693                 // ... or enclosing inset if we are in the last inset position
1694                 nextAtom().nucleus()->lock(!nextAtom()->lock());
1695                 posRight();
1696         }
1697 }
1698
1699
1700 CursorSlice LCursor::normalAnchor()
1701 {
1702         if (anchor_.size() < depth()) {
1703                 resetAnchor();
1704                 lyxerr << "unusual Anchor size" << endl;
1705         }
1706         //lyx::BOOST_ASSERT(Anchor_.size() >= cursor.depth());
1707         // use Anchor on the same level as Cursor
1708         CursorSlice normal = anchor_[depth() - 1];
1709 #if 0
1710         if (depth() < anchor_.size() && !(normal < xx())) {
1711                 // anchor is behind cursor -> move anchor behind the inset
1712                 ++normal.pos_;
1713         }
1714 #endif
1715         return normal;
1716 }
1717
1718
1719 /*
1720 DispatchResult dispatch(LCursor & cur, FuncRequest const & cmd)
1721 {
1722         // mouse clicks are somewhat special
1723         // check
1724         switch (cmd.action) {
1725         case LFUN_MOUSE_PRESS:
1726         case LFUN_MOUSE_MOTION:
1727         case LFUN_MOUSE_RELEASE:
1728         case LFUN_MOUSE_DOUBLE: {
1729                 CursorSlice & pos = cursor_.back();
1730                 int x = 0;
1731                 int y = 0;
1732                 getPos(x, y);
1733                 if (x < cmd.x && pos() != 0) {
1734                         DispatchResult const res = prevAtom().nucleus()->dispatch(cmd);
1735                         if (res.dispatched())
1736                                 return res;
1737                 }
1738                 if (x > cmd.x && pos() != lastpos()) {
1739                         DispatchResult const res = inset()->dispatch(cmd);
1740                         if (res.dispatched())
1741                                 return res;
1742                 }
1743         }
1744         default:
1745         break;
1746         }
1747 }
1748 */
1749
1750
1751 void LCursor::handleFont(string const & font)
1752 {
1753         string safe;
1754         if (selection()) {
1755                 macroModeClose();
1756                 safe = grabAndEraseSelection();
1757         }
1758
1759         if (lastpos() != 0) {
1760                 // something left in the cell
1761                 if (pos() == 0) {
1762                         // cursor in first position
1763                         popLeft();
1764                 } else if (pos() == lastpos()) {
1765                         // cursor in last position
1766                         popRight();
1767                 } else {
1768                         // cursor in between. split cell
1769                         MathArray::iterator bt = cell().begin();
1770                         MathAtom at = createMathInset(font);
1771                         at.nucleus()->cell(0) = MathArray(bt, bt + pos());
1772                         cell().erase(bt, bt + pos());
1773                         popLeft();
1774                         plainInsert(at);
1775                 }
1776         } else {
1777                 // nothing left in the cell
1778                 pullArg();
1779                 plainErase();
1780         }
1781         insert(safe);
1782 }
1783
1784
1785 void LCursor::releaseMathCursor()
1786 {
1787         if (inMathed())
1788                 formula()->insetUnlock(bv());
1789 }
1790
1791
1792 bool LCursor::inMathed() const
1793 {
1794         return formula();
1795 }