Kate
katecursor.cpp
Go to the documentation of this file.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->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;
00171 m_col = 0;
00172 }
00173
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;
00183 if(m_line == 0) return false;
00184 --m_line;
00185 m_col = m_doc->plainKateTextLine(m_line)->length();
00186 }
00187
00188 setPos(-1, -1);
00189 return false;
00190 }
00191
00192