blob: 4005f10ec73c238f683afd78fdbcc1d011375a73 [file] [log] [blame]
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
Bram Moolenaarb4210b32004-06-13 14:51:16 +000010#define EXTERN
11#include "vim.h"
12
Bram Moolenaarb4210b32004-06-13 14:51:16 +000013#ifdef __CYGWIN__
14# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000015# include <cygwin/version.h>
16# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
17 * cygwin_conv_path() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000018# endif
19# include <limits.h>
20#endif
21
Bram Moolenaarc013cb62005-07-24 21:18:31 +000022/* Maximum number of commands from + or -c arguments. */
23#define MAX_ARG_CMDS 10
24
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000025/* values for "window_layout" */
26#define WIN_HOR 1 /* "-o" horizontally split windows */
27#define WIN_VER 2 /* "-O" vertically split windows */
28#define WIN_TABS 3 /* "-p" windows on tab pages */
29
Bram Moolenaar58d98232005-07-23 22:25:46 +000030/* Struct for various parameters passed between main() and other functions. */
31typedef struct
32{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000033 int argc;
34 char **argv;
35
36 int evim_mode; /* started as "evim" */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000037 char_u *use_vimrc; /* vimrc from -u argument */
38
39 int n_commands; /* no. of commands from + or -c */
40 char_u *commands[MAX_ARG_CMDS]; /* commands from + or -c arg. */
41 char_u cmds_tofree[MAX_ARG_CMDS]; /* commands that need free() */
42 int n_pre_commands; /* no. of commands from --cmd */
43 char_u *pre_commands[MAX_ARG_CMDS]; /* commands from --cmd argument */
44
45 int edit_type; /* type of editing to do */
46 char_u *tagname; /* tag from -t argument */
47#ifdef FEAT_QUICKFIX
48 char_u *use_ef; /* 'errorfile' from -q argument */
49#endif
50
51 int want_full_screen;
52 int stdout_isatty; /* is stdout a terminal? */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +010053 int not_a_term; /* no warning for missing term? */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000054 char_u *term; /* specified terminal name */
55#ifdef FEAT_CRYPT
56 int ask_for_key; /* -x argument */
57#endif
58 int no_swap_file; /* "-n" argument used */
59#ifdef FEAT_EVAL
60 int use_debug_break_level;
61#endif
62#ifdef FEAT_WINDOWS
63 int window_count; /* number of windows to use */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000064 int window_layout; /* 0, WIN_HOR, WIN_VER or WIN_TABS */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000065#endif
66
67#ifdef FEAT_CLIENTSERVER
Bram Moolenaar58d98232005-07-23 22:25:46 +000068 int serverArg; /* TRUE when argument for a server */
69 char_u *serverName_arg; /* cmdline arg for server name */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000070 char_u *serverStr; /* remote server command */
71 char_u *serverStrEnc; /* encoding of serverStr */
72 char_u *servername; /* allocated name for our server */
73#endif
Bram Moolenaar53076832015-12-31 19:53:21 +010074#if !defined(UNIX) && !defined(__EMX__)
75# define EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +000076 int literal; /* don't expand file names */
77#endif
78#ifdef MSWIN
79 int full_path; /* file name argument was full path */
80#endif
81#ifdef FEAT_DIFF
82 int diff_mode; /* start with 'diff' set */
83#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +000084} mparm_T;
85
Bram Moolenaarc013cb62005-07-24 21:18:31 +000086/* Values for edit_type. */
87#define EDIT_NONE 0 /* no edit type yet */
88#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
89#define EDIT_STDIN 2 /* read file from stdin */
90#define EDIT_TAG 3 /* tag name argument given, use tagname */
91#define EDIT_QF 4 /* start in quickfix mode */
92
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010093#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010094static int file_owned(char *fname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +000095#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010096static void mainerr(int, char_u *);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010097#ifndef NO_VIM_MAIN
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010098static void main_msg(char *s);
99static void usage(void);
100static int get_number_arg(char_u *p, int *idx, int def);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100101# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100102static void init_locale(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100103# endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100104static void parse_command_name(mparm_T *parmp);
105static void early_arg_scan(mparm_T *parmp);
106static void command_line_scan(mparm_T *parmp);
107static void check_tty(mparm_T *parmp);
108static void read_stdin(void);
109static void create_windows(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100110# ifdef FEAT_WINDOWS
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100111static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100112# endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100113static void exe_pre_commands(mparm_T *parmp);
114static void exe_commands(mparm_T *parmp);
115static void source_startup_scripts(mparm_T *parmp);
116static void main_start_gui(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100117# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100118static void check_swap_exists_action(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100119# endif
120# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100121static void exec_on_server(mparm_T *parmp);
122static void prepare_server(mparm_T *parmp);
123static void cmdsrv_main(int *argc, char **argv, char_u *serverName_arg, char_u **serverStr);
124static char_u *serverMakeName(char_u *arg, char *cmd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100125# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000126#endif
127
128
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000129/*
130 * Different types of error messages.
131 */
132static char *(main_errors[]) =
133{
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000134 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000135#define ME_UNKNOWN_OPTION 0
136 N_("Too many edit arguments"),
137#define ME_TOO_MANY_ARGS 1
138 N_("Argument missing after"),
139#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000140 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000141#define ME_GARBAGE 3
142 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
143#define ME_EXTRA_CMD 4
144 N_("Invalid argument for"),
145#define ME_INVALID_ARG 5
146};
147
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100148#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100149#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +0200150
151static char_u *start_dir = NULL; /* current working dir on startup */
152
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000153 int
154# ifdef VIMDLL
155_export
156# endif
157# ifdef FEAT_GUI_MSWIN
158# ifdef __BORLANDC__
159_cdecl
160# endif
161VimMain
162# else
163main
164# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100165(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000166{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000167 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000168 mparm_T params; /* various parameters passed between
169 * main() and other functions. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000170#ifdef STARTUPTIME
171 int i;
172#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000173
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000174 /*
175 * Do any system-specific initialisations. These can NOT use IObuff or
176 * NameBuff. Thus emsg2() cannot be called!
177 */
178 mch_early_init();
179
Bram Moolenaar14993322014-09-09 12:25:33 +0200180#if defined(WIN32) && defined(FEAT_MBYTE)
181 /*
182 * MingW expands command line arguments, which confuses our code to
183 * convert when 'encoding' changes. Get the unexpanded arguments.
184 */
185 argc = get_cmd_argsW(&argv);
186#endif
187
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000188 /* Many variables are in "params" so that we can pass them to invoked
189 * functions without a lot of arguments. "argc" and "argv" are also
190 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000191 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000192 params.argc = argc;
193 params.argv = argv;
194 params.want_full_screen = TRUE;
195#ifdef FEAT_EVAL
196 params.use_debug_break_level = -1;
197#endif
198#ifdef FEAT_WINDOWS
199 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000200#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000201
Bram Moolenaar99685e62013-05-11 13:56:18 +0200202#ifdef FEAT_RUBY
203 {
204 int ruby_stack_start;
205 vim_ruby_init((void *)&ruby_stack_start);
206 }
207#endif
208
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000209#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000210 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000211#endif
212
213#ifdef MEM_PROFILE
214 atexit(vim_mem_profile_dump);
215#endif
216
217#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000218 for (i = 1; i < argc; ++i)
219 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000220 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000221 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000222 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000223 TIME_MSG("--- VIM STARTING ---");
224 break;
225 }
226 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000227#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000228 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000229
230#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000231 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000232#endif
233
234#ifdef FEAT_MBYTE
235 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
236#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000237#ifdef FEAT_EVAL
238 eval_init(); /* init global variables */
239#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000240
241#ifdef __QNXNTO__
242 qnx_init(); /* PhAttach() for clipboard, (and gui) */
243#endif
244
245#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000246 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000247 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000248 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000249 TIME_MSG("GUI prepared");
250#endif
251
252 /* Init the table of Normal mode commands. */
253 init_normal_cmds();
254
255#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000256 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000257#endif
258
259 /*
260 * Allocate space for the generic buffers (needed for set_init_1() and
261 * EMSG2()).
262 */
263 if ((IObuff = alloc(IOSIZE)) == NULL
264 || (NameBuff = alloc(MAXPATHL)) == NULL)
265 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000266 TIME_MSG("Allocated generic buffers");
267
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000268#ifdef NBDEBUG
269 /* Wait a moment for debugging NetBeans. Must be after allocating
270 * NameBuff. */
271 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
272 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000273 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000274#endif
275
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000276#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
277 /*
278 * Setup to use the current locale (for ctype() and many other things).
279 * NOTE: Translated messages with encodings other than latin1 will not
280 * work until set_init_1() has been called!
281 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000282 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000283 TIME_MSG("locale set");
284#endif
285
286#ifdef FEAT_GUI
287 gui.dofork = TRUE; /* default is to use fork() */
288#endif
289
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000290 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000291 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000292 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000293 * --server...
294 * --socketid
Bram Moolenaar78e17622007-08-30 10:26:19 +0000295 * --windowid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000296 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000297 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000298
299#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000300 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000301#endif
302#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000303 /* Prepare for possibly starting GUI sometime */
304 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000305 TIME_MSG("GUI prepared");
306#endif
307
308#ifdef FEAT_CLIPBOARD
309 clip_init(FALSE); /* Initialise clipboard stuff */
310 TIME_MSG("clipboard setup");
311#endif
312
313 /*
314 * Check if we have an interactive window.
315 * On the Amiga: If there is no window, we open one with a newcli command
316 * (needed for :! to * work). mch_check_win() will also handle the -d or
317 * -dev argument.
318 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000319 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000320 TIME_MSG("window checked");
321
322 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000323 * Allocate the first window and buffer.
324 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000325 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000326 if (win_alloc_first() == FAIL)
327 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000328
329 init_yank(); /* init yank buffers */
330
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000331 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200332 global_alist.id = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000333
334 /*
335 * Set the default values for the options.
336 * NOTE: Non-latin1 translated messages are working only after this,
337 * because this is where "has_mbyte" will be set, which is used by
338 * msg_outtrans_len_attr().
339 * First find out the home directory, needed to expand "~" in options.
340 */
341 init_homedir(); /* find real value of $HOME */
342 set_init_1();
343 TIME_MSG("inits 1");
344
345#ifdef FEAT_EVAL
346 set_lang_var(); /* set v:lang and v:ctype */
347#endif
348
349#ifdef FEAT_CLIENTSERVER
350 /*
351 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000352 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000353 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000354 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000355#endif
356
357 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000358 * Figure out the way to work from the command name argv[0].
359 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000360 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000361 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000362
363 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000364 * Process the command line arguments. File names are put in the global
365 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000366 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000367 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000368 TIME_MSG("parsing arguments");
369
370 /*
371 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000372 * there is no terminal version, and on Windows we can't fork one off with
373 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000374 */
375#ifdef ALWAYS_USE_GUI
376 gui.starting = TRUE;
377#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000378# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000379 /*
380 * Check if the GUI can be started. Reset gui.starting if not.
381 * Don't know about other systems, stay on the safe side and don't check.
382 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000383 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000384 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000385 if (gui_init_check() == FAIL)
386 {
387 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000388
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000389 /* When running "evim" or "gvim -y" we need the menus, exit if we
390 * don't have them. */
391 if (params.evim_mode)
392 mch_exit(1);
393 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000394 }
395# endif
396#endif
397
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000398 if (GARGCOUNT > 0)
399 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100400#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000401 /*
402 * Expand wildcards in file names.
403 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000404 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000405 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200406 start_dir = alloc(MAXPATHL);
407 if (start_dir != NULL)
408 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000409 /* Temporarily add '(' and ')' to 'isfname'. These are valid
410 * filename characters but are excluded from 'isfname' to make
411 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
412 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000413 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000414 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200415 if (start_dir != NULL)
416 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000417 }
418#endif
419 fname = alist_name(&GARGLIST[0]);
420 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000421
422#if defined(WIN32) && defined(FEAT_MBYTE)
423 {
424 extern void set_alist_count(void);
425
426 /* Remember the number of entries in the argument list. If it changes
427 * we don't react on setting 'encoding'. */
428 set_alist_count();
429 }
430#endif
431
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000432#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000433 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000434 {
435 /*
436 * If there is one filename, fully qualified, we have very probably
437 * been invoked from explorer, so change to the file's directory.
438 * Hint: to avoid this when typing a command use a forward slash.
439 * If the cd fails, it doesn't matter.
440 */
441 (void)vim_chdirfile(fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200442 if (start_dir != NULL)
443 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000444 }
445#endif
446 TIME_MSG("expanding arguments");
447
448#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000449 if (params.diff_mode && params.window_count == -1)
450 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000451#endif
452
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000453 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000454 ++RedrawingDisabled;
455
456 /*
457 * When listing swap file names, don't do cursor positioning et. al.
458 */
459 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000460 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000461
462 /*
463 * When certain to start the GUI, don't check capabilities of terminal.
464 * For GTK we can't be sure, but when started from the desktop it doesn't
465 * make sense to try using a terminal.
466 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000467#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000468 if (gui.starting
469# ifdef FEAT_GUI_GTK
470 && !isatty(2)
471# endif
472 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000473 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000474#endif
475
476#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
477 /* When the GUI is started from Finder, need to display messages in a
478 * message box. isatty(2) returns TRUE anyway, thus we need to check the
479 * name to know we're not started from a terminal. */
480 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000481 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000482 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000483
484 /* Avoid always using "/" as the current directory. Note that when
485 * started from Finder the arglist will be filled later in
486 * HandleODocAE() and "fname" will be NULL. */
487 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
488 && STRCMP(NameBuff, "/") == 0)
489 {
490 if (fname != NULL)
491 (void)vim_chdirfile(fname);
492 else
493 {
494 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
495 vim_chdir(NameBuff);
496 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200497 if (start_dir != NULL)
498 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000499 }
500 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000501#endif
502
503 /*
504 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100505 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000506 * Note that we may use mch_exit() before mch_init()!
507 */
508 mch_init();
509 TIME_MSG("shell init");
510
511#ifdef USE_XSMP
512 /*
513 * For want of anywhere else to do it, try to connect to xsmp here.
514 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
515 * Hijacking -X 'no X connection' to also disable XSMP connection as that
516 * has a similar delay upon failure.
517 * Only try if SESSION_MANAGER is set to something non-null.
518 */
519 if (!x_no_connect)
520 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000521 char *p = getenv("SESSION_MANAGER");
522
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000523 if (p != NULL && *p != NUL)
524 {
525 xsmp_init();
526 TIME_MSG("xsmp init");
527 }
528 }
529#endif
530
531 /*
532 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000533 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000534 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000535
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000536 /* This message comes before term inits, but after setting "silent_mode"
537 * when the input is not a tty. */
538 if (GARGCOUNT > 1 && !silent_mode)
539 printf(_("%d files to edit\n"), GARGCOUNT);
540
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000541 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000542 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000543 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000544 capabilities (will set full_screen) */
545 screen_start(); /* don't know where cursor is now */
546 TIME_MSG("Termcap init");
547 }
548
549 /*
550 * Set the default values for the options that use Rows and Columns.
551 */
552 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000553 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000554#ifdef FEAT_DIFF
555 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
556 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000557 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000558 diff_win_options(firstwin, FALSE);
559#endif
560
561 cmdline_row = Rows - p_ch;
562 msg_row = cmdline_row;
563 screenalloc(FALSE); /* allocate screen buffers */
564 set_init_2();
565 TIME_MSG("inits 2");
566
567 msg_scroll = TRUE;
568 no_wait_return = TRUE;
569
570 init_mappings(); /* set up initial mappings */
571
572 init_highlight(TRUE, FALSE); /* set the default highlight groups */
573 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000574
575#ifdef FEAT_EVAL
576 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000577 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000578#endif
579
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100580#ifdef FEAT_MZSCHEME
581 /*
582 * Newer version of MzScheme (Racket) require earlier (trampolined)
583 * initialisation via scheme_main_setup.
584 * Implement this by initialising it as early as possible
585 * and splitting off remaining Vim main into vim_main2
586 */
587 {
588 /* Pack up preprocessed command line arguments.
589 * It is safe because Scheme does not access argc/argv. */
590 char *args[2];
591 args[0] = (char *)fname;
592 args[1] = (char *)&params;
593 return mzscheme_main(2, args);
594 }
595}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100596#endif
597#endif /* NO_VIM_MAIN */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100598
Bram Moolenaar8866d272012-11-28 15:55:42 +0100599/* vim_main2() needs to be produced when FEAT_MZSCHEME is defined even when
600 * NO_VIM_MAIN is defined. */
601#ifdef FEAT_MZSCHEME
602 int
603vim_main2(int argc UNUSED, char **argv UNUSED)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100604{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100605# ifndef NO_VIM_MAIN
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100606 char_u *fname = (char_u *)argv[0];
607 mparm_T params;
608
609 memcpy(&params, argv[1], sizeof(params));
Bram Moolenaar8866d272012-11-28 15:55:42 +0100610# else
611 return 0;
612}
613# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100614#endif
615
Bram Moolenaar8866d272012-11-28 15:55:42 +0100616#ifndef NO_VIM_MAIN
Bram Moolenaar58d98232005-07-23 22:25:46 +0000617 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000618 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000619
Bram Moolenaar58d98232005-07-23 22:25:46 +0000620 /* Source startup scripts. */
621 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000622
623#ifdef FEAT_EVAL
624 /*
625 * Read all the plugin files.
626 * Only when compiled with +eval, since most plugins need it.
627 */
628 if (p_lpl)
629 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000630# ifdef VMS /* Somehow VMS doesn't handle the "**". */
631 source_runtime((char_u *)"plugin/*.vim", TRUE);
632# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000633 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000634# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000635 TIME_MSG("loading plugins");
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100636
637 source_packages();
638 TIME_MSG("loading packages");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000639 }
640#endif
641
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000642#ifdef FEAT_DIFF
643 /* Decide about window layout for diff mode after reading vimrc. */
644 if (params.diff_mode && params.window_layout == 0)
645 {
646 if (diffopt_horizontal())
647 params.window_layout = WIN_HOR; /* use horizontal split */
648 else
649 params.window_layout = WIN_VER; /* use vertical split */
650 }
651#endif
652
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000653 /*
654 * Recovery mode without a file name: List swap files.
655 * This uses the 'dir' option, therefore it must be after the
656 * initializations.
657 */
658 if (recoverymode && fname == NULL)
659 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200660 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000661 mch_exit(0);
662 }
663
664 /*
665 * Set a few option defaults after reading .vimrc files:
666 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
667 */
668 set_init_3();
669 TIME_MSG("inits 3");
670
671 /*
672 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
673 * Note that this overrides anything from a vimrc file.
674 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000675 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000676 p_uc = 0;
677
678#ifdef FEAT_FKMAP
679 if (curwin->w_p_rl && p_altkeymap)
680 {
681 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
682# ifdef FEAT_ARABIC
683 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
684# endif
685 p_fkmap = TRUE; /* Set the Farsi keymap mode */
686 }
687#endif
688
689#ifdef FEAT_GUI
690 if (gui.starting)
691 {
692#if defined(UNIX) || defined(VMS)
693 /* When something caused a message from a vimrc script, need to output
694 * an extra newline before the shell prompt. */
695 if (did_emsg || msg_didout)
696 putchar('\n');
697#endif
698
699 gui_start(); /* will set full_screen to TRUE */
700 TIME_MSG("starting GUI");
701
702 /* When running "evim" or "gvim -y" we need the menus, exit if we
703 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000704 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000705 mch_exit(1);
706 }
707#endif
708
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000709#ifdef FEAT_VIMINFO
710 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000711 * Read in registers, history etc, but not marks, from the viminfo file.
712 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000713 */
714 if (*p_viminfo != NUL)
715 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000716 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000717 TIME_MSG("reading viminfo");
718 }
719#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100720#ifdef FEAT_EVAL
721 /* It's better to make v:oldfiles an empty list than NULL. */
722 if (get_vim_var_list(VV_OLDFILES) == NULL)
723 set_vim_var_list(VV_OLDFILES, list_alloc());
724#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000725
726#ifdef FEAT_QUICKFIX
727 /*
728 * "-q errorfile": Load the error file now.
729 * If the error file can't be read, exit before doing anything else.
730 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000731 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000732 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000733 if (params.use_ef != NULL)
734 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000735 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200736 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
737 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000738 {
739 out_char('\n');
740 mch_exit(3);
741 }
742 TIME_MSG("reading errorfile");
743 }
744#endif
745
746 /*
747 * Start putting things on the screen.
748 * Scroll screen down before drawing over it
749 * Clear screen now, so file message will not be cleared.
750 */
751 starting = NO_BUFFERS;
752 no_wait_return = FALSE;
753 if (!exmode_active)
754 msg_scroll = FALSE;
755
756#ifdef FEAT_GUI
757 /*
758 * This seems to be required to make callbacks to be called now, instead
759 * of after things have been put on the screen, which then may be deleted
760 * when getting a resize callback.
761 * For the Mac this handles putting files dropped on the Vim icon to
762 * global_alist.
763 */
764 if (gui.in_use)
765 {
766# ifdef FEAT_SUN_WORKSHOP
767 if (!usingSunWorkShop)
768# endif
769 gui_wait_for_chars(50L);
770 TIME_MSG("GUI delay");
771 }
772#endif
773
774#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
775 qnx_clip_init();
776#endif
777
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200778#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
779 clip_init(TRUE);
780#endif
781
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000782#ifdef FEAT_XCLIPBOARD
783 /* Start using the X clipboard, unless the GUI was started. */
784# ifdef FEAT_GUI
785 if (!gui.in_use)
786# endif
787 {
788 setup_term_clip();
789 TIME_MSG("setup clipboard");
790 }
791#endif
792
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000793#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000794 /* Prepare for being a Vim server. */
795 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000796#endif
797
798 /*
799 * If "-" argument given: Read file from stdin.
800 * Do this before starting Raw mode, because it may change things that the
801 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
802 * are the same terminal: "cat | vim -".
803 * Using autocommands here may cause trouble...
804 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000805 if (params.edit_type == EDIT_STDIN && !recoverymode)
806 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000807
808#if defined(UNIX) || defined(VMS)
809 /* When switching screens and something caused a message from a vimrc
810 * script, need to output an extra newline on exit. */
811 if ((did_emsg || msg_didout) && *T_TI != NUL)
812 newline_on_exit = TRUE;
813#endif
814
815 /*
816 * When done something that is not allowed or error message call
817 * wait_return. This must be done before starttermcap(), because it may
818 * switch to another screen. It must be done after settmode(TMODE_RAW),
819 * because we want to react on a single key stroke.
820 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000821 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000822 */
823 settmode(TMODE_RAW);
824 TIME_MSG("setting raw mode");
825
826 if (need_wait_return || msg_didany)
827 {
828 wait_return(TRUE);
829 TIME_MSG("waiting for return");
830 }
831
832 starttermcap(); /* start termcap if not done by wait_return() */
833 TIME_MSG("start termcap");
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200834#if defined(FEAT_TERMRESPONSE)
835# if defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200836 may_req_ambiguous_char_width();
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200837# endif
838 may_req_bg_color();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100839#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000840
841#ifdef FEAT_MOUSE
842 setmouse(); /* may start using the mouse */
843#endif
844 if (scroll_region)
845 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000846 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000847
848 /*
849 * Don't clear the screen when starting in Ex mode, unless using the GUI.
850 */
851 if (exmode_active
852#ifdef FEAT_GUI
853 && !gui.in_use
854#endif
855 )
856 must_redraw = CLEAR;
857 else
858 {
859 screenclear(); /* clear screen */
860 TIME_MSG("clearing screen");
861 }
862
863#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000864 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000865 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100866 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200867 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000868 TIME_MSG("getting crypt key");
869 }
870#endif
871
872 no_wait_return = TRUE;
873
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000874 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000875 * Create the requested number of windows and edit buffers in them.
876 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000877 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000878 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000879 TIME_MSG("opening buffers");
880
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000881#ifdef FEAT_EVAL
882 /* clear v:swapcommand */
883 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
884#endif
885
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000886 /* Ex starts at last line of the file */
887 if (exmode_active)
888 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
889
890#ifdef FEAT_AUTOCMD
891 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
892 TIME_MSG("BufEnter autocommands");
893#endif
894 setpcmark();
895
896#ifdef FEAT_QUICKFIX
897 /*
898 * When started with "-q errorfile" jump to first error now.
899 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000900 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000901 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000902 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000903 TIME_MSG("jump to first error");
904 }
905#endif
906
907#ifdef FEAT_WINDOWS
908 /*
909 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000910 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000911 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200912 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000913#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200914 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000915
916#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000917 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000918 {
919 win_T *wp;
920
921 /* set options in each window for "vimdiff". */
922 for (wp = firstwin; wp != NULL; wp = wp->w_next)
923 diff_win_options(wp, TRUE);
924 }
925#endif
926
927 /*
928 * Shorten any of the filenames, but only when absolute.
929 */
930 shorten_fnames(FALSE);
931
932 /*
933 * Need to jump to the tag before executing the '-c command'.
934 * Makes "vim -c '/return' -t main" work.
935 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000936 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000937 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000938#if defined(HAS_SWAP_EXISTS_ACTION)
939 swap_exists_did_quit = FALSE;
940#endif
941
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000942 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000943 do_cmdline_cmd(IObuff);
944 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000945
946#if defined(HAS_SWAP_EXISTS_ACTION)
947 /* If the user doesn't want to edit the file then we quit here. */
948 if (swap_exists_did_quit)
949 getout(1);
950#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000951 }
952
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000953 /* Execute any "+", "-c" and "-S" arguments. */
954 if (params.n_commands > 0)
955 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000956
957 RedrawingDisabled = 0;
958 redraw_all_later(NOT_VALID);
959 no_wait_return = FALSE;
960 starting = 0;
961
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000962#ifdef FEAT_TERMRESPONSE
963 /* Requesting the termresponse is postponed until here, so that a "-c q"
964 * argument doesn't make it appear in the shell Vim was started from. */
965 may_req_termresponse();
966#endif
967
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000968 /* start in insert mode */
969 if (p_im)
970 need_start_insertmode = TRUE;
971
972#ifdef FEAT_AUTOCMD
973 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
974 TIME_MSG("VimEnter autocommands");
975#endif
976
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200977#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
978 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
979 * done after the clipboard is available and all initial commands that may
980 * modify the 'clipboard' setting have run; i.e. just before entering the
981 * main loop. */
982 {
983 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100984
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200985 adjust_clip_reg(&default_regname);
986 set_reg_var(default_regname);
987 }
988#endif
989
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000990#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
991 /* When a startup script or session file setup for diff'ing and
992 * scrollbind, sync the scrollbind now. */
993 if (curwin->w_p_diff && curwin->w_p_scb)
994 {
995 update_topline();
996 check_scrollbind((linenr_T)0, 0L);
997 TIME_MSG("diff scrollbinding");
998 }
999#endif
1000
1001#if defined(WIN3264) && !defined(FEAT_GUI_W32)
1002 mch_set_winsize_now(); /* Allow winsize changes from now on */
1003#endif
1004
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001005#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
1006 /* When tab pages were created, may need to update the tab pages line and
1007 * scrollbars. This is skipped while creating them. */
1008 if (first_tabpage->tp_next != NULL)
1009 {
1010 out_flush();
1011 gui_init_which_components(NULL);
1012 gui_update_scrollbars(TRUE);
1013 }
1014 need_mouse_correct = TRUE;
1015#endif
1016
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001017 /* If ":startinsert" command used, stuff a dummy command to be able to
1018 * call normal_cmd(), which will then start Insert mode. */
1019 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001020 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001021
1022#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001023 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001024 {
1025# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +02001026# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02001027 && !defined(FEAT_GUI_W32)
1028 if (gui.in_use)
1029 {
1030 mch_errmsg(_("netbeans is not supported with this GUI\n"));
1031 mch_exit(2);
1032 }
1033# endif
1034# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001035 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001036 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001037 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001038#endif
1039
1040 TIME_MSG("before starting main loop");
1041
1042 /*
1043 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001044 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001045 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001046
1047 return 0;
1048}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001049#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001050#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001051
1052/*
1053 * Main loop: Execute Normal mode commands until exiting Vim.
1054 * Also used to handle commands in the command-line window, until the window
1055 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 * Also used to handle ":visual" command after ":global": execute Normal mode
1057 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001058 */
1059 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001060main_loop(
1061 int cmdwin, /* TRUE when working in the command-line window */
1062 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001063{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001064 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001065 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001066#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001067 /* these are static to avoid a compiler warning */
1068 static linenr_T conceal_old_cursor_line = 0;
1069 static linenr_T conceal_new_cursor_line = 0;
1070 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001071#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001072
1073#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1074 /* Setup to catch a terminating error from the X server. Just ignore
1075 * it, restore the state and continue. This might not always work
1076 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001077 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001078 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001079 {
1080 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001081 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001082 got_int = TRUE;
1083 need_wait_return = FALSE;
1084 global_busy = FALSE;
1085 exmode_active = 0;
1086 skip_redraw = FALSE;
1087 RedrawingDisabled = 0;
1088 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001089 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001090# ifdef FEAT_EVAL
1091 emsg_skip = 0;
1092# endif
1093 emsg_off = 0;
1094# ifdef FEAT_MOUSE
1095 setmouse();
1096# endif
1097 settmode(TMODE_RAW);
1098 starttermcap();
1099 scroll_start();
1100 redraw_later_clear();
1101 }
1102#endif
1103
1104 clear_oparg(&oa);
1105 while (!cmdwin
1106#ifdef FEAT_CMDWIN
1107 || cmdwin_result == 0
1108#endif
1109 )
1110 {
1111 if (stuff_empty())
1112 {
1113 did_check_timestamps = FALSE;
1114 if (need_check_timestamps)
1115 check_timestamps(FALSE);
1116 if (need_wait_return) /* if wait_return still needed ... */
1117 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001118 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001119 {
1120 need_start_insertmode = FALSE;
1121 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1122 /* skip the fileinfo message now, because it would be shown
1123 * after insert mode finishes! */
1124 need_fileinfo = FALSE;
1125 }
1126 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001127
1128 /* Reset "got_int" now that we got back to the main loop. Except when
1129 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1130 * the ":g" command.
1131 * For ":g/pat/vi" we reset "got_int" when used once. When used
1132 * a second time we go back to Ex mode and abort the ":g" command. */
1133 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001134 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001135 if (noexmode && global_busy && !exmode_active && previous_got_int)
1136 {
1137 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1138 * used and keep "got_int" set, so that it aborts ":g". */
1139 exmode_active = EXMODE_NORMAL;
1140 State = NORMAL;
1141 }
1142 else if (!global_busy || !exmode_active)
1143 {
1144 if (!quit_more)
1145 (void)vgetc(); /* flush all buffers */
1146 got_int = FALSE;
1147 }
1148 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001149 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001150 else
1151 previous_got_int = FALSE;
1152
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001153 if (!exmode_active)
1154 msg_scroll = FALSE;
1155 quit_more = FALSE;
1156
1157 /*
1158 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1159 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1160 * update cursor and redraw.
1161 */
1162 if (skip_redraw || exmode_active)
1163 skip_redraw = FALSE;
1164 else if (do_redraw || stuff_empty())
1165 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001166#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001167 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001168 if (!finish_op && (
1169# ifdef FEAT_AUTOCMD
1170 has_cursormoved()
1171# endif
1172# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1173 ||
1174# endif
1175# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001176 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001177# endif
1178 )
1179 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001180 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001181# ifdef FEAT_AUTOCMD
1182 if (has_cursormoved())
1183 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1184 FALSE, curbuf);
1185# endif
1186# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001187 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001188 {
1189 conceal_old_cursor_line = last_cursormoved.lnum;
1190 conceal_new_cursor_line = curwin->w_cursor.lnum;
1191 conceal_update_lines = TRUE;
1192 }
1193# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001194 last_cursormoved = curwin->w_cursor;
1195 }
1196#endif
1197
Bram Moolenaar186628f2013-03-19 13:33:23 +01001198#ifdef FEAT_AUTOCMD
1199 /* Trigger TextChanged if b_changedtick differs. */
1200 if (!finish_op && has_textchanged()
1201 && last_changedtick != curbuf->b_changedtick)
1202 {
1203 if (last_changedtick_buf == curbuf)
1204 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1205 FALSE, curbuf);
1206 last_changedtick_buf = curbuf;
1207 last_changedtick = curbuf->b_changedtick;
1208 }
1209#endif
1210
Bram Moolenaar33aec762006-01-22 23:30:12 +00001211#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1212 /* Scroll-binding for diff mode may have been postponed until
1213 * here. Avoids doing it for every change. */
1214 if (diff_need_scrollbind)
1215 {
1216 check_scrollbind((linenr_T)0, 0L);
1217 diff_need_scrollbind = FALSE;
1218 }
1219#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001220#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001221 /* Include a closed fold completely in the Visual area. */
1222 foldAdjustVisual();
1223#endif
1224#ifdef FEAT_FOLDING
1225 /*
1226 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1227 * contain the cursor.
1228 * When 'foldopen' is "all", open the fold(s) under the cursor.
1229 * This may mark the window for redrawing.
1230 */
1231 if (hasAnyFolding(curwin) && !char_avail())
1232 {
1233 foldCheckClose();
1234 if (fdo_flags & FDO_ALL)
1235 foldOpenCursor();
1236 }
1237#endif
1238
1239 /*
1240 * Before redrawing, make sure w_topline is correct, and w_leftcol
1241 * if lines don't wrap, and w_skipcol if lines wrap.
1242 */
1243 update_topline();
1244 validate_cursor();
1245
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001246 if (VIsual_active)
1247 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001248 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001249 update_screen(0);
1250 else if (redraw_cmdline || clear_cmdline)
1251 showmode();
1252#ifdef FEAT_WINDOWS
1253 redraw_statuslines();
1254#endif
1255#ifdef FEAT_TITLE
1256 if (need_maketitle)
1257 maketitle();
1258#endif
1259 /* display message after redraw */
1260 if (keep_msg != NULL)
1261 {
1262 char_u *p;
1263
1264 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001265 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1266 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001267 p = keep_msg;
1268 msg_attr(p, keep_msg_attr);
1269 vim_free(p);
1270 }
1271 if (need_fileinfo) /* show file info after redraw */
1272 {
1273 fileinfo(FALSE, TRUE, FALSE);
1274 need_fileinfo = FALSE;
1275 }
1276
1277 emsg_on_display = FALSE; /* can delete error message now */
1278 did_emsg = FALSE;
1279 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001280 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001281 showruler(FALSE);
1282
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001283# if defined(FEAT_CONCEAL)
1284 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001285 && (conceal_old_cursor_line != conceal_new_cursor_line
1286 || conceal_cursor_line(curwin)
1287 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001288 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001289 if (conceal_old_cursor_line != conceal_new_cursor_line
1290 && conceal_old_cursor_line
1291 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001292 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001293 update_single_line(curwin, conceal_new_cursor_line);
1294 curwin->w_valid &= ~VALID_CROW;
1295 }
1296# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001297 setcursor();
1298 cursor_on();
1299
1300 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001301
1302#ifdef STARTUPTIME
1303 /* Now that we have drawn the first screen all the startup stuff
1304 * has been done, close any file for startup messages. */
1305 if (time_fd != NULL)
1306 {
1307 TIME_MSG("first screen update");
1308 TIME_MSG("--- VIM STARTED ---");
1309 fclose(time_fd);
1310 time_fd = NULL;
1311 }
1312#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001313 }
1314#ifdef FEAT_GUI
1315 if (need_mouse_correct)
1316 gui_mouse_correct();
1317#endif
1318
1319 /*
1320 * Update w_curswant if w_set_curswant has been set.
1321 * Postponed until here to avoid computing w_virtcol too often.
1322 */
1323 update_curswant();
1324
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001325#ifdef FEAT_EVAL
1326 /*
1327 * May perform garbage collection when waiting for a character, but
1328 * only at the very toplevel. Otherwise we may be using a List or
1329 * Dict internally somewhere.
1330 * "may_garbage_collect" is reset in vgetc() which is invoked through
1331 * do_exmode() and normal_cmd().
1332 */
1333 may_garbage_collect = (!cmdwin && !noexmode);
1334#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001335 /*
1336 * If we're invoked as ex, do a round of ex commands.
1337 * Otherwise, get and execute a normal mode command.
1338 */
1339 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001340 {
1341 if (noexmode) /* End of ":global/path/visual" commands */
1342 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001343 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001344 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001345 else
1346 normal_cmd(&oa, TRUE);
1347 }
1348}
1349
1350
1351#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1352/*
1353 * Exit, but leave behind swap files for modified buffers.
1354 */
1355 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001356getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001357{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001358# if defined(SIGHUP) && defined(SIG_IGN)
1359 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1360 * makes Vim exit and then handling SIGHUP causes various reentrance
1361 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001362 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001363# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001364
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001365 ml_close_notmod(); /* close all not-modified buffers */
1366 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1367 ml_close_all(FALSE); /* close all memfiles, without deleting */
1368 getout(exitval); /* exit Vim properly */
1369}
1370#endif
1371
1372
1373/* Exit properly */
1374 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001375getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001376{
1377#ifdef FEAT_AUTOCMD
1378 buf_T *buf;
1379 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001380 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001381#endif
1382
1383 exiting = TRUE;
1384
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001385 /* When running in Ex mode an error causes us to exit with a non-zero exit
1386 * code. POSIX requires this, although it's not 100% clear from the
1387 * standard. */
1388 if (exmode_active)
1389 exitval += ex_exitval;
1390
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001391 /* Position the cursor on the last screen line, below all the text */
1392#ifdef FEAT_GUI
1393 if (!gui.in_use)
1394#endif
1395 windgoto((int)Rows - 1, 0);
1396
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001397#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1398 /* Optionally print hashtable efficiency. */
1399 hash_debug_results();
1400#endif
1401
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001402#ifdef FEAT_GUI
1403 msg_didany = FALSE;
1404#endif
1405
1406#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001407 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001408 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001409 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1410# if defined FEAT_WINDOWS
1411 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001412 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001413 next_tp = tp->tp_next;
1414 for (wp = (tp == curtab)
1415 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001416 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001417 if (wp->w_buffer == NULL)
1418 /* Autocmd must have close the buffer already, skip. */
1419 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001420 buf = wp->w_buffer;
1421 if (buf->b_changedtick != -1)
1422 {
1423 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1424 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001425 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001426 /* start all over, autocommands may mess up the lists */
1427 next_tp = first_tabpage;
1428 break;
1429 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001430 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001431 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001432# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001433 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1434 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001435# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001436
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001437 /* Trigger BufUnload for buffers that are loaded */
1438 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1439 if (buf->b_ml.ml_mfp != NULL)
1440 {
1441 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001442 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001443 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1444 break;
1445 }
1446 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1447 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001448#endif
1449
1450#ifdef FEAT_VIMINFO
1451 if (*p_viminfo != NUL)
1452 /* Write out the registers, history, marks etc, to the viminfo file */
1453 write_viminfo(NULL, FALSE);
1454#endif
1455
1456#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001457 if (get_vim_var_nr(VV_DYING) <= 1)
1458 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001459#endif
1460
Bram Moolenaar05159a02005-02-26 23:04:13 +00001461#ifdef FEAT_PROFILE
1462 profile_dump();
1463#endif
1464
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001465 if (did_emsg
1466#ifdef FEAT_GUI
1467 || (gui.in_use && msg_didany && p_verbose > 0)
1468#endif
1469 )
1470 {
1471 /* give the user a chance to read the (error) message */
1472 no_wait_return = FALSE;
1473 wait_return(FALSE);
1474 }
1475
1476#ifdef FEAT_AUTOCMD
1477 /* Position the cursor again, the autocommands may have moved it */
1478# ifdef FEAT_GUI
1479 if (!gui.in_use)
1480# endif
1481 windgoto((int)Rows - 1, 0);
1482#endif
1483
Bram Moolenaar65edff82016-02-21 16:40:11 +01001484#ifdef FEAT_JOB
1485 job_stop_on_exit();
1486#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001487#ifdef FEAT_LUA
1488 lua_end();
1489#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001490#ifdef FEAT_MZSCHEME
1491 mzscheme_end();
1492#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001493#ifdef FEAT_TCL
1494 tcl_end();
1495#endif
1496#ifdef FEAT_RUBY
1497 ruby_end();
1498#endif
1499#ifdef FEAT_PYTHON
1500 python_end();
1501#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001502#ifdef FEAT_PYTHON3
1503 python3_end();
1504#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001505#ifdef FEAT_PERL
1506 perl_end();
1507#endif
1508#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1509 iconv_end();
1510#endif
1511#ifdef FEAT_NETBEANS_INTG
1512 netbeans_end();
1513#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001514#ifdef FEAT_CSCOPE
1515 cs_end();
1516#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001517#ifdef FEAT_EVAL
1518 if (garbage_collect_at_exit)
1519 garbage_collect();
1520#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001521#if defined(WIN32) && defined(FEAT_MBYTE)
1522 free_cmd_argsW();
1523#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001524
1525 mch_exit(exitval);
1526}
1527
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001528#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001529/*
1530 * Get a (optional) count for a Vim argument.
1531 */
1532 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001533get_number_arg(
1534 char_u *p, /* pointer to argument */
1535 int *idx, /* index in argument, is incremented */
1536 int def) /* default value */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001537{
1538 if (vim_isdigit(p[*idx]))
1539 {
1540 def = atoi((char *)&(p[*idx]));
1541 while (vim_isdigit(p[*idx]))
1542 *idx = *idx + 1;
1543 }
1544 return def;
1545}
1546
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001547#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1548/*
1549 * Setup to use the current locale (for ctype() and many other things).
1550 */
1551 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001552init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001553{
1554 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001555
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001556# ifdef FEAT_GUI_GTK
1557 /* Tell Gtk not to change our locale settings. */
1558 gtk_disable_setlocale();
1559# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001560# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1561 /* Make sure strtod() uses a decimal point, not a comma. */
1562 setlocale(LC_NUMERIC, "C");
1563# endif
1564
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001565# ifdef WIN32
1566 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1567 * text while it's expecting text in the current locale. This call avoids
1568 * that. */
1569 setlocale(LC_CTYPE, "C");
1570# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001571
1572# ifdef FEAT_GETTEXT
1573 {
1574 int mustfree = FALSE;
1575 char_u *p;
1576
1577# ifdef DYNAMIC_GETTEXT
1578 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001579 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001580# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001581 /* expand_env() doesn't work yet, because g_chartab[] is not
1582 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001583 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1584 if (p != NULL && *p != NUL)
1585 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001586 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001587 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1588 }
1589 if (mustfree)
1590 vim_free(p);
1591 textdomain(VIMPACKAGE);
1592 }
1593# endif
1594}
1595#endif
1596
1597/*
1598 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1599 * If the executable name starts with "r" we disable shell commands.
1600 * If the next character is "e" we run in Easy mode.
1601 * If the next character is "g" we run the GUI version.
1602 * If the next characters are "view" we start in readonly mode.
1603 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1604 * If the next characters are "ex" we start in Ex mode. If it's followed
1605 * by "im" use improved Ex mode.
1606 */
1607 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001608parse_command_name(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001609{
1610 char_u *initstr;
1611
1612 initstr = gettail((char_u *)parmp->argv[0]);
1613
1614#ifdef MACOS_X_UNIX
1615 /* An issue has been seen when launching Vim in such a way that
1616 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1617 * executable or a symbolic link of it. Until this issue is resolved
1618 * we prohibit the GUI from being used.
1619 */
1620 if (STRCMP(initstr, parmp->argv[0]) == 0)
1621 disallow_gui = TRUE;
1622
1623 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001624 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001625#endif
1626
1627#ifdef FEAT_EVAL
1628 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaara1706c92014-04-01 19:55:49 +02001629 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001630#endif
1631
1632 if (TOLOWER_ASC(initstr[0]) == 'r')
1633 {
1634 restricted = TRUE;
1635 ++initstr;
1636 }
1637
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001638 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001639 if (TOLOWER_ASC(initstr[0]) == 'e'
1640 && (TOLOWER_ASC(initstr[1]) == 'v'
1641 || TOLOWER_ASC(initstr[1]) == 'g'))
1642 {
1643#ifdef FEAT_GUI
1644 gui.starting = TRUE;
1645#endif
1646 parmp->evim_mode = TRUE;
1647 ++initstr;
1648 }
1649
Bram Moolenaar806875d2008-09-18 18:57:10 +00001650 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1651 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001652 {
1653 main_start_gui();
1654#ifdef FEAT_GUI
1655 ++initstr;
1656#endif
1657 }
1658
1659 if (STRNICMP(initstr, "view", 4) == 0)
1660 {
1661 readonlymode = TRUE;
1662 curbuf->b_p_ro = TRUE;
1663 p_uc = 10000; /* don't update very often */
1664 initstr += 4;
1665 }
1666 else if (STRNICMP(initstr, "vim", 3) == 0)
1667 initstr += 3;
1668
1669 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1670 if (STRICMP(initstr, "diff") == 0)
1671 {
1672#ifdef FEAT_DIFF
1673 parmp->diff_mode = TRUE;
1674#else
1675 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1676 mch_errmsg("\n");
1677 mch_exit(2);
1678#endif
1679 }
1680
1681 if (STRNICMP(initstr, "ex", 2) == 0)
1682 {
1683 if (STRNICMP(initstr + 2, "im", 2) == 0)
1684 exmode_active = EXMODE_VIM;
1685 else
1686 exmode_active = EXMODE_NORMAL;
1687 change_compatible(TRUE); /* set 'compatible' */
1688 }
1689}
1690
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001691/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001692 * Get the name of the display, before gui_prepare() removes it from
1693 * argv[]. Used for the xterm-clipboard display.
1694 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001695 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001696 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001697 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001698early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001699{
Bram Moolenaar03005972008-11-20 13:12:36 +00001700#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1701 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001702 int argc = parmp->argc;
1703 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001704 int i;
1705
1706 for (i = 1; i < argc; i++)
1707 {
1708 if (STRCMP(argv[i], "--") == 0)
1709 break;
1710# ifdef FEAT_XCLIPBOARD
1711 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001712# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001713 || STRICMP(argv[i], "--display") == 0
1714# endif
1715 )
1716 {
1717 if (i == argc - 1)
1718 mainerr_arg_missing((char_u *)argv[i]);
1719 xterm_display = argv[++i];
1720 }
1721# endif
1722# ifdef FEAT_CLIENTSERVER
1723 else if (STRICMP(argv[i], "--servername") == 0)
1724 {
1725 if (i == argc - 1)
1726 mainerr_arg_missing((char_u *)argv[i]);
1727 parmp->serverName_arg = (char_u *)argv[++i];
1728 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001729 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001730 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001731 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001732 {
1733 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001734# ifdef FEAT_GUI
1735 if (strstr(argv[i], "-wait") != 0)
1736 /* don't fork() when starting the GUI to edit files ourself */
1737 gui.dofork = FALSE;
1738# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001739 }
1740# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001741
1742# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1743# ifdef FEAT_GUI_W32
1744 else if (STRICMP(argv[i], "--windowid") == 0)
1745# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001746 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001747# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001748 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001749 long_u id;
1750 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001751
1752 if (i == argc - 1)
1753 mainerr_arg_missing((char_u *)argv[i]);
1754 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001755 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001756 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001757 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001758 if (count != 1)
1759 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1760 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001761# ifdef FEAT_GUI_W32
1762 win_socket_id = id;
1763# else
1764 gtk_socket_id = id;
1765# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001766 i++;
1767 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001768# endif
1769# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001770 else if (STRICMP(argv[i], "--echo-wid") == 0)
1771 echo_wid_arg = TRUE;
1772# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001773# ifndef FEAT_NETBEANS_INTG
1774 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001775 {
1776 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1777 mch_exit(2);
1778 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001779# endif
1780
Bram Moolenaar58d98232005-07-23 22:25:46 +00001781 }
1782#endif
1783}
1784
1785/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001786 * Scan the command line arguments.
1787 */
1788 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001789command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001790{
1791 int argc = parmp->argc;
1792 char **argv = parmp->argv;
1793 int argv_idx; /* index in argv[n][] */
1794 int had_minmin = FALSE; /* found "--" argument */
1795 int want_argument; /* option argument with argument */
1796 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001797 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001798 long n;
1799
1800 --argc;
1801 ++argv;
1802 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1803 while (argc > 0)
1804 {
1805 /*
1806 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1807 */
1808 if (argv[0][0] == '+' && !had_minmin)
1809 {
1810 if (parmp->n_commands >= MAX_ARG_CMDS)
1811 mainerr(ME_EXTRA_CMD, NULL);
1812 argv_idx = -1; /* skip to next argument */
1813 if (argv[0][1] == NUL)
1814 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1815 else
1816 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1817 }
1818
1819 /*
1820 * Optional argument.
1821 */
1822 else if (argv[0][0] == '-' && !had_minmin)
1823 {
1824 want_argument = FALSE;
1825 c = argv[0][argv_idx++];
1826#ifdef VMS
1827 /*
1828 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1829 * and "-/X" as "-X".
1830 */
1831 if (c == '/')
1832 {
1833 c = argv[0][argv_idx++];
1834 c = TOUPPER_ASC(c);
1835 }
1836 else
1837 c = TOLOWER_ASC(c);
1838#endif
1839 switch (c)
1840 {
1841 case NUL: /* "vim -" read from stdin */
1842 /* "ex -" silent mode */
1843 if (exmode_active)
1844 silent_mode = TRUE;
1845 else
1846 {
1847 if (parmp->edit_type != EDIT_NONE)
1848 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1849 parmp->edit_type = EDIT_STDIN;
1850 read_cmd_fd = 2; /* read from stderr instead of stdin */
1851 }
1852 argv_idx = -1; /* skip to next argument */
1853 break;
1854
1855 case '-': /* "--" don't take any more option arguments */
1856 /* "--help" give help message */
1857 /* "--version" give version message */
1858 /* "--literal" take files literally */
1859 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001860 /* "--not-a-term" don't warn for not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001861 /* "--noplugin[s]" skip plugins */
1862 /* "--cmd <cmd>" execute cmd before vimrc */
1863 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1864 usage();
1865 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1866 {
1867 Columns = 80; /* need to init Columns */
1868 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1869 list_version();
1870 msg_putchar('\n');
1871 msg_didout = FALSE;
1872 mch_exit(0);
1873 }
1874 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1875 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001876#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001877 parmp->literal = TRUE;
1878#endif
1879 }
1880 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1881 {
1882#ifdef FEAT_GUI
1883 gui.dofork = FALSE; /* don't fork() when starting GUI */
1884#endif
1885 }
1886 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1887 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001888 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1889 parmp->not_a_term = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001890 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1891 {
1892 want_argument = TRUE;
1893 argv_idx += 3;
1894 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001895 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1896 {
1897 want_argument = TRUE;
1898 argv_idx += 11;
1899 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001900#ifdef FEAT_CLIENTSERVER
1901 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1902 ; /* already processed -- no arg */
1903 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1904 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1905 {
1906 /* already processed -- snatch the following arg */
1907 if (argc > 1)
1908 {
1909 --argc;
1910 ++argv;
1911 }
1912 }
1913#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001914#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1915# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001916 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001917# else
1918 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1919# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001920 {
1921 /* already processed -- snatch the following arg */
1922 if (argc > 1)
1923 {
1924 --argc;
1925 ++argv;
1926 }
1927 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001928#endif
1929#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001930 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1931 {
1932 /* already processed, skip */
1933 }
1934#endif
1935 else
1936 {
1937 if (argv[0][argv_idx])
1938 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1939 had_minmin = TRUE;
1940 }
1941 if (!want_argument)
1942 argv_idx = -1; /* skip to next argument */
1943 break;
1944
1945 case 'A': /* "-A" start in Arabic mode */
1946#ifdef FEAT_ARABIC
1947 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1948#else
1949 mch_errmsg(_(e_noarabic));
1950 mch_exit(2);
1951#endif
1952 break;
1953
1954 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001955 /* Needs to be effective before expanding file names, because
1956 * for Win32 this makes us edit a shortcut file itself,
1957 * instead of the file it links to. */
1958 set_options_bin(curbuf->b_p_bin, 1, 0);
1959 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001960 break;
1961
1962 case 'C': /* "-C" Compatible */
1963 change_compatible(TRUE);
1964 break;
1965
1966 case 'e': /* "-e" Ex mode */
1967 exmode_active = EXMODE_NORMAL;
1968 break;
1969
1970 case 'E': /* "-E" Improved Ex mode */
1971 exmode_active = EXMODE_VIM;
1972 break;
1973
1974 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1975 window directly, not with newcli */
1976#ifdef FEAT_GUI
1977 gui.dofork = FALSE; /* don't fork() when starting GUI */
1978#endif
1979 break;
1980
1981 case 'g': /* "-g" start GUI */
1982 main_start_gui();
1983 break;
1984
1985 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1986#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001987 p_fkmap = TRUE;
1988 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001989#else
1990 mch_errmsg(_(e_nofarsi));
1991 mch_exit(2);
1992#endif
1993 break;
1994
1995 case 'h': /* "-h" give help message */
1996#ifdef FEAT_GUI_GNOME
1997 /* Tell usage() to exit for "gvim". */
1998 gui.starting = FALSE;
1999#endif
2000 usage();
2001 break;
2002
2003 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2004#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002005 p_hkmap = TRUE;
2006 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002007#else
2008 mch_errmsg(_(e_nohebrew));
2009 mch_exit(2);
2010#endif
2011 break;
2012
2013 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2014#ifdef FEAT_LISP
2015 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2016 p_sm = TRUE;
2017#endif
2018 break;
2019
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002020 case 'M': /* "-M" no changes or writing of files */
2021 reset_modifiable();
2022 /* FALLTHROUGH */
2023
2024 case 'm': /* "-m" no writing of files */
2025 p_write = FALSE;
2026 break;
2027
2028 case 'y': /* "-y" easy mode */
2029#ifdef FEAT_GUI
2030 gui.starting = TRUE; /* start GUI a bit later */
2031#endif
2032 parmp->evim_mode = TRUE;
2033 break;
2034
2035 case 'N': /* "-N" Nocompatible */
2036 change_compatible(FALSE);
2037 break;
2038
2039 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002040#ifdef FEAT_NETBEANS_INTG
2041 /* checking for "-nb", netbeans parameters */
2042 if (argv[0][argv_idx] == 'b')
2043 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002044 netbeansArg = argv[0];
2045 argv_idx = -1; /* skip to next argument */
2046 }
2047 else
2048#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002049 parmp->no_swap_file = TRUE;
2050 break;
2051
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002052 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002053#ifdef TARGET_API_MAC_OSX
2054 /* For some reason on MacOS X, an argument like:
2055 -psn_0_10223617 is passed in when invoke from Finder
2056 or with the 'open' command */
2057 if (argv[0][argv_idx] == 's')
2058 {
2059 argv_idx = -1; /* bypass full -psn */
2060 main_start_gui();
2061 break;
2062 }
2063#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002064#ifdef FEAT_WINDOWS
2065 /* default is 0: open window for each file */
2066 parmp->window_count = get_number_arg((char_u *)argv[0],
2067 &argv_idx, 0);
2068 parmp->window_layout = WIN_TABS;
2069#endif
2070 break;
2071
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002072 case 'o': /* "-o[N]" open N horizontal split windows */
2073#ifdef FEAT_WINDOWS
2074 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002075 parmp->window_count = get_number_arg((char_u *)argv[0],
2076 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002077 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002078#endif
2079 break;
2080
2081 case 'O': /* "-O[N]" open N vertical split windows */
2082#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2083 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002084 parmp->window_count = get_number_arg((char_u *)argv[0],
2085 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002086 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002087#endif
2088 break;
2089
2090#ifdef FEAT_QUICKFIX
2091 case 'q': /* "-q" QuickFix mode */
2092 if (parmp->edit_type != EDIT_NONE)
2093 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2094 parmp->edit_type = EDIT_QF;
2095 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2096 {
2097 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2098 argv_idx = -1;
2099 }
2100 else if (argc > 1) /* "-q {errorfile}" */
2101 want_argument = TRUE;
2102 break;
2103#endif
2104
2105 case 'R': /* "-R" readonly mode */
2106 readonlymode = TRUE;
2107 curbuf->b_p_ro = TRUE;
2108 p_uc = 10000; /* don't update very often */
2109 break;
2110
2111 case 'r': /* "-r" recovery mode */
2112 case 'L': /* "-L" recovery mode */
2113 recoverymode = 1;
2114 break;
2115
2116 case 's':
2117 if (exmode_active) /* "-s" silent (batch) mode */
2118 silent_mode = TRUE;
2119 else /* "-s {scriptin}" read from script file */
2120 want_argument = TRUE;
2121 break;
2122
2123 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2124 if (parmp->edit_type != EDIT_NONE)
2125 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2126 parmp->edit_type = EDIT_TAG;
2127 if (argv[0][argv_idx]) /* "-t{tag}" */
2128 {
2129 parmp->tagname = (char_u *)argv[0] + argv_idx;
2130 argv_idx = -1;
2131 }
2132 else /* "-t {tag}" */
2133 want_argument = TRUE;
2134 break;
2135
2136#ifdef FEAT_EVAL
2137 case 'D': /* "-D" Debugging */
2138 parmp->use_debug_break_level = 9999;
2139 break;
2140#endif
2141#ifdef FEAT_DIFF
2142 case 'd': /* "-d" 'diff' */
2143# ifdef AMIGA
2144 /* check for "-dev {device}" */
2145 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2146 want_argument = TRUE;
2147 else
2148# endif
2149 parmp->diff_mode = TRUE;
2150 break;
2151#endif
2152 case 'V': /* "-V{N}" Verbose level */
2153 /* default is 10: a little bit verbose */
2154 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2155 if (argv[0][argv_idx] != NUL)
2156 {
2157 set_option_value((char_u *)"verbosefile", 0L,
2158 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002159 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002160 }
2161 break;
2162
2163 case 'v': /* "-v" Vi-mode (as if called "vi") */
2164 exmode_active = 0;
2165#ifdef FEAT_GUI
2166 gui.starting = FALSE; /* don't start GUI */
2167#endif
2168 break;
2169
2170 case 'w': /* "-w{number}" set window height */
2171 /* "-w {scriptout}" write to script */
2172 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2173 {
2174 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2175 set_option_value((char_u *)"window", n, NULL, 0);
2176 break;
2177 }
2178 want_argument = TRUE;
2179 break;
2180
2181#ifdef FEAT_CRYPT
2182 case 'x': /* "-x" encrypted reading/writing of files */
2183 parmp->ask_for_key = TRUE;
2184 break;
2185#endif
2186
2187 case 'X': /* "-X" don't connect to X server */
2188#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2189 x_no_connect = TRUE;
2190#endif
2191 break;
2192
2193 case 'Z': /* "-Z" restricted mode */
2194 restricted = TRUE;
2195 break;
2196
2197 case 'c': /* "-c{command}" or "-c {command}" execute
2198 command */
2199 if (argv[0][argv_idx] != NUL)
2200 {
2201 if (parmp->n_commands >= MAX_ARG_CMDS)
2202 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002203 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2204 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002205 argv_idx = -1;
2206 break;
2207 }
2208 /*FALLTHROUGH*/
2209 case 'S': /* "-S {file}" execute Vim script */
2210 case 'i': /* "-i {viminfo}" use for viminfo */
2211#ifndef FEAT_DIFF
2212 case 'd': /* "-d {device}" device (for Amiga) */
2213#endif
2214 case 'T': /* "-T {terminal}" terminal name */
2215 case 'u': /* "-u {vimrc}" vim inits file */
2216 case 'U': /* "-U {gvimrc}" gvim inits file */
2217 case 'W': /* "-W {scriptout}" overwrite */
2218#ifdef FEAT_GUI_W32
2219 case 'P': /* "-P {parent title}" MDI parent */
2220#endif
2221 want_argument = TRUE;
2222 break;
2223
2224 default:
2225 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2226 }
2227
2228 /*
2229 * Handle option arguments with argument.
2230 */
2231 if (want_argument)
2232 {
2233 /*
2234 * Check for garbage immediately after the option letter.
2235 */
2236 if (argv[0][argv_idx] != NUL)
2237 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2238
2239 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002240 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002241 mainerr_arg_missing((char_u *)argv[0]);
2242 ++argv;
2243 argv_idx = -1;
2244
2245 switch (c)
2246 {
2247 case 'c': /* "-c {command}" execute command */
2248 case 'S': /* "-S {file}" execute Vim script */
2249 if (parmp->n_commands >= MAX_ARG_CMDS)
2250 mainerr(ME_EXTRA_CMD, NULL);
2251 if (c == 'S')
2252 {
2253 char *a;
2254
2255 if (argc < 1)
2256 /* "-S" without argument: use default session file
2257 * name. */
2258 a = SESSION_FILE;
2259 else if (argv[0][0] == '-')
2260 {
2261 /* "-S" followed by another option: use default
2262 * session file name. */
2263 a = SESSION_FILE;
2264 ++argc;
2265 --argv;
2266 }
2267 else
2268 a = argv[0];
2269 p = alloc((unsigned)(STRLEN(a) + 4));
2270 if (p == NULL)
2271 mch_exit(2);
2272 sprintf((char *)p, "so %s", a);
2273 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2274 parmp->commands[parmp->n_commands++] = p;
2275 }
2276 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002277 parmp->commands[parmp->n_commands++] =
2278 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002279 break;
2280
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002281 case '-':
2282 if (argv[-1][2] == 'c')
2283 {
2284 /* "--cmd {command}" execute command */
2285 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2286 mainerr(ME_EXTRA_CMD, NULL);
2287 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002288 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002289 }
2290 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002291 break;
2292
2293 /* case 'd': -d {device} is handled in mch_check_win() for the
2294 * Amiga */
2295
2296#ifdef FEAT_QUICKFIX
2297 case 'q': /* "-q {errorfile}" QuickFix mode */
2298 parmp->use_ef = (char_u *)argv[0];
2299 break;
2300#endif
2301
2302 case 'i': /* "-i {viminfo}" use for viminfo */
2303 use_viminfo = (char_u *)argv[0];
2304 break;
2305
2306 case 's': /* "-s {scriptin}" read from script file */
2307 if (scriptin[0] != NULL)
2308 {
2309scripterror:
2310 mch_errmsg(_("Attempt to open script file again: \""));
2311 mch_errmsg(argv[-1]);
2312 mch_errmsg(" ");
2313 mch_errmsg(argv[0]);
2314 mch_errmsg("\"\n");
2315 mch_exit(2);
2316 }
2317 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2318 {
2319 mch_errmsg(_("Cannot open for reading: \""));
2320 mch_errmsg(argv[0]);
2321 mch_errmsg("\"\n");
2322 mch_exit(2);
2323 }
2324 if (save_typebuf() == FAIL)
2325 mch_exit(2); /* out of memory */
2326 break;
2327
2328 case 't': /* "-t {tag}" */
2329 parmp->tagname = (char_u *)argv[0];
2330 break;
2331
2332 case 'T': /* "-T {terminal}" terminal name */
2333 /*
2334 * The -T term argument is always available and when
2335 * HAVE_TERMLIB is supported it overrides the environment
2336 * variable TERM.
2337 */
2338#ifdef FEAT_GUI
2339 if (term_is_gui((char_u *)argv[0]))
2340 gui.starting = TRUE; /* start GUI a bit later */
2341 else
2342#endif
2343 parmp->term = (char_u *)argv[0];
2344 break;
2345
2346 case 'u': /* "-u {vimrc}" vim inits file */
2347 parmp->use_vimrc = (char_u *)argv[0];
2348 break;
2349
2350 case 'U': /* "-U {gvimrc}" gvim inits file */
2351#ifdef FEAT_GUI
2352 use_gvimrc = (char_u *)argv[0];
2353#endif
2354 break;
2355
2356 case 'w': /* "-w {nr}" 'window' value */
2357 /* "-w {scriptout}" append to script file */
2358 if (vim_isdigit(*((char_u *)argv[0])))
2359 {
2360 argv_idx = 0;
2361 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2362 set_option_value((char_u *)"window", n, NULL, 0);
2363 argv_idx = -1;
2364 break;
2365 }
2366 /*FALLTHROUGH*/
2367 case 'W': /* "-W {scriptout}" overwrite script file */
2368 if (scriptout != NULL)
2369 goto scripterror;
2370 if ((scriptout = mch_fopen(argv[0],
2371 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2372 {
2373 mch_errmsg(_("Cannot open for script output: \""));
2374 mch_errmsg(argv[0]);
2375 mch_errmsg("\"\n");
2376 mch_exit(2);
2377 }
2378 break;
2379
2380#ifdef FEAT_GUI_W32
2381 case 'P': /* "-P {parent title}" MDI parent */
2382 gui_mch_set_parent(argv[0]);
2383 break;
2384#endif
2385 }
2386 }
2387 }
2388
2389 /*
2390 * File name argument.
2391 */
2392 else
2393 {
2394 argv_idx = -1; /* skip to next argument */
2395
2396 /* Check for only one type of editing. */
2397 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2398 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2399 parmp->edit_type = EDIT_FILE;
2400
2401#ifdef MSWIN
2402 /* Remember if the argument was a full path before changing
2403 * slashes to backslashes. */
2404 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2405 parmp->full_path = TRUE;
2406#endif
2407
2408 /* Add the file to the global argument list. */
2409 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2410 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2411 mch_exit(2);
2412#ifdef FEAT_DIFF
2413 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2414 && !mch_isdir(alist_name(&GARGLIST[0])))
2415 {
2416 char_u *r;
2417
2418 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2419 if (r != NULL)
2420 {
2421 vim_free(p);
2422 p = r;
2423 }
2424 }
2425#endif
2426#if defined(__CYGWIN32__) && !defined(WIN32)
2427 /*
2428 * If vim is invoked by non-Cygwin tools, convert away any
2429 * DOS paths, so things like .swp files are created correctly.
2430 * Look for evidence of non-Cygwin paths before we bother.
2431 * This is only for when using the Unix files.
2432 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002433 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002434 {
2435 char posix_path[PATH_MAX];
2436
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002437# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2438 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2439# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002440 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002441# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002442 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002443 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002444 if (p == NULL)
2445 mch_exit(2);
2446 }
2447#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002448
2449#ifdef USE_FNAME_CASE
2450 /* Make the case of the file name match the actual file. */
2451 fname_case(p, 0);
2452#endif
2453
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002454 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002455#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002456 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002457#else
2458 2 /* add buffer number now and use curbuf */
2459#endif
2460 );
2461
2462#if defined(FEAT_MBYTE) && defined(WIN32)
2463 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002464 /* Remember this argument has been added to the argument list.
2465 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002466 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002467# ifdef FEAT_DIFF
2468 parmp->diff_mode
2469# else
2470 FALSE
2471# endif
2472 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002473 }
2474#endif
2475 }
2476
2477 /*
2478 * If there are no more letters after the current "-", go to next
2479 * argument. argv_idx is set to -1 when the current argument is to be
2480 * skipped.
2481 */
2482 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2483 {
2484 --argc;
2485 ++argv;
2486 argv_idx = 1;
2487 }
2488 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002489
2490#ifdef FEAT_EVAL
2491 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2492 * one. */
2493 if (parmp->n_commands > 0)
2494 {
2495 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2496 if (p != NULL)
2497 {
2498 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2499 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2500 vim_free(p);
2501 }
2502 }
2503#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002504}
2505
2506/*
2507 * Print a warning if stdout is not a terminal.
2508 * When starting in Ex mode and commands come from a file, set Silent mode.
2509 */
2510 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002511check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002512{
2513 int input_isatty; /* is active input a terminal? */
2514
2515 input_isatty = mch_input_isatty();
2516 if (exmode_active)
2517 {
2518 if (!input_isatty)
2519 silent_mode = TRUE;
2520 }
2521 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2522#ifdef FEAT_GUI
2523 /* don't want the delay when started from the desktop */
2524 && !gui.starting
2525#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002526 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002527 {
2528#ifdef NBDEBUG
2529 /*
2530 * This shouldn't be necessary. But if I run netbeans with the log
2531 * output coming to the console and XOpenDisplay fails, I get vim
2532 * trying to start with input/output to my console tty. This fills my
2533 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002534 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002535 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002536 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002537 {
2538 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2539 exit(1);
2540 }
2541#endif
2542 if (!parmp->stdout_isatty)
2543 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2544 if (!input_isatty)
2545 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2546 out_flush();
2547 if (scriptin[0] == NULL)
2548 ui_delay(2000L, TRUE);
2549 TIME_MSG("Warning delay");
2550 }
2551}
2552
2553/*
2554 * Read text from stdin.
2555 */
2556 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002557read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002558{
2559 int i;
2560
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002561#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002562 /* When getting the ATTENTION prompt here, use a dialog */
2563 swap_exists_action = SEA_DIALOG;
2564#endif
2565 no_wait_return = TRUE;
2566 i = msg_didany;
2567 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002568 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002569 no_wait_return = FALSE;
2570 msg_didany = i;
2571 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002572#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002573 check_swap_exists_action();
2574#endif
2575#if !(defined(AMIGA) || defined(MACOS))
2576 /*
2577 * Close stdin and dup it from stderr. Required for GPM to work
2578 * properly, and for running external commands.
2579 * Is there any other system that cannot do this?
2580 */
2581 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002582 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002583#endif
2584}
2585
2586/*
2587 * Create the requested number of windows and edit buffers in them.
2588 * Also does recovery if "recoverymode" set.
2589 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002590 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002591create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002592{
2593#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002594 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002595 int done = 0;
2596
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002597 /*
2598 * Create the number of windows that was requested.
2599 */
2600 if (parmp->window_count == -1) /* was not set */
2601 parmp->window_count = 1;
2602 if (parmp->window_count == 0)
2603 parmp->window_count = GARGCOUNT;
2604 if (parmp->window_count > 1)
2605 {
2606 /* Don't change the windows if there was a command in .vimrc that
2607 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002608 if (parmp->window_layout == 0)
2609 parmp->window_layout = WIN_HOR;
2610 if (parmp->window_layout == WIN_TABS)
2611 {
2612 parmp->window_count = make_tabpages(parmp->window_count);
2613 TIME_MSG("making tab pages");
2614 }
2615 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002616 {
2617 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002618 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002619 TIME_MSG("making windows");
2620 }
2621 else
2622 parmp->window_count = win_count();
2623 }
2624 else
2625 parmp->window_count = 1;
2626#endif
2627
2628 if (recoverymode) /* do recover */
2629 {
2630 msg_scroll = TRUE; /* scroll message up */
2631 ml_recover();
2632 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2633 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002634 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002635 }
2636 else
2637 {
2638 /*
2639 * Open a buffer for windows that don't have one yet.
2640 * Commands in the .vimrc might have loaded a file or split the window.
2641 * Watch out for autocommands that delete a window.
2642 */
2643#ifdef FEAT_AUTOCMD
2644 /*
2645 * Don't execute Win/Buf Enter/Leave autocommands here
2646 */
2647 ++autocmd_no_enter;
2648 ++autocmd_no_leave;
2649#endif
2650#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002651 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002652 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002653 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002654 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002655 {
2656 if (parmp->window_layout == WIN_TABS)
2657 goto_tabpage(1);
2658 else
2659 curwin = firstwin;
2660 }
2661 else if (parmp->window_layout == WIN_TABS)
2662 {
2663 if (curtab->tp_next == NULL)
2664 break;
2665 goto_tabpage(0);
2666 }
2667 else
2668 {
2669 if (curwin->w_next == NULL)
2670 break;
2671 curwin = curwin->w_next;
2672 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002673 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002674#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002675 curbuf = curwin->w_buffer;
2676 if (curbuf->b_ml.ml_mfp == NULL)
2677 {
2678#ifdef FEAT_FOLDING
2679 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2680 if (p_fdls >= 0)
2681 curwin->w_p_fdl = p_fdls;
2682#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002683#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002684 /* When getting the ATTENTION prompt here, use a dialog */
2685 swap_exists_action = SEA_DIALOG;
2686#endif
2687 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002688
2689 /* create memfile, read file */
2690 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002691
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002692#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002693 if (swap_exists_action == SEA_QUIT)
2694 {
2695 if (got_int || only_one_window())
2696 {
2697 /* abort selected or quit and only one window */
2698 did_emsg = FALSE; /* avoid hit-enter prompt */
2699 getout(1);
2700 }
2701 /* We can't close the window, it would disturb what
2702 * happens next. Clear the file name and set the arg
2703 * index to -1 to delete it later. */
2704 setfname(curbuf, NULL, NULL, FALSE);
2705 curwin->w_arg_idx = -1;
2706 swap_exists_action = SEA_NONE;
2707 }
2708 else
2709 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002710#endif
2711#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002712 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002713#endif
2714 }
2715#ifdef FEAT_WINDOWS
2716 ui_breakcheck();
2717 if (got_int)
2718 {
2719 (void)vgetc(); /* only break the file loading, not the rest */
2720 break;
2721 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002722 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002723#endif
2724#ifdef FEAT_WINDOWS
2725 if (parmp->window_layout == WIN_TABS)
2726 goto_tabpage(1);
2727 else
2728 curwin = firstwin;
2729 curbuf = curwin->w_buffer;
2730#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002731#ifdef FEAT_AUTOCMD
2732 --autocmd_no_enter;
2733 --autocmd_no_leave;
2734#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002735 }
2736}
2737
2738#ifdef FEAT_WINDOWS
2739 /*
2740 * If opened more than one window, start editing files in the other
2741 * windows. make_windows() has already opened the windows.
2742 */
2743 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002744edit_buffers(
2745 mparm_T *parmp,
2746 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002747{
2748 int arg_idx; /* index in argument list */
2749 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002750 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002751 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002752
2753# ifdef FEAT_AUTOCMD
2754 /*
2755 * Don't execute Win/Buf Enter/Leave autocommands here
2756 */
2757 ++autocmd_no_enter;
2758 ++autocmd_no_leave;
2759# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002760
2761 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2762 if (curwin->w_arg_idx == -1)
2763 {
2764 win_close(curwin, TRUE);
2765 advance = FALSE;
2766 }
2767
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002768 arg_idx = 1;
2769 for (i = 1; i < parmp->window_count; ++i)
2770 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002771 if (cwd != NULL)
2772 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002773 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2774 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002775 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002776 ++arg_idx;
2777 win_close(curwin, TRUE);
2778 advance = FALSE;
2779 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002780 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002781
Bram Moolenaar84212822006-11-07 21:59:47 +00002782 if (advance)
2783 {
2784 if (parmp->window_layout == WIN_TABS)
2785 {
2786 if (curtab->tp_next == NULL) /* just checking */
2787 break;
2788 goto_tabpage(0);
2789 }
2790 else
2791 {
2792 if (curwin->w_next == NULL) /* just checking */
2793 break;
2794 win_enter(curwin->w_next, FALSE);
2795 }
2796 }
2797 advance = TRUE;
2798
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002799 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002800 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002801 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2802 {
2803 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002804 /* Edit file from arg list, if there is one. When "Quit" selected
2805 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002806# ifdef HAS_SWAP_EXISTS_ACTION
2807 swap_exists_did_quit = FALSE;
2808# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002809 (void)do_ecmd(0, arg_idx < GARGCOUNT
2810 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002811 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002812# ifdef HAS_SWAP_EXISTS_ACTION
2813 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002814 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002815 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002816 if (got_int || only_one_window())
2817 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002818 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002819 did_emsg = FALSE; /* avoid hit-enter prompt */
2820 getout(1);
2821 }
2822 win_close(curwin, TRUE);
2823 advance = FALSE;
2824 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002825# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002826 if (arg_idx == GARGCOUNT - 1)
2827 arg_had_last = TRUE;
2828 ++arg_idx;
2829 }
2830 ui_breakcheck();
2831 if (got_int)
2832 {
2833 (void)vgetc(); /* only break the file loading, not the rest */
2834 break;
2835 }
2836 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002837
2838 if (parmp->window_layout == WIN_TABS)
2839 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002840# ifdef FEAT_AUTOCMD
2841 --autocmd_no_enter;
2842# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002843
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002844 /* make the first window the current window */
2845 win = firstwin;
2846#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2847 /* Avoid making a preview window the current window. */
2848 while (win->w_p_pvw)
2849 {
2850 win = win->w_next;
2851 if (win == NULL)
2852 {
2853 win = firstwin;
2854 break;
2855 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002856 }
2857#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002858 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002859
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002860# ifdef FEAT_AUTOCMD
2861 --autocmd_no_leave;
2862# endif
2863 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002864 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002865 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2866}
2867#endif /* FEAT_WINDOWS */
2868
2869/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002870 * Execute the commands from --cmd arguments "cmds[cnt]".
2871 */
2872 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002873exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002874{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002875 char_u **cmds = parmp->pre_commands;
2876 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002877 int i;
2878
2879 if (cnt > 0)
2880 {
2881 curwin->w_cursor.lnum = 0; /* just in case.. */
2882 sourcing_name = (char_u *)_("pre-vimrc command line");
2883# ifdef FEAT_EVAL
2884 current_SID = SID_CMDARG;
2885# endif
2886 for (i = 0; i < cnt; ++i)
2887 do_cmdline_cmd(cmds[i]);
2888 sourcing_name = NULL;
2889# ifdef FEAT_EVAL
2890 current_SID = 0;
2891# endif
2892 TIME_MSG("--cmd commands");
2893 }
2894}
2895
2896/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002897 * Execute "+", "-c" and "-S" arguments.
2898 */
2899 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002900exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002901{
2902 int i;
2903
2904 /*
2905 * We start commands on line 0, make "vim +/pat file" match a
2906 * pattern on line 1. But don't move the cursor when an autocommand
2907 * with g`" was used.
2908 */
2909 msg_scroll = TRUE;
2910 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2911 curwin->w_cursor.lnum = 0;
2912 sourcing_name = (char_u *)"command line";
2913#ifdef FEAT_EVAL
2914 current_SID = SID_CARG;
2915#endif
2916 for (i = 0; i < parmp->n_commands; ++i)
2917 {
2918 do_cmdline_cmd(parmp->commands[i]);
2919 if (parmp->cmds_tofree[i])
2920 vim_free(parmp->commands[i]);
2921 }
2922 sourcing_name = NULL;
2923#ifdef FEAT_EVAL
2924 current_SID = 0;
2925#endif
2926 if (curwin->w_cursor.lnum == 0)
2927 curwin->w_cursor.lnum = 1;
2928
2929 if (!exmode_active)
2930 msg_scroll = FALSE;
2931
2932#ifdef FEAT_QUICKFIX
2933 /* When started with "-q errorfile" jump to first error again. */
2934 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002935 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002936#endif
2937 TIME_MSG("executing command arguments");
2938}
2939
2940/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002941 * Source startup scripts.
2942 */
2943 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002944source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002945{
2946 int i;
2947
2948 /*
2949 * For "evim" source evim.vim first of all, so that the user can overrule
2950 * any things he doesn't like.
2951 */
2952 if (parmp->evim_mode)
2953 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002954 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002955 TIME_MSG("source evim file");
2956 }
2957
2958 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002959 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002960 * nothing else.
2961 */
2962 if (parmp->use_vimrc != NULL)
2963 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002964 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2965 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002966 {
2967#ifdef FEAT_GUI
2968 if (use_gvimrc == NULL) /* don't load gvimrc either */
2969 use_gvimrc = parmp->use_vimrc;
2970#endif
2971 if (parmp->use_vimrc[2] == 'N')
2972 p_lpl = FALSE; /* don't load plugins either */
2973 }
2974 else
2975 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002976 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002977 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2978 }
2979 }
2980 else if (!silent_mode)
2981 {
2982#ifdef AMIGA
2983 struct Process *proc = (struct Process *)FindTask(0L);
2984 APTR save_winptr = proc->pr_WindowPtr;
2985
2986 /* Avoid a requester here for a volume that doesn't exist. */
2987 proc->pr_WindowPtr = (APTR)-1L;
2988#endif
2989
2990 /*
2991 * Get system wide defaults, if the file name is defined.
2992 */
2993#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002994 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002995#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002996#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002997 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002998#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002999
3000 /*
3001 * Try to read initialization commands from the following places:
3002 * - environment variable VIMINIT
3003 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3004 * - second user vimrc file ($VIM/.vimrc for Dos)
3005 * - environment variable EXINIT
3006 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3007 * - second user exrc file ($VIM/.exrc for Dos)
3008 * The first that exists is used, the rest is ignored.
3009 */
3010 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3011 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003012 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003013#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003014 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3015 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003016#endif
3017#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003018 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3019 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003020#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003021#ifdef USR_VIMRC_FILE4
3022 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3023 DOSO_VIMRC) == FAIL
3024#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003025 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003026 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003027 {
3028#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003029 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003030#endif
3031 }
3032 }
3033
3034 /*
3035 * Read initialization commands from ".vimrc" or ".exrc" in current
3036 * directory. This is only done if the 'exrc' option is set.
3037 * Because of security reasons we disallow shell and write commands
3038 * now, except for unix if the file is owned by the user or 'secure'
3039 * option has been reset in environment of global ".exrc" or ".vimrc".
3040 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3041 * SYS_VIMRC_FILE.
3042 */
3043 if (p_exrc)
3044 {
3045#if defined(UNIX) || defined(VMS)
3046 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3047 if (!file_owned(VIMRC_FILE))
3048#endif
3049 secure = p_secure;
3050
3051 i = FAIL;
3052 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3053 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3054#ifdef USR_VIMRC_FILE2
3055 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3056 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3057#endif
3058#ifdef USR_VIMRC_FILE3
3059 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3060 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3061#endif
3062#ifdef SYS_VIMRC_FILE
3063 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3064 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3065#endif
3066 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003067 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003068
3069 if (i == FAIL)
3070 {
3071#if defined(UNIX) || defined(VMS)
3072 /* if ".exrc" is not owned by user set 'secure' mode */
3073 if (!file_owned(EXRC_FILE))
3074 secure = p_secure;
3075 else
3076 secure = 0;
3077#endif
3078 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3079 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3080#ifdef USR_EXRC_FILE2
3081 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3082 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3083#endif
3084 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003085 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003086 }
3087 }
3088 if (secure == 2)
3089 need_wait_return = TRUE;
3090 secure = 0;
3091#ifdef AMIGA
3092 proc->pr_WindowPtr = save_winptr;
3093#endif
3094 }
3095 TIME_MSG("sourcing vimrc file(s)");
3096}
3097
3098/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003099 * Setup to start using the GUI. Exit with an error when not available.
3100 */
3101 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003102main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003103{
3104#ifdef FEAT_GUI
3105 gui.starting = TRUE; /* start GUI a bit later */
3106#else
3107 mch_errmsg(_(e_nogvim));
3108 mch_errmsg("\n");
3109 mch_exit(2);
3110#endif
3111}
3112
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003113#endif /* NO_VIM_MAIN */
3114
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003115/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003116 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003117 * Returns FAIL if the environment variable was not executed, OK otherwise.
3118 */
3119 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003120process_env(
3121 char_u *env,
3122 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003123{
3124 char_u *initstr;
3125 char_u *save_sourcing_name;
3126 linenr_T save_sourcing_lnum;
3127#ifdef FEAT_EVAL
3128 scid_T save_sid;
3129#endif
3130
3131 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3132 {
3133 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003134 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003135 save_sourcing_name = sourcing_name;
3136 save_sourcing_lnum = sourcing_lnum;
3137 sourcing_name = env;
3138 sourcing_lnum = 0;
3139#ifdef FEAT_EVAL
3140 save_sid = current_SID;
3141 current_SID = SID_ENV;
3142#endif
3143 do_cmdline_cmd(initstr);
3144 sourcing_name = save_sourcing_name;
3145 sourcing_lnum = save_sourcing_lnum;
3146#ifdef FEAT_EVAL
3147 current_SID = save_sid;;
3148#endif
3149 return OK;
3150 }
3151 return FAIL;
3152}
3153
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003154#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003155/*
3156 * Return TRUE if we are certain the user owns the file "fname".
3157 * Used for ".vimrc" and ".exrc".
3158 * Use both stat() and lstat() for extra security.
3159 */
3160 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003161file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003162{
3163 struct stat s;
3164# ifdef UNIX
3165 uid_t uid = getuid();
3166# else /* VMS */
3167 uid_t uid = ((getgid() << 16) | getuid());
3168# endif
3169
3170 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3171# ifdef HAVE_LSTAT
3172 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3173# endif
3174 );
3175}
3176#endif
3177
3178/*
3179 * Give an error message main_errors["n"] and exit.
3180 */
3181 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003182mainerr(
3183 int n, /* one of the ME_ defines */
3184 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003185{
3186#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3187 reset_signals(); /* kill us with CTRL-C here, if you like */
3188#endif
3189
3190 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003191 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003192 mch_errmsg(_(main_errors[n]));
3193 if (str != NULL)
3194 {
3195 mch_errmsg(": \"");
3196 mch_errmsg((char *)str);
3197 mch_errmsg("\"");
3198 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003199 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003200
3201 mch_exit(1);
3202}
3203
3204 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003205mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003206{
3207 mainerr(ME_ARG_MISSING, str);
3208}
3209
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003210#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003211/*
3212 * print a message with three spaces prepended and '\n' appended.
3213 */
3214 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003215main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003216{
3217 mch_msg(" ");
3218 mch_msg(s);
3219 mch_msg("\n");
3220}
3221
3222/*
3223 * Print messages for "vim -h" or "vim --help" and exit.
3224 */
3225 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003226usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003227{
3228 int i;
3229 static char *(use[]) =
3230 {
3231 N_("[file ..] edit specified file(s)"),
3232 N_("- read text from stdin"),
3233 N_("-t tag edit file where tag is defined"),
3234#ifdef FEAT_QUICKFIX
3235 N_("-q [errorfile] edit file with first error")
3236#endif
3237 };
3238
3239#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3240 reset_signals(); /* kill us with CTRL-C here, if you like */
3241#endif
3242
3243 mch_msg(longVersion);
3244 mch_msg(_("\n\nusage:"));
3245 for (i = 0; ; ++i)
3246 {
3247 mch_msg(_(" vim [arguments] "));
3248 mch_msg(_(use[i]));
3249 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3250 break;
3251 mch_msg(_("\n or:"));
3252 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003253#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003254 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003255#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003256
3257 mch_msg(_("\n\nArguments:\n"));
3258 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003259#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003260 main_msg(_("--literal\t\tDon't expand wildcards"));
3261#endif
3262#ifdef FEAT_OLE
3263 main_msg(_("-register\t\tRegister this gvim for OLE"));
3264 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3265#endif
3266#ifdef FEAT_GUI
3267 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3268 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3269#endif
3270 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3271 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003272 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003273 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3274#ifdef FEAT_DIFF
3275 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3276#endif
3277 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3278 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3279 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3280 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3281 main_msg(_("-M\t\t\tModifications in text not allowed"));
3282 main_msg(_("-b\t\t\tBinary mode"));
3283#ifdef FEAT_LISP
3284 main_msg(_("-l\t\t\tLisp mode"));
3285#endif
3286 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3287 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003288 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3289#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003290 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003291#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003292 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3293 main_msg(_("-r\t\t\tList swap files and exit"));
3294 main_msg(_("-r (with file name)\tRecover crashed session"));
3295 main_msg(_("-L\t\t\tSame as -r"));
3296#ifdef AMIGA
3297 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3298 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3299#endif
3300#ifdef FEAT_ARABIC
3301 main_msg(_("-A\t\t\tstart in Arabic mode"));
3302#endif
3303#ifdef FEAT_RIGHTLEFT
3304 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3305#endif
3306#ifdef FEAT_FKMAP
3307 main_msg(_("-F\t\t\tStart in Farsi mode"));
3308#endif
3309 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003310 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003311 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3312#ifdef FEAT_GUI
3313 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3314#endif
3315 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003316#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003317 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003318 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3319 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003320#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003321 main_msg(_("+\t\t\tStart at end of file"));
3322 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003323 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003324 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3325 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3326 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3327 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3328 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3329#ifdef FEAT_CRYPT
3330 main_msg(_("-x\t\t\tEdit encrypted files"));
3331#endif
3332#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3333# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3334 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3335# endif
3336 main_msg(_("-X\t\t\tDo not connect to X server"));
3337#endif
3338#ifdef FEAT_CLIENTSERVER
3339 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3340 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3341 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3342 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003343# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003344 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003345# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003346 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3347 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3348 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3349 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3350#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003351#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003352 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003353#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003354#ifdef FEAT_VIMINFO
3355 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3356#endif
3357 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3358 main_msg(_("--version\t\tPrint version information and exit"));
3359
3360#ifdef FEAT_GUI_X11
3361# ifdef FEAT_GUI_MOTIF
3362 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3363# else
3364# ifdef FEAT_GUI_ATHENA
3365# ifdef FEAT_GUI_NEXTAW
3366 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3367# else
3368 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3369# endif
3370# endif
3371# endif
3372 main_msg(_("-display <display>\tRun vim on <display>"));
3373 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003374 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3375 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3376 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3377 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3378 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3379 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3380 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3381 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3382# ifdef FEAT_GUI_ATHENA
3383 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3384# endif
3385 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3386 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3387 main_msg(_("-xrm <resource>\tSet the specified resource"));
3388#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003389#ifdef FEAT_GUI_GTK
3390 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3391 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3392 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3393 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3394 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003395 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003396 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003397 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003398#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003399#ifdef FEAT_GUI_W32
3400 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003401 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003402#endif
3403
3404#ifdef FEAT_GUI_GNOME
3405 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3406 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003407 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003408 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003409 gui.dofork = FALSE;
3410 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003411 else
3412#endif
3413 mch_exit(0);
3414}
3415
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003416#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003417/*
3418 * Check the result of the ATTENTION dialog:
3419 * When "Quit" selected, exit Vim.
3420 * When "Recover" selected, recover the file.
3421 */
3422 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003423check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003424{
3425 if (swap_exists_action == SEA_QUIT)
3426 getout(1);
3427 handle_swap_exists(NULL);
3428}
3429#endif
3430
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003431#endif
3432
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003433#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003434static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003435
3436static struct timeval prev_timeval;
3437
Bram Moolenaar3f269672009-11-03 11:11:11 +00003438# ifdef WIN3264
3439/*
3440 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3441 */
3442 static int
3443gettimeofday(struct timeval *tv, char *dummy)
3444{
3445 long t = clock();
3446 tv->tv_sec = t / CLOCKS_PER_SEC;
3447 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3448 return 0;
3449}
3450# endif
3451
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003452/*
3453 * Save the previous time before doing something that could nest.
3454 * set "*tv_rel" to the time elapsed so far.
3455 */
3456 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003457time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003458{
3459 *((struct timeval *)tv_rel) = prev_timeval;
3460 gettimeofday(&prev_timeval, NULL);
3461 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3462 - ((struct timeval *)tv_rel)->tv_usec;
3463 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3464 - ((struct timeval *)tv_rel)->tv_sec;
3465 if (((struct timeval *)tv_rel)->tv_usec < 0)
3466 {
3467 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3468 --((struct timeval *)tv_rel)->tv_sec;
3469 }
3470 *(struct timeval *)tv_start = prev_timeval;
3471}
3472
3473/*
3474 * Compute the previous time after doing something that could nest.
3475 * Subtract "*tp" from prev_timeval;
3476 * Note: The arguments are (void *) to avoid trouble with systems that don't
3477 * have struct timeval.
3478 */
3479 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003480time_pop(
3481 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003482{
3483 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3484 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3485 if (prev_timeval.tv_usec < 0)
3486 {
3487 prev_timeval.tv_usec += 1000000;
3488 --prev_timeval.tv_sec;
3489 }
3490}
3491
3492 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003493time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003494{
3495 long usec;
3496 long msec;
3497
3498 usec = now->tv_usec - then->tv_usec;
3499 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3500 usec = usec % 1000L;
3501 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3502}
3503
3504 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003505time_msg(
3506 char *mesg,
3507 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003508 (struct timeval *) */
3509{
3510 static struct timeval start;
3511 struct timeval now;
3512
3513 if (time_fd != NULL)
3514 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003515 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003516 {
3517 gettimeofday(&start, NULL);
3518 prev_timeval = start;
3519 fprintf(time_fd, "\n\ntimes in msec\n");
3520 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3521 fprintf(time_fd, " clock elapsed: other lines\n\n");
3522 }
3523 gettimeofday(&now, NULL);
3524 time_diff(&start, &now);
3525 if (((struct timeval *)tv_start) != NULL)
3526 {
3527 fprintf(time_fd, " ");
3528 time_diff(((struct timeval *)tv_start), &now);
3529 }
3530 fprintf(time_fd, " ");
3531 time_diff(&prev_timeval, &now);
3532 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003533 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003534 }
3535}
3536
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003537#endif
3538
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003539#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003540
3541/*
3542 * Common code for the X command server and the Win32 command server.
3543 */
3544
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003545static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003546
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003547/*
3548 * Do the client-server stuff, unless "--servername ''" was used.
3549 */
3550 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003551exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003552{
3553 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3554 {
3555# ifdef WIN32
3556 /* Initialise the client/server messaging infrastructure. */
3557 serverInitMessaging();
3558# endif
3559
3560 /*
3561 * When a command server argument was found, execute it. This may
3562 * exit Vim when it was successful. Otherwise it's executed further
3563 * on. Remember the encoding used here in "serverStrEnc".
3564 */
3565 if (parmp->serverArg)
3566 {
3567 cmdsrv_main(&parmp->argc, parmp->argv,
3568 parmp->serverName_arg, &parmp->serverStr);
3569# ifdef FEAT_MBYTE
3570 parmp->serverStrEnc = vim_strsave(p_enc);
3571# endif
3572 }
3573
3574 /* If we're still running, get the name to register ourselves.
3575 * On Win32 can register right now, for X11 need to setup the
3576 * clipboard first, it's further down. */
3577 parmp->servername = serverMakeName(parmp->serverName_arg,
3578 parmp->argv[0]);
3579# ifdef WIN32
3580 if (parmp->servername != NULL)
3581 {
3582 serverSetName(parmp->servername);
3583 vim_free(parmp->servername);
3584 }
3585# endif
3586 }
3587}
3588
3589/*
3590 * Prepare for running as a Vim server.
3591 */
3592 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003593prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003594{
3595# if defined(FEAT_X11)
3596 /*
3597 * Register for remote command execution with :serversend and --remote
3598 * unless there was a -X or a --servername '' on the command line.
3599 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003600 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003601 */
3602 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3603# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003604 (gui.in_use
3605# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003606 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003607# endif
3608 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003609# endif
3610 parmp->serverName_arg != NULL))
3611 {
3612 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3613 vim_free(parmp->servername);
3614 TIME_MSG("register server name");
3615 }
3616 else
3617 serverDelayedStartName = parmp->servername;
3618# endif
3619
3620 /*
3621 * Execute command ourselves if we're here because the send failed (or
3622 * else we would have exited above).
3623 */
3624 if (parmp->serverStr != NULL)
3625 {
3626 char_u *p;
3627
3628 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3629 parmp->serverStr, &p));
3630 vim_free(p);
3631 }
3632}
3633
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003634 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003635cmdsrv_main(
3636 int *argc,
3637 char **argv,
3638 char_u *serverName_arg,
3639 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003640{
3641 char_u *res;
3642 int i;
3643 char_u *sname;
3644 int ret;
3645 int didone = FALSE;
3646 int exiterr = 0;
3647 char **newArgV = argv + 1;
3648 int newArgC = 1,
3649 Argc = *argc;
3650 int argtype;
3651#define ARGTYPE_OTHER 0
3652#define ARGTYPE_EDIT 1
3653#define ARGTYPE_EDIT_WAIT 2
3654#define ARGTYPE_SEND 3
3655 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003656 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003657# ifndef FEAT_X11
3658 HWND srv;
3659# else
3660 Window srv;
3661
3662 setup_term_clip();
3663# endif
3664
3665 sname = serverMakeName(serverName_arg, argv[0]);
3666 if (sname == NULL)
3667 return;
3668
3669 /*
3670 * Execute the command server related arguments and remove them
3671 * from the argc/argv array; We may have to return into main()
3672 */
3673 for (i = 1; i < Argc; i++)
3674 {
3675 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003676 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003677 {
3678 for (; i < *argc; i++)
3679 {
3680 *newArgV++ = argv[i];
3681 newArgC++;
3682 }
3683 break;
3684 }
3685
Bram Moolenaareb94e552006-03-11 21:35:11 +00003686 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003687 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003688 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3689 {
3690 char *p = argv[i] + 8;
3691
3692 argtype = ARGTYPE_EDIT;
3693 while (*p != NUL)
3694 {
3695 if (STRNICMP(p, "-wait", 5) == 0)
3696 {
3697 argtype = ARGTYPE_EDIT_WAIT;
3698 p += 5;
3699 }
3700 else if (STRNICMP(p, "-silent", 7) == 0)
3701 {
3702 silent = TRUE;
3703 p += 7;
3704 }
3705 else if (STRNICMP(p, "-tab", 4) == 0)
3706 {
3707 tabs = TRUE;
3708 p += 4;
3709 }
3710 else
3711 {
3712 argtype = ARGTYPE_OTHER;
3713 break;
3714 }
3715 }
3716 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003717 else
3718 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003719
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003720 if (argtype != ARGTYPE_OTHER)
3721 {
3722 if (i == *argc - 1)
3723 mainerr_arg_missing((char_u *)argv[i]);
3724 if (argtype == ARGTYPE_SEND)
3725 {
3726 *serverStr = (char_u *)argv[i + 1];
3727 i++;
3728 }
3729 else
3730 {
3731 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003732 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003733 if (*serverStr == NULL)
3734 {
3735 /* Probably out of memory, exit. */
3736 didone = TRUE;
3737 exiterr = 1;
3738 break;
3739 }
3740 Argc = i;
3741 }
3742# ifdef FEAT_X11
3743 if (xterm_dpy == NULL)
3744 {
3745 mch_errmsg(_("No display"));
3746 ret = -1;
3747 }
3748 else
3749 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3750 NULL, &srv, 0, 0, silent);
3751# else
3752 /* Win32 always works? */
3753 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3754# endif
3755 if (ret < 0)
3756 {
3757 if (argtype == ARGTYPE_SEND)
3758 {
3759 /* Failed to send, abort. */
3760 mch_errmsg(_(": Send failed.\n"));
3761 didone = TRUE;
3762 exiterr = 1;
3763 }
3764 else if (!silent)
3765 /* Let vim start normally. */
3766 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3767 break;
3768 }
3769
3770# ifdef FEAT_GUI_W32
3771 /* Guess that when the server name starts with "g" it's a GUI
3772 * server, which we can bring to the foreground here.
3773 * Foreground() in the server doesn't work very well. */
3774 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3775 SetForegroundWindow(srv);
3776# endif
3777
3778 /*
3779 * For --remote-wait: Wait until the server did edit each
3780 * file. Also detect that the server no longer runs.
3781 */
3782 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3783 {
3784 int numFiles = *argc - i - 1;
3785 int j;
3786 char_u *done = alloc(numFiles);
3787 char_u *p;
3788# ifdef FEAT_GUI_W32
3789 NOTIFYICONDATA ni;
3790 int count = 0;
3791 extern HWND message_window;
3792# endif
3793
3794 if (numFiles > 0 && argv[i + 1][0] == '+')
3795 /* Skip "+cmd" argument, don't wait for it to be edited. */
3796 --numFiles;
3797
3798# ifdef FEAT_GUI_W32
3799 ni.cbSize = sizeof(ni);
3800 ni.hWnd = message_window;
3801 ni.uID = 0;
3802 ni.uFlags = NIF_ICON|NIF_TIP;
3803 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3804 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3805 Shell_NotifyIcon(NIM_ADD, &ni);
3806# endif
3807
3808 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003809 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003810 while (memchr(done, 0, numFiles) != NULL)
3811 {
3812# ifdef WIN32
3813 p = serverGetReply(srv, NULL, TRUE, TRUE);
3814 if (p == NULL)
3815 break;
3816# else
3817 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3818 break;
3819# endif
3820 j = atoi((char *)p);
3821 if (j >= 0 && j < numFiles)
3822 {
3823# ifdef FEAT_GUI_W32
3824 ++count;
3825 sprintf(ni.szTip, _("%d of %d edited"),
3826 count, numFiles);
3827 Shell_NotifyIcon(NIM_MODIFY, &ni);
3828# endif
3829 done[j] = 1;
3830 }
3831 }
3832# ifdef FEAT_GUI_W32
3833 Shell_NotifyIcon(NIM_DELETE, &ni);
3834# endif
3835 }
3836 }
3837 else if (STRICMP(argv[i], "--remote-expr") == 0)
3838 {
3839 if (i == *argc - 1)
3840 mainerr_arg_missing((char_u *)argv[i]);
3841# ifdef WIN32
3842 /* Win32 always works? */
3843 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3844 &res, NULL, 1, FALSE) < 0)
3845# else
3846 if (xterm_dpy == NULL)
3847 mch_errmsg(_("No display: Send expression failed.\n"));
3848 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3849 &res, NULL, 1, 1, FALSE) < 0)
3850# endif
3851 {
3852 if (res != NULL && *res != NUL)
3853 {
3854 /* Output error from remote */
3855 mch_errmsg((char *)res);
3856 vim_free(res);
3857 res = NULL;
3858 }
3859 mch_errmsg(_(": Send expression failed.\n"));
3860 }
3861 }
3862 else if (STRICMP(argv[i], "--serverlist") == 0)
3863 {
3864# ifdef WIN32
3865 /* Win32 always works? */
3866 res = serverGetVimNames();
3867# else
3868 if (xterm_dpy != NULL)
3869 res = serverGetVimNames(xterm_dpy);
3870# endif
3871 if (called_emsg)
3872 mch_errmsg("\n");
3873 }
3874 else if (STRICMP(argv[i], "--servername") == 0)
3875 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003876 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003877 i++;
3878 continue;
3879 }
3880 else
3881 {
3882 *newArgV++ = argv[i];
3883 newArgC++;
3884 continue;
3885 }
3886 didone = TRUE;
3887 if (res != NULL && *res != NUL)
3888 {
3889 mch_msg((char *)res);
3890 if (res[STRLEN(res) - 1] != '\n')
3891 mch_msg("\n");
3892 }
3893 vim_free(res);
3894 }
3895
3896 if (didone)
3897 {
3898 display_errors(); /* display any collected messages */
3899 exit(exiterr); /* Mission accomplished - get out */
3900 }
3901
3902 /* Return back into main() */
3903 *argc = newArgC;
3904 vim_free(sname);
3905}
3906
3907/*
3908 * Build a ":drop" command to send to a Vim server.
3909 */
3910 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003911build_drop_cmd(
3912 int filec,
3913 char **filev,
3914 int tabs, /* Use ":tab drop" instead of ":drop". */
3915 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003916{
3917 garray_T ga;
3918 int i;
3919 char_u *inicmd = NULL;
3920 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003921 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003922 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003923
3924 if (filec > 0 && filev[0][0] == '+')
3925 {
3926 inicmd = (char_u *)filev[0] + 1;
3927 filev++;
3928 filec--;
3929 }
3930 /* Check if we have at least one argument. */
3931 if (filec <= 0)
3932 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003933
3934 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003935 cwd = alloc(MAXPATHL);
3936 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003937 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003938 if (mch_dirname(cwd, MAXPATHL) != OK)
3939 {
3940 vim_free(cwd);
3941 return NULL;
3942 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003943 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003944#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003945 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00003946#else
3947 PATH_ESC_CHARS,
3948#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003949 '\\', TRUE);
3950 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003951 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003952 return NULL;
3953 ga_init2(&ga, 1, 100);
3954 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003955 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003956
3957 /* Call inputsave() so that a prompt for an encryption key works. */
3958 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3959 if (tabs)
3960 ga_concat(&ga, (char_u *)"tab ");
3961 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003962 for (i = 0; i < filec; i++)
3963 {
3964 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003965 * do it again in the Vim server. On MS-Windows only escape
3966 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003967 p = vim_strsave_escaped((char_u *)filev[i],
3968#ifdef UNIX
3969 PATH_ESC_CHARS
3970#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003971 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003972#endif
3973 );
3974 if (p == NULL)
3975 {
3976 vim_free(ga.ga_data);
3977 return NULL;
3978 }
3979 ga_concat(&ga, (char_u *)" ");
3980 ga_concat(&ga, p);
3981 vim_free(p);
3982 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003983 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3984
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003985 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3986 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003987 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3988
3989 /* Switch back to the correct current directory (prior to temporary path
3990 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003991 * correct after the :drop command. With line breaks and spaces:
3992 * if !exists('+acd') || !&acd
3993 * if haslocaldir()
3994 * cd -
3995 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02003996 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003997 * cd -
3998 * endif
3999 * endif
4000 */
4001 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004002 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004003 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004004 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004005 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004006
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004007 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004008 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4009 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004010 if (inicmd != NULL)
4011 {
4012 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4013 * the following commands to be inserted as text. Use a "|",
4014 * hopefully "inicmd" does allow this... */
4015 ga_concat(&ga, inicmd);
4016 ga_concat(&ga, (char_u *)"|");
4017 }
4018 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4019 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004020 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004021 ga_append(&ga, NUL);
4022 return ga.ga_data;
4023}
4024
4025/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004026 * Make our basic server name: use the specified "arg" if given, otherwise use
4027 * the tail of the command "cmd" we were started with.
4028 * Return the name in allocated memory. This doesn't include a serial number.
4029 */
4030 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004031serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004032{
4033 char_u *p;
4034
4035 if (arg != NULL && *arg != NUL)
4036 p = vim_strsave_up(arg);
4037 else
4038 {
4039 p = vim_strsave_up(gettail((char_u *)cmd));
4040 /* Remove .exe or .bat from the name. */
4041 if (p != NULL && vim_strchr(p, '.') != NULL)
4042 *vim_strchr(p, '.') = NUL;
4043 }
4044 return p;
4045}
4046#endif /* FEAT_CLIENTSERVER */
4047
4048#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4049/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004050 * Replace termcodes such as <CR> and insert as key presses if there is room.
4051 */
4052 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004053server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004054{
4055 char_u *ptr = NULL;
4056 char_u *cpo_save = p_cpo;
4057
4058 /* Set 'cpoptions' the way we want it.
4059 * B set - backslashes are *not* treated specially
4060 * k set - keycodes are *not* reverse-engineered
4061 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004062 * The last but one parameter of replace_termcodes() is TRUE so that the
4063 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004064 */
4065 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004066 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004067 p_cpo = cpo_save;
4068
4069 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4070 {
4071 /*
4072 * Add the string to the input stream.
4073 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4074 *
4075 * First clear typed characters from the typeahead buffer, there could
4076 * be half a mapping there. Then append to the existing string, so
4077 * that multiple commands from a client are concatenated.
4078 */
4079 if (typebuf.tb_maplen < typebuf.tb_len)
4080 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4081 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4082
4083 /* Let input_available() know we inserted text in the typeahead
4084 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004085 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004086 }
4087 vim_free((char_u *)ptr);
4088}
4089
4090/*
4091 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004092 */
4093 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004094eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004095{
4096 char_u *res;
4097 int save_dbl = debug_break_level;
4098 int save_ro = redir_off;
4099
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004100 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4101 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004102 debug_break_level = -1;
4103 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004104 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4105 * to be typed. Do generate errors so that try/catch works. */
4106 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004107
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004108 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004109
4110 debug_break_level = save_dbl;
4111 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004112 --emsg_silent;
4113 if (emsg_silent < 0)
4114 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004115
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004116 /* A client can tell us to redraw, but not to display the cursor, so do
4117 * that here. */
4118 setcursor();
4119 out_flush();
4120#ifdef FEAT_GUI
4121 if (gui.in_use)
4122 gui_update_cursor(FALSE, FALSE);
4123#endif
4124
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004125 return res;
4126}
4127
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004128/*
4129 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4130 * return an allocated string. Otherwise return "data".
4131 * "*tofree" is set to the result when it needs to be freed later.
4132 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004133 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004134serverConvert(
4135 char_u *client_enc UNUSED,
4136 char_u *data,
4137 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004138{
4139 char_u *res = data;
4140
4141 *tofree = NULL;
4142# ifdef FEAT_MBYTE
4143 if (client_enc != NULL && p_enc != NULL)
4144 {
4145 vimconv_T vimconv;
4146
4147 vimconv.vc_type = CONV_NONE;
4148 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4149 && vimconv.vc_type != CONV_NONE)
4150 {
4151 res = string_convert(&vimconv, data, NULL);
4152 if (res == NULL)
4153 res = data;
4154 else
4155 *tofree = res;
4156 }
4157 convert_setup(&vimconv, NULL, NULL);
4158 }
4159# endif
4160 return res;
4161}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004162#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004163
4164/*
4165 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4166 */
4167#if defined(FEAT_FKMAP) || defined(PROTO)
4168# include "farsi.c"
4169#endif
4170
4171/*
4172 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4173 */
4174#if defined(FEAT_ARABIC) || defined(PROTO)
4175# include "arabic.c"
4176#endif