blob: e412641cc3a603b202c31ce3eb59e47c4398a5b4 [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 Moolenaar0ba04292010-07-14 23:23:17 +02001489#ifdef FEAT_LUA
1490 lua_end();
1491#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001492#ifdef FEAT_MZSCHEME
1493 mzscheme_end();
1494#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001495#ifdef FEAT_TCL
1496 tcl_end();
1497#endif
1498#ifdef FEAT_RUBY
1499 ruby_end();
1500#endif
1501#ifdef FEAT_PYTHON
1502 python_end();
1503#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001504#ifdef FEAT_PYTHON3
1505 python3_end();
1506#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001507#ifdef FEAT_PERL
1508 perl_end();
1509#endif
1510#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1511 iconv_end();
1512#endif
1513#ifdef FEAT_NETBEANS_INTG
1514 netbeans_end();
1515#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001516#ifdef FEAT_CSCOPE
1517 cs_end();
1518#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001519#ifdef FEAT_EVAL
1520 if (garbage_collect_at_exit)
1521 garbage_collect();
1522#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001523#if defined(WIN32) && defined(FEAT_MBYTE)
1524 free_cmd_argsW();
1525#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001526
1527 mch_exit(exitval);
1528}
1529
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001530#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001531/*
1532 * Get a (optional) count for a Vim argument.
1533 */
1534 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001535get_number_arg(
1536 char_u *p, /* pointer to argument */
1537 int *idx, /* index in argument, is incremented */
1538 int def) /* default value */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001539{
1540 if (vim_isdigit(p[*idx]))
1541 {
1542 def = atoi((char *)&(p[*idx]));
1543 while (vim_isdigit(p[*idx]))
1544 *idx = *idx + 1;
1545 }
1546 return def;
1547}
1548
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001549#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1550/*
1551 * Setup to use the current locale (for ctype() and many other things).
1552 */
1553 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001554init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001555{
1556 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001557
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001558# ifdef FEAT_GUI_GTK
1559 /* Tell Gtk not to change our locale settings. */
1560 gtk_disable_setlocale();
1561# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001562# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1563 /* Make sure strtod() uses a decimal point, not a comma. */
1564 setlocale(LC_NUMERIC, "C");
1565# endif
1566
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001567# ifdef WIN32
1568 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1569 * text while it's expecting text in the current locale. This call avoids
1570 * that. */
1571 setlocale(LC_CTYPE, "C");
1572# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001573
1574# ifdef FEAT_GETTEXT
1575 {
1576 int mustfree = FALSE;
1577 char_u *p;
1578
1579# ifdef DYNAMIC_GETTEXT
1580 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001581 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001582# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001583 /* expand_env() doesn't work yet, because g_chartab[] is not
1584 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001585 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1586 if (p != NULL && *p != NUL)
1587 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001588 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001589 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1590 }
1591 if (mustfree)
1592 vim_free(p);
1593 textdomain(VIMPACKAGE);
1594 }
1595# endif
1596}
1597#endif
1598
1599/*
1600 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1601 * If the executable name starts with "r" we disable shell commands.
1602 * If the next character is "e" we run in Easy mode.
1603 * If the next character is "g" we run the GUI version.
1604 * If the next characters are "view" we start in readonly mode.
1605 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1606 * If the next characters are "ex" we start in Ex mode. If it's followed
1607 * by "im" use improved Ex mode.
1608 */
1609 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001610parse_command_name(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001611{
1612 char_u *initstr;
1613
1614 initstr = gettail((char_u *)parmp->argv[0]);
1615
1616#ifdef MACOS_X_UNIX
1617 /* An issue has been seen when launching Vim in such a way that
1618 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1619 * executable or a symbolic link of it. Until this issue is resolved
1620 * we prohibit the GUI from being used.
1621 */
1622 if (STRCMP(initstr, parmp->argv[0]) == 0)
1623 disallow_gui = TRUE;
1624
1625 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001626 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001627#endif
1628
1629#ifdef FEAT_EVAL
1630 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaara1706c92014-04-01 19:55:49 +02001631 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001632#endif
1633
1634 if (TOLOWER_ASC(initstr[0]) == 'r')
1635 {
1636 restricted = TRUE;
1637 ++initstr;
1638 }
1639
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001640 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001641 if (TOLOWER_ASC(initstr[0]) == 'e'
1642 && (TOLOWER_ASC(initstr[1]) == 'v'
1643 || TOLOWER_ASC(initstr[1]) == 'g'))
1644 {
1645#ifdef FEAT_GUI
1646 gui.starting = TRUE;
1647#endif
1648 parmp->evim_mode = TRUE;
1649 ++initstr;
1650 }
1651
Bram Moolenaar806875d2008-09-18 18:57:10 +00001652 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1653 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001654 {
1655 main_start_gui();
1656#ifdef FEAT_GUI
1657 ++initstr;
1658#endif
1659 }
1660
1661 if (STRNICMP(initstr, "view", 4) == 0)
1662 {
1663 readonlymode = TRUE;
1664 curbuf->b_p_ro = TRUE;
1665 p_uc = 10000; /* don't update very often */
1666 initstr += 4;
1667 }
1668 else if (STRNICMP(initstr, "vim", 3) == 0)
1669 initstr += 3;
1670
1671 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1672 if (STRICMP(initstr, "diff") == 0)
1673 {
1674#ifdef FEAT_DIFF
1675 parmp->diff_mode = TRUE;
1676#else
1677 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1678 mch_errmsg("\n");
1679 mch_exit(2);
1680#endif
1681 }
1682
1683 if (STRNICMP(initstr, "ex", 2) == 0)
1684 {
1685 if (STRNICMP(initstr + 2, "im", 2) == 0)
1686 exmode_active = EXMODE_VIM;
1687 else
1688 exmode_active = EXMODE_NORMAL;
1689 change_compatible(TRUE); /* set 'compatible' */
1690 }
1691}
1692
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001693/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001694 * Get the name of the display, before gui_prepare() removes it from
1695 * argv[]. Used for the xterm-clipboard display.
1696 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001697 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001698 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001699 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001700early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001701{
Bram Moolenaar03005972008-11-20 13:12:36 +00001702#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1703 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001704 int argc = parmp->argc;
1705 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001706 int i;
1707
1708 for (i = 1; i < argc; i++)
1709 {
1710 if (STRCMP(argv[i], "--") == 0)
1711 break;
1712# ifdef FEAT_XCLIPBOARD
1713 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001714# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001715 || STRICMP(argv[i], "--display") == 0
1716# endif
1717 )
1718 {
1719 if (i == argc - 1)
1720 mainerr_arg_missing((char_u *)argv[i]);
1721 xterm_display = argv[++i];
1722 }
1723# endif
1724# ifdef FEAT_CLIENTSERVER
1725 else if (STRICMP(argv[i], "--servername") == 0)
1726 {
1727 if (i == argc - 1)
1728 mainerr_arg_missing((char_u *)argv[i]);
1729 parmp->serverName_arg = (char_u *)argv[++i];
1730 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001731 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001732 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001733 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001734 {
1735 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001736# ifdef FEAT_GUI
1737 if (strstr(argv[i], "-wait") != 0)
1738 /* don't fork() when starting the GUI to edit files ourself */
1739 gui.dofork = FALSE;
1740# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001741 }
1742# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001743
1744# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1745# ifdef FEAT_GUI_W32
1746 else if (STRICMP(argv[i], "--windowid") == 0)
1747# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001748 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001749# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001750 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001751 long_u id;
1752 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001753
1754 if (i == argc - 1)
1755 mainerr_arg_missing((char_u *)argv[i]);
1756 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001757 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001758 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001759 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001760 if (count != 1)
1761 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1762 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001763# ifdef FEAT_GUI_W32
1764 win_socket_id = id;
1765# else
1766 gtk_socket_id = id;
1767# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001768 i++;
1769 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001770# endif
1771# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001772 else if (STRICMP(argv[i], "--echo-wid") == 0)
1773 echo_wid_arg = TRUE;
1774# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001775# ifndef FEAT_NETBEANS_INTG
1776 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001777 {
1778 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1779 mch_exit(2);
1780 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001781# endif
1782
Bram Moolenaar58d98232005-07-23 22:25:46 +00001783 }
1784#endif
1785}
1786
1787/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001788 * Scan the command line arguments.
1789 */
1790 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001791command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001792{
1793 int argc = parmp->argc;
1794 char **argv = parmp->argv;
1795 int argv_idx; /* index in argv[n][] */
1796 int had_minmin = FALSE; /* found "--" argument */
1797 int want_argument; /* option argument with argument */
1798 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001799 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001800 long n;
1801
1802 --argc;
1803 ++argv;
1804 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1805 while (argc > 0)
1806 {
1807 /*
1808 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1809 */
1810 if (argv[0][0] == '+' && !had_minmin)
1811 {
1812 if (parmp->n_commands >= MAX_ARG_CMDS)
1813 mainerr(ME_EXTRA_CMD, NULL);
1814 argv_idx = -1; /* skip to next argument */
1815 if (argv[0][1] == NUL)
1816 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1817 else
1818 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1819 }
1820
1821 /*
1822 * Optional argument.
1823 */
1824 else if (argv[0][0] == '-' && !had_minmin)
1825 {
1826 want_argument = FALSE;
1827 c = argv[0][argv_idx++];
1828#ifdef VMS
1829 /*
1830 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1831 * and "-/X" as "-X".
1832 */
1833 if (c == '/')
1834 {
1835 c = argv[0][argv_idx++];
1836 c = TOUPPER_ASC(c);
1837 }
1838 else
1839 c = TOLOWER_ASC(c);
1840#endif
1841 switch (c)
1842 {
1843 case NUL: /* "vim -" read from stdin */
1844 /* "ex -" silent mode */
1845 if (exmode_active)
1846 silent_mode = TRUE;
1847 else
1848 {
1849 if (parmp->edit_type != EDIT_NONE)
1850 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1851 parmp->edit_type = EDIT_STDIN;
1852 read_cmd_fd = 2; /* read from stderr instead of stdin */
1853 }
1854 argv_idx = -1; /* skip to next argument */
1855 break;
1856
1857 case '-': /* "--" don't take any more option arguments */
1858 /* "--help" give help message */
1859 /* "--version" give version message */
1860 /* "--literal" take files literally */
1861 /* "--nofork" don't fork */
1862 /* "--noplugin[s]" skip plugins */
1863 /* "--cmd <cmd>" execute cmd before vimrc */
1864 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1865 usage();
1866 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1867 {
1868 Columns = 80; /* need to init Columns */
1869 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1870 list_version();
1871 msg_putchar('\n');
1872 msg_didout = FALSE;
1873 mch_exit(0);
1874 }
1875 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1876 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001877#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001878 parmp->literal = TRUE;
1879#endif
1880 }
1881 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1882 {
1883#ifdef FEAT_GUI
1884 gui.dofork = FALSE; /* don't fork() when starting GUI */
1885#endif
1886 }
1887 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1888 p_lpl = FALSE;
1889 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1890 {
1891 want_argument = TRUE;
1892 argv_idx += 3;
1893 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001894 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1895 {
1896 want_argument = TRUE;
1897 argv_idx += 11;
1898 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001899#ifdef FEAT_CLIENTSERVER
1900 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1901 ; /* already processed -- no arg */
1902 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1903 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1904 {
1905 /* already processed -- snatch the following arg */
1906 if (argc > 1)
1907 {
1908 --argc;
1909 ++argv;
1910 }
1911 }
1912#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001913#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1914# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001915 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001916# else
1917 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1918# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001919 {
1920 /* already processed -- snatch the following arg */
1921 if (argc > 1)
1922 {
1923 --argc;
1924 ++argv;
1925 }
1926 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001927#endif
1928#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001929 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1930 {
1931 /* already processed, skip */
1932 }
1933#endif
1934 else
1935 {
1936 if (argv[0][argv_idx])
1937 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1938 had_minmin = TRUE;
1939 }
1940 if (!want_argument)
1941 argv_idx = -1; /* skip to next argument */
1942 break;
1943
1944 case 'A': /* "-A" start in Arabic mode */
1945#ifdef FEAT_ARABIC
1946 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1947#else
1948 mch_errmsg(_(e_noarabic));
1949 mch_exit(2);
1950#endif
1951 break;
1952
1953 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001954 /* Needs to be effective before expanding file names, because
1955 * for Win32 this makes us edit a shortcut file itself,
1956 * instead of the file it links to. */
1957 set_options_bin(curbuf->b_p_bin, 1, 0);
1958 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001959 break;
1960
1961 case 'C': /* "-C" Compatible */
1962 change_compatible(TRUE);
1963 break;
1964
1965 case 'e': /* "-e" Ex mode */
1966 exmode_active = EXMODE_NORMAL;
1967 break;
1968
1969 case 'E': /* "-E" Improved Ex mode */
1970 exmode_active = EXMODE_VIM;
1971 break;
1972
1973 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1974 window directly, not with newcli */
1975#ifdef FEAT_GUI
1976 gui.dofork = FALSE; /* don't fork() when starting GUI */
1977#endif
1978 break;
1979
1980 case 'g': /* "-g" start GUI */
1981 main_start_gui();
1982 break;
1983
1984 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1985#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001986 p_fkmap = TRUE;
1987 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001988#else
1989 mch_errmsg(_(e_nofarsi));
1990 mch_exit(2);
1991#endif
1992 break;
1993
1994 case 'h': /* "-h" give help message */
1995#ifdef FEAT_GUI_GNOME
1996 /* Tell usage() to exit for "gvim". */
1997 gui.starting = FALSE;
1998#endif
1999 usage();
2000 break;
2001
2002 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2003#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002004 p_hkmap = TRUE;
2005 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002006#else
2007 mch_errmsg(_(e_nohebrew));
2008 mch_exit(2);
2009#endif
2010 break;
2011
2012 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2013#ifdef FEAT_LISP
2014 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2015 p_sm = TRUE;
2016#endif
2017 break;
2018
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002019 case 'M': /* "-M" no changes or writing of files */
2020 reset_modifiable();
2021 /* FALLTHROUGH */
2022
2023 case 'm': /* "-m" no writing of files */
2024 p_write = FALSE;
2025 break;
2026
2027 case 'y': /* "-y" easy mode */
2028#ifdef FEAT_GUI
2029 gui.starting = TRUE; /* start GUI a bit later */
2030#endif
2031 parmp->evim_mode = TRUE;
2032 break;
2033
2034 case 'N': /* "-N" Nocompatible */
2035 change_compatible(FALSE);
2036 break;
2037
2038 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002039#ifdef FEAT_NETBEANS_INTG
2040 /* checking for "-nb", netbeans parameters */
2041 if (argv[0][argv_idx] == 'b')
2042 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002043 netbeansArg = argv[0];
2044 argv_idx = -1; /* skip to next argument */
2045 }
2046 else
2047#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002048 parmp->no_swap_file = TRUE;
2049 break;
2050
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002051 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002052#ifdef TARGET_API_MAC_OSX
2053 /* For some reason on MacOS X, an argument like:
2054 -psn_0_10223617 is passed in when invoke from Finder
2055 or with the 'open' command */
2056 if (argv[0][argv_idx] == 's')
2057 {
2058 argv_idx = -1; /* bypass full -psn */
2059 main_start_gui();
2060 break;
2061 }
2062#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002063#ifdef FEAT_WINDOWS
2064 /* default is 0: open window for each file */
2065 parmp->window_count = get_number_arg((char_u *)argv[0],
2066 &argv_idx, 0);
2067 parmp->window_layout = WIN_TABS;
2068#endif
2069 break;
2070
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002071 case 'o': /* "-o[N]" open N horizontal split windows */
2072#ifdef FEAT_WINDOWS
2073 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002074 parmp->window_count = get_number_arg((char_u *)argv[0],
2075 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002076 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002077#endif
2078 break;
2079
2080 case 'O': /* "-O[N]" open N vertical split windows */
2081#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2082 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002083 parmp->window_count = get_number_arg((char_u *)argv[0],
2084 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002085 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002086#endif
2087 break;
2088
2089#ifdef FEAT_QUICKFIX
2090 case 'q': /* "-q" QuickFix mode */
2091 if (parmp->edit_type != EDIT_NONE)
2092 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2093 parmp->edit_type = EDIT_QF;
2094 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2095 {
2096 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2097 argv_idx = -1;
2098 }
2099 else if (argc > 1) /* "-q {errorfile}" */
2100 want_argument = TRUE;
2101 break;
2102#endif
2103
2104 case 'R': /* "-R" readonly mode */
2105 readonlymode = TRUE;
2106 curbuf->b_p_ro = TRUE;
2107 p_uc = 10000; /* don't update very often */
2108 break;
2109
2110 case 'r': /* "-r" recovery mode */
2111 case 'L': /* "-L" recovery mode */
2112 recoverymode = 1;
2113 break;
2114
2115 case 's':
2116 if (exmode_active) /* "-s" silent (batch) mode */
2117 silent_mode = TRUE;
2118 else /* "-s {scriptin}" read from script file */
2119 want_argument = TRUE;
2120 break;
2121
2122 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2123 if (parmp->edit_type != EDIT_NONE)
2124 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2125 parmp->edit_type = EDIT_TAG;
2126 if (argv[0][argv_idx]) /* "-t{tag}" */
2127 {
2128 parmp->tagname = (char_u *)argv[0] + argv_idx;
2129 argv_idx = -1;
2130 }
2131 else /* "-t {tag}" */
2132 want_argument = TRUE;
2133 break;
2134
2135#ifdef FEAT_EVAL
2136 case 'D': /* "-D" Debugging */
2137 parmp->use_debug_break_level = 9999;
2138 break;
2139#endif
2140#ifdef FEAT_DIFF
2141 case 'd': /* "-d" 'diff' */
2142# ifdef AMIGA
2143 /* check for "-dev {device}" */
2144 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2145 want_argument = TRUE;
2146 else
2147# endif
2148 parmp->diff_mode = TRUE;
2149 break;
2150#endif
2151 case 'V': /* "-V{N}" Verbose level */
2152 /* default is 10: a little bit verbose */
2153 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2154 if (argv[0][argv_idx] != NUL)
2155 {
2156 set_option_value((char_u *)"verbosefile", 0L,
2157 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002158 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002159 }
2160 break;
2161
2162 case 'v': /* "-v" Vi-mode (as if called "vi") */
2163 exmode_active = 0;
2164#ifdef FEAT_GUI
2165 gui.starting = FALSE; /* don't start GUI */
2166#endif
2167 break;
2168
2169 case 'w': /* "-w{number}" set window height */
2170 /* "-w {scriptout}" write to script */
2171 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2172 {
2173 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2174 set_option_value((char_u *)"window", n, NULL, 0);
2175 break;
2176 }
2177 want_argument = TRUE;
2178 break;
2179
2180#ifdef FEAT_CRYPT
2181 case 'x': /* "-x" encrypted reading/writing of files */
2182 parmp->ask_for_key = TRUE;
2183 break;
2184#endif
2185
2186 case 'X': /* "-X" don't connect to X server */
2187#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2188 x_no_connect = TRUE;
2189#endif
2190 break;
2191
2192 case 'Z': /* "-Z" restricted mode */
2193 restricted = TRUE;
2194 break;
2195
2196 case 'c': /* "-c{command}" or "-c {command}" execute
2197 command */
2198 if (argv[0][argv_idx] != NUL)
2199 {
2200 if (parmp->n_commands >= MAX_ARG_CMDS)
2201 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002202 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2203 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002204 argv_idx = -1;
2205 break;
2206 }
2207 /*FALLTHROUGH*/
2208 case 'S': /* "-S {file}" execute Vim script */
2209 case 'i': /* "-i {viminfo}" use for viminfo */
2210#ifndef FEAT_DIFF
2211 case 'd': /* "-d {device}" device (for Amiga) */
2212#endif
2213 case 'T': /* "-T {terminal}" terminal name */
2214 case 'u': /* "-u {vimrc}" vim inits file */
2215 case 'U': /* "-U {gvimrc}" gvim inits file */
2216 case 'W': /* "-W {scriptout}" overwrite */
2217#ifdef FEAT_GUI_W32
2218 case 'P': /* "-P {parent title}" MDI parent */
2219#endif
2220 want_argument = TRUE;
2221 break;
2222
2223 default:
2224 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2225 }
2226
2227 /*
2228 * Handle option arguments with argument.
2229 */
2230 if (want_argument)
2231 {
2232 /*
2233 * Check for garbage immediately after the option letter.
2234 */
2235 if (argv[0][argv_idx] != NUL)
2236 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2237
2238 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002239 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002240 mainerr_arg_missing((char_u *)argv[0]);
2241 ++argv;
2242 argv_idx = -1;
2243
2244 switch (c)
2245 {
2246 case 'c': /* "-c {command}" execute command */
2247 case 'S': /* "-S {file}" execute Vim script */
2248 if (parmp->n_commands >= MAX_ARG_CMDS)
2249 mainerr(ME_EXTRA_CMD, NULL);
2250 if (c == 'S')
2251 {
2252 char *a;
2253
2254 if (argc < 1)
2255 /* "-S" without argument: use default session file
2256 * name. */
2257 a = SESSION_FILE;
2258 else if (argv[0][0] == '-')
2259 {
2260 /* "-S" followed by another option: use default
2261 * session file name. */
2262 a = SESSION_FILE;
2263 ++argc;
2264 --argv;
2265 }
2266 else
2267 a = argv[0];
2268 p = alloc((unsigned)(STRLEN(a) + 4));
2269 if (p == NULL)
2270 mch_exit(2);
2271 sprintf((char *)p, "so %s", a);
2272 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2273 parmp->commands[parmp->n_commands++] = p;
2274 }
2275 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002276 parmp->commands[parmp->n_commands++] =
2277 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002278 break;
2279
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002280 case '-':
2281 if (argv[-1][2] == 'c')
2282 {
2283 /* "--cmd {command}" execute command */
2284 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2285 mainerr(ME_EXTRA_CMD, NULL);
2286 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002287 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002288 }
2289 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002290 break;
2291
2292 /* case 'd': -d {device} is handled in mch_check_win() for the
2293 * Amiga */
2294
2295#ifdef FEAT_QUICKFIX
2296 case 'q': /* "-q {errorfile}" QuickFix mode */
2297 parmp->use_ef = (char_u *)argv[0];
2298 break;
2299#endif
2300
2301 case 'i': /* "-i {viminfo}" use for viminfo */
2302 use_viminfo = (char_u *)argv[0];
2303 break;
2304
2305 case 's': /* "-s {scriptin}" read from script file */
2306 if (scriptin[0] != NULL)
2307 {
2308scripterror:
2309 mch_errmsg(_("Attempt to open script file again: \""));
2310 mch_errmsg(argv[-1]);
2311 mch_errmsg(" ");
2312 mch_errmsg(argv[0]);
2313 mch_errmsg("\"\n");
2314 mch_exit(2);
2315 }
2316 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2317 {
2318 mch_errmsg(_("Cannot open for reading: \""));
2319 mch_errmsg(argv[0]);
2320 mch_errmsg("\"\n");
2321 mch_exit(2);
2322 }
2323 if (save_typebuf() == FAIL)
2324 mch_exit(2); /* out of memory */
2325 break;
2326
2327 case 't': /* "-t {tag}" */
2328 parmp->tagname = (char_u *)argv[0];
2329 break;
2330
2331 case 'T': /* "-T {terminal}" terminal name */
2332 /*
2333 * The -T term argument is always available and when
2334 * HAVE_TERMLIB is supported it overrides the environment
2335 * variable TERM.
2336 */
2337#ifdef FEAT_GUI
2338 if (term_is_gui((char_u *)argv[0]))
2339 gui.starting = TRUE; /* start GUI a bit later */
2340 else
2341#endif
2342 parmp->term = (char_u *)argv[0];
2343 break;
2344
2345 case 'u': /* "-u {vimrc}" vim inits file */
2346 parmp->use_vimrc = (char_u *)argv[0];
2347 break;
2348
2349 case 'U': /* "-U {gvimrc}" gvim inits file */
2350#ifdef FEAT_GUI
2351 use_gvimrc = (char_u *)argv[0];
2352#endif
2353 break;
2354
2355 case 'w': /* "-w {nr}" 'window' value */
2356 /* "-w {scriptout}" append to script file */
2357 if (vim_isdigit(*((char_u *)argv[0])))
2358 {
2359 argv_idx = 0;
2360 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2361 set_option_value((char_u *)"window", n, NULL, 0);
2362 argv_idx = -1;
2363 break;
2364 }
2365 /*FALLTHROUGH*/
2366 case 'W': /* "-W {scriptout}" overwrite script file */
2367 if (scriptout != NULL)
2368 goto scripterror;
2369 if ((scriptout = mch_fopen(argv[0],
2370 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2371 {
2372 mch_errmsg(_("Cannot open for script output: \""));
2373 mch_errmsg(argv[0]);
2374 mch_errmsg("\"\n");
2375 mch_exit(2);
2376 }
2377 break;
2378
2379#ifdef FEAT_GUI_W32
2380 case 'P': /* "-P {parent title}" MDI parent */
2381 gui_mch_set_parent(argv[0]);
2382 break;
2383#endif
2384 }
2385 }
2386 }
2387
2388 /*
2389 * File name argument.
2390 */
2391 else
2392 {
2393 argv_idx = -1; /* skip to next argument */
2394
2395 /* Check for only one type of editing. */
2396 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2397 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2398 parmp->edit_type = EDIT_FILE;
2399
2400#ifdef MSWIN
2401 /* Remember if the argument was a full path before changing
2402 * slashes to backslashes. */
2403 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2404 parmp->full_path = TRUE;
2405#endif
2406
2407 /* Add the file to the global argument list. */
2408 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2409 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2410 mch_exit(2);
2411#ifdef FEAT_DIFF
2412 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2413 && !mch_isdir(alist_name(&GARGLIST[0])))
2414 {
2415 char_u *r;
2416
2417 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2418 if (r != NULL)
2419 {
2420 vim_free(p);
2421 p = r;
2422 }
2423 }
2424#endif
2425#if defined(__CYGWIN32__) && !defined(WIN32)
2426 /*
2427 * If vim is invoked by non-Cygwin tools, convert away any
2428 * DOS paths, so things like .swp files are created correctly.
2429 * Look for evidence of non-Cygwin paths before we bother.
2430 * This is only for when using the Unix files.
2431 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002432 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002433 {
2434 char posix_path[PATH_MAX];
2435
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002436# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2437 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2438# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002439 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002440# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002441 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002442 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002443 if (p == NULL)
2444 mch_exit(2);
2445 }
2446#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002447
2448#ifdef USE_FNAME_CASE
2449 /* Make the case of the file name match the actual file. */
2450 fname_case(p, 0);
2451#endif
2452
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002453 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002454#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002455 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002456#else
2457 2 /* add buffer number now and use curbuf */
2458#endif
2459 );
2460
2461#if defined(FEAT_MBYTE) && defined(WIN32)
2462 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002463 /* Remember this argument has been added to the argument list.
2464 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002465 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002466# ifdef FEAT_DIFF
2467 parmp->diff_mode
2468# else
2469 FALSE
2470# endif
2471 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002472 }
2473#endif
2474 }
2475
2476 /*
2477 * If there are no more letters after the current "-", go to next
2478 * argument. argv_idx is set to -1 when the current argument is to be
2479 * skipped.
2480 */
2481 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2482 {
2483 --argc;
2484 ++argv;
2485 argv_idx = 1;
2486 }
2487 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002488
2489#ifdef FEAT_EVAL
2490 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2491 * one. */
2492 if (parmp->n_commands > 0)
2493 {
2494 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2495 if (p != NULL)
2496 {
2497 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2498 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2499 vim_free(p);
2500 }
2501 }
2502#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002503}
2504
2505/*
2506 * Print a warning if stdout is not a terminal.
2507 * When starting in Ex mode and commands come from a file, set Silent mode.
2508 */
2509 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002510check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002511{
2512 int input_isatty; /* is active input a terminal? */
2513
2514 input_isatty = mch_input_isatty();
2515 if (exmode_active)
2516 {
2517 if (!input_isatty)
2518 silent_mode = TRUE;
2519 }
2520 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2521#ifdef FEAT_GUI
2522 /* don't want the delay when started from the desktop */
2523 && !gui.starting
2524#endif
2525 )
2526 {
2527#ifdef NBDEBUG
2528 /*
2529 * This shouldn't be necessary. But if I run netbeans with the log
2530 * output coming to the console and XOpenDisplay fails, I get vim
2531 * trying to start with input/output to my console tty. This fills my
2532 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002533 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002534 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002535 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002536 {
2537 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2538 exit(1);
2539 }
2540#endif
2541 if (!parmp->stdout_isatty)
2542 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2543 if (!input_isatty)
2544 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2545 out_flush();
2546 if (scriptin[0] == NULL)
2547 ui_delay(2000L, TRUE);
2548 TIME_MSG("Warning delay");
2549 }
2550}
2551
2552/*
2553 * Read text from stdin.
2554 */
2555 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002556read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002557{
2558 int i;
2559
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002560#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002561 /* When getting the ATTENTION prompt here, use a dialog */
2562 swap_exists_action = SEA_DIALOG;
2563#endif
2564 no_wait_return = TRUE;
2565 i = msg_didany;
2566 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002567 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002568 no_wait_return = FALSE;
2569 msg_didany = i;
2570 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002571#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002572 check_swap_exists_action();
2573#endif
2574#if !(defined(AMIGA) || defined(MACOS))
2575 /*
2576 * Close stdin and dup it from stderr. Required for GPM to work
2577 * properly, and for running external commands.
2578 * Is there any other system that cannot do this?
2579 */
2580 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002581 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002582#endif
2583}
2584
2585/*
2586 * Create the requested number of windows and edit buffers in them.
2587 * Also does recovery if "recoverymode" set.
2588 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002589 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002590create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002591{
2592#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002593 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002594 int done = 0;
2595
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002596 /*
2597 * Create the number of windows that was requested.
2598 */
2599 if (parmp->window_count == -1) /* was not set */
2600 parmp->window_count = 1;
2601 if (parmp->window_count == 0)
2602 parmp->window_count = GARGCOUNT;
2603 if (parmp->window_count > 1)
2604 {
2605 /* Don't change the windows if there was a command in .vimrc that
2606 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002607 if (parmp->window_layout == 0)
2608 parmp->window_layout = WIN_HOR;
2609 if (parmp->window_layout == WIN_TABS)
2610 {
2611 parmp->window_count = make_tabpages(parmp->window_count);
2612 TIME_MSG("making tab pages");
2613 }
2614 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002615 {
2616 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002617 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002618 TIME_MSG("making windows");
2619 }
2620 else
2621 parmp->window_count = win_count();
2622 }
2623 else
2624 parmp->window_count = 1;
2625#endif
2626
2627 if (recoverymode) /* do recover */
2628 {
2629 msg_scroll = TRUE; /* scroll message up */
2630 ml_recover();
2631 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2632 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002633 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002634 }
2635 else
2636 {
2637 /*
2638 * Open a buffer for windows that don't have one yet.
2639 * Commands in the .vimrc might have loaded a file or split the window.
2640 * Watch out for autocommands that delete a window.
2641 */
2642#ifdef FEAT_AUTOCMD
2643 /*
2644 * Don't execute Win/Buf Enter/Leave autocommands here
2645 */
2646 ++autocmd_no_enter;
2647 ++autocmd_no_leave;
2648#endif
2649#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002650 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002651 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002652 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002653 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002654 {
2655 if (parmp->window_layout == WIN_TABS)
2656 goto_tabpage(1);
2657 else
2658 curwin = firstwin;
2659 }
2660 else if (parmp->window_layout == WIN_TABS)
2661 {
2662 if (curtab->tp_next == NULL)
2663 break;
2664 goto_tabpage(0);
2665 }
2666 else
2667 {
2668 if (curwin->w_next == NULL)
2669 break;
2670 curwin = curwin->w_next;
2671 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002672 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002673#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002674 curbuf = curwin->w_buffer;
2675 if (curbuf->b_ml.ml_mfp == NULL)
2676 {
2677#ifdef FEAT_FOLDING
2678 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2679 if (p_fdls >= 0)
2680 curwin->w_p_fdl = p_fdls;
2681#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002682#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002683 /* When getting the ATTENTION prompt here, use a dialog */
2684 swap_exists_action = SEA_DIALOG;
2685#endif
2686 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002687
2688 /* create memfile, read file */
2689 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002690
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002691#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002692 if (swap_exists_action == SEA_QUIT)
2693 {
2694 if (got_int || only_one_window())
2695 {
2696 /* abort selected or quit and only one window */
2697 did_emsg = FALSE; /* avoid hit-enter prompt */
2698 getout(1);
2699 }
2700 /* We can't close the window, it would disturb what
2701 * happens next. Clear the file name and set the arg
2702 * index to -1 to delete it later. */
2703 setfname(curbuf, NULL, NULL, FALSE);
2704 curwin->w_arg_idx = -1;
2705 swap_exists_action = SEA_NONE;
2706 }
2707 else
2708 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002709#endif
2710#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002711 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002712#endif
2713 }
2714#ifdef FEAT_WINDOWS
2715 ui_breakcheck();
2716 if (got_int)
2717 {
2718 (void)vgetc(); /* only break the file loading, not the rest */
2719 break;
2720 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002721 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002722#endif
2723#ifdef FEAT_WINDOWS
2724 if (parmp->window_layout == WIN_TABS)
2725 goto_tabpage(1);
2726 else
2727 curwin = firstwin;
2728 curbuf = curwin->w_buffer;
2729#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002730#ifdef FEAT_AUTOCMD
2731 --autocmd_no_enter;
2732 --autocmd_no_leave;
2733#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002734 }
2735}
2736
2737#ifdef FEAT_WINDOWS
2738 /*
2739 * If opened more than one window, start editing files in the other
2740 * windows. make_windows() has already opened the windows.
2741 */
2742 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002743edit_buffers(
2744 mparm_T *parmp,
2745 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002746{
2747 int arg_idx; /* index in argument list */
2748 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002749 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002750 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002751
2752# ifdef FEAT_AUTOCMD
2753 /*
2754 * Don't execute Win/Buf Enter/Leave autocommands here
2755 */
2756 ++autocmd_no_enter;
2757 ++autocmd_no_leave;
2758# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002759
2760 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2761 if (curwin->w_arg_idx == -1)
2762 {
2763 win_close(curwin, TRUE);
2764 advance = FALSE;
2765 }
2766
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002767 arg_idx = 1;
2768 for (i = 1; i < parmp->window_count; ++i)
2769 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002770 if (cwd != NULL)
2771 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002772 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2773 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002774 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002775 ++arg_idx;
2776 win_close(curwin, TRUE);
2777 advance = FALSE;
2778 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002779 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002780
Bram Moolenaar84212822006-11-07 21:59:47 +00002781 if (advance)
2782 {
2783 if (parmp->window_layout == WIN_TABS)
2784 {
2785 if (curtab->tp_next == NULL) /* just checking */
2786 break;
2787 goto_tabpage(0);
2788 }
2789 else
2790 {
2791 if (curwin->w_next == NULL) /* just checking */
2792 break;
2793 win_enter(curwin->w_next, FALSE);
2794 }
2795 }
2796 advance = TRUE;
2797
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002798 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002799 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002800 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2801 {
2802 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002803 /* Edit file from arg list, if there is one. When "Quit" selected
2804 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002805# ifdef HAS_SWAP_EXISTS_ACTION
2806 swap_exists_did_quit = FALSE;
2807# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002808 (void)do_ecmd(0, arg_idx < GARGCOUNT
2809 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002810 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002811# ifdef HAS_SWAP_EXISTS_ACTION
2812 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002813 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002814 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002815 if (got_int || only_one_window())
2816 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002817 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002818 did_emsg = FALSE; /* avoid hit-enter prompt */
2819 getout(1);
2820 }
2821 win_close(curwin, TRUE);
2822 advance = FALSE;
2823 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002824# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002825 if (arg_idx == GARGCOUNT - 1)
2826 arg_had_last = TRUE;
2827 ++arg_idx;
2828 }
2829 ui_breakcheck();
2830 if (got_int)
2831 {
2832 (void)vgetc(); /* only break the file loading, not the rest */
2833 break;
2834 }
2835 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002836
2837 if (parmp->window_layout == WIN_TABS)
2838 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002839# ifdef FEAT_AUTOCMD
2840 --autocmd_no_enter;
2841# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002842
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002843 /* make the first window the current window */
2844 win = firstwin;
2845#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2846 /* Avoid making a preview window the current window. */
2847 while (win->w_p_pvw)
2848 {
2849 win = win->w_next;
2850 if (win == NULL)
2851 {
2852 win = firstwin;
2853 break;
2854 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002855 }
2856#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002857 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002858
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002859# ifdef FEAT_AUTOCMD
2860 --autocmd_no_leave;
2861# endif
2862 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002863 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002864 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2865}
2866#endif /* FEAT_WINDOWS */
2867
2868/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002869 * Execute the commands from --cmd arguments "cmds[cnt]".
2870 */
2871 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002872exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002873{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002874 char_u **cmds = parmp->pre_commands;
2875 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002876 int i;
2877
2878 if (cnt > 0)
2879 {
2880 curwin->w_cursor.lnum = 0; /* just in case.. */
2881 sourcing_name = (char_u *)_("pre-vimrc command line");
2882# ifdef FEAT_EVAL
2883 current_SID = SID_CMDARG;
2884# endif
2885 for (i = 0; i < cnt; ++i)
2886 do_cmdline_cmd(cmds[i]);
2887 sourcing_name = NULL;
2888# ifdef FEAT_EVAL
2889 current_SID = 0;
2890# endif
2891 TIME_MSG("--cmd commands");
2892 }
2893}
2894
2895/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002896 * Execute "+", "-c" and "-S" arguments.
2897 */
2898 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002899exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002900{
2901 int i;
2902
2903 /*
2904 * We start commands on line 0, make "vim +/pat file" match a
2905 * pattern on line 1. But don't move the cursor when an autocommand
2906 * with g`" was used.
2907 */
2908 msg_scroll = TRUE;
2909 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2910 curwin->w_cursor.lnum = 0;
2911 sourcing_name = (char_u *)"command line";
2912#ifdef FEAT_EVAL
2913 current_SID = SID_CARG;
2914#endif
2915 for (i = 0; i < parmp->n_commands; ++i)
2916 {
2917 do_cmdline_cmd(parmp->commands[i]);
2918 if (parmp->cmds_tofree[i])
2919 vim_free(parmp->commands[i]);
2920 }
2921 sourcing_name = NULL;
2922#ifdef FEAT_EVAL
2923 current_SID = 0;
2924#endif
2925 if (curwin->w_cursor.lnum == 0)
2926 curwin->w_cursor.lnum = 1;
2927
2928 if (!exmode_active)
2929 msg_scroll = FALSE;
2930
2931#ifdef FEAT_QUICKFIX
2932 /* When started with "-q errorfile" jump to first error again. */
2933 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002934 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002935#endif
2936 TIME_MSG("executing command arguments");
2937}
2938
2939/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002940 * Source startup scripts.
2941 */
2942 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002943source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002944{
2945 int i;
2946
2947 /*
2948 * For "evim" source evim.vim first of all, so that the user can overrule
2949 * any things he doesn't like.
2950 */
2951 if (parmp->evim_mode)
2952 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002953 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002954 TIME_MSG("source evim file");
2955 }
2956
2957 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002958 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002959 * nothing else.
2960 */
2961 if (parmp->use_vimrc != NULL)
2962 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002963 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2964 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002965 {
2966#ifdef FEAT_GUI
2967 if (use_gvimrc == NULL) /* don't load gvimrc either */
2968 use_gvimrc = parmp->use_vimrc;
2969#endif
2970 if (parmp->use_vimrc[2] == 'N')
2971 p_lpl = FALSE; /* don't load plugins either */
2972 }
2973 else
2974 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002975 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002976 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2977 }
2978 }
2979 else if (!silent_mode)
2980 {
2981#ifdef AMIGA
2982 struct Process *proc = (struct Process *)FindTask(0L);
2983 APTR save_winptr = proc->pr_WindowPtr;
2984
2985 /* Avoid a requester here for a volume that doesn't exist. */
2986 proc->pr_WindowPtr = (APTR)-1L;
2987#endif
2988
2989 /*
2990 * Get system wide defaults, if the file name is defined.
2991 */
2992#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002993 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002994#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002995#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002996 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002997#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002998
2999 /*
3000 * Try to read initialization commands from the following places:
3001 * - environment variable VIMINIT
3002 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3003 * - second user vimrc file ($VIM/.vimrc for Dos)
3004 * - environment variable EXINIT
3005 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3006 * - second user exrc file ($VIM/.exrc for Dos)
3007 * The first that exists is used, the rest is ignored.
3008 */
3009 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3010 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003011 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003012#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003013 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3014 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003015#endif
3016#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003017 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3018 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003019#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003020#ifdef USR_VIMRC_FILE4
3021 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3022 DOSO_VIMRC) == FAIL
3023#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003024 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003025 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003026 {
3027#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003028 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003029#endif
3030 }
3031 }
3032
3033 /*
3034 * Read initialization commands from ".vimrc" or ".exrc" in current
3035 * directory. This is only done if the 'exrc' option is set.
3036 * Because of security reasons we disallow shell and write commands
3037 * now, except for unix if the file is owned by the user or 'secure'
3038 * option has been reset in environment of global ".exrc" or ".vimrc".
3039 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3040 * SYS_VIMRC_FILE.
3041 */
3042 if (p_exrc)
3043 {
3044#if defined(UNIX) || defined(VMS)
3045 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3046 if (!file_owned(VIMRC_FILE))
3047#endif
3048 secure = p_secure;
3049
3050 i = FAIL;
3051 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3052 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3053#ifdef USR_VIMRC_FILE2
3054 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3055 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3056#endif
3057#ifdef USR_VIMRC_FILE3
3058 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3059 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3060#endif
3061#ifdef SYS_VIMRC_FILE
3062 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3063 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3064#endif
3065 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003066 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003067
3068 if (i == FAIL)
3069 {
3070#if defined(UNIX) || defined(VMS)
3071 /* if ".exrc" is not owned by user set 'secure' mode */
3072 if (!file_owned(EXRC_FILE))
3073 secure = p_secure;
3074 else
3075 secure = 0;
3076#endif
3077 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3078 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3079#ifdef USR_EXRC_FILE2
3080 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3081 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3082#endif
3083 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003084 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003085 }
3086 }
3087 if (secure == 2)
3088 need_wait_return = TRUE;
3089 secure = 0;
3090#ifdef AMIGA
3091 proc->pr_WindowPtr = save_winptr;
3092#endif
3093 }
3094 TIME_MSG("sourcing vimrc file(s)");
3095}
3096
3097/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003098 * Setup to start using the GUI. Exit with an error when not available.
3099 */
3100 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003101main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003102{
3103#ifdef FEAT_GUI
3104 gui.starting = TRUE; /* start GUI a bit later */
3105#else
3106 mch_errmsg(_(e_nogvim));
3107 mch_errmsg("\n");
3108 mch_exit(2);
3109#endif
3110}
3111
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003112#endif /* NO_VIM_MAIN */
3113
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003114/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003115 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003116 * Returns FAIL if the environment variable was not executed, OK otherwise.
3117 */
3118 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003119process_env(
3120 char_u *env,
3121 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003122{
3123 char_u *initstr;
3124 char_u *save_sourcing_name;
3125 linenr_T save_sourcing_lnum;
3126#ifdef FEAT_EVAL
3127 scid_T save_sid;
3128#endif
3129
3130 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3131 {
3132 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003133 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003134 save_sourcing_name = sourcing_name;
3135 save_sourcing_lnum = sourcing_lnum;
3136 sourcing_name = env;
3137 sourcing_lnum = 0;
3138#ifdef FEAT_EVAL
3139 save_sid = current_SID;
3140 current_SID = SID_ENV;
3141#endif
3142 do_cmdline_cmd(initstr);
3143 sourcing_name = save_sourcing_name;
3144 sourcing_lnum = save_sourcing_lnum;
3145#ifdef FEAT_EVAL
3146 current_SID = save_sid;;
3147#endif
3148 return OK;
3149 }
3150 return FAIL;
3151}
3152
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003153#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003154/*
3155 * Return TRUE if we are certain the user owns the file "fname".
3156 * Used for ".vimrc" and ".exrc".
3157 * Use both stat() and lstat() for extra security.
3158 */
3159 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003160file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003161{
3162 struct stat s;
3163# ifdef UNIX
3164 uid_t uid = getuid();
3165# else /* VMS */
3166 uid_t uid = ((getgid() << 16) | getuid());
3167# endif
3168
3169 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3170# ifdef HAVE_LSTAT
3171 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3172# endif
3173 );
3174}
3175#endif
3176
3177/*
3178 * Give an error message main_errors["n"] and exit.
3179 */
3180 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003181mainerr(
3182 int n, /* one of the ME_ defines */
3183 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003184{
3185#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3186 reset_signals(); /* kill us with CTRL-C here, if you like */
3187#endif
3188
3189 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003190 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003191 mch_errmsg(_(main_errors[n]));
3192 if (str != NULL)
3193 {
3194 mch_errmsg(": \"");
3195 mch_errmsg((char *)str);
3196 mch_errmsg("\"");
3197 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003198 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003199
3200 mch_exit(1);
3201}
3202
3203 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003204mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003205{
3206 mainerr(ME_ARG_MISSING, str);
3207}
3208
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003209#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003210/*
3211 * print a message with three spaces prepended and '\n' appended.
3212 */
3213 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003214main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003215{
3216 mch_msg(" ");
3217 mch_msg(s);
3218 mch_msg("\n");
3219}
3220
3221/*
3222 * Print messages for "vim -h" or "vim --help" and exit.
3223 */
3224 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003225usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003226{
3227 int i;
3228 static char *(use[]) =
3229 {
3230 N_("[file ..] edit specified file(s)"),
3231 N_("- read text from stdin"),
3232 N_("-t tag edit file where tag is defined"),
3233#ifdef FEAT_QUICKFIX
3234 N_("-q [errorfile] edit file with first error")
3235#endif
3236 };
3237
3238#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3239 reset_signals(); /* kill us with CTRL-C here, if you like */
3240#endif
3241
3242 mch_msg(longVersion);
3243 mch_msg(_("\n\nusage:"));
3244 for (i = 0; ; ++i)
3245 {
3246 mch_msg(_(" vim [arguments] "));
3247 mch_msg(_(use[i]));
3248 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3249 break;
3250 mch_msg(_("\n or:"));
3251 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003252#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003253 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003254#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003255
3256 mch_msg(_("\n\nArguments:\n"));
3257 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003258#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003259 main_msg(_("--literal\t\tDon't expand wildcards"));
3260#endif
3261#ifdef FEAT_OLE
3262 main_msg(_("-register\t\tRegister this gvim for OLE"));
3263 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3264#endif
3265#ifdef FEAT_GUI
3266 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3267 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3268#endif
3269 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3270 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003271 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003272 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3273#ifdef FEAT_DIFF
3274 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3275#endif
3276 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3277 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3278 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3279 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3280 main_msg(_("-M\t\t\tModifications in text not allowed"));
3281 main_msg(_("-b\t\t\tBinary mode"));
3282#ifdef FEAT_LISP
3283 main_msg(_("-l\t\t\tLisp mode"));
3284#endif
3285 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3286 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003287 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3288#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003289 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003290#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003291 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3292 main_msg(_("-r\t\t\tList swap files and exit"));
3293 main_msg(_("-r (with file name)\tRecover crashed session"));
3294 main_msg(_("-L\t\t\tSame as -r"));
3295#ifdef AMIGA
3296 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3297 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3298#endif
3299#ifdef FEAT_ARABIC
3300 main_msg(_("-A\t\t\tstart in Arabic mode"));
3301#endif
3302#ifdef FEAT_RIGHTLEFT
3303 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3304#endif
3305#ifdef FEAT_FKMAP
3306 main_msg(_("-F\t\t\tStart in Farsi mode"));
3307#endif
3308 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3309 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3310#ifdef FEAT_GUI
3311 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3312#endif
3313 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003314#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003315 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003316 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3317 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003318#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003319 main_msg(_("+\t\t\tStart at end of file"));
3320 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003321 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003322 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3323 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3324 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3325 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3326 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3327#ifdef FEAT_CRYPT
3328 main_msg(_("-x\t\t\tEdit encrypted files"));
3329#endif
3330#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3331# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3332 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3333# endif
3334 main_msg(_("-X\t\t\tDo not connect to X server"));
3335#endif
3336#ifdef FEAT_CLIENTSERVER
3337 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3338 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3339 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3340 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003341# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003342 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003343# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003344 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3345 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3346 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3347 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3348#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003349#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003350 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003351#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003352#ifdef FEAT_VIMINFO
3353 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3354#endif
3355 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3356 main_msg(_("--version\t\tPrint version information and exit"));
3357
3358#ifdef FEAT_GUI_X11
3359# ifdef FEAT_GUI_MOTIF
3360 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3361# else
3362# ifdef FEAT_GUI_ATHENA
3363# ifdef FEAT_GUI_NEXTAW
3364 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3365# else
3366 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3367# endif
3368# endif
3369# endif
3370 main_msg(_("-display <display>\tRun vim on <display>"));
3371 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003372 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3373 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3374 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3375 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3376 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3377 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3378 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3379 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3380# ifdef FEAT_GUI_ATHENA
3381 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3382# endif
3383 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3384 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3385 main_msg(_("-xrm <resource>\tSet the specified resource"));
3386#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003387#ifdef FEAT_GUI_GTK
3388 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3389 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3390 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3391 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3392 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003393 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003394 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003395 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003396#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003397#ifdef FEAT_GUI_W32
3398 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003399 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003400#endif
3401
3402#ifdef FEAT_GUI_GNOME
3403 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3404 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003405 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003406 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003407 gui.dofork = FALSE;
3408 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003409 else
3410#endif
3411 mch_exit(0);
3412}
3413
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003414#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003415/*
3416 * Check the result of the ATTENTION dialog:
3417 * When "Quit" selected, exit Vim.
3418 * When "Recover" selected, recover the file.
3419 */
3420 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003421check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003422{
3423 if (swap_exists_action == SEA_QUIT)
3424 getout(1);
3425 handle_swap_exists(NULL);
3426}
3427#endif
3428
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003429#endif
3430
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003431#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003432static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003433
3434static struct timeval prev_timeval;
3435
Bram Moolenaar3f269672009-11-03 11:11:11 +00003436# ifdef WIN3264
3437/*
3438 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3439 */
3440 static int
3441gettimeofday(struct timeval *tv, char *dummy)
3442{
3443 long t = clock();
3444 tv->tv_sec = t / CLOCKS_PER_SEC;
3445 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3446 return 0;
3447}
3448# endif
3449
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003450/*
3451 * Save the previous time before doing something that could nest.
3452 * set "*tv_rel" to the time elapsed so far.
3453 */
3454 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003455time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003456{
3457 *((struct timeval *)tv_rel) = prev_timeval;
3458 gettimeofday(&prev_timeval, NULL);
3459 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3460 - ((struct timeval *)tv_rel)->tv_usec;
3461 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3462 - ((struct timeval *)tv_rel)->tv_sec;
3463 if (((struct timeval *)tv_rel)->tv_usec < 0)
3464 {
3465 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3466 --((struct timeval *)tv_rel)->tv_sec;
3467 }
3468 *(struct timeval *)tv_start = prev_timeval;
3469}
3470
3471/*
3472 * Compute the previous time after doing something that could nest.
3473 * Subtract "*tp" from prev_timeval;
3474 * Note: The arguments are (void *) to avoid trouble with systems that don't
3475 * have struct timeval.
3476 */
3477 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003478time_pop(
3479 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003480{
3481 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3482 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3483 if (prev_timeval.tv_usec < 0)
3484 {
3485 prev_timeval.tv_usec += 1000000;
3486 --prev_timeval.tv_sec;
3487 }
3488}
3489
3490 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003491time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003492{
3493 long usec;
3494 long msec;
3495
3496 usec = now->tv_usec - then->tv_usec;
3497 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3498 usec = usec % 1000L;
3499 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3500}
3501
3502 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003503time_msg(
3504 char *mesg,
3505 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003506 (struct timeval *) */
3507{
3508 static struct timeval start;
3509 struct timeval now;
3510
3511 if (time_fd != NULL)
3512 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003513 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003514 {
3515 gettimeofday(&start, NULL);
3516 prev_timeval = start;
3517 fprintf(time_fd, "\n\ntimes in msec\n");
3518 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3519 fprintf(time_fd, " clock elapsed: other lines\n\n");
3520 }
3521 gettimeofday(&now, NULL);
3522 time_diff(&start, &now);
3523 if (((struct timeval *)tv_start) != NULL)
3524 {
3525 fprintf(time_fd, " ");
3526 time_diff(((struct timeval *)tv_start), &now);
3527 }
3528 fprintf(time_fd, " ");
3529 time_diff(&prev_timeval, &now);
3530 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003531 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003532 }
3533}
3534
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003535#endif
3536
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003537#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003538
3539/*
3540 * Common code for the X command server and the Win32 command server.
3541 */
3542
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003543static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003544
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003545/*
3546 * Do the client-server stuff, unless "--servername ''" was used.
3547 */
3548 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003549exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003550{
3551 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3552 {
3553# ifdef WIN32
3554 /* Initialise the client/server messaging infrastructure. */
3555 serverInitMessaging();
3556# endif
3557
3558 /*
3559 * When a command server argument was found, execute it. This may
3560 * exit Vim when it was successful. Otherwise it's executed further
3561 * on. Remember the encoding used here in "serverStrEnc".
3562 */
3563 if (parmp->serverArg)
3564 {
3565 cmdsrv_main(&parmp->argc, parmp->argv,
3566 parmp->serverName_arg, &parmp->serverStr);
3567# ifdef FEAT_MBYTE
3568 parmp->serverStrEnc = vim_strsave(p_enc);
3569# endif
3570 }
3571
3572 /* If we're still running, get the name to register ourselves.
3573 * On Win32 can register right now, for X11 need to setup the
3574 * clipboard first, it's further down. */
3575 parmp->servername = serverMakeName(parmp->serverName_arg,
3576 parmp->argv[0]);
3577# ifdef WIN32
3578 if (parmp->servername != NULL)
3579 {
3580 serverSetName(parmp->servername);
3581 vim_free(parmp->servername);
3582 }
3583# endif
3584 }
3585}
3586
3587/*
3588 * Prepare for running as a Vim server.
3589 */
3590 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003591prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003592{
3593# if defined(FEAT_X11)
3594 /*
3595 * Register for remote command execution with :serversend and --remote
3596 * unless there was a -X or a --servername '' on the command line.
3597 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003598 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003599 */
3600 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3601# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003602 (gui.in_use
3603# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003604 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003605# endif
3606 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003607# endif
3608 parmp->serverName_arg != NULL))
3609 {
3610 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3611 vim_free(parmp->servername);
3612 TIME_MSG("register server name");
3613 }
3614 else
3615 serverDelayedStartName = parmp->servername;
3616# endif
3617
3618 /*
3619 * Execute command ourselves if we're here because the send failed (or
3620 * else we would have exited above).
3621 */
3622 if (parmp->serverStr != NULL)
3623 {
3624 char_u *p;
3625
3626 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3627 parmp->serverStr, &p));
3628 vim_free(p);
3629 }
3630}
3631
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003632 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003633cmdsrv_main(
3634 int *argc,
3635 char **argv,
3636 char_u *serverName_arg,
3637 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003638{
3639 char_u *res;
3640 int i;
3641 char_u *sname;
3642 int ret;
3643 int didone = FALSE;
3644 int exiterr = 0;
3645 char **newArgV = argv + 1;
3646 int newArgC = 1,
3647 Argc = *argc;
3648 int argtype;
3649#define ARGTYPE_OTHER 0
3650#define ARGTYPE_EDIT 1
3651#define ARGTYPE_EDIT_WAIT 2
3652#define ARGTYPE_SEND 3
3653 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003654 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003655# ifndef FEAT_X11
3656 HWND srv;
3657# else
3658 Window srv;
3659
3660 setup_term_clip();
3661# endif
3662
3663 sname = serverMakeName(serverName_arg, argv[0]);
3664 if (sname == NULL)
3665 return;
3666
3667 /*
3668 * Execute the command server related arguments and remove them
3669 * from the argc/argv array; We may have to return into main()
3670 */
3671 for (i = 1; i < Argc; i++)
3672 {
3673 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003674 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003675 {
3676 for (; i < *argc; i++)
3677 {
3678 *newArgV++ = argv[i];
3679 newArgC++;
3680 }
3681 break;
3682 }
3683
Bram Moolenaareb94e552006-03-11 21:35:11 +00003684 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003685 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003686 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3687 {
3688 char *p = argv[i] + 8;
3689
3690 argtype = ARGTYPE_EDIT;
3691 while (*p != NUL)
3692 {
3693 if (STRNICMP(p, "-wait", 5) == 0)
3694 {
3695 argtype = ARGTYPE_EDIT_WAIT;
3696 p += 5;
3697 }
3698 else if (STRNICMP(p, "-silent", 7) == 0)
3699 {
3700 silent = TRUE;
3701 p += 7;
3702 }
3703 else if (STRNICMP(p, "-tab", 4) == 0)
3704 {
3705 tabs = TRUE;
3706 p += 4;
3707 }
3708 else
3709 {
3710 argtype = ARGTYPE_OTHER;
3711 break;
3712 }
3713 }
3714 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003715 else
3716 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003717
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003718 if (argtype != ARGTYPE_OTHER)
3719 {
3720 if (i == *argc - 1)
3721 mainerr_arg_missing((char_u *)argv[i]);
3722 if (argtype == ARGTYPE_SEND)
3723 {
3724 *serverStr = (char_u *)argv[i + 1];
3725 i++;
3726 }
3727 else
3728 {
3729 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003730 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003731 if (*serverStr == NULL)
3732 {
3733 /* Probably out of memory, exit. */
3734 didone = TRUE;
3735 exiterr = 1;
3736 break;
3737 }
3738 Argc = i;
3739 }
3740# ifdef FEAT_X11
3741 if (xterm_dpy == NULL)
3742 {
3743 mch_errmsg(_("No display"));
3744 ret = -1;
3745 }
3746 else
3747 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3748 NULL, &srv, 0, 0, silent);
3749# else
3750 /* Win32 always works? */
3751 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3752# endif
3753 if (ret < 0)
3754 {
3755 if (argtype == ARGTYPE_SEND)
3756 {
3757 /* Failed to send, abort. */
3758 mch_errmsg(_(": Send failed.\n"));
3759 didone = TRUE;
3760 exiterr = 1;
3761 }
3762 else if (!silent)
3763 /* Let vim start normally. */
3764 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3765 break;
3766 }
3767
3768# ifdef FEAT_GUI_W32
3769 /* Guess that when the server name starts with "g" it's a GUI
3770 * server, which we can bring to the foreground here.
3771 * Foreground() in the server doesn't work very well. */
3772 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3773 SetForegroundWindow(srv);
3774# endif
3775
3776 /*
3777 * For --remote-wait: Wait until the server did edit each
3778 * file. Also detect that the server no longer runs.
3779 */
3780 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3781 {
3782 int numFiles = *argc - i - 1;
3783 int j;
3784 char_u *done = alloc(numFiles);
3785 char_u *p;
3786# ifdef FEAT_GUI_W32
3787 NOTIFYICONDATA ni;
3788 int count = 0;
3789 extern HWND message_window;
3790# endif
3791
3792 if (numFiles > 0 && argv[i + 1][0] == '+')
3793 /* Skip "+cmd" argument, don't wait for it to be edited. */
3794 --numFiles;
3795
3796# ifdef FEAT_GUI_W32
3797 ni.cbSize = sizeof(ni);
3798 ni.hWnd = message_window;
3799 ni.uID = 0;
3800 ni.uFlags = NIF_ICON|NIF_TIP;
3801 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3802 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3803 Shell_NotifyIcon(NIM_ADD, &ni);
3804# endif
3805
3806 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003807 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003808 while (memchr(done, 0, numFiles) != NULL)
3809 {
3810# ifdef WIN32
3811 p = serverGetReply(srv, NULL, TRUE, TRUE);
3812 if (p == NULL)
3813 break;
3814# else
3815 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3816 break;
3817# endif
3818 j = atoi((char *)p);
3819 if (j >= 0 && j < numFiles)
3820 {
3821# ifdef FEAT_GUI_W32
3822 ++count;
3823 sprintf(ni.szTip, _("%d of %d edited"),
3824 count, numFiles);
3825 Shell_NotifyIcon(NIM_MODIFY, &ni);
3826# endif
3827 done[j] = 1;
3828 }
3829 }
3830# ifdef FEAT_GUI_W32
3831 Shell_NotifyIcon(NIM_DELETE, &ni);
3832# endif
3833 }
3834 }
3835 else if (STRICMP(argv[i], "--remote-expr") == 0)
3836 {
3837 if (i == *argc - 1)
3838 mainerr_arg_missing((char_u *)argv[i]);
3839# ifdef WIN32
3840 /* Win32 always works? */
3841 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3842 &res, NULL, 1, FALSE) < 0)
3843# else
3844 if (xterm_dpy == NULL)
3845 mch_errmsg(_("No display: Send expression failed.\n"));
3846 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3847 &res, NULL, 1, 1, FALSE) < 0)
3848# endif
3849 {
3850 if (res != NULL && *res != NUL)
3851 {
3852 /* Output error from remote */
3853 mch_errmsg((char *)res);
3854 vim_free(res);
3855 res = NULL;
3856 }
3857 mch_errmsg(_(": Send expression failed.\n"));
3858 }
3859 }
3860 else if (STRICMP(argv[i], "--serverlist") == 0)
3861 {
3862# ifdef WIN32
3863 /* Win32 always works? */
3864 res = serverGetVimNames();
3865# else
3866 if (xterm_dpy != NULL)
3867 res = serverGetVimNames(xterm_dpy);
3868# endif
3869 if (called_emsg)
3870 mch_errmsg("\n");
3871 }
3872 else if (STRICMP(argv[i], "--servername") == 0)
3873 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003874 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003875 i++;
3876 continue;
3877 }
3878 else
3879 {
3880 *newArgV++ = argv[i];
3881 newArgC++;
3882 continue;
3883 }
3884 didone = TRUE;
3885 if (res != NULL && *res != NUL)
3886 {
3887 mch_msg((char *)res);
3888 if (res[STRLEN(res) - 1] != '\n')
3889 mch_msg("\n");
3890 }
3891 vim_free(res);
3892 }
3893
3894 if (didone)
3895 {
3896 display_errors(); /* display any collected messages */
3897 exit(exiterr); /* Mission accomplished - get out */
3898 }
3899
3900 /* Return back into main() */
3901 *argc = newArgC;
3902 vim_free(sname);
3903}
3904
3905/*
3906 * Build a ":drop" command to send to a Vim server.
3907 */
3908 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003909build_drop_cmd(
3910 int filec,
3911 char **filev,
3912 int tabs, /* Use ":tab drop" instead of ":drop". */
3913 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003914{
3915 garray_T ga;
3916 int i;
3917 char_u *inicmd = NULL;
3918 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003919 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003920 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003921
3922 if (filec > 0 && filev[0][0] == '+')
3923 {
3924 inicmd = (char_u *)filev[0] + 1;
3925 filev++;
3926 filec--;
3927 }
3928 /* Check if we have at least one argument. */
3929 if (filec <= 0)
3930 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003931
3932 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003933 cwd = alloc(MAXPATHL);
3934 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003935 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003936 if (mch_dirname(cwd, MAXPATHL) != OK)
3937 {
3938 vim_free(cwd);
3939 return NULL;
3940 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003941 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003942#ifdef BACKSLASH_IN_FILENAME
3943 "", /* rem_backslash() will tell what chars to escape */
3944#else
3945 PATH_ESC_CHARS,
3946#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003947 '\\', TRUE);
3948 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003949 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003950 return NULL;
3951 ga_init2(&ga, 1, 100);
3952 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003953 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003954
3955 /* Call inputsave() so that a prompt for an encryption key works. */
3956 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3957 if (tabs)
3958 ga_concat(&ga, (char_u *)"tab ");
3959 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003960 for (i = 0; i < filec; i++)
3961 {
3962 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003963 * do it again in the Vim server. On MS-Windows only escape
3964 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003965 p = vim_strsave_escaped((char_u *)filev[i],
3966#ifdef UNIX
3967 PATH_ESC_CHARS
3968#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003969 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003970#endif
3971 );
3972 if (p == NULL)
3973 {
3974 vim_free(ga.ga_data);
3975 return NULL;
3976 }
3977 ga_concat(&ga, (char_u *)" ");
3978 ga_concat(&ga, p);
3979 vim_free(p);
3980 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003981 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3982
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003983 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3984 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003985 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3986
3987 /* Switch back to the correct current directory (prior to temporary path
3988 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003989 * correct after the :drop command. With line breaks and spaces:
3990 * if !exists('+acd') || !&acd
3991 * if haslocaldir()
3992 * cd -
3993 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02003994 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003995 * cd -
3996 * endif
3997 * endif
3998 */
3999 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004000 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004001 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004002 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004003 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004004
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004005 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004006 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4007 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004008 if (inicmd != NULL)
4009 {
4010 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4011 * the following commands to be inserted as text. Use a "|",
4012 * hopefully "inicmd" does allow this... */
4013 ga_concat(&ga, inicmd);
4014 ga_concat(&ga, (char_u *)"|");
4015 }
4016 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4017 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004018 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004019 ga_append(&ga, NUL);
4020 return ga.ga_data;
4021}
4022
4023/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004024 * Make our basic server name: use the specified "arg" if given, otherwise use
4025 * the tail of the command "cmd" we were started with.
4026 * Return the name in allocated memory. This doesn't include a serial number.
4027 */
4028 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004029serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004030{
4031 char_u *p;
4032
4033 if (arg != NULL && *arg != NUL)
4034 p = vim_strsave_up(arg);
4035 else
4036 {
4037 p = vim_strsave_up(gettail((char_u *)cmd));
4038 /* Remove .exe or .bat from the name. */
4039 if (p != NULL && vim_strchr(p, '.') != NULL)
4040 *vim_strchr(p, '.') = NUL;
4041 }
4042 return p;
4043}
4044#endif /* FEAT_CLIENTSERVER */
4045
4046#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4047/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004048 * Replace termcodes such as <CR> and insert as key presses if there is room.
4049 */
4050 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004051server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004052{
4053 char_u *ptr = NULL;
4054 char_u *cpo_save = p_cpo;
4055
4056 /* Set 'cpoptions' the way we want it.
4057 * B set - backslashes are *not* treated specially
4058 * k set - keycodes are *not* reverse-engineered
4059 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004060 * The last but one parameter of replace_termcodes() is TRUE so that the
4061 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004062 */
4063 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004064 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004065 p_cpo = cpo_save;
4066
4067 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4068 {
4069 /*
4070 * Add the string to the input stream.
4071 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4072 *
4073 * First clear typed characters from the typeahead buffer, there could
4074 * be half a mapping there. Then append to the existing string, so
4075 * that multiple commands from a client are concatenated.
4076 */
4077 if (typebuf.tb_maplen < typebuf.tb_len)
4078 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4079 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4080
4081 /* Let input_available() know we inserted text in the typeahead
4082 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004083 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004084 }
4085 vim_free((char_u *)ptr);
4086}
4087
4088/*
4089 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004090 */
4091 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004092eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004093{
4094 char_u *res;
4095 int save_dbl = debug_break_level;
4096 int save_ro = redir_off;
4097
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004098 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4099 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004100 debug_break_level = -1;
4101 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004102 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4103 * to be typed. Do generate errors so that try/catch works. */
4104 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004105
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004106 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004107
4108 debug_break_level = save_dbl;
4109 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004110 --emsg_silent;
4111 if (emsg_silent < 0)
4112 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004113
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004114 /* A client can tell us to redraw, but not to display the cursor, so do
4115 * that here. */
4116 setcursor();
4117 out_flush();
4118#ifdef FEAT_GUI
4119 if (gui.in_use)
4120 gui_update_cursor(FALSE, FALSE);
4121#endif
4122
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004123 return res;
4124}
4125
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004126/*
4127 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4128 * return an allocated string. Otherwise return "data".
4129 * "*tofree" is set to the result when it needs to be freed later.
4130 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004131 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004132serverConvert(
4133 char_u *client_enc UNUSED,
4134 char_u *data,
4135 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004136{
4137 char_u *res = data;
4138
4139 *tofree = NULL;
4140# ifdef FEAT_MBYTE
4141 if (client_enc != NULL && p_enc != NULL)
4142 {
4143 vimconv_T vimconv;
4144
4145 vimconv.vc_type = CONV_NONE;
4146 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4147 && vimconv.vc_type != CONV_NONE)
4148 {
4149 res = string_convert(&vimconv, data, NULL);
4150 if (res == NULL)
4151 res = data;
4152 else
4153 *tofree = res;
4154 }
4155 convert_setup(&vimconv, NULL, NULL);
4156 }
4157# endif
4158 return res;
4159}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004160#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004161
4162/*
4163 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4164 */
4165#if defined(FEAT_FKMAP) || defined(PROTO)
4166# include "farsi.c"
4167#endif
4168
4169/*
4170 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4171 */
4172#if defined(FEAT_ARABIC) || defined(PROTO)
4173# include "arabic.c"
4174#endif