vis: rename uses of Cursor to Selection
[vis.git] / main.c
blob3f73b6ea65d345f41ca870dc7afa9074ba2e6869
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 /* set mark indicated by keys to current cursor position */
42 static const char *mark_set(Vis*, const char *keys, const Arg *arg);
43 /* add a new line either before or after the one where the cursor currently is */
44 static const char *openline(Vis*, const char *keys, const Arg *arg);
45 /* join lines from current cursor position to movement indicated by arg */
46 static const char *join(Vis*, const char *keys, const Arg *arg);
47 /* perform last action i.e. action_prev again */
48 static const char *repeat(Vis*, const char *keys, const Arg *arg);
49 /* replace character at cursor with one from keys */
50 static const char *replace(Vis*, const char *keys, const Arg *arg);
51 /* create a new cursor on the previous (arg->i < 0) or next (arg->i > 0) line */
52 static const char *cursors_new(Vis*, const char *keys, const Arg *arg);
53 /* try to align all cursors on the same column */
54 static const char *cursors_align(Vis*, const char *keys, const Arg *arg);
55 /* try to align all cursors by inserting the correct amount of white spaces */
56 static const char *cursors_align_indent(Vis*, const char *keys, const Arg *arg);
57 /* remove all but the primary cursor and their selections */
58 static const char *cursors_clear(Vis*, const char *keys, const Arg *arg);
59 /* remove the least recently added cursor */
60 static const char *cursors_remove(Vis*, const char *keys, const Arg *arg);
61 /* remove count (or arg->i)-th cursor column */
62 static const char *cursors_remove_column(Vis*, const char *keys, const Arg *arg);
63 /* remove all but the count (or arg->i)-th cursor column */
64 static const char *cursors_remove_column_except(Vis*, const char *keys, const Arg *arg);
65 /* move to the previous (arg->i < 0) or next (arg->i > 0) cursor */
66 static const char *cursors_navigate(Vis*, const char *keys, const Arg *arg);
67 /* select the word the cursor is currently over */
68 static const char *cursors_select(Vis*, const char *keys, const Arg *arg);
69 /* select the next region matching the current selection */
70 static const char *cursors_select_next(Vis*, const char *keys, const Arg *arg);
71 /* clear current selection but select next match */
72 static const char *cursors_select_skip(Vis*, const char *keys, const Arg *arg);
73 /* rotate selection content count times left (arg->i < 0) or right (arg->i > 0) */
74 static const char *selections_rotate(Vis*, const char *keys, const Arg *arg);
75 /* remove leading and trailing white spaces from selections */
76 static const char *selections_trim(Vis*, const char *keys, const Arg *arg);
77 /* adjust current used count according to keys */
78 static const char *count(Vis*, const char *keys, const Arg *arg);
79 /* move to the count-th line or if not given either to the first (arg->i < 0)
80 * or last (arg->i > 0) line of file */
81 static const char *gotoline(Vis*, const char *keys, const Arg *arg);
82 /* set motion type either LINEWISE or CHARWISE via arg->i */
83 static const char *motiontype(Vis*, const char *keys, const Arg *arg);
84 /* make the current action use the operator indicated by arg->i */
85 static const char *operator(Vis*, const char *keys, const Arg *arg);
86 /* use arg->s as command for the filter operator */
87 static const char *operator_filter(Vis*, const char *keys, const Arg *arg);
88 /* blocks to read a key and performs movement indicated by arg->i which
89 * should be one of VIS_MOVE_{RIGHT,LEFT}_{TO,TILL} */
90 static const char *movement_key(Vis*, const char *keys, const Arg *arg);
91 /* perform the movement as indicated by arg->i */
92 static const char *movement(Vis*, const char *keys, const Arg *arg);
93 /* let the current operator affect the range indicated by the text object arg->i */
94 static const char *textobj(Vis*, const char *keys, const Arg *arg);
95 /* move to the other end of selected text */
96 static const char *selection_end(Vis*, const char *keys, const Arg *arg);
97 /* restore least recently used selection */
98 static const char *selection_restore(Vis*, const char *keys, const Arg *arg);
99 /* use register indicated by keys for the current operator */
100 static const char *reg(Vis*, const char *keys, const Arg *arg);
101 /* perform arg->i motion with a mark indicated by keys as argument */
102 static const char *mark_motion(Vis*, const char *keys, const Arg *arg);
103 /* {un,re}do last action, redraw window */
104 static const char *undo(Vis*, const char *keys, const Arg *arg);
105 static const char *redo(Vis*, const char *keys, const Arg *arg);
106 /* earlier, later action chronologically, redraw window */
107 static const char *earlier(Vis*, const char *keys, const Arg *arg);
108 static const char *later(Vis*, const char *keys, const Arg *arg);
109 /* delete from the current cursor position to the end of
110 * movement as indicated by arg->i */
111 static const char *delete(Vis*, const char *keys, const Arg *arg);
112 /* insert register content indicated by keys at current cursor position */
113 static const char *insert_register(Vis*, const char *keys, const Arg *arg);
114 /* show a user prompt to get input with title arg->s */
115 static const char *prompt_show(Vis*, const char *keys, const Arg *arg);
116 /* blocks to read 3 consecutive digits and inserts the corresponding byte value */
117 static const char *insert_verbatim(Vis*, const char *keys, const Arg *arg);
118 /* scroll window content according to arg->i which can be either PAGE, PAGE_HALF,
119 * or an arbitrary number of lines. a multiplier overrides what is given in arg->i.
120 * negative values scroll back, positive forward. */
121 static const char *wscroll(Vis*, const char *keys, const Arg *arg);
122 /* similar to scroll, but do only move window content not cursor position */
123 static const char *wslide(Vis*, const char *keys, const Arg *arg);
124 /* call editor function as indicated by arg->f */
125 static const char *call(Vis*, const char *keys, const Arg *arg);
126 /* call window function as indicated by arg->w */
127 static const char *window(Vis*, const char *keys, const Arg *arg);
128 /* show info about Unicode character at cursor position */
129 static const char *unicode_info(Vis*, const char *keys, const Arg *arg);
130 /* either go to count % of ile or to matching item */
131 static const char *percent(Vis*, const char *keys, const Arg *arg);
133 enum {
134 VIS_ACTION_EDITOR_SUSPEND,
135 VIS_ACTION_CURSOR_CHAR_PREV,
136 VIS_ACTION_CURSOR_CHAR_NEXT,
137 VIS_ACTION_CURSOR_LINE_CHAR_PREV,
138 VIS_ACTION_CURSOR_LINE_CHAR_NEXT,
139 VIS_ACTION_CURSOR_CODEPOINT_PREV,
140 VIS_ACTION_CURSOR_CODEPOINT_NEXT,
141 VIS_ACTION_CURSOR_WORD_START_PREV,
142 VIS_ACTION_CURSOR_WORD_START_NEXT,
143 VIS_ACTION_CURSOR_WORD_END_PREV,
144 VIS_ACTION_CURSOR_WORD_END_NEXT,
145 VIS_ACTION_CURSOR_LONGWORD_START_PREV,
146 VIS_ACTION_CURSOR_LONGWORD_START_NEXT,
147 VIS_ACTION_CURSOR_LONGWORD_END_PREV,
148 VIS_ACTION_CURSOR_LONGWORD_END_NEXT,
149 VIS_ACTION_CURSOR_LINE_UP,
150 VIS_ACTION_CURSOR_LINE_DOWN,
151 VIS_ACTION_CURSOR_LINE_START,
152 VIS_ACTION_CURSOR_LINE_FINISH,
153 VIS_ACTION_CURSOR_LINE_BEGIN,
154 VIS_ACTION_CURSOR_LINE_END,
155 VIS_ACTION_CURSOR_SCREEN_LINE_UP,
156 VIS_ACTION_CURSOR_SCREEN_LINE_DOWN,
157 VIS_ACTION_CURSOR_SCREEN_LINE_BEGIN,
158 VIS_ACTION_CURSOR_SCREEN_LINE_MIDDLE,
159 VIS_ACTION_CURSOR_SCREEN_LINE_END,
160 VIS_ACTION_CURSOR_PERCENT,
161 VIS_ACTION_CURSOR_BYTE,
162 VIS_ACTION_CURSOR_BYTE_LEFT,
163 VIS_ACTION_CURSOR_BYTE_RIGHT,
164 VIS_ACTION_CURSOR_PARAGRAPH_PREV,
165 VIS_ACTION_CURSOR_PARAGRAPH_NEXT,
166 VIS_ACTION_CURSOR_SENTENCE_PREV,
167 VIS_ACTION_CURSOR_SENTENCE_NEXT,
168 VIS_ACTION_CURSOR_BLOCK_START,
169 VIS_ACTION_CURSOR_BLOCK_END,
170 VIS_ACTION_CURSOR_PARENTHESE_START,
171 VIS_ACTION_CURSOR_PARENTHESE_END,
172 VIS_ACTION_CURSOR_COLUMN,
173 VIS_ACTION_CURSOR_LINE_FIRST,
174 VIS_ACTION_CURSOR_LINE_LAST,
175 VIS_ACTION_CURSOR_WINDOW_LINE_TOP,
176 VIS_ACTION_CURSOR_WINDOW_LINE_MIDDLE,
177 VIS_ACTION_CURSOR_WINDOW_LINE_BOTTOM,
178 VIS_ACTION_CURSOR_SEARCH_REPEAT_FORWARD,
179 VIS_ACTION_CURSOR_SEARCH_REPEAT_BACKWARD,
180 VIS_ACTION_CURSOR_SEARCH_REPEAT,
181 VIS_ACTION_CURSOR_SEARCH_REPEAT_REVERSE,
182 VIS_ACTION_CURSOR_SEARCH_WORD_FORWARD,
183 VIS_ACTION_CURSOR_SEARCH_WORD_BACKWARD,
184 VIS_ACTION_WINDOW_PAGE_UP,
185 VIS_ACTION_WINDOW_PAGE_DOWN,
186 VIS_ACTION_WINDOW_HALFPAGE_UP,
187 VIS_ACTION_WINDOW_HALFPAGE_DOWN,
188 VIS_ACTION_MODE_NORMAL,
189 VIS_ACTION_MODE_VISUAL,
190 VIS_ACTION_MODE_VISUAL_LINE,
191 VIS_ACTION_MODE_INSERT,
192 VIS_ACTION_MODE_REPLACE,
193 VIS_ACTION_DELETE_CHAR_PREV,
194 VIS_ACTION_DELETE_CHAR_NEXT,
195 VIS_ACTION_DELETE_LINE_BEGIN,
196 VIS_ACTION_DELETE_WORD_PREV,
197 VIS_ACTION_JUMPLIST_PREV,
198 VIS_ACTION_JUMPLIST_NEXT,
199 VIS_ACTION_CHANGELIST_PREV,
200 VIS_ACTION_CHANGELIST_NEXT,
201 VIS_ACTION_UNDO,
202 VIS_ACTION_REDO,
203 VIS_ACTION_EARLIER,
204 VIS_ACTION_LATER,
205 VIS_ACTION_MACRO_RECORD,
206 VIS_ACTION_MACRO_REPLAY,
207 VIS_ACTION_MARK_SET,
208 VIS_ACTION_MARK_GOTO,
209 VIS_ACTION_MARK_GOTO_LINE,
210 VIS_ACTION_REDRAW,
211 VIS_ACTION_REPLACE_CHAR,
212 VIS_ACTION_TOTILL_REPEAT,
213 VIS_ACTION_TOTILL_REVERSE,
214 VIS_ACTION_PROMPT_SEARCH_FORWARD,
215 VIS_ACTION_PROMPT_SEARCH_BACKWARD,
216 VIS_ACTION_TILL_LEFT,
217 VIS_ACTION_TILL_RIGHT,
218 VIS_ACTION_TO_LEFT,
219 VIS_ACTION_TO_RIGHT,
220 VIS_ACTION_REGISTER,
221 VIS_ACTION_OPERATOR_CHANGE,
222 VIS_ACTION_OPERATOR_DELETE,
223 VIS_ACTION_OPERATOR_YANK,
224 VIS_ACTION_OPERATOR_SHIFT_LEFT,
225 VIS_ACTION_OPERATOR_SHIFT_RIGHT,
226 VIS_ACTION_OPERATOR_CASE_LOWER,
227 VIS_ACTION_OPERATOR_CASE_UPPER,
228 VIS_ACTION_OPERATOR_CASE_SWAP,
229 VIS_ACTION_OPERATOR_FILTER,
230 VIS_ACTION_OPERATOR_FILTER_FMT,
231 VIS_ACTION_COUNT,
232 VIS_ACTION_INSERT_NEWLINE,
233 VIS_ACTION_INSERT_TAB,
234 VIS_ACTION_INSERT_VERBATIM,
235 VIS_ACTION_INSERT_REGISTER,
236 VIS_ACTION_WINDOW_NEXT,
237 VIS_ACTION_WINDOW_PREV,
238 VIS_ACTION_APPEND_CHAR_NEXT,
239 VIS_ACTION_APPEND_LINE_END,
240 VIS_ACTION_INSERT_LINE_START,
241 VIS_ACTION_OPEN_LINE_ABOVE,
242 VIS_ACTION_OPEN_LINE_BELOW,
243 VIS_ACTION_JOIN_LINES,
244 VIS_ACTION_JOIN_LINES_TRIM,
245 VIS_ACTION_PROMPT_SHOW,
246 VIS_ACTION_REPEAT,
247 VIS_ACTION_SELECTION_FLIP,
248 VIS_ACTION_SELECTION_RESTORE,
249 VIS_ACTION_WINDOW_REDRAW_TOP,
250 VIS_ACTION_WINDOW_REDRAW_CENTER,
251 VIS_ACTION_WINDOW_REDRAW_BOTTOM,
252 VIS_ACTION_WINDOW_SLIDE_UP,
253 VIS_ACTION_WINDOW_SLIDE_DOWN,
254 VIS_ACTION_PUT_AFTER,
255 VIS_ACTION_PUT_BEFORE,
256 VIS_ACTION_PUT_AFTER_END,
257 VIS_ACTION_PUT_BEFORE_END,
258 VIS_ACTION_CURSOR_SELECT_WORD,
259 VIS_ACTION_CURSORS_NEW_LINE_ABOVE,
260 VIS_ACTION_CURSORS_NEW_LINE_ABOVE_FIRST,
261 VIS_ACTION_CURSORS_NEW_LINE_BELOW,
262 VIS_ACTION_CURSORS_NEW_LINE_BELOW_LAST,
263 VIS_ACTION_CURSORS_NEW_LINES_BEGIN,
264 VIS_ACTION_CURSORS_NEW_LINES_END,
265 VIS_ACTION_CURSORS_NEW_MATCH_NEXT,
266 VIS_ACTION_CURSORS_NEW_MATCH_SKIP,
267 VIS_ACTION_CURSORS_ALIGN,
268 VIS_ACTION_CURSORS_ALIGN_INDENT_LEFT,
269 VIS_ACTION_CURSORS_ALIGN_INDENT_RIGHT,
270 VIS_ACTION_CURSORS_REMOVE_ALL,
271 VIS_ACTION_CURSORS_REMOVE_LAST,
272 VIS_ACTION_CURSORS_REMOVE_COLUMN,
273 VIS_ACTION_CURSORS_REMOVE_COLUMN_EXCEPT,
274 VIS_ACTION_CURSORS_PREV,
275 VIS_ACTION_CURSORS_NEXT,
276 VIS_ACTION_SELECTIONS_ROTATE_LEFT,
277 VIS_ACTION_SELECTIONS_ROTATE_RIGHT,
278 VIS_ACTION_SELECTIONS_TRIM,
279 VIS_ACTION_TEXT_OBJECT_WORD_OUTER,
280 VIS_ACTION_TEXT_OBJECT_WORD_INNER,
281 VIS_ACTION_TEXT_OBJECT_LONGWORD_OUTER,
282 VIS_ACTION_TEXT_OBJECT_LONGWORD_INNER,
283 VIS_ACTION_TEXT_OBJECT_SENTENCE,
284 VIS_ACTION_TEXT_OBJECT_PARAGRAPH,
285 VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_OUTER,
286 VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_INNER,
287 VIS_ACTION_TEXT_OBJECT_PARANTHESE_OUTER,
288 VIS_ACTION_TEXT_OBJECT_PARANTHESE_INNER,
289 VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_OUTER,
290 VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_INNER,
291 VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_OUTER,
292 VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_INNER,
293 VIS_ACTION_TEXT_OBJECT_QUOTE_OUTER,
294 VIS_ACTION_TEXT_OBJECT_QUOTE_INNER,
295 VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_OUTER,
296 VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_INNER,
297 VIS_ACTION_TEXT_OBJECT_BACKTICK_OUTER,
298 VIS_ACTION_TEXT_OBJECT_BACKTICK_INNER,
299 VIS_ACTION_TEXT_OBJECT_ENTIRE_OUTER,
300 VIS_ACTION_TEXT_OBJECT_ENTIRE_INNER,
301 VIS_ACTION_TEXT_OBJECT_LINE_OUTER,
302 VIS_ACTION_TEXT_OBJECT_LINE_INNER,
303 VIS_ACTION_TEXT_OBJECT_INDENTATION,
304 VIS_ACTION_TEXT_OBJECT_SEARCH_FORWARD,
305 VIS_ACTION_TEXT_OBJECT_SEARCH_BACKWARD,
306 VIS_ACTION_MOTION_CHARWISE,
307 VIS_ACTION_MOTION_LINEWISE,
308 VIS_ACTION_UNICODE_INFO,
309 VIS_ACTION_UTF8_INFO,
310 VIS_ACTION_NOP,
313 static const KeyAction vis_action[] = {
314 [VIS_ACTION_EDITOR_SUSPEND] = {
315 "vis-suspend",
316 VIS_HELP("Suspend the editor")
317 suspend,
319 [VIS_ACTION_CURSOR_CHAR_PREV] = {
320 "vis-motion-char-prev",
321 VIS_HELP("Move cursor left, to the previous character")
322 movement, { .i = VIS_MOVE_CHAR_PREV }
324 [VIS_ACTION_CURSOR_CHAR_NEXT] = {
325 "vis-motion-char-next",
326 VIS_HELP("Move cursor right, to the next character")
327 movement, { .i = VIS_MOVE_CHAR_NEXT }
329 [VIS_ACTION_CURSOR_LINE_CHAR_PREV] = {
330 "vis-motion-line-char-prev",
331 VIS_HELP("Move cursor left, to the previous character on the same line")
332 movement, { .i = VIS_MOVE_LINE_CHAR_PREV }
334 [VIS_ACTION_CURSOR_LINE_CHAR_NEXT] = {
335 "vis-motion-line-char-next",
336 VIS_HELP("Move cursor right, to the next character on the same line")
337 movement, { .i = VIS_MOVE_LINE_CHAR_NEXT }
339 [VIS_ACTION_CURSOR_CODEPOINT_PREV] = {
340 "vis-motion-codepoint-prev",
341 VIS_HELP("Move to the previous Unicode codepoint")
342 movement, { .i = VIS_MOVE_CODEPOINT_PREV }
344 [VIS_ACTION_CURSOR_CODEPOINT_NEXT] = {
345 "vis-motion-codepoint-next",
346 VIS_HELP("Move to the next Unicode codepoint")
347 movement, { .i = VIS_MOVE_CODEPOINT_NEXT }
349 [VIS_ACTION_CURSOR_WORD_START_PREV] = {
350 "vis-motion-word-start-prev",
351 VIS_HELP("Move cursor words backwards")
352 movement, { .i = VIS_MOVE_WORD_START_PREV }
354 [VIS_ACTION_CURSOR_WORD_START_NEXT] = {
355 "vis-motion-word-start-next",
356 VIS_HELP("Move cursor words forwards")
357 movement, { .i = VIS_MOVE_WORD_START_NEXT }
359 [VIS_ACTION_CURSOR_WORD_END_PREV] = {
360 "vis-motion-word-end-prev",
361 VIS_HELP("Move cursor backwards to the end of word")
362 movement, { .i = VIS_MOVE_WORD_END_PREV }
364 [VIS_ACTION_CURSOR_WORD_END_NEXT] = {
365 "vis-motion-word-end-next",
366 VIS_HELP("Move cursor forward to the end of word")
367 movement, { .i = VIS_MOVE_WORD_END_NEXT }
369 [VIS_ACTION_CURSOR_LONGWORD_START_PREV] = {
370 "vis-motion-bigword-start-prev",
371 VIS_HELP("Move cursor WORDS backwards")
372 movement, { .i = VIS_MOVE_LONGWORD_START_PREV }
374 [VIS_ACTION_CURSOR_LONGWORD_START_NEXT] = {
375 "vis-motion-bigword-start-next",
376 VIS_HELP("Move cursor WORDS forwards")
377 movement, { .i = VIS_MOVE_LONGWORD_START_NEXT }
379 [VIS_ACTION_CURSOR_LONGWORD_END_PREV] = {
380 "vis-motion-bigword-end-prev",
381 VIS_HELP("Move cursor backwards to the end of WORD")
382 movement, { .i = VIS_MOVE_LONGWORD_END_PREV }
384 [VIS_ACTION_CURSOR_LONGWORD_END_NEXT] = {
385 "vis-motion-bigword-end-next",
386 VIS_HELP("Move cursor forward to the end of WORD")
387 movement, { .i = VIS_MOVE_LONGWORD_END_NEXT }
389 [VIS_ACTION_CURSOR_LINE_UP] = {
390 "vis-motion-line-up",
391 VIS_HELP("Move cursor line upwards")
392 movement, { .i = VIS_MOVE_LINE_UP }
394 [VIS_ACTION_CURSOR_LINE_DOWN] = {
395 "vis-motion-line-down",
396 VIS_HELP("Move cursor line downwards")
397 movement, { .i = VIS_MOVE_LINE_DOWN }
399 [VIS_ACTION_CURSOR_LINE_START] = {
400 "vis-motion-line-start",
401 VIS_HELP("Move cursor to first non-blank character of the line")
402 movement, { .i = VIS_MOVE_LINE_START }
404 [VIS_ACTION_CURSOR_LINE_FINISH] = {
405 "vis-motion-line-finish",
406 VIS_HELP("Move cursor to last non-blank character of the line")
407 movement, { .i = VIS_MOVE_LINE_FINISH }
409 [VIS_ACTION_CURSOR_LINE_BEGIN] = {
410 "vis-motion-line-begin",
411 VIS_HELP("Move cursor to first character of the line")
412 movement, { .i = VIS_MOVE_LINE_BEGIN }
414 [VIS_ACTION_CURSOR_LINE_END] = {
415 "vis-motion-line-end",
416 VIS_HELP("Move cursor to end of the line")
417 movement, { .i = VIS_MOVE_LINE_END }
419 [VIS_ACTION_CURSOR_SCREEN_LINE_UP] = {
420 "vis-motion-screenline-up",
421 VIS_HELP("Move cursor screen/display line upwards")
422 movement, { .i = VIS_MOVE_SCREEN_LINE_UP }
424 [VIS_ACTION_CURSOR_SCREEN_LINE_DOWN] = {
425 "vis-motion-screenline-down",
426 VIS_HELP("Move cursor screen/display line downwards")
427 movement, { .i = VIS_MOVE_SCREEN_LINE_DOWN }
429 [VIS_ACTION_CURSOR_SCREEN_LINE_BEGIN] = {
430 "vis-motion-screenline-begin",
431 VIS_HELP("Move cursor to beginning of screen/display line")
432 movement, { .i = VIS_MOVE_SCREEN_LINE_BEGIN }
434 [VIS_ACTION_CURSOR_SCREEN_LINE_MIDDLE] = {
435 "vis-motion-screenline-middle",
436 VIS_HELP("Move cursor to middle of screen/display line")
437 movement, { .i = VIS_MOVE_SCREEN_LINE_MIDDLE }
439 [VIS_ACTION_CURSOR_SCREEN_LINE_END] = {
440 "vis-motion-screenline-end",
441 VIS_HELP("Move cursor to end of screen/display line")
442 movement, { .i = VIS_MOVE_SCREEN_LINE_END }
444 [VIS_ACTION_CURSOR_PERCENT] = {
445 "vis-motion-percent",
446 VIS_HELP("Move to count % of file or matching item")
447 percent
449 [VIS_ACTION_CURSOR_BYTE] = {
450 "vis-motion-byte",
451 VIS_HELP("Move to absolute byte position")
452 movement, { .i = VIS_MOVE_BYTE }
454 [VIS_ACTION_CURSOR_BYTE_LEFT] = {
455 "vis-motion-byte-left",
456 VIS_HELP("Move count bytes to the left")
457 movement, { .i = VIS_MOVE_BYTE_LEFT }
459 [VIS_ACTION_CURSOR_BYTE_RIGHT] = {
460 "vis-motion-byte-right",
461 VIS_HELP("Move count bytes to the right")
462 movement, { .i = VIS_MOVE_BYTE_RIGHT }
464 [VIS_ACTION_CURSOR_PARAGRAPH_PREV] = {
465 "vis-motion-paragraph-prev",
466 VIS_HELP("Move cursor paragraph backward")
467 movement, { .i = VIS_MOVE_PARAGRAPH_PREV }
469 [VIS_ACTION_CURSOR_PARAGRAPH_NEXT] = {
470 "vis-motion-paragraph-next",
471 VIS_HELP("Move cursor paragraph forward")
472 movement, { .i = VIS_MOVE_PARAGRAPH_NEXT }
474 [VIS_ACTION_CURSOR_SENTENCE_PREV] = {
475 "vis-motion-sentence-prev",
476 VIS_HELP("Move cursor sentence backward")
477 movement, { .i = VIS_MOVE_SENTENCE_PREV }
479 [VIS_ACTION_CURSOR_SENTENCE_NEXT] = {
480 "vis-motion-sentence-next",
481 VIS_HELP("Move cursor sentence forward")
482 movement, { .i = VIS_MOVE_SENTENCE_NEXT }
484 [VIS_ACTION_CURSOR_BLOCK_START] = {
485 "vis-motion-block-start",
486 VIS_HELP("Move cursor to the opening curly brace in a block")
487 movement, { .i = VIS_MOVE_BLOCK_START }
489 [VIS_ACTION_CURSOR_BLOCK_END] = {
490 "vis-motion-block-end",
491 VIS_HELP("Move cursor to the closing curly brace in a block")
492 movement, { .i = VIS_MOVE_BLOCK_END }
494 [VIS_ACTION_CURSOR_PARENTHESE_START] = {
495 "vis-motion-parenthese-start",
496 VIS_HELP("Move cursor to the opening parenthese inside a pair of parentheses")
497 movement, { .i = VIS_MOVE_PARENTHESE_START }
499 [VIS_ACTION_CURSOR_PARENTHESE_END] = {
500 "vis-motion-parenthese-end",
501 VIS_HELP("Move cursor to the closing parenthese inside a pair of parentheses")
502 movement, { .i = VIS_MOVE_PARENTHESE_END }
504 [VIS_ACTION_CURSOR_COLUMN] = {
505 "vis-motion-column",
506 VIS_HELP("Move cursor to given column of current line")
507 movement, { .i = VIS_MOVE_COLUMN }
509 [VIS_ACTION_CURSOR_LINE_FIRST] = {
510 "vis-motion-line-first",
511 VIS_HELP("Move cursor to given line (defaults to first)")
512 gotoline, { .i = -1 }
514 [VIS_ACTION_CURSOR_LINE_LAST] = {
515 "vis-motion-line-last",
516 VIS_HELP("Move cursor to given line (defaults to last)")
517 gotoline, { .i = +1 }
519 [VIS_ACTION_CURSOR_WINDOW_LINE_TOP] = {
520 "vis-motion-window-line-top",
521 VIS_HELP("Move cursor to top line of the window")
522 movement, { .i = VIS_MOVE_WINDOW_LINE_TOP }
524 [VIS_ACTION_CURSOR_WINDOW_LINE_MIDDLE] = {
525 "vis-motion-window-line-middle",
526 VIS_HELP("Move cursor to middle line of the window")
527 movement, { .i = VIS_MOVE_WINDOW_LINE_MIDDLE }
529 [VIS_ACTION_CURSOR_WINDOW_LINE_BOTTOM] = {
530 "vis-motion-window-line-bottom",
531 VIS_HELP("Move cursor to bottom line of the window")
532 movement, { .i = VIS_MOVE_WINDOW_LINE_BOTTOM }
534 [VIS_ACTION_CURSOR_SEARCH_REPEAT_FORWARD] = {
535 "vis-motion-search-repeat-forward",
536 VIS_HELP("Move cursor to next match in forward direction")
537 movement, { .i = VIS_MOVE_SEARCH_REPEAT_FORWARD }
539 [VIS_ACTION_CURSOR_SEARCH_REPEAT_BACKWARD] = {
540 "vis-motion-search-repeat-backward",
541 VIS_HELP("Move cursor to previous match in backward direction")
542 movement, { .i = VIS_MOVE_SEARCH_REPEAT_BACKWARD }
544 [VIS_ACTION_CURSOR_SEARCH_REPEAT] = {
545 "vis-motion-search-repeat",
546 VIS_HELP("Move cursor to next match")
547 movement, { .i = VIS_MOVE_SEARCH_REPEAT }
549 [VIS_ACTION_CURSOR_SEARCH_REPEAT_REVERSE] = {
550 "vis-motion-search-repeat-reverse",
551 VIS_HELP("Move cursor to next match in opposite direction")
552 movement, { .i = VIS_MOVE_SEARCH_REPEAT_REVERSE }
554 [VIS_ACTION_CURSOR_SEARCH_WORD_FORWARD] = {
555 "vis-motion-search-word-forward",
556 VIS_HELP("Move cursor to next occurrence of the word under cursor")
557 movement, { .i = VIS_MOVE_SEARCH_WORD_FORWARD }
559 [VIS_ACTION_CURSOR_SEARCH_WORD_BACKWARD] = {
560 "vis-motion-search-word-backward",
561 VIS_HELP("Move cursor to previous occurrence of the word under cursor")
562 movement, { .i = VIS_MOVE_SEARCH_WORD_BACKWARD }
564 [VIS_ACTION_WINDOW_PAGE_UP] = {
565 "vis-window-page-up",
566 VIS_HELP("Scroll window pages backwards (upwards)")
567 wscroll, { .i = -PAGE }
569 [VIS_ACTION_WINDOW_HALFPAGE_UP] = {
570 "vis-window-halfpage-up",
571 VIS_HELP("Scroll window half pages backwards (upwards)")
572 wscroll, { .i = -PAGE_HALF }
574 [VIS_ACTION_WINDOW_PAGE_DOWN] = {
575 "vis-window-page-down",
576 VIS_HELP("Scroll window pages forwards (downwards)")
577 wscroll, { .i = +PAGE }
579 [VIS_ACTION_WINDOW_HALFPAGE_DOWN] = {
580 "vis-window-halfpage-down",
581 VIS_HELP("Scroll window half pages forwards (downwards)")
582 wscroll, { .i = +PAGE_HALF }
584 [VIS_ACTION_MODE_NORMAL] = {
585 "vis-mode-normal",
586 VIS_HELP("Enter normal mode")
587 switchmode, { .i = VIS_MODE_NORMAL }
589 [VIS_ACTION_MODE_VISUAL] = {
590 "vis-mode-visual-charwise",
591 VIS_HELP("Enter characterwise visual mode")
592 switchmode, { .i = VIS_MODE_VISUAL }
594 [VIS_ACTION_MODE_VISUAL_LINE] = {
595 "vis-mode-visual-linewise",
596 VIS_HELP("Enter linewise visual mode")
597 switchmode, { .i = VIS_MODE_VISUAL_LINE }
599 [VIS_ACTION_MODE_INSERT] = {
600 "vis-mode-insert",
601 VIS_HELP("Enter insert mode")
602 insertmode, { .i = VIS_MOVE_NOP }
604 [VIS_ACTION_MODE_REPLACE] = {
605 "vis-mode-replace",
606 VIS_HELP("Enter replace mode")
607 replacemode, { .i = VIS_MOVE_NOP }
609 [VIS_ACTION_DELETE_CHAR_PREV] = {
610 "vis-delete-char-prev",
611 VIS_HELP("Delete the previous character")
612 delete, { .i = VIS_MOVE_CHAR_PREV }
614 [VIS_ACTION_DELETE_CHAR_NEXT] = {
615 "vis-delete-char-next",
616 VIS_HELP("Delete the next character")
617 delete, { .i = VIS_MOVE_CHAR_NEXT }
619 [VIS_ACTION_DELETE_LINE_BEGIN] = {
620 "vis-delete-line-begin",
621 VIS_HELP("Delete until the start of the current line")
622 delete, { .i = VIS_MOVE_LINE_BEGIN }
624 [VIS_ACTION_DELETE_WORD_PREV] = {
625 "vis-delete-word-prev",
626 VIS_HELP("Delete the previous WORD")
627 delete, { .i = VIS_MOVE_WORD_START_PREV }
629 [VIS_ACTION_JUMPLIST_PREV] = {
630 "vis-jumplist-prev",
631 VIS_HELP("Go to older cursor position in jump list")
632 movement, { .i = VIS_MOVE_JUMPLIST_PREV }
634 [VIS_ACTION_JUMPLIST_NEXT] = {
635 "vis-jumplist-next",
636 VIS_HELP("Go to newer cursor position in jump list")
637 movement, { .i = VIS_MOVE_JUMPLIST_NEXT }
639 [VIS_ACTION_CHANGELIST_PREV] = {
640 "vis-changelist-prev",
641 VIS_HELP("Go to older cursor position in change list")
642 movement, { .i = VIS_MOVE_CHANGELIST_PREV }
644 [VIS_ACTION_CHANGELIST_NEXT] = {
645 "vis-changelist-next",
646 VIS_HELP("Go to newer cursor position in change list")
647 movement, { .i = VIS_MOVE_CHANGELIST_NEXT }
649 [VIS_ACTION_UNDO] = {
650 "vis-undo",
651 VIS_HELP("Undo last change")
652 undo,
654 [VIS_ACTION_REDO] = {
655 "vis-redo",
656 VIS_HELP("Redo last change")
657 redo,
659 [VIS_ACTION_EARLIER] = {
660 "vis-earlier",
661 VIS_HELP("Goto older text state")
662 earlier,
664 [VIS_ACTION_LATER] = {
665 "vis-later",
666 VIS_HELP("Goto newer text state")
667 later,
669 [VIS_ACTION_MACRO_RECORD] = {
670 "vis-macro-record",
671 VIS_HELP("Record macro into given register")
672 macro_record,
674 [VIS_ACTION_MACRO_REPLAY] = {
675 "vis-macro-replay",
676 VIS_HELP("Replay macro, execute the content of the given register")
677 macro_replay,
679 [VIS_ACTION_MARK_SET] = {
680 "vis-mark-set",
681 VIS_HELP("Set given mark at current cursor position")
682 mark_set,
684 [VIS_ACTION_MARK_GOTO] = {
685 "vis-mark-goto",
686 VIS_HELP("Goto the position of the given mark")
687 mark_motion, { .i = VIS_MOVE_MARK }
689 [VIS_ACTION_MARK_GOTO_LINE] = {
690 "vis-mark-goto-line",
691 VIS_HELP("Goto first non-blank character of the line containing the given mark")
692 mark_motion, { .i = VIS_MOVE_MARK_LINE }
694 [VIS_ACTION_REDRAW] = {
695 "vis-redraw",
696 VIS_HELP("Redraw current editor content")
697 call, { .f = vis_redraw }
699 [VIS_ACTION_REPLACE_CHAR] = {
700 "vis-replace-char",
701 VIS_HELP("Replace the character under the cursor")
702 replace,
704 [VIS_ACTION_TOTILL_REPEAT] = {
705 "vis-motion-totill-repeat",
706 VIS_HELP("Repeat latest to/till motion")
707 movement, { .i = VIS_MOVE_TOTILL_REPEAT }
709 [VIS_ACTION_TOTILL_REVERSE] = {
710 "vis-motion-totill-reverse",
711 VIS_HELP("Repeat latest to/till motion but in opposite direction")
712 movement, { .i = VIS_MOVE_TOTILL_REVERSE }
714 [VIS_ACTION_PROMPT_SEARCH_FORWARD] = {
715 "vis-search-forward",
716 VIS_HELP("Search forward")
717 prompt_show, { .s = "/" }
719 [VIS_ACTION_PROMPT_SEARCH_BACKWARD] = {
720 "vis-search-backward",
721 VIS_HELP("Search backward")
722 prompt_show, { .s = "?" }
724 [VIS_ACTION_TILL_LEFT] = {
725 "vis-motion-till-left",
726 VIS_HELP("Till after the occurrence of character to the left")
727 movement_key, { .i = VIS_MOVE_LEFT_TILL }
729 [VIS_ACTION_TILL_RIGHT] = {
730 "vis-motion-till-right",
731 VIS_HELP("Till before the occurrence of character to the right")
732 movement_key, { .i = VIS_MOVE_RIGHT_TILL }
734 [VIS_ACTION_TO_LEFT] = {
735 "vis-motion-to-left",
736 VIS_HELP("To the first occurrence of character to the left")
737 movement_key, { .i = VIS_MOVE_LEFT_TO }
739 [VIS_ACTION_TO_RIGHT] = {
740 "vis-motion-to-right",
741 VIS_HELP("To the first occurrence of character to the right")
742 movement_key, { .i = VIS_MOVE_RIGHT_TO }
744 [VIS_ACTION_REGISTER] = {
745 "vis-register",
746 VIS_HELP("Use given register for next operator")
747 reg,
749 [VIS_ACTION_OPERATOR_CHANGE] = {
750 "vis-operator-change",
751 VIS_HELP("Change operator")
752 operator, { .i = VIS_OP_CHANGE }
754 [VIS_ACTION_OPERATOR_DELETE] = {
755 "vis-operator-delete",
756 VIS_HELP("Delete operator")
757 operator, { .i = VIS_OP_DELETE }
759 [VIS_ACTION_OPERATOR_YANK] = {
760 "vis-operator-yank",
761 VIS_HELP("Yank operator")
762 operator, { .i = VIS_OP_YANK }
764 [VIS_ACTION_OPERATOR_SHIFT_LEFT] = {
765 "vis-operator-shift-left",
766 VIS_HELP("Shift left operator")
767 operator, { .i = VIS_OP_SHIFT_LEFT }
769 [VIS_ACTION_OPERATOR_SHIFT_RIGHT] = {
770 "vis-operator-shift-right",
771 VIS_HELP("Shift right operator")
772 operator, { .i = VIS_OP_SHIFT_RIGHT }
774 [VIS_ACTION_OPERATOR_CASE_LOWER] = {
775 "vis-operator-case-lower",
776 VIS_HELP("Lowercase operator")
777 operator, { .i = VIS_OP_CASE_LOWER }
779 [VIS_ACTION_OPERATOR_CASE_UPPER] = {
780 "vis-operator-case-upper",
781 VIS_HELP("Uppercase operator")
782 operator, { .i = VIS_OP_CASE_UPPER }
784 [VIS_ACTION_OPERATOR_CASE_SWAP] = {
785 "vis-operator-case-swap",
786 VIS_HELP("Swap case operator")
787 operator, { .i = VIS_OP_CASE_SWAP }
789 [VIS_ACTION_OPERATOR_FILTER] = {
790 "vis-operator-filter",
791 VIS_HELP("Filter operator")
792 operator_filter,
794 [VIS_ACTION_OPERATOR_FILTER_FMT] = {
795 "vis-operator-filter-format",
796 VIS_HELP("Formatting operator, filter range through fmt(1)")
797 operator_filter, { .s = "|fmt" }
799 [VIS_ACTION_COUNT] = {
800 "vis-count",
801 VIS_HELP("Count specifier")
802 count,
804 [VIS_ACTION_INSERT_NEWLINE] = {
805 "vis-insert-newline",
806 VIS_HELP("Insert a line break (depending on file type)")
807 call, { .f = vis_insert_nl }
809 [VIS_ACTION_INSERT_TAB] = {
810 "vis-insert-tab",
811 VIS_HELP("Insert a tab (might be converted to spaces)")
812 call, { .f = vis_insert_tab }
814 [VIS_ACTION_INSERT_VERBATIM] = {
815 "vis-insert-verbatim",
816 VIS_HELP("Insert Unicode character based on code point")
817 insert_verbatim,
819 [VIS_ACTION_INSERT_REGISTER] = {
820 "vis-insert-register",
821 VIS_HELP("Insert specified register content")
822 insert_register,
824 [VIS_ACTION_WINDOW_NEXT] = {
825 "vis-window-next",
826 VIS_HELP("Focus next window")
827 call, { .f = vis_window_next }
829 [VIS_ACTION_WINDOW_PREV] = {
830 "vis-window-prev",
831 VIS_HELP("Focus previous window")
832 call, { .f = vis_window_prev }
834 [VIS_ACTION_APPEND_CHAR_NEXT] = {
835 "vis-append-char-next",
836 VIS_HELP("Append text after the cursor")
837 insertmode, { .i = VIS_MOVE_LINE_CHAR_NEXT }
839 [VIS_ACTION_APPEND_LINE_END] = {
840 "vis-append-line-end",
841 VIS_HELP("Append text after the end of the line")
842 insertmode, { .i = VIS_MOVE_LINE_END },
844 [VIS_ACTION_INSERT_LINE_START] = {
845 "vis-insert-line-start",
846 VIS_HELP("Insert text before the first non-blank in the line")
847 insertmode, { .i = VIS_MOVE_LINE_START },
849 [VIS_ACTION_OPEN_LINE_ABOVE] = {
850 "vis-open-line-above",
851 VIS_HELP("Begin a new line above the cursor")
852 openline, { .i = -1 }
854 [VIS_ACTION_OPEN_LINE_BELOW] = {
855 "vis-open-line-below",
856 VIS_HELP("Begin a new line below the cursor")
857 openline, { .i = +1 }
859 [VIS_ACTION_JOIN_LINES] = {
860 "vis-join-lines",
861 VIS_HELP("Join selected lines")
862 join, { .s = " " }
864 [VIS_ACTION_JOIN_LINES_TRIM] = {
865 "vis-join-lines-trim",
866 VIS_HELP("Join selected lines, remove white space")
867 join, { .s = "" }
869 [VIS_ACTION_PROMPT_SHOW] = {
870 "vis-prompt-show",
871 VIS_HELP("Show editor command line prompt")
872 prompt_show, { .s = ":" }
874 [VIS_ACTION_REPEAT] = {
875 "vis-repeat",
876 VIS_HELP("Repeat latest editor command")
877 repeat
879 [VIS_ACTION_SELECTION_FLIP] = {
880 "vis-selection-flip",
881 VIS_HELP("Flip selection, move cursor to other end")
882 selection_end,
884 [VIS_ACTION_SELECTION_RESTORE] = {
885 "vis-selection-restore",
886 VIS_HELP("Restore last selection")
887 selection_restore,
889 [VIS_ACTION_WINDOW_REDRAW_TOP] = {
890 "vis-window-redraw-top",
891 VIS_HELP("Redraw cursor line at the top of the window")
892 window, { .w = view_redraw_top }
894 [VIS_ACTION_WINDOW_REDRAW_CENTER] = {
895 "vis-window-redraw-center",
896 VIS_HELP("Redraw cursor line at the center of the window")
897 window, { .w = view_redraw_center }
899 [VIS_ACTION_WINDOW_REDRAW_BOTTOM] = {
900 "vis-window-redraw-bottom",
901 VIS_HELP("Redraw cursor line at the bottom of the window")
902 window, { .w = view_redraw_bottom }
904 [VIS_ACTION_WINDOW_SLIDE_UP] = {
905 "vis-window-slide-up",
906 VIS_HELP("Slide window content upwards")
907 wslide, { .i = -1 }
909 [VIS_ACTION_WINDOW_SLIDE_DOWN] = {
910 "vis-window-slide-down",
911 VIS_HELP("Slide window content downwards")
912 wslide, { .i = +1 }
914 [VIS_ACTION_PUT_AFTER] = {
915 "vis-put-after",
916 VIS_HELP("Put text after the cursor")
917 operator, { .i = VIS_OP_PUT_AFTER }
919 [VIS_ACTION_PUT_BEFORE] = {
920 "vis-put-before",
921 VIS_HELP("Put text before the cursor")
922 operator, { .i = VIS_OP_PUT_BEFORE }
924 [VIS_ACTION_PUT_AFTER_END] = {
925 "vis-put-after-end",
926 VIS_HELP("Put text after the cursor, place cursor after new text")
927 operator, { .i = VIS_OP_PUT_AFTER_END }
929 [VIS_ACTION_PUT_BEFORE_END] = {
930 "vis-put-before-end",
931 VIS_HELP("Put text before the cursor, place cursor after new text")
932 operator, { .i = VIS_OP_PUT_BEFORE_END }
934 [VIS_ACTION_CURSOR_SELECT_WORD] = {
935 "vis-cursor-select-word",
936 VIS_HELP("Select word under cursor")
937 cursors_select,
939 [VIS_ACTION_CURSORS_NEW_LINE_ABOVE] = {
940 "vis-cursor-new-lines-above",
941 VIS_HELP("Create a new cursor on the line above")
942 cursors_new, { .i = -1 }
944 [VIS_ACTION_CURSORS_NEW_LINE_ABOVE_FIRST] = {
945 "vis-cursor-new-lines-above-first",
946 VIS_HELP("Create a new cursor on the line above the first cursor")
947 cursors_new, { .i = INT_MIN }
949 [VIS_ACTION_CURSORS_NEW_LINE_BELOW] = {
950 "vis-cursor-new-lines-below",
951 VIS_HELP("Create a new cursor on the line below")
952 cursors_new, { .i = +1 }
954 [VIS_ACTION_CURSORS_NEW_LINE_BELOW_LAST] = {
955 "vis-cursor-new-lines-below-last",
956 VIS_HELP("Create a new cursor on the line below the last cursor")
957 cursors_new, { .i = INT_MAX }
959 [VIS_ACTION_CURSORS_NEW_LINES_BEGIN] = {
960 "vis-cursor-new-lines-begin",
961 VIS_HELP("Create a new cursor at the start of every line covered by selection")
962 operator, { .i = VIS_OP_CURSOR_SOL }
964 [VIS_ACTION_CURSORS_NEW_LINES_END] = {
965 "vis-cursor-new-lines-end",
966 VIS_HELP("Create a new cursor at the end of every line covered by selection")
967 operator, { .i = VIS_OP_CURSOR_EOL }
969 [VIS_ACTION_CURSORS_NEW_MATCH_NEXT] = {
970 "vis-cursor-new-match-next",
971 VIS_HELP("Select the next region matching the current selection")
972 cursors_select_next
974 [VIS_ACTION_CURSORS_NEW_MATCH_SKIP] = {
975 "vis-cursor-new-match-skip",
976 VIS_HELP("Clear current selection, but select next match")
977 cursors_select_skip,
979 [VIS_ACTION_CURSORS_ALIGN] = {
980 "vis-cursors-align",
981 VIS_HELP("Try to align all cursors on the same column")
982 cursors_align,
984 [VIS_ACTION_CURSORS_ALIGN_INDENT_LEFT] = {
985 "vis-cursors-align-indent-left",
986 VIS_HELP("Left align all cursors/selections by inserting spaces")
987 cursors_align_indent, { .i = -1 }
989 [VIS_ACTION_CURSORS_ALIGN_INDENT_RIGHT] = {
990 "vis-cursors-align-indent-right",
991 VIS_HELP("Right align all cursors/selections by inserting spaces")
992 cursors_align_indent, { .i = +1 }
994 [VIS_ACTION_CURSORS_REMOVE_ALL] = {
995 "vis-cursors-remove-all",
996 VIS_HELP("Remove all but the primary cursor")
997 cursors_clear,
999 [VIS_ACTION_CURSORS_REMOVE_LAST] = {
1000 "vis-cursors-remove-last",
1001 VIS_HELP("Remove least recently created cursor")
1002 cursors_remove,
1004 [VIS_ACTION_CURSORS_REMOVE_COLUMN] = {
1005 "vis-cursors-remove-column",
1006 VIS_HELP("Remove count cursor column")
1007 cursors_remove_column, { .i = 1 }
1009 [VIS_ACTION_CURSORS_REMOVE_COLUMN_EXCEPT] = {
1010 "vis-cursors-remove-column-except",
1011 VIS_HELP("Remove all but the count cursor column")
1012 cursors_remove_column_except, { .i = 1 }
1014 [VIS_ACTION_CURSORS_PREV] = {
1015 "vis-cursor-prev",
1016 VIS_HELP("Move to the previous cursor")
1017 cursors_navigate, { .i = -PAGE_HALF }
1019 [VIS_ACTION_CURSORS_NEXT] = {
1020 "vis-cursor-next",
1021 VIS_HELP("Move to the next cursor")
1022 cursors_navigate, { .i = +PAGE_HALF }
1024 [VIS_ACTION_SELECTIONS_ROTATE_LEFT] = {
1025 "vis-selections-rotate-left",
1026 VIS_HELP("Rotate selections left")
1027 selections_rotate, { .i = -1 }
1029 [VIS_ACTION_SELECTIONS_ROTATE_RIGHT] = {
1030 "vis-selections-rotate-right",
1031 VIS_HELP("Rotate selections right")
1032 selections_rotate, { .i = +1 }
1034 [VIS_ACTION_SELECTIONS_TRIM] = {
1035 "vis-selections-trim",
1036 VIS_HELP("Remove leading and trailing white space from selections")
1037 selections_trim
1039 [VIS_ACTION_TEXT_OBJECT_WORD_OUTER] = {
1040 "vis-textobject-word-outer",
1041 VIS_HELP("A word leading and trailing whitespace included")
1042 textobj, { .i = VIS_TEXTOBJECT_OUTER_WORD }
1044 [VIS_ACTION_TEXT_OBJECT_WORD_INNER] = {
1045 "vis-textobject-word-inner",
1046 VIS_HELP("A word leading and trailing whitespace excluded")
1047 textobj, { .i = VIS_TEXTOBJECT_INNER_WORD }
1049 [VIS_ACTION_TEXT_OBJECT_LONGWORD_OUTER] = {
1050 "vis-textobject-bigword-outer",
1051 VIS_HELP("A WORD leading and trailing whitespace included")
1052 textobj, { .i = VIS_TEXTOBJECT_OUTER_LONGWORD }
1054 [VIS_ACTION_TEXT_OBJECT_LONGWORD_INNER] = {
1055 "vis-textobject-bigword-inner",
1056 VIS_HELP("A WORD leading and trailing whitespace excluded")
1057 textobj, { .i = VIS_TEXTOBJECT_INNER_LONGWORD }
1059 [VIS_ACTION_TEXT_OBJECT_SENTENCE] = {
1060 "vis-textobject-sentence",
1061 VIS_HELP("A sentence")
1062 textobj, { .i = VIS_TEXTOBJECT_SENTENCE }
1064 [VIS_ACTION_TEXT_OBJECT_PARAGRAPH] = {
1065 "vis-textobject-paragraph",
1066 VIS_HELP("A paragraph")
1067 textobj, { .i = VIS_TEXTOBJECT_PARAGRAPH }
1069 [VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_OUTER] = {
1070 "vis-textobject-square-bracket-outer",
1071 VIS_HELP("[] block (outer variant)")
1072 textobj, { .i = VIS_TEXTOBJECT_OUTER_SQUARE_BRACKET }
1074 [VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_INNER] = {
1075 "vis-textobject-square-bracket-inner",
1076 VIS_HELP("[] block (inner variant)")
1077 textobj, { .i = VIS_TEXTOBJECT_INNER_SQUARE_BRACKET }
1079 [VIS_ACTION_TEXT_OBJECT_PARANTHESE_OUTER] = {
1080 "vis-textobject-parentheses-outer",
1081 VIS_HELP("() block (outer variant)")
1082 textobj, { .i = VIS_TEXTOBJECT_OUTER_PARANTHESE }
1084 [VIS_ACTION_TEXT_OBJECT_PARANTHESE_INNER] = {
1085 "vis-textobject-parentheses-inner",
1086 VIS_HELP("() block (inner variant)")
1087 textobj, { .i = VIS_TEXTOBJECT_INNER_PARANTHESE }
1089 [VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_OUTER] = {
1090 "vis-textobject-angle-bracket-outer",
1091 VIS_HELP("<> block (outer variant)")
1092 textobj, { .i = VIS_TEXTOBJECT_OUTER_ANGLE_BRACKET }
1094 [VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_INNER] = {
1095 "vis-textobject-angle-bracket-inner",
1096 VIS_HELP("<> block (inner variant)")
1097 textobj, { .i = VIS_TEXTOBJECT_INNER_ANGLE_BRACKET }
1099 [VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_OUTER] = {
1100 "vis-textobject-curly-bracket-outer",
1101 VIS_HELP("{} block (outer variant)")
1102 textobj, { .i = VIS_TEXTOBJECT_OUTER_CURLY_BRACKET }
1104 [VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_INNER] = {
1105 "vis-textobject-curly-bracket-inner",
1106 VIS_HELP("{} block (inner variant)")
1107 textobj, { .i = VIS_TEXTOBJECT_INNER_CURLY_BRACKET }
1109 [VIS_ACTION_TEXT_OBJECT_QUOTE_OUTER] = {
1110 "vis-textobject-quote-outer",
1111 VIS_HELP("A quoted string, including the quotation marks")
1112 textobj, { .i = VIS_TEXTOBJECT_OUTER_QUOTE }
1114 [VIS_ACTION_TEXT_OBJECT_QUOTE_INNER] = {
1115 "vis-textobject-quote-inner",
1116 VIS_HELP("A quoted string, excluding the quotation marks")
1117 textobj, { .i = VIS_TEXTOBJECT_INNER_QUOTE }
1119 [VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_OUTER] = {
1120 "vis-textobject-single-quote-outer",
1121 VIS_HELP("A single quoted string, including the quotation marks")
1122 textobj, { .i = VIS_TEXTOBJECT_OUTER_SINGLE_QUOTE }
1124 [VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_INNER] = {
1125 "vis-textobject-single-quote-inner",
1126 VIS_HELP("A single quoted string, excluding the quotation marks")
1127 textobj, { .i = VIS_TEXTOBJECT_INNER_SINGLE_QUOTE }
1129 [VIS_ACTION_TEXT_OBJECT_BACKTICK_OUTER] = {
1130 "vis-textobject-backtick-outer",
1131 VIS_HELP("A backtick delimited string (outer variant)")
1132 textobj, { .i = VIS_TEXTOBJECT_OUTER_BACKTICK }
1134 [VIS_ACTION_TEXT_OBJECT_BACKTICK_INNER] = {
1135 "vis-textobject-backtick-inner",
1136 VIS_HELP("A backtick delimited string (inner variant)")
1137 textobj, { .i = VIS_TEXTOBJECT_INNER_BACKTICK }
1139 [VIS_ACTION_TEXT_OBJECT_ENTIRE_OUTER] = {
1140 "vis-textobject-entire-outer",
1141 VIS_HELP("The whole text content")
1142 textobj, { .i = VIS_TEXTOBJECT_OUTER_ENTIRE }
1144 [VIS_ACTION_TEXT_OBJECT_ENTIRE_INNER] = {
1145 "vis-textobject-entire-inner",
1146 VIS_HELP("The whole text content, except for leading and trailing empty lines")
1147 textobj, { .i = VIS_TEXTOBJECT_INNER_ENTIRE }
1149 [VIS_ACTION_TEXT_OBJECT_LINE_OUTER] = {
1150 "vis-textobject-line-outer",
1151 VIS_HELP("The whole line")
1152 textobj, { .i = VIS_TEXTOBJECT_OUTER_LINE }
1154 [VIS_ACTION_TEXT_OBJECT_LINE_INNER] = {
1155 "vis-textobject-line-inner",
1156 VIS_HELP("The whole line, excluding leading and trailing whitespace")
1157 textobj, { .i = VIS_TEXTOBJECT_INNER_LINE }
1159 [VIS_ACTION_TEXT_OBJECT_INDENTATION] = {
1160 "vis-textobject-indentation",
1161 VIS_HELP("All adjacent lines with the same indentation level as the current one")
1162 textobj, { .i = VIS_TEXTOBJECT_INDENTATION }
1164 [VIS_ACTION_TEXT_OBJECT_SEARCH_FORWARD] = {
1165 "vis-textobject-search-forward",
1166 VIS_HELP("The next search match in forward direction")
1167 textobj, { .i = VIS_TEXTOBJECT_SEARCH_FORWARD }
1169 [VIS_ACTION_TEXT_OBJECT_SEARCH_BACKWARD] = {
1170 "vis-textobject-search-backward",
1171 VIS_HELP("The next search match in backward direction")
1172 textobj, { .i = VIS_TEXTOBJECT_SEARCH_BACKWARD }
1174 [VIS_ACTION_MOTION_CHARWISE] = {
1175 "vis-motion-charwise",
1176 VIS_HELP("Force motion to be charwise")
1177 motiontype, { .i = VIS_MOTIONTYPE_CHARWISE }
1179 [VIS_ACTION_MOTION_LINEWISE] = {
1180 "vis-motion-linewise",
1181 VIS_HELP("Force motion to be linewise")
1182 motiontype, { .i = VIS_MOTIONTYPE_LINEWISE }
1184 [VIS_ACTION_UNICODE_INFO] = {
1185 "vis-unicode-info",
1186 VIS_HELP("Show Unicode codepoint(s) of character under cursor")
1187 unicode_info, { .i = VIS_ACTION_UNICODE_INFO }
1189 [VIS_ACTION_UTF8_INFO] = {
1190 "vis-utf8-info",
1191 VIS_HELP("Show UTF-8 encoded codepoint(s) of character under cursor")
1192 unicode_info, { .i = VIS_ACTION_UTF8_INFO }
1194 [VIS_ACTION_NOP] = {
1195 "vis-nop",
1196 VIS_HELP("Ignore key, do nothing")
1197 nop,
1201 #include "config.h"
1203 /** key bindings functions */
1205 static const char *nop(Vis *vis, const char *keys, const Arg *arg) {
1206 return keys;
1209 static const char *macro_record(Vis *vis, const char *keys, const Arg *arg) {
1210 if (!vis_macro_record_stop(vis)) {
1211 if (!keys[0])
1212 return NULL;
1213 if (keys[1])
1214 return vis_keys_next(vis, keys);
1215 enum VisRegister reg = vis_register_from(vis, keys[0]);
1216 vis_macro_record(vis, reg);
1217 keys++;
1219 vis_draw(vis);
1220 return keys;
1223 static const char *macro_replay(Vis *vis, const char *keys, const Arg *arg) {
1224 if (!keys[0])
1225 return NULL;
1226 if (keys[1])
1227 return vis_keys_next(vis, keys);
1228 enum VisRegister reg = vis_register_from(vis, keys[0]);
1229 vis_macro_replay(vis, reg);
1230 return keys+1;
1233 static const char *suspend(Vis *vis, const char *keys, const Arg *arg) {
1234 vis_suspend(vis);
1235 return keys;
1238 static const char *repeat(Vis *vis, const char *keys, const Arg *arg) {
1239 vis_repeat(vis);
1240 return keys;
1243 static const char *cursors_new(Vis *vis, const char *keys, const Arg *arg) {
1244 View *view = vis_view(vis);
1245 VisCountIterator it = vis_count_iterator_get(vis, 1);
1246 while (vis_count_iterator_next(&it)) {
1247 Selection *sel = NULL;
1248 switch (arg->i) {
1249 case -1:
1250 case +1:
1251 sel = view_selections_primary_get(view);
1252 break;
1253 case INT_MIN:
1254 sel = view_selections(view);
1255 break;
1256 case INT_MAX:
1257 for (Selection *s = view_selections(view); s; s = view_selections_next(s))
1258 sel = s;
1259 break;
1260 default:
1261 return keys;
1263 size_t oldpos = view_cursors_pos(sel);
1264 if (arg->i > 0)
1265 view_line_down(sel);
1266 else if (arg->i < 0)
1267 view_line_up(sel);
1268 size_t newpos = view_cursors_pos(sel);
1269 view_cursors_to(sel, oldpos);
1270 Selection *sel_new = view_selections_new(view, newpos);
1271 if (!sel_new) {
1272 if (arg->i == -1)
1273 sel_new = view_selections_prev(sel);
1274 else if (arg->i == +1)
1275 sel_new = view_selections_next(sel);
1277 if (sel_new)
1278 view_selections_primary_set(sel_new);
1280 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1281 return keys;
1284 static const char *cursors_align(Vis *vis, const char *keys, const Arg *arg) {
1285 View *view = vis_view(vis);
1286 Text *txt = vis_text(vis);
1287 int mincol = INT_MAX;
1288 for (Selection *s = view_selections(view); s; s = view_selections_next(s)) {
1289 int col = view_cursors_cell_get(s);
1290 if (col >= 0 && col < mincol)
1291 mincol = col;
1293 for (Selection *s = view_selections(view); s; s = view_selections_next(s)) {
1294 if (view_cursors_cell_set(s, mincol) == -1) {
1295 size_t pos = view_cursors_pos(s);
1296 size_t col = text_line_width_set(txt, pos, mincol);
1297 view_cursors_to(s, col);
1300 return keys;
1303 static const char *cursors_align_indent(Vis *vis, const char *keys, const Arg *arg) {
1304 View *view = vis_view(vis);
1305 Text *txt = vis_text(vis);
1306 bool left_align = arg->i < 0;
1307 int columns = view_selections_column_count(view);
1309 for (int i = 0; i < columns; i++) {
1310 int mincol = INT_MAX, maxcol = 0;
1311 for (Selection *s = view_selections_column(view, i); s; s = view_selections_column_next(s, i)) {
1312 Filerange sel = view_selections_get(s);
1313 size_t pos = left_align ? sel.start : sel.end;
1314 int col = text_line_width_get(txt, pos);
1315 if (col < mincol)
1316 mincol = col;
1317 if (col > maxcol)
1318 maxcol = col;
1321 size_t len = maxcol - mincol;
1322 char *buf = malloc(len+1);
1323 if (!buf)
1324 return keys;
1325 memset(buf, ' ', len);
1327 for (Selection *s = view_selections_column(view, i); s; s = view_selections_column_next(s, i)) {
1328 Filerange sel = view_selections_get(s);
1329 size_t pos = left_align ? sel.start : sel.end;
1330 size_t ipos = sel.start;
1331 int col = text_line_width_get(txt, pos);
1332 if (col < maxcol) {
1333 size_t off = maxcol - col;
1334 if (off <= len)
1335 text_insert(txt, ipos, buf, off);
1339 free(buf);
1342 view_draw(view);
1343 return keys;
1346 static const char *cursors_clear(Vis *vis, const char *keys, const Arg *arg) {
1347 View *view = vis_view(vis);
1348 if (view_selections_count(view) > 1)
1349 view_selections_dispose_all(view);
1350 else
1351 view_selection_clear(view_selections_primary_get(view));
1352 return keys;
1355 static const char *cursors_select(Vis *vis, const char *keys, const Arg *arg) {
1356 Text *txt = vis_text(vis);
1357 View *view = vis_view(vis);
1358 for (Selection *s = view_selections(view); s; s = view_selections_next(s)) {
1359 Filerange word = text_object_word(txt, view_cursors_pos(s));
1360 if (text_range_valid(&word))
1361 view_selections_set(s, &word);
1363 vis_mode_switch(vis, VIS_MODE_VISUAL);
1364 return keys;
1367 static const char *cursors_select_next(Vis *vis, const char *keys, const Arg *arg) {
1368 Text *txt = vis_text(vis);
1369 View *view = vis_view(vis);
1370 Selection *s = view_selections_primary_get(view);
1371 Filerange sel = view_selections_get(s);
1372 if (!text_range_valid(&sel))
1373 return keys;
1375 char *buf = text_bytes_alloc0(txt, sel.start, text_range_size(&sel));
1376 if (!buf)
1377 return keys;
1378 Filerange word = text_object_word_find_next(txt, sel.end, buf);
1379 if (text_range_valid(&word)) {
1380 size_t pos = text_char_prev(txt, word.end);
1381 if ((s = view_selections_new(view, pos))) {
1382 view_selections_set(s, &word);
1383 view_selections_primary_set(s);
1384 goto out;
1388 sel = view_selections_get(view_selections(view));
1389 word = text_object_word_find_prev(txt, sel.start, buf);
1390 if (!text_range_valid(&word))
1391 goto out;
1392 size_t pos = text_char_prev(txt, word.end);
1393 if ((s = view_selections_new(view, pos))) {
1394 view_selections_set(s, &word);
1395 view_selections_primary_set(s);
1398 out:
1399 free(buf);
1400 return keys;
1403 static const char *cursors_select_skip(Vis *vis, const char *keys, const Arg *arg) {
1404 View *view = vis_view(vis);
1405 Selection *sel = view_selections_primary_get(view);
1406 keys = cursors_select_next(vis, keys, arg);
1407 if (sel != view_selections_primary_get(view))
1408 view_selections_dispose(sel);
1409 return keys;
1412 static const char *cursors_remove(Vis *vis, const char *keys, const Arg *arg) {
1413 View *view = vis_view(vis);
1414 view_selections_dispose(view_selections_primary_get(view));
1415 view_cursor_to(view, view_cursor_get(view));
1416 return keys;
1419 static const char *cursors_remove_column(Vis *vis, const char *keys, const Arg *arg) {
1420 View *view = vis_view(vis);
1421 int max = view_selections_column_count(view);
1422 int column = vis_count_get_default(vis, arg->i) - 1;
1423 if (column >= max)
1424 column = max - 1;
1425 if (view_selections_count(view) == 1) {
1426 vis_mode_switch(vis, VIS_MODE_NORMAL);
1427 return keys;
1430 for (Selection *s = view_selections_column(view, column), *next; s; s = next) {
1431 next = view_selections_column_next(s, column);
1432 view_selections_dispose(s);
1435 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1436 return keys;
1439 static const char *cursors_remove_column_except(Vis *vis, const char *keys, const Arg *arg) {
1440 View *view = vis_view(vis);
1441 int max = view_selections_column_count(view);
1442 int column = vis_count_get_default(vis, arg->i) - 1;
1443 if (column >= max)
1444 column = max - 1;
1445 if (view_selections_count(view) == 1) {
1446 vis_redraw(vis);
1447 return keys;
1450 Selection *sel = view_selections(view);
1451 Selection *col = view_selections_column(view, column);
1452 for (Selection *next; sel; sel = next) {
1453 next = view_selections_next(sel);
1454 if (sel == col)
1455 col = view_selections_column_next(col, column);
1456 else
1457 view_selections_dispose(sel);
1460 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1461 return keys;
1464 static const char *cursors_navigate(Vis *vis, const char *keys, const Arg *arg) {
1465 View *view = vis_view(vis);
1466 if (view_selections_count(view) == 1)
1467 return wscroll(vis, keys, arg);
1468 Selection *s = view_selections_primary_get(view);
1469 VisCountIterator it = vis_count_iterator_get(vis, 1);
1470 while (vis_count_iterator_next(&it)) {
1471 if (arg->i > 0) {
1472 s = view_selections_next(s);
1473 if (!s)
1474 s = view_selections(view);
1475 } else {
1476 s = view_selections_prev(s);
1477 if (!s) {
1478 s = view_selections(view);
1479 for (Selection *n = s; n; n = view_selections_next(n))
1480 s = n;
1484 view_selections_primary_set(s);
1485 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1486 return keys;
1489 static const char *selections_rotate(Vis *vis, const char *keys, const Arg *arg) {
1491 typedef struct {
1492 Selection *sel;
1493 char *data;
1494 size_t len;
1495 } Rotate;
1497 Array arr;
1498 Text *txt = vis_text(vis);
1499 View *view = vis_view(vis);
1500 int columns = view_selections_column_count(view);
1501 int selections = columns == 1 ? view_selections_count(view) : columns;
1502 int count = vis_count_get_default(vis, 1);
1503 array_init_sized(&arr, sizeof(Rotate));
1504 if (!array_reserve(&arr, selections))
1505 return keys;
1506 size_t line = 0;
1508 for (Selection *s = view_selections(view), *next; s; s = next) {
1509 next = view_selections_next(s);
1510 size_t line_next = 0;
1512 Filerange sel = view_selections_get(s);
1513 Rotate rot;
1514 rot.sel = s;
1515 rot.len = text_range_size(&sel);
1516 if ((rot.data = malloc(rot.len)))
1517 rot.len = text_bytes_get(txt, sel.start, rot.len, rot.data);
1518 else
1519 rot.len = 0;
1520 array_add(&arr, &rot);
1522 if (!line)
1523 line = text_lineno_by_pos(txt, view_cursors_pos(s));
1524 if (next)
1525 line_next = text_lineno_by_pos(txt, view_cursors_pos(next));
1526 if (!next || (columns > 1 && line != line_next)) {
1527 size_t len = array_length(&arr);
1528 size_t off = arg->i > 0 ? count % len : len - (count % len);
1529 for (size_t i = 0; i < len; i++) {
1530 size_t j = (i + off) % len;
1531 Rotate *oldrot = array_get(&arr, i);
1532 Rotate *newrot = array_get(&arr, j);
1533 if (!oldrot || !newrot || oldrot == newrot)
1534 continue;
1535 Filerange newsel = view_selections_get(newrot->sel);
1536 if (!text_range_valid(&newsel))
1537 continue;
1538 if (!text_delete_range(txt, &newsel))
1539 continue;
1540 if (!text_insert(txt, newsel.start, oldrot->data, oldrot->len))
1541 continue;
1542 newsel.end = newsel.start + oldrot->len;
1543 view_selections_set(newrot->sel, &newsel);
1544 free(oldrot->data);
1546 array_clear(&arr);
1548 line = line_next;
1551 array_release(&arr);
1552 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1553 return keys;
1556 static const char *selections_trim(Vis *vis, const char *keys, const Arg *arg) {
1557 Text *txt = vis_text(vis);
1558 View *view = vis_view(vis);
1559 for (Selection *s = view_selections(view), *next; s; s = next) {
1560 next = view_selections_next(s);
1561 Filerange sel = view_selections_get(s);
1562 if (!text_range_valid(&sel))
1563 continue;
1564 for (char b; sel.start < sel.end && text_byte_get(txt, sel.end-1, &b)
1565 && isspace((unsigned char)b); sel.end--);
1566 for (char b; sel.start <= sel.end && text_byte_get(txt, sel.start, &b)
1567 && isspace((unsigned char)b); sel.start++);
1568 if (sel.start < sel.end) {
1569 view_selections_set(s, &sel);
1570 } else if (!view_selections_dispose(s)) {
1571 vis_mode_switch(vis, VIS_MODE_NORMAL);
1574 return keys;
1577 static const char *replace(Vis *vis, const char *keys, const Arg *arg) {
1578 if (!keys[0]) {
1579 vis_keymap_disable(vis);
1580 return NULL;
1583 const char *next = vis_keys_next(vis, keys);
1584 if (!next)
1585 return NULL;
1587 char replacement[UTFmax+1];
1588 if (!vis_keys_utf8(vis, keys, replacement))
1589 return next;
1591 if (replacement[0] == 0x1b) /* <Escape> */
1592 return next;
1594 vis_operator(vis, VIS_OP_REPLACE, replacement);
1595 if (vis_mode_get(vis) == VIS_MODE_OPERATOR_PENDING)
1596 vis_motion(vis, VIS_MOVE_CHAR_NEXT);
1597 return next;
1600 static const char *count(Vis *vis, const char *keys, const Arg *arg) {
1601 int digit = keys[-1] - '0';
1602 int count = vis_count_get_default(vis, 0);
1603 if (0 <= digit && digit <= 9) {
1604 if (digit == 0 && count == 0)
1605 vis_motion(vis, VIS_MOVE_LINE_BEGIN);
1606 else
1607 vis_count_set(vis, count * 10 + digit);
1609 return keys;
1612 static const char *gotoline(Vis *vis, const char *keys, const Arg *arg) {
1613 if (vis_count_get(vis) != VIS_COUNT_UNKNOWN)
1614 vis_motion(vis, VIS_MOVE_LINE);
1615 else if (arg->i < 0)
1616 vis_motion(vis, VIS_MOVE_FILE_BEGIN);
1617 else
1618 vis_motion(vis, VIS_MOVE_FILE_END);
1619 return keys;
1622 static const char *motiontype(Vis *vis, const char *keys, const Arg *arg) {
1623 vis_motion_type(vis, arg->i);
1624 return keys;
1627 static const char *operator(Vis *vis, const char *keys, const Arg *arg) {
1628 vis_operator(vis, arg->i);
1629 return keys;
1632 static const char *operator_filter(Vis *vis, const char *keys, const Arg *arg) {
1633 vis_operator(vis, VIS_OP_FILTER, arg->s);
1634 return keys;
1637 static const char *movement_key(Vis *vis, const char *keys, const Arg *arg) {
1638 if (!keys[0]) {
1639 vis_keymap_disable(vis);
1640 return NULL;
1643 const char *next = vis_keys_next(vis, keys);
1644 if (!next)
1645 return NULL;
1646 char utf8[UTFmax+1];
1647 if (vis_keys_utf8(vis, keys, utf8))
1648 vis_motion(vis, arg->i, utf8);
1649 return next;
1652 static const char *movement(Vis *vis, const char *keys, const Arg *arg) {
1653 vis_motion(vis, arg->i);
1654 return keys;
1657 static const char *textobj(Vis *vis, const char *keys, const Arg *arg) {
1658 vis_textobject(vis, arg->i);
1659 return keys;
1662 static const char *selection_end(Vis *vis, const char *keys, const Arg *arg) {
1663 for (Selection *s = view_selections(vis_view(vis)); s; s = view_selections_next(s))
1664 view_selections_flip(s);
1665 return keys;
1668 static const char *selection_restore(Vis *vis, const char *keys, const Arg *arg) {
1669 Text *txt = vis_text(vis);
1670 View *view = vis_view(vis);
1671 for (Selection *s = view_selections(view); s; s = view_selections_next(s))
1672 view_selections_restore(s);
1673 Filerange sel = view_selection_get(view);
1674 if (text_range_is_linewise(txt, &sel))
1675 vis_mode_switch(vis, VIS_MODE_VISUAL_LINE);
1676 else
1677 vis_mode_switch(vis, VIS_MODE_VISUAL);
1678 return keys;
1681 static const char *reg(Vis *vis, const char *keys, const Arg *arg) {
1682 if (!keys[0])
1683 return NULL;
1684 if (keys[1])
1685 return vis_keys_next(vis, keys);
1686 enum VisRegister reg = vis_register_from(vis, keys[0]);
1687 vis_register(vis, reg);
1688 return keys+1;
1691 static const char *mark_set(Vis *vis, const char *keys, const Arg *arg) {
1692 if (!keys[0])
1693 return NULL;
1694 if (keys[1])
1695 return vis_keys_next(vis, keys);
1696 vis_mark_set(vis, vis_mark_from(vis, keys[0]), view_cursor_get(vis_view(vis)));
1697 return keys+1;
1700 static const char *mark_motion(Vis *vis, const char *keys, const Arg *arg) {
1701 if (!keys[0])
1702 return NULL;
1703 if (keys[1])
1704 return vis_keys_next(vis, keys);
1705 vis_motion(vis, arg->i, vis_mark_from(vis, keys[0]));
1706 return keys+1;
1709 static const char *undo(Vis *vis, const char *keys, const Arg *arg) {
1710 size_t pos = text_undo(vis_text(vis));
1711 if (pos != EPOS) {
1712 View *view = vis_view(vis);
1713 if (view_selections_count(view) == 1)
1714 view_cursor_to(view, pos);
1715 /* redraw all windows in case some display the same file */
1716 vis_draw(vis);
1718 return keys;
1721 static const char *redo(Vis *vis, const char *keys, const Arg *arg) {
1722 size_t pos = text_redo(vis_text(vis));
1723 if (pos != EPOS) {
1724 View *view = vis_view(vis);
1725 if (view_selections_count(view) == 1)
1726 view_cursor_to(view, pos);
1727 /* redraw all windows in case some display the same file */
1728 vis_draw(vis);
1730 return keys;
1733 static const char *earlier(Vis *vis, const char *keys, const Arg *arg) {
1734 size_t pos = EPOS;
1735 VisCountIterator it = vis_count_iterator_get(vis, 1);
1736 while (vis_count_iterator_next(&it))
1737 pos = text_earlier(vis_text(vis));
1738 if (pos != EPOS) {
1739 view_cursor_to(vis_view(vis), pos);
1740 /* redraw all windows in case some display the same file */
1741 vis_draw(vis);
1743 return keys;
1746 static const char *later(Vis *vis, const char *keys, const Arg *arg) {
1747 size_t pos = EPOS;
1748 VisCountIterator it = vis_count_iterator_get(vis, 1);
1749 while (vis_count_iterator_next(&it))
1750 pos = text_later(vis_text(vis));
1751 if (pos != EPOS) {
1752 view_cursor_to(vis_view(vis), pos);
1753 /* redraw all windows in case some display the same file */
1754 vis_draw(vis);
1756 return keys;
1759 static const char *delete(Vis *vis, const char *keys, const Arg *arg) {
1760 vis_operator(vis, VIS_OP_DELETE);
1761 vis_motion(vis, arg->i);
1762 return keys;
1765 static const char *insert_register(Vis *vis, const char *keys, const Arg *arg) {
1766 if (!keys[0])
1767 return NULL;
1768 if (keys[1])
1769 return vis_keys_next(vis, keys);
1770 enum VisRegister reg = vis_register_from(vis, keys[0]);
1771 if (reg != VIS_REG_INVALID) {
1772 vis_register(vis, reg);
1773 vis_operator(vis, VIS_OP_PUT_BEFORE_END);
1775 return keys+1;
1778 static const char *prompt_show(Vis *vis, const char *keys, const Arg *arg) {
1779 vis_prompt_show(vis, arg->s);
1780 return keys;
1783 static const char *insert_verbatim(Vis *vis, const char *keys, const Arg *arg) {
1784 Rune rune = 0;
1785 char buf[4], type = keys[0];
1786 const char *data = NULL;
1787 int len = 0, count = 0, base = 0;
1788 switch (type) {
1789 case '\0':
1790 return NULL;
1791 case 'o':
1792 case 'O':
1793 count = 3;
1794 base = 8;
1795 break;
1796 case 'U':
1797 count = 4;
1798 /* fall through */
1799 case 'u':
1800 count += 4;
1801 base = 16;
1802 break;
1803 case 'x':
1804 case 'X':
1805 count = 2;
1806 base = 16;
1807 break;
1808 default:
1809 if ('0' <= type && type <= '9') {
1810 rune = type - '0';
1811 count = 2;
1812 base = 10;
1814 break;
1817 if (base) {
1818 for (keys++; keys[0] && count > 0; keys++, count--) {
1819 int v = 0;
1820 if (base == 8 && '0' <= keys[0] && keys[0] <= '7') {
1821 v = keys[0] - '0';
1822 } else if ((base == 10 || base == 16) && '0' <= keys[0] && keys[0] <= '9') {
1823 v = keys[0] - '0';
1824 } else if (base == 16 && 'a' <= keys[0] && keys[0] <= 'f') {
1825 v = 10 + keys[0] - 'a';
1826 } else if (base == 16 && 'A' <= keys[0] && keys[0] <= 'F') {
1827 v = 10 + keys[0] - 'A';
1828 } else {
1829 count = 0;
1830 break;
1832 rune = rune * base + v;
1835 if (count > 0)
1836 return NULL;
1837 if (type == 'u' || type == 'U') {
1838 len = runetochar(buf, &rune);
1839 } else {
1840 buf[0] = rune;
1841 len = 1;
1844 data = buf;
1845 } else {
1846 const char *next = vis_keys_next(vis, keys);
1847 if (!next)
1848 return NULL;
1849 if ((rune = vis_keys_codepoint(vis, keys)) != (Rune)-1) {
1850 len = runetochar(buf, &rune);
1851 data = buf;
1852 } else {
1853 vis_info_show(vis, "Unknown key");
1855 keys = next;
1858 if (len > 0)
1859 vis_insert_key(vis, data, len);
1860 return keys;
1863 static const char *wscroll(Vis *vis, const char *keys, const Arg *arg) {
1864 View *view = vis_view(vis);
1865 int count = vis_count_get(vis);
1866 switch (arg->i) {
1867 case -PAGE:
1868 view_scroll_page_up(view);
1869 break;
1870 case +PAGE:
1871 view_scroll_page_down(view);
1872 break;
1873 case -PAGE_HALF:
1874 view_scroll_halfpage_up(view);
1875 break;
1876 case +PAGE_HALF:
1877 view_scroll_halfpage_down(view);
1878 break;
1879 default:
1880 if (count == VIS_COUNT_UNKNOWN)
1881 count = arg->i < 0 ? -arg->i : arg->i;
1882 if (arg->i < 0)
1883 view_scroll_up(view, count);
1884 else
1885 view_scroll_down(view, count);
1886 break;
1888 return keys;
1891 static const char *wslide(Vis *vis, const char *keys, const Arg *arg) {
1892 View *view = vis_view(vis);
1893 int count = vis_count_get(vis);
1894 if (count == VIS_COUNT_UNKNOWN)
1895 count = arg->i < 0 ? -arg->i : arg->i;
1896 if (arg->i >= 0)
1897 view_slide_down(view, count);
1898 else
1899 view_slide_up(view, count);
1900 return keys;
1903 static const char *call(Vis *vis, const char *keys, const Arg *arg) {
1904 arg->f(vis);
1905 return keys;
1908 static const char *window(Vis *vis, const char *keys, const Arg *arg) {
1909 arg->w(vis_view(vis));
1910 return keys;
1913 static const char *openline(Vis *vis, const char *keys, const Arg *arg) {
1914 vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_INSERT);
1915 if (arg->i > 0) {
1916 vis_motion(vis, VIS_MOVE_LINE_END);
1917 vis_keys_feed(vis, "<Enter>");
1918 } else {
1919 if (vis_get_autoindent(vis)) {
1920 vis_motion(vis, VIS_MOVE_LINE_START);
1921 vis_keys_feed(vis, "<vis-motion-line-start>");
1922 } else {
1923 vis_motion(vis, VIS_MOVE_LINE_BEGIN);
1924 vis_keys_feed(vis, "<vis-motion-line-begin>");
1926 vis_keys_feed(vis, "<Enter><Up>");
1928 return keys;
1931 static const char *join(Vis *vis, const char *keys, const Arg *arg) {
1932 bool normal = (vis_mode_get(vis) == VIS_MODE_NORMAL);
1933 vis_operator(vis, VIS_OP_JOIN, arg->s);
1934 if (normal) {
1935 int count = vis_count_get_default(vis, 0);
1936 if (count)
1937 vis_count_set(vis, count-1);
1938 vis_motion(vis, VIS_MOVE_LINE_NEXT);
1940 return keys;
1943 static const char *switchmode(Vis *vis, const char *keys, const Arg *arg) {
1944 vis_mode_switch(vis, arg->i);
1945 return keys;
1948 static const char *insertmode(Vis *vis, const char *keys, const Arg *arg) {
1949 vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_INSERT);
1950 vis_motion(vis, arg->i);
1951 return keys;
1954 static const char *replacemode(Vis *vis, const char *keys, const Arg *arg) {
1955 vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_REPLACE);
1956 vis_motion(vis, arg->i);
1957 return keys;
1960 static const char *unicode_info(Vis *vis, const char *keys, const Arg *arg) {
1961 View *view = vis_view(vis);
1962 Text *txt = vis_text(vis);
1963 size_t start = view_cursor_get(view);
1964 size_t end = text_char_next(txt, start);
1965 char *grapheme = text_bytes_alloc0(txt, start, end-start), *codepoint = grapheme;
1966 if (!grapheme)
1967 return keys;
1968 Buffer info;
1969 buffer_init(&info);
1970 mbstate_t ps = { 0 };
1971 Iterator it = text_iterator_get(txt, start);
1972 for (size_t pos = start; it.pos < end; pos = it.pos) {
1973 if (!text_iterator_codepoint_next(&it, NULL)) {
1974 vis_info_show(vis, "Failed to parse code point");
1975 goto err;
1977 size_t len = it.pos - pos;
1978 wchar_t wc = 0xFFFD;
1979 size_t res = mbrtowc(&wc, codepoint, len, &ps);
1980 bool combining = false;
1981 if (res != (size_t)-1 && res != (size_t)-2)
1982 combining = (wc != L'\0' && wcwidth(wc) == 0);
1983 unsigned char ch = *codepoint;
1984 if (ch < 128 && !isprint(ch))
1985 buffer_appendf(&info, "<^%c> ", ch == 127 ? '?' : ch + 64);
1986 else
1987 buffer_appendf(&info, "<%s%.*s> ", combining ? " " : "", (int)len, codepoint);
1988 if (arg->i == VIS_ACTION_UNICODE_INFO) {
1989 buffer_appendf(&info, "U+%04"PRIX32" ", (uint32_t)wc);
1990 } else {
1991 for (size_t i = 0; i < len; i++)
1992 buffer_appendf(&info, "%02x ", (uint8_t)codepoint[i]);
1994 codepoint += len;
1996 vis_info_show(vis, "%s", buffer_content0(&info));
1997 err:
1998 free(grapheme);
1999 buffer_release(&info);
2000 return keys;
2003 static const char *percent(Vis *vis, const char *keys, const Arg *arg) {
2004 if (vis_count_get(vis) == VIS_COUNT_UNKNOWN)
2005 vis_motion(vis, VIS_MOVE_BRACKET_MATCH);
2006 else
2007 vis_motion(vis, VIS_MOVE_PERCENT);
2008 return keys;
2011 static Vis *vis;
2013 static void signal_handler(int signum, siginfo_t *siginfo, void *context) {
2014 vis_signal_handler(vis, signum, siginfo, context);
2017 int main(int argc, char *argv[]) {
2019 VisEvent event = {
2020 .init = vis_lua_init,
2021 .start = vis_lua_start,
2022 .quit = vis_lua_quit,
2023 .mode_insert_input = vis_lua_mode_insert_input,
2024 .mode_replace_input = vis_lua_mode_replace_input,
2025 .file_open = vis_lua_file_open,
2026 .file_save_pre = vis_lua_file_save_pre,
2027 .file_save_post = vis_lua_file_save_post,
2028 .file_close = vis_lua_file_close,
2029 .win_open = vis_lua_win_open,
2030 .win_close = vis_lua_win_close,
2031 .win_highlight = vis_lua_win_highlight,
2032 .win_status = vis_lua_win_status,
2035 vis = vis_new(ui_term_new(), &event);
2036 if (!vis)
2037 return EXIT_FAILURE;
2039 for (int i = 0; i < LENGTH(vis_action); i++) {
2040 const KeyAction *action = &vis_action[i];
2041 if (!vis_action_register(vis, action))
2042 vis_die(vis, "Could not register action: %s\n", action->name);
2045 for (int i = 0; i < LENGTH(default_bindings); i++) {
2046 for (const KeyBinding **binding = default_bindings[i]; binding && *binding; binding++) {
2047 for (const KeyBinding *kb = *binding; kb->key; kb++) {
2048 vis_mode_map(vis, i, false, kb->key, kb);
2053 for (const char **k = keymaps; k[0]; k += 2)
2054 vis_keymap_add(vis, k[0], k[1]);
2056 /* install signal handlers etc. */
2057 struct sigaction sa;
2058 memset(&sa, 0, sizeof sa);
2059 sigfillset(&sa.sa_mask);
2060 sa.sa_flags = SA_SIGINFO;
2061 sa.sa_sigaction = signal_handler;
2062 if (sigaction(SIGBUS, &sa, NULL) == -1 ||
2063 sigaction(SIGINT, &sa, NULL) == -1 ||
2064 sigaction(SIGCONT, &sa, NULL) == -1 ||
2065 sigaction(SIGWINCH, &sa, NULL) == -1 ||
2066 sigaction(SIGTERM, &sa, NULL) == -1 ||
2067 sigaction(SIGHUP, &sa, NULL) == -1) {
2068 vis_die(vis, "Failed to set signal handler: %s\n", strerror(errno));
2071 sa.sa_handler = SIG_IGN;
2072 if (sigaction(SIGPIPE, &sa, NULL) == -1)
2073 vis_die(vis, "Failed to ignore SIGPIPE\n");
2075 sigset_t blockset;
2076 sigemptyset(&blockset);
2077 sigaddset(&blockset, SIGBUS);
2078 sigaddset(&blockset, SIGCONT);
2079 sigaddset(&blockset, SIGWINCH);
2080 sigaddset(&blockset, SIGTERM);
2081 sigaddset(&blockset, SIGHUP);
2082 if (sigprocmask(SIG_BLOCK, &blockset, NULL) == -1)
2083 vis_die(vis, "Failed to block signals\n");
2085 for (int i = 1; i < argc; i++) {
2086 if (argv[i][0] != '-') {
2087 continue;
2088 } else if (strcmp(argv[i], "-") == 0) {
2089 continue;
2090 } else if (strcmp(argv[i], "--") == 0) {
2091 break;
2092 } else if (strcmp(argv[i], "-v") == 0) {
2093 printf("vis %s%s%s%s%s%s%s\n", VERSION,
2094 CONFIG_CURSES ? " +curses" : "",
2095 CONFIG_LUA ? " +lua" : "",
2096 CONFIG_LPEG ? " +lpeg" : "",
2097 CONFIG_TRE ? " +tre" : "",
2098 CONFIG_ACL ? " +acl" : "",
2099 CONFIG_SELINUX ? " +selinux" : "");
2100 return 0;
2101 } else {
2102 fprintf(stderr, "Unknown command option: %s\n", argv[i]);
2103 return 1;
2107 char *cmd = NULL;
2108 bool end_of_options = false, win_created = false;
2110 for (int i = 1; i < argc; i++) {
2111 if (argv[i][0] == '-' && !end_of_options) {
2112 if (strcmp(argv[i], "-") == 0) {
2113 if (!vis_window_new_fd(vis, STDOUT_FILENO))
2114 vis_die(vis, "Can not create empty buffer\n");
2115 ssize_t len = 0;
2116 char buf[PIPE_BUF];
2117 Text *txt = vis_text(vis);
2118 while ((len = read(STDIN_FILENO, buf, sizeof buf)) > 0)
2119 text_insert(txt, text_size(txt), buf, len);
2120 if (len == -1)
2121 vis_die(vis, "Can not read from stdin\n");
2122 text_snapshot(txt);
2123 int fd = open("/dev/tty", O_RDWR);
2124 if (fd == -1)
2125 vis_die(vis, "Can not reopen stdin\n");
2126 dup2(fd, STDIN_FILENO);
2127 close(fd);
2128 } else if (strcmp(argv[i], "--") == 0) {
2129 end_of_options = true;
2130 continue;
2132 } else if (argv[i][0] == '+' && !end_of_options) {
2133 cmd = argv[i] + (argv[i][1] == '/' || argv[i][1] == '?');
2134 continue;
2135 } else if (!vis_window_new(vis, argv[i])) {
2136 vis_die(vis, "Can not load `%s': %s\n", argv[i], strerror(errno));
2138 win_created = true;
2139 if (cmd) {
2140 vis_prompt_cmd(vis, cmd);
2141 cmd = NULL;
2145 if (!vis_window(vis) && !win_created) {
2146 if (!vis_window_new(vis, NULL))
2147 vis_die(vis, "Can not create empty buffer\n");
2148 if (cmd)
2149 vis_prompt_cmd(vis, cmd);
2152 int status = vis_run(vis);
2153 vis_free(vis);
2154 return status;