3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
7 * \author Alfredo Braunstein
8 * \author Dov Feldstern
10 * \author Stefan Schimanski
12 * Full author contact details are available in file CREDITS.
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "CoordCache.h"
22 #include "CutAndPaste.h"
23 #include "DispatchResult.h"
27 #include "FuncRequest.h"
30 #include "LyXAction.h"
32 #include "Paragraph.h"
33 #include "ParIterator.h"
35 #include "texstream.h"
37 #include "TextMetrics.h"
38 #include "TocBackend.h"
40 #include "support/debug.h"
41 #include "support/docstream.h"
42 #include "support/ExceptionMessage.h"
43 #include "support/gettext.h"
44 #include "support/lassert.h"
46 #include "insets/InsetTabular.h"
47 #include "insets/InsetText.h"
49 #include "mathed/InsetMath.h"
50 #include "mathed/InsetMathBrace.h"
51 #include "mathed/InsetMathEnsureMath.h"
52 #include "mathed/InsetMathScript.h"
53 #include "mathed/MacroTable.h"
54 #include "mathed/MathData.h"
55 #include "mathed/MathFactory.h"
56 #include "mathed/InsetMathMacro.h"
69 // Find position closest to (x, y) in cell given by iter.
70 // Used only in mathed
71 DocIterator bruteFind(Cursor const & c, int x, int y)
73 double best_dist = numeric_limits<double>::max();
80 et.pos() = et.lastpos();
81 for (size_t i = 0;; ++i) {
84 Inset const * inset = &it.inset();
85 CoordCache::Insets const & insetCache = c.bv().coordCache().getInsets();
87 // FIXME: in the case where the inset is not in the cache, this
88 // means that no part of it is visible on screen. In this case
89 // we don't do elaborate search and we just return the forwarded
90 // DocIterator at its beginning.
91 if (!insetCache.has(inset)) {
96 Point const o = insetCache.xy(inset);
97 inset->cursorPos(c.bv(), it.top(), c.boundary(), xo, yo);
98 // Convert to absolute
101 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
102 // '<=' in order to take the last possible position
103 // this is important for clicking behind \sum in e.g. '\sum_i a'
104 LYXERR(Debug::DEBUG, "i: " << i << " d: " << d
105 << " best: " << best_dist);
106 if (d <= best_dist) {
121 CursorData::CursorData()
122 : DocIterator(), anchor_(),
123 selection_(false), mark_(false), word_selection_(false),
124 current_font(inherit_font),
125 autocorrect_(false), macromode_(false)
129 CursorData::CursorData(Buffer * buffer)
130 : DocIterator(buffer), anchor_(),
131 selection_(false), mark_(false), word_selection_(false),
132 current_font(inherit_font),
133 autocorrect_(false), macromode_(false)
137 CursorData::CursorData(DocIterator const & dit)
138 : DocIterator(dit), anchor_(),
139 selection_(false), mark_(false), word_selection_(false),
140 current_font(inherit_font),
141 autocorrect_(false), macromode_(false)
147 ostream & operator<<(ostream & os, CursorData const & cur)
149 os << "\n cursor: | anchor:\n";
150 for (size_t i = 0, n = cur.depth(); i != n; ++i) {
151 os << " " << cur[i] << " | ";
152 if (i < cur.anchor_.depth())
153 os << cur.anchor_[i];
155 os << "-------------------------------";
158 for (size_t i = cur.depth(), n = cur.anchor_.depth(); i < n; ++i) {
159 os << "------------------------------- | " << cur.anchor_[i] << "\n";
161 os << " selection: " << cur.selection_
162 // << " x_target: " << cur.x_target_
163 << " boundary: " << cur.boundary() << endl;
168 LyXErr & operator<<(LyXErr & os, CursorData const & cur)
175 // be careful: this is called from the bv's constructor, too, so
176 // bv functions are not yet available!
177 Cursor::Cursor(BufferView & bv)
178 : CursorData(&bv.buffer()), bv_(&bv),
179 x_target_(-1), textTargetOffset_(0),
180 beforeDispatchPosX_(0), beforeDispatchPosY_(0)
187 push_back(CursorSlice(buffer()->inset()));
188 anchor_ = doc_iterator_begin(buffer());
190 new_word_ = doc_iterator_begin(buffer());
198 void Cursor::setCursor(DocIterator const & cur)
200 DocIterator::operator=(cur);
204 void Cursor::setCursorSelectionTo(DocIterator dit)
208 while (i < dit.depth() && i < anchor_.depth() && dit[i] == anchor_[i])
210 if (i != dit.depth()) {
211 // otherwise the cursor is already normal
212 if (i == anchor_.depth())
213 // dit is a proper extension of the anchor_
215 else if (i + 1 < dit.depth()) {
216 // one has dit[i] != anchor_[i] but either dit[i-1] == anchor_[i-1]
217 // or i == 0. Remove excess.
219 if (dit[i] > anchor_[i])
220 // place dit after the inset it was in
229 void Cursor::setCursorToAnchor()
232 DocIterator normal = anchor_;
233 while (depth() < normal.depth())
235 if (depth() < anchor_.depth() && top() <= anchor_[depth() - 1])
242 void Cursor::setCursorData(CursorData const & data)
244 CursorData::operator=(data);
248 bool Cursor::getStatus(FuncRequest const & cmd, FuncStatus & status) const
252 // Try to fix cursor in case it is broken.
255 // Is this a function that acts on inset at point?
256 Inset * inset = cur.nextInset();
257 if (lyxaction.funcHasFlag(cmd.action(), LyXAction::AtPoint)
258 && inset && inset->getStatus(cur, cmd, status))
261 // This is, of course, a mess. Better create a new doc iterator and use
262 // this in Inset::getStatus. This might require an additional
263 // BufferView * arg, though (which should be avoided)
264 //Cursor safe = *this;
266 for ( ; cur.depth(); cur.pop()) {
267 //lyxerr << "\nCursor::getStatus: cmd: " << cmd << endl << *this << endl;
268 // LASSERT: Is it safe to continue here, or should we return?
269 LASSERT(cur.idx() <= cur.lastidx(), /**/);
270 LASSERT(cur.pit() <= cur.lastpit(), /**/);
271 LASSERT(cur.pos() <= cur.lastpos(), /**/);
273 // The inset's getStatus() will return 'true' if it made
274 // a definitive decision on whether it want to handle the
275 // request or not. The result of this decision is put into
276 // the 'status' parameter.
277 if (cur.inset().getStatus(cur, cmd, status)) {
286 void Cursor::saveBeforeDispatchPosXY()
288 getPos(beforeDispatchPosX_, beforeDispatchPosY_);
292 void Cursor::dispatch(FuncRequest const & cmd0)
294 LYXERR(Debug::ACTION, "Cursor::dispatch: cmd: " << cmd0 << '\n' << *this);
299 FuncRequest cmd = cmd0;
302 disp_ = DispatchResult();
306 // Is this a function that acts on inset at point?
307 if (lyxaction.funcHasFlag(cmd.action(), LyXAction::AtPoint)
309 disp_.dispatched(true);
310 disp_.screenUpdate(Update::FitCursor | Update::Force);
311 FuncRequest tmpcmd = cmd;
312 LYXERR(Debug::DEBUG, "Cursor::dispatch: (AtPoint) cmd: "
313 << cmd0 << endl << *this);
314 nextInset()->dispatch(*this, tmpcmd);
315 if (disp_.dispatched()) {
321 // store some values to be used inside of the handlers
322 beforeDispatchCursor_ = *this;
323 for (; depth(); pop(), boundary(false)) {
324 LYXERR(Debug::DEBUG, "Cursor::dispatch: cmd: "
325 << cmd0 << endl << *this);
327 // In any of these cases, the cursor is invalid, and we should
328 // try to save this document rather than crash.
329 LBUFERR(pos() <= lastpos());
330 LBUFERR(idx() <= lastidx());
331 LBUFERR(pit() <= lastpit());
333 // The common case is 'LFUN handled, need update', so make the
334 // LFUN handler's life easier by assuming this as default value.
335 // The handler can reset the update and val flags if necessary.
336 disp_.screenUpdate(Update::FitCursor | Update::Force);
337 disp_.dispatched(true);
338 inset().dispatch(*this, cmd);
339 if (disp_.dispatched())
343 // it completely to get a 'bomb early' behaviour in case this
344 // object will be used again.
345 if (!disp_.dispatched()) {
346 LYXERR(Debug::DEBUG, "RESTORING OLD CURSOR!");
347 // We might have invalidated the cursor when removing an empty
348 // paragraph while the cursor could not be moved out the inset
349 // while we initially thought we could. This might happen when
350 // a multiline inset becomes an inline inset when the second
351 // paragraph is removed.
352 if (safe.pit() > safe.lastpit()) {
353 safe.pit() = safe.lastpit();
354 safe.pos() = safe.lastpos();
357 disp_.screenUpdate(Update::None);
358 disp_.dispatched(false);
360 // restore the previous one because nested Cursor::dispatch calls
361 // are possible which would change it
362 beforeDispatchCursor_ = safe.beforeDispatchCursor_;
366 // NOTE: The code below has been copied to BufferView::dispatch.
367 // If you need to modify this, please update the other one too.
369 // notify insets we just left
371 old.beginUndoGroup();
373 bool badcursor = notifyCursorLeavesOrEnters(old, *this);
376 bv().resetInlineCompletionPos();
383 DispatchResult const & Cursor::result() const
389 BufferView & Cursor::bv() const
398 LBUFERR(depth() >= 1);
403 void Cursor::push(Inset & p)
405 push_back(CursorSlice(p));
406 p.setBuffer(*buffer());
410 void Cursor::pushBackward(Inset & p)
412 LASSERT(!empty(), return);
413 //lyxerr << "Entering inset " << t << " front" << endl;
419 bool Cursor::popBackward()
421 LASSERT(!empty(), return false);
429 bool Cursor::popForward()
431 LASSERT(!empty(), return false);
432 //lyxerr << "Leaving inset from in back" << endl;
433 const pos_type lp = (depth() > 1) ? (*this)[depth() - 2].lastpos() : 0;
437 pos() += lastpos() - lp + 1;
442 int Cursor::currentMode()
444 LASSERT(!empty(), return Inset::UNDECIDED_MODE);
445 for (int i = depth() - 1; i >= 0; --i) {
446 int res = operator[](i).inset().currentMode();
447 bool locked_mode = operator[](i).inset().lockedMode();
448 // Also return UNDECIDED_MODE when the mode is locked,
449 // as in this case it is treated the same as TEXT_MODE
450 if (res != Inset::UNDECIDED_MODE || locked_mode)
453 return Inset::TEXT_MODE;
457 void Cursor::getPos(int & x, int & y) const
459 Point p = bv().getPos(*this);
465 Row const & Cursor::textRow() const
467 CursorSlice const & cs = innerTextSlice();
468 ParagraphMetrics const & pm = bv().parMetrics(cs.text(), cs.pit());
469 return pm.getRow(pos(), boundary());
473 void Cursor::resetAnchor()
476 checkNewWordPosition();
480 void Cursor::markNewWordPosition()
482 if (lyxrc.spellcheck_continuously && inTexted() && new_word_.empty()) {
483 FontSpan nw = locateWord(WHOLE_WORD);
484 if (nw.size() == 1) {
485 LYXERR(Debug::DEBUG, "start new word: "
487 << " pos: " << nw.first);
494 void Cursor::clearNewWordPosition()
496 if (!new_word_.empty()) {
497 LYXERR(Debug::DEBUG, "clear new word: "
499 << " pos: " << pos());
505 void Cursor::checkNewWordPosition()
507 if (!lyxrc.spellcheck_continuously || new_word_.empty())
510 clearNewWordPosition();
512 // forget the position of the current new word if
513 // 1) the paragraph changes or
514 // 2) the count of nested insets changes or
515 // 3) the cursor pos is out of paragraph bound
516 if (pit() != new_word_.pit() ||
517 depth() != new_word_.depth() ||
518 new_word_.pos() > new_word_.lastpos()) {
519 clearNewWordPosition();
520 } else if (new_word_.fixIfBroken())
521 // 4) or the remembered position was "broken"
522 clearNewWordPosition();
524 FontSpan nw = locateWord(WHOLE_WORD);
526 FontSpan ow = new_word_.locateWord(WHOLE_WORD);
527 if (nw.intersect(ow).empty())
528 clearNewWordPosition();
530 LYXERR(Debug::DEBUG, "new word: "
532 << " pos: " << nw.first << ".." << nw.last);
534 clearNewWordPosition();
541 bool Cursor::posVisRight(bool skip_inset)
543 Cursor new_cur = *this; // where we will move to
544 pos_type left_pos; // position visually left of current cursor
545 pos_type right_pos; // position visually right of current cursor
547 getSurroundingPos(left_pos, right_pos);
549 LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
551 // Are we at an inset?
552 new_cur.pos() = right_pos;
553 new_cur.boundary(false);
555 text()->checkAndActivateInsetVisual(new_cur, right_pos >= pos(), false)) {
556 // we actually move the cursor at the end of this
557 // function, for now we just keep track of the new
558 // position in new_cur...
559 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
562 // Are we already at rightmost pos in row?
563 else if (text()->empty() || right_pos == -1) {
566 if (!new_cur.posVisToNewRow(false)) {
567 LYXERR(Debug::RTL, "not moving!");
571 // we actually move the cursor at the end of this
572 // function, for now just keep track of the new
573 // position in new_cur...
574 LYXERR(Debug::RTL, "right edge, moving: " << int(new_cur.pit()) << ","
575 << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
578 // normal movement to the right
581 // Recall, if the cursor is at position 'x', that
582 // means *before* the character at position 'x'. In
583 // RTL, "before" means "to the right of", in LTR, "to
584 // the left of". So currently our situation is this:
585 // the position to our right is 'right_pos' (i.e.,
586 // we're currently to the left of 'right_pos'). In
587 // order to move to the right, it depends whether or
588 // not the character at 'right_pos' is RTL.
589 bool const new_pos_is_RTL = paragraph().getFontSettings(
590 buffer()->params(), right_pos).isVisibleRightToLeft();
591 // If the character at 'right_pos' *is* LTR, then in
592 // order to move to the right of it, we need to be
593 // *after* 'right_pos', i.e., move to position
595 if (!new_pos_is_RTL) {
596 new_cur.pos() = right_pos + 1;
597 // set the boundary to true in two situations:
599 // 1. if new_pos is now lastpos, and we're in
600 // an RTL paragraph (this means that we're
601 // moving right to the end of an LTR chunk
602 // which is at the end of an RTL paragraph);
603 (new_cur.pos() == lastpos()
604 && paragraph().isRTL(buffer()->params()))
605 // 2. if the position *after* right_pos is RTL
606 // (we want to be *after* right_pos, not
607 // before right_pos + 1!)
608 || paragraph().getFontSettings(buffer()->params(),
609 new_cur.pos()).isVisibleRightToLeft()
611 new_cur.boundary(true);
612 else // set the boundary to false
613 new_cur.boundary(false);
615 // Otherwise (if the character at position 'right_pos'
616 // is RTL), then moving to the right of it is as easy
617 // as setting the new position to 'right_pos'.
619 new_cur.pos() = right_pos;
620 new_cur.boundary(false);
625 bool const moved = new_cur != *this || new_cur.boundary() != boundary();
628 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
629 << (new_cur.boundary() ? " (boundary)" : ""));
637 bool Cursor::posVisLeft(bool skip_inset)
639 Cursor new_cur = *this; // where we will move to
640 pos_type left_pos; // position visually left of current cursor
641 pos_type right_pos; // position visually right of current cursor
643 getSurroundingPos(left_pos, right_pos);
645 LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
647 // Are we at an inset?
648 new_cur.pos() = left_pos;
649 new_cur.boundary(false);
651 text()->checkAndActivateInsetVisual(new_cur, left_pos >= pos(), true)) {
652 // we actually move the cursor at the end of this
653 // function, for now we just keep track of the new
654 // position in new_cur...
655 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
658 // Are we already at leftmost pos in row?
659 else if (text()->empty() || left_pos == -1) {
662 if (!new_cur.posVisToNewRow(true)) {
663 LYXERR(Debug::RTL, "not moving!");
667 // we actually move the cursor at the end of this
668 // function, for now just keep track of the new
669 // position in new_cur...
670 LYXERR(Debug::RTL, "left edge, moving: " << int(new_cur.pit()) << ","
671 << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
674 // normal movement to the left
677 // Recall, if the cursor is at position 'x', that
678 // means *before* the character at position 'x'. In
679 // RTL, "before" means "to the right of", in LTR, "to
680 // the left of". So currently our situation is this:
681 // the position to our left is 'left_pos' (i.e., we're
682 // currently to the right of 'left_pos'). In order to
683 // move to the left, it depends whether or not the
684 // character at 'left_pos' is RTL.
685 bool const new_pos_is_RTL = paragraph().getFontSettings(
686 buffer()->params(), left_pos).isVisibleRightToLeft();
687 // If the character at 'left_pos' *is* RTL, then in
688 // order to move to the left of it, we need to be
689 // *after* 'left_pos', i.e., move to position
691 if (new_pos_is_RTL) {
692 new_cur.pos() = left_pos + 1;
693 // set the boundary to true in two situations:
695 // 1. if new_pos is now lastpos and we're in
696 // an LTR paragraph (this means that we're
697 // moving left to the end of an RTL chunk
698 // which is at the end of an LTR paragraph);
699 (new_cur.pos() == lastpos()
700 && !paragraph().isRTL(buffer()->params()))
701 // 2. if the position *after* left_pos is not
702 // RTL (we want to be *after* left_pos, not
703 // before left_pos + 1!)
704 || !paragraph().getFontSettings(buffer()->params(),
705 new_cur.pos()).isVisibleRightToLeft()
707 new_cur.boundary(true);
708 else // set the boundary to false
709 new_cur.boundary(false);
711 // Otherwise (if the character at position 'left_pos'
712 // is LTR), then moving to the left of it is as easy
713 // as setting the new position to 'left_pos'.
715 new_cur.pos() = left_pos;
716 new_cur.boundary(false);
721 bool const moved = new_cur != *this || new_cur.boundary() != boundary();
724 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
725 << (new_cur.boundary() ? " (boundary)" : ""));
735 // Return true on success
736 bool findNonVirtual(Row const & row, Row::const_iterator & cit, bool onleft)
739 while (cit != row.begin() && cit->isVirtual())
742 while (cit != row.end() && cit->isVirtual())
745 return cit != row.end() && !cit->isVirtual();
750 void Cursor::getSurroundingPos(pos_type & left_pos, pos_type & right_pos) const
752 // by default, we know nothing.
756 Row const & row = textRow();
758 Row::const_iterator cit = row.findElement(pos(), boundary(), dummy);
759 // Handle the case of empty row
760 if (cit == row.end()) {
762 right_pos = row.pos();
764 left_pos = row.pos() - 1;
768 // skip virtual elements and exit if no non-virtual one exists
769 if (!findNonVirtual(row, cit, !cit->isRTL()))
772 // if the position is at the left side of the element, we have to
773 // look at the previous element
774 if (pos() == cit->left_pos()) {
775 LYXERR(Debug::RTL, "getSurroundingPos(" << pos() << (boundary() ? "b" : "")
776 << "), AT LEFT of *cit=" << *cit);
777 // this one is easy (see common case below)
778 right_pos = pos() - (cit->isRTL() ? 1 : 0);
779 // at the left of the row
780 if (cit == row.begin())
783 if (!findNonVirtual(row, cit, true))
785 // [...[ is the row element, | is cursor position (! with boundary)
786 // [ 1 2 [ is a ltr row element with pos=1 and endpos=3
787 // ] 2 1] is an rtl row element with pos=1 and endpos=3
788 // [ 1 2 [ [|3 4 [ => (2, 3)
789 // or [ 1 2 [ ]!4 3 ] => (2, 4)
790 // or ] 2 1 ] [|3 4 [ => (1, 3)
791 // or ] 4 3 ] ]!2 1 ] => (3, 2)
792 left_pos = cit->right_pos() - (cit->isRTL() ? 0 : 1);
793 // happens with consecutive row of same direction
794 if (left_pos == right_pos) {
795 left_pos += cit->isRTL() ? 1 : -1;
798 // same code but with the element at the right
799 else if (pos() == cit->right_pos()) {
800 LYXERR(Debug::RTL, "getSurroundingPos(" << pos() << (boundary() ? "b" : "")
801 << "), AT RIGHT of *cit=" << *cit);
802 // this one is easy (see common case below)
803 left_pos = pos() - (cit->isRTL() ? 0 : 1);
804 // at the right of the row
805 if (cit + 1 == row.end())
808 if (!findNonVirtual(row, cit, false))
810 // [ 1 2![ [ 3 4 [ => (2, 3)
811 // or [ 1 2![ ] 4 3 ] => (2, 4)
812 // or ] 2 1|] [ 3 4 [ => (1, 3)
813 // or ] 4 3|] ] 2 1 ] => (3, 2)
814 right_pos = cit->left_pos() - (cit->isRTL() ? 1 : 0);
815 // happens with consecutive row of same direction
816 if (right_pos == left_pos)
817 right_pos += cit->isRTL() ? -1 : 1;
819 // common case: both positions are inside the row element
821 // [ 1 2|3 [ => (2, 3)
822 // or ] 3|2 1 ] => (3, 2)
823 left_pos = pos() - (cit->isRTL() ? 0 : 1);
824 right_pos = pos() - (cit->isRTL() ? 1 : 0);
827 // Note that debug message does not catch all early returns above
828 LYXERR(Debug::RTL,"getSurroundingPos(" << pos() << (boundary() ? "b" : "")
829 << ") => (" << left_pos << ", " << right_pos <<")");
833 bool Cursor::posVisToNewRow(bool movingLeft)
835 Row const & row = textRow();
836 bool par_is_LTR = !row.isRTL();
838 // Inside a table, determining whether to move to the next or
839 // previous row should be done based on the table's direction.
840 if (inset().asInsetTabular()) {
841 par_is_LTR = !inset().asInsetTabular()->isRightToLeft(*this);
842 LYXERR(Debug::RTL, "Inside table! par_is_LTR=" << (par_is_LTR ? 1 : 0));
845 // if moving left in an LTR paragraph or moving right in an
846 // RTL one, move to previous row
847 if (par_is_LTR == movingLeft) {
848 if (row.pos() == 0) { // we're at first row in paragraph
849 if (pit() == 0) // no previous paragraph! don't move
851 // move to last pos in previous par
855 } else { // move to previous row in this par
856 pos() = row.pos() - 1; // this is guaranteed to be in previous row
860 // if moving left in an RTL paragraph or moving right in an
861 // LTR one, move to next row
863 if (row.endpos() == lastpos()) { // we're at last row in paragraph
864 if (pit() == lastpit()) // last paragraph! don't move
866 // move to first row in next par
870 } else { // move to next row in this par
871 pos() = row.endpos();
876 // make sure we're at left-/right-most pos in new row
877 posVisToRowExtremity(!movingLeft);
883 void Cursor::posVisToRowExtremity(bool left)
885 LYXERR(Debug::RTL, "entering extremity: " << pit() << "," << pos() << ","
886 << (boundary() ? 1 : 0));
888 TextMetrics const & tm = bv_->textMetrics(text());
889 // Looking for extremities is like clicking on the left or the
891 int x = tm.origin().x_ + (left ? 0 : textRow().width());
893 pos() = tm.getPosNearX(textRow(), x, b);
896 LYXERR(Debug::RTL, "leaving extremity: " << pit() << "," << pos() << ","
897 << (boundary() ? 1 : 0));
901 bool Cursor::reverseDirectionNeeded() const
904 * We determine the directions based on the direction of the
905 * bottom() --- i.e., outermost --- paragraph, because that is
906 * the only way to achieve consistency of the arrow's movements
907 * within a paragraph, and thus avoid situations in which the
910 return bottom().paragraph().isRTL(bv().buffer().params());
914 CursorSlice Cursor::normalAnchor() const
918 // LASSERT: There have been several bugs around this code, that seem
919 // to involve failures to reset the anchor. We can at least not crash
920 // in release mode by resetting it ourselves.
921 if (anchor_.depth() < depth()) {
922 LYXERR0("Cursor is deeper than anchor. PLEASE REPORT.\nCursor is"
924 const_cast<DocIterator &>(anchor_) = *this;
927 CursorSlice normal = anchor_[depth() - 1];
928 if (depth() < anchor_.depth() && top() <= normal) {
929 // anchor is behind cursor -> move anchor behind the inset
936 CursorSlice Cursor::selBegin() const
940 return normalAnchor() < top() ? normalAnchor() : top();
944 CursorSlice Cursor::selEnd() const
948 return normalAnchor() > top() ? normalAnchor() : top();
952 DocIterator Cursor::selectionBegin() const
958 // FIXME: This is a work-around for the problem that
959 // CursorSlice doesn't keep track of the boundary.
960 if (normalAnchor() == top())
961 di = anchor_.boundary() > boundary() ? anchor_ : *this;
963 di = normalAnchor() < top() ? anchor_ : *this;
969 DocIterator Cursor::selectionEnd() const
975 // FIXME: This is a work-around for the problem that
976 // CursorSlice doesn't keep track of the boundary.
977 if (normalAnchor() == top())
978 di = anchor_.boundary() < boundary() ? anchor_ : *this;
980 di = normalAnchor() > top() ? anchor_ : *this;
982 if (di.depth() > depth()) {
990 void Cursor::setSelection()
993 if (idx() == normalAnchor().idx() &&
994 pit() == normalAnchor().pit() &&
995 pos() == normalAnchor().pos())
1000 void Cursor::setSelection(DocIterator const & where, int n)
1009 void Cursor::clearSelection()
1012 setWordSelection(false);
1018 void Cursor::setTargetX(int x)
1021 textTargetOffset_ = 0;
1025 int Cursor::x_target() const
1031 void Cursor::clearTargetX()
1034 textTargetOffset_ = 0;
1038 void Cursor::updateTextTargetOffset()
1043 textTargetOffset_ = x - x_target_;
1047 void Cursor::info(odocstream & os, bool devel_mode) const
1049 for (int i = 1, n = depth(); i < n; ++i) {
1050 operator[](i).inset().infoize(os);
1054 Inset const * inset = prevInset();
1055 // prevInset() can return 0 in certain case.
1057 prevInset()->infoize2(os);
1060 InsetMath * math = inset().asInsetMath();
1062 os << _(", Inset: ") << math->id();
1063 os << _(", Cell: ") << idx();
1064 os << _(", Position: ") << pos();
1070 bool Cursor::selHandle(bool sel)
1072 //lyxerr << "Cursor::selHandle" << endl;
1075 if (sel == selection())
1079 cap::saveSelection(*this);
1088 ///////////////////////////////////////////////////////////////////
1091 // The part below is the non-integrated rest of the original math
1092 // cursor. This should be either generalized for texted or moved
1093 // back to mathed (in most cases to InsetMathNest).
1095 ///////////////////////////////////////////////////////////////////
1097 #include "mathed/InsetMathChar.h"
1098 #include "mathed/InsetMathGrid.h"
1099 #include "mathed/InsetMathScript.h"
1100 #include "mathed/InsetMathUnknown.h"
1101 #include "mathed/MathFactory.h"
1102 #include "mathed/MathStream.h"
1103 #include "mathed/MathSupport.h"
1108 bool Cursor::isInside(Inset const * p) const
1110 for (size_t i = 0; i != depth(); ++i)
1111 if (&operator[](i).inset() == p)
1117 void Cursor::leaveInset(Inset const & inset)
1119 for (size_t i = 0; i != depth(); ++i) {
1120 if (&operator[](i).inset() == &inset) {
1128 bool Cursor::openable(MathAtom const & t) const
1139 // we can't move into anything new during selection
1140 if (depth() >= anchor_.depth())
1142 if (t.nucleus() != &anchor_[depth()].inset())
1149 void Cursor::setScreenPos(int x, int /*y*/)
1152 //bruteFind(*this, x, y, 0, bv().workWidth(), 0, bv().workHeight());
1157 void Cursor::plainErase()
1159 cell().erase(pos());
1163 void Cursor::markInsert()
1165 insert(char_type(0));
1169 void Cursor::markErase()
1171 cell().erase(pos());
1175 void Cursor::plainInsert(MathAtom const & t)
1177 cell().insert(pos(), t);
1179 inset().setBuffer(bv_->buffer());
1181 checkBufferStructure();
1185 void Cursor::insert(docstring const & str)
1187 for (char_type c : str)
1192 void Cursor::insert(char_type c)
1194 //lyxerr << "Cursor::insert char '" << c << "'" << endl;
1195 LASSERT(!empty(), return);
1197 cap::selClearOrDel(*this);
1198 insert(new InsetMathChar(c));
1200 text()->insertChar(*this, c);
1205 void Cursor::insert(MathAtom const & t)
1207 //lyxerr << "Cursor::insert MathAtom '" << t << "'" << endl;
1209 cap::selClearOrDel(*this);
1214 void Cursor::insert(Inset * inset0)
1216 LASSERT(inset0, return);
1218 insert(MathAtom(inset0->asInsetMath()));
1220 text()->insertInset(*this, inset0);
1221 inset0->setBuffer(bv_->buffer());
1223 if (inset0->isLabeled())
1224 forceBufferUpdate();
1229 int Cursor::niceInsert(docstring const & t, Parse::flags f, bool enter)
1231 MathData ar(buffer());
1233 if (ar.size() == 1 && (enter || selection()))
1241 void Cursor::niceInsert(MathAtom const & t)
1244 docstring const safe = cap::grabAndEraseSelection(*this);
1246 // If possible, enter the new inset and move the contents of the selection
1247 if (t->isActive()) {
1249 // be careful here: don't use 'pushBackward(t)' as this we need to
1250 // push the clone, not the original
1251 pushBackward(*nextInset());
1252 // We may not use niceInsert here (recursion)
1253 MathData ar(buffer());
1256 } else if (t->asMacro() && !safe.empty()) {
1257 MathData ar(buffer());
1259 docstring const name = t->asMacro()->name();
1260 MacroData const * data = buffer()->getMacro(name);
1261 if (data && data->numargs() - data->optionals() > 0) {
1262 plainInsert(MathAtom(new InsetMathBrace(ar)));
1269 void Cursor::insert(MathData const & ar)
1273 cap::eraseSelection(*this);
1274 cell().insert(pos(), ar);
1276 // FIXME audit setBuffer calls
1277 inset().setBuffer(bv_->buffer());
1281 bool Cursor::backspace(bool const force)
1284 cap::eraseSelection(*this);
1289 // If empty cell, and not part of a big cell
1290 if (lastpos() == 0 && inset().nargs() == 1) {
1292 // Directly delete empty cell: [|[]] => [|]
1298 // [|], can not delete from inside
1309 if (inMacroMode()) {
1310 InsetMathUnknown * p = activeMacro();
1311 if (p->name().size() > 1) {
1312 p->setName(p->name().substr(0, p->name().size() - 1));
1317 if (pos() != 0 && !force && prevAtom()->confirmDeletion()) {
1318 // let's require two backspaces for 'big stuff' and
1319 // highlight on the first
1331 bool Cursor::erase(bool const force)
1337 cap::eraseSelection(*this);
1341 // delete empty cells if possible
1342 if (pos() == lastpos() && inset().idxDelete(idx()))
1345 // special behaviour when in last position of cell
1346 if (pos() == lastpos()) {
1347 bool one_cell = inset().nargs() == 1;
1348 if (one_cell && lastpos() == 0) {
1350 // Directly delete empty cell: [|[]] => [|]
1356 // [|], can not delete from inside
1361 inset().idxGlue(idx());
1365 // 'clever' UI hack: only erase large items if previously slected
1366 if (pos() != lastpos() && !force && nextAtom()->confirmDeletion()) {
1381 DocIterator save = *this;
1382 FuncRequest cmd(selection() ? LFUN_UP_SELECT : LFUN_UP, docstring());
1383 this->dispatch(cmd);
1384 if (disp_.dispatched())
1394 DocIterator save = *this;
1395 FuncRequest cmd(selection() ? LFUN_DOWN_SELECT : LFUN_DOWN, docstring());
1396 this->dispatch(cmd);
1397 if (disp_.dispatched())
1404 bool Cursor::macroModeClose()
1408 InsetMathUnknown * p = activeMacro();
1410 MathData selection(buffer());
1411 asArray(p->selection(), selection);
1412 docstring const s = p->name();
1414 cell().erase(pos());
1416 // do nothing if the macro name is empty
1420 // trigger updates of macros, at least, if no full
1421 // updates take place anyway
1422 screenUpdateFlags(Update::Force);
1424 docstring const name = s.substr(1);
1425 InsetMathNest * const in = inset().asInsetMath()->asNestInset();
1426 if (in && in->interpretString(*this, s))
1428 bool const user_macro = buffer()->getMacro(name, *this, false);
1429 MathAtom atom = user_macro ? MathAtom(new InsetMathMacro(buffer(), name))
1430 : createInsetMath(name, buffer());
1432 // try to put argument into macro, if we just inserted a macro
1433 bool macroArg = false;
1434 InsetMathMacro * atomAsMacro = atom.nucleus()->asMacro();
1436 // macros here are still unfolded (in init mode in fact). So
1437 // we have to resolve the macro here manually and check its arity
1438 // to put the selection behind it if arity > 0.
1439 MacroData const * data = buffer()->getMacro(atomAsMacro->name());
1440 if (!selection.empty() && data && data->numargs() - data->optionals() > 0) {
1442 atomAsMacro->setDisplayMode(InsetMathMacro::DISPLAY_INTERACTIVE_INIT, 1);
1444 // non-greedy case. Do not touch the arguments behind
1445 atomAsMacro->setDisplayMode(InsetMathMacro::DISPLAY_INTERACTIVE_INIT, 0);
1448 // insert remembered selection into first argument of a non-macro
1449 else if (atom.nucleus()->nargs() > 0)
1450 atom.nucleus()->cell(0).append(selection);
1452 MathWordList const & words = mathedWordList();
1453 MathWordList::const_iterator it = words.find(name);
1454 bool keep_mathmode = it != words.end() && (it->second.inset == "font"
1455 || it->second.inset == "oldfont"
1456 || it->second.inset == "mbox");
1457 bool ert_macro = !user_macro && it == words.end() && atomAsMacro;
1459 if (in && in->currentMode() == Inset::TEXT_MODE
1460 && atom.nucleus()->currentMode() == Inset::MATH_MODE
1461 && name != from_ascii("ensuremath") && !ert_macro) {
1462 MathAtom at(new InsetMathEnsureMath(buffer()));
1463 at.nucleus()->cell(0).push_back(atom);
1466 } else if (in && in->currentMode() == Inset::MATH_MODE
1467 && atom.nucleus()->currentMode() == Inset::TEXT_MODE
1468 && !keep_mathmode) {
1469 MathAtom at = createInsetMath("text", buffer());
1470 at.nucleus()->cell(0).push_back(atom);
1476 // finally put the macro argument behind, if needed
1478 if (selection.size() > 1 || selection[0]->asScriptInset())
1479 plainInsert(MathAtom(new InsetMathBrace(selection)));
1488 docstring Cursor::macroName()
1490 return inMacroMode() ? activeMacro()->name() : docstring();
1494 void Cursor::handleNest(MathAtom const & a, int c)
1496 //lyxerr << "Cursor::handleNest: " << c << endl;
1498 asArray(cap::grabAndEraseSelection(*this), t.nucleus()->cell(c));
1501 pushBackward(*nextInset());
1505 int Cursor::targetX() const
1507 if (x_target() != -1)
1516 int Cursor::textTargetOffset() const
1518 return textTargetOffset_;
1522 void Cursor::setTargetX()
1531 bool Cursor::inMacroMode() const
1535 if (pos() == 0 || cell().empty())
1537 InsetMathUnknown const * p = prevAtom()->asUnknownInset();
1538 return p && !p->final();
1542 InsetMathUnknown * Cursor::activeMacro()
1544 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1548 InsetMathUnknown const * Cursor::activeMacro() const
1550 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1554 void Cursor::pullArg()
1557 MathData ar = cell();
1558 if (popBackward() && inMathed()) {
1560 cell().insert(pos(), ar);
1563 //formula()->mutateToText();
1568 void Cursor::touch()
1572 DocIterator::const_iterator it = begin();
1573 DocIterator::const_iterator et = end();
1574 for ( ; it != et; ++it)
1580 void Cursor::normalize()
1582 if (idx() > lastidx()) {
1583 lyxerr << "this should not really happen - 1: "
1584 << idx() << ' ' << nargs()
1585 << " in: " << &inset() << endl;
1589 if (pos() > lastpos()) {
1590 lyxerr << "this should not really happen - 2: "
1591 << pos() << ' ' << lastpos() << " in idx: " << idx()
1593 odocstringstream os;
1594 otexrowstream ots(os);
1595 WriteStream wi(ots, false, true, WriteStream::wsDefault);
1596 inset().asInsetMath()->write(wi);
1597 lyxerr << to_utf8(os.str()) << endl;
1603 bool Cursor::upDownInMath(bool up)
1605 // Be warned: The 'logic' implemented in this function is highly
1606 // fragile. A distance of one pixel or a '<' vs '<=' _really
1607 // matters. So fiddle around with it only if you think you know
1608 // what you are doing!
1612 xo = beforeDispatchPosX_;
1614 // check if we had something else in mind, if not, this is the future
1616 if (x_target_ == -1)
1618 else if (inset().asInsetText() && xo - textTargetOffset() != x_target()) {
1619 // In text mode inside the line (not left or right) possibly set a new target_x,
1620 // but only if we are somewhere else than the previous target-offset.
1622 // We want to keep the x-target on subsequent up/down movements
1623 // that cross beyond the end of short lines. Thus a special
1624 // handling when the cursor is at the end of line: Use the new
1625 // x-target only if the old one was before the end of line
1626 // or the old one was after the beginning of the line
1627 bool inRTL = innerParagraph().isRTL(bv().buffer().params());
1631 left = pos() == textRow().endpos();
1632 right = pos() == textRow().pos();
1634 left = pos() == textRow().pos();
1635 right = pos() == textRow().endpos();
1637 if ((!left && !right) ||
1638 (left && !right && xo < x_target_) ||
1639 (!left && right && x_target_ < xo))
1646 // try neigbouring script insets
1648 if (inMathed() && !selection()) {
1651 InsetMathScript const * p = prevAtom()->asScriptInset();
1652 if (p && p->has(up)) {
1654 push(*const_cast<InsetMathScript*>(p));
1655 idx() = p->idxOfScript(up);
1658 // we went in the right direction? Otherwise don't jump into the script
1662 int oy = beforeDispatchPosY_;
1663 if ((!up && y <= oy) ||
1672 if (pos() != lastpos()) {
1673 InsetMathScript const * p = nextAtom()->asScriptInset();
1674 if (p && p->has(up)) {
1675 push(*const_cast<InsetMathScript*>(p));
1676 idx() = p->idxOfScript(up);
1679 // we went in the right direction? Otherwise don't jump into the script
1683 int oy = beforeDispatchPosY_;
1684 if ((!up && y <= oy) ||
1693 // try to find an inset that knows better then we,
1694 if (inset().idxUpDown(*this, up)) {
1695 //lyxerr << "idxUpDown triggered" << endl;
1696 // try to find best position within this inset
1698 setCursor(bruteFind(*this, xo, yo));
1702 // any improvement going just out of inset?
1703 if (popBackward() && inMathed()) {
1704 //lyxerr << "updown: popBackward succeeded" << endl;
1707 int yold = beforeDispatchPosY_;
1709 if (up ? ynew < yold : ynew > yold)
1713 // no success, we are probably at the document top or bottom
1719 InsetMath & Cursor::nextMath()
1721 return *nextAtom().nucleus();
1725 InsetMath & Cursor::prevMath()
1727 return *prevAtom().nucleus();
1731 bool Cursor::mathForward(bool word)
1733 LASSERT(inMathed(), return false);
1734 if (pos() < lastpos()) {
1736 // word: skip a group of insets of the form X*(B*|R*|P*) (greedy
1737 // match) where X is any math class, B is mathbin, R is mathrel, and
1738 // P is mathpunct. Make sure that the following remains true:
1739 // mathForward(true); mathBackward(true); mathForward(true)
1740 // is the same as mathForward(true) and
1741 // mathBackward(true); mathForward(true); mathBackward(true)
1742 // is the same as mathBackward(true).
1743 MathClass mc = nextMath().mathClass();
1746 while (pos() < lastpos() && mc == nextMath().mathClass());
1747 if (pos() < lastpos() &&
1748 ((mc = nextMath().mathClass()) == MC_BIN ||
1749 mc == MC_REL || mc == MC_PUNCT))
1752 while (pos() < lastpos() && mc == nextMath().mathClass());
1753 } else if (openable(nextAtom())) {
1754 // single step: try to enter the next inset
1755 pushBackward(nextMath());
1756 inset().idxFirst(*this);
1761 if (inset().idxForward(*this))
1763 // try to pop forwards --- but don't pop out of math! leave that to
1765 int s = depth() - 2;
1766 if (s >= 0 && operator[](s).inset().asInsetMath())
1767 return popForward();
1772 bool Cursor::mathBackward(bool word)
1774 LASSERT(inMathed(), return false);
1777 // word: skip a group of insets. See the comment in mathForward.
1778 MathClass mc = prevMath().mathClass();
1781 while (pos() > 0 && mc == prevMath().mathClass());
1782 if (pos() > 0 && (mc == MC_BIN || mc == MC_REL || mc == MC_PUNCT)) {
1783 mc = prevMath().mathClass();
1786 while (pos() > 0 && mc == prevMath().mathClass());
1788 } else if (openable(prevAtom())) {
1789 // single step: try to enter the preceding inset
1792 inset().idxLast(*this);
1797 if (inset().idxBackward(*this))
1799 // try to pop backwards --- but don't pop out of math! leave that to
1801 int s = depth() - 2;
1802 if (s >= 0 && operator[](s).inset().asInsetMath())
1803 return popBackward();
1808 bool Cursor::atFirstOrLastRow(bool up)
1810 TextMetrics const & tm = bv_->textMetrics(text());
1811 ParagraphMetrics const & pm = tm.parMetrics(pit());
1814 if (pos() && boundary())
1815 row = pm.pos2row(pos() - 1);
1817 row = pm.pos2row(pos());
1820 if (pit() == 0 && row == 0)
1823 if (pit() + 1 >= int(text()->paragraphs().size()) &&
1824 row + 1 >= int(pm.rows().size()))
1831 bool Cursor::upDownInText(bool up, bool & updateNeeded)
1833 LASSERT(text(), return false);
1839 xo = beforeDispatchPosX_;
1841 // update the targetX - this is here before the "return false"
1842 // to set a new target which can be used by InsetTexts above
1843 // if we cannot move up/down inside this inset anymore
1844 if (x_target_ == -1)
1846 else if (xo - textTargetOffset() != x_target() &&
1847 depth() == beforeDispatchCursor_.depth()) {
1848 // In text mode inside the line (not left or right)
1849 // possibly set a new target_x, but only if we are
1850 // somewhere else than the previous target-offset.
1852 // We want to keep the x-target on subsequent up/down
1853 // movements that cross beyond the end of short lines.
1854 // Thus a special handling when the cursor is at the
1855 // end of line: Use the new x-target only if the old
1856 // one was before the end of line or the old one was
1857 // after the beginning of the line
1858 bool inRTL = innerParagraph().isRTL(bv().buffer().params());
1862 left = pos() == textRow().endpos();
1863 right = pos() == textRow().pos();
1865 left = pos() == textRow().pos();
1866 right = pos() == textRow().endpos();
1868 if ((!left && !right) ||
1869 (left && !right && xo < x_target_) ||
1870 (!left && right && x_target_ < xo))
1877 // first get the current line
1878 TextMetrics & tm = bv_->textMetrics(text());
1879 ParagraphMetrics const & pm = tm.parMetrics(pit());
1881 if (pos() && boundary())
1882 row = pm.pos2row(pos() - 1);
1884 row = pm.pos2row(pos());
1886 if (atFirstOrLastRow(up)) {
1887 // Is there a place for the cursor to go ? If yes, we
1888 // can execute the DEPM, otherwise we should keep the
1889 // paragraph to host the cursor.
1890 Cursor dummy = *this;
1891 bool valid_destination = false;
1892 for(; dummy.depth(); dummy.pop())
1893 if (!dummy.atFirstOrLastRow(up)) {
1894 valid_destination = true;
1898 // will a next dispatch follow and if there is a new
1899 // dispatch will it move the cursor out ?
1900 if (depth() > 1 && valid_destination) {
1901 // The cursor hasn't changed yet. This happens when
1902 // you e.g. move out of an inset. And to give the
1903 // DEPM the possibility of doing something we must
1904 // provide it with two different cursors. (Lgb, vfr)
1906 dummy.pos() = dummy.pos() == 0 ? dummy.lastpos() : 0;
1907 dummy.pit() = dummy.pit() == 0 ? dummy.lastpit() : 0;
1909 updateNeeded |= bv().checkDepm(dummy, *this);
1910 updateTextTargetOffset();
1912 forceBufferUpdate();
1917 // with and without selection are handled differently
1919 int yo = bv().getPos(*this).y_;
1921 // To next/previous row
1922 // FIXME: the y position is often guessed wrongly across styles and
1923 // insets, which leads to weird behaviour.
1925 tm.editXY(*this, xo, yo - textRow().ascent() - 1);
1927 tm.editXY(*this, xo, yo + textRow().descent() + 1);
1928 x_target_ = old.x_target_;
1931 // This happens when you move out of an inset.
1932 // And to give the DEPM the possibility of doing
1933 // something we must provide it with two different
1935 Cursor dummy = *this;
1938 if (bv().checkDepm(dummy, old)) {
1939 updateNeeded = true;
1940 // Make sure that cur gets back whatever happened to dummy (Lgb)
1943 if (inTexted() && pos() && paragraph().isEnvSeparator(pos() - 1))
1946 // if there is a selection, we stay out of any inset,
1947 // and just jump to the right position:
1953 } else if (pit() > 0) {
1955 TextMetrics & tm = bv_->textMetrics(text());
1956 if (!tm.contains(pit()))
1957 tm.newParMetricsUp();
1958 ParagraphMetrics const & pmcur = tm.parMetrics(pit());
1959 next_row = pmcur.rows().size() - 1;
1962 if (row + 1 < int(pm.rows().size())) {
1964 } else if (pit() + 1 < int(text()->paragraphs().size())) {
1966 TextMetrics & tm = bv_->textMetrics(text());
1967 if (!tm.contains(pit()))
1968 tm.newParMetricsDown();
1973 Row const & real_next_row = tm.parMetrics(pit()).rows()[next_row];
1975 top().pos() = tm.getPosNearX(real_next_row, xo, bound);
1977 // When selection==false, this is done by TextMetrics::editXY
1980 updateNeeded |= bv().checkDepm(*this, old);
1984 forceBufferUpdate();
1985 updateTextTargetOffset();
1990 void Cursor::handleFont(string const & font)
1992 LYXERR(Debug::DEBUG, font);
1996 safe = cap::grabAndEraseSelection(*this);
2001 if (lastpos() != 0) {
2002 // something left in the cell
2004 // cursor in first position
2006 } else if (pos() == lastpos()) {
2007 // cursor in last position
2010 // cursor in between. split cell
2011 MathData::iterator bt = cell().begin();
2012 MathAtom at = createInsetMath(from_utf8(font), buffer());
2013 at.nucleus()->cell(0) = MathData(buffer(), bt, bt + pos());
2014 cell().erase(bt, bt + pos());
2019 // nothing left in the cell
2028 void Cursor::message(docstring const & msg) const
2030 disp_.setMessage(msg);
2034 void Cursor::errorMessage(docstring const & msg) const
2036 disp_.setMessage(msg);
2037 disp_.setError(true);
2043 docstring parbreak(Cursor const * cur)
2045 odocstringstream os;
2047 // only add blank line if we're not in a ParbreakIsNewline situation
2048 if (!cur->inset().getLayout().parbreakIsNewline()
2049 && !cur->paragraph().layout().parbreak_is_newline)
2057 docstring Cursor::selectionAsString(bool with_label) const
2063 return cap::grabSelection(*this);
2065 int const label = with_label
2066 ? AS_STR_LABEL | AS_STR_INSETS : AS_STR_INSETS;
2068 idx_type const startidx = selBegin().idx();
2069 idx_type const endidx = selEnd().idx();
2070 if (startidx != endidx) {
2071 // multicell selection
2072 InsetTabular * table = inset().asInsetTabular();
2073 LASSERT(table, return docstring());
2074 return table->asString(startidx, endidx);
2077 ParagraphList const & pars = text()->paragraphs();
2079 pit_type const startpit = selBegin().pit();
2080 pit_type const endpit = selEnd().pit();
2081 size_t const startpos = selBegin().pos();
2082 size_t const endpos = selEnd().pos();
2084 if (startpit == endpit)
2085 return pars[startpit].asString(startpos, endpos, label);
2087 // First paragraph in selection
2088 docstring result = pars[startpit].
2089 asString(startpos, pars[startpit].size(), label)
2092 // The paragraphs in between (if any)
2093 for (pit_type pit = startpit + 1; pit != endpit; ++pit) {
2094 Paragraph const & par = pars[pit];
2095 result += par.asString(0, par.size(), label)
2099 // Last paragraph in selection
2100 result += pars[endpit].asString(0, endpos, label);
2106 docstring Cursor::currentState(bool devel_mode) const
2109 odocstringstream os;
2110 info(os, devel_mode);
2115 return text()->currentState(*this, devel_mode);
2121 docstring Cursor::getPossibleLabel() const
2123 return inMathed() ? from_ascii("eq:") : text()->getPossibleLabel(*this);
2127 Encoding const * Cursor::getEncoding() const
2131 BufferParams const & bp = bv().buffer().params();
2132 if (bp.useNonTeXFonts)
2133 return encodings.fromLyXName("utf8-plain");
2135 CursorSlice const & sl = innerTextSlice();
2136 Text const & text = *sl.text();
2137 Font font = text.getPar(sl.pit()).getFont(bp, sl.pos(),
2138 text.outerFont(sl.pit()));
2139 return font.language()->encoding();
2143 void Cursor::undispatched() const
2145 disp_.dispatched(false);
2149 void Cursor::dispatched() const
2151 disp_.dispatched(true);
2155 void Cursor::screenUpdateFlags(Update::flags f) const
2157 disp_.screenUpdate(f);
2161 void Cursor::forceBufferUpdate() const
2163 disp_.forceBufferUpdate();
2167 void Cursor::clearBufferUpdate() const
2169 disp_.clearBufferUpdate();
2173 bool Cursor::needBufferUpdate() const
2175 return disp_.needBufferUpdate();
2179 void Cursor::noScreenUpdate() const
2181 disp_.screenUpdate(Update::None);
2185 Font Cursor::getFont() const
2187 // The logic here should more or less match to the
2188 // Cursor::setCurrentFont logic, i.e. the cursor height should
2189 // give a hint what will happen if a character is entered.
2190 // FIXME: this is not the case, what about removing this method ? (see #10478).
2192 // HACK. far from being perfect...
2194 CursorSlice const & sl = innerTextSlice();
2195 Text const & text = *sl.text();
2196 Paragraph const & par = text.getPar(sl.pit());
2198 // on boundary, so we are really at the character before
2199 pos_type pos = sl.pos();
2200 if (pos > 0 && boundary())
2203 // on space? Take the font before (only for RTL boundary stay)
2205 TextMetrics const & tm = bv().textMetrics(&text);
2206 if (pos == sl.lastpos()
2207 || (par.isSeparator(pos)
2208 && !tm.isRTLBoundary(sl.pit(), pos)))
2212 // get font at the position
2213 Font font = par.getFont(buffer()->params(), pos,
2214 text.outerFont(sl.pit()));
2220 bool Cursor::fixIfBroken()
2222 bool const broken_cursor = DocIterator::fixIfBroken();
2223 bool const broken_anchor = anchor_.fixIfBroken();
2225 if (broken_cursor || broken_anchor) {
2226 clearNewWordPosition();
2234 void Cursor::sanitize()
2236 setBuffer(&bv_->buffer());
2237 DocIterator::sanitize();
2238 new_word_.sanitize();
2246 bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur)
2248 // find inset in common
2250 for (i = 0; i < old.depth() && i < cur.depth(); ++i) {
2251 if (&old[i].inset() != &cur[i].inset())
2255 // update words if we just moved to another paragraph
2256 if (i == old.depth() && i == cur.depth()
2257 && !cur.buffer()->isClean()
2258 && cur.inTexted() && old.inTexted()
2259 && cur.pit() != old.pit()) {
2260 old.paragraph().updateWords();
2263 // notify everything on top of the common part in old cursor,
2264 // but stop if the inset claims the cursor to be invalid now
2265 for (size_type j = i; j < old.depth(); ++j) {
2266 Cursor inset_pos = old;
2267 inset_pos.cutOff(j);
2268 if (old[j].inset().notifyCursorLeaves(inset_pos, cur))
2272 // notify everything on top of the common part in new cursor,
2273 // but stop if the inset claims the cursor to be invalid now
2274 for (; i < cur.depth(); ++i) {
2275 if (cur[i].inset().notifyCursorEnters(cur))
2283 void Cursor::setCurrentFont()
2285 CursorSlice const & cs = innerTextSlice();
2286 Paragraph const & par = cs.paragraph();
2287 pos_type cpit = cs.pit();
2288 pos_type cpos = cs.pos();
2289 Text const & ctext = *cs.text();
2290 TextMetrics const & tm = bv().textMetrics(&ctext);
2292 // are we behind previous char in fact? -> go to that char
2293 if (cpos > 0 && boundary())
2296 // find position to take the font from
2298 // paragraph end? -> font of last char
2299 if (cpos == lastpos())
2301 // on space? -> look at the words in front of space
2302 else if (cpos > 0 && par.isSeparator(cpos)) {
2303 // abc| def -> font of c
2304 // abc |[WERBEH], i.e. boundary==true -> font of c
2305 // abc [WERBEH]| def, font of the space
2306 if (!tm.isRTLBoundary(cpit, cpos))
2312 BufferParams const & bufparams = buffer()->params();
2313 current_font = par.getFontSettings(bufparams, cpos);
2314 real_current_font = tm.displayFont(cpit, cpos);
2316 // special case for paragraph end
2317 if (cs.pos() == lastpos()
2318 && tm.isRTLBoundary(cpit, cs.pos())
2320 Language const * lang = par.getParLanguage(bufparams);
2321 current_font.setLanguage(lang);
2322 current_font.fontInfo().setNumber(FONT_OFF);
2323 real_current_font.setLanguage(lang);
2324 real_current_font.fontInfo().setNumber(FONT_OFF);
2329 bool Cursor::textUndo()
2331 if (!buffer()->undo().textUndo(*this))
2338 bool Cursor::textRedo()
2340 if (!buffer()->undo().textRedo(*this))
2347 void Cursor::finishUndo() const
2349 buffer()->undo().finishUndo();
2353 void Cursor::beginUndoGroup() const
2355 buffer()->undo().beginUndoGroup(*this);
2359 void Cursor::endUndoGroup() const
2361 buffer()->undo().endUndoGroup(*this);
2365 void Cursor::recordUndo(pit_type from, pit_type to) const
2367 buffer()->undo().recordUndo(*this, from, to);
2371 void Cursor::recordUndo(pit_type from) const
2373 buffer()->undo().recordUndo(*this, from, pit());
2377 void Cursor::recordUndo(UndoKind kind) const
2379 buffer()->undo().recordUndo(*this, kind);
2383 void Cursor::recordUndoInset(Inset const * in) const
2385 buffer()->undo().recordUndoInset(*this, in);
2389 void Cursor::recordUndoFullBuffer() const
2391 buffer()->undo().recordUndoFullBuffer(*this);
2395 void Cursor::recordUndoBufferParams() const
2397 buffer()->undo().recordUndoBufferParams(*this);
2401 void Cursor::recordUndoSelection() const
2404 if (cap::multipleCellsSelected(*this))
2409 buffer()->undo().recordUndo(*this,
2410 selBegin().pit(), selEnd().pit());
2415 void Cursor::checkBufferStructure()
2417 Buffer const * master = buffer()->masterBuffer();
2418 master->tocBackend().updateItem(*this);
2419 if (master != buffer() && !master->hasGuiDelegate())
2420 // In case the master has no gui associated with it,
2421 // the TocItem is not updated (part of bug 5699).
2422 buffer()->tocBackend().updateItem(*this);
2424 // If the last tracked change of the paragraph has just been
2425 // deleted, then we need to recompute the buffer flag
2426 // tracked_changes_present_.
2427 if (inTexted() && paragraph().isChangeUpdateRequired())
2428 disp_.forceChangesUpdate();
2432 bool Cursor::confirmDeletion(bool const before) const
2435 if (Inset const * inset = before ? prevInset() : nextInset())
2436 return inset->confirmDeletion();
2438 DocIterator dit = selectionBegin();
2439 CursorSlice const end = selectionEnd().top();
2440 for (; dit.top() < end; dit.top().forwardPos())
2441 if (Inset const * inset = dit.nextInset())
2442 if (inset->confirmDeletion())
2449 void Cursor::moveToClosestEdge(int const x, bool const edit)
2451 if (Inset const * inset = nextInset()) {
2452 // stay in front of insets for which we want to open the dialog
2453 // (e.g. InsetMathSpace).
2454 if (edit && (inset->hasSettings() || !inset->contextMenuName().empty()))
2456 CoordCache::Insets const & insetCache = bv().coordCache().getInsets();
2457 if (!insetCache.has(inset))
2459 int const wid = insetCache.dim(inset).wid;
2460 Point p = insetCache.xy(inset);
2461 if (x > p.x_ + (wid + 1) / 2)