00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
#include "katecursor.h"
00023
00024
#include "katedocument.h"
00025
#include "katetextline.h"
00026
00027
00028
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
00147
KateDocCursor endCursor = *
this;
00148 endCursor.
moveForward(nbChar);
00149
00150
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->kateTextLine(line())->getChar(col());
00158 }
00159
00160 bool KateDocCursor::nextNonSpaceChar()
00161 {
00162
for(; m_line < (
int)m_doc->numLines(); m_line++) {
00163 m_col = m_doc->kateTextLine(line())->nextNonSpaceChar(col());
00164
if(m_col != -1)
00165
return true;
00166 m_col = 0;
00167 }
00168
00169 setPos(-1, -1);
00170
return false;
00171 }
00172
00173 bool KateDocCursor::previousNonSpaceChar()
00174 {
00175
while (
true) {
00176 m_col = m_doc->kateTextLine(line())->previousNonSpaceChar(col());
00177
if(m_col != -1)
return true;
00178
if(m_line == 0)
return false;
00179 --m_line;
00180 m_col = m_doc->kateTextLine(m_line)->length();
00181 }
00182
00183 setPos(-1, -1);
00184
return false;
00185 }
00186
00187