VimEditingEngine.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  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. if (m_editor->on_escape_pressed)
  828. m_editor->on_escape_pressed();
  829. return true;
  830. default:
  831. break;
  832. }
  833. // SHIFT is pressed.
  834. if (event.shift() && !event.ctrl() && !event.alt()) {
  835. switch (event.key()) {
  836. case (KeyCode::Key_A):
  837. move_to_logical_line_end();
  838. switch_to_insert_mode();
  839. return true;
  840. case (KeyCode::Key_D):
  841. m_editor->delete_text_range({ m_editor->cursor(), { m_editor->cursor().line(), m_editor->current_line().length() } });
  842. if (m_editor->cursor().column() != 0)
  843. move_one_left();
  844. break;
  845. case (KeyCode::Key_I):
  846. move_to_logical_line_beginning();
  847. switch_to_insert_mode();
  848. return true;
  849. case (KeyCode::Key_O):
  850. move_to_logical_line_beginning();
  851. m_editor->add_code_point(0x0A);
  852. move_one_up(event);
  853. switch_to_insert_mode();
  854. return true;
  855. case (KeyCode::Key_LeftBrace): {
  856. auto amount = m_motion.amount() > 0 ? m_motion.amount() : 1;
  857. m_motion.reset();
  858. for (int i = 0; i < amount; i++)
  859. move_to_previous_empty_lines_block();
  860. return true;
  861. }
  862. case (KeyCode::Key_RightBrace): {
  863. auto amount = m_motion.amount() > 0 ? m_motion.amount() : 1;
  864. m_motion.reset();
  865. for (int i = 0; i < amount; i++)
  866. move_to_next_empty_lines_block();
  867. return true;
  868. }
  869. case (KeyCode::Key_J): {
  870. // 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
  871. auto amount = (m_motion.amount() > 2) ? (m_motion.amount() - 1) : 1;
  872. m_motion.reset();
  873. for (int i = 0; i < amount; i++) {
  874. if (m_editor->cursor().line() + 1 >= m_editor->line_count())
  875. return true;
  876. move_to_logical_line_end();
  877. m_editor->add_code_point(' ');
  878. TextPosition next_line = { m_editor->cursor().line() + 1, 0 };
  879. m_editor->delete_text_range({ m_editor->cursor(), next_line });
  880. move_one_left();
  881. }
  882. return true;
  883. }
  884. case (KeyCode::Key_P):
  885. put_before();
  886. break;
  887. case (KeyCode::Key_V):
  888. switch_to_visual_line_mode();
  889. return true;
  890. default:
  891. break;
  892. }
  893. }
  894. // CTRL is pressed.
  895. if (event.ctrl() && !event.shift() && !event.alt()) {
  896. switch (event.key()) {
  897. case (KeyCode::Key_D):
  898. move_half_page_down();
  899. return true;
  900. case (KeyCode::Key_R):
  901. m_editor->redo();
  902. return true;
  903. case (KeyCode::Key_U):
  904. move_half_page_up();
  905. return true;
  906. default:
  907. break;
  908. }
  909. }
  910. // FIXME: H and L movement keys will move to the previous or next line when reaching the beginning or end
  911. // of the line and pressed again.
  912. // No modifier is pressed.
  913. if (!event.ctrl() && !event.shift() && !event.alt()) {
  914. switch (event.key()) {
  915. case (KeyCode::Key_A):
  916. move_one_right();
  917. switch_to_insert_mode();
  918. return true;
  919. case (KeyCode::Key_C):
  920. m_previous_key = event.key();
  921. return true;
  922. case (KeyCode::Key_D):
  923. m_previous_key = event.key();
  924. return true;
  925. case (KeyCode::Key_I):
  926. switch_to_insert_mode();
  927. return true;
  928. case (KeyCode::Key_O):
  929. move_to_logical_line_end();
  930. m_editor->add_code_point(0x0A);
  931. switch_to_insert_mode();
  932. return true;
  933. case (KeyCode::Key_U):
  934. m_editor->undo();
  935. return true;
  936. case (KeyCode::Key_X): {
  937. TextRange range = { m_editor->cursor(), { m_editor->cursor().line(), m_editor->cursor().column() + 1 } };
  938. if (m_motion.amount()) {
  939. auto opt = m_motion.get_repeat_range(*this, VimMotion::Unit::Character);
  940. VERIFY(opt.has_value());
  941. range = *opt;
  942. m_motion.reset();
  943. }
  944. yank(range, Selection);
  945. m_editor->delete_text_range(range);
  946. return true;
  947. }
  948. case (KeyCode::Key_V):
  949. switch_to_visual_mode();
  950. return true;
  951. case (KeyCode::Key_Y):
  952. m_previous_key = event.key();
  953. return true;
  954. case (KeyCode::Key_P):
  955. put_after();
  956. return true;
  957. case (KeyCode::Key_PageUp):
  958. move_page_up();
  959. return true;
  960. case (KeyCode::Key_PageDown):
  961. move_page_down();
  962. return true;
  963. default:
  964. break;
  965. }
  966. }
  967. // If nothing else handled the key, we'll be feeding the motion state
  968. // machine instead.
  969. m_motion.add_key_code(event.key(), event.ctrl(), event.shift(), event.alt());
  970. if (m_motion.is_complete()) {
  971. if (!m_motion.is_cancelled()) {
  972. auto maybe_new_position = m_motion.get_position(*this);
  973. if (maybe_new_position.has_value()) {
  974. auto new_position = maybe_new_position.value();
  975. m_editor->set_cursor(new_position);
  976. }
  977. }
  978. m_motion.reset();
  979. }
  980. }
  981. return true;
  982. }
  983. bool VimEditingEngine::on_key_in_visual_mode(KeyEvent const& event)
  984. {
  985. // If the motion state machine requires the next character, feed it.
  986. if (m_motion.should_consume_next_character()) {
  987. m_motion.add_key_code(event.key(), event.ctrl(), event.shift(), event.alt());
  988. if (m_motion.is_complete()) {
  989. if (!m_motion.is_cancelled()) {
  990. auto maybe_new_position = m_motion.get_position(*this, true);
  991. if (maybe_new_position.has_value()) {
  992. auto new_position = maybe_new_position.value();
  993. m_editor->set_cursor(new_position);
  994. update_selection_on_cursor_move();
  995. }
  996. }
  997. m_motion.reset();
  998. }
  999. return true;
  1000. }
  1001. // Handle first any key codes that are to be applied regardless of modifiers.
  1002. switch (event.key()) {
  1003. case (KeyCode::Key_Escape):
  1004. switch_to_normal_mode();
  1005. if (m_editor->on_escape_pressed)
  1006. m_editor->on_escape_pressed();
  1007. return true;
  1008. default:
  1009. break;
  1010. }
  1011. // SHIFT is pressed.
  1012. if (event.shift() && !event.ctrl() && !event.alt()) {
  1013. switch (event.key()) {
  1014. case (KeyCode::Key_A):
  1015. move_to_logical_line_end();
  1016. switch_to_insert_mode();
  1017. return true;
  1018. case (KeyCode::Key_I):
  1019. move_to_logical_line_beginning();
  1020. switch_to_insert_mode();
  1021. return true;
  1022. case (KeyCode::Key_U):
  1023. casefold_selection(Casing::Uppercase);
  1024. switch_to_normal_mode();
  1025. return true;
  1026. case (KeyCode::Key_Tilde):
  1027. casefold_selection(Casing::Invertcase);
  1028. switch_to_normal_mode();
  1029. return true;
  1030. default:
  1031. break;
  1032. }
  1033. }
  1034. // CTRL is pressed.
  1035. if (event.ctrl() && !event.shift() && !event.alt()) {
  1036. switch (event.key()) {
  1037. case (KeyCode::Key_D):
  1038. move_half_page_down();
  1039. update_selection_on_cursor_move();
  1040. return true;
  1041. case (KeyCode::Key_U):
  1042. move_half_page_up();
  1043. update_selection_on_cursor_move();
  1044. return true;
  1045. default:
  1046. break;
  1047. }
  1048. }
  1049. // No modifier is pressed.
  1050. if (!event.ctrl() && !event.shift() && !event.alt()) {
  1051. switch (event.key()) {
  1052. case (KeyCode::Key_D):
  1053. yank(Selection);
  1054. m_editor->do_delete();
  1055. switch_to_normal_mode();
  1056. return true;
  1057. case (KeyCode::Key_X):
  1058. yank(Selection);
  1059. m_editor->do_delete();
  1060. switch_to_normal_mode();
  1061. return true;
  1062. case (KeyCode::Key_V):
  1063. switch_to_normal_mode();
  1064. return true;
  1065. case (KeyCode::Key_C):
  1066. yank(Selection);
  1067. m_editor->do_delete();
  1068. switch_to_insert_mode();
  1069. return true;
  1070. case (KeyCode::Key_Y):
  1071. yank(Selection);
  1072. switch_to_normal_mode();
  1073. return true;
  1074. case (KeyCode::Key_U):
  1075. casefold_selection(Casing::Lowercase);
  1076. switch_to_normal_mode();
  1077. return true;
  1078. case (KeyCode::Key_PageUp):
  1079. move_page_up();
  1080. update_selection_on_cursor_move();
  1081. return true;
  1082. case (KeyCode::Key_PageDown):
  1083. move_page_down();
  1084. update_selection_on_cursor_move();
  1085. return true;
  1086. default:
  1087. break;
  1088. }
  1089. }
  1090. // By default, we feed the motion state machine.
  1091. m_motion.add_key_code(event.key(), event.ctrl(), event.shift(), event.alt());
  1092. if (m_motion.is_complete()) {
  1093. if (!m_motion.is_cancelled()) {
  1094. auto maybe_new_position = m_motion.get_position(*this, true);
  1095. if (maybe_new_position.has_value()) {
  1096. auto new_position = maybe_new_position.value();
  1097. m_editor->set_cursor(new_position);
  1098. update_selection_on_cursor_move();
  1099. }
  1100. }
  1101. m_motion.reset();
  1102. }
  1103. return true;
  1104. }
  1105. bool VimEditingEngine::on_key_in_visual_line_mode(KeyEvent const& event)
  1106. {
  1107. // If the motion state machine requires the next character, feed it.
  1108. if (m_motion.should_consume_next_character()) {
  1109. m_motion.add_key_code(event.key(), event.ctrl(), event.shift(), event.alt());
  1110. if (m_motion.is_complete()) {
  1111. if (!m_motion.is_cancelled()) {
  1112. auto maybe_new_position = m_motion.get_position(*this, true);
  1113. if (maybe_new_position.has_value()) {
  1114. auto new_position = maybe_new_position.value();
  1115. m_editor->set_cursor(new_position);
  1116. update_selection_on_cursor_move();
  1117. }
  1118. }
  1119. m_motion.reset();
  1120. }
  1121. return true;
  1122. }
  1123. // Handle first any key codes that are to be applied regardless of modifiers.
  1124. switch (event.key()) {
  1125. case (KeyCode::Key_Escape):
  1126. switch_to_normal_mode();
  1127. if (m_editor->on_escape_pressed)
  1128. m_editor->on_escape_pressed();
  1129. return true;
  1130. default:
  1131. break;
  1132. }
  1133. // SHIFT is pressed.
  1134. if (event.shift() && !event.ctrl() && !event.alt()) {
  1135. switch (event.key()) {
  1136. case (KeyCode::Key_U):
  1137. casefold_selection(Casing::Uppercase);
  1138. switch_to_normal_mode();
  1139. return true;
  1140. case (KeyCode::Key_Tilde):
  1141. casefold_selection(Casing::Invertcase);
  1142. switch_to_normal_mode();
  1143. return true;
  1144. default:
  1145. break;
  1146. }
  1147. }
  1148. // CTRL is pressed.
  1149. if (event.ctrl() && !event.shift() && !event.alt()) {
  1150. switch (event.key()) {
  1151. case (KeyCode::Key_D):
  1152. move_half_page_down();
  1153. update_selection_on_cursor_move();
  1154. return true;
  1155. case (KeyCode::Key_U):
  1156. move_half_page_up();
  1157. update_selection_on_cursor_move();
  1158. return true;
  1159. default:
  1160. break;
  1161. }
  1162. }
  1163. // No modifier is pressed.
  1164. if (!event.ctrl() && !event.shift() && !event.alt()) {
  1165. switch (event.key()) {
  1166. case (KeyCode::Key_D):
  1167. yank(m_editor->selection(), Line);
  1168. m_editor->do_delete();
  1169. switch_to_normal_mode();
  1170. return true;
  1171. case (KeyCode::Key_X):
  1172. yank(m_editor->selection(), Line);
  1173. m_editor->do_delete();
  1174. switch_to_normal_mode();
  1175. return true;
  1176. case (KeyCode::Key_C):
  1177. yank(m_editor->selection(), Line);
  1178. m_editor->do_delete();
  1179. switch_to_insert_mode();
  1180. return true;
  1181. case (KeyCode::Key_Y):
  1182. yank(m_editor->selection(), Line);
  1183. switch_to_normal_mode();
  1184. return true;
  1185. case (KeyCode::Key_U):
  1186. casefold_selection(Casing::Lowercase);
  1187. switch_to_normal_mode();
  1188. return true;
  1189. case (KeyCode::Key_PageUp):
  1190. move_page_up();
  1191. update_selection_on_cursor_move();
  1192. return true;
  1193. case (KeyCode::Key_PageDown):
  1194. move_page_down();
  1195. update_selection_on_cursor_move();
  1196. return true;
  1197. default:
  1198. break;
  1199. }
  1200. }
  1201. // By default, we feed the motion state machine.
  1202. m_motion.add_key_code(event.key(), event.ctrl(), event.shift(), event.alt());
  1203. if (m_motion.is_complete()) {
  1204. if (!m_motion.is_cancelled()) {
  1205. auto maybe_new_position = m_motion.get_position(*this, true);
  1206. if (maybe_new_position.has_value()) {
  1207. auto new_position = maybe_new_position.value();
  1208. m_editor->set_cursor(new_position);
  1209. update_selection_on_cursor_move();
  1210. }
  1211. }
  1212. m_motion.reset();
  1213. }
  1214. return true;
  1215. }
  1216. void VimEditingEngine::switch_to_normal_mode()
  1217. {
  1218. m_vim_mode = VimMode::Normal;
  1219. m_editor->reset_cursor_blink();
  1220. m_previous_key = {};
  1221. clear_visual_mode_data();
  1222. m_motion.reset();
  1223. };
  1224. void VimEditingEngine::switch_to_insert_mode()
  1225. {
  1226. m_vim_mode = VimMode::Insert;
  1227. m_editor->reset_cursor_blink();
  1228. m_previous_key = {};
  1229. clear_visual_mode_data();
  1230. m_motion.reset();
  1231. };
  1232. void VimEditingEngine::switch_to_visual_mode()
  1233. {
  1234. m_vim_mode = VimMode::Visual;
  1235. m_editor->reset_cursor_blink();
  1236. m_previous_key = {};
  1237. m_selection_start_position = m_editor->cursor();
  1238. m_editor->selection().set(m_editor->cursor(), { m_editor->cursor().line(), m_editor->cursor().column() + 1 });
  1239. m_editor->did_update_selection();
  1240. m_motion.reset();
  1241. }
  1242. void VimEditingEngine::switch_to_visual_line_mode()
  1243. {
  1244. m_vim_mode = VimMode::VisualLine;
  1245. m_editor->reset_cursor_blink();
  1246. m_previous_key = {};
  1247. m_selection_start_position = TextPosition { m_editor->cursor().line(), 0 };
  1248. m_editor->selection().set(m_selection_start_position, { m_editor->cursor().line(), m_editor->current_line().length() });
  1249. m_editor->did_update_selection();
  1250. m_motion.reset();
  1251. }
  1252. void VimEditingEngine::update_selection_on_cursor_move()
  1253. {
  1254. auto cursor = m_editor->cursor();
  1255. auto start = m_selection_start_position < cursor ? m_selection_start_position : cursor;
  1256. auto end = m_selection_start_position < cursor ? cursor : m_selection_start_position;
  1257. if (end.column() >= m_editor->current_line().length()) {
  1258. if (end.line() != m_editor->line_count() - 1)
  1259. end = { end.line() + 1, 0 };
  1260. } else {
  1261. end.set_column(end.column() + 1);
  1262. }
  1263. if (m_vim_mode == VimMode::VisualLine) {
  1264. start = TextPosition { start.line(), 0 };
  1265. end = TextPosition { end.line(), m_editor->line(end.line()).length() };
  1266. }
  1267. m_editor->selection().set(start, end);
  1268. m_editor->did_update_selection();
  1269. }
  1270. void VimEditingEngine::clamp_cursor_position()
  1271. {
  1272. auto cursor = m_editor->cursor();
  1273. if (cursor.column() >= m_editor->current_line().length()) {
  1274. cursor.set_column(m_editor->current_line().length() - 1);
  1275. m_editor->set_cursor(cursor);
  1276. }
  1277. }
  1278. void VimEditingEngine::clear_visual_mode_data()
  1279. {
  1280. if (m_editor->has_selection()) {
  1281. m_editor->selection().clear();
  1282. m_editor->did_update_selection();
  1283. clamp_cursor_position();
  1284. }
  1285. m_selection_start_position = {};
  1286. }
  1287. void VimEditingEngine::move_half_page_up()
  1288. {
  1289. move_up(0.5);
  1290. };
  1291. void VimEditingEngine::move_half_page_down()
  1292. {
  1293. move_down(0.5);
  1294. };
  1295. void VimEditingEngine::yank(YankType type)
  1296. {
  1297. m_yank_type = type;
  1298. if (type == YankType::Line) {
  1299. m_yank_buffer = m_editor->current_line().to_utf8();
  1300. } else {
  1301. m_yank_buffer = m_editor->selected_text();
  1302. }
  1303. // When putting this, auto indentation (if enabled) will indent as far as
  1304. // is necessary, then any whitespace captured before the yanked text will be placed
  1305. // after the indentation, doubling the indentation.
  1306. if (m_editor->is_automatic_indentation_enabled())
  1307. m_yank_buffer = m_yank_buffer.trim_whitespace(TrimMode::Left);
  1308. }
  1309. void VimEditingEngine::yank(TextRange range, YankType yank_type)
  1310. {
  1311. m_yank_type = yank_type;
  1312. m_yank_buffer = m_editor->document().text_in_range(range).trim_whitespace(AK::TrimMode::Right);
  1313. }
  1314. void VimEditingEngine::put_before()
  1315. {
  1316. auto amount = m_motion.amount() ? m_motion.amount() : 1;
  1317. m_motion.reset();
  1318. if (m_yank_type == YankType::Line) {
  1319. move_to_logical_line_beginning();
  1320. StringBuilder sb = StringBuilder(amount * (m_yank_buffer.length() + 1));
  1321. for (auto i = 0; i < amount; i++) {
  1322. sb.append(m_yank_buffer);
  1323. sb.append_code_point(0x0A);
  1324. }
  1325. m_editor->insert_at_cursor_or_replace_selection(sb.to_deprecated_string());
  1326. m_editor->set_cursor({ m_editor->cursor().line(), m_editor->current_line().first_non_whitespace_column() });
  1327. } else {
  1328. StringBuilder sb = StringBuilder(m_yank_buffer.length() * amount);
  1329. for (auto i = 0; i < amount; i++) {
  1330. sb.append(m_yank_buffer);
  1331. }
  1332. m_editor->insert_at_cursor_or_replace_selection(sb.to_deprecated_string());
  1333. move_one_left();
  1334. }
  1335. }
  1336. void VimEditingEngine::put_after()
  1337. {
  1338. auto amount = m_motion.amount() ? m_motion.amount() : 1;
  1339. m_motion.reset();
  1340. if (m_yank_type == YankType::Line) {
  1341. move_to_logical_line_end();
  1342. StringBuilder sb = StringBuilder(m_yank_buffer.length() + 1);
  1343. for (auto i = 0; i < amount; i++) {
  1344. sb.append_code_point(0x0A);
  1345. sb.append(m_yank_buffer);
  1346. }
  1347. m_editor->insert_at_cursor_or_replace_selection(sb.to_deprecated_string());
  1348. m_editor->set_cursor({ m_editor->cursor().line(), m_editor->current_line().first_non_whitespace_column() });
  1349. } else {
  1350. // FIXME: If attempting to put on the last column a line,
  1351. // the buffer will bne placed on the next line due to the move_one_left/right behavior.
  1352. move_one_right();
  1353. StringBuilder sb = StringBuilder(m_yank_buffer.length() * amount);
  1354. for (auto i = 0; i < amount; i++) {
  1355. sb.append(m_yank_buffer);
  1356. }
  1357. m_editor->insert_at_cursor_or_replace_selection(sb.to_deprecated_string());
  1358. move_one_left();
  1359. }
  1360. }
  1361. void VimEditingEngine::move_to_previous_empty_lines_block()
  1362. {
  1363. VERIFY(!m_editor.is_null());
  1364. size_t line_idx = m_editor->cursor().line();
  1365. bool skipping_initial_empty_lines = true;
  1366. while (line_idx > 0) {
  1367. if (m_editor->document().line(line_idx).is_empty()) {
  1368. if (!skipping_initial_empty_lines)
  1369. break;
  1370. } else {
  1371. skipping_initial_empty_lines = false;
  1372. }
  1373. line_idx--;
  1374. }
  1375. TextPosition new_cursor = { line_idx, 0 };
  1376. m_editor->set_cursor(new_cursor);
  1377. };
  1378. void VimEditingEngine::move_to_next_empty_lines_block()
  1379. {
  1380. VERIFY(!m_editor.is_null());
  1381. size_t line_idx = m_editor->cursor().line();
  1382. bool skipping_initial_empty_lines = true;
  1383. while (line_idx < m_editor->line_count() - 1) {
  1384. if (m_editor->document().line(line_idx).is_empty()) {
  1385. if (!skipping_initial_empty_lines)
  1386. break;
  1387. } else {
  1388. skipping_initial_empty_lines = false;
  1389. }
  1390. line_idx++;
  1391. }
  1392. TextPosition new_cursor = { line_idx, 0 };
  1393. m_editor->set_cursor(new_cursor);
  1394. };
  1395. void VimEditingEngine::casefold_selection(Casing casing)
  1396. {
  1397. VERIFY(!m_editor.is_null());
  1398. VERIFY(m_editor->has_selection());
  1399. switch (casing) {
  1400. case Casing::Uppercase:
  1401. m_editor->insert_at_cursor_or_replace_selection(m_editor->selected_text().to_uppercase());
  1402. return;
  1403. case Casing::Lowercase:
  1404. m_editor->insert_at_cursor_or_replace_selection(m_editor->selected_text().to_lowercase());
  1405. return;
  1406. case Casing::Invertcase:
  1407. m_editor->insert_at_cursor_or_replace_selection(m_editor->selected_text().invert_case());
  1408. return;
  1409. }
  1410. }
  1411. }