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