]> git.lyx.org Git - lyx.git/blob - src/Cursor.cpp
cc9b76eee08e58ba07961239b4f52488e8d4e18b
[lyx.git] / src / Cursor.cpp
1 /**
2  * \file Cursor.cpp
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 Dov Feldstern
9  * \author André Pönitz
10  * \author Stefan Schimanski
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "CoordCache.h"
21 #include "Cursor.h"
22 #include "CutAndPaste.h"
23 #include "DispatchResult.h"
24 #include "Encoding.h"
25 #include "Font.h"
26 #include "FuncCode.h"
27 #include "FuncRequest.h"
28 #include "Language.h"
29 #include "Layout.h"
30 #include "LyXAction.h"
31 #include "LyXRC.h"
32 #include "Paragraph.h"
33 #include "ParIterator.h"
34 #include "Row.h"
35 #include "texstream.h"
36 #include "Text.h"
37 #include "TextMetrics.h"
38 #include "TocBackend.h"
39
40 #include "support/debug.h"
41 #include "support/docstream.h"
42 #include "support/ExceptionMessage.h"
43 #include "support/gettext.h"
44 #include "support/lassert.h"
45
46 #include "insets/InsetTabular.h"
47 #include "insets/InsetText.h"
48
49 #include "mathed/InsetMath.h"
50 #include "mathed/InsetMathBrace.h"
51 #include "mathed/InsetMathEnsureMath.h"
52 #include "mathed/InsetMathScript.h"
53 #include "mathed/MacroTable.h"
54 #include "mathed/MathData.h"
55 #include "mathed/MathFactory.h"
56 #include "mathed/InsetMathMacro.h"
57
58 #include <sstream>
59 #include <limits>
60 #include <map>
61 #include <algorithm>
62
63 using namespace std;
64
65 namespace lyx {
66
67 namespace {
68
69 // Find position closest to (x, y) in cell given by iter.
70 // Used only in mathed
71 DocIterator bruteFind(Cursor const & c, int x, int y)
72 {
73         double best_dist = numeric_limits<double>::max();
74
75         DocIterator result;
76
77         DocIterator it = c;
78         it.pos() = 0;
79         DocIterator et = c;
80         et.pos() = et.lastpos();
81         for (size_t i = 0;; ++i) {
82                 int xo;
83                 int yo;
84                 Inset const * inset = &it.inset();
85                 CoordCache::Insets const & insetCache = c.bv().coordCache().getInsets();
86
87                 // FIXME: in the case where the inset is not in the cache, this
88                 // means that no part of it is visible on screen. In this case
89                 // we don't do elaborate search and we just return the forwarded
90                 // DocIterator at its beginning.
91                 if (!insetCache.has(inset)) {
92                         it.top().pos() = 0;
93                         return it;
94                 }
95
96                 Point const o = insetCache.xy(inset);
97                 inset->cursorPos(c.bv(), it.top(), c.boundary(), xo, yo);
98                 // Convert to absolute
99                 xo += o.x_;
100                 yo += o.y_;
101                 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
102                 // '<=' in order to take the last possible position
103                 // this is important for clicking behind \sum in e.g. '\sum_i a'
104                 LYXERR(Debug::DEBUG, "i: " << i << " d: " << d
105                         << " best: " << best_dist);
106                 if (d <= best_dist) {
107                         best_dist = d;
108                         result = it;
109                 }
110                 if (it == et)
111                         break;
112                 it.forwardPos();
113         }
114         return result;
115 }
116
117
118 } // namespace
119
120
121 CursorData::CursorData()
122         : DocIterator(), anchor_(),
123           selection_(false), mark_(false), word_selection_(false),
124           current_font(inherit_font),
125           autocorrect_(false), macromode_(false)
126 {}
127
128
129 CursorData::CursorData(Buffer * buffer)
130         : DocIterator(buffer), anchor_(),
131           selection_(false), mark_(false), word_selection_(false),
132           current_font(inherit_font),
133           autocorrect_(false), macromode_(false)
134 {}
135
136
137 CursorData::CursorData(DocIterator const & dit)
138         : DocIterator(dit), anchor_(),
139           selection_(false), mark_(false), word_selection_(false),
140           current_font(inherit_font),
141           autocorrect_(false), macromode_(false)
142 {}
143
144
145
146
147 ostream & operator<<(ostream & os, CursorData const & cur)
148 {
149         os << "\n cursor:                                | anchor:\n";
150         for (size_t i = 0, n = cur.depth(); i != n; ++i) {
151                 os << " " << cur[i] << " | ";
152                 if (i < cur.anchor_.depth())
153                         os << cur.anchor_[i];
154                 else
155                         os << "-------------------------------";
156                 os << "\n";
157         }
158         for (size_t i = cur.depth(), n = cur.anchor_.depth(); i < n; ++i) {
159                 os << "------------------------------- | " << cur.anchor_[i] << "\n";
160         }
161         os << " selection: " << cur.selection_
162 //         << " x_target: " << cur.x_target_
163            << " boundary: " << cur.boundary() << endl;
164         return os;
165 }
166
167
168 LyXErr & operator<<(LyXErr & os, CursorData const & cur)
169 {
170         os.stream() << cur;
171         return os;
172 }
173
174
175 // be careful: this is called from the bv's constructor, too, so
176 // bv functions are not yet available!
177 Cursor::Cursor(BufferView & bv)
178         : CursorData(&bv.buffer()), bv_(&bv),
179           x_target_(-1), textTargetOffset_(0),
180           beforeDispatchPosX_(0), beforeDispatchPosY_(0)
181 {}
182
183
184 void Cursor::reset()
185 {
186         clear();
187         push_back(CursorSlice(buffer()->inset()));
188         anchor_ = doc_iterator_begin(buffer());
189         anchor_.clear();
190         new_word_ = doc_iterator_begin(buffer());
191         new_word_.clear();
192         clearTargetX();
193         selection_ = false;
194         mark_ = false;
195 }
196
197
198 void Cursor::setCursor(DocIterator const & cur)
199 {
200         DocIterator::operator=(cur);
201 }
202
203
204 void Cursor::setCursorSelectionTo(DocIterator dit)
205 {
206         size_t i = 0;
207         // normalise dit
208         while (i < dit.depth() && i < anchor_.depth() && dit[i] == anchor_[i])
209                 ++i;
210         if (i != dit.depth()) {
211                 // otherwise the cursor is already normal
212                 if (i == anchor_.depth())
213                         // dit is a proper extension of the anchor_
214                         dit.cutOff(i - 1);
215                 else if (i + 1 < dit.depth()) {
216                         // one has dit[i] != anchor_[i] but either dit[i-1] == anchor_[i-1]
217                         // or i == 0. Remove excess.
218                         dit.cutOff(i);
219                         if (dit[i] > anchor_[i])
220                                 // place dit after the inset it was in
221                                 ++dit.pos();
222                 }
223         }
224         setCursor(dit);
225         setSelection();
226 }
227
228
229 void Cursor::setCursorToAnchor()
230 {
231         if (selection()) {
232                 DocIterator normal = anchor_;
233                 while (depth() < normal.depth())
234                         normal.pop_back();
235                 if (depth() < anchor_.depth() && top() <= anchor_[depth() - 1])
236                         ++normal.pos();
237                 setCursor(normal);
238         }
239 }
240
241
242 void Cursor::setCursorData(CursorData const & data)
243 {
244         CursorData::operator=(data);
245 }
246
247
248 bool Cursor::getStatus(FuncRequest const & cmd, FuncStatus & status) const
249 {
250         Cursor cur = *this;
251
252         // Try to fix cursor in case it is broken.
253         cur.fixIfBroken();
254
255         // Is this a function that acts on inset at point?
256         Inset * inset = cur.nextInset();
257         if (lyxaction.funcHasFlag(cmd.action(), LyXAction::AtPoint)
258             && inset && inset->getStatus(cur, cmd, status))
259                 return true;
260
261         // This is, of course, a mess. Better create a new doc iterator and use
262         // this in Inset::getStatus. This might require an additional
263         // BufferView * arg, though (which should be avoided)
264         //Cursor safe = *this;
265         bool res = false;
266         for ( ; cur.depth(); cur.pop()) {
267                 //lyxerr << "\nCursor::getStatus: cmd: " << cmd << endl << *this << endl;
268                 // LASSERT: Is it safe to continue here, or should we return?
269                 LASSERT(cur.idx() <= cur.lastidx(), /**/);
270                 LASSERT(cur.pit() <= cur.lastpit(), /**/);
271                 LASSERT(cur.pos() <= cur.lastpos(), /**/);
272
273                 // The inset's getStatus() will return 'true' if it made
274                 // a definitive decision on whether it want to handle the
275                 // request or not. The result of this decision is put into
276                 // the 'status' parameter.
277                 if (cur.inset().getStatus(cur, cmd, status)) {
278                         res = true;
279                         break;
280                 }
281         }
282         return res;
283 }
284
285
286 void Cursor::saveBeforeDispatchPosXY()
287 {
288         getPos(beforeDispatchPosX_, beforeDispatchPosY_);
289 }
290
291
292 void Cursor::dispatch(FuncRequest const & cmd0)
293 {
294         LYXERR(Debug::ACTION, "Cursor::dispatch: cmd: " << cmd0 << '\n' << *this);
295         if (empty())
296                 return;
297
298         fixIfBroken();
299         FuncRequest cmd = cmd0;
300         Cursor safe = *this;
301         Cursor old = *this;
302         disp_ = DispatchResult();
303
304         beginUndoGroup();
305
306         // Is this a function that acts on inset at point?
307         if (lyxaction.funcHasFlag(cmd.action(), LyXAction::AtPoint)
308             && nextInset()) {
309                 disp_.dispatched(true);
310                 disp_.screenUpdate(Update::FitCursor | Update::Force);
311                 FuncRequest tmpcmd = cmd;
312                 LYXERR(Debug::DEBUG, "Cursor::dispatch: (AtPoint) cmd: "
313                         << cmd0 << endl << *this);
314                 nextInset()->dispatch(*this, tmpcmd);
315                 if (disp_.dispatched()) {
316                         endUndoGroup();
317                         return;
318                 }
319         }
320
321         // store some values to be used inside of the handlers
322         beforeDispatchCursor_ = *this;
323         for (; depth(); pop(), boundary(false)) {
324                 LYXERR(Debug::DEBUG, "Cursor::dispatch: cmd: "
325                         << cmd0 << endl << *this);
326
327                 // In any of these cases, the cursor is invalid, and we should
328                 // try to save this document rather than crash.
329                 LBUFERR(pos() <= lastpos());
330                 LBUFERR(idx() <= lastidx());
331                 LBUFERR(pit() <= lastpit());
332
333                 // The common case is 'LFUN handled, need update', so make the
334                 // LFUN handler's life easier by assuming this as default value.
335                 // The handler can reset the update and val flags if necessary.
336                 disp_.screenUpdate(Update::FitCursor | Update::Force);
337                 disp_.dispatched(true);
338                 inset().dispatch(*this, cmd);
339                 if (disp_.dispatched())
340                         break;
341         }
342
343         // it completely to get a 'bomb early' behaviour in case this
344         // object will be used again.
345         if (!disp_.dispatched()) {
346                 LYXERR(Debug::DEBUG, "RESTORING OLD CURSOR!");
347                 // We might have invalidated the cursor when removing an empty
348                 // paragraph while the cursor could not be moved out the inset
349                 // while we initially thought we could. This might happen when
350                 // a multiline inset becomes an inline inset when the second
351                 // paragraph is removed.
352                 if (safe.pit() > safe.lastpit()) {
353                         safe.pit() = safe.lastpit();
354                         safe.pos() = safe.lastpos();
355                 }
356                 operator=(safe);
357                 disp_.screenUpdate(Update::None);
358                 disp_.dispatched(false);
359         } else {
360                 // restore the previous one because nested Cursor::dispatch calls
361                 // are possible which would change it
362                 beforeDispatchCursor_ = safe.beforeDispatchCursor_;
363         }
364         endUndoGroup();
365
366         // NOTE: The code below has been copied to BufferView::dispatch.
367         // If you need to modify this, please update the other one too.
368
369         // notify insets we just left
370         if (*this != old) {
371                 old.beginUndoGroup();
372                 old.fixIfBroken();
373                 bool badcursor = notifyCursorLeavesOrEnters(old, *this);
374                 if (badcursor) {
375                         fixIfBroken();
376                         bv().resetInlineCompletionPos();
377                 }
378                 old.endUndoGroup();
379         }
380 }
381
382
383 DispatchResult const & Cursor::result() const
384 {
385         return disp_;
386 }
387
388
389 BufferView & Cursor::bv() const
390 {
391         LBUFERR(bv_);
392         return *bv_;
393 }
394
395
396 void Cursor::pop()
397 {
398         LBUFERR(depth() >= 1);
399         pop_back();
400 }
401
402
403 void Cursor::push(Inset & p)
404 {
405         push_back(CursorSlice(p));
406         p.setBuffer(*buffer());
407 }
408
409
410 void Cursor::pushBackward(Inset & p)
411 {
412         LASSERT(!empty(), return);
413         //lyxerr << "Entering inset " << t << " front" << endl;
414         push(p);
415         p.idxFirst(*this);
416 }
417
418
419 bool Cursor::popBackward()
420 {
421         LASSERT(!empty(), return false);
422         if (depth() == 1)
423                 return false;
424         pop();
425         return true;
426 }
427
428
429 bool Cursor::popForward()
430 {
431         LASSERT(!empty(), return false);
432         //lyxerr << "Leaving inset from in back" << endl;
433         const pos_type lp = (depth() > 1) ? (*this)[depth() - 2].lastpos() : 0;
434         if (depth() == 1)
435                 return false;
436         pop();
437         pos() += lastpos() - lp + 1;
438         return true;
439 }
440
441
442 int Cursor::currentMode()
443 {
444         LASSERT(!empty(), return Inset::UNDECIDED_MODE);
445         for (int i = depth() - 1; i >= 0; --i) {
446                 int res = operator[](i).inset().currentMode();
447                 bool locked_mode = operator[](i).inset().lockedMode();
448                 // Also return UNDECIDED_MODE when the mode is locked,
449                 // as in this case it is treated the same as TEXT_MODE
450                 if (res != Inset::UNDECIDED_MODE || locked_mode)
451                         return res;
452         }
453         return Inset::TEXT_MODE;
454 }
455
456
457 void Cursor::getPos(int & x, int & y) const
458 {
459         Point p = bv().getPos(*this);
460         x = p.x_;
461         y = p.y_;
462 }
463
464
465 Row const & Cursor::textRow() const
466 {
467         CursorSlice const & cs = innerTextSlice();
468         ParagraphMetrics const & pm = bv().parMetrics(cs.text(), cs.pit());
469         return pm.getRow(pos(), boundary());
470 }
471
472
473 void Cursor::resetAnchor()
474 {
475         anchor_ = *this;
476         checkNewWordPosition();
477 }
478
479
480 void Cursor::markNewWordPosition()
481 {
482         if (lyxrc.spellcheck_continuously && inTexted() && new_word_.empty()) {
483                 FontSpan nw = locateWord(WHOLE_WORD);
484                 if (nw.size() == 1) {
485                         LYXERR(Debug::DEBUG, "start new word: "
486                                 << " par: " << pit()
487                                 << " pos: " << nw.first);
488                         new_word_ = *this;
489                 }
490         }
491 }
492
493
494 void Cursor::clearNewWordPosition()
495 {
496         if (!new_word_.empty()) {
497                 LYXERR(Debug::DEBUG, "clear new word: "
498                         << " par: " << pit()
499                         << " pos: " << pos());
500                 new_word_.resize(0);
501         }
502 }
503
504
505 void Cursor::checkNewWordPosition()
506 {
507         if (!lyxrc.spellcheck_continuously || new_word_.empty())
508                 return ;
509         if (!inTexted())
510                 clearNewWordPosition();
511         else {
512                 // forget the position of the current new word if
513                 // 1) the paragraph changes or
514                 // 2) the count of nested insets changes or
515                 // 3) the cursor pos is out of paragraph bound
516                 if (pit() != new_word_.pit() ||
517                         depth() != new_word_.depth() ||
518                         new_word_.pos() > new_word_.lastpos()) {
519                         clearNewWordPosition();
520                 } else if (new_word_.fixIfBroken())
521                         // 4) or the remembered position was "broken"
522                         clearNewWordPosition();
523                 else {
524                         FontSpan nw = locateWord(WHOLE_WORD);
525                         if (!nw.empty()) {
526                                 FontSpan ow = new_word_.locateWord(WHOLE_WORD);
527                                 if (nw.intersect(ow).empty())
528                                         clearNewWordPosition();
529                                 else
530                                         LYXERR(Debug::DEBUG, "new word: "
531                                                    << " par: " << pit()
532                                                    << " pos: " << nw.first << ".." << nw.last);
533                         } else {
534                                 clearNewWordPosition();
535                         }
536                 }
537         }
538 }
539
540
541 bool Cursor::posVisRight(bool skip_inset)
542 {
543         Cursor new_cur = *this; // where we will move to
544         pos_type left_pos; // position visually left of current cursor
545         pos_type right_pos; // position visually right of current cursor
546
547         getSurroundingPos(left_pos, right_pos);
548
549         LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
550
551         // Are we at an inset?
552         new_cur.pos() = right_pos;
553         new_cur.boundary(false);
554         if (!skip_inset &&
555                 text()->checkAndActivateInsetVisual(new_cur, right_pos >= pos(), false)) {
556                 // we actually move the cursor at the end of this
557                 // function, for now we just keep track of the new
558                 // position in new_cur...
559                 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
560         }
561
562         // Are we already at rightmost pos in row?
563         else if (text()->empty() || right_pos == -1) {
564
565                 new_cur = *this;
566                 if (!new_cur.posVisToNewRow(false)) {
567                         LYXERR(Debug::RTL, "not moving!");
568                         return false;
569                 }
570
571                 // we actually move the cursor at the end of this
572                 // function, for now just keep track of the new
573                 // position in new_cur...
574                 LYXERR(Debug::RTL, "right edge, moving: " << int(new_cur.pit()) << ","
575                         << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
576
577         }
578         // normal movement to the right
579         else {
580                 new_cur = *this;
581                 // Recall, if the cursor is at position 'x', that
582                 // means *before* the character at position 'x'. In
583                 // RTL, "before" means "to the right of", in LTR, "to
584                 // the left of". So currently our situation is this:
585                 // the position to our right is 'right_pos' (i.e.,
586                 // we're currently to the left of 'right_pos'). In
587                 // order to move to the right, it depends whether or
588                 // not the character at 'right_pos' is RTL.
589                 bool const new_pos_is_RTL = paragraph().getFontSettings(
590                         buffer()->params(), right_pos).isVisibleRightToLeft();
591                 // If the character at 'right_pos' *is* LTR, then in
592                 // order to move to the right of it, we need to be
593                 // *after* 'right_pos', i.e., move to position
594                 // 'right_pos' + 1.
595                 if (!new_pos_is_RTL) {
596                         new_cur.pos() = right_pos + 1;
597                         // set the boundary to true in two situations:
598                         if (
599                         // 1. if new_pos is now lastpos, and we're in
600                         // an RTL paragraph (this means that we're
601                         // moving right to the end of an LTR chunk
602                         // which is at the end of an RTL paragraph);
603                                 (new_cur.pos() == lastpos()
604                                  && paragraph().isRTL(buffer()->params()))
605                         // 2. if the position *after* right_pos is RTL
606                         // (we want to be *after* right_pos, not
607                         // before right_pos + 1!)
608                                 || paragraph().getFontSettings(buffer()->params(),
609                                                 new_cur.pos()).isVisibleRightToLeft()
610                         )
611                                 new_cur.boundary(true);
612                         else // set the boundary to false
613                                 new_cur.boundary(false);
614                 }
615                 // Otherwise (if the character at position 'right_pos'
616                 // is RTL), then moving to the right of it is as easy
617                 // as setting the new position to 'right_pos'.
618                 else {
619                         new_cur.pos() = right_pos;
620                         new_cur.boundary(false);
621                 }
622
623         }
624
625         bool const moved = new_cur != *this || new_cur.boundary() != boundary();
626
627         if (moved) {
628                 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
629                         << (new_cur.boundary() ? " (boundary)" : ""));
630                 *this = new_cur;
631         }
632
633         return moved;
634 }
635
636
637 bool Cursor::posVisLeft(bool skip_inset)
638 {
639         Cursor new_cur = *this; // where we will move to
640         pos_type left_pos; // position visually left of current cursor
641         pos_type right_pos; // position visually right of current cursor
642
643         getSurroundingPos(left_pos, right_pos);
644
645         LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
646
647         // Are we at an inset?
648         new_cur.pos() = left_pos;
649         new_cur.boundary(false);
650         if (!skip_inset &&
651                 text()->checkAndActivateInsetVisual(new_cur, left_pos >= pos(), true)) {
652                 // we actually move the cursor at the end of this
653                 // function, for now we just keep track of the new
654                 // position in new_cur...
655                 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
656         }
657
658         // Are we already at leftmost pos in row?
659         else if (text()->empty() || left_pos == -1) {
660
661                 new_cur = *this;
662                 if (!new_cur.posVisToNewRow(true)) {
663                         LYXERR(Debug::RTL, "not moving!");
664                         return false;
665                 }
666
667                 // we actually move the cursor at the end of this
668                 // function, for now just keep track of the new
669                 // position in new_cur...
670                 LYXERR(Debug::RTL, "left edge, moving: " << int(new_cur.pit()) << ","
671                         << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
672
673         }
674         // normal movement to the left
675         else {
676                 new_cur = *this;
677                 // Recall, if the cursor is at position 'x', that
678                 // means *before* the character at position 'x'. In
679                 // RTL, "before" means "to the right of", in LTR, "to
680                 // the left of". So currently our situation is this:
681                 // the position to our left is 'left_pos' (i.e., we're
682                 // currently to the right of 'left_pos'). In order to
683                 // move to the left, it depends whether or not the
684                 // character at 'left_pos' is RTL.
685                 bool const new_pos_is_RTL = paragraph().getFontSettings(
686                         buffer()->params(), left_pos).isVisibleRightToLeft();
687                 // If the character at 'left_pos' *is* RTL, then in
688                 // order to move to the left of it, we need to be
689                 // *after* 'left_pos', i.e., move to position
690                 // 'left_pos' + 1.
691                 if (new_pos_is_RTL) {
692                         new_cur.pos() = left_pos + 1;
693                         // set the boundary to true in two situations:
694                         if (
695                         // 1. if new_pos is now lastpos and we're in
696                         // an LTR paragraph (this means that we're
697                         // moving left to the end of an RTL chunk
698                         // which is at the end of an LTR paragraph);
699                                 (new_cur.pos() == lastpos()
700                                  && !paragraph().isRTL(buffer()->params()))
701                         // 2. if the position *after* left_pos is not
702                         // RTL (we want to be *after* left_pos, not
703                         // before left_pos + 1!)
704                                 || !paragraph().getFontSettings(buffer()->params(),
705                                                 new_cur.pos()).isVisibleRightToLeft()
706                         )
707                                 new_cur.boundary(true);
708                         else // set the boundary to false
709                                 new_cur.boundary(false);
710                 }
711                 // Otherwise (if the character at position 'left_pos'
712                 // is LTR), then moving to the left of it is as easy
713                 // as setting the new position to 'left_pos'.
714                 else {
715                         new_cur.pos() = left_pos;
716                         new_cur.boundary(false);
717                 }
718
719         }
720
721         bool const moved = new_cur != *this || new_cur.boundary() != boundary();
722
723         if (moved) {
724                 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
725                         << (new_cur.boundary() ? " (boundary)" : ""));
726                 *this = new_cur;
727         }
728
729         return moved;
730 }
731
732
733 namespace {
734
735 // Return true on success
736 bool findNonVirtual(Row const & row, Row::const_iterator & cit, bool onleft)
737 {
738         if (onleft) {
739                 while (cit != row.begin() && cit->isVirtual())
740                         --cit;
741         } else {
742                 while (cit != row.end() && cit->isVirtual())
743                         ++cit;
744         }
745         return cit != row.end() && !cit->isVirtual();
746 }
747
748 } // namespace
749
750 void Cursor::getSurroundingPos(pos_type & left_pos, pos_type & right_pos) const
751 {
752         // by default, we know nothing.
753         left_pos = -1;
754         right_pos = -1;
755
756         Row const & row = textRow();
757         double dummy = 0;
758         Row::const_iterator cit = row.findElement(pos(), boundary(), dummy);
759         // Handle the case of empty row
760         if (cit == row.end()) {
761                 if (row.isRTL())
762                         right_pos = row.pos();
763                 else
764                         left_pos = row.pos() - 1;
765                 return;
766         }
767
768         // skip virtual elements and exit if no non-virtual one exists
769         if (!findNonVirtual(row, cit, !cit->isRTL()))
770                 return;
771
772         // if the position is at the left side of the element, we have to
773         // look at the previous element
774         if (pos() == cit->left_pos()) {
775                 LYXERR(Debug::RTL, "getSurroundingPos(" << pos() << (boundary() ? "b" : "")
776                            << "), AT LEFT of *cit=" << *cit);
777                 // this one is easy (see common case below)
778                 right_pos = pos() - (cit->isRTL() ? 1 : 0);
779                 // at the left of the row
780                 if (cit == row.begin())
781                         return;
782                 --cit;
783                 if (!findNonVirtual(row, cit, true))
784                         return;
785                 // [...[ is the row element, | is cursor position (! with boundary)
786                 // [ 1 2 [ is a ltr row element with pos=1 and endpos=3
787                 // ] 2 1] is an rtl row element with pos=1 and endpos=3
788                 //    [ 1 2 [  [|3 4 [ => (2, 3)
789                 // or [ 1 2 [  ]!4 3 ] => (2, 4)
790                 // or ] 2 1 ]  [|3 4 [ => (1, 3)
791                 // or ] 4 3 ]  ]!2 1 ] => (3, 2)
792                 left_pos = cit->right_pos() - (cit->isRTL() ? 0 : 1);
793                 // happens with consecutive row of same direction
794                 if (left_pos == right_pos) {
795                         left_pos += cit->isRTL() ? 1 : -1;
796                 }
797         }
798         // same code but with the element at the right
799         else if (pos() == cit->right_pos()) {
800                 LYXERR(Debug::RTL, "getSurroundingPos(" << pos() << (boundary() ? "b" : "")
801                            << "), AT RIGHT of *cit=" << *cit);
802                 // this one is easy (see common case below)
803                 left_pos = pos() - (cit->isRTL() ? 0 : 1);
804                 // at the right of the row
805                 if (cit + 1 == row.end())
806                         return;
807                 ++cit;
808                 if (!findNonVirtual(row, cit, false))
809                         return;
810                 //    [ 1 2![  [ 3 4 [ => (2, 3)
811                 // or [ 1 2![  ] 4 3 ] => (2, 4)
812                 // or ] 2 1|]  [ 3 4 [ => (1, 3)
813                 // or ] 4 3|]  ] 2 1 ] => (3, 2)
814                 right_pos = cit->left_pos() - (cit->isRTL() ? 1 : 0);
815                 // happens with consecutive row of same direction
816                 if (right_pos == left_pos)
817                         right_pos += cit->isRTL() ? -1 : 1;
818         }
819         // common case: both positions are inside the row element
820         else {
821                 //    [ 1 2|3 [ => (2, 3)
822                 // or ] 3|2 1 ] => (3, 2)
823                 left_pos = pos() - (cit->isRTL() ? 0 : 1);
824                 right_pos = pos() - (cit->isRTL() ? 1 : 0);
825         }
826
827         // Note that debug message does not catch all early returns above
828         LYXERR(Debug::RTL,"getSurroundingPos(" << pos() << (boundary() ? "b" : "")
829                    << ") => (" << left_pos << ", " << right_pos <<")");
830 }
831
832
833 bool Cursor::posVisToNewRow(bool movingLeft)
834 {
835         Row const & row = textRow();
836         bool par_is_LTR = !row.isRTL();
837
838         // Inside a table, determining whether to move to the next or
839         // previous row should be done based on the table's direction.
840         if (inset().asInsetTabular()) {
841                 par_is_LTR = !inset().asInsetTabular()->isRightToLeft(*this);
842                 LYXERR(Debug::RTL, "Inside table! par_is_LTR=" << (par_is_LTR ? 1 : 0));
843         }
844
845         // if moving left in an LTR paragraph or moving right in an
846         // RTL one, move to previous row
847         if (par_is_LTR == movingLeft) {
848                 if (row.pos() == 0) { // we're at first row in paragraph
849                         if (pit() == 0) // no previous paragraph! don't move
850                                 return false;
851                         // move to last pos in previous par
852                         --pit();
853                         pos() = lastpos();
854                         boundary(false);
855                 } else { // move to previous row in this par
856                         pos() = row.pos() - 1; // this is guaranteed to be in previous row
857                         boundary(false);
858                 }
859         }
860         // if moving left in an RTL paragraph or moving right in an
861         // LTR one, move to next row
862         else {
863                 if (row.endpos() == lastpos()) { // we're at last row in paragraph
864                         if (pit() == lastpit()) // last paragraph! don't move
865                                 return false;
866                         // move to first row in next par
867                         ++pit();
868                         pos() = 0;
869                         boundary(false);
870                 } else { // move to next row in this par
871                         pos() = row.endpos();
872                         boundary(false);
873                 }
874         }
875
876         // make sure we're at left-/right-most pos in new row
877         posVisToRowExtremity(!movingLeft);
878
879         return true;
880 }
881
882
883 void Cursor::posVisToRowExtremity(bool left)
884 {
885         LYXERR(Debug::RTL, "entering extremity: " << pit() << "," << pos() << ","
886                 << (boundary() ? 1 : 0));
887
888         TextMetrics const & tm = bv_->textMetrics(text());
889         // Looking for extremities is like clicking on the left or the
890         // right of the row.
891         int x = tm.origin().x_ + (left ? 0 : textRow().width());
892         bool b = false;
893         pos() = tm.getPosNearX(textRow(), x, b);
894         boundary(b);
895
896         LYXERR(Debug::RTL, "leaving extremity: " << pit() << "," << pos() << ","
897                 << (boundary() ? 1 : 0));
898 }
899
900
901 bool Cursor::reverseDirectionNeeded() const
902 {
903         /*
904          * We determine the directions based on the direction of the
905          * bottom() --- i.e., outermost --- paragraph, because that is
906          * the only way to achieve consistency of the arrow's movements
907          * within a paragraph, and thus avoid situations in which the
908          * cursor gets stuck.
909          */
910         return bottom().paragraph().isRTL(bv().buffer().params());
911 }
912
913
914 CursorSlice Cursor::normalAnchor() const
915 {
916         if (!selection())
917                 return top();
918         // LASSERT: There have been several bugs around this code, that seem
919         // to involve failures to reset the anchor. We can at least not crash
920         // in release mode by resetting it ourselves.
921         if (anchor_.depth() < depth()) {
922                 LYXERR0("Cursor is deeper than anchor. PLEASE REPORT.\nCursor is"
923                         << *this);
924                 const_cast<DocIterator &>(anchor_) = *this;
925         }
926
927         CursorSlice normal = anchor_[depth() - 1];
928         if (depth() < anchor_.depth() && top() <= normal) {
929                 // anchor is behind cursor -> move anchor behind the inset
930                 ++normal.pos();
931         }
932         return normal;
933 }
934
935
936 CursorSlice Cursor::selBegin() const
937 {
938         if (!selection())
939                 return top();
940         return normalAnchor() < top() ? normalAnchor() : top();
941 }
942
943
944 CursorSlice Cursor::selEnd() const
945 {
946         if (!selection())
947                 return top();
948         return normalAnchor() > top() ? normalAnchor() : top();
949 }
950
951
952 DocIterator Cursor::selectionBegin() const
953 {
954         if (!selection())
955                 return *this;
956
957         DocIterator di;
958         // FIXME: This is a work-around for the problem that
959         // CursorSlice doesn't keep track of the boundary.
960         if (normalAnchor() == top())
961                 di = anchor_.boundary() > boundary() ? anchor_ : *this;
962         else
963                 di = normalAnchor() < top() ? anchor_ : *this;
964         di.resize(depth());
965         return di;
966 }
967
968
969 DocIterator Cursor::selectionEnd() const
970 {
971         if (!selection())
972                 return *this;
973
974         DocIterator di;
975         // FIXME: This is a work-around for the problem that
976         // CursorSlice doesn't keep track of the boundary.
977         if (normalAnchor() == top())
978                 di = anchor_.boundary() < boundary() ? anchor_ : *this;
979         else
980                 di = normalAnchor() > top() ? anchor_ : *this;
981
982         if (di.depth() > depth()) {
983                 di.resize(depth());
984                 ++di.pos();
985         }
986         return di;
987 }
988
989
990 void Cursor::setSelection()
991 {
992         selection(true);
993         if (idx() == normalAnchor().idx() &&
994             pit() == normalAnchor().pit() &&
995             pos() == normalAnchor().pos())
996                 selection(false);
997 }
998
999
1000 void Cursor::setSelection(DocIterator const & where, int n)
1001 {
1002         setCursor(where);
1003         selection(true);
1004         anchor_ = where;
1005         pos() += n;
1006 }
1007
1008
1009 void Cursor::clearSelection()
1010 {
1011         selection(false);
1012         setWordSelection(false);
1013         setMark(false);
1014         resetAnchor();
1015 }
1016
1017
1018 void Cursor::setTargetX(int x)
1019 {
1020         x_target_ = x;
1021         textTargetOffset_ = 0;
1022 }
1023
1024
1025 int Cursor::x_target() const
1026 {
1027         return x_target_;
1028 }
1029
1030
1031 void Cursor::clearTargetX()
1032 {
1033         x_target_ = -1;
1034         textTargetOffset_ = 0;
1035 }
1036
1037
1038 void Cursor::updateTextTargetOffset()
1039 {
1040         int x;
1041         int y;
1042         getPos(x, y);
1043         textTargetOffset_ = x - x_target_;
1044 }
1045
1046
1047 void Cursor::info(odocstream & os, bool devel_mode) const
1048 {
1049         for (int i = 1, n = depth(); i < n; ++i) {
1050                 operator[](i).inset().infoize(os);
1051                 os << "  ";
1052         }
1053         if (pos() != 0) {
1054                 Inset const * inset = prevInset();
1055                 // prevInset() can return 0 in certain case.
1056                 if (inset)
1057                         prevInset()->infoize2(os);
1058         }
1059         if (devel_mode) {
1060                 InsetMath * math = inset().asInsetMath();
1061                 if (math)
1062                         os << _(", Inset: ") << math->id();
1063                 os << _(", Cell: ") << idx();
1064                 os << _(", Position: ") << pos();
1065         }
1066
1067 }
1068
1069
1070 bool Cursor::selHandle(bool sel)
1071 {
1072         //lyxerr << "Cursor::selHandle" << endl;
1073         if (mark())
1074                 sel = true;
1075         if (sel == selection())
1076                 return false;
1077
1078         if (!sel)
1079                 cap::saveSelection(*this);
1080
1081         resetAnchor();
1082         selection(sel);
1083         return true;
1084 }
1085 } // namespace lyx
1086
1087
1088 ///////////////////////////////////////////////////////////////////
1089 //
1090 // FIXME: Look here
1091 // The part below is the non-integrated rest of the original math
1092 // cursor. This should be either generalized for texted or moved
1093 // back to mathed (in most cases to InsetMathNest).
1094 //
1095 ///////////////////////////////////////////////////////////////////
1096
1097 #include "mathed/InsetMathChar.h"
1098 #include "mathed/InsetMathGrid.h"
1099 #include "mathed/InsetMathScript.h"
1100 #include "mathed/InsetMathUnknown.h"
1101 #include "mathed/MathFactory.h"
1102 #include "mathed/MathStream.h"
1103 #include "mathed/MathSupport.h"
1104
1105
1106 namespace lyx {
1107
1108 bool Cursor::isInside(Inset const * p) const
1109 {
1110         for (size_t i = 0; i != depth(); ++i)
1111                 if (&operator[](i).inset() == p)
1112                         return true;
1113         return false;
1114 }
1115
1116
1117 void Cursor::leaveInset(Inset const & inset)
1118 {
1119         for (size_t i = 0; i != depth(); ++i) {
1120                 if (&operator[](i).inset() == &inset) {
1121                         resize(i);
1122                         return;
1123                 }
1124         }
1125 }
1126
1127
1128 bool Cursor::openable(MathAtom const & t) const
1129 {
1130         if (!t->isActive())
1131                 return false;
1132
1133         if (t->lock())
1134                 return false;
1135
1136         if (!selection())
1137                 return true;
1138
1139         // we can't move into anything new during selection
1140         if (depth() >= anchor_.depth())
1141                 return false;
1142         if (t.nucleus() != &anchor_[depth()].inset())
1143                 return false;
1144
1145         return true;
1146 }
1147
1148
1149 void Cursor::setScreenPos(int x, int /*y*/)
1150 {
1151         setTargetX(x);
1152         //bruteFind(*this, x, y, 0, bv().workWidth(), 0, bv().workHeight());
1153 }
1154
1155
1156
1157 void Cursor::plainErase()
1158 {
1159         cell().erase(pos());
1160 }
1161
1162
1163 void Cursor::markInsert()
1164 {
1165         insert(char_type(0));
1166 }
1167
1168
1169 void Cursor::markErase()
1170 {
1171         cell().erase(pos());
1172 }
1173
1174
1175 void Cursor::plainInsert(MathAtom const & t)
1176 {
1177         cell().insert(pos(), t);
1178         ++pos();
1179         inset().setBuffer(bv_->buffer());
1180         inset().initView();
1181         checkBufferStructure();
1182 }
1183
1184
1185 void Cursor::insert(docstring const & str)
1186 {
1187         for (char_type c : str)
1188                 insert(c);
1189 }
1190
1191
1192 void Cursor::insert(char_type c)
1193 {
1194         //lyxerr << "Cursor::insert char '" << c << "'" << endl;
1195         LASSERT(!empty(), return);
1196         if (inMathed()) {
1197                 cap::selClearOrDel(*this);
1198                 insert(new InsetMathChar(c));
1199         } else {
1200                 text()->insertChar(*this, c);
1201         }
1202 }
1203
1204
1205 void Cursor::insert(MathAtom const & t)
1206 {
1207         //lyxerr << "Cursor::insert MathAtom '" << t << "'" << endl;
1208         macroModeClose();
1209         cap::selClearOrDel(*this);
1210         plainInsert(t);
1211 }
1212
1213
1214 void Cursor::insert(Inset * inset0)
1215 {
1216         LASSERT(inset0, return);
1217         if (inMathed())
1218                 insert(MathAtom(inset0->asInsetMath()));
1219         else {
1220                 text()->insertInset(*this, inset0);
1221                 inset0->setBuffer(bv_->buffer());
1222                 inset0->initView();
1223                 if (inset0->isLabeled())
1224                         forceBufferUpdate();
1225         }
1226 }
1227
1228
1229 int Cursor::niceInsert(docstring const & t, Parse::flags f, bool enter)
1230 {
1231         MathData ar(buffer());
1232         asArray(t, ar, f);
1233         if (ar.size() == 1 && (enter || selection()))
1234                 niceInsert(ar[0]);
1235         else
1236                 insert(ar);
1237         return ar.size();
1238 }
1239
1240
1241 void Cursor::niceInsert(MathAtom const & t)
1242 {
1243         macroModeClose();
1244         docstring const safe = cap::grabAndEraseSelection(*this);
1245         plainInsert(t);
1246         // If possible, enter the new inset and move the contents of the selection
1247         if (t->isActive()) {
1248                 posBackward();
1249                 // be careful here: don't use 'pushBackward(t)' as this we need to
1250                 // push the clone, not the original
1251                 pushBackward(*nextInset());
1252                 // We may not use niceInsert here (recursion)
1253                 MathData ar(buffer());
1254                 asArray(safe, ar);
1255                 insert(ar);
1256         } else if (t->asMacro() && !safe.empty()) {
1257                 MathData ar(buffer());
1258                 asArray(safe, ar);
1259                 docstring const name = t->asMacro()->name();
1260                 MacroData const * data = buffer()->getMacro(name);
1261                 if (data && data->numargs() - data->optionals() > 0) {
1262                         plainInsert(MathAtom(new InsetMathBrace(ar)));
1263                         posBackward();
1264                 }
1265         }
1266 }
1267
1268
1269 void Cursor::insert(MathData const & ar)
1270 {
1271         macroModeClose();
1272         if (selection())
1273                 cap::eraseSelection(*this);
1274         cell().insert(pos(), ar);
1275         pos() += ar.size();
1276         // FIXME audit setBuffer calls
1277         inset().setBuffer(bv_->buffer());
1278 }
1279
1280
1281 bool Cursor::backspace(bool const force)
1282 {
1283         if (selection()) {
1284                 cap::eraseSelection(*this);
1285                 return true;
1286         }
1287
1288         if (pos() == 0) {
1289                 // If empty cell, and not part of a big cell
1290                 if (lastpos() == 0 && inset().nargs() == 1) {
1291                         popBackward();
1292                         // Directly delete empty cell: [|[]] => [|]
1293                         if (inMathed()) {
1294                                 plainErase();
1295                                 resetAnchor();
1296                                 return true;
1297                         }
1298                         // [|], can not delete from inside
1299                         return false;
1300                 } else {
1301                         if (inMathed())
1302                                 pullArg();
1303                         else
1304                                 popBackward();
1305                         return true;
1306                 }
1307         }
1308
1309         if (inMacroMode()) {
1310                 InsetMathUnknown * p = activeMacro();
1311                 if (p->name().size() > 1) {
1312                         p->setName(p->name().substr(0, p->name().size() - 1));
1313                         return true;
1314                 }
1315         }
1316
1317         if (pos() != 0 && !force && prevAtom()->confirmDeletion()) {
1318                 // let's require two backspaces for 'big stuff' and
1319                 // highlight on the first
1320                 resetAnchor();
1321                 selection(true);
1322                 --pos();
1323         } else {
1324                 --pos();
1325                 plainErase();
1326         }
1327         return true;
1328 }
1329
1330
1331 bool Cursor::erase(bool const force)
1332 {
1333         if (inMacroMode())
1334                 return true;
1335
1336         if (selection()) {
1337                 cap::eraseSelection(*this);
1338                 return true;
1339         }
1340
1341         // delete empty cells if possible
1342         if (pos() == lastpos() && inset().idxDelete(idx()))
1343                 return true;
1344
1345         // special behaviour when in last position of cell
1346         if (pos() == lastpos()) {
1347                 bool one_cell = inset().nargs() == 1;
1348                 if (one_cell && lastpos() == 0) {
1349                         popBackward();
1350                         // Directly delete empty cell: [|[]] => [|]
1351                         if (inMathed()) {
1352                                 plainErase();
1353                                 resetAnchor();
1354                                 return true;
1355                         }
1356                         // [|], can not delete from inside
1357                         return false;
1358                 }
1359                 // remove markup
1360                 if (!one_cell)
1361                         inset().idxGlue(idx());
1362                 return true;
1363         }
1364
1365         // 'clever' UI hack: only erase large items if previously slected
1366         if (pos() != lastpos() && !force && nextAtom()->confirmDeletion()) {
1367                 resetAnchor();
1368                 selection(true);
1369                 ++pos();
1370         } else {
1371                 plainErase();
1372         }
1373
1374         return true;
1375 }
1376
1377
1378 bool Cursor::up()
1379 {
1380         macroModeClose();
1381         DocIterator save = *this;
1382         FuncRequest cmd(selection() ? LFUN_UP_SELECT : LFUN_UP, docstring());
1383         this->dispatch(cmd);
1384         if (disp_.dispatched())
1385                 return true;
1386         setCursor(save);
1387         return false;
1388 }
1389
1390
1391 bool Cursor::down()
1392 {
1393         macroModeClose();
1394         DocIterator save = *this;
1395         FuncRequest cmd(selection() ? LFUN_DOWN_SELECT : LFUN_DOWN, docstring());
1396         this->dispatch(cmd);
1397         if (disp_.dispatched())
1398                 return true;
1399         setCursor(save);
1400         return false;
1401 }
1402
1403
1404 bool Cursor::macroModeClose()
1405 {
1406         if (!inMacroMode())
1407                 return false;
1408         InsetMathUnknown * p = activeMacro();
1409         p->finalize();
1410         MathData selection(buffer());
1411         asArray(p->selection(), selection);
1412         docstring const s = p->name();
1413         --pos();
1414         cell().erase(pos());
1415
1416         // do nothing if the macro name is empty
1417         if (s == "\\")
1418                 return false;
1419
1420         // trigger updates of macros, at least, if no full
1421         // updates take place anyway
1422         screenUpdateFlags(Update::Force);
1423
1424         docstring const name = s.substr(1);
1425         InsetMathNest * const in = inset().asInsetMath()->asNestInset();
1426         if (in && in->interpretString(*this, s))
1427                 return true;
1428         bool const user_macro = buffer()->getMacro(name, *this, false);
1429         MathAtom atom = user_macro ? MathAtom(new InsetMathMacro(buffer(), name))
1430                                    : createInsetMath(name, buffer());
1431
1432         // try to put argument into macro, if we just inserted a macro
1433         bool macroArg = false;
1434         InsetMathMacro * atomAsMacro = atom.nucleus()->asMacro();
1435         if (atomAsMacro) {
1436                 // macros here are still unfolded (in init mode in fact). So
1437                 // we have to resolve the macro here manually and check its arity
1438                 // to put the selection behind it if arity > 0.
1439                 MacroData const * data = buffer()->getMacro(atomAsMacro->name());
1440                 if (!selection.empty() && data && data->numargs() - data->optionals() > 0) {
1441                         macroArg = true;
1442                         atomAsMacro->setDisplayMode(InsetMathMacro::DISPLAY_INTERACTIVE_INIT, 1);
1443                 } else
1444                         // non-greedy case. Do not touch the arguments behind
1445                         atomAsMacro->setDisplayMode(InsetMathMacro::DISPLAY_INTERACTIVE_INIT, 0);
1446         }
1447
1448         // insert remembered selection into first argument of a non-macro
1449         else if (atom.nucleus()->nargs() > 0)
1450                 atom.nucleus()->cell(0).append(selection);
1451
1452         MathWordList const & words = mathedWordList();
1453         MathWordList::const_iterator it = words.find(name);
1454         bool keep_mathmode = user_macro
1455                 || (it != words.end() && (it->second.inset == "font"
1456                                           || it->second.inset == "oldfont"
1457                                           || it->second.inset == "mbox"));
1458         bool ert_macro = !user_macro && it == words.end() && atomAsMacro;
1459
1460         if (in && in->currentMode() == Inset::TEXT_MODE
1461             && atom.nucleus()->currentMode() == Inset::MATH_MODE
1462             && name != from_ascii("ensuremath") && !ert_macro) {
1463                 MathAtom at(new InsetMathEnsureMath(buffer()));
1464                 at.nucleus()->cell(0).push_back(atom);
1465                 niceInsert(at);
1466                 posForward();
1467         } else if (in && in->currentMode() == Inset::MATH_MODE
1468                    && atom.nucleus()->currentMode() == Inset::TEXT_MODE
1469                    && !keep_mathmode) {
1470                 MathAtom at = createInsetMath("text", buffer());
1471                 at.nucleus()->cell(0).push_back(atom);
1472                 niceInsert(at);
1473                 posForward();
1474         } else
1475                 plainInsert(atom);
1476
1477         // finally put the macro argument behind, if needed
1478         if (macroArg) {
1479                 if (selection.size() > 1 || selection[0]->asScriptInset())
1480                         plainInsert(MathAtom(new InsetMathBrace(selection)));
1481                 else
1482                         insert(selection);
1483         }
1484
1485         return true;
1486 }
1487
1488
1489 docstring Cursor::macroName()
1490 {
1491         return inMacroMode() ? activeMacro()->name() : docstring();
1492 }
1493
1494
1495 void Cursor::handleNest(MathAtom const & a, int c)
1496 {
1497         //lyxerr << "Cursor::handleNest: " << c << endl;
1498         MathAtom t = a;
1499         asArray(cap::grabAndEraseSelection(*this), t.nucleus()->cell(c));
1500         insert(t);
1501         posBackward();
1502         pushBackward(*nextInset());
1503 }
1504
1505
1506 int Cursor::targetX() const
1507 {
1508         if (x_target() != -1)
1509                 return x_target();
1510         int x = 0;
1511         int y = 0;
1512         getPos(x, y);
1513         return x;
1514 }
1515
1516
1517 int Cursor::textTargetOffset() const
1518 {
1519         return textTargetOffset_;
1520 }
1521
1522
1523 void Cursor::setTargetX()
1524 {
1525         int x;
1526         int y;
1527         getPos(x, y);
1528         setTargetX(x);
1529 }
1530
1531
1532 bool Cursor::inMacroMode() const
1533 {
1534         if (!inMathed())
1535                 return false;
1536         if (pos() == 0 || cell().empty())
1537                 return false;
1538         InsetMathUnknown const * p = prevAtom()->asUnknownInset();
1539         return p && !p->final();
1540 }
1541
1542
1543 InsetMathUnknown * Cursor::activeMacro()
1544 {
1545         return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1546 }
1547
1548
1549 InsetMathUnknown const * Cursor::activeMacro() const
1550 {
1551         return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1552 }
1553
1554
1555 void Cursor::pullArg()
1556 {
1557         // FIXME: Look here
1558         MathData ar = cell();
1559         if (popBackward() && inMathed()) {
1560                 plainErase();
1561                 cell().insert(pos(), ar);
1562                 resetAnchor();
1563         } else {
1564                 //formula()->mutateToText();
1565         }
1566 }
1567
1568
1569 void Cursor::touch()
1570 {
1571         // FIXME: look here
1572 #if 0
1573         DocIterator::const_iterator it = begin();
1574         DocIterator::const_iterator et = end();
1575         for ( ; it != et; ++it)
1576                 it->cell().touch();
1577 #endif
1578 }
1579
1580
1581 void Cursor::normalize()
1582 {
1583         if (idx() > lastidx()) {
1584                 lyxerr << "this should not really happen - 1: "
1585                        << idx() << ' ' << nargs()
1586                        << " in: " << &inset() << endl;
1587                 idx() = lastidx();
1588         }
1589
1590         if (pos() > lastpos()) {
1591                 lyxerr << "this should not really happen - 2: "
1592                         << pos() << ' ' << lastpos() <<  " in idx: " << idx()
1593                        << " in atom: '";
1594                 odocstringstream os;
1595                 otexrowstream ots(os);
1596                 WriteStream wi(ots, false, true, WriteStream::wsDefault);
1597                 inset().asInsetMath()->write(wi);
1598                 lyxerr << to_utf8(os.str()) << endl;
1599                 pos() = lastpos();
1600         }
1601 }
1602
1603
1604 bool Cursor::upDownInMath(bool up)
1605 {
1606         // Be warned: The 'logic' implemented in this function is highly
1607         // fragile. A distance of one pixel or a '<' vs '<=' _really
1608         // matters. So fiddle around with it only if you think you know
1609         // what you are doing!
1610         int xo = 0;
1611         int yo = 0;
1612         getPos(xo, yo);
1613         xo = beforeDispatchPosX_;
1614
1615         // check if we had something else in mind, if not, this is the future
1616         // target
1617         if (x_target_ == -1)
1618                 setTargetX(xo);
1619         else if (inset().asInsetText() && xo - textTargetOffset() != x_target()) {
1620                 // In text mode inside the line (not left or right) possibly set a new target_x,
1621                 // but only if we are somewhere else than the previous target-offset.
1622
1623                 // We want to keep the x-target on subsequent up/down movements
1624                 // that cross beyond the end of short lines. Thus a special
1625                 // handling when the cursor is at the end of line: Use the new
1626                 // x-target only if the old one was before the end of line
1627                 // or the old one was after the beginning of the line
1628                 bool inRTL = innerParagraph().isRTL(bv().buffer().params());
1629                 bool left;
1630                 bool right;
1631                 if (inRTL) {
1632                         left = pos() == textRow().endpos();
1633                         right = pos() == textRow().pos();
1634                 } else {
1635                         left = pos() == textRow().pos();
1636                         right = pos() == textRow().endpos();
1637                 }
1638                 if ((!left && !right) ||
1639                                 (left && !right && xo < x_target_) ||
1640                                 (!left && right && x_target_ < xo))
1641                         setTargetX(xo);
1642                 else
1643                         xo = targetX();
1644         } else
1645                 xo = targetX();
1646
1647         // try neigbouring script insets
1648         Cursor old = *this;
1649         if (inMathed() && !selection()) {
1650                 // try left
1651                 if (pos() != 0) {
1652                         InsetMathScript const * p = prevAtom()->asScriptInset();
1653                         if (p && p->has(up)) {
1654                                 --pos();
1655                                 push(*const_cast<InsetMathScript*>(p));
1656                                 idx() = p->idxOfScript(up);
1657                                 pos() = lastpos();
1658
1659                                 // we went in the right direction? Otherwise don't jump into the script
1660                                 int x;
1661                                 int y;
1662                                 getPos(x, y);
1663                                 int oy = beforeDispatchPosY_;
1664                                 if ((!up && y <= oy) ||
1665                                                 (up && y >= oy))
1666                                         operator=(old);
1667                                 else
1668                                         return true;
1669                         }
1670                 }
1671
1672                 // try right
1673                 if (pos() != lastpos()) {
1674                         InsetMathScript const * p = nextAtom()->asScriptInset();
1675                         if (p && p->has(up)) {
1676                                 push(*const_cast<InsetMathScript*>(p));
1677                                 idx() = p->idxOfScript(up);
1678                                 pos() = 0;
1679
1680                                 // we went in the right direction? Otherwise don't jump into the script
1681                                 int x;
1682                                 int y;
1683                                 getPos(x, y);
1684                                 int oy = beforeDispatchPosY_;
1685                                 if ((!up && y <= oy) ||
1686                                                 (up && y >= oy))
1687                                         operator=(old);
1688                                 else
1689                                         return true;
1690                         }
1691                 }
1692         }
1693
1694         // try to find an inset that knows better then we,
1695         if (inset().idxUpDown(*this, up)) {
1696                 //lyxerr << "idxUpDown triggered" << endl;
1697                 // try to find best position within this inset
1698                 if (!selection())
1699                         setCursor(bruteFind(*this, xo, yo));
1700                 return true;
1701         }
1702
1703         // any improvement going just out of inset?
1704         if (popBackward() && inMathed()) {
1705                 //lyxerr << "updown: popBackward succeeded" << endl;
1706                 int xnew;
1707                 int ynew;
1708                 int yold = beforeDispatchPosY_;
1709                 getPos(xnew, ynew);
1710                 if (up ? ynew < yold : ynew > yold)
1711                         return true;
1712         }
1713
1714         // no success, we are probably at the document top or bottom
1715         operator=(old);
1716         return false;
1717 }
1718
1719
1720 InsetMath & Cursor::nextMath()
1721 {
1722         return *nextAtom().nucleus();
1723 }
1724
1725
1726 InsetMath & Cursor::prevMath()
1727 {
1728         return *prevAtom().nucleus();
1729 }
1730
1731
1732 bool Cursor::mathForward(bool word)
1733 {
1734         LASSERT(inMathed(), return false);
1735         if (pos() < lastpos()) {
1736                 if (word) {
1737                         // word: skip a group of insets of the form X*(B*|R*|P*) (greedy
1738                         // match) where X is any math class, B is mathbin, R is mathrel, and
1739                         // P is mathpunct. Make sure that the following remains true:
1740                         //   mathForward(true); mathBackward(true); mathForward(true)
1741                         // is the same as mathForward(true) and
1742                         //   mathBackward(true); mathForward(true); mathBackward(true)
1743                         // is the same as mathBackward(true).
1744                         MathClass mc = nextMath().mathClass();
1745                         do
1746                                 posForward();
1747                         while (pos() < lastpos() && mc == nextMath().mathClass());
1748                         if (pos() < lastpos() &&
1749                             ((mc = nextMath().mathClass()) == MC_BIN ||
1750                              mc == MC_REL || mc == MC_PUNCT))
1751                                 do
1752                                         posForward();
1753                                 while (pos() < lastpos() && mc == nextMath().mathClass());
1754                 } else if (openable(nextAtom())) {
1755                         // single step: try to enter the next inset
1756                         pushBackward(nextMath());
1757                         inset().idxFirst(*this);
1758                 } else
1759                         posForward();
1760                 return true;
1761         }
1762         if (inset().idxForward(*this))
1763                 return true;
1764         // try to pop forwards --- but don't pop out of math! leave that to
1765         // the FINISH lfuns
1766         int s = depth() - 2;
1767         if (s >= 0 && operator[](s).inset().asInsetMath())
1768                 return popForward();
1769         return false;
1770 }
1771
1772
1773 bool Cursor::mathBackward(bool word)
1774 {
1775         LASSERT(inMathed(), return false);
1776         if (pos() > 0) {
1777                 if (word) {
1778                         // word: skip a group of insets. See the comment in mathForward.
1779                         MathClass mc = prevMath().mathClass();
1780                         do
1781                                 posBackward();
1782                         while (pos() > 0 && mc == prevMath().mathClass());
1783                         if (pos() > 0 && (mc == MC_BIN || mc == MC_REL || mc == MC_PUNCT)) {
1784                                 mc = prevMath().mathClass();
1785                                 do
1786                                         posBackward();
1787                                 while (pos() > 0 && mc == prevMath().mathClass());
1788                         }
1789                 } else if (openable(prevAtom())) {
1790                         // single step: try to enter the preceding inset
1791                         posBackward();
1792                         push(nextMath());
1793                         inset().idxLast(*this);
1794                 } else
1795                         posBackward();
1796                 return true;
1797         }
1798         if (inset().idxBackward(*this))
1799                 return true;
1800         // try to pop backwards --- but don't pop out of math! leave that to
1801         // the FINISH lfuns
1802         int s = depth() - 2;
1803         if (s >= 0 && operator[](s).inset().asInsetMath())
1804                 return popBackward();
1805         return false;
1806 }
1807
1808
1809 bool Cursor::atFirstOrLastRow(bool up)
1810 {
1811         TextMetrics const & tm = bv_->textMetrics(text());
1812         ParagraphMetrics const & pm = tm.parMetrics(pit());
1813
1814         int row;
1815         if (pos() && boundary())
1816                 row = pm.pos2row(pos() - 1);
1817         else
1818                 row = pm.pos2row(pos());
1819
1820         if (up) {
1821                 if (pit() == 0 && row == 0)
1822                         return true;
1823         } else {
1824                 if (pit() + 1 >= int(text()->paragraphs().size()) &&
1825                                 row + 1 >= int(pm.rows().size()))
1826                         return true;
1827         }
1828         return false;
1829 }
1830
1831
1832 bool Cursor::upDownInText(bool up, bool & updateNeeded)
1833 {
1834         LASSERT(text(), return false);
1835
1836         // where are we?
1837         int xo = 0;
1838         int yo = 0;
1839         getPos(xo, yo);
1840         xo = beforeDispatchPosX_;
1841
1842         // update the targetX - this is here before the "return false"
1843         // to set a new target which can be used by InsetTexts above
1844         // if we cannot move up/down inside this inset anymore
1845         if (x_target_ == -1)
1846                 setTargetX(xo);
1847         else if (xo - textTargetOffset() != x_target() &&
1848                                          depth() == beforeDispatchCursor_.depth()) {
1849                 // In text mode inside the line (not left or right)
1850                 // possibly set a new target_x, but only if we are
1851                 // somewhere else than the previous target-offset.
1852
1853                 // We want to keep the x-target on subsequent up/down
1854                 // movements that cross beyond the end of short lines.
1855                 // Thus a special handling when the cursor is at the
1856                 // end of line: Use the new x-target only if the old
1857                 // one was before the end of line or the old one was
1858                 // after the beginning of the line
1859                 bool inRTL = innerParagraph().isRTL(bv().buffer().params());
1860                 bool left;
1861                 bool right;
1862                 if (inRTL) {
1863                         left = pos() == textRow().endpos();
1864                         right = pos() == textRow().pos();
1865                 } else {
1866                         left = pos() == textRow().pos();
1867                         right = pos() == textRow().endpos();
1868                 }
1869                 if ((!left && !right) ||
1870                                 (left && !right && xo < x_target_) ||
1871                                 (!left && right && x_target_ < xo))
1872                         setTargetX(xo);
1873                 else
1874                         xo = targetX();
1875         } else
1876                 xo = targetX();
1877
1878         // first get the current line
1879         TextMetrics & tm = bv_->textMetrics(text());
1880         ParagraphMetrics const & pm = tm.parMetrics(pit());
1881         int row;
1882         if (pos() && boundary())
1883                 row = pm.pos2row(pos() - 1);
1884         else
1885                 row = pm.pos2row(pos());
1886
1887         if (atFirstOrLastRow(up)) {
1888                 // Is there a place for the cursor to go ? If yes, we
1889                 // can execute the DEPM, otherwise we should keep the
1890                 // paragraph to host the cursor.
1891                 Cursor dummy = *this;
1892                 bool valid_destination = false;
1893                 for(; dummy.depth(); dummy.pop())
1894                         if (!dummy.atFirstOrLastRow(up)) {
1895                                 valid_destination = true;
1896                                 break;
1897                         }
1898
1899                 // will a next dispatch follow and if there is a new
1900                 // dispatch will it move the cursor out ?
1901                 if (depth() > 1 && valid_destination) {
1902                         // The cursor hasn't changed yet. This happens when
1903                         // you e.g. move out of an inset. And to give the
1904                         // DEPM the possibility of doing something we must
1905                         // provide it with two different cursors. (Lgb, vfr)
1906                         dummy = *this;
1907                         dummy.pos() = dummy.pos() == 0 ? dummy.lastpos() : 0;
1908                         dummy.pit() = dummy.pit() == 0 ? dummy.lastpit() : 0;
1909
1910                         updateNeeded |= bv().checkDepm(dummy, *this);
1911                         updateTextTargetOffset();
1912                         if (updateNeeded)
1913                                 forceBufferUpdate();
1914                 }
1915                 return false;
1916         }
1917
1918         // with and without selection are handled differently
1919         if (!selection()) {
1920                 int yo = bv().getPos(*this).y_;
1921                 Cursor old = *this;
1922                 // To next/previous row
1923                 // FIXME: the y position is often guessed wrongly across styles and
1924                 // insets, which leads to weird behaviour.
1925                 if (up)
1926                         tm.editXY(*this, xo, yo - textRow().ascent() - 1);
1927                 else
1928                         tm.editXY(*this, xo, yo + textRow().descent() + 1);
1929                 x_target_ = old.x_target_;
1930                 clearSelection();
1931
1932                 // This happens when you move out of an inset.
1933                 // And to give the DEPM the possibility of doing
1934                 // something we must provide it with two different
1935                 // cursors. (Lgb)
1936                 Cursor dummy = *this;
1937                 if (dummy == old)
1938                         ++dummy.pos();
1939                 if (bv().checkDepm(dummy, old)) {
1940                         updateNeeded = true;
1941                         // Make sure that cur gets back whatever happened to dummy (Lgb)
1942                         operator=(dummy);
1943                 }
1944                 if (inTexted() && pos() && paragraph().isEnvSeparator(pos() - 1))
1945                         posBackward();
1946         } else {
1947                 // if there is a selection, we stay out of any inset,
1948                 // and just jump to the right position:
1949                 Cursor old = *this;
1950                 int next_row = row;
1951                 if (up) {
1952                         if (row > 0) {
1953                                 --next_row;
1954                         } else if (pit() > 0) {
1955                                 --pit();
1956                                 TextMetrics & tm = bv_->textMetrics(text());
1957                                 if (!tm.contains(pit()))
1958                                         tm.newParMetricsUp();
1959                                 ParagraphMetrics const & pmcur = tm.parMetrics(pit());
1960                                 next_row = pmcur.rows().size() - 1;
1961                         }
1962                 } else {
1963                         if (row + 1 < int(pm.rows().size())) {
1964                                 ++next_row;
1965                         } else if (pit() + 1 < int(text()->paragraphs().size())) {
1966                                 ++pit();
1967                                 TextMetrics & tm = bv_->textMetrics(text());
1968                                 if (!tm.contains(pit()))
1969                                         tm.newParMetricsDown();
1970                                 next_row = 0;
1971                         }
1972                 }
1973
1974                 Row const & real_next_row = tm.parMetrics(pit()).rows()[next_row];
1975                 bool bound = false;
1976                 top().pos() = tm.getPosNearX(real_next_row, xo, bound);
1977                 boundary(bound);
1978                 // When selection==false, this is done by TextMetrics::editXY
1979                 setCurrentFont();
1980
1981                 updateNeeded |= bv().checkDepm(*this, old);
1982         }
1983
1984         if (updateNeeded)
1985                 forceBufferUpdate();
1986         updateTextTargetOffset();
1987         return true;
1988 }
1989
1990
1991 void Cursor::handleFont(string const & font)
1992 {
1993         LYXERR(Debug::DEBUG, font);
1994         docstring safe;
1995         if (selection()) {
1996                 macroModeClose();
1997                 safe = cap::grabAndEraseSelection(*this);
1998         }
1999
2000         recordUndoInset();
2001
2002         if (lastpos() != 0) {
2003                 // something left in the cell
2004                 if (pos() == 0) {
2005                         // cursor in first position
2006                         popBackward();
2007                 } else if (pos() == lastpos()) {
2008                         // cursor in last position
2009                         popForward();
2010                 } else {
2011                         // cursor in between. split cell
2012                         MathData::iterator bt = cell().begin();
2013                         MathAtom at = createInsetMath(from_utf8(font), buffer());
2014                         at.nucleus()->cell(0) = MathData(buffer(), bt, bt + pos());
2015                         cell().erase(bt, bt + pos());
2016                         popBackward();
2017                         plainInsert(at);
2018                 }
2019         } else {
2020                 // nothing left in the cell
2021                 popBackward();
2022                 plainErase();
2023                 resetAnchor();
2024         }
2025         insert(safe);
2026 }
2027
2028
2029 void Cursor::message(docstring const & msg) const
2030 {
2031         disp_.setMessage(msg);
2032 }
2033
2034
2035 void Cursor::errorMessage(docstring const & msg) const
2036 {
2037         disp_.setMessage(msg);
2038         disp_.setError(true);
2039 }
2040
2041
2042 namespace {
2043
2044 docstring parbreak(Cursor const * cur)
2045 {
2046         odocstringstream os;
2047         os << '\n';
2048         // only add blank line if we're not in a ParbreakIsNewline situation
2049         if (!cur->inset().getLayout().parbreakIsNewline()
2050             && !cur->paragraph().layout().parbreak_is_newline)
2051                 os << '\n';
2052         return os.str();
2053 }
2054
2055 } // namespace
2056
2057
2058 docstring Cursor::selectionAsString(bool with_label) const
2059 {
2060         if (!selection())
2061                 return docstring();
2062
2063         if (inMathed())
2064                 return cap::grabSelection(*this);
2065
2066         int const label = with_label
2067                 ? AS_STR_LABEL | AS_STR_INSETS : AS_STR_INSETS;
2068
2069         idx_type const startidx = selBegin().idx();
2070         idx_type const endidx = selEnd().idx();
2071         if (startidx != endidx) {
2072                 // multicell selection
2073                 InsetTabular * table = inset().asInsetTabular();
2074                 LASSERT(table, return docstring());
2075                 return table->asString(startidx, endidx);
2076         }
2077
2078         ParagraphList const & pars = text()->paragraphs();
2079
2080         pit_type const startpit = selBegin().pit();
2081         pit_type const endpit = selEnd().pit();
2082         size_t const startpos = selBegin().pos();
2083         size_t const endpos = selEnd().pos();
2084
2085         if (startpit == endpit)
2086                 return pars[startpit].asString(startpos, endpos, label);
2087
2088         // First paragraph in selection
2089         docstring result = pars[startpit].
2090                 asString(startpos, pars[startpit].size(), label)
2091                 + parbreak(this);
2092
2093         // The paragraphs in between (if any)
2094         for (pit_type pit = startpit + 1; pit != endpit; ++pit) {
2095                 Paragraph const & par = pars[pit];
2096                 result += par.asString(0, par.size(), label)
2097                         + parbreak(this);
2098         }
2099
2100         // Last paragraph in selection
2101         result += pars[endpit].asString(0, endpos, label);
2102
2103         return result;
2104 }
2105
2106
2107 docstring Cursor::currentState(bool devel_mode) const
2108 {
2109         if (inMathed()) {
2110                 odocstringstream os;
2111                 info(os, devel_mode);
2112                 return os.str();
2113         }
2114
2115         if (inTexted())
2116                 return text()->currentState(*this, devel_mode);
2117
2118         return docstring();
2119 }
2120
2121
2122 docstring Cursor::getPossibleLabel() const
2123 {
2124         return inMathed() ? from_ascii("eq:") : text()->getPossibleLabel(*this);
2125 }
2126
2127
2128 Encoding const * Cursor::getEncoding() const
2129 {
2130         if (empty())
2131                 return 0;
2132         BufferParams const & bp = bv().buffer().params();
2133         if (bp.useNonTeXFonts)
2134                 return encodings.fromLyXName("utf8-plain");
2135
2136         CursorSlice const & sl = innerTextSlice();
2137         Text const & text = *sl.text();
2138         Font font = text.getPar(sl.pit()).getFont(bp, sl.pos(),
2139                                                   text.outerFont(sl.pit()));
2140         return font.language()->encoding();
2141 }
2142
2143
2144 void Cursor::undispatched() const
2145 {
2146         disp_.dispatched(false);
2147 }
2148
2149
2150 void Cursor::dispatched() const
2151 {
2152         disp_.dispatched(true);
2153 }
2154
2155
2156 void Cursor::screenUpdateFlags(Update::flags f) const
2157 {
2158         disp_.screenUpdate(f);
2159 }
2160
2161
2162 void Cursor::forceBufferUpdate() const
2163 {
2164         disp_.forceBufferUpdate();
2165 }
2166
2167
2168 void Cursor::clearBufferUpdate() const
2169 {
2170         disp_.clearBufferUpdate();
2171 }
2172
2173
2174 bool Cursor::needBufferUpdate() const
2175 {
2176         return disp_.needBufferUpdate();
2177 }
2178
2179
2180 void Cursor::noScreenUpdate() const
2181 {
2182         disp_.screenUpdate(Update::None);
2183 }
2184
2185
2186 Font Cursor::getFont() const
2187 {
2188         // The logic here should more or less match to the
2189         // Cursor::setCurrentFont logic, i.e. the cursor height should
2190         // give a hint what will happen if a character is entered.
2191         // FIXME: this is not the case, what about removing this method ? (see #10478).
2192
2193         // HACK. far from being perfect...
2194
2195         CursorSlice const & sl = innerTextSlice();
2196         Text const & text = *sl.text();
2197         Paragraph const & par = text.getPar(sl.pit());
2198
2199         // on boundary, so we are really at the character before
2200         pos_type pos = sl.pos();
2201         if (pos > 0 && boundary())
2202                 --pos;
2203
2204         // on space? Take the font before (only for RTL boundary stay)
2205         if (pos > 0) {
2206                 TextMetrics const & tm = bv().textMetrics(&text);
2207                 if (pos == sl.lastpos()
2208                         || (par.isSeparator(pos)
2209                         && !tm.isRTLBoundary(sl.pit(), pos)))
2210                         --pos;
2211         }
2212
2213         // get font at the position
2214         Font font = par.getFont(buffer()->params(), pos,
2215                 text.outerFont(sl.pit()));
2216
2217         return font;
2218 }
2219
2220
2221 bool Cursor::fixIfBroken()
2222 {
2223         bool const broken_cursor = DocIterator::fixIfBroken();
2224         bool const broken_anchor = anchor_.fixIfBroken();
2225
2226         if (broken_cursor || broken_anchor) {
2227                 clearNewWordPosition();
2228                 clearSelection();
2229                 return true;
2230         }
2231         return false;
2232 }
2233
2234
2235 void Cursor::sanitize()
2236 {
2237         setBuffer(&bv_->buffer());
2238         DocIterator::sanitize();
2239         new_word_.sanitize();
2240         if (selection())
2241                 anchor_.sanitize();
2242         else
2243                 resetAnchor();
2244 }
2245
2246
2247 bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur)
2248 {
2249         // find inset in common
2250         size_type i;
2251         for (i = 0; i < old.depth() && i < cur.depth(); ++i) {
2252                 if (&old[i].inset() != &cur[i].inset())
2253                         break;
2254         }
2255
2256         // update words if we just moved to another paragraph
2257         if (i == old.depth() && i == cur.depth()
2258             && !cur.buffer()->isClean()
2259             && cur.inTexted() && old.inTexted()
2260             && cur.pit() != old.pit()) {
2261                 old.paragraph().updateWords();
2262         }
2263
2264         // notify everything on top of the common part in old cursor,
2265         // but stop if the inset claims the cursor to be invalid now
2266         for (size_type j = i; j < old.depth(); ++j) {
2267                 Cursor inset_pos = old;
2268                 inset_pos.cutOff(j);
2269                 if (old[j].inset().notifyCursorLeaves(inset_pos, cur))
2270                         return true;
2271         }
2272
2273         // notify everything on top of the common part in new cursor,
2274         // but stop if the inset claims the cursor to be invalid now
2275         for (; i < cur.depth(); ++i) {
2276                 if (cur[i].inset().notifyCursorEnters(cur))
2277                         return true;
2278         }
2279
2280         return false;
2281 }
2282
2283
2284 void Cursor::setCurrentFont()
2285 {
2286         CursorSlice const & cs = innerTextSlice();
2287         Paragraph const & par = cs.paragraph();
2288         pos_type cpit = cs.pit();
2289         pos_type cpos = cs.pos();
2290         Text const & ctext = *cs.text();
2291         TextMetrics const & tm = bv().textMetrics(&ctext);
2292
2293         // are we behind previous char in fact? -> go to that char
2294         if (cpos > 0 && boundary())
2295                 --cpos;
2296
2297         // find position to take the font from
2298         if (cpos != 0) {
2299                 // paragraph end? -> font of last char
2300                 if (cpos == lastpos())
2301                         --cpos;
2302                 // on space? -> look at the words in front of space
2303                 else if (cpos > 0 && par.isSeparator(cpos))     {
2304                         // abc| def -> font of c
2305                         // abc |[WERBEH], i.e. boundary==true -> font of c
2306                         // abc [WERBEH]| def, font of the space
2307                         if (!tm.isRTLBoundary(cpit, cpos))
2308                                 --cpos;
2309                 }
2310         }
2311
2312         // get font
2313         BufferParams const & bufparams = buffer()->params();
2314         current_font = par.getFontSettings(bufparams, cpos);
2315         real_current_font = tm.displayFont(cpit, cpos);
2316
2317         // special case for paragraph end
2318         if (cs.pos() == lastpos()
2319             && tm.isRTLBoundary(cpit, cs.pos())
2320             && !boundary()) {
2321                 Language const * lang = par.getParLanguage(bufparams);
2322                 current_font.setLanguage(lang);
2323                 current_font.fontInfo().setNumber(FONT_OFF);
2324                 real_current_font.setLanguage(lang);
2325                 real_current_font.fontInfo().setNumber(FONT_OFF);
2326         }
2327 }
2328
2329
2330 bool Cursor::textUndo()
2331 {
2332         if (!buffer()->undo().textUndo(*this))
2333                 return false;
2334         sanitize();
2335         return true;
2336 }
2337
2338
2339 bool Cursor::textRedo()
2340 {
2341         if (!buffer()->undo().textRedo(*this))
2342                 return false;
2343         sanitize();
2344         return true;
2345 }
2346
2347
2348 void Cursor::finishUndo() const
2349 {
2350         buffer()->undo().finishUndo();
2351 }
2352
2353
2354 void Cursor::beginUndoGroup() const
2355 {
2356         buffer()->undo().beginUndoGroup(*this);
2357 }
2358
2359
2360 void Cursor::endUndoGroup() const
2361 {
2362         buffer()->undo().endUndoGroup(*this);
2363 }
2364
2365
2366 void Cursor::recordUndo(pit_type from, pit_type to) const
2367 {
2368         buffer()->undo().recordUndo(*this, from, to);
2369 }
2370
2371
2372 void Cursor::recordUndo(pit_type from) const
2373 {
2374         buffer()->undo().recordUndo(*this, from, pit());
2375 }
2376
2377
2378 void Cursor::recordUndo(UndoKind kind) const
2379 {
2380         buffer()->undo().recordUndo(*this, kind);
2381 }
2382
2383
2384 void Cursor::recordUndoInset(Inset const * in) const
2385 {
2386         buffer()->undo().recordUndoInset(*this, in);
2387 }
2388
2389
2390 void Cursor::recordUndoFullBuffer() const
2391 {
2392         buffer()->undo().recordUndoFullBuffer(*this);
2393 }
2394
2395
2396 void Cursor::recordUndoBufferParams() const
2397 {
2398         buffer()->undo().recordUndoBufferParams(*this);
2399 }
2400
2401
2402 void Cursor::recordUndoSelection() const
2403 {
2404         if (inMathed()) {
2405                 if (cap::multipleCellsSelected(*this))
2406                         recordUndoInset();
2407                 else
2408                         recordUndo();
2409         } else {
2410                 buffer()->undo().recordUndo(*this,
2411                         selBegin().pit(), selEnd().pit());
2412         }
2413 }
2414
2415
2416 void Cursor::checkBufferStructure()
2417 {
2418         Buffer const * master = buffer()->masterBuffer();
2419         master->tocBackend().updateItem(*this);
2420         if (master != buffer() && !master->hasGuiDelegate())
2421                 // In case the master has no gui associated with it,
2422                 // the TocItem is not updated (part of bug 5699).
2423                 buffer()->tocBackend().updateItem(*this);
2424
2425         // If the last tracked change of the paragraph has just been
2426         // deleted, then we need to recompute the buffer flag
2427         // tracked_changes_present_.
2428         if (inTexted() && paragraph().isChangeUpdateRequired())
2429                 disp_.forceChangesUpdate();
2430 }
2431
2432
2433 bool Cursor::confirmDeletion(bool const before) const
2434 {
2435         if (!selection()) {
2436                 if (Inset const * inset = before ? prevInset() : nextInset())
2437                         return inset->confirmDeletion();
2438         } else {
2439                 DocIterator dit = selectionBegin();
2440                 CursorSlice const end = selectionEnd().top();
2441                 for (; dit.top() < end; dit.top().forwardPos())
2442                         if (Inset const * inset = dit.nextInset())
2443                                 if (inset->confirmDeletion())
2444                                         return true;
2445         }
2446         return false;
2447 }
2448
2449
2450 void Cursor::moveToClosestEdge(int const x, bool const edit)
2451 {
2452         if (Inset const * inset = nextInset()) {
2453                 // stay in front of insets for which we want to open the dialog
2454                 // (e.g. InsetMathSpace).
2455                 if (edit && (inset->hasSettings() || !inset->contextMenuName().empty()))
2456                         return;
2457                 CoordCache::Insets const & insetCache = bv().coordCache().getInsets();
2458                 if (!insetCache.has(inset))
2459                         return;
2460                 int const wid = insetCache.dim(inset).wid;
2461                 Point p = insetCache.xy(inset);
2462                 if (x > p.x_ + (wid + 1) / 2)
2463                         posForward();
2464         }
2465 }
2466
2467
2468 } // namespace lyx