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

Kate

  • kde-4.14
  • 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 
52  if (ch == QChar::Null) {
53  return false;
54  }
55 
56  if (c.column() == doc()->lineLength(c.line())) {
57  return doc()->insertText(c, ch);
58  }
59 
60  QChar removed = doc()->line(c.line()).at(c.column());
61  if (doc()->replaceText(KTextEditor::Range(c, c2), ch)) {
62  overwrittenChar(removed);
63  return true;
64  }
65 
66  return false;
67 }
68 
69 bool KateViReplaceMode::commandMoveOneWordLeft()
70 {
71  KTextEditor::Cursor c( m_view->cursorPosition() );
72  c = findPrevWordStart( c.line(), c.column() );
73 
74  if (!c.isValid())
75  {
76  c = Cursor(0, 0);
77  }
78 
79  updateCursor( c );
80  return true;
81 }
82 
83 bool KateViReplaceMode::commandMoveOneWordRight()
84 {
85  KTextEditor::Cursor c( m_view->cursorPosition() );
86  c = findNextWordStart( c.line(), c.column() );
87 
88  if (!c.isValid())
89  {
90  c = doc()->documentEnd();
91  }
92 
93  updateCursor( c );
94  return true;
95 }
96 
101 bool KateViReplaceMode::handleKeypress( const QKeyEvent *e )
102 {
103  // backspace should work even if the shift key is down
104  if (e->modifiers() != Qt::ControlModifier && e->key() == Qt::Key_Backspace ) {
105  backspace();
106  return true;
107  }
108 
109  KTextEditor::Cursor c( m_view->cursorPosition() );
110 
111  if ( e->modifiers() == Qt::NoModifier ) {
112  switch ( e->key() ) {
113  case Qt::Key_Escape:
114  m_overwritten.clear();
115  startNormalMode();
116  return true;
117  case Qt::Key_Left:
118  m_overwritten.clear();
119  m_view->cursorLeft();
120  return true;
121  case Qt::Key_Right:
122  m_overwritten.clear();
123  m_view->cursorRight();
124  return true;
125  case Qt::Key_Up:
126  m_overwritten.clear();
127  m_view->up();
128  return true;
129  case Qt::Key_Down:
130  m_overwritten.clear();
131  m_view->down();
132  return true;
133  case Qt::Key_Home:
134  m_overwritten.clear();
135  m_view->home();
136  return true;
137  case Qt::Key_End:
138  m_overwritten.clear();
139  m_view->end();
140  return true;
141  case Qt::Key_PageUp:
142  m_overwritten.clear();
143  m_view->pageUp();
144  return true;
145  case Qt::Key_PageDown:
146  m_overwritten.clear();
147  m_view->pageDown();
148  return true;
149  case Qt::Key_Delete:
150  m_view->keyDelete();
151  return true;
152  case Qt::Key_Insert:
153  startInsertMode();
154  return true;
155  default:
156  return false;
157  }
158  } else if ( e->modifiers() == Qt::ControlModifier ) {
159  switch( e->key() ) {
160  case Qt::Key_BracketLeft:
161  case Qt::Key_C:
162  startNormalMode();
163  return true;
164  case Qt::Key_E:
165  commandInsertFromLine( 1 );
166  return true;
167  case Qt::Key_Y:
168  commandInsertFromLine( -1 );
169  return true;
170  case Qt::Key_W:
171  commandBackWord();
172  return true;
173  case Qt::Key_U:
174  commandBackLine();
175  return true;
176  case Qt::Key_Left:
177  m_overwritten.clear();
178  commandMoveOneWordLeft();
179  return true;
180  case Qt::Key_Right:
181  m_overwritten.clear();
182  commandMoveOneWordRight();
183  return true;
184  default:
185  return false;
186  }
187  }
188 
189  return false;
190 }
191 
192 void KateViReplaceMode::backspace()
193 {
194  KTextEditor::Cursor c1( m_view->cursorPosition() );
195  KTextEditor::Cursor c2( c1.line(), c1.column()-1 );
196 
197  if ( c1.column() > 0 ) {
198  if ( !m_overwritten.isEmpty() ) {
199  doc()->removeText(KTextEditor::Range(c1.line(), c1.column()-1, c1.line(), c1.column()));
200  doc()->insertText(c2 , m_overwritten.right( 1 ) );
201  m_overwritten.remove( m_overwritten.length()-1, 1);
202  }
203  updateCursor( c2 );
204  }
205 }
206 
207 void KateViReplaceMode::commandBackWord()
208 {
209  KTextEditor::Cursor current(m_view->cursorPosition());
210  KTextEditor::Cursor to(findPrevWordStart(current.line(), current.column()));
211 
212  if (!to.isValid()) {
213  return;
214  }
215 
216  while (current.isValid() && current != to) {
217  backspace();
218  current = m_view->cursorPosition();
219  }
220 }
221 
222 void KateViReplaceMode::commandBackLine()
223 {
224  const int column = m_view->cursorPosition().column();
225 
226  for (int i = column; i >= 0 && !m_overwritten.isEmpty(); i--) {
227  backspace();
228  }
229 }
KateDocument::line
virtual QString line(int line) const
Definition: katedocument.cpp:447
KateDocument::config
KateDocumentConfig * config()
Configuration.
Definition: katedocument.h:1009
QKeyEvent::modifiers
Qt::KeyboardModifiers modifiers() const
kateview.h
KateView::end
void end()
Definition: kateview.cpp:2683
KateViModeBase::getCharAtVirtualColumn
const QChar getCharAtVirtualColumn(QString &line, int virtualColumn, int tabWidht) const
Definition: katevimodebase.cpp:1290
QChar
KateViModeBase::doc
KateDocument * doc() const
Definition: katevimodebase.h:172
KateDocument::lineLength
virtual int lineLength(int line) const
Definition: katedocument.cpp:758
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:69
KateViReplaceMode::commandMoveOneWordRight
bool commandMoveOneWordRight()
Definition: katevireplacemode.cpp:83
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:101
QString::remove
QString & remove(int position, int n)
KateView::home
void home()
Definition: kateview.cpp:2673
QString::clear
void clear()
kateviinputmodemanager.h
KateDocument::replaceText
virtual bool replaceText(const KTextEditor::Range &range, const QString &s, bool block=false)
Definition: katedocument.cpp:4686
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
KateView::up
void up()
Definition: kateview.cpp:2693
KateView::keyDelete
void keyDelete()
Definition: kateview.cpp:2594
KateViModeBase::startNormalMode
bool startNormalMode()
Definition: katevimodebase.cpp:1171
KateViReplaceMode::~KateViReplaceMode
~KateViReplaceMode()
Definition: katevireplacemode.cpp:35
KateViewInternal
Definition: kateviewinternal.h:58
QString::isEmpty
bool isEmpty() const
KateViModeBase::updateCursor
void updateCursor(const Cursor &c) const
Definition: katevimodebase.cpp:932
KateDocument::lines
virtual int lines() const
Definition: katedocument.cpp:753
KateDocument::documentEnd
virtual KTextEditor::Cursor documentEnd() const
Definition: katedocument.cpp:4681
kateviewinternal.h
QString
KateViModeBase::startInsertMode
bool startInsertMode()
Definition: katevimodebase.cpp:1190
KateView
Definition: kateview.h:77
QString::right
QString right(int n) const
KateViModeBase::findNextWordStart
Cursor findNextWordStart(int fromLine, int fromColumn, bool onlyCurrentLine=false) const
Definition: katevimodebase.cpp:312
QKeyEvent::key
int key() const
KateView::cursorRight
void cursorRight()
Definition: kateview.cpp:2625
KateView::cursorLeft
void cursorLeft()
Definition: kateview.cpp:2609
KateViReplaceMode::commandBackLine
void commandBackLine()
Definition: katevireplacemode.cpp:222
KateView::down
void down()
Definition: kateview.cpp:2703
KateView::pageUp
void pageUp()
Definition: kateview.cpp:2743
KateView::cursorPosition
KTextEditor::Cursor cursorPosition() const
Definition: kateview.cpp:2423
QKeyEvent
KateViReplaceMode::KateViReplaceMode
KateViReplaceMode(KateViInputModeManager *viInputModeManager, KateView *view, KateViewInternal *viewInternal)
Definition: katevireplacemode.cpp:27
KateViModeBase::m_viInputModeManager
KateViInputModeManager * m_viInputModeManager
Definition: katevimodebase.h:177
QString::length
int length() const
katevireplacemode.h
KateViReplaceMode::backspace
void backspace()
Definition: katevireplacemode.cpp:192
KateView::virtualCursorColumn
int virtualCursorColumn() const
Return the virtual cursor column, each tab is expanded into the document's tabWidth characters...
Definition: kateview.cpp:1944
KateDocumentConfig::tabWidth
int tabWidth() const
Definition: kateconfig.cpp:444
KateViModeBase::findPrevWordStart
Cursor findPrevWordStart(int fromLine, int fromColumn, bool onlyCurrentLine=false) const
Definition: katevimodebase.cpp:493
kateconfig.h
KateViReplaceMode::commandBackWord
void commandBackWord()
Definition: katevireplacemode.cpp:207
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
KateView::pageDown
void pageDown()
Definition: kateview.cpp:2753
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:59 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
  • 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