vis: remove z| pairwise union
[vis.git] / main.c
blob452b6633201a1df09634c02275c85f0fa32df477
1 #include <signal.h>
2 #include <limits.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <wchar.h>
6 #include <ctype.h>
7 #include <errno.h>
8 #include <fcntl.h>
9 #include <inttypes.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
13 #include "ui-terminal.h"
14 #include "vis.h"
15 #include "vis-lua.h"
16 #include "text-util.h"
17 #include "text-motions.h"
18 #include "text-objects.h"
19 #include "util.h"
20 #include "libutf.h"
21 #include "array.h"
22 #include "buffer.h"
24 #define PAGE INT_MAX
25 #define PAGE_HALF (INT_MAX-1)
27 /** functions to be called from keybindings */
28 /* ignore key, do nothing */
29 static const char *nop(Vis*, const char *keys, const Arg *arg);
30 /* record/replay macro indicated by keys */
31 static const char *macro_record(Vis*, const char *keys, const Arg *arg);
32 static const char *macro_replay(Vis*, const char *keys, const Arg *arg);
33 /* temporarily suspend the editor and return to the shell, type 'fg' to get back */
34 static const char *suspend(Vis*, const char *keys, const Arg *arg);
35 /* reset count if set, otherwise remove all but the primary selection */
36 static const char *normalmode_escape(Vis*, const char *keys, const Arg *arg);
37 /* reset count if set, otherwise switch to normal mode */
38 static const char *visualmode_escape(Vis*, const char *keys, const Arg *arg);
39 /* switch to mode indicated by arg->i */
40 static const char *switchmode(Vis*, const char *keys, const Arg *arg);
41 /* switch to insert mode after performing movement indicated by arg->i */
42 static const char *insertmode(Vis*, const char *keys, const Arg *arg);
43 /* switch to replace mode after performing movement indicated by arg->i */
44 static const char *replacemode(Vis*, const char *keys, const Arg *arg);
45 /* add a new line either before or after the one where the cursor currently is */
46 static const char *openline(Vis*, const char *keys, const Arg *arg);
47 /* join lines from current cursor position to movement indicated by arg */
48 static const char *join(Vis*, const char *keys, const Arg *arg);
49 /* perform last action i.e. action_prev again */
50 static const char *repeat(Vis*, const char *keys, const Arg *arg);
51 /* replace character at cursor with one from keys */
52 static const char *replace(Vis*, const char *keys, const Arg *arg);
53 /* create a new cursor on the previous (arg->i < 0) or next (arg->i > 0) line */
54 static const char *selections_new(Vis*, const char *keys, const Arg *arg);
55 /* try to align all selections on the same column */
56 static const char *selections_align(Vis*, const char *keys, const Arg *arg);
57 /* try to align all selections by inserting the correct amount of white spaces */
58 static const char *selections_align_indent(Vis*, const char *keys, const Arg *arg);
59 /* remove all but the primary cursor and their selections */
60 static const char *selections_clear(Vis*, const char *keys, const Arg *arg);
61 /* remove the least recently added selection */
62 static const char *selections_remove(Vis*, const char *keys, const Arg *arg);
63 /* remove count (or arg->i)-th selection column */
64 static const char *selections_remove_column(Vis*, const char *keys, const Arg *arg);
65 /* remove all but the count (or arg->i)-th selection column */
66 static const char *selections_remove_column_except(Vis*, const char *keys, const Arg *arg);
67 /* move to the previous (arg->i < 0) or next (arg->i > 0) selection */
68 static const char *selections_navigate(Vis*, const char *keys, const Arg *arg);
69 /* select the word the selection is currently over */
70 static const char *selections_match_word(Vis*, const char *keys, const Arg *arg);
71 /* select the next region matching the current selection */
72 static const char *selections_match_next(Vis*, const char *keys, const Arg *arg);
73 /* clear current selection but select next match */
74 static const char *selections_match_skip(Vis*, const char *keys, const Arg *arg);
75 /* rotate selection content count times left (arg->i < 0) or right (arg->i > 0) */
76 static const char *selections_rotate(Vis*, const char *keys, const Arg *arg);
77 /* remove leading and trailing white spaces from selections */
78 static const char *selections_trim(Vis*, const char *keys, const Arg *arg);
79 /* save active selections to mark */
80 static const char *selections_save(Vis*, const char *keys, const Arg *arg);
81 /* restore selections from mark */
82 static const char *selections_restore(Vis*, const char *keys, const Arg *arg);
83 /* union selections from mark */
84 static const char *selections_union(Vis*, const char *keys, const Arg *arg);
85 /* intersect selections from mark */
86 static const char *selections_intersect(Vis*, const char *keys, const Arg *arg);
87 /* perform complement of current active selections */
88 static const char *selections_complement(Vis*, const char *keys, const Arg *arg);
89 /* subtract selections from mark */
90 static const char *selections_minus(Vis*, const char *keys, const Arg *arg);
91 /* pairwise combine selections from mark */
92 static const char *selections_combine(Vis*, const char *keys, const Arg *arg);
93 static Filerange combine_intersect(const Filerange*, const Filerange*);
94 static Filerange combine_longer(const Filerange*, const Filerange*);
95 static Filerange combine_shorter(const Filerange*, const Filerange*);
96 static Filerange combine_leftmost(const Filerange*, const Filerange*);
97 static Filerange combine_rightmost(const Filerange*, const Filerange*);
98 /* adjust current used count according to keys */
99 static const char *count(Vis*, const char *keys, const Arg *arg);
100 /* move to the count-th line or if not given either to the first (arg->i < 0)
101 * or last (arg->i > 0) line of file */
102 static const char *gotoline(Vis*, const char *keys, const Arg *arg);
103 /* make the current action use the operator indicated by arg->i */
104 static const char *operator(Vis*, const char *keys, const Arg *arg);
105 /* blocks to read a key and performs movement indicated by arg->i which
106 * should be one of VIS_MOVE_{RIGHT,LEFT}_{TO,TILL} */
107 static const char *movement_key(Vis*, const char *keys, const Arg *arg);
108 /* perform the movement as indicated by arg->i */
109 static const char *movement(Vis*, const char *keys, const Arg *arg);
110 /* let the current operator affect the range indicated by the text object arg->i */
111 static const char *textobj(Vis*, const char *keys, const Arg *arg);
112 /* move to the other end of selected text */
113 static const char *selection_end(Vis*, const char *keys, const Arg *arg);
114 /* use register indicated by keys for the current operator */
115 static const char *reg(Vis*, const char *keys, const Arg *arg);
116 /* use mark indicated by keys for the current action */
117 static const char *mark(Vis*, const char *keys, const Arg *arg);
118 /* {un,re}do last action, redraw window */
119 static const char *undo(Vis*, const char *keys, const Arg *arg);
120 static const char *redo(Vis*, const char *keys, const Arg *arg);
121 /* earlier, later action chronologically, redraw window */
122 static const char *earlier(Vis*, const char *keys, const Arg *arg);
123 static const char *later(Vis*, const char *keys, const Arg *arg);
124 /* delete from the current cursor position to the end of
125 * movement as indicated by arg->i */
126 static const char *delete(Vis*, const char *keys, const Arg *arg);
127 /* insert register content indicated by keys at current cursor position */
128 static const char *insert_register(Vis*, const char *keys, const Arg *arg);
129 /* show a user prompt to get input with title arg->s */
130 static const char *prompt_show(Vis*, const char *keys, const Arg *arg);
131 /* blocks to read 3 consecutive digits and inserts the corresponding byte value */
132 static const char *insert_verbatim(Vis*, const char *keys, const Arg *arg);
133 /* scroll window content according to arg->i which can be either PAGE, PAGE_HALF,
134 * or an arbitrary number of lines. a multiplier overrides what is given in arg->i.
135 * negative values scroll back, positive forward. */
136 static const char *wscroll(Vis*, const char *keys, const Arg *arg);
137 /* similar to scroll, but do only move window content not cursor position */
138 static const char *wslide(Vis*, const char *keys, const Arg *arg);
139 /* call editor function as indicated by arg->f */
140 static const char *call(Vis*, const char *keys, const Arg *arg);
141 /* call window function as indicated by arg->w */
142 static const char *window(Vis*, const char *keys, const Arg *arg);
143 /* show info about Unicode character at cursor position */
144 static const char *unicode_info(Vis*, const char *keys, const Arg *arg);
145 /* either go to count % of file or to matching item */
146 static const char *percent(Vis*, const char *keys, const Arg *arg);
147 /* navigate jumplist next (arg->i > 0), prev (arg->i < 0), save (arg->i = 0) */
148 static const char *jumplist(Vis*, const char *keys, const Arg *arg);
150 enum {
151 VIS_ACTION_EDITOR_SUSPEND,
152 VIS_ACTION_CURSOR_CHAR_PREV,
153 VIS_ACTION_CURSOR_CHAR_NEXT,
154 VIS_ACTION_CURSOR_LINE_CHAR_PREV,
155 VIS_ACTION_CURSOR_LINE_CHAR_NEXT,
156 VIS_ACTION_CURSOR_CODEPOINT_PREV,
157 VIS_ACTION_CURSOR_CODEPOINT_NEXT,
158 VIS_ACTION_CURSOR_WORD_START_PREV,
159 VIS_ACTION_CURSOR_WORD_START_NEXT,
160 VIS_ACTION_CURSOR_WORD_END_PREV,
161 VIS_ACTION_CURSOR_WORD_END_NEXT,
162 VIS_ACTION_CURSOR_LONGWORD_START_PREV,
163 VIS_ACTION_CURSOR_LONGWORD_START_NEXT,
164 VIS_ACTION_CURSOR_LONGWORD_END_PREV,
165 VIS_ACTION_CURSOR_LONGWORD_END_NEXT,
166 VIS_ACTION_CURSOR_LINE_UP,
167 VIS_ACTION_CURSOR_LINE_DOWN,
168 VIS_ACTION_CURSOR_LINE_START,
169 VIS_ACTION_CURSOR_LINE_FINISH,
170 VIS_ACTION_CURSOR_LINE_BEGIN,
171 VIS_ACTION_CURSOR_LINE_END,
172 VIS_ACTION_CURSOR_SCREEN_LINE_UP,
173 VIS_ACTION_CURSOR_SCREEN_LINE_DOWN,
174 VIS_ACTION_CURSOR_SCREEN_LINE_BEGIN,
175 VIS_ACTION_CURSOR_SCREEN_LINE_MIDDLE,
176 VIS_ACTION_CURSOR_SCREEN_LINE_END,
177 VIS_ACTION_CURSOR_PERCENT,
178 VIS_ACTION_CURSOR_BYTE,
179 VIS_ACTION_CURSOR_BYTE_LEFT,
180 VIS_ACTION_CURSOR_BYTE_RIGHT,
181 VIS_ACTION_CURSOR_PARAGRAPH_PREV,
182 VIS_ACTION_CURSOR_PARAGRAPH_NEXT,
183 VIS_ACTION_CURSOR_SENTENCE_PREV,
184 VIS_ACTION_CURSOR_SENTENCE_NEXT,
185 VIS_ACTION_CURSOR_BLOCK_START,
186 VIS_ACTION_CURSOR_BLOCK_END,
187 VIS_ACTION_CURSOR_PARENTHESIS_START,
188 VIS_ACTION_CURSOR_PARENTHESIS_END,
189 VIS_ACTION_CURSOR_COLUMN,
190 VIS_ACTION_CURSOR_LINE_FIRST,
191 VIS_ACTION_CURSOR_LINE_LAST,
192 VIS_ACTION_CURSOR_WINDOW_LINE_TOP,
193 VIS_ACTION_CURSOR_WINDOW_LINE_MIDDLE,
194 VIS_ACTION_CURSOR_WINDOW_LINE_BOTTOM,
195 VIS_ACTION_CURSOR_SEARCH_REPEAT_FORWARD,
196 VIS_ACTION_CURSOR_SEARCH_REPEAT_BACKWARD,
197 VIS_ACTION_CURSOR_SEARCH_REPEAT,
198 VIS_ACTION_CURSOR_SEARCH_REPEAT_REVERSE,
199 VIS_ACTION_CURSOR_SEARCH_WORD_FORWARD,
200 VIS_ACTION_CURSOR_SEARCH_WORD_BACKWARD,
201 VIS_ACTION_WINDOW_PAGE_UP,
202 VIS_ACTION_WINDOW_PAGE_DOWN,
203 VIS_ACTION_WINDOW_HALFPAGE_UP,
204 VIS_ACTION_WINDOW_HALFPAGE_DOWN,
205 VIS_ACTION_MODE_NORMAL,
206 VIS_ACTION_MODE_NORMAL_ESCAPE,
207 VIS_ACTION_MODE_VISUAL,
208 VIS_ACTION_MODE_VISUAL_ESCAPE,
209 VIS_ACTION_MODE_VISUAL_LINE,
210 VIS_ACTION_MODE_INSERT,
211 VIS_ACTION_MODE_REPLACE,
212 VIS_ACTION_DELETE_CHAR_PREV,
213 VIS_ACTION_DELETE_CHAR_NEXT,
214 VIS_ACTION_DELETE_LINE_BEGIN,
215 VIS_ACTION_DELETE_WORD_PREV,
216 VIS_ACTION_JUMPLIST_PREV,
217 VIS_ACTION_JUMPLIST_NEXT,
218 VIS_ACTION_JUMPLIST_SAVE,
219 VIS_ACTION_UNDO,
220 VIS_ACTION_REDO,
221 VIS_ACTION_EARLIER,
222 VIS_ACTION_LATER,
223 VIS_ACTION_MACRO_RECORD,
224 VIS_ACTION_MACRO_REPLAY,
225 VIS_ACTION_MARK,
226 VIS_ACTION_REDRAW,
227 VIS_ACTION_REPLACE_CHAR,
228 VIS_ACTION_TOTILL_REPEAT,
229 VIS_ACTION_TOTILL_REVERSE,
230 VIS_ACTION_PROMPT_SEARCH_FORWARD,
231 VIS_ACTION_PROMPT_SEARCH_BACKWARD,
232 VIS_ACTION_TILL_LEFT,
233 VIS_ACTION_TILL_RIGHT,
234 VIS_ACTION_TO_LEFT,
235 VIS_ACTION_TO_RIGHT,
236 VIS_ACTION_REGISTER,
237 VIS_ACTION_OPERATOR_CHANGE,
238 VIS_ACTION_OPERATOR_DELETE,
239 VIS_ACTION_OPERATOR_YANK,
240 VIS_ACTION_OPERATOR_SHIFT_LEFT,
241 VIS_ACTION_OPERATOR_SHIFT_RIGHT,
242 VIS_ACTION_COUNT,
243 VIS_ACTION_INSERT_NEWLINE,
244 VIS_ACTION_INSERT_TAB,
245 VIS_ACTION_INSERT_VERBATIM,
246 VIS_ACTION_INSERT_REGISTER,
247 VIS_ACTION_WINDOW_NEXT,
248 VIS_ACTION_WINDOW_PREV,
249 VIS_ACTION_APPEND_CHAR_NEXT,
250 VIS_ACTION_APPEND_LINE_END,
251 VIS_ACTION_INSERT_LINE_START,
252 VIS_ACTION_OPEN_LINE_ABOVE,
253 VIS_ACTION_OPEN_LINE_BELOW,
254 VIS_ACTION_JOIN_LINES,
255 VIS_ACTION_JOIN_LINES_TRIM,
256 VIS_ACTION_PROMPT_SHOW,
257 VIS_ACTION_REPEAT,
258 VIS_ACTION_SELECTION_FLIP,
259 VIS_ACTION_WINDOW_REDRAW_TOP,
260 VIS_ACTION_WINDOW_REDRAW_CENTER,
261 VIS_ACTION_WINDOW_REDRAW_BOTTOM,
262 VIS_ACTION_WINDOW_SLIDE_UP,
263 VIS_ACTION_WINDOW_SLIDE_DOWN,
264 VIS_ACTION_PUT_AFTER,
265 VIS_ACTION_PUT_BEFORE,
266 VIS_ACTION_SELECTIONS_MATCH_WORD,
267 VIS_ACTION_SELECTIONS_NEW_LINE_ABOVE,
268 VIS_ACTION_SELECTIONS_NEW_LINE_ABOVE_FIRST,
269 VIS_ACTION_SELECTIONS_NEW_LINE_BELOW,
270 VIS_ACTION_SELECTIONS_NEW_LINE_BELOW_LAST,
271 VIS_ACTION_SELECTIONS_NEW_LINES_BEGIN,
272 VIS_ACTION_SELECTIONS_NEW_LINES_END,
273 VIS_ACTION_SELECTIONS_NEW_MATCH_NEXT,
274 VIS_ACTION_SELECTIONS_NEW_MATCH_SKIP,
275 VIS_ACTION_SELECTIONS_ALIGN,
276 VIS_ACTION_SELECTIONS_ALIGN_INDENT_LEFT,
277 VIS_ACTION_SELECTIONS_ALIGN_INDENT_RIGHT,
278 VIS_ACTION_SELECTIONS_REMOVE_ALL,
279 VIS_ACTION_SELECTIONS_REMOVE_LAST,
280 VIS_ACTION_SELECTIONS_REMOVE_COLUMN,
281 VIS_ACTION_SELECTIONS_REMOVE_COLUMN_EXCEPT,
282 VIS_ACTION_SELECTIONS_PREV,
283 VIS_ACTION_SELECTIONS_NEXT,
284 VIS_ACTION_SELECTIONS_ROTATE_LEFT,
285 VIS_ACTION_SELECTIONS_ROTATE_RIGHT,
286 VIS_ACTION_SELECTIONS_TRIM,
287 VIS_ACTION_SELECTIONS_SAVE,
288 VIS_ACTION_SELECTIONS_RESTORE,
289 VIS_ACTION_SELECTIONS_UNION,
290 VIS_ACTION_SELECTIONS_INTERSECT,
291 VIS_ACTION_SELECTIONS_COMPLEMENT,
292 VIS_ACTION_SELECTIONS_MINUS,
293 VIS_ACTION_SELECTIONS_COMBINE_INTERSECT,
294 VIS_ACTION_SELECTIONS_COMBINE_LONGER,
295 VIS_ACTION_SELECTIONS_COMBINE_SHORTER,
296 VIS_ACTION_SELECTIONS_COMBINE_LEFTMOST,
297 VIS_ACTION_SELECTIONS_COMBINE_RIGHTMOST,
298 VIS_ACTION_TEXT_OBJECT_WORD_OUTER,
299 VIS_ACTION_TEXT_OBJECT_WORD_INNER,
300 VIS_ACTION_TEXT_OBJECT_LONGWORD_OUTER,
301 VIS_ACTION_TEXT_OBJECT_LONGWORD_INNER,
302 VIS_ACTION_TEXT_OBJECT_SENTENCE,
303 VIS_ACTION_TEXT_OBJECT_PARAGRAPH,
304 VIS_ACTION_TEXT_OBJECT_PARAGRAPH_OUTER,
305 VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_OUTER,
306 VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_INNER,
307 VIS_ACTION_TEXT_OBJECT_PARENTHESIS_OUTER,
308 VIS_ACTION_TEXT_OBJECT_PARENTHESIS_INNER,
309 VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_OUTER,
310 VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_INNER,
311 VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_OUTER,
312 VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_INNER,
313 VIS_ACTION_TEXT_OBJECT_QUOTE_OUTER,
314 VIS_ACTION_TEXT_OBJECT_QUOTE_INNER,
315 VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_OUTER,
316 VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_INNER,
317 VIS_ACTION_TEXT_OBJECT_BACKTICK_OUTER,
318 VIS_ACTION_TEXT_OBJECT_BACKTICK_INNER,
319 VIS_ACTION_TEXT_OBJECT_ENTIRE_OUTER,
320 VIS_ACTION_TEXT_OBJECT_ENTIRE_INNER,
321 VIS_ACTION_TEXT_OBJECT_LINE_OUTER,
322 VIS_ACTION_TEXT_OBJECT_LINE_INNER,
323 VIS_ACTION_TEXT_OBJECT_INDENTATION,
324 VIS_ACTION_TEXT_OBJECT_SEARCH_FORWARD,
325 VIS_ACTION_TEXT_OBJECT_SEARCH_BACKWARD,
326 VIS_ACTION_UNICODE_INFO,
327 VIS_ACTION_UTF8_INFO,
328 VIS_ACTION_NOP,
331 static const KeyAction vis_action[] = {
332 [VIS_ACTION_EDITOR_SUSPEND] = {
333 "vis-suspend",
334 VIS_HELP("Suspend the editor")
335 suspend,
337 [VIS_ACTION_CURSOR_CHAR_PREV] = {
338 "vis-motion-char-prev",
339 VIS_HELP("Move cursor left, to the previous character")
340 movement, { .i = VIS_MOVE_CHAR_PREV }
342 [VIS_ACTION_CURSOR_CHAR_NEXT] = {
343 "vis-motion-char-next",
344 VIS_HELP("Move cursor right, to the next character")
345 movement, { .i = VIS_MOVE_CHAR_NEXT }
347 [VIS_ACTION_CURSOR_LINE_CHAR_PREV] = {
348 "vis-motion-line-char-prev",
349 VIS_HELP("Move cursor left, to the previous character on the same line")
350 movement, { .i = VIS_MOVE_LINE_CHAR_PREV }
352 [VIS_ACTION_CURSOR_LINE_CHAR_NEXT] = {
353 "vis-motion-line-char-next",
354 VIS_HELP("Move cursor right, to the next character on the same line")
355 movement, { .i = VIS_MOVE_LINE_CHAR_NEXT }
357 [VIS_ACTION_CURSOR_CODEPOINT_PREV] = {
358 "vis-motion-codepoint-prev",
359 VIS_HELP("Move to the previous Unicode codepoint")
360 movement, { .i = VIS_MOVE_CODEPOINT_PREV }
362 [VIS_ACTION_CURSOR_CODEPOINT_NEXT] = {
363 "vis-motion-codepoint-next",
364 VIS_HELP("Move to the next Unicode codepoint")
365 movement, { .i = VIS_MOVE_CODEPOINT_NEXT }
367 [VIS_ACTION_CURSOR_WORD_START_PREV] = {
368 "vis-motion-word-start-prev",
369 VIS_HELP("Move cursor words backwards")
370 movement, { .i = VIS_MOVE_WORD_START_PREV }
372 [VIS_ACTION_CURSOR_WORD_START_NEXT] = {
373 "vis-motion-word-start-next",
374 VIS_HELP("Move cursor words forwards")
375 movement, { .i = VIS_MOVE_WORD_START_NEXT }
377 [VIS_ACTION_CURSOR_WORD_END_PREV] = {
378 "vis-motion-word-end-prev",
379 VIS_HELP("Move cursor backwards to the end of word")
380 movement, { .i = VIS_MOVE_WORD_END_PREV }
382 [VIS_ACTION_CURSOR_WORD_END_NEXT] = {
383 "vis-motion-word-end-next",
384 VIS_HELP("Move cursor forward to the end of word")
385 movement, { .i = VIS_MOVE_WORD_END_NEXT }
387 [VIS_ACTION_CURSOR_LONGWORD_START_PREV] = {
388 "vis-motion-bigword-start-prev",
389 VIS_HELP("Move cursor WORDS backwards")
390 movement, { .i = VIS_MOVE_LONGWORD_START_PREV }
392 [VIS_ACTION_CURSOR_LONGWORD_START_NEXT] = {
393 "vis-motion-bigword-start-next",
394 VIS_HELP("Move cursor WORDS forwards")
395 movement, { .i = VIS_MOVE_LONGWORD_START_NEXT }
397 [VIS_ACTION_CURSOR_LONGWORD_END_PREV] = {
398 "vis-motion-bigword-end-prev",
399 VIS_HELP("Move cursor backwards to the end of WORD")
400 movement, { .i = VIS_MOVE_LONGWORD_END_PREV }
402 [VIS_ACTION_CURSOR_LONGWORD_END_NEXT] = {
403 "vis-motion-bigword-end-next",
404 VIS_HELP("Move cursor forward to the end of WORD")
405 movement, { .i = VIS_MOVE_LONGWORD_END_NEXT }
407 [VIS_ACTION_CURSOR_LINE_UP] = {
408 "vis-motion-line-up",
409 VIS_HELP("Move cursor line upwards")
410 movement, { .i = VIS_MOVE_LINE_UP }
412 [VIS_ACTION_CURSOR_LINE_DOWN] = {
413 "vis-motion-line-down",
414 VIS_HELP("Move cursor line downwards")
415 movement, { .i = VIS_MOVE_LINE_DOWN }
417 [VIS_ACTION_CURSOR_LINE_START] = {
418 "vis-motion-line-start",
419 VIS_HELP("Move cursor to first non-blank character of the line")
420 movement, { .i = VIS_MOVE_LINE_START }
422 [VIS_ACTION_CURSOR_LINE_FINISH] = {
423 "vis-motion-line-finish",
424 VIS_HELP("Move cursor to last non-blank character of the line")
425 movement, { .i = VIS_MOVE_LINE_FINISH }
427 [VIS_ACTION_CURSOR_LINE_BEGIN] = {
428 "vis-motion-line-begin",
429 VIS_HELP("Move cursor to first character of the line")
430 movement, { .i = VIS_MOVE_LINE_BEGIN }
432 [VIS_ACTION_CURSOR_LINE_END] = {
433 "vis-motion-line-end",
434 VIS_HELP("Move cursor to end of the line")
435 movement, { .i = VIS_MOVE_LINE_END }
437 [VIS_ACTION_CURSOR_SCREEN_LINE_UP] = {
438 "vis-motion-screenline-up",
439 VIS_HELP("Move cursor screen/display line upwards")
440 movement, { .i = VIS_MOVE_SCREEN_LINE_UP }
442 [VIS_ACTION_CURSOR_SCREEN_LINE_DOWN] = {
443 "vis-motion-screenline-down",
444 VIS_HELP("Move cursor screen/display line downwards")
445 movement, { .i = VIS_MOVE_SCREEN_LINE_DOWN }
447 [VIS_ACTION_CURSOR_SCREEN_LINE_BEGIN] = {
448 "vis-motion-screenline-begin",
449 VIS_HELP("Move cursor to beginning of screen/display line")
450 movement, { .i = VIS_MOVE_SCREEN_LINE_BEGIN }
452 [VIS_ACTION_CURSOR_SCREEN_LINE_MIDDLE] = {
453 "vis-motion-screenline-middle",
454 VIS_HELP("Move cursor to middle of screen/display line")
455 movement, { .i = VIS_MOVE_SCREEN_LINE_MIDDLE }
457 [VIS_ACTION_CURSOR_SCREEN_LINE_END] = {
458 "vis-motion-screenline-end",
459 VIS_HELP("Move cursor to end of screen/display line")
460 movement, { .i = VIS_MOVE_SCREEN_LINE_END }
462 [VIS_ACTION_CURSOR_PERCENT] = {
463 "vis-motion-percent",
464 VIS_HELP("Move to count % of file or matching item")
465 percent
467 [VIS_ACTION_CURSOR_BYTE] = {
468 "vis-motion-byte",
469 VIS_HELP("Move to absolute byte position")
470 movement, { .i = VIS_MOVE_BYTE }
472 [VIS_ACTION_CURSOR_BYTE_LEFT] = {
473 "vis-motion-byte-left",
474 VIS_HELP("Move count bytes to the left")
475 movement, { .i = VIS_MOVE_BYTE_LEFT }
477 [VIS_ACTION_CURSOR_BYTE_RIGHT] = {
478 "vis-motion-byte-right",
479 VIS_HELP("Move count bytes to the right")
480 movement, { .i = VIS_MOVE_BYTE_RIGHT }
482 [VIS_ACTION_CURSOR_PARAGRAPH_PREV] = {
483 "vis-motion-paragraph-prev",
484 VIS_HELP("Move cursor paragraph backward")
485 movement, { .i = VIS_MOVE_PARAGRAPH_PREV }
487 [VIS_ACTION_CURSOR_PARAGRAPH_NEXT] = {
488 "vis-motion-paragraph-next",
489 VIS_HELP("Move cursor paragraph forward")
490 movement, { .i = VIS_MOVE_PARAGRAPH_NEXT }
492 [VIS_ACTION_CURSOR_SENTENCE_PREV] = {
493 "vis-motion-sentence-prev",
494 VIS_HELP("Move cursor sentence backward")
495 movement, { .i = VIS_MOVE_SENTENCE_PREV }
497 [VIS_ACTION_CURSOR_SENTENCE_NEXT] = {
498 "vis-motion-sentence-next",
499 VIS_HELP("Move cursor sentence forward")
500 movement, { .i = VIS_MOVE_SENTENCE_NEXT }
502 [VIS_ACTION_CURSOR_BLOCK_START] = {
503 "vis-motion-block-start",
504 VIS_HELP("Move cursor to the opening curly brace in a block")
505 movement, { .i = VIS_MOVE_BLOCK_START }
507 [VIS_ACTION_CURSOR_BLOCK_END] = {
508 "vis-motion-block-end",
509 VIS_HELP("Move cursor to the closing curly brace in a block")
510 movement, { .i = VIS_MOVE_BLOCK_END }
512 [VIS_ACTION_CURSOR_PARENTHESIS_START] = {
513 "vis-motion-parenthesis-start",
514 VIS_HELP("Move cursor to the opening parenthesis inside a pair of parentheses")
515 movement, { .i = VIS_MOVE_PARENTHESIS_START }
517 [VIS_ACTION_CURSOR_PARENTHESIS_END] = {
518 "vis-motion-parenthesis-end",
519 VIS_HELP("Move cursor to the closing parenthesis inside a pair of parentheses")
520 movement, { .i = VIS_MOVE_PARENTHESIS_END }
522 [VIS_ACTION_CURSOR_COLUMN] = {
523 "vis-motion-column",
524 VIS_HELP("Move cursor to given column of current line")
525 movement, { .i = VIS_MOVE_COLUMN }
527 [VIS_ACTION_CURSOR_LINE_FIRST] = {
528 "vis-motion-line-first",
529 VIS_HELP("Move cursor to given line (defaults to first)")
530 gotoline, { .i = -1 }
532 [VIS_ACTION_CURSOR_LINE_LAST] = {
533 "vis-motion-line-last",
534 VIS_HELP("Move cursor to given line (defaults to last)")
535 gotoline, { .i = +1 }
537 [VIS_ACTION_CURSOR_WINDOW_LINE_TOP] = {
538 "vis-motion-window-line-top",
539 VIS_HELP("Move cursor to top line of the window")
540 movement, { .i = VIS_MOVE_WINDOW_LINE_TOP }
542 [VIS_ACTION_CURSOR_WINDOW_LINE_MIDDLE] = {
543 "vis-motion-window-line-middle",
544 VIS_HELP("Move cursor to middle line of the window")
545 movement, { .i = VIS_MOVE_WINDOW_LINE_MIDDLE }
547 [VIS_ACTION_CURSOR_WINDOW_LINE_BOTTOM] = {
548 "vis-motion-window-line-bottom",
549 VIS_HELP("Move cursor to bottom line of the window")
550 movement, { .i = VIS_MOVE_WINDOW_LINE_BOTTOM }
552 [VIS_ACTION_CURSOR_SEARCH_REPEAT_FORWARD] = {
553 "vis-motion-search-repeat-forward",
554 VIS_HELP("Move cursor to next match in forward direction")
555 movement, { .i = VIS_MOVE_SEARCH_REPEAT_FORWARD }
557 [VIS_ACTION_CURSOR_SEARCH_REPEAT_BACKWARD] = {
558 "vis-motion-search-repeat-backward",
559 VIS_HELP("Move cursor to previous match in backward direction")
560 movement, { .i = VIS_MOVE_SEARCH_REPEAT_BACKWARD }
562 [VIS_ACTION_CURSOR_SEARCH_REPEAT] = {
563 "vis-motion-search-repeat",
564 VIS_HELP("Move cursor to next match")
565 movement, { .i = VIS_MOVE_SEARCH_REPEAT }
567 [VIS_ACTION_CURSOR_SEARCH_REPEAT_REVERSE] = {
568 "vis-motion-search-repeat-reverse",
569 VIS_HELP("Move cursor to next match in opposite direction")
570 movement, { .i = VIS_MOVE_SEARCH_REPEAT_REVERSE }
572 [VIS_ACTION_CURSOR_SEARCH_WORD_FORWARD] = {
573 "vis-motion-search-word-forward",
574 VIS_HELP("Move cursor to next occurrence of the word under cursor")
575 movement, { .i = VIS_MOVE_SEARCH_WORD_FORWARD }
577 [VIS_ACTION_CURSOR_SEARCH_WORD_BACKWARD] = {
578 "vis-motion-search-word-backward",
579 VIS_HELP("Move cursor to previous occurrence of the word under cursor")
580 movement, { .i = VIS_MOVE_SEARCH_WORD_BACKWARD }
582 [VIS_ACTION_WINDOW_PAGE_UP] = {
583 "vis-window-page-up",
584 VIS_HELP("Scroll window pages backwards (upwards)")
585 wscroll, { .i = -PAGE }
587 [VIS_ACTION_WINDOW_HALFPAGE_UP] = {
588 "vis-window-halfpage-up",
589 VIS_HELP("Scroll window half pages backwards (upwards)")
590 wscroll, { .i = -PAGE_HALF }
592 [VIS_ACTION_WINDOW_PAGE_DOWN] = {
593 "vis-window-page-down",
594 VIS_HELP("Scroll window pages forwards (downwards)")
595 wscroll, { .i = +PAGE }
597 [VIS_ACTION_WINDOW_HALFPAGE_DOWN] = {
598 "vis-window-halfpage-down",
599 VIS_HELP("Scroll window half pages forwards (downwards)")
600 wscroll, { .i = +PAGE_HALF }
602 [VIS_ACTION_MODE_NORMAL] = {
603 "vis-mode-normal",
604 VIS_HELP("Enter normal mode")
605 switchmode, { .i = VIS_MODE_NORMAL }
607 [VIS_ACTION_MODE_NORMAL_ESCAPE] = {
608 "vis-mode-normal-escape",
609 VIS_HELP("Reset count or remove all non-primary selections")
610 normalmode_escape,
612 [VIS_ACTION_MODE_VISUAL] = {
613 "vis-mode-visual-charwise",
614 VIS_HELP("Enter characterwise visual mode")
615 switchmode, { .i = VIS_MODE_VISUAL }
617 [VIS_ACTION_MODE_VISUAL_ESCAPE] = {
618 "vis-mode-visual-escape",
619 VIS_HELP("Reset count or switch to normal mode")
620 visualmode_escape,
622 [VIS_ACTION_MODE_VISUAL_LINE] = {
623 "vis-mode-visual-linewise",
624 VIS_HELP("Enter linewise visual mode")
625 switchmode, { .i = VIS_MODE_VISUAL_LINE }
627 [VIS_ACTION_MODE_INSERT] = {
628 "vis-mode-insert",
629 VIS_HELP("Enter insert mode")
630 insertmode, { .i = VIS_MOVE_NOP }
632 [VIS_ACTION_MODE_REPLACE] = {
633 "vis-mode-replace",
634 VIS_HELP("Enter replace mode")
635 replacemode, { .i = VIS_MOVE_NOP }
637 [VIS_ACTION_DELETE_CHAR_PREV] = {
638 "vis-delete-char-prev",
639 VIS_HELP("Delete the previous character")
640 delete, { .i = VIS_MOVE_CHAR_PREV }
642 [VIS_ACTION_DELETE_CHAR_NEXT] = {
643 "vis-delete-char-next",
644 VIS_HELP("Delete the next character")
645 delete, { .i = VIS_MOVE_CHAR_NEXT }
647 [VIS_ACTION_DELETE_LINE_BEGIN] = {
648 "vis-delete-line-begin",
649 VIS_HELP("Delete until the start of the current line")
650 delete, { .i = VIS_MOVE_LINE_BEGIN }
652 [VIS_ACTION_DELETE_WORD_PREV] = {
653 "vis-delete-word-prev",
654 VIS_HELP("Delete the previous WORD")
655 delete, { .i = VIS_MOVE_WORD_START_PREV }
657 [VIS_ACTION_JUMPLIST_PREV] = {
658 "vis-jumplist-prev",
659 VIS_HELP("Go to older cursor position in jump list")
660 jumplist, { .i = -1 }
662 [VIS_ACTION_JUMPLIST_NEXT] = {
663 "vis-jumplist-next",
664 VIS_HELP("Go to newer cursor position in jump list")
665 jumplist, { .i = +1 }
667 [VIS_ACTION_JUMPLIST_SAVE] = {
668 "vis-jumplist-save",
669 VIS_HELP("Save current selections in jump list")
670 jumplist, { .i = 0 }
672 [VIS_ACTION_UNDO] = {
673 "vis-undo",
674 VIS_HELP("Undo last change")
675 undo,
677 [VIS_ACTION_REDO] = {
678 "vis-redo",
679 VIS_HELP("Redo last change")
680 redo,
682 [VIS_ACTION_EARLIER] = {
683 "vis-earlier",
684 VIS_HELP("Goto older text state")
685 earlier,
687 [VIS_ACTION_LATER] = {
688 "vis-later",
689 VIS_HELP("Goto newer text state")
690 later,
692 [VIS_ACTION_MACRO_RECORD] = {
693 "vis-macro-record",
694 VIS_HELP("Record macro into given register")
695 macro_record,
697 [VIS_ACTION_MACRO_REPLAY] = {
698 "vis-macro-replay",
699 VIS_HELP("Replay macro, execute the content of the given register")
700 macro_replay,
702 [VIS_ACTION_MARK] = {
703 "vis-mark",
704 VIS_HELP("Use given mark for next action")
705 mark,
707 [VIS_ACTION_REDRAW] = {
708 "vis-redraw",
709 VIS_HELP("Redraw current editor content")
710 call, { .f = vis_redraw }
712 [VIS_ACTION_REPLACE_CHAR] = {
713 "vis-replace-char",
714 VIS_HELP("Replace the character under the cursor")
715 replace,
717 [VIS_ACTION_TOTILL_REPEAT] = {
718 "vis-motion-totill-repeat",
719 VIS_HELP("Repeat latest to/till motion")
720 movement, { .i = VIS_MOVE_TOTILL_REPEAT }
722 [VIS_ACTION_TOTILL_REVERSE] = {
723 "vis-motion-totill-reverse",
724 VIS_HELP("Repeat latest to/till motion but in opposite direction")
725 movement, { .i = VIS_MOVE_TOTILL_REVERSE }
727 [VIS_ACTION_PROMPT_SEARCH_FORWARD] = {
728 "vis-search-forward",
729 VIS_HELP("Search forward")
730 prompt_show, { .s = "/" }
732 [VIS_ACTION_PROMPT_SEARCH_BACKWARD] = {
733 "vis-search-backward",
734 VIS_HELP("Search backward")
735 prompt_show, { .s = "?" }
737 [VIS_ACTION_TILL_LEFT] = {
738 "vis-motion-till-left",
739 VIS_HELP("Till after the occurrence of character to the left")
740 movement_key, { .i = VIS_MOVE_LEFT_TILL }
742 [VIS_ACTION_TILL_RIGHT] = {
743 "vis-motion-till-right",
744 VIS_HELP("Till before the occurrence of character to the right")
745 movement_key, { .i = VIS_MOVE_RIGHT_TILL }
747 [VIS_ACTION_TO_LEFT] = {
748 "vis-motion-to-left",
749 VIS_HELP("To the first occurrence of character to the left")
750 movement_key, { .i = VIS_MOVE_LEFT_TO }
752 [VIS_ACTION_TO_RIGHT] = {
753 "vis-motion-to-right",
754 VIS_HELP("To the first occurrence of character to the right")
755 movement_key, { .i = VIS_MOVE_RIGHT_TO }
757 [VIS_ACTION_REGISTER] = {
758 "vis-register",
759 VIS_HELP("Use given register for next operator")
760 reg,
762 [VIS_ACTION_OPERATOR_CHANGE] = {
763 "vis-operator-change",
764 VIS_HELP("Change operator")
765 operator, { .i = VIS_OP_CHANGE }
767 [VIS_ACTION_OPERATOR_DELETE] = {
768 "vis-operator-delete",
769 VIS_HELP("Delete operator")
770 operator, { .i = VIS_OP_DELETE }
772 [VIS_ACTION_OPERATOR_YANK] = {
773 "vis-operator-yank",
774 VIS_HELP("Yank operator")
775 operator, { .i = VIS_OP_YANK }
777 [VIS_ACTION_OPERATOR_SHIFT_LEFT] = {
778 "vis-operator-shift-left",
779 VIS_HELP("Shift left operator")
780 operator, { .i = VIS_OP_SHIFT_LEFT }
782 [VIS_ACTION_OPERATOR_SHIFT_RIGHT] = {
783 "vis-operator-shift-right",
784 VIS_HELP("Shift right operator")
785 operator, { .i = VIS_OP_SHIFT_RIGHT }
787 [VIS_ACTION_COUNT] = {
788 "vis-count",
789 VIS_HELP("Count specifier")
790 count,
792 [VIS_ACTION_INSERT_NEWLINE] = {
793 "vis-insert-newline",
794 VIS_HELP("Insert a line break (depending on file type)")
795 call, { .f = vis_insert_nl }
797 [VIS_ACTION_INSERT_TAB] = {
798 "vis-insert-tab",
799 VIS_HELP("Insert a tab (might be converted to spaces)")
800 call, { .f = vis_insert_tab }
802 [VIS_ACTION_INSERT_VERBATIM] = {
803 "vis-insert-verbatim",
804 VIS_HELP("Insert Unicode character based on code point")
805 insert_verbatim,
807 [VIS_ACTION_INSERT_REGISTER] = {
808 "vis-insert-register",
809 VIS_HELP("Insert specified register content")
810 insert_register,
812 [VIS_ACTION_WINDOW_NEXT] = {
813 "vis-window-next",
814 VIS_HELP("Focus next window")
815 call, { .f = vis_window_next }
817 [VIS_ACTION_WINDOW_PREV] = {
818 "vis-window-prev",
819 VIS_HELP("Focus previous window")
820 call, { .f = vis_window_prev }
822 [VIS_ACTION_APPEND_CHAR_NEXT] = {
823 "vis-append-char-next",
824 VIS_HELP("Append text after the cursor")
825 insertmode, { .i = VIS_MOVE_LINE_CHAR_NEXT }
827 [VIS_ACTION_APPEND_LINE_END] = {
828 "vis-append-line-end",
829 VIS_HELP("Append text after the end of the line")
830 insertmode, { .i = VIS_MOVE_LINE_END },
832 [VIS_ACTION_INSERT_LINE_START] = {
833 "vis-insert-line-start",
834 VIS_HELP("Insert text before the first non-blank in the line")
835 insertmode, { .i = VIS_MOVE_LINE_START },
837 [VIS_ACTION_OPEN_LINE_ABOVE] = {
838 "vis-open-line-above",
839 VIS_HELP("Begin a new line above the cursor")
840 openline, { .i = -1 }
842 [VIS_ACTION_OPEN_LINE_BELOW] = {
843 "vis-open-line-below",
844 VIS_HELP("Begin a new line below the cursor")
845 openline, { .i = +1 }
847 [VIS_ACTION_JOIN_LINES] = {
848 "vis-join-lines",
849 VIS_HELP("Join selected lines")
850 join, { .s = " " }
852 [VIS_ACTION_JOIN_LINES_TRIM] = {
853 "vis-join-lines-trim",
854 VIS_HELP("Join selected lines, remove white space")
855 join, { .s = "" }
857 [VIS_ACTION_PROMPT_SHOW] = {
858 "vis-prompt-show",
859 VIS_HELP("Show editor command line prompt")
860 prompt_show, { .s = ":" }
862 [VIS_ACTION_REPEAT] = {
863 "vis-repeat",
864 VIS_HELP("Repeat latest editor command")
865 repeat
867 [VIS_ACTION_SELECTION_FLIP] = {
868 "vis-selection-flip",
869 VIS_HELP("Flip selection, move cursor to other end")
870 selection_end,
872 [VIS_ACTION_WINDOW_REDRAW_TOP] = {
873 "vis-window-redraw-top",
874 VIS_HELP("Redraw cursor line at the top of the window")
875 window, { .w = view_redraw_top }
877 [VIS_ACTION_WINDOW_REDRAW_CENTER] = {
878 "vis-window-redraw-center",
879 VIS_HELP("Redraw cursor line at the center of the window")
880 window, { .w = view_redraw_center }
882 [VIS_ACTION_WINDOW_REDRAW_BOTTOM] = {
883 "vis-window-redraw-bottom",
884 VIS_HELP("Redraw cursor line at the bottom of the window")
885 window, { .w = view_redraw_bottom }
887 [VIS_ACTION_WINDOW_SLIDE_UP] = {
888 "vis-window-slide-up",
889 VIS_HELP("Slide window content upwards")
890 wslide, { .i = -1 }
892 [VIS_ACTION_WINDOW_SLIDE_DOWN] = {
893 "vis-window-slide-down",
894 VIS_HELP("Slide window content downwards")
895 wslide, { .i = +1 }
897 [VIS_ACTION_PUT_AFTER] = {
898 "vis-put-after",
899 VIS_HELP("Put text after the cursor")
900 operator, { .i = VIS_OP_PUT_AFTER }
902 [VIS_ACTION_PUT_BEFORE] = {
903 "vis-put-before",
904 VIS_HELP("Put text before the cursor")
905 operator, { .i = VIS_OP_PUT_BEFORE }
907 [VIS_ACTION_SELECTIONS_MATCH_WORD] = {
908 "vis-selections-select-word",
909 VIS_HELP("Select word under cursor")
910 selections_match_word,
912 [VIS_ACTION_SELECTIONS_NEW_LINE_ABOVE] = {
913 "vis-selection-new-lines-above",
914 VIS_HELP("Create a new selection on the line above")
915 selections_new, { .i = -1 }
917 [VIS_ACTION_SELECTIONS_NEW_LINE_ABOVE_FIRST] = {
918 "vis-selection-new-lines-above-first",
919 VIS_HELP("Create a new selection on the line above the first selection")
920 selections_new, { .i = INT_MIN }
922 [VIS_ACTION_SELECTIONS_NEW_LINE_BELOW] = {
923 "vis-selection-new-lines-below",
924 VIS_HELP("Create a new selection on the line below")
925 selections_new, { .i = +1 }
927 [VIS_ACTION_SELECTIONS_NEW_LINE_BELOW_LAST] = {
928 "vis-selection-new-lines-below-last",
929 VIS_HELP("Create a new selection on the line below the last selection")
930 selections_new, { .i = INT_MAX }
932 [VIS_ACTION_SELECTIONS_NEW_LINES_BEGIN] = {
933 "vis-selection-new-lines-begin",
934 VIS_HELP("Create a new selection at the start of every line covered by selection")
935 operator, { .i = VIS_OP_CURSOR_SOL }
937 [VIS_ACTION_SELECTIONS_NEW_LINES_END] = {
938 "vis-selection-new-lines-end",
939 VIS_HELP("Create a new selection at the end of every line covered by selection")
940 operator, { .i = VIS_OP_CURSOR_EOL }
942 [VIS_ACTION_SELECTIONS_NEW_MATCH_NEXT] = {
943 "vis-selection-new-match-next",
944 VIS_HELP("Select the next region matching the current selection")
945 selections_match_next,
947 [VIS_ACTION_SELECTIONS_NEW_MATCH_SKIP] = {
948 "vis-selection-new-match-skip",
949 VIS_HELP("Clear current selection, but select next match")
950 selections_match_skip,
952 [VIS_ACTION_SELECTIONS_ALIGN] = {
953 "vis-selections-align",
954 VIS_HELP("Try to align all selections on the same column")
955 selections_align,
957 [VIS_ACTION_SELECTIONS_ALIGN_INDENT_LEFT] = {
958 "vis-selections-align-indent-left",
959 VIS_HELP("Left-align all selections by inserting spaces")
960 selections_align_indent, { .i = -1 }
962 [VIS_ACTION_SELECTIONS_ALIGN_INDENT_RIGHT] = {
963 "vis-selections-align-indent-right",
964 VIS_HELP("Right-align all selections by inserting spaces")
965 selections_align_indent, { .i = +1 }
967 [VIS_ACTION_SELECTIONS_REMOVE_ALL] = {
968 "vis-selections-remove-all",
969 VIS_HELP("Remove all but the primary selection")
970 selections_clear,
972 [VIS_ACTION_SELECTIONS_REMOVE_LAST] = {
973 "vis-selections-remove-last",
974 VIS_HELP("Remove primary selection")
975 selections_remove,
977 [VIS_ACTION_SELECTIONS_REMOVE_COLUMN] = {
978 "vis-selections-remove-column",
979 VIS_HELP("Remove count selection column")
980 selections_remove_column, { .i = 1 }
982 [VIS_ACTION_SELECTIONS_REMOVE_COLUMN_EXCEPT] = {
983 "vis-selections-remove-column-except",
984 VIS_HELP("Remove all but the count selection column")
985 selections_remove_column_except, { .i = 1 }
987 [VIS_ACTION_SELECTIONS_PREV] = {
988 "vis-selection-prev",
989 VIS_HELP("Move to the previous selection")
990 selections_navigate, { .i = -PAGE_HALF }
992 [VIS_ACTION_SELECTIONS_NEXT] = {
993 "vis-selection-next",
994 VIS_HELP("Move to the next selection")
995 selections_navigate, { .i = +PAGE_HALF }
997 [VIS_ACTION_SELECTIONS_ROTATE_LEFT] = {
998 "vis-selections-rotate-left",
999 VIS_HELP("Rotate selections left")
1000 selections_rotate, { .i = -1 }
1002 [VIS_ACTION_SELECTIONS_ROTATE_RIGHT] = {
1003 "vis-selections-rotate-right",
1004 VIS_HELP("Rotate selections right")
1005 selections_rotate, { .i = +1 }
1007 [VIS_ACTION_SELECTIONS_TRIM] = {
1008 "vis-selections-trim",
1009 VIS_HELP("Remove leading and trailing white space from selections")
1010 selections_trim
1012 [VIS_ACTION_SELECTIONS_SAVE] = {
1013 "vis-selections-save",
1014 VIS_HELP("Save currently active selections to mark")
1015 selections_save
1017 [VIS_ACTION_SELECTIONS_RESTORE] = {
1018 "vis-selections-restore",
1019 VIS_HELP("Restore selections from mark")
1020 selections_restore
1022 [VIS_ACTION_SELECTIONS_UNION] = {
1023 "vis-selections-union",
1024 VIS_HELP("Add selections from mark")
1025 selections_union
1027 [VIS_ACTION_SELECTIONS_INTERSECT] = {
1028 "vis-selections-intersect",
1029 VIS_HELP("Intersect with selections from mark")
1030 selections_intersect
1032 [VIS_ACTION_SELECTIONS_COMPLEMENT] = {
1033 "vis-selections-complement",
1034 VIS_HELP("Complement selections")
1035 selections_complement
1037 [VIS_ACTION_SELECTIONS_MINUS] = {
1038 "vis-selections-minus",
1039 VIS_HELP("Subtract selections from mark")
1040 selections_minus
1042 [VIS_ACTION_SELECTIONS_COMBINE_INTERSECT] = {
1043 "vis-selections-combine-intersect",
1044 VIS_HELP("Pairwise intersect with selections from mark")
1045 selections_combine, { .combine = combine_intersect }
1047 [VIS_ACTION_SELECTIONS_COMBINE_LONGER] = {
1048 "vis-selections-combine-longer",
1049 VIS_HELP("Pairwise combine: take longer")
1050 selections_combine, { .combine = combine_longer }
1052 [VIS_ACTION_SELECTIONS_COMBINE_SHORTER] = {
1053 "vis-selections-combine-shorter",
1054 VIS_HELP("Pairwise combine: take shorter")
1055 selections_combine, { .combine = combine_shorter }
1057 [VIS_ACTION_SELECTIONS_COMBINE_LEFTMOST] = {
1058 "vis-selections-combine-leftmost",
1059 VIS_HELP("Pairwise combine: leftmost")
1060 selections_combine, { .combine = combine_leftmost }
1062 [VIS_ACTION_SELECTIONS_COMBINE_RIGHTMOST] = {
1063 "vis-selections-combine-rightmost",
1064 VIS_HELP("Pairwise combine: rightmost")
1065 selections_combine, { .combine = combine_rightmost }
1067 [VIS_ACTION_TEXT_OBJECT_WORD_OUTER] = {
1068 "vis-textobject-word-outer",
1069 VIS_HELP("A word leading and trailing whitespace included")
1070 textobj, { .i = VIS_TEXTOBJECT_OUTER_WORD }
1072 [VIS_ACTION_TEXT_OBJECT_WORD_INNER] = {
1073 "vis-textobject-word-inner",
1074 VIS_HELP("A word leading and trailing whitespace excluded")
1075 textobj, { .i = VIS_TEXTOBJECT_INNER_WORD }
1077 [VIS_ACTION_TEXT_OBJECT_LONGWORD_OUTER] = {
1078 "vis-textobject-bigword-outer",
1079 VIS_HELP("A WORD leading and trailing whitespace included")
1080 textobj, { .i = VIS_TEXTOBJECT_OUTER_LONGWORD }
1082 [VIS_ACTION_TEXT_OBJECT_LONGWORD_INNER] = {
1083 "vis-textobject-bigword-inner",
1084 VIS_HELP("A WORD leading and trailing whitespace excluded")
1085 textobj, { .i = VIS_TEXTOBJECT_INNER_LONGWORD }
1087 [VIS_ACTION_TEXT_OBJECT_SENTENCE] = {
1088 "vis-textobject-sentence",
1089 VIS_HELP("A sentence")
1090 textobj, { .i = VIS_TEXTOBJECT_SENTENCE }
1092 [VIS_ACTION_TEXT_OBJECT_PARAGRAPH] = {
1093 "vis-textobject-paragraph",
1094 VIS_HELP("A paragraph")
1095 textobj, { .i = VIS_TEXTOBJECT_PARAGRAPH }
1097 [VIS_ACTION_TEXT_OBJECT_PARAGRAPH_OUTER] = {
1098 "vis-textobject-paragraph-outer",
1099 VIS_HELP("A paragraph (outer variant)")
1100 textobj, { .i = VIS_TEXTOBJECT_PARAGRAPH_OUTER }
1102 [VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_OUTER] = {
1103 "vis-textobject-square-bracket-outer",
1104 VIS_HELP("[] block (outer variant)")
1105 textobj, { .i = VIS_TEXTOBJECT_OUTER_SQUARE_BRACKET }
1107 [VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_INNER] = {
1108 "vis-textobject-square-bracket-inner",
1109 VIS_HELP("[] block (inner variant)")
1110 textobj, { .i = VIS_TEXTOBJECT_INNER_SQUARE_BRACKET }
1112 [VIS_ACTION_TEXT_OBJECT_PARENTHESIS_OUTER] = {
1113 "vis-textobject-parenthesis-outer",
1114 VIS_HELP("() block (outer variant)")
1115 textobj, { .i = VIS_TEXTOBJECT_OUTER_PARENTHESIS }
1117 [VIS_ACTION_TEXT_OBJECT_PARENTHESIS_INNER] = {
1118 "vis-textobject-parenthesis-inner",
1119 VIS_HELP("() block (inner variant)")
1120 textobj, { .i = VIS_TEXTOBJECT_INNER_PARENTHESIS }
1122 [VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_OUTER] = {
1123 "vis-textobject-angle-bracket-outer",
1124 VIS_HELP("<> block (outer variant)")
1125 textobj, { .i = VIS_TEXTOBJECT_OUTER_ANGLE_BRACKET }
1127 [VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_INNER] = {
1128 "vis-textobject-angle-bracket-inner",
1129 VIS_HELP("<> block (inner variant)")
1130 textobj, { .i = VIS_TEXTOBJECT_INNER_ANGLE_BRACKET }
1132 [VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_OUTER] = {
1133 "vis-textobject-curly-bracket-outer",
1134 VIS_HELP("{} block (outer variant)")
1135 textobj, { .i = VIS_TEXTOBJECT_OUTER_CURLY_BRACKET }
1137 [VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_INNER] = {
1138 "vis-textobject-curly-bracket-inner",
1139 VIS_HELP("{} block (inner variant)")
1140 textobj, { .i = VIS_TEXTOBJECT_INNER_CURLY_BRACKET }
1142 [VIS_ACTION_TEXT_OBJECT_QUOTE_OUTER] = {
1143 "vis-textobject-quote-outer",
1144 VIS_HELP("A quoted string, including the quotation marks")
1145 textobj, { .i = VIS_TEXTOBJECT_OUTER_QUOTE }
1147 [VIS_ACTION_TEXT_OBJECT_QUOTE_INNER] = {
1148 "vis-textobject-quote-inner",
1149 VIS_HELP("A quoted string, excluding the quotation marks")
1150 textobj, { .i = VIS_TEXTOBJECT_INNER_QUOTE }
1152 [VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_OUTER] = {
1153 "vis-textobject-single-quote-outer",
1154 VIS_HELP("A single quoted string, including the quotation marks")
1155 textobj, { .i = VIS_TEXTOBJECT_OUTER_SINGLE_QUOTE }
1157 [VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_INNER] = {
1158 "vis-textobject-single-quote-inner",
1159 VIS_HELP("A single quoted string, excluding the quotation marks")
1160 textobj, { .i = VIS_TEXTOBJECT_INNER_SINGLE_QUOTE }
1162 [VIS_ACTION_TEXT_OBJECT_BACKTICK_OUTER] = {
1163 "vis-textobject-backtick-outer",
1164 VIS_HELP("A backtick delimited string (outer variant)")
1165 textobj, { .i = VIS_TEXTOBJECT_OUTER_BACKTICK }
1167 [VIS_ACTION_TEXT_OBJECT_BACKTICK_INNER] = {
1168 "vis-textobject-backtick-inner",
1169 VIS_HELP("A backtick delimited string (inner variant)")
1170 textobj, { .i = VIS_TEXTOBJECT_INNER_BACKTICK }
1172 [VIS_ACTION_TEXT_OBJECT_ENTIRE_OUTER] = {
1173 "vis-textobject-entire-outer",
1174 VIS_HELP("The whole text content")
1175 textobj, { .i = VIS_TEXTOBJECT_OUTER_ENTIRE }
1177 [VIS_ACTION_TEXT_OBJECT_ENTIRE_INNER] = {
1178 "vis-textobject-entire-inner",
1179 VIS_HELP("The whole text content, except for leading and trailing empty lines")
1180 textobj, { .i = VIS_TEXTOBJECT_INNER_ENTIRE }
1182 [VIS_ACTION_TEXT_OBJECT_LINE_OUTER] = {
1183 "vis-textobject-line-outer",
1184 VIS_HELP("The whole line")
1185 textobj, { .i = VIS_TEXTOBJECT_OUTER_LINE }
1187 [VIS_ACTION_TEXT_OBJECT_LINE_INNER] = {
1188 "vis-textobject-line-inner",
1189 VIS_HELP("The whole line, excluding leading and trailing whitespace")
1190 textobj, { .i = VIS_TEXTOBJECT_INNER_LINE }
1192 [VIS_ACTION_TEXT_OBJECT_INDENTATION] = {
1193 "vis-textobject-indentation",
1194 VIS_HELP("All adjacent lines with the same indentation level as the current one")
1195 textobj, { .i = VIS_TEXTOBJECT_INDENTATION }
1197 [VIS_ACTION_TEXT_OBJECT_SEARCH_FORWARD] = {
1198 "vis-textobject-search-forward",
1199 VIS_HELP("The next search match in forward direction")
1200 textobj, { .i = VIS_TEXTOBJECT_SEARCH_FORWARD }
1202 [VIS_ACTION_TEXT_OBJECT_SEARCH_BACKWARD] = {
1203 "vis-textobject-search-backward",
1204 VIS_HELP("The next search match in backward direction")
1205 textobj, { .i = VIS_TEXTOBJECT_SEARCH_BACKWARD }
1207 [VIS_ACTION_UNICODE_INFO] = {
1208 "vis-unicode-info",
1209 VIS_HELP("Show Unicode codepoint(s) of character under cursor")
1210 unicode_info, { .i = VIS_ACTION_UNICODE_INFO }
1212 [VIS_ACTION_UTF8_INFO] = {
1213 "vis-utf8-info",
1214 VIS_HELP("Show UTF-8 encoded codepoint(s) of character under cursor")
1215 unicode_info, { .i = VIS_ACTION_UTF8_INFO }
1217 [VIS_ACTION_NOP] = {
1218 "vis-nop",
1219 VIS_HELP("Ignore key, do nothing")
1220 nop,
1224 #include "config.h"
1226 /** key bindings functions */
1228 static const char *nop(Vis *vis, const char *keys, const Arg *arg) {
1229 return keys;
1232 static const char *macro_record(Vis *vis, const char *keys, const Arg *arg) {
1233 if (!vis_macro_record_stop(vis)) {
1234 if (!keys[0])
1235 return NULL;
1236 const char *next = vis_keys_next(vis, keys);
1237 if (next - keys > 1)
1238 return next;
1239 enum VisRegister reg = vis_register_from(vis, keys[0]);
1240 vis_macro_record(vis, reg);
1241 keys++;
1243 vis_draw(vis);
1244 return keys;
1247 static const char *macro_replay(Vis *vis, const char *keys, const Arg *arg) {
1248 if (!keys[0])
1249 return NULL;
1250 const char *next = vis_keys_next(vis, keys);
1251 if (next - keys > 1)
1252 return next;
1253 enum VisRegister reg = vis_register_from(vis, keys[0]);
1254 vis_macro_replay(vis, reg);
1255 return keys+1;
1258 static const char *suspend(Vis *vis, const char *keys, const Arg *arg) {
1259 vis_suspend(vis);
1260 return keys;
1263 static const char *repeat(Vis *vis, const char *keys, const Arg *arg) {
1264 vis_repeat(vis);
1265 return keys;
1268 static const char *selections_new(Vis *vis, const char *keys, const Arg *arg) {
1269 View *view = vis_view(vis);
1270 bool anchored = view_selections_anchored(view_selections_primary_get(view));
1271 VisCountIterator it = vis_count_iterator_get(vis, 1);
1272 while (vis_count_iterator_next(&it)) {
1273 Selection *sel = NULL;
1274 switch (arg->i) {
1275 case -1:
1276 case +1:
1277 sel = view_selections_primary_get(view);
1278 break;
1279 case INT_MIN:
1280 sel = view_selections(view);
1281 break;
1282 case INT_MAX:
1283 for (Selection *s = view_selections(view); s; s = view_selections_next(s))
1284 sel = s;
1285 break;
1288 if (!sel)
1289 return keys;
1291 size_t oldpos = view_cursors_pos(sel);
1292 if (arg->i > 0)
1293 view_line_down(sel);
1294 else if (arg->i < 0)
1295 view_line_up(sel);
1296 size_t newpos = view_cursors_pos(sel);
1297 view_cursors_to(sel, oldpos);
1298 Selection *sel_new = view_selections_new(view, newpos);
1299 if (!sel_new) {
1300 if (arg->i == -1)
1301 sel_new = view_selections_prev(sel);
1302 else if (arg->i == +1)
1303 sel_new = view_selections_next(sel);
1305 if (sel_new) {
1306 view_selections_primary_set(sel_new);
1307 view_selections_anchor(sel_new, anchored);
1310 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1311 return keys;
1314 static const char *selections_align(Vis *vis, const char *keys, const Arg *arg) {
1315 View *view = vis_view(vis);
1316 Text *txt = vis_text(vis);
1317 int mincol = INT_MAX;
1318 for (Selection *s = view_selections(view); s; s = view_selections_next(s)) {
1319 int col = view_cursors_cell_get(s);
1320 if (col >= 0 && col < mincol)
1321 mincol = col;
1323 for (Selection *s = view_selections(view); s; s = view_selections_next(s)) {
1324 if (view_cursors_cell_set(s, mincol) == -1) {
1325 size_t pos = view_cursors_pos(s);
1326 size_t col = text_line_width_set(txt, pos, mincol);
1327 view_cursors_to(s, col);
1330 return keys;
1333 static const char *selections_align_indent(Vis *vis, const char *keys, const Arg *arg) {
1334 View *view = vis_view(vis);
1335 Text *txt = vis_text(vis);
1336 bool left_align = arg->i < 0;
1337 int columns = view_selections_column_count(view);
1339 for (int i = 0; i < columns; i++) {
1340 int mincol = INT_MAX, maxcol = 0;
1341 for (Selection *s = view_selections_column(view, i); s; s = view_selections_column_next(s, i)) {
1342 Filerange sel = view_selections_get(s);
1343 size_t pos = left_align ? sel.start : sel.end;
1344 int col = text_line_width_get(txt, pos);
1345 if (col < mincol)
1346 mincol = col;
1347 if (col > maxcol)
1348 maxcol = col;
1351 size_t len = maxcol - mincol;
1352 char *buf = malloc(len+1);
1353 if (!buf)
1354 return keys;
1355 memset(buf, ' ', len);
1357 for (Selection *s = view_selections_column(view, i); s; s = view_selections_column_next(s, i)) {
1358 Filerange sel = view_selections_get(s);
1359 size_t pos = left_align ? sel.start : sel.end;
1360 size_t ipos = sel.start;
1361 int col = text_line_width_get(txt, pos);
1362 if (col < maxcol) {
1363 size_t off = maxcol - col;
1364 if (off <= len)
1365 text_insert(txt, ipos, buf, off);
1369 free(buf);
1372 view_draw(view);
1373 return keys;
1376 static const char *selections_clear(Vis *vis, const char *keys, const Arg *arg) {
1377 View *view = vis_view(vis);
1378 if (view_selections_count(view) > 1)
1379 view_selections_dispose_all(view);
1380 else
1381 view_selection_clear(view_selections_primary_get(view));
1382 return keys;
1385 static const char *selections_match_word(Vis *vis, const char *keys, const Arg *arg) {
1386 Text *txt = vis_text(vis);
1387 View *view = vis_view(vis);
1388 for (Selection *s = view_selections(view); s; s = view_selections_next(s)) {
1389 Filerange word = text_object_word(txt, view_cursors_pos(s));
1390 if (text_range_valid(&word))
1391 view_selections_set(s, &word);
1393 vis_mode_switch(vis, VIS_MODE_VISUAL);
1394 return keys;
1397 static const Selection *selection_new_primary(View *view, Filerange *r) {
1398 Text *txt = view_text(view);
1399 size_t pos = text_char_prev(txt, r->end);
1400 Selection *s = view_selections_new(view, pos);
1401 if (!s)
1402 return NULL;
1403 view_selections_set(s, r);
1404 view_selections_anchor(s, true);
1405 view_selections_primary_set(s);
1406 return s;
1409 static const char *selections_match_next_literal(Vis *vis, const char *keys, const Arg *arg) {
1410 Text *txt = vis_text(vis);
1411 View *view = vis_view(vis);
1412 Selection *s = view_selections_primary_get(view);
1413 Filerange sel = view_selections_get(s);
1414 size_t len = text_range_size(&sel);
1415 if (!len)
1416 return keys;
1418 char *buf = text_bytes_alloc0(txt, sel.start, len);
1419 if (!buf)
1420 return keys;
1422 size_t start = text_find_next(txt, sel.end, buf);
1423 Filerange match = text_range_new(start, start+len);
1424 if (start != sel.end && selection_new_primary(view, &match))
1425 goto out;
1427 sel = view_selections_get(view_selections(view));
1428 start = text_find_prev(txt, sel.start, buf);
1429 if (start == sel.start)
1430 goto out;
1432 match = text_range_new(start, start+len);
1433 selection_new_primary(view, &match);
1435 out:
1436 free(buf);
1437 return keys;
1440 static const char *selections_match_next(Vis *vis, const char *keys, const Arg *arg) {
1441 Text *txt = vis_text(vis);
1442 View *view = vis_view(vis);
1443 Selection *s = view_selections_primary_get(view);
1444 Filerange sel = view_selections_get(s);
1445 if (!text_range_valid(&sel))
1446 return keys;
1448 Filerange word = text_object_word(txt, view_cursors_pos(s));
1449 if (!text_range_equal(&sel, &word))
1450 return selections_match_next_literal(vis, keys, arg);
1452 char *buf = text_bytes_alloc0(txt, sel.start, text_range_size(&sel));
1453 if (!buf)
1454 return keys;
1456 word = text_object_word_find_next(txt, sel.end, buf);
1457 if (text_range_valid(&word) && selection_new_primary(view, &word))
1458 goto out;
1460 sel = view_selections_get(view_selections(view));
1461 word = text_object_word_find_prev(txt, sel.start, buf);
1462 if (!text_range_valid(&word))
1463 goto out;
1464 selection_new_primary(view, &word);
1466 out:
1467 free(buf);
1468 return keys;
1471 static const char *selections_match_skip(Vis *vis, const char *keys, const Arg *arg) {
1472 View *view = vis_view(vis);
1473 Selection *sel = view_selections_primary_get(view);
1474 keys = selections_match_next(vis, keys, arg);
1475 if (sel != view_selections_primary_get(view))
1476 view_selections_dispose(sel);
1477 return keys;
1480 static const char *selections_remove(Vis *vis, const char *keys, const Arg *arg) {
1481 View *view = vis_view(vis);
1482 view_selections_dispose(view_selections_primary_get(view));
1483 view_cursor_to(view, view_cursor_get(view));
1484 return keys;
1487 static const char *selections_remove_column(Vis *vis, const char *keys, const Arg *arg) {
1488 View *view = vis_view(vis);
1489 int max = view_selections_column_count(view);
1490 int column = vis_count_get_default(vis, arg->i) - 1;
1491 if (column >= max)
1492 column = max - 1;
1493 if (view_selections_count(view) == 1) {
1494 vis_keys_feed(vis, "<Escape>");
1495 return keys;
1498 for (Selection *s = view_selections_column(view, column), *next; s; s = next) {
1499 next = view_selections_column_next(s, column);
1500 view_selections_dispose(s);
1503 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1504 return keys;
1507 static const char *selections_remove_column_except(Vis *vis, const char *keys, const Arg *arg) {
1508 View *view = vis_view(vis);
1509 int max = view_selections_column_count(view);
1510 int column = vis_count_get_default(vis, arg->i) - 1;
1511 if (column >= max)
1512 column = max - 1;
1513 if (view_selections_count(view) == 1) {
1514 vis_redraw(vis);
1515 return keys;
1518 Selection *sel = view_selections(view);
1519 Selection *col = view_selections_column(view, column);
1520 for (Selection *next; sel; sel = next) {
1521 next = view_selections_next(sel);
1522 if (sel == col)
1523 col = view_selections_column_next(col, column);
1524 else
1525 view_selections_dispose(sel);
1528 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1529 return keys;
1532 static const char *selections_navigate(Vis *vis, const char *keys, const Arg *arg) {
1533 View *view = vis_view(vis);
1534 if (view_selections_count(view) == 1)
1535 return wscroll(vis, keys, arg);
1536 Selection *s = view_selections_primary_get(view);
1537 VisCountIterator it = vis_count_iterator_get(vis, 1);
1538 while (vis_count_iterator_next(&it)) {
1539 if (arg->i > 0) {
1540 s = view_selections_next(s);
1541 if (!s)
1542 s = view_selections(view);
1543 } else {
1544 s = view_selections_prev(s);
1545 if (!s) {
1546 s = view_selections(view);
1547 for (Selection *n = s; n; n = view_selections_next(n))
1548 s = n;
1552 view_selections_primary_set(s);
1553 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1554 return keys;
1557 static const char *selections_rotate(Vis *vis, const char *keys, const Arg *arg) {
1559 typedef struct {
1560 Selection *sel;
1561 char *data;
1562 size_t len;
1563 } Rotate;
1565 Array arr;
1566 Text *txt = vis_text(vis);
1567 View *view = vis_view(vis);
1568 int columns = view_selections_column_count(view);
1569 int selections = columns == 1 ? view_selections_count(view) : columns;
1570 int count = vis_count_get_default(vis, 1);
1571 array_init_sized(&arr, sizeof(Rotate));
1572 if (!array_reserve(&arr, selections))
1573 return keys;
1574 size_t line = 0;
1576 for (Selection *s = view_selections(view), *next; s; s = next) {
1577 next = view_selections_next(s);
1578 size_t line_next = 0;
1580 Filerange sel = view_selections_get(s);
1581 Rotate rot;
1582 rot.sel = s;
1583 rot.len = text_range_size(&sel);
1584 if ((rot.data = malloc(rot.len)))
1585 rot.len = text_bytes_get(txt, sel.start, rot.len, rot.data);
1586 else
1587 rot.len = 0;
1588 array_add(&arr, &rot);
1590 if (!line)
1591 line = text_lineno_by_pos(txt, view_cursors_pos(s));
1592 if (next)
1593 line_next = text_lineno_by_pos(txt, view_cursors_pos(next));
1594 if (!next || (columns > 1 && line != line_next)) {
1595 size_t len = array_length(&arr);
1596 size_t off = arg->i > 0 ? count % len : len - (count % len);
1597 for (size_t i = 0; i < len; i++) {
1598 size_t j = (i + off) % len;
1599 Rotate *oldrot = array_get(&arr, i);
1600 Rotate *newrot = array_get(&arr, j);
1601 if (!oldrot || !newrot || oldrot == newrot)
1602 continue;
1603 Filerange newsel = view_selections_get(newrot->sel);
1604 if (!text_range_valid(&newsel))
1605 continue;
1606 if (!text_delete_range(txt, &newsel))
1607 continue;
1608 if (!text_insert(txt, newsel.start, oldrot->data, oldrot->len))
1609 continue;
1610 newsel.end = newsel.start + oldrot->len;
1611 view_selections_set(newrot->sel, &newsel);
1612 free(oldrot->data);
1614 array_clear(&arr);
1616 line = line_next;
1619 array_release(&arr);
1620 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1621 return keys;
1624 static const char *selections_trim(Vis *vis, const char *keys, const Arg *arg) {
1625 Text *txt = vis_text(vis);
1626 View *view = vis_view(vis);
1627 for (Selection *s = view_selections(view), *next; s; s = next) {
1628 next = view_selections_next(s);
1629 Filerange sel = view_selections_get(s);
1630 if (!text_range_valid(&sel))
1631 continue;
1632 for (char b; sel.start < sel.end && text_byte_get(txt, sel.end-1, &b)
1633 && isspace((unsigned char)b); sel.end--);
1634 for (char b; sel.start <= sel.end && text_byte_get(txt, sel.start, &b)
1635 && isspace((unsigned char)b); sel.start++);
1636 if (sel.start < sel.end) {
1637 view_selections_set(s, &sel);
1638 } else if (!view_selections_dispose(s)) {
1639 vis_mode_switch(vis, VIS_MODE_NORMAL);
1642 return keys;
1645 static void selections_set(Vis *vis, View *view, Array *sel) {
1646 enum VisMode mode = vis_mode_get(vis);
1647 bool anchored = mode == VIS_MODE_VISUAL || mode == VIS_MODE_VISUAL_LINE;
1648 view_selections_set_all(view, sel, anchored);
1649 if (!anchored)
1650 view_selections_clear_all(view);
1653 static const char *selections_save(Vis *vis, const char *keys, const Arg *arg) {
1654 Win *win = vis_window(vis);
1655 View *view = vis_view(vis);
1656 enum VisMark mark = vis_mark_used(vis);
1657 Array sel = view_selections_get_all(view);
1658 vis_mark_set(win, mark, &sel);
1659 array_release(&sel);
1660 vis_cancel(vis);
1661 return keys;
1664 static const char *selections_restore(Vis *vis, const char *keys, const Arg *arg) {
1665 Win *win = vis_window(vis);
1666 View *view = vis_view(vis);
1667 enum VisMark mark = vis_mark_used(vis);
1668 Array sel = vis_mark_get(win, mark);
1669 selections_set(vis, view, &sel);
1670 array_release(&sel);
1671 vis_cancel(vis);
1672 return keys;
1675 static const char *selections_union(Vis *vis, const char *keys, const Arg *arg) {
1676 Win *win = vis_window(vis);
1677 View *view = vis_view(vis);
1678 enum VisMark mark = vis_mark_used(vis);
1679 Array a = vis_mark_get(win, mark);
1680 Array b = view_selections_get_all(view);
1681 Array sel;
1682 array_init_from(&sel, &a);
1684 size_t i = 0, j = 0;
1685 Filerange *r1 = array_get(&a, i), *r2 = array_get(&b, j), cur = text_range_empty();
1686 while (r1 || r2) {
1687 if (r1 && text_range_overlap(r1, &cur)) {
1688 cur = text_range_union(r1, &cur);
1689 r1 = array_get(&a, ++i);
1690 } else if (r2 && text_range_overlap(r2, &cur)) {
1691 cur = text_range_union(r2, &cur);
1692 r2 = array_get(&b, ++j);
1693 } else {
1694 if (text_range_valid(&cur))
1695 array_add(&sel, &cur);
1696 if (!r1) {
1697 cur = *r2;
1698 r2 = array_get(&b, ++j);
1699 } else if (!r2) {
1700 cur = *r1;
1701 r1 = array_get(&a, ++i);
1702 } else {
1703 if (r1->start < r2->start) {
1704 cur = *r1;
1705 r1 = array_get(&a, ++i);
1706 } else {
1707 cur = *r2;
1708 r2 = array_get(&b, ++j);
1714 if (text_range_valid(&cur))
1715 array_add(&sel, &cur);
1717 selections_set(vis, view, &sel);
1718 vis_cancel(vis);
1720 array_release(&a);
1721 array_release(&b);
1722 array_release(&sel);
1724 return keys;
1727 static void intersect(Array *ret, Array *a, Array *b) {
1728 size_t i = 0, j = 0;
1729 Filerange *r1 = array_get(a, i), *r2 = array_get(b, j);
1730 while (r1 && r2) {
1731 if (text_range_overlap(r1, r2)) {
1732 Filerange new = text_range_intersect(r1, r2);
1733 array_add(ret, &new);
1735 if (r1->end < r2->end)
1736 r1 = array_get(a, ++i);
1737 else
1738 r2 = array_get(b, ++j);
1742 static const char *selections_intersect(Vis *vis, const char *keys, const Arg *arg) {
1743 Win *win = vis_window(vis);
1744 View *view = vis_view(vis);
1745 enum VisMark mark = vis_mark_used(vis);
1746 Array a = vis_mark_get(win, mark);
1747 Array b = view_selections_get_all(view);
1748 Array sel;
1749 array_init_from(&sel, &a);
1751 intersect(&sel, &a, &b);
1752 selections_set(vis, view, &sel);
1753 vis_cancel(vis);
1755 array_release(&a);
1756 array_release(&b);
1757 array_release(&sel);
1759 return keys;
1762 static void complement(Array *ret, Array *a, Filerange *universe) {
1763 size_t pos = universe->start;
1764 for (size_t i = 0, len = array_length(a); i < len; i++) {
1765 Filerange *r = array_get(a, i);
1766 if (pos < r->start) {
1767 Filerange new = text_range_new(pos, r->start);
1768 array_add(ret, &new);
1770 pos = r->end;
1772 if (pos < universe->end) {
1773 Filerange new = text_range_new(pos, universe->end);
1774 array_add(ret, &new);
1778 static const char *selections_complement(Vis *vis, const char *keys, const Arg *arg) {
1779 Text *txt = vis_text(vis);
1780 View *view = vis_view(vis);
1781 Filerange universe = text_object_entire(txt, 0);
1782 Array a = view_selections_get_all(view);
1783 Array sel;
1784 array_init_from(&sel, &a);
1786 complement(&sel, &a, &universe);
1788 selections_set(vis, view, &sel);
1789 array_release(&a);
1790 array_release(&sel);
1791 return keys;
1794 static const char *selections_minus(Vis *vis, const char *keys, const Arg *arg) {
1795 Text *txt = vis_text(vis);
1796 Win *win = vis_window(vis);
1797 View *view = vis_view(vis);
1798 enum VisMark mark = vis_mark_used(vis);
1799 Array a = view_selections_get_all(view);
1800 Array b = vis_mark_get(win, mark);
1801 Array sel;
1802 array_init_from(&sel, &a);
1803 Array b_complement;
1804 array_init_from(&b_complement, &b);
1806 Filerange universe = text_object_entire(txt, 0);
1807 complement(&b_complement, &b, &universe);
1808 intersect(&sel, &a, &b_complement);
1810 selections_set(vis, view, &sel);
1811 vis_cancel(vis);
1813 array_release(&a);
1814 array_release(&b);
1815 array_release(&b_complement);
1816 array_release(&sel);
1818 return keys;
1821 static Filerange combine_intersect(const Filerange *r1, const Filerange *r2) {
1822 if (!r1 || !r2)
1823 return text_range_empty();
1824 return text_range_intersect(r1, r2);
1827 static Filerange combine_longer(const Filerange *r1, const Filerange *r2) {
1828 if (!r1)
1829 return *r2;
1830 if (!r2)
1831 return *r1;
1832 size_t l1 = text_range_size(r1);
1833 size_t l2 = text_range_size(r2);
1834 return l1 < l2 ? *r2 : *r1;
1837 static Filerange combine_shorter(const Filerange *r1, const Filerange *r2) {
1838 if (!r1)
1839 return *r2;
1840 if (!r2)
1841 return *r1;
1842 size_t l1 = text_range_size(r1);
1843 size_t l2 = text_range_size(r2);
1844 return l1 < l2 ? *r1 : *r2;
1847 static Filerange combine_leftmost(const Filerange *r1, const Filerange *r2) {
1848 if (!r1)
1849 return *r2;
1850 if (!r2)
1851 return *r1;
1852 return r1->start < r2->start || (r1->start == r2->start && r1->end < r2->end) ? *r1 : *r2;
1855 static Filerange combine_rightmost(const Filerange *r1, const Filerange *r2) {
1856 if (!r1)
1857 return *r2;
1858 if (!r2)
1859 return *r1;
1860 return r1->start < r2->start || (r1->start == r2->start && r1->end < r2->end) ? *r2 : *r1;
1863 static const char *selections_combine(Vis *vis, const char *keys, const Arg *arg) {
1864 Win *win = vis_window(vis);
1865 View *view = vis_view(vis);
1866 enum VisMark mark = vis_mark_used(vis);
1867 Array a = view_selections_get_all(view);
1868 Array b = vis_mark_get(win, mark);
1869 Array sel;
1870 array_init_from(&sel, &a);
1872 Filerange *r1 = array_get(&a, 0), *r2 = array_get(&b, 0);
1873 for (size_t i = 0, j = 0; r1 || r2; r1 = array_get(&a, ++i), r2 = array_get(&b, ++j)) {
1874 Filerange new = arg->combine(r1, r2);
1875 if (text_range_valid(&new))
1876 array_add(&sel, &new);
1879 vis_mark_normalize(&sel);
1880 selections_set(vis, view, &sel);
1881 vis_cancel(vis);
1883 array_release(&a);
1884 array_release(&b);
1885 array_release(&sel);
1887 return keys;
1890 static const char *replace(Vis *vis, const char *keys, const Arg *arg) {
1891 if (!keys[0]) {
1892 vis_keymap_disable(vis);
1893 return NULL;
1896 const char *next = vis_keys_next(vis, keys);
1897 if (!next)
1898 return NULL;
1900 char replacement[UTFmax+1];
1901 if (!vis_keys_utf8(vis, keys, replacement))
1902 return next;
1904 if (replacement[0] == 0x1b) /* <Escape> */
1905 return next;
1907 vis_operator(vis, VIS_OP_REPLACE, replacement);
1908 if (vis_mode_get(vis) == VIS_MODE_OPERATOR_PENDING)
1909 vis_motion(vis, VIS_MOVE_CHAR_NEXT);
1910 return next;
1913 static const char *count(Vis *vis, const char *keys, const Arg *arg) {
1914 int digit = keys[-1] - '0';
1915 int count = vis_count_get_default(vis, 0);
1916 if (0 <= digit && digit <= 9) {
1917 if (digit == 0 && count == 0)
1918 vis_motion(vis, VIS_MOVE_LINE_BEGIN);
1919 else
1920 vis_count_set(vis, count * 10 + digit);
1922 return keys;
1925 static const char *gotoline(Vis *vis, const char *keys, const Arg *arg) {
1926 if (vis_count_get(vis) != VIS_COUNT_UNKNOWN)
1927 vis_motion(vis, VIS_MOVE_LINE);
1928 else if (arg->i < 0)
1929 vis_motion(vis, VIS_MOVE_FILE_BEGIN);
1930 else
1931 vis_motion(vis, VIS_MOVE_FILE_END);
1932 return keys;
1935 static const char *operator(Vis *vis, const char *keys, const Arg *arg) {
1936 vis_operator(vis, arg->i);
1937 return keys;
1940 static const char *movement_key(Vis *vis, const char *keys, const Arg *arg) {
1941 if (!keys[0]) {
1942 vis_keymap_disable(vis);
1943 return NULL;
1946 const char *next = vis_keys_next(vis, keys);
1947 if (!next)
1948 return NULL;
1949 char utf8[UTFmax+1];
1950 if (vis_keys_utf8(vis, keys, utf8))
1951 vis_motion(vis, arg->i, utf8);
1952 return next;
1955 static const char *movement(Vis *vis, const char *keys, const Arg *arg) {
1956 vis_motion(vis, arg->i);
1957 return keys;
1960 static const char *textobj(Vis *vis, const char *keys, const Arg *arg) {
1961 vis_textobject(vis, arg->i);
1962 return keys;
1965 static const char *selection_end(Vis *vis, const char *keys, const Arg *arg) {
1966 for (Selection *s = view_selections(vis_view(vis)); s; s = view_selections_next(s))
1967 view_selections_flip(s);
1968 return keys;
1971 static const char *reg(Vis *vis, const char *keys, const Arg *arg) {
1972 if (!keys[0])
1973 return NULL;
1974 const char *next = vis_keys_next(vis, keys);
1975 if (next - keys > 1)
1976 return next;
1977 enum VisRegister reg = vis_register_from(vis, keys[0]);
1978 vis_register(vis, reg);
1979 return keys+1;
1982 static const char *mark(Vis *vis, const char *keys, const Arg *arg) {
1983 if (!keys[0])
1984 return NULL;
1985 const char *next = vis_keys_next(vis, keys);
1986 if (next - keys > 1)
1987 return next;
1988 enum VisMark mark = vis_mark_from(vis, keys[0]);
1989 vis_mark(vis, mark);
1990 return keys+1;
1993 static const char *undo(Vis *vis, const char *keys, const Arg *arg) {
1994 size_t pos = text_undo(vis_text(vis));
1995 if (pos != EPOS) {
1996 View *view = vis_view(vis);
1997 if (view_selections_count(view) == 1)
1998 view_cursor_to(view, pos);
1999 /* redraw all windows in case some display the same file */
2000 vis_draw(vis);
2002 return keys;
2005 static const char *redo(Vis *vis, const char *keys, const Arg *arg) {
2006 size_t pos = text_redo(vis_text(vis));
2007 if (pos != EPOS) {
2008 View *view = vis_view(vis);
2009 if (view_selections_count(view) == 1)
2010 view_cursor_to(view, pos);
2011 /* redraw all windows in case some display the same file */
2012 vis_draw(vis);
2014 return keys;
2017 static const char *earlier(Vis *vis, const char *keys, const Arg *arg) {
2018 size_t pos = EPOS;
2019 VisCountIterator it = vis_count_iterator_get(vis, 1);
2020 while (vis_count_iterator_next(&it))
2021 pos = text_earlier(vis_text(vis));
2022 if (pos != EPOS) {
2023 view_cursor_to(vis_view(vis), pos);
2024 /* redraw all windows in case some display the same file */
2025 vis_draw(vis);
2027 return keys;
2030 static const char *later(Vis *vis, const char *keys, const Arg *arg) {
2031 size_t pos = EPOS;
2032 VisCountIterator it = vis_count_iterator_get(vis, 1);
2033 while (vis_count_iterator_next(&it))
2034 pos = text_later(vis_text(vis));
2035 if (pos != EPOS) {
2036 view_cursor_to(vis_view(vis), pos);
2037 /* redraw all windows in case some display the same file */
2038 vis_draw(vis);
2040 return keys;
2043 static const char *delete(Vis *vis, const char *keys, const Arg *arg) {
2044 vis_operator(vis, VIS_OP_DELETE);
2045 vis_motion(vis, arg->i);
2046 return keys;
2049 static const char *insert_register(Vis *vis, const char *keys, const Arg *arg) {
2050 if (!keys[0])
2051 return NULL;
2052 const char *next = vis_keys_next(vis, keys);
2053 if (next - keys > 1)
2054 return next;
2055 enum VisRegister reg = vis_register_from(vis, keys[0]);
2056 if (reg != VIS_REG_INVALID) {
2057 vis_register(vis, reg);
2058 vis_operator(vis, VIS_OP_PUT_BEFORE_END);
2060 return keys+1;
2063 static const char *prompt_show(Vis *vis, const char *keys, const Arg *arg) {
2064 vis_prompt_show(vis, arg->s);
2065 return keys;
2068 static const char *insert_verbatim(Vis *vis, const char *keys, const Arg *arg) {
2069 Rune rune = 0;
2070 char buf[4], type = keys[0];
2071 const char *data = NULL;
2072 int len = 0, count = 0, base = 0;
2073 switch (type) {
2074 case '\0':
2075 return NULL;
2076 case 'o':
2077 case 'O':
2078 count = 3;
2079 base = 8;
2080 break;
2081 case 'U':
2082 count = 4;
2083 /* fall through */
2084 case 'u':
2085 count += 4;
2086 base = 16;
2087 break;
2088 case 'x':
2089 case 'X':
2090 count = 2;
2091 base = 16;
2092 break;
2093 default:
2094 if ('0' <= type && type <= '9') {
2095 rune = type - '0';
2096 count = 2;
2097 base = 10;
2099 break;
2102 if (base) {
2103 for (keys++; keys[0] && count > 0; keys++, count--) {
2104 int v = 0;
2105 if (base == 8 && '0' <= keys[0] && keys[0] <= '7') {
2106 v = keys[0] - '0';
2107 } else if ((base == 10 || base == 16) && '0' <= keys[0] && keys[0] <= '9') {
2108 v = keys[0] - '0';
2109 } else if (base == 16 && 'a' <= keys[0] && keys[0] <= 'f') {
2110 v = 10 + keys[0] - 'a';
2111 } else if (base == 16 && 'A' <= keys[0] && keys[0] <= 'F') {
2112 v = 10 + keys[0] - 'A';
2113 } else {
2114 count = 0;
2115 break;
2117 rune = rune * base + v;
2120 if (count > 0)
2121 return NULL;
2122 if (type == 'u' || type == 'U') {
2123 len = runetochar(buf, &rune);
2124 } else {
2125 buf[0] = rune;
2126 len = 1;
2129 data = buf;
2130 } else {
2131 const char *next = vis_keys_next(vis, keys);
2132 if (!next)
2133 return NULL;
2134 if ((rune = vis_keys_codepoint(vis, keys)) != (Rune)-1) {
2135 len = runetochar(buf, &rune);
2136 if (buf[0] == '\n')
2137 buf[0] = '\r';
2138 data = buf;
2139 } else {
2140 vis_info_show(vis, "Unknown key");
2142 keys = next;
2145 if (len > 0)
2146 vis_insert_key(vis, data, len);
2147 return keys;
2150 static const char *wscroll(Vis *vis, const char *keys, const Arg *arg) {
2151 View *view = vis_view(vis);
2152 int count = vis_count_get(vis);
2153 switch (arg->i) {
2154 case -PAGE:
2155 view_scroll_page_up(view);
2156 break;
2157 case +PAGE:
2158 view_scroll_page_down(view);
2159 break;
2160 case -PAGE_HALF:
2161 view_scroll_halfpage_up(view);
2162 break;
2163 case +PAGE_HALF:
2164 view_scroll_halfpage_down(view);
2165 break;
2166 default:
2167 if (count == VIS_COUNT_UNKNOWN)
2168 count = arg->i < 0 ? -arg->i : arg->i;
2169 if (arg->i < 0)
2170 view_scroll_up(view, count);
2171 else
2172 view_scroll_down(view, count);
2173 break;
2175 vis_count_set(vis, VIS_COUNT_UNKNOWN);
2176 return keys;
2179 static const char *wslide(Vis *vis, const char *keys, const Arg *arg) {
2180 View *view = vis_view(vis);
2181 int count = vis_count_get(vis);
2182 if (count == VIS_COUNT_UNKNOWN)
2183 count = arg->i < 0 ? -arg->i : arg->i;
2184 if (arg->i >= 0)
2185 view_slide_down(view, count);
2186 else
2187 view_slide_up(view, count);
2188 vis_count_set(vis, VIS_COUNT_UNKNOWN);
2189 return keys;
2192 static const char *call(Vis *vis, const char *keys, const Arg *arg) {
2193 arg->f(vis);
2194 return keys;
2197 static const char *window(Vis *vis, const char *keys, const Arg *arg) {
2198 arg->w(vis_view(vis));
2199 return keys;
2202 static const char *openline(Vis *vis, const char *keys, const Arg *arg) {
2203 vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_INSERT);
2204 if (arg->i > 0) {
2205 vis_motion(vis, VIS_MOVE_LINE_END);
2206 vis_keys_feed(vis, "<Enter>");
2207 } else {
2208 if (vis_get_autoindent(vis)) {
2209 vis_motion(vis, VIS_MOVE_LINE_START);
2210 vis_keys_feed(vis, "<vis-motion-line-start>");
2211 } else {
2212 vis_motion(vis, VIS_MOVE_LINE_BEGIN);
2213 vis_keys_feed(vis, "<vis-motion-line-begin>");
2215 vis_keys_feed(vis, "<Enter><Up>");
2217 return keys;
2220 static const char *join(Vis *vis, const char *keys, const Arg *arg) {
2221 bool normal = (vis_mode_get(vis) == VIS_MODE_NORMAL);
2222 vis_operator(vis, VIS_OP_JOIN, arg->s);
2223 if (normal) {
2224 int count = vis_count_get_default(vis, 0);
2225 if (count)
2226 vis_count_set(vis, count-1);
2227 vis_motion(vis, VIS_MOVE_LINE_NEXT);
2229 return keys;
2232 static const char *normalmode_escape(Vis *vis, const char *keys, const Arg *arg) {
2233 if (vis_count_get(vis) == VIS_COUNT_UNKNOWN)
2234 selections_clear(vis, keys, arg);
2235 else
2236 vis_count_set(vis, VIS_COUNT_UNKNOWN);
2237 return keys;
2240 static const char *visualmode_escape(Vis *vis, const char *keys, const Arg *arg) {
2241 if (vis_count_get(vis) == VIS_COUNT_UNKNOWN)
2242 vis_mode_switch(vis, VIS_MODE_NORMAL);
2243 else
2244 vis_count_set(vis, VIS_COUNT_UNKNOWN);
2245 return keys;
2248 static const char *switchmode(Vis *vis, const char *keys, const Arg *arg) {
2249 vis_mode_switch(vis, arg->i);
2250 return keys;
2253 static const char *insertmode(Vis *vis, const char *keys, const Arg *arg) {
2254 vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_INSERT);
2255 vis_motion(vis, arg->i);
2256 return keys;
2259 static const char *replacemode(Vis *vis, const char *keys, const Arg *arg) {
2260 vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_REPLACE);
2261 vis_motion(vis, arg->i);
2262 return keys;
2265 static const char *unicode_info(Vis *vis, const char *keys, const Arg *arg) {
2266 View *view = vis_view(vis);
2267 Text *txt = vis_text(vis);
2268 size_t start = view_cursor_get(view);
2269 size_t end = text_char_next(txt, start);
2270 char *grapheme = text_bytes_alloc0(txt, start, end-start), *codepoint = grapheme;
2271 if (!grapheme)
2272 return keys;
2273 Buffer info;
2274 buffer_init(&info);
2275 mbstate_t ps = { 0 };
2276 Iterator it = text_iterator_get(txt, start);
2277 for (size_t pos = start; it.pos < end; pos = it.pos) {
2278 if (!text_iterator_codepoint_next(&it, NULL)) {
2279 vis_info_show(vis, "Failed to parse code point");
2280 goto err;
2282 size_t len = it.pos - pos;
2283 wchar_t wc = 0xFFFD;
2284 size_t res = mbrtowc(&wc, codepoint, len, &ps);
2285 bool combining = false;
2286 if (res != (size_t)-1 && res != (size_t)-2)
2287 combining = (wc != L'\0' && wcwidth(wc) == 0);
2288 unsigned char ch = *codepoint;
2289 if (ch < 128 && !isprint(ch))
2290 buffer_appendf(&info, "<^%c> ", ch == 127 ? '?' : ch + 64);
2291 else
2292 buffer_appendf(&info, "<%s%.*s> ", combining ? " " : "", (int)len, codepoint);
2293 if (arg->i == VIS_ACTION_UNICODE_INFO) {
2294 buffer_appendf(&info, "U+%04"PRIX32" ", (uint32_t)wc);
2295 } else {
2296 for (size_t i = 0; i < len; i++)
2297 buffer_appendf(&info, "%02x ", (uint8_t)codepoint[i]);
2299 codepoint += len;
2301 vis_info_show(vis, "%s", buffer_content0(&info));
2302 err:
2303 free(grapheme);
2304 buffer_release(&info);
2305 return keys;
2308 static const char *percent(Vis *vis, const char *keys, const Arg *arg) {
2309 if (vis_count_get(vis) == VIS_COUNT_UNKNOWN)
2310 vis_motion(vis, VIS_MOVE_BRACKET_MATCH);
2311 else
2312 vis_motion(vis, VIS_MOVE_PERCENT);
2313 return keys;
2316 static const char *jumplist(Vis *vis, const char *keys, const Arg *arg) {
2317 if (arg->i < 0)
2318 vis_jumplist_prev(vis);
2319 else if (arg->i > 0)
2320 vis_jumplist_next(vis);
2321 else
2322 vis_jumplist_save(vis);
2323 return keys;
2326 static Vis *vis;
2328 static void signal_handler(int signum, siginfo_t *siginfo, void *context) {
2329 vis_signal_handler(vis, signum, siginfo, context);
2332 int main(int argc, char *argv[]) {
2334 VisEvent event = {
2335 .init = vis_lua_init,
2336 .start = vis_lua_start,
2337 .quit = vis_lua_quit,
2338 .mode_insert_input = vis_lua_mode_insert_input,
2339 .mode_replace_input = vis_lua_mode_replace_input,
2340 .file_open = vis_lua_file_open,
2341 .file_save_pre = vis_lua_file_save_pre,
2342 .file_save_post = vis_lua_file_save_post,
2343 .file_close = vis_lua_file_close,
2344 .win_open = vis_lua_win_open,
2345 .win_close = vis_lua_win_close,
2346 .win_highlight = vis_lua_win_highlight,
2347 .win_status = vis_lua_win_status,
2350 vis = vis_new(ui_term_new(), &event);
2351 if (!vis)
2352 return EXIT_FAILURE;
2354 for (int i = 0; i < LENGTH(vis_action); i++) {
2355 const KeyAction *action = &vis_action[i];
2356 if (!vis_action_register(vis, action))
2357 vis_die(vis, "Could not register action: %s\n", action->name);
2360 for (int i = 0; i < LENGTH(default_bindings); i++) {
2361 for (const KeyBinding **binding = default_bindings[i]; binding && *binding; binding++) {
2362 for (const KeyBinding *kb = *binding; kb->key; kb++) {
2363 vis_mode_map(vis, i, false, kb->key, kb);
2368 for (const char **k = keymaps; k[0]; k += 2)
2369 vis_keymap_add(vis, k[0], k[1]);
2371 /* install signal handlers etc. */
2372 struct sigaction sa;
2373 memset(&sa, 0, sizeof sa);
2374 sigfillset(&sa.sa_mask);
2375 sa.sa_flags = SA_SIGINFO;
2376 sa.sa_sigaction = signal_handler;
2377 if (sigaction(SIGBUS, &sa, NULL) == -1 ||
2378 sigaction(SIGINT, &sa, NULL) == -1 ||
2379 sigaction(SIGCONT, &sa, NULL) == -1 ||
2380 sigaction(SIGWINCH, &sa, NULL) == -1 ||
2381 sigaction(SIGTERM, &sa, NULL) == -1 ||
2382 sigaction(SIGHUP, &sa, NULL) == -1) {
2383 vis_die(vis, "Failed to set signal handler: %s\n", strerror(errno));
2386 sa.sa_handler = SIG_IGN;
2387 if (sigaction(SIGPIPE, &sa, NULL) == -1 || sigaction(SIGQUIT, &sa, NULL) == -1)
2388 vis_die(vis, "Failed to ignore signals\n");
2390 sigset_t blockset;
2391 sigemptyset(&blockset);
2392 sigaddset(&blockset, SIGBUS);
2393 sigaddset(&blockset, SIGCONT);
2394 sigaddset(&blockset, SIGWINCH);
2395 sigaddset(&blockset, SIGTERM);
2396 sigaddset(&blockset, SIGHUP);
2397 if (sigprocmask(SIG_BLOCK, &blockset, NULL) == -1)
2398 vis_die(vis, "Failed to block signals\n");
2400 for (int i = 1; i < argc; i++) {
2401 if (argv[i][0] != '-') {
2402 continue;
2403 } else if (strcmp(argv[i], "-") == 0) {
2404 continue;
2405 } else if (strcmp(argv[i], "--") == 0) {
2406 break;
2407 } else if (strcmp(argv[i], "-v") == 0) {
2408 printf("vis %s%s%s%s%s%s%s\n", VERSION,
2409 CONFIG_CURSES ? " +curses" : "",
2410 CONFIG_LUA ? " +lua" : "",
2411 CONFIG_LPEG ? " +lpeg" : "",
2412 CONFIG_TRE ? " +tre" : "",
2413 CONFIG_ACL ? " +acl" : "",
2414 CONFIG_SELINUX ? " +selinux" : "");
2415 return 0;
2416 } else {
2417 fprintf(stderr, "Unknown command option: %s\n", argv[i]);
2418 return 1;
2422 char *cmd = NULL;
2423 bool end_of_options = false, win_created = false;
2425 for (int i = 1; i < argc; i++) {
2426 if (argv[i][0] == '-' && !end_of_options) {
2427 if (strcmp(argv[i], "-") == 0) {
2428 if (!vis_window_new_fd(vis, STDOUT_FILENO))
2429 vis_die(vis, "Can not create empty buffer\n");
2430 ssize_t len = 0;
2431 char buf[PIPE_BUF];
2432 Text *txt = vis_text(vis);
2433 while ((len = read(STDIN_FILENO, buf, sizeof buf)) > 0)
2434 text_insert(txt, text_size(txt), buf, len);
2435 if (len == -1)
2436 vis_die(vis, "Can not read from stdin\n");
2437 text_snapshot(txt);
2438 int fd = open("/dev/tty", O_RDWR);
2439 if (fd == -1)
2440 vis_die(vis, "Can not reopen stdin\n");
2441 dup2(fd, STDIN_FILENO);
2442 close(fd);
2443 } else if (strcmp(argv[i], "--") == 0) {
2444 end_of_options = true;
2445 continue;
2447 } else if (argv[i][0] == '+' && !end_of_options) {
2448 cmd = argv[i] + (argv[i][1] == '/' || argv[i][1] == '?');
2449 continue;
2450 } else if (!vis_window_new(vis, argv[i])) {
2451 vis_die(vis, "Can not load `%s': %s\n", argv[i], strerror(errno));
2453 win_created = true;
2454 if (cmd) {
2455 vis_prompt_cmd(vis, cmd);
2456 cmd = NULL;
2460 if (!vis_window(vis) && !win_created) {
2461 if (!vis_window_new(vis, NULL))
2462 vis_die(vis, "Can not create empty buffer\n");
2463 if (cmd)
2464 vis_prompt_cmd(vis, cmd);
2467 int status = vis_run(vis);
2468 vis_free(vis);
2469 return status;