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