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
17 #include "text_funcs.h"
19 #include "lyxcursor.h"
20 #include "paragraph.h"
22 #include <boost/next_prior.hpp>
25 using lyx::word_location;
30 bool transposeChars(LyXCursor const & cursor)
32 ParagraphList::iterator tmppit = cursor.par();
33 pos_type tmppos = cursor.pos();
35 // First decide if it is possible to transpose at all
37 if (tmppos == 0 || tmppos == tmppit->size())
40 if (isDeletedText(*tmppit, tmppos - 1)
41 || isDeletedText(*tmppit, tmppos))
44 unsigned char c1 = tmppit->getChar(tmppos);
45 unsigned char c2 = tmppit->getChar(tmppos - 1);
47 // We should have an implementation that handles insets
48 // as well, but that will have to come later. (Lgb)
49 if (c1 == Paragraph::META_INSET || c2 == Paragraph::META_INSET)
52 bool const erased = tmppit->erase(tmppos - 1, tmppos + 1);
53 size_t const ipos = erased ? tmppos - 1 : tmppos + 1;
55 tmppit->insertChar(ipos, c1);
56 tmppit->insertChar(ipos + 1, c2);
61 void cursorLeftOneWord(LyXCursor & cursor, ParagraphList const & pars)
63 // treat HFills, floats and Insets as words
65 ParagraphList::iterator pit = cursor.par();
66 size_t pos = cursor.pos();
69 (pit->isSeparator(pos - 1) ||
70 pit->isKomma(pos - 1) ||
71 pit->isNewline(pos - 1)) &&
72 !(pit->isHfill(pos - 1) ||
73 pit->isInset(pos - 1)))
77 (pit->isInset(pos - 1) ||
78 pit->isHfill(pos - 1))) {
81 // cast only for BSD's g++ 2.95
82 if (pit != const_cast<ParagraphList &>(pars).begin()) {
86 } else { // Here, cur != 0
87 while (pos > 0 && pit->isWord(pos - 1))
96 void cursorRightOneWord(LyXCursor & cursor, ParagraphList const & pars)
98 // treat floats, HFills and Insets as words
99 ParagraphList::iterator pit = cursor.par();
100 pos_type pos = cursor.pos();
102 // CHECK See comment on top of text.C
104 // cast only for BSD's g++ 2.95
105 if (pos == pit->size() &&
106 boost::next(pit) != const_cast<ParagraphList &>(pars).end()) {
110 // Skip through initial nonword stuff.
111 while (pos < pit->size() && !pit->isWord(pos)) {
114 // Advance through word.
115 while (pos < pit->size() && pit->isWord(pos)) {
125 // Select current word. This depends on behaviour of
126 // CursorLeftOneWord(), so it is patched as well.
127 void getWord(LyXCursor & from, LyXCursor & to, word_location const loc,
128 ParagraphList const & pars)
131 case lyx::WHOLE_WORD_STRICT:
132 if (from.pos() == 0 || from.pos() == from.par()->size()
133 || from.par()->isSeparator(from.pos())
134 || from.par()->isKomma(from.pos())
135 || from.par()->isNewline(from.pos())
136 || from.par()->isSeparator(from.pos() - 1)
137 || from.par()->isKomma(from.pos() - 1)
138 || from.par()->isNewline(from.pos() - 1)) {
142 // no break here, we go to the next
144 case lyx::WHOLE_WORD:
145 // Move cursor to the beginning, when not already there.
146 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
147 && !(from.par()->isKomma(from.pos() - 1)
148 || from.par()->isNewline(from.pos() - 1)))
149 cursorLeftOneWord(from, pars);
151 case lyx::PREVIOUS_WORD:
152 // always move the cursor to the beginning of previous word
153 cursorLeftOneWord(from, pars);
156 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
159 case lyx::PARTIAL_WORD:
163 while (to.pos() < to.par()->size()
164 && !to.par()->isSeparator(to.pos())
165 && !to.par()->isKomma(to.pos())
166 && !to.par()->isNewline(to.pos())
167 && !to.par()->isHfill(to.pos())
168 && !to.par()->isInset(to.pos()))
170 to.pos(to.pos() + 1);