]> git.lyx.org Git - lyx.git/blob - src/text_funcs.C
cosmetic fix
[lyx.git] / src / text_funcs.C
1 /**
2  * \file text_funcs.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS
10  *
11  * This file contains some utility functions for actually mutating
12  * the text contents of a document
13  */
14
15 #include <config.h>
16
17 #include "debug.h"
18 #include "lyxcursor.h"
19 #include "ParagraphList.h"
20
21 #include "support/types.h"
22
23 #include <boost/next_prior.hpp>
24
25 using lyx::pos_type;
26 using lyx::word_location;
27
28
29 bool transposeChars(LyXCursor const & cursor)
30 {
31         ParagraphList::iterator tmppit = cursor.par();
32         pos_type tmppos = cursor.pos();
33
34         // First decide if it is possible to transpose at all
35
36         if (tmppos == 0 || tmppos == tmppit->size())
37                 return false;
38
39         if (isDeletedText(*tmppit, tmppos - 1)
40                 || isDeletedText(*tmppit, tmppos))
41                 return false;
42
43         unsigned char c1 = tmppit->getChar(tmppos);
44         unsigned char c2 = tmppit->getChar(tmppos - 1);
45
46         // We should have an implementation that handles insets
47         // as well, but that will have to come later. (Lgb)
48         if (c1 == Paragraph::META_INSET || c2 == Paragraph::META_INSET)
49                 return false;
50
51         bool const erased = tmppit->erase(tmppos - 1, tmppos + 1);
52         size_t const ipos = erased ? tmppos - 1 : tmppos + 1;
53
54         tmppit->insertChar(ipos, c1);
55         tmppit->insertChar(ipos + 1, c2);
56         return true;
57 }
58
59
60 void cursorLeftOneWord(LyXCursor & cursor, ParagraphList const & pars)
61 {
62         // treat HFills, floats and Insets as words
63
64         ParagraphList::iterator pit = cursor.par();
65         size_t pos = cursor.pos();
66
67         while (pos &&
68                (pit->isSeparator(pos - 1) ||
69                 pit->isKomma(pos - 1) ||
70                 pit->isNewline(pos - 1)) &&
71                !(pit->isHfill(pos - 1) ||
72                  pit->isInset(pos - 1)))
73                 --pos;
74
75         if (pos &&
76             (pit->isInset(pos - 1) ||
77              pit->isHfill(pos - 1))) {
78                 --pos;
79         } else if (!pos) {
80                 // cast only for BSD's g++ 2.95
81                 if (pit != const_cast<ParagraphList &>(pars).begin()) {
82                         --pit;
83                         pos = pit->size();
84                 }
85         } else {                // Here, cur != 0
86                 while (pos > 0 && pit->isWord(pos - 1))
87                         --pos;
88         }
89
90         cursor.par(pit);
91         cursor.pos(pos);
92 }
93
94
95 void cursorRightOneWord(LyXCursor & cursor, ParagraphList const & pars)
96 {
97         // treat floats, HFills and Insets as words
98         ParagraphList::iterator pit = cursor.par();
99         pos_type pos = cursor.pos();
100
101         // CHECK See comment on top of text.C
102
103         // cast only for BSD's g++ 2.95
104         if (pos == pit->size() &&
105                 boost::next(pit) != const_cast<ParagraphList &>(pars).end()) {
106                 ++pit;
107                 pos = 0;
108         } else {
109                 // Skip through initial nonword stuff.
110                 while (pos < pit->size() && !pit->isWord(pos)) {
111                         ++pos;
112                 }
113                 // Advance through word.
114                 while (pos < pit->size() && pit->isWord(pos)) {
115                         ++pos;
116                 }
117         }
118
119         cursor.par(pit);
120         cursor.pos(pos);
121 }
122
123
124 // Select current word. This depends on behaviour of
125 // CursorLeftOneWord(), so it is patched as well.
126 void getWord(LyXCursor & from, LyXCursor & to, word_location const loc,
127         ParagraphList const & pars)
128 {
129         switch (loc) {
130         case lyx::WHOLE_WORD_STRICT:
131                 if (from.pos() == 0 || from.pos() == from.par()->size()
132                     || from.par()->isSeparator(from.pos())
133                     || from.par()->isKomma(from.pos())
134                     || from.par()->isNewline(from.pos())
135                     || from.par()->isSeparator(from.pos() - 1)
136                     || from.par()->isKomma(from.pos() - 1)
137                     || from.par()->isNewline(from.pos() - 1)) {
138                         to = from;
139                         return;
140                 }
141                 // no break here, we go to the next
142
143         case lyx::WHOLE_WORD:
144                 // Move cursor to the beginning, when not already there.
145                 if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
146                     && !(from.par()->isKomma(from.pos() - 1)
147                          || from.par()->isNewline(from.pos() - 1)))
148                         cursorLeftOneWord(from, pars);
149                 break;
150         case lyx::PREVIOUS_WORD:
151                 // always move the cursor to the beginning of previous word
152                 cursorLeftOneWord(from, pars);
153                 break;
154         case lyx::NEXT_WORD:
155                 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
156                 break;
157         case lyx::PARTIAL_WORD:
158                 break;
159         }
160         to = from;
161         while (to.pos() < to.par()->size()
162                && !to.par()->isSeparator(to.pos())
163                && !to.par()->isKomma(to.pos())
164                && !to.par()->isNewline(to.pos())
165                && !to.par()->isHfill(to.pos())
166                && !to.par()->isInset(to.pos()))
167         {
168                 to.pos(to.pos() + 1);
169         }
170 }