| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2008 Erlend Hamberg <ehamberg@gmail.com> |
| 3 | SPDX-FileCopyrightText: 2011 Svyatoslav Kuzmich <svatoslav1@gmail.com> |
| 4 | SPDX-FileCopyrightText: 2012-2013 Simon St James <kdedevel@etotheipiplusone.com> |
| 5 | |
| 6 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #ifndef KATEVI_INPUT_MODE_MANAGER_H |
| 10 | #define KATEVI_INPUT_MODE_MANAGER_H |
| 11 | |
| 12 | #include <QKeyEvent> |
| 13 | #include <QStack> |
| 14 | #include <ktexteditor/cursor.h> |
| 15 | #include <ktexteditor/view.h> |
| 16 | #include <ktexteditor_export.h> |
| 17 | |
| 18 | #include <vimode/completion.h> |
| 19 | #include <vimode/definitions.h> |
| 20 | |
| 21 | class KConfigGroup; |
| 22 | class KateViewInternal; |
| 23 | class KateViInputMode; |
| 24 | class QString; |
| 25 | |
| 26 | namespace KTextEditor |
| 27 | { |
| 28 | class ViewPrivate; |
| 29 | class DocumentPrivate; |
| 30 | class MovingCursor; |
| 31 | class Mark; |
| 32 | class MarkInterface; |
| 33 | } |
| 34 | |
| 35 | namespace KateVi |
| 36 | { |
| 37 | class GlobalState; |
| 38 | class Searcher; |
| 39 | class CompletionRecorder; |
| 40 | class CompletionReplayer; |
| 41 | class Marks; |
| 42 | class Jumps; |
| 43 | class MacroRecorder; |
| 44 | class LastChangeRecorder; |
| 45 | class ModeBase; |
| 46 | class NormalViMode; |
| 47 | class InsertViMode; |
| 48 | class VisualViMode; |
| 49 | class ReplaceViMode; |
| 50 | class KeyParser; |
| 51 | class KeyMapper; |
| 52 | |
| 53 | class InputModeManager |
| 54 | { |
| 55 | friend KateViInputMode; |
| 56 | |
| 57 | public: |
| 58 | InputModeManager(KateViInputMode *inputAdapter, KTextEditor::ViewPrivate *view, KateViewInternal *viewInternal); |
| 59 | ~InputModeManager(); |
| 60 | InputModeManager(const InputModeManager &) = delete; |
| 61 | InputModeManager &operator=(const InputModeManager &) = delete; |
| 62 | |
| 63 | /** |
| 64 | * feed key the given key press to the command parser |
| 65 | * @return true if keypress was is [part of a] command, false otherwise |
| 66 | */ |
| 67 | bool handleKeypress(const QKeyEvent *e); |
| 68 | |
| 69 | /** |
| 70 | * feed key the given list of key presses to the key handling code, one by one |
| 71 | */ |
| 72 | void feedKeyPresses(const QString &keyPresses) const; |
| 73 | |
| 74 | /** |
| 75 | * Determines whether we are currently processing a Vi keypress |
| 76 | * @return true if we are still in a call to handleKeypress, false otherwise |
| 77 | */ |
| 78 | bool isHandlingKeypress() const; |
| 79 | |
| 80 | /** |
| 81 | * @return The current vi mode |
| 82 | */ |
| 83 | KTEXTEDITOR_EXPORT ViMode getCurrentViMode() const; |
| 84 | |
| 85 | /** |
| 86 | * @return The current vi mode string representation |
| 87 | */ |
| 88 | KTextEditor::View::ViewMode getCurrentViewMode() const; |
| 89 | |
| 90 | /** |
| 91 | * @return the previous vi mode |
| 92 | */ |
| 93 | ViMode getPreviousViMode() const; |
| 94 | |
| 95 | /** |
| 96 | * @return true if and only if the current mode is one of VisualMode, VisualBlockMode or VisualLineMode. |
| 97 | */ |
| 98 | bool isAnyVisualMode() const; |
| 99 | |
| 100 | /** |
| 101 | * @return one of getViNormalMode(), getViVisualMode(), etc, depending on getCurrentViMode(). |
| 102 | */ |
| 103 | ModeBase *getCurrentViModeHandler() const; |
| 104 | |
| 105 | const QString getVerbatimKeys() const; |
| 106 | |
| 107 | /** |
| 108 | * changes the current vi mode to the given mode |
| 109 | */ |
| 110 | void changeViMode(ViMode newMode); |
| 111 | |
| 112 | /** |
| 113 | * set normal mode to be the active vi mode and perform the needed setup work |
| 114 | */ |
| 115 | KTEXTEDITOR_EXPORT void viEnterNormalMode(); |
| 116 | |
| 117 | /** |
| 118 | * set insert mode to be the active vi mode and perform the needed setup work |
| 119 | */ |
| 120 | void viEnterInsertMode(); |
| 121 | |
| 122 | /** |
| 123 | * set visual mode to be the active vi mode and make the needed setup work |
| 124 | */ |
| 125 | void viEnterVisualMode(ViMode visualMode = ViMode::VisualMode); |
| 126 | |
| 127 | /** |
| 128 | * set replace mode to be the active vi mode and make the needed setup work |
| 129 | */ |
| 130 | void viEnterReplaceMode(); |
| 131 | |
| 132 | /** |
| 133 | * @return the NormalMode instance |
| 134 | */ |
| 135 | NormalViMode *getViNormalMode(); |
| 136 | |
| 137 | /** |
| 138 | * @return the InsertMode instance |
| 139 | */ |
| 140 | InsertViMode *getViInsertMode(); |
| 141 | |
| 142 | /** |
| 143 | * @return the VisualMode instance |
| 144 | */ |
| 145 | VisualViMode *getViVisualMode(); |
| 146 | |
| 147 | /** |
| 148 | * @return the ReplaceMode instance |
| 149 | */ |
| 150 | ReplaceViMode *getViReplaceMode(); |
| 151 | |
| 152 | /** |
| 153 | * clear the key event log |
| 154 | */ |
| 155 | void clearCurrentChangeLog(); |
| 156 | |
| 157 | /** |
| 158 | * copy the contents of the key events log to m_lastChange so that it can be repeated |
| 159 | */ |
| 160 | void storeLastChangeCommand(); |
| 161 | |
| 162 | /** |
| 163 | * repeat last change by feeding the contents of m_lastChange to feedKeys() |
| 164 | */ |
| 165 | void repeatLastChange(); |
| 166 | |
| 167 | void doNotLogCurrentKeypress(); |
| 168 | |
| 169 | bool getTemporaryNormalMode() |
| 170 | { |
| 171 | return m_temporaryNormalMode; |
| 172 | } |
| 173 | |
| 174 | void setTemporaryNormalMode(bool b) |
| 175 | { |
| 176 | m_temporaryNormalMode = b; |
| 177 | } |
| 178 | |
| 179 | void reset(); |
| 180 | |
| 181 | inline Marks *marks() |
| 182 | { |
| 183 | return m_marks; |
| 184 | } |
| 185 | inline Jumps *jumps() |
| 186 | { |
| 187 | return m_jumps; |
| 188 | } |
| 189 | |
| 190 | inline Searcher *searcher() |
| 191 | { |
| 192 | return m_searcher; |
| 193 | } |
| 194 | |
| 195 | CompletionRecorder *completionRecorder() |
| 196 | { |
| 197 | return m_completionRecorder; |
| 198 | } |
| 199 | CompletionReplayer *completionReplayer() |
| 200 | { |
| 201 | return m_completionReplayer; |
| 202 | } |
| 203 | |
| 204 | MacroRecorder *macroRecorder() |
| 205 | { |
| 206 | return m_macroRecorder; |
| 207 | } |
| 208 | |
| 209 | LastChangeRecorder *lastChangeRecorder() |
| 210 | { |
| 211 | return m_lastChangeRecorder; |
| 212 | } |
| 213 | |
| 214 | // session stuff |
| 215 | void readSessionConfig(const KConfigGroup &config); |
| 216 | void writeSessionConfig(KConfigGroup &config); |
| 217 | |
| 218 | KTEXTEDITOR_EXPORT KeyMapper *keyMapper(); |
| 219 | GlobalState *globalState() const; |
| 220 | KTextEditor::ViewPrivate *view() const; |
| 221 | |
| 222 | KateViInputMode *inputAdapter() |
| 223 | { |
| 224 | return m_inputAdapter; |
| 225 | } |
| 226 | |
| 227 | void updateCursor(const KTextEditor::Cursor c); |
| 228 | |
| 229 | void pushKeyMapper(std::shared_ptr<KeyMapper> mapper); |
| 230 | void popKeyMapper(); |
| 231 | |
| 232 | private: |
| 233 | NormalViMode *m_viNormalMode; |
| 234 | InsertViMode *m_viInsertMode; |
| 235 | VisualViMode *m_viVisualMode; |
| 236 | ReplaceViMode *m_viReplaceMode; |
| 237 | |
| 238 | ViMode m_currentViMode; |
| 239 | ViMode m_previousViMode; |
| 240 | |
| 241 | KateViInputMode *m_inputAdapter; |
| 242 | KTextEditor::ViewPrivate *m_view; |
| 243 | KateViewInternal *m_viewInternal; |
| 244 | KeyParser *m_keyParser; |
| 245 | |
| 246 | // Create a new keymapper for each macro event, to simplify expansion of mappings in macros |
| 247 | // where the macro itself was triggered by expanding a mapping! |
| 248 | QStack<std::shared_ptr<KeyMapper>> m_keyMapperStack; |
| 249 | |
| 250 | int m_insideHandlingKeyPressCount; |
| 251 | |
| 252 | /** |
| 253 | * a list of the (encoded) key events that was part of the last change. |
| 254 | */ |
| 255 | QString m_lastChange; |
| 256 | |
| 257 | CompletionList m_lastChangeCompletionsLog; |
| 258 | |
| 259 | /** |
| 260 | * true when normal mode was started by Ctrl-O command in insert mode. |
| 261 | */ |
| 262 | bool m_temporaryNormalMode; |
| 263 | |
| 264 | Marks *m_marks; |
| 265 | Jumps *m_jumps; |
| 266 | |
| 267 | Searcher *m_searcher; |
| 268 | CompletionRecorder *m_completionRecorder; |
| 269 | CompletionReplayer *m_completionReplayer; |
| 270 | |
| 271 | MacroRecorder *m_macroRecorder; |
| 272 | |
| 273 | LastChangeRecorder *m_lastChangeRecorder; |
| 274 | }; |
| 275 | |
| 276 | } |
| 277 | |
| 278 | #endif |
| 279 | |