-rw-r--r-- | noncore/apps/tinykate/libkate/document/katetextline.h | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/noncore/apps/tinykate/libkate/document/katetextline.h b/noncore/apps/tinykate/libkate/document/katetextline.h index c2968cc..3518900 100644 --- a/noncore/apps/tinykate/libkate/document/katetextline.h +++ b/noncore/apps/tinykate/libkate/document/katetextline.h | |||
@@ -1,335 +1,335 @@ | |||
1 | /* | 1 | /* |
2 | Copyright (C) 1998, 1999 Jochen Wilhelmy | 2 | Copyright (C) 1998, 1999 Jochen Wilhelmy |
3 | digisnap@cs.tu-berlin.de | 3 | digisnap@cs.tu-berlin.de |
4 | (C) 2002, 2001 The Kate Team <kwrite-devel@kde.org> | 4 | (C) 2002, 2001 The Kate Team <kwrite-devel@kde.org> |
5 | (C) 2002 Joseph Wenninger <jowenn@kde.org> | 5 | (C) 2002 Joseph Wenninger <jowenn@kde.org> |
6 | 6 | ||
7 | This library is free software; you can redistribute it and/or | 7 | This library is free software; you can redistribute it and/or |
8 | modify it under the terms of the GNU Library General Public | 8 | modify it under the terms of the GNU Library General Public |
9 | License as published by the Free Software Foundation; either | 9 | License as published by the Free Software Foundation; either |
10 | version 2 of the License, or (at your option) any later version. | 10 | version 2 of the License, or (at your option) any later version. |
11 | 11 | ||
12 | This library is distributed in the hope that it will be useful, | 12 | This library is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | Library General Public License for more details. | 15 | Library General Public License for more details. |
16 | 16 | ||
17 | You should have received a copy of the GNU Library General Public License | 17 | You should have received a copy of the GNU Library General Public License |
18 | along with this library; see the file COPYING.LIB. If not, write to | 18 | along with this library; see the file COPYING.LIB. If not, write to |
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
20 | Boston, MA 02111-1307, USA. | 20 | Boston, MA 02111-1307, USA. |
21 | */ | 21 | */ |
22 | 22 | ||
23 | #ifndef _KWTEXTLINE_H_ | 23 | #ifndef _KWTEXTLINE_H_ |
24 | #define _KWTEXTLINE_H_ | 24 | #define _KWTEXTLINE_H_ |
25 | 25 | ||
26 | #include <stdlib.h> | 26 | #include <stdlib.h> |
27 | 27 | ||
28 | #include <qstring.h> | 28 | #include <qstring.h> |
29 | #include <qarray.h> | 29 | #include <qarray.h> |
30 | #include <qvaluelist.h> | 30 | #include <qvaluelist.h> |
31 | 31 | ||
32 | #include <ksharedptr.h> | 32 | #include <ksharedptr.h> |
33 | 33 | ||
34 | /** | 34 | /** |
35 | FastValueList: QValueList, but with a faster at() like QList | 35 | FastValueList: QValueList, but with a faster at() like QList |
36 | FVPrivate is needed so that "const" functions can change the | 36 | FVPrivate is needed so that "const" functions can change the |
37 | current position | 37 | current position |
38 | */ | 38 | */ |
39 | template<class T> | 39 | template<class T> |
40 | class FVPrivate | 40 | class FVPrivate |
41 | { | 41 | { |
42 | public: | 42 | public: |
43 | int curpos; | 43 | int curpos; |
44 | typedef QValueListConstIterator<T> Iterator; | 44 | typedef QValueListConstIterator<T> Iterator; |
45 | Iterator curit; | 45 | Iterator curit; |
46 | 46 | ||
47 | FVPrivate() { curpos=-1; }; | 47 | FVPrivate() { curpos=-1; }; |
48 | }; | 48 | }; |
49 | 49 | ||
50 | template<class T> | 50 | template<class T> |
51 | class FastValueList : public QValueList<T> | 51 | class FastValueList : public QValueList<T> |
52 | { | 52 | { |
53 | public: | 53 | public: |
54 | typedef QValueListIterator<T> Iterator; | 54 | typedef QValueListIterator<T> Iterator; |
55 | typedef QValueListConstIterator<T> ConstIterator; | 55 | typedef QValueListConstIterator<T> ConstIterator; |
56 | protected: | 56 | protected: |
57 | FVPrivate<T> *fvp; | 57 | FVPrivate<T> *fvp; |
58 | 58 | ||
59 | Iterator fastat( uint i ) { | 59 | Iterator fastat( uint i ) { |
60 | uint num=count(); | 60 | uint num=this->count(); |
61 | if (i>=num) {return end();} | 61 | if (i>=num) {return this->end();} |
62 | if (fvp->curpos<0) { fvp->curpos=0; fvp->curit=begin(); } | 62 | if (fvp->curpos<0) { fvp->curpos=0; fvp->curit=this->begin(); } |
63 | uint curpos=(uint) fvp->curpos; | 63 | uint curpos=(uint) fvp->curpos; |
64 | Iterator curit(fvp->curit.node); | 64 | Iterator curit(fvp->curit.node); |
65 | if (curpos==i) return curit; | 65 | if (curpos==i) return curit; |
66 | 66 | ||
67 | int diff=i-curpos; | 67 | int diff=i-curpos; |
68 | bool forward; | 68 | bool forward; |
69 | if (diff<0) diff=-diff; | 69 | if (diff<0) diff=-diff; |
70 | if (((uint)diff < i) && ((uint)diff < num-i)) { // start from current node | 70 | if (((uint)diff < i) && ((uint)diff < num-i)) { // start from current node |
71 | forward=i > (uint)curpos; | 71 | forward=i > (uint)curpos; |
72 | } else if (i < num - i) { // start from first node | 72 | } else if (i < num - i) { // start from first node |
73 | curit=begin(); diff=i; forward=TRUE; | 73 | curit=this->begin(); diff=i; forward=TRUE; |
74 | } else { // start from last node | 74 | } else { // start from last node |
75 | curit=fromLast(); diff=num - i - 1; | 75 | curit=this->fromLast(); diff=num - i - 1; |
76 | if (diff<0) diff=0; | 76 | if (diff<0) diff=0; |
77 | forward=FALSE; | 77 | forward=FALSE; |
78 | } | 78 | } |
79 | if (forward) { | 79 | if (forward) { |
80 | while(diff--) curit++; | 80 | while(diff--) curit++; |
81 | } else { | 81 | } else { |
82 | while(diff--) curit--; | 82 | while(diff--) curit--; |
83 | } | 83 | } |
84 | fvp->curpos=i; fvp->curit=curit; | 84 | fvp->curpos=i; fvp->curit=curit; |
85 | return curit; | 85 | return curit; |
86 | } | 86 | } |
87 | ConstIterator fastat( uint i ) const { | 87 | ConstIterator fastat( uint i ) const { |
88 | uint num=count(); | 88 | uint num=this->count(); |
89 | if (i>=num) {return end();} | 89 | if (i>=num) {return this->end();} |
90 | if (fvp->curpos<0) { fvp->curpos=0; fvp->curit=begin(); } | 90 | if (fvp->curpos<0) { fvp->curpos=0; fvp->curit=this->begin(); } |
91 | uint curpos=(uint) fvp->curpos; | 91 | uint curpos=(uint) fvp->curpos; |
92 | ConstIterator curit=fvp->curit; | 92 | ConstIterator curit=fvp->curit; |
93 | if (curpos==i) return curit; | 93 | if (curpos==i) return curit; |
94 | 94 | ||
95 | int diff=i-curpos; | 95 | int diff=i-curpos; |
96 | bool forward; | 96 | bool forward; |
97 | if (diff<0) diff=-diff; | 97 | if (diff<0) diff=-diff; |
98 | if (((uint)diff < i) && ((uint)diff < num-i)) { // start from current node | 98 | if (((uint)diff < i) && ((uint)diff < num-i)) { // start from current node |
99 | forward=i > (uint)curpos; | 99 | forward=i > (uint)curpos; |
100 | } else if (i < num - i) { // start from first node | 100 | } else if (i < num - i) { // start from first node |
101 | curit=begin(); diff=i; forward=TRUE; | 101 | curit=this->begin(); diff=i; forward=TRUE; |
102 | } else { // start from last node | 102 | } else { // start from last node |
103 | curit=fromLast(); diff=num - i - 1; | 103 | curit=this->fromLast(); diff=num - i - 1; |
104 | if (diff<0) diff=0; | 104 | if (diff<0) diff=0; |
105 | forward=FALSE; | 105 | forward=FALSE; |
106 | } | 106 | } |
107 | if (forward) { | 107 | if (forward) { |
108 | while(diff--) curit++; | 108 | while(diff--) curit++; |
109 | } else { | 109 | } else { |
110 | while(diff--) curit--; | 110 | while(diff--) curit--; |
111 | } | 111 | } |
112 | fvp->curpos=i; fvp->curit=curit; | 112 | fvp->curpos=i; fvp->curit=curit; |
113 | return curit; | 113 | return curit; |
114 | } | 114 | } |
115 | 115 | ||
116 | public: | 116 | public: |
117 | FastValueList() : QValueList<T>() | 117 | FastValueList() : QValueList<T>() |
118 | { fvp=new FVPrivate<T>(); } | 118 | { fvp=new FVPrivate<T>(); } |
119 | FastValueList(const FastValueList<T>& l) : QValueList<T>(l) | 119 | FastValueList(const FastValueList<T>& l) : QValueList<T>(l) |
120 | { fvp=new FVPrivate<T>(); } | 120 | { fvp=new FVPrivate<T>(); } |
121 | ~FastValueList() { delete fvp; } | 121 | ~FastValueList() { delete fvp; } |
122 | 122 | ||
123 | Iterator insert( Iterator it, const T& x ) { | 123 | Iterator insert( Iterator it, const T& x ) { |
124 | fvp->curpos=-1; return QValueList<T>::insert(it, x); | 124 | fvp->curpos=-1; return QValueList<T>::insert(it, x); |
125 | } | 125 | } |
126 | 126 | ||
127 | Iterator append( const T& x ) { | 127 | Iterator append( const T& x ) { |
128 | fvp->curpos=-1; return QValueList<T>::append( x ); | 128 | fvp->curpos=-1; return QValueList<T>::append( x ); |
129 | } | 129 | } |
130 | Iterator prepend( const T& x ) { | 130 | Iterator prepend( const T& x ) { |
131 | fvp->curpos=-1; return QValueList<T>::prepend( x ); | 131 | fvp->curpos=-1; return QValueList<T>::prepend( x ); |
132 | } | 132 | } |
133 | 133 | ||
134 | Iterator remove( Iterator it ) { | 134 | Iterator remove( Iterator it ) { |
135 | fvp->curpos=-1; return QValueList<T>::remove( it ); | 135 | fvp->curpos=-1; return QValueList<T>::remove( it ); |
136 | } | 136 | } |
137 | void remove( const T& x ) { | 137 | void remove( const T& x ) { |
138 | fvp->curpos=-1; QValueList<T>::remove( x ); | 138 | fvp->curpos=-1; QValueList<T>::remove( x ); |
139 | } | 139 | } |
140 | 140 | ||
141 | T& operator[] ( uint i ) { detach(); return fastat(i); } | 141 | T& operator[] ( uint i ) { this->detach(); return fastat(i); } |
142 | const T& operator[] ( uint i ) const { return *fastat(i); } | 142 | const T& operator[] ( uint i ) const { return *fastat(i); } |
143 | Iterator at( uint i ) { detach(); return fastat(i); } | 143 | Iterator at( uint i ) { this->detach(); return fastat(i); } |
144 | ConstIterator at( uint i ) const { return ConstIterator( fastat(i) ); } | 144 | ConstIterator at( uint i ) const { return ConstIterator( fastat(i) ); } |
145 | }; | 145 | }; |
146 | 146 | ||
147 | 147 | ||
148 | /** | 148 | /** |
149 | The TextLine represents a line of text. A text line that contains the | 149 | The TextLine represents a line of text. A text line that contains the |
150 | text, an attribute for each character, an attribute for the free space | 150 | text, an attribute for each character, an attribute for the free space |
151 | behind the last character and a context number for the syntax highlight. | 151 | behind the last character and a context number for the syntax highlight. |
152 | The attribute stores the index to a table that contains fonts and colors | 152 | The attribute stores the index to a table that contains fonts and colors |
153 | and also if a character is selected. | 153 | and also if a character is selected. |
154 | */ | 154 | */ |
155 | class TextLine : public KShared | 155 | class TextLine : public KShared |
156 | { | 156 | { |
157 | friend class KWBuffer; | 157 | friend class KWBuffer; |
158 | friend class KWBufBlock; | 158 | friend class KWBufBlock; |
159 | 159 | ||
160 | public: | 160 | public: |
161 | typedef KSharedPtr<TextLine> Ptr; | 161 | typedef KSharedPtr<TextLine> Ptr; |
162 | typedef FastValueList<Ptr> List; | 162 | typedef FastValueList<Ptr> List; |
163 | 163 | ||
164 | public: | 164 | public: |
165 | /** | 165 | /** |
166 | Creates an empty text line with given attribute and syntax highlight | 166 | Creates an empty text line with given attribute and syntax highlight |
167 | context | 167 | context |
168 | */ | 168 | */ |
169 | TextLine(uchar attribute = 0, int context = 0); | 169 | TextLine(uchar attribute = 0, int context = 0); |
170 | ~TextLine(); | 170 | ~TextLine(); |
171 | 171 | ||
172 | /** | 172 | /** |
173 | Returns the length | 173 | Returns the length |
174 | */ | 174 | */ |
175 | uint length() const {return text.length();} | 175 | uint length() const {return text.length();} |
176 | /** | 176 | /** |
177 | Universal text manipulation method. It can be used to insert, delete | 177 | Universal text manipulation method. It can be used to insert, delete |
178 | or replace text. | 178 | or replace text. |
179 | */ | 179 | */ |
180 | void replace(uint pos, uint delLen, const QChar *insText, uint insLen, uchar *insAttribs = 0L); | 180 | void replace(uint pos, uint delLen, const QChar *insText, uint insLen, uchar *insAttribs = 0L); |
181 | 181 | ||
182 | /** | 182 | /** |
183 | Appends a string of length l to the textline | 183 | Appends a string of length l to the textline |
184 | */ | 184 | */ |
185 | void append(const QChar *s, uint l) {replace(text.length(), 0, s, l);} | 185 | void append(const QChar *s, uint l) {replace(text.length(), 0, s, l);} |
186 | /** | 186 | /** |
187 | Wraps the text from the given position to the end to the next line | 187 | Wraps the text from the given position to the end to the next line |
188 | */ | 188 | */ |
189 | void wrap(TextLine::Ptr nextLine, uint pos); | 189 | void wrap(TextLine::Ptr nextLine, uint pos); |
190 | /** | 190 | /** |
191 | Wraps the text of given length from the beginning of the next line to | 191 | Wraps the text of given length from the beginning of the next line to |
192 | this line at the given position | 192 | this line at the given position |
193 | */ | 193 | */ |
194 | void unWrap(uint pos, TextLine::Ptr nextLine, uint len); | 194 | void unWrap(uint pos, TextLine::Ptr nextLine, uint len); |
195 | /** | 195 | /** |
196 | Truncates the textline to the new length | 196 | Truncates the textline to the new length |
197 | */ | 197 | */ |
198 | void truncate(uint newLen) { text.truncate(newLen); attributes.resize(text.length()); } | 198 | void truncate(uint newLen) { text.truncate(newLen); attributes.resize(text.length()); } |
199 | /** | 199 | /** |
200 | Returns the position of the first character which is not a white space | 200 | Returns the position of the first character which is not a white space |
201 | */ | 201 | */ |
202 | int firstChar() const; | 202 | int firstChar() const; |
203 | /** | 203 | /** |
204 | Returns the position of the last character which is not a white space | 204 | Returns the position of the last character which is not a white space |
205 | */ | 205 | */ |
206 | int lastChar() const; | 206 | int lastChar() const; |
207 | /** | 207 | /** |
208 | Removes trailing spaces | 208 | Removes trailing spaces |
209 | */ | 209 | */ |
210 | void removeSpaces(); | 210 | void removeSpaces(); |
211 | /** | 211 | /** |
212 | Gets the char at the given position | 212 | Gets the char at the given position |
213 | */ | 213 | */ |
214 | QChar getChar(uint pos) const; | 214 | QChar getChar(uint pos) const; |
215 | /** | 215 | /** |
216 | Gets the text. WARNING: it is not null terminated | 216 | Gets the text. WARNING: it is not null terminated |
217 | */ | 217 | */ |
218 | const QChar *getText() const {return text.unicode();}; | 218 | const QChar *getText() const {return text.unicode();}; |
219 | /** | 219 | /** |
220 | Gets a C-like null terminated string | 220 | Gets a C-like null terminated string |
221 | */ | 221 | */ |
222 | const QString getString() { return text; }; | 222 | const QString getString() { return text; }; |
223 | 223 | ||
224 | /* | 224 | /* |
225 | Gets a null terminated pointer to first non space char | 225 | Gets a null terminated pointer to first non space char |
226 | */ | 226 | */ |
227 | const QChar *firstNonSpace(); | 227 | const QChar *firstNonSpace(); |
228 | /** | 228 | /** |
229 | Returns the x position of the cursor at the given position, which | 229 | Returns the x position of the cursor at the given position, which |
230 | depends on the number of tab characters | 230 | depends on the number of tab characters |
231 | */ | 231 | */ |
232 | int cursorX(uint pos, uint tabChars) const; | 232 | int cursorX(uint pos, uint tabChars) const; |
233 | /** | 233 | /** |
234 | Is the line starting with the given string | 234 | Is the line starting with the given string |
235 | */ | 235 | */ |
236 | bool startingWith(QString& match); | 236 | bool startingWith(QString& match); |
237 | /** | 237 | /** |
238 | Is the line ending with the given string | 238 | Is the line ending with the given string |
239 | */ | 239 | */ |
240 | bool endingWith(QString& match); | 240 | bool endingWith(QString& match); |
241 | 241 | ||
242 | /** | 242 | /** |
243 | Sets the attributes from start to end -1 | 243 | Sets the attributes from start to end -1 |
244 | */ | 244 | */ |
245 | void setAttribs(uchar attribute, uint start, uint end); | 245 | void setAttribs(uchar attribute, uint start, uint end); |
246 | /** | 246 | /** |
247 | Sets the attribute for the free space behind the last character | 247 | Sets the attribute for the free space behind the last character |
248 | */ | 248 | */ |
249 | void setAttr(uchar attribute); | 249 | void setAttr(uchar attribute); |
250 | /** | 250 | /** |
251 | Gets the attribute at the given position | 251 | Gets the attribute at the given position |
252 | */ | 252 | */ |
253 | uchar getAttr(uint pos) const; | 253 | uchar getAttr(uint pos) const; |
254 | /** | 254 | /** |
255 | Gets the attribute for the free space behind the last character | 255 | Gets the attribute for the free space behind the last character |
256 | */ | 256 | */ |
257 | uchar getAttr() const; | 257 | uchar getAttr() const; |
258 | /** | 258 | /** |
259 | Gets the attribute, including the select state, at the given position | 259 | Gets the attribute, including the select state, at the given position |
260 | */ | 260 | */ |
261 | uchar getRawAttr(uint pos) const; | 261 | uchar getRawAttr(uint pos) const; |
262 | /** | 262 | /** |
263 | Gets the attribute, including the select state, for the free space | 263 | Gets the attribute, including the select state, for the free space |
264 | behind the last character | 264 | behind the last character |
265 | */ | 265 | */ |
266 | uchar getRawAttr() const; | 266 | uchar getRawAttr() const; |
267 | 267 | ||
268 | /** | 268 | /** |
269 | Sets the syntax highlight context number | 269 | Sets the syntax highlight context number |
270 | */ | 270 | */ |
271 | void setContext(int context); | 271 | void setContext(int context); |
272 | /** | 272 | /** |
273 | Gets the syntax highlight context number | 273 | Gets the syntax highlight context number |
274 | */ | 274 | */ |
275 | int getContext() const; | 275 | int getContext() const; |
276 | 276 | ||
277 | /** | 277 | /** |
278 | Sets the select state from start to end -1 | 278 | Sets the select state from start to end -1 |
279 | */ | 279 | */ |
280 | void select(bool sel, uint start, uint end); | 280 | void select(bool sel, uint start, uint end); |
281 | /** | 281 | /** |
282 | Sets the select state from the given position to the end, including | 282 | Sets the select state from the given position to the end, including |
283 | the free space behind the last character | 283 | the free space behind the last character |
284 | */ | 284 | */ |
285 | void selectEol(bool sel, uint pos); | 285 | void selectEol(bool sel, uint pos); |
286 | /** | 286 | /** |
287 | Toggles the select state from start to end -1 | 287 | Toggles the select state from start to end -1 |
288 | */ | 288 | */ |
289 | void toggleSelect(uint start, uint end); | 289 | void toggleSelect(uint start, uint end); |
290 | /** | 290 | /** |
291 | Toggles the select state from the given position to the end, including | 291 | Toggles the select state from the given position to the end, including |
292 | the free space behind the last character | 292 | the free space behind the last character |
293 | */ | 293 | */ |
294 | void toggleSelectEol(uint pos); | 294 | void toggleSelectEol(uint pos); |
295 | /** | 295 | /** |
296 | Returns the number of selected characters | 296 | Returns the number of selected characters |
297 | */ | 297 | */ |
298 | int numSelected() const; | 298 | int numSelected() const; |
299 | /** | 299 | /** |
300 | Returns if the character at the given position is selected | 300 | Returns if the character at the given position is selected |
301 | */ | 301 | */ |
302 | bool isSelected(uint pos) const; | 302 | bool isSelected(uint pos) const; |
303 | /** | 303 | /** |
304 | Returns true if the free space behind the last character is selected | 304 | Returns true if the free space behind the last character is selected |
305 | */ | 305 | */ |
306 | bool isSelected() const; | 306 | bool isSelected() const; |
307 | /** | 307 | /** |
308 | Finds the next selected character, starting at the given position | 308 | Finds the next selected character, starting at the given position |
309 | */ | 309 | */ |
310 | int findSelected(uint pos) const; | 310 | int findSelected(uint pos) const; |
311 | /** | 311 | /** |
312 | Finds the next unselected character, starting at the given position | 312 | Finds the next unselected character, starting at the given position |
313 | */ | 313 | */ |
314 | int findUnselected(uint pos) const; | 314 | int findUnselected(uint pos) const; |
315 | /** | 315 | /** |
316 | Finds the previous selected character, starting at the given position | 316 | Finds the previous selected character, starting at the given position |
317 | */ | 317 | */ |
318 | int findRevSelected(uint pos) const; | 318 | int findRevSelected(uint pos) const; |
319 | /** | 319 | /** |
320 | Finds the previous unselected character, starting at the given position | 320 | Finds the previous unselected character, starting at the given position |
321 | */ | 321 | */ |
322 | int findRevUnselected(uint pos) const; | 322 | int findRevUnselected(uint pos) const; |
323 | 323 | ||
324 | void clearMark () { myMark = 0; }; | 324 | void clearMark () { myMark = 0; }; |
325 | void addMark ( uint m ); | 325 | void addMark ( uint m ); |
326 | void delMark ( uint m ); | 326 | void delMark ( uint m ); |
327 | uint mark() { return myMark; }; | 327 | uint mark() { return myMark; }; |
328 | 328 | ||
329 | uchar *getAttribs() { return attributes.data(); } | 329 | uchar *getAttribs() { return attributes.data(); } |
330 | 330 | ||
331 | protected: | 331 | protected: |
332 | /** | 332 | /** |
333 | The text | 333 | The text |
334 | */ | 334 | */ |
335 | QString text; | 335 | QString text; |