6#include "katedocument.h"
7#include "kateviinputmode.h"
10#include <utils/kateconfig.h>
11#include <view/kateviewinternal.h>
12#include <vimode/emulatedcommandbar/emulatedcommandbar.h>
13#include <vimode/inputmodemanager.h>
14#include <vimode/marks.h>
15#include <vimode/modes/replacevimode.h>
17using namespace KateVi;
19ReplaceViMode::ReplaceViMode(InputModeManager *viInputModeManager, KTextEditor::ViewPrivate *view, KateViewInternal *viewInternal)
23 m_viewInternal = viewInternal;
24 m_viInputModeManager = viInputModeManager;
28bool ReplaceViMode::commandInsertFromLine(
int offset)
30 KTextEditor::Cursor c(m_view->cursorPosition());
32 if (c.line() + offset >= doc()->lines() || c.line() + offset < 0) {
37 KTextEditor::Cursor target(c.line() + offset, c.column());
38 QChar ch = doc()->characterAt(target);
44 if (c.column() == doc()->lineLength(c.line())) {
45 return doc()->insertText(c, ch);
49 KTextEditor::Cursor
next(c.line(), c.column() + 1);
50 QChar removed = doc()->line(c.line()).at(c.column());
51 if (doc()->replaceText(KTextEditor::Range(c, next), ch)) {
58bool ReplaceViMode::commandMoveOneWordLeft()
60 KTextEditor::Cursor c(m_view->cursorPosition());
61 c = findPrevWordStart(c.line(), c.column());
64 c = KTextEditor::Cursor(0, 0);
71bool ReplaceViMode::commandMoveOneWordRight()
73 KTextEditor::Cursor c(m_view->cursorPosition());
74 c = findNextWordStart(c.line(), c.column());
77 c = doc()->documentEnd();
96 m_overwritten.clear();
100 m_overwritten.clear();
101 m_view->cursorLeft();
104 m_overwritten.clear();
105 m_view->cursorRight();
108 m_overwritten.clear();
112 m_overwritten.clear();
116 m_overwritten.clear();
120 m_overwritten.clear();
124 m_overwritten.clear();
128 m_overwritten.clear();
139 if (m_viInputModeManager->inputAdapter()->viModeEmulatedCommandBar()->isSendingSyntheticSearchCompletedKeypress()) {
141 m_viInputModeManager->doNotLogCurrentKeypress();
148 }
else if (e->
modifiers() == CONTROL_MODIFIER) {
155 commandInsertFromLine(1);
158 commandInsertFromLine(-1);
167 m_overwritten.clear();
168 commandMoveOneWordLeft();
171 m_overwritten.clear();
172 commandMoveOneWordRight();
182void ReplaceViMode::backspace()
187 if (c1.column() > 0) {
188 if (!m_overwritten.
isEmpty()) {
189 doc()->removeText(
KTextEditor::Range(c1.line(), c1.column() - 1, c1.line(), c1.column()));
190 doc()->insertText(c2, m_overwritten.
right(1));
197void ReplaceViMode::commandBackWord()
199 KTextEditor::Cursor current(m_view->cursorPosition());
200 KTextEditor::Cursor to(findPrevWordStart(current.line(), current.column()));
206 while (current.isValid() && current != to) {
208 current = m_view->cursorPosition();
212void ReplaceViMode::commandBackLine()
214 const int column = m_view->cursorPosition().column();
216 for (
int i = column; i >= 0 && !m_overwritten.isEmpty(); i--) {
221void ReplaceViMode::leaveReplaceMode()
224 m_view->abortCompletion();
228 const QString added = doc()->text(KTextEditor::Range(m_viInputModeManager->marks()->getStartEditYanked(), m_view->cursorPosition()));
229 for (
unsigned int i = 0; i < m_count - 1; i++) {
230 KTextEditor::Cursor c(m_view->cursorPosition());
231 KTextEditor::Cursor c2(c.line(), c.column() + added.
length());
232 doc()->replaceText(KTextEditor::Range(c, c2), added);
The Cursor represents a position in a Document.
An object representing a section of text, from one Cursor to another.
bool handleKeypress(const QKeyEvent *e) override
Checks if the key is a valid command in replace mode.
void overwrittenChar(const QChar &s)
Update the track of overwritten characters with the s character.
QAction * next(const QObject *recvr, const char *slot, QObject *parent)
Qt::KeyboardModifiers modifiers() const const
bool isEmpty() const const
qsizetype length() const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QString right(qsizetype n) const const