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