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 "LyXAction.h"
31 #include "Paragraph.h"
32 #include "ParIterator.h"
35 #include "TextMetrics.h"
36 #include "TocBackend.h"
38 #include "support/lassert.h"
39 #include "support/debug.h"
40 #include "support/docstream.h"
42 #include "insets/InsetTabular.h"
43 #include "insets/InsetText.h"
45 #include "mathed/InsetMath.h"
46 #include "mathed/InsetMathBrace.h"
47 #include "mathed/InsetMathScript.h"
48 #include "mathed/MacroTable.h"
49 #include "mathed/MathData.h"
50 #include "mathed/MathMacro.h"
52 #include "support/bind.h"
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.realAnchor()))) {
219 // If this function is ever used again, check whether this
220 // is the same as "bv.getPos(it, false)" with boundary = false.
221 Point p = bv.getPos(it);
224 if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
225 double const dx = xo - x;
226 double const dy = yo - y;
227 double const d = dx * dx + dy * dy;
228 //lyxerr << "itx: " << xo << " ity: " << yo << " d: " << d
229 // << " dx: " << dx << " dy: " << dy
230 // << " idx: " << it.idx() << " pos: " << it.pos()
233 // '<=' in order to take the last possible position
234 // this is important for clicking behind \sum in e.g. '\sum_i a'
235 if (d <= best_dist) {
236 //lyxerr << "*" << endl;
244 //lyxerr << "best_dist: " << best_dist << " cur:\n" << best_cursor << endl;
245 if (best_cursor == et)
247 cur.setCursor(best_cursor);
254 // be careful: this is called from the bv's constructor, too, so
255 // bv functions are not yet available!
256 Cursor::Cursor(BufferView & bv)
257 : DocIterator(&bv.buffer()), bv_(&bv), anchor_(),
258 x_target_(-1), textTargetOffset_(0),
259 selection_(false), mark_(false), word_selection_(false),
260 logicalpos_(false), current_font(inherit_font)
267 push_back(CursorSlice(buffer()->inset()));
268 anchor_ = doc_iterator_begin(buffer());
276 // this (intentionally) does neither touch anchor nor selection status
277 void Cursor::setCursor(DocIterator const & cur)
279 DocIterator::operator=(cur);
283 bool Cursor::getStatus(FuncRequest const & cmd, FuncStatus & status) const
287 // Try to fix cursor in case it is broken.
290 // Is this a function that acts on inset at point?
291 Inset * inset = cur.nextInset();
292 if (lyxaction.funcHasFlag(cmd.action(), LyXAction::AtPoint)
293 && inset && inset->getStatus(cur, cmd, status))
296 // This is, of course, a mess. Better create a new doc iterator and use
297 // this in Inset::getStatus. This might require an additional
298 // BufferView * arg, though (which should be avoided)
299 //Cursor safe = *this;
301 for ( ; cur.depth(); cur.pop()) {
302 //lyxerr << "\nCursor::getStatus: cmd: " << cmd << endl << *this << endl;
303 LASSERT(cur.idx() <= cur.lastidx(), /**/);
304 LASSERT(cur.pit() <= cur.lastpit(), /**/);
305 LASSERT(cur.pos() <= cur.lastpos(), /**/);
307 // The inset's getStatus() will return 'true' if it made
308 // a definitive decision on whether it want to handle the
309 // request or not. The result of this decision is put into
310 // the 'status' parameter.
311 if (cur.inset().getStatus(cur, cmd, status)) {
320 void Cursor::saveBeforeDispatchPosXY()
322 getPos(beforeDispatchPosX_, beforeDispatchPosY_);
326 void Cursor::dispatch(FuncRequest const & cmd0)
328 LYXERR(Debug::DEBUG, "cmd: " << cmd0 << '\n' << *this);
333 FuncRequest cmd = cmd0;
336 buffer()->undo().beginUndoGroup();
338 // Is this a function that acts on inset at point?
339 if (lyxaction.funcHasFlag(cmd.action(), LyXAction::AtPoint)
341 disp_.dispatched(true);
342 disp_.update(Update::FitCursor | Update::Force);
343 FuncRequest tmpcmd = cmd;
344 LYXERR(Debug::DEBUG, "Cursor::dispatch: (AtPoint) cmd: "
345 << cmd0 << endl << *this);
346 nextInset()->dispatch(*this, tmpcmd);
347 if (disp_.dispatched()) {
348 buffer()->undo().endUndoGroup();
353 // store some values to be used inside of the handlers
354 beforeDispatchCursor_ = *this;
355 for (; depth(); pop(), boundary(false)) {
356 LYXERR(Debug::DEBUG, "Cursor::dispatch: cmd: "
357 << cmd0 << endl << *this);
358 LASSERT(pos() <= lastpos(), /**/);
359 LASSERT(idx() <= lastidx(), /**/);
360 LASSERT(pit() <= lastpit(), /**/);
362 // The common case is 'LFUN handled, need update', so make the
363 // LFUN handler's life easier by assuming this as default value.
364 // The handler can reset the update and val flags if necessary.
365 disp_.update(Update::FitCursor | Update::Force);
366 disp_.dispatched(true);
367 inset().dispatch(*this, cmd);
368 if (disp_.dispatched())
372 // it completely to get a 'bomb early' behaviour in case this
373 // object will be used again.
374 if (!disp_.dispatched()) {
375 LYXERR(Debug::DEBUG, "RESTORING OLD CURSOR!");
376 // We might have invalidated the cursor when removing an empty
377 // paragraph while the cursor could not be moved out the inset
378 // while we initially thought we could. This might happen when
379 // a multiline inset becomes an inline inset when the second
380 // paragraph is removed.
381 if (safe.pit() > safe.lastpit()) {
382 safe.pit() = safe.lastpit();
383 safe.pos() = safe.lastpos();
386 disp_.update(Update::None);
387 disp_.dispatched(false);
389 // restore the previous one because nested Cursor::dispatch calls
390 // are possible which would change it
391 beforeDispatchCursor_ = safe.beforeDispatchCursor_;
393 buffer()->undo().endUndoGroup();
397 DispatchResult const & Cursor::result() const
403 BufferView & Cursor::bv() const
412 LASSERT(depth() >= 1, /**/);
417 void Cursor::push(Inset & p)
419 push_back(CursorSlice(p));
420 p.setBuffer(*buffer());
424 void Cursor::pushBackward(Inset & p)
426 LASSERT(!empty(), return);
427 //lyxerr << "Entering inset " << t << " front" << endl;
433 bool Cursor::popBackward()
435 LASSERT(!empty(), return false);
443 bool Cursor::popForward()
445 LASSERT(!empty(), return false);
446 //lyxerr << "Leaving inset from in back" << endl;
447 const pos_type lp = (depth() > 1) ? (*this)[depth() - 2].lastpos() : 0;
451 pos() += lastpos() - lp + 1;
456 int Cursor::currentMode()
458 LASSERT(!empty(), /**/);
459 for (int i = depth() - 1; i >= 0; --i) {
460 int res = operator[](i).inset().currentMode();
461 bool locked_mode = operator[](i).inset().lockedMode();
462 // Also return UNDECIDED_MODE when the mode is locked,
463 // as in this case it is treated the same as TEXT_MODE
464 if (res != Inset::UNDECIDED_MODE || locked_mode)
467 return Inset::TEXT_MODE;
471 void Cursor::getPos(int & x, int & y) const
473 Point p = bv().getPos(*this);
479 Row const & Cursor::textRow() const
481 CursorSlice const & cs = innerTextSlice();
482 ParagraphMetrics const & pm = bv().parMetrics(cs.text(), cs.pit());
483 LASSERT(!pm.rows().empty(), /**/);
484 return pm.getRow(pos(), boundary());
488 void Cursor::resetAnchor()
494 void Cursor::setCursorToAnchor()
497 DocIterator normal = anchor_;
498 while (depth() < normal.depth())
500 if (depth() < anchor_.depth() && top() <= anchor_[depth() - 1])
507 bool Cursor::posBackward()
516 bool Cursor::posForward()
518 if (pos() == lastpos())
525 bool Cursor::posVisRight(bool skip_inset)
527 Cursor new_cur = *this; // where we will move to
528 pos_type left_pos; // position visually left of current cursor
529 pos_type right_pos; // position visually right of current cursor
530 bool new_pos_is_RTL; // is new position we're moving to RTL?
532 getSurroundingPos(left_pos, right_pos);
534 LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
536 // Are we at an inset?
537 new_cur.pos() = right_pos;
538 new_cur.boundary(false);
540 text()->checkAndActivateInsetVisual(new_cur, right_pos >= pos(), false)) {
541 // we actually move the cursor at the end of this function, for now
542 // we just keep track of the new position in new_cur...
543 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
546 // Are we already at rightmost pos in row?
547 else if (text()->empty() || right_pos == -1) {
550 if (!new_cur.posVisToNewRow(false)) {
551 LYXERR(Debug::RTL, "not moving!");
555 // we actually move the cursor at the end of this function, for now
556 // just keep track of the new position in new_cur...
557 LYXERR(Debug::RTL, "right edge, moving: " << int(new_cur.pit()) << ","
558 << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
561 // normal movement to the right
564 // Recall, if the cursor is at position 'x', that means *before*
565 // the character at position 'x'. In RTL, "before" means "to the
566 // right of", in LTR, "to the left of". So currently our situation
567 // is this: the position to our right is 'right_pos' (i.e., we're
568 // currently to the left of 'right_pos'). In order to move to the
569 // right, it depends whether or not the character at 'right_pos' is RTL.
570 new_pos_is_RTL = paragraph().getFontSettings(
571 buffer()->params(), right_pos).isVisibleRightToLeft();
572 // If the character at 'right_pos' *is* LTR, then in order to move to
573 // the right of it, we need to be *after* 'right_pos', i.e., move to
574 // position 'right_pos' + 1.
575 if (!new_pos_is_RTL) {
576 new_cur.pos() = right_pos + 1;
577 // set the boundary to true in two situations:
579 // 1. if new_pos is now lastpos, and we're in an RTL paragraph
580 // (this means that we're moving right to the end of an LTR chunk
581 // which is at the end of an RTL paragraph);
582 (new_cur.pos() == lastpos()
583 && paragraph().isRTL(buffer()->params()))
584 // 2. if the position *after* right_pos is RTL (we want to be
585 // *after* right_pos, not before right_pos + 1!)
586 || paragraph().getFontSettings(buffer()->params(),
587 new_cur.pos()).isVisibleRightToLeft()
589 new_cur.boundary(true);
590 else // set the boundary to false
591 new_cur.boundary(false);
593 // Otherwise (if the character at position 'right_pos' is RTL), then
594 // moving to the right of it is as easy as setting the new position
597 new_cur.pos() = right_pos;
598 new_cur.boundary(false);
603 bool moved = (new_cur.pos() != pos()
604 || new_cur.pit() != pit()
605 || new_cur.boundary() != boundary()
606 || &new_cur.inset() != &inset());
609 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
610 << (new_cur.boundary() ? " (boundary)" : ""));
618 bool Cursor::posVisLeft(bool skip_inset)
620 Cursor new_cur = *this; // where we will move to
621 pos_type left_pos; // position visually left of current cursor
622 pos_type right_pos; // position visually right of current cursor
623 bool new_pos_is_RTL; // is new position we're moving to RTL?
625 getSurroundingPos(left_pos, right_pos);
627 LYXERR(Debug::RTL, left_pos <<"|"<< right_pos << " (pos: "<< pos() <<")");
629 // Are we at an inset?
630 new_cur.pos() = left_pos;
631 new_cur.boundary(false);
633 text()->checkAndActivateInsetVisual(new_cur, left_pos >= pos(), true)) {
634 // we actually move the cursor at the end of this function, for now
635 // we just keep track of the new position in new_cur...
636 LYXERR(Debug::RTL, "entering inset at: " << new_cur.pos());
639 // Are we already at leftmost pos in row?
640 else if (text()->empty() || left_pos == -1) {
643 if (!new_cur.posVisToNewRow(true)) {
644 LYXERR(Debug::RTL, "not moving!");
648 // we actually move the cursor at the end of this function, for now
649 // just keep track of the new position in new_cur...
650 LYXERR(Debug::RTL, "left edge, moving: " << int(new_cur.pit()) << ","
651 << int(new_cur.pos()) << "," << (new_cur.boundary() ? 1 : 0));
654 // normal movement to the left
657 // Recall, if the cursor is at position 'x', that means *before*
658 // the character at position 'x'. In RTL, "before" means "to the
659 // right of", in LTR, "to the left of". So currently our situation
660 // is this: the position to our left is 'left_pos' (i.e., we're
661 // currently to the right of 'left_pos'). In order to move to the
662 // left, it depends whether or not the character at 'left_pos' is RTL.
663 new_pos_is_RTL = paragraph().getFontSettings(
664 buffer()->params(), left_pos).isVisibleRightToLeft();
665 // If the character at 'left_pos' *is* RTL, then in order to move to
666 // the left of it, we need to be *after* 'left_pos', i.e., move to
667 // position 'left_pos' + 1.
668 if (new_pos_is_RTL) {
669 new_cur.pos() = left_pos + 1;
670 // set the boundary to true in two situations:
672 // 1. if new_pos is now lastpos and we're in an LTR paragraph
673 // (this means that we're moving left to the end of an RTL chunk
674 // which is at the end of an LTR paragraph);
675 (new_cur.pos() == lastpos()
676 && !paragraph().isRTL(buffer()->params()))
677 // 2. if the position *after* left_pos is not RTL (we want to be
678 // *after* left_pos, not before left_pos + 1!)
679 || !paragraph().getFontSettings(buffer()->params(),
680 new_cur.pos()).isVisibleRightToLeft()
682 new_cur.boundary(true);
683 else // set the boundary to false
684 new_cur.boundary(false);
686 // Otherwise (if the character at position 'left_pos' is LTR), then
687 // moving to the left of it is as easy as setting the new position
690 new_cur.pos() = left_pos;
691 new_cur.boundary(false);
696 bool moved = (new_cur.pos() != pos()
697 || new_cur.pit() != pit()
698 || new_cur.boundary() != boundary());
701 LYXERR(Debug::RTL, "moving to: " << new_cur.pos()
702 << (new_cur.boundary() ? " (boundary)" : ""));
710 void Cursor::getSurroundingPos(pos_type & left_pos, pos_type & right_pos)
712 // preparing bidi tables
713 Paragraph const & par = paragraph();
714 Buffer const & buf = *buffer();
715 Row const & row = textRow();
717 bidi.computeTables(par, buf, row);
719 LYXERR(Debug::RTL, "bidi: " << row.pos() << "--" << row.endpos());
721 // The cursor is painted *before* the character at pos(), or, if 'boundary'
722 // is true, *after* the character at (pos() - 1). So we already have one
723 // known position around the cursor:
724 pos_type const known_pos = boundary() && pos() > 0 ? pos() - 1 : pos();
726 // edge case: if we're at the end of the paragraph, things are a little
727 // different (because lastpos is a position which does not really "exist"
728 // --- there's no character there yet).
729 if (known_pos == lastpos()) {
730 if (par.isRTL(buf.params())) {
732 right_pos = bidi.vis2log(row.pos());
736 left_pos = bidi.vis2log(row.endpos() - 1);
741 // Whether 'known_pos' is to the left or to the right of the cursor depends
742 // on whether it is an RTL or LTR character...
743 bool const cur_is_RTL =
744 par.getFontSettings(buf.params(), known_pos).isVisibleRightToLeft();
745 // ... in the following manner:
746 // For an RTL character, "before" means "to the right" and "after" means
747 // "to the left"; and for LTR, it's the reverse. So, 'known_pos' is to the
748 // right of the cursor if (RTL && boundary) or (!RTL && !boundary):
749 bool const known_pos_on_right = cur_is_RTL == boundary();
751 // So we now know one of the positions surrounding the cursor. Let's
752 // determine the other one:
753 if (known_pos_on_right) {
754 right_pos = known_pos;
755 // *visual* position of 'left_pos':
756 pos_type v_left_pos = bidi.log2vis(right_pos) - 1;
757 // If the position we just identified as 'left_pos' is a "skipped
758 // separator" (a separator which is at the logical end of a row,
759 // except for the last row in a paragraph; such separators are not
760 // painted, so they "are not really there"; note that in bidi text,
761 // such a separator could appear visually in the middle of a row),
762 // set 'left_pos' to the *next* position to the left.
763 if (bidi.inRange(v_left_pos)
764 && bidi.vis2log(v_left_pos) + 1 == row.endpos()
765 && row.endpos() < lastpos()
766 && par.isSeparator(bidi.vis2log(v_left_pos)))
769 // calculate the logical position of 'left_pos', if in row
770 if (!bidi.inRange(v_left_pos))
773 left_pos = bidi.vis2log(v_left_pos);
774 // If the position we identified as 'right_pos' is a "skipped
775 // separator", set 'right_pos' to the *next* position to the right.
776 if (right_pos + 1 == row.endpos() && row.endpos() < lastpos()
777 && par.isSeparator(right_pos)) {
778 pos_type const v_right_pos = bidi.log2vis(right_pos) + 1;
779 if (!bidi.inRange(v_right_pos))
782 right_pos = bidi.vis2log(v_right_pos);
785 // known_pos is on the left
786 left_pos = known_pos;
787 // *visual* position of 'right_pos'
788 pos_type v_right_pos = bidi.log2vis(left_pos) + 1;
789 // If the position we just identified as 'right_pos' is a "skipped
790 // separator", set 'right_pos' to the *next* position to the right.
791 if (bidi.inRange(v_right_pos)
792 && bidi.vis2log(v_right_pos) + 1 == row.endpos()
793 && row.endpos() < lastpos()
794 && par.isSeparator(bidi.vis2log(v_right_pos)))
797 // calculate the logical position of 'right_pos', if in row
798 if (!bidi.inRange(v_right_pos))
801 right_pos = bidi.vis2log(v_right_pos);
802 // If the position we identified as 'left_pos' is a "skipped
803 // separator", set 'left_pos' to the *next* position to the left.
804 if (left_pos + 1 == row.endpos() && row.endpos() < lastpos()
805 && par.isSeparator(left_pos)) {
806 pos_type const v_left_pos = bidi.log2vis(left_pos) - 1;
807 if (!bidi.inRange(v_left_pos))
810 left_pos = bidi.vis2log(v_left_pos);
817 bool Cursor::posVisToNewRow(bool movingLeft)
819 Paragraph const & par = paragraph();
820 Buffer const & buf = *buffer();
821 Row const & row = textRow();
822 bool par_is_LTR = !par.isRTL(buf.params());
824 // Inside a table, determining whether to move to the next or previous row
825 // should be done based on the table's direction.
827 if (s >= 1 && (*this)[s].inset().asInsetTabular()) {
828 par_is_LTR = !(*this)[s].inset().asInsetTabular()->isRightToLeft(*this);
829 LYXERR(Debug::RTL, "Inside table! par_is_LTR=" << (par_is_LTR ? 1 : 0));
832 // if moving left in an LTR paragraph or moving right in an RTL one,
833 // move to previous row
834 if (par_is_LTR == movingLeft) {
835 if (row.pos() == 0) { // we're at first row in paragraph
836 if (pit() == 0) // no previous paragraph! don't move
838 // move to last pos in previous par
842 } else { // move to previous row in this par
843 pos() = row.pos() - 1; // this is guaranteed to be in previous row
847 // if moving left in an RTL paragraph or moving right in an LTR one,
850 if (row.endpos() == lastpos()) { // we're at last row in paragraph
851 if (pit() == lastpit()) // last paragraph! don't move
853 // move to first row in next par
857 } else { // move to next row in this par
858 pos() = row.endpos();
863 // make sure we're at left-/right-most pos in new row
864 posVisToRowExtremity(!movingLeft);
870 void Cursor::posVisToRowExtremity(bool left)
872 // prepare bidi tables
873 Paragraph const & par = paragraph();
874 Buffer const & buf = *buffer();
875 Row const & row = textRow();
877 bidi.computeTables(par, buf, row);
879 LYXERR(Debug::RTL, "entering extremity: " << pit() << "," << pos() << ","
880 << (boundary() ? 1 : 0));
882 if (left) { // move to leftmost position
883 // if this is an RTL paragraph, and we're at the last row in the
884 // paragraph, move to lastpos
885 if (par.isRTL(buf.params()) && row.endpos() == lastpos())
888 pos() = bidi.vis2log(row.pos());
890 // Moving to the leftmost position in the row, the cursor should
891 // normally be placed to the *left* of the leftmost position.
892 // A very common exception, though, is if the leftmost character
893 // also happens to be the separator at the (logical) end of the row
894 // --- in this case, the separator is positioned beyond the left
895 // margin, and we don't want to move the cursor there (moving to
896 // the left of the separator is equivalent to moving to the next
897 // line). So, in this case we actually want to place the cursor
898 // to the *right* of the leftmost position (the separator).
899 // Another exception is if we're moving to the logically last
900 // position in the row, which is *not* a separator: this means
901 // that the entire row has no separators (if there were any, the
902 // row would have been broken there); and therefore in this case
903 // we also move to the *right* of the last position (this indicates
904 // to the user that there is no space after this position, and is
905 // consistent with the behavior in the middle of a row --- moving
906 // right or left moves to the next/previous character; if we were
907 // to move to the *left* of this position, that would simulate
908 // a separator which is not really there!).
909 // Finally, there is an exception to the previous exception: if
910 // this non-separator-but-last-position-in-row is an inset, then
911 // we *do* want to stay to the left of it anyway: this is the
912 // "boundary" which we simulate at insets.
913 // Another exception is when row.endpos() is 0.
915 // do we want to be to the right of pos?
916 // as explained above, if at last pos in row, stay to the right
917 bool const right_of_pos = row.endpos() > 0
918 && pos() == row.endpos() - 1 && !par.isInset(pos());
920 // Now we know if we want to be to the left or to the right of pos,
921 // let's make sure we are where we want to be.
922 bool const new_pos_is_RTL =
923 par.getFontSettings(buf.params(), pos()).isVisibleRightToLeft();
925 if (new_pos_is_RTL != right_of_pos) {
931 // move to rightmost position
932 // if this is an LTR paragraph, and we're at the last row in the
933 // paragraph, move to lastpos
934 if (!par.isRTL(buf.params()) && row.endpos() == lastpos())
937 pos() = row.endpos() > 0 ? bidi.vis2log(row.endpos() - 1) : 0;
939 // Moving to the rightmost position in the row, the cursor should
940 // normally be placed to the *right* of the rightmost position.
941 // A very common exception, though, is if the rightmost character
942 // also happens to be the separator at the (logical) end of the row
943 // --- in this case, the separator is positioned beyond the right
944 // margin, and we don't want to move the cursor there (moving to
945 // the right of the separator is equivalent to moving to the next
946 // line). So, in this case we actually want to place the cursor
947 // to the *left* of the rightmost position (the separator).
948 // Another exception is if we're moving to the logically last
949 // position in the row, which is *not* a separator: this means
950 // that the entire row has no separators (if there were any, the
951 // row would have been broken there); and therefore in this case
952 // we also move to the *left* of the last position (this indicates
953 // to the user that there is no space after this position, and is
954 // consistent with the behavior in the middle of a row --- moving
955 // right or left moves to the next/previous character; if we were
956 // to move to the *right* of this position, that would simulate
957 // a separator which is not really there!).
958 // Finally, there is an exception to the previous exception: if
959 // this non-separator-but-last-position-in-row is an inset, then
960 // we *do* want to stay to the right of it anyway: this is the
961 // "boundary" which we simulate at insets.
962 // Another exception is when row.endpos() is 0.
964 // do we want to be to the left of pos?
965 // as explained above, if at last pos in row, stay to the left,
966 // unless the last position is the same as the first.
967 bool const left_of_pos = row.endpos() > 0
968 && pos() == row.endpos() - 1 && !par.isInset(pos());
970 // Now we know if we want to be to the left or to the right of pos,
971 // let's make sure we are where we want to be.
972 bool const new_pos_is_RTL =
973 par.getFontSettings(buf.params(), pos()).isVisibleRightToLeft();
975 if (new_pos_is_RTL == left_of_pos) {
981 LYXERR(Debug::RTL, "leaving extremity: " << pit() << "," << pos() << ","
982 << (boundary() ? 1 : 0));
986 CursorSlice Cursor::normalAnchor() const
990 LASSERT(anchor_.depth() >= depth(), /**/);
991 CursorSlice normal = anchor_[depth() - 1];
992 if (depth() < anchor_.depth() && top() <= normal) {
993 // anchor is behind cursor -> move anchor behind the inset
1000 DocIterator & Cursor::realAnchor()
1006 CursorSlice Cursor::selBegin() const
1010 return normalAnchor() < top() ? normalAnchor() : top();
1014 CursorSlice Cursor::selEnd() const
1018 return normalAnchor() > top() ? normalAnchor() : top();
1022 DocIterator Cursor::selectionBegin() const
1028 // FIXME: This is a work-around for the problem that
1029 // CursorSlice doesn't keep track of the boundary.
1030 if (normalAnchor() == top())
1031 di = anchor_.boundary() > boundary() ? anchor_ : *this;
1033 di = normalAnchor() < top() ? anchor_ : *this;
1039 DocIterator Cursor::selectionEnd() const
1045 // FIXME: This is a work-around for the problem that
1046 // CursorSlice doesn't keep track of the boundary.
1047 if (normalAnchor() == top())
1048 di = anchor_.boundary() < boundary() ? anchor_ : *this;
1050 di = normalAnchor() > top() ? anchor_ : *this;
1052 if (di.depth() > depth()) {
1060 void Cursor::setSelection()
1063 // A selection with no contents is not a selection
1064 // FIXME: doesnt look ok
1065 if (idx() == normalAnchor().idx() &&
1066 pit() == normalAnchor().pit() &&
1067 pos() == normalAnchor().pos())
1068 setSelection(false);
1072 void Cursor::setSelection(DocIterator const & where, int n)
1081 void Cursor::clearSelection()
1083 setSelection(false);
1084 setWordSelection(false);
1090 void Cursor::setTargetX(int x)
1093 textTargetOffset_ = 0;
1097 int Cursor::x_target() const
1103 void Cursor::clearTargetX()
1106 textTargetOffset_ = 0;
1110 void Cursor::updateTextTargetOffset()
1115 textTargetOffset_ = x - x_target_;
1119 void Cursor::info(odocstream & os) const
1121 for (int i = 1, n = depth(); i < n; ++i) {
1122 operator[](i).inset().infoize(os);
1126 Inset const * inset = prevInset();
1127 // prevInset() can return 0 in certain case.
1129 prevInset()->infoize2(os);
1131 // overwite old message
1136 bool Cursor::selHandle(bool sel)
1138 //lyxerr << "Cursor::selHandle" << endl;
1141 if (sel == selection())
1145 cap::saveSelection(*this);
1153 ostream & operator<<(ostream & os, Cursor const & cur)
1155 os << "\n cursor: | anchor:\n";
1156 for (size_t i = 0, n = cur.depth(); i != n; ++i) {
1157 os << " " << cur[i] << " | ";
1158 if (i < cur.anchor_.depth())
1159 os << cur.anchor_[i];
1161 os << "-------------------------------";
1164 for (size_t i = cur.depth(), n = cur.anchor_.depth(); i < n; ++i) {
1165 os << "------------------------------- | " << cur.anchor_[i] << "\n";
1167 os << " selection: " << cur.selection_
1168 << " x_target: " << cur.x_target_ << endl;
1173 LyXErr & operator<<(LyXErr & os, Cursor const & cur)
1183 ///////////////////////////////////////////////////////////////////
1186 // The part below is the non-integrated rest of the original math
1187 // cursor. This should be either generalized for texted or moved
1188 // back to mathed (in most cases to InsetMathNest).
1190 ///////////////////////////////////////////////////////////////////
1192 #include "mathed/InsetMathChar.h"
1193 #include "mathed/InsetMathGrid.h"
1194 #include "mathed/InsetMathScript.h"
1195 #include "mathed/InsetMathUnknown.h"
1196 #include "mathed/MathFactory.h"
1197 #include "mathed/MathStream.h"
1198 #include "mathed/MathSupport.h"
1203 //#define FILEDEBUG 1
1206 bool Cursor::isInside(Inset const * p) const
1208 for (size_t i = 0; i != depth(); ++i)
1209 if (&operator[](i).inset() == p)
1215 void Cursor::leaveInset(Inset const & inset)
1217 for (size_t i = 0; i != depth(); ++i) {
1218 if (&operator[](i).inset() == &inset) {
1226 bool Cursor::openable(MathAtom const & t) const
1237 // we can't move into anything new during selection
1238 if (depth() >= anchor_.depth())
1240 if (t.nucleus() != &anchor_[depth()].inset())
1247 void Cursor::setScreenPos(int x, int /*y*/)
1250 //bruteFind(*this, x, y, 0, bv().workWidth(), 0, bv().workHeight());
1255 void Cursor::plainErase()
1257 cell().erase(pos());
1261 void Cursor::markInsert()
1263 insert(char_type(0));
1267 void Cursor::markErase()
1269 cell().erase(pos());
1273 void Cursor::plainInsert(MathAtom const & t)
1275 cell().insert(pos(), t);
1277 inset().setBuffer(bv_->buffer());
1279 forceBufferUpdate();
1283 void Cursor::insert(docstring const & str)
1285 for_each(str.begin(), str.end(),
1286 bind(static_cast<void(Cursor::*)(char_type)>
1287 (&Cursor::insert), this, _1));
1291 void Cursor::insert(char_type c)
1293 //lyxerr << "Cursor::insert char '" << c << "'" << endl;
1294 LASSERT(!empty(), /**/);
1296 cap::selClearOrDel(*this);
1297 insert(new InsetMathChar(c));
1299 text()->insertChar(*this, c);
1304 void Cursor::insert(MathAtom const & t)
1306 //lyxerr << "Cursor::insert MathAtom '" << t << "'" << endl;
1308 cap::selClearOrDel(*this);
1313 void Cursor::insert(Inset * inset0)
1315 LASSERT(inset0, /**/);
1317 insert(MathAtom(inset0));
1319 text()->insertInset(*this, inset0);
1320 inset0->setBuffer(bv_->buffer());
1322 if (inset0->isLabeled())
1323 forceBufferUpdate();
1328 void Cursor::niceInsert(docstring const & t, Parse::flags f, bool enter)
1330 MathData ar(buffer());
1332 if (ar.size() == 1 && (enter || selection()))
1339 void Cursor::niceInsert(MathAtom const & t)
1342 docstring const safe = cap::grabAndEraseSelection(*this);
1344 // If possible, enter the new inset and move the contents of the selection
1345 if (t->isActive()) {
1347 // be careful here: don't use 'pushBackward(t)' as this we need to
1348 // push the clone, not the original
1349 pushBackward(*nextInset());
1350 // We may not use niceInsert here (recursion)
1351 MathData ar(buffer());
1358 void Cursor::insert(MathData const & ar)
1362 cap::eraseSelection(*this);
1363 cell().insert(pos(), ar);
1365 // FIXME audit setBuffer calls
1366 inset().setBuffer(bv_->buffer());
1370 bool Cursor::backspace()
1372 autocorrect() = false;
1375 cap::eraseSelection(*this);
1380 // If empty cell, and not part of a big cell
1381 if (lastpos() == 0 && inset().nargs() == 1) {
1383 // Directly delete empty cell: [|[]] => [|]
1389 // [|], can not delete from inside
1400 if (inMacroMode()) {
1401 InsetMathUnknown * p = activeMacro();
1402 if (p->name().size() > 1) {
1403 p->setName(p->name().substr(0, p->name().size() - 1));
1408 if (pos() != 0 && prevAtom()->nargs() > 0) {
1409 // let's require two backspaces for 'big stuff' and
1410 // highlight on the first
1422 bool Cursor::erase()
1424 autocorrect() = false;
1429 cap::eraseSelection(*this);
1433 // delete empty cells if possible
1434 if (pos() == lastpos() && inset().idxDelete(idx()))
1437 // special behaviour when in last position of cell
1438 if (pos() == lastpos()) {
1439 bool one_cell = inset().nargs() == 1;
1440 if (one_cell && lastpos() == 0) {
1442 // Directly delete empty cell: [|[]] => [|]
1448 // [|], can not delete from inside
1453 inset().idxGlue(idx());
1457 // 'clever' UI hack: only erase large items if previously slected
1458 if (pos() != lastpos() && nextAtom()->nargs() > 0) {
1473 DocIterator save = *this;
1474 FuncRequest cmd(selection() ? LFUN_UP_SELECT : LFUN_UP, docstring());
1475 this->dispatch(cmd);
1476 if (disp_.dispatched())
1479 autocorrect() = false;
1487 DocIterator save = *this;
1488 FuncRequest cmd(selection() ? LFUN_DOWN_SELECT : LFUN_DOWN, docstring());
1489 this->dispatch(cmd);
1490 if (disp_.dispatched())
1493 autocorrect() = false;
1498 bool Cursor::macroModeClose()
1502 InsetMathUnknown * p = activeMacro();
1504 MathData selection(buffer());
1505 asArray(p->selection(), selection);
1506 docstring const s = p->name();
1508 cell().erase(pos());
1510 // do nothing if the macro name is empty
1514 // trigger updates of macros, at least, if no full
1515 // updates take place anyway
1516 screenUpdateFlags(Update::Force);
1518 docstring const name = s.substr(1);
1519 InsetMathNest * const in = inset().asInsetMath()->asNestInset();
1520 if (in && in->interpretString(*this, s))
1522 MathAtom atom = buffer()->getMacro(name, *this, false) ?
1523 MathAtom(new MathMacro(buffer(), name)) : createInsetMath(name, buffer());
1525 // try to put argument into macro, if we just inserted a macro
1526 bool macroArg = false;
1527 MathMacro * atomAsMacro = atom.nucleus()->asMacro();
1529 // macros here are still unfolded (in init mode in fact). So
1530 // we have to resolve the macro here manually and check its arity
1531 // to put the selection behind it if arity > 0.
1532 MacroData const * data = buffer()->getMacro(atomAsMacro->name());
1533 if (selection.size() > 0 && data && data->numargs() - data->optionals() > 0) {
1535 atomAsMacro->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT, 1);
1537 // non-greedy case. Do not touch the arguments behind
1538 atomAsMacro->setDisplayMode(MathMacro::DISPLAY_INTERACTIVE_INIT, 0);
1541 // insert remembered selection into first argument of a non-macro
1542 else if (atom.nucleus()->nargs() > 0)
1543 atom.nucleus()->cell(0).append(selection);
1547 // finally put the macro argument behind, if needed
1549 if (selection.size() > 1 || selection[0]->asScriptInset())
1550 plainInsert(MathAtom(new InsetMathBrace(selection)));
1559 docstring Cursor::macroName()
1561 return inMacroMode() ? activeMacro()->name() : docstring();
1565 void Cursor::handleNest(MathAtom const & a, int c)
1567 //lyxerr << "Cursor::handleNest: " << c << endl;
1569 asArray(cap::grabAndEraseSelection(*this), t.nucleus()->cell(c));
1572 pushBackward(*nextInset());
1576 int Cursor::targetX() const
1578 if (x_target() != -1)
1587 int Cursor::textTargetOffset() const
1589 return textTargetOffset_;
1593 void Cursor::setTargetX()
1602 bool Cursor::inMacroMode() const
1608 InsetMathUnknown const * p = prevAtom()->asUnknownInset();
1609 return p && !p->final();
1613 InsetMathUnknown * Cursor::activeMacro()
1615 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1619 InsetMathUnknown const * Cursor::activeMacro() const
1621 return inMacroMode() ? prevAtom().nucleus()->asUnknownInset() : 0;
1625 void Cursor::pullArg()
1628 MathData ar = cell();
1629 if (popBackward() && inMathed()) {
1631 cell().insert(pos(), ar);
1634 //formula()->mutateToText();
1639 void Cursor::touch()
1643 DocIterator::const_iterator it = begin();
1644 DocIterator::const_iterator et = end();
1645 for ( ; it != et; ++it)
1651 void Cursor::normalize()
1653 if (idx() > lastidx()) {
1654 lyxerr << "this should not really happen - 1: "
1655 << idx() << ' ' << nargs()
1656 << " in: " << &inset() << endl;
1660 if (pos() > lastpos()) {
1661 lyxerr << "this should not really happen - 2: "
1662 << pos() << ' ' << lastpos() << " in idx: " << idx()
1664 odocstringstream os;
1665 WriteStream wi(os, false, true, WriteStream::wsDefault);
1666 inset().asInsetMath()->write(wi);
1667 lyxerr << to_utf8(os.str()) << endl;
1673 bool Cursor::upDownInMath(bool up)
1675 // Be warned: The 'logic' implemented in this function is highly
1676 // fragile. A distance of one pixel or a '<' vs '<=' _really
1677 // matters. So fiddle around with it only if you think you know
1678 // what you are doing!
1682 xo = beforeDispatchPosX_;
1684 // check if we had something else in mind, if not, this is the future
1686 if (x_target_ == -1)
1688 else if (inset().asInsetText() && xo - textTargetOffset() != x_target()) {
1689 // In text mode inside the line (not left or right) possibly set a new target_x,
1690 // but only if we are somewhere else than the previous target-offset.
1692 // We want to keep the x-target on subsequent up/down movements
1693 // that cross beyond the end of short lines. Thus a special
1694 // handling when the cursor is at the end of line: Use the new
1695 // x-target only if the old one was before the end of line
1696 // or the old one was after the beginning of the line
1697 bool inRTL = isWithinRtlParagraph(*this);
1701 left = pos() == textRow().endpos();
1702 right = pos() == textRow().pos();
1704 left = pos() == textRow().pos();
1705 right = pos() == textRow().endpos();
1707 if ((!left && !right) ||
1708 (left && !right && xo < x_target_) ||
1709 (!left && right && x_target_ < xo))
1716 // try neigbouring script insets
1718 if (inMathed() && !selection()) {
1721 InsetMathScript const * p = prevAtom()->asScriptInset();
1722 if (p && p->has(up)) {
1724 push(*const_cast<InsetMathScript*>(p));
1725 idx() = p->idxOfScript(up);
1728 // we went in the right direction? Otherwise don't jump into the script
1732 int oy = beforeDispatchPosY_;
1733 if ((!up && y <= oy) ||
1742 if (pos() != lastpos()) {
1743 InsetMathScript const * p = nextAtom()->asScriptInset();
1744 if (p && p->has(up)) {
1745 push(*const_cast<InsetMathScript*>(p));
1746 idx() = p->idxOfScript(up);
1749 // we went in the right direction? Otherwise don't jump into the script
1753 int oy = beforeDispatchPosY_;
1754 if ((!up && y <= oy) ||
1763 // try to find an inset that knows better then we,
1764 if (inset().idxUpDown(*this, up)) {
1765 //lyxerr << "idxUpDown triggered" << endl;
1766 // try to find best position within this inset
1768 setCursor(bruteFind2(*this, xo, yo));
1772 // any improvement going just out of inset?
1773 if (popBackward() && inMathed()) {
1774 //lyxerr << "updown: popBackward succeeded" << endl;
1777 int yold = beforeDispatchPosY_;
1779 if (up ? ynew < yold : ynew > yold)
1783 // no success, we are probably at the document top or bottom
1789 bool Cursor::atFirstOrLastRow(bool up)
1791 TextMetrics const & tm = bv_->textMetrics(text());
1792 ParagraphMetrics const & pm = tm.parMetrics(pit());
1795 if (pos() && boundary())
1796 row = pm.pos2row(pos() - 1);
1798 row = pm.pos2row(pos());
1801 if (pit() == 0 && row == 0)
1804 if (pit() + 1 >= int(text()->paragraphs().size()) &&
1805 row + 1 >= int(pm.rows().size()))
1811 bool Cursor::upDownInText(bool up, bool & updateNeeded)
1813 LASSERT(text(), /**/);
1819 xo = beforeDispatchPosX_;
1821 // update the targetX - this is here before the "return false"
1822 // to set a new target which can be used by InsetTexts above
1823 // if we cannot move up/down inside this inset anymore
1824 if (x_target_ == -1)
1826 else if (xo - textTargetOffset() != x_target() &&
1827 depth() == beforeDispatchCursor_.depth()) {
1828 // In text mode inside the line (not left or right) possibly set a new target_x,
1829 // but only if we are somewhere else than the previous target-offset.
1831 // We want to keep the x-target on subsequent up/down movements
1832 // that cross beyond the end of short lines. Thus a special
1833 // handling when the cursor is at the end of line: Use the new
1834 // x-target only if the old one was before the end of line
1835 // or the old one was after the beginning of the line
1836 bool inRTL = isWithinRtlParagraph(*this);
1840 left = pos() == textRow().endpos();
1841 right = pos() == textRow().pos();
1843 left = pos() == textRow().pos();
1844 right = pos() == textRow().endpos();
1846 if ((!left && !right) ||
1847 (left && !right && xo < x_target_) ||
1848 (!left && right && x_target_ < xo))
1855 // first get the current line
1856 TextMetrics & tm = bv_->textMetrics(text());
1857 ParagraphMetrics const & pm = tm.parMetrics(pit());
1859 if (pos() && boundary())
1860 row = pm.pos2row(pos() - 1);
1862 row = pm.pos2row(pos());
1864 if (atFirstOrLastRow(up)) {
1865 // Is there a place for the cursor to go ? If yes, we
1866 // can execute the DEPM, otherwise we should keep the
1867 // paragraph to host the cursor.
1868 Cursor dummy = *this;
1869 bool valid_destination = false;
1870 for(; dummy.depth(); dummy.pop())
1871 if (!dummy.atFirstOrLastRow(up)) {
1872 valid_destination = true;
1876 // will a next dispatch follow and if there is a new
1877 // dispatch will it move the cursor out ?
1878 if (depth() > 1 && valid_destination) {
1879 // The cursor hasn't changed yet. This happens when
1880 // you e.g. move out of an inset. And to give the
1881 // DEPM the possibility of doing something we must
1882 // provide it with two different cursors. (Lgb, vfr)
1884 dummy.pos() = dummy.pos() == 0 ? dummy.lastpos() : 0;
1885 dummy.pit() = dummy.pit() == 0 ? dummy.lastpit() : 0;
1887 updateNeeded |= bv().checkDepm(dummy, *this);
1888 updateTextTargetOffset();
1890 forceBufferUpdate();
1895 // with and without selection are handled differently
1897 int yo = bv().getPos(*this).y_;
1899 // To next/previous row
1901 tm.editXY(*this, xo, yo - textRow().ascent() - 1);
1903 tm.editXY(*this, xo, yo + textRow().descent() + 1);
1906 // This happens when you move out of an inset.
1907 // And to give the DEPM the possibility of doing
1908 // something we must provide it with two different
1910 Cursor dummy = *this;
1913 if (bv().checkDepm(dummy, old)) {
1914 updateNeeded = true;
1915 // Make sure that cur gets back whatever happened to dummy (Lgb)
1919 // if there is a selection, we stay out of any inset,
1920 // and just jump to the right position:
1926 } else if (pit() > 0) {
1928 TextMetrics & tm = bv_->textMetrics(text());
1929 if (!tm.contains(pit()))
1930 tm.newParMetricsUp();
1931 ParagraphMetrics const & pmcur = tm.parMetrics(pit());
1932 next_row = pmcur.rows().size() - 1;
1935 if (row + 1 < int(pm.rows().size())) {
1937 } else if (pit() + 1 < int(text()->paragraphs().size())) {
1939 TextMetrics & tm = bv_->textMetrics(text());
1940 if (!tm.contains(pit()))
1941 tm.newParMetricsDown();
1945 top().pos() = min(tm.x2pos(pit(), next_row, xo), top().lastpos());
1947 int const xpos = tm.x2pos(pit(), next_row, xo);
1948 bool const at_end_row = xpos == tm.x2pos(pit(), next_row, tm.width());
1949 bool const at_beg_row = xpos == tm.x2pos(pit(), next_row, 0);
1951 if (at_end_row && at_beg_row)
1952 // make sure the cursor ends up on this row
1955 boundary(at_end_row);
1957 updateNeeded |= bv().checkDepm(*this, old);
1961 forceBufferUpdate();
1962 updateTextTargetOffset();
1967 void Cursor::handleFont(string const & font)
1969 LYXERR(Debug::DEBUG, font);
1973 safe = cap::grabAndEraseSelection(*this);
1978 if (lastpos() != 0) {
1979 // something left in the cell
1981 // cursor in first position
1983 } else if (pos() == lastpos()) {
1984 // cursor in last position
1987 // cursor in between. split cell
1988 MathData::iterator bt = cell().begin();
1989 MathAtom at = createInsetMath(from_utf8(font), buffer());
1990 at.nucleus()->cell(0) = MathData(buffer(), bt, bt + pos());
1991 cell().erase(bt, bt + pos());
1996 // nothing left in the cell
2005 void Cursor::message(docstring const & msg) const
2007 disp_.setMessage(msg);
2011 void Cursor::errorMessage(docstring const & msg) const
2013 disp_.setMessage(msg);
2014 disp_.setError(true);
2018 static docstring parbreak(InsetCode code)
2020 odocstringstream os;
2022 // only add blank line if we're not in an ERT or Listings inset
2023 if (code != ERT_CODE && code != LISTINGS_CODE)
2029 docstring Cursor::selectionAsString(bool with_label) const
2035 return cap::grabSelection(*this);
2037 int const label = with_label
2038 ? AS_STR_LABEL | AS_STR_INSETS : AS_STR_INSETS;
2040 idx_type const startidx = selBegin().idx();
2041 idx_type const endidx = selEnd().idx();
2042 if (startidx != endidx) {
2043 // multicell selection
2044 InsetTabular * table = inset().asInsetTabular();
2045 LASSERT(table, return docstring());
2046 return table->asString(startidx, endidx);
2049 ParagraphList const & pars = text()->paragraphs();
2051 pit_type const startpit = selBegin().pit();
2052 pit_type const endpit = selEnd().pit();
2053 size_t const startpos = selBegin().pos();
2054 size_t const endpos = selEnd().pos();
2056 if (startpit == endpit)
2057 return pars[startpit].asString(startpos, endpos, label);
2059 // First paragraph in selection
2060 docstring result = pars[startpit].
2061 asString(startpos, pars[startpit].size(), label)
2062 + parbreak(inset().lyxCode());
2064 // The paragraphs in between (if any)
2065 for (pit_type pit = startpit + 1; pit != endpit; ++pit) {
2066 Paragraph const & par = pars[pit];
2067 result += par.asString(0, par.size(), label)
2068 + parbreak(inset().lyxCode());
2071 // Last paragraph in selection
2072 result += pars[endpit].asString(0, endpos, label);
2078 docstring Cursor::currentState() const
2081 odocstringstream os;
2087 return text()->currentState(*this);
2093 docstring Cursor::getPossibleLabel() const
2095 return inMathed() ? from_ascii("eq:") : text()->getPossibleLabel(*this);
2099 Encoding const * Cursor::getEncoding() const
2103 CursorSlice const & sl = innerTextSlice();
2104 Text const & text = *sl.text();
2105 Font font = text.getPar(sl.pit()).getFont(
2106 bv().buffer().params(), sl.pos(), text.outerFont(sl.pit()));
2107 return font.language()->encoding();
2111 void Cursor::undispatched()
2113 disp_.dispatched(false);
2117 void Cursor::dispatched()
2119 disp_.dispatched(true);
2123 void Cursor::screenUpdateFlags(Update::flags f)
2129 void Cursor::forceBufferUpdate()
2131 disp_.forceBufferUpdate();
2135 void Cursor::clearBufferUpdate()
2137 disp_.clearBufferUpdate();
2141 bool Cursor::needBufferUpdate() const
2143 return disp_.needBufferUpdate();
2147 void Cursor::noScreenUpdate()
2149 disp_.update(Update::None);
2153 Font Cursor::getFont() const
2155 // The logic here should more or less match to the Cursor::setCurrentFont
2156 // logic, i.e. the cursor height should give a hint what will happen
2157 // if a character is entered.
2159 // HACK. far from being perfect...
2161 CursorSlice const & sl = innerTextSlice();
2162 Text const & text = *sl.text();
2163 Paragraph const & par = text.getPar(sl.pit());
2165 // on boundary, so we are really at the character before
2166 pos_type pos = sl.pos();
2167 if (pos > 0 && boundary())
2170 // on space? Take the font before (only for RTL boundary stay)
2172 TextMetrics const & tm = bv().textMetrics(&text);
2173 if (pos == sl.lastpos()
2174 || (par.isSeparator(pos)
2175 && !tm.isRTLBoundary(sl.pit(), pos)))
2179 // get font at the position
2180 Font font = par.getFont(buffer()->params(), pos,
2181 text.outerFont(sl.pit()));
2187 bool Cursor::fixIfBroken()
2189 bool const broken_cursor = DocIterator::fixIfBroken();
2190 bool const broken_anchor = anchor_.fixIfBroken();
2192 if (broken_cursor || broken_anchor) {
2200 bool notifyCursorLeavesOrEnters(Cursor const & old, Cursor & cur)
2202 // find inset in common
2204 for (i = 0; i < old.depth() && i < cur.depth(); ++i) {
2205 if (&old[i].inset() != &cur[i].inset())
2209 // update words if we just moved to another paragraph
2210 if (i == old.depth() && i == cur.depth()
2211 && !cur.buffer()->isClean()
2212 && cur.inTexted() && old.inTexted()
2213 && cur.pit() != old.pit()) {
2214 old.paragraph().updateWords();
2217 // notify everything on top of the common part in old cursor,
2218 // but stop if the inset claims the cursor to be invalid now
2219 for (size_type j = i; j < old.depth(); ++j) {
2220 Cursor inset_pos = old;
2221 inset_pos.cutOff(j);
2222 if (old[j].inset().notifyCursorLeaves(inset_pos, cur))
2226 // notify everything on top of the common part in new cursor,
2227 // but stop if the inset claims the cursor to be invalid now
2228 for (; i < cur.depth(); ++i) {
2229 if (cur[i].inset().notifyCursorEnters(cur))
2237 void Cursor::setCurrentFont()
2239 CursorSlice const & cs = innerTextSlice();
2240 Paragraph const & par = cs.paragraph();
2241 pos_type cpit = cs.pit();
2242 pos_type cpos = cs.pos();
2243 Text const & ctext = *cs.text();
2244 TextMetrics const & tm = bv().textMetrics(&ctext);
2246 // are we behind previous char in fact? -> go to that char
2247 if (cpos > 0 && boundary())
2250 // find position to take the font from
2252 // paragraph end? -> font of last char
2253 if (cpos == lastpos())
2255 // on space? -> look at the words in front of space
2256 else if (cpos > 0 && par.isSeparator(cpos)) {
2257 // abc| def -> font of c
2258 // abc |[WERBEH], i.e. boundary==true -> font of c
2259 // abc [WERBEH]| def, font of the space
2260 if (!tm.isRTLBoundary(cpit, cpos))
2266 BufferParams const & bufparams = buffer()->params();
2267 current_font = par.getFontSettings(bufparams, cpos);
2268 real_current_font = tm.displayFont(cpit, cpos);
2270 // special case for paragraph end
2271 if (cs.pos() == lastpos()
2272 && tm.isRTLBoundary(cpit, cs.pos())
2274 Language const * lang = par.getParLanguage(bufparams);
2275 current_font.setLanguage(lang);
2276 current_font.fontInfo().setNumber(FONT_OFF);
2277 real_current_font.setLanguage(lang);
2278 real_current_font.fontInfo().setNumber(FONT_OFF);
2283 bool Cursor::textUndo()
2285 DocIterator dit = *this;
2286 // Undo::textUndo() will modify dit.
2287 if (!buffer()->undo().textUndo(dit))
2297 bool Cursor::textRedo()
2299 DocIterator dit = *this;
2300 // Undo::textRedo() will modify dit.
2301 if (!buffer()->undo().textRedo(dit))
2311 void Cursor::finishUndo() const
2313 buffer()->undo().finishUndo();
2317 void Cursor::beginUndoGroup() const
2319 buffer()->undo().beginUndoGroup();
2323 void Cursor::endUndoGroup() const
2325 buffer()->undo().endUndoGroup();
2329 void Cursor::recordUndo(UndoKind kind, pit_type from, pit_type to) const
2331 buffer()->undo().recordUndo(*this, kind, from, to);
2335 void Cursor::recordUndo(UndoKind kind, pit_type from) const
2337 buffer()->undo().recordUndo(*this, kind, from);
2341 void Cursor::recordUndo(UndoKind kind) const
2343 buffer()->undo().recordUndo(*this, kind);
2347 void Cursor::recordUndoInset(UndoKind kind) const
2349 buffer()->undo().recordUndoInset(*this, kind);
2353 void Cursor::recordUndoFullDocument() const
2355 buffer()->undo().recordUndoFullDocument(*this);
2359 void Cursor::recordUndoSelection() const
2362 if (cap::multipleCellsSelected(*this))
2367 buffer()->undo().recordUndo(*this, ATOMIC_UNDO,
2368 selBegin().pit(), selEnd().pit());
2373 void Cursor::checkBufferStructure()
2375 Buffer const * master = buffer()->masterBuffer();
2376 master->tocBackend().updateItem(*this);
2377 if (master != buffer() && !master->hasGuiDelegate())
2378 // In case the master has no gui associated with it,
2379 // the TocItem is not updated (part of bug 5699).
2380 buffer()->tocBackend().updateItem(*this);