Revert "vis: process aliased key sequences individually"
[vis.git] / vis-prompt.c
blobd3e9b8ef4a0cb817bf4d4b044b9de092119383dd
1 #include <string.h>
2 #include "vis-core.h"
3 #include "text-motions.h"
4 #include "text-objects.h"
5 #include "text-util.h"
7 bool vis_prompt_cmd(Vis *vis, const char *cmd) {
8 if (!cmd || !cmd[0] || !cmd[1])
9 return true;
10 switch (cmd[0]) {
11 case '/':
12 return vis_motion(vis, VIS_MOVE_SEARCH_FORWARD, cmd+1);
13 case '?':
14 return vis_motion(vis, VIS_MOVE_SEARCH_BACKWARD, cmd+1);
15 case '+':
16 case ':':
17 register_put0(vis, &vis->registers[VIS_REG_COMMAND], cmd+1);
18 return vis_cmd(vis, cmd+1);
19 default:
20 return false;
24 static void prompt_hide(Win *win) {
25 Text *txt = win->file->text;
26 size_t size = text_size(txt);
27 /* make sure that file is new line terminated */
28 char lastchar = '\0';
29 if (size >= 1 && text_byte_get(txt, size-1, &lastchar) && lastchar != '\n')
30 text_insert(txt, size, "\n", 1);
31 /* remove empty entries */
32 Filerange line_range = text_object_line(txt, text_size(txt)-1);
33 char *line = text_bytes_alloc0(txt, line_range.start, text_range_size(&line_range));
34 if (line && (line[0] == '\n' || (strchr(":/?", line[0]) && (line[1] == '\n' || line[1] == '\0'))))
35 text_delete_range(txt, &line_range);
36 free(line);
37 vis_window_close(win);
40 static void prompt_restore(Win *win) {
41 Vis *vis = win->vis;
42 /* restore window and mode which was active before the prompt window
43 * we deliberately don't use vis_mode_switch because we do not want
44 * to invoke the modes enter/leave functions */
45 if (win->parent)
46 vis->win = win->parent;
47 vis->mode = win->parent_mode;
50 static const char *prompt_enter(Vis *vis, const char *keys, const Arg *arg) {
51 Win *prompt = vis->win;
52 View *view = prompt->view;
53 Text *txt = prompt->file->text;
54 Win *win = prompt->parent;
55 char *cmd = NULL;
57 Filerange range = view_selection_get(view);
58 if (!vis->mode->visual) {
59 const char *pattern = NULL;
60 Regex *regex = text_regex_new();
61 size_t pos = view_cursor_get(view);
62 if (prompt->file == vis->command_file)
63 pattern = "^:";
64 else if (prompt->file == vis->search_file)
65 pattern = "^(/|\\?)";
66 if (pattern && regex && text_regex_compile(regex, pattern, REG_EXTENDED|REG_NEWLINE) == 0) {
67 char c;
68 size_t prev;
69 if (text_byte_get(txt, pos, &c) && (c == ':' || c == '/' || c == '?'))
70 prev = pos;
71 else
72 prev = text_search_backward(txt, pos, regex);
73 if (prev > pos)
74 prev = EPOS;
75 size_t next = text_search_forward(txt, pos, regex);
76 if (next < pos)
77 next = text_size(txt);
78 range = text_range_new(prev, next);
80 text_regex_free(regex);
82 if (text_range_valid(&range))
83 cmd = text_bytes_alloc0(txt, range.start, text_range_size(&range));
85 if (!win || !cmd) {
86 if (!win)
87 vis_info_show(vis, "Prompt window invalid");
88 else if (!cmd)
89 vis_info_show(vis, "Failed to detect command");
90 prompt_restore(prompt);
91 prompt_hide(prompt);
92 free(cmd);
93 return keys;
96 size_t len = strlen(cmd);
97 if (len > 0 && cmd[len-1] == '\n')
98 cmd[len-1] = '\0';
100 bool lastline = (range.end == text_size(txt));
102 prompt_restore(prompt);
103 if (vis_prompt_cmd(vis, cmd)) {
104 prompt_hide(prompt);
105 if (!lastline) {
106 text_delete(txt, range.start, text_range_size(&range));
107 text_appendf(txt, "%s\n", cmd);
109 } else {
110 vis->win = prompt;
111 vis->mode = &vis_modes[VIS_MODE_INSERT];
113 free(cmd);
114 vis_draw(vis);
115 return keys;
118 static const char *prompt_esc(Vis *vis, const char *keys, const Arg *arg) {
119 Win *prompt = vis->win;
120 if (view_selections_count(prompt->view) > 1) {
121 view_selections_dispose_all(prompt->view);
122 } else {
123 prompt_restore(prompt);
124 prompt_hide(prompt);
126 return keys;
129 static const char *prompt_up(Vis *vis, const char *keys, const Arg *arg) {
130 vis_motion(vis, VIS_MOVE_LINE_UP);
131 vis_window_mode_unmap(vis->win, VIS_MODE_INSERT, "<Up>");
132 view_options_set(vis->win->view, UI_OPTION_SYMBOL_EOF);
133 return keys;
136 static const KeyBinding prompt_enter_binding = {
137 .key = "<Enter>",
138 .action = &(KeyAction){
139 .func = prompt_enter,
143 static const KeyBinding prompt_esc_binding = {
144 .key = "<Escape>",
145 .action = &(KeyAction){
146 .func = prompt_esc,
150 static const KeyBinding prompt_up_binding = {
151 .key = "<Up>",
152 .action = &(KeyAction){
153 .func = prompt_up,
157 static const KeyBinding prompt_tab_binding = {
158 .key = "<Tab>",
159 .alias = "<C-x><C-o>",
162 void vis_prompt_show(Vis *vis, const char *title) {
163 Win *active = vis->win;
164 Win *prompt = window_new_file(vis, title[0] == ':' ? vis->command_file : vis->search_file,
165 UI_OPTION_ONELINE);
166 if (!prompt)
167 return;
168 Text *txt = prompt->file->text;
169 text_appendf(txt, "%s\n", title);
170 Selection *sel = view_selections_primary_get(prompt->view);
171 view_cursors_scroll_to(sel, text_size(txt)-1);
172 prompt->parent = active;
173 prompt->parent_mode = vis->mode;
174 vis_window_mode_map(prompt, VIS_MODE_NORMAL, true, "<Enter>", &prompt_enter_binding);
175 vis_window_mode_map(prompt, VIS_MODE_INSERT, true, "<Enter>", &prompt_enter_binding);
176 vis_window_mode_map(prompt, VIS_MODE_VISUAL, true, "<Enter>", &prompt_enter_binding);
177 vis_window_mode_map(prompt, VIS_MODE_NORMAL, true, "<Escape>", &prompt_esc_binding);
178 vis_window_mode_map(prompt, VIS_MODE_INSERT, true, "<Up>", &prompt_up_binding);
179 if (CONFIG_LUA)
180 vis_window_mode_map(prompt, VIS_MODE_INSERT, true, "<Tab>", &prompt_tab_binding);
181 vis_mode_switch(vis, VIS_MODE_INSERT);
184 void vis_info_show(Vis *vis, const char *msg, ...) {
185 va_list ap;
186 va_start(ap, msg);
187 vis->ui->info(vis->ui, msg, ap);
188 va_end(ap);
191 void vis_info_hide(Vis *vis) {
192 vis->ui->info_hide(vis->ui);
195 void vis_message_show(Vis *vis, const char *msg) {
196 if (!msg)
197 return;
198 if (!vis->message_window)
199 vis->message_window = window_new_file(vis, vis->error_file, UI_OPTION_STATUSBAR);
200 Win *win = vis->message_window;
201 if (!win)
202 return;
203 Text *txt = win->file->text;
204 size_t pos = text_size(txt);
205 text_appendf(txt, "%s\n", msg);
206 text_save(txt, NULL);
207 view_cursor_to(win->view, pos);
208 vis_window_focus(win);
211 void vis_message_hide(Vis *vis) {
212 if (!vis->message_window)
213 return;
214 vis_window_close(vis->message_window);
215 vis->message_window = NULL;