| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2013-2016 Simon St James <kdedevel@etotheipiplusone.com> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #ifndef KATEVI_EMULATED_COMMAND_BAR_H |
| 8 | #define KATEVI_EMULATED_COMMAND_BAR_H |
| 9 | |
| 10 | #include "kateviewhelpers.h" |
| 11 | #include <vimode/cmds.h> |
| 12 | |
| 13 | namespace KTextEditor |
| 14 | { |
| 15 | class ViewPrivate; |
| 16 | class Command; |
| 17 | } |
| 18 | |
| 19 | class QLabel; |
| 20 | class QLayout; |
| 21 | |
| 22 | namespace KateVi |
| 23 | { |
| 24 | class MatchHighlighter; |
| 25 | class InteractiveSedReplaceMode; |
| 26 | class SearchMode; |
| 27 | class CommandMode; |
| 28 | class ActiveMode; |
| 29 | class Completer; |
| 30 | |
| 31 | /** |
| 32 | * A KateViewBarWidget that attempts to emulate some of the features of Vim's own command bar, |
| 33 | * including insertion of register contents via ctr-r<registername>; dismissal via |
| 34 | * ctrl-c and ctrl-[; bi-directional incremental searching, with SmartCase; interactive sed-replace; |
| 35 | * plus a few extensions such as completion from document and navigable sed search and sed replace history. |
| 36 | */ |
| 37 | class EmulatedCommandBar : public KateViewBarWidget |
| 38 | { |
| 39 | public: |
| 40 | enum Mode { |
| 41 | NoMode, |
| 42 | SearchForward, |
| 43 | SearchBackward, |
| 44 | Command |
| 45 | }; |
| 46 | explicit EmulatedCommandBar(KateViInputMode *viInputMode, InputModeManager *viInputModeManager, QWidget *parent = nullptr); |
| 47 | ~EmulatedCommandBar() override; |
| 48 | void init(Mode mode, const QString &initialText = QString()); |
| 49 | bool isActive() const; |
| 50 | KTEXTEDITOR_EXPORT void setCommandResponseMessageTimeout(long commandResponseMessageTimeOutMS); |
| 51 | bool handleKeyPress(const QKeyEvent *keyEvent); |
| 52 | bool isSendingSyntheticSearchCompletedKeypress(); |
| 53 | |
| 54 | void startInteractiveSearchAndReplace(std::shared_ptr<SedReplace::InteractiveSedReplacer> interactiveSedReplace); |
| 55 | KTEXTEDITOR_EXPORT QString executeCommand(const QString &commandToExecute); |
| 56 | |
| 57 | void setViInputModeManager(InputModeManager *viInputModeManager); |
| 58 | |
| 59 | private: |
| 60 | KateViInputMode *m_viInputMode; |
| 61 | InputModeManager *m_viInputModeManager; |
| 62 | bool m_isActive = false; |
| 63 | bool m_wasAborted = true; |
| 64 | Mode m_mode = NoMode; |
| 65 | KTextEditor::ViewPrivate *m_view = nullptr; |
| 66 | QLineEdit *m_edit = nullptr; |
| 67 | |
| 68 | QLabel *m_barTypeIndicator = nullptr; |
| 69 | void showBarTypeIndicator(Mode mode); |
| 70 | |
| 71 | bool m_suspendEditEventFiltering = false; |
| 72 | |
| 73 | bool m_waitingForRegister = false; |
| 74 | QLabel *m_waitingForRegisterIndicator; |
| 75 | bool m_insertedTextShouldBeEscapedForSearchingAsLiteral = false; |
| 76 | |
| 77 | void hideAllWidgetsExcept(QWidget *widgetToKeepVisible); |
| 78 | |
| 79 | friend class ActiveMode; |
| 80 | std::unique_ptr<MatchHighlighter> m_matchHighligher; |
| 81 | |
| 82 | std::unique_ptr<Completer> m_completer; |
| 83 | |
| 84 | std::unique_ptr<InteractiveSedReplaceMode> m_interactiveSedReplaceMode; |
| 85 | std::unique_ptr<SearchMode> m_searchMode; |
| 86 | std::unique_ptr<CommandMode> m_commandMode; |
| 87 | |
| 88 | void switchToMode(ActiveMode *newMode); |
| 89 | ActiveMode *m_currentMode = nullptr; |
| 90 | |
| 91 | bool barHandledKeypress(const QKeyEvent *keyEvent); |
| 92 | void insertRegisterContents(const QKeyEvent *keyEvent); |
| 93 | bool eventFilter(QObject *object, QEvent *event) override; |
| 94 | void deleteSpacesToLeftOfCursor(); |
| 95 | void deleteWordCharsToLeftOfCursor(); |
| 96 | bool deleteNonWordCharsToLeftOfCursor(); |
| 97 | |
| 98 | void closed() override; |
| 99 | void closeWithStatusMessage(const QString &exitStatusMessage); |
| 100 | QTimer *m_exitStatusMessageDisplayHideTimer; |
| 101 | QLabel *m_exitStatusMessageDisplay; |
| 102 | long m_exitStatusMessageHideTimeOutMS = 4000; |
| 103 | |
| 104 | void createAndAddBarTypeIndicator(QLayout *layout); |
| 105 | void createAndAddEditWidget(QLayout *layout); |
| 106 | void createAndAddExitStatusMessageDisplay(QLayout *layout); |
| 107 | void createAndInitExitStatusMessageDisplayTimer(); |
| 108 | void createAndAddWaitingForRegisterIndicator(QLayout *layout); |
| 109 | |
| 110 | private: |
| 111 | void editTextChanged(const QString &newText); |
| 112 | void startHideExitStatusMessageTimer(); |
| 113 | }; |
| 114 | } |
| 115 | |
| 116 | #endif /* KATEVI_EMULATED_COMMAND_BAR_H */ |
| 117 | |