Kate
katevireplacemode.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 "katevireplacemode.h" 00022 #include "kateviinputmodemanager.h" 00023 #include "kateview.h" 00024 #include "kateviewinternal.h" 00025 #include "kateconfig.h" 00026 00027 KateViReplaceMode::KateViReplaceMode( KateViInputModeManager *viInputModeManager, 00028 KateView * view, KateViewInternal * viewInternal ) : KateViModeBase() 00029 { 00030 m_view = view; 00031 m_viewInternal = viewInternal; 00032 m_viInputModeManager = viInputModeManager; 00033 } 00034 00035 KateViReplaceMode::~KateViReplaceMode() 00036 { 00037 } 00038 00039 bool KateViReplaceMode::commandInsertFromLine( int offset ) 00040 { 00041 KTextEditor::Cursor c( m_view->cursorPosition() ); 00042 KTextEditor::Cursor c2( c.line(), c.column()+1 ); 00043 00044 if ( c.line()+offset > doc()->lines() || c.line()+offset < 0 ) { 00045 return false; 00046 } 00047 00048 QString line = doc()->line( c.line()+offset ); 00049 int tabWidth = doc()->config()->tabWidth(); 00050 QChar ch = getCharAtVirtualColumn( line, m_view->virtualCursorColumn(), tabWidth ); 00051 QChar removed = doc()->line( c.line() ).at( c.column() ); 00052 00053 if ( ch == QChar::Null ) { 00054 return false; 00055 } 00056 00057 if ( doc()->replaceText( KTextEditor::Range( c, c2 ), ch ) ) { 00058 overwrittenChar( removed ); 00059 return true; 00060 } 00061 00062 return false; 00063 } 00064 00065 bool KateViReplaceMode::commandMoveOneWordLeft() 00066 { 00067 KTextEditor::Cursor c( m_view->cursorPosition() ); 00068 c = findPrevWordStart( c.line(), c.column() ); 00069 00070 updateCursor( c ); 00071 return true; 00072 } 00073 00074 bool KateViReplaceMode::commandMoveOneWordRight() 00075 { 00076 KTextEditor::Cursor c( m_view->cursorPosition() ); 00077 c = findNextWordStart( c.line(), c.column() ); 00078 00079 updateCursor( c ); 00080 return true; 00081 } 00082 00087 bool KateViReplaceMode::handleKeypress( const QKeyEvent *e ) 00088 { 00089 // backspace should work even if the shift key is down 00090 if (e->modifiers() != Qt::ControlModifier && e->key() == Qt::Key_Backspace ) { 00091 backspace(); 00092 return true; 00093 } 00094 00095 KTextEditor::Cursor c( m_view->cursorPosition() ); 00096 00097 if ( e->modifiers() == Qt::NoModifier ) { 00098 switch ( e->key() ) { 00099 case Qt::Key_Escape: 00100 m_overwritten.clear(); 00101 startNormalMode(); 00102 return true; 00103 break; 00104 case Qt::Key_Left: 00105 m_overwritten.clear(); 00106 m_view->cursorLeft(); 00107 return true; 00108 case Qt::Key_Right: 00109 m_overwritten.clear(); 00110 m_view->cursorRight(); 00111 return true; 00112 case Qt::Key_Home: 00113 m_overwritten.clear(); 00114 m_view->home(); 00115 return true; 00116 case Qt::Key_End: 00117 m_overwritten.clear(); 00118 m_view->end(); 00119 return true; 00120 default: 00121 return false; 00122 break; 00123 } 00124 } else if ( e->modifiers() == Qt::ControlModifier ) { 00125 switch( e->key() ) { 00126 case Qt::Key_BracketLeft: 00127 case Qt::Key_C: 00128 startNormalMode(); 00129 return true; 00130 break; 00131 case Qt::Key_E: 00132 commandInsertFromLine( 1 ); 00133 return true; 00134 break; 00135 case Qt::Key_Y: 00136 commandInsertFromLine( -1 ); 00137 return true; 00138 break; 00139 case Qt::Key_Left: 00140 m_overwritten.clear(); 00141 commandMoveOneWordLeft(); 00142 return true; 00143 break; 00144 case Qt::Key_Right: 00145 m_overwritten.clear(); 00146 commandMoveOneWordRight(); 00147 return true; 00148 break; 00149 default: 00150 return false; 00151 } 00152 } 00153 00154 return false; 00155 } 00156 00157 void KateViReplaceMode::backspace() 00158 { 00159 KTextEditor::Cursor c1( m_view->cursorPosition() ); 00160 KTextEditor::Cursor c2( c1.line(), c1.column()-1 ); 00161 00162 if ( c1.column() > 0 ) { 00163 if ( !m_overwritten.isEmpty() ) { 00164 doc()->removeText(KTextEditor::Range(c1.line(), c1.column()-1, c1.line(), c1.column())); 00165 doc()->insertText(c2 , m_overwritten.right( 1 ) ); 00166 m_overwritten.remove( m_overwritten.length()-1, 1); 00167 } 00168 updateCursor( c2 ); 00169 } 00170 }
KDE 4.6 API Reference