vis-lua: expose win.syntax
[vis.git] / main.c
bloba7fc2345baaeff42a4ec14b8d0699aa08cf77609
1 #include <signal.h>
2 #include <limits.h>
3 #include <string.h>
4 #include <wchar.h>
5 #include <errno.h>
7 #include "ui-curses.h"
8 #include "vis.h"
9 #include "vis-lua.h"
10 #include "text-util.h"
11 #include "text-motions.h"
12 #include "text-objects.h"
13 #include "util.h"
14 #include "libutf.h"
16 #define PAGE INT_MAX
17 #define PAGE_HALF (INT_MAX-1)
19 /** functions to be called from keybindings */
20 /* ignore key, do nothing */
21 static const char *nop(Vis*, const char *keys, const Arg *arg);
22 /* record/replay macro indicated by keys */
23 static const char *macro_record(Vis*, const char *keys, const Arg *arg);
24 static const char *macro_replay(Vis*, const char *keys, const Arg *arg);
25 /* temporarily suspend the editor and return to the shell, type 'fg' to get back */
26 static const char *suspend(Vis*, const char *keys, const Arg *arg);
27 /* switch to mode indicated by arg->i */
28 static const char *switchmode(Vis*, const char *keys, const Arg *arg);
29 /* switch to insert mode after performing movement indicated by arg->i */
30 static const char *insertmode(Vis*, const char *keys, const Arg *arg);
31 /* set mark indicated by keys to current cursor position */
32 static const char *mark_set(Vis*, const char *keys, const Arg *arg);
33 /* add a new line either before or after the one where the cursor currently is */
34 static const char *openline(Vis*, const char *keys, const Arg *arg);
35 /* join lines from current cursor position to movement indicated by arg */
36 static const char *join(Vis*, const char *keys, const Arg *arg);
37 /* execute arg->s as if it was typed on command prompt */
38 static const char *cmd(Vis*, const char *keys, const Arg *arg);
39 /* perform last action i.e. action_prev again */
40 static const char *repeat(Vis*, const char *keys, const Arg *arg);
41 /* replace character at cursor with one from keys */
42 static const char *replace(Vis*, const char *keys, const Arg *arg);
43 /* create a new cursor on the previous (arg->i < 0) or next (arg->i > 0) line */
44 static const char *cursors_new(Vis*, const char *keys, const Arg *arg);
45 /* try to align all cursors on the same column */
46 static const char *cursors_align(Vis*, const char *keys, const Arg *arg);
47 /* try to align all cursors by inserting the correct amount of white spaces */
48 static const char *cursors_align_indent(Vis*, const char *keys, const Arg *arg);
49 /* remove all but the primary cursor and their selections */
50 static const char *cursors_clear(Vis*, const char *keys, const Arg *arg);
51 /* remove the least recently added cursor */
52 static const char *cursors_remove(Vis*, const char *keys, const Arg *arg);
53 /* select the word the cursor is currently over */
54 static const char *cursors_select(Vis*, const char *keys, const Arg *arg);
55 /* select the next region matching the current selection */
56 static const char *cursors_select_next(Vis*, const char *keys, const Arg *arg);
57 /* clear current selection but select next match */
58 static const char *cursors_select_skip(Vis*, const char *keys, const Arg *arg);
59 /* adjust current used count according to keys */
60 static const char *count(Vis*, const char *keys, const Arg *arg);
61 /* move to the count-th line or if not given either to the first (arg->i < 0)
62 * or last (arg->i > 0) line of file */
63 static const char *gotoline(Vis*, const char *keys, const Arg *arg);
64 /* set motion type either LINEWISE or CHARWISE via arg->i */
65 static const char *motiontype(Vis*, const char *keys, const Arg *arg);
66 /* make the current action use the operator indicated by arg->i */
67 static const char *operator(Vis*, const char *keys, const Arg *arg);
68 /* use arg->s as command for the filter operator */
69 static const char *operator_filter(Vis*, const char *keys, const Arg *arg);
70 /* blocks to read a key and performs movement indicated by arg->i which
71 * should be one of VIS_MOVE_{RIGHT,LEFT}_{TO,TILL} */
72 static const char *movement_key(Vis*, const char *keys, const Arg *arg);
73 /* perform the movement as indicated by arg->i */
74 static const char *movement(Vis*, const char *keys, const Arg *arg);
75 /* let the current operator affect the range indicated by the text object arg->i */
76 static const char *textobj(Vis*, const char *keys, const Arg *arg);
77 /* move to the other end of selected text */
78 static const char *selection_end(Vis*, const char *keys, const Arg *arg);
79 /* restore least recently used selection */
80 static const char *selection_restore(Vis*, const char *keys, const Arg *arg);
81 /* use register indicated by keys for the current operator */
82 static const char *reg(Vis*, const char *keys, const Arg *arg);
83 /* perform arg->i motion with a mark indicated by keys as argument */
84 static const char *mark_motion(Vis*, const char *keys, const Arg *arg);
85 /* {un,re}do last action, redraw window */
86 static const char *undo(Vis*, const char *keys, const Arg *arg);
87 static const char *redo(Vis*, const char *keys, const Arg *arg);
88 /* earlier, later action chronologically, redraw window */
89 static const char *earlier(Vis*, const char *keys, const Arg *arg);
90 static const char *later(Vis*, const char *keys, const Arg *arg);
91 /* delete from the current cursor position to the end of
92 * movement as indicated by arg->i */
93 static const char *delete(Vis*, const char *keys, const Arg *arg);
94 /* insert register content indicated by keys at current cursor position */
95 static const char *insert_register(Vis*, const char *keys, const Arg *arg);
96 /* show a user prompt to get input with title arg->s */
97 static const char *prompt_show(Vis*, const char *keys, const Arg *arg);
98 /* blocks to read 3 consecutive digits and inserts the corresponding byte value */
99 static const char *insert_verbatim(Vis*, const char *keys, const Arg *arg);
100 /* scroll window content according to arg->i which can be either PAGE, PAGE_HALF,
101 * or an arbitrary number of lines. a multiplier overrides what is given in arg->i.
102 * negative values scroll back, positive forward. */
103 static const char *wscroll(Vis*, const char *keys, const Arg *arg);
104 /* similar to scroll, but do only move window content not cursor position */
105 static const char *wslide(Vis*, const char *keys, const Arg *arg);
106 /* call editor function as indicated by arg->f */
107 static const char *call(Vis*, const char *keys, const Arg *arg);
108 /* call window function as indicated by arg->w */
109 static const char *window(Vis*, const char *keys, const Arg *arg);
110 /* show info about Unicode character at cursor position */
111 static const char *unicode_info(Vis*, const char *keys, const Arg *arg);
112 /* either go to count % of ile or to matching item */
113 static const char *percent(Vis*, const char *keys, const Arg *arg);
114 /* either increment (arg->i > 0) or decrement (arg->i < 0) number under cursor */
115 static const char *number_increment_decrement(Vis*, const char *keys, const Arg *arg);
116 /* open a filename under cursor in same (!arg->b) or new (arg->b) window */
117 static const char *open_file_under_cursor(Vis*, const char *keys, const Arg *arg);
119 enum {
120 VIS_ACTION_EDITOR_SUSPEND,
121 VIS_ACTION_CURSOR_CHAR_PREV,
122 VIS_ACTION_CURSOR_CHAR_NEXT,
123 VIS_ACTION_CURSOR_WORD_START_PREV,
124 VIS_ACTION_CURSOR_WORD_START_NEXT,
125 VIS_ACTION_CURSOR_WORD_END_PREV,
126 VIS_ACTION_CURSOR_WORD_END_NEXT,
127 VIS_ACTION_CURSOR_LONGWORD_START_PREV,
128 VIS_ACTION_CURSOR_LONGWORD_START_NEXT,
129 VIS_ACTION_CURSOR_LONGWORD_END_PREV,
130 VIS_ACTION_CURSOR_LONGWORD_END_NEXT,
131 VIS_ACTION_CURSOR_LINE_UP,
132 VIS_ACTION_CURSOR_LINE_DOWN,
133 VIS_ACTION_CURSOR_LINE_START,
134 VIS_ACTION_CURSOR_LINE_FINISH,
135 VIS_ACTION_CURSOR_LINE_BEGIN,
136 VIS_ACTION_CURSOR_LINE_END,
137 VIS_ACTION_CURSOR_LINE_LASTCHAR,
138 VIS_ACTION_CURSOR_SCREEN_LINE_UP,
139 VIS_ACTION_CURSOR_SCREEN_LINE_DOWN,
140 VIS_ACTION_CURSOR_SCREEN_LINE_BEGIN,
141 VIS_ACTION_CURSOR_SCREEN_LINE_MIDDLE,
142 VIS_ACTION_CURSOR_SCREEN_LINE_END,
143 VIS_ACTION_CURSOR_PERCENT,
144 VIS_ACTION_CURSOR_PARAGRAPH_PREV,
145 VIS_ACTION_CURSOR_PARAGRAPH_NEXT,
146 VIS_ACTION_CURSOR_SENTENCE_PREV,
147 VIS_ACTION_CURSOR_SENTENCE_NEXT,
148 VIS_ACTION_CURSOR_FUNCTION_START_PREV,
149 VIS_ACTION_CURSOR_FUNCTION_END_PREV,
150 VIS_ACTION_CURSOR_FUNCTION_START_NEXT,
151 VIS_ACTION_CURSOR_FUNCTION_END_NEXT,
152 VIS_ACTION_CURSOR_COLUMN,
153 VIS_ACTION_CURSOR_LINE_FIRST,
154 VIS_ACTION_CURSOR_LINE_LAST,
155 VIS_ACTION_CURSOR_WINDOW_LINE_TOP,
156 VIS_ACTION_CURSOR_WINDOW_LINE_MIDDLE,
157 VIS_ACTION_CURSOR_WINDOW_LINE_BOTTOM,
158 VIS_ACTION_CURSOR_SEARCH_NEXT,
159 VIS_ACTION_CURSOR_SEARCH_PREV,
160 VIS_ACTION_CURSOR_SEARCH_WORD_FORWARD,
161 VIS_ACTION_CURSOR_SEARCH_WORD_BACKWARD,
162 VIS_ACTION_WINDOW_PAGE_UP,
163 VIS_ACTION_WINDOW_PAGE_DOWN,
164 VIS_ACTION_WINDOW_HALFPAGE_UP,
165 VIS_ACTION_WINDOW_HALFPAGE_DOWN,
166 VIS_ACTION_MODE_NORMAL,
167 VIS_ACTION_MODE_VISUAL,
168 VIS_ACTION_MODE_VISUAL_LINE,
169 VIS_ACTION_MODE_INSERT,
170 VIS_ACTION_MODE_REPLACE,
171 VIS_ACTION_MODE_OPERATOR_PENDING,
172 VIS_ACTION_DELETE_CHAR_PREV,
173 VIS_ACTION_DELETE_CHAR_NEXT,
174 VIS_ACTION_DELETE_LINE_BEGIN,
175 VIS_ACTION_DELETE_WORD_PREV,
176 VIS_ACTION_JUMPLIST_PREV,
177 VIS_ACTION_JUMPLIST_NEXT,
178 VIS_ACTION_CHANGELIST_PREV,
179 VIS_ACTION_CHANGELIST_NEXT,
180 VIS_ACTION_UNDO,
181 VIS_ACTION_REDO,
182 VIS_ACTION_EARLIER,
183 VIS_ACTION_LATER,
184 VIS_ACTION_MACRO_RECORD,
185 VIS_ACTION_MACRO_REPLAY,
186 VIS_ACTION_MARK_SET,
187 VIS_ACTION_MARK_GOTO,
188 VIS_ACTION_MARK_GOTO_LINE,
189 VIS_ACTION_REDRAW,
190 VIS_ACTION_REPLACE_CHAR,
191 VIS_ACTION_TOTILL_REPEAT,
192 VIS_ACTION_TOTILL_REVERSE,
193 VIS_ACTION_PROMPT_SEARCH_FORWARD,
194 VIS_ACTION_PROMPT_SEARCH_BACKWARD,
195 VIS_ACTION_TILL_LEFT,
196 VIS_ACTION_TILL_RIGHT,
197 VIS_ACTION_TO_LEFT,
198 VIS_ACTION_TO_RIGHT,
199 VIS_ACTION_REGISTER,
200 VIS_ACTION_OPERATOR_CHANGE,
201 VIS_ACTION_OPERATOR_DELETE,
202 VIS_ACTION_OPERATOR_YANK,
203 VIS_ACTION_OPERATOR_SHIFT_LEFT,
204 VIS_ACTION_OPERATOR_SHIFT_RIGHT,
205 VIS_ACTION_OPERATOR_CASE_LOWER,
206 VIS_ACTION_OPERATOR_CASE_UPPER,
207 VIS_ACTION_OPERATOR_CASE_SWAP,
208 VIS_ACTION_OPERATOR_FILTER,
209 VIS_ACTION_OPERATOR_FILTER_FMT,
210 VIS_ACTION_COUNT,
211 VIS_ACTION_INSERT_NEWLINE,
212 VIS_ACTION_INSERT_TAB,
213 VIS_ACTION_INSERT_VERBATIM,
214 VIS_ACTION_INSERT_REGISTER,
215 VIS_ACTION_WINDOW_NEXT,
216 VIS_ACTION_WINDOW_PREV,
217 VIS_ACTION_APPEND_CHAR_NEXT,
218 VIS_ACTION_APPEND_LINE_END,
219 VIS_ACTION_INSERT_LINE_START,
220 VIS_ACTION_OPEN_LINE_ABOVE,
221 VIS_ACTION_OPEN_LINE_BELOW,
222 VIS_ACTION_JOIN_LINE_BELOW,
223 VIS_ACTION_JOIN_LINES,
224 VIS_ACTION_PROMPT_SHOW,
225 VIS_ACTION_PROMPT_SHOW_VISUAL,
226 VIS_ACTION_REPEAT,
227 VIS_ACTION_SELECTION_FLIP,
228 VIS_ACTION_SELECTION_RESTORE,
229 VIS_ACTION_WINDOW_REDRAW_TOP,
230 VIS_ACTION_WINDOW_REDRAW_CENTER,
231 VIS_ACTION_WINDOW_REDRAW_BOTTOM,
232 VIS_ACTION_WINDOW_SLIDE_UP,
233 VIS_ACTION_WINDOW_SLIDE_DOWN,
234 VIS_ACTION_PUT_AFTER,
235 VIS_ACTION_PUT_BEFORE,
236 VIS_ACTION_PUT_AFTER_END,
237 VIS_ACTION_PUT_BEFORE_END,
238 VIS_ACTION_CURSOR_SELECT_WORD,
239 VIS_ACTION_CURSORS_NEW_LINE_ABOVE,
240 VIS_ACTION_CURSORS_NEW_LINE_BELOW,
241 VIS_ACTION_CURSORS_NEW_LINES_BEGIN,
242 VIS_ACTION_CURSORS_NEW_LINES_END,
243 VIS_ACTION_CURSORS_NEW_MATCH_NEXT,
244 VIS_ACTION_CURSORS_NEW_MATCH_SKIP,
245 VIS_ACTION_CURSORS_ALIGN,
246 VIS_ACTION_CURSORS_ALIGN_INDENT,
247 VIS_ACTION_CURSORS_REMOVE_ALL,
248 VIS_ACTION_CURSORS_REMOVE_LAST,
249 VIS_ACTION_TEXT_OBJECT_WORD_OUTER,
250 VIS_ACTION_TEXT_OBJECT_WORD_INNER,
251 VIS_ACTION_TEXT_OBJECT_LONGWORD_OUTER,
252 VIS_ACTION_TEXT_OBJECT_LONGWORD_INNER,
253 VIS_ACTION_TEXT_OBJECT_SENTENCE,
254 VIS_ACTION_TEXT_OBJECT_PARAGRAPH,
255 VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_OUTER,
256 VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_INNER,
257 VIS_ACTION_TEXT_OBJECT_PARANTHESE_OUTER,
258 VIS_ACTION_TEXT_OBJECT_PARANTHESE_INNER,
259 VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_OUTER,
260 VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_INNER,
261 VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_OUTER,
262 VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_INNER,
263 VIS_ACTION_TEXT_OBJECT_QUOTE_OUTER,
264 VIS_ACTION_TEXT_OBJECT_QUOTE_INNER,
265 VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_OUTER,
266 VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_INNER,
267 VIS_ACTION_TEXT_OBJECT_BACKTICK_OUTER,
268 VIS_ACTION_TEXT_OBJECT_BACKTICK_INNER,
269 VIS_ACTION_TEXT_OBJECT_ENTIRE_OUTER,
270 VIS_ACTION_TEXT_OBJECT_ENTIRE_INNER,
271 VIS_ACTION_TEXT_OBJECT_FUNCTION_OUTER,
272 VIS_ACTION_TEXT_OBJECT_FUNCTION_INNER,
273 VIS_ACTION_TEXT_OBJECT_LINE_OUTER,
274 VIS_ACTION_TEXT_OBJECT_LINE_INNER,
275 VIS_ACTION_TEXT_OBJECT_INDENTATION,
276 VIS_ACTION_TEXT_OBJECT_SEARCH_FORWARD,
277 VIS_ACTION_TEXT_OBJECT_SEARCH_BACKWARD,
278 VIS_ACTION_MOTION_CHARWISE,
279 VIS_ACTION_MOTION_LINEWISE,
280 VIS_ACTION_UNICODE_INFO,
281 VIS_ACTION_NUMBER_INCREMENT,
282 VIS_ACTION_NUMBER_DECREMENT,
283 VIS_ACTION_OPEN_FILE_UNDER_CURSOR,
284 VIS_ACTION_OPEN_FILE_UNDER_CURSOR_NEW_WINDOW,
285 VIS_ACTION_NOP,
288 static const KeyAction vis_action[] = {
289 [VIS_ACTION_EDITOR_SUSPEND] = {
290 "editor-suspend",
291 "Suspend the editor",
292 suspend,
294 [VIS_ACTION_CURSOR_CHAR_PREV] = {
295 "cursor-char-prev",
296 "Move cursor left, to the previous character",
297 movement, { .i = VIS_MOVE_CHAR_PREV }
299 [VIS_ACTION_CURSOR_CHAR_NEXT] = {
300 "cursor-char-next",
301 "Move cursor right, to the next character",
302 movement, { .i = VIS_MOVE_CHAR_NEXT }
304 [VIS_ACTION_CURSOR_WORD_START_PREV] = {
305 "cursor-word-start-prev",
306 "Move cursor words backwards",
307 movement, { .i = VIS_MOVE_WORD_START_PREV }
309 [VIS_ACTION_CURSOR_WORD_START_NEXT] = {
310 "cursor-word-start-next",
311 "Move cursor words forwards",
312 movement, { .i = VIS_MOVE_WORD_START_NEXT }
314 [VIS_ACTION_CURSOR_WORD_END_PREV] = {
315 "cursor-word-end-prev",
316 "Move cursor backwards to the end of word",
317 movement, { .i = VIS_MOVE_WORD_END_PREV }
319 [VIS_ACTION_CURSOR_WORD_END_NEXT] = {
320 "cursor-word-end-next",
321 "Move cursor forward to the end of word",
322 movement, { .i = VIS_MOVE_WORD_END_NEXT }
324 [VIS_ACTION_CURSOR_LONGWORD_START_PREV] = {
325 "cursor-longword-start-prev",
326 "Move cursor WORDS backwards",
327 movement, { .i = VIS_MOVE_LONGWORD_START_PREV }
329 [VIS_ACTION_CURSOR_LONGWORD_START_NEXT] = {
330 "cursor-longword-start-next",
331 "Move cursor WORDS forwards",
332 movement, { .i = VIS_MOVE_LONGWORD_START_NEXT }
334 [VIS_ACTION_CURSOR_LONGWORD_END_PREV] = {
335 "cursor-longword-end-prev",
336 "Move cursor backwards to the end of WORD",
337 movement, { .i = VIS_MOVE_LONGWORD_END_PREV }
339 [VIS_ACTION_CURSOR_LONGWORD_END_NEXT] = {
340 "cursor-longword-end-next",
341 "Move cursor forward to the end of WORD",
342 movement, { .i = VIS_MOVE_LONGWORD_END_NEXT }
344 [VIS_ACTION_CURSOR_LINE_UP] = {
345 "cursor-line-up",
346 "Move cursor line upwards",
347 movement, { .i = VIS_MOVE_LINE_UP }
349 [VIS_ACTION_CURSOR_LINE_DOWN] = {
350 "cursor-line-down",
351 "Move cursor line downwards",
352 movement, { .i = VIS_MOVE_LINE_DOWN }
354 [VIS_ACTION_CURSOR_LINE_START] = {
355 "cursor-line-start",
356 "Move cursor to first non-blank character of the line",
357 movement, { .i = VIS_MOVE_LINE_START }
359 [VIS_ACTION_CURSOR_LINE_FINISH] = {
360 "cursor-line-finish",
361 "Move cursor to last non-blank character of the line",
362 movement, { .i = VIS_MOVE_LINE_FINISH }
364 [VIS_ACTION_CURSOR_LINE_BEGIN] = {
365 "cursor-line-begin",
366 "Move cursor to first character of the line",
367 movement, { .i = VIS_MOVE_LINE_BEGIN }
369 [VIS_ACTION_CURSOR_LINE_END] = {
370 "cursor-line-end",
371 "Move cursor to end of the line",
372 movement, { .i = VIS_MOVE_LINE_END }
374 [VIS_ACTION_CURSOR_LINE_LASTCHAR] = {
375 "cursor-line-lastchar",
376 "Move cursor to last character of the line",
377 movement, { .i = VIS_MOVE_LINE_LASTCHAR }
379 [VIS_ACTION_CURSOR_SCREEN_LINE_UP] = {
380 "cursor-sceenline-up",
381 "Move cursor screen/display line upwards",
382 movement, { .i = VIS_MOVE_SCREEN_LINE_UP }
384 [VIS_ACTION_CURSOR_SCREEN_LINE_DOWN] = {
385 "cursor-screenline-down",
386 "Move cursor screen/display line downwards",
387 movement, { .i = VIS_MOVE_SCREEN_LINE_DOWN }
389 [VIS_ACTION_CURSOR_SCREEN_LINE_BEGIN] = {
390 "cursor-screenline-begin",
391 "Move cursor to beginning of screen/display line",
392 movement, { .i = VIS_MOVE_SCREEN_LINE_BEGIN }
394 [VIS_ACTION_CURSOR_SCREEN_LINE_MIDDLE] = {
395 "cursor-screenline-middle",
396 "Move cursor to middle of screen/display line",
397 movement, { .i = VIS_MOVE_SCREEN_LINE_MIDDLE }
399 [VIS_ACTION_CURSOR_SCREEN_LINE_END] = {
400 "cursor-screenline-end",
401 "Move cursor to end of screen/display line",
402 movement, { .i = VIS_MOVE_SCREEN_LINE_END }
404 [VIS_ACTION_CURSOR_PERCENT] = {
405 "cursor-percent",
406 "Move to count % of file or matching item",
407 percent
409 [VIS_ACTION_CURSOR_PARAGRAPH_PREV] = {
410 "cursor-paragraph-prev",
411 "Move cursor paragraph backward",
412 movement, { .i = VIS_MOVE_PARAGRAPH_PREV }
414 [VIS_ACTION_CURSOR_PARAGRAPH_NEXT] = {
415 "cursor-paragraph-next",
416 "Move cursor paragraph forward",
417 movement, { .i = VIS_MOVE_PARAGRAPH_NEXT }
419 [VIS_ACTION_CURSOR_SENTENCE_PREV] = {
420 "cursor-sentence-prev",
421 "Move cursor sentence backward",
422 movement, { .i = VIS_MOVE_SENTENCE_PREV }
424 [VIS_ACTION_CURSOR_SENTENCE_NEXT] = {
425 "cursor-sentence-next",
426 "Move cursor sentence forward",
427 movement, { .i = VIS_MOVE_SENTENCE_NEXT }
429 [VIS_ACTION_CURSOR_FUNCTION_START_PREV] = {
430 "cursor-function-start-prev",
431 "Move cursor backwards to start of function",
432 movement, { .i = VIS_MOVE_FUNCTION_START_PREV }
434 [VIS_ACTION_CURSOR_FUNCTION_START_NEXT] = {
435 "cursor-function-start-next",
436 "Move cursor forwards to start of function",
437 movement, { .i = VIS_MOVE_FUNCTION_START_NEXT }
439 [VIS_ACTION_CURSOR_FUNCTION_END_PREV] = {
440 "cursor-function-end-prev",
441 "Move cursor backwards to end of function",
442 movement, { .i = VIS_MOVE_FUNCTION_END_PREV }
444 [VIS_ACTION_CURSOR_FUNCTION_END_NEXT] = {
445 "cursor-function-end-next",
446 "Move cursor forwards to end of function",
447 movement, { .i = VIS_MOVE_FUNCTION_END_NEXT }
449 [VIS_ACTION_CURSOR_COLUMN] = {
450 "cursor-column",
451 "Move cursor to given column of current line",
452 movement, { .i = VIS_MOVE_COLUMN }
454 [VIS_ACTION_CURSOR_LINE_FIRST] = {
455 "cursor-line-first",
456 "Move cursor to given line (defaults to first)",
457 gotoline, { .i = -1 }
459 [VIS_ACTION_CURSOR_LINE_LAST] = {
460 "cursor-line-last",
461 "Move cursor to given line (defaults to last)",
462 gotoline, { .i = +1 }
464 [VIS_ACTION_CURSOR_WINDOW_LINE_TOP] = {
465 "cursor-window-line-top",
466 "Move cursor to top line of the window",
467 movement, { .i = VIS_MOVE_WINDOW_LINE_TOP }
469 [VIS_ACTION_CURSOR_WINDOW_LINE_MIDDLE] = {
470 "cursor-window-line-middle",
471 "Move cursor to middle line of the window",
472 movement, { .i = VIS_MOVE_WINDOW_LINE_MIDDLE }
474 [VIS_ACTION_CURSOR_WINDOW_LINE_BOTTOM] = {
475 "cursor-window-line-bottom",
476 "Move cursor to bottom line of the window",
477 movement, { .i = VIS_MOVE_WINDOW_LINE_BOTTOM }
479 [VIS_ACTION_CURSOR_SEARCH_NEXT] = {
480 "cursor-search-forward",
481 "Move cursor to bottom line of the window",
482 movement, { .i = VIS_MOVE_SEARCH_NEXT }
484 [VIS_ACTION_CURSOR_SEARCH_PREV] = {
485 "cursor-search-backward",
486 "Move cursor to bottom line of the window",
487 movement, { .i = VIS_MOVE_SEARCH_PREV }
489 [VIS_ACTION_CURSOR_SEARCH_WORD_FORWARD] = {
490 "cursor-search-word-forward",
491 "Move cursor to next occurence of the word under cursor",
492 movement, { .i = VIS_MOVE_SEARCH_WORD_FORWARD }
494 [VIS_ACTION_CURSOR_SEARCH_WORD_BACKWARD] = {
495 "cursor-search-word-backward",
496 "Move cursor to previous occurence of the word under cursor",
497 movement, { .i = VIS_MOVE_SEARCH_WORD_BACKWARD }
499 [VIS_ACTION_WINDOW_PAGE_UP] = {
500 "window-page-up",
501 "Scroll window pages backwards (upwards)",
502 wscroll, { .i = -PAGE }
504 [VIS_ACTION_WINDOW_HALFPAGE_UP] = {
505 "window-halfpage-up",
506 "Scroll window half pages backwards (upwards)",
507 wscroll, { .i = -PAGE_HALF }
509 [VIS_ACTION_WINDOW_PAGE_DOWN] = {
510 "window-page-down",
511 "Scroll window pages forwards (downwards)",
512 wscroll, { .i = +PAGE }
514 [VIS_ACTION_WINDOW_HALFPAGE_DOWN] = {
515 "window-halfpage-down",
516 "Scroll window half pages forwards (downwards)",
517 wscroll, { .i = +PAGE_HALF }
519 [VIS_ACTION_MODE_NORMAL] = {
520 "vis-mode-normal",
521 "Enter normal mode",
522 switchmode, { .i = VIS_MODE_NORMAL }
524 [VIS_ACTION_MODE_VISUAL] = {
525 "vis-mode-visual-charwise",
526 "Enter characterwise visual mode",
527 switchmode, { .i = VIS_MODE_VISUAL }
529 [VIS_ACTION_MODE_VISUAL_LINE] = {
530 "vis-mode-visual-linewise",
531 "Enter linewise visual mode",
532 switchmode, { .i = VIS_MODE_VISUAL_LINE }
534 [VIS_ACTION_MODE_INSERT] = {
535 "vis-mode-insert",
536 "Enter insert mode",
537 switchmode, { .i = VIS_MODE_INSERT }
539 [VIS_ACTION_MODE_REPLACE] = {
540 "vis-mode-replace",
541 "Enter replace mode",
542 switchmode, { .i = VIS_MODE_REPLACE }
544 [VIS_ACTION_MODE_OPERATOR_PENDING] = {
545 "vis-mode-operator-pending",
546 "Enter to operator pending mode",
547 switchmode, { .i = VIS_MODE_OPERATOR_PENDING }
549 [VIS_ACTION_DELETE_CHAR_PREV] = {
550 "delete-char-prev",
551 "Delete the previous character",
552 delete, { .i = VIS_MOVE_CHAR_PREV }
554 [VIS_ACTION_DELETE_CHAR_NEXT] = {
555 "delete-char-next",
556 "Delete the next character",
557 delete, { .i = VIS_MOVE_CHAR_NEXT }
559 [VIS_ACTION_DELETE_LINE_BEGIN] = {
560 "delete-line-begin",
561 "Delete until the start of the current line",
562 delete, { .i = VIS_MOVE_LINE_BEGIN }
564 [VIS_ACTION_DELETE_WORD_PREV] = {
565 "delete-word-prev",
566 "Delete the previous WORD",
567 delete, { .i = VIS_MOVE_LONGWORD_START_PREV }
569 [VIS_ACTION_JUMPLIST_PREV] = {
570 "jumplist-prev",
571 "Go to older cursor position in jump list",
572 movement, { .i = VIS_MOVE_JUMPLIST_PREV }
574 [VIS_ACTION_JUMPLIST_NEXT] = {
575 "jumplist-next",
576 "Go to newer cursor position in jump list",
577 movement, { .i = VIS_MOVE_JUMPLIST_NEXT }
579 [VIS_ACTION_CHANGELIST_PREV] = {
580 "changelist-prev",
581 "Go to older cursor position in change list",
582 movement, { .i = VIS_MOVE_CHANGELIST_PREV }
584 [VIS_ACTION_CHANGELIST_NEXT] = {
585 "changelist-next",
586 "Go to newer cursor position in change list",
587 movement, { .i = VIS_MOVE_CHANGELIST_NEXT }
589 [VIS_ACTION_UNDO] = {
590 "editor-undo",
591 "Undo last change",
592 undo,
594 [VIS_ACTION_REDO] = {
595 "editor-redo",
596 "Redo last change",
597 redo,
599 [VIS_ACTION_EARLIER] = {
600 "editor-earlier",
601 "Goto older text state",
602 earlier,
604 [VIS_ACTION_LATER] = {
605 "editor-later",
606 "Goto newer text state",
607 later,
609 [VIS_ACTION_MACRO_RECORD] = {
610 "macro-record",
611 "Record macro into given register",
612 macro_record,
614 [VIS_ACTION_MACRO_REPLAY] = {
615 "macro-replay",
616 "Replay macro, execute the content of the given register",
617 macro_replay,
619 [VIS_ACTION_MARK_SET] = {
620 "mark-set",
621 "Set given mark at current cursor position",
622 mark_set,
624 [VIS_ACTION_MARK_GOTO] = {
625 "mark-goto",
626 "Goto the position of the given mark",
627 mark_motion, { .i = VIS_MOVE_MARK }
629 [VIS_ACTION_MARK_GOTO_LINE] = {
630 "mark-goto-line",
631 "Goto first non-blank character of the line containing the given mark",
632 mark_motion, { .i = VIS_MOVE_MARK_LINE }
634 [VIS_ACTION_REDRAW] = {
635 "editor-redraw",
636 "Redraw current editor content",
637 call, { .f = vis_redraw }
639 [VIS_ACTION_REPLACE_CHAR] = {
640 "replace-char",
641 "Replace the character under the cursor",
642 replace,
644 [VIS_ACTION_TOTILL_REPEAT] = {
645 "totill-repeat",
646 "Repeat latest to/till motion",
647 movement, { .i = VIS_MOVE_TOTILL_REPEAT }
649 [VIS_ACTION_TOTILL_REVERSE] = {
650 "totill-reverse",
651 "Repeat latest to/till motion but in opposite direction",
652 movement, { .i = VIS_MOVE_TOTILL_REVERSE }
654 [VIS_ACTION_PROMPT_SEARCH_FORWARD] = {
655 "search-forward",
656 "Search forward",
657 prompt_show, { .s = "/" }
659 [VIS_ACTION_PROMPT_SEARCH_BACKWARD] = {
660 "search-backward",
661 "Search backward",
662 prompt_show, { .s = "?" }
664 [VIS_ACTION_TILL_LEFT] = {
665 "till-left",
666 "Till after the occurrence of character to the left",
667 movement_key, { .i = VIS_MOVE_LEFT_TILL }
669 [VIS_ACTION_TILL_RIGHT] = {
670 "till-right",
671 "Till before the occurrence of character to the right",
672 movement_key, { .i = VIS_MOVE_RIGHT_TILL }
674 [VIS_ACTION_TO_LEFT] = {
675 "to-left",
676 "To the first occurrence of character to the left",
677 movement_key, { .i = VIS_MOVE_LEFT_TO }
679 [VIS_ACTION_TO_RIGHT] = {
680 "to-right",
681 "To the first occurrence of character to the right",
682 movement_key, { .i = VIS_MOVE_RIGHT_TO }
684 [VIS_ACTION_REGISTER] = {
685 "register",
686 "Use given register for next operator",
687 reg,
689 [VIS_ACTION_OPERATOR_CHANGE] = {
690 "vis-operator-change",
691 "Change operator",
692 operator, { .i = VIS_OP_CHANGE }
694 [VIS_ACTION_OPERATOR_DELETE] = {
695 "vis-operator-delete",
696 "Delete operator",
697 operator, { .i = VIS_OP_DELETE }
699 [VIS_ACTION_OPERATOR_YANK] = {
700 "vis-operator-yank",
701 "Yank operator",
702 operator, { .i = VIS_OP_YANK }
704 [VIS_ACTION_OPERATOR_SHIFT_LEFT] = {
705 "vis-operator-shift-left",
706 "Shift left operator",
707 operator, { .i = VIS_OP_SHIFT_LEFT }
709 [VIS_ACTION_OPERATOR_SHIFT_RIGHT] = {
710 "vis-operator-shift-right",
711 "Shift right operator",
712 operator, { .i = VIS_OP_SHIFT_RIGHT }
714 [VIS_ACTION_OPERATOR_CASE_LOWER] = {
715 "vis-operator-case-lower",
716 "Lowercase operator",
717 operator, { .i = VIS_OP_CASE_LOWER }
719 [VIS_ACTION_OPERATOR_CASE_UPPER] = {
720 "vis-operator-case-upper",
721 "Uppercase operator",
722 operator, { .i = VIS_OP_CASE_UPPER }
724 [VIS_ACTION_OPERATOR_CASE_SWAP] = {
725 "vis-operator-case-swap",
726 "Swap case operator",
727 operator, { .i = VIS_OP_CASE_SWAP }
729 [VIS_ACTION_OPERATOR_FILTER] = {
730 "vis-operator-filter",
731 "Filter operator",
732 operator_filter,
734 [VIS_ACTION_OPERATOR_FILTER_FMT] = {
735 "vis-operator-filter-format",
736 "Formating operator, filter range through fmt(1)",
737 operator_filter, { .s = "'<,'>!fmt" }
739 [VIS_ACTION_COUNT] = {
740 "vis-count",
741 "Count specifier",
742 count,
744 [VIS_ACTION_INSERT_NEWLINE] = {
745 "insert-newline",
746 "Insert a line break (depending on file type)",
747 call, { .f = vis_insert_nl }
749 [VIS_ACTION_INSERT_TAB] = {
750 "insert-tab",
751 "Insert a tab (might be converted to spaces)",
752 call, { .f = vis_insert_tab }
754 [VIS_ACTION_INSERT_VERBATIM] = {
755 "insert-verbatim",
756 "Insert Unicode character based on code point",
757 insert_verbatim,
759 [VIS_ACTION_INSERT_REGISTER] = {
760 "insert-register",
761 "Insert specified register content",
762 insert_register,
764 [VIS_ACTION_WINDOW_NEXT] = {
765 "window-next",
766 "Focus next window",
767 call, { .f = vis_window_next }
769 [VIS_ACTION_WINDOW_PREV] = {
770 "window-prev",
771 "Focus previous window",
772 call, { .f = vis_window_prev }
774 [VIS_ACTION_APPEND_CHAR_NEXT] = {
775 "append-char-next",
776 "Append text after the cursor",
777 insertmode, { .i = VIS_MOVE_CHAR_NEXT }
779 [VIS_ACTION_APPEND_LINE_END] = {
780 "append-line-end",
781 "Append text after the end of the line",
782 insertmode, { .i = VIS_MOVE_LINE_END },
784 [VIS_ACTION_INSERT_LINE_START] = {
785 "insert-line-start",
786 "Insert text before the first non-blank in the line",
787 insertmode, { .i = VIS_MOVE_LINE_START },
789 [VIS_ACTION_OPEN_LINE_ABOVE] = {
790 "open-line-above",
791 "Begin a new line above the cursor",
792 openline, { .i = -1 }
794 [VIS_ACTION_OPEN_LINE_BELOW] = {
795 "open-line-below",
796 "Begin a new line below the cursor",
797 openline, { .i = +1 }
799 [VIS_ACTION_JOIN_LINE_BELOW] = {
800 "join-line-below",
801 "Join line(s)",
802 join, { .i = VIS_MOVE_LINE_NEXT },
804 [VIS_ACTION_JOIN_LINES] = {
805 "join-lines",
806 "Join selected lines",
807 operator, { .i = VIS_OP_JOIN }
809 [VIS_ACTION_PROMPT_SHOW] = {
810 "prompt-show",
811 "Show editor command line prompt",
812 prompt_show, { .s = ":" }
814 [VIS_ACTION_PROMPT_SHOW_VISUAL] = {
815 "prompt-show-visual",
816 "Show editor command line prompt in visual mode",
817 prompt_show, { .s = ":'<,'>" }
819 [VIS_ACTION_REPEAT] = {
820 "editor-repeat",
821 "Repeat latest editor command",
822 repeat
824 [VIS_ACTION_SELECTION_FLIP] = {
825 "selection-flip",
826 "Flip selection, move cursor to other end",
827 selection_end,
829 [VIS_ACTION_SELECTION_RESTORE] = {
830 "selection-restore",
831 "Restore last selection",
832 selection_restore,
834 [VIS_ACTION_WINDOW_REDRAW_TOP] = {
835 "window-redraw-top",
836 "Redraw cursor line at the top of the window",
837 window, { .w = view_redraw_top }
839 [VIS_ACTION_WINDOW_REDRAW_CENTER] = {
840 "window-redraw-center",
841 "Redraw cursor line at the center of the window",
842 window, { .w = view_redraw_center }
844 [VIS_ACTION_WINDOW_REDRAW_BOTTOM] = {
845 "window-redraw-bottom",
846 "Redraw cursor line at the bottom of the window",
847 window, { .w = view_redraw_bottom }
849 [VIS_ACTION_WINDOW_SLIDE_UP] = {
850 "window-slide-up",
851 "Slide window content upwards",
852 wslide, { .i = -1 }
854 [VIS_ACTION_WINDOW_SLIDE_DOWN] = {
855 "window-slide-down",
856 "Slide window content downwards",
857 wslide, { .i = +1 }
859 [VIS_ACTION_PUT_AFTER] = {
860 "put-after",
861 "Put text after the cursor",
862 operator, { .i = VIS_OP_PUT_AFTER }
864 [VIS_ACTION_PUT_BEFORE] = {
865 "put-before",
866 "Put text before the cursor",
867 operator, { .i = VIS_OP_PUT_BEFORE }
869 [VIS_ACTION_PUT_AFTER_END] = {
870 "put-after-end",
871 "Put text after the cursor, place cursor after new text",
872 operator, { .i = VIS_OP_PUT_AFTER_END }
874 [VIS_ACTION_PUT_BEFORE_END] = {
875 "put-before-end",
876 "Put text before the cursor, place cursor after new text",
877 operator, { .i = VIS_OP_PUT_BEFORE_END }
879 [VIS_ACTION_CURSOR_SELECT_WORD] = {
880 "cursors-select-word",
881 "Select word under cursor",
882 cursors_select,
884 [VIS_ACTION_CURSORS_NEW_LINE_ABOVE] = {
885 "cursors-new-lines-above",
886 "Create a new cursor on the line above",
887 cursors_new, { .i = -1 }
889 [VIS_ACTION_CURSORS_NEW_LINE_BELOW] = {
890 "cursor-new-lines-below",
891 "Create a new cursor on the line below",
892 cursors_new, { .i = +1 }
894 [VIS_ACTION_CURSORS_NEW_LINES_BEGIN] = {
895 "cursors-new-lines-begin",
896 "Create a new cursor at the start of every line covered by selection",
897 operator, { .i = VIS_OP_CURSOR_SOL }
899 [VIS_ACTION_CURSORS_NEW_LINES_END] = {
900 "cursors-new-lines-end",
901 "Create a new cursor at the end of every line covered by selection",
902 operator, { .i = VIS_OP_CURSOR_EOL }
904 [VIS_ACTION_CURSORS_NEW_MATCH_NEXT] = {
905 "cursors-new-match-next",
906 "Select the next region matching the current selection",
907 cursors_select_next
909 [VIS_ACTION_CURSORS_NEW_MATCH_SKIP] = {
910 "cursors-new-match-skip",
911 "Clear current selection, but select next match",
912 cursors_select_skip,
914 [VIS_ACTION_CURSORS_ALIGN] = {
915 "cursors-align",
916 "Try to align all cursors on the same column",
917 cursors_align,
919 [VIS_ACTION_CURSORS_ALIGN_INDENT] = {
920 "cursors-align-indent",
921 "Try to align all cursors by inserting spaces",
922 cursors_align_indent,
924 [VIS_ACTION_CURSORS_REMOVE_ALL] = {
925 "cursors-remove-all",
926 "Remove all but the primary cursor",
927 cursors_clear,
929 [VIS_ACTION_CURSORS_REMOVE_LAST] = {
930 "cursors-remove-last",
931 "Remove least recently created cursor",
932 cursors_remove,
934 [VIS_ACTION_TEXT_OBJECT_WORD_OUTER] = {
935 "text-object-word-outer",
936 "A word leading and trailing whitespace included",
937 textobj, { .i = VIS_TEXTOBJECT_OUTER_WORD }
939 [VIS_ACTION_TEXT_OBJECT_WORD_INNER] = {
940 "text-object-word-inner",
941 "A word leading and trailing whitespace excluded",
942 textobj, { .i = VIS_TEXTOBJECT_INNER_WORD }
944 [VIS_ACTION_TEXT_OBJECT_LONGWORD_OUTER] = {
945 "text-object-longword-outer",
946 "A WORD leading and trailing whitespace included",
947 textobj, { .i = VIS_TEXTOBJECT_OUTER_LONGWORD }
949 [VIS_ACTION_TEXT_OBJECT_LONGWORD_INNER] = {
950 "text-object-longword-inner",
951 "A WORD leading and trailing whitespace excluded",
952 textobj, { .i = VIS_TEXTOBJECT_INNER_LONGWORD }
954 [VIS_ACTION_TEXT_OBJECT_SENTENCE] = {
955 "text-object-sentence",
956 "A sentence",
957 textobj, { .i = VIS_TEXTOBJECT_SENTENCE }
959 [VIS_ACTION_TEXT_OBJECT_PARAGRAPH] = {
960 "text-object-paragraph",
961 "A paragraph",
962 textobj, { .i = VIS_TEXTOBJECT_PARAGRAPH }
964 [VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_OUTER] = {
965 "text-object-square-bracket-outer",
966 "[] block (outer variant)",
967 textobj, { .i = VIS_TEXTOBJECT_OUTER_SQUARE_BRACKET }
969 [VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_INNER] = {
970 "text-object-square-bracket-inner",
971 "[] block (inner variant)",
972 textobj, { .i = VIS_TEXTOBJECT_INNER_SQUARE_BRACKET }
974 [VIS_ACTION_TEXT_OBJECT_PARANTHESE_OUTER] = {
975 "text-object-parentheses-outer",
976 "() block (outer variant)",
977 textobj, { .i = VIS_TEXTOBJECT_OUTER_PARANTHESE }
979 [VIS_ACTION_TEXT_OBJECT_PARANTHESE_INNER] = {
980 "text-object-parentheses-inner",
981 "() block (inner variant)",
982 textobj, { .i = VIS_TEXTOBJECT_INNER_PARANTHESE }
984 [VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_OUTER] = {
985 "text-object-angle-bracket-outer",
986 "<> block (outer variant)",
987 textobj, { .i = VIS_TEXTOBJECT_OUTER_ANGLE_BRACKET }
989 [VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_INNER] = {
990 "text-object-angle-bracket-inner",
991 "<> block (inner variant)",
992 textobj, { .i = VIS_TEXTOBJECT_INNER_ANGLE_BRACKET }
994 [VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_OUTER] = {
995 "text-object-curly-bracket-outer",
996 "{} block (outer variant)",
997 textobj, { .i = VIS_TEXTOBJECT_OUTER_CURLY_BRACKET }
999 [VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_INNER] = {
1000 "text-object-curly-bracket-inner",
1001 "{} block (inner variant)",
1002 textobj, { .i = VIS_TEXTOBJECT_INNER_CURLY_BRACKET }
1004 [VIS_ACTION_TEXT_OBJECT_QUOTE_OUTER] = {
1005 "text-object-quote-outer",
1006 "A quoted string, including the quotation marks",
1007 textobj, { .i = VIS_TEXTOBJECT_OUTER_QUOTE }
1009 [VIS_ACTION_TEXT_OBJECT_QUOTE_INNER] = {
1010 "text-object-quote-inner",
1011 "A quoted string, excluding the quotation marks",
1012 textobj, { .i = VIS_TEXTOBJECT_INNER_QUOTE }
1014 [VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_OUTER] = {
1015 "text-object-single-quote-outer",
1016 "A single quoted string, including the quotation marks",
1017 textobj, { .i = VIS_TEXTOBJECT_OUTER_SINGLE_QUOTE }
1019 [VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_INNER] = {
1020 "text-object-single-quote-inner",
1021 "A single quoted string, excluding the quotation marks",
1022 textobj, { .i = VIS_TEXTOBJECT_INNER_SINGLE_QUOTE }
1024 [VIS_ACTION_TEXT_OBJECT_BACKTICK_OUTER] = {
1025 "text-object-backtick-outer",
1026 "A backtick delimited string (outer variant)",
1027 textobj, { .i = VIS_TEXTOBJECT_OUTER_BACKTICK }
1029 [VIS_ACTION_TEXT_OBJECT_BACKTICK_INNER] = {
1030 "text-object-backtick-inner",
1031 "A backtick delimited string (inner variant)",
1032 textobj, { .i = VIS_TEXTOBJECT_INNER_BACKTICK }
1034 [VIS_ACTION_TEXT_OBJECT_ENTIRE_OUTER] = {
1035 "text-object-entire-outer",
1036 "The whole text content",
1037 textobj, { .i = VIS_TEXTOBJECT_OUTER_ENTIRE }
1039 [VIS_ACTION_TEXT_OBJECT_ENTIRE_INNER] = {
1040 "text-object-entire-inner",
1041 "The whole text content, except for leading and trailing empty lines",
1042 textobj, { .i = VIS_TEXTOBJECT_INNER_ENTIRE }
1044 [VIS_ACTION_TEXT_OBJECT_FUNCTION_OUTER] = {
1045 "text-object-function-outer",
1046 "A whole C-like function",
1047 textobj, { .i = VIS_TEXTOBJECT_OUTER_FUNCTION }
1049 [VIS_ACTION_TEXT_OBJECT_FUNCTION_INNER] = {
1050 "text-object-function-inner",
1051 "A whole C-like function body",
1052 textobj, { .i = VIS_TEXTOBJECT_INNER_FUNCTION }
1054 [VIS_ACTION_TEXT_OBJECT_LINE_OUTER] = {
1055 "text-object-line-outer",
1056 "The whole line",
1057 textobj, { .i = VIS_TEXTOBJECT_OUTER_LINE }
1059 [VIS_ACTION_TEXT_OBJECT_LINE_INNER] = {
1060 "text-object-line-inner",
1061 "The whole line, excluding leading and trailing whitespace",
1062 textobj, { .i = VIS_TEXTOBJECT_INNER_LINE }
1064 [VIS_ACTION_TEXT_OBJECT_INDENTATION] = {
1065 "text-object-indentation",
1066 "All adjacent lines with the same indentation level as the current one",
1067 textobj, { .i = VIS_TEXTOBJECT_INDENTATION }
1069 [VIS_ACTION_TEXT_OBJECT_SEARCH_FORWARD] = {
1070 "text-object-search-forward",
1071 "The next search match in forward direction",
1072 textobj, { .i = VIS_TEXTOBJECT_SEARCH_FORWARD }
1074 [VIS_ACTION_TEXT_OBJECT_SEARCH_BACKWARD] = {
1075 "text-object-search-backward",
1076 "The next search match in backward direction",
1077 textobj, { .i = VIS_TEXTOBJECT_SEARCH_BACKWARD }
1079 [VIS_ACTION_MOTION_CHARWISE] = {
1080 "motion-charwise",
1081 "Force motion to be charwise",
1082 motiontype, { .i = VIS_MOTIONTYPE_CHARWISE }
1084 [VIS_ACTION_MOTION_LINEWISE] = {
1085 "motion-linewise",
1086 "Force motion to be linewise",
1087 motiontype, { .i = VIS_MOTIONTYPE_LINEWISE }
1089 [VIS_ACTION_UNICODE_INFO] = {
1090 "unicode-info",
1091 "Show Unicode codepoint(s) of character under cursor",
1092 unicode_info,
1094 [VIS_ACTION_NUMBER_INCREMENT] = {
1095 "number-increment",
1096 "Increment number under cursor",
1097 number_increment_decrement, { .i = +1 }
1099 [VIS_ACTION_NUMBER_DECREMENT] = {
1100 "number-decrement",
1101 "Decrement number under cursor",
1102 number_increment_decrement, { .i = -1 }
1104 [VIS_ACTION_OPEN_FILE_UNDER_CURSOR] = {
1105 "open-file-under-cursor",
1106 "Open file under the cursor",
1107 open_file_under_cursor, { .b = false }
1109 [VIS_ACTION_OPEN_FILE_UNDER_CURSOR_NEW_WINDOW] = {
1110 "open-file-under-cursor-new-cursor",
1111 "Open file under the cursor in a new window",
1112 open_file_under_cursor, { .b = true }
1114 [VIS_ACTION_NOP] = {
1115 "nop",
1116 "Ignore key, do nothing",
1117 nop,
1121 #include "config.h"
1123 /** key bindings functions */
1125 static const char *nop(Vis *vis, const char *keys, const Arg *arg) {
1126 return keys;
1129 static const char *key2macro(Vis *vis, const char *keys, enum VisMacro *macro) {
1130 *macro = VIS_MACRO_INVALID;
1131 if (keys[0] >= 'a' && keys[0] <= 'z')
1132 *macro = keys[0] - 'a';
1133 else if (keys[0] == '@')
1134 *macro = VIS_MACRO_LAST_RECORDED;
1135 else if (keys[0] == '\0')
1136 return NULL;
1137 return keys+1;
1140 static const char *macro_record(Vis *vis, const char *keys, const Arg *arg) {
1141 if (vis_macro_record_stop(vis))
1142 return keys;
1143 enum VisMacro macro;
1144 keys = key2macro(vis, keys, &macro);
1145 vis_macro_record(vis, macro);
1146 vis_draw(vis);
1147 return keys;
1150 static const char *macro_replay(Vis *vis, const char *keys, const Arg *arg) {
1151 enum VisMacro macro;
1152 keys = key2macro(vis, keys, &macro);
1153 vis_macro_replay(vis, macro);
1154 return keys;
1157 static const char *suspend(Vis *vis, const char *keys, const Arg *arg) {
1158 vis_suspend(vis);
1159 return keys;
1162 static const char *repeat(Vis *vis, const char *keys, const Arg *arg) {
1163 vis_repeat(vis);
1164 return keys;
1167 static const char *cursors_new(Vis *vis, const char *keys, const Arg *arg) {
1168 View *view = vis_view(vis);
1169 size_t pos = view_cursor_get(view);
1170 Cursor *cursor = view_cursors_new(view);
1171 if (cursor) {
1172 view_cursors_to(cursor, pos);
1173 if (arg->i > 0)
1174 view_line_down(cursor);
1175 else if (arg->i < 0)
1176 view_line_up(cursor);
1178 return keys;
1181 static const char *cursors_align(Vis *vis, const char *keys, const Arg *arg) {
1182 View *view = vis_view(vis);
1183 Text *txt = vis_text(vis);
1184 int mincol = INT_MAX;
1185 for (Cursor *c = view_cursors(view); c; c = view_cursors_next(c)) {
1186 int col = view_cursors_cell_get(c);
1187 if (col >= 0 && col < mincol)
1188 mincol = col;
1190 for (Cursor *c = view_cursors(view); c; c = view_cursors_next(c)) {
1191 if (view_cursors_cell_set(c, mincol) == -1) {
1192 size_t pos = view_cursors_pos(c);
1193 size_t col = text_line_width_set(txt, pos, mincol);
1194 view_cursors_to(c, col);
1197 return keys;
1200 static const char *cursors_align_indent(Vis *vis, const char *keys, const Arg *arg) {
1201 View *view = vis_view(vis);
1202 Text *txt = vis_text(vis);
1203 int mincol = INT_MAX, maxcol = 0;
1205 for (Cursor *c = view_cursors(view); c; c = view_cursors_next(c)) {
1206 int col = text_line_width_get(txt, view_cursors_pos(c));
1207 if (col < mincol)
1208 mincol = col;
1209 if (col > maxcol)
1210 maxcol = col;
1213 size_t len = maxcol - mincol;
1214 char *buf = malloc(len+1);
1215 if (!buf)
1216 return keys;
1217 memset(buf, ' ', len);
1219 for (Cursor *c = view_cursors(view); c; c = view_cursors_next(c)) {
1220 size_t pos = view_cursors_pos(c);
1221 int col = text_line_width_get(txt, pos);
1222 if (col < maxcol) {
1223 size_t off = maxcol - col;
1224 if (off <= len)
1225 text_insert(txt, pos, buf, off);
1229 view_draw(view);
1230 free(buf);
1231 return keys;
1234 static const char *cursors_clear(Vis *vis, const char *keys, const Arg *arg) {
1235 View *view = vis_view(vis);
1236 if (view_cursors_count(view) > 1)
1237 view_cursors_clear(view);
1238 else
1239 view_cursors_selection_clear(view_cursor(view));
1240 return keys;
1243 static const char *cursors_select(Vis *vis, const char *keys, const Arg *arg) {
1244 Text *txt = vis_text(vis);
1245 View *view = vis_view(vis);
1246 for (Cursor *cursor = view_cursors(view); cursor; cursor = view_cursors_next(cursor)) {
1247 Filerange sel = view_cursors_selection_get(cursor);
1248 Filerange word = text_object_word(txt, view_cursors_pos(cursor));
1249 if (!text_range_valid(&sel) && text_range_valid(&word)) {
1250 view_cursors_selection_set(cursor, &word);
1251 view_cursors_to(cursor, text_char_prev(txt, word.end));
1254 vis_mode_switch(vis, VIS_MODE_VISUAL);
1255 return keys;
1258 static const char *cursors_select_next(Vis *vis, const char *keys, const Arg *arg) {
1259 Text *txt = vis_text(vis);
1260 View *view = vis_view(vis);
1261 Cursor *cursor = view_cursor(view);
1262 Filerange sel = view_cursors_selection_get(cursor);
1263 if (!text_range_valid(&sel))
1264 return keys;
1266 char *buf = text_bytes_alloc0(txt, sel.start, text_range_size(&sel));
1267 if (!buf)
1268 return keys;
1269 Filerange word = text_object_word_find_next(txt, sel.end, buf);
1270 free(buf);
1272 if (text_range_valid(&word)) {
1273 cursor = view_cursors_new(view);
1274 if (!cursor)
1275 return keys;
1276 view_cursors_selection_set(cursor, &word);
1277 view_cursors_to(cursor, text_char_prev(txt, word.end));
1279 return keys;
1282 static const char *cursors_select_skip(Vis *vis, const char *keys, const Arg *arg) {
1283 View *view = vis_view(vis);
1284 Cursor *cursor = view_cursor(view);
1285 keys = cursors_select_next(vis, keys, arg);
1286 if (cursor != view_cursor(view))
1287 view_cursors_dispose(cursor);
1288 return keys;
1291 static const char *cursors_remove(Vis *vis, const char *keys, const Arg *arg) {
1292 View *view = vis_view(vis);
1293 view_cursors_dispose(view_cursor(view));
1294 view_cursor_to(view, view_cursor_get(view));
1295 return keys;
1298 static const char *replace(Vis *vis, const char *keys, const Arg *arg) {
1299 if (!keys[0])
1300 return NULL;
1301 const char *next = vis_keys_next(vis, keys);
1302 if (!next)
1303 return NULL;
1304 size_t len = next - keys;
1305 char key[len+1];
1306 memcpy(key, keys, len);
1307 key[len] = '\0';
1308 vis_operator(vis, VIS_OP_REPLACE);
1309 vis_motion(vis, VIS_MOVE_NOP);
1310 vis_keys_inject(vis, next, key);
1311 vis_keys_inject(vis, next+len, "<Escape>");
1312 return next;
1315 static const char *count(Vis *vis, const char *keys, const Arg *arg) {
1316 int digit = keys[-1] - '0';
1317 int count = vis_count_get_default(vis, 0);
1318 if (0 <= digit && digit <= 9) {
1319 if (digit == 0 && count == 0)
1320 vis_motion(vis, VIS_MOVE_LINE_BEGIN);
1321 vis_count_set(vis, count * 10 + digit);
1323 return keys;
1326 static const char *gotoline(Vis *vis, const char *keys, const Arg *arg) {
1327 if (vis_count_get(vis) != VIS_COUNT_UNKNOWN)
1328 vis_motion(vis, VIS_MOVE_LINE);
1329 else if (arg->i < 0)
1330 vis_motion(vis, VIS_MOVE_FILE_BEGIN);
1331 else
1332 vis_motion(vis, VIS_MOVE_FILE_END);
1333 return keys;
1336 static const char *motiontype(Vis *vis, const char *keys, const Arg *arg) {
1337 vis_motion_type(vis, arg->i);
1338 return keys;
1341 static const char *operator(Vis *vis, const char *keys, const Arg *arg) {
1342 vis_operator(vis, arg->i);
1343 return keys;
1346 static const char *operator_filter(Vis *vis, const char *keys, const Arg *arg) {
1347 vis_operator(vis, VIS_OP_FILTER, arg->s);
1348 return keys;
1351 static const char *movement_key(Vis *vis, const char *keys, const Arg *arg) {
1352 if (!keys[0])
1353 return NULL;
1354 char key[32];
1355 const char *next = vis_keys_next(vis, keys);
1356 strncpy(key, keys, next - keys + 1);
1357 key[sizeof(key)-1] = '\0';
1358 vis_motion(vis, arg->i, key);
1359 return next;
1362 static const char *movement(Vis *vis, const char *keys, const Arg *arg) {
1363 vis_motion(vis, arg->i);
1364 return keys;
1367 static const char *textobj(Vis *vis, const char *keys, const Arg *arg) {
1368 vis_textobject(vis, arg->i);
1369 return keys;
1372 static const char *selection_end(Vis *vis, const char *keys, const Arg *arg) {
1373 for (Cursor *c = view_cursors(vis_view(vis)); c; c = view_cursors_next(c))
1374 view_cursors_selection_swap(c);
1375 return keys;
1378 static const char *selection_restore(Vis *vis, const char *keys, const Arg *arg) {
1379 Text *txt = vis_text(vis);
1380 View *view = vis_view(vis);
1381 for (Cursor *c = view_cursors(view); c; c = view_cursors_next(c))
1382 view_cursors_selection_restore(c);
1383 Filerange sel = view_selection_get(view);
1384 if (text_range_is_linewise(txt, &sel))
1385 vis_mode_switch(vis, VIS_MODE_VISUAL_LINE);
1386 else
1387 vis_mode_switch(vis, VIS_MODE_VISUAL);
1388 return keys;
1391 static const char *key2register(Vis *vis, const char *keys, enum VisRegister *reg) {
1392 *reg = VIS_REG_INVALID;
1393 if (!keys[0])
1394 return NULL;
1395 if (keys[0] >= 'a' && keys[0] <= 'z')
1396 *reg = keys[0] - 'a';
1397 else if (keys[0] >= 'A' && keys[0] <= 'Z')
1398 *reg = VIS_REG_A + keys[0] - 'A';
1399 else if (keys[0] == '*' || keys[0] == '+')
1400 *reg = VIS_REG_CLIPBOARD;
1401 else if (keys[0] == '_')
1402 *reg = VIS_REG_BLACKHOLE;
1403 else if (keys[0] == '0')
1404 *reg = VIS_REG_ZERO;
1405 return keys+1;
1408 static const char *reg(Vis *vis, const char *keys, const Arg *arg) {
1409 enum VisRegister reg;
1410 keys = key2register(vis, keys, &reg);
1411 vis_register_set(vis, reg);
1412 return keys;
1415 static const char *key2mark(Vis *vis, const char *keys, int *mark) {
1416 *mark = VIS_MARK_INVALID;
1417 if (!keys[0])
1418 return NULL;
1419 if (keys[0] >= 'a' && keys[0] <= 'z')
1420 *mark = keys[0] - 'a';
1421 else if (keys[0] == '<')
1422 *mark = VIS_MARK_SELECTION_START;
1423 else if (keys[0] == '>')
1424 *mark = VIS_MARK_SELECTION_END;
1425 return keys+1;
1428 static const char *mark_set(Vis *vis, const char *keys, const Arg *arg) {
1429 int mark;
1430 keys = key2mark(vis, keys, &mark);
1431 vis_mark_set(vis, mark, view_cursor_get(vis_view(vis)));
1432 return keys;
1435 static const char *mark_motion(Vis *vis, const char *keys, const Arg *arg) {
1436 int mark;
1437 keys = key2mark(vis, keys, &mark);
1438 vis_motion(vis, arg->i, mark);
1439 return keys;
1442 static const char *undo(Vis *vis, const char *keys, const Arg *arg) {
1443 size_t pos = text_undo(vis_text(vis));
1444 if (pos != EPOS) {
1445 View *view = vis_view(vis);
1446 if (view_cursors_count(view) == 1)
1447 view_cursor_to(view, pos);
1448 /* redraw all windows in case some display the same file */
1449 vis_draw(vis);
1451 return keys;
1454 static const char *redo(Vis *vis, const char *keys, const Arg *arg) {
1455 size_t pos = text_redo(vis_text(vis));
1456 if (pos != EPOS) {
1457 View *view = vis_view(vis);
1458 if (view_cursors_count(view) == 1)
1459 view_cursor_to(view, pos);
1460 /* redraw all windows in case some display the same file */
1461 vis_draw(vis);
1463 return keys;
1466 static const char *earlier(Vis *vis, const char *keys, const Arg *arg) {
1467 size_t pos = text_earlier(vis_text(vis), vis_count_get_default(vis, 1));
1468 if (pos != EPOS) {
1469 view_cursor_to(vis_view(vis), pos);
1470 /* redraw all windows in case some display the same file */
1471 vis_draw(vis);
1473 return keys;
1476 static const char *later(Vis *vis, const char *keys, const Arg *arg) {
1477 size_t pos = text_later(vis_text(vis), vis_count_get_default(vis, 1));
1478 if (pos != EPOS) {
1479 view_cursor_to(vis_view(vis), pos);
1480 /* redraw all windows in case some display the same file */
1481 vis_draw(vis);
1483 return keys;
1486 static const char *delete(Vis *vis, const char *keys, const Arg *arg) {
1487 vis_operator(vis, VIS_OP_DELETE);
1488 vis_motion(vis, arg->i);
1489 return keys;
1492 static const char *insert_register(Vis *vis, const char *keys, const Arg *arg) {
1493 enum VisRegister regid;
1494 keys = key2register(vis, keys, &regid);
1495 size_t len;
1496 const char *data = vis_register_get(vis, regid, &len);
1497 vis_insert_key(vis, data, len);
1498 return keys;
1501 static const char *prompt_show(Vis *vis, const char *keys, const Arg *arg) {
1502 vis_prompt_show(vis, arg->s);
1503 return keys;
1506 static const char *insert_verbatim(Vis *vis, const char *keys, const Arg *arg) {
1507 Rune rune = 0;
1508 char buf[4], type = keys[0];
1509 const char *data = NULL;
1510 int len = 0, count = 0, base = 0;
1511 switch (type) {
1512 case '\0':
1513 return NULL;
1514 case 'o':
1515 case 'O':
1516 count = 3;
1517 base = 8;
1518 break;
1519 case 'U':
1520 count = 4;
1521 /* fall through */
1522 case 'u':
1523 count += 4;
1524 base = 16;
1525 break;
1526 case 'x':
1527 case 'X':
1528 count = 2;
1529 base = 16;
1530 break;
1531 default:
1532 if ('0' <= type && type <= '9') {
1533 rune = type - '0';
1534 count = 2;
1535 base = 10;
1537 break;
1540 if (base) {
1541 for (keys++; keys[0] && count > 0; keys++, count--) {
1542 int v = 0;
1543 if (base == 8 && '0' <= keys[0] && keys[0] <= '7') {
1544 v = keys[0] - '0';
1545 } else if ((base == 10 || base == 16) && '0' <= keys[0] && keys[0] <= '9') {
1546 v = keys[0] - '0';
1547 } else if (base == 16 && 'a' <= keys[0] && keys[0] <= 'f') {
1548 v = 10 + keys[0] - 'a';
1549 } else if (base == 16 && 'A' <= keys[0] && keys[0] <= 'F') {
1550 v = 10 + keys[0] - 'A';
1551 } else {
1552 count = 0;
1553 break;
1555 rune = rune * base + v;
1558 if (count > 0)
1559 return NULL;
1560 if (type == 'u' || type == 'U') {
1561 len = runetochar(buf, &rune);
1562 } else {
1563 buf[0] = rune;
1564 len = 1;
1567 data = buf;
1568 } else {
1569 const char *next = vis_keys_next(vis, keys);
1570 if (!next)
1571 return NULL;
1572 size_t keylen = next - keys;
1573 char key[keylen+1];
1574 memcpy(key, keys, keylen);
1575 key[keylen] = '\0';
1577 static const char *keysym[] = {
1578 "<Enter>", "\n",
1579 "<Tab>", "\t",
1580 "<Backspace>", "\b",
1581 "<Escape>", "\x1b",
1582 "<DEL>", "\x7f",
1583 NULL,
1586 for (const char **k = keysym; k[0]; k += 2) {
1587 if (strcmp(k[0], key) == 0) {
1588 data = k[1];
1589 len = strlen(data);
1590 keys = next;
1591 break;
1596 if (len > 0)
1597 vis_insert_key(vis, data, len);
1598 return keys;
1601 static const char *cmd(Vis *vis, const char *keys, const Arg *arg) {
1602 vis_cmd(vis, arg->s);
1603 return keys;
1606 static int argi2lines(Vis *vis, const Arg *arg) {
1607 int count = vis_count_get(vis);
1608 switch (arg->i) {
1609 case -PAGE:
1610 case +PAGE:
1611 return view_height_get(vis_view(vis));
1612 case -PAGE_HALF:
1613 case +PAGE_HALF:
1614 return view_height_get(vis_view(vis))/2;
1615 default:
1616 if (count != VIS_COUNT_UNKNOWN)
1617 return count;
1618 return arg->i < 0 ? -arg->i : arg->i;
1622 static const char *wscroll(Vis *vis, const char *keys, const Arg *arg) {
1623 if (arg->i >= 0)
1624 view_scroll_down(vis_view(vis), argi2lines(vis, arg));
1625 else
1626 view_scroll_up(vis_view(vis), argi2lines(vis, arg));
1627 return keys;
1630 static const char *wslide(Vis *vis, const char *keys, const Arg *arg) {
1631 if (arg->i >= 0)
1632 view_slide_down(vis_view(vis), argi2lines(vis, arg));
1633 else
1634 view_slide_up(vis_view(vis), argi2lines(vis, arg));
1635 return keys;
1638 static const char *call(Vis *vis, const char *keys, const Arg *arg) {
1639 arg->f(vis);
1640 return keys;
1643 static const char *window(Vis *vis, const char *keys, const Arg *arg) {
1644 arg->w(vis_view(vis));
1645 return keys;
1648 static const char *openline(Vis *vis, const char *keys, const Arg *arg) {
1649 vis_operator(vis, VIS_OP_INSERT);
1650 if (arg->i > 0) {
1651 vis_motion(vis, VIS_MOVE_LINE_END);
1652 vis_keys_inject(vis, keys, "<insert-newline>");
1653 } else {
1654 vis_motion(vis, VIS_MOVE_LINE_BEGIN);
1655 vis_keys_inject(vis, keys, "<insert-newline><Up>");
1657 return keys;
1660 static const char *join(Vis *vis, const char *keys, const Arg *arg) {
1661 int count = vis_count_get_default(vis, 0);
1662 if (count)
1663 vis_count_set(vis, count-1);
1664 vis_operator(vis, VIS_OP_JOIN);
1665 vis_motion(vis, arg->i);
1666 return keys;
1669 static const char *switchmode(Vis *vis, const char *keys, const Arg *arg) {
1670 vis_mode_switch(vis, arg->i);
1671 return keys;
1674 static const char *insertmode(Vis *vis, const char *keys, const Arg *arg) {
1675 vis_operator(vis, VIS_OP_INSERT);
1676 vis_motion(vis, arg->i);
1677 return keys;
1680 static const char *unicode_info(Vis *vis, const char *keys, const Arg *arg) {
1681 View *view = vis_view(vis);
1682 Text *txt = vis_text(vis);
1683 size_t start = view_cursor_get(view);
1684 size_t end = text_char_next(txt, start);
1685 char data[end-start], *data_cur = data;
1686 text_bytes_get(txt, start, end - start, data);
1687 Iterator it = text_iterator_get(txt, start);
1688 char info[255] = "", *info_cur = info;
1689 for (size_t pos = start; it.pos < end; pos = it.pos) {
1690 text_iterator_codepoint_next(&it, NULL);
1691 size_t len = it.pos - pos;
1692 wchar_t wc = 0xFFFD;
1693 mbtowc(&wc, data_cur, len);
1694 int width = wcwidth(wc);
1695 info_cur += snprintf(info_cur, sizeof(info) - (info_cur - info) - 1,
1696 "<%s%.*s> U+%04x ", width == 0 ? " " : "", (int)len, data_cur, wc);
1697 data_cur += len;
1699 vis_info_show(vis, "%s", info);
1700 return keys;
1703 static const char *percent(Vis *vis, const char *keys, const Arg *arg) {
1704 if (vis_count_get(vis) == VIS_COUNT_UNKNOWN)
1705 vis_motion(vis, VIS_MOVE_BRACKET_MATCH);
1706 else
1707 vis_motion(vis, VIS_MOVE_PERCENT);
1708 return keys;
1711 static const char *number_increment_decrement(Vis *vis, const char *keys, const Arg *arg) {
1712 View *view = vis_view(vis);
1713 Text *txt = vis_text(vis);
1715 int delta = arg->i;
1716 int count = vis_count_get(vis);
1717 if (count != 0 && count != VIS_COUNT_UNKNOWN)
1718 delta *= count;
1720 for (Cursor *c = view_cursors(view); c; c = view_cursors_next(c)) {
1721 Filerange r = text_object_number(txt, view_cursors_pos(c));
1722 if (!text_range_valid(&r))
1723 continue;
1724 char *buf = text_bytes_alloc0(txt, r.start, text_range_size(&r));
1725 if (buf) {
1726 char *number = buf, fmt[255];
1727 if (number[0] == '-')
1728 number++;
1729 bool octal = number[0] == '0' && ('0' <= number[1] && number[1] <= '7');
1730 bool hex = number[0] == '0' && (number[1] == 'x' || number[1] == 'X');
1731 bool dec = !hex && !octal;
1733 long long value = strtoll(buf, NULL, 0);
1734 value += delta;
1735 if (dec) {
1736 snprintf(fmt, sizeof fmt, "%lld", value);
1737 } else if (hex) {
1738 size_t len = strlen(number) - 2;
1739 snprintf(fmt, sizeof fmt, "0x%0*llx", (int)len, value);
1740 } else {
1741 size_t len = strlen(number) - 1;
1742 snprintf(fmt, sizeof fmt, "0%0*llo", (int)len, value);
1744 text_delete_range(txt, &r);
1745 text_insert(txt, r.start, fmt, strlen(fmt));
1746 view_cursors_to(c, r.start);
1748 free(buf);
1751 vis_cancel(vis);
1753 return keys;
1756 static const char *open_file_under_cursor(Vis *vis, const char *keys, const Arg *arg) {
1757 Win *win = vis_window(vis);
1758 View *view = vis_view(vis);
1759 Text *txt = vis_text(vis);
1761 if (!arg->b && !vis_window_closable(win)) {
1762 vis_info_show(vis, "No write since last change");
1763 return keys;
1766 for (Cursor *c = view_cursors(view); c; c = view_cursors_next(c)) {
1767 Filerange r = text_object_filename(txt, view_cursors_pos(c));
1768 if (!text_range_valid(&r))
1769 continue;
1770 char *name = text_bytes_alloc0(txt, r.start, text_range_size(&r));
1771 if (!name)
1772 continue;
1774 struct stat st;
1775 if (stat(name, &st) == -1) {
1776 vis_info_show(vis, "File `%s' not found", name);
1777 free(name);
1778 continue;
1781 if (!vis_window_new(vis, name)) {
1782 vis_info_show(vis, "Failed to open `%s': %s", name, strerror(errno));
1783 free(name);
1784 continue;
1785 } else if (!arg->b) {
1786 vis_window_close(win);
1787 free(name);
1788 return keys;
1791 free(name);
1794 return keys;
1797 static Vis *vis;
1799 static void signal_handler(int signum, siginfo_t *siginfo, void *context) {
1800 vis_signal_handler(vis, signum, siginfo, context);
1803 int main(int argc, char *argv[]) {
1805 VisEvent event = {
1806 .vis_start = vis_lua_start,
1807 .vis_quit = vis_lua_quit,
1808 .file_open = vis_lua_file_open,
1809 .file_save = vis_lua_file_save,
1810 .file_close = vis_lua_file_close,
1811 .win_open = vis_lua_win_open,
1812 .win_close = vis_lua_win_close,
1815 vis = vis_new(ui_curses_new(), &event);
1816 if (!vis)
1817 return EXIT_FAILURE;
1819 for (int i = 0; i < LENGTH(vis_action); i++) {
1820 const KeyAction *action = &vis_action[i];
1821 if (!vis_action_register(vis, action))
1822 vis_die(vis, "Could not register action: %s\n", action->name);
1825 for (int i = 0; i < LENGTH(default_bindings); i++) {
1826 for (const KeyBinding **binding = default_bindings[i]; binding && *binding; binding++) {
1827 for (const KeyBinding *kb = *binding; kb->key; kb++) {
1828 vis_mode_map(vis, i, kb->key, kb);
1833 for (const char **k = keymaps; k[0]; k += 2)
1834 vis_keymap_add(vis, k[0], k[1]);
1836 /* install signal handlers etc. */
1837 struct sigaction sa;
1838 memset(&sa, 0, sizeof sa);
1839 sa.sa_flags = SA_SIGINFO;
1840 sa.sa_sigaction = signal_handler;
1841 if (sigaction(SIGBUS, &sa, NULL) || sigaction(SIGINT, &sa, NULL))
1842 vis_die(vis, "sigaction: %s", strerror(errno));
1844 sigset_t blockset;
1845 sigemptyset(&blockset);
1846 sigaddset(&blockset, SIGWINCH);
1847 sigprocmask(SIG_BLOCK, &blockset, NULL);
1848 signal(SIGPIPE, SIG_IGN);
1850 int status = vis_run(vis, argc, argv);
1851 vis_free(vis);
1852 return status;