Kate
kateviinsertmode.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 #include "kateviinsertmode.h"
00021 #include "kateviinputmodemanager.h"
00022 #include "kateview.h"
00023 #include "kateviewinternal.h"
00024 #include "katesmartrange.h"
00025 #include "katecursor.h"
00026
00027 KateViInsertMode::KateViInsertMode( KateViInputModeManager *viInputModeManager,
00028 KateView * view, KateViewInternal * viewInternal )
00029 {
00030 m_view = view;
00031 m_viewInternal = viewInternal;
00032 m_viInputModeManager = viInputModeManager;
00033 }
00034
00035 KateViInsertMode::~KateViInsertMode()
00036 {
00037 }
00038
00039 const QChar KateViInsertMode::getCharAtVirtualColumn( QString &line, int virtualColumn,
00040 int tabWidth ) const
00041 {
00042 int column = 0;
00043 int tempCol = 0;
00044
00045 while ( tempCol < virtualColumn ) {
00046 if ( line.at( column ) == '\t' ) {
00047 tempCol += tabWidth - ( tempCol % tabWidth );
00048 } else {
00049 tempCol++;
00050 }
00051
00052 if ( tempCol <= virtualColumn ) {
00053 column++;
00054
00055 if ( column >= line.length() ) {
00056 return QChar::Null;
00057 }
00058 }
00059 }
00060
00061 if ( line.length() > column )
00062 return line.at( column );
00063
00064 return QChar::Null;
00065 }
00066
00067 bool KateViInsertMode::commandInsertFromAbove()
00068 {
00069 KTextEditor::Cursor c( m_view->cursorPosition() );
00070
00071 if ( c.line() <= 0 ) {
00072 return false;
00073 }
00074
00075 QString line = doc()->line( c.line()-1 );
00076 int tabWidth = doc()->config()->tabWidth();
00077 QChar ch = getCharAtVirtualColumn( line, m_view->virtualCursorColumn(), tabWidth );
00078
00079 if ( ch == QChar::Null ) {
00080 return false;
00081 }
00082
00083 return doc()->insertText( c, ch );
00084 }
00085
00086 bool KateViInsertMode::commandInsertFromBelow()
00087 {
00088 KTextEditor::Cursor c( m_view->cursorPosition() );
00089
00090 if ( c.line() >= doc()->lines()-1 ) {
00091 return false;
00092 }
00093
00094 QString line = doc()->line( c.line()+1 );
00095 int tabWidth = doc()->config()->tabWidth();
00096 QChar ch = getCharAtVirtualColumn( line, m_view->virtualCursorColumn(), tabWidth );
00097
00098 if ( ch == QChar::Null ) {
00099 return false;
00100 }
00101
00102 return doc()->insertText( c, ch );
00103 }
00104
00105 bool KateViInsertMode::commandDeleteWord()
00106 {
00107 KTextEditor::Cursor c1( m_view->cursorPosition() );
00108 KTextEditor::Cursor c2;
00109
00110 c2 = findPrevWordStart( c1.line(), c1.column() );
00111
00112 if ( c2.line() != c1.line() ) {
00113 if ( c1.column() == 0 ) {
00114 c2.setColumn( doc()->line( c2.line() ).length() );
00115 } else {
00116 c2.setColumn( 0 );
00117 c2.setLine( c2.line()+1 );
00118 }
00119 }
00120
00121 KateViRange r( c2.line(), c2.column(), c1.line(), c1.column(), ViMotion::ExclusiveMotion );
00122
00123 return deleteRange( r, false, false );
00124 }
00125
00126 bool KateViInsertMode::commandIndent()
00127 {
00128
00129 return false;
00130 }
00131
00132 bool KateViInsertMode::commandUnindent()
00133 {
00134
00135 return false;
00136 }
00137
00138 bool KateViInsertMode::commandToFirstCharacterInFile()
00139 {
00140 KTextEditor::Cursor c;
00141
00142 c.setLine( 0 );
00143 c.setColumn( 0 );
00144
00145 updateCursor( c );
00146
00147 return true;
00148 }
00149
00150 bool KateViInsertMode::commandToLastCharacterInFile()
00151 {
00152 KTextEditor::Cursor c;
00153
00154 int lines = doc()->lines()-1;
00155 c.setLine( lines );
00156 c.setColumn( doc()->line( lines ).length() );
00157
00158 updateCursor( c );
00159
00160 return true;
00161 }
00162
00163 bool KateViInsertMode::commandMoveOneWordLeft()
00164 {
00165 KTextEditor::Cursor c( m_view->cursorPosition() );
00166 c = findPrevWordStart( c.line(), c.column() );
00167
00168 updateCursor( c );
00169 return true;
00170 }
00171
00172 bool KateViInsertMode::commandMoveOneWordRight()
00173 {
00174 KTextEditor::Cursor c( m_view->cursorPosition() );
00175 c = findNextWordStart( c.line(), c.column() );
00176
00177 updateCursor( c );
00178 return true;
00179 }
00180
00185 bool KateViInsertMode::handleKeypress( const QKeyEvent *e )
00186 {
00187
00188 if (e->modifiers() != Qt::ControlModifier && e->key() == Qt::Key_Backspace ) {
00189 m_view->backspace();
00190 return true;
00191 }
00192
00193 if ( e->modifiers() == Qt::NoModifier ) {
00194 switch ( e->key() ) {
00195 case Qt::Key_Escape:
00196 startNormalMode();
00197 return true;
00198 break;
00199 case Qt::Key_Left:
00200 m_view->cursorLeft();
00201 return true;
00202 case Qt::Key_Right:
00203 m_view->cursorRight();
00204 return true;
00205 case Qt::Key_Up:
00206 m_view->up();
00207 return true;
00208 case Qt::Key_Down:
00209 m_view->down();
00210 return true;
00211 case Qt::Key_Delete:
00212 m_view->keyDelete();
00213 return true;
00214 case Qt::Key_Home:
00215 m_view->home();
00216 return true;
00217 case Qt::Key_End:
00218 m_view->end();
00219 return true;
00220 case Qt::Key_PageUp:
00221 m_view->pageUp();
00222 return true;
00223 case Qt::Key_PageDown:
00224 m_view->pageDown();
00225 return true;
00226 default:
00227 return false;
00228 break;
00229 }
00230 } else if ( e->modifiers() == Qt::ControlModifier ) {
00231 switch( e->key() ) {
00232 case Qt::Key_BracketLeft:
00233 case Qt::Key_C:
00234 startNormalMode();
00235 return true;
00236 break;
00237 case Qt::Key_D:
00238 commandUnindent();
00239 return true;
00240 break;
00241 case Qt::Key_E:
00242 commandInsertFromBelow();
00243 return true;
00244 break;
00245 case Qt::Key_T:
00246 commandIndent();
00247 return true;
00248 break;
00249 case Qt::Key_W:
00250 commandDeleteWord();
00251 return true;
00252 break;
00253 case Qt::Key_Y:
00254 commandInsertFromAbove();
00255 return true;
00256 break;
00257 case Qt::Key_Home:
00258 commandToFirstCharacterInFile();
00259 return true;
00260 break;
00261 case Qt::Key_End:
00262 commandToLastCharacterInFile();
00263 return true;
00264 break;
00265 case Qt::Key_Left:
00266 commandMoveOneWordLeft();
00267 return true;
00268 break;
00269 case Qt::Key_Right:
00270 commandMoveOneWordRight();
00271 return true;
00272 break;
00273 default:
00274 return false;
00275 }
00276 }
00277
00278 return false;
00279 }