VimEditingEngine.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <LibGUI/Event.h>
  8. #include <LibGUI/TextEditor.h>
  9. #include <LibGUI/VimEditingEngine.h>
  10. #include <string.h>
  11. namespace GUI {
  12. void VimCursor::move()
  13. {
  14. if (m_forwards)
  15. move_forwards();
  16. else
  17. move_backwards();
  18. }
  19. void VimCursor::move_reverse()
  20. {
  21. if (m_forwards)
  22. move_backwards();
  23. else
  24. move_forwards();
  25. }
  26. u32 VimCursor::peek()
  27. {
  28. TextPosition saved_position = m_position;
  29. move();
  30. u32 peeked = current_char();
  31. m_position = saved_position;
  32. return peeked;
  33. }
  34. u32 VimCursor::peek_reverse()
  35. {
  36. TextPosition saved_position = m_position;
  37. move_reverse();
  38. u32 peeked = current_char();
  39. m_position = saved_position;
  40. return peeked;
  41. }
  42. TextDocumentLine& VimCursor::current_line()
  43. {
  44. return m_editor.line(m_position.line());
  45. }
  46. u32 VimCursor::current_char()
  47. {
  48. if (on_empty_line()) {
  49. // Fails all of isspace, ispunct, isalnum so should be good.
  50. return 0;
  51. } else {
  52. return current_line().view().code_points()[m_position.column()];
  53. }
  54. }
  55. bool VimCursor::on_empty_line()
  56. {
  57. return current_line().length() == 0;
  58. }
  59. bool VimCursor::will_cross_line_boundary()
  60. {
  61. if (on_empty_line())
  62. return true;
  63. else if (m_forwards && m_position.column() == current_line().length() - 1)
  64. return true;
  65. else if (!m_forwards && m_position.column() == 0)
  66. return true;
  67. else
  68. return false;
  69. }
  70. void VimCursor::move_forwards()
  71. {
  72. if (on_empty_line() || m_position.column() == current_line().length() - 1) {
  73. if (m_position.line() == m_editor.line_count() - 1) {
  74. // We have reached the end of the document, so any other
  75. // forward movements are no-ops.
  76. m_hit_edge = true;
  77. } else {
  78. m_position.set_column(0);
  79. m_position.set_line(m_position.line() + 1);
  80. m_crossed_line_boundary = true;
  81. }
  82. } else {
  83. m_position.set_column(m_position.column() + 1);
  84. m_crossed_line_boundary = false;
  85. }
  86. }
  87. void VimCursor::move_backwards()
  88. {
  89. if (m_position.column() == 0) {
  90. if (m_position.line() == 0) {
  91. // We have reached the start of the document, so any other
  92. // backward movements are no-ops.
  93. m_hit_edge = true;
  94. } else {
  95. m_position.set_line(m_position.line() - 1);
  96. if (!on_empty_line())
  97. m_position.set_column(current_line().length() - 1);
  98. else
  99. m_position.set_column(0);
  100. m_crossed_line_boundary = true;
  101. }
  102. } else {
  103. m_position.set_column(m_position.column() - 1);
  104. m_crossed_line_boundary = false;
  105. }
  106. }
  107. void VimMotion::add_key_code(KeyCode key, [[maybe_unused]] bool ctrl, bool shift, [[maybe_unused]] bool alt)
  108. {
  109. if (is_complete())
  110. return;
  111. if (m_find_mode != FindMode::None) {
  112. // We need to consume the next character because we are going to find
  113. // until that character.
  114. // HACK: there is no good way to obtain whether a character is alphanumeric
  115. // from the keycode itself.
  116. char const* keycode_str = key_code_to_string(key);
  117. if (strlen(keycode_str) == 1 && (isalpha(keycode_str[0]) || isspace(keycode_str[0]))) {
  118. m_next_character = tolower(keycode_str[0]);
  119. m_unit = Unit::Find;
  120. } else {
  121. m_unit = Unit::Unknown;
  122. }
  123. m_is_complete = true;
  124. m_should_consume_next_character = false;
  125. return;
  126. }
  127. bool should_use_guirky = m_guirky_mode;
  128. switch (key) {
  129. #define DIGIT(n) \
  130. case KeyCode::Key_##n: \
  131. m_amount = (m_amount * 10) + n; \
  132. break
  133. // Digits add digits to the amount.
  134. DIGIT(1);
  135. DIGIT(2);
  136. DIGIT(3);
  137. DIGIT(4);
  138. DIGIT(5);
  139. DIGIT(6);
  140. DIGIT(7);
  141. DIGIT(8);
  142. DIGIT(9);
  143. #undef DIGIT
  144. // Home means to the beginning of the line.
  145. case KeyCode::Key_Home:
  146. m_unit = Unit::Character;
  147. m_amount = START_OF_LINE;
  148. m_is_complete = true;
  149. break;
  150. // If 0 appears while amount is 0, then it means beginning of line.
  151. // Otherwise, it adds 0 to the amount.
  152. case KeyCode::Key_0:
  153. if (m_amount == 0) {
  154. m_unit = Unit::Character;
  155. m_amount = START_OF_LINE;
  156. m_is_complete = true;
  157. } else {
  158. m_amount = m_amount * 10;
  159. }
  160. break;
  161. // End or $ means end of line.
  162. // TODO: d2$ in vim deletes to the end of the line and then the next line.
  163. case KeyCode::Key_End:
  164. case KeyCode::Key_Dollar:
  165. m_unit = Unit::Character;
  166. m_amount = END_OF_LINE;
  167. m_is_complete = true;
  168. break;
  169. // ^ means the first non-whitespace character for this line.
  170. // It deletes backwards if you're in front of it, and forwards if you're behind.
  171. case KeyCode::Key_Circumflex:
  172. m_unit = Unit::Character;
  173. m_amount = START_OF_NON_WHITESPACE;
  174. m_is_complete = true;
  175. break;
  176. // j, down or + operates on this line and amount line(s) after.
  177. case KeyCode::Key_J:
  178. case KeyCode::Key_Down:
  179. case KeyCode::Key_Plus:
  180. m_unit = Unit::Line;
  181. if (m_amount == 0)
  182. m_amount = 1;
  183. m_is_complete = true;
  184. break;
  185. // k, up or - operates on this line and amount line(s) before.
  186. case KeyCode::Key_K:
  187. case KeyCode::Key_Up:
  188. case KeyCode::Key_Minus:
  189. m_unit = Unit::Line;
  190. if (m_amount == 0)
  191. m_amount = -1;
  192. else
  193. m_amount = -m_amount;
  194. m_is_complete = true;
  195. break;
  196. // BS, h or left operates on this character and amount character(s) before.
  197. case KeyCode::Key_Backspace:
  198. case KeyCode::Key_H:
  199. case KeyCode::Key_Left:
  200. m_unit = Unit::Character;
  201. if (m_amount == 0)
  202. m_amount = -1;
  203. else
  204. m_amount = -m_amount;
  205. m_is_complete = true;
  206. break;
  207. // l or right operates on this character and amount character(s) after.
  208. case KeyCode::Key_L:
  209. case KeyCode::Key_Right:
  210. m_unit = Unit::Character;
  211. if (m_amount > 0)
  212. m_amount--;
  213. m_is_complete = true;
  214. break;
  215. // w operates on amount word(s) after.
  216. // W operates on amount WORD(s) after.
  217. case KeyCode::Key_W:
  218. if (shift)
  219. m_unit = Unit::WORD;
  220. else
  221. m_unit = Unit::Word;
  222. if (m_amount == 0)
  223. m_amount = 1;
  224. m_is_complete = true;
  225. break;
  226. // b operates on amount word(s) before.
  227. // B operates on amount WORD(s) before.
  228. case KeyCode::Key_B:
  229. if (shift)
  230. m_unit = Unit::WORD;
  231. else
  232. m_unit = Unit::Word;
  233. if (m_amount == 0)
  234. m_amount = -1;
  235. else
  236. m_amount = -m_amount;
  237. m_is_complete = true;
  238. break;
  239. // e operates on amount of word(s) after, till the end of the last word.
  240. // E operates on amount of WORD(s) after, till the end of the last WORD.
  241. // ge operates on amount of word(s) before, till the end of the last word.
  242. // gE operates on amount of WORD(s) before, till the end of the last WORD.
  243. case KeyCode::Key_E:
  244. if (shift)
  245. m_unit = Unit::EndOfWORD;
  246. else
  247. m_unit = Unit::EndOfWord;
  248. if (m_guirky_mode) {
  249. if (m_amount == 0)
  250. m_amount = -1;
  251. else
  252. m_amount = -m_amount;
  253. m_guirky_mode = false;
  254. } else {
  255. if (m_amount == 0)
  256. m_amount = 1;
  257. }
  258. m_is_complete = true;
  259. break;
  260. // g enables guirky (g-prefix commands) mode.
  261. // gg operates from the start of the document to the cursor.
  262. // G operates from the cursor to the end of the document.
  263. case KeyCode::Key_G:
  264. if (m_guirky_mode) {
  265. if (shift) {
  266. // gG is not a valid command in vim.
  267. m_guirky_mode = false;
  268. m_unit = Unit::Unknown;
  269. m_is_complete = true;
  270. } else {
  271. m_guirky_mode = false;
  272. m_unit = Unit::Document;
  273. m_amount = -1;
  274. m_is_complete = true;
  275. }
  276. } else {
  277. if (shift) {
  278. m_unit = Unit::Document;
  279. m_amount = 1;
  280. m_is_complete = true;
  281. } else {
  282. m_guirky_mode = true;
  283. }
  284. }
  285. break;
  286. // t operates until the given character.
  287. case KeyCode::Key_T:
  288. m_find_mode = FindMode::To;
  289. m_should_consume_next_character = true;
  290. if (m_amount == 0)
  291. m_amount = 1;
  292. break;
  293. // f operates through the given character.
  294. case KeyCode::Key_F:
  295. m_find_mode = FindMode::Find;
  296. m_should_consume_next_character = true;
  297. if (m_amount == 0)
  298. m_amount = 1;
  299. break;
  300. default:
  301. m_unit = Unit::Unknown;
  302. m_is_complete = true;
  303. break;
  304. }
  305. if (should_use_guirky && m_guirky_mode) {
  306. // If we didn't use the g then we cancel the motion.
  307. m_guirky_mode = false;
  308. m_unit = Unit::Unknown;
  309. m_is_complete = true;
  310. }
  311. }
  312. Optional<TextRange> VimMotion::get_range(VimEditingEngine& engine, bool normalize_for_position)
  313. {
  314. if (!is_complete() || is_cancelled())
  315. return {};
  316. TextEditor& editor = engine.editor();
  317. auto position = editor.cursor();
  318. int amount = abs(m_amount);
  319. bool forwards = m_amount >= 0;
  320. VimCursor cursor { editor, position, forwards };
  321. m_start_line = m_end_line = position.line();
  322. m_start_column = m_end_column = position.column();
  323. switch (m_unit) {
  324. case Unit::Unknown:
  325. VERIFY_NOT_REACHED();
  326. case Unit::Document: {
  327. calculate_document_range(editor);
  328. break;
  329. }
  330. case Unit::Line: {
  331. calculate_line_range(editor, normalize_for_position);
  332. break;
  333. }
  334. case Unit::EndOfWord:
  335. case Unit::Word:
  336. case Unit::EndOfWORD:
  337. case Unit::WORD: {
  338. calculate_word_range(cursor, amount, normalize_for_position);
  339. break;
  340. }
  341. case Unit::Character: {
  342. calculate_character_range(cursor, amount, normalize_for_position);
  343. break;
  344. }
  345. case Unit::Find: {
  346. calculate_find_range(cursor, amount);
  347. break;
  348. }
  349. }
  350. return { TextRange { { m_start_line, m_start_column }, { m_end_line, m_end_column } } };
  351. }
  352. Optional<TextRange> VimMotion::get_repeat_range(VimEditingEngine& engine, VimMotion::Unit unit, bool normalize_for_position)
  353. {
  354. TextEditor& editor = engine.editor();
  355. if (m_amount > 0) {
  356. m_amount--;
  357. } else if (m_amount < 0) {
  358. m_amount++;
  359. }
  360. auto position = editor.cursor();
  361. int amount = abs(m_amount);
  362. bool forwards = m_amount >= 0;
  363. VimCursor cursor { editor, position, forwards };
  364. m_start_line = m_end_line = position.line();
  365. m_start_column = m_end_column = position.column();
  366. switch (unit) {
  367. case Unit::Line: {
  368. calculate_line_range(editor, normalize_for_position);
  369. break;
  370. }
  371. case Unit::Character: {
  372. calculate_character_range(cursor, amount, normalize_for_position);
  373. break;
  374. }
  375. default:
  376. return {};
  377. }
  378. return { TextRange { { m_start_line, m_start_column }, { m_end_line, m_end_column } } };
  379. }
  380. void VimMotion::calculate_document_range(TextEditor& editor)
  381. {
  382. if (m_amount >= 0) {
  383. m_end_line = editor.line_count() - 1;
  384. auto& last_line = editor.line(m_end_line);
  385. m_end_column = last_line.length();
  386. } else {
  387. m_start_line = 0;
  388. m_start_column = 0;
  389. }
  390. }
  391. void VimMotion::calculate_line_range(TextEditor& editor, bool normalize_for_position)
  392. {
  393. // Use this line +/- m_amount lines.
  394. m_start_column = 0;
  395. m_end_column = 0;
  396. if (m_amount >= 0) {
  397. m_end_line = min(m_end_line + !normalize_for_position + m_amount, editor.line_count());
  398. // We can't delete to "last line + 1", so if we're on the last line,
  399. // delete until the end.
  400. if (m_end_line == editor.line_count()) {
  401. m_end_line--;
  402. m_end_column = editor.line(m_end_line).length();
  403. }
  404. } else {
  405. // Can't write it as max(start_line + m_amount, 0) because of unsigned
  406. // shenanigans.
  407. if (m_start_line <= (unsigned)-m_amount)
  408. m_start_line = 0;
  409. else
  410. m_start_line += m_amount;
  411. if (m_end_line == editor.line_count() - 1)
  412. m_end_column = editor.line(m_end_line).length();
  413. else
  414. m_end_line++;
  415. }
  416. }
  417. void VimMotion::calculate_word_range(VimCursor& cursor, int amount, bool normalize_for_position)
  418. {
  419. enum {
  420. Whitespace,
  421. Word,
  422. Punctuation,
  423. Unknown
  424. };
  425. // Word is defined as a-zA-Z0-9_.
  426. auto part_of_word = [](u32 ch) { return ch == '_' || isalnum(ch); };
  427. auto part_of_punctuation = [](u32 ch) { return ch != '_' && ispunct(ch); };
  428. auto classify = [&](u32 ch) {
  429. if (isspace(ch))
  430. return Whitespace;
  431. else if (part_of_word(ch))
  432. return Word;
  433. else if (part_of_punctuation(ch))
  434. return Punctuation;
  435. else
  436. return Unknown;
  437. };
  438. // A small explanation for the code below: Because the direction of the
  439. // movement for this motion determines what the "start" and "end" of a word
  440. // is, the code below treats the motions like so:
  441. // - Start of word: w/W/ge/gE
  442. // - End of word: e/E/b/B
  443. while (amount > 0) {
  444. if (cursor.hit_edge())
  445. break;
  446. if ((!cursor.forwards() && (m_unit == Unit::Word || m_unit == Unit::WORD))
  447. || (cursor.forwards() && (m_unit == Unit::EndOfWord || m_unit == Unit::EndOfWORD))) {
  448. // End-of-word motions peek at the "next" character and if its class
  449. // is not the same as ours, they move over one character (to end up
  450. // at the new character class). This is required because we don't
  451. // want to exit the word with end-of-word motions.
  452. if (m_unit == Unit::Word || m_unit == Unit::EndOfWord) {
  453. // Word-style peeking
  454. int current_class = classify(cursor.current_char());
  455. int peeked_class = classify(cursor.peek());
  456. if (current_class != peeked_class) {
  457. cursor.move();
  458. }
  459. } else {
  460. // WORD-style peeking, much simpler
  461. if (isspace(cursor.peek())) {
  462. cursor.move();
  463. }
  464. }
  465. } else {
  466. // Start-of-word motions want to exit the word no matter which part
  467. // of it we're in.
  468. if (m_unit == Unit::Word || m_unit == Unit::EndOfWord) {
  469. // Word-style consumption
  470. if (part_of_word(cursor.current_char())) {
  471. do {
  472. cursor.move();
  473. if (cursor.hit_edge() || cursor.crossed_line_boundary())
  474. break;
  475. } while (part_of_word(cursor.current_char()));
  476. } else if (part_of_punctuation(cursor.current_char())) {
  477. do {
  478. cursor.move();
  479. if (cursor.hit_edge() || cursor.crossed_line_boundary())
  480. break;
  481. } while (part_of_punctuation(cursor.current_char()));
  482. } else if (cursor.on_empty_line()) {
  483. cursor.move();
  484. }
  485. } else {
  486. // WORD-style consumption
  487. if (!isspace(cursor.current_char())) {
  488. do {
  489. cursor.move();
  490. if (cursor.hit_edge() || cursor.crossed_line_boundary())
  491. break;
  492. } while (!isspace(cursor.current_char()));
  493. } else if (cursor.on_empty_line()) {
  494. cursor.move();
  495. }
  496. }
  497. }
  498. // Now consume any space if it exists.
  499. if (isspace(cursor.current_char())) {
  500. do {
  501. cursor.move();
  502. if (cursor.hit_edge())
  503. break;
  504. } while (isspace(cursor.current_char()));
  505. }
  506. if ((!cursor.forwards() && (m_unit == Unit::Word || m_unit == Unit::WORD))
  507. || (cursor.forwards() && (m_unit == Unit::EndOfWord || m_unit == Unit::EndOfWORD))) {
  508. // End-of-word motions consume until the class doesn't match.
  509. if (m_unit == Unit::Word || m_unit == Unit::EndOfWord) {
  510. // Word-style consumption
  511. int current_class = classify(cursor.current_char());
  512. while (classify(cursor.current_char()) == current_class) {
  513. cursor.move();
  514. if (cursor.hit_edge() || cursor.crossed_line_boundary())
  515. break;
  516. }
  517. } else {
  518. // WORD-style consumption
  519. while (!isspace(cursor.current_char())) {
  520. cursor.move();
  521. if (cursor.hit_edge() || cursor.crossed_line_boundary())
  522. break;
  523. }
  524. }
  525. }
  526. amount--;
  527. }
  528. // If we need to normalize for position then we do a move_reverse for
  529. // end-of-word motions, because vim acts on end-of-word ranges through the
  530. // character your cursor is placed on but acts on start-of-words *until* the
  531. // character your cursor is placed on.
  532. if (normalize_for_position) {
  533. if ((!cursor.forwards() && (m_unit == Unit::Word || m_unit == Unit::WORD))
  534. || (cursor.forwards() && (m_unit == Unit::EndOfWord || m_unit == Unit::EndOfWORD))) {
  535. if (!cursor.hit_edge())
  536. cursor.move_reverse();
  537. }
  538. }
  539. if (cursor.forwards()) {
  540. m_end_line = cursor.current_position().line();
  541. m_end_column = cursor.current_position().column() + normalize_for_position;
  542. } else {
  543. m_start_line = cursor.current_position().line();
  544. m_start_column = cursor.current_position().column();
  545. }
  546. }
  547. void VimMotion::calculate_character_range(VimCursor& cursor, int amount, bool normalize_for_position)
  548. {
  549. if (m_amount == START_OF_LINE) {
  550. m_start_column = 0;
  551. } else if (m_amount == END_OF_LINE) {
  552. m_end_column = cursor.current_line().length();
  553. } else if (m_amount == START_OF_NON_WHITESPACE) {
  554. // Find the first non-whitespace character and set the range from current
  555. // position to it.
  556. TextPosition cursor_copy = cursor.current_position();
  557. cursor.current_position().set_column(0);
  558. while (isspace(cursor.current_char())) {
  559. if (cursor.will_cross_line_boundary())
  560. break;
  561. cursor.move_forwards();
  562. }
  563. if (cursor_copy < cursor.current_position())
  564. m_end_column = cursor.current_position().column() + 1;
  565. else
  566. m_start_column = cursor.current_position().column();
  567. } else {
  568. while (amount > 0) {
  569. if (cursor.hit_edge() || cursor.will_cross_line_boundary())
  570. break;
  571. cursor.move();
  572. amount--;
  573. }
  574. if (cursor.forwards()) {
  575. m_end_column = cursor.current_position().column() + 1 + normalize_for_position;
  576. } else {
  577. m_start_column = cursor.current_position().column();
  578. }
  579. }
  580. }
  581. void VimMotion::calculate_find_range(VimCursor& cursor, int amount)
  582. {
  583. // Find the searched character (case-insensitive).
  584. while (amount > 0) {
  585. cursor.move_forwards();
  586. while ((unsigned)tolower(cursor.current_char()) != m_next_character) {
  587. if (cursor.will_cross_line_boundary())
  588. break;
  589. cursor.move_forwards();
  590. }
  591. amount--;
  592. }
  593. // If we didn't find our character before reaching the end of the line, then
  594. // we want the range to be invalid so no operation is performed.
  595. if ((unsigned)tolower(cursor.current_char()) == m_next_character) {
  596. // We found our character.
  597. bool in_find_mode = m_find_mode == FindMode::Find;
  598. m_end_column = cursor.current_position().column() + in_find_mode;
  599. }
  600. m_find_mode = FindMode::None;
  601. }
  602. Optional<TextPosition> VimMotion::get_position(VimEditingEngine& engine, bool in_visual_mode)
  603. {
  604. auto range_optional = get_range(engine, true);
  605. if (!range_optional.has_value())
  606. return {};
  607. auto range = range_optional.value();
  608. if (!range.is_valid())
  609. return {};
  610. TextEditor& editor = engine.editor();
  611. auto cursor_position = editor.cursor();
  612. switch (m_unit) {
  613. case Unit::Document: {
  614. if (range.start().line() < cursor_position.line()) {
  615. cursor_position.set_line(range.start().line());
  616. } else {
  617. cursor_position.set_line(range.end().line());
  618. }
  619. cursor_position.set_column(0);
  620. return { cursor_position };
  621. }
  622. case Unit::Line: {
  623. size_t line_number;
  624. // Because we select lines from start to end, we can't use that
  625. // to get the new position, so we do some correction here.
  626. if (range.start().line() < cursor_position.line() || m_amount < 0) {
  627. line_number = range.start().line();
  628. } else {
  629. line_number = range.end().line();
  630. }
  631. auto& line = editor.line(line_number);
  632. cursor_position.set_line(line_number);
  633. if (line.length() <= cursor_position.column()) {
  634. cursor_position.set_column(line.length() - 1);
  635. }
  636. return { cursor_position };
  637. }
  638. default: {
  639. if (range.start() < cursor_position) {
  640. return { range.start() };
  641. } else {
  642. // Ranges are end-exclusive. The normalize_for_position argument we pass
  643. // above in get_range normalizes some values which shouldn't be
  644. // end-exclusive during normal operations.
  645. bool is_at_start = range.end().column() == 0;
  646. auto& line = editor.line(range.end().line());
  647. size_t column = is_at_start ? 0 : range.end().column() - 1;
  648. column = min(column, line.length() - (in_visual_mode ? 0 : 1));
  649. // Need to not go beyond the last character, as standard in vim.
  650. return { TextPosition { range.end().line(), column } };
  651. }
  652. }
  653. }
  654. }
  655. void VimMotion::reset()
  656. {
  657. m_unit = Unit::Unknown;
  658. m_amount = 0;
  659. m_is_complete = false;
  660. }
  661. CursorWidth VimEditingEngine::cursor_width() const
  662. {
  663. return m_vim_mode == VimMode::Insert ? CursorWidth::NARROW : CursorWidth::WIDE;
  664. }
  665. bool VimEditingEngine::on_key(KeyEvent const& event)
  666. {
  667. switch (m_vim_mode) {
  668. case (VimMode::Insert):
  669. return on_key_in_insert_mode(event);
  670. case (VimMode::Visual):
  671. return on_key_in_visual_mode(event);
  672. case (VimMode::VisualLine):
  673. return on_key_in_visual_line_mode(event);
  674. case (VimMode::Normal):
  675. return on_key_in_normal_mode(event);
  676. default:
  677. VERIFY_NOT_REACHED();
  678. }
  679. return false;
  680. }
  681. bool VimEditingEngine::on_key_in_insert_mode(KeyEvent const& event)
  682. {
  683. if (EditingEngine::on_key(event))
  684. return true;
  685. if (event.ctrl()) {
  686. switch (event.key()) {
  687. case KeyCode::Key_W:
  688. m_editor->delete_previous_word();
  689. return true;
  690. case KeyCode::Key_H:
  691. m_editor->delete_previous_char();
  692. return true;
  693. case KeyCode::Key_U:
  694. m_editor->delete_from_line_start_to_cursor();
  695. return true;
  696. default:
  697. break;
  698. }
  699. }
  700. if (event.key() == KeyCode::Key_Escape || (event.ctrl() && event.key() == KeyCode::Key_LeftBracket) || (event.ctrl() && event.key() == KeyCode::Key_C)) {
  701. if (m_editor->cursor().column() > 0)
  702. move_one_left();
  703. switch_to_normal_mode();
  704. return true;
  705. }
  706. return false;
  707. }
  708. bool VimEditingEngine::on_key_in_normal_mode(KeyEvent const& event)
  709. {
  710. // Ignore auxiliary keypress events.
  711. if (event.key() == KeyCode::Key_LeftShift
  712. || event.key() == KeyCode::Key_RightShift
  713. || event.key() == KeyCode::Key_Control
  714. || event.key() == KeyCode::Key_Alt) {
  715. return false;
  716. }
  717. if (m_previous_key == KeyCode::Key_D) {
  718. if (event.key() == KeyCode::Key_D && !m_motion.should_consume_next_character()) {
  719. if (m_motion.amount()) {
  720. auto range = m_motion.get_repeat_range(*this, VimMotion::Unit::Line);
  721. VERIFY(range.has_value());
  722. yank(*range, Line);
  723. m_editor->delete_text_range(*range);
  724. } else {
  725. yank(Line);
  726. delete_line();
  727. }
  728. m_motion.reset();
  729. m_previous_key = {};
  730. } else {
  731. m_motion.add_key_code(event.key(), event.ctrl(), event.shift(), event.alt());
  732. if (m_motion.is_complete()) {
  733. if (!m_motion.is_cancelled()) {
  734. auto range = m_motion.get_range(*this);
  735. VERIFY(range.has_value());
  736. if (range->is_valid()) {
  737. m_editor->delete_text_range(*range);
  738. }
  739. }
  740. m_motion.reset();
  741. m_previous_key = {};
  742. }
  743. }
  744. } else if (m_previous_key == KeyCode::Key_Y) {
  745. if (event.key() == KeyCode::Key_Y && !m_motion.should_consume_next_character()) {
  746. if (m_motion.amount()) {
  747. auto range = m_motion.get_repeat_range(*this, VimMotion::Unit::Line);
  748. VERIFY(range.has_value());
  749. yank(*range, Line);
  750. } else {
  751. yank(Line);
  752. }
  753. m_motion.reset();
  754. m_previous_key = {};
  755. } else {
  756. m_motion.add_key_code(event.key(), event.ctrl(), event.shift(), event.alt());
  757. if (m_motion.is_complete()) {
  758. if (!m_motion.is_cancelled()) {
  759. auto range = m_motion.get_range(*this);
  760. VERIFY(range.has_value());
  761. if (range->is_valid()) {
  762. m_editor->set_selection(*range);
  763. yank(Selection);
  764. m_editor->clear_selection();
  765. }
  766. }
  767. m_motion.reset();
  768. m_previous_key = {};
  769. }
  770. }
  771. } else if (m_previous_key == KeyCode::Key_C) {
  772. if (event.key() == KeyCode::Key_C && !m_motion.should_consume_next_character()) {
  773. // Needed because the code to replace the deleted line is called after delete_line() so
  774. // what was the second last line before the delete, is now the last line.
  775. bool was_second_last_line = m_editor->cursor().line() == m_editor->line_count() - 2;
  776. yank(Line);
  777. delete_line();
  778. if (was_second_last_line || (m_editor->cursor().line() != 0 && m_editor->cursor().line() != m_editor->line_count() - 1)) {
  779. move_one_up(event);
  780. move_to_logical_line_end();
  781. m_editor->add_code_point(0x0A);
  782. } else if (m_editor->cursor().line() == 0) {
  783. move_to_logical_line_beginning();
  784. m_editor->add_code_point(0x0A);
  785. move_one_up(event);
  786. } else if (m_editor->cursor().line() == m_editor->line_count() - 1) {
  787. m_editor->add_code_point(0x0A);
  788. }
  789. switch_to_insert_mode();
  790. } else {
  791. m_motion.add_key_code(event.key(), event.ctrl(), event.shift(), event.alt());
  792. if (m_motion.is_complete()) {
  793. if (!m_motion.is_cancelled()) {
  794. auto range = m_motion.get_range(*this);
  795. VERIFY(range.has_value());
  796. if (range->is_valid()) {
  797. m_editor->set_selection(*range);
  798. yank(Selection);
  799. m_editor->delete_text_range(*range);
  800. switch_to_insert_mode();
  801. }
  802. }
  803. m_motion.reset();
  804. m_previous_key = {};
  805. }
  806. }
  807. } else {
  808. if (m_motion.should_consume_next_character()) {
  809. // We must consume the next character.
  810. // FIXME: deduplicate with code below.
  811. m_motion.add_key_code(event.key(), event.ctrl(), event.shift(), event.alt());
  812. if (m_motion.is_complete()) {
  813. if (!m_motion.is_cancelled()) {
  814. auto maybe_new_position = m_motion.get_position(*this);
  815. if (maybe_new_position.has_value()) {
  816. auto new_position = maybe_new_position.value();
  817. m_editor->set_cursor(new_position);
  818. }
  819. }
  820. m_motion.reset();
  821. }
  822. return true;
  823. }
  824. // Handle first any key codes that are to be applied regardless of modifiers.
  825. switch (event.key()) {
  826. case (KeyCode::Key_Escape):
  827. return false;
  828. default:
  829. break;
  830. }
  831. // SHIFT is pressed.
  832. if (event.shift() && !event.ctrl() && !event.alt()) {
  833. switch (event.key()) {
  834. case (KeyCode::Key_A):
  835. move_to_logical_line_end();
  836. switch_to_insert_mode();
  837. return true;
  838. case (KeyCode::Key_D):
  839. m_editor->delete_text_range({ m_editor->cursor(), { m_editor->cursor().line(), m_editor->current_line().length() } });
  840. if (m_editor->cursor().column() != 0)
  841. move_one_left();
  842. break;
  843. case (KeyCode::Key_I):
  844. move_to_logical_line_beginning();
  845. switch_to_insert_mode();
  846. return true;
  847. case (KeyCode::Key_O):
  848. move_to_logical_line_beginning();
  849. m_editor->add_code_point(0x0A);
  850. move_one_up(event);
  851. switch_to_insert_mode();
  852. return true;
  853. case (KeyCode::Key_LeftBrace): {
  854. auto amount = m_motion.amount() > 0 ? m_motion.amount() : 1;
  855. m_motion.reset();
  856. for (int i = 0; i < amount; i++)
  857. move_to_previous_empty_lines_block();
  858. return true;
  859. }
  860. case (KeyCode::Key_RightBrace): {
  861. auto amount = m_motion.amount() > 0 ? m_motion.amount() : 1;
  862. m_motion.reset();
  863. for (int i = 0; i < amount; i++)
  864. move_to_next_empty_lines_block();
  865. return true;
  866. }
  867. case (KeyCode::Key_J): {
  868. // Looks a bit strange, but join without a repeat, with 1 as the repeat or 2 as the repeat all join the current and next lines
  869. auto amount = (m_motion.amount() > 2) ? (m_motion.amount() - 1) : 1;
  870. m_motion.reset();
  871. for (int i = 0; i < amount; i++) {
  872. if (m_editor->cursor().line() + 1 >= m_editor->line_count())
  873. return true;
  874. move_to_logical_line_end();
  875. m_editor->add_code_point(' ');
  876. TextPosition next_line = { m_editor->cursor().line() + 1, 0 };
  877. m_editor->delete_text_range({ m_editor->cursor(), next_line });
  878. move_one_left();
  879. }
  880. return true;
  881. }
  882. case (KeyCode::Key_P):
  883. put_before();
  884. break;
  885. case (KeyCode::Key_V):
  886. switch_to_visual_line_mode();
  887. return true;
  888. default:
  889. break;
  890. }
  891. }
  892. // CTRL is pressed.
  893. if (event.ctrl() && !event.shift() && !event.alt()) {
  894. switch (event.key()) {
  895. case (KeyCode::Key_D):
  896. move_half_page_down();
  897. return true;
  898. case (KeyCode::Key_R):
  899. m_editor->redo();
  900. return true;
  901. case (KeyCode::Key_U):
  902. move_half_page_up();
  903. return true;
  904. default:
  905. break;
  906. }
  907. }
  908. // FIXME: H and L movement keys will move to the previous or next line when reaching the beginning or end
  909. // of the line and pressed again.
  910. // No modifier is pressed.
  911. if (!event.ctrl() && !event.shift() && !event.alt()) {
  912. switch (event.key()) {
  913. case (KeyCode::Key_A):
  914. move_one_right();
  915. switch_to_insert_mode();
  916. return true;
  917. case (KeyCode::Key_C):
  918. m_previous_key = event.key();
  919. return true;
  920. case (KeyCode::Key_D):
  921. m_previous_key = event.key();
  922. return true;
  923. case (KeyCode::Key_I):
  924. switch_to_insert_mode();
  925. return true;
  926. case (KeyCode::Key_O):
  927. move_to_logical_line_end();
  928. m_editor->add_code_point(0x0A);
  929. switch_to_insert_mode();
  930. return true;
  931. case (KeyCode::Key_U):
  932. m_editor->undo();
  933. return true;
  934. case (KeyCode::Key_X): {
  935. TextRange range = { m_editor->cursor(), { m_editor->cursor().line(), m_editor->cursor().column() + 1 } };
  936. if (m_motion.amount()) {
  937. auto opt = m_motion.get_repeat_range(*this, VimMotion::Unit::Character);
  938. VERIFY(opt.has_value());
  939. range = *opt;
  940. m_motion.reset();
  941. }
  942. yank(range, Selection);
  943. m_editor->delete_text_range(range);
  944. return true;
  945. }
  946. case (KeyCode::Key_V):
  947. switch_to_visual_mode();
  948. return true;
  949. case (KeyCode::Key_Y):
  950. m_previous_key = event.key();
  951. return true;
  952. case (KeyCode::Key_P):
  953. put_after();
  954. return true;
  955. case (KeyCode::Key_PageUp):
  956. move_page_up();
  957. return true;
  958. case (KeyCode::Key_PageDown):
  959. move_page_down();
  960. return true;
  961. default:
  962. break;
  963. }
  964. }
  965. // If nothing else handled the key, we'll be feeding the motion state
  966. // machine instead.
  967. m_motion.add_key_code(event.key(), event.ctrl(), event.shift(), event.alt());
  968. if (m_motion.is_complete()) {
  969. if (!m_motion.is_cancelled()) {
  970. auto maybe_new_position = m_motion.get_position(*this);
  971. if (maybe_new_position.has_value()) {
  972. auto new_position = maybe_new_position.value();
  973. m_editor->set_cursor(new_position);
  974. }
  975. }
  976. m_motion.reset();
  977. }
  978. }
  979. return true;
  980. }
  981. bool VimEditingEngine::on_key_in_visual_mode(KeyEvent const& event)
  982. {
  983. // If the motion state machine requires the next character, feed it.
  984. if (m_motion.should_consume_next_character()) {
  985. m_motion.add_key_code(event.key(), event.ctrl(), event.shift(), event.alt());
  986. if (m_motion.is_complete()) {
  987. if (!m_motion.is_cancelled()) {
  988. auto maybe_new_position = m_motion.get_position(*this, true);
  989. if (maybe_new_position.has_value()) {
  990. auto new_position = maybe_new_position.value();
  991. m_editor->set_cursor(new_position);
  992. update_selection_on_cursor_move();
  993. }
  994. }
  995. m_motion.reset();
  996. }
  997. return true;
  998. }
  999. // Handle first any key codes that are to be applied regardless of modifiers.
  1000. switch (event.key()) {
  1001. case (KeyCode::Key_Escape):
  1002. switch_to_normal_mode();
  1003. return false;
  1004. default:
  1005. break;
  1006. }
  1007. // SHIFT is pressed.
  1008. if (event.shift() && !event.ctrl() && !event.alt()) {
  1009. switch (event.key()) {
  1010. case (KeyCode::Key_A):
  1011. move_to_logical_line_end();
  1012. switch_to_insert_mode();
  1013. return true;
  1014. case (KeyCode::Key_I):
  1015. move_to_logical_line_beginning();
  1016. switch_to_insert_mode();
  1017. return true;
  1018. case (KeyCode::Key_U):
  1019. casefold_selection(Casing::Uppercase);
  1020. switch_to_normal_mode();
  1021. return true;
  1022. case (KeyCode::Key_Tilde):
  1023. casefold_selection(Casing::Invertcase);
  1024. switch_to_normal_mode();
  1025. return true;
  1026. default:
  1027. break;
  1028. }
  1029. }
  1030. // CTRL is pressed.
  1031. if (event.ctrl() && !event.shift() && !event.alt()) {
  1032. switch (event.key()) {
  1033. case (KeyCode::Key_D):
  1034. move_half_page_down();
  1035. update_selection_on_cursor_move();
  1036. return true;
  1037. case (KeyCode::Key_U):
  1038. move_half_page_up();
  1039. update_selection_on_cursor_move();
  1040. return true;
  1041. default:
  1042. break;
  1043. }
  1044. }
  1045. // No modifier is pressed.
  1046. if (!event.ctrl() && !event.shift() && !event.alt()) {
  1047. switch (event.key()) {
  1048. case (KeyCode::Key_D):
  1049. yank(Selection);
  1050. m_editor->do_delete();
  1051. switch_to_normal_mode();
  1052. return true;
  1053. case (KeyCode::Key_X):
  1054. yank(Selection);
  1055. m_editor->do_delete();
  1056. switch_to_normal_mode();
  1057. return true;
  1058. case (KeyCode::Key_V):
  1059. switch_to_normal_mode();
  1060. return true;
  1061. case (KeyCode::Key_C):
  1062. yank(Selection);
  1063. m_editor->do_delete();
  1064. switch_to_insert_mode();
  1065. return true;
  1066. case (KeyCode::Key_Y):
  1067. yank(Selection);
  1068. switch_to_normal_mode();
  1069. return true;
  1070. case (KeyCode::Key_U):
  1071. casefold_selection(Casing::Lowercase);
  1072. switch_to_normal_mode();
  1073. return true;
  1074. case (KeyCode::Key_PageUp):
  1075. move_page_up();
  1076. update_selection_on_cursor_move();
  1077. return true;
  1078. case (KeyCode::Key_PageDown):
  1079. move_page_down();
  1080. update_selection_on_cursor_move();
  1081. return true;
  1082. default:
  1083. break;
  1084. }
  1085. }
  1086. // By default, we feed the motion state machine.
  1087. m_motion.add_key_code(event.key(), event.ctrl(), event.shift(), event.alt());
  1088. if (m_motion.is_complete()) {
  1089. if (!m_motion.is_cancelled()) {
  1090. auto maybe_new_position = m_motion.get_position(*this, true);
  1091. if (maybe_new_position.has_value()) {
  1092. auto new_position = maybe_new_position.value();
  1093. m_editor->set_cursor(new_position);
  1094. update_selection_on_cursor_move();
  1095. }
  1096. }
  1097. m_motion.reset();
  1098. }
  1099. return true;
  1100. }
  1101. bool VimEditingEngine::on_key_in_visual_line_mode(KeyEvent const& event)
  1102. {
  1103. // If the motion state machine requires the next character, feed it.
  1104. if (m_motion.should_consume_next_character()) {
  1105. m_motion.add_key_code(event.key(), event.ctrl(), event.shift(), event.alt());
  1106. if (m_motion.is_complete()) {
  1107. if (!m_motion.is_cancelled()) {
  1108. auto maybe_new_position = m_motion.get_position(*this, true);
  1109. if (maybe_new_position.has_value()) {
  1110. auto new_position = maybe_new_position.value();
  1111. m_editor->set_cursor(new_position);
  1112. update_selection_on_cursor_move();
  1113. }
  1114. }
  1115. m_motion.reset();
  1116. }
  1117. return true;
  1118. }
  1119. // Handle first any key codes that are to be applied regardless of modifiers.
  1120. switch (event.key()) {
  1121. case (KeyCode::Key_Escape):
  1122. switch_to_normal_mode();
  1123. return false;
  1124. default:
  1125. break;
  1126. }
  1127. // SHIFT is pressed.
  1128. if (event.shift() && !event.ctrl() && !event.alt()) {
  1129. switch (event.key()) {
  1130. case (KeyCode::Key_U):
  1131. casefold_selection(Casing::Uppercase);
  1132. switch_to_normal_mode();
  1133. return true;
  1134. case (KeyCode::Key_Tilde):
  1135. casefold_selection(Casing::Invertcase);
  1136. switch_to_normal_mode();
  1137. return true;
  1138. default:
  1139. break;
  1140. }
  1141. }
  1142. // CTRL is pressed.
  1143. if (event.ctrl() && !event.shift() && !event.alt()) {
  1144. switch (event.key()) {
  1145. case (KeyCode::Key_D):
  1146. move_half_page_down();
  1147. update_selection_on_cursor_move();
  1148. return true;
  1149. case (KeyCode::Key_U):
  1150. move_half_page_up();
  1151. update_selection_on_cursor_move();
  1152. return true;
  1153. default:
  1154. break;
  1155. }
  1156. }
  1157. // No modifier is pressed.
  1158. if (!event.ctrl() && !event.shift() && !event.alt()) {
  1159. switch (event.key()) {
  1160. case (KeyCode::Key_D):
  1161. yank(m_editor->selection(), Line);
  1162. m_editor->do_delete();
  1163. switch_to_normal_mode();
  1164. return true;
  1165. case (KeyCode::Key_X):
  1166. yank(m_editor->selection(), Line);
  1167. m_editor->do_delete();
  1168. switch_to_normal_mode();
  1169. return true;
  1170. case (KeyCode::Key_C):
  1171. yank(m_editor->selection(), Line);
  1172. m_editor->do_delete();
  1173. switch_to_insert_mode();
  1174. return true;
  1175. case (KeyCode::Key_Y):
  1176. yank(m_editor->selection(), Line);
  1177. switch_to_normal_mode();
  1178. return true;
  1179. case (KeyCode::Key_U):
  1180. casefold_selection(Casing::Lowercase);
  1181. switch_to_normal_mode();
  1182. return true;
  1183. case (KeyCode::Key_PageUp):
  1184. move_page_up();
  1185. update_selection_on_cursor_move();
  1186. return true;
  1187. case (KeyCode::Key_PageDown):
  1188. move_page_down();
  1189. update_selection_on_cursor_move();
  1190. return true;
  1191. default:
  1192. break;
  1193. }
  1194. }
  1195. // By default, we feed the motion state machine.
  1196. m_motion.add_key_code(event.key(), event.ctrl(), event.shift(), event.alt());
  1197. if (m_motion.is_complete()) {
  1198. if (!m_motion.is_cancelled()) {
  1199. auto maybe_new_position = m_motion.get_position(*this, true);
  1200. if (maybe_new_position.has_value()) {
  1201. auto new_position = maybe_new_position.value();
  1202. m_editor->set_cursor(new_position);
  1203. update_selection_on_cursor_move();
  1204. }
  1205. }
  1206. m_motion.reset();
  1207. }
  1208. return true;
  1209. }
  1210. void VimEditingEngine::switch_to_normal_mode()
  1211. {
  1212. m_vim_mode = VimMode::Normal;
  1213. m_editor->reset_cursor_blink();
  1214. m_previous_key = {};
  1215. clear_visual_mode_data();
  1216. m_motion.reset();
  1217. }
  1218. void VimEditingEngine::switch_to_insert_mode()
  1219. {
  1220. m_vim_mode = VimMode::Insert;
  1221. m_editor->reset_cursor_blink();
  1222. m_previous_key = {};
  1223. clear_visual_mode_data();
  1224. m_motion.reset();
  1225. }
  1226. void VimEditingEngine::switch_to_visual_mode()
  1227. {
  1228. m_vim_mode = VimMode::Visual;
  1229. m_editor->reset_cursor_blink();
  1230. m_previous_key = {};
  1231. m_selection_start_position = m_editor->cursor();
  1232. m_editor->selection().set(m_editor->cursor(), { m_editor->cursor().line(), m_editor->cursor().column() + 1 });
  1233. m_editor->did_update_selection();
  1234. m_motion.reset();
  1235. }
  1236. void VimEditingEngine::switch_to_visual_line_mode()
  1237. {
  1238. m_vim_mode = VimMode::VisualLine;
  1239. m_editor->reset_cursor_blink();
  1240. m_previous_key = {};
  1241. m_selection_start_position = TextPosition { m_editor->cursor().line(), 0 };
  1242. m_editor->selection().set(m_selection_start_position, { m_editor->cursor().line(), m_editor->current_line().length() });
  1243. m_editor->did_update_selection();
  1244. m_motion.reset();
  1245. }
  1246. void VimEditingEngine::update_selection_on_cursor_move()
  1247. {
  1248. auto cursor = m_editor->cursor();
  1249. auto start = m_selection_start_position < cursor ? m_selection_start_position : cursor;
  1250. auto end = m_selection_start_position < cursor ? cursor : m_selection_start_position;
  1251. if (end.column() >= m_editor->current_line().length()) {
  1252. if (end.line() != m_editor->line_count() - 1)
  1253. end = { end.line() + 1, 0 };
  1254. } else {
  1255. end.set_column(end.column() + 1);
  1256. }
  1257. if (m_vim_mode == VimMode::VisualLine) {
  1258. start = TextPosition { start.line(), 0 };
  1259. end = TextPosition { end.line(), m_editor->line(end.line()).length() };
  1260. }
  1261. m_editor->selection().set(start, end);
  1262. m_editor->did_update_selection();
  1263. }
  1264. void VimEditingEngine::clamp_cursor_position()
  1265. {
  1266. auto cursor = m_editor->cursor();
  1267. if (cursor.column() >= m_editor->current_line().length()) {
  1268. cursor.set_column(m_editor->current_line().length() - 1);
  1269. m_editor->set_cursor(cursor);
  1270. }
  1271. }
  1272. void VimEditingEngine::clear_visual_mode_data()
  1273. {
  1274. if (m_editor->has_selection()) {
  1275. m_editor->selection().clear();
  1276. m_editor->did_update_selection();
  1277. clamp_cursor_position();
  1278. }
  1279. m_selection_start_position = {};
  1280. }
  1281. void VimEditingEngine::move_half_page_up()
  1282. {
  1283. move_up(0.5);
  1284. }
  1285. void VimEditingEngine::move_half_page_down()
  1286. {
  1287. move_down(0.5);
  1288. }
  1289. void VimEditingEngine::yank(YankType type)
  1290. {
  1291. m_yank_type = type;
  1292. if (type == YankType::Line) {
  1293. m_yank_buffer = m_editor->current_line().to_utf8();
  1294. } else {
  1295. m_yank_buffer = m_editor->selected_text();
  1296. }
  1297. // When putting this, auto indentation (if enabled) will indent as far as
  1298. // is necessary, then any whitespace captured before the yanked text will be placed
  1299. // after the indentation, doubling the indentation.
  1300. if (m_editor->is_automatic_indentation_enabled())
  1301. m_yank_buffer = m_yank_buffer.trim_whitespace(TrimMode::Left);
  1302. }
  1303. void VimEditingEngine::yank(TextRange range, YankType yank_type)
  1304. {
  1305. m_yank_type = yank_type;
  1306. m_yank_buffer = m_editor->document().text_in_range(range).trim_whitespace(AK::TrimMode::Right);
  1307. }
  1308. void VimEditingEngine::put_before()
  1309. {
  1310. auto amount = m_motion.amount() ? m_motion.amount() : 1;
  1311. m_motion.reset();
  1312. if (m_yank_type == YankType::Line) {
  1313. move_to_logical_line_beginning();
  1314. StringBuilder sb = StringBuilder(amount * (m_yank_buffer.length() + 1));
  1315. for (auto i = 0; i < amount; i++) {
  1316. sb.append(m_yank_buffer);
  1317. sb.append_code_point(0x0A);
  1318. }
  1319. m_editor->insert_at_cursor_or_replace_selection(sb.to_deprecated_string());
  1320. m_editor->set_cursor({ m_editor->cursor().line(), m_editor->current_line().first_non_whitespace_column() });
  1321. } else {
  1322. StringBuilder sb = StringBuilder(m_yank_buffer.length() * amount);
  1323. for (auto i = 0; i < amount; i++) {
  1324. sb.append(m_yank_buffer);
  1325. }
  1326. m_editor->insert_at_cursor_or_replace_selection(sb.to_deprecated_string());
  1327. move_one_left();
  1328. }
  1329. }
  1330. void VimEditingEngine::put_after()
  1331. {
  1332. auto amount = m_motion.amount() ? m_motion.amount() : 1;
  1333. m_motion.reset();
  1334. if (m_yank_type == YankType::Line) {
  1335. move_to_logical_line_end();
  1336. StringBuilder sb = StringBuilder(m_yank_buffer.length() + 1);
  1337. for (auto i = 0; i < amount; i++) {
  1338. sb.append_code_point(0x0A);
  1339. sb.append(m_yank_buffer);
  1340. }
  1341. m_editor->insert_at_cursor_or_replace_selection(sb.to_deprecated_string());
  1342. m_editor->set_cursor({ m_editor->cursor().line(), m_editor->current_line().first_non_whitespace_column() });
  1343. } else {
  1344. // FIXME: If attempting to put on the last column a line,
  1345. // the buffer will bne placed on the next line due to the move_one_left/right behavior.
  1346. move_one_right();
  1347. StringBuilder sb = StringBuilder(m_yank_buffer.length() * amount);
  1348. for (auto i = 0; i < amount; i++) {
  1349. sb.append(m_yank_buffer);
  1350. }
  1351. m_editor->insert_at_cursor_or_replace_selection(sb.to_deprecated_string());
  1352. move_one_left();
  1353. }
  1354. }
  1355. void VimEditingEngine::move_to_previous_empty_lines_block()
  1356. {
  1357. VERIFY(!m_editor.is_null());
  1358. size_t line_idx = m_editor->cursor().line();
  1359. bool skipping_initial_empty_lines = true;
  1360. while (line_idx > 0) {
  1361. if (m_editor->document().line(line_idx).is_empty()) {
  1362. if (!skipping_initial_empty_lines)
  1363. break;
  1364. } else {
  1365. skipping_initial_empty_lines = false;
  1366. }
  1367. line_idx--;
  1368. }
  1369. TextPosition new_cursor = { line_idx, 0 };
  1370. m_editor->set_cursor(new_cursor);
  1371. }
  1372. void VimEditingEngine::move_to_next_empty_lines_block()
  1373. {
  1374. VERIFY(!m_editor.is_null());
  1375. size_t line_idx = m_editor->cursor().line();
  1376. bool skipping_initial_empty_lines = true;
  1377. while (line_idx < m_editor->line_count() - 1) {
  1378. if (m_editor->document().line(line_idx).is_empty()) {
  1379. if (!skipping_initial_empty_lines)
  1380. break;
  1381. } else {
  1382. skipping_initial_empty_lines = false;
  1383. }
  1384. line_idx++;
  1385. }
  1386. TextPosition new_cursor = { line_idx, 0 };
  1387. m_editor->set_cursor(new_cursor);
  1388. }
  1389. void VimEditingEngine::casefold_selection(Casing casing)
  1390. {
  1391. VERIFY(!m_editor.is_null());
  1392. VERIFY(m_editor->has_selection());
  1393. switch (casing) {
  1394. case Casing::Uppercase:
  1395. m_editor->insert_at_cursor_or_replace_selection(m_editor->selected_text().to_uppercase());
  1396. return;
  1397. case Casing::Lowercase:
  1398. m_editor->insert_at_cursor_or_replace_selection(m_editor->selected_text().to_lowercase());
  1399. return;
  1400. case Casing::Invertcase:
  1401. m_editor->insert_at_cursor_or_replace_selection(m_editor->selected_text().invert_case());
  1402. return;
  1403. }
  1404. }
  1405. }