blob: 958e9c8fc63fcadd997e7a9d04e4bf49a9daef91 [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
1276 /* display message after redraw */
1277 if (keep_msg != NULL)
1278 {
1279 char_u *p;
1280
1281 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001282 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1283 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001284 p = keep_msg;
1285 msg_attr(p, keep_msg_attr);
1286 vim_free(p);
1287 }
1288 if (need_fileinfo) /* show file info after redraw */
1289 {
1290 fileinfo(FALSE, TRUE, FALSE);
1291 need_fileinfo = FALSE;
1292 }
1293
1294 emsg_on_display = FALSE; /* can delete error message now */
1295 did_emsg = FALSE;
1296 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001297 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001298 showruler(FALSE);
1299
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001300# if defined(FEAT_CONCEAL)
1301 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001302 && (conceal_old_cursor_line != conceal_new_cursor_line
1303 || conceal_cursor_line(curwin)
1304 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001305 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001306 if (conceal_old_cursor_line != conceal_new_cursor_line
1307 && conceal_old_cursor_line
1308 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001309 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001310 update_single_line(curwin, conceal_new_cursor_line);
1311 curwin->w_valid &= ~VALID_CROW;
1312 }
1313# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001314 setcursor();
1315 cursor_on();
1316
1317 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001318
1319#ifdef STARTUPTIME
1320 /* Now that we have drawn the first screen all the startup stuff
1321 * has been done, close any file for startup messages. */
1322 if (time_fd != NULL)
1323 {
1324 TIME_MSG("first screen update");
1325 TIME_MSG("--- VIM STARTED ---");
1326 fclose(time_fd);
1327 time_fd = NULL;
1328 }
1329#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001330 }
1331#ifdef FEAT_GUI
1332 if (need_mouse_correct)
1333 gui_mouse_correct();
1334#endif
1335
1336 /*
1337 * Update w_curswant if w_set_curswant has been set.
1338 * Postponed until here to avoid computing w_virtcol too often.
1339 */
1340 update_curswant();
1341
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001342#ifdef FEAT_EVAL
1343 /*
1344 * May perform garbage collection when waiting for a character, but
1345 * only at the very toplevel. Otherwise we may be using a List or
1346 * Dict internally somewhere.
1347 * "may_garbage_collect" is reset in vgetc() which is invoked through
1348 * do_exmode() and normal_cmd().
1349 */
1350 may_garbage_collect = (!cmdwin && !noexmode);
1351#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001352 /*
1353 * If we're invoked as ex, do a round of ex commands.
1354 * Otherwise, get and execute a normal mode command.
1355 */
1356 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001357 {
1358 if (noexmode) /* End of ":global/path/visual" commands */
1359 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001360 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001361 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001362 else
1363 normal_cmd(&oa, TRUE);
1364 }
1365}
1366
1367
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001368#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001369/*
1370 * Exit, but leave behind swap files for modified buffers.
1371 */
1372 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001373getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001374{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001375# if defined(SIGHUP) && defined(SIG_IGN)
1376 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1377 * makes Vim exit and then handling SIGHUP causes various reentrance
1378 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001379 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001380# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001381
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001382 ml_close_notmod(); /* close all not-modified buffers */
1383 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1384 ml_close_all(FALSE); /* close all memfiles, without deleting */
1385 getout(exitval); /* exit Vim properly */
1386}
1387#endif
1388
1389
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001390/*
1391 * Exit properly.
1392 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001393 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001394getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001395{
1396#ifdef FEAT_AUTOCMD
1397 buf_T *buf;
1398 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001399 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001400#endif
1401
1402 exiting = TRUE;
1403
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001404 /* When running in Ex mode an error causes us to exit with a non-zero exit
1405 * code. POSIX requires this, although it's not 100% clear from the
1406 * standard. */
1407 if (exmode_active)
1408 exitval += ex_exitval;
1409
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001410 /* Position the cursor on the last screen line, below all the text */
1411#ifdef FEAT_GUI
1412 if (!gui.in_use)
1413#endif
1414 windgoto((int)Rows - 1, 0);
1415
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001416#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1417 /* Optionally print hashtable efficiency. */
1418 hash_debug_results();
1419#endif
1420
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001421#ifdef FEAT_GUI
1422 msg_didany = FALSE;
1423#endif
1424
1425#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001426 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001427 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001428 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1429# if defined FEAT_WINDOWS
1430 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001431 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001432 next_tp = tp->tp_next;
1433 for (wp = (tp == curtab)
1434 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001435 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001436 if (wp->w_buffer == NULL)
1437 /* Autocmd must have close the buffer already, skip. */
1438 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001439 buf = wp->w_buffer;
1440 if (buf->b_changedtick != -1)
1441 {
1442 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1443 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001444 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001445 /* start all over, autocommands may mess up the lists */
1446 next_tp = first_tabpage;
1447 break;
1448 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001449 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001450 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001451# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001452 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1453 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001454# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001455
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001456 /* Trigger BufUnload for buffers that are loaded */
1457 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1458 if (buf->b_ml.ml_mfp != NULL)
1459 {
1460 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001461 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001462 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1463 break;
1464 }
1465 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1466 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001467#endif
1468
1469#ifdef FEAT_VIMINFO
1470 if (*p_viminfo != NUL)
1471 /* Write out the registers, history, marks etc, to the viminfo file */
1472 write_viminfo(NULL, FALSE);
1473#endif
1474
1475#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001476 if (get_vim_var_nr(VV_DYING) <= 1)
1477 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001478#endif
1479
Bram Moolenaar05159a02005-02-26 23:04:13 +00001480#ifdef FEAT_PROFILE
1481 profile_dump();
1482#endif
1483
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001484 if (did_emsg
1485#ifdef FEAT_GUI
1486 || (gui.in_use && msg_didany && p_verbose > 0)
1487#endif
1488 )
1489 {
1490 /* give the user a chance to read the (error) message */
1491 no_wait_return = FALSE;
1492 wait_return(FALSE);
1493 }
1494
1495#ifdef FEAT_AUTOCMD
1496 /* Position the cursor again, the autocommands may have moved it */
1497# ifdef FEAT_GUI
1498 if (!gui.in_use)
1499# endif
1500 windgoto((int)Rows - 1, 0);
1501#endif
1502
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001503#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001504 job_stop_on_exit();
1505#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001506#ifdef FEAT_LUA
1507 lua_end();
1508#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001509#ifdef FEAT_MZSCHEME
1510 mzscheme_end();
1511#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001512#ifdef FEAT_TCL
1513 tcl_end();
1514#endif
1515#ifdef FEAT_RUBY
1516 ruby_end();
1517#endif
1518#ifdef FEAT_PYTHON
1519 python_end();
1520#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001521#ifdef FEAT_PYTHON3
1522 python3_end();
1523#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001524#ifdef FEAT_PERL
1525 perl_end();
1526#endif
1527#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1528 iconv_end();
1529#endif
1530#ifdef FEAT_NETBEANS_INTG
1531 netbeans_end();
1532#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001533#ifdef FEAT_CSCOPE
1534 cs_end();
1535#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001536#ifdef FEAT_EVAL
1537 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001538 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001539#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001540#if defined(WIN32) && defined(FEAT_MBYTE)
1541 free_cmd_argsW();
1542#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001543
1544 mch_exit(exitval);
1545}
1546
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001547#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001548/*
1549 * Get a (optional) count for a Vim argument.
1550 */
1551 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001552get_number_arg(
1553 char_u *p, /* pointer to argument */
1554 int *idx, /* index in argument, is incremented */
1555 int def) /* default value */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001556{
1557 if (vim_isdigit(p[*idx]))
1558 {
1559 def = atoi((char *)&(p[*idx]));
1560 while (vim_isdigit(p[*idx]))
1561 *idx = *idx + 1;
1562 }
1563 return def;
1564}
1565
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001566#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1567/*
1568 * Setup to use the current locale (for ctype() and many other things).
1569 */
1570 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001571init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001572{
1573 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001574
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001575# ifdef FEAT_GUI_GTK
1576 /* Tell Gtk not to change our locale settings. */
1577 gtk_disable_setlocale();
1578# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001579# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1580 /* Make sure strtod() uses a decimal point, not a comma. */
1581 setlocale(LC_NUMERIC, "C");
1582# endif
1583
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001584# ifdef WIN32
1585 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1586 * text while it's expecting text in the current locale. This call avoids
1587 * that. */
1588 setlocale(LC_CTYPE, "C");
1589# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001590
1591# ifdef FEAT_GETTEXT
1592 {
1593 int mustfree = FALSE;
1594 char_u *p;
1595
1596# ifdef DYNAMIC_GETTEXT
1597 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001598 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001599# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001600 /* expand_env() doesn't work yet, because g_chartab[] is not
1601 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001602 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1603 if (p != NULL && *p != NUL)
1604 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001605 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001606 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1607 }
1608 if (mustfree)
1609 vim_free(p);
1610 textdomain(VIMPACKAGE);
1611 }
1612# endif
1613}
1614#endif
1615
1616/*
1617 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1618 * If the executable name starts with "r" we disable shell commands.
1619 * If the next character is "e" we run in Easy mode.
1620 * If the next character is "g" we run the GUI version.
1621 * If the next characters are "view" we start in readonly mode.
1622 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1623 * If the next characters are "ex" we start in Ex mode. If it's followed
1624 * by "im" use improved Ex mode.
1625 */
1626 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001627parse_command_name(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001628{
1629 char_u *initstr;
1630
1631 initstr = gettail((char_u *)parmp->argv[0]);
1632
1633#ifdef MACOS_X_UNIX
1634 /* An issue has been seen when launching Vim in such a way that
1635 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1636 * executable or a symbolic link of it. Until this issue is resolved
1637 * we prohibit the GUI from being used.
1638 */
1639 if (STRCMP(initstr, parmp->argv[0]) == 0)
1640 disallow_gui = TRUE;
1641
1642 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001643 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001644#endif
1645
1646#ifdef FEAT_EVAL
1647 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaara1706c92014-04-01 19:55:49 +02001648 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001649#endif
1650
1651 if (TOLOWER_ASC(initstr[0]) == 'r')
1652 {
1653 restricted = TRUE;
1654 ++initstr;
1655 }
1656
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001657 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001658 if (TOLOWER_ASC(initstr[0]) == 'e'
1659 && (TOLOWER_ASC(initstr[1]) == 'v'
1660 || TOLOWER_ASC(initstr[1]) == 'g'))
1661 {
1662#ifdef FEAT_GUI
1663 gui.starting = TRUE;
1664#endif
1665 parmp->evim_mode = TRUE;
1666 ++initstr;
1667 }
1668
Bram Moolenaar806875d2008-09-18 18:57:10 +00001669 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1670 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001671 {
1672 main_start_gui();
1673#ifdef FEAT_GUI
1674 ++initstr;
1675#endif
1676 }
1677
1678 if (STRNICMP(initstr, "view", 4) == 0)
1679 {
1680 readonlymode = TRUE;
1681 curbuf->b_p_ro = TRUE;
1682 p_uc = 10000; /* don't update very often */
1683 initstr += 4;
1684 }
1685 else if (STRNICMP(initstr, "vim", 3) == 0)
1686 initstr += 3;
1687
1688 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1689 if (STRICMP(initstr, "diff") == 0)
1690 {
1691#ifdef FEAT_DIFF
1692 parmp->diff_mode = TRUE;
1693#else
1694 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1695 mch_errmsg("\n");
1696 mch_exit(2);
1697#endif
1698 }
1699
1700 if (STRNICMP(initstr, "ex", 2) == 0)
1701 {
1702 if (STRNICMP(initstr + 2, "im", 2) == 0)
1703 exmode_active = EXMODE_VIM;
1704 else
1705 exmode_active = EXMODE_NORMAL;
1706 change_compatible(TRUE); /* set 'compatible' */
1707 }
1708}
1709
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001710/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001711 * Get the name of the display, before gui_prepare() removes it from
1712 * argv[]. Used for the xterm-clipboard display.
1713 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001714 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001715 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001716 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001717early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001718{
Bram Moolenaar03005972008-11-20 13:12:36 +00001719#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1720 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001721 int argc = parmp->argc;
1722 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001723 int i;
1724
1725 for (i = 1; i < argc; i++)
1726 {
1727 if (STRCMP(argv[i], "--") == 0)
1728 break;
1729# ifdef FEAT_XCLIPBOARD
1730 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001731# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001732 || STRICMP(argv[i], "--display") == 0
1733# endif
1734 )
1735 {
1736 if (i == argc - 1)
1737 mainerr_arg_missing((char_u *)argv[i]);
1738 xterm_display = argv[++i];
1739 }
1740# endif
1741# ifdef FEAT_CLIENTSERVER
1742 else if (STRICMP(argv[i], "--servername") == 0)
1743 {
1744 if (i == argc - 1)
1745 mainerr_arg_missing((char_u *)argv[i]);
1746 parmp->serverName_arg = (char_u *)argv[++i];
1747 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001748 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001749 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001750 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001751 {
1752 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001753# ifdef FEAT_GUI
1754 if (strstr(argv[i], "-wait") != 0)
1755 /* don't fork() when starting the GUI to edit files ourself */
1756 gui.dofork = FALSE;
1757# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001758 }
1759# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001760
1761# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1762# ifdef FEAT_GUI_W32
1763 else if (STRICMP(argv[i], "--windowid") == 0)
1764# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001765 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001766# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001767 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001768 long_u id;
1769 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001770
1771 if (i == argc - 1)
1772 mainerr_arg_missing((char_u *)argv[i]);
1773 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001774 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001775 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001776 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001777 if (count != 1)
1778 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1779 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001780# ifdef FEAT_GUI_W32
1781 win_socket_id = id;
1782# else
1783 gtk_socket_id = id;
1784# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001785 i++;
1786 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001787# endif
1788# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001789 else if (STRICMP(argv[i], "--echo-wid") == 0)
1790 echo_wid_arg = TRUE;
1791# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001792# ifndef FEAT_NETBEANS_INTG
1793 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001794 {
1795 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1796 mch_exit(2);
1797 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001798# endif
1799
Bram Moolenaar58d98232005-07-23 22:25:46 +00001800 }
1801#endif
1802}
1803
1804/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001805 * Scan the command line arguments.
1806 */
1807 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001808command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001809{
1810 int argc = parmp->argc;
1811 char **argv = parmp->argv;
1812 int argv_idx; /* index in argv[n][] */
1813 int had_minmin = FALSE; /* found "--" argument */
1814 int want_argument; /* option argument with argument */
1815 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001816 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001817 long n;
1818
1819 --argc;
1820 ++argv;
1821 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1822 while (argc > 0)
1823 {
1824 /*
1825 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1826 */
1827 if (argv[0][0] == '+' && !had_minmin)
1828 {
1829 if (parmp->n_commands >= MAX_ARG_CMDS)
1830 mainerr(ME_EXTRA_CMD, NULL);
1831 argv_idx = -1; /* skip to next argument */
1832 if (argv[0][1] == NUL)
1833 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1834 else
1835 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1836 }
1837
1838 /*
1839 * Optional argument.
1840 */
1841 else if (argv[0][0] == '-' && !had_minmin)
1842 {
1843 want_argument = FALSE;
1844 c = argv[0][argv_idx++];
1845#ifdef VMS
1846 /*
1847 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1848 * and "-/X" as "-X".
1849 */
1850 if (c == '/')
1851 {
1852 c = argv[0][argv_idx++];
1853 c = TOUPPER_ASC(c);
1854 }
1855 else
1856 c = TOLOWER_ASC(c);
1857#endif
1858 switch (c)
1859 {
1860 case NUL: /* "vim -" read from stdin */
1861 /* "ex -" silent mode */
1862 if (exmode_active)
1863 silent_mode = TRUE;
1864 else
1865 {
1866 if (parmp->edit_type != EDIT_NONE)
1867 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1868 parmp->edit_type = EDIT_STDIN;
1869 read_cmd_fd = 2; /* read from stderr instead of stdin */
1870 }
1871 argv_idx = -1; /* skip to next argument */
1872 break;
1873
1874 case '-': /* "--" don't take any more option arguments */
1875 /* "--help" give help message */
1876 /* "--version" give version message */
1877 /* "--literal" take files literally */
1878 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001879 /* "--not-a-term" don't warn for not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001880 /* "--noplugin[s]" skip plugins */
1881 /* "--cmd <cmd>" execute cmd before vimrc */
1882 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1883 usage();
1884 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1885 {
1886 Columns = 80; /* need to init Columns */
1887 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1888 list_version();
1889 msg_putchar('\n');
1890 msg_didout = FALSE;
1891 mch_exit(0);
1892 }
1893 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1894 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001895#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001896 parmp->literal = TRUE;
1897#endif
1898 }
1899 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1900 {
1901#ifdef FEAT_GUI
1902 gui.dofork = FALSE; /* don't fork() when starting GUI */
1903#endif
1904 }
1905 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1906 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001907 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1908 parmp->not_a_term = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001909 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1910 {
1911 want_argument = TRUE;
1912 argv_idx += 3;
1913 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001914 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1915 {
1916 want_argument = TRUE;
1917 argv_idx += 11;
1918 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001919#ifdef FEAT_CLIENTSERVER
1920 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1921 ; /* already processed -- no arg */
1922 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1923 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1924 {
1925 /* already processed -- snatch the following arg */
1926 if (argc > 1)
1927 {
1928 --argc;
1929 ++argv;
1930 }
1931 }
1932#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001933#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1934# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001935 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001936# else
1937 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1938# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001939 {
1940 /* already processed -- snatch the following arg */
1941 if (argc > 1)
1942 {
1943 --argc;
1944 ++argv;
1945 }
1946 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001947#endif
1948#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001949 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1950 {
1951 /* already processed, skip */
1952 }
1953#endif
1954 else
1955 {
1956 if (argv[0][argv_idx])
1957 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1958 had_minmin = TRUE;
1959 }
1960 if (!want_argument)
1961 argv_idx = -1; /* skip to next argument */
1962 break;
1963
1964 case 'A': /* "-A" start in Arabic mode */
1965#ifdef FEAT_ARABIC
1966 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1967#else
1968 mch_errmsg(_(e_noarabic));
1969 mch_exit(2);
1970#endif
1971 break;
1972
1973 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001974 /* Needs to be effective before expanding file names, because
1975 * for Win32 this makes us edit a shortcut file itself,
1976 * instead of the file it links to. */
1977 set_options_bin(curbuf->b_p_bin, 1, 0);
1978 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001979 break;
1980
1981 case 'C': /* "-C" Compatible */
1982 change_compatible(TRUE);
1983 break;
1984
1985 case 'e': /* "-e" Ex mode */
1986 exmode_active = EXMODE_NORMAL;
1987 break;
1988
1989 case 'E': /* "-E" Improved Ex mode */
1990 exmode_active = EXMODE_VIM;
1991 break;
1992
1993 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1994 window directly, not with newcli */
1995#ifdef FEAT_GUI
1996 gui.dofork = FALSE; /* don't fork() when starting GUI */
1997#endif
1998 break;
1999
2000 case 'g': /* "-g" start GUI */
2001 main_start_gui();
2002 break;
2003
2004 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
2005#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002006 p_fkmap = TRUE;
2007 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002008#else
2009 mch_errmsg(_(e_nofarsi));
2010 mch_exit(2);
2011#endif
2012 break;
2013
2014 case 'h': /* "-h" give help message */
2015#ifdef FEAT_GUI_GNOME
2016 /* Tell usage() to exit for "gvim". */
2017 gui.starting = FALSE;
2018#endif
2019 usage();
2020 break;
2021
2022 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2023#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002024 p_hkmap = TRUE;
2025 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002026#else
2027 mch_errmsg(_(e_nohebrew));
2028 mch_exit(2);
2029#endif
2030 break;
2031
2032 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2033#ifdef FEAT_LISP
2034 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2035 p_sm = TRUE;
2036#endif
2037 break;
2038
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002039 case 'M': /* "-M" no changes or writing of files */
2040 reset_modifiable();
2041 /* FALLTHROUGH */
2042
2043 case 'm': /* "-m" no writing of files */
2044 p_write = FALSE;
2045 break;
2046
2047 case 'y': /* "-y" easy mode */
2048#ifdef FEAT_GUI
2049 gui.starting = TRUE; /* start GUI a bit later */
2050#endif
2051 parmp->evim_mode = TRUE;
2052 break;
2053
2054 case 'N': /* "-N" Nocompatible */
2055 change_compatible(FALSE);
2056 break;
2057
2058 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002059#ifdef FEAT_NETBEANS_INTG
2060 /* checking for "-nb", netbeans parameters */
2061 if (argv[0][argv_idx] == 'b')
2062 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002063 netbeansArg = argv[0];
2064 argv_idx = -1; /* skip to next argument */
2065 }
2066 else
2067#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002068 parmp->no_swap_file = TRUE;
2069 break;
2070
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002071 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002072#ifdef TARGET_API_MAC_OSX
2073 /* For some reason on MacOS X, an argument like:
2074 -psn_0_10223617 is passed in when invoke from Finder
2075 or with the 'open' command */
2076 if (argv[0][argv_idx] == 's')
2077 {
2078 argv_idx = -1; /* bypass full -psn */
2079 main_start_gui();
2080 break;
2081 }
2082#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002083#ifdef FEAT_WINDOWS
2084 /* default is 0: open window for each file */
2085 parmp->window_count = get_number_arg((char_u *)argv[0],
2086 &argv_idx, 0);
2087 parmp->window_layout = WIN_TABS;
2088#endif
2089 break;
2090
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002091 case 'o': /* "-o[N]" open N horizontal split windows */
2092#ifdef FEAT_WINDOWS
2093 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002094 parmp->window_count = get_number_arg((char_u *)argv[0],
2095 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002096 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002097#endif
2098 break;
2099
2100 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002101#ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002102 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002103 parmp->window_count = get_number_arg((char_u *)argv[0],
2104 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002105 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002106#endif
2107 break;
2108
2109#ifdef FEAT_QUICKFIX
2110 case 'q': /* "-q" QuickFix mode */
2111 if (parmp->edit_type != EDIT_NONE)
2112 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2113 parmp->edit_type = EDIT_QF;
2114 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2115 {
2116 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2117 argv_idx = -1;
2118 }
2119 else if (argc > 1) /* "-q {errorfile}" */
2120 want_argument = TRUE;
2121 break;
2122#endif
2123
2124 case 'R': /* "-R" readonly mode */
2125 readonlymode = TRUE;
2126 curbuf->b_p_ro = TRUE;
2127 p_uc = 10000; /* don't update very often */
2128 break;
2129
2130 case 'r': /* "-r" recovery mode */
2131 case 'L': /* "-L" recovery mode */
2132 recoverymode = 1;
2133 break;
2134
2135 case 's':
2136 if (exmode_active) /* "-s" silent (batch) mode */
2137 silent_mode = TRUE;
2138 else /* "-s {scriptin}" read from script file */
2139 want_argument = TRUE;
2140 break;
2141
2142 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2143 if (parmp->edit_type != EDIT_NONE)
2144 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2145 parmp->edit_type = EDIT_TAG;
2146 if (argv[0][argv_idx]) /* "-t{tag}" */
2147 {
2148 parmp->tagname = (char_u *)argv[0] + argv_idx;
2149 argv_idx = -1;
2150 }
2151 else /* "-t {tag}" */
2152 want_argument = TRUE;
2153 break;
2154
2155#ifdef FEAT_EVAL
2156 case 'D': /* "-D" Debugging */
2157 parmp->use_debug_break_level = 9999;
2158 break;
2159#endif
2160#ifdef FEAT_DIFF
2161 case 'd': /* "-d" 'diff' */
2162# ifdef AMIGA
2163 /* check for "-dev {device}" */
2164 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2165 want_argument = TRUE;
2166 else
2167# endif
2168 parmp->diff_mode = TRUE;
2169 break;
2170#endif
2171 case 'V': /* "-V{N}" Verbose level */
2172 /* default is 10: a little bit verbose */
2173 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2174 if (argv[0][argv_idx] != NUL)
2175 {
2176 set_option_value((char_u *)"verbosefile", 0L,
2177 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002178 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002179 }
2180 break;
2181
2182 case 'v': /* "-v" Vi-mode (as if called "vi") */
2183 exmode_active = 0;
2184#ifdef FEAT_GUI
2185 gui.starting = FALSE; /* don't start GUI */
2186#endif
2187 break;
2188
2189 case 'w': /* "-w{number}" set window height */
2190 /* "-w {scriptout}" write to script */
2191 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2192 {
2193 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2194 set_option_value((char_u *)"window", n, NULL, 0);
2195 break;
2196 }
2197 want_argument = TRUE;
2198 break;
2199
2200#ifdef FEAT_CRYPT
2201 case 'x': /* "-x" encrypted reading/writing of files */
2202 parmp->ask_for_key = TRUE;
2203 break;
2204#endif
2205
2206 case 'X': /* "-X" don't connect to X server */
2207#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2208 x_no_connect = TRUE;
2209#endif
2210 break;
2211
2212 case 'Z': /* "-Z" restricted mode */
2213 restricted = TRUE;
2214 break;
2215
2216 case 'c': /* "-c{command}" or "-c {command}" execute
2217 command */
2218 if (argv[0][argv_idx] != NUL)
2219 {
2220 if (parmp->n_commands >= MAX_ARG_CMDS)
2221 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002222 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2223 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002224 argv_idx = -1;
2225 break;
2226 }
2227 /*FALLTHROUGH*/
2228 case 'S': /* "-S {file}" execute Vim script */
2229 case 'i': /* "-i {viminfo}" use for viminfo */
2230#ifndef FEAT_DIFF
2231 case 'd': /* "-d {device}" device (for Amiga) */
2232#endif
2233 case 'T': /* "-T {terminal}" terminal name */
2234 case 'u': /* "-u {vimrc}" vim inits file */
2235 case 'U': /* "-U {gvimrc}" gvim inits file */
2236 case 'W': /* "-W {scriptout}" overwrite */
2237#ifdef FEAT_GUI_W32
2238 case 'P': /* "-P {parent title}" MDI parent */
2239#endif
2240 want_argument = TRUE;
2241 break;
2242
2243 default:
2244 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2245 }
2246
2247 /*
2248 * Handle option arguments with argument.
2249 */
2250 if (want_argument)
2251 {
2252 /*
2253 * Check for garbage immediately after the option letter.
2254 */
2255 if (argv[0][argv_idx] != NUL)
2256 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2257
2258 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002259 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002260 mainerr_arg_missing((char_u *)argv[0]);
2261 ++argv;
2262 argv_idx = -1;
2263
2264 switch (c)
2265 {
2266 case 'c': /* "-c {command}" execute command */
2267 case 'S': /* "-S {file}" execute Vim script */
2268 if (parmp->n_commands >= MAX_ARG_CMDS)
2269 mainerr(ME_EXTRA_CMD, NULL);
2270 if (c == 'S')
2271 {
2272 char *a;
2273
2274 if (argc < 1)
2275 /* "-S" without argument: use default session file
2276 * name. */
2277 a = SESSION_FILE;
2278 else if (argv[0][0] == '-')
2279 {
2280 /* "-S" followed by another option: use default
2281 * session file name. */
2282 a = SESSION_FILE;
2283 ++argc;
2284 --argv;
2285 }
2286 else
2287 a = argv[0];
2288 p = alloc((unsigned)(STRLEN(a) + 4));
2289 if (p == NULL)
2290 mch_exit(2);
2291 sprintf((char *)p, "so %s", a);
2292 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2293 parmp->commands[parmp->n_commands++] = p;
2294 }
2295 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002296 parmp->commands[parmp->n_commands++] =
2297 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002298 break;
2299
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002300 case '-':
2301 if (argv[-1][2] == 'c')
2302 {
2303 /* "--cmd {command}" execute command */
2304 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2305 mainerr(ME_EXTRA_CMD, NULL);
2306 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002307 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002308 }
2309 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002310 break;
2311
2312 /* case 'd': -d {device} is handled in mch_check_win() for the
2313 * Amiga */
2314
2315#ifdef FEAT_QUICKFIX
2316 case 'q': /* "-q {errorfile}" QuickFix mode */
2317 parmp->use_ef = (char_u *)argv[0];
2318 break;
2319#endif
2320
2321 case 'i': /* "-i {viminfo}" use for viminfo */
2322 use_viminfo = (char_u *)argv[0];
2323 break;
2324
2325 case 's': /* "-s {scriptin}" read from script file */
2326 if (scriptin[0] != NULL)
2327 {
2328scripterror:
2329 mch_errmsg(_("Attempt to open script file again: \""));
2330 mch_errmsg(argv[-1]);
2331 mch_errmsg(" ");
2332 mch_errmsg(argv[0]);
2333 mch_errmsg("\"\n");
2334 mch_exit(2);
2335 }
2336 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2337 {
2338 mch_errmsg(_("Cannot open for reading: \""));
2339 mch_errmsg(argv[0]);
2340 mch_errmsg("\"\n");
2341 mch_exit(2);
2342 }
2343 if (save_typebuf() == FAIL)
2344 mch_exit(2); /* out of memory */
2345 break;
2346
2347 case 't': /* "-t {tag}" */
2348 parmp->tagname = (char_u *)argv[0];
2349 break;
2350
2351 case 'T': /* "-T {terminal}" terminal name */
2352 /*
2353 * The -T term argument is always available and when
2354 * HAVE_TERMLIB is supported it overrides the environment
2355 * variable TERM.
2356 */
2357#ifdef FEAT_GUI
2358 if (term_is_gui((char_u *)argv[0]))
2359 gui.starting = TRUE; /* start GUI a bit later */
2360 else
2361#endif
2362 parmp->term = (char_u *)argv[0];
2363 break;
2364
2365 case 'u': /* "-u {vimrc}" vim inits file */
2366 parmp->use_vimrc = (char_u *)argv[0];
2367 break;
2368
2369 case 'U': /* "-U {gvimrc}" gvim inits file */
2370#ifdef FEAT_GUI
2371 use_gvimrc = (char_u *)argv[0];
2372#endif
2373 break;
2374
2375 case 'w': /* "-w {nr}" 'window' value */
2376 /* "-w {scriptout}" append to script file */
2377 if (vim_isdigit(*((char_u *)argv[0])))
2378 {
2379 argv_idx = 0;
2380 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2381 set_option_value((char_u *)"window", n, NULL, 0);
2382 argv_idx = -1;
2383 break;
2384 }
2385 /*FALLTHROUGH*/
2386 case 'W': /* "-W {scriptout}" overwrite script file */
2387 if (scriptout != NULL)
2388 goto scripterror;
2389 if ((scriptout = mch_fopen(argv[0],
2390 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2391 {
2392 mch_errmsg(_("Cannot open for script output: \""));
2393 mch_errmsg(argv[0]);
2394 mch_errmsg("\"\n");
2395 mch_exit(2);
2396 }
2397 break;
2398
2399#ifdef FEAT_GUI_W32
2400 case 'P': /* "-P {parent title}" MDI parent */
2401 gui_mch_set_parent(argv[0]);
2402 break;
2403#endif
2404 }
2405 }
2406 }
2407
2408 /*
2409 * File name argument.
2410 */
2411 else
2412 {
2413 argv_idx = -1; /* skip to next argument */
2414
2415 /* Check for only one type of editing. */
2416 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2417 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2418 parmp->edit_type = EDIT_FILE;
2419
2420#ifdef MSWIN
2421 /* Remember if the argument was a full path before changing
2422 * slashes to backslashes. */
2423 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2424 parmp->full_path = TRUE;
2425#endif
2426
2427 /* Add the file to the global argument list. */
2428 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2429 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2430 mch_exit(2);
2431#ifdef FEAT_DIFF
2432 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2433 && !mch_isdir(alist_name(&GARGLIST[0])))
2434 {
2435 char_u *r;
2436
2437 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2438 if (r != NULL)
2439 {
2440 vim_free(p);
2441 p = r;
2442 }
2443 }
2444#endif
2445#if defined(__CYGWIN32__) && !defined(WIN32)
2446 /*
2447 * If vim is invoked by non-Cygwin tools, convert away any
2448 * DOS paths, so things like .swp files are created correctly.
2449 * Look for evidence of non-Cygwin paths before we bother.
2450 * This is only for when using the Unix files.
2451 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002452 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002453 {
2454 char posix_path[PATH_MAX];
2455
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002456# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2457 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2458# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002459 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002460# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002461 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002462 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002463 if (p == NULL)
2464 mch_exit(2);
2465 }
2466#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002467
2468#ifdef USE_FNAME_CASE
2469 /* Make the case of the file name match the actual file. */
2470 fname_case(p, 0);
2471#endif
2472
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002473 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002474#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002475 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002476#else
2477 2 /* add buffer number now and use curbuf */
2478#endif
2479 );
2480
2481#if defined(FEAT_MBYTE) && defined(WIN32)
2482 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002483 /* Remember this argument has been added to the argument list.
2484 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002485 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002486# ifdef FEAT_DIFF
2487 parmp->diff_mode
2488# else
2489 FALSE
2490# endif
2491 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002492 }
2493#endif
2494 }
2495
2496 /*
2497 * If there are no more letters after the current "-", go to next
2498 * argument. argv_idx is set to -1 when the current argument is to be
2499 * skipped.
2500 */
2501 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2502 {
2503 --argc;
2504 ++argv;
2505 argv_idx = 1;
2506 }
2507 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002508
2509#ifdef FEAT_EVAL
2510 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2511 * one. */
2512 if (parmp->n_commands > 0)
2513 {
2514 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2515 if (p != NULL)
2516 {
2517 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2518 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2519 vim_free(p);
2520 }
2521 }
2522#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002523}
2524
2525/*
2526 * Print a warning if stdout is not a terminal.
2527 * When starting in Ex mode and commands come from a file, set Silent mode.
2528 */
2529 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002530check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002531{
2532 int input_isatty; /* is active input a terminal? */
2533
2534 input_isatty = mch_input_isatty();
2535 if (exmode_active)
2536 {
2537 if (!input_isatty)
2538 silent_mode = TRUE;
2539 }
2540 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2541#ifdef FEAT_GUI
2542 /* don't want the delay when started from the desktop */
2543 && !gui.starting
2544#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002545 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002546 {
2547#ifdef NBDEBUG
2548 /*
2549 * This shouldn't be necessary. But if I run netbeans with the log
2550 * output coming to the console and XOpenDisplay fails, I get vim
2551 * trying to start with input/output to my console tty. This fills my
2552 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002553 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002554 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002555 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002556 {
2557 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2558 exit(1);
2559 }
2560#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002561#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2562 if (is_cygpty_used())
2563 {
2564 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2565 exit(1);
2566 }
2567#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002568 if (!parmp->stdout_isatty)
2569 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2570 if (!input_isatty)
2571 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2572 out_flush();
2573 if (scriptin[0] == NULL)
2574 ui_delay(2000L, TRUE);
2575 TIME_MSG("Warning delay");
2576 }
2577}
2578
2579/*
2580 * Read text from stdin.
2581 */
2582 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002583read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002584{
2585 int i;
2586
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002587#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002588 /* When getting the ATTENTION prompt here, use a dialog */
2589 swap_exists_action = SEA_DIALOG;
2590#endif
2591 no_wait_return = TRUE;
2592 i = msg_didany;
2593 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002594 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002595 no_wait_return = FALSE;
2596 msg_didany = i;
2597 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002598#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002599 check_swap_exists_action();
2600#endif
2601#if !(defined(AMIGA) || defined(MACOS))
2602 /*
2603 * Close stdin and dup it from stderr. Required for GPM to work
2604 * properly, and for running external commands.
2605 * Is there any other system that cannot do this?
2606 */
2607 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002608 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002609#endif
2610}
2611
2612/*
2613 * Create the requested number of windows and edit buffers in them.
2614 * Also does recovery if "recoverymode" set.
2615 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002616 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002617create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002618{
2619#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002620 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002621 int done = 0;
2622
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002623 /*
2624 * Create the number of windows that was requested.
2625 */
2626 if (parmp->window_count == -1) /* was not set */
2627 parmp->window_count = 1;
2628 if (parmp->window_count == 0)
2629 parmp->window_count = GARGCOUNT;
2630 if (parmp->window_count > 1)
2631 {
2632 /* Don't change the windows if there was a command in .vimrc that
2633 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002634 if (parmp->window_layout == 0)
2635 parmp->window_layout = WIN_HOR;
2636 if (parmp->window_layout == WIN_TABS)
2637 {
2638 parmp->window_count = make_tabpages(parmp->window_count);
2639 TIME_MSG("making tab pages");
2640 }
2641 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002642 {
2643 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002644 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002645 TIME_MSG("making windows");
2646 }
2647 else
2648 parmp->window_count = win_count();
2649 }
2650 else
2651 parmp->window_count = 1;
2652#endif
2653
2654 if (recoverymode) /* do recover */
2655 {
2656 msg_scroll = TRUE; /* scroll message up */
2657 ml_recover();
2658 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2659 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002660 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002661 }
2662 else
2663 {
2664 /*
2665 * Open a buffer for windows that don't have one yet.
2666 * Commands in the .vimrc might have loaded a file or split the window.
2667 * Watch out for autocommands that delete a window.
2668 */
2669#ifdef FEAT_AUTOCMD
2670 /*
2671 * Don't execute Win/Buf Enter/Leave autocommands here
2672 */
2673 ++autocmd_no_enter;
2674 ++autocmd_no_leave;
2675#endif
2676#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002677 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002678 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002679 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002680 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002681 {
2682 if (parmp->window_layout == WIN_TABS)
2683 goto_tabpage(1);
2684 else
2685 curwin = firstwin;
2686 }
2687 else if (parmp->window_layout == WIN_TABS)
2688 {
2689 if (curtab->tp_next == NULL)
2690 break;
2691 goto_tabpage(0);
2692 }
2693 else
2694 {
2695 if (curwin->w_next == NULL)
2696 break;
2697 curwin = curwin->w_next;
2698 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002699 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002700#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002701 curbuf = curwin->w_buffer;
2702 if (curbuf->b_ml.ml_mfp == NULL)
2703 {
2704#ifdef FEAT_FOLDING
2705 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2706 if (p_fdls >= 0)
2707 curwin->w_p_fdl = p_fdls;
2708#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002709#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002710 /* When getting the ATTENTION prompt here, use a dialog */
2711 swap_exists_action = SEA_DIALOG;
2712#endif
2713 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002714
2715 /* create memfile, read file */
2716 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002717
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002718#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002719 if (swap_exists_action == SEA_QUIT)
2720 {
2721 if (got_int || only_one_window())
2722 {
2723 /* abort selected or quit and only one window */
2724 did_emsg = FALSE; /* avoid hit-enter prompt */
2725 getout(1);
2726 }
2727 /* We can't close the window, it would disturb what
2728 * happens next. Clear the file name and set the arg
2729 * index to -1 to delete it later. */
2730 setfname(curbuf, NULL, NULL, FALSE);
2731 curwin->w_arg_idx = -1;
2732 swap_exists_action = SEA_NONE;
2733 }
2734 else
2735 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002736#endif
2737#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002738 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002739#endif
2740 }
2741#ifdef FEAT_WINDOWS
2742 ui_breakcheck();
2743 if (got_int)
2744 {
2745 (void)vgetc(); /* only break the file loading, not the rest */
2746 break;
2747 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002748 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002749#endif
2750#ifdef FEAT_WINDOWS
2751 if (parmp->window_layout == WIN_TABS)
2752 goto_tabpage(1);
2753 else
2754 curwin = firstwin;
2755 curbuf = curwin->w_buffer;
2756#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002757#ifdef FEAT_AUTOCMD
2758 --autocmd_no_enter;
2759 --autocmd_no_leave;
2760#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002761 }
2762}
2763
2764#ifdef FEAT_WINDOWS
2765 /*
2766 * If opened more than one window, start editing files in the other
2767 * windows. make_windows() has already opened the windows.
2768 */
2769 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002770edit_buffers(
2771 mparm_T *parmp,
2772 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002773{
2774 int arg_idx; /* index in argument list */
2775 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002776 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002777 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002778
2779# ifdef FEAT_AUTOCMD
2780 /*
2781 * Don't execute Win/Buf Enter/Leave autocommands here
2782 */
2783 ++autocmd_no_enter;
2784 ++autocmd_no_leave;
2785# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002786
2787 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2788 if (curwin->w_arg_idx == -1)
2789 {
2790 win_close(curwin, TRUE);
2791 advance = FALSE;
2792 }
2793
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002794 arg_idx = 1;
2795 for (i = 1; i < parmp->window_count; ++i)
2796 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002797 if (cwd != NULL)
2798 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002799 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2800 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002801 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002802 ++arg_idx;
2803 win_close(curwin, TRUE);
2804 advance = FALSE;
2805 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002806 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002807
Bram Moolenaar84212822006-11-07 21:59:47 +00002808 if (advance)
2809 {
2810 if (parmp->window_layout == WIN_TABS)
2811 {
2812 if (curtab->tp_next == NULL) /* just checking */
2813 break;
2814 goto_tabpage(0);
2815 }
2816 else
2817 {
2818 if (curwin->w_next == NULL) /* just checking */
2819 break;
2820 win_enter(curwin->w_next, FALSE);
2821 }
2822 }
2823 advance = TRUE;
2824
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002825 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002826 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002827 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2828 {
2829 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002830 /* Edit file from arg list, if there is one. When "Quit" selected
2831 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002832# ifdef HAS_SWAP_EXISTS_ACTION
2833 swap_exists_did_quit = FALSE;
2834# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002835 (void)do_ecmd(0, arg_idx < GARGCOUNT
2836 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002837 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002838# ifdef HAS_SWAP_EXISTS_ACTION
2839 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002840 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002841 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002842 if (got_int || only_one_window())
2843 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002844 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002845 did_emsg = FALSE; /* avoid hit-enter prompt */
2846 getout(1);
2847 }
2848 win_close(curwin, TRUE);
2849 advance = FALSE;
2850 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002851# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002852 if (arg_idx == GARGCOUNT - 1)
2853 arg_had_last = TRUE;
2854 ++arg_idx;
2855 }
2856 ui_breakcheck();
2857 if (got_int)
2858 {
2859 (void)vgetc(); /* only break the file loading, not the rest */
2860 break;
2861 }
2862 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002863
2864 if (parmp->window_layout == WIN_TABS)
2865 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002866# ifdef FEAT_AUTOCMD
2867 --autocmd_no_enter;
2868# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002869
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002870 /* make the first window the current window */
2871 win = firstwin;
2872#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2873 /* Avoid making a preview window the current window. */
2874 while (win->w_p_pvw)
2875 {
2876 win = win->w_next;
2877 if (win == NULL)
2878 {
2879 win = firstwin;
2880 break;
2881 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002882 }
2883#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002884 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002885
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002886# ifdef FEAT_AUTOCMD
2887 --autocmd_no_leave;
2888# endif
2889 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002890 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002891 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2892}
2893#endif /* FEAT_WINDOWS */
2894
2895/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002896 * Execute the commands from --cmd arguments "cmds[cnt]".
2897 */
2898 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002899exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002900{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002901 char_u **cmds = parmp->pre_commands;
2902 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002903 int i;
2904
2905 if (cnt > 0)
2906 {
2907 curwin->w_cursor.lnum = 0; /* just in case.. */
2908 sourcing_name = (char_u *)_("pre-vimrc command line");
2909# ifdef FEAT_EVAL
2910 current_SID = SID_CMDARG;
2911# endif
2912 for (i = 0; i < cnt; ++i)
2913 do_cmdline_cmd(cmds[i]);
2914 sourcing_name = NULL;
2915# ifdef FEAT_EVAL
2916 current_SID = 0;
2917# endif
2918 TIME_MSG("--cmd commands");
2919 }
2920}
2921
2922/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002923 * Execute "+", "-c" and "-S" arguments.
2924 */
2925 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002926exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002927{
2928 int i;
2929
2930 /*
2931 * We start commands on line 0, make "vim +/pat file" match a
2932 * pattern on line 1. But don't move the cursor when an autocommand
2933 * with g`" was used.
2934 */
2935 msg_scroll = TRUE;
2936 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2937 curwin->w_cursor.lnum = 0;
2938 sourcing_name = (char_u *)"command line";
2939#ifdef FEAT_EVAL
2940 current_SID = SID_CARG;
2941#endif
2942 for (i = 0; i < parmp->n_commands; ++i)
2943 {
2944 do_cmdline_cmd(parmp->commands[i]);
2945 if (parmp->cmds_tofree[i])
2946 vim_free(parmp->commands[i]);
2947 }
2948 sourcing_name = NULL;
2949#ifdef FEAT_EVAL
2950 current_SID = 0;
2951#endif
2952 if (curwin->w_cursor.lnum == 0)
2953 curwin->w_cursor.lnum = 1;
2954
2955 if (!exmode_active)
2956 msg_scroll = FALSE;
2957
2958#ifdef FEAT_QUICKFIX
2959 /* When started with "-q errorfile" jump to first error again. */
2960 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002961 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002962#endif
2963 TIME_MSG("executing command arguments");
2964}
2965
2966/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002967 * Source startup scripts.
2968 */
2969 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002970source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002971{
2972 int i;
2973
2974 /*
2975 * For "evim" source evim.vim first of all, so that the user can overrule
2976 * any things he doesn't like.
2977 */
2978 if (parmp->evim_mode)
2979 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002980 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002981 TIME_MSG("source evim file");
2982 }
2983
2984 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002985 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002986 * nothing else.
2987 */
2988 if (parmp->use_vimrc != NULL)
2989 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002990 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2991 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002992 {
2993#ifdef FEAT_GUI
2994 if (use_gvimrc == NULL) /* don't load gvimrc either */
2995 use_gvimrc = parmp->use_vimrc;
2996#endif
2997 if (parmp->use_vimrc[2] == 'N')
2998 p_lpl = FALSE; /* don't load plugins either */
2999 }
3000 else
3001 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003002 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003003 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
3004 }
3005 }
3006 else if (!silent_mode)
3007 {
3008#ifdef AMIGA
3009 struct Process *proc = (struct Process *)FindTask(0L);
3010 APTR save_winptr = proc->pr_WindowPtr;
3011
3012 /* Avoid a requester here for a volume that doesn't exist. */
3013 proc->pr_WindowPtr = (APTR)-1L;
3014#endif
3015
3016 /*
3017 * Get system wide defaults, if the file name is defined.
3018 */
3019#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003020 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003021#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003022#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003023 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003024#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003025
3026 /*
3027 * Try to read initialization commands from the following places:
3028 * - environment variable VIMINIT
3029 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3030 * - second user vimrc file ($VIM/.vimrc for Dos)
3031 * - environment variable EXINIT
3032 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3033 * - second user exrc file ($VIM/.exrc for Dos)
3034 * The first that exists is used, the rest is ignored.
3035 */
3036 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3037 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003038 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003039#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003040 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3041 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003042#endif
3043#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003044 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3045 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003046#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003047#ifdef USR_VIMRC_FILE4
3048 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3049 DOSO_VIMRC) == FAIL
3050#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003051 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003052 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003053 {
3054#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003055 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003056#endif
3057 }
3058 }
3059
3060 /*
3061 * Read initialization commands from ".vimrc" or ".exrc" in current
3062 * directory. This is only done if the 'exrc' option is set.
3063 * Because of security reasons we disallow shell and write commands
3064 * now, except for unix if the file is owned by the user or 'secure'
3065 * option has been reset in environment of global ".exrc" or ".vimrc".
3066 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3067 * SYS_VIMRC_FILE.
3068 */
3069 if (p_exrc)
3070 {
3071#if defined(UNIX) || defined(VMS)
3072 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3073 if (!file_owned(VIMRC_FILE))
3074#endif
3075 secure = p_secure;
3076
3077 i = FAIL;
3078 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3079 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3080#ifdef USR_VIMRC_FILE2
3081 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3082 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3083#endif
3084#ifdef USR_VIMRC_FILE3
3085 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3086 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3087#endif
3088#ifdef SYS_VIMRC_FILE
3089 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3090 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3091#endif
3092 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003093 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003094
3095 if (i == FAIL)
3096 {
3097#if defined(UNIX) || defined(VMS)
3098 /* if ".exrc" is not owned by user set 'secure' mode */
3099 if (!file_owned(EXRC_FILE))
3100 secure = p_secure;
3101 else
3102 secure = 0;
3103#endif
3104 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3105 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3106#ifdef USR_EXRC_FILE2
3107 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3108 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3109#endif
3110 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003111 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003112 }
3113 }
3114 if (secure == 2)
3115 need_wait_return = TRUE;
3116 secure = 0;
3117#ifdef AMIGA
3118 proc->pr_WindowPtr = save_winptr;
3119#endif
3120 }
3121 TIME_MSG("sourcing vimrc file(s)");
3122}
3123
3124/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003125 * Setup to start using the GUI. Exit with an error when not available.
3126 */
3127 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003128main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003129{
3130#ifdef FEAT_GUI
3131 gui.starting = TRUE; /* start GUI a bit later */
3132#else
3133 mch_errmsg(_(e_nogvim));
3134 mch_errmsg("\n");
3135 mch_exit(2);
3136#endif
3137}
3138
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003139#endif /* NO_VIM_MAIN */
3140
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003141/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003142 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003143 * Returns FAIL if the environment variable was not executed, OK otherwise.
3144 */
3145 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003146process_env(
3147 char_u *env,
3148 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003149{
3150 char_u *initstr;
3151 char_u *save_sourcing_name;
3152 linenr_T save_sourcing_lnum;
3153#ifdef FEAT_EVAL
3154 scid_T save_sid;
3155#endif
3156
3157 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3158 {
3159 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003160 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003161 save_sourcing_name = sourcing_name;
3162 save_sourcing_lnum = sourcing_lnum;
3163 sourcing_name = env;
3164 sourcing_lnum = 0;
3165#ifdef FEAT_EVAL
3166 save_sid = current_SID;
3167 current_SID = SID_ENV;
3168#endif
3169 do_cmdline_cmd(initstr);
3170 sourcing_name = save_sourcing_name;
3171 sourcing_lnum = save_sourcing_lnum;
3172#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003173 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003174#endif
3175 return OK;
3176 }
3177 return FAIL;
3178}
3179
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003180#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003181/*
3182 * Return TRUE if we are certain the user owns the file "fname".
3183 * Used for ".vimrc" and ".exrc".
3184 * Use both stat() and lstat() for extra security.
3185 */
3186 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003187file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003188{
3189 struct stat s;
3190# ifdef UNIX
3191 uid_t uid = getuid();
3192# else /* VMS */
3193 uid_t uid = ((getgid() << 16) | getuid());
3194# endif
3195
3196 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3197# ifdef HAVE_LSTAT
3198 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3199# endif
3200 );
3201}
3202#endif
3203
3204/*
3205 * Give an error message main_errors["n"] and exit.
3206 */
3207 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003208mainerr(
3209 int n, /* one of the ME_ defines */
3210 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003211{
3212#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3213 reset_signals(); /* kill us with CTRL-C here, if you like */
3214#endif
3215
3216 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003217 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003218 mch_errmsg(_(main_errors[n]));
3219 if (str != NULL)
3220 {
3221 mch_errmsg(": \"");
3222 mch_errmsg((char *)str);
3223 mch_errmsg("\"");
3224 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003225 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003226
3227 mch_exit(1);
3228}
3229
3230 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003231mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003232{
3233 mainerr(ME_ARG_MISSING, str);
3234}
3235
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003236#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003237/*
3238 * print a message with three spaces prepended and '\n' appended.
3239 */
3240 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003241main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003242{
3243 mch_msg(" ");
3244 mch_msg(s);
3245 mch_msg("\n");
3246}
3247
3248/*
3249 * Print messages for "vim -h" or "vim --help" and exit.
3250 */
3251 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003252usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003253{
3254 int i;
3255 static char *(use[]) =
3256 {
3257 N_("[file ..] edit specified file(s)"),
3258 N_("- read text from stdin"),
3259 N_("-t tag edit file where tag is defined"),
3260#ifdef FEAT_QUICKFIX
3261 N_("-q [errorfile] edit file with first error")
3262#endif
3263 };
3264
3265#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3266 reset_signals(); /* kill us with CTRL-C here, if you like */
3267#endif
3268
3269 mch_msg(longVersion);
3270 mch_msg(_("\n\nusage:"));
3271 for (i = 0; ; ++i)
3272 {
3273 mch_msg(_(" vim [arguments] "));
3274 mch_msg(_(use[i]));
3275 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3276 break;
3277 mch_msg(_("\n or:"));
3278 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003279#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003280 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003281#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003282
3283 mch_msg(_("\n\nArguments:\n"));
3284 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003285#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003286 main_msg(_("--literal\t\tDon't expand wildcards"));
3287#endif
3288#ifdef FEAT_OLE
3289 main_msg(_("-register\t\tRegister this gvim for OLE"));
3290 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3291#endif
3292#ifdef FEAT_GUI
3293 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3294 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3295#endif
3296 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3297 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003298 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003299 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3300#ifdef FEAT_DIFF
3301 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3302#endif
3303 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3304 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3305 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3306 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3307 main_msg(_("-M\t\t\tModifications in text not allowed"));
3308 main_msg(_("-b\t\t\tBinary mode"));
3309#ifdef FEAT_LISP
3310 main_msg(_("-l\t\t\tLisp mode"));
3311#endif
3312 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3313 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003314 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3315#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003316 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003317#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003318 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3319 main_msg(_("-r\t\t\tList swap files and exit"));
3320 main_msg(_("-r (with file name)\tRecover crashed session"));
3321 main_msg(_("-L\t\t\tSame as -r"));
3322#ifdef AMIGA
3323 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3324 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3325#endif
3326#ifdef FEAT_ARABIC
3327 main_msg(_("-A\t\t\tstart in Arabic mode"));
3328#endif
3329#ifdef FEAT_RIGHTLEFT
3330 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3331#endif
3332#ifdef FEAT_FKMAP
3333 main_msg(_("-F\t\t\tStart in Farsi mode"));
3334#endif
3335 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003336 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003337 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3338#ifdef FEAT_GUI
3339 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3340#endif
3341 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003342#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003343 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003344 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3345 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003346#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003347 main_msg(_("+\t\t\tStart at end of file"));
3348 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003349 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003350 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3351 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3352 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3353 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3354 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3355#ifdef FEAT_CRYPT
3356 main_msg(_("-x\t\t\tEdit encrypted files"));
3357#endif
3358#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3359# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3360 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3361# endif
3362 main_msg(_("-X\t\t\tDo not connect to X server"));
3363#endif
3364#ifdef FEAT_CLIENTSERVER
3365 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3366 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3367 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3368 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003369# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003370 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003371# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003372 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3373 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3374 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3375 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3376#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003377#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003378 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003379#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003380#ifdef FEAT_VIMINFO
3381 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3382#endif
3383 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3384 main_msg(_("--version\t\tPrint version information and exit"));
3385
3386#ifdef FEAT_GUI_X11
3387# ifdef FEAT_GUI_MOTIF
3388 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3389# else
3390# ifdef FEAT_GUI_ATHENA
3391# ifdef FEAT_GUI_NEXTAW
3392 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3393# else
3394 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3395# endif
3396# endif
3397# endif
3398 main_msg(_("-display <display>\tRun vim on <display>"));
3399 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003400 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3401 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3402 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3403 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3404 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3405 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3406 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3407 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3408# ifdef FEAT_GUI_ATHENA
3409 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3410# endif
3411 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3412 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3413 main_msg(_("-xrm <resource>\tSet the specified resource"));
3414#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003415#ifdef FEAT_GUI_GTK
3416 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3417 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3418 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3419 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3420 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003421 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003422 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003423 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003424#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003425#ifdef FEAT_GUI_W32
3426 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003427 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003428#endif
3429
3430#ifdef FEAT_GUI_GNOME
3431 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3432 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003433 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003434 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003435 gui.dofork = FALSE;
3436 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003437 else
3438#endif
3439 mch_exit(0);
3440}
3441
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003442#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003443/*
3444 * Check the result of the ATTENTION dialog:
3445 * When "Quit" selected, exit Vim.
3446 * When "Recover" selected, recover the file.
3447 */
3448 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003449check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003450{
3451 if (swap_exists_action == SEA_QUIT)
3452 getout(1);
3453 handle_swap_exists(NULL);
3454}
3455#endif
3456
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003457#endif
3458
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003459#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003460static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003461
3462static struct timeval prev_timeval;
3463
Bram Moolenaar3f269672009-11-03 11:11:11 +00003464# ifdef WIN3264
3465/*
3466 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3467 */
3468 static int
3469gettimeofday(struct timeval *tv, char *dummy)
3470{
3471 long t = clock();
3472 tv->tv_sec = t / CLOCKS_PER_SEC;
3473 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3474 return 0;
3475}
3476# endif
3477
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003478/*
3479 * Save the previous time before doing something that could nest.
3480 * set "*tv_rel" to the time elapsed so far.
3481 */
3482 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003483time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003484{
3485 *((struct timeval *)tv_rel) = prev_timeval;
3486 gettimeofday(&prev_timeval, NULL);
3487 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3488 - ((struct timeval *)tv_rel)->tv_usec;
3489 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3490 - ((struct timeval *)tv_rel)->tv_sec;
3491 if (((struct timeval *)tv_rel)->tv_usec < 0)
3492 {
3493 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3494 --((struct timeval *)tv_rel)->tv_sec;
3495 }
3496 *(struct timeval *)tv_start = prev_timeval;
3497}
3498
3499/*
3500 * Compute the previous time after doing something that could nest.
3501 * Subtract "*tp" from prev_timeval;
3502 * Note: The arguments are (void *) to avoid trouble with systems that don't
3503 * have struct timeval.
3504 */
3505 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003506time_pop(
3507 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003508{
3509 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3510 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3511 if (prev_timeval.tv_usec < 0)
3512 {
3513 prev_timeval.tv_usec += 1000000;
3514 --prev_timeval.tv_sec;
3515 }
3516}
3517
3518 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003519time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003520{
3521 long usec;
3522 long msec;
3523
3524 usec = now->tv_usec - then->tv_usec;
3525 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3526 usec = usec % 1000L;
3527 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3528}
3529
3530 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003531time_msg(
3532 char *mesg,
3533 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003534 (struct timeval *) */
3535{
3536 static struct timeval start;
3537 struct timeval now;
3538
3539 if (time_fd != NULL)
3540 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003541 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003542 {
3543 gettimeofday(&start, NULL);
3544 prev_timeval = start;
3545 fprintf(time_fd, "\n\ntimes in msec\n");
3546 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3547 fprintf(time_fd, " clock elapsed: other lines\n\n");
3548 }
3549 gettimeofday(&now, NULL);
3550 time_diff(&start, &now);
3551 if (((struct timeval *)tv_start) != NULL)
3552 {
3553 fprintf(time_fd, " ");
3554 time_diff(((struct timeval *)tv_start), &now);
3555 }
3556 fprintf(time_fd, " ");
3557 time_diff(&prev_timeval, &now);
3558 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003559 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003560 }
3561}
3562
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003563#endif
3564
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003565#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003566
3567/*
3568 * Common code for the X command server and the Win32 command server.
3569 */
3570
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003571static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003572
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003573/*
3574 * Do the client-server stuff, unless "--servername ''" was used.
3575 */
3576 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003577exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003578{
3579 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3580 {
3581# ifdef WIN32
3582 /* Initialise the client/server messaging infrastructure. */
3583 serverInitMessaging();
3584# endif
3585
3586 /*
3587 * When a command server argument was found, execute it. This may
3588 * exit Vim when it was successful. Otherwise it's executed further
3589 * on. Remember the encoding used here in "serverStrEnc".
3590 */
3591 if (parmp->serverArg)
3592 {
3593 cmdsrv_main(&parmp->argc, parmp->argv,
3594 parmp->serverName_arg, &parmp->serverStr);
3595# ifdef FEAT_MBYTE
3596 parmp->serverStrEnc = vim_strsave(p_enc);
3597# endif
3598 }
3599
3600 /* If we're still running, get the name to register ourselves.
3601 * On Win32 can register right now, for X11 need to setup the
3602 * clipboard first, it's further down. */
3603 parmp->servername = serverMakeName(parmp->serverName_arg,
3604 parmp->argv[0]);
3605# ifdef WIN32
3606 if (parmp->servername != NULL)
3607 {
3608 serverSetName(parmp->servername);
3609 vim_free(parmp->servername);
3610 }
3611# endif
3612 }
3613}
3614
3615/*
3616 * Prepare for running as a Vim server.
3617 */
3618 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003619prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003620{
3621# if defined(FEAT_X11)
3622 /*
3623 * Register for remote command execution with :serversend and --remote
3624 * unless there was a -X or a --servername '' on the command line.
3625 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003626 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003627 */
3628 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3629# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003630 (gui.in_use
3631# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003632 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003633# endif
3634 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003635# endif
3636 parmp->serverName_arg != NULL))
3637 {
3638 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3639 vim_free(parmp->servername);
3640 TIME_MSG("register server name");
3641 }
3642 else
3643 serverDelayedStartName = parmp->servername;
3644# endif
3645
3646 /*
3647 * Execute command ourselves if we're here because the send failed (or
3648 * else we would have exited above).
3649 */
3650 if (parmp->serverStr != NULL)
3651 {
3652 char_u *p;
3653
3654 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3655 parmp->serverStr, &p));
3656 vim_free(p);
3657 }
3658}
3659
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003660 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003661cmdsrv_main(
3662 int *argc,
3663 char **argv,
3664 char_u *serverName_arg,
3665 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003666{
3667 char_u *res;
3668 int i;
3669 char_u *sname;
3670 int ret;
3671 int didone = FALSE;
3672 int exiterr = 0;
3673 char **newArgV = argv + 1;
3674 int newArgC = 1,
3675 Argc = *argc;
3676 int argtype;
3677#define ARGTYPE_OTHER 0
3678#define ARGTYPE_EDIT 1
3679#define ARGTYPE_EDIT_WAIT 2
3680#define ARGTYPE_SEND 3
3681 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003682 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003683# ifndef FEAT_X11
3684 HWND srv;
3685# else
3686 Window srv;
3687
3688 setup_term_clip();
3689# endif
3690
3691 sname = serverMakeName(serverName_arg, argv[0]);
3692 if (sname == NULL)
3693 return;
3694
3695 /*
3696 * Execute the command server related arguments and remove them
3697 * from the argc/argv array; We may have to return into main()
3698 */
3699 for (i = 1; i < Argc; i++)
3700 {
3701 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003702 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003703 {
3704 for (; i < *argc; i++)
3705 {
3706 *newArgV++ = argv[i];
3707 newArgC++;
3708 }
3709 break;
3710 }
3711
Bram Moolenaareb94e552006-03-11 21:35:11 +00003712 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003713 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003714 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3715 {
3716 char *p = argv[i] + 8;
3717
3718 argtype = ARGTYPE_EDIT;
3719 while (*p != NUL)
3720 {
3721 if (STRNICMP(p, "-wait", 5) == 0)
3722 {
3723 argtype = ARGTYPE_EDIT_WAIT;
3724 p += 5;
3725 }
3726 else if (STRNICMP(p, "-silent", 7) == 0)
3727 {
3728 silent = TRUE;
3729 p += 7;
3730 }
3731 else if (STRNICMP(p, "-tab", 4) == 0)
3732 {
3733 tabs = TRUE;
3734 p += 4;
3735 }
3736 else
3737 {
3738 argtype = ARGTYPE_OTHER;
3739 break;
3740 }
3741 }
3742 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003743 else
3744 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003745
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003746 if (argtype != ARGTYPE_OTHER)
3747 {
3748 if (i == *argc - 1)
3749 mainerr_arg_missing((char_u *)argv[i]);
3750 if (argtype == ARGTYPE_SEND)
3751 {
3752 *serverStr = (char_u *)argv[i + 1];
3753 i++;
3754 }
3755 else
3756 {
3757 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003758 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003759 if (*serverStr == NULL)
3760 {
3761 /* Probably out of memory, exit. */
3762 didone = TRUE;
3763 exiterr = 1;
3764 break;
3765 }
3766 Argc = i;
3767 }
3768# ifdef FEAT_X11
3769 if (xterm_dpy == NULL)
3770 {
3771 mch_errmsg(_("No display"));
3772 ret = -1;
3773 }
3774 else
3775 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3776 NULL, &srv, 0, 0, silent);
3777# else
3778 /* Win32 always works? */
3779 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3780# endif
3781 if (ret < 0)
3782 {
3783 if (argtype == ARGTYPE_SEND)
3784 {
3785 /* Failed to send, abort. */
3786 mch_errmsg(_(": Send failed.\n"));
3787 didone = TRUE;
3788 exiterr = 1;
3789 }
3790 else if (!silent)
3791 /* Let vim start normally. */
3792 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3793 break;
3794 }
3795
3796# ifdef FEAT_GUI_W32
3797 /* Guess that when the server name starts with "g" it's a GUI
3798 * server, which we can bring to the foreground here.
3799 * Foreground() in the server doesn't work very well. */
3800 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3801 SetForegroundWindow(srv);
3802# endif
3803
3804 /*
3805 * For --remote-wait: Wait until the server did edit each
3806 * file. Also detect that the server no longer runs.
3807 */
3808 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3809 {
3810 int numFiles = *argc - i - 1;
3811 int j;
3812 char_u *done = alloc(numFiles);
3813 char_u *p;
3814# ifdef FEAT_GUI_W32
3815 NOTIFYICONDATA ni;
3816 int count = 0;
3817 extern HWND message_window;
3818# endif
3819
3820 if (numFiles > 0 && argv[i + 1][0] == '+')
3821 /* Skip "+cmd" argument, don't wait for it to be edited. */
3822 --numFiles;
3823
3824# ifdef FEAT_GUI_W32
3825 ni.cbSize = sizeof(ni);
3826 ni.hWnd = message_window;
3827 ni.uID = 0;
3828 ni.uFlags = NIF_ICON|NIF_TIP;
3829 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3830 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3831 Shell_NotifyIcon(NIM_ADD, &ni);
3832# endif
3833
3834 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003835 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003836 while (memchr(done, 0, numFiles) != NULL)
3837 {
3838# ifdef WIN32
3839 p = serverGetReply(srv, NULL, TRUE, TRUE);
3840 if (p == NULL)
3841 break;
3842# else
3843 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3844 break;
3845# endif
3846 j = atoi((char *)p);
3847 if (j >= 0 && j < numFiles)
3848 {
3849# ifdef FEAT_GUI_W32
3850 ++count;
3851 sprintf(ni.szTip, _("%d of %d edited"),
3852 count, numFiles);
3853 Shell_NotifyIcon(NIM_MODIFY, &ni);
3854# endif
3855 done[j] = 1;
3856 }
3857 }
3858# ifdef FEAT_GUI_W32
3859 Shell_NotifyIcon(NIM_DELETE, &ni);
3860# endif
3861 }
3862 }
3863 else if (STRICMP(argv[i], "--remote-expr") == 0)
3864 {
3865 if (i == *argc - 1)
3866 mainerr_arg_missing((char_u *)argv[i]);
3867# ifdef WIN32
3868 /* Win32 always works? */
3869 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3870 &res, NULL, 1, FALSE) < 0)
3871# else
3872 if (xterm_dpy == NULL)
3873 mch_errmsg(_("No display: Send expression failed.\n"));
3874 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3875 &res, NULL, 1, 1, FALSE) < 0)
3876# endif
3877 {
3878 if (res != NULL && *res != NUL)
3879 {
3880 /* Output error from remote */
3881 mch_errmsg((char *)res);
3882 vim_free(res);
3883 res = NULL;
3884 }
3885 mch_errmsg(_(": Send expression failed.\n"));
3886 }
3887 }
3888 else if (STRICMP(argv[i], "--serverlist") == 0)
3889 {
3890# ifdef WIN32
3891 /* Win32 always works? */
3892 res = serverGetVimNames();
3893# else
3894 if (xterm_dpy != NULL)
3895 res = serverGetVimNames(xterm_dpy);
3896# endif
3897 if (called_emsg)
3898 mch_errmsg("\n");
3899 }
3900 else if (STRICMP(argv[i], "--servername") == 0)
3901 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003902 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003903 i++;
3904 continue;
3905 }
3906 else
3907 {
3908 *newArgV++ = argv[i];
3909 newArgC++;
3910 continue;
3911 }
3912 didone = TRUE;
3913 if (res != NULL && *res != NUL)
3914 {
3915 mch_msg((char *)res);
3916 if (res[STRLEN(res) - 1] != '\n')
3917 mch_msg("\n");
3918 }
3919 vim_free(res);
3920 }
3921
3922 if (didone)
3923 {
3924 display_errors(); /* display any collected messages */
3925 exit(exiterr); /* Mission accomplished - get out */
3926 }
3927
3928 /* Return back into main() */
3929 *argc = newArgC;
3930 vim_free(sname);
3931}
3932
3933/*
3934 * Build a ":drop" command to send to a Vim server.
3935 */
3936 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003937build_drop_cmd(
3938 int filec,
3939 char **filev,
3940 int tabs, /* Use ":tab drop" instead of ":drop". */
3941 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003942{
3943 garray_T ga;
3944 int i;
3945 char_u *inicmd = NULL;
3946 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003947 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003948 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003949
3950 if (filec > 0 && filev[0][0] == '+')
3951 {
3952 inicmd = (char_u *)filev[0] + 1;
3953 filev++;
3954 filec--;
3955 }
3956 /* Check if we have at least one argument. */
3957 if (filec <= 0)
3958 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003959
3960 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003961 cwd = alloc(MAXPATHL);
3962 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003963 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003964 if (mch_dirname(cwd, MAXPATHL) != OK)
3965 {
3966 vim_free(cwd);
3967 return NULL;
3968 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003969 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003970#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003971 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00003972#else
3973 PATH_ESC_CHARS,
3974#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003975 '\\', TRUE);
3976 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003977 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003978 return NULL;
3979 ga_init2(&ga, 1, 100);
3980 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003981 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003982
3983 /* Call inputsave() so that a prompt for an encryption key works. */
3984 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3985 if (tabs)
3986 ga_concat(&ga, (char_u *)"tab ");
3987 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003988 for (i = 0; i < filec; i++)
3989 {
3990 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003991 * do it again in the Vim server. On MS-Windows only escape
3992 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003993 p = vim_strsave_escaped((char_u *)filev[i],
3994#ifdef UNIX
3995 PATH_ESC_CHARS
3996#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003997 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003998#endif
3999 );
4000 if (p == NULL)
4001 {
4002 vim_free(ga.ga_data);
4003 return NULL;
4004 }
4005 ga_concat(&ga, (char_u *)" ");
4006 ga_concat(&ga, p);
4007 vim_free(p);
4008 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004009 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4010
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004011 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4012 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004013 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4014
4015 /* Switch back to the correct current directory (prior to temporary path
4016 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004017 * correct after the :drop command. With line breaks and spaces:
4018 * if !exists('+acd') || !&acd
4019 * if haslocaldir()
4020 * cd -
4021 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004022 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004023 * cd -
4024 * endif
4025 * endif
4026 */
4027 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004028 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004029 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004030 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004031 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004032
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004033 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004034 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4035 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004036 if (inicmd != NULL)
4037 {
4038 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4039 * the following commands to be inserted as text. Use a "|",
4040 * hopefully "inicmd" does allow this... */
4041 ga_concat(&ga, inicmd);
4042 ga_concat(&ga, (char_u *)"|");
4043 }
4044 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4045 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004046 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004047 ga_append(&ga, NUL);
4048 return ga.ga_data;
4049}
4050
4051/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004052 * Make our basic server name: use the specified "arg" if given, otherwise use
4053 * the tail of the command "cmd" we were started with.
4054 * Return the name in allocated memory. This doesn't include a serial number.
4055 */
4056 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004057serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004058{
4059 char_u *p;
4060
4061 if (arg != NULL && *arg != NUL)
4062 p = vim_strsave_up(arg);
4063 else
4064 {
4065 p = vim_strsave_up(gettail((char_u *)cmd));
4066 /* Remove .exe or .bat from the name. */
4067 if (p != NULL && vim_strchr(p, '.') != NULL)
4068 *vim_strchr(p, '.') = NUL;
4069 }
4070 return p;
4071}
4072#endif /* FEAT_CLIENTSERVER */
4073
4074#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4075/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004076 * Replace termcodes such as <CR> and insert as key presses if there is room.
4077 */
4078 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004079server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004080{
4081 char_u *ptr = NULL;
4082 char_u *cpo_save = p_cpo;
4083
4084 /* Set 'cpoptions' the way we want it.
4085 * B set - backslashes are *not* treated specially
4086 * k set - keycodes are *not* reverse-engineered
4087 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004088 * The last but one parameter of replace_termcodes() is TRUE so that the
4089 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004090 */
4091 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004092 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004093 p_cpo = cpo_save;
4094
4095 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4096 {
4097 /*
4098 * Add the string to the input stream.
4099 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4100 *
4101 * First clear typed characters from the typeahead buffer, there could
4102 * be half a mapping there. Then append to the existing string, so
4103 * that multiple commands from a client are concatenated.
4104 */
4105 if (typebuf.tb_maplen < typebuf.tb_len)
4106 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4107 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4108
4109 /* Let input_available() know we inserted text in the typeahead
4110 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004111 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004112 }
4113 vim_free((char_u *)ptr);
4114}
4115
4116/*
4117 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004118 */
4119 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004120eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004121{
4122 char_u *res;
4123 int save_dbl = debug_break_level;
4124 int save_ro = redir_off;
4125
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004126 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4127 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004128 debug_break_level = -1;
4129 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004130 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4131 * to be typed. Do generate errors so that try/catch works. */
4132 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004133
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004134 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004135
4136 debug_break_level = save_dbl;
4137 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004138 --emsg_silent;
4139 if (emsg_silent < 0)
4140 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004141
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004142 /* A client can tell us to redraw, but not to display the cursor, so do
4143 * that here. */
4144 setcursor();
4145 out_flush();
4146#ifdef FEAT_GUI
4147 if (gui.in_use)
4148 gui_update_cursor(FALSE, FALSE);
4149#endif
4150
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004151 return res;
4152}
4153
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004154/*
4155 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4156 * return an allocated string. Otherwise return "data".
4157 * "*tofree" is set to the result when it needs to be freed later.
4158 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004159 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004160serverConvert(
4161 char_u *client_enc UNUSED,
4162 char_u *data,
4163 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004164{
4165 char_u *res = data;
4166
4167 *tofree = NULL;
4168# ifdef FEAT_MBYTE
4169 if (client_enc != NULL && p_enc != NULL)
4170 {
4171 vimconv_T vimconv;
4172
4173 vimconv.vc_type = CONV_NONE;
4174 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4175 && vimconv.vc_type != CONV_NONE)
4176 {
4177 res = string_convert(&vimconv, data, NULL);
4178 if (res == NULL)
4179 res = data;
4180 else
4181 *tofree = res;
4182 }
4183 convert_setup(&vimconv, NULL, NULL);
4184 }
4185# endif
4186 return res;
4187}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004188#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004189
4190/*
4191 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4192 */
4193#if defined(FEAT_FKMAP) || defined(PROTO)
4194# include "farsi.c"
4195#endif
4196
4197/*
4198 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4199 */
4200#if defined(FEAT_ARABIC) || defined(PROTO)
4201# include "arabic.c"
4202#endif