]> git.lyx.org Git - lyx.git/blob - src/Cursor.cpp
d21570a41fd77ab53e2c9c96c1823647787a9c2f
[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 = it != words.end() && (it->second.inset == "font"
1455                                                 || it->second.inset == "oldfont"
1456                                                 || it->second.inset == "mbox");
1457         bool ert_macro = !user_macro && it == words.end() && atomAsMacro;
1458
1459         if (in && in->currentMode() == Inset::TEXT_MODE
1460             && atom.nucleus()->currentMode() == Inset::MATH_MODE
1461             && name != from_ascii("ensuremath") && !ert_macro) {
1462                 MathAtom at(new InsetMathEnsureMath(buffer()));
1463                 at.nucleus()->cell(0).push_back(atom);
1464                 niceInsert(at);
1465                 posForward();
1466         } else if (in && in->currentMode() == Inset::MATH_MODE
1467                    && atom.nucleus()->currentMode() == Inset::TEXT_MODE
1468                    && !keep_mathmode) {
1469                 MathAtom at = createInsetMath("text", buffer());
1470                 at.nucleus()->cell(0).push_back(atom);
1471                 niceInsert(at);
1472                 posForward();
1473         } else
1474                 plainInsert(atom);
1475
1476         // finally put the macro argument behind, if needed
1477         if (macroArg) {
1478                 if (selection.size() > 1 || selection[0]->asScriptInset())
1479                         plainInsert(MathAtom(new InsetMathBrace(selection)));
1480                 else
1481                         insert(selection);
1482         }
1483
1484         return true;
1485 }
1486
1487
1488 docstring Cursor::macroName()
1489 {
1490         return inMacroMode() ? activeMacro()->name() : docstring();
1491 }
1492
1493
1494 void Cursor::handleNest(MathAtom const & a, int c)
1495 {
1496         //lyxerr << "Cursor::handleNest: " << c << endl;
1497         MathAtom t = a;
1498         asArray(cap::grabAndEraseSelection(*this), t.nucleus()->cell(c));
1499         insert(t);
1500         posBackward();
1501         pushBackward(*nextInset());
1502 }
1503
1504
1505 int Cursor::targetX() const
1506 {
1507         if (x_target() != -1)
1508                 return x_target();
1509         int x = 0;
1510         int y = 0;
1511         getPos(x, y);
1512         return x;
1513 }
1514
1515
1516 int Cursor::textTargetOffset() const
1517 {
1518         return textTargetOffset_;
1519 }
1520
1521
1522 void Cursor::setTargetX()
1523 {
1524         int x;
1525         int y;
1526         getPos(x, y);
1527         setTargetX(x);
1528 }
1529
1530
1531 bool Cursor::inMacroMode() const
1532 {
1533         if (!inMathed())
1534                 return false;
1535         if (pos() == 0 || cell().empty())
1536                 return false;
1537         InsetMathUnknown const * p = prevAtom()->asUnknownInset();
1538         return p && !p->final();
1539 }
1540
1541
1542 InsetMathUnknown * Cursor::activeMacro()
1543 {
1544         return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1545 }
1546
1547
1548 InsetMathUnknown const * Cursor::activeMacro() const
1549 {
1550         return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1551 }
1552
1553
1554 void Cursor::pullArg()
1555 {
1556         // FIXME: Look here
1557         MathData ar = cell();
1558         if (popBackward() && inMathed()) {
1559                 plainErase();
1560                 cell().insert(pos(), ar);
1561                 resetAnchor();
1562         } else {
1563                 //formula()->mutateToText();
1564         }
1565 }
1566
1567
1568 void Cursor::touch()
1569 {
1570         // FIXME: look here
1571 #if 0
1572         DocIterator::const_iterator it = begin();
1573         DocIterator::const_iterator et = end();
1574         for ( ; it != et; ++it)
1575                 it->cell().touch();
1576 #endif
1577 }
1578
1579
1580 void Cursor::normalize()
1581 {
1582         if (idx() > lastidx()) {
1583                 lyxerr << "this should not really happen - 1: "
1584                        << idx() << ' ' << nargs()
1585                        << " in: " << &inset() << endl;
1586                 idx() = lastidx();
1587         }
1588
1589         if (pos() > lastpos()) {
1590                 lyxerr << "this should not really happen - 2: "
1591                         << pos() << ' ' << lastpos() <<  " in idx: " << idx()
1592                        << " in atom: '";
1593                 odocstringstream os;
1594                 otexrowstream ots(os);
1595                 WriteStream wi(ots, false, true, WriteStream::wsDefault);
1596                 inset().asInsetMath()->write(wi);
1597                 lyxerr << to_utf8(os.str()) << endl;
1598                 pos() = lastpos();
1599         }
1600 }
1601
1602
1603 bool Cursor::upDownInMath(bool up)
1604 {
1605         // Be warned: The 'logic' implemented in this function is highly
1606         // fragile. A distance of one pixel or a '<' vs '<=' _really
1607         // matters. So fiddle around with it only if you think you know
1608         // what you are doing!
1609         int xo = 0;
1610         int yo = 0;
1611         getPos(xo, yo);
1612         xo = beforeDispatchPosX_;
1613
1614         // check if we had something else in mind, if not, this is the future
1615         // target
1616         if (x_target_ == -1)
1617                 setTargetX(xo);
1618         else if (inset().asInsetText() && xo - textTargetOffset() != x_target()) {
1619                 // In text mode inside the line (not left or right) possibly set a new target_x,
1620                 // but only if we are somewhere else than the previous target-offset.
1621
1622                 // We want to keep the x-target on subsequent up/down movements
1623                 // that cross beyond the end of short lines. Thus a special
1624                 // handling when the cursor is at the end of line: Use the new
1625                 // x-target only if the old one was before the end of line
1626                 // or the old one was after the beginning of the line
1627                 bool inRTL = innerParagraph().isRTL(bv().buffer().params());
1628                 bool left;
1629                 bool right;
1630                 if (inRTL) {
1631                         left = pos() == textRow().endpos();
1632                         right = pos() == textRow().pos();
1633                 } else {
1634                         left = pos() == textRow().pos();
1635                         right = pos() == textRow().endpos();
1636                 }
1637                 if ((!left && !right) ||
1638                                 (left && !right && xo < x_target_) ||
1639                                 (!left && right && x_target_ < xo))
1640                         setTargetX(xo);
1641                 else
1642                         xo = targetX();
1643         } else
1644                 xo = targetX();
1645
1646         // try neigbouring script insets
1647         Cursor old = *this;
1648         if (inMathed() && !selection()) {
1649                 // try left
1650                 if (pos() != 0) {
1651                         InsetMathScript const * p = prevAtom()->asScriptInset();
1652                         if (p && p->has(up)) {
1653                                 --pos();
1654                                 push(*const_cast<InsetMathScript*>(p));
1655                                 idx() = p->idxOfScript(up);
1656                                 pos() = lastpos();
1657
1658                                 // we went in the right direction? Otherwise don't jump into the script
1659                                 int x;
1660                                 int y;
1661                                 getPos(x, y);
1662                                 int oy = beforeDispatchPosY_;
1663                                 if ((!up && y <= oy) ||
1664                                                 (up && y >= oy))
1665                                         operator=(old);
1666                                 else
1667                                         return true;
1668                         }
1669                 }
1670
1671                 // try right
1672                 if (pos() != lastpos()) {
1673                         InsetMathScript const * p = nextAtom()->asScriptInset();
1674                         if (p && p->has(up)) {
1675                                 push(*const_cast<InsetMathScript*>(p));
1676                                 idx() = p->idxOfScript(up);
1677                                 pos() = 0;
1678
1679                                 // we went in the right direction? Otherwise don't jump into the script
1680                                 int x;
1681                                 int y;
1682                                 getPos(x, y);
1683                                 int oy = beforeDispatchPosY_;
1684                                 if ((!up && y <= oy) ||
1685                                                 (up && y >= oy))
1686                                         operator=(old);
1687                                 else
1688                                         return true;
1689                         }
1690                 }
1691         }
1692
1693         // try to find an inset that knows better then we,
1694         if (inset().idxUpDown(*this, up)) {
1695                 //lyxerr << "idxUpDown triggered" << endl;
1696                 // try to find best position within this inset
1697                 if (!selection())
1698                         setCursor(bruteFind(*this, xo, yo));
1699                 return true;
1700         }
1701
1702         // any improvement going just out of inset?
1703         if (popBackward() && inMathed()) {
1704                 //lyxerr << "updown: popBackward succeeded" << endl;
1705                 int xnew;
1706                 int ynew;
1707                 int yold = beforeDispatchPosY_;
1708                 getPos(xnew, ynew);
1709                 if (up ? ynew < yold : ynew > yold)
1710                         return true;
1711         }
1712
1713         // no success, we are probably at the document top or bottom
1714         operator=(old);
1715         return false;
1716 }
1717
1718
1719 InsetMath & Cursor::nextMath()
1720 {
1721         return *nextAtom().nucleus();
1722 }
1723
1724
1725 InsetMath & Cursor::prevMath()
1726 {
1727         return *prevAtom().nucleus();
1728 }
1729
1730
1731 bool Cursor::mathForward(bool word)
1732 {
1733         LASSERT(inMathed(), return false);
1734         if (pos() < lastpos()) {
1735                 if (word) {
1736                         // word: skip a group of insets of the form X*(B*|R*|P*) (greedy
1737                         // match) where X is any math class, B is mathbin, R is mathrel, and
1738                         // P is mathpunct. Make sure that the following remains true:
1739                         //   mathForward(true); mathBackward(true); mathForward(true)
1740                         // is the same as mathForward(true) and
1741                         //   mathBackward(true); mathForward(true); mathBackward(true)
1742                         // is the same as mathBackward(true).
1743                         MathClass mc = nextMath().mathClass();
1744                         do
1745                                 posForward();
1746                         while (pos() < lastpos() && mc == nextMath().mathClass());
1747                         if (pos() < lastpos() &&
1748                             ((mc = nextMath().mathClass()) == MC_BIN ||
1749                              mc == MC_REL || mc == MC_PUNCT))
1750                                 do
1751                                         posForward();
1752                                 while (pos() < lastpos() && mc == nextMath().mathClass());
1753                 } else if (openable(nextAtom())) {
1754                         // single step: try to enter the next inset
1755                         pushBackward(nextMath());
1756                         inset().idxFirst(*this);
1757                 } else
1758                         posForward();
1759                 return true;
1760         }
1761         if (inset().idxForward(*this))
1762                 return true;
1763         // try to pop forwards --- but don't pop out of math! leave that to
1764         // the FINISH lfuns
1765         int s = depth() - 2;
1766         if (s >= 0 && operator[](s).inset().asInsetMath())
1767                 return popForward();
1768         return false;
1769 }
1770
1771
1772 bool Cursor::mathBackward(bool word)
1773 {
1774         LASSERT(inMathed(), return false);
1775         if (pos() > 0) {
1776                 if (word) {
1777                         // word: skip a group of insets. See the comment in mathForward.
1778                         MathClass mc = prevMath().mathClass();
1779                         do
1780                                 posBackward();
1781                         while (pos() > 0 && mc == prevMath().mathClass());
1782                         if (pos() > 0 && (mc == MC_BIN || mc == MC_REL || mc == MC_PUNCT)) {
1783                                 mc = prevMath().mathClass();
1784                                 do
1785                                         posBackward();
1786                                 while (pos() > 0 && mc == prevMath().mathClass());
1787                         }
1788                 } else if (openable(prevAtom())) {
1789                         // single step: try to enter the preceding inset
1790                         posBackward();
1791                         push(nextMath());
1792                         inset().idxLast(*this);
1793                 } else
1794                         posBackward();
1795                 return true;
1796         }
1797         if (inset().idxBackward(*this))
1798                 return true;
1799         // try to pop backwards --- but don't pop out of math! leave that to
1800         // the FINISH lfuns
1801         int s = depth() - 2;
1802         if (s >= 0 && operator[](s).inset().asInsetMath())
1803                 return popBackward();
1804         return false;
1805 }
1806
1807
1808 bool Cursor::atFirstOrLastRow(bool up)
1809 {
1810         TextMetrics const & tm = bv_->textMetrics(text());
1811         ParagraphMetrics const & pm = tm.parMetrics(pit());
1812
1813         int row;
1814         if (pos() && boundary())
1815                 row = pm.pos2row(pos() - 1);
1816         else
1817                 row = pm.pos2row(pos());
1818
1819         if (up) {
1820                 if (pit() == 0 && row == 0)
1821                         return true;
1822         } else {
1823                 if (pit() + 1 >= int(text()->paragraphs().size()) &&
1824                                 row + 1 >= int(pm.rows().size()))
1825                         return true;
1826         }
1827         return false;
1828 }
1829
1830
1831 bool Cursor::upDownInText(bool up, bool & updateNeeded)
1832 {
1833         LASSERT(text(), return false);
1834
1835         // where are we?
1836         int xo = 0;
1837         int yo = 0;
1838         getPos(xo, yo);
1839         xo = beforeDispatchPosX_;
1840
1841         // update the targetX - this is here before the "return false"
1842         // to set a new target which can be used by InsetTexts above
1843         // if we cannot move up/down inside this inset anymore
1844         if (x_target_ == -1)
1845                 setTargetX(xo);
1846         else if (xo - textTargetOffset() != x_target() &&
1847                                          depth() == beforeDispatchCursor_.depth()) {
1848                 // In text mode inside the line (not left or right)
1849                 // possibly set a new target_x, but only if we are
1850                 // somewhere else than the previous target-offset.
1851
1852                 // We want to keep the x-target on subsequent up/down
1853                 // movements that cross beyond the end of short lines.
1854                 // Thus a special handling when the cursor is at the
1855                 // end of line: Use the new x-target only if the old
1856                 // one was before the end of line or the old one was
1857                 // after the beginning of the line
1858                 bool inRTL = innerParagraph().isRTL(bv().buffer().params());
1859                 bool left;
1860                 bool right;
1861                 if (inRTL) {
1862                         left = pos() == textRow().endpos();
1863                         right = pos() == textRow().pos();
1864                 } else {
1865                         left = pos() == textRow().pos();
1866                         right = pos() == textRow().endpos();
1867                 }
1868                 if ((!left && !right) ||
1869                                 (left && !right && xo < x_target_) ||
1870                                 (!left && right && x_target_ < xo))
1871                         setTargetX(xo);
1872                 else
1873                         xo = targetX();
1874         } else
1875                 xo = targetX();
1876
1877         // first get the current line
1878         TextMetrics & tm = bv_->textMetrics(text());
1879         ParagraphMetrics const & pm = tm.parMetrics(pit());
1880         int row;
1881         if (pos() && boundary())
1882                 row = pm.pos2row(pos() - 1);
1883         else
1884                 row = pm.pos2row(pos());
1885
1886         if (atFirstOrLastRow(up)) {
1887                 // Is there a place for the cursor to go ? If yes, we
1888                 // can execute the DEPM, otherwise we should keep the
1889                 // paragraph to host the cursor.
1890                 Cursor dummy = *this;
1891                 bool valid_destination = false;
1892                 for(; dummy.depth(); dummy.pop())
1893                         if (!dummy.atFirstOrLastRow(up)) {
1894                                 valid_destination = true;
1895                                 break;
1896                         }
1897
1898                 // will a next dispatch follow and if there is a new
1899                 // dispatch will it move the cursor out ?
1900                 if (depth() > 1 && valid_destination) {
1901                         // The cursor hasn't changed yet. This happens when
1902                         // you e.g. move out of an inset. And to give the
1903                         // DEPM the possibility of doing something we must
1904                         // provide it with two different cursors. (Lgb, vfr)
1905                         dummy = *this;
1906                         dummy.pos() = dummy.pos() == 0 ? dummy.lastpos() : 0;
1907                         dummy.pit() = dummy.pit() == 0 ? dummy.lastpit() : 0;
1908
1909                         updateNeeded |= bv().checkDepm(dummy, *this);
1910                         updateTextTargetOffset();
1911                         if (updateNeeded)
1912                                 forceBufferUpdate();
1913                 }
1914                 return false;
1915         }
1916
1917         // with and without selection are handled differently
1918         if (!selection()) {
1919                 int yo = bv().getPos(*this).y_;
1920                 Cursor old = *this;
1921                 // To next/previous row
1922                 // FIXME: the y position is often guessed wrongly across styles and
1923                 // insets, which leads to weird behaviour.
1924                 if (up)
1925                         tm.editXY(*this, xo, yo - textRow().ascent() - 1);
1926                 else
1927                         tm.editXY(*this, xo, yo + textRow().descent() + 1);
1928                 x_target_ = old.x_target_;
1929                 clearSelection();
1930
1931                 // This happens when you move out of an inset.
1932                 // And to give the DEPM the possibility of doing
1933                 // something we must provide it with two different
1934                 // cursors. (Lgb)
1935                 Cursor dummy = *this;
1936                 if (dummy == old)
1937                         ++dummy.pos();
1938                 if (bv().checkDepm(dummy, old)) {
1939                         updateNeeded = true;
1940                         // Make sure that cur gets back whatever happened to dummy (Lgb)
1941                         operator=(dummy);
1942                 }
1943                 if (inTexted() && pos() && paragraph().isEnvSeparator(pos() - 1))
1944                         posBackward();
1945         } else {
1946                 // if there is a selection, we stay out of any inset,
1947                 // and just jump to the right position:
1948                 Cursor old = *this;
1949                 int next_row = row;
1950                 if (up) {
1951                         if (row > 0) {
1952                                 --next_row;
1953                         } else if (pit() > 0) {
1954                                 --pit();
1955                                 TextMetrics & tm = bv_->textMetrics(text());
1956                                 if (!tm.contains(pit()))
1957                                         tm.newParMetricsUp();
1958                                 ParagraphMetrics const & pmcur = tm.parMetrics(pit());
1959                                 next_row = pmcur.rows().size() - 1;
1960                         }
1961                 } else {
1962                         if (row + 1 < int(pm.rows().size())) {
1963                                 ++next_row;
1964                         } else if (pit() + 1 < int(text()->paragraphs().size())) {
1965                                 ++pit();
1966                                 TextMetrics & tm = bv_->textMetrics(text());
1967                                 if (!tm.contains(pit()))
1968                                         tm.newParMetricsDown();
1969                                 next_row = 0;
1970                         }
1971                 }
1972
1973                 Row const & real_next_row = tm.parMetrics(pit()).rows()[next_row];
1974                 bool bound = false;
1975                 top().pos() = tm.getPosNearX(real_next_row, xo, bound);
1976                 boundary(bound);
1977                 // When selection==false, this is done by TextMetrics::editXY
1978                 setCurrentFont();
1979
1980                 updateNeeded |= bv().checkDepm(*this, old);
1981         }
1982
1983         if (updateNeeded)
1984                 forceBufferUpdate();
1985         updateTextTargetOffset();
1986         return true;
1987 }
1988
1989
1990 void Cursor::handleFont(string const & font)
1991 {
1992         LYXERR(Debug::DEBUG, font);
1993         docstring safe;
1994         if (selection()) {
1995                 macroModeClose();
1996                 safe = cap::grabAndEraseSelection(*this);
1997         }
1998
1999         recordUndoInset();
2000
2001         if (lastpos() != 0) {
2002                 // something left in the cell
2003                 if (pos() == 0) {
2004                         // cursor in first position
2005                         popBackward();
2006                 } else if (pos() == lastpos()) {
2007                         // cursor in last position
2008                         popForward();
2009                 } else {
2010                         // cursor in between. split cell
2011                         MathData::iterator bt = cell().begin();
2012                         MathAtom at = createInsetMath(from_utf8(font), buffer());
2013                         at.nucleus()->cell(0) = MathData(buffer(), bt, bt + pos());
2014                         cell().erase(bt, bt + pos());
2015                         popBackward();
2016                         plainInsert(at);
2017                 }
2018         } else {
2019                 // nothing left in the cell
2020                 popBackward();
2021                 plainErase();
2022                 resetAnchor();
2023         }
2024         insert(safe);
2025 }
2026
2027
2028 void Cursor::message(docstring const & msg) const
2029 {
2030         disp_.setMessage(msg);
2031 }
2032
2033
2034 void Cursor::errorMessage(docstring const & msg) const
2035 {
2036         disp_.setMessage(msg);
2037         disp_.setError(true);
2038 }
2039
2040
2041 namespace {
2042
2043 docstring parbreak(Cursor const * cur)
2044 {
2045         odocstringstream os;
2046         os << '\n';
2047         // only add blank line if we're not in a ParbreakIsNewline situation
2048         if (!cur->inset().getLayout().parbreakIsNewline()
2049             && !cur->paragraph().layout().parbreak_is_newline)
2050                 os << '\n';
2051         return os.str();
2052 }
2053
2054 } // namespace
2055
2056
2057 docstring Cursor::selectionAsString(bool with_label) const
2058 {
2059         if (!selection())
2060                 return docstring();
2061
2062         if (inMathed())
2063                 return cap::grabSelection(*this);
2064
2065         int const label = with_label
2066                 ? AS_STR_LABEL | AS_STR_INSETS : AS_STR_INSETS;
2067
2068         idx_type const startidx = selBegin().idx();
2069         idx_type const endidx = selEnd().idx();
2070         if (startidx != endidx) {
2071                 // multicell selection
2072                 InsetTabular * table = inset().asInsetTabular();
2073                 LASSERT(table, return docstring());
2074                 return table->asString(startidx, endidx);
2075         }
2076
2077         ParagraphList const & pars = text()->paragraphs();
2078
2079         pit_type const startpit = selBegin().pit();
2080         pit_type const endpit = selEnd().pit();
2081         size_t const startpos = selBegin().pos();
2082         size_t const endpos = selEnd().pos();
2083
2084         if (startpit == endpit)
2085                 return pars[startpit].asString(startpos, endpos, label);
2086
2087         // First paragraph in selection
2088         docstring result = pars[startpit].
2089                 asString(startpos, pars[startpit].size(), label)
2090                 + parbreak(this);
2091
2092         // The paragraphs in between (if any)
2093         for (pit_type pit = startpit + 1; pit != endpit; ++pit) {
2094                 Paragraph const & par = pars[pit];
2095                 result += par.asString(0, par.size(), label)
2096                         + parbreak(this);
2097         }
2098
2099         // Last paragraph in selection
2100         result += pars[endpit].asString(0, endpos, label);
2101
2102         return result;
2103 }
2104
2105
2106 docstring Cursor::currentState(bool devel_mode) const
2107 {
2108         if (inMathed()) {
2109                 odocstringstream os;
2110                 info(os, devel_mode);
2111                 return os.str();
2112         }
2113
2114         if (inTexted())
2115                 return text()->currentState(*this, devel_mode);
2116
2117         return docstring();
2118 }
2119
2120
2121 docstring Cursor::getPossibleLabel() const
2122 {
2123         return inMathed() ? from_ascii("eq:") : text()->getPossibleLabel(*this);
2124 }
2125
2126
2127 Encoding const * Cursor::getEncoding() const
2128 {
2129         if (empty())
2130                 return 0;
2131         BufferParams const & bp = bv().buffer().params();
2132         if (bp.useNonTeXFonts)
2133                 return encodings.fromLyXName("utf8-plain");
2134
2135         CursorSlice const & sl = innerTextSlice();
2136         Text const & text = *sl.text();
2137         Font font = text.getPar(sl.pit()).getFont(bp, sl.pos(),
2138                                                   text.outerFont(sl.pit()));
2139         return font.language()->encoding();
2140 }
2141
2142
2143 void Cursor::undispatched() const
2144 {
2145         disp_.dispatched(false);
2146 }
2147
2148
2149 void Cursor::dispatched() const
2150 {
2151         disp_.dispatched(true);
2152 }
2153
2154
2155 void Cursor::screenUpdateFlags(Update::flags f) const
2156 {
2157         disp_.screenUpdate(f);
2158 }
2159
2160
2161 void Cursor::forceBufferUpdate() const
2162 {
2163         disp_.forceBufferUpdate();
2164 }
2165
2166
2167 void Cursor::clearBufferUpdate() const
2168 {
2169         disp_.clearBufferUpdate();
2170 }
2171
2172
2173 bool Cursor::needBufferUpdate() const
2174 {
2175         return disp_.needBufferUpdate();
2176 }
2177
2178
2179 void Cursor::noScreenUpdate() const
2180 {
2181         disp_.screenUpdate(Update::None);
2182 }
2183
2184
2185 Font Cursor::getFont() const
2186 {
2187         // The logic here should more or less match to the
2188         // Cursor::setCurrentFont logic, i.e. the cursor height should
2189         // give a hint what will happen if a character is entered.
2190         // FIXME: this is not the case, what about removing this method ? (see #10478).
2191
2192         // HACK. far from being perfect...
2193
2194         CursorSlice const & sl = innerTextSlice();
2195         Text const & text = *sl.text();
2196         Paragraph const & par = text.getPar(sl.pit());
2197
2198         // on boundary, so we are really at the character before
2199         pos_type pos = sl.pos();
2200         if (pos > 0 && boundary())
2201                 --pos;
2202
2203         // on space? Take the font before (only for RTL boundary stay)
2204         if (pos > 0) {
2205                 TextMetrics const & tm = bv().textMetrics(&text);
2206                 if (pos == sl.lastpos()
2207                         || (par.isSeparator(pos)
2208                         && !tm.isRTLBoundary(sl.pit(), pos)))
2209                         --pos;
2210         }
2211
2212         // get font at the position
2213         Font font = par.getFont(buffer()->params(), pos,
2214                 text.outerFont(sl.pit()));
2215
2216         return font;
2217 }
2218
2219
2220 bool Cursor::fixIfBroken()
2221 {
2222         bool const broken_cursor = DocIterator::fixIfBroken();
2223         bool const broken_anchor = anchor_.fixIfBroken();
2224
2225         if (broken_cursor || broken_anchor) {
2226                 clearNewWordPosition();
2227                 clearSelection();
2228                 return true;
2229         }
2230         return false;
2231 }
2232
2233
2234 void Cursor::sanitize()
2235 {
2236         setBuffer(&bv_->buffer());
2237         DocIterator::sanitize();
2238         new_word_.sanitize();
2239         if (selection())
2240                 anchor_.sanitize();
2241         else
2242                 resetAnchor();
2243 }
2244
2245
2246 bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur)
2247 {
2248         // find inset in common
2249         size_type i;
2250         for (i = 0; i < old.depth() && i < cur.depth(); ++i) {
2251                 if (&old[i].inset() != &cur[i].inset())
2252                         break;
2253         }
2254
2255         // update words if we just moved to another paragraph
2256         if (i == old.depth() && i == cur.depth()
2257             && !cur.buffer()->isClean()
2258             && cur.inTexted() && old.inTexted()
2259             && cur.pit() != old.pit()) {
2260                 old.paragraph().updateWords();
2261         }
2262
2263         // notify everything on top of the common part in old cursor,
2264         // but stop if the inset claims the cursor to be invalid now
2265         for (size_type j = i; j < old.depth(); ++j) {
2266                 Cursor inset_pos = old;
2267                 inset_pos.cutOff(j);
2268                 if (old[j].inset().notifyCursorLeaves(inset_pos, cur))
2269                         return true;
2270         }
2271
2272         // notify everything on top of the common part in new cursor,
2273         // but stop if the inset claims the cursor to be invalid now
2274         for (; i < cur.depth(); ++i) {
2275                 if (cur[i].inset().notifyCursorEnters(cur))
2276                         return true;
2277         }
2278
2279         return false;
2280 }
2281
2282
2283 void Cursor::setCurrentFont()
2284 {
2285         CursorSlice const & cs = innerTextSlice();
2286         Paragraph const & par = cs.paragraph();
2287         pos_type cpit = cs.pit();
2288         pos_type cpos = cs.pos();
2289         Text const & ctext = *cs.text();
2290         TextMetrics const & tm = bv().textMetrics(&ctext);
2291
2292         // are we behind previous char in fact? -> go to that char
2293         if (cpos > 0 && boundary())
2294                 --cpos;
2295
2296         // find position to take the font from
2297         if (cpos != 0) {
2298                 // paragraph end? -> font of last char
2299                 if (cpos == lastpos())
2300                         --cpos;
2301                 // on space? -> look at the words in front of space
2302                 else if (cpos > 0 && par.isSeparator(cpos))     {
2303                         // abc| def -> font of c
2304                         // abc |[WERBEH], i.e. boundary==true -> font of c
2305                         // abc [WERBEH]| def, font of the space
2306                         if (!tm.isRTLBoundary(cpit, cpos))
2307                                 --cpos;
2308                 }
2309         }
2310
2311         // get font
2312         BufferParams const & bufparams = buffer()->params();
2313         current_font = par.getFontSettings(bufparams, cpos);
2314         real_current_font = tm.displayFont(cpit, cpos);
2315
2316         // special case for paragraph end
2317         if (cs.pos() == lastpos()
2318             && tm.isRTLBoundary(cpit, cs.pos())
2319             && !boundary()) {
2320                 Language const * lang = par.getParLanguage(bufparams);
2321                 current_font.setLanguage(lang);
2322                 current_font.fontInfo().setNumber(FONT_OFF);
2323                 real_current_font.setLanguage(lang);
2324                 real_current_font.fontInfo().setNumber(FONT_OFF);
2325         }
2326 }
2327
2328
2329 bool Cursor::textUndo()
2330 {
2331         if (!buffer()->undo().textUndo(*this))
2332                 return false;
2333         sanitize();
2334         return true;
2335 }
2336
2337
2338 bool Cursor::textRedo()
2339 {
2340         if (!buffer()->undo().textRedo(*this))
2341                 return false;
2342         sanitize();
2343         return true;
2344 }
2345
2346
2347 void Cursor::finishUndo() const
2348 {
2349         buffer()->undo().finishUndo();
2350 }
2351
2352
2353 void Cursor::beginUndoGroup() const
2354 {
2355         buffer()->undo().beginUndoGroup(*this);
2356 }
2357
2358
2359 void Cursor::endUndoGroup() const
2360 {
2361         buffer()->undo().endUndoGroup(*this);
2362 }
2363
2364
2365 void Cursor::recordUndo(pit_type from, pit_type to) const
2366 {
2367         buffer()->undo().recordUndo(*this, from, to);
2368 }
2369
2370
2371 void Cursor::recordUndo(pit_type from) const
2372 {
2373         buffer()->undo().recordUndo(*this, from, pit());
2374 }
2375
2376
2377 void Cursor::recordUndo(UndoKind kind) const
2378 {
2379         buffer()->undo().recordUndo(*this, kind);
2380 }
2381
2382
2383 void Cursor::recordUndoInset(Inset const * in) const
2384 {
2385         buffer()->undo().recordUndoInset(*this, in);
2386 }
2387
2388
2389 void Cursor::recordUndoFullBuffer() const
2390 {
2391         buffer()->undo().recordUndoFullBuffer(*this);
2392 }
2393
2394
2395 void Cursor::recordUndoBufferParams() const
2396 {
2397         buffer()->undo().recordUndoBufferParams(*this);
2398 }
2399
2400
2401 void Cursor::recordUndoSelection() const
2402 {
2403         if (inMathed()) {
2404                 if (cap::multipleCellsSelected(*this))
2405                         recordUndoInset();
2406                 else
2407                         recordUndo();
2408         } else {
2409                 buffer()->undo().recordUndo(*this,
2410                         selBegin().pit(), selEnd().pit());
2411         }
2412 }
2413
2414
2415 void Cursor::checkBufferStructure()
2416 {
2417         Buffer const * master = buffer()->masterBuffer();
2418         master->tocBackend().updateItem(*this);
2419         if (master != buffer() && !master->hasGuiDelegate())
2420                 // In case the master has no gui associated with it,
2421                 // the TocItem is not updated (part of bug 5699).
2422                 buffer()->tocBackend().updateItem(*this);
2423
2424         // If the last tracked change of the paragraph has just been
2425         // deleted, then we need to recompute the buffer flag
2426         // tracked_changes_present_.
2427         if (inTexted() && paragraph().isChangeUpdateRequired())
2428                 disp_.forceChangesUpdate();
2429 }
2430
2431
2432 bool Cursor::confirmDeletion(bool const before) const
2433 {
2434         if (!selection()) {
2435                 if (Inset const * inset = before ? prevInset() : nextInset())
2436                         return inset->confirmDeletion();
2437         } else {
2438                 DocIterator dit = selectionBegin();
2439                 CursorSlice const end = selectionEnd().top();
2440                 for (; dit.top() < end; dit.top().forwardPos())
2441                         if (Inset const * inset = dit.nextInset())
2442                                 if (inset->confirmDeletion())
2443                                         return true;
2444         }
2445         return false;
2446 }
2447
2448
2449 void Cursor::moveToClosestEdge(int const x, bool const edit)
2450 {
2451         if (Inset const * inset = nextInset()) {
2452                 // stay in front of insets for which we want to open the dialog
2453                 // (e.g. InsetMathSpace).
2454                 if (edit && (inset->hasSettings() || !inset->contextMenuName().empty()))
2455                         return;
2456                 CoordCache::Insets const & insetCache = bv().coordCache().getInsets();
2457                 if (!insetCache.has(inset))
2458                         return;
2459                 int const wid = insetCache.dim(inset).wid;
2460                 Point p = insetCache.xy(inset);
2461                 if (x > p.x_ + (wid + 1) / 2)
2462                         posForward();
2463         }
2464 }
2465
2466
2467 } // namespace lyx