input_handler.hh (5763B)
1 #ifndef input_handler_hh_INCLUDED 2 #define input_handler_hh_INCLUDED 3 4 #include "completion.hh" 5 #include "array.hh" 6 #include "context.hh" 7 #include "env_vars.hh" 8 #include "enum.hh" 9 #include "face.hh" 10 #include "normal.hh" 11 #include "optional.hh" 12 #include "keys.hh" 13 #include "string.hh" 14 #include "utils.hh" 15 #include "safe_ptr.hh" 16 #include "display_buffer.hh" 17 18 namespace Kakoune 19 { 20 21 enum class PromptEvent 22 { 23 Change, 24 Abort, 25 Validate 26 }; 27 using PromptCallback = MoveOnlyFunction<void (StringView, PromptEvent, Context&)>; 28 29 enum class PromptFlags 30 { 31 None = 0, 32 Password = 1 << 0, 33 DropHistoryEntriesWithBlankPrefix = 1 << 1, 34 Search = 1 << 2, 35 Command = 1 << 3, 36 }; 37 constexpr bool with_bit_ops(Meta::Type<PromptFlags>) { return true; } 38 39 using KeyCallback = Function<void (Key, Context&)>; 40 41 class InputMode; 42 enum class KeymapMode : char; 43 enum class CursorMode; 44 45 class Timer; 46 47 using PromptCompleter = Function<Completions (const Context&, StringView, ByteCount)>; 48 enum class InsertMode : unsigned 49 { 50 Insert, 51 Append, 52 Replace, 53 InsertAtLineBegin, 54 AppendAtLineEnd, 55 OpenLineBelow, 56 OpenLineAbove 57 }; 58 59 struct ModeInfo 60 { 61 DisplayLine display_line; 62 Optional<NormalParams> normal_params; 63 }; 64 65 class InputHandler : public SafeCountable 66 { 67 public: 68 InputHandler(SelectionList selections, 69 Context::Flags flags = Context::Flags::None, 70 String name = ""); 71 ~InputHandler(); 72 73 // switch to insert mode 74 void insert(InsertMode mode, int count); 75 // repeat last insert mode key sequence 76 void repeat_last_insert(); 77 // insert a string without affecting the mode stack 78 void paste(StringView content); 79 80 // enter prompt mode, callback is called on each change, 81 // abort or validation with corresponding PromptEvent value 82 // returns to normal mode after validation if callback does 83 // not change the mode itself 84 void prompt(StringView prompt, String initstr, String emptystr, 85 Face prompt_face, PromptFlags flags, char history_register, 86 PromptCompleter completer, PromptCallback callback); 87 void set_prompt_face(Face prompt_face); 88 bool history_enabled() const; 89 90 // execute callback on next keypress and returns to normal mode 91 // if callback does not change the mode itself 92 void on_next_key(StringView mode_name, KeymapMode mode, KeyCallback callback, 93 Function<void (Timer& timer)> idle_callback = {}); 94 95 // process the given key 96 void handle_key(Key key, bool synthesized = true); 97 98 void refresh_ifn(); 99 100 void start_recording(char reg); 101 bool is_recording() const; 102 void stop_recording(); 103 char recording_reg() const { return m_recording_reg; } 104 105 void reset_normal_mode(); 106 107 Context& context() { return m_context; } 108 const Context& context() const { return m_context; } 109 110 ModeInfo mode_info() const; 111 112 // Force an input handler into normal mode temporarily 113 struct ScopedForceNormal 114 { 115 ScopedForceNormal(InputHandler& handler, NormalParams params); 116 ~ScopedForceNormal(); 117 118 private: 119 InputHandler& m_handler; 120 InputMode* m_mode; 121 }; 122 123 private: 124 Context m_context; 125 126 friend class InputMode; 127 Vector<RefPtr<InputMode>, MemoryDomain::Client> m_mode_stack; 128 129 InputMode& current_mode() const { return *m_mode_stack.back(); } 130 131 void push_mode(InputMode* new_mode); 132 void pop_mode(InputMode* current_mode); 133 134 void record_key(Key key); 135 void drop_last_recorded_key(); 136 137 struct Insertion{ 138 NestedBool recording; 139 bool repeating; 140 InsertMode mode; 141 Vector<Key> keys; 142 bool disable_hooks; 143 int count; 144 } m_last_insert = { {}, false, InsertMode::Insert, {}, false, 1 }; 145 146 int m_handle_key_level = 0; 147 148 char m_recording_reg = 0; 149 Vector<Key> m_recorded_keys; 150 int m_recording_level = -1; 151 }; 152 153 enum class AutoInfo 154 { 155 None = 0, 156 Command = 1 << 0, 157 OnKey = 1 << 1, 158 Normal = 1 << 2 159 }; 160 161 constexpr bool with_bit_ops(Meta::Type<AutoInfo>) { return true; } 162 163 constexpr auto enum_desc(Meta::Type<AutoInfo>) 164 { 165 return make_array<EnumDesc<AutoInfo>>({ 166 { AutoInfo::Command, "command"}, 167 { AutoInfo::OnKey, "onkey"}, 168 { AutoInfo::Normal, "normal" } 169 }); 170 } 171 172 enum class AutoComplete 173 { 174 None = 0, 175 Insert = 0b01, 176 Prompt = 0b10 177 }; 178 constexpr bool with_bit_ops(Meta::Type<AutoComplete>) { return true; } 179 180 constexpr auto enum_desc(Meta::Type<AutoComplete>) 181 { 182 return make_array<EnumDesc<AutoComplete>>({ 183 { AutoComplete::Insert, "insert"}, 184 { AutoComplete::Prompt, "prompt" } 185 }); 186 } 187 188 bool should_show_info(AutoInfo mask, const Context& context); 189 bool show_auto_info_ifn(StringView title, StringView info, AutoInfo mask, const Context& context); 190 void hide_auto_info_ifn(const Context& context, bool hide); 191 192 template<typename Cmd> 193 void on_next_key_with_autoinfo(const Context& context, StringView mode_name, 194 KeymapMode keymap_mode, Cmd cmd, 195 String title, String info) 196 { 197 context.input_handler().on_next_key(mode_name, 198 keymap_mode, [cmd](Key key, Context& context) mutable { 199 bool hide = should_show_info(AutoInfo::OnKey, context); 200 hide_auto_info_ifn(context, hide); 201 cmd(key, context); 202 }, [&context, title=std::move(title), info=std::move(info)](Timer&) { 203 show_auto_info_ifn(title, info, AutoInfo::OnKey, context); 204 }); 205 } 206 207 enum class OnHiddenCursor { 208 PreserveSelections, 209 MoveCursor, 210 MoveCursorAndAnchor, 211 }; 212 213 void scroll_window(Context& context, LineCount offset, OnHiddenCursor on_hidden_cursor); 214 215 } 216 217 #endif // input_handler_hh_INCLUDED