Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2021, the SerenityOS developers.
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include "Editor.h"
9#include <AK/CharacterTypes.h>
10#include <AK/Debug.h>
11#include <AK/GenericLexer.h>
12#include <AK/JsonObject.h>
13#include <AK/MemoryStream.h>
14#include <AK/RedBlackTree.h>
15#include <AK/ScopeGuard.h>
16#include <AK/ScopedValueRollback.h>
17#include <AK/StringBuilder.h>
18#include <AK/Utf32View.h>
19#include <AK/Utf8View.h>
20#include <LibCore/ConfigFile.h>
21#include <LibCore/DeprecatedFile.h>
22#include <LibCore/Event.h>
23#include <LibCore/EventLoop.h>
24#include <LibCore/Notifier.h>
25#include <errno.h>
26#include <fcntl.h>
27#include <signal.h>
28#include <stdio.h>
29#include <sys/ioctl.h>
30#include <sys/select.h>
31#include <sys/time.h>
32#include <unistd.h>
33
34namespace {
35constexpr u32 ctrl(char c) { return c & 0x3f; }
36}
37
38namespace Line {
39
40Configuration Configuration::from_config(StringView libname)
41{
42 Configuration configuration;
43 auto config_file = Core::ConfigFile::open_for_lib(libname).release_value_but_fixme_should_propagate_errors();
44
45 // Read behavior options.
46 auto refresh = config_file->read_entry("behavior", "refresh", "lazy");
47 auto operation = config_file->read_entry("behavior", "operation_mode");
48 auto bracketed_paste = config_file->read_bool_entry("behavior", "bracketed_paste", true);
49 auto default_text_editor = config_file->read_entry("behavior", "default_text_editor");
50
51 Configuration::Flags flags { Configuration::Flags::None };
52 if (bracketed_paste)
53 flags = static_cast<Flags>(flags | Configuration::Flags::BracketedPaste);
54
55 configuration.set(flags);
56
57 if (refresh.equals_ignoring_ascii_case("lazy"sv))
58 configuration.set(Configuration::Lazy);
59 else if (refresh.equals_ignoring_ascii_case("eager"sv))
60 configuration.set(Configuration::Eager);
61
62 if (operation.equals_ignoring_ascii_case("full"sv))
63 configuration.set(Configuration::OperationMode::Full);
64 else if (operation.equals_ignoring_ascii_case("noescapesequences"sv))
65 configuration.set(Configuration::OperationMode::NoEscapeSequences);
66 else if (operation.equals_ignoring_ascii_case("noninteractive"sv))
67 configuration.set(Configuration::OperationMode::NonInteractive);
68 else
69 configuration.set(Configuration::OperationMode::Unset);
70
71 if (!default_text_editor.is_empty())
72 configuration.set(DefaultTextEditor { move(default_text_editor) });
73 else
74 configuration.set(DefaultTextEditor { "/bin/TextEditor" });
75
76 // Read keybinds.
77
78 for (auto& binding_key : config_file->keys("keybinds")) {
79 GenericLexer key_lexer(binding_key);
80 auto has_ctrl = false;
81 auto alt = false;
82 auto escape = false;
83 Vector<Key> keys;
84
85 while (!key_lexer.is_eof()) {
86 unsigned key;
87 if (escape) {
88 key = key_lexer.consume_escaped_character();
89 escape = false;
90 } else {
91 if (key_lexer.next_is("alt+")) {
92 alt = key_lexer.consume_specific("alt+");
93 continue;
94 }
95 if (key_lexer.next_is("^[")) {
96 alt = key_lexer.consume_specific("^[");
97 continue;
98 }
99 if (key_lexer.next_is("^")) {
100 has_ctrl = key_lexer.consume_specific("^");
101 continue;
102 }
103 if (key_lexer.next_is("ctrl+")) {
104 has_ctrl = key_lexer.consume_specific("ctrl+");
105 continue;
106 }
107 if (key_lexer.next_is("\\")) {
108 escape = true;
109 continue;
110 }
111 // FIXME: Support utf?
112 key = key_lexer.consume();
113 }
114 if (has_ctrl)
115 key = ctrl(key);
116
117 keys.append(Key { key, alt ? Key::Alt : Key::None });
118 alt = false;
119 has_ctrl = false;
120 }
121
122 GenericLexer value_lexer { config_file->read_entry("keybinds", binding_key) };
123 StringBuilder value_builder;
124 while (!value_lexer.is_eof())
125 value_builder.append(value_lexer.consume_escaped_character());
126 auto value = value_builder.string_view();
127 if (value.starts_with("internal:"sv)) {
128 configuration.set(KeyBinding {
129 keys,
130 KeyBinding::Kind::InternalFunction,
131 value.substring_view(9, value.length() - 9) });
132 } else {
133 configuration.set(KeyBinding {
134 keys,
135 KeyBinding::Kind::Insertion,
136 value });
137 }
138 }
139
140 return configuration;
141}
142
143void Editor::set_default_keybinds()
144{
145 register_key_input_callback(ctrl('N'), EDITOR_INTERNAL_FUNCTION(search_forwards));
146 register_key_input_callback(ctrl('P'), EDITOR_INTERNAL_FUNCTION(search_backwards));
147 register_key_input_callback(ctrl('A'), EDITOR_INTERNAL_FUNCTION(go_home));
148 register_key_input_callback(ctrl('B'), EDITOR_INTERNAL_FUNCTION(cursor_left_character));
149 register_key_input_callback(ctrl('D'), EDITOR_INTERNAL_FUNCTION(erase_character_forwards));
150 register_key_input_callback(ctrl('E'), EDITOR_INTERNAL_FUNCTION(go_end));
151 register_key_input_callback(ctrl('F'), EDITOR_INTERNAL_FUNCTION(cursor_right_character));
152 // ^H: ctrl('H') == '\b'
153 register_key_input_callback(ctrl('H'), EDITOR_INTERNAL_FUNCTION(erase_character_backwards));
154 // DEL - Some terminals send this instead of ^H.
155 register_key_input_callback((char)127, EDITOR_INTERNAL_FUNCTION(erase_character_backwards));
156 register_key_input_callback(ctrl('K'), EDITOR_INTERNAL_FUNCTION(erase_to_end));
157 register_key_input_callback(ctrl('L'), EDITOR_INTERNAL_FUNCTION(clear_screen));
158 register_key_input_callback(ctrl('R'), EDITOR_INTERNAL_FUNCTION(enter_search));
159 register_key_input_callback(ctrl('T'), EDITOR_INTERNAL_FUNCTION(transpose_characters));
160 register_key_input_callback('\n', EDITOR_INTERNAL_FUNCTION(finish));
161
162 // ^X^E: Edit in external editor
163 register_key_input_callback(Vector<Key> { ctrl('X'), ctrl('E') }, EDITOR_INTERNAL_FUNCTION(edit_in_external_editor));
164
165 // ^[.: alt-.: insert last arg of previous command (similar to `!$`)
166 register_key_input_callback(Key { '.', Key::Alt }, EDITOR_INTERNAL_FUNCTION(insert_last_words));
167 register_key_input_callback(Key { 'b', Key::Alt }, EDITOR_INTERNAL_FUNCTION(cursor_left_word));
168 register_key_input_callback(Key { 'f', Key::Alt }, EDITOR_INTERNAL_FUNCTION(cursor_right_word));
169 // ^[^H: alt-backspace: backward delete word
170 register_key_input_callback(Key { '\b', Key::Alt }, EDITOR_INTERNAL_FUNCTION(erase_alnum_word_backwards));
171 register_key_input_callback(Key { 'd', Key::Alt }, EDITOR_INTERNAL_FUNCTION(erase_alnum_word_forwards));
172 register_key_input_callback(Key { 'c', Key::Alt }, EDITOR_INTERNAL_FUNCTION(capitalize_word));
173 register_key_input_callback(Key { 'l', Key::Alt }, EDITOR_INTERNAL_FUNCTION(lowercase_word));
174 register_key_input_callback(Key { 'u', Key::Alt }, EDITOR_INTERNAL_FUNCTION(uppercase_word));
175 register_key_input_callback(Key { 't', Key::Alt }, EDITOR_INTERNAL_FUNCTION(transpose_words));
176
177 // Register these last to all the user to override the previous key bindings
178 // Normally ^W. `stty werase \^n` can change it to ^N (or something else).
179 register_key_input_callback(m_termios.c_cc[VWERASE], EDITOR_INTERNAL_FUNCTION(erase_word_backwards));
180 // Normally ^U. `stty kill \^n` can change it to ^N (or something else).
181 register_key_input_callback(m_termios.c_cc[VKILL], EDITOR_INTERNAL_FUNCTION(kill_line));
182 register_key_input_callback(m_termios.c_cc[VERASE], EDITOR_INTERNAL_FUNCTION(erase_character_backwards));
183}
184
185Editor::Editor(Configuration configuration)
186 : m_configuration(move(configuration))
187{
188 m_always_refresh = m_configuration.refresh_behavior == Configuration::RefreshBehavior::Eager;
189 m_pending_chars = {};
190 get_terminal_size();
191 m_suggestion_display = make<XtermSuggestionDisplay>(m_num_lines, m_num_columns);
192}
193
194Editor::~Editor()
195{
196 if (m_initialized)
197 restore();
198}
199
200void Editor::ensure_free_lines_from_origin(size_t count)
201{
202 if (count > m_num_lines) {
203 // FIXME: Implement paging
204 }
205
206 if (m_origin_row + count <= m_num_lines)
207 return;
208
209 auto diff = m_origin_row + count - m_num_lines - 1;
210 out(stderr, "\x1b[{}S", diff);
211 fflush(stderr);
212 m_origin_row -= diff;
213 m_refresh_needed = false;
214 m_chars_touched_in_the_middle = 0;
215}
216
217void Editor::get_terminal_size()
218{
219 struct winsize ws;
220 ioctl(STDERR_FILENO, TIOCGWINSZ, &ws);
221 if (ws.ws_col == 0 || ws.ws_row == 0) {
222 // LLDB uses ttys which "work" and then gives us a zero sized
223 // terminal which is far from useful
224 if (int fd = open("/dev/tty", O_RDONLY); fd != -1) {
225 ioctl(fd, TIOCGWINSZ, &ws);
226 close(fd);
227 }
228 }
229 m_num_columns = ws.ws_col;
230 m_num_lines = ws.ws_row;
231}
232
233void Editor::add_to_history(DeprecatedString const& line)
234{
235 if (line.is_empty())
236 return;
237 DeprecatedString histcontrol = getenv("HISTCONTROL");
238 auto ignoredups = histcontrol == "ignoredups" || histcontrol == "ignoreboth";
239 auto ignorespace = histcontrol == "ignorespace" || histcontrol == "ignoreboth";
240 if (ignoredups && !m_history.is_empty() && line == m_history.last().entry)
241 return;
242 if (ignorespace && line.starts_with(' '))
243 return;
244 if ((m_history.size() + 1) > m_history_capacity)
245 m_history.take_first();
246 struct timeval tv;
247 gettimeofday(&tv, nullptr);
248 m_history.append({ line, tv.tv_sec });
249 m_history_dirty = true;
250}
251
252bool Editor::load_history(DeprecatedString const& path)
253{
254 auto history_file = Core::DeprecatedFile::construct(path);
255 if (!history_file->open(Core::OpenMode::ReadOnly))
256 return false;
257 auto data = history_file->read_all();
258 auto hist = StringView { data.data(), data.size() };
259 for (auto& str : hist.split_view("\n\n"sv)) {
260 auto it = str.find("::"sv).value_or(0);
261 auto time = str.substring_view(0, it).to_int<time_t>().value_or(0);
262 auto string = str.substring_view(it == 0 ? it : it + 2);
263 m_history.append({ string, time });
264 }
265 return true;
266}
267
268template<typename It0, typename It1, typename OutputT, typename MapperT, typename LessThan>
269static void merge(It0&& begin0, It0 const& end0, It1&& begin1, It1 const& end1, OutputT& output, MapperT left_mapper, LessThan less_than)
270{
271 for (;;) {
272 if (begin0 == end0 && begin1 == end1)
273 return;
274
275 if (begin0 == end0) {
276 auto&& right = *begin1;
277 if (output.last().entry != right.entry)
278 output.append(right);
279 ++begin1;
280 continue;
281 }
282
283 auto&& left = left_mapper(*begin0);
284 if (left.entry.is_whitespace()) {
285 ++begin0;
286 continue;
287 }
288 if (begin1 == end1) {
289 if (output.last().entry != left.entry)
290 output.append(left);
291 ++begin0;
292 continue;
293 }
294
295 auto&& right = *begin1;
296 if (less_than(left, right)) {
297 if (output.last().entry != left.entry)
298 output.append(left);
299 ++begin0;
300 } else {
301 if (output.last().entry != right.entry)
302 output.append(right);
303 ++begin1;
304 if (right.entry == left.entry)
305 ++begin0;
306 }
307 }
308}
309
310bool Editor::save_history(DeprecatedString const& path)
311{
312 Vector<HistoryEntry> final_history { { "", 0 } };
313 {
314 auto file_or_error = Core::DeprecatedFile::open(path, Core::OpenMode::ReadWrite, 0600);
315 if (file_or_error.is_error())
316 return false;
317 auto file = file_or_error.release_value();
318 merge(
319 file->line_begin(), file->line_end(), m_history.begin(), m_history.end(), final_history,
320 [](StringView str) {
321 auto it = str.find("::"sv).value_or(0);
322 auto time = str.substring_view(0, it).to_int<time_t>().value_or(0);
323 auto string = str.substring_view(it == 0 ? it : it + 2);
324 return HistoryEntry { string, time };
325 },
326 [](HistoryEntry const& left, HistoryEntry const& right) { return left.timestamp < right.timestamp; });
327 }
328
329 auto file_or_error = Core::DeprecatedFile::open(path, Core::OpenMode::WriteOnly, 0600);
330 if (file_or_error.is_error())
331 return false;
332 auto file = file_or_error.release_value();
333 final_history.take_first();
334 for (auto const& entry : final_history)
335 file->write(DeprecatedString::formatted("{}::{}\n\n", entry.timestamp, entry.entry));
336
337 m_history_dirty = false;
338 return true;
339}
340
341void Editor::clear_line()
342{
343 for (size_t i = 0; i < m_cursor; ++i)
344 fputc(0x8, stderr);
345 fputs("\033[K", stderr);
346 fflush(stderr);
347 m_chars_touched_in_the_middle = buffer().size();
348 m_buffer.clear();
349 m_cursor = 0;
350 m_inline_search_cursor = m_cursor;
351}
352
353void Editor::insert(Utf32View const& string)
354{
355 for (size_t i = 0; i < string.length(); ++i)
356 insert(string.code_points()[i]);
357}
358
359void Editor::insert(DeprecatedString const& string)
360{
361 for (auto ch : Utf8View { string })
362 insert(ch);
363}
364
365void Editor::insert(StringView string_view)
366{
367 for (auto ch : Utf8View { string_view })
368 insert(ch);
369}
370
371void Editor::insert(const u32 cp)
372{
373 StringBuilder builder;
374 builder.append(Utf32View(&cp, 1));
375 auto str = builder.to_deprecated_string();
376 if (m_pending_chars.try_append(str.characters(), str.length()).is_error())
377 return;
378
379 readjust_anchored_styles(m_cursor, ModificationKind::Insertion);
380
381 if (m_cursor == m_buffer.size()) {
382 m_buffer.append(cp);
383 m_cursor = m_buffer.size();
384 m_inline_search_cursor = m_cursor;
385 return;
386 }
387
388 m_buffer.insert(m_cursor, cp);
389 ++m_chars_touched_in_the_middle;
390 ++m_cursor;
391 m_inline_search_cursor = m_cursor;
392}
393
394void Editor::register_key_input_callback(KeyBinding const& binding)
395{
396 if (binding.kind == KeyBinding::Kind::InternalFunction) {
397 auto internal_function = find_internal_function(binding.binding);
398 if (!internal_function) {
399 dbgln("LibLine: Unknown internal function '{}'", binding.binding);
400 return;
401 }
402 return register_key_input_callback(binding.keys, move(internal_function));
403 }
404
405 return register_key_input_callback(binding.keys, [binding = DeprecatedString(binding.binding)](auto& editor) {
406 editor.insert(binding);
407 return false;
408 });
409}
410
411static size_t code_point_length_in_utf8(u32 code_point)
412{
413 if (code_point <= 0x7f)
414 return 1;
415 if (code_point <= 0x07ff)
416 return 2;
417 if (code_point <= 0xffff)
418 return 3;
419 if (code_point <= 0x10ffff)
420 return 4;
421 return 3;
422}
423
424// buffer [ 0 1 2 3 . . . A . . . B . . . M . . . N ]
425// ^ ^ ^ ^
426// | | | +- end of buffer
427// | | +- scan offset = M
428// | +- range end = M - B
429// +- range start = M - A
430// This method converts a byte range defined by [start_byte_offset, end_byte_offset] to a code_point range [M - A, M - B] as shown in the diagram above.
431// If `reverse' is true, A and B are before M, if not, A and B are after M.
432Editor::CodepointRange Editor::byte_offset_range_to_code_point_offset_range(size_t start_byte_offset, size_t end_byte_offset, size_t scan_code_point_offset, bool reverse) const
433{
434 size_t byte_offset = 0;
435 size_t code_point_offset = scan_code_point_offset + (reverse ? 1 : 0);
436 CodepointRange range;
437
438 for (;;) {
439 if (!reverse) {
440 if (code_point_offset >= m_buffer.size())
441 break;
442 } else {
443 if (code_point_offset == 0)
444 break;
445 }
446
447 if (byte_offset > end_byte_offset)
448 break;
449
450 if (byte_offset < start_byte_offset)
451 ++range.start;
452
453 if (byte_offset < end_byte_offset)
454 ++range.end;
455
456 byte_offset += code_point_length_in_utf8(m_buffer[reverse ? --code_point_offset : code_point_offset++]);
457 }
458
459 return range;
460}
461
462void Editor::stylize(Span const& span, Style const& style)
463{
464 if (!span.is_empty())
465 return;
466 if (style.is_empty())
467 return;
468
469 auto start = span.beginning();
470 auto end = span.end();
471
472 if (span.mode() == Span::ByteOriented) {
473 auto offsets = byte_offset_range_to_code_point_offset_range(start, end, 0);
474
475 start = offsets.start;
476 end = offsets.end;
477 }
478
479 if (auto maybe_mask = style.mask(); maybe_mask.has_value()) {
480 auto it = m_current_masks.find_smallest_not_below_iterator(span.beginning());
481 Optional<Style::Mask> last_encountered_entry;
482 if (!it.is_end()) {
483 // Delete all overlapping old masks.
484 while (true) {
485 auto next_it = m_current_masks.find_largest_not_above_iterator(span.end());
486 if (next_it.is_end())
487 break;
488 if (it->has_value())
489 last_encountered_entry = *it;
490 m_current_masks.remove(next_it.key());
491 }
492 }
493 m_current_masks.insert(span.beginning(), move(maybe_mask));
494 m_current_masks.insert(span.end(), {});
495 if (last_encountered_entry.has_value())
496 m_current_masks.insert(span.end() + 1, move(last_encountered_entry));
497 style.unset_mask();
498 }
499
500 auto& spans_starting = style.is_anchored() ? m_current_spans.m_anchored_spans_starting : m_current_spans.m_spans_starting;
501 auto& spans_ending = style.is_anchored() ? m_current_spans.m_anchored_spans_ending : m_current_spans.m_spans_ending;
502
503 auto& starting_map = spans_starting.ensure(start);
504 if (!starting_map.contains(end))
505 m_refresh_needed = true;
506 starting_map.set(end, style);
507
508 auto& ending_map = spans_ending.ensure(end);
509 if (!ending_map.contains(start))
510 m_refresh_needed = true;
511 ending_map.set(start, style);
512}
513
514void Editor::transform_suggestion_offsets(size_t& invariant_offset, size_t& static_offset, Span::Mode offset_mode) const
515{
516 auto internal_static_offset = static_offset;
517 auto internal_invariant_offset = invariant_offset;
518 if (offset_mode == Span::Mode::ByteOriented) {
519 // FIXME: We're assuming that invariant_offset points to the end of the available data
520 // this is not necessarily true, but is true in most cases.
521 auto offsets = byte_offset_range_to_code_point_offset_range(internal_static_offset, internal_invariant_offset + internal_static_offset, m_cursor - 1, true);
522
523 internal_static_offset = offsets.start;
524 internal_invariant_offset = offsets.end - offsets.start;
525 }
526 invariant_offset = internal_invariant_offset;
527 static_offset = internal_static_offset;
528}
529
530void Editor::initialize()
531{
532 if (m_initialized)
533 return;
534
535 struct termios termios;
536 tcgetattr(0, &termios);
537 m_default_termios = termios; // grab a copy to restore
538
539 get_terminal_size();
540
541 if (m_configuration.operation_mode == Configuration::Unset) {
542 auto istty = isatty(STDIN_FILENO) && isatty(STDERR_FILENO);
543 if (!istty) {
544 m_configuration.set(Configuration::NonInteractive);
545 } else {
546 auto* term = getenv("TERM");
547 if (term != NULL && StringView { term, strlen(term) }.starts_with("xterm"sv))
548 m_configuration.set(Configuration::Full);
549 else
550 m_configuration.set(Configuration::NoEscapeSequences);
551 }
552 }
553
554 // Because we use our own line discipline which includes echoing,
555 // we disable ICANON and ECHO.
556 if (m_configuration.operation_mode == Configuration::Full) {
557 termios.c_lflag &= ~(ECHO | ICANON);
558 tcsetattr(0, TCSANOW, &termios);
559 }
560
561 m_termios = termios;
562
563 set_default_keybinds();
564 for (auto& keybind : m_configuration.keybindings)
565 register_key_input_callback(keybind);
566
567 if (m_configuration.m_signal_mode == Configuration::WithSignalHandlers) {
568 m_signal_handlers.append(Core::EventLoop::register_signal(SIGINT, [this](int) {
569 interrupted().release_value_but_fixme_should_propagate_errors();
570 }));
571
572 m_signal_handlers.append(Core::EventLoop::register_signal(SIGWINCH, [this](int) {
573 resized().release_value_but_fixme_should_propagate_errors();
574 }));
575 }
576
577 m_initialized = true;
578}
579
580void Editor::refetch_default_termios()
581{
582 struct termios termios;
583 tcgetattr(0, &termios);
584 m_default_termios = termios;
585 if (m_configuration.operation_mode == Configuration::Full)
586 termios.c_lflag &= ~(ECHO | ICANON);
587 m_termios = termios;
588}
589
590ErrorOr<void> Editor::interrupted()
591{
592 if (m_is_searching)
593 return m_search_editor->interrupted();
594
595 if (!m_is_editing)
596 return {};
597
598 m_was_interrupted = true;
599 handle_interrupt_event();
600 if (!m_finish || !m_previous_interrupt_was_handled_as_interrupt)
601 return {};
602
603 m_finish = false;
604 {
605 auto stderr_stream = TRY(Core::File::standard_error());
606 TRY(reposition_cursor(*stderr_stream, true));
607 if (TRY(m_suggestion_display->cleanup()))
608 TRY(reposition_cursor(*stderr_stream, true));
609 TRY(stderr_stream->write_until_depleted("\n"sv.bytes()));
610 }
611 m_buffer.clear();
612 m_chars_touched_in_the_middle = buffer().size();
613 m_is_editing = false;
614 restore();
615 m_notifier->set_enabled(false);
616 m_notifier = nullptr;
617 Core::EventLoop::current().quit(Retry);
618 return {};
619}
620
621ErrorOr<void> Editor::resized()
622{
623 m_was_resized = true;
624 m_previous_num_columns = m_num_columns;
625 get_terminal_size();
626
627 if (!m_has_origin_reset_scheduled) {
628 // Reset the origin, but make sure it doesn't blow up if we can't read it
629 if (set_origin(false)) {
630 TRY(handle_resize_event(false));
631 } else {
632 deferred_invoke([this] { handle_resize_event(true).release_value_but_fixme_should_propagate_errors(); });
633 m_has_origin_reset_scheduled = true;
634 }
635 }
636
637 return {};
638}
639
640ErrorOr<void> Editor::handle_resize_event(bool reset_origin)
641{
642 m_has_origin_reset_scheduled = false;
643 if (reset_origin && !set_origin(false)) {
644 m_has_origin_reset_scheduled = true;
645 deferred_invoke([this] { handle_resize_event(true).release_value_but_fixme_should_propagate_errors(); });
646 return {};
647 }
648
649 set_origin(m_origin_row, 1);
650
651 auto stderr_stream = TRY(Core::File::standard_error());
652
653 TRY(reposition_cursor(*stderr_stream, true));
654 TRY(m_suggestion_display->redisplay(m_suggestion_manager, m_num_lines, m_num_columns));
655 m_origin_row = m_suggestion_display->origin_row();
656 TRY(reposition_cursor(*stderr_stream));
657
658 if (m_is_searching)
659 TRY(m_search_editor->resized());
660
661 return {};
662}
663
664ErrorOr<void> Editor::really_quit_event_loop()
665{
666 m_finish = false;
667 {
668 auto stderr_stream = TRY(Core::File::standard_error());
669 TRY(reposition_cursor(*stderr_stream, true));
670 TRY(stderr_stream->write_until_depleted("\n"sv.bytes()));
671 }
672 auto string = line();
673 m_buffer.clear();
674 m_chars_touched_in_the_middle = buffer().size();
675 m_is_editing = false;
676
677 if (m_initialized)
678 restore();
679
680 m_returned_line = string;
681 m_notifier->set_enabled(false);
682 m_notifier = nullptr;
683 Core::EventLoop::current().quit(Exit);
684 return {};
685}
686
687auto Editor::get_line(DeprecatedString const& prompt) -> Result<DeprecatedString, Editor::Error>
688{
689 initialize();
690 m_is_editing = true;
691
692 if (m_configuration.operation_mode == Configuration::NoEscapeSequences || m_configuration.operation_mode == Configuration::NonInteractive) {
693 // Do not use escape sequences, instead, use LibC's getline.
694 size_t size = 0;
695 char* line = nullptr;
696 // Show the prompt only on interactive mode (NoEscapeSequences in this case).
697 if (m_configuration.operation_mode != Configuration::NonInteractive)
698 fputs(prompt.characters(), stderr);
699 auto line_length = getline(&line, &size, stdin);
700 // getline() returns -1 and sets errno=0 on EOF.
701 if (line_length == -1) {
702 if (line)
703 free(line);
704 if (errno == 0)
705 return Error::Eof;
706
707 return Error::ReadFailure;
708 }
709 restore();
710 if (line) {
711 DeprecatedString result { line, (size_t)line_length, Chomp };
712 free(line);
713 return result;
714 }
715
716 return Error::ReadFailure;
717 }
718
719 auto old_cols = m_num_columns;
720 auto old_lines = m_num_lines;
721 get_terminal_size();
722
723 if (m_configuration.enable_bracketed_paste)
724 fprintf(stderr, "\x1b[?2004h");
725
726 if (m_num_columns != old_cols || m_num_lines != old_lines)
727 m_refresh_needed = true;
728
729 set_prompt(prompt);
730 reset();
731 strip_styles(true);
732
733 {
734 auto stderr_stream = Core::File::standard_error().release_value_but_fixme_should_propagate_errors();
735 auto prompt_lines = max(current_prompt_metrics().line_metrics.size(), 1ul) - 1;
736 for (size_t i = 0; i < prompt_lines; ++i)
737 stderr_stream->write_until_depleted("\n"sv.bytes()).release_value_but_fixme_should_propagate_errors();
738
739 VT::move_relative(-static_cast<int>(prompt_lines), 0, *stderr_stream).release_value_but_fixme_should_propagate_errors();
740 }
741
742 set_origin();
743
744 m_history_cursor = m_history.size();
745
746 refresh_display().release_value_but_fixme_should_propagate_errors();
747
748 Core::EventLoop loop;
749
750 m_notifier = Core::Notifier::construct(STDIN_FILENO, Core::Notifier::Read);
751
752 m_notifier->on_ready_to_read = [&] {
753 if (try_update_once().is_error())
754 loop.quit(Exit);
755 };
756
757 if (!m_incomplete_data.is_empty()) {
758 deferred_invoke([&] {
759 if (try_update_once().is_error())
760 loop.quit(Exit);
761 });
762 }
763
764 if (loop.exec() == Retry)
765 return get_line(prompt);
766
767 return m_input_error.has_value() ? Result<DeprecatedString, Editor::Error> { m_input_error.value() } : Result<DeprecatedString, Editor::Error> { m_returned_line };
768}
769
770void Editor::save_to(JsonObject& object)
771{
772 Core::Object::save_to(object);
773 object.set("is_searching", m_is_searching);
774 object.set("is_editing", m_is_editing);
775 object.set("cursor_offset", m_cursor);
776 object.set("needs_refresh", m_refresh_needed);
777 object.set("unprocessed_characters", m_incomplete_data.size());
778 object.set("history_size", m_history.size());
779 object.set("current_prompt", m_new_prompt);
780 object.set("was_interrupted", m_was_interrupted);
781 JsonObject display_area;
782 display_area.set("top_left_row", m_origin_row);
783 display_area.set("top_left_column", m_origin_column);
784 display_area.set("line_count", num_lines());
785 object.set("used_display_area", move(display_area));
786}
787
788ErrorOr<void> Editor::try_update_once()
789{
790 if (m_was_interrupted) {
791 handle_interrupt_event();
792 }
793
794 TRY(handle_read_event());
795
796 if (m_always_refresh)
797 m_refresh_needed = true;
798
799 TRY(refresh_display());
800
801 if (m_finish)
802 TRY(really_quit_event_loop());
803
804 return {};
805}
806
807void Editor::handle_interrupt_event()
808{
809 m_was_interrupted = false;
810 m_previous_interrupt_was_handled_as_interrupt = false;
811
812 m_callback_machine.interrupted(*this);
813 if (!m_callback_machine.should_process_last_pressed_key())
814 return;
815
816 m_previous_interrupt_was_handled_as_interrupt = true;
817
818 fprintf(stderr, "^C");
819 fflush(stderr);
820
821 if (on_interrupt_handled)
822 on_interrupt_handled();
823
824 m_buffer.clear();
825 m_chars_touched_in_the_middle = buffer().size();
826 m_cursor = 0;
827
828 finish();
829}
830
831ErrorOr<void> Editor::handle_read_event()
832{
833 if (m_prohibit_input_processing) {
834 m_have_unprocessed_read_event = true;
835 return {};
836 }
837
838 auto prohibit_scope = prohibit_input();
839
840 char keybuf[16];
841 ssize_t nread = 0;
842
843 if (!m_incomplete_data.size())
844 nread = read(0, keybuf, sizeof(keybuf));
845
846 if (nread < 0) {
847 if (errno == EINTR) {
848 if (!m_was_interrupted) {
849 if (m_was_resized)
850 return {};
851
852 finish();
853 return {};
854 }
855
856 handle_interrupt_event();
857 return {};
858 }
859
860 ScopedValueRollback errno_restorer(errno);
861 perror("read failed");
862
863 m_input_error = Error::ReadFailure;
864 finish();
865 return {};
866 }
867
868 m_incomplete_data.append(keybuf, nread);
869 auto available_bytes = m_incomplete_data.size();
870
871 if (available_bytes == 0) {
872 m_input_error = Error::Empty;
873 finish();
874 return {};
875 }
876
877 auto reverse_tab = false;
878
879 // Discard starting bytes until they make sense as utf-8.
880 size_t valid_bytes = 0;
881 while (available_bytes > 0) {
882 Utf8View { StringView { m_incomplete_data.data(), available_bytes } }.validate(valid_bytes);
883 if (valid_bytes != 0)
884 break;
885 m_incomplete_data.take_first();
886 --available_bytes;
887 }
888
889 Utf8View input_view { StringView { m_incomplete_data.data(), valid_bytes } };
890 size_t consumed_code_points = 0;
891
892 static Vector<u8, 4> csi_parameter_bytes;
893 static Vector<u8> csi_intermediate_bytes;
894 Vector<unsigned, 4> csi_parameters;
895 u8 csi_final;
896 enum CSIMod {
897 Shift = 1,
898 Alt = 2,
899 Ctrl = 4,
900 };
901
902 for (auto code_point : input_view) {
903 if (m_finish)
904 break;
905
906 ++consumed_code_points;
907
908 if (code_point == 0)
909 continue;
910
911 switch (m_state) {
912 case InputState::GotEscape:
913 switch (code_point) {
914 case '[':
915 m_state = InputState::CSIExpectParameter;
916 continue;
917 default: {
918 m_callback_machine.key_pressed(*this, { code_point, Key::Alt });
919 m_state = InputState::Free;
920 TRY(cleanup_suggestions());
921 continue;
922 }
923 }
924 case InputState::CSIExpectParameter:
925 if (code_point >= 0x30 && code_point <= 0x3f) { // '0123456789:;<=>?'
926 csi_parameter_bytes.append(code_point);
927 continue;
928 }
929 m_state = InputState::CSIExpectIntermediate;
930 [[fallthrough]];
931 case InputState::CSIExpectIntermediate:
932 if (code_point >= 0x20 && code_point <= 0x2f) { // ' !"#$%&\'()*+,-./'
933 csi_intermediate_bytes.append(code_point);
934 continue;
935 }
936 m_state = InputState::CSIExpectFinal;
937 [[fallthrough]];
938 case InputState::CSIExpectFinal: {
939 m_state = m_previous_free_state;
940 auto is_in_paste = m_state == InputState::Paste;
941 for (auto& parameter : DeprecatedString::copy(csi_parameter_bytes).split(';')) {
942 if (auto value = parameter.to_uint(); value.has_value())
943 csi_parameters.append(value.value());
944 else
945 csi_parameters.append(0);
946 }
947 unsigned param1 = 0, param2 = 0;
948 if (csi_parameters.size() >= 1)
949 param1 = csi_parameters[0];
950 if (csi_parameters.size() >= 2)
951 param2 = csi_parameters[1];
952 unsigned modifiers = param2 ? param2 - 1 : 0;
953
954 if (is_in_paste && code_point != '~' && param1 != 201) {
955 // The only valid escape to process in paste mode is the stop-paste sequence.
956 // so treat everything else as part of the pasted data.
957 insert('\x1b');
958 insert('[');
959 insert(StringView { csi_parameter_bytes.data(), csi_parameter_bytes.size() });
960 insert(StringView { csi_intermediate_bytes.data(), csi_intermediate_bytes.size() });
961 insert(code_point);
962 continue;
963 }
964 if (!(code_point >= 0x40 && code_point <= 0x7f)) {
965 dbgln("LibLine: Invalid CSI: {:02x} ({:c})", code_point, code_point);
966 continue;
967 }
968 csi_final = code_point;
969 csi_parameters.clear();
970 csi_parameter_bytes.clear();
971 csi_intermediate_bytes.clear();
972
973 if (csi_final == 'Z') {
974 // 'reverse tab'
975 reverse_tab = true;
976 break;
977 }
978 TRY(cleanup_suggestions());
979
980 switch (csi_final) {
981 case 'A': // ^[[A: arrow up
982 search_backwards();
983 continue;
984 case 'B': // ^[[B: arrow down
985 search_forwards();
986 continue;
987 case 'D': // ^[[D: arrow left
988 if (modifiers == CSIMod::Alt || modifiers == CSIMod::Ctrl)
989 cursor_left_word();
990 else
991 cursor_left_character();
992 continue;
993 case 'C': // ^[[C: arrow right
994 if (modifiers == CSIMod::Alt || modifiers == CSIMod::Ctrl)
995 cursor_right_word();
996 else
997 cursor_right_character();
998 continue;
999 case 'H': // ^[[H: home
1000 go_home();
1001 continue;
1002 case 'F': // ^[[F: end
1003 go_end();
1004 continue;
1005 case 127:
1006 if (modifiers == CSIMod::Ctrl)
1007 erase_alnum_word_backwards();
1008 else
1009 erase_character_backwards();
1010 continue;
1011 case '~':
1012 if (param1 == 3) { // ^[[3~: delete
1013 if (modifiers == CSIMod::Ctrl)
1014 erase_alnum_word_forwards();
1015 else
1016 erase_character_forwards();
1017 m_search_offset = 0;
1018 continue;
1019 }
1020 if (m_configuration.enable_bracketed_paste) {
1021 // ^[[200~: start bracketed paste
1022 // ^[[201~: end bracketed paste
1023 if (!is_in_paste && param1 == 200) {
1024 m_state = InputState::Paste;
1025 continue;
1026 }
1027 if (is_in_paste && param1 == 201) {
1028 m_state = InputState::Free;
1029 if (on_paste) {
1030 on_paste(Utf32View { m_paste_buffer.data(), m_paste_buffer.size() }, *this);
1031 m_paste_buffer.clear_with_capacity();
1032 }
1033 if (!m_paste_buffer.is_empty())
1034 insert(Utf32View { m_paste_buffer.data(), m_paste_buffer.size() });
1035 continue;
1036 }
1037 }
1038 // ^[[5~: page up
1039 // ^[[6~: page down
1040 dbgln("LibLine: Unhandled '~': {}", param1);
1041 continue;
1042 default:
1043 dbgln("LibLine: Unhandled final: {:02x} ({:c})", code_point, code_point);
1044 continue;
1045 }
1046 VERIFY_NOT_REACHED();
1047 }
1048 case InputState::Verbatim:
1049 m_state = InputState::Free;
1050 // Verbatim mode will bypass all mechanisms and just insert the code point.
1051 insert(code_point);
1052 continue;
1053 case InputState::Paste:
1054 if (code_point == 27) {
1055 m_previous_free_state = InputState::Paste;
1056 m_state = InputState::GotEscape;
1057 continue;
1058 }
1059 if (on_paste)
1060 m_paste_buffer.append(code_point);
1061 else
1062 insert(code_point);
1063 continue;
1064 case InputState::Free:
1065 m_previous_free_state = InputState::Free;
1066 if (code_point == 27) {
1067 m_callback_machine.key_pressed(*this, code_point);
1068 // Note that this should also deal with explicitly registered keys
1069 // that would otherwise be interpreted as escapes.
1070 if (m_callback_machine.should_process_last_pressed_key())
1071 m_state = InputState::GotEscape;
1072 continue;
1073 }
1074 if (code_point == 22) { // ^v
1075 m_callback_machine.key_pressed(*this, code_point);
1076 if (m_callback_machine.should_process_last_pressed_key())
1077 m_state = InputState::Verbatim;
1078 continue;
1079 }
1080 break;
1081 }
1082
1083 // There are no sequences past this point, so short of 'tab', we will want to cleanup the suggestions.
1084 ArmedScopeGuard suggestion_cleanup { [this] { cleanup_suggestions().release_value_but_fixme_should_propagate_errors(); } };
1085
1086 // Normally ^D. `stty eof \^n` can change it to ^N (or something else), but Serenity doesn't have `stty` yet.
1087 // Process this here since the keybinds might override its behavior.
1088 // This only applies when the buffer is empty. at any other time, the behavior should be configurable.
1089 if (code_point == m_termios.c_cc[VEOF] && m_buffer.size() == 0) {
1090 finish_edit();
1091 continue;
1092 }
1093
1094 m_callback_machine.key_pressed(*this, code_point);
1095 if (!m_callback_machine.should_process_last_pressed_key())
1096 continue;
1097
1098 m_search_offset = 0; // reset search offset on any key
1099
1100 if (code_point == '\t' || reverse_tab) {
1101 suggestion_cleanup.disarm();
1102
1103 if (!on_tab_complete)
1104 continue;
1105
1106 // Reverse tab can count as regular tab here.
1107 m_times_tab_pressed++;
1108
1109 int token_start = m_cursor;
1110
1111 // Ask for completions only on the first tab
1112 // and scan for the largest common prefix to display,
1113 // further tabs simply show the cached completions.
1114 if (m_times_tab_pressed == 1) {
1115 m_suggestion_manager.set_suggestions(on_tab_complete(*this));
1116 m_suggestion_manager.set_start_index(0);
1117 m_prompt_lines_at_suggestion_initiation = num_lines();
1118 if (m_suggestion_manager.count() == 0) {
1119 // There are no suggestions, beep.
1120 fputc('\a', stderr);
1121 fflush(stderr);
1122 }
1123 }
1124
1125 // Adjust already incremented / decremented index when switching tab direction.
1126 if (reverse_tab && m_tab_direction != TabDirection::Backward) {
1127 m_suggestion_manager.previous();
1128 m_suggestion_manager.previous();
1129 m_tab_direction = TabDirection::Backward;
1130 }
1131 if (!reverse_tab && m_tab_direction != TabDirection::Forward) {
1132 m_suggestion_manager.next();
1133 m_suggestion_manager.next();
1134 m_tab_direction = TabDirection::Forward;
1135 }
1136 reverse_tab = false;
1137
1138 SuggestionManager::CompletionMode completion_mode;
1139 switch (m_times_tab_pressed) {
1140 case 1:
1141 completion_mode = SuggestionManager::CompletePrefix;
1142 break;
1143 case 2:
1144 completion_mode = SuggestionManager::ShowSuggestions;
1145 break;
1146 default:
1147 completion_mode = SuggestionManager::CycleSuggestions;
1148 break;
1149 }
1150
1151 insert(Utf32View { m_remembered_suggestion_static_data.data(), m_remembered_suggestion_static_data.size() });
1152 m_remembered_suggestion_static_data.clear_with_capacity();
1153
1154 auto completion_result = m_suggestion_manager.attempt_completion(completion_mode, token_start);
1155
1156 auto new_cursor = m_cursor;
1157
1158 new_cursor += completion_result.new_cursor_offset;
1159 for (size_t i = completion_result.offset_region_to_remove.start; i < completion_result.offset_region_to_remove.end; ++i)
1160 remove_at_index(new_cursor);
1161
1162 new_cursor -= completion_result.static_offset_from_cursor;
1163 for (size_t i = 0; i < completion_result.static_offset_from_cursor; ++i) {
1164 m_remembered_suggestion_static_data.append(m_buffer[new_cursor]);
1165 remove_at_index(new_cursor);
1166 }
1167
1168 m_cursor = new_cursor;
1169 m_inline_search_cursor = new_cursor;
1170 m_refresh_needed = true;
1171 m_chars_touched_in_the_middle++;
1172
1173 for (auto& view : completion_result.insert)
1174 insert(view);
1175
1176 auto stderr_stream = TRY(Core::File::standard_error());
1177 TRY(reposition_cursor(*stderr_stream));
1178
1179 if (completion_result.style_to_apply.has_value()) {
1180 // Apply the style of the last suggestion.
1181 readjust_anchored_styles(m_suggestion_manager.current_suggestion().start_index, ModificationKind::ForcedOverlapRemoval);
1182 stylize({ m_suggestion_manager.current_suggestion().start_index, m_cursor, Span::Mode::CodepointOriented }, completion_result.style_to_apply.value());
1183 }
1184
1185 switch (completion_result.new_completion_mode) {
1186 case SuggestionManager::DontComplete:
1187 m_times_tab_pressed = 0;
1188 m_remembered_suggestion_static_data.clear_with_capacity();
1189 break;
1190 case SuggestionManager::CompletePrefix:
1191 break;
1192 default:
1193 ++m_times_tab_pressed;
1194 break;
1195 }
1196
1197 if (m_times_tab_pressed > 1 && m_suggestion_manager.count() > 0) {
1198 if (TRY(m_suggestion_display->cleanup()))
1199 TRY(reposition_cursor(*stderr_stream));
1200
1201 m_suggestion_display->set_initial_prompt_lines(m_prompt_lines_at_suggestion_initiation);
1202
1203 TRY(m_suggestion_display->display(m_suggestion_manager));
1204
1205 m_origin_row = m_suggestion_display->origin_row();
1206 }
1207
1208 if (m_times_tab_pressed > 2) {
1209 if (m_tab_direction == TabDirection::Forward)
1210 m_suggestion_manager.next();
1211 else
1212 m_suggestion_manager.previous();
1213 }
1214
1215 if (m_suggestion_manager.count() < 2 && !completion_result.avoid_committing_to_single_suggestion) {
1216 // We have none, or just one suggestion,
1217 // we should just commit that and continue
1218 // after it, as if it were auto-completed.
1219 TRY(reposition_cursor(*stderr_stream, true));
1220 TRY(cleanup_suggestions());
1221 m_remembered_suggestion_static_data.clear_with_capacity();
1222 }
1223 continue;
1224 }
1225
1226 // If we got here, manually cleanup the suggestions and then insert the new code point.
1227 m_remembered_suggestion_static_data.clear_with_capacity();
1228 suggestion_cleanup.disarm();
1229 TRY(cleanup_suggestions());
1230 insert(code_point);
1231 }
1232
1233 if (consumed_code_points == m_incomplete_data.size()) {
1234 m_incomplete_data.clear();
1235 } else {
1236 for (size_t i = 0; i < consumed_code_points; ++i)
1237 m_incomplete_data.take_first();
1238 }
1239
1240 if (!m_incomplete_data.is_empty() && !m_finish)
1241 deferred_invoke([&] { try_update_once().release_value_but_fixme_should_propagate_errors(); });
1242
1243 return {};
1244}
1245
1246ErrorOr<void> Editor::cleanup_suggestions()
1247{
1248 if (m_times_tab_pressed != 0) {
1249 // Apply the style of the last suggestion.
1250 readjust_anchored_styles(m_suggestion_manager.current_suggestion().start_index, ModificationKind::ForcedOverlapRemoval);
1251 stylize({ m_suggestion_manager.current_suggestion().start_index, m_cursor, Span::Mode::CodepointOriented }, m_suggestion_manager.current_suggestion().style);
1252 // We probably have some suggestions drawn,
1253 // let's clean them up.
1254 if (TRY(m_suggestion_display->cleanup())) {
1255 auto stderr_stream = TRY(Core::File::standard_error());
1256 TRY(reposition_cursor(*stderr_stream));
1257 m_refresh_needed = true;
1258 }
1259 m_suggestion_manager.reset();
1260 m_suggestion_display->finish();
1261 }
1262 m_times_tab_pressed = 0; // Safe to say if we get here, the user didn't press TAB
1263 return {};
1264}
1265
1266bool Editor::search(StringView phrase, bool allow_empty, bool from_beginning)
1267{
1268 int last_matching_offset = -1;
1269 bool found = false;
1270
1271 // Do not search for empty strings.
1272 if (allow_empty || phrase.length() > 0) {
1273 size_t search_offset = m_search_offset;
1274 for (size_t i = m_history_cursor; i > 0; --i) {
1275 auto& entry = m_history[i - 1];
1276 auto contains = from_beginning ? entry.entry.starts_with(phrase) : entry.entry.contains(phrase);
1277 if (contains) {
1278 last_matching_offset = i - 1;
1279 if (search_offset == 0) {
1280 found = true;
1281 break;
1282 }
1283 --search_offset;
1284 }
1285 }
1286
1287 if (!found) {
1288 fputc('\a', stderr);
1289 fflush(stderr);
1290 }
1291 }
1292
1293 if (found) {
1294 // We plan to clear the buffer, so mark the entire thing touched.
1295 m_chars_touched_in_the_middle = m_buffer.size();
1296 m_buffer.clear();
1297 m_cursor = 0;
1298 insert(m_history[last_matching_offset].entry);
1299 // Always needed, as we have cleared the buffer above.
1300 m_refresh_needed = true;
1301 }
1302
1303 return found;
1304}
1305
1306void Editor::recalculate_origin()
1307{
1308 // Changing the columns can affect our origin if
1309 // the new size is smaller than our prompt, which would
1310 // cause said prompt to take up more space, so we should
1311 // compensate for that.
1312 if (m_cached_prompt_metrics.max_line_length >= m_num_columns) {
1313 auto added_lines = (m_cached_prompt_metrics.max_line_length + 1) / m_num_columns - 1;
1314 m_origin_row += added_lines;
1315 }
1316
1317 // We also need to recalculate our cursor position,
1318 // but that will be calculated and applied at the next
1319 // refresh cycle.
1320}
1321
1322ErrorOr<void> Editor::cleanup()
1323{
1324 auto current_buffer_metrics = actual_rendered_string_metrics(buffer_view(), m_current_masks);
1325 auto new_lines = current_prompt_metrics().lines_with_addition(current_buffer_metrics, m_num_columns);
1326 if (new_lines < m_shown_lines)
1327 m_extra_forward_lines = max(m_shown_lines - new_lines, m_extra_forward_lines);
1328
1329 auto stderr_stream = TRY(Core::File::standard_error());
1330 TRY(reposition_cursor(*stderr_stream, true));
1331 auto current_line = num_lines() - 1;
1332 TRY(VT::clear_lines(current_line, m_extra_forward_lines, *stderr_stream));
1333 m_extra_forward_lines = 0;
1334 TRY(reposition_cursor(*stderr_stream));
1335 return {};
1336};
1337
1338ErrorOr<void> Editor::refresh_display()
1339{
1340 AllocatingMemoryStream output_stream;
1341 ScopeGuard flush_stream {
1342 [&] {
1343 m_shown_lines = current_prompt_metrics().lines_with_addition(m_cached_buffer_metrics, m_num_columns);
1344
1345 if (output_stream.used_buffer_size() == 0)
1346 return;
1347
1348 auto buffer = ByteBuffer::create_uninitialized(output_stream.used_buffer_size()).release_value_but_fixme_should_propagate_errors();
1349 output_stream.read_until_filled(buffer).release_value_but_fixme_should_propagate_errors();
1350 fwrite(buffer.data(), sizeof(char), buffer.size(), stderr);
1351 }
1352 };
1353
1354 auto has_cleaned_up = false;
1355 // Someone changed the window size, figure it out
1356 // and react to it, we might need to redraw.
1357 if (m_was_resized) {
1358 if (m_previous_num_columns != m_num_columns) {
1359 // We need to cleanup and redo everything.
1360 m_cached_prompt_valid = false;
1361 m_refresh_needed = true;
1362 swap(m_previous_num_columns, m_num_columns);
1363 recalculate_origin();
1364 TRY(cleanup());
1365 swap(m_previous_num_columns, m_num_columns);
1366 has_cleaned_up = true;
1367 }
1368 m_was_resized = false;
1369 }
1370 // We might be at the last line, and have more than one line;
1371 // Refreshing the display will cause the terminal to scroll,
1372 // so note that fact and bring origin up, making sure to
1373 // reserve the space for however many lines we move it up.
1374 auto current_num_lines = num_lines();
1375 if (m_origin_row + current_num_lines > m_num_lines) {
1376 if (current_num_lines > m_num_lines) {
1377 for (size_t i = 0; i < m_num_lines; ++i)
1378 TRY(output_stream.write_until_depleted("\n"sv.bytes()));
1379 m_origin_row = 0;
1380 } else {
1381 auto old_origin_row = m_origin_row;
1382 m_origin_row = m_num_lines - current_num_lines + 1;
1383 for (size_t i = 0; i < old_origin_row - m_origin_row; ++i)
1384 TRY(output_stream.write_until_depleted("\n"sv.bytes()));
1385 }
1386 }
1387 // Do not call hook on pure cursor movement.
1388 if (m_cached_prompt_valid && !m_refresh_needed && m_pending_chars.size() == 0) {
1389 // Probably just moving around.
1390 TRY(reposition_cursor(output_stream));
1391 m_cached_buffer_metrics = actual_rendered_string_metrics(buffer_view(), m_current_masks);
1392 m_drawn_end_of_line_offset = m_buffer.size();
1393 return {};
1394 }
1395
1396 if (on_display_refresh)
1397 on_display_refresh(*this);
1398
1399 if (m_cached_prompt_valid) {
1400 if (!m_refresh_needed && m_cursor == m_buffer.size()) {
1401 // Just write the characters out and continue,
1402 // no need to refresh the entire line.
1403 TRY(output_stream.write_until_depleted(m_pending_chars));
1404 m_pending_chars.clear();
1405 m_drawn_cursor = m_cursor;
1406 m_drawn_end_of_line_offset = m_buffer.size();
1407 m_cached_buffer_metrics = actual_rendered_string_metrics(buffer_view(), m_current_masks);
1408 m_drawn_spans = m_current_spans;
1409 return {};
1410 }
1411 }
1412
1413 auto apply_styles = [&, empty_styles = HashMap<u32, Style> {}](size_t i) -> ErrorOr<void> {
1414 auto ends = m_current_spans.m_spans_ending.get(i).value_or(empty_styles);
1415 auto starts = m_current_spans.m_spans_starting.get(i).value_or(empty_styles);
1416
1417 auto anchored_ends = m_current_spans.m_anchored_spans_ending.get(i).value_or(empty_styles);
1418 auto anchored_starts = m_current_spans.m_anchored_spans_starting.get(i).value_or(empty_styles);
1419
1420 if (ends.size() || anchored_ends.size()) {
1421 Style style;
1422
1423 for (auto& applicable_style : ends)
1424 style.unify_with(applicable_style.value);
1425
1426 for (auto& applicable_style : anchored_ends)
1427 style.unify_with(applicable_style.value);
1428
1429 // Disable any style that should be turned off.
1430 TRY(VT::apply_style(style, output_stream, false));
1431
1432 // Reapply styles for overlapping spans that include this one.
1433 style = find_applicable_style(i);
1434 TRY(VT::apply_style(style, output_stream, true));
1435 }
1436 if (starts.size() || anchored_starts.size()) {
1437 Style style;
1438
1439 for (auto& applicable_style : starts)
1440 style.unify_with(applicable_style.value);
1441
1442 for (auto& applicable_style : anchored_starts)
1443 style.unify_with(applicable_style.value);
1444
1445 // Set new styles.
1446 TRY(VT::apply_style(style, output_stream, true));
1447 }
1448
1449 return {};
1450 };
1451
1452 auto print_character_at = [&](size_t i) {
1453 Variant<u32, Utf8View> c { Utf8View {} };
1454 if (auto it = m_current_masks.find_largest_not_above_iterator(i); !it.is_end() && it->has_value()) {
1455 auto offset = i - it.key();
1456 if (it->value().mode == Style::Mask::Mode::ReplaceEntireSelection) {
1457 auto& mask = it->value().replacement_view;
1458 auto replacement = mask.begin().peek(offset);
1459 if (!replacement.has_value())
1460 return;
1461 c = replacement.value();
1462 ++it;
1463 u32 next_offset = it.is_end() ? m_drawn_end_of_line_offset : it.key();
1464 if (i + 1 == next_offset)
1465 c = mask.unicode_substring_view(offset, mask.length() - offset);
1466 } else {
1467 c = it->value().replacement_view;
1468 }
1469 } else {
1470 c = m_buffer[i];
1471 }
1472 auto print_single_character = [&](auto c) -> ErrorOr<void> {
1473 StringBuilder builder;
1474 bool should_print_masked = is_ascii_control(c) && c != '\n';
1475 bool should_print_caret = c < 64 && should_print_masked;
1476 if (should_print_caret)
1477 builder.appendff("^{:c}", c + 64);
1478 else if (should_print_masked)
1479 builder.appendff("\\x{:0>2x}", c);
1480 else
1481 builder.append(Utf32View { &c, 1 });
1482
1483 if (should_print_masked)
1484 TRY(output_stream.write_until_depleted("\033[7m"sv.bytes()));
1485
1486 TRY(output_stream.write_until_depleted(builder.string_view().bytes()));
1487
1488 if (should_print_masked)
1489 TRY(output_stream.write_until_depleted("\033[27m"sv.bytes()));
1490
1491 return {};
1492 };
1493 c.visit(
1494 [&](u32 c) { print_single_character(c).release_value_but_fixme_should_propagate_errors(); },
1495 [&](auto& view) { for (auto c : view) print_single_character(c).release_value_but_fixme_should_propagate_errors(); });
1496 };
1497
1498 // If there have been no changes to previous sections of the line (style or text)
1499 // just append the new text with the appropriate styles.
1500 if (!m_always_refresh && m_cached_prompt_valid && m_chars_touched_in_the_middle == 0 && m_drawn_spans.contains_up_to_offset(m_current_spans, m_drawn_cursor)) {
1501 auto initial_style = find_applicable_style(m_drawn_end_of_line_offset);
1502 TRY(VT::apply_style(initial_style, output_stream));
1503
1504 for (size_t i = m_drawn_end_of_line_offset; i < m_buffer.size(); ++i) {
1505 TRY(apply_styles(i));
1506 print_character_at(i);
1507 }
1508
1509 TRY(VT::apply_style(Style::reset_style(), output_stream));
1510 m_pending_chars.clear();
1511 m_refresh_needed = false;
1512 m_cached_buffer_metrics = actual_rendered_string_metrics(buffer_view(), m_current_masks);
1513 m_chars_touched_in_the_middle = 0;
1514 m_drawn_cursor = m_cursor;
1515 m_drawn_end_of_line_offset = m_buffer.size();
1516
1517 // No need to reposition the cursor, the cursor is already where it needs to be.
1518 return {};
1519 }
1520
1521 if constexpr (LINE_EDITOR_DEBUG) {
1522 if (m_cached_prompt_valid && m_chars_touched_in_the_middle == 0) {
1523 auto x = m_drawn_spans.contains_up_to_offset(m_current_spans, m_drawn_cursor);
1524 dbgln("Contains: {} At offset: {}", x, m_drawn_cursor);
1525 dbgln("Drawn Spans:");
1526 for (auto& sentry : m_drawn_spans.m_spans_starting) {
1527 for (auto& entry : sentry.value) {
1528 dbgln("{}-{}: {}", sentry.key, entry.key, entry.value.to_deprecated_string());
1529 }
1530 }
1531 dbgln("==========================================================================");
1532 dbgln("Current Spans:");
1533 for (auto& sentry : m_current_spans.m_spans_starting) {
1534 for (auto& entry : sentry.value) {
1535 dbgln("{}-{}: {}", sentry.key, entry.key, entry.value.to_deprecated_string());
1536 }
1537 }
1538 }
1539 }
1540
1541 // Ouch, reflow entire line.
1542 if (!has_cleaned_up) {
1543 TRY(cleanup());
1544 }
1545 TRY(VT::move_absolute(m_origin_row, m_origin_column, output_stream));
1546
1547 TRY(output_stream.write_until_depleted(m_new_prompt.bytes()));
1548
1549 TRY(VT::clear_to_end_of_line(output_stream));
1550 StringBuilder builder;
1551 for (size_t i = 0; i < m_buffer.size(); ++i) {
1552 TRY(apply_styles(i));
1553 print_character_at(i);
1554 }
1555
1556 TRY(VT::apply_style(Style::reset_style(), output_stream)); // don't bleed to EOL
1557
1558 m_pending_chars.clear();
1559 m_refresh_needed = false;
1560 m_cached_buffer_metrics = actual_rendered_string_metrics(buffer_view(), m_current_masks);
1561 m_chars_touched_in_the_middle = 0;
1562 m_drawn_spans = m_current_spans;
1563 m_drawn_end_of_line_offset = m_buffer.size();
1564 m_cached_prompt_valid = true;
1565
1566 TRY(reposition_cursor(output_stream));
1567 return {};
1568}
1569
1570void Editor::strip_styles(bool strip_anchored)
1571{
1572 m_current_spans.m_spans_starting.clear();
1573 m_current_spans.m_spans_ending.clear();
1574 m_current_masks.clear();
1575 m_cached_buffer_metrics = actual_rendered_string_metrics(buffer_view(), {});
1576
1577 if (strip_anchored) {
1578 m_current_spans.m_anchored_spans_starting.clear();
1579 m_current_spans.m_anchored_spans_ending.clear();
1580 }
1581
1582 m_refresh_needed = true;
1583}
1584
1585ErrorOr<void> Editor::reposition_cursor(Stream& stream, bool to_end)
1586{
1587 auto cursor = m_cursor;
1588 auto saved_cursor = m_cursor;
1589 if (to_end)
1590 cursor = m_buffer.size();
1591
1592 m_cursor = cursor;
1593 m_drawn_cursor = cursor;
1594
1595 auto line = cursor_line() - 1;
1596 auto column = offset_in_line();
1597
1598 ensure_free_lines_from_origin(line);
1599
1600 VERIFY(column + m_origin_column <= m_num_columns);
1601 TRY(VT::move_absolute(line + m_origin_row, column + m_origin_column, stream));
1602
1603 m_cursor = saved_cursor;
1604 return {};
1605}
1606
1607ErrorOr<void> VT::move_absolute(u32 row, u32 col, Stream& stream)
1608{
1609 return stream.write_until_depleted(DeprecatedString::formatted("\033[{};{}H", row, col).bytes());
1610}
1611
1612ErrorOr<void> VT::move_relative(int row, int col, Stream& stream)
1613{
1614 char x_op = 'A', y_op = 'D';
1615
1616 if (row > 0)
1617 x_op = 'B';
1618 else
1619 row = -row;
1620 if (col > 0)
1621 y_op = 'C';
1622 else
1623 col = -col;
1624
1625 if (row > 0)
1626 TRY(stream.write_until_depleted(DeprecatedString::formatted("\033[{}{}", row, x_op).bytes()));
1627 if (col > 0)
1628 TRY(stream.write_until_depleted(DeprecatedString::formatted("\033[{}{}", col, y_op).bytes()));
1629
1630 return {};
1631}
1632
1633Style Editor::find_applicable_style(size_t offset) const
1634{
1635 // Walk through our styles and merge all that fit in the offset.
1636 auto style = Style::reset_style();
1637 auto unify = [&](auto& entry) {
1638 if (entry.key >= offset)
1639 return;
1640 for (auto& style_value : entry.value) {
1641 if (style_value.key <= offset)
1642 return;
1643 style.unify_with(style_value.value, true);
1644 }
1645 };
1646
1647 for (auto& entry : m_current_spans.m_spans_starting) {
1648 unify(entry);
1649 }
1650
1651 for (auto& entry : m_current_spans.m_anchored_spans_starting) {
1652 unify(entry);
1653 }
1654
1655 return style;
1656}
1657
1658DeprecatedString Style::Background::to_vt_escape() const
1659{
1660 if (is_default())
1661 return "";
1662
1663 if (m_is_rgb) {
1664 return DeprecatedString::formatted("\e[48;2;{};{};{}m", m_rgb_color[0], m_rgb_color[1], m_rgb_color[2]);
1665 } else {
1666 return DeprecatedString::formatted("\e[{}m", (u8)m_xterm_color + 40);
1667 }
1668}
1669
1670DeprecatedString Style::Foreground::to_vt_escape() const
1671{
1672 if (is_default())
1673 return "";
1674
1675 if (m_is_rgb) {
1676 return DeprecatedString::formatted("\e[38;2;{};{};{}m", m_rgb_color[0], m_rgb_color[1], m_rgb_color[2]);
1677 } else {
1678 return DeprecatedString::formatted("\e[{}m", (u8)m_xterm_color + 30);
1679 }
1680}
1681
1682DeprecatedString Style::Hyperlink::to_vt_escape(bool starting) const
1683{
1684 if (is_empty())
1685 return "";
1686
1687 return DeprecatedString::formatted("\e]8;;{}\e\\", starting ? m_link : DeprecatedString::empty());
1688}
1689
1690void Style::unify_with(Style const& other, bool prefer_other)
1691{
1692 // Unify colors.
1693 if (prefer_other || m_background.is_default())
1694 m_background = other.background();
1695
1696 if (prefer_other || m_foreground.is_default())
1697 m_foreground = other.foreground();
1698
1699 // Unify graphic renditions.
1700 if (other.bold())
1701 set(Bold);
1702
1703 if (other.italic())
1704 set(Italic);
1705
1706 if (other.underline())
1707 set(Underline);
1708
1709 // Unify links.
1710 if (prefer_other || m_hyperlink.is_empty())
1711 m_hyperlink = other.hyperlink();
1712}
1713
1714DeprecatedString Style::to_deprecated_string() const
1715{
1716 StringBuilder builder;
1717 builder.append("Style { "sv);
1718
1719 if (!m_foreground.is_default()) {
1720 builder.append("Foreground("sv);
1721 if (m_foreground.m_is_rgb) {
1722 builder.join(", "sv, m_foreground.m_rgb_color);
1723 } else {
1724 builder.appendff("(XtermColor) {}", (int)m_foreground.m_xterm_color);
1725 }
1726 builder.append("), "sv);
1727 }
1728
1729 if (!m_background.is_default()) {
1730 builder.append("Background("sv);
1731 if (m_background.m_is_rgb) {
1732 builder.join(' ', m_background.m_rgb_color);
1733 } else {
1734 builder.appendff("(XtermColor) {}", (int)m_background.m_xterm_color);
1735 }
1736 builder.append("), "sv);
1737 }
1738
1739 if (bold())
1740 builder.append("Bold, "sv);
1741
1742 if (underline())
1743 builder.append("Underline, "sv);
1744
1745 if (italic())
1746 builder.append("Italic, "sv);
1747
1748 if (!m_hyperlink.is_empty())
1749 builder.appendff("Hyperlink(\"{}\"), ", m_hyperlink.m_link);
1750
1751 if (!m_mask.has_value()) {
1752 builder.appendff("Mask(\"{}\", {}), ",
1753 m_mask->replacement,
1754 m_mask->mode == Mask::Mode::ReplaceEntireSelection
1755 ? "ReplaceEntireSelection"
1756 : "ReplaceEachCodePointInSelection");
1757 }
1758
1759 builder.append('}');
1760
1761 return builder.to_deprecated_string();
1762}
1763
1764ErrorOr<void> VT::apply_style(Style const& style, Stream& stream, bool is_starting)
1765{
1766 if (is_starting) {
1767 TRY(stream.write_until_depleted(DeprecatedString::formatted("\033[{};{};{}m{}{}{}",
1768 style.bold() ? 1 : 22,
1769 style.underline() ? 4 : 24,
1770 style.italic() ? 3 : 23,
1771 style.background().to_vt_escape(),
1772 style.foreground().to_vt_escape(),
1773 style.hyperlink().to_vt_escape(true))
1774 .bytes()));
1775 } else {
1776 TRY(stream.write_until_depleted(style.hyperlink().to_vt_escape(false).bytes()));
1777 }
1778
1779 return {};
1780}
1781
1782ErrorOr<void> VT::clear_lines(size_t count_above, size_t count_below, Stream& stream)
1783{
1784 if (count_below + count_above == 0) {
1785 TRY(stream.write_until_depleted("\033[2K"sv.bytes()));
1786 } else {
1787 // Go down count_below lines.
1788 if (count_below > 0)
1789 TRY(stream.write_until_depleted(DeprecatedString::formatted("\033[{}B", count_below).bytes()));
1790 // Then clear lines going upwards.
1791 for (size_t i = count_below + count_above; i > 0; --i) {
1792 TRY(stream.write_until_depleted("\033[2K"sv.bytes()));
1793 if (i != 1)
1794 TRY(stream.write_until_depleted("\033[A"sv.bytes()));
1795 }
1796 }
1797
1798 return {};
1799}
1800
1801ErrorOr<void> VT::save_cursor(Stream& stream)
1802{
1803 return stream.write_until_depleted("\033[s"sv.bytes());
1804}
1805
1806ErrorOr<void> VT::restore_cursor(Stream& stream)
1807{
1808 return stream.write_until_depleted("\033[u"sv.bytes());
1809}
1810
1811ErrorOr<void> VT::clear_to_end_of_line(Stream& stream)
1812{
1813 return stream.write_until_depleted("\033[K"sv.bytes());
1814}
1815
1816enum VTState {
1817 Free = 1,
1818 Escape = 3,
1819 Bracket = 5,
1820 BracketArgsSemi = 7,
1821 Title = 9,
1822 URL = 11,
1823};
1824static VTState actual_rendered_string_length_step(StringMetrics& metrics, size_t index, StringMetrics::LineMetrics& current_line, u32 c, u32 next_c, VTState state, Optional<Style::Mask> const& mask, Optional<size_t> const& maximum_line_width = {}, Optional<size_t&> last_return = {});
1825
1826enum class MaskedSelectionDecision {
1827 Skip,
1828 Continue,
1829};
1830static MaskedSelectionDecision resolve_masked_selection(Optional<Style::Mask>& mask, size_t& i, auto& mask_it, auto& view, auto& state, auto& metrics, auto& current_line)
1831{
1832 if (mask.has_value() && mask->mode == Style::Mask::Mode::ReplaceEntireSelection) {
1833 ++mask_it;
1834 auto actual_end_offset = mask_it.is_end() ? view.length() : mask_it.key();
1835 auto end_offset = min(actual_end_offset, view.length());
1836 size_t j = 0;
1837 for (auto it = mask->replacement_view.begin(); it != mask->replacement_view.end(); ++it) {
1838 auto it_copy = it;
1839 ++it_copy;
1840 auto next_c = it_copy == mask->replacement_view.end() ? 0 : *it_copy;
1841 state = actual_rendered_string_length_step(metrics, j, current_line, *it, next_c, state, {});
1842 ++j;
1843 if (j <= actual_end_offset - i && j + i >= view.length())
1844 break;
1845 }
1846 current_line.masked_chars.empend(i, end_offset - i, j);
1847 i = end_offset;
1848
1849 if (mask_it.is_end())
1850 mask = {};
1851 else
1852 mask = *mask_it;
1853 return MaskedSelectionDecision::Skip;
1854 }
1855 return MaskedSelectionDecision::Continue;
1856}
1857
1858StringMetrics Editor::actual_rendered_string_metrics(StringView string, RedBlackTree<u32, Optional<Style::Mask>> const& masks, Optional<size_t> maximum_line_width)
1859{
1860 StringMetrics metrics;
1861 StringMetrics::LineMetrics current_line;
1862 VTState state { Free };
1863 Utf8View view { string };
1864 size_t last_return {};
1865 auto it = view.begin();
1866 Optional<Style::Mask> mask;
1867 size_t i = 0;
1868 auto mask_it = masks.begin();
1869
1870 for (; it != view.end(); ++it) {
1871 if (!mask_it.is_end() && mask_it.key() <= i)
1872 mask = *mask_it;
1873 auto c = *it;
1874 auto it_copy = it;
1875 ++it_copy;
1876
1877 if (resolve_masked_selection(mask, i, mask_it, view, state, metrics, current_line) == MaskedSelectionDecision::Skip)
1878 continue;
1879
1880 auto next_c = it_copy == view.end() ? 0 : *it_copy;
1881 state = actual_rendered_string_length_step(metrics, view.iterator_offset(it), current_line, c, next_c, state, mask, maximum_line_width, last_return);
1882 if (!mask_it.is_end() && mask_it.key() <= i) {
1883 auto mask_it_peek = mask_it;
1884 ++mask_it_peek;
1885 if (!mask_it_peek.is_end() && mask_it_peek.key() > i)
1886 mask_it = mask_it_peek;
1887 }
1888 ++i;
1889 }
1890
1891 metrics.line_metrics.append(current_line);
1892
1893 for (auto& line : metrics.line_metrics)
1894 metrics.max_line_length = max(line.total_length(), metrics.max_line_length);
1895
1896 return metrics;
1897}
1898
1899StringMetrics Editor::actual_rendered_string_metrics(Utf32View const& view, RedBlackTree<u32, Optional<Style::Mask>> const& masks)
1900{
1901 StringMetrics metrics;
1902 StringMetrics::LineMetrics current_line;
1903 VTState state { Free };
1904 Optional<Style::Mask> mask;
1905
1906 auto mask_it = masks.begin();
1907
1908 for (size_t i = 0; i < view.length(); ++i) {
1909 auto c = view[i];
1910 if (!mask_it.is_end() && mask_it.key() <= i)
1911 mask = *mask_it;
1912
1913 if (resolve_masked_selection(mask, i, mask_it, view, state, metrics, current_line) == MaskedSelectionDecision::Skip) {
1914 --i;
1915 continue;
1916 }
1917
1918 auto next_c = i + 1 < view.length() ? view.code_points()[i + 1] : 0;
1919 state = actual_rendered_string_length_step(metrics, i, current_line, c, next_c, state, mask);
1920 if (!mask_it.is_end() && mask_it.key() <= i) {
1921 auto mask_it_peek = mask_it;
1922 ++mask_it_peek;
1923 if (!mask_it_peek.is_end() && mask_it_peek.key() > i)
1924 mask_it = mask_it_peek;
1925 }
1926 }
1927
1928 metrics.line_metrics.append(current_line);
1929
1930 for (auto& line : metrics.line_metrics)
1931 metrics.max_line_length = max(line.total_length(), metrics.max_line_length);
1932
1933 return metrics;
1934}
1935
1936VTState actual_rendered_string_length_step(StringMetrics& metrics, size_t index, StringMetrics::LineMetrics& current_line, u32 c, u32 next_c, VTState state, Optional<Style::Mask> const& mask, Optional<size_t> const& maximum_line_width, Optional<size_t&> last_return)
1937{
1938 auto const save_line = [&metrics, ¤t_line, &last_return, &index]() {
1939 if (last_return.has_value()) {
1940 auto const last_index = index - 1;
1941 current_line.bit_length = last_index - *last_return + 1;
1942 last_return.value() = last_index + 1;
1943 }
1944 metrics.line_metrics.append(current_line);
1945
1946 current_line.masked_chars = {};
1947 current_line.length = 0;
1948 current_line.visible_length = 0;
1949 current_line.bit_length = {};
1950 };
1951
1952 // FIXME: current_line.visible_length can go above maximum_line_width when using masks
1953 if (maximum_line_width.has_value() && current_line.visible_length >= maximum_line_width.value())
1954 save_line();
1955
1956 ScopeGuard bit_length_update { [&last_return, ¤t_line, &index]() {
1957 if (last_return.has_value())
1958 current_line.bit_length = index - *last_return + 1;
1959 } };
1960
1961 switch (state) {
1962 case Free: {
1963 if (c == '\x1b') { // escape
1964 return Escape;
1965 }
1966 if (c == '\r') { // carriage return
1967 current_line.masked_chars = {};
1968 current_line.length = 0;
1969 current_line.visible_length = 0;
1970 if (!metrics.line_metrics.is_empty())
1971 metrics.line_metrics.last() = { {}, 0 };
1972 return state;
1973 }
1974 if (c == '\n') { // return
1975 save_line();
1976 return state;
1977 }
1978 if (c == '\t') {
1979 // Tabs are a special case, because their width is variable.
1980 ++current_line.length;
1981 current_line.visible_length += (8 - (current_line.visible_length % 8));
1982 return state;
1983 }
1984 auto is_control = is_ascii_control(c);
1985 if (is_control) {
1986 if (mask.has_value())
1987 current_line.masked_chars.append({ index, 1, mask->replacement_view.length() });
1988 else
1989 current_line.masked_chars.append({ index, 1, c < 64 ? 2u : 4u }); // if the character cannot be represented as ^c, represent it as \xbb.
1990 }
1991 // FIXME: This will not support anything sophisticated
1992 if (mask.has_value()) {
1993 current_line.length += mask->replacement_view.length();
1994 current_line.visible_length += mask->replacement_view.length();
1995 metrics.total_length += mask->replacement_view.length();
1996 } else if (is_control) {
1997 current_line.length += current_line.masked_chars.last().masked_length;
1998 current_line.visible_length += current_line.masked_chars.last().masked_length;
1999 metrics.total_length += current_line.masked_chars.last().masked_length;
2000 } else {
2001 ++current_line.length;
2002 ++current_line.visible_length;
2003 ++metrics.total_length;
2004 }
2005 return state;
2006 }
2007 case Escape:
2008 if (c == ']') {
2009 if (next_c == '0')
2010 state = Title;
2011 if (next_c == '8')
2012 state = URL;
2013 return state;
2014 }
2015 if (c == '[') {
2016 return Bracket;
2017 }
2018 // FIXME: This does not support non-VT (aside from set-title) escapes
2019 return state;
2020 case Bracket:
2021 if (is_ascii_digit(c)) {
2022 return BracketArgsSemi;
2023 }
2024 return state;
2025 case BracketArgsSemi:
2026 if (c == ';') {
2027 return Bracket;
2028 }
2029 if (!is_ascii_digit(c))
2030 state = Free;
2031 return state;
2032 case Title:
2033 if (c == 7)
2034 state = Free;
2035 return state;
2036 case URL:
2037 if (c == '\\')
2038 state = Free;
2039 return state;
2040 }
2041 return state;
2042}
2043
2044Result<Vector<size_t, 2>, Editor::Error> Editor::vt_dsr()
2045{
2046 char buf[16];
2047
2048 // Read whatever junk there is before talking to the terminal
2049 // and insert them later when we're reading user input.
2050 bool more_junk_to_read { false };
2051 timeval timeout { 0, 0 };
2052 fd_set readfds;
2053 FD_ZERO(&readfds);
2054 FD_SET(0, &readfds);
2055
2056 do {
2057 more_junk_to_read = false;
2058 [[maybe_unused]] auto rc = select(1, &readfds, nullptr, nullptr, &timeout);
2059 if (FD_ISSET(0, &readfds)) {
2060 auto nread = read(0, buf, 16);
2061 if (nread < 0) {
2062 m_input_error = Error::ReadFailure;
2063 finish();
2064 break;
2065 }
2066
2067 if (nread == 0)
2068 break;
2069
2070 m_incomplete_data.append(buf, nread);
2071 more_junk_to_read = true;
2072 }
2073 } while (more_junk_to_read);
2074
2075 if (m_input_error.has_value())
2076 return m_input_error.value();
2077
2078 fputs("\033[6n", stderr);
2079 fflush(stderr);
2080
2081 // Parse the DSR response
2082 // it should be of the form .*\e[\d+;\d+R.*
2083 // Anything not part of the response is just added to the incomplete data.
2084 enum {
2085 Free,
2086 SawEsc,
2087 SawBracket,
2088 InFirstCoordinate,
2089 SawSemicolon,
2090 InSecondCoordinate,
2091 SawR,
2092 } state { Free };
2093 auto has_error = false;
2094 Vector<char, 4> coordinate_buffer;
2095 size_t row { 1 }, col { 1 };
2096
2097 do {
2098 char c;
2099 auto nread = read(0, &c, 1);
2100 if (nread < 0) {
2101 if (errno == 0 || errno == EINTR) {
2102 // ????
2103 continue;
2104 }
2105 dbgln("Error while reading DSR: {}", strerror(errno));
2106 return Error::ReadFailure;
2107 }
2108 if (nread == 0) {
2109 dbgln("Terminal DSR issue; received no response");
2110 return Error::Empty;
2111 }
2112
2113 switch (state) {
2114 case Free:
2115 if (c == '\x1b') {
2116 state = SawEsc;
2117 continue;
2118 }
2119 m_incomplete_data.append(c);
2120 continue;
2121 case SawEsc:
2122 if (c == '[') {
2123 state = SawBracket;
2124 continue;
2125 }
2126 m_incomplete_data.append(c);
2127 continue;
2128 case SawBracket:
2129 if (is_ascii_digit(c)) {
2130 state = InFirstCoordinate;
2131 coordinate_buffer.append(c);
2132 continue;
2133 }
2134 m_incomplete_data.append(c);
2135 continue;
2136 case InFirstCoordinate:
2137 if (is_ascii_digit(c)) {
2138 coordinate_buffer.append(c);
2139 continue;
2140 }
2141 if (c == ';') {
2142 auto maybe_row = StringView { coordinate_buffer.data(), coordinate_buffer.size() }.to_uint();
2143 if (!maybe_row.has_value())
2144 has_error = true;
2145 row = maybe_row.value_or(1u);
2146 coordinate_buffer.clear_with_capacity();
2147 state = SawSemicolon;
2148 continue;
2149 }
2150 m_incomplete_data.append(c);
2151 continue;
2152 case SawSemicolon:
2153 if (is_ascii_digit(c)) {
2154 state = InSecondCoordinate;
2155 coordinate_buffer.append(c);
2156 continue;
2157 }
2158 m_incomplete_data.append(c);
2159 continue;
2160 case InSecondCoordinate:
2161 if (is_ascii_digit(c)) {
2162 coordinate_buffer.append(c);
2163 continue;
2164 }
2165 if (c == 'R') {
2166 auto maybe_column = StringView { coordinate_buffer.data(), coordinate_buffer.size() }.to_uint();
2167 if (!maybe_column.has_value())
2168 has_error = true;
2169 col = maybe_column.value_or(1u);
2170 coordinate_buffer.clear_with_capacity();
2171 state = SawR;
2172 continue;
2173 }
2174 m_incomplete_data.append(c);
2175 continue;
2176 case SawR:
2177 m_incomplete_data.append(c);
2178 continue;
2179 default:
2180 VERIFY_NOT_REACHED();
2181 }
2182 } while (state != SawR);
2183
2184 if (has_error)
2185 dbgln("Terminal DSR issue, couldn't parse DSR response");
2186 return Vector<size_t, 2> { row, col };
2187}
2188
2189DeprecatedString Editor::line(size_t up_to_index) const
2190{
2191 StringBuilder builder;
2192 builder.append(Utf32View { m_buffer.data(), min(m_buffer.size(), up_to_index) });
2193 return builder.to_deprecated_string();
2194}
2195
2196void Editor::remove_at_index(size_t index)
2197{
2198 // See if we have any anchored styles, and reposition them if needed.
2199 readjust_anchored_styles(index, ModificationKind::Removal);
2200 auto cp = m_buffer[index];
2201 m_buffer.remove(index);
2202 if (cp == '\n')
2203 ++m_extra_forward_lines;
2204 ++m_chars_touched_in_the_middle;
2205}
2206
2207void Editor::readjust_anchored_styles(size_t hint_index, ModificationKind modification)
2208{
2209 struct Anchor {
2210 Span old_span;
2211 Span new_span;
2212 Style style;
2213 };
2214 Vector<Anchor> anchors_to_relocate;
2215 auto index_shift = modification == ModificationKind::Insertion ? 1 : -1;
2216 auto forced_removal = modification == ModificationKind::ForcedOverlapRemoval;
2217
2218 for (auto& start_entry : m_current_spans.m_anchored_spans_starting) {
2219 for (auto& end_entry : start_entry.value) {
2220 if (forced_removal) {
2221 if (start_entry.key <= hint_index && end_entry.key > hint_index) {
2222 // Remove any overlapping regions.
2223 continue;
2224 }
2225 }
2226 if (start_entry.key >= hint_index) {
2227 if (start_entry.key == hint_index && end_entry.key == hint_index + 1 && modification == ModificationKind::Removal) {
2228 // Remove the anchor, as all its text was wiped.
2229 continue;
2230 }
2231 // Shift everything.
2232 anchors_to_relocate.append({ { start_entry.key, end_entry.key, Span::Mode::CodepointOriented }, { start_entry.key + index_shift, end_entry.key + index_shift, Span::Mode::CodepointOriented }, end_entry.value });
2233 continue;
2234 }
2235 if (end_entry.key > hint_index) {
2236 // Shift just the end.
2237 anchors_to_relocate.append({ { start_entry.key, end_entry.key, Span::Mode::CodepointOriented }, { start_entry.key, end_entry.key + index_shift, Span::Mode::CodepointOriented }, end_entry.value });
2238 continue;
2239 }
2240 anchors_to_relocate.append({ { start_entry.key, end_entry.key, Span::Mode::CodepointOriented }, { start_entry.key, end_entry.key, Span::Mode::CodepointOriented }, end_entry.value });
2241 }
2242 }
2243
2244 m_current_spans.m_anchored_spans_ending.clear();
2245 m_current_spans.m_anchored_spans_starting.clear();
2246 // Pass over the relocations and update the stale entries.
2247 for (auto& relocation : anchors_to_relocate) {
2248 stylize(relocation.new_span, relocation.style);
2249 }
2250}
2251
2252size_t StringMetrics::lines_with_addition(StringMetrics const& offset, size_t column_width) const
2253{
2254 size_t lines = 0;
2255
2256 if (!line_metrics.is_empty()) {
2257 for (size_t i = 0; i < line_metrics.size() - 1; ++i)
2258 lines += (line_metrics[i].total_length() + column_width) / column_width;
2259
2260 auto last = line_metrics.last().total_length();
2261 last += offset.line_metrics.first().total_length();
2262 lines += (last + column_width) / column_width;
2263 }
2264
2265 for (size_t i = 1; i < offset.line_metrics.size(); ++i)
2266 lines += (offset.line_metrics[i].total_length() + column_width) / column_width;
2267
2268 return lines;
2269}
2270
2271size_t StringMetrics::offset_with_addition(StringMetrics const& offset, size_t column_width) const
2272{
2273 if (offset.line_metrics.size() > 1)
2274 return offset.line_metrics.last().total_length() % column_width;
2275
2276 if (!line_metrics.is_empty()) {
2277 auto last = line_metrics.last().total_length();
2278 last += offset.line_metrics.first().total_length();
2279 return last % column_width;
2280 }
2281
2282 if (offset.line_metrics.is_empty())
2283 return 0;
2284
2285 return offset.line_metrics.first().total_length() % column_width;
2286}
2287
2288bool Editor::Spans::contains_up_to_offset(Spans const& other, size_t offset) const
2289{
2290 auto compare = [&]<typename K, typename V>(HashMap<K, HashMap<K, V>> const& left, HashMap<K, HashMap<K, V>> const& right) -> bool {
2291 for (auto& entry : right) {
2292 if (entry.key > offset + 1)
2293 continue;
2294
2295 auto left_map_it = left.find(entry.key);
2296 if (left_map_it == left.end())
2297 return false;
2298
2299 for (auto& left_entry : left_map_it->value) {
2300 auto value_it = entry.value.find(left_entry.key);
2301 if (value_it == entry.value.end()) {
2302 // Might have the same thing with a longer span
2303 bool found = false;
2304 for (auto& possibly_longer_span_entry : entry.value) {
2305 if (possibly_longer_span_entry.key > left_entry.key && possibly_longer_span_entry.key > offset && left_entry.value == possibly_longer_span_entry.value) {
2306 found = true;
2307 break;
2308 }
2309 }
2310 if (found)
2311 continue;
2312 if constexpr (LINE_EDITOR_DEBUG) {
2313 dbgln("Compare for {}-{} failed, no entry", entry.key, left_entry.key);
2314 for (auto& x : entry.value)
2315 dbgln("Have: {}-{} = {}", entry.key, x.key, x.value.to_deprecated_string());
2316 }
2317 return false;
2318 } else if (value_it->value != left_entry.value) {
2319 dbgln_if(LINE_EDITOR_DEBUG, "Compare for {}-{} failed, different values: {} != {}", entry.key, left_entry.key, value_it->value.to_deprecated_string(), left_entry.value.to_deprecated_string());
2320 return false;
2321 }
2322 }
2323 }
2324
2325 return true;
2326 };
2327
2328 return compare(m_spans_starting, other.m_spans_starting)
2329 && compare(m_anchored_spans_starting, other.m_anchored_spans_starting);
2330}
2331
2332}