Editor.cpp 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343
  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(const u32 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. TRY(reposition_cursor(*stderr_stream, true));
  544. TRY(stderr_stream->write_until_depleted("\r"sv.bytes()));
  545. }
  546. m_buffer.clear();
  547. m_chars_touched_in_the_middle = buffer().size();
  548. m_is_editing = false;
  549. restore();
  550. m_notifier->set_enabled(false);
  551. m_notifier = nullptr;
  552. Core::EventLoop::current().quit(Retry);
  553. return {};
  554. }
  555. ErrorOr<void> Editor::resized()
  556. {
  557. m_was_resized = true;
  558. m_previous_num_columns = m_num_columns;
  559. get_terminal_size();
  560. if (!m_has_origin_reset_scheduled) {
  561. // Reset the origin, but make sure it doesn't blow up if we can't read it
  562. if (set_origin(false)) {
  563. TRY(handle_resize_event(false));
  564. } else {
  565. deferred_invoke([this] { handle_resize_event(true).release_value_but_fixme_should_propagate_errors(); });
  566. m_has_origin_reset_scheduled = true;
  567. }
  568. }
  569. return {};
  570. }
  571. ErrorOr<void> Editor::handle_resize_event(bool reset_origin)
  572. {
  573. m_has_origin_reset_scheduled = false;
  574. if (reset_origin && !set_origin(false)) {
  575. m_has_origin_reset_scheduled = true;
  576. deferred_invoke([this] { handle_resize_event(true).release_value_but_fixme_should_propagate_errors(); });
  577. return {};
  578. }
  579. set_origin(m_origin_row, 1);
  580. auto stderr_stream = TRY(Core::File::standard_error());
  581. TRY(reposition_cursor(*stderr_stream, true));
  582. TRY(m_suggestion_display->redisplay(m_suggestion_manager, m_num_lines, m_num_columns));
  583. m_origin_row = m_suggestion_display->origin_row();
  584. TRY(reposition_cursor(*stderr_stream));
  585. if (m_is_searching)
  586. TRY(m_search_editor->resized());
  587. return {};
  588. }
  589. ErrorOr<void> Editor::really_quit_event_loop()
  590. {
  591. m_finish = false;
  592. {
  593. auto stderr_stream = TRY(Core::File::standard_error());
  594. TRY(reposition_cursor(*stderr_stream, true));
  595. TRY(stderr_stream->write_until_depleted("\n"sv.bytes()));
  596. }
  597. auto string = line();
  598. m_buffer.clear();
  599. m_chars_touched_in_the_middle = buffer().size();
  600. m_is_editing = false;
  601. if (m_initialized)
  602. restore();
  603. m_returned_line = string;
  604. m_notifier->set_enabled(false);
  605. m_notifier = nullptr;
  606. Core::EventLoop::current().quit(Exit);
  607. return {};
  608. }
  609. auto Editor::get_line(DeprecatedString const& prompt) -> Result<DeprecatedString, Editor::Error>
  610. {
  611. initialize();
  612. m_is_editing = true;
  613. if (m_configuration.operation_mode == Configuration::NoEscapeSequences || m_configuration.operation_mode == Configuration::NonInteractive) {
  614. // Do not use escape sequences, instead, use LibC's getline.
  615. size_t size = 0;
  616. char* line = nullptr;
  617. // Show the prompt only on interactive mode (NoEscapeSequences in this case).
  618. if (m_configuration.operation_mode != Configuration::NonInteractive)
  619. fputs(prompt.characters(), stderr);
  620. auto line_length = getline(&line, &size, stdin);
  621. // getline() returns -1 and sets errno=0 on EOF.
  622. if (line_length == -1) {
  623. if (line)
  624. free(line);
  625. if (errno == 0)
  626. return Error::Eof;
  627. return Error::ReadFailure;
  628. }
  629. restore();
  630. if (line) {
  631. DeprecatedString result { line, (size_t)line_length, Chomp };
  632. free(line);
  633. return result;
  634. }
  635. return Error::ReadFailure;
  636. }
  637. auto old_cols = m_num_columns;
  638. auto old_lines = m_num_lines;
  639. get_terminal_size();
  640. if (m_configuration.enable_bracketed_paste)
  641. fprintf(stderr, "\x1b[?2004h");
  642. if (m_num_columns != old_cols || m_num_lines != old_lines)
  643. m_refresh_needed = true;
  644. set_prompt(prompt);
  645. reset();
  646. strip_styles(true);
  647. {
  648. auto stderr_stream = Core::File::standard_error().release_value_but_fixme_should_propagate_errors();
  649. auto prompt_lines = max(current_prompt_metrics().line_metrics.size(), 1ul) - 1;
  650. for (size_t i = 0; i < prompt_lines; ++i)
  651. stderr_stream->write_until_depleted("\n"sv.bytes()).release_value_but_fixme_should_propagate_errors();
  652. VT::move_relative(-static_cast<int>(prompt_lines), 0, *stderr_stream).release_value_but_fixme_should_propagate_errors();
  653. }
  654. set_origin();
  655. m_history_cursor = m_history.size();
  656. if (auto refresh_result = refresh_display(); refresh_result.is_error())
  657. m_input_error = Error::ReadFailure;
  658. Core::EventLoop loop;
  659. m_notifier = Core::Notifier::construct(STDIN_FILENO, Core::Notifier::Type::Read);
  660. if (m_input_error.has_value())
  661. loop.quit(Exit);
  662. m_notifier->on_activation = [&] {
  663. if (try_update_once().is_error())
  664. loop.quit(Exit);
  665. };
  666. if (!m_incomplete_data.is_empty()) {
  667. deferred_invoke([&] {
  668. if (try_update_once().is_error())
  669. loop.quit(Exit);
  670. });
  671. }
  672. if (loop.exec() == Retry)
  673. return get_line(prompt);
  674. return m_input_error.has_value() ? Result<DeprecatedString, Editor::Error> { m_input_error.value() } : Result<DeprecatedString, Editor::Error> { m_returned_line };
  675. }
  676. ErrorOr<void> Editor::try_update_once()
  677. {
  678. if (m_was_interrupted) {
  679. handle_interrupt_event();
  680. }
  681. TRY(handle_read_event());
  682. if (m_always_refresh)
  683. m_refresh_needed = true;
  684. TRY(refresh_display());
  685. if (m_finish)
  686. TRY(really_quit_event_loop());
  687. return {};
  688. }
  689. void Editor::handle_interrupt_event()
  690. {
  691. m_was_interrupted = false;
  692. m_previous_interrupt_was_handled_as_interrupt = false;
  693. m_callback_machine.interrupted(*this);
  694. if (!m_callback_machine.should_process_last_pressed_key())
  695. return;
  696. m_previous_interrupt_was_handled_as_interrupt = true;
  697. fprintf(stderr, "^C\n");
  698. fflush(stderr);
  699. if (on_interrupt_handled)
  700. on_interrupt_handled();
  701. m_buffer.clear();
  702. m_chars_touched_in_the_middle = buffer().size();
  703. m_cursor = 0;
  704. set_origin(false);
  705. finish();
  706. }
  707. ErrorOr<void> Editor::handle_read_event()
  708. {
  709. if (m_prohibit_input_processing) {
  710. m_have_unprocessed_read_event = true;
  711. return {};
  712. }
  713. auto prohibit_scope = prohibit_input();
  714. char keybuf[16];
  715. ssize_t nread = 0;
  716. if (!m_incomplete_data.size())
  717. nread = read(0, keybuf, sizeof(keybuf));
  718. if (nread < 0) {
  719. if (errno == EINTR) {
  720. if (!m_was_interrupted) {
  721. if (m_was_resized)
  722. return {};
  723. finish();
  724. return {};
  725. }
  726. handle_interrupt_event();
  727. return {};
  728. }
  729. ScopedValueRollback errno_restorer(errno);
  730. perror("read failed");
  731. m_input_error = Error::ReadFailure;
  732. finish();
  733. return {};
  734. }
  735. m_incomplete_data.append(keybuf, nread);
  736. auto available_bytes = m_incomplete_data.size();
  737. if (available_bytes == 0) {
  738. m_input_error = Error::Empty;
  739. finish();
  740. return {};
  741. }
  742. auto reverse_tab = false;
  743. // Discard starting bytes until they make sense as utf-8.
  744. size_t valid_bytes = 0;
  745. while (available_bytes > 0) {
  746. Utf8View { StringView { m_incomplete_data.data(), available_bytes } }.validate(valid_bytes);
  747. if (valid_bytes != 0)
  748. break;
  749. m_incomplete_data.take_first();
  750. --available_bytes;
  751. }
  752. Utf8View input_view { StringView { m_incomplete_data.data(), valid_bytes } };
  753. size_t consumed_code_points = 0;
  754. static Vector<u8, 4> csi_parameter_bytes;
  755. static Vector<u8> csi_intermediate_bytes;
  756. Vector<unsigned, 4> csi_parameters;
  757. u8 csi_final;
  758. enum CSIMod {
  759. Shift = 1,
  760. Alt = 2,
  761. Ctrl = 4,
  762. };
  763. for (auto code_point : input_view) {
  764. if (m_finish)
  765. break;
  766. ++consumed_code_points;
  767. if (code_point == 0)
  768. continue;
  769. switch (m_state) {
  770. case InputState::GotEscape:
  771. switch (code_point) {
  772. case '[':
  773. m_state = InputState::CSIExpectParameter;
  774. continue;
  775. default: {
  776. m_callback_machine.key_pressed(*this, { code_point, Key::Alt });
  777. m_state = InputState::Free;
  778. TRY(cleanup_suggestions());
  779. continue;
  780. }
  781. }
  782. case InputState::CSIExpectParameter:
  783. if (code_point >= 0x30 && code_point <= 0x3f) { // '0123456789:;<=>?'
  784. csi_parameter_bytes.append(code_point);
  785. continue;
  786. }
  787. m_state = InputState::CSIExpectIntermediate;
  788. [[fallthrough]];
  789. case InputState::CSIExpectIntermediate:
  790. if (code_point >= 0x20 && code_point <= 0x2f) { // ' !"#$%&\'()*+,-./'
  791. csi_intermediate_bytes.append(code_point);
  792. continue;
  793. }
  794. m_state = InputState::CSIExpectFinal;
  795. [[fallthrough]];
  796. case InputState::CSIExpectFinal: {
  797. m_state = m_previous_free_state;
  798. auto is_in_paste = m_state == InputState::Paste;
  799. for (auto& parameter : DeprecatedString::copy(csi_parameter_bytes).split(';')) {
  800. if (auto value = parameter.to_uint(); value.has_value())
  801. csi_parameters.append(value.value());
  802. else
  803. csi_parameters.append(0);
  804. }
  805. unsigned param1 = 0, param2 = 0;
  806. if (csi_parameters.size() >= 1)
  807. param1 = csi_parameters[0];
  808. if (csi_parameters.size() >= 2)
  809. param2 = csi_parameters[1];
  810. unsigned modifiers = param2 ? param2 - 1 : 0;
  811. if (is_in_paste && code_point != '~' && param1 != 201) {
  812. // The only valid escape to process in paste mode is the stop-paste sequence.
  813. // so treat everything else as part of the pasted data.
  814. insert('\x1b');
  815. insert('[');
  816. insert(StringView { csi_parameter_bytes.data(), csi_parameter_bytes.size() });
  817. insert(StringView { csi_intermediate_bytes.data(), csi_intermediate_bytes.size() });
  818. insert(code_point);
  819. continue;
  820. }
  821. if (!(code_point >= 0x40 && code_point <= 0x7f)) {
  822. dbgln("LibLine: Invalid CSI: {:02x} ({:c})", code_point, code_point);
  823. continue;
  824. }
  825. csi_final = code_point;
  826. csi_parameters.clear();
  827. csi_parameter_bytes.clear();
  828. csi_intermediate_bytes.clear();
  829. if (csi_final == 'Z') {
  830. // 'reverse tab'
  831. reverse_tab = true;
  832. break;
  833. }
  834. TRY(cleanup_suggestions());
  835. switch (csi_final) {
  836. case 'A': // ^[[A: arrow up
  837. search_backwards();
  838. continue;
  839. case 'B': // ^[[B: arrow down
  840. search_forwards();
  841. continue;
  842. case 'D': // ^[[D: arrow left
  843. if (modifiers == CSIMod::Alt || modifiers == CSIMod::Ctrl)
  844. cursor_left_word();
  845. else
  846. cursor_left_character();
  847. continue;
  848. case 'C': // ^[[C: arrow right
  849. if (modifiers == CSIMod::Alt || modifiers == CSIMod::Ctrl)
  850. cursor_right_word();
  851. else
  852. cursor_right_character();
  853. continue;
  854. case 'H': // ^[[H: home
  855. go_home();
  856. continue;
  857. case 'F': // ^[[F: end
  858. go_end();
  859. continue;
  860. case 127:
  861. if (modifiers == CSIMod::Ctrl)
  862. erase_alnum_word_backwards();
  863. else
  864. erase_character_backwards();
  865. continue;
  866. case '~':
  867. if (param1 == 3) { // ^[[3~: delete
  868. if (modifiers == CSIMod::Ctrl)
  869. erase_alnum_word_forwards();
  870. else
  871. erase_character_forwards();
  872. m_search_offset = 0;
  873. continue;
  874. }
  875. if (m_configuration.enable_bracketed_paste) {
  876. // ^[[200~: start bracketed paste
  877. // ^[[201~: end bracketed paste
  878. if (!is_in_paste && param1 == 200) {
  879. m_state = InputState::Paste;
  880. continue;
  881. }
  882. if (is_in_paste && param1 == 201) {
  883. m_state = InputState::Free;
  884. if (on_paste) {
  885. on_paste(Utf32View { m_paste_buffer.data(), m_paste_buffer.size() }, *this);
  886. m_paste_buffer.clear_with_capacity();
  887. }
  888. if (!m_paste_buffer.is_empty())
  889. insert(Utf32View { m_paste_buffer.data(), m_paste_buffer.size() });
  890. continue;
  891. }
  892. }
  893. // ^[[5~: page up
  894. // ^[[6~: page down
  895. dbgln("LibLine: Unhandled '~': {}", param1);
  896. continue;
  897. default:
  898. dbgln("LibLine: Unhandled final: {:02x} ({:c})", code_point, code_point);
  899. continue;
  900. }
  901. VERIFY_NOT_REACHED();
  902. }
  903. case InputState::Verbatim:
  904. m_state = InputState::Free;
  905. // Verbatim mode will bypass all mechanisms and just insert the code point.
  906. insert(code_point);
  907. continue;
  908. case InputState::Paste:
  909. if (code_point == 27) {
  910. m_previous_free_state = InputState::Paste;
  911. m_state = InputState::GotEscape;
  912. continue;
  913. }
  914. if (on_paste)
  915. m_paste_buffer.append(code_point);
  916. else
  917. insert(code_point);
  918. continue;
  919. case InputState::Free:
  920. m_previous_free_state = InputState::Free;
  921. if (code_point == 27) {
  922. m_callback_machine.key_pressed(*this, code_point);
  923. // Note that this should also deal with explicitly registered keys
  924. // that would otherwise be interpreted as escapes.
  925. if (m_callback_machine.should_process_last_pressed_key())
  926. m_state = InputState::GotEscape;
  927. continue;
  928. }
  929. if (code_point == 22) { // ^v
  930. m_callback_machine.key_pressed(*this, code_point);
  931. if (m_callback_machine.should_process_last_pressed_key())
  932. m_state = InputState::Verbatim;
  933. continue;
  934. }
  935. break;
  936. }
  937. // There are no sequences past this point, so short of 'tab', we will want to cleanup the suggestions.
  938. ArmedScopeGuard suggestion_cleanup { [this] { cleanup_suggestions().release_value_but_fixme_should_propagate_errors(); } };
  939. // Normally ^D. `stty eof \^n` can change it to ^N (or something else), but Serenity doesn't have `stty` yet.
  940. // Process this here since the keybinds might override its behavior.
  941. // This only applies when the buffer is empty. at any other time, the behavior should be configurable.
  942. if (code_point == m_termios.c_cc[VEOF] && m_buffer.size() == 0) {
  943. finish_edit();
  944. continue;
  945. }
  946. m_callback_machine.key_pressed(*this, code_point);
  947. if (!m_callback_machine.should_process_last_pressed_key())
  948. continue;
  949. m_search_offset = 0; // reset search offset on any key
  950. if (code_point == '\t' || reverse_tab) {
  951. suggestion_cleanup.disarm();
  952. if (!on_tab_complete)
  953. continue;
  954. // Reverse tab can count as regular tab here.
  955. m_times_tab_pressed++;
  956. int token_start = m_cursor;
  957. // Ask for completions only on the first tab
  958. // and scan for the largest common prefix to display,
  959. // further tabs simply show the cached completions.
  960. if (m_times_tab_pressed == 1) {
  961. m_suggestion_manager.set_suggestions(on_tab_complete(*this));
  962. m_suggestion_manager.set_start_index(0);
  963. m_prompt_lines_at_suggestion_initiation = num_lines();
  964. if (m_suggestion_manager.count() == 0) {
  965. // There are no suggestions, beep.
  966. fputc('\a', stderr);
  967. fflush(stderr);
  968. }
  969. }
  970. // Adjust already incremented / decremented index when switching tab direction.
  971. if (reverse_tab && m_tab_direction != TabDirection::Backward) {
  972. m_suggestion_manager.previous();
  973. m_suggestion_manager.previous();
  974. m_tab_direction = TabDirection::Backward;
  975. }
  976. if (!reverse_tab && m_tab_direction != TabDirection::Forward) {
  977. m_suggestion_manager.next();
  978. m_suggestion_manager.next();
  979. m_tab_direction = TabDirection::Forward;
  980. }
  981. reverse_tab = false;
  982. SuggestionManager::CompletionMode completion_mode;
  983. switch (m_times_tab_pressed) {
  984. case 1:
  985. completion_mode = SuggestionManager::CompletePrefix;
  986. break;
  987. case 2:
  988. completion_mode = SuggestionManager::ShowSuggestions;
  989. break;
  990. default:
  991. completion_mode = SuggestionManager::CycleSuggestions;
  992. break;
  993. }
  994. insert(Utf32View { m_remembered_suggestion_static_data.data(), m_remembered_suggestion_static_data.size() });
  995. m_remembered_suggestion_static_data.clear_with_capacity();
  996. auto completion_result = m_suggestion_manager.attempt_completion(completion_mode, token_start);
  997. auto new_cursor = m_cursor;
  998. new_cursor += completion_result.new_cursor_offset;
  999. for (size_t i = completion_result.offset_region_to_remove.start; i < completion_result.offset_region_to_remove.end; ++i)
  1000. remove_at_index(new_cursor);
  1001. new_cursor -= completion_result.static_offset_from_cursor;
  1002. for (size_t i = 0; i < completion_result.static_offset_from_cursor; ++i) {
  1003. m_remembered_suggestion_static_data.append(m_buffer[new_cursor]);
  1004. remove_at_index(new_cursor);
  1005. }
  1006. m_cursor = new_cursor;
  1007. m_inline_search_cursor = new_cursor;
  1008. m_refresh_needed = true;
  1009. m_chars_touched_in_the_middle++;
  1010. for (auto& view : completion_result.insert)
  1011. insert(view);
  1012. auto stderr_stream = TRY(Core::File::standard_error());
  1013. TRY(reposition_cursor(*stderr_stream));
  1014. if (completion_result.style_to_apply.has_value()) {
  1015. // Apply the style of the last suggestion.
  1016. readjust_anchored_styles(m_suggestion_manager.current_suggestion().start_index, ModificationKind::ForcedOverlapRemoval);
  1017. stylize({ m_suggestion_manager.current_suggestion().start_index, m_cursor, Span::Mode::CodepointOriented }, completion_result.style_to_apply.value());
  1018. }
  1019. switch (completion_result.new_completion_mode) {
  1020. case SuggestionManager::DontComplete:
  1021. m_times_tab_pressed = 0;
  1022. m_remembered_suggestion_static_data.clear_with_capacity();
  1023. break;
  1024. case SuggestionManager::CompletePrefix:
  1025. break;
  1026. default:
  1027. ++m_times_tab_pressed;
  1028. break;
  1029. }
  1030. if (m_times_tab_pressed > 1 && m_suggestion_manager.count() > 0) {
  1031. if (TRY(m_suggestion_display->cleanup()))
  1032. TRY(reposition_cursor(*stderr_stream));
  1033. m_suggestion_display->set_initial_prompt_lines(m_prompt_lines_at_suggestion_initiation);
  1034. TRY(m_suggestion_display->display(m_suggestion_manager));
  1035. m_origin_row = m_suggestion_display->origin_row();
  1036. }
  1037. if (m_times_tab_pressed > 2) {
  1038. if (m_tab_direction == TabDirection::Forward)
  1039. m_suggestion_manager.next();
  1040. else
  1041. m_suggestion_manager.previous();
  1042. }
  1043. if (m_suggestion_manager.count() < 2 && !completion_result.avoid_committing_to_single_suggestion) {
  1044. // We have none, or just one suggestion,
  1045. // we should just commit that and continue
  1046. // after it, as if it were auto-completed.
  1047. TRY(reposition_cursor(*stderr_stream, true));
  1048. TRY(cleanup_suggestions());
  1049. m_remembered_suggestion_static_data.clear_with_capacity();
  1050. }
  1051. continue;
  1052. }
  1053. // If we got here, manually cleanup the suggestions and then insert the new code point.
  1054. m_remembered_suggestion_static_data.clear_with_capacity();
  1055. suggestion_cleanup.disarm();
  1056. TRY(cleanup_suggestions());
  1057. insert(code_point);
  1058. }
  1059. if (consumed_code_points == m_incomplete_data.size()) {
  1060. m_incomplete_data.clear();
  1061. } else {
  1062. for (size_t i = 0; i < consumed_code_points; ++i)
  1063. m_incomplete_data.take_first();
  1064. }
  1065. if (!m_incomplete_data.is_empty() && !m_finish)
  1066. deferred_invoke([&] { try_update_once().release_value_but_fixme_should_propagate_errors(); });
  1067. return {};
  1068. }
  1069. ErrorOr<void> Editor::cleanup_suggestions()
  1070. {
  1071. if (m_times_tab_pressed != 0) {
  1072. // Apply the style of the last suggestion.
  1073. readjust_anchored_styles(m_suggestion_manager.current_suggestion().start_index, ModificationKind::ForcedOverlapRemoval);
  1074. stylize({ m_suggestion_manager.current_suggestion().start_index, m_cursor, Span::Mode::CodepointOriented }, m_suggestion_manager.current_suggestion().style);
  1075. // We probably have some suggestions drawn,
  1076. // let's clean them up.
  1077. if (TRY(m_suggestion_display->cleanup())) {
  1078. auto stderr_stream = TRY(Core::File::standard_error());
  1079. TRY(reposition_cursor(*stderr_stream));
  1080. m_refresh_needed = true;
  1081. }
  1082. m_suggestion_manager.reset();
  1083. m_suggestion_display->finish();
  1084. }
  1085. m_times_tab_pressed = 0; // Safe to say if we get here, the user didn't press TAB
  1086. return {};
  1087. }
  1088. bool Editor::search(StringView phrase, bool allow_empty, bool from_beginning)
  1089. {
  1090. int last_matching_offset = -1;
  1091. bool found = false;
  1092. // Do not search for empty strings.
  1093. if (allow_empty || phrase.length() > 0) {
  1094. size_t search_offset = m_search_offset;
  1095. for (size_t i = m_history_cursor; i > 0; --i) {
  1096. auto& entry = m_history[i - 1];
  1097. auto contains = from_beginning ? entry.entry.starts_with(phrase) : entry.entry.contains(phrase);
  1098. if (contains) {
  1099. last_matching_offset = i - 1;
  1100. if (search_offset == 0) {
  1101. found = true;
  1102. break;
  1103. }
  1104. --search_offset;
  1105. }
  1106. }
  1107. if (!found) {
  1108. fputc('\a', stderr);
  1109. fflush(stderr);
  1110. }
  1111. }
  1112. if (found) {
  1113. // We plan to clear the buffer, so mark the entire thing touched.
  1114. m_chars_touched_in_the_middle = m_buffer.size();
  1115. m_buffer.clear();
  1116. m_cursor = 0;
  1117. insert(m_history[last_matching_offset].entry);
  1118. // Always needed, as we have cleared the buffer above.
  1119. m_refresh_needed = true;
  1120. }
  1121. return found;
  1122. }
  1123. void Editor::recalculate_origin()
  1124. {
  1125. // Changing the columns can affect our origin if
  1126. // the new size is smaller than our prompt, which would
  1127. // cause said prompt to take up more space, so we should
  1128. // compensate for that.
  1129. if (m_cached_prompt_metrics.max_line_length >= m_num_columns) {
  1130. auto added_lines = (m_cached_prompt_metrics.max_line_length + 1) / m_num_columns - 1;
  1131. m_origin_row += added_lines;
  1132. }
  1133. // We also need to recalculate our cursor position,
  1134. // but that will be calculated and applied at the next
  1135. // refresh cycle.
  1136. }
  1137. ErrorOr<void> Editor::cleanup()
  1138. {
  1139. auto current_buffer_metrics = actual_rendered_string_metrics(buffer_view(), m_current_masks);
  1140. auto new_lines = current_prompt_metrics().lines_with_addition(current_buffer_metrics, m_num_columns);
  1141. if (new_lines < m_shown_lines)
  1142. m_extra_forward_lines = max(m_shown_lines - new_lines, m_extra_forward_lines);
  1143. auto stderr_stream = TRY(Core::File::standard_error());
  1144. TRY(reposition_cursor(*stderr_stream, true));
  1145. auto current_line = num_lines() - 1;
  1146. TRY(VT::clear_lines(current_line, m_extra_forward_lines, *stderr_stream));
  1147. m_extra_forward_lines = 0;
  1148. TRY(reposition_cursor(*stderr_stream));
  1149. return {};
  1150. }
  1151. ErrorOr<void> Editor::refresh_display()
  1152. {
  1153. AllocatingMemoryStream output_stream;
  1154. ScopeGuard flush_stream {
  1155. [&] {
  1156. m_shown_lines = current_prompt_metrics().lines_with_addition(m_cached_buffer_metrics, m_num_columns);
  1157. if (output_stream.used_buffer_size() == 0)
  1158. return;
  1159. auto buffer = ByteBuffer::create_uninitialized(output_stream.used_buffer_size()).release_value_but_fixme_should_propagate_errors();
  1160. output_stream.read_until_filled(buffer).release_value_but_fixme_should_propagate_errors();
  1161. fwrite(buffer.data(), sizeof(char), buffer.size(), stderr);
  1162. }
  1163. };
  1164. auto has_cleaned_up = false;
  1165. // Someone changed the window size, figure it out
  1166. // and react to it, we might need to redraw.
  1167. if (m_was_resized) {
  1168. if (m_previous_num_columns != m_num_columns) {
  1169. // We need to cleanup and redo everything.
  1170. m_cached_prompt_valid = false;
  1171. m_refresh_needed = true;
  1172. swap(m_previous_num_columns, m_num_columns);
  1173. recalculate_origin();
  1174. TRY(cleanup());
  1175. swap(m_previous_num_columns, m_num_columns);
  1176. has_cleaned_up = true;
  1177. }
  1178. m_was_resized = false;
  1179. }
  1180. // We might be at the last line, and have more than one line;
  1181. // Refreshing the display will cause the terminal to scroll,
  1182. // so note that fact and bring origin up, making sure to
  1183. // reserve the space for however many lines we move it up.
  1184. auto current_num_lines = num_lines();
  1185. if (m_origin_row + current_num_lines > m_num_lines) {
  1186. if (current_num_lines > m_num_lines) {
  1187. for (size_t i = 0; i < m_num_lines; ++i)
  1188. TRY(output_stream.write_until_depleted("\n"sv.bytes()));
  1189. m_origin_row = 0;
  1190. } else {
  1191. auto old_origin_row = m_origin_row;
  1192. m_origin_row = m_num_lines - current_num_lines + 1;
  1193. for (size_t i = 0; i < old_origin_row - m_origin_row; ++i)
  1194. TRY(output_stream.write_until_depleted("\n"sv.bytes()));
  1195. }
  1196. }
  1197. // Do not call hook on pure cursor movement.
  1198. if (m_cached_prompt_valid && !m_refresh_needed && m_pending_chars.size() == 0) {
  1199. // Probably just moving around.
  1200. TRY(reposition_cursor(output_stream));
  1201. m_cached_buffer_metrics = actual_rendered_string_metrics(buffer_view(), m_current_masks);
  1202. m_drawn_end_of_line_offset = m_buffer.size();
  1203. return {};
  1204. }
  1205. if (on_display_refresh)
  1206. on_display_refresh(*this);
  1207. if (m_cached_prompt_valid) {
  1208. if (!m_refresh_needed && m_cursor == m_buffer.size()) {
  1209. // Just write the characters out and continue,
  1210. // no need to refresh the entire line.
  1211. TRY(output_stream.write_until_depleted(m_pending_chars));
  1212. m_pending_chars.clear();
  1213. m_drawn_cursor = m_cursor;
  1214. m_drawn_end_of_line_offset = m_buffer.size();
  1215. m_cached_buffer_metrics = actual_rendered_string_metrics(buffer_view(), m_current_masks);
  1216. m_drawn_spans = m_current_spans;
  1217. return {};
  1218. }
  1219. }
  1220. auto apply_styles = [&, empty_styles = HashMap<u32, Style> {}](size_t i) -> ErrorOr<void> {
  1221. auto& ends = m_current_spans.m_spans_ending.get(i).value_or<>(empty_styles);
  1222. auto& starts = m_current_spans.m_spans_starting.get(i).value_or<>(empty_styles);
  1223. auto& anchored_ends = m_current_spans.m_anchored_spans_ending.get(i).value_or<>(empty_styles);
  1224. auto& anchored_starts = m_current_spans.m_anchored_spans_starting.get(i).value_or<>(empty_styles);
  1225. if (ends.size() || anchored_ends.size()) {
  1226. Style style;
  1227. for (auto& applicable_style : ends)
  1228. style.unify_with(applicable_style.value);
  1229. for (auto& applicable_style : anchored_ends)
  1230. style.unify_with(applicable_style.value);
  1231. // Disable any style that should be turned off.
  1232. TRY(VT::apply_style(style, output_stream, false));
  1233. // Reapply styles for overlapping spans that include this one.
  1234. style = find_applicable_style(i);
  1235. TRY(VT::apply_style(style, output_stream, true));
  1236. }
  1237. if (starts.size() || anchored_starts.size()) {
  1238. Style style;
  1239. for (auto& applicable_style : starts)
  1240. style.unify_with(applicable_style.value);
  1241. for (auto& applicable_style : anchored_starts)
  1242. style.unify_with(applicable_style.value);
  1243. // Set new styles.
  1244. TRY(VT::apply_style(style, output_stream, true));
  1245. }
  1246. return {};
  1247. };
  1248. auto print_character_at = [&](size_t i) {
  1249. Variant<u32, Utf8View> c { Utf8View {} };
  1250. if (auto it = m_current_masks.find_largest_not_above_iterator(i); !it.is_end() && it->has_value()) {
  1251. auto offset = i - it.key();
  1252. if (it->value().mode == Style::Mask::Mode::ReplaceEntireSelection) {
  1253. auto& mask = it->value().replacement_view;
  1254. auto replacement = mask.begin().peek(offset);
  1255. if (!replacement.has_value())
  1256. return;
  1257. c = replacement.value();
  1258. ++it;
  1259. u32 next_offset = it.is_end() ? m_drawn_end_of_line_offset : it.key();
  1260. if (i + 1 == next_offset)
  1261. c = mask.unicode_substring_view(offset, mask.length() - offset);
  1262. } else {
  1263. c = it->value().replacement_view;
  1264. }
  1265. } else {
  1266. c = m_buffer[i];
  1267. }
  1268. auto print_single_character = [&](auto c) -> ErrorOr<void> {
  1269. StringBuilder builder;
  1270. bool should_print_masked = is_ascii_control(c) && c != '\n';
  1271. bool should_print_caret = c < 64 && should_print_masked;
  1272. if (should_print_caret)
  1273. builder.appendff("^{:c}", c + 64);
  1274. else if (should_print_masked)
  1275. builder.appendff("\\x{:0>2x}", c);
  1276. else
  1277. builder.append(Utf32View { &c, 1 });
  1278. if (should_print_masked)
  1279. TRY(output_stream.write_until_depleted("\033[7m"sv.bytes()));
  1280. TRY(output_stream.write_until_depleted(builder.string_view().bytes()));
  1281. if (should_print_masked)
  1282. TRY(output_stream.write_until_depleted("\033[27m"sv.bytes()));
  1283. return {};
  1284. };
  1285. c.visit(
  1286. [&](u32 c) { print_single_character(c).release_value_but_fixme_should_propagate_errors(); },
  1287. [&](auto& view) { for (auto c : view) print_single_character(c).release_value_but_fixme_should_propagate_errors(); });
  1288. };
  1289. // If there have been no changes to previous sections of the line (style or text)
  1290. // just append the new text with the appropriate styles.
  1291. 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)) {
  1292. auto initial_style = find_applicable_style(m_drawn_end_of_line_offset);
  1293. TRY(VT::apply_style(initial_style, output_stream));
  1294. for (size_t i = m_drawn_end_of_line_offset; i < m_buffer.size(); ++i) {
  1295. TRY(apply_styles(i));
  1296. print_character_at(i);
  1297. }
  1298. TRY(VT::apply_style(Style::reset_style(), output_stream));
  1299. m_pending_chars.clear();
  1300. m_refresh_needed = false;
  1301. m_cached_buffer_metrics = actual_rendered_string_metrics(buffer_view(), m_current_masks);
  1302. m_chars_touched_in_the_middle = 0;
  1303. m_drawn_cursor = m_cursor;
  1304. m_drawn_end_of_line_offset = m_buffer.size();
  1305. // No need to reposition the cursor, the cursor is already where it needs to be.
  1306. return {};
  1307. }
  1308. if constexpr (LINE_EDITOR_DEBUG) {
  1309. if (m_cached_prompt_valid && m_chars_touched_in_the_middle == 0) {
  1310. auto x = m_drawn_spans.contains_up_to_offset(m_current_spans, m_drawn_cursor);
  1311. dbgln("Contains: {} At offset: {}", x, m_drawn_cursor);
  1312. dbgln("Drawn Spans:");
  1313. for (auto& sentry : m_drawn_spans.m_spans_starting) {
  1314. for (auto& entry : sentry.value) {
  1315. dbgln("{}-{}: {}", sentry.key, entry.key, entry.value.to_deprecated_string());
  1316. }
  1317. }
  1318. dbgln("==========================================================================");
  1319. dbgln("Current Spans:");
  1320. for (auto& sentry : m_current_spans.m_spans_starting) {
  1321. for (auto& entry : sentry.value) {
  1322. dbgln("{}-{}: {}", sentry.key, entry.key, entry.value.to_deprecated_string());
  1323. }
  1324. }
  1325. }
  1326. }
  1327. // Ouch, reflow entire line.
  1328. if (!has_cleaned_up) {
  1329. TRY(cleanup());
  1330. }
  1331. TRY(VT::move_absolute(m_origin_row, m_origin_column, output_stream));
  1332. TRY(output_stream.write_until_depleted(m_new_prompt.bytes()));
  1333. TRY(VT::clear_to_end_of_line(output_stream));
  1334. StringBuilder builder;
  1335. for (size_t i = 0; i < m_buffer.size(); ++i) {
  1336. TRY(apply_styles(i));
  1337. print_character_at(i);
  1338. }
  1339. TRY(VT::apply_style(Style::reset_style(), output_stream)); // don't bleed to EOL
  1340. m_pending_chars.clear();
  1341. m_refresh_needed = false;
  1342. m_cached_buffer_metrics = actual_rendered_string_metrics(buffer_view(), m_current_masks);
  1343. m_chars_touched_in_the_middle = 0;
  1344. m_drawn_spans = m_current_spans;
  1345. m_drawn_end_of_line_offset = m_buffer.size();
  1346. m_cached_prompt_valid = true;
  1347. TRY(reposition_cursor(output_stream));
  1348. return {};
  1349. }
  1350. void Editor::strip_styles(bool strip_anchored)
  1351. {
  1352. m_current_spans.m_spans_starting.clear();
  1353. m_current_spans.m_spans_ending.clear();
  1354. m_current_masks.clear();
  1355. m_cached_buffer_metrics = actual_rendered_string_metrics(buffer_view(), {});
  1356. if (strip_anchored) {
  1357. m_current_spans.m_anchored_spans_starting.clear();
  1358. m_current_spans.m_anchored_spans_ending.clear();
  1359. }
  1360. m_refresh_needed = true;
  1361. }
  1362. ErrorOr<void> Editor::reposition_cursor(Stream& stream, bool to_end)
  1363. {
  1364. auto cursor = m_cursor;
  1365. auto saved_cursor = m_cursor;
  1366. if (to_end)
  1367. cursor = m_buffer.size();
  1368. m_cursor = cursor;
  1369. m_drawn_cursor = cursor;
  1370. auto line = cursor_line() - 1;
  1371. auto column = offset_in_line();
  1372. ensure_free_lines_from_origin(line);
  1373. VERIFY(column + m_origin_column <= m_num_columns);
  1374. TRY(VT::move_absolute(line + m_origin_row, column + m_origin_column, stream));
  1375. m_cursor = saved_cursor;
  1376. return {};
  1377. }
  1378. ErrorOr<void> VT::move_absolute(u32 row, u32 col, Stream& stream)
  1379. {
  1380. return stream.write_until_depleted(DeprecatedString::formatted("\033[{};{}H", row, col).bytes());
  1381. }
  1382. ErrorOr<void> VT::move_relative(int row, int col, Stream& stream)
  1383. {
  1384. char x_op = 'A', y_op = 'D';
  1385. if (row > 0)
  1386. x_op = 'B';
  1387. else
  1388. row = -row;
  1389. if (col > 0)
  1390. y_op = 'C';
  1391. else
  1392. col = -col;
  1393. if (row > 0)
  1394. TRY(stream.write_until_depleted(DeprecatedString::formatted("\033[{}{}", row, x_op).bytes()));
  1395. if (col > 0)
  1396. TRY(stream.write_until_depleted(DeprecatedString::formatted("\033[{}{}", col, y_op).bytes()));
  1397. return {};
  1398. }
  1399. Style Editor::find_applicable_style(size_t offset) const
  1400. {
  1401. // Walk through our styles and merge all that fit in the offset.
  1402. auto style = Style::reset_style();
  1403. auto unify = [&](auto& entry) {
  1404. if (entry.key >= offset)
  1405. return;
  1406. for (auto& style_value : entry.value) {
  1407. if (style_value.key <= offset)
  1408. return;
  1409. style.unify_with(style_value.value, true);
  1410. }
  1411. };
  1412. for (auto& entry : m_current_spans.m_spans_starting) {
  1413. unify(entry);
  1414. }
  1415. for (auto& entry : m_current_spans.m_anchored_spans_starting) {
  1416. unify(entry);
  1417. }
  1418. return style;
  1419. }
  1420. DeprecatedString Style::Background::to_vt_escape() const
  1421. {
  1422. if (is_default())
  1423. return "";
  1424. if (m_is_rgb) {
  1425. return DeprecatedString::formatted("\e[48;2;{};{};{}m", m_rgb_color[0], m_rgb_color[1], m_rgb_color[2]);
  1426. } else {
  1427. return DeprecatedString::formatted("\e[{}m", (u8)m_xterm_color + 40);
  1428. }
  1429. }
  1430. DeprecatedString Style::Foreground::to_vt_escape() const
  1431. {
  1432. if (is_default())
  1433. return "";
  1434. if (m_is_rgb) {
  1435. return DeprecatedString::formatted("\e[38;2;{};{};{}m", m_rgb_color[0], m_rgb_color[1], m_rgb_color[2]);
  1436. } else {
  1437. return DeprecatedString::formatted("\e[{}m", (u8)m_xterm_color + 30);
  1438. }
  1439. }
  1440. DeprecatedString Style::Hyperlink::to_vt_escape(bool starting) const
  1441. {
  1442. if (is_empty())
  1443. return "";
  1444. return DeprecatedString::formatted("\e]8;;{}\e\\", starting ? m_link : DeprecatedString::empty());
  1445. }
  1446. void Style::unify_with(Style const& other, bool prefer_other)
  1447. {
  1448. // Unify colors.
  1449. if (prefer_other || m_background.is_default())
  1450. m_background = other.background();
  1451. if (prefer_other || m_foreground.is_default())
  1452. m_foreground = other.foreground();
  1453. // Unify graphic renditions.
  1454. if (other.bold())
  1455. set(Bold);
  1456. if (other.italic())
  1457. set(Italic);
  1458. if (other.underline())
  1459. set(Underline);
  1460. // Unify links.
  1461. if (prefer_other || m_hyperlink.is_empty())
  1462. m_hyperlink = other.hyperlink();
  1463. m_is_empty &= other.m_is_empty;
  1464. }
  1465. DeprecatedString Style::to_deprecated_string() const
  1466. {
  1467. StringBuilder builder;
  1468. builder.append("Style { "sv);
  1469. if (!m_foreground.is_default()) {
  1470. builder.append("Foreground("sv);
  1471. if (m_foreground.m_is_rgb) {
  1472. builder.join(", "sv, m_foreground.m_rgb_color);
  1473. } else {
  1474. builder.appendff("(XtermColor) {}", (int)m_foreground.m_xterm_color);
  1475. }
  1476. builder.append("), "sv);
  1477. }
  1478. if (!m_background.is_default()) {
  1479. builder.append("Background("sv);
  1480. if (m_background.m_is_rgb) {
  1481. builder.join(' ', m_background.m_rgb_color);
  1482. } else {
  1483. builder.appendff("(XtermColor) {}", (int)m_background.m_xterm_color);
  1484. }
  1485. builder.append("), "sv);
  1486. }
  1487. if (bold())
  1488. builder.append("Bold, "sv);
  1489. if (underline())
  1490. builder.append("Underline, "sv);
  1491. if (italic())
  1492. builder.append("Italic, "sv);
  1493. if (!m_hyperlink.is_empty())
  1494. builder.appendff("Hyperlink(\"{}\"), ", m_hyperlink.m_link);
  1495. if (!m_mask.has_value()) {
  1496. builder.appendff("Mask(\"{}\", {}), ",
  1497. m_mask->replacement,
  1498. m_mask->mode == Mask::Mode::ReplaceEntireSelection
  1499. ? "ReplaceEntireSelection"
  1500. : "ReplaceEachCodePointInSelection");
  1501. }
  1502. builder.append('}');
  1503. return builder.to_deprecated_string();
  1504. }
  1505. ErrorOr<void> VT::apply_style(Style const& style, Stream& stream, bool is_starting)
  1506. {
  1507. if (is_starting) {
  1508. TRY(stream.write_until_depleted(DeprecatedString::formatted("\033[{};{};{}m{}{}{}",
  1509. style.bold() ? 1 : 22,
  1510. style.underline() ? 4 : 24,
  1511. style.italic() ? 3 : 23,
  1512. style.background().to_vt_escape(),
  1513. style.foreground().to_vt_escape(),
  1514. style.hyperlink().to_vt_escape(true))
  1515. .bytes()));
  1516. } else {
  1517. TRY(stream.write_until_depleted(style.hyperlink().to_vt_escape(false).bytes()));
  1518. }
  1519. return {};
  1520. }
  1521. ErrorOr<void> VT::clear_lines(size_t count_above, size_t count_below, Stream& stream)
  1522. {
  1523. if (count_below + count_above == 0) {
  1524. TRY(stream.write_until_depleted("\033[2K"sv.bytes()));
  1525. } else {
  1526. // Go down count_below lines.
  1527. if (count_below > 0)
  1528. TRY(stream.write_until_depleted(DeprecatedString::formatted("\033[{}B", count_below).bytes()));
  1529. // Then clear lines going upwards.
  1530. for (size_t i = count_below + count_above; i > 0; --i) {
  1531. TRY(stream.write_until_depleted("\033[2K"sv.bytes()));
  1532. if (i != 1)
  1533. TRY(stream.write_until_depleted("\033[A"sv.bytes()));
  1534. }
  1535. }
  1536. return {};
  1537. }
  1538. ErrorOr<void> VT::save_cursor(Stream& stream)
  1539. {
  1540. return stream.write_until_depleted("\033[s"sv.bytes());
  1541. }
  1542. ErrorOr<void> VT::restore_cursor(Stream& stream)
  1543. {
  1544. return stream.write_until_depleted("\033[u"sv.bytes());
  1545. }
  1546. ErrorOr<void> VT::clear_to_end_of_line(Stream& stream)
  1547. {
  1548. return stream.write_until_depleted("\033[K"sv.bytes());
  1549. }
  1550. enum VTState {
  1551. Free = 1,
  1552. Escape = 3,
  1553. Bracket = 5,
  1554. BracketArgsSemi = 7,
  1555. Title = 9,
  1556. URL = 11,
  1557. };
  1558. 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 = {});
  1559. enum class MaskedSelectionDecision {
  1560. Skip,
  1561. Continue,
  1562. };
  1563. static MaskedSelectionDecision resolve_masked_selection(Optional<Style::Mask>& mask, size_t& i, auto& mask_it, auto& view, auto& state, auto& metrics, auto& current_line)
  1564. {
  1565. if (mask.has_value() && mask->mode == Style::Mask::Mode::ReplaceEntireSelection) {
  1566. ++mask_it;
  1567. auto actual_end_offset = mask_it.is_end() ? view.length() : mask_it.key();
  1568. auto end_offset = min(actual_end_offset, view.length());
  1569. size_t j = 0;
  1570. for (auto it = mask->replacement_view.begin(); it != mask->replacement_view.end(); ++it) {
  1571. auto it_copy = it;
  1572. ++it_copy;
  1573. auto next_c = it_copy == mask->replacement_view.end() ? 0 : *it_copy;
  1574. state = actual_rendered_string_length_step(metrics, j, current_line, *it, next_c, state, {});
  1575. ++j;
  1576. if (j <= actual_end_offset - i && j + i >= view.length())
  1577. break;
  1578. }
  1579. current_line.masked_chars.empend(i, end_offset - i, j);
  1580. i = end_offset;
  1581. if (mask_it.is_end())
  1582. mask = {};
  1583. else
  1584. mask = *mask_it;
  1585. return MaskedSelectionDecision::Skip;
  1586. }
  1587. return MaskedSelectionDecision::Continue;
  1588. }
  1589. StringMetrics Editor::actual_rendered_string_metrics(StringView string, RedBlackTree<u32, Optional<Style::Mask>> const& masks, Optional<size_t> maximum_line_width)
  1590. {
  1591. StringMetrics metrics;
  1592. StringMetrics::LineMetrics current_line;
  1593. VTState state { Free };
  1594. Utf8View view { string };
  1595. size_t last_return {};
  1596. auto it = view.begin();
  1597. Optional<Style::Mask> mask;
  1598. size_t i = 0;
  1599. auto mask_it = masks.begin();
  1600. for (; it != view.end(); ++it) {
  1601. if (!mask_it.is_end() && mask_it.key() <= i)
  1602. mask = *mask_it;
  1603. auto c = *it;
  1604. auto it_copy = it;
  1605. ++it_copy;
  1606. if (resolve_masked_selection(mask, i, mask_it, view, state, metrics, current_line) == MaskedSelectionDecision::Skip)
  1607. continue;
  1608. auto next_c = it_copy == view.end() ? 0 : *it_copy;
  1609. state = actual_rendered_string_length_step(metrics, view.iterator_offset(it), current_line, c, next_c, state, mask, maximum_line_width, last_return);
  1610. if (!mask_it.is_end() && mask_it.key() <= i) {
  1611. auto mask_it_peek = mask_it;
  1612. ++mask_it_peek;
  1613. if (!mask_it_peek.is_end() && mask_it_peek.key() > i)
  1614. mask_it = mask_it_peek;
  1615. }
  1616. ++i;
  1617. }
  1618. metrics.line_metrics.append(current_line);
  1619. for (auto& line : metrics.line_metrics)
  1620. metrics.max_line_length = max(line.total_length(), metrics.max_line_length);
  1621. return metrics;
  1622. }
  1623. StringMetrics Editor::actual_rendered_string_metrics(Utf32View const& view, RedBlackTree<u32, Optional<Style::Mask>> const& masks)
  1624. {
  1625. StringMetrics metrics;
  1626. StringMetrics::LineMetrics current_line;
  1627. VTState state { Free };
  1628. Optional<Style::Mask> mask;
  1629. auto mask_it = masks.begin();
  1630. for (size_t i = 0; i < view.length(); ++i) {
  1631. auto c = view[i];
  1632. if (!mask_it.is_end() && mask_it.key() <= i)
  1633. mask = *mask_it;
  1634. if (resolve_masked_selection(mask, i, mask_it, view, state, metrics, current_line) == MaskedSelectionDecision::Skip) {
  1635. --i;
  1636. continue;
  1637. }
  1638. auto next_c = i + 1 < view.length() ? view.code_points()[i + 1] : 0;
  1639. state = actual_rendered_string_length_step(metrics, i, current_line, c, next_c, state, mask);
  1640. if (!mask_it.is_end() && mask_it.key() <= i) {
  1641. auto mask_it_peek = mask_it;
  1642. ++mask_it_peek;
  1643. if (!mask_it_peek.is_end() && mask_it_peek.key() > i)
  1644. mask_it = mask_it_peek;
  1645. }
  1646. }
  1647. metrics.line_metrics.append(current_line);
  1648. for (auto& line : metrics.line_metrics)
  1649. metrics.max_line_length = max(line.total_length(), metrics.max_line_length);
  1650. return metrics;
  1651. }
  1652. 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)
  1653. {
  1654. auto const save_line = [&metrics, &current_line, &last_return, &index]() {
  1655. if (last_return.has_value()) {
  1656. auto const last_index = index - 1;
  1657. current_line.bit_length = last_index - *last_return + 1;
  1658. last_return.value() = last_index + 1;
  1659. }
  1660. metrics.line_metrics.append(current_line);
  1661. current_line.masked_chars = {};
  1662. current_line.length = 0;
  1663. current_line.visible_length = 0;
  1664. current_line.bit_length = {};
  1665. };
  1666. // FIXME: current_line.visible_length can go above maximum_line_width when using masks
  1667. if (maximum_line_width.has_value() && current_line.visible_length >= maximum_line_width.value())
  1668. save_line();
  1669. ScopeGuard bit_length_update { [&last_return, &current_line, &index]() {
  1670. if (last_return.has_value())
  1671. current_line.bit_length = index - *last_return + 1;
  1672. } };
  1673. switch (state) {
  1674. case Free: {
  1675. if (c == '\x1b') { // escape
  1676. return Escape;
  1677. }
  1678. if (c == '\r') { // carriage return
  1679. current_line.masked_chars = {};
  1680. current_line.length = 0;
  1681. current_line.visible_length = 0;
  1682. if (!metrics.line_metrics.is_empty())
  1683. metrics.line_metrics.last() = { {}, 0 };
  1684. return state;
  1685. }
  1686. if (c == '\n') { // return
  1687. save_line();
  1688. return state;
  1689. }
  1690. if (c == '\t') {
  1691. // Tabs are a special case, because their width is variable.
  1692. ++current_line.length;
  1693. current_line.visible_length += (8 - (current_line.visible_length % 8));
  1694. return state;
  1695. }
  1696. auto is_control = is_ascii_control(c);
  1697. if (is_control) {
  1698. if (mask.has_value())
  1699. current_line.masked_chars.append({ index, 1, mask->replacement_view.length() });
  1700. else
  1701. current_line.masked_chars.append({ index, 1, c < 64 ? 2u : 4u }); // if the character cannot be represented as ^c, represent it as \xbb.
  1702. }
  1703. // FIXME: This will not support anything sophisticated
  1704. if (mask.has_value()) {
  1705. current_line.length += mask->replacement_view.length();
  1706. current_line.visible_length += mask->replacement_view.length();
  1707. metrics.total_length += mask->replacement_view.length();
  1708. } else if (is_control) {
  1709. current_line.length += current_line.masked_chars.last().masked_length;
  1710. current_line.visible_length += current_line.masked_chars.last().masked_length;
  1711. metrics.total_length += current_line.masked_chars.last().masked_length;
  1712. } else {
  1713. ++current_line.length;
  1714. ++current_line.visible_length;
  1715. ++metrics.total_length;
  1716. }
  1717. return state;
  1718. }
  1719. case Escape:
  1720. if (c == ']') {
  1721. if (next_c == '0')
  1722. state = Title;
  1723. if (next_c == '8')
  1724. state = URL;
  1725. return state;
  1726. }
  1727. if (c == '[') {
  1728. return Bracket;
  1729. }
  1730. // FIXME: This does not support non-VT (aside from set-title) escapes
  1731. return state;
  1732. case Bracket:
  1733. if (is_ascii_digit(c)) {
  1734. return BracketArgsSemi;
  1735. }
  1736. return state;
  1737. case BracketArgsSemi:
  1738. if (c == ';') {
  1739. return Bracket;
  1740. }
  1741. if (!is_ascii_digit(c))
  1742. state = Free;
  1743. return state;
  1744. case Title:
  1745. if (c == 7)
  1746. state = Free;
  1747. return state;
  1748. case URL:
  1749. if (c == '\\')
  1750. state = Free;
  1751. return state;
  1752. }
  1753. return state;
  1754. }
  1755. Result<Vector<size_t, 2>, Editor::Error> Editor::vt_dsr()
  1756. {
  1757. char buf[16];
  1758. // Read whatever junk there is before talking to the terminal
  1759. // and insert them later when we're reading user input.
  1760. bool more_junk_to_read { false };
  1761. timeval timeout { 0, 0 };
  1762. fd_set readfds;
  1763. FD_ZERO(&readfds);
  1764. FD_SET(0, &readfds);
  1765. do {
  1766. more_junk_to_read = false;
  1767. [[maybe_unused]] auto rc = select(1, &readfds, nullptr, nullptr, &timeout);
  1768. if (FD_ISSET(0, &readfds)) {
  1769. auto nread = read(0, buf, 16);
  1770. if (nread < 0) {
  1771. m_input_error = Error::ReadFailure;
  1772. finish();
  1773. break;
  1774. }
  1775. if (nread == 0)
  1776. break;
  1777. m_incomplete_data.append(buf, nread);
  1778. more_junk_to_read = true;
  1779. }
  1780. } while (more_junk_to_read);
  1781. if (m_input_error.has_value())
  1782. return m_input_error.value();
  1783. fputs("\033[6n", stderr);
  1784. fflush(stderr);
  1785. // Parse the DSR response
  1786. // it should be of the form .*\e[\d+;\d+R.*
  1787. // Anything not part of the response is just added to the incomplete data.
  1788. enum {
  1789. Free,
  1790. SawEsc,
  1791. SawBracket,
  1792. InFirstCoordinate,
  1793. SawSemicolon,
  1794. InSecondCoordinate,
  1795. SawR,
  1796. } state { Free };
  1797. auto has_error = false;
  1798. Vector<char, 4> coordinate_buffer;
  1799. size_t row { 1 }, col { 1 };
  1800. do {
  1801. char c;
  1802. auto nread = read(0, &c, 1);
  1803. if (nread < 0) {
  1804. if (errno == 0 || errno == EINTR) {
  1805. // ????
  1806. continue;
  1807. }
  1808. dbgln("Error while reading DSR: {}", strerror(errno));
  1809. return Error::ReadFailure;
  1810. }
  1811. if (nread == 0) {
  1812. dbgln("Terminal DSR issue; received no response");
  1813. return Error::Empty;
  1814. }
  1815. switch (state) {
  1816. case Free:
  1817. if (c == '\x1b') {
  1818. state = SawEsc;
  1819. continue;
  1820. }
  1821. m_incomplete_data.append(c);
  1822. continue;
  1823. case SawEsc:
  1824. if (c == '[') {
  1825. state = SawBracket;
  1826. continue;
  1827. }
  1828. m_incomplete_data.append(c);
  1829. state = Free;
  1830. continue;
  1831. case SawBracket:
  1832. if (is_ascii_digit(c)) {
  1833. state = InFirstCoordinate;
  1834. coordinate_buffer.clear_with_capacity();
  1835. coordinate_buffer.append(c);
  1836. continue;
  1837. }
  1838. m_incomplete_data.append(c);
  1839. state = Free;
  1840. continue;
  1841. case InFirstCoordinate:
  1842. if (is_ascii_digit(c)) {
  1843. coordinate_buffer.append(c);
  1844. continue;
  1845. }
  1846. if (c == ';') {
  1847. auto maybe_row = StringView { coordinate_buffer.data(), coordinate_buffer.size() }.to_uint();
  1848. if (!maybe_row.has_value())
  1849. has_error = true;
  1850. row = maybe_row.value_or(1u);
  1851. coordinate_buffer.clear_with_capacity();
  1852. state = SawSemicolon;
  1853. continue;
  1854. }
  1855. m_incomplete_data.append(c);
  1856. state = Free;
  1857. continue;
  1858. case SawSemicolon:
  1859. if (is_ascii_digit(c)) {
  1860. state = InSecondCoordinate;
  1861. coordinate_buffer.append(c);
  1862. continue;
  1863. }
  1864. m_incomplete_data.append(c);
  1865. state = Free;
  1866. continue;
  1867. case InSecondCoordinate:
  1868. if (is_ascii_digit(c)) {
  1869. coordinate_buffer.append(c);
  1870. continue;
  1871. }
  1872. if (c == 'R') {
  1873. auto maybe_column = StringView { coordinate_buffer.data(), coordinate_buffer.size() }.to_uint();
  1874. if (!maybe_column.has_value())
  1875. has_error = true;
  1876. col = maybe_column.value_or(1u);
  1877. coordinate_buffer.clear_with_capacity();
  1878. state = SawR;
  1879. continue;
  1880. }
  1881. m_incomplete_data.append(c);
  1882. state = Free;
  1883. continue;
  1884. case SawR:
  1885. m_incomplete_data.append(c);
  1886. continue;
  1887. default:
  1888. VERIFY_NOT_REACHED();
  1889. }
  1890. } while (state != SawR);
  1891. if (has_error)
  1892. dbgln("Terminal DSR issue, couldn't parse DSR response");
  1893. return Vector<size_t, 2> { row, col };
  1894. }
  1895. DeprecatedString Editor::line(size_t up_to_index) const
  1896. {
  1897. StringBuilder builder;
  1898. builder.append(Utf32View { m_buffer.data(), min(m_buffer.size(), up_to_index) });
  1899. return builder.to_deprecated_string();
  1900. }
  1901. void Editor::remove_at_index(size_t index)
  1902. {
  1903. // See if we have any anchored styles, and reposition them if needed.
  1904. readjust_anchored_styles(index, ModificationKind::Removal);
  1905. auto cp = m_buffer[index];
  1906. m_buffer.remove(index);
  1907. if (cp == '\n')
  1908. ++m_extra_forward_lines;
  1909. ++m_chars_touched_in_the_middle;
  1910. }
  1911. void Editor::readjust_anchored_styles(size_t hint_index, ModificationKind modification)
  1912. {
  1913. struct Anchor {
  1914. Span old_span;
  1915. Span new_span;
  1916. Style style;
  1917. };
  1918. Vector<Anchor> anchors_to_relocate;
  1919. auto index_shift = modification == ModificationKind::Insertion ? 1 : -1;
  1920. auto forced_removal = modification == ModificationKind::ForcedOverlapRemoval;
  1921. for (auto& start_entry : m_current_spans.m_anchored_spans_starting) {
  1922. for (auto& end_entry : start_entry.value) {
  1923. if (forced_removal) {
  1924. if (start_entry.key <= hint_index && end_entry.key > hint_index) {
  1925. // Remove any overlapping regions.
  1926. continue;
  1927. }
  1928. }
  1929. if (start_entry.key >= hint_index) {
  1930. if (start_entry.key == hint_index && end_entry.key == hint_index + 1 && modification == ModificationKind::Removal) {
  1931. // Remove the anchor, as all its text was wiped.
  1932. continue;
  1933. }
  1934. // Shift everything.
  1935. 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 });
  1936. continue;
  1937. }
  1938. if (end_entry.key > hint_index) {
  1939. // Shift just the end.
  1940. 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 });
  1941. continue;
  1942. }
  1943. 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 });
  1944. }
  1945. }
  1946. m_current_spans.m_anchored_spans_ending.clear();
  1947. m_current_spans.m_anchored_spans_starting.clear();
  1948. // Pass over the relocations and update the stale entries.
  1949. for (auto& relocation : anchors_to_relocate) {
  1950. stylize(relocation.new_span, relocation.style);
  1951. }
  1952. }
  1953. size_t StringMetrics::lines_with_addition(StringMetrics const& offset, size_t column_width) const
  1954. {
  1955. size_t lines = 0;
  1956. if (!line_metrics.is_empty()) {
  1957. for (size_t i = 0; i < line_metrics.size() - 1; ++i)
  1958. lines += (line_metrics[i].total_length() + column_width) / column_width;
  1959. auto last = line_metrics.last().total_length();
  1960. last += offset.line_metrics.first().total_length();
  1961. lines += (last + column_width) / column_width;
  1962. }
  1963. for (size_t i = 1; i < offset.line_metrics.size(); ++i)
  1964. lines += (offset.line_metrics[i].total_length() + column_width) / column_width;
  1965. return lines;
  1966. }
  1967. size_t StringMetrics::offset_with_addition(StringMetrics const& offset, size_t column_width) const
  1968. {
  1969. if (offset.line_metrics.size() > 1)
  1970. return offset.line_metrics.last().total_length() % column_width;
  1971. if (!line_metrics.is_empty()) {
  1972. auto last = line_metrics.last().total_length();
  1973. last += offset.line_metrics.first().total_length();
  1974. return last % column_width;
  1975. }
  1976. if (offset.line_metrics.is_empty())
  1977. return 0;
  1978. return offset.line_metrics.first().total_length() % column_width;
  1979. }
  1980. bool Editor::Spans::contains_up_to_offset(Spans const& other, size_t offset) const
  1981. {
  1982. auto compare = [&]<typename K, typename V>(HashMap<K, HashMap<K, V>> const& left, HashMap<K, HashMap<K, V>> const& right) -> bool {
  1983. for (auto& entry : right) {
  1984. if (entry.key > offset + 1)
  1985. continue;
  1986. auto left_map_it = left.find(entry.key);
  1987. if (left_map_it == left.end())
  1988. return false;
  1989. for (auto& left_entry : left_map_it->value) {
  1990. auto value_it = entry.value.find(left_entry.key);
  1991. if (value_it == entry.value.end()) {
  1992. // Might have the same thing with a longer span
  1993. bool found = false;
  1994. for (auto& possibly_longer_span_entry : entry.value) {
  1995. if (possibly_longer_span_entry.key > left_entry.key && possibly_longer_span_entry.key > offset && left_entry.value == possibly_longer_span_entry.value) {
  1996. found = true;
  1997. break;
  1998. }
  1999. }
  2000. if (found)
  2001. continue;
  2002. if constexpr (LINE_EDITOR_DEBUG) {
  2003. dbgln("Compare for {}-{} failed, no entry", entry.key, left_entry.key);
  2004. for (auto& x : entry.value)
  2005. dbgln("Have: {}-{} = {}", entry.key, x.key, x.value.to_deprecated_string());
  2006. }
  2007. return false;
  2008. } else if (value_it->value != left_entry.value) {
  2009. 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());
  2010. return false;
  2011. }
  2012. }
  2013. }
  2014. return true;
  2015. };
  2016. return compare(m_spans_starting, other.m_spans_starting)
  2017. && compare(m_anchored_spans_starting, other.m_anchored_spans_starting);
  2018. }
  2019. }