vi_mode.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  1. /* vi_mode.c -- A vi emulation mode for Bash.
  2. Derived from code written by Jeff Sparkes (jsparkes@bnr.ca). */
  3. /* Copyright (C) 1987-2010 Free Software Foundation, Inc.
  4. This file is part of the GNU Readline Library (Readline), a library
  5. for reading lines of text with interactive input and history editing.
  6. Readline is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Readline is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Readline. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #define READLINE_LIBRARY
  18. /* **************************************************************** */
  19. /* */
  20. /* VI Emulation Mode */
  21. /* */
  22. /* **************************************************************** */
  23. #include "rlconf.h"
  24. #if defined (VI_MODE)
  25. #if defined (HAVE_CONFIG_H)
  26. # include <config.h>
  27. #endif
  28. #include <sys/types.h>
  29. #if defined (HAVE_STDLIB_H)
  30. # include <stdlib.h>
  31. #else
  32. # include "ansi_stdlib.h"
  33. #endif /* HAVE_STDLIB_H */
  34. #if defined (HAVE_UNISTD_H)
  35. # include <unistd.h>
  36. #endif
  37. #include <stdio.h>
  38. /* Some standard library routines. */
  39. #include "rldefs.h"
  40. #include "rlmbutil.h"
  41. #include "readline.h"
  42. #include "history.h"
  43. #include "rlprivate.h"
  44. #include "xmalloc.h"
  45. #ifndef member
  46. #define member(c, s) ((c) ? (char *)strchr ((s), (c)) != (char *)NULL : 0)
  47. #endif
  48. int _rl_vi_last_command = 'i'; /* default `.' puts you in insert mode */
  49. _rl_vimotion_cxt *_rl_vimvcxt = 0;
  50. /* Non-zero means enter insertion mode. */
  51. static int _rl_vi_doing_insert;
  52. /* Command keys which do movement for xxx_to commands. */
  53. static const char * const vi_motion = " hl^$0ftFT;,%wbeWBE|`";
  54. /* Keymap used for vi replace characters. Created dynamically since
  55. rarely used. */
  56. static Keymap vi_replace_map;
  57. /* The number of characters inserted in the last replace operation. */
  58. static int vi_replace_count;
  59. /* If non-zero, we have text inserted after a c[motion] command that put
  60. us implicitly into insert mode. Some people want this text to be
  61. attached to the command so that it is `redoable' with `.'. */
  62. static int vi_continued_command;
  63. static char *vi_insert_buffer;
  64. static int vi_insert_buffer_size;
  65. static int _rl_vi_last_repeat = 1;
  66. static int _rl_vi_last_arg_sign = 1;
  67. static int _rl_vi_last_motion;
  68. #if defined (HANDLE_MULTIBYTE)
  69. static char _rl_vi_last_search_mbchar[MB_LEN_MAX];
  70. static int _rl_vi_last_search_mblen;
  71. #else
  72. static int _rl_vi_last_search_char;
  73. #endif
  74. static int _rl_vi_last_replacement;
  75. static int _rl_vi_last_key_before_insert;
  76. static int vi_redoing;
  77. /* Text modification commands. These are the `redoable' commands. */
  78. static const char * const vi_textmod = "_*\\AaIiCcDdPpYyRrSsXx~";
  79. /* Arrays for the saved marks. */
  80. static int vi_mark_chars['z' - 'a' + 1];
  81. static void _rl_vi_stuff_insert PARAMS((int));
  82. static void _rl_vi_save_insert PARAMS((UNDO_LIST *));
  83. static void _rl_vi_backup PARAMS((void));
  84. static int _rl_vi_arg_dispatch PARAMS((int));
  85. static int rl_digit_loop1 PARAMS((void));
  86. static int _rl_vi_set_mark PARAMS((void));
  87. static int _rl_vi_goto_mark PARAMS((void));
  88. static void _rl_vi_append_forward PARAMS((int));
  89. static int _rl_vi_callback_getchar PARAMS((char *, int));
  90. #if defined (READLINE_CALLBACKS)
  91. static int _rl_vi_callback_set_mark PARAMS((_rl_callback_generic_arg *));
  92. static int _rl_vi_callback_goto_mark PARAMS((_rl_callback_generic_arg *));
  93. static int _rl_vi_callback_change_char PARAMS((_rl_callback_generic_arg *));
  94. static int _rl_vi_callback_char_search PARAMS((_rl_callback_generic_arg *));
  95. #endif
  96. static int rl_domove_read_callback PARAMS((_rl_vimotion_cxt *));
  97. static int rl_domove_motion_callback PARAMS((_rl_vimotion_cxt *));
  98. static int rl_vi_domove_getchar PARAMS((_rl_vimotion_cxt *));
  99. static int vi_change_dispatch PARAMS((_rl_vimotion_cxt *));
  100. static int vi_delete_dispatch PARAMS((_rl_vimotion_cxt *));
  101. static int vi_yank_dispatch PARAMS((_rl_vimotion_cxt *));
  102. static int vidomove_dispatch PARAMS((_rl_vimotion_cxt *));
  103. void
  104. _rl_vi_initialize_line ()
  105. {
  106. register int i, n;
  107. n = sizeof (vi_mark_chars) / sizeof (vi_mark_chars[0]);
  108. for (i = 0; i < n; i++)
  109. vi_mark_chars[i] = -1;
  110. RL_UNSETSTATE(RL_STATE_VICMDONCE);
  111. }
  112. void
  113. _rl_vi_reset_last ()
  114. {
  115. _rl_vi_last_command = 'i';
  116. _rl_vi_last_repeat = 1;
  117. _rl_vi_last_arg_sign = 1;
  118. _rl_vi_last_motion = 0;
  119. }
  120. void
  121. _rl_vi_set_last (key, repeat, sign)
  122. int key, repeat, sign;
  123. {
  124. _rl_vi_last_command = key;
  125. _rl_vi_last_repeat = repeat;
  126. _rl_vi_last_arg_sign = sign;
  127. }
  128. /* A convenience function that calls _rl_vi_set_last to save the last command
  129. information and enters insertion mode. */
  130. void
  131. rl_vi_start_inserting (key, repeat, sign)
  132. int key, repeat, sign;
  133. {
  134. _rl_vi_set_last (key, repeat, sign);
  135. rl_vi_insertion_mode (1, key);
  136. }
  137. /* Is the command C a VI mode text modification command? */
  138. int
  139. _rl_vi_textmod_command (c)
  140. int c;
  141. {
  142. return (member (c, vi_textmod));
  143. }
  144. static void
  145. _rl_vi_stuff_insert (count)
  146. int count;
  147. {
  148. rl_begin_undo_group ();
  149. while (count--)
  150. rl_insert_text (vi_insert_buffer);
  151. rl_end_undo_group ();
  152. }
  153. /* Bound to `.'. Called from command mode, so we know that we have to
  154. redo a text modification command. The default for _rl_vi_last_command
  155. puts you back into insert mode. */
  156. int
  157. rl_vi_redo (count, c)
  158. int count, c;
  159. {
  160. int r;
  161. if (!rl_explicit_arg)
  162. {
  163. rl_numeric_arg = _rl_vi_last_repeat;
  164. rl_arg_sign = _rl_vi_last_arg_sign;
  165. }
  166. r = 0;
  167. vi_redoing = 1;
  168. /* If we're redoing an insert with `i', stuff in the inserted text
  169. and do not go into insertion mode. */
  170. if (_rl_vi_last_command == 'i' && vi_insert_buffer && *vi_insert_buffer)
  171. {
  172. _rl_vi_stuff_insert (count);
  173. /* And back up point over the last character inserted. */
  174. if (rl_point > 0)
  175. _rl_vi_backup ();
  176. }
  177. /* Ditto for redoing an insert with `I', but move to the beginning of the
  178. line like the `I' command does. */
  179. else if (_rl_vi_last_command == 'I' && vi_insert_buffer && *vi_insert_buffer)
  180. {
  181. rl_beg_of_line (1, 'I');
  182. _rl_vi_stuff_insert (count);
  183. if (rl_point > 0)
  184. _rl_vi_backup ();
  185. }
  186. /* Ditto for redoing an insert with `a', but move forward a character first
  187. like the `a' command does. */
  188. else if (_rl_vi_last_command == 'a' && vi_insert_buffer && *vi_insert_buffer)
  189. {
  190. _rl_vi_append_forward ('a');
  191. _rl_vi_stuff_insert (count);
  192. if (rl_point > 0)
  193. _rl_vi_backup ();
  194. }
  195. /* Ditto for redoing an insert with `A', but move to the end of the line
  196. like the `A' command does. */
  197. else if (_rl_vi_last_command == 'A' && vi_insert_buffer && *vi_insert_buffer)
  198. {
  199. rl_end_of_line (1, 'A');
  200. _rl_vi_stuff_insert (count);
  201. if (rl_point > 0)
  202. _rl_vi_backup ();
  203. }
  204. else
  205. r = _rl_dispatch (_rl_vi_last_command, _rl_keymap);
  206. vi_redoing = 0;
  207. return (r);
  208. }
  209. /* A placeholder for further expansion. */
  210. int
  211. rl_vi_undo (count, key)
  212. int count, key;
  213. {
  214. return (rl_undo_command (count, key));
  215. }
  216. /* Yank the nth arg from the previous line into this line at point. */
  217. int
  218. rl_vi_yank_arg (count, key)
  219. int count, key;
  220. {
  221. /* Readline thinks that the first word on a line is the 0th, while vi
  222. thinks the first word on a line is the 1st. Compensate. */
  223. if (rl_explicit_arg)
  224. rl_yank_nth_arg (count - 1, 0);
  225. else
  226. rl_yank_nth_arg ('$', 0);
  227. return (0);
  228. }
  229. /* With an argument, move back that many history lines, else move to the
  230. beginning of history. */
  231. int
  232. rl_vi_fetch_history (count, c)
  233. int count, c;
  234. {
  235. int wanted;
  236. /* Giving an argument of n means we want the nth command in the history
  237. file. The command number is interpreted the same way that the bash
  238. `history' command does it -- that is, giving an argument count of 450
  239. to this command would get the command listed as number 450 in the
  240. output of `history'. */
  241. if (rl_explicit_arg)
  242. {
  243. wanted = history_base + where_history () - count;
  244. if (wanted <= 0)
  245. rl_beginning_of_history (0, 0);
  246. else
  247. rl_get_previous_history (wanted, c);
  248. }
  249. else
  250. rl_beginning_of_history (count, 0);
  251. return (0);
  252. }
  253. /* Search again for the last thing searched for. */
  254. int
  255. rl_vi_search_again (count, key)
  256. int count, key;
  257. {
  258. switch (key)
  259. {
  260. case 'n':
  261. rl_noninc_reverse_search_again (count, key);
  262. break;
  263. case 'N':
  264. rl_noninc_forward_search_again (count, key);
  265. break;
  266. }
  267. return (0);
  268. }
  269. /* Do a vi style search. */
  270. int
  271. rl_vi_search (count, key)
  272. int count, key;
  273. {
  274. switch (key)
  275. {
  276. case '?':
  277. _rl_free_saved_history_line ();
  278. rl_noninc_forward_search (count, key);
  279. break;
  280. case '/':
  281. _rl_free_saved_history_line ();
  282. rl_noninc_reverse_search (count, key);
  283. break;
  284. default:
  285. rl_ding ();
  286. break;
  287. }
  288. return (0);
  289. }
  290. /* Completion, from vi's point of view. */
  291. int
  292. rl_vi_complete (ignore, key)
  293. int ignore, key;
  294. {
  295. if ((rl_point < rl_end) && (!whitespace (rl_line_buffer[rl_point])))
  296. {
  297. if (!whitespace (rl_line_buffer[rl_point + 1]))
  298. rl_vi_end_word (1, 'E');
  299. rl_point++;
  300. }
  301. if (key == '*')
  302. rl_complete_internal ('*'); /* Expansion and replacement. */
  303. else if (key == '=')
  304. rl_complete_internal ('?'); /* List possible completions. */
  305. else if (key == '\\')
  306. rl_complete_internal (TAB); /* Standard Readline completion. */
  307. else
  308. rl_complete (0, key);
  309. if (key == '*' || key == '\\')
  310. rl_vi_start_inserting (key, 1, rl_arg_sign);
  311. return (0);
  312. }
  313. /* Tilde expansion for vi mode. */
  314. int
  315. rl_vi_tilde_expand (ignore, key)
  316. int ignore, key;
  317. {
  318. rl_tilde_expand (0, key);
  319. rl_vi_start_inserting (key, 1, rl_arg_sign);
  320. return (0);
  321. }
  322. /* Previous word in vi mode. */
  323. int
  324. rl_vi_prev_word (count, key)
  325. int count, key;
  326. {
  327. if (count < 0)
  328. return (rl_vi_next_word (-count, key));
  329. if (rl_point == 0)
  330. {
  331. rl_ding ();
  332. return (0);
  333. }
  334. if (_rl_uppercase_p (key))
  335. rl_vi_bWord (count, key);
  336. else
  337. rl_vi_bword (count, key);
  338. return (0);
  339. }
  340. /* Next word in vi mode. */
  341. int
  342. rl_vi_next_word (count, key)
  343. int count, key;
  344. {
  345. if (count < 0)
  346. return (rl_vi_prev_word (-count, key));
  347. if (rl_point >= (rl_end - 1))
  348. {
  349. rl_ding ();
  350. return (0);
  351. }
  352. if (_rl_uppercase_p (key))
  353. rl_vi_fWord (count, key);
  354. else
  355. rl_vi_fword (count, key);
  356. return (0);
  357. }
  358. /* Move to the end of the ?next? word. */
  359. int
  360. rl_vi_end_word (count, key)
  361. int count, key;
  362. {
  363. if (count < 0)
  364. {
  365. rl_ding ();
  366. return -1;
  367. }
  368. if (_rl_uppercase_p (key))
  369. rl_vi_eWord (count, key);
  370. else
  371. rl_vi_eword (count, key);
  372. return (0);
  373. }
  374. /* Move forward a word the way that 'W' does. */
  375. int
  376. rl_vi_fWord (count, ignore)
  377. int count, ignore;
  378. {
  379. while (count-- && rl_point < (rl_end - 1))
  380. {
  381. /* Skip until whitespace. */
  382. while (!whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
  383. rl_point++;
  384. /* Now skip whitespace. */
  385. while (whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
  386. rl_point++;
  387. }
  388. return (0);
  389. }
  390. int
  391. rl_vi_bWord (count, ignore)
  392. int count, ignore;
  393. {
  394. while (count-- && rl_point > 0)
  395. {
  396. /* If we are at the start of a word, move back to whitespace so
  397. we will go back to the start of the previous word. */
  398. if (!whitespace (rl_line_buffer[rl_point]) &&
  399. whitespace (rl_line_buffer[rl_point - 1]))
  400. rl_point--;
  401. while (rl_point > 0 && whitespace (rl_line_buffer[rl_point]))
  402. rl_point--;
  403. if (rl_point > 0)
  404. {
  405. while (--rl_point >= 0 && !whitespace (rl_line_buffer[rl_point]));
  406. rl_point++;
  407. }
  408. }
  409. return (0);
  410. }
  411. int
  412. rl_vi_eWord (count, ignore)
  413. int count, ignore;
  414. {
  415. while (count-- && rl_point < (rl_end - 1))
  416. {
  417. if (!whitespace (rl_line_buffer[rl_point]))
  418. rl_point++;
  419. /* Move to the next non-whitespace character (to the start of the
  420. next word). */
  421. while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
  422. rl_point++;
  423. if (rl_point && rl_point < rl_end)
  424. {
  425. /* Skip whitespace. */
  426. while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
  427. rl_point++;
  428. /* Skip until whitespace. */
  429. while (rl_point < rl_end && !whitespace (rl_line_buffer[rl_point]))
  430. rl_point++;
  431. /* Move back to the last character of the word. */
  432. rl_point--;
  433. }
  434. }
  435. return (0);
  436. }
  437. int
  438. rl_vi_fword (count, ignore)
  439. int count, ignore;
  440. {
  441. while (count-- && rl_point < (rl_end - 1))
  442. {
  443. /* Move to white space (really non-identifer). */
  444. if (_rl_isident (rl_line_buffer[rl_point]))
  445. {
  446. while (_rl_isident (rl_line_buffer[rl_point]) && rl_point < rl_end)
  447. rl_point++;
  448. }
  449. else /* if (!whitespace (rl_line_buffer[rl_point])) */
  450. {
  451. while (!_rl_isident (rl_line_buffer[rl_point]) &&
  452. !whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
  453. rl_point++;
  454. }
  455. /* Move past whitespace. */
  456. while (whitespace (rl_line_buffer[rl_point]) && rl_point < rl_end)
  457. rl_point++;
  458. }
  459. return (0);
  460. }
  461. int
  462. rl_vi_bword (count, ignore)
  463. int count, ignore;
  464. {
  465. while (count-- && rl_point > 0)
  466. {
  467. int last_is_ident;
  468. /* If we are at the start of a word, move back to whitespace
  469. so we will go back to the start of the previous word. */
  470. if (!whitespace (rl_line_buffer[rl_point]) &&
  471. whitespace (rl_line_buffer[rl_point - 1]))
  472. rl_point--;
  473. /* If this character and the previous character are `opposite', move
  474. back so we don't get messed up by the rl_point++ down there in
  475. the while loop. Without this code, words like `l;' screw up the
  476. function. */
  477. last_is_ident = _rl_isident (rl_line_buffer[rl_point - 1]);
  478. if ((_rl_isident (rl_line_buffer[rl_point]) && !last_is_ident) ||
  479. (!_rl_isident (rl_line_buffer[rl_point]) && last_is_ident))
  480. rl_point--;
  481. while (rl_point > 0 && whitespace (rl_line_buffer[rl_point]))
  482. rl_point--;
  483. if (rl_point > 0)
  484. {
  485. if (_rl_isident (rl_line_buffer[rl_point]))
  486. while (--rl_point >= 0 && _rl_isident (rl_line_buffer[rl_point]));
  487. else
  488. while (--rl_point >= 0 && !_rl_isident (rl_line_buffer[rl_point]) &&
  489. !whitespace (rl_line_buffer[rl_point]));
  490. rl_point++;
  491. }
  492. }
  493. return (0);
  494. }
  495. int
  496. rl_vi_eword (count, ignore)
  497. int count, ignore;
  498. {
  499. while (count-- && rl_point < rl_end - 1)
  500. {
  501. if (!whitespace (rl_line_buffer[rl_point]))
  502. rl_point++;
  503. while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
  504. rl_point++;
  505. if (rl_point < rl_end)
  506. {
  507. if (_rl_isident (rl_line_buffer[rl_point]))
  508. while (++rl_point < rl_end && _rl_isident (rl_line_buffer[rl_point]));
  509. else
  510. while (++rl_point < rl_end && !_rl_isident (rl_line_buffer[rl_point])
  511. && !whitespace (rl_line_buffer[rl_point]));
  512. }
  513. rl_point--;
  514. }
  515. return (0);
  516. }
  517. int
  518. rl_vi_insert_beg (count, key)
  519. int count, key;
  520. {
  521. rl_beg_of_line (1, key);
  522. rl_vi_insert_mode (1, key);
  523. return (0);
  524. }
  525. static void
  526. _rl_vi_append_forward (key)
  527. int key;
  528. {
  529. int point;
  530. if (rl_point < rl_end)
  531. {
  532. if (MB_CUR_MAX == 1 || rl_byte_oriented)
  533. rl_point++;
  534. else
  535. {
  536. point = rl_point;
  537. #if 0
  538. rl_forward_char (1, key);
  539. #else
  540. rl_point = _rl_forward_char_internal (1);
  541. #endif
  542. if (point == rl_point)
  543. rl_point = rl_end;
  544. }
  545. }
  546. }
  547. int
  548. rl_vi_append_mode (count, key)
  549. int count, key;
  550. {
  551. _rl_vi_append_forward (key);
  552. rl_vi_start_inserting (key, 1, rl_arg_sign);
  553. return (0);
  554. }
  555. int
  556. rl_vi_append_eol (count, key)
  557. int count, key;
  558. {
  559. rl_end_of_line (1, key);
  560. rl_vi_append_mode (1, key);
  561. return (0);
  562. }
  563. /* What to do in the case of C-d. */
  564. int
  565. rl_vi_eof_maybe (count, c)
  566. int count, c;
  567. {
  568. return (rl_newline (1, '\n'));
  569. }
  570. /* Insertion mode stuff. */
  571. /* Switching from one mode to the other really just involves
  572. switching keymaps. */
  573. int
  574. rl_vi_insertion_mode (count, key)
  575. int count, key;
  576. {
  577. _rl_keymap = vi_insertion_keymap;
  578. _rl_vi_last_key_before_insert = key;
  579. return (0);
  580. }
  581. int
  582. rl_vi_insert_mode (count, key)
  583. int count, key;
  584. {
  585. rl_vi_start_inserting (key, 1, rl_arg_sign);
  586. return (0);
  587. }
  588. static void
  589. _rl_vi_save_insert (up)
  590. UNDO_LIST *up;
  591. {
  592. int len, start, end;
  593. if (up == 0 || up->what != UNDO_INSERT)
  594. {
  595. if (vi_insert_buffer_size >= 1)
  596. vi_insert_buffer[0] = '\0';
  597. return;
  598. }
  599. start = up->start;
  600. end = up->end;
  601. len = end - start + 1;
  602. if (len >= vi_insert_buffer_size)
  603. {
  604. vi_insert_buffer_size += (len + 32) - (len % 32);
  605. vi_insert_buffer = (char *)xrealloc (vi_insert_buffer, vi_insert_buffer_size);
  606. }
  607. strncpy (vi_insert_buffer, rl_line_buffer + start, len - 1);
  608. vi_insert_buffer[len-1] = '\0';
  609. }
  610. void
  611. _rl_vi_done_inserting ()
  612. {
  613. if (_rl_vi_doing_insert)
  614. {
  615. /* The `C', `s', and `S' commands set this. */
  616. rl_end_undo_group ();
  617. /* Now, the text between rl_undo_list->next->start and
  618. rl_undo_list->next->end is what was inserted while in insert
  619. mode. It gets copied to VI_INSERT_BUFFER because it depends
  620. on absolute indices into the line which may change (though they
  621. probably will not). */
  622. _rl_vi_doing_insert = 0;
  623. _rl_vi_save_insert (rl_undo_list->next);
  624. vi_continued_command = 1;
  625. }
  626. else
  627. {
  628. if (rl_undo_list && (_rl_vi_last_key_before_insert == 'i' ||
  629. _rl_vi_last_key_before_insert == 'a' ||
  630. _rl_vi_last_key_before_insert == 'I' ||
  631. _rl_vi_last_key_before_insert == 'A'))
  632. _rl_vi_save_insert (rl_undo_list);
  633. /* XXX - Other keys probably need to be checked. */
  634. else if (_rl_vi_last_key_before_insert == 'C')
  635. rl_end_undo_group ();
  636. while (_rl_undo_group_level > 0)
  637. rl_end_undo_group ();
  638. vi_continued_command = 0;
  639. }
  640. }
  641. int
  642. rl_vi_movement_mode (count, key)
  643. int count, key;
  644. {
  645. if (rl_point > 0)
  646. rl_backward_char (1, key);
  647. _rl_keymap = vi_movement_keymap;
  648. _rl_vi_done_inserting ();
  649. /* This is how POSIX.2 says `U' should behave -- everything up until the
  650. first time you go into command mode should not be undone. */
  651. if (RL_ISSTATE (RL_STATE_VICMDONCE) == 0)
  652. rl_free_undo_list ();
  653. RL_SETSTATE (RL_STATE_VICMDONCE);
  654. return (0);
  655. }
  656. int
  657. rl_vi_arg_digit (count, c)
  658. int count, c;
  659. {
  660. if (c == '0' && rl_numeric_arg == 1 && !rl_explicit_arg)
  661. return (rl_beg_of_line (1, c));
  662. else
  663. return (rl_digit_argument (count, c));
  664. }
  665. /* Change the case of the next COUNT characters. */
  666. #if defined (HANDLE_MULTIBYTE)
  667. static int
  668. _rl_vi_change_mbchar_case (count)
  669. int count;
  670. {
  671. wchar_t wc;
  672. char mb[MB_LEN_MAX+1];
  673. int mlen, p;
  674. size_t m;
  675. mbstate_t ps;
  676. memset (&ps, 0, sizeof (mbstate_t));
  677. if (_rl_adjust_point (rl_line_buffer, rl_point, &ps) > 0)
  678. count--;
  679. while (count-- && rl_point < rl_end)
  680. {
  681. m = mbrtowc (&wc, rl_line_buffer + rl_point, rl_end - rl_point, &ps);
  682. if (MB_INVALIDCH (m))
  683. wc = (wchar_t)rl_line_buffer[rl_point];
  684. else if (MB_NULLWCH (m))
  685. wc = L'\0';
  686. if (iswupper (wc))
  687. wc = towlower (wc);
  688. else if (iswlower (wc))
  689. wc = towupper (wc);
  690. else
  691. {
  692. /* Just skip over chars neither upper nor lower case */
  693. rl_forward_char (1, 0);
  694. continue;
  695. }
  696. /* Vi is kind of strange here. */
  697. if (wc)
  698. {
  699. p = rl_point;
  700. mlen = wcrtomb (mb, wc, &ps);
  701. if (mlen >= 0)
  702. mb[mlen] = '\0';
  703. rl_begin_undo_group ();
  704. rl_vi_delete (1, 0);
  705. if (rl_point < p) /* Did we retreat at EOL? */
  706. rl_point++; /* XXX - should we advance more than 1 for mbchar? */
  707. rl_insert_text (mb);
  708. rl_end_undo_group ();
  709. rl_vi_check ();
  710. }
  711. else
  712. rl_forward_char (1, 0);
  713. }
  714. return 0;
  715. }
  716. #endif
  717. int
  718. rl_vi_change_case (count, ignore)
  719. int count, ignore;
  720. {
  721. int c, p;
  722. /* Don't try this on an empty line. */
  723. if (rl_point >= rl_end)
  724. return (0);
  725. c = 0;
  726. #if defined (HANDLE_MULTIBYTE)
  727. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  728. return (_rl_vi_change_mbchar_case (count));
  729. #endif
  730. while (count-- && rl_point < rl_end)
  731. {
  732. if (_rl_uppercase_p (rl_line_buffer[rl_point]))
  733. c = _rl_to_lower (rl_line_buffer[rl_point]);
  734. else if (_rl_lowercase_p (rl_line_buffer[rl_point]))
  735. c = _rl_to_upper (rl_line_buffer[rl_point]);
  736. else
  737. {
  738. /* Just skip over characters neither upper nor lower case. */
  739. rl_forward_char (1, c);
  740. continue;
  741. }
  742. /* Vi is kind of strange here. */
  743. if (c)
  744. {
  745. p = rl_point;
  746. rl_begin_undo_group ();
  747. rl_vi_delete (1, c);
  748. if (rl_point < p) /* Did we retreat at EOL? */
  749. rl_point++;
  750. _rl_insert_char (1, c);
  751. rl_end_undo_group ();
  752. rl_vi_check ();
  753. }
  754. else
  755. rl_forward_char (1, c);
  756. }
  757. return (0);
  758. }
  759. int
  760. rl_vi_put (count, key)
  761. int count, key;
  762. {
  763. if (!_rl_uppercase_p (key) && (rl_point + 1 <= rl_end))
  764. rl_point = _rl_find_next_mbchar (rl_line_buffer, rl_point, 1, MB_FIND_NONZERO);
  765. while (count--)
  766. rl_yank (1, key);
  767. rl_backward_char (1, key);
  768. return (0);
  769. }
  770. static void
  771. _rl_vi_backup ()
  772. {
  773. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  774. rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_NONZERO);
  775. else
  776. rl_point--;
  777. }
  778. int
  779. rl_vi_check ()
  780. {
  781. if (rl_point && rl_point == rl_end)
  782. {
  783. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  784. rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_NONZERO);
  785. else
  786. rl_point--;
  787. }
  788. return (0);
  789. }
  790. int
  791. rl_vi_column (count, key)
  792. int count, key;
  793. {
  794. if (count > rl_end)
  795. rl_end_of_line (1, key);
  796. else
  797. rl_point = count - 1;
  798. return (0);
  799. }
  800. /* Process C as part of the current numeric argument. Return -1 if the
  801. argument should be aborted, 0 if we should not read any more chars, and
  802. 1 if we should continue to read chars. */
  803. static int
  804. _rl_vi_arg_dispatch (c)
  805. int c;
  806. {
  807. int key;
  808. key = c;
  809. if (c >= 0 && _rl_keymap[c].type == ISFUNC && _rl_keymap[c].function == rl_universal_argument)
  810. {
  811. rl_numeric_arg *= 4;
  812. return 1;
  813. }
  814. c = UNMETA (c);
  815. if (_rl_digit_p (c))
  816. {
  817. if (rl_explicit_arg)
  818. rl_numeric_arg = (rl_numeric_arg * 10) + _rl_digit_value (c);
  819. else
  820. rl_numeric_arg = _rl_digit_value (c);
  821. rl_explicit_arg = 1;
  822. return 1; /* keep going */
  823. }
  824. else
  825. {
  826. rl_clear_message ();
  827. rl_stuff_char (key);
  828. return 0; /* done */
  829. }
  830. }
  831. /* A simplified loop for vi. Don't dispatch key at end.
  832. Don't recognize minus sign?
  833. Should this do rl_save_prompt/rl_restore_prompt? */
  834. static int
  835. rl_digit_loop1 ()
  836. {
  837. int c, r;
  838. while (1)
  839. {
  840. if (_rl_arg_overflow ())
  841. return 1;
  842. c = _rl_arg_getchar ();
  843. r = _rl_vi_arg_dispatch (c);
  844. if (r <= 0)
  845. break;
  846. }
  847. RL_UNSETSTATE(RL_STATE_NUMERICARG);
  848. return (0);
  849. }
  850. static void
  851. _rl_mvcxt_init (m, op, key)
  852. _rl_vimotion_cxt *m;
  853. int op, key;
  854. {
  855. m->op = op;
  856. m->state = m->flags = 0;
  857. m->ncxt = 0;
  858. m->numeric_arg = -1;
  859. m->start = rl_point;
  860. m->end = rl_end;
  861. m->key = key;
  862. m->motion = -1;
  863. }
  864. static _rl_vimotion_cxt *
  865. _rl_mvcxt_alloc (op, key)
  866. int op, key;
  867. {
  868. _rl_vimotion_cxt *m;
  869. m = xmalloc (sizeof (_rl_vimotion_cxt));
  870. _rl_mvcxt_init (m, op, key);
  871. return m;
  872. }
  873. static void
  874. _rl_mvcxt_dispose (m)
  875. _rl_vimotion_cxt *m;
  876. {
  877. xfree (m);
  878. }
  879. static int
  880. rl_domove_motion_callback (m)
  881. _rl_vimotion_cxt *m;
  882. {
  883. int c, save, r;
  884. int old_end;
  885. _rl_vi_last_motion = c = m->motion;
  886. /* Append a blank character temporarily so that the motion routines
  887. work right at the end of the line. */
  888. old_end = rl_end;
  889. rl_line_buffer[rl_end++] = ' ';
  890. rl_line_buffer[rl_end] = '\0';
  891. _rl_dispatch (c, _rl_keymap);
  892. /* Remove the blank that we added. */
  893. rl_end = old_end;
  894. rl_line_buffer[rl_end] = '\0';
  895. if (rl_point > rl_end)
  896. rl_point = rl_end;
  897. /* No change in position means the command failed. */
  898. if (rl_mark == rl_point)
  899. return (-1);
  900. /* rl_vi_f[wW]ord () leaves the cursor on the first character of the next
  901. word. If we are not at the end of the line, and we are on a
  902. non-whitespace character, move back one (presumably to whitespace). */
  903. if ((_rl_to_upper (c) == 'W') && rl_point < rl_end && rl_point > rl_mark &&
  904. !whitespace (rl_line_buffer[rl_point]))
  905. rl_point--;
  906. /* If cw or cW, back up to the end of a word, so the behaviour of ce
  907. or cE is the actual result. Brute-force, no subtlety. */
  908. if (m->key == 'c' && rl_point >= rl_mark && (_rl_to_upper (c) == 'W'))
  909. {
  910. /* Don't move farther back than where we started. */
  911. while (rl_point > rl_mark && whitespace (rl_line_buffer[rl_point]))
  912. rl_point--;
  913. /* Posix.2 says that if cw or cW moves the cursor towards the end of
  914. the line, the character under the cursor should be deleted. */
  915. if (rl_point == rl_mark)
  916. rl_point++;
  917. else
  918. {
  919. /* Move past the end of the word so that the kill doesn't
  920. remove the last letter of the previous word. Only do this
  921. if we are not at the end of the line. */
  922. if (rl_point >= 0 && rl_point < (rl_end - 1) && !whitespace (rl_line_buffer[rl_point]))
  923. rl_point++;
  924. }
  925. }
  926. if (rl_mark < rl_point)
  927. SWAP (rl_point, rl_mark);
  928. #if defined (READLINE_CALLBACKS)
  929. if (RL_ISSTATE (RL_STATE_CALLBACK))
  930. (*rl_redisplay_function)(); /* make sure motion is displayed */
  931. #endif
  932. r = vidomove_dispatch (m);
  933. return (r);
  934. }
  935. #define RL_VIMOVENUMARG() (RL_ISSTATE (RL_STATE_VIMOTION) && RL_ISSTATE (RL_STATE_NUMERICARG))
  936. static int
  937. rl_domove_read_callback (m)
  938. _rl_vimotion_cxt *m;
  939. {
  940. int c, save;
  941. c = m->motion;
  942. if (member (c, vi_motion))
  943. {
  944. #if defined (READLINE_CALLBACKS)
  945. /* If we just read a vi-mode motion command numeric argument, turn off
  946. the `reading numeric arg' state */
  947. if (RL_ISSTATE (RL_STATE_CALLBACK) && RL_VIMOVENUMARG())
  948. RL_UNSETSTATE (RL_STATE_NUMERICARG);
  949. #endif
  950. /* Should do everything, including turning off RL_STATE_VIMOTION */
  951. return (rl_domove_motion_callback (m));
  952. }
  953. else if (m->key == c && (m->key == 'd' || m->key == 'y' || m->key == 'c'))
  954. {
  955. rl_mark = rl_end;
  956. rl_beg_of_line (1, c);
  957. _rl_vi_last_motion = c;
  958. RL_UNSETSTATE (RL_STATE_VIMOTION);
  959. return (vidomove_dispatch (m));
  960. }
  961. #if defined (READLINE_CALLBACKS)
  962. /* XXX - these need to handle rl_universal_argument bindings */
  963. /* Reading vi motion char continuing numeric argument */
  964. else if (_rl_digit_p (c) && RL_ISSTATE (RL_STATE_CALLBACK) && RL_VIMOVENUMARG())
  965. {
  966. return (_rl_vi_arg_dispatch (c));
  967. }
  968. /* Readine vi motion char starting numeric argument */
  969. else if (_rl_digit_p (c) && RL_ISSTATE (RL_STATE_CALLBACK) && RL_ISSTATE (RL_STATE_VIMOTION) && (RL_ISSTATE (RL_STATE_NUMERICARG) == 0))
  970. {
  971. RL_SETSTATE (RL_STATE_NUMERICARG);
  972. return (_rl_vi_arg_dispatch (c));
  973. }
  974. #endif
  975. else if (_rl_digit_p (c))
  976. {
  977. /* This code path taken when not in callback mode */
  978. save = rl_numeric_arg;
  979. rl_numeric_arg = _rl_digit_value (c);
  980. rl_explicit_arg = 1;
  981. RL_SETSTATE (RL_STATE_NUMERICARG);
  982. rl_digit_loop1 ();
  983. rl_numeric_arg *= save;
  984. c = rl_vi_domove_getchar (m);
  985. if (c < 0)
  986. {
  987. m->motion = 0;
  988. return -1;
  989. }
  990. m->motion = c;
  991. return (rl_domove_motion_callback (m));
  992. }
  993. else
  994. {
  995. RL_UNSETSTATE (RL_STATE_VIMOTION);
  996. RL_UNSETSTATE (RL_STATE_NUMERICARG);
  997. return (1);
  998. }
  999. }
  1000. static int
  1001. rl_vi_domove_getchar (m)
  1002. _rl_vimotion_cxt *m;
  1003. {
  1004. int c;
  1005. RL_SETSTATE(RL_STATE_MOREINPUT);
  1006. c = rl_read_key ();
  1007. RL_UNSETSTATE(RL_STATE_MOREINPUT);
  1008. return c;
  1009. }
  1010. #if defined (READLINE_CALLBACKS)
  1011. int
  1012. _rl_vi_domove_callback (m)
  1013. _rl_vimotion_cxt *m;
  1014. {
  1015. int c, r;
  1016. m->motion = c = rl_vi_domove_getchar (m);
  1017. /* XXX - what to do if this returns -1? Should we return 1 for eof to
  1018. callback code? */
  1019. r = rl_domove_read_callback (m);
  1020. return ((r == 0) ? r : 1); /* normalize return values */
  1021. }
  1022. #endif
  1023. /* This code path taken when not in callback mode. */
  1024. int
  1025. rl_vi_domove (x, ignore)
  1026. int x, *ignore;
  1027. {
  1028. int r;
  1029. _rl_vimotion_cxt *m;
  1030. m = _rl_vimvcxt;
  1031. *ignore = m->motion = rl_vi_domove_getchar (m);
  1032. if (m->motion < 0)
  1033. {
  1034. m->motion = 0;
  1035. return -1;
  1036. }
  1037. return (rl_domove_read_callback (m));
  1038. }
  1039. static int
  1040. vi_delete_dispatch (m)
  1041. _rl_vimotion_cxt *m;
  1042. {
  1043. /* These are the motion commands that do not require adjusting the
  1044. mark. */
  1045. if (((strchr (" l|h^0bBFT`", m->motion) == 0) && (rl_point >= m->start)) &&
  1046. (rl_mark < rl_end))
  1047. rl_mark++;
  1048. rl_kill_text (rl_point, rl_mark);
  1049. return (0);
  1050. }
  1051. int
  1052. rl_vi_delete_to (count, key)
  1053. int count, key;
  1054. {
  1055. int c, r;
  1056. _rl_vimvcxt = _rl_mvcxt_alloc (VIM_DELETE, key);
  1057. _rl_vimvcxt->start = rl_point;
  1058. rl_mark = rl_point;
  1059. if (_rl_uppercase_p (key))
  1060. {
  1061. _rl_vimvcxt->motion = '$';
  1062. r = rl_domove_motion_callback (_rl_vimvcxt);
  1063. }
  1064. else if (vi_redoing)
  1065. {
  1066. _rl_vimvcxt->motion = _rl_vi_last_motion;
  1067. r = rl_domove_motion_callback (_rl_vimvcxt);
  1068. }
  1069. #if defined (READLINE_CALLBACKS)
  1070. else if (RL_ISSTATE (RL_STATE_CALLBACK))
  1071. {
  1072. RL_SETSTATE (RL_STATE_VIMOTION);
  1073. return (0);
  1074. }
  1075. #endif
  1076. else
  1077. r = rl_vi_domove (key, &c);
  1078. if (r < 0)
  1079. {
  1080. rl_ding ();
  1081. r = -1;
  1082. }
  1083. _rl_mvcxt_dispose (_rl_vimvcxt);
  1084. _rl_vimvcxt = 0;
  1085. return r;
  1086. }
  1087. static int
  1088. vi_change_dispatch (m)
  1089. _rl_vimotion_cxt *m;
  1090. {
  1091. /* These are the motion commands that do not require adjusting the
  1092. mark. c[wW] are handled by special-case code in rl_vi_domove(),
  1093. and already leave the mark at the correct location. */
  1094. if (((strchr (" l|hwW^0bBFT`", m->motion) == 0) && (rl_point >= m->start)) &&
  1095. (rl_mark < rl_end))
  1096. rl_mark++;
  1097. /* The cursor never moves with c[wW]. */
  1098. if ((_rl_to_upper (m->motion) == 'W') && rl_point < m->start)
  1099. rl_point = m->start;
  1100. if (vi_redoing)
  1101. {
  1102. if (vi_insert_buffer && *vi_insert_buffer)
  1103. rl_begin_undo_group ();
  1104. rl_delete_text (rl_point, rl_mark);
  1105. if (vi_insert_buffer && *vi_insert_buffer)
  1106. {
  1107. rl_insert_text (vi_insert_buffer);
  1108. rl_end_undo_group ();
  1109. }
  1110. }
  1111. else
  1112. {
  1113. rl_begin_undo_group (); /* to make the `u' command work */
  1114. rl_kill_text (rl_point, rl_mark);
  1115. /* `C' does not save the text inserted for undoing or redoing. */
  1116. if (_rl_uppercase_p (m->key) == 0)
  1117. _rl_vi_doing_insert = 1;
  1118. /* XXX -- TODO -- use m->numericarg? */
  1119. rl_vi_start_inserting (m->key, rl_numeric_arg, rl_arg_sign);
  1120. }
  1121. return (0);
  1122. }
  1123. int
  1124. rl_vi_change_to (count, key)
  1125. int count, key;
  1126. {
  1127. int c, r;
  1128. _rl_vimvcxt = _rl_mvcxt_alloc (VIM_CHANGE, key);
  1129. _rl_vimvcxt->start = rl_point;
  1130. rl_mark = rl_point;
  1131. if (_rl_uppercase_p (key))
  1132. {
  1133. _rl_vimvcxt->motion = '$';
  1134. r = rl_domove_motion_callback (_rl_vimvcxt);
  1135. }
  1136. else if (vi_redoing)
  1137. {
  1138. _rl_vimvcxt->motion = _rl_vi_last_motion;
  1139. r = rl_domove_motion_callback (_rl_vimvcxt);
  1140. }
  1141. #if defined (READLINE_CALLBACKS)
  1142. else if (RL_ISSTATE (RL_STATE_CALLBACK))
  1143. {
  1144. RL_SETSTATE (RL_STATE_VIMOTION);
  1145. return (0);
  1146. }
  1147. #endif
  1148. else
  1149. r = rl_vi_domove (key, &c);
  1150. if (r < 0)
  1151. {
  1152. rl_ding ();
  1153. r = -1; /* normalize return value */
  1154. }
  1155. _rl_mvcxt_dispose (_rl_vimvcxt);
  1156. _rl_vimvcxt = 0;
  1157. return r;
  1158. }
  1159. static int
  1160. vi_yank_dispatch (m)
  1161. _rl_vimotion_cxt *m;
  1162. {
  1163. /* These are the motion commands that do not require adjusting the
  1164. mark. */
  1165. if (((strchr (" l|h^0%bBFT`", m->motion) == 0) && (rl_point >= m->start)) &&
  1166. (rl_mark < rl_end))
  1167. rl_mark++;
  1168. rl_begin_undo_group ();
  1169. rl_kill_text (rl_point, rl_mark);
  1170. rl_end_undo_group ();
  1171. rl_do_undo ();
  1172. rl_point = m->start;
  1173. return (0);
  1174. }
  1175. int
  1176. rl_vi_yank_to (count, key)
  1177. int count, key;
  1178. {
  1179. int c, r;
  1180. _rl_vimvcxt = _rl_mvcxt_alloc (VIM_YANK, key);
  1181. _rl_vimvcxt->start = rl_point;
  1182. rl_mark = rl_point;
  1183. if (_rl_uppercase_p (key))
  1184. {
  1185. _rl_vimvcxt->motion = '$';
  1186. r = rl_domove_motion_callback (_rl_vimvcxt);
  1187. }
  1188. #if defined (READLINE_CALLBACKS)
  1189. else if (RL_ISSTATE (RL_STATE_CALLBACK))
  1190. {
  1191. RL_SETSTATE (RL_STATE_VIMOTION);
  1192. return (0);
  1193. }
  1194. #endif
  1195. else
  1196. r = rl_vi_domove (key, &c);
  1197. if (r < 0)
  1198. {
  1199. rl_ding ();
  1200. r = -1;
  1201. }
  1202. _rl_mvcxt_dispose (_rl_vimvcxt);
  1203. _rl_vimvcxt = 0;
  1204. return r;
  1205. }
  1206. static int
  1207. vidomove_dispatch (m)
  1208. _rl_vimotion_cxt *m;
  1209. {
  1210. int r;
  1211. switch (m->op)
  1212. {
  1213. case VIM_DELETE:
  1214. r = vi_delete_dispatch (m);
  1215. break;
  1216. case VIM_CHANGE:
  1217. r = vi_change_dispatch (m);
  1218. break;
  1219. case VIM_YANK:
  1220. r = vi_yank_dispatch (m);
  1221. break;
  1222. default:
  1223. _rl_errmsg ("vidomove_dispatch: unknown operator %d", m->op);
  1224. r = 1;
  1225. break;
  1226. }
  1227. RL_UNSETSTATE (RL_STATE_VIMOTION);
  1228. return r;
  1229. }
  1230. int
  1231. rl_vi_rubout (count, key)
  1232. int count, key;
  1233. {
  1234. int opoint;
  1235. if (count < 0)
  1236. return (rl_vi_delete (-count, key));
  1237. if (rl_point == 0)
  1238. {
  1239. rl_ding ();
  1240. return -1;
  1241. }
  1242. opoint = rl_point;
  1243. if (count > 1 && MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1244. rl_backward_char (count, key);
  1245. else if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1246. rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_NONZERO);
  1247. else
  1248. rl_point -= count;
  1249. if (rl_point < 0)
  1250. rl_point = 0;
  1251. rl_kill_text (rl_point, opoint);
  1252. return (0);
  1253. }
  1254. int
  1255. rl_vi_delete (count, key)
  1256. int count, key;
  1257. {
  1258. int end;
  1259. if (count < 0)
  1260. return (rl_vi_rubout (-count, key));
  1261. if (rl_end == 0)
  1262. {
  1263. rl_ding ();
  1264. return -1;
  1265. }
  1266. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1267. end = _rl_find_next_mbchar (rl_line_buffer, rl_point, count, MB_FIND_NONZERO);
  1268. else
  1269. end = rl_point + count;
  1270. if (end >= rl_end)
  1271. end = rl_end;
  1272. rl_kill_text (rl_point, end);
  1273. if (rl_point > 0 && rl_point == rl_end)
  1274. rl_backward_char (1, key);
  1275. return (0);
  1276. }
  1277. int
  1278. rl_vi_back_to_indent (count, key)
  1279. int count, key;
  1280. {
  1281. rl_beg_of_line (1, key);
  1282. while (rl_point < rl_end && whitespace (rl_line_buffer[rl_point]))
  1283. rl_point++;
  1284. return (0);
  1285. }
  1286. int
  1287. rl_vi_first_print (count, key)
  1288. int count, key;
  1289. {
  1290. return (rl_vi_back_to_indent (1, key));
  1291. }
  1292. static int _rl_cs_dir, _rl_cs_orig_dir;
  1293. #if defined (READLINE_CALLBACKS)
  1294. static int
  1295. _rl_vi_callback_char_search (data)
  1296. _rl_callback_generic_arg *data;
  1297. {
  1298. int c;
  1299. #if defined (HANDLE_MULTIBYTE)
  1300. c = _rl_vi_last_search_mblen = _rl_read_mbchar (_rl_vi_last_search_mbchar, MB_LEN_MAX);
  1301. #else
  1302. RL_SETSTATE(RL_STATE_MOREINPUT);
  1303. c = rl_read_key ();
  1304. RL_UNSETSTATE(RL_STATE_MOREINPUT);
  1305. #endif
  1306. if (c <= 0)
  1307. return -1;
  1308. #if !defined (HANDLE_MULTIBYTE)
  1309. _rl_vi_last_search_char = c;
  1310. #endif
  1311. _rl_callback_func = 0;
  1312. _rl_want_redisplay = 1;
  1313. #if defined (HANDLE_MULTIBYTE)
  1314. return (_rl_char_search_internal (data->count, _rl_cs_dir, _rl_vi_last_search_mbchar, _rl_vi_last_search_mblen));
  1315. #else
  1316. return (_rl_char_search_internal (data->count, _rl_cs_dir, _rl_vi_last_search_char));
  1317. #endif
  1318. }
  1319. #endif
  1320. int
  1321. rl_vi_char_search (count, key)
  1322. int count, key;
  1323. {
  1324. int c;
  1325. #if defined (HANDLE_MULTIBYTE)
  1326. static char *target;
  1327. static int tlen;
  1328. #else
  1329. static char target;
  1330. #endif
  1331. if (key == ';' || key == ',')
  1332. {
  1333. if (_rl_cs_orig_dir == 0)
  1334. return -1;
  1335. #if defined (HANDLE_MULTIBYTE)
  1336. if (_rl_vi_last_search_mblen == 0)
  1337. return -1;
  1338. #else
  1339. if (_rl_vi_last_search_char == 0)
  1340. return -1;
  1341. #endif
  1342. _rl_cs_dir = (key == ';') ? _rl_cs_orig_dir : -_rl_cs_orig_dir;
  1343. }
  1344. else
  1345. {
  1346. switch (key)
  1347. {
  1348. case 't':
  1349. _rl_cs_orig_dir = _rl_cs_dir = FTO;
  1350. break;
  1351. case 'T':
  1352. _rl_cs_orig_dir = _rl_cs_dir = BTO;
  1353. break;
  1354. case 'f':
  1355. _rl_cs_orig_dir = _rl_cs_dir = FFIND;
  1356. break;
  1357. case 'F':
  1358. _rl_cs_orig_dir = _rl_cs_dir = BFIND;
  1359. break;
  1360. }
  1361. if (vi_redoing)
  1362. {
  1363. /* set target and tlen below */
  1364. }
  1365. #if defined (READLINE_CALLBACKS)
  1366. else if (RL_ISSTATE (RL_STATE_CALLBACK))
  1367. {
  1368. _rl_callback_data = _rl_callback_data_alloc (count);
  1369. _rl_callback_data->i1 = _rl_cs_dir;
  1370. _rl_callback_func = _rl_vi_callback_char_search;
  1371. return (0);
  1372. }
  1373. #endif
  1374. else
  1375. {
  1376. #if defined (HANDLE_MULTIBYTE)
  1377. c = _rl_read_mbchar (_rl_vi_last_search_mbchar, MB_LEN_MAX);
  1378. if (c <= 0)
  1379. return -1;
  1380. _rl_vi_last_search_mblen = c;
  1381. #else
  1382. RL_SETSTATE(RL_STATE_MOREINPUT);
  1383. c = rl_read_key ();
  1384. RL_UNSETSTATE(RL_STATE_MOREINPUT);
  1385. if (c < 0)
  1386. return -1;
  1387. _rl_vi_last_search_char = c;
  1388. #endif
  1389. }
  1390. }
  1391. #if defined (HANDLE_MULTIBYTE)
  1392. target = _rl_vi_last_search_mbchar;
  1393. tlen = _rl_vi_last_search_mblen;
  1394. #else
  1395. target = _rl_vi_last_search_char;
  1396. #endif
  1397. #if defined (HANDLE_MULTIBYTE)
  1398. return (_rl_char_search_internal (count, _rl_cs_dir, target, tlen));
  1399. #else
  1400. return (_rl_char_search_internal (count, _rl_cs_dir, target));
  1401. #endif
  1402. }
  1403. /* Match brackets */
  1404. int
  1405. rl_vi_match (ignore, key)
  1406. int ignore, key;
  1407. {
  1408. int count = 1, brack, pos, tmp, pre;
  1409. pos = rl_point;
  1410. if ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0)
  1411. {
  1412. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1413. {
  1414. while ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0)
  1415. {
  1416. pre = rl_point;
  1417. rl_forward_char (1, key);
  1418. if (pre == rl_point)
  1419. break;
  1420. }
  1421. }
  1422. else
  1423. while ((brack = rl_vi_bracktype (rl_line_buffer[rl_point])) == 0 &&
  1424. rl_point < rl_end - 1)
  1425. rl_forward_char (1, key);
  1426. if (brack <= 0)
  1427. {
  1428. rl_point = pos;
  1429. rl_ding ();
  1430. return -1;
  1431. }
  1432. }
  1433. pos = rl_point;
  1434. if (brack < 0)
  1435. {
  1436. while (count)
  1437. {
  1438. tmp = pos;
  1439. if (MB_CUR_MAX == 1 || rl_byte_oriented)
  1440. pos--;
  1441. else
  1442. {
  1443. pos = _rl_find_prev_mbchar (rl_line_buffer, pos, MB_FIND_ANY);
  1444. if (tmp == pos)
  1445. pos--;
  1446. }
  1447. if (pos >= 0)
  1448. {
  1449. int b = rl_vi_bracktype (rl_line_buffer[pos]);
  1450. if (b == -brack)
  1451. count--;
  1452. else if (b == brack)
  1453. count++;
  1454. }
  1455. else
  1456. {
  1457. rl_ding ();
  1458. return -1;
  1459. }
  1460. }
  1461. }
  1462. else
  1463. { /* brack > 0 */
  1464. while (count)
  1465. {
  1466. if (MB_CUR_MAX == 1 || rl_byte_oriented)
  1467. pos++;
  1468. else
  1469. pos = _rl_find_next_mbchar (rl_line_buffer, pos, 1, MB_FIND_ANY);
  1470. if (pos < rl_end)
  1471. {
  1472. int b = rl_vi_bracktype (rl_line_buffer[pos]);
  1473. if (b == -brack)
  1474. count--;
  1475. else if (b == brack)
  1476. count++;
  1477. }
  1478. else
  1479. {
  1480. rl_ding ();
  1481. return -1;
  1482. }
  1483. }
  1484. }
  1485. rl_point = pos;
  1486. return (0);
  1487. }
  1488. int
  1489. rl_vi_bracktype (c)
  1490. int c;
  1491. {
  1492. switch (c)
  1493. {
  1494. case '(': return 1;
  1495. case ')': return -1;
  1496. case '[': return 2;
  1497. case ']': return -2;
  1498. case '{': return 3;
  1499. case '}': return -3;
  1500. default: return 0;
  1501. }
  1502. }
  1503. static int
  1504. _rl_vi_change_char (count, c, mb)
  1505. int count, c;
  1506. char *mb;
  1507. {
  1508. int p;
  1509. if (c == '\033' || c == CTRL ('C'))
  1510. return -1;
  1511. rl_begin_undo_group ();
  1512. while (count-- && rl_point < rl_end)
  1513. {
  1514. p = rl_point;
  1515. rl_vi_delete (1, c);
  1516. if (rl_point < p) /* Did we retreat at EOL? */
  1517. rl_point++;
  1518. #if defined (HANDLE_MULTIBYTE)
  1519. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1520. rl_insert_text (mb);
  1521. else
  1522. #endif
  1523. _rl_insert_char (1, c);
  1524. }
  1525. /* The cursor shall be left on the last character changed. */
  1526. rl_backward_char (1, c);
  1527. rl_end_undo_group ();
  1528. return (0);
  1529. }
  1530. static int
  1531. _rl_vi_callback_getchar (mb, mlen)
  1532. char *mb;
  1533. int mlen;
  1534. {
  1535. int c;
  1536. RL_SETSTATE(RL_STATE_MOREINPUT);
  1537. c = rl_read_key ();
  1538. RL_UNSETSTATE(RL_STATE_MOREINPUT);
  1539. if (c < 0)
  1540. return -1;
  1541. #if defined (HANDLE_MULTIBYTE)
  1542. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  1543. c = _rl_read_mbstring (c, mb, mlen);
  1544. #endif
  1545. return c;
  1546. }
  1547. #if defined (READLINE_CALLBACKS)
  1548. static int
  1549. _rl_vi_callback_change_char (data)
  1550. _rl_callback_generic_arg *data;
  1551. {
  1552. int c;
  1553. char mb[MB_LEN_MAX];
  1554. _rl_vi_last_replacement = c = _rl_vi_callback_getchar (mb, MB_LEN_MAX);
  1555. if (c < 0)
  1556. return -1;
  1557. _rl_callback_func = 0;
  1558. _rl_want_redisplay = 1;
  1559. return (_rl_vi_change_char (data->count, c, mb));
  1560. }
  1561. #endif
  1562. int
  1563. rl_vi_change_char (count, key)
  1564. int count, key;
  1565. {
  1566. int c;
  1567. char mb[MB_LEN_MAX];
  1568. if (vi_redoing)
  1569. {
  1570. c = _rl_vi_last_replacement;
  1571. mb[0] = c;
  1572. mb[1] = '\0';
  1573. }
  1574. #if defined (READLINE_CALLBACKS)
  1575. else if (RL_ISSTATE (RL_STATE_CALLBACK))
  1576. {
  1577. _rl_callback_data = _rl_callback_data_alloc (count);
  1578. _rl_callback_func = _rl_vi_callback_change_char;
  1579. return (0);
  1580. }
  1581. #endif
  1582. else
  1583. _rl_vi_last_replacement = c = _rl_vi_callback_getchar (mb, MB_LEN_MAX);
  1584. if (c < 0)
  1585. return -1;
  1586. return (_rl_vi_change_char (count, c, mb));
  1587. }
  1588. int
  1589. rl_vi_subst (count, key)
  1590. int count, key;
  1591. {
  1592. /* If we are redoing, rl_vi_change_to will stuff the last motion char */
  1593. if (vi_redoing == 0)
  1594. rl_stuff_char ((key == 'S') ? 'c' : 'l'); /* `S' == `cc', `s' == `cl' */
  1595. return (rl_vi_change_to (count, 'c'));
  1596. }
  1597. int
  1598. rl_vi_overstrike (count, key)
  1599. int count, key;
  1600. {
  1601. if (_rl_vi_doing_insert == 0)
  1602. {
  1603. _rl_vi_doing_insert = 1;
  1604. rl_begin_undo_group ();
  1605. }
  1606. if (count > 0)
  1607. {
  1608. _rl_overwrite_char (count, key);
  1609. vi_replace_count += count;
  1610. }
  1611. return (0);
  1612. }
  1613. int
  1614. rl_vi_overstrike_delete (count, key)
  1615. int count, key;
  1616. {
  1617. int i, s;
  1618. for (i = 0; i < count; i++)
  1619. {
  1620. if (vi_replace_count == 0)
  1621. {
  1622. rl_ding ();
  1623. break;
  1624. }
  1625. s = rl_point;
  1626. if (rl_do_undo ())
  1627. vi_replace_count--;
  1628. if (rl_point == s)
  1629. rl_backward_char (1, key);
  1630. }
  1631. if (vi_replace_count == 0 && _rl_vi_doing_insert)
  1632. {
  1633. rl_end_undo_group ();
  1634. rl_do_undo ();
  1635. _rl_vi_doing_insert = 0;
  1636. }
  1637. return (0);
  1638. }
  1639. int
  1640. rl_vi_replace (count, key)
  1641. int count, key;
  1642. {
  1643. int i;
  1644. vi_replace_count = 0;
  1645. if (!vi_replace_map)
  1646. {
  1647. vi_replace_map = rl_make_bare_keymap ();
  1648. for (i = ' '; i < KEYMAP_SIZE; i++)
  1649. vi_replace_map[i].function = rl_vi_overstrike;
  1650. vi_replace_map[RUBOUT].function = rl_vi_overstrike_delete;
  1651. vi_replace_map[ESC].function = rl_vi_movement_mode;
  1652. vi_replace_map[RETURN].function = rl_newline;
  1653. vi_replace_map[NEWLINE].function = rl_newline;
  1654. /* If the normal vi insertion keymap has ^H bound to erase, do the
  1655. same here. Probably should remove the assignment to RUBOUT up
  1656. there, but I don't think it will make a difference in real life. */
  1657. if (vi_insertion_keymap[CTRL ('H')].type == ISFUNC &&
  1658. vi_insertion_keymap[CTRL ('H')].function == rl_rubout)
  1659. vi_replace_map[CTRL ('H')].function = rl_vi_overstrike_delete;
  1660. }
  1661. _rl_keymap = vi_replace_map;
  1662. return (0);
  1663. }
  1664. #if 0
  1665. /* Try to complete the word we are standing on or the word that ends with
  1666. the previous character. A space matches everything. Word delimiters are
  1667. space and ;. */
  1668. int
  1669. rl_vi_possible_completions()
  1670. {
  1671. int save_pos = rl_point;
  1672. if (rl_line_buffer[rl_point] != ' ' && rl_line_buffer[rl_point] != ';')
  1673. {
  1674. while (rl_point < rl_end && rl_line_buffer[rl_point] != ' ' &&
  1675. rl_line_buffer[rl_point] != ';')
  1676. rl_point++;
  1677. }
  1678. else if (rl_line_buffer[rl_point - 1] == ';')
  1679. {
  1680. rl_ding ();
  1681. return (0);
  1682. }
  1683. rl_possible_completions ();
  1684. rl_point = save_pos;
  1685. return (0);
  1686. }
  1687. #endif
  1688. /* Functions to save and restore marks. */
  1689. static int
  1690. _rl_vi_set_mark ()
  1691. {
  1692. int ch;
  1693. RL_SETSTATE(RL_STATE_MOREINPUT);
  1694. ch = rl_read_key ();
  1695. RL_UNSETSTATE(RL_STATE_MOREINPUT);
  1696. if (ch < 0 || ch < 'a' || ch > 'z') /* make test against 0 explicit */
  1697. {
  1698. rl_ding ();
  1699. return -1;
  1700. }
  1701. ch -= 'a';
  1702. vi_mark_chars[ch] = rl_point;
  1703. return 0;
  1704. }
  1705. #if defined (READLINE_CALLBACKS)
  1706. static int
  1707. _rl_vi_callback_set_mark (data)
  1708. _rl_callback_generic_arg *data;
  1709. {
  1710. _rl_callback_func = 0;
  1711. _rl_want_redisplay = 1;
  1712. return (_rl_vi_set_mark ());
  1713. }
  1714. #endif
  1715. int
  1716. rl_vi_set_mark (count, key)
  1717. int count, key;
  1718. {
  1719. #if defined (READLINE_CALLBACKS)
  1720. if (RL_ISSTATE (RL_STATE_CALLBACK))
  1721. {
  1722. _rl_callback_data = 0;
  1723. _rl_callback_func = _rl_vi_callback_set_mark;
  1724. return (0);
  1725. }
  1726. #endif
  1727. return (_rl_vi_set_mark ());
  1728. }
  1729. static int
  1730. _rl_vi_goto_mark ()
  1731. {
  1732. int ch;
  1733. RL_SETSTATE(RL_STATE_MOREINPUT);
  1734. ch = rl_read_key ();
  1735. RL_UNSETSTATE(RL_STATE_MOREINPUT);
  1736. if (ch == '`')
  1737. {
  1738. rl_point = rl_mark;
  1739. return 0;
  1740. }
  1741. else if (ch < 0 || ch < 'a' || ch > 'z') /* make test against 0 explicit */
  1742. {
  1743. rl_ding ();
  1744. return -1;
  1745. }
  1746. ch -= 'a';
  1747. if (vi_mark_chars[ch] == -1)
  1748. {
  1749. rl_ding ();
  1750. return -1;
  1751. }
  1752. rl_point = vi_mark_chars[ch];
  1753. return 0;
  1754. }
  1755. #if defined (READLINE_CALLBACKS)
  1756. static int
  1757. _rl_vi_callback_goto_mark (data)
  1758. _rl_callback_generic_arg *data;
  1759. {
  1760. _rl_callback_func = 0;
  1761. _rl_want_redisplay = 1;
  1762. return (_rl_vi_goto_mark ());
  1763. }
  1764. #endif
  1765. int
  1766. rl_vi_goto_mark (count, key)
  1767. int count, key;
  1768. {
  1769. #if defined (READLINE_CALLBACKS)
  1770. if (RL_ISSTATE (RL_STATE_CALLBACK))
  1771. {
  1772. _rl_callback_data = 0;
  1773. _rl_callback_func = _rl_vi_callback_goto_mark;
  1774. return (0);
  1775. }
  1776. #endif
  1777. return (_rl_vi_goto_mark ());
  1778. }
  1779. #endif /* VI_MODE */