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