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