Kate
kateviinsertmode.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries and the Kate part. 00002 * 00003 * Copyright (C) 2008 Erlend Hamberg <ehamberg@gmail.com> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "kateviinsertmode.h" 00022 #include "kateviinputmodemanager.h" 00023 #include "kateview.h" 00024 #include "kateviewinternal.h" 00025 #include "kateconfig.h" 00026 #include "katecompletionwidget.h" 00027 00028 using KTextEditor::Cursor; 00029 00030 KateViInsertMode::KateViInsertMode( KateViInputModeManager *viInputModeManager, 00031 KateView * view, KateViewInternal * viewInternal ) : KateViModeBase() 00032 { 00033 m_view = view; 00034 m_viewInternal = viewInternal; 00035 m_viInputModeManager = viInputModeManager; 00036 } 00037 00038 KateViInsertMode::~KateViInsertMode() 00039 { 00040 } 00041 00042 00043 bool KateViInsertMode::commandInsertFromAbove() 00044 { 00045 Cursor c( m_view->cursorPosition() ); 00046 00047 if ( c.line() <= 0 ) { 00048 return false; 00049 } 00050 00051 QString line = doc()->line( c.line()-1 ); 00052 int tabWidth = doc()->config()->tabWidth(); 00053 QChar ch = getCharAtVirtualColumn( line, m_view->virtualCursorColumn(), tabWidth ); 00054 00055 if ( ch == QChar::Null ) { 00056 return false; 00057 } 00058 00059 return doc()->insertText( c, ch ); 00060 } 00061 00062 bool KateViInsertMode::commandInsertFromBelow() 00063 { 00064 Cursor c( m_view->cursorPosition() ); 00065 00066 if ( c.line() >= doc()->lines()-1 ) { 00067 return false; 00068 } 00069 00070 QString line = doc()->line( c.line()+1 ); 00071 int tabWidth = doc()->config()->tabWidth(); 00072 QChar ch = getCharAtVirtualColumn( line, m_view->virtualCursorColumn(), tabWidth ); 00073 00074 if ( ch == QChar::Null ) { 00075 return false; 00076 } 00077 00078 return doc()->insertText( c, ch ); 00079 } 00080 00081 bool KateViInsertMode::commandDeleteWord() 00082 { 00083 Cursor c1( m_view->cursorPosition() ); 00084 Cursor c2; 00085 00086 c2 = findPrevWordStart( c1.line(), c1.column() ); 00087 00088 if ( c2.line() != c1.line() ) { 00089 if ( c1.column() == 0 ) { 00090 c2.setColumn( doc()->line( c2.line() ).length() ); 00091 } else { 00092 c2.setColumn( 0 ); 00093 c2.setLine( c2.line()+1 ); 00094 } 00095 } 00096 00097 KateViRange r( c2.line(), c2.column(), c1.line(), c1.column(), ViMotion::ExclusiveMotion ); 00098 00099 return deleteRange( r, false, false ); 00100 } 00101 00102 bool KateViInsertMode::commandIndent() 00103 { 00104 //return getViNormalMode()->commandIndentLine(); 00105 return false; 00106 } 00107 00108 bool KateViInsertMode::commandUnindent() 00109 { 00110 //return getViNormalMode()->commandUnindentLine(); 00111 return false; 00112 } 00113 00114 bool KateViInsertMode::commandToFirstCharacterInFile() 00115 { 00116 Cursor c; 00117 00118 c.setLine( 0 ); 00119 c.setColumn( 0 ); 00120 00121 updateCursor( c ); 00122 00123 return true; 00124 } 00125 00126 bool KateViInsertMode::commandToLastCharacterInFile() 00127 { 00128 Cursor c; 00129 00130 int lines = doc()->lines()-1; 00131 c.setLine( lines ); 00132 c.setColumn( doc()->line( lines ).length() ); 00133 00134 updateCursor( c ); 00135 00136 return true; 00137 } 00138 00139 bool KateViInsertMode::commandMoveOneWordLeft() 00140 { 00141 Cursor c( m_view->cursorPosition() ); 00142 c = findPrevWordStart( c.line(), c.column() ); 00143 00144 updateCursor( c ); 00145 return true; 00146 } 00147 00148 bool KateViInsertMode::commandMoveOneWordRight() 00149 { 00150 Cursor c( m_view->cursorPosition() ); 00151 c = findNextWordStart( c.line(), c.column() ); 00152 00153 updateCursor( c ); 00154 return true; 00155 } 00156 00157 bool KateViInsertMode::commandCompleteNext() 00158 { 00159 if(m_view->completionWidget()->isCompletionActive()) { 00160 m_view->completionWidget()->cursorDown(); 00161 } else { 00162 m_view->userInvokedCompletion(); 00163 } 00164 return true; 00165 } 00166 00167 bool KateViInsertMode::commandCompletePrevious() 00168 { 00169 if(m_view->completionWidget()->isCompletionActive()) { 00170 m_view->completionWidget()->cursorUp(); 00171 } else { 00172 m_view->userInvokedCompletion(); 00173 m_view->completionWidget()->bottom(); 00174 } 00175 return true; 00176 } 00177 00182 bool KateViInsertMode::handleKeypress( const QKeyEvent *e ) 00183 { 00184 // backspace should work even if the shift key is down 00185 if (e->modifiers() != Qt::ControlModifier && e->key() == Qt::Key_Backspace ) { 00186 m_view->backspace(); 00187 return true; 00188 } 00189 00190 if ( e->modifiers() == Qt::NoModifier ) { 00191 switch ( e->key() ) { 00192 case Qt::Key_Escape: 00193 startNormalMode(); 00194 return true; 00195 break; 00196 case Qt::Key_Left: 00197 m_view->cursorLeft(); 00198 return true; 00199 case Qt::Key_Right: 00200 m_view->cursorRight(); 00201 return true; 00202 case Qt::Key_Up: 00203 m_view->up(); 00204 return true; 00205 case Qt::Key_Down: 00206 m_view->down(); 00207 return true; 00208 case Qt::Key_Delete: 00209 m_view->keyDelete(); 00210 return true; 00211 case Qt::Key_Home: 00212 m_view->home(); 00213 return true; 00214 case Qt::Key_End: 00215 m_view->end(); 00216 return true; 00217 case Qt::Key_PageUp: 00218 m_view->pageUp(); 00219 return true; 00220 case Qt::Key_PageDown: 00221 m_view->pageDown(); 00222 return true; 00223 default: 00224 return false; 00225 break; 00226 } 00227 } else if ( e->modifiers() == Qt::ControlModifier ) { 00228 switch( e->key() ) { 00229 case Qt::Key_BracketLeft: 00230 case Qt::Key_3: 00231 case Qt::Key_C: 00232 startNormalMode(); 00233 return true; 00234 break; 00235 case Qt::Key_D: 00236 commandUnindent(); 00237 return true; 00238 break; 00239 case Qt::Key_E: 00240 commandInsertFromBelow(); 00241 return true; 00242 break; 00243 case Qt::Key_N: 00244 commandCompleteNext(); 00245 return true; 00246 break; 00247 case Qt::Key_P: 00248 commandCompletePrevious(); 00249 return true; 00250 break; 00251 case Qt::Key_T: 00252 commandIndent(); 00253 return true; 00254 break; 00255 case Qt::Key_W: 00256 commandDeleteWord(); 00257 return true; 00258 break; 00259 case Qt::Key_Y: 00260 commandInsertFromAbove(); 00261 return true; 00262 break; 00263 case Qt::Key_Home: 00264 commandToFirstCharacterInFile(); 00265 return true; 00266 break; 00267 case Qt::Key_End: 00268 commandToLastCharacterInFile(); 00269 return true; 00270 break; 00271 case Qt::Key_Left: 00272 commandMoveOneWordLeft(); 00273 return true; 00274 break; 00275 case Qt::Key_Right: 00276 commandMoveOneWordRight(); 00277 return true; 00278 break; 00279 default: 00280 return false; 00281 } 00282 } 00283 00284 return false; 00285 }
KDE 4.6 API Reference