Editor.cpp 81 KB

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