blob: ee375000327c376a39352b799ade95655da11028 [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 Moolenaarbaec5c12016-04-06 23:06:23 +0200962 /* 'autochdir' has been postponed */
963 DO_AUTOCHDIR
964
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000965#ifdef FEAT_TERMRESPONSE
966 /* Requesting the termresponse is postponed until here, so that a "-c q"
967 * argument doesn't make it appear in the shell Vim was started from. */
968 may_req_termresponse();
969#endif
970
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000971 /* start in insert mode */
972 if (p_im)
973 need_start_insertmode = TRUE;
974
Bram Moolenaar14735512016-03-26 21:00:08 +0100975#ifdef FEAT_EVAL
976 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
977#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000978#ifdef FEAT_AUTOCMD
979 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
980 TIME_MSG("VimEnter autocommands");
981#endif
982
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200983#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
984 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
985 * done after the clipboard is available and all initial commands that may
986 * modify the 'clipboard' setting have run; i.e. just before entering the
987 * main loop. */
988 {
989 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100990
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200991 adjust_clip_reg(&default_regname);
992 set_reg_var(default_regname);
993 }
994#endif
995
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000996#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
997 /* When a startup script or session file setup for diff'ing and
998 * scrollbind, sync the scrollbind now. */
999 if (curwin->w_p_diff && curwin->w_p_scb)
1000 {
1001 update_topline();
1002 check_scrollbind((linenr_T)0, 0L);
1003 TIME_MSG("diff scrollbinding");
1004 }
1005#endif
1006
1007#if defined(WIN3264) && !defined(FEAT_GUI_W32)
1008 mch_set_winsize_now(); /* Allow winsize changes from now on */
1009#endif
1010
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001011#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
1012 /* When tab pages were created, may need to update the tab pages line and
1013 * scrollbars. This is skipped while creating them. */
1014 if (first_tabpage->tp_next != NULL)
1015 {
1016 out_flush();
1017 gui_init_which_components(NULL);
1018 gui_update_scrollbars(TRUE);
1019 }
1020 need_mouse_correct = TRUE;
1021#endif
1022
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001023 /* If ":startinsert" command used, stuff a dummy command to be able to
1024 * call normal_cmd(), which will then start Insert mode. */
1025 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001026 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001027
1028#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001029 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001030 {
1031# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +02001032# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02001033 && !defined(FEAT_GUI_W32)
1034 if (gui.in_use)
1035 {
1036 mch_errmsg(_("netbeans is not supported with this GUI\n"));
1037 mch_exit(2);
1038 }
1039# endif
1040# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001041 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001042 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001043 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001044#endif
1045
1046 TIME_MSG("before starting main loop");
1047
1048 /*
1049 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001050 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001051 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001052
1053 return 0;
1054}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001055#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001056#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001057
1058/*
1059 * Main loop: Execute Normal mode commands until exiting Vim.
1060 * Also used to handle commands in the command-line window, until the window
1061 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 * Also used to handle ":visual" command after ":global": execute Normal mode
1063 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001064 */
1065 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001066main_loop(
1067 int cmdwin, /* TRUE when working in the command-line window */
1068 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001069{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001070 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001071 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001072#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001073 /* these are static to avoid a compiler warning */
1074 static linenr_T conceal_old_cursor_line = 0;
1075 static linenr_T conceal_new_cursor_line = 0;
1076 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001077#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001078
1079#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1080 /* Setup to catch a terminating error from the X server. Just ignore
1081 * it, restore the state and continue. This might not always work
1082 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001083 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001084 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001085 {
1086 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001087 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001088 got_int = TRUE;
1089 need_wait_return = FALSE;
1090 global_busy = FALSE;
1091 exmode_active = 0;
1092 skip_redraw = FALSE;
1093 RedrawingDisabled = 0;
1094 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001095 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001096# ifdef FEAT_EVAL
1097 emsg_skip = 0;
1098# endif
1099 emsg_off = 0;
1100# ifdef FEAT_MOUSE
1101 setmouse();
1102# endif
1103 settmode(TMODE_RAW);
1104 starttermcap();
1105 scroll_start();
1106 redraw_later_clear();
1107 }
1108#endif
1109
1110 clear_oparg(&oa);
1111 while (!cmdwin
1112#ifdef FEAT_CMDWIN
1113 || cmdwin_result == 0
1114#endif
1115 )
1116 {
1117 if (stuff_empty())
1118 {
1119 did_check_timestamps = FALSE;
1120 if (need_check_timestamps)
1121 check_timestamps(FALSE);
1122 if (need_wait_return) /* if wait_return still needed ... */
1123 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001124 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001125 {
1126 need_start_insertmode = FALSE;
1127 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1128 /* skip the fileinfo message now, because it would be shown
1129 * after insert mode finishes! */
1130 need_fileinfo = FALSE;
1131 }
1132 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001133
1134 /* Reset "got_int" now that we got back to the main loop. Except when
1135 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1136 * the ":g" command.
1137 * For ":g/pat/vi" we reset "got_int" when used once. When used
1138 * a second time we go back to Ex mode and abort the ":g" command. */
1139 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001140 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001141 if (noexmode && global_busy && !exmode_active && previous_got_int)
1142 {
1143 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1144 * used and keep "got_int" set, so that it aborts ":g". */
1145 exmode_active = EXMODE_NORMAL;
1146 State = NORMAL;
1147 }
1148 else if (!global_busy || !exmode_active)
1149 {
1150 if (!quit_more)
1151 (void)vgetc(); /* flush all buffers */
1152 got_int = FALSE;
1153 }
1154 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001155 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001156 else
1157 previous_got_int = FALSE;
1158
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001159 if (!exmode_active)
1160 msg_scroll = FALSE;
1161 quit_more = FALSE;
1162
1163 /*
1164 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1165 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1166 * update cursor and redraw.
1167 */
1168 if (skip_redraw || exmode_active)
1169 skip_redraw = FALSE;
1170 else if (do_redraw || stuff_empty())
1171 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001172#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001173 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001174 if (!finish_op && (
1175# ifdef FEAT_AUTOCMD
1176 has_cursormoved()
1177# endif
1178# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1179 ||
1180# endif
1181# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001182 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001183# endif
1184 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001185# ifdef FEAT_AUTOCMD
1186 && !equalpos(last_cursormoved, curwin->w_cursor)
1187# endif
1188 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001189 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001190# ifdef FEAT_AUTOCMD
1191 if (has_cursormoved())
1192 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1193 FALSE, curbuf);
1194# endif
1195# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001196 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001197 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001198# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001199 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001200# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001201 conceal_new_cursor_line = curwin->w_cursor.lnum;
1202 conceal_update_lines = TRUE;
1203 }
1204# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001205# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001206 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001207# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001208 }
1209#endif
1210
Bram Moolenaar186628f2013-03-19 13:33:23 +01001211#ifdef FEAT_AUTOCMD
1212 /* Trigger TextChanged if b_changedtick differs. */
1213 if (!finish_op && has_textchanged()
1214 && last_changedtick != curbuf->b_changedtick)
1215 {
1216 if (last_changedtick_buf == curbuf)
1217 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1218 FALSE, curbuf);
1219 last_changedtick_buf = curbuf;
1220 last_changedtick = curbuf->b_changedtick;
1221 }
1222#endif
1223
Bram Moolenaar33aec762006-01-22 23:30:12 +00001224#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1225 /* Scroll-binding for diff mode may have been postponed until
1226 * here. Avoids doing it for every change. */
1227 if (diff_need_scrollbind)
1228 {
1229 check_scrollbind((linenr_T)0, 0L);
1230 diff_need_scrollbind = FALSE;
1231 }
1232#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001233#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001234 /* Include a closed fold completely in the Visual area. */
1235 foldAdjustVisual();
1236#endif
1237#ifdef FEAT_FOLDING
1238 /*
1239 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1240 * contain the cursor.
1241 * When 'foldopen' is "all", open the fold(s) under the cursor.
1242 * This may mark the window for redrawing.
1243 */
1244 if (hasAnyFolding(curwin) && !char_avail())
1245 {
1246 foldCheckClose();
1247 if (fdo_flags & FDO_ALL)
1248 foldOpenCursor();
1249 }
1250#endif
1251
1252 /*
1253 * Before redrawing, make sure w_topline is correct, and w_leftcol
1254 * if lines don't wrap, and w_skipcol if lines wrap.
1255 */
1256 update_topline();
1257 validate_cursor();
1258
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001259 if (VIsual_active)
1260 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001261 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001262 update_screen(0);
1263 else if (redraw_cmdline || clear_cmdline)
1264 showmode();
1265#ifdef FEAT_WINDOWS
1266 redraw_statuslines();
1267#endif
1268#ifdef FEAT_TITLE
1269 if (need_maketitle)
1270 maketitle();
1271#endif
1272 /* display message after redraw */
1273 if (keep_msg != NULL)
1274 {
1275 char_u *p;
1276
1277 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001278 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1279 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001280 p = keep_msg;
1281 msg_attr(p, keep_msg_attr);
1282 vim_free(p);
1283 }
1284 if (need_fileinfo) /* show file info after redraw */
1285 {
1286 fileinfo(FALSE, TRUE, FALSE);
1287 need_fileinfo = FALSE;
1288 }
1289
1290 emsg_on_display = FALSE; /* can delete error message now */
1291 did_emsg = FALSE;
1292 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001293 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001294 showruler(FALSE);
1295
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001296# if defined(FEAT_CONCEAL)
1297 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001298 && (conceal_old_cursor_line != conceal_new_cursor_line
1299 || conceal_cursor_line(curwin)
1300 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001301 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001302 if (conceal_old_cursor_line != conceal_new_cursor_line
1303 && conceal_old_cursor_line
1304 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001305 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001306 update_single_line(curwin, conceal_new_cursor_line);
1307 curwin->w_valid &= ~VALID_CROW;
1308 }
1309# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001310 setcursor();
1311 cursor_on();
1312
1313 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001314
1315#ifdef STARTUPTIME
1316 /* Now that we have drawn the first screen all the startup stuff
1317 * has been done, close any file for startup messages. */
1318 if (time_fd != NULL)
1319 {
1320 TIME_MSG("first screen update");
1321 TIME_MSG("--- VIM STARTED ---");
1322 fclose(time_fd);
1323 time_fd = NULL;
1324 }
1325#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001326 }
1327#ifdef FEAT_GUI
1328 if (need_mouse_correct)
1329 gui_mouse_correct();
1330#endif
1331
1332 /*
1333 * Update w_curswant if w_set_curswant has been set.
1334 * Postponed until here to avoid computing w_virtcol too often.
1335 */
1336 update_curswant();
1337
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001338#ifdef FEAT_EVAL
1339 /*
1340 * May perform garbage collection when waiting for a character, but
1341 * only at the very toplevel. Otherwise we may be using a List or
1342 * Dict internally somewhere.
1343 * "may_garbage_collect" is reset in vgetc() which is invoked through
1344 * do_exmode() and normal_cmd().
1345 */
1346 may_garbage_collect = (!cmdwin && !noexmode);
1347#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001348 /*
1349 * If we're invoked as ex, do a round of ex commands.
1350 * Otherwise, get and execute a normal mode command.
1351 */
1352 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001353 {
1354 if (noexmode) /* End of ":global/path/visual" commands */
1355 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001356 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001357 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001358 else
1359 normal_cmd(&oa, TRUE);
1360 }
1361}
1362
1363
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001364#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001365/*
1366 * Exit, but leave behind swap files for modified buffers.
1367 */
1368 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001369getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001370{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001371# if defined(SIGHUP) && defined(SIG_IGN)
1372 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1373 * makes Vim exit and then handling SIGHUP causes various reentrance
1374 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001375 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001376# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001377
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001378 ml_close_notmod(); /* close all not-modified buffers */
1379 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1380 ml_close_all(FALSE); /* close all memfiles, without deleting */
1381 getout(exitval); /* exit Vim properly */
1382}
1383#endif
1384
1385
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001386/*
1387 * Exit properly.
1388 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001389 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001390getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001391{
1392#ifdef FEAT_AUTOCMD
1393 buf_T *buf;
1394 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001395 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001396#endif
1397
1398 exiting = TRUE;
1399
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001400 /* When running in Ex mode an error causes us to exit with a non-zero exit
1401 * code. POSIX requires this, although it's not 100% clear from the
1402 * standard. */
1403 if (exmode_active)
1404 exitval += ex_exitval;
1405
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001406 /* Position the cursor on the last screen line, below all the text */
1407#ifdef FEAT_GUI
1408 if (!gui.in_use)
1409#endif
1410 windgoto((int)Rows - 1, 0);
1411
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001412#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1413 /* Optionally print hashtable efficiency. */
1414 hash_debug_results();
1415#endif
1416
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001417#ifdef FEAT_GUI
1418 msg_didany = FALSE;
1419#endif
1420
1421#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001422 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001423 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001424 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1425# if defined FEAT_WINDOWS
1426 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001427 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001428 next_tp = tp->tp_next;
1429 for (wp = (tp == curtab)
1430 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001431 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001432 if (wp->w_buffer == NULL)
1433 /* Autocmd must have close the buffer already, skip. */
1434 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001435 buf = wp->w_buffer;
1436 if (buf->b_changedtick != -1)
1437 {
1438 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1439 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001440 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001441 /* start all over, autocommands may mess up the lists */
1442 next_tp = first_tabpage;
1443 break;
1444 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001445 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001446 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001447# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001448 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1449 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001450# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001451
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001452 /* Trigger BufUnload for buffers that are loaded */
1453 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1454 if (buf->b_ml.ml_mfp != NULL)
1455 {
1456 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001457 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001458 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1459 break;
1460 }
1461 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1462 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001463#endif
1464
1465#ifdef FEAT_VIMINFO
1466 if (*p_viminfo != NUL)
1467 /* Write out the registers, history, marks etc, to the viminfo file */
1468 write_viminfo(NULL, FALSE);
1469#endif
1470
1471#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001472 if (get_vim_var_nr(VV_DYING) <= 1)
1473 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001474#endif
1475
Bram Moolenaar05159a02005-02-26 23:04:13 +00001476#ifdef FEAT_PROFILE
1477 profile_dump();
1478#endif
1479
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001480 if (did_emsg
1481#ifdef FEAT_GUI
1482 || (gui.in_use && msg_didany && p_verbose > 0)
1483#endif
1484 )
1485 {
1486 /* give the user a chance to read the (error) message */
1487 no_wait_return = FALSE;
1488 wait_return(FALSE);
1489 }
1490
1491#ifdef FEAT_AUTOCMD
1492 /* Position the cursor again, the autocommands may have moved it */
1493# ifdef FEAT_GUI
1494 if (!gui.in_use)
1495# endif
1496 windgoto((int)Rows - 1, 0);
1497#endif
1498
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001499#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001500 job_stop_on_exit();
1501#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001502#ifdef FEAT_LUA
1503 lua_end();
1504#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001505#ifdef FEAT_MZSCHEME
1506 mzscheme_end();
1507#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001508#ifdef FEAT_TCL
1509 tcl_end();
1510#endif
1511#ifdef FEAT_RUBY
1512 ruby_end();
1513#endif
1514#ifdef FEAT_PYTHON
1515 python_end();
1516#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001517#ifdef FEAT_PYTHON3
1518 python3_end();
1519#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001520#ifdef FEAT_PERL
1521 perl_end();
1522#endif
1523#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1524 iconv_end();
1525#endif
1526#ifdef FEAT_NETBEANS_INTG
1527 netbeans_end();
1528#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001529#ifdef FEAT_CSCOPE
1530 cs_end();
1531#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001532#ifdef FEAT_EVAL
1533 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001534 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001535#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001536#if defined(WIN32) && defined(FEAT_MBYTE)
1537 free_cmd_argsW();
1538#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001539
1540 mch_exit(exitval);
1541}
1542
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001543#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001544/*
1545 * Get a (optional) count for a Vim argument.
1546 */
1547 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001548get_number_arg(
1549 char_u *p, /* pointer to argument */
1550 int *idx, /* index in argument, is incremented */
1551 int def) /* default value */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001552{
1553 if (vim_isdigit(p[*idx]))
1554 {
1555 def = atoi((char *)&(p[*idx]));
1556 while (vim_isdigit(p[*idx]))
1557 *idx = *idx + 1;
1558 }
1559 return def;
1560}
1561
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001562#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1563/*
1564 * Setup to use the current locale (for ctype() and many other things).
1565 */
1566 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001567init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001568{
1569 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001570
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001571# ifdef FEAT_GUI_GTK
1572 /* Tell Gtk not to change our locale settings. */
1573 gtk_disable_setlocale();
1574# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001575# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1576 /* Make sure strtod() uses a decimal point, not a comma. */
1577 setlocale(LC_NUMERIC, "C");
1578# endif
1579
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001580# ifdef WIN32
1581 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1582 * text while it's expecting text in the current locale. This call avoids
1583 * that. */
1584 setlocale(LC_CTYPE, "C");
1585# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001586
1587# ifdef FEAT_GETTEXT
1588 {
1589 int mustfree = FALSE;
1590 char_u *p;
1591
1592# ifdef DYNAMIC_GETTEXT
1593 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001594 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001595# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001596 /* expand_env() doesn't work yet, because g_chartab[] is not
1597 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001598 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1599 if (p != NULL && *p != NUL)
1600 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001601 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001602 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1603 }
1604 if (mustfree)
1605 vim_free(p);
1606 textdomain(VIMPACKAGE);
1607 }
1608# endif
1609}
1610#endif
1611
1612/*
1613 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1614 * If the executable name starts with "r" we disable shell commands.
1615 * If the next character is "e" we run in Easy mode.
1616 * If the next character is "g" we run the GUI version.
1617 * If the next characters are "view" we start in readonly mode.
1618 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1619 * If the next characters are "ex" we start in Ex mode. If it's followed
1620 * by "im" use improved Ex mode.
1621 */
1622 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001623parse_command_name(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001624{
1625 char_u *initstr;
1626
1627 initstr = gettail((char_u *)parmp->argv[0]);
1628
1629#ifdef MACOS_X_UNIX
1630 /* An issue has been seen when launching Vim in such a way that
1631 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1632 * executable or a symbolic link of it. Until this issue is resolved
1633 * we prohibit the GUI from being used.
1634 */
1635 if (STRCMP(initstr, parmp->argv[0]) == 0)
1636 disallow_gui = TRUE;
1637
1638 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001639 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001640#endif
1641
1642#ifdef FEAT_EVAL
1643 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaara1706c92014-04-01 19:55:49 +02001644 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001645#endif
1646
1647 if (TOLOWER_ASC(initstr[0]) == 'r')
1648 {
1649 restricted = TRUE;
1650 ++initstr;
1651 }
1652
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001653 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001654 if (TOLOWER_ASC(initstr[0]) == 'e'
1655 && (TOLOWER_ASC(initstr[1]) == 'v'
1656 || TOLOWER_ASC(initstr[1]) == 'g'))
1657 {
1658#ifdef FEAT_GUI
1659 gui.starting = TRUE;
1660#endif
1661 parmp->evim_mode = TRUE;
1662 ++initstr;
1663 }
1664
Bram Moolenaar806875d2008-09-18 18:57:10 +00001665 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1666 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001667 {
1668 main_start_gui();
1669#ifdef FEAT_GUI
1670 ++initstr;
1671#endif
1672 }
1673
1674 if (STRNICMP(initstr, "view", 4) == 0)
1675 {
1676 readonlymode = TRUE;
1677 curbuf->b_p_ro = TRUE;
1678 p_uc = 10000; /* don't update very often */
1679 initstr += 4;
1680 }
1681 else if (STRNICMP(initstr, "vim", 3) == 0)
1682 initstr += 3;
1683
1684 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1685 if (STRICMP(initstr, "diff") == 0)
1686 {
1687#ifdef FEAT_DIFF
1688 parmp->diff_mode = TRUE;
1689#else
1690 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1691 mch_errmsg("\n");
1692 mch_exit(2);
1693#endif
1694 }
1695
1696 if (STRNICMP(initstr, "ex", 2) == 0)
1697 {
1698 if (STRNICMP(initstr + 2, "im", 2) == 0)
1699 exmode_active = EXMODE_VIM;
1700 else
1701 exmode_active = EXMODE_NORMAL;
1702 change_compatible(TRUE); /* set 'compatible' */
1703 }
1704}
1705
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001706/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001707 * Get the name of the display, before gui_prepare() removes it from
1708 * argv[]. Used for the xterm-clipboard display.
1709 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001710 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001711 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001712 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001713early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001714{
Bram Moolenaar03005972008-11-20 13:12:36 +00001715#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1716 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001717 int argc = parmp->argc;
1718 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001719 int i;
1720
1721 for (i = 1; i < argc; i++)
1722 {
1723 if (STRCMP(argv[i], "--") == 0)
1724 break;
1725# ifdef FEAT_XCLIPBOARD
1726 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001727# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001728 || STRICMP(argv[i], "--display") == 0
1729# endif
1730 )
1731 {
1732 if (i == argc - 1)
1733 mainerr_arg_missing((char_u *)argv[i]);
1734 xterm_display = argv[++i];
1735 }
1736# endif
1737# ifdef FEAT_CLIENTSERVER
1738 else if (STRICMP(argv[i], "--servername") == 0)
1739 {
1740 if (i == argc - 1)
1741 mainerr_arg_missing((char_u *)argv[i]);
1742 parmp->serverName_arg = (char_u *)argv[++i];
1743 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001744 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001745 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001746 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001747 {
1748 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001749# ifdef FEAT_GUI
1750 if (strstr(argv[i], "-wait") != 0)
1751 /* don't fork() when starting the GUI to edit files ourself */
1752 gui.dofork = FALSE;
1753# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001754 }
1755# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001756
1757# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1758# ifdef FEAT_GUI_W32
1759 else if (STRICMP(argv[i], "--windowid") == 0)
1760# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001761 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001762# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001763 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001764 long_u id;
1765 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001766
1767 if (i == argc - 1)
1768 mainerr_arg_missing((char_u *)argv[i]);
1769 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001770 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001771 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001772 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001773 if (count != 1)
1774 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1775 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001776# ifdef FEAT_GUI_W32
1777 win_socket_id = id;
1778# else
1779 gtk_socket_id = id;
1780# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001781 i++;
1782 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001783# endif
1784# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001785 else if (STRICMP(argv[i], "--echo-wid") == 0)
1786 echo_wid_arg = TRUE;
1787# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001788# ifndef FEAT_NETBEANS_INTG
1789 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001790 {
1791 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1792 mch_exit(2);
1793 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001794# endif
1795
Bram Moolenaar58d98232005-07-23 22:25:46 +00001796 }
1797#endif
1798}
1799
1800/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001801 * Scan the command line arguments.
1802 */
1803 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001804command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001805{
1806 int argc = parmp->argc;
1807 char **argv = parmp->argv;
1808 int argv_idx; /* index in argv[n][] */
1809 int had_minmin = FALSE; /* found "--" argument */
1810 int want_argument; /* option argument with argument */
1811 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001812 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001813 long n;
1814
1815 --argc;
1816 ++argv;
1817 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1818 while (argc > 0)
1819 {
1820 /*
1821 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1822 */
1823 if (argv[0][0] == '+' && !had_minmin)
1824 {
1825 if (parmp->n_commands >= MAX_ARG_CMDS)
1826 mainerr(ME_EXTRA_CMD, NULL);
1827 argv_idx = -1; /* skip to next argument */
1828 if (argv[0][1] == NUL)
1829 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1830 else
1831 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1832 }
1833
1834 /*
1835 * Optional argument.
1836 */
1837 else if (argv[0][0] == '-' && !had_minmin)
1838 {
1839 want_argument = FALSE;
1840 c = argv[0][argv_idx++];
1841#ifdef VMS
1842 /*
1843 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1844 * and "-/X" as "-X".
1845 */
1846 if (c == '/')
1847 {
1848 c = argv[0][argv_idx++];
1849 c = TOUPPER_ASC(c);
1850 }
1851 else
1852 c = TOLOWER_ASC(c);
1853#endif
1854 switch (c)
1855 {
1856 case NUL: /* "vim -" read from stdin */
1857 /* "ex -" silent mode */
1858 if (exmode_active)
1859 silent_mode = TRUE;
1860 else
1861 {
1862 if (parmp->edit_type != EDIT_NONE)
1863 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1864 parmp->edit_type = EDIT_STDIN;
1865 read_cmd_fd = 2; /* read from stderr instead of stdin */
1866 }
1867 argv_idx = -1; /* skip to next argument */
1868 break;
1869
1870 case '-': /* "--" don't take any more option arguments */
1871 /* "--help" give help message */
1872 /* "--version" give version message */
1873 /* "--literal" take files literally */
1874 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001875 /* "--not-a-term" don't warn for not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001876 /* "--noplugin[s]" skip plugins */
1877 /* "--cmd <cmd>" execute cmd before vimrc */
1878 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1879 usage();
1880 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1881 {
1882 Columns = 80; /* need to init Columns */
1883 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1884 list_version();
1885 msg_putchar('\n');
1886 msg_didout = FALSE;
1887 mch_exit(0);
1888 }
1889 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1890 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001891#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001892 parmp->literal = TRUE;
1893#endif
1894 }
1895 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1896 {
1897#ifdef FEAT_GUI
1898 gui.dofork = FALSE; /* don't fork() when starting GUI */
1899#endif
1900 }
1901 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1902 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001903 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1904 parmp->not_a_term = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001905 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1906 {
1907 want_argument = TRUE;
1908 argv_idx += 3;
1909 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001910 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1911 {
1912 want_argument = TRUE;
1913 argv_idx += 11;
1914 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001915#ifdef FEAT_CLIENTSERVER
1916 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1917 ; /* already processed -- no arg */
1918 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1919 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1920 {
1921 /* already processed -- snatch the following arg */
1922 if (argc > 1)
1923 {
1924 --argc;
1925 ++argv;
1926 }
1927 }
1928#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001929#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1930# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001931 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001932# else
1933 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1934# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001935 {
1936 /* already processed -- snatch the following arg */
1937 if (argc > 1)
1938 {
1939 --argc;
1940 ++argv;
1941 }
1942 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001943#endif
1944#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001945 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1946 {
1947 /* already processed, skip */
1948 }
1949#endif
1950 else
1951 {
1952 if (argv[0][argv_idx])
1953 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1954 had_minmin = TRUE;
1955 }
1956 if (!want_argument)
1957 argv_idx = -1; /* skip to next argument */
1958 break;
1959
1960 case 'A': /* "-A" start in Arabic mode */
1961#ifdef FEAT_ARABIC
1962 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1963#else
1964 mch_errmsg(_(e_noarabic));
1965 mch_exit(2);
1966#endif
1967 break;
1968
1969 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001970 /* Needs to be effective before expanding file names, because
1971 * for Win32 this makes us edit a shortcut file itself,
1972 * instead of the file it links to. */
1973 set_options_bin(curbuf->b_p_bin, 1, 0);
1974 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001975 break;
1976
1977 case 'C': /* "-C" Compatible */
1978 change_compatible(TRUE);
1979 break;
1980
1981 case 'e': /* "-e" Ex mode */
1982 exmode_active = EXMODE_NORMAL;
1983 break;
1984
1985 case 'E': /* "-E" Improved Ex mode */
1986 exmode_active = EXMODE_VIM;
1987 break;
1988
1989 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1990 window directly, not with newcli */
1991#ifdef FEAT_GUI
1992 gui.dofork = FALSE; /* don't fork() when starting GUI */
1993#endif
1994 break;
1995
1996 case 'g': /* "-g" start GUI */
1997 main_start_gui();
1998 break;
1999
2000 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
2001#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002002 p_fkmap = TRUE;
2003 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002004#else
2005 mch_errmsg(_(e_nofarsi));
2006 mch_exit(2);
2007#endif
2008 break;
2009
2010 case 'h': /* "-h" give help message */
2011#ifdef FEAT_GUI_GNOME
2012 /* Tell usage() to exit for "gvim". */
2013 gui.starting = FALSE;
2014#endif
2015 usage();
2016 break;
2017
2018 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2019#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002020 p_hkmap = TRUE;
2021 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002022#else
2023 mch_errmsg(_(e_nohebrew));
2024 mch_exit(2);
2025#endif
2026 break;
2027
2028 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2029#ifdef FEAT_LISP
2030 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2031 p_sm = TRUE;
2032#endif
2033 break;
2034
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002035 case 'M': /* "-M" no changes or writing of files */
2036 reset_modifiable();
2037 /* FALLTHROUGH */
2038
2039 case 'm': /* "-m" no writing of files */
2040 p_write = FALSE;
2041 break;
2042
2043 case 'y': /* "-y" easy mode */
2044#ifdef FEAT_GUI
2045 gui.starting = TRUE; /* start GUI a bit later */
2046#endif
2047 parmp->evim_mode = TRUE;
2048 break;
2049
2050 case 'N': /* "-N" Nocompatible */
2051 change_compatible(FALSE);
2052 break;
2053
2054 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002055#ifdef FEAT_NETBEANS_INTG
2056 /* checking for "-nb", netbeans parameters */
2057 if (argv[0][argv_idx] == 'b')
2058 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002059 netbeansArg = argv[0];
2060 argv_idx = -1; /* skip to next argument */
2061 }
2062 else
2063#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002064 parmp->no_swap_file = TRUE;
2065 break;
2066
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002067 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002068#ifdef TARGET_API_MAC_OSX
2069 /* For some reason on MacOS X, an argument like:
2070 -psn_0_10223617 is passed in when invoke from Finder
2071 or with the 'open' command */
2072 if (argv[0][argv_idx] == 's')
2073 {
2074 argv_idx = -1; /* bypass full -psn */
2075 main_start_gui();
2076 break;
2077 }
2078#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002079#ifdef FEAT_WINDOWS
2080 /* default is 0: open window for each file */
2081 parmp->window_count = get_number_arg((char_u *)argv[0],
2082 &argv_idx, 0);
2083 parmp->window_layout = WIN_TABS;
2084#endif
2085 break;
2086
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002087 case 'o': /* "-o[N]" open N horizontal split windows */
2088#ifdef FEAT_WINDOWS
2089 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002090 parmp->window_count = get_number_arg((char_u *)argv[0],
2091 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002092 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002093#endif
2094 break;
2095
2096 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002097#ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002098 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002099 parmp->window_count = get_number_arg((char_u *)argv[0],
2100 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002101 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002102#endif
2103 break;
2104
2105#ifdef FEAT_QUICKFIX
2106 case 'q': /* "-q" QuickFix mode */
2107 if (parmp->edit_type != EDIT_NONE)
2108 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2109 parmp->edit_type = EDIT_QF;
2110 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2111 {
2112 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2113 argv_idx = -1;
2114 }
2115 else if (argc > 1) /* "-q {errorfile}" */
2116 want_argument = TRUE;
2117 break;
2118#endif
2119
2120 case 'R': /* "-R" readonly mode */
2121 readonlymode = TRUE;
2122 curbuf->b_p_ro = TRUE;
2123 p_uc = 10000; /* don't update very often */
2124 break;
2125
2126 case 'r': /* "-r" recovery mode */
2127 case 'L': /* "-L" recovery mode */
2128 recoverymode = 1;
2129 break;
2130
2131 case 's':
2132 if (exmode_active) /* "-s" silent (batch) mode */
2133 silent_mode = TRUE;
2134 else /* "-s {scriptin}" read from script file */
2135 want_argument = TRUE;
2136 break;
2137
2138 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2139 if (parmp->edit_type != EDIT_NONE)
2140 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2141 parmp->edit_type = EDIT_TAG;
2142 if (argv[0][argv_idx]) /* "-t{tag}" */
2143 {
2144 parmp->tagname = (char_u *)argv[0] + argv_idx;
2145 argv_idx = -1;
2146 }
2147 else /* "-t {tag}" */
2148 want_argument = TRUE;
2149 break;
2150
2151#ifdef FEAT_EVAL
2152 case 'D': /* "-D" Debugging */
2153 parmp->use_debug_break_level = 9999;
2154 break;
2155#endif
2156#ifdef FEAT_DIFF
2157 case 'd': /* "-d" 'diff' */
2158# ifdef AMIGA
2159 /* check for "-dev {device}" */
2160 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2161 want_argument = TRUE;
2162 else
2163# endif
2164 parmp->diff_mode = TRUE;
2165 break;
2166#endif
2167 case 'V': /* "-V{N}" Verbose level */
2168 /* default is 10: a little bit verbose */
2169 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2170 if (argv[0][argv_idx] != NUL)
2171 {
2172 set_option_value((char_u *)"verbosefile", 0L,
2173 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002174 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002175 }
2176 break;
2177
2178 case 'v': /* "-v" Vi-mode (as if called "vi") */
2179 exmode_active = 0;
2180#ifdef FEAT_GUI
2181 gui.starting = FALSE; /* don't start GUI */
2182#endif
2183 break;
2184
2185 case 'w': /* "-w{number}" set window height */
2186 /* "-w {scriptout}" write to script */
2187 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2188 {
2189 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2190 set_option_value((char_u *)"window", n, NULL, 0);
2191 break;
2192 }
2193 want_argument = TRUE;
2194 break;
2195
2196#ifdef FEAT_CRYPT
2197 case 'x': /* "-x" encrypted reading/writing of files */
2198 parmp->ask_for_key = TRUE;
2199 break;
2200#endif
2201
2202 case 'X': /* "-X" don't connect to X server */
2203#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2204 x_no_connect = TRUE;
2205#endif
2206 break;
2207
2208 case 'Z': /* "-Z" restricted mode */
2209 restricted = TRUE;
2210 break;
2211
2212 case 'c': /* "-c{command}" or "-c {command}" execute
2213 command */
2214 if (argv[0][argv_idx] != NUL)
2215 {
2216 if (parmp->n_commands >= MAX_ARG_CMDS)
2217 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002218 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2219 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002220 argv_idx = -1;
2221 break;
2222 }
2223 /*FALLTHROUGH*/
2224 case 'S': /* "-S {file}" execute Vim script */
2225 case 'i': /* "-i {viminfo}" use for viminfo */
2226#ifndef FEAT_DIFF
2227 case 'd': /* "-d {device}" device (for Amiga) */
2228#endif
2229 case 'T': /* "-T {terminal}" terminal name */
2230 case 'u': /* "-u {vimrc}" vim inits file */
2231 case 'U': /* "-U {gvimrc}" gvim inits file */
2232 case 'W': /* "-W {scriptout}" overwrite */
2233#ifdef FEAT_GUI_W32
2234 case 'P': /* "-P {parent title}" MDI parent */
2235#endif
2236 want_argument = TRUE;
2237 break;
2238
2239 default:
2240 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2241 }
2242
2243 /*
2244 * Handle option arguments with argument.
2245 */
2246 if (want_argument)
2247 {
2248 /*
2249 * Check for garbage immediately after the option letter.
2250 */
2251 if (argv[0][argv_idx] != NUL)
2252 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2253
2254 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002255 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002256 mainerr_arg_missing((char_u *)argv[0]);
2257 ++argv;
2258 argv_idx = -1;
2259
2260 switch (c)
2261 {
2262 case 'c': /* "-c {command}" execute command */
2263 case 'S': /* "-S {file}" execute Vim script */
2264 if (parmp->n_commands >= MAX_ARG_CMDS)
2265 mainerr(ME_EXTRA_CMD, NULL);
2266 if (c == 'S')
2267 {
2268 char *a;
2269
2270 if (argc < 1)
2271 /* "-S" without argument: use default session file
2272 * name. */
2273 a = SESSION_FILE;
2274 else if (argv[0][0] == '-')
2275 {
2276 /* "-S" followed by another option: use default
2277 * session file name. */
2278 a = SESSION_FILE;
2279 ++argc;
2280 --argv;
2281 }
2282 else
2283 a = argv[0];
2284 p = alloc((unsigned)(STRLEN(a) + 4));
2285 if (p == NULL)
2286 mch_exit(2);
2287 sprintf((char *)p, "so %s", a);
2288 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2289 parmp->commands[parmp->n_commands++] = p;
2290 }
2291 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002292 parmp->commands[parmp->n_commands++] =
2293 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002294 break;
2295
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002296 case '-':
2297 if (argv[-1][2] == 'c')
2298 {
2299 /* "--cmd {command}" execute command */
2300 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2301 mainerr(ME_EXTRA_CMD, NULL);
2302 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002303 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002304 }
2305 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002306 break;
2307
2308 /* case 'd': -d {device} is handled in mch_check_win() for the
2309 * Amiga */
2310
2311#ifdef FEAT_QUICKFIX
2312 case 'q': /* "-q {errorfile}" QuickFix mode */
2313 parmp->use_ef = (char_u *)argv[0];
2314 break;
2315#endif
2316
2317 case 'i': /* "-i {viminfo}" use for viminfo */
2318 use_viminfo = (char_u *)argv[0];
2319 break;
2320
2321 case 's': /* "-s {scriptin}" read from script file */
2322 if (scriptin[0] != NULL)
2323 {
2324scripterror:
2325 mch_errmsg(_("Attempt to open script file again: \""));
2326 mch_errmsg(argv[-1]);
2327 mch_errmsg(" ");
2328 mch_errmsg(argv[0]);
2329 mch_errmsg("\"\n");
2330 mch_exit(2);
2331 }
2332 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2333 {
2334 mch_errmsg(_("Cannot open for reading: \""));
2335 mch_errmsg(argv[0]);
2336 mch_errmsg("\"\n");
2337 mch_exit(2);
2338 }
2339 if (save_typebuf() == FAIL)
2340 mch_exit(2); /* out of memory */
2341 break;
2342
2343 case 't': /* "-t {tag}" */
2344 parmp->tagname = (char_u *)argv[0];
2345 break;
2346
2347 case 'T': /* "-T {terminal}" terminal name */
2348 /*
2349 * The -T term argument is always available and when
2350 * HAVE_TERMLIB is supported it overrides the environment
2351 * variable TERM.
2352 */
2353#ifdef FEAT_GUI
2354 if (term_is_gui((char_u *)argv[0]))
2355 gui.starting = TRUE; /* start GUI a bit later */
2356 else
2357#endif
2358 parmp->term = (char_u *)argv[0];
2359 break;
2360
2361 case 'u': /* "-u {vimrc}" vim inits file */
2362 parmp->use_vimrc = (char_u *)argv[0];
2363 break;
2364
2365 case 'U': /* "-U {gvimrc}" gvim inits file */
2366#ifdef FEAT_GUI
2367 use_gvimrc = (char_u *)argv[0];
2368#endif
2369 break;
2370
2371 case 'w': /* "-w {nr}" 'window' value */
2372 /* "-w {scriptout}" append to script file */
2373 if (vim_isdigit(*((char_u *)argv[0])))
2374 {
2375 argv_idx = 0;
2376 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2377 set_option_value((char_u *)"window", n, NULL, 0);
2378 argv_idx = -1;
2379 break;
2380 }
2381 /*FALLTHROUGH*/
2382 case 'W': /* "-W {scriptout}" overwrite script file */
2383 if (scriptout != NULL)
2384 goto scripterror;
2385 if ((scriptout = mch_fopen(argv[0],
2386 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2387 {
2388 mch_errmsg(_("Cannot open for script output: \""));
2389 mch_errmsg(argv[0]);
2390 mch_errmsg("\"\n");
2391 mch_exit(2);
2392 }
2393 break;
2394
2395#ifdef FEAT_GUI_W32
2396 case 'P': /* "-P {parent title}" MDI parent */
2397 gui_mch_set_parent(argv[0]);
2398 break;
2399#endif
2400 }
2401 }
2402 }
2403
2404 /*
2405 * File name argument.
2406 */
2407 else
2408 {
2409 argv_idx = -1; /* skip to next argument */
2410
2411 /* Check for only one type of editing. */
2412 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2413 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2414 parmp->edit_type = EDIT_FILE;
2415
2416#ifdef MSWIN
2417 /* Remember if the argument was a full path before changing
2418 * slashes to backslashes. */
2419 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2420 parmp->full_path = TRUE;
2421#endif
2422
2423 /* Add the file to the global argument list. */
2424 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2425 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2426 mch_exit(2);
2427#ifdef FEAT_DIFF
2428 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2429 && !mch_isdir(alist_name(&GARGLIST[0])))
2430 {
2431 char_u *r;
2432
2433 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2434 if (r != NULL)
2435 {
2436 vim_free(p);
2437 p = r;
2438 }
2439 }
2440#endif
2441#if defined(__CYGWIN32__) && !defined(WIN32)
2442 /*
2443 * If vim is invoked by non-Cygwin tools, convert away any
2444 * DOS paths, so things like .swp files are created correctly.
2445 * Look for evidence of non-Cygwin paths before we bother.
2446 * This is only for when using the Unix files.
2447 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002448 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002449 {
2450 char posix_path[PATH_MAX];
2451
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002452# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2453 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2454# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002455 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002456# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002457 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002458 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002459 if (p == NULL)
2460 mch_exit(2);
2461 }
2462#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002463
2464#ifdef USE_FNAME_CASE
2465 /* Make the case of the file name match the actual file. */
2466 fname_case(p, 0);
2467#endif
2468
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002469 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002470#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002471 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002472#else
2473 2 /* add buffer number now and use curbuf */
2474#endif
2475 );
2476
2477#if defined(FEAT_MBYTE) && defined(WIN32)
2478 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002479 /* Remember this argument has been added to the argument list.
2480 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002481 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002482# ifdef FEAT_DIFF
2483 parmp->diff_mode
2484# else
2485 FALSE
2486# endif
2487 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002488 }
2489#endif
2490 }
2491
2492 /*
2493 * If there are no more letters after the current "-", go to next
2494 * argument. argv_idx is set to -1 when the current argument is to be
2495 * skipped.
2496 */
2497 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2498 {
2499 --argc;
2500 ++argv;
2501 argv_idx = 1;
2502 }
2503 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002504
2505#ifdef FEAT_EVAL
2506 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2507 * one. */
2508 if (parmp->n_commands > 0)
2509 {
2510 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2511 if (p != NULL)
2512 {
2513 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2514 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2515 vim_free(p);
2516 }
2517 }
2518#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002519}
2520
2521/*
2522 * Print a warning if stdout is not a terminal.
2523 * When starting in Ex mode and commands come from a file, set Silent mode.
2524 */
2525 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002526check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002527{
2528 int input_isatty; /* is active input a terminal? */
2529
2530 input_isatty = mch_input_isatty();
2531 if (exmode_active)
2532 {
2533 if (!input_isatty)
2534 silent_mode = TRUE;
2535 }
2536 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2537#ifdef FEAT_GUI
2538 /* don't want the delay when started from the desktop */
2539 && !gui.starting
2540#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002541 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002542 {
2543#ifdef NBDEBUG
2544 /*
2545 * This shouldn't be necessary. But if I run netbeans with the log
2546 * output coming to the console and XOpenDisplay fails, I get vim
2547 * trying to start with input/output to my console tty. This fills my
2548 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002549 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002550 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002551 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002552 {
2553 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2554 exit(1);
2555 }
2556#endif
2557 if (!parmp->stdout_isatty)
2558 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2559 if (!input_isatty)
2560 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2561 out_flush();
2562 if (scriptin[0] == NULL)
2563 ui_delay(2000L, TRUE);
2564 TIME_MSG("Warning delay");
2565 }
2566}
2567
2568/*
2569 * Read text from stdin.
2570 */
2571 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002572read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002573{
2574 int i;
2575
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002576#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002577 /* When getting the ATTENTION prompt here, use a dialog */
2578 swap_exists_action = SEA_DIALOG;
2579#endif
2580 no_wait_return = TRUE;
2581 i = msg_didany;
2582 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002583 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002584 no_wait_return = FALSE;
2585 msg_didany = i;
2586 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002587#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002588 check_swap_exists_action();
2589#endif
2590#if !(defined(AMIGA) || defined(MACOS))
2591 /*
2592 * Close stdin and dup it from stderr. Required for GPM to work
2593 * properly, and for running external commands.
2594 * Is there any other system that cannot do this?
2595 */
2596 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002597 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002598#endif
2599}
2600
2601/*
2602 * Create the requested number of windows and edit buffers in them.
2603 * Also does recovery if "recoverymode" set.
2604 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002605 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002606create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002607{
2608#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002609 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002610 int done = 0;
2611
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002612 /*
2613 * Create the number of windows that was requested.
2614 */
2615 if (parmp->window_count == -1) /* was not set */
2616 parmp->window_count = 1;
2617 if (parmp->window_count == 0)
2618 parmp->window_count = GARGCOUNT;
2619 if (parmp->window_count > 1)
2620 {
2621 /* Don't change the windows if there was a command in .vimrc that
2622 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002623 if (parmp->window_layout == 0)
2624 parmp->window_layout = WIN_HOR;
2625 if (parmp->window_layout == WIN_TABS)
2626 {
2627 parmp->window_count = make_tabpages(parmp->window_count);
2628 TIME_MSG("making tab pages");
2629 }
2630 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002631 {
2632 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002633 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002634 TIME_MSG("making windows");
2635 }
2636 else
2637 parmp->window_count = win_count();
2638 }
2639 else
2640 parmp->window_count = 1;
2641#endif
2642
2643 if (recoverymode) /* do recover */
2644 {
2645 msg_scroll = TRUE; /* scroll message up */
2646 ml_recover();
2647 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2648 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002649 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002650 }
2651 else
2652 {
2653 /*
2654 * Open a buffer for windows that don't have one yet.
2655 * Commands in the .vimrc might have loaded a file or split the window.
2656 * Watch out for autocommands that delete a window.
2657 */
2658#ifdef FEAT_AUTOCMD
2659 /*
2660 * Don't execute Win/Buf Enter/Leave autocommands here
2661 */
2662 ++autocmd_no_enter;
2663 ++autocmd_no_leave;
2664#endif
2665#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002666 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002667 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002668 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002669 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002670 {
2671 if (parmp->window_layout == WIN_TABS)
2672 goto_tabpage(1);
2673 else
2674 curwin = firstwin;
2675 }
2676 else if (parmp->window_layout == WIN_TABS)
2677 {
2678 if (curtab->tp_next == NULL)
2679 break;
2680 goto_tabpage(0);
2681 }
2682 else
2683 {
2684 if (curwin->w_next == NULL)
2685 break;
2686 curwin = curwin->w_next;
2687 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002688 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002689#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002690 curbuf = curwin->w_buffer;
2691 if (curbuf->b_ml.ml_mfp == NULL)
2692 {
2693#ifdef FEAT_FOLDING
2694 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2695 if (p_fdls >= 0)
2696 curwin->w_p_fdl = p_fdls;
2697#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002698#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002699 /* When getting the ATTENTION prompt here, use a dialog */
2700 swap_exists_action = SEA_DIALOG;
2701#endif
2702 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002703
2704 /* create memfile, read file */
2705 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002706
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002707#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002708 if (swap_exists_action == SEA_QUIT)
2709 {
2710 if (got_int || only_one_window())
2711 {
2712 /* abort selected or quit and only one window */
2713 did_emsg = FALSE; /* avoid hit-enter prompt */
2714 getout(1);
2715 }
2716 /* We can't close the window, it would disturb what
2717 * happens next. Clear the file name and set the arg
2718 * index to -1 to delete it later. */
2719 setfname(curbuf, NULL, NULL, FALSE);
2720 curwin->w_arg_idx = -1;
2721 swap_exists_action = SEA_NONE;
2722 }
2723 else
2724 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002725#endif
2726#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002727 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002728#endif
2729 }
2730#ifdef FEAT_WINDOWS
2731 ui_breakcheck();
2732 if (got_int)
2733 {
2734 (void)vgetc(); /* only break the file loading, not the rest */
2735 break;
2736 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002737 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002738#endif
2739#ifdef FEAT_WINDOWS
2740 if (parmp->window_layout == WIN_TABS)
2741 goto_tabpage(1);
2742 else
2743 curwin = firstwin;
2744 curbuf = curwin->w_buffer;
2745#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002746#ifdef FEAT_AUTOCMD
2747 --autocmd_no_enter;
2748 --autocmd_no_leave;
2749#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002750 }
2751}
2752
2753#ifdef FEAT_WINDOWS
2754 /*
2755 * If opened more than one window, start editing files in the other
2756 * windows. make_windows() has already opened the windows.
2757 */
2758 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002759edit_buffers(
2760 mparm_T *parmp,
2761 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002762{
2763 int arg_idx; /* index in argument list */
2764 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002765 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002766 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002767
2768# ifdef FEAT_AUTOCMD
2769 /*
2770 * Don't execute Win/Buf Enter/Leave autocommands here
2771 */
2772 ++autocmd_no_enter;
2773 ++autocmd_no_leave;
2774# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002775
2776 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2777 if (curwin->w_arg_idx == -1)
2778 {
2779 win_close(curwin, TRUE);
2780 advance = FALSE;
2781 }
2782
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002783 arg_idx = 1;
2784 for (i = 1; i < parmp->window_count; ++i)
2785 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002786 if (cwd != NULL)
2787 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002788 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2789 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002790 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002791 ++arg_idx;
2792 win_close(curwin, TRUE);
2793 advance = FALSE;
2794 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002795 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002796
Bram Moolenaar84212822006-11-07 21:59:47 +00002797 if (advance)
2798 {
2799 if (parmp->window_layout == WIN_TABS)
2800 {
2801 if (curtab->tp_next == NULL) /* just checking */
2802 break;
2803 goto_tabpage(0);
2804 }
2805 else
2806 {
2807 if (curwin->w_next == NULL) /* just checking */
2808 break;
2809 win_enter(curwin->w_next, FALSE);
2810 }
2811 }
2812 advance = TRUE;
2813
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002814 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002815 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002816 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2817 {
2818 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002819 /* Edit file from arg list, if there is one. When "Quit" selected
2820 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002821# ifdef HAS_SWAP_EXISTS_ACTION
2822 swap_exists_did_quit = FALSE;
2823# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002824 (void)do_ecmd(0, arg_idx < GARGCOUNT
2825 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002826 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002827# ifdef HAS_SWAP_EXISTS_ACTION
2828 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002829 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002830 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002831 if (got_int || only_one_window())
2832 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002833 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002834 did_emsg = FALSE; /* avoid hit-enter prompt */
2835 getout(1);
2836 }
2837 win_close(curwin, TRUE);
2838 advance = FALSE;
2839 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002840# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002841 if (arg_idx == GARGCOUNT - 1)
2842 arg_had_last = TRUE;
2843 ++arg_idx;
2844 }
2845 ui_breakcheck();
2846 if (got_int)
2847 {
2848 (void)vgetc(); /* only break the file loading, not the rest */
2849 break;
2850 }
2851 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002852
2853 if (parmp->window_layout == WIN_TABS)
2854 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002855# ifdef FEAT_AUTOCMD
2856 --autocmd_no_enter;
2857# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002858
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002859 /* make the first window the current window */
2860 win = firstwin;
2861#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2862 /* Avoid making a preview window the current window. */
2863 while (win->w_p_pvw)
2864 {
2865 win = win->w_next;
2866 if (win == NULL)
2867 {
2868 win = firstwin;
2869 break;
2870 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002871 }
2872#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002873 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002874
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002875# ifdef FEAT_AUTOCMD
2876 --autocmd_no_leave;
2877# endif
2878 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002879 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002880 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2881}
2882#endif /* FEAT_WINDOWS */
2883
2884/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002885 * Execute the commands from --cmd arguments "cmds[cnt]".
2886 */
2887 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002888exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002889{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002890 char_u **cmds = parmp->pre_commands;
2891 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002892 int i;
2893
2894 if (cnt > 0)
2895 {
2896 curwin->w_cursor.lnum = 0; /* just in case.. */
2897 sourcing_name = (char_u *)_("pre-vimrc command line");
2898# ifdef FEAT_EVAL
2899 current_SID = SID_CMDARG;
2900# endif
2901 for (i = 0; i < cnt; ++i)
2902 do_cmdline_cmd(cmds[i]);
2903 sourcing_name = NULL;
2904# ifdef FEAT_EVAL
2905 current_SID = 0;
2906# endif
2907 TIME_MSG("--cmd commands");
2908 }
2909}
2910
2911/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002912 * Execute "+", "-c" and "-S" arguments.
2913 */
2914 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002915exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002916{
2917 int i;
2918
2919 /*
2920 * We start commands on line 0, make "vim +/pat file" match a
2921 * pattern on line 1. But don't move the cursor when an autocommand
2922 * with g`" was used.
2923 */
2924 msg_scroll = TRUE;
2925 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2926 curwin->w_cursor.lnum = 0;
2927 sourcing_name = (char_u *)"command line";
2928#ifdef FEAT_EVAL
2929 current_SID = SID_CARG;
2930#endif
2931 for (i = 0; i < parmp->n_commands; ++i)
2932 {
2933 do_cmdline_cmd(parmp->commands[i]);
2934 if (parmp->cmds_tofree[i])
2935 vim_free(parmp->commands[i]);
2936 }
2937 sourcing_name = NULL;
2938#ifdef FEAT_EVAL
2939 current_SID = 0;
2940#endif
2941 if (curwin->w_cursor.lnum == 0)
2942 curwin->w_cursor.lnum = 1;
2943
2944 if (!exmode_active)
2945 msg_scroll = FALSE;
2946
2947#ifdef FEAT_QUICKFIX
2948 /* When started with "-q errorfile" jump to first error again. */
2949 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002950 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002951#endif
2952 TIME_MSG("executing command arguments");
2953}
2954
2955/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002956 * Source startup scripts.
2957 */
2958 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002959source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002960{
2961 int i;
2962
2963 /*
2964 * For "evim" source evim.vim first of all, so that the user can overrule
2965 * any things he doesn't like.
2966 */
2967 if (parmp->evim_mode)
2968 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002969 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002970 TIME_MSG("source evim file");
2971 }
2972
2973 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002974 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002975 * nothing else.
2976 */
2977 if (parmp->use_vimrc != NULL)
2978 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002979 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2980 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002981 {
2982#ifdef FEAT_GUI
2983 if (use_gvimrc == NULL) /* don't load gvimrc either */
2984 use_gvimrc = parmp->use_vimrc;
2985#endif
2986 if (parmp->use_vimrc[2] == 'N')
2987 p_lpl = FALSE; /* don't load plugins either */
2988 }
2989 else
2990 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002991 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002992 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2993 }
2994 }
2995 else if (!silent_mode)
2996 {
2997#ifdef AMIGA
2998 struct Process *proc = (struct Process *)FindTask(0L);
2999 APTR save_winptr = proc->pr_WindowPtr;
3000
3001 /* Avoid a requester here for a volume that doesn't exist. */
3002 proc->pr_WindowPtr = (APTR)-1L;
3003#endif
3004
3005 /*
3006 * Get system wide defaults, if the file name is defined.
3007 */
3008#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003009 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003010#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003011#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003012 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003013#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003014
3015 /*
3016 * Try to read initialization commands from the following places:
3017 * - environment variable VIMINIT
3018 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3019 * - second user vimrc file ($VIM/.vimrc for Dos)
3020 * - environment variable EXINIT
3021 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3022 * - second user exrc file ($VIM/.exrc for Dos)
3023 * The first that exists is used, the rest is ignored.
3024 */
3025 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3026 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003027 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003028#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003029 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3030 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003031#endif
3032#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003033 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3034 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003035#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003036#ifdef USR_VIMRC_FILE4
3037 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3038 DOSO_VIMRC) == FAIL
3039#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003040 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003041 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003042 {
3043#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003044 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003045#endif
3046 }
3047 }
3048
3049 /*
3050 * Read initialization commands from ".vimrc" or ".exrc" in current
3051 * directory. This is only done if the 'exrc' option is set.
3052 * Because of security reasons we disallow shell and write commands
3053 * now, except for unix if the file is owned by the user or 'secure'
3054 * option has been reset in environment of global ".exrc" or ".vimrc".
3055 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3056 * SYS_VIMRC_FILE.
3057 */
3058 if (p_exrc)
3059 {
3060#if defined(UNIX) || defined(VMS)
3061 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3062 if (!file_owned(VIMRC_FILE))
3063#endif
3064 secure = p_secure;
3065
3066 i = FAIL;
3067 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3068 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3069#ifdef USR_VIMRC_FILE2
3070 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3071 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3072#endif
3073#ifdef USR_VIMRC_FILE3
3074 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3075 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3076#endif
3077#ifdef SYS_VIMRC_FILE
3078 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3079 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3080#endif
3081 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003082 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003083
3084 if (i == FAIL)
3085 {
3086#if defined(UNIX) || defined(VMS)
3087 /* if ".exrc" is not owned by user set 'secure' mode */
3088 if (!file_owned(EXRC_FILE))
3089 secure = p_secure;
3090 else
3091 secure = 0;
3092#endif
3093 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3094 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3095#ifdef USR_EXRC_FILE2
3096 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3097 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3098#endif
3099 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003100 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003101 }
3102 }
3103 if (secure == 2)
3104 need_wait_return = TRUE;
3105 secure = 0;
3106#ifdef AMIGA
3107 proc->pr_WindowPtr = save_winptr;
3108#endif
3109 }
3110 TIME_MSG("sourcing vimrc file(s)");
3111}
3112
3113/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003114 * Setup to start using the GUI. Exit with an error when not available.
3115 */
3116 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003117main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003118{
3119#ifdef FEAT_GUI
3120 gui.starting = TRUE; /* start GUI a bit later */
3121#else
3122 mch_errmsg(_(e_nogvim));
3123 mch_errmsg("\n");
3124 mch_exit(2);
3125#endif
3126}
3127
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003128#endif /* NO_VIM_MAIN */
3129
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003130/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003131 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003132 * Returns FAIL if the environment variable was not executed, OK otherwise.
3133 */
3134 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003135process_env(
3136 char_u *env,
3137 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003138{
3139 char_u *initstr;
3140 char_u *save_sourcing_name;
3141 linenr_T save_sourcing_lnum;
3142#ifdef FEAT_EVAL
3143 scid_T save_sid;
3144#endif
3145
3146 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3147 {
3148 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003149 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003150 save_sourcing_name = sourcing_name;
3151 save_sourcing_lnum = sourcing_lnum;
3152 sourcing_name = env;
3153 sourcing_lnum = 0;
3154#ifdef FEAT_EVAL
3155 save_sid = current_SID;
3156 current_SID = SID_ENV;
3157#endif
3158 do_cmdline_cmd(initstr);
3159 sourcing_name = save_sourcing_name;
3160 sourcing_lnum = save_sourcing_lnum;
3161#ifdef FEAT_EVAL
3162 current_SID = save_sid;;
3163#endif
3164 return OK;
3165 }
3166 return FAIL;
3167}
3168
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003169#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003170/*
3171 * Return TRUE if we are certain the user owns the file "fname".
3172 * Used for ".vimrc" and ".exrc".
3173 * Use both stat() and lstat() for extra security.
3174 */
3175 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003176file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003177{
3178 struct stat s;
3179# ifdef UNIX
3180 uid_t uid = getuid();
3181# else /* VMS */
3182 uid_t uid = ((getgid() << 16) | getuid());
3183# endif
3184
3185 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3186# ifdef HAVE_LSTAT
3187 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3188# endif
3189 );
3190}
3191#endif
3192
3193/*
3194 * Give an error message main_errors["n"] and exit.
3195 */
3196 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003197mainerr(
3198 int n, /* one of the ME_ defines */
3199 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003200{
3201#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3202 reset_signals(); /* kill us with CTRL-C here, if you like */
3203#endif
3204
3205 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003206 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003207 mch_errmsg(_(main_errors[n]));
3208 if (str != NULL)
3209 {
3210 mch_errmsg(": \"");
3211 mch_errmsg((char *)str);
3212 mch_errmsg("\"");
3213 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003214 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003215
3216 mch_exit(1);
3217}
3218
3219 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003220mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003221{
3222 mainerr(ME_ARG_MISSING, str);
3223}
3224
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003225#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003226/*
3227 * print a message with three spaces prepended and '\n' appended.
3228 */
3229 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003230main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003231{
3232 mch_msg(" ");
3233 mch_msg(s);
3234 mch_msg("\n");
3235}
3236
3237/*
3238 * Print messages for "vim -h" or "vim --help" and exit.
3239 */
3240 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003241usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003242{
3243 int i;
3244 static char *(use[]) =
3245 {
3246 N_("[file ..] edit specified file(s)"),
3247 N_("- read text from stdin"),
3248 N_("-t tag edit file where tag is defined"),
3249#ifdef FEAT_QUICKFIX
3250 N_("-q [errorfile] edit file with first error")
3251#endif
3252 };
3253
3254#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3255 reset_signals(); /* kill us with CTRL-C here, if you like */
3256#endif
3257
3258 mch_msg(longVersion);
3259 mch_msg(_("\n\nusage:"));
3260 for (i = 0; ; ++i)
3261 {
3262 mch_msg(_(" vim [arguments] "));
3263 mch_msg(_(use[i]));
3264 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3265 break;
3266 mch_msg(_("\n or:"));
3267 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003268#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003269 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003270#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003271
3272 mch_msg(_("\n\nArguments:\n"));
3273 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003274#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003275 main_msg(_("--literal\t\tDon't expand wildcards"));
3276#endif
3277#ifdef FEAT_OLE
3278 main_msg(_("-register\t\tRegister this gvim for OLE"));
3279 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3280#endif
3281#ifdef FEAT_GUI
3282 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3283 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3284#endif
3285 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3286 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003287 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003288 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3289#ifdef FEAT_DIFF
3290 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3291#endif
3292 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3293 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3294 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3295 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3296 main_msg(_("-M\t\t\tModifications in text not allowed"));
3297 main_msg(_("-b\t\t\tBinary mode"));
3298#ifdef FEAT_LISP
3299 main_msg(_("-l\t\t\tLisp mode"));
3300#endif
3301 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3302 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003303 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3304#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003305 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003306#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003307 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3308 main_msg(_("-r\t\t\tList swap files and exit"));
3309 main_msg(_("-r (with file name)\tRecover crashed session"));
3310 main_msg(_("-L\t\t\tSame as -r"));
3311#ifdef AMIGA
3312 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3313 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3314#endif
3315#ifdef FEAT_ARABIC
3316 main_msg(_("-A\t\t\tstart in Arabic mode"));
3317#endif
3318#ifdef FEAT_RIGHTLEFT
3319 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3320#endif
3321#ifdef FEAT_FKMAP
3322 main_msg(_("-F\t\t\tStart in Farsi mode"));
3323#endif
3324 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003325 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003326 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3327#ifdef FEAT_GUI
3328 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3329#endif
3330 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003331#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003332 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003333 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3334 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003335#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003336 main_msg(_("+\t\t\tStart at end of file"));
3337 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003338 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003339 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3340 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3341 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3342 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3343 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3344#ifdef FEAT_CRYPT
3345 main_msg(_("-x\t\t\tEdit encrypted files"));
3346#endif
3347#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3348# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3349 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3350# endif
3351 main_msg(_("-X\t\t\tDo not connect to X server"));
3352#endif
3353#ifdef FEAT_CLIENTSERVER
3354 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3355 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3356 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3357 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003358# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003359 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003360# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003361 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3362 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3363 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3364 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3365#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003366#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003367 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003368#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003369#ifdef FEAT_VIMINFO
3370 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3371#endif
3372 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3373 main_msg(_("--version\t\tPrint version information and exit"));
3374
3375#ifdef FEAT_GUI_X11
3376# ifdef FEAT_GUI_MOTIF
3377 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3378# else
3379# ifdef FEAT_GUI_ATHENA
3380# ifdef FEAT_GUI_NEXTAW
3381 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3382# else
3383 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3384# endif
3385# endif
3386# endif
3387 main_msg(_("-display <display>\tRun vim on <display>"));
3388 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003389 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3390 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3391 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3392 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3393 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3394 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3395 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3396 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3397# ifdef FEAT_GUI_ATHENA
3398 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3399# endif
3400 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3401 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3402 main_msg(_("-xrm <resource>\tSet the specified resource"));
3403#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003404#ifdef FEAT_GUI_GTK
3405 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3406 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3407 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3408 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3409 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003410 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003411 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003412 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003413#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003414#ifdef FEAT_GUI_W32
3415 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003416 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003417#endif
3418
3419#ifdef FEAT_GUI_GNOME
3420 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3421 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003422 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003423 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003424 gui.dofork = FALSE;
3425 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003426 else
3427#endif
3428 mch_exit(0);
3429}
3430
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003431#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003432/*
3433 * Check the result of the ATTENTION dialog:
3434 * When "Quit" selected, exit Vim.
3435 * When "Recover" selected, recover the file.
3436 */
3437 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003438check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003439{
3440 if (swap_exists_action == SEA_QUIT)
3441 getout(1);
3442 handle_swap_exists(NULL);
3443}
3444#endif
3445
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003446#endif
3447
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003448#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003449static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003450
3451static struct timeval prev_timeval;
3452
Bram Moolenaar3f269672009-11-03 11:11:11 +00003453# ifdef WIN3264
3454/*
3455 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3456 */
3457 static int
3458gettimeofday(struct timeval *tv, char *dummy)
3459{
3460 long t = clock();
3461 tv->tv_sec = t / CLOCKS_PER_SEC;
3462 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3463 return 0;
3464}
3465# endif
3466
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003467/*
3468 * Save the previous time before doing something that could nest.
3469 * set "*tv_rel" to the time elapsed so far.
3470 */
3471 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003472time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003473{
3474 *((struct timeval *)tv_rel) = prev_timeval;
3475 gettimeofday(&prev_timeval, NULL);
3476 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3477 - ((struct timeval *)tv_rel)->tv_usec;
3478 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3479 - ((struct timeval *)tv_rel)->tv_sec;
3480 if (((struct timeval *)tv_rel)->tv_usec < 0)
3481 {
3482 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3483 --((struct timeval *)tv_rel)->tv_sec;
3484 }
3485 *(struct timeval *)tv_start = prev_timeval;
3486}
3487
3488/*
3489 * Compute the previous time after doing something that could nest.
3490 * Subtract "*tp" from prev_timeval;
3491 * Note: The arguments are (void *) to avoid trouble with systems that don't
3492 * have struct timeval.
3493 */
3494 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003495time_pop(
3496 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003497{
3498 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3499 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3500 if (prev_timeval.tv_usec < 0)
3501 {
3502 prev_timeval.tv_usec += 1000000;
3503 --prev_timeval.tv_sec;
3504 }
3505}
3506
3507 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003508time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003509{
3510 long usec;
3511 long msec;
3512
3513 usec = now->tv_usec - then->tv_usec;
3514 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3515 usec = usec % 1000L;
3516 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3517}
3518
3519 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003520time_msg(
3521 char *mesg,
3522 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003523 (struct timeval *) */
3524{
3525 static struct timeval start;
3526 struct timeval now;
3527
3528 if (time_fd != NULL)
3529 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003530 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003531 {
3532 gettimeofday(&start, NULL);
3533 prev_timeval = start;
3534 fprintf(time_fd, "\n\ntimes in msec\n");
3535 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3536 fprintf(time_fd, " clock elapsed: other lines\n\n");
3537 }
3538 gettimeofday(&now, NULL);
3539 time_diff(&start, &now);
3540 if (((struct timeval *)tv_start) != NULL)
3541 {
3542 fprintf(time_fd, " ");
3543 time_diff(((struct timeval *)tv_start), &now);
3544 }
3545 fprintf(time_fd, " ");
3546 time_diff(&prev_timeval, &now);
3547 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003548 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003549 }
3550}
3551
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003552#endif
3553
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003554#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003555
3556/*
3557 * Common code for the X command server and the Win32 command server.
3558 */
3559
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003560static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003561
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003562/*
3563 * Do the client-server stuff, unless "--servername ''" was used.
3564 */
3565 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003566exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003567{
3568 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3569 {
3570# ifdef WIN32
3571 /* Initialise the client/server messaging infrastructure. */
3572 serverInitMessaging();
3573# endif
3574
3575 /*
3576 * When a command server argument was found, execute it. This may
3577 * exit Vim when it was successful. Otherwise it's executed further
3578 * on. Remember the encoding used here in "serverStrEnc".
3579 */
3580 if (parmp->serverArg)
3581 {
3582 cmdsrv_main(&parmp->argc, parmp->argv,
3583 parmp->serverName_arg, &parmp->serverStr);
3584# ifdef FEAT_MBYTE
3585 parmp->serverStrEnc = vim_strsave(p_enc);
3586# endif
3587 }
3588
3589 /* If we're still running, get the name to register ourselves.
3590 * On Win32 can register right now, for X11 need to setup the
3591 * clipboard first, it's further down. */
3592 parmp->servername = serverMakeName(parmp->serverName_arg,
3593 parmp->argv[0]);
3594# ifdef WIN32
3595 if (parmp->servername != NULL)
3596 {
3597 serverSetName(parmp->servername);
3598 vim_free(parmp->servername);
3599 }
3600# endif
3601 }
3602}
3603
3604/*
3605 * Prepare for running as a Vim server.
3606 */
3607 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003608prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003609{
3610# if defined(FEAT_X11)
3611 /*
3612 * Register for remote command execution with :serversend and --remote
3613 * unless there was a -X or a --servername '' on the command line.
3614 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003615 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003616 */
3617 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3618# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003619 (gui.in_use
3620# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003621 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003622# endif
3623 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003624# endif
3625 parmp->serverName_arg != NULL))
3626 {
3627 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3628 vim_free(parmp->servername);
3629 TIME_MSG("register server name");
3630 }
3631 else
3632 serverDelayedStartName = parmp->servername;
3633# endif
3634
3635 /*
3636 * Execute command ourselves if we're here because the send failed (or
3637 * else we would have exited above).
3638 */
3639 if (parmp->serverStr != NULL)
3640 {
3641 char_u *p;
3642
3643 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3644 parmp->serverStr, &p));
3645 vim_free(p);
3646 }
3647}
3648
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003649 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003650cmdsrv_main(
3651 int *argc,
3652 char **argv,
3653 char_u *serverName_arg,
3654 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003655{
3656 char_u *res;
3657 int i;
3658 char_u *sname;
3659 int ret;
3660 int didone = FALSE;
3661 int exiterr = 0;
3662 char **newArgV = argv + 1;
3663 int newArgC = 1,
3664 Argc = *argc;
3665 int argtype;
3666#define ARGTYPE_OTHER 0
3667#define ARGTYPE_EDIT 1
3668#define ARGTYPE_EDIT_WAIT 2
3669#define ARGTYPE_SEND 3
3670 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003671 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003672# ifndef FEAT_X11
3673 HWND srv;
3674# else
3675 Window srv;
3676
3677 setup_term_clip();
3678# endif
3679
3680 sname = serverMakeName(serverName_arg, argv[0]);
3681 if (sname == NULL)
3682 return;
3683
3684 /*
3685 * Execute the command server related arguments and remove them
3686 * from the argc/argv array; We may have to return into main()
3687 */
3688 for (i = 1; i < Argc; i++)
3689 {
3690 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003691 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003692 {
3693 for (; i < *argc; i++)
3694 {
3695 *newArgV++ = argv[i];
3696 newArgC++;
3697 }
3698 break;
3699 }
3700
Bram Moolenaareb94e552006-03-11 21:35:11 +00003701 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003702 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003703 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3704 {
3705 char *p = argv[i] + 8;
3706
3707 argtype = ARGTYPE_EDIT;
3708 while (*p != NUL)
3709 {
3710 if (STRNICMP(p, "-wait", 5) == 0)
3711 {
3712 argtype = ARGTYPE_EDIT_WAIT;
3713 p += 5;
3714 }
3715 else if (STRNICMP(p, "-silent", 7) == 0)
3716 {
3717 silent = TRUE;
3718 p += 7;
3719 }
3720 else if (STRNICMP(p, "-tab", 4) == 0)
3721 {
3722 tabs = TRUE;
3723 p += 4;
3724 }
3725 else
3726 {
3727 argtype = ARGTYPE_OTHER;
3728 break;
3729 }
3730 }
3731 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003732 else
3733 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003734
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003735 if (argtype != ARGTYPE_OTHER)
3736 {
3737 if (i == *argc - 1)
3738 mainerr_arg_missing((char_u *)argv[i]);
3739 if (argtype == ARGTYPE_SEND)
3740 {
3741 *serverStr = (char_u *)argv[i + 1];
3742 i++;
3743 }
3744 else
3745 {
3746 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003747 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003748 if (*serverStr == NULL)
3749 {
3750 /* Probably out of memory, exit. */
3751 didone = TRUE;
3752 exiterr = 1;
3753 break;
3754 }
3755 Argc = i;
3756 }
3757# ifdef FEAT_X11
3758 if (xterm_dpy == NULL)
3759 {
3760 mch_errmsg(_("No display"));
3761 ret = -1;
3762 }
3763 else
3764 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3765 NULL, &srv, 0, 0, silent);
3766# else
3767 /* Win32 always works? */
3768 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3769# endif
3770 if (ret < 0)
3771 {
3772 if (argtype == ARGTYPE_SEND)
3773 {
3774 /* Failed to send, abort. */
3775 mch_errmsg(_(": Send failed.\n"));
3776 didone = TRUE;
3777 exiterr = 1;
3778 }
3779 else if (!silent)
3780 /* Let vim start normally. */
3781 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3782 break;
3783 }
3784
3785# ifdef FEAT_GUI_W32
3786 /* Guess that when the server name starts with "g" it's a GUI
3787 * server, which we can bring to the foreground here.
3788 * Foreground() in the server doesn't work very well. */
3789 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3790 SetForegroundWindow(srv);
3791# endif
3792
3793 /*
3794 * For --remote-wait: Wait until the server did edit each
3795 * file. Also detect that the server no longer runs.
3796 */
3797 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3798 {
3799 int numFiles = *argc - i - 1;
3800 int j;
3801 char_u *done = alloc(numFiles);
3802 char_u *p;
3803# ifdef FEAT_GUI_W32
3804 NOTIFYICONDATA ni;
3805 int count = 0;
3806 extern HWND message_window;
3807# endif
3808
3809 if (numFiles > 0 && argv[i + 1][0] == '+')
3810 /* Skip "+cmd" argument, don't wait for it to be edited. */
3811 --numFiles;
3812
3813# ifdef FEAT_GUI_W32
3814 ni.cbSize = sizeof(ni);
3815 ni.hWnd = message_window;
3816 ni.uID = 0;
3817 ni.uFlags = NIF_ICON|NIF_TIP;
3818 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3819 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3820 Shell_NotifyIcon(NIM_ADD, &ni);
3821# endif
3822
3823 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003824 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003825 while (memchr(done, 0, numFiles) != NULL)
3826 {
3827# ifdef WIN32
3828 p = serverGetReply(srv, NULL, TRUE, TRUE);
3829 if (p == NULL)
3830 break;
3831# else
3832 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3833 break;
3834# endif
3835 j = atoi((char *)p);
3836 if (j >= 0 && j < numFiles)
3837 {
3838# ifdef FEAT_GUI_W32
3839 ++count;
3840 sprintf(ni.szTip, _("%d of %d edited"),
3841 count, numFiles);
3842 Shell_NotifyIcon(NIM_MODIFY, &ni);
3843# endif
3844 done[j] = 1;
3845 }
3846 }
3847# ifdef FEAT_GUI_W32
3848 Shell_NotifyIcon(NIM_DELETE, &ni);
3849# endif
3850 }
3851 }
3852 else if (STRICMP(argv[i], "--remote-expr") == 0)
3853 {
3854 if (i == *argc - 1)
3855 mainerr_arg_missing((char_u *)argv[i]);
3856# ifdef WIN32
3857 /* Win32 always works? */
3858 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3859 &res, NULL, 1, FALSE) < 0)
3860# else
3861 if (xterm_dpy == NULL)
3862 mch_errmsg(_("No display: Send expression failed.\n"));
3863 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3864 &res, NULL, 1, 1, FALSE) < 0)
3865# endif
3866 {
3867 if (res != NULL && *res != NUL)
3868 {
3869 /* Output error from remote */
3870 mch_errmsg((char *)res);
3871 vim_free(res);
3872 res = NULL;
3873 }
3874 mch_errmsg(_(": Send expression failed.\n"));
3875 }
3876 }
3877 else if (STRICMP(argv[i], "--serverlist") == 0)
3878 {
3879# ifdef WIN32
3880 /* Win32 always works? */
3881 res = serverGetVimNames();
3882# else
3883 if (xterm_dpy != NULL)
3884 res = serverGetVimNames(xterm_dpy);
3885# endif
3886 if (called_emsg)
3887 mch_errmsg("\n");
3888 }
3889 else if (STRICMP(argv[i], "--servername") == 0)
3890 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003891 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003892 i++;
3893 continue;
3894 }
3895 else
3896 {
3897 *newArgV++ = argv[i];
3898 newArgC++;
3899 continue;
3900 }
3901 didone = TRUE;
3902 if (res != NULL && *res != NUL)
3903 {
3904 mch_msg((char *)res);
3905 if (res[STRLEN(res) - 1] != '\n')
3906 mch_msg("\n");
3907 }
3908 vim_free(res);
3909 }
3910
3911 if (didone)
3912 {
3913 display_errors(); /* display any collected messages */
3914 exit(exiterr); /* Mission accomplished - get out */
3915 }
3916
3917 /* Return back into main() */
3918 *argc = newArgC;
3919 vim_free(sname);
3920}
3921
3922/*
3923 * Build a ":drop" command to send to a Vim server.
3924 */
3925 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003926build_drop_cmd(
3927 int filec,
3928 char **filev,
3929 int tabs, /* Use ":tab drop" instead of ":drop". */
3930 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003931{
3932 garray_T ga;
3933 int i;
3934 char_u *inicmd = NULL;
3935 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003936 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003937 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003938
3939 if (filec > 0 && filev[0][0] == '+')
3940 {
3941 inicmd = (char_u *)filev[0] + 1;
3942 filev++;
3943 filec--;
3944 }
3945 /* Check if we have at least one argument. */
3946 if (filec <= 0)
3947 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003948
3949 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003950 cwd = alloc(MAXPATHL);
3951 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003952 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003953 if (mch_dirname(cwd, MAXPATHL) != OK)
3954 {
3955 vim_free(cwd);
3956 return NULL;
3957 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003958 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003959#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003960 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00003961#else
3962 PATH_ESC_CHARS,
3963#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003964 '\\', TRUE);
3965 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003966 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003967 return NULL;
3968 ga_init2(&ga, 1, 100);
3969 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003970 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003971
3972 /* Call inputsave() so that a prompt for an encryption key works. */
3973 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3974 if (tabs)
3975 ga_concat(&ga, (char_u *)"tab ");
3976 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003977 for (i = 0; i < filec; i++)
3978 {
3979 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003980 * do it again in the Vim server. On MS-Windows only escape
3981 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003982 p = vim_strsave_escaped((char_u *)filev[i],
3983#ifdef UNIX
3984 PATH_ESC_CHARS
3985#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003986 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003987#endif
3988 );
3989 if (p == NULL)
3990 {
3991 vim_free(ga.ga_data);
3992 return NULL;
3993 }
3994 ga_concat(&ga, (char_u *)" ");
3995 ga_concat(&ga, p);
3996 vim_free(p);
3997 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003998 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3999
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004000 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4001 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004002 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4003
4004 /* Switch back to the correct current directory (prior to temporary path
4005 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004006 * correct after the :drop command. With line breaks and spaces:
4007 * if !exists('+acd') || !&acd
4008 * if haslocaldir()
4009 * cd -
4010 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004011 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004012 * cd -
4013 * endif
4014 * endif
4015 */
4016 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004017 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004018 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004019 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004020 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004021
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004022 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004023 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4024 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004025 if (inicmd != NULL)
4026 {
4027 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4028 * the following commands to be inserted as text. Use a "|",
4029 * hopefully "inicmd" does allow this... */
4030 ga_concat(&ga, inicmd);
4031 ga_concat(&ga, (char_u *)"|");
4032 }
4033 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4034 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004035 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004036 ga_append(&ga, NUL);
4037 return ga.ga_data;
4038}
4039
4040/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004041 * Make our basic server name: use the specified "arg" if given, otherwise use
4042 * the tail of the command "cmd" we were started with.
4043 * Return the name in allocated memory. This doesn't include a serial number.
4044 */
4045 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004046serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004047{
4048 char_u *p;
4049
4050 if (arg != NULL && *arg != NUL)
4051 p = vim_strsave_up(arg);
4052 else
4053 {
4054 p = vim_strsave_up(gettail((char_u *)cmd));
4055 /* Remove .exe or .bat from the name. */
4056 if (p != NULL && vim_strchr(p, '.') != NULL)
4057 *vim_strchr(p, '.') = NUL;
4058 }
4059 return p;
4060}
4061#endif /* FEAT_CLIENTSERVER */
4062
4063#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4064/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004065 * Replace termcodes such as <CR> and insert as key presses if there is room.
4066 */
4067 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004068server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004069{
4070 char_u *ptr = NULL;
4071 char_u *cpo_save = p_cpo;
4072
4073 /* Set 'cpoptions' the way we want it.
4074 * B set - backslashes are *not* treated specially
4075 * k set - keycodes are *not* reverse-engineered
4076 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004077 * The last but one parameter of replace_termcodes() is TRUE so that the
4078 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004079 */
4080 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004081 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004082 p_cpo = cpo_save;
4083
4084 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4085 {
4086 /*
4087 * Add the string to the input stream.
4088 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4089 *
4090 * First clear typed characters from the typeahead buffer, there could
4091 * be half a mapping there. Then append to the existing string, so
4092 * that multiple commands from a client are concatenated.
4093 */
4094 if (typebuf.tb_maplen < typebuf.tb_len)
4095 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4096 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4097
4098 /* Let input_available() know we inserted text in the typeahead
4099 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004100 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004101 }
4102 vim_free((char_u *)ptr);
4103}
4104
4105/*
4106 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004107 */
4108 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004109eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004110{
4111 char_u *res;
4112 int save_dbl = debug_break_level;
4113 int save_ro = redir_off;
4114
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004115 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4116 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004117 debug_break_level = -1;
4118 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004119 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4120 * to be typed. Do generate errors so that try/catch works. */
4121 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004122
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004123 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004124
4125 debug_break_level = save_dbl;
4126 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004127 --emsg_silent;
4128 if (emsg_silent < 0)
4129 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004130
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004131 /* A client can tell us to redraw, but not to display the cursor, so do
4132 * that here. */
4133 setcursor();
4134 out_flush();
4135#ifdef FEAT_GUI
4136 if (gui.in_use)
4137 gui_update_cursor(FALSE, FALSE);
4138#endif
4139
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004140 return res;
4141}
4142
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004143/*
4144 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4145 * return an allocated string. Otherwise return "data".
4146 * "*tofree" is set to the result when it needs to be freed later.
4147 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004148 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004149serverConvert(
4150 char_u *client_enc UNUSED,
4151 char_u *data,
4152 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004153{
4154 char_u *res = data;
4155
4156 *tofree = NULL;
4157# ifdef FEAT_MBYTE
4158 if (client_enc != NULL && p_enc != NULL)
4159 {
4160 vimconv_T vimconv;
4161
4162 vimconv.vc_type = CONV_NONE;
4163 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4164 && vimconv.vc_type != CONV_NONE)
4165 {
4166 res = string_convert(&vimconv, data, NULL);
4167 if (res == NULL)
4168 res = data;
4169 else
4170 *tofree = res;
4171 }
4172 convert_setup(&vimconv, NULL, NULL);
4173 }
4174# endif
4175 return res;
4176}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004177#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004178
4179/*
4180 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4181 */
4182#if defined(FEAT_FKMAP) || defined(PROTO)
4183# include "farsi.c"
4184#endif
4185
4186/*
4187 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4188 */
4189#if defined(FEAT_ARABIC) || defined(PROTO)
4190# include "arabic.c"
4191#endif