• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kate
 

kate

  • kate
  • part
katetextline.cpp
1/* This file is part of the KDE libraries
2 Copyright (C) 2001-2003 Christoph Cullmann <cullmann@kde.org>
3 Copyright (C) 2002 Joseph Wenninger <jowenn@kde.org>
4
5 Based on:
6 KateTextLine : Copyright (C) 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de>
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License version 2 as published by the Free Software Foundation.
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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21*/
22
23#include "katetextline.h"
24#include "katerenderer.h"
25
26#include <tdeglobal.h>
27
28#include <tqregexp.h>
29
30KateTextLine::KateTextLine ()
31 : m_flags(0)
32{
33}
34
35KateTextLine::~KateTextLine()
36{
37}
38
39void KateTextLine::insertText (uint pos, uint insLen, const TQChar *insText, uchar *insAttribs)
40{
41 // nothing to do
42 if (insLen == 0)
43 return;
44
45 // calc new textLen, store old
46 uint oldTextLen = m_text.length();
47 m_text.insert (pos, insText, insLen);
48 uint textLen = m_text.length();
49
50 // resize the array
51 m_attributes.resize (textLen);
52
53 // HA, insert behind text end, fill with spaces
54 if (pos >= oldTextLen)
55 {
56 for (uint z = oldTextLen; z < pos; z++)
57 m_attributes[z] = 0;
58 }
59 // HA, insert in text, move the old text behind pos
60 else if (oldTextLen > 0)
61 {
62 for (int z = oldTextLen -1; z >= (int) pos; z--)
63 m_attributes[z+insLen] = m_attributes[z];
64 }
65
66 // BUH, actually insert the new text
67 for (uint z = 0; z < insLen; z++)
68 {
69 if (insAttribs == 0)
70 m_attributes[z+pos] = 0;
71 else
72 m_attributes[z+pos] = insAttribs[z];
73 }
74}
75
76void KateTextLine::removeText (uint pos, uint delLen)
77{
78 // nothing to do
79 if (delLen == 0)
80 return;
81
82 uint textLen = m_text.length();
83
84 if (textLen == 0)
85 return; // uh, again nothing real to do ;)
86
87 if (pos >= textLen)
88 return;
89
90 if ((pos + delLen) > textLen)
91 delLen = textLen - pos;
92
93 // BU, MOVE THE OLD TEXT AROUND
94 for (uint z = pos; z < textLen - delLen; z++)
95 m_attributes[z] = m_attributes[z+delLen];
96
97 m_text.remove (pos, delLen);
98 m_attributes.resize (m_text.length ());
99}
100
101void KateTextLine::truncate(uint newLen)
102{
103 if (newLen < m_text.length())
104 {
105 m_text.truncate (newLen);
106 m_attributes.truncate (newLen);
107 }
108}
109
110int KateTextLine::nextNonSpaceChar(uint pos) const
111{
112 const uint len = m_text.length();
113 const TQChar *unicode = m_text.unicode();
114
115 for(uint i = pos; i < len; i++)
116 {
117 if(!unicode[i].isSpace())
118 return i;
119 }
120
121 return -1;
122}
123
124int KateTextLine::previousNonSpaceChar(uint pos) const
125{
126 const int len = m_text.length();
127
128 if (pos >= (uint)len)
129 pos = len - 1;
130
131 const TQChar *unicode = m_text.unicode();
132
133 for(int i = pos; i >= 0; i--)
134 {
135 if(!unicode[i].isSpace())
136 return i;
137 }
138
139 return -1;
140}
141
142int KateTextLine::firstChar() const
143{
144 return nextNonSpaceChar(0);
145}
146
147int KateTextLine::lastChar() const
148{
149 return previousNonSpaceChar(m_text.length() - 1);
150}
151
152const TQChar *KateTextLine::firstNonSpace() const
153{
154 int first = firstChar();
155 return (first > -1) ? ((TQChar*)m_text.unicode())+first : m_text.unicode();
156}
157
158uint KateTextLine::indentDepth (uint tabwidth) const
159{
160 uint d = 0;
161 const uint len = m_text.length();
162 const TQChar *unicode = m_text.unicode();
163
164 for(uint i = 0; i < len; i++)
165 {
166 if(unicode[i].isSpace())
167 {
168 if (unicode[i] == TQChar('\t'))
169 d += tabwidth - (d % tabwidth);
170 else
171 d++;
172 }
173 else
174 return d;
175 }
176
177 return d;
178}
179
180bool KateTextLine::stringAtPos(uint pos, const TQString& match) const
181{
182 const uint len = m_text.length();
183 const uint matchlen = match.length();
184
185 if ((pos+matchlen) > len)
186 return false;
187
188 // (pos > len) in case the uint pos was assigned a signed -1, pos+matchlen can
189 // overflow again which (pos+matchlen > len) does not catch; see bugs #129263 and #129580
190 Q_ASSERT(pos < len);
191
192 const TQChar *unicode = m_text.unicode();
193 const TQChar *matchUnicode = match.unicode();
194
195 for (uint i=0; i < matchlen; i++)
196 if (unicode[i+pos] != matchUnicode[i])
197 return false;
198
199 return true;
200}
201
202bool KateTextLine::startingWith(const TQString& match) const
203{
204 const uint matchlen = match.length();
205
206 if (matchlen > m_text.length())
207 return false;
208
209 const TQChar *unicode = m_text.unicode();
210 const TQChar *matchUnicode = match.unicode();
211
212 for (uint i=0; i < matchlen; i++)
213 if (unicode[i] != matchUnicode[i])
214 return false;
215
216 return true;
217}
218
219bool KateTextLine::endingWith(const TQString& match) const
220{
221 const uint matchlen = match.length();
222
223 if (matchlen > m_text.length())
224 return false;
225
226 const TQChar *unicode = m_text.unicode();
227 const TQChar *matchUnicode = match.unicode();
228
229 uint start = m_text.length() - matchlen;
230 for (uint i=0; i < matchlen; i++)
231 if (unicode[start+i] != matchUnicode[i])
232 return false;
233
234 return true;
235}
236
237int KateTextLine::cursorX(uint pos, uint tabChars) const
238{
239 uint x = 0;
240
241 const uint n = kMin (pos, (uint)m_text.length());
242 const TQChar *unicode = m_text.unicode();
243
244 for ( uint z = 0; z < n; z++)
245 {
246 if (unicode[z] == TQChar('\t'))
247 x += tabChars - (x % tabChars);
248 else
249 x++;
250 }
251
252 return x;
253}
254
255
256uint KateTextLine::lengthWithTabs (uint tabChars) const
257{
258 uint x = 0;
259 const uint len = m_text.length();
260 const TQChar *unicode = m_text.unicode();
261
262 for ( uint z = 0; z < len; z++)
263 {
264 if (unicode[z] == TQChar('\t'))
265 x += tabChars - (x % tabChars);
266 else
267 x++;
268 }
269
270 return x;
271}
272
273bool KateTextLine::searchText (uint startCol, const TQString &text, uint *foundAtCol, uint *matchLen, bool casesensitive, bool backwards)
274{
275 int index;
276
277 if (backwards)
278 {
279 int col = startCol;
280 uint l = text.length();
281 // allow finding the string ending at eol
282 if ( col == (int) m_text.length() ) ++startCol;
283
284 do {
285 index = m_text.findRev( text, col, casesensitive );
286 col--;
287 } while ( col >= 0 && l + index >= startCol );
288 }
289 else
290 index = m_text.find (text, startCol, casesensitive);
291
292 if (index > -1)
293 {
294 if (foundAtCol)
295 (*foundAtCol) = index;
296 if (matchLen)
297 (*matchLen)=text.length();
298 return true;
299 }
300
301 return false;
302}
303
304bool KateTextLine::searchText (uint startCol, const TQRegExp &regexp, uint *foundAtCol, uint *matchLen, bool backwards)
305{
306 int index;
307
308 if (backwards)
309 {
310 int col = startCol;
311
312 // allow finding the string ending at eol
313 if ( col == (int) m_text.length() ) ++startCol;
314 do {
315 index = regexp.searchRev (m_text, col);
316 col--;
317 } while ( col >= 0 && regexp.matchedLength() + index >= (int)startCol );
318 }
319 else
320 index = regexp.search (m_text, startCol);
321
322 if (index > -1)
323 {
324 if (foundAtCol)
325 (*foundAtCol) = index;
326
327 if (matchLen)
328 (*matchLen)=regexp.matchedLength();
329 return true;
330 }
331
332 return false;
333}
334
335char *KateTextLine::dump (char *buf, bool withHighlighting) const
336{
337 uint l = m_text.length();
338 char f = m_flags;
339
340 if (!withHighlighting)
341 f = f | KateTextLine::flagNoOtherData;
342
343 memcpy(buf, (char *) &f, 1);
344 buf += 1;
345
346 memcpy(buf, &l, sizeof(uint));
347 buf += sizeof(uint);
348
349 memcpy(buf, (char *) m_text.unicode(), sizeof(TQChar)*l);
350 buf += sizeof(TQChar) * l;
351
352 if (!withHighlighting)
353 return buf;
354
355 memcpy(buf, (char *)m_attributes.data(), sizeof(uchar) * l);
356 buf += sizeof (uchar) * l;
357
358 uint lctx = m_ctx.size();
359 uint lfold = m_foldingList.size();
360 uint lind = m_indentationDepth.size();
361
362 memcpy(buf, &lctx, sizeof(uint));
363 buf += sizeof(uint);
364
365 memcpy(buf, &lfold, sizeof(uint));
366 buf += sizeof(uint);
367
368 memcpy(buf, &lind, sizeof(uint));
369 buf += sizeof(uint);
370
371 memcpy(buf, (char *)m_ctx.data(), sizeof(short) * lctx);
372 buf += sizeof (short) * lctx;
373
374 memcpy(buf, (char *)m_foldingList.data(), sizeof(uint)*lfold);
375 buf += sizeof (uint) * lfold;
376
377 memcpy(buf, (char *)m_indentationDepth.data(), sizeof(unsigned short) * lind);
378 buf += sizeof (unsigned short) * lind;
379
380 return buf;
381}
382
383char *KateTextLine::restore (char *buf)
384{
385 uint l = 0;
386 char f = 0;
387
388 memcpy((char *) &f, buf, 1);
389 buf += 1;
390
391 // text + context length read
392 memcpy((char *) &l, buf, sizeof(uint));
393 buf += sizeof(uint);
394
395 // text + attributes
396 m_text.setUnicode ((TQChar *) buf, l);
397 buf += sizeof(TQChar) * l;
398
399 // we just restore a KateTextLine from a buffer first time
400 if (f & KateTextLine::flagNoOtherData)
401 {
402 m_flags = 0;
403
404 if (f & KateTextLine::flagAutoWrapped)
405 m_flags = m_flags | KateTextLine::flagAutoWrapped;
406
407 // fill with clean empty attribs !
408 m_attributes.fill (0, l);
409
410 return buf;
411 }
412 else
413 m_flags = f;
414
415 m_attributes.duplicate ((uchar *) buf, l);
416 buf += sizeof(uchar) * l;
417
418 uint lctx = 0;
419 uint lfold = 0;
420 uint lind = 0;
421
422 memcpy((char *) &lctx, buf, sizeof(uint));
423 buf += sizeof(uint);
424
425 memcpy((char *) &lfold, buf, sizeof(uint));
426 buf += sizeof(uint);
427
428 memcpy((char *) &lind, buf, sizeof(uint));
429 buf += sizeof(uint);
430
431 m_ctx.duplicate ((short *) buf, lctx);
432 buf += sizeof(short) * lctx;
433
434 m_foldingList.duplicate ((uint *) buf, lfold);
435 buf += sizeof(uint)*lfold;
436
437 m_indentationDepth.duplicate ((unsigned short *) buf, lind);
438 buf += sizeof(unsigned short) * lind;
439
440 return buf;
441}
KateTextLine::endingWith
bool endingWith(const TQString &match) const
Is the line ending with the given string.
Definition: katetextline.cpp:219
KateTextLine::searchText
bool searchText(uint startCol, const TQString &text, uint *foundAtCol, uint *matchLen, bool casesensitive=true, bool backwards=false)
search given string
Definition: katetextline.cpp:273
KateTextLine::previousNonSpaceChar
int previousNonSpaceChar(uint pos) const
Find the position of the previous char that is not a space.
Definition: katetextline.cpp:124
KateTextLine::nextNonSpaceChar
int nextNonSpaceChar(uint pos) const
Find the position of the next char that is not a space.
Definition: katetextline.cpp:110
KateTextLine::lengthWithTabs
uint lengthWithTabs(uint tabChars) const
Returns the text length with tabs calced in.
Definition: katetextline.cpp:256
KateTextLine::stringAtPos
bool stringAtPos(uint pos, const TQString &match) const
Can we find the given string at the given position.
Definition: katetextline.cpp:180
KateTextLine::KateTextLine
KateTextLine()
Constructor Creates an empty text line with given attribute and syntax highlight context.
Definition: katetextline.cpp:30
KateTextLine::truncate
void truncate(uint newLen)
Truncates the textline to the new length.
Definition: katetextline.cpp:101
KateTextLine::firstChar
int firstChar() const
Returns the position of the first non-whitespace character.
Definition: katetextline.cpp:142
KateTextLine::insertText
void insertText(uint pos, uint insLen, const TQChar *insText, uchar *insAttribs=0)
insert text into line
Definition: katetextline.cpp:39
KateTextLine::indentDepth
uint indentDepth(uint tabwidth) const
indentation depth of this line
Definition: katetextline.cpp:158
KateTextLine::cursorX
int cursorX(uint pos, uint tabChars) const
Returns the x position of the cursor at the given position, which depends on the number of tab charac...
Definition: katetextline.cpp:237
KateTextLine::text
const TQChar * text() const
Gets the text as a unicode representation.
Definition: katetextline.h:151
KateTextLine::lastChar
int lastChar() const
Returns the position of the last non-whitespace character.
Definition: katetextline.cpp:147
KateTextLine::~KateTextLine
~KateTextLine()
Destructor.
Definition: katetextline.cpp:35
KateTextLine::restore
char * restore(char *buf)
Restores the line from *buf and counts buff dumpSize bytes up as return value.
Definition: katetextline.cpp:383
KateTextLine::removeText
void removeText(uint pos, uint delLen)
remove text at given position
Definition: katetextline.cpp:76
KateTextLine::dump
char * dump(char *buf, bool withHighlighting) const
Dumps the line to *buf and counts buff dumpSize bytes up as return value.
Definition: katetextline.cpp:335
KateTextLine::firstNonSpace
const TQChar * firstNonSpace() const
Gets a null terminated pointer to first non space char.
Definition: katetextline.cpp:152
KateTextLine::startingWith
bool startingWith(const TQString &match) const
Is the line starting with the given string.
Definition: katetextline.cpp:202

kate

Skip menu "kate"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kate

Skip menu "kate"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kate by doxygen 1.9.4
This website is maintained by Timothy Pearson.