blob: 26941bd6e7e608519c8be5bfc7674ebc8a4839fe [file] [log] [blame]
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
Bram 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/* Maximum number of commands from + or -c arguments. */
27#define MAX_ARG_CMDS 10
28
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000029/* values for "window_layout" */
30#define WIN_HOR 1 /* "-o" horizontally split windows */
31#define WIN_VER 2 /* "-O" vertically split windows */
32#define WIN_TABS 3 /* "-p" windows on tab pages */
33
Bram Moolenaar58d98232005-07-23 22:25:46 +000034/* Struct for various parameters passed between main() and other functions. */
35typedef struct
36{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000037 int argc;
38 char **argv;
39
40 int evim_mode; /* started as "evim" */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000041 char_u *use_vimrc; /* vimrc from -u argument */
42
43 int n_commands; /* no. of commands from + or -c */
44 char_u *commands[MAX_ARG_CMDS]; /* commands from + or -c arg. */
45 char_u cmds_tofree[MAX_ARG_CMDS]; /* commands that need free() */
46 int n_pre_commands; /* no. of commands from --cmd */
47 char_u *pre_commands[MAX_ARG_CMDS]; /* commands from --cmd argument */
48
49 int edit_type; /* type of editing to do */
50 char_u *tagname; /* tag from -t argument */
51#ifdef FEAT_QUICKFIX
52 char_u *use_ef; /* 'errorfile' from -q argument */
53#endif
54
55 int want_full_screen;
56 int stdout_isatty; /* is stdout a terminal? */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +010057 int not_a_term; /* no warning for missing term? */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000058 char_u *term; /* specified terminal name */
59#ifdef FEAT_CRYPT
60 int ask_for_key; /* -x argument */
61#endif
62 int no_swap_file; /* "-n" argument used */
63#ifdef FEAT_EVAL
64 int use_debug_break_level;
65#endif
66#ifdef FEAT_WINDOWS
67 int window_count; /* number of windows to use */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000068 int window_layout; /* 0, WIN_HOR, WIN_VER or WIN_TABS */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000069#endif
70
71#ifdef FEAT_CLIENTSERVER
Bram Moolenaar58d98232005-07-23 22:25:46 +000072 int serverArg; /* TRUE when argument for a server */
73 char_u *serverName_arg; /* cmdline arg for server name */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000074 char_u *serverStr; /* remote server command */
75 char_u *serverStrEnc; /* encoding of serverStr */
76 char_u *servername; /* allocated name for our server */
77#endif
Bram Moolenaar53076832015-12-31 19:53:21 +010078#if !defined(UNIX) && !defined(__EMX__)
79# define EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +000080 int literal; /* don't expand file names */
81#endif
82#ifdef MSWIN
83 int full_path; /* file name argument was full path */
84#endif
85#ifdef FEAT_DIFF
86 int diff_mode; /* start with 'diff' set */
87#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +000088} mparm_T;
89
Bram Moolenaarc013cb62005-07-24 21:18:31 +000090/* Values for edit_type. */
91#define EDIT_NONE 0 /* no edit type yet */
92#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
93#define EDIT_STDIN 2 /* read file from stdin */
94#define EDIT_TAG 3 /* tag name argument given, use tagname */
95#define EDIT_QF 4 /* start in quickfix mode */
96
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010097#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010098static int file_owned(char *fname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +000099#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100100static void mainerr(int, char_u *);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100101#ifndef NO_VIM_MAIN
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100102static void main_msg(char *s);
103static void usage(void);
104static int get_number_arg(char_u *p, int *idx, int def);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100105# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100106static void init_locale(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100107# endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100108static void parse_command_name(mparm_T *parmp);
109static void early_arg_scan(mparm_T *parmp);
110static void command_line_scan(mparm_T *parmp);
111static void check_tty(mparm_T *parmp);
112static void read_stdin(void);
113static void create_windows(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100114# ifdef FEAT_WINDOWS
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100115static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100116# endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void exe_pre_commands(mparm_T *parmp);
118static void exe_commands(mparm_T *parmp);
119static void source_startup_scripts(mparm_T *parmp);
120static void main_start_gui(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100121# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100122static void check_swap_exists_action(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100123# endif
124# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100125static void exec_on_server(mparm_T *parmp);
126static void prepare_server(mparm_T *parmp);
127static void cmdsrv_main(int *argc, char **argv, char_u *serverName_arg, char_u **serverStr);
128static char_u *serverMakeName(char_u *arg, char *cmd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100129# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000130#endif
131
132
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000133/*
134 * Different types of error messages.
135 */
136static char *(main_errors[]) =
137{
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000138 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000139#define ME_UNKNOWN_OPTION 0
140 N_("Too many edit arguments"),
141#define ME_TOO_MANY_ARGS 1
142 N_("Argument missing after"),
143#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000144 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000145#define ME_GARBAGE 3
146 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
147#define ME_EXTRA_CMD 4
148 N_("Invalid argument for"),
149#define ME_INVALID_ARG 5
150};
151
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100152#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100153#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +0200154
155static char_u *start_dir = NULL; /* current working dir on startup */
156
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000157 int
158# ifdef VIMDLL
159_export
160# endif
161# ifdef FEAT_GUI_MSWIN
162# ifdef __BORLANDC__
163_cdecl
164# endif
165VimMain
166# else
167main
168# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100169(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000170{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000171 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000172 mparm_T params; /* various parameters passed between
173 * main() and other functions. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000174#ifdef STARTUPTIME
175 int i;
176#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000177
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000178 /*
179 * Do any system-specific initialisations. These can NOT use IObuff or
180 * NameBuff. Thus emsg2() cannot be called!
181 */
182 mch_early_init();
183
Bram Moolenaar14993322014-09-09 12:25:33 +0200184#if defined(WIN32) && defined(FEAT_MBYTE)
185 /*
186 * MingW expands command line arguments, which confuses our code to
187 * convert when 'encoding' changes. Get the unexpanded arguments.
188 */
189 argc = get_cmd_argsW(&argv);
190#endif
191
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000192 /* Many variables are in "params" so that we can pass them to invoked
193 * functions without a lot of arguments. "argc" and "argv" are also
194 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000195 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000196 params.argc = argc;
197 params.argv = argv;
198 params.want_full_screen = TRUE;
199#ifdef FEAT_EVAL
200 params.use_debug_break_level = -1;
201#endif
202#ifdef FEAT_WINDOWS
203 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000204#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000205
Bram Moolenaar99685e62013-05-11 13:56:18 +0200206#ifdef FEAT_RUBY
207 {
208 int ruby_stack_start;
209 vim_ruby_init((void *)&ruby_stack_start);
210 }
211#endif
212
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000213#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000214 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000215#endif
216
217#ifdef MEM_PROFILE
218 atexit(vim_mem_profile_dump);
219#endif
220
221#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000222 for (i = 1; i < argc; ++i)
223 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000224 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000225 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000226 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000227 TIME_MSG("--- VIM STARTING ---");
228 break;
229 }
230 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000231#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000232 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000233
234#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000235 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000236#endif
237
238#ifdef FEAT_MBYTE
239 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
240#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000241#ifdef FEAT_EVAL
242 eval_init(); /* init global variables */
243#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000244
245#ifdef __QNXNTO__
246 qnx_init(); /* PhAttach() for clipboard, (and gui) */
247#endif
248
249#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000250 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000251 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000252 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000253 TIME_MSG("GUI prepared");
254#endif
255
256 /* Init the table of Normal mode commands. */
257 init_normal_cmds();
258
259#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000260 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000261#endif
262
263 /*
264 * Allocate space for the generic buffers (needed for set_init_1() and
265 * EMSG2()).
266 */
267 if ((IObuff = alloc(IOSIZE)) == NULL
268 || (NameBuff = alloc(MAXPATHL)) == NULL)
269 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000270 TIME_MSG("Allocated generic buffers");
271
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000272#ifdef NBDEBUG
273 /* Wait a moment for debugging NetBeans. Must be after allocating
274 * NameBuff. */
275 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
276 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000277 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000278#endif
279
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000280#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
281 /*
282 * Setup to use the current locale (for ctype() and many other things).
283 * NOTE: Translated messages with encodings other than latin1 will not
284 * work until set_init_1() has been called!
285 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000286 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000287 TIME_MSG("locale set");
288#endif
289
290#ifdef FEAT_GUI
291 gui.dofork = TRUE; /* default is to use fork() */
292#endif
293
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000294 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000295 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000296 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000297 * --server...
298 * --socketid
Bram Moolenaar78e17622007-08-30 10:26:19 +0000299 * --windowid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000300 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000301 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000302
303#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000304 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000305#endif
306#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000307 /* Prepare for possibly starting GUI sometime */
308 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000309 TIME_MSG("GUI prepared");
310#endif
311
312#ifdef FEAT_CLIPBOARD
313 clip_init(FALSE); /* Initialise clipboard stuff */
314 TIME_MSG("clipboard setup");
315#endif
316
317 /*
318 * Check if we have an interactive window.
319 * On the Amiga: If there is no window, we open one with a newcli command
320 * (needed for :! to * work). mch_check_win() will also handle the -d or
321 * -dev argument.
322 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000323 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000324 TIME_MSG("window checked");
325
326 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000327 * Allocate the first window and buffer.
328 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000329 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000330 if (win_alloc_first() == FAIL)
331 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000332
333 init_yank(); /* init yank buffers */
334
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000335 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200336 global_alist.id = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000337
338 /*
339 * Set the default values for the options.
340 * NOTE: Non-latin1 translated messages are working only after this,
341 * because this is where "has_mbyte" will be set, which is used by
342 * msg_outtrans_len_attr().
343 * First find out the home directory, needed to expand "~" in options.
344 */
345 init_homedir(); /* find real value of $HOME */
346 set_init_1();
347 TIME_MSG("inits 1");
348
349#ifdef FEAT_EVAL
350 set_lang_var(); /* set v:lang and v:ctype */
351#endif
352
353#ifdef FEAT_CLIENTSERVER
354 /*
355 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000356 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000357 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000358 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000359#endif
360
361 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000362 * Figure out the way to work from the command name argv[0].
363 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000364 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000365 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000366
367 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000368 * Process the command line arguments. File names are put in the global
369 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000370 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000371 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000372 TIME_MSG("parsing arguments");
373
374 /*
375 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000376 * there is no terminal version, and on Windows we can't fork one off with
377 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000378 */
379#ifdef ALWAYS_USE_GUI
380 gui.starting = TRUE;
381#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000382# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000383 /*
384 * Check if the GUI can be started. Reset gui.starting if not.
385 * Don't know about other systems, stay on the safe side and don't check.
386 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000387 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000388 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000389 if (gui_init_check() == FAIL)
390 {
391 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000392
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000393 /* When running "evim" or "gvim -y" we need the menus, exit if we
394 * don't have them. */
395 if (params.evim_mode)
396 mch_exit(1);
397 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000398 }
399# endif
400#endif
401
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000402 if (GARGCOUNT > 0)
403 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100404#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000405 /*
406 * Expand wildcards in file names.
407 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000408 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000409 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200410 start_dir = alloc(MAXPATHL);
411 if (start_dir != NULL)
412 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000413 /* Temporarily add '(' and ')' to 'isfname'. These are valid
414 * filename characters but are excluded from 'isfname' to make
415 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
416 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000417 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000418 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200419 if (start_dir != NULL)
420 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000421 }
422#endif
423 fname = alist_name(&GARGLIST[0]);
424 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000425
426#if defined(WIN32) && defined(FEAT_MBYTE)
427 {
428 extern void set_alist_count(void);
429
430 /* Remember the number of entries in the argument list. If it changes
431 * we don't react on setting 'encoding'. */
432 set_alist_count();
433 }
434#endif
435
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000436#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000437 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000438 {
439 /*
440 * If there is one filename, fully qualified, we have very probably
441 * been invoked from explorer, so change to the file's directory.
442 * Hint: to avoid this when typing a command use a forward slash.
443 * If the cd fails, it doesn't matter.
444 */
445 (void)vim_chdirfile(fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200446 if (start_dir != NULL)
447 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000448 }
449#endif
450 TIME_MSG("expanding arguments");
451
452#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000453 if (params.diff_mode && params.window_count == -1)
454 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000455#endif
456
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000457 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000458 ++RedrawingDisabled;
459
460 /*
461 * When listing swap file names, don't do cursor positioning et. al.
462 */
463 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000464 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000465
466 /*
467 * When certain to start the GUI, don't check capabilities of terminal.
468 * For GTK we can't be sure, but when started from the desktop it doesn't
469 * make sense to try using a terminal.
470 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000471#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000472 if (gui.starting
473# ifdef FEAT_GUI_GTK
474 && !isatty(2)
475# endif
476 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000477 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000478#endif
479
480#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
481 /* When the GUI is started from Finder, need to display messages in a
482 * message box. isatty(2) returns TRUE anyway, thus we need to check the
483 * name to know we're not started from a terminal. */
484 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000485 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000486 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000487
488 /* Avoid always using "/" as the current directory. Note that when
489 * started from Finder the arglist will be filled later in
490 * HandleODocAE() and "fname" will be NULL. */
491 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
492 && STRCMP(NameBuff, "/") == 0)
493 {
494 if (fname != NULL)
495 (void)vim_chdirfile(fname);
496 else
497 {
498 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
499 vim_chdir(NameBuff);
500 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200501 if (start_dir != NULL)
502 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000503 }
504 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000505#endif
506
507 /*
508 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100509 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000510 * Note that we may use mch_exit() before mch_init()!
511 */
512 mch_init();
513 TIME_MSG("shell init");
514
515#ifdef USE_XSMP
516 /*
517 * For want of anywhere else to do it, try to connect to xsmp here.
518 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
519 * Hijacking -X 'no X connection' to also disable XSMP connection as that
520 * has a similar delay upon failure.
521 * Only try if SESSION_MANAGER is set to something non-null.
522 */
523 if (!x_no_connect)
524 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000525 char *p = getenv("SESSION_MANAGER");
526
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000527 if (p != NULL && *p != NUL)
528 {
529 xsmp_init();
530 TIME_MSG("xsmp init");
531 }
532 }
533#endif
534
535 /*
536 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000537 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000538 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000539
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000540 /* This message comes before term inits, but after setting "silent_mode"
541 * when the input is not a tty. */
542 if (GARGCOUNT > 1 && !silent_mode)
543 printf(_("%d files to edit\n"), GARGCOUNT);
544
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000545 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000546 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000547 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000548 capabilities (will set full_screen) */
549 screen_start(); /* don't know where cursor is now */
550 TIME_MSG("Termcap init");
551 }
552
553 /*
554 * Set the default values for the options that use Rows and Columns.
555 */
556 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000557 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000558#ifdef FEAT_DIFF
559 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
560 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000561 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000562 diff_win_options(firstwin, FALSE);
563#endif
564
565 cmdline_row = Rows - p_ch;
566 msg_row = cmdline_row;
567 screenalloc(FALSE); /* allocate screen buffers */
568 set_init_2();
569 TIME_MSG("inits 2");
570
571 msg_scroll = TRUE;
572 no_wait_return = TRUE;
573
574 init_mappings(); /* set up initial mappings */
575
576 init_highlight(TRUE, FALSE); /* set the default highlight groups */
577 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000578
579#ifdef FEAT_EVAL
580 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000581 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000582#endif
583
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100584#ifdef FEAT_MZSCHEME
585 /*
586 * Newer version of MzScheme (Racket) require earlier (trampolined)
587 * initialisation via scheme_main_setup.
588 * Implement this by initialising it as early as possible
589 * and splitting off remaining Vim main into vim_main2
590 */
591 {
592 /* Pack up preprocessed command line arguments.
593 * It is safe because Scheme does not access argc/argv. */
594 char *args[2];
595 args[0] = (char *)fname;
596 args[1] = (char *)&params;
597 return mzscheme_main(2, args);
598 }
599}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100600#endif
601#endif /* NO_VIM_MAIN */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100602
Bram Moolenaar8866d272012-11-28 15:55:42 +0100603/* vim_main2() needs to be produced when FEAT_MZSCHEME is defined even when
604 * NO_VIM_MAIN is defined. */
605#ifdef FEAT_MZSCHEME
606 int
607vim_main2(int argc UNUSED, char **argv UNUSED)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100608{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100609# ifndef NO_VIM_MAIN
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100610 char_u *fname = (char_u *)argv[0];
611 mparm_T params;
612
613 memcpy(&params, argv[1], sizeof(params));
Bram Moolenaar8866d272012-11-28 15:55:42 +0100614# else
615 return 0;
616}
617# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100618#endif
619
Bram Moolenaar8866d272012-11-28 15:55:42 +0100620#ifndef NO_VIM_MAIN
Bram Moolenaar58d98232005-07-23 22:25:46 +0000621 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000622 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000623
Bram Moolenaar58d98232005-07-23 22:25:46 +0000624 /* Source startup scripts. */
625 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000626
627#ifdef FEAT_EVAL
628 /*
629 * Read all the plugin files.
630 * Only when compiled with +eval, since most plugins need it.
631 */
632 if (p_lpl)
633 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000634# ifdef VMS /* Somehow VMS doesn't handle the "**". */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100635 source_runtime((char_u *)"plugin/*.vim", DIP_ALL);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000636# else
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100637 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000638# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000639 TIME_MSG("loading plugins");
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100640
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100641 ex_packloadall(NULL);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100642 TIME_MSG("loading packages");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000643 }
644#endif
645
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000646#ifdef FEAT_DIFF
647 /* Decide about window layout for diff mode after reading vimrc. */
648 if (params.diff_mode && params.window_layout == 0)
649 {
650 if (diffopt_horizontal())
651 params.window_layout = WIN_HOR; /* use horizontal split */
652 else
653 params.window_layout = WIN_VER; /* use vertical split */
654 }
655#endif
656
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000657 /*
658 * Recovery mode without a file name: List swap files.
659 * This uses the 'dir' option, therefore it must be after the
660 * initializations.
661 */
662 if (recoverymode && fname == NULL)
663 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200664 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000665 mch_exit(0);
666 }
667
668 /*
669 * Set a few option defaults after reading .vimrc files:
670 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
671 */
672 set_init_3();
673 TIME_MSG("inits 3");
674
675 /*
676 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
677 * Note that this overrides anything from a vimrc file.
678 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000679 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000680 p_uc = 0;
681
682#ifdef FEAT_FKMAP
683 if (curwin->w_p_rl && p_altkeymap)
684 {
685 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
686# ifdef FEAT_ARABIC
687 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
688# endif
689 p_fkmap = TRUE; /* Set the Farsi keymap mode */
690 }
691#endif
692
693#ifdef FEAT_GUI
694 if (gui.starting)
695 {
696#if defined(UNIX) || defined(VMS)
697 /* When something caused a message from a vimrc script, need to output
698 * an extra newline before the shell prompt. */
699 if (did_emsg || msg_didout)
700 putchar('\n');
701#endif
702
703 gui_start(); /* will set full_screen to TRUE */
704 TIME_MSG("starting GUI");
705
706 /* When running "evim" or "gvim -y" we need the menus, exit if we
707 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000708 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000709 mch_exit(1);
710 }
711#endif
712
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000713#ifdef FEAT_VIMINFO
714 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000715 * Read in registers, history etc, but not marks, from the viminfo file.
716 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000717 */
718 if (*p_viminfo != NUL)
719 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000720 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000721 TIME_MSG("reading viminfo");
722 }
723#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100724#ifdef FEAT_EVAL
725 /* It's better to make v:oldfiles an empty list than NULL. */
726 if (get_vim_var_list(VV_OLDFILES) == NULL)
727 set_vim_var_list(VV_OLDFILES, list_alloc());
728#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000729
730#ifdef FEAT_QUICKFIX
731 /*
732 * "-q errorfile": Load the error file now.
733 * If the error file can't be read, exit before doing anything else.
734 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000735 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000736 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000737 if (params.use_ef != NULL)
738 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000739 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200740 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
741 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000742 {
743 out_char('\n');
744 mch_exit(3);
745 }
746 TIME_MSG("reading errorfile");
747 }
748#endif
749
750 /*
751 * Start putting things on the screen.
752 * Scroll screen down before drawing over it
753 * Clear screen now, so file message will not be cleared.
754 */
755 starting = NO_BUFFERS;
756 no_wait_return = FALSE;
757 if (!exmode_active)
758 msg_scroll = FALSE;
759
760#ifdef FEAT_GUI
761 /*
762 * This seems to be required to make callbacks to be called now, instead
763 * of after things have been put on the screen, which then may be deleted
764 * when getting a resize callback.
765 * For the Mac this handles putting files dropped on the Vim icon to
766 * global_alist.
767 */
768 if (gui.in_use)
769 {
770# ifdef FEAT_SUN_WORKSHOP
771 if (!usingSunWorkShop)
772# endif
773 gui_wait_for_chars(50L);
774 TIME_MSG("GUI delay");
775 }
776#endif
777
778#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
779 qnx_clip_init();
780#endif
781
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200782#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
783 clip_init(TRUE);
784#endif
785
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000786#ifdef FEAT_XCLIPBOARD
787 /* Start using the X clipboard, unless the GUI was started. */
788# ifdef FEAT_GUI
789 if (!gui.in_use)
790# endif
791 {
792 setup_term_clip();
793 TIME_MSG("setup clipboard");
794 }
795#endif
796
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000797#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000798 /* Prepare for being a Vim server. */
799 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000800#endif
801
802 /*
803 * If "-" argument given: Read file from stdin.
804 * Do this before starting Raw mode, because it may change things that the
805 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
806 * are the same terminal: "cat | vim -".
807 * Using autocommands here may cause trouble...
808 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000809 if (params.edit_type == EDIT_STDIN && !recoverymode)
810 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000811
812#if defined(UNIX) || defined(VMS)
813 /* When switching screens and something caused a message from a vimrc
814 * script, need to output an extra newline on exit. */
815 if ((did_emsg || msg_didout) && *T_TI != NUL)
816 newline_on_exit = TRUE;
817#endif
818
819 /*
820 * When done something that is not allowed or error message call
821 * wait_return. This must be done before starttermcap(), because it may
822 * switch to another screen. It must be done after settmode(TMODE_RAW),
823 * because we want to react on a single key stroke.
824 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000825 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000826 */
827 settmode(TMODE_RAW);
828 TIME_MSG("setting raw mode");
829
830 if (need_wait_return || msg_didany)
831 {
832 wait_return(TRUE);
833 TIME_MSG("waiting for return");
834 }
835
836 starttermcap(); /* start termcap if not done by wait_return() */
837 TIME_MSG("start termcap");
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200838#if defined(FEAT_TERMRESPONSE)
839# if defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200840 may_req_ambiguous_char_width();
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200841# endif
842 may_req_bg_color();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100843#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000844
845#ifdef FEAT_MOUSE
846 setmouse(); /* may start using the mouse */
847#endif
848 if (scroll_region)
849 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000850 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000851
852 /*
853 * Don't clear the screen when starting in Ex mode, unless using the GUI.
854 */
855 if (exmode_active
856#ifdef FEAT_GUI
857 && !gui.in_use
858#endif
859 )
860 must_redraw = CLEAR;
861 else
862 {
863 screenclear(); /* clear screen */
864 TIME_MSG("clearing screen");
865 }
866
867#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000868 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000869 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100870 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200871 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000872 TIME_MSG("getting crypt key");
873 }
874#endif
875
876 no_wait_return = TRUE;
877
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000878 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000879 * Create the requested number of windows and edit buffers in them.
880 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000881 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000882 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000883 TIME_MSG("opening buffers");
884
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000885#ifdef FEAT_EVAL
886 /* clear v:swapcommand */
887 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
888#endif
889
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000890 /* Ex starts at last line of the file */
891 if (exmode_active)
892 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
893
894#ifdef FEAT_AUTOCMD
895 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
896 TIME_MSG("BufEnter autocommands");
897#endif
898 setpcmark();
899
900#ifdef FEAT_QUICKFIX
901 /*
902 * When started with "-q errorfile" jump to first error now.
903 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000904 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000905 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000906 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000907 TIME_MSG("jump to first error");
908 }
909#endif
910
911#ifdef FEAT_WINDOWS
912 /*
913 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000914 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000915 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200916 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000917#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200918 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000919
920#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000921 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000922 {
923 win_T *wp;
924
925 /* set options in each window for "vimdiff". */
926 for (wp = firstwin; wp != NULL; wp = wp->w_next)
927 diff_win_options(wp, TRUE);
928 }
929#endif
930
931 /*
932 * Shorten any of the filenames, but only when absolute.
933 */
934 shorten_fnames(FALSE);
935
936 /*
937 * Need to jump to the tag before executing the '-c command'.
938 * Makes "vim -c '/return' -t main" work.
939 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000940 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000941 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000942#if defined(HAS_SWAP_EXISTS_ACTION)
943 swap_exists_did_quit = FALSE;
944#endif
945
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000946 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000947 do_cmdline_cmd(IObuff);
948 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000949
950#if defined(HAS_SWAP_EXISTS_ACTION)
951 /* If the user doesn't want to edit the file then we quit here. */
952 if (swap_exists_did_quit)
953 getout(1);
954#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000955 }
956
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000957 /* Execute any "+", "-c" and "-S" arguments. */
958 if (params.n_commands > 0)
959 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000960
961 RedrawingDisabled = 0;
962 redraw_all_later(NOT_VALID);
963 no_wait_return = FALSE;
964 starting = 0;
965
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200966 /* 'autochdir' has been postponed */
967 DO_AUTOCHDIR
968
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000969#ifdef FEAT_TERMRESPONSE
970 /* Requesting the termresponse is postponed until here, so that a "-c q"
971 * argument doesn't make it appear in the shell Vim was started from. */
972 may_req_termresponse();
973#endif
974
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000975 /* start in insert mode */
976 if (p_im)
977 need_start_insertmode = TRUE;
978
Bram Moolenaar14735512016-03-26 21:00:08 +0100979#ifdef FEAT_EVAL
980 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
981#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000982#ifdef FEAT_AUTOCMD
983 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
984 TIME_MSG("VimEnter autocommands");
985#endif
986
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200987#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
988 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
989 * done after the clipboard is available and all initial commands that may
990 * modify the 'clipboard' setting have run; i.e. just before entering the
991 * main loop. */
992 {
993 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100994
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200995 adjust_clip_reg(&default_regname);
996 set_reg_var(default_regname);
997 }
998#endif
999
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001000#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1001 /* When a startup script or session file setup for diff'ing and
1002 * scrollbind, sync the scrollbind now. */
1003 if (curwin->w_p_diff && curwin->w_p_scb)
1004 {
1005 update_topline();
1006 check_scrollbind((linenr_T)0, 0L);
1007 TIME_MSG("diff scrollbinding");
1008 }
1009#endif
1010
1011#if defined(WIN3264) && !defined(FEAT_GUI_W32)
1012 mch_set_winsize_now(); /* Allow winsize changes from now on */
1013#endif
1014
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001015#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
1016 /* When tab pages were created, may need to update the tab pages line and
1017 * scrollbars. This is skipped while creating them. */
1018 if (first_tabpage->tp_next != NULL)
1019 {
1020 out_flush();
1021 gui_init_which_components(NULL);
1022 gui_update_scrollbars(TRUE);
1023 }
1024 need_mouse_correct = TRUE;
1025#endif
1026
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001027 /* If ":startinsert" command used, stuff a dummy command to be able to
1028 * call normal_cmd(), which will then start Insert mode. */
1029 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001030 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001031
1032#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001033 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001034 {
1035# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +02001036# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02001037 && !defined(FEAT_GUI_W32)
1038 if (gui.in_use)
1039 {
1040 mch_errmsg(_("netbeans is not supported with this GUI\n"));
1041 mch_exit(2);
1042 }
1043# endif
1044# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001045 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001046 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001047 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001048#endif
1049
1050 TIME_MSG("before starting main loop");
1051
1052 /*
1053 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001054 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001055 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001056
1057 return 0;
1058}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001059#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001060#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001061
1062/*
1063 * Main loop: Execute Normal mode commands until exiting Vim.
1064 * Also used to handle commands in the command-line window, until the window
1065 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 * Also used to handle ":visual" command after ":global": execute Normal mode
1067 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001068 */
1069 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001070main_loop(
1071 int cmdwin, /* TRUE when working in the command-line window */
1072 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001073{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001074 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001075 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001076#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001077 /* these are static to avoid a compiler warning */
1078 static linenr_T conceal_old_cursor_line = 0;
1079 static linenr_T conceal_new_cursor_line = 0;
1080 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001081#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001082
1083#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1084 /* Setup to catch a terminating error from the X server. Just ignore
1085 * it, restore the state and continue. This might not always work
1086 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001087 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001089 {
1090 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001091 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001092 got_int = TRUE;
1093 need_wait_return = FALSE;
1094 global_busy = FALSE;
1095 exmode_active = 0;
1096 skip_redraw = FALSE;
1097 RedrawingDisabled = 0;
1098 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001099 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001100# ifdef FEAT_EVAL
1101 emsg_skip = 0;
1102# endif
1103 emsg_off = 0;
1104# ifdef FEAT_MOUSE
1105 setmouse();
1106# endif
1107 settmode(TMODE_RAW);
1108 starttermcap();
1109 scroll_start();
1110 redraw_later_clear();
1111 }
1112#endif
1113
1114 clear_oparg(&oa);
1115 while (!cmdwin
1116#ifdef FEAT_CMDWIN
1117 || cmdwin_result == 0
1118#endif
1119 )
1120 {
1121 if (stuff_empty())
1122 {
1123 did_check_timestamps = FALSE;
1124 if (need_check_timestamps)
1125 check_timestamps(FALSE);
1126 if (need_wait_return) /* if wait_return still needed ... */
1127 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001128 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001129 {
1130 need_start_insertmode = FALSE;
1131 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1132 /* skip the fileinfo message now, because it would be shown
1133 * after insert mode finishes! */
1134 need_fileinfo = FALSE;
1135 }
1136 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001137
1138 /* Reset "got_int" now that we got back to the main loop. Except when
1139 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1140 * the ":g" command.
1141 * For ":g/pat/vi" we reset "got_int" when used once. When used
1142 * a second time we go back to Ex mode and abort the ":g" command. */
1143 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001144 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001145 if (noexmode && global_busy && !exmode_active && previous_got_int)
1146 {
1147 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1148 * used and keep "got_int" set, so that it aborts ":g". */
1149 exmode_active = EXMODE_NORMAL;
1150 State = NORMAL;
1151 }
1152 else if (!global_busy || !exmode_active)
1153 {
1154 if (!quit_more)
1155 (void)vgetc(); /* flush all buffers */
1156 got_int = FALSE;
1157 }
1158 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001159 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001160 else
1161 previous_got_int = FALSE;
1162
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001163 if (!exmode_active)
1164 msg_scroll = FALSE;
1165 quit_more = FALSE;
1166
1167 /*
1168 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1169 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1170 * update cursor and redraw.
1171 */
1172 if (skip_redraw || exmode_active)
1173 skip_redraw = FALSE;
1174 else if (do_redraw || stuff_empty())
1175 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001176#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001177 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001178 if (!finish_op && (
1179# ifdef FEAT_AUTOCMD
1180 has_cursormoved()
1181# endif
1182# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1183 ||
1184# endif
1185# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001186 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001187# endif
1188 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001189# ifdef FEAT_AUTOCMD
1190 && !equalpos(last_cursormoved, curwin->w_cursor)
1191# endif
1192 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001193 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001194# ifdef FEAT_AUTOCMD
1195 if (has_cursormoved())
1196 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1197 FALSE, curbuf);
1198# endif
1199# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001200 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001201 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001202# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001203 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001204# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001205 conceal_new_cursor_line = curwin->w_cursor.lnum;
1206 conceal_update_lines = TRUE;
1207 }
1208# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001209# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001210 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001211# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001212 }
1213#endif
1214
Bram Moolenaar186628f2013-03-19 13:33:23 +01001215#ifdef FEAT_AUTOCMD
1216 /* Trigger TextChanged if b_changedtick differs. */
1217 if (!finish_op && has_textchanged()
1218 && last_changedtick != curbuf->b_changedtick)
1219 {
1220 if (last_changedtick_buf == curbuf)
1221 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1222 FALSE, curbuf);
1223 last_changedtick_buf = curbuf;
1224 last_changedtick = curbuf->b_changedtick;
1225 }
1226#endif
1227
Bram Moolenaar33aec762006-01-22 23:30:12 +00001228#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1229 /* Scroll-binding for diff mode may have been postponed until
1230 * here. Avoids doing it for every change. */
1231 if (diff_need_scrollbind)
1232 {
1233 check_scrollbind((linenr_T)0, 0L);
1234 diff_need_scrollbind = FALSE;
1235 }
1236#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001237#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001238 /* Include a closed fold completely in the Visual area. */
1239 foldAdjustVisual();
1240#endif
1241#ifdef FEAT_FOLDING
1242 /*
1243 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1244 * contain the cursor.
1245 * When 'foldopen' is "all", open the fold(s) under the cursor.
1246 * This may mark the window for redrawing.
1247 */
1248 if (hasAnyFolding(curwin) && !char_avail())
1249 {
1250 foldCheckClose();
1251 if (fdo_flags & FDO_ALL)
1252 foldOpenCursor();
1253 }
1254#endif
1255
1256 /*
1257 * Before redrawing, make sure w_topline is correct, and w_leftcol
1258 * if lines don't wrap, and w_skipcol if lines wrap.
1259 */
1260 update_topline();
1261 validate_cursor();
1262
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001263 if (VIsual_active)
1264 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001265 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001266 update_screen(0);
1267 else if (redraw_cmdline || clear_cmdline)
1268 showmode();
1269#ifdef FEAT_WINDOWS
1270 redraw_statuslines();
1271#endif
1272#ifdef FEAT_TITLE
1273 if (need_maketitle)
1274 maketitle();
1275#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001276#ifdef FEAT_VIMINFO
1277 curbuf->b_last_used = vim_time();
1278#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001279 /* display message after redraw */
1280 if (keep_msg != NULL)
1281 {
1282 char_u *p;
1283
1284 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001285 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1286 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001287 p = keep_msg;
1288 msg_attr(p, keep_msg_attr);
1289 vim_free(p);
1290 }
1291 if (need_fileinfo) /* show file info after redraw */
1292 {
1293 fileinfo(FALSE, TRUE, FALSE);
1294 need_fileinfo = FALSE;
1295 }
1296
1297 emsg_on_display = FALSE; /* can delete error message now */
1298 did_emsg = FALSE;
1299 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001300 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001301 showruler(FALSE);
1302
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001303# if defined(FEAT_CONCEAL)
1304 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001305 && (conceal_old_cursor_line != conceal_new_cursor_line
1306 || conceal_cursor_line(curwin)
1307 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001308 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001309 if (conceal_old_cursor_line != conceal_new_cursor_line
1310 && conceal_old_cursor_line
1311 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001312 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001313 update_single_line(curwin, conceal_new_cursor_line);
1314 curwin->w_valid &= ~VALID_CROW;
1315 }
1316# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001317 setcursor();
1318 cursor_on();
1319
1320 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001321
1322#ifdef STARTUPTIME
1323 /* Now that we have drawn the first screen all the startup stuff
1324 * has been done, close any file for startup messages. */
1325 if (time_fd != NULL)
1326 {
1327 TIME_MSG("first screen update");
1328 TIME_MSG("--- VIM STARTED ---");
1329 fclose(time_fd);
1330 time_fd = NULL;
1331 }
1332#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001333 }
1334#ifdef FEAT_GUI
1335 if (need_mouse_correct)
1336 gui_mouse_correct();
1337#endif
1338
1339 /*
1340 * Update w_curswant if w_set_curswant has been set.
1341 * Postponed until here to avoid computing w_virtcol too often.
1342 */
1343 update_curswant();
1344
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001345#ifdef FEAT_EVAL
1346 /*
1347 * May perform garbage collection when waiting for a character, but
1348 * only at the very toplevel. Otherwise we may be using a List or
1349 * Dict internally somewhere.
1350 * "may_garbage_collect" is reset in vgetc() which is invoked through
1351 * do_exmode() and normal_cmd().
1352 */
1353 may_garbage_collect = (!cmdwin && !noexmode);
1354#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001355 /*
1356 * If we're invoked as ex, do a round of ex commands.
1357 * Otherwise, get and execute a normal mode command.
1358 */
1359 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001360 {
1361 if (noexmode) /* End of ":global/path/visual" commands */
1362 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001363 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001364 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001365 else
1366 normal_cmd(&oa, TRUE);
1367 }
1368}
1369
1370
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001371#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001372/*
1373 * Exit, but leave behind swap files for modified buffers.
1374 */
1375 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001376getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001377{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001378# if defined(SIGHUP) && defined(SIG_IGN)
1379 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1380 * makes Vim exit and then handling SIGHUP causes various reentrance
1381 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001382 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001383# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001384
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001385 ml_close_notmod(); /* close all not-modified buffers */
1386 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1387 ml_close_all(FALSE); /* close all memfiles, without deleting */
1388 getout(exitval); /* exit Vim properly */
1389}
1390#endif
1391
1392
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001393/*
1394 * Exit properly.
1395 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001396 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001397getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001398{
1399#ifdef FEAT_AUTOCMD
1400 buf_T *buf;
1401 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001402 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001403#endif
1404
1405 exiting = TRUE;
1406
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001407 /* When running in Ex mode an error causes us to exit with a non-zero exit
1408 * code. POSIX requires this, although it's not 100% clear from the
1409 * standard. */
1410 if (exmode_active)
1411 exitval += ex_exitval;
1412
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001413 /* Position the cursor on the last screen line, below all the text */
1414#ifdef FEAT_GUI
1415 if (!gui.in_use)
1416#endif
1417 windgoto((int)Rows - 1, 0);
1418
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001419#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1420 /* Optionally print hashtable efficiency. */
1421 hash_debug_results();
1422#endif
1423
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001424#ifdef FEAT_GUI
1425 msg_didany = FALSE;
1426#endif
1427
1428#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001429 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001430 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001431 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1432# if defined FEAT_WINDOWS
1433 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001434 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001435 next_tp = tp->tp_next;
1436 for (wp = (tp == curtab)
1437 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001438 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001439 if (wp->w_buffer == NULL)
1440 /* Autocmd must have close the buffer already, skip. */
1441 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001442 buf = wp->w_buffer;
1443 if (buf->b_changedtick != -1)
1444 {
1445 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1446 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001447 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001448 /* start all over, autocommands may mess up the lists */
1449 next_tp = first_tabpage;
1450 break;
1451 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001452 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001453 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001454# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001455 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1456 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001457# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001458
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001459 /* Trigger BufUnload for buffers that are loaded */
1460 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1461 if (buf->b_ml.ml_mfp != NULL)
1462 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001463 bufref_T bufref;
1464
1465 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001466 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001467 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001468 if (!bufref_valid(&bufref))
1469 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001470 break;
1471 }
1472 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1473 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001474#endif
1475
1476#ifdef FEAT_VIMINFO
1477 if (*p_viminfo != NUL)
1478 /* Write out the registers, history, marks etc, to the viminfo file */
1479 write_viminfo(NULL, FALSE);
1480#endif
1481
1482#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001483 if (get_vim_var_nr(VV_DYING) <= 1)
1484 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001485#endif
1486
Bram Moolenaar05159a02005-02-26 23:04:13 +00001487#ifdef FEAT_PROFILE
1488 profile_dump();
1489#endif
1490
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001491 if (did_emsg
1492#ifdef FEAT_GUI
1493 || (gui.in_use && msg_didany && p_verbose > 0)
1494#endif
1495 )
1496 {
1497 /* give the user a chance to read the (error) message */
1498 no_wait_return = FALSE;
1499 wait_return(FALSE);
1500 }
1501
1502#ifdef FEAT_AUTOCMD
1503 /* Position the cursor again, the autocommands may have moved it */
1504# ifdef FEAT_GUI
1505 if (!gui.in_use)
1506# endif
1507 windgoto((int)Rows - 1, 0);
1508#endif
1509
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001510#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001511 job_stop_on_exit();
1512#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001513#ifdef FEAT_LUA
1514 lua_end();
1515#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001516#ifdef FEAT_MZSCHEME
1517 mzscheme_end();
1518#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001519#ifdef FEAT_TCL
1520 tcl_end();
1521#endif
1522#ifdef FEAT_RUBY
1523 ruby_end();
1524#endif
1525#ifdef FEAT_PYTHON
1526 python_end();
1527#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001528#ifdef FEAT_PYTHON3
1529 python3_end();
1530#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001531#ifdef FEAT_PERL
1532 perl_end();
1533#endif
1534#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1535 iconv_end();
1536#endif
1537#ifdef FEAT_NETBEANS_INTG
1538 netbeans_end();
1539#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001540#ifdef FEAT_CSCOPE
1541 cs_end();
1542#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001543#ifdef FEAT_EVAL
1544 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001545 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001546#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001547#if defined(WIN32) && defined(FEAT_MBYTE)
1548 free_cmd_argsW();
1549#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001550
1551 mch_exit(exitval);
1552}
1553
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001554#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001555/*
1556 * Get a (optional) count for a Vim argument.
1557 */
1558 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001559get_number_arg(
1560 char_u *p, /* pointer to argument */
1561 int *idx, /* index in argument, is incremented */
1562 int def) /* default value */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001563{
1564 if (vim_isdigit(p[*idx]))
1565 {
1566 def = atoi((char *)&(p[*idx]));
1567 while (vim_isdigit(p[*idx]))
1568 *idx = *idx + 1;
1569 }
1570 return def;
1571}
1572
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001573#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1574/*
1575 * Setup to use the current locale (for ctype() and many other things).
1576 */
1577 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001578init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001579{
1580 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001581
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001582# ifdef FEAT_GUI_GTK
1583 /* Tell Gtk not to change our locale settings. */
1584 gtk_disable_setlocale();
1585# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001586# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1587 /* Make sure strtod() uses a decimal point, not a comma. */
1588 setlocale(LC_NUMERIC, "C");
1589# endif
1590
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001591# ifdef WIN32
1592 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1593 * text while it's expecting text in the current locale. This call avoids
1594 * that. */
1595 setlocale(LC_CTYPE, "C");
1596# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001597
1598# ifdef FEAT_GETTEXT
1599 {
1600 int mustfree = FALSE;
1601 char_u *p;
1602
1603# ifdef DYNAMIC_GETTEXT
1604 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001605 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001606# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001607 /* expand_env() doesn't work yet, because g_chartab[] is not
1608 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001609 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1610 if (p != NULL && *p != NUL)
1611 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001612 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001613 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1614 }
1615 if (mustfree)
1616 vim_free(p);
1617 textdomain(VIMPACKAGE);
1618 }
1619# endif
1620}
1621#endif
1622
1623/*
1624 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1625 * If the executable name starts with "r" we disable shell commands.
1626 * If the next character is "e" we run in Easy mode.
1627 * If the next character is "g" we run the GUI version.
1628 * If the next characters are "view" we start in readonly mode.
1629 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1630 * If the next characters are "ex" we start in Ex mode. If it's followed
1631 * by "im" use improved Ex mode.
1632 */
1633 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001634parse_command_name(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001635{
1636 char_u *initstr;
1637
1638 initstr = gettail((char_u *)parmp->argv[0]);
1639
1640#ifdef MACOS_X_UNIX
1641 /* An issue has been seen when launching Vim in such a way that
1642 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1643 * executable or a symbolic link of it. Until this issue is resolved
1644 * we prohibit the GUI from being used.
1645 */
1646 if (STRCMP(initstr, parmp->argv[0]) == 0)
1647 disallow_gui = TRUE;
1648
1649 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001650 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001651#endif
1652
1653#ifdef FEAT_EVAL
1654 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaara1706c92014-04-01 19:55:49 +02001655 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001656#endif
1657
1658 if (TOLOWER_ASC(initstr[0]) == 'r')
1659 {
1660 restricted = TRUE;
1661 ++initstr;
1662 }
1663
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001664 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001665 if (TOLOWER_ASC(initstr[0]) == 'e'
1666 && (TOLOWER_ASC(initstr[1]) == 'v'
1667 || TOLOWER_ASC(initstr[1]) == 'g'))
1668 {
1669#ifdef FEAT_GUI
1670 gui.starting = TRUE;
1671#endif
1672 parmp->evim_mode = TRUE;
1673 ++initstr;
1674 }
1675
Bram Moolenaar806875d2008-09-18 18:57:10 +00001676 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1677 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001678 {
1679 main_start_gui();
1680#ifdef FEAT_GUI
1681 ++initstr;
1682#endif
1683 }
1684
1685 if (STRNICMP(initstr, "view", 4) == 0)
1686 {
1687 readonlymode = TRUE;
1688 curbuf->b_p_ro = TRUE;
1689 p_uc = 10000; /* don't update very often */
1690 initstr += 4;
1691 }
1692 else if (STRNICMP(initstr, "vim", 3) == 0)
1693 initstr += 3;
1694
1695 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1696 if (STRICMP(initstr, "diff") == 0)
1697 {
1698#ifdef FEAT_DIFF
1699 parmp->diff_mode = TRUE;
1700#else
1701 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1702 mch_errmsg("\n");
1703 mch_exit(2);
1704#endif
1705 }
1706
1707 if (STRNICMP(initstr, "ex", 2) == 0)
1708 {
1709 if (STRNICMP(initstr + 2, "im", 2) == 0)
1710 exmode_active = EXMODE_VIM;
1711 else
1712 exmode_active = EXMODE_NORMAL;
1713 change_compatible(TRUE); /* set 'compatible' */
1714 }
1715}
1716
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001717/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001718 * Get the name of the display, before gui_prepare() removes it from
1719 * argv[]. Used for the xterm-clipboard display.
1720 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001721 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001722 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001723 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001724early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001725{
Bram Moolenaar03005972008-11-20 13:12:36 +00001726#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1727 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001728 int argc = parmp->argc;
1729 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001730 int i;
1731
1732 for (i = 1; i < argc; i++)
1733 {
1734 if (STRCMP(argv[i], "--") == 0)
1735 break;
1736# ifdef FEAT_XCLIPBOARD
1737 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001738# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001739 || STRICMP(argv[i], "--display") == 0
1740# endif
1741 )
1742 {
1743 if (i == argc - 1)
1744 mainerr_arg_missing((char_u *)argv[i]);
1745 xterm_display = argv[++i];
1746 }
1747# endif
1748# ifdef FEAT_CLIENTSERVER
1749 else if (STRICMP(argv[i], "--servername") == 0)
1750 {
1751 if (i == argc - 1)
1752 mainerr_arg_missing((char_u *)argv[i]);
1753 parmp->serverName_arg = (char_u *)argv[++i];
1754 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001755 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001756 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001757 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001758 {
1759 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001760# ifdef FEAT_GUI
1761 if (strstr(argv[i], "-wait") != 0)
1762 /* don't fork() when starting the GUI to edit files ourself */
1763 gui.dofork = FALSE;
1764# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001765 }
1766# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001767
1768# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1769# ifdef FEAT_GUI_W32
1770 else if (STRICMP(argv[i], "--windowid") == 0)
1771# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001772 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001773# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001774 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001775 long_u id;
1776 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001777
1778 if (i == argc - 1)
1779 mainerr_arg_missing((char_u *)argv[i]);
1780 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001781 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001782 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001783 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001784 if (count != 1)
1785 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1786 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001787# ifdef FEAT_GUI_W32
1788 win_socket_id = id;
1789# else
1790 gtk_socket_id = id;
1791# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001792 i++;
1793 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001794# endif
1795# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001796 else if (STRICMP(argv[i], "--echo-wid") == 0)
1797 echo_wid_arg = TRUE;
1798# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001799# ifndef FEAT_NETBEANS_INTG
1800 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001801 {
1802 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1803 mch_exit(2);
1804 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001805# endif
1806
Bram Moolenaar58d98232005-07-23 22:25:46 +00001807 }
1808#endif
1809}
1810
1811/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001812 * Scan the command line arguments.
1813 */
1814 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001815command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001816{
1817 int argc = parmp->argc;
1818 char **argv = parmp->argv;
1819 int argv_idx; /* index in argv[n][] */
1820 int had_minmin = FALSE; /* found "--" argument */
1821 int want_argument; /* option argument with argument */
1822 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001823 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001824 long n;
1825
1826 --argc;
1827 ++argv;
1828 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1829 while (argc > 0)
1830 {
1831 /*
1832 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1833 */
1834 if (argv[0][0] == '+' && !had_minmin)
1835 {
1836 if (parmp->n_commands >= MAX_ARG_CMDS)
1837 mainerr(ME_EXTRA_CMD, NULL);
1838 argv_idx = -1; /* skip to next argument */
1839 if (argv[0][1] == NUL)
1840 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1841 else
1842 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1843 }
1844
1845 /*
1846 * Optional argument.
1847 */
1848 else if (argv[0][0] == '-' && !had_minmin)
1849 {
1850 want_argument = FALSE;
1851 c = argv[0][argv_idx++];
1852#ifdef VMS
1853 /*
1854 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1855 * and "-/X" as "-X".
1856 */
1857 if (c == '/')
1858 {
1859 c = argv[0][argv_idx++];
1860 c = TOUPPER_ASC(c);
1861 }
1862 else
1863 c = TOLOWER_ASC(c);
1864#endif
1865 switch (c)
1866 {
1867 case NUL: /* "vim -" read from stdin */
1868 /* "ex -" silent mode */
1869 if (exmode_active)
1870 silent_mode = TRUE;
1871 else
1872 {
1873 if (parmp->edit_type != EDIT_NONE)
1874 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1875 parmp->edit_type = EDIT_STDIN;
1876 read_cmd_fd = 2; /* read from stderr instead of stdin */
1877 }
1878 argv_idx = -1; /* skip to next argument */
1879 break;
1880
1881 case '-': /* "--" don't take any more option arguments */
1882 /* "--help" give help message */
1883 /* "--version" give version message */
1884 /* "--literal" take files literally */
1885 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001886 /* "--not-a-term" don't warn for not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001887 /* "--noplugin[s]" skip plugins */
1888 /* "--cmd <cmd>" execute cmd before vimrc */
1889 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1890 usage();
1891 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1892 {
1893 Columns = 80; /* need to init Columns */
1894 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1895 list_version();
1896 msg_putchar('\n');
1897 msg_didout = FALSE;
1898 mch_exit(0);
1899 }
1900 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1901 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001902#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001903 parmp->literal = TRUE;
1904#endif
1905 }
1906 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1907 {
1908#ifdef FEAT_GUI
1909 gui.dofork = FALSE; /* don't fork() when starting GUI */
1910#endif
1911 }
1912 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1913 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001914 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1915 parmp->not_a_term = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001916 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1917 {
1918 want_argument = TRUE;
1919 argv_idx += 3;
1920 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001921 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1922 {
1923 want_argument = TRUE;
1924 argv_idx += 11;
1925 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001926#ifdef FEAT_CLIENTSERVER
1927 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1928 ; /* already processed -- no arg */
1929 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1930 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1931 {
1932 /* already processed -- snatch the following arg */
1933 if (argc > 1)
1934 {
1935 --argc;
1936 ++argv;
1937 }
1938 }
1939#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001940#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1941# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001942 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001943# else
1944 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1945# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001946 {
1947 /* already processed -- snatch the following arg */
1948 if (argc > 1)
1949 {
1950 --argc;
1951 ++argv;
1952 }
1953 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001954#endif
1955#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001956 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1957 {
1958 /* already processed, skip */
1959 }
1960#endif
1961 else
1962 {
1963 if (argv[0][argv_idx])
1964 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1965 had_minmin = TRUE;
1966 }
1967 if (!want_argument)
1968 argv_idx = -1; /* skip to next argument */
1969 break;
1970
1971 case 'A': /* "-A" start in Arabic mode */
1972#ifdef FEAT_ARABIC
1973 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1974#else
1975 mch_errmsg(_(e_noarabic));
1976 mch_exit(2);
1977#endif
1978 break;
1979
1980 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001981 /* Needs to be effective before expanding file names, because
1982 * for Win32 this makes us edit a shortcut file itself,
1983 * instead of the file it links to. */
1984 set_options_bin(curbuf->b_p_bin, 1, 0);
1985 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001986 break;
1987
1988 case 'C': /* "-C" Compatible */
1989 change_compatible(TRUE);
1990 break;
1991
1992 case 'e': /* "-e" Ex mode */
1993 exmode_active = EXMODE_NORMAL;
1994 break;
1995
1996 case 'E': /* "-E" Improved Ex mode */
1997 exmode_active = EXMODE_VIM;
1998 break;
1999
2000 case 'f': /* "-f" GUI: run in foreground. Amiga: open
2001 window directly, not with newcli */
2002#ifdef FEAT_GUI
2003 gui.dofork = FALSE; /* don't fork() when starting GUI */
2004#endif
2005 break;
2006
2007 case 'g': /* "-g" start GUI */
2008 main_start_gui();
2009 break;
2010
2011 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
2012#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002013 p_fkmap = TRUE;
2014 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002015#else
2016 mch_errmsg(_(e_nofarsi));
2017 mch_exit(2);
2018#endif
2019 break;
2020
2021 case 'h': /* "-h" give help message */
2022#ifdef FEAT_GUI_GNOME
2023 /* Tell usage() to exit for "gvim". */
2024 gui.starting = FALSE;
2025#endif
2026 usage();
2027 break;
2028
2029 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2030#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002031 p_hkmap = TRUE;
2032 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002033#else
2034 mch_errmsg(_(e_nohebrew));
2035 mch_exit(2);
2036#endif
2037 break;
2038
2039 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2040#ifdef FEAT_LISP
2041 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2042 p_sm = TRUE;
2043#endif
2044 break;
2045
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002046 case 'M': /* "-M" no changes or writing of files */
2047 reset_modifiable();
2048 /* FALLTHROUGH */
2049
2050 case 'm': /* "-m" no writing of files */
2051 p_write = FALSE;
2052 break;
2053
2054 case 'y': /* "-y" easy mode */
2055#ifdef FEAT_GUI
2056 gui.starting = TRUE; /* start GUI a bit later */
2057#endif
2058 parmp->evim_mode = TRUE;
2059 break;
2060
2061 case 'N': /* "-N" Nocompatible */
2062 change_compatible(FALSE);
2063 break;
2064
2065 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002066#ifdef FEAT_NETBEANS_INTG
2067 /* checking for "-nb", netbeans parameters */
2068 if (argv[0][argv_idx] == 'b')
2069 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002070 netbeansArg = argv[0];
2071 argv_idx = -1; /* skip to next argument */
2072 }
2073 else
2074#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002075 parmp->no_swap_file = TRUE;
2076 break;
2077
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002078 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002079#ifdef TARGET_API_MAC_OSX
2080 /* For some reason on MacOS X, an argument like:
2081 -psn_0_10223617 is passed in when invoke from Finder
2082 or with the 'open' command */
2083 if (argv[0][argv_idx] == 's')
2084 {
2085 argv_idx = -1; /* bypass full -psn */
2086 main_start_gui();
2087 break;
2088 }
2089#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002090#ifdef FEAT_WINDOWS
2091 /* default is 0: open window for each file */
2092 parmp->window_count = get_number_arg((char_u *)argv[0],
2093 &argv_idx, 0);
2094 parmp->window_layout = WIN_TABS;
2095#endif
2096 break;
2097
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002098 case 'o': /* "-o[N]" open N horizontal split windows */
2099#ifdef FEAT_WINDOWS
2100 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002101 parmp->window_count = get_number_arg((char_u *)argv[0],
2102 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002103 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002104#endif
2105 break;
2106
2107 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002108#ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002109 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002110 parmp->window_count = get_number_arg((char_u *)argv[0],
2111 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002112 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002113#endif
2114 break;
2115
2116#ifdef FEAT_QUICKFIX
2117 case 'q': /* "-q" QuickFix mode */
2118 if (parmp->edit_type != EDIT_NONE)
2119 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2120 parmp->edit_type = EDIT_QF;
2121 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2122 {
2123 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2124 argv_idx = -1;
2125 }
2126 else if (argc > 1) /* "-q {errorfile}" */
2127 want_argument = TRUE;
2128 break;
2129#endif
2130
2131 case 'R': /* "-R" readonly mode */
2132 readonlymode = TRUE;
2133 curbuf->b_p_ro = TRUE;
2134 p_uc = 10000; /* don't update very often */
2135 break;
2136
2137 case 'r': /* "-r" recovery mode */
2138 case 'L': /* "-L" recovery mode */
2139 recoverymode = 1;
2140 break;
2141
2142 case 's':
2143 if (exmode_active) /* "-s" silent (batch) mode */
2144 silent_mode = TRUE;
2145 else /* "-s {scriptin}" read from script file */
2146 want_argument = TRUE;
2147 break;
2148
2149 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2150 if (parmp->edit_type != EDIT_NONE)
2151 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2152 parmp->edit_type = EDIT_TAG;
2153 if (argv[0][argv_idx]) /* "-t{tag}" */
2154 {
2155 parmp->tagname = (char_u *)argv[0] + argv_idx;
2156 argv_idx = -1;
2157 }
2158 else /* "-t {tag}" */
2159 want_argument = TRUE;
2160 break;
2161
2162#ifdef FEAT_EVAL
2163 case 'D': /* "-D" Debugging */
2164 parmp->use_debug_break_level = 9999;
2165 break;
2166#endif
2167#ifdef FEAT_DIFF
2168 case 'd': /* "-d" 'diff' */
2169# ifdef AMIGA
2170 /* check for "-dev {device}" */
2171 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2172 want_argument = TRUE;
2173 else
2174# endif
2175 parmp->diff_mode = TRUE;
2176 break;
2177#endif
2178 case 'V': /* "-V{N}" Verbose level */
2179 /* default is 10: a little bit verbose */
2180 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2181 if (argv[0][argv_idx] != NUL)
2182 {
2183 set_option_value((char_u *)"verbosefile", 0L,
2184 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002185 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002186 }
2187 break;
2188
2189 case 'v': /* "-v" Vi-mode (as if called "vi") */
2190 exmode_active = 0;
2191#ifdef FEAT_GUI
2192 gui.starting = FALSE; /* don't start GUI */
2193#endif
2194 break;
2195
2196 case 'w': /* "-w{number}" set window height */
2197 /* "-w {scriptout}" write to script */
2198 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2199 {
2200 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2201 set_option_value((char_u *)"window", n, NULL, 0);
2202 break;
2203 }
2204 want_argument = TRUE;
2205 break;
2206
2207#ifdef FEAT_CRYPT
2208 case 'x': /* "-x" encrypted reading/writing of files */
2209 parmp->ask_for_key = TRUE;
2210 break;
2211#endif
2212
2213 case 'X': /* "-X" don't connect to X server */
2214#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2215 x_no_connect = TRUE;
2216#endif
2217 break;
2218
2219 case 'Z': /* "-Z" restricted mode */
2220 restricted = TRUE;
2221 break;
2222
2223 case 'c': /* "-c{command}" or "-c {command}" execute
2224 command */
2225 if (argv[0][argv_idx] != NUL)
2226 {
2227 if (parmp->n_commands >= MAX_ARG_CMDS)
2228 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002229 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2230 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002231 argv_idx = -1;
2232 break;
2233 }
2234 /*FALLTHROUGH*/
2235 case 'S': /* "-S {file}" execute Vim script */
2236 case 'i': /* "-i {viminfo}" use for viminfo */
2237#ifndef FEAT_DIFF
2238 case 'd': /* "-d {device}" device (for Amiga) */
2239#endif
2240 case 'T': /* "-T {terminal}" terminal name */
2241 case 'u': /* "-u {vimrc}" vim inits file */
2242 case 'U': /* "-U {gvimrc}" gvim inits file */
2243 case 'W': /* "-W {scriptout}" overwrite */
2244#ifdef FEAT_GUI_W32
2245 case 'P': /* "-P {parent title}" MDI parent */
2246#endif
2247 want_argument = TRUE;
2248 break;
2249
2250 default:
2251 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2252 }
2253
2254 /*
2255 * Handle option arguments with argument.
2256 */
2257 if (want_argument)
2258 {
2259 /*
2260 * Check for garbage immediately after the option letter.
2261 */
2262 if (argv[0][argv_idx] != NUL)
2263 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2264
2265 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002266 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002267 mainerr_arg_missing((char_u *)argv[0]);
2268 ++argv;
2269 argv_idx = -1;
2270
2271 switch (c)
2272 {
2273 case 'c': /* "-c {command}" execute command */
2274 case 'S': /* "-S {file}" execute Vim script */
2275 if (parmp->n_commands >= MAX_ARG_CMDS)
2276 mainerr(ME_EXTRA_CMD, NULL);
2277 if (c == 'S')
2278 {
2279 char *a;
2280
2281 if (argc < 1)
2282 /* "-S" without argument: use default session file
2283 * name. */
2284 a = SESSION_FILE;
2285 else if (argv[0][0] == '-')
2286 {
2287 /* "-S" followed by another option: use default
2288 * session file name. */
2289 a = SESSION_FILE;
2290 ++argc;
2291 --argv;
2292 }
2293 else
2294 a = argv[0];
2295 p = alloc((unsigned)(STRLEN(a) + 4));
2296 if (p == NULL)
2297 mch_exit(2);
2298 sprintf((char *)p, "so %s", a);
2299 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2300 parmp->commands[parmp->n_commands++] = p;
2301 }
2302 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002303 parmp->commands[parmp->n_commands++] =
2304 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002305 break;
2306
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002307 case '-':
2308 if (argv[-1][2] == 'c')
2309 {
2310 /* "--cmd {command}" execute command */
2311 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2312 mainerr(ME_EXTRA_CMD, NULL);
2313 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002314 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002315 }
2316 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002317 break;
2318
2319 /* case 'd': -d {device} is handled in mch_check_win() for the
2320 * Amiga */
2321
2322#ifdef FEAT_QUICKFIX
2323 case 'q': /* "-q {errorfile}" QuickFix mode */
2324 parmp->use_ef = (char_u *)argv[0];
2325 break;
2326#endif
2327
2328 case 'i': /* "-i {viminfo}" use for viminfo */
2329 use_viminfo = (char_u *)argv[0];
2330 break;
2331
2332 case 's': /* "-s {scriptin}" read from script file */
2333 if (scriptin[0] != NULL)
2334 {
2335scripterror:
2336 mch_errmsg(_("Attempt to open script file again: \""));
2337 mch_errmsg(argv[-1]);
2338 mch_errmsg(" ");
2339 mch_errmsg(argv[0]);
2340 mch_errmsg("\"\n");
2341 mch_exit(2);
2342 }
2343 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2344 {
2345 mch_errmsg(_("Cannot open for reading: \""));
2346 mch_errmsg(argv[0]);
2347 mch_errmsg("\"\n");
2348 mch_exit(2);
2349 }
2350 if (save_typebuf() == FAIL)
2351 mch_exit(2); /* out of memory */
2352 break;
2353
2354 case 't': /* "-t {tag}" */
2355 parmp->tagname = (char_u *)argv[0];
2356 break;
2357
2358 case 'T': /* "-T {terminal}" terminal name */
2359 /*
2360 * The -T term argument is always available and when
2361 * HAVE_TERMLIB is supported it overrides the environment
2362 * variable TERM.
2363 */
2364#ifdef FEAT_GUI
2365 if (term_is_gui((char_u *)argv[0]))
2366 gui.starting = TRUE; /* start GUI a bit later */
2367 else
2368#endif
2369 parmp->term = (char_u *)argv[0];
2370 break;
2371
2372 case 'u': /* "-u {vimrc}" vim inits file */
2373 parmp->use_vimrc = (char_u *)argv[0];
2374 break;
2375
2376 case 'U': /* "-U {gvimrc}" gvim inits file */
2377#ifdef FEAT_GUI
2378 use_gvimrc = (char_u *)argv[0];
2379#endif
2380 break;
2381
2382 case 'w': /* "-w {nr}" 'window' value */
2383 /* "-w {scriptout}" append to script file */
2384 if (vim_isdigit(*((char_u *)argv[0])))
2385 {
2386 argv_idx = 0;
2387 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2388 set_option_value((char_u *)"window", n, NULL, 0);
2389 argv_idx = -1;
2390 break;
2391 }
2392 /*FALLTHROUGH*/
2393 case 'W': /* "-W {scriptout}" overwrite script file */
2394 if (scriptout != NULL)
2395 goto scripterror;
2396 if ((scriptout = mch_fopen(argv[0],
2397 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2398 {
2399 mch_errmsg(_("Cannot open for script output: \""));
2400 mch_errmsg(argv[0]);
2401 mch_errmsg("\"\n");
2402 mch_exit(2);
2403 }
2404 break;
2405
2406#ifdef FEAT_GUI_W32
2407 case 'P': /* "-P {parent title}" MDI parent */
2408 gui_mch_set_parent(argv[0]);
2409 break;
2410#endif
2411 }
2412 }
2413 }
2414
2415 /*
2416 * File name argument.
2417 */
2418 else
2419 {
2420 argv_idx = -1; /* skip to next argument */
2421
2422 /* Check for only one type of editing. */
2423 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2424 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2425 parmp->edit_type = EDIT_FILE;
2426
2427#ifdef MSWIN
2428 /* Remember if the argument was a full path before changing
2429 * slashes to backslashes. */
2430 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2431 parmp->full_path = TRUE;
2432#endif
2433
2434 /* Add the file to the global argument list. */
2435 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2436 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2437 mch_exit(2);
2438#ifdef FEAT_DIFF
2439 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2440 && !mch_isdir(alist_name(&GARGLIST[0])))
2441 {
2442 char_u *r;
2443
2444 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2445 if (r != NULL)
2446 {
2447 vim_free(p);
2448 p = r;
2449 }
2450 }
2451#endif
2452#if defined(__CYGWIN32__) && !defined(WIN32)
2453 /*
2454 * If vim is invoked by non-Cygwin tools, convert away any
2455 * DOS paths, so things like .swp files are created correctly.
2456 * Look for evidence of non-Cygwin paths before we bother.
2457 * This is only for when using the Unix files.
2458 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002459 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002460 {
2461 char posix_path[PATH_MAX];
2462
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002463# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2464 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2465# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002466 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002467# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002468 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002469 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002470 if (p == NULL)
2471 mch_exit(2);
2472 }
2473#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002474
2475#ifdef USE_FNAME_CASE
2476 /* Make the case of the file name match the actual file. */
2477 fname_case(p, 0);
2478#endif
2479
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002480 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002481#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002482 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002483#else
2484 2 /* add buffer number now and use curbuf */
2485#endif
2486 );
2487
2488#if defined(FEAT_MBYTE) && defined(WIN32)
2489 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002490 /* Remember this argument has been added to the argument list.
2491 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002492 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002493# ifdef FEAT_DIFF
2494 parmp->diff_mode
2495# else
2496 FALSE
2497# endif
2498 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002499 }
2500#endif
2501 }
2502
2503 /*
2504 * If there are no more letters after the current "-", go to next
2505 * argument. argv_idx is set to -1 when the current argument is to be
2506 * skipped.
2507 */
2508 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2509 {
2510 --argc;
2511 ++argv;
2512 argv_idx = 1;
2513 }
2514 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002515
2516#ifdef FEAT_EVAL
2517 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2518 * one. */
2519 if (parmp->n_commands > 0)
2520 {
2521 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2522 if (p != NULL)
2523 {
2524 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2525 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2526 vim_free(p);
2527 }
2528 }
2529#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002530}
2531
2532/*
2533 * Print a warning if stdout is not a terminal.
2534 * When starting in Ex mode and commands come from a file, set Silent mode.
2535 */
2536 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002537check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002538{
2539 int input_isatty; /* is active input a terminal? */
2540
2541 input_isatty = mch_input_isatty();
2542 if (exmode_active)
2543 {
2544 if (!input_isatty)
2545 silent_mode = TRUE;
2546 }
2547 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2548#ifdef FEAT_GUI
2549 /* don't want the delay when started from the desktop */
2550 && !gui.starting
2551#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002552 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002553 {
2554#ifdef NBDEBUG
2555 /*
2556 * This shouldn't be necessary. But if I run netbeans with the log
2557 * output coming to the console and XOpenDisplay fails, I get vim
2558 * trying to start with input/output to my console tty. This fills my
2559 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002560 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002561 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002562 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002563 {
2564 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2565 exit(1);
2566 }
2567#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002568#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2569 if (is_cygpty_used())
2570 {
2571 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2572 exit(1);
2573 }
2574#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002575 if (!parmp->stdout_isatty)
2576 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2577 if (!input_isatty)
2578 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2579 out_flush();
2580 if (scriptin[0] == NULL)
2581 ui_delay(2000L, TRUE);
2582 TIME_MSG("Warning delay");
2583 }
2584}
2585
2586/*
2587 * Read text from stdin.
2588 */
2589 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002590read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002591{
2592 int i;
2593
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002594#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002595 /* When getting the ATTENTION prompt here, use a dialog */
2596 swap_exists_action = SEA_DIALOG;
2597#endif
2598 no_wait_return = TRUE;
2599 i = msg_didany;
2600 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002601 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002602 no_wait_return = FALSE;
2603 msg_didany = i;
2604 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002605#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002606 check_swap_exists_action();
2607#endif
2608#if !(defined(AMIGA) || defined(MACOS))
2609 /*
2610 * Close stdin and dup it from stderr. Required for GPM to work
2611 * properly, and for running external commands.
2612 * Is there any other system that cannot do this?
2613 */
2614 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002615 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002616#endif
2617}
2618
2619/*
2620 * Create the requested number of windows and edit buffers in them.
2621 * Also does recovery if "recoverymode" set.
2622 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002623 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002624create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002625{
2626#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002627 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002628 int done = 0;
2629
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002630 /*
2631 * Create the number of windows that was requested.
2632 */
2633 if (parmp->window_count == -1) /* was not set */
2634 parmp->window_count = 1;
2635 if (parmp->window_count == 0)
2636 parmp->window_count = GARGCOUNT;
2637 if (parmp->window_count > 1)
2638 {
2639 /* Don't change the windows if there was a command in .vimrc that
2640 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002641 if (parmp->window_layout == 0)
2642 parmp->window_layout = WIN_HOR;
2643 if (parmp->window_layout == WIN_TABS)
2644 {
2645 parmp->window_count = make_tabpages(parmp->window_count);
2646 TIME_MSG("making tab pages");
2647 }
2648 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002649 {
2650 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002651 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002652 TIME_MSG("making windows");
2653 }
2654 else
2655 parmp->window_count = win_count();
2656 }
2657 else
2658 parmp->window_count = 1;
2659#endif
2660
2661 if (recoverymode) /* do recover */
2662 {
2663 msg_scroll = TRUE; /* scroll message up */
2664 ml_recover();
2665 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2666 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002667 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002668 }
2669 else
2670 {
2671 /*
2672 * Open a buffer for windows that don't have one yet.
2673 * Commands in the .vimrc might have loaded a file or split the window.
2674 * Watch out for autocommands that delete a window.
2675 */
2676#ifdef FEAT_AUTOCMD
2677 /*
2678 * Don't execute Win/Buf Enter/Leave autocommands here
2679 */
2680 ++autocmd_no_enter;
2681 ++autocmd_no_leave;
2682#endif
2683#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002684 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002685 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002686 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002687 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002688 {
2689 if (parmp->window_layout == WIN_TABS)
2690 goto_tabpage(1);
2691 else
2692 curwin = firstwin;
2693 }
2694 else if (parmp->window_layout == WIN_TABS)
2695 {
2696 if (curtab->tp_next == NULL)
2697 break;
2698 goto_tabpage(0);
2699 }
2700 else
2701 {
2702 if (curwin->w_next == NULL)
2703 break;
2704 curwin = curwin->w_next;
2705 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002706 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002707#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002708 curbuf = curwin->w_buffer;
2709 if (curbuf->b_ml.ml_mfp == NULL)
2710 {
2711#ifdef FEAT_FOLDING
2712 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2713 if (p_fdls >= 0)
2714 curwin->w_p_fdl = p_fdls;
2715#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002716#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002717 /* When getting the ATTENTION prompt here, use a dialog */
2718 swap_exists_action = SEA_DIALOG;
2719#endif
2720 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002721
2722 /* create memfile, read file */
2723 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002724
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002725#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002726 if (swap_exists_action == SEA_QUIT)
2727 {
2728 if (got_int || only_one_window())
2729 {
2730 /* abort selected or quit and only one window */
2731 did_emsg = FALSE; /* avoid hit-enter prompt */
2732 getout(1);
2733 }
2734 /* We can't close the window, it would disturb what
2735 * happens next. Clear the file name and set the arg
2736 * index to -1 to delete it later. */
2737 setfname(curbuf, NULL, NULL, FALSE);
2738 curwin->w_arg_idx = -1;
2739 swap_exists_action = SEA_NONE;
2740 }
2741 else
2742 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002743#endif
2744#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002745 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002746#endif
2747 }
2748#ifdef FEAT_WINDOWS
2749 ui_breakcheck();
2750 if (got_int)
2751 {
2752 (void)vgetc(); /* only break the file loading, not the rest */
2753 break;
2754 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002755 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002756#endif
2757#ifdef FEAT_WINDOWS
2758 if (parmp->window_layout == WIN_TABS)
2759 goto_tabpage(1);
2760 else
2761 curwin = firstwin;
2762 curbuf = curwin->w_buffer;
2763#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002764#ifdef FEAT_AUTOCMD
2765 --autocmd_no_enter;
2766 --autocmd_no_leave;
2767#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002768 }
2769}
2770
2771#ifdef FEAT_WINDOWS
2772 /*
2773 * If opened more than one window, start editing files in the other
2774 * windows. make_windows() has already opened the windows.
2775 */
2776 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002777edit_buffers(
2778 mparm_T *parmp,
2779 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002780{
2781 int arg_idx; /* index in argument list */
2782 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002783 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002784 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002785
2786# ifdef FEAT_AUTOCMD
2787 /*
2788 * Don't execute Win/Buf Enter/Leave autocommands here
2789 */
2790 ++autocmd_no_enter;
2791 ++autocmd_no_leave;
2792# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002793
2794 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2795 if (curwin->w_arg_idx == -1)
2796 {
2797 win_close(curwin, TRUE);
2798 advance = FALSE;
2799 }
2800
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002801 arg_idx = 1;
2802 for (i = 1; i < parmp->window_count; ++i)
2803 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002804 if (cwd != NULL)
2805 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002806 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2807 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002808 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002809 ++arg_idx;
2810 win_close(curwin, TRUE);
2811 advance = FALSE;
2812 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002813 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002814
Bram Moolenaar84212822006-11-07 21:59:47 +00002815 if (advance)
2816 {
2817 if (parmp->window_layout == WIN_TABS)
2818 {
2819 if (curtab->tp_next == NULL) /* just checking */
2820 break;
2821 goto_tabpage(0);
2822 }
2823 else
2824 {
2825 if (curwin->w_next == NULL) /* just checking */
2826 break;
2827 win_enter(curwin->w_next, FALSE);
2828 }
2829 }
2830 advance = TRUE;
2831
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002832 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002833 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002834 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2835 {
2836 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002837 /* Edit file from arg list, if there is one. When "Quit" selected
2838 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002839# ifdef HAS_SWAP_EXISTS_ACTION
2840 swap_exists_did_quit = FALSE;
2841# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002842 (void)do_ecmd(0, arg_idx < GARGCOUNT
2843 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002844 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002845# ifdef HAS_SWAP_EXISTS_ACTION
2846 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002847 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002848 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002849 if (got_int || only_one_window())
2850 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002851 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002852 did_emsg = FALSE; /* avoid hit-enter prompt */
2853 getout(1);
2854 }
2855 win_close(curwin, TRUE);
2856 advance = FALSE;
2857 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002858# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002859 if (arg_idx == GARGCOUNT - 1)
2860 arg_had_last = TRUE;
2861 ++arg_idx;
2862 }
2863 ui_breakcheck();
2864 if (got_int)
2865 {
2866 (void)vgetc(); /* only break the file loading, not the rest */
2867 break;
2868 }
2869 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002870
2871 if (parmp->window_layout == WIN_TABS)
2872 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002873# ifdef FEAT_AUTOCMD
2874 --autocmd_no_enter;
2875# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002876
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002877 /* make the first window the current window */
2878 win = firstwin;
2879#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2880 /* Avoid making a preview window the current window. */
2881 while (win->w_p_pvw)
2882 {
2883 win = win->w_next;
2884 if (win == NULL)
2885 {
2886 win = firstwin;
2887 break;
2888 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002889 }
2890#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002891 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002892
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002893# ifdef FEAT_AUTOCMD
2894 --autocmd_no_leave;
2895# endif
2896 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002897 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002898 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2899}
2900#endif /* FEAT_WINDOWS */
2901
2902/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002903 * Execute the commands from --cmd arguments "cmds[cnt]".
2904 */
2905 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002906exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002907{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002908 char_u **cmds = parmp->pre_commands;
2909 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002910 int i;
2911
2912 if (cnt > 0)
2913 {
2914 curwin->w_cursor.lnum = 0; /* just in case.. */
2915 sourcing_name = (char_u *)_("pre-vimrc command line");
2916# ifdef FEAT_EVAL
2917 current_SID = SID_CMDARG;
2918# endif
2919 for (i = 0; i < cnt; ++i)
2920 do_cmdline_cmd(cmds[i]);
2921 sourcing_name = NULL;
2922# ifdef FEAT_EVAL
2923 current_SID = 0;
2924# endif
2925 TIME_MSG("--cmd commands");
2926 }
2927}
2928
2929/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002930 * Execute "+", "-c" and "-S" arguments.
2931 */
2932 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002933exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002934{
2935 int i;
2936
2937 /*
2938 * We start commands on line 0, make "vim +/pat file" match a
2939 * pattern on line 1. But don't move the cursor when an autocommand
2940 * with g`" was used.
2941 */
2942 msg_scroll = TRUE;
2943 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2944 curwin->w_cursor.lnum = 0;
2945 sourcing_name = (char_u *)"command line";
2946#ifdef FEAT_EVAL
2947 current_SID = SID_CARG;
2948#endif
2949 for (i = 0; i < parmp->n_commands; ++i)
2950 {
2951 do_cmdline_cmd(parmp->commands[i]);
2952 if (parmp->cmds_tofree[i])
2953 vim_free(parmp->commands[i]);
2954 }
2955 sourcing_name = NULL;
2956#ifdef FEAT_EVAL
2957 current_SID = 0;
2958#endif
2959 if (curwin->w_cursor.lnum == 0)
2960 curwin->w_cursor.lnum = 1;
2961
2962 if (!exmode_active)
2963 msg_scroll = FALSE;
2964
2965#ifdef FEAT_QUICKFIX
2966 /* When started with "-q errorfile" jump to first error again. */
2967 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002968 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002969#endif
2970 TIME_MSG("executing command arguments");
2971}
2972
2973/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002974 * Source startup scripts.
2975 */
2976 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002977source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002978{
2979 int i;
2980
2981 /*
2982 * For "evim" source evim.vim first of all, so that the user can overrule
2983 * any things he doesn't like.
2984 */
2985 if (parmp->evim_mode)
2986 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002987 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002988 TIME_MSG("source evim file");
2989 }
2990
2991 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002992 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002993 * nothing else.
2994 */
2995 if (parmp->use_vimrc != NULL)
2996 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002997 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2998 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002999 {
3000#ifdef FEAT_GUI
3001 if (use_gvimrc == NULL) /* don't load gvimrc either */
3002 use_gvimrc = parmp->use_vimrc;
3003#endif
3004 if (parmp->use_vimrc[2] == 'N')
3005 p_lpl = FALSE; /* don't load plugins either */
3006 }
3007 else
3008 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003009 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003010 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
3011 }
3012 }
3013 else if (!silent_mode)
3014 {
3015#ifdef AMIGA
3016 struct Process *proc = (struct Process *)FindTask(0L);
3017 APTR save_winptr = proc->pr_WindowPtr;
3018
3019 /* Avoid a requester here for a volume that doesn't exist. */
3020 proc->pr_WindowPtr = (APTR)-1L;
3021#endif
3022
3023 /*
3024 * Get system wide defaults, if the file name is defined.
3025 */
3026#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003027 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003028#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003029#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003030 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003031#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003032
3033 /*
3034 * Try to read initialization commands from the following places:
3035 * - environment variable VIMINIT
3036 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3037 * - second user vimrc file ($VIM/.vimrc for Dos)
3038 * - environment variable EXINIT
3039 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3040 * - second user exrc file ($VIM/.exrc for Dos)
3041 * The first that exists is used, the rest is ignored.
3042 */
3043 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3044 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003045 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003046#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003047 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3048 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003049#endif
3050#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003051 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3052 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003053#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003054#ifdef USR_VIMRC_FILE4
3055 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3056 DOSO_VIMRC) == FAIL
3057#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003058 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003059 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003060 {
3061#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003062 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003063#endif
3064 }
3065 }
3066
3067 /*
3068 * Read initialization commands from ".vimrc" or ".exrc" in current
3069 * directory. This is only done if the 'exrc' option is set.
3070 * Because of security reasons we disallow shell and write commands
3071 * now, except for unix if the file is owned by the user or 'secure'
3072 * option has been reset in environment of global ".exrc" or ".vimrc".
3073 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3074 * SYS_VIMRC_FILE.
3075 */
3076 if (p_exrc)
3077 {
3078#if defined(UNIX) || defined(VMS)
3079 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3080 if (!file_owned(VIMRC_FILE))
3081#endif
3082 secure = p_secure;
3083
3084 i = FAIL;
3085 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3086 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3087#ifdef USR_VIMRC_FILE2
3088 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3089 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3090#endif
3091#ifdef USR_VIMRC_FILE3
3092 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3093 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3094#endif
3095#ifdef SYS_VIMRC_FILE
3096 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3097 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3098#endif
3099 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003100 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003101
3102 if (i == FAIL)
3103 {
3104#if defined(UNIX) || defined(VMS)
3105 /* if ".exrc" is not owned by user set 'secure' mode */
3106 if (!file_owned(EXRC_FILE))
3107 secure = p_secure;
3108 else
3109 secure = 0;
3110#endif
3111 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3112 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3113#ifdef USR_EXRC_FILE2
3114 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3115 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3116#endif
3117 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003118 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003119 }
3120 }
3121 if (secure == 2)
3122 need_wait_return = TRUE;
3123 secure = 0;
3124#ifdef AMIGA
3125 proc->pr_WindowPtr = save_winptr;
3126#endif
3127 }
3128 TIME_MSG("sourcing vimrc file(s)");
3129}
3130
3131/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003132 * Setup to start using the GUI. Exit with an error when not available.
3133 */
3134 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003135main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003136{
3137#ifdef FEAT_GUI
3138 gui.starting = TRUE; /* start GUI a bit later */
3139#else
3140 mch_errmsg(_(e_nogvim));
3141 mch_errmsg("\n");
3142 mch_exit(2);
3143#endif
3144}
3145
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003146#endif /* NO_VIM_MAIN */
3147
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003148/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003149 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003150 * Returns FAIL if the environment variable was not executed, OK otherwise.
3151 */
3152 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003153process_env(
3154 char_u *env,
3155 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003156{
3157 char_u *initstr;
3158 char_u *save_sourcing_name;
3159 linenr_T save_sourcing_lnum;
3160#ifdef FEAT_EVAL
3161 scid_T save_sid;
3162#endif
3163
3164 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3165 {
3166 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003167 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003168 save_sourcing_name = sourcing_name;
3169 save_sourcing_lnum = sourcing_lnum;
3170 sourcing_name = env;
3171 sourcing_lnum = 0;
3172#ifdef FEAT_EVAL
3173 save_sid = current_SID;
3174 current_SID = SID_ENV;
3175#endif
3176 do_cmdline_cmd(initstr);
3177 sourcing_name = save_sourcing_name;
3178 sourcing_lnum = save_sourcing_lnum;
3179#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003180 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003181#endif
3182 return OK;
3183 }
3184 return FAIL;
3185}
3186
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003187#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003188/*
3189 * Return TRUE if we are certain the user owns the file "fname".
3190 * Used for ".vimrc" and ".exrc".
3191 * Use both stat() and lstat() for extra security.
3192 */
3193 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003194file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003195{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003196 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003197# ifdef UNIX
3198 uid_t uid = getuid();
3199# else /* VMS */
3200 uid_t uid = ((getgid() << 16) | getuid());
3201# endif
3202
3203 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3204# ifdef HAVE_LSTAT
3205 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3206# endif
3207 );
3208}
3209#endif
3210
3211/*
3212 * Give an error message main_errors["n"] and exit.
3213 */
3214 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003215mainerr(
3216 int n, /* one of the ME_ defines */
3217 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003218{
3219#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3220 reset_signals(); /* kill us with CTRL-C here, if you like */
3221#endif
3222
3223 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003224 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003225 mch_errmsg(_(main_errors[n]));
3226 if (str != NULL)
3227 {
3228 mch_errmsg(": \"");
3229 mch_errmsg((char *)str);
3230 mch_errmsg("\"");
3231 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003232 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003233
3234 mch_exit(1);
3235}
3236
3237 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003238mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003239{
3240 mainerr(ME_ARG_MISSING, str);
3241}
3242
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003243#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003244/*
3245 * print a message with three spaces prepended and '\n' appended.
3246 */
3247 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003248main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003249{
3250 mch_msg(" ");
3251 mch_msg(s);
3252 mch_msg("\n");
3253}
3254
3255/*
3256 * Print messages for "vim -h" or "vim --help" and exit.
3257 */
3258 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003259usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003260{
3261 int i;
3262 static char *(use[]) =
3263 {
3264 N_("[file ..] edit specified file(s)"),
3265 N_("- read text from stdin"),
3266 N_("-t tag edit file where tag is defined"),
3267#ifdef FEAT_QUICKFIX
3268 N_("-q [errorfile] edit file with first error")
3269#endif
3270 };
3271
3272#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3273 reset_signals(); /* kill us with CTRL-C here, if you like */
3274#endif
3275
3276 mch_msg(longVersion);
3277 mch_msg(_("\n\nusage:"));
3278 for (i = 0; ; ++i)
3279 {
3280 mch_msg(_(" vim [arguments] "));
3281 mch_msg(_(use[i]));
3282 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3283 break;
3284 mch_msg(_("\n or:"));
3285 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003286#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003287 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003288#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003289
3290 mch_msg(_("\n\nArguments:\n"));
3291 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003292#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003293 main_msg(_("--literal\t\tDon't expand wildcards"));
3294#endif
3295#ifdef FEAT_OLE
3296 main_msg(_("-register\t\tRegister this gvim for OLE"));
3297 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3298#endif
3299#ifdef FEAT_GUI
3300 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3301 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3302#endif
3303 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3304 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003305 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003306 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3307#ifdef FEAT_DIFF
3308 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3309#endif
3310 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3311 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3312 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3313 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3314 main_msg(_("-M\t\t\tModifications in text not allowed"));
3315 main_msg(_("-b\t\t\tBinary mode"));
3316#ifdef FEAT_LISP
3317 main_msg(_("-l\t\t\tLisp mode"));
3318#endif
3319 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3320 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003321 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3322#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003323 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003324#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003325 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3326 main_msg(_("-r\t\t\tList swap files and exit"));
3327 main_msg(_("-r (with file name)\tRecover crashed session"));
3328 main_msg(_("-L\t\t\tSame as -r"));
3329#ifdef AMIGA
3330 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3331 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3332#endif
3333#ifdef FEAT_ARABIC
3334 main_msg(_("-A\t\t\tstart in Arabic mode"));
3335#endif
3336#ifdef FEAT_RIGHTLEFT
3337 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3338#endif
3339#ifdef FEAT_FKMAP
3340 main_msg(_("-F\t\t\tStart in Farsi mode"));
3341#endif
3342 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003343 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003344 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3345#ifdef FEAT_GUI
3346 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3347#endif
3348 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003349#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003350 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003351 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3352 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003353#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003354 main_msg(_("+\t\t\tStart at end of file"));
3355 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003356 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003357 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3358 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3359 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3360 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3361 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3362#ifdef FEAT_CRYPT
3363 main_msg(_("-x\t\t\tEdit encrypted files"));
3364#endif
3365#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3366# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3367 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3368# endif
3369 main_msg(_("-X\t\t\tDo not connect to X server"));
3370#endif
3371#ifdef FEAT_CLIENTSERVER
3372 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3373 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3374 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3375 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003376# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003377 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003378# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003379 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3380 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3381 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3382 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3383#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003384#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003385 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003386#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003387#ifdef FEAT_VIMINFO
3388 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3389#endif
3390 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3391 main_msg(_("--version\t\tPrint version information and exit"));
3392
3393#ifdef FEAT_GUI_X11
3394# ifdef FEAT_GUI_MOTIF
3395 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3396# else
3397# ifdef FEAT_GUI_ATHENA
3398# ifdef FEAT_GUI_NEXTAW
3399 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3400# else
3401 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3402# endif
3403# endif
3404# endif
3405 main_msg(_("-display <display>\tRun vim on <display>"));
3406 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003407 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3408 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3409 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3410 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3411 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3412 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3413 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3414 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3415# ifdef FEAT_GUI_ATHENA
3416 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3417# endif
3418 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3419 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3420 main_msg(_("-xrm <resource>\tSet the specified resource"));
3421#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003422#ifdef FEAT_GUI_GTK
3423 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3424 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3425 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3426 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3427 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003428 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003429 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003430 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003431#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003432#ifdef FEAT_GUI_W32
3433 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003434 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003435#endif
3436
3437#ifdef FEAT_GUI_GNOME
3438 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3439 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003440 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003441 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003442 gui.dofork = FALSE;
3443 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003444 else
3445#endif
3446 mch_exit(0);
3447}
3448
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003449#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003450/*
3451 * Check the result of the ATTENTION dialog:
3452 * When "Quit" selected, exit Vim.
3453 * When "Recover" selected, recover the file.
3454 */
3455 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003456check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003457{
3458 if (swap_exists_action == SEA_QUIT)
3459 getout(1);
3460 handle_swap_exists(NULL);
3461}
3462#endif
3463
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003464#endif
3465
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003466#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003467static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003468
3469static struct timeval prev_timeval;
3470
Bram Moolenaar3f269672009-11-03 11:11:11 +00003471# ifdef WIN3264
3472/*
3473 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3474 */
3475 static int
3476gettimeofday(struct timeval *tv, char *dummy)
3477{
3478 long t = clock();
3479 tv->tv_sec = t / CLOCKS_PER_SEC;
3480 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3481 return 0;
3482}
3483# endif
3484
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003485/*
3486 * Save the previous time before doing something that could nest.
3487 * set "*tv_rel" to the time elapsed so far.
3488 */
3489 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003490time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003491{
3492 *((struct timeval *)tv_rel) = prev_timeval;
3493 gettimeofday(&prev_timeval, NULL);
3494 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3495 - ((struct timeval *)tv_rel)->tv_usec;
3496 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3497 - ((struct timeval *)tv_rel)->tv_sec;
3498 if (((struct timeval *)tv_rel)->tv_usec < 0)
3499 {
3500 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3501 --((struct timeval *)tv_rel)->tv_sec;
3502 }
3503 *(struct timeval *)tv_start = prev_timeval;
3504}
3505
3506/*
3507 * Compute the previous time after doing something that could nest.
3508 * Subtract "*tp" from prev_timeval;
3509 * Note: The arguments are (void *) to avoid trouble with systems that don't
3510 * have struct timeval.
3511 */
3512 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003513time_pop(
3514 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003515{
3516 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3517 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3518 if (prev_timeval.tv_usec < 0)
3519 {
3520 prev_timeval.tv_usec += 1000000;
3521 --prev_timeval.tv_sec;
3522 }
3523}
3524
3525 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003526time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003527{
3528 long usec;
3529 long msec;
3530
3531 usec = now->tv_usec - then->tv_usec;
3532 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3533 usec = usec % 1000L;
3534 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3535}
3536
3537 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003538time_msg(
3539 char *mesg,
3540 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003541 (struct timeval *) */
3542{
3543 static struct timeval start;
3544 struct timeval now;
3545
3546 if (time_fd != NULL)
3547 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003548 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003549 {
3550 gettimeofday(&start, NULL);
3551 prev_timeval = start;
3552 fprintf(time_fd, "\n\ntimes in msec\n");
3553 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3554 fprintf(time_fd, " clock elapsed: other lines\n\n");
3555 }
3556 gettimeofday(&now, NULL);
3557 time_diff(&start, &now);
3558 if (((struct timeval *)tv_start) != NULL)
3559 {
3560 fprintf(time_fd, " ");
3561 time_diff(((struct timeval *)tv_start), &now);
3562 }
3563 fprintf(time_fd, " ");
3564 time_diff(&prev_timeval, &now);
3565 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003566 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003567 }
3568}
3569
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003570#endif
3571
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003572#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003573
3574/*
3575 * Common code for the X command server and the Win32 command server.
3576 */
3577
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003578static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003579
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003580/*
3581 * Do the client-server stuff, unless "--servername ''" was used.
3582 */
3583 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003584exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003585{
3586 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3587 {
3588# ifdef WIN32
3589 /* Initialise the client/server messaging infrastructure. */
3590 serverInitMessaging();
3591# endif
3592
3593 /*
3594 * When a command server argument was found, execute it. This may
3595 * exit Vim when it was successful. Otherwise it's executed further
3596 * on. Remember the encoding used here in "serverStrEnc".
3597 */
3598 if (parmp->serverArg)
3599 {
3600 cmdsrv_main(&parmp->argc, parmp->argv,
3601 parmp->serverName_arg, &parmp->serverStr);
3602# ifdef FEAT_MBYTE
3603 parmp->serverStrEnc = vim_strsave(p_enc);
3604# endif
3605 }
3606
3607 /* If we're still running, get the name to register ourselves.
3608 * On Win32 can register right now, for X11 need to setup the
3609 * clipboard first, it's further down. */
3610 parmp->servername = serverMakeName(parmp->serverName_arg,
3611 parmp->argv[0]);
3612# ifdef WIN32
3613 if (parmp->servername != NULL)
3614 {
3615 serverSetName(parmp->servername);
3616 vim_free(parmp->servername);
3617 }
3618# endif
3619 }
3620}
3621
3622/*
3623 * Prepare for running as a Vim server.
3624 */
3625 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003626prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003627{
3628# if defined(FEAT_X11)
3629 /*
3630 * Register for remote command execution with :serversend and --remote
3631 * unless there was a -X or a --servername '' on the command line.
3632 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003633 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003634 */
3635 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3636# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003637 (gui.in_use
3638# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003639 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003640# endif
3641 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003642# endif
3643 parmp->serverName_arg != NULL))
3644 {
3645 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3646 vim_free(parmp->servername);
3647 TIME_MSG("register server name");
3648 }
3649 else
3650 serverDelayedStartName = parmp->servername;
3651# endif
3652
3653 /*
3654 * Execute command ourselves if we're here because the send failed (or
3655 * else we would have exited above).
3656 */
3657 if (parmp->serverStr != NULL)
3658 {
3659 char_u *p;
3660
3661 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3662 parmp->serverStr, &p));
3663 vim_free(p);
3664 }
3665}
3666
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003667 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003668cmdsrv_main(
3669 int *argc,
3670 char **argv,
3671 char_u *serverName_arg,
3672 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003673{
3674 char_u *res;
3675 int i;
3676 char_u *sname;
3677 int ret;
3678 int didone = FALSE;
3679 int exiterr = 0;
3680 char **newArgV = argv + 1;
3681 int newArgC = 1,
3682 Argc = *argc;
3683 int argtype;
3684#define ARGTYPE_OTHER 0
3685#define ARGTYPE_EDIT 1
3686#define ARGTYPE_EDIT_WAIT 2
3687#define ARGTYPE_SEND 3
3688 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003689 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003690# ifndef FEAT_X11
3691 HWND srv;
3692# else
3693 Window srv;
3694
3695 setup_term_clip();
3696# endif
3697
3698 sname = serverMakeName(serverName_arg, argv[0]);
3699 if (sname == NULL)
3700 return;
3701
3702 /*
3703 * Execute the command server related arguments and remove them
3704 * from the argc/argv array; We may have to return into main()
3705 */
3706 for (i = 1; i < Argc; i++)
3707 {
3708 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003709 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003710 {
3711 for (; i < *argc; i++)
3712 {
3713 *newArgV++ = argv[i];
3714 newArgC++;
3715 }
3716 break;
3717 }
3718
Bram Moolenaareb94e552006-03-11 21:35:11 +00003719 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003720 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003721 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3722 {
3723 char *p = argv[i] + 8;
3724
3725 argtype = ARGTYPE_EDIT;
3726 while (*p != NUL)
3727 {
3728 if (STRNICMP(p, "-wait", 5) == 0)
3729 {
3730 argtype = ARGTYPE_EDIT_WAIT;
3731 p += 5;
3732 }
3733 else if (STRNICMP(p, "-silent", 7) == 0)
3734 {
3735 silent = TRUE;
3736 p += 7;
3737 }
3738 else if (STRNICMP(p, "-tab", 4) == 0)
3739 {
3740 tabs = TRUE;
3741 p += 4;
3742 }
3743 else
3744 {
3745 argtype = ARGTYPE_OTHER;
3746 break;
3747 }
3748 }
3749 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003750 else
3751 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003752
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003753 if (argtype != ARGTYPE_OTHER)
3754 {
3755 if (i == *argc - 1)
3756 mainerr_arg_missing((char_u *)argv[i]);
3757 if (argtype == ARGTYPE_SEND)
3758 {
3759 *serverStr = (char_u *)argv[i + 1];
3760 i++;
3761 }
3762 else
3763 {
3764 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003765 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003766 if (*serverStr == NULL)
3767 {
3768 /* Probably out of memory, exit. */
3769 didone = TRUE;
3770 exiterr = 1;
3771 break;
3772 }
3773 Argc = i;
3774 }
3775# ifdef FEAT_X11
3776 if (xterm_dpy == NULL)
3777 {
3778 mch_errmsg(_("No display"));
3779 ret = -1;
3780 }
3781 else
3782 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3783 NULL, &srv, 0, 0, silent);
3784# else
3785 /* Win32 always works? */
3786 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3787# endif
3788 if (ret < 0)
3789 {
3790 if (argtype == ARGTYPE_SEND)
3791 {
3792 /* Failed to send, abort. */
3793 mch_errmsg(_(": Send failed.\n"));
3794 didone = TRUE;
3795 exiterr = 1;
3796 }
3797 else if (!silent)
3798 /* Let vim start normally. */
3799 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3800 break;
3801 }
3802
3803# ifdef FEAT_GUI_W32
3804 /* Guess that when the server name starts with "g" it's a GUI
3805 * server, which we can bring to the foreground here.
3806 * Foreground() in the server doesn't work very well. */
3807 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3808 SetForegroundWindow(srv);
3809# endif
3810
3811 /*
3812 * For --remote-wait: Wait until the server did edit each
3813 * file. Also detect that the server no longer runs.
3814 */
3815 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3816 {
3817 int numFiles = *argc - i - 1;
3818 int j;
3819 char_u *done = alloc(numFiles);
3820 char_u *p;
3821# ifdef FEAT_GUI_W32
3822 NOTIFYICONDATA ni;
3823 int count = 0;
3824 extern HWND message_window;
3825# endif
3826
3827 if (numFiles > 0 && argv[i + 1][0] == '+')
3828 /* Skip "+cmd" argument, don't wait for it to be edited. */
3829 --numFiles;
3830
3831# ifdef FEAT_GUI_W32
3832 ni.cbSize = sizeof(ni);
3833 ni.hWnd = message_window;
3834 ni.uID = 0;
3835 ni.uFlags = NIF_ICON|NIF_TIP;
3836 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3837 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3838 Shell_NotifyIcon(NIM_ADD, &ni);
3839# endif
3840
3841 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003842 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003843 while (memchr(done, 0, numFiles) != NULL)
3844 {
3845# ifdef WIN32
3846 p = serverGetReply(srv, NULL, TRUE, TRUE);
3847 if (p == NULL)
3848 break;
3849# else
3850 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3851 break;
3852# endif
3853 j = atoi((char *)p);
3854 if (j >= 0 && j < numFiles)
3855 {
3856# ifdef FEAT_GUI_W32
3857 ++count;
3858 sprintf(ni.szTip, _("%d of %d edited"),
3859 count, numFiles);
3860 Shell_NotifyIcon(NIM_MODIFY, &ni);
3861# endif
3862 done[j] = 1;
3863 }
3864 }
3865# ifdef FEAT_GUI_W32
3866 Shell_NotifyIcon(NIM_DELETE, &ni);
3867# endif
3868 }
3869 }
3870 else if (STRICMP(argv[i], "--remote-expr") == 0)
3871 {
3872 if (i == *argc - 1)
3873 mainerr_arg_missing((char_u *)argv[i]);
3874# ifdef WIN32
3875 /* Win32 always works? */
3876 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3877 &res, NULL, 1, FALSE) < 0)
3878# else
3879 if (xterm_dpy == NULL)
3880 mch_errmsg(_("No display: Send expression failed.\n"));
3881 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3882 &res, NULL, 1, 1, FALSE) < 0)
3883# endif
3884 {
3885 if (res != NULL && *res != NUL)
3886 {
3887 /* Output error from remote */
3888 mch_errmsg((char *)res);
3889 vim_free(res);
3890 res = NULL;
3891 }
3892 mch_errmsg(_(": Send expression failed.\n"));
3893 }
3894 }
3895 else if (STRICMP(argv[i], "--serverlist") == 0)
3896 {
3897# ifdef WIN32
3898 /* Win32 always works? */
3899 res = serverGetVimNames();
3900# else
3901 if (xterm_dpy != NULL)
3902 res = serverGetVimNames(xterm_dpy);
3903# endif
3904 if (called_emsg)
3905 mch_errmsg("\n");
3906 }
3907 else if (STRICMP(argv[i], "--servername") == 0)
3908 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003909 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003910 i++;
3911 continue;
3912 }
3913 else
3914 {
3915 *newArgV++ = argv[i];
3916 newArgC++;
3917 continue;
3918 }
3919 didone = TRUE;
3920 if (res != NULL && *res != NUL)
3921 {
3922 mch_msg((char *)res);
3923 if (res[STRLEN(res) - 1] != '\n')
3924 mch_msg("\n");
3925 }
3926 vim_free(res);
3927 }
3928
3929 if (didone)
3930 {
3931 display_errors(); /* display any collected messages */
3932 exit(exiterr); /* Mission accomplished - get out */
3933 }
3934
3935 /* Return back into main() */
3936 *argc = newArgC;
3937 vim_free(sname);
3938}
3939
3940/*
3941 * Build a ":drop" command to send to a Vim server.
3942 */
3943 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003944build_drop_cmd(
3945 int filec,
3946 char **filev,
3947 int tabs, /* Use ":tab drop" instead of ":drop". */
3948 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003949{
3950 garray_T ga;
3951 int i;
3952 char_u *inicmd = NULL;
3953 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003954 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003955 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003956
3957 if (filec > 0 && filev[0][0] == '+')
3958 {
3959 inicmd = (char_u *)filev[0] + 1;
3960 filev++;
3961 filec--;
3962 }
3963 /* Check if we have at least one argument. */
3964 if (filec <= 0)
3965 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003966
3967 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003968 cwd = alloc(MAXPATHL);
3969 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003970 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003971 if (mch_dirname(cwd, MAXPATHL) != OK)
3972 {
3973 vim_free(cwd);
3974 return NULL;
3975 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003976 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003977#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003978 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00003979#else
3980 PATH_ESC_CHARS,
3981#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003982 '\\', TRUE);
3983 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003984 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003985 return NULL;
3986 ga_init2(&ga, 1, 100);
3987 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003988 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003989
3990 /* Call inputsave() so that a prompt for an encryption key works. */
3991 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3992 if (tabs)
3993 ga_concat(&ga, (char_u *)"tab ");
3994 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003995 for (i = 0; i < filec; i++)
3996 {
3997 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003998 * do it again in the Vim server. On MS-Windows only escape
3999 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004000 p = vim_strsave_escaped((char_u *)filev[i],
4001#ifdef UNIX
4002 PATH_ESC_CHARS
4003#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004004 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004005#endif
4006 );
4007 if (p == NULL)
4008 {
4009 vim_free(ga.ga_data);
4010 return NULL;
4011 }
4012 ga_concat(&ga, (char_u *)" ");
4013 ga_concat(&ga, p);
4014 vim_free(p);
4015 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004016 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4017
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004018 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4019 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004020 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4021
4022 /* Switch back to the correct current directory (prior to temporary path
4023 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004024 * correct after the :drop command. With line breaks and spaces:
4025 * if !exists('+acd') || !&acd
4026 * if haslocaldir()
4027 * cd -
4028 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004029 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004030 * cd -
4031 * endif
4032 * endif
4033 */
4034 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004035 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004036 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004037 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004038 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004039
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004040 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004041 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4042 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004043 if (inicmd != NULL)
4044 {
4045 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4046 * the following commands to be inserted as text. Use a "|",
4047 * hopefully "inicmd" does allow this... */
4048 ga_concat(&ga, inicmd);
4049 ga_concat(&ga, (char_u *)"|");
4050 }
4051 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4052 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004053 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004054 ga_append(&ga, NUL);
4055 return ga.ga_data;
4056}
4057
4058/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004059 * Make our basic server name: use the specified "arg" if given, otherwise use
4060 * the tail of the command "cmd" we were started with.
4061 * Return the name in allocated memory. This doesn't include a serial number.
4062 */
4063 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004064serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004065{
4066 char_u *p;
4067
4068 if (arg != NULL && *arg != NUL)
4069 p = vim_strsave_up(arg);
4070 else
4071 {
4072 p = vim_strsave_up(gettail((char_u *)cmd));
4073 /* Remove .exe or .bat from the name. */
4074 if (p != NULL && vim_strchr(p, '.') != NULL)
4075 *vim_strchr(p, '.') = NUL;
4076 }
4077 return p;
4078}
4079#endif /* FEAT_CLIENTSERVER */
4080
4081#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4082/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004083 * Replace termcodes such as <CR> and insert as key presses if there is room.
4084 */
4085 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004086server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004087{
4088 char_u *ptr = NULL;
4089 char_u *cpo_save = p_cpo;
4090
4091 /* Set 'cpoptions' the way we want it.
4092 * B set - backslashes are *not* treated specially
4093 * k set - keycodes are *not* reverse-engineered
4094 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004095 * The last but one parameter of replace_termcodes() is TRUE so that the
4096 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004097 */
4098 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004099 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004100 p_cpo = cpo_save;
4101
4102 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4103 {
4104 /*
4105 * Add the string to the input stream.
4106 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4107 *
4108 * First clear typed characters from the typeahead buffer, there could
4109 * be half a mapping there. Then append to the existing string, so
4110 * that multiple commands from a client are concatenated.
4111 */
4112 if (typebuf.tb_maplen < typebuf.tb_len)
4113 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4114 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4115
4116 /* Let input_available() know we inserted text in the typeahead
4117 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004118 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004119 }
4120 vim_free((char_u *)ptr);
4121}
4122
4123/*
4124 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004125 */
4126 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004127eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004128{
4129 char_u *res;
4130 int save_dbl = debug_break_level;
4131 int save_ro = redir_off;
4132
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004133 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4134 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004135 debug_break_level = -1;
4136 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004137 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4138 * to be typed. Do generate errors so that try/catch works. */
4139 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004140
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004141 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004142
4143 debug_break_level = save_dbl;
4144 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004145 --emsg_silent;
4146 if (emsg_silent < 0)
4147 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004148
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004149 /* A client can tell us to redraw, but not to display the cursor, so do
4150 * that here. */
4151 setcursor();
4152 out_flush();
4153#ifdef FEAT_GUI
4154 if (gui.in_use)
4155 gui_update_cursor(FALSE, FALSE);
4156#endif
4157
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004158 return res;
4159}
4160
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004161/*
4162 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4163 * return an allocated string. Otherwise return "data".
4164 * "*tofree" is set to the result when it needs to be freed later.
4165 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004166 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004167serverConvert(
4168 char_u *client_enc UNUSED,
4169 char_u *data,
4170 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004171{
4172 char_u *res = data;
4173
4174 *tofree = NULL;
4175# ifdef FEAT_MBYTE
4176 if (client_enc != NULL && p_enc != NULL)
4177 {
4178 vimconv_T vimconv;
4179
4180 vimconv.vc_type = CONV_NONE;
4181 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4182 && vimconv.vc_type != CONV_NONE)
4183 {
4184 res = string_convert(&vimconv, data, NULL);
4185 if (res == NULL)
4186 res = data;
4187 else
4188 *tofree = res;
4189 }
4190 convert_setup(&vimconv, NULL, NULL);
4191 }
4192# endif
4193 return res;
4194}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004195#endif