kate Library API Documentation

katetextline.cpp

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2001-2003 Christoph Cullmann <cullmann@kde.org>
00003    Copyright (C) 2002 Joseph Wenninger <jowenn@kde.org>
00004 
00005    Based on:
00006      KateTextLine : Copyright (C) 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de>
00007 
00008    This library is free software; you can redistribute it and/or
00009    modify it under the terms of the GNU Library General Public
00010    License version 2 as published by the Free Software Foundation.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public License
00018    along with this library; see the file COPYING.LIB.  If not, write to
00019    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020    Boston, MA 02111-1307, USA.
00021 */
00022 
00023 #include "katetextline.h"
00024 
00025 #include <qregexp.h>
00026 #include <kglobal.h>
00027 
00028 KateTextLine::KateTextLine ()
00029   : m_flags(KateTextLine::flagVisible)
00030 {
00031 }
00032 
00033 KateTextLine::~KateTextLine()
00034 {
00035 }
00036 
00037 void KateTextLine::insertText (uint pos, uint insLen, const QChar *insText, uchar *insAttribs)
00038 {
00039   // nothing to do
00040   if (insLen == 0)
00041     return;
00042 
00043   // calc new textLen, store old
00044   uint oldTextLen = m_text.length();
00045   m_text.insert (pos, insText, insLen);
00046   uint textLen = m_text.length();
00047 
00048   // resize the array
00049   m_attributes.resize (textLen);
00050 
00051   // HA, insert behind text end, fill with spaces
00052   if (pos >= oldTextLen)
00053   {
00054     for (uint z = oldTextLen; z < pos; z++)
00055       m_attributes[z] = 0;
00056   }
00057   // HA, insert in text, move the old text behind pos
00058   else if (oldTextLen > 0)
00059   {
00060     for (int z = oldTextLen -1; z >= (int) pos; z--)
00061       m_attributes[z+insLen] = m_attributes[z];
00062   }
00063 
00064   // BUH, actually insert the new text
00065   for (uint z = 0; z < insLen; z++)
00066   {
00067     if (insAttribs == 0)
00068       m_attributes[z+pos] = 0;
00069     else
00070       m_attributes[z+pos] = insAttribs[z];
00071   }
00072 }
00073 
00074 void KateTextLine::removeText (uint pos, uint delLen)
00075 {
00076   // nothing to do
00077   if (delLen == 0)
00078     return;
00079 
00080   uint textLen = m_text.length();
00081 
00082   if (textLen == 0)
00083     return; // uh, again nothing real to do ;)
00084 
00085   if (pos >= textLen)
00086     return;
00087 
00088   if ((pos + delLen) > textLen)
00089     delLen = textLen - pos;
00090 
00091   // BU, MOVE THE OLD TEXT AROUND
00092   for (uint z = pos; z < textLen - delLen; z++)
00093     m_attributes[z] = m_attributes[z+delLen];
00094 
00095   m_text.remove (pos, delLen);
00096   m_attributes.resize (m_text.length ());
00097 }
00098 
00099 void KateTextLine::truncate(uint newLen)
00100 {
00101   if (newLen < m_text.length())
00102   {
00103     m_text.truncate (newLen);
00104     m_attributes.truncate (newLen);
00105   }
00106 }
00107 
00108 int KateTextLine::nextNonSpaceChar(uint pos) const
00109 {
00110   for(int i = pos; i < (int)m_text.length(); i++)
00111   {
00112     if(!m_text[i].isSpace())
00113       return i;
00114   }
00115 
00116   return -1;
00117 }
00118 
00119 int KateTextLine::previousNonSpaceChar(uint pos) const
00120 {
00121   if (pos >= m_text.length())
00122     pos = m_text.length() - 1;
00123 
00124   for(int i = pos; i >= 0; i--)
00125   {
00126     if(!m_text[i].isSpace())
00127       return i;
00128   }
00129 
00130   return -1;
00131 }
00132 
00133 int KateTextLine::firstChar() const
00134 {
00135   return nextNonSpaceChar(0);
00136 }
00137 
00138 int KateTextLine::lastChar() const
00139 {
00140   return previousNonSpaceChar(m_text.length() - 1);
00141 }
00142 
00143 const QChar *KateTextLine::firstNonSpace() const
00144 {
00145   int first = firstChar();
00146   return (first > -1) ? ((QChar*)m_text.unicode())+first : m_text.unicode();
00147 }
00148 
00149 uint KateTextLine::indentDepth (uint tabwidth) const
00150 {
00151   uint d = 0;
00152 
00153   for(uint i = 0; i < m_text.length(); i++)
00154   {
00155     if(m_text[i].isSpace())
00156     {
00157       if (m_text[i] == QChar('\t'))
00158         d += tabwidth - (d % tabwidth);
00159       else
00160         d++;
00161     }
00162     else
00163       return d;
00164   }
00165 
00166   return d;
00167 }
00168 
00169 bool KateTextLine::stringAtPos(uint pos, const QString& match) const
00170 {
00171   return (m_text.mid(pos, match.length()) == match);
00172 }
00173 
00174 bool KateTextLine::startingWith(const QString& match) const
00175 {
00176   return (m_text.left(match.length()) == match);
00177 }
00178 
00179 bool KateTextLine::endingWith(const QString& match) const
00180 {
00181   return (m_text.right(match.length()) == match);
00182 }
00183 
00184 int KateTextLine::cursorX(uint pos, uint tabChars) const
00185 {
00186   uint x = 0;
00187   uint z;
00188   for ( z = 0; z < kMin (pos, m_text.length()); z++)
00189   {
00190     if (m_text[z] == QChar('\t'))
00191       x += tabChars - (x % tabChars);
00192     else
00193       x++;
00194   }
00195 
00196   return x;
00197 }
00198 
00199 void KateTextLine::setAttribs(uchar attribute, uint start, uint end)
00200 {
00201   if (end > m_attributes.size())
00202     end = m_attributes.size();
00203 
00204   for (uint z = start; z < end; z++)
00205     m_attributes[z] = attribute;
00206 }
00207 
00208 bool KateTextLine::searchText (uint startCol, const QString &text, uint *foundAtCol, uint *matchLen, bool casesensitive, bool backwards)
00209 {
00210   int index;
00211 
00212   if (backwards)
00213     index = m_text.findRev (text, startCol, casesensitive);
00214   else
00215     index = m_text.find (text, startCol, casesensitive);
00216 
00217   if (index > -1)
00218   {
00219     (*foundAtCol) = index;
00220     (*matchLen)=text.length();
00221     return true;
00222   }
00223 
00224   return false;
00225 }
00226 
00227 bool KateTextLine::searchText (uint startCol, const QRegExp &regexp, uint *foundAtCol, uint *matchLen, bool backwards)
00228 {
00229   int index;
00230 
00231   if (backwards)
00232     index = regexp.searchRev (m_text, startCol);
00233   else
00234     index = regexp.search (m_text, startCol);
00235 
00236   if (index > -1)
00237   {
00238     (*foundAtCol) = index;
00239     (*matchLen)=regexp.matchedLength();
00240     return true;
00241   }
00242 
00243   return false;
00244 }
00245 
00246 char *KateTextLine::dump (char *buf, bool withHighlighting) const
00247 {
00248   uint l = m_text.length();
00249   char f = m_flags;
00250 
00251   if (!withHighlighting)
00252     f = f | KateTextLine::flagNoOtherData;
00253 
00254   memcpy(buf, (char *) &f, 1);
00255   buf += 1;
00256 
00257   memcpy(buf, &l, sizeof(uint));
00258   buf += sizeof(uint);
00259 
00260   memcpy(buf, (char *) m_text.unicode(), sizeof(QChar)*l);
00261   buf += sizeof(QChar) * l;
00262 
00263   if (!withHighlighting)
00264     return buf;
00265 
00266   memcpy(buf, (char *)m_attributes.data(), sizeof(uchar) * l);
00267   buf += sizeof (uchar) * l;
00268 
00269   uint lctx = m_ctx.size();
00270   uint lfold = m_foldingList.size();
00271   uint lind = m_indentationDepth.size();
00272 
00273   memcpy(buf, &lctx, sizeof(uint));
00274   buf += sizeof(uint);
00275 
00276   memcpy(buf, &lfold, sizeof(uint));
00277   buf += sizeof(uint);
00278 
00279   memcpy(buf, &lind, sizeof(uint));
00280   buf += sizeof(uint);
00281 
00282   memcpy(buf, (char *)m_ctx.data(), sizeof(short) * lctx);
00283   buf += sizeof (short) * lctx;
00284 
00285   memcpy(buf, (char *)m_foldingList.data(), lfold);
00286   buf += sizeof (signed char) * lfold;
00287 
00288   memcpy(buf, (char *)m_indentationDepth.data(), sizeof(unsigned short) * lind);
00289   buf += sizeof (unsigned short) * lind;
00290 
00291   return buf;
00292 }
00293 
00294 char *KateTextLine::restore (char *buf)
00295 {
00296   uint l = 0;
00297   char f = 0;
00298 
00299   memcpy((char *) &f, buf, 1);
00300   buf += 1;
00301 
00302   // text + context length read
00303   memcpy((char *) &l, buf, sizeof(uint));
00304   buf += sizeof(uint);
00305 
00306   // text + attributes
00307   m_text.setUnicode ((QChar *) buf, l);
00308   buf += sizeof(QChar) * l;
00309 
00310   // we just restore a KateTextLine from a buffer first time
00311   if (f & KateTextLine::flagNoOtherData)
00312   {
00313     m_flags = KateTextLine::flagVisible;
00314 
00315     if (f & KateTextLine::flagAutoWrapped)
00316       m_flags = m_flags | KateTextLine::flagAutoWrapped;
00317 
00318     // fill with clean empty attribs !
00319     m_attributes.fill (0, l);
00320 
00321     return buf;
00322   }
00323   else
00324     m_flags = f;
00325 
00326   m_attributes.duplicate ((uchar *) buf, l);
00327   buf += sizeof(uchar) * l;
00328 
00329   uint lctx = 0;
00330   uint lfold = 0;
00331   uint lind = 0;
00332 
00333   memcpy((char *) &lctx, buf, sizeof(uint));
00334   buf += sizeof(uint);
00335 
00336   memcpy((char *) &lfold, buf, sizeof(uint));
00337   buf += sizeof(uint);
00338 
00339   memcpy((char *) &lind, buf, sizeof(uint));
00340   buf += sizeof(uint);
00341 
00342   m_ctx.duplicate ((short *) buf, lctx);
00343   buf += sizeof(short) * lctx;
00344 
00345   m_foldingList.duplicate ((signed char *) buf, lfold);
00346   buf += lfold;
00347 
00348   m_indentationDepth.duplicate ((unsigned short *) buf, lind);
00349   buf += sizeof(unsigned short) * lind;
00350 
00351   return buf;
00352 }
00353 
00354 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE Logo
This file is part of the documentation for kate Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Nov 27 13:52:43 2004 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003