blob: 67c0a30d420ed1f04e5af1f3918cc1b8f8e5bf62 [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. */
436 if (params.use_vimrc != NULL && STRCMP(params.use_vimrc, "NONE") == 0)
437 p_lpl = FALSE;
438
Bram Moolenaar58d98232005-07-23 22:25:46 +0000439 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000440 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000441
Bram Moolenaar58d98232005-07-23 22:25:46 +0000442 /* Source startup scripts. */
443 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000444
445#ifdef FEAT_EVAL
446 /*
447 * Read all the plugin files.
448 * Only when compiled with +eval, since most plugins need it.
449 */
450 if (p_lpl)
451 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200452 char_u *rtp_copy = NULL;
453
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200454 /* First add all package directories to 'runtimepath', so that their
455 * autoload directories can be found. Only if not done already with a
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200456 * :packloadall command.
457 * Make a copy of 'runtimepath', so that source_runtime does not use
458 * the pack directories. */
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200459 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200460 {
461 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200462 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200463 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200464
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200465 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy,
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000466# ifdef VMS /* Somehow VMS doesn't handle the "**". */
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200467 (char_u *)"plugin/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000468# else
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200469 (char_u *)"plugin/**/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000470# endif
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200471 DIP_ALL | DIP_NOAFTER);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000472 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200473 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100474
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200475 /* Only source "start" packages if not done already with a :packloadall
476 * command. */
477 if (!did_source_packages)
478 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100479 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200480
481# ifdef VMS /* Somehow VMS doesn't handle the "**". */
482 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_AFTER);
483# else
484 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_AFTER);
485# endif
486 TIME_MSG("loading after plugins");
487
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000488 }
489#endif
490
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000491#ifdef FEAT_DIFF
492 /* Decide about window layout for diff mode after reading vimrc. */
493 if (params.diff_mode && params.window_layout == 0)
494 {
495 if (diffopt_horizontal())
496 params.window_layout = WIN_HOR; /* use horizontal split */
497 else
498 params.window_layout = WIN_VER; /* use vertical split */
499 }
500#endif
501
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000502 /*
503 * Recovery mode without a file name: List swap files.
504 * This uses the 'dir' option, therefore it must be after the
505 * initializations.
506 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200507 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000508 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200509 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000510 mch_exit(0);
511 }
512
513 /*
514 * Set a few option defaults after reading .vimrc files:
515 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
516 */
517 set_init_3();
518 TIME_MSG("inits 3");
519
520 /*
521 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
522 * Note that this overrides anything from a vimrc file.
523 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000524 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000525 p_uc = 0;
526
527#ifdef FEAT_FKMAP
528 if (curwin->w_p_rl && p_altkeymap)
529 {
530 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
531# ifdef FEAT_ARABIC
532 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
533# endif
534 p_fkmap = TRUE; /* Set the Farsi keymap mode */
535 }
536#endif
537
538#ifdef FEAT_GUI
539 if (gui.starting)
540 {
541#if defined(UNIX) || defined(VMS)
542 /* When something caused a message from a vimrc script, need to output
543 * an extra newline before the shell prompt. */
544 if (did_emsg || msg_didout)
545 putchar('\n');
546#endif
547
548 gui_start(); /* will set full_screen to TRUE */
549 TIME_MSG("starting GUI");
550
551 /* When running "evim" or "gvim -y" we need the menus, exit if we
552 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000553 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000554 mch_exit(1);
555 }
556#endif
557
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000558#ifdef FEAT_VIMINFO
559 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000560 * Read in registers, history etc, but not marks, from the viminfo file.
561 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000562 */
563 if (*p_viminfo != NUL)
564 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000565 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000566 TIME_MSG("reading viminfo");
567 }
568#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100569#ifdef FEAT_EVAL
570 /* It's better to make v:oldfiles an empty list than NULL. */
571 if (get_vim_var_list(VV_OLDFILES) == NULL)
572 set_vim_var_list(VV_OLDFILES, list_alloc());
573#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000574
575#ifdef FEAT_QUICKFIX
576 /*
577 * "-q errorfile": Load the error file now.
578 * If the error file can't be read, exit before doing anything else.
579 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000580 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000581 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100582 char_u *enc = NULL;
583
584# ifdef FEAT_MBYTE
585 enc = p_menc;
586# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000587 if (params.use_ef != NULL)
588 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000589 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200590 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100591 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000592 {
593 out_char('\n');
594 mch_exit(3);
595 }
596 TIME_MSG("reading errorfile");
597 }
598#endif
599
600 /*
601 * Start putting things on the screen.
602 * Scroll screen down before drawing over it
603 * Clear screen now, so file message will not be cleared.
604 */
605 starting = NO_BUFFERS;
606 no_wait_return = FALSE;
607 if (!exmode_active)
608 msg_scroll = FALSE;
609
610#ifdef FEAT_GUI
611 /*
612 * This seems to be required to make callbacks to be called now, instead
613 * of after things have been put on the screen, which then may be deleted
614 * when getting a resize callback.
615 * For the Mac this handles putting files dropped on the Vim icon to
616 * global_alist.
617 */
618 if (gui.in_use)
619 {
620# ifdef FEAT_SUN_WORKSHOP
621 if (!usingSunWorkShop)
622# endif
623 gui_wait_for_chars(50L);
624 TIME_MSG("GUI delay");
625 }
626#endif
627
628#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
629 qnx_clip_init();
630#endif
631
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200632#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
633 clip_init(TRUE);
634#endif
635
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000636#ifdef FEAT_XCLIPBOARD
637 /* Start using the X clipboard, unless the GUI was started. */
638# ifdef FEAT_GUI
639 if (!gui.in_use)
640# endif
641 {
642 setup_term_clip();
643 TIME_MSG("setup clipboard");
644 }
645#endif
646
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000647#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000648 /* Prepare for being a Vim server. */
649 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000650#endif
651
652 /*
653 * If "-" argument given: Read file from stdin.
654 * Do this before starting Raw mode, because it may change things that the
655 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
656 * are the same terminal: "cat | vim -".
657 * Using autocommands here may cause trouble...
658 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000659 if (params.edit_type == EDIT_STDIN && !recoverymode)
660 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000661
662#if defined(UNIX) || defined(VMS)
663 /* When switching screens and something caused a message from a vimrc
664 * script, need to output an extra newline on exit. */
665 if ((did_emsg || msg_didout) && *T_TI != NUL)
666 newline_on_exit = TRUE;
667#endif
668
669 /*
670 * When done something that is not allowed or error message call
671 * wait_return. This must be done before starttermcap(), because it may
672 * switch to another screen. It must be done after settmode(TMODE_RAW),
673 * because we want to react on a single key stroke.
674 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000675 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000676 */
677 settmode(TMODE_RAW);
678 TIME_MSG("setting raw mode");
679
680 if (need_wait_return || msg_didany)
681 {
682 wait_return(TRUE);
683 TIME_MSG("waiting for return");
684 }
685
686 starttermcap(); /* start termcap if not done by wait_return() */
687 TIME_MSG("start termcap");
688
689#ifdef FEAT_MOUSE
690 setmouse(); /* may start using the mouse */
691#endif
692 if (scroll_region)
693 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000694 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000695
696 /*
697 * Don't clear the screen when starting in Ex mode, unless using the GUI.
698 */
699 if (exmode_active
700#ifdef FEAT_GUI
701 && !gui.in_use
702#endif
703 )
704 must_redraw = CLEAR;
705 else
706 {
707 screenclear(); /* clear screen */
708 TIME_MSG("clearing screen");
709 }
710
711#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000712 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000713 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100714 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200715 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000716 TIME_MSG("getting crypt key");
717 }
718#endif
719
720 no_wait_return = TRUE;
721
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000722 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000723 * Create the requested number of windows and edit buffers in them.
724 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000725 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000726 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000727 TIME_MSG("opening buffers");
728
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000729#ifdef FEAT_EVAL
730 /* clear v:swapcommand */
731 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
732#endif
733
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000734 /* Ex starts at last line of the file */
735 if (exmode_active)
736 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
737
738#ifdef FEAT_AUTOCMD
739 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
740 TIME_MSG("BufEnter autocommands");
741#endif
742 setpcmark();
743
744#ifdef FEAT_QUICKFIX
745 /*
746 * When started with "-q errorfile" jump to first error now.
747 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000748 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000749 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000750 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000751 TIME_MSG("jump to first error");
752 }
753#endif
754
755#ifdef FEAT_WINDOWS
756 /*
757 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000758 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000759 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200760 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000761#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200762 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000763
764#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000765 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000766 {
767 win_T *wp;
768
769 /* set options in each window for "vimdiff". */
Bram Moolenaar29323592016-07-24 22:04:11 +0200770 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000771 diff_win_options(wp, TRUE);
772 }
773#endif
774
775 /*
776 * Shorten any of the filenames, but only when absolute.
777 */
778 shorten_fnames(FALSE);
779
780 /*
781 * Need to jump to the tag before executing the '-c command'.
782 * Makes "vim -c '/return' -t main" work.
783 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000784 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000785 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000786#if defined(HAS_SWAP_EXISTS_ACTION)
787 swap_exists_did_quit = FALSE;
788#endif
789
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000790 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000791 do_cmdline_cmd(IObuff);
792 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000793
794#if defined(HAS_SWAP_EXISTS_ACTION)
795 /* If the user doesn't want to edit the file then we quit here. */
796 if (swap_exists_did_quit)
797 getout(1);
798#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000799 }
800
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000801 /* Execute any "+", "-c" and "-S" arguments. */
802 if (params.n_commands > 0)
803 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000804
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200805 /* Must come before the may_req_ calls. */
806 starting = 0;
807
Bram Moolenaar976787d2017-06-04 15:45:50 +0200808#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
809 /* Must be done before redrawing, puts a few characters on the screen. */
810 may_req_ambiguous_char_width();
811#endif
812
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000813 RedrawingDisabled = 0;
814 redraw_all_later(NOT_VALID);
815 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000816
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200817 /* 'autochdir' has been postponed */
818 DO_AUTOCHDIR
819
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000820#ifdef FEAT_TERMRESPONSE
821 /* Requesting the termresponse is postponed until here, so that a "-c q"
822 * argument doesn't make it appear in the shell Vim was started from. */
823 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200824
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200825 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000826#endif
827
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000828 /* start in insert mode */
829 if (p_im)
830 need_start_insertmode = TRUE;
831
Bram Moolenaar14735512016-03-26 21:00:08 +0100832#ifdef FEAT_EVAL
833 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
834#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000835#ifdef FEAT_AUTOCMD
836 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
837 TIME_MSG("VimEnter autocommands");
838#endif
839
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200840#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
841 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
842 * done after the clipboard is available and all initial commands that may
843 * modify the 'clipboard' setting have run; i.e. just before entering the
844 * main loop. */
845 {
846 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100847
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200848 adjust_clip_reg(&default_regname);
849 set_reg_var(default_regname);
850 }
851#endif
852
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000853#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
854 /* When a startup script or session file setup for diff'ing and
855 * scrollbind, sync the scrollbind now. */
856 if (curwin->w_p_diff && curwin->w_p_scb)
857 {
858 update_topline();
859 check_scrollbind((linenr_T)0, 0L);
860 TIME_MSG("diff scrollbinding");
861 }
862#endif
863
864#if defined(WIN3264) && !defined(FEAT_GUI_W32)
865 mch_set_winsize_now(); /* Allow winsize changes from now on */
866#endif
867
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000868#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
869 /* When tab pages were created, may need to update the tab pages line and
870 * scrollbars. This is skipped while creating them. */
871 if (first_tabpage->tp_next != NULL)
872 {
873 out_flush();
874 gui_init_which_components(NULL);
875 gui_update_scrollbars(TRUE);
876 }
877 need_mouse_correct = TRUE;
878#endif
879
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000880 /* If ":startinsert" command used, stuff a dummy command to be able to
881 * call normal_cmd(), which will then start Insert mode. */
882 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000883 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000884
885#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200886 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200887 {
888# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200889# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200890 && !defined(FEAT_GUI_W32)
891 if (gui.in_use)
892 {
893 mch_errmsg(_("netbeans is not supported with this GUI\n"));
894 mch_exit(2);
895 }
896# endif
897# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000898 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200899 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200900 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000901#endif
902
903 TIME_MSG("before starting main loop");
904
905 /*
906 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100907 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000908 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000909
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200910#endif /* NO_VIM_MAIN */
911
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000912 return 0;
913}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000914
915/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200916 * Initialisation shared by main() and some tests.
917 */
918 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200919common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200920{
921
922#ifdef FEAT_MBYTE
923 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
924#endif
925#ifdef FEAT_EVAL
926 eval_init(); /* init global variables */
927#endif
928
929#ifdef __QNXNTO__
930 qnx_init(); /* PhAttach() for clipboard, (and gui) */
931#endif
932
933#ifdef MAC_OS_CLASSIC
934 /* Prepare for possibly starting GUI sometime */
935 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200936 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200937 TIME_MSG("GUI prepared");
938#endif
939
940 /* Init the table of Normal mode commands. */
941 init_normal_cmds();
942
943#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
944 make_version(); /* Construct the long version string. */
945#endif
946
947 /*
948 * Allocate space for the generic buffers (needed for set_init_1() and
949 * EMSG2()).
950 */
951 if ((IObuff = alloc(IOSIZE)) == NULL
952 || (NameBuff = alloc(MAXPATHL)) == NULL)
953 mch_exit(0);
954 TIME_MSG("Allocated generic buffers");
955
956#ifdef NBDEBUG
957 /* Wait a moment for debugging NetBeans. Must be after allocating
958 * NameBuff. */
959 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
960 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
961 TIME_MSG("NetBeans debug wait");
962#endif
963
964#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
965 /*
966 * Setup to use the current locale (for ctype() and many other things).
967 * NOTE: Translated messages with encodings other than latin1 will not
968 * work until set_init_1() has been called!
969 */
970 init_locale();
971 TIME_MSG("locale set");
972#endif
973
974#ifdef FEAT_GUI
975 gui.dofork = TRUE; /* default is to use fork() */
976#endif
977
978 /*
979 * Do a first scan of the arguments in "argv[]":
980 * -display or --display
981 * --server...
982 * --socketid
983 * --windowid
984 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200985 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200986
987#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200988 findYourself(paramp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200989#endif
990#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
991 /* Prepare for possibly starting GUI sometime */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200992 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200993 TIME_MSG("GUI prepared");
994#endif
995
996#ifdef FEAT_CLIPBOARD
997 clip_init(FALSE); /* Initialise clipboard stuff */
998 TIME_MSG("clipboard setup");
999#endif
1000
1001 /*
1002 * Check if we have an interactive window.
1003 * On the Amiga: If there is no window, we open one with a newcli command
1004 * (needed for :! to * work). mch_check_win() will also handle the -d or
1005 * -dev argument.
1006 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001007 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001008 TIME_MSG("window checked");
1009
1010 /*
1011 * Allocate the first window and buffer.
1012 * Can't do anything without it, exit when it fails.
1013 */
1014 if (win_alloc_first() == FAIL)
1015 mch_exit(0);
1016
1017 init_yank(); /* init yank buffers */
1018
1019 alist_init(&global_alist); /* Init the argument list to empty. */
1020 global_alist.id = 0;
1021
1022 /*
1023 * Set the default values for the options.
1024 * NOTE: Non-latin1 translated messages are working only after this,
1025 * because this is where "has_mbyte" will be set, which is used by
1026 * msg_outtrans_len_attr().
1027 * First find out the home directory, needed to expand "~" in options.
1028 */
1029 init_homedir(); /* find real value of $HOME */
1030 set_init_1();
1031 TIME_MSG("inits 1");
1032
1033#ifdef FEAT_EVAL
1034 set_lang_var(); /* set v:lang and v:ctype */
1035#endif
1036}
1037
1038/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001039 * Return TRUE when the --not-a-term argument was found.
1040 */
1041 int
1042is_not_a_term()
1043{
1044 return params.not_a_term;
1045}
1046
1047/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001048 * Main loop: Execute Normal mode commands until exiting Vim.
1049 * Also used to handle commands in the command-line window, until the window
1050 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001051 * Also used to handle ":visual" command after ":global": execute Normal mode
1052 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001053 */
1054 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001055main_loop(
1056 int cmdwin, /* TRUE when working in the command-line window */
1057 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001058{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001059 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001060 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001061#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001062 /* these are static to avoid a compiler warning */
1063 static linenr_T conceal_old_cursor_line = 0;
1064 static linenr_T conceal_new_cursor_line = 0;
1065 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001066#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001067
1068#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1069 /* Setup to catch a terminating error from the X server. Just ignore
1070 * it, restore the state and continue. This might not always work
1071 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001072 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001073 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001074 {
1075 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001076 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001077 got_int = TRUE;
1078 need_wait_return = FALSE;
1079 global_busy = FALSE;
1080 exmode_active = 0;
1081 skip_redraw = FALSE;
1082 RedrawingDisabled = 0;
1083 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001084 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001085# ifdef FEAT_EVAL
1086 emsg_skip = 0;
1087# endif
1088 emsg_off = 0;
1089# ifdef FEAT_MOUSE
1090 setmouse();
1091# endif
1092 settmode(TMODE_RAW);
1093 starttermcap();
1094 scroll_start();
1095 redraw_later_clear();
1096 }
1097#endif
1098
1099 clear_oparg(&oa);
1100 while (!cmdwin
1101#ifdef FEAT_CMDWIN
1102 || cmdwin_result == 0
1103#endif
1104 )
1105 {
1106 if (stuff_empty())
1107 {
1108 did_check_timestamps = FALSE;
1109 if (need_check_timestamps)
1110 check_timestamps(FALSE);
1111 if (need_wait_return) /* if wait_return still needed ... */
1112 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001113 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001114 {
1115 need_start_insertmode = FALSE;
1116 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1117 /* skip the fileinfo message now, because it would be shown
1118 * after insert mode finishes! */
1119 need_fileinfo = FALSE;
1120 }
1121 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001122
1123 /* Reset "got_int" now that we got back to the main loop. Except when
1124 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1125 * the ":g" command.
1126 * For ":g/pat/vi" we reset "got_int" when used once. When used
1127 * a second time we go back to Ex mode and abort the ":g" command. */
1128 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001129 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001130 if (noexmode && global_busy && !exmode_active && previous_got_int)
1131 {
1132 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1133 * used and keep "got_int" set, so that it aborts ":g". */
1134 exmode_active = EXMODE_NORMAL;
1135 State = NORMAL;
1136 }
1137 else if (!global_busy || !exmode_active)
1138 {
1139 if (!quit_more)
1140 (void)vgetc(); /* flush all buffers */
1141 got_int = FALSE;
1142 }
1143 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001144 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001145 else
1146 previous_got_int = FALSE;
1147
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001148 if (!exmode_active)
1149 msg_scroll = FALSE;
1150 quit_more = FALSE;
1151
1152 /*
1153 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1154 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1155 * update cursor and redraw.
1156 */
1157 if (skip_redraw || exmode_active)
1158 skip_redraw = FALSE;
1159 else if (do_redraw || stuff_empty())
1160 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001161# ifdef FEAT_GUI
1162 /* If ui_breakcheck() was used a resize may have been postponed. */
1163 gui_may_resize_shell();
1164# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001165#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001166 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001167 if (!finish_op && (
1168# ifdef FEAT_AUTOCMD
1169 has_cursormoved()
1170# endif
1171# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1172 ||
1173# endif
1174# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001175 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001176# endif
1177 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001178# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001179 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001180# endif
1181 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001182 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001183# ifdef FEAT_AUTOCMD
1184 if (has_cursormoved())
1185 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1186 FALSE, curbuf);
1187# endif
1188# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001189 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001190 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001191# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001192 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001193# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001194 conceal_new_cursor_line = curwin->w_cursor.lnum;
1195 conceal_update_lines = TRUE;
1196 }
1197# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001198# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001199 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001200# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001201 }
1202#endif
1203
Bram Moolenaar186628f2013-03-19 13:33:23 +01001204#ifdef FEAT_AUTOCMD
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001205 /* Trigger TextChanged if b:changedtick differs. */
Bram Moolenaar186628f2013-03-19 13:33:23 +01001206 if (!finish_op && has_textchanged()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001207 && last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001208 {
1209 if (last_changedtick_buf == curbuf)
1210 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1211 FALSE, curbuf);
1212 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001213 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001214 }
1215#endif
1216
Bram Moolenaar33aec762006-01-22 23:30:12 +00001217#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1218 /* Scroll-binding for diff mode may have been postponed until
1219 * here. Avoids doing it for every change. */
1220 if (diff_need_scrollbind)
1221 {
1222 check_scrollbind((linenr_T)0, 0L);
1223 diff_need_scrollbind = FALSE;
1224 }
1225#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001226#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001227 /* Include a closed fold completely in the Visual area. */
1228 foldAdjustVisual();
1229#endif
1230#ifdef FEAT_FOLDING
1231 /*
1232 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1233 * contain the cursor.
1234 * When 'foldopen' is "all", open the fold(s) under the cursor.
1235 * This may mark the window for redrawing.
1236 */
1237 if (hasAnyFolding(curwin) && !char_avail())
1238 {
1239 foldCheckClose();
1240 if (fdo_flags & FDO_ALL)
1241 foldOpenCursor();
1242 }
1243#endif
1244
1245 /*
1246 * Before redrawing, make sure w_topline is correct, and w_leftcol
1247 * if lines don't wrap, and w_skipcol if lines wrap.
1248 */
1249 update_topline();
1250 validate_cursor();
1251
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001252 if (VIsual_active)
1253 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001254 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001255 update_screen(0);
1256 else if (redraw_cmdline || clear_cmdline)
1257 showmode();
1258#ifdef FEAT_WINDOWS
1259 redraw_statuslines();
1260#endif
1261#ifdef FEAT_TITLE
1262 if (need_maketitle)
1263 maketitle();
1264#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001265#ifdef FEAT_VIMINFO
1266 curbuf->b_last_used = vim_time();
1267#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001268 /* display message after redraw */
1269 if (keep_msg != NULL)
1270 {
1271 char_u *p;
1272
1273 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001274 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1275 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001276 p = keep_msg;
1277 msg_attr(p, keep_msg_attr);
1278 vim_free(p);
1279 }
1280 if (need_fileinfo) /* show file info after redraw */
1281 {
1282 fileinfo(FALSE, TRUE, FALSE);
1283 need_fileinfo = FALSE;
1284 }
1285
1286 emsg_on_display = FALSE; /* can delete error message now */
1287 did_emsg = FALSE;
1288 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001289 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001290 showruler(FALSE);
1291
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001292# if defined(FEAT_CONCEAL)
1293 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001294 && (conceal_old_cursor_line != conceal_new_cursor_line
1295 || conceal_cursor_line(curwin)
1296 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001297 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001298 if (conceal_old_cursor_line != conceal_new_cursor_line
1299 && conceal_old_cursor_line
1300 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001301 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001302 update_single_line(curwin, conceal_new_cursor_line);
1303 curwin->w_valid &= ~VALID_CROW;
1304 }
1305# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001306 setcursor();
1307 cursor_on();
1308
1309 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001310
1311#ifdef STARTUPTIME
1312 /* Now that we have drawn the first screen all the startup stuff
1313 * has been done, close any file for startup messages. */
1314 if (time_fd != NULL)
1315 {
1316 TIME_MSG("first screen update");
1317 TIME_MSG("--- VIM STARTED ---");
1318 fclose(time_fd);
1319 time_fd = NULL;
1320 }
1321#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001322 }
1323#ifdef FEAT_GUI
1324 if (need_mouse_correct)
1325 gui_mouse_correct();
1326#endif
1327
1328 /*
1329 * Update w_curswant if w_set_curswant has been set.
1330 * Postponed until here to avoid computing w_virtcol too often.
1331 */
1332 update_curswant();
1333
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001334#ifdef FEAT_EVAL
1335 /*
1336 * May perform garbage collection when waiting for a character, but
1337 * only at the very toplevel. Otherwise we may be using a List or
1338 * Dict internally somewhere.
1339 * "may_garbage_collect" is reset in vgetc() which is invoked through
1340 * do_exmode() and normal_cmd().
1341 */
1342 may_garbage_collect = (!cmdwin && !noexmode);
1343#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001344 /*
1345 * If we're invoked as ex, do a round of ex commands.
1346 * Otherwise, get and execute a normal mode command.
1347 */
1348 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001349 {
1350 if (noexmode) /* End of ":global/path/visual" commands */
1351 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001352 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001353 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001354 else
1355 normal_cmd(&oa, TRUE);
1356 }
1357}
1358
1359
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001360#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001361/*
1362 * Exit, but leave behind swap files for modified buffers.
1363 */
1364 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001365getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001366{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001367# if defined(SIGHUP) && defined(SIG_IGN)
1368 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1369 * makes Vim exit and then handling SIGHUP causes various reentrance
1370 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001371 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001372# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001373
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001374 ml_close_notmod(); /* close all not-modified buffers */
1375 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1376 ml_close_all(FALSE); /* close all memfiles, without deleting */
1377 getout(exitval); /* exit Vim properly */
1378}
1379#endif
1380
1381
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001382/*
1383 * Exit properly.
1384 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001385 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001386getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001387{
1388#ifdef FEAT_AUTOCMD
1389 buf_T *buf;
1390 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001391 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001392#endif
1393
1394 exiting = TRUE;
1395
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001396 /* When running in Ex mode an error causes us to exit with a non-zero exit
1397 * code. POSIX requires this, although it's not 100% clear from the
1398 * standard. */
1399 if (exmode_active)
1400 exitval += ex_exitval;
1401
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001402 /* Position the cursor on the last screen line, below all the text */
1403#ifdef FEAT_GUI
1404 if (!gui.in_use)
1405#endif
1406 windgoto((int)Rows - 1, 0);
1407
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001408#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1409 /* Optionally print hashtable efficiency. */
1410 hash_debug_results();
1411#endif
1412
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001413#ifdef FEAT_GUI
1414 msg_didany = FALSE;
1415#endif
1416
1417#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001418 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001419 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001420 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1421# if defined FEAT_WINDOWS
1422 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001423 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001424 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001425 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001426 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001427 if (wp->w_buffer == NULL)
1428 /* Autocmd must have close the buffer already, skip. */
1429 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001430 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001431 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001432 {
1433 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1434 buf->b_fname, FALSE, buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001435 CHANGEDTICK(buf) = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001436 /* start all over, autocommands may mess up the lists */
1437 next_tp = first_tabpage;
1438 break;
1439 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001440 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001441 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001442# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001443 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1444 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001445# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001446
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001447 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001448 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001449 if (buf->b_ml.ml_mfp != NULL)
1450 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001451 bufref_T bufref;
1452
1453 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001454 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001455 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001456 if (!bufref_valid(&bufref))
1457 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001458 break;
1459 }
1460 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1461 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001462#endif
1463
1464#ifdef FEAT_VIMINFO
1465 if (*p_viminfo != NUL)
1466 /* Write out the registers, history, marks etc, to the viminfo file */
1467 write_viminfo(NULL, FALSE);
1468#endif
1469
1470#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001471 if (get_vim_var_nr(VV_DYING) <= 1)
1472 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001473#endif
1474
Bram Moolenaar05159a02005-02-26 23:04:13 +00001475#ifdef FEAT_PROFILE
1476 profile_dump();
1477#endif
1478
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001479 if (did_emsg
1480#ifdef FEAT_GUI
1481 || (gui.in_use && msg_didany && p_verbose > 0)
1482#endif
1483 )
1484 {
1485 /* give the user a chance to read the (error) message */
1486 no_wait_return = FALSE;
1487 wait_return(FALSE);
1488 }
1489
1490#ifdef FEAT_AUTOCMD
1491 /* Position the cursor again, the autocommands may have moved it */
1492# ifdef FEAT_GUI
1493 if (!gui.in_use)
1494# endif
1495 windgoto((int)Rows - 1, 0);
1496#endif
1497
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001498#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001499 job_stop_on_exit();
1500#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001501#ifdef FEAT_LUA
1502 lua_end();
1503#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001504#ifdef FEAT_MZSCHEME
1505 mzscheme_end();
1506#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001507#ifdef FEAT_TCL
1508 tcl_end();
1509#endif
1510#ifdef FEAT_RUBY
1511 ruby_end();
1512#endif
1513#ifdef FEAT_PYTHON
1514 python_end();
1515#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001516#ifdef FEAT_PYTHON3
1517 python3_end();
1518#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001519#ifdef FEAT_PERL
1520 perl_end();
1521#endif
1522#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1523 iconv_end();
1524#endif
1525#ifdef FEAT_NETBEANS_INTG
1526 netbeans_end();
1527#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001528#ifdef FEAT_CSCOPE
1529 cs_end();
1530#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001531#ifdef FEAT_EVAL
1532 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001533 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001534#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001535#if defined(WIN32) && defined(FEAT_MBYTE)
1536 free_cmd_argsW();
1537#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001538
1539 mch_exit(exitval);
1540}
1541
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001542#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1543/*
1544 * Setup to use the current locale (for ctype() and many other things).
1545 */
1546 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001547init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001548{
1549 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001550
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001551# ifdef FEAT_GUI_GTK
1552 /* Tell Gtk not to change our locale settings. */
1553 gtk_disable_setlocale();
1554# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001555# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1556 /* Make sure strtod() uses a decimal point, not a comma. */
1557 setlocale(LC_NUMERIC, "C");
1558# endif
1559
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001560# ifdef WIN32
1561 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1562 * text while it's expecting text in the current locale. This call avoids
1563 * that. */
1564 setlocale(LC_CTYPE, "C");
1565# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001566
1567# ifdef FEAT_GETTEXT
1568 {
1569 int mustfree = FALSE;
1570 char_u *p;
1571
1572# ifdef DYNAMIC_GETTEXT
1573 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001574 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001575# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001576 /* expand_env() doesn't work yet, because g_chartab[] is not
1577 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001578 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1579 if (p != NULL && *p != NUL)
1580 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001581 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001582 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1583 }
1584 if (mustfree)
1585 vim_free(p);
1586 textdomain(VIMPACKAGE);
1587 }
1588# endif
1589}
1590#endif
1591
1592/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001593 * Get the name of the display, before gui_prepare() removes it from
1594 * argv[]. Used for the xterm-clipboard display.
1595 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001596 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001597 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001598 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001599early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001600{
Bram Moolenaar03005972008-11-20 13:12:36 +00001601#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1602 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001603 int argc = parmp->argc;
1604 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001605 int i;
1606
1607 for (i = 1; i < argc; i++)
1608 {
1609 if (STRCMP(argv[i], "--") == 0)
1610 break;
1611# ifdef FEAT_XCLIPBOARD
1612 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001613# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001614 || STRICMP(argv[i], "--display") == 0
1615# endif
1616 )
1617 {
1618 if (i == argc - 1)
1619 mainerr_arg_missing((char_u *)argv[i]);
1620 xterm_display = argv[++i];
1621 }
1622# endif
1623# ifdef FEAT_CLIENTSERVER
1624 else if (STRICMP(argv[i], "--servername") == 0)
1625 {
1626 if (i == argc - 1)
1627 mainerr_arg_missing((char_u *)argv[i]);
1628 parmp->serverName_arg = (char_u *)argv[++i];
1629 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001630 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001631 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001632 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001633 {
1634 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001635# ifdef FEAT_GUI
1636 if (strstr(argv[i], "-wait") != 0)
1637 /* don't fork() when starting the GUI to edit files ourself */
1638 gui.dofork = FALSE;
1639# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001640 }
1641# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001642
1643# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1644# ifdef FEAT_GUI_W32
1645 else if (STRICMP(argv[i], "--windowid") == 0)
1646# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001647 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001648# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001649 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001650 long_u id;
1651 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001652
1653 if (i == argc - 1)
1654 mainerr_arg_missing((char_u *)argv[i]);
1655 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001656 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001657 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001658 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001659 if (count != 1)
1660 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1661 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001662# ifdef FEAT_GUI_W32
1663 win_socket_id = id;
1664# else
1665 gtk_socket_id = id;
1666# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001667 i++;
1668 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001669# endif
1670# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001671 else if (STRICMP(argv[i], "--echo-wid") == 0)
1672 echo_wid_arg = TRUE;
1673# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001674# ifndef FEAT_NETBEANS_INTG
1675 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001676 {
1677 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1678 mch_exit(2);
1679 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001680# endif
1681
Bram Moolenaar58d98232005-07-23 22:25:46 +00001682 }
1683#endif
1684}
1685
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001686#ifndef NO_VIM_MAIN
1687/*
1688 * Get a (optional) count for a Vim argument.
1689 */
1690 static int
1691get_number_arg(
1692 char_u *p, /* pointer to argument */
1693 int *idx, /* index in argument, is incremented */
1694 int def) /* default value */
1695{
1696 if (vim_isdigit(p[*idx]))
1697 {
1698 def = atoi((char *)&(p[*idx]));
1699 while (vim_isdigit(p[*idx]))
1700 *idx = *idx + 1;
1701 }
1702 return def;
1703}
1704
1705/*
1706 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1707 * If the executable name starts with "r" we disable shell commands.
1708 * If the next character is "e" we run in Easy mode.
1709 * If the next character is "g" we run the GUI version.
1710 * If the next characters are "view" we start in readonly mode.
1711 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1712 * If the next characters are "ex" we start in Ex mode. If it's followed
1713 * by "im" use improved Ex mode.
1714 */
1715 static void
1716parse_command_name(mparm_T *parmp)
1717{
1718 char_u *initstr;
1719
1720 initstr = gettail((char_u *)parmp->argv[0]);
1721
1722#ifdef MACOS_X_UNIX
1723 /* An issue has been seen when launching Vim in such a way that
1724 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1725 * executable or a symbolic link of it. Until this issue is resolved
1726 * we prohibit the GUI from being used.
1727 */
1728 if (STRCMP(initstr, parmp->argv[0]) == 0)
1729 disallow_gui = TRUE;
1730
1731 /* TODO: On MacOS X default to gui if argv[0] ends in:
1732 * /Vim.app/Contents/MacOS/Vim */
1733#endif
1734
1735#ifdef FEAT_EVAL
1736 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001737 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001738#endif
1739
1740 if (TOLOWER_ASC(initstr[0]) == 'r')
1741 {
1742 restricted = TRUE;
1743 ++initstr;
1744 }
1745
1746 /* Use evim mode for "evim" and "egvim", not for "editor". */
1747 if (TOLOWER_ASC(initstr[0]) == 'e'
1748 && (TOLOWER_ASC(initstr[1]) == 'v'
1749 || TOLOWER_ASC(initstr[1]) == 'g'))
1750 {
1751#ifdef FEAT_GUI
1752 gui.starting = TRUE;
1753#endif
1754 parmp->evim_mode = TRUE;
1755 ++initstr;
1756 }
1757
1758 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1759 if (TOLOWER_ASC(initstr[0]) == 'g')
1760 {
1761 main_start_gui();
1762#ifdef FEAT_GUI
1763 ++initstr;
1764#endif
1765 }
1766
1767 if (STRNICMP(initstr, "view", 4) == 0)
1768 {
1769 readonlymode = TRUE;
1770 curbuf->b_p_ro = TRUE;
1771 p_uc = 10000; /* don't update very often */
1772 initstr += 4;
1773 }
1774 else if (STRNICMP(initstr, "vim", 3) == 0)
1775 initstr += 3;
1776
1777 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1778 if (STRICMP(initstr, "diff") == 0)
1779 {
1780#ifdef FEAT_DIFF
1781 parmp->diff_mode = TRUE;
1782#else
1783 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1784 mch_errmsg("\n");
1785 mch_exit(2);
1786#endif
1787 }
1788
1789 if (STRNICMP(initstr, "ex", 2) == 0)
1790 {
1791 if (STRNICMP(initstr + 2, "im", 2) == 0)
1792 exmode_active = EXMODE_VIM;
1793 else
1794 exmode_active = EXMODE_NORMAL;
1795 change_compatible(TRUE); /* set 'compatible' */
1796 }
1797}
1798
Bram Moolenaar58d98232005-07-23 22:25:46 +00001799/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001800 * Scan the command line arguments.
1801 */
1802 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001803command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001804{
1805 int argc = parmp->argc;
1806 char **argv = parmp->argv;
1807 int argv_idx; /* index in argv[n][] */
1808 int had_minmin = FALSE; /* found "--" argument */
1809 int want_argument; /* option argument with argument */
1810 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001811 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001812 long n;
1813
1814 --argc;
1815 ++argv;
1816 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1817 while (argc > 0)
1818 {
1819 /*
1820 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1821 */
1822 if (argv[0][0] == '+' && !had_minmin)
1823 {
1824 if (parmp->n_commands >= MAX_ARG_CMDS)
1825 mainerr(ME_EXTRA_CMD, NULL);
1826 argv_idx = -1; /* skip to next argument */
1827 if (argv[0][1] == NUL)
1828 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1829 else
1830 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1831 }
1832
1833 /*
1834 * Optional argument.
1835 */
1836 else if (argv[0][0] == '-' && !had_minmin)
1837 {
1838 want_argument = FALSE;
1839 c = argv[0][argv_idx++];
1840#ifdef VMS
1841 /*
1842 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1843 * and "-/X" as "-X".
1844 */
1845 if (c == '/')
1846 {
1847 c = argv[0][argv_idx++];
1848 c = TOUPPER_ASC(c);
1849 }
1850 else
1851 c = TOLOWER_ASC(c);
1852#endif
1853 switch (c)
1854 {
1855 case NUL: /* "vim -" read from stdin */
1856 /* "ex -" silent mode */
1857 if (exmode_active)
1858 silent_mode = TRUE;
1859 else
1860 {
1861 if (parmp->edit_type != EDIT_NONE)
1862 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1863 parmp->edit_type = EDIT_STDIN;
1864 read_cmd_fd = 2; /* read from stderr instead of stdin */
1865 }
1866 argv_idx = -1; /* skip to next argument */
1867 break;
1868
1869 case '-': /* "--" don't take any more option arguments */
1870 /* "--help" give help message */
1871 /* "--version" give version message */
1872 /* "--literal" take files literally */
1873 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001874 /* "--not-a-term" don't warn for not a term */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001875 /* "--ttyfail" exit if not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001876 /* "--noplugin[s]" skip plugins */
1877 /* "--cmd <cmd>" execute cmd before vimrc */
1878 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1879 usage();
1880 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1881 {
1882 Columns = 80; /* need to init Columns */
1883 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1884 list_version();
1885 msg_putchar('\n');
1886 msg_didout = FALSE;
1887 mch_exit(0);
1888 }
1889 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1890 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001891#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001892 parmp->literal = TRUE;
1893#endif
1894 }
1895 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1896 {
1897#ifdef FEAT_GUI
1898 gui.dofork = FALSE; /* don't fork() when starting GUI */
1899#endif
1900 }
1901 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1902 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001903 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1904 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001905 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
1906 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001907 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1908 {
1909 want_argument = TRUE;
1910 argv_idx += 3;
1911 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001912 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1913 {
1914 want_argument = TRUE;
1915 argv_idx += 11;
1916 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001917#ifdef FEAT_CLIENTSERVER
1918 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1919 ; /* already processed -- no arg */
1920 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1921 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1922 {
1923 /* already processed -- snatch the following arg */
1924 if (argc > 1)
1925 {
1926 --argc;
1927 ++argv;
1928 }
1929 }
1930#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001931#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1932# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001933 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001934# else
1935 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1936# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001937 {
1938 /* already processed -- snatch the following arg */
1939 if (argc > 1)
1940 {
1941 --argc;
1942 ++argv;
1943 }
1944 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001945#endif
1946#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001947 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1948 {
1949 /* already processed, skip */
1950 }
1951#endif
1952 else
1953 {
1954 if (argv[0][argv_idx])
1955 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1956 had_minmin = TRUE;
1957 }
1958 if (!want_argument)
1959 argv_idx = -1; /* skip to next argument */
1960 break;
1961
1962 case 'A': /* "-A" start in Arabic mode */
1963#ifdef FEAT_ARABIC
1964 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1965#else
1966 mch_errmsg(_(e_noarabic));
1967 mch_exit(2);
1968#endif
1969 break;
1970
1971 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001972 /* Needs to be effective before expanding file names, because
1973 * for Win32 this makes us edit a shortcut file itself,
1974 * instead of the file it links to. */
1975 set_options_bin(curbuf->b_p_bin, 1, 0);
1976 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001977 break;
1978
1979 case 'C': /* "-C" Compatible */
1980 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02001981 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001982 break;
1983
1984 case 'e': /* "-e" Ex mode */
1985 exmode_active = EXMODE_NORMAL;
1986 break;
1987
1988 case 'E': /* "-E" Improved Ex mode */
1989 exmode_active = EXMODE_VIM;
1990 break;
1991
1992 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1993 window directly, not with newcli */
1994#ifdef FEAT_GUI
1995 gui.dofork = FALSE; /* don't fork() when starting GUI */
1996#endif
1997 break;
1998
1999 case 'g': /* "-g" start GUI */
2000 main_start_gui();
2001 break;
2002
2003 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
2004#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002005 p_fkmap = TRUE;
2006 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002007#else
2008 mch_errmsg(_(e_nofarsi));
2009 mch_exit(2);
2010#endif
2011 break;
2012
2013 case 'h': /* "-h" give help message */
2014#ifdef FEAT_GUI_GNOME
2015 /* Tell usage() to exit for "gvim". */
2016 gui.starting = FALSE;
2017#endif
2018 usage();
2019 break;
2020
2021 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2022#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002023 p_hkmap = TRUE;
2024 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002025#else
2026 mch_errmsg(_(e_nohebrew));
2027 mch_exit(2);
2028#endif
2029 break;
2030
2031 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2032#ifdef FEAT_LISP
2033 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2034 p_sm = TRUE;
2035#endif
2036 break;
2037
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002038 case 'M': /* "-M" no changes or writing of files */
2039 reset_modifiable();
2040 /* FALLTHROUGH */
2041
2042 case 'm': /* "-m" no writing of files */
2043 p_write = FALSE;
2044 break;
2045
2046 case 'y': /* "-y" easy mode */
2047#ifdef FEAT_GUI
2048 gui.starting = TRUE; /* start GUI a bit later */
2049#endif
2050 parmp->evim_mode = TRUE;
2051 break;
2052
2053 case 'N': /* "-N" Nocompatible */
2054 change_compatible(FALSE);
2055 break;
2056
2057 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002058#ifdef FEAT_NETBEANS_INTG
2059 /* checking for "-nb", netbeans parameters */
2060 if (argv[0][argv_idx] == 'b')
2061 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002062 netbeansArg = argv[0];
2063 argv_idx = -1; /* skip to next argument */
2064 }
2065 else
2066#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002067 parmp->no_swap_file = TRUE;
2068 break;
2069
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002070 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002071#ifdef TARGET_API_MAC_OSX
2072 /* For some reason on MacOS X, an argument like:
2073 -psn_0_10223617 is passed in when invoke from Finder
2074 or with the 'open' command */
2075 if (argv[0][argv_idx] == 's')
2076 {
2077 argv_idx = -1; /* bypass full -psn */
2078 main_start_gui();
2079 break;
2080 }
2081#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002082#ifdef FEAT_WINDOWS
2083 /* default is 0: open window for each file */
2084 parmp->window_count = get_number_arg((char_u *)argv[0],
2085 &argv_idx, 0);
2086 parmp->window_layout = WIN_TABS;
2087#endif
2088 break;
2089
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002090 case 'o': /* "-o[N]" open N horizontal split windows */
2091#ifdef FEAT_WINDOWS
2092 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002093 parmp->window_count = get_number_arg((char_u *)argv[0],
2094 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002095 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002096#endif
2097 break;
2098
2099 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002100#ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002101 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002102 parmp->window_count = get_number_arg((char_u *)argv[0],
2103 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002104 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002105#endif
2106 break;
2107
2108#ifdef FEAT_QUICKFIX
2109 case 'q': /* "-q" QuickFix mode */
2110 if (parmp->edit_type != EDIT_NONE)
2111 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2112 parmp->edit_type = EDIT_QF;
2113 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2114 {
2115 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2116 argv_idx = -1;
2117 }
2118 else if (argc > 1) /* "-q {errorfile}" */
2119 want_argument = TRUE;
2120 break;
2121#endif
2122
2123 case 'R': /* "-R" readonly mode */
2124 readonlymode = TRUE;
2125 curbuf->b_p_ro = TRUE;
2126 p_uc = 10000; /* don't update very often */
2127 break;
2128
2129 case 'r': /* "-r" recovery mode */
2130 case 'L': /* "-L" recovery mode */
2131 recoverymode = 1;
2132 break;
2133
2134 case 's':
2135 if (exmode_active) /* "-s" silent (batch) mode */
2136 silent_mode = TRUE;
2137 else /* "-s {scriptin}" read from script file */
2138 want_argument = TRUE;
2139 break;
2140
2141 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2142 if (parmp->edit_type != EDIT_NONE)
2143 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2144 parmp->edit_type = EDIT_TAG;
2145 if (argv[0][argv_idx]) /* "-t{tag}" */
2146 {
2147 parmp->tagname = (char_u *)argv[0] + argv_idx;
2148 argv_idx = -1;
2149 }
2150 else /* "-t {tag}" */
2151 want_argument = TRUE;
2152 break;
2153
2154#ifdef FEAT_EVAL
2155 case 'D': /* "-D" Debugging */
2156 parmp->use_debug_break_level = 9999;
2157 break;
2158#endif
2159#ifdef FEAT_DIFF
2160 case 'd': /* "-d" 'diff' */
2161# ifdef AMIGA
2162 /* check for "-dev {device}" */
2163 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2164 want_argument = TRUE;
2165 else
2166# endif
2167 parmp->diff_mode = TRUE;
2168 break;
2169#endif
2170 case 'V': /* "-V{N}" Verbose level */
2171 /* default is 10: a little bit verbose */
2172 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2173 if (argv[0][argv_idx] != NUL)
2174 {
2175 set_option_value((char_u *)"verbosefile", 0L,
2176 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002177 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002178 }
2179 break;
2180
2181 case 'v': /* "-v" Vi-mode (as if called "vi") */
2182 exmode_active = 0;
2183#ifdef FEAT_GUI
2184 gui.starting = FALSE; /* don't start GUI */
2185#endif
2186 break;
2187
2188 case 'w': /* "-w{number}" set window height */
2189 /* "-w {scriptout}" write to script */
2190 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2191 {
2192 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2193 set_option_value((char_u *)"window", n, NULL, 0);
2194 break;
2195 }
2196 want_argument = TRUE;
2197 break;
2198
2199#ifdef FEAT_CRYPT
2200 case 'x': /* "-x" encrypted reading/writing of files */
2201 parmp->ask_for_key = TRUE;
2202 break;
2203#endif
2204
2205 case 'X': /* "-X" don't connect to X server */
2206#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2207 x_no_connect = TRUE;
2208#endif
2209 break;
2210
2211 case 'Z': /* "-Z" restricted mode */
2212 restricted = TRUE;
2213 break;
2214
2215 case 'c': /* "-c{command}" or "-c {command}" execute
2216 command */
2217 if (argv[0][argv_idx] != NUL)
2218 {
2219 if (parmp->n_commands >= MAX_ARG_CMDS)
2220 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002221 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2222 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002223 argv_idx = -1;
2224 break;
2225 }
2226 /*FALLTHROUGH*/
2227 case 'S': /* "-S {file}" execute Vim script */
2228 case 'i': /* "-i {viminfo}" use for viminfo */
2229#ifndef FEAT_DIFF
2230 case 'd': /* "-d {device}" device (for Amiga) */
2231#endif
2232 case 'T': /* "-T {terminal}" terminal name */
2233 case 'u': /* "-u {vimrc}" vim inits file */
2234 case 'U': /* "-U {gvimrc}" gvim inits file */
2235 case 'W': /* "-W {scriptout}" overwrite */
2236#ifdef FEAT_GUI_W32
2237 case 'P': /* "-P {parent title}" MDI parent */
2238#endif
2239 want_argument = TRUE;
2240 break;
2241
2242 default:
2243 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2244 }
2245
2246 /*
2247 * Handle option arguments with argument.
2248 */
2249 if (want_argument)
2250 {
2251 /*
2252 * Check for garbage immediately after the option letter.
2253 */
2254 if (argv[0][argv_idx] != NUL)
2255 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2256
2257 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002258 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002259 mainerr_arg_missing((char_u *)argv[0]);
2260 ++argv;
2261 argv_idx = -1;
2262
2263 switch (c)
2264 {
2265 case 'c': /* "-c {command}" execute command */
2266 case 'S': /* "-S {file}" execute Vim script */
2267 if (parmp->n_commands >= MAX_ARG_CMDS)
2268 mainerr(ME_EXTRA_CMD, NULL);
2269 if (c == 'S')
2270 {
2271 char *a;
2272
2273 if (argc < 1)
2274 /* "-S" without argument: use default session file
2275 * name. */
2276 a = SESSION_FILE;
2277 else if (argv[0][0] == '-')
2278 {
2279 /* "-S" followed by another option: use default
2280 * session file name. */
2281 a = SESSION_FILE;
2282 ++argc;
2283 --argv;
2284 }
2285 else
2286 a = argv[0];
2287 p = alloc((unsigned)(STRLEN(a) + 4));
2288 if (p == NULL)
2289 mch_exit(2);
2290 sprintf((char *)p, "so %s", a);
2291 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2292 parmp->commands[parmp->n_commands++] = p;
2293 }
2294 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002295 parmp->commands[parmp->n_commands++] =
2296 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002297 break;
2298
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002299 case '-':
2300 if (argv[-1][2] == 'c')
2301 {
2302 /* "--cmd {command}" execute command */
2303 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2304 mainerr(ME_EXTRA_CMD, NULL);
2305 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002306 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002307 }
2308 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002309 break;
2310
2311 /* case 'd': -d {device} is handled in mch_check_win() for the
2312 * Amiga */
2313
2314#ifdef FEAT_QUICKFIX
2315 case 'q': /* "-q {errorfile}" QuickFix mode */
2316 parmp->use_ef = (char_u *)argv[0];
2317 break;
2318#endif
2319
2320 case 'i': /* "-i {viminfo}" use for viminfo */
2321 use_viminfo = (char_u *)argv[0];
2322 break;
2323
2324 case 's': /* "-s {scriptin}" read from script file */
2325 if (scriptin[0] != NULL)
2326 {
2327scripterror:
2328 mch_errmsg(_("Attempt to open script file again: \""));
2329 mch_errmsg(argv[-1]);
2330 mch_errmsg(" ");
2331 mch_errmsg(argv[0]);
2332 mch_errmsg("\"\n");
2333 mch_exit(2);
2334 }
2335 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2336 {
2337 mch_errmsg(_("Cannot open for reading: \""));
2338 mch_errmsg(argv[0]);
2339 mch_errmsg("\"\n");
2340 mch_exit(2);
2341 }
2342 if (save_typebuf() == FAIL)
2343 mch_exit(2); /* out of memory */
2344 break;
2345
2346 case 't': /* "-t {tag}" */
2347 parmp->tagname = (char_u *)argv[0];
2348 break;
2349
2350 case 'T': /* "-T {terminal}" terminal name */
2351 /*
2352 * The -T term argument is always available and when
2353 * HAVE_TERMLIB is supported it overrides the environment
2354 * variable TERM.
2355 */
2356#ifdef FEAT_GUI
2357 if (term_is_gui((char_u *)argv[0]))
2358 gui.starting = TRUE; /* start GUI a bit later */
2359 else
2360#endif
2361 parmp->term = (char_u *)argv[0];
2362 break;
2363
2364 case 'u': /* "-u {vimrc}" vim inits file */
2365 parmp->use_vimrc = (char_u *)argv[0];
2366 break;
2367
2368 case 'U': /* "-U {gvimrc}" gvim inits file */
2369#ifdef FEAT_GUI
2370 use_gvimrc = (char_u *)argv[0];
2371#endif
2372 break;
2373
2374 case 'w': /* "-w {nr}" 'window' value */
2375 /* "-w {scriptout}" append to script file */
2376 if (vim_isdigit(*((char_u *)argv[0])))
2377 {
2378 argv_idx = 0;
2379 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2380 set_option_value((char_u *)"window", n, NULL, 0);
2381 argv_idx = -1;
2382 break;
2383 }
2384 /*FALLTHROUGH*/
2385 case 'W': /* "-W {scriptout}" overwrite script file */
2386 if (scriptout != NULL)
2387 goto scripterror;
2388 if ((scriptout = mch_fopen(argv[0],
2389 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2390 {
2391 mch_errmsg(_("Cannot open for script output: \""));
2392 mch_errmsg(argv[0]);
2393 mch_errmsg("\"\n");
2394 mch_exit(2);
2395 }
2396 break;
2397
2398#ifdef FEAT_GUI_W32
2399 case 'P': /* "-P {parent title}" MDI parent */
2400 gui_mch_set_parent(argv[0]);
2401 break;
2402#endif
2403 }
2404 }
2405 }
2406
2407 /*
2408 * File name argument.
2409 */
2410 else
2411 {
2412 argv_idx = -1; /* skip to next argument */
2413
2414 /* Check for only one type of editing. */
2415 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2416 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2417 parmp->edit_type = EDIT_FILE;
2418
2419#ifdef MSWIN
2420 /* Remember if the argument was a full path before changing
2421 * slashes to backslashes. */
2422 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2423 parmp->full_path = TRUE;
2424#endif
2425
2426 /* Add the file to the global argument list. */
2427 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2428 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2429 mch_exit(2);
2430#ifdef FEAT_DIFF
2431 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2432 && !mch_isdir(alist_name(&GARGLIST[0])))
2433 {
2434 char_u *r;
2435
2436 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2437 if (r != NULL)
2438 {
2439 vim_free(p);
2440 p = r;
2441 }
2442 }
2443#endif
2444#if defined(__CYGWIN32__) && !defined(WIN32)
2445 /*
2446 * If vim is invoked by non-Cygwin tools, convert away any
2447 * DOS paths, so things like .swp files are created correctly.
2448 * Look for evidence of non-Cygwin paths before we bother.
2449 * This is only for when using the Unix files.
2450 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002451 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002452 {
2453 char posix_path[PATH_MAX];
2454
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002455# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2456 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2457# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002458 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002459# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002460 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002461 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002462 if (p == NULL)
2463 mch_exit(2);
2464 }
2465#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002466
2467#ifdef USE_FNAME_CASE
2468 /* Make the case of the file name match the actual file. */
2469 fname_case(p, 0);
2470#endif
2471
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002472 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002473#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002474 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002475#else
2476 2 /* add buffer number now and use curbuf */
2477#endif
2478 );
2479
2480#if defined(FEAT_MBYTE) && defined(WIN32)
2481 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002482 /* Remember this argument has been added to the argument list.
2483 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002484 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002485# ifdef FEAT_DIFF
2486 parmp->diff_mode
2487# else
2488 FALSE
2489# endif
2490 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002491 }
2492#endif
2493 }
2494
2495 /*
2496 * If there are no more letters after the current "-", go to next
2497 * argument. argv_idx is set to -1 when the current argument is to be
2498 * skipped.
2499 */
2500 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2501 {
2502 --argc;
2503 ++argv;
2504 argv_idx = 1;
2505 }
2506 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002507
2508#ifdef FEAT_EVAL
2509 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2510 * one. */
2511 if (parmp->n_commands > 0)
2512 {
2513 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2514 if (p != NULL)
2515 {
2516 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2517 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2518 vim_free(p);
2519 }
2520 }
2521#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002522}
2523
2524/*
2525 * Print a warning if stdout is not a terminal.
2526 * When starting in Ex mode and commands come from a file, set Silent mode.
2527 */
2528 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002529check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002530{
2531 int input_isatty; /* is active input a terminal? */
2532
2533 input_isatty = mch_input_isatty();
2534 if (exmode_active)
2535 {
2536 if (!input_isatty)
2537 silent_mode = TRUE;
2538 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002539 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002540#ifdef FEAT_GUI
2541 /* don't want the delay when started from the desktop */
2542 && !gui.starting
2543#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002544 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002545 {
2546#ifdef NBDEBUG
2547 /*
2548 * This shouldn't be necessary. But if I run netbeans with the log
2549 * output coming to the console and XOpenDisplay fails, I get vim
2550 * trying to start with input/output to my console tty. This fills my
2551 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002552 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002553 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002554 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002555 {
2556 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2557 exit(1);
2558 }
2559#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002560#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2561 if (is_cygpty_used())
2562 {
2563 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2564 exit(1);
2565 }
2566#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002567 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002568 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2569 if (!input_isatty)
2570 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2571 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002572 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2573 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002574 if (scriptin[0] == NULL)
2575 ui_delay(2000L, TRUE);
2576 TIME_MSG("Warning delay");
2577 }
2578}
2579
2580/*
2581 * Read text from stdin.
2582 */
2583 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002584read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002585{
2586 int i;
2587
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002588#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002589 /* When getting the ATTENTION prompt here, use a dialog */
2590 swap_exists_action = SEA_DIALOG;
2591#endif
2592 no_wait_return = TRUE;
2593 i = msg_didany;
2594 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002595 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002596 no_wait_return = FALSE;
2597 msg_didany = i;
2598 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002599#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002600 check_swap_exists_action();
2601#endif
2602#if !(defined(AMIGA) || defined(MACOS))
2603 /*
2604 * Close stdin and dup it from stderr. Required for GPM to work
2605 * properly, and for running external commands.
2606 * Is there any other system that cannot do this?
2607 */
2608 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002609 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002610#endif
2611}
2612
2613/*
2614 * Create the requested number of windows and edit buffers in them.
2615 * Also does recovery if "recoverymode" set.
2616 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002617 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002618create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002619{
2620#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002621 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002622 int done = 0;
2623
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002624 /*
2625 * Create the number of windows that was requested.
2626 */
2627 if (parmp->window_count == -1) /* was not set */
2628 parmp->window_count = 1;
2629 if (parmp->window_count == 0)
2630 parmp->window_count = GARGCOUNT;
2631 if (parmp->window_count > 1)
2632 {
2633 /* Don't change the windows if there was a command in .vimrc that
2634 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002635 if (parmp->window_layout == 0)
2636 parmp->window_layout = WIN_HOR;
2637 if (parmp->window_layout == WIN_TABS)
2638 {
2639 parmp->window_count = make_tabpages(parmp->window_count);
2640 TIME_MSG("making tab pages");
2641 }
2642 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002643 {
2644 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002645 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002646 TIME_MSG("making windows");
2647 }
2648 else
2649 parmp->window_count = win_count();
2650 }
2651 else
2652 parmp->window_count = 1;
2653#endif
2654
2655 if (recoverymode) /* do recover */
2656 {
2657 msg_scroll = TRUE; /* scroll message up */
2658 ml_recover();
2659 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2660 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002661 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002662 }
2663 else
2664 {
2665 /*
2666 * Open a buffer for windows that don't have one yet.
2667 * Commands in the .vimrc might have loaded a file or split the window.
2668 * Watch out for autocommands that delete a window.
2669 */
2670#ifdef FEAT_AUTOCMD
2671 /*
2672 * Don't execute Win/Buf Enter/Leave autocommands here
2673 */
2674 ++autocmd_no_enter;
2675 ++autocmd_no_leave;
2676#endif
2677#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002678 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002679 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002680 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002681 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002682 {
2683 if (parmp->window_layout == WIN_TABS)
2684 goto_tabpage(1);
2685 else
2686 curwin = firstwin;
2687 }
2688 else if (parmp->window_layout == WIN_TABS)
2689 {
2690 if (curtab->tp_next == NULL)
2691 break;
2692 goto_tabpage(0);
2693 }
2694 else
2695 {
2696 if (curwin->w_next == NULL)
2697 break;
2698 curwin = curwin->w_next;
2699 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002700 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002701#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002702 curbuf = curwin->w_buffer;
2703 if (curbuf->b_ml.ml_mfp == NULL)
2704 {
2705#ifdef FEAT_FOLDING
2706 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2707 if (p_fdls >= 0)
2708 curwin->w_p_fdl = p_fdls;
2709#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002710#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002711 /* When getting the ATTENTION prompt here, use a dialog */
2712 swap_exists_action = SEA_DIALOG;
2713#endif
2714 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002715
2716 /* create memfile, read file */
2717 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002718
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002719#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002720 if (swap_exists_action == SEA_QUIT)
2721 {
2722 if (got_int || only_one_window())
2723 {
2724 /* abort selected or quit and only one window */
2725 did_emsg = FALSE; /* avoid hit-enter prompt */
2726 getout(1);
2727 }
2728 /* We can't close the window, it would disturb what
2729 * happens next. Clear the file name and set the arg
2730 * index to -1 to delete it later. */
2731 setfname(curbuf, NULL, NULL, FALSE);
2732 curwin->w_arg_idx = -1;
2733 swap_exists_action = SEA_NONE;
2734 }
2735 else
2736 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002737#endif
2738#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002739 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002740#endif
2741 }
2742#ifdef FEAT_WINDOWS
2743 ui_breakcheck();
2744 if (got_int)
2745 {
2746 (void)vgetc(); /* only break the file loading, not the rest */
2747 break;
2748 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002749 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002750#endif
2751#ifdef FEAT_WINDOWS
2752 if (parmp->window_layout == WIN_TABS)
2753 goto_tabpage(1);
2754 else
2755 curwin = firstwin;
2756 curbuf = curwin->w_buffer;
2757#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002758#ifdef FEAT_AUTOCMD
2759 --autocmd_no_enter;
2760 --autocmd_no_leave;
2761#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002762 }
2763}
2764
2765#ifdef FEAT_WINDOWS
2766 /*
2767 * If opened more than one window, start editing files in the other
2768 * windows. make_windows() has already opened the windows.
2769 */
2770 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002771edit_buffers(
2772 mparm_T *parmp,
2773 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002774{
2775 int arg_idx; /* index in argument list */
2776 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002777 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002778 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002779
2780# ifdef FEAT_AUTOCMD
2781 /*
2782 * Don't execute Win/Buf Enter/Leave autocommands here
2783 */
2784 ++autocmd_no_enter;
2785 ++autocmd_no_leave;
2786# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002787
2788 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2789 if (curwin->w_arg_idx == -1)
2790 {
2791 win_close(curwin, TRUE);
2792 advance = FALSE;
2793 }
2794
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002795 arg_idx = 1;
2796 for (i = 1; i < parmp->window_count; ++i)
2797 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002798 if (cwd != NULL)
2799 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002800 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2801 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002802 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002803 ++arg_idx;
2804 win_close(curwin, TRUE);
2805 advance = FALSE;
2806 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002807 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002808
Bram Moolenaar84212822006-11-07 21:59:47 +00002809 if (advance)
2810 {
2811 if (parmp->window_layout == WIN_TABS)
2812 {
2813 if (curtab->tp_next == NULL) /* just checking */
2814 break;
2815 goto_tabpage(0);
2816 }
2817 else
2818 {
2819 if (curwin->w_next == NULL) /* just checking */
2820 break;
2821 win_enter(curwin->w_next, FALSE);
2822 }
2823 }
2824 advance = TRUE;
2825
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002826 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002827 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002828 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2829 {
2830 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002831 /* Edit file from arg list, if there is one. When "Quit" selected
2832 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002833# ifdef HAS_SWAP_EXISTS_ACTION
2834 swap_exists_did_quit = FALSE;
2835# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002836 (void)do_ecmd(0, arg_idx < GARGCOUNT
2837 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002838 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002839# ifdef HAS_SWAP_EXISTS_ACTION
2840 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002841 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002842 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002843 if (got_int || only_one_window())
2844 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002845 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002846 did_emsg = FALSE; /* avoid hit-enter prompt */
2847 getout(1);
2848 }
2849 win_close(curwin, TRUE);
2850 advance = FALSE;
2851 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002852# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002853 if (arg_idx == GARGCOUNT - 1)
2854 arg_had_last = TRUE;
2855 ++arg_idx;
2856 }
2857 ui_breakcheck();
2858 if (got_int)
2859 {
2860 (void)vgetc(); /* only break the file loading, not the rest */
2861 break;
2862 }
2863 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002864
2865 if (parmp->window_layout == WIN_TABS)
2866 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002867# ifdef FEAT_AUTOCMD
2868 --autocmd_no_enter;
2869# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002870
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002871 /* make the first window the current window */
2872 win = firstwin;
2873#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2874 /* Avoid making a preview window the current window. */
2875 while (win->w_p_pvw)
2876 {
2877 win = win->w_next;
2878 if (win == NULL)
2879 {
2880 win = firstwin;
2881 break;
2882 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002883 }
2884#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002885 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002886
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002887# ifdef FEAT_AUTOCMD
2888 --autocmd_no_leave;
2889# endif
2890 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002891 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002892 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2893}
2894#endif /* FEAT_WINDOWS */
2895
2896/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002897 * Execute the commands from --cmd arguments "cmds[cnt]".
2898 */
2899 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002900exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002901{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002902 char_u **cmds = parmp->pre_commands;
2903 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002904 int i;
2905
2906 if (cnt > 0)
2907 {
2908 curwin->w_cursor.lnum = 0; /* just in case.. */
2909 sourcing_name = (char_u *)_("pre-vimrc command line");
2910# ifdef FEAT_EVAL
2911 current_SID = SID_CMDARG;
2912# endif
2913 for (i = 0; i < cnt; ++i)
2914 do_cmdline_cmd(cmds[i]);
2915 sourcing_name = NULL;
2916# ifdef FEAT_EVAL
2917 current_SID = 0;
2918# endif
2919 TIME_MSG("--cmd commands");
2920 }
2921}
2922
2923/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002924 * Execute "+", "-c" and "-S" arguments.
2925 */
2926 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002927exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002928{
2929 int i;
2930
2931 /*
2932 * We start commands on line 0, make "vim +/pat file" match a
2933 * pattern on line 1. But don't move the cursor when an autocommand
2934 * with g`" was used.
2935 */
2936 msg_scroll = TRUE;
2937 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2938 curwin->w_cursor.lnum = 0;
2939 sourcing_name = (char_u *)"command line";
2940#ifdef FEAT_EVAL
2941 current_SID = SID_CARG;
2942#endif
2943 for (i = 0; i < parmp->n_commands; ++i)
2944 {
2945 do_cmdline_cmd(parmp->commands[i]);
2946 if (parmp->cmds_tofree[i])
2947 vim_free(parmp->commands[i]);
2948 }
2949 sourcing_name = NULL;
2950#ifdef FEAT_EVAL
2951 current_SID = 0;
2952#endif
2953 if (curwin->w_cursor.lnum == 0)
2954 curwin->w_cursor.lnum = 1;
2955
2956 if (!exmode_active)
2957 msg_scroll = FALSE;
2958
2959#ifdef FEAT_QUICKFIX
2960 /* When started with "-q errorfile" jump to first error again. */
2961 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002962 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002963#endif
2964 TIME_MSG("executing command arguments");
2965}
2966
2967/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002968 * Source startup scripts.
2969 */
2970 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002971source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002972{
2973 int i;
2974
2975 /*
2976 * For "evim" source evim.vim first of all, so that the user can overrule
2977 * any things he doesn't like.
2978 */
2979 if (parmp->evim_mode)
2980 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002981 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002982 TIME_MSG("source evim file");
2983 }
2984
2985 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002986 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002987 * nothing else.
2988 */
2989 if (parmp->use_vimrc != NULL)
2990 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002991 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2992 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002993 {
2994#ifdef FEAT_GUI
2995 if (use_gvimrc == NULL) /* don't load gvimrc either */
2996 use_gvimrc = parmp->use_vimrc;
2997#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002998 }
2999 else
3000 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003001 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003002 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
3003 }
3004 }
3005 else if (!silent_mode)
3006 {
3007#ifdef AMIGA
3008 struct Process *proc = (struct Process *)FindTask(0L);
3009 APTR save_winptr = proc->pr_WindowPtr;
3010
3011 /* Avoid a requester here for a volume that doesn't exist. */
3012 proc->pr_WindowPtr = (APTR)-1L;
3013#endif
3014
3015 /*
3016 * Get system wide defaults, if the file name is defined.
3017 */
3018#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003019 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003020#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003021#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003022 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003023#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003024
3025 /*
3026 * Try to read initialization commands from the following places:
3027 * - environment variable VIMINIT
3028 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3029 * - second user vimrc file ($VIM/.vimrc for Dos)
3030 * - environment variable EXINIT
3031 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3032 * - second user exrc file ($VIM/.exrc for Dos)
3033 * The first that exists is used, the rest is ignored.
3034 */
3035 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3036 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003037 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003038#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003039 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3040 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003041#endif
3042#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003043 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3044 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003045#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003046#ifdef USR_VIMRC_FILE4
3047 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3048 DOSO_VIMRC) == FAIL
3049#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003050 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003051 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003052#ifdef USR_EXRC_FILE2
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003053 && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003054#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003055 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003056 {
3057 /* When no .vimrc file was found: source defaults.vim. */
3058 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003059 }
3060 }
3061
3062 /*
3063 * Read initialization commands from ".vimrc" or ".exrc" in current
3064 * directory. This is only done if the 'exrc' option is set.
3065 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003066 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003067 * option has been reset in environment of global ".exrc" or ".vimrc".
3068 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3069 * SYS_VIMRC_FILE.
3070 */
3071 if (p_exrc)
3072 {
3073#if defined(UNIX) || defined(VMS)
3074 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3075 if (!file_owned(VIMRC_FILE))
3076#endif
3077 secure = p_secure;
3078
3079 i = FAIL;
3080 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3081 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3082#ifdef USR_VIMRC_FILE2
3083 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3084 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3085#endif
3086#ifdef USR_VIMRC_FILE3
3087 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3088 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3089#endif
3090#ifdef SYS_VIMRC_FILE
3091 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3092 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3093#endif
3094 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003095 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003096
3097 if (i == FAIL)
3098 {
3099#if defined(UNIX) || defined(VMS)
3100 /* if ".exrc" is not owned by user set 'secure' mode */
3101 if (!file_owned(EXRC_FILE))
3102 secure = p_secure;
3103 else
3104 secure = 0;
3105#endif
3106 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3107 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3108#ifdef USR_EXRC_FILE2
3109 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3110 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3111#endif
3112 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003113 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003114 }
3115 }
3116 if (secure == 2)
3117 need_wait_return = TRUE;
3118 secure = 0;
3119#ifdef AMIGA
3120 proc->pr_WindowPtr = save_winptr;
3121#endif
3122 }
3123 TIME_MSG("sourcing vimrc file(s)");
3124}
3125
3126/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003127 * Setup to start using the GUI. Exit with an error when not available.
3128 */
3129 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003130main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003131{
3132#ifdef FEAT_GUI
3133 gui.starting = TRUE; /* start GUI a bit later */
3134#else
3135 mch_errmsg(_(e_nogvim));
3136 mch_errmsg("\n");
3137 mch_exit(2);
3138#endif
3139}
3140
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003141#endif /* NO_VIM_MAIN */
3142
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003143/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003144 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003145 * Returns FAIL if the environment variable was not executed, OK otherwise.
3146 */
3147 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003148process_env(
3149 char_u *env,
3150 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003151{
3152 char_u *initstr;
3153 char_u *save_sourcing_name;
3154 linenr_T save_sourcing_lnum;
3155#ifdef FEAT_EVAL
3156 scid_T save_sid;
3157#endif
3158
3159 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3160 {
3161 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003162 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003163 save_sourcing_name = sourcing_name;
3164 save_sourcing_lnum = sourcing_lnum;
3165 sourcing_name = env;
3166 sourcing_lnum = 0;
3167#ifdef FEAT_EVAL
3168 save_sid = current_SID;
3169 current_SID = SID_ENV;
3170#endif
3171 do_cmdline_cmd(initstr);
3172 sourcing_name = save_sourcing_name;
3173 sourcing_lnum = save_sourcing_lnum;
3174#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003175 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003176#endif
3177 return OK;
3178 }
3179 return FAIL;
3180}
3181
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003182#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003183/*
3184 * Return TRUE if we are certain the user owns the file "fname".
3185 * Used for ".vimrc" and ".exrc".
3186 * Use both stat() and lstat() for extra security.
3187 */
3188 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003189file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003190{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003191 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003192# ifdef UNIX
3193 uid_t uid = getuid();
3194# else /* VMS */
3195 uid_t uid = ((getgid() << 16) | getuid());
3196# endif
3197
3198 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3199# ifdef HAVE_LSTAT
3200 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3201# endif
3202 );
3203}
3204#endif
3205
3206/*
3207 * Give an error message main_errors["n"] and exit.
3208 */
3209 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003210mainerr(
3211 int n, /* one of the ME_ defines */
3212 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003213{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003214#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003215 reset_signals(); /* kill us with CTRL-C here, if you like */
3216#endif
3217
3218 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003219 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003220 mch_errmsg(_(main_errors[n]));
3221 if (str != NULL)
3222 {
3223 mch_errmsg(": \"");
3224 mch_errmsg((char *)str);
3225 mch_errmsg("\"");
3226 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003227 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003228
3229 mch_exit(1);
3230}
3231
3232 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003233mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003234{
3235 mainerr(ME_ARG_MISSING, str);
3236}
3237
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003238#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003239/*
3240 * print a message with three spaces prepended and '\n' appended.
3241 */
3242 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003243main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003244{
3245 mch_msg(" ");
3246 mch_msg(s);
3247 mch_msg("\n");
3248}
3249
3250/*
3251 * Print messages for "vim -h" or "vim --help" and exit.
3252 */
3253 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003254usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003255{
3256 int i;
3257 static char *(use[]) =
3258 {
3259 N_("[file ..] edit specified file(s)"),
3260 N_("- read text from stdin"),
3261 N_("-t tag edit file where tag is defined"),
3262#ifdef FEAT_QUICKFIX
3263 N_("-q [errorfile] edit file with first error")
3264#endif
3265 };
3266
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003267#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003268 reset_signals(); /* kill us with CTRL-C here, if you like */
3269#endif
3270
3271 mch_msg(longVersion);
3272 mch_msg(_("\n\nusage:"));
3273 for (i = 0; ; ++i)
3274 {
3275 mch_msg(_(" vim [arguments] "));
3276 mch_msg(_(use[i]));
3277 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3278 break;
3279 mch_msg(_("\n or:"));
3280 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003281#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003282 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003283#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003284
3285 mch_msg(_("\n\nArguments:\n"));
3286 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003287#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003288 main_msg(_("--literal\t\tDon't expand wildcards"));
3289#endif
3290#ifdef FEAT_OLE
3291 main_msg(_("-register\t\tRegister this gvim for OLE"));
3292 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3293#endif
3294#ifdef FEAT_GUI
3295 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3296 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3297#endif
3298 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3299 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003300 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003301 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3302#ifdef FEAT_DIFF
3303 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3304#endif
3305 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3306 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3307 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3308 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3309 main_msg(_("-M\t\t\tModifications in text not allowed"));
3310 main_msg(_("-b\t\t\tBinary mode"));
3311#ifdef FEAT_LISP
3312 main_msg(_("-l\t\t\tLisp mode"));
3313#endif
3314 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3315 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003316 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3317#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003318 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003319#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003320 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3321 main_msg(_("-r\t\t\tList swap files and exit"));
3322 main_msg(_("-r (with file name)\tRecover crashed session"));
3323 main_msg(_("-L\t\t\tSame as -r"));
3324#ifdef AMIGA
3325 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3326 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3327#endif
3328#ifdef FEAT_ARABIC
3329 main_msg(_("-A\t\t\tstart in Arabic mode"));
3330#endif
3331#ifdef FEAT_RIGHTLEFT
3332 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3333#endif
3334#ifdef FEAT_FKMAP
3335 main_msg(_("-F\t\t\tStart in Farsi mode"));
3336#endif
3337 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003338 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003339 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003340 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3341#ifdef FEAT_GUI
3342 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3343#endif
3344 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003345#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003346 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003347 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3348 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003349#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003350 main_msg(_("+\t\t\tStart at end of file"));
3351 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003352 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003353 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3354 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3355 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3356 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3357 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3358#ifdef FEAT_CRYPT
3359 main_msg(_("-x\t\t\tEdit encrypted files"));
3360#endif
3361#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3362# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3363 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3364# endif
3365 main_msg(_("-X\t\t\tDo not connect to X server"));
3366#endif
3367#ifdef FEAT_CLIENTSERVER
3368 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3369 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3370 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3371 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003372# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003373 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003374# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003375 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3376 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3377 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3378 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3379#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003380#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003381 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003382#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003383#ifdef FEAT_VIMINFO
3384 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3385#endif
3386 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3387 main_msg(_("--version\t\tPrint version information and exit"));
3388
3389#ifdef FEAT_GUI_X11
3390# ifdef FEAT_GUI_MOTIF
3391 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3392# else
3393# ifdef FEAT_GUI_ATHENA
3394# ifdef FEAT_GUI_NEXTAW
3395 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3396# else
3397 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3398# endif
3399# endif
3400# endif
3401 main_msg(_("-display <display>\tRun vim on <display>"));
3402 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003403 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3404 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3405 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3406 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3407 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3408 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3409 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3410 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3411# ifdef FEAT_GUI_ATHENA
3412 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3413# endif
3414 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3415 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3416 main_msg(_("-xrm <resource>\tSet the specified resource"));
3417#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003418#ifdef FEAT_GUI_GTK
3419 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3420 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3421 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3422 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3423 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003424 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003425 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003426 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003427#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003428#ifdef FEAT_GUI_W32
3429 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003430 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003431#endif
3432
3433#ifdef FEAT_GUI_GNOME
3434 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3435 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003436 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003437 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003438 gui.dofork = FALSE;
3439 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003440 else
3441#endif
3442 mch_exit(0);
3443}
3444
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003445#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003446/*
3447 * Check the result of the ATTENTION dialog:
3448 * When "Quit" selected, exit Vim.
3449 * When "Recover" selected, recover the file.
3450 */
3451 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003452check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003453{
3454 if (swap_exists_action == SEA_QUIT)
3455 getout(1);
3456 handle_swap_exists(NULL);
3457}
3458#endif
3459
Bram Moolenaar08cab962017-03-04 14:37:18 +01003460#endif /* NO_VIM_MAIN */
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003461
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003462#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003463static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003464
3465static struct timeval prev_timeval;
3466
Bram Moolenaar3f269672009-11-03 11:11:11 +00003467# ifdef WIN3264
3468/*
3469 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3470 */
3471 static int
3472gettimeofday(struct timeval *tv, char *dummy)
3473{
3474 long t = clock();
3475 tv->tv_sec = t / CLOCKS_PER_SEC;
3476 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3477 return 0;
3478}
3479# endif
3480
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003481/*
3482 * Save the previous time before doing something that could nest.
3483 * set "*tv_rel" to the time elapsed so far.
3484 */
3485 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003486time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003487{
3488 *((struct timeval *)tv_rel) = prev_timeval;
3489 gettimeofday(&prev_timeval, NULL);
3490 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3491 - ((struct timeval *)tv_rel)->tv_usec;
3492 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3493 - ((struct timeval *)tv_rel)->tv_sec;
3494 if (((struct timeval *)tv_rel)->tv_usec < 0)
3495 {
3496 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3497 --((struct timeval *)tv_rel)->tv_sec;
3498 }
3499 *(struct timeval *)tv_start = prev_timeval;
3500}
3501
3502/*
3503 * Compute the previous time after doing something that could nest.
3504 * Subtract "*tp" from prev_timeval;
3505 * Note: The arguments are (void *) to avoid trouble with systems that don't
3506 * have struct timeval.
3507 */
3508 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003509time_pop(
3510 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003511{
3512 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3513 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3514 if (prev_timeval.tv_usec < 0)
3515 {
3516 prev_timeval.tv_usec += 1000000;
3517 --prev_timeval.tv_sec;
3518 }
3519}
3520
3521 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003522time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003523{
3524 long usec;
3525 long msec;
3526
3527 usec = now->tv_usec - then->tv_usec;
3528 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3529 usec = usec % 1000L;
3530 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3531}
3532
3533 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003534time_msg(
3535 char *mesg,
3536 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003537 (struct timeval *) */
3538{
3539 static struct timeval start;
3540 struct timeval now;
3541
3542 if (time_fd != NULL)
3543 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003544 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003545 {
3546 gettimeofday(&start, NULL);
3547 prev_timeval = start;
3548 fprintf(time_fd, "\n\ntimes in msec\n");
3549 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3550 fprintf(time_fd, " clock elapsed: other lines\n\n");
3551 }
3552 gettimeofday(&now, NULL);
3553 time_diff(&start, &now);
3554 if (((struct timeval *)tv_start) != NULL)
3555 {
3556 fprintf(time_fd, " ");
3557 time_diff(((struct timeval *)tv_start), &now);
3558 }
3559 fprintf(time_fd, " ");
3560 time_diff(&prev_timeval, &now);
3561 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003562 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003563 }
3564}
3565
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003566#endif
3567
Bram Moolenaar595297d2017-03-04 19:11:12 +01003568#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003569 static void
3570set_progpath(char_u *argv0)
3571{
3572 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003573
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003574# ifdef PROC_EXE_LINK
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003575 char buf[PATH_MAX + 1];
3576 ssize_t len;
3577
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003578 len = readlink(PROC_EXE_LINK, buf, PATH_MAX);
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003579 if (len > 0)
3580 {
3581 buf[len] = NUL;
3582 val = (char_u *)buf;
3583 }
3584# else
Bram Moolenaar08cab962017-03-04 14:37:18 +01003585 /* A relative path containing a "/" will become invalid when using ":cd",
3586 * turn it into a full path.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003587 * On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3588 * this. */
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003589# ifdef WIN32
Bram Moolenaar066029e2017-03-05 15:19:32 +01003590 char_u *path = NULL;
3591
3592 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3593 val = path;
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003594# else
Bram Moolenaar066029e2017-03-05 15:19:32 +01003595 char_u buf[MAXPATHL];
3596
Bram Moolenaar43663192017-03-05 14:29:12 +01003597 if (!mch_isFullName(argv0))
3598 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003599 if (gettail(argv0) != argv0
3600 && vim_FullName(argv0, buf, MAXPATHL, TRUE) != FAIL)
3601 val = buf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003602 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003603# endif
Bram Moolenaar066029e2017-03-05 15:19:32 +01003604# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003605
Bram Moolenaar08cab962017-03-04 14:37:18 +01003606 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003607
Bram Moolenaar066029e2017-03-05 15:19:32 +01003608# ifdef WIN32
Bram Moolenaar43663192017-03-05 14:29:12 +01003609 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003610# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003611}
3612
3613#endif /* NO_VIM_MAIN */
3614
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003615#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003616
3617/*
3618 * Common code for the X command server and the Win32 command server.
3619 */
3620
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003621static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003622
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003623/*
3624 * Do the client-server stuff, unless "--servername ''" was used.
3625 */
3626 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003627exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003628{
3629 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3630 {
3631# ifdef WIN32
3632 /* Initialise the client/server messaging infrastructure. */
3633 serverInitMessaging();
3634# endif
3635
3636 /*
3637 * When a command server argument was found, execute it. This may
3638 * exit Vim when it was successful. Otherwise it's executed further
3639 * on. Remember the encoding used here in "serverStrEnc".
3640 */
3641 if (parmp->serverArg)
3642 {
3643 cmdsrv_main(&parmp->argc, parmp->argv,
3644 parmp->serverName_arg, &parmp->serverStr);
3645# ifdef FEAT_MBYTE
3646 parmp->serverStrEnc = vim_strsave(p_enc);
3647# endif
3648 }
3649
3650 /* If we're still running, get the name to register ourselves.
3651 * On Win32 can register right now, for X11 need to setup the
3652 * clipboard first, it's further down. */
3653 parmp->servername = serverMakeName(parmp->serverName_arg,
3654 parmp->argv[0]);
3655# ifdef WIN32
3656 if (parmp->servername != NULL)
3657 {
3658 serverSetName(parmp->servername);
3659 vim_free(parmp->servername);
3660 }
3661# endif
3662 }
3663}
3664
3665/*
3666 * Prepare for running as a Vim server.
3667 */
3668 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003669prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003670{
3671# if defined(FEAT_X11)
3672 /*
3673 * Register for remote command execution with :serversend and --remote
3674 * unless there was a -X or a --servername '' on the command line.
3675 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003676 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003677 */
3678 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3679# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003680 (gui.in_use
3681# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003682 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003683# endif
3684 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003685# endif
3686 parmp->serverName_arg != NULL))
3687 {
3688 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3689 vim_free(parmp->servername);
3690 TIME_MSG("register server name");
3691 }
3692 else
3693 serverDelayedStartName = parmp->servername;
3694# endif
3695
3696 /*
3697 * Execute command ourselves if we're here because the send failed (or
3698 * else we would have exited above).
3699 */
3700 if (parmp->serverStr != NULL)
3701 {
3702 char_u *p;
3703
3704 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3705 parmp->serverStr, &p));
3706 vim_free(p);
3707 }
3708}
3709
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003710 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003711cmdsrv_main(
3712 int *argc,
3713 char **argv,
3714 char_u *serverName_arg,
3715 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003716{
3717 char_u *res;
3718 int i;
3719 char_u *sname;
3720 int ret;
3721 int didone = FALSE;
3722 int exiterr = 0;
3723 char **newArgV = argv + 1;
3724 int newArgC = 1,
3725 Argc = *argc;
3726 int argtype;
3727#define ARGTYPE_OTHER 0
3728#define ARGTYPE_EDIT 1
3729#define ARGTYPE_EDIT_WAIT 2
3730#define ARGTYPE_SEND 3
3731 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003732 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003733# ifndef FEAT_X11
3734 HWND srv;
3735# else
3736 Window srv;
3737
3738 setup_term_clip();
3739# endif
3740
3741 sname = serverMakeName(serverName_arg, argv[0]);
3742 if (sname == NULL)
3743 return;
3744
3745 /*
3746 * Execute the command server related arguments and remove them
3747 * from the argc/argv array; We may have to return into main()
3748 */
3749 for (i = 1; i < Argc; i++)
3750 {
3751 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003752 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003753 {
3754 for (; i < *argc; i++)
3755 {
3756 *newArgV++ = argv[i];
3757 newArgC++;
3758 }
3759 break;
3760 }
3761
Bram Moolenaareb94e552006-03-11 21:35:11 +00003762 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003763 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003764 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3765 {
3766 char *p = argv[i] + 8;
3767
3768 argtype = ARGTYPE_EDIT;
3769 while (*p != NUL)
3770 {
3771 if (STRNICMP(p, "-wait", 5) == 0)
3772 {
3773 argtype = ARGTYPE_EDIT_WAIT;
3774 p += 5;
3775 }
3776 else if (STRNICMP(p, "-silent", 7) == 0)
3777 {
3778 silent = TRUE;
3779 p += 7;
3780 }
3781 else if (STRNICMP(p, "-tab", 4) == 0)
3782 {
3783 tabs = TRUE;
3784 p += 4;
3785 }
3786 else
3787 {
3788 argtype = ARGTYPE_OTHER;
3789 break;
3790 }
3791 }
3792 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003793 else
3794 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003795
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003796 if (argtype != ARGTYPE_OTHER)
3797 {
3798 if (i == *argc - 1)
3799 mainerr_arg_missing((char_u *)argv[i]);
3800 if (argtype == ARGTYPE_SEND)
3801 {
3802 *serverStr = (char_u *)argv[i + 1];
3803 i++;
3804 }
3805 else
3806 {
3807 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003808 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003809 if (*serverStr == NULL)
3810 {
3811 /* Probably out of memory, exit. */
3812 didone = TRUE;
3813 exiterr = 1;
3814 break;
3815 }
3816 Argc = i;
3817 }
3818# ifdef FEAT_X11
3819 if (xterm_dpy == NULL)
3820 {
3821 mch_errmsg(_("No display"));
3822 ret = -1;
3823 }
3824 else
3825 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003826 NULL, &srv, 0, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003827# else
3828 /* Win32 always works? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003829 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003830# endif
3831 if (ret < 0)
3832 {
3833 if (argtype == ARGTYPE_SEND)
3834 {
3835 /* Failed to send, abort. */
3836 mch_errmsg(_(": Send failed.\n"));
3837 didone = TRUE;
3838 exiterr = 1;
3839 }
3840 else if (!silent)
3841 /* Let vim start normally. */
3842 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3843 break;
3844 }
3845
3846# ifdef FEAT_GUI_W32
3847 /* Guess that when the server name starts with "g" it's a GUI
3848 * server, which we can bring to the foreground here.
3849 * Foreground() in the server doesn't work very well. */
3850 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3851 SetForegroundWindow(srv);
3852# endif
3853
3854 /*
3855 * For --remote-wait: Wait until the server did edit each
3856 * file. Also detect that the server no longer runs.
3857 */
3858 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3859 {
3860 int numFiles = *argc - i - 1;
3861 int j;
3862 char_u *done = alloc(numFiles);
3863 char_u *p;
3864# ifdef FEAT_GUI_W32
3865 NOTIFYICONDATA ni;
3866 int count = 0;
3867 extern HWND message_window;
3868# endif
3869
3870 if (numFiles > 0 && argv[i + 1][0] == '+')
3871 /* Skip "+cmd" argument, don't wait for it to be edited. */
3872 --numFiles;
3873
3874# ifdef FEAT_GUI_W32
3875 ni.cbSize = sizeof(ni);
3876 ni.hWnd = message_window;
3877 ni.uID = 0;
3878 ni.uFlags = NIF_ICON|NIF_TIP;
3879 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3880 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3881 Shell_NotifyIcon(NIM_ADD, &ni);
3882# endif
3883
3884 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003885 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003886 while (memchr(done, 0, numFiles) != NULL)
3887 {
3888# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003889 p = serverGetReply(srv, NULL, TRUE, TRUE, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003890 if (p == NULL)
3891 break;
3892# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003893 if (serverReadReply(xterm_dpy, srv, &p, TRUE, -1) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003894 break;
3895# endif
3896 j = atoi((char *)p);
3897 if (j >= 0 && j < numFiles)
3898 {
3899# ifdef FEAT_GUI_W32
3900 ++count;
3901 sprintf(ni.szTip, _("%d of %d edited"),
3902 count, numFiles);
3903 Shell_NotifyIcon(NIM_MODIFY, &ni);
3904# endif
3905 done[j] = 1;
3906 }
3907 }
3908# ifdef FEAT_GUI_W32
3909 Shell_NotifyIcon(NIM_DELETE, &ni);
3910# endif
3911 }
3912 }
3913 else if (STRICMP(argv[i], "--remote-expr") == 0)
3914 {
3915 if (i == *argc - 1)
3916 mainerr_arg_missing((char_u *)argv[i]);
3917# ifdef WIN32
3918 /* Win32 always works? */
3919 if (serverSendToVim(sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003920 &res, NULL, 1, 0, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003921# else
3922 if (xterm_dpy == NULL)
3923 mch_errmsg(_("No display: Send expression failed.\n"));
3924 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003925 &res, NULL, 1, 0, 1, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003926# endif
3927 {
3928 if (res != NULL && *res != NUL)
3929 {
3930 /* Output error from remote */
3931 mch_errmsg((char *)res);
3932 vim_free(res);
3933 res = NULL;
3934 }
3935 mch_errmsg(_(": Send expression failed.\n"));
3936 }
3937 }
3938 else if (STRICMP(argv[i], "--serverlist") == 0)
3939 {
3940# ifdef WIN32
3941 /* Win32 always works? */
3942 res = serverGetVimNames();
3943# else
3944 if (xterm_dpy != NULL)
3945 res = serverGetVimNames(xterm_dpy);
3946# endif
3947 if (called_emsg)
3948 mch_errmsg("\n");
3949 }
3950 else if (STRICMP(argv[i], "--servername") == 0)
3951 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003952 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003953 i++;
3954 continue;
3955 }
3956 else
3957 {
3958 *newArgV++ = argv[i];
3959 newArgC++;
3960 continue;
3961 }
3962 didone = TRUE;
3963 if (res != NULL && *res != NUL)
3964 {
3965 mch_msg((char *)res);
3966 if (res[STRLEN(res) - 1] != '\n')
3967 mch_msg("\n");
3968 }
3969 vim_free(res);
3970 }
3971
3972 if (didone)
3973 {
3974 display_errors(); /* display any collected messages */
3975 exit(exiterr); /* Mission accomplished - get out */
3976 }
3977
3978 /* Return back into main() */
3979 *argc = newArgC;
3980 vim_free(sname);
3981}
3982
3983/*
3984 * Build a ":drop" command to send to a Vim server.
3985 */
3986 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003987build_drop_cmd(
3988 int filec,
3989 char **filev,
3990 int tabs, /* Use ":tab drop" instead of ":drop". */
3991 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003992{
3993 garray_T ga;
3994 int i;
3995 char_u *inicmd = NULL;
3996 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003997 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003998 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003999
4000 if (filec > 0 && filev[0][0] == '+')
4001 {
4002 inicmd = (char_u *)filev[0] + 1;
4003 filev++;
4004 filec--;
4005 }
4006 /* Check if we have at least one argument. */
4007 if (filec <= 0)
4008 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004009
4010 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02004011 cwd = alloc(MAXPATHL);
4012 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004013 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004014 if (mch_dirname(cwd, MAXPATHL) != OK)
4015 {
4016 vim_free(cwd);
4017 return NULL;
4018 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004019 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00004020#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004021 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00004022#else
4023 PATH_ESC_CHARS,
4024#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02004025 '\\', TRUE);
4026 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004027 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004028 return NULL;
4029 ga_init2(&ga, 1, 100);
4030 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004031 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00004032
4033 /* Call inputsave() so that a prompt for an encryption key works. */
4034 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
4035 if (tabs)
4036 ga_concat(&ga, (char_u *)"tab ");
4037 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004038 for (i = 0; i < filec; i++)
4039 {
4040 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004041 * do it again in the Vim server. On MS-Windows only escape
4042 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004043 p = vim_strsave_escaped((char_u *)filev[i],
4044#ifdef UNIX
4045 PATH_ESC_CHARS
4046#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004047 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004048#endif
4049 );
4050 if (p == NULL)
4051 {
4052 vim_free(ga.ga_data);
4053 return NULL;
4054 }
4055 ga_concat(&ga, (char_u *)" ");
4056 ga_concat(&ga, p);
4057 vim_free(p);
4058 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004059 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4060
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004061 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4062 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004063 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4064
4065 /* Switch back to the correct current directory (prior to temporary path
4066 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004067 * correct after the :drop command. With line breaks and spaces:
4068 * if !exists('+acd') || !&acd
4069 * if haslocaldir()
4070 * cd -
4071 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004072 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004073 * cd -
4074 * endif
4075 * endif
4076 */
4077 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004078 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004079 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004080 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004081 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004082
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004083 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004084 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4085 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004086 if (inicmd != NULL)
4087 {
4088 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4089 * the following commands to be inserted as text. Use a "|",
4090 * hopefully "inicmd" does allow this... */
4091 ga_concat(&ga, inicmd);
4092 ga_concat(&ga, (char_u *)"|");
4093 }
4094 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4095 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004096 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004097 ga_append(&ga, NUL);
4098 return ga.ga_data;
4099}
4100
4101/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004102 * Make our basic server name: use the specified "arg" if given, otherwise use
4103 * the tail of the command "cmd" we were started with.
4104 * Return the name in allocated memory. This doesn't include a serial number.
4105 */
4106 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004107serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004108{
4109 char_u *p;
4110
4111 if (arg != NULL && *arg != NUL)
4112 p = vim_strsave_up(arg);
4113 else
4114 {
4115 p = vim_strsave_up(gettail((char_u *)cmd));
4116 /* Remove .exe or .bat from the name. */
4117 if (p != NULL && vim_strchr(p, '.') != NULL)
4118 *vim_strchr(p, '.') = NUL;
4119 }
4120 return p;
4121}
4122#endif /* FEAT_CLIENTSERVER */
4123
4124#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4125/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004126 * Replace termcodes such as <CR> and insert as key presses if there is room.
4127 */
4128 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004129server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004130{
4131 char_u *ptr = NULL;
4132 char_u *cpo_save = p_cpo;
4133
4134 /* Set 'cpoptions' the way we want it.
4135 * B set - backslashes are *not* treated specially
4136 * k set - keycodes are *not* reverse-engineered
4137 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004138 * The last but one parameter of replace_termcodes() is TRUE so that the
4139 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004140 */
4141 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004142 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004143 p_cpo = cpo_save;
4144
4145 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4146 {
4147 /*
4148 * Add the string to the input stream.
4149 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4150 *
4151 * First clear typed characters from the typeahead buffer, there could
4152 * be half a mapping there. Then append to the existing string, so
4153 * that multiple commands from a client are concatenated.
4154 */
4155 if (typebuf.tb_maplen < typebuf.tb_len)
4156 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4157 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4158
4159 /* Let input_available() know we inserted text in the typeahead
4160 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004161 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004162 }
4163 vim_free((char_u *)ptr);
4164}
4165
4166/*
4167 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004168 */
4169 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004170eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004171{
4172 char_u *res;
4173 int save_dbl = debug_break_level;
4174 int save_ro = redir_off;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004175 void *fc;
4176
4177 /* Evaluate the expression at the toplevel, don't use variables local to
4178 * the calling function. */
4179 fc = clear_current_funccal();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004180
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004181 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4182 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004183 debug_break_level = -1;
4184 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004185 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4186 * to be typed. Do generate errors so that try/catch works. */
4187 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004188
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004189 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004190
4191 debug_break_level = save_dbl;
4192 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004193 --emsg_silent;
4194 if (emsg_silent < 0)
4195 emsg_silent = 0;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004196 restore_current_funccal(fc);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004197
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004198 /* A client can tell us to redraw, but not to display the cursor, so do
4199 * that here. */
4200 setcursor();
4201 out_flush();
4202#ifdef FEAT_GUI
4203 if (gui.in_use)
4204 gui_update_cursor(FALSE, FALSE);
4205#endif
4206
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004207 return res;
4208}
4209
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004210/*
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004211 * Evaluate a command or expression sent to ourselves.
4212 */
4213 int
4214sendToLocalVim(char_u *cmd, int asExpr, char_u **result)
4215{
4216 if (asExpr)
4217 {
4218 char_u *ret;
4219
4220 ret = eval_client_expr_to_string(cmd);
4221 if (result != NULL)
4222 {
4223 if (ret == NULL)
4224 {
4225 char *err = _(e_invexprmsg);
4226 size_t len = STRLEN(cmd) + STRLEN(err) + 5;
4227 char_u *msg;
4228
Bram Moolenaar1662ce12017-03-19 21:47:50 +01004229 msg = alloc((unsigned)len);
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004230 if (msg != NULL)
4231 vim_snprintf((char *)msg, len, "%s: \"%s\"", err, cmd);
4232 *result = msg;
4233 }
4234 else
4235 *result = ret;
4236 }
4237 else
4238 vim_free(ret);
4239 return ret == NULL ? -1 : 0;
4240 }
4241 server_to_input_buf(cmd);
4242 return 0;
4243}
4244
4245/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004246 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4247 * return an allocated string. Otherwise return "data".
4248 * "*tofree" is set to the result when it needs to be freed later.
4249 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004250 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004251serverConvert(
4252 char_u *client_enc UNUSED,
4253 char_u *data,
4254 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004255{
4256 char_u *res = data;
4257
4258 *tofree = NULL;
4259# ifdef FEAT_MBYTE
4260 if (client_enc != NULL && p_enc != NULL)
4261 {
4262 vimconv_T vimconv;
4263
4264 vimconv.vc_type = CONV_NONE;
4265 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4266 && vimconv.vc_type != CONV_NONE)
4267 {
4268 res = string_convert(&vimconv, data, NULL);
4269 if (res == NULL)
4270 res = data;
4271 else
4272 *tofree = res;
4273 }
4274 convert_setup(&vimconv, NULL, NULL);
4275 }
4276# endif
4277 return res;
4278}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004279#endif