static void vis_args(Vis *vis, int argc, char *argv[]) { char *cmd = NULL; bool end_of_options = false; for (int i = 1; i < argc; i++) { if (argv[i][0] == '-' && !end_of_options) { switch (argv[i][1]) { case '-': end_of_options = true; break; case 'v': vis_die(vis, "vis %s\n", VERSION); break; case '\0': break; default: vis_die(vis, "Unknown command option: %s\n", argv[i]); break; } } else if (argv[i][0] == '+') { cmd = argv[i] + (argv[i][1] == '/' || argv[i][1] == '?'); } else if (!vis_window_new(vis, argv[i])) { vis_die(vis, "Can not load `%s': %s\n", argv[i], strerror(errno)); } else if (cmd) { vis_prompt_cmd(vis, cmd); cmd = NULL; } } if (!vis->windows && vis->running) { if (!strcmp(argv[argc-1], "-")) { if (!vis_window_new(vis, NULL)) vis_die(vis, "Can not create empty buffer\n"); ssize_t len = 0; char buf[PIPE_BUF]; File *file = vis->win->file; Text *txt = file->text; file->is_stdin = true; while ((len = read(STDIN_FILENO, buf, sizeof buf)) > 0) text_insert(txt, text_size(txt), buf, len); if (len == -1) vis_die(vis, "Can not read from stdin\n"); text_snapshot(txt); int fd = open("/dev/tty", O_RDONLY); if (fd == -1) vis_die(vis, "Can not reopen stdin\n"); dup2(fd, STDIN_FILENO); close(fd); } else if (!vis_window_new(vis, NULL)) { vis_die(vis, "Can not create empty buffer\n"); } if (cmd) vis_prompt_cmd(vis, cmd); } }
static const char *prompt_enter(Vis *vis, const char *keys, const Arg *arg) { Win *prompt = vis->win; View *view = prompt->view; Text *txt = prompt->file->text; Win *win = prompt->parent; char *cmd = NULL; Filerange range = view_selection_get(view); if (!text_range_valid(&range)) range = text_object_line(txt, view_cursor_get(view)); if (text_range_valid(&range)) cmd = text_bytes_alloc0(txt, range.start, text_range_size(&range)); if (!win || !cmd) { vis_info_show(vis, "Prompt window invalid\n"); prompt_restore(prompt); prompt_hide(prompt); free(cmd); return keys; } size_t len = strlen(cmd); if (len > 0 && cmd[len-1] == '\n') cmd[len-1] = '\0'; bool lastline = (range.end == text_size(txt)); prompt_restore(prompt); if (vis_prompt_cmd(vis, cmd)) { prompt_hide(prompt); if (!lastline) { text_delete(txt, range.start, text_range_size(&range)); text_appendf(txt, "%s\n", cmd); } } else { vis->win = prompt; vis->mode = &vis_modes[VIS_MODE_INSERT]; } free(cmd); vis_draw(vis); return keys; }