int vim_format_help_cmd(const char topic[], char cmd[], size_t cmd_size) { int bg; #ifndef _WIN32 char *const escaped_rtp = shell_like_escape(PACKAGE_DATA_DIR, 0); char *const escaped_args = shell_like_escape(topic, 0); snprintf(cmd, cmd_size, "%s -c 'set runtimepath+=%s/vim-doc' -c help\\ %s -c only", cfg_get_vicmd(&bg), escaped_rtp, escaped_args); free(escaped_args); free(escaped_rtp); #else char exe_dir[PATH_MAX]; char *escaped_rtp; (void)get_exe_dir(exe_dir, sizeof(exe_dir)); escaped_rtp = shell_like_escape(exe_dir, 0); snprintf(cmd, cmd_size, "%s -c \"set runtimepath+=%s/data/vim-doc\" -c \"help %s\" -c only", cfg_get_vicmd(&bg), escaped_rtp, topic); free(escaped_rtp); #endif return bg; }
/* Handles current content of the menu to Vim as quickfix list. */ static void cmd_v(key_info_t key_info, keys_info_t *keys_info) { int bg; const char *vi_cmd; FILE *vim_stdin; char *cmd; int i; int qf = 1; /* If both first and last lines do not contain colons, treat lines as list of * file names. */ if(strchr(menu->items[0], ':') == NULL && strchr(menu->items[menu->len - 1], ':') == NULL) { qf = 0; } ui_shutdown(); curr_stats.need_update = UT_FULL; vi_cmd = cfg_get_vicmd(&bg); if(!qf) { char *const arg = shell_like_escape("+exe 'bd!|args' " "join(map(getline('1','$'),'fnameescape(v:val)'))", 0); cmd = format_str("%s %s +argument%d -", vi_cmd, arg, menu->pos + 1); free(arg); } else if(menu->pos == 0) { /* For some reason +cc1 causes noisy messages on status line, so handle this * case separately. */ cmd = format_str("%s +cgetbuffer +bd! +cfirst -", vi_cmd); } else { cmd = format_str("%s +cgetbuffer +bd! +cfirst +cc%d -", vi_cmd, menu->pos + 1); } vim_stdin = popen(cmd, "w"); free(cmd); if(vim_stdin == NULL) { recover_after_shellout(); show_error_msg("Vim QuickFix", "Failed to send list of files to editor."); return; } for(i = 0; i < menu->len; ++i) { fputs(menu->items[i], vim_stdin); putc('\n', vim_stdin); } pclose(vim_stdin); recover_after_shellout(); }
int vim_view_file(const char filename[], int line, int column, int allow_forking) { char vicmd[PATH_MAX]; char cmd[PATH_MAX + 5]; const char *fork_str = allow_forking ? "" : "--nofork"; char *escaped; int bg; int result; cmd[0] = '\0'; if(!path_exists(filename, DEREF)) { if(path_exists(filename, NODEREF)) { show_error_msg("Broken Link", "Link destination doesn't exist"); } else { show_error_msg("Wrong Path", "File doesn't exist"); } return 1; } #ifndef _WIN32 escaped = shell_like_escape(filename, 0); #else escaped = (char *)enclose_in_dquotes(filename); #endif copy_str(vicmd, sizeof(vicmd), cfg_get_vicmd(&bg)); trim_right(vicmd); if(!allow_forking) { char *p = strrchr(vicmd, ' '); if(p != NULL && strstr(p, "remote")) { *p = '\0'; } } if(line < 0 && column < 0) snprintf(cmd, sizeof(cmd), "%s %s %s", vicmd, fork_str, escaped); else if(column < 0) snprintf(cmd, sizeof(cmd), "%s %s +%d %s", vicmd, fork_str, line, escaped); else snprintf(cmd, sizeof(cmd), "%s %s \"+call cursor(%d, %d)\" %s", vicmd, fork_str, line, column, escaped); #ifndef _WIN32 free(escaped); #endif result = run_vim(cmd, bg && allow_forking, allow_forking); curs_set(FALSE); return result; }
/* Formats a command to edit selected files of the current view in an editor. * Returns a newly allocated string, which should be freed by the caller. */ TSTATIC char * format_edit_selection_cmd(int *bg) { char *const files = expand_macros("%f", NULL, NULL, 1); char *const cmd = format_str("%s %s", cfg_get_vicmd(bg), files); free(files); return cmd; }
void vim_edit_files(int nfiles, char *files[]) { char cmd[PATH_MAX]; size_t len; int i; int bg; len = snprintf(cmd, sizeof(cmd), "%s ", cfg_get_vicmd(&bg)); for(i = 0; i < nfiles && len < sizeof(cmd) - 1; ++i) { char *escaped = shell_like_escape(files[i], 0); len += snprintf(cmd + len, sizeof(cmd) - len, "%s ", escaped); free(escaped); } run_vim(cmd, bg, 1); }
/* Composes title for window of a terminal multiplexer from a command. Returns * newly allocated string that should be freed by the caller. */ static char * gen_term_multiplexer_title_arg(const char cmd[]) { int bg; const char *const vicmd = cfg_get_vicmd(&bg); const char *const visubcmd = strstr(cmd, vicmd); char *command_name = NULL; const char *title; char *title_arg; if(visubcmd != NULL) { title = skip_whitespace(visubcmd + strlen(vicmd) + 1); } else { char *const separator = strchr(cmd, ' '); if(separator != NULL) { *separator = '\0'; command_name = strdup(cmd); *separator = ' '; } title = command_name; } if(is_null_or_empty(title)) { title_arg = strdup(""); } else { const char opt_c = (curr_stats.term_multiplexer == TM_SCREEN) ? 't' : 'n'; char *const escaped_title = shell_like_escape(title, 0); title_arg = format_str("-%c %s", opt_c, escaped_title); free(escaped_title); } free(command_name); return title_arg; }