• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Kate

kateviinsertmode.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002  * Copyright (C) 2008 Erlend Hamberg <ehamberg@gmail.com>
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) version 3.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this library; see the file COPYING.LIB.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
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 ) : KateViModeBase()
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   //return getViNormalMode()->commandIndentLine();
00129   return false;
00130 }
00131 
00132 bool KateViInsertMode::commandUnindent()
00133 {
00134   //return getViNormalMode()->commandUnindentLine();
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 
00181 bool KateViInsertMode::commandCompleteNext()
00182 {
00183   m_view->userInvokedCompletion();
00184   return true;
00185 }
00186 
00187 bool KateViInsertMode::commandCompletePrevious()
00188 {
00189   m_view->userInvokedCompletion();
00190   return true;
00191 }
00192 
00197 bool KateViInsertMode::handleKeypress( const QKeyEvent *e )
00198 {
00199   // backspace should work even if the shift key is down
00200   if (e->modifiers() != Qt::ControlModifier && e->key() == Qt::Key_Backspace ) {
00201     m_view->backspace();
00202     return true;
00203   }
00204 
00205   if ( e->modifiers() == Qt::NoModifier ) {
00206     switch ( e->key() ) {
00207     case Qt::Key_Escape:
00208       startNormalMode();
00209       return true;
00210       break;
00211     case Qt::Key_Left:
00212       m_view->cursorLeft();
00213       return true;
00214     case Qt::Key_Right:
00215       m_view->cursorRight();
00216       return true;
00217     case Qt::Key_Up:
00218       m_view->up();
00219       return true;
00220     case Qt::Key_Down:
00221       m_view->down();
00222       return true;
00223     case Qt::Key_Delete:
00224       m_view->keyDelete();
00225       return true;
00226     case Qt::Key_Home:
00227       m_view->home();
00228       return true;
00229     case Qt::Key_End:
00230       m_view->end();
00231       return true;
00232     case Qt::Key_PageUp:
00233       m_view->pageUp();
00234       return true;
00235     case Qt::Key_PageDown:
00236       m_view->pageDown();
00237       return true;
00238     default:
00239       return false;
00240       break;
00241     }
00242   } else if ( e->modifiers() == Qt::ControlModifier ) {
00243     switch( e->key() ) {
00244     case Qt::Key_BracketLeft:
00245     case Qt::Key_C:
00246       startNormalMode();
00247       return true;
00248       break;
00249     case Qt::Key_D:
00250       commandUnindent();
00251       return true;
00252       break;
00253     case Qt::Key_E:
00254       commandInsertFromBelow();
00255       return true;
00256       break;
00257     case Qt::Key_N:
00258       commandCompleteNext();
00259       return true;
00260       break;
00261     case Qt::Key_P:
00262       commandCompletePrevious();
00263       return true;
00264       break;
00265     case Qt::Key_T:
00266       commandIndent();
00267       return true;
00268       break;
00269     case Qt::Key_W:
00270       commandDeleteWord();
00271       return true;
00272       break;
00273     case Qt::Key_Y:
00274       commandInsertFromAbove();
00275       return true;
00276       break;
00277     case Qt::Key_Home:
00278       commandToFirstCharacterInFile();
00279       return true;
00280       break;
00281     case Qt::Key_End:
00282       commandToLastCharacterInFile();
00283       return true;
00284       break;
00285     case Qt::Key_Left:
00286       commandMoveOneWordLeft();
00287       return true;
00288       break;
00289     case Qt::Key_Right:
00290       commandMoveOneWordRight();
00291       return true;
00292       break;
00293     default:
00294       return false;
00295     }
00296   }
00297 
00298   return false;
00299 }

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal