Editor.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Editor.h"
  27. #include <AK/StringBuilder.h>
  28. #include <AK/Utf32View.h>
  29. #include <AK/Utf8View.h>
  30. #include <ctype.h>
  31. #include <stdio.h>
  32. #include <sys/ioctl.h>
  33. #include <sys/select.h>
  34. #include <sys/time.h>
  35. #include <unistd.h>
  36. // #define SUGGESTIONS_DEBUG
  37. namespace Line {
  38. Editor::Editor(Configuration configuration)
  39. : m_configuration(move(configuration))
  40. {
  41. m_always_refresh = configuration.refresh_behaviour == Configuration::RefreshBehaviour::Eager;
  42. m_pending_chars = ByteBuffer::create_uninitialized(0);
  43. struct winsize ws;
  44. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
  45. m_num_columns = 80;
  46. m_num_lines = 25;
  47. } else {
  48. m_num_columns = ws.ws_col;
  49. m_num_lines = ws.ws_row;
  50. }
  51. m_suggestion_display = make<XtermSuggestionDisplay>(m_num_lines, m_num_columns);
  52. }
  53. Editor::~Editor()
  54. {
  55. if (m_initialized)
  56. restore();
  57. }
  58. void Editor::add_to_history(const String& line)
  59. {
  60. if ((m_history.size() + 1) > m_history_capacity)
  61. m_history.take_first();
  62. m_history.append(line);
  63. }
  64. void Editor::clear_line()
  65. {
  66. for (size_t i = 0; i < m_cursor; ++i)
  67. fputc(0x8, stdout);
  68. fputs("\033[K", stdout);
  69. fflush(stdout);
  70. m_buffer.clear();
  71. m_cursor = 0;
  72. m_inline_search_cursor = m_cursor;
  73. }
  74. void Editor::insert(const Utf32View& string)
  75. {
  76. for (size_t i = 0; i < string.length(); ++i)
  77. insert(string.codepoints()[i]);
  78. }
  79. void Editor::insert(const String& string)
  80. {
  81. for (auto ch : Utf8View { string })
  82. insert(ch);
  83. }
  84. void Editor::insert(const u32 cp)
  85. {
  86. StringBuilder builder;
  87. builder.append(Utf32View(&cp, 1));
  88. auto str = builder.build();
  89. m_pending_chars.append(str.characters(), str.length());
  90. readjust_anchored_styles(m_cursor, ModificationKind::Insertion);
  91. if (m_cursor == m_buffer.size()) {
  92. m_buffer.append(cp);
  93. m_cursor = m_buffer.size();
  94. m_inline_search_cursor = m_cursor;
  95. return;
  96. }
  97. m_buffer.insert(m_cursor, cp);
  98. ++m_chars_inserted_in_the_middle;
  99. ++m_cursor;
  100. m_inline_search_cursor = m_cursor;
  101. }
  102. void Editor::register_character_input_callback(char ch, Function<bool(Editor&)> callback)
  103. {
  104. if (m_key_callbacks.contains(ch)) {
  105. dbg() << "Key callback registered twice for " << ch;
  106. ASSERT_NOT_REACHED();
  107. }
  108. m_key_callbacks.set(ch, make<KeyCallback>(move(callback)));
  109. }
  110. static size_t codepoint_length_in_utf8(u32 codepoint)
  111. {
  112. if (codepoint <= 0x7f)
  113. return 1;
  114. if (codepoint <= 0x07ff)
  115. return 2;
  116. if (codepoint <= 0xffff)
  117. return 3;
  118. if (codepoint <= 0x10ffff)
  119. return 4;
  120. return 3;
  121. }
  122. // buffer [ 0 1 2 3 . . . A . . . B . . . M . . . N ]
  123. // ^ ^ ^ ^
  124. // | | | +- end of buffer
  125. // | | +- scan offset = M
  126. // | +- range end = M - B
  127. // +- range start = M - A
  128. // This method converts a byte range defined by [start_byte_offset, end_byte_offset] to a codepoint range [M - A, M - B] as shown in the diagram above.
  129. // If `reverse' is true, A and B are before M, if not, A and B are after M.
  130. Editor::CodepointRange Editor::byte_offset_range_to_codepoint_offset_range(size_t start_byte_offset, size_t end_byte_offset, size_t scan_codepoint_offset, bool reverse) const
  131. {
  132. size_t byte_offset = 0;
  133. size_t codepoint_offset = scan_codepoint_offset + (reverse ? 1 : 0);
  134. CodepointRange range;
  135. for (;;) {
  136. if (!reverse) {
  137. if (codepoint_offset >= m_buffer.size())
  138. break;
  139. } else {
  140. if (codepoint_offset == 0)
  141. break;
  142. }
  143. if (byte_offset > end_byte_offset)
  144. break;
  145. if (byte_offset < start_byte_offset)
  146. ++range.start;
  147. if (byte_offset < end_byte_offset)
  148. ++range.end;
  149. byte_offset += codepoint_length_in_utf8(m_buffer[reverse ? --codepoint_offset : codepoint_offset++]);
  150. }
  151. return range;
  152. }
  153. void Editor::stylize(const Span& span, const Style& style)
  154. {
  155. if (style.is_empty())
  156. return;
  157. auto start = span.beginning();
  158. auto end = span.end();
  159. if (span.mode() == Span::ByteOriented) {
  160. auto offsets = byte_offset_range_to_codepoint_offset_range(start, end, 0);
  161. start = offsets.start;
  162. end = offsets.end;
  163. }
  164. auto& spans_starting = style.is_anchored() ? m_anchored_spans_starting : m_spans_starting;
  165. auto& spans_ending = style.is_anchored() ? m_anchored_spans_ending : m_spans_ending;
  166. auto starting_map = spans_starting.get(start).value_or({});
  167. if (!starting_map.contains(end))
  168. m_refresh_needed = true;
  169. starting_map.set(end, style);
  170. spans_starting.set(start, starting_map);
  171. auto ending_map = spans_ending.get(end).value_or({});
  172. if (!ending_map.contains(start))
  173. m_refresh_needed = true;
  174. ending_map.set(start, style);
  175. spans_ending.set(end, ending_map);
  176. }
  177. void Editor::suggest(size_t invariant_offset, size_t static_offset, Span::Mode offset_mode) const
  178. {
  179. auto internal_static_offset = static_offset;
  180. auto internal_invariant_offset = invariant_offset;
  181. if (offset_mode == Span::Mode::ByteOriented) {
  182. // FIXME: We're assuming that invariant_offset points to the end of the available data
  183. // this is not necessarily true, but is true in most cases.
  184. auto offsets = byte_offset_range_to_codepoint_offset_range(internal_static_offset, internal_invariant_offset + internal_static_offset, m_cursor - 1, true);
  185. internal_static_offset = offsets.start;
  186. internal_invariant_offset = offsets.end - offsets.start;
  187. }
  188. m_suggestion_manager.set_suggestion_variants(internal_static_offset, internal_invariant_offset, 0);
  189. }
  190. String Editor::get_line(const String& prompt)
  191. {
  192. initialize();
  193. m_is_editing = true;
  194. set_prompt(prompt);
  195. reset();
  196. set_origin();
  197. strip_styles(true);
  198. m_history_cursor = m_history.size();
  199. for (;;) {
  200. if (m_always_refresh)
  201. m_refresh_needed = true;
  202. refresh_display();
  203. if (m_finish) {
  204. m_finish = false;
  205. printf("\n");
  206. fflush(stdout);
  207. auto string = line();
  208. m_buffer.clear();
  209. m_is_editing = false;
  210. restore();
  211. return string;
  212. }
  213. char keybuf[16];
  214. ssize_t nread = 0;
  215. if (!m_incomplete_data.size())
  216. nread = read(0, keybuf, sizeof(keybuf));
  217. if (nread < 0) {
  218. if (errno == EINTR) {
  219. if (!m_was_interrupted) {
  220. if (m_was_resized)
  221. continue;
  222. finish();
  223. continue;
  224. }
  225. m_was_interrupted = false;
  226. if (!m_buffer.is_empty())
  227. printf("^C");
  228. m_buffer.clear();
  229. m_cursor = 0;
  230. if (on_interrupt_handled)
  231. on_interrupt_handled();
  232. m_refresh_needed = true;
  233. continue;
  234. }
  235. perror("read failed");
  236. // FIXME: exit()ing here is a bit off. Should communicate failure to caller somehow instead.
  237. exit(2);
  238. }
  239. m_incomplete_data.append(keybuf, nread);
  240. nread = m_incomplete_data.size();
  241. // FIXME: exit()ing here is a bit off. Should communicate failure to caller somehow instead.
  242. if (nread == 0)
  243. exit(0);
  244. auto reverse_tab = false;
  245. auto ctrl_held = false;
  246. // Discard starting bytes until they make sense as utf-8.
  247. size_t valid_bytes = 0;
  248. while (nread) {
  249. Utf8View { StringView { m_incomplete_data.data(), (size_t)nread } }.validate(valid_bytes);
  250. if (valid_bytes)
  251. break;
  252. m_incomplete_data.take_first();
  253. --nread;
  254. }
  255. Utf8View input_view { StringView { m_incomplete_data.data(), valid_bytes } };
  256. size_t consumed_codepoints = 0;
  257. for (auto codepoint : input_view) {
  258. if (m_finish)
  259. break;
  260. ++consumed_codepoints;
  261. if (codepoint == 0)
  262. continue;
  263. switch (m_state) {
  264. case InputState::ExpectBracket:
  265. if (codepoint == '[') {
  266. m_state = InputState::ExpectFinal;
  267. continue;
  268. } else {
  269. m_state = InputState::Free;
  270. break;
  271. }
  272. case InputState::ExpectFinal:
  273. switch (codepoint) {
  274. case 'O': // mod_ctrl
  275. ctrl_held = true;
  276. continue;
  277. case 'A': // up
  278. {
  279. m_searching_backwards = true;
  280. auto inline_search_cursor = m_inline_search_cursor;
  281. StringBuilder builder;
  282. builder.append(Utf32View { m_buffer.data(), inline_search_cursor });
  283. String search_phrase = builder.to_string();
  284. if (search(search_phrase, true, true)) {
  285. ++m_search_offset;
  286. } else {
  287. insert(search_phrase);
  288. }
  289. m_inline_search_cursor = inline_search_cursor;
  290. m_state = InputState::Free;
  291. ctrl_held = false;
  292. continue;
  293. }
  294. case 'B': // down
  295. {
  296. auto inline_search_cursor = m_inline_search_cursor;
  297. StringBuilder builder;
  298. builder.append(Utf32View { m_buffer.data(), inline_search_cursor });
  299. String search_phrase = builder.to_string();
  300. auto search_changed_directions = m_searching_backwards;
  301. m_searching_backwards = false;
  302. if (m_search_offset > 0) {
  303. m_search_offset -= 1 + search_changed_directions;
  304. if (!search(search_phrase, true, true)) {
  305. insert(search_phrase);
  306. }
  307. } else {
  308. m_search_offset = 0;
  309. m_cursor = 0;
  310. m_buffer.clear();
  311. insert(search_phrase);
  312. m_refresh_needed = true;
  313. }
  314. m_inline_search_cursor = inline_search_cursor;
  315. m_state = InputState::Free;
  316. ctrl_held = false;
  317. continue;
  318. }
  319. case 'D': // left
  320. if (m_cursor > 0) {
  321. if (ctrl_held) {
  322. auto skipped_at_least_one_character = false;
  323. for (;;) {
  324. if (m_cursor == 0)
  325. break;
  326. if (skipped_at_least_one_character && isspace(m_buffer[m_cursor - 1])) // stop *after* a space, but only if it changes the position
  327. break;
  328. skipped_at_least_one_character = true;
  329. --m_cursor;
  330. }
  331. } else {
  332. --m_cursor;
  333. }
  334. }
  335. m_inline_search_cursor = m_cursor;
  336. m_state = InputState::Free;
  337. ctrl_held = false;
  338. continue;
  339. case 'C': // right
  340. if (m_cursor < m_buffer.size()) {
  341. if (ctrl_held) {
  342. // Temporarily put a space at the end of our buffer,
  343. // doing this greatly simplifies the logic below.
  344. m_buffer.append(' ');
  345. for (;;) {
  346. if (m_cursor >= m_buffer.size())
  347. break;
  348. if (isspace(m_buffer[++m_cursor]))
  349. break;
  350. }
  351. m_buffer.take_last();
  352. } else {
  353. ++m_cursor;
  354. }
  355. }
  356. m_inline_search_cursor = m_cursor;
  357. m_search_offset = 0;
  358. m_state = InputState::Free;
  359. ctrl_held = false;
  360. continue;
  361. case 'H':
  362. m_cursor = 0;
  363. m_inline_search_cursor = m_cursor;
  364. m_search_offset = 0;
  365. m_state = InputState::Free;
  366. ctrl_held = false;
  367. continue;
  368. case 'F':
  369. m_cursor = m_buffer.size();
  370. m_state = InputState::Free;
  371. m_inline_search_cursor = m_cursor;
  372. m_search_offset = 0;
  373. ctrl_held = false;
  374. continue;
  375. case 'Z': // shift+tab
  376. reverse_tab = true;
  377. m_state = InputState::Free;
  378. ctrl_held = false;
  379. break;
  380. case '3':
  381. if (m_cursor == m_buffer.size()) {
  382. fputc('\a', stdout);
  383. fflush(stdout);
  384. continue;
  385. }
  386. remove_at_index(m_cursor);
  387. m_refresh_needed = true;
  388. m_search_offset = 0;
  389. m_state = InputState::ExpectTerminator;
  390. ctrl_held = false;
  391. continue;
  392. default:
  393. dbgprintf("Shell: Unhandled final: %02x (%c)\r\n", codepoint, codepoint);
  394. m_state = InputState::Free;
  395. ctrl_held = false;
  396. continue;
  397. }
  398. break;
  399. case InputState::ExpectTerminator:
  400. m_state = InputState::Free;
  401. continue;
  402. case InputState::Free:
  403. if (codepoint == 27) {
  404. m_state = InputState::ExpectBracket;
  405. continue;
  406. }
  407. break;
  408. }
  409. auto cb = m_key_callbacks.get(codepoint);
  410. if (cb.has_value()) {
  411. if (!cb.value()->callback(*this)) {
  412. continue;
  413. }
  414. }
  415. m_search_offset = 0; // reset search offset on any key
  416. if (codepoint == '\t' || reverse_tab) {
  417. if (!on_tab_complete)
  418. continue;
  419. // Reverse tab can count as regular tab here.
  420. m_times_tab_pressed++;
  421. int token_start = m_cursor;
  422. // Ask for completions only on the first tab
  423. // and scan for the largest common prefix to display,
  424. // further tabs simply show the cached completions.
  425. if (m_times_tab_pressed == 1) {
  426. m_suggestion_manager.set_suggestions(on_tab_complete(*this));
  427. m_prompt_lines_at_suggestion_initiation = num_lines();
  428. if (m_suggestion_manager.count() == 0) {
  429. // There are no suggestions, beep.
  430. putchar('\a');
  431. fflush(stdout);
  432. }
  433. }
  434. // Adjust already incremented / decremented index when switching tab direction.
  435. if (reverse_tab && m_tab_direction != TabDirection::Backward) {
  436. m_suggestion_manager.previous();
  437. m_suggestion_manager.previous();
  438. m_tab_direction = TabDirection::Backward;
  439. }
  440. if (!reverse_tab && m_tab_direction != TabDirection::Forward) {
  441. m_suggestion_manager.next();
  442. m_suggestion_manager.next();
  443. m_tab_direction = TabDirection::Forward;
  444. }
  445. reverse_tab = false;
  446. auto completion_mode = m_times_tab_pressed == 1 ? SuggestionManager::CompletePrefix : m_times_tab_pressed == 2 ? SuggestionManager::ShowSuggestions : SuggestionManager::CycleSuggestions;
  447. auto completion_result = m_suggestion_manager.attempt_completion(completion_mode, token_start);
  448. auto new_cursor = m_cursor + completion_result.new_cursor_offset;
  449. for (size_t i = completion_result.offset_region_to_remove.start; i < completion_result.offset_region_to_remove.end; ++i)
  450. remove_at_index(new_cursor);
  451. m_cursor = new_cursor;
  452. m_inline_search_cursor = new_cursor;
  453. m_refresh_needed = true;
  454. for (auto& view : completion_result.insert)
  455. insert(view);
  456. if (completion_result.style_to_apply.has_value()) {
  457. // Apply the style of the last suggestion.
  458. readjust_anchored_styles(m_suggestion_manager.current_suggestion().start_index, ModificationKind::ForcedOverlapRemoval);
  459. stylize({ m_suggestion_manager.current_suggestion().start_index, m_cursor, Span::Mode::CodepointOriented }, completion_result.style_to_apply.value());
  460. }
  461. switch (completion_result.new_completion_mode) {
  462. case SuggestionManager::DontComplete:
  463. m_times_tab_pressed = 0;
  464. break;
  465. case SuggestionManager::CompletePrefix:
  466. break;
  467. default:
  468. ++m_times_tab_pressed;
  469. break;
  470. }
  471. if (m_times_tab_pressed > 1) {
  472. if (m_suggestion_manager.count() > 0) {
  473. if (m_suggestion_display->cleanup())
  474. reposition_cursor();
  475. m_suggestion_display->set_initial_prompt_lines(m_prompt_lines_at_suggestion_initiation);
  476. m_suggestion_display->display(m_suggestion_manager);
  477. m_origin_x = m_suggestion_display->origin_x();
  478. }
  479. }
  480. if (m_times_tab_pressed > 2) {
  481. if (m_tab_direction == TabDirection::Forward)
  482. m_suggestion_manager.next();
  483. else
  484. m_suggestion_manager.previous();
  485. }
  486. if (m_suggestion_manager.count() < 2) {
  487. // We have none, or just one suggestion,
  488. // we should just commit that and continue
  489. // after it, as if it were auto-completed.
  490. suggest(0, 0, Span::CodepointOriented);
  491. m_times_tab_pressed = 0;
  492. m_suggestion_manager.reset();
  493. m_suggestion_display->finish();
  494. }
  495. continue;
  496. }
  497. if (m_times_tab_pressed) {
  498. // Apply the style of the last suggestion.
  499. readjust_anchored_styles(m_suggestion_manager.current_suggestion().start_index, ModificationKind::ForcedOverlapRemoval);
  500. stylize({ m_suggestion_manager.current_suggestion().start_index, m_cursor, Span::Mode::CodepointOriented }, m_suggestion_manager.current_suggestion().style);
  501. // We probably have some suggestions drawn,
  502. // let's clean them up.
  503. if (m_suggestion_display->cleanup()) {
  504. reposition_cursor();
  505. m_refresh_needed = true;
  506. }
  507. m_suggestion_manager.reset();
  508. suggest(0, 0, Span::CodepointOriented);
  509. m_suggestion_display->finish();
  510. }
  511. m_times_tab_pressed = 0; // Safe to say if we get here, the user didn't press TAB
  512. auto do_backspace = [&] {
  513. if (m_is_searching) {
  514. return;
  515. }
  516. if (m_cursor == 0) {
  517. fputc('\a', stdout);
  518. fflush(stdout);
  519. return;
  520. }
  521. remove_at_index(m_cursor - 1);
  522. --m_cursor;
  523. m_inline_search_cursor = m_cursor;
  524. // We will have to redraw :(
  525. m_refresh_needed = true;
  526. };
  527. if (codepoint == 8 || codepoint == m_termios.c_cc[VERASE]) {
  528. do_backspace();
  529. continue;
  530. }
  531. if (codepoint == m_termios.c_cc[VWERASE]) {
  532. bool has_seen_nonspace = false;
  533. while (m_cursor > 0) {
  534. if (isspace(m_buffer[m_cursor - 1])) {
  535. if (has_seen_nonspace)
  536. break;
  537. } else {
  538. has_seen_nonspace = true;
  539. }
  540. do_backspace();
  541. }
  542. continue;
  543. }
  544. if (codepoint == m_termios.c_cc[VKILL]) {
  545. for (size_t i = 0; i < m_cursor; ++i)
  546. remove_at_index(0);
  547. m_cursor = 0;
  548. m_refresh_needed = true;
  549. continue;
  550. }
  551. // ^L
  552. if (codepoint == 0xc) {
  553. printf("\033[3J\033[H\033[2J"); // Clear screen.
  554. VT::move_absolute(1, 1);
  555. set_origin(1, 1);
  556. m_refresh_needed = true;
  557. continue;
  558. }
  559. // ^A
  560. if (codepoint == 0x01) {
  561. m_cursor = 0;
  562. continue;
  563. }
  564. // ^R
  565. if (codepoint == 0x12) {
  566. if (m_is_searching) {
  567. // how did we get here?
  568. ASSERT_NOT_REACHED();
  569. } else {
  570. m_is_searching = true;
  571. m_search_offset = 0;
  572. m_pre_search_buffer.clear();
  573. for (auto codepoint : m_buffer)
  574. m_pre_search_buffer.append(codepoint);
  575. m_pre_search_cursor = m_cursor;
  576. m_search_editor = make<Editor>(Configuration { Configuration::Eager, m_configuration.split_mechanism }); // Has anyone seen 'Inception'?
  577. m_search_editor->on_display_refresh = [this](Editor& search_editor) {
  578. StringBuilder builder;
  579. builder.append(Utf32View { search_editor.buffer().data(), search_editor.buffer().size() });
  580. search(builder.build());
  581. refresh_display();
  582. return;
  583. };
  584. // Whenever the search editor gets a ^R, cycle between history entries.
  585. m_search_editor->register_character_input_callback(0x12, [this](Editor& search_editor) {
  586. ++m_search_offset;
  587. search_editor.m_refresh_needed = true;
  588. return false; // Do not process this key event
  589. });
  590. // Whenever the search editor gets a backspace, cycle back between history entries
  591. // unless we're at the zeroth entry, in which case, allow the deletion.
  592. m_search_editor->register_character_input_callback(m_termios.c_cc[VERASE], [this](Editor& search_editor) {
  593. if (m_search_offset > 0) {
  594. --m_search_offset;
  595. search_editor.m_refresh_needed = true;
  596. return false; // Do not process this key event
  597. }
  598. return true;
  599. });
  600. // ^L - This is a source of issues, as the search editor refreshes first,
  601. // and we end up with the wrong order of prompts, so we will first refresh
  602. // ourselves, then refresh the search editor, and then tell him not to process
  603. // this event.
  604. m_search_editor->register_character_input_callback(0x0c, [this](auto& search_editor) {
  605. printf("\033[3J\033[H\033[2J"); // Clear screen.
  606. // refresh our own prompt
  607. set_origin(1, 1);
  608. m_refresh_needed = true;
  609. refresh_display();
  610. // move the search prompt below ours
  611. // and tell it to redraw itself
  612. search_editor.set_origin(2, 1);
  613. search_editor.m_refresh_needed = true;
  614. return false;
  615. });
  616. // quit without clearing the current buffer
  617. m_search_editor->register_character_input_callback('\t', [this](Editor& search_editor) {
  618. search_editor.finish();
  619. m_reset_buffer_on_search_end = false;
  620. return false;
  621. });
  622. printf("\n");
  623. fflush(stdout);
  624. auto search_prompt = "\x1b[32msearch:\x1b[0m ";
  625. auto search_string = m_search_editor->get_line(search_prompt);
  626. m_search_editor = nullptr;
  627. m_is_searching = false;
  628. m_search_offset = 0;
  629. // Manually cleanup the search line.
  630. reposition_cursor();
  631. auto search_string_codepoint_length = Utf8View { search_string }.length_in_codepoints();
  632. VT::clear_lines(0, (search_string_codepoint_length + actual_rendered_string_length(search_prompt) + m_num_columns - 1) / m_num_columns);
  633. reposition_cursor();
  634. if (!m_reset_buffer_on_search_end || search_string_codepoint_length == 0) {
  635. // If the entry was empty, or we purposely quit without a newline,
  636. // do not return anything; instead, just end the search.
  637. end_search();
  638. continue;
  639. }
  640. // Return the string,
  641. finish();
  642. continue;
  643. }
  644. continue;
  645. }
  646. // Normally ^D
  647. if (codepoint == m_termios.c_cc[VEOF]) {
  648. if (m_buffer.is_empty()) {
  649. printf("<EOF>\n");
  650. if (!m_always_refresh) // This is a little off, but it'll do for now
  651. exit(0);
  652. }
  653. continue;
  654. }
  655. // ^E
  656. if (codepoint == 0x05) {
  657. m_cursor = m_buffer.size();
  658. continue;
  659. }
  660. if (codepoint == '\n') {
  661. finish();
  662. continue;
  663. }
  664. insert(codepoint);
  665. }
  666. if (consumed_codepoints == m_incomplete_data.size()) {
  667. m_incomplete_data.clear();
  668. } else {
  669. for (size_t i = 0; i < consumed_codepoints; ++i)
  670. m_incomplete_data.take_first();
  671. }
  672. }
  673. }
  674. bool Editor::search(const StringView& phrase, bool allow_empty, bool from_beginning)
  675. {
  676. int last_matching_offset = -1;
  677. // Do not search for empty strings.
  678. if (allow_empty || phrase.length() > 0) {
  679. size_t search_offset = m_search_offset;
  680. for (size_t i = m_history_cursor; i > 0; --i) {
  681. auto contains = from_beginning ? m_history[i - 1].starts_with(phrase) : m_history[i - 1].contains(phrase);
  682. if (contains) {
  683. last_matching_offset = i - 1;
  684. if (search_offset == 0)
  685. break;
  686. --search_offset;
  687. }
  688. }
  689. if (last_matching_offset == -1) {
  690. fputc('\a', stdout);
  691. fflush(stdout);
  692. }
  693. }
  694. m_buffer.clear();
  695. m_cursor = 0;
  696. if (last_matching_offset >= 0) {
  697. insert(m_history[last_matching_offset]);
  698. }
  699. // Always needed, as we have cleared the buffer above.
  700. m_refresh_needed = true;
  701. return last_matching_offset >= 0;
  702. }
  703. void Editor::recalculate_origin()
  704. {
  705. // Changing the columns can affect our origin if
  706. // the new size is smaller than our prompt, which would
  707. // cause said prompt to take up more space, so we should
  708. // compensate for that.
  709. if (m_cached_prompt_length >= m_num_columns) {
  710. auto added_lines = (m_cached_prompt_length + 1) / m_num_columns - 1;
  711. m_origin_x += added_lines;
  712. }
  713. // We also need to recalculate our cursor position,
  714. // but that will be calculated and applied at the next
  715. // refresh cycle.
  716. }
  717. void Editor::cleanup()
  718. {
  719. VT::move_relative(0, m_pending_chars.size() - m_chars_inserted_in_the_middle);
  720. auto current_line = cursor_line();
  721. VT::clear_lines(current_line - 1, num_lines() - current_line);
  722. VT::move_relative(-num_lines() + 1, -offset_in_line() - m_old_prompt_length - m_pending_chars.size() + m_chars_inserted_in_the_middle);
  723. };
  724. void Editor::refresh_display()
  725. {
  726. auto has_cleaned_up = false;
  727. // Someone changed the window size, figure it out
  728. // and react to it, we might need to redraw.
  729. if (m_was_resized) {
  730. auto previous_num_columns = m_num_columns;
  731. struct winsize ws;
  732. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
  733. m_num_columns = 80;
  734. m_num_lines = 25;
  735. } else {
  736. m_num_columns = ws.ws_col;
  737. m_num_lines = ws.ws_row;
  738. }
  739. m_suggestion_display->set_vt_size(m_num_lines, m_num_columns);
  740. if (previous_num_columns != m_num_columns) {
  741. // We need to cleanup and redo everything.
  742. m_cached_prompt_valid = false;
  743. m_refresh_needed = true;
  744. swap(previous_num_columns, m_num_columns);
  745. recalculate_origin();
  746. cleanup();
  747. swap(previous_num_columns, m_num_columns);
  748. has_cleaned_up = true;
  749. }
  750. }
  751. // Do not call hook on pure cursor movement.
  752. if (m_cached_prompt_valid && !m_refresh_needed && m_pending_chars.size() == 0) {
  753. // Probably just moving around.
  754. reposition_cursor();
  755. m_cached_buffer_size = m_buffer.size();
  756. return;
  757. }
  758. if (on_display_refresh)
  759. on_display_refresh(*this);
  760. if (m_cached_prompt_valid) {
  761. if (!m_refresh_needed && m_cursor == m_buffer.size()) {
  762. // Just write the characters out and continue,
  763. // no need to refresh the entire line.
  764. char null = 0;
  765. m_pending_chars.append(&null, 1);
  766. fputs((char*)m_pending_chars.data(), stdout);
  767. m_pending_chars.clear();
  768. m_drawn_cursor = m_cursor;
  769. m_cached_buffer_size = m_buffer.size();
  770. fflush(stdout);
  771. return;
  772. }
  773. }
  774. // Ouch, reflow entire line.
  775. // FIXME: handle multiline stuff
  776. if (!has_cleaned_up) {
  777. cleanup();
  778. }
  779. VT::move_absolute(m_origin_x, m_origin_y);
  780. fputs(m_new_prompt.characters(), stdout);
  781. VT::clear_to_end_of_line();
  782. HashMap<u32, Style> empty_styles {};
  783. StringBuilder builder;
  784. for (size_t i = 0; i < m_buffer.size(); ++i) {
  785. auto ends = m_spans_ending.get(i).value_or(empty_styles);
  786. auto starts = m_spans_starting.get(i).value_or(empty_styles);
  787. auto anchored_ends = m_anchored_spans_ending.get(i).value_or(empty_styles);
  788. auto anchored_starts = m_anchored_spans_starting.get(i).value_or(empty_styles);
  789. if (ends.size() || anchored_ends.size()) {
  790. Style style;
  791. for (auto& applicable_style : ends)
  792. style.unify_with(applicable_style.value);
  793. for (auto& applicable_style : anchored_ends)
  794. style.unify_with(applicable_style.value);
  795. // Disable any style that should be turned off.
  796. VT::apply_style(style, false);
  797. // Reapply styles for overlapping spans that include this one.
  798. style = find_applicable_style(i);
  799. VT::apply_style(style, true);
  800. }
  801. if (starts.size() || anchored_starts.size()) {
  802. Style style;
  803. for (auto& applicable_style : starts)
  804. style.unify_with(applicable_style.value);
  805. for (auto& applicable_style : anchored_starts)
  806. style.unify_with(applicable_style.value);
  807. // Set new styles.
  808. VT::apply_style(style, true);
  809. }
  810. builder.clear();
  811. builder.append(Utf32View { &m_buffer[i], 1 });
  812. fputs(builder.to_string().characters(), stdout);
  813. }
  814. VT::apply_style(Style::reset_style()); // don't bleed to EOL
  815. m_pending_chars.clear();
  816. m_refresh_needed = false;
  817. m_cached_buffer_size = m_buffer.size();
  818. m_chars_inserted_in_the_middle = 0;
  819. if (!m_cached_prompt_valid) {
  820. m_cached_prompt_valid = true;
  821. }
  822. reposition_cursor();
  823. fflush(stdout);
  824. }
  825. void Editor::strip_styles(bool strip_anchored)
  826. {
  827. m_spans_starting.clear();
  828. m_spans_ending.clear();
  829. if (strip_anchored) {
  830. m_anchored_spans_starting.clear();
  831. m_anchored_spans_ending.clear();
  832. }
  833. m_refresh_needed = true;
  834. }
  835. void Editor::reposition_cursor()
  836. {
  837. m_drawn_cursor = m_cursor;
  838. auto line = cursor_line() - 1;
  839. auto column = offset_in_line();
  840. VT::move_absolute(line + m_origin_x, column + m_origin_y);
  841. }
  842. void VT::move_absolute(u32 x, u32 y)
  843. {
  844. printf("\033[%d;%dH", x, y);
  845. fflush(stdout);
  846. }
  847. void VT::move_relative(int x, int y)
  848. {
  849. char x_op = 'A', y_op = 'D';
  850. if (x > 0)
  851. x_op = 'B';
  852. else
  853. x = -x;
  854. if (y > 0)
  855. y_op = 'C';
  856. else
  857. y = -y;
  858. if (x > 0)
  859. printf("\033[%d%c", x, x_op);
  860. if (y > 0)
  861. printf("\033[%d%c", y, y_op);
  862. }
  863. Style Editor::find_applicable_style(size_t offset) const
  864. {
  865. // Walk through our styles and merge all that fit in the offset.
  866. auto style = Style::reset_style();
  867. auto unify = [&](auto& entry) {
  868. if (entry.key >= offset)
  869. return;
  870. for (auto& style_value : entry.value) {
  871. if (style_value.key <= offset)
  872. return;
  873. style.unify_with(style_value.value, true);
  874. }
  875. };
  876. for (auto& entry : m_spans_starting) {
  877. unify(entry);
  878. }
  879. for (auto& entry : m_anchored_spans_starting) {
  880. unify(entry);
  881. }
  882. return style;
  883. }
  884. String Style::Background::to_vt_escape() const
  885. {
  886. if (is_default())
  887. return "";
  888. if (m_is_rgb) {
  889. return String::format("\033[48;2;%d;%d;%dm", m_rgb_color[0], m_rgb_color[1], m_rgb_color[2]);
  890. } else {
  891. return String::format("\033[%dm", (u8)m_xterm_color + 40);
  892. }
  893. }
  894. String Style::Foreground::to_vt_escape() const
  895. {
  896. if (is_default())
  897. return "";
  898. if (m_is_rgb) {
  899. return String::format("\033[38;2;%d;%d;%dm", m_rgb_color[0], m_rgb_color[1], m_rgb_color[2]);
  900. } else {
  901. return String::format("\033[%dm", (u8)m_xterm_color + 30);
  902. }
  903. }
  904. String Style::Hyperlink::to_vt_escape(bool starting) const
  905. {
  906. if (is_empty())
  907. return "";
  908. return String::format("\033]8;;%s\033\\", starting ? m_link.characters() : "");
  909. }
  910. void Style::unify_with(const Style& other, bool prefer_other)
  911. {
  912. // Unify colors.
  913. if (prefer_other || m_background.is_default())
  914. m_background = other.background();
  915. if (prefer_other || m_foreground.is_default())
  916. m_foreground = other.foreground();
  917. // Unify graphic renditions.
  918. if (other.bold())
  919. set(Bold);
  920. if (other.italic())
  921. set(Italic);
  922. if (other.underline())
  923. set(Underline);
  924. // Unify links.
  925. if (prefer_other || m_hyperlink.is_empty())
  926. m_hyperlink = other.hyperlink();
  927. }
  928. String Style::to_string() const
  929. {
  930. StringBuilder builder;
  931. builder.append("Style { ");
  932. if (!m_foreground.is_default()) {
  933. builder.append("Foreground(");
  934. if (m_foreground.m_is_rgb) {
  935. builder.join(", ", m_foreground.m_rgb_color);
  936. } else {
  937. builder.appendf("(XtermColor) %d", m_foreground.m_xterm_color);
  938. }
  939. builder.append("), ");
  940. }
  941. if (!m_background.is_default()) {
  942. builder.append("Background(");
  943. if (m_background.m_is_rgb) {
  944. builder.join(' ', m_background.m_rgb_color);
  945. } else {
  946. builder.appendf("(XtermColor) %d", m_background.m_xterm_color);
  947. }
  948. builder.append("), ");
  949. }
  950. if (bold())
  951. builder.append("Bold, ");
  952. if (underline())
  953. builder.append("Underline, ");
  954. if (italic())
  955. builder.append("Italic, ");
  956. if (!m_hyperlink.is_empty())
  957. builder.appendf("Hyperlink(\"%s\"), ", m_hyperlink.m_link.characters());
  958. builder.append("}");
  959. return builder.build();
  960. }
  961. void VT::apply_style(const Style& style, bool is_starting)
  962. {
  963. if (is_starting) {
  964. printf(
  965. "\033[%d;%d;%dm%s%s%s",
  966. style.bold() ? 1 : 22,
  967. style.underline() ? 4 : 24,
  968. style.italic() ? 3 : 23,
  969. style.background().to_vt_escape().characters(),
  970. style.foreground().to_vt_escape().characters(),
  971. style.hyperlink().to_vt_escape(true).characters());
  972. } else {
  973. printf("%s", style.hyperlink().to_vt_escape(false).characters());
  974. }
  975. }
  976. void VT::clear_lines(size_t count_above, size_t count_below)
  977. {
  978. // Go down count_below lines.
  979. if (count_below > 0)
  980. printf("\033[%dB", (int)count_below);
  981. // Then clear lines going upwards.
  982. for (size_t i = count_below + count_above; i > 0; --i)
  983. fputs(i == 1 ? "\033[2K" : "\033[2K\033[A", stdout);
  984. }
  985. void VT::save_cursor()
  986. {
  987. fputs("\033[s", stdout);
  988. fflush(stdout);
  989. }
  990. void VT::restore_cursor()
  991. {
  992. fputs("\033[u", stdout);
  993. fflush(stdout);
  994. }
  995. void VT::clear_to_end_of_line()
  996. {
  997. fputs("\033[K", stdout);
  998. fflush(stdout);
  999. }
  1000. size_t Editor::actual_rendered_string_length(const StringView& string) const
  1001. {
  1002. size_t length { 0 };
  1003. enum VTState {
  1004. Free = 1,
  1005. Escape = 3,
  1006. Bracket = 5,
  1007. BracketArgsSemi = 7,
  1008. Title = 9,
  1009. } state { Free };
  1010. Utf8View view { string };
  1011. auto it = view.begin();
  1012. for (size_t i = 0; i < view.length_in_codepoints(); ++i, ++it) {
  1013. auto c = *it;
  1014. switch (state) {
  1015. case Free:
  1016. if (c == '\x1b') { // escape
  1017. state = Escape;
  1018. continue;
  1019. }
  1020. if (c == '\r' || c == '\n') { // return or carriage return
  1021. // Reset length to 0, since we either overwrite, or are on a newline.
  1022. length = 0;
  1023. continue;
  1024. }
  1025. // FIXME: This will not support anything sophisticated
  1026. ++length;
  1027. break;
  1028. case Escape:
  1029. if (c == ']') {
  1030. ++i;
  1031. ++it;
  1032. if (*it == '0')
  1033. state = Title;
  1034. continue;
  1035. }
  1036. if (c == '[') {
  1037. state = Bracket;
  1038. continue;
  1039. }
  1040. // FIXME: This does not support non-VT (aside from set-title) escapes
  1041. break;
  1042. case Bracket:
  1043. if (isdigit(c)) {
  1044. state = BracketArgsSemi;
  1045. continue;
  1046. }
  1047. break;
  1048. case BracketArgsSemi:
  1049. if (c == ';') {
  1050. state = Bracket;
  1051. continue;
  1052. }
  1053. if (!isdigit(c))
  1054. state = Free;
  1055. break;
  1056. case Title:
  1057. if (c == 7)
  1058. state = Free;
  1059. break;
  1060. }
  1061. }
  1062. return length;
  1063. }
  1064. Vector<size_t, 2> Editor::vt_dsr()
  1065. {
  1066. char buf[16];
  1067. u32 length { 0 };
  1068. // Read whatever junk there is before talking to the terminal
  1069. // and insert them later when we're reading user input.
  1070. bool more_junk_to_read { false };
  1071. timeval timeout { 0, 0 };
  1072. fd_set readfds;
  1073. FD_ZERO(&readfds);
  1074. FD_SET(0, &readfds);
  1075. do {
  1076. more_junk_to_read = false;
  1077. (void)select(1, &readfds, nullptr, nullptr, &timeout);
  1078. if (FD_ISSET(0, &readfds)) {
  1079. auto nread = read(0, buf, 16);
  1080. m_incomplete_data.append(buf, nread);
  1081. more_junk_to_read = true;
  1082. }
  1083. } while (more_junk_to_read);
  1084. fputs("\033[6n", stdout);
  1085. fflush(stdout);
  1086. do {
  1087. auto nread = read(0, buf + length, 16 - length);
  1088. if (nread < 0) {
  1089. if (errno == 0) {
  1090. // ????
  1091. continue;
  1092. }
  1093. dbg() << "Error while reading DSR: " << strerror(errno);
  1094. return { 1, 1 };
  1095. }
  1096. if (nread == 0) {
  1097. dbg() << "Terminal DSR issue; received no response";
  1098. return { 1, 1 };
  1099. }
  1100. length += nread;
  1101. } while (buf[length - 1] != 'R' && length < 16);
  1102. size_t x { 1 }, y { 1 };
  1103. if (buf[0] == '\033' && buf[1] == '[') {
  1104. auto parts = StringView(buf + 2, length - 3).split_view(';');
  1105. bool ok;
  1106. x = parts[0].to_int(ok);
  1107. if (!ok) {
  1108. dbg() << "Terminal DSR issue; received garbage x";
  1109. }
  1110. y = parts[1].to_int(ok);
  1111. if (!ok) {
  1112. dbg() << "Terminal DSR issue; received garbage y";
  1113. }
  1114. }
  1115. return { x, y };
  1116. }
  1117. String Editor::line(size_t up_to_index) const
  1118. {
  1119. StringBuilder builder;
  1120. builder.append(Utf32View { m_buffer.data(), min(m_buffer.size(), up_to_index) });
  1121. return builder.build();
  1122. }
  1123. bool Editor::should_break_token(Vector<u32, 1024>& buffer, size_t index)
  1124. {
  1125. switch (m_configuration.split_mechanism) {
  1126. case Configuration::TokenSplitMechanism::Spaces:
  1127. return buffer[index] == ' ';
  1128. case Configuration::TokenSplitMechanism::UnescapedSpaces:
  1129. return buffer[index] == ' ' && (index == 0 || buffer[index - 1] != '\\');
  1130. }
  1131. ASSERT_NOT_REACHED();
  1132. return true;
  1133. };
  1134. void Editor::remove_at_index(size_t index)
  1135. {
  1136. // See if we have any anchored styles, and reposition them if needed.
  1137. readjust_anchored_styles(index, ModificationKind::Removal);
  1138. m_buffer.remove(index);
  1139. }
  1140. void Editor::readjust_anchored_styles(size_t hint_index, ModificationKind modification)
  1141. {
  1142. struct Anchor {
  1143. Span old_span;
  1144. Span new_span;
  1145. Style style;
  1146. };
  1147. Vector<Anchor> anchors_to_relocate;
  1148. auto index_shift = modification == ModificationKind::Insertion ? 1 : -1;
  1149. auto forced_removal = modification == ModificationKind::ForcedOverlapRemoval;
  1150. for (auto& start_entry : m_anchored_spans_starting) {
  1151. for (auto& end_entry : start_entry.value) {
  1152. if (forced_removal) {
  1153. if (start_entry.key <= hint_index && end_entry.key > hint_index) {
  1154. // Remove any overlapping regions.
  1155. continue;
  1156. }
  1157. }
  1158. if (start_entry.key >= hint_index) {
  1159. if (start_entry.key == hint_index && end_entry.key == hint_index + 1 && modification == ModificationKind::Removal) {
  1160. // Remove the anchor, as all its text was wiped.
  1161. continue;
  1162. }
  1163. // Shift everything.
  1164. 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 });
  1165. continue;
  1166. }
  1167. if (end_entry.key > hint_index) {
  1168. // Shift just the end.
  1169. 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 });
  1170. continue;
  1171. }
  1172. 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 });
  1173. }
  1174. }
  1175. m_anchored_spans_ending.clear();
  1176. m_anchored_spans_starting.clear();
  1177. // Pass over the relocations and update the stale entries.
  1178. for (auto& relocation : anchors_to_relocate) {
  1179. stylize(relocation.new_span, relocation.style);
  1180. }
  1181. }
  1182. }