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