]> git.lyx.org Git - lyx.git/blob - src/Cursor.cpp
revert r35282
[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         forceBufferUpdate();
1280 }
1281
1282
1283 void Cursor::insert(docstring const & str)
1284 {
1285         for_each(str.begin(), str.end(),
1286                  bind(static_cast<void(Cursor::*)(char_type)>
1287                              (&Cursor::insert), this, _1));
1288 }
1289
1290
1291 void Cursor::insert(char_type c)
1292 {
1293         //lyxerr << "Cursor::insert char '" << c << "'" << endl;
1294         LASSERT(!empty(), /**/);
1295         if (inMathed()) {
1296                 cap::selClearOrDel(*this);
1297                 insert(new InsetMathChar(c));
1298         } else {
1299                 text()->insertChar(*this, c);
1300         }
1301 }
1302
1303
1304 void Cursor::insert(MathAtom const & t)
1305 {
1306         //lyxerr << "Cursor::insert MathAtom '" << t << "'" << endl;
1307         macroModeClose();
1308         cap::selClearOrDel(*this);
1309         plainInsert(t);
1310 }
1311
1312
1313 void Cursor::insert(Inset * inset0)
1314 {
1315         LASSERT(inset0, /**/);
1316         if (inMathed())
1317                 insert(MathAtom(inset0));
1318         else {
1319                 text()->insertInset(*this, inset0);
1320                 inset0->setBuffer(bv_->buffer());
1321                 inset0->initView();
1322                 if (inset0->isLabeled())
1323                         forceBufferUpdate();
1324         }
1325 }
1326
1327
1328 void Cursor::niceInsert(docstring const & t, Parse::flags f, bool enter)
1329 {
1330         MathData ar(buffer());
1331         asArray(t, ar, f);
1332         if (ar.size() == 1 && (enter || selection()))
1333                 niceInsert(ar[0]);
1334         else
1335                 insert(ar);
1336 }
1337
1338
1339 void Cursor::niceInsert(MathAtom const & t)
1340 {
1341         macroModeClose();
1342         docstring const safe = cap::grabAndEraseSelection(*this);
1343         plainInsert(t);
1344         // If possible, enter the new inset and move the contents of the selection
1345         if (t->isActive()) {
1346                 posBackward();
1347                 // be careful here: don't use 'pushBackward(t)' as this we need to
1348                 // push the clone, not the original
1349                 pushBackward(*nextInset());
1350                 // We may not use niceInsert here (recursion)
1351                 MathData ar(buffer());
1352                 asArray(safe, ar);
1353                 insert(ar);
1354         }
1355 }
1356
1357
1358 void Cursor::insert(MathData const & ar)
1359 {
1360         macroModeClose();
1361         if (selection())
1362                 cap::eraseSelection(*this);
1363         cell().insert(pos(), ar);
1364         pos() += ar.size();
1365         // FIXME audit setBuffer calls
1366         inset().setBuffer(bv_->buffer());
1367 }
1368
1369
1370 bool Cursor::backspace()
1371 {
1372         autocorrect() = false;
1373
1374         if (selection()) {
1375                 cap::eraseSelection(*this);
1376                 return true;
1377         }
1378
1379         if (pos() == 0) {
1380                 // If empty cell, and not part of a big cell
1381                 if (lastpos() == 0 && inset().nargs() == 1) {
1382                         popBackward();
1383                         // Directly delete empty cell: [|[]] => [|]
1384                         if (inMathed()) {
1385                                 plainErase();
1386                                 resetAnchor();
1387                                 return true;
1388                         }
1389                         // [|], can not delete from inside
1390                         return false;
1391                 } else {
1392                         if (inMathed())
1393                                 pullArg();
1394                         else
1395                                 popBackward();
1396                         return true;
1397                 }
1398         }
1399
1400         if (inMacroMode()) {
1401                 InsetMathUnknown * p = activeMacro();
1402                 if (p->name().size() > 1) {
1403                         p->setName(p->name().substr(0, p->name().size() - 1));
1404                         return true;
1405                 }
1406         }
1407
1408         if (pos() != 0 && prevAtom()->nargs() > 0) {
1409                 // let's require two backspaces for 'big stuff' and
1410                 // highlight on the first
1411                 resetAnchor();
1412                 setSelection(true);
1413                 --pos();
1414         } else {
1415                 --pos();
1416                 plainErase();
1417         }
1418         return true;
1419 }
1420
1421
1422 bool Cursor::erase()
1423 {
1424         autocorrect() = false;
1425         if (inMacroMode())
1426                 return true;
1427
1428         if (selection()) {
1429                 cap::eraseSelection(*this);
1430                 return true;
1431         }
1432
1433         // delete empty cells if possible
1434         if (pos() == lastpos() && inset().idxDelete(idx()))
1435                 return true;
1436
1437         // special behaviour when in last position of cell
1438         if (pos() == lastpos()) {
1439                 bool one_cell = inset().nargs() == 1;
1440                 if (one_cell && lastpos() == 0) {
1441                         popBackward();
1442                         // Directly delete empty cell: [|[]] => [|]
1443                         if (inMathed()) {
1444                                 plainErase();
1445                                 resetAnchor();
1446                                 return true;
1447                         }
1448                         // [|], can not delete from inside
1449                         return false;
1450                 }
1451                 // remove markup
1452                 if (!one_cell)
1453                         inset().idxGlue(idx());
1454                 return true;
1455         }
1456
1457         // 'clever' UI hack: only erase large items if previously slected
1458         if (pos() != lastpos() && nextAtom()->nargs() > 0) {
1459                 resetAnchor();
1460                 setSelection(true);
1461                 ++pos();
1462         } else {
1463                 plainErase();
1464         }
1465
1466         return true;
1467 }
1468
1469
1470 bool Cursor::up()
1471 {
1472         macroModeClose();
1473         DocIterator save = *this;
1474         FuncRequest cmd(selection() ? LFUN_UP_SELECT : LFUN_UP, docstring());
1475         this->dispatch(cmd);
1476         if (disp_.dispatched())
1477                 return true;
1478         setCursor(save);
1479         autocorrect() = false;
1480         return false;
1481 }
1482
1483
1484 bool Cursor::down()
1485 {
1486         macroModeClose();
1487         DocIterator save = *this;
1488         FuncRequest cmd(selection() ? LFUN_DOWN_SELECT : LFUN_DOWN, docstring());
1489         this->dispatch(cmd);
1490         if (disp_.dispatched())
1491                 return true;
1492         setCursor(save);
1493         autocorrect() = false;
1494         return false;
1495 }
1496
1497
1498 bool Cursor::macroModeClose()
1499 {
1500         if (!inMacroMode())
1501                 return false;
1502         InsetMathUnknown * p = activeMacro();
1503         p->finalize();
1504         MathData selection(buffer());
1505         asArray(p->selection(), selection);
1506         docstring const s = p->name();
1507         --pos();
1508         cell().erase(pos());
1509
1510         // do nothing if the macro name is empty
1511         if (s == "\\")
1512                 return false;
1513
1514         // trigger updates of macros, at least, if no full
1515         // updates take place anyway
1516         screenUpdateFlags(Update::Force);
1517
1518         docstring const name = s.substr(1);
1519         InsetMathNest * const in = inset().asInsetMath()->asNestInset();
1520         if (in && in->interpretString(*this, s))
1521                 return true;
1522         MathAtom atom = buffer()->getMacro(name, *this, false) ?
1523                 MathAtom(new MathMacro(buffer(), name)) : createInsetMath(name, buffer());
1524
1525         // try to put argument into macro, if we just inserted a macro
1526         bool macroArg = false;
1527         MathMacro * atomAsMacro = atom.nucleus()->asMacro();
1528         if (atomAsMacro) {
1529                 // macros here are still unfolded (in init mode in fact). So
1530                 // we have to resolve the macro here manually and check its arity
1531                 // to put the selection behind it if arity > 0.
1532                 MacroData const * data = buffer()->getMacro(atomAsMacro->name());
1533                 if (selection.size() > 0 && data && data->numargs() - data->optionals() > 0) {
1534                         macroArg = true;
1535                         atomAsMacro->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT, 1);
1536                 } else
1537                         // non-greedy case. Do not touch the arguments behind
1538                         atomAsMacro->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT, 0);
1539         }
1540
1541         // insert remembered selection into first argument of a non-macro
1542         else if (atom.nucleus()->nargs() > 0)
1543                 atom.nucleus()->cell(0).append(selection);
1544         
1545         plainInsert(atom);
1546
1547         // finally put the macro argument behind, if needed
1548         if (macroArg) {
1549                 if (selection.size() > 1 || selection[0]->asScriptInset())
1550                         plainInsert(MathAtom(new InsetMathBrace(selection)));
1551                 else
1552                         insert(selection);
1553         }
1554         
1555         return true;
1556 }
1557
1558
1559 docstring Cursor::macroName()
1560 {
1561         return inMacroMode() ? activeMacro()->name() : docstring();
1562 }
1563
1564
1565 void Cursor::handleNest(MathAtom const & a, int c)
1566 {
1567         //lyxerr << "Cursor::handleNest: " << c << endl;
1568         MathAtom t = a;
1569         asArray(cap::grabAndEraseSelection(*this), t.nucleus()->cell(c));
1570         insert(t);
1571         posBackward();
1572         pushBackward(*nextInset());
1573 }
1574
1575
1576 int Cursor::targetX() const
1577 {
1578         if (x_target() != -1)
1579                 return x_target();
1580         int x = 0;
1581         int y = 0;
1582         getPos(x, y);
1583         return x;
1584 }
1585
1586
1587 int Cursor::textTargetOffset() const
1588 {
1589         return textTargetOffset_;
1590 }
1591
1592
1593 void Cursor::setTargetX()
1594 {
1595         int x;
1596         int y;
1597         getPos(x, y);
1598         setTargetX(x);
1599 }
1600
1601
1602 bool Cursor::inMacroMode() const
1603 {
1604         if (!inMathed())
1605                 return false;
1606         if (pos() == 0)
1607                 return false;
1608         InsetMathUnknown const * p = prevAtom()->asUnknownInset();
1609         return p && !p->final();
1610 }
1611
1612
1613 InsetMathUnknown * Cursor::activeMacro()
1614 {
1615         return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1616 }
1617
1618
1619 InsetMathUnknown const * Cursor::activeMacro() const
1620 {
1621         return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1622 }
1623
1624
1625 void Cursor::pullArg()
1626 {
1627         // FIXME: Look here
1628         MathData ar = cell();
1629         if (popBackward() && inMathed()) {
1630                 plainErase();
1631                 cell().insert(pos(), ar);
1632                 resetAnchor();
1633         } else {
1634                 //formula()->mutateToText();
1635         }
1636 }
1637
1638
1639 void Cursor::touch()
1640 {
1641         // FIXME: look here
1642 #if 0
1643         DocIterator::const_iterator it = begin();
1644         DocIterator::const_iterator et = end();
1645         for ( ; it != et; ++it)
1646                 it->cell().touch();
1647 #endif
1648 }
1649
1650
1651 void Cursor::normalize()
1652 {
1653         if (idx() > lastidx()) {
1654                 lyxerr << "this should not really happen - 1: "
1655                        << idx() << ' ' << nargs()
1656                        << " in: " << &inset() << endl;
1657                 idx() = lastidx();
1658         }
1659
1660         if (pos() > lastpos()) {
1661                 lyxerr << "this should not really happen - 2: "
1662                         << pos() << ' ' << lastpos() <<  " in idx: " << idx()
1663                        << " in atom: '";
1664                 odocstringstream os;
1665                 WriteStream wi(os, false, true, WriteStream::wsDefault);
1666                 inset().asInsetMath()->write(wi);
1667                 lyxerr << to_utf8(os.str()) << endl;
1668                 pos() = lastpos();
1669         }
1670 }
1671
1672
1673 bool Cursor::upDownInMath(bool up)
1674 {
1675         // Be warned: The 'logic' implemented in this function is highly
1676         // fragile. A distance of one pixel or a '<' vs '<=' _really
1677         // matters. So fiddle around with it only if you think you know
1678         // what you are doing!
1679         int xo = 0;
1680         int yo = 0;
1681         getPos(xo, yo);
1682         xo = beforeDispatchPosX_;
1683
1684         // check if we had something else in mind, if not, this is the future
1685         // target
1686         if (x_target_ == -1)
1687                 setTargetX(xo);
1688         else if (inset().asInsetText() && xo - textTargetOffset() != x_target()) {
1689                 // In text mode inside the line (not left or right) possibly set a new target_x,
1690                 // but only if we are somewhere else than the previous target-offset.
1691                 
1692                 // We want to keep the x-target on subsequent up/down movements
1693                 // that cross beyond the end of short lines. Thus a special
1694                 // handling when the cursor is at the end of line: Use the new
1695                 // x-target only if the old one was before the end of line
1696                 // or the old one was after the beginning of the line
1697                 bool inRTL = isWithinRtlParagraph(*this);
1698                 bool left;
1699                 bool right;
1700                 if (inRTL) {
1701                         left = pos() == textRow().endpos();
1702                         right = pos() == textRow().pos();
1703                 } else {
1704                         left = pos() == textRow().pos();
1705                         right = pos() == textRow().endpos();
1706                 }
1707                 if ((!left && !right) ||
1708                                 (left && !right && xo < x_target_) ||
1709                                 (!left && right && x_target_ < xo))
1710                         setTargetX(xo);
1711                 else
1712                         xo = targetX();
1713         } else
1714                 xo = targetX();
1715
1716         // try neigbouring script insets
1717         Cursor old = *this;
1718         if (inMathed() && !selection()) {
1719                 // try left
1720                 if (pos() != 0) {
1721                         InsetMathScript const * p = prevAtom()->asScriptInset();
1722                         if (p && p->has(up)) {
1723                                 --pos();
1724                                 push(*const_cast<InsetMathScript*>(p));
1725                                 idx() = p->idxOfScript(up);
1726                                 pos() = lastpos();
1727                                 
1728                                 // we went in the right direction? Otherwise don't jump into the script
1729                                 int x;
1730                                 int y;
1731                                 getPos(x, y);
1732                                 int oy = beforeDispatchPosY_;
1733                                 if ((!up && y <= oy) ||
1734                                                 (up && y >= oy))
1735                                         operator=(old);
1736                                 else
1737                                         return true;
1738                         }
1739                 }
1740                 
1741                 // try right
1742                 if (pos() != lastpos()) {
1743                         InsetMathScript const * p = nextAtom()->asScriptInset();
1744                         if (p && p->has(up)) {
1745                                 push(*const_cast<InsetMathScript*>(p));
1746                                 idx() = p->idxOfScript(up);
1747                                 pos() = 0;
1748                                 
1749                                 // we went in the right direction? Otherwise don't jump into the script
1750                                 int x;
1751                                 int y;
1752                                 getPos(x, y);
1753                                 int oy = beforeDispatchPosY_;
1754                                 if ((!up && y <= oy) ||
1755                                                 (up && y >= oy))
1756                                         operator=(old);
1757                                 else
1758                                         return true;
1759                         }
1760                 }
1761         }
1762                 
1763         // try to find an inset that knows better then we,
1764         if (inset().idxUpDown(*this, up)) {
1765                 //lyxerr << "idxUpDown triggered" << endl;
1766                 // try to find best position within this inset
1767                 if (!selection())
1768                         setCursor(bruteFind2(*this, xo, yo));
1769                 return true;
1770         }
1771         
1772         // any improvement going just out of inset?
1773         if (popBackward() && inMathed()) {
1774                 //lyxerr << "updown: popBackward succeeded" << endl;
1775                 int xnew;
1776                 int ynew;
1777                 int yold = beforeDispatchPosY_;
1778                 getPos(xnew, ynew);
1779                 if (up ? ynew < yold : ynew > yold)
1780                         return true;
1781         }
1782         
1783         // no success, we are probably at the document top or bottom
1784         operator=(old);
1785         return false;
1786 }
1787
1788
1789 bool Cursor::atFirstOrLastRow(bool up)
1790 {
1791         TextMetrics const & tm = bv_->textMetrics(text());
1792         ParagraphMetrics const & pm = tm.parMetrics(pit());
1793         
1794         int row;
1795         if (pos() && boundary())
1796                 row = pm.pos2row(pos() - 1);
1797         else
1798                 row = pm.pos2row(pos());
1799         
1800         if (up) {
1801                 if (pit() == 0 && row == 0)
1802                         return true;
1803         } else {
1804                 if (pit() + 1 >= int(text()->paragraphs().size()) &&
1805                                 row + 1 >= int(pm.rows().size()))
1806                         return true;
1807         }
1808         return false;
1809 }
1810
1811 bool Cursor::upDownInText(bool up, bool & updateNeeded)
1812 {
1813         LASSERT(text(), /**/);
1814
1815         // where are we?
1816         int xo = 0;
1817         int yo = 0;
1818         getPos(xo, yo);
1819         xo = beforeDispatchPosX_;
1820
1821         // update the targetX - this is here before the "return false"
1822         // to set a new target which can be used by InsetTexts above
1823         // if we cannot move up/down inside this inset anymore
1824         if (x_target_ == -1)
1825                 setTargetX(xo);
1826         else if (xo - textTargetOffset() != x_target() &&
1827                                          depth() == beforeDispatchCursor_.depth()) {
1828                 // In text mode inside the line (not left or right) possibly set a new target_x,
1829                 // but only if we are somewhere else than the previous target-offset.
1830                 
1831                 // We want to keep the x-target on subsequent up/down movements
1832                 // that cross beyond the end of short lines. Thus a special
1833                 // handling when the cursor is at the end of line: Use the new
1834                 // x-target only if the old one was before the end of line
1835                 // or the old one was after the beginning of the line
1836                 bool inRTL = isWithinRtlParagraph(*this);
1837                 bool left;
1838                 bool right;
1839                 if (inRTL) {
1840                         left = pos() == textRow().endpos();
1841                         right = pos() == textRow().pos();
1842                 } else {
1843                         left = pos() == textRow().pos();
1844                         right = pos() == textRow().endpos();
1845                 }
1846                 if ((!left && !right) ||
1847                                 (left && !right && xo < x_target_) ||
1848                                 (!left && right && x_target_ < xo))
1849                         setTargetX(xo);
1850                 else
1851                         xo = targetX();
1852         } else
1853                 xo = targetX();
1854                 
1855         // first get the current line
1856         TextMetrics & tm = bv_->textMetrics(text());
1857         ParagraphMetrics const & pm = tm.parMetrics(pit());
1858         int row;
1859         if (pos() && boundary())
1860                 row = pm.pos2row(pos() - 1);
1861         else
1862                 row = pm.pos2row(pos());
1863                 
1864         if (atFirstOrLastRow(up)) {
1865                 // Is there a place for the cursor to go ? If yes, we
1866                 // can execute the DEPM, otherwise we should keep the
1867                 // paragraph to host the cursor.
1868                 Cursor dummy = *this;
1869                 bool valid_destination = false;
1870                 for(; dummy.depth(); dummy.pop())
1871                         if (!dummy.atFirstOrLastRow(up)) {
1872                                 valid_destination = true;
1873                                 break;
1874                         }
1875
1876                 // will a next dispatch follow and if there is a new 
1877                 // dispatch will it move the cursor out ?
1878                 if (depth() > 1 && valid_destination) {
1879                         // The cursor hasn't changed yet. This happens when
1880                         // you e.g. move out of an inset. And to give the 
1881                         // DEPM the possibility of doing something we must
1882                         // provide it with two different cursors. (Lgb, vfr)
1883                         dummy = *this;
1884                         dummy.pos() = dummy.pos() == 0 ? dummy.lastpos() : 0;
1885                         dummy.pit() = dummy.pit() == 0 ? dummy.lastpit() : 0;
1886
1887                         updateNeeded |= bv().checkDepm(dummy, *this);
1888                         updateTextTargetOffset();
1889                         if (updateNeeded)
1890                                 forceBufferUpdate();
1891                 }
1892                 return false;
1893         }
1894
1895         // with and without selection are handled differently
1896         if (!selection()) {
1897                 int yo = bv().getPos(*this).y_;
1898                 Cursor old = *this;
1899                 // To next/previous row
1900                 if (up)
1901                         tm.editXY(*this, xo, yo - textRow().ascent() - 1);
1902                 else
1903                         tm.editXY(*this, xo, yo + textRow().descent() + 1);
1904                 clearSelection();
1905                 
1906                 // This happens when you move out of an inset.  
1907                 // And to give the DEPM the possibility of doing  
1908                 // something we must provide it with two different  
1909                 // cursors. (Lgb)  
1910                 Cursor dummy = *this;
1911                 if (dummy == old)
1912                         ++dummy.pos();
1913                 if (bv().checkDepm(dummy, old)) {
1914                         updateNeeded = true;
1915                         // Make sure that cur gets back whatever happened to dummy (Lgb) 
1916                         operator=(dummy);
1917                 }
1918         } else {
1919                 // if there is a selection, we stay out of any inset,
1920                 // and just jump to the right position:
1921                 Cursor old = *this;
1922                 int next_row = row;
1923                 if (up) {
1924                         if (row > 0) {
1925                                 --next_row;
1926                         } else if (pit() > 0) {
1927                                 --pit();
1928                                 TextMetrics & tm = bv_->textMetrics(text());
1929                                 if (!tm.contains(pit()))
1930                                         tm.newParMetricsUp();
1931                                 ParagraphMetrics const & pmcur = tm.parMetrics(pit());
1932                                 next_row = pmcur.rows().size() - 1;
1933                         }
1934                 } else {
1935                         if (row + 1 < int(pm.rows().size())) {
1936                                 ++next_row;
1937                         } else if (pit() + 1 < int(text()->paragraphs().size())) {
1938                                 ++pit();
1939                                 TextMetrics & tm = bv_->textMetrics(text());
1940                                 if (!tm.contains(pit()))
1941                                         tm.newParMetricsDown();
1942                                 next_row = 0;
1943                         }
1944                 }
1945                 top().pos() = min(tm.x2pos(pit(), next_row, xo), top().lastpos());
1946
1947                 int const xpos = tm.x2pos(pit(), next_row, xo);
1948                 bool const at_end_row = xpos == tm.x2pos(pit(), next_row, tm.width());
1949                 bool const at_beg_row = xpos == tm.x2pos(pit(), next_row, 0);
1950
1951                 if (at_end_row && at_beg_row)
1952                         // make sure the cursor ends up on this row
1953                         boundary(false);
1954                 else
1955                         boundary(at_end_row);
1956
1957                 updateNeeded |= bv().checkDepm(*this, old);
1958         }
1959
1960         if (updateNeeded)
1961                 forceBufferUpdate();
1962         updateTextTargetOffset();
1963         return true;
1964 }       
1965
1966
1967 void Cursor::handleFont(string const & font)
1968 {
1969         LYXERR(Debug::DEBUG, font);
1970         docstring safe;
1971         if (selection()) {
1972                 macroModeClose();
1973                 safe = cap::grabAndEraseSelection(*this);
1974         }
1975
1976         recordUndoInset();
1977
1978         if (lastpos() != 0) {
1979                 // something left in the cell
1980                 if (pos() == 0) {
1981                         // cursor in first position
1982                         popBackward();
1983                 } else if (pos() == lastpos()) {
1984                         // cursor in last position
1985                         popForward();
1986                 } else {
1987                         // cursor in between. split cell
1988                         MathData::iterator bt = cell().begin();
1989                         MathAtom at = createInsetMath(from_utf8(font), buffer());
1990                         at.nucleus()->cell(0) = MathData(buffer(), bt, bt + pos());
1991                         cell().erase(bt, bt + pos());
1992                         popBackward();
1993                         plainInsert(at);
1994                 }
1995         } else {
1996                 // nothing left in the cell
1997                 popBackward();
1998                 plainErase();
1999                 resetAnchor();
2000         }
2001         insert(safe);
2002 }
2003
2004
2005 void Cursor::message(docstring const & msg) const
2006 {
2007         disp_.setMessage(msg);
2008 }
2009
2010
2011 void Cursor::errorMessage(docstring const & msg) const
2012 {
2013         disp_.setMessage(msg);
2014         disp_.setError(true);
2015 }
2016
2017
2018 static docstring parbreak(InsetCode code)
2019 {
2020         odocstringstream os;
2021         os << '\n';
2022         // only add blank line if we're not in an ERT or Listings inset
2023         if (code != ERT_CODE && code != LISTINGS_CODE)
2024                 os << '\n';
2025         return os.str();
2026 }
2027
2028
2029 docstring Cursor::selectionAsString(bool with_label) const
2030 {
2031         if (!selection())
2032                 return docstring();
2033
2034         if (inMathed())
2035                 return cap::grabSelection(*this);
2036
2037         int const label = with_label
2038                 ? AS_STR_LABEL | AS_STR_INSETS : AS_STR_INSETS;
2039
2040         idx_type const startidx = selBegin().idx();
2041         idx_type const endidx = selEnd().idx();
2042         if (startidx != endidx) {
2043                 // multicell selection
2044                 InsetTabular * table = inset().asInsetTabular();
2045                 LASSERT(table, return docstring());
2046                 return table->asString(startidx, endidx);
2047         }
2048
2049         ParagraphList const & pars = text()->paragraphs();
2050
2051         pit_type const startpit = selBegin().pit();
2052         pit_type const endpit = selEnd().pit();
2053         size_t const startpos = selBegin().pos();
2054         size_t const endpos = selEnd().pos();
2055
2056         if (startpit == endpit)
2057                 return pars[startpit].asString(startpos, endpos, label);
2058
2059         // First paragraph in selection
2060         docstring result = pars[startpit].
2061                 asString(startpos, pars[startpit].size(), label)
2062                 + parbreak(inset().lyxCode());
2063
2064         // The paragraphs in between (if any)
2065         for (pit_type pit = startpit + 1; pit != endpit; ++pit) {
2066                 Paragraph const & par = pars[pit];
2067                 result += par.asString(0, par.size(), label)
2068                         + parbreak(inset().lyxCode());
2069         }
2070
2071         // Last paragraph in selection
2072         result += pars[endpit].asString(0, endpos, label);
2073
2074         return result;
2075 }
2076
2077
2078 docstring Cursor::currentState() const
2079 {
2080         if (inMathed()) {
2081                 odocstringstream os;
2082                 info(os);
2083                 return os.str();
2084         }
2085
2086         if (inTexted())
2087                 return text()->currentState(*this);
2088
2089         return docstring();
2090 }
2091
2092
2093 docstring Cursor::getPossibleLabel() const
2094 {
2095         return inMathed() ? from_ascii("eq:") : text()->getPossibleLabel(*this);
2096 }
2097
2098
2099 Encoding const * Cursor::getEncoding() const
2100 {
2101         if (empty())
2102                 return 0;
2103         CursorSlice const & sl = innerTextSlice();
2104         Text const & text = *sl.text();
2105         Font font = text.getPar(sl.pit()).getFont(
2106                 bv().buffer().params(), sl.pos(), text.outerFont(sl.pit()));
2107         return font.language()->encoding();
2108 }
2109
2110
2111 void Cursor::undispatched()
2112 {
2113         disp_.dispatched(false);
2114 }
2115
2116
2117 void Cursor::dispatched()
2118 {
2119         disp_.dispatched(true);
2120 }
2121
2122
2123 void Cursor::screenUpdateFlags(Update::flags f)
2124 {
2125         disp_.update(f);
2126 }
2127
2128
2129 void Cursor::forceBufferUpdate()
2130 {
2131         disp_.forceBufferUpdate();
2132 }
2133
2134
2135 void Cursor::clearBufferUpdate()
2136 {
2137         disp_.clearBufferUpdate();
2138 }
2139
2140
2141 bool Cursor::needBufferUpdate() const
2142 {
2143         return disp_.needBufferUpdate();
2144 }
2145
2146
2147 void Cursor::noScreenUpdate()
2148 {
2149         disp_.update(Update::None);
2150 }
2151
2152
2153 Font Cursor::getFont() const
2154 {
2155         // The logic here should more or less match to the Cursor::setCurrentFont
2156         // logic, i.e. the cursor height should give a hint what will happen
2157         // if a character is entered.
2158         
2159         // HACK. far from being perfect...
2160
2161         CursorSlice const & sl = innerTextSlice();
2162         Text const & text = *sl.text();
2163         Paragraph const & par = text.getPar(sl.pit());
2164         
2165         // on boundary, so we are really at the character before
2166         pos_type pos = sl.pos();
2167         if (pos > 0 && boundary())
2168                 --pos;
2169         
2170         // on space? Take the font before (only for RTL boundary stay)
2171         if (pos > 0) {
2172                 TextMetrics const & tm = bv().textMetrics(&text);
2173                 if (pos == sl.lastpos()
2174                         || (par.isSeparator(pos) 
2175                         && !tm.isRTLBoundary(sl.pit(), pos)))
2176                         --pos;
2177         }
2178         
2179         // get font at the position
2180         Font font = par.getFont(buffer()->params(), pos,
2181                 text.outerFont(sl.pit()));
2182
2183         return font;
2184 }
2185
2186
2187 bool Cursor::fixIfBroken()
2188 {
2189         bool const broken_cursor = DocIterator::fixIfBroken();
2190         bool const broken_anchor = anchor_.fixIfBroken();
2191         
2192         if (broken_cursor || broken_anchor) {
2193                 clearSelection();
2194                 return true;
2195         }
2196         return false;
2197 }
2198
2199
2200 bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur)
2201 {
2202         // find inset in common
2203         size_type i;
2204         for (i = 0; i < old.depth() && i < cur.depth(); ++i) {
2205                 if (&old[i].inset() != &cur[i].inset())
2206                         break;
2207         }
2208
2209         // update words if we just moved to another paragraph
2210         if (i == old.depth() && i == cur.depth()
2211             && !cur.buffer()->isClean()
2212             && cur.inTexted() && old.inTexted()
2213             && cur.pit() != old.pit()) {
2214                 old.paragraph().updateWords();
2215         }
2216
2217         // notify everything on top of the common part in old cursor,
2218         // but stop if the inset claims the cursor to be invalid now
2219         for (size_type j = i; j < old.depth(); ++j) {
2220                 Cursor inset_pos = old;
2221                 inset_pos.cutOff(j);
2222                 if (old[j].inset().notifyCursorLeaves(inset_pos, cur))
2223                         return true;
2224         }
2225
2226         // notify everything on top of the common part in new cursor,
2227         // but stop if the inset claims the cursor to be invalid now
2228         for (; i < cur.depth(); ++i) {
2229                 if (cur[i].inset().notifyCursorEnters(cur))
2230                         return true;
2231         }
2232         
2233         return false;
2234 }
2235
2236
2237 void Cursor::setCurrentFont()
2238 {
2239         CursorSlice const & cs = innerTextSlice();
2240         Paragraph const & par = cs.paragraph();
2241         pos_type cpit = cs.pit();
2242         pos_type cpos = cs.pos();
2243         Text const & ctext = *cs.text();
2244         TextMetrics const & tm = bv().textMetrics(&ctext);
2245
2246         // are we behind previous char in fact? -> go to that char
2247         if (cpos > 0 && boundary())
2248                 --cpos;
2249
2250         // find position to take the font from
2251         if (cpos != 0) {
2252                 // paragraph end? -> font of last char
2253                 if (cpos == lastpos())
2254                         --cpos;
2255                 // on space? -> look at the words in front of space
2256                 else if (cpos > 0 && par.isSeparator(cpos))     {
2257                         // abc| def -> font of c
2258                         // abc |[WERBEH], i.e. boundary==true -> font of c
2259                         // abc [WERBEH]| def, font of the space
2260                         if (!tm.isRTLBoundary(cpit, cpos))
2261                                 --cpos;
2262                 }
2263         }
2264
2265         // get font
2266         BufferParams const & bufparams = buffer()->params();
2267         current_font = par.getFontSettings(bufparams, cpos);
2268         current_font.setMisspelled(false);
2269         real_current_font = tm.displayFont(cpit, cpos);
2270         real_current_font.setMisspelled(false);
2271
2272         // special case for paragraph end
2273         if (cs.pos() == lastpos()
2274             && tm.isRTLBoundary(cpit, cs.pos())
2275             && !boundary()) {
2276                 Language const * lang = par.getParLanguage(bufparams);
2277                 current_font.setLanguage(lang);
2278                 current_font.fontInfo().setNumber(FONT_OFF);
2279                 real_current_font.setLanguage(lang);
2280                 real_current_font.fontInfo().setNumber(FONT_OFF);
2281         }
2282 }
2283
2284
2285 bool Cursor::textUndo()
2286 {
2287         DocIterator dit = *this;
2288         // Undo::textUndo() will modify dit.
2289         if (!buffer()->undo().textUndo(dit))
2290                 return false;
2291         // Set cursor
2292         setCursor(dit);
2293         clearSelection();
2294         fixIfBroken();
2295         return true;
2296 }
2297
2298
2299 bool Cursor::textRedo()
2300 {
2301         DocIterator dit = *this;
2302         // Undo::textRedo() will modify dit.
2303         if (!buffer()->undo().textRedo(dit))
2304                 return false;
2305         // Set cursor
2306         setCursor(dit);
2307         clearSelection();
2308         fixIfBroken();
2309         return true;
2310 }
2311
2312
2313 void Cursor::finishUndo() const
2314 {
2315         buffer()->undo().finishUndo();
2316 }
2317
2318
2319 void Cursor::beginUndoGroup() const
2320 {
2321         buffer()->undo().beginUndoGroup();
2322 }
2323
2324
2325 void Cursor::endUndoGroup() const
2326 {
2327         buffer()->undo().endUndoGroup();
2328 }
2329
2330
2331 void Cursor::recordUndo(UndoKind kind, pit_type from, pit_type to) const
2332 {
2333         buffer()->undo().recordUndo(*this, kind, from, to);
2334 }
2335
2336
2337 void Cursor::recordUndo(UndoKind kind, pit_type from) const
2338 {
2339         buffer()->undo().recordUndo(*this, kind, from);
2340 }
2341
2342
2343 void Cursor::recordUndo(UndoKind kind) const
2344 {
2345         buffer()->undo().recordUndo(*this, kind);
2346 }
2347
2348
2349 void Cursor::recordUndoInset(UndoKind kind) const
2350 {
2351         buffer()->undo().recordUndoInset(*this, kind);
2352 }
2353
2354
2355 void Cursor::recordUndoFullDocument() const
2356 {
2357         buffer()->undo().recordUndoFullDocument(*this);
2358 }
2359
2360
2361 void Cursor::recordUndoSelection() const
2362 {
2363         if (inMathed()) {
2364                 if (cap::multipleCellsSelected(*this))
2365                         recordUndoInset();
2366                 else
2367                         recordUndo();
2368         } else {
2369                 buffer()->undo().recordUndo(*this, ATOMIC_UNDO,
2370                         selBegin().pit(), selEnd().pit());
2371         }
2372 }
2373
2374
2375 void Cursor::checkBufferStructure()
2376 {
2377         Buffer const * master = buffer()->masterBuffer();
2378         master->tocBackend().updateItem(*this);
2379         if (master != buffer() && !master->hasGuiDelegate())
2380                 // In case the master has no gui associated with it, 
2381                 // the TocItem is not updated (part of bug 5699).
2382                 buffer()->tocBackend().updateItem(*this);
2383 }
2384
2385
2386 } // namespace lyx