VimEditingEngine.cpp 49 KB

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