filetype: Set "groovy" for Jenkinsfile
[vis.git] / main.c
blob858af23dfb6206f18bbfa2aabf5cd1495add4dac
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 next region matching the current selection */
70 static const char *selections_match_next(Vis*, const char *keys, const Arg *arg);
71 /* clear current selection but select next match */
72 static const char *selections_match_skip(Vis*, const char *keys, const Arg *arg);
73 /* rotate selection content count times left (arg->i < 0) or right (arg->i > 0) */
74 static const char *selections_rotate(Vis*, const char *keys, const Arg *arg);
75 /* remove leading and trailing white spaces from selections */
76 static const char *selections_trim(Vis*, const char *keys, const Arg *arg);
77 /* save active selections to mark */
78 static const char *selections_save(Vis*, const char *keys, const Arg *arg);
79 /* restore selections from mark */
80 static const char *selections_restore(Vis*, const char *keys, const Arg *arg);
81 /* union selections from mark */
82 static const char *selections_union(Vis*, const char *keys, const Arg *arg);
83 /* intersect selections from mark */
84 static const char *selections_intersect(Vis*, const char *keys, const Arg *arg);
85 /* perform complement of current active selections */
86 static const char *selections_complement(Vis*, const char *keys, const Arg *arg);
87 /* subtract selections from mark */
88 static const char *selections_minus(Vis*, const char *keys, const Arg *arg);
89 /* adjust current used count according to keys */
90 static const char *count(Vis*, const char *keys, const Arg *arg);
91 /* move to the count-th line or if not given either to the first (arg->i < 0)
92 * or last (arg->i > 0) line of file */
93 static const char *gotoline(Vis*, const char *keys, const Arg *arg);
94 /* make the current action use the operator indicated by arg->i */
95 static const char *operator(Vis*, const char *keys, const Arg *arg);
96 /* blocks to read a key and performs movement indicated by arg->i which
97 * should be one of VIS_MOVE_{TO,TILL}_{,LINE}_{RIGHT,LEFT}*/
98 static const char *movement_key(Vis*, const char *keys, const Arg *arg);
99 /* perform the movement as indicated by arg->i */
100 static const char *movement(Vis*, const char *keys, const Arg *arg);
101 /* let the current operator affect the range indicated by the text object arg->i */
102 static const char *textobj(Vis*, const char *keys, const Arg *arg);
103 /* move to the other end of selected text */
104 static const char *selection_end(Vis*, const char *keys, const Arg *arg);
105 /* use register indicated by keys for the current operator */
106 static const char *reg(Vis*, const char *keys, const Arg *arg);
107 /* use mark indicated by keys for the current action */
108 static const char *mark(Vis*, const char *keys, const Arg *arg);
109 /* {un,re}do last action, redraw window */
110 static const char *undo(Vis*, const char *keys, const Arg *arg);
111 static const char *redo(Vis*, const char *keys, const Arg *arg);
112 /* earlier, later action chronologically, redraw window */
113 static const char *earlier(Vis*, const char *keys, const Arg *arg);
114 static const char *later(Vis*, const char *keys, const Arg *arg);
115 /* delete from the current cursor position to the end of
116 * movement as indicated by arg->i */
117 static const char *delete(Vis*, const char *keys, const Arg *arg);
118 /* insert register content indicated by keys at current cursor position */
119 static const char *insert_register(Vis*, const char *keys, const Arg *arg);
120 /* show a user prompt to get input with title arg->s */
121 static const char *prompt_show(Vis*, const char *keys, const Arg *arg);
122 /* blocks to read 3 consecutive digits and inserts the corresponding byte value */
123 static const char *insert_verbatim(Vis*, const char *keys, const Arg *arg);
124 /* scroll window content according to arg->i which can be either PAGE, PAGE_HALF,
125 * or an arbitrary number of lines. a multiplier overrides what is given in arg->i.
126 * negative values scroll back, positive forward. */
127 static const char *wscroll(Vis*, const char *keys, const Arg *arg);
128 /* similar to scroll, but do only move window content not cursor position */
129 static const char *wslide(Vis*, const char *keys, const Arg *arg);
130 /* call editor function as indicated by arg->f */
131 static const char *call(Vis*, const char *keys, const Arg *arg);
132 /* call window function as indicated by arg->w */
133 static const char *window(Vis*, const char *keys, const Arg *arg);
134 /* show info about Unicode character at cursor position */
135 static const char *unicode_info(Vis*, const char *keys, const Arg *arg);
136 /* either go to count % of file or to matching item */
137 static const char *percent(Vis*, const char *keys, const Arg *arg);
138 /* navigate jumplist next (arg->i > 0), prev (arg->i < 0), save (arg->i = 0) */
139 static const char *jumplist(Vis*, const char *keys, const Arg *arg);
141 enum {
142 VIS_ACTION_EDITOR_SUSPEND,
143 VIS_ACTION_CURSOR_CHAR_PREV,
144 VIS_ACTION_CURSOR_CHAR_NEXT,
145 VIS_ACTION_CURSOR_LINE_CHAR_PREV,
146 VIS_ACTION_CURSOR_LINE_CHAR_NEXT,
147 VIS_ACTION_CURSOR_CODEPOINT_PREV,
148 VIS_ACTION_CURSOR_CODEPOINT_NEXT,
149 VIS_ACTION_CURSOR_WORD_START_PREV,
150 VIS_ACTION_CURSOR_WORD_START_NEXT,
151 VIS_ACTION_CURSOR_WORD_END_PREV,
152 VIS_ACTION_CURSOR_WORD_END_NEXT,
153 VIS_ACTION_CURSOR_LONGWORD_START_PREV,
154 VIS_ACTION_CURSOR_LONGWORD_START_NEXT,
155 VIS_ACTION_CURSOR_LONGWORD_END_PREV,
156 VIS_ACTION_CURSOR_LONGWORD_END_NEXT,
157 VIS_ACTION_CURSOR_LINE_UP,
158 VIS_ACTION_CURSOR_LINE_DOWN,
159 VIS_ACTION_CURSOR_LINE_START,
160 VIS_ACTION_CURSOR_LINE_FINISH,
161 VIS_ACTION_CURSOR_LINE_BEGIN,
162 VIS_ACTION_CURSOR_LINE_END,
163 VIS_ACTION_CURSOR_SCREEN_LINE_UP,
164 VIS_ACTION_CURSOR_SCREEN_LINE_DOWN,
165 VIS_ACTION_CURSOR_SCREEN_LINE_BEGIN,
166 VIS_ACTION_CURSOR_SCREEN_LINE_MIDDLE,
167 VIS_ACTION_CURSOR_SCREEN_LINE_END,
168 VIS_ACTION_CURSOR_PERCENT,
169 VIS_ACTION_CURSOR_BYTE,
170 VIS_ACTION_CURSOR_BYTE_LEFT,
171 VIS_ACTION_CURSOR_BYTE_RIGHT,
172 VIS_ACTION_CURSOR_PARAGRAPH_PREV,
173 VIS_ACTION_CURSOR_PARAGRAPH_NEXT,
174 VIS_ACTION_CURSOR_SENTENCE_PREV,
175 VIS_ACTION_CURSOR_SENTENCE_NEXT,
176 VIS_ACTION_CURSOR_BLOCK_START,
177 VIS_ACTION_CURSOR_BLOCK_END,
178 VIS_ACTION_CURSOR_PARENTHESIS_START,
179 VIS_ACTION_CURSOR_PARENTHESIS_END,
180 VIS_ACTION_CURSOR_COLUMN,
181 VIS_ACTION_CURSOR_LINE_FIRST,
182 VIS_ACTION_CURSOR_LINE_LAST,
183 VIS_ACTION_CURSOR_WINDOW_LINE_TOP,
184 VIS_ACTION_CURSOR_WINDOW_LINE_MIDDLE,
185 VIS_ACTION_CURSOR_WINDOW_LINE_BOTTOM,
186 VIS_ACTION_CURSOR_SEARCH_REPEAT_FORWARD,
187 VIS_ACTION_CURSOR_SEARCH_REPEAT_BACKWARD,
188 VIS_ACTION_CURSOR_SEARCH_REPEAT,
189 VIS_ACTION_CURSOR_SEARCH_REPEAT_REVERSE,
190 VIS_ACTION_CURSOR_SEARCH_WORD_FORWARD,
191 VIS_ACTION_CURSOR_SEARCH_WORD_BACKWARD,
192 VIS_ACTION_WINDOW_PAGE_UP,
193 VIS_ACTION_WINDOW_PAGE_DOWN,
194 VIS_ACTION_WINDOW_HALFPAGE_UP,
195 VIS_ACTION_WINDOW_HALFPAGE_DOWN,
196 VIS_ACTION_MODE_NORMAL,
197 VIS_ACTION_MODE_NORMAL_ESCAPE,
198 VIS_ACTION_MODE_VISUAL,
199 VIS_ACTION_MODE_VISUAL_ESCAPE,
200 VIS_ACTION_MODE_VISUAL_LINE,
201 VIS_ACTION_MODE_INSERT,
202 VIS_ACTION_MODE_REPLACE,
203 VIS_ACTION_DELETE_CHAR_PREV,
204 VIS_ACTION_DELETE_CHAR_NEXT,
205 VIS_ACTION_DELETE_LINE_BEGIN,
206 VIS_ACTION_DELETE_WORD_PREV,
207 VIS_ACTION_JUMPLIST_PREV,
208 VIS_ACTION_JUMPLIST_NEXT,
209 VIS_ACTION_JUMPLIST_SAVE,
210 VIS_ACTION_UNDO,
211 VIS_ACTION_REDO,
212 VIS_ACTION_EARLIER,
213 VIS_ACTION_LATER,
214 VIS_ACTION_MACRO_RECORD,
215 VIS_ACTION_MACRO_REPLAY,
216 VIS_ACTION_MARK,
217 VIS_ACTION_REDRAW,
218 VIS_ACTION_REPLACE_CHAR,
219 VIS_ACTION_TOTILL_REPEAT,
220 VIS_ACTION_TOTILL_REVERSE,
221 VIS_ACTION_PROMPT_SEARCH_FORWARD,
222 VIS_ACTION_PROMPT_SEARCH_BACKWARD,
223 VIS_ACTION_TILL_LEFT,
224 VIS_ACTION_TILL_RIGHT,
225 VIS_ACTION_TILL_LINE_LEFT,
226 VIS_ACTION_TILL_LINE_RIGHT,
227 VIS_ACTION_TO_LEFT,
228 VIS_ACTION_TO_RIGHT,
229 VIS_ACTION_TO_LINE_LEFT,
230 VIS_ACTION_TO_LINE_RIGHT,
231 VIS_ACTION_REGISTER,
232 VIS_ACTION_OPERATOR_CHANGE,
233 VIS_ACTION_OPERATOR_DELETE,
234 VIS_ACTION_OPERATOR_YANK,
235 VIS_ACTION_OPERATOR_SHIFT_LEFT,
236 VIS_ACTION_OPERATOR_SHIFT_RIGHT,
237 VIS_ACTION_COUNT,
238 VIS_ACTION_INSERT_NEWLINE,
239 VIS_ACTION_INSERT_TAB,
240 VIS_ACTION_INSERT_VERBATIM,
241 VIS_ACTION_INSERT_REGISTER,
242 VIS_ACTION_WINDOW_NEXT,
243 VIS_ACTION_WINDOW_PREV,
244 VIS_ACTION_APPEND_CHAR_NEXT,
245 VIS_ACTION_APPEND_LINE_END,
246 VIS_ACTION_INSERT_LINE_START,
247 VIS_ACTION_OPEN_LINE_ABOVE,
248 VIS_ACTION_OPEN_LINE_BELOW,
249 VIS_ACTION_JOIN_LINES,
250 VIS_ACTION_JOIN_LINES_TRIM,
251 VIS_ACTION_PROMPT_SHOW,
252 VIS_ACTION_REPEAT,
253 VIS_ACTION_SELECTION_FLIP,
254 VIS_ACTION_WINDOW_REDRAW_TOP,
255 VIS_ACTION_WINDOW_REDRAW_CENTER,
256 VIS_ACTION_WINDOW_REDRAW_BOTTOM,
257 VIS_ACTION_WINDOW_SLIDE_UP,
258 VIS_ACTION_WINDOW_SLIDE_DOWN,
259 VIS_ACTION_PUT_AFTER,
260 VIS_ACTION_PUT_BEFORE,
261 VIS_ACTION_SELECTIONS_NEW_LINE_ABOVE,
262 VIS_ACTION_SELECTIONS_NEW_LINE_ABOVE_FIRST,
263 VIS_ACTION_SELECTIONS_NEW_LINE_BELOW,
264 VIS_ACTION_SELECTIONS_NEW_LINE_BELOW_LAST,
265 VIS_ACTION_SELECTIONS_NEW_LINES_BEGIN,
266 VIS_ACTION_SELECTIONS_NEW_LINES_END,
267 VIS_ACTION_SELECTIONS_NEW_MATCH_ALL,
268 VIS_ACTION_SELECTIONS_NEW_MATCH_NEXT,
269 VIS_ACTION_SELECTIONS_NEW_MATCH_SKIP,
270 VIS_ACTION_SELECTIONS_ALIGN,
271 VIS_ACTION_SELECTIONS_ALIGN_INDENT_LEFT,
272 VIS_ACTION_SELECTIONS_ALIGN_INDENT_RIGHT,
273 VIS_ACTION_SELECTIONS_REMOVE_ALL,
274 VIS_ACTION_SELECTIONS_REMOVE_LAST,
275 VIS_ACTION_SELECTIONS_REMOVE_COLUMN,
276 VIS_ACTION_SELECTIONS_REMOVE_COLUMN_EXCEPT,
277 VIS_ACTION_SELECTIONS_PREV,
278 VIS_ACTION_SELECTIONS_NEXT,
279 VIS_ACTION_SELECTIONS_ROTATE_LEFT,
280 VIS_ACTION_SELECTIONS_ROTATE_RIGHT,
281 VIS_ACTION_SELECTIONS_TRIM,
282 VIS_ACTION_SELECTIONS_SAVE,
283 VIS_ACTION_SELECTIONS_RESTORE,
284 VIS_ACTION_SELECTIONS_UNION,
285 VIS_ACTION_SELECTIONS_INTERSECT,
286 VIS_ACTION_SELECTIONS_COMPLEMENT,
287 VIS_ACTION_SELECTIONS_MINUS,
288 VIS_ACTION_TEXT_OBJECT_WORD_OUTER,
289 VIS_ACTION_TEXT_OBJECT_WORD_INNER,
290 VIS_ACTION_TEXT_OBJECT_LONGWORD_OUTER,
291 VIS_ACTION_TEXT_OBJECT_LONGWORD_INNER,
292 VIS_ACTION_TEXT_OBJECT_SENTENCE,
293 VIS_ACTION_TEXT_OBJECT_PARAGRAPH,
294 VIS_ACTION_TEXT_OBJECT_PARAGRAPH_OUTER,
295 VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_OUTER,
296 VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_INNER,
297 VIS_ACTION_TEXT_OBJECT_PARENTHESIS_OUTER,
298 VIS_ACTION_TEXT_OBJECT_PARENTHESIS_INNER,
299 VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_OUTER,
300 VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_INNER,
301 VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_OUTER,
302 VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_INNER,
303 VIS_ACTION_TEXT_OBJECT_QUOTE_OUTER,
304 VIS_ACTION_TEXT_OBJECT_QUOTE_INNER,
305 VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_OUTER,
306 VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_INNER,
307 VIS_ACTION_TEXT_OBJECT_BACKTICK_OUTER,
308 VIS_ACTION_TEXT_OBJECT_BACKTICK_INNER,
309 VIS_ACTION_TEXT_OBJECT_LINE_OUTER,
310 VIS_ACTION_TEXT_OBJECT_LINE_INNER,
311 VIS_ACTION_TEXT_OBJECT_INDENTATION,
312 VIS_ACTION_TEXT_OBJECT_SEARCH_FORWARD,
313 VIS_ACTION_TEXT_OBJECT_SEARCH_BACKWARD,
314 VIS_ACTION_UNICODE_INFO,
315 VIS_ACTION_UTF8_INFO,
316 VIS_ACTION_NOP,
319 static const KeyAction vis_action[] = {
320 [VIS_ACTION_EDITOR_SUSPEND] = {
321 "vis-suspend",
322 VIS_HELP("Suspend the editor")
323 suspend,
325 [VIS_ACTION_CURSOR_CHAR_PREV] = {
326 "vis-motion-char-prev",
327 VIS_HELP("Move cursor left, to the previous character")
328 movement, { .i = VIS_MOVE_CHAR_PREV }
330 [VIS_ACTION_CURSOR_CHAR_NEXT] = {
331 "vis-motion-char-next",
332 VIS_HELP("Move cursor right, to the next character")
333 movement, { .i = VIS_MOVE_CHAR_NEXT }
335 [VIS_ACTION_CURSOR_LINE_CHAR_PREV] = {
336 "vis-motion-line-char-prev",
337 VIS_HELP("Move cursor left, to the previous character on the same line")
338 movement, { .i = VIS_MOVE_LINE_CHAR_PREV }
340 [VIS_ACTION_CURSOR_LINE_CHAR_NEXT] = {
341 "vis-motion-line-char-next",
342 VIS_HELP("Move cursor right, to the next character on the same line")
343 movement, { .i = VIS_MOVE_LINE_CHAR_NEXT }
345 [VIS_ACTION_CURSOR_CODEPOINT_PREV] = {
346 "vis-motion-codepoint-prev",
347 VIS_HELP("Move to the previous Unicode codepoint")
348 movement, { .i = VIS_MOVE_CODEPOINT_PREV }
350 [VIS_ACTION_CURSOR_CODEPOINT_NEXT] = {
351 "vis-motion-codepoint-next",
352 VIS_HELP("Move to the next Unicode codepoint")
353 movement, { .i = VIS_MOVE_CODEPOINT_NEXT }
355 [VIS_ACTION_CURSOR_WORD_START_PREV] = {
356 "vis-motion-word-start-prev",
357 VIS_HELP("Move cursor words backwards")
358 movement, { .i = VIS_MOVE_WORD_START_PREV }
360 [VIS_ACTION_CURSOR_WORD_START_NEXT] = {
361 "vis-motion-word-start-next",
362 VIS_HELP("Move cursor words forwards")
363 movement, { .i = VIS_MOVE_WORD_START_NEXT }
365 [VIS_ACTION_CURSOR_WORD_END_PREV] = {
366 "vis-motion-word-end-prev",
367 VIS_HELP("Move cursor backwards to the end of word")
368 movement, { .i = VIS_MOVE_WORD_END_PREV }
370 [VIS_ACTION_CURSOR_WORD_END_NEXT] = {
371 "vis-motion-word-end-next",
372 VIS_HELP("Move cursor forward to the end of word")
373 movement, { .i = VIS_MOVE_WORD_END_NEXT }
375 [VIS_ACTION_CURSOR_LONGWORD_START_PREV] = {
376 "vis-motion-bigword-start-prev",
377 VIS_HELP("Move cursor WORDS backwards")
378 movement, { .i = VIS_MOVE_LONGWORD_START_PREV }
380 [VIS_ACTION_CURSOR_LONGWORD_START_NEXT] = {
381 "vis-motion-bigword-start-next",
382 VIS_HELP("Move cursor WORDS forwards")
383 movement, { .i = VIS_MOVE_LONGWORD_START_NEXT }
385 [VIS_ACTION_CURSOR_LONGWORD_END_PREV] = {
386 "vis-motion-bigword-end-prev",
387 VIS_HELP("Move cursor backwards to the end of WORD")
388 movement, { .i = VIS_MOVE_LONGWORD_END_PREV }
390 [VIS_ACTION_CURSOR_LONGWORD_END_NEXT] = {
391 "vis-motion-bigword-end-next",
392 VIS_HELP("Move cursor forward to the end of WORD")
393 movement, { .i = VIS_MOVE_LONGWORD_END_NEXT }
395 [VIS_ACTION_CURSOR_LINE_UP] = {
396 "vis-motion-line-up",
397 VIS_HELP("Move cursor line upwards")
398 movement, { .i = VIS_MOVE_LINE_UP }
400 [VIS_ACTION_CURSOR_LINE_DOWN] = {
401 "vis-motion-line-down",
402 VIS_HELP("Move cursor line downwards")
403 movement, { .i = VIS_MOVE_LINE_DOWN }
405 [VIS_ACTION_CURSOR_LINE_START] = {
406 "vis-motion-line-start",
407 VIS_HELP("Move cursor to first non-blank character of the line")
408 movement, { .i = VIS_MOVE_LINE_START }
410 [VIS_ACTION_CURSOR_LINE_FINISH] = {
411 "vis-motion-line-finish",
412 VIS_HELP("Move cursor to last non-blank character of the line")
413 movement, { .i = VIS_MOVE_LINE_FINISH }
415 [VIS_ACTION_CURSOR_LINE_BEGIN] = {
416 "vis-motion-line-begin",
417 VIS_HELP("Move cursor to first character of the line")
418 movement, { .i = VIS_MOVE_LINE_BEGIN }
420 [VIS_ACTION_CURSOR_LINE_END] = {
421 "vis-motion-line-end",
422 VIS_HELP("Move cursor to end of the line")
423 movement, { .i = VIS_MOVE_LINE_END }
425 [VIS_ACTION_CURSOR_SCREEN_LINE_UP] = {
426 "vis-motion-screenline-up",
427 VIS_HELP("Move cursor screen/display line upwards")
428 movement, { .i = VIS_MOVE_SCREEN_LINE_UP }
430 [VIS_ACTION_CURSOR_SCREEN_LINE_DOWN] = {
431 "vis-motion-screenline-down",
432 VIS_HELP("Move cursor screen/display line downwards")
433 movement, { .i = VIS_MOVE_SCREEN_LINE_DOWN }
435 [VIS_ACTION_CURSOR_SCREEN_LINE_BEGIN] = {
436 "vis-motion-screenline-begin",
437 VIS_HELP("Move cursor to beginning of screen/display line")
438 movement, { .i = VIS_MOVE_SCREEN_LINE_BEGIN }
440 [VIS_ACTION_CURSOR_SCREEN_LINE_MIDDLE] = {
441 "vis-motion-screenline-middle",
442 VIS_HELP("Move cursor to middle of screen/display line")
443 movement, { .i = VIS_MOVE_SCREEN_LINE_MIDDLE }
445 [VIS_ACTION_CURSOR_SCREEN_LINE_END] = {
446 "vis-motion-screenline-end",
447 VIS_HELP("Move cursor to end of screen/display line")
448 movement, { .i = VIS_MOVE_SCREEN_LINE_END }
450 [VIS_ACTION_CURSOR_PERCENT] = {
451 "vis-motion-percent",
452 VIS_HELP("Move to count % of file or matching item")
453 percent
455 [VIS_ACTION_CURSOR_BYTE] = {
456 "vis-motion-byte",
457 VIS_HELP("Move to absolute byte position")
458 movement, { .i = VIS_MOVE_BYTE }
460 [VIS_ACTION_CURSOR_BYTE_LEFT] = {
461 "vis-motion-byte-left",
462 VIS_HELP("Move count bytes to the left")
463 movement, { .i = VIS_MOVE_BYTE_LEFT }
465 [VIS_ACTION_CURSOR_BYTE_RIGHT] = {
466 "vis-motion-byte-right",
467 VIS_HELP("Move count bytes to the right")
468 movement, { .i = VIS_MOVE_BYTE_RIGHT }
470 [VIS_ACTION_CURSOR_PARAGRAPH_PREV] = {
471 "vis-motion-paragraph-prev",
472 VIS_HELP("Move cursor paragraph backward")
473 movement, { .i = VIS_MOVE_PARAGRAPH_PREV }
475 [VIS_ACTION_CURSOR_PARAGRAPH_NEXT] = {
476 "vis-motion-paragraph-next",
477 VIS_HELP("Move cursor paragraph forward")
478 movement, { .i = VIS_MOVE_PARAGRAPH_NEXT }
480 [VIS_ACTION_CURSOR_SENTENCE_PREV] = {
481 "vis-motion-sentence-prev",
482 VIS_HELP("Move cursor sentence backward")
483 movement, { .i = VIS_MOVE_SENTENCE_PREV }
485 [VIS_ACTION_CURSOR_SENTENCE_NEXT] = {
486 "vis-motion-sentence-next",
487 VIS_HELP("Move cursor sentence forward")
488 movement, { .i = VIS_MOVE_SENTENCE_NEXT }
490 [VIS_ACTION_CURSOR_BLOCK_START] = {
491 "vis-motion-block-start",
492 VIS_HELP("Move cursor to the opening curly brace in a block")
493 movement, { .i = VIS_MOVE_BLOCK_START }
495 [VIS_ACTION_CURSOR_BLOCK_END] = {
496 "vis-motion-block-end",
497 VIS_HELP("Move cursor to the closing curly brace in a block")
498 movement, { .i = VIS_MOVE_BLOCK_END }
500 [VIS_ACTION_CURSOR_PARENTHESIS_START] = {
501 "vis-motion-parenthesis-start",
502 VIS_HELP("Move cursor to the opening parenthesis inside a pair of parentheses")
503 movement, { .i = VIS_MOVE_PARENTHESIS_START }
505 [VIS_ACTION_CURSOR_PARENTHESIS_END] = {
506 "vis-motion-parenthesis-end",
507 VIS_HELP("Move cursor to the closing parenthesis inside a pair of parentheses")
508 movement, { .i = VIS_MOVE_PARENTHESIS_END }
510 [VIS_ACTION_CURSOR_COLUMN] = {
511 "vis-motion-column",
512 VIS_HELP("Move cursor to given column of current line")
513 movement, { .i = VIS_MOVE_COLUMN }
515 [VIS_ACTION_CURSOR_LINE_FIRST] = {
516 "vis-motion-line-first",
517 VIS_HELP("Move cursor to given line (defaults to first)")
518 gotoline, { .i = -1 }
520 [VIS_ACTION_CURSOR_LINE_LAST] = {
521 "vis-motion-line-last",
522 VIS_HELP("Move cursor to given line (defaults to last)")
523 gotoline, { .i = +1 }
525 [VIS_ACTION_CURSOR_WINDOW_LINE_TOP] = {
526 "vis-motion-window-line-top",
527 VIS_HELP("Move cursor to top line of the window")
528 movement, { .i = VIS_MOVE_WINDOW_LINE_TOP }
530 [VIS_ACTION_CURSOR_WINDOW_LINE_MIDDLE] = {
531 "vis-motion-window-line-middle",
532 VIS_HELP("Move cursor to middle line of the window")
533 movement, { .i = VIS_MOVE_WINDOW_LINE_MIDDLE }
535 [VIS_ACTION_CURSOR_WINDOW_LINE_BOTTOM] = {
536 "vis-motion-window-line-bottom",
537 VIS_HELP("Move cursor to bottom line of the window")
538 movement, { .i = VIS_MOVE_WINDOW_LINE_BOTTOM }
540 [VIS_ACTION_CURSOR_SEARCH_REPEAT_FORWARD] = {
541 "vis-motion-search-repeat-forward",
542 VIS_HELP("Move cursor to next match in forward direction")
543 movement, { .i = VIS_MOVE_SEARCH_REPEAT_FORWARD }
545 [VIS_ACTION_CURSOR_SEARCH_REPEAT_BACKWARD] = {
546 "vis-motion-search-repeat-backward",
547 VIS_HELP("Move cursor to previous match in backward direction")
548 movement, { .i = VIS_MOVE_SEARCH_REPEAT_BACKWARD }
550 [VIS_ACTION_CURSOR_SEARCH_REPEAT] = {
551 "vis-motion-search-repeat",
552 VIS_HELP("Move cursor to next match")
553 movement, { .i = VIS_MOVE_SEARCH_REPEAT }
555 [VIS_ACTION_CURSOR_SEARCH_REPEAT_REVERSE] = {
556 "vis-motion-search-repeat-reverse",
557 VIS_HELP("Move cursor to next match in opposite direction")
558 movement, { .i = VIS_MOVE_SEARCH_REPEAT_REVERSE }
560 [VIS_ACTION_CURSOR_SEARCH_WORD_FORWARD] = {
561 "vis-motion-search-word-forward",
562 VIS_HELP("Move cursor to next occurrence of the word under cursor")
563 movement, { .i = VIS_MOVE_SEARCH_WORD_FORWARD }
565 [VIS_ACTION_CURSOR_SEARCH_WORD_BACKWARD] = {
566 "vis-motion-search-word-backward",
567 VIS_HELP("Move cursor to previous occurrence of the word under cursor")
568 movement, { .i = VIS_MOVE_SEARCH_WORD_BACKWARD }
570 [VIS_ACTION_WINDOW_PAGE_UP] = {
571 "vis-window-page-up",
572 VIS_HELP("Scroll window pages backwards (upwards)")
573 wscroll, { .i = -PAGE }
575 [VIS_ACTION_WINDOW_HALFPAGE_UP] = {
576 "vis-window-halfpage-up",
577 VIS_HELP("Scroll window half pages backwards (upwards)")
578 wscroll, { .i = -PAGE_HALF }
580 [VIS_ACTION_WINDOW_PAGE_DOWN] = {
581 "vis-window-page-down",
582 VIS_HELP("Scroll window pages forwards (downwards)")
583 wscroll, { .i = +PAGE }
585 [VIS_ACTION_WINDOW_HALFPAGE_DOWN] = {
586 "vis-window-halfpage-down",
587 VIS_HELP("Scroll window half pages forwards (downwards)")
588 wscroll, { .i = +PAGE_HALF }
590 [VIS_ACTION_MODE_NORMAL] = {
591 "vis-mode-normal",
592 VIS_HELP("Enter normal mode")
593 switchmode, { .i = VIS_MODE_NORMAL }
595 [VIS_ACTION_MODE_NORMAL_ESCAPE] = {
596 "vis-mode-normal-escape",
597 VIS_HELP("Reset count or remove all non-primary selections")
598 normalmode_escape,
600 [VIS_ACTION_MODE_VISUAL] = {
601 "vis-mode-visual-charwise",
602 VIS_HELP("Enter characterwise visual mode")
603 switchmode, { .i = VIS_MODE_VISUAL }
605 [VIS_ACTION_MODE_VISUAL_ESCAPE] = {
606 "vis-mode-visual-escape",
607 VIS_HELP("Reset count or switch to normal mode")
608 visualmode_escape,
610 [VIS_ACTION_MODE_VISUAL_LINE] = {
611 "vis-mode-visual-linewise",
612 VIS_HELP("Enter linewise visual mode")
613 switchmode, { .i = VIS_MODE_VISUAL_LINE }
615 [VIS_ACTION_MODE_INSERT] = {
616 "vis-mode-insert",
617 VIS_HELP("Enter insert mode")
618 insertmode, { .i = VIS_MOVE_NOP }
620 [VIS_ACTION_MODE_REPLACE] = {
621 "vis-mode-replace",
622 VIS_HELP("Enter replace mode")
623 replacemode, { .i = VIS_MOVE_NOP }
625 [VIS_ACTION_DELETE_CHAR_PREV] = {
626 "vis-delete-char-prev",
627 VIS_HELP("Delete the previous character")
628 delete, { .i = VIS_MOVE_CHAR_PREV }
630 [VIS_ACTION_DELETE_CHAR_NEXT] = {
631 "vis-delete-char-next",
632 VIS_HELP("Delete the next character")
633 delete, { .i = VIS_MOVE_CHAR_NEXT }
635 [VIS_ACTION_DELETE_LINE_BEGIN] = {
636 "vis-delete-line-begin",
637 VIS_HELP("Delete until the start of the current line")
638 delete, { .i = VIS_MOVE_LINE_BEGIN }
640 [VIS_ACTION_DELETE_WORD_PREV] = {
641 "vis-delete-word-prev",
642 VIS_HELP("Delete the previous WORD")
643 delete, { .i = VIS_MOVE_WORD_START_PREV }
645 [VIS_ACTION_JUMPLIST_PREV] = {
646 "vis-jumplist-prev",
647 VIS_HELP("Go to older cursor position in jump list")
648 jumplist, { .i = -1 }
650 [VIS_ACTION_JUMPLIST_NEXT] = {
651 "vis-jumplist-next",
652 VIS_HELP("Go to newer cursor position in jump list")
653 jumplist, { .i = +1 }
655 [VIS_ACTION_JUMPLIST_SAVE] = {
656 "vis-jumplist-save",
657 VIS_HELP("Save current selections in jump list")
658 jumplist, { .i = 0 }
660 [VIS_ACTION_UNDO] = {
661 "vis-undo",
662 VIS_HELP("Undo last change")
663 undo,
665 [VIS_ACTION_REDO] = {
666 "vis-redo",
667 VIS_HELP("Redo last change")
668 redo,
670 [VIS_ACTION_EARLIER] = {
671 "vis-earlier",
672 VIS_HELP("Goto older text state")
673 earlier,
675 [VIS_ACTION_LATER] = {
676 "vis-later",
677 VIS_HELP("Goto newer text state")
678 later,
680 [VIS_ACTION_MACRO_RECORD] = {
681 "vis-macro-record",
682 VIS_HELP("Record macro into given register")
683 macro_record,
685 [VIS_ACTION_MACRO_REPLAY] = {
686 "vis-macro-replay",
687 VIS_HELP("Replay macro, execute the content of the given register")
688 macro_replay,
690 [VIS_ACTION_MARK] = {
691 "vis-mark",
692 VIS_HELP("Use given mark for next action")
693 mark,
695 [VIS_ACTION_REDRAW] = {
696 "vis-redraw",
697 VIS_HELP("Redraw current editor content")
698 call, { .f = vis_redraw }
700 [VIS_ACTION_REPLACE_CHAR] = {
701 "vis-replace-char",
702 VIS_HELP("Replace the character under the cursor")
703 replace,
705 [VIS_ACTION_TOTILL_REPEAT] = {
706 "vis-motion-totill-repeat",
707 VIS_HELP("Repeat latest to/till motion")
708 movement, { .i = VIS_MOVE_TOTILL_REPEAT }
710 [VIS_ACTION_TOTILL_REVERSE] = {
711 "vis-motion-totill-reverse",
712 VIS_HELP("Repeat latest to/till motion but in opposite direction")
713 movement, { .i = VIS_MOVE_TOTILL_REVERSE }
715 [VIS_ACTION_PROMPT_SEARCH_FORWARD] = {
716 "vis-search-forward",
717 VIS_HELP("Search forward")
718 prompt_show, { .s = "/" }
720 [VIS_ACTION_PROMPT_SEARCH_BACKWARD] = {
721 "vis-search-backward",
722 VIS_HELP("Search backward")
723 prompt_show, { .s = "?" }
725 [VIS_ACTION_TILL_LEFT] = {
726 "vis-motion-till-left",
727 VIS_HELP("Till after the occurrence of character to the left")
728 movement_key, { .i = VIS_MOVE_TILL_LEFT }
730 [VIS_ACTION_TILL_RIGHT] = {
731 "vis-motion-till-right",
732 VIS_HELP("Till before the occurrence of character to the right")
733 movement_key, { .i = VIS_MOVE_TILL_RIGHT }
735 [VIS_ACTION_TILL_LINE_LEFT] = {
736 "vis-motion-till-line-left",
737 VIS_HELP("Till after the occurrence of character to the left on the current line")
738 movement_key, { .i = VIS_MOVE_TILL_LINE_LEFT }
740 [VIS_ACTION_TILL_LINE_RIGHT] = {
741 "vis-motion-till-line-right",
742 VIS_HELP("Till before the occurrence of character to the right on the current line")
743 movement_key, { .i = VIS_MOVE_TILL_LINE_RIGHT }
745 [VIS_ACTION_TO_LEFT] = {
746 "vis-motion-to-left",
747 VIS_HELP("To the first occurrence of character to the left")
748 movement_key, { .i = VIS_MOVE_TO_LEFT }
750 [VIS_ACTION_TO_RIGHT] = {
751 "vis-motion-to-right",
752 VIS_HELP("To the first occurrence of character to the right")
753 movement_key, { .i = VIS_MOVE_TO_RIGHT }
755 [VIS_ACTION_TO_LINE_LEFT] = {
756 "vis-motion-to-line-left",
757 VIS_HELP("To the first occurrence of character to the left on the current line")
758 movement_key, { .i = VIS_MOVE_TO_LINE_LEFT }
760 [VIS_ACTION_TO_LINE_RIGHT] = {
761 "vis-motion-to-line-right",
762 VIS_HELP("To the first occurrence of character to the right on the current line")
763 movement_key, { .i = VIS_MOVE_TO_LINE_RIGHT }
765 [VIS_ACTION_REGISTER] = {
766 "vis-register",
767 VIS_HELP("Use given register for next operator")
768 reg,
770 [VIS_ACTION_OPERATOR_CHANGE] = {
771 "vis-operator-change",
772 VIS_HELP("Change operator")
773 operator, { .i = VIS_OP_CHANGE }
775 [VIS_ACTION_OPERATOR_DELETE] = {
776 "vis-operator-delete",
777 VIS_HELP("Delete operator")
778 operator, { .i = VIS_OP_DELETE }
780 [VIS_ACTION_OPERATOR_YANK] = {
781 "vis-operator-yank",
782 VIS_HELP("Yank operator")
783 operator, { .i = VIS_OP_YANK }
785 [VIS_ACTION_OPERATOR_SHIFT_LEFT] = {
786 "vis-operator-shift-left",
787 VIS_HELP("Shift left operator")
788 operator, { .i = VIS_OP_SHIFT_LEFT }
790 [VIS_ACTION_OPERATOR_SHIFT_RIGHT] = {
791 "vis-operator-shift-right",
792 VIS_HELP("Shift right operator")
793 operator, { .i = VIS_OP_SHIFT_RIGHT }
795 [VIS_ACTION_COUNT] = {
796 "vis-count",
797 VIS_HELP("Count specifier")
798 count,
800 [VIS_ACTION_INSERT_NEWLINE] = {
801 "vis-insert-newline",
802 VIS_HELP("Insert a line break (depending on file type)")
803 call, { .f = vis_insert_nl }
805 [VIS_ACTION_INSERT_TAB] = {
806 "vis-insert-tab",
807 VIS_HELP("Insert a tab (might be converted to spaces)")
808 call, { .f = vis_insert_tab }
810 [VIS_ACTION_INSERT_VERBATIM] = {
811 "vis-insert-verbatim",
812 VIS_HELP("Insert Unicode character based on code point")
813 insert_verbatim,
815 [VIS_ACTION_INSERT_REGISTER] = {
816 "vis-insert-register",
817 VIS_HELP("Insert specified register content")
818 insert_register,
820 [VIS_ACTION_WINDOW_NEXT] = {
821 "vis-window-next",
822 VIS_HELP("Focus next window")
823 call, { .f = vis_window_next }
825 [VIS_ACTION_WINDOW_PREV] = {
826 "vis-window-prev",
827 VIS_HELP("Focus previous window")
828 call, { .f = vis_window_prev }
830 [VIS_ACTION_APPEND_CHAR_NEXT] = {
831 "vis-append-char-next",
832 VIS_HELP("Append text after the cursor")
833 insertmode, { .i = VIS_MOVE_LINE_CHAR_NEXT }
835 [VIS_ACTION_APPEND_LINE_END] = {
836 "vis-append-line-end",
837 VIS_HELP("Append text after the end of the line")
838 insertmode, { .i = VIS_MOVE_LINE_END },
840 [VIS_ACTION_INSERT_LINE_START] = {
841 "vis-insert-line-start",
842 VIS_HELP("Insert text before the first non-blank in the line")
843 insertmode, { .i = VIS_MOVE_LINE_START },
845 [VIS_ACTION_OPEN_LINE_ABOVE] = {
846 "vis-open-line-above",
847 VIS_HELP("Begin a new line above the cursor")
848 openline, { .i = -1 }
850 [VIS_ACTION_OPEN_LINE_BELOW] = {
851 "vis-open-line-below",
852 VIS_HELP("Begin a new line below the cursor")
853 openline, { .i = +1 }
855 [VIS_ACTION_JOIN_LINES] = {
856 "vis-join-lines",
857 VIS_HELP("Join selected lines")
858 join, { .s = " " }
860 [VIS_ACTION_JOIN_LINES_TRIM] = {
861 "vis-join-lines-trim",
862 VIS_HELP("Join selected lines, remove white space")
863 join, { .s = "" }
865 [VIS_ACTION_PROMPT_SHOW] = {
866 "vis-prompt-show",
867 VIS_HELP("Show editor command line prompt")
868 prompt_show, { .s = ":" }
870 [VIS_ACTION_REPEAT] = {
871 "vis-repeat",
872 VIS_HELP("Repeat latest editor command")
873 repeat
875 [VIS_ACTION_SELECTION_FLIP] = {
876 "vis-selection-flip",
877 VIS_HELP("Flip selection, move cursor to other end")
878 selection_end,
880 [VIS_ACTION_WINDOW_REDRAW_TOP] = {
881 "vis-window-redraw-top",
882 VIS_HELP("Redraw cursor line at the top of the window")
883 window, { .w = view_redraw_top }
885 [VIS_ACTION_WINDOW_REDRAW_CENTER] = {
886 "vis-window-redraw-center",
887 VIS_HELP("Redraw cursor line at the center of the window")
888 window, { .w = view_redraw_center }
890 [VIS_ACTION_WINDOW_REDRAW_BOTTOM] = {
891 "vis-window-redraw-bottom",
892 VIS_HELP("Redraw cursor line at the bottom of the window")
893 window, { .w = view_redraw_bottom }
895 [VIS_ACTION_WINDOW_SLIDE_UP] = {
896 "vis-window-slide-up",
897 VIS_HELP("Slide window content upwards")
898 wslide, { .i = -1 }
900 [VIS_ACTION_WINDOW_SLIDE_DOWN] = {
901 "vis-window-slide-down",
902 VIS_HELP("Slide window content downwards")
903 wslide, { .i = +1 }
905 [VIS_ACTION_PUT_AFTER] = {
906 "vis-put-after",
907 VIS_HELP("Put text after the cursor")
908 operator, { .i = VIS_OP_PUT_AFTER }
910 [VIS_ACTION_PUT_BEFORE] = {
911 "vis-put-before",
912 VIS_HELP("Put text before the cursor")
913 operator, { .i = VIS_OP_PUT_BEFORE }
915 [VIS_ACTION_SELECTIONS_NEW_LINE_ABOVE] = {
916 "vis-selection-new-lines-above",
917 VIS_HELP("Create a new selection on the line above")
918 selections_new, { .i = -1 }
920 [VIS_ACTION_SELECTIONS_NEW_LINE_ABOVE_FIRST] = {
921 "vis-selection-new-lines-above-first",
922 VIS_HELP("Create a new selection on the line above the first selection")
923 selections_new, { .i = INT_MIN }
925 [VIS_ACTION_SELECTIONS_NEW_LINE_BELOW] = {
926 "vis-selection-new-lines-below",
927 VIS_HELP("Create a new selection on the line below")
928 selections_new, { .i = +1 }
930 [VIS_ACTION_SELECTIONS_NEW_LINE_BELOW_LAST] = {
931 "vis-selection-new-lines-below-last",
932 VIS_HELP("Create a new selection on the line below the last selection")
933 selections_new, { .i = INT_MAX }
935 [VIS_ACTION_SELECTIONS_NEW_LINES_BEGIN] = {
936 "vis-selection-new-lines-begin",
937 VIS_HELP("Create a new selection at the start of every line covered by selection")
938 operator, { .i = VIS_OP_CURSOR_SOL }
940 [VIS_ACTION_SELECTIONS_NEW_LINES_END] = {
941 "vis-selection-new-lines-end",
942 VIS_HELP("Create a new selection at the end of every line covered by selection")
943 operator, { .i = VIS_OP_CURSOR_EOL }
945 [VIS_ACTION_SELECTIONS_NEW_MATCH_ALL] = {
946 "vis-selection-new-match-all",
947 VIS_HELP("Select all regions matching the current selection")
948 selections_match_next, { .b = true }
950 [VIS_ACTION_SELECTIONS_NEW_MATCH_NEXT] = {
951 "vis-selection-new-match-next",
952 VIS_HELP("Select the next region matching the current selection")
953 selections_match_next,
955 [VIS_ACTION_SELECTIONS_NEW_MATCH_SKIP] = {
956 "vis-selection-new-match-skip",
957 VIS_HELP("Clear current selection, but select next match")
958 selections_match_skip,
960 [VIS_ACTION_SELECTIONS_ALIGN] = {
961 "vis-selections-align",
962 VIS_HELP("Try to align all selections on the same column")
963 selections_align,
965 [VIS_ACTION_SELECTIONS_ALIGN_INDENT_LEFT] = {
966 "vis-selections-align-indent-left",
967 VIS_HELP("Left-align all selections by inserting spaces")
968 selections_align_indent, { .i = -1 }
970 [VIS_ACTION_SELECTIONS_ALIGN_INDENT_RIGHT] = {
971 "vis-selections-align-indent-right",
972 VIS_HELP("Right-align all selections by inserting spaces")
973 selections_align_indent, { .i = +1 }
975 [VIS_ACTION_SELECTIONS_REMOVE_ALL] = {
976 "vis-selections-remove-all",
977 VIS_HELP("Remove all but the primary selection")
978 selections_clear,
980 [VIS_ACTION_SELECTIONS_REMOVE_LAST] = {
981 "vis-selections-remove-last",
982 VIS_HELP("Remove primary selection")
983 selections_remove,
985 [VIS_ACTION_SELECTIONS_REMOVE_COLUMN] = {
986 "vis-selections-remove-column",
987 VIS_HELP("Remove count selection column")
988 selections_remove_column, { .i = 1 }
990 [VIS_ACTION_SELECTIONS_REMOVE_COLUMN_EXCEPT] = {
991 "vis-selections-remove-column-except",
992 VIS_HELP("Remove all but the count selection column")
993 selections_remove_column_except, { .i = 1 }
995 [VIS_ACTION_SELECTIONS_PREV] = {
996 "vis-selection-prev",
997 VIS_HELP("Move to the previous selection")
998 selections_navigate, { .i = -PAGE_HALF }
1000 [VIS_ACTION_SELECTIONS_NEXT] = {
1001 "vis-selection-next",
1002 VIS_HELP("Move to the next selection")
1003 selections_navigate, { .i = +PAGE_HALF }
1005 [VIS_ACTION_SELECTIONS_ROTATE_LEFT] = {
1006 "vis-selections-rotate-left",
1007 VIS_HELP("Rotate selections left")
1008 selections_rotate, { .i = -1 }
1010 [VIS_ACTION_SELECTIONS_ROTATE_RIGHT] = {
1011 "vis-selections-rotate-right",
1012 VIS_HELP("Rotate selections right")
1013 selections_rotate, { .i = +1 }
1015 [VIS_ACTION_SELECTIONS_TRIM] = {
1016 "vis-selections-trim",
1017 VIS_HELP("Remove leading and trailing white space from selections")
1018 selections_trim
1020 [VIS_ACTION_SELECTIONS_SAVE] = {
1021 "vis-selections-save",
1022 VIS_HELP("Save currently active selections to mark")
1023 selections_save
1025 [VIS_ACTION_SELECTIONS_RESTORE] = {
1026 "vis-selections-restore",
1027 VIS_HELP("Restore selections from mark")
1028 selections_restore
1030 [VIS_ACTION_SELECTIONS_UNION] = {
1031 "vis-selections-union",
1032 VIS_HELP("Add selections from mark")
1033 selections_union
1035 [VIS_ACTION_SELECTIONS_INTERSECT] = {
1036 "vis-selections-intersect",
1037 VIS_HELP("Intersect with selections from mark")
1038 selections_intersect
1040 [VIS_ACTION_SELECTIONS_COMPLEMENT] = {
1041 "vis-selections-complement",
1042 VIS_HELP("Complement selections")
1043 selections_complement
1045 [VIS_ACTION_SELECTIONS_MINUS] = {
1046 "vis-selections-minus",
1047 VIS_HELP("Subtract selections from mark")
1048 selections_minus
1050 [VIS_ACTION_TEXT_OBJECT_WORD_OUTER] = {
1051 "vis-textobject-word-outer",
1052 VIS_HELP("A word leading and trailing whitespace included")
1053 textobj, { .i = VIS_TEXTOBJECT_OUTER_WORD }
1055 [VIS_ACTION_TEXT_OBJECT_WORD_INNER] = {
1056 "vis-textobject-word-inner",
1057 VIS_HELP("A word leading and trailing whitespace excluded")
1058 textobj, { .i = VIS_TEXTOBJECT_INNER_WORD }
1060 [VIS_ACTION_TEXT_OBJECT_LONGWORD_OUTER] = {
1061 "vis-textobject-bigword-outer",
1062 VIS_HELP("A WORD leading and trailing whitespace included")
1063 textobj, { .i = VIS_TEXTOBJECT_OUTER_LONGWORD }
1065 [VIS_ACTION_TEXT_OBJECT_LONGWORD_INNER] = {
1066 "vis-textobject-bigword-inner",
1067 VIS_HELP("A WORD leading and trailing whitespace excluded")
1068 textobj, { .i = VIS_TEXTOBJECT_INNER_LONGWORD }
1070 [VIS_ACTION_TEXT_OBJECT_SENTENCE] = {
1071 "vis-textobject-sentence",
1072 VIS_HELP("A sentence")
1073 textobj, { .i = VIS_TEXTOBJECT_SENTENCE }
1075 [VIS_ACTION_TEXT_OBJECT_PARAGRAPH] = {
1076 "vis-textobject-paragraph",
1077 VIS_HELP("A paragraph")
1078 textobj, { .i = VIS_TEXTOBJECT_PARAGRAPH }
1080 [VIS_ACTION_TEXT_OBJECT_PARAGRAPH_OUTER] = {
1081 "vis-textobject-paragraph-outer",
1082 VIS_HELP("A paragraph (outer variant)")
1083 textobj, { .i = VIS_TEXTOBJECT_PARAGRAPH_OUTER }
1085 [VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_OUTER] = {
1086 "vis-textobject-square-bracket-outer",
1087 VIS_HELP("[] block (outer variant)")
1088 textobj, { .i = VIS_TEXTOBJECT_OUTER_SQUARE_BRACKET }
1090 [VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_INNER] = {
1091 "vis-textobject-square-bracket-inner",
1092 VIS_HELP("[] block (inner variant)")
1093 textobj, { .i = VIS_TEXTOBJECT_INNER_SQUARE_BRACKET }
1095 [VIS_ACTION_TEXT_OBJECT_PARENTHESIS_OUTER] = {
1096 "vis-textobject-parenthesis-outer",
1097 VIS_HELP("() block (outer variant)")
1098 textobj, { .i = VIS_TEXTOBJECT_OUTER_PARENTHESIS }
1100 [VIS_ACTION_TEXT_OBJECT_PARENTHESIS_INNER] = {
1101 "vis-textobject-parenthesis-inner",
1102 VIS_HELP("() block (inner variant)")
1103 textobj, { .i = VIS_TEXTOBJECT_INNER_PARENTHESIS }
1105 [VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_OUTER] = {
1106 "vis-textobject-angle-bracket-outer",
1107 VIS_HELP("<> block (outer variant)")
1108 textobj, { .i = VIS_TEXTOBJECT_OUTER_ANGLE_BRACKET }
1110 [VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_INNER] = {
1111 "vis-textobject-angle-bracket-inner",
1112 VIS_HELP("<> block (inner variant)")
1113 textobj, { .i = VIS_TEXTOBJECT_INNER_ANGLE_BRACKET }
1115 [VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_OUTER] = {
1116 "vis-textobject-curly-bracket-outer",
1117 VIS_HELP("{} block (outer variant)")
1118 textobj, { .i = VIS_TEXTOBJECT_OUTER_CURLY_BRACKET }
1120 [VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_INNER] = {
1121 "vis-textobject-curly-bracket-inner",
1122 VIS_HELP("{} block (inner variant)")
1123 textobj, { .i = VIS_TEXTOBJECT_INNER_CURLY_BRACKET }
1125 [VIS_ACTION_TEXT_OBJECT_QUOTE_OUTER] = {
1126 "vis-textobject-quote-outer",
1127 VIS_HELP("A quoted string, including the quotation marks")
1128 textobj, { .i = VIS_TEXTOBJECT_OUTER_QUOTE }
1130 [VIS_ACTION_TEXT_OBJECT_QUOTE_INNER] = {
1131 "vis-textobject-quote-inner",
1132 VIS_HELP("A quoted string, excluding the quotation marks")
1133 textobj, { .i = VIS_TEXTOBJECT_INNER_QUOTE }
1135 [VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_OUTER] = {
1136 "vis-textobject-single-quote-outer",
1137 VIS_HELP("A single quoted string, including the quotation marks")
1138 textobj, { .i = VIS_TEXTOBJECT_OUTER_SINGLE_QUOTE }
1140 [VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_INNER] = {
1141 "vis-textobject-single-quote-inner",
1142 VIS_HELP("A single quoted string, excluding the quotation marks")
1143 textobj, { .i = VIS_TEXTOBJECT_INNER_SINGLE_QUOTE }
1145 [VIS_ACTION_TEXT_OBJECT_BACKTICK_OUTER] = {
1146 "vis-textobject-backtick-outer",
1147 VIS_HELP("A backtick delimited string (outer variant)")
1148 textobj, { .i = VIS_TEXTOBJECT_OUTER_BACKTICK }
1150 [VIS_ACTION_TEXT_OBJECT_BACKTICK_INNER] = {
1151 "vis-textobject-backtick-inner",
1152 VIS_HELP("A backtick delimited string (inner variant)")
1153 textobj, { .i = VIS_TEXTOBJECT_INNER_BACKTICK }
1155 [VIS_ACTION_TEXT_OBJECT_LINE_OUTER] = {
1156 "vis-textobject-line-outer",
1157 VIS_HELP("The whole line")
1158 textobj, { .i = VIS_TEXTOBJECT_OUTER_LINE }
1160 [VIS_ACTION_TEXT_OBJECT_LINE_INNER] = {
1161 "vis-textobject-line-inner",
1162 VIS_HELP("The whole line, excluding leading and trailing whitespace")
1163 textobj, { .i = VIS_TEXTOBJECT_INNER_LINE }
1165 [VIS_ACTION_TEXT_OBJECT_INDENTATION] = {
1166 "vis-textobject-indentation",
1167 VIS_HELP("All adjacent lines with the same indentation level as the current one")
1168 textobj, { .i = VIS_TEXTOBJECT_INDENTATION }
1170 [VIS_ACTION_TEXT_OBJECT_SEARCH_FORWARD] = {
1171 "vis-textobject-search-forward",
1172 VIS_HELP("The next search match in forward direction")
1173 textobj, { .i = VIS_TEXTOBJECT_SEARCH_FORWARD }
1175 [VIS_ACTION_TEXT_OBJECT_SEARCH_BACKWARD] = {
1176 "vis-textobject-search-backward",
1177 VIS_HELP("The next search match in backward direction")
1178 textobj, { .i = VIS_TEXTOBJECT_SEARCH_BACKWARD }
1180 [VIS_ACTION_UNICODE_INFO] = {
1181 "vis-unicode-info",
1182 VIS_HELP("Show Unicode codepoint(s) of character under cursor")
1183 unicode_info, { .i = VIS_ACTION_UNICODE_INFO }
1185 [VIS_ACTION_UTF8_INFO] = {
1186 "vis-utf8-info",
1187 VIS_HELP("Show UTF-8 encoded codepoint(s) of character under cursor")
1188 unicode_info, { .i = VIS_ACTION_UTF8_INFO }
1190 [VIS_ACTION_NOP] = {
1191 "vis-nop",
1192 VIS_HELP("Ignore key, do nothing")
1193 nop,
1197 #include "config.h"
1199 /** key bindings functions */
1201 static const char *nop(Vis *vis, const char *keys, const Arg *arg) {
1202 return keys;
1205 static const char *macro_record(Vis *vis, const char *keys, const Arg *arg) {
1206 if (!vis_macro_record_stop(vis)) {
1207 if (!keys[0])
1208 return NULL;
1209 const char *next = vis_keys_next(vis, keys);
1210 if (next - keys > 1)
1211 return next;
1212 enum VisRegister reg = vis_register_from(vis, keys[0]);
1213 vis_macro_record(vis, reg);
1214 keys++;
1216 vis_draw(vis);
1217 return keys;
1220 static const char *macro_replay(Vis *vis, const char *keys, const Arg *arg) {
1221 if (!keys[0])
1222 return NULL;
1223 const char *next = vis_keys_next(vis, keys);
1224 if (next - keys > 1)
1225 return next;
1226 enum VisRegister reg = vis_register_from(vis, keys[0]);
1227 vis_macro_replay(vis, reg);
1228 return keys+1;
1231 static const char *suspend(Vis *vis, const char *keys, const Arg *arg) {
1232 vis_suspend(vis);
1233 return keys;
1236 static const char *repeat(Vis *vis, const char *keys, const Arg *arg) {
1237 vis_repeat(vis);
1238 return keys;
1241 static const char *selections_new(Vis *vis, const char *keys, const Arg *arg) {
1242 View *view = vis_view(vis);
1243 bool anchored = view_selections_anchored(view_selections_primary_get(view));
1244 VisCountIterator it = vis_count_iterator_get(vis, 1);
1245 while (vis_count_iterator_next(&it)) {
1246 Selection *sel = NULL;
1247 switch (arg->i) {
1248 case -1:
1249 case +1:
1250 sel = view_selections_primary_get(view);
1251 break;
1252 case INT_MIN:
1253 sel = view_selections(view);
1254 break;
1255 case INT_MAX:
1256 for (Selection *s = view_selections(view); s; s = view_selections_next(s))
1257 sel = s;
1258 break;
1261 if (!sel)
1262 return keys;
1264 size_t oldpos = view_cursors_pos(sel);
1265 if (arg->i > 0)
1266 view_line_down(sel);
1267 else if (arg->i < 0)
1268 view_line_up(sel);
1269 size_t newpos = view_cursors_pos(sel);
1270 view_cursors_to(sel, oldpos);
1271 Selection *sel_new = view_selections_new(view, newpos);
1272 if (!sel_new) {
1273 if (arg->i == -1)
1274 sel_new = view_selections_prev(sel);
1275 else if (arg->i == +1)
1276 sel_new = view_selections_next(sel);
1278 if (sel_new) {
1279 view_selections_primary_set(sel_new);
1280 view_selections_anchor(sel_new, anchored);
1283 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1284 return keys;
1287 static const char *selections_align(Vis *vis, const char *keys, const Arg *arg) {
1288 View *view = vis_view(vis);
1289 Text *txt = vis_text(vis);
1290 int mincol = INT_MAX;
1291 for (Selection *s = view_selections(view); s; s = view_selections_next(s)) {
1292 int col = view_cursors_cell_get(s);
1293 if (col >= 0 && col < mincol)
1294 mincol = col;
1296 for (Selection *s = view_selections(view); s; s = view_selections_next(s)) {
1297 if (view_cursors_cell_set(s, mincol) == -1) {
1298 size_t pos = view_cursors_pos(s);
1299 size_t col = text_line_width_set(txt, pos, mincol);
1300 view_cursors_to(s, col);
1303 return keys;
1306 static const char *selections_align_indent(Vis *vis, const char *keys, const Arg *arg) {
1307 View *view = vis_view(vis);
1308 Text *txt = vis_text(vis);
1309 bool left_align = arg->i < 0;
1310 int columns = view_selections_column_count(view);
1312 for (int i = 0; i < columns; i++) {
1313 int mincol = INT_MAX, maxcol = 0;
1314 for (Selection *s = view_selections_column(view, i); s; s = view_selections_column_next(s, i)) {
1315 Filerange sel = view_selections_get(s);
1316 size_t pos = left_align ? sel.start : sel.end;
1317 int col = text_line_width_get(txt, pos);
1318 if (col < mincol)
1319 mincol = col;
1320 if (col > maxcol)
1321 maxcol = col;
1324 size_t len = maxcol - mincol;
1325 char *buf = malloc(len+1);
1326 if (!buf)
1327 return keys;
1328 memset(buf, ' ', len);
1330 for (Selection *s = view_selections_column(view, i); s; s = view_selections_column_next(s, i)) {
1331 Filerange sel = view_selections_get(s);
1332 size_t pos = left_align ? sel.start : sel.end;
1333 size_t ipos = sel.start;
1334 int col = text_line_width_get(txt, pos);
1335 if (col < maxcol) {
1336 size_t off = maxcol - col;
1337 if (off <= len)
1338 text_insert(txt, ipos, buf, off);
1342 free(buf);
1345 view_draw(view);
1346 return keys;
1349 static const char *selections_clear(Vis *vis, const char *keys, const Arg *arg) {
1350 View *view = vis_view(vis);
1351 if (view_selections_count(view) > 1)
1352 view_selections_dispose_all(view);
1353 else
1354 view_selection_clear(view_selections_primary_get(view));
1355 return keys;
1358 static Selection *selection_new(View *view, Filerange *r, bool isprimary) {
1359 Text *txt = view_text(view);
1360 size_t pos = text_char_prev(txt, r->end);
1361 Selection *s = view_selections_new(view, pos);
1362 if (!s)
1363 return NULL;
1364 view_selections_set(s, r);
1365 view_selections_anchor(s, true);
1366 if (isprimary)
1367 view_selections_primary_set(s);
1368 return s;
1371 static const char *selections_match_next(Vis *vis, const char *keys, const Arg *arg) {
1372 Text *txt = vis_text(vis);
1373 View *view = vis_view(vis);
1374 Selection *s = view_selections_primary_get(view);
1375 Filerange sel = view_selections_get(s);
1376 if (!text_range_valid(&sel))
1377 return keys;
1379 static bool match_word;
1381 if (view_selections_count(view) == 1) {
1382 Filerange word = text_object_word(txt, view_cursors_pos(s));
1383 match_word = text_range_equal(&sel, &word);
1386 Filerange (*find_next)(Text *, size_t, const char *) = text_object_word_find_next;
1387 Filerange (*find_prev)(Text *, size_t, const char *) = text_object_word_find_prev;
1388 if (!match_word) {
1389 find_next = text_object_find_next;
1390 find_prev = text_object_find_prev;
1393 char *buf = text_bytes_alloc0(txt, sel.start, text_range_size(&sel));
1394 if (!buf)
1395 return keys;
1397 bool match_all = arg->b;
1398 Filerange primary = sel;
1400 for (;;) {
1401 sel = find_next(txt, sel.end, buf);
1402 if (!text_range_valid(&sel))
1403 break;
1404 if (selection_new(view, &sel, !match_all) && !match_all)
1405 goto out;
1408 sel = primary;
1410 for (;;) {
1411 sel = find_prev(txt, sel.start, buf);
1412 if (!text_range_valid(&sel))
1413 break;
1414 if (selection_new(view, &sel, !match_all) && !match_all)
1415 break;
1418 out:
1419 free(buf);
1420 return keys;
1423 static const char *selections_match_skip(Vis *vis, const char *keys, const Arg *arg) {
1424 View *view = vis_view(vis);
1425 Selection *sel = view_selections_primary_get(view);
1426 keys = selections_match_next(vis, keys, arg);
1427 if (sel != view_selections_primary_get(view))
1428 view_selections_dispose(sel);
1429 return keys;
1432 static const char *selections_remove(Vis *vis, const char *keys, const Arg *arg) {
1433 View *view = vis_view(vis);
1434 view_selections_dispose(view_selections_primary_get(view));
1435 view_cursor_to(view, view_cursor_get(view));
1436 return keys;
1439 static const char *selections_remove_column(Vis *vis, const char *keys, const Arg *arg) {
1440 View *view = vis_view(vis);
1441 int max = view_selections_column_count(view);
1442 int column = vis_count_get_default(vis, arg->i) - 1;
1443 if (column >= max)
1444 column = max - 1;
1445 if (view_selections_count(view) == 1) {
1446 vis_keys_feed(vis, "<Escape>");
1447 return keys;
1450 for (Selection *s = view_selections_column(view, column), *next; s; s = next) {
1451 next = view_selections_column_next(s, column);
1452 view_selections_dispose(s);
1455 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1456 return keys;
1459 static const char *selections_remove_column_except(Vis *vis, const char *keys, const Arg *arg) {
1460 View *view = vis_view(vis);
1461 int max = view_selections_column_count(view);
1462 int column = vis_count_get_default(vis, arg->i) - 1;
1463 if (column >= max)
1464 column = max - 1;
1465 if (view_selections_count(view) == 1) {
1466 vis_redraw(vis);
1467 return keys;
1470 Selection *sel = view_selections(view);
1471 Selection *col = view_selections_column(view, column);
1472 for (Selection *next; sel; sel = next) {
1473 next = view_selections_next(sel);
1474 if (sel == col)
1475 col = view_selections_column_next(col, column);
1476 else
1477 view_selections_dispose(sel);
1480 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1481 return keys;
1484 static const char *selections_navigate(Vis *vis, const char *keys, const Arg *arg) {
1485 View *view = vis_view(vis);
1486 if (view_selections_count(view) == 1)
1487 return wscroll(vis, keys, arg);
1488 Selection *s = view_selections_primary_get(view);
1489 VisCountIterator it = vis_count_iterator_get(vis, 1);
1490 while (vis_count_iterator_next(&it)) {
1491 if (arg->i > 0) {
1492 s = view_selections_next(s);
1493 if (!s)
1494 s = view_selections(view);
1495 } else {
1496 s = view_selections_prev(s);
1497 if (!s) {
1498 s = view_selections(view);
1499 for (Selection *n = s; n; n = view_selections_next(n))
1500 s = n;
1504 view_selections_primary_set(s);
1505 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1506 return keys;
1509 static const char *selections_rotate(Vis *vis, const char *keys, const Arg *arg) {
1511 typedef struct {
1512 Selection *sel;
1513 char *data;
1514 size_t len;
1515 } Rotate;
1517 Array arr;
1518 Text *txt = vis_text(vis);
1519 View *view = vis_view(vis);
1520 int columns = view_selections_column_count(view);
1521 int selections = columns == 1 ? view_selections_count(view) : columns;
1522 int count = vis_count_get_default(vis, 1);
1523 array_init_sized(&arr, sizeof(Rotate));
1524 if (!array_reserve(&arr, selections))
1525 return keys;
1526 size_t line = 0;
1528 for (Selection *s = view_selections(view), *next; s; s = next) {
1529 next = view_selections_next(s);
1530 size_t line_next = 0;
1532 Filerange sel = view_selections_get(s);
1533 Rotate rot;
1534 rot.sel = s;
1535 rot.len = text_range_size(&sel);
1536 if ((rot.data = malloc(rot.len)))
1537 rot.len = text_bytes_get(txt, sel.start, rot.len, rot.data);
1538 else
1539 rot.len = 0;
1540 array_add(&arr, &rot);
1542 if (!line)
1543 line = text_lineno_by_pos(txt, view_cursors_pos(s));
1544 if (next)
1545 line_next = text_lineno_by_pos(txt, view_cursors_pos(next));
1546 if (!next || (columns > 1 && line != line_next)) {
1547 size_t len = array_length(&arr);
1548 size_t off = arg->i > 0 ? count % len : len - (count % len);
1549 for (size_t i = 0; i < len; i++) {
1550 size_t j = (i + off) % len;
1551 Rotate *oldrot = array_get(&arr, i);
1552 Rotate *newrot = array_get(&arr, j);
1553 if (!oldrot || !newrot || oldrot == newrot)
1554 continue;
1555 Filerange newsel = view_selections_get(newrot->sel);
1556 if (!text_range_valid(&newsel))
1557 continue;
1558 if (!text_delete_range(txt, &newsel))
1559 continue;
1560 if (!text_insert(txt, newsel.start, oldrot->data, oldrot->len))
1561 continue;
1562 newsel.end = newsel.start + oldrot->len;
1563 view_selections_set(newrot->sel, &newsel);
1564 free(oldrot->data);
1566 array_clear(&arr);
1568 line = line_next;
1571 array_release(&arr);
1572 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1573 return keys;
1576 static const char *selections_trim(Vis *vis, const char *keys, const Arg *arg) {
1577 Text *txt = vis_text(vis);
1578 View *view = vis_view(vis);
1579 for (Selection *s = view_selections(view), *next; s; s = next) {
1580 next = view_selections_next(s);
1581 Filerange sel = view_selections_get(s);
1582 if (!text_range_valid(&sel))
1583 continue;
1584 for (char b; sel.start < sel.end && text_byte_get(txt, sel.end-1, &b)
1585 && isspace((unsigned char)b); sel.end--);
1586 for (char b; sel.start <= sel.end && text_byte_get(txt, sel.start, &b)
1587 && isspace((unsigned char)b); sel.start++);
1588 if (sel.start < sel.end) {
1589 view_selections_set(s, &sel);
1590 } else if (!view_selections_dispose(s)) {
1591 vis_mode_switch(vis, VIS_MODE_NORMAL);
1594 return keys;
1597 static void selections_set(Vis *vis, View *view, Array *sel) {
1598 enum VisMode mode = vis_mode_get(vis);
1599 bool anchored = mode == VIS_MODE_VISUAL || mode == VIS_MODE_VISUAL_LINE;
1600 view_selections_set_all(view, sel, anchored);
1601 if (!anchored)
1602 view_selections_clear_all(view);
1605 static const char *selections_save(Vis *vis, const char *keys, const Arg *arg) {
1606 Win *win = vis_window(vis);
1607 View *view = vis_view(vis);
1608 enum VisMark mark = vis_mark_used(vis);
1609 Array sel = view_selections_get_all(view);
1610 vis_mark_set(win, mark, &sel);
1611 array_release(&sel);
1612 vis_cancel(vis);
1613 return keys;
1616 static const char *selections_restore(Vis *vis, const char *keys, const Arg *arg) {
1617 Win *win = vis_window(vis);
1618 View *view = vis_view(vis);
1619 enum VisMark mark = vis_mark_used(vis);
1620 Array sel = vis_mark_get(win, mark);
1621 selections_set(vis, view, &sel);
1622 array_release(&sel);
1623 vis_cancel(vis);
1624 return keys;
1627 static const char *selections_union(Vis *vis, const char *keys, const Arg *arg) {
1628 Win *win = vis_window(vis);
1629 View *view = vis_view(vis);
1630 enum VisMark mark = vis_mark_used(vis);
1631 Array a = vis_mark_get(win, mark);
1632 Array b = view_selections_get_all(view);
1633 Array sel;
1634 array_init_from(&sel, &a);
1636 size_t i = 0, j = 0;
1637 Filerange *r1 = array_get(&a, i), *r2 = array_get(&b, j), cur = text_range_empty();
1638 while (r1 || r2) {
1639 if (r1 && text_range_overlap(r1, &cur)) {
1640 cur = text_range_union(r1, &cur);
1641 r1 = array_get(&a, ++i);
1642 } else if (r2 && text_range_overlap(r2, &cur)) {
1643 cur = text_range_union(r2, &cur);
1644 r2 = array_get(&b, ++j);
1645 } else {
1646 if (text_range_valid(&cur))
1647 array_add(&sel, &cur);
1648 if (!r1) {
1649 cur = *r2;
1650 r2 = array_get(&b, ++j);
1651 } else if (!r2) {
1652 cur = *r1;
1653 r1 = array_get(&a, ++i);
1654 } else {
1655 if (r1->start < r2->start) {
1656 cur = *r1;
1657 r1 = array_get(&a, ++i);
1658 } else {
1659 cur = *r2;
1660 r2 = array_get(&b, ++j);
1666 if (text_range_valid(&cur))
1667 array_add(&sel, &cur);
1669 selections_set(vis, view, &sel);
1670 vis_cancel(vis);
1672 array_release(&a);
1673 array_release(&b);
1674 array_release(&sel);
1676 return keys;
1679 static void intersect(Array *ret, Array *a, Array *b) {
1680 size_t i = 0, j = 0;
1681 Filerange *r1 = array_get(a, i), *r2 = array_get(b, j);
1682 while (r1 && r2) {
1683 if (text_range_overlap(r1, r2)) {
1684 Filerange new = text_range_intersect(r1, r2);
1685 array_add(ret, &new);
1687 if (r1->end < r2->end)
1688 r1 = array_get(a, ++i);
1689 else
1690 r2 = array_get(b, ++j);
1694 static const char *selections_intersect(Vis *vis, const char *keys, const Arg *arg) {
1695 Win *win = vis_window(vis);
1696 View *view = vis_view(vis);
1697 enum VisMark mark = vis_mark_used(vis);
1698 Array a = vis_mark_get(win, mark);
1699 Array b = view_selections_get_all(view);
1700 Array sel;
1701 array_init_from(&sel, &a);
1703 intersect(&sel, &a, &b);
1704 selections_set(vis, view, &sel);
1705 vis_cancel(vis);
1707 array_release(&a);
1708 array_release(&b);
1709 array_release(&sel);
1711 return keys;
1714 static void complement(Array *ret, Array *a, Filerange *universe) {
1715 size_t pos = universe->start;
1716 for (size_t i = 0, len = array_length(a); i < len; i++) {
1717 Filerange *r = array_get(a, i);
1718 if (pos < r->start) {
1719 Filerange new = text_range_new(pos, r->start);
1720 array_add(ret, &new);
1722 pos = r->end;
1724 if (pos < universe->end) {
1725 Filerange new = text_range_new(pos, universe->end);
1726 array_add(ret, &new);
1730 static const char *selections_complement(Vis *vis, const char *keys, const Arg *arg) {
1731 Text *txt = vis_text(vis);
1732 View *view = vis_view(vis);
1733 Filerange universe = text_object_entire(txt, 0);
1734 Array a = view_selections_get_all(view);
1735 Array sel;
1736 array_init_from(&sel, &a);
1738 complement(&sel, &a, &universe);
1740 selections_set(vis, view, &sel);
1741 array_release(&a);
1742 array_release(&sel);
1743 return keys;
1746 static const char *selections_minus(Vis *vis, const char *keys, const Arg *arg) {
1747 Text *txt = vis_text(vis);
1748 Win *win = vis_window(vis);
1749 View *view = vis_view(vis);
1750 enum VisMark mark = vis_mark_used(vis);
1751 Array a = view_selections_get_all(view);
1752 Array b = vis_mark_get(win, mark);
1753 Array sel;
1754 array_init_from(&sel, &a);
1755 Array b_complement;
1756 array_init_from(&b_complement, &b);
1758 Filerange universe = text_object_entire(txt, 0);
1759 complement(&b_complement, &b, &universe);
1760 intersect(&sel, &a, &b_complement);
1762 selections_set(vis, view, &sel);
1763 vis_cancel(vis);
1765 array_release(&a);
1766 array_release(&b);
1767 array_release(&b_complement);
1768 array_release(&sel);
1770 return keys;
1773 static const char *replace(Vis *vis, const char *keys, const Arg *arg) {
1774 if (!keys[0]) {
1775 vis_keymap_disable(vis);
1776 return NULL;
1779 const char *next = vis_keys_next(vis, keys);
1780 if (!next)
1781 return NULL;
1783 char replacement[UTFmax+1];
1784 if (!vis_keys_utf8(vis, keys, replacement))
1785 return next;
1787 if (replacement[0] == 0x1b) /* <Escape> */
1788 return next;
1790 vis_operator(vis, VIS_OP_REPLACE, replacement);
1791 if (vis_mode_get(vis) == VIS_MODE_OPERATOR_PENDING)
1792 vis_motion(vis, VIS_MOVE_CHAR_NEXT);
1793 return next;
1796 static const char *count(Vis *vis, const char *keys, const Arg *arg) {
1797 int digit = keys[-1] - '0';
1798 int count = vis_count_get_default(vis, 0);
1799 if (0 <= digit && digit <= 9) {
1800 if (digit == 0 && count == 0)
1801 vis_motion(vis, VIS_MOVE_LINE_BEGIN);
1802 else
1803 vis_count_set(vis, count * 10 + digit);
1805 return keys;
1808 static const char *gotoline(Vis *vis, const char *keys, const Arg *arg) {
1809 if (vis_count_get(vis) != VIS_COUNT_UNKNOWN)
1810 vis_motion(vis, VIS_MOVE_LINE);
1811 else if (arg->i < 0)
1812 vis_motion(vis, VIS_MOVE_FILE_BEGIN);
1813 else
1814 vis_motion(vis, VIS_MOVE_FILE_END);
1815 return keys;
1818 static const char *operator(Vis *vis, const char *keys, const Arg *arg) {
1819 vis_operator(vis, arg->i);
1820 return keys;
1823 static const char *movement_key(Vis *vis, const char *keys, const Arg *arg) {
1824 if (!keys[0]) {
1825 vis_keymap_disable(vis);
1826 return NULL;
1829 const char *next = vis_keys_next(vis, keys);
1830 if (!next)
1831 return NULL;
1832 char utf8[UTFmax+1];
1833 if (vis_keys_utf8(vis, keys, utf8))
1834 vis_motion(vis, arg->i, utf8);
1835 return next;
1838 static const char *movement(Vis *vis, const char *keys, const Arg *arg) {
1839 vis_motion(vis, arg->i);
1840 return keys;
1843 static const char *textobj(Vis *vis, const char *keys, const Arg *arg) {
1844 vis_textobject(vis, arg->i);
1845 return keys;
1848 static const char *selection_end(Vis *vis, const char *keys, const Arg *arg) {
1849 for (Selection *s = view_selections(vis_view(vis)); s; s = view_selections_next(s))
1850 view_selections_flip(s);
1851 return keys;
1854 static const char *reg(Vis *vis, const char *keys, const Arg *arg) {
1855 if (!keys[0])
1856 return NULL;
1857 const char *next = vis_keys_next(vis, keys);
1858 if (next - keys > 1)
1859 return next;
1860 enum VisRegister reg = vis_register_from(vis, keys[0]);
1861 vis_register(vis, reg);
1862 return keys+1;
1865 static const char *mark(Vis *vis, const char *keys, const Arg *arg) {
1866 if (!keys[0])
1867 return NULL;
1868 const char *next = vis_keys_next(vis, keys);
1869 if (next - keys > 1)
1870 return next;
1871 enum VisMark mark = vis_mark_from(vis, keys[0]);
1872 vis_mark(vis, mark);
1873 return keys+1;
1876 static const char *undo(Vis *vis, const char *keys, const Arg *arg) {
1877 size_t pos = text_undo(vis_text(vis));
1878 if (pos != EPOS) {
1879 View *view = vis_view(vis);
1880 if (view_selections_count(view) == 1)
1881 view_cursor_to(view, pos);
1882 /* redraw all windows in case some display the same file */
1883 vis_draw(vis);
1885 return keys;
1888 static const char *redo(Vis *vis, const char *keys, const Arg *arg) {
1889 size_t pos = text_redo(vis_text(vis));
1890 if (pos != EPOS) {
1891 View *view = vis_view(vis);
1892 if (view_selections_count(view) == 1)
1893 view_cursor_to(view, pos);
1894 /* redraw all windows in case some display the same file */
1895 vis_draw(vis);
1897 return keys;
1900 static const char *earlier(Vis *vis, const char *keys, const Arg *arg) {
1901 size_t pos = EPOS;
1902 VisCountIterator it = vis_count_iterator_get(vis, 1);
1903 while (vis_count_iterator_next(&it))
1904 pos = text_earlier(vis_text(vis));
1905 if (pos != EPOS) {
1906 view_cursor_to(vis_view(vis), pos);
1907 /* redraw all windows in case some display the same file */
1908 vis_draw(vis);
1910 return keys;
1913 static const char *later(Vis *vis, const char *keys, const Arg *arg) {
1914 size_t pos = EPOS;
1915 VisCountIterator it = vis_count_iterator_get(vis, 1);
1916 while (vis_count_iterator_next(&it))
1917 pos = text_later(vis_text(vis));
1918 if (pos != EPOS) {
1919 view_cursor_to(vis_view(vis), pos);
1920 /* redraw all windows in case some display the same file */
1921 vis_draw(vis);
1923 return keys;
1926 static const char *delete(Vis *vis, const char *keys, const Arg *arg) {
1927 vis_operator(vis, VIS_OP_DELETE);
1928 vis_motion(vis, arg->i);
1929 return keys;
1932 static const char *insert_register(Vis *vis, const char *keys, const Arg *arg) {
1933 if (!keys[0])
1934 return NULL;
1935 const char *next = vis_keys_next(vis, keys);
1936 if (next - keys > 1)
1937 return next;
1938 enum VisRegister reg = vis_register_from(vis, keys[0]);
1939 if (reg != VIS_REG_INVALID) {
1940 vis_register(vis, reg);
1941 vis_operator(vis, VIS_OP_PUT_BEFORE_END);
1943 return keys+1;
1946 static const char *prompt_show(Vis *vis, const char *keys, const Arg *arg) {
1947 vis_prompt_show(vis, arg->s);
1948 return keys;
1951 static const char *insert_verbatim(Vis *vis, const char *keys, const Arg *arg) {
1952 Rune rune = 0;
1953 char buf[4], type = keys[0];
1954 const char *data = NULL;
1955 int len = 0, count = 0, base = 0;
1956 switch (type) {
1957 case '\0':
1958 return NULL;
1959 case 'o':
1960 case 'O':
1961 count = 3;
1962 base = 8;
1963 break;
1964 case 'U':
1965 count = 4;
1966 /* fall through */
1967 case 'u':
1968 count += 4;
1969 base = 16;
1970 break;
1971 case 'x':
1972 case 'X':
1973 count = 2;
1974 base = 16;
1975 break;
1976 default:
1977 if ('0' <= type && type <= '9') {
1978 rune = type - '0';
1979 count = 2;
1980 base = 10;
1982 break;
1985 if (base) {
1986 for (keys++; keys[0] && count > 0; keys++, count--) {
1987 int v = 0;
1988 if (base == 8 && '0' <= keys[0] && keys[0] <= '7') {
1989 v = keys[0] - '0';
1990 } else if ((base == 10 || base == 16) && '0' <= keys[0] && keys[0] <= '9') {
1991 v = keys[0] - '0';
1992 } else if (base == 16 && 'a' <= keys[0] && keys[0] <= 'f') {
1993 v = 10 + keys[0] - 'a';
1994 } else if (base == 16 && 'A' <= keys[0] && keys[0] <= 'F') {
1995 v = 10 + keys[0] - 'A';
1996 } else {
1997 count = 0;
1998 break;
2000 rune = rune * base + v;
2003 if (count > 0)
2004 return NULL;
2005 if (type == 'u' || type == 'U') {
2006 len = runetochar(buf, &rune);
2007 } else {
2008 buf[0] = rune;
2009 len = 1;
2012 data = buf;
2013 } else {
2014 const char *next = vis_keys_next(vis, keys);
2015 if (!next)
2016 return NULL;
2017 if ((rune = vis_keys_codepoint(vis, keys)) != (Rune)-1) {
2018 len = runetochar(buf, &rune);
2019 if (buf[0] == '\n')
2020 buf[0] = '\r';
2021 data = buf;
2022 } else {
2023 vis_info_show(vis, "Unknown key");
2025 keys = next;
2028 if (len > 0)
2029 vis_insert_key(vis, data, len);
2030 return keys;
2033 static const char *wscroll(Vis *vis, const char *keys, const Arg *arg) {
2034 View *view = vis_view(vis);
2035 int count = vis_count_get(vis);
2036 switch (arg->i) {
2037 case -PAGE:
2038 view_scroll_page_up(view);
2039 break;
2040 case +PAGE:
2041 view_scroll_page_down(view);
2042 break;
2043 case -PAGE_HALF:
2044 view_scroll_halfpage_up(view);
2045 break;
2046 case +PAGE_HALF:
2047 view_scroll_halfpage_down(view);
2048 break;
2049 default:
2050 if (count == VIS_COUNT_UNKNOWN)
2051 count = arg->i < 0 ? -arg->i : arg->i;
2052 if (arg->i < 0)
2053 view_scroll_up(view, count);
2054 else
2055 view_scroll_down(view, count);
2056 break;
2058 vis_count_set(vis, VIS_COUNT_UNKNOWN);
2059 return keys;
2062 static const char *wslide(Vis *vis, const char *keys, const Arg *arg) {
2063 View *view = vis_view(vis);
2064 int count = vis_count_get(vis);
2065 if (count == VIS_COUNT_UNKNOWN)
2066 count = arg->i < 0 ? -arg->i : arg->i;
2067 if (arg->i >= 0)
2068 view_slide_down(view, count);
2069 else
2070 view_slide_up(view, count);
2071 vis_count_set(vis, VIS_COUNT_UNKNOWN);
2072 return keys;
2075 static const char *call(Vis *vis, const char *keys, const Arg *arg) {
2076 arg->f(vis);
2077 return keys;
2080 static const char *window(Vis *vis, const char *keys, const Arg *arg) {
2081 arg->w(vis_view(vis));
2082 return keys;
2085 static const char *openline(Vis *vis, const char *keys, const Arg *arg) {
2086 vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_INSERT);
2087 if (arg->i > 0) {
2088 vis_motion(vis, VIS_MOVE_LINE_END);
2089 vis_keys_feed(vis, "<Enter>");
2090 } else {
2091 if (vis_get_autoindent(vis)) {
2092 vis_motion(vis, VIS_MOVE_LINE_START);
2093 vis_keys_feed(vis, "<vis-motion-line-start>");
2094 } else {
2095 vis_motion(vis, VIS_MOVE_LINE_BEGIN);
2096 vis_keys_feed(vis, "<vis-motion-line-begin>");
2098 vis_keys_feed(vis, "<Enter><vis-motion-line-up>");
2100 return keys;
2103 static const char *join(Vis *vis, const char *keys, const Arg *arg) {
2104 bool normal = (vis_mode_get(vis) == VIS_MODE_NORMAL);
2105 vis_operator(vis, VIS_OP_JOIN, arg->s);
2106 if (normal) {
2107 int count = vis_count_get_default(vis, 0);
2108 if (count)
2109 vis_count_set(vis, count-1);
2110 vis_motion(vis, VIS_MOVE_LINE_NEXT);
2112 return keys;
2115 static const char *normalmode_escape(Vis *vis, const char *keys, const Arg *arg) {
2116 if (vis_count_get(vis) == VIS_COUNT_UNKNOWN)
2117 selections_clear(vis, keys, arg);
2118 else
2119 vis_count_set(vis, VIS_COUNT_UNKNOWN);
2120 return keys;
2123 static const char *visualmode_escape(Vis *vis, const char *keys, const Arg *arg) {
2124 if (vis_count_get(vis) == VIS_COUNT_UNKNOWN)
2125 vis_mode_switch(vis, VIS_MODE_NORMAL);
2126 else
2127 vis_count_set(vis, VIS_COUNT_UNKNOWN);
2128 return keys;
2131 static const char *switchmode(Vis *vis, const char *keys, const Arg *arg) {
2132 vis_mode_switch(vis, arg->i);
2133 return keys;
2136 static const char *insertmode(Vis *vis, const char *keys, const Arg *arg) {
2137 vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_INSERT);
2138 vis_motion(vis, arg->i);
2139 return keys;
2142 static const char *replacemode(Vis *vis, const char *keys, const Arg *arg) {
2143 vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_REPLACE);
2144 vis_motion(vis, arg->i);
2145 return keys;
2148 static const char *unicode_info(Vis *vis, const char *keys, const Arg *arg) {
2149 View *view = vis_view(vis);
2150 Text *txt = vis_text(vis);
2151 size_t start = view_cursor_get(view);
2152 size_t end = text_char_next(txt, start);
2153 char *grapheme = text_bytes_alloc0(txt, start, end-start), *codepoint = grapheme;
2154 if (!grapheme)
2155 return keys;
2156 Buffer info;
2157 buffer_init(&info);
2158 mbstate_t ps = { 0 };
2159 Iterator it = text_iterator_get(txt, start);
2160 for (size_t pos = start; it.pos < end; pos = it.pos) {
2161 if (!text_iterator_codepoint_next(&it, NULL)) {
2162 vis_info_show(vis, "Failed to parse code point");
2163 goto err;
2165 size_t len = it.pos - pos;
2166 wchar_t wc = 0xFFFD;
2167 size_t res = mbrtowc(&wc, codepoint, len, &ps);
2168 bool combining = false;
2169 if (res != (size_t)-1 && res != (size_t)-2)
2170 combining = (wc != L'\0' && wcwidth(wc) == 0);
2171 unsigned char ch = *codepoint;
2172 if (ch < 128 && !isprint(ch))
2173 buffer_appendf(&info, "<^%c> ", ch == 127 ? '?' : ch + 64);
2174 else
2175 buffer_appendf(&info, "<%s%.*s> ", combining ? " " : "", (int)len, codepoint);
2176 if (arg->i == VIS_ACTION_UNICODE_INFO) {
2177 buffer_appendf(&info, "U+%04"PRIX32" ", (uint32_t)wc);
2178 } else {
2179 for (size_t i = 0; i < len; i++)
2180 buffer_appendf(&info, "%02x ", (uint8_t)codepoint[i]);
2182 codepoint += len;
2184 vis_info_show(vis, "%s", buffer_content0(&info));
2185 err:
2186 free(grapheme);
2187 buffer_release(&info);
2188 return keys;
2191 static const char *percent(Vis *vis, const char *keys, const Arg *arg) {
2192 if (vis_count_get(vis) == VIS_COUNT_UNKNOWN)
2193 vis_motion(vis, VIS_MOVE_BRACKET_MATCH);
2194 else
2195 vis_motion(vis, VIS_MOVE_PERCENT);
2196 return keys;
2199 static const char *jumplist(Vis *vis, const char *keys, const Arg *arg) {
2200 if (arg->i < 0)
2201 vis_jumplist_prev(vis);
2202 else if (arg->i > 0)
2203 vis_jumplist_next(vis);
2204 else
2205 vis_jumplist_save(vis);
2206 return keys;
2209 static Vis *vis;
2211 static void signal_handler(int signum, siginfo_t *siginfo, void *context) {
2212 vis_signal_handler(vis, signum, siginfo, context);
2215 int main(int argc, char *argv[]) {
2217 VisEvent event = {
2218 .init = vis_lua_init,
2219 .start = vis_lua_start,
2220 .quit = vis_lua_quit,
2221 .mode_insert_input = vis_lua_mode_insert_input,
2222 .mode_replace_input = vis_lua_mode_replace_input,
2223 .file_open = vis_lua_file_open,
2224 .file_save_pre = vis_lua_file_save_pre,
2225 .file_save_post = vis_lua_file_save_post,
2226 .file_close = vis_lua_file_close,
2227 .win_open = vis_lua_win_open,
2228 .win_close = vis_lua_win_close,
2229 .win_highlight = vis_lua_win_highlight,
2230 .win_status = vis_lua_win_status,
2231 .term_csi = vis_lua_term_csi,
2234 vis = vis_new(ui_term_new(), &event);
2235 if (!vis)
2236 return EXIT_FAILURE;
2238 for (int i = 0; i < LENGTH(vis_action); i++) {
2239 const KeyAction *action = &vis_action[i];
2240 if (!vis_action_register(vis, action))
2241 vis_die(vis, "Could not register action: %s\n", action->name);
2244 for (int i = 0; i < LENGTH(default_bindings); i++) {
2245 for (const KeyBinding **binding = default_bindings[i]; binding && *binding; binding++) {
2246 for (const KeyBinding *kb = *binding; kb->key; kb++) {
2247 vis_mode_map(vis, i, false, kb->key, kb);
2252 for (const char **k = keymaps; k[0]; k += 2)
2253 vis_keymap_add(vis, k[0], k[1]);
2255 /* install signal handlers etc. */
2256 struct sigaction sa;
2257 memset(&sa, 0, sizeof sa);
2258 sigfillset(&sa.sa_mask);
2259 sa.sa_flags = SA_SIGINFO;
2260 sa.sa_sigaction = signal_handler;
2261 if (sigaction(SIGBUS, &sa, NULL) == -1 ||
2262 sigaction(SIGINT, &sa, NULL) == -1 ||
2263 sigaction(SIGCONT, &sa, NULL) == -1 ||
2264 sigaction(SIGWINCH, &sa, NULL) == -1 ||
2265 sigaction(SIGTERM, &sa, NULL) == -1 ||
2266 sigaction(SIGHUP, &sa, NULL) == -1) {
2267 vis_die(vis, "Failed to set signal handler: %s\n", strerror(errno));
2270 sa.sa_handler = SIG_IGN;
2271 if (sigaction(SIGPIPE, &sa, NULL) == -1 || sigaction(SIGQUIT, &sa, NULL) == -1)
2272 vis_die(vis, "Failed to ignore signals\n");
2274 sigset_t blockset;
2275 sigemptyset(&blockset);
2276 sigaddset(&blockset, SIGBUS);
2277 sigaddset(&blockset, SIGCONT);
2278 sigaddset(&blockset, SIGWINCH);
2279 sigaddset(&blockset, SIGTERM);
2280 sigaddset(&blockset, SIGHUP);
2281 if (sigprocmask(SIG_BLOCK, &blockset, NULL) == -1)
2282 vis_die(vis, "Failed to block signals\n");
2284 for (int i = 1; i < argc; i++) {
2285 if (argv[i][0] != '-') {
2286 continue;
2287 } else if (strcmp(argv[i], "-") == 0) {
2288 continue;
2289 } else if (strcmp(argv[i], "--") == 0) {
2290 break;
2291 } else if (strcmp(argv[i], "-v") == 0) {
2292 printf("vis %s%s%s%s%s%s%s\n", VERSION,
2293 CONFIG_CURSES ? " +curses" : "",
2294 CONFIG_LUA ? " +lua" : "",
2295 CONFIG_LPEG ? " +lpeg" : "",
2296 CONFIG_TRE ? " +tre" : "",
2297 CONFIG_ACL ? " +acl" : "",
2298 CONFIG_SELINUX ? " +selinux" : "");
2299 return 0;
2300 } else {
2301 fprintf(stderr, "Unknown command option: %s\n", argv[i]);
2302 return 1;
2306 char *cmd = NULL;
2307 bool end_of_options = false, win_created = false;
2309 for (int i = 1; i < argc; i++) {
2310 if (argv[i][0] == '-' && !end_of_options) {
2311 if (strcmp(argv[i], "-") == 0) {
2312 if (!vis_window_new_fd(vis, STDOUT_FILENO))
2313 vis_die(vis, "Can not create empty buffer\n");
2314 ssize_t len = 0;
2315 char buf[PIPE_BUF];
2316 Text *txt = vis_text(vis);
2317 while ((len = read(STDIN_FILENO, buf, sizeof buf)) > 0)
2318 text_insert(txt, text_size(txt), buf, len);
2319 if (len == -1)
2320 vis_die(vis, "Can not read from stdin\n");
2321 text_snapshot(txt);
2322 int fd = open("/dev/tty", O_RDWR);
2323 if (fd == -1)
2324 vis_die(vis, "Can not reopen stdin\n");
2325 dup2(fd, STDIN_FILENO);
2326 close(fd);
2327 } else if (strcmp(argv[i], "--") == 0) {
2328 end_of_options = true;
2329 continue;
2331 } else if (argv[i][0] == '+' && !end_of_options) {
2332 cmd = argv[i] + (argv[i][1] == '/' || argv[i][1] == '?');
2333 continue;
2334 } else if (!vis_window_new(vis, argv[i])) {
2335 vis_die(vis, "Can not load `%s': %s\n", argv[i], strerror(errno));
2337 win_created = true;
2338 if (cmd) {
2339 vis_prompt_cmd(vis, cmd);
2340 cmd = NULL;
2344 if (!vis_window(vis) && !win_created) {
2345 if (!vis_window_new(vis, NULL))
2346 vis_die(vis, "Can not create empty buffer\n");
2347 if (cmd)
2348 vis_prompt_cmd(vis, cmd);
2351 int status = vis_run(vis);
2352 vis_free(vis);
2353 return status;