• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • sources
  • kde-4.12
  • applications
  • kate
  • part
  • vimode
katevireplacemode.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries and the Kate part.
2  *
3  * Copyright (C) 2008 Erlend Hamberg <ehamberg@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #include "katevireplacemode.h"
22 #include "kateviinputmodemanager.h"
23 #include "kateview.h"
24 #include "kateviewinternal.h"
25 #include "kateconfig.h"
26 
27 KateViReplaceMode::KateViReplaceMode( KateViInputModeManager *viInputModeManager,
28  KateView * view, KateViewInternal * viewInternal ) : KateViModeBase()
29 {
30  m_view = view;
31  m_viewInternal = viewInternal;
32  m_viInputModeManager = viInputModeManager;
33 }
34 
35 KateViReplaceMode::~KateViReplaceMode()
36 {
37 }
38 
39 bool KateViReplaceMode::commandInsertFromLine( int offset )
40 {
41  KTextEditor::Cursor c( m_view->cursorPosition() );
42  KTextEditor::Cursor c2( c.line(), c.column()+1 );
43 
44  if ( c.line()+offset > doc()->lines() || c.line()+offset < 0 ) {
45  return false;
46  }
47 
48  QString line = doc()->line( c.line()+offset );
49  int tabWidth = doc()->config()->tabWidth();
50  QChar ch = getCharAtVirtualColumn( line, m_view->virtualCursorColumn(), tabWidth );
51  QChar removed = doc()->line( c.line() ).at( c.column() );
52 
53  if ( ch == QChar::Null ) {
54  return false;
55  }
56 
57  if ( doc()->replaceText( KTextEditor::Range( c, c2 ), ch ) ) {
58  overwrittenChar( removed );
59  return true;
60  }
61 
62  return false;
63 }
64 
65 bool KateViReplaceMode::commandMoveOneWordLeft()
66 {
67  KTextEditor::Cursor c( m_view->cursorPosition() );
68  c = findPrevWordStart( c.line(), c.column() );
69 
70  if (!c.isValid())
71  {
72  c = Cursor(0, 0);
73  }
74 
75  updateCursor( c );
76  return true;
77 }
78 
79 bool KateViReplaceMode::commandMoveOneWordRight()
80 {
81  KTextEditor::Cursor c( m_view->cursorPosition() );
82  c = findNextWordStart( c.line(), c.column() );
83 
84  if (!c.isValid())
85  {
86  c = doc()->documentEnd();
87  }
88 
89  updateCursor( c );
90  return true;
91 }
92 
97 bool KateViReplaceMode::handleKeypress( const QKeyEvent *e )
98 {
99  // backspace should work even if the shift key is down
100  if (e->modifiers() != Qt::ControlModifier && e->key() == Qt::Key_Backspace ) {
101  backspace();
102  return true;
103  }
104 
105  KTextEditor::Cursor c( m_view->cursorPosition() );
106 
107  if ( e->modifiers() == Qt::NoModifier ) {
108  switch ( e->key() ) {
109  case Qt::Key_Escape:
110  m_overwritten.clear();
111  startNormalMode();
112  return true;
113  break;
114  case Qt::Key_Left:
115  m_overwritten.clear();
116  m_view->cursorLeft();
117  return true;
118  case Qt::Key_Right:
119  m_overwritten.clear();
120  m_view->cursorRight();
121  return true;
122  case Qt::Key_Home:
123  m_overwritten.clear();
124  m_view->home();
125  return true;
126  case Qt::Key_End:
127  m_overwritten.clear();
128  m_view->end();
129  return true;
130  default:
131  return false;
132  break;
133  }
134  } else if ( e->modifiers() == Qt::ControlModifier ) {
135  switch( e->key() ) {
136  case Qt::Key_BracketLeft:
137  case Qt::Key_C:
138  startNormalMode();
139  return true;
140  break;
141  case Qt::Key_E:
142  commandInsertFromLine( 1 );
143  return true;
144  break;
145  case Qt::Key_Y:
146  commandInsertFromLine( -1 );
147  return true;
148  break;
149  case Qt::Key_Left:
150  m_overwritten.clear();
151  commandMoveOneWordLeft();
152  return true;
153  break;
154  case Qt::Key_Right:
155  m_overwritten.clear();
156  commandMoveOneWordRight();
157  return true;
158  break;
159  default:
160  return false;
161  }
162  }
163 
164  return false;
165 }
166 
167 void KateViReplaceMode::backspace()
168 {
169  KTextEditor::Cursor c1( m_view->cursorPosition() );
170  KTextEditor::Cursor c2( c1.line(), c1.column()-1 );
171 
172  if ( c1.column() > 0 ) {
173  if ( !m_overwritten.isEmpty() ) {
174  doc()->removeText(KTextEditor::Range(c1.line(), c1.column()-1, c1.line(), c1.column()));
175  doc()->insertText(c2 , m_overwritten.right( 1 ) );
176  m_overwritten.remove( m_overwritten.length()-1, 1);
177  }
178  updateCursor( c2 );
179  }
180 }
KateDocument::line
virtual QString line(int line) const
Definition: katedocument.cpp:447
KateDocument::config
KateDocumentConfig * config()
Configuration.
Definition: katedocument.h:1009
kateview.h
KateView::end
void end()
Definition: kateview.cpp:2650
KateViModeBase::getCharAtVirtualColumn
const QChar getCharAtVirtualColumn(QString &line, int virtualColumn, int tabWidht) const
Definition: katevimodebase.cpp:1292
KateViModeBase::doc
KateDocument * doc() const
Definition: katevimodebase.h:172
KateViModeBase
Definition: katevimodebase.h:64
KateViInputModeManager
Definition: kateviinputmodemanager.h:68
KateViReplaceMode::commandInsertFromLine
bool commandInsertFromLine(int offset)
Definition: katevireplacemode.cpp:39
KateViReplaceMode::commandMoveOneWordLeft
bool commandMoveOneWordLeft()
Definition: katevireplacemode.cpp:65
KateViReplaceMode::commandMoveOneWordRight
bool commandMoveOneWordRight()
Definition: katevireplacemode.cpp:79
KateViReplaceMode::overwrittenChar
void overwrittenChar(const QChar &s)
Definition: katevireplacemode.h:48
KateViReplaceMode::handleKeypress
bool handleKeypress(const QKeyEvent *e)
checks if the key is a valid command
Definition: katevireplacemode.cpp:97
QString
KTextEditor::Cursor
KateView::home
void home()
Definition: kateview.cpp:2640
kateviinputmodemanager.h
KateDocument::replaceText
virtual bool replaceText(const KTextEditor::Range &range, const QString &s, bool block=false)
Definition: katedocument.cpp:4687
KateDocument::insertText
virtual bool insertText(const KTextEditor::Cursor &position, const QString &s, bool block=false)
Definition: katedocument.cpp:530
KateViModeBase::m_view
KateView * m_view
Definition: katevimodebase.h:172
KateViModeBase::startNormalMode
bool startNormalMode()
Definition: katevimodebase.cpp:1176
KateViReplaceMode::~KateViReplaceMode
~KateViReplaceMode()
Definition: katevireplacemode.cpp:35
KateViewInternal
Definition: kateviewinternal.h:57
KateViModeBase::updateCursor
void updateCursor(const Cursor &c) const
Definition: katevimodebase.cpp:937
KateDocument::lines
virtual int lines() const
Definition: katedocument.cpp:753
KateDocument::documentEnd
virtual KTextEditor::Cursor documentEnd() const
Definition: katedocument.cpp:4682
kateviewinternal.h
KateView
Definition: kateview.h:78
KTextEditor::Range
KateViModeBase::findNextWordStart
Cursor findNextWordStart(int fromLine, int fromColumn, bool onlyCurrentLine=false) const
Definition: katevimodebase.cpp:317
KateView::cursorRight
void cursorRight()
Definition: kateview.cpp:2592
KateView::cursorLeft
void cursorLeft()
Definition: kateview.cpp:2576
KateView::cursorPosition
KTextEditor::Cursor cursorPosition() const
Definition: kateview.cpp:2398
KateViReplaceMode::KateViReplaceMode
KateViReplaceMode(KateViInputModeManager *viInputModeManager, KateView *view, KateViewInternal *viewInternal)
Definition: katevireplacemode.cpp:27
KateViModeBase::m_viInputModeManager
KateViInputModeManager * m_viInputModeManager
Definition: katevimodebase.h:177
katevireplacemode.h
KateViReplaceMode::backspace
void backspace()
Definition: katevireplacemode.cpp:167
KateView::virtualCursorColumn
int virtualCursorColumn() const
Return the virtual cursor column, each tab is expanded into the document's tabWidth characters...
Definition: kateview.cpp:1919
KateDocumentConfig::tabWidth
int tabWidth() const
Definition: kateconfig.cpp:403
KateViModeBase::findPrevWordStart
Cursor findPrevWordStart(int fromLine, int fromColumn, bool onlyCurrentLine=false) const
Definition: katevimodebase.cpp:498
kateconfig.h
KateViModeBase::m_viewInternal
KateViewInternal * m_viewInternal
Definition: katevimodebase.h:176
KateDocument::removeText
virtual bool removeText(const KTextEditor::Range &range, bool block=false)
Definition: katedocument.cpp:633
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:31:53 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

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

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Applications
  •   Libraries
  •     libkonq
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal