Editor.cpp 83 KB

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