VimEditingEngine.cpp 50 KB

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