context.cc (13481B)
1 #include "context.hh" 2 3 #include "client.hh" 4 #include "user_interface.hh" 5 #include "scope.hh" 6 #include "buffer_manager.hh" 7 #include "register_manager.hh" 8 #include "hook_manager.hh" 9 #include "face_registry.hh" 10 #include "window.hh" 11 12 namespace Kakoune 13 { 14 15 Context::~Context() = default; 16 17 Context::Context(InputHandler& input_handler, SelectionList selections, 18 Flags flags, String name) 19 : m_flags(flags), 20 m_input_handler{&input_handler}, 21 m_selection_history{*this, std::move(selections)}, 22 m_name(std::move(name)) 23 {} 24 25 Context::Context(EmptyContextFlag) : m_selection_history{*this} {} 26 27 Buffer& Context::buffer() const 28 { 29 if (not has_buffer()) 30 throw runtime_error("no buffer in context"); 31 return const_cast<Buffer&>(selections(false).buffer()); 32 } 33 34 Window& Context::window() const 35 { 36 if (not has_window()) 37 throw runtime_error("no window in context"); 38 return *m_window; 39 } 40 41 InputHandler& Context::input_handler() const 42 { 43 if (not has_input_handler()) 44 throw runtime_error("no input handler in context"); 45 return *m_input_handler; 46 } 47 48 Client& Context::client() const 49 { 50 if (not has_client()) 51 throw runtime_error("no client in context"); 52 return *m_client; 53 } 54 55 Scope& Context::scope(bool allow_local) const 56 { 57 if (allow_local and not m_local_scopes.empty()) 58 return *m_local_scopes.back(); 59 if (has_window()) 60 return window(); 61 if (has_buffer()) 62 return buffer(); 63 return GlobalScope::instance(); 64 } 65 66 OptionManager& Context::options() const { return scope().options(); } 67 HookManager& Context::hooks() const { return scope().hooks(); } 68 KeymapManager& Context::keymaps() const { return scope().keymaps(); } 69 AliasRegistry& Context::aliases() const { return scope().aliases(); } 70 FaceRegistry& Context::faces(bool allow_local) const { return scope(allow_local).faces(); } 71 72 void Context::set_client(Client& client) 73 { 74 kak_assert(not has_client()); 75 m_client.reset(&client); 76 } 77 78 void Context::set_window(Window& window) 79 { 80 kak_assert(&window.buffer() == &buffer()); 81 m_window.reset(&window); 82 if (not m_local_scopes.empty()) 83 m_local_scopes.front()->reparent(window); 84 } 85 86 void Context::print_status(DisplayLine prompt, DisplayLine content, ColumnCount cursor_pos, StatusStyle style) const 87 { 88 if (has_client()) 89 client().print_status(std::move(prompt), std::move(content), cursor_pos, style); 90 } 91 92 void Context::print_status(DisplayLine status) const 93 { 94 print_status({}, std::move(status), {-1}, StatusStyle::Status); 95 } 96 97 void Context::push_jump(bool force) 98 { 99 if (force or not (m_flags & Flags::Draft)) 100 m_jump_list.push(selections()); 101 } 102 103 void JumpList::push(SelectionList jump, Optional<size_t> index) 104 { 105 if (index) 106 { 107 m_current = *index; 108 kak_assert(m_current <= m_jumps.size()); 109 } 110 111 if (m_current != m_jumps.size()) 112 m_jumps.erase(m_jumps.begin()+m_current+1, m_jumps.end()); 113 m_jumps.erase(std::remove(begin(m_jumps), end(m_jumps), jump), 114 end(m_jumps)); 115 m_jumps.push_back(jump); 116 m_current = m_jumps.size(); 117 } 118 119 const SelectionList& JumpList::forward(Context& context, int count) 120 { 121 if (m_current != m_jumps.size() and 122 m_current + count < m_jumps.size()) 123 { 124 m_current += count; 125 SelectionList& res = m_jumps[m_current]; 126 res.update(); 127 context.print_status({ format("jumped to #{} ({})", 128 m_current, m_jumps.size() - 1), 129 context.faces()["Information"] }); 130 return res; 131 } 132 throw runtime_error("no next jump"); 133 } 134 135 const SelectionList& JumpList::backward(Context& context, int count) 136 { 137 const SelectionList& current = context.selections(); 138 const bool should_push_current = 139 m_current == m_jumps.size() or 140 (m_current != m_jumps.size() and m_jumps[m_current] != current); 141 if (should_push_current) 142 push(current); 143 144 const int steps = count + (should_push_current ? 1 : 0); 145 if ((int)m_current - steps < 0) 146 throw runtime_error("no previous jump"); 147 148 m_current -= steps; 149 SelectionList& res = m_jumps[m_current]; 150 res.update(); 151 context.print_status({ format("jumped to #{} ({})", 152 m_current, m_jumps.size() - 1), 153 context.faces()["Information"] }); 154 return res; 155 } 156 157 void JumpList::forget_buffer(Buffer& buffer) 158 { 159 for (size_t i = 0; i < m_jumps.size();) 160 { 161 if (&m_jumps[i].buffer() == &buffer) 162 { 163 if (i < m_current) 164 --m_current; 165 else if (i == m_current) 166 m_current = m_jumps.size()-1; 167 168 m_jumps.erase(m_jumps.begin() + i); 169 } 170 else 171 ++i; 172 } 173 } 174 175 Context::SelectionHistory::SelectionHistory(Context& context) : m_context(context) {} 176 177 Context::SelectionHistory::SelectionHistory(Context& context, SelectionList selections) 178 : m_context(context), 179 m_history{HistoryNode{std::move(selections), HistoryId::Invalid}}, 180 m_history_id(HistoryId::First) {} 181 182 void Context::SelectionHistory::initialize(SelectionList selections) 183 { 184 kak_assert(empty()); 185 m_history = {HistoryNode{std::move(selections), HistoryId::Invalid}}; 186 m_history_id = HistoryId::First; 187 } 188 189 SelectionList& Context::SelectionHistory::selections(bool update) 190 { 191 if (empty()) 192 throw runtime_error("no selections in context"); 193 auto& sels = m_staging ? m_staging->selections : current_history_node().selections; 194 if (update) 195 sels.update(); 196 return sels; 197 } 198 199 void Context::SelectionHistory::begin_edition() 200 { 201 if (not in_edition()) 202 m_staging = HistoryNode{selections(), m_history_id}; 203 m_in_edition.set(); 204 } 205 206 void Context::SelectionHistory::end_edition() 207 { 208 kak_assert(in_edition()); 209 m_in_edition.unset(); 210 if (in_edition()) 211 return; 212 213 if (m_history_id != HistoryId::Invalid and current_history_node().selections == m_staging->selections) 214 { 215 // No change, except maybe the index of the main selection. 216 // Update timestamp to potentially improve interaction with content undo. 217 auto& node = current_history_node(); 218 node.selections.force_timestamp(m_staging->selections.timestamp()); 219 node.selections.set_main_index(m_staging->selections.main_index()); 220 } 221 else 222 { 223 m_history_id = next_history_id(); 224 m_history.push_back(std::move(*m_staging)); 225 } 226 m_staging.reset(); 227 } 228 229 template<Direction direction> 230 void Context::SelectionHistory::undo() 231 { 232 static constexpr bool backward = direction == Backward; 233 if (in_edition()) 234 throw runtime_error("selection undo is only supported at top-level"); 235 kak_assert(not empty()); 236 SelectionList old_selections = selections(); 237 HistoryId next; 238 do 239 { 240 if constexpr (backward) 241 next = current_history_node().parent; 242 else 243 next = current_history_node().redo_child; 244 if (next == HistoryId::Invalid) 245 throw runtime_error(backward ? "no selection change to undo" : "no selection change to redo"); 246 auto select_next = [&, next] { 247 HistoryId previous_id = m_history_id; 248 m_history_id = next; 249 if constexpr (backward) 250 current_history_node().redo_child = previous_id; 251 }; 252 Buffer& destination_buffer = history_node(next).selections.buffer(); 253 if (&destination_buffer == &m_context.buffer()) 254 select_next(); 255 else 256 m_context.change_buffer(destination_buffer, { std::move(select_next) }); 257 } 258 while (selections() == old_selections); 259 } 260 261 void Context::SelectionHistory::forget_buffer(Buffer& buffer) 262 { 263 Vector<HistoryId, MemoryDomain::Selections> new_ids; 264 size_t bias = 0; 265 for (size_t i = 0; i < m_history.size(); ++i) 266 { 267 auto& node = history_node((HistoryId)i); 268 HistoryId id; 269 if (&node.selections.buffer() == &buffer) 270 { 271 id = HistoryId::Invalid; 272 ++bias; 273 } 274 else 275 id = (HistoryId)(i - bias); 276 new_ids.push_back(id); 277 } 278 auto new_id = [&new_ids](HistoryId old_id) -> HistoryId { 279 return old_id == HistoryId::Invalid ? HistoryId::Invalid : new_ids[(size_t)old_id]; 280 }; 281 282 m_history.erase(remove_if(m_history, [&buffer](const auto& node) { 283 return &node.selections.buffer() == &buffer; 284 }), m_history.end()); 285 286 for (auto& node : m_history) 287 { 288 node.parent = new_id(node.parent); 289 node.redo_child = new_id(node.redo_child); 290 } 291 m_history_id = new_id(m_history_id); 292 if (m_staging) 293 { 294 m_staging->parent = new_id(m_staging->parent); 295 kak_assert(m_staging->redo_child == HistoryId::Invalid); 296 } 297 kak_assert(m_history_id != HistoryId::Invalid or m_staging); 298 } 299 300 void Context::change_buffer(Buffer& buffer, Optional<FunctionRef<void()>> set_selections) 301 { 302 if (has_buffer() and &buffer == &this->buffer()) 303 return; 304 305 if (has_buffer() and m_edition_level > 0) 306 this->buffer().commit_undo_group(); 307 308 if (has_client() and &client().context() == this) // Hook contexts may set the client but not be the client's context 309 { 310 client().info_hide(); 311 client().menu_hide(); 312 client().change_buffer(buffer, std::move(set_selections)); 313 } 314 else 315 { 316 m_window.reset(); 317 if (m_selection_history.empty()) 318 m_selection_history.initialize(SelectionList{buffer, Selection{}}); 319 else 320 { 321 ScopedSelectionEdition selection_edition{*this}; 322 selections_write_only() = SelectionList{buffer, Selection{}}; 323 } 324 if (not m_local_scopes.empty()) 325 m_local_scopes.front()->reparent(buffer); 326 } 327 328 if (has_input_handler()) 329 input_handler().reset_normal_mode(); 330 } 331 332 void Context::forget_buffer(Buffer& buffer) 333 { 334 m_jump_list.forget_buffer(buffer); 335 336 if (&this->buffer() == &buffer) 337 { 338 if (is_editing() and has_input_handler()) 339 input_handler().reset_normal_mode(); 340 341 auto last_buffer = this->last_buffer(); 342 change_buffer(last_buffer ? *last_buffer : BufferManager::instance().get_first_buffer()); 343 } 344 345 m_selection_history.forget_buffer(buffer); 346 } 347 348 Buffer* Context::last_buffer() const 349 { 350 const auto jump_list = m_jump_list.get_as_list(); 351 if (jump_list.empty()) 352 return nullptr; 353 354 auto predicate = [this](const auto& sels) { 355 return &sels.buffer() != &this->buffer(); 356 }; 357 358 auto next_buffer = find_if(jump_list.subrange(m_jump_list.current_index()-1), 359 predicate); 360 if (next_buffer != jump_list.end()) 361 return &next_buffer->buffer(); 362 363 auto previous_buffer = find_if(jump_list.subrange(0, m_jump_list.current_index()) | reverse(), 364 predicate); 365 366 return previous_buffer != jump_list.rend() ? &previous_buffer->buffer() : nullptr; 367 } 368 369 SelectionList& Context::selections(bool update) 370 { 371 return m_selection_history.selections(update); 372 } 373 374 template<Direction direction> 375 void Context::undo_selection_change() 376 { 377 m_selection_history.undo<direction>(); 378 } 379 template void Context::undo_selection_change<Backward>(); 380 template void Context::undo_selection_change<Forward>(); 381 382 SelectionList& Context::selections_write_only() 383 { 384 return selections(false); 385 } 386 387 const SelectionList& Context::selections(bool update) const 388 { 389 return const_cast<Context&>(*this).selections(update); 390 } 391 392 Vector<String> Context::selections_content() const 393 { 394 auto& buf = buffer(); 395 Vector<String> contents; 396 for (auto& sel : selections()) 397 contents.push_back(buf.string(sel.min(), buf.char_next(sel.max()))); 398 return contents; 399 } 400 401 void Context::begin_edition() 402 { 403 if (m_edition_level >= 0) 404 { 405 if (m_edition_level == 0) 406 m_edition_timestamp = buffer().timestamp(); 407 ++m_edition_level; 408 } 409 } 410 411 void Context::end_edition() 412 { 413 if (m_edition_level < 0) 414 return; 415 416 kak_assert(m_edition_level != 0); 417 if (m_edition_level == 1 and 418 buffer().timestamp() != m_edition_timestamp) 419 buffer().commit_undo_group(); 420 421 --m_edition_level; 422 } 423 424 StringView Context::main_sel_register_value(StringView reg) const 425 { 426 size_t index = has_buffer() ? selections(false).main_index() : 0; 427 return RegisterManager::instance()[reg].get_main(*this, index); 428 } 429 430 void Context::set_name(String name) { 431 String old_name = std::exchange(m_name, std::move(name)); 432 hooks().run_hook(Hook::ClientRenamed, format("{}:{}", old_name, m_name), *this); 433 } 434 435 ScopedEdition::ScopedEdition(Context& context) 436 : m_context{context}, 437 m_buffer{context.has_buffer() ? &context.buffer() : nullptr} 438 { if (m_buffer) m_context.begin_edition(); } 439 440 ScopedEdition::~ScopedEdition() { if (m_buffer) m_context.end_edition(); } 441 442 ScopedSelectionEdition::ScopedSelectionEdition(Context& context) 443 : m_context{context}, 444 m_valid{not (m_context.flags() & Context::Flags::Draft) and context.has_buffer()} 445 { if (m_valid) m_context.m_selection_history.begin_edition(); } 446 447 ScopedSelectionEdition::ScopedSelectionEdition(ScopedSelectionEdition&& other) : m_context{other.m_context}, m_valid{other.m_valid} 448 { other.m_valid = false; } 449 450 ScopedSelectionEdition::~ScopedSelectionEdition() { if (m_valid) m_context.m_selection_history.end_edition(); } 451 452 }