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