]> git.lyx.org Git - lyx.git/blob - src/Cursor.cpp
support for the Iwona fonts
[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         if (selection()) {
1529                 cap::eraseSelection(*this);
1530                 return true;
1531         }
1532
1533         if (pos() == 0) {
1534                 // If empty cell, and not part of a big cell
1535                 if (lastpos() == 0 && inset().nargs() == 1) {
1536                         popBackward();
1537                         // Directly delete empty cell: [|[]] => [|]
1538                         if (inMathed()) {
1539                                 plainErase();
1540                                 resetAnchor();
1541                                 return true;
1542                         }
1543                         // [|], can not delete from inside
1544                         return false;
1545                 } else {
1546                         if (inMathed())
1547                                 pullArg();
1548                         else
1549                                 popBackward();
1550                         return true;
1551                 }
1552         }
1553
1554         if (inMacroMode()) {
1555                 InsetMathUnknown * p = activeMacro();
1556                 if (p->name().size() > 1) {
1557                         p->setName(p->name().substr(0, p->name().size() - 1));
1558                         return true;
1559                 }
1560         }
1561
1562         if (pos() != 0 && prevAtom()->nargs() > 0) {
1563                 // let's require two backspaces for 'big stuff' and
1564                 // highlight on the first
1565                 resetAnchor();
1566                 setSelection(true);
1567                 --pos();
1568         } else {
1569                 --pos();
1570                 plainErase();
1571         }
1572         return true;
1573 }
1574
1575
1576 bool Cursor::erase()
1577 {
1578         if (inMacroMode())
1579                 return true;
1580
1581         if (selection()) {
1582                 cap::eraseSelection(*this);
1583                 return true;
1584         }
1585
1586         // delete empty cells if possible
1587         if (pos() == lastpos() && inset().idxDelete(idx()))
1588                 return true;
1589
1590         // special behaviour when in last position of cell
1591         if (pos() == lastpos()) {
1592                 bool one_cell = inset().nargs() == 1;
1593                 if (one_cell && lastpos() == 0) {
1594                         popBackward();
1595                         // Directly delete empty cell: [|[]] => [|]
1596                         if (inMathed()) {
1597                                 plainErase();
1598                                 resetAnchor();
1599                                 return true;
1600                         }
1601                         // [|], can not delete from inside
1602                         return false;
1603                 }
1604                 // remove markup
1605                 if (!one_cell)
1606                         inset().idxGlue(idx());
1607                 return true;
1608         }
1609
1610         // 'clever' UI hack: only erase large items if previously slected
1611         if (pos() != lastpos() && nextAtom()->nargs() > 0) {
1612                 resetAnchor();
1613                 setSelection(true);
1614                 ++pos();
1615         } else {
1616                 plainErase();
1617         }
1618
1619         return true;
1620 }
1621
1622
1623 bool Cursor::up()
1624 {
1625         macroModeClose();
1626         DocIterator save = *this;
1627         FuncRequest cmd(selection() ? LFUN_UP_SELECT : LFUN_UP, docstring());
1628         this->dispatch(cmd);
1629         if (disp_.dispatched())
1630                 return true;
1631         setCursor(save);
1632         return false;
1633 }
1634
1635
1636 bool Cursor::down()
1637 {
1638         macroModeClose();
1639         DocIterator save = *this;
1640         FuncRequest cmd(selection() ? LFUN_DOWN_SELECT : LFUN_DOWN, docstring());
1641         this->dispatch(cmd);
1642         if (disp_.dispatched())
1643                 return true;
1644         setCursor(save);
1645         return false;
1646 }
1647
1648
1649 bool Cursor::macroModeClose()
1650 {
1651         if (!inMacroMode())
1652                 return false;
1653         InsetMathUnknown * p = activeMacro();
1654         p->finalize();
1655         MathData selection(buffer());
1656         asArray(p->selection(), selection);
1657         docstring const s = p->name();
1658         --pos();
1659         cell().erase(pos());
1660
1661         // do nothing if the macro name is empty
1662         if (s == "\\")
1663                 return false;
1664
1665         // trigger updates of macros, at least, if no full
1666         // updates take place anyway
1667         screenUpdateFlags(Update::Force);
1668
1669         docstring const name = s.substr(1);
1670         InsetMathNest * const in = inset().asInsetMath()->asNestInset();
1671         if (in && in->interpretString(*this, s))
1672                 return true;
1673         MathAtom atom = buffer()->getMacro(name, *this, false) ?
1674                 MathAtom(new MathMacro(buffer(), name)) : createInsetMath(name, buffer());
1675
1676         // try to put argument into macro, if we just inserted a macro
1677         bool macroArg = false;
1678         MathMacro * atomAsMacro = atom.nucleus()->asMacro();
1679         if (atomAsMacro) {
1680                 // macros here are still unfolded (in init mode in fact). So
1681                 // we have to resolve the macro here manually and check its arity
1682                 // to put the selection behind it if arity > 0.
1683                 MacroData const * data = buffer()->getMacro(atomAsMacro->name());
1684                 if (!selection.empty() && data && data->numargs() - data->optionals() > 0) {
1685                         macroArg = true;
1686                         atomAsMacro->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT, 1);
1687                 } else
1688                         // non-greedy case. Do not touch the arguments behind
1689                         atomAsMacro->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT, 0);
1690         }
1691
1692         // insert remembered selection into first argument of a non-macro
1693         else if (atom.nucleus()->nargs() > 0)
1694                 atom.nucleus()->cell(0).append(selection);
1695         
1696         plainInsert(atom);
1697
1698         // finally put the macro argument behind, if needed
1699         if (macroArg) {
1700                 if (selection.size() > 1 || selection[0]->asScriptInset())
1701                         plainInsert(MathAtom(new InsetMathBrace(selection)));
1702                 else
1703                         insert(selection);
1704         }
1705         
1706         return true;
1707 }
1708
1709
1710 docstring Cursor::macroName()
1711 {
1712         return inMacroMode() ? activeMacro()->name() : docstring();
1713 }
1714
1715
1716 void Cursor::handleNest(MathAtom const & a, int c)
1717 {
1718         //lyxerr << "Cursor::handleNest: " << c << endl;
1719         MathAtom t = a;
1720         asArray(cap::grabAndEraseSelection(*this), t.nucleus()->cell(c));
1721         insert(t);
1722         posBackward();
1723         pushBackward(*nextInset());
1724 }
1725
1726
1727 int Cursor::targetX() const
1728 {
1729         if (x_target() != -1)
1730                 return x_target();
1731         int x = 0;
1732         int y = 0;
1733         getPos(x, y);
1734         return x;
1735 }
1736
1737
1738 int Cursor::textTargetOffset() const
1739 {
1740         return textTargetOffset_;
1741 }
1742
1743
1744 void Cursor::setTargetX()
1745 {
1746         int x;
1747         int y;
1748         getPos(x, y);
1749         setTargetX(x);
1750 }
1751
1752
1753 bool Cursor::inMacroMode() const
1754 {
1755         if (!inMathed())
1756                 return false;
1757         if (pos() == 0 || cell().empty())
1758                 return false;
1759         InsetMathUnknown const * p = prevAtom()->asUnknownInset();
1760         return p && !p->final();
1761 }
1762
1763
1764 InsetMathUnknown * Cursor::activeMacro()
1765 {
1766         return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1767 }
1768
1769
1770 InsetMathUnknown const * Cursor::activeMacro() const
1771 {
1772         return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1773 }
1774
1775
1776 void Cursor::pullArg()
1777 {
1778         // FIXME: Look here
1779         MathData ar = cell();
1780         if (popBackward() && inMathed()) {
1781                 plainErase();
1782                 cell().insert(pos(), ar);
1783                 resetAnchor();
1784         } else {
1785                 //formula()->mutateToText();
1786         }
1787 }
1788
1789
1790 void Cursor::touch()
1791 {
1792         // FIXME: look here
1793 #if 0
1794         DocIterator::const_iterator it = begin();
1795         DocIterator::const_iterator et = end();
1796         for ( ; it != et; ++it)
1797                 it->cell().touch();
1798 #endif
1799 }
1800
1801
1802 void Cursor::normalize()
1803 {
1804         if (idx() > lastidx()) {
1805                 lyxerr << "this should not really happen - 1: "
1806                        << idx() << ' ' << nargs()
1807                        << " in: " << &inset() << endl;
1808                 idx() = lastidx();
1809         }
1810
1811         if (pos() > lastpos()) {
1812                 lyxerr << "this should not really happen - 2: "
1813                         << pos() << ' ' << lastpos() <<  " in idx: " << idx()
1814                        << " in atom: '";
1815                 odocstringstream os;
1816                 WriteStream wi(os, false, true, WriteStream::wsDefault);
1817                 inset().asInsetMath()->write(wi);
1818                 lyxerr << to_utf8(os.str()) << endl;
1819                 pos() = lastpos();
1820         }
1821 }
1822
1823
1824 bool Cursor::upDownInMath(bool up)
1825 {
1826         // Be warned: The 'logic' implemented in this function is highly
1827         // fragile. A distance of one pixel or a '<' vs '<=' _really
1828         // matters. So fiddle around with it only if you think you know
1829         // what you are doing!
1830         int xo = 0;
1831         int yo = 0;
1832         getPos(xo, yo);
1833         xo = beforeDispatchPosX_;
1834
1835         // check if we had something else in mind, if not, this is the future
1836         // target
1837         if (x_target_ == -1)
1838                 setTargetX(xo);
1839         else if (inset().asInsetText() && xo - textTargetOffset() != x_target()) {
1840                 // In text mode inside the line (not left or right) possibly set a new target_x,
1841                 // but only if we are somewhere else than the previous target-offset.
1842                 
1843                 // We want to keep the x-target on subsequent up/down movements
1844                 // that cross beyond the end of short lines. Thus a special
1845                 // handling when the cursor is at the end of line: Use the new
1846                 // x-target only if the old one was before the end of line
1847                 // or the old one was after the beginning of the line
1848                 bool inRTL = isWithinRtlParagraph(*this);
1849                 bool left;
1850                 bool right;
1851                 if (inRTL) {
1852                         left = pos() == textRow().endpos();
1853                         right = pos() == textRow().pos();
1854                 } else {
1855                         left = pos() == textRow().pos();
1856                         right = pos() == textRow().endpos();
1857                 }
1858                 if ((!left && !right) ||
1859                                 (left && !right && xo < x_target_) ||
1860                                 (!left && right && x_target_ < xo))
1861                         setTargetX(xo);
1862                 else
1863                         xo = targetX();
1864         } else
1865                 xo = targetX();
1866
1867         // try neigbouring script insets
1868         Cursor old = *this;
1869         if (inMathed() && !selection()) {
1870                 // try left
1871                 if (pos() != 0) {
1872                         InsetMathScript const * p = prevAtom()->asScriptInset();
1873                         if (p && p->has(up)) {
1874                                 --pos();
1875                                 push(*const_cast<InsetMathScript*>(p));
1876                                 idx() = p->idxOfScript(up);
1877                                 pos() = lastpos();
1878                                 
1879                                 // we went in the right direction? Otherwise don't jump into the script
1880                                 int x;
1881                                 int y;
1882                                 getPos(x, y);
1883                                 int oy = beforeDispatchPosY_;
1884                                 if ((!up && y <= oy) ||
1885                                                 (up && y >= oy))
1886                                         operator=(old);
1887                                 else
1888                                         return true;
1889                         }
1890                 }
1891                 
1892                 // try right
1893                 if (pos() != lastpos()) {
1894                         InsetMathScript const * p = nextAtom()->asScriptInset();
1895                         if (p && p->has(up)) {
1896                                 push(*const_cast<InsetMathScript*>(p));
1897                                 idx() = p->idxOfScript(up);
1898                                 pos() = 0;
1899                                 
1900                                 // we went in the right direction? Otherwise don't jump into the script
1901                                 int x;
1902                                 int y;
1903                                 getPos(x, y);
1904                                 int oy = beforeDispatchPosY_;
1905                                 if ((!up && y <= oy) ||
1906                                                 (up && y >= oy))
1907                                         operator=(old);
1908                                 else
1909                                         return true;
1910                         }
1911                 }
1912         }
1913                 
1914         // try to find an inset that knows better then we,
1915         if (inset().idxUpDown(*this, up)) {
1916                 //lyxerr << "idxUpDown triggered" << endl;
1917                 // try to find best position within this inset
1918                 if (!selection())
1919                         setCursor(bruteFind2(*this, xo, yo));
1920                 return true;
1921         }
1922         
1923         // any improvement going just out of inset?
1924         if (popBackward() && inMathed()) {
1925                 //lyxerr << "updown: popBackward succeeded" << endl;
1926                 int xnew;
1927                 int ynew;
1928                 int yold = beforeDispatchPosY_;
1929                 getPos(xnew, ynew);
1930                 if (up ? ynew < yold : ynew > yold)
1931                         return true;
1932         }
1933         
1934         // no success, we are probably at the document top or bottom
1935         operator=(old);
1936         return false;
1937 }
1938
1939
1940 bool Cursor::atFirstOrLastRow(bool up)
1941 {
1942         TextMetrics const & tm = bv_->textMetrics(text());
1943         ParagraphMetrics const & pm = tm.parMetrics(pit());
1944         
1945         int row;
1946         if (pos() && boundary())
1947                 row = pm.pos2row(pos() - 1);
1948         else
1949                 row = pm.pos2row(pos());
1950         
1951         if (up) {
1952                 if (pit() == 0 && row == 0)
1953                         return true;
1954         } else {
1955                 if (pit() + 1 >= int(text()->paragraphs().size()) &&
1956                                 row + 1 >= int(pm.rows().size()))
1957                         return true;
1958         }
1959         return false;
1960 }
1961
1962
1963 bool Cursor::upDownInText(bool up, bool & updateNeeded)
1964 {
1965         LASSERT(text(), /**/);
1966
1967         // where are we?
1968         int xo = 0;
1969         int yo = 0;
1970         getPos(xo, yo);
1971         xo = beforeDispatchPosX_;
1972
1973         // update the targetX - this is here before the "return false"
1974         // to set a new target which can be used by InsetTexts above
1975         // if we cannot move up/down inside this inset anymore
1976         if (x_target_ == -1)
1977                 setTargetX(xo);
1978         else if (xo - textTargetOffset() != x_target() &&
1979                                          depth() == beforeDispatchCursor_.depth()) {
1980                 // In text mode inside the line (not left or right)
1981                 // possibly set a new target_x, but only if we are
1982                 // somewhere else than the previous target-offset.
1983                 
1984                 // We want to keep the x-target on subsequent up/down
1985                 // movements that cross beyond the end of short lines.
1986                 // Thus a special handling when the cursor is at the
1987                 // end of line: Use the new x-target only if the old
1988                 // one was before the end of line or the old one was
1989                 // after the beginning of the line
1990                 bool inRTL = isWithinRtlParagraph(*this);
1991                 bool left;
1992                 bool right;
1993                 if (inRTL) {
1994                         left = pos() == textRow().endpos();
1995                         right = pos() == textRow().pos();
1996                 } else {
1997                         left = pos() == textRow().pos();
1998                         right = pos() == textRow().endpos();
1999                 }
2000                 if ((!left && !right) ||
2001                                 (left && !right && xo < x_target_) ||
2002                                 (!left && right && x_target_ < xo))
2003                         setTargetX(xo);
2004                 else
2005                         xo = targetX();
2006         } else
2007                 xo = targetX();
2008                 
2009         // first get the current line
2010         TextMetrics & tm = bv_->textMetrics(text());
2011         ParagraphMetrics const & pm = tm.parMetrics(pit());
2012         int row;
2013         if (pos() && boundary())
2014                 row = pm.pos2row(pos() - 1);
2015         else
2016                 row = pm.pos2row(pos());
2017                 
2018         if (atFirstOrLastRow(up)) {
2019                 // Is there a place for the cursor to go ? If yes, we
2020                 // can execute the DEPM, otherwise we should keep the
2021                 // paragraph to host the cursor.
2022                 Cursor dummy = *this;
2023                 bool valid_destination = false;
2024                 for(; dummy.depth(); dummy.pop())
2025                         if (!dummy.atFirstOrLastRow(up)) {
2026                                 valid_destination = true;
2027                                 break;
2028                         }
2029
2030                 // will a next dispatch follow and if there is a new 
2031                 // dispatch will it move the cursor out ?
2032                 if (depth() > 1 && valid_destination) {
2033                         // The cursor hasn't changed yet. This happens when
2034                         // you e.g. move out of an inset. And to give the 
2035                         // DEPM the possibility of doing something we must
2036                         // provide it with two different cursors. (Lgb, vfr)
2037                         dummy = *this;
2038                         dummy.pos() = dummy.pos() == 0 ? dummy.lastpos() : 0;
2039                         dummy.pit() = dummy.pit() == 0 ? dummy.lastpit() : 0;
2040
2041                         updateNeeded |= bv().checkDepm(dummy, *this);
2042                         updateTextTargetOffset();
2043                         if (updateNeeded) {
2044                                 forceBufferUpdate();
2045                                 // DEPM may have requested a screen update
2046                                 this->screenUpdateFlags(
2047                                         this->screenUpdate() | dummy.screenUpdate());
2048                         }
2049                 }
2050                 return false;
2051         }
2052
2053         // with and without selection are handled differently
2054         if (!selection()) {
2055                 int yo = bv().getPos(*this).y_;
2056                 Cursor old = *this;
2057                 // To next/previous row
2058                 if (up)
2059                         tm.editXY(*this, xo, yo - textRow().ascent() - 1);
2060                 else
2061                         tm.editXY(*this, xo, yo + textRow().descent() + 1);
2062                 clearSelection();
2063                 
2064                 // This happens when you move out of an inset.  
2065                 // And to give the DEPM the possibility of doing  
2066                 // something we must provide it with two different  
2067                 // cursors. (Lgb)  
2068                 Cursor dummy = *this;
2069                 if (dummy == old)
2070                         ++dummy.pos();
2071                 if (bv().checkDepm(dummy, old)) {
2072                         updateNeeded = true;
2073                         // Make sure that cur gets back whatever happened to dummy (Lgb)
2074                         // This will include any screen update requested by DEPM
2075                         operator=(dummy);
2076                 }
2077         } else {
2078                 // if there is a selection, we stay out of any inset,
2079                 // and just jump to the right position:
2080                 Cursor old = *this;
2081                 int next_row = row;
2082                 if (up) {
2083                         if (row > 0) {
2084                                 --next_row;
2085                         } else if (pit() > 0) {
2086                                 --pit();
2087                                 TextMetrics & tm = bv_->textMetrics(text());
2088                                 if (!tm.contains(pit()))
2089                                         tm.newParMetricsUp();
2090                                 ParagraphMetrics const & pmcur = tm.parMetrics(pit());
2091                                 next_row = pmcur.rows().size() - 1;
2092                         }
2093                 } else {
2094                         if (row + 1 < int(pm.rows().size())) {
2095                                 ++next_row;
2096                         } else if (pit() + 1 < int(text()->paragraphs().size())) {
2097                                 ++pit();
2098                                 TextMetrics & tm = bv_->textMetrics(text());
2099                                 if (!tm.contains(pit()))
2100                                         tm.newParMetricsDown();
2101                                 next_row = 0;
2102                         }
2103                 }
2104                 top().pos() = min(tm.x2pos(pit(), next_row, xo), top().lastpos());
2105
2106                 int const xpos = tm.x2pos(pit(), next_row, xo);
2107                 bool const at_end_row = xpos == tm.x2pos(pit(), next_row, tm.width());
2108                 bool const at_beg_row = xpos == tm.x2pos(pit(), next_row, 0);
2109
2110                 if (at_end_row && at_beg_row)
2111                         // make sure the cursor ends up on this row
2112                         boundary(false);
2113                 else
2114                         boundary(at_end_row);
2115
2116                 updateNeeded |= bv().checkDepm(*this, old);
2117         }
2118
2119         if (updateNeeded)
2120                 forceBufferUpdate();
2121         updateTextTargetOffset();
2122         return true;
2123 }       
2124
2125
2126 void Cursor::handleFont(string const & font)
2127 {
2128         LYXERR(Debug::DEBUG, font);
2129         docstring safe;
2130         if (selection()) {
2131                 macroModeClose();
2132                 safe = cap::grabAndEraseSelection(*this);
2133         }
2134
2135         recordUndoInset();
2136
2137         if (lastpos() != 0) {
2138                 // something left in the cell
2139                 if (pos() == 0) {
2140                         // cursor in first position
2141                         popBackward();
2142                 } else if (pos() == lastpos()) {
2143                         // cursor in last position
2144                         popForward();
2145                 } else {
2146                         // cursor in between. split cell
2147                         MathData::iterator bt = cell().begin();
2148                         MathAtom at = createInsetMath(from_utf8(font), buffer());
2149                         at.nucleus()->cell(0) = MathData(buffer(), bt, bt + pos());
2150                         cell().erase(bt, bt + pos());
2151                         popBackward();
2152                         plainInsert(at);
2153                 }
2154         } else {
2155                 // nothing left in the cell
2156                 popBackward();
2157                 plainErase();
2158                 resetAnchor();
2159         }
2160         insert(safe);
2161 }
2162
2163
2164 void Cursor::message(docstring const & msg) const
2165 {
2166         disp_.setMessage(msg);
2167 }
2168
2169
2170 void Cursor::errorMessage(docstring const & msg) const
2171 {
2172         disp_.setMessage(msg);
2173         disp_.setError(true);
2174 }
2175
2176
2177 namespace {
2178
2179 docstring parbreak(Cursor const * cur)
2180 {
2181         odocstringstream os;
2182         os << '\n';
2183         // only add blank line if we're not in a ParbreakIsNewline situation
2184         if (!cur->inset().getLayout().parbreakIsNewline() 
2185             && !cur->paragraph().layout().parbreak_is_newline)
2186                 os << '\n';
2187         return os.str();
2188 }
2189
2190 }
2191
2192
2193 docstring Cursor::selectionAsString(bool with_label) const
2194 {
2195         if (!selection())
2196                 return docstring();
2197
2198         if (inMathed())
2199                 return cap::grabSelection(*this);
2200
2201         int const label = with_label
2202                 ? AS_STR_LABEL | AS_STR_INSETS : AS_STR_INSETS;
2203
2204         idx_type const startidx = selBegin().idx();
2205         idx_type const endidx = selEnd().idx();
2206         if (startidx != endidx) {
2207                 // multicell selection
2208                 InsetTabular * table = inset().asInsetTabular();
2209                 LASSERT(table, return docstring());
2210                 return table->asString(startidx, endidx);
2211         }
2212
2213         ParagraphList const & pars = text()->paragraphs();
2214
2215         pit_type const startpit = selBegin().pit();
2216         pit_type const endpit = selEnd().pit();
2217         size_t const startpos = selBegin().pos();
2218         size_t const endpos = selEnd().pos();
2219
2220         if (startpit == endpit)
2221                 return pars[startpit].asString(startpos, endpos, label);
2222
2223         // First paragraph in selection
2224         docstring result = pars[startpit].
2225                 asString(startpos, pars[startpit].size(), label)
2226                 + parbreak(this);
2227
2228         // The paragraphs in between (if any)
2229         for (pit_type pit = startpit + 1; pit != endpit; ++pit) {
2230                 Paragraph const & par = pars[pit];
2231                 result += par.asString(0, par.size(), label)
2232                         + parbreak(this);
2233         }
2234
2235         // Last paragraph in selection
2236         result += pars[endpit].asString(0, endpos, label);
2237
2238         return result;
2239 }
2240
2241
2242 docstring Cursor::currentState() const
2243 {
2244         if (inMathed()) {
2245                 odocstringstream os;
2246                 info(os);
2247                 return os.str();
2248         }
2249
2250         if (inTexted())
2251                 return text()->currentState(*this);
2252
2253         return docstring();
2254 }
2255
2256
2257 docstring Cursor::getPossibleLabel() const
2258 {
2259         return inMathed() ? from_ascii("eq:") : text()->getPossibleLabel(*this);
2260 }
2261
2262
2263 Encoding const * Cursor::getEncoding() const
2264 {
2265         if (empty())
2266                 return 0;
2267         CursorSlice const & sl = innerTextSlice();
2268         Text const & text = *sl.text();
2269         Font font = text.getPar(sl.pit()).getFont(
2270                 bv().buffer().params(), sl.pos(), text.outerFont(sl.pit()));
2271         return font.language()->encoding();
2272 }
2273
2274
2275 void Cursor::undispatched() const
2276 {
2277         disp_.dispatched(false);
2278 }
2279
2280
2281 void Cursor::dispatched() const
2282 {
2283         disp_.dispatched(true);
2284 }
2285
2286
2287 void Cursor::screenUpdateFlags(Update::flags f) const
2288 {
2289         disp_.screenUpdate(f);
2290 }
2291
2292
2293 void Cursor::forceBufferUpdate() const
2294 {
2295         disp_.forceBufferUpdate();
2296 }
2297
2298
2299 void Cursor::clearBufferUpdate() const
2300 {
2301         disp_.clearBufferUpdate();
2302 }
2303
2304
2305 bool Cursor::needBufferUpdate() const
2306 {
2307         return disp_.needBufferUpdate();
2308 }
2309
2310
2311 void Cursor::noScreenUpdate() const
2312 {
2313         disp_.screenUpdate(Update::None);
2314 }
2315
2316
2317 Font Cursor::getFont() const
2318 {
2319         // The logic here should more or less match to the
2320         // Cursor::setCurrentFont logic, i.e. the cursor height should
2321         // give a hint what will happen if a character is entered.
2322         
2323         // HACK. far from being perfect...
2324
2325         CursorSlice const & sl = innerTextSlice();
2326         Text const & text = *sl.text();
2327         Paragraph const & par = text.getPar(sl.pit());
2328         
2329         // on boundary, so we are really at the character before
2330         pos_type pos = sl.pos();
2331         if (pos > 0 && boundary())
2332                 --pos;
2333         
2334         // on space? Take the font before (only for RTL boundary stay)
2335         if (pos > 0) {
2336                 TextMetrics const & tm = bv().textMetrics(&text);
2337                 if (pos == sl.lastpos()
2338                         || (par.isSeparator(pos) 
2339                         && !tm.isRTLBoundary(sl.pit(), pos)))
2340                         --pos;
2341         }
2342         
2343         // get font at the position
2344         Font font = par.getFont(buffer()->params(), pos,
2345                 text.outerFont(sl.pit()));
2346
2347         return font;
2348 }
2349
2350
2351 bool Cursor::fixIfBroken()
2352 {
2353         bool const broken_cursor = DocIterator::fixIfBroken();
2354         bool const broken_anchor = anchor_.fixIfBroken();
2355         
2356         if (broken_cursor || broken_anchor) {
2357                 clearNewWordPosition();
2358                 clearSelection();
2359                 return true;
2360         }
2361         return false;
2362 }
2363
2364
2365 void Cursor::sanitize()
2366 {
2367         setBuffer(&bv_->buffer());
2368         DocIterator::sanitize();
2369         anchor_.sanitize();
2370 }
2371
2372
2373 bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur)
2374 {
2375         // find inset in common
2376         size_type i;
2377         for (i = 0; i < old.depth() && i < cur.depth(); ++i) {
2378                 if (&old[i].inset() != &cur[i].inset())
2379                         break;
2380         }
2381
2382         // update words if we just moved to another paragraph
2383         if (i == old.depth() && i == cur.depth()
2384             && !cur.buffer()->isClean()
2385             && cur.inTexted() && old.inTexted()
2386             && cur.pit() != old.pit()) {
2387                 old.paragraph().updateWords();
2388         }
2389
2390         // notify everything on top of the common part in old cursor,
2391         // but stop if the inset claims the cursor to be invalid now
2392         for (size_type j = i; j < old.depth(); ++j) {
2393                 Cursor inset_pos = old;
2394                 inset_pos.cutOff(j);
2395                 if (old[j].inset().notifyCursorLeaves(inset_pos, cur))
2396                         return true;
2397         }
2398
2399         // notify everything on top of the common part in new cursor,
2400         // but stop if the inset claims the cursor to be invalid now
2401         for (; i < cur.depth(); ++i) {
2402                 if (cur[i].inset().notifyCursorEnters(cur))
2403                         return true;
2404         }
2405         
2406         return false;
2407 }
2408
2409
2410 void Cursor::setCurrentFont()
2411 {
2412         CursorSlice const & cs = innerTextSlice();
2413         Paragraph const & par = cs.paragraph();
2414         pos_type cpit = cs.pit();
2415         pos_type cpos = cs.pos();
2416         Text const & ctext = *cs.text();
2417         TextMetrics const & tm = bv().textMetrics(&ctext);
2418
2419         // are we behind previous char in fact? -> go to that char
2420         if (cpos > 0 && boundary())
2421                 --cpos;
2422
2423         // find position to take the font from
2424         if (cpos != 0) {
2425                 // paragraph end? -> font of last char
2426                 if (cpos == lastpos())
2427                         --cpos;
2428                 // on space? -> look at the words in front of space
2429                 else if (cpos > 0 && par.isSeparator(cpos))     {
2430                         // abc| def -> font of c
2431                         // abc |[WERBEH], i.e. boundary==true -> font of c
2432                         // abc [WERBEH]| def, font of the space
2433                         if (!tm.isRTLBoundary(cpit, cpos))
2434                                 --cpos;
2435                 }
2436         }
2437
2438         // get font
2439         BufferParams const & bufparams = buffer()->params();
2440         current_font = par.getFontSettings(bufparams, cpos);
2441         real_current_font = tm.displayFont(cpit, cpos);
2442
2443         // special case for paragraph end
2444         if (cs.pos() == lastpos()
2445             && tm.isRTLBoundary(cpit, cs.pos())
2446             && !boundary()) {
2447                 Language const * lang = par.getParLanguage(bufparams);
2448                 current_font.setLanguage(lang);
2449                 current_font.fontInfo().setNumber(FONT_OFF);
2450                 real_current_font.setLanguage(lang);
2451                 real_current_font.fontInfo().setNumber(FONT_OFF);
2452         }
2453 }
2454
2455
2456 bool Cursor::textUndo()
2457 {
2458         if (!buffer()->undo().textUndo(*this))
2459                 return false;
2460         sanitize();
2461         return true;
2462 }
2463
2464
2465 bool Cursor::textRedo()
2466 {
2467         if (!buffer()->undo().textRedo(*this))
2468                 return false;
2469         sanitize();
2470         return true;
2471 }
2472
2473
2474 void Cursor::finishUndo() const
2475 {
2476         buffer()->undo().finishUndo();
2477 }
2478
2479
2480 void Cursor::beginUndoGroup() const
2481 {
2482         buffer()->undo().beginUndoGroup();
2483 }
2484
2485
2486 void Cursor::endUndoGroup() const
2487 {
2488         buffer()->undo().endUndoGroup(*this);
2489 }
2490
2491
2492 void Cursor::recordUndo(UndoKind kind, pit_type from, pit_type to) const
2493 {
2494         buffer()->undo().recordUndo(*this, kind, from, to);
2495 }
2496
2497
2498 void Cursor::recordUndo(UndoKind kind, pit_type from) const
2499 {
2500         buffer()->undo().recordUndo(*this, kind, from);
2501 }
2502
2503
2504 void Cursor::recordUndo(UndoKind kind) const
2505 {
2506         buffer()->undo().recordUndo(*this, kind);
2507 }
2508
2509
2510 void Cursor::recordUndoInset(UndoKind kind, Inset const * inset) const
2511 {
2512         buffer()->undo().recordUndoInset(*this, kind, inset);
2513 }
2514
2515
2516 void Cursor::recordUndoFullDocument() const
2517 {
2518         buffer()->undo().recordUndoFullDocument(*this);
2519 }
2520
2521
2522 void Cursor::recordUndoSelection() const
2523 {
2524         if (inMathed()) {
2525                 if (cap::multipleCellsSelected(*this))
2526                         recordUndoInset();
2527                 else
2528                         recordUndo();
2529         } else {
2530                 buffer()->undo().recordUndo(*this, ATOMIC_UNDO,
2531                         selBegin().pit(), selEnd().pit());
2532         }
2533 }
2534
2535
2536 void Cursor::checkBufferStructure()
2537 {
2538         Buffer const * master = buffer()->masterBuffer();
2539         master->tocBackend().updateItem(*this);
2540         if (master != buffer() && !master->hasGuiDelegate())
2541                 // In case the master has no gui associated with it, 
2542                 // the TocItem is not updated (part of bug 5699).
2543                 buffer()->tocBackend().updateItem(*this);
2544 }
2545
2546
2547 } // namespace lyx