summaryrefslogtreecommitdiff
path: root/noncore/apps/tinykate/libkate/document/katetextline.cpp
Unidiff
Diffstat (limited to 'noncore/apps/tinykate/libkate/document/katetextline.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/tinykate/libkate/document/katetextline.cpp289
1 files changed, 289 insertions, 0 deletions
diff --git a/noncore/apps/tinykate/libkate/document/katetextline.cpp b/noncore/apps/tinykate/libkate/document/katetextline.cpp
new file mode 100644
index 0000000..f44cc1c
--- a/dev/null
+++ b/noncore/apps/tinykate/libkate/document/katetextline.cpp
@@ -0,0 +1,289 @@
1/*
2 Copyright (C) 1998, 1999 Jochen Wilhelmy
3 digisnap@cs.tu-berlin.de
4 (C) 2002, 2001 The Kate Team <kwrite-devel@kde.org>
5 (C) 2002 Joseph Wenninger <jowenn@kde.org>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
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
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
21*/
22
23#include "katetextline.h"
24#include <kdebug.h>
25
26TextLine::TextLine(uchar attribute, int context)
27 : text(0L), attributes(0L), attr(attribute), ctx(context), myMark (0)
28{
29}
30
31TextLine::~TextLine()
32{
33}
34
35
36void TextLine::replace(uint pos, uint delLen, const QChar *insText, uint insLen, uchar *insAttribs)
37{
38 uint oldLen = text.length();
39
40 text.remove (pos, delLen);
41 text.insert (pos, insText, insLen);
42
43 if (oldLen<text.length()) attributes.resize (text.length());
44
45 if (text.length() == 0)
46 {
47 attributes.resize (0);
48 return;
49 }
50
51 if (pos >= oldLen)
52 {
53 for (uint t=oldLen; t < attributes.size(); t++)
54 {
55 attributes[t]=0;
56 }
57 }
58
59 int newAtStuff = insLen-delLen;
60 for (uint m=pos+delLen; m < attributes.size(); m++)
61 {
62 if (m+newAtStuff >= attributes.size()) break;
63 if (m >= attributes.size()) break;
64
65 attributes[m+newAtStuff]=attributes[m];
66 }
67
68 if (insAttribs == 0L)
69 {
70 for (uint m3=pos; m3 < pos+insLen; m3++)
71 {
72 if (m3 < attributes.size()) attributes[m3]=0;
73 }
74 }
75 else
76 {
77 for (uint m2=pos; m2 < pos+insLen; m2++)
78 {
79 if (m2 < attributes.size()) attributes[m2]=insAttribs[m2-pos];
80 }
81 }
82
83 if (oldLen>text.length()) attributes.resize (text.length());
84}
85
86void TextLine::wrap(TextLine::Ptr nextLine, uint pos)
87{
88 int l = text.length() - pos;
89
90 if (l > 0)
91 {
92 nextLine->replace(0, 0, &text.unicode()[pos], l, &attributes[pos]);
93 attr = attributes[pos];
94 truncate(pos);
95 }
96}
97
98void TextLine::unWrap(uint pos, TextLine::Ptr nextLine, uint len) {
99
100 replace(pos, 0, nextLine->text.unicode(), len, nextLine->attributes.data());
101 attr = nextLine->getRawAttr(len);
102 nextLine->replace(0, len, 0L, 0);
103}
104
105int TextLine::firstChar() const {
106 uint z = 0;
107
108 while (z < text.length() && text[z].isSpace()) z++;
109
110 if (z < text.length())
111 return z;
112 else
113 return -1;
114}
115
116int TextLine::lastChar() const {
117 uint z = text.length();
118
119 while (z > 0 && text[z - 1].isSpace()) z--;
120 return z;
121}
122
123void TextLine::removeSpaces() {
124
125 while (text.length() > 0 && text[text.length() - 1].isSpace()) text.truncate (text.length()-1);
126}
127
128QChar TextLine::getChar(uint pos) const {
129 if (pos < text.length()) return text.constref(pos);
130 return ' ';
131}
132const QChar *TextLine::firstNonSpace()
133{
134 const QChar *ptr=getText();
135 int first=firstChar();
136 return (first > -1) ? ptr+first : ptr;
137}
138
139bool TextLine::startingWith(QString& match)
140{
141 return text.startsWith (match);
142}
143
144bool TextLine::endingWith(QString& match) {
145
146 int matchLen = match.length();
147
148 // Get the last chars of the textline
149 QString lastChars = text.right(matchLen);
150
151 return (lastChars == match);
152}
153
154int TextLine::cursorX(uint pos, uint tabChars) const {
155 int l, x, z;
156
157 l = (pos < text.length()) ? pos : text.length();
158 x = 0;
159 for (z = 0; z < l; z++) {
160 if (text[z] == '\t') x += tabChars - (x % tabChars); else x++;
161 }
162 x += pos - l;
163 return x;
164}
165
166void TextLine::setAttribs(uchar attribute, uint start, uint end) {
167 uint z;
168
169 if (end > text.length()) end = text.length();
170 for (z = start; z < end; z++) attributes[z] = (attributes[z] & taSelected) | attribute;
171}
172
173void TextLine::setAttr(uchar attribute) {
174 attr = (attr & taSelected) | attribute;
175}
176
177uchar TextLine::getAttr(uint pos) const {
178 if (pos < text.length()) return attributes[pos] & taAttrMask;
179 return attr & taAttrMask;
180}
181
182uchar TextLine::getAttr() const {
183 return attr & taAttrMask;
184}
185
186uchar TextLine::getRawAttr(uint pos) const {
187 if (pos < text.length()) return attributes[pos];
188 return (attr & taSelected) ? attr : attr | 256;
189}
190
191uchar TextLine::getRawAttr() const {
192 return attr;
193}
194
195void TextLine::setContext(int context) {
196 ctx = context;
197}
198
199int TextLine::getContext() const {
200 return ctx;
201}
202
203
204void TextLine::select(bool sel, uint start, uint end) {
205 uint z;
206
207 if (end > text.length()) end = text.length();
208 if (sel) {
209 for (z = start; z < end; z++) attributes[z] |= taSelected;
210 } else {
211 for (z = start; z < end; z++) attributes[z] &= ~taSelected;
212 }
213}
214
215void TextLine::selectEol(bool sel, uint pos) {
216 uint z;
217
218 if (sel) {
219 for (z = pos; z < text.length(); z++) attributes[z] |= taSelected;
220 attr |= taSelected;
221 } else {
222 for (z = pos; z < text.length(); z++) attributes[z] &= ~taSelected;
223 attr &= ~taSelected;
224 }
225}
226
227
228void TextLine::toggleSelect(uint start, uint end) {
229 uint z;
230
231 if (end > text.length()) end = text.length();
232 for (z = start; z < end; z++) attributes[z] = attributes[z] ^ taSelected;
233}
234
235
236void TextLine::toggleSelectEol(uint pos) {
237 uint z;
238
239 for (z = pos; z < text.length(); z++) attributes[z] = attributes[z] ^ taSelected;
240 attr = attr ^ taSelected;
241}
242
243
244int TextLine::numSelected() const {
245 uint z, n;
246
247 n = 0;
248 for (z = 0; z < text.length(); z++) if (attributes[z] & taSelected) n++;
249 return n;
250}
251
252bool TextLine::isSelected(uint pos) const {
253 if (pos < text.length()) return (attributes[pos] & taSelected);
254 return (attr & taSelected);
255}
256
257bool TextLine::isSelected() const {
258 return (attr & taSelected);
259}
260
261int TextLine::findSelected(uint pos) const {
262 while (pos < text.length() && attributes[pos] & taSelected) pos++;
263 return pos;
264}
265
266int TextLine::findUnselected(uint pos) const {
267 while (pos < text.length() && !(attributes[pos] & taSelected)) pos++;
268 return pos;
269}
270
271int TextLine::findRevSelected(uint pos) const {
272 while (pos > 0 && attributes[pos - 1] & taSelected) pos--;
273 return pos;
274}
275
276int TextLine::findRevUnselected(uint pos) const {
277 while (pos > 0 && !(attributes[pos - 1] & taSelected)) pos--;
278 return pos;
279}
280
281void TextLine::addMark (uint m)
282{
283 myMark = myMark | m;
284}
285
286void TextLine::delMark (uint m)
287{
288 myMark = myMark & ~m;
289}