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