kate Library: katecursor.cpp Source File (kate Library)

| # kate Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# katecursor.cpp

    00001 /* This file is part of the KDE libraries
    00002    Copyright (C) 2002 Christian Couder <christian@kdevelop.org>
    00003    Copyright (C) 2001, 2003 Christoph Cullmann <cullmann@kde.org>
    00004    Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
    00005    Copyright (C) 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de>
    00006
    00007    This library is free software; you can redistribute it and/or
    00008    modify it under the terms of the GNU Library General Public
    00009    License version 2 as published by the Free Software Foundation.
    00010
    00011    This library is distributed in the hope that it will be useful,
    00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00014    Library General Public License for more details.
    00015
    00016    You should have received a copy of the GNU Library General Public License
    00017    along with this library; see the file COPYING.LIB.  If not, write to
    00018    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00019    Boston, MA 02111-1307, USA.
    00020 */
    00021
    00022 #include "katecursor.h"
    00023
    00024 #include "katedocument.h"
    00025 #include "katetextline.h"
    00026
    00027 //
    00028 // KateDocCursor implementation
    00029 //
    00030
    00031 KateDocCursor::KateDocCursor(KateDocument *doc) : KateTextCursor(), m_doc(doc)
    00032 {
    00033 }
    00034
    00035 KateDocCursor::KateDocCursor(int line, int col, KateDocument *doc)
    00036   : KateTextCursor(line, col), m_doc(doc)
    00037 {
    00038 }
    00039
    00040 bool KateDocCursor::validPosition(uint line, uint col)
    00041 {
    00042   return line < m_doc->numLines() && (int)col <= m_doc->lineLength(line);
    00043 }
    00044
    00045 bool KateDocCursor::validPosition()
    00046 {
    00047   return validPosition(line(), col());
    00048 }
    00049
    00050 void KateDocCursor::position(uint *pline, uint *pcol) const
    00051 {
    00052   if (pline)
    00053     *pline = (uint)line();
    00054
    00055   if (pcol)
    00056     *pcol = (uint)col();
    00057 }
    00058
    00059 bool KateDocCursor::setPosition(uint line, uint col)
    00060 {
    00061   bool ok = validPosition(line, col);
    00062
    00063   if(ok)
    00064     setPos(line, col);
    00065
    00066   return ok;
    00067 }
    00068
    00069 bool KateDocCursor::gotoNextLine()
    00070 {
    00071   bool ok = (line() + 1 < (int)m_doc->numLines());
    00072
    00073   if (ok) {
    00074     m_line++;
    00075     m_col = 0;
    00076   }
    00077
    00078   return ok;
    00079 }
    00080
    00081 bool KateDocCursor::gotoPreviousLine()
    00082 {
    00083   bool ok = (line() > 0);
    00084
    00085   if (ok) {
    00086     m_line--;
    00087     m_col = 0;
    00088   }
    00089
    00090   return ok;
    00091 }
    00092
    00093 bool KateDocCursor::gotoEndOfNextLine()
    00094 {
    00095   bool ok = gotoNextLine();
    00096   if(ok)
    00097     m_col = m_doc->lineLength(line());
    00098
    00099   return ok;
    00100 }
    00101
    00102 bool KateDocCursor::gotoEndOfPreviousLine()
    00103 {
    00104   bool ok = gotoPreviousLine();
    00105   if(ok)
    00106     m_col = m_doc->lineLength(line());
    00107
    00108   return ok;
    00109 }
    00110
    00111 int KateDocCursor::nbCharsOnLineAfter()
    00112 {
    00113   return ((int)m_doc->lineLength(line()) - col());
    00114 }
    00115
    00116 bool KateDocCursor::moveForward(uint nbChar)
    00117 {
    00118   int nbCharLeft = nbChar - nbCharsOnLineAfter();
    00119
    00120   if(nbCharLeft > 0) {
    00121     return gotoNextLine() && moveForward((uint)nbCharLeft);
    00122   } else {
    00123     m_col += nbChar;
    00124     return true;
    00125   }
    00126 }
    00127
    00128 bool KateDocCursor::moveBackward(uint nbChar)
    00129 {
    00130   int nbCharLeft = nbChar - m_col;
    00131   if(nbCharLeft > 0) {
    00132     return gotoEndOfPreviousLine() && moveBackward((uint)nbCharLeft);
    00133   } else {
    00134     m_col -= nbChar;
    00135     return true;
    00136   }
    00137 }
    00138
    00139 bool KateDocCursor::insertText(const QString& s)
    00140 {
    00141   return m_doc->insertText(line(), col(), s);
    00142 }
    00143
    00144 bool KateDocCursor::removeText(uint nbChar)
    00145 {
    00146   // Get a cursor at the end of the removed area
    00147   KateDocCursor endCursor = *this;
    00148   endCursor.moveForward(nbChar);
    00149
    00150   // Remove the text
    00151   return m_doc->removeText((uint)line(), (uint)col(),
    00152          (uint)endCursor.line(), (uint)endCursor.col());
    00153 }
    00154
    00155 QChar KateDocCursor::currentChar() const
    00156 {
    00157   return m_doc->plainKateTextLine(line())->getChar(col());
    00158 }
    00159
    00160 uchar KateDocCursor::currentAttrib() const
    00161 {
    00162   return m_doc->plainKateTextLine(line())->attribute(col());
    00163 }
    00164
    00165 bool KateDocCursor::nextNonSpaceChar()
    00166 {
    00167   for(; m_line < (int)m_doc->numLines(); m_line++) {
    00168     m_col = m_doc->plainKateTextLine(line())->nextNonSpaceChar(col());
    00169     if(m_col != -1)
    00170       return true; // Next non-space char found
    00171     m_col = 0;
    00172   }
    00173   // No non-space char found
    00174   setPos(-1, -1);
    00175   return false;
    00176 }
    00177
    00178 bool KateDocCursor::previousNonSpaceChar()
    00179 {
    00180   while (true) {
    00181     m_col = m_doc->plainKateTextLine(line())->previousNonSpaceChar(col());
    00182     if(m_col != -1) return true; // Previous non-space char found
    00183     if(m_line == 0) return false;
    00184     --m_line;
    00185     m_col = m_doc->plainKateTextLine(m_line)->length();
    00186   }
    00187   // No non-space char found
    00188   setPos(-1, -1);
    00189   return false;
    00190 }
    00191
    00192 // kate: space-indent on; indent-width 2; replace-tabs on;

This file is part of the documentation for kate Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:36:30 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003