GTextEditor.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. #include <AK/StringBuilder.h>
  2. #include <Kernel/KeyCode.h>
  3. #include <LibGUI/GAction.h>
  4. #include <LibGUI/GClipboard.h>
  5. #include <LibGUI/GFontDatabase.h>
  6. #include <LibGUI/GMenu.h>
  7. #include <LibGUI/GPainter.h>
  8. #include <LibGUI/GScrollBar.h>
  9. #include <LibGUI/GTextEditor.h>
  10. #include <LibGUI/GWindow.h>
  11. #include <ctype.h>
  12. #include <fcntl.h>
  13. #include <stdio.h>
  14. #include <unistd.h>
  15. GTextEditor::GTextEditor(Type type, GWidget* parent)
  16. : GScrollableWidget(parent)
  17. , m_type(type)
  18. {
  19. set_frame_shape(FrameShape::Container);
  20. set_frame_shadow(FrameShadow::Sunken);
  21. set_frame_thickness(2);
  22. set_scrollbars_enabled(is_multi_line());
  23. set_font(GFontDatabase::the().get_by_name("Csilla Thin"));
  24. // FIXME: Recompute vertical scrollbar step size on font change.
  25. vertical_scrollbar().set_step(line_height());
  26. m_lines.append(make<Line>(*this));
  27. m_cursor = { 0, 0 };
  28. create_actions();
  29. }
  30. GTextEditor::~GTextEditor()
  31. {
  32. }
  33. void GTextEditor::create_actions()
  34. {
  35. m_undo_action = GAction::create(
  36. "Undo", { Mod_Ctrl, Key_Z }, GraphicsBitmap::load_from_file("/res/icons/16x16/undo.png"), [&](const GAction&) {
  37. // FIXME: Undo
  38. },
  39. this);
  40. m_redo_action = GAction::create(
  41. "Redo", { Mod_Ctrl, Key_Y }, GraphicsBitmap::load_from_file("/res/icons/16x16/redo.png"), [&](const GAction&) {
  42. // FIXME: Redo
  43. },
  44. this);
  45. m_cut_action = GAction::create(
  46. "Cut", { Mod_Ctrl, Key_X }, GraphicsBitmap::load_from_file("/res/icons/cut16.png"), [&](const GAction&) {
  47. cut();
  48. },
  49. this);
  50. m_copy_action = GAction::create(
  51. "Copy", { Mod_Ctrl, Key_C }, GraphicsBitmap::load_from_file("/res/icons/16x16/edit-copy.png"), [&](const GAction&) {
  52. copy();
  53. },
  54. this);
  55. m_paste_action = GAction::create(
  56. "Paste", { Mod_Ctrl, Key_V }, GraphicsBitmap::load_from_file("/res/icons/paste16.png"), [&](const GAction&) {
  57. paste();
  58. },
  59. this);
  60. m_delete_action = GAction::create(
  61. "Delete", { 0, Key_Delete }, GraphicsBitmap::load_from_file("/res/icons/16x16/delete.png"), [&](const GAction&) {
  62. do_delete();
  63. },
  64. this);
  65. }
  66. void GTextEditor::set_text(const StringView& text)
  67. {
  68. if (is_single_line() && text.length() == m_lines[0].length() && !memcmp(text.characters_without_null_termination(), m_lines[0].characters(), text.length()))
  69. return;
  70. m_selection.clear();
  71. m_lines.clear();
  72. int start_of_current_line = 0;
  73. auto add_line = [&](int current_position) {
  74. int line_length = current_position - start_of_current_line;
  75. auto line = make<Line>(*this);
  76. if (line_length)
  77. line->set_text(text.substring_view(start_of_current_line, current_position - start_of_current_line));
  78. m_lines.append(move(line));
  79. start_of_current_line = current_position + 1;
  80. };
  81. int i = 0;
  82. for (i = 0; i < text.length(); ++i) {
  83. if (text[i] == '\n')
  84. add_line(i);
  85. }
  86. add_line(i);
  87. update_content_size();
  88. recompute_all_visual_lines();
  89. if (is_single_line())
  90. set_cursor(0, m_lines[0].length());
  91. else
  92. set_cursor(0, 0);
  93. did_update_selection();
  94. update();
  95. }
  96. void GTextEditor::update_content_size()
  97. {
  98. int content_width = 0;
  99. for (auto& line : m_lines)
  100. content_width = max(line.width(font()), content_width);
  101. content_width += m_horizontal_content_padding * 2;
  102. if (is_right_text_alignment(m_text_alignment))
  103. content_width = max(frame_inner_rect().width(), content_width);
  104. int content_height = line_count() * line_height();
  105. set_content_size({ content_width, content_height });
  106. set_size_occupied_by_fixed_elements({ ruler_width(), 0 });
  107. }
  108. GTextPosition GTextEditor::text_position_at(const Point& a_position) const
  109. {
  110. auto position = a_position;
  111. position.move_by(horizontal_scrollbar().value(), vertical_scrollbar().value());
  112. position.move_by(-(m_horizontal_content_padding + ruler_width()), 0);
  113. position.move_by(-frame_thickness(), -frame_thickness());
  114. int line_index = -1;
  115. if (is_line_wrapping_enabled()) {
  116. for (int i = 0; i < m_lines.size(); ++i) {
  117. auto& rect = m_lines[i].m_visual_rect;
  118. if (position.y() >= rect.top() && position.y() <= rect.bottom()) {
  119. line_index = i;
  120. break;
  121. }
  122. }
  123. } else {
  124. line_index = position.y() / line_height();
  125. }
  126. line_index = max(0, min(line_index, line_count() - 1));
  127. auto& line = m_lines[line_index];
  128. int column_index;
  129. switch (m_text_alignment) {
  130. case TextAlignment::CenterLeft:
  131. column_index = (position.x() + glyph_width() / 2) / glyph_width();
  132. if (is_line_wrapping_enabled()) {
  133. line.for_each_visual_line([&](const Rect& rect, const StringView&, int start_of_line) {
  134. if (rect.contains(position)) {
  135. column_index += start_of_line;
  136. return IterationDecision::Break;
  137. }
  138. return IterationDecision::Continue;
  139. });
  140. }
  141. break;
  142. case TextAlignment::CenterRight:
  143. // FIXME: Support right-aligned line wrapping, I guess.
  144. ASSERT(!is_line_wrapping_enabled());
  145. column_index = (position.x() - content_x_for_position({ line_index, 0 }) + glyph_width() / 2) / glyph_width();
  146. break;
  147. default:
  148. ASSERT_NOT_REACHED();
  149. }
  150. column_index = max(0, min(column_index, m_lines[line_index].length()));
  151. return { line_index, column_index };
  152. }
  153. void GTextEditor::doubleclick_event(GMouseEvent& event)
  154. {
  155. if (event.button() != GMouseButton::Left)
  156. return;
  157. m_triple_click_timer.start();
  158. m_in_drag_select = false;
  159. auto start = text_position_at(event.position());
  160. auto end = start;
  161. auto& line = m_lines[start.line()];
  162. while (start.column() > 0) {
  163. if (isspace(line.characters()[start.column() - 1]))
  164. break;
  165. start.set_column(start.column() - 1);
  166. }
  167. while (end.column() < line.length()) {
  168. if (isspace(line.characters()[end.column()]))
  169. break;
  170. end.set_column(end.column() + 1);
  171. }
  172. m_selection.set(start, end);
  173. set_cursor(end);
  174. update();
  175. did_update_selection();
  176. }
  177. void GTextEditor::mousedown_event(GMouseEvent& event)
  178. {
  179. if (event.button() != GMouseButton::Left) {
  180. return;
  181. }
  182. if (m_triple_click_timer.is_valid() && m_triple_click_timer.elapsed() < 250) {
  183. m_triple_click_timer = CElapsedTimer();
  184. GTextPosition start;
  185. GTextPosition end;
  186. if (is_multi_line()) {
  187. // select *current* line
  188. start = GTextPosition(m_cursor.line(), 0);
  189. end = GTextPosition(m_cursor.line(), m_lines[m_cursor.line()].length());
  190. } else {
  191. // select *whole* line
  192. start = GTextPosition(0, 0);
  193. end = GTextPosition(line_count() - 1, m_lines[line_count() - 1].length());
  194. }
  195. m_selection.set(start, end);
  196. set_cursor(end);
  197. return;
  198. }
  199. if (event.modifiers() & Mod_Shift) {
  200. if (!has_selection())
  201. m_selection.set(m_cursor, {});
  202. } else {
  203. m_selection.clear();
  204. }
  205. m_in_drag_select = true;
  206. set_cursor(text_position_at(event.position()));
  207. if (!(event.modifiers() & Mod_Shift)) {
  208. if (!has_selection())
  209. m_selection.set(m_cursor, {});
  210. }
  211. if (m_selection.start().is_valid() && m_selection.start() != m_cursor)
  212. m_selection.set_end(m_cursor);
  213. // FIXME: Only update the relevant rects.
  214. update();
  215. did_update_selection();
  216. }
  217. void GTextEditor::mouseup_event(GMouseEvent& event)
  218. {
  219. if (event.button() == GMouseButton::Left) {
  220. if (m_in_drag_select) {
  221. m_in_drag_select = false;
  222. }
  223. return;
  224. }
  225. }
  226. void GTextEditor::mousemove_event(GMouseEvent& event)
  227. {
  228. if (m_in_drag_select) {
  229. set_cursor(text_position_at(event.position()));
  230. m_selection.set_end(m_cursor);
  231. did_update_selection();
  232. update();
  233. return;
  234. }
  235. }
  236. int GTextEditor::ruler_width() const
  237. {
  238. if (!m_ruler_visible)
  239. return 0;
  240. // FIXME: Resize based on needed space.
  241. return 5 * font().glyph_width('x') + 4;
  242. }
  243. Rect GTextEditor::ruler_content_rect(int line_index) const
  244. {
  245. if (!m_ruler_visible)
  246. return {};
  247. return {
  248. 0 - ruler_width() + horizontal_scrollbar().value(),
  249. line_content_rect(line_index).y(),
  250. ruler_width(),
  251. line_content_rect(line_index).height()
  252. };
  253. }
  254. Rect GTextEditor::ruler_rect_in_inner_coordinates() const
  255. {
  256. return { 0, 0, ruler_width(), height() - height_occupied_by_horizontal_scrollbar() };
  257. }
  258. Rect GTextEditor::visible_text_rect_in_inner_coordinates() const
  259. {
  260. return {
  261. (m_horizontal_content_padding * 2) + (m_ruler_visible ? (ruler_rect_in_inner_coordinates().right() + 1) : 0),
  262. 0,
  263. width() - width_occupied_by_vertical_scrollbar() - ruler_width(),
  264. height() - height_occupied_by_horizontal_scrollbar()
  265. };
  266. }
  267. void GTextEditor::paint_event(GPaintEvent& event)
  268. {
  269. GFrame::paint_event(event);
  270. GPainter painter(*this);
  271. painter.add_clip_rect(widget_inner_rect());
  272. painter.add_clip_rect(event.rect());
  273. painter.fill_rect(event.rect(), Color::White);
  274. painter.translate(frame_thickness(), frame_thickness());
  275. auto ruler_rect = ruler_rect_in_inner_coordinates();
  276. if (m_ruler_visible) {
  277. painter.fill_rect(ruler_rect, Color::WarmGray);
  278. painter.draw_line(ruler_rect.top_right(), ruler_rect.bottom_right(), Color::DarkGray);
  279. }
  280. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  281. if (m_ruler_visible)
  282. painter.translate(ruler_width(), 0);
  283. int first_visible_line = text_position_at(event.rect().top_left()).line();
  284. int last_visible_line = text_position_at(event.rect().bottom_right()).line();
  285. auto selection = normalized_selection();
  286. bool has_selection = selection.is_valid();
  287. if (m_ruler_visible) {
  288. for (int i = first_visible_line; i <= last_visible_line; ++i) {
  289. bool is_current_line = i == m_cursor.line();
  290. auto ruler_line_rect = ruler_content_rect(i);
  291. painter.draw_text(
  292. ruler_line_rect.shrunken(2, 0),
  293. String::number(i + 1),
  294. is_current_line ? Font::default_bold_font() : font(),
  295. TextAlignment::CenterRight,
  296. is_current_line ? Color::DarkGray : Color::MidGray);
  297. }
  298. }
  299. Rect text_clip_rect {
  300. (m_ruler_visible ? (ruler_rect_in_inner_coordinates().right() + frame_thickness() + 1) : frame_thickness()),
  301. frame_thickness(),
  302. width() - width_occupied_by_vertical_scrollbar() - ruler_width(),
  303. height() - height_occupied_by_horizontal_scrollbar()
  304. };
  305. painter.add_clip_rect(text_clip_rect);
  306. for (int i = first_visible_line; i <= last_visible_line; ++i) {
  307. auto& line = m_lines[i];
  308. line.for_each_visual_line([&](const Rect& line_rect, const StringView& visual_line_text, int) {
  309. // FIXME: Make sure we always fill the entire line.
  310. //line_rect.set_width(exposed_width);
  311. if (is_multi_line() && i == m_cursor.line())
  312. painter.fill_rect(line_rect, Color(230, 230, 230));
  313. painter.draw_text(line_rect, visual_line_text, m_text_alignment, Color::Black);
  314. bool line_has_selection = has_selection && i >= selection.start().line() && i <= selection.end().line();
  315. if (line_has_selection) {
  316. int selection_start_column_on_line = selection.start().line() == i ? selection.start().column() : 0;
  317. int selection_end_column_on_line = selection.end().line() == i ? selection.end().column() : line.length();
  318. int selection_left = content_x_for_position({ i, selection_start_column_on_line });
  319. int selection_right = content_x_for_position({ i, selection_end_column_on_line });
  320. Rect selection_rect { selection_left, line_rect.y(), selection_right - selection_left, line_rect.height() };
  321. painter.fill_rect(selection_rect, Color::from_rgb(0x955233));
  322. painter.draw_text(selection_rect, StringView(line.characters() + selection_start_column_on_line, line.length() - selection_start_column_on_line - (line.length() - selection_end_column_on_line)), TextAlignment::CenterLeft, Color::White);
  323. }
  324. return IterationDecision::Continue;
  325. });
  326. }
  327. if (is_focused() && m_cursor_state)
  328. painter.fill_rect(cursor_content_rect(), Color::Red);
  329. }
  330. void GTextEditor::toggle_selection_if_needed_for_event(const GKeyEvent& event)
  331. {
  332. if (event.shift() && !m_selection.is_valid()) {
  333. m_selection.set(m_cursor, {});
  334. did_update_selection();
  335. update();
  336. return;
  337. }
  338. if (!event.shift() && m_selection.is_valid()) {
  339. m_selection.clear();
  340. did_update_selection();
  341. update();
  342. return;
  343. }
  344. }
  345. void GTextEditor::select_all()
  346. {
  347. GTextPosition start_of_document { 0, 0 };
  348. GTextPosition end_of_document { line_count() - 1, m_lines[line_count() - 1].length() };
  349. m_selection.set(start_of_document, end_of_document);
  350. did_update_selection();
  351. set_cursor(end_of_document);
  352. update();
  353. }
  354. void GTextEditor::keydown_event(GKeyEvent& event)
  355. {
  356. if (is_single_line() && event.key() == KeyCode::Key_Tab)
  357. return GWidget::keydown_event(event);
  358. if (is_single_line() && event.key() == KeyCode::Key_Return) {
  359. if (on_return_pressed)
  360. on_return_pressed();
  361. return;
  362. }
  363. if (event.key() == KeyCode::Key_Escape) {
  364. if (on_escape_pressed)
  365. on_escape_pressed();
  366. return;
  367. }
  368. if (event.key() == KeyCode::Key_Up) {
  369. if (m_cursor.line() > 0) {
  370. int new_line = m_cursor.line() - 1;
  371. int new_column = min(m_cursor.column(), m_lines[new_line].length());
  372. toggle_selection_if_needed_for_event(event);
  373. set_cursor(new_line, new_column);
  374. if (m_selection.start().is_valid()) {
  375. m_selection.set_end(m_cursor);
  376. did_update_selection();
  377. }
  378. }
  379. return;
  380. }
  381. if (event.key() == KeyCode::Key_Down) {
  382. if (m_cursor.line() < (m_lines.size() - 1)) {
  383. int new_line = m_cursor.line() + 1;
  384. int new_column = min(m_cursor.column(), m_lines[new_line].length());
  385. toggle_selection_if_needed_for_event(event);
  386. set_cursor(new_line, new_column);
  387. if (m_selection.start().is_valid()) {
  388. m_selection.set_end(m_cursor);
  389. did_update_selection();
  390. }
  391. }
  392. return;
  393. }
  394. if (event.key() == KeyCode::Key_PageUp) {
  395. if (m_cursor.line() > 0) {
  396. int new_line = max(0, m_cursor.line() - visible_content_rect().height() / line_height());
  397. int new_column = min(m_cursor.column(), m_lines[new_line].length());
  398. toggle_selection_if_needed_for_event(event);
  399. set_cursor(new_line, new_column);
  400. if (m_selection.start().is_valid()) {
  401. m_selection.set_end(m_cursor);
  402. did_update_selection();
  403. }
  404. }
  405. return;
  406. }
  407. if (event.key() == KeyCode::Key_PageDown) {
  408. if (m_cursor.line() < (m_lines.size() - 1)) {
  409. int new_line = min(line_count() - 1, m_cursor.line() + visible_content_rect().height() / line_height());
  410. int new_column = min(m_cursor.column(), m_lines[new_line].length());
  411. toggle_selection_if_needed_for_event(event);
  412. set_cursor(new_line, new_column);
  413. if (m_selection.start().is_valid()) {
  414. m_selection.set_end(m_cursor);
  415. did_update_selection();
  416. }
  417. }
  418. return;
  419. }
  420. if (event.key() == KeyCode::Key_Left) {
  421. if (m_cursor.column() > 0) {
  422. int new_column = m_cursor.column() - 1;
  423. toggle_selection_if_needed_for_event(event);
  424. set_cursor(m_cursor.line(), new_column);
  425. if (m_selection.start().is_valid()) {
  426. m_selection.set_end(m_cursor);
  427. did_update_selection();
  428. }
  429. } else if (m_cursor.line() > 0) {
  430. int new_line = m_cursor.line() - 1;
  431. int new_column = m_lines[new_line].length();
  432. toggle_selection_if_needed_for_event(event);
  433. set_cursor(new_line, new_column);
  434. if (m_selection.start().is_valid()) {
  435. m_selection.set_end(m_cursor);
  436. did_update_selection();
  437. }
  438. }
  439. return;
  440. }
  441. if (event.key() == KeyCode::Key_Right) {
  442. if (m_cursor.column() < current_line().length()) {
  443. int new_column = m_cursor.column() + 1;
  444. toggle_selection_if_needed_for_event(event);
  445. set_cursor(m_cursor.line(), new_column);
  446. if (m_selection.start().is_valid()) {
  447. m_selection.set_end(m_cursor);
  448. did_update_selection();
  449. }
  450. } else if (m_cursor.line() != line_count() - 1) {
  451. int new_line = m_cursor.line() + 1;
  452. int new_column = 0;
  453. toggle_selection_if_needed_for_event(event);
  454. set_cursor(new_line, new_column);
  455. if (m_selection.start().is_valid()) {
  456. m_selection.set_end(m_cursor);
  457. did_update_selection();
  458. }
  459. }
  460. return;
  461. }
  462. if (!event.ctrl() && event.key() == KeyCode::Key_Home) {
  463. toggle_selection_if_needed_for_event(event);
  464. set_cursor(m_cursor.line(), 0);
  465. if (m_selection.start().is_valid()) {
  466. m_selection.set_end(m_cursor);
  467. did_update_selection();
  468. }
  469. return;
  470. }
  471. if (!event.ctrl() && event.key() == KeyCode::Key_End) {
  472. toggle_selection_if_needed_for_event(event);
  473. set_cursor(m_cursor.line(), current_line().length());
  474. if (m_selection.start().is_valid()) {
  475. m_selection.set_end(m_cursor);
  476. did_update_selection();
  477. }
  478. return;
  479. }
  480. if (event.ctrl() && event.key() == KeyCode::Key_Home) {
  481. toggle_selection_if_needed_for_event(event);
  482. set_cursor(0, 0);
  483. if (m_selection.start().is_valid()) {
  484. m_selection.set_end(m_cursor);
  485. did_update_selection();
  486. }
  487. return;
  488. }
  489. if (event.ctrl() && event.key() == KeyCode::Key_End) {
  490. toggle_selection_if_needed_for_event(event);
  491. set_cursor(line_count() - 1, m_lines[line_count() - 1].length());
  492. if (m_selection.start().is_valid()) {
  493. m_selection.set_end(m_cursor);
  494. did_update_selection();
  495. }
  496. return;
  497. }
  498. if (event.modifiers() == Mod_Ctrl && event.key() == KeyCode::Key_A) {
  499. select_all();
  500. return;
  501. }
  502. if (event.key() == KeyCode::Key_Backspace) {
  503. if (is_readonly())
  504. return;
  505. if (has_selection()) {
  506. delete_selection();
  507. did_update_selection();
  508. return;
  509. }
  510. if (m_cursor.column() > 0) {
  511. // Backspace within line
  512. current_line().remove(m_cursor.column() - 1);
  513. update_content_size();
  514. set_cursor(m_cursor.line(), m_cursor.column() - 1);
  515. return;
  516. }
  517. if (m_cursor.column() == 0 && m_cursor.line() != 0) {
  518. // Backspace at column 0; merge with previous line
  519. auto& previous_line = m_lines[m_cursor.line() - 1];
  520. int previous_length = previous_line.length();
  521. previous_line.append(current_line().characters(), current_line().length());
  522. m_lines.remove(m_cursor.line());
  523. update_content_size();
  524. update();
  525. set_cursor(m_cursor.line() - 1, previous_length);
  526. return;
  527. }
  528. return;
  529. }
  530. if (event.modifiers() == Mod_Shift && event.key() == KeyCode::Key_Delete) {
  531. if (is_readonly())
  532. return;
  533. delete_current_line();
  534. return;
  535. }
  536. if (event.key() == KeyCode::Key_Delete) {
  537. if (is_readonly())
  538. return;
  539. do_delete();
  540. return;
  541. }
  542. if (!is_readonly() && !event.ctrl() && !event.alt() && !event.text().is_empty())
  543. insert_at_cursor_or_replace_selection(event.text());
  544. }
  545. void GTextEditor::delete_current_line()
  546. {
  547. if (has_selection())
  548. return delete_selection();
  549. m_lines.remove(m_cursor.line());
  550. if (m_lines.is_empty())
  551. m_lines.append(make<Line>(*this));
  552. update_content_size();
  553. update();
  554. }
  555. void GTextEditor::do_delete()
  556. {
  557. if (is_readonly())
  558. return;
  559. if (has_selection())
  560. return delete_selection();
  561. if (m_cursor.column() < current_line().length()) {
  562. // Delete within line
  563. current_line().remove(m_cursor.column());
  564. did_change();
  565. update_cursor();
  566. return;
  567. }
  568. if (m_cursor.column() == current_line().length() && m_cursor.line() != line_count() - 1) {
  569. // Delete at end of line; merge with next line
  570. auto& next_line = m_lines[m_cursor.line() + 1];
  571. int previous_length = current_line().length();
  572. current_line().append(next_line.characters(), next_line.length());
  573. m_lines.remove(m_cursor.line() + 1);
  574. update();
  575. did_change();
  576. set_cursor(m_cursor.line(), previous_length);
  577. return;
  578. }
  579. }
  580. void GTextEditor::insert_at_cursor(const StringView& text)
  581. {
  582. // FIXME: This should obviously not be implemented this way.
  583. for (int i = 0; i < text.length(); ++i) {
  584. insert_at_cursor(text[i]);
  585. }
  586. }
  587. void GTextEditor::insert_at_cursor(char ch)
  588. {
  589. bool at_head = m_cursor.column() == 0;
  590. bool at_tail = m_cursor.column() == current_line().length();
  591. if (ch == '\n') {
  592. if (at_tail || at_head) {
  593. String new_line_contents;
  594. if (m_automatic_indentation_enabled && at_tail) {
  595. int leading_spaces = 0;
  596. auto& old_line = m_lines[m_cursor.line()];
  597. for (int i = 0; i < old_line.length(); ++i) {
  598. if (old_line.characters()[i] == ' ')
  599. ++leading_spaces;
  600. else
  601. break;
  602. }
  603. if (leading_spaces)
  604. new_line_contents = String::repeated(' ', leading_spaces);
  605. }
  606. m_lines.insert(m_cursor.line() + (at_tail ? 1 : 0), make<Line>(*this, new_line_contents));
  607. update();
  608. did_change();
  609. set_cursor(m_cursor.line() + 1, m_lines[m_cursor.line() + 1].length());
  610. return;
  611. }
  612. auto new_line = make<Line>(*this);
  613. new_line->append(current_line().characters() + m_cursor.column(), current_line().length() - m_cursor.column());
  614. current_line().truncate(m_cursor.column());
  615. m_lines.insert(m_cursor.line() + 1, move(new_line));
  616. update();
  617. did_change();
  618. set_cursor(m_cursor.line() + 1, 0);
  619. return;
  620. }
  621. if (ch == '\t') {
  622. int next_soft_tab_stop = ((m_cursor.column() + m_soft_tab_width) / m_soft_tab_width) * m_soft_tab_width;
  623. int spaces_to_insert = next_soft_tab_stop - m_cursor.column();
  624. for (int i = 0; i < spaces_to_insert; ++i) {
  625. current_line().insert(m_cursor.column(), ' ');
  626. }
  627. did_change();
  628. set_cursor(m_cursor.line(), next_soft_tab_stop);
  629. return;
  630. }
  631. current_line().insert(m_cursor.column(), ch);
  632. did_change();
  633. set_cursor(m_cursor.line(), m_cursor.column() + 1);
  634. }
  635. int GTextEditor::content_x_for_position(const GTextPosition& position) const
  636. {
  637. auto& line = m_lines[position.line()];
  638. switch (m_text_alignment) {
  639. case TextAlignment::CenterLeft:
  640. return m_horizontal_content_padding + position.column() * glyph_width();
  641. case TextAlignment::CenterRight:
  642. return content_width() - m_horizontal_content_padding - (line.length() * glyph_width()) + (position.column() * glyph_width());
  643. default:
  644. ASSERT_NOT_REACHED();
  645. }
  646. }
  647. Rect GTextEditor::content_rect_for_position(const GTextPosition& position) const
  648. {
  649. if (!position.is_valid())
  650. return {};
  651. ASSERT(!m_lines.is_empty());
  652. ASSERT(position.column() <= (current_line().length() + 1));
  653. int x = content_x_for_position(position);
  654. if (is_single_line()) {
  655. Rect rect { x, 0, 1, font().glyph_height() + 2 };
  656. rect.center_vertically_within({ {}, frame_inner_rect().size() });
  657. return rect;
  658. }
  659. return { x, position.line() * line_height(), 1, line_height() };
  660. }
  661. Rect GTextEditor::cursor_content_rect() const
  662. {
  663. return content_rect_for_position(m_cursor);
  664. }
  665. Rect GTextEditor::line_widget_rect(int line_index) const
  666. {
  667. auto rect = line_content_rect(line_index);
  668. rect.set_x(frame_thickness());
  669. rect.set_width(frame_inner_rect().width());
  670. rect.move_by(0, -(vertical_scrollbar().value()));
  671. rect.move_by(0, frame_thickness());
  672. rect.intersect(frame_inner_rect());
  673. return rect;
  674. }
  675. void GTextEditor::scroll_position_into_view(const GTextPosition& position)
  676. {
  677. auto rect = content_rect_for_position(position);
  678. if (position.column() == 0)
  679. rect.set_x(content_x_for_position({ position.line(), 0 }) - 2);
  680. else if (position.column() == m_lines[position.line()].length())
  681. rect.set_x(content_x_for_position({ position.line(), m_lines[position.line()].length() }) + 2);
  682. scroll_into_view(rect, true, true);
  683. }
  684. void GTextEditor::scroll_cursor_into_view()
  685. {
  686. scroll_position_into_view(m_cursor);
  687. }
  688. Rect GTextEditor::line_content_rect(int line_index) const
  689. {
  690. auto& line = m_lines[line_index];
  691. if (is_single_line()) {
  692. Rect line_rect = { content_x_for_position({ line_index, 0 }), 0, line.length() * glyph_width(), font().glyph_height() + 2 };
  693. line_rect.center_vertically_within({ {}, frame_inner_rect().size() });
  694. return line_rect;
  695. }
  696. if (is_line_wrapping_enabled())
  697. return line.m_visual_rect;
  698. return {
  699. content_x_for_position({ line_index, 0 }),
  700. line_index * line_height(),
  701. line.length() * glyph_width(),
  702. line_height()
  703. };
  704. }
  705. void GTextEditor::update_cursor()
  706. {
  707. update(line_widget_rect(m_cursor.line()));
  708. }
  709. void GTextEditor::set_cursor(int line, int column)
  710. {
  711. set_cursor({ line, column });
  712. }
  713. void GTextEditor::set_cursor(const GTextPosition& position)
  714. {
  715. ASSERT(!m_lines.is_empty());
  716. ASSERT(position.line() < m_lines.size());
  717. ASSERT(position.column() <= m_lines[position.line()].length());
  718. if (m_cursor != position) {
  719. // NOTE: If the old cursor is no longer valid, repaint everything just in case.
  720. auto old_cursor_line_rect = m_cursor.line() < m_lines.size()
  721. ? line_widget_rect(m_cursor.line())
  722. : rect();
  723. m_cursor = position;
  724. m_cursor_state = true;
  725. scroll_cursor_into_view();
  726. update(old_cursor_line_rect);
  727. update_cursor();
  728. }
  729. if (on_cursor_change)
  730. on_cursor_change();
  731. }
  732. void GTextEditor::focusin_event(CEvent&)
  733. {
  734. update_cursor();
  735. start_timer(500);
  736. }
  737. void GTextEditor::focusout_event(CEvent&)
  738. {
  739. stop_timer();
  740. }
  741. void GTextEditor::timer_event(CTimerEvent&)
  742. {
  743. m_cursor_state = !m_cursor_state;
  744. if (is_focused())
  745. update_cursor();
  746. }
  747. GTextEditor::Line::Line(GTextEditor& editor)
  748. : m_editor(editor)
  749. {
  750. clear();
  751. }
  752. GTextEditor::Line::Line(GTextEditor& editor, const StringView& text)
  753. : m_editor(editor)
  754. {
  755. set_text(text);
  756. }
  757. void GTextEditor::Line::clear()
  758. {
  759. m_text.clear();
  760. m_text.append(0);
  761. }
  762. void GTextEditor::Line::set_text(const StringView& text)
  763. {
  764. if (text.length() == length() && !memcmp(text.characters_without_null_termination(), characters(), length()))
  765. return;
  766. if (text.is_empty()) {
  767. clear();
  768. return;
  769. }
  770. m_text.resize(text.length() + 1);
  771. memcpy(m_text.data(), text.characters_without_null_termination(), text.length() + 1);
  772. }
  773. int GTextEditor::Line::width(const Font& font) const
  774. {
  775. if (m_editor.is_line_wrapping_enabled())
  776. return m_editor.visible_text_rect_in_inner_coordinates().width();
  777. return font.width(view());
  778. }
  779. void GTextEditor::Line::append(const char* characters, int length)
  780. {
  781. int old_length = m_text.size() - 1;
  782. m_text.resize(m_text.size() + length);
  783. memcpy(m_text.data() + old_length, characters, length);
  784. m_text.last() = 0;
  785. }
  786. void GTextEditor::Line::append(char ch)
  787. {
  788. insert(length(), ch);
  789. }
  790. void GTextEditor::Line::prepend(char ch)
  791. {
  792. insert(0, ch);
  793. }
  794. void GTextEditor::Line::insert(int index, char ch)
  795. {
  796. if (index == length()) {
  797. m_text.last() = ch;
  798. m_text.append(0);
  799. } else {
  800. m_text.insert(index, move(ch));
  801. }
  802. }
  803. void GTextEditor::Line::remove(int index)
  804. {
  805. if (index == length()) {
  806. m_text.take_last();
  807. m_text.last() = 0;
  808. } else {
  809. m_text.remove(index);
  810. }
  811. }
  812. void GTextEditor::Line::truncate(int length)
  813. {
  814. m_text.resize(length + 1);
  815. m_text.last() = 0;
  816. }
  817. bool GTextEditor::write_to_file(const StringView& path)
  818. {
  819. int fd = open_with_path_length(path.characters_without_null_termination(), path.length(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
  820. if (fd < 0) {
  821. perror("open");
  822. return false;
  823. }
  824. for (int i = 0; i < m_lines.size(); ++i) {
  825. auto& line = m_lines[i];
  826. if (line.length()) {
  827. ssize_t nwritten = write(fd, line.characters(), line.length());
  828. if (nwritten < 0) {
  829. perror("write");
  830. close(fd);
  831. return false;
  832. }
  833. }
  834. if (i != m_lines.size() - 1) {
  835. char ch = '\n';
  836. ssize_t nwritten = write(fd, &ch, 1);
  837. if (nwritten != 1) {
  838. perror("write");
  839. close(fd);
  840. return false;
  841. }
  842. }
  843. }
  844. close(fd);
  845. return true;
  846. }
  847. String GTextEditor::text() const
  848. {
  849. StringBuilder builder;
  850. for (int i = 0; i < line_count(); ++i) {
  851. auto& line = m_lines[i];
  852. builder.append(line.characters(), line.length());
  853. if (i != line_count() - 1)
  854. builder.append('\n');
  855. }
  856. return builder.to_string();
  857. }
  858. void GTextEditor::clear()
  859. {
  860. m_lines.clear();
  861. m_lines.append(make<Line>(*this));
  862. m_selection.clear();
  863. did_update_selection();
  864. set_cursor(0, 0);
  865. update();
  866. }
  867. String GTextEditor::selected_text() const
  868. {
  869. if (!has_selection())
  870. return {};
  871. auto selection = normalized_selection();
  872. StringBuilder builder;
  873. for (int i = selection.start().line(); i <= selection.end().line(); ++i) {
  874. auto& line = m_lines[i];
  875. int selection_start_column_on_line = selection.start().line() == i ? selection.start().column() : 0;
  876. int selection_end_column_on_line = selection.end().line() == i ? selection.end().column() : line.length();
  877. builder.append(line.characters() + selection_start_column_on_line, selection_end_column_on_line - selection_start_column_on_line);
  878. if (i != selection.end().line())
  879. builder.append('\n');
  880. }
  881. return builder.to_string();
  882. }
  883. void GTextEditor::delete_selection()
  884. {
  885. if (!has_selection())
  886. return;
  887. auto selection = normalized_selection();
  888. // First delete all the lines in between the first and last one.
  889. for (int i = selection.start().line() + 1; i < selection.end().line();) {
  890. m_lines.remove(i);
  891. selection.end().set_line(selection.end().line() - 1);
  892. }
  893. if (selection.start().line() == selection.end().line()) {
  894. // Delete within same line.
  895. auto& line = m_lines[selection.start().line()];
  896. bool whole_line_is_selected = selection.start().column() == 0 && selection.end().column() == line.length();
  897. if (whole_line_is_selected) {
  898. line.clear();
  899. } else {
  900. auto before_selection = String(line.characters(), line.length()).substring(0, selection.start().column());
  901. auto after_selection = String(line.characters(), line.length()).substring(selection.end().column(), line.length() - selection.end().column());
  902. StringBuilder builder(before_selection.length() + after_selection.length());
  903. builder.append(before_selection);
  904. builder.append(after_selection);
  905. line.set_text(builder.to_string());
  906. }
  907. } else {
  908. // Delete across a newline, merging lines.
  909. ASSERT(selection.start().line() == selection.end().line() - 1);
  910. auto& first_line = m_lines[selection.start().line()];
  911. auto& second_line = m_lines[selection.end().line()];
  912. auto before_selection = String(first_line.characters(), first_line.length()).substring(0, selection.start().column());
  913. auto after_selection = String(second_line.characters(), second_line.length()).substring(selection.end().column(), second_line.length() - selection.end().column());
  914. StringBuilder builder(before_selection.length() + after_selection.length());
  915. builder.append(before_selection);
  916. builder.append(after_selection);
  917. first_line.set_text(builder.to_string());
  918. m_lines.remove(selection.end().line());
  919. }
  920. if (m_lines.is_empty())
  921. m_lines.append(make<Line>(*this));
  922. m_selection.clear();
  923. did_update_selection();
  924. did_change();
  925. set_cursor(selection.start());
  926. update();
  927. }
  928. void GTextEditor::insert_at_cursor_or_replace_selection(const StringView& text)
  929. {
  930. ASSERT(!is_readonly());
  931. if (has_selection())
  932. delete_selection();
  933. insert_at_cursor(text);
  934. }
  935. void GTextEditor::cut()
  936. {
  937. if (is_readonly())
  938. return;
  939. auto selected_text = this->selected_text();
  940. printf("Cut: \"%s\"\n", selected_text.characters());
  941. GClipboard::the().set_data(selected_text);
  942. delete_selection();
  943. }
  944. void GTextEditor::copy()
  945. {
  946. auto selected_text = this->selected_text();
  947. printf("Copy: \"%s\"\n", selected_text.characters());
  948. GClipboard::the().set_data(selected_text);
  949. }
  950. void GTextEditor::paste()
  951. {
  952. if (is_readonly())
  953. return;
  954. auto paste_text = GClipboard::the().data();
  955. printf("Paste: \"%s\"\n", paste_text.characters());
  956. insert_at_cursor_or_replace_selection(paste_text);
  957. }
  958. void GTextEditor::enter_event(CEvent&)
  959. {
  960. ASSERT(window());
  961. window()->set_override_cursor(GStandardCursor::IBeam);
  962. }
  963. void GTextEditor::leave_event(CEvent&)
  964. {
  965. ASSERT(window());
  966. window()->set_override_cursor(GStandardCursor::None);
  967. }
  968. void GTextEditor::did_change()
  969. {
  970. ASSERT(!is_readonly());
  971. update_content_size();
  972. recompute_all_visual_lines();
  973. if (!m_have_pending_change_notification) {
  974. m_have_pending_change_notification = true;
  975. deferred_invoke([this](auto&) {
  976. if (on_change)
  977. on_change();
  978. m_have_pending_change_notification = false;
  979. });
  980. }
  981. }
  982. void GTextEditor::set_readonly(bool readonly)
  983. {
  984. if (m_readonly == readonly)
  985. return;
  986. m_readonly = readonly;
  987. m_cut_action->set_enabled(!is_readonly() && has_selection());
  988. m_delete_action->set_enabled(!is_readonly());
  989. m_paste_action->set_enabled(!is_readonly());
  990. }
  991. void GTextEditor::did_update_selection()
  992. {
  993. m_cut_action->set_enabled(!is_readonly() && has_selection());
  994. m_copy_action->set_enabled(has_selection());
  995. if (on_selection_change)
  996. on_selection_change();
  997. }
  998. void GTextEditor::context_menu_event(GContextMenuEvent& event)
  999. {
  1000. if (!m_context_menu) {
  1001. m_context_menu = make<GMenu>("GTextEditor context menu");
  1002. m_context_menu->add_action(undo_action());
  1003. m_context_menu->add_action(redo_action());
  1004. m_context_menu->add_separator();
  1005. m_context_menu->add_action(cut_action());
  1006. m_context_menu->add_action(copy_action());
  1007. m_context_menu->add_action(paste_action());
  1008. m_context_menu->add_action(delete_action());
  1009. }
  1010. m_context_menu->popup(event.screen_position());
  1011. }
  1012. void GTextEditor::set_text_alignment(TextAlignment alignment)
  1013. {
  1014. if (m_text_alignment == alignment)
  1015. return;
  1016. m_text_alignment = alignment;
  1017. update();
  1018. }
  1019. void GTextEditor::resize_event(GResizeEvent& event)
  1020. {
  1021. GScrollableWidget::resize_event(event);
  1022. update_content_size();
  1023. recompute_all_visual_lines();
  1024. }
  1025. GTextPosition GTextEditor::next_position_after(const GTextPosition& position, ShouldWrapAtEndOfDocument should_wrap)
  1026. {
  1027. auto& line = m_lines[position.line()];
  1028. if (position.column() == line.length()) {
  1029. if (position.line() == line_count() - 1) {
  1030. if (should_wrap == ShouldWrapAtEndOfDocument::Yes)
  1031. return { 0, 0 };
  1032. return {};
  1033. }
  1034. return { position.line() + 1, 0 };
  1035. }
  1036. return { position.line(), position.column() + 1 };
  1037. }
  1038. GTextPosition GTextEditor::prev_position_before(const GTextPosition& position, ShouldWrapAtStartOfDocument should_wrap)
  1039. {
  1040. if (position.column() == 0) {
  1041. if (position.line() == 0) {
  1042. if (should_wrap == ShouldWrapAtStartOfDocument::Yes) {
  1043. auto& last_line = m_lines[line_count() - 1];
  1044. return { line_count() - 1, last_line.length() };
  1045. }
  1046. return {};
  1047. }
  1048. auto& prev_line = m_lines[position.line() - 1];
  1049. return { position.line() - 1, prev_line.length() };
  1050. }
  1051. return { position.line(), position.column() - 1 };
  1052. }
  1053. GTextRange GTextEditor::find_next(const StringView& needle, const GTextPosition& start)
  1054. {
  1055. if (needle.is_empty())
  1056. return {};
  1057. GTextPosition position = start.is_valid() ? start : GTextPosition(0, 0);
  1058. GTextPosition original_position = position;
  1059. GTextPosition start_of_potential_match;
  1060. int needle_index = 0;
  1061. do {
  1062. auto ch = character_at(position);
  1063. if (ch == needle[needle_index]) {
  1064. if (needle_index == 0)
  1065. start_of_potential_match = position;
  1066. ++needle_index;
  1067. if (needle_index >= needle.length())
  1068. return { start_of_potential_match, next_position_after(position) };
  1069. } else {
  1070. if (needle_index > 0)
  1071. position = start_of_potential_match;
  1072. needle_index = 0;
  1073. }
  1074. position = next_position_after(position);
  1075. } while (position.is_valid() && position != original_position);
  1076. return {};
  1077. }
  1078. GTextRange GTextEditor::find_prev(const StringView& needle, const GTextPosition& start)
  1079. {
  1080. if (needle.is_empty())
  1081. return {};
  1082. GTextPosition position = start.is_valid() ? start : GTextPosition(0, 0);
  1083. GTextPosition original_position = position;
  1084. GTextPosition end_of_potential_match;
  1085. int needle_index = needle.length() - 1;
  1086. do {
  1087. auto ch = character_at(position);
  1088. if (ch == needle[needle_index]) {
  1089. if (needle_index == needle.length() - 1)
  1090. end_of_potential_match = position;
  1091. --needle_index;
  1092. if (needle_index < 0)
  1093. return { position, next_position_after(end_of_potential_match) };
  1094. } else {
  1095. if (needle_index < needle.length() - 1)
  1096. position = end_of_potential_match;
  1097. needle_index = needle.length() - 1;
  1098. }
  1099. position = prev_position_before(position);
  1100. } while (position.is_valid() && position != original_position);
  1101. return {};
  1102. }
  1103. void GTextEditor::set_selection(const GTextRange& selection)
  1104. {
  1105. if (m_selection == selection)
  1106. return;
  1107. m_selection = selection;
  1108. set_cursor(m_selection.end());
  1109. scroll_position_into_view(normalized_selection().start());
  1110. update();
  1111. }
  1112. char GTextEditor::character_at(const GTextPosition& position) const
  1113. {
  1114. ASSERT(position.line() < line_count());
  1115. auto& line = m_lines[position.line()];
  1116. if (position.column() == line.length())
  1117. return '\n';
  1118. return line.characters()[position.column()];
  1119. }
  1120. void GTextEditor::recompute_all_visual_lines()
  1121. {
  1122. int y_offset = 0;
  1123. for (auto& line : m_lines) {
  1124. line.recompute_visual_lines();
  1125. line.m_visual_rect.set_y(y_offset);
  1126. y_offset += line.m_visual_rect.height();
  1127. }
  1128. }
  1129. void GTextEditor::Line::recompute_visual_lines()
  1130. {
  1131. m_visual_line_breaks.clear_with_capacity();
  1132. int available_width = m_editor.visible_text_rect_in_inner_coordinates().width();
  1133. if (m_editor.is_line_wrapping_enabled()) {
  1134. int line_width_so_far = 0;
  1135. for (int i = 0; i < length(); ++i) {
  1136. auto ch = characters()[i];
  1137. auto glyph_width = m_editor.font().glyph_width(ch);
  1138. if ((line_width_so_far + glyph_width) > available_width) {
  1139. m_visual_line_breaks.append(i);
  1140. line_width_so_far = 0;
  1141. continue;
  1142. }
  1143. line_width_so_far += glyph_width;
  1144. }
  1145. }
  1146. m_visual_line_breaks.append(length());
  1147. if (m_editor.is_line_wrapping_enabled())
  1148. m_visual_rect = { 0, 0, available_width, m_visual_line_breaks.size() * m_editor.line_height() };
  1149. else
  1150. m_visual_rect = { 0, 0, m_editor.font().width(view()), m_editor.line_height() };
  1151. }
  1152. template<typename Callback>
  1153. void GTextEditor::Line::for_each_visual_line(Callback callback) const
  1154. {
  1155. int start_of_line = 0;
  1156. int line_index = 0;
  1157. for (auto visual_line_break : m_visual_line_breaks) {
  1158. auto visual_line_view = StringView(characters() + start_of_line, visual_line_break - start_of_line);
  1159. Rect visual_line_rect {
  1160. m_visual_rect.x(),
  1161. m_visual_rect.y() + (line_index * m_editor.line_height()),
  1162. m_visual_rect.width(),
  1163. m_editor.line_height()
  1164. };
  1165. if (callback(visual_line_rect, visual_line_view, start_of_line) == IterationDecision::Break)
  1166. break;
  1167. start_of_line = visual_line_break;
  1168. ++line_index;
  1169. }
  1170. }