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 #include "katesmartrange.h"
00027
00028 #include <ktexteditor/attribute.h>
00029
00030
00031
00032
00033
00034 KateDocCursor::KateDocCursor(KateDocument *doc) : KTextEditor::Cursor(), m_doc(doc)
00035 {
00036 }
00037
00038 KateDocCursor::KateDocCursor(const KTextEditor::Cursor &position, KateDocument *doc)
00039 : KTextEditor::Cursor(position), m_doc(doc)
00040 {
00041 }
00042
00043 KateDocCursor::KateDocCursor(int line, int col, KateDocument *doc)
00044 : KTextEditor::Cursor(line, col), m_doc(doc)
00045 {
00046 }
00047
00048 bool KateDocCursor::validPosition(int line, int col)
00049 {
00050 return line >= 0 && col >= 0 && line < m_doc->lines() && col <= m_doc->lineLength(line);
00051 }
00052
00053 bool KateDocCursor::validPosition()
00054 {
00055 return validPosition(line(), column());
00056 }
00057
00058 bool KateDocCursor::gotoNextLine()
00059 {
00060 bool ok = (line() + 1 < m_doc->lines());
00061
00062 if (ok) {
00063 m_line++;
00064 m_column = 0;
00065 }
00066
00067 return ok;
00068 }
00069
00070 bool KateDocCursor::gotoPreviousLine()
00071 {
00072 bool ok = (line() > 0);
00073
00074 if (ok) {
00075 m_line--;
00076 m_column = 0;
00077 }
00078
00079 return ok;
00080 }
00081
00082 bool KateDocCursor::gotoEndOfNextLine()
00083 {
00084 bool ok = gotoNextLine();
00085 if(ok)
00086 m_column = m_doc->lineLength(line());
00087
00088 return ok;
00089 }
00090
00091 bool KateDocCursor::gotoEndOfPreviousLine()
00092 {
00093 bool ok = gotoPreviousLine();
00094 if(ok)
00095 m_column = m_doc->lineLength(line());
00096
00097 return ok;
00098 }
00099
00100 int KateDocCursor::nbCharsOnLineAfter()
00101 {
00102 return ((int)m_doc->lineLength(line()) - column());
00103 }
00104
00105 bool KateDocCursor::moveForward(uint nbChar)
00106 {
00107 int nbCharLeft = nbChar - nbCharsOnLineAfter();
00108
00109 if(nbCharLeft > 0) {
00110 return gotoNextLine() && moveForward((uint)nbCharLeft - 1);
00111 } else {
00112 m_column += nbChar;
00113 return true;
00114 }
00115 }
00116
00117 bool KateDocCursor::moveBackward(uint nbChar)
00118 {
00119 int nbCharLeft = nbChar - m_column;
00120 if(nbCharLeft > 0) {
00121 return gotoEndOfPreviousLine() && moveBackward((uint)nbCharLeft - 1);
00122 } else {
00123 m_column -= nbChar;
00124 return true;
00125 }
00126 }
00127
00128 bool KateDocCursor::insertText(const QString& s)
00129 {
00130 return m_doc->insertText(*this, s);
00131 }
00132
00133 bool KateDocCursor::removeText(uint nbChar)
00134 {
00135
00136 KateDocCursor endCursor = *this;
00137 endCursor.moveForward(nbChar);
00138
00139
00140 return m_doc->removeText(KTextEditor::Range(*this, endCursor));
00141 }
00142
00143 QChar KateDocCursor::currentChar() const
00144 {
00145 return m_doc->plainKateTextLine(line())->at(column());
00146 }
00147
00148 uchar KateDocCursor::currentAttrib() const
00149 {
00150 return m_doc->plainKateTextLine(line())->attribute(column());
00151 }
00152
00153 bool KateDocCursor::nextNonSpaceChar()
00154 {
00155 for(; m_line < m_doc->lines(); ++m_line) {
00156 m_column = m_doc->plainKateTextLine(line())->nextNonSpaceChar(column());
00157 if(m_column != -1)
00158 return true;
00159 m_column = 0;
00160 }
00161
00162 setPosition(-1, -1);
00163 return false;
00164 }
00165
00166 bool KateDocCursor::previousNonSpaceChar()
00167 {
00168 while (true) {
00169 m_column = m_doc->plainKateTextLine(line())->previousNonSpaceChar(column());
00170 if(m_column != -1) return true;
00171 if(m_line == 0) return false;
00172 --m_line;
00173 m_column = m_doc->plainKateTextLine(m_line)->length();
00174 }
00175
00176 setPosition(-1, -1);
00177 return false;
00178 }
00179
00180