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.
19 #include "BufferView.h"
20 #include "CoordCache.h"
22 #include "CutAndPaste.h"
23 #include "DispatchResult.h"
27 #include "FuncRequest.h"
29 #include "LyXFunc.h" // only for setMessage()
31 #include "paragraph_funcs.h"
32 #include "Paragraph.h"
33 #include "ParIterator.h"
36 #include "TextMetrics.h"
37 #include "TocBackend.h"
39 #include "support/lassert.h"
40 #include "support/debug.h"
41 #include "support/docstream.h"
43 #include "insets/InsetTabular.h"
44 #include "insets/InsetText.h"
46 #include "mathed/InsetMath.h"
47 #include "mathed/InsetMathBrace.h"
48 #include "mathed/InsetMathScript.h"
49 #include "mathed/MacroTable.h"
50 #include "mathed/MathData.h"
51 #include "mathed/MathMacro.h"
53 #include <boost/bind.hpp>
65 bool positionable(DocIterator const & cursor, DocIterator const & anchor)
67 // avoid deeper nested insets when selecting
68 if (cursor.depth() > anchor.depth())
71 // anchor might be deeper, should have same path then
72 for (size_t i = 0; i < cursor.depth(); ++i)
73 if (&cursor[i].inset() != &anchor[i].inset())
76 // position should be ok.
81 // Find position closest to (x, y) in cell given by iter.
82 // Used only in mathed
83 DocIterator bruteFind2(Cursor const & c, int x, int y)
85 double best_dist = numeric_limits<double>::max();
92 et.top().pos() = et.top().asInsetMath()->cell(et.top().idx()).size();
93 for (size_t i = 0;; ++i) {
96 Inset const * inset = &it.inset();
97 map<Inset const *, Geometry> const & data =
98 c.bv().coordCache().getInsets().getData();
99 map<Inset const *, Geometry>::const_iterator I = data.find(inset);
101 // FIXME: in the case where the inset is not in the cache, this
102 // means that no part of it is visible on screen. In this case
103 // we don't do elaborate search and we just return the forwarded
104 // DocIterator at its beginning.
105 if (I == data.end()) {
110 Point o = I->second.pos;
111 inset->cursorPos(c.bv(), it.top(), c.boundary(), xo, yo);
112 // Convert to absolute
115 double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
116 // '<=' in order to take the last possible position
117 // this is important for clicking behind \sum in e.g. '\sum_i a'
118 LYXERR(Debug::DEBUG, "i: " << i << " d: " << d
119 << " best: " << best_dist);
120 if (d <= best_dist) {
133 /// moves position closest to (x, y) in given box
134 bool bruteFind(Cursor & cursor,
135 int x, int y, int xlow, int xhigh, int ylow, int yhigh)
137 LASSERT(!cursor.empty(), return false);
138 Inset & inset = cursor[0].inset();
139 BufferView & bv = cursor.bv();
141 CoordCache::InnerParPosCache const & cache =
142 bv.coordCache().getParPos().find(cursor.bottom().text())->second;
143 // Get an iterator on the first paragraph in the cache
144 DocIterator it(inset);
145 it.push_back(CursorSlice(inset));
146 it.pit() = cache.begin()->first;
147 // Get an iterator after the last paragraph in the cache
148 DocIterator et(inset);
149 et.push_back(CursorSlice(inset));
150 et.pit() = boost::prior(cache.end())->first;
151 if (et.pit() >= et.lastpit())
152 et = doc_iterator_end(inset);
156 double best_dist = numeric_limits<double>::max();;
157 DocIterator best_cursor = et;
159 for ( ; it != et; it.forwardPos(true)) {
160 // avoid invalid nesting when selecting
161 if (!cursor.selection() || positionable(it, cursor.anchor_)) {
162 Point p = bv.getPos(it, false);
165 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
166 double const dx = xo - x;
167 double const dy = yo - y;
168 double const d = dx * dx + dy * dy;
169 // '<=' in order to take the last possible position
170 // this is important for clicking behind \sum in e.g. '\sum_i a'
171 if (d <= best_dist) {
172 // lyxerr << "*" << endl;
180 if (best_cursor != et) {
181 cursor.setCursor(best_cursor);
190 /// moves position closest to (x, y) in given box
191 bool bruteFind3(Cursor & cur, int x, int y, bool up)
193 BufferView & bv = cur.bv();
194 int ylow = up ? 0 : y + 1;
195 int yhigh = up ? y - 1 : bv.workHeight();
197 int xhigh = bv.workWidth();
199 // FIXME: bit more work needed to get 'from' and 'to' right.
200 pit_type from = cur.bottom().pit();
201 //pit_type to = cur.bottom().pit();
202 //lyxerr << "Pit start: " << from << endl;
204 //lyxerr << "bruteFind3: x: " << x << " y: " << y
205 // << " xlow: " << xlow << " xhigh: " << xhigh
206 // << " ylow: " << ylow << " yhigh: " << yhigh
208 DocIterator it = doc_iterator_begin(cur.buffer());
210 DocIterator et = doc_iterator_end(cur.buffer());
212 double best_dist = numeric_limits<double>::max();
213 DocIterator best_cursor = et;
215 for ( ; it != et; it.forwardPos()) {
216 // avoid invalid nesting when selecting
217 if (bv.cursorStatus(it) == CUR_INSIDE
218 && (!cur.selection() || positionable(it, cur.anchor_))) {
219 Point p = bv.getPos(it, false);
222 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
223 double const dx = xo - x;
224 double const dy = yo - y;
225 double const d = dx * dx + dy * dy;
226 //lyxerr << "itx: " << xo << " ity: " << yo << " d: " << d
227 // << " dx: " << dx << " dy: " << dy
228 // << " idx: " << it.idx() << " pos: " << it.pos()
231 // '<=' in order to take the last possible position
232 // this is important for clicking behind \sum in e.g. '\sum_i a'
233 if (d <= best_dist) {
234 //lyxerr << "*" << endl;
242 //lyxerr << "best_dist: " << best_dist << " cur:\n" << best_cursor << endl;
243 if (best_cursor == et)
245 cur.setCursor(best_cursor);
249 docstring parbreak(Paragraph const & par)
253 // only add blank line if we're not in an ERT or Listings inset
254 if (par.ownerCode() != ERT_CODE && par.ownerCode() != LISTINGS_CODE)
262 // be careful: this is called from the bv's constructor, too, so
263 // bv functions are not yet available!
264 Cursor::Cursor(BufferView & bv)
265 : DocIterator(&bv.buffer()), bv_(&bv), anchor_(),
266 x_target_(-1), textTargetOffset_(0),
267 selection_(false), mark_(false), logicalpos_(false),
268 current_font(inherit_font)
272 void Cursor::reset(Inset & inset)
275 push_back(CursorSlice(inset));
276 anchor_ = doc_iterator_begin(&inset.buffer(), &inset);
284 // this (intentionally) does neither touch anchor nor selection status
285 void Cursor::setCursor(DocIterator const & cur)
287 DocIterator::operator=(cur);
291 void Cursor::dispatch(FuncRequest const & cmd0)
293 LYXERR(Debug::DEBUG, "cmd: " << cmd0 << '\n' << *this);
298 FuncRequest cmd = cmd0;
301 // store some values to be used inside of the handlers
302 beforeDispatchCursor_ = *this;
303 for (; depth(); pop(), boundary(false)) {
304 LYXERR(Debug::DEBUG, "Cursor::dispatch: cmd: "
305 << cmd0 << endl << *this);
306 LASSERT(pos() <= lastpos(), /**/);
307 LASSERT(idx() <= lastidx(), /**/);
308 LASSERT(pit() <= lastpit(), /**/);
310 // The common case is 'LFUN handled, need update', so make the
311 // LFUN handler's life easier by assuming this as default value.
312 // The handler can reset the update and val flags if necessary.
313 disp_.update(Update::FitCursor | Update::Force);
314 disp_.dispatched(true);
315 inset().dispatch(*this, cmd);
316 if (disp_.dispatched())
320 // it completely to get a 'bomb early' behaviour in case this
321 // object will be used again.
322 if (!disp_.dispatched()) {
323 LYXERR(Debug::DEBUG, "RESTORING OLD CURSOR!");
325 disp_.update(Update::None);
326 disp_.dispatched(false);
328 // restore the previous one because nested Cursor::dispatch calls
329 // are possible which would change it
330 beforeDispatchCursor_ = safe.beforeDispatchCursor_;
335 DispatchResult Cursor::result() const
341 BufferView & Cursor::bv() const
350 LASSERT(depth() >= 1, /**/);
355 void Cursor::push(Inset & p)
357 push_back(CursorSlice(p));
358 p.setBuffer(*buffer());
362 void Cursor::pushBackward(Inset & p)
364 LASSERT(!empty(), return);
365 //lyxerr << "Entering inset " << t << " front" << endl;
371 bool Cursor::popBackward()
373 LASSERT(!empty(), return false);
381 bool Cursor::popForward()
383 LASSERT(!empty(), return false);
384 //lyxerr << "Leaving inset from in back" << endl;
385 const pos_type lp = (depth() > 1) ? (*this)[depth() - 2].lastpos() : 0;
389 pos() += lastpos() - lp + 1;
394 int Cursor::currentMode()
396 LASSERT(!empty(), /**/);
397 for (int i = depth() - 1; i >= 0; --i) {
398 int res = operator[](i).inset().currentMode();
399 if (res != Inset::UNDECIDED_MODE)
402 return Inset::TEXT_MODE;
406 void Cursor::getPos(int & x, int & y) const
408 Point p = bv().getPos(*this, boundary());
414 Row const & Cursor::textRow() const
416 CursorSlice const & cs = innerTextSlice();
417 ParagraphMetrics const & pm = bv().parMetrics(cs.text(), cs.pit());
418 LASSERT(!pm.rows().empty(), /**/);
419 return pm.getRow(pos(), boundary());
423 void Cursor::resetAnchor()
430 bool Cursor::posBackward()
439 bool Cursor::posForward()
441 if (pos() == lastpos())
448 bool Cursor::posVisRight(bool skip_inset)
450 Cursor new_cur = *this; // where we will move to
451 pos_type left_pos; // position visually left of current cursor
452 pos_type right_pos; // position visually right of current cursor
453 bool new_pos_is_RTL; // is new position we're moving to RTL?
455 getSurroundingPos(left_pos, right_pos);
457 LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
459 // Are we at an inset?
460 new_cur.pos() = right_pos;
461 new_cur.boundary(false);
463 text()->checkAndActivateInsetVisual(new_cur, right_pos >= pos(), false)) {
464 // we actually move the cursor at the end of this function, for now
465 // we just keep track of the new position in new_cur...
466 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
469 // Are we already at rightmost pos in row?
470 else if (text()->empty() || right_pos == -1) {
473 if (!new_cur.posVisToNewRow(false)) {
474 LYXERR(Debug::RTL, "not moving!");
478 // we actually move the cursor at the end of this function, for now
479 // just keep track of the new position in new_cur...
480 LYXERR(Debug::RTL, "right edge, moving: " << int(new_cur.pit()) << ","
481 << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
484 // normal movement to the right
487 // Recall, if the cursor is at position 'x', that means *before*
488 // the character at position 'x'. In RTL, "before" means "to the
489 // right of", in LTR, "to the left of". So currently our situation
490 // is this: the position to our right is 'right_pos' (i.e., we're
491 // currently to the left of 'right_pos'). In order to move to the
492 // right, it depends whether or not the character at 'right_pos' is RTL.
493 new_pos_is_RTL = paragraph().getFontSettings(
494 buffer()->params(), right_pos).isVisibleRightToLeft();
495 // If the character at 'right_pos' *is* LTR, then in order to move to
496 // the right of it, we need to be *after* 'right_pos', i.e., move to
497 // position 'right_pos' + 1.
498 if (!new_pos_is_RTL) {
499 new_cur.pos() = right_pos + 1;
500 // set the boundary to true in two situations:
502 // 1. if new_pos is now lastpos, and we're in an RTL paragraph
503 // (this means that we're moving right to the end of an LTR chunk
504 // which is at the end of an RTL paragraph);
505 (new_cur.pos() == lastpos()
506 && paragraph().isRTL(buffer()->params()))
507 // 2. if the position *after* right_pos is RTL (we want to be
508 // *after* right_pos, not before right_pos + 1!)
509 || paragraph().getFontSettings(buffer()->params(),
510 new_cur.pos()).isVisibleRightToLeft()
512 new_cur.boundary(true);
513 else // set the boundary to false
514 new_cur.boundary(false);
516 // Otherwise (if the character at position 'right_pos' is RTL), then
517 // moving to the right of it is as easy as setting the new position
520 new_cur.pos() = right_pos;
521 new_cur.boundary(false);
526 bool moved = (new_cur.pos() != pos()
527 || new_cur.pit() != pit()
528 || new_cur.boundary() != boundary());
531 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
532 << (new_cur.boundary() ? " (boundary)" : ""));
540 bool Cursor::posVisLeft(bool skip_inset)
542 Cursor new_cur = *this; // where we will move to
543 pos_type left_pos; // position visually left of current cursor
544 pos_type right_pos; // position visually right of current cursor
545 bool new_pos_is_RTL; // is new position we're moving to RTL?
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() = left_pos;
553 new_cur.boundary(false);
555 text()->checkAndActivateInsetVisual(new_cur, left_pos >= pos(), true)) {
556 // we actually move the cursor at the end of this function, for now
557 // we just keep track of the new position in new_cur...
558 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
561 // Are we already at leftmost pos in row?
562 else if (text()->empty() || left_pos == -1) {
565 if (!new_cur.posVisToNewRow(true)) {
566 LYXERR(Debug::RTL, "not moving!");
570 // we actually move the cursor at the end of this function, for now
571 // just keep track of the new position in new_cur...
572 LYXERR(Debug::RTL, "left edge, moving: " << int(new_cur.pit()) << ","
573 << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
576 // normal movement to the left
579 // Recall, if the cursor is at position 'x', that means *before*
580 // the character at position 'x'. In RTL, "before" means "to the
581 // right of", in LTR, "to the left of". So currently our situation
582 // is this: the position to our left is 'left_pos' (i.e., we're
583 // currently to the right of 'left_pos'). In order to move to the
584 // left, it depends whether or not the character at 'left_pos' is RTL.
585 new_pos_is_RTL = paragraph().getFontSettings(
586 buffer()->params(), left_pos).isVisibleRightToLeft();
587 // If the character at 'left_pos' *is* RTL, then in order to move to
588 // the left of it, we need to be *after* 'left_pos', i.e., move to
589 // position 'left_pos' + 1.
590 if (new_pos_is_RTL) {
591 new_cur.pos() = left_pos + 1;
592 // set the boundary to true in two situations:
594 // 1. if new_pos is now lastpos and we're in an LTR paragraph
595 // (this means that we're moving left to the end of an RTL chunk
596 // which is at the end of an LTR paragraph);
597 (new_cur.pos() == lastpos()
598 && !paragraph().isRTL(buffer()->params()))
599 // 2. if the position *after* left_pos is not RTL (we want to be
600 // *after* left_pos, not before left_pos + 1!)
601 || !paragraph().getFontSettings(buffer()->params(),
602 new_cur.pos()).isVisibleRightToLeft()
604 new_cur.boundary(true);
605 else // set the boundary to false
606 new_cur.boundary(false);
608 // Otherwise (if the character at position 'left_pos' is LTR), then
609 // moving to the left of it is as easy as setting the new position
612 new_cur.pos() = left_pos;
613 new_cur.boundary(false);
618 bool moved = (new_cur.pos() != pos()
619 || new_cur.pit() != pit()
620 || new_cur.boundary() != boundary());
623 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
624 << (new_cur.boundary() ? " (boundary)" : ""));
632 void Cursor::getSurroundingPos(pos_type & left_pos, pos_type & right_pos)
634 // preparing bidi tables
635 Paragraph const & par = paragraph();
636 Buffer const & buf = *buffer();
637 Row const & row = textRow();
639 bidi.computeTables(par, buf, row);
641 LYXERR(Debug::RTL, "bidi: " << row.pos() << "--" << row.endpos());
643 // The cursor is painted *before* the character at pos(), or, if 'boundary'
644 // is true, *after* the character at (pos() - 1). So we already have one
645 // known position around the cursor:
646 pos_type known_pos = boundary() ? pos() - 1 : pos();
648 // edge case: if we're at the end of the paragraph, things are a little
649 // different (because lastpos is a position which does not really "exist"
650 // --- there's no character there yet).
651 if (known_pos == lastpos()) {
652 if (par.isRTL(buf.params())) {
654 right_pos = bidi.vis2log(row.pos());
656 else { // LTR paragraph
658 left_pos = bidi.vis2log(row.endpos() - 1);
663 // Whether 'known_pos' is to the left or to the right of the cursor depends
664 // on whether it is an RTL or LTR character...
665 bool const cur_is_RTL =
666 par.getFontSettings(buf.params(), known_pos).isVisibleRightToLeft();
667 // ... in the following manner:
668 // For an RTL character, "before" means "to the right" and "after" means
669 // "to the left"; and for LTR, it's the reverse. So, 'known_pos' is to the
670 // right of the cursor if (RTL && boundary) or (!RTL && !boundary):
671 bool known_pos_on_right = (cur_is_RTL == boundary());
673 // So we now know one of the positions surrounding the cursor. Let's
674 // determine the other one:
676 if (known_pos_on_right) {
677 right_pos = known_pos;
678 // *visual* position of 'left_pos':
679 pos_type v_left_pos = bidi.log2vis(right_pos) - 1;
680 // If the position we just identified as 'left_pos' is a "skipped
681 // separator" (a separator which is at the logical end of a row,
682 // except for the last row in a paragraph; such separators are not
683 // painted, so they "are not really there"; note that in bidi text,
684 // such a separator could appear visually in the middle of a row),
685 // set 'left_pos' to the *next* position to the left.
686 if (bidi.inRange(v_left_pos)
687 && bidi.vis2log(v_left_pos) + 1 == row.endpos()
688 && row.endpos() < lastpos()
689 && par.isSeparator(bidi.vis2log(v_left_pos))) {
692 // calculate the logical position of 'left_pos', if in row
693 if (!bidi.inRange(v_left_pos))
696 left_pos = bidi.vis2log(v_left_pos);
697 // If the position we identified as 'right_pos' is a "skipped
698 // separator", set 'right_pos' to the *next* position to the right.
699 if (right_pos + 1 == row.endpos() && row.endpos() < lastpos()
700 && par.isSeparator(right_pos)) {
701 pos_type v_right_pos = bidi.log2vis(right_pos) + 1;
702 if (!bidi.inRange(v_right_pos))
705 right_pos = bidi.vis2log(v_right_pos);
708 else { // known_pos is on the left
709 left_pos = known_pos;
710 // *visual* position of 'right_pos'
711 pos_type v_right_pos = bidi.log2vis(left_pos) + 1;
712 // If the position we just identified as 'right_pos' is a "skipped
713 // separator", set 'right_pos' to the *next* position to the right.
714 if (bidi.inRange(v_right_pos)
715 && bidi.vis2log(v_right_pos) + 1 == row.endpos()
716 && row.endpos() < lastpos()
717 && par.isSeparator(bidi.vis2log(v_right_pos))) {
720 // calculate the logical position of 'right_pos', if in row
721 if (!bidi.inRange(v_right_pos))
724 right_pos = bidi.vis2log(v_right_pos);
725 // If the position we identified as 'left_pos' is a "skipped
726 // separator", set 'left_pos' to the *next* position to the left.
727 if (left_pos + 1 == row.endpos() && row.endpos() < lastpos()
728 && par.isSeparator(left_pos)) {
729 pos_type v_left_pos = bidi.log2vis(left_pos) - 1;
730 if (!bidi.inRange(v_left_pos))
733 left_pos = bidi.vis2log(v_left_pos);
740 bool Cursor::posVisToNewRow(bool movingLeft)
742 Paragraph const & par = paragraph();
743 Buffer const & buf = *buffer();
744 Row const & row = textRow();
745 bool par_is_LTR = !par.isRTL(buf.params());
747 // Inside a table, determining whether to move to the next or previous row
748 // should be done based on the table's direction.
750 if (s >= 1 && (*this)[s].inset().asInsetTabular()) {
751 par_is_LTR = !(*this)[s].inset().asInsetTabular()->isRightToLeft(*this);
752 LYXERR(Debug::RTL, "Inside table! par_is_LTR=" << (par_is_LTR ? 1 : 0));
755 // if moving left in an LTR paragraph or moving right in an RTL one,
756 // move to previous row
757 if (par_is_LTR == movingLeft) {
758 if (row.pos() == 0) { // we're at first row in paragraph
759 if (pit() == 0) // no previous paragraph! don't move
761 // move to last pos in previous par
765 } else { // move to previous row in this par
766 pos() = row.pos() - 1; // this is guaranteed to be in previous row
770 // if moving left in an RTL paragraph or moving right in an LTR one,
773 if (row.endpos() == lastpos()) { // we're at last row in paragraph
774 if (pit() == lastpit()) // last paragraph! don't move
776 // move to first row in next par
780 } else { // move to next row in this par
781 pos() = row.endpos();
786 // make sure we're at left-/right-most pos in new row
787 posVisToRowExtremity(!movingLeft);
793 void Cursor::posVisToRowExtremity(bool left)
795 // prepare bidi tables
796 Paragraph const & par = paragraph();
797 Buffer const & buf = *buffer();
798 Row const & row = textRow();
800 bidi.computeTables(par, buf, row);
802 LYXERR(Debug::RTL, "entering extremity: " << pit() << "," << pos() << ","
803 << (boundary() ? 1 : 0));
805 if (left) { // move to leftmost position
806 // if this is an RTL paragraph, and we're at the last row in the
807 // paragraph, move to lastpos
808 if (par.isRTL(buf.params()) && row.endpos() == lastpos())
811 pos() = bidi.vis2log(row.pos());
813 // Moving to the leftmost position in the row, the cursor should
814 // normally be placed to the *left* of the leftmost position.
815 // A very common exception, though, is if the leftmost character
816 // also happens to be the separator at the (logical) end of the row
817 // --- in this case, the separator is positioned beyond the left
818 // margin, and we don't want to move the cursor there (moving to
819 // the left of the separator is equivalent to moving to the next
820 // line). So, in this case we actually want to place the cursor
821 // to the *right* of the leftmost position (the separator).
822 // Another exception is if we're moving to the logically last
823 // position in the row, which is *not* a separator: this means
824 // that the entire row has no separators (if there were any, the
825 // row would have been broken there); and therefore in this case
826 // we also move to the *right* of the last position (this indicates
827 // to the user that there is no space after this position, and is
828 // consistent with the behavior in the middle of a row --- moving
829 // right or left moves to the next/previous character; if we were
830 // to move to the *left* of this position, that would simulate
831 // a separator which is not really there!).
832 // Finally, there is an exception to the previous exception: if
833 // this non-separator-but-last-position-in-row is an inset, then
834 // we *do* want to stay to the left of it anyway: this is the
835 // "boundary" which we simulate at insets.
837 bool right_of_pos = false; // do we want to be to the right of pos?
839 // as explained above, if at last pos in row, stay to the right
840 if ((pos() == row.endpos() - 1) && !par.isInset(pos()))
843 // Now we know if we want to be to the left or to the right of pos,
844 // let's make sure we are where we want to be.
845 bool new_pos_is_RTL =
846 par.getFontSettings(buf.params(), pos()).isVisibleRightToLeft();
848 if (new_pos_is_RTL == !right_of_pos) {
855 else { // move to rightmost position
856 // if this is an LTR paragraph, and we're at the last row in the
857 // paragraph, move to lastpos
858 if (!par.isRTL(buf.params()) && row.endpos() == lastpos())
861 pos() = bidi.vis2log(row.endpos() - 1);
863 // Moving to the rightmost position in the row, the cursor should
864 // normally be placed to the *right* of the rightmost position.
865 // A very common exception, though, is if the rightmost character
866 // also happens to be the separator at the (logical) end of the row
867 // --- in this case, the separator is positioned beyond the right
868 // margin, and we don't want to move the cursor there (moving to
869 // the right of the separator is equivalent to moving to the next
870 // line). So, in this case we actually want to place the cursor
871 // to the *left* of the rightmost position (the separator).
872 // Another exception is if we're moving to the logically last
873 // position in the row, which is *not* a separator: this means
874 // that the entire row has no separators (if there were any, the
875 // row would have been broken there); and therefore in this case
876 // we also move to the *left* of the last position (this indicates
877 // to the user that there is no space after this position, and is
878 // consistent with the behavior in the middle of a row --- moving
879 // right or left moves to the next/previous character; if we were
880 // to move to the *right* of this position, that would simulate
881 // a separator which is not really there!).
882 // Finally, there is an exception to the previous exception: if
883 // this non-separator-but-last-position-in-row is an inset, then
884 // we *do* want to stay to the right of it anyway: this is the
885 // "boundary" which we simulate at insets.
887 bool left_of_pos = false; // do we want to be to the left of pos?
889 // as explained above, if at last pos in row, stay to the left
890 if ((pos() == row.endpos() - 1) && !par.isInset(pos()))
893 // Now we know if we want to be to the left or to the right of pos,
894 // let's make sure we are where we want to be.
895 bool new_pos_is_RTL =
896 par.getFontSettings(buf.params(), pos()).isVisibleRightToLeft();
898 if (new_pos_is_RTL == left_of_pos) {
904 LYXERR(Debug::RTL, "leaving extremity: " << pit() << "," << pos() << ","
905 << (boundary() ? 1 : 0));
909 CursorSlice Cursor::anchor() const
911 LASSERT(anchor_.depth() >= depth(), /**/);
912 CursorSlice normal = anchor_[depth() - 1];
913 if (depth() < anchor_.depth() && top() <= normal) {
914 // anchor is behind cursor -> move anchor behind the inset
921 CursorSlice Cursor::selBegin() const
925 return anchor() < top() ? anchor() : top();
929 CursorSlice Cursor::selEnd() const
933 return anchor() > top() ? anchor() : top();
937 DocIterator Cursor::selectionBegin() const
943 // FIXME: This is a work-around for the problem that
944 // CursorSlice doesn't keep track of the boundary.
945 if (anchor() == top())
946 di = anchor_.boundary() > boundary() ? anchor_ : *this;
948 di = anchor() < top() ? anchor_ : *this;
954 DocIterator Cursor::selectionEnd() const
960 // FIXME: This is a work-around for the problem that
961 // CursorSlice doesn't keep track of the boundary.
962 if (anchor() == top())
963 di = anchor_.boundary() < boundary() ? anchor_ : *this;
965 di = anchor() > top() ? anchor_ : *this;
967 if (di.depth() > depth()) {
975 void Cursor::setSelection()
978 // A selection with no contents is not a selection
979 // FIXME: doesnt look ok
980 if (idx() == anchor().idx() &&
981 pit() == anchor().pit() &&
982 pos() == anchor().pos())
987 void Cursor::setSelection(DocIterator const & where, int n)
996 void Cursor::clearSelection()
1004 void Cursor::setTargetX(int x)
1007 textTargetOffset_ = 0;
1011 int Cursor::x_target() const
1017 void Cursor::clearTargetX()
1020 textTargetOffset_ = 0;
1024 void Cursor::updateTextTargetOffset()
1029 textTargetOffset_ = x - x_target_;
1033 void Cursor::info(odocstream & os) const
1035 for (int i = 1, n = depth(); i < n; ++i) {
1036 operator[](i).inset().infoize(os);
1040 Inset const * inset = prevInset();
1041 // prevInset() can return 0 in certain case.
1043 prevInset()->infoize2(os);
1045 // overwite old message
1050 bool Cursor::selHandle(bool sel)
1052 //lyxerr << "Cursor::selHandle" << endl;
1055 if (sel == selection())
1059 cap::saveSelection(*this);
1067 ostream & operator<<(ostream & os, Cursor const & cur)
1069 os << "\n cursor: | anchor:\n";
1070 for (size_t i = 0, n = cur.depth(); i != n; ++i) {
1071 os << " " << cur[i] << " | ";
1072 if (i < cur.anchor_.depth())
1073 os << cur.anchor_[i];
1075 os << "-------------------------------";
1078 for (size_t i = cur.depth(), n = cur.anchor_.depth(); i < n; ++i) {
1079 os << "------------------------------- | " << cur.anchor_[i] << "\n";
1081 os << " selection: " << cur.selection_
1082 << " x_target: " << cur.x_target_ << endl;
1087 LyXErr & operator<<(LyXErr & os, Cursor const & cur)
1097 ///////////////////////////////////////////////////////////////////
1100 // The part below is the non-integrated rest of the original math
1101 // cursor. This should be either generalized for texted or moved
1102 // back to mathed (in most cases to InsetMathNest).
1104 ///////////////////////////////////////////////////////////////////
1106 #include "mathed/InsetMathChar.h"
1107 #include "mathed/InsetMathGrid.h"
1108 #include "mathed/InsetMathScript.h"
1109 #include "mathed/InsetMathUnknown.h"
1110 #include "mathed/MathFactory.h"
1111 #include "mathed/MathStream.h"
1112 #include "mathed/MathSupport.h"
1117 //#define FILEDEBUG 1
1120 bool Cursor::isInside(Inset const * p) const
1122 for (size_t i = 0; i != depth(); ++i)
1123 if (&operator[](i).inset() == p)
1129 void Cursor::leaveInset(Inset const & inset)
1131 for (size_t i = 0; i != depth(); ++i) {
1132 if (&operator[](i).inset() == &inset) {
1140 bool Cursor::openable(MathAtom const & t) const
1151 // we can't move into anything new during selection
1152 if (depth() >= anchor_.depth())
1154 if (t.nucleus() != &anchor_[depth()].inset())
1161 void Cursor::setScreenPos(int x, int /*y*/)
1164 //bruteFind(*this, x, y, 0, bv().workWidth(), 0, bv().workHeight());
1169 void Cursor::plainErase()
1171 cell().erase(pos());
1175 void Cursor::markInsert()
1177 insert(char_type(0));
1181 void Cursor::markErase()
1183 cell().erase(pos());
1187 void Cursor::plainInsert(MathAtom const & t)
1189 cell().insert(pos(), t);
1191 inset().setBuffer(bv_->buffer());
1196 void Cursor::insert(docstring const & str)
1198 for_each(str.begin(), str.end(),
1199 boost::bind(static_cast<void(Cursor::*)(char_type)>
1200 (&Cursor::insert), this, _1));
1204 void Cursor::insert(char_type c)
1206 //lyxerr << "Cursor::insert char '" << c << "'" << endl;
1207 LASSERT(!empty(), /**/);
1209 cap::selClearOrDel(*this);
1210 insert(new InsetMathChar(c));
1212 text()->insertChar(*this, c);
1217 void Cursor::insert(MathAtom const & t)
1219 //lyxerr << "Cursor::insert MathAtom '" << t << "'" << endl;
1221 cap::selClearOrDel(*this);
1226 void Cursor::insert(Inset * inset0)
1228 LASSERT(inset0, /**/);
1230 insert(MathAtom(inset0));
1232 text()->insertInset(*this, inset0);
1233 inset0->setBuffer(bv_->buffer());
1239 void Cursor::niceInsert(docstring const & t, Parse::flags f)
1250 void Cursor::niceInsert(MathAtom const & t)
1253 docstring const safe = cap::grabAndEraseSelection(*this);
1255 // enter the new inset and move the contents of the selection if possible
1256 if (t->isActive()) {
1258 // be careful here: don't use 'pushBackward(t)' as this we need to
1259 // push the clone, not the original
1260 pushBackward(*nextInset());
1261 // We may not use niceInsert here (recursion)
1269 void Cursor::insert(MathData const & ar)
1273 cap::eraseSelection(*this);
1274 cell().insert(pos(), ar);
1279 bool Cursor::backspace()
1281 autocorrect() = false;
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 && prevAtom()->nargs() > 0) {
1318 // let's require two backspaces for 'big stuff' and
1319 // highlight on the first
1331 bool Cursor::erase()
1333 autocorrect() = false;
1338 cap::eraseSelection(*this);
1342 // delete empty cells if possible
1343 if (pos() == lastpos() && inset().idxDelete(idx()))
1346 // special behaviour when in last position of cell
1347 if (pos() == lastpos()) {
1348 bool one_cell = inset().nargs() == 1;
1349 if (one_cell && lastpos() == 0) {
1351 // Directly delete empty cell: [|[]] => [|]
1357 // [|], can not delete from inside
1362 inset().idxGlue(idx());
1366 // 'clever' UI hack: only erase large items if previously slected
1367 if (pos() != lastpos() && nextAtom()->nargs() > 0) {
1382 DocIterator save = *this;
1383 FuncRequest cmd(selection() ? LFUN_UP_SELECT : LFUN_UP, docstring());
1384 this->dispatch(cmd);
1385 if (disp_.dispatched())
1388 autocorrect() = false;
1396 DocIterator save = *this;
1397 FuncRequest cmd(selection() ? LFUN_DOWN_SELECT : LFUN_DOWN, docstring());
1398 this->dispatch(cmd);
1399 if (disp_.dispatched())
1402 autocorrect() = false;
1407 bool Cursor::macroModeClose()
1411 InsetMathUnknown * p = activeMacro();
1414 asArray(p->selection(), selection);
1415 docstring const s = p->name();
1417 cell().erase(pos());
1419 // do nothing if the macro name is empty
1423 // trigger updates of macros, at least, if no full
1424 // updates take place anyway
1425 updateFlags(Update::Force);
1427 docstring const name = s.substr(1);
1428 InsetMathNest * const in = inset().asInsetMath()->asNestInset();
1429 if (in && in->interpretString(*this, s))
1431 MathAtom atom = createInsetMath(name);
1433 // try to put argument into macro, if we just inserted a macro
1434 bool macroArg = false;
1435 MathMacro * atomAsMacro = atom.nucleus()->asMacro();
1437 // macros here are still unfolded (in init mode in fact). So
1438 // we have to resolve the macro here manually and check its arity
1439 // to put the selection behind it if arity > 0.
1440 MacroData const * data = buffer()->getMacro(atomAsMacro->name());
1441 if (selection.size() > 0 && data && data->numargs() - data->optionals() > 0) {
1443 atomAsMacro->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT, 1);
1445 // non-greedy case. Do not touch the arguments behind
1446 atomAsMacro->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT, 0);
1449 // insert remembered selection into first argument of a non-macro
1450 else if (atom.nucleus()->nargs() > 0)
1451 atom.nucleus()->cell(0).append(selection);
1455 // finally put the macro argument behind, if needed
1457 if (selection.size() > 1)
1458 plainInsert(MathAtom(new InsetMathBrace(selection)));
1467 docstring Cursor::macroName()
1469 return inMacroMode() ? activeMacro()->name() : docstring();
1473 void Cursor::handleNest(MathAtom const & a, int c)
1475 //lyxerr << "Cursor::handleNest: " << c << endl;
1477 asArray(cap::grabAndEraseSelection(*this), t.nucleus()->cell(c));
1480 pushBackward(*nextInset());
1484 int Cursor::targetX() const
1486 if (x_target() != -1)
1495 int Cursor::textTargetOffset() const
1497 return textTargetOffset_;
1501 void Cursor::setTargetX()
1510 bool Cursor::inMacroMode() const
1516 InsetMathUnknown const * p = prevAtom()->asUnknownInset();
1517 return p && !p->final();
1521 InsetMathUnknown * Cursor::activeMacro()
1523 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1527 InsetMathUnknown const * Cursor::activeMacro() const
1529 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1533 void Cursor::pullArg()
1536 MathData ar = cell();
1537 if (popBackward() && inMathed()) {
1539 cell().insert(pos(), ar);
1542 //formula()->mutateToText();
1547 void Cursor::touch()
1551 DocIterator::const_iterator it = begin();
1552 DocIterator::const_iterator et = end();
1553 for ( ; it != et; ++it)
1559 void Cursor::normalize()
1561 if (idx() > lastidx()) {
1562 lyxerr << "this should not really happen - 1: "
1563 << idx() << ' ' << nargs()
1564 << " in: " << &inset() << endl;
1568 if (pos() > lastpos()) {
1569 lyxerr << "this should not really happen - 2: "
1570 << pos() << ' ' << lastpos() << " in idx: " << idx()
1572 odocstringstream os;
1573 WriteStream wi(os, false, true, false);
1574 inset().asInsetMath()->write(wi);
1575 lyxerr << to_utf8(os.str()) << endl;
1581 bool Cursor::upDownInMath(bool up)
1583 // Be warned: The 'logic' implemented in this function is highly
1584 // fragile. A distance of one pixel or a '<' vs '<=' _really
1585 // matters. So fiddle around with it only if you think you know
1586 // what you are doing!
1590 xo = theLyXFunc().cursorBeforeDispatchX();
1592 // check if we had something else in mind, if not, this is the future
1594 if (x_target_ == -1)
1596 else if (inset().asInsetText() && xo - textTargetOffset() != x_target()) {
1597 // In text mode inside the line (not left or right) possibly set a new target_x,
1598 // but only if we are somewhere else than the previous target-offset.
1600 // We want to keep the x-target on subsequent up/down movements
1601 // that cross beyond the end of short lines. Thus a special
1602 // handling when the cursor is at the end of line: Use the new
1603 // x-target only if the old one was before the end of line
1604 // or the old one was after the beginning of the line
1605 bool inRTL = isWithinRtlParagraph(*this);
1609 left = pos() == textRow().endpos();
1610 right = pos() == textRow().pos();
1612 left = pos() == textRow().pos();
1613 right = pos() == textRow().endpos();
1615 if ((!left && !right) ||
1616 (left && !right && xo < x_target_) ||
1617 (!left && right && x_target_ < xo))
1624 // try neigbouring script insets
1626 if (inMathed() && !selection()) {
1629 InsetMathScript const * p = prevAtom()->asScriptInset();
1630 if (p && p->has(up)) {
1632 push(*const_cast<InsetMathScript*>(p));
1633 idx() = p->idxOfScript(up);
1636 // we went in the right direction? Otherwise don't jump into the script
1640 int oy = theLyXFunc().cursorBeforeDispatchY();
1641 if ((!up && y <= oy) ||
1650 if (pos() != lastpos()) {
1651 InsetMathScript const * p = nextAtom()->asScriptInset();
1652 if (p && p->has(up)) {
1653 push(*const_cast<InsetMathScript*>(p));
1654 idx() = p->idxOfScript(up);
1657 // we went in the right direction? Otherwise don't jump into the script
1661 int oy = theLyXFunc().cursorBeforeDispatchY();
1662 if ((!up && y <= oy) ||
1671 // try to find an inset that knows better then we,
1672 if (inset().idxUpDown(*this, up)) {
1673 //lyxerr << "idxUpDown triggered" << endl;
1674 // try to find best position within this inset
1676 setCursor(bruteFind2(*this, xo, yo));
1680 // any improvement going just out of inset?
1681 if (popBackward() && inMathed()) {
1682 //lyxerr << "updown: popBackward succeeded" << endl;
1685 int yold = theLyXFunc().cursorBeforeDispatchY();
1687 if (up ? ynew < yold : ynew > yold)
1691 // no success, we are probably at the document top or bottom
1697 bool Cursor::atFirstOrLastRow(bool up)
1699 TextMetrics const & tm = bv_->textMetrics(text());
1700 ParagraphMetrics const & pm = tm.parMetrics(pit());
1703 if (pos() && boundary())
1704 row = pm.pos2row(pos() - 1);
1706 row = pm.pos2row(pos());
1709 if (pit() == 0 && row == 0)
1712 if (pit() + 1 >= int(text()->paragraphs().size()) &&
1713 row + 1 >= int(pm.rows().size()))
1719 bool Cursor::upDownInText(bool up, bool & updateNeeded)
1721 LASSERT(text(), /**/);
1727 xo = theLyXFunc().cursorBeforeDispatchX();
1729 // update the targetX - this is here before the "return false"
1730 // to set a new target which can be used by InsetTexts above
1731 // if we cannot move up/down inside this inset anymore
1732 if (x_target_ == -1)
1734 else if (xo - textTargetOffset() != x_target() &&
1735 depth() == beforeDispatchCursor_.depth()) {
1736 // In text mode inside the line (not left or right) possibly set a new target_x,
1737 // but only if we are somewhere else than the previous target-offset.
1739 // We want to keep the x-target on subsequent up/down movements
1740 // that cross beyond the end of short lines. Thus a special
1741 // handling when the cursor is at the end of line: Use the new
1742 // x-target only if the old one was before the end of line
1743 // or the old one was after the beginning of the line
1744 bool inRTL = isWithinRtlParagraph(*this);
1748 left = pos() == textRow().endpos();
1749 right = pos() == textRow().pos();
1751 left = pos() == textRow().pos();
1752 right = pos() == textRow().endpos();
1754 if ((!left && !right) ||
1755 (left && !right && xo < x_target_) ||
1756 (!left && right && x_target_ < xo))
1763 // first get the current line
1764 TextMetrics & tm = bv_->textMetrics(text());
1765 ParagraphMetrics const & pm = tm.parMetrics(pit());
1767 if (pos() && boundary())
1768 row = pm.pos2row(pos() - 1);
1770 row = pm.pos2row(pos());
1772 if (atFirstOrLastRow(up))
1775 // with and without selection are handled differently
1777 int yo = bv().getPos(*this, boundary()).y_;
1779 // To next/previous row
1781 tm.editXY(*this, xo, yo - textRow().ascent() - 1);
1783 tm.editXY(*this, xo, yo + textRow().descent() + 1);
1786 // This happens when you move out of an inset.
1787 // And to give the DEPM the possibility of doing
1788 // something we must provide it with two different
1790 Cursor dummy = *this;
1793 if (bv().checkDepm(dummy, old)) {
1794 updateNeeded = true;
1795 // Make sure that cur gets back whatever happened to dummy(Lgb)
1799 // if there is a selection, we stay out of any inset, and just jump to the right position:
1803 top().pos() = min(tm.x2pos(pit(), row - 1, xo), top().lastpos());
1804 } else if (pit() > 0) {
1806 TextMetrics & tm = bv_->textMetrics(text());
1807 if (!tm.contains(pit()))
1808 tm.newParMetricsUp();
1809 ParagraphMetrics const & pmcur = tm.parMetrics(pit());
1810 top().pos() = min(tm.x2pos(pit(), pmcur.rows().size() - 1, xo), top().lastpos());
1813 if (row + 1 < int(pm.rows().size())) {
1814 top().pos() = min(tm.x2pos(pit(), row + 1, xo), top().lastpos());
1815 } else if (pit() + 1 < int(text()->paragraphs().size())) {
1817 TextMetrics & tm = bv_->textMetrics(text());
1818 if (!tm.contains(pit()))
1819 tm.newParMetricsDown();
1820 top().pos() = min(tm.x2pos(pit(), 0, xo), top().lastpos());
1824 updateNeeded |= bv().checkDepm(*this, old);
1827 updateTextTargetOffset();
1832 void Cursor::handleFont(string const & font)
1834 LYXERR(Debug::DEBUG, font);
1838 safe = cap::grabAndEraseSelection(*this);
1843 if (lastpos() != 0) {
1844 // something left in the cell
1846 // cursor in first position
1848 } else if (pos() == lastpos()) {
1849 // cursor in last position
1852 // cursor in between. split cell
1853 MathData::iterator bt = cell().begin();
1854 MathAtom at = createInsetMath(from_utf8(font));
1855 at.nucleus()->cell(0) = MathData(bt, bt + pos());
1856 cell().erase(bt, bt + pos());
1861 // nothing left in the cell
1870 void Cursor::message(docstring const & msg) const
1872 theLyXFunc().setMessage(msg);
1876 void Cursor::errorMessage(docstring const & msg) const
1878 theLyXFunc().setErrorMessage(msg);
1882 docstring Cursor::selectionAsString(bool with_label) const
1887 int const label = with_label
1888 ? AS_STR_LABEL | AS_STR_INSETS : AS_STR_INSETS;
1891 idx_type const startidx = selBegin().idx();
1892 idx_type const endidx = selEnd().idx();
1893 if (startidx != endidx) {
1894 // multicell selection
1895 InsetTabular * table = inset().asInsetTabular();
1896 LASSERT(table, return docstring());
1897 return table->asString(startidx, endidx);
1900 ParagraphList const & pars = text()->paragraphs();
1902 pit_type const startpit = selBegin().pit();
1903 pit_type const endpit = selEnd().pit();
1904 size_t const startpos = selBegin().pos();
1905 size_t const endpos = selEnd().pos();
1907 if (startpit == endpit)
1908 return pars[startpit].asString(startpos, endpos, label);
1910 // First paragraph in selection
1911 docstring result = pars[startpit].
1912 asString(startpos, pars[startpit].size(), label)
1913 + parbreak(pars[startpit]);
1915 // The paragraphs in between (if any)
1916 for (pit_type pit = startpit + 1; pit != endpit; ++pit) {
1917 Paragraph const & par = pars[pit];
1918 result += par.asString(0, par.size(), label)
1919 + parbreak(pars[pit]);
1922 // Last paragraph in selection
1923 result += pars[endpit].asString(0, endpos, label);
1929 return cap::grabSelection(*this);
1935 docstring Cursor::currentState() const
1938 odocstringstream os;
1944 return text()->currentState(*this);
1950 docstring Cursor::getPossibleLabel() const
1952 return inMathed() ? from_ascii("eq:") : text()->getPossibleLabel(*this);
1956 Encoding const * Cursor::getEncoding() const
1960 CursorSlice const & sl = innerTextSlice();
1961 Text const & text = *sl.text();
1962 Font font = text.getPar(sl.pit()).getFont(
1963 bv().buffer().params(), sl.pos(), outerFont(sl.pit(), text.paragraphs()));
1964 return font.language()->encoding();
1968 void Cursor::undispatched()
1970 disp_.dispatched(false);
1974 void Cursor::dispatched()
1976 disp_.dispatched(true);
1980 void Cursor::updateFlags(Update::flags f)
1986 void Cursor::noUpdate()
1988 disp_.update(Update::None);
1992 Font Cursor::getFont() const
1994 // The logic here should more or less match to the Cursor::setCurrentFont
1995 // logic, i.e. the cursor height should give a hint what will happen
1996 // if a character is entered.
1998 // HACK. far from being perfect...
2000 CursorSlice const & sl = innerTextSlice();
2001 Text const & text = *sl.text();
2002 Paragraph const & par = text.getPar(sl.pit());
2004 // on boundary, so we are really at the character before
2005 pos_type pos = sl.pos();
2006 if (pos > 0 && boundary())
2009 // on space? Take the font before (only for RTL boundary stay)
2011 TextMetrics const & tm = bv().textMetrics(&text);
2012 if (pos == sl.lastpos()
2013 || (par.isSeparator(pos)
2014 && !tm.isRTLBoundary(sl.pit(), pos)))
2018 // get font at the position
2019 Font font = par.getFont(buffer()->params(), pos,
2020 outerFont(sl.pit(), text.paragraphs()));
2026 bool Cursor::fixIfBroken()
2028 if (DocIterator::fixIfBroken()) {
2036 bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur)
2038 // find inset in common
2040 for (i = 0; i < old.depth() && i < cur.depth(); ++i) {
2041 if (&old[i].inset() != &cur[i].inset())
2045 // update words if we just moved to another paragraph
2046 if (i == old.depth() && i == cur.depth()
2047 && !cur.buffer()->isClean()
2048 && cur.inTexted() && old.inTexted()
2049 && cur.pit() != old.pit()) {
2050 old.paragraph().updateWords(old.top());
2053 // notify everything on top of the common part in old cursor,
2054 // but stop if the inset claims the cursor to be invalid now
2055 for (size_type j = i; j < old.depth(); ++j) {
2056 Cursor insetPos = old;
2058 if (old[j].inset().notifyCursorLeaves(insetPos, cur))
2062 // notify everything on top of the common part in new cursor,
2063 // but stop if the inset claims the cursor to be invalid now
2064 for (; i < cur.depth(); ++i) {
2065 if (cur[i].inset().notifyCursorEnters(cur))
2073 void Cursor::setCurrentFont()
2075 CursorSlice const & cs = innerTextSlice();
2076 Paragraph const & par = cs.paragraph();
2077 pos_type cpit = cs.pit();
2078 pos_type cpos = cs.pos();
2079 Text const & ctext = *cs.text();
2080 TextMetrics const & tm = bv().textMetrics(&ctext);
2082 // are we behind previous char in fact? -> go to that char
2083 if (cpos > 0 && boundary())
2086 // find position to take the font from
2088 // paragraph end? -> font of last char
2089 if (cpos == lastpos())
2091 // on space? -> look at the words in front of space
2092 else if (cpos > 0 && par.isSeparator(cpos)) {
2093 // abc| def -> font of c
2094 // abc |[WERBEH], i.e. boundary==true -> font of c
2095 // abc [WERBEH]| def, font of the space
2096 if (!tm.isRTLBoundary(cpit, cpos))
2102 BufferParams const & bufparams = buffer()->params();
2103 current_font = par.getFontSettings(bufparams, cpos);
2104 real_current_font = tm.displayFont(cpit, cpos);
2106 // special case for paragraph end
2107 if (cs.pos() == lastpos()
2108 && tm.isRTLBoundary(cpit, cs.pos())
2110 Language const * lang = par.getParLanguage(bufparams);
2111 current_font.setLanguage(lang);
2112 current_font.fontInfo().setNumber(FONT_OFF);
2113 real_current_font.setLanguage(lang);
2114 real_current_font.fontInfo().setNumber(FONT_OFF);
2119 bool Cursor::textUndo()
2121 DocIterator dit = *this;
2122 // Undo::textUndo() will modify dit.
2123 if (!buffer()->undo().textUndo(dit))
2133 bool Cursor::textRedo()
2135 DocIterator dit = *this;
2136 // Undo::textRedo() will modify dit.
2137 if (!buffer()->undo().textRedo(dit))
2147 void Cursor::finishUndo() const
2149 buffer()->undo().finishUndo();
2153 void Cursor::beginUndoGroup() const
2155 buffer()->undo().beginUndoGroup();
2159 void Cursor::endUndoGroup() const
2161 buffer()->undo().endUndoGroup();
2165 void Cursor::recordUndo(UndoKind kind, pit_type from, pit_type to) const
2167 buffer()->undo().recordUndo(*this, kind, from, to);
2171 void Cursor::recordUndo(UndoKind kind, pit_type from) const
2173 buffer()->undo().recordUndo(*this, kind, from);
2177 void Cursor::recordUndo(UndoKind kind) const
2179 buffer()->undo().recordUndo(*this, kind);
2183 void Cursor::recordUndoInset(UndoKind kind) const
2185 buffer()->undo().recordUndoInset(*this, kind);
2189 void Cursor::recordUndoFullDocument() const
2191 buffer()->undo().recordUndoFullDocument(*this);
2195 void Cursor::recordUndoSelection() const
2198 if (cap::multipleCellsSelected(*this))
2203 buffer()->undo().recordUndo(*this, ATOMIC_UNDO,
2204 selBegin().pit(), selEnd().pit());
2209 void Cursor::checkBufferStructure()
2211 Buffer const * master = buffer()->masterBuffer();
2212 master->tocBackend().updateItem(*this);