Merge branch 'csi_event' of https://github.com/ezdiy/vis into master
[vis.git] / main.c
blobeb855aedc43aa094d687762c360648dfc057c28c
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_{RIGHT,LEFT}_{TO,TILL} */
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_TO_LEFT,
226 VIS_ACTION_TO_RIGHT,
227 VIS_ACTION_REGISTER,
228 VIS_ACTION_OPERATOR_CHANGE,
229 VIS_ACTION_OPERATOR_DELETE,
230 VIS_ACTION_OPERATOR_YANK,
231 VIS_ACTION_OPERATOR_SHIFT_LEFT,
232 VIS_ACTION_OPERATOR_SHIFT_RIGHT,
233 VIS_ACTION_COUNT,
234 VIS_ACTION_INSERT_NEWLINE,
235 VIS_ACTION_INSERT_TAB,
236 VIS_ACTION_INSERT_VERBATIM,
237 VIS_ACTION_INSERT_REGISTER,
238 VIS_ACTION_WINDOW_NEXT,
239 VIS_ACTION_WINDOW_PREV,
240 VIS_ACTION_APPEND_CHAR_NEXT,
241 VIS_ACTION_APPEND_LINE_END,
242 VIS_ACTION_INSERT_LINE_START,
243 VIS_ACTION_OPEN_LINE_ABOVE,
244 VIS_ACTION_OPEN_LINE_BELOW,
245 VIS_ACTION_JOIN_LINES,
246 VIS_ACTION_JOIN_LINES_TRIM,
247 VIS_ACTION_PROMPT_SHOW,
248 VIS_ACTION_REPEAT,
249 VIS_ACTION_SELECTION_FLIP,
250 VIS_ACTION_WINDOW_REDRAW_TOP,
251 VIS_ACTION_WINDOW_REDRAW_CENTER,
252 VIS_ACTION_WINDOW_REDRAW_BOTTOM,
253 VIS_ACTION_WINDOW_SLIDE_UP,
254 VIS_ACTION_WINDOW_SLIDE_DOWN,
255 VIS_ACTION_PUT_AFTER,
256 VIS_ACTION_PUT_BEFORE,
257 VIS_ACTION_SELECTIONS_NEW_LINE_ABOVE,
258 VIS_ACTION_SELECTIONS_NEW_LINE_ABOVE_FIRST,
259 VIS_ACTION_SELECTIONS_NEW_LINE_BELOW,
260 VIS_ACTION_SELECTIONS_NEW_LINE_BELOW_LAST,
261 VIS_ACTION_SELECTIONS_NEW_LINES_BEGIN,
262 VIS_ACTION_SELECTIONS_NEW_LINES_END,
263 VIS_ACTION_SELECTIONS_NEW_MATCH_NEXT,
264 VIS_ACTION_SELECTIONS_NEW_MATCH_SKIP,
265 VIS_ACTION_SELECTIONS_ALIGN,
266 VIS_ACTION_SELECTIONS_ALIGN_INDENT_LEFT,
267 VIS_ACTION_SELECTIONS_ALIGN_INDENT_RIGHT,
268 VIS_ACTION_SELECTIONS_REMOVE_ALL,
269 VIS_ACTION_SELECTIONS_REMOVE_LAST,
270 VIS_ACTION_SELECTIONS_REMOVE_COLUMN,
271 VIS_ACTION_SELECTIONS_REMOVE_COLUMN_EXCEPT,
272 VIS_ACTION_SELECTIONS_PREV,
273 VIS_ACTION_SELECTIONS_NEXT,
274 VIS_ACTION_SELECTIONS_ROTATE_LEFT,
275 VIS_ACTION_SELECTIONS_ROTATE_RIGHT,
276 VIS_ACTION_SELECTIONS_TRIM,
277 VIS_ACTION_SELECTIONS_SAVE,
278 VIS_ACTION_SELECTIONS_RESTORE,
279 VIS_ACTION_SELECTIONS_UNION,
280 VIS_ACTION_SELECTIONS_INTERSECT,
281 VIS_ACTION_SELECTIONS_COMPLEMENT,
282 VIS_ACTION_SELECTIONS_MINUS,
283 VIS_ACTION_TEXT_OBJECT_WORD_OUTER,
284 VIS_ACTION_TEXT_OBJECT_WORD_INNER,
285 VIS_ACTION_TEXT_OBJECT_LONGWORD_OUTER,
286 VIS_ACTION_TEXT_OBJECT_LONGWORD_INNER,
287 VIS_ACTION_TEXT_OBJECT_SENTENCE,
288 VIS_ACTION_TEXT_OBJECT_PARAGRAPH,
289 VIS_ACTION_TEXT_OBJECT_PARAGRAPH_OUTER,
290 VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_OUTER,
291 VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_INNER,
292 VIS_ACTION_TEXT_OBJECT_PARENTHESIS_OUTER,
293 VIS_ACTION_TEXT_OBJECT_PARENTHESIS_INNER,
294 VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_OUTER,
295 VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_INNER,
296 VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_OUTER,
297 VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_INNER,
298 VIS_ACTION_TEXT_OBJECT_QUOTE_OUTER,
299 VIS_ACTION_TEXT_OBJECT_QUOTE_INNER,
300 VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_OUTER,
301 VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_INNER,
302 VIS_ACTION_TEXT_OBJECT_BACKTICK_OUTER,
303 VIS_ACTION_TEXT_OBJECT_BACKTICK_INNER,
304 VIS_ACTION_TEXT_OBJECT_LINE_OUTER,
305 VIS_ACTION_TEXT_OBJECT_LINE_INNER,
306 VIS_ACTION_TEXT_OBJECT_INDENTATION,
307 VIS_ACTION_TEXT_OBJECT_SEARCH_FORWARD,
308 VIS_ACTION_TEXT_OBJECT_SEARCH_BACKWARD,
309 VIS_ACTION_UNICODE_INFO,
310 VIS_ACTION_UTF8_INFO,
311 VIS_ACTION_NOP,
314 static const KeyAction vis_action[] = {
315 [VIS_ACTION_EDITOR_SUSPEND] = {
316 "vis-suspend",
317 VIS_HELP("Suspend the editor")
318 suspend,
320 [VIS_ACTION_CURSOR_CHAR_PREV] = {
321 "vis-motion-char-prev",
322 VIS_HELP("Move cursor left, to the previous character")
323 movement, { .i = VIS_MOVE_CHAR_PREV }
325 [VIS_ACTION_CURSOR_CHAR_NEXT] = {
326 "vis-motion-char-next",
327 VIS_HELP("Move cursor right, to the next character")
328 movement, { .i = VIS_MOVE_CHAR_NEXT }
330 [VIS_ACTION_CURSOR_LINE_CHAR_PREV] = {
331 "vis-motion-line-char-prev",
332 VIS_HELP("Move cursor left, to the previous character on the same line")
333 movement, { .i = VIS_MOVE_LINE_CHAR_PREV }
335 [VIS_ACTION_CURSOR_LINE_CHAR_NEXT] = {
336 "vis-motion-line-char-next",
337 VIS_HELP("Move cursor right, to the next character on the same line")
338 movement, { .i = VIS_MOVE_LINE_CHAR_NEXT }
340 [VIS_ACTION_CURSOR_CODEPOINT_PREV] = {
341 "vis-motion-codepoint-prev",
342 VIS_HELP("Move to the previous Unicode codepoint")
343 movement, { .i = VIS_MOVE_CODEPOINT_PREV }
345 [VIS_ACTION_CURSOR_CODEPOINT_NEXT] = {
346 "vis-motion-codepoint-next",
347 VIS_HELP("Move to the next Unicode codepoint")
348 movement, { .i = VIS_MOVE_CODEPOINT_NEXT }
350 [VIS_ACTION_CURSOR_WORD_START_PREV] = {
351 "vis-motion-word-start-prev",
352 VIS_HELP("Move cursor words backwards")
353 movement, { .i = VIS_MOVE_WORD_START_PREV }
355 [VIS_ACTION_CURSOR_WORD_START_NEXT] = {
356 "vis-motion-word-start-next",
357 VIS_HELP("Move cursor words forwards")
358 movement, { .i = VIS_MOVE_WORD_START_NEXT }
360 [VIS_ACTION_CURSOR_WORD_END_PREV] = {
361 "vis-motion-word-end-prev",
362 VIS_HELP("Move cursor backwards to the end of word")
363 movement, { .i = VIS_MOVE_WORD_END_PREV }
365 [VIS_ACTION_CURSOR_WORD_END_NEXT] = {
366 "vis-motion-word-end-next",
367 VIS_HELP("Move cursor forward to the end of word")
368 movement, { .i = VIS_MOVE_WORD_END_NEXT }
370 [VIS_ACTION_CURSOR_LONGWORD_START_PREV] = {
371 "vis-motion-bigword-start-prev",
372 VIS_HELP("Move cursor WORDS backwards")
373 movement, { .i = VIS_MOVE_LONGWORD_START_PREV }
375 [VIS_ACTION_CURSOR_LONGWORD_START_NEXT] = {
376 "vis-motion-bigword-start-next",
377 VIS_HELP("Move cursor WORDS forwards")
378 movement, { .i = VIS_MOVE_LONGWORD_START_NEXT }
380 [VIS_ACTION_CURSOR_LONGWORD_END_PREV] = {
381 "vis-motion-bigword-end-prev",
382 VIS_HELP("Move cursor backwards to the end of WORD")
383 movement, { .i = VIS_MOVE_LONGWORD_END_PREV }
385 [VIS_ACTION_CURSOR_LONGWORD_END_NEXT] = {
386 "vis-motion-bigword-end-next",
387 VIS_HELP("Move cursor forward to the end of WORD")
388 movement, { .i = VIS_MOVE_LONGWORD_END_NEXT }
390 [VIS_ACTION_CURSOR_LINE_UP] = {
391 "vis-motion-line-up",
392 VIS_HELP("Move cursor line upwards")
393 movement, { .i = VIS_MOVE_LINE_UP }
395 [VIS_ACTION_CURSOR_LINE_DOWN] = {
396 "vis-motion-line-down",
397 VIS_HELP("Move cursor line downwards")
398 movement, { .i = VIS_MOVE_LINE_DOWN }
400 [VIS_ACTION_CURSOR_LINE_START] = {
401 "vis-motion-line-start",
402 VIS_HELP("Move cursor to first non-blank character of the line")
403 movement, { .i = VIS_MOVE_LINE_START }
405 [VIS_ACTION_CURSOR_LINE_FINISH] = {
406 "vis-motion-line-finish",
407 VIS_HELP("Move cursor to last non-blank character of the line")
408 movement, { .i = VIS_MOVE_LINE_FINISH }
410 [VIS_ACTION_CURSOR_LINE_BEGIN] = {
411 "vis-motion-line-begin",
412 VIS_HELP("Move cursor to first character of the line")
413 movement, { .i = VIS_MOVE_LINE_BEGIN }
415 [VIS_ACTION_CURSOR_LINE_END] = {
416 "vis-motion-line-end",
417 VIS_HELP("Move cursor to end of the line")
418 movement, { .i = VIS_MOVE_LINE_END }
420 [VIS_ACTION_CURSOR_SCREEN_LINE_UP] = {
421 "vis-motion-screenline-up",
422 VIS_HELP("Move cursor screen/display line upwards")
423 movement, { .i = VIS_MOVE_SCREEN_LINE_UP }
425 [VIS_ACTION_CURSOR_SCREEN_LINE_DOWN] = {
426 "vis-motion-screenline-down",
427 VIS_HELP("Move cursor screen/display line downwards")
428 movement, { .i = VIS_MOVE_SCREEN_LINE_DOWN }
430 [VIS_ACTION_CURSOR_SCREEN_LINE_BEGIN] = {
431 "vis-motion-screenline-begin",
432 VIS_HELP("Move cursor to beginning of screen/display line")
433 movement, { .i = VIS_MOVE_SCREEN_LINE_BEGIN }
435 [VIS_ACTION_CURSOR_SCREEN_LINE_MIDDLE] = {
436 "vis-motion-screenline-middle",
437 VIS_HELP("Move cursor to middle of screen/display line")
438 movement, { .i = VIS_MOVE_SCREEN_LINE_MIDDLE }
440 [VIS_ACTION_CURSOR_SCREEN_LINE_END] = {
441 "vis-motion-screenline-end",
442 VIS_HELP("Move cursor to end of screen/display line")
443 movement, { .i = VIS_MOVE_SCREEN_LINE_END }
445 [VIS_ACTION_CURSOR_PERCENT] = {
446 "vis-motion-percent",
447 VIS_HELP("Move to count % of file or matching item")
448 percent
450 [VIS_ACTION_CURSOR_BYTE] = {
451 "vis-motion-byte",
452 VIS_HELP("Move to absolute byte position")
453 movement, { .i = VIS_MOVE_BYTE }
455 [VIS_ACTION_CURSOR_BYTE_LEFT] = {
456 "vis-motion-byte-left",
457 VIS_HELP("Move count bytes to the left")
458 movement, { .i = VIS_MOVE_BYTE_LEFT }
460 [VIS_ACTION_CURSOR_BYTE_RIGHT] = {
461 "vis-motion-byte-right",
462 VIS_HELP("Move count bytes to the right")
463 movement, { .i = VIS_MOVE_BYTE_RIGHT }
465 [VIS_ACTION_CURSOR_PARAGRAPH_PREV] = {
466 "vis-motion-paragraph-prev",
467 VIS_HELP("Move cursor paragraph backward")
468 movement, { .i = VIS_MOVE_PARAGRAPH_PREV }
470 [VIS_ACTION_CURSOR_PARAGRAPH_NEXT] = {
471 "vis-motion-paragraph-next",
472 VIS_HELP("Move cursor paragraph forward")
473 movement, { .i = VIS_MOVE_PARAGRAPH_NEXT }
475 [VIS_ACTION_CURSOR_SENTENCE_PREV] = {
476 "vis-motion-sentence-prev",
477 VIS_HELP("Move cursor sentence backward")
478 movement, { .i = VIS_MOVE_SENTENCE_PREV }
480 [VIS_ACTION_CURSOR_SENTENCE_NEXT] = {
481 "vis-motion-sentence-next",
482 VIS_HELP("Move cursor sentence forward")
483 movement, { .i = VIS_MOVE_SENTENCE_NEXT }
485 [VIS_ACTION_CURSOR_BLOCK_START] = {
486 "vis-motion-block-start",
487 VIS_HELP("Move cursor to the opening curly brace in a block")
488 movement, { .i = VIS_MOVE_BLOCK_START }
490 [VIS_ACTION_CURSOR_BLOCK_END] = {
491 "vis-motion-block-end",
492 VIS_HELP("Move cursor to the closing curly brace in a block")
493 movement, { .i = VIS_MOVE_BLOCK_END }
495 [VIS_ACTION_CURSOR_PARENTHESIS_START] = {
496 "vis-motion-parenthesis-start",
497 VIS_HELP("Move cursor to the opening parenthesis inside a pair of parentheses")
498 movement, { .i = VIS_MOVE_PARENTHESIS_START }
500 [VIS_ACTION_CURSOR_PARENTHESIS_END] = {
501 "vis-motion-parenthesis-end",
502 VIS_HELP("Move cursor to the closing parenthesis inside a pair of parentheses")
503 movement, { .i = VIS_MOVE_PARENTHESIS_END }
505 [VIS_ACTION_CURSOR_COLUMN] = {
506 "vis-motion-column",
507 VIS_HELP("Move cursor to given column of current line")
508 movement, { .i = VIS_MOVE_COLUMN }
510 [VIS_ACTION_CURSOR_LINE_FIRST] = {
511 "vis-motion-line-first",
512 VIS_HELP("Move cursor to given line (defaults to first)")
513 gotoline, { .i = -1 }
515 [VIS_ACTION_CURSOR_LINE_LAST] = {
516 "vis-motion-line-last",
517 VIS_HELP("Move cursor to given line (defaults to last)")
518 gotoline, { .i = +1 }
520 [VIS_ACTION_CURSOR_WINDOW_LINE_TOP] = {
521 "vis-motion-window-line-top",
522 VIS_HELP("Move cursor to top line of the window")
523 movement, { .i = VIS_MOVE_WINDOW_LINE_TOP }
525 [VIS_ACTION_CURSOR_WINDOW_LINE_MIDDLE] = {
526 "vis-motion-window-line-middle",
527 VIS_HELP("Move cursor to middle line of the window")
528 movement, { .i = VIS_MOVE_WINDOW_LINE_MIDDLE }
530 [VIS_ACTION_CURSOR_WINDOW_LINE_BOTTOM] = {
531 "vis-motion-window-line-bottom",
532 VIS_HELP("Move cursor to bottom line of the window")
533 movement, { .i = VIS_MOVE_WINDOW_LINE_BOTTOM }
535 [VIS_ACTION_CURSOR_SEARCH_REPEAT_FORWARD] = {
536 "vis-motion-search-repeat-forward",
537 VIS_HELP("Move cursor to next match in forward direction")
538 movement, { .i = VIS_MOVE_SEARCH_REPEAT_FORWARD }
540 [VIS_ACTION_CURSOR_SEARCH_REPEAT_BACKWARD] = {
541 "vis-motion-search-repeat-backward",
542 VIS_HELP("Move cursor to previous match in backward direction")
543 movement, { .i = VIS_MOVE_SEARCH_REPEAT_BACKWARD }
545 [VIS_ACTION_CURSOR_SEARCH_REPEAT] = {
546 "vis-motion-search-repeat",
547 VIS_HELP("Move cursor to next match")
548 movement, { .i = VIS_MOVE_SEARCH_REPEAT }
550 [VIS_ACTION_CURSOR_SEARCH_REPEAT_REVERSE] = {
551 "vis-motion-search-repeat-reverse",
552 VIS_HELP("Move cursor to next match in opposite direction")
553 movement, { .i = VIS_MOVE_SEARCH_REPEAT_REVERSE }
555 [VIS_ACTION_CURSOR_SEARCH_WORD_FORWARD] = {
556 "vis-motion-search-word-forward",
557 VIS_HELP("Move cursor to next occurrence of the word under cursor")
558 movement, { .i = VIS_MOVE_SEARCH_WORD_FORWARD }
560 [VIS_ACTION_CURSOR_SEARCH_WORD_BACKWARD] = {
561 "vis-motion-search-word-backward",
562 VIS_HELP("Move cursor to previous occurrence of the word under cursor")
563 movement, { .i = VIS_MOVE_SEARCH_WORD_BACKWARD }
565 [VIS_ACTION_WINDOW_PAGE_UP] = {
566 "vis-window-page-up",
567 VIS_HELP("Scroll window pages backwards (upwards)")
568 wscroll, { .i = -PAGE }
570 [VIS_ACTION_WINDOW_HALFPAGE_UP] = {
571 "vis-window-halfpage-up",
572 VIS_HELP("Scroll window half pages backwards (upwards)")
573 wscroll, { .i = -PAGE_HALF }
575 [VIS_ACTION_WINDOW_PAGE_DOWN] = {
576 "vis-window-page-down",
577 VIS_HELP("Scroll window pages forwards (downwards)")
578 wscroll, { .i = +PAGE }
580 [VIS_ACTION_WINDOW_HALFPAGE_DOWN] = {
581 "vis-window-halfpage-down",
582 VIS_HELP("Scroll window half pages forwards (downwards)")
583 wscroll, { .i = +PAGE_HALF }
585 [VIS_ACTION_MODE_NORMAL] = {
586 "vis-mode-normal",
587 VIS_HELP("Enter normal mode")
588 switchmode, { .i = VIS_MODE_NORMAL }
590 [VIS_ACTION_MODE_NORMAL_ESCAPE] = {
591 "vis-mode-normal-escape",
592 VIS_HELP("Reset count or remove all non-primary selections")
593 normalmode_escape,
595 [VIS_ACTION_MODE_VISUAL] = {
596 "vis-mode-visual-charwise",
597 VIS_HELP("Enter characterwise visual mode")
598 switchmode, { .i = VIS_MODE_VISUAL }
600 [VIS_ACTION_MODE_VISUAL_ESCAPE] = {
601 "vis-mode-visual-escape",
602 VIS_HELP("Reset count or switch to normal mode")
603 visualmode_escape,
605 [VIS_ACTION_MODE_VISUAL_LINE] = {
606 "vis-mode-visual-linewise",
607 VIS_HELP("Enter linewise visual mode")
608 switchmode, { .i = VIS_MODE_VISUAL_LINE }
610 [VIS_ACTION_MODE_INSERT] = {
611 "vis-mode-insert",
612 VIS_HELP("Enter insert mode")
613 insertmode, { .i = VIS_MOVE_NOP }
615 [VIS_ACTION_MODE_REPLACE] = {
616 "vis-mode-replace",
617 VIS_HELP("Enter replace mode")
618 replacemode, { .i = VIS_MOVE_NOP }
620 [VIS_ACTION_DELETE_CHAR_PREV] = {
621 "vis-delete-char-prev",
622 VIS_HELP("Delete the previous character")
623 delete, { .i = VIS_MOVE_CHAR_PREV }
625 [VIS_ACTION_DELETE_CHAR_NEXT] = {
626 "vis-delete-char-next",
627 VIS_HELP("Delete the next character")
628 delete, { .i = VIS_MOVE_CHAR_NEXT }
630 [VIS_ACTION_DELETE_LINE_BEGIN] = {
631 "vis-delete-line-begin",
632 VIS_HELP("Delete until the start of the current line")
633 delete, { .i = VIS_MOVE_LINE_BEGIN }
635 [VIS_ACTION_DELETE_WORD_PREV] = {
636 "vis-delete-word-prev",
637 VIS_HELP("Delete the previous WORD")
638 delete, { .i = VIS_MOVE_WORD_START_PREV }
640 [VIS_ACTION_JUMPLIST_PREV] = {
641 "vis-jumplist-prev",
642 VIS_HELP("Go to older cursor position in jump list")
643 jumplist, { .i = -1 }
645 [VIS_ACTION_JUMPLIST_NEXT] = {
646 "vis-jumplist-next",
647 VIS_HELP("Go to newer cursor position in jump list")
648 jumplist, { .i = +1 }
650 [VIS_ACTION_JUMPLIST_SAVE] = {
651 "vis-jumplist-save",
652 VIS_HELP("Save current selections in jump list")
653 jumplist, { .i = 0 }
655 [VIS_ACTION_UNDO] = {
656 "vis-undo",
657 VIS_HELP("Undo last change")
658 undo,
660 [VIS_ACTION_REDO] = {
661 "vis-redo",
662 VIS_HELP("Redo last change")
663 redo,
665 [VIS_ACTION_EARLIER] = {
666 "vis-earlier",
667 VIS_HELP("Goto older text state")
668 earlier,
670 [VIS_ACTION_LATER] = {
671 "vis-later",
672 VIS_HELP("Goto newer text state")
673 later,
675 [VIS_ACTION_MACRO_RECORD] = {
676 "vis-macro-record",
677 VIS_HELP("Record macro into given register")
678 macro_record,
680 [VIS_ACTION_MACRO_REPLAY] = {
681 "vis-macro-replay",
682 VIS_HELP("Replay macro, execute the content of the given register")
683 macro_replay,
685 [VIS_ACTION_MARK] = {
686 "vis-mark",
687 VIS_HELP("Use given mark for next action")
688 mark,
690 [VIS_ACTION_REDRAW] = {
691 "vis-redraw",
692 VIS_HELP("Redraw current editor content")
693 call, { .f = vis_redraw }
695 [VIS_ACTION_REPLACE_CHAR] = {
696 "vis-replace-char",
697 VIS_HELP("Replace the character under the cursor")
698 replace,
700 [VIS_ACTION_TOTILL_REPEAT] = {
701 "vis-motion-totill-repeat",
702 VIS_HELP("Repeat latest to/till motion")
703 movement, { .i = VIS_MOVE_TOTILL_REPEAT }
705 [VIS_ACTION_TOTILL_REVERSE] = {
706 "vis-motion-totill-reverse",
707 VIS_HELP("Repeat latest to/till motion but in opposite direction")
708 movement, { .i = VIS_MOVE_TOTILL_REVERSE }
710 [VIS_ACTION_PROMPT_SEARCH_FORWARD] = {
711 "vis-search-forward",
712 VIS_HELP("Search forward")
713 prompt_show, { .s = "/" }
715 [VIS_ACTION_PROMPT_SEARCH_BACKWARD] = {
716 "vis-search-backward",
717 VIS_HELP("Search backward")
718 prompt_show, { .s = "?" }
720 [VIS_ACTION_TILL_LEFT] = {
721 "vis-motion-till-left",
722 VIS_HELP("Till after the occurrence of character to the left")
723 movement_key, { .i = VIS_MOVE_LEFT_TILL }
725 [VIS_ACTION_TILL_RIGHT] = {
726 "vis-motion-till-right",
727 VIS_HELP("Till before the occurrence of character to the right")
728 movement_key, { .i = VIS_MOVE_RIGHT_TILL }
730 [VIS_ACTION_TO_LEFT] = {
731 "vis-motion-to-left",
732 VIS_HELP("To the first occurrence of character to the left")
733 movement_key, { .i = VIS_MOVE_LEFT_TO }
735 [VIS_ACTION_TO_RIGHT] = {
736 "vis-motion-to-right",
737 VIS_HELP("To the first occurrence of character to the right")
738 movement_key, { .i = VIS_MOVE_RIGHT_TO }
740 [VIS_ACTION_REGISTER] = {
741 "vis-register",
742 VIS_HELP("Use given register for next operator")
743 reg,
745 [VIS_ACTION_OPERATOR_CHANGE] = {
746 "vis-operator-change",
747 VIS_HELP("Change operator")
748 operator, { .i = VIS_OP_CHANGE }
750 [VIS_ACTION_OPERATOR_DELETE] = {
751 "vis-operator-delete",
752 VIS_HELP("Delete operator")
753 operator, { .i = VIS_OP_DELETE }
755 [VIS_ACTION_OPERATOR_YANK] = {
756 "vis-operator-yank",
757 VIS_HELP("Yank operator")
758 operator, { .i = VIS_OP_YANK }
760 [VIS_ACTION_OPERATOR_SHIFT_LEFT] = {
761 "vis-operator-shift-left",
762 VIS_HELP("Shift left operator")
763 operator, { .i = VIS_OP_SHIFT_LEFT }
765 [VIS_ACTION_OPERATOR_SHIFT_RIGHT] = {
766 "vis-operator-shift-right",
767 VIS_HELP("Shift right operator")
768 operator, { .i = VIS_OP_SHIFT_RIGHT }
770 [VIS_ACTION_COUNT] = {
771 "vis-count",
772 VIS_HELP("Count specifier")
773 count,
775 [VIS_ACTION_INSERT_NEWLINE] = {
776 "vis-insert-newline",
777 VIS_HELP("Insert a line break (depending on file type)")
778 call, { .f = vis_insert_nl }
780 [VIS_ACTION_INSERT_TAB] = {
781 "vis-insert-tab",
782 VIS_HELP("Insert a tab (might be converted to spaces)")
783 call, { .f = vis_insert_tab }
785 [VIS_ACTION_INSERT_VERBATIM] = {
786 "vis-insert-verbatim",
787 VIS_HELP("Insert Unicode character based on code point")
788 insert_verbatim,
790 [VIS_ACTION_INSERT_REGISTER] = {
791 "vis-insert-register",
792 VIS_HELP("Insert specified register content")
793 insert_register,
795 [VIS_ACTION_WINDOW_NEXT] = {
796 "vis-window-next",
797 VIS_HELP("Focus next window")
798 call, { .f = vis_window_next }
800 [VIS_ACTION_WINDOW_PREV] = {
801 "vis-window-prev",
802 VIS_HELP("Focus previous window")
803 call, { .f = vis_window_prev }
805 [VIS_ACTION_APPEND_CHAR_NEXT] = {
806 "vis-append-char-next",
807 VIS_HELP("Append text after the cursor")
808 insertmode, { .i = VIS_MOVE_LINE_CHAR_NEXT }
810 [VIS_ACTION_APPEND_LINE_END] = {
811 "vis-append-line-end",
812 VIS_HELP("Append text after the end of the line")
813 insertmode, { .i = VIS_MOVE_LINE_END },
815 [VIS_ACTION_INSERT_LINE_START] = {
816 "vis-insert-line-start",
817 VIS_HELP("Insert text before the first non-blank in the line")
818 insertmode, { .i = VIS_MOVE_LINE_START },
820 [VIS_ACTION_OPEN_LINE_ABOVE] = {
821 "vis-open-line-above",
822 VIS_HELP("Begin a new line above the cursor")
823 openline, { .i = -1 }
825 [VIS_ACTION_OPEN_LINE_BELOW] = {
826 "vis-open-line-below",
827 VIS_HELP("Begin a new line below the cursor")
828 openline, { .i = +1 }
830 [VIS_ACTION_JOIN_LINES] = {
831 "vis-join-lines",
832 VIS_HELP("Join selected lines")
833 join, { .s = " " }
835 [VIS_ACTION_JOIN_LINES_TRIM] = {
836 "vis-join-lines-trim",
837 VIS_HELP("Join selected lines, remove white space")
838 join, { .s = "" }
840 [VIS_ACTION_PROMPT_SHOW] = {
841 "vis-prompt-show",
842 VIS_HELP("Show editor command line prompt")
843 prompt_show, { .s = ":" }
845 [VIS_ACTION_REPEAT] = {
846 "vis-repeat",
847 VIS_HELP("Repeat latest editor command")
848 repeat
850 [VIS_ACTION_SELECTION_FLIP] = {
851 "vis-selection-flip",
852 VIS_HELP("Flip selection, move cursor to other end")
853 selection_end,
855 [VIS_ACTION_WINDOW_REDRAW_TOP] = {
856 "vis-window-redraw-top",
857 VIS_HELP("Redraw cursor line at the top of the window")
858 window, { .w = view_redraw_top }
860 [VIS_ACTION_WINDOW_REDRAW_CENTER] = {
861 "vis-window-redraw-center",
862 VIS_HELP("Redraw cursor line at the center of the window")
863 window, { .w = view_redraw_center }
865 [VIS_ACTION_WINDOW_REDRAW_BOTTOM] = {
866 "vis-window-redraw-bottom",
867 VIS_HELP("Redraw cursor line at the bottom of the window")
868 window, { .w = view_redraw_bottom }
870 [VIS_ACTION_WINDOW_SLIDE_UP] = {
871 "vis-window-slide-up",
872 VIS_HELP("Slide window content upwards")
873 wslide, { .i = -1 }
875 [VIS_ACTION_WINDOW_SLIDE_DOWN] = {
876 "vis-window-slide-down",
877 VIS_HELP("Slide window content downwards")
878 wslide, { .i = +1 }
880 [VIS_ACTION_PUT_AFTER] = {
881 "vis-put-after",
882 VIS_HELP("Put text after the cursor")
883 operator, { .i = VIS_OP_PUT_AFTER }
885 [VIS_ACTION_PUT_BEFORE] = {
886 "vis-put-before",
887 VIS_HELP("Put text before the cursor")
888 operator, { .i = VIS_OP_PUT_BEFORE }
890 [VIS_ACTION_SELECTIONS_NEW_LINE_ABOVE] = {
891 "vis-selection-new-lines-above",
892 VIS_HELP("Create a new selection on the line above")
893 selections_new, { .i = -1 }
895 [VIS_ACTION_SELECTIONS_NEW_LINE_ABOVE_FIRST] = {
896 "vis-selection-new-lines-above-first",
897 VIS_HELP("Create a new selection on the line above the first selection")
898 selections_new, { .i = INT_MIN }
900 [VIS_ACTION_SELECTIONS_NEW_LINE_BELOW] = {
901 "vis-selection-new-lines-below",
902 VIS_HELP("Create a new selection on the line below")
903 selections_new, { .i = +1 }
905 [VIS_ACTION_SELECTIONS_NEW_LINE_BELOW_LAST] = {
906 "vis-selection-new-lines-below-last",
907 VIS_HELP("Create a new selection on the line below the last selection")
908 selections_new, { .i = INT_MAX }
910 [VIS_ACTION_SELECTIONS_NEW_LINES_BEGIN] = {
911 "vis-selection-new-lines-begin",
912 VIS_HELP("Create a new selection at the start of every line covered by selection")
913 operator, { .i = VIS_OP_CURSOR_SOL }
915 [VIS_ACTION_SELECTIONS_NEW_LINES_END] = {
916 "vis-selection-new-lines-end",
917 VIS_HELP("Create a new selection at the end of every line covered by selection")
918 operator, { .i = VIS_OP_CURSOR_EOL }
920 [VIS_ACTION_SELECTIONS_NEW_MATCH_NEXT] = {
921 "vis-selection-new-match-next",
922 VIS_HELP("Select the next region matching the current selection")
923 selections_match_next,
925 [VIS_ACTION_SELECTIONS_NEW_MATCH_SKIP] = {
926 "vis-selection-new-match-skip",
927 VIS_HELP("Clear current selection, but select next match")
928 selections_match_skip,
930 [VIS_ACTION_SELECTIONS_ALIGN] = {
931 "vis-selections-align",
932 VIS_HELP("Try to align all selections on the same column")
933 selections_align,
935 [VIS_ACTION_SELECTIONS_ALIGN_INDENT_LEFT] = {
936 "vis-selections-align-indent-left",
937 VIS_HELP("Left-align all selections by inserting spaces")
938 selections_align_indent, { .i = -1 }
940 [VIS_ACTION_SELECTIONS_ALIGN_INDENT_RIGHT] = {
941 "vis-selections-align-indent-right",
942 VIS_HELP("Right-align all selections by inserting spaces")
943 selections_align_indent, { .i = +1 }
945 [VIS_ACTION_SELECTIONS_REMOVE_ALL] = {
946 "vis-selections-remove-all",
947 VIS_HELP("Remove all but the primary selection")
948 selections_clear,
950 [VIS_ACTION_SELECTIONS_REMOVE_LAST] = {
951 "vis-selections-remove-last",
952 VIS_HELP("Remove primary selection")
953 selections_remove,
955 [VIS_ACTION_SELECTIONS_REMOVE_COLUMN] = {
956 "vis-selections-remove-column",
957 VIS_HELP("Remove count selection column")
958 selections_remove_column, { .i = 1 }
960 [VIS_ACTION_SELECTIONS_REMOVE_COLUMN_EXCEPT] = {
961 "vis-selections-remove-column-except",
962 VIS_HELP("Remove all but the count selection column")
963 selections_remove_column_except, { .i = 1 }
965 [VIS_ACTION_SELECTIONS_PREV] = {
966 "vis-selection-prev",
967 VIS_HELP("Move to the previous selection")
968 selections_navigate, { .i = -PAGE_HALF }
970 [VIS_ACTION_SELECTIONS_NEXT] = {
971 "vis-selection-next",
972 VIS_HELP("Move to the next selection")
973 selections_navigate, { .i = +PAGE_HALF }
975 [VIS_ACTION_SELECTIONS_ROTATE_LEFT] = {
976 "vis-selections-rotate-left",
977 VIS_HELP("Rotate selections left")
978 selections_rotate, { .i = -1 }
980 [VIS_ACTION_SELECTIONS_ROTATE_RIGHT] = {
981 "vis-selections-rotate-right",
982 VIS_HELP("Rotate selections right")
983 selections_rotate, { .i = +1 }
985 [VIS_ACTION_SELECTIONS_TRIM] = {
986 "vis-selections-trim",
987 VIS_HELP("Remove leading and trailing white space from selections")
988 selections_trim
990 [VIS_ACTION_SELECTIONS_SAVE] = {
991 "vis-selections-save",
992 VIS_HELP("Save currently active selections to mark")
993 selections_save
995 [VIS_ACTION_SELECTIONS_RESTORE] = {
996 "vis-selections-restore",
997 VIS_HELP("Restore selections from mark")
998 selections_restore
1000 [VIS_ACTION_SELECTIONS_UNION] = {
1001 "vis-selections-union",
1002 VIS_HELP("Add selections from mark")
1003 selections_union
1005 [VIS_ACTION_SELECTIONS_INTERSECT] = {
1006 "vis-selections-intersect",
1007 VIS_HELP("Intersect with selections from mark")
1008 selections_intersect
1010 [VIS_ACTION_SELECTIONS_COMPLEMENT] = {
1011 "vis-selections-complement",
1012 VIS_HELP("Complement selections")
1013 selections_complement
1015 [VIS_ACTION_SELECTIONS_MINUS] = {
1016 "vis-selections-minus",
1017 VIS_HELP("Subtract selections from mark")
1018 selections_minus
1020 [VIS_ACTION_TEXT_OBJECT_WORD_OUTER] = {
1021 "vis-textobject-word-outer",
1022 VIS_HELP("A word leading and trailing whitespace included")
1023 textobj, { .i = VIS_TEXTOBJECT_OUTER_WORD }
1025 [VIS_ACTION_TEXT_OBJECT_WORD_INNER] = {
1026 "vis-textobject-word-inner",
1027 VIS_HELP("A word leading and trailing whitespace excluded")
1028 textobj, { .i = VIS_TEXTOBJECT_INNER_WORD }
1030 [VIS_ACTION_TEXT_OBJECT_LONGWORD_OUTER] = {
1031 "vis-textobject-bigword-outer",
1032 VIS_HELP("A WORD leading and trailing whitespace included")
1033 textobj, { .i = VIS_TEXTOBJECT_OUTER_LONGWORD }
1035 [VIS_ACTION_TEXT_OBJECT_LONGWORD_INNER] = {
1036 "vis-textobject-bigword-inner",
1037 VIS_HELP("A WORD leading and trailing whitespace excluded")
1038 textobj, { .i = VIS_TEXTOBJECT_INNER_LONGWORD }
1040 [VIS_ACTION_TEXT_OBJECT_SENTENCE] = {
1041 "vis-textobject-sentence",
1042 VIS_HELP("A sentence")
1043 textobj, { .i = VIS_TEXTOBJECT_SENTENCE }
1045 [VIS_ACTION_TEXT_OBJECT_PARAGRAPH] = {
1046 "vis-textobject-paragraph",
1047 VIS_HELP("A paragraph")
1048 textobj, { .i = VIS_TEXTOBJECT_PARAGRAPH }
1050 [VIS_ACTION_TEXT_OBJECT_PARAGRAPH_OUTER] = {
1051 "vis-textobject-paragraph-outer",
1052 VIS_HELP("A paragraph (outer variant)")
1053 textobj, { .i = VIS_TEXTOBJECT_PARAGRAPH_OUTER }
1055 [VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_OUTER] = {
1056 "vis-textobject-square-bracket-outer",
1057 VIS_HELP("[] block (outer variant)")
1058 textobj, { .i = VIS_TEXTOBJECT_OUTER_SQUARE_BRACKET }
1060 [VIS_ACTION_TEXT_OBJECT_SQUARE_BRACKET_INNER] = {
1061 "vis-textobject-square-bracket-inner",
1062 VIS_HELP("[] block (inner variant)")
1063 textobj, { .i = VIS_TEXTOBJECT_INNER_SQUARE_BRACKET }
1065 [VIS_ACTION_TEXT_OBJECT_PARENTHESIS_OUTER] = {
1066 "vis-textobject-parenthesis-outer",
1067 VIS_HELP("() block (outer variant)")
1068 textobj, { .i = VIS_TEXTOBJECT_OUTER_PARENTHESIS }
1070 [VIS_ACTION_TEXT_OBJECT_PARENTHESIS_INNER] = {
1071 "vis-textobject-parenthesis-inner",
1072 VIS_HELP("() block (inner variant)")
1073 textobj, { .i = VIS_TEXTOBJECT_INNER_PARENTHESIS }
1075 [VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_OUTER] = {
1076 "vis-textobject-angle-bracket-outer",
1077 VIS_HELP("<> block (outer variant)")
1078 textobj, { .i = VIS_TEXTOBJECT_OUTER_ANGLE_BRACKET }
1080 [VIS_ACTION_TEXT_OBJECT_ANGLE_BRACKET_INNER] = {
1081 "vis-textobject-angle-bracket-inner",
1082 VIS_HELP("<> block (inner variant)")
1083 textobj, { .i = VIS_TEXTOBJECT_INNER_ANGLE_BRACKET }
1085 [VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_OUTER] = {
1086 "vis-textobject-curly-bracket-outer",
1087 VIS_HELP("{} block (outer variant)")
1088 textobj, { .i = VIS_TEXTOBJECT_OUTER_CURLY_BRACKET }
1090 [VIS_ACTION_TEXT_OBJECT_CURLY_BRACKET_INNER] = {
1091 "vis-textobject-curly-bracket-inner",
1092 VIS_HELP("{} block (inner variant)")
1093 textobj, { .i = VIS_TEXTOBJECT_INNER_CURLY_BRACKET }
1095 [VIS_ACTION_TEXT_OBJECT_QUOTE_OUTER] = {
1096 "vis-textobject-quote-outer",
1097 VIS_HELP("A quoted string, including the quotation marks")
1098 textobj, { .i = VIS_TEXTOBJECT_OUTER_QUOTE }
1100 [VIS_ACTION_TEXT_OBJECT_QUOTE_INNER] = {
1101 "vis-textobject-quote-inner",
1102 VIS_HELP("A quoted string, excluding the quotation marks")
1103 textobj, { .i = VIS_TEXTOBJECT_INNER_QUOTE }
1105 [VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_OUTER] = {
1106 "vis-textobject-single-quote-outer",
1107 VIS_HELP("A single quoted string, including the quotation marks")
1108 textobj, { .i = VIS_TEXTOBJECT_OUTER_SINGLE_QUOTE }
1110 [VIS_ACTION_TEXT_OBJECT_SINGLE_QUOTE_INNER] = {
1111 "vis-textobject-single-quote-inner",
1112 VIS_HELP("A single quoted string, excluding the quotation marks")
1113 textobj, { .i = VIS_TEXTOBJECT_INNER_SINGLE_QUOTE }
1115 [VIS_ACTION_TEXT_OBJECT_BACKTICK_OUTER] = {
1116 "vis-textobject-backtick-outer",
1117 VIS_HELP("A backtick delimited string (outer variant)")
1118 textobj, { .i = VIS_TEXTOBJECT_OUTER_BACKTICK }
1120 [VIS_ACTION_TEXT_OBJECT_BACKTICK_INNER] = {
1121 "vis-textobject-backtick-inner",
1122 VIS_HELP("A backtick delimited string (inner variant)")
1123 textobj, { .i = VIS_TEXTOBJECT_INNER_BACKTICK }
1125 [VIS_ACTION_TEXT_OBJECT_LINE_OUTER] = {
1126 "vis-textobject-line-outer",
1127 VIS_HELP("The whole line")
1128 textobj, { .i = VIS_TEXTOBJECT_OUTER_LINE }
1130 [VIS_ACTION_TEXT_OBJECT_LINE_INNER] = {
1131 "vis-textobject-line-inner",
1132 VIS_HELP("The whole line, excluding leading and trailing whitespace")
1133 textobj, { .i = VIS_TEXTOBJECT_INNER_LINE }
1135 [VIS_ACTION_TEXT_OBJECT_INDENTATION] = {
1136 "vis-textobject-indentation",
1137 VIS_HELP("All adjacent lines with the same indentation level as the current one")
1138 textobj, { .i = VIS_TEXTOBJECT_INDENTATION }
1140 [VIS_ACTION_TEXT_OBJECT_SEARCH_FORWARD] = {
1141 "vis-textobject-search-forward",
1142 VIS_HELP("The next search match in forward direction")
1143 textobj, { .i = VIS_TEXTOBJECT_SEARCH_FORWARD }
1145 [VIS_ACTION_TEXT_OBJECT_SEARCH_BACKWARD] = {
1146 "vis-textobject-search-backward",
1147 VIS_HELP("The next search match in backward direction")
1148 textobj, { .i = VIS_TEXTOBJECT_SEARCH_BACKWARD }
1150 [VIS_ACTION_UNICODE_INFO] = {
1151 "vis-unicode-info",
1152 VIS_HELP("Show Unicode codepoint(s) of character under cursor")
1153 unicode_info, { .i = VIS_ACTION_UNICODE_INFO }
1155 [VIS_ACTION_UTF8_INFO] = {
1156 "vis-utf8-info",
1157 VIS_HELP("Show UTF-8 encoded codepoint(s) of character under cursor")
1158 unicode_info, { .i = VIS_ACTION_UTF8_INFO }
1160 [VIS_ACTION_NOP] = {
1161 "vis-nop",
1162 VIS_HELP("Ignore key, do nothing")
1163 nop,
1167 #include "config.h"
1169 /** key bindings functions */
1171 static const char *nop(Vis *vis, const char *keys, const Arg *arg) {
1172 return keys;
1175 static const char *macro_record(Vis *vis, const char *keys, const Arg *arg) {
1176 if (!vis_macro_record_stop(vis)) {
1177 if (!keys[0])
1178 return NULL;
1179 const char *next = vis_keys_next(vis, keys);
1180 if (next - keys > 1)
1181 return next;
1182 enum VisRegister reg = vis_register_from(vis, keys[0]);
1183 vis_macro_record(vis, reg);
1184 keys++;
1186 vis_draw(vis);
1187 return keys;
1190 static const char *macro_replay(Vis *vis, const char *keys, const Arg *arg) {
1191 if (!keys[0])
1192 return NULL;
1193 const char *next = vis_keys_next(vis, keys);
1194 if (next - keys > 1)
1195 return next;
1196 enum VisRegister reg = vis_register_from(vis, keys[0]);
1197 vis_macro_replay(vis, reg);
1198 return keys+1;
1201 static const char *suspend(Vis *vis, const char *keys, const Arg *arg) {
1202 vis_suspend(vis);
1203 return keys;
1206 static const char *repeat(Vis *vis, const char *keys, const Arg *arg) {
1207 vis_repeat(vis);
1208 return keys;
1211 static const char *selections_new(Vis *vis, const char *keys, const Arg *arg) {
1212 View *view = vis_view(vis);
1213 bool anchored = view_selections_anchored(view_selections_primary_get(view));
1214 VisCountIterator it = vis_count_iterator_get(vis, 1);
1215 while (vis_count_iterator_next(&it)) {
1216 Selection *sel = NULL;
1217 switch (arg->i) {
1218 case -1:
1219 case +1:
1220 sel = view_selections_primary_get(view);
1221 break;
1222 case INT_MIN:
1223 sel = view_selections(view);
1224 break;
1225 case INT_MAX:
1226 for (Selection *s = view_selections(view); s; s = view_selections_next(s))
1227 sel = s;
1228 break;
1231 if (!sel)
1232 return keys;
1234 size_t oldpos = view_cursors_pos(sel);
1235 if (arg->i > 0)
1236 view_line_down(sel);
1237 else if (arg->i < 0)
1238 view_line_up(sel);
1239 size_t newpos = view_cursors_pos(sel);
1240 view_cursors_to(sel, oldpos);
1241 Selection *sel_new = view_selections_new(view, newpos);
1242 if (!sel_new) {
1243 if (arg->i == -1)
1244 sel_new = view_selections_prev(sel);
1245 else if (arg->i == +1)
1246 sel_new = view_selections_next(sel);
1248 if (sel_new) {
1249 view_selections_primary_set(sel_new);
1250 view_selections_anchor(sel_new, anchored);
1253 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1254 return keys;
1257 static const char *selections_align(Vis *vis, const char *keys, const Arg *arg) {
1258 View *view = vis_view(vis);
1259 Text *txt = vis_text(vis);
1260 int mincol = INT_MAX;
1261 for (Selection *s = view_selections(view); s; s = view_selections_next(s)) {
1262 int col = view_cursors_cell_get(s);
1263 if (col >= 0 && col < mincol)
1264 mincol = col;
1266 for (Selection *s = view_selections(view); s; s = view_selections_next(s)) {
1267 if (view_cursors_cell_set(s, mincol) == -1) {
1268 size_t pos = view_cursors_pos(s);
1269 size_t col = text_line_width_set(txt, pos, mincol);
1270 view_cursors_to(s, col);
1273 return keys;
1276 static const char *selections_align_indent(Vis *vis, const char *keys, const Arg *arg) {
1277 View *view = vis_view(vis);
1278 Text *txt = vis_text(vis);
1279 bool left_align = arg->i < 0;
1280 int columns = view_selections_column_count(view);
1282 for (int i = 0; i < columns; i++) {
1283 int mincol = INT_MAX, maxcol = 0;
1284 for (Selection *s = view_selections_column(view, i); s; s = view_selections_column_next(s, i)) {
1285 Filerange sel = view_selections_get(s);
1286 size_t pos = left_align ? sel.start : sel.end;
1287 int col = text_line_width_get(txt, pos);
1288 if (col < mincol)
1289 mincol = col;
1290 if (col > maxcol)
1291 maxcol = col;
1294 size_t len = maxcol - mincol;
1295 char *buf = malloc(len+1);
1296 if (!buf)
1297 return keys;
1298 memset(buf, ' ', len);
1300 for (Selection *s = view_selections_column(view, i); s; s = view_selections_column_next(s, i)) {
1301 Filerange sel = view_selections_get(s);
1302 size_t pos = left_align ? sel.start : sel.end;
1303 size_t ipos = sel.start;
1304 int col = text_line_width_get(txt, pos);
1305 if (col < maxcol) {
1306 size_t off = maxcol - col;
1307 if (off <= len)
1308 text_insert(txt, ipos, buf, off);
1312 free(buf);
1315 view_draw(view);
1316 return keys;
1319 static const char *selections_clear(Vis *vis, const char *keys, const Arg *arg) {
1320 View *view = vis_view(vis);
1321 if (view_selections_count(view) > 1)
1322 view_selections_dispose_all(view);
1323 else
1324 view_selection_clear(view_selections_primary_get(view));
1325 return keys;
1328 static const Selection *selection_new_primary(View *view, Filerange *r) {
1329 Text *txt = view_text(view);
1330 size_t pos = text_char_prev(txt, r->end);
1331 Selection *s = view_selections_new(view, pos);
1332 if (!s)
1333 return NULL;
1334 view_selections_set(s, r);
1335 view_selections_anchor(s, true);
1336 view_selections_primary_set(s);
1337 return s;
1340 static const char *selections_match_next_literal(Vis *vis, const char *keys, const Arg *arg) {
1341 Text *txt = vis_text(vis);
1342 View *view = vis_view(vis);
1343 Selection *s = view_selections_primary_get(view);
1344 Filerange sel = view_selections_get(s);
1345 size_t len = text_range_size(&sel);
1346 if (!len)
1347 return keys;
1349 char *buf = text_bytes_alloc0(txt, sel.start, len);
1350 if (!buf)
1351 return keys;
1353 size_t start = text_find_next(txt, sel.end, buf);
1354 Filerange match = text_range_new(start, start+len);
1355 if (start != sel.end && selection_new_primary(view, &match))
1356 goto out;
1358 sel = view_selections_get(view_selections(view));
1359 start = text_find_prev(txt, sel.start, buf);
1360 if (start == sel.start)
1361 goto out;
1363 match = text_range_new(start, start+len);
1364 selection_new_primary(view, &match);
1366 out:
1367 free(buf);
1368 return keys;
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 if (!match_word)
1387 return selections_match_next_literal(vis, keys, arg);
1389 char *buf = text_bytes_alloc0(txt, sel.start, text_range_size(&sel));
1390 if (!buf)
1391 return keys;
1393 Filerange word = text_object_word_find_next(txt, sel.end, buf);
1394 if (text_range_valid(&word) && selection_new_primary(view, &word))
1395 goto out;
1397 sel = view_selections_get(view_selections(view));
1398 word = text_object_word_find_prev(txt, sel.start, buf);
1399 if (!text_range_valid(&word))
1400 goto out;
1401 selection_new_primary(view, &word);
1403 out:
1404 free(buf);
1405 return keys;
1408 static const char *selections_match_skip(Vis *vis, const char *keys, const Arg *arg) {
1409 View *view = vis_view(vis);
1410 Selection *sel = view_selections_primary_get(view);
1411 keys = selections_match_next(vis, keys, arg);
1412 if (sel != view_selections_primary_get(view))
1413 view_selections_dispose(sel);
1414 return keys;
1417 static const char *selections_remove(Vis *vis, const char *keys, const Arg *arg) {
1418 View *view = vis_view(vis);
1419 view_selections_dispose(view_selections_primary_get(view));
1420 view_cursor_to(view, view_cursor_get(view));
1421 return keys;
1424 static const char *selections_remove_column(Vis *vis, const char *keys, const Arg *arg) {
1425 View *view = vis_view(vis);
1426 int max = view_selections_column_count(view);
1427 int column = vis_count_get_default(vis, arg->i) - 1;
1428 if (column >= max)
1429 column = max - 1;
1430 if (view_selections_count(view) == 1) {
1431 vis_keys_feed(vis, "<Escape>");
1432 return keys;
1435 for (Selection *s = view_selections_column(view, column), *next; s; s = next) {
1436 next = view_selections_column_next(s, column);
1437 view_selections_dispose(s);
1440 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1441 return keys;
1444 static const char *selections_remove_column_except(Vis *vis, const char *keys, const Arg *arg) {
1445 View *view = vis_view(vis);
1446 int max = view_selections_column_count(view);
1447 int column = vis_count_get_default(vis, arg->i) - 1;
1448 if (column >= max)
1449 column = max - 1;
1450 if (view_selections_count(view) == 1) {
1451 vis_redraw(vis);
1452 return keys;
1455 Selection *sel = view_selections(view);
1456 Selection *col = view_selections_column(view, column);
1457 for (Selection *next; sel; sel = next) {
1458 next = view_selections_next(sel);
1459 if (sel == col)
1460 col = view_selections_column_next(col, column);
1461 else
1462 view_selections_dispose(sel);
1465 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1466 return keys;
1469 static const char *selections_navigate(Vis *vis, const char *keys, const Arg *arg) {
1470 View *view = vis_view(vis);
1471 if (view_selections_count(view) == 1)
1472 return wscroll(vis, keys, arg);
1473 Selection *s = view_selections_primary_get(view);
1474 VisCountIterator it = vis_count_iterator_get(vis, 1);
1475 while (vis_count_iterator_next(&it)) {
1476 if (arg->i > 0) {
1477 s = view_selections_next(s);
1478 if (!s)
1479 s = view_selections(view);
1480 } else {
1481 s = view_selections_prev(s);
1482 if (!s) {
1483 s = view_selections(view);
1484 for (Selection *n = s; n; n = view_selections_next(n))
1485 s = n;
1489 view_selections_primary_set(s);
1490 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1491 return keys;
1494 static const char *selections_rotate(Vis *vis, const char *keys, const Arg *arg) {
1496 typedef struct {
1497 Selection *sel;
1498 char *data;
1499 size_t len;
1500 } Rotate;
1502 Array arr;
1503 Text *txt = vis_text(vis);
1504 View *view = vis_view(vis);
1505 int columns = view_selections_column_count(view);
1506 int selections = columns == 1 ? view_selections_count(view) : columns;
1507 int count = vis_count_get_default(vis, 1);
1508 array_init_sized(&arr, sizeof(Rotate));
1509 if (!array_reserve(&arr, selections))
1510 return keys;
1511 size_t line = 0;
1513 for (Selection *s = view_selections(view), *next; s; s = next) {
1514 next = view_selections_next(s);
1515 size_t line_next = 0;
1517 Filerange sel = view_selections_get(s);
1518 Rotate rot;
1519 rot.sel = s;
1520 rot.len = text_range_size(&sel);
1521 if ((rot.data = malloc(rot.len)))
1522 rot.len = text_bytes_get(txt, sel.start, rot.len, rot.data);
1523 else
1524 rot.len = 0;
1525 array_add(&arr, &rot);
1527 if (!line)
1528 line = text_lineno_by_pos(txt, view_cursors_pos(s));
1529 if (next)
1530 line_next = text_lineno_by_pos(txt, view_cursors_pos(next));
1531 if (!next || (columns > 1 && line != line_next)) {
1532 size_t len = array_length(&arr);
1533 size_t off = arg->i > 0 ? count % len : len - (count % len);
1534 for (size_t i = 0; i < len; i++) {
1535 size_t j = (i + off) % len;
1536 Rotate *oldrot = array_get(&arr, i);
1537 Rotate *newrot = array_get(&arr, j);
1538 if (!oldrot || !newrot || oldrot == newrot)
1539 continue;
1540 Filerange newsel = view_selections_get(newrot->sel);
1541 if (!text_range_valid(&newsel))
1542 continue;
1543 if (!text_delete_range(txt, &newsel))
1544 continue;
1545 if (!text_insert(txt, newsel.start, oldrot->data, oldrot->len))
1546 continue;
1547 newsel.end = newsel.start + oldrot->len;
1548 view_selections_set(newrot->sel, &newsel);
1549 free(oldrot->data);
1551 array_clear(&arr);
1553 line = line_next;
1556 array_release(&arr);
1557 vis_count_set(vis, VIS_COUNT_UNKNOWN);
1558 return keys;
1561 static const char *selections_trim(Vis *vis, const char *keys, const Arg *arg) {
1562 Text *txt = vis_text(vis);
1563 View *view = vis_view(vis);
1564 for (Selection *s = view_selections(view), *next; s; s = next) {
1565 next = view_selections_next(s);
1566 Filerange sel = view_selections_get(s);
1567 if (!text_range_valid(&sel))
1568 continue;
1569 for (char b; sel.start < sel.end && text_byte_get(txt, sel.end-1, &b)
1570 && isspace((unsigned char)b); sel.end--);
1571 for (char b; sel.start <= sel.end && text_byte_get(txt, sel.start, &b)
1572 && isspace((unsigned char)b); sel.start++);
1573 if (sel.start < sel.end) {
1574 view_selections_set(s, &sel);
1575 } else if (!view_selections_dispose(s)) {
1576 vis_mode_switch(vis, VIS_MODE_NORMAL);
1579 return keys;
1582 static void selections_set(Vis *vis, View *view, Array *sel) {
1583 enum VisMode mode = vis_mode_get(vis);
1584 bool anchored = mode == VIS_MODE_VISUAL || mode == VIS_MODE_VISUAL_LINE;
1585 view_selections_set_all(view, sel, anchored);
1586 if (!anchored)
1587 view_selections_clear_all(view);
1590 static const char *selections_save(Vis *vis, const char *keys, const Arg *arg) {
1591 Win *win = vis_window(vis);
1592 View *view = vis_view(vis);
1593 enum VisMark mark = vis_mark_used(vis);
1594 Array sel = view_selections_get_all(view);
1595 vis_mark_set(win, mark, &sel);
1596 array_release(&sel);
1597 vis_cancel(vis);
1598 return keys;
1601 static const char *selections_restore(Vis *vis, const char *keys, const Arg *arg) {
1602 Win *win = vis_window(vis);
1603 View *view = vis_view(vis);
1604 enum VisMark mark = vis_mark_used(vis);
1605 Array sel = vis_mark_get(win, mark);
1606 selections_set(vis, view, &sel);
1607 array_release(&sel);
1608 vis_cancel(vis);
1609 return keys;
1612 static const char *selections_union(Vis *vis, const char *keys, const Arg *arg) {
1613 Win *win = vis_window(vis);
1614 View *view = vis_view(vis);
1615 enum VisMark mark = vis_mark_used(vis);
1616 Array a = vis_mark_get(win, mark);
1617 Array b = view_selections_get_all(view);
1618 Array sel;
1619 array_init_from(&sel, &a);
1621 size_t i = 0, j = 0;
1622 Filerange *r1 = array_get(&a, i), *r2 = array_get(&b, j), cur = text_range_empty();
1623 while (r1 || r2) {
1624 if (r1 && text_range_overlap(r1, &cur)) {
1625 cur = text_range_union(r1, &cur);
1626 r1 = array_get(&a, ++i);
1627 } else if (r2 && text_range_overlap(r2, &cur)) {
1628 cur = text_range_union(r2, &cur);
1629 r2 = array_get(&b, ++j);
1630 } else {
1631 if (text_range_valid(&cur))
1632 array_add(&sel, &cur);
1633 if (!r1) {
1634 cur = *r2;
1635 r2 = array_get(&b, ++j);
1636 } else if (!r2) {
1637 cur = *r1;
1638 r1 = array_get(&a, ++i);
1639 } else {
1640 if (r1->start < r2->start) {
1641 cur = *r1;
1642 r1 = array_get(&a, ++i);
1643 } else {
1644 cur = *r2;
1645 r2 = array_get(&b, ++j);
1651 if (text_range_valid(&cur))
1652 array_add(&sel, &cur);
1654 selections_set(vis, view, &sel);
1655 vis_cancel(vis);
1657 array_release(&a);
1658 array_release(&b);
1659 array_release(&sel);
1661 return keys;
1664 static void intersect(Array *ret, Array *a, Array *b) {
1665 size_t i = 0, j = 0;
1666 Filerange *r1 = array_get(a, i), *r2 = array_get(b, j);
1667 while (r1 && r2) {
1668 if (text_range_overlap(r1, r2)) {
1669 Filerange new = text_range_intersect(r1, r2);
1670 array_add(ret, &new);
1672 if (r1->end < r2->end)
1673 r1 = array_get(a, ++i);
1674 else
1675 r2 = array_get(b, ++j);
1679 static const char *selections_intersect(Vis *vis, const char *keys, const Arg *arg) {
1680 Win *win = vis_window(vis);
1681 View *view = vis_view(vis);
1682 enum VisMark mark = vis_mark_used(vis);
1683 Array a = vis_mark_get(win, mark);
1684 Array b = view_selections_get_all(view);
1685 Array sel;
1686 array_init_from(&sel, &a);
1688 intersect(&sel, &a, &b);
1689 selections_set(vis, view, &sel);
1690 vis_cancel(vis);
1692 array_release(&a);
1693 array_release(&b);
1694 array_release(&sel);
1696 return keys;
1699 static void complement(Array *ret, Array *a, Filerange *universe) {
1700 size_t pos = universe->start;
1701 for (size_t i = 0, len = array_length(a); i < len; i++) {
1702 Filerange *r = array_get(a, i);
1703 if (pos < r->start) {
1704 Filerange new = text_range_new(pos, r->start);
1705 array_add(ret, &new);
1707 pos = r->end;
1709 if (pos < universe->end) {
1710 Filerange new = text_range_new(pos, universe->end);
1711 array_add(ret, &new);
1715 static const char *selections_complement(Vis *vis, const char *keys, const Arg *arg) {
1716 Text *txt = vis_text(vis);
1717 View *view = vis_view(vis);
1718 Filerange universe = text_object_entire(txt, 0);
1719 Array a = view_selections_get_all(view);
1720 Array sel;
1721 array_init_from(&sel, &a);
1723 complement(&sel, &a, &universe);
1725 selections_set(vis, view, &sel);
1726 array_release(&a);
1727 array_release(&sel);
1728 return keys;
1731 static const char *selections_minus(Vis *vis, const char *keys, const Arg *arg) {
1732 Text *txt = vis_text(vis);
1733 Win *win = vis_window(vis);
1734 View *view = vis_view(vis);
1735 enum VisMark mark = vis_mark_used(vis);
1736 Array a = view_selections_get_all(view);
1737 Array b = vis_mark_get(win, mark);
1738 Array sel;
1739 array_init_from(&sel, &a);
1740 Array b_complement;
1741 array_init_from(&b_complement, &b);
1743 Filerange universe = text_object_entire(txt, 0);
1744 complement(&b_complement, &b, &universe);
1745 intersect(&sel, &a, &b_complement);
1747 selections_set(vis, view, &sel);
1748 vis_cancel(vis);
1750 array_release(&a);
1751 array_release(&b);
1752 array_release(&b_complement);
1753 array_release(&sel);
1755 return keys;
1758 static const char *replace(Vis *vis, const char *keys, const Arg *arg) {
1759 if (!keys[0]) {
1760 vis_keymap_disable(vis);
1761 return NULL;
1764 const char *next = vis_keys_next(vis, keys);
1765 if (!next)
1766 return NULL;
1768 char replacement[UTFmax+1];
1769 if (!vis_keys_utf8(vis, keys, replacement))
1770 return next;
1772 if (replacement[0] == 0x1b) /* <Escape> */
1773 return next;
1775 vis_operator(vis, VIS_OP_REPLACE, replacement);
1776 if (vis_mode_get(vis) == VIS_MODE_OPERATOR_PENDING)
1777 vis_motion(vis, VIS_MOVE_CHAR_NEXT);
1778 return next;
1781 static const char *count(Vis *vis, const char *keys, const Arg *arg) {
1782 int digit = keys[-1] - '0';
1783 int count = vis_count_get_default(vis, 0);
1784 if (0 <= digit && digit <= 9) {
1785 if (digit == 0 && count == 0)
1786 vis_motion(vis, VIS_MOVE_LINE_BEGIN);
1787 else
1788 vis_count_set(vis, count * 10 + digit);
1790 return keys;
1793 static const char *gotoline(Vis *vis, const char *keys, const Arg *arg) {
1794 if (vis_count_get(vis) != VIS_COUNT_UNKNOWN)
1795 vis_motion(vis, VIS_MOVE_LINE);
1796 else if (arg->i < 0)
1797 vis_motion(vis, VIS_MOVE_FILE_BEGIN);
1798 else
1799 vis_motion(vis, VIS_MOVE_FILE_END);
1800 return keys;
1803 static const char *operator(Vis *vis, const char *keys, const Arg *arg) {
1804 vis_operator(vis, arg->i);
1805 return keys;
1808 static const char *movement_key(Vis *vis, const char *keys, const Arg *arg) {
1809 if (!keys[0]) {
1810 vis_keymap_disable(vis);
1811 return NULL;
1814 const char *next = vis_keys_next(vis, keys);
1815 if (!next)
1816 return NULL;
1817 char utf8[UTFmax+1];
1818 if (vis_keys_utf8(vis, keys, utf8))
1819 vis_motion(vis, arg->i, utf8);
1820 return next;
1823 static const char *movement(Vis *vis, const char *keys, const Arg *arg) {
1824 vis_motion(vis, arg->i);
1825 return keys;
1828 static const char *textobj(Vis *vis, const char *keys, const Arg *arg) {
1829 vis_textobject(vis, arg->i);
1830 return keys;
1833 static const char *selection_end(Vis *vis, const char *keys, const Arg *arg) {
1834 for (Selection *s = view_selections(vis_view(vis)); s; s = view_selections_next(s))
1835 view_selections_flip(s);
1836 return keys;
1839 static const char *reg(Vis *vis, const char *keys, const Arg *arg) {
1840 if (!keys[0])
1841 return NULL;
1842 const char *next = vis_keys_next(vis, keys);
1843 if (next - keys > 1)
1844 return next;
1845 enum VisRegister reg = vis_register_from(vis, keys[0]);
1846 vis_register(vis, reg);
1847 return keys+1;
1850 static const char *mark(Vis *vis, const char *keys, const Arg *arg) {
1851 if (!keys[0])
1852 return NULL;
1853 const char *next = vis_keys_next(vis, keys);
1854 if (next - keys > 1)
1855 return next;
1856 enum VisMark mark = vis_mark_from(vis, keys[0]);
1857 vis_mark(vis, mark);
1858 return keys+1;
1861 static const char *undo(Vis *vis, const char *keys, const Arg *arg) {
1862 size_t pos = text_undo(vis_text(vis));
1863 if (pos != EPOS) {
1864 View *view = vis_view(vis);
1865 if (view_selections_count(view) == 1)
1866 view_cursor_to(view, pos);
1867 /* redraw all windows in case some display the same file */
1868 vis_draw(vis);
1870 return keys;
1873 static const char *redo(Vis *vis, const char *keys, const Arg *arg) {
1874 size_t pos = text_redo(vis_text(vis));
1875 if (pos != EPOS) {
1876 View *view = vis_view(vis);
1877 if (view_selections_count(view) == 1)
1878 view_cursor_to(view, pos);
1879 /* redraw all windows in case some display the same file */
1880 vis_draw(vis);
1882 return keys;
1885 static const char *earlier(Vis *vis, const char *keys, const Arg *arg) {
1886 size_t pos = EPOS;
1887 VisCountIterator it = vis_count_iterator_get(vis, 1);
1888 while (vis_count_iterator_next(&it))
1889 pos = text_earlier(vis_text(vis));
1890 if (pos != EPOS) {
1891 view_cursor_to(vis_view(vis), pos);
1892 /* redraw all windows in case some display the same file */
1893 vis_draw(vis);
1895 return keys;
1898 static const char *later(Vis *vis, const char *keys, const Arg *arg) {
1899 size_t pos = EPOS;
1900 VisCountIterator it = vis_count_iterator_get(vis, 1);
1901 while (vis_count_iterator_next(&it))
1902 pos = text_later(vis_text(vis));
1903 if (pos != EPOS) {
1904 view_cursor_to(vis_view(vis), pos);
1905 /* redraw all windows in case some display the same file */
1906 vis_draw(vis);
1908 return keys;
1911 static const char *delete(Vis *vis, const char *keys, const Arg *arg) {
1912 vis_operator(vis, VIS_OP_DELETE);
1913 vis_motion(vis, arg->i);
1914 return keys;
1917 static const char *insert_register(Vis *vis, const char *keys, const Arg *arg) {
1918 if (!keys[0])
1919 return NULL;
1920 const char *next = vis_keys_next(vis, keys);
1921 if (next - keys > 1)
1922 return next;
1923 enum VisRegister reg = vis_register_from(vis, keys[0]);
1924 if (reg != VIS_REG_INVALID) {
1925 vis_register(vis, reg);
1926 vis_operator(vis, VIS_OP_PUT_BEFORE_END);
1928 return keys+1;
1931 static const char *prompt_show(Vis *vis, const char *keys, const Arg *arg) {
1932 vis_prompt_show(vis, arg->s);
1933 return keys;
1936 static const char *insert_verbatim(Vis *vis, const char *keys, const Arg *arg) {
1937 Rune rune = 0;
1938 char buf[4], type = keys[0];
1939 const char *data = NULL;
1940 int len = 0, count = 0, base = 0;
1941 switch (type) {
1942 case '\0':
1943 return NULL;
1944 case 'o':
1945 case 'O':
1946 count = 3;
1947 base = 8;
1948 break;
1949 case 'U':
1950 count = 4;
1951 /* fall through */
1952 case 'u':
1953 count += 4;
1954 base = 16;
1955 break;
1956 case 'x':
1957 case 'X':
1958 count = 2;
1959 base = 16;
1960 break;
1961 default:
1962 if ('0' <= type && type <= '9') {
1963 rune = type - '0';
1964 count = 2;
1965 base = 10;
1967 break;
1970 if (base) {
1971 for (keys++; keys[0] && count > 0; keys++, count--) {
1972 int v = 0;
1973 if (base == 8 && '0' <= keys[0] && keys[0] <= '7') {
1974 v = keys[0] - '0';
1975 } else if ((base == 10 || base == 16) && '0' <= keys[0] && keys[0] <= '9') {
1976 v = keys[0] - '0';
1977 } else if (base == 16 && 'a' <= keys[0] && keys[0] <= 'f') {
1978 v = 10 + keys[0] - 'a';
1979 } else if (base == 16 && 'A' <= keys[0] && keys[0] <= 'F') {
1980 v = 10 + keys[0] - 'A';
1981 } else {
1982 count = 0;
1983 break;
1985 rune = rune * base + v;
1988 if (count > 0)
1989 return NULL;
1990 if (type == 'u' || type == 'U') {
1991 len = runetochar(buf, &rune);
1992 } else {
1993 buf[0] = rune;
1994 len = 1;
1997 data = buf;
1998 } else {
1999 const char *next = vis_keys_next(vis, keys);
2000 if (!next)
2001 return NULL;
2002 if ((rune = vis_keys_codepoint(vis, keys)) != (Rune)-1) {
2003 len = runetochar(buf, &rune);
2004 if (buf[0] == '\n')
2005 buf[0] = '\r';
2006 data = buf;
2007 } else {
2008 vis_info_show(vis, "Unknown key");
2010 keys = next;
2013 if (len > 0)
2014 vis_insert_key(vis, data, len);
2015 return keys;
2018 static const char *wscroll(Vis *vis, const char *keys, const Arg *arg) {
2019 View *view = vis_view(vis);
2020 int count = vis_count_get(vis);
2021 switch (arg->i) {
2022 case -PAGE:
2023 view_scroll_page_up(view);
2024 break;
2025 case +PAGE:
2026 view_scroll_page_down(view);
2027 break;
2028 case -PAGE_HALF:
2029 view_scroll_halfpage_up(view);
2030 break;
2031 case +PAGE_HALF:
2032 view_scroll_halfpage_down(view);
2033 break;
2034 default:
2035 if (count == VIS_COUNT_UNKNOWN)
2036 count = arg->i < 0 ? -arg->i : arg->i;
2037 if (arg->i < 0)
2038 view_scroll_up(view, count);
2039 else
2040 view_scroll_down(view, count);
2041 break;
2043 vis_count_set(vis, VIS_COUNT_UNKNOWN);
2044 return keys;
2047 static const char *wslide(Vis *vis, const char *keys, const Arg *arg) {
2048 View *view = vis_view(vis);
2049 int count = vis_count_get(vis);
2050 if (count == VIS_COUNT_UNKNOWN)
2051 count = arg->i < 0 ? -arg->i : arg->i;
2052 if (arg->i >= 0)
2053 view_slide_down(view, count);
2054 else
2055 view_slide_up(view, count);
2056 vis_count_set(vis, VIS_COUNT_UNKNOWN);
2057 return keys;
2060 static const char *call(Vis *vis, const char *keys, const Arg *arg) {
2061 arg->f(vis);
2062 return keys;
2065 static const char *window(Vis *vis, const char *keys, const Arg *arg) {
2066 arg->w(vis_view(vis));
2067 return keys;
2070 static const char *openline(Vis *vis, const char *keys, const Arg *arg) {
2071 vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_INSERT);
2072 if (arg->i > 0) {
2073 vis_motion(vis, VIS_MOVE_LINE_END);
2074 vis_keys_feed(vis, "<Enter>");
2075 } else {
2076 if (vis_get_autoindent(vis)) {
2077 vis_motion(vis, VIS_MOVE_LINE_START);
2078 vis_keys_feed(vis, "<vis-motion-line-start>");
2079 } else {
2080 vis_motion(vis, VIS_MOVE_LINE_BEGIN);
2081 vis_keys_feed(vis, "<vis-motion-line-begin>");
2083 vis_keys_feed(vis, "<Enter><Up>");
2085 return keys;
2088 static const char *join(Vis *vis, const char *keys, const Arg *arg) {
2089 bool normal = (vis_mode_get(vis) == VIS_MODE_NORMAL);
2090 vis_operator(vis, VIS_OP_JOIN, arg->s);
2091 if (normal) {
2092 int count = vis_count_get_default(vis, 0);
2093 if (count)
2094 vis_count_set(vis, count-1);
2095 vis_motion(vis, VIS_MOVE_LINE_NEXT);
2097 return keys;
2100 static const char *normalmode_escape(Vis *vis, const char *keys, const Arg *arg) {
2101 if (vis_count_get(vis) == VIS_COUNT_UNKNOWN)
2102 selections_clear(vis, keys, arg);
2103 else
2104 vis_count_set(vis, VIS_COUNT_UNKNOWN);
2105 return keys;
2108 static const char *visualmode_escape(Vis *vis, const char *keys, const Arg *arg) {
2109 if (vis_count_get(vis) == VIS_COUNT_UNKNOWN)
2110 vis_mode_switch(vis, VIS_MODE_NORMAL);
2111 else
2112 vis_count_set(vis, VIS_COUNT_UNKNOWN);
2113 return keys;
2116 static const char *switchmode(Vis *vis, const char *keys, const Arg *arg) {
2117 vis_mode_switch(vis, arg->i);
2118 return keys;
2121 static const char *insertmode(Vis *vis, const char *keys, const Arg *arg) {
2122 vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_INSERT);
2123 vis_motion(vis, arg->i);
2124 return keys;
2127 static const char *replacemode(Vis *vis, const char *keys, const Arg *arg) {
2128 vis_operator(vis, VIS_OP_MODESWITCH, VIS_MODE_REPLACE);
2129 vis_motion(vis, arg->i);
2130 return keys;
2133 static const char *unicode_info(Vis *vis, const char *keys, const Arg *arg) {
2134 View *view = vis_view(vis);
2135 Text *txt = vis_text(vis);
2136 size_t start = view_cursor_get(view);
2137 size_t end = text_char_next(txt, start);
2138 char *grapheme = text_bytes_alloc0(txt, start, end-start), *codepoint = grapheme;
2139 if (!grapheme)
2140 return keys;
2141 Buffer info;
2142 buffer_init(&info);
2143 mbstate_t ps = { 0 };
2144 Iterator it = text_iterator_get(txt, start);
2145 for (size_t pos = start; it.pos < end; pos = it.pos) {
2146 if (!text_iterator_codepoint_next(&it, NULL)) {
2147 vis_info_show(vis, "Failed to parse code point");
2148 goto err;
2150 size_t len = it.pos - pos;
2151 wchar_t wc = 0xFFFD;
2152 size_t res = mbrtowc(&wc, codepoint, len, &ps);
2153 bool combining = false;
2154 if (res != (size_t)-1 && res != (size_t)-2)
2155 combining = (wc != L'\0' && wcwidth(wc) == 0);
2156 unsigned char ch = *codepoint;
2157 if (ch < 128 && !isprint(ch))
2158 buffer_appendf(&info, "<^%c> ", ch == 127 ? '?' : ch + 64);
2159 else
2160 buffer_appendf(&info, "<%s%.*s> ", combining ? " " : "", (int)len, codepoint);
2161 if (arg->i == VIS_ACTION_UNICODE_INFO) {
2162 buffer_appendf(&info, "U+%04"PRIX32" ", (uint32_t)wc);
2163 } else {
2164 for (size_t i = 0; i < len; i++)
2165 buffer_appendf(&info, "%02x ", (uint8_t)codepoint[i]);
2167 codepoint += len;
2169 vis_info_show(vis, "%s", buffer_content0(&info));
2170 err:
2171 free(grapheme);
2172 buffer_release(&info);
2173 return keys;
2176 static const char *percent(Vis *vis, const char *keys, const Arg *arg) {
2177 if (vis_count_get(vis) == VIS_COUNT_UNKNOWN)
2178 vis_motion(vis, VIS_MOVE_BRACKET_MATCH);
2179 else
2180 vis_motion(vis, VIS_MOVE_PERCENT);
2181 return keys;
2184 static const char *jumplist(Vis *vis, const char *keys, const Arg *arg) {
2185 if (arg->i < 0)
2186 vis_jumplist_prev(vis);
2187 else if (arg->i > 0)
2188 vis_jumplist_next(vis);
2189 else
2190 vis_jumplist_save(vis);
2191 return keys;
2194 static Vis *vis;
2196 static void signal_handler(int signum, siginfo_t *siginfo, void *context) {
2197 vis_signal_handler(vis, signum, siginfo, context);
2200 int main(int argc, char *argv[]) {
2202 VisEvent event = {
2203 .init = vis_lua_init,
2204 .start = vis_lua_start,
2205 .quit = vis_lua_quit,
2206 .mode_insert_input = vis_lua_mode_insert_input,
2207 .mode_replace_input = vis_lua_mode_replace_input,
2208 .file_open = vis_lua_file_open,
2209 .file_save_pre = vis_lua_file_save_pre,
2210 .file_save_post = vis_lua_file_save_post,
2211 .file_close = vis_lua_file_close,
2212 .win_open = vis_lua_win_open,
2213 .win_close = vis_lua_win_close,
2214 .win_highlight = vis_lua_win_highlight,
2215 .win_status = vis_lua_win_status,
2216 .term_csi = vis_lua_term_csi,
2219 vis = vis_new(ui_term_new(), &event);
2220 if (!vis)
2221 return EXIT_FAILURE;
2223 for (int i = 0; i < LENGTH(vis_action); i++) {
2224 const KeyAction *action = &vis_action[i];
2225 if (!vis_action_register(vis, action))
2226 vis_die(vis, "Could not register action: %s\n", action->name);
2229 for (int i = 0; i < LENGTH(default_bindings); i++) {
2230 for (const KeyBinding **binding = default_bindings[i]; binding && *binding; binding++) {
2231 for (const KeyBinding *kb = *binding; kb->key; kb++) {
2232 vis_mode_map(vis, i, false, kb->key, kb);
2237 for (const char **k = keymaps; k[0]; k += 2)
2238 vis_keymap_add(vis, k[0], k[1]);
2240 /* install signal handlers etc. */
2241 struct sigaction sa;
2242 memset(&sa, 0, sizeof sa);
2243 sigfillset(&sa.sa_mask);
2244 sa.sa_flags = SA_SIGINFO;
2245 sa.sa_sigaction = signal_handler;
2246 if (sigaction(SIGBUS, &sa, NULL) == -1 ||
2247 sigaction(SIGINT, &sa, NULL) == -1 ||
2248 sigaction(SIGCONT, &sa, NULL) == -1 ||
2249 sigaction(SIGWINCH, &sa, NULL) == -1 ||
2250 sigaction(SIGTERM, &sa, NULL) == -1 ||
2251 sigaction(SIGHUP, &sa, NULL) == -1) {
2252 vis_die(vis, "Failed to set signal handler: %s\n", strerror(errno));
2255 sa.sa_handler = SIG_IGN;
2256 if (sigaction(SIGPIPE, &sa, NULL) == -1 || sigaction(SIGQUIT, &sa, NULL) == -1)
2257 vis_die(vis, "Failed to ignore signals\n");
2259 sigset_t blockset;
2260 sigemptyset(&blockset);
2261 sigaddset(&blockset, SIGBUS);
2262 sigaddset(&blockset, SIGCONT);
2263 sigaddset(&blockset, SIGWINCH);
2264 sigaddset(&blockset, SIGTERM);
2265 sigaddset(&blockset, SIGHUP);
2266 if (sigprocmask(SIG_BLOCK, &blockset, NULL) == -1)
2267 vis_die(vis, "Failed to block signals\n");
2269 for (int i = 1; i < argc; i++) {
2270 if (argv[i][0] != '-') {
2271 continue;
2272 } else if (strcmp(argv[i], "-") == 0) {
2273 continue;
2274 } else if (strcmp(argv[i], "--") == 0) {
2275 break;
2276 } else if (strcmp(argv[i], "-v") == 0) {
2277 printf("vis %s%s%s%s%s%s%s\n", VERSION,
2278 CONFIG_CURSES ? " +curses" : "",
2279 CONFIG_LUA ? " +lua" : "",
2280 CONFIG_LPEG ? " +lpeg" : "",
2281 CONFIG_TRE ? " +tre" : "",
2282 CONFIG_ACL ? " +acl" : "",
2283 CONFIG_SELINUX ? " +selinux" : "");
2284 return 0;
2285 } else {
2286 fprintf(stderr, "Unknown command option: %s\n", argv[i]);
2287 return 1;
2291 char *cmd = NULL;
2292 bool end_of_options = false, win_created = false;
2294 for (int i = 1; i < argc; i++) {
2295 if (argv[i][0] == '-' && !end_of_options) {
2296 if (strcmp(argv[i], "-") == 0) {
2297 if (!vis_window_new_fd(vis, STDOUT_FILENO))
2298 vis_die(vis, "Can not create empty buffer\n");
2299 ssize_t len = 0;
2300 char buf[PIPE_BUF];
2301 Text *txt = vis_text(vis);
2302 while ((len = read(STDIN_FILENO, buf, sizeof buf)) > 0)
2303 text_insert(txt, text_size(txt), buf, len);
2304 if (len == -1)
2305 vis_die(vis, "Can not read from stdin\n");
2306 text_snapshot(txt);
2307 int fd = open("/dev/tty", O_RDWR);
2308 if (fd == -1)
2309 vis_die(vis, "Can not reopen stdin\n");
2310 dup2(fd, STDIN_FILENO);
2311 close(fd);
2312 } else if (strcmp(argv[i], "--") == 0) {
2313 end_of_options = true;
2314 continue;
2316 } else if (argv[i][0] == '+' && !end_of_options) {
2317 cmd = argv[i] + (argv[i][1] == '/' || argv[i][1] == '?');
2318 continue;
2319 } else if (!vis_window_new(vis, argv[i])) {
2320 vis_die(vis, "Can not load `%s': %s\n", argv[i], strerror(errno));
2322 win_created = true;
2323 if (cmd) {
2324 vis_prompt_cmd(vis, cmd);
2325 cmd = NULL;
2329 if (!vis_window(vis) && !win_created) {
2330 if (!vis_window_new(vis, NULL))
2331 vis_die(vis, "Can not create empty buffer\n");
2332 if (cmd)
2333 vis_prompt_cmd(vis, cmd);
2336 int status = vis_run(vis);
2337 vis_free(vis);
2338 return status;