KTextEditor

kateviinputmode.cpp
1/*
2 SPDX-FileCopyrightText: KDE Developers
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "kateviinputmode.h"
8#include "kateconfig.h"
9#include "katedocument.h"
10#include "katerenderer.h"
11#include "kateview.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>
19
20#include <KLocalizedString>
21
22#include <QCoreApplication>
23
24namespace
25{
26QString viModeToString(KateVi::ViMode mode)
27{
28 QString modeStr;
29 switch (mode) {
30 case KateVi::InsertMode:
31 modeStr = i18n("VI: INSERT MODE");
32 break;
33 case KateVi::NormalMode:
34 modeStr = i18n("VI: NORMAL MODE");
35 break;
36 case KateVi::VisualMode:
37 modeStr = i18n("VI: VISUAL");
38 break;
39 case KateVi::VisualBlockMode:
40 modeStr = i18n("VI: VISUAL BLOCK");
41 break;
42 case KateVi::VisualLineMode:
43 modeStr = i18n("VI: VISUAL LINE");
44 break;
45 case KateVi::ReplaceMode:
46 modeStr = i18n("VI: REPLACE");
47 break;
48 }
49
50 return modeStr;
51}
52}
53
54KateViInputMode::KateViInputMode(KateViewInternal *viewInternal, KateVi::GlobalState *global)
55 : KateAbstractInputMode(viewInternal)
56 , m_viModeEmulatedCommandBar(nullptr)
57 , m_viGlobal(global)
58 , m_caret(KTextEditor::caretStyles::Block)
59 , m_nextKeypressIsOverriddenShortCut(false)
60 , m_relLineNumbers(KateViewConfig::global()->viRelativeLineNumbers())
61 , m_activated(false)
62 , m_viModeManager(new KateVi::InputModeManager(this, view(), viewInternal))
63{
64}
65
66void KateViInputMode::activate()
67{
68 m_activated = true;
69 setCaretStyle(KTextEditor::caretStyles::Block); // TODO: can we end up in insert mode?
70 reset(); // TODO: is this necessary? (well, not anymore I guess)
71
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();
76 }
77 viewInternal()->iconBorder()->setRelLineNumbersOn(m_relLineNumbers);
78}
79
80void KateViInputMode::deactivate()
81{
82 if (m_viModeEmulatedCommandBar) {
83 m_viModeEmulatedCommandBar->hideMe();
84 }
85
86 // make sure to turn off edits merging when leaving vi input mode
87 view()->doc()->setUndoMergeAllEdits(false);
88 m_activated = false;
89 viewInternal()->iconBorder()->setRelLineNumbersOn(false);
90 m_viModeManager->searcher()->enableHighlightSearch(false);
91}
92
93void KateViInputMode::reset()
94{
95 if (m_viModeEmulatedCommandBar) {
96 m_viModeEmulatedCommandBar->hideMe();
97 }
98
99 // ensure first the old stuff is deleted and then the new manager is constructed
100 m_viModeManager.reset();
101 m_viModeManager.reset(new KateVi::InputModeManager(this, view(), viewInternal()));
102
103 if (m_viModeEmulatedCommandBar) {
104 m_viModeEmulatedCommandBar->setViInputModeManager(m_viModeManager.get());
105 }
106}
107
108bool KateViInputMode::overwrite() const
109{
110 return m_viModeManager->getCurrentViMode() == KateVi::ViMode::ReplaceMode;
111}
112
113void KateViInputMode::overwrittenChar(const QChar &c)
114{
115 m_viModeManager->getViReplaceMode()->overwrittenChar(c);
116}
117
118void KateViInputMode::clearSelection()
119{
120 // do nothing, handled elsewhere
121}
122
123bool KateViInputMode::stealKey(QKeyEvent *k)
124{
125 if (!KateViewConfig::global()->viInputModeStealKeys()) {
126 return false;
127 }
128
129 // Actually see if we can make use of this key - if so, we've stolen it; if not,
130 // let Qt's shortcut handling system deal with it.
131 const bool stolen = keyPress(k);
132 if (stolen) {
133 // Qt will replay this QKeyEvent, next time as an ordinary KeyPress.
134 m_nextKeypressIsOverriddenShortCut = true;
135 }
136 return stolen;
137}
138
139KTextEditor::View::InputMode KateViInputMode::viewInputMode() const
140{
142}
143
144QString KateViInputMode::viewInputModeHuman() const
145{
146 return i18n("vi-mode");
147}
148
149KTextEditor::View::ViewMode KateViInputMode::viewMode() const
150{
151 return m_viModeManager->getCurrentViewMode();
152}
153
154QString KateViInputMode::viewModeHuman() const
155{
156 QString currentMode = viModeToString(m_viModeManager->getCurrentViMode());
157
158 if (m_viModeManager->macroRecorder()->isRecording()) {
159 currentMode.prepend(QLatin1Char('(') + i18n("recording") + QLatin1String(") "));
160 }
161
162 QString cmd = m_viModeManager->getVerbatimKeys();
163 if (!cmd.isEmpty()) {
164 currentMode.prepend(QStringLiteral("%1 ").arg(cmd));
165 }
166
167 return currentMode;
168}
169
170void KateViInputMode::gotFocus()
171{
172 // nothing to do
173}
174
175void KateViInputMode::lostFocus()
176{
177 // nothing to do
178}
179
180void KateViInputMode::readSessionConfig(const KConfigGroup &config)
181{
182 // restore vi registers and jump list
183 m_viModeManager->readSessionConfig(config);
184}
185
186void KateViInputMode::writeSessionConfig(KConfigGroup &config)
187{
188 // save vi registers and jump list
189 m_viModeManager->writeSessionConfig(config);
190}
191
192void KateViInputMode::updateConfig()
193{
194 KateViewConfig *cfg = view()->config();
195
196 // whether relative line numbers should be used or not.
197 m_relLineNumbers = cfg->viRelativeLineNumbers();
198
199 if (m_activated) {
200 viewInternal()->iconBorder()->setRelLineNumbersOn(m_relLineNumbers);
201 }
202}
203
204void KateViInputMode::readWriteChanged(bool)
205{
206 // nothing todo
207}
208
209void KateViInputMode::find()
210{
211 showViModeEmulatedCommandBar();
212 viModeEmulatedCommandBar()->init(KateVi::EmulatedCommandBar::SearchForward);
213}
214
215void KateViInputMode::findSelectedForwards()
216{
217 m_viModeManager->searcher()->findNext();
218}
219
220void KateViInputMode::findSelectedBackwards()
221{
222 m_viModeManager->searcher()->findPrevious();
223}
224
225void KateViInputMode::findReplace()
226{
227 showViModeEmulatedCommandBar();
228 viModeEmulatedCommandBar()->init(KateVi::EmulatedCommandBar::SearchForward);
229}
230
231void KateViInputMode::findNext()
232{
233 m_viModeManager->searcher()->findNext();
234}
235
236void KateViInputMode::findPrevious()
237{
238 m_viModeManager->searcher()->findPrevious();
239}
240
241void KateViInputMode::activateCommandLine()
242{
243 showViModeEmulatedCommandBar();
244 viModeEmulatedCommandBar()->init(KateVi::EmulatedCommandBar::Command);
245}
246
247void KateViInputMode::showViModeEmulatedCommandBar()
248{
249 view()->bottomViewBar()->addBarWidget(viModeEmulatedCommandBar());
250 view()->bottomViewBar()->showBarWidget(viModeEmulatedCommandBar());
251}
252
253KateVi::EmulatedCommandBar *KateViInputMode::viModeEmulatedCommandBar()
254{
255 if (!m_viModeEmulatedCommandBar) {
256 m_viModeEmulatedCommandBar = new KateVi::EmulatedCommandBar(this, m_viModeManager.get(), view());
257 m_viModeEmulatedCommandBar->hide();
258 }
259
260 return m_viModeEmulatedCommandBar;
261}
262
263void KateViInputMode::updateRendererConfig()
264{
265 m_viModeManager->searcher()->updateHighlightColors();
266}
267
268bool KateViInputMode::keyPress(QKeyEvent *e)
269{
270 if (m_nextKeypressIsOverriddenShortCut) {
271 // This is just the replay of a shortcut that we stole, this time as a QKeyEvent.
272 // Ignore it, as we'll have already handled it via stealKey()!
273 m_nextKeypressIsOverriddenShortCut = false;
274 return true;
275 }
276
277 if (m_viModeManager->handleKeypress(e)) {
278 Q_EMIT view()->viewModeChanged(view(), viewMode());
279 return true;
280 }
281
282 return false;
283}
284
285bool KateViInputMode::blinkCaret() const
286{
287 return false;
288}
289
290KTextEditor::caretStyles KateViInputMode::caretStyle() const
291{
292 return m_caret;
293}
294
295void KateViInputMode::toggleInsert()
296{
297 // do nothing
298}
299
300void KateViInputMode::launchInteractiveCommand(const QString &)
301{
302 // do nothing so far
303}
304
305QString KateViInputMode::bookmarkLabel(int line) const
306{
307 return m_viModeManager->marks()->getMarksOnTheLine(line);
308}
309
310void KateViInputMode::setCaretStyle(const KTextEditor::caretStyles caret)
311{
312 if (m_caret != caret) {
313 m_caret = caret;
314
315 view()->renderer()->setCaretStyle(m_caret);
316 view()->renderer()->setDrawCaret(true);
317 viewInternal()->paintCursor();
318 }
319}
The Cursor represents a position in a Document.
Definition cursor.h:75
ViewMode
Possible view modes These correspond to various modes the text editor might be in.
Definition view.h:295
InputMode
Possible input modes.
Definition view.h:286
@ ViInputMode
Vi mode.
Definition view.h:288
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)
void hide()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 4 2024 12:03:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.