]> git.lyx.org Git - features.git/blob - src/Cursor.cpp
Painter: Add a line style to disable antialiasing
[features.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 "Text.h"
35 #include "TextMetrics.h"
36 #include "TocBackend.h"
37
38 #include "support/debug.h"
39 #include "support/docstream.h"
40 #include "support/ExceptionMessage.h"
41 #include "support/gettext.h"
42 #include "support/lassert.h"
43
44 #include "insets/InsetTabular.h"
45 #include "insets/InsetText.h"
46
47 #include "mathed/InsetMath.h"
48 #include "mathed/InsetMathBrace.h"
49 #include "mathed/InsetMathScript.h"
50 #include "mathed/MacroTable.h"
51 #include "mathed/MathData.h"
52 #include "mathed/MathMacro.h"
53
54 #include <sstream>
55 #include <limits>
56 #include <map>
57 #include <algorithm>
58
59 using namespace std;
60
61 namespace lyx {
62
63 namespace {
64
65 // Find position closest to (x, y) in cell given by iter.
66 // Used only in mathed
67 DocIterator bruteFind(Cursor const & c, int x, int y)
68 {
69         double best_dist = numeric_limits<double>::max();
70
71         DocIterator result;
72
73         DocIterator it = c;
74         it.pos() = 0;
75         DocIterator et = c;
76         et.pos() = et.lastpos();
77         for (size_t i = 0;; ++i) {
78                 int xo;
79                 int yo;
80                 Inset const * inset = &it.inset();
81                 CoordCache::Insets const & insetCache = c.bv().coordCache().getInsets();
82
83                 // FIXME: in the case where the inset is not in the cache, this
84                 // means that no part of it is visible on screen. In this case
85                 // we don't do elaborate search and we just return the forwarded
86                 // DocIterator at its beginning.
87                 if (!insetCache.has(inset)) {
88                         it.top().pos() = 0;
89                         return it;
90                 }
91
92                 Point const o = insetCache.xy(inset);
93                 inset->cursorPos(c.bv(), it.top(), c.boundary(), xo, yo);
94                 // Convert to absolute
95                 xo += o.x_;
96                 yo += o.y_;
97                 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
98                 // '<=' in order to take the last possible position
99                 // this is important for clicking behind \sum in e.g. '\sum_i a'
100                 LYXERR(Debug::DEBUG, "i: " << i << " d: " << d
101                         << " best: " << best_dist);
102                 if (d <= best_dist) {
103                         best_dist = d;
104                         result = it;
105                 }
106                 if (it == et)
107                         break;
108                 it.forwardPos();
109         }
110         return result;
111 }
112
113
114 } // namespace anon
115
116
117 CursorData::CursorData()
118         : DocIterator(), anchor_(),
119           selection_(false), mark_(false), word_selection_(false),
120           current_font(inherit_font),
121           autocorrect_(false), macromode_(false)
122 {}
123
124
125 CursorData::CursorData(Buffer * buffer)
126         : DocIterator(buffer), anchor_(),
127           selection_(false), mark_(false), word_selection_(false),
128           current_font(inherit_font),
129           autocorrect_(false), macromode_(false)
130 {}
131
132
133 CursorData::CursorData(DocIterator const & dit)
134         : DocIterator(dit), anchor_(),
135           selection_(false), mark_(false), word_selection_(false),
136           current_font(inherit_font),
137           autocorrect_(false), macromode_(false)
138 {}
139
140
141
142
143 ostream & operator<<(ostream & os, CursorData const & cur)
144 {
145         os << "\n cursor:                                | anchor:\n";
146         for (size_t i = 0, n = cur.depth(); i != n; ++i) {
147                 os << " " << cur[i] << " | ";
148                 if (i < cur.anchor_.depth())
149                         os << cur.anchor_[i];
150                 else
151                         os << "-------------------------------";
152                 os << "\n";
153         }
154         for (size_t i = cur.depth(), n = cur.anchor_.depth(); i < n; ++i) {
155                 os << "------------------------------- | " << cur.anchor_[i] << "\n";
156         }
157         os << " selection: " << cur.selection_
158 //         << " x_target: " << cur.x_target_
159            << " boundary: " << cur.boundary() << endl;
160         return os;
161 }
162
163
164 LyXErr & operator<<(LyXErr & os, CursorData const & cur)
165 {
166         os.stream() << cur;
167         return os;
168 }
169
170
171 // be careful: this is called from the bv's constructor, too, so
172 // bv functions are not yet available!
173 Cursor::Cursor(BufferView & bv)
174         : CursorData(&bv.buffer()), bv_(&bv),
175           x_target_(-1), textTargetOffset_(0),
176           beforeDispatchPosX_(0), beforeDispatchPosY_(0)
177 {}
178
179
180 void Cursor::reset()
181 {
182         clear();
183         push_back(CursorSlice(buffer()->inset()));
184         anchor_ = doc_iterator_begin(buffer());
185         anchor_.clear();
186         new_word_ = doc_iterator_begin(buffer());
187         new_word_.clear();
188         clearTargetX();
189         selection_ = false;
190         mark_ = false;
191 }
192
193
194 // this (intentionally) does neither touch anchor nor selection status
195 void Cursor::setCursor(DocIterator const & cur)
196 {
197         DocIterator::operator=(cur);
198 }
199
200
201 void Cursor::setCursorToAnchor()
202 {
203         if (selection()) {
204                 DocIterator normal = anchor_;
205                 while (depth() < normal.depth())
206                         normal.pop_back();
207                 if (depth() < anchor_.depth() && top() <= anchor_[depth() - 1])
208                         ++normal.pos();
209                 setCursor(normal);
210         }
211 }
212
213
214 void Cursor::setCursorData(CursorData const & data)
215 {
216         CursorData::operator=(data);
217 }
218
219
220 bool Cursor::getStatus(FuncRequest const & cmd, FuncStatus & status) const
221 {
222         Cursor cur = *this;
223
224         // Try to fix cursor in case it is broken.
225         cur.fixIfBroken();
226
227         // Is this a function that acts on inset at point?
228         Inset * inset = cur.nextInset();
229         if (lyxaction.funcHasFlag(cmd.action(), LyXAction::AtPoint)
230             && inset && inset->getStatus(cur, cmd, status))
231                 return true;
232
233         // This is, of course, a mess. Better create a new doc iterator and use
234         // this in Inset::getStatus. This might require an additional
235         // BufferView * arg, though (which should be avoided)
236         //Cursor safe = *this;
237         bool res = false;
238         for ( ; cur.depth(); cur.pop()) {
239                 //lyxerr << "\nCursor::getStatus: cmd: " << cmd << endl << *this << endl;
240                 // LASSERT: Is it safe to continue here, or should we return?
241                 LASSERT(cur.idx() <= cur.lastidx(), /**/);
242                 LASSERT(cur.pit() <= cur.lastpit(), /**/);
243                 LASSERT(cur.pos() <= cur.lastpos(), /**/);
244
245                 // The inset's getStatus() will return 'true' if it made
246                 // a definitive decision on whether it want to handle the
247                 // request or not. The result of this decision is put into
248                 // the 'status' parameter.
249                 if (cur.inset().getStatus(cur, cmd, status)) {
250                         res = true;
251                         break;
252                 }
253         }
254         return res;
255 }
256
257
258 void Cursor::saveBeforeDispatchPosXY()
259 {
260         getPos(beforeDispatchPosX_, beforeDispatchPosY_);
261 }
262
263
264 void Cursor::dispatch(FuncRequest const & cmd0)
265 {
266         LYXERR(Debug::ACTION, "Cursor::dispatch: cmd: " << cmd0 << '\n' << *this);
267         if (empty())
268                 return;
269
270         fixIfBroken();
271         FuncRequest cmd = cmd0;
272         Cursor safe = *this;
273         Cursor old = *this;
274         disp_ = DispatchResult();
275
276         beginUndoGroup();
277
278         // Is this a function that acts on inset at point?
279         if (lyxaction.funcHasFlag(cmd.action(), LyXAction::AtPoint)
280             && nextInset()) {
281                 disp_.dispatched(true);
282                 disp_.screenUpdate(Update::FitCursor | Update::Force);
283                 FuncRequest tmpcmd = cmd;
284                 LYXERR(Debug::DEBUG, "Cursor::dispatch: (AtPoint) cmd: "
285                         << cmd0 << endl << *this);
286                 nextInset()->dispatch(*this, tmpcmd);
287                 if (disp_.dispatched()) {
288                         endUndoGroup();
289                         return;
290                 }
291         }
292
293         // store some values to be used inside of the handlers
294         beforeDispatchCursor_ = *this;
295         for (; depth(); pop(), boundary(false)) {
296                 LYXERR(Debug::DEBUG, "Cursor::dispatch: cmd: "
297                         << cmd0 << endl << *this);
298
299                 // In any of these cases, the cursor is invalid, and we should
300                 // try to save this document rather than crash.
301                 LBUFERR(pos() <= lastpos());
302                 LBUFERR(idx() <= lastidx());
303                 LBUFERR(pit() <= lastpit());
304
305                 // The common case is 'LFUN handled, need update', so make the
306                 // LFUN handler's life easier by assuming this as default value.
307                 // The handler can reset the update and val flags if necessary.
308                 disp_.screenUpdate(Update::FitCursor | Update::Force);
309                 disp_.dispatched(true);
310                 inset().dispatch(*this, cmd);
311                 if (disp_.dispatched())
312                         break;
313         }
314
315         // it completely to get a 'bomb early' behaviour in case this
316         // object will be used again.
317         if (!disp_.dispatched()) {
318                 LYXERR(Debug::DEBUG, "RESTORING OLD CURSOR!");
319                 // We might have invalidated the cursor when removing an empty
320                 // paragraph while the cursor could not be moved out the inset
321                 // while we initially thought we could. This might happen when
322                 // a multiline inset becomes an inline inset when the second
323                 // paragraph is removed.
324                 if (safe.pit() > safe.lastpit()) {
325                         safe.pit() = safe.lastpit();
326                         safe.pos() = safe.lastpos();
327                 }
328                 operator=(safe);
329                 disp_.screenUpdate(Update::None);
330                 disp_.dispatched(false);
331         } else {
332                 // restore the previous one because nested Cursor::dispatch calls
333                 // are possible which would change it
334                 beforeDispatchCursor_ = safe.beforeDispatchCursor_;
335         }
336         endUndoGroup();
337
338         // NOTE: The code below has been copied to BufferView::dispatch.
339         // If you need to modify this, please update the other one too.
340
341         // notify insets we just left
342         if (*this != old) {
343                 old.beginUndoGroup();
344                 old.fixIfBroken();
345                 bool badcursor = notifyCursorLeavesOrEnters(old, *this);
346                 if (badcursor) {
347                         fixIfBroken();
348                         bv().resetInlineCompletionPos();
349                 }
350                 old.endUndoGroup();
351         }
352 }
353
354
355 DispatchResult const & Cursor::result() const
356 {
357         return disp_;
358 }
359
360
361 BufferView & Cursor::bv() const
362 {
363         LBUFERR(bv_);
364         return *bv_;
365 }
366
367
368 void Cursor::pop()
369 {
370         LBUFERR(depth() >= 1);
371         pop_back();
372 }
373
374
375 void Cursor::push(Inset & p)
376 {
377         push_back(CursorSlice(p));
378         p.setBuffer(*buffer());
379 }
380
381
382 void Cursor::pushBackward(Inset & p)
383 {
384         LASSERT(!empty(), return);
385         //lyxerr << "Entering inset " << t << " front" << endl;
386         push(p);
387         p.idxFirst(*this);
388 }
389
390
391 bool Cursor::popBackward()
392 {
393         LASSERT(!empty(), return false);
394         if (depth() == 1)
395                 return false;
396         pop();
397         return true;
398 }
399
400
401 bool Cursor::popForward()
402 {
403         LASSERT(!empty(), return false);
404         //lyxerr << "Leaving inset from in back" << endl;
405         const pos_type lp = (depth() > 1) ? (*this)[depth() - 2].lastpos() : 0;
406         if (depth() == 1)
407                 return false;
408         pop();
409         pos() += lastpos() - lp + 1;
410         return true;
411 }
412
413
414 int Cursor::currentMode()
415 {
416         LASSERT(!empty(), return Inset::UNDECIDED_MODE);
417         for (int i = depth() - 1; i >= 0; --i) {
418                 int res = operator[](i).inset().currentMode();
419                 bool locked_mode = operator[](i).inset().lockedMode();
420                 // Also return UNDECIDED_MODE when the mode is locked,
421                 // as in this case it is treated the same as TEXT_MODE
422                 if (res != Inset::UNDECIDED_MODE || locked_mode)
423                         return res;
424         }
425         return Inset::TEXT_MODE;
426 }
427
428
429 bool Cursor::inCoordCache() const
430 {
431         // the root inset is not in cache, but we do not need it.
432         if (depth() == 1)
433                 return true;
434         CoordCache::Insets const & icache = bv_->coordCache().getInsets();
435         for (size_t i = 1 ; i < depth() ; ++i)
436                 if (!icache.has(&(*this)[i].inset()))
437                         return false;
438         return true;
439 }
440
441
442 void Cursor::getPos(int & x, int & y) const
443 {
444         Point p = bv().getPos(*this);
445         x = p.x_;
446         y = p.y_;
447 }
448
449
450 Row const & Cursor::textRow() const
451 {
452         CursorSlice const & cs = innerTextSlice();
453         ParagraphMetrics const & pm = bv().parMetrics(cs.text(), cs.pit());
454         return pm.getRow(pos(), boundary());
455 }
456
457
458 void Cursor::resetAnchor()
459 {
460         anchor_ = *this;
461         checkNewWordPosition();
462 }
463
464
465 void Cursor::markNewWordPosition()
466 {
467         if (lyxrc.spellcheck_continuously && inTexted() && new_word_.empty()) {
468                 FontSpan nw = locateWord(WHOLE_WORD);
469                 if (nw.size() == 1) {
470                         LYXERR(Debug::DEBUG, "start new word: "
471                                 << " par: " << pit()
472                                 << " pos: " << nw.first);
473                         new_word_ = *this;
474                 }
475         }
476 }
477
478
479 void Cursor::clearNewWordPosition()
480 {
481         if (!new_word_.empty()) {
482                 LYXERR(Debug::DEBUG, "clear new word: "
483                         << " par: " << pit()
484                         << " pos: " << pos());
485                 new_word_.resize(0);
486         }
487 }
488
489
490 void Cursor::checkNewWordPosition()
491 {
492         if (!lyxrc.spellcheck_continuously || new_word_.empty())
493                 return ;
494         if (!inTexted())
495                 clearNewWordPosition();
496         else {
497                 // forget the position of the current new word if
498                 // 1) the paragraph changes or
499                 // 2) the count of nested insets changes or
500                 // 3) the cursor pos is out of paragraph bound
501                 if (pit() != new_word_.pit() ||
502                         depth() != new_word_.depth() ||
503                         new_word_.pos() > new_word_.lastpos()) {
504                         clearNewWordPosition();
505                 } else if (new_word_.fixIfBroken())
506                         // 4) or the remembered position was "broken"
507                         clearNewWordPosition();
508                 else {
509                         FontSpan nw = locateWord(WHOLE_WORD);
510                         if (!nw.empty()) {
511                                 FontSpan ow = new_word_.locateWord(WHOLE_WORD);
512                                 if (nw.intersect(ow).empty())
513                                         clearNewWordPosition();
514                                 else
515                                         LYXERR(Debug::DEBUG, "new word: "
516                                                    << " par: " << pit()
517                                                    << " pos: " << nw.first << ".." << nw.last);
518                         } else {
519                                 clearNewWordPosition();
520                         }
521                 }
522         }
523 }
524
525
526 bool Cursor::posBackward()
527 {
528         if (pos() == 0)
529                 return false;
530         --pos();
531         return true;
532 }
533
534
535 bool Cursor::posForward()
536 {
537         if (pos() == lastpos())
538                 return false;
539         ++pos();
540         return true;
541 }
542
543
544 bool Cursor::posVisRight(bool skip_inset)
545 {
546         Cursor new_cur = *this; // where we will move to
547         pos_type left_pos; // position visually left of current cursor
548         pos_type right_pos; // position visually right of current cursor
549
550         getSurroundingPos(left_pos, right_pos);
551
552         LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
553
554         // Are we at an inset?
555         new_cur.pos() = right_pos;
556         new_cur.boundary(false);
557         if (!skip_inset &&
558                 text()->checkAndActivateInsetVisual(new_cur, right_pos >= pos(), false)) {
559                 // we actually move the cursor at the end of this
560                 // function, for now we just keep track of the new
561                 // position in new_cur...
562                 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
563         }
564
565         // Are we already at rightmost pos in row?
566         else if (text()->empty() || right_pos == -1) {
567
568                 new_cur = *this;
569                 if (!new_cur.posVisToNewRow(false)) {
570                         LYXERR(Debug::RTL, "not moving!");
571                         return false;
572                 }
573
574                 // we actually move the cursor at the end of this
575                 // function, for now just keep track of the new
576                 // position in new_cur...
577                 LYXERR(Debug::RTL, "right edge, moving: " << int(new_cur.pit()) << ","
578                         << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
579
580         }
581         // normal movement to the right
582         else {
583                 new_cur = *this;
584                 // Recall, if the cursor is at position 'x', that
585                 // means *before* the character at position 'x'. In
586                 // RTL, "before" means "to the right of", in LTR, "to
587                 // the left of". So currently our situation is this:
588                 // the position to our right is 'right_pos' (i.e.,
589                 // we're currently to the left of 'right_pos'). In
590                 // order to move to the right, it depends whether or
591                 // not the character at 'right_pos' is RTL.
592                 bool const new_pos_is_RTL = paragraph().getFontSettings(
593                         buffer()->params(), right_pos).isVisibleRightToLeft();
594                 // If the character at 'right_pos' *is* LTR, then in
595                 // order to move to the right of it, we need to be
596                 // *after* 'right_pos', i.e., move to position
597                 // 'right_pos' + 1.
598                 if (!new_pos_is_RTL) {
599                         new_cur.pos() = right_pos + 1;
600                         // set the boundary to true in two situations:
601                         if (
602                         // 1. if new_pos is now lastpos, and we're in
603                         // an RTL paragraph (this means that we're
604                         // moving right to the end of an LTR chunk
605                         // which is at the end of an RTL paragraph);
606                                 (new_cur.pos() == lastpos()
607                                  && paragraph().isRTL(buffer()->params()))
608                         // 2. if the position *after* right_pos is RTL
609                         // (we want to be *after* right_pos, not
610                         // before right_pos + 1!)
611                                 || paragraph().getFontSettings(buffer()->params(),
612                                                 new_cur.pos()).isVisibleRightToLeft()
613                         )
614                                 new_cur.boundary(true);
615                         else // set the boundary to false
616                                 new_cur.boundary(false);
617                 }
618                 // Otherwise (if the character at position 'right_pos'
619                 // is RTL), then moving to the right of it is as easy
620                 // as setting the new position to 'right_pos'.
621                 else {
622                         new_cur.pos() = right_pos;
623                         new_cur.boundary(false);
624                 }
625
626         }
627
628         bool const moved = new_cur != *this || new_cur.boundary() != boundary();
629
630         if (moved) {
631                 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
632                         << (new_cur.boundary() ? " (boundary)" : ""));
633                 *this = new_cur;
634         }
635
636         return moved;
637 }
638
639
640 bool Cursor::posVisLeft(bool skip_inset)
641 {
642         Cursor new_cur = *this; // where we will move to
643         pos_type left_pos; // position visually left of current cursor
644         pos_type right_pos; // position visually right of current cursor
645
646         getSurroundingPos(left_pos, right_pos);
647
648         LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
649
650         // Are we at an inset?
651         new_cur.pos() = left_pos;
652         new_cur.boundary(false);
653         if (!skip_inset &&
654                 text()->checkAndActivateInsetVisual(new_cur, left_pos >= pos(), true)) {
655                 // we actually move the cursor at the end of this
656                 // function, for now we just keep track of the new
657                 // position in new_cur...
658                 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
659         }
660
661         // Are we already at leftmost pos in row?
662         else if (text()->empty() || left_pos == -1) {
663
664                 new_cur = *this;
665                 if (!new_cur.posVisToNewRow(true)) {
666                         LYXERR(Debug::RTL, "not moving!");
667                         return false;
668                 }
669
670                 // we actually move the cursor at the end of this
671                 // function, for now just keep track of the new
672                 // position in new_cur...
673                 LYXERR(Debug::RTL, "left edge, moving: " << int(new_cur.pit()) << ","
674                         << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
675
676         }
677         // normal movement to the left
678         else {
679                 new_cur = *this;
680                 // Recall, if the cursor is at position 'x', that
681                 // means *before* the character at position 'x'. In
682                 // RTL, "before" means "to the right of", in LTR, "to
683                 // the left of". So currently our situation is this:
684                 // the position to our left is 'left_pos' (i.e., we're
685                 // currently to the right of 'left_pos'). In order to
686                 // move to the left, it depends whether or not the
687                 // character at 'left_pos' is RTL.
688                 bool const new_pos_is_RTL = paragraph().getFontSettings(
689                         buffer()->params(), left_pos).isVisibleRightToLeft();
690                 // If the character at 'left_pos' *is* RTL, then in
691                 // order to move to the left of it, we need to be
692                 // *after* 'left_pos', i.e., move to position
693                 // 'left_pos' + 1.
694                 if (new_pos_is_RTL) {
695                         new_cur.pos() = left_pos + 1;
696                         // set the boundary to true in two situations:
697                         if (
698                         // 1. if new_pos is now lastpos and we're in
699                         // an LTR paragraph (this means that we're
700                         // moving left to the end of an RTL chunk
701                         // which is at the end of an LTR paragraph);
702                                 (new_cur.pos() == lastpos()
703                                  && !paragraph().isRTL(buffer()->params()))
704                         // 2. if the position *after* left_pos is not
705                         // RTL (we want to be *after* left_pos, not
706                         // before left_pos + 1!)
707                                 || !paragraph().getFontSettings(buffer()->params(),
708                                                 new_cur.pos()).isVisibleRightToLeft()
709                         )
710                                 new_cur.boundary(true);
711                         else // set the boundary to false
712                                 new_cur.boundary(false);
713                 }
714                 // Otherwise (if the character at position 'left_pos'
715                 // is LTR), then moving to the left of it is as easy
716                 // as setting the new position to 'left_pos'.
717                 else {
718                         new_cur.pos() = left_pos;
719                         new_cur.boundary(false);
720                 }
721
722         }
723
724         bool const moved = new_cur != *this || new_cur.boundary() != boundary();
725
726         if (moved) {
727                 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
728                         << (new_cur.boundary() ? " (boundary)" : ""));
729                 *this = new_cur;
730         }
731
732         return moved;
733 }
734
735
736 namespace {
737
738 // Return true on success
739 bool findNonVirtual(Row const & row, Row::const_iterator & cit, bool onleft)
740 {
741         if (onleft) {
742                 while (cit != row.begin() && cit->isVirtual())
743                         --cit;
744         } else {
745                 while (cit != row.end() && cit->isVirtual())
746                         ++cit;
747         }
748         return cit != row.end() && !cit->isVirtual();
749 }
750
751 }
752
753 void Cursor::getSurroundingPos(pos_type & left_pos, pos_type & right_pos) const
754 {
755         // by default, we know nothing.
756         left_pos = -1;
757         right_pos = -1;
758
759         Row const & row = textRow();
760         TextMetrics const & tm = bv_->textMetrics(text());
761         double dummy = 0;
762         Row::const_iterator cit = tm.findRowElement(row, pos(), boundary(), dummy);
763         // Handle the case of empty row
764         if (cit == row.end()) {
765                 if (paragraph().isRTL(buffer()->params()))
766                         right_pos = row.pos();
767                 else
768                         left_pos = row.pos() - 1;
769                 return;
770         }
771
772         // skip virtual elements and exit if no non-virtual one exists
773         if (!findNonVirtual(row, cit, !cit->isRTL()))
774                 return;
775
776         // if the position is at the left side of the element, we have to
777         // look at the previous element
778         if (pos() == cit->left_pos()) {
779                 LYXERR(Debug::RTL, "getSurroundingPos(" << pos() << (boundary() ? "b" : "")
780                            << "), AT LEFT of *cit=" << *cit);
781                 // this one is easy (see common case below)
782                 right_pos = pos() - (cit->isRTL() ? 1 : 0);
783                 // at the left of the row
784                 if (cit == row.begin())
785                         return;
786                 --cit;
787                 if (!findNonVirtual(row, cit, true))
788                         return;
789                 // [...[ is the row element, | is cursor position (! with boundary)
790                 // [ 1 2 [ is a ltr row element with pos=1 and endpos=3
791                 // ] 2 1] is an rtl row element with pos=1 and endpos=3
792                 //    [ 1 2 [  [|3 4 [ => (2, 3)
793                 // or [ 1 2 [  ]!4 3 ] => (2, 4)
794                 // or ] 2 1 ]  [|3 4 [ => (1, 3)
795                 // or ] 4 3 ]  ]!2 1 ] => (3, 2)
796                 left_pos = cit->right_pos() - (cit->isRTL() ? 0 : 1);
797                 // happens with consecutive row of same direction
798                 if (left_pos == right_pos) {
799                         left_pos += cit->isRTL() ? 1 : -1;
800                 }
801         }
802         // same code but with the element at the right
803         else if (pos() == cit->right_pos()) {
804                 LYXERR(Debug::RTL, "getSurroundingPos(" << pos() << (boundary() ? "b" : "")
805                            << "), AT RIGHT of *cit=" << *cit);
806                 // this one is easy (see common case below)
807                 left_pos = pos() - (cit->isRTL() ? 0 : 1);
808                 // at the right of the row
809                 if (cit + 1 == row.end())
810                         return;
811                 ++cit;
812                 if (!findNonVirtual(row, cit, false))
813                         return;
814                 //    [ 1 2![  [ 3 4 [ => (2, 3)
815                 // or [ 1 2![  ] 4 3 ] => (2, 4)
816                 // or ] 2 1|]  [ 3 4 [ => (1, 3)
817                 // or ] 4 3|]  ] 2 1 ] => (3, 2)
818                 right_pos = cit->left_pos() - (cit->isRTL() ? 1 : 0);
819                 // happens with consecutive row of same direction
820                 if (right_pos == left_pos)
821                         right_pos += cit->isRTL() ? -1 : 1;
822         }
823         // common case: both positions are inside the row element
824         else {
825                 //    [ 1 2|3 [ => (2, 3)
826                 // or ] 3|2 1 ] => (3, 2)
827                 left_pos = pos() - (cit->isRTL() ? 0 : 1);
828                 right_pos = pos() - (cit->isRTL() ? 1 : 0);
829         }
830
831         // Note that debug message does not catch all early returns above
832         LYXERR(Debug::RTL,"getSurroundingPos(" << pos() << (boundary() ? "b" : "")
833                    << ") => (" << left_pos << ", " << right_pos <<")");
834 }
835
836
837 bool Cursor::posVisToNewRow(bool movingLeft)
838 {
839         Paragraph const & par = paragraph();
840         Buffer const & buf = *buffer();
841         Row const & row = textRow();
842         bool par_is_LTR = !par.isRTL(buf.params());
843
844         // Inside a table, determining whether to move to the next or
845         // previous row should be done based on the table's direction.
846         if (inset().asInsetTabular()) {
847                 par_is_LTR = !inset().asInsetTabular()->isRightToLeft(*this);
848                 LYXERR(Debug::RTL, "Inside table! par_is_LTR=" << (par_is_LTR ? 1 : 0));
849         }
850
851         // if moving left in an LTR paragraph or moving right in an
852         // RTL one, move to previous row
853         if (par_is_LTR == movingLeft) {
854                 if (row.pos() == 0) { // we're at first row in paragraph
855                         if (pit() == 0) // no previous paragraph! don't move
856                                 return false;
857                         // move to last pos in previous par
858                         --pit();
859                         pos() = lastpos();
860                         boundary(false);
861                 } else { // move to previous row in this par
862                         pos() = row.pos() - 1; // this is guaranteed to be in previous row
863                         boundary(false);
864                 }
865         }
866         // if moving left in an RTL paragraph or moving right in an
867         // LTR one, move to next row
868         else {
869                 if (row.endpos() == lastpos()) { // we're at last row in paragraph
870                         if (pit() == lastpit()) // last paragraph! don't move
871                                 return false;
872                         // move to first row in next par
873                         ++pit();
874                         pos() = 0;
875                         boundary(false);
876                 } else { // move to next row in this par
877                         pos() = row.endpos();
878                         boundary(false);
879                 }
880         }
881
882         // make sure we're at left-/right-most pos in new row
883         posVisToRowExtremity(!movingLeft);
884
885         return true;
886 }
887
888
889 void Cursor::posVisToRowExtremity(bool left)
890 {
891         LYXERR(Debug::RTL, "entering extremity: " << pit() << "," << pos() << ","
892                 << (boundary() ? 1 : 0));
893
894         TextMetrics const & tm = bv_->textMetrics(text());
895         // Looking for extremities is like clicking on the left or the
896         // right of the row.
897         int x = tm.origin().x_ + (left ? 0 : textRow().width());
898         bool b = false;
899         pos() = tm.getPosNearX(textRow(), x, b);
900         boundary(b);
901
902         LYXERR(Debug::RTL, "leaving extremity: " << pit() << "," << pos() << ","
903                 << (boundary() ? 1 : 0));
904 }
905
906
907 bool Cursor::reverseDirectionNeeded() const
908 {
909         /*
910          * We determine the directions based on the direction of the
911          * bottom() --- i.e., outermost --- paragraph, because that is
912          * the only way to achieve consistency of the arrow's movements
913          * within a paragraph, and thus avoid situations in which the
914          * cursor gets stuck.
915          */
916         return bottom().paragraph().isRTL(bv().buffer().params());
917 }
918
919
920 CursorSlice Cursor::normalAnchor() const
921 {
922         if (!selection())
923                 return top();
924         // LASSERT: There have been several bugs around this code, that seem
925         // to involve failures to reset the anchor. We can at least not crash
926         // in release mode by resetting it ourselves.
927         if (anchor_.depth() < depth()) {
928                 LYXERR0("Cursor is deeper than anchor. PLEASE REPORT.\nCursor is"
929                         << *this);
930                 const_cast<DocIterator &>(anchor_) = *this;
931         }
932
933         CursorSlice normal = anchor_[depth() - 1];
934         if (depth() < anchor_.depth() && top() <= normal) {
935                 // anchor is behind cursor -> move anchor behind the inset
936                 ++normal.pos();
937         }
938         return normal;
939 }
940
941
942 CursorSlice Cursor::selBegin() const
943 {
944         if (!selection())
945                 return top();
946         return normalAnchor() < top() ? normalAnchor() : top();
947 }
948
949
950 CursorSlice Cursor::selEnd() const
951 {
952         if (!selection())
953                 return top();
954         return normalAnchor() > top() ? normalAnchor() : top();
955 }
956
957
958 DocIterator Cursor::selectionBegin() const
959 {
960         if (!selection())
961                 return *this;
962
963         DocIterator di;
964         // FIXME: This is a work-around for the problem that
965         // CursorSlice doesn't keep track of the boundary.
966         if (normalAnchor() == top())
967                 di = anchor_.boundary() > boundary() ? anchor_ : *this;
968         else
969                 di = normalAnchor() < top() ? anchor_ : *this;
970         di.resize(depth());
971         return di;
972 }
973
974
975 DocIterator Cursor::selectionEnd() const
976 {
977         if (!selection())
978                 return *this;
979
980         DocIterator di;
981         // FIXME: This is a work-around for the problem that
982         // CursorSlice doesn't keep track of the boundary.
983         if (normalAnchor() == top())
984                 di = anchor_.boundary() < boundary() ? anchor_ : *this;
985         else
986                 di = normalAnchor() > top() ? anchor_ : *this;
987
988         if (di.depth() > depth()) {
989                 di.resize(depth());
990                 ++di.pos();
991         }
992         return di;
993 }
994
995
996 void Cursor::setSelection()
997 {
998         selection(true);
999         if (idx() == normalAnchor().idx() &&
1000             pit() == normalAnchor().pit() &&
1001             pos() == normalAnchor().pos())
1002                 selection(false);
1003 }
1004
1005
1006 void Cursor::setSelection(DocIterator const & where, int n)
1007 {
1008         setCursor(where);
1009         selection(true);
1010         anchor_ = where;
1011         pos() += n;
1012 }
1013
1014
1015 void Cursor::clearSelection()
1016 {
1017         selection(false);
1018         setWordSelection(false);
1019         setMark(false);
1020         resetAnchor();
1021 }
1022
1023
1024 void Cursor::setTargetX(int x)
1025 {
1026         x_target_ = x;
1027         textTargetOffset_ = 0;
1028 }
1029
1030
1031 int Cursor::x_target() const
1032 {
1033         return x_target_;
1034 }
1035
1036
1037 void Cursor::clearTargetX()
1038 {
1039         x_target_ = -1;
1040         textTargetOffset_ = 0;
1041 }
1042
1043
1044 void Cursor::updateTextTargetOffset()
1045 {
1046         int x;
1047         int y;
1048         getPos(x, y);
1049         textTargetOffset_ = x - x_target_;
1050 }
1051
1052
1053 void Cursor::info(odocstream & os) const
1054 {
1055         for (int i = 1, n = depth(); i < n; ++i) {
1056                 operator[](i).inset().infoize(os);
1057                 os << "  ";
1058         }
1059         if (pos() != 0) {
1060                 Inset const * inset = prevInset();
1061                 // prevInset() can return 0 in certain case.
1062                 if (inset)
1063                         prevInset()->infoize2(os);
1064         }
1065 }
1066
1067
1068 bool Cursor::selHandle(bool sel)
1069 {
1070         //lyxerr << "Cursor::selHandle" << endl;
1071         if (mark())
1072                 sel = true;
1073         if (sel == selection())
1074                 return false;
1075
1076         if (!sel)
1077                 cap::saveSelection(*this);
1078
1079         resetAnchor();
1080         selection(sel);
1081         return true;
1082 }
1083 } // namespace lyx
1084
1085
1086 ///////////////////////////////////////////////////////////////////
1087 //
1088 // FIXME: Look here
1089 // The part below is the non-integrated rest of the original math
1090 // cursor. This should be either generalized for texted or moved
1091 // back to mathed (in most cases to InsetMathNest).
1092 //
1093 ///////////////////////////////////////////////////////////////////
1094
1095 #include "mathed/InsetMathChar.h"
1096 #include "mathed/InsetMathGrid.h"
1097 #include "mathed/InsetMathScript.h"
1098 #include "mathed/InsetMathUnknown.h"
1099 #include "mathed/MathFactory.h"
1100 #include "mathed/MathStream.h"
1101 #include "mathed/MathSupport.h"
1102
1103
1104 namespace lyx {
1105
1106 bool Cursor::isInside(Inset const * p) const
1107 {
1108         for (size_t i = 0; i != depth(); ++i)
1109                 if (&operator[](i).inset() == p)
1110                         return true;
1111         return false;
1112 }
1113
1114
1115 void Cursor::leaveInset(Inset const & inset)
1116 {
1117         for (size_t i = 0; i != depth(); ++i) {
1118                 if (&operator[](i).inset() == &inset) {
1119                         resize(i);
1120                         return;
1121                 }
1122         }
1123 }
1124
1125
1126 bool Cursor::openable(MathAtom const & t) const
1127 {
1128         if (!t->isActive())
1129                 return false;
1130
1131         if (t->lock())
1132                 return false;
1133
1134         if (!selection())
1135                 return true;
1136
1137         // we can't move into anything new during selection
1138         if (depth() >= anchor_.depth())
1139                 return false;
1140         if (t.nucleus() != &anchor_[depth()].inset())
1141                 return false;
1142
1143         return true;
1144 }
1145
1146
1147 void Cursor::setScreenPos(int x, int /*y*/)
1148 {
1149         setTargetX(x);
1150         //bruteFind(*this, x, y, 0, bv().workWidth(), 0, bv().workHeight());
1151 }
1152
1153
1154
1155 void Cursor::plainErase()
1156 {
1157         cell().erase(pos());
1158 }
1159
1160
1161 void Cursor::markInsert()
1162 {
1163         insert(char_type(0));
1164 }
1165
1166
1167 void Cursor::markErase()
1168 {
1169         cell().erase(pos());
1170 }
1171
1172
1173 void Cursor::plainInsert(MathAtom const & t)
1174 {
1175         cell().insert(pos(), t);
1176         ++pos();
1177         inset().setBuffer(bv_->buffer());
1178         inset().initView();
1179         checkBufferStructure();
1180 }
1181
1182
1183 void Cursor::insert(docstring const & str)
1184 {
1185         for (char_type c : str)
1186                 insert(c);
1187 }
1188
1189
1190 void Cursor::insert(char_type c)
1191 {
1192         //lyxerr << "Cursor::insert char '" << c << "'" << endl;
1193         LASSERT(!empty(), return);
1194         if (inMathed()) {
1195                 cap::selClearOrDel(*this);
1196                 insert(new InsetMathChar(c));
1197         } else {
1198                 text()->insertChar(*this, c);
1199         }
1200 }
1201
1202
1203 void Cursor::insert(MathAtom const & t)
1204 {
1205         //lyxerr << "Cursor::insert MathAtom '" << t << "'" << endl;
1206         macroModeClose();
1207         cap::selClearOrDel(*this);
1208         plainInsert(t);
1209 }
1210
1211
1212 void Cursor::insert(Inset * inset0)
1213 {
1214         LASSERT(inset0, return);
1215         if (inMathed())
1216                 insert(MathAtom(inset0->asInsetMath()));
1217         else {
1218                 text()->insertInset(*this, inset0);
1219                 inset0->setBuffer(bv_->buffer());
1220                 inset0->initView();
1221                 if (inset0->isLabeled())
1222                         forceBufferUpdate();
1223         }
1224 }
1225
1226
1227 int Cursor::niceInsert(docstring const & t, Parse::flags f, bool enter)
1228 {
1229         MathData ar(buffer());
1230         asArray(t, ar, f);
1231         if (ar.size() == 1 && (enter || selection()))
1232                 niceInsert(ar[0]);
1233         else
1234                 insert(ar);
1235         return ar.size();
1236 }
1237
1238
1239 void Cursor::niceInsert(MathAtom const & t)
1240 {
1241         macroModeClose();
1242         docstring const safe = cap::grabAndEraseSelection(*this);
1243         plainInsert(t);
1244         // If possible, enter the new inset and move the contents of the selection
1245         if (t->isActive()) {
1246                 posBackward();
1247                 // be careful here: don't use 'pushBackward(t)' as this we need to
1248                 // push the clone, not the original
1249                 pushBackward(*nextInset());
1250                 // We may not use niceInsert here (recursion)
1251                 MathData ar(buffer());
1252                 asArray(safe, ar);
1253                 insert(ar);
1254         } else if (t->asMacro() && !safe.empty()) {
1255                 MathData ar(buffer());
1256                 asArray(safe, ar);
1257                 docstring const name = t->asMacro()->name();
1258                 MacroData const * data = buffer()->getMacro(name);
1259                 if (data && data->numargs() - data->optionals() > 0) {
1260                         plainInsert(MathAtom(new InsetMathBrace(ar)));
1261                         posBackward();
1262                 }
1263         }
1264 }
1265
1266
1267 void Cursor::insert(MathData const & ar)
1268 {
1269         macroModeClose();
1270         if (selection())
1271                 cap::eraseSelection(*this);
1272         cell().insert(pos(), ar);
1273         pos() += ar.size();
1274         // FIXME audit setBuffer calls
1275         inset().setBuffer(bv_->buffer());
1276 }
1277
1278
1279 bool Cursor::backspace()
1280 {
1281         if (selection()) {
1282                 cap::eraseSelection(*this);
1283                 return true;
1284         }
1285
1286         if (pos() == 0) {
1287                 // If empty cell, and not part of a big cell
1288                 if (lastpos() == 0 && inset().nargs() == 1) {
1289                         popBackward();
1290                         // Directly delete empty cell: [|[]] => [|]
1291                         if (inMathed()) {
1292                                 plainErase();
1293                                 resetAnchor();
1294                                 return true;
1295                         }
1296                         // [|], can not delete from inside
1297                         return false;
1298                 } else {
1299                         if (inMathed())
1300                                 pullArg();
1301                         else
1302                                 popBackward();
1303                         return true;
1304                 }
1305         }
1306
1307         if (inMacroMode()) {
1308                 InsetMathUnknown * p = activeMacro();
1309                 if (p->name().size() > 1) {
1310                         p->setName(p->name().substr(0, p->name().size() - 1));
1311                         return true;
1312                 }
1313         }
1314
1315         if (pos() != 0 && prevAtom()->nargs() > 0) {
1316                 // let's require two backspaces for 'big stuff' and
1317                 // highlight on the first
1318                 resetAnchor();
1319                 selection(true);
1320                 --pos();
1321         } else {
1322                 --pos();
1323                 plainErase();
1324         }
1325         return true;
1326 }
1327
1328
1329 bool Cursor::erase()
1330 {
1331         if (inMacroMode())
1332                 return true;
1333
1334         if (selection()) {
1335                 cap::eraseSelection(*this);
1336                 return true;
1337         }
1338
1339         // delete empty cells if possible
1340         if (pos() == lastpos() && inset().idxDelete(idx()))
1341                 return true;
1342
1343         // special behaviour when in last position of cell
1344         if (pos() == lastpos()) {
1345                 bool one_cell = inset().nargs() == 1;
1346                 if (one_cell && lastpos() == 0) {
1347                         popBackward();
1348                         // Directly delete empty cell: [|[]] => [|]
1349                         if (inMathed()) {
1350                                 plainErase();
1351                                 resetAnchor();
1352                                 return true;
1353                         }
1354                         // [|], can not delete from inside
1355                         return false;
1356                 }
1357                 // remove markup
1358                 if (!one_cell)
1359                         inset().idxGlue(idx());
1360                 return true;
1361         }
1362
1363         // 'clever' UI hack: only erase large items if previously slected
1364         if (pos() != lastpos() && nextAtom()->nargs() > 0) {
1365                 resetAnchor();
1366                 selection(true);
1367                 ++pos();
1368         } else {
1369                 plainErase();
1370         }
1371
1372         return true;
1373 }
1374
1375
1376 bool Cursor::up()
1377 {
1378         macroModeClose();
1379         DocIterator save = *this;
1380         FuncRequest cmd(selection() ? LFUN_UP_SELECT : LFUN_UP, docstring());
1381         this->dispatch(cmd);
1382         if (disp_.dispatched())
1383                 return true;
1384         setCursor(save);
1385         return false;
1386 }
1387
1388
1389 bool Cursor::down()
1390 {
1391         macroModeClose();
1392         DocIterator save = *this;
1393         FuncRequest cmd(selection() ? LFUN_DOWN_SELECT : LFUN_DOWN, docstring());
1394         this->dispatch(cmd);
1395         if (disp_.dispatched())
1396                 return true;
1397         setCursor(save);
1398         return false;
1399 }
1400
1401
1402 bool Cursor::macroModeClose()
1403 {
1404         if (!inMacroMode())
1405                 return false;
1406         InsetMathUnknown * p = activeMacro();
1407         p->finalize();
1408         MathData selection(buffer());
1409         asArray(p->selection(), selection);
1410         docstring const s = p->name();
1411         --pos();
1412         cell().erase(pos());
1413
1414         // do nothing if the macro name is empty
1415         if (s == "\\")
1416                 return false;
1417
1418         // trigger updates of macros, at least, if no full
1419         // updates take place anyway
1420         screenUpdateFlags(Update::Force);
1421
1422         docstring const name = s.substr(1);
1423         InsetMathNest * const in = inset().asInsetMath()->asNestInset();
1424         if (in && in->interpretString(*this, s))
1425                 return true;
1426         MathAtom atom = buffer()->getMacro(name, *this, false) ?
1427                 MathAtom(new MathMacro(buffer(), name)) : createInsetMath(name, buffer());
1428
1429         // try to put argument into macro, if we just inserted a macro
1430         bool macroArg = false;
1431         MathMacro * atomAsMacro = atom.nucleus()->asMacro();
1432         if (atomAsMacro) {
1433                 // macros here are still unfolded (in init mode in fact). So
1434                 // we have to resolve the macro here manually and check its arity
1435                 // to put the selection behind it if arity > 0.
1436                 MacroData const * data = buffer()->getMacro(atomAsMacro->name());
1437                 if (!selection.empty() && data && data->numargs() - data->optionals() > 0) {
1438                         macroArg = true;
1439                         atomAsMacro->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT, 1);
1440                 } else
1441                         // non-greedy case. Do not touch the arguments behind
1442                         atomAsMacro->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT, 0);
1443         }
1444
1445         // insert remembered selection into first argument of a non-macro
1446         else if (atom.nucleus()->nargs() > 0)
1447                 atom.nucleus()->cell(0).append(selection);
1448
1449         plainInsert(atom);
1450
1451         // finally put the macro argument behind, if needed
1452         if (macroArg) {
1453                 if (selection.size() > 1 || selection[0]->asScriptInset())
1454                         plainInsert(MathAtom(new InsetMathBrace(selection)));
1455                 else
1456                         insert(selection);
1457         }
1458
1459         return true;
1460 }
1461
1462
1463 docstring Cursor::macroName()
1464 {
1465         return inMacroMode() ? activeMacro()->name() : docstring();
1466 }
1467
1468
1469 void Cursor::handleNest(MathAtom const & a, int c)
1470 {
1471         //lyxerr << "Cursor::handleNest: " << c << endl;
1472         MathAtom t = a;
1473         asArray(cap::grabAndEraseSelection(*this), t.nucleus()->cell(c));
1474         insert(t);
1475         posBackward();
1476         pushBackward(*nextInset());
1477 }
1478
1479
1480 int Cursor::targetX() const
1481 {
1482         if (x_target() != -1)
1483                 return x_target();
1484         int x = 0;
1485         int y = 0;
1486         getPos(x, y);
1487         return x;
1488 }
1489
1490
1491 int Cursor::textTargetOffset() const
1492 {
1493         return textTargetOffset_;
1494 }
1495
1496
1497 void Cursor::setTargetX()
1498 {
1499         int x;
1500         int y;
1501         getPos(x, y);
1502         setTargetX(x);
1503 }
1504
1505
1506 bool Cursor::inMacroMode() const
1507 {
1508         if (!inMathed())
1509                 return false;
1510         if (pos() == 0 || cell().empty())
1511                 return false;
1512         InsetMathUnknown const * p = prevAtom()->asUnknownInset();
1513         return p && !p->final();
1514 }
1515
1516
1517 InsetMathUnknown * Cursor::activeMacro()
1518 {
1519         return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1520 }
1521
1522
1523 InsetMathUnknown const * Cursor::activeMacro() const
1524 {
1525         return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1526 }
1527
1528
1529 void Cursor::pullArg()
1530 {
1531         // FIXME: Look here
1532         MathData ar = cell();
1533         if (popBackward() && inMathed()) {
1534                 plainErase();
1535                 cell().insert(pos(), ar);
1536                 resetAnchor();
1537         } else {
1538                 //formula()->mutateToText();
1539         }
1540 }
1541
1542
1543 void Cursor::touch()
1544 {
1545         // FIXME: look here
1546 #if 0
1547         DocIterator::const_iterator it = begin();
1548         DocIterator::const_iterator et = end();
1549         for ( ; it != et; ++it)
1550                 it->cell().touch();
1551 #endif
1552 }
1553
1554
1555 void Cursor::normalize()
1556 {
1557         if (idx() > lastidx()) {
1558                 lyxerr << "this should not really happen - 1: "
1559                        << idx() << ' ' << nargs()
1560                        << " in: " << &inset() << endl;
1561                 idx() = lastidx();
1562         }
1563
1564         if (pos() > lastpos()) {
1565                 lyxerr << "this should not really happen - 2: "
1566                         << pos() << ' ' << lastpos() <<  " in idx: " << idx()
1567                        << " in atom: '";
1568                 odocstringstream os;
1569                 TexRow texrow(false);
1570                 otexrowstream ots(os,texrow);
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