blob: 076898c34561092705c45340b77344477d62beab [file] [log] [blame]
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
Bram Moolenaarb4210b32004-06-13 14:51:16 +000010#define EXTERN
11#include "vim.h"
12
Bram Moolenaarb4210b32004-06-13 14:51:16 +000013#ifdef __CYGWIN__
14# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000015# include <cygwin/version.h>
16# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
17 * cygwin_conv_path() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000018# endif
19# include <limits.h>
20#endif
21
Bram Moolenaarc013cb62005-07-24 21:18:31 +000022/* Maximum number of commands from + or -c arguments. */
23#define MAX_ARG_CMDS 10
24
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000025/* values for "window_layout" */
26#define WIN_HOR 1 /* "-o" horizontally split windows */
27#define WIN_VER 2 /* "-O" vertically split windows */
28#define WIN_TABS 3 /* "-p" windows on tab pages */
29
Bram Moolenaar58d98232005-07-23 22:25:46 +000030/* Struct for various parameters passed between main() and other functions. */
31typedef struct
32{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000033 int argc;
34 char **argv;
35
36 int evim_mode; /* started as "evim" */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000037 char_u *use_vimrc; /* vimrc from -u argument */
38
39 int n_commands; /* no. of commands from + or -c */
40 char_u *commands[MAX_ARG_CMDS]; /* commands from + or -c arg. */
41 char_u cmds_tofree[MAX_ARG_CMDS]; /* commands that need free() */
42 int n_pre_commands; /* no. of commands from --cmd */
43 char_u *pre_commands[MAX_ARG_CMDS]; /* commands from --cmd argument */
44
45 int edit_type; /* type of editing to do */
46 char_u *tagname; /* tag from -t argument */
47#ifdef FEAT_QUICKFIX
48 char_u *use_ef; /* 'errorfile' from -q argument */
49#endif
50
51 int want_full_screen;
52 int stdout_isatty; /* is stdout a terminal? */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +010053 int not_a_term; /* no warning for missing term? */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000054 char_u *term; /* specified terminal name */
55#ifdef FEAT_CRYPT
56 int ask_for_key; /* -x argument */
57#endif
58 int no_swap_file; /* "-n" argument used */
59#ifdef FEAT_EVAL
60 int use_debug_break_level;
61#endif
62#ifdef FEAT_WINDOWS
63 int window_count; /* number of windows to use */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000064 int window_layout; /* 0, WIN_HOR, WIN_VER or WIN_TABS */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000065#endif
66
67#ifdef FEAT_CLIENTSERVER
Bram Moolenaar58d98232005-07-23 22:25:46 +000068 int serverArg; /* TRUE when argument for a server */
69 char_u *serverName_arg; /* cmdline arg for server name */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000070 char_u *serverStr; /* remote server command */
71 char_u *serverStrEnc; /* encoding of serverStr */
72 char_u *servername; /* allocated name for our server */
73#endif
Bram Moolenaar53076832015-12-31 19:53:21 +010074#if !defined(UNIX) && !defined(__EMX__)
75# define EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +000076 int literal; /* don't expand file names */
77#endif
78#ifdef MSWIN
79 int full_path; /* file name argument was full path */
80#endif
81#ifdef FEAT_DIFF
82 int diff_mode; /* start with 'diff' set */
83#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +000084} mparm_T;
85
Bram Moolenaarc013cb62005-07-24 21:18:31 +000086/* Values for edit_type. */
87#define EDIT_NONE 0 /* no edit type yet */
88#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
89#define EDIT_STDIN 2 /* read file from stdin */
90#define EDIT_TAG 3 /* tag name argument given, use tagname */
91#define EDIT_QF 4 /* start in quickfix mode */
92
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010093#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010094static int file_owned(char *fname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +000095#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010096static void mainerr(int, char_u *);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010097#ifndef NO_VIM_MAIN
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010098static void main_msg(char *s);
99static void usage(void);
100static int get_number_arg(char_u *p, int *idx, int def);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100101# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100102static void init_locale(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100103# endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100104static void parse_command_name(mparm_T *parmp);
105static void early_arg_scan(mparm_T *parmp);
106static void command_line_scan(mparm_T *parmp);
107static void check_tty(mparm_T *parmp);
108static void read_stdin(void);
109static void create_windows(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100110# ifdef FEAT_WINDOWS
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100111static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100112# endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100113static void exe_pre_commands(mparm_T *parmp);
114static void exe_commands(mparm_T *parmp);
115static void source_startup_scripts(mparm_T *parmp);
116static void main_start_gui(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100117# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100118static void check_swap_exists_action(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100119# endif
120# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100121static void exec_on_server(mparm_T *parmp);
122static void prepare_server(mparm_T *parmp);
123static void cmdsrv_main(int *argc, char **argv, char_u *serverName_arg, char_u **serverStr);
124static char_u *serverMakeName(char_u *arg, char *cmd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100125# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000126#endif
127
128
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000129/*
130 * Different types of error messages.
131 */
132static char *(main_errors[]) =
133{
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000134 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000135#define ME_UNKNOWN_OPTION 0
136 N_("Too many edit arguments"),
137#define ME_TOO_MANY_ARGS 1
138 N_("Argument missing after"),
139#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000140 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000141#define ME_GARBAGE 3
142 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
143#define ME_EXTRA_CMD 4
144 N_("Invalid argument for"),
145#define ME_INVALID_ARG 5
146};
147
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100148#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100149#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +0200150
151static char_u *start_dir = NULL; /* current working dir on startup */
152
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000153 int
154# ifdef VIMDLL
155_export
156# endif
157# ifdef FEAT_GUI_MSWIN
158# ifdef __BORLANDC__
159_cdecl
160# endif
161VimMain
162# else
163main
164# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100165(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000166{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000167 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000168 mparm_T params; /* various parameters passed between
169 * main() and other functions. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000170#ifdef STARTUPTIME
171 int i;
172#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000173
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000174 /*
175 * Do any system-specific initialisations. These can NOT use IObuff or
176 * NameBuff. Thus emsg2() cannot be called!
177 */
178 mch_early_init();
179
Bram Moolenaar14993322014-09-09 12:25:33 +0200180#if defined(WIN32) && defined(FEAT_MBYTE)
181 /*
182 * MingW expands command line arguments, which confuses our code to
183 * convert when 'encoding' changes. Get the unexpanded arguments.
184 */
185 argc = get_cmd_argsW(&argv);
186#endif
187
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000188 /* Many variables are in "params" so that we can pass them to invoked
189 * functions without a lot of arguments. "argc" and "argv" are also
190 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000191 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000192 params.argc = argc;
193 params.argv = argv;
194 params.want_full_screen = TRUE;
195#ifdef FEAT_EVAL
196 params.use_debug_break_level = -1;
197#endif
198#ifdef FEAT_WINDOWS
199 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000200#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000201
Bram Moolenaar99685e62013-05-11 13:56:18 +0200202#ifdef FEAT_RUBY
203 {
204 int ruby_stack_start;
205 vim_ruby_init((void *)&ruby_stack_start);
206 }
207#endif
208
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000209#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000210 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000211#endif
212
213#ifdef MEM_PROFILE
214 atexit(vim_mem_profile_dump);
215#endif
216
217#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000218 for (i = 1; i < argc; ++i)
219 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000220 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000221 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000222 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000223 TIME_MSG("--- VIM STARTING ---");
224 break;
225 }
226 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000227#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000228 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000229
230#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000231 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000232#endif
233
234#ifdef FEAT_MBYTE
235 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
236#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000237#ifdef FEAT_EVAL
238 eval_init(); /* init global variables */
239#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000240
241#ifdef __QNXNTO__
242 qnx_init(); /* PhAttach() for clipboard, (and gui) */
243#endif
244
245#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000246 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000247 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000248 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000249 TIME_MSG("GUI prepared");
250#endif
251
252 /* Init the table of Normal mode commands. */
253 init_normal_cmds();
254
255#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000256 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000257#endif
258
259 /*
260 * Allocate space for the generic buffers (needed for set_init_1() and
261 * EMSG2()).
262 */
263 if ((IObuff = alloc(IOSIZE)) == NULL
264 || (NameBuff = alloc(MAXPATHL)) == NULL)
265 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000266 TIME_MSG("Allocated generic buffers");
267
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000268#ifdef NBDEBUG
269 /* Wait a moment for debugging NetBeans. Must be after allocating
270 * NameBuff. */
271 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
272 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000273 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000274#endif
275
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000276#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
277 /*
278 * Setup to use the current locale (for ctype() and many other things).
279 * NOTE: Translated messages with encodings other than latin1 will not
280 * work until set_init_1() has been called!
281 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000282 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000283 TIME_MSG("locale set");
284#endif
285
286#ifdef FEAT_GUI
287 gui.dofork = TRUE; /* default is to use fork() */
288#endif
289
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000290 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000291 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000292 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000293 * --server...
294 * --socketid
Bram Moolenaar78e17622007-08-30 10:26:19 +0000295 * --windowid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000296 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000297 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000298
299#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000300 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000301#endif
302#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000303 /* Prepare for possibly starting GUI sometime */
304 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000305 TIME_MSG("GUI prepared");
306#endif
307
308#ifdef FEAT_CLIPBOARD
309 clip_init(FALSE); /* Initialise clipboard stuff */
310 TIME_MSG("clipboard setup");
311#endif
312
313 /*
314 * Check if we have an interactive window.
315 * On the Amiga: If there is no window, we open one with a newcli command
316 * (needed for :! to * work). mch_check_win() will also handle the -d or
317 * -dev argument.
318 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000319 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000320 TIME_MSG("window checked");
321
322 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000323 * Allocate the first window and buffer.
324 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000325 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000326 if (win_alloc_first() == FAIL)
327 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000328
329 init_yank(); /* init yank buffers */
330
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000331 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200332 global_alist.id = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000333
334 /*
335 * Set the default values for the options.
336 * NOTE: Non-latin1 translated messages are working only after this,
337 * because this is where "has_mbyte" will be set, which is used by
338 * msg_outtrans_len_attr().
339 * First find out the home directory, needed to expand "~" in options.
340 */
341 init_homedir(); /* find real value of $HOME */
342 set_init_1();
343 TIME_MSG("inits 1");
344
345#ifdef FEAT_EVAL
346 set_lang_var(); /* set v:lang and v:ctype */
347#endif
348
349#ifdef FEAT_CLIENTSERVER
350 /*
351 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000352 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000353 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000354 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000355#endif
356
357 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000358 * Figure out the way to work from the command name argv[0].
359 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000360 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000361 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000362
363 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000364 * Process the command line arguments. File names are put in the global
365 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000366 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000367 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000368 TIME_MSG("parsing arguments");
369
370 /*
371 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000372 * there is no terminal version, and on Windows we can't fork one off with
373 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000374 */
375#ifdef ALWAYS_USE_GUI
376 gui.starting = TRUE;
377#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000378# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000379 /*
380 * Check if the GUI can be started. Reset gui.starting if not.
381 * Don't know about other systems, stay on the safe side and don't check.
382 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000383 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000384 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000385 if (gui_init_check() == FAIL)
386 {
387 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000388
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000389 /* When running "evim" or "gvim -y" we need the menus, exit if we
390 * don't have them. */
391 if (params.evim_mode)
392 mch_exit(1);
393 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000394 }
395# endif
396#endif
397
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000398 if (GARGCOUNT > 0)
399 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100400#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000401 /*
402 * Expand wildcards in file names.
403 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000404 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000405 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200406 start_dir = alloc(MAXPATHL);
407 if (start_dir != NULL)
408 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000409 /* Temporarily add '(' and ')' to 'isfname'. These are valid
410 * filename characters but are excluded from 'isfname' to make
411 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
412 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000413 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000414 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200415 if (start_dir != NULL)
416 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000417 }
418#endif
419 fname = alist_name(&GARGLIST[0]);
420 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000421
422#if defined(WIN32) && defined(FEAT_MBYTE)
423 {
424 extern void set_alist_count(void);
425
426 /* Remember the number of entries in the argument list. If it changes
427 * we don't react on setting 'encoding'. */
428 set_alist_count();
429 }
430#endif
431
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000432#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000433 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000434 {
435 /*
436 * If there is one filename, fully qualified, we have very probably
437 * been invoked from explorer, so change to the file's directory.
438 * Hint: to avoid this when typing a command use a forward slash.
439 * If the cd fails, it doesn't matter.
440 */
441 (void)vim_chdirfile(fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200442 if (start_dir != NULL)
443 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000444 }
445#endif
446 TIME_MSG("expanding arguments");
447
448#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000449 if (params.diff_mode && params.window_count == -1)
450 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000451#endif
452
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000453 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000454 ++RedrawingDisabled;
455
456 /*
457 * When listing swap file names, don't do cursor positioning et. al.
458 */
459 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000460 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000461
462 /*
463 * When certain to start the GUI, don't check capabilities of terminal.
464 * For GTK we can't be sure, but when started from the desktop it doesn't
465 * make sense to try using a terminal.
466 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000467#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000468 if (gui.starting
469# ifdef FEAT_GUI_GTK
470 && !isatty(2)
471# endif
472 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000473 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000474#endif
475
476#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
477 /* When the GUI is started from Finder, need to display messages in a
478 * message box. isatty(2) returns TRUE anyway, thus we need to check the
479 * name to know we're not started from a terminal. */
480 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000481 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000482 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000483
484 /* Avoid always using "/" as the current directory. Note that when
485 * started from Finder the arglist will be filled later in
486 * HandleODocAE() and "fname" will be NULL. */
487 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
488 && STRCMP(NameBuff, "/") == 0)
489 {
490 if (fname != NULL)
491 (void)vim_chdirfile(fname);
492 else
493 {
494 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
495 vim_chdir(NameBuff);
496 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200497 if (start_dir != NULL)
498 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000499 }
500 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000501#endif
502
503 /*
504 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100505 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000506 * Note that we may use mch_exit() before mch_init()!
507 */
508 mch_init();
509 TIME_MSG("shell init");
510
511#ifdef USE_XSMP
512 /*
513 * For want of anywhere else to do it, try to connect to xsmp here.
514 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
515 * Hijacking -X 'no X connection' to also disable XSMP connection as that
516 * has a similar delay upon failure.
517 * Only try if SESSION_MANAGER is set to something non-null.
518 */
519 if (!x_no_connect)
520 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000521 char *p = getenv("SESSION_MANAGER");
522
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000523 if (p != NULL && *p != NUL)
524 {
525 xsmp_init();
526 TIME_MSG("xsmp init");
527 }
528 }
529#endif
530
531 /*
532 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000533 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000534 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000535
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000536 /* This message comes before term inits, but after setting "silent_mode"
537 * when the input is not a tty. */
538 if (GARGCOUNT > 1 && !silent_mode)
539 printf(_("%d files to edit\n"), GARGCOUNT);
540
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000541 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000542 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000543 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000544 capabilities (will set full_screen) */
545 screen_start(); /* don't know where cursor is now */
546 TIME_MSG("Termcap init");
547 }
548
549 /*
550 * Set the default values for the options that use Rows and Columns.
551 */
552 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000553 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000554#ifdef FEAT_DIFF
555 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
556 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000557 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000558 diff_win_options(firstwin, FALSE);
559#endif
560
561 cmdline_row = Rows - p_ch;
562 msg_row = cmdline_row;
563 screenalloc(FALSE); /* allocate screen buffers */
564 set_init_2();
565 TIME_MSG("inits 2");
566
567 msg_scroll = TRUE;
568 no_wait_return = TRUE;
569
570 init_mappings(); /* set up initial mappings */
571
572 init_highlight(TRUE, FALSE); /* set the default highlight groups */
573 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000574
575#ifdef FEAT_EVAL
576 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000577 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000578#endif
579
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100580#ifdef FEAT_MZSCHEME
581 /*
582 * Newer version of MzScheme (Racket) require earlier (trampolined)
583 * initialisation via scheme_main_setup.
584 * Implement this by initialising it as early as possible
585 * and splitting off remaining Vim main into vim_main2
586 */
587 {
588 /* Pack up preprocessed command line arguments.
589 * It is safe because Scheme does not access argc/argv. */
590 char *args[2];
591 args[0] = (char *)fname;
592 args[1] = (char *)&params;
593 return mzscheme_main(2, args);
594 }
595}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100596#endif
597#endif /* NO_VIM_MAIN */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100598
Bram Moolenaar8866d272012-11-28 15:55:42 +0100599/* vim_main2() needs to be produced when FEAT_MZSCHEME is defined even when
600 * NO_VIM_MAIN is defined. */
601#ifdef FEAT_MZSCHEME
602 int
603vim_main2(int argc UNUSED, char **argv UNUSED)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100604{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100605# ifndef NO_VIM_MAIN
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100606 char_u *fname = (char_u *)argv[0];
607 mparm_T params;
608
609 memcpy(&params, argv[1], sizeof(params));
Bram Moolenaar8866d272012-11-28 15:55:42 +0100610# else
611 return 0;
612}
613# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100614#endif
615
Bram Moolenaar8866d272012-11-28 15:55:42 +0100616#ifndef NO_VIM_MAIN
Bram Moolenaar58d98232005-07-23 22:25:46 +0000617 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000618 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000619
Bram Moolenaar58d98232005-07-23 22:25:46 +0000620 /* Source startup scripts. */
621 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000622
623#ifdef FEAT_EVAL
624 /*
625 * Read all the plugin files.
626 * Only when compiled with +eval, since most plugins need it.
627 */
628 if (p_lpl)
629 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000630# ifdef VMS /* Somehow VMS doesn't handle the "**". */
631 source_runtime((char_u *)"plugin/*.vim", TRUE);
632# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000633 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000634# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000635 TIME_MSG("loading plugins");
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100636
637 source_packages();
638 TIME_MSG("loading packages");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000639 }
640#endif
641
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000642#ifdef FEAT_DIFF
643 /* Decide about window layout for diff mode after reading vimrc. */
644 if (params.diff_mode && params.window_layout == 0)
645 {
646 if (diffopt_horizontal())
647 params.window_layout = WIN_HOR; /* use horizontal split */
648 else
649 params.window_layout = WIN_VER; /* use vertical split */
650 }
651#endif
652
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000653 /*
654 * Recovery mode without a file name: List swap files.
655 * This uses the 'dir' option, therefore it must be after the
656 * initializations.
657 */
658 if (recoverymode && fname == NULL)
659 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200660 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000661 mch_exit(0);
662 }
663
664 /*
665 * Set a few option defaults after reading .vimrc files:
666 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
667 */
668 set_init_3();
669 TIME_MSG("inits 3");
670
671 /*
672 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
673 * Note that this overrides anything from a vimrc file.
674 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000675 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000676 p_uc = 0;
677
678#ifdef FEAT_FKMAP
679 if (curwin->w_p_rl && p_altkeymap)
680 {
681 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
682# ifdef FEAT_ARABIC
683 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
684# endif
685 p_fkmap = TRUE; /* Set the Farsi keymap mode */
686 }
687#endif
688
689#ifdef FEAT_GUI
690 if (gui.starting)
691 {
692#if defined(UNIX) || defined(VMS)
693 /* When something caused a message from a vimrc script, need to output
694 * an extra newline before the shell prompt. */
695 if (did_emsg || msg_didout)
696 putchar('\n');
697#endif
698
699 gui_start(); /* will set full_screen to TRUE */
700 TIME_MSG("starting GUI");
701
702 /* When running "evim" or "gvim -y" we need the menus, exit if we
703 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000704 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000705 mch_exit(1);
706 }
707#endif
708
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000709#ifdef FEAT_VIMINFO
710 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000711 * Read in registers, history etc, but not marks, from the viminfo file.
712 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000713 */
714 if (*p_viminfo != NUL)
715 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000716 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000717 TIME_MSG("reading viminfo");
718 }
719#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100720#ifdef FEAT_EVAL
721 /* It's better to make v:oldfiles an empty list than NULL. */
722 if (get_vim_var_list(VV_OLDFILES) == NULL)
723 set_vim_var_list(VV_OLDFILES, list_alloc());
724#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000725
726#ifdef FEAT_QUICKFIX
727 /*
728 * "-q errorfile": Load the error file now.
729 * If the error file can't be read, exit before doing anything else.
730 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000731 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000732 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000733 if (params.use_ef != NULL)
734 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000735 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200736 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
737 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000738 {
739 out_char('\n');
740 mch_exit(3);
741 }
742 TIME_MSG("reading errorfile");
743 }
744#endif
745
746 /*
747 * Start putting things on the screen.
748 * Scroll screen down before drawing over it
749 * Clear screen now, so file message will not be cleared.
750 */
751 starting = NO_BUFFERS;
752 no_wait_return = FALSE;
753 if (!exmode_active)
754 msg_scroll = FALSE;
755
756#ifdef FEAT_GUI
757 /*
758 * This seems to be required to make callbacks to be called now, instead
759 * of after things have been put on the screen, which then may be deleted
760 * when getting a resize callback.
761 * For the Mac this handles putting files dropped on the Vim icon to
762 * global_alist.
763 */
764 if (gui.in_use)
765 {
766# ifdef FEAT_SUN_WORKSHOP
767 if (!usingSunWorkShop)
768# endif
769 gui_wait_for_chars(50L);
770 TIME_MSG("GUI delay");
771 }
772#endif
773
774#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
775 qnx_clip_init();
776#endif
777
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200778#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
779 clip_init(TRUE);
780#endif
781
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000782#ifdef FEAT_XCLIPBOARD
783 /* Start using the X clipboard, unless the GUI was started. */
784# ifdef FEAT_GUI
785 if (!gui.in_use)
786# endif
787 {
788 setup_term_clip();
789 TIME_MSG("setup clipboard");
790 }
791#endif
792
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000793#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000794 /* Prepare for being a Vim server. */
795 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000796#endif
797
798 /*
799 * If "-" argument given: Read file from stdin.
800 * Do this before starting Raw mode, because it may change things that the
801 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
802 * are the same terminal: "cat | vim -".
803 * Using autocommands here may cause trouble...
804 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000805 if (params.edit_type == EDIT_STDIN && !recoverymode)
806 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000807
808#if defined(UNIX) || defined(VMS)
809 /* When switching screens and something caused a message from a vimrc
810 * script, need to output an extra newline on exit. */
811 if ((did_emsg || msg_didout) && *T_TI != NUL)
812 newline_on_exit = TRUE;
813#endif
814
815 /*
816 * When done something that is not allowed or error message call
817 * wait_return. This must be done before starttermcap(), because it may
818 * switch to another screen. It must be done after settmode(TMODE_RAW),
819 * because we want to react on a single key stroke.
820 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000821 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000822 */
823 settmode(TMODE_RAW);
824 TIME_MSG("setting raw mode");
825
826 if (need_wait_return || msg_didany)
827 {
828 wait_return(TRUE);
829 TIME_MSG("waiting for return");
830 }
831
832 starttermcap(); /* start termcap if not done by wait_return() */
833 TIME_MSG("start termcap");
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200834#if defined(FEAT_TERMRESPONSE)
835# if defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200836 may_req_ambiguous_char_width();
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200837# endif
838 may_req_bg_color();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100839#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000840
841#ifdef FEAT_MOUSE
842 setmouse(); /* may start using the mouse */
843#endif
844 if (scroll_region)
845 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000846 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000847
848 /*
849 * Don't clear the screen when starting in Ex mode, unless using the GUI.
850 */
851 if (exmode_active
852#ifdef FEAT_GUI
853 && !gui.in_use
854#endif
855 )
856 must_redraw = CLEAR;
857 else
858 {
859 screenclear(); /* clear screen */
860 TIME_MSG("clearing screen");
861 }
862
863#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000864 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000865 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100866 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200867 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000868 TIME_MSG("getting crypt key");
869 }
870#endif
871
872 no_wait_return = TRUE;
873
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000874 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000875 * Create the requested number of windows and edit buffers in them.
876 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000877 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000878 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000879 TIME_MSG("opening buffers");
880
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000881#ifdef FEAT_EVAL
882 /* clear v:swapcommand */
883 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
884#endif
885
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000886 /* Ex starts at last line of the file */
887 if (exmode_active)
888 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
889
890#ifdef FEAT_AUTOCMD
891 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
892 TIME_MSG("BufEnter autocommands");
893#endif
894 setpcmark();
895
896#ifdef FEAT_QUICKFIX
897 /*
898 * When started with "-q errorfile" jump to first error now.
899 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000900 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000901 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000902 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000903 TIME_MSG("jump to first error");
904 }
905#endif
906
907#ifdef FEAT_WINDOWS
908 /*
909 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000910 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000911 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200912 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000913#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200914 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000915
916#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000917 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000918 {
919 win_T *wp;
920
921 /* set options in each window for "vimdiff". */
922 for (wp = firstwin; wp != NULL; wp = wp->w_next)
923 diff_win_options(wp, TRUE);
924 }
925#endif
926
927 /*
928 * Shorten any of the filenames, but only when absolute.
929 */
930 shorten_fnames(FALSE);
931
932 /*
933 * Need to jump to the tag before executing the '-c command'.
934 * Makes "vim -c '/return' -t main" work.
935 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000936 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000937 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000938#if defined(HAS_SWAP_EXISTS_ACTION)
939 swap_exists_did_quit = FALSE;
940#endif
941
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000942 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000943 do_cmdline_cmd(IObuff);
944 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000945
946#if defined(HAS_SWAP_EXISTS_ACTION)
947 /* If the user doesn't want to edit the file then we quit here. */
948 if (swap_exists_did_quit)
949 getout(1);
950#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000951 }
952
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000953 /* Execute any "+", "-c" and "-S" arguments. */
954 if (params.n_commands > 0)
955 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000956
957 RedrawingDisabled = 0;
958 redraw_all_later(NOT_VALID);
959 no_wait_return = FALSE;
960 starting = 0;
961
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000962#ifdef FEAT_TERMRESPONSE
963 /* Requesting the termresponse is postponed until here, so that a "-c q"
964 * argument doesn't make it appear in the shell Vim was started from. */
965 may_req_termresponse();
966#endif
967
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000968 /* start in insert mode */
969 if (p_im)
970 need_start_insertmode = TRUE;
971
972#ifdef FEAT_AUTOCMD
973 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
974 TIME_MSG("VimEnter autocommands");
975#endif
976
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200977#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
978 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
979 * done after the clipboard is available and all initial commands that may
980 * modify the 'clipboard' setting have run; i.e. just before entering the
981 * main loop. */
982 {
983 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100984
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200985 adjust_clip_reg(&default_regname);
986 set_reg_var(default_regname);
987 }
988#endif
989
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000990#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
991 /* When a startup script or session file setup for diff'ing and
992 * scrollbind, sync the scrollbind now. */
993 if (curwin->w_p_diff && curwin->w_p_scb)
994 {
995 update_topline();
996 check_scrollbind((linenr_T)0, 0L);
997 TIME_MSG("diff scrollbinding");
998 }
999#endif
1000
1001#if defined(WIN3264) && !defined(FEAT_GUI_W32)
1002 mch_set_winsize_now(); /* Allow winsize changes from now on */
1003#endif
1004
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001005#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
1006 /* When tab pages were created, may need to update the tab pages line and
1007 * scrollbars. This is skipped while creating them. */
1008 if (first_tabpage->tp_next != NULL)
1009 {
1010 out_flush();
1011 gui_init_which_components(NULL);
1012 gui_update_scrollbars(TRUE);
1013 }
1014 need_mouse_correct = TRUE;
1015#endif
1016
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001017 /* If ":startinsert" command used, stuff a dummy command to be able to
1018 * call normal_cmd(), which will then start Insert mode. */
1019 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001020 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001021
1022#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001023 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001024 {
1025# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +02001026# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02001027 && !defined(FEAT_GUI_W32)
1028 if (gui.in_use)
1029 {
1030 mch_errmsg(_("netbeans is not supported with this GUI\n"));
1031 mch_exit(2);
1032 }
1033# endif
1034# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001035 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001036 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001037 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001038#endif
1039
1040 TIME_MSG("before starting main loop");
1041
1042 /*
1043 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001044 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001045 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001046
1047 return 0;
1048}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001049#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001050#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001051
1052/*
1053 * Main loop: Execute Normal mode commands until exiting Vim.
1054 * Also used to handle commands in the command-line window, until the window
1055 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 * Also used to handle ":visual" command after ":global": execute Normal mode
1057 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001058 */
1059 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001060main_loop(
1061 int cmdwin, /* TRUE when working in the command-line window */
1062 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001063{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001064 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001065 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001066#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001067 /* these are static to avoid a compiler warning */
1068 static linenr_T conceal_old_cursor_line = 0;
1069 static linenr_T conceal_new_cursor_line = 0;
1070 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001071#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001072
1073#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1074 /* Setup to catch a terminating error from the X server. Just ignore
1075 * it, restore the state and continue. This might not always work
1076 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001077 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001078 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001079 {
1080 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001081 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001082 got_int = TRUE;
1083 need_wait_return = FALSE;
1084 global_busy = FALSE;
1085 exmode_active = 0;
1086 skip_redraw = FALSE;
1087 RedrawingDisabled = 0;
1088 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001089 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001090# ifdef FEAT_EVAL
1091 emsg_skip = 0;
1092# endif
1093 emsg_off = 0;
1094# ifdef FEAT_MOUSE
1095 setmouse();
1096# endif
1097 settmode(TMODE_RAW);
1098 starttermcap();
1099 scroll_start();
1100 redraw_later_clear();
1101 }
1102#endif
1103
1104 clear_oparg(&oa);
1105 while (!cmdwin
1106#ifdef FEAT_CMDWIN
1107 || cmdwin_result == 0
1108#endif
1109 )
1110 {
1111 if (stuff_empty())
1112 {
1113 did_check_timestamps = FALSE;
1114 if (need_check_timestamps)
1115 check_timestamps(FALSE);
1116 if (need_wait_return) /* if wait_return still needed ... */
1117 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001118 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001119 {
1120 need_start_insertmode = FALSE;
1121 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1122 /* skip the fileinfo message now, because it would be shown
1123 * after insert mode finishes! */
1124 need_fileinfo = FALSE;
1125 }
1126 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001127
1128 /* Reset "got_int" now that we got back to the main loop. Except when
1129 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1130 * the ":g" command.
1131 * For ":g/pat/vi" we reset "got_int" when used once. When used
1132 * a second time we go back to Ex mode and abort the ":g" command. */
1133 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001134 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001135 if (noexmode && global_busy && !exmode_active && previous_got_int)
1136 {
1137 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1138 * used and keep "got_int" set, so that it aborts ":g". */
1139 exmode_active = EXMODE_NORMAL;
1140 State = NORMAL;
1141 }
1142 else if (!global_busy || !exmode_active)
1143 {
1144 if (!quit_more)
1145 (void)vgetc(); /* flush all buffers */
1146 got_int = FALSE;
1147 }
1148 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001149 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001150 else
1151 previous_got_int = FALSE;
1152
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001153 if (!exmode_active)
1154 msg_scroll = FALSE;
1155 quit_more = FALSE;
1156
1157 /*
1158 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1159 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1160 * update cursor and redraw.
1161 */
1162 if (skip_redraw || exmode_active)
1163 skip_redraw = FALSE;
1164 else if (do_redraw || stuff_empty())
1165 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001166#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001167 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001168 if (!finish_op && (
1169# ifdef FEAT_AUTOCMD
1170 has_cursormoved()
1171# endif
1172# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1173 ||
1174# endif
1175# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001176 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001177# endif
1178 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001179# ifdef FEAT_AUTOCMD
1180 && !equalpos(last_cursormoved, curwin->w_cursor)
1181# endif
1182 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001183 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001184# ifdef FEAT_AUTOCMD
1185 if (has_cursormoved())
1186 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1187 FALSE, curbuf);
1188# endif
1189# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001190 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001191 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001192# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001193 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001194# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001195 conceal_new_cursor_line = curwin->w_cursor.lnum;
1196 conceal_update_lines = TRUE;
1197 }
1198# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001199# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001200 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001201# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001202 }
1203#endif
1204
Bram Moolenaar186628f2013-03-19 13:33:23 +01001205#ifdef FEAT_AUTOCMD
1206 /* Trigger TextChanged if b_changedtick differs. */
1207 if (!finish_op && has_textchanged()
1208 && last_changedtick != curbuf->b_changedtick)
1209 {
1210 if (last_changedtick_buf == curbuf)
1211 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1212 FALSE, curbuf);
1213 last_changedtick_buf = curbuf;
1214 last_changedtick = curbuf->b_changedtick;
1215 }
1216#endif
1217
Bram Moolenaar33aec762006-01-22 23:30:12 +00001218#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1219 /* Scroll-binding for diff mode may have been postponed until
1220 * here. Avoids doing it for every change. */
1221 if (diff_need_scrollbind)
1222 {
1223 check_scrollbind((linenr_T)0, 0L);
1224 diff_need_scrollbind = FALSE;
1225 }
1226#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001227#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001228 /* Include a closed fold completely in the Visual area. */
1229 foldAdjustVisual();
1230#endif
1231#ifdef FEAT_FOLDING
1232 /*
1233 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1234 * contain the cursor.
1235 * When 'foldopen' is "all", open the fold(s) under the cursor.
1236 * This may mark the window for redrawing.
1237 */
1238 if (hasAnyFolding(curwin) && !char_avail())
1239 {
1240 foldCheckClose();
1241 if (fdo_flags & FDO_ALL)
1242 foldOpenCursor();
1243 }
1244#endif
1245
1246 /*
1247 * Before redrawing, make sure w_topline is correct, and w_leftcol
1248 * if lines don't wrap, and w_skipcol if lines wrap.
1249 */
1250 update_topline();
1251 validate_cursor();
1252
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001253 if (VIsual_active)
1254 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001255 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001256 update_screen(0);
1257 else if (redraw_cmdline || clear_cmdline)
1258 showmode();
1259#ifdef FEAT_WINDOWS
1260 redraw_statuslines();
1261#endif
1262#ifdef FEAT_TITLE
1263 if (need_maketitle)
1264 maketitle();
1265#endif
1266 /* display message after redraw */
1267 if (keep_msg != NULL)
1268 {
1269 char_u *p;
1270
1271 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001272 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1273 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001274 p = keep_msg;
1275 msg_attr(p, keep_msg_attr);
1276 vim_free(p);
1277 }
1278 if (need_fileinfo) /* show file info after redraw */
1279 {
1280 fileinfo(FALSE, TRUE, FALSE);
1281 need_fileinfo = FALSE;
1282 }
1283
1284 emsg_on_display = FALSE; /* can delete error message now */
1285 did_emsg = FALSE;
1286 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001287 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001288 showruler(FALSE);
1289
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001290# if defined(FEAT_CONCEAL)
1291 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001292 && (conceal_old_cursor_line != conceal_new_cursor_line
1293 || conceal_cursor_line(curwin)
1294 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001295 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001296 if (conceal_old_cursor_line != conceal_new_cursor_line
1297 && conceal_old_cursor_line
1298 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001299 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001300 update_single_line(curwin, conceal_new_cursor_line);
1301 curwin->w_valid &= ~VALID_CROW;
1302 }
1303# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001304 setcursor();
1305 cursor_on();
1306
1307 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001308
1309#ifdef STARTUPTIME
1310 /* Now that we have drawn the first screen all the startup stuff
1311 * has been done, close any file for startup messages. */
1312 if (time_fd != NULL)
1313 {
1314 TIME_MSG("first screen update");
1315 TIME_MSG("--- VIM STARTED ---");
1316 fclose(time_fd);
1317 time_fd = NULL;
1318 }
1319#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001320 }
1321#ifdef FEAT_GUI
1322 if (need_mouse_correct)
1323 gui_mouse_correct();
1324#endif
1325
1326 /*
1327 * Update w_curswant if w_set_curswant has been set.
1328 * Postponed until here to avoid computing w_virtcol too often.
1329 */
1330 update_curswant();
1331
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001332#ifdef FEAT_EVAL
1333 /*
1334 * May perform garbage collection when waiting for a character, but
1335 * only at the very toplevel. Otherwise we may be using a List or
1336 * Dict internally somewhere.
1337 * "may_garbage_collect" is reset in vgetc() which is invoked through
1338 * do_exmode() and normal_cmd().
1339 */
1340 may_garbage_collect = (!cmdwin && !noexmode);
1341#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001342 /*
1343 * If we're invoked as ex, do a round of ex commands.
1344 * Otherwise, get and execute a normal mode command.
1345 */
1346 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001347 {
1348 if (noexmode) /* End of ":global/path/visual" commands */
1349 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001350 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001351 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001352 else
1353 normal_cmd(&oa, TRUE);
1354 }
1355}
1356
1357
1358#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1359/*
1360 * Exit, but leave behind swap files for modified buffers.
1361 */
1362 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001363getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001364{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001365# if defined(SIGHUP) && defined(SIG_IGN)
1366 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1367 * makes Vim exit and then handling SIGHUP causes various reentrance
1368 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001369 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001370# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001371
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001372 ml_close_notmod(); /* close all not-modified buffers */
1373 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1374 ml_close_all(FALSE); /* close all memfiles, without deleting */
1375 getout(exitval); /* exit Vim properly */
1376}
1377#endif
1378
1379
1380/* Exit properly */
1381 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001382getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001383{
1384#ifdef FEAT_AUTOCMD
1385 buf_T *buf;
1386 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001387 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001388#endif
1389
1390 exiting = TRUE;
1391
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001392 /* When running in Ex mode an error causes us to exit with a non-zero exit
1393 * code. POSIX requires this, although it's not 100% clear from the
1394 * standard. */
1395 if (exmode_active)
1396 exitval += ex_exitval;
1397
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001398 /* Position the cursor on the last screen line, below all the text */
1399#ifdef FEAT_GUI
1400 if (!gui.in_use)
1401#endif
1402 windgoto((int)Rows - 1, 0);
1403
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001404#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1405 /* Optionally print hashtable efficiency. */
1406 hash_debug_results();
1407#endif
1408
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001409#ifdef FEAT_GUI
1410 msg_didany = FALSE;
1411#endif
1412
1413#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001414 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001415 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001416 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1417# if defined FEAT_WINDOWS
1418 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001419 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001420 next_tp = tp->tp_next;
1421 for (wp = (tp == curtab)
1422 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001423 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001424 if (wp->w_buffer == NULL)
1425 /* Autocmd must have close the buffer already, skip. */
1426 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001427 buf = wp->w_buffer;
1428 if (buf->b_changedtick != -1)
1429 {
1430 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1431 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001432 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001433 /* start all over, autocommands may mess up the lists */
1434 next_tp = first_tabpage;
1435 break;
1436 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001437 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001438 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001439# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001440 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1441 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001442# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001443
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001444 /* Trigger BufUnload for buffers that are loaded */
1445 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1446 if (buf->b_ml.ml_mfp != NULL)
1447 {
1448 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001449 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001450 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1451 break;
1452 }
1453 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1454 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001455#endif
1456
1457#ifdef FEAT_VIMINFO
1458 if (*p_viminfo != NUL)
1459 /* Write out the registers, history, marks etc, to the viminfo file */
1460 write_viminfo(NULL, FALSE);
1461#endif
1462
1463#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001464 if (get_vim_var_nr(VV_DYING) <= 1)
1465 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001466#endif
1467
Bram Moolenaar05159a02005-02-26 23:04:13 +00001468#ifdef FEAT_PROFILE
1469 profile_dump();
1470#endif
1471
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001472 if (did_emsg
1473#ifdef FEAT_GUI
1474 || (gui.in_use && msg_didany && p_verbose > 0)
1475#endif
1476 )
1477 {
1478 /* give the user a chance to read the (error) message */
1479 no_wait_return = FALSE;
1480 wait_return(FALSE);
1481 }
1482
1483#ifdef FEAT_AUTOCMD
1484 /* Position the cursor again, the autocommands may have moved it */
1485# ifdef FEAT_GUI
1486 if (!gui.in_use)
1487# endif
1488 windgoto((int)Rows - 1, 0);
1489#endif
1490
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001491#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001492 job_stop_on_exit();
1493#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001494#ifdef FEAT_LUA
1495 lua_end();
1496#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001497#ifdef FEAT_MZSCHEME
1498 mzscheme_end();
1499#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001500#ifdef FEAT_TCL
1501 tcl_end();
1502#endif
1503#ifdef FEAT_RUBY
1504 ruby_end();
1505#endif
1506#ifdef FEAT_PYTHON
1507 python_end();
1508#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001509#ifdef FEAT_PYTHON3
1510 python3_end();
1511#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001512#ifdef FEAT_PERL
1513 perl_end();
1514#endif
1515#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1516 iconv_end();
1517#endif
1518#ifdef FEAT_NETBEANS_INTG
1519 netbeans_end();
1520#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001521#ifdef FEAT_CSCOPE
1522 cs_end();
1523#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001524#ifdef FEAT_EVAL
1525 if (garbage_collect_at_exit)
1526 garbage_collect();
1527#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001528#if defined(WIN32) && defined(FEAT_MBYTE)
1529 free_cmd_argsW();
1530#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001531
1532 mch_exit(exitval);
1533}
1534
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001535#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001536/*
1537 * Get a (optional) count for a Vim argument.
1538 */
1539 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001540get_number_arg(
1541 char_u *p, /* pointer to argument */
1542 int *idx, /* index in argument, is incremented */
1543 int def) /* default value */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001544{
1545 if (vim_isdigit(p[*idx]))
1546 {
1547 def = atoi((char *)&(p[*idx]));
1548 while (vim_isdigit(p[*idx]))
1549 *idx = *idx + 1;
1550 }
1551 return def;
1552}
1553
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001554#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1555/*
1556 * Setup to use the current locale (for ctype() and many other things).
1557 */
1558 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001559init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001560{
1561 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001562
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001563# ifdef FEAT_GUI_GTK
1564 /* Tell Gtk not to change our locale settings. */
1565 gtk_disable_setlocale();
1566# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001567# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1568 /* Make sure strtod() uses a decimal point, not a comma. */
1569 setlocale(LC_NUMERIC, "C");
1570# endif
1571
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001572# ifdef WIN32
1573 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1574 * text while it's expecting text in the current locale. This call avoids
1575 * that. */
1576 setlocale(LC_CTYPE, "C");
1577# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001578
1579# ifdef FEAT_GETTEXT
1580 {
1581 int mustfree = FALSE;
1582 char_u *p;
1583
1584# ifdef DYNAMIC_GETTEXT
1585 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001586 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001587# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001588 /* expand_env() doesn't work yet, because g_chartab[] is not
1589 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001590 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1591 if (p != NULL && *p != NUL)
1592 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001593 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001594 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1595 }
1596 if (mustfree)
1597 vim_free(p);
1598 textdomain(VIMPACKAGE);
1599 }
1600# endif
1601}
1602#endif
1603
1604/*
1605 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1606 * If the executable name starts with "r" we disable shell commands.
1607 * If the next character is "e" we run in Easy mode.
1608 * If the next character is "g" we run the GUI version.
1609 * If the next characters are "view" we start in readonly mode.
1610 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1611 * If the next characters are "ex" we start in Ex mode. If it's followed
1612 * by "im" use improved Ex mode.
1613 */
1614 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001615parse_command_name(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001616{
1617 char_u *initstr;
1618
1619 initstr = gettail((char_u *)parmp->argv[0]);
1620
1621#ifdef MACOS_X_UNIX
1622 /* An issue has been seen when launching Vim in such a way that
1623 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1624 * executable or a symbolic link of it. Until this issue is resolved
1625 * we prohibit the GUI from being used.
1626 */
1627 if (STRCMP(initstr, parmp->argv[0]) == 0)
1628 disallow_gui = TRUE;
1629
1630 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001631 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001632#endif
1633
1634#ifdef FEAT_EVAL
1635 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaara1706c92014-04-01 19:55:49 +02001636 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001637#endif
1638
1639 if (TOLOWER_ASC(initstr[0]) == 'r')
1640 {
1641 restricted = TRUE;
1642 ++initstr;
1643 }
1644
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001645 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001646 if (TOLOWER_ASC(initstr[0]) == 'e'
1647 && (TOLOWER_ASC(initstr[1]) == 'v'
1648 || TOLOWER_ASC(initstr[1]) == 'g'))
1649 {
1650#ifdef FEAT_GUI
1651 gui.starting = TRUE;
1652#endif
1653 parmp->evim_mode = TRUE;
1654 ++initstr;
1655 }
1656
Bram Moolenaar806875d2008-09-18 18:57:10 +00001657 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1658 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001659 {
1660 main_start_gui();
1661#ifdef FEAT_GUI
1662 ++initstr;
1663#endif
1664 }
1665
1666 if (STRNICMP(initstr, "view", 4) == 0)
1667 {
1668 readonlymode = TRUE;
1669 curbuf->b_p_ro = TRUE;
1670 p_uc = 10000; /* don't update very often */
1671 initstr += 4;
1672 }
1673 else if (STRNICMP(initstr, "vim", 3) == 0)
1674 initstr += 3;
1675
1676 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1677 if (STRICMP(initstr, "diff") == 0)
1678 {
1679#ifdef FEAT_DIFF
1680 parmp->diff_mode = TRUE;
1681#else
1682 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1683 mch_errmsg("\n");
1684 mch_exit(2);
1685#endif
1686 }
1687
1688 if (STRNICMP(initstr, "ex", 2) == 0)
1689 {
1690 if (STRNICMP(initstr + 2, "im", 2) == 0)
1691 exmode_active = EXMODE_VIM;
1692 else
1693 exmode_active = EXMODE_NORMAL;
1694 change_compatible(TRUE); /* set 'compatible' */
1695 }
1696}
1697
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001698/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001699 * Get the name of the display, before gui_prepare() removes it from
1700 * argv[]. Used for the xterm-clipboard display.
1701 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001702 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001703 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001704 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001705early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001706{
Bram Moolenaar03005972008-11-20 13:12:36 +00001707#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1708 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001709 int argc = parmp->argc;
1710 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001711 int i;
1712
1713 for (i = 1; i < argc; i++)
1714 {
1715 if (STRCMP(argv[i], "--") == 0)
1716 break;
1717# ifdef FEAT_XCLIPBOARD
1718 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001719# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001720 || STRICMP(argv[i], "--display") == 0
1721# endif
1722 )
1723 {
1724 if (i == argc - 1)
1725 mainerr_arg_missing((char_u *)argv[i]);
1726 xterm_display = argv[++i];
1727 }
1728# endif
1729# ifdef FEAT_CLIENTSERVER
1730 else if (STRICMP(argv[i], "--servername") == 0)
1731 {
1732 if (i == argc - 1)
1733 mainerr_arg_missing((char_u *)argv[i]);
1734 parmp->serverName_arg = (char_u *)argv[++i];
1735 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001736 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001737 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001738 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001739 {
1740 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001741# ifdef FEAT_GUI
1742 if (strstr(argv[i], "-wait") != 0)
1743 /* don't fork() when starting the GUI to edit files ourself */
1744 gui.dofork = FALSE;
1745# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001746 }
1747# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001748
1749# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1750# ifdef FEAT_GUI_W32
1751 else if (STRICMP(argv[i], "--windowid") == 0)
1752# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001753 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001754# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001755 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001756 long_u id;
1757 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001758
1759 if (i == argc - 1)
1760 mainerr_arg_missing((char_u *)argv[i]);
1761 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001762 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001763 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001764 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001765 if (count != 1)
1766 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1767 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001768# ifdef FEAT_GUI_W32
1769 win_socket_id = id;
1770# else
1771 gtk_socket_id = id;
1772# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001773 i++;
1774 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001775# endif
1776# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001777 else if (STRICMP(argv[i], "--echo-wid") == 0)
1778 echo_wid_arg = TRUE;
1779# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001780# ifndef FEAT_NETBEANS_INTG
1781 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001782 {
1783 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1784 mch_exit(2);
1785 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001786# endif
1787
Bram Moolenaar58d98232005-07-23 22:25:46 +00001788 }
1789#endif
1790}
1791
1792/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001793 * Scan the command line arguments.
1794 */
1795 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001796command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001797{
1798 int argc = parmp->argc;
1799 char **argv = parmp->argv;
1800 int argv_idx; /* index in argv[n][] */
1801 int had_minmin = FALSE; /* found "--" argument */
1802 int want_argument; /* option argument with argument */
1803 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001804 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001805 long n;
1806
1807 --argc;
1808 ++argv;
1809 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1810 while (argc > 0)
1811 {
1812 /*
1813 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1814 */
1815 if (argv[0][0] == '+' && !had_minmin)
1816 {
1817 if (parmp->n_commands >= MAX_ARG_CMDS)
1818 mainerr(ME_EXTRA_CMD, NULL);
1819 argv_idx = -1; /* skip to next argument */
1820 if (argv[0][1] == NUL)
1821 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1822 else
1823 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1824 }
1825
1826 /*
1827 * Optional argument.
1828 */
1829 else if (argv[0][0] == '-' && !had_minmin)
1830 {
1831 want_argument = FALSE;
1832 c = argv[0][argv_idx++];
1833#ifdef VMS
1834 /*
1835 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1836 * and "-/X" as "-X".
1837 */
1838 if (c == '/')
1839 {
1840 c = argv[0][argv_idx++];
1841 c = TOUPPER_ASC(c);
1842 }
1843 else
1844 c = TOLOWER_ASC(c);
1845#endif
1846 switch (c)
1847 {
1848 case NUL: /* "vim -" read from stdin */
1849 /* "ex -" silent mode */
1850 if (exmode_active)
1851 silent_mode = TRUE;
1852 else
1853 {
1854 if (parmp->edit_type != EDIT_NONE)
1855 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1856 parmp->edit_type = EDIT_STDIN;
1857 read_cmd_fd = 2; /* read from stderr instead of stdin */
1858 }
1859 argv_idx = -1; /* skip to next argument */
1860 break;
1861
1862 case '-': /* "--" don't take any more option arguments */
1863 /* "--help" give help message */
1864 /* "--version" give version message */
1865 /* "--literal" take files literally */
1866 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001867 /* "--not-a-term" don't warn for not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001868 /* "--noplugin[s]" skip plugins */
1869 /* "--cmd <cmd>" execute cmd before vimrc */
1870 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1871 usage();
1872 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1873 {
1874 Columns = 80; /* need to init Columns */
1875 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1876 list_version();
1877 msg_putchar('\n');
1878 msg_didout = FALSE;
1879 mch_exit(0);
1880 }
1881 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1882 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001883#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001884 parmp->literal = TRUE;
1885#endif
1886 }
1887 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1888 {
1889#ifdef FEAT_GUI
1890 gui.dofork = FALSE; /* don't fork() when starting GUI */
1891#endif
1892 }
1893 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1894 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001895 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1896 parmp->not_a_term = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001897 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1898 {
1899 want_argument = TRUE;
1900 argv_idx += 3;
1901 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001902 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1903 {
1904 want_argument = TRUE;
1905 argv_idx += 11;
1906 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001907#ifdef FEAT_CLIENTSERVER
1908 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1909 ; /* already processed -- no arg */
1910 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1911 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1912 {
1913 /* already processed -- snatch the following arg */
1914 if (argc > 1)
1915 {
1916 --argc;
1917 ++argv;
1918 }
1919 }
1920#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001921#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1922# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001923 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001924# else
1925 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1926# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001927 {
1928 /* already processed -- snatch the following arg */
1929 if (argc > 1)
1930 {
1931 --argc;
1932 ++argv;
1933 }
1934 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001935#endif
1936#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001937 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1938 {
1939 /* already processed, skip */
1940 }
1941#endif
1942 else
1943 {
1944 if (argv[0][argv_idx])
1945 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1946 had_minmin = TRUE;
1947 }
1948 if (!want_argument)
1949 argv_idx = -1; /* skip to next argument */
1950 break;
1951
1952 case 'A': /* "-A" start in Arabic mode */
1953#ifdef FEAT_ARABIC
1954 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1955#else
1956 mch_errmsg(_(e_noarabic));
1957 mch_exit(2);
1958#endif
1959 break;
1960
1961 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001962 /* Needs to be effective before expanding file names, because
1963 * for Win32 this makes us edit a shortcut file itself,
1964 * instead of the file it links to. */
1965 set_options_bin(curbuf->b_p_bin, 1, 0);
1966 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001967 break;
1968
1969 case 'C': /* "-C" Compatible */
1970 change_compatible(TRUE);
1971 break;
1972
1973 case 'e': /* "-e" Ex mode */
1974 exmode_active = EXMODE_NORMAL;
1975 break;
1976
1977 case 'E': /* "-E" Improved Ex mode */
1978 exmode_active = EXMODE_VIM;
1979 break;
1980
1981 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1982 window directly, not with newcli */
1983#ifdef FEAT_GUI
1984 gui.dofork = FALSE; /* don't fork() when starting GUI */
1985#endif
1986 break;
1987
1988 case 'g': /* "-g" start GUI */
1989 main_start_gui();
1990 break;
1991
1992 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1993#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001994 p_fkmap = TRUE;
1995 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001996#else
1997 mch_errmsg(_(e_nofarsi));
1998 mch_exit(2);
1999#endif
2000 break;
2001
2002 case 'h': /* "-h" give help message */
2003#ifdef FEAT_GUI_GNOME
2004 /* Tell usage() to exit for "gvim". */
2005 gui.starting = FALSE;
2006#endif
2007 usage();
2008 break;
2009
2010 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2011#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002012 p_hkmap = TRUE;
2013 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002014#else
2015 mch_errmsg(_(e_nohebrew));
2016 mch_exit(2);
2017#endif
2018 break;
2019
2020 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2021#ifdef FEAT_LISP
2022 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2023 p_sm = TRUE;
2024#endif
2025 break;
2026
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002027 case 'M': /* "-M" no changes or writing of files */
2028 reset_modifiable();
2029 /* FALLTHROUGH */
2030
2031 case 'm': /* "-m" no writing of files */
2032 p_write = FALSE;
2033 break;
2034
2035 case 'y': /* "-y" easy mode */
2036#ifdef FEAT_GUI
2037 gui.starting = TRUE; /* start GUI a bit later */
2038#endif
2039 parmp->evim_mode = TRUE;
2040 break;
2041
2042 case 'N': /* "-N" Nocompatible */
2043 change_compatible(FALSE);
2044 break;
2045
2046 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002047#ifdef FEAT_NETBEANS_INTG
2048 /* checking for "-nb", netbeans parameters */
2049 if (argv[0][argv_idx] == 'b')
2050 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002051 netbeansArg = argv[0];
2052 argv_idx = -1; /* skip to next argument */
2053 }
2054 else
2055#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002056 parmp->no_swap_file = TRUE;
2057 break;
2058
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002059 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002060#ifdef TARGET_API_MAC_OSX
2061 /* For some reason on MacOS X, an argument like:
2062 -psn_0_10223617 is passed in when invoke from Finder
2063 or with the 'open' command */
2064 if (argv[0][argv_idx] == 's')
2065 {
2066 argv_idx = -1; /* bypass full -psn */
2067 main_start_gui();
2068 break;
2069 }
2070#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002071#ifdef FEAT_WINDOWS
2072 /* default is 0: open window for each file */
2073 parmp->window_count = get_number_arg((char_u *)argv[0],
2074 &argv_idx, 0);
2075 parmp->window_layout = WIN_TABS;
2076#endif
2077 break;
2078
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002079 case 'o': /* "-o[N]" open N horizontal split windows */
2080#ifdef FEAT_WINDOWS
2081 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002082 parmp->window_count = get_number_arg((char_u *)argv[0],
2083 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002084 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002085#endif
2086 break;
2087
2088 case 'O': /* "-O[N]" open N vertical split windows */
2089#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2090 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002091 parmp->window_count = get_number_arg((char_u *)argv[0],
2092 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002093 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002094#endif
2095 break;
2096
2097#ifdef FEAT_QUICKFIX
2098 case 'q': /* "-q" QuickFix mode */
2099 if (parmp->edit_type != EDIT_NONE)
2100 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2101 parmp->edit_type = EDIT_QF;
2102 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2103 {
2104 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2105 argv_idx = -1;
2106 }
2107 else if (argc > 1) /* "-q {errorfile}" */
2108 want_argument = TRUE;
2109 break;
2110#endif
2111
2112 case 'R': /* "-R" readonly mode */
2113 readonlymode = TRUE;
2114 curbuf->b_p_ro = TRUE;
2115 p_uc = 10000; /* don't update very often */
2116 break;
2117
2118 case 'r': /* "-r" recovery mode */
2119 case 'L': /* "-L" recovery mode */
2120 recoverymode = 1;
2121 break;
2122
2123 case 's':
2124 if (exmode_active) /* "-s" silent (batch) mode */
2125 silent_mode = TRUE;
2126 else /* "-s {scriptin}" read from script file */
2127 want_argument = TRUE;
2128 break;
2129
2130 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2131 if (parmp->edit_type != EDIT_NONE)
2132 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2133 parmp->edit_type = EDIT_TAG;
2134 if (argv[0][argv_idx]) /* "-t{tag}" */
2135 {
2136 parmp->tagname = (char_u *)argv[0] + argv_idx;
2137 argv_idx = -1;
2138 }
2139 else /* "-t {tag}" */
2140 want_argument = TRUE;
2141 break;
2142
2143#ifdef FEAT_EVAL
2144 case 'D': /* "-D" Debugging */
2145 parmp->use_debug_break_level = 9999;
2146 break;
2147#endif
2148#ifdef FEAT_DIFF
2149 case 'd': /* "-d" 'diff' */
2150# ifdef AMIGA
2151 /* check for "-dev {device}" */
2152 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2153 want_argument = TRUE;
2154 else
2155# endif
2156 parmp->diff_mode = TRUE;
2157 break;
2158#endif
2159 case 'V': /* "-V{N}" Verbose level */
2160 /* default is 10: a little bit verbose */
2161 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2162 if (argv[0][argv_idx] != NUL)
2163 {
2164 set_option_value((char_u *)"verbosefile", 0L,
2165 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002166 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002167 }
2168 break;
2169
2170 case 'v': /* "-v" Vi-mode (as if called "vi") */
2171 exmode_active = 0;
2172#ifdef FEAT_GUI
2173 gui.starting = FALSE; /* don't start GUI */
2174#endif
2175 break;
2176
2177 case 'w': /* "-w{number}" set window height */
2178 /* "-w {scriptout}" write to script */
2179 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2180 {
2181 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2182 set_option_value((char_u *)"window", n, NULL, 0);
2183 break;
2184 }
2185 want_argument = TRUE;
2186 break;
2187
2188#ifdef FEAT_CRYPT
2189 case 'x': /* "-x" encrypted reading/writing of files */
2190 parmp->ask_for_key = TRUE;
2191 break;
2192#endif
2193
2194 case 'X': /* "-X" don't connect to X server */
2195#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2196 x_no_connect = TRUE;
2197#endif
2198 break;
2199
2200 case 'Z': /* "-Z" restricted mode */
2201 restricted = TRUE;
2202 break;
2203
2204 case 'c': /* "-c{command}" or "-c {command}" execute
2205 command */
2206 if (argv[0][argv_idx] != NUL)
2207 {
2208 if (parmp->n_commands >= MAX_ARG_CMDS)
2209 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002210 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2211 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002212 argv_idx = -1;
2213 break;
2214 }
2215 /*FALLTHROUGH*/
2216 case 'S': /* "-S {file}" execute Vim script */
2217 case 'i': /* "-i {viminfo}" use for viminfo */
2218#ifndef FEAT_DIFF
2219 case 'd': /* "-d {device}" device (for Amiga) */
2220#endif
2221 case 'T': /* "-T {terminal}" terminal name */
2222 case 'u': /* "-u {vimrc}" vim inits file */
2223 case 'U': /* "-U {gvimrc}" gvim inits file */
2224 case 'W': /* "-W {scriptout}" overwrite */
2225#ifdef FEAT_GUI_W32
2226 case 'P': /* "-P {parent title}" MDI parent */
2227#endif
2228 want_argument = TRUE;
2229 break;
2230
2231 default:
2232 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2233 }
2234
2235 /*
2236 * Handle option arguments with argument.
2237 */
2238 if (want_argument)
2239 {
2240 /*
2241 * Check for garbage immediately after the option letter.
2242 */
2243 if (argv[0][argv_idx] != NUL)
2244 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2245
2246 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002247 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002248 mainerr_arg_missing((char_u *)argv[0]);
2249 ++argv;
2250 argv_idx = -1;
2251
2252 switch (c)
2253 {
2254 case 'c': /* "-c {command}" execute command */
2255 case 'S': /* "-S {file}" execute Vim script */
2256 if (parmp->n_commands >= MAX_ARG_CMDS)
2257 mainerr(ME_EXTRA_CMD, NULL);
2258 if (c == 'S')
2259 {
2260 char *a;
2261
2262 if (argc < 1)
2263 /* "-S" without argument: use default session file
2264 * name. */
2265 a = SESSION_FILE;
2266 else if (argv[0][0] == '-')
2267 {
2268 /* "-S" followed by another option: use default
2269 * session file name. */
2270 a = SESSION_FILE;
2271 ++argc;
2272 --argv;
2273 }
2274 else
2275 a = argv[0];
2276 p = alloc((unsigned)(STRLEN(a) + 4));
2277 if (p == NULL)
2278 mch_exit(2);
2279 sprintf((char *)p, "so %s", a);
2280 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2281 parmp->commands[parmp->n_commands++] = p;
2282 }
2283 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002284 parmp->commands[parmp->n_commands++] =
2285 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002286 break;
2287
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002288 case '-':
2289 if (argv[-1][2] == 'c')
2290 {
2291 /* "--cmd {command}" execute command */
2292 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2293 mainerr(ME_EXTRA_CMD, NULL);
2294 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002295 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002296 }
2297 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002298 break;
2299
2300 /* case 'd': -d {device} is handled in mch_check_win() for the
2301 * Amiga */
2302
2303#ifdef FEAT_QUICKFIX
2304 case 'q': /* "-q {errorfile}" QuickFix mode */
2305 parmp->use_ef = (char_u *)argv[0];
2306 break;
2307#endif
2308
2309 case 'i': /* "-i {viminfo}" use for viminfo */
2310 use_viminfo = (char_u *)argv[0];
2311 break;
2312
2313 case 's': /* "-s {scriptin}" read from script file */
2314 if (scriptin[0] != NULL)
2315 {
2316scripterror:
2317 mch_errmsg(_("Attempt to open script file again: \""));
2318 mch_errmsg(argv[-1]);
2319 mch_errmsg(" ");
2320 mch_errmsg(argv[0]);
2321 mch_errmsg("\"\n");
2322 mch_exit(2);
2323 }
2324 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2325 {
2326 mch_errmsg(_("Cannot open for reading: \""));
2327 mch_errmsg(argv[0]);
2328 mch_errmsg("\"\n");
2329 mch_exit(2);
2330 }
2331 if (save_typebuf() == FAIL)
2332 mch_exit(2); /* out of memory */
2333 break;
2334
2335 case 't': /* "-t {tag}" */
2336 parmp->tagname = (char_u *)argv[0];
2337 break;
2338
2339 case 'T': /* "-T {terminal}" terminal name */
2340 /*
2341 * The -T term argument is always available and when
2342 * HAVE_TERMLIB is supported it overrides the environment
2343 * variable TERM.
2344 */
2345#ifdef FEAT_GUI
2346 if (term_is_gui((char_u *)argv[0]))
2347 gui.starting = TRUE; /* start GUI a bit later */
2348 else
2349#endif
2350 parmp->term = (char_u *)argv[0];
2351 break;
2352
2353 case 'u': /* "-u {vimrc}" vim inits file */
2354 parmp->use_vimrc = (char_u *)argv[0];
2355 break;
2356
2357 case 'U': /* "-U {gvimrc}" gvim inits file */
2358#ifdef FEAT_GUI
2359 use_gvimrc = (char_u *)argv[0];
2360#endif
2361 break;
2362
2363 case 'w': /* "-w {nr}" 'window' value */
2364 /* "-w {scriptout}" append to script file */
2365 if (vim_isdigit(*((char_u *)argv[0])))
2366 {
2367 argv_idx = 0;
2368 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2369 set_option_value((char_u *)"window", n, NULL, 0);
2370 argv_idx = -1;
2371 break;
2372 }
2373 /*FALLTHROUGH*/
2374 case 'W': /* "-W {scriptout}" overwrite script file */
2375 if (scriptout != NULL)
2376 goto scripterror;
2377 if ((scriptout = mch_fopen(argv[0],
2378 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2379 {
2380 mch_errmsg(_("Cannot open for script output: \""));
2381 mch_errmsg(argv[0]);
2382 mch_errmsg("\"\n");
2383 mch_exit(2);
2384 }
2385 break;
2386
2387#ifdef FEAT_GUI_W32
2388 case 'P': /* "-P {parent title}" MDI parent */
2389 gui_mch_set_parent(argv[0]);
2390 break;
2391#endif
2392 }
2393 }
2394 }
2395
2396 /*
2397 * File name argument.
2398 */
2399 else
2400 {
2401 argv_idx = -1; /* skip to next argument */
2402
2403 /* Check for only one type of editing. */
2404 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2405 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2406 parmp->edit_type = EDIT_FILE;
2407
2408#ifdef MSWIN
2409 /* Remember if the argument was a full path before changing
2410 * slashes to backslashes. */
2411 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2412 parmp->full_path = TRUE;
2413#endif
2414
2415 /* Add the file to the global argument list. */
2416 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2417 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2418 mch_exit(2);
2419#ifdef FEAT_DIFF
2420 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2421 && !mch_isdir(alist_name(&GARGLIST[0])))
2422 {
2423 char_u *r;
2424
2425 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2426 if (r != NULL)
2427 {
2428 vim_free(p);
2429 p = r;
2430 }
2431 }
2432#endif
2433#if defined(__CYGWIN32__) && !defined(WIN32)
2434 /*
2435 * If vim is invoked by non-Cygwin tools, convert away any
2436 * DOS paths, so things like .swp files are created correctly.
2437 * Look for evidence of non-Cygwin paths before we bother.
2438 * This is only for when using the Unix files.
2439 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002440 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002441 {
2442 char posix_path[PATH_MAX];
2443
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002444# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2445 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2446# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002447 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002448# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002449 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002450 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002451 if (p == NULL)
2452 mch_exit(2);
2453 }
2454#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002455
2456#ifdef USE_FNAME_CASE
2457 /* Make the case of the file name match the actual file. */
2458 fname_case(p, 0);
2459#endif
2460
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002461 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002462#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002463 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002464#else
2465 2 /* add buffer number now and use curbuf */
2466#endif
2467 );
2468
2469#if defined(FEAT_MBYTE) && defined(WIN32)
2470 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002471 /* Remember this argument has been added to the argument list.
2472 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002473 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002474# ifdef FEAT_DIFF
2475 parmp->diff_mode
2476# else
2477 FALSE
2478# endif
2479 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002480 }
2481#endif
2482 }
2483
2484 /*
2485 * If there are no more letters after the current "-", go to next
2486 * argument. argv_idx is set to -1 when the current argument is to be
2487 * skipped.
2488 */
2489 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2490 {
2491 --argc;
2492 ++argv;
2493 argv_idx = 1;
2494 }
2495 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002496
2497#ifdef FEAT_EVAL
2498 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2499 * one. */
2500 if (parmp->n_commands > 0)
2501 {
2502 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2503 if (p != NULL)
2504 {
2505 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2506 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2507 vim_free(p);
2508 }
2509 }
2510#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002511}
2512
2513/*
2514 * Print a warning if stdout is not a terminal.
2515 * When starting in Ex mode and commands come from a file, set Silent mode.
2516 */
2517 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002518check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002519{
2520 int input_isatty; /* is active input a terminal? */
2521
2522 input_isatty = mch_input_isatty();
2523 if (exmode_active)
2524 {
2525 if (!input_isatty)
2526 silent_mode = TRUE;
2527 }
2528 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2529#ifdef FEAT_GUI
2530 /* don't want the delay when started from the desktop */
2531 && !gui.starting
2532#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002533 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002534 {
2535#ifdef NBDEBUG
2536 /*
2537 * This shouldn't be necessary. But if I run netbeans with the log
2538 * output coming to the console and XOpenDisplay fails, I get vim
2539 * trying to start with input/output to my console tty. This fills my
2540 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002541 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002542 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002543 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002544 {
2545 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2546 exit(1);
2547 }
2548#endif
2549 if (!parmp->stdout_isatty)
2550 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2551 if (!input_isatty)
2552 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2553 out_flush();
2554 if (scriptin[0] == NULL)
2555 ui_delay(2000L, TRUE);
2556 TIME_MSG("Warning delay");
2557 }
2558}
2559
2560/*
2561 * Read text from stdin.
2562 */
2563 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002564read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002565{
2566 int i;
2567
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002568#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002569 /* When getting the ATTENTION prompt here, use a dialog */
2570 swap_exists_action = SEA_DIALOG;
2571#endif
2572 no_wait_return = TRUE;
2573 i = msg_didany;
2574 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002575 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002576 no_wait_return = FALSE;
2577 msg_didany = i;
2578 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002579#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002580 check_swap_exists_action();
2581#endif
2582#if !(defined(AMIGA) || defined(MACOS))
2583 /*
2584 * Close stdin and dup it from stderr. Required for GPM to work
2585 * properly, and for running external commands.
2586 * Is there any other system that cannot do this?
2587 */
2588 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002589 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002590#endif
2591}
2592
2593/*
2594 * Create the requested number of windows and edit buffers in them.
2595 * Also does recovery if "recoverymode" set.
2596 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002597 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002598create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002599{
2600#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002601 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002602 int done = 0;
2603
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002604 /*
2605 * Create the number of windows that was requested.
2606 */
2607 if (parmp->window_count == -1) /* was not set */
2608 parmp->window_count = 1;
2609 if (parmp->window_count == 0)
2610 parmp->window_count = GARGCOUNT;
2611 if (parmp->window_count > 1)
2612 {
2613 /* Don't change the windows if there was a command in .vimrc that
2614 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002615 if (parmp->window_layout == 0)
2616 parmp->window_layout = WIN_HOR;
2617 if (parmp->window_layout == WIN_TABS)
2618 {
2619 parmp->window_count = make_tabpages(parmp->window_count);
2620 TIME_MSG("making tab pages");
2621 }
2622 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002623 {
2624 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002625 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002626 TIME_MSG("making windows");
2627 }
2628 else
2629 parmp->window_count = win_count();
2630 }
2631 else
2632 parmp->window_count = 1;
2633#endif
2634
2635 if (recoverymode) /* do recover */
2636 {
2637 msg_scroll = TRUE; /* scroll message up */
2638 ml_recover();
2639 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2640 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002641 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002642 }
2643 else
2644 {
2645 /*
2646 * Open a buffer for windows that don't have one yet.
2647 * Commands in the .vimrc might have loaded a file or split the window.
2648 * Watch out for autocommands that delete a window.
2649 */
2650#ifdef FEAT_AUTOCMD
2651 /*
2652 * Don't execute Win/Buf Enter/Leave autocommands here
2653 */
2654 ++autocmd_no_enter;
2655 ++autocmd_no_leave;
2656#endif
2657#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002658 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002659 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002660 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002661 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002662 {
2663 if (parmp->window_layout == WIN_TABS)
2664 goto_tabpage(1);
2665 else
2666 curwin = firstwin;
2667 }
2668 else if (parmp->window_layout == WIN_TABS)
2669 {
2670 if (curtab->tp_next == NULL)
2671 break;
2672 goto_tabpage(0);
2673 }
2674 else
2675 {
2676 if (curwin->w_next == NULL)
2677 break;
2678 curwin = curwin->w_next;
2679 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002680 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002681#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002682 curbuf = curwin->w_buffer;
2683 if (curbuf->b_ml.ml_mfp == NULL)
2684 {
2685#ifdef FEAT_FOLDING
2686 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2687 if (p_fdls >= 0)
2688 curwin->w_p_fdl = p_fdls;
2689#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002690#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002691 /* When getting the ATTENTION prompt here, use a dialog */
2692 swap_exists_action = SEA_DIALOG;
2693#endif
2694 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002695
2696 /* create memfile, read file */
2697 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002698
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002699#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002700 if (swap_exists_action == SEA_QUIT)
2701 {
2702 if (got_int || only_one_window())
2703 {
2704 /* abort selected or quit and only one window */
2705 did_emsg = FALSE; /* avoid hit-enter prompt */
2706 getout(1);
2707 }
2708 /* We can't close the window, it would disturb what
2709 * happens next. Clear the file name and set the arg
2710 * index to -1 to delete it later. */
2711 setfname(curbuf, NULL, NULL, FALSE);
2712 curwin->w_arg_idx = -1;
2713 swap_exists_action = SEA_NONE;
2714 }
2715 else
2716 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002717#endif
2718#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002719 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002720#endif
2721 }
2722#ifdef FEAT_WINDOWS
2723 ui_breakcheck();
2724 if (got_int)
2725 {
2726 (void)vgetc(); /* only break the file loading, not the rest */
2727 break;
2728 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002729 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002730#endif
2731#ifdef FEAT_WINDOWS
2732 if (parmp->window_layout == WIN_TABS)
2733 goto_tabpage(1);
2734 else
2735 curwin = firstwin;
2736 curbuf = curwin->w_buffer;
2737#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002738#ifdef FEAT_AUTOCMD
2739 --autocmd_no_enter;
2740 --autocmd_no_leave;
2741#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002742 }
2743}
2744
2745#ifdef FEAT_WINDOWS
2746 /*
2747 * If opened more than one window, start editing files in the other
2748 * windows. make_windows() has already opened the windows.
2749 */
2750 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002751edit_buffers(
2752 mparm_T *parmp,
2753 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002754{
2755 int arg_idx; /* index in argument list */
2756 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002757 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002758 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002759
2760# ifdef FEAT_AUTOCMD
2761 /*
2762 * Don't execute Win/Buf Enter/Leave autocommands here
2763 */
2764 ++autocmd_no_enter;
2765 ++autocmd_no_leave;
2766# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002767
2768 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2769 if (curwin->w_arg_idx == -1)
2770 {
2771 win_close(curwin, TRUE);
2772 advance = FALSE;
2773 }
2774
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002775 arg_idx = 1;
2776 for (i = 1; i < parmp->window_count; ++i)
2777 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002778 if (cwd != NULL)
2779 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002780 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2781 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002782 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002783 ++arg_idx;
2784 win_close(curwin, TRUE);
2785 advance = FALSE;
2786 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002787 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002788
Bram Moolenaar84212822006-11-07 21:59:47 +00002789 if (advance)
2790 {
2791 if (parmp->window_layout == WIN_TABS)
2792 {
2793 if (curtab->tp_next == NULL) /* just checking */
2794 break;
2795 goto_tabpage(0);
2796 }
2797 else
2798 {
2799 if (curwin->w_next == NULL) /* just checking */
2800 break;
2801 win_enter(curwin->w_next, FALSE);
2802 }
2803 }
2804 advance = TRUE;
2805
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002806 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002807 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002808 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2809 {
2810 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002811 /* Edit file from arg list, if there is one. When "Quit" selected
2812 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002813# ifdef HAS_SWAP_EXISTS_ACTION
2814 swap_exists_did_quit = FALSE;
2815# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002816 (void)do_ecmd(0, arg_idx < GARGCOUNT
2817 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002818 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002819# ifdef HAS_SWAP_EXISTS_ACTION
2820 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002821 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002822 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002823 if (got_int || only_one_window())
2824 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002825 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002826 did_emsg = FALSE; /* avoid hit-enter prompt */
2827 getout(1);
2828 }
2829 win_close(curwin, TRUE);
2830 advance = FALSE;
2831 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002832# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002833 if (arg_idx == GARGCOUNT - 1)
2834 arg_had_last = TRUE;
2835 ++arg_idx;
2836 }
2837 ui_breakcheck();
2838 if (got_int)
2839 {
2840 (void)vgetc(); /* only break the file loading, not the rest */
2841 break;
2842 }
2843 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002844
2845 if (parmp->window_layout == WIN_TABS)
2846 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002847# ifdef FEAT_AUTOCMD
2848 --autocmd_no_enter;
2849# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002850
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002851 /* make the first window the current window */
2852 win = firstwin;
2853#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2854 /* Avoid making a preview window the current window. */
2855 while (win->w_p_pvw)
2856 {
2857 win = win->w_next;
2858 if (win == NULL)
2859 {
2860 win = firstwin;
2861 break;
2862 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002863 }
2864#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002865 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002866
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002867# ifdef FEAT_AUTOCMD
2868 --autocmd_no_leave;
2869# endif
2870 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002871 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002872 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2873}
2874#endif /* FEAT_WINDOWS */
2875
2876/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002877 * Execute the commands from --cmd arguments "cmds[cnt]".
2878 */
2879 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002880exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002881{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002882 char_u **cmds = parmp->pre_commands;
2883 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002884 int i;
2885
2886 if (cnt > 0)
2887 {
2888 curwin->w_cursor.lnum = 0; /* just in case.. */
2889 sourcing_name = (char_u *)_("pre-vimrc command line");
2890# ifdef FEAT_EVAL
2891 current_SID = SID_CMDARG;
2892# endif
2893 for (i = 0; i < cnt; ++i)
2894 do_cmdline_cmd(cmds[i]);
2895 sourcing_name = NULL;
2896# ifdef FEAT_EVAL
2897 current_SID = 0;
2898# endif
2899 TIME_MSG("--cmd commands");
2900 }
2901}
2902
2903/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002904 * Execute "+", "-c" and "-S" arguments.
2905 */
2906 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002907exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002908{
2909 int i;
2910
2911 /*
2912 * We start commands on line 0, make "vim +/pat file" match a
2913 * pattern on line 1. But don't move the cursor when an autocommand
2914 * with g`" was used.
2915 */
2916 msg_scroll = TRUE;
2917 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2918 curwin->w_cursor.lnum = 0;
2919 sourcing_name = (char_u *)"command line";
2920#ifdef FEAT_EVAL
2921 current_SID = SID_CARG;
2922#endif
2923 for (i = 0; i < parmp->n_commands; ++i)
2924 {
2925 do_cmdline_cmd(parmp->commands[i]);
2926 if (parmp->cmds_tofree[i])
2927 vim_free(parmp->commands[i]);
2928 }
2929 sourcing_name = NULL;
2930#ifdef FEAT_EVAL
2931 current_SID = 0;
2932#endif
2933 if (curwin->w_cursor.lnum == 0)
2934 curwin->w_cursor.lnum = 1;
2935
2936 if (!exmode_active)
2937 msg_scroll = FALSE;
2938
2939#ifdef FEAT_QUICKFIX
2940 /* When started with "-q errorfile" jump to first error again. */
2941 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002942 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002943#endif
2944 TIME_MSG("executing command arguments");
2945}
2946
2947/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002948 * Source startup scripts.
2949 */
2950 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002951source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002952{
2953 int i;
2954
2955 /*
2956 * For "evim" source evim.vim first of all, so that the user can overrule
2957 * any things he doesn't like.
2958 */
2959 if (parmp->evim_mode)
2960 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002961 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002962 TIME_MSG("source evim file");
2963 }
2964
2965 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002966 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002967 * nothing else.
2968 */
2969 if (parmp->use_vimrc != NULL)
2970 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002971 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2972 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002973 {
2974#ifdef FEAT_GUI
2975 if (use_gvimrc == NULL) /* don't load gvimrc either */
2976 use_gvimrc = parmp->use_vimrc;
2977#endif
2978 if (parmp->use_vimrc[2] == 'N')
2979 p_lpl = FALSE; /* don't load plugins either */
2980 }
2981 else
2982 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002983 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002984 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2985 }
2986 }
2987 else if (!silent_mode)
2988 {
2989#ifdef AMIGA
2990 struct Process *proc = (struct Process *)FindTask(0L);
2991 APTR save_winptr = proc->pr_WindowPtr;
2992
2993 /* Avoid a requester here for a volume that doesn't exist. */
2994 proc->pr_WindowPtr = (APTR)-1L;
2995#endif
2996
2997 /*
2998 * Get system wide defaults, if the file name is defined.
2999 */
3000#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003001 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003002#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003003#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003004 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003005#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003006
3007 /*
3008 * Try to read initialization commands from the following places:
3009 * - environment variable VIMINIT
3010 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3011 * - second user vimrc file ($VIM/.vimrc for Dos)
3012 * - environment variable EXINIT
3013 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3014 * - second user exrc file ($VIM/.exrc for Dos)
3015 * The first that exists is used, the rest is ignored.
3016 */
3017 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3018 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003019 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003020#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003021 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3022 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003023#endif
3024#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003025 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3026 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003027#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003028#ifdef USR_VIMRC_FILE4
3029 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3030 DOSO_VIMRC) == FAIL
3031#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003032 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003033 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003034 {
3035#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003036 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003037#endif
3038 }
3039 }
3040
3041 /*
3042 * Read initialization commands from ".vimrc" or ".exrc" in current
3043 * directory. This is only done if the 'exrc' option is set.
3044 * Because of security reasons we disallow shell and write commands
3045 * now, except for unix if the file is owned by the user or 'secure'
3046 * option has been reset in environment of global ".exrc" or ".vimrc".
3047 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3048 * SYS_VIMRC_FILE.
3049 */
3050 if (p_exrc)
3051 {
3052#if defined(UNIX) || defined(VMS)
3053 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3054 if (!file_owned(VIMRC_FILE))
3055#endif
3056 secure = p_secure;
3057
3058 i = FAIL;
3059 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3060 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3061#ifdef USR_VIMRC_FILE2
3062 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3063 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3064#endif
3065#ifdef USR_VIMRC_FILE3
3066 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3067 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3068#endif
3069#ifdef SYS_VIMRC_FILE
3070 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3071 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3072#endif
3073 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003074 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003075
3076 if (i == FAIL)
3077 {
3078#if defined(UNIX) || defined(VMS)
3079 /* if ".exrc" is not owned by user set 'secure' mode */
3080 if (!file_owned(EXRC_FILE))
3081 secure = p_secure;
3082 else
3083 secure = 0;
3084#endif
3085 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3086 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3087#ifdef USR_EXRC_FILE2
3088 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3089 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3090#endif
3091 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003092 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003093 }
3094 }
3095 if (secure == 2)
3096 need_wait_return = TRUE;
3097 secure = 0;
3098#ifdef AMIGA
3099 proc->pr_WindowPtr = save_winptr;
3100#endif
3101 }
3102 TIME_MSG("sourcing vimrc file(s)");
3103}
3104
3105/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003106 * Setup to start using the GUI. Exit with an error when not available.
3107 */
3108 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003109main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003110{
3111#ifdef FEAT_GUI
3112 gui.starting = TRUE; /* start GUI a bit later */
3113#else
3114 mch_errmsg(_(e_nogvim));
3115 mch_errmsg("\n");
3116 mch_exit(2);
3117#endif
3118}
3119
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003120#endif /* NO_VIM_MAIN */
3121
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003122/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003123 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003124 * Returns FAIL if the environment variable was not executed, OK otherwise.
3125 */
3126 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003127process_env(
3128 char_u *env,
3129 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003130{
3131 char_u *initstr;
3132 char_u *save_sourcing_name;
3133 linenr_T save_sourcing_lnum;
3134#ifdef FEAT_EVAL
3135 scid_T save_sid;
3136#endif
3137
3138 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3139 {
3140 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003141 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003142 save_sourcing_name = sourcing_name;
3143 save_sourcing_lnum = sourcing_lnum;
3144 sourcing_name = env;
3145 sourcing_lnum = 0;
3146#ifdef FEAT_EVAL
3147 save_sid = current_SID;
3148 current_SID = SID_ENV;
3149#endif
3150 do_cmdline_cmd(initstr);
3151 sourcing_name = save_sourcing_name;
3152 sourcing_lnum = save_sourcing_lnum;
3153#ifdef FEAT_EVAL
3154 current_SID = save_sid;;
3155#endif
3156 return OK;
3157 }
3158 return FAIL;
3159}
3160
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003161#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003162/*
3163 * Return TRUE if we are certain the user owns the file "fname".
3164 * Used for ".vimrc" and ".exrc".
3165 * Use both stat() and lstat() for extra security.
3166 */
3167 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003168file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003169{
3170 struct stat s;
3171# ifdef UNIX
3172 uid_t uid = getuid();
3173# else /* VMS */
3174 uid_t uid = ((getgid() << 16) | getuid());
3175# endif
3176
3177 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3178# ifdef HAVE_LSTAT
3179 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3180# endif
3181 );
3182}
3183#endif
3184
3185/*
3186 * Give an error message main_errors["n"] and exit.
3187 */
3188 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003189mainerr(
3190 int n, /* one of the ME_ defines */
3191 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003192{
3193#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3194 reset_signals(); /* kill us with CTRL-C here, if you like */
3195#endif
3196
3197 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003198 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003199 mch_errmsg(_(main_errors[n]));
3200 if (str != NULL)
3201 {
3202 mch_errmsg(": \"");
3203 mch_errmsg((char *)str);
3204 mch_errmsg("\"");
3205 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003206 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003207
3208 mch_exit(1);
3209}
3210
3211 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003212mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003213{
3214 mainerr(ME_ARG_MISSING, str);
3215}
3216
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003217#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003218/*
3219 * print a message with three spaces prepended and '\n' appended.
3220 */
3221 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003222main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003223{
3224 mch_msg(" ");
3225 mch_msg(s);
3226 mch_msg("\n");
3227}
3228
3229/*
3230 * Print messages for "vim -h" or "vim --help" and exit.
3231 */
3232 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003233usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003234{
3235 int i;
3236 static char *(use[]) =
3237 {
3238 N_("[file ..] edit specified file(s)"),
3239 N_("- read text from stdin"),
3240 N_("-t tag edit file where tag is defined"),
3241#ifdef FEAT_QUICKFIX
3242 N_("-q [errorfile] edit file with first error")
3243#endif
3244 };
3245
3246#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3247 reset_signals(); /* kill us with CTRL-C here, if you like */
3248#endif
3249
3250 mch_msg(longVersion);
3251 mch_msg(_("\n\nusage:"));
3252 for (i = 0; ; ++i)
3253 {
3254 mch_msg(_(" vim [arguments] "));
3255 mch_msg(_(use[i]));
3256 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3257 break;
3258 mch_msg(_("\n or:"));
3259 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003260#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003261 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003262#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003263
3264 mch_msg(_("\n\nArguments:\n"));
3265 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003266#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003267 main_msg(_("--literal\t\tDon't expand wildcards"));
3268#endif
3269#ifdef FEAT_OLE
3270 main_msg(_("-register\t\tRegister this gvim for OLE"));
3271 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3272#endif
3273#ifdef FEAT_GUI
3274 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3275 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3276#endif
3277 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3278 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003279 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003280 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3281#ifdef FEAT_DIFF
3282 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3283#endif
3284 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3285 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3286 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3287 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3288 main_msg(_("-M\t\t\tModifications in text not allowed"));
3289 main_msg(_("-b\t\t\tBinary mode"));
3290#ifdef FEAT_LISP
3291 main_msg(_("-l\t\t\tLisp mode"));
3292#endif
3293 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3294 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003295 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3296#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003297 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003298#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003299 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3300 main_msg(_("-r\t\t\tList swap files and exit"));
3301 main_msg(_("-r (with file name)\tRecover crashed session"));
3302 main_msg(_("-L\t\t\tSame as -r"));
3303#ifdef AMIGA
3304 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3305 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3306#endif
3307#ifdef FEAT_ARABIC
3308 main_msg(_("-A\t\t\tstart in Arabic mode"));
3309#endif
3310#ifdef FEAT_RIGHTLEFT
3311 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3312#endif
3313#ifdef FEAT_FKMAP
3314 main_msg(_("-F\t\t\tStart in Farsi mode"));
3315#endif
3316 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003317 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003318 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3319#ifdef FEAT_GUI
3320 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3321#endif
3322 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003323#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003324 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003325 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3326 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003327#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003328 main_msg(_("+\t\t\tStart at end of file"));
3329 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003330 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003331 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3332 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3333 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3334 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3335 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3336#ifdef FEAT_CRYPT
3337 main_msg(_("-x\t\t\tEdit encrypted files"));
3338#endif
3339#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3340# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3341 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3342# endif
3343 main_msg(_("-X\t\t\tDo not connect to X server"));
3344#endif
3345#ifdef FEAT_CLIENTSERVER
3346 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3347 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3348 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3349 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003350# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003351 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003352# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003353 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3354 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3355 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3356 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3357#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003358#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003359 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003360#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003361#ifdef FEAT_VIMINFO
3362 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3363#endif
3364 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3365 main_msg(_("--version\t\tPrint version information and exit"));
3366
3367#ifdef FEAT_GUI_X11
3368# ifdef FEAT_GUI_MOTIF
3369 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3370# else
3371# ifdef FEAT_GUI_ATHENA
3372# ifdef FEAT_GUI_NEXTAW
3373 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3374# else
3375 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3376# endif
3377# endif
3378# endif
3379 main_msg(_("-display <display>\tRun vim on <display>"));
3380 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003381 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3382 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3383 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3384 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3385 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3386 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3387 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3388 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3389# ifdef FEAT_GUI_ATHENA
3390 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3391# endif
3392 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3393 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3394 main_msg(_("-xrm <resource>\tSet the specified resource"));
3395#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003396#ifdef FEAT_GUI_GTK
3397 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3398 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3399 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3400 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3401 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003402 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003403 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003404 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003405#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003406#ifdef FEAT_GUI_W32
3407 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003408 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003409#endif
3410
3411#ifdef FEAT_GUI_GNOME
3412 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3413 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003414 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003415 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003416 gui.dofork = FALSE;
3417 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003418 else
3419#endif
3420 mch_exit(0);
3421}
3422
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003423#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003424/*
3425 * Check the result of the ATTENTION dialog:
3426 * When "Quit" selected, exit Vim.
3427 * When "Recover" selected, recover the file.
3428 */
3429 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003430check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003431{
3432 if (swap_exists_action == SEA_QUIT)
3433 getout(1);
3434 handle_swap_exists(NULL);
3435}
3436#endif
3437
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003438#endif
3439
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003440#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003441static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003442
3443static struct timeval prev_timeval;
3444
Bram Moolenaar3f269672009-11-03 11:11:11 +00003445# ifdef WIN3264
3446/*
3447 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3448 */
3449 static int
3450gettimeofday(struct timeval *tv, char *dummy)
3451{
3452 long t = clock();
3453 tv->tv_sec = t / CLOCKS_PER_SEC;
3454 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3455 return 0;
3456}
3457# endif
3458
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003459/*
3460 * Save the previous time before doing something that could nest.
3461 * set "*tv_rel" to the time elapsed so far.
3462 */
3463 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003464time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003465{
3466 *((struct timeval *)tv_rel) = prev_timeval;
3467 gettimeofday(&prev_timeval, NULL);
3468 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3469 - ((struct timeval *)tv_rel)->tv_usec;
3470 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3471 - ((struct timeval *)tv_rel)->tv_sec;
3472 if (((struct timeval *)tv_rel)->tv_usec < 0)
3473 {
3474 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3475 --((struct timeval *)tv_rel)->tv_sec;
3476 }
3477 *(struct timeval *)tv_start = prev_timeval;
3478}
3479
3480/*
3481 * Compute the previous time after doing something that could nest.
3482 * Subtract "*tp" from prev_timeval;
3483 * Note: The arguments are (void *) to avoid trouble with systems that don't
3484 * have struct timeval.
3485 */
3486 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003487time_pop(
3488 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003489{
3490 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3491 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3492 if (prev_timeval.tv_usec < 0)
3493 {
3494 prev_timeval.tv_usec += 1000000;
3495 --prev_timeval.tv_sec;
3496 }
3497}
3498
3499 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003500time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003501{
3502 long usec;
3503 long msec;
3504
3505 usec = now->tv_usec - then->tv_usec;
3506 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3507 usec = usec % 1000L;
3508 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3509}
3510
3511 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003512time_msg(
3513 char *mesg,
3514 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003515 (struct timeval *) */
3516{
3517 static struct timeval start;
3518 struct timeval now;
3519
3520 if (time_fd != NULL)
3521 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003522 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003523 {
3524 gettimeofday(&start, NULL);
3525 prev_timeval = start;
3526 fprintf(time_fd, "\n\ntimes in msec\n");
3527 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3528 fprintf(time_fd, " clock elapsed: other lines\n\n");
3529 }
3530 gettimeofday(&now, NULL);
3531 time_diff(&start, &now);
3532 if (((struct timeval *)tv_start) != NULL)
3533 {
3534 fprintf(time_fd, " ");
3535 time_diff(((struct timeval *)tv_start), &now);
3536 }
3537 fprintf(time_fd, " ");
3538 time_diff(&prev_timeval, &now);
3539 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003540 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003541 }
3542}
3543
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003544#endif
3545
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003546#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003547
3548/*
3549 * Common code for the X command server and the Win32 command server.
3550 */
3551
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003552static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003553
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003554/*
3555 * Do the client-server stuff, unless "--servername ''" was used.
3556 */
3557 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003558exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003559{
3560 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3561 {
3562# ifdef WIN32
3563 /* Initialise the client/server messaging infrastructure. */
3564 serverInitMessaging();
3565# endif
3566
3567 /*
3568 * When a command server argument was found, execute it. This may
3569 * exit Vim when it was successful. Otherwise it's executed further
3570 * on. Remember the encoding used here in "serverStrEnc".
3571 */
3572 if (parmp->serverArg)
3573 {
3574 cmdsrv_main(&parmp->argc, parmp->argv,
3575 parmp->serverName_arg, &parmp->serverStr);
3576# ifdef FEAT_MBYTE
3577 parmp->serverStrEnc = vim_strsave(p_enc);
3578# endif
3579 }
3580
3581 /* If we're still running, get the name to register ourselves.
3582 * On Win32 can register right now, for X11 need to setup the
3583 * clipboard first, it's further down. */
3584 parmp->servername = serverMakeName(parmp->serverName_arg,
3585 parmp->argv[0]);
3586# ifdef WIN32
3587 if (parmp->servername != NULL)
3588 {
3589 serverSetName(parmp->servername);
3590 vim_free(parmp->servername);
3591 }
3592# endif
3593 }
3594}
3595
3596/*
3597 * Prepare for running as a Vim server.
3598 */
3599 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003600prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003601{
3602# if defined(FEAT_X11)
3603 /*
3604 * Register for remote command execution with :serversend and --remote
3605 * unless there was a -X or a --servername '' on the command line.
3606 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003607 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003608 */
3609 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3610# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003611 (gui.in_use
3612# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003613 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003614# endif
3615 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003616# endif
3617 parmp->serverName_arg != NULL))
3618 {
3619 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3620 vim_free(parmp->servername);
3621 TIME_MSG("register server name");
3622 }
3623 else
3624 serverDelayedStartName = parmp->servername;
3625# endif
3626
3627 /*
3628 * Execute command ourselves if we're here because the send failed (or
3629 * else we would have exited above).
3630 */
3631 if (parmp->serverStr != NULL)
3632 {
3633 char_u *p;
3634
3635 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3636 parmp->serverStr, &p));
3637 vim_free(p);
3638 }
3639}
3640
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003641 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003642cmdsrv_main(
3643 int *argc,
3644 char **argv,
3645 char_u *serverName_arg,
3646 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003647{
3648 char_u *res;
3649 int i;
3650 char_u *sname;
3651 int ret;
3652 int didone = FALSE;
3653 int exiterr = 0;
3654 char **newArgV = argv + 1;
3655 int newArgC = 1,
3656 Argc = *argc;
3657 int argtype;
3658#define ARGTYPE_OTHER 0
3659#define ARGTYPE_EDIT 1
3660#define ARGTYPE_EDIT_WAIT 2
3661#define ARGTYPE_SEND 3
3662 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003663 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003664# ifndef FEAT_X11
3665 HWND srv;
3666# else
3667 Window srv;
3668
3669 setup_term_clip();
3670# endif
3671
3672 sname = serverMakeName(serverName_arg, argv[0]);
3673 if (sname == NULL)
3674 return;
3675
3676 /*
3677 * Execute the command server related arguments and remove them
3678 * from the argc/argv array; We may have to return into main()
3679 */
3680 for (i = 1; i < Argc; i++)
3681 {
3682 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003683 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003684 {
3685 for (; i < *argc; i++)
3686 {
3687 *newArgV++ = argv[i];
3688 newArgC++;
3689 }
3690 break;
3691 }
3692
Bram Moolenaareb94e552006-03-11 21:35:11 +00003693 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003694 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003695 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3696 {
3697 char *p = argv[i] + 8;
3698
3699 argtype = ARGTYPE_EDIT;
3700 while (*p != NUL)
3701 {
3702 if (STRNICMP(p, "-wait", 5) == 0)
3703 {
3704 argtype = ARGTYPE_EDIT_WAIT;
3705 p += 5;
3706 }
3707 else if (STRNICMP(p, "-silent", 7) == 0)
3708 {
3709 silent = TRUE;
3710 p += 7;
3711 }
3712 else if (STRNICMP(p, "-tab", 4) == 0)
3713 {
3714 tabs = TRUE;
3715 p += 4;
3716 }
3717 else
3718 {
3719 argtype = ARGTYPE_OTHER;
3720 break;
3721 }
3722 }
3723 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003724 else
3725 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003726
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003727 if (argtype != ARGTYPE_OTHER)
3728 {
3729 if (i == *argc - 1)
3730 mainerr_arg_missing((char_u *)argv[i]);
3731 if (argtype == ARGTYPE_SEND)
3732 {
3733 *serverStr = (char_u *)argv[i + 1];
3734 i++;
3735 }
3736 else
3737 {
3738 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003739 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003740 if (*serverStr == NULL)
3741 {
3742 /* Probably out of memory, exit. */
3743 didone = TRUE;
3744 exiterr = 1;
3745 break;
3746 }
3747 Argc = i;
3748 }
3749# ifdef FEAT_X11
3750 if (xterm_dpy == NULL)
3751 {
3752 mch_errmsg(_("No display"));
3753 ret = -1;
3754 }
3755 else
3756 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3757 NULL, &srv, 0, 0, silent);
3758# else
3759 /* Win32 always works? */
3760 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3761# endif
3762 if (ret < 0)
3763 {
3764 if (argtype == ARGTYPE_SEND)
3765 {
3766 /* Failed to send, abort. */
3767 mch_errmsg(_(": Send failed.\n"));
3768 didone = TRUE;
3769 exiterr = 1;
3770 }
3771 else if (!silent)
3772 /* Let vim start normally. */
3773 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3774 break;
3775 }
3776
3777# ifdef FEAT_GUI_W32
3778 /* Guess that when the server name starts with "g" it's a GUI
3779 * server, which we can bring to the foreground here.
3780 * Foreground() in the server doesn't work very well. */
3781 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3782 SetForegroundWindow(srv);
3783# endif
3784
3785 /*
3786 * For --remote-wait: Wait until the server did edit each
3787 * file. Also detect that the server no longer runs.
3788 */
3789 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3790 {
3791 int numFiles = *argc - i - 1;
3792 int j;
3793 char_u *done = alloc(numFiles);
3794 char_u *p;
3795# ifdef FEAT_GUI_W32
3796 NOTIFYICONDATA ni;
3797 int count = 0;
3798 extern HWND message_window;
3799# endif
3800
3801 if (numFiles > 0 && argv[i + 1][0] == '+')
3802 /* Skip "+cmd" argument, don't wait for it to be edited. */
3803 --numFiles;
3804
3805# ifdef FEAT_GUI_W32
3806 ni.cbSize = sizeof(ni);
3807 ni.hWnd = message_window;
3808 ni.uID = 0;
3809 ni.uFlags = NIF_ICON|NIF_TIP;
3810 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3811 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3812 Shell_NotifyIcon(NIM_ADD, &ni);
3813# endif
3814
3815 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003816 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003817 while (memchr(done, 0, numFiles) != NULL)
3818 {
3819# ifdef WIN32
3820 p = serverGetReply(srv, NULL, TRUE, TRUE);
3821 if (p == NULL)
3822 break;
3823# else
3824 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3825 break;
3826# endif
3827 j = atoi((char *)p);
3828 if (j >= 0 && j < numFiles)
3829 {
3830# ifdef FEAT_GUI_W32
3831 ++count;
3832 sprintf(ni.szTip, _("%d of %d edited"),
3833 count, numFiles);
3834 Shell_NotifyIcon(NIM_MODIFY, &ni);
3835# endif
3836 done[j] = 1;
3837 }
3838 }
3839# ifdef FEAT_GUI_W32
3840 Shell_NotifyIcon(NIM_DELETE, &ni);
3841# endif
3842 }
3843 }
3844 else if (STRICMP(argv[i], "--remote-expr") == 0)
3845 {
3846 if (i == *argc - 1)
3847 mainerr_arg_missing((char_u *)argv[i]);
3848# ifdef WIN32
3849 /* Win32 always works? */
3850 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3851 &res, NULL, 1, FALSE) < 0)
3852# else
3853 if (xterm_dpy == NULL)
3854 mch_errmsg(_("No display: Send expression failed.\n"));
3855 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3856 &res, NULL, 1, 1, FALSE) < 0)
3857# endif
3858 {
3859 if (res != NULL && *res != NUL)
3860 {
3861 /* Output error from remote */
3862 mch_errmsg((char *)res);
3863 vim_free(res);
3864 res = NULL;
3865 }
3866 mch_errmsg(_(": Send expression failed.\n"));
3867 }
3868 }
3869 else if (STRICMP(argv[i], "--serverlist") == 0)
3870 {
3871# ifdef WIN32
3872 /* Win32 always works? */
3873 res = serverGetVimNames();
3874# else
3875 if (xterm_dpy != NULL)
3876 res = serverGetVimNames(xterm_dpy);
3877# endif
3878 if (called_emsg)
3879 mch_errmsg("\n");
3880 }
3881 else if (STRICMP(argv[i], "--servername") == 0)
3882 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003883 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003884 i++;
3885 continue;
3886 }
3887 else
3888 {
3889 *newArgV++ = argv[i];
3890 newArgC++;
3891 continue;
3892 }
3893 didone = TRUE;
3894 if (res != NULL && *res != NUL)
3895 {
3896 mch_msg((char *)res);
3897 if (res[STRLEN(res) - 1] != '\n')
3898 mch_msg("\n");
3899 }
3900 vim_free(res);
3901 }
3902
3903 if (didone)
3904 {
3905 display_errors(); /* display any collected messages */
3906 exit(exiterr); /* Mission accomplished - get out */
3907 }
3908
3909 /* Return back into main() */
3910 *argc = newArgC;
3911 vim_free(sname);
3912}
3913
3914/*
3915 * Build a ":drop" command to send to a Vim server.
3916 */
3917 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003918build_drop_cmd(
3919 int filec,
3920 char **filev,
3921 int tabs, /* Use ":tab drop" instead of ":drop". */
3922 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003923{
3924 garray_T ga;
3925 int i;
3926 char_u *inicmd = NULL;
3927 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003928 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003929 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003930
3931 if (filec > 0 && filev[0][0] == '+')
3932 {
3933 inicmd = (char_u *)filev[0] + 1;
3934 filev++;
3935 filec--;
3936 }
3937 /* Check if we have at least one argument. */
3938 if (filec <= 0)
3939 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003940
3941 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003942 cwd = alloc(MAXPATHL);
3943 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003944 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003945 if (mch_dirname(cwd, MAXPATHL) != OK)
3946 {
3947 vim_free(cwd);
3948 return NULL;
3949 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003950 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003951#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003952 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00003953#else
3954 PATH_ESC_CHARS,
3955#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003956 '\\', TRUE);
3957 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003958 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003959 return NULL;
3960 ga_init2(&ga, 1, 100);
3961 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003962 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003963
3964 /* Call inputsave() so that a prompt for an encryption key works. */
3965 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3966 if (tabs)
3967 ga_concat(&ga, (char_u *)"tab ");
3968 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003969 for (i = 0; i < filec; i++)
3970 {
3971 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003972 * do it again in the Vim server. On MS-Windows only escape
3973 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003974 p = vim_strsave_escaped((char_u *)filev[i],
3975#ifdef UNIX
3976 PATH_ESC_CHARS
3977#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003978 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003979#endif
3980 );
3981 if (p == NULL)
3982 {
3983 vim_free(ga.ga_data);
3984 return NULL;
3985 }
3986 ga_concat(&ga, (char_u *)" ");
3987 ga_concat(&ga, p);
3988 vim_free(p);
3989 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003990 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3991
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003992 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3993 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003994 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3995
3996 /* Switch back to the correct current directory (prior to temporary path
3997 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003998 * correct after the :drop command. With line breaks and spaces:
3999 * if !exists('+acd') || !&acd
4000 * if haslocaldir()
4001 * cd -
4002 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004003 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004004 * cd -
4005 * endif
4006 * endif
4007 */
4008 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004009 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004010 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004011 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004012 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004013
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004014 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004015 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4016 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004017 if (inicmd != NULL)
4018 {
4019 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4020 * the following commands to be inserted as text. Use a "|",
4021 * hopefully "inicmd" does allow this... */
4022 ga_concat(&ga, inicmd);
4023 ga_concat(&ga, (char_u *)"|");
4024 }
4025 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4026 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004027 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004028 ga_append(&ga, NUL);
4029 return ga.ga_data;
4030}
4031
4032/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004033 * Make our basic server name: use the specified "arg" if given, otherwise use
4034 * the tail of the command "cmd" we were started with.
4035 * Return the name in allocated memory. This doesn't include a serial number.
4036 */
4037 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004038serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004039{
4040 char_u *p;
4041
4042 if (arg != NULL && *arg != NUL)
4043 p = vim_strsave_up(arg);
4044 else
4045 {
4046 p = vim_strsave_up(gettail((char_u *)cmd));
4047 /* Remove .exe or .bat from the name. */
4048 if (p != NULL && vim_strchr(p, '.') != NULL)
4049 *vim_strchr(p, '.') = NUL;
4050 }
4051 return p;
4052}
4053#endif /* FEAT_CLIENTSERVER */
4054
4055#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4056/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004057 * Replace termcodes such as <CR> and insert as key presses if there is room.
4058 */
4059 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004060server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004061{
4062 char_u *ptr = NULL;
4063 char_u *cpo_save = p_cpo;
4064
4065 /* Set 'cpoptions' the way we want it.
4066 * B set - backslashes are *not* treated specially
4067 * k set - keycodes are *not* reverse-engineered
4068 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004069 * The last but one parameter of replace_termcodes() is TRUE so that the
4070 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004071 */
4072 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004073 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004074 p_cpo = cpo_save;
4075
4076 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4077 {
4078 /*
4079 * Add the string to the input stream.
4080 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4081 *
4082 * First clear typed characters from the typeahead buffer, there could
4083 * be half a mapping there. Then append to the existing string, so
4084 * that multiple commands from a client are concatenated.
4085 */
4086 if (typebuf.tb_maplen < typebuf.tb_len)
4087 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4088 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4089
4090 /* Let input_available() know we inserted text in the typeahead
4091 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004092 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004093 }
4094 vim_free((char_u *)ptr);
4095}
4096
4097/*
4098 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004099 */
4100 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004101eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004102{
4103 char_u *res;
4104 int save_dbl = debug_break_level;
4105 int save_ro = redir_off;
4106
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004107 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4108 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004109 debug_break_level = -1;
4110 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004111 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4112 * to be typed. Do generate errors so that try/catch works. */
4113 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004114
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004115 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004116
4117 debug_break_level = save_dbl;
4118 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004119 --emsg_silent;
4120 if (emsg_silent < 0)
4121 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004122
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004123 /* A client can tell us to redraw, but not to display the cursor, so do
4124 * that here. */
4125 setcursor();
4126 out_flush();
4127#ifdef FEAT_GUI
4128 if (gui.in_use)
4129 gui_update_cursor(FALSE, FALSE);
4130#endif
4131
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004132 return res;
4133}
4134
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004135/*
4136 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4137 * return an allocated string. Otherwise return "data".
4138 * "*tofree" is set to the result when it needs to be freed later.
4139 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004140 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004141serverConvert(
4142 char_u *client_enc UNUSED,
4143 char_u *data,
4144 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004145{
4146 char_u *res = data;
4147
4148 *tofree = NULL;
4149# ifdef FEAT_MBYTE
4150 if (client_enc != NULL && p_enc != NULL)
4151 {
4152 vimconv_T vimconv;
4153
4154 vimconv.vc_type = CONV_NONE;
4155 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4156 && vimconv.vc_type != CONV_NONE)
4157 {
4158 res = string_convert(&vimconv, data, NULL);
4159 if (res == NULL)
4160 res = data;
4161 else
4162 *tofree = res;
4163 }
4164 convert_setup(&vimconv, NULL, NULL);
4165 }
4166# endif
4167 return res;
4168}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004169#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004170
4171/*
4172 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4173 */
4174#if defined(FEAT_FKMAP) || defined(PROTO)
4175# include "farsi.c"
4176#endif
4177
4178/*
4179 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4180 */
4181#if defined(FEAT_ARABIC) || defined(PROTO)
4182# include "arabic.c"
4183#endif