blob: 42638e4485fbf8801932481abec891d2de540655 [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 {
1463 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001464 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001465 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1466 break;
1467 }
1468 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1469 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001470#endif
1471
1472#ifdef FEAT_VIMINFO
1473 if (*p_viminfo != NUL)
1474 /* Write out the registers, history, marks etc, to the viminfo file */
1475 write_viminfo(NULL, FALSE);
1476#endif
1477
1478#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001479 if (get_vim_var_nr(VV_DYING) <= 1)
1480 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001481#endif
1482
Bram Moolenaar05159a02005-02-26 23:04:13 +00001483#ifdef FEAT_PROFILE
1484 profile_dump();
1485#endif
1486
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001487 if (did_emsg
1488#ifdef FEAT_GUI
1489 || (gui.in_use && msg_didany && p_verbose > 0)
1490#endif
1491 )
1492 {
1493 /* give the user a chance to read the (error) message */
1494 no_wait_return = FALSE;
1495 wait_return(FALSE);
1496 }
1497
1498#ifdef FEAT_AUTOCMD
1499 /* Position the cursor again, the autocommands may have moved it */
1500# ifdef FEAT_GUI
1501 if (!gui.in_use)
1502# endif
1503 windgoto((int)Rows - 1, 0);
1504#endif
1505
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001506#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001507 job_stop_on_exit();
1508#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001509#ifdef FEAT_LUA
1510 lua_end();
1511#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001512#ifdef FEAT_MZSCHEME
1513 mzscheme_end();
1514#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001515#ifdef FEAT_TCL
1516 tcl_end();
1517#endif
1518#ifdef FEAT_RUBY
1519 ruby_end();
1520#endif
1521#ifdef FEAT_PYTHON
1522 python_end();
1523#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001524#ifdef FEAT_PYTHON3
1525 python3_end();
1526#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001527#ifdef FEAT_PERL
1528 perl_end();
1529#endif
1530#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1531 iconv_end();
1532#endif
1533#ifdef FEAT_NETBEANS_INTG
1534 netbeans_end();
1535#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001536#ifdef FEAT_CSCOPE
1537 cs_end();
1538#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001539#ifdef FEAT_EVAL
1540 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001541 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001542#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001543#if defined(WIN32) && defined(FEAT_MBYTE)
1544 free_cmd_argsW();
1545#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001546
1547 mch_exit(exitval);
1548}
1549
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001550#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001551/*
1552 * Get a (optional) count for a Vim argument.
1553 */
1554 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001555get_number_arg(
1556 char_u *p, /* pointer to argument */
1557 int *idx, /* index in argument, is incremented */
1558 int def) /* default value */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001559{
1560 if (vim_isdigit(p[*idx]))
1561 {
1562 def = atoi((char *)&(p[*idx]));
1563 while (vim_isdigit(p[*idx]))
1564 *idx = *idx + 1;
1565 }
1566 return def;
1567}
1568
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001569#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1570/*
1571 * Setup to use the current locale (for ctype() and many other things).
1572 */
1573 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001574init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001575{
1576 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001577
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001578# ifdef FEAT_GUI_GTK
1579 /* Tell Gtk not to change our locale settings. */
1580 gtk_disable_setlocale();
1581# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001582# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1583 /* Make sure strtod() uses a decimal point, not a comma. */
1584 setlocale(LC_NUMERIC, "C");
1585# endif
1586
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001587# ifdef WIN32
1588 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1589 * text while it's expecting text in the current locale. This call avoids
1590 * that. */
1591 setlocale(LC_CTYPE, "C");
1592# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001593
1594# ifdef FEAT_GETTEXT
1595 {
1596 int mustfree = FALSE;
1597 char_u *p;
1598
1599# ifdef DYNAMIC_GETTEXT
1600 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001601 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001602# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001603 /* expand_env() doesn't work yet, because g_chartab[] is not
1604 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001605 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1606 if (p != NULL && *p != NUL)
1607 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001608 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001609 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1610 }
1611 if (mustfree)
1612 vim_free(p);
1613 textdomain(VIMPACKAGE);
1614 }
1615# endif
1616}
1617#endif
1618
1619/*
1620 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1621 * If the executable name starts with "r" we disable shell commands.
1622 * If the next character is "e" we run in Easy mode.
1623 * If the next character is "g" we run the GUI version.
1624 * If the next characters are "view" we start in readonly mode.
1625 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1626 * If the next characters are "ex" we start in Ex mode. If it's followed
1627 * by "im" use improved Ex mode.
1628 */
1629 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001630parse_command_name(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001631{
1632 char_u *initstr;
1633
1634 initstr = gettail((char_u *)parmp->argv[0]);
1635
1636#ifdef MACOS_X_UNIX
1637 /* An issue has been seen when launching Vim in such a way that
1638 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1639 * executable or a symbolic link of it. Until this issue is resolved
1640 * we prohibit the GUI from being used.
1641 */
1642 if (STRCMP(initstr, parmp->argv[0]) == 0)
1643 disallow_gui = TRUE;
1644
1645 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001646 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001647#endif
1648
1649#ifdef FEAT_EVAL
1650 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaara1706c92014-04-01 19:55:49 +02001651 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001652#endif
1653
1654 if (TOLOWER_ASC(initstr[0]) == 'r')
1655 {
1656 restricted = TRUE;
1657 ++initstr;
1658 }
1659
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001660 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001661 if (TOLOWER_ASC(initstr[0]) == 'e'
1662 && (TOLOWER_ASC(initstr[1]) == 'v'
1663 || TOLOWER_ASC(initstr[1]) == 'g'))
1664 {
1665#ifdef FEAT_GUI
1666 gui.starting = TRUE;
1667#endif
1668 parmp->evim_mode = TRUE;
1669 ++initstr;
1670 }
1671
Bram Moolenaar806875d2008-09-18 18:57:10 +00001672 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1673 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001674 {
1675 main_start_gui();
1676#ifdef FEAT_GUI
1677 ++initstr;
1678#endif
1679 }
1680
1681 if (STRNICMP(initstr, "view", 4) == 0)
1682 {
1683 readonlymode = TRUE;
1684 curbuf->b_p_ro = TRUE;
1685 p_uc = 10000; /* don't update very often */
1686 initstr += 4;
1687 }
1688 else if (STRNICMP(initstr, "vim", 3) == 0)
1689 initstr += 3;
1690
1691 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1692 if (STRICMP(initstr, "diff") == 0)
1693 {
1694#ifdef FEAT_DIFF
1695 parmp->diff_mode = TRUE;
1696#else
1697 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1698 mch_errmsg("\n");
1699 mch_exit(2);
1700#endif
1701 }
1702
1703 if (STRNICMP(initstr, "ex", 2) == 0)
1704 {
1705 if (STRNICMP(initstr + 2, "im", 2) == 0)
1706 exmode_active = EXMODE_VIM;
1707 else
1708 exmode_active = EXMODE_NORMAL;
1709 change_compatible(TRUE); /* set 'compatible' */
1710 }
1711}
1712
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001713/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001714 * Get the name of the display, before gui_prepare() removes it from
1715 * argv[]. Used for the xterm-clipboard display.
1716 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001717 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001718 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001719 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001720early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001721{
Bram Moolenaar03005972008-11-20 13:12:36 +00001722#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1723 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001724 int argc = parmp->argc;
1725 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001726 int i;
1727
1728 for (i = 1; i < argc; i++)
1729 {
1730 if (STRCMP(argv[i], "--") == 0)
1731 break;
1732# ifdef FEAT_XCLIPBOARD
1733 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001734# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001735 || STRICMP(argv[i], "--display") == 0
1736# endif
1737 )
1738 {
1739 if (i == argc - 1)
1740 mainerr_arg_missing((char_u *)argv[i]);
1741 xterm_display = argv[++i];
1742 }
1743# endif
1744# ifdef FEAT_CLIENTSERVER
1745 else if (STRICMP(argv[i], "--servername") == 0)
1746 {
1747 if (i == argc - 1)
1748 mainerr_arg_missing((char_u *)argv[i]);
1749 parmp->serverName_arg = (char_u *)argv[++i];
1750 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001751 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001752 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001753 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001754 {
1755 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001756# ifdef FEAT_GUI
1757 if (strstr(argv[i], "-wait") != 0)
1758 /* don't fork() when starting the GUI to edit files ourself */
1759 gui.dofork = FALSE;
1760# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001761 }
1762# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001763
1764# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1765# ifdef FEAT_GUI_W32
1766 else if (STRICMP(argv[i], "--windowid") == 0)
1767# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001768 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001769# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001770 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001771 long_u id;
1772 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001773
1774 if (i == argc - 1)
1775 mainerr_arg_missing((char_u *)argv[i]);
1776 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001777 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001778 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001779 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001780 if (count != 1)
1781 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1782 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001783# ifdef FEAT_GUI_W32
1784 win_socket_id = id;
1785# else
1786 gtk_socket_id = id;
1787# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001788 i++;
1789 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001790# endif
1791# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001792 else if (STRICMP(argv[i], "--echo-wid") == 0)
1793 echo_wid_arg = TRUE;
1794# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001795# ifndef FEAT_NETBEANS_INTG
1796 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001797 {
1798 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1799 mch_exit(2);
1800 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001801# endif
1802
Bram Moolenaar58d98232005-07-23 22:25:46 +00001803 }
1804#endif
1805}
1806
1807/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001808 * Scan the command line arguments.
1809 */
1810 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001811command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001812{
1813 int argc = parmp->argc;
1814 char **argv = parmp->argv;
1815 int argv_idx; /* index in argv[n][] */
1816 int had_minmin = FALSE; /* found "--" argument */
1817 int want_argument; /* option argument with argument */
1818 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001819 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001820 long n;
1821
1822 --argc;
1823 ++argv;
1824 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1825 while (argc > 0)
1826 {
1827 /*
1828 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1829 */
1830 if (argv[0][0] == '+' && !had_minmin)
1831 {
1832 if (parmp->n_commands >= MAX_ARG_CMDS)
1833 mainerr(ME_EXTRA_CMD, NULL);
1834 argv_idx = -1; /* skip to next argument */
1835 if (argv[0][1] == NUL)
1836 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1837 else
1838 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1839 }
1840
1841 /*
1842 * Optional argument.
1843 */
1844 else if (argv[0][0] == '-' && !had_minmin)
1845 {
1846 want_argument = FALSE;
1847 c = argv[0][argv_idx++];
1848#ifdef VMS
1849 /*
1850 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1851 * and "-/X" as "-X".
1852 */
1853 if (c == '/')
1854 {
1855 c = argv[0][argv_idx++];
1856 c = TOUPPER_ASC(c);
1857 }
1858 else
1859 c = TOLOWER_ASC(c);
1860#endif
1861 switch (c)
1862 {
1863 case NUL: /* "vim -" read from stdin */
1864 /* "ex -" silent mode */
1865 if (exmode_active)
1866 silent_mode = TRUE;
1867 else
1868 {
1869 if (parmp->edit_type != EDIT_NONE)
1870 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1871 parmp->edit_type = EDIT_STDIN;
1872 read_cmd_fd = 2; /* read from stderr instead of stdin */
1873 }
1874 argv_idx = -1; /* skip to next argument */
1875 break;
1876
1877 case '-': /* "--" don't take any more option arguments */
1878 /* "--help" give help message */
1879 /* "--version" give version message */
1880 /* "--literal" take files literally */
1881 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001882 /* "--not-a-term" don't warn for not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001883 /* "--noplugin[s]" skip plugins */
1884 /* "--cmd <cmd>" execute cmd before vimrc */
1885 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1886 usage();
1887 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1888 {
1889 Columns = 80; /* need to init Columns */
1890 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1891 list_version();
1892 msg_putchar('\n');
1893 msg_didout = FALSE;
1894 mch_exit(0);
1895 }
1896 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1897 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001898#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001899 parmp->literal = TRUE;
1900#endif
1901 }
1902 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1903 {
1904#ifdef FEAT_GUI
1905 gui.dofork = FALSE; /* don't fork() when starting GUI */
1906#endif
1907 }
1908 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1909 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001910 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1911 parmp->not_a_term = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001912 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1913 {
1914 want_argument = TRUE;
1915 argv_idx += 3;
1916 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001917 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1918 {
1919 want_argument = TRUE;
1920 argv_idx += 11;
1921 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001922#ifdef FEAT_CLIENTSERVER
1923 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1924 ; /* already processed -- no arg */
1925 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1926 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1927 {
1928 /* already processed -- snatch the following arg */
1929 if (argc > 1)
1930 {
1931 --argc;
1932 ++argv;
1933 }
1934 }
1935#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001936#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1937# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001938 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001939# else
1940 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1941# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001942 {
1943 /* already processed -- snatch the following arg */
1944 if (argc > 1)
1945 {
1946 --argc;
1947 ++argv;
1948 }
1949 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001950#endif
1951#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001952 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1953 {
1954 /* already processed, skip */
1955 }
1956#endif
1957 else
1958 {
1959 if (argv[0][argv_idx])
1960 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1961 had_minmin = TRUE;
1962 }
1963 if (!want_argument)
1964 argv_idx = -1; /* skip to next argument */
1965 break;
1966
1967 case 'A': /* "-A" start in Arabic mode */
1968#ifdef FEAT_ARABIC
1969 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1970#else
1971 mch_errmsg(_(e_noarabic));
1972 mch_exit(2);
1973#endif
1974 break;
1975
1976 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001977 /* Needs to be effective before expanding file names, because
1978 * for Win32 this makes us edit a shortcut file itself,
1979 * instead of the file it links to. */
1980 set_options_bin(curbuf->b_p_bin, 1, 0);
1981 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001982 break;
1983
1984 case 'C': /* "-C" Compatible */
1985 change_compatible(TRUE);
1986 break;
1987
1988 case 'e': /* "-e" Ex mode */
1989 exmode_active = EXMODE_NORMAL;
1990 break;
1991
1992 case 'E': /* "-E" Improved Ex mode */
1993 exmode_active = EXMODE_VIM;
1994 break;
1995
1996 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1997 window directly, not with newcli */
1998#ifdef FEAT_GUI
1999 gui.dofork = FALSE; /* don't fork() when starting GUI */
2000#endif
2001 break;
2002
2003 case 'g': /* "-g" start GUI */
2004 main_start_gui();
2005 break;
2006
2007 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
2008#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002009 p_fkmap = TRUE;
2010 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002011#else
2012 mch_errmsg(_(e_nofarsi));
2013 mch_exit(2);
2014#endif
2015 break;
2016
2017 case 'h': /* "-h" give help message */
2018#ifdef FEAT_GUI_GNOME
2019 /* Tell usage() to exit for "gvim". */
2020 gui.starting = FALSE;
2021#endif
2022 usage();
2023 break;
2024
2025 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2026#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002027 p_hkmap = TRUE;
2028 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002029#else
2030 mch_errmsg(_(e_nohebrew));
2031 mch_exit(2);
2032#endif
2033 break;
2034
2035 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2036#ifdef FEAT_LISP
2037 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2038 p_sm = TRUE;
2039#endif
2040 break;
2041
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002042 case 'M': /* "-M" no changes or writing of files */
2043 reset_modifiable();
2044 /* FALLTHROUGH */
2045
2046 case 'm': /* "-m" no writing of files */
2047 p_write = FALSE;
2048 break;
2049
2050 case 'y': /* "-y" easy mode */
2051#ifdef FEAT_GUI
2052 gui.starting = TRUE; /* start GUI a bit later */
2053#endif
2054 parmp->evim_mode = TRUE;
2055 break;
2056
2057 case 'N': /* "-N" Nocompatible */
2058 change_compatible(FALSE);
2059 break;
2060
2061 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002062#ifdef FEAT_NETBEANS_INTG
2063 /* checking for "-nb", netbeans parameters */
2064 if (argv[0][argv_idx] == 'b')
2065 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002066 netbeansArg = argv[0];
2067 argv_idx = -1; /* skip to next argument */
2068 }
2069 else
2070#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002071 parmp->no_swap_file = TRUE;
2072 break;
2073
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002074 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002075#ifdef TARGET_API_MAC_OSX
2076 /* For some reason on MacOS X, an argument like:
2077 -psn_0_10223617 is passed in when invoke from Finder
2078 or with the 'open' command */
2079 if (argv[0][argv_idx] == 's')
2080 {
2081 argv_idx = -1; /* bypass full -psn */
2082 main_start_gui();
2083 break;
2084 }
2085#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002086#ifdef FEAT_WINDOWS
2087 /* default is 0: open window for each file */
2088 parmp->window_count = get_number_arg((char_u *)argv[0],
2089 &argv_idx, 0);
2090 parmp->window_layout = WIN_TABS;
2091#endif
2092 break;
2093
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002094 case 'o': /* "-o[N]" open N horizontal split windows */
2095#ifdef FEAT_WINDOWS
2096 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002097 parmp->window_count = get_number_arg((char_u *)argv[0],
2098 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002099 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002100#endif
2101 break;
2102
2103 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002104#ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002105 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002106 parmp->window_count = get_number_arg((char_u *)argv[0],
2107 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002108 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002109#endif
2110 break;
2111
2112#ifdef FEAT_QUICKFIX
2113 case 'q': /* "-q" QuickFix mode */
2114 if (parmp->edit_type != EDIT_NONE)
2115 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2116 parmp->edit_type = EDIT_QF;
2117 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2118 {
2119 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2120 argv_idx = -1;
2121 }
2122 else if (argc > 1) /* "-q {errorfile}" */
2123 want_argument = TRUE;
2124 break;
2125#endif
2126
2127 case 'R': /* "-R" readonly mode */
2128 readonlymode = TRUE;
2129 curbuf->b_p_ro = TRUE;
2130 p_uc = 10000; /* don't update very often */
2131 break;
2132
2133 case 'r': /* "-r" recovery mode */
2134 case 'L': /* "-L" recovery mode */
2135 recoverymode = 1;
2136 break;
2137
2138 case 's':
2139 if (exmode_active) /* "-s" silent (batch) mode */
2140 silent_mode = TRUE;
2141 else /* "-s {scriptin}" read from script file */
2142 want_argument = TRUE;
2143 break;
2144
2145 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2146 if (parmp->edit_type != EDIT_NONE)
2147 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2148 parmp->edit_type = EDIT_TAG;
2149 if (argv[0][argv_idx]) /* "-t{tag}" */
2150 {
2151 parmp->tagname = (char_u *)argv[0] + argv_idx;
2152 argv_idx = -1;
2153 }
2154 else /* "-t {tag}" */
2155 want_argument = TRUE;
2156 break;
2157
2158#ifdef FEAT_EVAL
2159 case 'D': /* "-D" Debugging */
2160 parmp->use_debug_break_level = 9999;
2161 break;
2162#endif
2163#ifdef FEAT_DIFF
2164 case 'd': /* "-d" 'diff' */
2165# ifdef AMIGA
2166 /* check for "-dev {device}" */
2167 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2168 want_argument = TRUE;
2169 else
2170# endif
2171 parmp->diff_mode = TRUE;
2172 break;
2173#endif
2174 case 'V': /* "-V{N}" Verbose level */
2175 /* default is 10: a little bit verbose */
2176 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2177 if (argv[0][argv_idx] != NUL)
2178 {
2179 set_option_value((char_u *)"verbosefile", 0L,
2180 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002181 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002182 }
2183 break;
2184
2185 case 'v': /* "-v" Vi-mode (as if called "vi") */
2186 exmode_active = 0;
2187#ifdef FEAT_GUI
2188 gui.starting = FALSE; /* don't start GUI */
2189#endif
2190 break;
2191
2192 case 'w': /* "-w{number}" set window height */
2193 /* "-w {scriptout}" write to script */
2194 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2195 {
2196 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2197 set_option_value((char_u *)"window", n, NULL, 0);
2198 break;
2199 }
2200 want_argument = TRUE;
2201 break;
2202
2203#ifdef FEAT_CRYPT
2204 case 'x': /* "-x" encrypted reading/writing of files */
2205 parmp->ask_for_key = TRUE;
2206 break;
2207#endif
2208
2209 case 'X': /* "-X" don't connect to X server */
2210#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2211 x_no_connect = TRUE;
2212#endif
2213 break;
2214
2215 case 'Z': /* "-Z" restricted mode */
2216 restricted = TRUE;
2217 break;
2218
2219 case 'c': /* "-c{command}" or "-c {command}" execute
2220 command */
2221 if (argv[0][argv_idx] != NUL)
2222 {
2223 if (parmp->n_commands >= MAX_ARG_CMDS)
2224 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002225 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2226 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002227 argv_idx = -1;
2228 break;
2229 }
2230 /*FALLTHROUGH*/
2231 case 'S': /* "-S {file}" execute Vim script */
2232 case 'i': /* "-i {viminfo}" use for viminfo */
2233#ifndef FEAT_DIFF
2234 case 'd': /* "-d {device}" device (for Amiga) */
2235#endif
2236 case 'T': /* "-T {terminal}" terminal name */
2237 case 'u': /* "-u {vimrc}" vim inits file */
2238 case 'U': /* "-U {gvimrc}" gvim inits file */
2239 case 'W': /* "-W {scriptout}" overwrite */
2240#ifdef FEAT_GUI_W32
2241 case 'P': /* "-P {parent title}" MDI parent */
2242#endif
2243 want_argument = TRUE;
2244 break;
2245
2246 default:
2247 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2248 }
2249
2250 /*
2251 * Handle option arguments with argument.
2252 */
2253 if (want_argument)
2254 {
2255 /*
2256 * Check for garbage immediately after the option letter.
2257 */
2258 if (argv[0][argv_idx] != NUL)
2259 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2260
2261 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002262 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002263 mainerr_arg_missing((char_u *)argv[0]);
2264 ++argv;
2265 argv_idx = -1;
2266
2267 switch (c)
2268 {
2269 case 'c': /* "-c {command}" execute command */
2270 case 'S': /* "-S {file}" execute Vim script */
2271 if (parmp->n_commands >= MAX_ARG_CMDS)
2272 mainerr(ME_EXTRA_CMD, NULL);
2273 if (c == 'S')
2274 {
2275 char *a;
2276
2277 if (argc < 1)
2278 /* "-S" without argument: use default session file
2279 * name. */
2280 a = SESSION_FILE;
2281 else if (argv[0][0] == '-')
2282 {
2283 /* "-S" followed by another option: use default
2284 * session file name. */
2285 a = SESSION_FILE;
2286 ++argc;
2287 --argv;
2288 }
2289 else
2290 a = argv[0];
2291 p = alloc((unsigned)(STRLEN(a) + 4));
2292 if (p == NULL)
2293 mch_exit(2);
2294 sprintf((char *)p, "so %s", a);
2295 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2296 parmp->commands[parmp->n_commands++] = p;
2297 }
2298 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002299 parmp->commands[parmp->n_commands++] =
2300 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002301 break;
2302
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002303 case '-':
2304 if (argv[-1][2] == 'c')
2305 {
2306 /* "--cmd {command}" execute command */
2307 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2308 mainerr(ME_EXTRA_CMD, NULL);
2309 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002310 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002311 }
2312 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002313 break;
2314
2315 /* case 'd': -d {device} is handled in mch_check_win() for the
2316 * Amiga */
2317
2318#ifdef FEAT_QUICKFIX
2319 case 'q': /* "-q {errorfile}" QuickFix mode */
2320 parmp->use_ef = (char_u *)argv[0];
2321 break;
2322#endif
2323
2324 case 'i': /* "-i {viminfo}" use for viminfo */
2325 use_viminfo = (char_u *)argv[0];
2326 break;
2327
2328 case 's': /* "-s {scriptin}" read from script file */
2329 if (scriptin[0] != NULL)
2330 {
2331scripterror:
2332 mch_errmsg(_("Attempt to open script file again: \""));
2333 mch_errmsg(argv[-1]);
2334 mch_errmsg(" ");
2335 mch_errmsg(argv[0]);
2336 mch_errmsg("\"\n");
2337 mch_exit(2);
2338 }
2339 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2340 {
2341 mch_errmsg(_("Cannot open for reading: \""));
2342 mch_errmsg(argv[0]);
2343 mch_errmsg("\"\n");
2344 mch_exit(2);
2345 }
2346 if (save_typebuf() == FAIL)
2347 mch_exit(2); /* out of memory */
2348 break;
2349
2350 case 't': /* "-t {tag}" */
2351 parmp->tagname = (char_u *)argv[0];
2352 break;
2353
2354 case 'T': /* "-T {terminal}" terminal name */
2355 /*
2356 * The -T term argument is always available and when
2357 * HAVE_TERMLIB is supported it overrides the environment
2358 * variable TERM.
2359 */
2360#ifdef FEAT_GUI
2361 if (term_is_gui((char_u *)argv[0]))
2362 gui.starting = TRUE; /* start GUI a bit later */
2363 else
2364#endif
2365 parmp->term = (char_u *)argv[0];
2366 break;
2367
2368 case 'u': /* "-u {vimrc}" vim inits file */
2369 parmp->use_vimrc = (char_u *)argv[0];
2370 break;
2371
2372 case 'U': /* "-U {gvimrc}" gvim inits file */
2373#ifdef FEAT_GUI
2374 use_gvimrc = (char_u *)argv[0];
2375#endif
2376 break;
2377
2378 case 'w': /* "-w {nr}" 'window' value */
2379 /* "-w {scriptout}" append to script file */
2380 if (vim_isdigit(*((char_u *)argv[0])))
2381 {
2382 argv_idx = 0;
2383 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2384 set_option_value((char_u *)"window", n, NULL, 0);
2385 argv_idx = -1;
2386 break;
2387 }
2388 /*FALLTHROUGH*/
2389 case 'W': /* "-W {scriptout}" overwrite script file */
2390 if (scriptout != NULL)
2391 goto scripterror;
2392 if ((scriptout = mch_fopen(argv[0],
2393 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2394 {
2395 mch_errmsg(_("Cannot open for script output: \""));
2396 mch_errmsg(argv[0]);
2397 mch_errmsg("\"\n");
2398 mch_exit(2);
2399 }
2400 break;
2401
2402#ifdef FEAT_GUI_W32
2403 case 'P': /* "-P {parent title}" MDI parent */
2404 gui_mch_set_parent(argv[0]);
2405 break;
2406#endif
2407 }
2408 }
2409 }
2410
2411 /*
2412 * File name argument.
2413 */
2414 else
2415 {
2416 argv_idx = -1; /* skip to next argument */
2417
2418 /* Check for only one type of editing. */
2419 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2420 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2421 parmp->edit_type = EDIT_FILE;
2422
2423#ifdef MSWIN
2424 /* Remember if the argument was a full path before changing
2425 * slashes to backslashes. */
2426 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2427 parmp->full_path = TRUE;
2428#endif
2429
2430 /* Add the file to the global argument list. */
2431 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2432 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2433 mch_exit(2);
2434#ifdef FEAT_DIFF
2435 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2436 && !mch_isdir(alist_name(&GARGLIST[0])))
2437 {
2438 char_u *r;
2439
2440 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2441 if (r != NULL)
2442 {
2443 vim_free(p);
2444 p = r;
2445 }
2446 }
2447#endif
2448#if defined(__CYGWIN32__) && !defined(WIN32)
2449 /*
2450 * If vim is invoked by non-Cygwin tools, convert away any
2451 * DOS paths, so things like .swp files are created correctly.
2452 * Look for evidence of non-Cygwin paths before we bother.
2453 * This is only for when using the Unix files.
2454 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002455 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002456 {
2457 char posix_path[PATH_MAX];
2458
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002459# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2460 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2461# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002462 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002463# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002464 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002465 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002466 if (p == NULL)
2467 mch_exit(2);
2468 }
2469#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002470
2471#ifdef USE_FNAME_CASE
2472 /* Make the case of the file name match the actual file. */
2473 fname_case(p, 0);
2474#endif
2475
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002476 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002477#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002478 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002479#else
2480 2 /* add buffer number now and use curbuf */
2481#endif
2482 );
2483
2484#if defined(FEAT_MBYTE) && defined(WIN32)
2485 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002486 /* Remember this argument has been added to the argument list.
2487 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002488 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002489# ifdef FEAT_DIFF
2490 parmp->diff_mode
2491# else
2492 FALSE
2493# endif
2494 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002495 }
2496#endif
2497 }
2498
2499 /*
2500 * If there are no more letters after the current "-", go to next
2501 * argument. argv_idx is set to -1 when the current argument is to be
2502 * skipped.
2503 */
2504 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2505 {
2506 --argc;
2507 ++argv;
2508 argv_idx = 1;
2509 }
2510 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002511
2512#ifdef FEAT_EVAL
2513 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2514 * one. */
2515 if (parmp->n_commands > 0)
2516 {
2517 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2518 if (p != NULL)
2519 {
2520 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2521 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2522 vim_free(p);
2523 }
2524 }
2525#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002526}
2527
2528/*
2529 * Print a warning if stdout is not a terminal.
2530 * When starting in Ex mode and commands come from a file, set Silent mode.
2531 */
2532 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002533check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002534{
2535 int input_isatty; /* is active input a terminal? */
2536
2537 input_isatty = mch_input_isatty();
2538 if (exmode_active)
2539 {
2540 if (!input_isatty)
2541 silent_mode = TRUE;
2542 }
2543 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2544#ifdef FEAT_GUI
2545 /* don't want the delay when started from the desktop */
2546 && !gui.starting
2547#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002548 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002549 {
2550#ifdef NBDEBUG
2551 /*
2552 * This shouldn't be necessary. But if I run netbeans with the log
2553 * output coming to the console and XOpenDisplay fails, I get vim
2554 * trying to start with input/output to my console tty. This fills my
2555 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002556 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002557 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002558 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002559 {
2560 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2561 exit(1);
2562 }
2563#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002564#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2565 if (is_cygpty_used())
2566 {
2567 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2568 exit(1);
2569 }
2570#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002571 if (!parmp->stdout_isatty)
2572 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2573 if (!input_isatty)
2574 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2575 out_flush();
2576 if (scriptin[0] == NULL)
2577 ui_delay(2000L, TRUE);
2578 TIME_MSG("Warning delay");
2579 }
2580}
2581
2582/*
2583 * Read text from stdin.
2584 */
2585 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002586read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002587{
2588 int i;
2589
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002590#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002591 /* When getting the ATTENTION prompt here, use a dialog */
2592 swap_exists_action = SEA_DIALOG;
2593#endif
2594 no_wait_return = TRUE;
2595 i = msg_didany;
2596 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002597 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002598 no_wait_return = FALSE;
2599 msg_didany = i;
2600 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002601#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002602 check_swap_exists_action();
2603#endif
2604#if !(defined(AMIGA) || defined(MACOS))
2605 /*
2606 * Close stdin and dup it from stderr. Required for GPM to work
2607 * properly, and for running external commands.
2608 * Is there any other system that cannot do this?
2609 */
2610 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002611 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002612#endif
2613}
2614
2615/*
2616 * Create the requested number of windows and edit buffers in them.
2617 * Also does recovery if "recoverymode" set.
2618 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002619 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002620create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002621{
2622#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002623 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002624 int done = 0;
2625
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002626 /*
2627 * Create the number of windows that was requested.
2628 */
2629 if (parmp->window_count == -1) /* was not set */
2630 parmp->window_count = 1;
2631 if (parmp->window_count == 0)
2632 parmp->window_count = GARGCOUNT;
2633 if (parmp->window_count > 1)
2634 {
2635 /* Don't change the windows if there was a command in .vimrc that
2636 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002637 if (parmp->window_layout == 0)
2638 parmp->window_layout = WIN_HOR;
2639 if (parmp->window_layout == WIN_TABS)
2640 {
2641 parmp->window_count = make_tabpages(parmp->window_count);
2642 TIME_MSG("making tab pages");
2643 }
2644 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002645 {
2646 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002647 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002648 TIME_MSG("making windows");
2649 }
2650 else
2651 parmp->window_count = win_count();
2652 }
2653 else
2654 parmp->window_count = 1;
2655#endif
2656
2657 if (recoverymode) /* do recover */
2658 {
2659 msg_scroll = TRUE; /* scroll message up */
2660 ml_recover();
2661 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2662 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002663 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002664 }
2665 else
2666 {
2667 /*
2668 * Open a buffer for windows that don't have one yet.
2669 * Commands in the .vimrc might have loaded a file or split the window.
2670 * Watch out for autocommands that delete a window.
2671 */
2672#ifdef FEAT_AUTOCMD
2673 /*
2674 * Don't execute Win/Buf Enter/Leave autocommands here
2675 */
2676 ++autocmd_no_enter;
2677 ++autocmd_no_leave;
2678#endif
2679#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002680 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002681 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002682 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002683 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002684 {
2685 if (parmp->window_layout == WIN_TABS)
2686 goto_tabpage(1);
2687 else
2688 curwin = firstwin;
2689 }
2690 else if (parmp->window_layout == WIN_TABS)
2691 {
2692 if (curtab->tp_next == NULL)
2693 break;
2694 goto_tabpage(0);
2695 }
2696 else
2697 {
2698 if (curwin->w_next == NULL)
2699 break;
2700 curwin = curwin->w_next;
2701 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002702 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002703#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002704 curbuf = curwin->w_buffer;
2705 if (curbuf->b_ml.ml_mfp == NULL)
2706 {
2707#ifdef FEAT_FOLDING
2708 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2709 if (p_fdls >= 0)
2710 curwin->w_p_fdl = p_fdls;
2711#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002712#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002713 /* When getting the ATTENTION prompt here, use a dialog */
2714 swap_exists_action = SEA_DIALOG;
2715#endif
2716 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002717
2718 /* create memfile, read file */
2719 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002720
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002721#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002722 if (swap_exists_action == SEA_QUIT)
2723 {
2724 if (got_int || only_one_window())
2725 {
2726 /* abort selected or quit and only one window */
2727 did_emsg = FALSE; /* avoid hit-enter prompt */
2728 getout(1);
2729 }
2730 /* We can't close the window, it would disturb what
2731 * happens next. Clear the file name and set the arg
2732 * index to -1 to delete it later. */
2733 setfname(curbuf, NULL, NULL, FALSE);
2734 curwin->w_arg_idx = -1;
2735 swap_exists_action = SEA_NONE;
2736 }
2737 else
2738 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002739#endif
2740#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002741 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002742#endif
2743 }
2744#ifdef FEAT_WINDOWS
2745 ui_breakcheck();
2746 if (got_int)
2747 {
2748 (void)vgetc(); /* only break the file loading, not the rest */
2749 break;
2750 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002751 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002752#endif
2753#ifdef FEAT_WINDOWS
2754 if (parmp->window_layout == WIN_TABS)
2755 goto_tabpage(1);
2756 else
2757 curwin = firstwin;
2758 curbuf = curwin->w_buffer;
2759#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002760#ifdef FEAT_AUTOCMD
2761 --autocmd_no_enter;
2762 --autocmd_no_leave;
2763#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002764 }
2765}
2766
2767#ifdef FEAT_WINDOWS
2768 /*
2769 * If opened more than one window, start editing files in the other
2770 * windows. make_windows() has already opened the windows.
2771 */
2772 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002773edit_buffers(
2774 mparm_T *parmp,
2775 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002776{
2777 int arg_idx; /* index in argument list */
2778 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002779 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002780 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002781
2782# ifdef FEAT_AUTOCMD
2783 /*
2784 * Don't execute Win/Buf Enter/Leave autocommands here
2785 */
2786 ++autocmd_no_enter;
2787 ++autocmd_no_leave;
2788# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002789
2790 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2791 if (curwin->w_arg_idx == -1)
2792 {
2793 win_close(curwin, TRUE);
2794 advance = FALSE;
2795 }
2796
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002797 arg_idx = 1;
2798 for (i = 1; i < parmp->window_count; ++i)
2799 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002800 if (cwd != NULL)
2801 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002802 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2803 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002804 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002805 ++arg_idx;
2806 win_close(curwin, TRUE);
2807 advance = FALSE;
2808 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002809 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002810
Bram Moolenaar84212822006-11-07 21:59:47 +00002811 if (advance)
2812 {
2813 if (parmp->window_layout == WIN_TABS)
2814 {
2815 if (curtab->tp_next == NULL) /* just checking */
2816 break;
2817 goto_tabpage(0);
2818 }
2819 else
2820 {
2821 if (curwin->w_next == NULL) /* just checking */
2822 break;
2823 win_enter(curwin->w_next, FALSE);
2824 }
2825 }
2826 advance = TRUE;
2827
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002828 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002829 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002830 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2831 {
2832 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002833 /* Edit file from arg list, if there is one. When "Quit" selected
2834 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002835# ifdef HAS_SWAP_EXISTS_ACTION
2836 swap_exists_did_quit = FALSE;
2837# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002838 (void)do_ecmd(0, arg_idx < GARGCOUNT
2839 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002840 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002841# ifdef HAS_SWAP_EXISTS_ACTION
2842 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002843 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002844 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002845 if (got_int || only_one_window())
2846 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002847 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002848 did_emsg = FALSE; /* avoid hit-enter prompt */
2849 getout(1);
2850 }
2851 win_close(curwin, TRUE);
2852 advance = FALSE;
2853 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002854# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002855 if (arg_idx == GARGCOUNT - 1)
2856 arg_had_last = TRUE;
2857 ++arg_idx;
2858 }
2859 ui_breakcheck();
2860 if (got_int)
2861 {
2862 (void)vgetc(); /* only break the file loading, not the rest */
2863 break;
2864 }
2865 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002866
2867 if (parmp->window_layout == WIN_TABS)
2868 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002869# ifdef FEAT_AUTOCMD
2870 --autocmd_no_enter;
2871# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002872
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002873 /* make the first window the current window */
2874 win = firstwin;
2875#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2876 /* Avoid making a preview window the current window. */
2877 while (win->w_p_pvw)
2878 {
2879 win = win->w_next;
2880 if (win == NULL)
2881 {
2882 win = firstwin;
2883 break;
2884 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002885 }
2886#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002887 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002888
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002889# ifdef FEAT_AUTOCMD
2890 --autocmd_no_leave;
2891# endif
2892 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002893 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002894 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2895}
2896#endif /* FEAT_WINDOWS */
2897
2898/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002899 * Execute the commands from --cmd arguments "cmds[cnt]".
2900 */
2901 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002902exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002903{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002904 char_u **cmds = parmp->pre_commands;
2905 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002906 int i;
2907
2908 if (cnt > 0)
2909 {
2910 curwin->w_cursor.lnum = 0; /* just in case.. */
2911 sourcing_name = (char_u *)_("pre-vimrc command line");
2912# ifdef FEAT_EVAL
2913 current_SID = SID_CMDARG;
2914# endif
2915 for (i = 0; i < cnt; ++i)
2916 do_cmdline_cmd(cmds[i]);
2917 sourcing_name = NULL;
2918# ifdef FEAT_EVAL
2919 current_SID = 0;
2920# endif
2921 TIME_MSG("--cmd commands");
2922 }
2923}
2924
2925/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002926 * Execute "+", "-c" and "-S" arguments.
2927 */
2928 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002929exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002930{
2931 int i;
2932
2933 /*
2934 * We start commands on line 0, make "vim +/pat file" match a
2935 * pattern on line 1. But don't move the cursor when an autocommand
2936 * with g`" was used.
2937 */
2938 msg_scroll = TRUE;
2939 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2940 curwin->w_cursor.lnum = 0;
2941 sourcing_name = (char_u *)"command line";
2942#ifdef FEAT_EVAL
2943 current_SID = SID_CARG;
2944#endif
2945 for (i = 0; i < parmp->n_commands; ++i)
2946 {
2947 do_cmdline_cmd(parmp->commands[i]);
2948 if (parmp->cmds_tofree[i])
2949 vim_free(parmp->commands[i]);
2950 }
2951 sourcing_name = NULL;
2952#ifdef FEAT_EVAL
2953 current_SID = 0;
2954#endif
2955 if (curwin->w_cursor.lnum == 0)
2956 curwin->w_cursor.lnum = 1;
2957
2958 if (!exmode_active)
2959 msg_scroll = FALSE;
2960
2961#ifdef FEAT_QUICKFIX
2962 /* When started with "-q errorfile" jump to first error again. */
2963 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002964 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002965#endif
2966 TIME_MSG("executing command arguments");
2967}
2968
2969/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002970 * Source startup scripts.
2971 */
2972 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002973source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002974{
2975 int i;
2976
2977 /*
2978 * For "evim" source evim.vim first of all, so that the user can overrule
2979 * any things he doesn't like.
2980 */
2981 if (parmp->evim_mode)
2982 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002983 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002984 TIME_MSG("source evim file");
2985 }
2986
2987 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002988 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002989 * nothing else.
2990 */
2991 if (parmp->use_vimrc != NULL)
2992 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002993 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2994 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002995 {
2996#ifdef FEAT_GUI
2997 if (use_gvimrc == NULL) /* don't load gvimrc either */
2998 use_gvimrc = parmp->use_vimrc;
2999#endif
3000 if (parmp->use_vimrc[2] == 'N')
3001 p_lpl = FALSE; /* don't load plugins either */
3002 }
3003 else
3004 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003005 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003006 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
3007 }
3008 }
3009 else if (!silent_mode)
3010 {
3011#ifdef AMIGA
3012 struct Process *proc = (struct Process *)FindTask(0L);
3013 APTR save_winptr = proc->pr_WindowPtr;
3014
3015 /* Avoid a requester here for a volume that doesn't exist. */
3016 proc->pr_WindowPtr = (APTR)-1L;
3017#endif
3018
3019 /*
3020 * Get system wide defaults, if the file name is defined.
3021 */
3022#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003023 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003024#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003025#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003026 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003027#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003028
3029 /*
3030 * Try to read initialization commands from the following places:
3031 * - environment variable VIMINIT
3032 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3033 * - second user vimrc file ($VIM/.vimrc for Dos)
3034 * - environment variable EXINIT
3035 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3036 * - second user exrc file ($VIM/.exrc for Dos)
3037 * The first that exists is used, the rest is ignored.
3038 */
3039 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3040 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003041 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003042#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003043 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3044 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003045#endif
3046#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003047 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3048 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003049#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003050#ifdef USR_VIMRC_FILE4
3051 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3052 DOSO_VIMRC) == FAIL
3053#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003054 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003055 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003056 {
3057#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003058 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003059#endif
3060 }
3061 }
3062
3063 /*
3064 * Read initialization commands from ".vimrc" or ".exrc" in current
3065 * directory. This is only done if the 'exrc' option is set.
3066 * Because of security reasons we disallow shell and write commands
3067 * now, except for unix if the file is owned by the user or 'secure'
3068 * option has been reset in environment of global ".exrc" or ".vimrc".
3069 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3070 * SYS_VIMRC_FILE.
3071 */
3072 if (p_exrc)
3073 {
3074#if defined(UNIX) || defined(VMS)
3075 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3076 if (!file_owned(VIMRC_FILE))
3077#endif
3078 secure = p_secure;
3079
3080 i = FAIL;
3081 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3082 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3083#ifdef USR_VIMRC_FILE2
3084 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3085 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3086#endif
3087#ifdef USR_VIMRC_FILE3
3088 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3089 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3090#endif
3091#ifdef SYS_VIMRC_FILE
3092 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3093 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3094#endif
3095 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003096 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003097
3098 if (i == FAIL)
3099 {
3100#if defined(UNIX) || defined(VMS)
3101 /* if ".exrc" is not owned by user set 'secure' mode */
3102 if (!file_owned(EXRC_FILE))
3103 secure = p_secure;
3104 else
3105 secure = 0;
3106#endif
3107 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3108 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3109#ifdef USR_EXRC_FILE2
3110 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3111 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3112#endif
3113 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003114 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003115 }
3116 }
3117 if (secure == 2)
3118 need_wait_return = TRUE;
3119 secure = 0;
3120#ifdef AMIGA
3121 proc->pr_WindowPtr = save_winptr;
3122#endif
3123 }
3124 TIME_MSG("sourcing vimrc file(s)");
3125}
3126
3127/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003128 * Setup to start using the GUI. Exit with an error when not available.
3129 */
3130 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003131main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003132{
3133#ifdef FEAT_GUI
3134 gui.starting = TRUE; /* start GUI a bit later */
3135#else
3136 mch_errmsg(_(e_nogvim));
3137 mch_errmsg("\n");
3138 mch_exit(2);
3139#endif
3140}
3141
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003142#endif /* NO_VIM_MAIN */
3143
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003144/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003145 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003146 * Returns FAIL if the environment variable was not executed, OK otherwise.
3147 */
3148 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003149process_env(
3150 char_u *env,
3151 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003152{
3153 char_u *initstr;
3154 char_u *save_sourcing_name;
3155 linenr_T save_sourcing_lnum;
3156#ifdef FEAT_EVAL
3157 scid_T save_sid;
3158#endif
3159
3160 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3161 {
3162 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003163 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003164 save_sourcing_name = sourcing_name;
3165 save_sourcing_lnum = sourcing_lnum;
3166 sourcing_name = env;
3167 sourcing_lnum = 0;
3168#ifdef FEAT_EVAL
3169 save_sid = current_SID;
3170 current_SID = SID_ENV;
3171#endif
3172 do_cmdline_cmd(initstr);
3173 sourcing_name = save_sourcing_name;
3174 sourcing_lnum = save_sourcing_lnum;
3175#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003176 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003177#endif
3178 return OK;
3179 }
3180 return FAIL;
3181}
3182
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003183#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003184/*
3185 * Return TRUE if we are certain the user owns the file "fname".
3186 * Used for ".vimrc" and ".exrc".
3187 * Use both stat() and lstat() for extra security.
3188 */
3189 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003190file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003191{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003192 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003193# ifdef UNIX
3194 uid_t uid = getuid();
3195# else /* VMS */
3196 uid_t uid = ((getgid() << 16) | getuid());
3197# endif
3198
3199 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3200# ifdef HAVE_LSTAT
3201 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3202# endif
3203 );
3204}
3205#endif
3206
3207/*
3208 * Give an error message main_errors["n"] and exit.
3209 */
3210 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003211mainerr(
3212 int n, /* one of the ME_ defines */
3213 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003214{
3215#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3216 reset_signals(); /* kill us with CTRL-C here, if you like */
3217#endif
3218
3219 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003220 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003221 mch_errmsg(_(main_errors[n]));
3222 if (str != NULL)
3223 {
3224 mch_errmsg(": \"");
3225 mch_errmsg((char *)str);
3226 mch_errmsg("\"");
3227 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003228 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003229
3230 mch_exit(1);
3231}
3232
3233 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003234mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003235{
3236 mainerr(ME_ARG_MISSING, str);
3237}
3238
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003239#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003240/*
3241 * print a message with three spaces prepended and '\n' appended.
3242 */
3243 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003244main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003245{
3246 mch_msg(" ");
3247 mch_msg(s);
3248 mch_msg("\n");
3249}
3250
3251/*
3252 * Print messages for "vim -h" or "vim --help" and exit.
3253 */
3254 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003255usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003256{
3257 int i;
3258 static char *(use[]) =
3259 {
3260 N_("[file ..] edit specified file(s)"),
3261 N_("- read text from stdin"),
3262 N_("-t tag edit file where tag is defined"),
3263#ifdef FEAT_QUICKFIX
3264 N_("-q [errorfile] edit file with first error")
3265#endif
3266 };
3267
3268#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3269 reset_signals(); /* kill us with CTRL-C here, if you like */
3270#endif
3271
3272 mch_msg(longVersion);
3273 mch_msg(_("\n\nusage:"));
3274 for (i = 0; ; ++i)
3275 {
3276 mch_msg(_(" vim [arguments] "));
3277 mch_msg(_(use[i]));
3278 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3279 break;
3280 mch_msg(_("\n or:"));
3281 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003282#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003283 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003284#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003285
3286 mch_msg(_("\n\nArguments:\n"));
3287 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003288#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003289 main_msg(_("--literal\t\tDon't expand wildcards"));
3290#endif
3291#ifdef FEAT_OLE
3292 main_msg(_("-register\t\tRegister this gvim for OLE"));
3293 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3294#endif
3295#ifdef FEAT_GUI
3296 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3297 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3298#endif
3299 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3300 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003301 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003302 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3303#ifdef FEAT_DIFF
3304 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3305#endif
3306 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3307 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3308 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3309 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3310 main_msg(_("-M\t\t\tModifications in text not allowed"));
3311 main_msg(_("-b\t\t\tBinary mode"));
3312#ifdef FEAT_LISP
3313 main_msg(_("-l\t\t\tLisp mode"));
3314#endif
3315 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3316 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003317 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3318#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003319 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003320#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003321 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3322 main_msg(_("-r\t\t\tList swap files and exit"));
3323 main_msg(_("-r (with file name)\tRecover crashed session"));
3324 main_msg(_("-L\t\t\tSame as -r"));
3325#ifdef AMIGA
3326 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3327 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3328#endif
3329#ifdef FEAT_ARABIC
3330 main_msg(_("-A\t\t\tstart in Arabic mode"));
3331#endif
3332#ifdef FEAT_RIGHTLEFT
3333 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3334#endif
3335#ifdef FEAT_FKMAP
3336 main_msg(_("-F\t\t\tStart in Farsi mode"));
3337#endif
3338 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003339 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003340 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3341#ifdef FEAT_GUI
3342 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3343#endif
3344 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003345#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003346 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003347 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3348 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003349#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003350 main_msg(_("+\t\t\tStart at end of file"));
3351 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003352 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003353 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3354 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3355 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3356 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3357 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3358#ifdef FEAT_CRYPT
3359 main_msg(_("-x\t\t\tEdit encrypted files"));
3360#endif
3361#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3362# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3363 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3364# endif
3365 main_msg(_("-X\t\t\tDo not connect to X server"));
3366#endif
3367#ifdef FEAT_CLIENTSERVER
3368 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3369 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3370 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3371 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003372# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003373 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003374# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003375 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3376 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3377 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3378 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3379#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003380#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003381 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003382#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003383#ifdef FEAT_VIMINFO
3384 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3385#endif
3386 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3387 main_msg(_("--version\t\tPrint version information and exit"));
3388
3389#ifdef FEAT_GUI_X11
3390# ifdef FEAT_GUI_MOTIF
3391 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3392# else
3393# ifdef FEAT_GUI_ATHENA
3394# ifdef FEAT_GUI_NEXTAW
3395 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3396# else
3397 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3398# endif
3399# endif
3400# endif
3401 main_msg(_("-display <display>\tRun vim on <display>"));
3402 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003403 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3404 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3405 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3406 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3407 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3408 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3409 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3410 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3411# ifdef FEAT_GUI_ATHENA
3412 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3413# endif
3414 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3415 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3416 main_msg(_("-xrm <resource>\tSet the specified resource"));
3417#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003418#ifdef FEAT_GUI_GTK
3419 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3420 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3421 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3422 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3423 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003424 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003425 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003426 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003427#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003428#ifdef FEAT_GUI_W32
3429 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003430 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003431#endif
3432
3433#ifdef FEAT_GUI_GNOME
3434 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3435 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003436 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003437 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003438 gui.dofork = FALSE;
3439 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003440 else
3441#endif
3442 mch_exit(0);
3443}
3444
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003445#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003446/*
3447 * Check the result of the ATTENTION dialog:
3448 * When "Quit" selected, exit Vim.
3449 * When "Recover" selected, recover the file.
3450 */
3451 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003452check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003453{
3454 if (swap_exists_action == SEA_QUIT)
3455 getout(1);
3456 handle_swap_exists(NULL);
3457}
3458#endif
3459
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003460#endif
3461
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003462#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003463static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003464
3465static struct timeval prev_timeval;
3466
Bram Moolenaar3f269672009-11-03 11:11:11 +00003467# ifdef WIN3264
3468/*
3469 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3470 */
3471 static int
3472gettimeofday(struct timeval *tv, char *dummy)
3473{
3474 long t = clock();
3475 tv->tv_sec = t / CLOCKS_PER_SEC;
3476 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3477 return 0;
3478}
3479# endif
3480
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003481/*
3482 * Save the previous time before doing something that could nest.
3483 * set "*tv_rel" to the time elapsed so far.
3484 */
3485 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003486time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003487{
3488 *((struct timeval *)tv_rel) = prev_timeval;
3489 gettimeofday(&prev_timeval, NULL);
3490 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3491 - ((struct timeval *)tv_rel)->tv_usec;
3492 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3493 - ((struct timeval *)tv_rel)->tv_sec;
3494 if (((struct timeval *)tv_rel)->tv_usec < 0)
3495 {
3496 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3497 --((struct timeval *)tv_rel)->tv_sec;
3498 }
3499 *(struct timeval *)tv_start = prev_timeval;
3500}
3501
3502/*
3503 * Compute the previous time after doing something that could nest.
3504 * Subtract "*tp" from prev_timeval;
3505 * Note: The arguments are (void *) to avoid trouble with systems that don't
3506 * have struct timeval.
3507 */
3508 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003509time_pop(
3510 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003511{
3512 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3513 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3514 if (prev_timeval.tv_usec < 0)
3515 {
3516 prev_timeval.tv_usec += 1000000;
3517 --prev_timeval.tv_sec;
3518 }
3519}
3520
3521 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003522time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003523{
3524 long usec;
3525 long msec;
3526
3527 usec = now->tv_usec - then->tv_usec;
3528 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3529 usec = usec % 1000L;
3530 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3531}
3532
3533 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003534time_msg(
3535 char *mesg,
3536 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003537 (struct timeval *) */
3538{
3539 static struct timeval start;
3540 struct timeval now;
3541
3542 if (time_fd != NULL)
3543 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003544 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003545 {
3546 gettimeofday(&start, NULL);
3547 prev_timeval = start;
3548 fprintf(time_fd, "\n\ntimes in msec\n");
3549 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3550 fprintf(time_fd, " clock elapsed: other lines\n\n");
3551 }
3552 gettimeofday(&now, NULL);
3553 time_diff(&start, &now);
3554 if (((struct timeval *)tv_start) != NULL)
3555 {
3556 fprintf(time_fd, " ");
3557 time_diff(((struct timeval *)tv_start), &now);
3558 }
3559 fprintf(time_fd, " ");
3560 time_diff(&prev_timeval, &now);
3561 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003562 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003563 }
3564}
3565
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003566#endif
3567
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003568#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003569
3570/*
3571 * Common code for the X command server and the Win32 command server.
3572 */
3573
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003574static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003575
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003576/*
3577 * Do the client-server stuff, unless "--servername ''" was used.
3578 */
3579 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003580exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003581{
3582 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3583 {
3584# ifdef WIN32
3585 /* Initialise the client/server messaging infrastructure. */
3586 serverInitMessaging();
3587# endif
3588
3589 /*
3590 * When a command server argument was found, execute it. This may
3591 * exit Vim when it was successful. Otherwise it's executed further
3592 * on. Remember the encoding used here in "serverStrEnc".
3593 */
3594 if (parmp->serverArg)
3595 {
3596 cmdsrv_main(&parmp->argc, parmp->argv,
3597 parmp->serverName_arg, &parmp->serverStr);
3598# ifdef FEAT_MBYTE
3599 parmp->serverStrEnc = vim_strsave(p_enc);
3600# endif
3601 }
3602
3603 /* If we're still running, get the name to register ourselves.
3604 * On Win32 can register right now, for X11 need to setup the
3605 * clipboard first, it's further down. */
3606 parmp->servername = serverMakeName(parmp->serverName_arg,
3607 parmp->argv[0]);
3608# ifdef WIN32
3609 if (parmp->servername != NULL)
3610 {
3611 serverSetName(parmp->servername);
3612 vim_free(parmp->servername);
3613 }
3614# endif
3615 }
3616}
3617
3618/*
3619 * Prepare for running as a Vim server.
3620 */
3621 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003622prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003623{
3624# if defined(FEAT_X11)
3625 /*
3626 * Register for remote command execution with :serversend and --remote
3627 * unless there was a -X or a --servername '' on the command line.
3628 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003629 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003630 */
3631 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3632# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003633 (gui.in_use
3634# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003635 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003636# endif
3637 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003638# endif
3639 parmp->serverName_arg != NULL))
3640 {
3641 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3642 vim_free(parmp->servername);
3643 TIME_MSG("register server name");
3644 }
3645 else
3646 serverDelayedStartName = parmp->servername;
3647# endif
3648
3649 /*
3650 * Execute command ourselves if we're here because the send failed (or
3651 * else we would have exited above).
3652 */
3653 if (parmp->serverStr != NULL)
3654 {
3655 char_u *p;
3656
3657 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3658 parmp->serverStr, &p));
3659 vim_free(p);
3660 }
3661}
3662
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003663 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003664cmdsrv_main(
3665 int *argc,
3666 char **argv,
3667 char_u *serverName_arg,
3668 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003669{
3670 char_u *res;
3671 int i;
3672 char_u *sname;
3673 int ret;
3674 int didone = FALSE;
3675 int exiterr = 0;
3676 char **newArgV = argv + 1;
3677 int newArgC = 1,
3678 Argc = *argc;
3679 int argtype;
3680#define ARGTYPE_OTHER 0
3681#define ARGTYPE_EDIT 1
3682#define ARGTYPE_EDIT_WAIT 2
3683#define ARGTYPE_SEND 3
3684 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003685 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003686# ifndef FEAT_X11
3687 HWND srv;
3688# else
3689 Window srv;
3690
3691 setup_term_clip();
3692# endif
3693
3694 sname = serverMakeName(serverName_arg, argv[0]);
3695 if (sname == NULL)
3696 return;
3697
3698 /*
3699 * Execute the command server related arguments and remove them
3700 * from the argc/argv array; We may have to return into main()
3701 */
3702 for (i = 1; i < Argc; i++)
3703 {
3704 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003705 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003706 {
3707 for (; i < *argc; i++)
3708 {
3709 *newArgV++ = argv[i];
3710 newArgC++;
3711 }
3712 break;
3713 }
3714
Bram Moolenaareb94e552006-03-11 21:35:11 +00003715 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003716 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003717 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3718 {
3719 char *p = argv[i] + 8;
3720
3721 argtype = ARGTYPE_EDIT;
3722 while (*p != NUL)
3723 {
3724 if (STRNICMP(p, "-wait", 5) == 0)
3725 {
3726 argtype = ARGTYPE_EDIT_WAIT;
3727 p += 5;
3728 }
3729 else if (STRNICMP(p, "-silent", 7) == 0)
3730 {
3731 silent = TRUE;
3732 p += 7;
3733 }
3734 else if (STRNICMP(p, "-tab", 4) == 0)
3735 {
3736 tabs = TRUE;
3737 p += 4;
3738 }
3739 else
3740 {
3741 argtype = ARGTYPE_OTHER;
3742 break;
3743 }
3744 }
3745 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003746 else
3747 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003748
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003749 if (argtype != ARGTYPE_OTHER)
3750 {
3751 if (i == *argc - 1)
3752 mainerr_arg_missing((char_u *)argv[i]);
3753 if (argtype == ARGTYPE_SEND)
3754 {
3755 *serverStr = (char_u *)argv[i + 1];
3756 i++;
3757 }
3758 else
3759 {
3760 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003761 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003762 if (*serverStr == NULL)
3763 {
3764 /* Probably out of memory, exit. */
3765 didone = TRUE;
3766 exiterr = 1;
3767 break;
3768 }
3769 Argc = i;
3770 }
3771# ifdef FEAT_X11
3772 if (xterm_dpy == NULL)
3773 {
3774 mch_errmsg(_("No display"));
3775 ret = -1;
3776 }
3777 else
3778 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3779 NULL, &srv, 0, 0, silent);
3780# else
3781 /* Win32 always works? */
3782 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3783# endif
3784 if (ret < 0)
3785 {
3786 if (argtype == ARGTYPE_SEND)
3787 {
3788 /* Failed to send, abort. */
3789 mch_errmsg(_(": Send failed.\n"));
3790 didone = TRUE;
3791 exiterr = 1;
3792 }
3793 else if (!silent)
3794 /* Let vim start normally. */
3795 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3796 break;
3797 }
3798
3799# ifdef FEAT_GUI_W32
3800 /* Guess that when the server name starts with "g" it's a GUI
3801 * server, which we can bring to the foreground here.
3802 * Foreground() in the server doesn't work very well. */
3803 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3804 SetForegroundWindow(srv);
3805# endif
3806
3807 /*
3808 * For --remote-wait: Wait until the server did edit each
3809 * file. Also detect that the server no longer runs.
3810 */
3811 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3812 {
3813 int numFiles = *argc - i - 1;
3814 int j;
3815 char_u *done = alloc(numFiles);
3816 char_u *p;
3817# ifdef FEAT_GUI_W32
3818 NOTIFYICONDATA ni;
3819 int count = 0;
3820 extern HWND message_window;
3821# endif
3822
3823 if (numFiles > 0 && argv[i + 1][0] == '+')
3824 /* Skip "+cmd" argument, don't wait for it to be edited. */
3825 --numFiles;
3826
3827# ifdef FEAT_GUI_W32
3828 ni.cbSize = sizeof(ni);
3829 ni.hWnd = message_window;
3830 ni.uID = 0;
3831 ni.uFlags = NIF_ICON|NIF_TIP;
3832 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3833 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3834 Shell_NotifyIcon(NIM_ADD, &ni);
3835# endif
3836
3837 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003838 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003839 while (memchr(done, 0, numFiles) != NULL)
3840 {
3841# ifdef WIN32
3842 p = serverGetReply(srv, NULL, TRUE, TRUE);
3843 if (p == NULL)
3844 break;
3845# else
3846 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3847 break;
3848# endif
3849 j = atoi((char *)p);
3850 if (j >= 0 && j < numFiles)
3851 {
3852# ifdef FEAT_GUI_W32
3853 ++count;
3854 sprintf(ni.szTip, _("%d of %d edited"),
3855 count, numFiles);
3856 Shell_NotifyIcon(NIM_MODIFY, &ni);
3857# endif
3858 done[j] = 1;
3859 }
3860 }
3861# ifdef FEAT_GUI_W32
3862 Shell_NotifyIcon(NIM_DELETE, &ni);
3863# endif
3864 }
3865 }
3866 else if (STRICMP(argv[i], "--remote-expr") == 0)
3867 {
3868 if (i == *argc - 1)
3869 mainerr_arg_missing((char_u *)argv[i]);
3870# ifdef WIN32
3871 /* Win32 always works? */
3872 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3873 &res, NULL, 1, FALSE) < 0)
3874# else
3875 if (xterm_dpy == NULL)
3876 mch_errmsg(_("No display: Send expression failed.\n"));
3877 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3878 &res, NULL, 1, 1, FALSE) < 0)
3879# endif
3880 {
3881 if (res != NULL && *res != NUL)
3882 {
3883 /* Output error from remote */
3884 mch_errmsg((char *)res);
3885 vim_free(res);
3886 res = NULL;
3887 }
3888 mch_errmsg(_(": Send expression failed.\n"));
3889 }
3890 }
3891 else if (STRICMP(argv[i], "--serverlist") == 0)
3892 {
3893# ifdef WIN32
3894 /* Win32 always works? */
3895 res = serverGetVimNames();
3896# else
3897 if (xterm_dpy != NULL)
3898 res = serverGetVimNames(xterm_dpy);
3899# endif
3900 if (called_emsg)
3901 mch_errmsg("\n");
3902 }
3903 else if (STRICMP(argv[i], "--servername") == 0)
3904 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003905 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003906 i++;
3907 continue;
3908 }
3909 else
3910 {
3911 *newArgV++ = argv[i];
3912 newArgC++;
3913 continue;
3914 }
3915 didone = TRUE;
3916 if (res != NULL && *res != NUL)
3917 {
3918 mch_msg((char *)res);
3919 if (res[STRLEN(res) - 1] != '\n')
3920 mch_msg("\n");
3921 }
3922 vim_free(res);
3923 }
3924
3925 if (didone)
3926 {
3927 display_errors(); /* display any collected messages */
3928 exit(exiterr); /* Mission accomplished - get out */
3929 }
3930
3931 /* Return back into main() */
3932 *argc = newArgC;
3933 vim_free(sname);
3934}
3935
3936/*
3937 * Build a ":drop" command to send to a Vim server.
3938 */
3939 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003940build_drop_cmd(
3941 int filec,
3942 char **filev,
3943 int tabs, /* Use ":tab drop" instead of ":drop". */
3944 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003945{
3946 garray_T ga;
3947 int i;
3948 char_u *inicmd = NULL;
3949 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003950 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003951 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003952
3953 if (filec > 0 && filev[0][0] == '+')
3954 {
3955 inicmd = (char_u *)filev[0] + 1;
3956 filev++;
3957 filec--;
3958 }
3959 /* Check if we have at least one argument. */
3960 if (filec <= 0)
3961 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003962
3963 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003964 cwd = alloc(MAXPATHL);
3965 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003966 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003967 if (mch_dirname(cwd, MAXPATHL) != OK)
3968 {
3969 vim_free(cwd);
3970 return NULL;
3971 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003972 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003973#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003974 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00003975#else
3976 PATH_ESC_CHARS,
3977#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003978 '\\', TRUE);
3979 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003980 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003981 return NULL;
3982 ga_init2(&ga, 1, 100);
3983 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003984 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003985
3986 /* Call inputsave() so that a prompt for an encryption key works. */
3987 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3988 if (tabs)
3989 ga_concat(&ga, (char_u *)"tab ");
3990 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003991 for (i = 0; i < filec; i++)
3992 {
3993 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003994 * do it again in the Vim server. On MS-Windows only escape
3995 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003996 p = vim_strsave_escaped((char_u *)filev[i],
3997#ifdef UNIX
3998 PATH_ESC_CHARS
3999#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004000 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004001#endif
4002 );
4003 if (p == NULL)
4004 {
4005 vim_free(ga.ga_data);
4006 return NULL;
4007 }
4008 ga_concat(&ga, (char_u *)" ");
4009 ga_concat(&ga, p);
4010 vim_free(p);
4011 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004012 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4013
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004014 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4015 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004016 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4017
4018 /* Switch back to the correct current directory (prior to temporary path
4019 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004020 * correct after the :drop command. With line breaks and spaces:
4021 * if !exists('+acd') || !&acd
4022 * if haslocaldir()
4023 * cd -
4024 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004025 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004026 * cd -
4027 * endif
4028 * endif
4029 */
4030 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004031 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004032 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004033 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004034 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004035
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004036 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004037 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4038 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004039 if (inicmd != NULL)
4040 {
4041 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4042 * the following commands to be inserted as text. Use a "|",
4043 * hopefully "inicmd" does allow this... */
4044 ga_concat(&ga, inicmd);
4045 ga_concat(&ga, (char_u *)"|");
4046 }
4047 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4048 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004049 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004050 ga_append(&ga, NUL);
4051 return ga.ga_data;
4052}
4053
4054/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004055 * Make our basic server name: use the specified "arg" if given, otherwise use
4056 * the tail of the command "cmd" we were started with.
4057 * Return the name in allocated memory. This doesn't include a serial number.
4058 */
4059 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004060serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004061{
4062 char_u *p;
4063
4064 if (arg != NULL && *arg != NUL)
4065 p = vim_strsave_up(arg);
4066 else
4067 {
4068 p = vim_strsave_up(gettail((char_u *)cmd));
4069 /* Remove .exe or .bat from the name. */
4070 if (p != NULL && vim_strchr(p, '.') != NULL)
4071 *vim_strchr(p, '.') = NUL;
4072 }
4073 return p;
4074}
4075#endif /* FEAT_CLIENTSERVER */
4076
4077#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4078/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004079 * Replace termcodes such as <CR> and insert as key presses if there is room.
4080 */
4081 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004082server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004083{
4084 char_u *ptr = NULL;
4085 char_u *cpo_save = p_cpo;
4086
4087 /* Set 'cpoptions' the way we want it.
4088 * B set - backslashes are *not* treated specially
4089 * k set - keycodes are *not* reverse-engineered
4090 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004091 * The last but one parameter of replace_termcodes() is TRUE so that the
4092 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004093 */
4094 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004095 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004096 p_cpo = cpo_save;
4097
4098 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4099 {
4100 /*
4101 * Add the string to the input stream.
4102 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4103 *
4104 * First clear typed characters from the typeahead buffer, there could
4105 * be half a mapping there. Then append to the existing string, so
4106 * that multiple commands from a client are concatenated.
4107 */
4108 if (typebuf.tb_maplen < typebuf.tb_len)
4109 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4110 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4111
4112 /* Let input_available() know we inserted text in the typeahead
4113 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004114 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004115 }
4116 vim_free((char_u *)ptr);
4117}
4118
4119/*
4120 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004121 */
4122 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004123eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004124{
4125 char_u *res;
4126 int save_dbl = debug_break_level;
4127 int save_ro = redir_off;
4128
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004129 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4130 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004131 debug_break_level = -1;
4132 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004133 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4134 * to be typed. Do generate errors so that try/catch works. */
4135 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004136
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004137 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004138
4139 debug_break_level = save_dbl;
4140 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004141 --emsg_silent;
4142 if (emsg_silent < 0)
4143 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004144
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004145 /* A client can tell us to redraw, but not to display the cursor, so do
4146 * that here. */
4147 setcursor();
4148 out_flush();
4149#ifdef FEAT_GUI
4150 if (gui.in_use)
4151 gui_update_cursor(FALSE, FALSE);
4152#endif
4153
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004154 return res;
4155}
4156
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004157/*
4158 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4159 * return an allocated string. Otherwise return "data".
4160 * "*tofree" is set to the result when it needs to be freed later.
4161 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004162 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004163serverConvert(
4164 char_u *client_enc UNUSED,
4165 char_u *data,
4166 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004167{
4168 char_u *res = data;
4169
4170 *tofree = NULL;
4171# ifdef FEAT_MBYTE
4172 if (client_enc != NULL && p_enc != NULL)
4173 {
4174 vimconv_T vimconv;
4175
4176 vimconv.vc_type = CONV_NONE;
4177 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4178 && vimconv.vc_type != CONV_NONE)
4179 {
4180 res = string_convert(&vimconv, data, NULL);
4181 if (res == NULL)
4182 res = data;
4183 else
4184 *tofree = res;
4185 }
4186 convert_setup(&vimconv, NULL, NULL);
4187 }
4188# endif
4189 return res;
4190}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004191#endif