Editor.cpp 81 KB

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