blob: 57aeeed5f05430e3380e49eb51558a77550f8da4 [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
13#ifdef SPAWNO
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +000014# include <spawno.h> /* special MS-DOS swapping library */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000015#endif
16
Bram Moolenaarb4210b32004-06-13 14:51:16 +000017#ifdef __CYGWIN__
18# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000019# include <cygwin/version.h>
20# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
21 * cygwin_conv_path() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000022# endif
23# include <limits.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? */
57 char_u *term; /* specified terminal name */
58#ifdef FEAT_CRYPT
59 int ask_for_key; /* -x argument */
60#endif
61 int no_swap_file; /* "-n" argument used */
62#ifdef FEAT_EVAL
63 int use_debug_break_level;
64#endif
65#ifdef FEAT_WINDOWS
66 int window_count; /* number of windows to use */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000067 int window_layout; /* 0, WIN_HOR, WIN_VER or WIN_TABS */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000068#endif
69
70#ifdef FEAT_CLIENTSERVER
Bram Moolenaar58d98232005-07-23 22:25:46 +000071 int serverArg; /* TRUE when argument for a server */
72 char_u *serverName_arg; /* cmdline arg for server name */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000073 char_u *serverStr; /* remote server command */
74 char_u *serverStrEnc; /* encoding of serverStr */
75 char_u *servername; /* allocated name for our server */
76#endif
Bram Moolenaar53076832015-12-31 19:53:21 +010077#if !defined(UNIX) && !defined(__EMX__)
78# define EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +000079 int literal; /* don't expand file names */
80#endif
81#ifdef MSWIN
82 int full_path; /* file name argument was full path */
83#endif
84#ifdef FEAT_DIFF
85 int diff_mode; /* start with 'diff' set */
86#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +000087} mparm_T;
88
Bram Moolenaarc013cb62005-07-24 21:18:31 +000089/* Values for edit_type. */
90#define EDIT_NONE 0 /* no edit type yet */
91#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
92#define EDIT_STDIN 2 /* read file from stdin */
93#define EDIT_TAG 3 /* tag name argument given, use tagname */
94#define EDIT_QF 4 /* start in quickfix mode */
95
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010096#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010097static int file_owned(char *fname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +000098#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010099static void mainerr(int, char_u *);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100100#ifndef NO_VIM_MAIN
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100101static void main_msg(char *s);
102static void usage(void);
103static int get_number_arg(char_u *p, int *idx, int def);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100104# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100105static void init_locale(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100106# endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100107static void parse_command_name(mparm_T *parmp);
108static void early_arg_scan(mparm_T *parmp);
109static void command_line_scan(mparm_T *parmp);
110static void check_tty(mparm_T *parmp);
111static void read_stdin(void);
112static void create_windows(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100113# ifdef FEAT_WINDOWS
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100114static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100115# endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100116static void exe_pre_commands(mparm_T *parmp);
117static void exe_commands(mparm_T *parmp);
118static void source_startup_scripts(mparm_T *parmp);
119static void main_start_gui(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100120# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100121static void check_swap_exists_action(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100122# endif
123# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100124static void exec_on_server(mparm_T *parmp);
125static void prepare_server(mparm_T *parmp);
126static void cmdsrv_main(int *argc, char **argv, char_u *serverName_arg, char_u **serverStr);
127static char_u *serverMakeName(char_u *arg, char *cmd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100128# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000129#endif
130
131
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000132/*
133 * Different types of error messages.
134 */
135static char *(main_errors[]) =
136{
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000137 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000138#define ME_UNKNOWN_OPTION 0
139 N_("Too many edit arguments"),
140#define ME_TOO_MANY_ARGS 1
141 N_("Argument missing after"),
142#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000143 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000144#define ME_GARBAGE 3
145 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
146#define ME_EXTRA_CMD 4
147 N_("Invalid argument for"),
148#define ME_INVALID_ARG 5
149};
150
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100151#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100152#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +0200153
154static char_u *start_dir = NULL; /* current working dir on startup */
155
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000156 int
157# ifdef VIMDLL
158_export
159# endif
160# ifdef FEAT_GUI_MSWIN
161# ifdef __BORLANDC__
162_cdecl
163# endif
164VimMain
165# else
166main
167# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100168(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000169{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000170 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000171 mparm_T params; /* various parameters passed between
172 * main() and other functions. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000173#ifdef STARTUPTIME
174 int i;
175#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000176
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000177 /*
178 * Do any system-specific initialisations. These can NOT use IObuff or
179 * NameBuff. Thus emsg2() cannot be called!
180 */
181 mch_early_init();
182
Bram Moolenaar14993322014-09-09 12:25:33 +0200183#if defined(WIN32) && defined(FEAT_MBYTE)
184 /*
185 * MingW expands command line arguments, which confuses our code to
186 * convert when 'encoding' changes. Get the unexpanded arguments.
187 */
188 argc = get_cmd_argsW(&argv);
189#endif
190
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000191 /* Many variables are in "params" so that we can pass them to invoked
192 * functions without a lot of arguments. "argc" and "argv" are also
193 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000194 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000195 params.argc = argc;
196 params.argv = argv;
197 params.want_full_screen = TRUE;
198#ifdef FEAT_EVAL
199 params.use_debug_break_level = -1;
200#endif
201#ifdef FEAT_WINDOWS
202 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000203#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000204
Bram Moolenaar99685e62013-05-11 13:56:18 +0200205#ifdef FEAT_RUBY
206 {
207 int ruby_stack_start;
208 vim_ruby_init((void *)&ruby_stack_start);
209 }
210#endif
211
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000212#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000213 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000214#endif
215
216#ifdef MEM_PROFILE
217 atexit(vim_mem_profile_dump);
218#endif
219
220#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000221 for (i = 1; i < argc; ++i)
222 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000223 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000224 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000225 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000226 TIME_MSG("--- VIM STARTING ---");
227 break;
228 }
229 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000230#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000231 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000232
233#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000234 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000235#endif
236
237#ifdef FEAT_MBYTE
238 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
239#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000240#ifdef FEAT_EVAL
241 eval_init(); /* init global variables */
242#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000243
244#ifdef __QNXNTO__
245 qnx_init(); /* PhAttach() for clipboard, (and gui) */
246#endif
247
248#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000249 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000250 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000251 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000252 TIME_MSG("GUI prepared");
253#endif
254
255 /* Init the table of Normal mode commands. */
256 init_normal_cmds();
257
258#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000259 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000260#endif
261
262 /*
263 * Allocate space for the generic buffers (needed for set_init_1() and
264 * EMSG2()).
265 */
266 if ((IObuff = alloc(IOSIZE)) == NULL
267 || (NameBuff = alloc(MAXPATHL)) == NULL)
268 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000269 TIME_MSG("Allocated generic buffers");
270
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000271#ifdef NBDEBUG
272 /* Wait a moment for debugging NetBeans. Must be after allocating
273 * NameBuff. */
274 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
275 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000276 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000277#endif
278
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000279#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
280 /*
281 * Setup to use the current locale (for ctype() and many other things).
282 * NOTE: Translated messages with encodings other than latin1 will not
283 * work until set_init_1() has been called!
284 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000285 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000286 TIME_MSG("locale set");
287#endif
288
289#ifdef FEAT_GUI
290 gui.dofork = TRUE; /* default is to use fork() */
291#endif
292
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000293 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000294 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000295 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000296 * --server...
297 * --socketid
Bram Moolenaar78e17622007-08-30 10:26:19 +0000298 * --windowid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000299 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000300 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000301
302#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000303 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000304#endif
305#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000306 /* Prepare for possibly starting GUI sometime */
307 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000308 TIME_MSG("GUI prepared");
309#endif
310
311#ifdef FEAT_CLIPBOARD
312 clip_init(FALSE); /* Initialise clipboard stuff */
313 TIME_MSG("clipboard setup");
314#endif
315
316 /*
317 * Check if we have an interactive window.
318 * On the Amiga: If there is no window, we open one with a newcli command
319 * (needed for :! to * work). mch_check_win() will also handle the -d or
320 * -dev argument.
321 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000322 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000323 TIME_MSG("window checked");
324
325 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000326 * Allocate the first window and buffer.
327 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000328 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000329 if (win_alloc_first() == FAIL)
330 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000331
332 init_yank(); /* init yank buffers */
333
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000334 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200335 global_alist.id = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000336
337 /*
338 * Set the default values for the options.
339 * NOTE: Non-latin1 translated messages are working only after this,
340 * because this is where "has_mbyte" will be set, which is used by
341 * msg_outtrans_len_attr().
342 * First find out the home directory, needed to expand "~" in options.
343 */
344 init_homedir(); /* find real value of $HOME */
345 set_init_1();
346 TIME_MSG("inits 1");
347
348#ifdef FEAT_EVAL
349 set_lang_var(); /* set v:lang and v:ctype */
350#endif
351
352#ifdef FEAT_CLIENTSERVER
353 /*
354 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000355 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000356 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000357 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000358#endif
359
360 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000361 * Figure out the way to work from the command name argv[0].
362 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000363 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000364 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000365
366 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000367 * Process the command line arguments. File names are put in the global
368 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000369 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000370 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000371 TIME_MSG("parsing arguments");
372
373 /*
374 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000375 * there is no terminal version, and on Windows we can't fork one off with
376 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000377 */
378#ifdef ALWAYS_USE_GUI
379 gui.starting = TRUE;
380#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000381# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000382 /*
383 * Check if the GUI can be started. Reset gui.starting if not.
384 * Don't know about other systems, stay on the safe side and don't check.
385 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000386 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000387 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000388 if (gui_init_check() == FAIL)
389 {
390 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000391
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000392 /* When running "evim" or "gvim -y" we need the menus, exit if we
393 * don't have them. */
394 if (params.evim_mode)
395 mch_exit(1);
396 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000397 }
398# endif
399#endif
400
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000401 if (GARGCOUNT > 0)
402 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100403#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000404 /*
405 * Expand wildcards in file names.
406 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000407 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000408 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200409 start_dir = alloc(MAXPATHL);
410 if (start_dir != NULL)
411 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000412 /* Temporarily add '(' and ')' to 'isfname'. These are valid
413 * filename characters but are excluded from 'isfname' to make
414 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
415 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000416 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000417 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200418 if (start_dir != NULL)
419 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000420 }
421#endif
422 fname = alist_name(&GARGLIST[0]);
423 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000424
425#if defined(WIN32) && defined(FEAT_MBYTE)
426 {
427 extern void set_alist_count(void);
428
429 /* Remember the number of entries in the argument list. If it changes
430 * we don't react on setting 'encoding'. */
431 set_alist_count();
432 }
433#endif
434
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000435#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000436 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000437 {
438 /*
439 * If there is one filename, fully qualified, we have very probably
440 * been invoked from explorer, so change to the file's directory.
441 * Hint: to avoid this when typing a command use a forward slash.
442 * If the cd fails, it doesn't matter.
443 */
444 (void)vim_chdirfile(fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200445 if (start_dir != NULL)
446 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000447 }
448#endif
449 TIME_MSG("expanding arguments");
450
451#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000452 if (params.diff_mode && params.window_count == -1)
453 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000454#endif
455
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000456 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000457 ++RedrawingDisabled;
458
459 /*
460 * When listing swap file names, don't do cursor positioning et. al.
461 */
462 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000463 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000464
465 /*
466 * When certain to start the GUI, don't check capabilities of terminal.
467 * For GTK we can't be sure, but when started from the desktop it doesn't
468 * make sense to try using a terminal.
469 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000470#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000471 if (gui.starting
472# ifdef FEAT_GUI_GTK
473 && !isatty(2)
474# endif
475 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000476 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000477#endif
478
479#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
480 /* When the GUI is started from Finder, need to display messages in a
481 * message box. isatty(2) returns TRUE anyway, thus we need to check the
482 * name to know we're not started from a terminal. */
483 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000484 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000485 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000486
487 /* Avoid always using "/" as the current directory. Note that when
488 * started from Finder the arglist will be filled later in
489 * HandleODocAE() and "fname" will be NULL. */
490 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
491 && STRCMP(NameBuff, "/") == 0)
492 {
493 if (fname != NULL)
494 (void)vim_chdirfile(fname);
495 else
496 {
497 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
498 vim_chdir(NameBuff);
499 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200500 if (start_dir != NULL)
501 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000502 }
503 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000504#endif
505
506 /*
507 * mch_init() sets up the terminal (window) for use. This must be
508 * done after resetting full_screen, otherwise it may move the cursor
509 * (MSDOS).
510 * 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 "**". */
635 source_runtime((char_u *)"plugin/*.vim", TRUE);
636# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000637 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000638# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000639 TIME_MSG("loading plugins");
640 }
641#endif
642
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000643#ifdef FEAT_DIFF
644 /* Decide about window layout for diff mode after reading vimrc. */
645 if (params.diff_mode && params.window_layout == 0)
646 {
647 if (diffopt_horizontal())
648 params.window_layout = WIN_HOR; /* use horizontal split */
649 else
650 params.window_layout = WIN_VER; /* use vertical split */
651 }
652#endif
653
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000654 /*
655 * Recovery mode without a file name: List swap files.
656 * This uses the 'dir' option, therefore it must be after the
657 * initializations.
658 */
659 if (recoverymode && fname == NULL)
660 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200661 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000662 mch_exit(0);
663 }
664
665 /*
666 * Set a few option defaults after reading .vimrc files:
667 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
668 */
669 set_init_3();
670 TIME_MSG("inits 3");
671
672 /*
673 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
674 * Note that this overrides anything from a vimrc file.
675 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000676 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000677 p_uc = 0;
678
679#ifdef FEAT_FKMAP
680 if (curwin->w_p_rl && p_altkeymap)
681 {
682 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
683# ifdef FEAT_ARABIC
684 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
685# endif
686 p_fkmap = TRUE; /* Set the Farsi keymap mode */
687 }
688#endif
689
690#ifdef FEAT_GUI
691 if (gui.starting)
692 {
693#if defined(UNIX) || defined(VMS)
694 /* When something caused a message from a vimrc script, need to output
695 * an extra newline before the shell prompt. */
696 if (did_emsg || msg_didout)
697 putchar('\n');
698#endif
699
700 gui_start(); /* will set full_screen to TRUE */
701 TIME_MSG("starting GUI");
702
703 /* When running "evim" or "gvim -y" we need the menus, exit if we
704 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000705 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000706 mch_exit(1);
707 }
708#endif
709
710#ifdef SPAWNO /* special MSDOS swapping library */
711 init_SPAWNO("", SWAP_ANY);
712#endif
713
714#ifdef FEAT_VIMINFO
715 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000716 * Read in registers, history etc, but not marks, from the viminfo file.
717 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000718 */
719 if (*p_viminfo != NUL)
720 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000721 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000722 TIME_MSG("reading viminfo");
723 }
724#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100725#ifdef FEAT_EVAL
726 /* It's better to make v:oldfiles an empty list than NULL. */
727 if (get_vim_var_list(VV_OLDFILES) == NULL)
728 set_vim_var_list(VV_OLDFILES, list_alloc());
729#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000730
731#ifdef FEAT_QUICKFIX
732 /*
733 * "-q errorfile": Load the error file now.
734 * If the error file can't be read, exit before doing anything else.
735 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000736 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000737 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000738 if (params.use_ef != NULL)
739 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000740 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200741 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
742 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000743 {
744 out_char('\n');
745 mch_exit(3);
746 }
747 TIME_MSG("reading errorfile");
748 }
749#endif
750
751 /*
752 * Start putting things on the screen.
753 * Scroll screen down before drawing over it
754 * Clear screen now, so file message will not be cleared.
755 */
756 starting = NO_BUFFERS;
757 no_wait_return = FALSE;
758 if (!exmode_active)
759 msg_scroll = FALSE;
760
761#ifdef FEAT_GUI
762 /*
763 * This seems to be required to make callbacks to be called now, instead
764 * of after things have been put on the screen, which then may be deleted
765 * when getting a resize callback.
766 * For the Mac this handles putting files dropped on the Vim icon to
767 * global_alist.
768 */
769 if (gui.in_use)
770 {
771# ifdef FEAT_SUN_WORKSHOP
772 if (!usingSunWorkShop)
773# endif
774 gui_wait_for_chars(50L);
775 TIME_MSG("GUI delay");
776 }
777#endif
778
779#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
780 qnx_clip_init();
781#endif
782
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200783#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
784 clip_init(TRUE);
785#endif
786
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000787#ifdef FEAT_XCLIPBOARD
788 /* Start using the X clipboard, unless the GUI was started. */
789# ifdef FEAT_GUI
790 if (!gui.in_use)
791# endif
792 {
793 setup_term_clip();
794 TIME_MSG("setup clipboard");
795 }
796#endif
797
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000798#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000799 /* Prepare for being a Vim server. */
800 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000801#endif
802
803 /*
804 * If "-" argument given: Read file from stdin.
805 * Do this before starting Raw mode, because it may change things that the
806 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
807 * are the same terminal: "cat | vim -".
808 * Using autocommands here may cause trouble...
809 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000810 if (params.edit_type == EDIT_STDIN && !recoverymode)
811 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000812
813#if defined(UNIX) || defined(VMS)
814 /* When switching screens and something caused a message from a vimrc
815 * script, need to output an extra newline on exit. */
816 if ((did_emsg || msg_didout) && *T_TI != NUL)
817 newline_on_exit = TRUE;
818#endif
819
820 /*
821 * When done something that is not allowed or error message call
822 * wait_return. This must be done before starttermcap(), because it may
823 * switch to another screen. It must be done after settmode(TMODE_RAW),
824 * because we want to react on a single key stroke.
825 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000826 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000827 */
828 settmode(TMODE_RAW);
829 TIME_MSG("setting raw mode");
830
831 if (need_wait_return || msg_didany)
832 {
833 wait_return(TRUE);
834 TIME_MSG("waiting for return");
835 }
836
837 starttermcap(); /* start termcap if not done by wait_return() */
838 TIME_MSG("start termcap");
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200839#if defined(FEAT_TERMRESPONSE)
840# if defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200841 may_req_ambiguous_char_width();
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200842# endif
843 may_req_bg_color();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100844#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000845
846#ifdef FEAT_MOUSE
847 setmouse(); /* may start using the mouse */
848#endif
849 if (scroll_region)
850 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000851 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000852
853 /*
854 * Don't clear the screen when starting in Ex mode, unless using the GUI.
855 */
856 if (exmode_active
857#ifdef FEAT_GUI
858 && !gui.in_use
859#endif
860 )
861 must_redraw = CLEAR;
862 else
863 {
864 screenclear(); /* clear screen */
865 TIME_MSG("clearing screen");
866 }
867
868#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000869 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000870 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100871 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200872 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000873 TIME_MSG("getting crypt key");
874 }
875#endif
876
877 no_wait_return = TRUE;
878
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000879 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000880 * Create the requested number of windows and edit buffers in them.
881 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000882 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000883 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000884 TIME_MSG("opening buffers");
885
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000886#ifdef FEAT_EVAL
887 /* clear v:swapcommand */
888 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
889#endif
890
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000891 /* Ex starts at last line of the file */
892 if (exmode_active)
893 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
894
895#ifdef FEAT_AUTOCMD
896 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
897 TIME_MSG("BufEnter autocommands");
898#endif
899 setpcmark();
900
901#ifdef FEAT_QUICKFIX
902 /*
903 * When started with "-q errorfile" jump to first error now.
904 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000905 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000906 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000907 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000908 TIME_MSG("jump to first error");
909 }
910#endif
911
912#ifdef FEAT_WINDOWS
913 /*
914 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000915 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000916 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200917 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000918#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200919 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000920
921#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000922 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000923 {
924 win_T *wp;
925
926 /* set options in each window for "vimdiff". */
927 for (wp = firstwin; wp != NULL; wp = wp->w_next)
928 diff_win_options(wp, TRUE);
929 }
930#endif
931
932 /*
933 * Shorten any of the filenames, but only when absolute.
934 */
935 shorten_fnames(FALSE);
936
937 /*
938 * Need to jump to the tag before executing the '-c command'.
939 * Makes "vim -c '/return' -t main" work.
940 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000941 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000942 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000943#if defined(HAS_SWAP_EXISTS_ACTION)
944 swap_exists_did_quit = FALSE;
945#endif
946
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000947 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000948 do_cmdline_cmd(IObuff);
949 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000950
951#if defined(HAS_SWAP_EXISTS_ACTION)
952 /* If the user doesn't want to edit the file then we quit here. */
953 if (swap_exists_did_quit)
954 getout(1);
955#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000956 }
957
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000958 /* Execute any "+", "-c" and "-S" arguments. */
959 if (params.n_commands > 0)
960 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000961
962 RedrawingDisabled = 0;
963 redraw_all_later(NOT_VALID);
964 no_wait_return = FALSE;
965 starting = 0;
966
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000967#ifdef FEAT_TERMRESPONSE
968 /* Requesting the termresponse is postponed until here, so that a "-c q"
969 * argument doesn't make it appear in the shell Vim was started from. */
970 may_req_termresponse();
971#endif
972
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000973 /* start in insert mode */
974 if (p_im)
975 need_start_insertmode = TRUE;
976
977#ifdef FEAT_AUTOCMD
978 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
979 TIME_MSG("VimEnter autocommands");
980#endif
981
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200982#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
983 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
984 * done after the clipboard is available and all initial commands that may
985 * modify the 'clipboard' setting have run; i.e. just before entering the
986 * main loop. */
987 {
988 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100989
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200990 adjust_clip_reg(&default_regname);
991 set_reg_var(default_regname);
992 }
993#endif
994
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000995#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
996 /* When a startup script or session file setup for diff'ing and
997 * scrollbind, sync the scrollbind now. */
998 if (curwin->w_p_diff && curwin->w_p_scb)
999 {
1000 update_topline();
1001 check_scrollbind((linenr_T)0, 0L);
1002 TIME_MSG("diff scrollbinding");
1003 }
1004#endif
1005
1006#if defined(WIN3264) && !defined(FEAT_GUI_W32)
1007 mch_set_winsize_now(); /* Allow winsize changes from now on */
1008#endif
1009
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001010#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
1011 /* When tab pages were created, may need to update the tab pages line and
1012 * scrollbars. This is skipped while creating them. */
1013 if (first_tabpage->tp_next != NULL)
1014 {
1015 out_flush();
1016 gui_init_which_components(NULL);
1017 gui_update_scrollbars(TRUE);
1018 }
1019 need_mouse_correct = TRUE;
1020#endif
1021
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001022 /* If ":startinsert" command used, stuff a dummy command to be able to
1023 * call normal_cmd(), which will then start Insert mode. */
1024 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001025 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001026
1027#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001028 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001029 {
1030# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +02001031# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02001032 && !defined(FEAT_GUI_W32)
1033 if (gui.in_use)
1034 {
1035 mch_errmsg(_("netbeans is not supported with this GUI\n"));
1036 mch_exit(2);
1037 }
1038# endif
1039# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001040 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001041 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001042 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001043#endif
1044
1045 TIME_MSG("before starting main loop");
1046
1047 /*
1048 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001049 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001050 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001051
1052 return 0;
1053}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001054#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001055#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001056
1057/*
1058 * Main loop: Execute Normal mode commands until exiting Vim.
1059 * Also used to handle commands in the command-line window, until the window
1060 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 * Also used to handle ":visual" command after ":global": execute Normal mode
1062 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001063 */
1064 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001065main_loop(
1066 int cmdwin, /* TRUE when working in the command-line window */
1067 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001068{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001069 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001070 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001071#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001072 /* these are static to avoid a compiler warning */
1073 static linenr_T conceal_old_cursor_line = 0;
1074 static linenr_T conceal_new_cursor_line = 0;
1075 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001076#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001077
1078#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1079 /* Setup to catch a terminating error from the X server. Just ignore
1080 * it, restore the state and continue. This might not always work
1081 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001082 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001084 {
1085 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001086 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001087 got_int = TRUE;
1088 need_wait_return = FALSE;
1089 global_busy = FALSE;
1090 exmode_active = 0;
1091 skip_redraw = FALSE;
1092 RedrawingDisabled = 0;
1093 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001094 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001095# ifdef FEAT_EVAL
1096 emsg_skip = 0;
1097# endif
1098 emsg_off = 0;
1099# ifdef FEAT_MOUSE
1100 setmouse();
1101# endif
1102 settmode(TMODE_RAW);
1103 starttermcap();
1104 scroll_start();
1105 redraw_later_clear();
1106 }
1107#endif
1108
1109 clear_oparg(&oa);
1110 while (!cmdwin
1111#ifdef FEAT_CMDWIN
1112 || cmdwin_result == 0
1113#endif
1114 )
1115 {
1116 if (stuff_empty())
1117 {
1118 did_check_timestamps = FALSE;
1119 if (need_check_timestamps)
1120 check_timestamps(FALSE);
1121 if (need_wait_return) /* if wait_return still needed ... */
1122 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001123 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001124 {
1125 need_start_insertmode = FALSE;
1126 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1127 /* skip the fileinfo message now, because it would be shown
1128 * after insert mode finishes! */
1129 need_fileinfo = FALSE;
1130 }
1131 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001132
1133 /* Reset "got_int" now that we got back to the main loop. Except when
1134 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1135 * the ":g" command.
1136 * For ":g/pat/vi" we reset "got_int" when used once. When used
1137 * a second time we go back to Ex mode and abort the ":g" command. */
1138 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001139 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001140 if (noexmode && global_busy && !exmode_active && previous_got_int)
1141 {
1142 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1143 * used and keep "got_int" set, so that it aborts ":g". */
1144 exmode_active = EXMODE_NORMAL;
1145 State = NORMAL;
1146 }
1147 else if (!global_busy || !exmode_active)
1148 {
1149 if (!quit_more)
1150 (void)vgetc(); /* flush all buffers */
1151 got_int = FALSE;
1152 }
1153 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001154 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001155 else
1156 previous_got_int = FALSE;
1157
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001158 if (!exmode_active)
1159 msg_scroll = FALSE;
1160 quit_more = FALSE;
1161
1162 /*
1163 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1164 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1165 * update cursor and redraw.
1166 */
1167 if (skip_redraw || exmode_active)
1168 skip_redraw = FALSE;
1169 else if (do_redraw || stuff_empty())
1170 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001171#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001172 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001173 if (!finish_op && (
1174# ifdef FEAT_AUTOCMD
1175 has_cursormoved()
1176# endif
1177# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1178 ||
1179# endif
1180# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001181 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001182# endif
1183 )
1184 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001185 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001186# ifdef FEAT_AUTOCMD
1187 if (has_cursormoved())
1188 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1189 FALSE, curbuf);
1190# endif
1191# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001192 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001193 {
1194 conceal_old_cursor_line = last_cursormoved.lnum;
1195 conceal_new_cursor_line = curwin->w_cursor.lnum;
1196 conceal_update_lines = TRUE;
1197 }
1198# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001199 last_cursormoved = curwin->w_cursor;
1200 }
1201#endif
1202
Bram Moolenaar186628f2013-03-19 13:33:23 +01001203#ifdef FEAT_AUTOCMD
1204 /* Trigger TextChanged if b_changedtick differs. */
1205 if (!finish_op && has_textchanged()
1206 && last_changedtick != curbuf->b_changedtick)
1207 {
1208 if (last_changedtick_buf == curbuf)
1209 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1210 FALSE, curbuf);
1211 last_changedtick_buf = curbuf;
1212 last_changedtick = curbuf->b_changedtick;
1213 }
1214#endif
1215
Bram Moolenaar33aec762006-01-22 23:30:12 +00001216#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1217 /* Scroll-binding for diff mode may have been postponed until
1218 * here. Avoids doing it for every change. */
1219 if (diff_need_scrollbind)
1220 {
1221 check_scrollbind((linenr_T)0, 0L);
1222 diff_need_scrollbind = FALSE;
1223 }
1224#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001225#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001226 /* Include a closed fold completely in the Visual area. */
1227 foldAdjustVisual();
1228#endif
1229#ifdef FEAT_FOLDING
1230 /*
1231 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1232 * contain the cursor.
1233 * When 'foldopen' is "all", open the fold(s) under the cursor.
1234 * This may mark the window for redrawing.
1235 */
1236 if (hasAnyFolding(curwin) && !char_avail())
1237 {
1238 foldCheckClose();
1239 if (fdo_flags & FDO_ALL)
1240 foldOpenCursor();
1241 }
1242#endif
1243
1244 /*
1245 * Before redrawing, make sure w_topline is correct, and w_leftcol
1246 * if lines don't wrap, and w_skipcol if lines wrap.
1247 */
1248 update_topline();
1249 validate_cursor();
1250
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001251 if (VIsual_active)
1252 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001253 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001254 update_screen(0);
1255 else if (redraw_cmdline || clear_cmdline)
1256 showmode();
1257#ifdef FEAT_WINDOWS
1258 redraw_statuslines();
1259#endif
1260#ifdef FEAT_TITLE
1261 if (need_maketitle)
1262 maketitle();
1263#endif
1264 /* display message after redraw */
1265 if (keep_msg != NULL)
1266 {
1267 char_u *p;
1268
1269 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001270 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1271 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001272 p = keep_msg;
1273 msg_attr(p, keep_msg_attr);
1274 vim_free(p);
1275 }
1276 if (need_fileinfo) /* show file info after redraw */
1277 {
1278 fileinfo(FALSE, TRUE, FALSE);
1279 need_fileinfo = FALSE;
1280 }
1281
1282 emsg_on_display = FALSE; /* can delete error message now */
1283 did_emsg = FALSE;
1284 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001285 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001286 showruler(FALSE);
1287
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001288# if defined(FEAT_CONCEAL)
1289 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001290 && (conceal_old_cursor_line != conceal_new_cursor_line
1291 || conceal_cursor_line(curwin)
1292 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001293 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001294 if (conceal_old_cursor_line != conceal_new_cursor_line
1295 && conceal_old_cursor_line
1296 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001297 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001298 update_single_line(curwin, conceal_new_cursor_line);
1299 curwin->w_valid &= ~VALID_CROW;
1300 }
1301# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001302 setcursor();
1303 cursor_on();
1304
1305 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001306
1307#ifdef STARTUPTIME
1308 /* Now that we have drawn the first screen all the startup stuff
1309 * has been done, close any file for startup messages. */
1310 if (time_fd != NULL)
1311 {
1312 TIME_MSG("first screen update");
1313 TIME_MSG("--- VIM STARTED ---");
1314 fclose(time_fd);
1315 time_fd = NULL;
1316 }
1317#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001318 }
1319#ifdef FEAT_GUI
1320 if (need_mouse_correct)
1321 gui_mouse_correct();
1322#endif
1323
1324 /*
1325 * Update w_curswant if w_set_curswant has been set.
1326 * Postponed until here to avoid computing w_virtcol too often.
1327 */
1328 update_curswant();
1329
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001330#ifdef FEAT_EVAL
1331 /*
1332 * May perform garbage collection when waiting for a character, but
1333 * only at the very toplevel. Otherwise we may be using a List or
1334 * Dict internally somewhere.
1335 * "may_garbage_collect" is reset in vgetc() which is invoked through
1336 * do_exmode() and normal_cmd().
1337 */
1338 may_garbage_collect = (!cmdwin && !noexmode);
1339#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001340 /*
1341 * If we're invoked as ex, do a round of ex commands.
1342 * Otherwise, get and execute a normal mode command.
1343 */
1344 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001345 {
1346 if (noexmode) /* End of ":global/path/visual" commands */
1347 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001348 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001349 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001350 else
1351 normal_cmd(&oa, TRUE);
1352 }
1353}
1354
1355
1356#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1357/*
1358 * Exit, but leave behind swap files for modified buffers.
1359 */
1360 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001361getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001362{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001363# if defined(SIGHUP) && defined(SIG_IGN)
1364 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1365 * makes Vim exit and then handling SIGHUP causes various reentrance
1366 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001367 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001368# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001369
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001370 ml_close_notmod(); /* close all not-modified buffers */
1371 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1372 ml_close_all(FALSE); /* close all memfiles, without deleting */
1373 getout(exitval); /* exit Vim properly */
1374}
1375#endif
1376
1377
1378/* Exit properly */
1379 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001380getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001381{
1382#ifdef FEAT_AUTOCMD
1383 buf_T *buf;
1384 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001385 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001386#endif
1387
1388 exiting = TRUE;
1389
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001390 /* When running in Ex mode an error causes us to exit with a non-zero exit
1391 * code. POSIX requires this, although it's not 100% clear from the
1392 * standard. */
1393 if (exmode_active)
1394 exitval += ex_exitval;
1395
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001396 /* Position the cursor on the last screen line, below all the text */
1397#ifdef FEAT_GUI
1398 if (!gui.in_use)
1399#endif
1400 windgoto((int)Rows - 1, 0);
1401
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001402#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1403 /* Optionally print hashtable efficiency. */
1404 hash_debug_results();
1405#endif
1406
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001407#ifdef FEAT_GUI
1408 msg_didany = FALSE;
1409#endif
1410
1411#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001412 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001413 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001414 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1415# if defined FEAT_WINDOWS
1416 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001417 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001418 next_tp = tp->tp_next;
1419 for (wp = (tp == curtab)
1420 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001421 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001422 if (wp->w_buffer == NULL)
1423 /* Autocmd must have close the buffer already, skip. */
1424 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001425 buf = wp->w_buffer;
1426 if (buf->b_changedtick != -1)
1427 {
1428 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1429 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001430 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001431 /* start all over, autocommands may mess up the lists */
1432 next_tp = first_tabpage;
1433 break;
1434 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001435 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001436 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001437# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001438 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1439 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001440# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001441
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001442 /* Trigger BufUnload for buffers that are loaded */
1443 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1444 if (buf->b_ml.ml_mfp != NULL)
1445 {
1446 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001447 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001448 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1449 break;
1450 }
1451 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1452 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001453#endif
1454
1455#ifdef FEAT_VIMINFO
1456 if (*p_viminfo != NUL)
1457 /* Write out the registers, history, marks etc, to the viminfo file */
1458 write_viminfo(NULL, FALSE);
1459#endif
1460
1461#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001462 if (get_vim_var_nr(VV_DYING) <= 1)
1463 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001464#endif
1465
Bram Moolenaar05159a02005-02-26 23:04:13 +00001466#ifdef FEAT_PROFILE
1467 profile_dump();
1468#endif
1469
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001470 if (did_emsg
1471#ifdef FEAT_GUI
1472 || (gui.in_use && msg_didany && p_verbose > 0)
1473#endif
1474 )
1475 {
1476 /* give the user a chance to read the (error) message */
1477 no_wait_return = FALSE;
1478 wait_return(FALSE);
1479 }
1480
1481#ifdef FEAT_AUTOCMD
1482 /* Position the cursor again, the autocommands may have moved it */
1483# ifdef FEAT_GUI
1484 if (!gui.in_use)
1485# endif
1486 windgoto((int)Rows - 1, 0);
1487#endif
1488
Bram Moolenaar65edff82016-02-21 16:40:11 +01001489#ifdef FEAT_JOB
1490 job_stop_on_exit();
1491#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001492#ifdef FEAT_LUA
1493 lua_end();
1494#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001495#ifdef FEAT_MZSCHEME
1496 mzscheme_end();
1497#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001498#ifdef FEAT_TCL
1499 tcl_end();
1500#endif
1501#ifdef FEAT_RUBY
1502 ruby_end();
1503#endif
1504#ifdef FEAT_PYTHON
1505 python_end();
1506#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001507#ifdef FEAT_PYTHON3
1508 python3_end();
1509#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001510#ifdef FEAT_PERL
1511 perl_end();
1512#endif
1513#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1514 iconv_end();
1515#endif
1516#ifdef FEAT_NETBEANS_INTG
1517 netbeans_end();
1518#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001519#ifdef FEAT_CSCOPE
1520 cs_end();
1521#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001522#ifdef FEAT_EVAL
1523 if (garbage_collect_at_exit)
1524 garbage_collect();
1525#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001526#if defined(WIN32) && defined(FEAT_MBYTE)
1527 free_cmd_argsW();
1528#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001529
1530 mch_exit(exitval);
1531}
1532
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001533#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001534/*
1535 * Get a (optional) count for a Vim argument.
1536 */
1537 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001538get_number_arg(
1539 char_u *p, /* pointer to argument */
1540 int *idx, /* index in argument, is incremented */
1541 int def) /* default value */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001542{
1543 if (vim_isdigit(p[*idx]))
1544 {
1545 def = atoi((char *)&(p[*idx]));
1546 while (vim_isdigit(p[*idx]))
1547 *idx = *idx + 1;
1548 }
1549 return def;
1550}
1551
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001552#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1553/*
1554 * Setup to use the current locale (for ctype() and many other things).
1555 */
1556 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001557init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001558{
1559 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001560
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001561# ifdef FEAT_GUI_GTK
1562 /* Tell Gtk not to change our locale settings. */
1563 gtk_disable_setlocale();
1564# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001565# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1566 /* Make sure strtod() uses a decimal point, not a comma. */
1567 setlocale(LC_NUMERIC, "C");
1568# endif
1569
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001570# ifdef WIN32
1571 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1572 * text while it's expecting text in the current locale. This call avoids
1573 * that. */
1574 setlocale(LC_CTYPE, "C");
1575# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001576
1577# ifdef FEAT_GETTEXT
1578 {
1579 int mustfree = FALSE;
1580 char_u *p;
1581
1582# ifdef DYNAMIC_GETTEXT
1583 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001584 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001585# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001586 /* expand_env() doesn't work yet, because g_chartab[] is not
1587 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001588 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1589 if (p != NULL && *p != NUL)
1590 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001591 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001592 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1593 }
1594 if (mustfree)
1595 vim_free(p);
1596 textdomain(VIMPACKAGE);
1597 }
1598# endif
1599}
1600#endif
1601
1602/*
1603 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1604 * If the executable name starts with "r" we disable shell commands.
1605 * If the next character is "e" we run in Easy mode.
1606 * If the next character is "g" we run the GUI version.
1607 * If the next characters are "view" we start in readonly mode.
1608 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1609 * If the next characters are "ex" we start in Ex mode. If it's followed
1610 * by "im" use improved Ex mode.
1611 */
1612 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001613parse_command_name(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001614{
1615 char_u *initstr;
1616
1617 initstr = gettail((char_u *)parmp->argv[0]);
1618
1619#ifdef MACOS_X_UNIX
1620 /* An issue has been seen when launching Vim in such a way that
1621 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1622 * executable or a symbolic link of it. Until this issue is resolved
1623 * we prohibit the GUI from being used.
1624 */
1625 if (STRCMP(initstr, parmp->argv[0]) == 0)
1626 disallow_gui = TRUE;
1627
1628 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001629 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001630#endif
1631
1632#ifdef FEAT_EVAL
1633 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaara1706c92014-04-01 19:55:49 +02001634 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001635#endif
1636
1637 if (TOLOWER_ASC(initstr[0]) == 'r')
1638 {
1639 restricted = TRUE;
1640 ++initstr;
1641 }
1642
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001643 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001644 if (TOLOWER_ASC(initstr[0]) == 'e'
1645 && (TOLOWER_ASC(initstr[1]) == 'v'
1646 || TOLOWER_ASC(initstr[1]) == 'g'))
1647 {
1648#ifdef FEAT_GUI
1649 gui.starting = TRUE;
1650#endif
1651 parmp->evim_mode = TRUE;
1652 ++initstr;
1653 }
1654
Bram Moolenaar806875d2008-09-18 18:57:10 +00001655 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1656 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001657 {
1658 main_start_gui();
1659#ifdef FEAT_GUI
1660 ++initstr;
1661#endif
1662 }
1663
1664 if (STRNICMP(initstr, "view", 4) == 0)
1665 {
1666 readonlymode = TRUE;
1667 curbuf->b_p_ro = TRUE;
1668 p_uc = 10000; /* don't update very often */
1669 initstr += 4;
1670 }
1671 else if (STRNICMP(initstr, "vim", 3) == 0)
1672 initstr += 3;
1673
1674 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1675 if (STRICMP(initstr, "diff") == 0)
1676 {
1677#ifdef FEAT_DIFF
1678 parmp->diff_mode = TRUE;
1679#else
1680 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1681 mch_errmsg("\n");
1682 mch_exit(2);
1683#endif
1684 }
1685
1686 if (STRNICMP(initstr, "ex", 2) == 0)
1687 {
1688 if (STRNICMP(initstr + 2, "im", 2) == 0)
1689 exmode_active = EXMODE_VIM;
1690 else
1691 exmode_active = EXMODE_NORMAL;
1692 change_compatible(TRUE); /* set 'compatible' */
1693 }
1694}
1695
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001696/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001697 * Get the name of the display, before gui_prepare() removes it from
1698 * argv[]. Used for the xterm-clipboard display.
1699 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001700 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001701 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001702 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001703early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001704{
Bram Moolenaar03005972008-11-20 13:12:36 +00001705#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1706 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001707 int argc = parmp->argc;
1708 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001709 int i;
1710
1711 for (i = 1; i < argc; i++)
1712 {
1713 if (STRCMP(argv[i], "--") == 0)
1714 break;
1715# ifdef FEAT_XCLIPBOARD
1716 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001717# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001718 || STRICMP(argv[i], "--display") == 0
1719# endif
1720 )
1721 {
1722 if (i == argc - 1)
1723 mainerr_arg_missing((char_u *)argv[i]);
1724 xterm_display = argv[++i];
1725 }
1726# endif
1727# ifdef FEAT_CLIENTSERVER
1728 else if (STRICMP(argv[i], "--servername") == 0)
1729 {
1730 if (i == argc - 1)
1731 mainerr_arg_missing((char_u *)argv[i]);
1732 parmp->serverName_arg = (char_u *)argv[++i];
1733 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001734 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001735 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001736 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001737 {
1738 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001739# ifdef FEAT_GUI
1740 if (strstr(argv[i], "-wait") != 0)
1741 /* don't fork() when starting the GUI to edit files ourself */
1742 gui.dofork = FALSE;
1743# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001744 }
1745# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001746
1747# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1748# ifdef FEAT_GUI_W32
1749 else if (STRICMP(argv[i], "--windowid") == 0)
1750# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001751 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001752# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001753 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001754 long_u id;
1755 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001756
1757 if (i == argc - 1)
1758 mainerr_arg_missing((char_u *)argv[i]);
1759 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001760 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001761 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001762 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001763 if (count != 1)
1764 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1765 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001766# ifdef FEAT_GUI_W32
1767 win_socket_id = id;
1768# else
1769 gtk_socket_id = id;
1770# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001771 i++;
1772 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001773# endif
1774# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001775 else if (STRICMP(argv[i], "--echo-wid") == 0)
1776 echo_wid_arg = TRUE;
1777# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001778# ifndef FEAT_NETBEANS_INTG
1779 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001780 {
1781 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1782 mch_exit(2);
1783 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001784# endif
1785
Bram Moolenaar58d98232005-07-23 22:25:46 +00001786 }
1787#endif
1788}
1789
1790/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001791 * Scan the command line arguments.
1792 */
1793 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001794command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001795{
1796 int argc = parmp->argc;
1797 char **argv = parmp->argv;
1798 int argv_idx; /* index in argv[n][] */
1799 int had_minmin = FALSE; /* found "--" argument */
1800 int want_argument; /* option argument with argument */
1801 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001802 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001803 long n;
1804
1805 --argc;
1806 ++argv;
1807 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1808 while (argc > 0)
1809 {
1810 /*
1811 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1812 */
1813 if (argv[0][0] == '+' && !had_minmin)
1814 {
1815 if (parmp->n_commands >= MAX_ARG_CMDS)
1816 mainerr(ME_EXTRA_CMD, NULL);
1817 argv_idx = -1; /* skip to next argument */
1818 if (argv[0][1] == NUL)
1819 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1820 else
1821 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1822 }
1823
1824 /*
1825 * Optional argument.
1826 */
1827 else if (argv[0][0] == '-' && !had_minmin)
1828 {
1829 want_argument = FALSE;
1830 c = argv[0][argv_idx++];
1831#ifdef VMS
1832 /*
1833 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1834 * and "-/X" as "-X".
1835 */
1836 if (c == '/')
1837 {
1838 c = argv[0][argv_idx++];
1839 c = TOUPPER_ASC(c);
1840 }
1841 else
1842 c = TOLOWER_ASC(c);
1843#endif
1844 switch (c)
1845 {
1846 case NUL: /* "vim -" read from stdin */
1847 /* "ex -" silent mode */
1848 if (exmode_active)
1849 silent_mode = TRUE;
1850 else
1851 {
1852 if (parmp->edit_type != EDIT_NONE)
1853 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1854 parmp->edit_type = EDIT_STDIN;
1855 read_cmd_fd = 2; /* read from stderr instead of stdin */
1856 }
1857 argv_idx = -1; /* skip to next argument */
1858 break;
1859
1860 case '-': /* "--" don't take any more option arguments */
1861 /* "--help" give help message */
1862 /* "--version" give version message */
1863 /* "--literal" take files literally */
1864 /* "--nofork" don't fork */
1865 /* "--noplugin[s]" skip plugins */
1866 /* "--cmd <cmd>" execute cmd before vimrc */
1867 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1868 usage();
1869 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1870 {
1871 Columns = 80; /* need to init Columns */
1872 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1873 list_version();
1874 msg_putchar('\n');
1875 msg_didout = FALSE;
1876 mch_exit(0);
1877 }
1878 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1879 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001880#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001881 parmp->literal = TRUE;
1882#endif
1883 }
1884 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1885 {
1886#ifdef FEAT_GUI
1887 gui.dofork = FALSE; /* don't fork() when starting GUI */
1888#endif
1889 }
1890 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1891 p_lpl = FALSE;
1892 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1893 {
1894 want_argument = TRUE;
1895 argv_idx += 3;
1896 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001897 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1898 {
1899 want_argument = TRUE;
1900 argv_idx += 11;
1901 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001902#ifdef FEAT_CLIENTSERVER
1903 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1904 ; /* already processed -- no arg */
1905 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1906 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1907 {
1908 /* already processed -- snatch the following arg */
1909 if (argc > 1)
1910 {
1911 --argc;
1912 ++argv;
1913 }
1914 }
1915#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001916#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1917# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001918 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001919# else
1920 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1921# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001922 {
1923 /* already processed -- snatch the following arg */
1924 if (argc > 1)
1925 {
1926 --argc;
1927 ++argv;
1928 }
1929 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001930#endif
1931#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001932 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1933 {
1934 /* already processed, skip */
1935 }
1936#endif
1937 else
1938 {
1939 if (argv[0][argv_idx])
1940 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1941 had_minmin = TRUE;
1942 }
1943 if (!want_argument)
1944 argv_idx = -1; /* skip to next argument */
1945 break;
1946
1947 case 'A': /* "-A" start in Arabic mode */
1948#ifdef FEAT_ARABIC
1949 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1950#else
1951 mch_errmsg(_(e_noarabic));
1952 mch_exit(2);
1953#endif
1954 break;
1955
1956 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001957 /* Needs to be effective before expanding file names, because
1958 * for Win32 this makes us edit a shortcut file itself,
1959 * instead of the file it links to. */
1960 set_options_bin(curbuf->b_p_bin, 1, 0);
1961 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001962 break;
1963
1964 case 'C': /* "-C" Compatible */
1965 change_compatible(TRUE);
1966 break;
1967
1968 case 'e': /* "-e" Ex mode */
1969 exmode_active = EXMODE_NORMAL;
1970 break;
1971
1972 case 'E': /* "-E" Improved Ex mode */
1973 exmode_active = EXMODE_VIM;
1974 break;
1975
1976 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1977 window directly, not with newcli */
1978#ifdef FEAT_GUI
1979 gui.dofork = FALSE; /* don't fork() when starting GUI */
1980#endif
1981 break;
1982
1983 case 'g': /* "-g" start GUI */
1984 main_start_gui();
1985 break;
1986
1987 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1988#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001989 p_fkmap = TRUE;
1990 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001991#else
1992 mch_errmsg(_(e_nofarsi));
1993 mch_exit(2);
1994#endif
1995 break;
1996
1997 case 'h': /* "-h" give help message */
1998#ifdef FEAT_GUI_GNOME
1999 /* Tell usage() to exit for "gvim". */
2000 gui.starting = FALSE;
2001#endif
2002 usage();
2003 break;
2004
2005 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2006#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002007 p_hkmap = TRUE;
2008 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002009#else
2010 mch_errmsg(_(e_nohebrew));
2011 mch_exit(2);
2012#endif
2013 break;
2014
2015 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2016#ifdef FEAT_LISP
2017 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2018 p_sm = TRUE;
2019#endif
2020 break;
2021
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002022 case 'M': /* "-M" no changes or writing of files */
2023 reset_modifiable();
2024 /* FALLTHROUGH */
2025
2026 case 'm': /* "-m" no writing of files */
2027 p_write = FALSE;
2028 break;
2029
2030 case 'y': /* "-y" easy mode */
2031#ifdef FEAT_GUI
2032 gui.starting = TRUE; /* start GUI a bit later */
2033#endif
2034 parmp->evim_mode = TRUE;
2035 break;
2036
2037 case 'N': /* "-N" Nocompatible */
2038 change_compatible(FALSE);
2039 break;
2040
2041 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002042#ifdef FEAT_NETBEANS_INTG
2043 /* checking for "-nb", netbeans parameters */
2044 if (argv[0][argv_idx] == 'b')
2045 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002046 netbeansArg = argv[0];
2047 argv_idx = -1; /* skip to next argument */
2048 }
2049 else
2050#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002051 parmp->no_swap_file = TRUE;
2052 break;
2053
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002054 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002055#ifdef TARGET_API_MAC_OSX
2056 /* For some reason on MacOS X, an argument like:
2057 -psn_0_10223617 is passed in when invoke from Finder
2058 or with the 'open' command */
2059 if (argv[0][argv_idx] == 's')
2060 {
2061 argv_idx = -1; /* bypass full -psn */
2062 main_start_gui();
2063 break;
2064 }
2065#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002066#ifdef FEAT_WINDOWS
2067 /* default is 0: open window for each file */
2068 parmp->window_count = get_number_arg((char_u *)argv[0],
2069 &argv_idx, 0);
2070 parmp->window_layout = WIN_TABS;
2071#endif
2072 break;
2073
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002074 case 'o': /* "-o[N]" open N horizontal split windows */
2075#ifdef FEAT_WINDOWS
2076 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002077 parmp->window_count = get_number_arg((char_u *)argv[0],
2078 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002079 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002080#endif
2081 break;
2082
2083 case 'O': /* "-O[N]" open N vertical split windows */
2084#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2085 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002086 parmp->window_count = get_number_arg((char_u *)argv[0],
2087 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002088 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002089#endif
2090 break;
2091
2092#ifdef FEAT_QUICKFIX
2093 case 'q': /* "-q" QuickFix mode */
2094 if (parmp->edit_type != EDIT_NONE)
2095 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2096 parmp->edit_type = EDIT_QF;
2097 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2098 {
2099 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2100 argv_idx = -1;
2101 }
2102 else if (argc > 1) /* "-q {errorfile}" */
2103 want_argument = TRUE;
2104 break;
2105#endif
2106
2107 case 'R': /* "-R" readonly mode */
2108 readonlymode = TRUE;
2109 curbuf->b_p_ro = TRUE;
2110 p_uc = 10000; /* don't update very often */
2111 break;
2112
2113 case 'r': /* "-r" recovery mode */
2114 case 'L': /* "-L" recovery mode */
2115 recoverymode = 1;
2116 break;
2117
2118 case 's':
2119 if (exmode_active) /* "-s" silent (batch) mode */
2120 silent_mode = TRUE;
2121 else /* "-s {scriptin}" read from script file */
2122 want_argument = TRUE;
2123 break;
2124
2125 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2126 if (parmp->edit_type != EDIT_NONE)
2127 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2128 parmp->edit_type = EDIT_TAG;
2129 if (argv[0][argv_idx]) /* "-t{tag}" */
2130 {
2131 parmp->tagname = (char_u *)argv[0] + argv_idx;
2132 argv_idx = -1;
2133 }
2134 else /* "-t {tag}" */
2135 want_argument = TRUE;
2136 break;
2137
2138#ifdef FEAT_EVAL
2139 case 'D': /* "-D" Debugging */
2140 parmp->use_debug_break_level = 9999;
2141 break;
2142#endif
2143#ifdef FEAT_DIFF
2144 case 'd': /* "-d" 'diff' */
2145# ifdef AMIGA
2146 /* check for "-dev {device}" */
2147 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2148 want_argument = TRUE;
2149 else
2150# endif
2151 parmp->diff_mode = TRUE;
2152 break;
2153#endif
2154 case 'V': /* "-V{N}" Verbose level */
2155 /* default is 10: a little bit verbose */
2156 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2157 if (argv[0][argv_idx] != NUL)
2158 {
2159 set_option_value((char_u *)"verbosefile", 0L,
2160 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002161 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002162 }
2163 break;
2164
2165 case 'v': /* "-v" Vi-mode (as if called "vi") */
2166 exmode_active = 0;
2167#ifdef FEAT_GUI
2168 gui.starting = FALSE; /* don't start GUI */
2169#endif
2170 break;
2171
2172 case 'w': /* "-w{number}" set window height */
2173 /* "-w {scriptout}" write to script */
2174 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2175 {
2176 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2177 set_option_value((char_u *)"window", n, NULL, 0);
2178 break;
2179 }
2180 want_argument = TRUE;
2181 break;
2182
2183#ifdef FEAT_CRYPT
2184 case 'x': /* "-x" encrypted reading/writing of files */
2185 parmp->ask_for_key = TRUE;
2186 break;
2187#endif
2188
2189 case 'X': /* "-X" don't connect to X server */
2190#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2191 x_no_connect = TRUE;
2192#endif
2193 break;
2194
2195 case 'Z': /* "-Z" restricted mode */
2196 restricted = TRUE;
2197 break;
2198
2199 case 'c': /* "-c{command}" or "-c {command}" execute
2200 command */
2201 if (argv[0][argv_idx] != NUL)
2202 {
2203 if (parmp->n_commands >= MAX_ARG_CMDS)
2204 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002205 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2206 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002207 argv_idx = -1;
2208 break;
2209 }
2210 /*FALLTHROUGH*/
2211 case 'S': /* "-S {file}" execute Vim script */
2212 case 'i': /* "-i {viminfo}" use for viminfo */
2213#ifndef FEAT_DIFF
2214 case 'd': /* "-d {device}" device (for Amiga) */
2215#endif
2216 case 'T': /* "-T {terminal}" terminal name */
2217 case 'u': /* "-u {vimrc}" vim inits file */
2218 case 'U': /* "-U {gvimrc}" gvim inits file */
2219 case 'W': /* "-W {scriptout}" overwrite */
2220#ifdef FEAT_GUI_W32
2221 case 'P': /* "-P {parent title}" MDI parent */
2222#endif
2223 want_argument = TRUE;
2224 break;
2225
2226 default:
2227 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2228 }
2229
2230 /*
2231 * Handle option arguments with argument.
2232 */
2233 if (want_argument)
2234 {
2235 /*
2236 * Check for garbage immediately after the option letter.
2237 */
2238 if (argv[0][argv_idx] != NUL)
2239 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2240
2241 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002242 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002243 mainerr_arg_missing((char_u *)argv[0]);
2244 ++argv;
2245 argv_idx = -1;
2246
2247 switch (c)
2248 {
2249 case 'c': /* "-c {command}" execute command */
2250 case 'S': /* "-S {file}" execute Vim script */
2251 if (parmp->n_commands >= MAX_ARG_CMDS)
2252 mainerr(ME_EXTRA_CMD, NULL);
2253 if (c == 'S')
2254 {
2255 char *a;
2256
2257 if (argc < 1)
2258 /* "-S" without argument: use default session file
2259 * name. */
2260 a = SESSION_FILE;
2261 else if (argv[0][0] == '-')
2262 {
2263 /* "-S" followed by another option: use default
2264 * session file name. */
2265 a = SESSION_FILE;
2266 ++argc;
2267 --argv;
2268 }
2269 else
2270 a = argv[0];
2271 p = alloc((unsigned)(STRLEN(a) + 4));
2272 if (p == NULL)
2273 mch_exit(2);
2274 sprintf((char *)p, "so %s", a);
2275 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2276 parmp->commands[parmp->n_commands++] = p;
2277 }
2278 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002279 parmp->commands[parmp->n_commands++] =
2280 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002281 break;
2282
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002283 case '-':
2284 if (argv[-1][2] == 'c')
2285 {
2286 /* "--cmd {command}" execute command */
2287 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2288 mainerr(ME_EXTRA_CMD, NULL);
2289 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002290 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002291 }
2292 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002293 break;
2294
2295 /* case 'd': -d {device} is handled in mch_check_win() for the
2296 * Amiga */
2297
2298#ifdef FEAT_QUICKFIX
2299 case 'q': /* "-q {errorfile}" QuickFix mode */
2300 parmp->use_ef = (char_u *)argv[0];
2301 break;
2302#endif
2303
2304 case 'i': /* "-i {viminfo}" use for viminfo */
2305 use_viminfo = (char_u *)argv[0];
2306 break;
2307
2308 case 's': /* "-s {scriptin}" read from script file */
2309 if (scriptin[0] != NULL)
2310 {
2311scripterror:
2312 mch_errmsg(_("Attempt to open script file again: \""));
2313 mch_errmsg(argv[-1]);
2314 mch_errmsg(" ");
2315 mch_errmsg(argv[0]);
2316 mch_errmsg("\"\n");
2317 mch_exit(2);
2318 }
2319 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2320 {
2321 mch_errmsg(_("Cannot open for reading: \""));
2322 mch_errmsg(argv[0]);
2323 mch_errmsg("\"\n");
2324 mch_exit(2);
2325 }
2326 if (save_typebuf() == FAIL)
2327 mch_exit(2); /* out of memory */
2328 break;
2329
2330 case 't': /* "-t {tag}" */
2331 parmp->tagname = (char_u *)argv[0];
2332 break;
2333
2334 case 'T': /* "-T {terminal}" terminal name */
2335 /*
2336 * The -T term argument is always available and when
2337 * HAVE_TERMLIB is supported it overrides the environment
2338 * variable TERM.
2339 */
2340#ifdef FEAT_GUI
2341 if (term_is_gui((char_u *)argv[0]))
2342 gui.starting = TRUE; /* start GUI a bit later */
2343 else
2344#endif
2345 parmp->term = (char_u *)argv[0];
2346 break;
2347
2348 case 'u': /* "-u {vimrc}" vim inits file */
2349 parmp->use_vimrc = (char_u *)argv[0];
2350 break;
2351
2352 case 'U': /* "-U {gvimrc}" gvim inits file */
2353#ifdef FEAT_GUI
2354 use_gvimrc = (char_u *)argv[0];
2355#endif
2356 break;
2357
2358 case 'w': /* "-w {nr}" 'window' value */
2359 /* "-w {scriptout}" append to script file */
2360 if (vim_isdigit(*((char_u *)argv[0])))
2361 {
2362 argv_idx = 0;
2363 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2364 set_option_value((char_u *)"window", n, NULL, 0);
2365 argv_idx = -1;
2366 break;
2367 }
2368 /*FALLTHROUGH*/
2369 case 'W': /* "-W {scriptout}" overwrite script file */
2370 if (scriptout != NULL)
2371 goto scripterror;
2372 if ((scriptout = mch_fopen(argv[0],
2373 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2374 {
2375 mch_errmsg(_("Cannot open for script output: \""));
2376 mch_errmsg(argv[0]);
2377 mch_errmsg("\"\n");
2378 mch_exit(2);
2379 }
2380 break;
2381
2382#ifdef FEAT_GUI_W32
2383 case 'P': /* "-P {parent title}" MDI parent */
2384 gui_mch_set_parent(argv[0]);
2385 break;
2386#endif
2387 }
2388 }
2389 }
2390
2391 /*
2392 * File name argument.
2393 */
2394 else
2395 {
2396 argv_idx = -1; /* skip to next argument */
2397
2398 /* Check for only one type of editing. */
2399 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2400 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2401 parmp->edit_type = EDIT_FILE;
2402
2403#ifdef MSWIN
2404 /* Remember if the argument was a full path before changing
2405 * slashes to backslashes. */
2406 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2407 parmp->full_path = TRUE;
2408#endif
2409
2410 /* Add the file to the global argument list. */
2411 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2412 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2413 mch_exit(2);
2414#ifdef FEAT_DIFF
2415 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2416 && !mch_isdir(alist_name(&GARGLIST[0])))
2417 {
2418 char_u *r;
2419
2420 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2421 if (r != NULL)
2422 {
2423 vim_free(p);
2424 p = r;
2425 }
2426 }
2427#endif
2428#if defined(__CYGWIN32__) && !defined(WIN32)
2429 /*
2430 * If vim is invoked by non-Cygwin tools, convert away any
2431 * DOS paths, so things like .swp files are created correctly.
2432 * Look for evidence of non-Cygwin paths before we bother.
2433 * This is only for when using the Unix files.
2434 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002435 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002436 {
2437 char posix_path[PATH_MAX];
2438
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002439# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2440 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2441# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002442 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002443# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002444 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002445 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002446 if (p == NULL)
2447 mch_exit(2);
2448 }
2449#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002450
2451#ifdef USE_FNAME_CASE
2452 /* Make the case of the file name match the actual file. */
2453 fname_case(p, 0);
2454#endif
2455
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002456 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002457#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002458 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002459#else
2460 2 /* add buffer number now and use curbuf */
2461#endif
2462 );
2463
2464#if defined(FEAT_MBYTE) && defined(WIN32)
2465 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002466 /* Remember this argument has been added to the argument list.
2467 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002468 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002469# ifdef FEAT_DIFF
2470 parmp->diff_mode
2471# else
2472 FALSE
2473# endif
2474 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002475 }
2476#endif
2477 }
2478
2479 /*
2480 * If there are no more letters after the current "-", go to next
2481 * argument. argv_idx is set to -1 when the current argument is to be
2482 * skipped.
2483 */
2484 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2485 {
2486 --argc;
2487 ++argv;
2488 argv_idx = 1;
2489 }
2490 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002491
2492#ifdef FEAT_EVAL
2493 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2494 * one. */
2495 if (parmp->n_commands > 0)
2496 {
2497 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2498 if (p != NULL)
2499 {
2500 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2501 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2502 vim_free(p);
2503 }
2504 }
2505#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002506}
2507
2508/*
2509 * Print a warning if stdout is not a terminal.
2510 * When starting in Ex mode and commands come from a file, set Silent mode.
2511 */
2512 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002513check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002514{
2515 int input_isatty; /* is active input a terminal? */
2516
2517 input_isatty = mch_input_isatty();
2518 if (exmode_active)
2519 {
2520 if (!input_isatty)
2521 silent_mode = TRUE;
2522 }
2523 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2524#ifdef FEAT_GUI
2525 /* don't want the delay when started from the desktop */
2526 && !gui.starting
2527#endif
2528 )
2529 {
2530#ifdef NBDEBUG
2531 /*
2532 * This shouldn't be necessary. But if I run netbeans with the log
2533 * output coming to the console and XOpenDisplay fails, I get vim
2534 * trying to start with input/output to my console tty. This fills my
2535 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002536 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002537 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002538 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002539 {
2540 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2541 exit(1);
2542 }
2543#endif
2544 if (!parmp->stdout_isatty)
2545 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2546 if (!input_isatty)
2547 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2548 out_flush();
2549 if (scriptin[0] == NULL)
2550 ui_delay(2000L, TRUE);
2551 TIME_MSG("Warning delay");
2552 }
2553}
2554
2555/*
2556 * Read text from stdin.
2557 */
2558 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002559read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002560{
2561 int i;
2562
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002563#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002564 /* When getting the ATTENTION prompt here, use a dialog */
2565 swap_exists_action = SEA_DIALOG;
2566#endif
2567 no_wait_return = TRUE;
2568 i = msg_didany;
2569 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002570 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002571 no_wait_return = FALSE;
2572 msg_didany = i;
2573 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002574#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002575 check_swap_exists_action();
2576#endif
2577#if !(defined(AMIGA) || defined(MACOS))
2578 /*
2579 * Close stdin and dup it from stderr. Required for GPM to work
2580 * properly, and for running external commands.
2581 * Is there any other system that cannot do this?
2582 */
2583 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002584 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002585#endif
2586}
2587
2588/*
2589 * Create the requested number of windows and edit buffers in them.
2590 * Also does recovery if "recoverymode" set.
2591 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002592 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002593create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002594{
2595#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002596 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002597 int done = 0;
2598
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002599 /*
2600 * Create the number of windows that was requested.
2601 */
2602 if (parmp->window_count == -1) /* was not set */
2603 parmp->window_count = 1;
2604 if (parmp->window_count == 0)
2605 parmp->window_count = GARGCOUNT;
2606 if (parmp->window_count > 1)
2607 {
2608 /* Don't change the windows if there was a command in .vimrc that
2609 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002610 if (parmp->window_layout == 0)
2611 parmp->window_layout = WIN_HOR;
2612 if (parmp->window_layout == WIN_TABS)
2613 {
2614 parmp->window_count = make_tabpages(parmp->window_count);
2615 TIME_MSG("making tab pages");
2616 }
2617 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002618 {
2619 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002620 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002621 TIME_MSG("making windows");
2622 }
2623 else
2624 parmp->window_count = win_count();
2625 }
2626 else
2627 parmp->window_count = 1;
2628#endif
2629
2630 if (recoverymode) /* do recover */
2631 {
2632 msg_scroll = TRUE; /* scroll message up */
2633 ml_recover();
2634 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2635 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002636 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002637 }
2638 else
2639 {
2640 /*
2641 * Open a buffer for windows that don't have one yet.
2642 * Commands in the .vimrc might have loaded a file or split the window.
2643 * Watch out for autocommands that delete a window.
2644 */
2645#ifdef FEAT_AUTOCMD
2646 /*
2647 * Don't execute Win/Buf Enter/Leave autocommands here
2648 */
2649 ++autocmd_no_enter;
2650 ++autocmd_no_leave;
2651#endif
2652#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002653 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002654 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002655 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002656 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002657 {
2658 if (parmp->window_layout == WIN_TABS)
2659 goto_tabpage(1);
2660 else
2661 curwin = firstwin;
2662 }
2663 else if (parmp->window_layout == WIN_TABS)
2664 {
2665 if (curtab->tp_next == NULL)
2666 break;
2667 goto_tabpage(0);
2668 }
2669 else
2670 {
2671 if (curwin->w_next == NULL)
2672 break;
2673 curwin = curwin->w_next;
2674 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002675 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002676#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002677 curbuf = curwin->w_buffer;
2678 if (curbuf->b_ml.ml_mfp == NULL)
2679 {
2680#ifdef FEAT_FOLDING
2681 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2682 if (p_fdls >= 0)
2683 curwin->w_p_fdl = p_fdls;
2684#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002685#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002686 /* When getting the ATTENTION prompt here, use a dialog */
2687 swap_exists_action = SEA_DIALOG;
2688#endif
2689 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002690
2691 /* create memfile, read file */
2692 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002693
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002694#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002695 if (swap_exists_action == SEA_QUIT)
2696 {
2697 if (got_int || only_one_window())
2698 {
2699 /* abort selected or quit and only one window */
2700 did_emsg = FALSE; /* avoid hit-enter prompt */
2701 getout(1);
2702 }
2703 /* We can't close the window, it would disturb what
2704 * happens next. Clear the file name and set the arg
2705 * index to -1 to delete it later. */
2706 setfname(curbuf, NULL, NULL, FALSE);
2707 curwin->w_arg_idx = -1;
2708 swap_exists_action = SEA_NONE;
2709 }
2710 else
2711 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002712#endif
2713#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002714 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002715#endif
2716 }
2717#ifdef FEAT_WINDOWS
2718 ui_breakcheck();
2719 if (got_int)
2720 {
2721 (void)vgetc(); /* only break the file loading, not the rest */
2722 break;
2723 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002724 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002725#endif
2726#ifdef FEAT_WINDOWS
2727 if (parmp->window_layout == WIN_TABS)
2728 goto_tabpage(1);
2729 else
2730 curwin = firstwin;
2731 curbuf = curwin->w_buffer;
2732#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002733#ifdef FEAT_AUTOCMD
2734 --autocmd_no_enter;
2735 --autocmd_no_leave;
2736#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002737 }
2738}
2739
2740#ifdef FEAT_WINDOWS
2741 /*
2742 * If opened more than one window, start editing files in the other
2743 * windows. make_windows() has already opened the windows.
2744 */
2745 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002746edit_buffers(
2747 mparm_T *parmp,
2748 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002749{
2750 int arg_idx; /* index in argument list */
2751 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002752 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002753 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002754
2755# ifdef FEAT_AUTOCMD
2756 /*
2757 * Don't execute Win/Buf Enter/Leave autocommands here
2758 */
2759 ++autocmd_no_enter;
2760 ++autocmd_no_leave;
2761# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002762
2763 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2764 if (curwin->w_arg_idx == -1)
2765 {
2766 win_close(curwin, TRUE);
2767 advance = FALSE;
2768 }
2769
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002770 arg_idx = 1;
2771 for (i = 1; i < parmp->window_count; ++i)
2772 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002773 if (cwd != NULL)
2774 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002775 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2776 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002777 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002778 ++arg_idx;
2779 win_close(curwin, TRUE);
2780 advance = FALSE;
2781 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002782 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002783
Bram Moolenaar84212822006-11-07 21:59:47 +00002784 if (advance)
2785 {
2786 if (parmp->window_layout == WIN_TABS)
2787 {
2788 if (curtab->tp_next == NULL) /* just checking */
2789 break;
2790 goto_tabpage(0);
2791 }
2792 else
2793 {
2794 if (curwin->w_next == NULL) /* just checking */
2795 break;
2796 win_enter(curwin->w_next, FALSE);
2797 }
2798 }
2799 advance = TRUE;
2800
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002801 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002802 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002803 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2804 {
2805 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002806 /* Edit file from arg list, if there is one. When "Quit" selected
2807 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002808# ifdef HAS_SWAP_EXISTS_ACTION
2809 swap_exists_did_quit = FALSE;
2810# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002811 (void)do_ecmd(0, arg_idx < GARGCOUNT
2812 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002813 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002814# ifdef HAS_SWAP_EXISTS_ACTION
2815 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002816 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002817 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002818 if (got_int || only_one_window())
2819 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002820 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002821 did_emsg = FALSE; /* avoid hit-enter prompt */
2822 getout(1);
2823 }
2824 win_close(curwin, TRUE);
2825 advance = FALSE;
2826 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002827# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002828 if (arg_idx == GARGCOUNT - 1)
2829 arg_had_last = TRUE;
2830 ++arg_idx;
2831 }
2832 ui_breakcheck();
2833 if (got_int)
2834 {
2835 (void)vgetc(); /* only break the file loading, not the rest */
2836 break;
2837 }
2838 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002839
2840 if (parmp->window_layout == WIN_TABS)
2841 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002842# ifdef FEAT_AUTOCMD
2843 --autocmd_no_enter;
2844# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002845
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002846 /* make the first window the current window */
2847 win = firstwin;
2848#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2849 /* Avoid making a preview window the current window. */
2850 while (win->w_p_pvw)
2851 {
2852 win = win->w_next;
2853 if (win == NULL)
2854 {
2855 win = firstwin;
2856 break;
2857 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002858 }
2859#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002860 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002861
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002862# ifdef FEAT_AUTOCMD
2863 --autocmd_no_leave;
2864# endif
2865 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002866 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002867 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2868}
2869#endif /* FEAT_WINDOWS */
2870
2871/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002872 * Execute the commands from --cmd arguments "cmds[cnt]".
2873 */
2874 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002875exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002876{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002877 char_u **cmds = parmp->pre_commands;
2878 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002879 int i;
2880
2881 if (cnt > 0)
2882 {
2883 curwin->w_cursor.lnum = 0; /* just in case.. */
2884 sourcing_name = (char_u *)_("pre-vimrc command line");
2885# ifdef FEAT_EVAL
2886 current_SID = SID_CMDARG;
2887# endif
2888 for (i = 0; i < cnt; ++i)
2889 do_cmdline_cmd(cmds[i]);
2890 sourcing_name = NULL;
2891# ifdef FEAT_EVAL
2892 current_SID = 0;
2893# endif
2894 TIME_MSG("--cmd commands");
2895 }
2896}
2897
2898/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002899 * Execute "+", "-c" and "-S" arguments.
2900 */
2901 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002902exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002903{
2904 int i;
2905
2906 /*
2907 * We start commands on line 0, make "vim +/pat file" match a
2908 * pattern on line 1. But don't move the cursor when an autocommand
2909 * with g`" was used.
2910 */
2911 msg_scroll = TRUE;
2912 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2913 curwin->w_cursor.lnum = 0;
2914 sourcing_name = (char_u *)"command line";
2915#ifdef FEAT_EVAL
2916 current_SID = SID_CARG;
2917#endif
2918 for (i = 0; i < parmp->n_commands; ++i)
2919 {
2920 do_cmdline_cmd(parmp->commands[i]);
2921 if (parmp->cmds_tofree[i])
2922 vim_free(parmp->commands[i]);
2923 }
2924 sourcing_name = NULL;
2925#ifdef FEAT_EVAL
2926 current_SID = 0;
2927#endif
2928 if (curwin->w_cursor.lnum == 0)
2929 curwin->w_cursor.lnum = 1;
2930
2931 if (!exmode_active)
2932 msg_scroll = FALSE;
2933
2934#ifdef FEAT_QUICKFIX
2935 /* When started with "-q errorfile" jump to first error again. */
2936 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002937 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002938#endif
2939 TIME_MSG("executing command arguments");
2940}
2941
2942/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002943 * Source startup scripts.
2944 */
2945 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002946source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002947{
2948 int i;
2949
2950 /*
2951 * For "evim" source evim.vim first of all, so that the user can overrule
2952 * any things he doesn't like.
2953 */
2954 if (parmp->evim_mode)
2955 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002956 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002957 TIME_MSG("source evim file");
2958 }
2959
2960 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002961 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002962 * nothing else.
2963 */
2964 if (parmp->use_vimrc != NULL)
2965 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002966 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2967 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002968 {
2969#ifdef FEAT_GUI
2970 if (use_gvimrc == NULL) /* don't load gvimrc either */
2971 use_gvimrc = parmp->use_vimrc;
2972#endif
2973 if (parmp->use_vimrc[2] == 'N')
2974 p_lpl = FALSE; /* don't load plugins either */
2975 }
2976 else
2977 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002978 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002979 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2980 }
2981 }
2982 else if (!silent_mode)
2983 {
2984#ifdef AMIGA
2985 struct Process *proc = (struct Process *)FindTask(0L);
2986 APTR save_winptr = proc->pr_WindowPtr;
2987
2988 /* Avoid a requester here for a volume that doesn't exist. */
2989 proc->pr_WindowPtr = (APTR)-1L;
2990#endif
2991
2992 /*
2993 * Get system wide defaults, if the file name is defined.
2994 */
2995#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002996 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002997#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002998#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002999 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003000#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003001
3002 /*
3003 * Try to read initialization commands from the following places:
3004 * - environment variable VIMINIT
3005 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3006 * - second user vimrc file ($VIM/.vimrc for Dos)
3007 * - environment variable EXINIT
3008 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3009 * - second user exrc file ($VIM/.exrc for Dos)
3010 * The first that exists is used, the rest is ignored.
3011 */
3012 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3013 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003014 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003015#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003016 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3017 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003018#endif
3019#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003020 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3021 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003022#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003023#ifdef USR_VIMRC_FILE4
3024 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3025 DOSO_VIMRC) == FAIL
3026#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003027 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003028 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003029 {
3030#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003031 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003032#endif
3033 }
3034 }
3035
3036 /*
3037 * Read initialization commands from ".vimrc" or ".exrc" in current
3038 * directory. This is only done if the 'exrc' option is set.
3039 * Because of security reasons we disallow shell and write commands
3040 * now, except for unix if the file is owned by the user or 'secure'
3041 * option has been reset in environment of global ".exrc" or ".vimrc".
3042 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3043 * SYS_VIMRC_FILE.
3044 */
3045 if (p_exrc)
3046 {
3047#if defined(UNIX) || defined(VMS)
3048 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3049 if (!file_owned(VIMRC_FILE))
3050#endif
3051 secure = p_secure;
3052
3053 i = FAIL;
3054 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3055 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3056#ifdef USR_VIMRC_FILE2
3057 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3058 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3059#endif
3060#ifdef USR_VIMRC_FILE3
3061 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3062 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3063#endif
3064#ifdef SYS_VIMRC_FILE
3065 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3066 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3067#endif
3068 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003069 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003070
3071 if (i == FAIL)
3072 {
3073#if defined(UNIX) || defined(VMS)
3074 /* if ".exrc" is not owned by user set 'secure' mode */
3075 if (!file_owned(EXRC_FILE))
3076 secure = p_secure;
3077 else
3078 secure = 0;
3079#endif
3080 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3081 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3082#ifdef USR_EXRC_FILE2
3083 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3084 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3085#endif
3086 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003087 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003088 }
3089 }
3090 if (secure == 2)
3091 need_wait_return = TRUE;
3092 secure = 0;
3093#ifdef AMIGA
3094 proc->pr_WindowPtr = save_winptr;
3095#endif
3096 }
3097 TIME_MSG("sourcing vimrc file(s)");
3098}
3099
3100/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003101 * Setup to start using the GUI. Exit with an error when not available.
3102 */
3103 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003104main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003105{
3106#ifdef FEAT_GUI
3107 gui.starting = TRUE; /* start GUI a bit later */
3108#else
3109 mch_errmsg(_(e_nogvim));
3110 mch_errmsg("\n");
3111 mch_exit(2);
3112#endif
3113}
3114
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003115#endif /* NO_VIM_MAIN */
3116
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003117/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003118 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003119 * Returns FAIL if the environment variable was not executed, OK otherwise.
3120 */
3121 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003122process_env(
3123 char_u *env,
3124 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003125{
3126 char_u *initstr;
3127 char_u *save_sourcing_name;
3128 linenr_T save_sourcing_lnum;
3129#ifdef FEAT_EVAL
3130 scid_T save_sid;
3131#endif
3132
3133 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3134 {
3135 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003136 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003137 save_sourcing_name = sourcing_name;
3138 save_sourcing_lnum = sourcing_lnum;
3139 sourcing_name = env;
3140 sourcing_lnum = 0;
3141#ifdef FEAT_EVAL
3142 save_sid = current_SID;
3143 current_SID = SID_ENV;
3144#endif
3145 do_cmdline_cmd(initstr);
3146 sourcing_name = save_sourcing_name;
3147 sourcing_lnum = save_sourcing_lnum;
3148#ifdef FEAT_EVAL
3149 current_SID = save_sid;;
3150#endif
3151 return OK;
3152 }
3153 return FAIL;
3154}
3155
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003156#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003157/*
3158 * Return TRUE if we are certain the user owns the file "fname".
3159 * Used for ".vimrc" and ".exrc".
3160 * Use both stat() and lstat() for extra security.
3161 */
3162 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003163file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003164{
3165 struct stat s;
3166# ifdef UNIX
3167 uid_t uid = getuid();
3168# else /* VMS */
3169 uid_t uid = ((getgid() << 16) | getuid());
3170# endif
3171
3172 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3173# ifdef HAVE_LSTAT
3174 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3175# endif
3176 );
3177}
3178#endif
3179
3180/*
3181 * Give an error message main_errors["n"] and exit.
3182 */
3183 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003184mainerr(
3185 int n, /* one of the ME_ defines */
3186 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003187{
3188#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3189 reset_signals(); /* kill us with CTRL-C here, if you like */
3190#endif
3191
3192 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003193 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003194 mch_errmsg(_(main_errors[n]));
3195 if (str != NULL)
3196 {
3197 mch_errmsg(": \"");
3198 mch_errmsg((char *)str);
3199 mch_errmsg("\"");
3200 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003201 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003202
3203 mch_exit(1);
3204}
3205
3206 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003207mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003208{
3209 mainerr(ME_ARG_MISSING, str);
3210}
3211
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003212#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003213/*
3214 * print a message with three spaces prepended and '\n' appended.
3215 */
3216 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003217main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003218{
3219 mch_msg(" ");
3220 mch_msg(s);
3221 mch_msg("\n");
3222}
3223
3224/*
3225 * Print messages for "vim -h" or "vim --help" and exit.
3226 */
3227 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003228usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003229{
3230 int i;
3231 static char *(use[]) =
3232 {
3233 N_("[file ..] edit specified file(s)"),
3234 N_("- read text from stdin"),
3235 N_("-t tag edit file where tag is defined"),
3236#ifdef FEAT_QUICKFIX
3237 N_("-q [errorfile] edit file with first error")
3238#endif
3239 };
3240
3241#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3242 reset_signals(); /* kill us with CTRL-C here, if you like */
3243#endif
3244
3245 mch_msg(longVersion);
3246 mch_msg(_("\n\nusage:"));
3247 for (i = 0; ; ++i)
3248 {
3249 mch_msg(_(" vim [arguments] "));
3250 mch_msg(_(use[i]));
3251 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3252 break;
3253 mch_msg(_("\n or:"));
3254 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003255#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003256 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003257#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003258
3259 mch_msg(_("\n\nArguments:\n"));
3260 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003261#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003262 main_msg(_("--literal\t\tDon't expand wildcards"));
3263#endif
3264#ifdef FEAT_OLE
3265 main_msg(_("-register\t\tRegister this gvim for OLE"));
3266 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3267#endif
3268#ifdef FEAT_GUI
3269 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3270 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3271#endif
3272 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3273 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003274 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003275 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3276#ifdef FEAT_DIFF
3277 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3278#endif
3279 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3280 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3281 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3282 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3283 main_msg(_("-M\t\t\tModifications in text not allowed"));
3284 main_msg(_("-b\t\t\tBinary mode"));
3285#ifdef FEAT_LISP
3286 main_msg(_("-l\t\t\tLisp mode"));
3287#endif
3288 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3289 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003290 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3291#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003292 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003293#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003294 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3295 main_msg(_("-r\t\t\tList swap files and exit"));
3296 main_msg(_("-r (with file name)\tRecover crashed session"));
3297 main_msg(_("-L\t\t\tSame as -r"));
3298#ifdef AMIGA
3299 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3300 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3301#endif
3302#ifdef FEAT_ARABIC
3303 main_msg(_("-A\t\t\tstart in Arabic mode"));
3304#endif
3305#ifdef FEAT_RIGHTLEFT
3306 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3307#endif
3308#ifdef FEAT_FKMAP
3309 main_msg(_("-F\t\t\tStart in Farsi mode"));
3310#endif
3311 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3312 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3313#ifdef FEAT_GUI
3314 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3315#endif
3316 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003317#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003318 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003319 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3320 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003321#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003322 main_msg(_("+\t\t\tStart at end of file"));
3323 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003324 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003325 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3326 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3327 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3328 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3329 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3330#ifdef FEAT_CRYPT
3331 main_msg(_("-x\t\t\tEdit encrypted files"));
3332#endif
3333#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3334# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3335 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3336# endif
3337 main_msg(_("-X\t\t\tDo not connect to X server"));
3338#endif
3339#ifdef FEAT_CLIENTSERVER
3340 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3341 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3342 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3343 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003344# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003345 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003346# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003347 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3348 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3349 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3350 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3351#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003352#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003353 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003354#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003355#ifdef FEAT_VIMINFO
3356 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3357#endif
3358 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3359 main_msg(_("--version\t\tPrint version information and exit"));
3360
3361#ifdef FEAT_GUI_X11
3362# ifdef FEAT_GUI_MOTIF
3363 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3364# else
3365# ifdef FEAT_GUI_ATHENA
3366# ifdef FEAT_GUI_NEXTAW
3367 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3368# else
3369 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3370# endif
3371# endif
3372# endif
3373 main_msg(_("-display <display>\tRun vim on <display>"));
3374 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003375 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3376 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3377 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3378 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3379 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3380 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3381 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3382 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3383# ifdef FEAT_GUI_ATHENA
3384 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3385# endif
3386 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3387 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3388 main_msg(_("-xrm <resource>\tSet the specified resource"));
3389#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003390#ifdef FEAT_GUI_GTK
3391 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3392 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3393 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3394 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3395 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003396 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003397 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003398 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003399#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003400#ifdef FEAT_GUI_W32
3401 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003402 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003403#endif
3404
3405#ifdef FEAT_GUI_GNOME
3406 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3407 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003408 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003409 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003410 gui.dofork = FALSE;
3411 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003412 else
3413#endif
3414 mch_exit(0);
3415}
3416
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003417#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003418/*
3419 * Check the result of the ATTENTION dialog:
3420 * When "Quit" selected, exit Vim.
3421 * When "Recover" selected, recover the file.
3422 */
3423 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003424check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003425{
3426 if (swap_exists_action == SEA_QUIT)
3427 getout(1);
3428 handle_swap_exists(NULL);
3429}
3430#endif
3431
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003432#endif
3433
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003434#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003435static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003436
3437static struct timeval prev_timeval;
3438
Bram Moolenaar3f269672009-11-03 11:11:11 +00003439# ifdef WIN3264
3440/*
3441 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3442 */
3443 static int
3444gettimeofday(struct timeval *tv, char *dummy)
3445{
3446 long t = clock();
3447 tv->tv_sec = t / CLOCKS_PER_SEC;
3448 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3449 return 0;
3450}
3451# endif
3452
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003453/*
3454 * Save the previous time before doing something that could nest.
3455 * set "*tv_rel" to the time elapsed so far.
3456 */
3457 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003458time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003459{
3460 *((struct timeval *)tv_rel) = prev_timeval;
3461 gettimeofday(&prev_timeval, NULL);
3462 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3463 - ((struct timeval *)tv_rel)->tv_usec;
3464 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3465 - ((struct timeval *)tv_rel)->tv_sec;
3466 if (((struct timeval *)tv_rel)->tv_usec < 0)
3467 {
3468 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3469 --((struct timeval *)tv_rel)->tv_sec;
3470 }
3471 *(struct timeval *)tv_start = prev_timeval;
3472}
3473
3474/*
3475 * Compute the previous time after doing something that could nest.
3476 * Subtract "*tp" from prev_timeval;
3477 * Note: The arguments are (void *) to avoid trouble with systems that don't
3478 * have struct timeval.
3479 */
3480 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003481time_pop(
3482 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003483{
3484 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3485 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3486 if (prev_timeval.tv_usec < 0)
3487 {
3488 prev_timeval.tv_usec += 1000000;
3489 --prev_timeval.tv_sec;
3490 }
3491}
3492
3493 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003494time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003495{
3496 long usec;
3497 long msec;
3498
3499 usec = now->tv_usec - then->tv_usec;
3500 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3501 usec = usec % 1000L;
3502 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3503}
3504
3505 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003506time_msg(
3507 char *mesg,
3508 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003509 (struct timeval *) */
3510{
3511 static struct timeval start;
3512 struct timeval now;
3513
3514 if (time_fd != NULL)
3515 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003516 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003517 {
3518 gettimeofday(&start, NULL);
3519 prev_timeval = start;
3520 fprintf(time_fd, "\n\ntimes in msec\n");
3521 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3522 fprintf(time_fd, " clock elapsed: other lines\n\n");
3523 }
3524 gettimeofday(&now, NULL);
3525 time_diff(&start, &now);
3526 if (((struct timeval *)tv_start) != NULL)
3527 {
3528 fprintf(time_fd, " ");
3529 time_diff(((struct timeval *)tv_start), &now);
3530 }
3531 fprintf(time_fd, " ");
3532 time_diff(&prev_timeval, &now);
3533 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003534 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003535 }
3536}
3537
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003538#endif
3539
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003540#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003541
3542/*
3543 * Common code for the X command server and the Win32 command server.
3544 */
3545
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003546static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003547
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003548/*
3549 * Do the client-server stuff, unless "--servername ''" was used.
3550 */
3551 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003552exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003553{
3554 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3555 {
3556# ifdef WIN32
3557 /* Initialise the client/server messaging infrastructure. */
3558 serverInitMessaging();
3559# endif
3560
3561 /*
3562 * When a command server argument was found, execute it. This may
3563 * exit Vim when it was successful. Otherwise it's executed further
3564 * on. Remember the encoding used here in "serverStrEnc".
3565 */
3566 if (parmp->serverArg)
3567 {
3568 cmdsrv_main(&parmp->argc, parmp->argv,
3569 parmp->serverName_arg, &parmp->serverStr);
3570# ifdef FEAT_MBYTE
3571 parmp->serverStrEnc = vim_strsave(p_enc);
3572# endif
3573 }
3574
3575 /* If we're still running, get the name to register ourselves.
3576 * On Win32 can register right now, for X11 need to setup the
3577 * clipboard first, it's further down. */
3578 parmp->servername = serverMakeName(parmp->serverName_arg,
3579 parmp->argv[0]);
3580# ifdef WIN32
3581 if (parmp->servername != NULL)
3582 {
3583 serverSetName(parmp->servername);
3584 vim_free(parmp->servername);
3585 }
3586# endif
3587 }
3588}
3589
3590/*
3591 * Prepare for running as a Vim server.
3592 */
3593 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003594prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003595{
3596# if defined(FEAT_X11)
3597 /*
3598 * Register for remote command execution with :serversend and --remote
3599 * unless there was a -X or a --servername '' on the command line.
3600 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003601 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003602 */
3603 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3604# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003605 (gui.in_use
3606# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003607 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003608# endif
3609 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003610# endif
3611 parmp->serverName_arg != NULL))
3612 {
3613 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3614 vim_free(parmp->servername);
3615 TIME_MSG("register server name");
3616 }
3617 else
3618 serverDelayedStartName = parmp->servername;
3619# endif
3620
3621 /*
3622 * Execute command ourselves if we're here because the send failed (or
3623 * else we would have exited above).
3624 */
3625 if (parmp->serverStr != NULL)
3626 {
3627 char_u *p;
3628
3629 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3630 parmp->serverStr, &p));
3631 vim_free(p);
3632 }
3633}
3634
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003635 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003636cmdsrv_main(
3637 int *argc,
3638 char **argv,
3639 char_u *serverName_arg,
3640 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003641{
3642 char_u *res;
3643 int i;
3644 char_u *sname;
3645 int ret;
3646 int didone = FALSE;
3647 int exiterr = 0;
3648 char **newArgV = argv + 1;
3649 int newArgC = 1,
3650 Argc = *argc;
3651 int argtype;
3652#define ARGTYPE_OTHER 0
3653#define ARGTYPE_EDIT 1
3654#define ARGTYPE_EDIT_WAIT 2
3655#define ARGTYPE_SEND 3
3656 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003657 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003658# ifndef FEAT_X11
3659 HWND srv;
3660# else
3661 Window srv;
3662
3663 setup_term_clip();
3664# endif
3665
3666 sname = serverMakeName(serverName_arg, argv[0]);
3667 if (sname == NULL)
3668 return;
3669
3670 /*
3671 * Execute the command server related arguments and remove them
3672 * from the argc/argv array; We may have to return into main()
3673 */
3674 for (i = 1; i < Argc; i++)
3675 {
3676 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003677 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003678 {
3679 for (; i < *argc; i++)
3680 {
3681 *newArgV++ = argv[i];
3682 newArgC++;
3683 }
3684 break;
3685 }
3686
Bram Moolenaareb94e552006-03-11 21:35:11 +00003687 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003688 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003689 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3690 {
3691 char *p = argv[i] + 8;
3692
3693 argtype = ARGTYPE_EDIT;
3694 while (*p != NUL)
3695 {
3696 if (STRNICMP(p, "-wait", 5) == 0)
3697 {
3698 argtype = ARGTYPE_EDIT_WAIT;
3699 p += 5;
3700 }
3701 else if (STRNICMP(p, "-silent", 7) == 0)
3702 {
3703 silent = TRUE;
3704 p += 7;
3705 }
3706 else if (STRNICMP(p, "-tab", 4) == 0)
3707 {
3708 tabs = TRUE;
3709 p += 4;
3710 }
3711 else
3712 {
3713 argtype = ARGTYPE_OTHER;
3714 break;
3715 }
3716 }
3717 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003718 else
3719 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003720
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003721 if (argtype != ARGTYPE_OTHER)
3722 {
3723 if (i == *argc - 1)
3724 mainerr_arg_missing((char_u *)argv[i]);
3725 if (argtype == ARGTYPE_SEND)
3726 {
3727 *serverStr = (char_u *)argv[i + 1];
3728 i++;
3729 }
3730 else
3731 {
3732 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003733 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003734 if (*serverStr == NULL)
3735 {
3736 /* Probably out of memory, exit. */
3737 didone = TRUE;
3738 exiterr = 1;
3739 break;
3740 }
3741 Argc = i;
3742 }
3743# ifdef FEAT_X11
3744 if (xterm_dpy == NULL)
3745 {
3746 mch_errmsg(_("No display"));
3747 ret = -1;
3748 }
3749 else
3750 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3751 NULL, &srv, 0, 0, silent);
3752# else
3753 /* Win32 always works? */
3754 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3755# endif
3756 if (ret < 0)
3757 {
3758 if (argtype == ARGTYPE_SEND)
3759 {
3760 /* Failed to send, abort. */
3761 mch_errmsg(_(": Send failed.\n"));
3762 didone = TRUE;
3763 exiterr = 1;
3764 }
3765 else if (!silent)
3766 /* Let vim start normally. */
3767 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3768 break;
3769 }
3770
3771# ifdef FEAT_GUI_W32
3772 /* Guess that when the server name starts with "g" it's a GUI
3773 * server, which we can bring to the foreground here.
3774 * Foreground() in the server doesn't work very well. */
3775 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3776 SetForegroundWindow(srv);
3777# endif
3778
3779 /*
3780 * For --remote-wait: Wait until the server did edit each
3781 * file. Also detect that the server no longer runs.
3782 */
3783 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3784 {
3785 int numFiles = *argc - i - 1;
3786 int j;
3787 char_u *done = alloc(numFiles);
3788 char_u *p;
3789# ifdef FEAT_GUI_W32
3790 NOTIFYICONDATA ni;
3791 int count = 0;
3792 extern HWND message_window;
3793# endif
3794
3795 if (numFiles > 0 && argv[i + 1][0] == '+')
3796 /* Skip "+cmd" argument, don't wait for it to be edited. */
3797 --numFiles;
3798
3799# ifdef FEAT_GUI_W32
3800 ni.cbSize = sizeof(ni);
3801 ni.hWnd = message_window;
3802 ni.uID = 0;
3803 ni.uFlags = NIF_ICON|NIF_TIP;
3804 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3805 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3806 Shell_NotifyIcon(NIM_ADD, &ni);
3807# endif
3808
3809 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003810 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003811 while (memchr(done, 0, numFiles) != NULL)
3812 {
3813# ifdef WIN32
3814 p = serverGetReply(srv, NULL, TRUE, TRUE);
3815 if (p == NULL)
3816 break;
3817# else
3818 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3819 break;
3820# endif
3821 j = atoi((char *)p);
3822 if (j >= 0 && j < numFiles)
3823 {
3824# ifdef FEAT_GUI_W32
3825 ++count;
3826 sprintf(ni.szTip, _("%d of %d edited"),
3827 count, numFiles);
3828 Shell_NotifyIcon(NIM_MODIFY, &ni);
3829# endif
3830 done[j] = 1;
3831 }
3832 }
3833# ifdef FEAT_GUI_W32
3834 Shell_NotifyIcon(NIM_DELETE, &ni);
3835# endif
3836 }
3837 }
3838 else if (STRICMP(argv[i], "--remote-expr") == 0)
3839 {
3840 if (i == *argc - 1)
3841 mainerr_arg_missing((char_u *)argv[i]);
3842# ifdef WIN32
3843 /* Win32 always works? */
3844 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3845 &res, NULL, 1, FALSE) < 0)
3846# else
3847 if (xterm_dpy == NULL)
3848 mch_errmsg(_("No display: Send expression failed.\n"));
3849 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3850 &res, NULL, 1, 1, FALSE) < 0)
3851# endif
3852 {
3853 if (res != NULL && *res != NUL)
3854 {
3855 /* Output error from remote */
3856 mch_errmsg((char *)res);
3857 vim_free(res);
3858 res = NULL;
3859 }
3860 mch_errmsg(_(": Send expression failed.\n"));
3861 }
3862 }
3863 else if (STRICMP(argv[i], "--serverlist") == 0)
3864 {
3865# ifdef WIN32
3866 /* Win32 always works? */
3867 res = serverGetVimNames();
3868# else
3869 if (xterm_dpy != NULL)
3870 res = serverGetVimNames(xterm_dpy);
3871# endif
3872 if (called_emsg)
3873 mch_errmsg("\n");
3874 }
3875 else if (STRICMP(argv[i], "--servername") == 0)
3876 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003877 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003878 i++;
3879 continue;
3880 }
3881 else
3882 {
3883 *newArgV++ = argv[i];
3884 newArgC++;
3885 continue;
3886 }
3887 didone = TRUE;
3888 if (res != NULL && *res != NUL)
3889 {
3890 mch_msg((char *)res);
3891 if (res[STRLEN(res) - 1] != '\n')
3892 mch_msg("\n");
3893 }
3894 vim_free(res);
3895 }
3896
3897 if (didone)
3898 {
3899 display_errors(); /* display any collected messages */
3900 exit(exiterr); /* Mission accomplished - get out */
3901 }
3902
3903 /* Return back into main() */
3904 *argc = newArgC;
3905 vim_free(sname);
3906}
3907
3908/*
3909 * Build a ":drop" command to send to a Vim server.
3910 */
3911 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003912build_drop_cmd(
3913 int filec,
3914 char **filev,
3915 int tabs, /* Use ":tab drop" instead of ":drop". */
3916 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003917{
3918 garray_T ga;
3919 int i;
3920 char_u *inicmd = NULL;
3921 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003922 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003923 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003924
3925 if (filec > 0 && filev[0][0] == '+')
3926 {
3927 inicmd = (char_u *)filev[0] + 1;
3928 filev++;
3929 filec--;
3930 }
3931 /* Check if we have at least one argument. */
3932 if (filec <= 0)
3933 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003934
3935 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003936 cwd = alloc(MAXPATHL);
3937 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003938 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003939 if (mch_dirname(cwd, MAXPATHL) != OK)
3940 {
3941 vim_free(cwd);
3942 return NULL;
3943 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003944 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003945#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003946 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00003947#else
3948 PATH_ESC_CHARS,
3949#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003950 '\\', TRUE);
3951 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003952 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003953 return NULL;
3954 ga_init2(&ga, 1, 100);
3955 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003956 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003957
3958 /* Call inputsave() so that a prompt for an encryption key works. */
3959 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3960 if (tabs)
3961 ga_concat(&ga, (char_u *)"tab ");
3962 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003963 for (i = 0; i < filec; i++)
3964 {
3965 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003966 * do it again in the Vim server. On MS-Windows only escape
3967 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003968 p = vim_strsave_escaped((char_u *)filev[i],
3969#ifdef UNIX
3970 PATH_ESC_CHARS
3971#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003972 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003973#endif
3974 );
3975 if (p == NULL)
3976 {
3977 vim_free(ga.ga_data);
3978 return NULL;
3979 }
3980 ga_concat(&ga, (char_u *)" ");
3981 ga_concat(&ga, p);
3982 vim_free(p);
3983 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003984 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3985
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003986 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3987 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003988 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3989
3990 /* Switch back to the correct current directory (prior to temporary path
3991 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003992 * correct after the :drop command. With line breaks and spaces:
3993 * if !exists('+acd') || !&acd
3994 * if haslocaldir()
3995 * cd -
3996 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02003997 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003998 * cd -
3999 * endif
4000 * endif
4001 */
4002 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004003 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004004 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004005 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004006 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004007
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004008 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004009 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4010 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004011 if (inicmd != NULL)
4012 {
4013 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4014 * the following commands to be inserted as text. Use a "|",
4015 * hopefully "inicmd" does allow this... */
4016 ga_concat(&ga, inicmd);
4017 ga_concat(&ga, (char_u *)"|");
4018 }
4019 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4020 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004021 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004022 ga_append(&ga, NUL);
4023 return ga.ga_data;
4024}
4025
4026/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004027 * Make our basic server name: use the specified "arg" if given, otherwise use
4028 * the tail of the command "cmd" we were started with.
4029 * Return the name in allocated memory. This doesn't include a serial number.
4030 */
4031 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004032serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004033{
4034 char_u *p;
4035
4036 if (arg != NULL && *arg != NUL)
4037 p = vim_strsave_up(arg);
4038 else
4039 {
4040 p = vim_strsave_up(gettail((char_u *)cmd));
4041 /* Remove .exe or .bat from the name. */
4042 if (p != NULL && vim_strchr(p, '.') != NULL)
4043 *vim_strchr(p, '.') = NUL;
4044 }
4045 return p;
4046}
4047#endif /* FEAT_CLIENTSERVER */
4048
4049#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4050/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004051 * Replace termcodes such as <CR> and insert as key presses if there is room.
4052 */
4053 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004054server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004055{
4056 char_u *ptr = NULL;
4057 char_u *cpo_save = p_cpo;
4058
4059 /* Set 'cpoptions' the way we want it.
4060 * B set - backslashes are *not* treated specially
4061 * k set - keycodes are *not* reverse-engineered
4062 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004063 * The last but one parameter of replace_termcodes() is TRUE so that the
4064 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004065 */
4066 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004067 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004068 p_cpo = cpo_save;
4069
4070 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4071 {
4072 /*
4073 * Add the string to the input stream.
4074 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4075 *
4076 * First clear typed characters from the typeahead buffer, there could
4077 * be half a mapping there. Then append to the existing string, so
4078 * that multiple commands from a client are concatenated.
4079 */
4080 if (typebuf.tb_maplen < typebuf.tb_len)
4081 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4082 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4083
4084 /* Let input_available() know we inserted text in the typeahead
4085 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004086 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004087 }
4088 vim_free((char_u *)ptr);
4089}
4090
4091/*
4092 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004093 */
4094 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004095eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004096{
4097 char_u *res;
4098 int save_dbl = debug_break_level;
4099 int save_ro = redir_off;
4100
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004101 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4102 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004103 debug_break_level = -1;
4104 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004105 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4106 * to be typed. Do generate errors so that try/catch works. */
4107 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004108
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004109 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004110
4111 debug_break_level = save_dbl;
4112 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004113 --emsg_silent;
4114 if (emsg_silent < 0)
4115 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004116
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004117 /* A client can tell us to redraw, but not to display the cursor, so do
4118 * that here. */
4119 setcursor();
4120 out_flush();
4121#ifdef FEAT_GUI
4122 if (gui.in_use)
4123 gui_update_cursor(FALSE, FALSE);
4124#endif
4125
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004126 return res;
4127}
4128
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004129/*
4130 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4131 * return an allocated string. Otherwise return "data".
4132 * "*tofree" is set to the result when it needs to be freed later.
4133 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004134 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004135serverConvert(
4136 char_u *client_enc UNUSED,
4137 char_u *data,
4138 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004139{
4140 char_u *res = data;
4141
4142 *tofree = NULL;
4143# ifdef FEAT_MBYTE
4144 if (client_enc != NULL && p_enc != NULL)
4145 {
4146 vimconv_T vimconv;
4147
4148 vimconv.vc_type = CONV_NONE;
4149 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4150 && vimconv.vc_type != CONV_NONE)
4151 {
4152 res = string_convert(&vimconv, data, NULL);
4153 if (res == NULL)
4154 res = data;
4155 else
4156 *tofree = res;
4157 }
4158 convert_setup(&vimconv, NULL, NULL);
4159 }
4160# endif
4161 return res;
4162}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004163#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004164
4165/*
4166 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4167 */
4168#if defined(FEAT_FKMAP) || defined(PROTO)
4169# include "farsi.c"
4170#endif
4171
4172/*
4173 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4174 */
4175#if defined(FEAT_ARABIC) || defined(PROTO)
4176# include "arabic.c"
4177#endif