1 /**************************************************************************** 2 ** 3 ** Copyright (C) 2016 The Qt Company Ltd. 4 ** Contact: https://www.qt.io/licensing/ 5 ** 6 ** This file is part of Qt Creator. 7 ** 8 ** Commercial License Usage 9 ** Licensees holding valid commercial Qt licenses may use this file in 10 ** accordance with the commercial license agreement provided with the 11 ** Software or, alternatively, in accordance with the terms contained in 12 ** a written agreement between you and The Qt Company. For licensing terms 13 ** and conditions see https://www.qt.io/terms-conditions. For further 14 ** information use the contact form at https://www.qt.io/contact-us. 15 ** 16 ** GNU General Public License Usage 17 ** Alternatively, this file may be used under the terms of the GNU 18 ** General Public License version 3 as published by the Free Software 19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT 20 ** included in the packaging of this file. Please review the following 21 ** information to ensure the GNU General Public License requirements will 22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html. 23 ** 24 ****************************************************************************/ 25 26 #pragma once 27 28 #define FAKEVIM_STANDALONE 29 30 #ifdef FAKEVIM_STANDALONE 31 # include "private/fakevim_export.h" 32 #endif 33 34 #include <QObject> 35 #include <QTextEdit> 36 37 #include <functional> 38 #include <vector> 39 40 namespace FakeVim { 41 namespace Internal { 42 43 enum RangeMode 44 { 45 // Reordering first three enum items here will break 46 // compatibility with clipboard format stored by Vim. 47 RangeCharMode, // v 48 RangeLineMode, // V 49 RangeBlockMode, // Ctrl-v 50 RangeLineModeExclusive, 51 RangeBlockAndTailMode // Ctrl-v for D and X 52 }; 53 54 struct FAKEVIM_EXPORT Range 55 { 56 Range() = default; 57 Range(int b, int e, RangeMode m = RangeCharMode); 58 QString toString() const; 59 bool isValid() const; 60 61 int beginPos = -1; 62 int endPos = -1; 63 RangeMode rangemode = RangeCharMode; 64 }; 65 66 struct FAKEVIM_EXPORT ExCommand 67 { 68 ExCommand() = default; 69 ExCommand(const QString &cmd, const QString &args = QString(), 70 const Range &range = Range()); 71 72 bool matches(const QString &min, const QString &full) const; 73 74 QString cmd; 75 bool hasBang = false; 76 QString args; 77 Range range; 78 int count = 1; 79 }; 80 81 // message levels sorted by severity 82 enum MessageLevel 83 { 84 MessageMode, // show current mode (format "-- %1 --") 85 MessageCommand, // show last Ex command or search 86 MessageInfo, // result of a command 87 MessageWarning, // warning 88 MessageError, // error 89 MessageShowCmd // partial command 90 }; 91 92 template <typename Type> 93 class Signal 94 { 95 public: 96 using Callable = std::function<Type>; 97 connect(const Callable & callable)98 void connect(const Callable &callable) { m_callables.push_back(callable); } 99 100 template <typename ...Args> operator()101 void operator()(Args ...args) const 102 { 103 for (const Callable &callable : m_callables) 104 callable(args...); 105 } 106 107 private: 108 std::vector<Callable> m_callables; 109 }; 110 111 class FAKEVIM_EXPORT FakeVimHandler : public QObject 112 { 113 Q_OBJECT 114 115 public: 116 explicit FakeVimHandler(QWidget *widget, QObject *parent = nullptr); 117 ~FakeVimHandler() override; 118 119 QWidget *widget(); 120 121 // call before widget is deleted 122 void disconnectFromEditor(); 123 124 static void updateGlobalMarksFilenames(const QString &oldFileName, const QString &newFileName); 125 126 public: 127 void setCurrentFileName(const QString &fileName); 128 QString currentFileName() const; 129 130 void showMessage(MessageLevel level, const QString &msg); 131 132 // This executes an "ex" style command taking context 133 // information from the current widget. 134 void handleCommand(const QString &cmd); 135 void handleReplay(const QString &keys); 136 void handleInput(const QString &keys); 137 void enterCommandMode(); 138 139 void installEventFilter(); 140 141 // Convenience 142 void setupWidget(); 143 void restoreWidget(int tabSize); 144 145 // Test only 146 int physicalIndentation(const QString &line) const; 147 int logicalIndentation(const QString &line) const; 148 QString tabExpand(int n) const; 149 150 void miniBufferTextEdited(const QString &text, int cursorPos, int anchorPos); 151 152 // Set text cursor position. Keeps anchor if in visual mode. 153 void setTextCursorPosition(int position); 154 155 QTextCursor textCursor() const; 156 void setTextCursor(const QTextCursor &cursor); 157 158 bool jumpToLocalMark(QChar mark, bool backTickMode); 159 160 bool eventFilter(QObject *ob, QEvent *ev) override; 161 162 Signal<void(const QString &msg, int cursorPos, int anchorPos, int messageLevel)> commandBufferChanged; 163 Signal<void(const QString &msg)> statusDataChanged; 164 Signal<void(const QString &msg)> extraInformationChanged; 165 Signal<void(const QList<QTextEdit::ExtraSelection> &selection)> selectionChanged; 166 Signal<void(const QString &needle)> highlightMatches; 167 Signal<void(bool *moved, bool *forward, QTextCursor *cursor)> moveToMatchingParenthesis; 168 Signal<void(bool *result, QChar c)> checkForElectricCharacter; 169 Signal<void(int beginLine, int endLine, QChar typedChar)> indentRegion; 170 Signal<void(const QString &needle, bool forward)> simpleCompletionRequested; 171 Signal<void(const QString &key, int count)> windowCommandRequested; 172 Signal<void(bool reverse)> findRequested; 173 Signal<void(bool reverse)> findNextRequested; 174 Signal<void(bool *handled, const ExCommand &cmd)> handleExCommandRequested; 175 Signal<void()> requestDisableBlockSelection; 176 Signal<void(const QTextCursor &cursor)> requestSetBlockSelection; 177 Signal<void(QTextCursor *cursor)> requestBlockSelection; 178 Signal<void(bool *on)> requestHasBlockSelection; 179 Signal<void(int depth)> foldToggle; 180 Signal<void(bool fold)> foldAll; 181 Signal<void(int depth, bool dofold)> fold; 182 Signal<void(int count, bool current)> foldGoTo; 183 Signal<void(QChar mark, bool backTickMode, const QString &fileName)> requestJumpToLocalMark; 184 Signal<void(QChar mark, bool backTickMode, const QString &fileName)> requestJumpToGlobalMark; 185 Signal<void()> completionRequested; 186 Signal<void()> tabPreviousRequested; 187 Signal<void()> tabNextRequested; 188 189 public: 190 class Private; 191 192 private: 193 Private *d; 194 }; 195 196 } // namespace Internal 197 } // namespace FakeVim 198 199 Q_DECLARE_METATYPE(FakeVim::Internal::ExCommand) 200