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