2 * \file CursorSlice.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Matthias Ettrich
11 * Full author contact details are available in file CREDITS.
16 #include "CursorSlice.h"
19 #include "Paragraph.h"
21 #include "support/debug.h"
23 #include "insets/Inset.h"
25 #include "mathed/InsetMath.h"
26 #include "mathed/InsetMathMacro.h"
28 #include "support/ExceptionMessage.h"
29 #include "support/gettext.h"
30 #include "support/lassert.h"
39 CursorSlice::CursorSlice()
40 : inset_(nullptr), idx_(0), pit_(0), pos_(0)
44 CursorSlice::CursorSlice(Inset & p)
45 : inset_(&p), idx_(0), pit_(0), pos_(0)
51 MathData & CursorSlice::cell() const
53 return inset_->asInsetMath()->cell(idx_);
57 Paragraph & CursorSlice::paragraph() const
59 return text()->getPar(pit_);
63 pos_type CursorSlice::lastpos() const
66 InsetMath const * math = inset_->asInsetMath();
67 bool paramless_macro = math && math->asMacro() && !math->asMacro()->nargs();
68 return math ? (paramless_macro ? 0 : cell().size())
69 : (text()->empty() ? 0 : paragraph().size());
73 pit_type CursorSlice::lastpit() const
75 if (inset_->inMathed())
77 return text()->paragraphs().size() - 1;
81 CursorSlice::row_type CursorSlice::row() const
83 LASSERT(inset_, return 0);
84 return inset_->row(idx_);
88 CursorSlice::col_type CursorSlice::col() const
90 LASSERT(inset_, return 0);
91 return inset_->col(idx_);
95 void CursorSlice::setPitPos(pit_type pit, pos_type pos)
97 LASSERT(pit != int(text()->paragraphs().size()), return);
101 // Now some strict checking. None of these should happen, but
102 // we're scaredy-cats
104 LYXERR0("Don't like -1!");
108 if (pos > paragraph().size()) {
109 LYXERR0("Don't like 1, pos: " << pos
110 << " size: " << paragraph().size()
117 void CursorSlice::forwardPos()
119 // move on one position if possible
120 if (pos_ < lastpos()) {
121 //lyxerr << "... next pos" << endl;
126 // otherwise move on one paragraph if possible
127 if (pit_ < lastpit()) {
128 //lyxerr << "... next par" << endl;
134 // otherwise move on one cell
135 //lyxerr << "... next idx" << endl;
137 LASSERT(idx_ < nargs(), return);
145 void CursorSlice::forwardIdx()
147 LASSERT(idx_ < nargs(), return);
155 void CursorSlice::backwardPos()
179 bool CursorSlice::at_cell_end() const
181 return pit_ == lastpit() && pos_ == lastpos();
185 bool CursorSlice::at_cell_begin() const
187 return pit_ == 0 && pos_ == 0;
191 bool CursorSlice::at_end() const
193 return idx_ == lastidx() && at_cell_end();
197 bool CursorSlice::at_begin() const
199 return idx_ == 0 && at_cell_begin();
203 bool operator==(CursorSlice const & p, CursorSlice const & q)
205 return p.inset_ == q.inset_
212 bool operator!=(CursorSlice const & p, CursorSlice const & q)
214 return p.inset_ != q.inset_
221 bool operator<(CursorSlice const & p, CursorSlice const & q)
223 if (p.inset_ != q.inset_) {
224 LYXERR0("can't compare cursor and anchor in different insets\n"
225 << "p: " << p << '\n' << "q: " << q);
226 // It should be safe to continue, just registering the error.
227 LASSERT(false, return false);
229 if (p.idx_ != q.idx_)
230 return p.idx_ < q.idx_;
231 if (p.pit_ != q.pit_)
232 return p.pit_ < q.pit_;
233 return p.pos_ < q.pos_;
237 bool operator>(CursorSlice const & p, CursorSlice const & q)
243 bool operator<=(CursorSlice const & p, CursorSlice const & q)
249 ostream & operator<<(ostream & os, CursorSlice const & item)
252 << "inset: " << (void *)item.inset_
253 // << " text: " << item.text()
254 << " idx: " << item.idx_
255 << " par: " << item.pit_
256 << " pos: " << item.pos_
257 // << " x: " << item.inset_->x()
258 // << " y: " << item.inset_->y()