context.hh (7056B)
1 #ifndef context_hh_INCLUDED 2 #define context_hh_INCLUDED 3 4 #include "selection.hh" 5 #include "optional.hh" 6 #include "utils.hh" 7 #include "function.hh" 8 9 namespace Kakoune 10 { 11 12 class Context; 13 class Window; 14 class Buffer; 15 class Client; 16 class Scope; 17 class InputHandler; 18 class DisplayLine; 19 enum class StatusStyle; 20 21 class AliasRegistry; 22 class FaceRegistry; 23 class OptionManager; 24 class KeymapManager; 25 class HookManager; 26 27 enum Direction { Backward = -1, Forward = 1 }; 28 29 struct JumpList 30 { 31 void push(SelectionList jump, Optional<size_t> index = {}); 32 const SelectionList& forward(Context& context, int count); 33 const SelectionList& backward(Context& context, int count); 34 void forget_buffer(Buffer& buffer); 35 36 friend bool operator==(const JumpList& lhs, const JumpList& rhs) = default; 37 38 size_t current_index() const { return m_current; } 39 40 ConstArrayView<SelectionList> get_as_list() const { return m_jumps; } 41 42 private: 43 using Contents = Vector<SelectionList, MemoryDomain::Selections>; 44 Contents m_jumps; 45 size_t m_current = 0; 46 }; 47 48 using LastSelectFunc = Function<void (Context&)>; 49 50 struct LocalScope; 51 52 // A Context is used to access non singleton objects for various services 53 // in commands. 54 // 55 // The Context object links a Client, a Window, an InputHandler and a 56 // SelectionList. It may represent an interactive user window, a hook 57 // execution or a macro replay context. 58 class Context 59 { 60 public: 61 enum class Flags 62 { 63 None = 0, 64 Draft = 1, 65 }; 66 friend constexpr bool with_bit_ops(Meta::Type<Flags>) { return true; } 67 68 Context(InputHandler& input_handler, SelectionList selections, 69 Flags flags, String name = ""); 70 71 struct EmptyContextFlag {}; 72 explicit Context(EmptyContextFlag); 73 ~Context(); 74 75 Context(const Context&) = delete; 76 Context& operator=(const Context&) = delete; 77 78 Buffer& buffer() const; 79 bool has_buffer() const { return not m_selection_history.empty(); } 80 81 Window& window() const; 82 bool has_window() const { return (bool)m_window; } 83 84 Client& client() const; 85 bool has_client() const { return (bool)m_client; } 86 87 InputHandler& input_handler() const; 88 bool has_input_handler() const { return (bool)m_input_handler; } 89 90 SelectionList& selections(bool update = true); 91 const SelectionList& selections(bool update = true) const; 92 Vector<String> selections_content() const; 93 94 // Return potentially out of date selections 95 SelectionList& selections_write_only(); 96 97 void end_selection_edition() { m_selection_history.end_edition(); } 98 template<Direction direction> 99 void undo_selection_change(); 100 101 void change_buffer(Buffer& buffer, Optional<FunctionRef<void()>> set_selection = {}); 102 void forget_buffer(Buffer& buffer); 103 104 void set_client(Client& client); 105 void set_window(Window& window); 106 107 friend struct LocalScope; 108 109 Scope& scope(bool allow_local = true) const; 110 Scope* local_scope() const { return m_local_scopes.empty() ? nullptr : m_local_scopes.back(); } 111 112 OptionManager& options() const; 113 HookManager& hooks() const; 114 KeymapManager& keymaps() const; 115 AliasRegistry& aliases() const; 116 FaceRegistry& faces(bool allow_local = true) const; 117 118 void print_status(DisplayLine prompt, DisplayLine content, ColumnCount cursor_pos, StatusStyle style) const; 119 void print_status(DisplayLine content) const; 120 121 StringView main_sel_register_value(StringView reg) const; 122 123 const String& name() const { return m_name; } 124 void set_name(String name); 125 126 bool is_editing() const { return m_edition_level!= 0; } 127 void disable_undo_handling() { m_edition_level = -1; } 128 129 NestedBool& hooks_disabled() { return m_hooks_disabled; } 130 const NestedBool& hooks_disabled() const { return m_hooks_disabled; } 131 132 NestedBool& keymaps_disabled() { return m_keymaps_disabled; } 133 const NestedBool& keymaps_disabled() const { return m_keymaps_disabled; } 134 135 Flags flags() const { return m_flags; } 136 137 JumpList& jump_list() { return m_jump_list; } 138 void push_jump(bool force = false); 139 140 template<typename Func> 141 void set_last_select(Func&& last_select) { m_last_select = std::forward<Func>(last_select); } 142 143 void repeat_last_select() { if (m_last_select) m_last_select(*this); } 144 145 Buffer* last_buffer() const; 146 147 bool ensure_cursor_visible = true; 148 private: 149 void begin_edition(); 150 void end_edition(); 151 int m_edition_level = 0; 152 size_t m_edition_timestamp = 0; 153 154 friend struct ScopedEdition; 155 friend struct ScopedSelectionEdition; 156 157 Flags m_flags = Flags::None; 158 159 SafePtr<InputHandler> m_input_handler; 160 SafePtr<Window> m_window; 161 SafePtr<Client> m_client; 162 Vector<Scope*> m_local_scopes; 163 164 class SelectionHistory { 165 public: 166 SelectionHistory(Context& context); 167 SelectionHistory(Context& context, SelectionList selections); 168 void initialize(SelectionList selections); 169 bool empty() const { return m_history.empty() and not m_staging; } 170 SelectionList& selections(bool update = true); 171 172 void begin_edition(); 173 void end_edition(); 174 bool in_edition() const { return m_in_edition; } 175 176 template<Direction direction> 177 void undo(); 178 void forget_buffer(Buffer& buffer); 179 private: 180 enum class HistoryId : size_t { First = 0, Invalid = (size_t)-1 }; 181 182 struct HistoryNode 183 { 184 HistoryNode(SelectionList selections, HistoryId parent) : selections(selections), parent(parent) {} 185 186 SelectionList selections; 187 HistoryId parent; 188 HistoryId redo_child = HistoryId::Invalid; 189 }; 190 191 HistoryId next_history_id() const noexcept { return (HistoryId)m_history.size(); } 192 HistoryNode& history_node(HistoryId id) { return m_history[(size_t)id]; } 193 const HistoryNode& history_node(HistoryId id) const { return m_history[(size_t)id]; } 194 HistoryNode& current_history_node() { kak_assert((size_t)m_history_id < m_history.size()); return m_history[(size_t)m_history_id]; } 195 196 Context& m_context; 197 Vector<HistoryNode> m_history; 198 HistoryId m_history_id = HistoryId::Invalid; 199 Optional<HistoryNode> m_staging; 200 NestedBool m_in_edition; 201 }; 202 SelectionHistory m_selection_history; 203 204 String m_name; 205 206 JumpList m_jump_list; 207 208 LastSelectFunc m_last_select; 209 210 NestedBool m_hooks_disabled; 211 NestedBool m_keymaps_disabled; 212 }; 213 214 struct ScopedEdition 215 { 216 ScopedEdition(Context& context); 217 ~ScopedEdition(); 218 ScopedEdition(const ScopedEdition&) = delete; 219 220 Context& context() const { return m_context; } 221 private: 222 Context& m_context; 223 SafePtr<Buffer> m_buffer; 224 }; 225 226 struct ScopedSelectionEdition 227 { 228 ScopedSelectionEdition(Context& context); 229 ScopedSelectionEdition(ScopedSelectionEdition&& other); 230 ~ScopedSelectionEdition(); 231 232 private: 233 Context& m_context; 234 bool m_valid; 235 }; 236 237 } 238 #endif // context_hh_INCLUDED