2 * \file DocIterator.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Alfredo Braunstein
9 * Full author contact details are available in file CREDITS.
15 #include "DocIterator.h"
18 #include "BufferParams.h"
21 #include "InsetList.h"
23 #include "Paragraph.h"
27 #include "mathed/MathData.h"
28 #include "mathed/InsetMath.h"
29 #include "mathed/InsetMathHull.h"
31 #include "insets/InsetTabular.h"
33 #include "support/convert.h"
34 #include "support/debug.h"
35 #include "support/ExceptionMessage.h"
36 #include "support/gettext.h"
37 #include "support/lassert.h"
38 #include "support/lstrings.h"
43 using namespace lyx::support;
48 DocIterator::DocIterator()
49 : boundary_(false), inset_(0), buffer_(0)
53 // We could be able to get rid of this if only every BufferView were
54 // associated to a buffer on construction.
55 DocIterator::DocIterator(Buffer * buf)
56 : boundary_(false), inset_(0), buffer_(buf)
60 DocIterator::DocIterator(Buffer * buf, Inset * inset)
61 : boundary_(false), inset_(inset), buffer_(buf)
65 DocIterator doc_iterator_begin(const Buffer * buf0, const Inset * inset0)
67 Buffer * buf = const_cast<Buffer *>(buf0);
68 Inset * inset = const_cast<Inset *>(inset0);
69 DocIterator dit(buf, inset ? inset : &buf->inset());
75 DocIterator doc_iterator_end(const Buffer * buf0, const Inset * inset0)
77 Buffer * buf = const_cast<Buffer *>(buf0);
78 Inset * inset = const_cast<Inset *>(inset0);
79 return DocIterator(buf, inset ? inset : &buf->inset());
83 DocIterator DocIterator::clone(Buffer * buffer) const
85 LASSERT(buffer->isClone(), return DocIterator());
86 Inset * inset = &buffer->inset();
87 DocIterator dit(buffer);
88 size_t const n = slices_.size();
89 for (size_t i = 0 ; i != n; ++i) {
91 dit.push_back(slices_[i]);
92 dit.top().inset_ = inset;
94 inset = dit.nextInset();
100 bool DocIterator::inRegexped() const
102 InsetMath * im = inset().asInsetMath();
105 InsetMathHull * hull = im->asHullInset();
106 return hull && hull->getType() == hullRegexp;
110 LyXErr & operator<<(LyXErr & os, DocIterator const & it)
117 Inset * DocIterator::nextInset() const
119 LASSERT(!empty(), return 0);
120 if (pos() == lastpos())
122 if (pos() > lastpos()) {
123 LYXERR0("Should not happen, but it does: pos() = "
124 << pos() << ", lastpos() = " << lastpos());
128 return nextAtom().nucleus();
129 return paragraph().getInset(pos());
133 Inset * DocIterator::prevInset() const
135 LASSERT(!empty(), return 0);
140 // FIXME: this should not happen but it does.
142 // http://www.lyx.org/trac/ticket/3189
145 return prevAtom().nucleus();
147 return paragraph().getInset(pos() - 1);
151 Inset * DocIterator::realInset() const
153 LASSERT(inTexted(), return 0);
154 // if we are in a tabular, we need the cell
155 if (inset().lyxCode() == TABULAR_CODE) {
156 InsetTabular * tabular = inset().asInsetTabular();
157 return tabular->cell(idx()).get();
163 MathAtom & DocIterator::prevAtom() const
165 LASSERT(!empty(), /**/);
166 LASSERT(pos() > 0, /**/);
167 return cell()[pos() - 1];
171 MathAtom & DocIterator::nextAtom() const
173 LASSERT(!empty(), /**/);
174 //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
175 LASSERT(pos() < lastpos(), /**/);
176 return cell()[pos()];
180 Text * DocIterator::text() const
182 LASSERT(!empty(), return 0);
187 Paragraph & DocIterator::paragraph() const
193 return top().paragraph();
197 Paragraph & DocIterator::innerParagraph() const
200 return innerTextSlice().paragraph();
204 FontSpan DocIterator::locateWord(word_location const loc) const
206 FontSpan f = FontSpan();
208 if (!top().text()->empty()) {
210 top().paragraph().locateWord(f.first, f.last, loc);
216 CursorSlice const & DocIterator::innerTextSlice() const
219 // go up until first non-0 text is hit
220 // (innermost text is 0 in mathed)
221 for (int i = depth() - 1; i >= 0; --i)
222 if (slices_[i].text())
225 // This case is in principle not possible. We _must_
229 static const CursorSlice c;
234 docstring DocIterator::paragraphGotoArgument() const
236 CursorSlice const & s = innerTextSlice();
237 return convert<docstring>(s.paragraph().id()) + ' ' +
238 convert<docstring>(s.pos());
242 DocIterator DocIterator::getInnerText() const
244 DocIterator texted = *this;
245 while (!texted.inTexted())
251 pit_type DocIterator::lastpit() const
253 return inMathed() ? 0 : text()->paragraphs().size() - 1;
257 pos_type DocIterator::lastpos() const
259 return inMathed() ? cell().size() : paragraph().size();
263 DocIterator::idx_type DocIterator::lastidx() const
265 return top().lastidx();
269 size_t DocIterator::nargs() const
271 // assume 1x1 grid for main text
272 return top().nargs();
276 size_t DocIterator::ncols() const
278 // assume 1x1 grid for main text
279 return top().ncols();
283 size_t DocIterator::nrows() const
285 // assume 1x1 grid for main text
286 return top().nrows();
290 DocIterator::row_type DocIterator::row() const
296 DocIterator::col_type DocIterator::col() const
302 MathData & DocIterator::cell() const
304 // LASSERT(inMathed(), /**/);
309 Text * DocIterator::innerText() const
311 LASSERT(!empty(), return 0);
312 return innerTextSlice().text();
316 Inset * DocIterator::innerInsetOfType(int code) const
318 for (int i = depth() - 1; i >= 0; --i)
319 if (slices_[i].inset_->lyxCode() == code)
320 return slices_[i].inset_;
325 bool DocIterator::posBackward()
334 bool DocIterator::posForward()
336 if (pos() == lastpos())
343 // This duplicates code above, but is in the critical path.
344 // So please think twice before adding stuff
345 void DocIterator::forwardPos()
347 // this dog bites his tail
349 push_back(CursorSlice(*inset_));
353 CursorSlice & tip = top();
354 //lyxerr << "XXX\n" << *this << endl;
356 // not at cell/paragraph end?
357 if (tip.pos() != tip.lastpos()) {
358 // move into an inset to the right if possible
361 n = (tip.cell().begin() + tip.pos())->nucleus();
363 n = paragraph().getInset(tip.pos());
364 if (n && n->isActive()) {
365 //lyxerr << "... descend" << endl;
366 push_back(CursorSlice(*n));
371 // jump to the next cell/paragraph if possible
377 // otherwise leave inset and jump over inset as a whole
379 // 'tip' is invalid now...
385 void DocIterator::forwardPosIgnoreCollapsed()
387 Inset * const nextinset = nextInset();
388 // FIXME: the check for asInsetMath() shouldn't be necessary
389 // but math insets do not return a sensible editable() state yet.
390 if (nextinset && !nextinset->asInsetMath()
391 && !nextinset->editable()) {
399 void DocIterator::forwardPar()
403 while (!empty() && (!inTexted() || pos() != 0)) {
405 pos_type const lastp = lastpos();
406 Paragraph const & par = paragraph();
407 pos_type & pos = top().pos();
408 if (par.insetList().empty())
411 while (pos < lastp && !par.isInset(pos))
419 void DocIterator::forwardChar()
422 while (!empty() && pos() == lastpos())
427 void DocIterator::forwardInset()
431 while (!empty() && !nextInset()) {
433 pos_type const lastp = lastpos();
434 Paragraph const & par = paragraph();
435 pos_type & pos = top().pos();
436 while (pos < lastp && !par.isInset(pos))
446 void DocIterator::backwardChar()
449 while (!empty() && pos() == lastpos())
454 void DocIterator::backwardPos()
456 //this dog bites his tail
458 push_back(CursorSlice(*inset_));
459 top().idx() = lastidx();
460 top().pit() = lastpit();
461 top().pos() = lastpos();
465 // at inset beginning?
466 if (top().at_begin()) {
473 // entered another cell/paragraph from the right?
474 if (top().pos() == top().lastpos())
477 // move into an inset to the left if possible
480 n = (top().cell().begin() + top().pos())->nucleus();
482 n = paragraph().getInset(top().pos());
483 if (n && n->isActive()) {
484 push_back(CursorSlice(*n));
485 top().idx() = lastidx();
486 top().pit() = lastpit();
487 top().pos() = lastpos();
493 // works, but currently not needed
494 void DocIterator::backwardInset()
498 while (!empty() && !nextInset()) {
500 pos_type const lastp = lastpos();
501 Paragraph const & par = paragraph();
502 pos_type & pos = top().pos();
503 while (pos > 0 && (pos == lastp || !par.isInset(pos)))
514 bool DocIterator::hasPart(DocIterator const & it) const
516 // it can't be a part if it is larger
517 if (it.depth() > depth())
520 // as inset adresses are the 'last' level
521 return &it.top().inset() == &slices_[it.depth() - 1].inset();
525 bool DocIterator::allowSpellCheck() const
527 /// spell check is disabled if the iterator position
528 /// is inside of an inset which disables the spell checker
529 size_t const n = depth();
530 for (size_t i = 0; i < n; ++i) {
531 if (!slices_[i].inset_->allowSpellCheck())
538 void DocIterator::updateInsets(Inset * inset)
540 // this function re-creates the cache of inset pointers.
541 //lyxerr << "converting:\n" << *this << endl;
542 DocIterator dit = *this;
543 size_t const n = slices_.size();
545 for (size_t i = 0 ; i < n; ++i) {
548 top().inset_ = inset;
552 //lyxerr << "converted:\n" << *this << endl;
556 bool DocIterator::fixIfBroken()
561 // Go through the slice stack from the bottom.
562 // Check that all coordinates (idx, pit, pos) are correct and
563 // that the inset is the one which is claimed to be there
564 Inset * inset = &slices_[0].inset();
566 size_t n = slices_.size();
567 for (; i != n; ++i) {
568 CursorSlice & cs = slices_[i];
569 if (&cs.inset() != inset || ! cs.inset().isActive()) {
570 // the whole slice is wrong, chop off this as well
572 LYXERR(Debug::DEBUG, "fixIfBroken(): inset changed");
574 } else if (cs.idx() > cs.lastidx()) {
575 cs.idx() = cs.lastidx();
576 cs.pit() = cs.lastpit();
577 cs.pos() = cs.lastpos();
578 LYXERR(Debug::DEBUG, "fixIfBroken(): idx fixed");
580 } else if (cs.pit() > cs.lastpit()) {
581 cs.pit() = cs.lastpit();
582 cs.pos() = cs.lastpos();
583 LYXERR(Debug::DEBUG, "fixIfBroken(): pit fixed");
585 } else if (cs.pos() > cs.lastpos()) {
586 cs.pos() = cs.lastpos();
587 LYXERR(Debug::DEBUG, "fixIfBroken(): pos fixed");
589 } else if (i != n - 1 && cs.pos() != cs.lastpos()) {
590 // get inset which is supposed to be in the next slice
591 if (cs.inset().inMathed())
592 inset = (cs.cell().begin() + cs.pos())->nucleus();
593 else if (Inset * csInset = cs.paragraph().getInset(cs.pos()))
596 // there are slices left, so there must be another inset
602 // Did we make it through the whole slice stack? Otherwise there
603 // was a problem at slice i, and we have to chop off above
605 LYXERR(Debug::DEBUG, "fixIfBroken(): cursor chopped at " << i);
613 void DocIterator::sanitize()
615 // keep a copy of the slices
616 vector<CursorSlice> const sl = slices_;
619 inset_ = &buffer_->inset();
620 Inset * inset = inset_;
621 // re-add the slices one by one, and adjust the inset pointer.
622 for (size_t i = 0, n = sl.size(); i != n; ++i) {
625 LYXERR0(" Should not happen, but does e.g. after "
626 "C-n C-l C-z S-C-z\n"
627 << " or when a Buffer has been concurrently edited by two views"
628 << '\n' << "dit: " << *this << '\n'
629 << " lastpos: " << slices_[i].lastpos());
633 if (!inset->isActive()) {
634 LYXERR0("Inset found on cursor stack is not active.");
639 top().inset_ = inset;
648 int DocIterator::find(MathData const & cell) const
650 for (size_t l = 0; l != slices_.size(); ++l) {
651 if (slices_[l].asInsetMath() && &slices_[l].cell() == &cell)
658 int DocIterator::find(Inset const * inset) const
660 for (size_t l = 0; l != slices_.size(); ++l) {
661 if (&slices_[l].inset() == inset)
668 void DocIterator::cutOff(int above, vector<CursorSlice> & cut)
670 cut = vector<CursorSlice>(slices_.begin() + above + 1, slices_.end());
671 slices_.resize(above + 1);
675 void DocIterator::cutOff(int above)
677 slices_.resize(above + 1);
681 void DocIterator::append(vector<CursorSlice> const & x)
683 slices_.insert(slices_.end(), x.begin(), x.end());
687 void DocIterator::append(DocIterator::idx_type idx, pos_type pos)
689 slices_.push_back(CursorSlice());
695 Encoding const * DocIterator::getEncoding() const
700 BufferParams const & bp = buffer()->params();
701 if (bp.useNonTeXFonts)
702 return encodings.fromLyXName("utf8-plain");
704 CursorSlice const & sl = innerTextSlice();
705 Text const & text = *sl.text();
706 Language const * lang =
707 text.getPar(sl.pit()).getFont(bp, sl.pos(),
708 text.outerFont(sl.pit())).language();
709 // If we have a custom encoding for the buffer, we only switch
710 // encoding for CJK (see output_latex::switchEncoding())
711 bool const customenc =
712 bp.inputenc != "auto" && bp.inputenc != "default";
713 Encoding const * enc =
714 (customenc && lang->encoding()->package() != Encoding::CJK)
715 ? &bp.encoding() : lang->encoding();
717 // Inherited encoding (latex_language) is determined by the context
718 // Look for the first outer encoding that is not itself "inherit"
719 if (lang->encoding()->name() == "inherit") {
720 size_t const n = depth();
721 for (size_t i = 0; i < n; ++i) {
722 Text const & otext = *slices_[i].text();
723 Language const * olang =
724 otext.getPar(slices_[i].pit()).getFont(bp, slices_[i].pos(),
725 otext.outerFont(slices_[i].pit())).language();
726 // Again, if we have a custom encoding, this is used
727 // instead of the language's.
728 Encoding const * oenc =
729 (customenc && olang->encoding()->package() != Encoding::CJK)
730 ? &bp.encoding() : olang->encoding();
731 if (olang->encoding()->name() != "inherit")
740 ostream & operator<<(ostream & os, DocIterator const & dit)
742 for (size_t i = 0, n = dit.depth(); i != n; ++i)
743 os << " " << dit[i] << "\n";
748 ///////////////////////////////////////////////////////
750 StableDocIterator::StableDocIterator(DocIterator const & dit) :
751 data_(dit.internalData())
753 for (size_t i = 0, n = data_.size(); i != n; ++i)
758 DocIterator StableDocIterator::asDocIterator(Buffer * buf) const
760 DocIterator dit(buf);
767 ostream & operator<<(ostream & os, StableDocIterator const & dit)
769 for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
770 os << " " << dit.data_[i] << "\n";
775 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
777 return dit1.data_ == dit2.data_;