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 | |
24 | namespace |
25 | { |
26 | QString 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 | |
54 | KateViInputMode::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 | |
66 | void 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(newMode: 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 | |
80 | void 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(enable: false); |
91 | } |
92 | |
93 | void 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(p: new KateVi::InputModeManager(this, view(), viewInternal())); |
102 | |
103 | if (m_viModeEmulatedCommandBar) { |
104 | m_viModeEmulatedCommandBar->setViInputModeManager(m_viModeManager.get()); |
105 | } |
106 | } |
107 | |
108 | bool KateViInputMode::overwrite() const |
109 | { |
110 | return m_viModeManager->getCurrentViMode() == KateVi::ViMode::ReplaceMode; |
111 | } |
112 | |
113 | void KateViInputMode::overwrittenChar(const QChar &c) |
114 | { |
115 | m_viModeManager->getViReplaceMode()->overwrittenChar(s: c); |
116 | } |
117 | |
118 | void KateViInputMode::clearSelection() |
119 | { |
120 | // do nothing, handled elsewhere |
121 | } |
122 | |
123 | bool 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 | |
139 | KTextEditor::View::InputMode KateViInputMode::viewInputMode() const |
140 | { |
141 | return KTextEditor::View::ViInputMode; |
142 | } |
143 | |
144 | QString KateViInputMode::viewInputModeHuman() const |
145 | { |
146 | return i18n("vi-mode" ); |
147 | } |
148 | |
149 | KTextEditor::View::ViewMode KateViInputMode::viewMode() const |
150 | { |
151 | return m_viModeManager->getCurrentViewMode(); |
152 | } |
153 | |
154 | QString KateViInputMode::viewModeHuman() const |
155 | { |
156 | QString currentMode = viModeToString(mode: m_viModeManager->getCurrentViMode()); |
157 | |
158 | if (m_viModeManager->macroRecorder()->isRecording()) { |
159 | currentMode.prepend(s: QLatin1Char('(') + i18n("recording" ) + QLatin1String(") " )); |
160 | } |
161 | |
162 | QString cmd = m_viModeManager->getVerbatimKeys(); |
163 | if (!cmd.isEmpty()) { |
164 | currentMode.prepend(QStringLiteral("%1 " ).arg(a: cmd)); |
165 | } |
166 | |
167 | return currentMode; |
168 | } |
169 | |
170 | void KateViInputMode::gotFocus() |
171 | { |
172 | // nothing to do |
173 | } |
174 | |
175 | void KateViInputMode::lostFocus() |
176 | { |
177 | // nothing to do |
178 | } |
179 | |
180 | void KateViInputMode::readSessionConfig(const KConfigGroup &config) |
181 | { |
182 | // restore vi registers and jump list |
183 | m_viModeManager->readSessionConfig(config); |
184 | } |
185 | |
186 | void KateViInputMode::writeSessionConfig(KConfigGroup &config) |
187 | { |
188 | // save vi registers and jump list |
189 | m_viModeManager->writeSessionConfig(config); |
190 | } |
191 | |
192 | void 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 | |
204 | void KateViInputMode::readWriteChanged(bool) |
205 | { |
206 | // nothing todo |
207 | } |
208 | |
209 | void KateViInputMode::find() |
210 | { |
211 | showViModeEmulatedCommandBar(); |
212 | viModeEmulatedCommandBar()->init(mode: KateVi::EmulatedCommandBar::SearchForward); |
213 | } |
214 | |
215 | void KateViInputMode::findSelectedForwards() |
216 | { |
217 | m_viModeManager->searcher()->findNext(); |
218 | } |
219 | |
220 | void KateViInputMode::findSelectedBackwards() |
221 | { |
222 | m_viModeManager->searcher()->findPrevious(); |
223 | } |
224 | |
225 | void KateViInputMode::findReplace() |
226 | { |
227 | showViModeEmulatedCommandBar(); |
228 | viModeEmulatedCommandBar()->init(mode: KateVi::EmulatedCommandBar::SearchForward); |
229 | } |
230 | |
231 | void KateViInputMode::findNext() |
232 | { |
233 | m_viModeManager->searcher()->findNext(); |
234 | } |
235 | |
236 | void KateViInputMode::findPrevious() |
237 | { |
238 | m_viModeManager->searcher()->findPrevious(); |
239 | } |
240 | |
241 | void KateViInputMode::activateCommandLine() |
242 | { |
243 | showViModeEmulatedCommandBar(); |
244 | viModeEmulatedCommandBar()->init(mode: KateVi::EmulatedCommandBar::Command); |
245 | } |
246 | |
247 | void KateViInputMode::showViModeEmulatedCommandBar() |
248 | { |
249 | view()->bottomViewBar()->addBarWidget(newBarWidget: viModeEmulatedCommandBar()); |
250 | view()->bottomViewBar()->showBarWidget(barWidget: viModeEmulatedCommandBar()); |
251 | } |
252 | |
253 | KateVi::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 | |
263 | void KateViInputMode::updateRendererConfig() |
264 | { |
265 | m_viModeManager->searcher()->updateHighlightColors(); |
266 | } |
267 | |
268 | bool 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: view(), mode: viewMode()); |
279 | return true; |
280 | } |
281 | |
282 | return false; |
283 | } |
284 | |
285 | bool KateViInputMode::blinkCaret() const |
286 | { |
287 | return false; |
288 | } |
289 | |
290 | KTextEditor::caretStyles KateViInputMode::caretStyle() const |
291 | { |
292 | return m_caret; |
293 | } |
294 | |
295 | void KateViInputMode::toggleInsert() |
296 | { |
297 | // do nothing |
298 | } |
299 | |
300 | void KateViInputMode::launchInteractiveCommand(const QString &) |
301 | { |
302 | // do nothing so far |
303 | } |
304 | |
305 | QString KateViInputMode::bookmarkLabel(int line) const |
306 | { |
307 | return m_viModeManager->marks()->getMarksOnTheLine(line); |
308 | } |
309 | |
310 | void 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 | } |
320 | |