7#include "kateviinputmode.h"
9#include "katedocument.h"
10#include "katerenderer.h"
12#include "kateviewinternal.h"
13#include <vimode/emulatedcommandbar/emulatedcommandbar.h>
14#include <vimode/macrorecorder.h>
15#include <vimode/marks.h>
16#include <vimode/modes/replacevimode.h>
17#include <vimode/modes/visualvimode.h>
18#include <vimode/searcher.h>
20#include <KLocalizedString>
22#include <QCoreApplication>
26QString viModeToString(KateVi::ViMode mode)
30 case KateVi::InsertMode:
31 modeStr =
i18n(
"VI: INSERT MODE");
33 case KateVi::NormalMode:
34 modeStr =
i18n(
"VI: NORMAL MODE");
36 case KateVi::VisualMode:
37 modeStr =
i18n(
"VI: VISUAL");
39 case KateVi::VisualBlockMode:
40 modeStr =
i18n(
"VI: VISUAL BLOCK");
42 case KateVi::VisualLineMode:
43 modeStr =
i18n(
"VI: VISUAL LINE");
45 case KateVi::ReplaceMode:
46 modeStr =
i18n(
"VI: REPLACE");
54KateViInputMode::KateViInputMode(KateViewInternal *viewInternal, KateVi::GlobalState *global)
55 : KateAbstractInputMode(viewInternal)
56 , m_viModeEmulatedCommandBar(nullptr)
59 , m_nextKeypressIsOverriddenShortCut(false)
60 , m_relLineNumbers(KateViewConfig::global()->viRelativeLineNumbers())
62 , m_viModeManager(new KateVi::InputModeManager(this, view(), viewInternal))
66void KateViInputMode::activate()
69 setCaretStyle(KTextEditor::caretStyles::Block);
72 if (view()->selection()) {
73 m_viModeManager->changeViMode(KateVi::VisualMode);
74 view()->setCursorPosition(
KTextEditor::Cursor(view()->selectionRange().
end().line(), view()->selectionRange().
end().column() - 1));
75 m_viModeManager->m_viVisualMode->updateSelection();
77 viewInternal()->iconBorder()->setRelLineNumbersOn(m_relLineNumbers);
80void KateViInputMode::deactivate()
82 if (m_viModeEmulatedCommandBar) {
83 m_viModeEmulatedCommandBar->hideMe();
87 view()->doc()->setUndoMergeAllEdits(
false);
89 viewInternal()->iconBorder()->setRelLineNumbersOn(
false);
90 m_viModeManager->searcher()->enableHighlightSearch(
false);
93void KateViInputMode::reset()
95 if (m_viModeEmulatedCommandBar) {
96 m_viModeEmulatedCommandBar->hideMe();
100 m_viModeManager.reset();
101 m_viModeManager.reset(
new KateVi::InputModeManager(
this, view(), viewInternal()));
103 if (m_viModeEmulatedCommandBar) {
104 m_viModeEmulatedCommandBar->setViInputModeManager(m_viModeManager.get());
108bool KateViInputMode::overwrite()
const
110 return m_viModeManager->getCurrentViMode() == KateVi::ViMode::ReplaceMode;
113void KateViInputMode::overwrittenChar(
const QChar &c)
115 m_viModeManager->getViReplaceMode()->overwrittenChar(c);
118void KateViInputMode::clearSelection()
123bool KateViInputMode::stealKey(
QKeyEvent *k)
125 if (!KateViewConfig::global()->viInputModeStealKeys()) {
131 const bool stolen = keyPress(k);
134 m_nextKeypressIsOverriddenShortCut =
true;
144QString KateViInputMode::viewInputModeHuman()
const
146 return i18n(
"vi-mode");
151 return m_viModeManager->getCurrentViewMode();
154QString KateViInputMode::viewModeHuman()
const
156 QString currentMode = viModeToString(m_viModeManager->getCurrentViMode());
158 if (m_viModeManager->macroRecorder()->isRecording()) {
162 QString cmd = m_viModeManager->getVerbatimKeys();
164 currentMode.
prepend(QStringLiteral(
"%1 ").arg(cmd));
170void KateViInputMode::gotFocus()
175void KateViInputMode::lostFocus()
180void KateViInputMode::readSessionConfig(
const KConfigGroup &config)
183 m_viModeManager->readSessionConfig(config);
186void KateViInputMode::writeSessionConfig(
KConfigGroup &config)
189 m_viModeManager->writeSessionConfig(config);
192void KateViInputMode::updateConfig()
194 KateViewConfig *cfg = view()->config();
197 m_relLineNumbers = cfg->viRelativeLineNumbers();
200 viewInternal()->iconBorder()->setRelLineNumbersOn(m_relLineNumbers);
204void KateViInputMode::readWriteChanged(
bool)
209void KateViInputMode::find()
211 showViModeEmulatedCommandBar();
212 viModeEmulatedCommandBar()->init(KateVi::EmulatedCommandBar::SearchForward);
215void KateViInputMode::findSelectedForwards()
217 m_viModeManager->searcher()->findNext();
220void KateViInputMode::findSelectedBackwards()
222 m_viModeManager->searcher()->findPrevious();
225void KateViInputMode::findReplace()
227 showViModeEmulatedCommandBar();
228 viModeEmulatedCommandBar()->init(KateVi::EmulatedCommandBar::SearchForward);
231void KateViInputMode::findNext()
233 m_viModeManager->searcher()->findNext();
236void KateViInputMode::findPrevious()
238 m_viModeManager->searcher()->findPrevious();
241void KateViInputMode::activateCommandLine()
243 showViModeEmulatedCommandBar();
244 viModeEmulatedCommandBar()->init(KateVi::EmulatedCommandBar::Command);
247void KateViInputMode::showViModeEmulatedCommandBar()
249 view()->bottomViewBar()->addBarWidget(viModeEmulatedCommandBar());
250 view()->bottomViewBar()->showBarWidget(viModeEmulatedCommandBar());
255 if (!m_viModeEmulatedCommandBar) {
257 m_viModeEmulatedCommandBar->
hide();
260 return m_viModeEmulatedCommandBar;
263void KateViInputMode::updateRendererConfig()
265 m_viModeManager->searcher()->updateHighlightColors();
268bool KateViInputMode::keyPress(
QKeyEvent *e)
270 if (m_nextKeypressIsOverriddenShortCut) {
273 m_nextKeypressIsOverriddenShortCut =
false;
277 if (m_viModeManager->handleKeypress(e)) {
285bool KateViInputMode::blinkCaret()
const
290KTextEditor::caretStyles KateViInputMode::caretStyle()
const
295void KateViInputMode::toggleInsert()
300void KateViInputMode::launchInteractiveCommand(
const QString &)
305QString KateViInputMode::bookmarkLabel(
int line)
const
307 return m_viModeManager->marks()->getMarksOnTheLine(line);
310void KateViInputMode::setCaretStyle(
const KTextEditor::caretStyles caret)
312 if (m_caret != caret) {
317 viewInternal()->paintCursor();
The Cursor represents a position in a Document.
ViewMode
Possible view modes These correspond to various modes the text editor might be in.
InputMode
Possible input modes.
void viewModeChanged(KTextEditor::View *view, KTextEditor::View::ViewMode mode)
This signal is emitted whenever the view mode of view changes.
void setDrawCaret(bool drawCaret)
Set whether the caret (text cursor) will be drawn.
void setCaretStyle(KTextEditor::caretStyles style)
Set the style of caret to be painted.
A KateViewBarWidget that attempts to emulate some of the features of Vim's own command bar,...
QString i18n(const char *text, const TYPE &arg...)
const QList< QKeySequence > & end()
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
bool isEmpty() const const
QString & prepend(QChar ch)