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