Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 10 | #define EXTERN |
| 11 | #include "vim.h" |
| 12 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 13 | #ifdef __CYGWIN__ |
| 14 | # ifndef WIN32 |
Bram Moolenaar | 0d1498e | 2008-06-29 12:00:49 +0000 | [diff] [blame] | 15 | # include <cygwin/version.h> |
| 16 | # include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or |
| 17 | * cygwin_conv_path() */ |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 18 | # endif |
| 19 | # include <limits.h> |
| 20 | #endif |
| 21 | |
Bram Moolenaar | 97ff9b9 | 2016-06-26 20:37:46 +0200 | [diff] [blame] | 22 | #if defined(WIN3264) && !defined(FEAT_GUI_W32) |
| 23 | # include "iscygpty.h" |
| 24 | #endif |
| 25 | |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 26 | /* Values for edit_type. */ |
| 27 | #define EDIT_NONE 0 /* no edit type yet */ |
| 28 | #define EDIT_FILE 1 /* file name argument[s] given, use argument list */ |
| 29 | #define EDIT_STDIN 2 /* read file from stdin */ |
| 30 | #define EDIT_TAG 3 /* tag name argument given, use tagname */ |
| 31 | #define EDIT_QF 4 /* start in quickfix mode */ |
| 32 | |
Bram Moolenaar | b05b10a | 2011-03-22 18:10:45 +0100 | [diff] [blame] | 33 | #if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN) |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 34 | static int file_owned(char *fname); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 35 | #endif |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 36 | static void mainerr(int, char_u *); |
Bram Moolenaar | 502ae4b | 2016-07-16 19:50:13 +0200 | [diff] [blame] | 37 | # if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 38 | static void init_locale(void); |
| 39 | # endif |
| 40 | static void early_arg_scan(mparm_T *parmp); |
Bram Moolenaar | b05b10a | 2011-03-22 18:10:45 +0100 | [diff] [blame] | 41 | #ifndef NO_VIM_MAIN |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 42 | static void main_msg(char *s); |
| 43 | static void usage(void); |
| 44 | static int get_number_arg(char_u *p, int *idx, int def); |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 45 | static void parse_command_name(mparm_T *parmp); |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 46 | static void command_line_scan(mparm_T *parmp); |
| 47 | static void check_tty(mparm_T *parmp); |
| 48 | static void read_stdin(void); |
| 49 | static void create_windows(mparm_T *parmp); |
Bram Moolenaar | b05b10a | 2011-03-22 18:10:45 +0100 | [diff] [blame] | 50 | # ifdef FEAT_WINDOWS |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 51 | static void edit_buffers(mparm_T *parmp, char_u *cwd); |
Bram Moolenaar | b05b10a | 2011-03-22 18:10:45 +0100 | [diff] [blame] | 52 | # endif |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 53 | static void exe_pre_commands(mparm_T *parmp); |
| 54 | static void exe_commands(mparm_T *parmp); |
| 55 | static void source_startup_scripts(mparm_T *parmp); |
| 56 | static void main_start_gui(void); |
Bram Moolenaar | b05b10a | 2011-03-22 18:10:45 +0100 | [diff] [blame] | 57 | # if defined(HAS_SWAP_EXISTS_ACTION) |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 58 | static void check_swap_exists_action(void); |
Bram Moolenaar | b05b10a | 2011-03-22 18:10:45 +0100 | [diff] [blame] | 59 | # endif |
Bram Moolenaar | 08cab96 | 2017-03-04 14:37:18 +0100 | [diff] [blame] | 60 | # ifdef FEAT_EVAL |
| 61 | static void set_progpath(char_u *argv0); |
| 62 | # endif |
Bram Moolenaar | b05b10a | 2011-03-22 18:10:45 +0100 | [diff] [blame] | 63 | # if defined(FEAT_CLIENTSERVER) || defined(PROTO) |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 64 | static void exec_on_server(mparm_T *parmp); |
| 65 | static void prepare_server(mparm_T *parmp); |
| 66 | static void cmdsrv_main(int *argc, char **argv, char_u *serverName_arg, char_u **serverStr); |
| 67 | static char_u *serverMakeName(char_u *arg, char *cmd); |
Bram Moolenaar | b05b10a | 2011-03-22 18:10:45 +0100 | [diff] [blame] | 68 | # endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 69 | #endif |
| 70 | |
| 71 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 72 | /* |
| 73 | * Different types of error messages. |
| 74 | */ |
| 75 | static char *(main_errors[]) = |
| 76 | { |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 77 | N_("Unknown option argument"), |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 78 | #define ME_UNKNOWN_OPTION 0 |
| 79 | N_("Too many edit arguments"), |
| 80 | #define ME_TOO_MANY_ARGS 1 |
| 81 | N_("Argument missing after"), |
| 82 | #define ME_ARG_MISSING 2 |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 83 | N_("Garbage after option argument"), |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 84 | #define ME_GARBAGE 3 |
| 85 | N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"), |
| 86 | #define ME_EXTRA_CMD 4 |
| 87 | N_("Invalid argument for"), |
| 88 | #define ME_INVALID_ARG 5 |
| 89 | }; |
| 90 | |
Bram Moolenaar | b05b10a | 2011-03-22 18:10:45 +0100 | [diff] [blame] | 91 | #ifndef PROTO /* don't want a prototype for main() */ |
Bram Moolenaar | 8866d27 | 2012-11-28 15:55:42 +0100 | [diff] [blame] | 92 | #ifndef NO_VIM_MAIN /* skip this for unittests */ |
Bram Moolenaar | f9bde2b | 2015-04-17 22:08:16 +0200 | [diff] [blame] | 93 | |
| 94 | static char_u *start_dir = NULL; /* current working dir on startup */ |
| 95 | |
Bram Moolenaar | b9a46fe | 2016-07-29 18:13:42 +0200 | [diff] [blame] | 96 | static int has_dash_c_arg = FALSE; |
| 97 | |
Bram Moolenaar | a8e691d | 2016-08-07 15:19:26 +0200 | [diff] [blame] | 98 | /* Various parameters passed between main() and other functions. */ |
| 99 | static mparm_T params; |
| 100 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 101 | int |
| 102 | # ifdef VIMDLL |
| 103 | _export |
| 104 | # endif |
| 105 | # ifdef FEAT_GUI_MSWIN |
| 106 | # ifdef __BORLANDC__ |
| 107 | _cdecl |
| 108 | # endif |
| 109 | VimMain |
| 110 | # else |
| 111 | main |
| 112 | # endif |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 113 | (int argc, char **argv) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 114 | { |
Bram Moolenaar | 3f26967 | 2009-11-03 11:11:11 +0000 | [diff] [blame] | 115 | #ifdef STARTUPTIME |
| 116 | int i; |
| 117 | #endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 118 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 119 | /* |
| 120 | * Do any system-specific initialisations. These can NOT use IObuff or |
| 121 | * NameBuff. Thus emsg2() cannot be called! |
| 122 | */ |
| 123 | mch_early_init(); |
| 124 | |
Bram Moolenaar | 1499332 | 2014-09-09 12:25:33 +0200 | [diff] [blame] | 125 | #if defined(WIN32) && defined(FEAT_MBYTE) |
| 126 | /* |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 127 | * MinGW expands command line arguments, which confuses our code to |
Bram Moolenaar | 1499332 | 2014-09-09 12:25:33 +0200 | [diff] [blame] | 128 | * convert when 'encoding' changes. Get the unexpanded arguments. |
| 129 | */ |
| 130 | argc = get_cmd_argsW(&argv); |
| 131 | #endif |
| 132 | |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 133 | /* Many variables are in "params" so that we can pass them to invoked |
| 134 | * functions without a lot of arguments. "argc" and "argv" are also |
| 135 | * copied, so that they can be changed. */ |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 136 | vim_memset(¶ms, 0, sizeof(params)); |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 137 | params.argc = argc; |
| 138 | params.argv = argv; |
| 139 | params.want_full_screen = TRUE; |
| 140 | #ifdef FEAT_EVAL |
| 141 | params.use_debug_break_level = -1; |
| 142 | #endif |
| 143 | #ifdef FEAT_WINDOWS |
| 144 | params.window_count = -1; |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 145 | #endif |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 146 | |
Bram Moolenaar | 99685e6 | 2013-05-11 13:56:18 +0200 | [diff] [blame] | 147 | #ifdef FEAT_RUBY |
| 148 | { |
| 149 | int ruby_stack_start; |
| 150 | vim_ruby_init((void *)&ruby_stack_start); |
| 151 | } |
| 152 | #endif |
| 153 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 154 | #ifdef FEAT_TCL |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 155 | vim_tcl_init(params.argv[0]); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 156 | #endif |
| 157 | |
| 158 | #ifdef MEM_PROFILE |
| 159 | atexit(vim_mem_profile_dump); |
| 160 | #endif |
| 161 | |
| 162 | #ifdef STARTUPTIME |
Bram Moolenaar | a8e691d | 2016-08-07 15:19:26 +0200 | [diff] [blame] | 163 | /* Need to find "--startuptime" before actually parsing arguments. */ |
Bram Moolenaar | 3f26967 | 2009-11-03 11:11:11 +0000 | [diff] [blame] | 164 | for (i = 1; i < argc; ++i) |
| 165 | { |
Bram Moolenaar | ef94eec | 2009-11-11 13:22:11 +0000 | [diff] [blame] | 166 | if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc) |
Bram Moolenaar | 3f26967 | 2009-11-03 11:11:11 +0000 | [diff] [blame] | 167 | { |
Bram Moolenaar | ef94eec | 2009-11-11 13:22:11 +0000 | [diff] [blame] | 168 | time_fd = mch_fopen(argv[i + 1], "a"); |
Bram Moolenaar | 3f26967 | 2009-11-03 11:11:11 +0000 | [diff] [blame] | 169 | TIME_MSG("--- VIM STARTING ---"); |
| 170 | break; |
| 171 | } |
| 172 | } |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 173 | #endif |
Bram Moolenaar | ca003e1 | 2006-03-17 23:19:38 +0000 | [diff] [blame] | 174 | starttime = time(NULL); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 175 | |
Bram Moolenaar | 502ae4b | 2016-07-16 19:50:13 +0200 | [diff] [blame] | 176 | common_init(¶ms); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 177 | |
| 178 | #ifdef FEAT_CLIENTSERVER |
| 179 | /* |
| 180 | * Do the client-server stuff, unless "--servername ''" was used. |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 181 | * This may exit Vim if the command was sent to the server. |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 182 | */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 183 | exec_on_server(¶ms); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 184 | #endif |
| 185 | |
| 186 | /* |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 187 | * Figure out the way to work from the command name argv[0]. |
| 188 | * "vimdiff" starts diff mode, "rvim" sets "restricted", etc. |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 189 | */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 190 | parse_command_name(¶ms); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 191 | |
| 192 | /* |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 193 | * Process the command line arguments. File names are put in the global |
| 194 | * argument list "global_alist". |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 195 | */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 196 | command_line_scan(¶ms); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 197 | TIME_MSG("parsing arguments"); |
| 198 | |
| 199 | /* |
| 200 | * On some systems, when we compile with the GUI, we always use it. On Mac |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 201 | * there is no terminal version, and on Windows we can't fork one off with |
| 202 | * :gui. |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 203 | */ |
| 204 | #ifdef ALWAYS_USE_GUI |
| 205 | gui.starting = TRUE; |
| 206 | #else |
Bram Moolenaar | 241a8aa | 2005-12-06 20:04:44 +0000 | [diff] [blame] | 207 | # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 208 | /* |
| 209 | * Check if the GUI can be started. Reset gui.starting if not. |
| 210 | * Don't know about other systems, stay on the safe side and don't check. |
| 211 | */ |
Bram Moolenaar | 5d985b9 | 2009-12-16 17:28:07 +0000 | [diff] [blame] | 212 | if (gui.starting) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 213 | { |
Bram Moolenaar | 5d985b9 | 2009-12-16 17:28:07 +0000 | [diff] [blame] | 214 | if (gui_init_check() == FAIL) |
| 215 | { |
| 216 | gui.starting = FALSE; |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 217 | |
Bram Moolenaar | 5d985b9 | 2009-12-16 17:28:07 +0000 | [diff] [blame] | 218 | /* When running "evim" or "gvim -y" we need the menus, exit if we |
| 219 | * don't have them. */ |
| 220 | if (params.evim_mode) |
| 221 | mch_exit(1); |
| 222 | } |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 223 | } |
| 224 | # endif |
| 225 | #endif |
| 226 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 227 | if (GARGCOUNT > 0) |
| 228 | { |
Bram Moolenaar | 5307683 | 2015-12-31 19:53:21 +0100 | [diff] [blame] | 229 | #ifdef EXPAND_FILENAMES |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 230 | /* |
| 231 | * Expand wildcards in file names. |
| 232 | */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 233 | if (!params.literal) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 234 | { |
Bram Moolenaar | f630387 | 2015-04-03 17:59:43 +0200 | [diff] [blame] | 235 | start_dir = alloc(MAXPATHL); |
| 236 | if (start_dir != NULL) |
| 237 | mch_dirname(start_dir, MAXPATHL); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 238 | /* Temporarily add '(' and ')' to 'isfname'. These are valid |
| 239 | * filename characters but are excluded from 'isfname' to make |
| 240 | * "gf" work on a file name in parenthesis (e.g.: see vim.h). */ |
| 241 | do_cmdline_cmd((char_u *)":set isf+=(,)"); |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 242 | alist_expand(NULL, 0); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 243 | do_cmdline_cmd((char_u *)":set isf&"); |
Bram Moolenaar | f630387 | 2015-04-03 17:59:43 +0200 | [diff] [blame] | 244 | if (start_dir != NULL) |
| 245 | mch_chdir((char *)start_dir); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 246 | } |
| 247 | #endif |
Bram Moolenaar | a8e691d | 2016-08-07 15:19:26 +0200 | [diff] [blame] | 248 | params.fname = alist_name(&GARGLIST[0]); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 249 | } |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 250 | |
| 251 | #if defined(WIN32) && defined(FEAT_MBYTE) |
| 252 | { |
| 253 | extern void set_alist_count(void); |
| 254 | |
| 255 | /* Remember the number of entries in the argument list. If it changes |
| 256 | * we don't react on setting 'encoding'. */ |
| 257 | set_alist_count(); |
| 258 | } |
| 259 | #endif |
| 260 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 261 | #ifdef MSWIN |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 262 | if (GARGCOUNT == 1 && params.full_path) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 263 | { |
| 264 | /* |
| 265 | * If there is one filename, fully qualified, we have very probably |
| 266 | * been invoked from explorer, so change to the file's directory. |
| 267 | * Hint: to avoid this when typing a command use a forward slash. |
| 268 | * If the cd fails, it doesn't matter. |
| 269 | */ |
Bram Moolenaar | a8e691d | 2016-08-07 15:19:26 +0200 | [diff] [blame] | 270 | (void)vim_chdirfile(params.fname); |
Bram Moolenaar | f630387 | 2015-04-03 17:59:43 +0200 | [diff] [blame] | 271 | if (start_dir != NULL) |
| 272 | mch_dirname(start_dir, MAXPATHL); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 273 | } |
| 274 | #endif |
| 275 | TIME_MSG("expanding arguments"); |
| 276 | |
| 277 | #ifdef FEAT_DIFF |
Bram Moolenaar | 27dc195 | 2006-03-15 23:06:44 +0000 | [diff] [blame] | 278 | if (params.diff_mode && params.window_count == -1) |
| 279 | params.window_count = 0; /* open up to 3 windows */ |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 280 | #endif |
| 281 | |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 282 | /* Don't redraw until much later. */ |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 283 | ++RedrawingDisabled; |
| 284 | |
| 285 | /* |
| 286 | * When listing swap file names, don't do cursor positioning et. al. |
| 287 | */ |
Bram Moolenaar | a8e691d | 2016-08-07 15:19:26 +0200 | [diff] [blame] | 288 | if (recoverymode && params.fname == NULL) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 289 | params.want_full_screen = FALSE; |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 290 | |
| 291 | /* |
| 292 | * When certain to start the GUI, don't check capabilities of terminal. |
| 293 | * For GTK we can't be sure, but when started from the desktop it doesn't |
| 294 | * make sense to try using a terminal. |
| 295 | */ |
Bram Moolenaar | 241a8aa | 2005-12-06 20:04:44 +0000 | [diff] [blame] | 296 | #if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 297 | if (gui.starting |
| 298 | # ifdef FEAT_GUI_GTK |
| 299 | && !isatty(2) |
| 300 | # endif |
| 301 | ) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 302 | params.want_full_screen = FALSE; |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 303 | #endif |
| 304 | |
| 305 | #if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX) |
| 306 | /* When the GUI is started from Finder, need to display messages in a |
| 307 | * message box. isatty(2) returns TRUE anyway, thus we need to check the |
| 308 | * name to know we're not started from a terminal. */ |
| 309 | if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0)) |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 310 | { |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 311 | params.want_full_screen = FALSE; |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 312 | |
| 313 | /* Avoid always using "/" as the current directory. Note that when |
| 314 | * started from Finder the arglist will be filled later in |
| 315 | * HandleODocAE() and "fname" will be NULL. */ |
| 316 | if (getcwd((char *)NameBuff, MAXPATHL) != NULL |
| 317 | && STRCMP(NameBuff, "/") == 0) |
| 318 | { |
Bram Moolenaar | a8e691d | 2016-08-07 15:19:26 +0200 | [diff] [blame] | 319 | if (params.fname != NULL) |
| 320 | (void)vim_chdirfile(params.fname); |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 321 | else |
| 322 | { |
| 323 | expand_env((char_u *)"$HOME", NameBuff, MAXPATHL); |
| 324 | vim_chdir(NameBuff); |
| 325 | } |
Bram Moolenaar | f630387 | 2015-04-03 17:59:43 +0200 | [diff] [blame] | 326 | if (start_dir != NULL) |
| 327 | mch_dirname(start_dir, MAXPATHL); |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 330 | #endif |
| 331 | |
| 332 | /* |
| 333 | * mch_init() sets up the terminal (window) for use. This must be |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 334 | * done after resetting full_screen, otherwise it may move the cursor. |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 335 | * Note that we may use mch_exit() before mch_init()! |
| 336 | */ |
| 337 | mch_init(); |
| 338 | TIME_MSG("shell init"); |
| 339 | |
| 340 | #ifdef USE_XSMP |
| 341 | /* |
| 342 | * For want of anywhere else to do it, try to connect to xsmp here. |
| 343 | * Fitting it in after gui_mch_init, but before gui_init (via termcapinit). |
| 344 | * Hijacking -X 'no X connection' to also disable XSMP connection as that |
| 345 | * has a similar delay upon failure. |
| 346 | * Only try if SESSION_MANAGER is set to something non-null. |
| 347 | */ |
| 348 | if (!x_no_connect) |
| 349 | { |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 350 | char *p = getenv("SESSION_MANAGER"); |
| 351 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 352 | if (p != NULL && *p != NUL) |
| 353 | { |
| 354 | xsmp_init(); |
| 355 | TIME_MSG("xsmp init"); |
| 356 | } |
| 357 | } |
| 358 | #endif |
| 359 | |
| 360 | /* |
| 361 | * Print a warning if stdout is not a terminal. |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 362 | */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 363 | check_tty(¶ms); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 364 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 365 | /* This message comes before term inits, but after setting "silent_mode" |
| 366 | * when the input is not a tty. */ |
| 367 | if (GARGCOUNT > 1 && !silent_mode) |
| 368 | printf(_("%d files to edit\n"), GARGCOUNT); |
| 369 | |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 370 | if (params.want_full_screen && !silent_mode) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 371 | { |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 372 | termcapinit(params.term); /* set terminal name and get terminal |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 373 | capabilities (will set full_screen) */ |
| 374 | screen_start(); /* don't know where cursor is now */ |
| 375 | TIME_MSG("Termcap init"); |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * Set the default values for the options that use Rows and Columns. |
| 380 | */ |
| 381 | ui_get_shellsize(); /* inits Rows and Columns */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 382 | win_init_size(); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 383 | #ifdef FEAT_DIFF |
| 384 | /* Set the 'diff' option now, so that it can be checked for in a .vimrc |
| 385 | * file. There is no buffer yet though. */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 386 | if (params.diff_mode) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 387 | diff_win_options(firstwin, FALSE); |
| 388 | #endif |
| 389 | |
| 390 | cmdline_row = Rows - p_ch; |
| 391 | msg_row = cmdline_row; |
| 392 | screenalloc(FALSE); /* allocate screen buffers */ |
| 393 | set_init_2(); |
| 394 | TIME_MSG("inits 2"); |
| 395 | |
| 396 | msg_scroll = TRUE; |
| 397 | no_wait_return = TRUE; |
| 398 | |
| 399 | init_mappings(); /* set up initial mappings */ |
| 400 | |
| 401 | init_highlight(TRUE, FALSE); /* set the default highlight groups */ |
| 402 | TIME_MSG("init highlight"); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 403 | |
| 404 | #ifdef FEAT_EVAL |
| 405 | /* Set the break level after the terminal is initialized. */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 406 | debug_break_level = params.use_debug_break_level; |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 407 | #endif |
| 408 | |
Bram Moolenaar | bbc98db | 2012-02-12 01:55:55 +0100 | [diff] [blame] | 409 | #ifdef FEAT_MZSCHEME |
| 410 | /* |
| 411 | * Newer version of MzScheme (Racket) require earlier (trampolined) |
| 412 | * initialisation via scheme_main_setup. |
| 413 | * Implement this by initialising it as early as possible |
Bram Moolenaar | a8e691d | 2016-08-07 15:19:26 +0200 | [diff] [blame] | 414 | * and splitting off remaining Vim main into vim_main2(). |
Bram Moolenaar | bbc98db | 2012-02-12 01:55:55 +0100 | [diff] [blame] | 415 | */ |
Bram Moolenaar | a8e691d | 2016-08-07 15:19:26 +0200 | [diff] [blame] | 416 | return mzscheme_main(); |
| 417 | #else |
| 418 | return vim_main2(); |
Bram Moolenaar | 8866d27 | 2012-11-28 15:55:42 +0100 | [diff] [blame] | 419 | #endif |
Bram Moolenaar | a8e691d | 2016-08-07 15:19:26 +0200 | [diff] [blame] | 420 | } |
Bram Moolenaar | 8866d27 | 2012-11-28 15:55:42 +0100 | [diff] [blame] | 421 | #endif /* NO_VIM_MAIN */ |
Bram Moolenaar | a357e44 | 2016-08-10 20:45:07 +0200 | [diff] [blame] | 422 | #endif /* PROTO */ |
Bram Moolenaar | bbc98db | 2012-02-12 01:55:55 +0100 | [diff] [blame] | 423 | |
Bram Moolenaar | a8e691d | 2016-08-07 15:19:26 +0200 | [diff] [blame] | 424 | /* |
| 425 | * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep |
| 426 | * things simple. |
| 427 | * It is also defined when NO_VIM_MAIN is defined, but then it's empty. |
| 428 | */ |
Bram Moolenaar | 8866d27 | 2012-11-28 15:55:42 +0100 | [diff] [blame] | 429 | int |
Bram Moolenaar | a8e691d | 2016-08-07 15:19:26 +0200 | [diff] [blame] | 430 | vim_main2(void) |
Bram Moolenaar | bbc98db | 2012-02-12 01:55:55 +0100 | [diff] [blame] | 431 | { |
Bram Moolenaar | 8866d27 | 2012-11-28 15:55:42 +0100 | [diff] [blame] | 432 | #ifndef NO_VIM_MAIN |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 433 | /* Reset 'loadplugins' for "-u NONE" before "--cmd" arguments. |
| 434 | * Allows for setting 'loadplugins' there. */ |
| 435 | if (params.use_vimrc != NULL && STRCMP(params.use_vimrc, "NONE") == 0) |
| 436 | p_lpl = FALSE; |
| 437 | |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 438 | /* Execute --cmd arguments. */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 439 | exe_pre_commands(¶ms); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 440 | |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 441 | /* Source startup scripts. */ |
| 442 | source_startup_scripts(¶ms); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 443 | |
| 444 | #ifdef FEAT_EVAL |
| 445 | /* |
| 446 | * Read all the plugin files. |
| 447 | * Only when compiled with +eval, since most plugins need it. |
| 448 | */ |
| 449 | if (p_lpl) |
| 450 | { |
Bram Moolenaar | c1cb78c | 2006-06-20 16:51:47 +0000 | [diff] [blame] | 451 | # ifdef VMS /* Somehow VMS doesn't handle the "**". */ |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 452 | source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_NOAFTER); |
Bram Moolenaar | c1cb78c | 2006-06-20 16:51:47 +0000 | [diff] [blame] | 453 | # else |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 454 | source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_NOAFTER); |
Bram Moolenaar | c1cb78c | 2006-06-20 16:51:47 +0000 | [diff] [blame] | 455 | # endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 456 | TIME_MSG("loading plugins"); |
Bram Moolenaar | f6fee0e | 2016-02-21 23:02:49 +0100 | [diff] [blame] | 457 | |
Bram Moolenaar | 2d8f56a | 2016-03-12 20:34:27 +0100 | [diff] [blame] | 458 | ex_packloadall(NULL); |
Bram Moolenaar | f6fee0e | 2016-02-21 23:02:49 +0100 | [diff] [blame] | 459 | TIME_MSG("loading packages"); |
Bram Moolenaar | 66459b7 | 2016-08-06 19:01:55 +0200 | [diff] [blame] | 460 | |
| 461 | # ifdef VMS /* Somehow VMS doesn't handle the "**". */ |
| 462 | source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_AFTER); |
| 463 | # else |
| 464 | source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_AFTER); |
| 465 | # endif |
| 466 | TIME_MSG("loading after plugins"); |
| 467 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 468 | } |
| 469 | #endif |
| 470 | |
Bram Moolenaar | 27dc195 | 2006-03-15 23:06:44 +0000 | [diff] [blame] | 471 | #ifdef FEAT_DIFF |
| 472 | /* Decide about window layout for diff mode after reading vimrc. */ |
| 473 | if (params.diff_mode && params.window_layout == 0) |
| 474 | { |
| 475 | if (diffopt_horizontal()) |
| 476 | params.window_layout = WIN_HOR; /* use horizontal split */ |
| 477 | else |
| 478 | params.window_layout = WIN_VER; /* use vertical split */ |
| 479 | } |
| 480 | #endif |
| 481 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 482 | /* |
| 483 | * Recovery mode without a file name: List swap files. |
| 484 | * This uses the 'dir' option, therefore it must be after the |
| 485 | * initializations. |
| 486 | */ |
Bram Moolenaar | a8e691d | 2016-08-07 15:19:26 +0200 | [diff] [blame] | 487 | if (recoverymode && params.fname == NULL) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 488 | { |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 489 | recover_names(NULL, TRUE, 0, NULL); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 490 | mch_exit(0); |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * Set a few option defaults after reading .vimrc files: |
| 495 | * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'. |
| 496 | */ |
| 497 | set_init_3(); |
| 498 | TIME_MSG("inits 3"); |
| 499 | |
| 500 | /* |
| 501 | * "-n" argument: Disable swap file by setting 'updatecount' to 0. |
| 502 | * Note that this overrides anything from a vimrc file. |
| 503 | */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 504 | if (params.no_swap_file) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 505 | p_uc = 0; |
| 506 | |
| 507 | #ifdef FEAT_FKMAP |
| 508 | if (curwin->w_p_rl && p_altkeymap) |
| 509 | { |
| 510 | p_hkmap = FALSE; /* Reset the Hebrew keymap mode */ |
| 511 | # ifdef FEAT_ARABIC |
| 512 | curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */ |
| 513 | # endif |
| 514 | p_fkmap = TRUE; /* Set the Farsi keymap mode */ |
| 515 | } |
| 516 | #endif |
| 517 | |
| 518 | #ifdef FEAT_GUI |
| 519 | if (gui.starting) |
| 520 | { |
| 521 | #if defined(UNIX) || defined(VMS) |
| 522 | /* When something caused a message from a vimrc script, need to output |
| 523 | * an extra newline before the shell prompt. */ |
| 524 | if (did_emsg || msg_didout) |
| 525 | putchar('\n'); |
| 526 | #endif |
| 527 | |
| 528 | gui_start(); /* will set full_screen to TRUE */ |
| 529 | TIME_MSG("starting GUI"); |
| 530 | |
| 531 | /* When running "evim" or "gvim -y" we need the menus, exit if we |
| 532 | * don't have them. */ |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 533 | if (!gui.in_use && params.evim_mode) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 534 | mch_exit(1); |
| 535 | } |
| 536 | #endif |
| 537 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 538 | #ifdef FEAT_VIMINFO |
| 539 | /* |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 540 | * Read in registers, history etc, but not marks, from the viminfo file. |
| 541 | * This is where v:oldfiles gets filled. |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 542 | */ |
| 543 | if (*p_viminfo != NUL) |
| 544 | { |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 545 | read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 546 | TIME_MSG("reading viminfo"); |
| 547 | } |
| 548 | #endif |
Bram Moolenaar | 2cd3696 | 2014-01-14 12:57:05 +0100 | [diff] [blame] | 549 | #ifdef FEAT_EVAL |
| 550 | /* It's better to make v:oldfiles an empty list than NULL. */ |
| 551 | if (get_vim_var_list(VV_OLDFILES) == NULL) |
| 552 | set_vim_var_list(VV_OLDFILES, list_alloc()); |
| 553 | #endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 554 | |
| 555 | #ifdef FEAT_QUICKFIX |
| 556 | /* |
| 557 | * "-q errorfile": Load the error file now. |
| 558 | * If the error file can't be read, exit before doing anything else. |
| 559 | */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 560 | if (params.edit_type == EDIT_QF) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 561 | { |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 562 | if (params.use_ef != NULL) |
| 563 | set_string_option_direct((char_u *)"ef", -1, |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 564 | params.use_ef, OPT_FREE, SID_CARG); |
Bram Moolenaar | 7fd7320 | 2010-07-25 16:58:46 +0200 | [diff] [blame] | 565 | vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef); |
| 566 | if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 567 | { |
| 568 | out_char('\n'); |
| 569 | mch_exit(3); |
| 570 | } |
| 571 | TIME_MSG("reading errorfile"); |
| 572 | } |
| 573 | #endif |
| 574 | |
| 575 | /* |
| 576 | * Start putting things on the screen. |
| 577 | * Scroll screen down before drawing over it |
| 578 | * Clear screen now, so file message will not be cleared. |
| 579 | */ |
| 580 | starting = NO_BUFFERS; |
| 581 | no_wait_return = FALSE; |
| 582 | if (!exmode_active) |
| 583 | msg_scroll = FALSE; |
| 584 | |
| 585 | #ifdef FEAT_GUI |
| 586 | /* |
| 587 | * This seems to be required to make callbacks to be called now, instead |
| 588 | * of after things have been put on the screen, which then may be deleted |
| 589 | * when getting a resize callback. |
| 590 | * For the Mac this handles putting files dropped on the Vim icon to |
| 591 | * global_alist. |
| 592 | */ |
| 593 | if (gui.in_use) |
| 594 | { |
| 595 | # ifdef FEAT_SUN_WORKSHOP |
| 596 | if (!usingSunWorkShop) |
| 597 | # endif |
| 598 | gui_wait_for_chars(50L); |
| 599 | TIME_MSG("GUI delay"); |
| 600 | } |
| 601 | #endif |
| 602 | |
| 603 | #if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD) |
| 604 | qnx_clip_init(); |
| 605 | #endif |
| 606 | |
Bram Moolenaar | c8bbaa3 | 2010-07-14 16:54:21 +0200 | [diff] [blame] | 607 | #if defined(MACOS_X) && defined(FEAT_CLIPBOARD) |
| 608 | clip_init(TRUE); |
| 609 | #endif |
| 610 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 611 | #ifdef FEAT_XCLIPBOARD |
| 612 | /* Start using the X clipboard, unless the GUI was started. */ |
| 613 | # ifdef FEAT_GUI |
| 614 | if (!gui.in_use) |
| 615 | # endif |
| 616 | { |
| 617 | setup_term_clip(); |
| 618 | TIME_MSG("setup clipboard"); |
| 619 | } |
| 620 | #endif |
| 621 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 622 | #ifdef FEAT_CLIENTSERVER |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 623 | /* Prepare for being a Vim server. */ |
| 624 | prepare_server(¶ms); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 625 | #endif |
| 626 | |
| 627 | /* |
| 628 | * If "-" argument given: Read file from stdin. |
| 629 | * Do this before starting Raw mode, because it may change things that the |
| 630 | * writing end of the pipe doesn't like, e.g., in case stdin and stderr |
| 631 | * are the same terminal: "cat | vim -". |
| 632 | * Using autocommands here may cause trouble... |
| 633 | */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 634 | if (params.edit_type == EDIT_STDIN && !recoverymode) |
| 635 | read_stdin(); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 636 | |
| 637 | #if defined(UNIX) || defined(VMS) |
| 638 | /* When switching screens and something caused a message from a vimrc |
| 639 | * script, need to output an extra newline on exit. */ |
| 640 | if ((did_emsg || msg_didout) && *T_TI != NUL) |
| 641 | newline_on_exit = TRUE; |
| 642 | #endif |
| 643 | |
| 644 | /* |
| 645 | * When done something that is not allowed or error message call |
| 646 | * wait_return. This must be done before starttermcap(), because it may |
| 647 | * switch to another screen. It must be done after settmode(TMODE_RAW), |
| 648 | * because we want to react on a single key stroke. |
| 649 | * Call settmode and starttermcap here, so the T_KS and T_TI may be |
Bram Moolenaar | 4932594 | 2007-05-10 19:19:59 +0000 | [diff] [blame] | 650 | * defined by termcapinit and redefined in .exrc. |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 651 | */ |
| 652 | settmode(TMODE_RAW); |
| 653 | TIME_MSG("setting raw mode"); |
| 654 | |
| 655 | if (need_wait_return || msg_didany) |
| 656 | { |
| 657 | wait_return(TRUE); |
| 658 | TIME_MSG("waiting for return"); |
| 659 | } |
| 660 | |
| 661 | starttermcap(); /* start termcap if not done by wait_return() */ |
| 662 | TIME_MSG("start termcap"); |
Bram Moolenaar | b5c3265 | 2015-06-25 17:03:36 +0200 | [diff] [blame] | 663 | #if defined(FEAT_TERMRESPONSE) |
| 664 | # if defined(FEAT_MBYTE) |
Bram Moolenaar | 386dcde | 2013-09-29 16:27:47 +0200 | [diff] [blame] | 665 | may_req_ambiguous_char_width(); |
Bram Moolenaar | b5c3265 | 2015-06-25 17:03:36 +0200 | [diff] [blame] | 666 | # endif |
| 667 | may_req_bg_color(); |
Bram Moolenaar | 9584b31 | 2013-03-13 19:29:28 +0100 | [diff] [blame] | 668 | #endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 669 | |
| 670 | #ifdef FEAT_MOUSE |
| 671 | setmouse(); /* may start using the mouse */ |
| 672 | #endif |
| 673 | if (scroll_region) |
| 674 | scroll_region_reset(); /* In case Rows changed */ |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 675 | scroll_start(); /* may scroll the screen to the right position */ |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 676 | |
| 677 | /* |
| 678 | * Don't clear the screen when starting in Ex mode, unless using the GUI. |
| 679 | */ |
| 680 | if (exmode_active |
| 681 | #ifdef FEAT_GUI |
| 682 | && !gui.in_use |
| 683 | #endif |
| 684 | ) |
| 685 | must_redraw = CLEAR; |
| 686 | else |
| 687 | { |
| 688 | screenclear(); /* clear screen */ |
| 689 | TIME_MSG("clearing screen"); |
| 690 | } |
| 691 | |
| 692 | #ifdef FEAT_CRYPT |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 693 | if (params.ask_for_key) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 694 | { |
Bram Moolenaar | 3a0c908 | 2014-11-12 15:15:42 +0100 | [diff] [blame] | 695 | crypt_check_current_method(); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 696 | (void)crypt_get_key(TRUE, TRUE); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 697 | TIME_MSG("getting crypt key"); |
| 698 | } |
| 699 | #endif |
| 700 | |
| 701 | no_wait_return = TRUE; |
| 702 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 703 | /* |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 704 | * Create the requested number of windows and edit buffers in them. |
| 705 | * Also does recovery if "recoverymode" set. |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 706 | */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 707 | create_windows(¶ms); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 708 | TIME_MSG("opening buffers"); |
| 709 | |
Bram Moolenaar | 867a4b7 | 2007-03-18 20:51:46 +0000 | [diff] [blame] | 710 | #ifdef FEAT_EVAL |
| 711 | /* clear v:swapcommand */ |
| 712 | set_vim_var_string(VV_SWAPCOMMAND, NULL, -1); |
| 713 | #endif |
| 714 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 715 | /* Ex starts at last line of the file */ |
| 716 | if (exmode_active) |
| 717 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 718 | |
| 719 | #ifdef FEAT_AUTOCMD |
| 720 | apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf); |
| 721 | TIME_MSG("BufEnter autocommands"); |
| 722 | #endif |
| 723 | setpcmark(); |
| 724 | |
| 725 | #ifdef FEAT_QUICKFIX |
| 726 | /* |
| 727 | * When started with "-q errorfile" jump to first error now. |
| 728 | */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 729 | if (params.edit_type == EDIT_QF) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 730 | { |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 731 | qf_jump(NULL, 0, 0, FALSE); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 732 | TIME_MSG("jump to first error"); |
| 733 | } |
| 734 | #endif |
| 735 | |
| 736 | #ifdef FEAT_WINDOWS |
| 737 | /* |
| 738 | * If opened more than one window, start editing files in the other |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 739 | * windows. |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 740 | */ |
Bram Moolenaar | f630387 | 2015-04-03 17:59:43 +0200 | [diff] [blame] | 741 | edit_buffers(¶ms, start_dir); |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 742 | #endif |
Bram Moolenaar | f630387 | 2015-04-03 17:59:43 +0200 | [diff] [blame] | 743 | vim_free(start_dir); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 744 | |
| 745 | #ifdef FEAT_DIFF |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 746 | if (params.diff_mode) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 747 | { |
| 748 | win_T *wp; |
| 749 | |
| 750 | /* set options in each window for "vimdiff". */ |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 751 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 752 | diff_win_options(wp, TRUE); |
| 753 | } |
| 754 | #endif |
| 755 | |
| 756 | /* |
| 757 | * Shorten any of the filenames, but only when absolute. |
| 758 | */ |
| 759 | shorten_fnames(FALSE); |
| 760 | |
| 761 | /* |
| 762 | * Need to jump to the tag before executing the '-c command'. |
| 763 | * Makes "vim -c '/return' -t main" work. |
| 764 | */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 765 | if (params.tagname != NULL) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 766 | { |
Bram Moolenaar | 146522e | 2005-12-16 21:55:46 +0000 | [diff] [blame] | 767 | #if defined(HAS_SWAP_EXISTS_ACTION) |
| 768 | swap_exists_did_quit = FALSE; |
| 769 | #endif |
| 770 | |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 771 | vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 772 | do_cmdline_cmd(IObuff); |
| 773 | TIME_MSG("jumping to tag"); |
Bram Moolenaar | 146522e | 2005-12-16 21:55:46 +0000 | [diff] [blame] | 774 | |
| 775 | #if defined(HAS_SWAP_EXISTS_ACTION) |
| 776 | /* If the user doesn't want to edit the file then we quit here. */ |
| 777 | if (swap_exists_did_quit) |
| 778 | getout(1); |
| 779 | #endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 782 | /* Execute any "+", "-c" and "-S" arguments. */ |
| 783 | if (params.n_commands > 0) |
| 784 | exe_commands(¶ms); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 785 | |
| 786 | RedrawingDisabled = 0; |
| 787 | redraw_all_later(NOT_VALID); |
| 788 | no_wait_return = FALSE; |
| 789 | starting = 0; |
| 790 | |
Bram Moolenaar | baec5c1 | 2016-04-06 23:06:23 +0200 | [diff] [blame] | 791 | /* 'autochdir' has been postponed */ |
| 792 | DO_AUTOCHDIR |
| 793 | |
Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 794 | #ifdef FEAT_TERMRESPONSE |
| 795 | /* Requesting the termresponse is postponed until here, so that a "-c q" |
| 796 | * argument doesn't make it appear in the shell Vim was started from. */ |
| 797 | may_req_termresponse(); |
| 798 | #endif |
| 799 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 800 | /* start in insert mode */ |
| 801 | if (p_im) |
| 802 | need_start_insertmode = TRUE; |
| 803 | |
Bram Moolenaar | 1473551 | 2016-03-26 21:00:08 +0100 | [diff] [blame] | 804 | #ifdef FEAT_EVAL |
| 805 | set_vim_var_nr(VV_VIM_DID_ENTER, 1L); |
| 806 | #endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 807 | #ifdef FEAT_AUTOCMD |
| 808 | apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf); |
| 809 | TIME_MSG("VimEnter autocommands"); |
| 810 | #endif |
| 811 | |
Bram Moolenaar | b429cde | 2012-04-25 18:24:29 +0200 | [diff] [blame] | 812 | #if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD) |
| 813 | /* Adjust default register name for "unnamed" in 'clipboard'. Can only be |
| 814 | * done after the clipboard is available and all initial commands that may |
| 815 | * modify the 'clipboard' setting have run; i.e. just before entering the |
| 816 | * main loop. */ |
| 817 | { |
| 818 | int default_regname = 0; |
Bram Moolenaar | 5307683 | 2015-12-31 19:53:21 +0100 | [diff] [blame] | 819 | |
Bram Moolenaar | b429cde | 2012-04-25 18:24:29 +0200 | [diff] [blame] | 820 | adjust_clip_reg(&default_regname); |
| 821 | set_reg_var(default_regname); |
| 822 | } |
| 823 | #endif |
| 824 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 825 | #if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND) |
| 826 | /* When a startup script or session file setup for diff'ing and |
| 827 | * scrollbind, sync the scrollbind now. */ |
| 828 | if (curwin->w_p_diff && curwin->w_p_scb) |
| 829 | { |
| 830 | update_topline(); |
| 831 | check_scrollbind((linenr_T)0, 0L); |
| 832 | TIME_MSG("diff scrollbinding"); |
| 833 | } |
| 834 | #endif |
| 835 | |
| 836 | #if defined(WIN3264) && !defined(FEAT_GUI_W32) |
| 837 | mch_set_winsize_now(); /* Allow winsize changes from now on */ |
| 838 | #endif |
| 839 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 840 | #if defined(FEAT_GUI) && defined(FEAT_WINDOWS) |
| 841 | /* When tab pages were created, may need to update the tab pages line and |
| 842 | * scrollbars. This is skipped while creating them. */ |
| 843 | if (first_tabpage->tp_next != NULL) |
| 844 | { |
| 845 | out_flush(); |
| 846 | gui_init_which_components(NULL); |
| 847 | gui_update_scrollbars(TRUE); |
| 848 | } |
| 849 | need_mouse_correct = TRUE; |
| 850 | #endif |
| 851 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 852 | /* If ":startinsert" command used, stuff a dummy command to be able to |
| 853 | * call normal_cmd(), which will then start Insert mode. */ |
| 854 | if (restart_edit != 0) |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 855 | stuffcharReadbuff(K_NOP); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 856 | |
| 857 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 858 | if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0) |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 859 | { |
| 860 | # ifdef FEAT_GUI |
Bram Moolenaar | 173c985 | 2010-09-29 17:27:01 +0200 | [diff] [blame] | 861 | # if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \ |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 862 | && !defined(FEAT_GUI_W32) |
| 863 | if (gui.in_use) |
| 864 | { |
| 865 | mch_errmsg(_("netbeans is not supported with this GUI\n")); |
| 866 | mch_exit(2); |
| 867 | } |
| 868 | # endif |
| 869 | # endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 870 | /* Tell the client that it can start sending commands. */ |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 871 | netbeans_open(netbeansArg + 3, TRUE); |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 872 | } |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 873 | #endif |
| 874 | |
| 875 | TIME_MSG("before starting main loop"); |
| 876 | |
| 877 | /* |
| 878 | * Call the main command loop. This never returns. |
Bram Moolenaar | bbc98db | 2012-02-12 01:55:55 +0100 | [diff] [blame] | 879 | */ |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 880 | main_loop(FALSE, FALSE); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 881 | |
Bram Moolenaar | a8e691d | 2016-08-07 15:19:26 +0200 | [diff] [blame] | 882 | #endif /* NO_VIM_MAIN */ |
| 883 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 884 | return 0; |
| 885 | } |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 886 | |
| 887 | /* |
Bram Moolenaar | 502ae4b | 2016-07-16 19:50:13 +0200 | [diff] [blame] | 888 | * Initialisation shared by main() and some tests. |
| 889 | */ |
| 890 | void |
Bram Moolenaar | a8e691d | 2016-08-07 15:19:26 +0200 | [diff] [blame] | 891 | common_init(mparm_T *paramp) |
Bram Moolenaar | 502ae4b | 2016-07-16 19:50:13 +0200 | [diff] [blame] | 892 | { |
| 893 | |
| 894 | #ifdef FEAT_MBYTE |
| 895 | (void)mb_init(); /* init mb_bytelen_tab[] to ones */ |
| 896 | #endif |
| 897 | #ifdef FEAT_EVAL |
| 898 | eval_init(); /* init global variables */ |
| 899 | #endif |
| 900 | |
| 901 | #ifdef __QNXNTO__ |
| 902 | qnx_init(); /* PhAttach() for clipboard, (and gui) */ |
| 903 | #endif |
| 904 | |
| 905 | #ifdef MAC_OS_CLASSIC |
| 906 | /* Prepare for possibly starting GUI sometime */ |
| 907 | /* Macintosh needs this before any memory is allocated. */ |
Bram Moolenaar | a8e691d | 2016-08-07 15:19:26 +0200 | [diff] [blame] | 908 | gui_prepare(¶mp->argc, paramp->argv); |
Bram Moolenaar | 502ae4b | 2016-07-16 19:50:13 +0200 | [diff] [blame] | 909 | TIME_MSG("GUI prepared"); |
| 910 | #endif |
| 911 | |
| 912 | /* Init the table of Normal mode commands. */ |
| 913 | init_normal_cmds(); |
| 914 | |
| 915 | #if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC) |
| 916 | make_version(); /* Construct the long version string. */ |
| 917 | #endif |
| 918 | |
| 919 | /* |
| 920 | * Allocate space for the generic buffers (needed for set_init_1() and |
| 921 | * EMSG2()). |
| 922 | */ |
| 923 | if ((IObuff = alloc(IOSIZE)) == NULL |
| 924 | || (NameBuff = alloc(MAXPATHL)) == NULL) |
| 925 | mch_exit(0); |
| 926 | TIME_MSG("Allocated generic buffers"); |
| 927 | |
| 928 | #ifdef NBDEBUG |
| 929 | /* Wait a moment for debugging NetBeans. Must be after allocating |
| 930 | * NameBuff. */ |
| 931 | nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL"); |
| 932 | nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20); |
| 933 | TIME_MSG("NetBeans debug wait"); |
| 934 | #endif |
| 935 | |
| 936 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 937 | /* |
| 938 | * Setup to use the current locale (for ctype() and many other things). |
| 939 | * NOTE: Translated messages with encodings other than latin1 will not |
| 940 | * work until set_init_1() has been called! |
| 941 | */ |
| 942 | init_locale(); |
| 943 | TIME_MSG("locale set"); |
| 944 | #endif |
| 945 | |
| 946 | #ifdef FEAT_GUI |
| 947 | gui.dofork = TRUE; /* default is to use fork() */ |
| 948 | #endif |
| 949 | |
| 950 | /* |
| 951 | * Do a first scan of the arguments in "argv[]": |
| 952 | * -display or --display |
| 953 | * --server... |
| 954 | * --socketid |
| 955 | * --windowid |
| 956 | */ |
Bram Moolenaar | a8e691d | 2016-08-07 15:19:26 +0200 | [diff] [blame] | 957 | early_arg_scan(paramp); |
Bram Moolenaar | 502ae4b | 2016-07-16 19:50:13 +0200 | [diff] [blame] | 958 | |
| 959 | #ifdef FEAT_SUN_WORKSHOP |
Bram Moolenaar | a8e691d | 2016-08-07 15:19:26 +0200 | [diff] [blame] | 960 | findYourself(paramp->argv[0]); |
Bram Moolenaar | 502ae4b | 2016-07-16 19:50:13 +0200 | [diff] [blame] | 961 | #endif |
| 962 | #if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC) |
| 963 | /* Prepare for possibly starting GUI sometime */ |
Bram Moolenaar | a8e691d | 2016-08-07 15:19:26 +0200 | [diff] [blame] | 964 | gui_prepare(¶mp->argc, paramp->argv); |
Bram Moolenaar | 502ae4b | 2016-07-16 19:50:13 +0200 | [diff] [blame] | 965 | TIME_MSG("GUI prepared"); |
| 966 | #endif |
| 967 | |
| 968 | #ifdef FEAT_CLIPBOARD |
| 969 | clip_init(FALSE); /* Initialise clipboard stuff */ |
| 970 | TIME_MSG("clipboard setup"); |
| 971 | #endif |
| 972 | |
| 973 | /* |
| 974 | * Check if we have an interactive window. |
| 975 | * On the Amiga: If there is no window, we open one with a newcli command |
| 976 | * (needed for :! to * work). mch_check_win() will also handle the -d or |
| 977 | * -dev argument. |
| 978 | */ |
Bram Moolenaar | 2cab0e1 | 2016-11-24 15:09:07 +0100 | [diff] [blame] | 979 | stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL); |
Bram Moolenaar | 502ae4b | 2016-07-16 19:50:13 +0200 | [diff] [blame] | 980 | TIME_MSG("window checked"); |
| 981 | |
| 982 | /* |
| 983 | * Allocate the first window and buffer. |
| 984 | * Can't do anything without it, exit when it fails. |
| 985 | */ |
| 986 | if (win_alloc_first() == FAIL) |
| 987 | mch_exit(0); |
| 988 | |
| 989 | init_yank(); /* init yank buffers */ |
| 990 | |
| 991 | alist_init(&global_alist); /* Init the argument list to empty. */ |
| 992 | global_alist.id = 0; |
| 993 | |
| 994 | /* |
| 995 | * Set the default values for the options. |
| 996 | * NOTE: Non-latin1 translated messages are working only after this, |
| 997 | * because this is where "has_mbyte" will be set, which is used by |
| 998 | * msg_outtrans_len_attr(). |
| 999 | * First find out the home directory, needed to expand "~" in options. |
| 1000 | */ |
| 1001 | init_homedir(); /* find real value of $HOME */ |
| 1002 | set_init_1(); |
| 1003 | TIME_MSG("inits 1"); |
| 1004 | |
| 1005 | #ifdef FEAT_EVAL |
| 1006 | set_lang_var(); /* set v:lang and v:ctype */ |
| 1007 | #endif |
| 1008 | } |
| 1009 | |
| 1010 | /* |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1011 | * Main loop: Execute Normal mode commands until exiting Vim. |
| 1012 | * Also used to handle commands in the command-line window, until the window |
| 1013 | * is closed. |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1014 | * Also used to handle ":visual" command after ":global": execute Normal mode |
| 1015 | * commands, return when entering Ex mode. "noexmode" is TRUE then. |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1016 | */ |
| 1017 | void |
Bram Moolenaar | b7604cc | 2016-01-15 21:23:22 +0100 | [diff] [blame] | 1018 | main_loop( |
| 1019 | int cmdwin, /* TRUE when working in the command-line window */ |
| 1020 | int noexmode) /* TRUE when return on entering Ex mode */ |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1021 | { |
Bram Moolenaar | 225d32b | 2007-08-10 19:33:47 +0000 | [diff] [blame] | 1022 | oparg_T oa; /* operator arguments */ |
Bram Moolenaar | 8872ef1 | 2015-02-10 19:27:05 +0100 | [diff] [blame] | 1023 | volatile int previous_got_int = FALSE; /* "got_int" was TRUE */ |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1024 | #ifdef FEAT_CONCEAL |
Bram Moolenaar | 1db43b1 | 2015-07-12 16:21:23 +0200 | [diff] [blame] | 1025 | /* these are static to avoid a compiler warning */ |
| 1026 | static linenr_T conceal_old_cursor_line = 0; |
| 1027 | static linenr_T conceal_new_cursor_line = 0; |
| 1028 | static int conceal_update_lines = FALSE; |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1029 | #endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1030 | |
| 1031 | #if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD) |
| 1032 | /* Setup to catch a terminating error from the X server. Just ignore |
| 1033 | * it, restore the state and continue. This might not always work |
| 1034 | * properly, but at least we don't exit unexpectedly when the X server |
Bram Moolenaar | 2cd3696 | 2014-01-14 12:57:05 +0100 | [diff] [blame] | 1035 | * exits while Vim is running in a console. */ |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1036 | if (!cmdwin && !noexmode && SETJMP(x_jump_env)) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1037 | { |
| 1038 | State = NORMAL; |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1039 | VIsual_active = FALSE; |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1040 | got_int = TRUE; |
| 1041 | need_wait_return = FALSE; |
| 1042 | global_busy = FALSE; |
| 1043 | exmode_active = 0; |
| 1044 | skip_redraw = FALSE; |
| 1045 | RedrawingDisabled = 0; |
| 1046 | no_wait_return = 0; |
Bram Moolenaar | 7701c24 | 2011-10-04 16:43:53 +0200 | [diff] [blame] | 1047 | vgetc_busy = 0; |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1048 | # ifdef FEAT_EVAL |
| 1049 | emsg_skip = 0; |
| 1050 | # endif |
| 1051 | emsg_off = 0; |
| 1052 | # ifdef FEAT_MOUSE |
| 1053 | setmouse(); |
| 1054 | # endif |
| 1055 | settmode(TMODE_RAW); |
| 1056 | starttermcap(); |
| 1057 | scroll_start(); |
| 1058 | redraw_later_clear(); |
| 1059 | } |
| 1060 | #endif |
| 1061 | |
| 1062 | clear_oparg(&oa); |
| 1063 | while (!cmdwin |
| 1064 | #ifdef FEAT_CMDWIN |
| 1065 | || cmdwin_result == 0 |
| 1066 | #endif |
| 1067 | ) |
| 1068 | { |
| 1069 | if (stuff_empty()) |
| 1070 | { |
| 1071 | did_check_timestamps = FALSE; |
| 1072 | if (need_check_timestamps) |
| 1073 | check_timestamps(FALSE); |
| 1074 | if (need_wait_return) /* if wait_return still needed ... */ |
| 1075 | wait_return(FALSE); /* ... call it now */ |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 1076 | if (need_start_insertmode && goto_im() && !VIsual_active) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1077 | { |
| 1078 | need_start_insertmode = FALSE; |
| 1079 | stuffReadbuff((char_u *)"i"); /* start insert mode next */ |
| 1080 | /* skip the fileinfo message now, because it would be shown |
| 1081 | * after insert mode finishes! */ |
| 1082 | need_fileinfo = FALSE; |
| 1083 | } |
| 1084 | } |
Bram Moolenaar | 225d32b | 2007-08-10 19:33:47 +0000 | [diff] [blame] | 1085 | |
| 1086 | /* Reset "got_int" now that we got back to the main loop. Except when |
| 1087 | * inside a ":g/pat/cmd" command, then the "got_int" needs to abort |
| 1088 | * the ":g" command. |
| 1089 | * For ":g/pat/vi" we reset "got_int" when used once. When used |
| 1090 | * a second time we go back to Ex mode and abort the ":g" command. */ |
| 1091 | if (got_int) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1092 | { |
Bram Moolenaar | 225d32b | 2007-08-10 19:33:47 +0000 | [diff] [blame] | 1093 | if (noexmode && global_busy && !exmode_active && previous_got_int) |
| 1094 | { |
| 1095 | /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was |
| 1096 | * used and keep "got_int" set, so that it aborts ":g". */ |
| 1097 | exmode_active = EXMODE_NORMAL; |
| 1098 | State = NORMAL; |
| 1099 | } |
| 1100 | else if (!global_busy || !exmode_active) |
| 1101 | { |
| 1102 | if (!quit_more) |
| 1103 | (void)vgetc(); /* flush all buffers */ |
| 1104 | got_int = FALSE; |
| 1105 | } |
| 1106 | previous_got_int = TRUE; |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1107 | } |
Bram Moolenaar | 225d32b | 2007-08-10 19:33:47 +0000 | [diff] [blame] | 1108 | else |
| 1109 | previous_got_int = FALSE; |
| 1110 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1111 | if (!exmode_active) |
| 1112 | msg_scroll = FALSE; |
| 1113 | quit_more = FALSE; |
| 1114 | |
| 1115 | /* |
| 1116 | * If skip redraw is set (for ":" in wait_return()), don't redraw now. |
| 1117 | * If there is nothing in the stuff_buffer or do_redraw is TRUE, |
| 1118 | * update cursor and redraw. |
| 1119 | */ |
| 1120 | if (skip_redraw || exmode_active) |
| 1121 | skip_redraw = FALSE; |
| 1122 | else if (do_redraw || stuff_empty()) |
| 1123 | { |
Bram Moolenaar | 6b40f30 | 2017-02-03 22:01:47 +0100 | [diff] [blame] | 1124 | # ifdef FEAT_GUI |
| 1125 | /* If ui_breakcheck() was used a resize may have been postponed. */ |
| 1126 | gui_may_resize_shell(); |
| 1127 | # endif |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1128 | #if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL) |
Bram Moolenaar | 3d0a603 | 2006-02-09 23:54:54 +0000 | [diff] [blame] | 1129 | /* Trigger CursorMoved if the cursor moved. */ |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1130 | if (!finish_op && ( |
| 1131 | # ifdef FEAT_AUTOCMD |
| 1132 | has_cursormoved() |
| 1133 | # endif |
| 1134 | # if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL) |
| 1135 | || |
| 1136 | # endif |
| 1137 | # ifdef FEAT_CONCEAL |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 1138 | curwin->w_p_cole > 0 |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1139 | # endif |
| 1140 | ) |
Bram Moolenaar | d1413d9 | 2016-03-02 21:51:56 +0100 | [diff] [blame] | 1141 | # ifdef FEAT_AUTOCMD |
| 1142 | && !equalpos(last_cursormoved, curwin->w_cursor) |
| 1143 | # endif |
| 1144 | ) |
Bram Moolenaar | 3d0a603 | 2006-02-09 23:54:54 +0000 | [diff] [blame] | 1145 | { |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1146 | # ifdef FEAT_AUTOCMD |
| 1147 | if (has_cursormoved()) |
| 1148 | apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, |
| 1149 | FALSE, curbuf); |
| 1150 | # endif |
| 1151 | # ifdef FEAT_CONCEAL |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 1152 | if (curwin->w_p_cole > 0) |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1153 | { |
Bram Moolenaar | d1413d9 | 2016-03-02 21:51:56 +0100 | [diff] [blame] | 1154 | # ifdef FEAT_AUTOCMD |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1155 | conceal_old_cursor_line = last_cursormoved.lnum; |
Bram Moolenaar | d1413d9 | 2016-03-02 21:51:56 +0100 | [diff] [blame] | 1156 | # endif |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1157 | conceal_new_cursor_line = curwin->w_cursor.lnum; |
| 1158 | conceal_update_lines = TRUE; |
| 1159 | } |
| 1160 | # endif |
Bram Moolenaar | d1413d9 | 2016-03-02 21:51:56 +0100 | [diff] [blame] | 1161 | # ifdef FEAT_AUTOCMD |
Bram Moolenaar | 3d0a603 | 2006-02-09 23:54:54 +0000 | [diff] [blame] | 1162 | last_cursormoved = curwin->w_cursor; |
Bram Moolenaar | d1413d9 | 2016-03-02 21:51:56 +0100 | [diff] [blame] | 1163 | # endif |
Bram Moolenaar | 3d0a603 | 2006-02-09 23:54:54 +0000 | [diff] [blame] | 1164 | } |
| 1165 | #endif |
| 1166 | |
Bram Moolenaar | 186628f | 2013-03-19 13:33:23 +0100 | [diff] [blame] | 1167 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 1168 | /* Trigger TextChanged if b:changedtick differs. */ |
Bram Moolenaar | 186628f | 2013-03-19 13:33:23 +0100 | [diff] [blame] | 1169 | if (!finish_op && has_textchanged() |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 1170 | && last_changedtick != CHANGEDTICK(curbuf)) |
Bram Moolenaar | 186628f | 2013-03-19 13:33:23 +0100 | [diff] [blame] | 1171 | { |
| 1172 | if (last_changedtick_buf == curbuf) |
| 1173 | apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL, |
| 1174 | FALSE, curbuf); |
| 1175 | last_changedtick_buf = curbuf; |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 1176 | last_changedtick = CHANGEDTICK(curbuf); |
Bram Moolenaar | 186628f | 2013-03-19 13:33:23 +0100 | [diff] [blame] | 1177 | } |
| 1178 | #endif |
| 1179 | |
Bram Moolenaar | 33aec76 | 2006-01-22 23:30:12 +0000 | [diff] [blame] | 1180 | #if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND) |
| 1181 | /* Scroll-binding for diff mode may have been postponed until |
| 1182 | * here. Avoids doing it for every change. */ |
| 1183 | if (diff_need_scrollbind) |
| 1184 | { |
| 1185 | check_scrollbind((linenr_T)0, 0L); |
| 1186 | diff_need_scrollbind = FALSE; |
| 1187 | } |
| 1188 | #endif |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 1189 | #if defined(FEAT_FOLDING) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1190 | /* Include a closed fold completely in the Visual area. */ |
| 1191 | foldAdjustVisual(); |
| 1192 | #endif |
| 1193 | #ifdef FEAT_FOLDING |
| 1194 | /* |
| 1195 | * When 'foldclose' is set, apply 'foldlevel' to folds that don't |
| 1196 | * contain the cursor. |
| 1197 | * When 'foldopen' is "all", open the fold(s) under the cursor. |
| 1198 | * This may mark the window for redrawing. |
| 1199 | */ |
| 1200 | if (hasAnyFolding(curwin) && !char_avail()) |
| 1201 | { |
| 1202 | foldCheckClose(); |
| 1203 | if (fdo_flags & FDO_ALL) |
| 1204 | foldOpenCursor(); |
| 1205 | } |
| 1206 | #endif |
| 1207 | |
| 1208 | /* |
| 1209 | * Before redrawing, make sure w_topline is correct, and w_leftcol |
| 1210 | * if lines don't wrap, and w_skipcol if lines wrap. |
| 1211 | */ |
| 1212 | update_topline(); |
| 1213 | validate_cursor(); |
| 1214 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1215 | if (VIsual_active) |
| 1216 | update_curbuf(INVERTED);/* update inverted part */ |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 1217 | else if (must_redraw) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1218 | update_screen(0); |
| 1219 | else if (redraw_cmdline || clear_cmdline) |
| 1220 | showmode(); |
| 1221 | #ifdef FEAT_WINDOWS |
| 1222 | redraw_statuslines(); |
| 1223 | #endif |
| 1224 | #ifdef FEAT_TITLE |
| 1225 | if (need_maketitle) |
| 1226 | maketitle(); |
| 1227 | #endif |
Bram Moolenaar | ab9c89b | 2016-07-03 17:47:26 +0200 | [diff] [blame] | 1228 | #ifdef FEAT_VIMINFO |
| 1229 | curbuf->b_last_used = vim_time(); |
| 1230 | #endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1231 | /* display message after redraw */ |
| 1232 | if (keep_msg != NULL) |
| 1233 | { |
| 1234 | char_u *p; |
| 1235 | |
| 1236 | /* msg_attr_keep() will set keep_msg to NULL, must free the |
Bram Moolenaar | f638cbc | 2014-09-09 17:47:38 +0200 | [diff] [blame] | 1237 | * string here. Don't reset keep_msg, msg_attr_keep() uses it |
| 1238 | * to check for duplicates. */ |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1239 | p = keep_msg; |
| 1240 | msg_attr(p, keep_msg_attr); |
| 1241 | vim_free(p); |
| 1242 | } |
| 1243 | if (need_fileinfo) /* show file info after redraw */ |
| 1244 | { |
| 1245 | fileinfo(FALSE, TRUE, FALSE); |
| 1246 | need_fileinfo = FALSE; |
| 1247 | } |
| 1248 | |
| 1249 | emsg_on_display = FALSE; /* can delete error message now */ |
| 1250 | did_emsg = FALSE; |
| 1251 | msg_didany = FALSE; /* reset lines_left in msg_start() */ |
Bram Moolenaar | 661b182 | 2005-07-28 22:36:45 +0000 | [diff] [blame] | 1252 | may_clear_sb_text(); /* clear scroll-back text on next msg */ |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1253 | showruler(FALSE); |
| 1254 | |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1255 | # if defined(FEAT_CONCEAL) |
| 1256 | if (conceal_update_lines |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 1257 | && (conceal_old_cursor_line != conceal_new_cursor_line |
| 1258 | || conceal_cursor_line(curwin) |
| 1259 | || need_cursor_line_redraw)) |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1260 | { |
Bram Moolenaar | c2b4c62 | 2011-02-15 16:29:59 +0100 | [diff] [blame] | 1261 | if (conceal_old_cursor_line != conceal_new_cursor_line |
| 1262 | && conceal_old_cursor_line |
| 1263 | <= curbuf->b_ml.ml_line_count) |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 1264 | update_single_line(curwin, conceal_old_cursor_line); |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1265 | update_single_line(curwin, conceal_new_cursor_line); |
| 1266 | curwin->w_valid &= ~VALID_CROW; |
| 1267 | } |
| 1268 | # endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1269 | setcursor(); |
| 1270 | cursor_on(); |
| 1271 | |
| 1272 | do_redraw = FALSE; |
Bram Moolenaar | 3f26967 | 2009-11-03 11:11:11 +0000 | [diff] [blame] | 1273 | |
| 1274 | #ifdef STARTUPTIME |
| 1275 | /* Now that we have drawn the first screen all the startup stuff |
| 1276 | * has been done, close any file for startup messages. */ |
| 1277 | if (time_fd != NULL) |
| 1278 | { |
| 1279 | TIME_MSG("first screen update"); |
| 1280 | TIME_MSG("--- VIM STARTED ---"); |
| 1281 | fclose(time_fd); |
| 1282 | time_fd = NULL; |
| 1283 | } |
| 1284 | #endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1285 | } |
| 1286 | #ifdef FEAT_GUI |
| 1287 | if (need_mouse_correct) |
| 1288 | gui_mouse_correct(); |
| 1289 | #endif |
| 1290 | |
| 1291 | /* |
| 1292 | * Update w_curswant if w_set_curswant has been set. |
| 1293 | * Postponed until here to avoid computing w_virtcol too often. |
| 1294 | */ |
| 1295 | update_curswant(); |
| 1296 | |
Bram Moolenaar | 9fecb46 | 2006-09-05 10:59:47 +0000 | [diff] [blame] | 1297 | #ifdef FEAT_EVAL |
| 1298 | /* |
| 1299 | * May perform garbage collection when waiting for a character, but |
| 1300 | * only at the very toplevel. Otherwise we may be using a List or |
| 1301 | * Dict internally somewhere. |
| 1302 | * "may_garbage_collect" is reset in vgetc() which is invoked through |
| 1303 | * do_exmode() and normal_cmd(). |
| 1304 | */ |
| 1305 | may_garbage_collect = (!cmdwin && !noexmode); |
| 1306 | #endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1307 | /* |
| 1308 | * If we're invoked as ex, do a round of ex commands. |
| 1309 | * Otherwise, get and execute a normal mode command. |
| 1310 | */ |
| 1311 | if (exmode_active) |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1312 | { |
| 1313 | if (noexmode) /* End of ":global/path/visual" commands */ |
| 1314 | return; |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1315 | do_exmode(exmode_active == EXMODE_VIM); |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1316 | } |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1317 | else |
| 1318 | normal_cmd(&oa, TRUE); |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | |
Bram Moolenaar | 6d8d849 | 2016-03-19 14:48:31 +0100 | [diff] [blame] | 1323 | #if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1324 | /* |
| 1325 | * Exit, but leave behind swap files for modified buffers. |
| 1326 | */ |
| 1327 | void |
Bram Moolenaar | b7604cc | 2016-01-15 21:23:22 +0100 | [diff] [blame] | 1328 | getout_preserve_modified(int exitval) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1329 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1330 | # if defined(SIGHUP) && defined(SIG_IGN) |
| 1331 | /* Ignore SIGHUP, because a dropped connection causes a read error, which |
| 1332 | * makes Vim exit and then handling SIGHUP causes various reentrance |
| 1333 | * problems. */ |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1334 | signal(SIGHUP, SIG_IGN); |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1335 | # endif |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 1336 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1337 | ml_close_notmod(); /* close all not-modified buffers */ |
| 1338 | ml_sync_all(FALSE, FALSE); /* preserve all swap files */ |
| 1339 | ml_close_all(FALSE); /* close all memfiles, without deleting */ |
| 1340 | getout(exitval); /* exit Vim properly */ |
| 1341 | } |
| 1342 | #endif |
| 1343 | |
| 1344 | |
Bram Moolenaar | 6d8d849 | 2016-03-19 14:48:31 +0100 | [diff] [blame] | 1345 | /* |
| 1346 | * Exit properly. |
| 1347 | */ |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1348 | void |
Bram Moolenaar | b7604cc | 2016-01-15 21:23:22 +0100 | [diff] [blame] | 1349 | getout(int exitval) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1350 | { |
| 1351 | #ifdef FEAT_AUTOCMD |
| 1352 | buf_T *buf; |
| 1353 | win_T *wp; |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 1354 | tabpage_T *tp, *next_tp; |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1355 | #endif |
| 1356 | |
| 1357 | exiting = TRUE; |
| 1358 | |
Bram Moolenaar | 5313dcb | 2005-02-22 08:56:13 +0000 | [diff] [blame] | 1359 | /* When running in Ex mode an error causes us to exit with a non-zero exit |
| 1360 | * code. POSIX requires this, although it's not 100% clear from the |
| 1361 | * standard. */ |
| 1362 | if (exmode_active) |
| 1363 | exitval += ex_exitval; |
| 1364 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1365 | /* Position the cursor on the last screen line, below all the text */ |
| 1366 | #ifdef FEAT_GUI |
| 1367 | if (!gui.in_use) |
| 1368 | #endif |
| 1369 | windgoto((int)Rows - 1, 0); |
| 1370 | |
Bram Moolenaar | 0e21a3f | 2005-04-17 20:28:32 +0000 | [diff] [blame] | 1371 | #if defined(FEAT_EVAL) || defined(FEAT_SYN_HL) |
| 1372 | /* Optionally print hashtable efficiency. */ |
| 1373 | hash_debug_results(); |
| 1374 | #endif |
| 1375 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1376 | #ifdef FEAT_GUI |
| 1377 | msg_didany = FALSE; |
| 1378 | #endif |
| 1379 | |
| 1380 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 0e1e25f | 2010-05-28 21:07:08 +0200 | [diff] [blame] | 1381 | if (get_vim_var_nr(VV_DYING) <= 1) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1382 | { |
Bram Moolenaar | 0e1e25f | 2010-05-28 21:07:08 +0200 | [diff] [blame] | 1383 | /* Trigger BufWinLeave for all windows, but only once per buffer. */ |
| 1384 | # if defined FEAT_WINDOWS |
| 1385 | for (tp = first_tabpage; tp != NULL; tp = next_tp) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1386 | { |
Bram Moolenaar | 0e1e25f | 2010-05-28 21:07:08 +0200 | [diff] [blame] | 1387 | next_tp = tp->tp_next; |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 1388 | FOR_ALL_WINDOWS_IN_TAB(tp, wp) |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 1389 | { |
Bram Moolenaar | 802418d | 2013-01-17 14:00:11 +0100 | [diff] [blame] | 1390 | if (wp->w_buffer == NULL) |
| 1391 | /* Autocmd must have close the buffer already, skip. */ |
| 1392 | continue; |
Bram Moolenaar | 0e1e25f | 2010-05-28 21:07:08 +0200 | [diff] [blame] | 1393 | buf = wp->w_buffer; |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 1394 | if (CHANGEDTICK(buf) != -1) |
Bram Moolenaar | 0e1e25f | 2010-05-28 21:07:08 +0200 | [diff] [blame] | 1395 | { |
| 1396 | apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, |
| 1397 | buf->b_fname, FALSE, buf); |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 1398 | CHANGEDTICK(buf) = -1; /* note that we did it already */ |
Bram Moolenaar | 0e1e25f | 2010-05-28 21:07:08 +0200 | [diff] [blame] | 1399 | /* start all over, autocommands may mess up the lists */ |
| 1400 | next_tp = first_tabpage; |
| 1401 | break; |
| 1402 | } |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 1403 | } |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1404 | } |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 1405 | # else |
Bram Moolenaar | 0e1e25f | 2010-05-28 21:07:08 +0200 | [diff] [blame] | 1406 | apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname, |
| 1407 | FALSE, curbuf); |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 1408 | # endif |
Bram Moolenaar | 33aec76 | 2006-01-22 23:30:12 +0000 | [diff] [blame] | 1409 | |
Bram Moolenaar | 0e1e25f | 2010-05-28 21:07:08 +0200 | [diff] [blame] | 1410 | /* Trigger BufUnload for buffers that are loaded */ |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 1411 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | 0e1e25f | 2010-05-28 21:07:08 +0200 | [diff] [blame] | 1412 | if (buf->b_ml.ml_mfp != NULL) |
| 1413 | { |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1414 | bufref_T bufref; |
| 1415 | |
| 1416 | set_bufref(&bufref, buf); |
Bram Moolenaar | 0e1e25f | 2010-05-28 21:07:08 +0200 | [diff] [blame] | 1417 | apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1418 | FALSE, buf); |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1419 | if (!bufref_valid(&bufref)) |
| 1420 | /* autocmd deleted the buffer */ |
Bram Moolenaar | 0e1e25f | 2010-05-28 21:07:08 +0200 | [diff] [blame] | 1421 | break; |
| 1422 | } |
| 1423 | apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf); |
| 1424 | } |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1425 | #endif |
| 1426 | |
| 1427 | #ifdef FEAT_VIMINFO |
| 1428 | if (*p_viminfo != NUL) |
| 1429 | /* Write out the registers, history, marks etc, to the viminfo file */ |
| 1430 | write_viminfo(NULL, FALSE); |
| 1431 | #endif |
| 1432 | |
| 1433 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 0e1e25f | 2010-05-28 21:07:08 +0200 | [diff] [blame] | 1434 | if (get_vim_var_nr(VV_DYING) <= 1) |
| 1435 | apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1436 | #endif |
| 1437 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 1438 | #ifdef FEAT_PROFILE |
| 1439 | profile_dump(); |
| 1440 | #endif |
| 1441 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1442 | if (did_emsg |
| 1443 | #ifdef FEAT_GUI |
| 1444 | || (gui.in_use && msg_didany && p_verbose > 0) |
| 1445 | #endif |
| 1446 | ) |
| 1447 | { |
| 1448 | /* give the user a chance to read the (error) message */ |
| 1449 | no_wait_return = FALSE; |
| 1450 | wait_return(FALSE); |
| 1451 | } |
| 1452 | |
| 1453 | #ifdef FEAT_AUTOCMD |
| 1454 | /* Position the cursor again, the autocommands may have moved it */ |
| 1455 | # ifdef FEAT_GUI |
| 1456 | if (!gui.in_use) |
| 1457 | # endif |
| 1458 | windgoto((int)Rows - 1, 0); |
| 1459 | #endif |
| 1460 | |
Bram Moolenaar | 509ce2a | 2016-03-11 22:52:15 +0100 | [diff] [blame] | 1461 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 65edff8 | 2016-02-21 16:40:11 +0100 | [diff] [blame] | 1462 | job_stop_on_exit(); |
| 1463 | #endif |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 1464 | #ifdef FEAT_LUA |
| 1465 | lua_end(); |
| 1466 | #endif |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 1467 | #ifdef FEAT_MZSCHEME |
| 1468 | mzscheme_end(); |
| 1469 | #endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1470 | #ifdef FEAT_TCL |
| 1471 | tcl_end(); |
| 1472 | #endif |
| 1473 | #ifdef FEAT_RUBY |
| 1474 | ruby_end(); |
| 1475 | #endif |
| 1476 | #ifdef FEAT_PYTHON |
| 1477 | python_end(); |
| 1478 | #endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 1479 | #ifdef FEAT_PYTHON3 |
| 1480 | python3_end(); |
| 1481 | #endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1482 | #ifdef FEAT_PERL |
| 1483 | perl_end(); |
| 1484 | #endif |
| 1485 | #if defined(USE_ICONV) && defined(DYNAMIC_ICONV) |
| 1486 | iconv_end(); |
| 1487 | #endif |
| 1488 | #ifdef FEAT_NETBEANS_INTG |
| 1489 | netbeans_end(); |
| 1490 | #endif |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 1491 | #ifdef FEAT_CSCOPE |
| 1492 | cs_end(); |
| 1493 | #endif |
Bram Moolenaar | 9d2c8c1 | 2007-09-25 16:00:00 +0000 | [diff] [blame] | 1494 | #ifdef FEAT_EVAL |
| 1495 | if (garbage_collect_at_exit) |
Bram Moolenaar | ebf7dfa | 2016-04-14 12:46:51 +0200 | [diff] [blame] | 1496 | garbage_collect(FALSE); |
Bram Moolenaar | 9d2c8c1 | 2007-09-25 16:00:00 +0000 | [diff] [blame] | 1497 | #endif |
Bram Moolenaar | 1499332 | 2014-09-09 12:25:33 +0200 | [diff] [blame] | 1498 | #if defined(WIN32) && defined(FEAT_MBYTE) |
| 1499 | free_cmd_argsW(); |
| 1500 | #endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 1501 | |
| 1502 | mch_exit(exitval); |
| 1503 | } |
| 1504 | |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1505 | #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) |
| 1506 | /* |
| 1507 | * Setup to use the current locale (for ctype() and many other things). |
| 1508 | */ |
| 1509 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 1510 | init_locale(void) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1511 | { |
| 1512 | setlocale(LC_ALL, ""); |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1513 | |
Bram Moolenaar | c6af812 | 2010-05-21 12:04:55 +0200 | [diff] [blame] | 1514 | # ifdef FEAT_GUI_GTK |
| 1515 | /* Tell Gtk not to change our locale settings. */ |
| 1516 | gtk_disable_setlocale(); |
| 1517 | # endif |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1518 | # if defined(FEAT_FLOAT) && defined(LC_NUMERIC) |
| 1519 | /* Make sure strtod() uses a decimal point, not a comma. */ |
| 1520 | setlocale(LC_NUMERIC, "C"); |
| 1521 | # endif |
| 1522 | |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 1523 | # ifdef WIN32 |
| 1524 | /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit |
| 1525 | * text while it's expecting text in the current locale. This call avoids |
| 1526 | * that. */ |
| 1527 | setlocale(LC_CTYPE, "C"); |
| 1528 | # endif |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1529 | |
| 1530 | # ifdef FEAT_GETTEXT |
| 1531 | { |
| 1532 | int mustfree = FALSE; |
| 1533 | char_u *p; |
| 1534 | |
| 1535 | # ifdef DYNAMIC_GETTEXT |
| 1536 | /* Initialize the gettext library */ |
Bram Moolenaar | 286eacd | 2016-01-16 18:05:50 +0100 | [diff] [blame] | 1537 | dyn_libintl_init(); |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1538 | # endif |
Bram Moolenaar | 88e8f9f | 2016-01-20 22:48:02 +0100 | [diff] [blame] | 1539 | /* expand_env() doesn't work yet, because g_chartab[] is not |
| 1540 | * initialized yet, call vim_getenv() directly */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1541 | p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree); |
| 1542 | if (p != NULL && *p != NUL) |
| 1543 | { |
Bram Moolenaar | 1ad2f13 | 2007-06-19 18:27:18 +0000 | [diff] [blame] | 1544 | vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p); |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1545 | bindtextdomain(VIMPACKAGE, (char *)NameBuff); |
| 1546 | } |
| 1547 | if (mustfree) |
| 1548 | vim_free(p); |
| 1549 | textdomain(VIMPACKAGE); |
| 1550 | } |
| 1551 | # endif |
| 1552 | } |
| 1553 | #endif |
| 1554 | |
| 1555 | /* |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1556 | * Get the name of the display, before gui_prepare() removes it from |
| 1557 | * argv[]. Used for the xterm-clipboard display. |
| 1558 | * |
Bram Moolenaar | 78e1762 | 2007-08-30 10:26:19 +0000 | [diff] [blame] | 1559 | * Also find the --server... arguments and --socketid and --windowid |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1560 | */ |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1561 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 1562 | early_arg_scan(mparm_T *parmp UNUSED) |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1563 | { |
Bram Moolenaar | 0300597 | 2008-11-20 13:12:36 +0000 | [diff] [blame] | 1564 | #if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \ |
| 1565 | || !defined(FEAT_NETBEANS_INTG) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1566 | int argc = parmp->argc; |
| 1567 | char **argv = parmp->argv; |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1568 | int i; |
| 1569 | |
| 1570 | for (i = 1; i < argc; i++) |
| 1571 | { |
| 1572 | if (STRCMP(argv[i], "--") == 0) |
| 1573 | break; |
| 1574 | # ifdef FEAT_XCLIPBOARD |
| 1575 | else if (STRICMP(argv[i], "-display") == 0 |
Bram Moolenaar | 241a8aa | 2005-12-06 20:04:44 +0000 | [diff] [blame] | 1576 | # if defined(FEAT_GUI_GTK) |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1577 | || STRICMP(argv[i], "--display") == 0 |
| 1578 | # endif |
| 1579 | ) |
| 1580 | { |
| 1581 | if (i == argc - 1) |
| 1582 | mainerr_arg_missing((char_u *)argv[i]); |
| 1583 | xterm_display = argv[++i]; |
| 1584 | } |
| 1585 | # endif |
| 1586 | # ifdef FEAT_CLIENTSERVER |
| 1587 | else if (STRICMP(argv[i], "--servername") == 0) |
| 1588 | { |
| 1589 | if (i == argc - 1) |
| 1590 | mainerr_arg_missing((char_u *)argv[i]); |
| 1591 | parmp->serverName_arg = (char_u *)argv[++i]; |
| 1592 | } |
Bram Moolenaar | eb94e55 | 2006-03-11 21:35:11 +0000 | [diff] [blame] | 1593 | else if (STRICMP(argv[i], "--serverlist") == 0) |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1594 | parmp->serverArg = TRUE; |
Bram Moolenaar | eb94e55 | 2006-03-11 21:35:11 +0000 | [diff] [blame] | 1595 | else if (STRNICMP(argv[i], "--remote", 8) == 0) |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1596 | { |
| 1597 | parmp->serverArg = TRUE; |
Bram Moolenaar | eb94e55 | 2006-03-11 21:35:11 +0000 | [diff] [blame] | 1598 | # ifdef FEAT_GUI |
| 1599 | if (strstr(argv[i], "-wait") != 0) |
| 1600 | /* don't fork() when starting the GUI to edit files ourself */ |
| 1601 | gui.dofork = FALSE; |
| 1602 | # endif |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1603 | } |
| 1604 | # endif |
Bram Moolenaar | 78e1762 | 2007-08-30 10:26:19 +0000 | [diff] [blame] | 1605 | |
| 1606 | # if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) |
| 1607 | # ifdef FEAT_GUI_W32 |
| 1608 | else if (STRICMP(argv[i], "--windowid") == 0) |
| 1609 | # else |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1610 | else if (STRICMP(argv[i], "--socketid") == 0) |
Bram Moolenaar | 78e1762 | 2007-08-30 10:26:19 +0000 | [diff] [blame] | 1611 | # endif |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1612 | { |
Bram Moolenaar | cb4cef2 | 2008-03-16 15:04:34 +0000 | [diff] [blame] | 1613 | long_u id; |
| 1614 | int count; |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1615 | |
| 1616 | if (i == argc - 1) |
| 1617 | mainerr_arg_missing((char_u *)argv[i]); |
| 1618 | if (STRNICMP(argv[i+1], "0x", 2) == 0) |
Bram Moolenaar | cb4cef2 | 2008-03-16 15:04:34 +0000 | [diff] [blame] | 1619 | count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id); |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1620 | else |
Bram Moolenaar | cb4cef2 | 2008-03-16 15:04:34 +0000 | [diff] [blame] | 1621 | count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id); |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1622 | if (count != 1) |
| 1623 | mainerr(ME_INVALID_ARG, (char_u *)argv[i]); |
| 1624 | else |
Bram Moolenaar | 78e1762 | 2007-08-30 10:26:19 +0000 | [diff] [blame] | 1625 | # ifdef FEAT_GUI_W32 |
| 1626 | win_socket_id = id; |
| 1627 | # else |
| 1628 | gtk_socket_id = id; |
| 1629 | # endif |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1630 | i++; |
| 1631 | } |
Bram Moolenaar | 78e1762 | 2007-08-30 10:26:19 +0000 | [diff] [blame] | 1632 | # endif |
| 1633 | # ifdef FEAT_GUI_GTK |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1634 | else if (STRICMP(argv[i], "--echo-wid") == 0) |
| 1635 | echo_wid_arg = TRUE; |
| 1636 | # endif |
Bram Moolenaar | 0300597 | 2008-11-20 13:12:36 +0000 | [diff] [blame] | 1637 | # ifndef FEAT_NETBEANS_INTG |
| 1638 | else if (strncmp(argv[i], "-nb", (size_t)3) == 0) |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 1639 | { |
| 1640 | mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n")); |
| 1641 | mch_exit(2); |
| 1642 | } |
Bram Moolenaar | 0300597 | 2008-11-20 13:12:36 +0000 | [diff] [blame] | 1643 | # endif |
| 1644 | |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1645 | } |
| 1646 | #endif |
| 1647 | } |
| 1648 | |
Bram Moolenaar | 502ae4b | 2016-07-16 19:50:13 +0200 | [diff] [blame] | 1649 | #ifndef NO_VIM_MAIN |
| 1650 | /* |
| 1651 | * Get a (optional) count for a Vim argument. |
| 1652 | */ |
| 1653 | static int |
| 1654 | get_number_arg( |
| 1655 | char_u *p, /* pointer to argument */ |
| 1656 | int *idx, /* index in argument, is incremented */ |
| 1657 | int def) /* default value */ |
| 1658 | { |
| 1659 | if (vim_isdigit(p[*idx])) |
| 1660 | { |
| 1661 | def = atoi((char *)&(p[*idx])); |
| 1662 | while (vim_isdigit(p[*idx])) |
| 1663 | *idx = *idx + 1; |
| 1664 | } |
| 1665 | return def; |
| 1666 | } |
| 1667 | |
| 1668 | /* |
| 1669 | * Check for: [r][e][g][vi|vim|view][diff][ex[im]] |
| 1670 | * If the executable name starts with "r" we disable shell commands. |
| 1671 | * If the next character is "e" we run in Easy mode. |
| 1672 | * If the next character is "g" we run the GUI version. |
| 1673 | * If the next characters are "view" we start in readonly mode. |
| 1674 | * If the next characters are "diff" or "vimdiff" we start in diff mode. |
| 1675 | * If the next characters are "ex" we start in Ex mode. If it's followed |
| 1676 | * by "im" use improved Ex mode. |
| 1677 | */ |
| 1678 | static void |
| 1679 | parse_command_name(mparm_T *parmp) |
| 1680 | { |
| 1681 | char_u *initstr; |
| 1682 | |
| 1683 | initstr = gettail((char_u *)parmp->argv[0]); |
| 1684 | |
| 1685 | #ifdef MACOS_X_UNIX |
| 1686 | /* An issue has been seen when launching Vim in such a way that |
| 1687 | * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the |
| 1688 | * executable or a symbolic link of it. Until this issue is resolved |
| 1689 | * we prohibit the GUI from being used. |
| 1690 | */ |
| 1691 | if (STRCMP(initstr, parmp->argv[0]) == 0) |
| 1692 | disallow_gui = TRUE; |
| 1693 | |
| 1694 | /* TODO: On MacOS X default to gui if argv[0] ends in: |
| 1695 | * /Vim.app/Contents/MacOS/Vim */ |
| 1696 | #endif |
| 1697 | |
| 1698 | #ifdef FEAT_EVAL |
| 1699 | set_vim_var_string(VV_PROGNAME, initstr, -1); |
Bram Moolenaar | 08cab96 | 2017-03-04 14:37:18 +0100 | [diff] [blame] | 1700 | set_progpath((char_u *)parmp->argv[0]); |
Bram Moolenaar | 502ae4b | 2016-07-16 19:50:13 +0200 | [diff] [blame] | 1701 | #endif |
| 1702 | |
| 1703 | if (TOLOWER_ASC(initstr[0]) == 'r') |
| 1704 | { |
| 1705 | restricted = TRUE; |
| 1706 | ++initstr; |
| 1707 | } |
| 1708 | |
| 1709 | /* Use evim mode for "evim" and "egvim", not for "editor". */ |
| 1710 | if (TOLOWER_ASC(initstr[0]) == 'e' |
| 1711 | && (TOLOWER_ASC(initstr[1]) == 'v' |
| 1712 | || TOLOWER_ASC(initstr[1]) == 'g')) |
| 1713 | { |
| 1714 | #ifdef FEAT_GUI |
| 1715 | gui.starting = TRUE; |
| 1716 | #endif |
| 1717 | parmp->evim_mode = TRUE; |
| 1718 | ++initstr; |
| 1719 | } |
| 1720 | |
| 1721 | /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */ |
| 1722 | if (TOLOWER_ASC(initstr[0]) == 'g') |
| 1723 | { |
| 1724 | main_start_gui(); |
| 1725 | #ifdef FEAT_GUI |
| 1726 | ++initstr; |
| 1727 | #endif |
| 1728 | } |
| 1729 | |
| 1730 | if (STRNICMP(initstr, "view", 4) == 0) |
| 1731 | { |
| 1732 | readonlymode = TRUE; |
| 1733 | curbuf->b_p_ro = TRUE; |
| 1734 | p_uc = 10000; /* don't update very often */ |
| 1735 | initstr += 4; |
| 1736 | } |
| 1737 | else if (STRNICMP(initstr, "vim", 3) == 0) |
| 1738 | initstr += 3; |
| 1739 | |
| 1740 | /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */ |
| 1741 | if (STRICMP(initstr, "diff") == 0) |
| 1742 | { |
| 1743 | #ifdef FEAT_DIFF |
| 1744 | parmp->diff_mode = TRUE; |
| 1745 | #else |
| 1746 | mch_errmsg(_("This Vim was not compiled with the diff feature.")); |
| 1747 | mch_errmsg("\n"); |
| 1748 | mch_exit(2); |
| 1749 | #endif |
| 1750 | } |
| 1751 | |
| 1752 | if (STRNICMP(initstr, "ex", 2) == 0) |
| 1753 | { |
| 1754 | if (STRNICMP(initstr + 2, "im", 2) == 0) |
| 1755 | exmode_active = EXMODE_VIM; |
| 1756 | else |
| 1757 | exmode_active = EXMODE_NORMAL; |
| 1758 | change_compatible(TRUE); /* set 'compatible' */ |
| 1759 | } |
| 1760 | } |
| 1761 | |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 1762 | /* |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1763 | * Scan the command line arguments. |
| 1764 | */ |
| 1765 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 1766 | command_line_scan(mparm_T *parmp) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1767 | { |
| 1768 | int argc = parmp->argc; |
| 1769 | char **argv = parmp->argv; |
| 1770 | int argv_idx; /* index in argv[n][] */ |
| 1771 | int had_minmin = FALSE; /* found "--" argument */ |
| 1772 | int want_argument; /* option argument with argument */ |
| 1773 | int c; |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 1774 | char_u *p = NULL; |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1775 | long n; |
| 1776 | |
| 1777 | --argc; |
| 1778 | ++argv; |
| 1779 | argv_idx = 1; /* active option letter is argv[0][argv_idx] */ |
| 1780 | while (argc > 0) |
| 1781 | { |
| 1782 | /* |
| 1783 | * "+" or "+{number}" or "+/{pat}" or "+{command}" argument. |
| 1784 | */ |
| 1785 | if (argv[0][0] == '+' && !had_minmin) |
| 1786 | { |
| 1787 | if (parmp->n_commands >= MAX_ARG_CMDS) |
| 1788 | mainerr(ME_EXTRA_CMD, NULL); |
| 1789 | argv_idx = -1; /* skip to next argument */ |
| 1790 | if (argv[0][1] == NUL) |
| 1791 | parmp->commands[parmp->n_commands++] = (char_u *)"$"; |
| 1792 | else |
| 1793 | parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]); |
| 1794 | } |
| 1795 | |
| 1796 | /* |
| 1797 | * Optional argument. |
| 1798 | */ |
| 1799 | else if (argv[0][0] == '-' && !had_minmin) |
| 1800 | { |
| 1801 | want_argument = FALSE; |
| 1802 | c = argv[0][argv_idx++]; |
| 1803 | #ifdef VMS |
| 1804 | /* |
| 1805 | * VMS only uses upper case command lines. Interpret "-X" as "-x" |
| 1806 | * and "-/X" as "-X". |
| 1807 | */ |
| 1808 | if (c == '/') |
| 1809 | { |
| 1810 | c = argv[0][argv_idx++]; |
| 1811 | c = TOUPPER_ASC(c); |
| 1812 | } |
| 1813 | else |
| 1814 | c = TOLOWER_ASC(c); |
| 1815 | #endif |
| 1816 | switch (c) |
| 1817 | { |
| 1818 | case NUL: /* "vim -" read from stdin */ |
| 1819 | /* "ex -" silent mode */ |
| 1820 | if (exmode_active) |
| 1821 | silent_mode = TRUE; |
| 1822 | else |
| 1823 | { |
| 1824 | if (parmp->edit_type != EDIT_NONE) |
| 1825 | mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]); |
| 1826 | parmp->edit_type = EDIT_STDIN; |
| 1827 | read_cmd_fd = 2; /* read from stderr instead of stdin */ |
| 1828 | } |
| 1829 | argv_idx = -1; /* skip to next argument */ |
| 1830 | break; |
| 1831 | |
| 1832 | case '-': /* "--" don't take any more option arguments */ |
| 1833 | /* "--help" give help message */ |
| 1834 | /* "--version" give version message */ |
| 1835 | /* "--literal" take files literally */ |
| 1836 | /* "--nofork" don't fork */ |
Bram Moolenaar | 49c39ff | 2016-02-25 21:21:52 +0100 | [diff] [blame] | 1837 | /* "--not-a-term" don't warn for not a term */ |
Bram Moolenaar | 2cab0e1 | 2016-11-24 15:09:07 +0100 | [diff] [blame] | 1838 | /* "--ttyfail" exit if not a term */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1839 | /* "--noplugin[s]" skip plugins */ |
| 1840 | /* "--cmd <cmd>" execute cmd before vimrc */ |
| 1841 | if (STRICMP(argv[0] + argv_idx, "help") == 0) |
| 1842 | usage(); |
| 1843 | else if (STRICMP(argv[0] + argv_idx, "version") == 0) |
| 1844 | { |
| 1845 | Columns = 80; /* need to init Columns */ |
| 1846 | info_message = TRUE; /* use mch_msg(), not mch_errmsg() */ |
| 1847 | list_version(); |
| 1848 | msg_putchar('\n'); |
| 1849 | msg_didout = FALSE; |
| 1850 | mch_exit(0); |
| 1851 | } |
| 1852 | else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0) |
| 1853 | { |
Bram Moolenaar | 5307683 | 2015-12-31 19:53:21 +0100 | [diff] [blame] | 1854 | #ifdef EXPAND_FILENAMES |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1855 | parmp->literal = TRUE; |
| 1856 | #endif |
| 1857 | } |
| 1858 | else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0) |
| 1859 | { |
| 1860 | #ifdef FEAT_GUI |
| 1861 | gui.dofork = FALSE; /* don't fork() when starting GUI */ |
| 1862 | #endif |
| 1863 | } |
| 1864 | else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0) |
| 1865 | p_lpl = FALSE; |
Bram Moolenaar | 49c39ff | 2016-02-25 21:21:52 +0100 | [diff] [blame] | 1866 | else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0) |
| 1867 | parmp->not_a_term = TRUE; |
Bram Moolenaar | 2cab0e1 | 2016-11-24 15:09:07 +0100 | [diff] [blame] | 1868 | else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0) |
| 1869 | parmp->tty_fail = TRUE; |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1870 | else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0) |
| 1871 | { |
| 1872 | want_argument = TRUE; |
| 1873 | argv_idx += 3; |
| 1874 | } |
Bram Moolenaar | ef94eec | 2009-11-11 13:22:11 +0000 | [diff] [blame] | 1875 | else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0) |
| 1876 | { |
| 1877 | want_argument = TRUE; |
| 1878 | argv_idx += 11; |
| 1879 | } |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1880 | #ifdef FEAT_CLIENTSERVER |
| 1881 | else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0) |
| 1882 | ; /* already processed -- no arg */ |
| 1883 | else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0 |
| 1884 | || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0) |
| 1885 | { |
| 1886 | /* already processed -- snatch the following arg */ |
| 1887 | if (argc > 1) |
| 1888 | { |
| 1889 | --argc; |
| 1890 | ++argv; |
| 1891 | } |
| 1892 | } |
| 1893 | #endif |
Bram Moolenaar | 78e1762 | 2007-08-30 10:26:19 +0000 | [diff] [blame] | 1894 | #if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32) |
| 1895 | # ifdef FEAT_GUI_GTK |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1896 | else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0) |
Bram Moolenaar | 78e1762 | 2007-08-30 10:26:19 +0000 | [diff] [blame] | 1897 | # else |
| 1898 | else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0) |
| 1899 | # endif |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1900 | { |
| 1901 | /* already processed -- snatch the following arg */ |
| 1902 | if (argc > 1) |
| 1903 | { |
| 1904 | --argc; |
| 1905 | ++argv; |
| 1906 | } |
| 1907 | } |
Bram Moolenaar | 78e1762 | 2007-08-30 10:26:19 +0000 | [diff] [blame] | 1908 | #endif |
| 1909 | #ifdef FEAT_GUI_GTK |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1910 | else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0) |
| 1911 | { |
| 1912 | /* already processed, skip */ |
| 1913 | } |
| 1914 | #endif |
| 1915 | else |
| 1916 | { |
| 1917 | if (argv[0][argv_idx]) |
| 1918 | mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]); |
| 1919 | had_minmin = TRUE; |
| 1920 | } |
| 1921 | if (!want_argument) |
| 1922 | argv_idx = -1; /* skip to next argument */ |
| 1923 | break; |
| 1924 | |
| 1925 | case 'A': /* "-A" start in Arabic mode */ |
| 1926 | #ifdef FEAT_ARABIC |
| 1927 | set_option_value((char_u *)"arabic", 1L, NULL, 0); |
| 1928 | #else |
| 1929 | mch_errmsg(_(e_noarabic)); |
| 1930 | mch_exit(2); |
| 1931 | #endif |
| 1932 | break; |
| 1933 | |
| 1934 | case 'b': /* "-b" binary mode */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 1935 | /* Needs to be effective before expanding file names, because |
| 1936 | * for Win32 this makes us edit a shortcut file itself, |
| 1937 | * instead of the file it links to. */ |
| 1938 | set_options_bin(curbuf->b_p_bin, 1, 0); |
| 1939 | curbuf->b_p_bin = 1; /* binary file I/O */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1940 | break; |
| 1941 | |
| 1942 | case 'C': /* "-C" Compatible */ |
| 1943 | change_compatible(TRUE); |
Bram Moolenaar | b9a46fe | 2016-07-29 18:13:42 +0200 | [diff] [blame] | 1944 | has_dash_c_arg = TRUE; |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1945 | break; |
| 1946 | |
| 1947 | case 'e': /* "-e" Ex mode */ |
| 1948 | exmode_active = EXMODE_NORMAL; |
| 1949 | break; |
| 1950 | |
| 1951 | case 'E': /* "-E" Improved Ex mode */ |
| 1952 | exmode_active = EXMODE_VIM; |
| 1953 | break; |
| 1954 | |
| 1955 | case 'f': /* "-f" GUI: run in foreground. Amiga: open |
| 1956 | window directly, not with newcli */ |
| 1957 | #ifdef FEAT_GUI |
| 1958 | gui.dofork = FALSE; /* don't fork() when starting GUI */ |
| 1959 | #endif |
| 1960 | break; |
| 1961 | |
| 1962 | case 'g': /* "-g" start GUI */ |
| 1963 | main_start_gui(); |
| 1964 | break; |
| 1965 | |
| 1966 | case 'F': /* "-F" start in Farsi mode: rl + fkmap set */ |
| 1967 | #ifdef FEAT_FKMAP |
Bram Moolenaar | c4cd38f | 2008-01-13 15:18:01 +0000 | [diff] [blame] | 1968 | p_fkmap = TRUE; |
| 1969 | set_option_value((char_u *)"rl", 1L, NULL, 0); |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1970 | #else |
| 1971 | mch_errmsg(_(e_nofarsi)); |
| 1972 | mch_exit(2); |
| 1973 | #endif |
| 1974 | break; |
| 1975 | |
| 1976 | case 'h': /* "-h" give help message */ |
| 1977 | #ifdef FEAT_GUI_GNOME |
| 1978 | /* Tell usage() to exit for "gvim". */ |
| 1979 | gui.starting = FALSE; |
| 1980 | #endif |
| 1981 | usage(); |
| 1982 | break; |
| 1983 | |
| 1984 | case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */ |
| 1985 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | c4cd38f | 2008-01-13 15:18:01 +0000 | [diff] [blame] | 1986 | p_hkmap = TRUE; |
| 1987 | set_option_value((char_u *)"rl", 1L, NULL, 0); |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 1988 | #else |
| 1989 | mch_errmsg(_(e_nohebrew)); |
| 1990 | mch_exit(2); |
| 1991 | #endif |
| 1992 | break; |
| 1993 | |
| 1994 | case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */ |
| 1995 | #ifdef FEAT_LISP |
| 1996 | set_option_value((char_u *)"lisp", 1L, NULL, 0); |
| 1997 | p_sm = TRUE; |
| 1998 | #endif |
| 1999 | break; |
| 2000 | |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2001 | case 'M': /* "-M" no changes or writing of files */ |
| 2002 | reset_modifiable(); |
| 2003 | /* FALLTHROUGH */ |
| 2004 | |
| 2005 | case 'm': /* "-m" no writing of files */ |
| 2006 | p_write = FALSE; |
| 2007 | break; |
| 2008 | |
| 2009 | case 'y': /* "-y" easy mode */ |
| 2010 | #ifdef FEAT_GUI |
| 2011 | gui.starting = TRUE; /* start GUI a bit later */ |
| 2012 | #endif |
| 2013 | parmp->evim_mode = TRUE; |
| 2014 | break; |
| 2015 | |
| 2016 | case 'N': /* "-N" Nocompatible */ |
| 2017 | change_compatible(FALSE); |
| 2018 | break; |
| 2019 | |
| 2020 | case 'n': /* "-n" no swap file */ |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 2021 | #ifdef FEAT_NETBEANS_INTG |
| 2022 | /* checking for "-nb", netbeans parameters */ |
| 2023 | if (argv[0][argv_idx] == 'b') |
| 2024 | { |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 2025 | netbeansArg = argv[0]; |
| 2026 | argv_idx = -1; /* skip to next argument */ |
| 2027 | } |
| 2028 | else |
| 2029 | #endif |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2030 | parmp->no_swap_file = TRUE; |
| 2031 | break; |
| 2032 | |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 2033 | case 'p': /* "-p[N]" open N tab pages */ |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 2034 | #ifdef TARGET_API_MAC_OSX |
| 2035 | /* For some reason on MacOS X, an argument like: |
| 2036 | -psn_0_10223617 is passed in when invoke from Finder |
| 2037 | or with the 'open' command */ |
| 2038 | if (argv[0][argv_idx] == 's') |
| 2039 | { |
| 2040 | argv_idx = -1; /* bypass full -psn */ |
| 2041 | main_start_gui(); |
| 2042 | break; |
| 2043 | } |
| 2044 | #endif |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 2045 | #ifdef FEAT_WINDOWS |
| 2046 | /* default is 0: open window for each file */ |
| 2047 | parmp->window_count = get_number_arg((char_u *)argv[0], |
| 2048 | &argv_idx, 0); |
| 2049 | parmp->window_layout = WIN_TABS; |
| 2050 | #endif |
| 2051 | break; |
| 2052 | |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2053 | case 'o': /* "-o[N]" open N horizontal split windows */ |
| 2054 | #ifdef FEAT_WINDOWS |
| 2055 | /* default is 0: open window for each file */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 2056 | parmp->window_count = get_number_arg((char_u *)argv[0], |
| 2057 | &argv_idx, 0); |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 2058 | parmp->window_layout = WIN_HOR; |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2059 | #endif |
| 2060 | break; |
| 2061 | |
| 2062 | case 'O': /* "-O[N]" open N vertical split windows */ |
Bram Moolenaar | 44a2f92 | 2016-03-19 22:11:51 +0100 | [diff] [blame] | 2063 | #ifdef FEAT_WINDOWS |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2064 | /* default is 0: open window for each file */ |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 2065 | parmp->window_count = get_number_arg((char_u *)argv[0], |
| 2066 | &argv_idx, 0); |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 2067 | parmp->window_layout = WIN_VER; |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2068 | #endif |
| 2069 | break; |
| 2070 | |
| 2071 | #ifdef FEAT_QUICKFIX |
| 2072 | case 'q': /* "-q" QuickFix mode */ |
| 2073 | if (parmp->edit_type != EDIT_NONE) |
| 2074 | mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]); |
| 2075 | parmp->edit_type = EDIT_QF; |
| 2076 | if (argv[0][argv_idx]) /* "-q{errorfile}" */ |
| 2077 | { |
| 2078 | parmp->use_ef = (char_u *)argv[0] + argv_idx; |
| 2079 | argv_idx = -1; |
| 2080 | } |
| 2081 | else if (argc > 1) /* "-q {errorfile}" */ |
| 2082 | want_argument = TRUE; |
| 2083 | break; |
| 2084 | #endif |
| 2085 | |
| 2086 | case 'R': /* "-R" readonly mode */ |
| 2087 | readonlymode = TRUE; |
| 2088 | curbuf->b_p_ro = TRUE; |
| 2089 | p_uc = 10000; /* don't update very often */ |
| 2090 | break; |
| 2091 | |
| 2092 | case 'r': /* "-r" recovery mode */ |
| 2093 | case 'L': /* "-L" recovery mode */ |
| 2094 | recoverymode = 1; |
| 2095 | break; |
| 2096 | |
| 2097 | case 's': |
| 2098 | if (exmode_active) /* "-s" silent (batch) mode */ |
| 2099 | silent_mode = TRUE; |
| 2100 | else /* "-s {scriptin}" read from script file */ |
| 2101 | want_argument = TRUE; |
| 2102 | break; |
| 2103 | |
| 2104 | case 't': /* "-t {tag}" or "-t{tag}" jump to tag */ |
| 2105 | if (parmp->edit_type != EDIT_NONE) |
| 2106 | mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]); |
| 2107 | parmp->edit_type = EDIT_TAG; |
| 2108 | if (argv[0][argv_idx]) /* "-t{tag}" */ |
| 2109 | { |
| 2110 | parmp->tagname = (char_u *)argv[0] + argv_idx; |
| 2111 | argv_idx = -1; |
| 2112 | } |
| 2113 | else /* "-t {tag}" */ |
| 2114 | want_argument = TRUE; |
| 2115 | break; |
| 2116 | |
| 2117 | #ifdef FEAT_EVAL |
| 2118 | case 'D': /* "-D" Debugging */ |
| 2119 | parmp->use_debug_break_level = 9999; |
| 2120 | break; |
| 2121 | #endif |
| 2122 | #ifdef FEAT_DIFF |
| 2123 | case 'd': /* "-d" 'diff' */ |
| 2124 | # ifdef AMIGA |
| 2125 | /* check for "-dev {device}" */ |
| 2126 | if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v') |
| 2127 | want_argument = TRUE; |
| 2128 | else |
| 2129 | # endif |
| 2130 | parmp->diff_mode = TRUE; |
| 2131 | break; |
| 2132 | #endif |
| 2133 | case 'V': /* "-V{N}" Verbose level */ |
| 2134 | /* default is 10: a little bit verbose */ |
| 2135 | p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10); |
| 2136 | if (argv[0][argv_idx] != NUL) |
| 2137 | { |
| 2138 | set_option_value((char_u *)"verbosefile", 0L, |
| 2139 | (char_u *)argv[0] + argv_idx, 0); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2140 | argv_idx = (int)STRLEN(argv[0]); |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2141 | } |
| 2142 | break; |
| 2143 | |
| 2144 | case 'v': /* "-v" Vi-mode (as if called "vi") */ |
| 2145 | exmode_active = 0; |
| 2146 | #ifdef FEAT_GUI |
| 2147 | gui.starting = FALSE; /* don't start GUI */ |
| 2148 | #endif |
| 2149 | break; |
| 2150 | |
| 2151 | case 'w': /* "-w{number}" set window height */ |
| 2152 | /* "-w {scriptout}" write to script */ |
| 2153 | if (vim_isdigit(((char_u *)argv[0])[argv_idx])) |
| 2154 | { |
| 2155 | n = get_number_arg((char_u *)argv[0], &argv_idx, 10); |
| 2156 | set_option_value((char_u *)"window", n, NULL, 0); |
| 2157 | break; |
| 2158 | } |
| 2159 | want_argument = TRUE; |
| 2160 | break; |
| 2161 | |
| 2162 | #ifdef FEAT_CRYPT |
| 2163 | case 'x': /* "-x" encrypted reading/writing of files */ |
| 2164 | parmp->ask_for_key = TRUE; |
| 2165 | break; |
| 2166 | #endif |
| 2167 | |
| 2168 | case 'X': /* "-X" don't connect to X server */ |
| 2169 | #if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11) |
| 2170 | x_no_connect = TRUE; |
| 2171 | #endif |
| 2172 | break; |
| 2173 | |
| 2174 | case 'Z': /* "-Z" restricted mode */ |
| 2175 | restricted = TRUE; |
| 2176 | break; |
| 2177 | |
| 2178 | case 'c': /* "-c{command}" or "-c {command}" execute |
| 2179 | command */ |
| 2180 | if (argv[0][argv_idx] != NUL) |
| 2181 | { |
| 2182 | if (parmp->n_commands >= MAX_ARG_CMDS) |
| 2183 | mainerr(ME_EXTRA_CMD, NULL); |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 2184 | parmp->commands[parmp->n_commands++] = (char_u *)argv[0] |
| 2185 | + argv_idx; |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2186 | argv_idx = -1; |
| 2187 | break; |
| 2188 | } |
| 2189 | /*FALLTHROUGH*/ |
| 2190 | case 'S': /* "-S {file}" execute Vim script */ |
| 2191 | case 'i': /* "-i {viminfo}" use for viminfo */ |
| 2192 | #ifndef FEAT_DIFF |
| 2193 | case 'd': /* "-d {device}" device (for Amiga) */ |
| 2194 | #endif |
| 2195 | case 'T': /* "-T {terminal}" terminal name */ |
| 2196 | case 'u': /* "-u {vimrc}" vim inits file */ |
| 2197 | case 'U': /* "-U {gvimrc}" gvim inits file */ |
| 2198 | case 'W': /* "-W {scriptout}" overwrite */ |
| 2199 | #ifdef FEAT_GUI_W32 |
| 2200 | case 'P': /* "-P {parent title}" MDI parent */ |
| 2201 | #endif |
| 2202 | want_argument = TRUE; |
| 2203 | break; |
| 2204 | |
| 2205 | default: |
| 2206 | mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]); |
| 2207 | } |
| 2208 | |
| 2209 | /* |
| 2210 | * Handle option arguments with argument. |
| 2211 | */ |
| 2212 | if (want_argument) |
| 2213 | { |
| 2214 | /* |
| 2215 | * Check for garbage immediately after the option letter. |
| 2216 | */ |
| 2217 | if (argv[0][argv_idx] != NUL) |
| 2218 | mainerr(ME_GARBAGE, (char_u *)argv[0]); |
| 2219 | |
| 2220 | --argc; |
Bram Moolenaar | ef94eec | 2009-11-11 13:22:11 +0000 | [diff] [blame] | 2221 | if (argc < 1 && c != 'S') /* -S has an optional argument */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2222 | mainerr_arg_missing((char_u *)argv[0]); |
| 2223 | ++argv; |
| 2224 | argv_idx = -1; |
| 2225 | |
| 2226 | switch (c) |
| 2227 | { |
| 2228 | case 'c': /* "-c {command}" execute command */ |
| 2229 | case 'S': /* "-S {file}" execute Vim script */ |
| 2230 | if (parmp->n_commands >= MAX_ARG_CMDS) |
| 2231 | mainerr(ME_EXTRA_CMD, NULL); |
| 2232 | if (c == 'S') |
| 2233 | { |
| 2234 | char *a; |
| 2235 | |
| 2236 | if (argc < 1) |
| 2237 | /* "-S" without argument: use default session file |
| 2238 | * name. */ |
| 2239 | a = SESSION_FILE; |
| 2240 | else if (argv[0][0] == '-') |
| 2241 | { |
| 2242 | /* "-S" followed by another option: use default |
| 2243 | * session file name. */ |
| 2244 | a = SESSION_FILE; |
| 2245 | ++argc; |
| 2246 | --argv; |
| 2247 | } |
| 2248 | else |
| 2249 | a = argv[0]; |
| 2250 | p = alloc((unsigned)(STRLEN(a) + 4)); |
| 2251 | if (p == NULL) |
| 2252 | mch_exit(2); |
| 2253 | sprintf((char *)p, "so %s", a); |
| 2254 | parmp->cmds_tofree[parmp->n_commands] = TRUE; |
| 2255 | parmp->commands[parmp->n_commands++] = p; |
| 2256 | } |
| 2257 | else |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 2258 | parmp->commands[parmp->n_commands++] = |
| 2259 | (char_u *)argv[0]; |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2260 | break; |
| 2261 | |
Bram Moolenaar | ef94eec | 2009-11-11 13:22:11 +0000 | [diff] [blame] | 2262 | case '-': |
| 2263 | if (argv[-1][2] == 'c') |
| 2264 | { |
| 2265 | /* "--cmd {command}" execute command */ |
| 2266 | if (parmp->n_pre_commands >= MAX_ARG_CMDS) |
| 2267 | mainerr(ME_EXTRA_CMD, NULL); |
| 2268 | parmp->pre_commands[parmp->n_pre_commands++] = |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 2269 | (char_u *)argv[0]; |
Bram Moolenaar | ef94eec | 2009-11-11 13:22:11 +0000 | [diff] [blame] | 2270 | } |
| 2271 | /* "--startuptime <file>" already handled */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2272 | break; |
| 2273 | |
| 2274 | /* case 'd': -d {device} is handled in mch_check_win() for the |
| 2275 | * Amiga */ |
| 2276 | |
| 2277 | #ifdef FEAT_QUICKFIX |
| 2278 | case 'q': /* "-q {errorfile}" QuickFix mode */ |
| 2279 | parmp->use_ef = (char_u *)argv[0]; |
| 2280 | break; |
| 2281 | #endif |
| 2282 | |
| 2283 | case 'i': /* "-i {viminfo}" use for viminfo */ |
| 2284 | use_viminfo = (char_u *)argv[0]; |
| 2285 | break; |
| 2286 | |
| 2287 | case 's': /* "-s {scriptin}" read from script file */ |
| 2288 | if (scriptin[0] != NULL) |
| 2289 | { |
| 2290 | scripterror: |
| 2291 | mch_errmsg(_("Attempt to open script file again: \"")); |
| 2292 | mch_errmsg(argv[-1]); |
| 2293 | mch_errmsg(" "); |
| 2294 | mch_errmsg(argv[0]); |
| 2295 | mch_errmsg("\"\n"); |
| 2296 | mch_exit(2); |
| 2297 | } |
| 2298 | if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL) |
| 2299 | { |
| 2300 | mch_errmsg(_("Cannot open for reading: \"")); |
| 2301 | mch_errmsg(argv[0]); |
| 2302 | mch_errmsg("\"\n"); |
| 2303 | mch_exit(2); |
| 2304 | } |
| 2305 | if (save_typebuf() == FAIL) |
| 2306 | mch_exit(2); /* out of memory */ |
| 2307 | break; |
| 2308 | |
| 2309 | case 't': /* "-t {tag}" */ |
| 2310 | parmp->tagname = (char_u *)argv[0]; |
| 2311 | break; |
| 2312 | |
| 2313 | case 'T': /* "-T {terminal}" terminal name */ |
| 2314 | /* |
| 2315 | * The -T term argument is always available and when |
| 2316 | * HAVE_TERMLIB is supported it overrides the environment |
| 2317 | * variable TERM. |
| 2318 | */ |
| 2319 | #ifdef FEAT_GUI |
| 2320 | if (term_is_gui((char_u *)argv[0])) |
| 2321 | gui.starting = TRUE; /* start GUI a bit later */ |
| 2322 | else |
| 2323 | #endif |
| 2324 | parmp->term = (char_u *)argv[0]; |
| 2325 | break; |
| 2326 | |
| 2327 | case 'u': /* "-u {vimrc}" vim inits file */ |
| 2328 | parmp->use_vimrc = (char_u *)argv[0]; |
| 2329 | break; |
| 2330 | |
| 2331 | case 'U': /* "-U {gvimrc}" gvim inits file */ |
| 2332 | #ifdef FEAT_GUI |
| 2333 | use_gvimrc = (char_u *)argv[0]; |
| 2334 | #endif |
| 2335 | break; |
| 2336 | |
| 2337 | case 'w': /* "-w {nr}" 'window' value */ |
| 2338 | /* "-w {scriptout}" append to script file */ |
| 2339 | if (vim_isdigit(*((char_u *)argv[0]))) |
| 2340 | { |
| 2341 | argv_idx = 0; |
| 2342 | n = get_number_arg((char_u *)argv[0], &argv_idx, 10); |
| 2343 | set_option_value((char_u *)"window", n, NULL, 0); |
| 2344 | argv_idx = -1; |
| 2345 | break; |
| 2346 | } |
| 2347 | /*FALLTHROUGH*/ |
| 2348 | case 'W': /* "-W {scriptout}" overwrite script file */ |
| 2349 | if (scriptout != NULL) |
| 2350 | goto scripterror; |
| 2351 | if ((scriptout = mch_fopen(argv[0], |
| 2352 | c == 'w' ? APPENDBIN : WRITEBIN)) == NULL) |
| 2353 | { |
| 2354 | mch_errmsg(_("Cannot open for script output: \"")); |
| 2355 | mch_errmsg(argv[0]); |
| 2356 | mch_errmsg("\"\n"); |
| 2357 | mch_exit(2); |
| 2358 | } |
| 2359 | break; |
| 2360 | |
| 2361 | #ifdef FEAT_GUI_W32 |
| 2362 | case 'P': /* "-P {parent title}" MDI parent */ |
| 2363 | gui_mch_set_parent(argv[0]); |
| 2364 | break; |
| 2365 | #endif |
| 2366 | } |
| 2367 | } |
| 2368 | } |
| 2369 | |
| 2370 | /* |
| 2371 | * File name argument. |
| 2372 | */ |
| 2373 | else |
| 2374 | { |
| 2375 | argv_idx = -1; /* skip to next argument */ |
| 2376 | |
| 2377 | /* Check for only one type of editing. */ |
| 2378 | if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE) |
| 2379 | mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]); |
| 2380 | parmp->edit_type = EDIT_FILE; |
| 2381 | |
| 2382 | #ifdef MSWIN |
| 2383 | /* Remember if the argument was a full path before changing |
| 2384 | * slashes to backslashes. */ |
| 2385 | if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\') |
| 2386 | parmp->full_path = TRUE; |
| 2387 | #endif |
| 2388 | |
| 2389 | /* Add the file to the global argument list. */ |
| 2390 | if (ga_grow(&global_alist.al_ga, 1) == FAIL |
| 2391 | || (p = vim_strsave((char_u *)argv[0])) == NULL) |
| 2392 | mch_exit(2); |
| 2393 | #ifdef FEAT_DIFF |
| 2394 | if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0 |
| 2395 | && !mch_isdir(alist_name(&GARGLIST[0]))) |
| 2396 | { |
| 2397 | char_u *r; |
| 2398 | |
| 2399 | r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE); |
| 2400 | if (r != NULL) |
| 2401 | { |
| 2402 | vim_free(p); |
| 2403 | p = r; |
| 2404 | } |
| 2405 | } |
| 2406 | #endif |
| 2407 | #if defined(__CYGWIN32__) && !defined(WIN32) |
| 2408 | /* |
| 2409 | * If vim is invoked by non-Cygwin tools, convert away any |
| 2410 | * DOS paths, so things like .swp files are created correctly. |
| 2411 | * Look for evidence of non-Cygwin paths before we bother. |
| 2412 | * This is only for when using the Unix files. |
| 2413 | */ |
Bram Moolenaar | fe17e76 | 2013-06-29 14:17:02 +0200 | [diff] [blame] | 2414 | if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p)) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2415 | { |
| 2416 | char posix_path[PATH_MAX]; |
| 2417 | |
Bram Moolenaar | 0d1498e | 2008-06-29 12:00:49 +0000 | [diff] [blame] | 2418 | # if CYGWIN_VERSION_DLL_MAJOR >= 1007 |
| 2419 | cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX); |
| 2420 | # else |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2421 | cygwin_conv_to_posix_path(p, posix_path); |
Bram Moolenaar | 0d1498e | 2008-06-29 12:00:49 +0000 | [diff] [blame] | 2422 | # endif |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2423 | vim_free(p); |
Bram Moolenaar | fe17e76 | 2013-06-29 14:17:02 +0200 | [diff] [blame] | 2424 | p = vim_strsave((char_u *)posix_path); |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2425 | if (p == NULL) |
| 2426 | mch_exit(2); |
| 2427 | } |
| 2428 | #endif |
Bram Moolenaar | cc016f5 | 2005-12-10 20:23:46 +0000 | [diff] [blame] | 2429 | |
| 2430 | #ifdef USE_FNAME_CASE |
| 2431 | /* Make the case of the file name match the actual file. */ |
| 2432 | fname_case(p, 0); |
| 2433 | #endif |
| 2434 | |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2435 | alist_add(&global_alist, p, |
Bram Moolenaar | 5307683 | 2015-12-31 19:53:21 +0100 | [diff] [blame] | 2436 | #ifdef EXPAND_FILENAMES |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 2437 | parmp->literal ? 2 : 0 /* add buffer nr after exp. */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2438 | #else |
| 2439 | 2 /* add buffer number now and use curbuf */ |
| 2440 | #endif |
| 2441 | ); |
| 2442 | |
| 2443 | #if defined(FEAT_MBYTE) && defined(WIN32) |
| 2444 | { |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2445 | /* Remember this argument has been added to the argument list. |
| 2446 | * Needed when 'encoding' is changed. */ |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2447 | used_file_arg(argv[0], parmp->literal, parmp->full_path, |
Bram Moolenaar | 688e5f7 | 2008-07-24 11:51:40 +0000 | [diff] [blame] | 2448 | # ifdef FEAT_DIFF |
| 2449 | parmp->diff_mode |
| 2450 | # else |
| 2451 | FALSE |
| 2452 | # endif |
| 2453 | ); |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2454 | } |
| 2455 | #endif |
| 2456 | } |
| 2457 | |
| 2458 | /* |
| 2459 | * If there are no more letters after the current "-", go to next |
| 2460 | * argument. argv_idx is set to -1 when the current argument is to be |
| 2461 | * skipped. |
| 2462 | */ |
| 2463 | if (argv_idx <= 0 || argv[0][argv_idx] == NUL) |
| 2464 | { |
| 2465 | --argc; |
| 2466 | ++argv; |
| 2467 | argv_idx = 1; |
| 2468 | } |
| 2469 | } |
Bram Moolenaar | 867a4b7 | 2007-03-18 20:51:46 +0000 | [diff] [blame] | 2470 | |
| 2471 | #ifdef FEAT_EVAL |
| 2472 | /* If there is a "+123" or "-c" command, set v:swapcommand to the first |
| 2473 | * one. */ |
| 2474 | if (parmp->n_commands > 0) |
| 2475 | { |
| 2476 | p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3); |
| 2477 | if (p != NULL) |
| 2478 | { |
| 2479 | sprintf((char *)p, ":%s\r", parmp->commands[0]); |
| 2480 | set_vim_var_string(VV_SWAPCOMMAND, p, -1); |
| 2481 | vim_free(p); |
| 2482 | } |
| 2483 | } |
| 2484 | #endif |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2485 | } |
| 2486 | |
| 2487 | /* |
| 2488 | * Print a warning if stdout is not a terminal. |
| 2489 | * When starting in Ex mode and commands come from a file, set Silent mode. |
| 2490 | */ |
| 2491 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 2492 | check_tty(mparm_T *parmp) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2493 | { |
| 2494 | int input_isatty; /* is active input a terminal? */ |
| 2495 | |
| 2496 | input_isatty = mch_input_isatty(); |
| 2497 | if (exmode_active) |
| 2498 | { |
| 2499 | if (!input_isatty) |
| 2500 | silent_mode = TRUE; |
| 2501 | } |
Bram Moolenaar | 2cab0e1 | 2016-11-24 15:09:07 +0100 | [diff] [blame] | 2502 | else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2503 | #ifdef FEAT_GUI |
| 2504 | /* don't want the delay when started from the desktop */ |
| 2505 | && !gui.starting |
| 2506 | #endif |
Bram Moolenaar | 49c39ff | 2016-02-25 21:21:52 +0100 | [diff] [blame] | 2507 | && !parmp->not_a_term) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2508 | { |
| 2509 | #ifdef NBDEBUG |
| 2510 | /* |
| 2511 | * This shouldn't be necessary. But if I run netbeans with the log |
| 2512 | * output coming to the console and XOpenDisplay fails, I get vim |
| 2513 | * trying to start with input/output to my console tty. This fills my |
| 2514 | * input buffer so fast I can't even kill the process in under 2 |
Bram Moolenaar | 4932594 | 2007-05-10 19:19:59 +0000 | [diff] [blame] | 2515 | * minutes (and it beeps continuously the whole time :-) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2516 | */ |
Bram Moolenaar | 2cab0e1 | 2016-11-24 15:09:07 +0100 | [diff] [blame] | 2517 | if (netbeans_active() && (!stdout_isatty || !input_isatty)) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2518 | { |
| 2519 | mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n")); |
| 2520 | exit(1); |
| 2521 | } |
| 2522 | #endif |
Bram Moolenaar | 97ff9b9 | 2016-06-26 20:37:46 +0200 | [diff] [blame] | 2523 | #if defined(WIN3264) && !defined(FEAT_GUI_W32) |
| 2524 | if (is_cygpty_used()) |
| 2525 | { |
| 2526 | mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n")); |
| 2527 | exit(1); |
| 2528 | } |
| 2529 | #endif |
Bram Moolenaar | 2cab0e1 | 2016-11-24 15:09:07 +0100 | [diff] [blame] | 2530 | if (!stdout_isatty) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2531 | mch_errmsg(_("Vim: Warning: Output is not to a terminal\n")); |
| 2532 | if (!input_isatty) |
| 2533 | mch_errmsg(_("Vim: Warning: Input is not from a terminal\n")); |
| 2534 | out_flush(); |
Bram Moolenaar | 2cab0e1 | 2016-11-24 15:09:07 +0100 | [diff] [blame] | 2535 | if (parmp->tty_fail && (!stdout_isatty || !input_isatty)) |
| 2536 | exit(1); |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2537 | if (scriptin[0] == NULL) |
| 2538 | ui_delay(2000L, TRUE); |
| 2539 | TIME_MSG("Warning delay"); |
| 2540 | } |
| 2541 | } |
| 2542 | |
| 2543 | /* |
| 2544 | * Read text from stdin. |
| 2545 | */ |
| 2546 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 2547 | read_stdin(void) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2548 | { |
| 2549 | int i; |
| 2550 | |
Bram Moolenaar | d5bc83f | 2005-12-07 21:07:59 +0000 | [diff] [blame] | 2551 | #if defined(HAS_SWAP_EXISTS_ACTION) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2552 | /* When getting the ATTENTION prompt here, use a dialog */ |
| 2553 | swap_exists_action = SEA_DIALOG; |
| 2554 | #endif |
| 2555 | no_wait_return = TRUE; |
| 2556 | i = msg_didany; |
| 2557 | set_buflisted(TRUE); |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 2558 | (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2559 | no_wait_return = FALSE; |
| 2560 | msg_didany = i; |
| 2561 | TIME_MSG("reading stdin"); |
Bram Moolenaar | d5bc83f | 2005-12-07 21:07:59 +0000 | [diff] [blame] | 2562 | #if defined(HAS_SWAP_EXISTS_ACTION) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2563 | check_swap_exists_action(); |
| 2564 | #endif |
| 2565 | #if !(defined(AMIGA) || defined(MACOS)) |
| 2566 | /* |
| 2567 | * Close stdin and dup it from stderr. Required for GPM to work |
| 2568 | * properly, and for running external commands. |
| 2569 | * Is there any other system that cannot do this? |
| 2570 | */ |
| 2571 | close(0); |
Bram Moolenaar | fe86f2d | 2008-11-28 20:29:07 +0000 | [diff] [blame] | 2572 | ignored = dup(2); |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2573 | #endif |
| 2574 | } |
| 2575 | |
| 2576 | /* |
| 2577 | * Create the requested number of windows and edit buffers in them. |
| 2578 | * Also does recovery if "recoverymode" set. |
| 2579 | */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2580 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 2581 | create_windows(mparm_T *parmp UNUSED) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2582 | { |
| 2583 | #ifdef FEAT_WINDOWS |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2584 | int dorewind; |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 2585 | int done = 0; |
| 2586 | |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2587 | /* |
| 2588 | * Create the number of windows that was requested. |
| 2589 | */ |
| 2590 | if (parmp->window_count == -1) /* was not set */ |
| 2591 | parmp->window_count = 1; |
| 2592 | if (parmp->window_count == 0) |
| 2593 | parmp->window_count = GARGCOUNT; |
| 2594 | if (parmp->window_count > 1) |
| 2595 | { |
| 2596 | /* Don't change the windows if there was a command in .vimrc that |
| 2597 | * already split some windows */ |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 2598 | if (parmp->window_layout == 0) |
| 2599 | parmp->window_layout = WIN_HOR; |
| 2600 | if (parmp->window_layout == WIN_TABS) |
| 2601 | { |
| 2602 | parmp->window_count = make_tabpages(parmp->window_count); |
| 2603 | TIME_MSG("making tab pages"); |
| 2604 | } |
| 2605 | else if (firstwin->w_next == NULL) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2606 | { |
| 2607 | parmp->window_count = make_windows(parmp->window_count, |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 2608 | parmp->window_layout == WIN_VER); |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2609 | TIME_MSG("making windows"); |
| 2610 | } |
| 2611 | else |
| 2612 | parmp->window_count = win_count(); |
| 2613 | } |
| 2614 | else |
| 2615 | parmp->window_count = 1; |
| 2616 | #endif |
| 2617 | |
| 2618 | if (recoverymode) /* do recover */ |
| 2619 | { |
| 2620 | msg_scroll = TRUE; /* scroll message up */ |
| 2621 | ml_recover(); |
| 2622 | if (curbuf->b_ml.ml_mfp == NULL) /* failed */ |
| 2623 | getout(1); |
Bram Moolenaar | a3227e2 | 2006-03-08 21:32:40 +0000 | [diff] [blame] | 2624 | do_modelines(0); /* do modelines */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2625 | } |
| 2626 | else |
| 2627 | { |
| 2628 | /* |
| 2629 | * Open a buffer for windows that don't have one yet. |
| 2630 | * Commands in the .vimrc might have loaded a file or split the window. |
| 2631 | * Watch out for autocommands that delete a window. |
| 2632 | */ |
| 2633 | #ifdef FEAT_AUTOCMD |
| 2634 | /* |
| 2635 | * Don't execute Win/Buf Enter/Leave autocommands here |
| 2636 | */ |
| 2637 | ++autocmd_no_enter; |
| 2638 | ++autocmd_no_leave; |
| 2639 | #endif |
| 2640 | #ifdef FEAT_WINDOWS |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2641 | dorewind = TRUE; |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 2642 | while (done++ < 1000) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2643 | { |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2644 | if (dorewind) |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 2645 | { |
| 2646 | if (parmp->window_layout == WIN_TABS) |
| 2647 | goto_tabpage(1); |
| 2648 | else |
| 2649 | curwin = firstwin; |
| 2650 | } |
| 2651 | else if (parmp->window_layout == WIN_TABS) |
| 2652 | { |
| 2653 | if (curtab->tp_next == NULL) |
| 2654 | break; |
| 2655 | goto_tabpage(0); |
| 2656 | } |
| 2657 | else |
| 2658 | { |
| 2659 | if (curwin->w_next == NULL) |
| 2660 | break; |
| 2661 | curwin = curwin->w_next; |
| 2662 | } |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2663 | dorewind = FALSE; |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 2664 | #endif |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2665 | curbuf = curwin->w_buffer; |
| 2666 | if (curbuf->b_ml.ml_mfp == NULL) |
| 2667 | { |
| 2668 | #ifdef FEAT_FOLDING |
| 2669 | /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */ |
| 2670 | if (p_fdls >= 0) |
| 2671 | curwin->w_p_fdl = p_fdls; |
| 2672 | #endif |
Bram Moolenaar | d5bc83f | 2005-12-07 21:07:59 +0000 | [diff] [blame] | 2673 | #if defined(HAS_SWAP_EXISTS_ACTION) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2674 | /* When getting the ATTENTION prompt here, use a dialog */ |
| 2675 | swap_exists_action = SEA_DIALOG; |
| 2676 | #endif |
| 2677 | set_buflisted(TRUE); |
Bram Moolenaar | 59f931e | 2010-07-24 20:27:03 +0200 | [diff] [blame] | 2678 | |
| 2679 | /* create memfile, read file */ |
| 2680 | (void)open_buffer(FALSE, NULL, 0); |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2681 | |
Bram Moolenaar | d5bc83f | 2005-12-07 21:07:59 +0000 | [diff] [blame] | 2682 | #if defined(HAS_SWAP_EXISTS_ACTION) |
Bram Moolenaar | 8421282 | 2006-11-07 21:59:47 +0000 | [diff] [blame] | 2683 | if (swap_exists_action == SEA_QUIT) |
| 2684 | { |
| 2685 | if (got_int || only_one_window()) |
| 2686 | { |
| 2687 | /* abort selected or quit and only one window */ |
| 2688 | did_emsg = FALSE; /* avoid hit-enter prompt */ |
| 2689 | getout(1); |
| 2690 | } |
| 2691 | /* We can't close the window, it would disturb what |
| 2692 | * happens next. Clear the file name and set the arg |
| 2693 | * index to -1 to delete it later. */ |
| 2694 | setfname(curbuf, NULL, NULL, FALSE); |
| 2695 | curwin->w_arg_idx = -1; |
| 2696 | swap_exists_action = SEA_NONE; |
| 2697 | } |
| 2698 | else |
| 2699 | handle_swap_exists(NULL); |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2700 | #endif |
| 2701 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2702 | dorewind = TRUE; /* start again */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2703 | #endif |
| 2704 | } |
| 2705 | #ifdef FEAT_WINDOWS |
| 2706 | ui_breakcheck(); |
| 2707 | if (got_int) |
| 2708 | { |
| 2709 | (void)vgetc(); /* only break the file loading, not the rest */ |
| 2710 | break; |
| 2711 | } |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2712 | } |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 2713 | #endif |
| 2714 | #ifdef FEAT_WINDOWS |
| 2715 | if (parmp->window_layout == WIN_TABS) |
| 2716 | goto_tabpage(1); |
| 2717 | else |
| 2718 | curwin = firstwin; |
| 2719 | curbuf = curwin->w_buffer; |
| 2720 | #endif |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2721 | #ifdef FEAT_AUTOCMD |
| 2722 | --autocmd_no_enter; |
| 2723 | --autocmd_no_leave; |
| 2724 | #endif |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2725 | } |
| 2726 | } |
| 2727 | |
| 2728 | #ifdef FEAT_WINDOWS |
| 2729 | /* |
| 2730 | * If opened more than one window, start editing files in the other |
| 2731 | * windows. make_windows() has already opened the windows. |
| 2732 | */ |
| 2733 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 2734 | edit_buffers( |
| 2735 | mparm_T *parmp, |
| 2736 | char_u *cwd) /* current working dir */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2737 | { |
| 2738 | int arg_idx; /* index in argument list */ |
| 2739 | int i; |
Bram Moolenaar | 8421282 | 2006-11-07 21:59:47 +0000 | [diff] [blame] | 2740 | int advance = TRUE; |
Bram Moolenaar | 74cd624 | 2013-08-22 14:14:27 +0200 | [diff] [blame] | 2741 | win_T *win; |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2742 | |
| 2743 | # ifdef FEAT_AUTOCMD |
| 2744 | /* |
| 2745 | * Don't execute Win/Buf Enter/Leave autocommands here |
| 2746 | */ |
| 2747 | ++autocmd_no_enter; |
| 2748 | ++autocmd_no_leave; |
| 2749 | # endif |
Bram Moolenaar | 8421282 | 2006-11-07 21:59:47 +0000 | [diff] [blame] | 2750 | |
| 2751 | /* When w_arg_idx is -1 remove the window (see create_windows()). */ |
| 2752 | if (curwin->w_arg_idx == -1) |
| 2753 | { |
| 2754 | win_close(curwin, TRUE); |
| 2755 | advance = FALSE; |
| 2756 | } |
| 2757 | |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2758 | arg_idx = 1; |
| 2759 | for (i = 1; i < parmp->window_count; ++i) |
| 2760 | { |
Bram Moolenaar | d87c36e | 2015-04-03 14:56:49 +0200 | [diff] [blame] | 2761 | if (cwd != NULL) |
| 2762 | mch_chdir((char *)cwd); |
Bram Moolenaar | 8421282 | 2006-11-07 21:59:47 +0000 | [diff] [blame] | 2763 | /* When w_arg_idx is -1 remove the window (see create_windows()). */ |
| 2764 | if (curwin->w_arg_idx == -1) |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 2765 | { |
Bram Moolenaar | 8421282 | 2006-11-07 21:59:47 +0000 | [diff] [blame] | 2766 | ++arg_idx; |
| 2767 | win_close(curwin, TRUE); |
| 2768 | advance = FALSE; |
| 2769 | continue; |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 2770 | } |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2771 | |
Bram Moolenaar | 8421282 | 2006-11-07 21:59:47 +0000 | [diff] [blame] | 2772 | if (advance) |
| 2773 | { |
| 2774 | if (parmp->window_layout == WIN_TABS) |
| 2775 | { |
| 2776 | if (curtab->tp_next == NULL) /* just checking */ |
| 2777 | break; |
| 2778 | goto_tabpage(0); |
| 2779 | } |
| 2780 | else |
| 2781 | { |
| 2782 | if (curwin->w_next == NULL) /* just checking */ |
| 2783 | break; |
| 2784 | win_enter(curwin->w_next, FALSE); |
| 2785 | } |
| 2786 | } |
| 2787 | advance = TRUE; |
| 2788 | |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2789 | /* Only open the file if there is no file in this window yet (that can |
Bram Moolenaar | 8421282 | 2006-11-07 21:59:47 +0000 | [diff] [blame] | 2790 | * happen when .vimrc contains ":sall"). */ |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2791 | if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL) |
| 2792 | { |
| 2793 | curwin->w_arg_idx = arg_idx; |
Bram Moolenaar | 8421282 | 2006-11-07 21:59:47 +0000 | [diff] [blame] | 2794 | /* Edit file from arg list, if there is one. When "Quit" selected |
| 2795 | * at the ATTENTION prompt close the window. */ |
Bram Moolenaar | 4bfa608 | 2008-07-24 17:34:23 +0000 | [diff] [blame] | 2796 | # ifdef HAS_SWAP_EXISTS_ACTION |
| 2797 | swap_exists_did_quit = FALSE; |
| 2798 | # endif |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2799 | (void)do_ecmd(0, arg_idx < GARGCOUNT |
| 2800 | ? alist_name(&GARGLIST[arg_idx]) : NULL, |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 2801 | NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin); |
Bram Moolenaar | 4bfa608 | 2008-07-24 17:34:23 +0000 | [diff] [blame] | 2802 | # ifdef HAS_SWAP_EXISTS_ACTION |
| 2803 | if (swap_exists_did_quit) |
Bram Moolenaar | 8421282 | 2006-11-07 21:59:47 +0000 | [diff] [blame] | 2804 | { |
Bram Moolenaar | 4bfa608 | 2008-07-24 17:34:23 +0000 | [diff] [blame] | 2805 | /* abort or quit selected */ |
Bram Moolenaar | 8421282 | 2006-11-07 21:59:47 +0000 | [diff] [blame] | 2806 | if (got_int || only_one_window()) |
| 2807 | { |
Bram Moolenaar | 4bfa608 | 2008-07-24 17:34:23 +0000 | [diff] [blame] | 2808 | /* abort selected and only one window */ |
Bram Moolenaar | 8421282 | 2006-11-07 21:59:47 +0000 | [diff] [blame] | 2809 | did_emsg = FALSE; /* avoid hit-enter prompt */ |
| 2810 | getout(1); |
| 2811 | } |
| 2812 | win_close(curwin, TRUE); |
| 2813 | advance = FALSE; |
| 2814 | } |
Bram Moolenaar | 4bfa608 | 2008-07-24 17:34:23 +0000 | [diff] [blame] | 2815 | # endif |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2816 | if (arg_idx == GARGCOUNT - 1) |
| 2817 | arg_had_last = TRUE; |
| 2818 | ++arg_idx; |
| 2819 | } |
| 2820 | ui_breakcheck(); |
| 2821 | if (got_int) |
| 2822 | { |
| 2823 | (void)vgetc(); /* only break the file loading, not the rest */ |
| 2824 | break; |
| 2825 | } |
| 2826 | } |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 2827 | |
| 2828 | if (parmp->window_layout == WIN_TABS) |
| 2829 | goto_tabpage(1); |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2830 | # ifdef FEAT_AUTOCMD |
| 2831 | --autocmd_no_enter; |
| 2832 | # endif |
Bram Moolenaar | e66f06d | 2013-06-15 21:54:16 +0200 | [diff] [blame] | 2833 | |
Bram Moolenaar | 74cd624 | 2013-08-22 14:14:27 +0200 | [diff] [blame] | 2834 | /* make the first window the current window */ |
| 2835 | win = firstwin; |
| 2836 | #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) |
| 2837 | /* Avoid making a preview window the current window. */ |
| 2838 | while (win->w_p_pvw) |
| 2839 | { |
| 2840 | win = win->w_next; |
| 2841 | if (win == NULL) |
| 2842 | { |
| 2843 | win = firstwin; |
| 2844 | break; |
| 2845 | } |
Bram Moolenaar | e66f06d | 2013-06-15 21:54:16 +0200 | [diff] [blame] | 2846 | } |
| 2847 | #endif |
Bram Moolenaar | 74cd624 | 2013-08-22 14:14:27 +0200 | [diff] [blame] | 2848 | win_enter(win, FALSE); |
Bram Moolenaar | e66f06d | 2013-06-15 21:54:16 +0200 | [diff] [blame] | 2849 | |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2850 | # ifdef FEAT_AUTOCMD |
| 2851 | --autocmd_no_leave; |
| 2852 | # endif |
| 2853 | TIME_MSG("editing files in windows"); |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 2854 | if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2855 | win_equal(curwin, FALSE, 'b'); /* adjust heights */ |
| 2856 | } |
| 2857 | #endif /* FEAT_WINDOWS */ |
| 2858 | |
| 2859 | /* |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 2860 | * Execute the commands from --cmd arguments "cmds[cnt]". |
| 2861 | */ |
| 2862 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 2863 | exe_pre_commands(mparm_T *parmp) |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 2864 | { |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2865 | char_u **cmds = parmp->pre_commands; |
| 2866 | int cnt = parmp->n_pre_commands; |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 2867 | int i; |
| 2868 | |
| 2869 | if (cnt > 0) |
| 2870 | { |
| 2871 | curwin->w_cursor.lnum = 0; /* just in case.. */ |
| 2872 | sourcing_name = (char_u *)_("pre-vimrc command line"); |
| 2873 | # ifdef FEAT_EVAL |
| 2874 | current_SID = SID_CMDARG; |
| 2875 | # endif |
| 2876 | for (i = 0; i < cnt; ++i) |
| 2877 | do_cmdline_cmd(cmds[i]); |
| 2878 | sourcing_name = NULL; |
| 2879 | # ifdef FEAT_EVAL |
| 2880 | current_SID = 0; |
| 2881 | # endif |
| 2882 | TIME_MSG("--cmd commands"); |
| 2883 | } |
| 2884 | } |
| 2885 | |
| 2886 | /* |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2887 | * Execute "+", "-c" and "-S" arguments. |
| 2888 | */ |
| 2889 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 2890 | exe_commands(mparm_T *parmp) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2891 | { |
| 2892 | int i; |
| 2893 | |
| 2894 | /* |
| 2895 | * We start commands on line 0, make "vim +/pat file" match a |
| 2896 | * pattern on line 1. But don't move the cursor when an autocommand |
| 2897 | * with g`" was used. |
| 2898 | */ |
| 2899 | msg_scroll = TRUE; |
| 2900 | if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1) |
| 2901 | curwin->w_cursor.lnum = 0; |
| 2902 | sourcing_name = (char_u *)"command line"; |
| 2903 | #ifdef FEAT_EVAL |
| 2904 | current_SID = SID_CARG; |
| 2905 | #endif |
| 2906 | for (i = 0; i < parmp->n_commands; ++i) |
| 2907 | { |
| 2908 | do_cmdline_cmd(parmp->commands[i]); |
| 2909 | if (parmp->cmds_tofree[i]) |
| 2910 | vim_free(parmp->commands[i]); |
| 2911 | } |
| 2912 | sourcing_name = NULL; |
| 2913 | #ifdef FEAT_EVAL |
| 2914 | current_SID = 0; |
| 2915 | #endif |
| 2916 | if (curwin->w_cursor.lnum == 0) |
| 2917 | curwin->w_cursor.lnum = 1; |
| 2918 | |
| 2919 | if (!exmode_active) |
| 2920 | msg_scroll = FALSE; |
| 2921 | |
| 2922 | #ifdef FEAT_QUICKFIX |
| 2923 | /* When started with "-q errorfile" jump to first error again. */ |
| 2924 | if (parmp->edit_type == EDIT_QF) |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 2925 | qf_jump(NULL, 0, 0, FALSE); |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2926 | #endif |
| 2927 | TIME_MSG("executing command arguments"); |
| 2928 | } |
| 2929 | |
| 2930 | /* |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 2931 | * Source startup scripts. |
| 2932 | */ |
| 2933 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 2934 | source_startup_scripts(mparm_T *parmp) |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 2935 | { |
| 2936 | int i; |
| 2937 | |
| 2938 | /* |
| 2939 | * For "evim" source evim.vim first of all, so that the user can overrule |
| 2940 | * any things he doesn't like. |
| 2941 | */ |
| 2942 | if (parmp->evim_mode) |
| 2943 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2944 | (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE); |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 2945 | TIME_MSG("source evim file"); |
| 2946 | } |
| 2947 | |
| 2948 | /* |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 2949 | * If -u argument given, use only the initializations from that file and |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 2950 | * nothing else. |
| 2951 | */ |
| 2952 | if (parmp->use_vimrc != NULL) |
| 2953 | { |
Bram Moolenaar | 231334e | 2005-07-25 20:46:57 +0000 | [diff] [blame] | 2954 | if (STRCMP(parmp->use_vimrc, "NONE") == 0 |
| 2955 | || STRCMP(parmp->use_vimrc, "NORC") == 0) |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 2956 | { |
| 2957 | #ifdef FEAT_GUI |
| 2958 | if (use_gvimrc == NULL) /* don't load gvimrc either */ |
| 2959 | use_gvimrc = parmp->use_vimrc; |
| 2960 | #endif |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 2961 | } |
| 2962 | else |
| 2963 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2964 | if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK) |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 2965 | EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc); |
| 2966 | } |
| 2967 | } |
| 2968 | else if (!silent_mode) |
| 2969 | { |
| 2970 | #ifdef AMIGA |
| 2971 | struct Process *proc = (struct Process *)FindTask(0L); |
| 2972 | APTR save_winptr = proc->pr_WindowPtr; |
| 2973 | |
| 2974 | /* Avoid a requester here for a volume that doesn't exist. */ |
| 2975 | proc->pr_WindowPtr = (APTR)-1L; |
| 2976 | #endif |
| 2977 | |
| 2978 | /* |
| 2979 | * Get system wide defaults, if the file name is defined. |
| 2980 | */ |
| 2981 | #ifdef SYS_VIMRC_FILE |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2982 | (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE); |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 2983 | #endif |
Bram Moolenaar | 1056d98 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 2984 | #ifdef MACOS_X |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2985 | (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE); |
Bram Moolenaar | 1056d98 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 2986 | #endif |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 2987 | |
| 2988 | /* |
| 2989 | * Try to read initialization commands from the following places: |
| 2990 | * - environment variable VIMINIT |
| 2991 | * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise) |
| 2992 | * - second user vimrc file ($VIM/.vimrc for Dos) |
| 2993 | * - environment variable EXINIT |
| 2994 | * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise) |
| 2995 | * - second user exrc file ($VIM/.exrc for Dos) |
| 2996 | * The first that exists is used, the rest is ignored. |
| 2997 | */ |
| 2998 | if (process_env((char_u *)"VIMINIT", TRUE) != OK) |
| 2999 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3000 | if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 3001 | #ifdef USR_VIMRC_FILE2 |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3002 | && do_source((char_u *)USR_VIMRC_FILE2, TRUE, |
| 3003 | DOSO_VIMRC) == FAIL |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 3004 | #endif |
| 3005 | #ifdef USR_VIMRC_FILE3 |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3006 | && do_source((char_u *)USR_VIMRC_FILE3, TRUE, |
| 3007 | DOSO_VIMRC) == FAIL |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 3008 | #endif |
Bram Moolenaar | 22971aa | 2013-06-12 20:35:58 +0200 | [diff] [blame] | 3009 | #ifdef USR_VIMRC_FILE4 |
| 3010 | && do_source((char_u *)USR_VIMRC_FILE4, TRUE, |
| 3011 | DOSO_VIMRC) == FAIL |
| 3012 | #endif |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 3013 | && process_env((char_u *)"EXINIT", FALSE) == FAIL |
Bram Moolenaar | 8c08b5b | 2016-07-28 22:24:15 +0200 | [diff] [blame] | 3014 | && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 3015 | #ifdef USR_EXRC_FILE2 |
Bram Moolenaar | 8c08b5b | 2016-07-28 22:24:15 +0200 | [diff] [blame] | 3016 | && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 3017 | #endif |
Bram Moolenaar | b9a46fe | 2016-07-29 18:13:42 +0200 | [diff] [blame] | 3018 | && !has_dash_c_arg) |
Bram Moolenaar | 8c08b5b | 2016-07-28 22:24:15 +0200 | [diff] [blame] | 3019 | { |
| 3020 | /* When no .vimrc file was found: source defaults.vim. */ |
| 3021 | do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE); |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 3022 | } |
| 3023 | } |
| 3024 | |
| 3025 | /* |
| 3026 | * Read initialization commands from ".vimrc" or ".exrc" in current |
| 3027 | * directory. This is only done if the 'exrc' option is set. |
| 3028 | * Because of security reasons we disallow shell and write commands |
Bram Moolenaar | 8c08b5b | 2016-07-28 22:24:15 +0200 | [diff] [blame] | 3029 | * now, except for Unix if the file is owned by the user or 'secure' |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 3030 | * option has been reset in environment of global ".exrc" or ".vimrc". |
| 3031 | * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or |
| 3032 | * SYS_VIMRC_FILE. |
| 3033 | */ |
| 3034 | if (p_exrc) |
| 3035 | { |
| 3036 | #if defined(UNIX) || defined(VMS) |
| 3037 | /* If ".vimrc" file is not owned by user, set 'secure' mode. */ |
| 3038 | if (!file_owned(VIMRC_FILE)) |
| 3039 | #endif |
| 3040 | secure = p_secure; |
| 3041 | |
| 3042 | i = FAIL; |
| 3043 | if (fullpathcmp((char_u *)USR_VIMRC_FILE, |
| 3044 | (char_u *)VIMRC_FILE, FALSE) != FPC_SAME |
| 3045 | #ifdef USR_VIMRC_FILE2 |
| 3046 | && fullpathcmp((char_u *)USR_VIMRC_FILE2, |
| 3047 | (char_u *)VIMRC_FILE, FALSE) != FPC_SAME |
| 3048 | #endif |
| 3049 | #ifdef USR_VIMRC_FILE3 |
| 3050 | && fullpathcmp((char_u *)USR_VIMRC_FILE3, |
| 3051 | (char_u *)VIMRC_FILE, FALSE) != FPC_SAME |
| 3052 | #endif |
| 3053 | #ifdef SYS_VIMRC_FILE |
| 3054 | && fullpathcmp((char_u *)SYS_VIMRC_FILE, |
| 3055 | (char_u *)VIMRC_FILE, FALSE) != FPC_SAME |
| 3056 | #endif |
| 3057 | ) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3058 | i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC); |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 3059 | |
| 3060 | if (i == FAIL) |
| 3061 | { |
| 3062 | #if defined(UNIX) || defined(VMS) |
| 3063 | /* if ".exrc" is not owned by user set 'secure' mode */ |
| 3064 | if (!file_owned(EXRC_FILE)) |
| 3065 | secure = p_secure; |
| 3066 | else |
| 3067 | secure = 0; |
| 3068 | #endif |
| 3069 | if ( fullpathcmp((char_u *)USR_EXRC_FILE, |
| 3070 | (char_u *)EXRC_FILE, FALSE) != FPC_SAME |
| 3071 | #ifdef USR_EXRC_FILE2 |
| 3072 | && fullpathcmp((char_u *)USR_EXRC_FILE2, |
| 3073 | (char_u *)EXRC_FILE, FALSE) != FPC_SAME |
| 3074 | #endif |
| 3075 | ) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3076 | (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE); |
Bram Moolenaar | 58d9823 | 2005-07-23 22:25:46 +0000 | [diff] [blame] | 3077 | } |
| 3078 | } |
| 3079 | if (secure == 2) |
| 3080 | need_wait_return = TRUE; |
| 3081 | secure = 0; |
| 3082 | #ifdef AMIGA |
| 3083 | proc->pr_WindowPtr = save_winptr; |
| 3084 | #endif |
| 3085 | } |
| 3086 | TIME_MSG("sourcing vimrc file(s)"); |
| 3087 | } |
| 3088 | |
| 3089 | /* |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3090 | * Setup to start using the GUI. Exit with an error when not available. |
| 3091 | */ |
| 3092 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 3093 | main_start_gui(void) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3094 | { |
| 3095 | #ifdef FEAT_GUI |
| 3096 | gui.starting = TRUE; /* start GUI a bit later */ |
| 3097 | #else |
| 3098 | mch_errmsg(_(e_nogvim)); |
| 3099 | mch_errmsg("\n"); |
| 3100 | mch_exit(2); |
| 3101 | #endif |
| 3102 | } |
| 3103 | |
Bram Moolenaar | b05b10a | 2011-03-22 18:10:45 +0100 | [diff] [blame] | 3104 | #endif /* NO_VIM_MAIN */ |
| 3105 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3106 | /* |
Bram Moolenaar | 4932594 | 2007-05-10 19:19:59 +0000 | [diff] [blame] | 3107 | * Get an environment variable, and execute it as Ex commands. |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3108 | * Returns FAIL if the environment variable was not executed, OK otherwise. |
| 3109 | */ |
| 3110 | int |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 3111 | process_env( |
| 3112 | char_u *env, |
| 3113 | int is_viminit) /* when TRUE, called for VIMINIT */ |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3114 | { |
| 3115 | char_u *initstr; |
| 3116 | char_u *save_sourcing_name; |
| 3117 | linenr_T save_sourcing_lnum; |
| 3118 | #ifdef FEAT_EVAL |
| 3119 | scid_T save_sid; |
| 3120 | #endif |
| 3121 | |
| 3122 | if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL) |
| 3123 | { |
| 3124 | if (is_viminit) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3125 | vimrc_found(NULL, NULL); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3126 | save_sourcing_name = sourcing_name; |
| 3127 | save_sourcing_lnum = sourcing_lnum; |
| 3128 | sourcing_name = env; |
| 3129 | sourcing_lnum = 0; |
| 3130 | #ifdef FEAT_EVAL |
| 3131 | save_sid = current_SID; |
| 3132 | current_SID = SID_ENV; |
| 3133 | #endif |
| 3134 | do_cmdline_cmd(initstr); |
| 3135 | sourcing_name = save_sourcing_name; |
| 3136 | sourcing_lnum = save_sourcing_lnum; |
| 3137 | #ifdef FEAT_EVAL |
Bram Moolenaar | 945ec09 | 2016-06-08 21:17:43 +0200 | [diff] [blame] | 3138 | current_SID = save_sid; |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3139 | #endif |
| 3140 | return OK; |
| 3141 | } |
| 3142 | return FAIL; |
| 3143 | } |
| 3144 | |
Bram Moolenaar | b05b10a | 2011-03-22 18:10:45 +0100 | [diff] [blame] | 3145 | #if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3146 | /* |
| 3147 | * Return TRUE if we are certain the user owns the file "fname". |
| 3148 | * Used for ".vimrc" and ".exrc". |
| 3149 | * Use both stat() and lstat() for extra security. |
| 3150 | */ |
| 3151 | static int |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 3152 | file_owned(char *fname) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3153 | { |
Bram Moolenaar | 8767f52 | 2016-07-01 17:17:39 +0200 | [diff] [blame] | 3154 | stat_T s; |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3155 | # ifdef UNIX |
| 3156 | uid_t uid = getuid(); |
| 3157 | # else /* VMS */ |
| 3158 | uid_t uid = ((getgid() << 16) | getuid()); |
| 3159 | # endif |
| 3160 | |
| 3161 | return !(mch_stat(fname, &s) != 0 || s.st_uid != uid |
| 3162 | # ifdef HAVE_LSTAT |
| 3163 | || mch_lstat(fname, &s) != 0 || s.st_uid != uid |
| 3164 | # endif |
| 3165 | ); |
| 3166 | } |
| 3167 | #endif |
| 3168 | |
| 3169 | /* |
| 3170 | * Give an error message main_errors["n"] and exit. |
| 3171 | */ |
| 3172 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 3173 | mainerr( |
| 3174 | int n, /* one of the ME_ defines */ |
| 3175 | char_u *str) /* extra argument or NULL */ |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3176 | { |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 3177 | #if defined(UNIX) || defined(VMS) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3178 | reset_signals(); /* kill us with CTRL-C here, if you like */ |
| 3179 | #endif |
| 3180 | |
| 3181 | mch_errmsg(longVersion); |
Bram Moolenaar | 2a8d1f8 | 2005-02-05 21:43:56 +0000 | [diff] [blame] | 3182 | mch_errmsg("\n"); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3183 | mch_errmsg(_(main_errors[n])); |
| 3184 | if (str != NULL) |
| 3185 | { |
| 3186 | mch_errmsg(": \""); |
| 3187 | mch_errmsg((char *)str); |
| 3188 | mch_errmsg("\""); |
| 3189 | } |
Bram Moolenaar | 2a8d1f8 | 2005-02-05 21:43:56 +0000 | [diff] [blame] | 3190 | mch_errmsg(_("\nMore info with: \"vim -h\"\n")); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3191 | |
| 3192 | mch_exit(1); |
| 3193 | } |
| 3194 | |
| 3195 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 3196 | mainerr_arg_missing(char_u *str) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3197 | { |
| 3198 | mainerr(ME_ARG_MISSING, str); |
| 3199 | } |
| 3200 | |
Bram Moolenaar | b05b10a | 2011-03-22 18:10:45 +0100 | [diff] [blame] | 3201 | #ifndef NO_VIM_MAIN |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3202 | /* |
| 3203 | * print a message with three spaces prepended and '\n' appended. |
| 3204 | */ |
| 3205 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 3206 | main_msg(char *s) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3207 | { |
| 3208 | mch_msg(" "); |
| 3209 | mch_msg(s); |
| 3210 | mch_msg("\n"); |
| 3211 | } |
| 3212 | |
| 3213 | /* |
| 3214 | * Print messages for "vim -h" or "vim --help" and exit. |
| 3215 | */ |
| 3216 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 3217 | usage(void) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3218 | { |
| 3219 | int i; |
| 3220 | static char *(use[]) = |
| 3221 | { |
| 3222 | N_("[file ..] edit specified file(s)"), |
| 3223 | N_("- read text from stdin"), |
| 3224 | N_("-t tag edit file where tag is defined"), |
| 3225 | #ifdef FEAT_QUICKFIX |
| 3226 | N_("-q [errorfile] edit file with first error") |
| 3227 | #endif |
| 3228 | }; |
| 3229 | |
Bram Moolenaar | a06ecab | 2016-07-16 14:47:36 +0200 | [diff] [blame] | 3230 | #if defined(UNIX) || defined(VMS) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3231 | reset_signals(); /* kill us with CTRL-C here, if you like */ |
| 3232 | #endif |
| 3233 | |
| 3234 | mch_msg(longVersion); |
| 3235 | mch_msg(_("\n\nusage:")); |
| 3236 | for (i = 0; ; ++i) |
| 3237 | { |
| 3238 | mch_msg(_(" vim [arguments] ")); |
| 3239 | mch_msg(_(use[i])); |
| 3240 | if (i == (sizeof(use) / sizeof(char_u *)) - 1) |
| 3241 | break; |
| 3242 | mch_msg(_("\n or:")); |
| 3243 | } |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 3244 | #ifdef VMS |
Bram Moolenaar | 8cfdc0d | 2007-05-06 14:12:36 +0000 | [diff] [blame] | 3245 | mch_msg(_("\nWhere case is ignored prepend / to make flag upper case")); |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 3246 | #endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3247 | |
| 3248 | mch_msg(_("\n\nArguments:\n")); |
| 3249 | main_msg(_("--\t\t\tOnly file names after this")); |
Bram Moolenaar | 5307683 | 2015-12-31 19:53:21 +0100 | [diff] [blame] | 3250 | #ifdef EXPAND_FILENAMES |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3251 | main_msg(_("--literal\t\tDon't expand wildcards")); |
| 3252 | #endif |
| 3253 | #ifdef FEAT_OLE |
| 3254 | main_msg(_("-register\t\tRegister this gvim for OLE")); |
| 3255 | main_msg(_("-unregister\t\tUnregister gvim for OLE")); |
| 3256 | #endif |
| 3257 | #ifdef FEAT_GUI |
| 3258 | main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")")); |
| 3259 | main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI")); |
| 3260 | #endif |
| 3261 | main_msg(_("-v\t\t\tVi mode (like \"vi\")")); |
| 3262 | main_msg(_("-e\t\t\tEx mode (like \"ex\")")); |
Bram Moolenaar | f99bc6d | 2012-03-28 17:10:31 +0200 | [diff] [blame] | 3263 | main_msg(_("-E\t\t\tImproved Ex mode")); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3264 | main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")")); |
| 3265 | #ifdef FEAT_DIFF |
| 3266 | main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")")); |
| 3267 | #endif |
| 3268 | main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)")); |
| 3269 | main_msg(_("-R\t\t\tReadonly mode (like \"view\")")); |
| 3270 | main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")")); |
| 3271 | main_msg(_("-m\t\t\tModifications (writing files) not allowed")); |
| 3272 | main_msg(_("-M\t\t\tModifications in text not allowed")); |
| 3273 | main_msg(_("-b\t\t\tBinary mode")); |
| 3274 | #ifdef FEAT_LISP |
| 3275 | main_msg(_("-l\t\t\tLisp mode")); |
| 3276 | #endif |
| 3277 | main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'")); |
| 3278 | main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'")); |
Bram Moolenaar | 8cfdc0d | 2007-05-06 14:12:36 +0000 | [diff] [blame] | 3279 | main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]")); |
| 3280 | #ifdef FEAT_EVAL |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3281 | main_msg(_("-D\t\t\tDebugging mode")); |
Bram Moolenaar | 8cfdc0d | 2007-05-06 14:12:36 +0000 | [diff] [blame] | 3282 | #endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3283 | main_msg(_("-n\t\t\tNo swap file, use memory only")); |
| 3284 | main_msg(_("-r\t\t\tList swap files and exit")); |
| 3285 | main_msg(_("-r (with file name)\tRecover crashed session")); |
| 3286 | main_msg(_("-L\t\t\tSame as -r")); |
| 3287 | #ifdef AMIGA |
| 3288 | main_msg(_("-f\t\t\tDon't use newcli to open window")); |
| 3289 | main_msg(_("-dev <device>\t\tUse <device> for I/O")); |
| 3290 | #endif |
| 3291 | #ifdef FEAT_ARABIC |
| 3292 | main_msg(_("-A\t\t\tstart in Arabic mode")); |
| 3293 | #endif |
| 3294 | #ifdef FEAT_RIGHTLEFT |
| 3295 | main_msg(_("-H\t\t\tStart in Hebrew mode")); |
| 3296 | #endif |
| 3297 | #ifdef FEAT_FKMAP |
| 3298 | main_msg(_("-F\t\t\tStart in Farsi mode")); |
| 3299 | #endif |
| 3300 | main_msg(_("-T <terminal>\tSet terminal type to <terminal>")); |
Bram Moolenaar | 49c39ff | 2016-02-25 21:21:52 +0100 | [diff] [blame] | 3301 | main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal")); |
Bram Moolenaar | 2cab0e1 | 2016-11-24 15:09:07 +0100 | [diff] [blame] | 3302 | main_msg(_("--ttyfail\t\tExit if input or output is not a terminal")); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3303 | main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc")); |
| 3304 | #ifdef FEAT_GUI |
| 3305 | main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc")); |
| 3306 | #endif |
| 3307 | main_msg(_("--noplugin\t\tDon't load plugin scripts")); |
Bram Moolenaar | 8cfdc0d | 2007-05-06 14:12:36 +0000 | [diff] [blame] | 3308 | #ifdef FEAT_WINDOWS |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 3309 | main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)")); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3310 | main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)")); |
| 3311 | main_msg(_("-O[N]\t\tLike -o but split vertically")); |
Bram Moolenaar | 8cfdc0d | 2007-05-06 14:12:36 +0000 | [diff] [blame] | 3312 | #endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3313 | main_msg(_("+\t\t\tStart at end of file")); |
| 3314 | main_msg(_("+<lnum>\t\tStart at line <lnum>")); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3315 | main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file")); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3316 | main_msg(_("-c <command>\t\tExecute <command> after loading the first file")); |
| 3317 | main_msg(_("-S <session>\t\tSource file <session> after loading the first file")); |
| 3318 | main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>")); |
| 3319 | main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>")); |
| 3320 | main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>")); |
| 3321 | #ifdef FEAT_CRYPT |
| 3322 | main_msg(_("-x\t\t\tEdit encrypted files")); |
| 3323 | #endif |
| 3324 | #if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11) |
| 3325 | # if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) |
| 3326 | main_msg(_("-display <display>\tConnect vim to this particular X-server")); |
| 3327 | # endif |
| 3328 | main_msg(_("-X\t\t\tDo not connect to X server")); |
| 3329 | #endif |
| 3330 | #ifdef FEAT_CLIENTSERVER |
| 3331 | main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible")); |
| 3332 | main_msg(_("--remote-silent <files> Same, don't complain if there is no server")); |
| 3333 | main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited")); |
| 3334 | main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server")); |
Bram Moolenaar | 0ce2993 | 2006-03-13 22:18:45 +0000 | [diff] [blame] | 3335 | # ifdef FEAT_WINDOWS |
Bram Moolenaar | 82ad324 | 2008-01-11 19:26:36 +0000 | [diff] [blame] | 3336 | main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file")); |
Bram Moolenaar | 0ce2993 | 2006-03-13 22:18:45 +0000 | [diff] [blame] | 3337 | # endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3338 | main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit")); |
| 3339 | main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result")); |
| 3340 | main_msg(_("--serverlist\t\tList available Vim server names and exit")); |
| 3341 | main_msg(_("--servername <name>\tSend to/become the Vim server <name>")); |
| 3342 | #endif |
Bram Moolenaar | ef94eec | 2009-11-11 13:22:11 +0000 | [diff] [blame] | 3343 | #ifdef STARTUPTIME |
Bram Moolenaar | 34ef52d | 2009-11-17 11:31:25 +0000 | [diff] [blame] | 3344 | main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>")); |
Bram Moolenaar | ef94eec | 2009-11-11 13:22:11 +0000 | [diff] [blame] | 3345 | #endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3346 | #ifdef FEAT_VIMINFO |
| 3347 | main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo")); |
| 3348 | #endif |
| 3349 | main_msg(_("-h or --help\tPrint Help (this message) and exit")); |
| 3350 | main_msg(_("--version\t\tPrint version information and exit")); |
| 3351 | |
| 3352 | #ifdef FEAT_GUI_X11 |
| 3353 | # ifdef FEAT_GUI_MOTIF |
| 3354 | mch_msg(_("\nArguments recognised by gvim (Motif version):\n")); |
| 3355 | # else |
| 3356 | # ifdef FEAT_GUI_ATHENA |
| 3357 | # ifdef FEAT_GUI_NEXTAW |
| 3358 | mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n")); |
| 3359 | # else |
| 3360 | mch_msg(_("\nArguments recognised by gvim (Athena version):\n")); |
| 3361 | # endif |
| 3362 | # endif |
| 3363 | # endif |
| 3364 | main_msg(_("-display <display>\tRun vim on <display>")); |
| 3365 | main_msg(_("-iconic\t\tStart vim iconified")); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3366 | main_msg(_("-background <color>\tUse <color> for the background (also: -bg)")); |
| 3367 | main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)")); |
| 3368 | main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)")); |
| 3369 | main_msg(_("-boldfont <font>\tUse <font> for bold text")); |
| 3370 | main_msg(_("-italicfont <font>\tUse <font> for italic text")); |
| 3371 | main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)")); |
| 3372 | main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)")); |
| 3373 | main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)")); |
| 3374 | # ifdef FEAT_GUI_ATHENA |
| 3375 | main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)")); |
| 3376 | # endif |
| 3377 | main_msg(_("-reverse\t\tUse reverse video (also: -rv)")); |
| 3378 | main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)")); |
| 3379 | main_msg(_("-xrm <resource>\tSet the specified resource")); |
| 3380 | #endif /* FEAT_GUI_X11 */ |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3381 | #ifdef FEAT_GUI_GTK |
| 3382 | mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n")); |
| 3383 | main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)")); |
| 3384 | main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)")); |
| 3385 | main_msg(_("-reverse\t\tUse reverse video (also: -rv)")); |
| 3386 | main_msg(_("-display <display>\tRun vim on <display> (also: --display)")); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3387 | main_msg(_("--role <role>\tSet a unique role to identify the main window")); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3388 | main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget")); |
Bram Moolenaar | f99bc6d | 2012-03-28 17:10:31 +0200 | [diff] [blame] | 3389 | main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout")); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3390 | #endif |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3391 | #ifdef FEAT_GUI_W32 |
| 3392 | main_msg(_("-P <parent title>\tOpen Vim inside parent application")); |
Bram Moolenaar | 78e1762 | 2007-08-30 10:26:19 +0000 | [diff] [blame] | 3393 | main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget")); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3394 | #endif |
| 3395 | |
| 3396 | #ifdef FEAT_GUI_GNOME |
| 3397 | /* Gnome gives extra messages for --help if we continue, but not for -h. */ |
| 3398 | if (gui.starting) |
Bram Moolenaar | f4120a8 | 2011-12-08 15:57:59 +0100 | [diff] [blame] | 3399 | { |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3400 | mch_msg("\n"); |
Bram Moolenaar | f4120a8 | 2011-12-08 15:57:59 +0100 | [diff] [blame] | 3401 | gui.dofork = FALSE; |
| 3402 | } |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3403 | else |
| 3404 | #endif |
| 3405 | mch_exit(0); |
| 3406 | } |
| 3407 | |
Bram Moolenaar | d5bc83f | 2005-12-07 21:07:59 +0000 | [diff] [blame] | 3408 | #if defined(HAS_SWAP_EXISTS_ACTION) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3409 | /* |
| 3410 | * Check the result of the ATTENTION dialog: |
| 3411 | * When "Quit" selected, exit Vim. |
| 3412 | * When "Recover" selected, recover the file. |
| 3413 | */ |
| 3414 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 3415 | check_swap_exists_action(void) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3416 | { |
| 3417 | if (swap_exists_action == SEA_QUIT) |
| 3418 | getout(1); |
| 3419 | handle_swap_exists(NULL); |
| 3420 | } |
| 3421 | #endif |
| 3422 | |
Bram Moolenaar | 08cab96 | 2017-03-04 14:37:18 +0100 | [diff] [blame] | 3423 | #endif /* NO_VIM_MAIN */ |
Bram Moolenaar | b05b10a | 2011-03-22 18:10:45 +0100 | [diff] [blame] | 3424 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3425 | #if defined(STARTUPTIME) || defined(PROTO) |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 3426 | static void time_diff(struct timeval *then, struct timeval *now); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3427 | |
| 3428 | static struct timeval prev_timeval; |
| 3429 | |
Bram Moolenaar | 3f26967 | 2009-11-03 11:11:11 +0000 | [diff] [blame] | 3430 | # ifdef WIN3264 |
| 3431 | /* |
| 3432 | * Windows doesn't have gettimeofday(), although it does have struct timeval. |
| 3433 | */ |
| 3434 | static int |
| 3435 | gettimeofday(struct timeval *tv, char *dummy) |
| 3436 | { |
| 3437 | long t = clock(); |
| 3438 | tv->tv_sec = t / CLOCKS_PER_SEC; |
| 3439 | tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC; |
| 3440 | return 0; |
| 3441 | } |
| 3442 | # endif |
| 3443 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3444 | /* |
| 3445 | * Save the previous time before doing something that could nest. |
| 3446 | * set "*tv_rel" to the time elapsed so far. |
| 3447 | */ |
| 3448 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 3449 | time_push(void *tv_rel, void *tv_start) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3450 | { |
| 3451 | *((struct timeval *)tv_rel) = prev_timeval; |
| 3452 | gettimeofday(&prev_timeval, NULL); |
| 3453 | ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec |
| 3454 | - ((struct timeval *)tv_rel)->tv_usec; |
| 3455 | ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec |
| 3456 | - ((struct timeval *)tv_rel)->tv_sec; |
| 3457 | if (((struct timeval *)tv_rel)->tv_usec < 0) |
| 3458 | { |
| 3459 | ((struct timeval *)tv_rel)->tv_usec += 1000000; |
| 3460 | --((struct timeval *)tv_rel)->tv_sec; |
| 3461 | } |
| 3462 | *(struct timeval *)tv_start = prev_timeval; |
| 3463 | } |
| 3464 | |
| 3465 | /* |
| 3466 | * Compute the previous time after doing something that could nest. |
| 3467 | * Subtract "*tp" from prev_timeval; |
| 3468 | * Note: The arguments are (void *) to avoid trouble with systems that don't |
| 3469 | * have struct timeval. |
| 3470 | */ |
| 3471 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 3472 | time_pop( |
| 3473 | void *tp) /* actually (struct timeval *) */ |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3474 | { |
| 3475 | prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec; |
| 3476 | prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec; |
| 3477 | if (prev_timeval.tv_usec < 0) |
| 3478 | { |
| 3479 | prev_timeval.tv_usec += 1000000; |
| 3480 | --prev_timeval.tv_sec; |
| 3481 | } |
| 3482 | } |
| 3483 | |
| 3484 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 3485 | time_diff(struct timeval *then, struct timeval *now) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3486 | { |
| 3487 | long usec; |
| 3488 | long msec; |
| 3489 | |
| 3490 | usec = now->tv_usec - then->tv_usec; |
| 3491 | msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L, |
| 3492 | usec = usec % 1000L; |
| 3493 | fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L); |
| 3494 | } |
| 3495 | |
| 3496 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 3497 | time_msg( |
| 3498 | char *mesg, |
| 3499 | void *tv_start) /* only for do_source: start time; actually |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3500 | (struct timeval *) */ |
| 3501 | { |
| 3502 | static struct timeval start; |
| 3503 | struct timeval now; |
| 3504 | |
| 3505 | if (time_fd != NULL) |
| 3506 | { |
Bram Moolenaar | f506c5b | 2010-06-22 06:28:58 +0200 | [diff] [blame] | 3507 | if (strstr(mesg, "STARTING") != NULL) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3508 | { |
| 3509 | gettimeofday(&start, NULL); |
| 3510 | prev_timeval = start; |
| 3511 | fprintf(time_fd, "\n\ntimes in msec\n"); |
| 3512 | fprintf(time_fd, " clock self+sourced self: sourced script\n"); |
| 3513 | fprintf(time_fd, " clock elapsed: other lines\n\n"); |
| 3514 | } |
| 3515 | gettimeofday(&now, NULL); |
| 3516 | time_diff(&start, &now); |
| 3517 | if (((struct timeval *)tv_start) != NULL) |
| 3518 | { |
| 3519 | fprintf(time_fd, " "); |
| 3520 | time_diff(((struct timeval *)tv_start), &now); |
| 3521 | } |
| 3522 | fprintf(time_fd, " "); |
| 3523 | time_diff(&prev_timeval, &now); |
| 3524 | prev_timeval = now; |
Bram Moolenaar | f506c5b | 2010-06-22 06:28:58 +0200 | [diff] [blame] | 3525 | fprintf(time_fd, ": %s\n", mesg); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3526 | } |
| 3527 | } |
| 3528 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3529 | #endif |
| 3530 | |
Bram Moolenaar | 595297d | 2017-03-04 19:11:12 +0100 | [diff] [blame] | 3531 | #if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL) |
Bram Moolenaar | 08cab96 | 2017-03-04 14:37:18 +0100 | [diff] [blame] | 3532 | static void |
| 3533 | set_progpath(char_u *argv0) |
| 3534 | { |
| 3535 | char_u *val = argv0; |
Bram Moolenaar | 08cab96 | 2017-03-04 14:37:18 +0100 | [diff] [blame] | 3536 | |
| 3537 | /* A relative path containing a "/" will become invalid when using ":cd", |
| 3538 | * turn it into a full path. |
Bram Moolenaar | 066029e | 2017-03-05 15:19:32 +0100 | [diff] [blame] | 3539 | * On MS-Windows "vim" should be expanded to "vim.exe", thus always do |
| 3540 | * this. */ |
| 3541 | # ifdef WIN32 |
| 3542 | char_u *path = NULL; |
| 3543 | |
| 3544 | if (mch_can_exe(argv0, &path, FALSE) && path != NULL) |
| 3545 | val = path; |
| 3546 | # else |
| 3547 | char_u buf[MAXPATHL]; |
| 3548 | |
Bram Moolenaar | 4366319 | 2017-03-05 14:29:12 +0100 | [diff] [blame] | 3549 | if (!mch_isFullName(argv0)) |
| 3550 | { |
Bram Moolenaar | 4366319 | 2017-03-05 14:29:12 +0100 | [diff] [blame] | 3551 | if (gettail(argv0) != argv0 |
| 3552 | && vim_FullName(argv0, buf, MAXPATHL, TRUE) != FAIL) |
| 3553 | val = buf; |
Bram Moolenaar | 4366319 | 2017-03-05 14:29:12 +0100 | [diff] [blame] | 3554 | } |
Bram Moolenaar | 066029e | 2017-03-05 15:19:32 +0100 | [diff] [blame] | 3555 | # endif |
Bram Moolenaar | 08cab96 | 2017-03-04 14:37:18 +0100 | [diff] [blame] | 3556 | set_vim_var_string(VV_PROGPATH, val, -1); |
Bram Moolenaar | 066029e | 2017-03-05 15:19:32 +0100 | [diff] [blame] | 3557 | # ifdef WIN32 |
Bram Moolenaar | 4366319 | 2017-03-05 14:29:12 +0100 | [diff] [blame] | 3558 | vim_free(path); |
Bram Moolenaar | 066029e | 2017-03-05 15:19:32 +0100 | [diff] [blame] | 3559 | # endif |
Bram Moolenaar | 08cab96 | 2017-03-04 14:37:18 +0100 | [diff] [blame] | 3560 | } |
| 3561 | |
| 3562 | #endif /* NO_VIM_MAIN */ |
| 3563 | |
Bram Moolenaar | b05b10a | 2011-03-22 18:10:45 +0100 | [diff] [blame] | 3564 | #if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3565 | |
| 3566 | /* |
| 3567 | * Common code for the X command server and the Win32 command server. |
| 3568 | */ |
| 3569 | |
Bram Moolenaar | 92b8b2d | 2016-01-29 22:36:45 +0100 | [diff] [blame] | 3570 | static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3571 | |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 3572 | /* |
| 3573 | * Do the client-server stuff, unless "--servername ''" was used. |
| 3574 | */ |
| 3575 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 3576 | exec_on_server(mparm_T *parmp) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 3577 | { |
| 3578 | if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL) |
| 3579 | { |
| 3580 | # ifdef WIN32 |
| 3581 | /* Initialise the client/server messaging infrastructure. */ |
| 3582 | serverInitMessaging(); |
| 3583 | # endif |
| 3584 | |
| 3585 | /* |
| 3586 | * When a command server argument was found, execute it. This may |
| 3587 | * exit Vim when it was successful. Otherwise it's executed further |
| 3588 | * on. Remember the encoding used here in "serverStrEnc". |
| 3589 | */ |
| 3590 | if (parmp->serverArg) |
| 3591 | { |
| 3592 | cmdsrv_main(&parmp->argc, parmp->argv, |
| 3593 | parmp->serverName_arg, &parmp->serverStr); |
| 3594 | # ifdef FEAT_MBYTE |
| 3595 | parmp->serverStrEnc = vim_strsave(p_enc); |
| 3596 | # endif |
| 3597 | } |
| 3598 | |
| 3599 | /* If we're still running, get the name to register ourselves. |
| 3600 | * On Win32 can register right now, for X11 need to setup the |
| 3601 | * clipboard first, it's further down. */ |
| 3602 | parmp->servername = serverMakeName(parmp->serverName_arg, |
| 3603 | parmp->argv[0]); |
| 3604 | # ifdef WIN32 |
| 3605 | if (parmp->servername != NULL) |
| 3606 | { |
| 3607 | serverSetName(parmp->servername); |
| 3608 | vim_free(parmp->servername); |
| 3609 | } |
| 3610 | # endif |
| 3611 | } |
| 3612 | } |
| 3613 | |
| 3614 | /* |
| 3615 | * Prepare for running as a Vim server. |
| 3616 | */ |
| 3617 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 3618 | prepare_server(mparm_T *parmp) |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 3619 | { |
| 3620 | # if defined(FEAT_X11) |
| 3621 | /* |
| 3622 | * Register for remote command execution with :serversend and --remote |
| 3623 | * unless there was a -X or a --servername '' on the command line. |
| 3624 | * Only register nongui-vim's with an explicit --servername argument. |
Bram Moolenaar | 5f40231 | 2006-08-15 19:40:35 +0000 | [diff] [blame] | 3625 | * When running as root --servername is also required. |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 3626 | */ |
| 3627 | if (X_DISPLAY != NULL && parmp->servername != NULL && ( |
| 3628 | # ifdef FEAT_GUI |
Bram Moolenaar | 5f40231 | 2006-08-15 19:40:35 +0000 | [diff] [blame] | 3629 | (gui.in_use |
| 3630 | # ifdef UNIX |
Bram Moolenaar | 311d982 | 2007-02-27 15:48:28 +0000 | [diff] [blame] | 3631 | && getuid() != ROOT_UID |
Bram Moolenaar | 5f40231 | 2006-08-15 19:40:35 +0000 | [diff] [blame] | 3632 | # endif |
| 3633 | ) || |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 3634 | # endif |
| 3635 | parmp->serverName_arg != NULL)) |
| 3636 | { |
| 3637 | (void)serverRegisterName(X_DISPLAY, parmp->servername); |
| 3638 | vim_free(parmp->servername); |
| 3639 | TIME_MSG("register server name"); |
| 3640 | } |
| 3641 | else |
| 3642 | serverDelayedStartName = parmp->servername; |
| 3643 | # endif |
| 3644 | |
| 3645 | /* |
| 3646 | * Execute command ourselves if we're here because the send failed (or |
| 3647 | * else we would have exited above). |
| 3648 | */ |
| 3649 | if (parmp->serverStr != NULL) |
| 3650 | { |
| 3651 | char_u *p; |
| 3652 | |
| 3653 | server_to_input_buf(serverConvert(parmp->serverStrEnc, |
| 3654 | parmp->serverStr, &p)); |
| 3655 | vim_free(p); |
| 3656 | } |
| 3657 | } |
| 3658 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3659 | static void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 3660 | cmdsrv_main( |
| 3661 | int *argc, |
| 3662 | char **argv, |
| 3663 | char_u *serverName_arg, |
| 3664 | char_u **serverStr) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3665 | { |
| 3666 | char_u *res; |
| 3667 | int i; |
| 3668 | char_u *sname; |
| 3669 | int ret; |
| 3670 | int didone = FALSE; |
| 3671 | int exiterr = 0; |
| 3672 | char **newArgV = argv + 1; |
| 3673 | int newArgC = 1, |
| 3674 | Argc = *argc; |
| 3675 | int argtype; |
| 3676 | #define ARGTYPE_OTHER 0 |
| 3677 | #define ARGTYPE_EDIT 1 |
| 3678 | #define ARGTYPE_EDIT_WAIT 2 |
| 3679 | #define ARGTYPE_SEND 3 |
| 3680 | int silent = FALSE; |
Bram Moolenaar | eb94e55 | 2006-03-11 21:35:11 +0000 | [diff] [blame] | 3681 | int tabs = FALSE; |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3682 | # ifndef FEAT_X11 |
| 3683 | HWND srv; |
| 3684 | # else |
| 3685 | Window srv; |
| 3686 | |
| 3687 | setup_term_clip(); |
| 3688 | # endif |
| 3689 | |
| 3690 | sname = serverMakeName(serverName_arg, argv[0]); |
| 3691 | if (sname == NULL) |
| 3692 | return; |
| 3693 | |
| 3694 | /* |
| 3695 | * Execute the command server related arguments and remove them |
| 3696 | * from the argc/argv array; We may have to return into main() |
| 3697 | */ |
| 3698 | for (i = 1; i < Argc; i++) |
| 3699 | { |
| 3700 | res = NULL; |
Bram Moolenaar | c013cb6 | 2005-07-24 21:18:31 +0000 | [diff] [blame] | 3701 | if (STRCMP(argv[i], "--") == 0) /* end of option arguments */ |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3702 | { |
| 3703 | for (; i < *argc; i++) |
| 3704 | { |
| 3705 | *newArgV++ = argv[i]; |
| 3706 | newArgC++; |
| 3707 | } |
| 3708 | break; |
| 3709 | } |
| 3710 | |
Bram Moolenaar | eb94e55 | 2006-03-11 21:35:11 +0000 | [diff] [blame] | 3711 | if (STRICMP(argv[i], "--remote-send") == 0) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3712 | argtype = ARGTYPE_SEND; |
Bram Moolenaar | eb94e55 | 2006-03-11 21:35:11 +0000 | [diff] [blame] | 3713 | else if (STRNICMP(argv[i], "--remote", 8) == 0) |
| 3714 | { |
| 3715 | char *p = argv[i] + 8; |
| 3716 | |
| 3717 | argtype = ARGTYPE_EDIT; |
| 3718 | while (*p != NUL) |
| 3719 | { |
| 3720 | if (STRNICMP(p, "-wait", 5) == 0) |
| 3721 | { |
| 3722 | argtype = ARGTYPE_EDIT_WAIT; |
| 3723 | p += 5; |
| 3724 | } |
| 3725 | else if (STRNICMP(p, "-silent", 7) == 0) |
| 3726 | { |
| 3727 | silent = TRUE; |
| 3728 | p += 7; |
| 3729 | } |
| 3730 | else if (STRNICMP(p, "-tab", 4) == 0) |
| 3731 | { |
| 3732 | tabs = TRUE; |
| 3733 | p += 4; |
| 3734 | } |
| 3735 | else |
| 3736 | { |
| 3737 | argtype = ARGTYPE_OTHER; |
| 3738 | break; |
| 3739 | } |
| 3740 | } |
| 3741 | } |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3742 | else |
| 3743 | argtype = ARGTYPE_OTHER; |
Bram Moolenaar | eb94e55 | 2006-03-11 21:35:11 +0000 | [diff] [blame] | 3744 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3745 | if (argtype != ARGTYPE_OTHER) |
| 3746 | { |
| 3747 | if (i == *argc - 1) |
| 3748 | mainerr_arg_missing((char_u *)argv[i]); |
| 3749 | if (argtype == ARGTYPE_SEND) |
| 3750 | { |
| 3751 | *serverStr = (char_u *)argv[i + 1]; |
| 3752 | i++; |
| 3753 | } |
| 3754 | else |
| 3755 | { |
| 3756 | *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1, |
Bram Moolenaar | eb94e55 | 2006-03-11 21:35:11 +0000 | [diff] [blame] | 3757 | tabs, argtype == ARGTYPE_EDIT_WAIT); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3758 | if (*serverStr == NULL) |
| 3759 | { |
| 3760 | /* Probably out of memory, exit. */ |
| 3761 | didone = TRUE; |
| 3762 | exiterr = 1; |
| 3763 | break; |
| 3764 | } |
| 3765 | Argc = i; |
| 3766 | } |
| 3767 | # ifdef FEAT_X11 |
| 3768 | if (xterm_dpy == NULL) |
| 3769 | { |
| 3770 | mch_errmsg(_("No display")); |
| 3771 | ret = -1; |
| 3772 | } |
| 3773 | else |
| 3774 | ret = serverSendToVim(xterm_dpy, sname, *serverStr, |
| 3775 | NULL, &srv, 0, 0, silent); |
| 3776 | # else |
| 3777 | /* Win32 always works? */ |
| 3778 | ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent); |
| 3779 | # endif |
| 3780 | if (ret < 0) |
| 3781 | { |
| 3782 | if (argtype == ARGTYPE_SEND) |
| 3783 | { |
| 3784 | /* Failed to send, abort. */ |
| 3785 | mch_errmsg(_(": Send failed.\n")); |
| 3786 | didone = TRUE; |
| 3787 | exiterr = 1; |
| 3788 | } |
| 3789 | else if (!silent) |
| 3790 | /* Let vim start normally. */ |
| 3791 | mch_errmsg(_(": Send failed. Trying to execute locally\n")); |
| 3792 | break; |
| 3793 | } |
| 3794 | |
| 3795 | # ifdef FEAT_GUI_W32 |
| 3796 | /* Guess that when the server name starts with "g" it's a GUI |
| 3797 | * server, which we can bring to the foreground here. |
| 3798 | * Foreground() in the server doesn't work very well. */ |
| 3799 | if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G') |
| 3800 | SetForegroundWindow(srv); |
| 3801 | # endif |
| 3802 | |
| 3803 | /* |
| 3804 | * For --remote-wait: Wait until the server did edit each |
| 3805 | * file. Also detect that the server no longer runs. |
| 3806 | */ |
| 3807 | if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT) |
| 3808 | { |
| 3809 | int numFiles = *argc - i - 1; |
| 3810 | int j; |
| 3811 | char_u *done = alloc(numFiles); |
| 3812 | char_u *p; |
| 3813 | # ifdef FEAT_GUI_W32 |
| 3814 | NOTIFYICONDATA ni; |
| 3815 | int count = 0; |
| 3816 | extern HWND message_window; |
| 3817 | # endif |
| 3818 | |
| 3819 | if (numFiles > 0 && argv[i + 1][0] == '+') |
| 3820 | /* Skip "+cmd" argument, don't wait for it to be edited. */ |
| 3821 | --numFiles; |
| 3822 | |
| 3823 | # ifdef FEAT_GUI_W32 |
| 3824 | ni.cbSize = sizeof(ni); |
| 3825 | ni.hWnd = message_window; |
| 3826 | ni.uID = 0; |
| 3827 | ni.uFlags = NIF_ICON|NIF_TIP; |
| 3828 | ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM"); |
| 3829 | sprintf(ni.szTip, _("%d of %d edited"), count, numFiles); |
| 3830 | Shell_NotifyIcon(NIM_ADD, &ni); |
| 3831 | # endif |
| 3832 | |
| 3833 | /* Wait for all files to unload in remote */ |
Bram Moolenaar | 7db5fc8 | 2010-05-24 11:59:29 +0200 | [diff] [blame] | 3834 | vim_memset(done, 0, numFiles); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3835 | while (memchr(done, 0, numFiles) != NULL) |
| 3836 | { |
| 3837 | # ifdef WIN32 |
| 3838 | p = serverGetReply(srv, NULL, TRUE, TRUE); |
| 3839 | if (p == NULL) |
| 3840 | break; |
| 3841 | # else |
| 3842 | if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0) |
| 3843 | break; |
| 3844 | # endif |
| 3845 | j = atoi((char *)p); |
| 3846 | if (j >= 0 && j < numFiles) |
| 3847 | { |
| 3848 | # ifdef FEAT_GUI_W32 |
| 3849 | ++count; |
| 3850 | sprintf(ni.szTip, _("%d of %d edited"), |
| 3851 | count, numFiles); |
| 3852 | Shell_NotifyIcon(NIM_MODIFY, &ni); |
| 3853 | # endif |
| 3854 | done[j] = 1; |
| 3855 | } |
| 3856 | } |
| 3857 | # ifdef FEAT_GUI_W32 |
| 3858 | Shell_NotifyIcon(NIM_DELETE, &ni); |
| 3859 | # endif |
| 3860 | } |
| 3861 | } |
| 3862 | else if (STRICMP(argv[i], "--remote-expr") == 0) |
| 3863 | { |
| 3864 | if (i == *argc - 1) |
| 3865 | mainerr_arg_missing((char_u *)argv[i]); |
| 3866 | # ifdef WIN32 |
| 3867 | /* Win32 always works? */ |
| 3868 | if (serverSendToVim(sname, (char_u *)argv[i + 1], |
| 3869 | &res, NULL, 1, FALSE) < 0) |
| 3870 | # else |
| 3871 | if (xterm_dpy == NULL) |
| 3872 | mch_errmsg(_("No display: Send expression failed.\n")); |
| 3873 | else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1], |
| 3874 | &res, NULL, 1, 1, FALSE) < 0) |
| 3875 | # endif |
| 3876 | { |
| 3877 | if (res != NULL && *res != NUL) |
| 3878 | { |
| 3879 | /* Output error from remote */ |
| 3880 | mch_errmsg((char *)res); |
| 3881 | vim_free(res); |
| 3882 | res = NULL; |
| 3883 | } |
| 3884 | mch_errmsg(_(": Send expression failed.\n")); |
| 3885 | } |
| 3886 | } |
| 3887 | else if (STRICMP(argv[i], "--serverlist") == 0) |
| 3888 | { |
| 3889 | # ifdef WIN32 |
| 3890 | /* Win32 always works? */ |
| 3891 | res = serverGetVimNames(); |
| 3892 | # else |
| 3893 | if (xterm_dpy != NULL) |
| 3894 | res = serverGetVimNames(xterm_dpy); |
| 3895 | # endif |
| 3896 | if (called_emsg) |
| 3897 | mch_errmsg("\n"); |
| 3898 | } |
| 3899 | else if (STRICMP(argv[i], "--servername") == 0) |
| 3900 | { |
Bram Moolenaar | 5d985b9 | 2009-12-16 17:28:07 +0000 | [diff] [blame] | 3901 | /* Already processed. Take it out of the command line */ |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3902 | i++; |
| 3903 | continue; |
| 3904 | } |
| 3905 | else |
| 3906 | { |
| 3907 | *newArgV++ = argv[i]; |
| 3908 | newArgC++; |
| 3909 | continue; |
| 3910 | } |
| 3911 | didone = TRUE; |
| 3912 | if (res != NULL && *res != NUL) |
| 3913 | { |
| 3914 | mch_msg((char *)res); |
| 3915 | if (res[STRLEN(res) - 1] != '\n') |
| 3916 | mch_msg("\n"); |
| 3917 | } |
| 3918 | vim_free(res); |
| 3919 | } |
| 3920 | |
| 3921 | if (didone) |
| 3922 | { |
| 3923 | display_errors(); /* display any collected messages */ |
| 3924 | exit(exiterr); /* Mission accomplished - get out */ |
| 3925 | } |
| 3926 | |
| 3927 | /* Return back into main() */ |
| 3928 | *argc = newArgC; |
| 3929 | vim_free(sname); |
| 3930 | } |
| 3931 | |
| 3932 | /* |
| 3933 | * Build a ":drop" command to send to a Vim server. |
| 3934 | */ |
| 3935 | static char_u * |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 3936 | build_drop_cmd( |
| 3937 | int filec, |
| 3938 | char **filev, |
| 3939 | int tabs, /* Use ":tab drop" instead of ":drop". */ |
| 3940 | int sendReply) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3941 | { |
| 3942 | garray_T ga; |
| 3943 | int i; |
| 3944 | char_u *inicmd = NULL; |
| 3945 | char_u *p; |
Bram Moolenaar | f11ce66 | 2015-03-24 16:48:58 +0100 | [diff] [blame] | 3946 | char_u *cdp; |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 3947 | char_u *cwd; |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3948 | |
| 3949 | if (filec > 0 && filev[0][0] == '+') |
| 3950 | { |
| 3951 | inicmd = (char_u *)filev[0] + 1; |
| 3952 | filev++; |
| 3953 | filec--; |
| 3954 | } |
| 3955 | /* Check if we have at least one argument. */ |
| 3956 | if (filec <= 0) |
| 3957 | mainerr_arg_missing((char_u *)filev[-1]); |
Bram Moolenaar | 00b78c1 | 2010-11-16 16:25:51 +0100 | [diff] [blame] | 3958 | |
| 3959 | /* Temporarily cd to the current directory to handle relative file names. */ |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 3960 | cwd = alloc(MAXPATHL); |
| 3961 | if (cwd == NULL) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3962 | return NULL; |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 3963 | if (mch_dirname(cwd, MAXPATHL) != OK) |
| 3964 | { |
| 3965 | vim_free(cwd); |
| 3966 | return NULL; |
| 3967 | } |
Bram Moolenaar | f11ce66 | 2015-03-24 16:48:58 +0100 | [diff] [blame] | 3968 | cdp = vim_strsave_escaped_ext(cwd, |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 3969 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 3970 | (char_u *)"", /* rem_backslash() will tell what chars to escape */ |
Bram Moolenaar | 02b0631 | 2007-09-06 15:39:22 +0000 | [diff] [blame] | 3971 | #else |
| 3972 | PATH_ESC_CHARS, |
| 3973 | #endif |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 3974 | '\\', TRUE); |
| 3975 | vim_free(cwd); |
Bram Moolenaar | f11ce66 | 2015-03-24 16:48:58 +0100 | [diff] [blame] | 3976 | if (cdp == NULL) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3977 | return NULL; |
| 3978 | ga_init2(&ga, 1, 100); |
| 3979 | ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd "); |
Bram Moolenaar | f11ce66 | 2015-03-24 16:48:58 +0100 | [diff] [blame] | 3980 | ga_concat(&ga, cdp); |
Bram Moolenaar | eb94e55 | 2006-03-11 21:35:11 +0000 | [diff] [blame] | 3981 | |
| 3982 | /* Call inputsave() so that a prompt for an encryption key works. */ |
| 3983 | ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|"); |
| 3984 | if (tabs) |
| 3985 | ga_concat(&ga, (char_u *)"tab "); |
| 3986 | ga_concat(&ga, (char_u *)"drop"); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3987 | for (i = 0; i < filec; i++) |
| 3988 | { |
| 3989 | /* On Unix the shell has already expanded the wildcards, don't want to |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 3990 | * do it again in the Vim server. On MS-Windows only escape |
| 3991 | * non-wildcard characters. */ |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3992 | p = vim_strsave_escaped((char_u *)filev[i], |
| 3993 | #ifdef UNIX |
| 3994 | PATH_ESC_CHARS |
| 3995 | #else |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 3996 | (char_u *)" \t%#" |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 3997 | #endif |
| 3998 | ); |
| 3999 | if (p == NULL) |
| 4000 | { |
| 4001 | vim_free(ga.ga_data); |
| 4002 | return NULL; |
| 4003 | } |
| 4004 | ga_concat(&ga, (char_u *)" "); |
| 4005 | ga_concat(&ga, p); |
| 4006 | vim_free(p); |
| 4007 | } |
Bram Moolenaar | 00b78c1 | 2010-11-16 16:25:51 +0100 | [diff] [blame] | 4008 | ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>"); |
| 4009 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 4010 | /* The :drop commands goes to Insert mode when 'insertmode' is set, use |
| 4011 | * CTRL-\ CTRL-N again. */ |
Bram Moolenaar | 00b78c1 | 2010-11-16 16:25:51 +0100 | [diff] [blame] | 4012 | ga_concat(&ga, (char_u *)"<C-\\><C-N>"); |
| 4013 | |
| 4014 | /* Switch back to the correct current directory (prior to temporary path |
| 4015 | * switch) unless 'autochdir' is set, in which case it will already be |
Bram Moolenaar | f11ce66 | 2015-03-24 16:48:58 +0100 | [diff] [blame] | 4016 | * correct after the :drop command. With line breaks and spaces: |
| 4017 | * if !exists('+acd') || !&acd |
| 4018 | * if haslocaldir() |
| 4019 | * cd - |
| 4020 | * lcd - |
Bram Moolenaar | fafeee6 | 2015-07-03 13:33:01 +0200 | [diff] [blame] | 4021 | * elseif getcwd() ==# 'current path' |
Bram Moolenaar | f11ce66 | 2015-03-24 16:48:58 +0100 | [diff] [blame] | 4022 | * cd - |
| 4023 | * endif |
| 4024 | * endif |
| 4025 | */ |
| 4026 | ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|"); |
Bram Moolenaar | fafeee6 | 2015-07-03 13:33:01 +0200 | [diff] [blame] | 4027 | ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '"); |
Bram Moolenaar | f11ce66 | 2015-03-24 16:48:58 +0100 | [diff] [blame] | 4028 | ga_concat(&ga, cdp); |
Bram Moolenaar | fafeee6 | 2015-07-03 13:33:01 +0200 | [diff] [blame] | 4029 | ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>"); |
Bram Moolenaar | f11ce66 | 2015-03-24 16:48:58 +0100 | [diff] [blame] | 4030 | vim_free(cdp); |
Bram Moolenaar | 00b78c1 | 2010-11-16 16:25:51 +0100 | [diff] [blame] | 4031 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 4032 | if (sendReply) |
Bram Moolenaar | 00b78c1 | 2010-11-16 16:25:51 +0100 | [diff] [blame] | 4033 | ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>"); |
| 4034 | ga_concat(&ga, (char_u *)":"); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 4035 | if (inicmd != NULL) |
| 4036 | { |
| 4037 | /* Can't use <CR> after "inicmd", because an "startinsert" would cause |
| 4038 | * the following commands to be inserted as text. Use a "|", |
| 4039 | * hopefully "inicmd" does allow this... */ |
| 4040 | ga_concat(&ga, inicmd); |
| 4041 | ga_concat(&ga, (char_u *)"|"); |
| 4042 | } |
| 4043 | /* Bring the window to the foreground, goto Insert mode when 'im' set and |
| 4044 | * clear command line. */ |
Bram Moolenaar | 567e4de | 2004-12-31 21:01:02 +0000 | [diff] [blame] | 4045 | ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>"); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 4046 | ga_append(&ga, NUL); |
| 4047 | return ga.ga_data; |
| 4048 | } |
| 4049 | |
| 4050 | /* |
Bram Moolenaar | b05b10a | 2011-03-22 18:10:45 +0100 | [diff] [blame] | 4051 | * Make our basic server name: use the specified "arg" if given, otherwise use |
| 4052 | * the tail of the command "cmd" we were started with. |
| 4053 | * Return the name in allocated memory. This doesn't include a serial number. |
| 4054 | */ |
| 4055 | static char_u * |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 4056 | serverMakeName(char_u *arg, char *cmd) |
Bram Moolenaar | b05b10a | 2011-03-22 18:10:45 +0100 | [diff] [blame] | 4057 | { |
| 4058 | char_u *p; |
| 4059 | |
| 4060 | if (arg != NULL && *arg != NUL) |
| 4061 | p = vim_strsave_up(arg); |
| 4062 | else |
| 4063 | { |
| 4064 | p = vim_strsave_up(gettail((char_u *)cmd)); |
| 4065 | /* Remove .exe or .bat from the name. */ |
| 4066 | if (p != NULL && vim_strchr(p, '.') != NULL) |
| 4067 | *vim_strchr(p, '.') = NUL; |
| 4068 | } |
| 4069 | return p; |
| 4070 | } |
| 4071 | #endif /* FEAT_CLIENTSERVER */ |
| 4072 | |
| 4073 | #if defined(FEAT_CLIENTSERVER) || defined(PROTO) |
| 4074 | /* |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 4075 | * Replace termcodes such as <CR> and insert as key presses if there is room. |
| 4076 | */ |
| 4077 | void |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 4078 | server_to_input_buf(char_u *str) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 4079 | { |
| 4080 | char_u *ptr = NULL; |
| 4081 | char_u *cpo_save = p_cpo; |
| 4082 | |
| 4083 | /* Set 'cpoptions' the way we want it. |
| 4084 | * B set - backslashes are *not* treated specially |
| 4085 | * k set - keycodes are *not* reverse-engineered |
| 4086 | * < unset - <Key> sequences *are* interpreted |
Bram Moolenaar | 8b2d9c4 | 2006-05-03 21:28:47 +0000 | [diff] [blame] | 4087 | * The last but one parameter of replace_termcodes() is TRUE so that the |
| 4088 | * <lt> sequence is recognised - needed for a real backslash. |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 4089 | */ |
| 4090 | p_cpo = (char_u *)"Bk"; |
Bram Moolenaar | 8b2d9c4 | 2006-05-03 21:28:47 +0000 | [diff] [blame] | 4091 | str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 4092 | p_cpo = cpo_save; |
| 4093 | |
| 4094 | if (*ptr != NUL) /* trailing CTRL-V results in nothing */ |
| 4095 | { |
| 4096 | /* |
| 4097 | * Add the string to the input stream. |
| 4098 | * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes. |
| 4099 | * |
| 4100 | * First clear typed characters from the typeahead buffer, there could |
| 4101 | * be half a mapping there. Then append to the existing string, so |
| 4102 | * that multiple commands from a client are concatenated. |
| 4103 | */ |
| 4104 | if (typebuf.tb_maplen < typebuf.tb_len) |
| 4105 | del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen); |
| 4106 | (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE); |
| 4107 | |
| 4108 | /* Let input_available() know we inserted text in the typeahead |
| 4109 | * buffer. */ |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 4110 | typebuf_was_filled = TRUE; |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 4111 | } |
| 4112 | vim_free((char_u *)ptr); |
| 4113 | } |
| 4114 | |
| 4115 | /* |
| 4116 | * Evaluate an expression that the client sent to a string. |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 4117 | */ |
| 4118 | char_u * |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 4119 | eval_client_expr_to_string(char_u *expr) |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 4120 | { |
| 4121 | char_u *res; |
| 4122 | int save_dbl = debug_break_level; |
| 4123 | int save_ro = redir_off; |
| 4124 | |
Bram Moolenaar | 1e284f5 | 2013-03-13 20:23:22 +0100 | [diff] [blame] | 4125 | /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be |
| 4126 | * typed. */ |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 4127 | debug_break_level = -1; |
| 4128 | redir_off = 0; |
Bram Moolenaar | 1e284f5 | 2013-03-13 20:23:22 +0100 | [diff] [blame] | 4129 | /* Do not display error message, otherwise Vim hangs, waiting for "cont" |
| 4130 | * to be typed. Do generate errors so that try/catch works. */ |
| 4131 | ++emsg_silent; |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 4132 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4133 | res = eval_to_string(expr, NULL, TRUE); |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 4134 | |
| 4135 | debug_break_level = save_dbl; |
| 4136 | redir_off = save_ro; |
Bram Moolenaar | 1e284f5 | 2013-03-13 20:23:22 +0100 | [diff] [blame] | 4137 | --emsg_silent; |
| 4138 | if (emsg_silent < 0) |
| 4139 | emsg_silent = 0; |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 4140 | |
Bram Moolenaar | 63a121b | 2005-12-11 21:36:39 +0000 | [diff] [blame] | 4141 | /* A client can tell us to redraw, but not to display the cursor, so do |
| 4142 | * that here. */ |
| 4143 | setcursor(); |
| 4144 | out_flush(); |
| 4145 | #ifdef FEAT_GUI |
| 4146 | if (gui.in_use) |
| 4147 | gui_update_cursor(FALSE, FALSE); |
| 4148 | #endif |
| 4149 | |
Bram Moolenaar | b4210b3 | 2004-06-13 14:51:16 +0000 | [diff] [blame] | 4150 | return res; |
| 4151 | } |
| 4152 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4153 | /* |
| 4154 | * If conversion is needed, convert "data" from "client_enc" to 'encoding' and |
| 4155 | * return an allocated string. Otherwise return "data". |
| 4156 | * "*tofree" is set to the result when it needs to be freed later. |
| 4157 | */ |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4158 | char_u * |
Bram Moolenaar | 52ea13d | 2016-01-30 18:51:09 +0100 | [diff] [blame] | 4159 | serverConvert( |
| 4160 | char_u *client_enc UNUSED, |
| 4161 | char_u *data, |
| 4162 | char_u **tofree) |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4163 | { |
| 4164 | char_u *res = data; |
| 4165 | |
| 4166 | *tofree = NULL; |
| 4167 | # ifdef FEAT_MBYTE |
| 4168 | if (client_enc != NULL && p_enc != NULL) |
| 4169 | { |
| 4170 | vimconv_T vimconv; |
| 4171 | |
| 4172 | vimconv.vc_type = CONV_NONE; |
| 4173 | if (convert_setup(&vimconv, client_enc, p_enc) != FAIL |
| 4174 | && vimconv.vc_type != CONV_NONE) |
| 4175 | { |
| 4176 | res = string_convert(&vimconv, data, NULL); |
| 4177 | if (res == NULL) |
| 4178 | res = data; |
| 4179 | else |
| 4180 | *tofree = res; |
| 4181 | } |
| 4182 | convert_setup(&vimconv, NULL, NULL); |
| 4183 | } |
| 4184 | # endif |
| 4185 | return res; |
| 4186 | } |
Bram Moolenaar | b05b10a | 2011-03-22 18:10:45 +0100 | [diff] [blame] | 4187 | #endif |