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