10 #include "text-util.h"
11 #include "text-motions.h"
12 #include "text-objects.h"
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 /* remove all but the primary cursor and their selections */
48 static const char *cursors_clear(Vis
*, const char *keys
, const Arg
*arg
);
49 /* remove the least recently added cursor */
50 static const char *cursors_remove(Vis
*, const char *keys
, const Arg
*arg
);
51 /* select the word the cursor is currently over */
52 static const char *cursors_select(Vis
*, const char *keys
, const Arg
*arg
);
53 /* select the next region matching the current selection */
54 static const char *cursors_select_next(Vis
*, const char *keys
, const Arg
*arg
);
55 /* clear current selection but select next match */
56 static const char *cursors_select_skip(Vis
*, const char *keys
, const Arg
*arg
);
57 /* adjust current used count according to keys */
58 static const char *count(Vis
*, const char *keys
, const Arg
*arg
);
59 /* move to the count-th line or if not given either to the first (arg->i < 0)
60 * or last (arg->i > 0) line of file */
61 static const char *gotoline(Vis
*, const char *keys
, const Arg
*arg
);
62 /* set motion type either LINEWISE or CHARWISE via arg->i */
63 static const char *motiontype(Vis
*, const char *keys
, const Arg
*arg
);
64 /* make the current action use the operator indicated by arg->i */
65 static const char *operator(Vis
*, const char *keys
, const Arg
*arg
);
66 /* use arg->s as command for the filter operator */
67 static const char *operator_filter(Vis
*, const char *keys
, const Arg
*arg
);
68 /* blocks to read a key and performs movement indicated by arg->i which
69 * should be one of VIS_MOVE_{RIGHT,LEFT}_{TO,TILL} */
70 static const char *movement_key(Vis
*, const char *keys
, const Arg
*arg
);
71 /* perform the movement as indicated by arg->i */
72 static const char *movement(Vis
*, const char *keys
, const Arg
*arg
);
73 /* let the current operator affect the range indicated by the text object arg->i */
74 static const char *textobj(Vis
*, const char *keys
, const Arg
*arg
);
75 /* move to the other end of selected text */
76 static const char *selection_end(Vis
*, const char *keys
, const Arg
*arg
);
77 /* restore least recently used selection */
78 static const char *selection_restore(Vis
*, const char *keys
, const Arg
*arg
);
79 /* use register indicated by keys for the current operator */
80 static const char *reg(Vis
*, const char *keys
, const Arg
*arg
);
81 /* perform arg->i motion with a mark indicated by keys as argument */
82 static const char *mark_motion(Vis
*, const char *keys
, const Arg
*arg
);
83 /* {un,re}do last action, redraw window */
84 static const char *undo(Vis
*, const char *keys
, const Arg
*arg
);
85 static const char *redo(Vis
*, const char *keys
, const Arg
*arg
);
86 /* earlier, later action chronologically, redraw window */
87 static const char *earlier(Vis
*, const char *keys
, const Arg
*arg
);
88 static const char *later(Vis
*, const char *keys
, const Arg
*arg
);
89 /* delete from the current cursor position to the end of
90 * movement as indicated by arg->i */
91 static const char *delete(Vis
*, const char *keys
, const Arg
*arg
);
92 /* insert register content indicated by keys at current cursor position */
93 static const char *insert_register(Vis
*, const char *keys
, const Arg
*arg
);
94 /* show a user prompt to get input with title arg->s */
95 static const char *prompt_show(Vis
*, const char *keys
, const Arg
*arg
);
96 /* blocks to read 3 consecutive digits and inserts the corresponding byte value */
97 static const char *insert_verbatim(Vis
*, const char *keys
, const Arg
*arg
);
98 /* scroll window content according to arg->i which can be either PAGE, PAGE_HALF,
99 * or an arbitrary number of lines. a multiplier overrides what is given in arg->i.
100 * negative values scroll back, positive forward. */
101 static const char *wscroll(Vis
*, const char *keys
, const Arg
*arg
);
102 /* similar to scroll, but do only move window content not cursor position */
103 static const char *wslide(Vis
*, const char *keys
, const Arg
*arg
);
104 /* call editor function as indicated by arg->f */
105 static const char *call(Vis
*, const char *keys
, const Arg
*arg
);
106 /* call window function as indicated by arg->w */
107 static const char *window(Vis
*, const char *keys
, const Arg
*arg
);
108 /* show info about Unicode character at cursor position */
109 static const char *unicode_info(Vis
*, const char *keys
, const Arg
*arg
);
110 /* either go to count % of ile or to matching item */
111 static const char *percent(Vis
*, const char *keys
, const Arg
*arg
);
112 /* either increment (arg->i > 0) or decrement (arg->i < 0) number under cursor */
113 static const char *number_increment_decrement(Vis
*, const char *keys
, const Arg
*arg
);
116 VIS_ACTION_EDITOR_SUSPEND
,
117 VIS_ACTION_CURSOR_CHAR_PREV
,
118 VIS_ACTION_CURSOR_CHAR_NEXT
,
119 VIS_ACTION_CURSOR_WORD_START_PREV
,
120 VIS_ACTION_CURSOR_WORD_START_NEXT
,
121 VIS_ACTION_CURSOR_WORD_END_PREV
,
122 VIS_ACTION_CURSOR_WORD_END_NEXT
,
123 VIS_ACTION_CURSOR_LONGWORD_START_PREV
,
124 VIS_ACTION_CURSOR_LONGWORD_START_NEXT
,
125 VIS_ACTION_CURSOR_LONGWORD_END_PREV
,
126 VIS_ACTION_CURSOR_LONGWORD_END_NEXT
,
127 VIS_ACTION_CURSOR_LINE_UP
,
128 VIS_ACTION_CURSOR_LINE_DOWN
,
129 VIS_ACTION_CURSOR_LINE_START
,
130 VIS_ACTION_CURSOR_LINE_FINISH
,
131 VIS_ACTION_CURSOR_LINE_BEGIN
,
132 VIS_ACTION_CURSOR_LINE_END
,
133 VIS_ACTION_CURSOR_LINE_LASTCHAR
,
134 VIS_ACTION_CURSOR_SCREEN_LINE_UP
,
135 VIS_ACTION_CURSOR_SCREEN_LINE_DOWN
,
136 VIS_ACTION_CURSOR_SCREEN_LINE_BEGIN
,
137 VIS_ACTION_CURSOR_SCREEN_LINE_MIDDLE
,
138 VIS_ACTION_CURSOR_SCREEN_LINE_END
,
139 VIS_ACTION_CURSOR_PERCENT
,
140 VIS_ACTION_CURSOR_PARAGRAPH_PREV
,
141 VIS_ACTION_CURSOR_PARAGRAPH_NEXT
,
142 VIS_ACTION_CURSOR_SENTENCE_PREV
,
143 VIS_ACTION_CURSOR_SENTENCE_NEXT
,
144 VIS_ACTION_CURSOR_FUNCTION_START_PREV
,
145 VIS_ACTION_CURSOR_FUNCTION_END_PREV
,
146 VIS_ACTION_CURSOR_FUNCTION_START_NEXT
,
147 VIS_ACTION_CURSOR_FUNCTION_END_NEXT
,
148 VIS_ACTION_CURSOR_COLUMN
,
149 VIS_ACTION_CURSOR_LINE_FIRST
,
150 VIS_ACTION_CURSOR_LINE_LAST
,
151 VIS_ACTION_CURSOR_WINDOW_LINE_TOP
,
152 VIS_ACTION_CURSOR_WINDOW_LINE_MIDDLE
,
153 VIS_ACTION_CURSOR_WINDOW_LINE_BOTTOM
,
154 VIS_ACTION_CURSOR_SEARCH_NEXT
,
155 VIS_ACTION_CURSOR_SEARCH_PREV
,
156 VIS_ACTION_CURSOR_SEARCH_WORD_FORWARD
,
157 VIS_ACTION_CURSOR_SEARCH_WORD_BACKWARD
,
158 VIS_ACTION_WINDOW_PAGE_UP
,
159 VIS_ACTION_WINDOW_PAGE_DOWN
,
160 VIS_ACTION_WINDOW_HALFPAGE_UP
,
161 VIS_ACTION_WINDOW_HALFPAGE_DOWN
,
162 VIS_ACTION_MODE_NORMAL
,
163 VIS_ACTION_MODE_VISUAL
,
164 VIS_ACTION_MODE_VISUAL_LINE
,
165 VIS_ACTION_MODE_INSERT
,
166 VIS_ACTION_MODE_REPLACE
,
167 VIS_ACTION_MODE_OPERATOR_PENDING
,
168 VIS_ACTION_DELETE_CHAR_PREV
,
169 VIS_ACTION_DELETE_CHAR_NEXT
,
170 VIS_ACTION_DELETE_LINE_BEGIN
,
171 VIS_ACTION_DELETE_WORD_PREV
,
172 VIS_ACTION_JUMPLIST_PREV
,
173 VIS_ACTION_JUMPLIST_NEXT
,
174 VIS_ACTION_CHANGELIST_PREV
,
175 VIS_ACTION_CHANGELIST_NEXT
,
180 VIS_ACTION_MACRO_RECORD
,
181 VIS_ACTION_MACRO_REPLAY
,
183 VIS_ACTION_MARK_GOTO
,
184 VIS_ACTION_MARK_GOTO_LINE
,
186 VIS_ACTION_REPLACE_CHAR
,
187 VIS_ACTION_TOTILL_REPEAT
,
188 VIS_ACTION_TOTILL_REVERSE
,
189 VIS_ACTION_PROMPT_SEARCH_FORWARD
,
190 VIS_ACTION_PROMPT_SEARCH_BACKWARD
,
191 VIS_ACTION_TILL_LEFT
,
192 VIS_ACTION_TILL_RIGHT
,
196 VIS_ACTION_OPERATOR_CHANGE
,
197 VIS_ACTION_OPERATOR_DELETE
,
198 VIS_ACTION_OPERATOR_YANK
,
199 VIS_ACTION_OPERATOR_SHIFT_LEFT
,
200 VIS_ACTION_OPERATOR_SHIFT_RIGHT
,
201 VIS_ACTION_OPERATOR_CASE_LOWER
,
202 VIS_ACTION_OPERATOR_CASE_UPPER
,
203 VIS_ACTION_OPERATOR_CASE_SWAP
,
204 VIS_ACTION_OPERATOR_FILTER
,
205 VIS_ACTION_OPERATOR_FILTER_FMT
,
207 VIS_ACTION_INSERT_NEWLINE
,
208 VIS_ACTION_INSERT_TAB
,
209 VIS_ACTION_INSERT_VERBATIM
,
210 VIS_ACTION_INSERT_REGISTER
,
211 VIS_ACTION_WINDOW_NEXT
,
212 VIS_ACTION_WINDOW_PREV
,
213 VIS_ACTION_APPEND_CHAR_NEXT
,
214 VIS_ACTION_APPEND_LINE_END
,
215 VIS_ACTION_INSERT_LINE_START
,
216 VIS_ACTION_OPEN_LINE_ABOVE
,
217 VIS_ACTION_OPEN_LINE_BELOW
,
218 VIS_ACTION_JOIN_LINE_BELOW
,
219 VIS_ACTION_JOIN_LINES
,
220 VIS_ACTION_PROMPT_SHOW
,
221 VIS_ACTION_PROMPT_SHOW_VISUAL
,
223 VIS_ACTION_SELECTION_FLIP
,
224 VIS_ACTION_SELECTION_RESTORE
,
225 VIS_ACTION_WINDOW_REDRAW_TOP
,
226 VIS_ACTION_WINDOW_REDRAW_CENTER
,
227 VIS_ACTION_WINDOW_REDRAW_BOTTOM
,
228 VIS_ACTION_WINDOW_SLIDE_UP
,
229 VIS_ACTION_WINDOW_SLIDE_DOWN
,
230 VIS_ACTION_PUT_AFTER
,
231 VIS_ACTION_PUT_BEFORE
,
232 VIS_ACTION_PUT_AFTER_END
,
233 VIS_ACTION_PUT_BEFORE_END
,
234 VIS_ACTION_CURSOR_SELECT_WORD
,
235 VIS_ACTION_CURSORS_NEW_LINE_ABOVE
,
236 VIS_ACTION_CURSORS_NEW_LINE_BELOW
,
237 VIS_ACTION_CURSORS_NEW_LINES_BEGIN
,
238 VIS_ACTION_CURSORS_NEW_LINES_END
,
239 VIS_ACTION_CURSORS_NEW_MATCH_NEXT
,
240 VIS_ACTION_CURSORS_NEW_MATCH_SKIP
,
241 VIS_ACTION_CURSORS_ALIGN
,
242 VIS_ACTION_CURSORS_REMOVE_ALL
,
243 VIS_ACTION_CURSORS_REMOVE_LAST
,
244 VIS_ACTION_TEXT_OBJECT_WORD_OUTER
,
245 VIS_ACTION_TEXT_OBJECT_WORD_INNER
,
246 VIS_ACTION_TEXT_OBJECT_LONGWORD_OUTER
,
247 VIS_ACTION_TEXT_OBJECT_LONGWORD_INNER
,
248 VIS_ACTION_TEXT_OBJECT_SENTENCE
,
249 VIS_ACTION_TEXT_OBJECT_PARAGRAPH
,
250 VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_OUTER
,
251 VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_INNER
,
252 VIS_ACTION_TEXT_OBJECT_PARANTHESE_OUTER
,
253 VIS_ACTION_TEXT_OBJECT_PARANTHESE_INNER
,
254 VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_OUTER
,
255 VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_INNER
,
256 VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_OUTER
,
257 VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_INNER
,
258 VIS_ACTION_TEXT_OBJECT_QUOTE_OUTER
,
259 VIS_ACTION_TEXT_OBJECT_QUOTE_INNER
,
260 VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_OUTER
,
261 VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_INNER
,
262 VIS_ACTION_TEXT_OBJECT_BACKTICK_OUTER
,
263 VIS_ACTION_TEXT_OBJECT_BACKTICK_INNER
,
264 VIS_ACTION_TEXT_OBJECT_ENTIRE_OUTER
,
265 VIS_ACTION_TEXT_OBJECT_ENTIRE_INNER
,
266 VIS_ACTION_TEXT_OBJECT_FUNCTION_OUTER
,
267 VIS_ACTION_TEXT_OBJECT_FUNCTION_INNER
,
268 VIS_ACTION_TEXT_OBJECT_LINE_OUTER
,
269 VIS_ACTION_TEXT_OBJECT_LINE_INNER
,
270 VIS_ACTION_MOTION_CHARWISE
,
271 VIS_ACTION_MOTION_LINEWISE
,
272 VIS_ACTION_UNICODE_INFO
,
273 VIS_ACTION_NUMBER_INCREMENT
,
274 VIS_ACTION_NUMBER_DECREMENT
,
278 static KeyAction vis_action
[] = {
279 [VIS_ACTION_EDITOR_SUSPEND
] = {
281 "Suspend the editor",
284 [VIS_ACTION_CURSOR_CHAR_PREV
] = {
286 "Move cursor left, to the previous character",
287 movement
, { .i
= VIS_MOVE_CHAR_PREV
}
289 [VIS_ACTION_CURSOR_CHAR_NEXT
] = {
291 "Move cursor right, to the next character",
292 movement
, { .i
= VIS_MOVE_CHAR_NEXT
}
294 [VIS_ACTION_CURSOR_WORD_START_PREV
] = {
295 "cursor-word-start-prev",
296 "Move cursor words backwards",
297 movement
, { .i
= VIS_MOVE_WORD_START_PREV
}
299 [VIS_ACTION_CURSOR_WORD_START_NEXT
] = {
300 "cursor-word-start-next",
301 "Move cursor words forwards",
302 movement
, { .i
= VIS_MOVE_WORD_START_NEXT
}
304 [VIS_ACTION_CURSOR_WORD_END_PREV
] = {
305 "cursor-word-end-prev",
306 "Move cursor backwards to the end of word",
307 movement
, { .i
= VIS_MOVE_WORD_END_PREV
}
309 [VIS_ACTION_CURSOR_WORD_END_NEXT
] = {
310 "cursor-word-end-next",
311 "Move cursor forward to the end of word",
312 movement
, { .i
= VIS_MOVE_WORD_END_NEXT
}
314 [VIS_ACTION_CURSOR_LONGWORD_START_PREV
] = {
315 "cursor-longword-start-prev",
316 "Move cursor WORDS backwards",
317 movement
, { .i
= VIS_MOVE_LONGWORD_START_PREV
}
319 [VIS_ACTION_CURSOR_LONGWORD_START_NEXT
] = {
320 "cursor-longword-start-next",
321 "Move cursor WORDS forwards",
322 movement
, { .i
= VIS_MOVE_LONGWORD_START_NEXT
}
324 [VIS_ACTION_CURSOR_LONGWORD_END_PREV
] = {
325 "cursor-longword-end-prev",
326 "Move cursor backwards to the end of WORD",
327 movement
, { .i
= VIS_MOVE_LONGWORD_END_PREV
}
329 [VIS_ACTION_CURSOR_LONGWORD_END_NEXT
] = {
330 "cursor-longword-end-next",
331 "Move cursor forward to the end of WORD",
332 movement
, { .i
= VIS_MOVE_LONGWORD_END_NEXT
}
334 [VIS_ACTION_CURSOR_LINE_UP
] = {
336 "Move cursor line upwards",
337 movement
, { .i
= VIS_MOVE_LINE_UP
}
339 [VIS_ACTION_CURSOR_LINE_DOWN
] = {
341 "Move cursor line downwards",
342 movement
, { .i
= VIS_MOVE_LINE_DOWN
}
344 [VIS_ACTION_CURSOR_LINE_START
] = {
346 "Move cursor to first non-blank character of the line",
347 movement
, { .i
= VIS_MOVE_LINE_START
}
349 [VIS_ACTION_CURSOR_LINE_FINISH
] = {
350 "cursor-line-finish",
351 "Move cursor to last non-blank character of the line",
352 movement
, { .i
= VIS_MOVE_LINE_FINISH
}
354 [VIS_ACTION_CURSOR_LINE_BEGIN
] = {
356 "Move cursor to first character of the line",
357 movement
, { .i
= VIS_MOVE_LINE_BEGIN
}
359 [VIS_ACTION_CURSOR_LINE_END
] = {
361 "Move cursor to end of the line",
362 movement
, { .i
= VIS_MOVE_LINE_END
}
364 [VIS_ACTION_CURSOR_LINE_LASTCHAR
] = {
365 "cursor-line-lastchar",
366 "Move cursor to last character of the line",
367 movement
, { .i
= VIS_MOVE_LINE_LASTCHAR
}
369 [VIS_ACTION_CURSOR_SCREEN_LINE_UP
] = {
370 "cursor-sceenline-up",
371 "Move cursor screen/display line upwards",
372 movement
, { .i
= VIS_MOVE_SCREEN_LINE_UP
}
374 [VIS_ACTION_CURSOR_SCREEN_LINE_DOWN
] = {
375 "cursor-screenline-down",
376 "Move cursor screen/display line downwards",
377 movement
, { .i
= VIS_MOVE_SCREEN_LINE_DOWN
}
379 [VIS_ACTION_CURSOR_SCREEN_LINE_BEGIN
] = {
380 "cursor-screenline-begin",
381 "Move cursor to beginning of screen/display line",
382 movement
, { .i
= VIS_MOVE_SCREEN_LINE_BEGIN
}
384 [VIS_ACTION_CURSOR_SCREEN_LINE_MIDDLE
] = {
385 "cursor-screenline-middle",
386 "Move cursor to middle of screen/display line",
387 movement
, { .i
= VIS_MOVE_SCREEN_LINE_MIDDLE
}
389 [VIS_ACTION_CURSOR_SCREEN_LINE_END
] = {
390 "cursor-screenline-end",
391 "Move cursor to end of screen/display line",
392 movement
, { .i
= VIS_MOVE_SCREEN_LINE_END
}
394 [VIS_ACTION_CURSOR_PERCENT
] = {
396 "Move to count % of file or matching item",
399 [VIS_ACTION_CURSOR_PARAGRAPH_PREV
] = {
400 "cursor-paragraph-prev",
401 "Move cursor paragraph backward",
402 movement
, { .i
= VIS_MOVE_PARAGRAPH_PREV
}
404 [VIS_ACTION_CURSOR_PARAGRAPH_NEXT
] = {
405 "cursor-paragraph-next",
406 "Move cursor paragraph forward",
407 movement
, { .i
= VIS_MOVE_PARAGRAPH_NEXT
}
409 [VIS_ACTION_CURSOR_SENTENCE_PREV
] = {
410 "cursor-sentence-prev",
411 "Move cursor sentence backward",
412 movement
, { .i
= VIS_MOVE_SENTENCE_PREV
}
414 [VIS_ACTION_CURSOR_SENTENCE_NEXT
] = {
415 "cursor-sentence-next",
416 "Move cursor sentence forward",
417 movement
, { .i
= VIS_MOVE_SENTENCE_NEXT
}
419 [VIS_ACTION_CURSOR_FUNCTION_START_PREV
] = {
420 "cursor-function-start-prev",
421 "Move cursor backwards to start of function",
422 movement
, { .i
= VIS_MOVE_FUNCTION_START_PREV
}
424 [VIS_ACTION_CURSOR_FUNCTION_START_NEXT
] = {
425 "cursor-function-start-next",
426 "Move cursor forwards to start of function",
427 movement
, { .i
= VIS_MOVE_FUNCTION_START_NEXT
}
429 [VIS_ACTION_CURSOR_FUNCTION_END_PREV
] = {
430 "cursor-function-end-prev",
431 "Move cursor backwards to end of function",
432 movement
, { .i
= VIS_MOVE_FUNCTION_END_PREV
}
434 [VIS_ACTION_CURSOR_FUNCTION_END_NEXT
] = {
435 "cursor-function-end-next",
436 "Move cursor forwards to end of function",
437 movement
, { .i
= VIS_MOVE_FUNCTION_END_NEXT
}
439 [VIS_ACTION_CURSOR_COLUMN
] = {
441 "Move cursor to given column of current line",
442 movement
, { .i
= VIS_MOVE_COLUMN
}
444 [VIS_ACTION_CURSOR_LINE_FIRST
] = {
446 "Move cursor to given line (defaults to first)",
447 gotoline
, { .i
= -1 }
449 [VIS_ACTION_CURSOR_LINE_LAST
] = {
451 "Move cursor to given line (defaults to last)",
452 gotoline
, { .i
= +1 }
454 [VIS_ACTION_CURSOR_WINDOW_LINE_TOP
] = {
455 "cursor-window-line-top",
456 "Move cursor to top line of the window",
457 movement
, { .i
= VIS_MOVE_WINDOW_LINE_TOP
}
459 [VIS_ACTION_CURSOR_WINDOW_LINE_MIDDLE
] = {
460 "cursor-window-line-middle",
461 "Move cursor to middle line of the window",
462 movement
, { .i
= VIS_MOVE_WINDOW_LINE_MIDDLE
}
464 [VIS_ACTION_CURSOR_WINDOW_LINE_BOTTOM
] = {
465 "cursor-window-line-bottom",
466 "Move cursor to bottom line of the window",
467 movement
, { .i
= VIS_MOVE_WINDOW_LINE_BOTTOM
}
469 [VIS_ACTION_CURSOR_SEARCH_NEXT
] = {
470 "cursor-search-forward",
471 "Move cursor to bottom line of the window",
472 movement
, { .i
= VIS_MOVE_SEARCH_NEXT
}
474 [VIS_ACTION_CURSOR_SEARCH_PREV
] = {
475 "cursor-search-backward",
476 "Move cursor to bottom line of the window",
477 movement
, { .i
= VIS_MOVE_SEARCH_PREV
}
479 [VIS_ACTION_CURSOR_SEARCH_WORD_FORWARD
] = {
480 "cursor-search-word-forward",
481 "Move cursor to next occurence of the word under cursor",
482 movement
, { .i
= VIS_MOVE_SEARCH_WORD_FORWARD
}
484 [VIS_ACTION_CURSOR_SEARCH_WORD_BACKWARD
] = {
485 "cursor-search-word-backward",
486 "Move cursor to previous occurence of the word under cursor",
487 movement
, { .i
= VIS_MOVE_SEARCH_WORD_BACKWARD
}
489 [VIS_ACTION_WINDOW_PAGE_UP
] = {
491 "Scroll window pages backwards (upwards)",
492 wscroll
, { .i
= -PAGE
}
494 [VIS_ACTION_WINDOW_HALFPAGE_UP
] = {
495 "window-halfpage-up",
496 "Scroll window half pages backwards (upwards)",
497 wscroll
, { .i
= -PAGE_HALF
}
499 [VIS_ACTION_WINDOW_PAGE_DOWN
] = {
501 "Scroll window pages forwards (downwards)",
502 wscroll
, { .i
= +PAGE
}
504 [VIS_ACTION_WINDOW_HALFPAGE_DOWN
] = {
505 "window-halfpage-down",
506 "Scroll window half pages forwards (downwards)",
507 wscroll
, { .i
= +PAGE_HALF
}
509 [VIS_ACTION_MODE_NORMAL
] = {
512 switchmode
, { .i
= VIS_MODE_NORMAL
}
514 [VIS_ACTION_MODE_VISUAL
] = {
515 "vis-mode-visual-charwise",
516 "Enter characterwise visual mode",
517 switchmode
, { .i
= VIS_MODE_VISUAL
}
519 [VIS_ACTION_MODE_VISUAL_LINE
] = {
520 "vis-mode-visual-linewise",
521 "Enter linewise visual mode",
522 switchmode
, { .i
= VIS_MODE_VISUAL_LINE
}
524 [VIS_ACTION_MODE_INSERT
] = {
527 switchmode
, { .i
= VIS_MODE_INSERT
}
529 [VIS_ACTION_MODE_REPLACE
] = {
531 "Enter replace mode",
532 switchmode
, { .i
= VIS_MODE_REPLACE
}
534 [VIS_ACTION_MODE_OPERATOR_PENDING
] = {
535 "vis-mode-operator-pending",
536 "Enter to operator pending mode",
537 switchmode
, { .i
= VIS_MODE_OPERATOR_PENDING
}
539 [VIS_ACTION_DELETE_CHAR_PREV
] = {
541 "Delete the previous character",
542 delete, { .i
= VIS_MOVE_CHAR_PREV
}
544 [VIS_ACTION_DELETE_CHAR_NEXT
] = {
546 "Delete the next character",
547 delete, { .i
= VIS_MOVE_CHAR_NEXT
}
549 [VIS_ACTION_DELETE_LINE_BEGIN
] = {
551 "Delete until the start of the current line",
552 delete, { .i
= VIS_MOVE_LINE_BEGIN
}
554 [VIS_ACTION_DELETE_WORD_PREV
] = {
556 "Delete the previous WORD",
557 delete, { .i
= VIS_MOVE_LONGWORD_START_PREV
}
559 [VIS_ACTION_JUMPLIST_PREV
] = {
561 "Go to older cursor position in jump list",
562 movement
, { .i
= VIS_MOVE_JUMPLIST_PREV
}
564 [VIS_ACTION_JUMPLIST_NEXT
] = {
566 "Go to newer cursor position in jump list",
567 movement
, { .i
= VIS_MOVE_JUMPLIST_NEXT
}
569 [VIS_ACTION_CHANGELIST_PREV
] = {
571 "Go to older cursor position in change list",
572 movement
, { .i
= VIS_MOVE_CHANGELIST_PREV
}
574 [VIS_ACTION_CHANGELIST_NEXT
] = {
576 "Go to newer cursor position in change list",
577 movement
, { .i
= VIS_MOVE_CHANGELIST_NEXT
}
579 [VIS_ACTION_UNDO
] = {
584 [VIS_ACTION_REDO
] = {
589 [VIS_ACTION_EARLIER
] = {
591 "Goto older text state",
594 [VIS_ACTION_LATER
] = {
596 "Goto newer text state",
599 [VIS_ACTION_MACRO_RECORD
] = {
601 "Record macro into given register",
604 [VIS_ACTION_MACRO_REPLAY
] = {
606 "Replay macro, execute the content of the given register",
609 [VIS_ACTION_MARK_SET
] = {
611 "Set given mark at current cursor position",
614 [VIS_ACTION_MARK_GOTO
] = {
616 "Goto the position of the given mark",
617 mark_motion
, { .i
= VIS_MOVE_MARK
}
619 [VIS_ACTION_MARK_GOTO_LINE
] = {
621 "Goto first non-blank character of the line containing the given mark",
622 mark_motion
, { .i
= VIS_MOVE_MARK_LINE
}
624 [VIS_ACTION_REDRAW
] = {
626 "Redraw current editor content",
627 call
, { .f
= vis_redraw
}
629 [VIS_ACTION_REPLACE_CHAR
] = {
631 "Replace the character under the cursor",
634 [VIS_ACTION_TOTILL_REPEAT
] = {
636 "Repeat latest to/till motion",
637 movement
, { .i
= VIS_MOVE_TOTILL_REPEAT
}
639 [VIS_ACTION_TOTILL_REVERSE
] = {
641 "Repeat latest to/till motion but in opposite direction",
642 movement
, { .i
= VIS_MOVE_TOTILL_REVERSE
}
644 [VIS_ACTION_PROMPT_SEARCH_FORWARD
] = {
647 prompt_show
, { .s
= "/" }
649 [VIS_ACTION_PROMPT_SEARCH_BACKWARD
] = {
652 prompt_show
, { .s
= "?" }
654 [VIS_ACTION_TILL_LEFT
] = {
656 "Till after the occurrence of character to the left",
657 movement_key
, { .i
= VIS_MOVE_LEFT_TILL
}
659 [VIS_ACTION_TILL_RIGHT
] = {
661 "Till before the occurrence of character to the right",
662 movement_key
, { .i
= VIS_MOVE_RIGHT_TILL
}
664 [VIS_ACTION_TO_LEFT
] = {
666 "To the first occurrence of character to the left",
667 movement_key
, { .i
= VIS_MOVE_LEFT_TO
}
669 [VIS_ACTION_TO_RIGHT
] = {
671 "To the first occurrence of character to the right",
672 movement_key
, { .i
= VIS_MOVE_RIGHT_TO
}
674 [VIS_ACTION_REGISTER
] = {
676 "Use given register for next operator",
679 [VIS_ACTION_OPERATOR_CHANGE
] = {
680 "vis-operator-change",
682 operator, { .i
= VIS_OP_CHANGE
}
684 [VIS_ACTION_OPERATOR_DELETE
] = {
685 "vis-operator-delete",
687 operator, { .i
= VIS_OP_DELETE
}
689 [VIS_ACTION_OPERATOR_YANK
] = {
692 operator, { .i
= VIS_OP_YANK
}
694 [VIS_ACTION_OPERATOR_SHIFT_LEFT
] = {
695 "vis-operator-shift-left",
696 "Shift left operator",
697 operator, { .i
= VIS_OP_SHIFT_LEFT
}
699 [VIS_ACTION_OPERATOR_SHIFT_RIGHT
] = {
700 "vis-operator-shift-right",
701 "Shift right operator",
702 operator, { .i
= VIS_OP_SHIFT_RIGHT
}
704 [VIS_ACTION_OPERATOR_CASE_LOWER
] = {
705 "vis-operator-case-lower",
706 "Lowercase operator",
707 operator, { .i
= VIS_OP_CASE_LOWER
}
709 [VIS_ACTION_OPERATOR_CASE_UPPER
] = {
710 "vis-operator-case-upper",
711 "Uppercase operator",
712 operator, { .i
= VIS_OP_CASE_UPPER
}
714 [VIS_ACTION_OPERATOR_CASE_SWAP
] = {
715 "vis-operator-case-swap",
716 "Swap case operator",
717 operator, { .i
= VIS_OP_CASE_SWAP
}
719 [VIS_ACTION_OPERATOR_FILTER
] = {
720 "vis-operator-filter",
724 [VIS_ACTION_OPERATOR_FILTER_FMT
] = {
725 "vis-operator-filter-format",
726 "Formating operator, filter range through fmt(1)",
727 operator_filter
, { .s
= "'<,'>!fmt" }
729 [VIS_ACTION_COUNT
] = {
734 [VIS_ACTION_INSERT_NEWLINE
] = {
736 "Insert a line break (depending on file type)",
737 call
, { .f
= vis_insert_nl
}
739 [VIS_ACTION_INSERT_TAB
] = {
741 "Insert a tab (might be converted to spaces)",
742 call
, { .f
= vis_insert_tab
}
744 [VIS_ACTION_INSERT_VERBATIM
] = {
746 "Insert Unicode character based on code point",
749 [VIS_ACTION_INSERT_REGISTER
] = {
751 "Insert specified register content",
754 [VIS_ACTION_WINDOW_NEXT
] = {
757 call
, { .f
= vis_window_next
}
759 [VIS_ACTION_WINDOW_PREV
] = {
761 "Focus previous window",
762 call
, { .f
= vis_window_prev
}
764 [VIS_ACTION_APPEND_CHAR_NEXT
] = {
766 "Append text after the cursor",
767 insertmode
, { .i
= VIS_MOVE_CHAR_NEXT
}
769 [VIS_ACTION_APPEND_LINE_END
] = {
771 "Append text after the end of the line",
772 insertmode
, { .i
= VIS_MOVE_LINE_END
},
774 [VIS_ACTION_INSERT_LINE_START
] = {
776 "Insert text before the first non-blank in the line",
777 insertmode
, { .i
= VIS_MOVE_LINE_START
},
779 [VIS_ACTION_OPEN_LINE_ABOVE
] = {
781 "Begin a new line above the cursor",
782 openline
, { .i
= -1 }
784 [VIS_ACTION_OPEN_LINE_BELOW
] = {
786 "Begin a new line below the cursor",
787 openline
, { .i
= +1 }
789 [VIS_ACTION_JOIN_LINE_BELOW
] = {
792 join
, { .i
= VIS_MOVE_LINE_NEXT
},
794 [VIS_ACTION_JOIN_LINES
] = {
796 "Join selected lines",
797 operator, { .i
= VIS_OP_JOIN
}
799 [VIS_ACTION_PROMPT_SHOW
] = {
801 "Show editor command line prompt",
802 prompt_show
, { .s
= ":" }
804 [VIS_ACTION_PROMPT_SHOW_VISUAL
] = {
805 "prompt-show-visual",
806 "Show editor command line prompt in visual mode",
807 prompt_show
, { .s
= ":'<,'>" }
809 [VIS_ACTION_REPEAT
] = {
811 "Repeat latest editor command",
814 [VIS_ACTION_SELECTION_FLIP
] = {
816 "Flip selection, move cursor to other end",
819 [VIS_ACTION_SELECTION_RESTORE
] = {
821 "Restore last selection",
824 [VIS_ACTION_WINDOW_REDRAW_TOP
] = {
826 "Redraw cursor line at the top of the window",
827 window
, { .w
= view_redraw_top
}
829 [VIS_ACTION_WINDOW_REDRAW_CENTER
] = {
830 "window-redraw-center",
831 "Redraw cursor line at the center of the window",
832 window
, { .w
= view_redraw_center
}
834 [VIS_ACTION_WINDOW_REDRAW_BOTTOM
] = {
835 "window-redraw-bottom",
836 "Redraw cursor line at the bottom of the window",
837 window
, { .w
= view_redraw_bottom
}
839 [VIS_ACTION_WINDOW_SLIDE_UP
] = {
841 "Slide window content upwards",
844 [VIS_ACTION_WINDOW_SLIDE_DOWN
] = {
846 "Slide window content downwards",
849 [VIS_ACTION_PUT_AFTER
] = {
851 "Put text after the cursor",
852 operator, { .i
= VIS_OP_PUT_AFTER
}
854 [VIS_ACTION_PUT_BEFORE
] = {
856 "Put text before the cursor",
857 operator, { .i
= VIS_OP_PUT_BEFORE
}
859 [VIS_ACTION_PUT_AFTER_END
] = {
861 "Put text after the cursor, place cursor after new text",
862 operator, { .i
= VIS_OP_PUT_AFTER_END
}
864 [VIS_ACTION_PUT_BEFORE_END
] = {
866 "Put text before the cursor, place cursor after new text",
867 operator, { .i
= VIS_OP_PUT_BEFORE_END
}
869 [VIS_ACTION_CURSOR_SELECT_WORD
] = {
870 "cursors-select-word",
871 "Select word under cursor",
874 [VIS_ACTION_CURSORS_NEW_LINE_ABOVE
] = {
875 "cursors-new-lines-above",
876 "Create a new cursor on the line above",
877 cursors_new
, { .i
= -1 }
879 [VIS_ACTION_CURSORS_NEW_LINE_BELOW
] = {
880 "cursor-new-lines-below",
881 "Create a new cursor on the line below",
882 cursors_new
, { .i
= +1 }
884 [VIS_ACTION_CURSORS_NEW_LINES_BEGIN
] = {
885 "cursors-new-lines-begin",
886 "Create a new cursor at the start of every line covered by selection",
887 operator, { .i
= VIS_OP_CURSOR_SOL
}
889 [VIS_ACTION_CURSORS_NEW_LINES_END
] = {
890 "cursors-new-lines-end",
891 "Create a new cursor at the end of every line covered by selection",
892 operator, { .i
= VIS_OP_CURSOR_EOL
}
894 [VIS_ACTION_CURSORS_NEW_MATCH_NEXT
] = {
895 "cursors-new-match-next",
896 "Select the next region matching the current selection",
899 [VIS_ACTION_CURSORS_NEW_MATCH_SKIP
] = {
900 "cursors-new-match-skip",
901 "Clear current selection, but select next match",
904 [VIS_ACTION_CURSORS_ALIGN
] = {
906 "Try to align all cursors on the same column",
909 [VIS_ACTION_CURSORS_REMOVE_ALL
] = {
910 "cursors-remove-all",
911 "Remove all but the primary cursor",
914 [VIS_ACTION_CURSORS_REMOVE_LAST
] = {
915 "cursors-remove-last",
916 "Remove least recently created cursor",
919 [VIS_ACTION_TEXT_OBJECT_WORD_OUTER
] = {
920 "text-object-word-outer",
921 "A word leading and trailing whitespace included",
922 textobj
, { .i
= VIS_TEXTOBJECT_OUTER_WORD
}
924 [VIS_ACTION_TEXT_OBJECT_WORD_INNER
] = {
925 "text-object-word-inner",
926 "A word leading and trailing whitespace excluded",
927 textobj
, { .i
= VIS_TEXTOBJECT_INNER_WORD
}
929 [VIS_ACTION_TEXT_OBJECT_LONGWORD_OUTER
] = {
930 "text-object-longword-outer",
931 "A WORD leading and trailing whitespace included",
932 textobj
, { .i
= VIS_TEXTOBJECT_OUTER_LONGWORD
}
934 [VIS_ACTION_TEXT_OBJECT_LONGWORD_INNER
] = {
935 "text-object-longword-inner",
936 "A WORD leading and trailing whitespace excluded",
937 textobj
, { .i
= VIS_TEXTOBJECT_INNER_LONGWORD
}
939 [VIS_ACTION_TEXT_OBJECT_SENTENCE
] = {
940 "text-object-sentence",
942 textobj
, { .i
= VIS_TEXTOBJECT_SENTENCE
}
944 [VIS_ACTION_TEXT_OBJECT_PARAGRAPH
] = {
945 "text-object-paragraph",
947 textobj
, { .i
= VIS_TEXTOBJECT_PARAGRAPH
}
949 [VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_OUTER
] = {
950 "text-object-square-bracket-outer",
951 "[] block (outer variant)",
952 textobj
, { .i
= VIS_TEXTOBJECT_OUTER_SQUARE_BRACKET
}
954 [VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_INNER
] = {
955 "text-object-square-bracket-inner",
956 "[] block (inner variant)",
957 textobj
, { .i
= VIS_TEXTOBJECT_INNER_SQUARE_BRACKET
}
959 [VIS_ACTION_TEXT_OBJECT_PARANTHESE_OUTER
] = {
960 "text-object-parentheses-outer",
961 "() block (outer variant)",
962 textobj
, { .i
= VIS_TEXTOBJECT_OUTER_PARANTHESE
}
964 [VIS_ACTION_TEXT_OBJECT_PARANTHESE_INNER
] = {
965 "text-object-parentheses-inner",
966 "() block (inner variant)",
967 textobj
, { .i
= VIS_TEXTOBJECT_INNER_PARANTHESE
}
969 [VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_OUTER
] = {
970 "text-object-angle-bracket-outer",
971 "<> block (outer variant)",
972 textobj
, { .i
= VIS_TEXTOBJECT_OUTER_ANGLE_BRACKET
}
974 [VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_INNER
] = {
975 "text-object-angle-bracket-inner",
976 "<> block (inner variant)",
977 textobj
, { .i
= VIS_TEXTOBJECT_INNER_ANGLE_BRACKET
}
979 [VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_OUTER
] = {
980 "text-object-curly-bracket-outer",
981 "{} block (outer variant)",
982 textobj
, { .i
= VIS_TEXTOBJECT_OUTER_CURLY_BRACKET
}
984 [VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_INNER
] = {
985 "text-object-curly-bracket-inner",
986 "{} block (inner variant)",
987 textobj
, { .i
= VIS_TEXTOBJECT_INNER_CURLY_BRACKET
}
989 [VIS_ACTION_TEXT_OBJECT_QUOTE_OUTER
] = {
990 "text-object-quote-outer",
991 "A quoted string, including the quotation marks",
992 textobj
, { .i
= VIS_TEXTOBJECT_OUTER_QUOTE
}
994 [VIS_ACTION_TEXT_OBJECT_QUOTE_INNER
] = {
995 "text-object-quote-inner",
996 "A quoted string, excluding the quotation marks",
997 textobj
, { .i
= VIS_TEXTOBJECT_INNER_QUOTE
}
999 [VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_OUTER
] = {
1000 "text-object-single-quote-outer",
1001 "A single quoted string, including the quotation marks",
1002 textobj
, { .i
= VIS_TEXTOBJECT_OUTER_SINGLE_QUOTE
}
1004 [VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_INNER
] = {
1005 "text-object-single-quote-inner",
1006 "A single quoted string, excluding the quotation marks",
1007 textobj
, { .i
= VIS_TEXTOBJECT_INNER_SINGLE_QUOTE
}
1009 [VIS_ACTION_TEXT_OBJECT_BACKTICK_OUTER
] = {
1010 "text-object-backtick-outer",
1011 "A backtick delimited string (outer variant)",
1012 textobj
, { .i
= VIS_TEXTOBJECT_OUTER_BACKTICK
}
1014 [VIS_ACTION_TEXT_OBJECT_BACKTICK_INNER
] = {
1015 "text-object-backtick-inner",
1016 "A backtick delimited string (inner variant)",
1017 textobj
, { .i
= VIS_TEXTOBJECT_INNER_BACKTICK
}
1019 [VIS_ACTION_TEXT_OBJECT_ENTIRE_OUTER
] = {
1020 "text-object-entire-outer",
1021 "The whole text content",
1022 textobj
, { .i
= VIS_TEXTOBJECT_OUTER_ENTIRE
}
1024 [VIS_ACTION_TEXT_OBJECT_ENTIRE_INNER
] = {
1025 "text-object-entire-inner",
1026 "The whole text content, except for leading and trailing empty lines",
1027 textobj
, { .i
= VIS_TEXTOBJECT_INNER_ENTIRE
}
1029 [VIS_ACTION_TEXT_OBJECT_FUNCTION_OUTER
] = {
1030 "text-object-function-outer",
1031 "A whole C-like function",
1032 textobj
, { .i
= VIS_TEXTOBJECT_OUTER_FUNCTION
}
1034 [VIS_ACTION_TEXT_OBJECT_FUNCTION_INNER
] = {
1035 "text-object-function-inner",
1036 "A whole C-like function body",
1037 textobj
, { .i
= VIS_TEXTOBJECT_INNER_FUNCTION
}
1039 [VIS_ACTION_TEXT_OBJECT_LINE_OUTER
] = {
1040 "text-object-line-outer",
1042 textobj
, { .i
= VIS_TEXTOBJECT_OUTER_LINE
}
1044 [VIS_ACTION_TEXT_OBJECT_LINE_INNER
] = {
1045 "text-object-line-inner",
1046 "The whole line, excluding leading and trailing whitespace",
1047 textobj
, { .i
= VIS_TEXTOBJECT_INNER_LINE
}
1049 [VIS_ACTION_MOTION_CHARWISE
] = {
1051 "Force motion to be charwise",
1052 motiontype
, { .i
= VIS_MOTIONTYPE_CHARWISE
}
1054 [VIS_ACTION_MOTION_LINEWISE
] = {
1056 "Force motion to be linewise",
1057 motiontype
, { .i
= VIS_MOTIONTYPE_LINEWISE
}
1059 [VIS_ACTION_UNICODE_INFO
] = {
1061 "Show Unicode codepoint(s) of character under cursor",
1064 [VIS_ACTION_NUMBER_INCREMENT
] = {
1066 "Increment number under cursor",
1067 number_increment_decrement
, { .i
= +1 }
1069 [VIS_ACTION_NUMBER_DECREMENT
] = {
1071 "Decrement number under cursor",
1072 number_increment_decrement
, { .i
= -1 }
1074 [VIS_ACTION_NOP
] = {
1076 "Ignore key, do nothing",
1083 /** key bindings functions */
1085 static const char *nop(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1089 static const char *key2macro(Vis
*vis
, const char *keys
, enum VisMacro
*macro
) {
1090 *macro
= VIS_MACRO_INVALID
;
1091 if (keys
[0] >= 'a' && keys
[0] <= 'z')
1092 *macro
= keys
[0] - 'a';
1093 else if (keys
[0] == '@')
1094 *macro
= VIS_MACRO_LAST_RECORDED
;
1095 else if (keys
[0] == '\0')
1100 static const char *macro_record(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1101 if (vis_macro_record_stop(vis
))
1103 enum VisMacro macro
;
1104 keys
= key2macro(vis
, keys
, ¯o
);
1105 vis_macro_record(vis
, macro
);
1110 static const char *macro_replay(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1111 enum VisMacro macro
;
1112 keys
= key2macro(vis
, keys
, ¯o
);
1113 vis_macro_replay(vis
, macro
);
1117 static const char *suspend(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1122 static const char *repeat(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1127 static const char *cursors_new(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1128 View
*view
= vis_view(vis
);
1129 size_t pos
= view_cursor_get(view
);
1130 Cursor
*cursor
= view_cursors_new(view
);
1132 view_cursors_to(cursor
, pos
);
1134 view_line_down(cursor
);
1135 else if (arg
->i
< 0)
1136 view_line_up(cursor
);
1141 static const char *cursors_align(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1142 View
*view
= vis_view(vis
);
1143 Text
*txt
= vis_text(vis
);
1144 int mincol
= INT_MAX
;
1145 for (Cursor
*c
= view_cursors(view
); c
; c
= view_cursors_next(c
)) {
1146 int col
= view_cursors_cell_get(c
);
1147 if (col
>= 0 && col
< mincol
)
1150 for (Cursor
*c
= view_cursors(view
); c
; c
= view_cursors_next(c
)) {
1151 if (view_cursors_cell_set(c
, mincol
) == -1) {
1152 size_t pos
= view_cursors_pos(c
);
1153 size_t col
= text_line_char_set(txt
, pos
, mincol
);
1154 view_cursors_to(c
, col
);
1160 static const char *cursors_clear(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1161 View
*view
= vis_view(vis
);
1162 if (view_cursors_count(view
) > 1)
1163 view_cursors_clear(view
);
1165 view_cursors_selection_clear(view_cursor(view
));
1169 static const char *cursors_select(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1170 Text
*txt
= vis_text(vis
);
1171 View
*view
= vis_view(vis
);
1172 for (Cursor
*cursor
= view_cursors(view
); cursor
; cursor
= view_cursors_next(cursor
)) {
1173 Filerange sel
= view_cursors_selection_get(cursor
);
1174 Filerange word
= text_object_word(txt
, view_cursors_pos(cursor
));
1175 if (!text_range_valid(&sel
) && text_range_valid(&word
)) {
1176 view_cursors_selection_set(cursor
, &word
);
1177 view_cursors_to(cursor
, text_char_prev(txt
, word
.end
));
1180 vis_mode_switch(vis
, VIS_MODE_VISUAL
);
1184 static const char *cursors_select_next(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1185 Text
*txt
= vis_text(vis
);
1186 View
*view
= vis_view(vis
);
1187 Cursor
*cursor
= view_cursor(view
);
1188 Filerange sel
= view_cursors_selection_get(cursor
);
1189 if (!text_range_valid(&sel
))
1192 char *buf
= text_bytes_alloc0(txt
, sel
.start
, text_range_size(&sel
));
1195 Filerange word
= text_object_word_find_next(txt
, sel
.end
, buf
);
1198 if (text_range_valid(&word
)) {
1199 cursor
= view_cursors_new(view
);
1202 view_cursors_selection_set(cursor
, &word
);
1203 view_cursors_to(cursor
, text_char_prev(txt
, word
.end
));
1208 static const char *cursors_select_skip(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1209 View
*view
= vis_view(vis
);
1210 Cursor
*cursor
= view_cursor(view
);
1211 keys
= cursors_select_next(vis
, keys
, arg
);
1212 if (cursor
!= view_cursor(view
))
1213 view_cursors_dispose(cursor
);
1217 static const char *cursors_remove(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1218 View
*view
= vis_view(vis
);
1219 view_cursors_dispose(view_cursor(view
));
1220 view_cursor_to(view
, view_cursor_get(view
));
1224 static const char *replace(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1227 const char *next
= vis_keys_next(vis
, keys
);
1230 size_t len
= next
- keys
;
1232 memcpy(key
, keys
, len
);
1234 vis_operator(vis
, VIS_OP_REPLACE
);
1235 vis_motion(vis
, VIS_MOVE_NOP
);
1236 vis_keys_inject(vis
, next
, key
);
1237 vis_keys_inject(vis
, next
+len
, "<Escape>");
1241 static const char *count(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1242 int digit
= keys
[-1] - '0';
1243 int count
= vis_count_get_default(vis
, 0);
1244 if (0 <= digit
&& digit
<= 9) {
1245 if (digit
== 0 && count
== 0)
1246 vis_motion(vis
, VIS_MOVE_LINE_BEGIN
);
1247 vis_count_set(vis
, count
* 10 + digit
);
1252 static const char *gotoline(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1253 if (vis_count_get(vis
) != VIS_COUNT_UNKNOWN
)
1254 vis_motion(vis
, VIS_MOVE_LINE
);
1255 else if (arg
->i
< 0)
1256 vis_motion(vis
, VIS_MOVE_FILE_BEGIN
);
1258 vis_motion(vis
, VIS_MOVE_FILE_END
);
1262 static const char *motiontype(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1263 vis_motion_type(vis
, arg
->i
);
1267 static const char *operator(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1268 vis_operator(vis
, arg
->i
);
1272 static const char *operator_filter(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1273 vis_operator(vis
, VIS_OP_FILTER
, arg
->s
);
1277 static const char *movement_key(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1281 const char *next
= vis_keys_next(vis
, keys
);
1282 strncpy(key
, keys
, next
- keys
+ 1);
1283 key
[sizeof(key
)-1] = '\0';
1284 vis_motion(vis
, arg
->i
, key
);
1288 static const char *movement(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1289 vis_motion(vis
, arg
->i
);
1293 static const char *textobj(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1294 vis_textobject(vis
, arg
->i
);
1298 static const char *selection_end(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1299 for (Cursor
*c
= view_cursors(vis_view(vis
)); c
; c
= view_cursors_next(c
))
1300 view_cursors_selection_swap(c
);
1304 static const char *selection_restore(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1305 Text
*txt
= vis_text(vis
);
1306 View
*view
= vis_view(vis
);
1307 for (Cursor
*c
= view_cursors(view
); c
; c
= view_cursors_next(c
))
1308 view_cursors_selection_restore(c
);
1309 Filerange sel
= view_selection_get(view
);
1310 if (text_range_is_linewise(txt
, &sel
))
1311 vis_mode_switch(vis
, VIS_MODE_VISUAL_LINE
);
1313 vis_mode_switch(vis
, VIS_MODE_VISUAL
);
1317 static const char *key2register(Vis
*vis
, const char *keys
, enum VisRegister
*reg
) {
1318 *reg
= VIS_REG_INVALID
;
1321 if (keys
[0] >= 'a' && keys
[0] <= 'z')
1322 *reg
= keys
[0] - 'a';
1323 else if (keys
[0] >= 'A' && keys
[0] <= 'Z')
1324 *reg
= VIS_REG_A
+ keys
[0] - 'A';
1325 else if (keys
[0] == '*' || keys
[0] == '+')
1326 *reg
= VIS_REG_CLIPBOARD
;
1327 else if (keys
[0] == '_')
1328 *reg
= VIS_REG_BLACKHOLE
;
1332 static const char *reg(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1333 enum VisRegister reg
;
1334 keys
= key2register(vis
, keys
, ®
);
1335 vis_register_set(vis
, reg
);
1339 static const char *key2mark(Vis
*vis
, const char *keys
, int *mark
) {
1340 *mark
= VIS_MARK_INVALID
;
1343 if (keys
[0] >= 'a' && keys
[0] <= 'z')
1344 *mark
= keys
[0] - 'a';
1345 else if (keys
[0] == '<')
1346 *mark
= VIS_MARK_SELECTION_START
;
1347 else if (keys
[0] == '>')
1348 *mark
= VIS_MARK_SELECTION_END
;
1352 static const char *mark_set(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1354 keys
= key2mark(vis
, keys
, &mark
);
1355 vis_mark_set(vis
, mark
, view_cursor_get(vis_view(vis
)));
1359 static const char *mark_motion(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1361 keys
= key2mark(vis
, keys
, &mark
);
1362 vis_motion(vis
, arg
->i
, mark
);
1366 static const char *undo(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1367 size_t pos
= text_undo(vis_text(vis
));
1369 View
*view
= vis_view(vis
);
1370 if (view_cursors_count(view
) == 1)
1371 view_cursor_to(view
, pos
);
1372 /* redraw all windows in case some display the same file */
1378 static const char *redo(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1379 size_t pos
= text_redo(vis_text(vis
));
1381 View
*view
= vis_view(vis
);
1382 if (view_cursors_count(view
) == 1)
1383 view_cursor_to(view
, pos
);
1384 /* redraw all windows in case some display the same file */
1390 static const char *earlier(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1391 size_t pos
= text_earlier(vis_text(vis
), vis_count_get_default(vis
, 1));
1393 view_cursor_to(vis_view(vis
), pos
);
1394 /* redraw all windows in case some display the same file */
1400 static const char *later(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1401 size_t pos
= text_later(vis_text(vis
), vis_count_get_default(vis
, 1));
1403 view_cursor_to(vis_view(vis
), pos
);
1404 /* redraw all windows in case some display the same file */
1410 static const char *delete(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1411 vis_operator(vis
, VIS_OP_DELETE
);
1412 vis_motion(vis
, arg
->i
);
1416 static const char *insert_register(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1417 enum VisRegister regid
;
1418 keys
= key2register(vis
, keys
, ®id
);
1420 const char *data
= vis_register_get(vis
, regid
, &len
);
1421 vis_insert_key(vis
, data
, len
);
1425 static const char *prompt_show(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1426 vis_prompt_show(vis
, arg
->s
);
1430 static const char *insert_verbatim(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1432 char buf
[4], type
= keys
[0];
1433 const char *data
= NULL
;
1434 int len
= 0, count
= 0, base
= 0;
1456 if ('0' <= type
&& type
<= '9') {
1465 for (keys
++; keys
[0] && count
> 0; keys
++, count
--) {
1467 if (base
== 8 && '0' <= keys
[0] && keys
[0] <= '7') {
1469 } else if ((base
== 10 || base
== 16) && '0' <= keys
[0] && keys
[0] <= '9') {
1471 } else if (base
== 16 && 'a' <= keys
[0] && keys
[0] <= 'f') {
1472 v
= 10 + keys
[0] - 'a';
1473 } else if (base
== 16 && 'A' <= keys
[0] && keys
[0] <= 'F') {
1474 v
= 10 + keys
[0] - 'A';
1479 rune
= rune
* base
+ v
;
1484 if (type
== 'u' || type
== 'U') {
1485 len
= runetochar(buf
, &rune
);
1493 const char *next
= vis_keys_next(vis
, keys
);
1496 size_t keylen
= next
- keys
;
1498 memcpy(key
, keys
, keylen
);
1501 static const char *keysym
[] = {
1504 "<Backspace>", "\b",
1510 for (const char **k
= keysym
; k
[0]; k
+= 2) {
1511 if (strcmp(k
[0], key
) == 0) {
1521 vis_insert_key(vis
, data
, len
);
1525 static const char *cmd(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1526 vis_cmd(vis
, arg
->s
);
1530 static int argi2lines(Vis
*vis
, const Arg
*arg
) {
1531 int count
= vis_count_get(vis
);
1535 return view_height_get(vis_view(vis
));
1538 return view_height_get(vis_view(vis
))/2;
1540 if (count
!= VIS_COUNT_UNKNOWN
)
1542 return arg
->i
< 0 ? -arg
->i
: arg
->i
;
1546 static const char *wscroll(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1548 view_scroll_down(vis_view(vis
), argi2lines(vis
, arg
));
1550 view_scroll_up(vis_view(vis
), argi2lines(vis
, arg
));
1554 static const char *wslide(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1556 view_slide_down(vis_view(vis
), argi2lines(vis
, arg
));
1558 view_slide_up(vis_view(vis
), argi2lines(vis
, arg
));
1562 static const char *call(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1567 static const char *window(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1568 arg
->w(vis_view(vis
));
1572 static const char *openline(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1573 vis_operator(vis
, VIS_OP_INSERT
);
1575 vis_motion(vis
, VIS_MOVE_LINE_END
);
1576 vis_keys_inject(vis
, keys
, "<insert-newline>");
1578 vis_motion(vis
, VIS_MOVE_LINE_BEGIN
);
1579 vis_keys_inject(vis
, keys
, "<insert-newline><Up>");
1584 static const char *join(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1585 int count
= vis_count_get_default(vis
, 0);
1587 vis_count_set(vis
, count
-1);
1588 vis_operator(vis
, VIS_OP_JOIN
);
1589 vis_motion(vis
, arg
->i
);
1593 static const char *switchmode(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1594 vis_mode_switch(vis
, arg
->i
);
1598 static const char *insertmode(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1599 vis_operator(vis
, VIS_OP_INSERT
);
1600 vis_motion(vis
, arg
->i
);
1604 static const char *unicode_info(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1605 View
*view
= vis_view(vis
);
1606 Text
*txt
= vis_text(vis
);
1607 size_t start
= view_cursor_get(view
);
1608 size_t end
= text_char_next(txt
, start
);
1609 char data
[end
-start
], *data_cur
= data
;
1610 text_bytes_get(txt
, start
, end
- start
, data
);
1611 Iterator it
= text_iterator_get(txt
, start
);
1612 char info
[255] = "", *info_cur
= info
;
1613 for (size_t pos
= start
; it
.pos
< end
; pos
= it
.pos
) {
1614 text_iterator_codepoint_next(&it
, NULL
);
1615 size_t len
= it
.pos
- pos
;
1616 wchar_t wc
= 0xFFFD;
1617 mbtowc(&wc
, data_cur
, len
);
1618 int width
= wcwidth(wc
);
1619 info_cur
+= snprintf(info_cur
, sizeof(info
) - (info_cur
- info
) - 1,
1620 "<%s%.*s> U+%04x ", width
== 0 ? " " : "", (int)len
, data_cur
, wc
);
1623 vis_info_show(vis
, "%s", info
);
1627 static const char *percent(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1628 if (vis_count_get(vis
) == VIS_COUNT_UNKNOWN
)
1629 vis_motion(vis
, VIS_MOVE_BRACKET_MATCH
);
1631 vis_motion(vis
, VIS_MOVE_PERCENT
);
1635 static const char *number_increment_decrement(Vis
*vis
, const char *keys
, const Arg
*arg
) {
1636 View
*view
= vis_view(vis
);
1637 Text
*txt
= vis_text(vis
);
1640 int count
= vis_count_get(vis
);
1641 if (count
!= 0 && count
!= VIS_COUNT_UNKNOWN
)
1644 for (Cursor
*c
= view_cursors(view
); c
; c
= view_cursors_next(c
)) {
1645 Filerange r
= text_object_number(txt
, view_cursors_pos(c
));
1646 if (!text_range_valid(&r
))
1648 char *buf
= text_bytes_alloc0(txt
, r
.start
, text_range_size(&r
));
1650 char *number
= buf
, fmt
[255];
1651 if (number
[0] == '-')
1653 bool octal
= number
[0] == '0' && ('0' <= number
[1] && number
[1] <= '7');
1654 bool hex
= number
[0] == '0' && (number
[1] == 'x' || number
[1] == 'X');
1655 bool dec
= !hex
&& !octal
;
1657 long long value
= strtoll(buf
, NULL
, 0);
1660 snprintf(fmt
, sizeof fmt
, "%lld", value
);
1662 size_t len
= strlen(number
) - 2;
1663 snprintf(fmt
, sizeof fmt
, "0x%0*llx", len
, value
);
1665 size_t len
= strlen(number
) - 1;
1666 snprintf(fmt
, sizeof fmt
, "0%0*llo", len
, value
);
1668 text_delete_range(txt
, &r
);
1669 text_insert(txt
, r
.start
, fmt
, strlen(fmt
));
1670 view_cursors_to(c
, r
.start
);
1682 static void signal_handler(int signum
, siginfo_t
*siginfo
, void *context
) {
1683 vis_signal_handler(vis
, signum
, siginfo
, context
);
1686 int main(int argc
, char *argv
[]) {
1689 .vis_start
= vis_lua_start
,
1690 .vis_quit
= vis_lua_quit
,
1691 .file_open
= vis_lua_file_open
,
1692 .file_save
= vis_lua_file_save
,
1693 .file_close
= vis_lua_file_close
,
1694 .win_open
= vis_lua_win_open
,
1695 .win_close
= vis_lua_win_close
,
1698 vis
= vis_new(ui_curses_new(), &event
);
1700 return EXIT_FAILURE
;
1702 for (int i
= 0; i
< LENGTH(vis_action
); i
++) {
1703 KeyAction
*action
= &vis_action
[i
];
1704 if (!vis_action_register(vis
, action
))
1705 vis_die(vis
, "Could not register action: %s\n", action
->name
);
1708 for (int i
= 0; i
< LENGTH(default_bindings
); i
++) {
1709 for (const KeyBinding
**binding
= default_bindings
[i
]; binding
&& *binding
; binding
++) {
1710 for (const KeyBinding
*kb
= *binding
; kb
->key
; kb
++) {
1711 vis_mode_map(vis
, i
, kb
->key
, kb
);
1716 /* install signal handlers etc. */
1717 struct sigaction sa
;
1718 memset(&sa
, 0, sizeof sa
);
1719 sa
.sa_flags
= SA_SIGINFO
;
1720 sa
.sa_sigaction
= signal_handler
;
1721 if (sigaction(SIGBUS
, &sa
, NULL
) || sigaction(SIGINT
, &sa
, NULL
))
1722 vis_die(vis
, "sigaction: %s", strerror(errno
));
1725 sigemptyset(&blockset
);
1726 sigaddset(&blockset
, SIGWINCH
);
1727 sigprocmask(SIG_BLOCK
, &blockset
, NULL
);
1728 signal(SIGPIPE
, SIG_IGN
);
1730 int status
= vis_run(vis
, argc
, argv
);