blob: 83e55d1a70185d7fec3b24a2315be8784ce2ff2a [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 "**". */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100631 source_runtime((char_u *)"plugin/*.vim", DIP_ALL);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000632# else
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100633 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL);
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
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100637 ex_packloadall(NULL);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100638 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 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001179# ifdef FEAT_AUTOCMD
1180 && !equalpos(last_cursormoved, curwin->w_cursor)
1181# endif
1182 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001183 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001184# ifdef FEAT_AUTOCMD
1185 if (has_cursormoved())
1186 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1187 FALSE, curbuf);
1188# endif
1189# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001190 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001191 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001192# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001193 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001194# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001195 conceal_new_cursor_line = curwin->w_cursor.lnum;
1196 conceal_update_lines = TRUE;
1197 }
1198# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001199# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001200 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001201# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001202 }
1203#endif
1204
Bram Moolenaar186628f2013-03-19 13:33:23 +01001205#ifdef FEAT_AUTOCMD
1206 /* Trigger TextChanged if b_changedtick differs. */
1207 if (!finish_op && has_textchanged()
1208 && last_changedtick != curbuf->b_changedtick)
1209 {
1210 if (last_changedtick_buf == curbuf)
1211 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1212 FALSE, curbuf);
1213 last_changedtick_buf = curbuf;
1214 last_changedtick = curbuf->b_changedtick;
1215 }
1216#endif
1217
Bram Moolenaar33aec762006-01-22 23:30:12 +00001218#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1219 /* Scroll-binding for diff mode may have been postponed until
1220 * here. Avoids doing it for every change. */
1221 if (diff_need_scrollbind)
1222 {
1223 check_scrollbind((linenr_T)0, 0L);
1224 diff_need_scrollbind = FALSE;
1225 }
1226#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001227#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001228 /* Include a closed fold completely in the Visual area. */
1229 foldAdjustVisual();
1230#endif
1231#ifdef FEAT_FOLDING
1232 /*
1233 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1234 * contain the cursor.
1235 * When 'foldopen' is "all", open the fold(s) under the cursor.
1236 * This may mark the window for redrawing.
1237 */
1238 if (hasAnyFolding(curwin) && !char_avail())
1239 {
1240 foldCheckClose();
1241 if (fdo_flags & FDO_ALL)
1242 foldOpenCursor();
1243 }
1244#endif
1245
1246 /*
1247 * Before redrawing, make sure w_topline is correct, and w_leftcol
1248 * if lines don't wrap, and w_skipcol if lines wrap.
1249 */
1250 update_topline();
1251 validate_cursor();
1252
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001253 if (VIsual_active)
1254 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001255 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001256 update_screen(0);
1257 else if (redraw_cmdline || clear_cmdline)
1258 showmode();
1259#ifdef FEAT_WINDOWS
1260 redraw_statuslines();
1261#endif
1262#ifdef FEAT_TITLE
1263 if (need_maketitle)
1264 maketitle();
1265#endif
1266 /* display message after redraw */
1267 if (keep_msg != NULL)
1268 {
1269 char_u *p;
1270
1271 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001272 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1273 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001274 p = keep_msg;
1275 msg_attr(p, keep_msg_attr);
1276 vim_free(p);
1277 }
1278 if (need_fileinfo) /* show file info after redraw */
1279 {
1280 fileinfo(FALSE, TRUE, FALSE);
1281 need_fileinfo = FALSE;
1282 }
1283
1284 emsg_on_display = FALSE; /* can delete error message now */
1285 did_emsg = FALSE;
1286 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001287 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001288 showruler(FALSE);
1289
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001290# if defined(FEAT_CONCEAL)
1291 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001292 && (conceal_old_cursor_line != conceal_new_cursor_line
1293 || conceal_cursor_line(curwin)
1294 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001295 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001296 if (conceal_old_cursor_line != conceal_new_cursor_line
1297 && conceal_old_cursor_line
1298 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001299 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001300 update_single_line(curwin, conceal_new_cursor_line);
1301 curwin->w_valid &= ~VALID_CROW;
1302 }
1303# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001304 setcursor();
1305 cursor_on();
1306
1307 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001308
1309#ifdef STARTUPTIME
1310 /* Now that we have drawn the first screen all the startup stuff
1311 * has been done, close any file for startup messages. */
1312 if (time_fd != NULL)
1313 {
1314 TIME_MSG("first screen update");
1315 TIME_MSG("--- VIM STARTED ---");
1316 fclose(time_fd);
1317 time_fd = NULL;
1318 }
1319#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001320 }
1321#ifdef FEAT_GUI
1322 if (need_mouse_correct)
1323 gui_mouse_correct();
1324#endif
1325
1326 /*
1327 * Update w_curswant if w_set_curswant has been set.
1328 * Postponed until here to avoid computing w_virtcol too often.
1329 */
1330 update_curswant();
1331
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001332#ifdef FEAT_EVAL
1333 /*
1334 * May perform garbage collection when waiting for a character, but
1335 * only at the very toplevel. Otherwise we may be using a List or
1336 * Dict internally somewhere.
1337 * "may_garbage_collect" is reset in vgetc() which is invoked through
1338 * do_exmode() and normal_cmd().
1339 */
1340 may_garbage_collect = (!cmdwin && !noexmode);
1341#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001342 /*
1343 * If we're invoked as ex, do a round of ex commands.
1344 * Otherwise, get and execute a normal mode command.
1345 */
1346 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001347 {
1348 if (noexmode) /* End of ":global/path/visual" commands */
1349 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001350 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001351 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001352 else
1353 normal_cmd(&oa, TRUE);
1354 }
1355}
1356
1357
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001358#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001359/*
1360 * Exit, but leave behind swap files for modified buffers.
1361 */
1362 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001363getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001364{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001365# if defined(SIGHUP) && defined(SIG_IGN)
1366 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1367 * makes Vim exit and then handling SIGHUP causes various reentrance
1368 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001369 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001370# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001371
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001372 ml_close_notmod(); /* close all not-modified buffers */
1373 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1374 ml_close_all(FALSE); /* close all memfiles, without deleting */
1375 getout(exitval); /* exit Vim properly */
1376}
1377#endif
1378
1379
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001380/*
1381 * Exit properly.
1382 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001383 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001384getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001385{
1386#ifdef FEAT_AUTOCMD
1387 buf_T *buf;
1388 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001389 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001390#endif
1391
1392 exiting = TRUE;
1393
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001394 /* When running in Ex mode an error causes us to exit with a non-zero exit
1395 * code. POSIX requires this, although it's not 100% clear from the
1396 * standard. */
1397 if (exmode_active)
1398 exitval += ex_exitval;
1399
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001400 /* Position the cursor on the last screen line, below all the text */
1401#ifdef FEAT_GUI
1402 if (!gui.in_use)
1403#endif
1404 windgoto((int)Rows - 1, 0);
1405
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001406#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1407 /* Optionally print hashtable efficiency. */
1408 hash_debug_results();
1409#endif
1410
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001411#ifdef FEAT_GUI
1412 msg_didany = FALSE;
1413#endif
1414
1415#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001416 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001417 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001418 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1419# if defined FEAT_WINDOWS
1420 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001421 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001422 next_tp = tp->tp_next;
1423 for (wp = (tp == curtab)
1424 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001425 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001426 if (wp->w_buffer == NULL)
1427 /* Autocmd must have close the buffer already, skip. */
1428 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001429 buf = wp->w_buffer;
1430 if (buf->b_changedtick != -1)
1431 {
1432 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1433 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001434 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001435 /* start all over, autocommands may mess up the lists */
1436 next_tp = first_tabpage;
1437 break;
1438 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001439 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001440 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001441# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001442 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1443 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001444# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001445
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001446 /* Trigger BufUnload for buffers that are loaded */
1447 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1448 if (buf->b_ml.ml_mfp != NULL)
1449 {
1450 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001451 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001452 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1453 break;
1454 }
1455 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1456 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001457#endif
1458
1459#ifdef FEAT_VIMINFO
1460 if (*p_viminfo != NUL)
1461 /* Write out the registers, history, marks etc, to the viminfo file */
1462 write_viminfo(NULL, FALSE);
1463#endif
1464
1465#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001466 if (get_vim_var_nr(VV_DYING) <= 1)
1467 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001468#endif
1469
Bram Moolenaar05159a02005-02-26 23:04:13 +00001470#ifdef FEAT_PROFILE
1471 profile_dump();
1472#endif
1473
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001474 if (did_emsg
1475#ifdef FEAT_GUI
1476 || (gui.in_use && msg_didany && p_verbose > 0)
1477#endif
1478 )
1479 {
1480 /* give the user a chance to read the (error) message */
1481 no_wait_return = FALSE;
1482 wait_return(FALSE);
1483 }
1484
1485#ifdef FEAT_AUTOCMD
1486 /* Position the cursor again, the autocommands may have moved it */
1487# ifdef FEAT_GUI
1488 if (!gui.in_use)
1489# endif
1490 windgoto((int)Rows - 1, 0);
1491#endif
1492
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001493#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001494 job_stop_on_exit();
1495#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001496#ifdef FEAT_LUA
1497 lua_end();
1498#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001499#ifdef FEAT_MZSCHEME
1500 mzscheme_end();
1501#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001502#ifdef FEAT_TCL
1503 tcl_end();
1504#endif
1505#ifdef FEAT_RUBY
1506 ruby_end();
1507#endif
1508#ifdef FEAT_PYTHON
1509 python_end();
1510#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001511#ifdef FEAT_PYTHON3
1512 python3_end();
1513#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001514#ifdef FEAT_PERL
1515 perl_end();
1516#endif
1517#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1518 iconv_end();
1519#endif
1520#ifdef FEAT_NETBEANS_INTG
1521 netbeans_end();
1522#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001523#ifdef FEAT_CSCOPE
1524 cs_end();
1525#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001526#ifdef FEAT_EVAL
1527 if (garbage_collect_at_exit)
1528 garbage_collect();
1529#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001530#if defined(WIN32) && defined(FEAT_MBYTE)
1531 free_cmd_argsW();
1532#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001533
1534 mch_exit(exitval);
1535}
1536
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001537#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001538/*
1539 * Get a (optional) count for a Vim argument.
1540 */
1541 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001542get_number_arg(
1543 char_u *p, /* pointer to argument */
1544 int *idx, /* index in argument, is incremented */
1545 int def) /* default value */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001546{
1547 if (vim_isdigit(p[*idx]))
1548 {
1549 def = atoi((char *)&(p[*idx]));
1550 while (vim_isdigit(p[*idx]))
1551 *idx = *idx + 1;
1552 }
1553 return def;
1554}
1555
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001556#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1557/*
1558 * Setup to use the current locale (for ctype() and many other things).
1559 */
1560 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001561init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001562{
1563 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001564
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001565# ifdef FEAT_GUI_GTK
1566 /* Tell Gtk not to change our locale settings. */
1567 gtk_disable_setlocale();
1568# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1570 /* Make sure strtod() uses a decimal point, not a comma. */
1571 setlocale(LC_NUMERIC, "C");
1572# endif
1573
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001574# ifdef WIN32
1575 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1576 * text while it's expecting text in the current locale. This call avoids
1577 * that. */
1578 setlocale(LC_CTYPE, "C");
1579# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001580
1581# ifdef FEAT_GETTEXT
1582 {
1583 int mustfree = FALSE;
1584 char_u *p;
1585
1586# ifdef DYNAMIC_GETTEXT
1587 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001588 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001589# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001590 /* expand_env() doesn't work yet, because g_chartab[] is not
1591 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001592 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1593 if (p != NULL && *p != NUL)
1594 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001595 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001596 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1597 }
1598 if (mustfree)
1599 vim_free(p);
1600 textdomain(VIMPACKAGE);
1601 }
1602# endif
1603}
1604#endif
1605
1606/*
1607 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1608 * If the executable name starts with "r" we disable shell commands.
1609 * If the next character is "e" we run in Easy mode.
1610 * If the next character is "g" we run the GUI version.
1611 * If the next characters are "view" we start in readonly mode.
1612 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1613 * If the next characters are "ex" we start in Ex mode. If it's followed
1614 * by "im" use improved Ex mode.
1615 */
1616 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001617parse_command_name(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001618{
1619 char_u *initstr;
1620
1621 initstr = gettail((char_u *)parmp->argv[0]);
1622
1623#ifdef MACOS_X_UNIX
1624 /* An issue has been seen when launching Vim in such a way that
1625 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1626 * executable or a symbolic link of it. Until this issue is resolved
1627 * we prohibit the GUI from being used.
1628 */
1629 if (STRCMP(initstr, parmp->argv[0]) == 0)
1630 disallow_gui = TRUE;
1631
1632 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001633 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001634#endif
1635
1636#ifdef FEAT_EVAL
1637 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaara1706c92014-04-01 19:55:49 +02001638 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001639#endif
1640
1641 if (TOLOWER_ASC(initstr[0]) == 'r')
1642 {
1643 restricted = TRUE;
1644 ++initstr;
1645 }
1646
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001647 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001648 if (TOLOWER_ASC(initstr[0]) == 'e'
1649 && (TOLOWER_ASC(initstr[1]) == 'v'
1650 || TOLOWER_ASC(initstr[1]) == 'g'))
1651 {
1652#ifdef FEAT_GUI
1653 gui.starting = TRUE;
1654#endif
1655 parmp->evim_mode = TRUE;
1656 ++initstr;
1657 }
1658
Bram Moolenaar806875d2008-09-18 18:57:10 +00001659 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1660 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001661 {
1662 main_start_gui();
1663#ifdef FEAT_GUI
1664 ++initstr;
1665#endif
1666 }
1667
1668 if (STRNICMP(initstr, "view", 4) == 0)
1669 {
1670 readonlymode = TRUE;
1671 curbuf->b_p_ro = TRUE;
1672 p_uc = 10000; /* don't update very often */
1673 initstr += 4;
1674 }
1675 else if (STRNICMP(initstr, "vim", 3) == 0)
1676 initstr += 3;
1677
1678 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1679 if (STRICMP(initstr, "diff") == 0)
1680 {
1681#ifdef FEAT_DIFF
1682 parmp->diff_mode = TRUE;
1683#else
1684 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1685 mch_errmsg("\n");
1686 mch_exit(2);
1687#endif
1688 }
1689
1690 if (STRNICMP(initstr, "ex", 2) == 0)
1691 {
1692 if (STRNICMP(initstr + 2, "im", 2) == 0)
1693 exmode_active = EXMODE_VIM;
1694 else
1695 exmode_active = EXMODE_NORMAL;
1696 change_compatible(TRUE); /* set 'compatible' */
1697 }
1698}
1699
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001700/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001701 * Get the name of the display, before gui_prepare() removes it from
1702 * argv[]. Used for the xterm-clipboard display.
1703 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001704 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001705 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001706 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001707early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001708{
Bram Moolenaar03005972008-11-20 13:12:36 +00001709#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1710 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001711 int argc = parmp->argc;
1712 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001713 int i;
1714
1715 for (i = 1; i < argc; i++)
1716 {
1717 if (STRCMP(argv[i], "--") == 0)
1718 break;
1719# ifdef FEAT_XCLIPBOARD
1720 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001721# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001722 || STRICMP(argv[i], "--display") == 0
1723# endif
1724 )
1725 {
1726 if (i == argc - 1)
1727 mainerr_arg_missing((char_u *)argv[i]);
1728 xterm_display = argv[++i];
1729 }
1730# endif
1731# ifdef FEAT_CLIENTSERVER
1732 else if (STRICMP(argv[i], "--servername") == 0)
1733 {
1734 if (i == argc - 1)
1735 mainerr_arg_missing((char_u *)argv[i]);
1736 parmp->serverName_arg = (char_u *)argv[++i];
1737 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001738 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001739 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001740 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001741 {
1742 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001743# ifdef FEAT_GUI
1744 if (strstr(argv[i], "-wait") != 0)
1745 /* don't fork() when starting the GUI to edit files ourself */
1746 gui.dofork = FALSE;
1747# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001748 }
1749# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001750
1751# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1752# ifdef FEAT_GUI_W32
1753 else if (STRICMP(argv[i], "--windowid") == 0)
1754# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001755 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001756# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001757 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001758 long_u id;
1759 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001760
1761 if (i == argc - 1)
1762 mainerr_arg_missing((char_u *)argv[i]);
1763 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001764 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001765 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001766 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001767 if (count != 1)
1768 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1769 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001770# ifdef FEAT_GUI_W32
1771 win_socket_id = id;
1772# else
1773 gtk_socket_id = id;
1774# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001775 i++;
1776 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001777# endif
1778# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001779 else if (STRICMP(argv[i], "--echo-wid") == 0)
1780 echo_wid_arg = TRUE;
1781# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001782# ifndef FEAT_NETBEANS_INTG
1783 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001784 {
1785 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1786 mch_exit(2);
1787 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001788# endif
1789
Bram Moolenaar58d98232005-07-23 22:25:46 +00001790 }
1791#endif
1792}
1793
1794/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001795 * Scan the command line arguments.
1796 */
1797 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001798command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001799{
1800 int argc = parmp->argc;
1801 char **argv = parmp->argv;
1802 int argv_idx; /* index in argv[n][] */
1803 int had_minmin = FALSE; /* found "--" argument */
1804 int want_argument; /* option argument with argument */
1805 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001806 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001807 long n;
1808
1809 --argc;
1810 ++argv;
1811 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1812 while (argc > 0)
1813 {
1814 /*
1815 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1816 */
1817 if (argv[0][0] == '+' && !had_minmin)
1818 {
1819 if (parmp->n_commands >= MAX_ARG_CMDS)
1820 mainerr(ME_EXTRA_CMD, NULL);
1821 argv_idx = -1; /* skip to next argument */
1822 if (argv[0][1] == NUL)
1823 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1824 else
1825 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1826 }
1827
1828 /*
1829 * Optional argument.
1830 */
1831 else if (argv[0][0] == '-' && !had_minmin)
1832 {
1833 want_argument = FALSE;
1834 c = argv[0][argv_idx++];
1835#ifdef VMS
1836 /*
1837 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1838 * and "-/X" as "-X".
1839 */
1840 if (c == '/')
1841 {
1842 c = argv[0][argv_idx++];
1843 c = TOUPPER_ASC(c);
1844 }
1845 else
1846 c = TOLOWER_ASC(c);
1847#endif
1848 switch (c)
1849 {
1850 case NUL: /* "vim -" read from stdin */
1851 /* "ex -" silent mode */
1852 if (exmode_active)
1853 silent_mode = TRUE;
1854 else
1855 {
1856 if (parmp->edit_type != EDIT_NONE)
1857 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1858 parmp->edit_type = EDIT_STDIN;
1859 read_cmd_fd = 2; /* read from stderr instead of stdin */
1860 }
1861 argv_idx = -1; /* skip to next argument */
1862 break;
1863
1864 case '-': /* "--" don't take any more option arguments */
1865 /* "--help" give help message */
1866 /* "--version" give version message */
1867 /* "--literal" take files literally */
1868 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001869 /* "--not-a-term" don't warn for not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001870 /* "--noplugin[s]" skip plugins */
1871 /* "--cmd <cmd>" execute cmd before vimrc */
1872 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1873 usage();
1874 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1875 {
1876 Columns = 80; /* need to init Columns */
1877 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1878 list_version();
1879 msg_putchar('\n');
1880 msg_didout = FALSE;
1881 mch_exit(0);
1882 }
1883 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1884 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001885#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001886 parmp->literal = TRUE;
1887#endif
1888 }
1889 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1890 {
1891#ifdef FEAT_GUI
1892 gui.dofork = FALSE; /* don't fork() when starting GUI */
1893#endif
1894 }
1895 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1896 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001897 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1898 parmp->not_a_term = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001899 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1900 {
1901 want_argument = TRUE;
1902 argv_idx += 3;
1903 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001904 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1905 {
1906 want_argument = TRUE;
1907 argv_idx += 11;
1908 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001909#ifdef FEAT_CLIENTSERVER
1910 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1911 ; /* already processed -- no arg */
1912 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1913 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1914 {
1915 /* already processed -- snatch the following arg */
1916 if (argc > 1)
1917 {
1918 --argc;
1919 ++argv;
1920 }
1921 }
1922#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001923#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1924# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001925 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001926# else
1927 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1928# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001929 {
1930 /* already processed -- snatch the following arg */
1931 if (argc > 1)
1932 {
1933 --argc;
1934 ++argv;
1935 }
1936 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001937#endif
1938#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001939 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1940 {
1941 /* already processed, skip */
1942 }
1943#endif
1944 else
1945 {
1946 if (argv[0][argv_idx])
1947 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1948 had_minmin = TRUE;
1949 }
1950 if (!want_argument)
1951 argv_idx = -1; /* skip to next argument */
1952 break;
1953
1954 case 'A': /* "-A" start in Arabic mode */
1955#ifdef FEAT_ARABIC
1956 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1957#else
1958 mch_errmsg(_(e_noarabic));
1959 mch_exit(2);
1960#endif
1961 break;
1962
1963 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001964 /* Needs to be effective before expanding file names, because
1965 * for Win32 this makes us edit a shortcut file itself,
1966 * instead of the file it links to. */
1967 set_options_bin(curbuf->b_p_bin, 1, 0);
1968 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001969 break;
1970
1971 case 'C': /* "-C" Compatible */
1972 change_compatible(TRUE);
1973 break;
1974
1975 case 'e': /* "-e" Ex mode */
1976 exmode_active = EXMODE_NORMAL;
1977 break;
1978
1979 case 'E': /* "-E" Improved Ex mode */
1980 exmode_active = EXMODE_VIM;
1981 break;
1982
1983 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1984 window directly, not with newcli */
1985#ifdef FEAT_GUI
1986 gui.dofork = FALSE; /* don't fork() when starting GUI */
1987#endif
1988 break;
1989
1990 case 'g': /* "-g" start GUI */
1991 main_start_gui();
1992 break;
1993
1994 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1995#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001996 p_fkmap = TRUE;
1997 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001998#else
1999 mch_errmsg(_(e_nofarsi));
2000 mch_exit(2);
2001#endif
2002 break;
2003
2004 case 'h': /* "-h" give help message */
2005#ifdef FEAT_GUI_GNOME
2006 /* Tell usage() to exit for "gvim". */
2007 gui.starting = FALSE;
2008#endif
2009 usage();
2010 break;
2011
2012 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2013#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002014 p_hkmap = TRUE;
2015 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002016#else
2017 mch_errmsg(_(e_nohebrew));
2018 mch_exit(2);
2019#endif
2020 break;
2021
2022 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2023#ifdef FEAT_LISP
2024 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2025 p_sm = TRUE;
2026#endif
2027 break;
2028
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002029 case 'M': /* "-M" no changes or writing of files */
2030 reset_modifiable();
2031 /* FALLTHROUGH */
2032
2033 case 'm': /* "-m" no writing of files */
2034 p_write = FALSE;
2035 break;
2036
2037 case 'y': /* "-y" easy mode */
2038#ifdef FEAT_GUI
2039 gui.starting = TRUE; /* start GUI a bit later */
2040#endif
2041 parmp->evim_mode = TRUE;
2042 break;
2043
2044 case 'N': /* "-N" Nocompatible */
2045 change_compatible(FALSE);
2046 break;
2047
2048 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002049#ifdef FEAT_NETBEANS_INTG
2050 /* checking for "-nb", netbeans parameters */
2051 if (argv[0][argv_idx] == 'b')
2052 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002053 netbeansArg = argv[0];
2054 argv_idx = -1; /* skip to next argument */
2055 }
2056 else
2057#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002058 parmp->no_swap_file = TRUE;
2059 break;
2060
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002061 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002062#ifdef TARGET_API_MAC_OSX
2063 /* For some reason on MacOS X, an argument like:
2064 -psn_0_10223617 is passed in when invoke from Finder
2065 or with the 'open' command */
2066 if (argv[0][argv_idx] == 's')
2067 {
2068 argv_idx = -1; /* bypass full -psn */
2069 main_start_gui();
2070 break;
2071 }
2072#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002073#ifdef FEAT_WINDOWS
2074 /* default is 0: open window for each file */
2075 parmp->window_count = get_number_arg((char_u *)argv[0],
2076 &argv_idx, 0);
2077 parmp->window_layout = WIN_TABS;
2078#endif
2079 break;
2080
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002081 case 'o': /* "-o[N]" open N horizontal split windows */
2082#ifdef 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_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002087#endif
2088 break;
2089
2090 case 'O': /* "-O[N]" open N vertical split windows */
2091#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2092 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002093 parmp->window_count = get_number_arg((char_u *)argv[0],
2094 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002095 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002096#endif
2097 break;
2098
2099#ifdef FEAT_QUICKFIX
2100 case 'q': /* "-q" QuickFix mode */
2101 if (parmp->edit_type != EDIT_NONE)
2102 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2103 parmp->edit_type = EDIT_QF;
2104 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2105 {
2106 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2107 argv_idx = -1;
2108 }
2109 else if (argc > 1) /* "-q {errorfile}" */
2110 want_argument = TRUE;
2111 break;
2112#endif
2113
2114 case 'R': /* "-R" readonly mode */
2115 readonlymode = TRUE;
2116 curbuf->b_p_ro = TRUE;
2117 p_uc = 10000; /* don't update very often */
2118 break;
2119
2120 case 'r': /* "-r" recovery mode */
2121 case 'L': /* "-L" recovery mode */
2122 recoverymode = 1;
2123 break;
2124
2125 case 's':
2126 if (exmode_active) /* "-s" silent (batch) mode */
2127 silent_mode = TRUE;
2128 else /* "-s {scriptin}" read from script file */
2129 want_argument = TRUE;
2130 break;
2131
2132 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2133 if (parmp->edit_type != EDIT_NONE)
2134 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2135 parmp->edit_type = EDIT_TAG;
2136 if (argv[0][argv_idx]) /* "-t{tag}" */
2137 {
2138 parmp->tagname = (char_u *)argv[0] + argv_idx;
2139 argv_idx = -1;
2140 }
2141 else /* "-t {tag}" */
2142 want_argument = TRUE;
2143 break;
2144
2145#ifdef FEAT_EVAL
2146 case 'D': /* "-D" Debugging */
2147 parmp->use_debug_break_level = 9999;
2148 break;
2149#endif
2150#ifdef FEAT_DIFF
2151 case 'd': /* "-d" 'diff' */
2152# ifdef AMIGA
2153 /* check for "-dev {device}" */
2154 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2155 want_argument = TRUE;
2156 else
2157# endif
2158 parmp->diff_mode = TRUE;
2159 break;
2160#endif
2161 case 'V': /* "-V{N}" Verbose level */
2162 /* default is 10: a little bit verbose */
2163 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2164 if (argv[0][argv_idx] != NUL)
2165 {
2166 set_option_value((char_u *)"verbosefile", 0L,
2167 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002168 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002169 }
2170 break;
2171
2172 case 'v': /* "-v" Vi-mode (as if called "vi") */
2173 exmode_active = 0;
2174#ifdef FEAT_GUI
2175 gui.starting = FALSE; /* don't start GUI */
2176#endif
2177 break;
2178
2179 case 'w': /* "-w{number}" set window height */
2180 /* "-w {scriptout}" write to script */
2181 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2182 {
2183 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2184 set_option_value((char_u *)"window", n, NULL, 0);
2185 break;
2186 }
2187 want_argument = TRUE;
2188 break;
2189
2190#ifdef FEAT_CRYPT
2191 case 'x': /* "-x" encrypted reading/writing of files */
2192 parmp->ask_for_key = TRUE;
2193 break;
2194#endif
2195
2196 case 'X': /* "-X" don't connect to X server */
2197#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2198 x_no_connect = TRUE;
2199#endif
2200 break;
2201
2202 case 'Z': /* "-Z" restricted mode */
2203 restricted = TRUE;
2204 break;
2205
2206 case 'c': /* "-c{command}" or "-c {command}" execute
2207 command */
2208 if (argv[0][argv_idx] != NUL)
2209 {
2210 if (parmp->n_commands >= MAX_ARG_CMDS)
2211 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002212 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2213 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002214 argv_idx = -1;
2215 break;
2216 }
2217 /*FALLTHROUGH*/
2218 case 'S': /* "-S {file}" execute Vim script */
2219 case 'i': /* "-i {viminfo}" use for viminfo */
2220#ifndef FEAT_DIFF
2221 case 'd': /* "-d {device}" device (for Amiga) */
2222#endif
2223 case 'T': /* "-T {terminal}" terminal name */
2224 case 'u': /* "-u {vimrc}" vim inits file */
2225 case 'U': /* "-U {gvimrc}" gvim inits file */
2226 case 'W': /* "-W {scriptout}" overwrite */
2227#ifdef FEAT_GUI_W32
2228 case 'P': /* "-P {parent title}" MDI parent */
2229#endif
2230 want_argument = TRUE;
2231 break;
2232
2233 default:
2234 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2235 }
2236
2237 /*
2238 * Handle option arguments with argument.
2239 */
2240 if (want_argument)
2241 {
2242 /*
2243 * Check for garbage immediately after the option letter.
2244 */
2245 if (argv[0][argv_idx] != NUL)
2246 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2247
2248 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002249 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002250 mainerr_arg_missing((char_u *)argv[0]);
2251 ++argv;
2252 argv_idx = -1;
2253
2254 switch (c)
2255 {
2256 case 'c': /* "-c {command}" execute command */
2257 case 'S': /* "-S {file}" execute Vim script */
2258 if (parmp->n_commands >= MAX_ARG_CMDS)
2259 mainerr(ME_EXTRA_CMD, NULL);
2260 if (c == 'S')
2261 {
2262 char *a;
2263
2264 if (argc < 1)
2265 /* "-S" without argument: use default session file
2266 * name. */
2267 a = SESSION_FILE;
2268 else if (argv[0][0] == '-')
2269 {
2270 /* "-S" followed by another option: use default
2271 * session file name. */
2272 a = SESSION_FILE;
2273 ++argc;
2274 --argv;
2275 }
2276 else
2277 a = argv[0];
2278 p = alloc((unsigned)(STRLEN(a) + 4));
2279 if (p == NULL)
2280 mch_exit(2);
2281 sprintf((char *)p, "so %s", a);
2282 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2283 parmp->commands[parmp->n_commands++] = p;
2284 }
2285 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002286 parmp->commands[parmp->n_commands++] =
2287 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002288 break;
2289
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002290 case '-':
2291 if (argv[-1][2] == 'c')
2292 {
2293 /* "--cmd {command}" execute command */
2294 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2295 mainerr(ME_EXTRA_CMD, NULL);
2296 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002297 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002298 }
2299 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002300 break;
2301
2302 /* case 'd': -d {device} is handled in mch_check_win() for the
2303 * Amiga */
2304
2305#ifdef FEAT_QUICKFIX
2306 case 'q': /* "-q {errorfile}" QuickFix mode */
2307 parmp->use_ef = (char_u *)argv[0];
2308 break;
2309#endif
2310
2311 case 'i': /* "-i {viminfo}" use for viminfo */
2312 use_viminfo = (char_u *)argv[0];
2313 break;
2314
2315 case 's': /* "-s {scriptin}" read from script file */
2316 if (scriptin[0] != NULL)
2317 {
2318scripterror:
2319 mch_errmsg(_("Attempt to open script file again: \""));
2320 mch_errmsg(argv[-1]);
2321 mch_errmsg(" ");
2322 mch_errmsg(argv[0]);
2323 mch_errmsg("\"\n");
2324 mch_exit(2);
2325 }
2326 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2327 {
2328 mch_errmsg(_("Cannot open for reading: \""));
2329 mch_errmsg(argv[0]);
2330 mch_errmsg("\"\n");
2331 mch_exit(2);
2332 }
2333 if (save_typebuf() == FAIL)
2334 mch_exit(2); /* out of memory */
2335 break;
2336
2337 case 't': /* "-t {tag}" */
2338 parmp->tagname = (char_u *)argv[0];
2339 break;
2340
2341 case 'T': /* "-T {terminal}" terminal name */
2342 /*
2343 * The -T term argument is always available and when
2344 * HAVE_TERMLIB is supported it overrides the environment
2345 * variable TERM.
2346 */
2347#ifdef FEAT_GUI
2348 if (term_is_gui((char_u *)argv[0]))
2349 gui.starting = TRUE; /* start GUI a bit later */
2350 else
2351#endif
2352 parmp->term = (char_u *)argv[0];
2353 break;
2354
2355 case 'u': /* "-u {vimrc}" vim inits file */
2356 parmp->use_vimrc = (char_u *)argv[0];
2357 break;
2358
2359 case 'U': /* "-U {gvimrc}" gvim inits file */
2360#ifdef FEAT_GUI
2361 use_gvimrc = (char_u *)argv[0];
2362#endif
2363 break;
2364
2365 case 'w': /* "-w {nr}" 'window' value */
2366 /* "-w {scriptout}" append to script file */
2367 if (vim_isdigit(*((char_u *)argv[0])))
2368 {
2369 argv_idx = 0;
2370 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2371 set_option_value((char_u *)"window", n, NULL, 0);
2372 argv_idx = -1;
2373 break;
2374 }
2375 /*FALLTHROUGH*/
2376 case 'W': /* "-W {scriptout}" overwrite script file */
2377 if (scriptout != NULL)
2378 goto scripterror;
2379 if ((scriptout = mch_fopen(argv[0],
2380 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2381 {
2382 mch_errmsg(_("Cannot open for script output: \""));
2383 mch_errmsg(argv[0]);
2384 mch_errmsg("\"\n");
2385 mch_exit(2);
2386 }
2387 break;
2388
2389#ifdef FEAT_GUI_W32
2390 case 'P': /* "-P {parent title}" MDI parent */
2391 gui_mch_set_parent(argv[0]);
2392 break;
2393#endif
2394 }
2395 }
2396 }
2397
2398 /*
2399 * File name argument.
2400 */
2401 else
2402 {
2403 argv_idx = -1; /* skip to next argument */
2404
2405 /* Check for only one type of editing. */
2406 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2407 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2408 parmp->edit_type = EDIT_FILE;
2409
2410#ifdef MSWIN
2411 /* Remember if the argument was a full path before changing
2412 * slashes to backslashes. */
2413 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2414 parmp->full_path = TRUE;
2415#endif
2416
2417 /* Add the file to the global argument list. */
2418 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2419 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2420 mch_exit(2);
2421#ifdef FEAT_DIFF
2422 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2423 && !mch_isdir(alist_name(&GARGLIST[0])))
2424 {
2425 char_u *r;
2426
2427 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2428 if (r != NULL)
2429 {
2430 vim_free(p);
2431 p = r;
2432 }
2433 }
2434#endif
2435#if defined(__CYGWIN32__) && !defined(WIN32)
2436 /*
2437 * If vim is invoked by non-Cygwin tools, convert away any
2438 * DOS paths, so things like .swp files are created correctly.
2439 * Look for evidence of non-Cygwin paths before we bother.
2440 * This is only for when using the Unix files.
2441 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002442 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002443 {
2444 char posix_path[PATH_MAX];
2445
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002446# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2447 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2448# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002449 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002450# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002451 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002452 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002453 if (p == NULL)
2454 mch_exit(2);
2455 }
2456#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002457
2458#ifdef USE_FNAME_CASE
2459 /* Make the case of the file name match the actual file. */
2460 fname_case(p, 0);
2461#endif
2462
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002463 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002464#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002465 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002466#else
2467 2 /* add buffer number now and use curbuf */
2468#endif
2469 );
2470
2471#if defined(FEAT_MBYTE) && defined(WIN32)
2472 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002473 /* Remember this argument has been added to the argument list.
2474 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002475 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002476# ifdef FEAT_DIFF
2477 parmp->diff_mode
2478# else
2479 FALSE
2480# endif
2481 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002482 }
2483#endif
2484 }
2485
2486 /*
2487 * If there are no more letters after the current "-", go to next
2488 * argument. argv_idx is set to -1 when the current argument is to be
2489 * skipped.
2490 */
2491 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2492 {
2493 --argc;
2494 ++argv;
2495 argv_idx = 1;
2496 }
2497 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002498
2499#ifdef FEAT_EVAL
2500 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2501 * one. */
2502 if (parmp->n_commands > 0)
2503 {
2504 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2505 if (p != NULL)
2506 {
2507 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2508 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2509 vim_free(p);
2510 }
2511 }
2512#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002513}
2514
2515/*
2516 * Print a warning if stdout is not a terminal.
2517 * When starting in Ex mode and commands come from a file, set Silent mode.
2518 */
2519 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002520check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002521{
2522 int input_isatty; /* is active input a terminal? */
2523
2524 input_isatty = mch_input_isatty();
2525 if (exmode_active)
2526 {
2527 if (!input_isatty)
2528 silent_mode = TRUE;
2529 }
2530 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2531#ifdef FEAT_GUI
2532 /* don't want the delay when started from the desktop */
2533 && !gui.starting
2534#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002535 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002536 {
2537#ifdef NBDEBUG
2538 /*
2539 * This shouldn't be necessary. But if I run netbeans with the log
2540 * output coming to the console and XOpenDisplay fails, I get vim
2541 * trying to start with input/output to my console tty. This fills my
2542 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002543 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002544 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002545 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002546 {
2547 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2548 exit(1);
2549 }
2550#endif
2551 if (!parmp->stdout_isatty)
2552 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2553 if (!input_isatty)
2554 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2555 out_flush();
2556 if (scriptin[0] == NULL)
2557 ui_delay(2000L, TRUE);
2558 TIME_MSG("Warning delay");
2559 }
2560}
2561
2562/*
2563 * Read text from stdin.
2564 */
2565 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002566read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002567{
2568 int i;
2569
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002570#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002571 /* When getting the ATTENTION prompt here, use a dialog */
2572 swap_exists_action = SEA_DIALOG;
2573#endif
2574 no_wait_return = TRUE;
2575 i = msg_didany;
2576 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002577 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002578 no_wait_return = FALSE;
2579 msg_didany = i;
2580 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002581#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002582 check_swap_exists_action();
2583#endif
2584#if !(defined(AMIGA) || defined(MACOS))
2585 /*
2586 * Close stdin and dup it from stderr. Required for GPM to work
2587 * properly, and for running external commands.
2588 * Is there any other system that cannot do this?
2589 */
2590 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002591 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002592#endif
2593}
2594
2595/*
2596 * Create the requested number of windows and edit buffers in them.
2597 * Also does recovery if "recoverymode" set.
2598 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002599 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002600create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002601{
2602#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002603 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002604 int done = 0;
2605
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002606 /*
2607 * Create the number of windows that was requested.
2608 */
2609 if (parmp->window_count == -1) /* was not set */
2610 parmp->window_count = 1;
2611 if (parmp->window_count == 0)
2612 parmp->window_count = GARGCOUNT;
2613 if (parmp->window_count > 1)
2614 {
2615 /* Don't change the windows if there was a command in .vimrc that
2616 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002617 if (parmp->window_layout == 0)
2618 parmp->window_layout = WIN_HOR;
2619 if (parmp->window_layout == WIN_TABS)
2620 {
2621 parmp->window_count = make_tabpages(parmp->window_count);
2622 TIME_MSG("making tab pages");
2623 }
2624 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002625 {
2626 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002627 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002628 TIME_MSG("making windows");
2629 }
2630 else
2631 parmp->window_count = win_count();
2632 }
2633 else
2634 parmp->window_count = 1;
2635#endif
2636
2637 if (recoverymode) /* do recover */
2638 {
2639 msg_scroll = TRUE; /* scroll message up */
2640 ml_recover();
2641 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2642 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002643 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002644 }
2645 else
2646 {
2647 /*
2648 * Open a buffer for windows that don't have one yet.
2649 * Commands in the .vimrc might have loaded a file or split the window.
2650 * Watch out for autocommands that delete a window.
2651 */
2652#ifdef FEAT_AUTOCMD
2653 /*
2654 * Don't execute Win/Buf Enter/Leave autocommands here
2655 */
2656 ++autocmd_no_enter;
2657 ++autocmd_no_leave;
2658#endif
2659#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002660 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002661 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002662 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002663 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002664 {
2665 if (parmp->window_layout == WIN_TABS)
2666 goto_tabpage(1);
2667 else
2668 curwin = firstwin;
2669 }
2670 else if (parmp->window_layout == WIN_TABS)
2671 {
2672 if (curtab->tp_next == NULL)
2673 break;
2674 goto_tabpage(0);
2675 }
2676 else
2677 {
2678 if (curwin->w_next == NULL)
2679 break;
2680 curwin = curwin->w_next;
2681 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002682 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002683#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002684 curbuf = curwin->w_buffer;
2685 if (curbuf->b_ml.ml_mfp == NULL)
2686 {
2687#ifdef FEAT_FOLDING
2688 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2689 if (p_fdls >= 0)
2690 curwin->w_p_fdl = p_fdls;
2691#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002692#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002693 /* When getting the ATTENTION prompt here, use a dialog */
2694 swap_exists_action = SEA_DIALOG;
2695#endif
2696 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002697
2698 /* create memfile, read file */
2699 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002700
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002701#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002702 if (swap_exists_action == SEA_QUIT)
2703 {
2704 if (got_int || only_one_window())
2705 {
2706 /* abort selected or quit and only one window */
2707 did_emsg = FALSE; /* avoid hit-enter prompt */
2708 getout(1);
2709 }
2710 /* We can't close the window, it would disturb what
2711 * happens next. Clear the file name and set the arg
2712 * index to -1 to delete it later. */
2713 setfname(curbuf, NULL, NULL, FALSE);
2714 curwin->w_arg_idx = -1;
2715 swap_exists_action = SEA_NONE;
2716 }
2717 else
2718 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002719#endif
2720#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002721 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002722#endif
2723 }
2724#ifdef FEAT_WINDOWS
2725 ui_breakcheck();
2726 if (got_int)
2727 {
2728 (void)vgetc(); /* only break the file loading, not the rest */
2729 break;
2730 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002731 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002732#endif
2733#ifdef FEAT_WINDOWS
2734 if (parmp->window_layout == WIN_TABS)
2735 goto_tabpage(1);
2736 else
2737 curwin = firstwin;
2738 curbuf = curwin->w_buffer;
2739#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002740#ifdef FEAT_AUTOCMD
2741 --autocmd_no_enter;
2742 --autocmd_no_leave;
2743#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002744 }
2745}
2746
2747#ifdef FEAT_WINDOWS
2748 /*
2749 * If opened more than one window, start editing files in the other
2750 * windows. make_windows() has already opened the windows.
2751 */
2752 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002753edit_buffers(
2754 mparm_T *parmp,
2755 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002756{
2757 int arg_idx; /* index in argument list */
2758 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002759 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002760 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002761
2762# ifdef FEAT_AUTOCMD
2763 /*
2764 * Don't execute Win/Buf Enter/Leave autocommands here
2765 */
2766 ++autocmd_no_enter;
2767 ++autocmd_no_leave;
2768# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002769
2770 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2771 if (curwin->w_arg_idx == -1)
2772 {
2773 win_close(curwin, TRUE);
2774 advance = FALSE;
2775 }
2776
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002777 arg_idx = 1;
2778 for (i = 1; i < parmp->window_count; ++i)
2779 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002780 if (cwd != NULL)
2781 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002782 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2783 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002784 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002785 ++arg_idx;
2786 win_close(curwin, TRUE);
2787 advance = FALSE;
2788 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002789 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002790
Bram Moolenaar84212822006-11-07 21:59:47 +00002791 if (advance)
2792 {
2793 if (parmp->window_layout == WIN_TABS)
2794 {
2795 if (curtab->tp_next == NULL) /* just checking */
2796 break;
2797 goto_tabpage(0);
2798 }
2799 else
2800 {
2801 if (curwin->w_next == NULL) /* just checking */
2802 break;
2803 win_enter(curwin->w_next, FALSE);
2804 }
2805 }
2806 advance = TRUE;
2807
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002808 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002809 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002810 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2811 {
2812 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002813 /* Edit file from arg list, if there is one. When "Quit" selected
2814 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002815# ifdef HAS_SWAP_EXISTS_ACTION
2816 swap_exists_did_quit = FALSE;
2817# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002818 (void)do_ecmd(0, arg_idx < GARGCOUNT
2819 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002820 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002821# ifdef HAS_SWAP_EXISTS_ACTION
2822 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002823 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002824 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002825 if (got_int || only_one_window())
2826 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002827 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002828 did_emsg = FALSE; /* avoid hit-enter prompt */
2829 getout(1);
2830 }
2831 win_close(curwin, TRUE);
2832 advance = FALSE;
2833 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002834# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002835 if (arg_idx == GARGCOUNT - 1)
2836 arg_had_last = TRUE;
2837 ++arg_idx;
2838 }
2839 ui_breakcheck();
2840 if (got_int)
2841 {
2842 (void)vgetc(); /* only break the file loading, not the rest */
2843 break;
2844 }
2845 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002846
2847 if (parmp->window_layout == WIN_TABS)
2848 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002849# ifdef FEAT_AUTOCMD
2850 --autocmd_no_enter;
2851# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002852
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002853 /* make the first window the current window */
2854 win = firstwin;
2855#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2856 /* Avoid making a preview window the current window. */
2857 while (win->w_p_pvw)
2858 {
2859 win = win->w_next;
2860 if (win == NULL)
2861 {
2862 win = firstwin;
2863 break;
2864 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002865 }
2866#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002867 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002868
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002869# ifdef FEAT_AUTOCMD
2870 --autocmd_no_leave;
2871# endif
2872 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002873 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002874 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2875}
2876#endif /* FEAT_WINDOWS */
2877
2878/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002879 * Execute the commands from --cmd arguments "cmds[cnt]".
2880 */
2881 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002882exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002883{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002884 char_u **cmds = parmp->pre_commands;
2885 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002886 int i;
2887
2888 if (cnt > 0)
2889 {
2890 curwin->w_cursor.lnum = 0; /* just in case.. */
2891 sourcing_name = (char_u *)_("pre-vimrc command line");
2892# ifdef FEAT_EVAL
2893 current_SID = SID_CMDARG;
2894# endif
2895 for (i = 0; i < cnt; ++i)
2896 do_cmdline_cmd(cmds[i]);
2897 sourcing_name = NULL;
2898# ifdef FEAT_EVAL
2899 current_SID = 0;
2900# endif
2901 TIME_MSG("--cmd commands");
2902 }
2903}
2904
2905/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002906 * Execute "+", "-c" and "-S" arguments.
2907 */
2908 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002909exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002910{
2911 int i;
2912
2913 /*
2914 * We start commands on line 0, make "vim +/pat file" match a
2915 * pattern on line 1. But don't move the cursor when an autocommand
2916 * with g`" was used.
2917 */
2918 msg_scroll = TRUE;
2919 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2920 curwin->w_cursor.lnum = 0;
2921 sourcing_name = (char_u *)"command line";
2922#ifdef FEAT_EVAL
2923 current_SID = SID_CARG;
2924#endif
2925 for (i = 0; i < parmp->n_commands; ++i)
2926 {
2927 do_cmdline_cmd(parmp->commands[i]);
2928 if (parmp->cmds_tofree[i])
2929 vim_free(parmp->commands[i]);
2930 }
2931 sourcing_name = NULL;
2932#ifdef FEAT_EVAL
2933 current_SID = 0;
2934#endif
2935 if (curwin->w_cursor.lnum == 0)
2936 curwin->w_cursor.lnum = 1;
2937
2938 if (!exmode_active)
2939 msg_scroll = FALSE;
2940
2941#ifdef FEAT_QUICKFIX
2942 /* When started with "-q errorfile" jump to first error again. */
2943 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002944 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002945#endif
2946 TIME_MSG("executing command arguments");
2947}
2948
2949/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002950 * Source startup scripts.
2951 */
2952 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002953source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002954{
2955 int i;
2956
2957 /*
2958 * For "evim" source evim.vim first of all, so that the user can overrule
2959 * any things he doesn't like.
2960 */
2961 if (parmp->evim_mode)
2962 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002963 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002964 TIME_MSG("source evim file");
2965 }
2966
2967 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002968 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002969 * nothing else.
2970 */
2971 if (parmp->use_vimrc != NULL)
2972 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002973 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2974 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002975 {
2976#ifdef FEAT_GUI
2977 if (use_gvimrc == NULL) /* don't load gvimrc either */
2978 use_gvimrc = parmp->use_vimrc;
2979#endif
2980 if (parmp->use_vimrc[2] == 'N')
2981 p_lpl = FALSE; /* don't load plugins either */
2982 }
2983 else
2984 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002985 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002986 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2987 }
2988 }
2989 else if (!silent_mode)
2990 {
2991#ifdef AMIGA
2992 struct Process *proc = (struct Process *)FindTask(0L);
2993 APTR save_winptr = proc->pr_WindowPtr;
2994
2995 /* Avoid a requester here for a volume that doesn't exist. */
2996 proc->pr_WindowPtr = (APTR)-1L;
2997#endif
2998
2999 /*
3000 * Get system wide defaults, if the file name is defined.
3001 */
3002#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003003 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003004#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003005#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003006 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003007#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003008
3009 /*
3010 * Try to read initialization commands from the following places:
3011 * - environment variable VIMINIT
3012 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3013 * - second user vimrc file ($VIM/.vimrc for Dos)
3014 * - environment variable EXINIT
3015 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3016 * - second user exrc file ($VIM/.exrc for Dos)
3017 * The first that exists is used, the rest is ignored.
3018 */
3019 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3020 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003021 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003022#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003023 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3024 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003025#endif
3026#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003027 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3028 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003029#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003030#ifdef USR_VIMRC_FILE4
3031 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3032 DOSO_VIMRC) == FAIL
3033#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003034 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003035 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003036 {
3037#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003038 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003039#endif
3040 }
3041 }
3042
3043 /*
3044 * Read initialization commands from ".vimrc" or ".exrc" in current
3045 * directory. This is only done if the 'exrc' option is set.
3046 * Because of security reasons we disallow shell and write commands
3047 * now, except for unix if the file is owned by the user or 'secure'
3048 * option has been reset in environment of global ".exrc" or ".vimrc".
3049 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3050 * SYS_VIMRC_FILE.
3051 */
3052 if (p_exrc)
3053 {
3054#if defined(UNIX) || defined(VMS)
3055 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3056 if (!file_owned(VIMRC_FILE))
3057#endif
3058 secure = p_secure;
3059
3060 i = FAIL;
3061 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3062 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3063#ifdef USR_VIMRC_FILE2
3064 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3065 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3066#endif
3067#ifdef USR_VIMRC_FILE3
3068 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3069 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3070#endif
3071#ifdef SYS_VIMRC_FILE
3072 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3073 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3074#endif
3075 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003076 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003077
3078 if (i == FAIL)
3079 {
3080#if defined(UNIX) || defined(VMS)
3081 /* if ".exrc" is not owned by user set 'secure' mode */
3082 if (!file_owned(EXRC_FILE))
3083 secure = p_secure;
3084 else
3085 secure = 0;
3086#endif
3087 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3088 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3089#ifdef USR_EXRC_FILE2
3090 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3091 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3092#endif
3093 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003094 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003095 }
3096 }
3097 if (secure == 2)
3098 need_wait_return = TRUE;
3099 secure = 0;
3100#ifdef AMIGA
3101 proc->pr_WindowPtr = save_winptr;
3102#endif
3103 }
3104 TIME_MSG("sourcing vimrc file(s)");
3105}
3106
3107/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003108 * Setup to start using the GUI. Exit with an error when not available.
3109 */
3110 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003111main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003112{
3113#ifdef FEAT_GUI
3114 gui.starting = TRUE; /* start GUI a bit later */
3115#else
3116 mch_errmsg(_(e_nogvim));
3117 mch_errmsg("\n");
3118 mch_exit(2);
3119#endif
3120}
3121
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003122#endif /* NO_VIM_MAIN */
3123
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003124/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003125 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003126 * Returns FAIL if the environment variable was not executed, OK otherwise.
3127 */
3128 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003129process_env(
3130 char_u *env,
3131 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003132{
3133 char_u *initstr;
3134 char_u *save_sourcing_name;
3135 linenr_T save_sourcing_lnum;
3136#ifdef FEAT_EVAL
3137 scid_T save_sid;
3138#endif
3139
3140 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3141 {
3142 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003143 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003144 save_sourcing_name = sourcing_name;
3145 save_sourcing_lnum = sourcing_lnum;
3146 sourcing_name = env;
3147 sourcing_lnum = 0;
3148#ifdef FEAT_EVAL
3149 save_sid = current_SID;
3150 current_SID = SID_ENV;
3151#endif
3152 do_cmdline_cmd(initstr);
3153 sourcing_name = save_sourcing_name;
3154 sourcing_lnum = save_sourcing_lnum;
3155#ifdef FEAT_EVAL
3156 current_SID = save_sid;;
3157#endif
3158 return OK;
3159 }
3160 return FAIL;
3161}
3162
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003163#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003164/*
3165 * Return TRUE if we are certain the user owns the file "fname".
3166 * Used for ".vimrc" and ".exrc".
3167 * Use both stat() and lstat() for extra security.
3168 */
3169 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003170file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003171{
3172 struct stat s;
3173# ifdef UNIX
3174 uid_t uid = getuid();
3175# else /* VMS */
3176 uid_t uid = ((getgid() << 16) | getuid());
3177# endif
3178
3179 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3180# ifdef HAVE_LSTAT
3181 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3182# endif
3183 );
3184}
3185#endif
3186
3187/*
3188 * Give an error message main_errors["n"] and exit.
3189 */
3190 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003191mainerr(
3192 int n, /* one of the ME_ defines */
3193 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003194{
3195#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3196 reset_signals(); /* kill us with CTRL-C here, if you like */
3197#endif
3198
3199 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003200 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003201 mch_errmsg(_(main_errors[n]));
3202 if (str != NULL)
3203 {
3204 mch_errmsg(": \"");
3205 mch_errmsg((char *)str);
3206 mch_errmsg("\"");
3207 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003208 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003209
3210 mch_exit(1);
3211}
3212
3213 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003214mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003215{
3216 mainerr(ME_ARG_MISSING, str);
3217}
3218
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003219#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003220/*
3221 * print a message with three spaces prepended and '\n' appended.
3222 */
3223 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003224main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003225{
3226 mch_msg(" ");
3227 mch_msg(s);
3228 mch_msg("\n");
3229}
3230
3231/*
3232 * Print messages for "vim -h" or "vim --help" and exit.
3233 */
3234 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003235usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003236{
3237 int i;
3238 static char *(use[]) =
3239 {
3240 N_("[file ..] edit specified file(s)"),
3241 N_("- read text from stdin"),
3242 N_("-t tag edit file where tag is defined"),
3243#ifdef FEAT_QUICKFIX
3244 N_("-q [errorfile] edit file with first error")
3245#endif
3246 };
3247
3248#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3249 reset_signals(); /* kill us with CTRL-C here, if you like */
3250#endif
3251
3252 mch_msg(longVersion);
3253 mch_msg(_("\n\nusage:"));
3254 for (i = 0; ; ++i)
3255 {
3256 mch_msg(_(" vim [arguments] "));
3257 mch_msg(_(use[i]));
3258 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3259 break;
3260 mch_msg(_("\n or:"));
3261 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003262#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003263 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003264#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003265
3266 mch_msg(_("\n\nArguments:\n"));
3267 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003268#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003269 main_msg(_("--literal\t\tDon't expand wildcards"));
3270#endif
3271#ifdef FEAT_OLE
3272 main_msg(_("-register\t\tRegister this gvim for OLE"));
3273 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3274#endif
3275#ifdef FEAT_GUI
3276 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3277 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3278#endif
3279 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3280 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003281 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003282 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3283#ifdef FEAT_DIFF
3284 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3285#endif
3286 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3287 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3288 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3289 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3290 main_msg(_("-M\t\t\tModifications in text not allowed"));
3291 main_msg(_("-b\t\t\tBinary mode"));
3292#ifdef FEAT_LISP
3293 main_msg(_("-l\t\t\tLisp mode"));
3294#endif
3295 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3296 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003297 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3298#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003299 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003300#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003301 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3302 main_msg(_("-r\t\t\tList swap files and exit"));
3303 main_msg(_("-r (with file name)\tRecover crashed session"));
3304 main_msg(_("-L\t\t\tSame as -r"));
3305#ifdef AMIGA
3306 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3307 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3308#endif
3309#ifdef FEAT_ARABIC
3310 main_msg(_("-A\t\t\tstart in Arabic mode"));
3311#endif
3312#ifdef FEAT_RIGHTLEFT
3313 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3314#endif
3315#ifdef FEAT_FKMAP
3316 main_msg(_("-F\t\t\tStart in Farsi mode"));
3317#endif
3318 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003319 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003320 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3321#ifdef FEAT_GUI
3322 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3323#endif
3324 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003325#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003326 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003327 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3328 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003329#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003330 main_msg(_("+\t\t\tStart at end of file"));
3331 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003332 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003333 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3334 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3335 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3336 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3337 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3338#ifdef FEAT_CRYPT
3339 main_msg(_("-x\t\t\tEdit encrypted files"));
3340#endif
3341#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3342# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3343 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3344# endif
3345 main_msg(_("-X\t\t\tDo not connect to X server"));
3346#endif
3347#ifdef FEAT_CLIENTSERVER
3348 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3349 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3350 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3351 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003352# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003353 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003354# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003355 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3356 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3357 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3358 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3359#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003360#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003361 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003362#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003363#ifdef FEAT_VIMINFO
3364 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3365#endif
3366 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3367 main_msg(_("--version\t\tPrint version information and exit"));
3368
3369#ifdef FEAT_GUI_X11
3370# ifdef FEAT_GUI_MOTIF
3371 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3372# else
3373# ifdef FEAT_GUI_ATHENA
3374# ifdef FEAT_GUI_NEXTAW
3375 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3376# else
3377 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3378# endif
3379# endif
3380# endif
3381 main_msg(_("-display <display>\tRun vim on <display>"));
3382 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003383 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3384 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3385 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3386 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3387 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3388 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3389 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3390 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3391# ifdef FEAT_GUI_ATHENA
3392 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3393# endif
3394 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3395 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3396 main_msg(_("-xrm <resource>\tSet the specified resource"));
3397#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003398#ifdef FEAT_GUI_GTK
3399 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3400 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3401 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3402 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3403 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003404 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003405 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003406 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003407#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003408#ifdef FEAT_GUI_W32
3409 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003410 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003411#endif
3412
3413#ifdef FEAT_GUI_GNOME
3414 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3415 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003416 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003417 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003418 gui.dofork = FALSE;
3419 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003420 else
3421#endif
3422 mch_exit(0);
3423}
3424
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003425#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003426/*
3427 * Check the result of the ATTENTION dialog:
3428 * When "Quit" selected, exit Vim.
3429 * When "Recover" selected, recover the file.
3430 */
3431 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003432check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003433{
3434 if (swap_exists_action == SEA_QUIT)
3435 getout(1);
3436 handle_swap_exists(NULL);
3437}
3438#endif
3439
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003440#endif
3441
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003442#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003443static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003444
3445static struct timeval prev_timeval;
3446
Bram Moolenaar3f269672009-11-03 11:11:11 +00003447# ifdef WIN3264
3448/*
3449 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3450 */
3451 static int
3452gettimeofday(struct timeval *tv, char *dummy)
3453{
3454 long t = clock();
3455 tv->tv_sec = t / CLOCKS_PER_SEC;
3456 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3457 return 0;
3458}
3459# endif
3460
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003461/*
3462 * Save the previous time before doing something that could nest.
3463 * set "*tv_rel" to the time elapsed so far.
3464 */
3465 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003466time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003467{
3468 *((struct timeval *)tv_rel) = prev_timeval;
3469 gettimeofday(&prev_timeval, NULL);
3470 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3471 - ((struct timeval *)tv_rel)->tv_usec;
3472 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3473 - ((struct timeval *)tv_rel)->tv_sec;
3474 if (((struct timeval *)tv_rel)->tv_usec < 0)
3475 {
3476 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3477 --((struct timeval *)tv_rel)->tv_sec;
3478 }
3479 *(struct timeval *)tv_start = prev_timeval;
3480}
3481
3482/*
3483 * Compute the previous time after doing something that could nest.
3484 * Subtract "*tp" from prev_timeval;
3485 * Note: The arguments are (void *) to avoid trouble with systems that don't
3486 * have struct timeval.
3487 */
3488 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003489time_pop(
3490 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003491{
3492 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3493 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3494 if (prev_timeval.tv_usec < 0)
3495 {
3496 prev_timeval.tv_usec += 1000000;
3497 --prev_timeval.tv_sec;
3498 }
3499}
3500
3501 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003502time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003503{
3504 long usec;
3505 long msec;
3506
3507 usec = now->tv_usec - then->tv_usec;
3508 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3509 usec = usec % 1000L;
3510 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3511}
3512
3513 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003514time_msg(
3515 char *mesg,
3516 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003517 (struct timeval *) */
3518{
3519 static struct timeval start;
3520 struct timeval now;
3521
3522 if (time_fd != NULL)
3523 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003524 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003525 {
3526 gettimeofday(&start, NULL);
3527 prev_timeval = start;
3528 fprintf(time_fd, "\n\ntimes in msec\n");
3529 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3530 fprintf(time_fd, " clock elapsed: other lines\n\n");
3531 }
3532 gettimeofday(&now, NULL);
3533 time_diff(&start, &now);
3534 if (((struct timeval *)tv_start) != NULL)
3535 {
3536 fprintf(time_fd, " ");
3537 time_diff(((struct timeval *)tv_start), &now);
3538 }
3539 fprintf(time_fd, " ");
3540 time_diff(&prev_timeval, &now);
3541 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003542 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003543 }
3544}
3545
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003546#endif
3547
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003548#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003549
3550/*
3551 * Common code for the X command server and the Win32 command server.
3552 */
3553
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003554static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003555
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003556/*
3557 * Do the client-server stuff, unless "--servername ''" was used.
3558 */
3559 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003560exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003561{
3562 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3563 {
3564# ifdef WIN32
3565 /* Initialise the client/server messaging infrastructure. */
3566 serverInitMessaging();
3567# endif
3568
3569 /*
3570 * When a command server argument was found, execute it. This may
3571 * exit Vim when it was successful. Otherwise it's executed further
3572 * on. Remember the encoding used here in "serverStrEnc".
3573 */
3574 if (parmp->serverArg)
3575 {
3576 cmdsrv_main(&parmp->argc, parmp->argv,
3577 parmp->serverName_arg, &parmp->serverStr);
3578# ifdef FEAT_MBYTE
3579 parmp->serverStrEnc = vim_strsave(p_enc);
3580# endif
3581 }
3582
3583 /* If we're still running, get the name to register ourselves.
3584 * On Win32 can register right now, for X11 need to setup the
3585 * clipboard first, it's further down. */
3586 parmp->servername = serverMakeName(parmp->serverName_arg,
3587 parmp->argv[0]);
3588# ifdef WIN32
3589 if (parmp->servername != NULL)
3590 {
3591 serverSetName(parmp->servername);
3592 vim_free(parmp->servername);
3593 }
3594# endif
3595 }
3596}
3597
3598/*
3599 * Prepare for running as a Vim server.
3600 */
3601 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003602prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003603{
3604# if defined(FEAT_X11)
3605 /*
3606 * Register for remote command execution with :serversend and --remote
3607 * unless there was a -X or a --servername '' on the command line.
3608 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003609 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003610 */
3611 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3612# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003613 (gui.in_use
3614# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003615 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003616# endif
3617 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003618# endif
3619 parmp->serverName_arg != NULL))
3620 {
3621 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3622 vim_free(parmp->servername);
3623 TIME_MSG("register server name");
3624 }
3625 else
3626 serverDelayedStartName = parmp->servername;
3627# endif
3628
3629 /*
3630 * Execute command ourselves if we're here because the send failed (or
3631 * else we would have exited above).
3632 */
3633 if (parmp->serverStr != NULL)
3634 {
3635 char_u *p;
3636
3637 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3638 parmp->serverStr, &p));
3639 vim_free(p);
3640 }
3641}
3642
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003643 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003644cmdsrv_main(
3645 int *argc,
3646 char **argv,
3647 char_u *serverName_arg,
3648 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003649{
3650 char_u *res;
3651 int i;
3652 char_u *sname;
3653 int ret;
3654 int didone = FALSE;
3655 int exiterr = 0;
3656 char **newArgV = argv + 1;
3657 int newArgC = 1,
3658 Argc = *argc;
3659 int argtype;
3660#define ARGTYPE_OTHER 0
3661#define ARGTYPE_EDIT 1
3662#define ARGTYPE_EDIT_WAIT 2
3663#define ARGTYPE_SEND 3
3664 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003665 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003666# ifndef FEAT_X11
3667 HWND srv;
3668# else
3669 Window srv;
3670
3671 setup_term_clip();
3672# endif
3673
3674 sname = serverMakeName(serverName_arg, argv[0]);
3675 if (sname == NULL)
3676 return;
3677
3678 /*
3679 * Execute the command server related arguments and remove them
3680 * from the argc/argv array; We may have to return into main()
3681 */
3682 for (i = 1; i < Argc; i++)
3683 {
3684 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003685 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003686 {
3687 for (; i < *argc; i++)
3688 {
3689 *newArgV++ = argv[i];
3690 newArgC++;
3691 }
3692 break;
3693 }
3694
Bram Moolenaareb94e552006-03-11 21:35:11 +00003695 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003696 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003697 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3698 {
3699 char *p = argv[i] + 8;
3700
3701 argtype = ARGTYPE_EDIT;
3702 while (*p != NUL)
3703 {
3704 if (STRNICMP(p, "-wait", 5) == 0)
3705 {
3706 argtype = ARGTYPE_EDIT_WAIT;
3707 p += 5;
3708 }
3709 else if (STRNICMP(p, "-silent", 7) == 0)
3710 {
3711 silent = TRUE;
3712 p += 7;
3713 }
3714 else if (STRNICMP(p, "-tab", 4) == 0)
3715 {
3716 tabs = TRUE;
3717 p += 4;
3718 }
3719 else
3720 {
3721 argtype = ARGTYPE_OTHER;
3722 break;
3723 }
3724 }
3725 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003726 else
3727 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003728
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003729 if (argtype != ARGTYPE_OTHER)
3730 {
3731 if (i == *argc - 1)
3732 mainerr_arg_missing((char_u *)argv[i]);
3733 if (argtype == ARGTYPE_SEND)
3734 {
3735 *serverStr = (char_u *)argv[i + 1];
3736 i++;
3737 }
3738 else
3739 {
3740 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003741 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003742 if (*serverStr == NULL)
3743 {
3744 /* Probably out of memory, exit. */
3745 didone = TRUE;
3746 exiterr = 1;
3747 break;
3748 }
3749 Argc = i;
3750 }
3751# ifdef FEAT_X11
3752 if (xterm_dpy == NULL)
3753 {
3754 mch_errmsg(_("No display"));
3755 ret = -1;
3756 }
3757 else
3758 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3759 NULL, &srv, 0, 0, silent);
3760# else
3761 /* Win32 always works? */
3762 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3763# endif
3764 if (ret < 0)
3765 {
3766 if (argtype == ARGTYPE_SEND)
3767 {
3768 /* Failed to send, abort. */
3769 mch_errmsg(_(": Send failed.\n"));
3770 didone = TRUE;
3771 exiterr = 1;
3772 }
3773 else if (!silent)
3774 /* Let vim start normally. */
3775 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3776 break;
3777 }
3778
3779# ifdef FEAT_GUI_W32
3780 /* Guess that when the server name starts with "g" it's a GUI
3781 * server, which we can bring to the foreground here.
3782 * Foreground() in the server doesn't work very well. */
3783 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3784 SetForegroundWindow(srv);
3785# endif
3786
3787 /*
3788 * For --remote-wait: Wait until the server did edit each
3789 * file. Also detect that the server no longer runs.
3790 */
3791 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3792 {
3793 int numFiles = *argc - i - 1;
3794 int j;
3795 char_u *done = alloc(numFiles);
3796 char_u *p;
3797# ifdef FEAT_GUI_W32
3798 NOTIFYICONDATA ni;
3799 int count = 0;
3800 extern HWND message_window;
3801# endif
3802
3803 if (numFiles > 0 && argv[i + 1][0] == '+')
3804 /* Skip "+cmd" argument, don't wait for it to be edited. */
3805 --numFiles;
3806
3807# ifdef FEAT_GUI_W32
3808 ni.cbSize = sizeof(ni);
3809 ni.hWnd = message_window;
3810 ni.uID = 0;
3811 ni.uFlags = NIF_ICON|NIF_TIP;
3812 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3813 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3814 Shell_NotifyIcon(NIM_ADD, &ni);
3815# endif
3816
3817 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003818 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003819 while (memchr(done, 0, numFiles) != NULL)
3820 {
3821# ifdef WIN32
3822 p = serverGetReply(srv, NULL, TRUE, TRUE);
3823 if (p == NULL)
3824 break;
3825# else
3826 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3827 break;
3828# endif
3829 j = atoi((char *)p);
3830 if (j >= 0 && j < numFiles)
3831 {
3832# ifdef FEAT_GUI_W32
3833 ++count;
3834 sprintf(ni.szTip, _("%d of %d edited"),
3835 count, numFiles);
3836 Shell_NotifyIcon(NIM_MODIFY, &ni);
3837# endif
3838 done[j] = 1;
3839 }
3840 }
3841# ifdef FEAT_GUI_W32
3842 Shell_NotifyIcon(NIM_DELETE, &ni);
3843# endif
3844 }
3845 }
3846 else if (STRICMP(argv[i], "--remote-expr") == 0)
3847 {
3848 if (i == *argc - 1)
3849 mainerr_arg_missing((char_u *)argv[i]);
3850# ifdef WIN32
3851 /* Win32 always works? */
3852 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3853 &res, NULL, 1, FALSE) < 0)
3854# else
3855 if (xterm_dpy == NULL)
3856 mch_errmsg(_("No display: Send expression failed.\n"));
3857 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3858 &res, NULL, 1, 1, FALSE) < 0)
3859# endif
3860 {
3861 if (res != NULL && *res != NUL)
3862 {
3863 /* Output error from remote */
3864 mch_errmsg((char *)res);
3865 vim_free(res);
3866 res = NULL;
3867 }
3868 mch_errmsg(_(": Send expression failed.\n"));
3869 }
3870 }
3871 else if (STRICMP(argv[i], "--serverlist") == 0)
3872 {
3873# ifdef WIN32
3874 /* Win32 always works? */
3875 res = serverGetVimNames();
3876# else
3877 if (xterm_dpy != NULL)
3878 res = serverGetVimNames(xterm_dpy);
3879# endif
3880 if (called_emsg)
3881 mch_errmsg("\n");
3882 }
3883 else if (STRICMP(argv[i], "--servername") == 0)
3884 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003885 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003886 i++;
3887 continue;
3888 }
3889 else
3890 {
3891 *newArgV++ = argv[i];
3892 newArgC++;
3893 continue;
3894 }
3895 didone = TRUE;
3896 if (res != NULL && *res != NUL)
3897 {
3898 mch_msg((char *)res);
3899 if (res[STRLEN(res) - 1] != '\n')
3900 mch_msg("\n");
3901 }
3902 vim_free(res);
3903 }
3904
3905 if (didone)
3906 {
3907 display_errors(); /* display any collected messages */
3908 exit(exiterr); /* Mission accomplished - get out */
3909 }
3910
3911 /* Return back into main() */
3912 *argc = newArgC;
3913 vim_free(sname);
3914}
3915
3916/*
3917 * Build a ":drop" command to send to a Vim server.
3918 */
3919 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003920build_drop_cmd(
3921 int filec,
3922 char **filev,
3923 int tabs, /* Use ":tab drop" instead of ":drop". */
3924 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003925{
3926 garray_T ga;
3927 int i;
3928 char_u *inicmd = NULL;
3929 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003930 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003931 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003932
3933 if (filec > 0 && filev[0][0] == '+')
3934 {
3935 inicmd = (char_u *)filev[0] + 1;
3936 filev++;
3937 filec--;
3938 }
3939 /* Check if we have at least one argument. */
3940 if (filec <= 0)
3941 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003942
3943 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003944 cwd = alloc(MAXPATHL);
3945 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003946 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003947 if (mch_dirname(cwd, MAXPATHL) != OK)
3948 {
3949 vim_free(cwd);
3950 return NULL;
3951 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003952 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003953#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003954 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00003955#else
3956 PATH_ESC_CHARS,
3957#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003958 '\\', TRUE);
3959 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003960 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003961 return NULL;
3962 ga_init2(&ga, 1, 100);
3963 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003964 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003965
3966 /* Call inputsave() so that a prompt for an encryption key works. */
3967 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3968 if (tabs)
3969 ga_concat(&ga, (char_u *)"tab ");
3970 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003971 for (i = 0; i < filec; i++)
3972 {
3973 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003974 * do it again in the Vim server. On MS-Windows only escape
3975 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003976 p = vim_strsave_escaped((char_u *)filev[i],
3977#ifdef UNIX
3978 PATH_ESC_CHARS
3979#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003980 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003981#endif
3982 );
3983 if (p == NULL)
3984 {
3985 vim_free(ga.ga_data);
3986 return NULL;
3987 }
3988 ga_concat(&ga, (char_u *)" ");
3989 ga_concat(&ga, p);
3990 vim_free(p);
3991 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003992 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3993
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003994 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3995 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003996 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3997
3998 /* Switch back to the correct current directory (prior to temporary path
3999 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004000 * correct after the :drop command. With line breaks and spaces:
4001 * if !exists('+acd') || !&acd
4002 * if haslocaldir()
4003 * cd -
4004 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004005 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004006 * cd -
4007 * endif
4008 * endif
4009 */
4010 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004011 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004012 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004013 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004014 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004015
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004016 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004017 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4018 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004019 if (inicmd != NULL)
4020 {
4021 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4022 * the following commands to be inserted as text. Use a "|",
4023 * hopefully "inicmd" does allow this... */
4024 ga_concat(&ga, inicmd);
4025 ga_concat(&ga, (char_u *)"|");
4026 }
4027 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4028 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004029 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004030 ga_append(&ga, NUL);
4031 return ga.ga_data;
4032}
4033
4034/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004035 * Make our basic server name: use the specified "arg" if given, otherwise use
4036 * the tail of the command "cmd" we were started with.
4037 * Return the name in allocated memory. This doesn't include a serial number.
4038 */
4039 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004040serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004041{
4042 char_u *p;
4043
4044 if (arg != NULL && *arg != NUL)
4045 p = vim_strsave_up(arg);
4046 else
4047 {
4048 p = vim_strsave_up(gettail((char_u *)cmd));
4049 /* Remove .exe or .bat from the name. */
4050 if (p != NULL && vim_strchr(p, '.') != NULL)
4051 *vim_strchr(p, '.') = NUL;
4052 }
4053 return p;
4054}
4055#endif /* FEAT_CLIENTSERVER */
4056
4057#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4058/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004059 * Replace termcodes such as <CR> and insert as key presses if there is room.
4060 */
4061 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004062server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004063{
4064 char_u *ptr = NULL;
4065 char_u *cpo_save = p_cpo;
4066
4067 /* Set 'cpoptions' the way we want it.
4068 * B set - backslashes are *not* treated specially
4069 * k set - keycodes are *not* reverse-engineered
4070 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004071 * The last but one parameter of replace_termcodes() is TRUE so that the
4072 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004073 */
4074 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004075 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004076 p_cpo = cpo_save;
4077
4078 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4079 {
4080 /*
4081 * Add the string to the input stream.
4082 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4083 *
4084 * First clear typed characters from the typeahead buffer, there could
4085 * be half a mapping there. Then append to the existing string, so
4086 * that multiple commands from a client are concatenated.
4087 */
4088 if (typebuf.tb_maplen < typebuf.tb_len)
4089 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4090 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4091
4092 /* Let input_available() know we inserted text in the typeahead
4093 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004094 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004095 }
4096 vim_free((char_u *)ptr);
4097}
4098
4099/*
4100 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004101 */
4102 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004103eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004104{
4105 char_u *res;
4106 int save_dbl = debug_break_level;
4107 int save_ro = redir_off;
4108
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004109 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4110 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004111 debug_break_level = -1;
4112 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004113 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4114 * to be typed. Do generate errors so that try/catch works. */
4115 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004116
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004117 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004118
4119 debug_break_level = save_dbl;
4120 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004121 --emsg_silent;
4122 if (emsg_silent < 0)
4123 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004124
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004125 /* A client can tell us to redraw, but not to display the cursor, so do
4126 * that here. */
4127 setcursor();
4128 out_flush();
4129#ifdef FEAT_GUI
4130 if (gui.in_use)
4131 gui_update_cursor(FALSE, FALSE);
4132#endif
4133
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004134 return res;
4135}
4136
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004137/*
4138 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4139 * return an allocated string. Otherwise return "data".
4140 * "*tofree" is set to the result when it needs to be freed later.
4141 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004142 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004143serverConvert(
4144 char_u *client_enc UNUSED,
4145 char_u *data,
4146 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004147{
4148 char_u *res = data;
4149
4150 *tofree = NULL;
4151# ifdef FEAT_MBYTE
4152 if (client_enc != NULL && p_enc != NULL)
4153 {
4154 vimconv_T vimconv;
4155
4156 vimconv.vc_type = CONV_NONE;
4157 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4158 && vimconv.vc_type != CONV_NONE)
4159 {
4160 res = string_convert(&vimconv, data, NULL);
4161 if (res == NULL)
4162 res = data;
4163 else
4164 *tofree = res;
4165 }
4166 convert_setup(&vimconv, NULL, NULL);
4167 }
4168# endif
4169 return res;
4170}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004171#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004172
4173/*
4174 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4175 */
4176#if defined(FEAT_FKMAP) || defined(PROTO)
4177# include "farsi.c"
4178#endif
4179
4180/*
4181 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4182 */
4183#if defined(FEAT_ARABIC) || defined(PROTO)
4184# include "arabic.c"
4185#endif