vis: use strncpy to copy into fixed sized buffer
[vis.git] / main.c
blobf63fbbf3b5bb08a8efff61cb86890fd3657680e6
1 #include <signal.h>
2 #include <limits.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <wchar.h>
6 #include <ctype.h>
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <inttypes.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
13 #include "ui-terminal.h"
14 #include "vis.h"
15 #include "vis-lua.h"
16 #include "text-util.h"
17 #include "text-motions.h"
18 #include "text-objects.h"
19 #include "util.h"
20 #include "libutf.h"
21 #include "array.h"
22 #include "buffer.h"
24 #define PAGE INT_MAX
25 #define PAGE_HALF (INT_MAX-1)
27 /** functions to be called from keybindings */
28 /* ignore key, do nothing */
29 static const char *nop(Vis*, const char *keys, const Arg *arg);
30 /* record/replay macro indicated by keys */
31 static const char *macro_record(Vis*, const char *keys, const Arg *arg);
32 static const char *macro_replay(Vis*, const char *keys, const Arg *arg);
33 /* temporarily suspend the editor and return to the shell, type 'fg' to get back */
34 static const char *suspend(Vis*, const char *keys, const Arg *arg);
35 /* switch to mode indicated by arg->i */
36 static const char *switchmode(Vis*, const char *keys, const Arg *arg);
37 /* switch to insert mode after performing movement indicated by arg->i */
38 static const char *insertmode(Vis*, const char *keys, const Arg *arg);
39 /* switch to replace mode after performing movement indicated by arg->i */
40 static const char *replacemode(Vis*, const char *keys, const Arg *arg);
41 /* add a new line either before or after the one where the cursor currently is */
42 static const char *openline(Vis*, const char *keys, const Arg *arg);
43 /* join lines from current cursor position to movement indicated by arg */
44 static const char *join(Vis*, const char *keys, const Arg *arg);
45 /* perform last action i.e. action_prev again */
46 static const char *repeat(Vis*, const char *keys, const Arg *arg);
47 /* replace character at cursor with one from keys */
48 static const char *replace(Vis*, const char *keys, const Arg *arg);
49 /* create a new cursor on the previous (arg->i < 0) or next (arg->i > 0) line */
50 static const char *selections_new(Vis*, const char *keys, const Arg *arg);
51 /* try to align all selections on the same column */
52 static const char *selections_align(Vis*, const char *keys, const Arg *arg);
53 /* try to align all selections by inserting the correct amount of white spaces */
54 static const char *selections_align_indent(Vis*, const char *keys, const Arg *arg);
55 /* remove all but the primary cursor and their selections */
56 static const char *selections_clear(Vis*, const char *keys, const Arg *arg);
57 /* remove the least recently added selection */
58 static const char *selections_remove(Vis*, const char *keys, const Arg *arg);
59 /* remove count (or arg->i)-th selection column */
60 static const char *selections_remove_column(Vis*, const char *keys, const Arg *arg);
61 /* remove all but the count (or arg->i)-th selection column */
62 static const char *selections_remove_column_except(Vis*, const char *keys, const Arg *arg);
63 /* move to the previous (arg->i < 0) or next (arg->i > 0) selection */
64 static const char *selections_navigate(Vis*, const char *keys, const Arg *arg);
65 /* select the word the selection is currently over */
66 static const char *selections_match_word(Vis*, const char *keys, const Arg *arg);
67 /* select the next region matching the current selection */
68 static const char *selections_match_next(Vis*, const char *keys, const Arg *arg);
69 /* clear current selection but select next match */
70 static const char *selections_match_skip(Vis*, const char *keys, const Arg *arg);
71 /* rotate selection content count times left (arg->i < 0) or right (arg->i > 0) */
72 static const char *selections_rotate(Vis*, const char *keys, const Arg *arg);
73 /* remove leading and trailing white spaces from selections */
74 static const char *selections_trim(Vis*, const char *keys, const Arg *arg);
75 /* save active selections to register */
76 static const char *selections_save(Vis*, const char *keys, const Arg *arg);
77 /* restore selections from register */
78 static const char *selections_restore(Vis*, const char *keys, const Arg *arg);
79 /* union selections */
80 static const char *selections_union(Vis*, const char *keys, const Arg *arg);
81 /* intersect selections */
82 static const char *selections_intersect(Vis*, const char *keys, const Arg *arg);
83 /* perform complement of current active selections */
84 static const char *selections_complement(Vis*, const char *keys, const Arg *arg);
85 /* subtract selections from register */
86 static const char *selections_minus(Vis*, const char *keys, const Arg *arg);
87 /* pariwise combine selections */
88 static const char *selections_combine(Vis*, const char *keys, const Arg *arg);
89 static Filerange combine_union(const Filerange*, const Filerange*);
90 static Filerange combine_intersect(const Filerange*, const Filerange*);
91 static Filerange combine_longer(const Filerange*, const Filerange*);
92 static Filerange combine_shorter(const Filerange*, const Filerange*);
93 static Filerange combine_leftmost(const Filerange*, const Filerange*);
94 static Filerange combine_rightmost(const Filerange*, const Filerange*);
95 /* adjust current used count according to keys */
96 static const char *count(Vis*, const char *keys, const Arg *arg);
97 /* move to the count-th line or if not given either to the first (arg->i < 0)
98 * or last (arg->i > 0) line of file */
99 static const char *gotoline(Vis*, const char *keys, const Arg *arg);
100 /* set motion type either LINEWISE or CHARWISE via arg->i */
101 static const char *motiontype(Vis*, const char *keys, const Arg *arg);
102 /* make the current action use the operator indicated by arg->i */
103 static const char *operator(Vis*, const char *keys, const Arg *arg);
104 /* use arg->s as command for the filter operator */
105 static const char *operator_filter(Vis*, const char *keys, const Arg *arg);
106 /* blocks to read a key and performs movement indicated by arg->i which
107 * should be one of VIS_MOVE_{RIGHT,LEFT}_{TO,TILL} */
108 static const char *movement_key(Vis*, const char *keys, const Arg *arg);
109 /* perform the movement as indicated by arg->i */
110 static const char *movement(Vis*, const char *keys, const Arg *arg);
111 /* let the current operator affect the range indicated by the text object arg->i */
112 static const char *textobj(Vis*, const char *keys, const Arg *arg);
113 /* move to the other end of selected text */
114 static const char *selection_end(Vis*, const char *keys, const Arg *arg);
115 /* use register indicated by keys for the current operator */
116 static const char *reg(Vis*, const char *keys, const Arg *arg);
117 /* use mark indicated by keys for the current action */
118 static const char *mark(Vis*, const char *keys, const Arg *arg);
119 /* {un,re}do last action, redraw window */
120 static const char *undo(Vis*, const char *keys, const Arg *arg);
121 static const char *redo(Vis*, const char *keys, const Arg *arg);
122 /* earlier, later action chronologically, redraw window */
123 static const char *earlier(Vis*, const char *keys, const Arg *arg);
124 static const char *later(Vis*, const char *keys, const Arg *arg);
125 /* delete from the current cursor position to the end of
126 * movement as indicated by arg->i */
127 static const char *delete(Vis*, const char *keys, const Arg *arg);
128 /* insert register content indicated by keys at current cursor position */
129 static const char *insert_register(Vis*, const char *keys, const Arg *arg);
130 /* show a user prompt to get input with title arg->s */
131 static const char *prompt_show(Vis*, const char *keys, const Arg *arg);
132 /* blocks to read 3 consecutive digits and inserts the corresponding byte value */
133 static const char *insert_verbatim(Vis*, const char *keys, const Arg *arg);
134 /* scroll window content according to arg->i which can be either PAGE, PAGE_HALF,
135 * or an arbitrary number of lines. a multiplier overrides what is given in arg->i.
136 * negative values scroll back, positive forward. */
137 static const char *wscroll(Vis*, const char *keys, const Arg *arg);
138 /* similar to scroll, but do only move window content not cursor position */
139 static const char *wslide(Vis*, const char *keys, const Arg *arg);
140 /* call editor function as indicated by arg->f */
141 static const char *call(Vis*, const char *keys, const Arg *arg);
142 /* call window function as indicated by arg->w */
143 static const char *window(Vis*, const char *keys, const Arg *arg);
144 /* show info about Unicode character at cursor position */
145 static const char *unicode_info(Vis*, const char *keys, const Arg *arg);
146 /* either go to count % of ile or to matching item */
147 static const char *percent(Vis*, const char *keys, const Arg *arg);
148 /* navigate jumplist next (arg->i > 0), prev (arg->i < 0), save (arg->i = 0) */
149 static const char *jumplist(Vis*, const char *keys, const Arg *arg);
151 enum {
152 VIS_ACTION_EDITOR_SUSPEND,
153 VIS_ACTION_CURSOR_CHAR_PREV,
154 VIS_ACTION_CURSOR_CHAR_NEXT,
155 VIS_ACTION_CURSOR_LINE_CHAR_PREV,
156 VIS_ACTION_CURSOR_LINE_CHAR_NEXT,
157 VIS_ACTION_CURSOR_CODEPOINT_PREV,
158 VIS_ACTION_CURSOR_CODEPOINT_NEXT,
159 VIS_ACTION_CURSOR_WORD_START_PREV,
160 VIS_ACTION_CURSOR_WORD_START_NEXT,
161 VIS_ACTION_CURSOR_WORD_END_PREV,
162 VIS_ACTION_CURSOR_WORD_END_NEXT,
163 VIS_ACTION_CURSOR_LONGWORD_START_PREV,
164 VIS_ACTION_CURSOR_LONGWORD_START_NEXT,
165 VIS_ACTION_CURSOR_LONGWORD_END_PREV,
166 VIS_ACTION_CURSOR_LONGWORD_END_NEXT,
167 VIS_ACTION_CURSOR_LINE_UP,
168 VIS_ACTION_CURSOR_LINE_DOWN,
169 VIS_ACTION_CURSOR_LINE_START,
170 VIS_ACTION_CURSOR_LINE_FINISH,
171 VIS_ACTION_CURSOR_LINE_BEGIN,
172 VIS_ACTION_CURSOR_LINE_END,
173 VIS_ACTION_CURSOR_SCREEN_LINE_UP,
174 VIS_ACTION_CURSOR_SCREEN_LINE_DOWN,
175 VIS_ACTION_CURSOR_SCREEN_LINE_BEGIN,
176 VIS_ACTION_CURSOR_SCREEN_LINE_MIDDLE,
177 VIS_ACTION_CURSOR_SCREEN_LINE_END,
178 VIS_ACTION_CURSOR_PERCENT,
179 VIS_ACTION_CURSOR_BYTE,
180 VIS_ACTION_CURSOR_BYTE_LEFT,
181 VIS_ACTION_CURSOR_BYTE_RIGHT,
182 VIS_ACTION_CURSOR_PARAGRAPH_PREV,
183 VIS_ACTION_CURSOR_PARAGRAPH_NEXT,
184 VIS_ACTION_CURSOR_SENTENCE_PREV,
185 VIS_ACTION_CURSOR_SENTENCE_NEXT,
186 VIS_ACTION_CURSOR_BLOCK_START,
187 VIS_ACTION_CURSOR_BLOCK_END,
188 VIS_ACTION_CURSOR_PARENTHESE_START,
189 VIS_ACTION_CURSOR_PARENTHESE_END,
190 VIS_ACTION_CURSOR_COLUMN,
191 VIS_ACTION_CURSOR_LINE_FIRST,
192 VIS_ACTION_CURSOR_LINE_LAST,
193 VIS_ACTION_CURSOR_WINDOW_LINE_TOP,
194 VIS_ACTION_CURSOR_WINDOW_LINE_MIDDLE,
195 VIS_ACTION_CURSOR_WINDOW_LINE_BOTTOM,
196 VIS_ACTION_CURSOR_SEARCH_REPEAT_FORWARD,
197 VIS_ACTION_CURSOR_SEARCH_REPEAT_BACKWARD,
198 VIS_ACTION_CURSOR_SEARCH_REPEAT,
199 VIS_ACTION_CURSOR_SEARCH_REPEAT_REVERSE,
200 VIS_ACTION_CURSOR_SEARCH_WORD_FORWARD,
201 VIS_ACTION_CURSOR_SEARCH_WORD_BACKWARD,
202 VIS_ACTION_WINDOW_PAGE_UP,
203 VIS_ACTION_WINDOW_PAGE_DOWN,
204 VIS_ACTION_WINDOW_HALFPAGE_UP,
205 VIS_ACTION_WINDOW_HALFPAGE_DOWN,
206 VIS_ACTION_MODE_NORMAL,
207 VIS_ACTION_MODE_VISUAL,
208 VIS_ACTION_MODE_VISUAL_LINE,
209 VIS_ACTION_MODE_INSERT,
210 VIS_ACTION_MODE_REPLACE,
211 VIS_ACTION_DELETE_CHAR_PREV,
212 VIS_ACTION_DELETE_CHAR_NEXT,
213 VIS_ACTION_DELETE_LINE_BEGIN,
214 VIS_ACTION_DELETE_WORD_PREV,
215 VIS_ACTION_JUMPLIST_PREV,
216 VIS_ACTION_JUMPLIST_NEXT,
217 VIS_ACTION_JUMPLIST_SAVE,
218 VIS_ACTION_UNDO,
219 VIS_ACTION_REDO,
220 VIS_ACTION_EARLIER,
221 VIS_ACTION_LATER,
222 VIS_ACTION_MACRO_RECORD,
223 VIS_ACTION_MACRO_REPLAY,
224 VIS_ACTION_MARK,
225 VIS_ACTION_REDRAW,
226 VIS_ACTION_REPLACE_CHAR,
227 VIS_ACTION_TOTILL_REPEAT,
228 VIS_ACTION_TOTILL_REVERSE,
229 VIS_ACTION_PROMPT_SEARCH_FORWARD,
230 VIS_ACTION_PROMPT_SEARCH_BACKWARD,
231 VIS_ACTION_TILL_LEFT,
232 VIS_ACTION_TILL_RIGHT,
233 VIS_ACTION_TO_LEFT,
234 VIS_ACTION_TO_RIGHT,
235 VIS_ACTION_REGISTER,
236 VIS_ACTION_OPERATOR_CHANGE,
237 VIS_ACTION_OPERATOR_DELETE,
238 VIS_ACTION_OPERATOR_YANK,
239 VIS_ACTION_OPERATOR_SHIFT_LEFT,
240 VIS_ACTION_OPERATOR_SHIFT_RIGHT,
241 VIS_ACTION_OPERATOR_CASE_LOWER,
242 VIS_ACTION_OPERATOR_CASE_UPPER,
243 VIS_ACTION_OPERATOR_CASE_SWAP,
244 VIS_ACTION_OPERATOR_FILTER,
245 VIS_ACTION_OPERATOR_FILTER_FMT,
246 VIS_ACTION_COUNT,
247 VIS_ACTION_INSERT_NEWLINE,
248 VIS_ACTION_INSERT_TAB,
249 VIS_ACTION_INSERT_VERBATIM,
250 VIS_ACTION_INSERT_REGISTER,
251 VIS_ACTION_WINDOW_NEXT,
252 VIS_ACTION_WINDOW_PREV,
253 VIS_ACTION_APPEND_CHAR_NEXT,
254 VIS_ACTION_APPEND_LINE_END,
255 VIS_ACTION_INSERT_LINE_START,
256 VIS_ACTION_OPEN_LINE_ABOVE,
257 VIS_ACTION_OPEN_LINE_BELOW,
258 VIS_ACTION_JOIN_LINES,
259 VIS_ACTION_JOIN_LINES_TRIM,
260 VIS_ACTION_PROMPT_SHOW,
261 VIS_ACTION_REPEAT,
262 VIS_ACTION_SELECTION_FLIP,
263 VIS_ACTION_WINDOW_REDRAW_TOP,
264 VIS_ACTION_WINDOW_REDRAW_CENTER,
265 VIS_ACTION_WINDOW_REDRAW_BOTTOM,
266 VIS_ACTION_WINDOW_SLIDE_UP,
267 VIS_ACTION_WINDOW_SLIDE_DOWN,
268 VIS_ACTION_PUT_AFTER,
269 VIS_ACTION_PUT_BEFORE,
270 VIS_ACTION_PUT_AFTER_END,
271 VIS_ACTION_PUT_BEFORE_END,
272 VIS_ACTION_SELECTIONS_MATCH_WORD,
273 VIS_ACTION_SELECTIONS_NEW_LINE_ABOVE,
274 VIS_ACTION_SELECTIONS_NEW_LINE_ABOVE_FIRST,
275 VIS_ACTION_SELECTIONS_NEW_LINE_BELOW,
276 VIS_ACTION_SELECTIONS_NEW_LINE_BELOW_LAST,
277 VIS_ACTION_SELECTIONS_NEW_LINES_BEGIN,
278 VIS_ACTION_SELECTIONS_NEW_LINES_END,
279 VIS_ACTION_SELECTIONS_NEW_MATCH_NEXT,
280 VIS_ACTION_SELECTIONS_NEW_MATCH_SKIP,
281 VIS_ACTION_SELECTIONS_ALIGN,
282 VIS_ACTION_SELECTIONS_ALIGN_INDENT_LEFT,
283 VIS_ACTION_SELECTIONS_ALIGN_INDENT_RIGHT,
284 VIS_ACTION_SELECTIONS_REMOVE_ALL,
285 VIS_ACTION_SELECTIONS_REMOVE_LAST,
286 VIS_ACTION_SELECTIONS_REMOVE_COLUMN,
287 VIS_ACTION_SELECTIONS_REMOVE_COLUMN_EXCEPT,
288 VIS_ACTION_SELECTIONS_PREV,
289 VIS_ACTION_SELECTIONS_NEXT,
290 VIS_ACTION_SELECTIONS_ROTATE_LEFT,
291 VIS_ACTION_SELECTIONS_ROTATE_RIGHT,
292 VIS_ACTION_SELECTIONS_TRIM,
293 VIS_ACTION_SELECTIONS_SAVE,
294 VIS_ACTION_SELECTIONS_RESTORE,
295 VIS_ACTION_SELECTIONS_UNION,
296 VIS_ACTION_SELECTIONS_INTERSECT,
297 VIS_ACTION_SELECTIONS_COMPLEMENT,
298 VIS_ACTION_SELECTIONS_MINUS,
299 VIS_ACTION_SELECTIONS_COMBINE_UNION,
300 VIS_ACTION_SELECTIONS_COMBINE_INTERSECT,
301 VIS_ACTION_SELECTIONS_COMBINE_LONGER,
302 VIS_ACTION_SELECTIONS_COMBINE_SHORTER,
303 VIS_ACTION_SELECTIONS_COMBINE_LEFTMOST,
304 VIS_ACTION_SELECTIONS_COMBINE_RIGHTMOST,
305 VIS_ACTION_TEXT_OBJECT_WORD_OUTER,
306 VIS_ACTION_TEXT_OBJECT_WORD_INNER,
307 VIS_ACTION_TEXT_OBJECT_LONGWORD_OUTER,
308 VIS_ACTION_TEXT_OBJECT_LONGWORD_INNER,
309 VIS_ACTION_TEXT_OBJECT_SENTENCE,
310 VIS_ACTION_TEXT_OBJECT_PARAGRAPH,
311 VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_OUTER,
312 VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_INNER,
313 VIS_ACTION_TEXT_OBJECT_PARANTHESE_OUTER,
314 VIS_ACTION_TEXT_OBJECT_PARANTHESE_INNER,
315 VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_OUTER,
316 VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_INNER,
317 VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_OUTER,
318 VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_INNER,
319 VIS_ACTION_TEXT_OBJECT_QUOTE_OUTER,
320 VIS_ACTION_TEXT_OBJECT_QUOTE_INNER,
321 VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_OUTER,
322 VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_INNER,
323 VIS_ACTION_TEXT_OBJECT_BACKTICK_OUTER,
324 VIS_ACTION_TEXT_OBJECT_BACKTICK_INNER,
325 VIS_ACTION_TEXT_OBJECT_ENTIRE_OUTER,
326 VIS_ACTION_TEXT_OBJECT_ENTIRE_INNER,
327 VIS_ACTION_TEXT_OBJECT_LINE_OUTER,
328 VIS_ACTION_TEXT_OBJECT_LINE_INNER,
329 VIS_ACTION_TEXT_OBJECT_INDENTATION,
330 VIS_ACTION_TEXT_OBJECT_SEARCH_FORWARD,
331 VIS_ACTION_TEXT_OBJECT_SEARCH_BACKWARD,
332 VIS_ACTION_MOTION_CHARWISE,
333 VIS_ACTION_MOTION_LINEWISE,
334 VIS_ACTION_UNICODE_INFO,
335 VIS_ACTION_UTF8_INFO,
336 VIS_ACTION_NOP,
339 static const KeyAction vis_action[] = {
340 [VIS_ACTION_EDITOR_SUSPEND] = {
341 "vis-suspend",
342 VIS_HELP("Suspend the editor")
343 suspend,
345 [VIS_ACTION_CURSOR_CHAR_PREV] = {
346 "vis-motion-char-prev",
347 VIS_HELP("Move cursor left, to the previous character")
348 movement, { .i = VIS_MOVE_CHAR_PREV }
350 [VIS_ACTION_CURSOR_CHAR_NEXT] = {
351 "vis-motion-char-next",
352 VIS_HELP("Move cursor right, to the next character")
353 movement, { .i = VIS_MOVE_CHAR_NEXT }
355 [VIS_ACTION_CURSOR_LINE_CHAR_PREV] = {
356 "vis-motion-line-char-prev",
357 VIS_HELP("Move cursor left, to the previous character on the same line")
358 movement, { .i = VIS_MOVE_LINE_CHAR_PREV }
360 [VIS_ACTION_CURSOR_LINE_CHAR_NEXT] = {
361 "vis-motion-line-char-next",
362 VIS_HELP("Move cursor right, to the next character on the same line")
363 movement, { .i = VIS_MOVE_LINE_CHAR_NEXT }
365 [VIS_ACTION_CURSOR_CODEPOINT_PREV] = {
366 "vis-motion-codepoint-prev",
367 VIS_HELP("Move to the previous Unicode codepoint")
368 movement, { .i = VIS_MOVE_CODEPOINT_PREV }
370 [VIS_ACTION_CURSOR_CODEPOINT_NEXT] = {
371 "vis-motion-codepoint-next",
372 VIS_HELP("Move to the next Unicode codepoint")
373 movement, { .i = VIS_MOVE_CODEPOINT_NEXT }
375 [VIS_ACTION_CURSOR_WORD_START_PREV] = {
376 "vis-motion-word-start-prev",
377 VIS_HELP("Move cursor words backwards")
378 movement, { .i = VIS_MOVE_WORD_START_PREV }
380 [VIS_ACTION_CURSOR_WORD_START_NEXT] = {
381 "vis-motion-word-start-next",
382 VIS_HELP("Move cursor words forwards")
383 movement, { .i = VIS_MOVE_WORD_START_NEXT }
385 [VIS_ACTION_CURSOR_WORD_END_PREV] = {
386 "vis-motion-word-end-prev",
387 VIS_HELP("Move cursor backwards to the end of word")
388 movement, { .i = VIS_MOVE_WORD_END_PREV }
390 [VIS_ACTION_CURSOR_WORD_END_NEXT] = {
391 "vis-motion-word-end-next",
392 VIS_HELP("Move cursor forward to the end of word")
393 movement, { .i = VIS_MOVE_WORD_END_NEXT }
395 [VIS_ACTION_CURSOR_LONGWORD_START_PREV] = {
396 "vis-motion-bigword-start-prev",
397 VIS_HELP("Move cursor WORDS backwards")
398 movement, { .i = VIS_MOVE_LONGWORD_START_PREV }
400 [VIS_ACTION_CURSOR_LONGWORD_START_NEXT] = {
401 "vis-motion-bigword-start-next",
402 VIS_HELP("Move cursor WORDS forwards")
403 movement, { .i = VIS_MOVE_LONGWORD_START_NEXT }
405 [VIS_ACTION_CURSOR_LONGWORD_END_PREV] = {
406 "vis-motion-bigword-end-prev",
407 VIS_HELP("Move cursor backwards to the end of WORD")
408 movement, { .i = VIS_MOVE_LONGWORD_END_PREV }
410 [VIS_ACTION_CURSOR_LONGWORD_END_NEXT] = {
411 "vis-motion-bigword-end-next",
412 VIS_HELP("Move cursor forward to the end of WORD")
413 movement, { .i = VIS_MOVE_LONGWORD_END_NEXT }
415 [VIS_ACTION_CURSOR_LINE_UP] = {
416 "vis-motion-line-up",
417 VIS_HELP("Move cursor line upwards")
418 movement, { .i = VIS_MOVE_LINE_UP }
420 [VIS_ACTION_CURSOR_LINE_DOWN] = {
421 "vis-motion-line-down",
422 VIS_HELP("Move cursor line downwards")
423 movement, { .i = VIS_MOVE_LINE_DOWN }
425 [VIS_ACTION_CURSOR_LINE_START] = {
426 "vis-motion-line-start",
427 VIS_HELP("Move cursor to first non-blank character of the line")
428 movement, { .i = VIS_MOVE_LINE_START }
430 [VIS_ACTION_CURSOR_LINE_FINISH] = {
431 "vis-motion-line-finish",
432 VIS_HELP("Move cursor to last non-blank character of the line")
433 movement, { .i = VIS_MOVE_LINE_FINISH }
435 [VIS_ACTION_CURSOR_LINE_BEGIN] = {
436 "vis-motion-line-begin",
437 VIS_HELP("Move cursor to first character of the line")
438 movement, { .i = VIS_MOVE_LINE_BEGIN }
440 [VIS_ACTION_CURSOR_LINE_END] = {
441 "vis-motion-line-end",
442 VIS_HELP("Move cursor to end of the line")
443 movement, { .i = VIS_MOVE_LINE_END }
445 [VIS_ACTION_CURSOR_SCREEN_LINE_UP] = {
446 "vis-motion-screenline-up",
447 VIS_HELP("Move cursor screen/display line upwards")
448 movement, { .i = VIS_MOVE_SCREEN_LINE_UP }
450 [VIS_ACTION_CURSOR_SCREEN_LINE_DOWN] = {
451 "vis-motion-screenline-down",
452 VIS_HELP("Move cursor screen/display line downwards")
453 movement, { .i = VIS_MOVE_SCREEN_LINE_DOWN }
455 [VIS_ACTION_CURSOR_SCREEN_LINE_BEGIN] = {
456 "vis-motion-screenline-begin",
457 VIS_HELP("Move cursor to beginning of screen/display line")
458 movement, { .i = VIS_MOVE_SCREEN_LINE_BEGIN }
460 [VIS_ACTION_CURSOR_SCREEN_LINE_MIDDLE] = {
461 "vis-motion-screenline-middle",
462 VIS_HELP("Move cursor to middle of screen/display line")
463 movement, { .i = VIS_MOVE_SCREEN_LINE_MIDDLE }
465 [VIS_ACTION_CURSOR_SCREEN_LINE_END] = {
466 "vis-motion-screenline-end",
467 VIS_HELP("Move cursor to end of screen/display line")
468 movement, { .i = VIS_MOVE_SCREEN_LINE_END }
470 [VIS_ACTION_CURSOR_PERCENT] = {
471 "vis-motion-percent",
472 VIS_HELP("Move to count % of file or matching item")
473 percent
475 [VIS_ACTION_CURSOR_BYTE] = {
476 "vis-motion-byte",
477 VIS_HELP("Move to absolute byte position")
478 movement, { .i = VIS_MOVE_BYTE }
480 [VIS_ACTION_CURSOR_BYTE_LEFT] = {
481 "vis-motion-byte-left",
482 VIS_HELP("Move count bytes to the left")
483 movement, { .i = VIS_MOVE_BYTE_LEFT }
485 [VIS_ACTION_CURSOR_BYTE_RIGHT] = {
486 "vis-motion-byte-right",
487 VIS_HELP("Move count bytes to the right")
488 movement, { .i = VIS_MOVE_BYTE_RIGHT }
490 [VIS_ACTION_CURSOR_PARAGRAPH_PREV] = {
491 "vis-motion-paragraph-prev",
492 VIS_HELP("Move cursor paragraph backward")
493 movement, { .i = VIS_MOVE_PARAGRAPH_PREV }
495 [VIS_ACTION_CURSOR_PARAGRAPH_NEXT] = {
496 "vis-motion-paragraph-next",
497 VIS_HELP("Move cursor paragraph forward")
498 movement, { .i = VIS_MOVE_PARAGRAPH_NEXT }
500 [VIS_ACTION_CURSOR_SENTENCE_PREV] = {
501 "vis-motion-sentence-prev",
502 VIS_HELP("Move cursor sentence backward")
503 movement, { .i = VIS_MOVE_SENTENCE_PREV }
505 [VIS_ACTION_CURSOR_SENTENCE_NEXT] = {
506 "vis-motion-sentence-next",
507 VIS_HELP("Move cursor sentence forward")
508 movement, { .i = VIS_MOVE_SENTENCE_NEXT }
510 [VIS_ACTION_CURSOR_BLOCK_START] = {
511 "vis-motion-block-start",
512 VIS_HELP("Move cursor to the opening curly brace in a block")
513 movement, { .i = VIS_MOVE_BLOCK_START }
515 [VIS_ACTION_CURSOR_BLOCK_END] = {
516 "vis-motion-block-end",
517 VIS_HELP("Move cursor to the closing curly brace in a block")
518 movement, { .i = VIS_MOVE_BLOCK_END }
520 [VIS_ACTION_CURSOR_PARENTHESE_START] = {
521 "vis-motion-parenthese-start",
522 VIS_HELP("Move cursor to the opening parenthese inside a pair of parentheses")
523 movement, { .i = VIS_MOVE_PARENTHESE_START }
525 [VIS_ACTION_CURSOR_PARENTHESE_END] = {
526 "vis-motion-parenthese-end",
527 VIS_HELP("Move cursor to the closing parenthese inside a pair of parentheses")
528 movement, { .i = VIS_MOVE_PARENTHESE_END }
530 [VIS_ACTION_CURSOR_COLUMN] = {
531 "vis-motion-column",
532 VIS_HELP("Move cursor to given column of current line")
533 movement, { .i = VIS_MOVE_COLUMN }
535 [VIS_ACTION_CURSOR_LINE_FIRST] = {
536 "vis-motion-line-first",
537 VIS_HELP("Move cursor to given line (defaults to first)")
538 gotoline, { .i = -1 }
540 [VIS_ACTION_CURSOR_LINE_LAST] = {
541 "vis-motion-line-last",
542 VIS_HELP("Move cursor to given line (defaults to last)")
543 gotoline, { .i = +1 }
545 [VIS_ACTION_CURSOR_WINDOW_LINE_TOP] = {
546 "vis-motion-window-line-top",
547 VIS_HELP("Move cursor to top line of the window")
548 movement, { .i = VIS_MOVE_WINDOW_LINE_TOP }
550 [VIS_ACTION_CURSOR_WINDOW_LINE_MIDDLE] = {
551 "vis-motion-window-line-middle",
552 VIS_HELP("Move cursor to middle line of the window")
553 movement, { .i = VIS_MOVE_WINDOW_LINE_MIDDLE }
555 [VIS_ACTION_CURSOR_WINDOW_LINE_BOTTOM] = {
556 "vis-motion-window-line-bottom",
557 VIS_HELP("Move cursor to bottom line of the window")
558 movement, { .i = VIS_MOVE_WINDOW_LINE_BOTTOM }
560 [VIS_ACTION_CURSOR_SEARCH_REPEAT_FORWARD] = {
561 "vis-motion-search-repeat-forward",
562 VIS_HELP("Move cursor to next match in forward direction")
563 movement, { .i = VIS_MOVE_SEARCH_REPEAT_FORWARD }
565 [VIS_ACTION_CURSOR_SEARCH_REPEAT_BACKWARD] = {
566 "vis-motion-search-repeat-backward",
567 VIS_HELP("Move cursor to previous match in backward direction")
568 movement, { .i = VIS_MOVE_SEARCH_REPEAT_BACKWARD }
570 [VIS_ACTION_CURSOR_SEARCH_REPEAT] = {
571 "vis-motion-search-repeat",
572 VIS_HELP("Move cursor to next match")
573 movement, { .i = VIS_MOVE_SEARCH_REPEAT }
575 [VIS_ACTION_CURSOR_SEARCH_REPEAT_REVERSE] = {
576 "vis-motion-search-repeat-reverse",
577 VIS_HELP("Move cursor to next match in opposite direction")
578 movement, { .i = VIS_MOVE_SEARCH_REPEAT_REVERSE }
580 [VIS_ACTION_CURSOR_SEARCH_WORD_FORWARD] = {
581 "vis-motion-search-word-forward",
582 VIS_HELP("Move cursor to next occurrence of the word under cursor")
583 movement, { .i = VIS_MOVE_SEARCH_WORD_FORWARD }
585 [VIS_ACTION_CURSOR_SEARCH_WORD_BACKWARD] = {
586 "vis-motion-search-word-backward",
587 VIS_HELP("Move cursor to previous occurrence of the word under cursor")
588 movement, { .i = VIS_MOVE_SEARCH_WORD_BACKWARD }
590 [VIS_ACTION_WINDOW_PAGE_UP] = {
591 "vis-window-page-up",
592 VIS_HELP("Scroll window pages backwards (upwards)")
593 wscroll, { .i = -PAGE }
595 [VIS_ACTION_WINDOW_HALFPAGE_UP] = {
596 "vis-window-halfpage-up",
597 VIS_HELP("Scroll window half pages backwards (upwards)")
598 wscroll, { .i = -PAGE_HALF }
600 [VIS_ACTION_WINDOW_PAGE_DOWN] = {
601 "vis-window-page-down",
602 VIS_HELP("Scroll window pages forwards (downwards)")
603 wscroll, { .i = +PAGE }
605 [VIS_ACTION_WINDOW_HALFPAGE_DOWN] = {
606 "vis-window-halfpage-down",
607 VIS_HELP("Scroll window half pages forwards (downwards)")
608 wscroll, { .i = +PAGE_HALF }
610 [VIS_ACTION_MODE_NORMAL] = {
611 "vis-mode-normal",
612 VIS_HELP("Enter normal mode")
613 switchmode, { .i = VIS_MODE_NORMAL }
615 [VIS_ACTION_MODE_VISUAL] = {
616 "vis-mode-visual-charwise",
617 VIS_HELP("Enter characterwise visual mode")
618 switchmode, { .i = VIS_MODE_VISUAL }
620 [VIS_ACTION_MODE_VISUAL_LINE] = {
621 "vis-mode-visual-linewise",
622 VIS_HELP("Enter linewise visual mode")
623 switchmode, { .i = VIS_MODE_VISUAL_LINE }
625 [VIS_ACTION_MODE_INSERT] = {
626 "vis-mode-insert",
627 VIS_HELP("Enter insert mode")
628 insertmode, { .i = VIS_MOVE_NOP }
630 [VIS_ACTION_MODE_REPLACE] = {
631 "vis-mode-replace",
632 VIS_HELP("Enter replace mode")
633 replacemode, { .i = VIS_MOVE_NOP }
635 [VIS_ACTION_DELETE_CHAR_PREV] = {
636 "vis-delete-char-prev",
637 VIS_HELP("Delete the previous character")
638 delete, { .i = VIS_MOVE_CHAR_PREV }
640 [VIS_ACTION_DELETE_CHAR_NEXT] = {
641 "vis-delete-char-next",
642 VIS_HELP("Delete the next character")
643 delete, { .i = VIS_MOVE_CHAR_NEXT }
645 [VIS_ACTION_DELETE_LINE_BEGIN] = {
646 "vis-delete-line-begin",
647 VIS_HELP("Delete until the start of the current line")
648 delete, { .i = VIS_MOVE_LINE_BEGIN }
650 [VIS_ACTION_DELETE_WORD_PREV] = {
651 "vis-delete-word-prev",
652 VIS_HELP("Delete the previous WORD")
653 delete, { .i = VIS_MOVE_WORD_START_PREV }
655 [VIS_ACTION_JUMPLIST_PREV] = {
656 "vis-jumplist-prev",
657 VIS_HELP("Go to older cursor position in jump list")
658 jumplist, { .i = -1 }
660 [VIS_ACTION_JUMPLIST_NEXT] = {
661 "vis-jumplist-next",
662 VIS_HELP("Go to newer cursor position in jump list")
663 jumplist, { .i = +1 }
665 [VIS_ACTION_JUMPLIST_SAVE] = {
666 "vis-jumplist-save",
667 VIS_HELP("Save current selections in jump list")
668 jumplist, { .i = 0 }
670 [VIS_ACTION_UNDO] = {
671 "vis-undo",
672 VIS_HELP("Undo last change")
673 undo,
675 [VIS_ACTION_REDO] = {
676 "vis-redo",
677 VIS_HELP("Redo last change")
678 redo,
680 [VIS_ACTION_EARLIER] = {
681 "vis-earlier",
682 VIS_HELP("Goto older text state")
683 earlier,
685 [VIS_ACTION_LATER] = {
686 "vis-later",
687 VIS_HELP("Goto newer text state")
688 later,
690 [VIS_ACTION_MACRO_RECORD] = {
691 "vis-macro-record",
692 VIS_HELP("Record macro into given register")
693 macro_record,
695 [VIS_ACTION_MACRO_REPLAY] = {
696 "vis-macro-replay",
697 VIS_HELP("Replay macro, execute the content of the given register")
698 macro_replay,
700 [VIS_ACTION_MARK] = {
701 "vis-mark",
702 VIS_HELP("Use given mark for next action")
703 mark,
705 [VIS_ACTION_REDRAW] = {
706 "vis-redraw",
707 VIS_HELP("Redraw current editor content")
708 call, { .f = vis_redraw }
710 [VIS_ACTION_REPLACE_CHAR] = {
711 "vis-replace-char",
712 VIS_HELP("Replace the character under the cursor")
713 replace,
715 [VIS_ACTION_TOTILL_REPEAT] = {
716 "vis-motion-totill-repeat",
717 VIS_HELP("Repeat latest to/till motion")
718 movement, { .i = VIS_MOVE_TOTILL_REPEAT }
720 [VIS_ACTION_TOTILL_REVERSE] = {
721 "vis-motion-totill-reverse",
722 VIS_HELP("Repeat latest to/till motion but in opposite direction")
723 movement, { .i = VIS_MOVE_TOTILL_REVERSE }
725 [VIS_ACTION_PROMPT_SEARCH_FORWARD] = {
726 "vis-search-forward",
727 VIS_HELP("Search forward")
728 prompt_show, { .s = "/" }
730 [VIS_ACTION_PROMPT_SEARCH_BACKWARD] = {
731 "vis-search-backward",
732 VIS_HELP("Search backward")
733 prompt_show, { .s = "?" }
735 [VIS_ACTION_TILL_LEFT] = {
736 "vis-motion-till-left",
737 VIS_HELP("Till after the occurrence of character to the left")
738 movement_key, { .i = VIS_MOVE_LEFT_TILL }
740 [VIS_ACTION_TILL_RIGHT] = {
741 "vis-motion-till-right",
742 VIS_HELP("Till before the occurrence of character to the right")
743 movement_key, { .i = VIS_MOVE_RIGHT_TILL }
745 [VIS_ACTION_TO_LEFT] = {
746 "vis-motion-to-left",
747 VIS_HELP("To the first occurrence of character to the left")
748 movement_key, { .i = VIS_MOVE_LEFT_TO }
750 [VIS_ACTION_TO_RIGHT] = {
751 "vis-motion-to-right",
752 VIS_HELP("To the first occurrence of character to the right")
753 movement_key, { .i = VIS_MOVE_RIGHT_TO }
755 [VIS_ACTION_REGISTER] = {
756 "vis-register",
757 VIS_HELP("Use given register for next operator")
758 reg,
760 [VIS_ACTION_OPERATOR_CHANGE] = {
761 "vis-operator-change",
762 VIS_HELP("Change operator")
763 operator, { .i = VIS_OP_CHANGE }
765 [VIS_ACTION_OPERATOR_DELETE] = {
766 "vis-operator-delete",
767 VIS_HELP("Delete operator")
768 operator, { .i = VIS_OP_DELETE }
770 [VIS_ACTION_OPERATOR_YANK] = {
771 "vis-operator-yank",
772 VIS_HELP("Yank operator")
773 operator, { .i = VIS_OP_YANK }
775 [VIS_ACTION_OPERATOR_SHIFT_LEFT] = {
776 "vis-operator-shift-left",
777 VIS_HELP("Shift left operator")
778 operator, { .i = VIS_OP_SHIFT_LEFT }
780 [VIS_ACTION_OPERATOR_SHIFT_RIGHT] = {
781 "vis-operator-shift-right",
782 VIS_HELP("Shift right operator")
783 operator, { .i = VIS_OP_SHIFT_RIGHT }
785 [VIS_ACTION_OPERATOR_CASE_LOWER] = {
786 "vis-operator-case-lower",
787 VIS_HELP("Lowercase operator")
788 operator, { .i = VIS_OP_CASE_LOWER }
790 [VIS_ACTION_OPERATOR_CASE_UPPER] = {
791 "vis-operator-case-upper",
792 VIS_HELP("Uppercase operator")
793 operator, { .i = VIS_OP_CASE_UPPER }
795 [VIS_ACTION_OPERATOR_CASE_SWAP] = {
796 "vis-operator-case-swap",
797 VIS_HELP("Swap case operator")
798 operator, { .i = VIS_OP_CASE_SWAP }
800 [VIS_ACTION_OPERATOR_FILTER] = {
801 "vis-operator-filter",
802 VIS_HELP("Filter operator")
803 operator_filter,
805 [VIS_ACTION_OPERATOR_FILTER_FMT] = {
806 "vis-operator-filter-format",
807 VIS_HELP("Formatting operator, filter range through fmt(1)")
808 operator_filter, { .s = "|fmt" }
810 [VIS_ACTION_COUNT] = {
811 "vis-count",
812 VIS_HELP("Count specifier")
813 count,
815 [VIS_ACTION_INSERT_NEWLINE] = {
816 "vis-insert-newline",
817 VIS_HELP("Insert a line break (depending on file type)")
818 call, { .f = vis_insert_nl }
820 [VIS_ACTION_INSERT_TAB] = {
821 "vis-insert-tab",
822 VIS_HELP("Insert a tab (might be converted to spaces)")
823 call, { .f = vis_insert_tab }
825 [VIS_ACTION_INSERT_VERBATIM] = {
826 "vis-insert-verbatim",
827 VIS_HELP("Insert Unicode character based on code point")
828 insert_verbatim,
830 [VIS_ACTION_INSERT_REGISTER] = {
831 "vis-insert-register",
832 VIS_HELP("Insert specified register content")
833 insert_register,
835 [VIS_ACTION_WINDOW_NEXT] = {
836 "vis-window-next",
837 VIS_HELP("Focus next window")
838 call, { .f = vis_window_next }
840 [VIS_ACTION_WINDOW_PREV] = {
841 "vis-window-prev",
842 VIS_HELP("Focus previous window")
843 call, { .f = vis_window_prev }
845 [VIS_ACTION_APPEND_CHAR_NEXT] = {
846 "vis-append-char-next",
847 VIS_HELP("Append text after the cursor")
848 insertmode, { .i = VIS_MOVE_LINE_CHAR_NEXT }
850 [VIS_ACTION_APPEND_LINE_END] = {
851 "vis-append-line-end",
852 VIS_HELP("Append text after the end of the line")
853 insertmode, { .i = VIS_MOVE_LINE_END },
855 [VIS_ACTION_INSERT_LINE_START] = {
856 "vis-insert-line-start",
857 VIS_HELP("Insert text before the first non-blank in the line")
858 insertmode, { .i = VIS_MOVE_LINE_START },
860 [VIS_ACTION_OPEN_LINE_ABOVE] = {
861 "vis-open-line-above",
862 VIS_HELP("Begin a new line above the cursor")
863 openline, { .i = -1 }
865 [VIS_ACTION_OPEN_LINE_BELOW] = {
866 "vis-open-line-below",
867 VIS_HELP("Begin a new line below the cursor")
868 openline, { .i = +1 }
870 [VIS_ACTION_JOIN_LINES] = {
871 "vis-join-lines",
872 VIS_HELP("Join selected lines")
873 join, { .s = " " }
875 [VIS_ACTION_JOIN_LINES_TRIM] = {
876 "vis-join-lines-trim",
877 VIS_HELP("Join selected lines, remove white space")
878 join, { .s = "" }
880 [VIS_ACTION_PROMPT_SHOW] = {
881 "vis-prompt-show",
882 VIS_HELP("Show editor command line prompt")
883 prompt_show, { .s = ":" }
885 [VIS_ACTION_REPEAT] = {
886 "vis-repeat",
887 VIS_HELP("Repeat latest editor command")
888 repeat
890 [VIS_ACTION_SELECTION_FLIP] = {
891 "vis-selection-flip",
892 VIS_HELP("Flip selection, move cursor to other end")
893 selection_end,
895 [VIS_ACTION_WINDOW_REDRAW_TOP] = {
896 "vis-window-redraw-top",
897 VIS_HELP("Redraw cursor line at the top of the window")
898 window, { .w = view_redraw_top }
900 [VIS_ACTION_WINDOW_REDRAW_CENTER] = {
901 "vis-window-redraw-center",
902 VIS_HELP("Redraw cursor line at the center of the window")
903 window, { .w = view_redraw_center }
905 [VIS_ACTION_WINDOW_REDRAW_BOTTOM] = {
906 "vis-window-redraw-bottom",
907 VIS_HELP("Redraw cursor line at the bottom of the window")
908 window, { .w = view_redraw_bottom }
910 [VIS_ACTION_WINDOW_SLIDE_UP] = {
911 "vis-window-slide-up",
912 VIS_HELP("Slide window content upwards")
913 wslide, { .i = -1 }
915 [VIS_ACTION_WINDOW_SLIDE_DOWN] = {
916 "vis-window-slide-down",
917 VIS_HELP("Slide window content downwards")
918 wslide, { .i = +1 }
920 [VIS_ACTION_PUT_AFTER] = {
921 "vis-put-after",
922 VIS_HELP("Put text after the cursor")
923 operator, { .i = VIS_OP_PUT_AFTER }
925 [VIS_ACTION_PUT_BEFORE] = {
926 "vis-put-before",
927 VIS_HELP("Put text before the cursor")
928 operator, { .i = VIS_OP_PUT_BEFORE }
930 [VIS_ACTION_PUT_AFTER_END] = {
931 "vis-put-after-end",
932 VIS_HELP("Put text after the cursor, place cursor after new text")
933 operator, { .i = VIS_OP_PUT_AFTER_END }
935 [VIS_ACTION_PUT_BEFORE_END] = {
936 "vis-put-before-end",
937 VIS_HELP("Put text before the cursor, place cursor after new text")
938 operator, { .i = VIS_OP_PUT_BEFORE_END }
940 [VIS_ACTION_SELECTIONS_MATCH_WORD] = {
941 "vis-selections-select-word",
942 VIS_HELP("Select word under cursor")
943 selections_match_word,
945 [VIS_ACTION_SELECTIONS_NEW_LINE_ABOVE] = {
946 "vis-selection-new-lines-above",
947 VIS_HELP("Create a new selection on the line above")
948 selections_new, { .i = -1 }
950 [VIS_ACTION_SELECTIONS_NEW_LINE_ABOVE_FIRST] = {
951 "vis-selection-new-lines-above-first",
952 VIS_HELP("Create a new selection on the line above the first selection")
953 selections_new, { .i = INT_MIN }
955 [VIS_ACTION_SELECTIONS_NEW_LINE_BELOW] = {
956 "vis-selection-new-lines-below",
957 VIS_HELP("Create a new selection on the line below")
958 selections_new, { .i = +1 }
960 [VIS_ACTION_SELECTIONS_NEW_LINE_BELOW_LAST] = {
961 "vis-selection-new-lines-below-last",
962 VIS_HELP("Create a new selection on the line below the last selection")
963 selections_new, { .i = INT_MAX }
965 [VIS_ACTION_SELECTIONS_NEW_LINES_BEGIN] = {
966 "vis-selection-new-lines-begin",
967 VIS_HELP("Create a new selection at the start of every line covered by selection")
968 operator, { .i = VIS_OP_CURSOR_SOL }
970 [VIS_ACTION_SELECTIONS_NEW_LINES_END] = {
971 "vis-selection-new-lines-end",
972 VIS_HELP("Create a new selection at the end of every line covered by selection")
973 operator, { .i = VIS_OP_CURSOR_EOL }
975 [VIS_ACTION_SELECTIONS_NEW_MATCH_NEXT] = {
976 "vis-selection-new-match-next",
977 VIS_HELP("Select the next region matching the current selection")
978 selections_match_next,
980 [VIS_ACTION_SELECTIONS_NEW_MATCH_SKIP] = {
981 "vis-selection-new-match-skip",
982 VIS_HELP("Clear current selection, but select next match")
983 selections_match_skip,
985 [VIS_ACTION_SELECTIONS_ALIGN] = {
986 "vis-selections-align",
987 VIS_HELP("Try to align all selections on the same column")
988 selections_align,
990 [VIS_ACTION_SELECTIONS_ALIGN_INDENT_LEFT] = {
991 "vis-selections-align-indent-left",
992 VIS_HELP("Left align all selections/selections by inserting spaces")
993 selections_align_indent, { .i = -1 }
995 [VIS_ACTION_SELECTIONS_ALIGN_INDENT_RIGHT] = {
996 "vis-selections-align-indent-right",
997 VIS_HELP("Right align all selections/selections by inserting spaces")
998 selections_align_indent, { .i = +1 }
1000 [VIS_ACTION_SELECTIONS_REMOVE_ALL] = {
1001 "vis-selections-remove-all",
1002 VIS_HELP("Remove all but the primary selection")
1003 selections_clear,
1005 [VIS_ACTION_SELECTIONS_REMOVE_LAST] = {
1006 "vis-selections-remove-last",
1007 VIS_HELP("Remove least recently created selection")
1008 selections_remove,
1010 [VIS_ACTION_SELECTIONS_REMOVE_COLUMN] = {
1011 "vis-selections-remove-column",
1012 VIS_HELP("Remove count selection column")
1013 selections_remove_column, { .i = 1 }
1015 [VIS_ACTION_SELECTIONS_REMOVE_COLUMN_EXCEPT] = {
1016 "vis-selections-remove-column-except",
1017 VIS_HELP("Remove all but the count selection column")
1018 selections_remove_column_except, { .i = 1 }
1020 [VIS_ACTION_SELECTIONS_PREV] = {
1021 "vis-selection-prev",
1022 VIS_HELP("Move to the previous selection")
1023 selections_navigate, { .i = -PAGE_HALF }
1025 [VIS_ACTION_SELECTIONS_NEXT] = {
1026 "vis-selection-next",
1027 VIS_HELP("Move to the next selection")
1028 selections_navigate, { .i = +PAGE_HALF }
1030 [VIS_ACTION_SELECTIONS_ROTATE_LEFT] = {
1031 "vis-selections-rotate-left",
1032 VIS_HELP("Rotate selections left")
1033 selections_rotate, { .i = -1 }
1035 [VIS_ACTION_SELECTIONS_ROTATE_RIGHT] = {
1036 "vis-selections-rotate-right",
1037 VIS_HELP("Rotate selections right")
1038 selections_rotate, { .i = +1 }
1040 [VIS_ACTION_SELECTIONS_TRIM] = {
1041 "vis-selections-trim",
1042 VIS_HELP("Remove leading and trailing white space from selections")
1043 selections_trim
1045 [VIS_ACTION_SELECTIONS_SAVE] = {
1046 "vis-selections-save",
1047 VIS_HELP("Save currently active selections to register")
1048 selections_save
1050 [VIS_ACTION_SELECTIONS_RESTORE] = {
1051 "vis-selections-restore",
1052 VIS_HELP("Restore selections from register")
1053 selections_restore
1055 [VIS_ACTION_SELECTIONS_UNION] = {
1056 "vis-selections-union",
1057 VIS_HELP("Add selections from register")
1058 selections_union
1060 [VIS_ACTION_SELECTIONS_INTERSECT] = {
1061 "vis-selections-intersect",
1062 VIS_HELP("Intersect selections with register")
1063 selections_intersect
1065 [VIS_ACTION_SELECTIONS_COMPLEMENT] = {
1066 "vis-selections-complement",
1067 VIS_HELP("Complement selections")
1068 selections_complement
1070 [VIS_ACTION_SELECTIONS_MINUS] = {
1071 "vis-selections-minus",
1072 VIS_HELP("Subtract selections from register")
1073 selections_minus
1075 [VIS_ACTION_SELECTIONS_COMBINE_UNION] = {
1076 "vis-selections-combine-union",
1077 VIS_HELP("Pairwise union with selection from register")
1078 selections_combine, { .combine = combine_union }
1080 [VIS_ACTION_SELECTIONS_COMBINE_INTERSECT] = {
1081 "vis-selections-combine-intersect",
1082 VIS_HELP("Pairwise intersect with selections from register")
1083 selections_combine, { .combine = combine_intersect }
1085 [VIS_ACTION_SELECTIONS_COMBINE_LONGER] = {
1086 "vis-selections-combine-longer",
1087 VIS_HELP("Pairwise combine: take longer")
1088 selections_combine, { .combine = combine_longer }
1090 [VIS_ACTION_SELECTIONS_COMBINE_SHORTER] = {
1091 "vis-selections-combine-shorter",
1092 VIS_HELP("Pairwise combine: take shorter")
1093 selections_combine, { .combine = combine_shorter }
1095 [VIS_ACTION_SELECTIONS_COMBINE_LEFTMOST] = {
1096 "vis-selections-combine-leftmost",
1097 VIS_HELP("Pairwise combine: leftmost")
1098 selections_combine, { .combine = combine_leftmost }
1100 [VIS_ACTION_SELECTIONS_COMBINE_RIGHTMOST] = {
1101 "vis-selections-combine-rightmost",
1102 VIS_HELP("Pairwise combine: rightmost")
1103 selections_combine, { .combine = combine_rightmost }
1105 [VIS_ACTION_TEXT_OBJECT_WORD_OUTER] = {
1106 "vis-textobject-word-outer",
1107 VIS_HELP("A word leading and trailing whitespace included")
1108 textobj, { .i = VIS_TEXTOBJECT_OUTER_WORD }
1110 [VIS_ACTION_TEXT_OBJECT_WORD_INNER] = {
1111 "vis-textobject-word-inner",
1112 VIS_HELP("A word leading and trailing whitespace excluded")
1113 textobj, { .i = VIS_TEXTOBJECT_INNER_WORD }
1115 [VIS_ACTION_TEXT_OBJECT_LONGWORD_OUTER] = {
1116 "vis-textobject-bigword-outer",
1117 VIS_HELP("A WORD leading and trailing whitespace included")
1118 textobj, { .i = VIS_TEXTOBJECT_OUTER_LONGWORD }
1120 [VIS_ACTION_TEXT_OBJECT_LONGWORD_INNER] = {
1121 "vis-textobject-bigword-inner",
1122 VIS_HELP("A WORD leading and trailing whitespace excluded")
1123 textobj, { .i = VIS_TEXTOBJECT_INNER_LONGWORD }
1125 [VIS_ACTION_TEXT_OBJECT_SENTENCE] = {
1126 "vis-textobject-sentence",
1127 VIS_HELP("A sentence")
1128 textobj, { .i = VIS_TEXTOBJECT_SENTENCE }
1130 [VIS_ACTION_TEXT_OBJECT_PARAGRAPH] = {
1131 "vis-textobject-paragraph",
1132 VIS_HELP("A paragraph")
1133 textobj, { .i = VIS_TEXTOBJECT_PARAGRAPH }
1135 [VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_OUTER] = {
1136 "vis-textobject-square-bracket-outer",
1137 VIS_HELP("[] block (outer variant)")
1138 textobj, { .i = VIS_TEXTOBJECT_OUTER_SQUARE_BRACKET }
1140 [VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_INNER] = {
1141 "vis-textobject-square-bracket-inner",
1142 VIS_HELP("[] block (inner variant)")
1143 textobj, { .i = VIS_TEXTOBJECT_INNER_SQUARE_BRACKET }
1145 [VIS_ACTION_TEXT_OBJECT_PARANTHESE_OUTER] = {
1146 "vis-textobject-parentheses-outer",
1147 VIS_HELP("() block (outer variant)")
1148 textobj, { .i = VIS_TEXTOBJECT_OUTER_PARANTHESE }
1150 [VIS_ACTION_TEXT_OBJECT_PARANTHESE_INNER] = {
1151 "vis-textobject-parentheses-inner",
1152 VIS_HELP("() block (inner variant)")
1153 textobj, { .i = VIS_TEXTOBJECT_INNER_PARANTHESE }
1155 [VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_OUTER] = {
1156 "vis-textobject-angle-bracket-outer",
1157 VIS_HELP("<> block (outer variant)")
1158 textobj, { .i = VIS_TEXTOBJECT_OUTER_ANGLE_BRACKET }
1160 [VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_INNER] = {
1161 "vis-textobject-angle-bracket-inner",
1162 VIS_HELP("<> block (inner variant)")
1163 textobj, { .i = VIS_TEXTOBJECT_INNER_ANGLE_BRACKET }
1165 [VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_OUTER] = {
1166 "vis-textobject-curly-bracket-outer",
1167 VIS_HELP("{} block (outer variant)")
1168 textobj, { .i = VIS_TEXTOBJECT_OUTER_CURLY_BRACKET }
1170 [VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_INNER] = {
1171 "vis-textobject-curly-bracket-inner",
1172 VIS_HELP("{} block (inner variant)")
1173 textobj, { .i = VIS_TEXTOBJECT_INNER_CURLY_BRACKET }
1175 [VIS_ACTION_TEXT_OBJECT_QUOTE_OUTER] = {
1176 "vis-textobject-quote-outer",
1177 VIS_HELP("A quoted string, including the quotation marks")
1178 textobj, { .i = VIS_TEXTOBJECT_OUTER_QUOTE }
1180 [VIS_ACTION_TEXT_OBJECT_QUOTE_INNER] = {
1181 "vis-textobject-quote-inner",
1182 VIS_HELP("A quoted string, excluding the quotation marks")
1183 textobj, { .i = VIS_TEXTOBJECT_INNER_QUOTE }
1185 [VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_OUTER] = {
1186 "vis-textobject-single-quote-outer",
1187 VIS_HELP("A single quoted string, including the quotation marks")
1188 textobj, { .i = VIS_TEXTOBJECT_OUTER_SINGLE_QUOTE }
1190 [VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_INNER] = {
1191 "vis-textobject-single-quote-inner",
1192 VIS_HELP("A single quoted string, excluding the quotation marks")
1193 textobj, { .i = VIS_TEXTOBJECT_INNER_SINGLE_QUOTE }
1195 [VIS_ACTION_TEXT_OBJECT_BACKTICK_OUTER] = {
1196 "vis-textobject-backtick-outer",
1197 VIS_HELP("A backtick delimited string (outer variant)")
1198 textobj, { .i = VIS_TEXTOBJECT_OUTER_BACKTICK }
1200 [VIS_ACTION_TEXT_OBJECT_BACKTICK_INNER] = {
1201 "vis-textobject-backtick-inner",
1202 VIS_HELP("A backtick delimited string (inner variant)")
1203 textobj, { .i = VIS_TEXTOBJECT_INNER_BACKTICK }
1205 [VIS_ACTION_TEXT_OBJECT_ENTIRE_OUTER] = {
1206 "vis-textobject-entire-outer",
1207 VIS_HELP("The whole text content")
1208 textobj, { .i = VIS_TEXTOBJECT_OUTER_ENTIRE }
1210 [VIS_ACTION_TEXT_OBJECT_ENTIRE_INNER] = {
1211 "vis-textobject-entire-inner",
1212 VIS_HELP("The whole text content, except for leading and trailing empty lines")
1213 textobj, { .i = VIS_TEXTOBJECT_INNER_ENTIRE }
1215 [VIS_ACTION_TEXT_OBJECT_LINE_OUTER] = {
1216 "vis-textobject-line-outer",
1217 VIS_HELP("The whole line")
1218 textobj, { .i = VIS_TEXTOBJECT_OUTER_LINE }
1220 [VIS_ACTION_TEXT_OBJECT_LINE_INNER] = {
1221 "vis-textobject-line-inner",
1222 VIS_HELP("The whole line, excluding leading and trailing whitespace")
1223 textobj, { .i = VIS_TEXTOBJECT_INNER_LINE }
1225 [VIS_ACTION_TEXT_OBJECT_INDENTATION] = {
1226 "vis-textobject-indentation",
1227 VIS_HELP("All adjacent lines with the same indentation level as the current one")
1228 textobj, { .i = VIS_TEXTOBJECT_INDENTATION }
1230 [VIS_ACTION_TEXT_OBJECT_SEARCH_FORWARD] = {
1231 "vis-textobject-search-forward",
1232 VIS_HELP("The next search match in forward direction")
1233 textobj, { .i = VIS_TEXTOBJECT_SEARCH_FORWARD }
1235 [VIS_ACTION_TEXT_OBJECT_SEARCH_BACKWARD] = {
1236 "vis-textobject-search-backward",
1237 VIS_HELP("The next search match in backward direction")
1238 textobj, { .i = VIS_TEXTOBJECT_SEARCH_BACKWARD }
1240 [VIS_ACTION_MOTION_CHARWISE] = {
1241 "vis-motion-charwise",
1242 VIS_HELP("Force motion to be charwise")
1243 motiontype, { .i = VIS_MOTIONTYPE_CHARWISE }
1245 [VIS_ACTION_MOTION_LINEWISE] = {
1246 "vis-motion-linewise",
1247 VIS_HELP("Force motion to be linewise")
1248 motiontype, { .i = VIS_MOTIONTYPE_LINEWISE }
1250 [VIS_ACTION_UNICODE_INFO] = {
1251 "vis-unicode-info",
1252 VIS_HELP("Show Unicode codepoint(s) of character under cursor")
1253 unicode_info, { .i = VIS_ACTION_UNICODE_INFO }
1255 [VIS_ACTION_UTF8_INFO] = {
1256 "vis-utf8-info",
1257 VIS_HELP("Show UTF-8 encoded codepoint(s) of character under cursor")
1258 unicode_info, { .i = VIS_ACTION_UTF8_INFO }
1260 [VIS_ACTION_NOP] = {
1261 "vis-nop",
1262 VIS_HELP("Ignore key, do nothing")
1263 nop,
1267 #include "config.h"
1269 /** key bindings functions */
1271 static const char *nop(Vis *vis, const char *keys, const Arg *arg) {
1272 return keys;
1275 static const char *macro_record(Vis *vis, const char *keys, const Arg *arg) {
1276 if (!vis_macro_record_stop(vis)) {
1277 if (!keys[0])
1278 return NULL;
1279 const char *next = vis_keys_next(vis, keys);
1280 if (next - keys > 1)
1281 return next;
1282 enum VisRegister reg = vis_register_from(vis, keys[0]);
1283 vis_macro_record(vis, reg);
1284 keys++;
1286 vis_draw(vis);
1287 return keys;
1290 static const char *macro_replay(Vis *vis, const char *keys, const Arg *arg) {
1291 if (!keys[0])
1292 return NULL;
1293 const char *next = vis_keys_next(vis, keys);
1294 if (next - keys > 1)
1295 return next;
1296 enum VisRegister reg = vis_register_from(vis, keys[0]);
1297 vis_macro_replay(vis, reg);
1298 return keys+1;
1301 static const char *suspend(Vis *vis, const char *keys, const Arg *arg) {
1302 vis_suspend(vis);
1303 return keys;
1306 static const char *repeat(Vis *vis, const char *keys, const Arg *arg) {
1307 vis_repeat(vis);
1308 return keys;
1311 static const char *selections_new(Vis *vis, const char *keys, const Arg *arg) {
1312 View *view = vis_view(vis);
1313 bool anchored = view_selections_anchored(view_selections_primary_get(view));
1314 VisCountIterator it = vis_count_iterator_get(vis, 1);
1315 while (vis_count_iterator_next(&it)) {
1316 Selection *sel = NULL;
1317 switch (arg->i) {
1318 case -1:
1319 case +1:
1320 sel = view_selections_primary_get(view);
1321 break;
1322 case INT_MIN:
1323 sel = view_selections(view);
1324 break;
1325 case INT_MAX:
1326 for (Selection *s = view_selections(view); s; s = view_selections_next(s))
1327 sel = s;
1328 break;
1331 if (!sel)
1332 return keys;
1334 size_t oldpos = view_cursors_pos(sel);
1335 if (arg->i > 0)
1336 view_line_down(sel);
1337 else if (arg->i < 0)
1338 view_line_up(sel);
1339 size_t newpos = view_cursors_pos(sel);
1340 view_cursors_to(sel, oldpos);
1341 Selection *sel_new = view_selections_new(view, newpos);
1342 if (!sel_new) {
1343 if (arg->i == -1)
1344 sel_new = view_selections_prev(sel);
1345 else if (arg->i == +1)
1346 sel_new = view_selections_next(sel);
1348 if (sel_new) {
1349 view_selections_primary_set(sel_new);
1350 view_selections_anchor(sel_new, anchored);
1353 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1354 return keys;
1357 static const char *selections_align(Vis *vis, const char *keys, const Arg *arg) {
1358 View *view = vis_view(vis);
1359 Text *txt = vis_text(vis);
1360 int mincol = INT_MAX;
1361 for (Selection *s = view_selections(view); s; s = view_selections_next(s)) {
1362 int col = view_cursors_cell_get(s);
1363 if (col >= 0 && col < mincol)
1364 mincol = col;
1366 for (Selection *s = view_selections(view); s; s = view_selections_next(s)) {
1367 if (view_cursors_cell_set(s, mincol) == -1) {
1368 size_t pos = view_cursors_pos(s);
1369 size_t col = text_line_width_set(txt, pos, mincol);
1370 view_cursors_to(s, col);
1373 return keys;
1376 static const char *selections_align_indent(Vis *vis, const char *keys, const Arg *arg) {
1377 View *view = vis_view(vis);
1378 Text *txt = vis_text(vis);
1379 bool left_align = arg->i < 0;
1380 int columns = view_selections_column_count(view);
1382 for (int i = 0; i < columns; i++) {
1383 int mincol = INT_MAX, maxcol = 0;
1384 for (Selection *s = view_selections_column(view, i); s; s = view_selections_column_next(s, i)) {
1385 Filerange sel = view_selections_get(s);
1386 size_t pos = left_align ? sel.start : sel.end;
1387 int col = text_line_width_get(txt, pos);
1388 if (col < mincol)
1389 mincol = col;
1390 if (col > maxcol)
1391 maxcol = col;
1394 size_t len = maxcol - mincol;
1395 char *buf = malloc(len+1);
1396 if (!buf)
1397 return keys;
1398 memset(buf, ' ', len);
1400 for (Selection *s = view_selections_column(view, i); s; s = view_selections_column_next(s, i)) {
1401 Filerange sel = view_selections_get(s);
1402 size_t pos = left_align ? sel.start : sel.end;
1403 size_t ipos = sel.start;
1404 int col = text_line_width_get(txt, pos);
1405 if (col < maxcol) {
1406 size_t off = maxcol - col;
1407 if (off <= len)
1408 text_insert(txt, ipos, buf, off);
1412 free(buf);
1415 view_draw(view);
1416 return keys;
1419 static const char *selections_clear(Vis *vis, const char *keys, const Arg *arg) {
1420 View *view = vis_view(vis);
1421 if (view_selections_count(view) > 1)
1422 view_selections_dispose_all(view);
1423 else
1424 view_selection_clear(view_selections_primary_get(view));
1425 return keys;
1428 static const char *selections_match_word(Vis *vis, const char *keys, const Arg *arg) {
1429 Text *txt = vis_text(vis);
1430 View *view = vis_view(vis);
1431 for (Selection *s = view_selections(view); s; s = view_selections_next(s)) {
1432 Filerange word = text_object_word(txt, view_cursors_pos(s));
1433 if (text_range_valid(&word))
1434 view_selections_set(s, &word);
1436 vis_mode_switch(vis, VIS_MODE_VISUAL);
1437 return keys;
1440 static const char *selections_match_next(Vis *vis, const char *keys, const Arg *arg) {
1441 Text *txt = vis_text(vis);
1442 View *view = vis_view(vis);
1443 Selection *s = view_selections_primary_get(view);
1444 Filerange sel = view_selections_get(s);
1445 if (!text_range_valid(&sel))
1446 return keys;
1448 char *buf = text_bytes_alloc0(txt, sel.start, text_range_size(&sel));
1449 if (!buf)
1450 return keys;
1451 Filerange word = text_object_word_find_next(txt, sel.end, buf);
1452 if (text_range_valid(&word)) {
1453 size_t pos = text_char_prev(txt, word.end);
1454 if ((s = view_selections_new(view, pos))) {
1455 view_selections_set(s, &word);
1456 view_selections_anchor(s, true);
1457 view_selections_primary_set(s);
1458 goto out;
1462 sel = view_selections_get(view_selections(view));
1463 word = text_object_word_find_prev(txt, sel.start, buf);
1464 if (!text_range_valid(&word))
1465 goto out;
1466 size_t pos = text_char_prev(txt, word.end);
1467 if ((s = view_selections_new(view, pos))) {
1468 view_selections_set(s, &word);
1469 view_selections_anchor(s, true);
1470 view_selections_primary_set(s);
1473 out:
1474 free(buf);
1475 return keys;
1478 static const char *selections_match_skip(Vis *vis, const char *keys, const Arg *arg) {
1479 View *view = vis_view(vis);
1480 Selection *sel = view_selections_primary_get(view);
1481 keys = selections_match_next(vis, keys, arg);
1482 if (sel != view_selections_primary_get(view))
1483 view_selections_dispose(sel);
1484 return keys;
1487 static const char *selections_remove(Vis *vis, const char *keys, const Arg *arg) {
1488 View *view = vis_view(vis);
1489 view_selections_dispose(view_selections_primary_get(view));
1490 view_cursor_to(view, view_cursor_get(view));
1491 return keys;
1494 static const char *selections_remove_column(Vis *vis, const char *keys, const Arg *arg) {
1495 View *view = vis_view(vis);
1496 int max = view_selections_column_count(view);
1497 int column = vis_count_get_default(vis, arg->i) - 1;
1498 if (column >= max)
1499 column = max - 1;
1500 if (view_selections_count(view) == 1) {
1501 vis_mode_switch(vis, VIS_MODE_NORMAL);
1502 return keys;
1505 for (Selection *s = view_selections_column(view, column), *next; s; s = next) {
1506 next = view_selections_column_next(s, column);
1507 view_selections_dispose(s);
1510 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1511 return keys;
1514 static const char *selections_remove_column_except(Vis *vis, const char *keys, const Arg *arg) {
1515 View *view = vis_view(vis);
1516 int max = view_selections_column_count(view);
1517 int column = vis_count_get_default(vis, arg->i) - 1;
1518 if (column >= max)
1519 column = max - 1;
1520 if (view_selections_count(view) == 1) {
1521 vis_redraw(vis);
1522 return keys;
1525 Selection *sel = view_selections(view);
1526 Selection *col = view_selections_column(view, column);
1527 for (Selection *next; sel; sel = next) {
1528 next = view_selections_next(sel);
1529 if (sel == col)
1530 col = view_selections_column_next(col, column);
1531 else
1532 view_selections_dispose(sel);
1535 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1536 return keys;
1539 static const char *selections_navigate(Vis *vis, const char *keys, const Arg *arg) {
1540 View *view = vis_view(vis);
1541 if (view_selections_count(view) == 1)
1542 return wscroll(vis, keys, arg);
1543 Selection *s = view_selections_primary_get(view);
1544 VisCountIterator it = vis_count_iterator_get(vis, 1);
1545 while (vis_count_iterator_next(&it)) {
1546 if (arg->i > 0) {
1547 s = view_selections_next(s);
1548 if (!s)
1549 s = view_selections(view);
1550 } else {
1551 s = view_selections_prev(s);
1552 if (!s) {
1553 s = view_selections(view);
1554 for (Selection *n = s; n; n = view_selections_next(n))
1555 s = n;
1559 view_selections_primary_set(s);
1560 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1561 return keys;
1564 static const char *selections_rotate(Vis *vis, const char *keys, const Arg *arg) {
1566 typedef struct {
1567 Selection *sel;
1568 char *data;
1569 size_t len;
1570 } Rotate;
1572 Array arr;
1573 Text *txt = vis_text(vis);
1574 View *view = vis_view(vis);
1575 int columns = view_selections_column_count(view);
1576 int selections = columns == 1 ? view_selections_count(view) : columns;
1577 int count = vis_count_get_default(vis, 1);
1578 array_init_sized(&arr, sizeof(Rotate));
1579 if (!array_reserve(&arr, selections))
1580 return keys;
1581 size_t line = 0;
1583 for (Selection *s = view_selections(view), *next; s; s = next) {
1584 next = view_selections_next(s);
1585 size_t line_next = 0;
1587 Filerange sel = view_selections_get(s);
1588 Rotate rot;
1589 rot.sel = s;
1590 rot.len = text_range_size(&sel);
1591 if ((rot.data = malloc(rot.len)))
1592 rot.len = text_bytes_get(txt, sel.start, rot.len, rot.data);
1593 else
1594 rot.len = 0;
1595 array_add(&arr, &rot);
1597 if (!line)
1598 line = text_lineno_by_pos(txt, view_cursors_pos(s));
1599 if (next)
1600 line_next = text_lineno_by_pos(txt, view_cursors_pos(next));
1601 if (!next || (columns > 1 && line != line_next)) {
1602 size_t len = array_length(&arr);
1603 size_t off = arg->i > 0 ? count % len : len - (count % len);
1604 for (size_t i = 0; i < len; i++) {
1605 size_t j = (i + off) % len;
1606 Rotate *oldrot = array_get(&arr, i);
1607 Rotate *newrot = array_get(&arr, j);
1608 if (!oldrot || !newrot || oldrot == newrot)
1609 continue;
1610 Filerange newsel = view_selections_get(newrot->sel);
1611 if (!text_range_valid(&newsel))
1612 continue;
1613 if (!text_delete_range(txt, &newsel))
1614 continue;
1615 if (!text_insert(txt, newsel.start, oldrot->data, oldrot->len))
1616 continue;
1617 newsel.end = newsel.start + oldrot->len;
1618 view_selections_set(newrot->sel, &newsel);
1619 free(oldrot->data);
1621 array_clear(&arr);
1623 line = line_next;
1626 array_release(&arr);
1627 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1628 return keys;
1631 static const char *selections_trim(Vis *vis, const char *keys, const Arg *arg) {
1632 Text *txt = vis_text(vis);
1633 View *view = vis_view(vis);
1634 for (Selection *s = view_selections(view), *next; s; s = next) {
1635 next = view_selections_next(s);
1636 Filerange sel = view_selections_get(s);
1637 if (!text_range_valid(&sel))
1638 continue;
1639 for (char b; sel.start < sel.end && text_byte_get(txt, sel.end-1, &b)
1640 && isspace((unsigned char)b); sel.end--);
1641 for (char b; sel.start <= sel.end && text_byte_get(txt, sel.start, &b)
1642 && isspace((unsigned char)b); sel.start++);
1643 if (sel.start < sel.end) {
1644 view_selections_set(s, &sel);
1645 } else if (!view_selections_dispose(s)) {
1646 vis_mode_switch(vis, VIS_MODE_NORMAL);
1649 return keys;
1652 static void selections_set(Vis *vis, View *view, Array *sel) {
1653 enum VisMode mode = vis_mode_get(vis);
1654 bool anchored = mode == VIS_MODE_VISUAL || mode == VIS_MODE_VISUAL_LINE;
1655 view_selections_set_all(view, sel, anchored);
1656 if (!anchored)
1657 view_selections_clear_all(view);
1660 static const char *selections_save(Vis *vis, const char *keys, const Arg *arg) {
1661 Win *win = vis_window(vis);
1662 View *view = vis_view(vis);
1663 enum VisMark mark = vis_mark_used(vis);
1664 Array sel = view_selections_get_all(view);
1665 vis_mark_set(win, mark, &sel);
1666 array_release(&sel);
1667 vis_cancel(vis);
1668 return keys;
1671 static const char *selections_restore(Vis *vis, const char *keys, const Arg *arg) {
1672 Win *win = vis_window(vis);
1673 View *view = vis_view(vis);
1674 enum VisMark mark = vis_mark_used(vis);
1675 Array sel = vis_mark_get(win, mark);
1676 selections_set(vis, view, &sel);
1677 array_release(&sel);
1678 vis_cancel(vis);
1679 return keys;
1682 static const char *selections_union(Vis *vis, const char *keys, const Arg *arg) {
1683 Win *win = vis_window(vis);
1684 View *view = vis_view(vis);
1685 enum VisMark mark = vis_mark_used(vis);
1686 Array a = vis_mark_get(win, mark);
1687 Array b = view_selections_get_all(view);
1688 Array sel;
1689 array_init_from(&sel, &a);
1691 size_t i = 0, j = 0;
1692 Filerange *r1 = array_get(&a, i), *r2 = array_get(&b, j), cur = text_range_empty();
1693 while (r1 || r2) {
1694 if (r1 && text_range_overlap(r1, &cur)) {
1695 cur = text_range_union(r1, &cur);
1696 r1 = array_get(&a, ++i);
1697 } else if (r2 && text_range_overlap(r2, &cur)) {
1698 cur = text_range_union(r2, &cur);
1699 r2 = array_get(&b, ++j);
1700 } else {
1701 if (text_range_valid(&cur))
1702 array_add(&sel, &cur);
1703 if (!r1) {
1704 cur = *r2;
1705 r2 = array_get(&b, ++j);
1706 } else if (!r2) {
1707 cur = *r1;
1708 r1 = array_get(&a, ++i);
1709 } else {
1710 if (r1->start < r2->start) {
1711 cur = *r1;
1712 r1 = array_get(&a, ++i);
1713 } else {
1714 cur = *r2;
1715 r2 = array_get(&b, ++j);
1721 if (text_range_valid(&cur))
1722 array_add(&sel, &cur);
1724 selections_set(vis, view, &sel);
1725 vis_cancel(vis);
1727 array_release(&a);
1728 array_release(&b);
1729 array_release(&sel);
1731 return keys;
1734 static void intersect(Array *ret, Array *a, Array *b) {
1735 size_t i = 0, j = 0;
1736 Filerange *r1 = array_get(a, i), *r2 = array_get(b, j);
1737 while (r1 && r2) {
1738 if (text_range_overlap(r1, r2)) {
1739 Filerange new = text_range_intersect(r1, r2);
1740 array_add(ret, &new);
1742 if (r1->end < r2->end)
1743 r1 = array_get(a, ++i);
1744 else
1745 r2 = array_get(b, ++j);
1749 static const char *selections_intersect(Vis *vis, const char *keys, const Arg *arg) {
1750 Win *win = vis_window(vis);
1751 View *view = vis_view(vis);
1752 enum VisMark mark = vis_mark_used(vis);
1753 Array a = vis_mark_get(win, mark);
1754 Array b = view_selections_get_all(view);
1755 Array sel;
1756 array_init_from(&sel, &a);
1758 intersect(&sel, &a, &b);
1759 selections_set(vis, view, &sel);
1760 vis_cancel(vis);
1762 array_release(&a);
1763 array_release(&b);
1764 array_release(&sel);
1766 return keys;
1769 static void complement(Array *ret, Array *a, Filerange *universe) {
1770 size_t pos = universe->start;
1771 for (size_t i = 0, len = array_length(a); i < len; i++) {
1772 Filerange *r = array_get(a, i);
1773 if (pos < r->start) {
1774 Filerange new = text_range_new(pos, r->start);
1775 array_add(ret, &new);
1777 pos = r->end;
1779 if (pos < universe->end) {
1780 Filerange new = text_range_new(pos, universe->end);
1781 array_add(ret, &new);
1785 static const char *selections_complement(Vis *vis, const char *keys, const Arg *arg) {
1786 Text *txt = vis_text(vis);
1787 View *view = vis_view(vis);
1788 Filerange universe = text_object_entire(txt, 0);
1789 Array a = view_selections_get_all(view);
1790 Array sel;
1791 array_init_from(&sel, &a);
1793 complement(&sel, &a, &universe);
1795 selections_set(vis, view, &sel);
1796 array_release(&a);
1797 array_release(&sel);
1798 return keys;
1801 static const char *selections_minus(Vis *vis, const char *keys, const Arg *arg) {
1802 Text *txt = vis_text(vis);
1803 Win *win = vis_window(vis);
1804 View *view = vis_view(vis);
1805 enum VisMark mark = vis_mark_used(vis);
1806 Array a = view_selections_get_all(view);
1807 Array b = vis_mark_get(win, mark);
1808 Array sel;
1809 array_init_from(&sel, &a);
1810 Array b_complement;
1811 array_init_from(&b_complement, &b);
1813 Filerange universe = text_object_entire(txt, 0);
1814 complement(&b_complement, &b, &universe);
1815 intersect(&sel, &a, &b_complement);
1817 selections_set(vis, view, &sel);
1818 vis_cancel(vis);
1820 array_release(&a);
1821 array_release(&b);
1822 array_release(&b_complement);
1823 array_release(&sel);
1825 return keys;
1828 static Filerange combine_union(const Filerange *r1, const Filerange *r2) {
1829 if (!r1)
1830 return *r2;
1831 if (!r2)
1832 return *r1;
1833 return text_range_union(r1, r2);
1836 static Filerange combine_intersect(const Filerange *r1, const Filerange *r2) {
1837 if (!r1 || !r2)
1838 return text_range_empty();
1839 return text_range_intersect(r1, r2);
1842 static Filerange combine_longer(const Filerange *r1, const Filerange *r2) {
1843 if (!r1)
1844 return *r2;
1845 if (!r2)
1846 return *r1;
1847 size_t l1 = text_range_size(r1);
1848 size_t l2 = text_range_size(r2);
1849 return l1 < l2 ? *r2 : *r1;
1852 static Filerange combine_shorter(const Filerange *r1, const Filerange *r2) {
1853 if (!r1)
1854 return *r2;
1855 if (!r2)
1856 return *r1;
1857 size_t l1 = text_range_size(r1);
1858 size_t l2 = text_range_size(r2);
1859 return l1 < l2 ? *r1 : *r2;
1862 static Filerange combine_leftmost(const Filerange *r1, const Filerange *r2) {
1863 if (!r1)
1864 return *r2;
1865 if (!r2)
1866 return *r1;
1867 return r1->start < r2->start || (r1->start == r2->start && r1->end < r2->end) ? *r1 : *r2;
1870 static Filerange combine_rightmost(const Filerange *r1, const Filerange *r2) {
1871 if (!r1)
1872 return *r2;
1873 if (!r2)
1874 return *r1;
1875 return r1->start < r2->start || (r1->start == r2->start && r1->end < r2->end) ? *r2 : *r1;
1878 static const char *selections_combine(Vis *vis, const char *keys, const Arg *arg) {
1879 Win *win = vis_window(vis);
1880 View *view = vis_view(vis);
1881 enum VisMark mark = vis_mark_used(vis);
1882 Array a = view_selections_get_all(view);
1883 Array b = vis_mark_get(win, mark);
1884 Array sel;
1885 array_init_from(&sel, &a);
1887 Filerange *r1 = array_get(&a, 0), *r2 = array_get(&b, 0);
1888 for (size_t i = 0, j = 0; r1 || r2; r1 = array_get(&a, ++i), r2 = array_get(&b, ++j)) {
1889 Filerange new = arg->combine(r1, r2);
1890 if (text_range_valid(&new))
1891 array_add(&sel, &new);
1894 vis_mark_normalize(&sel);
1895 selections_set(vis, view, &sel);
1896 vis_cancel(vis);
1898 array_release(&a);
1899 array_release(&b);
1900 array_release(&sel);
1902 return keys;
1905 static const char *replace(Vis *vis, const char *keys, const Arg *arg) {
1906 if (!keys[0]) {
1907 vis_keymap_disable(vis);
1908 return NULL;
1911 const char *next = vis_keys_next(vis, keys);
1912 if (!next)
1913 return NULL;
1915 char replacement[UTFmax+1];
1916 if (!vis_keys_utf8(vis, keys, replacement))
1917 return next;
1919 if (replacement[0] == 0x1b) /* <Escape> */
1920 return next;
1922 vis_operator(vis, VIS_OP_REPLACE, replacement);
1923 if (vis_mode_get(vis) == VIS_MODE_OPERATOR_PENDING)
1924 vis_motion(vis, VIS_MOVE_CHAR_NEXT);
1925 return next;
1928 static const char *count(Vis *vis, const char *keys, const Arg *arg) {
1929 int digit = keys[-1] - '0';
1930 int count = vis_count_get_default(vis, 0);
1931 if (0 <= digit && digit <= 9) {
1932 if (digit == 0 && count == 0)
1933 vis_motion(vis, VIS_MOVE_LINE_BEGIN);
1934 else
1935 vis_count_set(vis, count * 10 + digit);
1937 return keys;
1940 static const char *gotoline(Vis *vis, const char *keys, const Arg *arg) {
1941 if (vis_count_get(vis) != VIS_COUNT_UNKNOWN)
1942 vis_motion(vis, VIS_MOVE_LINE);
1943 else if (arg->i < 0)
1944 vis_motion(vis, VIS_MOVE_FILE_BEGIN);
1945 else
1946 vis_motion(vis, VIS_MOVE_FILE_END);
1947 return keys;
1950 static const char *motiontype(Vis *vis, const char *keys, const Arg *arg) {
1951 vis_motion_type(vis, arg->i);
1952 return keys;
1955 static const char *operator(Vis *vis, const char *keys, const Arg *arg) {
1956 vis_operator(vis, arg->i);
1957 return keys;
1960 static const char *operator_filter(Vis *vis, const char *keys, const Arg *arg) {
1961 vis_operator(vis, VIS_OP_FILTER, arg->s);
1962 return keys;
1965 static const char *movement_key(Vis *vis, const char *keys, const Arg *arg) {
1966 if (!keys[0]) {
1967 vis_keymap_disable(vis);
1968 return NULL;
1971 const char *next = vis_keys_next(vis, keys);
1972 if (!next)
1973 return NULL;
1974 char utf8[UTFmax+1];
1975 if (vis_keys_utf8(vis, keys, utf8))
1976 vis_motion(vis, arg->i, utf8);
1977 return next;
1980 static const char *movement(Vis *vis, const char *keys, const Arg *arg) {
1981 vis_motion(vis, arg->i);
1982 return keys;
1985 static const char *textobj(Vis *vis, const char *keys, const Arg *arg) {
1986 vis_textobject(vis, arg->i);
1987 return keys;
1990 static const char *selection_end(Vis *vis, const char *keys, const Arg *arg) {
1991 for (Selection *s = view_selections(vis_view(vis)); s; s = view_selections_next(s))
1992 view_selections_flip(s);
1993 return keys;
1996 static const char *reg(Vis *vis, const char *keys, const Arg *arg) {
1997 if (!keys[0])
1998 return NULL;
1999 const char *next = vis_keys_next(vis, keys);
2000 if (next - keys > 1)
2001 return next;
2002 enum VisRegister reg = vis_register_from(vis, keys[0]);
2003 vis_register(vis, reg);
2004 return keys+1;
2007 static const char *mark(Vis *vis, const char *keys, const Arg *arg) {
2008 if (!keys[0])
2009 return NULL;
2010 const char *next = vis_keys_next(vis, keys);
2011 if (next - keys > 1)
2012 return next;
2013 enum VisMark mark = vis_mark_from(vis, keys[0]);
2014 vis_mark(vis, mark);
2015 return keys+1;
2018 static const char *undo(Vis *vis, const char *keys, const Arg *arg) {
2019 size_t pos = text_undo(vis_text(vis));
2020 if (pos != EPOS) {
2021 View *view = vis_view(vis);
2022 if (view_selections_count(view) == 1)
2023 view_cursor_to(view, pos);
2024 /* redraw all windows in case some display the same file */
2025 vis_draw(vis);
2027 return keys;
2030 static const char *redo(Vis *vis, const char *keys, const Arg *arg) {
2031 size_t pos = text_redo(vis_text(vis));
2032 if (pos != EPOS) {
2033 View *view = vis_view(vis);
2034 if (view_selections_count(view) == 1)
2035 view_cursor_to(view, pos);
2036 /* redraw all windows in case some display the same file */
2037 vis_draw(vis);
2039 return keys;
2042 static const char *earlier(Vis *vis, const char *keys, const Arg *arg) {
2043 size_t pos = EPOS;
2044 VisCountIterator it = vis_count_iterator_get(vis, 1);
2045 while (vis_count_iterator_next(&it))
2046 pos = text_earlier(vis_text(vis));
2047 if (pos != EPOS) {
2048 view_cursor_to(vis_view(vis), pos);
2049 /* redraw all windows in case some display the same file */
2050 vis_draw(vis);
2052 return keys;
2055 static const char *later(Vis *vis, const char *keys, const Arg *arg) {
2056 size_t pos = EPOS;
2057 VisCountIterator it = vis_count_iterator_get(vis, 1);
2058 while (vis_count_iterator_next(&it))
2059 pos = text_later(vis_text(vis));
2060 if (pos != EPOS) {
2061 view_cursor_to(vis_view(vis), pos);
2062 /* redraw all windows in case some display the same file */
2063 vis_draw(vis);
2065 return keys;
2068 static const char *delete(Vis *vis, const char *keys, const Arg *arg) {
2069 vis_operator(vis, VIS_OP_DELETE);
2070 vis_motion(vis, arg->i);
2071 return keys;
2074 static const char *insert_register(Vis *vis, const char *keys, const Arg *arg) {
2075 if (!keys[0])
2076 return NULL;
2077 const char *next = vis_keys_next(vis, keys);
2078 if (next - keys > 1)
2079 return next;
2080 enum VisRegister reg = vis_register_from(vis, keys[0]);
2081 if (reg != VIS_REG_INVALID) {
2082 vis_register(vis, reg);
2083 vis_operator(vis, VIS_OP_PUT_BEFORE_END);
2085 return keys+1;
2088 static const char *prompt_show(Vis *vis, const char *keys, const Arg *arg) {
2089 vis_prompt_show(vis, arg->s);
2090 return keys;
2093 static const char *insert_verbatim(Vis *vis, const char *keys, const Arg *arg) {
2094 Rune rune = 0;
2095 char buf[4], type = keys[0];
2096 const char *data = NULL;
2097 int len = 0, count = 0, base = 0;
2098 switch (type) {
2099 case '\0':
2100 return NULL;
2101 case 'o':
2102 case 'O':
2103 count = 3;
2104 base = 8;
2105 break;
2106 case 'U':
2107 count = 4;
2108 /* fall through */
2109 case 'u':
2110 count += 4;
2111 base = 16;
2112 break;
2113 case 'x':
2114 case 'X':
2115 count = 2;
2116 base = 16;
2117 break;
2118 default:
2119 if ('0' <= type && type <= '9') {
2120 rune = type - '0';
2121 count = 2;
2122 base = 10;
2124 break;
2127 if (base) {
2128 for (keys++; keys[0] && count > 0; keys++, count--) {
2129 int v = 0;
2130 if (base == 8 && '0' <= keys[0] && keys[0] <= '7') {
2131 v = keys[0] - '0';
2132 } else if ((base == 10 || base == 16) && '0' <= keys[0] && keys[0] <= '9') {
2133 v = keys[0] - '0';
2134 } else if (base == 16 && 'a' <= keys[0] && keys[0] <= 'f') {
2135 v = 10 + keys[0] - 'a';
2136 } else if (base == 16 && 'A' <= keys[0] && keys[0] <= 'F') {
2137 v = 10 + keys[0] - 'A';
2138 } else {
2139 count = 0;
2140 break;
2142 rune = rune * base + v;
2145 if (count > 0)
2146 return NULL;
2147 if (type == 'u' || type == 'U') {
2148 len = runetochar(buf, &rune);
2149 } else {
2150 buf[0] = rune;
2151 len = 1;
2154 data = buf;
2155 } else {
2156 const char *next = vis_keys_next(vis, keys);
2157 if (!next)
2158 return NULL;
2159 if ((rune = vis_keys_codepoint(vis, keys)) != (Rune)-1) {
2160 len = runetochar(buf, &rune);
2161 data = buf;
2162 } else {
2163 vis_info_show(vis, "Unknown key");
2165 keys = next;
2168 if (len > 0)
2169 vis_insert_key(vis, data, len);
2170 return keys;
2173 static const char *wscroll(Vis *vis, const char *keys, const Arg *arg) {
2174 View *view = vis_view(vis);
2175 int count = vis_count_get(vis);
2176 switch (arg->i) {
2177 case -PAGE:
2178 view_scroll_page_up(view);
2179 break;
2180 case +PAGE:
2181 view_scroll_page_down(view);
2182 break;
2183 case -PAGE_HALF:
2184 view_scroll_halfpage_up(view);
2185 break;
2186 case +PAGE_HALF:
2187 view_scroll_halfpage_down(view);
2188 break;
2189 default:
2190 if (count == VIS_COUNT_UNKNOWN)
2191 count = arg->i < 0 ? -arg->i : arg->i;
2192 if (arg->i < 0)
2193 view_scroll_up(view, count);
2194 else
2195 view_scroll_down(view, count);
2196 break;
2198 return keys;
2201 static const char *wslide(Vis *vis, const char *keys, const Arg *arg) {
2202 View *view = vis_view(vis);
2203 int count = vis_count_get(vis);
2204 if (count == VIS_COUNT_UNKNOWN)
2205 count = arg->i < 0 ? -arg->i : arg->i;
2206 if (arg->i >= 0)
2207 view_slide_down(view, count);
2208 else
2209 view_slide_up(view, count);
2210 return keys;
2213 static const char *call(Vis *vis, const char *keys, const Arg *arg) {
2214 arg->f(vis);
2215 return keys;
2218 static const char *window(Vis *vis, const char *keys, const Arg *arg) {
2219 arg->w(vis_view(vis));
2220 return keys;
2223 static const char *openline(Vis *vis, const char *keys, const Arg *arg) {
2224 vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_INSERT);
2225 if (arg->i > 0) {
2226 vis_motion(vis, VIS_MOVE_LINE_END);
2227 vis_keys_feed(vis, "<Enter>");
2228 } else {
2229 if (vis_get_autoindent(vis)) {
2230 vis_motion(vis, VIS_MOVE_LINE_START);
2231 vis_keys_feed(vis, "<vis-motion-line-start>");
2232 } else {
2233 vis_motion(vis, VIS_MOVE_LINE_BEGIN);
2234 vis_keys_feed(vis, "<vis-motion-line-begin>");
2236 vis_keys_feed(vis, "<Enter><Up>");
2238 return keys;
2241 static const char *join(Vis *vis, const char *keys, const Arg *arg) {
2242 bool normal = (vis_mode_get(vis) == VIS_MODE_NORMAL);
2243 vis_operator(vis, VIS_OP_JOIN, arg->s);
2244 if (normal) {
2245 int count = vis_count_get_default(vis, 0);
2246 if (count)
2247 vis_count_set(vis, count-1);
2248 vis_motion(vis, VIS_MOVE_LINE_NEXT);
2250 return keys;
2253 static const char *switchmode(Vis *vis, const char *keys, const Arg *arg) {
2254 vis_mode_switch(vis, arg->i);
2255 return keys;
2258 static const char *insertmode(Vis *vis, const char *keys, const Arg *arg) {
2259 vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_INSERT);
2260 vis_motion(vis, arg->i);
2261 return keys;
2264 static const char *replacemode(Vis *vis, const char *keys, const Arg *arg) {
2265 vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_REPLACE);
2266 vis_motion(vis, arg->i);
2267 return keys;
2270 static const char *unicode_info(Vis *vis, const char *keys, const Arg *arg) {
2271 View *view = vis_view(vis);
2272 Text *txt = vis_text(vis);
2273 size_t start = view_cursor_get(view);
2274 size_t end = text_char_next(txt, start);
2275 char *grapheme = text_bytes_alloc0(txt, start, end-start), *codepoint = grapheme;
2276 if (!grapheme)
2277 return keys;
2278 Buffer info;
2279 buffer_init(&info);
2280 mbstate_t ps = { 0 };
2281 Iterator it = text_iterator_get(txt, start);
2282 for (size_t pos = start; it.pos < end; pos = it.pos) {
2283 if (!text_iterator_codepoint_next(&it, NULL)) {
2284 vis_info_show(vis, "Failed to parse code point");
2285 goto err;
2287 size_t len = it.pos - pos;
2288 wchar_t wc = 0xFFFD;
2289 size_t res = mbrtowc(&wc, codepoint, len, &ps);
2290 bool combining = false;
2291 if (res != (size_t)-1 && res != (size_t)-2)
2292 combining = (wc != L'\0' && wcwidth(wc) == 0);
2293 unsigned char ch = *codepoint;
2294 if (ch < 128 && !isprint(ch))
2295 buffer_appendf(&info, "<^%c> ", ch == 127 ? '?' : ch + 64);
2296 else
2297 buffer_appendf(&info, "<%s%.*s> ", combining ? " " : "", (int)len, codepoint);
2298 if (arg->i == VIS_ACTION_UNICODE_INFO) {
2299 buffer_appendf(&info, "U+%04"PRIX32" ", (uint32_t)wc);
2300 } else {
2301 for (size_t i = 0; i < len; i++)
2302 buffer_appendf(&info, "%02x ", (uint8_t)codepoint[i]);
2304 codepoint += len;
2306 vis_info_show(vis, "%s", buffer_content0(&info));
2307 err:
2308 free(grapheme);
2309 buffer_release(&info);
2310 return keys;
2313 static const char *percent(Vis *vis, const char *keys, const Arg *arg) {
2314 if (vis_count_get(vis) == VIS_COUNT_UNKNOWN)
2315 vis_motion(vis, VIS_MOVE_BRACKET_MATCH);
2316 else
2317 vis_motion(vis, VIS_MOVE_PERCENT);
2318 return keys;
2321 static const char *jumplist(Vis *vis, const char *keys, const Arg *arg) {
2322 if (arg->i < 0)
2323 vis_jumplist_prev(vis);
2324 else if (arg->i > 0)
2325 vis_jumplist_next(vis);
2326 else
2327 vis_jumplist_save(vis);
2328 return keys;
2331 static Vis *vis;
2333 static void signal_handler(int signum, siginfo_t *siginfo, void *context) {
2334 vis_signal_handler(vis, signum, siginfo, context);
2337 int main(int argc, char *argv[]) {
2339 VisEvent event = {
2340 .init = vis_lua_init,
2341 .start = vis_lua_start,
2342 .quit = vis_lua_quit,
2343 .mode_insert_input = vis_lua_mode_insert_input,
2344 .mode_replace_input = vis_lua_mode_replace_input,
2345 .file_open = vis_lua_file_open,
2346 .file_save_pre = vis_lua_file_save_pre,
2347 .file_save_post = vis_lua_file_save_post,
2348 .file_close = vis_lua_file_close,
2349 .win_open = vis_lua_win_open,
2350 .win_close = vis_lua_win_close,
2351 .win_highlight = vis_lua_win_highlight,
2352 .win_status = vis_lua_win_status,
2355 vis = vis_new(ui_term_new(), &event);
2356 if (!vis)
2357 return EXIT_FAILURE;
2359 for (int i = 0; i < LENGTH(vis_action); i++) {
2360 const KeyAction *action = &vis_action[i];
2361 if (!vis_action_register(vis, action))
2362 vis_die(vis, "Could not register action: %s\n", action->name);
2365 for (int i = 0; i < LENGTH(default_bindings); i++) {
2366 for (const KeyBinding **binding = default_bindings[i]; binding && *binding; binding++) {
2367 for (const KeyBinding *kb = *binding; kb->key; kb++) {
2368 vis_mode_map(vis, i, false, kb->key, kb);
2373 for (const char **k = keymaps; k[0]; k += 2)
2374 vis_keymap_add(vis, k[0], k[1]);
2376 /* install signal handlers etc. */
2377 struct sigaction sa;
2378 memset(&sa, 0, sizeof sa);
2379 sigfillset(&sa.sa_mask);
2380 sa.sa_flags = SA_SIGINFO;
2381 sa.sa_sigaction = signal_handler;
2382 if (sigaction(SIGBUS, &sa, NULL) == -1 ||
2383 sigaction(SIGINT, &sa, NULL) == -1 ||
2384 sigaction(SIGCONT, &sa, NULL) == -1 ||
2385 sigaction(SIGWINCH, &sa, NULL) == -1 ||
2386 sigaction(SIGTERM, &sa, NULL) == -1 ||
2387 sigaction(SIGHUP, &sa, NULL) == -1) {
2388 vis_die(vis, "Failed to set signal handler: %s\n", strerror(errno));
2391 sa.sa_handler = SIG_IGN;
2392 if (sigaction(SIGPIPE, &sa, NULL) == -1)
2393 vis_die(vis, "Failed to ignore SIGPIPE\n");
2395 sigset_t blockset;
2396 sigemptyset(&blockset);
2397 sigaddset(&blockset, SIGBUS);
2398 sigaddset(&blockset, SIGCONT);
2399 sigaddset(&blockset, SIGWINCH);
2400 sigaddset(&blockset, SIGTERM);
2401 sigaddset(&blockset, SIGHUP);
2402 if (sigprocmask(SIG_BLOCK, &blockset, NULL) == -1)
2403 vis_die(vis, "Failed to block signals\n");
2405 for (int i = 1; i < argc; i++) {
2406 if (argv[i][0] != '-') {
2407 continue;
2408 } else if (strcmp(argv[i], "-") == 0) {
2409 continue;
2410 } else if (strcmp(argv[i], "--") == 0) {
2411 break;
2412 } else if (strcmp(argv[i], "-v") == 0) {
2413 printf("vis %s%s%s%s%s%s%s\n", VERSION,
2414 CONFIG_CURSES ? " +curses" : "",
2415 CONFIG_LUA ? " +lua" : "",
2416 CONFIG_LPEG ? " +lpeg" : "",
2417 CONFIG_TRE ? " +tre" : "",
2418 CONFIG_ACL ? " +acl" : "",
2419 CONFIG_SELINUX ? " +selinux" : "");
2420 return 0;
2421 } else {
2422 fprintf(stderr, "Unknown command option: %s\n", argv[i]);
2423 return 1;
2427 char *cmd = NULL;
2428 bool end_of_options = false, win_created = false;
2430 for (int i = 1; i < argc; i++) {
2431 if (argv[i][0] == '-' && !end_of_options) {
2432 if (strcmp(argv[i], "-") == 0) {
2433 if (!vis_window_new_fd(vis, STDOUT_FILENO))
2434 vis_die(vis, "Can not create empty buffer\n");
2435 ssize_t len = 0;
2436 char buf[PIPE_BUF];
2437 Text *txt = vis_text(vis);
2438 while ((len = read(STDIN_FILENO, buf, sizeof buf)) > 0)
2439 text_insert(txt, text_size(txt), buf, len);
2440 if (len == -1)
2441 vis_die(vis, "Can not read from stdin\n");
2442 text_snapshot(txt);
2443 int fd = open("/dev/tty", O_RDWR);
2444 if (fd == -1)
2445 vis_die(vis, "Can not reopen stdin\n");
2446 dup2(fd, STDIN_FILENO);
2447 close(fd);
2448 } else if (strcmp(argv[i], "--") == 0) {
2449 end_of_options = true;
2450 continue;
2452 } else if (argv[i][0] == '+' && !end_of_options) {
2453 cmd = argv[i] + (argv[i][1] == '/' || argv[i][1] == '?');
2454 continue;
2455 } else if (!vis_window_new(vis, argv[i])) {
2456 vis_die(vis, "Can not load `%s': %s\n", argv[i], strerror(errno));
2458 win_created = true;
2459 if (cmd) {
2460 vis_prompt_cmd(vis, cmd);
2461 cmd = NULL;
2465 if (!vis_window(vis) && !win_created) {
2466 if (!vis_window_new(vis, NULL))
2467 vis_die(vis, "Can not create empty buffer\n");
2468 if (cmd)
2469 vis_prompt_cmd(vis, cmd);
2472 int status = vis_run(vis);
2473 vis_free(vis);
2474 return status;