blob: 6e21f73148ce3e7e5c1c21ba4c7f97d9db728a5d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002 *
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 Moolenaarb4210b32004-06-13 14:51:16 +000010#define EXTERN
11#include "vim.h"
12
Bram Moolenaarb4210b32004-06-13 14:51:16 +000013#ifdef __CYGWIN__
14# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000015# include <cygwin/version.h>
16# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
17 * cygwin_conv_path() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000018# endif
19# include <limits.h>
20#endif
21
Bram Moolenaar97ff9b92016-06-26 20:37:46 +020022#if defined(WIN3264) && !defined(FEAT_GUI_W32)
23# include "iscygpty.h"
24#endif
25
Bram Moolenaarc013cb62005-07-24 21:18:31 +000026/* 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 Moolenaarb05b10a2011-03-22 18:10:45 +010033#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static int file_owned(char *fname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +000035#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void mainerr(int, char_u *);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020037# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
38static void init_locale(void);
39# endif
40static void early_arg_scan(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010041#ifndef NO_VIM_MAIN
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void main_msg(char *s);
43static void usage(void);
44static int get_number_arg(char_u *p, int *idx, int def);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void parse_command_name(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010046static void command_line_scan(mparm_T *parmp);
47static void check_tty(mparm_T *parmp);
48static void read_stdin(void);
49static void create_windows(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010050# ifdef FEAT_WINDOWS
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010051static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010052# endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010053static void exe_pre_commands(mparm_T *parmp);
54static void exe_commands(mparm_T *parmp);
55static void source_startup_scripts(mparm_T *parmp);
56static void main_start_gui(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010057# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010058static void check_swap_exists_action(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010059# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +010060# ifdef FEAT_EVAL
61static void set_progpath(char_u *argv0);
62# endif
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010063# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010064static void exec_on_server(mparm_T *parmp);
65static void prepare_server(mparm_T *parmp);
66static void cmdsrv_main(int *argc, char **argv, char_u *serverName_arg, char_u **serverStr);
67static char_u *serverMakeName(char_u *arg, char *cmd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010068# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000069#endif
70
71
Bram Moolenaarb4210b32004-06-13 14:51:16 +000072/*
73 * Different types of error messages.
74 */
75static char *(main_errors[]) =
76{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000077 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000078#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 Moolenaarc013cb62005-07-24 21:18:31 +000083 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000084#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 Moolenaarb05b10a2011-03-22 18:10:45 +010091#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaara6044292017-04-02 18:19:53 +020092
93/* Various parameters passed between main() and other functions. */
94static mparm_T params;
95
Bram Moolenaar8866d272012-11-28 15:55:42 +010096#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020097
98static char_u *start_dir = NULL; /* current working dir on startup */
99
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +0200100static int has_dash_c_arg = FALSE;
101
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000102 int
103# ifdef VIMDLL
104_export
105# endif
106# ifdef FEAT_GUI_MSWIN
107# ifdef __BORLANDC__
108_cdecl
109# endif
110VimMain
111# else
112main
113# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100114(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000115{
Bram Moolenaar3f269672009-11-03 11:11:11 +0000116#ifdef STARTUPTIME
117 int i;
118#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000119
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000120 /*
121 * Do any system-specific initialisations. These can NOT use IObuff or
122 * NameBuff. Thus emsg2() cannot be called!
123 */
124 mch_early_init();
125
Bram Moolenaar14993322014-09-09 12:25:33 +0200126#if defined(WIN32) && defined(FEAT_MBYTE)
127 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200128 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200129 * convert when 'encoding' changes. Get the unexpanded arguments.
130 */
131 argc = get_cmd_argsW(&argv);
132#endif
133
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000134 /* Many variables are in "params" so that we can pass them to invoked
135 * functions without a lot of arguments. "argc" and "argv" are also
136 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000137 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000138 params.argc = argc;
139 params.argv = argv;
140 params.want_full_screen = TRUE;
141#ifdef FEAT_EVAL
142 params.use_debug_break_level = -1;
143#endif
144#ifdef FEAT_WINDOWS
145 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000146#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000147
Bram Moolenaar99685e62013-05-11 13:56:18 +0200148#ifdef FEAT_RUBY
149 {
150 int ruby_stack_start;
151 vim_ruby_init((void *)&ruby_stack_start);
152 }
153#endif
154
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000155#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000156 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000157#endif
158
159#ifdef MEM_PROFILE
160 atexit(vim_mem_profile_dump);
161#endif
162
163#ifdef STARTUPTIME
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200164 /* Need to find "--startuptime" before actually parsing arguments. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000165 for (i = 1; i < argc; ++i)
166 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000167 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000168 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000169 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000170 TIME_MSG("--- VIM STARTING ---");
171 break;
172 }
173 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000174#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000175 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000176
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200177 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000178
179#ifdef FEAT_CLIENTSERVER
180 /*
181 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000182 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000183 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000184 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000185#endif
186
187 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000188 * Figure out the way to work from the command name argv[0].
189 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000190 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000191 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000192
193 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000194 * Process the command line arguments. File names are put in the global
195 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000196 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000197 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000198 TIME_MSG("parsing arguments");
199
200 /*
201 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000202 * there is no terminal version, and on Windows we can't fork one off with
203 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000204 */
205#ifdef ALWAYS_USE_GUI
206 gui.starting = TRUE;
207#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000208# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000209 /*
210 * Check if the GUI can be started. Reset gui.starting if not.
211 * Don't know about other systems, stay on the safe side and don't check.
212 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000213 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000214 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000215 if (gui_init_check() == FAIL)
216 {
217 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000218
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000219 /* When running "evim" or "gvim -y" we need the menus, exit if we
220 * don't have them. */
221 if (params.evim_mode)
222 mch_exit(1);
223 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000224 }
225# endif
226#endif
227
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000228 if (GARGCOUNT > 0)
229 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100230#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000231 /*
232 * Expand wildcards in file names.
233 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000234 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000235 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200236 start_dir = alloc(MAXPATHL);
237 if (start_dir != NULL)
238 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000239 /* Temporarily add '(' and ')' to 'isfname'. These are valid
240 * filename characters but are excluded from 'isfname' to make
241 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
242 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000243 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000244 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200245 if (start_dir != NULL)
246 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000247 }
248#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200249 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000250 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000251
252#if defined(WIN32) && defined(FEAT_MBYTE)
253 {
254 extern void set_alist_count(void);
255
256 /* Remember the number of entries in the argument list. If it changes
257 * we don't react on setting 'encoding'. */
258 set_alist_count();
259 }
260#endif
261
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000262#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000263 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000264 {
265 /*
266 * If there is one filename, fully qualified, we have very probably
267 * been invoked from explorer, so change to the file's directory.
268 * Hint: to avoid this when typing a command use a forward slash.
269 * If the cd fails, it doesn't matter.
270 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200271 (void)vim_chdirfile(params.fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200272 if (start_dir != NULL)
273 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000274 }
275#endif
276 TIME_MSG("expanding arguments");
277
278#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000279 if (params.diff_mode && params.window_count == -1)
280 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000281#endif
282
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000283 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000284 ++RedrawingDisabled;
285
286 /*
287 * When listing swap file names, don't do cursor positioning et. al.
288 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200289 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000290 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000291
292 /*
293 * When certain to start the GUI, don't check capabilities of terminal.
294 * For GTK we can't be sure, but when started from the desktop it doesn't
295 * make sense to try using a terminal.
296 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000297#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000298 if (gui.starting
299# ifdef FEAT_GUI_GTK
300 && !isatty(2)
301# endif
302 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000303 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000304#endif
305
306#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
307 /* When the GUI is started from Finder, need to display messages in a
308 * message box. isatty(2) returns TRUE anyway, thus we need to check the
309 * name to know we're not started from a terminal. */
310 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000311 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000312 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000313
314 /* Avoid always using "/" as the current directory. Note that when
315 * started from Finder the arglist will be filled later in
316 * HandleODocAE() and "fname" will be NULL. */
317 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
318 && STRCMP(NameBuff, "/") == 0)
319 {
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200320 if (params.fname != NULL)
321 (void)vim_chdirfile(params.fname);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000322 else
323 {
324 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
325 vim_chdir(NameBuff);
326 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200327 if (start_dir != NULL)
328 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000329 }
330 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000331#endif
332
333 /*
334 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100335 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000336 * Note that we may use mch_exit() before mch_init()!
337 */
338 mch_init();
339 TIME_MSG("shell init");
340
341#ifdef USE_XSMP
342 /*
343 * For want of anywhere else to do it, try to connect to xsmp here.
344 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
345 * Hijacking -X 'no X connection' to also disable XSMP connection as that
346 * has a similar delay upon failure.
347 * Only try if SESSION_MANAGER is set to something non-null.
348 */
349 if (!x_no_connect)
350 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000351 char *p = getenv("SESSION_MANAGER");
352
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000353 if (p != NULL && *p != NUL)
354 {
355 xsmp_init();
356 TIME_MSG("xsmp init");
357 }
358 }
359#endif
360
361 /*
362 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000363 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000364 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000365
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000366 /* This message comes before term inits, but after setting "silent_mode"
367 * when the input is not a tty. */
368 if (GARGCOUNT > 1 && !silent_mode)
369 printf(_("%d files to edit\n"), GARGCOUNT);
370
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000371 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000372 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000373 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000374 capabilities (will set full_screen) */
375 screen_start(); /* don't know where cursor is now */
376 TIME_MSG("Termcap init");
377 }
378
379 /*
380 * Set the default values for the options that use Rows and Columns.
381 */
382 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000383 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000384#ifdef FEAT_DIFF
385 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
386 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000387 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000388 diff_win_options(firstwin, FALSE);
389#endif
390
391 cmdline_row = Rows - p_ch;
392 msg_row = cmdline_row;
393 screenalloc(FALSE); /* allocate screen buffers */
394 set_init_2();
395 TIME_MSG("inits 2");
396
397 msg_scroll = TRUE;
398 no_wait_return = TRUE;
399
400 init_mappings(); /* set up initial mappings */
401
402 init_highlight(TRUE, FALSE); /* set the default highlight groups */
403 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000404
405#ifdef FEAT_EVAL
406 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000407 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000408#endif
409
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100410#ifdef FEAT_MZSCHEME
411 /*
412 * Newer version of MzScheme (Racket) require earlier (trampolined)
413 * initialisation via scheme_main_setup.
414 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200415 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100416 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200417 return mzscheme_main();
418#else
419 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100420#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200421}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100422#endif /* NO_VIM_MAIN */
Bram Moolenaara357e442016-08-10 20:45:07 +0200423#endif /* PROTO */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100424
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200425/*
426 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
427 * things simple.
428 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
429 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100430 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200431vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100432{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100433#ifndef NO_VIM_MAIN
Bram Moolenaar66459b72016-08-06 19:01:55 +0200434 /* Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
435 * Allows for setting 'loadplugins' there. */
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200436 if (params.use_vimrc != NULL
437 && (STRCMP(params.use_vimrc, "NONE") == 0
438 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
Bram Moolenaar66459b72016-08-06 19:01:55 +0200439 p_lpl = FALSE;
440
Bram Moolenaar58d98232005-07-23 22:25:46 +0000441 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000442 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000443
Bram Moolenaar58d98232005-07-23 22:25:46 +0000444 /* Source startup scripts. */
445 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000446
447#ifdef FEAT_EVAL
448 /*
449 * Read all the plugin files.
450 * Only when compiled with +eval, since most plugins need it.
451 */
452 if (p_lpl)
453 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200454 char_u *rtp_copy = NULL;
455
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200456 /* First add all package directories to 'runtimepath', so that their
457 * autoload directories can be found. Only if not done already with a
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200458 * :packloadall command.
459 * Make a copy of 'runtimepath', so that source_runtime does not use
460 * the pack directories. */
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200461 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200462 {
463 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200464 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200465 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200466
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200467 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy,
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000468# ifdef VMS /* Somehow VMS doesn't handle the "**". */
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200469 (char_u *)"plugin/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000470# else
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200471 (char_u *)"plugin/**/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000472# endif
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200473 DIP_ALL | DIP_NOAFTER);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000474 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200475 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100476
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200477 /* Only source "start" packages if not done already with a :packloadall
478 * command. */
479 if (!did_source_packages)
480 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100481 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200482
483# ifdef VMS /* Somehow VMS doesn't handle the "**". */
484 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_AFTER);
485# else
486 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_AFTER);
487# endif
488 TIME_MSG("loading after plugins");
489
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000490 }
491#endif
492
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000493#ifdef FEAT_DIFF
494 /* Decide about window layout for diff mode after reading vimrc. */
495 if (params.diff_mode && params.window_layout == 0)
496 {
497 if (diffopt_horizontal())
498 params.window_layout = WIN_HOR; /* use horizontal split */
499 else
500 params.window_layout = WIN_VER; /* use vertical split */
501 }
502#endif
503
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000504 /*
505 * Recovery mode without a file name: List swap files.
506 * This uses the 'dir' option, therefore it must be after the
507 * initializations.
508 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200509 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000510 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200511 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000512 mch_exit(0);
513 }
514
515 /*
516 * Set a few option defaults after reading .vimrc files:
517 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
518 */
519 set_init_3();
520 TIME_MSG("inits 3");
521
522 /*
523 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
524 * Note that this overrides anything from a vimrc file.
525 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000526 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000527 p_uc = 0;
528
529#ifdef FEAT_FKMAP
530 if (curwin->w_p_rl && p_altkeymap)
531 {
532 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
533# ifdef FEAT_ARABIC
534 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
535# endif
536 p_fkmap = TRUE; /* Set the Farsi keymap mode */
537 }
538#endif
539
540#ifdef FEAT_GUI
541 if (gui.starting)
542 {
543#if defined(UNIX) || defined(VMS)
544 /* When something caused a message from a vimrc script, need to output
545 * an extra newline before the shell prompt. */
546 if (did_emsg || msg_didout)
547 putchar('\n');
548#endif
549
550 gui_start(); /* will set full_screen to TRUE */
551 TIME_MSG("starting GUI");
552
553 /* When running "evim" or "gvim -y" we need the menus, exit if we
554 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000555 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000556 mch_exit(1);
557 }
558#endif
559
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000560#ifdef FEAT_VIMINFO
561 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000562 * Read in registers, history etc, but not marks, from the viminfo file.
563 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000564 */
565 if (*p_viminfo != NUL)
566 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000567 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000568 TIME_MSG("reading viminfo");
569 }
570#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100571#ifdef FEAT_EVAL
572 /* It's better to make v:oldfiles an empty list than NULL. */
573 if (get_vim_var_list(VV_OLDFILES) == NULL)
574 set_vim_var_list(VV_OLDFILES, list_alloc());
575#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000576
577#ifdef FEAT_QUICKFIX
578 /*
579 * "-q errorfile": Load the error file now.
580 * If the error file can't be read, exit before doing anything else.
581 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000582 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000583 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100584 char_u *enc = NULL;
585
586# ifdef FEAT_MBYTE
587 enc = p_menc;
588# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000589 if (params.use_ef != NULL)
590 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000591 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200592 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100593 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000594 {
595 out_char('\n');
596 mch_exit(3);
597 }
598 TIME_MSG("reading errorfile");
599 }
600#endif
601
602 /*
603 * Start putting things on the screen.
604 * Scroll screen down before drawing over it
605 * Clear screen now, so file message will not be cleared.
606 */
607 starting = NO_BUFFERS;
608 no_wait_return = FALSE;
609 if (!exmode_active)
610 msg_scroll = FALSE;
611
612#ifdef FEAT_GUI
613 /*
614 * This seems to be required to make callbacks to be called now, instead
615 * of after things have been put on the screen, which then may be deleted
616 * when getting a resize callback.
617 * For the Mac this handles putting files dropped on the Vim icon to
618 * global_alist.
619 */
620 if (gui.in_use)
621 {
622# ifdef FEAT_SUN_WORKSHOP
623 if (!usingSunWorkShop)
624# endif
625 gui_wait_for_chars(50L);
626 TIME_MSG("GUI delay");
627 }
628#endif
629
630#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
631 qnx_clip_init();
632#endif
633
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200634#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
635 clip_init(TRUE);
636#endif
637
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000638#ifdef FEAT_XCLIPBOARD
639 /* Start using the X clipboard, unless the GUI was started. */
640# ifdef FEAT_GUI
641 if (!gui.in_use)
642# endif
643 {
644 setup_term_clip();
645 TIME_MSG("setup clipboard");
646 }
647#endif
648
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000649#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000650 /* Prepare for being a Vim server. */
651 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000652#endif
653
654 /*
655 * If "-" argument given: Read file from stdin.
656 * Do this before starting Raw mode, because it may change things that the
657 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
658 * are the same terminal: "cat | vim -".
659 * Using autocommands here may cause trouble...
660 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000661 if (params.edit_type == EDIT_STDIN && !recoverymode)
662 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000663
664#if defined(UNIX) || defined(VMS)
665 /* When switching screens and something caused a message from a vimrc
666 * script, need to output an extra newline on exit. */
667 if ((did_emsg || msg_didout) && *T_TI != NUL)
668 newline_on_exit = TRUE;
669#endif
670
671 /*
672 * When done something that is not allowed or error message call
673 * wait_return. This must be done before starttermcap(), because it may
674 * switch to another screen. It must be done after settmode(TMODE_RAW),
675 * because we want to react on a single key stroke.
676 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000677 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000678 */
679 settmode(TMODE_RAW);
680 TIME_MSG("setting raw mode");
681
682 if (need_wait_return || msg_didany)
683 {
684 wait_return(TRUE);
685 TIME_MSG("waiting for return");
686 }
687
688 starttermcap(); /* start termcap if not done by wait_return() */
689 TIME_MSG("start termcap");
690
691#ifdef FEAT_MOUSE
692 setmouse(); /* may start using the mouse */
693#endif
694 if (scroll_region)
695 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000696 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000697
698 /*
699 * Don't clear the screen when starting in Ex mode, unless using the GUI.
700 */
701 if (exmode_active
702#ifdef FEAT_GUI
703 && !gui.in_use
704#endif
705 )
706 must_redraw = CLEAR;
707 else
708 {
709 screenclear(); /* clear screen */
710 TIME_MSG("clearing screen");
711 }
712
713#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000714 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000715 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100716 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200717 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000718 TIME_MSG("getting crypt key");
719 }
720#endif
721
722 no_wait_return = TRUE;
723
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000724 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000725 * Create the requested number of windows and edit buffers in them.
726 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000727 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000728 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000729 TIME_MSG("opening buffers");
730
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000731#ifdef FEAT_EVAL
732 /* clear v:swapcommand */
733 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
734#endif
735
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000736 /* Ex starts at last line of the file */
737 if (exmode_active)
738 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
739
740#ifdef FEAT_AUTOCMD
741 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
742 TIME_MSG("BufEnter autocommands");
743#endif
744 setpcmark();
745
746#ifdef FEAT_QUICKFIX
747 /*
748 * When started with "-q errorfile" jump to first error now.
749 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000750 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000751 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000752 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000753 TIME_MSG("jump to first error");
754 }
755#endif
756
757#ifdef FEAT_WINDOWS
758 /*
759 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000760 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000761 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200762 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000763#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200764 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000765
766#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000767 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000768 {
769 win_T *wp;
770
771 /* set options in each window for "vimdiff". */
Bram Moolenaar29323592016-07-24 22:04:11 +0200772 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000773 diff_win_options(wp, TRUE);
774 }
775#endif
776
777 /*
778 * Shorten any of the filenames, but only when absolute.
779 */
780 shorten_fnames(FALSE);
781
782 /*
783 * Need to jump to the tag before executing the '-c command'.
784 * Makes "vim -c '/return' -t main" work.
785 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000786 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000787 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000788#if defined(HAS_SWAP_EXISTS_ACTION)
789 swap_exists_did_quit = FALSE;
790#endif
791
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000792 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000793 do_cmdline_cmd(IObuff);
794 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000795
796#if defined(HAS_SWAP_EXISTS_ACTION)
797 /* If the user doesn't want to edit the file then we quit here. */
798 if (swap_exists_did_quit)
799 getout(1);
800#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000801 }
802
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000803 /* Execute any "+", "-c" and "-S" arguments. */
804 if (params.n_commands > 0)
805 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000806
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200807 /* Must come before the may_req_ calls. */
808 starting = 0;
809
Bram Moolenaar976787d2017-06-04 15:45:50 +0200810#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
811 /* Must be done before redrawing, puts a few characters on the screen. */
812 may_req_ambiguous_char_width();
813#endif
814
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000815 RedrawingDisabled = 0;
816 redraw_all_later(NOT_VALID);
817 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000818
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200819 /* 'autochdir' has been postponed */
820 DO_AUTOCHDIR
821
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000822#ifdef FEAT_TERMRESPONSE
823 /* Requesting the termresponse is postponed until here, so that a "-c q"
824 * argument doesn't make it appear in the shell Vim was started from. */
825 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200826
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200827 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000828#endif
829
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000830 /* start in insert mode */
831 if (p_im)
832 need_start_insertmode = TRUE;
833
Bram Moolenaar14735512016-03-26 21:00:08 +0100834#ifdef FEAT_EVAL
835 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
836#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000837#ifdef FEAT_AUTOCMD
838 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
839 TIME_MSG("VimEnter autocommands");
840#endif
841
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200842#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
843 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
844 * done after the clipboard is available and all initial commands that may
845 * modify the 'clipboard' setting have run; i.e. just before entering the
846 * main loop. */
847 {
848 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100849
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200850 adjust_clip_reg(&default_regname);
851 set_reg_var(default_regname);
852 }
853#endif
854
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000855#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
856 /* When a startup script or session file setup for diff'ing and
857 * scrollbind, sync the scrollbind now. */
858 if (curwin->w_p_diff && curwin->w_p_scb)
859 {
860 update_topline();
861 check_scrollbind((linenr_T)0, 0L);
862 TIME_MSG("diff scrollbinding");
863 }
864#endif
865
866#if defined(WIN3264) && !defined(FEAT_GUI_W32)
867 mch_set_winsize_now(); /* Allow winsize changes from now on */
868#endif
869
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000870#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
871 /* When tab pages were created, may need to update the tab pages line and
872 * scrollbars. This is skipped while creating them. */
873 if (first_tabpage->tp_next != NULL)
874 {
875 out_flush();
876 gui_init_which_components(NULL);
877 gui_update_scrollbars(TRUE);
878 }
879 need_mouse_correct = TRUE;
880#endif
881
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000882 /* If ":startinsert" command used, stuff a dummy command to be able to
883 * call normal_cmd(), which will then start Insert mode. */
884 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000885 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000886
887#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200888 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200889 {
890# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200891# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200892 && !defined(FEAT_GUI_W32)
893 if (gui.in_use)
894 {
895 mch_errmsg(_("netbeans is not supported with this GUI\n"));
896 mch_exit(2);
897 }
898# endif
899# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000900 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200901 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200902 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000903#endif
904
905 TIME_MSG("before starting main loop");
906
907 /*
908 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100909 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000910 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000911
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200912#endif /* NO_VIM_MAIN */
913
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000914 return 0;
915}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000916
917/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200918 * Initialisation shared by main() and some tests.
919 */
920 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200921common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200922{
923
924#ifdef FEAT_MBYTE
925 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
926#endif
927#ifdef FEAT_EVAL
928 eval_init(); /* init global variables */
929#endif
930
931#ifdef __QNXNTO__
932 qnx_init(); /* PhAttach() for clipboard, (and gui) */
933#endif
934
935#ifdef MAC_OS_CLASSIC
936 /* Prepare for possibly starting GUI sometime */
937 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200938 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200939 TIME_MSG("GUI prepared");
940#endif
941
942 /* Init the table of Normal mode commands. */
943 init_normal_cmds();
944
945#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
946 make_version(); /* Construct the long version string. */
947#endif
948
949 /*
950 * Allocate space for the generic buffers (needed for set_init_1() and
951 * EMSG2()).
952 */
953 if ((IObuff = alloc(IOSIZE)) == NULL
954 || (NameBuff = alloc(MAXPATHL)) == NULL)
955 mch_exit(0);
956 TIME_MSG("Allocated generic buffers");
957
958#ifdef NBDEBUG
959 /* Wait a moment for debugging NetBeans. Must be after allocating
960 * NameBuff. */
961 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
962 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
963 TIME_MSG("NetBeans debug wait");
964#endif
965
966#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
967 /*
968 * Setup to use the current locale (for ctype() and many other things).
969 * NOTE: Translated messages with encodings other than latin1 will not
970 * work until set_init_1() has been called!
971 */
972 init_locale();
973 TIME_MSG("locale set");
974#endif
975
976#ifdef FEAT_GUI
977 gui.dofork = TRUE; /* default is to use fork() */
978#endif
979
980 /*
981 * Do a first scan of the arguments in "argv[]":
982 * -display or --display
983 * --server...
984 * --socketid
985 * --windowid
986 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200987 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200988
989#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200990 findYourself(paramp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200991#endif
992#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
993 /* Prepare for possibly starting GUI sometime */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200994 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200995 TIME_MSG("GUI prepared");
996#endif
997
998#ifdef FEAT_CLIPBOARD
999 clip_init(FALSE); /* Initialise clipboard stuff */
1000 TIME_MSG("clipboard setup");
1001#endif
1002
1003 /*
1004 * Check if we have an interactive window.
1005 * On the Amiga: If there is no window, we open one with a newcli command
1006 * (needed for :! to * work). mch_check_win() will also handle the -d or
1007 * -dev argument.
1008 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001009 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001010 TIME_MSG("window checked");
1011
1012 /*
1013 * Allocate the first window and buffer.
1014 * Can't do anything without it, exit when it fails.
1015 */
1016 if (win_alloc_first() == FAIL)
1017 mch_exit(0);
1018
1019 init_yank(); /* init yank buffers */
1020
1021 alist_init(&global_alist); /* Init the argument list to empty. */
1022 global_alist.id = 0;
1023
1024 /*
1025 * Set the default values for the options.
1026 * NOTE: Non-latin1 translated messages are working only after this,
1027 * because this is where "has_mbyte" will be set, which is used by
1028 * msg_outtrans_len_attr().
1029 * First find out the home directory, needed to expand "~" in options.
1030 */
1031 init_homedir(); /* find real value of $HOME */
1032 set_init_1();
1033 TIME_MSG("inits 1");
1034
1035#ifdef FEAT_EVAL
1036 set_lang_var(); /* set v:lang and v:ctype */
1037#endif
1038}
1039
1040/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001041 * Return TRUE when the --not-a-term argument was found.
1042 */
1043 int
1044is_not_a_term()
1045{
1046 return params.not_a_term;
1047}
1048
1049/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001050 * Main loop: Execute Normal mode commands until exiting Vim.
1051 * Also used to handle commands in the command-line window, until the window
1052 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001053 * Also used to handle ":visual" command after ":global": execute Normal mode
1054 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001055 */
1056 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001057main_loop(
1058 int cmdwin, /* TRUE when working in the command-line window */
1059 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001060{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001061 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001062 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001063#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001064 /* these are static to avoid a compiler warning */
1065 static linenr_T conceal_old_cursor_line = 0;
1066 static linenr_T conceal_new_cursor_line = 0;
1067 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001068#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001069
1070#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1071 /* Setup to catch a terminating error from the X server. Just ignore
1072 * it, restore the state and continue. This might not always work
1073 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001074 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001076 {
1077 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001078 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001079 got_int = TRUE;
1080 need_wait_return = FALSE;
1081 global_busy = FALSE;
1082 exmode_active = 0;
1083 skip_redraw = FALSE;
1084 RedrawingDisabled = 0;
1085 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001086 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001087# ifdef FEAT_EVAL
1088 emsg_skip = 0;
1089# endif
1090 emsg_off = 0;
1091# ifdef FEAT_MOUSE
1092 setmouse();
1093# endif
1094 settmode(TMODE_RAW);
1095 starttermcap();
1096 scroll_start();
1097 redraw_later_clear();
1098 }
1099#endif
1100
1101 clear_oparg(&oa);
1102 while (!cmdwin
1103#ifdef FEAT_CMDWIN
1104 || cmdwin_result == 0
1105#endif
1106 )
1107 {
1108 if (stuff_empty())
1109 {
1110 did_check_timestamps = FALSE;
1111 if (need_check_timestamps)
1112 check_timestamps(FALSE);
1113 if (need_wait_return) /* if wait_return still needed ... */
1114 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001115 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001116 {
1117 need_start_insertmode = FALSE;
1118 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1119 /* skip the fileinfo message now, because it would be shown
1120 * after insert mode finishes! */
1121 need_fileinfo = FALSE;
1122 }
1123 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001124
1125 /* Reset "got_int" now that we got back to the main loop. Except when
1126 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1127 * the ":g" command.
1128 * For ":g/pat/vi" we reset "got_int" when used once. When used
1129 * a second time we go back to Ex mode and abort the ":g" command. */
1130 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001131 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001132 if (noexmode && global_busy && !exmode_active && previous_got_int)
1133 {
1134 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1135 * used and keep "got_int" set, so that it aborts ":g". */
1136 exmode_active = EXMODE_NORMAL;
1137 State = NORMAL;
1138 }
1139 else if (!global_busy || !exmode_active)
1140 {
1141 if (!quit_more)
1142 (void)vgetc(); /* flush all buffers */
1143 got_int = FALSE;
1144 }
1145 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001146 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001147 else
1148 previous_got_int = FALSE;
1149
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001150 if (!exmode_active)
1151 msg_scroll = FALSE;
1152 quit_more = FALSE;
1153
1154 /*
1155 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1156 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1157 * update cursor and redraw.
1158 */
1159 if (skip_redraw || exmode_active)
1160 skip_redraw = FALSE;
1161 else if (do_redraw || stuff_empty())
1162 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001163# ifdef FEAT_GUI
1164 /* If ui_breakcheck() was used a resize may have been postponed. */
1165 gui_may_resize_shell();
1166# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001167#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001168 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001169 if (!finish_op && (
1170# ifdef FEAT_AUTOCMD
1171 has_cursormoved()
1172# endif
1173# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1174 ||
1175# endif
1176# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001177 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001178# endif
1179 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001180# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001181 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001182# endif
1183 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001184 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001185# ifdef FEAT_AUTOCMD
1186 if (has_cursormoved())
1187 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1188 FALSE, curbuf);
1189# endif
1190# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001191 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001192 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001193# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001194 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001195# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001196 conceal_new_cursor_line = curwin->w_cursor.lnum;
1197 conceal_update_lines = TRUE;
1198 }
1199# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001200# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001201 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001202# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001203 }
1204#endif
1205
Bram Moolenaar186628f2013-03-19 13:33:23 +01001206#ifdef FEAT_AUTOCMD
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001207 /* Trigger TextChanged if b:changedtick differs. */
Bram Moolenaar186628f2013-03-19 13:33:23 +01001208 if (!finish_op && has_textchanged()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001209 && last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001210 {
1211 if (last_changedtick_buf == curbuf)
1212 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1213 FALSE, curbuf);
1214 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001215 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001216 }
1217#endif
1218
Bram Moolenaar33aec762006-01-22 23:30:12 +00001219#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1220 /* Scroll-binding for diff mode may have been postponed until
1221 * here. Avoids doing it for every change. */
1222 if (diff_need_scrollbind)
1223 {
1224 check_scrollbind((linenr_T)0, 0L);
1225 diff_need_scrollbind = FALSE;
1226 }
1227#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001228#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001229 /* Include a closed fold completely in the Visual area. */
1230 foldAdjustVisual();
1231#endif
1232#ifdef FEAT_FOLDING
1233 /*
1234 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1235 * contain the cursor.
1236 * When 'foldopen' is "all", open the fold(s) under the cursor.
1237 * This may mark the window for redrawing.
1238 */
1239 if (hasAnyFolding(curwin) && !char_avail())
1240 {
1241 foldCheckClose();
1242 if (fdo_flags & FDO_ALL)
1243 foldOpenCursor();
1244 }
1245#endif
1246
1247 /*
1248 * Before redrawing, make sure w_topline is correct, and w_leftcol
1249 * if lines don't wrap, and w_skipcol if lines wrap.
1250 */
1251 update_topline();
1252 validate_cursor();
1253
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001254 if (VIsual_active)
1255 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001256 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001257 update_screen(0);
1258 else if (redraw_cmdline || clear_cmdline)
1259 showmode();
1260#ifdef FEAT_WINDOWS
1261 redraw_statuslines();
1262#endif
1263#ifdef FEAT_TITLE
1264 if (need_maketitle)
1265 maketitle();
1266#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001267#ifdef FEAT_VIMINFO
1268 curbuf->b_last_used = vim_time();
1269#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001270 /* display message after redraw */
1271 if (keep_msg != NULL)
1272 {
1273 char_u *p;
1274
1275 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001276 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1277 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001278 p = keep_msg;
1279 msg_attr(p, keep_msg_attr);
1280 vim_free(p);
1281 }
1282 if (need_fileinfo) /* show file info after redraw */
1283 {
1284 fileinfo(FALSE, TRUE, FALSE);
1285 need_fileinfo = FALSE;
1286 }
1287
1288 emsg_on_display = FALSE; /* can delete error message now */
1289 did_emsg = FALSE;
1290 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001291 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001292 showruler(FALSE);
1293
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001294# if defined(FEAT_CONCEAL)
1295 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001296 && (conceal_old_cursor_line != conceal_new_cursor_line
1297 || conceal_cursor_line(curwin)
1298 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001299 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001300 if (conceal_old_cursor_line != conceal_new_cursor_line
1301 && conceal_old_cursor_line
1302 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001303 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001304 update_single_line(curwin, conceal_new_cursor_line);
1305 curwin->w_valid &= ~VALID_CROW;
1306 }
1307# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001308 setcursor();
1309 cursor_on();
1310
1311 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001312
1313#ifdef STARTUPTIME
1314 /* Now that we have drawn the first screen all the startup stuff
1315 * has been done, close any file for startup messages. */
1316 if (time_fd != NULL)
1317 {
1318 TIME_MSG("first screen update");
1319 TIME_MSG("--- VIM STARTED ---");
1320 fclose(time_fd);
1321 time_fd = NULL;
1322 }
1323#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001324 }
1325#ifdef FEAT_GUI
1326 if (need_mouse_correct)
1327 gui_mouse_correct();
1328#endif
1329
1330 /*
1331 * Update w_curswant if w_set_curswant has been set.
1332 * Postponed until here to avoid computing w_virtcol too often.
1333 */
1334 update_curswant();
1335
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001336#ifdef FEAT_EVAL
1337 /*
1338 * May perform garbage collection when waiting for a character, but
1339 * only at the very toplevel. Otherwise we may be using a List or
1340 * Dict internally somewhere.
1341 * "may_garbage_collect" is reset in vgetc() which is invoked through
1342 * do_exmode() and normal_cmd().
1343 */
1344 may_garbage_collect = (!cmdwin && !noexmode);
1345#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001346 /*
1347 * If we're invoked as ex, do a round of ex commands.
1348 * Otherwise, get and execute a normal mode command.
1349 */
1350 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001351 {
1352 if (noexmode) /* End of ":global/path/visual" commands */
1353 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001354 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001355 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001356 else
1357 normal_cmd(&oa, TRUE);
1358 }
1359}
1360
1361
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001362#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001363/*
1364 * Exit, but leave behind swap files for modified buffers.
1365 */
1366 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001367getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001368{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001369# if defined(SIGHUP) && defined(SIG_IGN)
1370 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1371 * makes Vim exit and then handling SIGHUP causes various reentrance
1372 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001373 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001374# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001375
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001376 ml_close_notmod(); /* close all not-modified buffers */
1377 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1378 ml_close_all(FALSE); /* close all memfiles, without deleting */
1379 getout(exitval); /* exit Vim properly */
1380}
1381#endif
1382
1383
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001384/*
1385 * Exit properly.
1386 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001387 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001388getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001389{
1390#ifdef FEAT_AUTOCMD
1391 buf_T *buf;
1392 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001393 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001394#endif
1395
1396 exiting = TRUE;
1397
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001398 /* When running in Ex mode an error causes us to exit with a non-zero exit
1399 * code. POSIX requires this, although it's not 100% clear from the
1400 * standard. */
1401 if (exmode_active)
1402 exitval += ex_exitval;
1403
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001404 /* Position the cursor on the last screen line, below all the text */
1405#ifdef FEAT_GUI
1406 if (!gui.in_use)
1407#endif
1408 windgoto((int)Rows - 1, 0);
1409
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001410#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1411 /* Optionally print hashtable efficiency. */
1412 hash_debug_results();
1413#endif
1414
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001415#ifdef FEAT_GUI
1416 msg_didany = FALSE;
1417#endif
1418
1419#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001420 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001421 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001422 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1423# if defined FEAT_WINDOWS
1424 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001425 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001426 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001427 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001428 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001429 if (wp->w_buffer == NULL)
1430 /* Autocmd must have close the buffer already, skip. */
1431 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001432 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001433 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001434 {
1435 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1436 buf->b_fname, FALSE, buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001437 CHANGEDTICK(buf) = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001438 /* start all over, autocommands may mess up the lists */
1439 next_tp = first_tabpage;
1440 break;
1441 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001442 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001443 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001444# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001445 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1446 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001447# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001448
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001449 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001450 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001451 if (buf->b_ml.ml_mfp != NULL)
1452 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001453 bufref_T bufref;
1454
1455 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001456 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001457 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001458 if (!bufref_valid(&bufref))
1459 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001460 break;
1461 }
1462 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1463 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001464#endif
1465
1466#ifdef FEAT_VIMINFO
1467 if (*p_viminfo != NUL)
1468 /* Write out the registers, history, marks etc, to the viminfo file */
1469 write_viminfo(NULL, FALSE);
1470#endif
1471
1472#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001473 if (get_vim_var_nr(VV_DYING) <= 1)
1474 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001475#endif
1476
Bram Moolenaar05159a02005-02-26 23:04:13 +00001477#ifdef FEAT_PROFILE
1478 profile_dump();
1479#endif
1480
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001481 if (did_emsg
1482#ifdef FEAT_GUI
1483 || (gui.in_use && msg_didany && p_verbose > 0)
1484#endif
1485 )
1486 {
1487 /* give the user a chance to read the (error) message */
1488 no_wait_return = FALSE;
1489 wait_return(FALSE);
1490 }
1491
1492#ifdef FEAT_AUTOCMD
1493 /* Position the cursor again, the autocommands may have moved it */
1494# ifdef FEAT_GUI
1495 if (!gui.in_use)
1496# endif
1497 windgoto((int)Rows - 1, 0);
1498#endif
1499
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001500#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001501 job_stop_on_exit();
1502#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001503#ifdef FEAT_LUA
1504 lua_end();
1505#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001506#ifdef FEAT_MZSCHEME
1507 mzscheme_end();
1508#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001509#ifdef FEAT_TCL
1510 tcl_end();
1511#endif
1512#ifdef FEAT_RUBY
1513 ruby_end();
1514#endif
1515#ifdef FEAT_PYTHON
1516 python_end();
1517#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001518#ifdef FEAT_PYTHON3
1519 python3_end();
1520#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001521#ifdef FEAT_PERL
1522 perl_end();
1523#endif
1524#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1525 iconv_end();
1526#endif
1527#ifdef FEAT_NETBEANS_INTG
1528 netbeans_end();
1529#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001530#ifdef FEAT_CSCOPE
1531 cs_end();
1532#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001533#ifdef FEAT_EVAL
1534 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001535 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001536#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001537#if defined(WIN32) && defined(FEAT_MBYTE)
1538 free_cmd_argsW();
1539#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001540
1541 mch_exit(exitval);
1542}
1543
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001544#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1545/*
1546 * Setup to use the current locale (for ctype() and many other things).
1547 */
1548 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001549init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001550{
1551 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001552
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001553# ifdef FEAT_GUI_GTK
1554 /* Tell Gtk not to change our locale settings. */
1555 gtk_disable_setlocale();
1556# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001557# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1558 /* Make sure strtod() uses a decimal point, not a comma. */
1559 setlocale(LC_NUMERIC, "C");
1560# endif
1561
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001562# ifdef WIN32
1563 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1564 * text while it's expecting text in the current locale. This call avoids
1565 * that. */
1566 setlocale(LC_CTYPE, "C");
1567# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001568
1569# ifdef FEAT_GETTEXT
1570 {
1571 int mustfree = FALSE;
1572 char_u *p;
1573
1574# ifdef DYNAMIC_GETTEXT
1575 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001576 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001577# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001578 /* expand_env() doesn't work yet, because g_chartab[] is not
1579 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001580 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1581 if (p != NULL && *p != NUL)
1582 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001583 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001584 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1585 }
1586 if (mustfree)
1587 vim_free(p);
1588 textdomain(VIMPACKAGE);
1589 }
1590# endif
1591}
1592#endif
1593
1594/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001595 * Get the name of the display, before gui_prepare() removes it from
1596 * argv[]. Used for the xterm-clipboard display.
1597 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001598 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001599 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001600 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001601early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001602{
Bram Moolenaar03005972008-11-20 13:12:36 +00001603#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1604 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001605 int argc = parmp->argc;
1606 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001607 int i;
1608
1609 for (i = 1; i < argc; i++)
1610 {
1611 if (STRCMP(argv[i], "--") == 0)
1612 break;
1613# ifdef FEAT_XCLIPBOARD
1614 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001615# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001616 || STRICMP(argv[i], "--display") == 0
1617# endif
1618 )
1619 {
1620 if (i == argc - 1)
1621 mainerr_arg_missing((char_u *)argv[i]);
1622 xterm_display = argv[++i];
1623 }
1624# endif
1625# ifdef FEAT_CLIENTSERVER
1626 else if (STRICMP(argv[i], "--servername") == 0)
1627 {
1628 if (i == argc - 1)
1629 mainerr_arg_missing((char_u *)argv[i]);
1630 parmp->serverName_arg = (char_u *)argv[++i];
1631 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001632 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001633 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001634 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001635 {
1636 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001637# ifdef FEAT_GUI
1638 if (strstr(argv[i], "-wait") != 0)
1639 /* don't fork() when starting the GUI to edit files ourself */
1640 gui.dofork = FALSE;
1641# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001642 }
1643# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001644
1645# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1646# ifdef FEAT_GUI_W32
1647 else if (STRICMP(argv[i], "--windowid") == 0)
1648# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001649 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001650# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001651 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001652 long_u id;
1653 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001654
1655 if (i == argc - 1)
1656 mainerr_arg_missing((char_u *)argv[i]);
1657 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001658 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001659 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001660 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001661 if (count != 1)
1662 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1663 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001664# ifdef FEAT_GUI_W32
1665 win_socket_id = id;
1666# else
1667 gtk_socket_id = id;
1668# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001669 i++;
1670 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001671# endif
1672# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001673 else if (STRICMP(argv[i], "--echo-wid") == 0)
1674 echo_wid_arg = TRUE;
1675# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001676# ifndef FEAT_NETBEANS_INTG
1677 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001678 {
1679 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1680 mch_exit(2);
1681 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001682# endif
1683
Bram Moolenaar58d98232005-07-23 22:25:46 +00001684 }
1685#endif
1686}
1687
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001688#ifndef NO_VIM_MAIN
1689/*
1690 * Get a (optional) count for a Vim argument.
1691 */
1692 static int
1693get_number_arg(
1694 char_u *p, /* pointer to argument */
1695 int *idx, /* index in argument, is incremented */
1696 int def) /* default value */
1697{
1698 if (vim_isdigit(p[*idx]))
1699 {
1700 def = atoi((char *)&(p[*idx]));
1701 while (vim_isdigit(p[*idx]))
1702 *idx = *idx + 1;
1703 }
1704 return def;
1705}
1706
1707/*
1708 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1709 * If the executable name starts with "r" we disable shell commands.
1710 * If the next character is "e" we run in Easy mode.
1711 * If the next character is "g" we run the GUI version.
1712 * If the next characters are "view" we start in readonly mode.
1713 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1714 * If the next characters are "ex" we start in Ex mode. If it's followed
1715 * by "im" use improved Ex mode.
1716 */
1717 static void
1718parse_command_name(mparm_T *parmp)
1719{
1720 char_u *initstr;
1721
1722 initstr = gettail((char_u *)parmp->argv[0]);
1723
1724#ifdef MACOS_X_UNIX
1725 /* An issue has been seen when launching Vim in such a way that
1726 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1727 * executable or a symbolic link of it. Until this issue is resolved
1728 * we prohibit the GUI from being used.
1729 */
1730 if (STRCMP(initstr, parmp->argv[0]) == 0)
1731 disallow_gui = TRUE;
1732
1733 /* TODO: On MacOS X default to gui if argv[0] ends in:
1734 * /Vim.app/Contents/MacOS/Vim */
1735#endif
1736
1737#ifdef FEAT_EVAL
1738 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001739 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001740#endif
1741
1742 if (TOLOWER_ASC(initstr[0]) == 'r')
1743 {
1744 restricted = TRUE;
1745 ++initstr;
1746 }
1747
1748 /* Use evim mode for "evim" and "egvim", not for "editor". */
1749 if (TOLOWER_ASC(initstr[0]) == 'e'
1750 && (TOLOWER_ASC(initstr[1]) == 'v'
1751 || TOLOWER_ASC(initstr[1]) == 'g'))
1752 {
1753#ifdef FEAT_GUI
1754 gui.starting = TRUE;
1755#endif
1756 parmp->evim_mode = TRUE;
1757 ++initstr;
1758 }
1759
1760 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1761 if (TOLOWER_ASC(initstr[0]) == 'g')
1762 {
1763 main_start_gui();
1764#ifdef FEAT_GUI
1765 ++initstr;
1766#endif
1767 }
1768
1769 if (STRNICMP(initstr, "view", 4) == 0)
1770 {
1771 readonlymode = TRUE;
1772 curbuf->b_p_ro = TRUE;
1773 p_uc = 10000; /* don't update very often */
1774 initstr += 4;
1775 }
1776 else if (STRNICMP(initstr, "vim", 3) == 0)
1777 initstr += 3;
1778
1779 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1780 if (STRICMP(initstr, "diff") == 0)
1781 {
1782#ifdef FEAT_DIFF
1783 parmp->diff_mode = TRUE;
1784#else
1785 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1786 mch_errmsg("\n");
1787 mch_exit(2);
1788#endif
1789 }
1790
1791 if (STRNICMP(initstr, "ex", 2) == 0)
1792 {
1793 if (STRNICMP(initstr + 2, "im", 2) == 0)
1794 exmode_active = EXMODE_VIM;
1795 else
1796 exmode_active = EXMODE_NORMAL;
1797 change_compatible(TRUE); /* set 'compatible' */
1798 }
1799}
1800
Bram Moolenaar58d98232005-07-23 22:25:46 +00001801/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001802 * Scan the command line arguments.
1803 */
1804 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001805command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001806{
1807 int argc = parmp->argc;
1808 char **argv = parmp->argv;
1809 int argv_idx; /* index in argv[n][] */
1810 int had_minmin = FALSE; /* found "--" argument */
1811 int want_argument; /* option argument with argument */
1812 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001813 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001814 long n;
1815
1816 --argc;
1817 ++argv;
1818 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1819 while (argc > 0)
1820 {
1821 /*
1822 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1823 */
1824 if (argv[0][0] == '+' && !had_minmin)
1825 {
1826 if (parmp->n_commands >= MAX_ARG_CMDS)
1827 mainerr(ME_EXTRA_CMD, NULL);
1828 argv_idx = -1; /* skip to next argument */
1829 if (argv[0][1] == NUL)
1830 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1831 else
1832 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1833 }
1834
1835 /*
1836 * Optional argument.
1837 */
1838 else if (argv[0][0] == '-' && !had_minmin)
1839 {
1840 want_argument = FALSE;
1841 c = argv[0][argv_idx++];
1842#ifdef VMS
1843 /*
1844 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1845 * and "-/X" as "-X".
1846 */
1847 if (c == '/')
1848 {
1849 c = argv[0][argv_idx++];
1850 c = TOUPPER_ASC(c);
1851 }
1852 else
1853 c = TOLOWER_ASC(c);
1854#endif
1855 switch (c)
1856 {
1857 case NUL: /* "vim -" read from stdin */
1858 /* "ex -" silent mode */
1859 if (exmode_active)
1860 silent_mode = TRUE;
1861 else
1862 {
1863 if (parmp->edit_type != EDIT_NONE)
1864 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1865 parmp->edit_type = EDIT_STDIN;
1866 read_cmd_fd = 2; /* read from stderr instead of stdin */
1867 }
1868 argv_idx = -1; /* skip to next argument */
1869 break;
1870
1871 case '-': /* "--" don't take any more option arguments */
1872 /* "--help" give help message */
1873 /* "--version" give version message */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001874 /* "--clean" clean context */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001875 /* "--literal" take files literally */
1876 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001877 /* "--not-a-term" don't warn for not a term */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001878 /* "--ttyfail" exit if not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001879 /* "--noplugin[s]" skip plugins */
1880 /* "--cmd <cmd>" execute cmd before vimrc */
1881 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1882 usage();
1883 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1884 {
1885 Columns = 80; /* need to init Columns */
1886 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1887 list_version();
1888 msg_putchar('\n');
1889 msg_didout = FALSE;
1890 mch_exit(0);
1891 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001892 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
1893 {
1894 parmp->use_vimrc = (char_u *)"DEFAULTS";
1895 set_option_value((char_u *)"vif", 0L, (char_u *)"NONE", 0);
1896 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001897 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1898 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001899#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001900 parmp->literal = TRUE;
1901#endif
1902 }
1903 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1904 {
1905#ifdef FEAT_GUI
1906 gui.dofork = FALSE; /* don't fork() when starting GUI */
1907#endif
1908 }
1909 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1910 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001911 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1912 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001913 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
1914 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001915 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1916 {
1917 want_argument = TRUE;
1918 argv_idx += 3;
1919 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001920 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1921 {
1922 want_argument = TRUE;
1923 argv_idx += 11;
1924 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001925#ifdef FEAT_CLIENTSERVER
1926 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1927 ; /* already processed -- no arg */
1928 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1929 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1930 {
1931 /* already processed -- snatch the following arg */
1932 if (argc > 1)
1933 {
1934 --argc;
1935 ++argv;
1936 }
1937 }
1938#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001939#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1940# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001941 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001942# else
1943 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1944# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001945 {
1946 /* already processed -- snatch the following arg */
1947 if (argc > 1)
1948 {
1949 --argc;
1950 ++argv;
1951 }
1952 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001953#endif
1954#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001955 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1956 {
1957 /* already processed, skip */
1958 }
1959#endif
1960 else
1961 {
1962 if (argv[0][argv_idx])
1963 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1964 had_minmin = TRUE;
1965 }
1966 if (!want_argument)
1967 argv_idx = -1; /* skip to next argument */
1968 break;
1969
1970 case 'A': /* "-A" start in Arabic mode */
1971#ifdef FEAT_ARABIC
1972 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1973#else
1974 mch_errmsg(_(e_noarabic));
1975 mch_exit(2);
1976#endif
1977 break;
1978
1979 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001980 /* Needs to be effective before expanding file names, because
1981 * for Win32 this makes us edit a shortcut file itself,
1982 * instead of the file it links to. */
1983 set_options_bin(curbuf->b_p_bin, 1, 0);
1984 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001985 break;
1986
1987 case 'C': /* "-C" Compatible */
1988 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02001989 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001990 break;
1991
1992 case 'e': /* "-e" Ex mode */
1993 exmode_active = EXMODE_NORMAL;
1994 break;
1995
1996 case 'E': /* "-E" Improved Ex mode */
1997 exmode_active = EXMODE_VIM;
1998 break;
1999
2000 case 'f': /* "-f" GUI: run in foreground. Amiga: open
2001 window directly, not with newcli */
2002#ifdef FEAT_GUI
2003 gui.dofork = FALSE; /* don't fork() when starting GUI */
2004#endif
2005 break;
2006
2007 case 'g': /* "-g" start GUI */
2008 main_start_gui();
2009 break;
2010
2011 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
2012#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002013 p_fkmap = TRUE;
2014 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002015#else
2016 mch_errmsg(_(e_nofarsi));
2017 mch_exit(2);
2018#endif
2019 break;
2020
2021 case 'h': /* "-h" give help message */
2022#ifdef FEAT_GUI_GNOME
2023 /* Tell usage() to exit for "gvim". */
2024 gui.starting = FALSE;
2025#endif
2026 usage();
2027 break;
2028
2029 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2030#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002031 p_hkmap = TRUE;
2032 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002033#else
2034 mch_errmsg(_(e_nohebrew));
2035 mch_exit(2);
2036#endif
2037 break;
2038
2039 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2040#ifdef FEAT_LISP
2041 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2042 p_sm = TRUE;
2043#endif
2044 break;
2045
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002046 case 'M': /* "-M" no changes or writing of files */
2047 reset_modifiable();
2048 /* FALLTHROUGH */
2049
2050 case 'm': /* "-m" no writing of files */
2051 p_write = FALSE;
2052 break;
2053
2054 case 'y': /* "-y" easy mode */
2055#ifdef FEAT_GUI
2056 gui.starting = TRUE; /* start GUI a bit later */
2057#endif
2058 parmp->evim_mode = TRUE;
2059 break;
2060
2061 case 'N': /* "-N" Nocompatible */
2062 change_compatible(FALSE);
2063 break;
2064
2065 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002066#ifdef FEAT_NETBEANS_INTG
2067 /* checking for "-nb", netbeans parameters */
2068 if (argv[0][argv_idx] == 'b')
2069 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002070 netbeansArg = argv[0];
2071 argv_idx = -1; /* skip to next argument */
2072 }
2073 else
2074#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002075 parmp->no_swap_file = TRUE;
2076 break;
2077
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002078 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002079#ifdef TARGET_API_MAC_OSX
2080 /* For some reason on MacOS X, an argument like:
2081 -psn_0_10223617 is passed in when invoke from Finder
2082 or with the 'open' command */
2083 if (argv[0][argv_idx] == 's')
2084 {
2085 argv_idx = -1; /* bypass full -psn */
2086 main_start_gui();
2087 break;
2088 }
2089#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002090#ifdef FEAT_WINDOWS
2091 /* default is 0: open window for each file */
2092 parmp->window_count = get_number_arg((char_u *)argv[0],
2093 &argv_idx, 0);
2094 parmp->window_layout = WIN_TABS;
2095#endif
2096 break;
2097
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002098 case 'o': /* "-o[N]" open N horizontal split windows */
2099#ifdef FEAT_WINDOWS
2100 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002101 parmp->window_count = get_number_arg((char_u *)argv[0],
2102 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002103 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002104#endif
2105 break;
2106
2107 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002108#ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002109 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002110 parmp->window_count = get_number_arg((char_u *)argv[0],
2111 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002112 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002113#endif
2114 break;
2115
2116#ifdef FEAT_QUICKFIX
2117 case 'q': /* "-q" QuickFix mode */
2118 if (parmp->edit_type != EDIT_NONE)
2119 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2120 parmp->edit_type = EDIT_QF;
2121 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2122 {
2123 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2124 argv_idx = -1;
2125 }
2126 else if (argc > 1) /* "-q {errorfile}" */
2127 want_argument = TRUE;
2128 break;
2129#endif
2130
2131 case 'R': /* "-R" readonly mode */
2132 readonlymode = TRUE;
2133 curbuf->b_p_ro = TRUE;
2134 p_uc = 10000; /* don't update very often */
2135 break;
2136
2137 case 'r': /* "-r" recovery mode */
2138 case 'L': /* "-L" recovery mode */
2139 recoverymode = 1;
2140 break;
2141
2142 case 's':
2143 if (exmode_active) /* "-s" silent (batch) mode */
2144 silent_mode = TRUE;
2145 else /* "-s {scriptin}" read from script file */
2146 want_argument = TRUE;
2147 break;
2148
2149 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2150 if (parmp->edit_type != EDIT_NONE)
2151 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2152 parmp->edit_type = EDIT_TAG;
2153 if (argv[0][argv_idx]) /* "-t{tag}" */
2154 {
2155 parmp->tagname = (char_u *)argv[0] + argv_idx;
2156 argv_idx = -1;
2157 }
2158 else /* "-t {tag}" */
2159 want_argument = TRUE;
2160 break;
2161
2162#ifdef FEAT_EVAL
2163 case 'D': /* "-D" Debugging */
2164 parmp->use_debug_break_level = 9999;
2165 break;
2166#endif
2167#ifdef FEAT_DIFF
2168 case 'd': /* "-d" 'diff' */
2169# ifdef AMIGA
2170 /* check for "-dev {device}" */
2171 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2172 want_argument = TRUE;
2173 else
2174# endif
2175 parmp->diff_mode = TRUE;
2176 break;
2177#endif
2178 case 'V': /* "-V{N}" Verbose level */
2179 /* default is 10: a little bit verbose */
2180 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2181 if (argv[0][argv_idx] != NUL)
2182 {
2183 set_option_value((char_u *)"verbosefile", 0L,
2184 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002185 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002186 }
2187 break;
2188
2189 case 'v': /* "-v" Vi-mode (as if called "vi") */
2190 exmode_active = 0;
2191#ifdef FEAT_GUI
2192 gui.starting = FALSE; /* don't start GUI */
2193#endif
2194 break;
2195
2196 case 'w': /* "-w{number}" set window height */
2197 /* "-w {scriptout}" write to script */
2198 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2199 {
2200 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2201 set_option_value((char_u *)"window", n, NULL, 0);
2202 break;
2203 }
2204 want_argument = TRUE;
2205 break;
2206
2207#ifdef FEAT_CRYPT
2208 case 'x': /* "-x" encrypted reading/writing of files */
2209 parmp->ask_for_key = TRUE;
2210 break;
2211#endif
2212
2213 case 'X': /* "-X" don't connect to X server */
2214#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2215 x_no_connect = TRUE;
2216#endif
2217 break;
2218
2219 case 'Z': /* "-Z" restricted mode */
2220 restricted = TRUE;
2221 break;
2222
2223 case 'c': /* "-c{command}" or "-c {command}" execute
2224 command */
2225 if (argv[0][argv_idx] != NUL)
2226 {
2227 if (parmp->n_commands >= MAX_ARG_CMDS)
2228 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002229 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2230 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002231 argv_idx = -1;
2232 break;
2233 }
2234 /*FALLTHROUGH*/
2235 case 'S': /* "-S {file}" execute Vim script */
2236 case 'i': /* "-i {viminfo}" use for viminfo */
2237#ifndef FEAT_DIFF
2238 case 'd': /* "-d {device}" device (for Amiga) */
2239#endif
2240 case 'T': /* "-T {terminal}" terminal name */
2241 case 'u': /* "-u {vimrc}" vim inits file */
2242 case 'U': /* "-U {gvimrc}" gvim inits file */
2243 case 'W': /* "-W {scriptout}" overwrite */
2244#ifdef FEAT_GUI_W32
2245 case 'P': /* "-P {parent title}" MDI parent */
2246#endif
2247 want_argument = TRUE;
2248 break;
2249
2250 default:
2251 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2252 }
2253
2254 /*
2255 * Handle option arguments with argument.
2256 */
2257 if (want_argument)
2258 {
2259 /*
2260 * Check for garbage immediately after the option letter.
2261 */
2262 if (argv[0][argv_idx] != NUL)
2263 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2264
2265 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002266 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002267 mainerr_arg_missing((char_u *)argv[0]);
2268 ++argv;
2269 argv_idx = -1;
2270
2271 switch (c)
2272 {
2273 case 'c': /* "-c {command}" execute command */
2274 case 'S': /* "-S {file}" execute Vim script */
2275 if (parmp->n_commands >= MAX_ARG_CMDS)
2276 mainerr(ME_EXTRA_CMD, NULL);
2277 if (c == 'S')
2278 {
2279 char *a;
2280
2281 if (argc < 1)
2282 /* "-S" without argument: use default session file
2283 * name. */
2284 a = SESSION_FILE;
2285 else if (argv[0][0] == '-')
2286 {
2287 /* "-S" followed by another option: use default
2288 * session file name. */
2289 a = SESSION_FILE;
2290 ++argc;
2291 --argv;
2292 }
2293 else
2294 a = argv[0];
2295 p = alloc((unsigned)(STRLEN(a) + 4));
2296 if (p == NULL)
2297 mch_exit(2);
2298 sprintf((char *)p, "so %s", a);
2299 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2300 parmp->commands[parmp->n_commands++] = p;
2301 }
2302 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002303 parmp->commands[parmp->n_commands++] =
2304 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002305 break;
2306
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002307 case '-':
2308 if (argv[-1][2] == 'c')
2309 {
2310 /* "--cmd {command}" execute command */
2311 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2312 mainerr(ME_EXTRA_CMD, NULL);
2313 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002314 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002315 }
2316 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002317 break;
2318
2319 /* case 'd': -d {device} is handled in mch_check_win() for the
2320 * Amiga */
2321
2322#ifdef FEAT_QUICKFIX
2323 case 'q': /* "-q {errorfile}" QuickFix mode */
2324 parmp->use_ef = (char_u *)argv[0];
2325 break;
2326#endif
2327
2328 case 'i': /* "-i {viminfo}" use for viminfo */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002329 set_option_value((char_u *)"vif", 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002330 break;
2331
2332 case 's': /* "-s {scriptin}" read from script file */
2333 if (scriptin[0] != NULL)
2334 {
2335scripterror:
2336 mch_errmsg(_("Attempt to open script file again: \""));
2337 mch_errmsg(argv[-1]);
2338 mch_errmsg(" ");
2339 mch_errmsg(argv[0]);
2340 mch_errmsg("\"\n");
2341 mch_exit(2);
2342 }
2343 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2344 {
2345 mch_errmsg(_("Cannot open for reading: \""));
2346 mch_errmsg(argv[0]);
2347 mch_errmsg("\"\n");
2348 mch_exit(2);
2349 }
2350 if (save_typebuf() == FAIL)
2351 mch_exit(2); /* out of memory */
2352 break;
2353
2354 case 't': /* "-t {tag}" */
2355 parmp->tagname = (char_u *)argv[0];
2356 break;
2357
2358 case 'T': /* "-T {terminal}" terminal name */
2359 /*
2360 * The -T term argument is always available and when
2361 * HAVE_TERMLIB is supported it overrides the environment
2362 * variable TERM.
2363 */
2364#ifdef FEAT_GUI
2365 if (term_is_gui((char_u *)argv[0]))
2366 gui.starting = TRUE; /* start GUI a bit later */
2367 else
2368#endif
2369 parmp->term = (char_u *)argv[0];
2370 break;
2371
2372 case 'u': /* "-u {vimrc}" vim inits file */
2373 parmp->use_vimrc = (char_u *)argv[0];
2374 break;
2375
2376 case 'U': /* "-U {gvimrc}" gvim inits file */
2377#ifdef FEAT_GUI
2378 use_gvimrc = (char_u *)argv[0];
2379#endif
2380 break;
2381
2382 case 'w': /* "-w {nr}" 'window' value */
2383 /* "-w {scriptout}" append to script file */
2384 if (vim_isdigit(*((char_u *)argv[0])))
2385 {
2386 argv_idx = 0;
2387 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2388 set_option_value((char_u *)"window", n, NULL, 0);
2389 argv_idx = -1;
2390 break;
2391 }
2392 /*FALLTHROUGH*/
2393 case 'W': /* "-W {scriptout}" overwrite script file */
2394 if (scriptout != NULL)
2395 goto scripterror;
2396 if ((scriptout = mch_fopen(argv[0],
2397 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2398 {
2399 mch_errmsg(_("Cannot open for script output: \""));
2400 mch_errmsg(argv[0]);
2401 mch_errmsg("\"\n");
2402 mch_exit(2);
2403 }
2404 break;
2405
2406#ifdef FEAT_GUI_W32
2407 case 'P': /* "-P {parent title}" MDI parent */
2408 gui_mch_set_parent(argv[0]);
2409 break;
2410#endif
2411 }
2412 }
2413 }
2414
2415 /*
2416 * File name argument.
2417 */
2418 else
2419 {
2420 argv_idx = -1; /* skip to next argument */
2421
2422 /* Check for only one type of editing. */
2423 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2424 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2425 parmp->edit_type = EDIT_FILE;
2426
2427#ifdef MSWIN
2428 /* Remember if the argument was a full path before changing
2429 * slashes to backslashes. */
2430 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2431 parmp->full_path = TRUE;
2432#endif
2433
2434 /* Add the file to the global argument list. */
2435 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2436 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2437 mch_exit(2);
2438#ifdef FEAT_DIFF
2439 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2440 && !mch_isdir(alist_name(&GARGLIST[0])))
2441 {
2442 char_u *r;
2443
2444 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2445 if (r != NULL)
2446 {
2447 vim_free(p);
2448 p = r;
2449 }
2450 }
2451#endif
2452#if defined(__CYGWIN32__) && !defined(WIN32)
2453 /*
2454 * If vim is invoked by non-Cygwin tools, convert away any
2455 * DOS paths, so things like .swp files are created correctly.
2456 * Look for evidence of non-Cygwin paths before we bother.
2457 * This is only for when using the Unix files.
2458 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002459 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002460 {
2461 char posix_path[PATH_MAX];
2462
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002463# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2464 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2465# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002466 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002467# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002468 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002469 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002470 if (p == NULL)
2471 mch_exit(2);
2472 }
2473#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002474
2475#ifdef USE_FNAME_CASE
2476 /* Make the case of the file name match the actual file. */
2477 fname_case(p, 0);
2478#endif
2479
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002480 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002481#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002482 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002483#else
2484 2 /* add buffer number now and use curbuf */
2485#endif
2486 );
2487
2488#if defined(FEAT_MBYTE) && defined(WIN32)
2489 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002490 /* Remember this argument has been added to the argument list.
2491 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002492 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002493# ifdef FEAT_DIFF
2494 parmp->diff_mode
2495# else
2496 FALSE
2497# endif
2498 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002499 }
2500#endif
2501 }
2502
2503 /*
2504 * If there are no more letters after the current "-", go to next
2505 * argument. argv_idx is set to -1 when the current argument is to be
2506 * skipped.
2507 */
2508 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2509 {
2510 --argc;
2511 ++argv;
2512 argv_idx = 1;
2513 }
2514 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002515
2516#ifdef FEAT_EVAL
2517 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2518 * one. */
2519 if (parmp->n_commands > 0)
2520 {
2521 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2522 if (p != NULL)
2523 {
2524 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2525 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2526 vim_free(p);
2527 }
2528 }
2529#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002530}
2531
2532/*
2533 * Print a warning if stdout is not a terminal.
2534 * When starting in Ex mode and commands come from a file, set Silent mode.
2535 */
2536 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002537check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002538{
2539 int input_isatty; /* is active input a terminal? */
2540
2541 input_isatty = mch_input_isatty();
2542 if (exmode_active)
2543 {
2544 if (!input_isatty)
2545 silent_mode = TRUE;
2546 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002547 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002548#ifdef FEAT_GUI
2549 /* don't want the delay when started from the desktop */
2550 && !gui.starting
2551#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002552 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002553 {
2554#ifdef NBDEBUG
2555 /*
2556 * This shouldn't be necessary. But if I run netbeans with the log
2557 * output coming to the console and XOpenDisplay fails, I get vim
2558 * trying to start with input/output to my console tty. This fills my
2559 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002560 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002561 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002562 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002563 {
2564 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2565 exit(1);
2566 }
2567#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002568#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2569 if (is_cygpty_used())
2570 {
2571 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2572 exit(1);
2573 }
2574#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002575 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002576 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2577 if (!input_isatty)
2578 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2579 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002580 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2581 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002582 if (scriptin[0] == NULL)
2583 ui_delay(2000L, TRUE);
2584 TIME_MSG("Warning delay");
2585 }
2586}
2587
2588/*
2589 * Read text from stdin.
2590 */
2591 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002592read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002593{
2594 int i;
2595
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002596#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002597 /* When getting the ATTENTION prompt here, use a dialog */
2598 swap_exists_action = SEA_DIALOG;
2599#endif
2600 no_wait_return = TRUE;
2601 i = msg_didany;
2602 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002603 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002604 no_wait_return = FALSE;
2605 msg_didany = i;
2606 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002607#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002608 check_swap_exists_action();
2609#endif
2610#if !(defined(AMIGA) || defined(MACOS))
2611 /*
2612 * Close stdin and dup it from stderr. Required for GPM to work
2613 * properly, and for running external commands.
2614 * Is there any other system that cannot do this?
2615 */
2616 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002617 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002618#endif
2619}
2620
2621/*
2622 * Create the requested number of windows and edit buffers in them.
2623 * Also does recovery if "recoverymode" set.
2624 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002625 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002626create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002627{
2628#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002629 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002630 int done = 0;
2631
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002632 /*
2633 * Create the number of windows that was requested.
2634 */
2635 if (parmp->window_count == -1) /* was not set */
2636 parmp->window_count = 1;
2637 if (parmp->window_count == 0)
2638 parmp->window_count = GARGCOUNT;
2639 if (parmp->window_count > 1)
2640 {
2641 /* Don't change the windows if there was a command in .vimrc that
2642 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002643 if (parmp->window_layout == 0)
2644 parmp->window_layout = WIN_HOR;
2645 if (parmp->window_layout == WIN_TABS)
2646 {
2647 parmp->window_count = make_tabpages(parmp->window_count);
2648 TIME_MSG("making tab pages");
2649 }
2650 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002651 {
2652 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002653 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002654 TIME_MSG("making windows");
2655 }
2656 else
2657 parmp->window_count = win_count();
2658 }
2659 else
2660 parmp->window_count = 1;
2661#endif
2662
2663 if (recoverymode) /* do recover */
2664 {
2665 msg_scroll = TRUE; /* scroll message up */
2666 ml_recover();
2667 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2668 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002669 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002670 }
2671 else
2672 {
2673 /*
2674 * Open a buffer for windows that don't have one yet.
2675 * Commands in the .vimrc might have loaded a file or split the window.
2676 * Watch out for autocommands that delete a window.
2677 */
2678#ifdef FEAT_AUTOCMD
2679 /*
2680 * Don't execute Win/Buf Enter/Leave autocommands here
2681 */
2682 ++autocmd_no_enter;
2683 ++autocmd_no_leave;
2684#endif
2685#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002686 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002687 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002688 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002689 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002690 {
2691 if (parmp->window_layout == WIN_TABS)
2692 goto_tabpage(1);
2693 else
2694 curwin = firstwin;
2695 }
2696 else if (parmp->window_layout == WIN_TABS)
2697 {
2698 if (curtab->tp_next == NULL)
2699 break;
2700 goto_tabpage(0);
2701 }
2702 else
2703 {
2704 if (curwin->w_next == NULL)
2705 break;
2706 curwin = curwin->w_next;
2707 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002708 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002709#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002710 curbuf = curwin->w_buffer;
2711 if (curbuf->b_ml.ml_mfp == NULL)
2712 {
2713#ifdef FEAT_FOLDING
2714 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2715 if (p_fdls >= 0)
2716 curwin->w_p_fdl = p_fdls;
2717#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002718#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002719 /* When getting the ATTENTION prompt here, use a dialog */
2720 swap_exists_action = SEA_DIALOG;
2721#endif
2722 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002723
2724 /* create memfile, read file */
2725 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002726
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002727#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002728 if (swap_exists_action == SEA_QUIT)
2729 {
2730 if (got_int || only_one_window())
2731 {
2732 /* abort selected or quit and only one window */
2733 did_emsg = FALSE; /* avoid hit-enter prompt */
2734 getout(1);
2735 }
2736 /* We can't close the window, it would disturb what
2737 * happens next. Clear the file name and set the arg
2738 * index to -1 to delete it later. */
2739 setfname(curbuf, NULL, NULL, FALSE);
2740 curwin->w_arg_idx = -1;
2741 swap_exists_action = SEA_NONE;
2742 }
2743 else
2744 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002745#endif
2746#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002747 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002748#endif
2749 }
2750#ifdef FEAT_WINDOWS
2751 ui_breakcheck();
2752 if (got_int)
2753 {
2754 (void)vgetc(); /* only break the file loading, not the rest */
2755 break;
2756 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002757 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002758#endif
2759#ifdef FEAT_WINDOWS
2760 if (parmp->window_layout == WIN_TABS)
2761 goto_tabpage(1);
2762 else
2763 curwin = firstwin;
2764 curbuf = curwin->w_buffer;
2765#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002766#ifdef FEAT_AUTOCMD
2767 --autocmd_no_enter;
2768 --autocmd_no_leave;
2769#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002770 }
2771}
2772
2773#ifdef FEAT_WINDOWS
2774 /*
2775 * If opened more than one window, start editing files in the other
2776 * windows. make_windows() has already opened the windows.
2777 */
2778 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002779edit_buffers(
2780 mparm_T *parmp,
2781 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002782{
2783 int arg_idx; /* index in argument list */
2784 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002785 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002786 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002787
2788# ifdef FEAT_AUTOCMD
2789 /*
2790 * Don't execute Win/Buf Enter/Leave autocommands here
2791 */
2792 ++autocmd_no_enter;
2793 ++autocmd_no_leave;
2794# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002795
2796 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2797 if (curwin->w_arg_idx == -1)
2798 {
2799 win_close(curwin, TRUE);
2800 advance = FALSE;
2801 }
2802
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002803 arg_idx = 1;
2804 for (i = 1; i < parmp->window_count; ++i)
2805 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002806 if (cwd != NULL)
2807 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002808 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2809 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002810 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002811 ++arg_idx;
2812 win_close(curwin, TRUE);
2813 advance = FALSE;
2814 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002815 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002816
Bram Moolenaar84212822006-11-07 21:59:47 +00002817 if (advance)
2818 {
2819 if (parmp->window_layout == WIN_TABS)
2820 {
2821 if (curtab->tp_next == NULL) /* just checking */
2822 break;
2823 goto_tabpage(0);
2824 }
2825 else
2826 {
2827 if (curwin->w_next == NULL) /* just checking */
2828 break;
2829 win_enter(curwin->w_next, FALSE);
2830 }
2831 }
2832 advance = TRUE;
2833
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002834 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002835 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002836 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2837 {
2838 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002839 /* Edit file from arg list, if there is one. When "Quit" selected
2840 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002841# ifdef HAS_SWAP_EXISTS_ACTION
2842 swap_exists_did_quit = FALSE;
2843# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002844 (void)do_ecmd(0, arg_idx < GARGCOUNT
2845 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002846 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002847# ifdef HAS_SWAP_EXISTS_ACTION
2848 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002849 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002850 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002851 if (got_int || only_one_window())
2852 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002853 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002854 did_emsg = FALSE; /* avoid hit-enter prompt */
2855 getout(1);
2856 }
2857 win_close(curwin, TRUE);
2858 advance = FALSE;
2859 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002860# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002861 if (arg_idx == GARGCOUNT - 1)
2862 arg_had_last = TRUE;
2863 ++arg_idx;
2864 }
2865 ui_breakcheck();
2866 if (got_int)
2867 {
2868 (void)vgetc(); /* only break the file loading, not the rest */
2869 break;
2870 }
2871 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002872
2873 if (parmp->window_layout == WIN_TABS)
2874 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002875# ifdef FEAT_AUTOCMD
2876 --autocmd_no_enter;
2877# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002878
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002879 /* make the first window the current window */
2880 win = firstwin;
2881#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2882 /* Avoid making a preview window the current window. */
2883 while (win->w_p_pvw)
2884 {
2885 win = win->w_next;
2886 if (win == NULL)
2887 {
2888 win = firstwin;
2889 break;
2890 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002891 }
2892#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002893 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002894
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002895# ifdef FEAT_AUTOCMD
2896 --autocmd_no_leave;
2897# endif
2898 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002899 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002900 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2901}
2902#endif /* FEAT_WINDOWS */
2903
2904/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002905 * Execute the commands from --cmd arguments "cmds[cnt]".
2906 */
2907 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002908exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002909{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002910 char_u **cmds = parmp->pre_commands;
2911 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002912 int i;
2913
2914 if (cnt > 0)
2915 {
2916 curwin->w_cursor.lnum = 0; /* just in case.. */
2917 sourcing_name = (char_u *)_("pre-vimrc command line");
2918# ifdef FEAT_EVAL
2919 current_SID = SID_CMDARG;
2920# endif
2921 for (i = 0; i < cnt; ++i)
2922 do_cmdline_cmd(cmds[i]);
2923 sourcing_name = NULL;
2924# ifdef FEAT_EVAL
2925 current_SID = 0;
2926# endif
2927 TIME_MSG("--cmd commands");
2928 }
2929}
2930
2931/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002932 * Execute "+", "-c" and "-S" arguments.
2933 */
2934 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002935exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002936{
2937 int i;
2938
2939 /*
2940 * We start commands on line 0, make "vim +/pat file" match a
2941 * pattern on line 1. But don't move the cursor when an autocommand
2942 * with g`" was used.
2943 */
2944 msg_scroll = TRUE;
2945 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2946 curwin->w_cursor.lnum = 0;
2947 sourcing_name = (char_u *)"command line";
2948#ifdef FEAT_EVAL
2949 current_SID = SID_CARG;
2950#endif
2951 for (i = 0; i < parmp->n_commands; ++i)
2952 {
2953 do_cmdline_cmd(parmp->commands[i]);
2954 if (parmp->cmds_tofree[i])
2955 vim_free(parmp->commands[i]);
2956 }
2957 sourcing_name = NULL;
2958#ifdef FEAT_EVAL
2959 current_SID = 0;
2960#endif
2961 if (curwin->w_cursor.lnum == 0)
2962 curwin->w_cursor.lnum = 1;
2963
2964 if (!exmode_active)
2965 msg_scroll = FALSE;
2966
2967#ifdef FEAT_QUICKFIX
2968 /* When started with "-q errorfile" jump to first error again. */
2969 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002970 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002971#endif
2972 TIME_MSG("executing command arguments");
2973}
2974
2975/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002976 * Source startup scripts.
2977 */
2978 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002979source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002980{
2981 int i;
2982
2983 /*
2984 * For "evim" source evim.vim first of all, so that the user can overrule
2985 * any things he doesn't like.
2986 */
2987 if (parmp->evim_mode)
2988 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002989 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002990 TIME_MSG("source evim file");
2991 }
2992
2993 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002994 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002995 * nothing else.
2996 */
2997 if (parmp->use_vimrc != NULL)
2998 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002999 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
3000 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
3001 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003002 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003003 {
3004#ifdef FEAT_GUI
3005 if (use_gvimrc == NULL) /* don't load gvimrc either */
3006 use_gvimrc = parmp->use_vimrc;
3007#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003008 }
3009 else
3010 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003011 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003012 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
3013 }
3014 }
3015 else if (!silent_mode)
3016 {
3017#ifdef AMIGA
3018 struct Process *proc = (struct Process *)FindTask(0L);
3019 APTR save_winptr = proc->pr_WindowPtr;
3020
3021 /* Avoid a requester here for a volume that doesn't exist. */
3022 proc->pr_WindowPtr = (APTR)-1L;
3023#endif
3024
3025 /*
3026 * Get system wide defaults, if the file name is defined.
3027 */
3028#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003029 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003030#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003031#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003032 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003033#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003034
3035 /*
3036 * Try to read initialization commands from the following places:
3037 * - environment variable VIMINIT
3038 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3039 * - second user vimrc file ($VIM/.vimrc for Dos)
3040 * - environment variable EXINIT
3041 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3042 * - second user exrc file ($VIM/.exrc for Dos)
3043 * The first that exists is used, the rest is ignored.
3044 */
3045 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3046 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003047 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003048#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003049 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3050 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003051#endif
3052#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003053 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3054 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003055#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003056#ifdef USR_VIMRC_FILE4
3057 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3058 DOSO_VIMRC) == FAIL
3059#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003060 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003061 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003062#ifdef USR_EXRC_FILE2
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003063 && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003064#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003065 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003066 {
3067 /* When no .vimrc file was found: source defaults.vim. */
3068 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003069 }
3070 }
3071
3072 /*
3073 * Read initialization commands from ".vimrc" or ".exrc" in current
3074 * directory. This is only done if the 'exrc' option is set.
3075 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003076 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003077 * option has been reset in environment of global ".exrc" or ".vimrc".
3078 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3079 * SYS_VIMRC_FILE.
3080 */
3081 if (p_exrc)
3082 {
3083#if defined(UNIX) || defined(VMS)
3084 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3085 if (!file_owned(VIMRC_FILE))
3086#endif
3087 secure = p_secure;
3088
3089 i = FAIL;
3090 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3091 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3092#ifdef USR_VIMRC_FILE2
3093 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3094 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3095#endif
3096#ifdef USR_VIMRC_FILE3
3097 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3098 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3099#endif
3100#ifdef SYS_VIMRC_FILE
3101 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3102 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3103#endif
3104 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003105 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003106
3107 if (i == FAIL)
3108 {
3109#if defined(UNIX) || defined(VMS)
3110 /* if ".exrc" is not owned by user set 'secure' mode */
3111 if (!file_owned(EXRC_FILE))
3112 secure = p_secure;
3113 else
3114 secure = 0;
3115#endif
3116 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3117 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3118#ifdef USR_EXRC_FILE2
3119 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3120 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3121#endif
3122 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003123 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003124 }
3125 }
3126 if (secure == 2)
3127 need_wait_return = TRUE;
3128 secure = 0;
3129#ifdef AMIGA
3130 proc->pr_WindowPtr = save_winptr;
3131#endif
3132 }
3133 TIME_MSG("sourcing vimrc file(s)");
3134}
3135
3136/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003137 * Setup to start using the GUI. Exit with an error when not available.
3138 */
3139 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003140main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003141{
3142#ifdef FEAT_GUI
3143 gui.starting = TRUE; /* start GUI a bit later */
3144#else
3145 mch_errmsg(_(e_nogvim));
3146 mch_errmsg("\n");
3147 mch_exit(2);
3148#endif
3149}
3150
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003151#endif /* NO_VIM_MAIN */
3152
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003153/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003154 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003155 * Returns FAIL if the environment variable was not executed, OK otherwise.
3156 */
3157 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003158process_env(
3159 char_u *env,
3160 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003161{
3162 char_u *initstr;
3163 char_u *save_sourcing_name;
3164 linenr_T save_sourcing_lnum;
3165#ifdef FEAT_EVAL
3166 scid_T save_sid;
3167#endif
3168
3169 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3170 {
3171 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003172 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003173 save_sourcing_name = sourcing_name;
3174 save_sourcing_lnum = sourcing_lnum;
3175 sourcing_name = env;
3176 sourcing_lnum = 0;
3177#ifdef FEAT_EVAL
3178 save_sid = current_SID;
3179 current_SID = SID_ENV;
3180#endif
3181 do_cmdline_cmd(initstr);
3182 sourcing_name = save_sourcing_name;
3183 sourcing_lnum = save_sourcing_lnum;
3184#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003185 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003186#endif
3187 return OK;
3188 }
3189 return FAIL;
3190}
3191
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003192#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003193/*
3194 * Return TRUE if we are certain the user owns the file "fname".
3195 * Used for ".vimrc" and ".exrc".
3196 * Use both stat() and lstat() for extra security.
3197 */
3198 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003199file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003200{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003201 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003202# ifdef UNIX
3203 uid_t uid = getuid();
3204# else /* VMS */
3205 uid_t uid = ((getgid() << 16) | getuid());
3206# endif
3207
3208 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3209# ifdef HAVE_LSTAT
3210 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3211# endif
3212 );
3213}
3214#endif
3215
3216/*
3217 * Give an error message main_errors["n"] and exit.
3218 */
3219 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003220mainerr(
3221 int n, /* one of the ME_ defines */
3222 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003223{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003224#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003225 reset_signals(); /* kill us with CTRL-C here, if you like */
3226#endif
3227
3228 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003229 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003230 mch_errmsg(_(main_errors[n]));
3231 if (str != NULL)
3232 {
3233 mch_errmsg(": \"");
3234 mch_errmsg((char *)str);
3235 mch_errmsg("\"");
3236 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003237 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003238
3239 mch_exit(1);
3240}
3241
3242 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003243mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003244{
3245 mainerr(ME_ARG_MISSING, str);
3246}
3247
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003248#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003249/*
3250 * print a message with three spaces prepended and '\n' appended.
3251 */
3252 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003253main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003254{
3255 mch_msg(" ");
3256 mch_msg(s);
3257 mch_msg("\n");
3258}
3259
3260/*
3261 * Print messages for "vim -h" or "vim --help" and exit.
3262 */
3263 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003264usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003265{
3266 int i;
3267 static char *(use[]) =
3268 {
3269 N_("[file ..] edit specified file(s)"),
3270 N_("- read text from stdin"),
3271 N_("-t tag edit file where tag is defined"),
3272#ifdef FEAT_QUICKFIX
3273 N_("-q [errorfile] edit file with first error")
3274#endif
3275 };
3276
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003277#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003278 reset_signals(); /* kill us with CTRL-C here, if you like */
3279#endif
3280
3281 mch_msg(longVersion);
3282 mch_msg(_("\n\nusage:"));
3283 for (i = 0; ; ++i)
3284 {
3285 mch_msg(_(" vim [arguments] "));
3286 mch_msg(_(use[i]));
3287 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3288 break;
3289 mch_msg(_("\n or:"));
3290 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003291#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003292 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003293#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003294
3295 mch_msg(_("\n\nArguments:\n"));
3296 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003297#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003298 main_msg(_("--literal\t\tDon't expand wildcards"));
3299#endif
3300#ifdef FEAT_OLE
3301 main_msg(_("-register\t\tRegister this gvim for OLE"));
3302 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3303#endif
3304#ifdef FEAT_GUI
3305 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3306 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3307#endif
3308 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3309 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003310 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003311 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3312#ifdef FEAT_DIFF
3313 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3314#endif
3315 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3316 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3317 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3318 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3319 main_msg(_("-M\t\t\tModifications in text not allowed"));
3320 main_msg(_("-b\t\t\tBinary mode"));
3321#ifdef FEAT_LISP
3322 main_msg(_("-l\t\t\tLisp mode"));
3323#endif
3324 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3325 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003326 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3327#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003328 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003329#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003330 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3331 main_msg(_("-r\t\t\tList swap files and exit"));
3332 main_msg(_("-r (with file name)\tRecover crashed session"));
3333 main_msg(_("-L\t\t\tSame as -r"));
3334#ifdef AMIGA
3335 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3336 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3337#endif
3338#ifdef FEAT_ARABIC
3339 main_msg(_("-A\t\t\tstart in Arabic mode"));
3340#endif
3341#ifdef FEAT_RIGHTLEFT
3342 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3343#endif
3344#ifdef FEAT_FKMAP
3345 main_msg(_("-F\t\t\tStart in Farsi mode"));
3346#endif
3347 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003348 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003349 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003350 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3351#ifdef FEAT_GUI
3352 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3353#endif
3354 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003355#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003356 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003357 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3358 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003359#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003360 main_msg(_("+\t\t\tStart at end of file"));
3361 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003362 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003363 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3364 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3365 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3366 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3367 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3368#ifdef FEAT_CRYPT
3369 main_msg(_("-x\t\t\tEdit encrypted files"));
3370#endif
3371#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3372# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3373 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3374# endif
3375 main_msg(_("-X\t\t\tDo not connect to X server"));
3376#endif
3377#ifdef FEAT_CLIENTSERVER
3378 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3379 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3380 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3381 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003382# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003383 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003384# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003385 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3386 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3387 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3388 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3389#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003390#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003391 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003392#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003393#ifdef FEAT_VIMINFO
3394 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3395#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003396 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003397 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3398 main_msg(_("--version\t\tPrint version information and exit"));
3399
3400#ifdef FEAT_GUI_X11
3401# ifdef FEAT_GUI_MOTIF
3402 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3403# else
3404# ifdef FEAT_GUI_ATHENA
3405# ifdef FEAT_GUI_NEXTAW
3406 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3407# else
3408 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3409# endif
3410# endif
3411# endif
3412 main_msg(_("-display <display>\tRun vim on <display>"));
3413 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003414 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3415 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3416 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3417 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3418 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3419 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3420 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3421 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3422# ifdef FEAT_GUI_ATHENA
3423 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3424# endif
3425 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3426 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3427 main_msg(_("-xrm <resource>\tSet the specified resource"));
3428#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003429#ifdef FEAT_GUI_GTK
3430 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3431 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3432 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3433 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3434 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003435 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003436 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003437 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003438#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003439#ifdef FEAT_GUI_W32
3440 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003441 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003442#endif
3443
3444#ifdef FEAT_GUI_GNOME
3445 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3446 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003447 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003448 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003449 gui.dofork = FALSE;
3450 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003451 else
3452#endif
3453 mch_exit(0);
3454}
3455
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003456#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003457/*
3458 * Check the result of the ATTENTION dialog:
3459 * When "Quit" selected, exit Vim.
3460 * When "Recover" selected, recover the file.
3461 */
3462 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003463check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003464{
3465 if (swap_exists_action == SEA_QUIT)
3466 getout(1);
3467 handle_swap_exists(NULL);
3468}
3469#endif
3470
Bram Moolenaar08cab962017-03-04 14:37:18 +01003471#endif /* NO_VIM_MAIN */
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003472
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003473#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003474static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003475
3476static struct timeval prev_timeval;
3477
Bram Moolenaar3f269672009-11-03 11:11:11 +00003478# ifdef WIN3264
3479/*
3480 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3481 */
3482 static int
3483gettimeofday(struct timeval *tv, char *dummy)
3484{
3485 long t = clock();
3486 tv->tv_sec = t / CLOCKS_PER_SEC;
3487 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3488 return 0;
3489}
3490# endif
3491
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003492/*
3493 * Save the previous time before doing something that could nest.
3494 * set "*tv_rel" to the time elapsed so far.
3495 */
3496 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003497time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003498{
3499 *((struct timeval *)tv_rel) = prev_timeval;
3500 gettimeofday(&prev_timeval, NULL);
3501 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3502 - ((struct timeval *)tv_rel)->tv_usec;
3503 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3504 - ((struct timeval *)tv_rel)->tv_sec;
3505 if (((struct timeval *)tv_rel)->tv_usec < 0)
3506 {
3507 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3508 --((struct timeval *)tv_rel)->tv_sec;
3509 }
3510 *(struct timeval *)tv_start = prev_timeval;
3511}
3512
3513/*
3514 * Compute the previous time after doing something that could nest.
3515 * Subtract "*tp" from prev_timeval;
3516 * Note: The arguments are (void *) to avoid trouble with systems that don't
3517 * have struct timeval.
3518 */
3519 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003520time_pop(
3521 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003522{
3523 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3524 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3525 if (prev_timeval.tv_usec < 0)
3526 {
3527 prev_timeval.tv_usec += 1000000;
3528 --prev_timeval.tv_sec;
3529 }
3530}
3531
3532 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003533time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003534{
3535 long usec;
3536 long msec;
3537
3538 usec = now->tv_usec - then->tv_usec;
3539 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3540 usec = usec % 1000L;
3541 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3542}
3543
3544 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003545time_msg(
3546 char *mesg,
3547 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003548 (struct timeval *) */
3549{
3550 static struct timeval start;
3551 struct timeval now;
3552
3553 if (time_fd != NULL)
3554 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003555 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003556 {
3557 gettimeofday(&start, NULL);
3558 prev_timeval = start;
3559 fprintf(time_fd, "\n\ntimes in msec\n");
3560 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3561 fprintf(time_fd, " clock elapsed: other lines\n\n");
3562 }
3563 gettimeofday(&now, NULL);
3564 time_diff(&start, &now);
3565 if (((struct timeval *)tv_start) != NULL)
3566 {
3567 fprintf(time_fd, " ");
3568 time_diff(((struct timeval *)tv_start), &now);
3569 }
3570 fprintf(time_fd, " ");
3571 time_diff(&prev_timeval, &now);
3572 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003573 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003574 }
3575}
3576
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003577#endif
3578
Bram Moolenaar595297d2017-03-04 19:11:12 +01003579#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003580 static void
3581set_progpath(char_u *argv0)
3582{
3583 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003584
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003585# ifdef PROC_EXE_LINK
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003586 char buf[PATH_MAX + 1];
3587 ssize_t len;
3588
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003589 len = readlink(PROC_EXE_LINK, buf, PATH_MAX);
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003590 if (len > 0)
3591 {
3592 buf[len] = NUL;
3593 val = (char_u *)buf;
3594 }
3595# else
Bram Moolenaar08cab962017-03-04 14:37:18 +01003596 /* A relative path containing a "/" will become invalid when using ":cd",
3597 * turn it into a full path.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003598 * On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3599 * this. */
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003600# ifdef WIN32
Bram Moolenaar066029e2017-03-05 15:19:32 +01003601 char_u *path = NULL;
3602
3603 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3604 val = path;
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003605# else
Bram Moolenaar066029e2017-03-05 15:19:32 +01003606 char_u buf[MAXPATHL];
3607
Bram Moolenaar43663192017-03-05 14:29:12 +01003608 if (!mch_isFullName(argv0))
3609 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003610 if (gettail(argv0) != argv0
3611 && vim_FullName(argv0, buf, MAXPATHL, TRUE) != FAIL)
3612 val = buf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003613 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003614# endif
Bram Moolenaar066029e2017-03-05 15:19:32 +01003615# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003616
Bram Moolenaar08cab962017-03-04 14:37:18 +01003617 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003618
Bram Moolenaar066029e2017-03-05 15:19:32 +01003619# ifdef WIN32
Bram Moolenaar43663192017-03-05 14:29:12 +01003620 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003621# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003622}
3623
3624#endif /* NO_VIM_MAIN */
3625
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003626#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003627
3628/*
3629 * Common code for the X command server and the Win32 command server.
3630 */
3631
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003632static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003633
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003634/*
3635 * Do the client-server stuff, unless "--servername ''" was used.
3636 */
3637 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003638exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003639{
3640 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3641 {
3642# ifdef WIN32
3643 /* Initialise the client/server messaging infrastructure. */
3644 serverInitMessaging();
3645# endif
3646
3647 /*
3648 * When a command server argument was found, execute it. This may
3649 * exit Vim when it was successful. Otherwise it's executed further
3650 * on. Remember the encoding used here in "serverStrEnc".
3651 */
3652 if (parmp->serverArg)
3653 {
3654 cmdsrv_main(&parmp->argc, parmp->argv,
3655 parmp->serverName_arg, &parmp->serverStr);
3656# ifdef FEAT_MBYTE
3657 parmp->serverStrEnc = vim_strsave(p_enc);
3658# endif
3659 }
3660
3661 /* If we're still running, get the name to register ourselves.
3662 * On Win32 can register right now, for X11 need to setup the
3663 * clipboard first, it's further down. */
3664 parmp->servername = serverMakeName(parmp->serverName_arg,
3665 parmp->argv[0]);
3666# ifdef WIN32
3667 if (parmp->servername != NULL)
3668 {
3669 serverSetName(parmp->servername);
3670 vim_free(parmp->servername);
3671 }
3672# endif
3673 }
3674}
3675
3676/*
3677 * Prepare for running as a Vim server.
3678 */
3679 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003680prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003681{
3682# if defined(FEAT_X11)
3683 /*
3684 * Register for remote command execution with :serversend and --remote
3685 * unless there was a -X or a --servername '' on the command line.
3686 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003687 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003688 */
3689 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3690# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003691 (gui.in_use
3692# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003693 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003694# endif
3695 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003696# endif
3697 parmp->serverName_arg != NULL))
3698 {
3699 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3700 vim_free(parmp->servername);
3701 TIME_MSG("register server name");
3702 }
3703 else
3704 serverDelayedStartName = parmp->servername;
3705# endif
3706
3707 /*
3708 * Execute command ourselves if we're here because the send failed (or
3709 * else we would have exited above).
3710 */
3711 if (parmp->serverStr != NULL)
3712 {
3713 char_u *p;
3714
3715 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3716 parmp->serverStr, &p));
3717 vim_free(p);
3718 }
3719}
3720
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003721 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003722cmdsrv_main(
3723 int *argc,
3724 char **argv,
3725 char_u *serverName_arg,
3726 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003727{
3728 char_u *res;
3729 int i;
3730 char_u *sname;
3731 int ret;
3732 int didone = FALSE;
3733 int exiterr = 0;
3734 char **newArgV = argv + 1;
3735 int newArgC = 1,
3736 Argc = *argc;
3737 int argtype;
3738#define ARGTYPE_OTHER 0
3739#define ARGTYPE_EDIT 1
3740#define ARGTYPE_EDIT_WAIT 2
3741#define ARGTYPE_SEND 3
3742 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003743 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003744# ifndef FEAT_X11
3745 HWND srv;
3746# else
3747 Window srv;
3748
3749 setup_term_clip();
3750# endif
3751
3752 sname = serverMakeName(serverName_arg, argv[0]);
3753 if (sname == NULL)
3754 return;
3755
3756 /*
3757 * Execute the command server related arguments and remove them
3758 * from the argc/argv array; We may have to return into main()
3759 */
3760 for (i = 1; i < Argc; i++)
3761 {
3762 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003763 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003764 {
3765 for (; i < *argc; i++)
3766 {
3767 *newArgV++ = argv[i];
3768 newArgC++;
3769 }
3770 break;
3771 }
3772
Bram Moolenaareb94e552006-03-11 21:35:11 +00003773 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003774 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003775 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3776 {
3777 char *p = argv[i] + 8;
3778
3779 argtype = ARGTYPE_EDIT;
3780 while (*p != NUL)
3781 {
3782 if (STRNICMP(p, "-wait", 5) == 0)
3783 {
3784 argtype = ARGTYPE_EDIT_WAIT;
3785 p += 5;
3786 }
3787 else if (STRNICMP(p, "-silent", 7) == 0)
3788 {
3789 silent = TRUE;
3790 p += 7;
3791 }
3792 else if (STRNICMP(p, "-tab", 4) == 0)
3793 {
3794 tabs = TRUE;
3795 p += 4;
3796 }
3797 else
3798 {
3799 argtype = ARGTYPE_OTHER;
3800 break;
3801 }
3802 }
3803 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003804 else
3805 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003806
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003807 if (argtype != ARGTYPE_OTHER)
3808 {
3809 if (i == *argc - 1)
3810 mainerr_arg_missing((char_u *)argv[i]);
3811 if (argtype == ARGTYPE_SEND)
3812 {
3813 *serverStr = (char_u *)argv[i + 1];
3814 i++;
3815 }
3816 else
3817 {
3818 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003819 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003820 if (*serverStr == NULL)
3821 {
3822 /* Probably out of memory, exit. */
3823 didone = TRUE;
3824 exiterr = 1;
3825 break;
3826 }
3827 Argc = i;
3828 }
3829# ifdef FEAT_X11
3830 if (xterm_dpy == NULL)
3831 {
3832 mch_errmsg(_("No display"));
3833 ret = -1;
3834 }
3835 else
3836 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003837 NULL, &srv, 0, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003838# else
3839 /* Win32 always works? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003840 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003841# endif
3842 if (ret < 0)
3843 {
3844 if (argtype == ARGTYPE_SEND)
3845 {
3846 /* Failed to send, abort. */
3847 mch_errmsg(_(": Send failed.\n"));
3848 didone = TRUE;
3849 exiterr = 1;
3850 }
3851 else if (!silent)
3852 /* Let vim start normally. */
3853 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3854 break;
3855 }
3856
3857# ifdef FEAT_GUI_W32
3858 /* Guess that when the server name starts with "g" it's a GUI
3859 * server, which we can bring to the foreground here.
3860 * Foreground() in the server doesn't work very well. */
3861 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3862 SetForegroundWindow(srv);
3863# endif
3864
3865 /*
3866 * For --remote-wait: Wait until the server did edit each
3867 * file. Also detect that the server no longer runs.
3868 */
3869 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3870 {
3871 int numFiles = *argc - i - 1;
3872 int j;
3873 char_u *done = alloc(numFiles);
3874 char_u *p;
3875# ifdef FEAT_GUI_W32
3876 NOTIFYICONDATA ni;
3877 int count = 0;
3878 extern HWND message_window;
3879# endif
3880
3881 if (numFiles > 0 && argv[i + 1][0] == '+')
3882 /* Skip "+cmd" argument, don't wait for it to be edited. */
3883 --numFiles;
3884
3885# ifdef FEAT_GUI_W32
3886 ni.cbSize = sizeof(ni);
3887 ni.hWnd = message_window;
3888 ni.uID = 0;
3889 ni.uFlags = NIF_ICON|NIF_TIP;
3890 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3891 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3892 Shell_NotifyIcon(NIM_ADD, &ni);
3893# endif
3894
3895 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003896 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003897 while (memchr(done, 0, numFiles) != NULL)
3898 {
3899# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003900 p = serverGetReply(srv, NULL, TRUE, TRUE, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003901 if (p == NULL)
3902 break;
3903# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003904 if (serverReadReply(xterm_dpy, srv, &p, TRUE, -1) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003905 break;
3906# endif
3907 j = atoi((char *)p);
3908 if (j >= 0 && j < numFiles)
3909 {
3910# ifdef FEAT_GUI_W32
3911 ++count;
3912 sprintf(ni.szTip, _("%d of %d edited"),
3913 count, numFiles);
3914 Shell_NotifyIcon(NIM_MODIFY, &ni);
3915# endif
3916 done[j] = 1;
3917 }
3918 }
3919# ifdef FEAT_GUI_W32
3920 Shell_NotifyIcon(NIM_DELETE, &ni);
3921# endif
3922 }
3923 }
3924 else if (STRICMP(argv[i], "--remote-expr") == 0)
3925 {
3926 if (i == *argc - 1)
3927 mainerr_arg_missing((char_u *)argv[i]);
3928# ifdef WIN32
3929 /* Win32 always works? */
3930 if (serverSendToVim(sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003931 &res, NULL, 1, 0, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003932# else
3933 if (xterm_dpy == NULL)
3934 mch_errmsg(_("No display: Send expression failed.\n"));
3935 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003936 &res, NULL, 1, 0, 1, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003937# endif
3938 {
3939 if (res != NULL && *res != NUL)
3940 {
3941 /* Output error from remote */
3942 mch_errmsg((char *)res);
3943 vim_free(res);
3944 res = NULL;
3945 }
3946 mch_errmsg(_(": Send expression failed.\n"));
3947 }
3948 }
3949 else if (STRICMP(argv[i], "--serverlist") == 0)
3950 {
3951# ifdef WIN32
3952 /* Win32 always works? */
3953 res = serverGetVimNames();
3954# else
3955 if (xterm_dpy != NULL)
3956 res = serverGetVimNames(xterm_dpy);
3957# endif
3958 if (called_emsg)
3959 mch_errmsg("\n");
3960 }
3961 else if (STRICMP(argv[i], "--servername") == 0)
3962 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003963 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003964 i++;
3965 continue;
3966 }
3967 else
3968 {
3969 *newArgV++ = argv[i];
3970 newArgC++;
3971 continue;
3972 }
3973 didone = TRUE;
3974 if (res != NULL && *res != NUL)
3975 {
3976 mch_msg((char *)res);
3977 if (res[STRLEN(res) - 1] != '\n')
3978 mch_msg("\n");
3979 }
3980 vim_free(res);
3981 }
3982
3983 if (didone)
3984 {
3985 display_errors(); /* display any collected messages */
3986 exit(exiterr); /* Mission accomplished - get out */
3987 }
3988
3989 /* Return back into main() */
3990 *argc = newArgC;
3991 vim_free(sname);
3992}
3993
3994/*
3995 * Build a ":drop" command to send to a Vim server.
3996 */
3997 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003998build_drop_cmd(
3999 int filec,
4000 char **filev,
4001 int tabs, /* Use ":tab drop" instead of ":drop". */
4002 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004003{
4004 garray_T ga;
4005 int i;
4006 char_u *inicmd = NULL;
4007 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004008 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004009 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004010
4011 if (filec > 0 && filev[0][0] == '+')
4012 {
4013 inicmd = (char_u *)filev[0] + 1;
4014 filev++;
4015 filec--;
4016 }
4017 /* Check if we have at least one argument. */
4018 if (filec <= 0)
4019 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004020
4021 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02004022 cwd = alloc(MAXPATHL);
4023 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004024 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004025 if (mch_dirname(cwd, MAXPATHL) != OK)
4026 {
4027 vim_free(cwd);
4028 return NULL;
4029 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004030 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00004031#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004032 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00004033#else
4034 PATH_ESC_CHARS,
4035#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02004036 '\\', TRUE);
4037 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004038 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004039 return NULL;
4040 ga_init2(&ga, 1, 100);
4041 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004042 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00004043
4044 /* Call inputsave() so that a prompt for an encryption key works. */
4045 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
4046 if (tabs)
4047 ga_concat(&ga, (char_u *)"tab ");
4048 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004049 for (i = 0; i < filec; i++)
4050 {
4051 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004052 * do it again in the Vim server. On MS-Windows only escape
4053 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004054 p = vim_strsave_escaped((char_u *)filev[i],
4055#ifdef UNIX
4056 PATH_ESC_CHARS
4057#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004058 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004059#endif
4060 );
4061 if (p == NULL)
4062 {
4063 vim_free(ga.ga_data);
4064 return NULL;
4065 }
4066 ga_concat(&ga, (char_u *)" ");
4067 ga_concat(&ga, p);
4068 vim_free(p);
4069 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004070 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4071
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004072 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4073 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004074 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4075
4076 /* Switch back to the correct current directory (prior to temporary path
4077 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004078 * correct after the :drop command. With line breaks and spaces:
4079 * if !exists('+acd') || !&acd
4080 * if haslocaldir()
4081 * cd -
4082 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004083 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004084 * cd -
4085 * endif
4086 * endif
4087 */
4088 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004089 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004090 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004091 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004092 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004093
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004094 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004095 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4096 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004097 if (inicmd != NULL)
4098 {
4099 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4100 * the following commands to be inserted as text. Use a "|",
4101 * hopefully "inicmd" does allow this... */
4102 ga_concat(&ga, inicmd);
4103 ga_concat(&ga, (char_u *)"|");
4104 }
4105 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4106 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004107 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004108 ga_append(&ga, NUL);
4109 return ga.ga_data;
4110}
4111
4112/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004113 * Make our basic server name: use the specified "arg" if given, otherwise use
4114 * the tail of the command "cmd" we were started with.
4115 * Return the name in allocated memory. This doesn't include a serial number.
4116 */
4117 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004118serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004119{
4120 char_u *p;
4121
4122 if (arg != NULL && *arg != NUL)
4123 p = vim_strsave_up(arg);
4124 else
4125 {
4126 p = vim_strsave_up(gettail((char_u *)cmd));
4127 /* Remove .exe or .bat from the name. */
4128 if (p != NULL && vim_strchr(p, '.') != NULL)
4129 *vim_strchr(p, '.') = NUL;
4130 }
4131 return p;
4132}
4133#endif /* FEAT_CLIENTSERVER */
4134
4135#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4136/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004137 * Replace termcodes such as <CR> and insert as key presses if there is room.
4138 */
4139 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004140server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004141{
4142 char_u *ptr = NULL;
4143 char_u *cpo_save = p_cpo;
4144
4145 /* Set 'cpoptions' the way we want it.
4146 * B set - backslashes are *not* treated specially
4147 * k set - keycodes are *not* reverse-engineered
4148 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004149 * The last but one parameter of replace_termcodes() is TRUE so that the
4150 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004151 */
4152 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004153 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004154 p_cpo = cpo_save;
4155
4156 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4157 {
4158 /*
4159 * Add the string to the input stream.
4160 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4161 *
4162 * First clear typed characters from the typeahead buffer, there could
4163 * be half a mapping there. Then append to the existing string, so
4164 * that multiple commands from a client are concatenated.
4165 */
4166 if (typebuf.tb_maplen < typebuf.tb_len)
4167 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4168 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4169
4170 /* Let input_available() know we inserted text in the typeahead
4171 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004172 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004173 }
4174 vim_free((char_u *)ptr);
4175}
4176
4177/*
4178 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004179 */
4180 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004181eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004182{
4183 char_u *res;
4184 int save_dbl = debug_break_level;
4185 int save_ro = redir_off;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004186 void *fc;
4187
4188 /* Evaluate the expression at the toplevel, don't use variables local to
4189 * the calling function. */
4190 fc = clear_current_funccal();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004191
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004192 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4193 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004194 debug_break_level = -1;
4195 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004196 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4197 * to be typed. Do generate errors so that try/catch works. */
4198 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004199
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004200 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004201
4202 debug_break_level = save_dbl;
4203 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004204 --emsg_silent;
4205 if (emsg_silent < 0)
4206 emsg_silent = 0;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004207 restore_current_funccal(fc);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004208
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004209 /* A client can tell us to redraw, but not to display the cursor, so do
4210 * that here. */
4211 setcursor();
4212 out_flush();
4213#ifdef FEAT_GUI
4214 if (gui.in_use)
4215 gui_update_cursor(FALSE, FALSE);
4216#endif
4217
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004218 return res;
4219}
4220
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004221/*
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004222 * Evaluate a command or expression sent to ourselves.
4223 */
4224 int
4225sendToLocalVim(char_u *cmd, int asExpr, char_u **result)
4226{
4227 if (asExpr)
4228 {
4229 char_u *ret;
4230
4231 ret = eval_client_expr_to_string(cmd);
4232 if (result != NULL)
4233 {
4234 if (ret == NULL)
4235 {
4236 char *err = _(e_invexprmsg);
4237 size_t len = STRLEN(cmd) + STRLEN(err) + 5;
4238 char_u *msg;
4239
Bram Moolenaar1662ce12017-03-19 21:47:50 +01004240 msg = alloc((unsigned)len);
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004241 if (msg != NULL)
4242 vim_snprintf((char *)msg, len, "%s: \"%s\"", err, cmd);
4243 *result = msg;
4244 }
4245 else
4246 *result = ret;
4247 }
4248 else
4249 vim_free(ret);
4250 return ret == NULL ? -1 : 0;
4251 }
4252 server_to_input_buf(cmd);
4253 return 0;
4254}
4255
4256/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004257 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4258 * return an allocated string. Otherwise return "data".
4259 * "*tofree" is set to the result when it needs to be freed later.
4260 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004261 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004262serverConvert(
4263 char_u *client_enc UNUSED,
4264 char_u *data,
4265 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004266{
4267 char_u *res = data;
4268
4269 *tofree = NULL;
4270# ifdef FEAT_MBYTE
4271 if (client_enc != NULL && p_enc != NULL)
4272 {
4273 vimconv_T vimconv;
4274
4275 vimconv.vc_type = CONV_NONE;
4276 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4277 && vimconv.vc_type != CONV_NONE)
4278 {
4279 res = string_convert(&vimconv, data, NULL);
4280 if (res == NULL)
4281 res = data;
4282 else
4283 *tofree = res;
4284 }
4285 convert_setup(&vimconv, NULL, NULL);
4286 }
4287# endif
4288 return res;
4289}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004290#endif