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