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
9 * Full author contact details are available in file CREDITS.
11 * This file contains some utility functions for actually mutating
12 * the text contents of a document
18 #include "lyxcursor.h"
19 #include "ParagraphList.h"
21 #include "support/types.h"
23 #include <boost/next_prior.hpp>
26 using lyx::word_location;
31 bool transposeChars(LyXCursor const & cursor)
33 ParagraphList::iterator tmppit = cursor.par();
34 pos_type tmppos = cursor.pos();
36 // First decide if it is possible to transpose at all
38 if (tmppos == 0 || tmppos == tmppit->size())
41 if (isDeletedText(*tmppit, tmppos - 1)
42 || isDeletedText(*tmppit, tmppos))
45 unsigned char c1 = tmppit->getChar(tmppos);
46 unsigned char c2 = tmppit->getChar(tmppos - 1);
48 // We should have an implementation that handles insets
49 // as well, but that will have to come later. (Lgb)
50 if (c1 == Paragraph::META_INSET || c2 == Paragraph::META_INSET)
53 bool const erased = tmppit->erase(tmppos - 1, tmppos + 1);
54 size_t const ipos = erased ? tmppos - 1 : tmppos + 1;
56 tmppit->insertChar(ipos, c1);
57 tmppit->insertChar(ipos + 1, c2);
62 void cursorLeftOneWord(LyXCursor & cursor, ParagraphList const & pars)
64 // treat HFills, floats and Insets as words
66 ParagraphList::iterator pit = cursor.par();
67 size_t pos = cursor.pos();
70 (pit->isSeparator(pos - 1) ||
71 pit->isKomma(pos - 1) ||
72 pit->isNewline(pos - 1)) &&
73 !(pit->isHfill(pos - 1) ||
74 pit->isInset(pos - 1)))
78 (pit->isInset(pos - 1) ||
79 pit->isHfill(pos - 1))) {
82 // cast only for BSD's g++ 2.95
83 if (pit != const_cast<ParagraphList &>(pars).begin()) {
87 } else { // Here, cur != 0
88 while (pos > 0 && pit->isWord(pos - 1))
97 void cursorRightOneWord(LyXCursor & cursor, ParagraphList const & pars)
99 // treat floats, HFills and Insets as words
100 ParagraphList::iterator pit = cursor.par();
101 pos_type pos = cursor.pos();
103 // CHECK See comment on top of text.C
105 // cast only for BSD's g++ 2.95
106 if (pos == pit->size() &&
107 boost::next(pit) != const_cast<ParagraphList &>(pars).end()) {
111 // Skip through initial nonword stuff.
112 while (pos < pit->size() && !pit->isWord(pos)) {
115 // Advance through word.
116 while (pos < pit->size() && pit->isWord(pos)) {
126 // Select current word. This depends on behaviour of
127 // CursorLeftOneWord(), so it is patched as well.
128 void getWord(LyXCursor & from, LyXCursor & to, word_location const loc,
129 ParagraphList const & pars)
132 case lyx::WHOLE_WORD_STRICT:
133 if (from.pos() == 0 || from.pos() == from.par()->size()
134 || from.par()->isSeparator(from.pos())
135 || from.par()->isKomma(from.pos())
136 || from.par()->isNewline(from.pos())
137 || from.par()->isSeparator(from.pos() - 1)
138 || from.par()->isKomma(from.pos() - 1)
139 || from.par()->isNewline(from.pos() - 1)) {
143 // no break here, we go to the next
145 case lyx::WHOLE_WORD:
146 // Move cursor to the beginning, when not already there.
147 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
148 && !(from.par()->isKomma(from.pos() - 1)
149 || from.par()->isNewline(from.pos() - 1)))
150 cursorLeftOneWord(from, pars);
152 case lyx::PREVIOUS_WORD:
153 // always move the cursor to the beginning of previous word
154 cursorLeftOneWord(from, pars);
157 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
160 case lyx::PARTIAL_WORD:
164 while (to.pos() < to.par()->size()
165 && !to.par()->isSeparator(to.pos())
166 && !to.par()->isKomma(to.pos())
167 && !to.par()->isNewline(to.pos())
168 && !to.par()->isHfill(to.pos())
169 && !to.par()->isInset(to.pos()))
171 to.pos(to.pos() + 1);