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