blob: e8006c30bd3fddc5f6b02d41929302d7f4039046 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002 *
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 Moolenaar97ff9b92016-06-26 20:37:46 +020022#if defined(WIN3264) && !defined(FEAT_GUI_W32)
23# include "iscygpty.h"
24#endif
25
Bram Moolenaarc013cb62005-07-24 21:18:31 +000026/* Values for edit_type. */
27#define EDIT_NONE 0 /* no edit type yet */
28#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
29#define EDIT_STDIN 2 /* read file from stdin */
30#define EDIT_TAG 3 /* tag name argument given, use tagname */
31#define EDIT_QF 4 /* start in quickfix mode */
32
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010033#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static int file_owned(char *fname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +000035#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void mainerr(int, char_u *);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020037# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
38static void init_locale(void);
39# endif
40static void early_arg_scan(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010041#ifndef NO_VIM_MAIN
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void main_msg(char *s);
43static void usage(void);
44static int get_number_arg(char_u *p, int *idx, int def);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void parse_command_name(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010046static void command_line_scan(mparm_T *parmp);
47static void check_tty(mparm_T *parmp);
48static void read_stdin(void);
49static void create_windows(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010050static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010051static void exe_pre_commands(mparm_T *parmp);
52static void exe_commands(mparm_T *parmp);
53static void source_startup_scripts(mparm_T *parmp);
54static void main_start_gui(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010055# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010056static void check_swap_exists_action(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010057# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +010058# ifdef FEAT_EVAL
59static void set_progpath(char_u *argv0);
60# endif
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010061# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010062static void exec_on_server(mparm_T *parmp);
63static void prepare_server(mparm_T *parmp);
64static void cmdsrv_main(int *argc, char **argv, char_u *serverName_arg, char_u **serverStr);
65static char_u *serverMakeName(char_u *arg, char *cmd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010066# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000067#endif
68
69
Bram Moolenaarb4210b32004-06-13 14:51:16 +000070/*
71 * Different types of error messages.
72 */
73static char *(main_errors[]) =
74{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000075 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000076#define ME_UNKNOWN_OPTION 0
77 N_("Too many edit arguments"),
78#define ME_TOO_MANY_ARGS 1
79 N_("Argument missing after"),
80#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +000081 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000082#define ME_GARBAGE 3
83 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
84#define ME_EXTRA_CMD 4
85 N_("Invalid argument for"),
86#define ME_INVALID_ARG 5
87};
88
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010089#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaara6044292017-04-02 18:19:53 +020090
91/* Various parameters passed between main() and other functions. */
92static mparm_T params;
93
Bram Moolenaar8866d272012-11-28 15:55:42 +010094#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020095
96static char_u *start_dir = NULL; /* current working dir on startup */
97
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +020098static int has_dash_c_arg = FALSE;
99
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000100 int
101# ifdef VIMDLL
102_export
103# endif
104# ifdef FEAT_GUI_MSWIN
105# ifdef __BORLANDC__
106_cdecl
107# endif
108VimMain
109# else
110main
111# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100112(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000113{
Bram Moolenaar3f269672009-11-03 11:11:11 +0000114#ifdef STARTUPTIME
115 int i;
116#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000117
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000118 /*
119 * Do any system-specific initialisations. These can NOT use IObuff or
120 * NameBuff. Thus emsg2() cannot be called!
121 */
122 mch_early_init();
123
Bram Moolenaar14993322014-09-09 12:25:33 +0200124#if defined(WIN32) && defined(FEAT_MBYTE)
125 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200126 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200127 * convert when 'encoding' changes. Get the unexpanded arguments.
128 */
129 argc = get_cmd_argsW(&argv);
130#endif
131
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000132 /* Many variables are in "params" so that we can pass them to invoked
133 * functions without a lot of arguments. "argc" and "argv" are also
134 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000135 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000136 params.argc = argc;
137 params.argv = argv;
138 params.want_full_screen = TRUE;
139#ifdef FEAT_EVAL
140 params.use_debug_break_level = -1;
141#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000142 params.window_count = -1;
Bram Moolenaar58d98232005-07-23 22:25:46 +0000143
Bram Moolenaar99685e62013-05-11 13:56:18 +0200144#ifdef FEAT_RUBY
145 {
146 int ruby_stack_start;
147 vim_ruby_init((void *)&ruby_stack_start);
148 }
149#endif
150
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000151#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000152 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000153#endif
154
155#ifdef MEM_PROFILE
156 atexit(vim_mem_profile_dump);
157#endif
158
159#ifdef STARTUPTIME
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200160 /* Need to find "--startuptime" before actually parsing arguments. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000161 for (i = 1; i < argc; ++i)
162 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000163 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000164 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000165 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000166 TIME_MSG("--- VIM STARTING ---");
167 break;
168 }
169 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000170#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000171 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000172
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200173 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000174
175#ifdef FEAT_CLIENTSERVER
176 /*
177 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000178 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000179 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000180 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000181#endif
182
183 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000184 * Figure out the way to work from the command name argv[0].
185 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000186 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000187 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000188
189 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000190 * Process the command line arguments. File names are put in the global
191 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000192 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000193 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000194 TIME_MSG("parsing arguments");
195
196 /*
197 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000198 * there is no terminal version, and on Windows we can't fork one off with
199 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000200 */
201#ifdef ALWAYS_USE_GUI
202 gui.starting = TRUE;
203#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000204# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000205 /*
206 * Check if the GUI can be started. Reset gui.starting if not.
207 * Don't know about other systems, stay on the safe side and don't check.
208 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000209 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000210 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000211 if (gui_init_check() == FAIL)
212 {
213 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000214
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000215 /* When running "evim" or "gvim -y" we need the menus, exit if we
216 * don't have them. */
217 if (params.evim_mode)
218 mch_exit(1);
219 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000220 }
221# endif
222#endif
223
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000224 if (GARGCOUNT > 0)
225 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100226#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000227 /*
228 * Expand wildcards in file names.
229 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000230 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000231 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200232 start_dir = alloc(MAXPATHL);
233 if (start_dir != NULL)
234 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000235 /* Temporarily add '(' and ')' to 'isfname'. These are valid
236 * filename characters but are excluded from 'isfname' to make
237 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
238 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000239 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000240 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200241 if (start_dir != NULL)
242 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000243 }
244#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200245 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000246 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000247
248#if defined(WIN32) && defined(FEAT_MBYTE)
249 {
250 extern void set_alist_count(void);
251
252 /* Remember the number of entries in the argument list. If it changes
253 * we don't react on setting 'encoding'. */
254 set_alist_count();
255 }
256#endif
257
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000258#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000259 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000260 {
261 /*
262 * If there is one filename, fully qualified, we have very probably
263 * been invoked from explorer, so change to the file's directory.
264 * Hint: to avoid this when typing a command use a forward slash.
265 * If the cd fails, it doesn't matter.
266 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200267 (void)vim_chdirfile(params.fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200268 if (start_dir != NULL)
269 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000270 }
271#endif
272 TIME_MSG("expanding arguments");
273
274#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000275 if (params.diff_mode && params.window_count == -1)
276 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000277#endif
278
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000279 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000280 ++RedrawingDisabled;
281
282 /*
283 * When listing swap file names, don't do cursor positioning et. al.
284 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200285 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000286 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000287
288 /*
289 * When certain to start the GUI, don't check capabilities of terminal.
290 * For GTK we can't be sure, but when started from the desktop it doesn't
291 * make sense to try using a terminal.
292 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000293#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000294 if (gui.starting
295# ifdef FEAT_GUI_GTK
296 && !isatty(2)
297# endif
298 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000299 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000300#endif
301
Bram Moolenaard0573012017-10-28 21:11:06 +0200302#if defined(FEAT_GUI_MAC) && defined(MACOS_X_DARWIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000303 /* When the GUI is started from Finder, need to display messages in a
304 * message box. isatty(2) returns TRUE anyway, thus we need to check the
305 * name to know we're not started from a terminal. */
306 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000307 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000308 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000309
310 /* Avoid always using "/" as the current directory. Note that when
311 * started from Finder the arglist will be filled later in
312 * HandleODocAE() and "fname" will be NULL. */
313 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
314 && STRCMP(NameBuff, "/") == 0)
315 {
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200316 if (params.fname != NULL)
317 (void)vim_chdirfile(params.fname);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000318 else
319 {
320 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
321 vim_chdir(NameBuff);
322 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200323 if (start_dir != NULL)
324 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000325 }
326 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000327#endif
328
329 /*
330 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100331 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000332 * Note that we may use mch_exit() before mch_init()!
333 */
334 mch_init();
335 TIME_MSG("shell init");
336
337#ifdef USE_XSMP
338 /*
339 * For want of anywhere else to do it, try to connect to xsmp here.
340 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
341 * Hijacking -X 'no X connection' to also disable XSMP connection as that
342 * has a similar delay upon failure.
343 * Only try if SESSION_MANAGER is set to something non-null.
344 */
345 if (!x_no_connect)
346 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000347 char *p = getenv("SESSION_MANAGER");
348
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000349 if (p != NULL && *p != NUL)
350 {
351 xsmp_init();
352 TIME_MSG("xsmp init");
353 }
354 }
355#endif
356
357 /*
358 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000359 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000360 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000361
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000362 /* This message comes before term inits, but after setting "silent_mode"
363 * when the input is not a tty. */
364 if (GARGCOUNT > 1 && !silent_mode)
365 printf(_("%d files to edit\n"), GARGCOUNT);
366
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000367 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000368 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000369 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000370 capabilities (will set full_screen) */
371 screen_start(); /* don't know where cursor is now */
372 TIME_MSG("Termcap init");
373 }
374
375 /*
376 * Set the default values for the options that use Rows and Columns.
377 */
378 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000379 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000380#ifdef FEAT_DIFF
381 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
382 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000383 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000384 diff_win_options(firstwin, FALSE);
385#endif
386
387 cmdline_row = Rows - p_ch;
388 msg_row = cmdline_row;
389 screenalloc(FALSE); /* allocate screen buffers */
390 set_init_2();
391 TIME_MSG("inits 2");
392
393 msg_scroll = TRUE;
394 no_wait_return = TRUE;
395
396 init_mappings(); /* set up initial mappings */
397
398 init_highlight(TRUE, FALSE); /* set the default highlight groups */
399 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000400
401#ifdef FEAT_EVAL
402 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000403 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000404#endif
405
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200406 /* Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
407 * Allows for setting 'loadplugins' there. */
408 if (params.use_vimrc != NULL
409 && (STRCMP(params.use_vimrc, "NONE") == 0
410 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
411 p_lpl = FALSE;
412
413 /* Execute --cmd arguments. */
414 exe_pre_commands(&params);
415
416 /* Source startup scripts. */
417 source_startup_scripts(&params);
418
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100419#ifdef FEAT_MZSCHEME
420 /*
421 * Newer version of MzScheme (Racket) require earlier (trampolined)
422 * initialisation via scheme_main_setup.
423 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200424 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200425 * Do source startup scripts, so that 'mzschemedll' can be set.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100426 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200427 return mzscheme_main();
428#else
429 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100430#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200431}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100432#endif /* NO_VIM_MAIN */
Bram Moolenaara357e442016-08-10 20:45:07 +0200433#endif /* PROTO */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100434
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200435/*
436 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
437 * things simple.
438 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
439 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100440 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200441vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100442{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100443#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000444#ifdef FEAT_EVAL
445 /*
446 * Read all the plugin files.
447 * Only when compiled with +eval, since most plugins need it.
448 */
449 if (p_lpl)
450 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200451 char_u *rtp_copy = NULL;
452
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200453 /* First add all package directories to 'runtimepath', so that their
454 * autoload directories can be found. Only if not done already with a
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200455 * :packloadall command.
456 * Make a copy of 'runtimepath', so that source_runtime does not use
457 * the pack directories. */
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200458 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200459 {
460 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200461 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200462 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200463
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200464 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy,
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000465# ifdef VMS /* Somehow VMS doesn't handle the "**". */
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200466 (char_u *)"plugin/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000467# else
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200468 (char_u *)"plugin/**/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000469# endif
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200470 DIP_ALL | DIP_NOAFTER);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000471 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200472 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100473
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200474 /* Only source "start" packages if not done already with a :packloadall
475 * command. */
476 if (!did_source_packages)
477 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100478 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200479
480# ifdef VMS /* Somehow VMS doesn't handle the "**". */
481 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_AFTER);
482# else
483 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_AFTER);
484# endif
485 TIME_MSG("loading after plugins");
486
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000487 }
488#endif
489
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000490#ifdef FEAT_DIFF
491 /* Decide about window layout for diff mode after reading vimrc. */
492 if (params.diff_mode && params.window_layout == 0)
493 {
494 if (diffopt_horizontal())
495 params.window_layout = WIN_HOR; /* use horizontal split */
496 else
497 params.window_layout = WIN_VER; /* use vertical split */
498 }
499#endif
500
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000501 /*
502 * Recovery mode without a file name: List swap files.
503 * This uses the 'dir' option, therefore it must be after the
504 * initializations.
505 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200506 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000507 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200508 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000509 mch_exit(0);
510 }
511
512 /*
513 * Set a few option defaults after reading .vimrc files:
514 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
515 */
516 set_init_3();
517 TIME_MSG("inits 3");
518
519 /*
520 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
521 * Note that this overrides anything from a vimrc file.
522 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000523 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000524 p_uc = 0;
525
526#ifdef FEAT_FKMAP
527 if (curwin->w_p_rl && p_altkeymap)
528 {
529 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
530# ifdef FEAT_ARABIC
531 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
532# endif
533 p_fkmap = TRUE; /* Set the Farsi keymap mode */
534 }
535#endif
536
537#ifdef FEAT_GUI
538 if (gui.starting)
539 {
540#if defined(UNIX) || defined(VMS)
541 /* When something caused a message from a vimrc script, need to output
542 * an extra newline before the shell prompt. */
543 if (did_emsg || msg_didout)
544 putchar('\n');
545#endif
546
547 gui_start(); /* will set full_screen to TRUE */
548 TIME_MSG("starting GUI");
549
550 /* When running "evim" or "gvim -y" we need the menus, exit if we
551 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000552 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000553 mch_exit(1);
554 }
555#endif
556
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000557#ifdef FEAT_VIMINFO
558 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000559 * Read in registers, history etc, but not marks, from the viminfo file.
560 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000561 */
562 if (*p_viminfo != NUL)
563 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000564 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000565 TIME_MSG("reading viminfo");
566 }
567#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100568#ifdef FEAT_EVAL
569 /* It's better to make v:oldfiles an empty list than NULL. */
570 if (get_vim_var_list(VV_OLDFILES) == NULL)
571 set_vim_var_list(VV_OLDFILES, list_alloc());
572#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000573
574#ifdef FEAT_QUICKFIX
575 /*
576 * "-q errorfile": Load the error file now.
577 * If the error file can't be read, exit before doing anything else.
578 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000579 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000580 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100581 char_u *enc = NULL;
582
583# ifdef FEAT_MBYTE
584 enc = p_menc;
585# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000586 if (params.use_ef != NULL)
587 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000588 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200589 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100590 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000591 {
592 out_char('\n');
593 mch_exit(3);
594 }
595 TIME_MSG("reading errorfile");
596 }
597#endif
598
599 /*
600 * Start putting things on the screen.
601 * Scroll screen down before drawing over it
602 * Clear screen now, so file message will not be cleared.
603 */
604 starting = NO_BUFFERS;
605 no_wait_return = FALSE;
606 if (!exmode_active)
607 msg_scroll = FALSE;
608
609#ifdef FEAT_GUI
610 /*
611 * This seems to be required to make callbacks to be called now, instead
612 * of after things have been put on the screen, which then may be deleted
613 * when getting a resize callback.
614 * For the Mac this handles putting files dropped on the Vim icon to
615 * global_alist.
616 */
617 if (gui.in_use)
618 {
619# ifdef FEAT_SUN_WORKSHOP
620 if (!usingSunWorkShop)
621# endif
622 gui_wait_for_chars(50L);
623 TIME_MSG("GUI delay");
624 }
625#endif
626
627#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
628 qnx_clip_init();
629#endif
630
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200631#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
632 clip_init(TRUE);
633#endif
634
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000635#ifdef FEAT_XCLIPBOARD
636 /* Start using the X clipboard, unless the GUI was started. */
637# ifdef FEAT_GUI
638 if (!gui.in_use)
639# endif
640 {
641 setup_term_clip();
642 TIME_MSG("setup clipboard");
643 }
644#endif
645
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000646#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000647 /* Prepare for being a Vim server. */
648 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000649#endif
650
651 /*
652 * If "-" argument given: Read file from stdin.
653 * Do this before starting Raw mode, because it may change things that the
654 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
655 * are the same terminal: "cat | vim -".
656 * Using autocommands here may cause trouble...
657 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000658 if (params.edit_type == EDIT_STDIN && !recoverymode)
659 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000660
661#if defined(UNIX) || defined(VMS)
662 /* When switching screens and something caused a message from a vimrc
663 * script, need to output an extra newline on exit. */
664 if ((did_emsg || msg_didout) && *T_TI != NUL)
665 newline_on_exit = TRUE;
666#endif
667
668 /*
669 * When done something that is not allowed or error message call
670 * wait_return. This must be done before starttermcap(), because it may
671 * switch to another screen. It must be done after settmode(TMODE_RAW),
672 * because we want to react on a single key stroke.
673 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000674 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000675 */
676 settmode(TMODE_RAW);
677 TIME_MSG("setting raw mode");
678
679 if (need_wait_return || msg_didany)
680 {
681 wait_return(TRUE);
682 TIME_MSG("waiting for return");
683 }
684
685 starttermcap(); /* start termcap if not done by wait_return() */
686 TIME_MSG("start termcap");
687
688#ifdef FEAT_MOUSE
689 setmouse(); /* may start using the mouse */
690#endif
691 if (scroll_region)
692 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000693 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000694
695 /*
696 * Don't clear the screen when starting in Ex mode, unless using the GUI.
697 */
698 if (exmode_active
699#ifdef FEAT_GUI
700 && !gui.in_use
701#endif
702 )
703 must_redraw = CLEAR;
704 else
705 {
706 screenclear(); /* clear screen */
707 TIME_MSG("clearing screen");
708 }
709
710#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000711 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000712 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100713 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200714 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000715 TIME_MSG("getting crypt key");
716 }
717#endif
718
719 no_wait_return = TRUE;
720
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000721 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000722 * Create the requested number of windows and edit buffers in them.
723 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000724 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000725 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000726 TIME_MSG("opening buffers");
727
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000728#ifdef FEAT_EVAL
729 /* clear v:swapcommand */
730 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
731#endif
732
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000733 /* Ex starts at last line of the file */
734 if (exmode_active)
735 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
736
737#ifdef FEAT_AUTOCMD
738 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
739 TIME_MSG("BufEnter autocommands");
740#endif
741 setpcmark();
742
743#ifdef FEAT_QUICKFIX
744 /*
745 * When started with "-q errorfile" jump to first error now.
746 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000747 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000748 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000749 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000750 TIME_MSG("jump to first error");
751 }
752#endif
753
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000754 /*
755 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000756 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000757 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200758 edit_buffers(&params, start_dir);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200759 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000760
761#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000762 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000763 {
764 win_T *wp;
765
766 /* set options in each window for "vimdiff". */
Bram Moolenaar29323592016-07-24 22:04:11 +0200767 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000768 diff_win_options(wp, TRUE);
769 }
770#endif
771
772 /*
773 * Shorten any of the filenames, but only when absolute.
774 */
775 shorten_fnames(FALSE);
776
777 /*
778 * Need to jump to the tag before executing the '-c command'.
779 * Makes "vim -c '/return' -t main" work.
780 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000781 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000782 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000783#if defined(HAS_SWAP_EXISTS_ACTION)
784 swap_exists_did_quit = FALSE;
785#endif
786
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000787 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000788 do_cmdline_cmd(IObuff);
789 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000790
791#if defined(HAS_SWAP_EXISTS_ACTION)
792 /* If the user doesn't want to edit the file then we quit here. */
793 if (swap_exists_did_quit)
794 getout(1);
795#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000796 }
797
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000798 /* Execute any "+", "-c" and "-S" arguments. */
799 if (params.n_commands > 0)
800 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000801
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200802 /* Must come before the may_req_ calls. */
803 starting = 0;
804
Bram Moolenaar976787d2017-06-04 15:45:50 +0200805#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
806 /* Must be done before redrawing, puts a few characters on the screen. */
807 may_req_ambiguous_char_width();
808#endif
809
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000810 RedrawingDisabled = 0;
811 redraw_all_later(NOT_VALID);
812 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000813
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200814 /* 'autochdir' has been postponed */
815 DO_AUTOCHDIR
816
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000817#ifdef FEAT_TERMRESPONSE
818 /* Requesting the termresponse is postponed until here, so that a "-c q"
819 * argument doesn't make it appear in the shell Vim was started from. */
820 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200821
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200822 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000823#endif
824
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000825 /* start in insert mode */
826 if (p_im)
827 need_start_insertmode = TRUE;
828
Bram Moolenaar14735512016-03-26 21:00:08 +0100829#ifdef FEAT_EVAL
830 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
831#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000832#ifdef FEAT_AUTOCMD
833 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
834 TIME_MSG("VimEnter autocommands");
835#endif
836
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200837#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
838 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
839 * done after the clipboard is available and all initial commands that may
840 * modify the 'clipboard' setting have run; i.e. just before entering the
841 * main loop. */
842 {
843 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100844
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200845 adjust_clip_reg(&default_regname);
846 set_reg_var(default_regname);
847 }
848#endif
849
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000850#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
851 /* When a startup script or session file setup for diff'ing and
852 * scrollbind, sync the scrollbind now. */
853 if (curwin->w_p_diff && curwin->w_p_scb)
854 {
855 update_topline();
856 check_scrollbind((linenr_T)0, 0L);
857 TIME_MSG("diff scrollbinding");
858 }
859#endif
860
861#if defined(WIN3264) && !defined(FEAT_GUI_W32)
862 mch_set_winsize_now(); /* Allow winsize changes from now on */
863#endif
864
Bram Moolenaar4033c552017-09-16 20:54:51 +0200865#if defined(FEAT_GUI)
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000866 /* When tab pages were created, may need to update the tab pages line and
867 * scrollbars. This is skipped while creating them. */
868 if (first_tabpage->tp_next != NULL)
869 {
870 out_flush();
871 gui_init_which_components(NULL);
872 gui_update_scrollbars(TRUE);
873 }
874 need_mouse_correct = TRUE;
875#endif
876
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000877 /* If ":startinsert" command used, stuff a dummy command to be able to
878 * call normal_cmd(), which will then start Insert mode. */
879 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000880 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000881
882#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200883 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200884 {
885# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200886# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200887 && !defined(FEAT_GUI_W32)
888 if (gui.in_use)
889 {
890 mch_errmsg(_("netbeans is not supported with this GUI\n"));
891 mch_exit(2);
892 }
893# endif
894# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000895 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200896 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200897 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000898#endif
899
900 TIME_MSG("before starting main loop");
901
902 /*
903 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100904 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000905 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000906
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200907#endif /* NO_VIM_MAIN */
908
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000909 return 0;
910}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000911
912/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200913 * Initialisation shared by main() and some tests.
914 */
915 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200916common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200917{
918
919#ifdef FEAT_MBYTE
920 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
921#endif
922#ifdef FEAT_EVAL
923 eval_init(); /* init global variables */
924#endif
925
926#ifdef __QNXNTO__
927 qnx_init(); /* PhAttach() for clipboard, (and gui) */
928#endif
929
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200930 /* Init the table of Normal mode commands. */
931 init_normal_cmds();
932
933#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
934 make_version(); /* Construct the long version string. */
935#endif
936
937 /*
938 * Allocate space for the generic buffers (needed for set_init_1() and
939 * EMSG2()).
940 */
941 if ((IObuff = alloc(IOSIZE)) == NULL
942 || (NameBuff = alloc(MAXPATHL)) == NULL)
943 mch_exit(0);
944 TIME_MSG("Allocated generic buffers");
945
946#ifdef NBDEBUG
947 /* Wait a moment for debugging NetBeans. Must be after allocating
948 * NameBuff. */
949 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
950 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
951 TIME_MSG("NetBeans debug wait");
952#endif
953
954#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
955 /*
956 * Setup to use the current locale (for ctype() and many other things).
957 * NOTE: Translated messages with encodings other than latin1 will not
958 * work until set_init_1() has been called!
959 */
960 init_locale();
961 TIME_MSG("locale set");
962#endif
963
964#ifdef FEAT_GUI
965 gui.dofork = TRUE; /* default is to use fork() */
966#endif
967
968 /*
969 * Do a first scan of the arguments in "argv[]":
970 * -display or --display
971 * --server...
972 * --socketid
973 * --windowid
974 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200975 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200976
977#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200978 findYourself(paramp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200979#endif
Bram Moolenaard0573012017-10-28 21:11:06 +0200980#if defined(FEAT_GUI)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200981 /* Prepare for possibly starting GUI sometime */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200982 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200983 TIME_MSG("GUI prepared");
984#endif
985
986#ifdef FEAT_CLIPBOARD
987 clip_init(FALSE); /* Initialise clipboard stuff */
988 TIME_MSG("clipboard setup");
989#endif
990
991 /*
992 * Check if we have an interactive window.
993 * On the Amiga: If there is no window, we open one with a newcli command
994 * (needed for :! to * work). mch_check_win() will also handle the -d or
995 * -dev argument.
996 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100997 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200998 TIME_MSG("window checked");
999
1000 /*
1001 * Allocate the first window and buffer.
1002 * Can't do anything without it, exit when it fails.
1003 */
1004 if (win_alloc_first() == FAIL)
1005 mch_exit(0);
1006
1007 init_yank(); /* init yank buffers */
1008
1009 alist_init(&global_alist); /* Init the argument list to empty. */
1010 global_alist.id = 0;
1011
1012 /*
1013 * Set the default values for the options.
1014 * NOTE: Non-latin1 translated messages are working only after this,
1015 * because this is where "has_mbyte" will be set, which is used by
1016 * msg_outtrans_len_attr().
1017 * First find out the home directory, needed to expand "~" in options.
1018 */
1019 init_homedir(); /* find real value of $HOME */
1020 set_init_1();
1021 TIME_MSG("inits 1");
1022
1023#ifdef FEAT_EVAL
1024 set_lang_var(); /* set v:lang and v:ctype */
1025#endif
1026}
1027
1028/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001029 * Return TRUE when the --not-a-term argument was found.
1030 */
1031 int
1032is_not_a_term()
1033{
1034 return params.not_a_term;
1035}
1036
1037/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001038 * Main loop: Execute Normal mode commands until exiting Vim.
1039 * Also used to handle commands in the command-line window, until the window
1040 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001041 * Also used to handle ":visual" command after ":global": execute Normal mode
1042 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001043 */
1044 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001045main_loop(
1046 int cmdwin, /* TRUE when working in the command-line window */
1047 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001048{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001049 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001050 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001051#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001052 /* these are static to avoid a compiler warning */
1053 static linenr_T conceal_old_cursor_line = 0;
1054 static linenr_T conceal_new_cursor_line = 0;
1055 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001056#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001057
1058#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1059 /* Setup to catch a terminating error from the X server. Just ignore
1060 * it, restore the state and continue. This might not always work
1061 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001062 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001064 {
1065 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001066 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001067 got_int = TRUE;
1068 need_wait_return = FALSE;
1069 global_busy = FALSE;
1070 exmode_active = 0;
1071 skip_redraw = FALSE;
1072 RedrawingDisabled = 0;
1073 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001074 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001075# ifdef FEAT_EVAL
1076 emsg_skip = 0;
1077# endif
1078 emsg_off = 0;
1079# ifdef FEAT_MOUSE
1080 setmouse();
1081# endif
1082 settmode(TMODE_RAW);
1083 starttermcap();
1084 scroll_start();
1085 redraw_later_clear();
1086 }
1087#endif
1088
1089 clear_oparg(&oa);
1090 while (!cmdwin
1091#ifdef FEAT_CMDWIN
1092 || cmdwin_result == 0
1093#endif
1094 )
1095 {
1096 if (stuff_empty())
1097 {
1098 did_check_timestamps = FALSE;
1099 if (need_check_timestamps)
1100 check_timestamps(FALSE);
1101 if (need_wait_return) /* if wait_return still needed ... */
1102 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001103 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001104 {
1105 need_start_insertmode = FALSE;
1106 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1107 /* skip the fileinfo message now, because it would be shown
1108 * after insert mode finishes! */
1109 need_fileinfo = FALSE;
1110 }
1111 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001112
1113 /* Reset "got_int" now that we got back to the main loop. Except when
1114 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1115 * the ":g" command.
1116 * For ":g/pat/vi" we reset "got_int" when used once. When used
1117 * a second time we go back to Ex mode and abort the ":g" command. */
1118 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001119 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001120 if (noexmode && global_busy && !exmode_active && previous_got_int)
1121 {
1122 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1123 * used and keep "got_int" set, so that it aborts ":g". */
1124 exmode_active = EXMODE_NORMAL;
1125 State = NORMAL;
1126 }
1127 else if (!global_busy || !exmode_active)
1128 {
1129 if (!quit_more)
1130 (void)vgetc(); /* flush all buffers */
1131 got_int = FALSE;
1132 }
1133 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001134 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001135 else
1136 previous_got_int = FALSE;
1137
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001138 if (!exmode_active)
1139 msg_scroll = FALSE;
1140 quit_more = FALSE;
1141
1142 /*
1143 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1144 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1145 * update cursor and redraw.
1146 */
1147 if (skip_redraw || exmode_active)
1148 skip_redraw = FALSE;
1149 else if (do_redraw || stuff_empty())
1150 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001151# ifdef FEAT_GUI
1152 /* If ui_breakcheck() was used a resize may have been postponed. */
1153 gui_may_resize_shell();
1154# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001155#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001156 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001157 if (!finish_op && (
1158# ifdef FEAT_AUTOCMD
1159 has_cursormoved()
1160# endif
1161# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1162 ||
1163# endif
1164# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001165 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001166# endif
1167 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001168# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001169 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001170# endif
1171 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001172 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001173# ifdef FEAT_AUTOCMD
1174 if (has_cursormoved())
1175 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1176 FALSE, curbuf);
1177# endif
1178# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001179 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001180 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001181# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001182 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001183# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001184 conceal_new_cursor_line = curwin->w_cursor.lnum;
1185 conceal_update_lines = TRUE;
1186 }
1187# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001188# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001189 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001190# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001191 }
1192#endif
1193
Bram Moolenaar186628f2013-03-19 13:33:23 +01001194#ifdef FEAT_AUTOCMD
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001195 /* Trigger TextChanged if b:changedtick differs. */
Bram Moolenaar186628f2013-03-19 13:33:23 +01001196 if (!finish_op && has_textchanged()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001197 && last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001198 {
1199 if (last_changedtick_buf == curbuf)
1200 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1201 FALSE, curbuf);
1202 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001203 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001204 }
1205#endif
1206
Bram Moolenaar33aec762006-01-22 23:30:12 +00001207#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1208 /* Scroll-binding for diff mode may have been postponed until
1209 * here. Avoids doing it for every change. */
1210 if (diff_need_scrollbind)
1211 {
1212 check_scrollbind((linenr_T)0, 0L);
1213 diff_need_scrollbind = FALSE;
1214 }
1215#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001216#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001217 /* Include a closed fold completely in the Visual area. */
1218 foldAdjustVisual();
1219#endif
1220#ifdef FEAT_FOLDING
1221 /*
1222 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1223 * contain the cursor.
1224 * When 'foldopen' is "all", open the fold(s) under the cursor.
1225 * This may mark the window for redrawing.
1226 */
1227 if (hasAnyFolding(curwin) && !char_avail())
1228 {
1229 foldCheckClose();
1230 if (fdo_flags & FDO_ALL)
1231 foldOpenCursor();
1232 }
1233#endif
1234
1235 /*
1236 * Before redrawing, make sure w_topline is correct, and w_leftcol
1237 * if lines don't wrap, and w_skipcol if lines wrap.
1238 */
1239 update_topline();
1240 validate_cursor();
1241
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001242 if (VIsual_active)
1243 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001244 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001245 update_screen(0);
1246 else if (redraw_cmdline || clear_cmdline)
1247 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001248 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001249#ifdef FEAT_TITLE
1250 if (need_maketitle)
1251 maketitle();
1252#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001253#ifdef FEAT_VIMINFO
1254 curbuf->b_last_used = vim_time();
1255#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001256 /* display message after redraw */
1257 if (keep_msg != NULL)
1258 {
1259 char_u *p;
1260
1261 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001262 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1263 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001264 p = keep_msg;
1265 msg_attr(p, keep_msg_attr);
1266 vim_free(p);
1267 }
1268 if (need_fileinfo) /* show file info after redraw */
1269 {
1270 fileinfo(FALSE, TRUE, FALSE);
1271 need_fileinfo = FALSE;
1272 }
1273
1274 emsg_on_display = FALSE; /* can delete error message now */
1275 did_emsg = FALSE;
1276 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001277 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001278 showruler(FALSE);
1279
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001280# if defined(FEAT_CONCEAL)
1281 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001282 && (conceal_old_cursor_line != conceal_new_cursor_line
1283 || conceal_cursor_line(curwin)
1284 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001285 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001286 if (conceal_old_cursor_line != conceal_new_cursor_line
1287 && conceal_old_cursor_line
1288 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001289 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001290 update_single_line(curwin, conceal_new_cursor_line);
1291 curwin->w_valid &= ~VALID_CROW;
1292 }
1293# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001294 setcursor();
1295 cursor_on();
1296
1297 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001298
1299#ifdef STARTUPTIME
1300 /* Now that we have drawn the first screen all the startup stuff
1301 * has been done, close any file for startup messages. */
1302 if (time_fd != NULL)
1303 {
1304 TIME_MSG("first screen update");
1305 TIME_MSG("--- VIM STARTED ---");
1306 fclose(time_fd);
1307 time_fd = NULL;
1308 }
1309#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001310 }
1311#ifdef FEAT_GUI
1312 if (need_mouse_correct)
1313 gui_mouse_correct();
1314#endif
1315
1316 /*
1317 * Update w_curswant if w_set_curswant has been set.
1318 * Postponed until here to avoid computing w_virtcol too often.
1319 */
1320 update_curswant();
1321
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001322#ifdef FEAT_EVAL
1323 /*
1324 * May perform garbage collection when waiting for a character, but
1325 * only at the very toplevel. Otherwise we may be using a List or
1326 * Dict internally somewhere.
1327 * "may_garbage_collect" is reset in vgetc() which is invoked through
1328 * do_exmode() and normal_cmd().
1329 */
1330 may_garbage_collect = (!cmdwin && !noexmode);
1331#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001332 /*
1333 * If we're invoked as ex, do a round of ex commands.
1334 * Otherwise, get and execute a normal mode command.
1335 */
1336 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001337 {
1338 if (noexmode) /* End of ":global/path/visual" commands */
1339 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001340 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001341 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001342 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001343 {
1344#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001345 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001346 && oa.op_type == OP_NOP && oa.regname == NUL
1347 && !VIsual_active)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001348 {
1349 /* If terminal_loop() returns OK we got a key that is handled
Bram Moolenaar6d819742017-08-06 14:57:49 +02001350 * in Normal model. With FAIL we first need to position the
1351 * cursor and the screen needs to be redrawn. */
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001352 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001353 normal_cmd(&oa, TRUE);
1354 }
1355 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001356#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001357 normal_cmd(&oa, TRUE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001358 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001359 }
1360}
1361
1362
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001363#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001364/*
1365 * Exit, but leave behind swap files for modified buffers.
1366 */
1367 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001368getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001369{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001370# if defined(SIGHUP) && defined(SIG_IGN)
1371 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1372 * makes Vim exit and then handling SIGHUP causes various reentrance
1373 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001374 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001375# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001376
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001377 ml_close_notmod(); /* close all not-modified buffers */
1378 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1379 ml_close_all(FALSE); /* close all memfiles, without deleting */
1380 getout(exitval); /* exit Vim properly */
1381}
1382#endif
1383
1384
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001385/*
1386 * Exit properly.
1387 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001388 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001389getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001390{
1391#ifdef FEAT_AUTOCMD
1392 buf_T *buf;
1393 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001394 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001395#endif
1396
1397 exiting = TRUE;
1398
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001399 /* When running in Ex mode an error causes us to exit with a non-zero exit
1400 * code. POSIX requires this, although it's not 100% clear from the
1401 * standard. */
1402 if (exmode_active)
1403 exitval += ex_exitval;
1404
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001405 /* Position the cursor on the last screen line, below all the text */
1406#ifdef FEAT_GUI
1407 if (!gui.in_use)
1408#endif
1409 windgoto((int)Rows - 1, 0);
1410
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001411#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1412 /* Optionally print hashtable efficiency. */
1413 hash_debug_results();
1414#endif
1415
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001416#ifdef FEAT_GUI
1417 msg_didany = FALSE;
1418#endif
1419
1420#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001421 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001422 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001423 /* Trigger BufWinLeave for all windows, but only once per buffer. */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001424 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001425 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001426 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001427 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001428 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001429 if (wp->w_buffer == NULL)
1430 /* Autocmd must have close the buffer already, skip. */
1431 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001432 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001433 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001434 {
1435 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1436 buf->b_fname, FALSE, buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001437 CHANGEDTICK(buf) = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001438 /* start all over, autocommands may mess up the lists */
1439 next_tp = first_tabpage;
1440 break;
1441 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001442 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001443 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001444
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001445 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001446 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001447 if (buf->b_ml.ml_mfp != NULL)
1448 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001449 bufref_T bufref;
1450
1451 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001452 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001453 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001454 if (!bufref_valid(&bufref))
1455 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001456 break;
1457 }
1458 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1459 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001460#endif
1461
1462#ifdef FEAT_VIMINFO
1463 if (*p_viminfo != NUL)
1464 /* Write out the registers, history, marks etc, to the viminfo file */
1465 write_viminfo(NULL, FALSE);
1466#endif
1467
1468#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001469 if (get_vim_var_nr(VV_DYING) <= 1)
1470 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001471#endif
1472
Bram Moolenaar05159a02005-02-26 23:04:13 +00001473#ifdef FEAT_PROFILE
1474 profile_dump();
1475#endif
1476
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001477 if (did_emsg
1478#ifdef FEAT_GUI
1479 || (gui.in_use && msg_didany && p_verbose > 0)
1480#endif
1481 )
1482 {
1483 /* give the user a chance to read the (error) message */
1484 no_wait_return = FALSE;
1485 wait_return(FALSE);
1486 }
1487
1488#ifdef FEAT_AUTOCMD
1489 /* Position the cursor again, the autocommands may have moved it */
1490# ifdef FEAT_GUI
1491 if (!gui.in_use)
1492# endif
1493 windgoto((int)Rows - 1, 0);
1494#endif
1495
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001496#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001497 job_stop_on_exit();
1498#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001499#ifdef FEAT_LUA
1500 lua_end();
1501#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001502#ifdef FEAT_MZSCHEME
1503 mzscheme_end();
1504#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001505#ifdef FEAT_TCL
1506 tcl_end();
1507#endif
1508#ifdef FEAT_RUBY
1509 ruby_end();
1510#endif
1511#ifdef FEAT_PYTHON
1512 python_end();
1513#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001514#ifdef FEAT_PYTHON3
1515 python3_end();
1516#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001517#ifdef FEAT_PERL
1518 perl_end();
1519#endif
1520#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1521 iconv_end();
1522#endif
1523#ifdef FEAT_NETBEANS_INTG
1524 netbeans_end();
1525#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001526#ifdef FEAT_CSCOPE
1527 cs_end();
1528#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001529#ifdef FEAT_EVAL
1530 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001531 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001532#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001533#if defined(WIN32) && defined(FEAT_MBYTE)
1534 free_cmd_argsW();
1535#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001536
1537 mch_exit(exitval);
1538}
1539
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001540#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1541/*
1542 * Setup to use the current locale (for ctype() and many other things).
1543 */
1544 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001545init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001546{
1547 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001548
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001549# ifdef FEAT_GUI_GTK
1550 /* Tell Gtk not to change our locale settings. */
1551 gtk_disable_setlocale();
1552# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001553# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1554 /* Make sure strtod() uses a decimal point, not a comma. */
1555 setlocale(LC_NUMERIC, "C");
1556# endif
1557
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001558# ifdef WIN32
1559 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1560 * text while it's expecting text in the current locale. This call avoids
1561 * that. */
1562 setlocale(LC_CTYPE, "C");
1563# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001564
1565# ifdef FEAT_GETTEXT
1566 {
1567 int mustfree = FALSE;
1568 char_u *p;
1569
1570# ifdef DYNAMIC_GETTEXT
1571 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001572 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001573# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001574 /* expand_env() doesn't work yet, because g_chartab[] is not
1575 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001576 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1577 if (p != NULL && *p != NUL)
1578 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001579 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001580 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1581 }
1582 if (mustfree)
1583 vim_free(p);
1584 textdomain(VIMPACKAGE);
1585 }
1586# endif
1587}
1588#endif
1589
1590/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001591 * Get the name of the display, before gui_prepare() removes it from
1592 * argv[]. Used for the xterm-clipboard display.
1593 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001594 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001595 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001596 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001597early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001598{
Bram Moolenaar03005972008-11-20 13:12:36 +00001599#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1600 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001601 int argc = parmp->argc;
1602 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001603 int i;
1604
1605 for (i = 1; i < argc; i++)
1606 {
1607 if (STRCMP(argv[i], "--") == 0)
1608 break;
1609# ifdef FEAT_XCLIPBOARD
1610 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001611# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001612 || STRICMP(argv[i], "--display") == 0
1613# endif
1614 )
1615 {
1616 if (i == argc - 1)
1617 mainerr_arg_missing((char_u *)argv[i]);
1618 xterm_display = argv[++i];
1619 }
1620# endif
1621# ifdef FEAT_CLIENTSERVER
1622 else if (STRICMP(argv[i], "--servername") == 0)
1623 {
1624 if (i == argc - 1)
1625 mainerr_arg_missing((char_u *)argv[i]);
1626 parmp->serverName_arg = (char_u *)argv[++i];
1627 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001628 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001629 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001630 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001631 {
1632 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001633# ifdef FEAT_GUI
1634 if (strstr(argv[i], "-wait") != 0)
1635 /* don't fork() when starting the GUI to edit files ourself */
1636 gui.dofork = FALSE;
1637# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001638 }
1639# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001640
1641# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1642# ifdef FEAT_GUI_W32
1643 else if (STRICMP(argv[i], "--windowid") == 0)
1644# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001645 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001646# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001647 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001648 long_u id;
1649 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001650
1651 if (i == argc - 1)
1652 mainerr_arg_missing((char_u *)argv[i]);
1653 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001654 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001655 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001656 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001657 if (count != 1)
1658 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1659 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001660# ifdef FEAT_GUI_W32
1661 win_socket_id = id;
1662# else
1663 gtk_socket_id = id;
1664# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001665 i++;
1666 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001667# endif
1668# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001669 else if (STRICMP(argv[i], "--echo-wid") == 0)
1670 echo_wid_arg = TRUE;
1671# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001672# ifndef FEAT_NETBEANS_INTG
1673 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001674 {
1675 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1676 mch_exit(2);
1677 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001678# endif
1679
Bram Moolenaar58d98232005-07-23 22:25:46 +00001680 }
1681#endif
1682}
1683
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001684#ifndef NO_VIM_MAIN
1685/*
1686 * Get a (optional) count for a Vim argument.
1687 */
1688 static int
1689get_number_arg(
1690 char_u *p, /* pointer to argument */
1691 int *idx, /* index in argument, is incremented */
1692 int def) /* default value */
1693{
1694 if (vim_isdigit(p[*idx]))
1695 {
1696 def = atoi((char *)&(p[*idx]));
1697 while (vim_isdigit(p[*idx]))
1698 *idx = *idx + 1;
1699 }
1700 return def;
1701}
1702
1703/*
1704 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1705 * If the executable name starts with "r" we disable shell commands.
1706 * If the next character is "e" we run in Easy mode.
1707 * If the next character is "g" we run the GUI version.
1708 * If the next characters are "view" we start in readonly mode.
1709 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1710 * If the next characters are "ex" we start in Ex mode. If it's followed
1711 * by "im" use improved Ex mode.
1712 */
1713 static void
1714parse_command_name(mparm_T *parmp)
1715{
1716 char_u *initstr;
1717
1718 initstr = gettail((char_u *)parmp->argv[0]);
1719
Bram Moolenaard0573012017-10-28 21:11:06 +02001720#ifdef FEAT_GUI_MAC
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001721 /* An issue has been seen when launching Vim in such a way that
1722 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1723 * executable or a symbolic link of it. Until this issue is resolved
1724 * we prohibit the GUI from being used.
1725 */
1726 if (STRCMP(initstr, parmp->argv[0]) == 0)
1727 disallow_gui = TRUE;
1728
1729 /* TODO: On MacOS X default to gui if argv[0] ends in:
1730 * /Vim.app/Contents/MacOS/Vim */
1731#endif
1732
1733#ifdef FEAT_EVAL
1734 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001735 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001736#endif
1737
1738 if (TOLOWER_ASC(initstr[0]) == 'r')
1739 {
1740 restricted = TRUE;
1741 ++initstr;
1742 }
1743
1744 /* Use evim mode for "evim" and "egvim", not for "editor". */
1745 if (TOLOWER_ASC(initstr[0]) == 'e'
1746 && (TOLOWER_ASC(initstr[1]) == 'v'
1747 || TOLOWER_ASC(initstr[1]) == 'g'))
1748 {
1749#ifdef FEAT_GUI
1750 gui.starting = TRUE;
1751#endif
1752 parmp->evim_mode = TRUE;
1753 ++initstr;
1754 }
1755
1756 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1757 if (TOLOWER_ASC(initstr[0]) == 'g')
1758 {
1759 main_start_gui();
1760#ifdef FEAT_GUI
1761 ++initstr;
1762#endif
1763 }
1764
1765 if (STRNICMP(initstr, "view", 4) == 0)
1766 {
1767 readonlymode = TRUE;
1768 curbuf->b_p_ro = TRUE;
1769 p_uc = 10000; /* don't update very often */
1770 initstr += 4;
1771 }
1772 else if (STRNICMP(initstr, "vim", 3) == 0)
1773 initstr += 3;
1774
1775 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1776 if (STRICMP(initstr, "diff") == 0)
1777 {
1778#ifdef FEAT_DIFF
1779 parmp->diff_mode = TRUE;
1780#else
1781 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1782 mch_errmsg("\n");
1783 mch_exit(2);
1784#endif
1785 }
1786
1787 if (STRNICMP(initstr, "ex", 2) == 0)
1788 {
1789 if (STRNICMP(initstr + 2, "im", 2) == 0)
1790 exmode_active = EXMODE_VIM;
1791 else
1792 exmode_active = EXMODE_NORMAL;
1793 change_compatible(TRUE); /* set 'compatible' */
1794 }
1795}
1796
Bram Moolenaar58d98232005-07-23 22:25:46 +00001797/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001798 * Scan the command line arguments.
1799 */
1800 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001801command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001802{
1803 int argc = parmp->argc;
1804 char **argv = parmp->argv;
1805 int argv_idx; /* index in argv[n][] */
1806 int had_minmin = FALSE; /* found "--" argument */
1807 int want_argument; /* option argument with argument */
1808 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001809 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001810 long n;
1811
1812 --argc;
1813 ++argv;
1814 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1815 while (argc > 0)
1816 {
1817 /*
1818 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1819 */
1820 if (argv[0][0] == '+' && !had_minmin)
1821 {
1822 if (parmp->n_commands >= MAX_ARG_CMDS)
1823 mainerr(ME_EXTRA_CMD, NULL);
1824 argv_idx = -1; /* skip to next argument */
1825 if (argv[0][1] == NUL)
1826 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1827 else
1828 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1829 }
1830
1831 /*
1832 * Optional argument.
1833 */
1834 else if (argv[0][0] == '-' && !had_minmin)
1835 {
1836 want_argument = FALSE;
1837 c = argv[0][argv_idx++];
1838#ifdef VMS
1839 /*
1840 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1841 * and "-/X" as "-X".
1842 */
1843 if (c == '/')
1844 {
1845 c = argv[0][argv_idx++];
1846 c = TOUPPER_ASC(c);
1847 }
1848 else
1849 c = TOLOWER_ASC(c);
1850#endif
1851 switch (c)
1852 {
1853 case NUL: /* "vim -" read from stdin */
1854 /* "ex -" silent mode */
1855 if (exmode_active)
1856 silent_mode = TRUE;
1857 else
1858 {
1859 if (parmp->edit_type != EDIT_NONE)
1860 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1861 parmp->edit_type = EDIT_STDIN;
1862 read_cmd_fd = 2; /* read from stderr instead of stdin */
1863 }
1864 argv_idx = -1; /* skip to next argument */
1865 break;
1866
1867 case '-': /* "--" don't take any more option arguments */
1868 /* "--help" give help message */
1869 /* "--version" give version message */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001870 /* "--clean" clean context */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001871 /* "--literal" take files literally */
1872 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001873 /* "--not-a-term" don't warn for not a term */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001874 /* "--ttyfail" exit if not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001875 /* "--noplugin[s]" skip plugins */
1876 /* "--cmd <cmd>" execute cmd before vimrc */
1877 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1878 usage();
1879 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1880 {
1881 Columns = 80; /* need to init Columns */
1882 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1883 list_version();
1884 msg_putchar('\n');
1885 msg_didout = FALSE;
1886 mch_exit(0);
1887 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001888 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
1889 {
1890 parmp->use_vimrc = (char_u *)"DEFAULTS";
1891 set_option_value((char_u *)"vif", 0L, (char_u *)"NONE", 0);
1892 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001893 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1894 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001895#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001896 parmp->literal = TRUE;
1897#endif
1898 }
1899 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1900 {
1901#ifdef FEAT_GUI
1902 gui.dofork = FALSE; /* don't fork() when starting GUI */
1903#endif
1904 }
1905 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1906 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001907 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1908 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001909 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
1910 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001911 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1912 {
1913 want_argument = TRUE;
1914 argv_idx += 3;
1915 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001916 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1917 {
1918 want_argument = TRUE;
1919 argv_idx += 11;
1920 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001921#ifdef FEAT_CLIENTSERVER
1922 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1923 ; /* already processed -- no arg */
1924 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1925 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1926 {
1927 /* already processed -- snatch the following arg */
1928 if (argc > 1)
1929 {
1930 --argc;
1931 ++argv;
1932 }
1933 }
1934#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001935#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1936# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001937 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001938# else
1939 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1940# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001941 {
1942 /* already processed -- snatch the following arg */
1943 if (argc > 1)
1944 {
1945 --argc;
1946 ++argv;
1947 }
1948 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001949#endif
1950#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001951 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1952 {
1953 /* already processed, skip */
1954 }
1955#endif
1956 else
1957 {
1958 if (argv[0][argv_idx])
1959 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1960 had_minmin = TRUE;
1961 }
1962 if (!want_argument)
1963 argv_idx = -1; /* skip to next argument */
1964 break;
1965
1966 case 'A': /* "-A" start in Arabic mode */
1967#ifdef FEAT_ARABIC
1968 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1969#else
1970 mch_errmsg(_(e_noarabic));
1971 mch_exit(2);
1972#endif
1973 break;
1974
1975 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001976 /* Needs to be effective before expanding file names, because
1977 * for Win32 this makes us edit a shortcut file itself,
1978 * instead of the file it links to. */
1979 set_options_bin(curbuf->b_p_bin, 1, 0);
1980 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001981 break;
1982
1983 case 'C': /* "-C" Compatible */
1984 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02001985 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001986 break;
1987
1988 case 'e': /* "-e" Ex mode */
1989 exmode_active = EXMODE_NORMAL;
1990 break;
1991
1992 case 'E': /* "-E" Improved Ex mode */
1993 exmode_active = EXMODE_VIM;
1994 break;
1995
1996 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1997 window directly, not with newcli */
1998#ifdef FEAT_GUI
1999 gui.dofork = FALSE; /* don't fork() when starting GUI */
2000#endif
2001 break;
2002
2003 case 'g': /* "-g" start GUI */
2004 main_start_gui();
2005 break;
2006
2007 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
2008#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002009 p_fkmap = TRUE;
2010 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002011#else
2012 mch_errmsg(_(e_nofarsi));
2013 mch_exit(2);
2014#endif
2015 break;
2016
2017 case 'h': /* "-h" give help message */
2018#ifdef FEAT_GUI_GNOME
2019 /* Tell usage() to exit for "gvim". */
2020 gui.starting = FALSE;
2021#endif
2022 usage();
2023 break;
2024
2025 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2026#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002027 p_hkmap = TRUE;
2028 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002029#else
2030 mch_errmsg(_(e_nohebrew));
2031 mch_exit(2);
2032#endif
2033 break;
2034
2035 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2036#ifdef FEAT_LISP
2037 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2038 p_sm = TRUE;
2039#endif
2040 break;
2041
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002042 case 'M': /* "-M" no changes or writing of files */
2043 reset_modifiable();
2044 /* FALLTHROUGH */
2045
2046 case 'm': /* "-m" no writing of files */
2047 p_write = FALSE;
2048 break;
2049
2050 case 'y': /* "-y" easy mode */
2051#ifdef FEAT_GUI
2052 gui.starting = TRUE; /* start GUI a bit later */
2053#endif
2054 parmp->evim_mode = TRUE;
2055 break;
2056
2057 case 'N': /* "-N" Nocompatible */
2058 change_compatible(FALSE);
2059 break;
2060
2061 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002062#ifdef FEAT_NETBEANS_INTG
2063 /* checking for "-nb", netbeans parameters */
2064 if (argv[0][argv_idx] == 'b')
2065 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002066 netbeansArg = argv[0];
2067 argv_idx = -1; /* skip to next argument */
2068 }
2069 else
2070#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002071 parmp->no_swap_file = TRUE;
2072 break;
2073
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002074 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002075#ifdef TARGET_API_MAC_OSX
2076 /* For some reason on MacOS X, an argument like:
2077 -psn_0_10223617 is passed in when invoke from Finder
2078 or with the 'open' command */
2079 if (argv[0][argv_idx] == 's')
2080 {
2081 argv_idx = -1; /* bypass full -psn */
2082 main_start_gui();
2083 break;
2084 }
2085#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002086 /* default is 0: open window for each file */
2087 parmp->window_count = get_number_arg((char_u *)argv[0],
2088 &argv_idx, 0);
2089 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002090 break;
2091
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002092 case 'o': /* "-o[N]" open N horizontal split windows */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002093 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002094 parmp->window_count = get_number_arg((char_u *)argv[0],
2095 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002096 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002097 break;
2098
2099 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002100 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002101 parmp->window_count = get_number_arg((char_u *)argv[0],
2102 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002103 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002104 break;
2105
2106#ifdef FEAT_QUICKFIX
2107 case 'q': /* "-q" QuickFix mode */
2108 if (parmp->edit_type != EDIT_NONE)
2109 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2110 parmp->edit_type = EDIT_QF;
2111 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2112 {
2113 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2114 argv_idx = -1;
2115 }
2116 else if (argc > 1) /* "-q {errorfile}" */
2117 want_argument = TRUE;
2118 break;
2119#endif
2120
2121 case 'R': /* "-R" readonly mode */
2122 readonlymode = TRUE;
2123 curbuf->b_p_ro = TRUE;
2124 p_uc = 10000; /* don't update very often */
2125 break;
2126
2127 case 'r': /* "-r" recovery mode */
2128 case 'L': /* "-L" recovery mode */
2129 recoverymode = 1;
2130 break;
2131
2132 case 's':
2133 if (exmode_active) /* "-s" silent (batch) mode */
2134 silent_mode = TRUE;
2135 else /* "-s {scriptin}" read from script file */
2136 want_argument = TRUE;
2137 break;
2138
2139 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2140 if (parmp->edit_type != EDIT_NONE)
2141 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2142 parmp->edit_type = EDIT_TAG;
2143 if (argv[0][argv_idx]) /* "-t{tag}" */
2144 {
2145 parmp->tagname = (char_u *)argv[0] + argv_idx;
2146 argv_idx = -1;
2147 }
2148 else /* "-t {tag}" */
2149 want_argument = TRUE;
2150 break;
2151
2152#ifdef FEAT_EVAL
2153 case 'D': /* "-D" Debugging */
2154 parmp->use_debug_break_level = 9999;
2155 break;
2156#endif
2157#ifdef FEAT_DIFF
2158 case 'd': /* "-d" 'diff' */
2159# ifdef AMIGA
2160 /* check for "-dev {device}" */
2161 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2162 want_argument = TRUE;
2163 else
2164# endif
2165 parmp->diff_mode = TRUE;
2166 break;
2167#endif
2168 case 'V': /* "-V{N}" Verbose level */
2169 /* default is 10: a little bit verbose */
2170 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2171 if (argv[0][argv_idx] != NUL)
2172 {
2173 set_option_value((char_u *)"verbosefile", 0L,
2174 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002175 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002176 }
2177 break;
2178
2179 case 'v': /* "-v" Vi-mode (as if called "vi") */
2180 exmode_active = 0;
2181#ifdef FEAT_GUI
2182 gui.starting = FALSE; /* don't start GUI */
2183#endif
2184 break;
2185
2186 case 'w': /* "-w{number}" set window height */
2187 /* "-w {scriptout}" write to script */
2188 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2189 {
2190 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2191 set_option_value((char_u *)"window", n, NULL, 0);
2192 break;
2193 }
2194 want_argument = TRUE;
2195 break;
2196
2197#ifdef FEAT_CRYPT
2198 case 'x': /* "-x" encrypted reading/writing of files */
2199 parmp->ask_for_key = TRUE;
2200 break;
2201#endif
2202
2203 case 'X': /* "-X" don't connect to X server */
2204#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2205 x_no_connect = TRUE;
2206#endif
2207 break;
2208
2209 case 'Z': /* "-Z" restricted mode */
2210 restricted = TRUE;
2211 break;
2212
2213 case 'c': /* "-c{command}" or "-c {command}" execute
2214 command */
2215 if (argv[0][argv_idx] != NUL)
2216 {
2217 if (parmp->n_commands >= MAX_ARG_CMDS)
2218 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002219 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2220 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002221 argv_idx = -1;
2222 break;
2223 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002224 /* FALLTHROUGH */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002225 case 'S': /* "-S {file}" execute Vim script */
2226 case 'i': /* "-i {viminfo}" use for viminfo */
2227#ifndef FEAT_DIFF
2228 case 'd': /* "-d {device}" device (for Amiga) */
2229#endif
2230 case 'T': /* "-T {terminal}" terminal name */
2231 case 'u': /* "-u {vimrc}" vim inits file */
2232 case 'U': /* "-U {gvimrc}" gvim inits file */
2233 case 'W': /* "-W {scriptout}" overwrite */
2234#ifdef FEAT_GUI_W32
2235 case 'P': /* "-P {parent title}" MDI parent */
2236#endif
2237 want_argument = TRUE;
2238 break;
2239
2240 default:
2241 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2242 }
2243
2244 /*
2245 * Handle option arguments with argument.
2246 */
2247 if (want_argument)
2248 {
2249 /*
2250 * Check for garbage immediately after the option letter.
2251 */
2252 if (argv[0][argv_idx] != NUL)
2253 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2254
2255 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002256 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002257 mainerr_arg_missing((char_u *)argv[0]);
2258 ++argv;
2259 argv_idx = -1;
2260
2261 switch (c)
2262 {
2263 case 'c': /* "-c {command}" execute command */
2264 case 'S': /* "-S {file}" execute Vim script */
2265 if (parmp->n_commands >= MAX_ARG_CMDS)
2266 mainerr(ME_EXTRA_CMD, NULL);
2267 if (c == 'S')
2268 {
2269 char *a;
2270
2271 if (argc < 1)
2272 /* "-S" without argument: use default session file
2273 * name. */
2274 a = SESSION_FILE;
2275 else if (argv[0][0] == '-')
2276 {
2277 /* "-S" followed by another option: use default
2278 * session file name. */
2279 a = SESSION_FILE;
2280 ++argc;
2281 --argv;
2282 }
2283 else
2284 a = argv[0];
2285 p = alloc((unsigned)(STRLEN(a) + 4));
2286 if (p == NULL)
2287 mch_exit(2);
2288 sprintf((char *)p, "so %s", a);
2289 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2290 parmp->commands[parmp->n_commands++] = p;
2291 }
2292 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002293 parmp->commands[parmp->n_commands++] =
2294 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002295 break;
2296
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002297 case '-':
2298 if (argv[-1][2] == 'c')
2299 {
2300 /* "--cmd {command}" execute command */
2301 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2302 mainerr(ME_EXTRA_CMD, NULL);
2303 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002304 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002305 }
2306 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002307 break;
2308
2309 /* case 'd': -d {device} is handled in mch_check_win() for the
2310 * Amiga */
2311
2312#ifdef FEAT_QUICKFIX
2313 case 'q': /* "-q {errorfile}" QuickFix mode */
2314 parmp->use_ef = (char_u *)argv[0];
2315 break;
2316#endif
2317
2318 case 'i': /* "-i {viminfo}" use for viminfo */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002319 set_option_value((char_u *)"vif", 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002320 break;
2321
2322 case 's': /* "-s {scriptin}" read from script file */
2323 if (scriptin[0] != NULL)
2324 {
2325scripterror:
2326 mch_errmsg(_("Attempt to open script file again: \""));
2327 mch_errmsg(argv[-1]);
2328 mch_errmsg(" ");
2329 mch_errmsg(argv[0]);
2330 mch_errmsg("\"\n");
2331 mch_exit(2);
2332 }
2333 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2334 {
2335 mch_errmsg(_("Cannot open for reading: \""));
2336 mch_errmsg(argv[0]);
2337 mch_errmsg("\"\n");
2338 mch_exit(2);
2339 }
2340 if (save_typebuf() == FAIL)
2341 mch_exit(2); /* out of memory */
2342 break;
2343
2344 case 't': /* "-t {tag}" */
2345 parmp->tagname = (char_u *)argv[0];
2346 break;
2347
2348 case 'T': /* "-T {terminal}" terminal name */
2349 /*
2350 * The -T term argument is always available and when
2351 * HAVE_TERMLIB is supported it overrides the environment
2352 * variable TERM.
2353 */
2354#ifdef FEAT_GUI
2355 if (term_is_gui((char_u *)argv[0]))
2356 gui.starting = TRUE; /* start GUI a bit later */
2357 else
2358#endif
2359 parmp->term = (char_u *)argv[0];
2360 break;
2361
2362 case 'u': /* "-u {vimrc}" vim inits file */
2363 parmp->use_vimrc = (char_u *)argv[0];
2364 break;
2365
2366 case 'U': /* "-U {gvimrc}" gvim inits file */
2367#ifdef FEAT_GUI
2368 use_gvimrc = (char_u *)argv[0];
2369#endif
2370 break;
2371
2372 case 'w': /* "-w {nr}" 'window' value */
2373 /* "-w {scriptout}" append to script file */
2374 if (vim_isdigit(*((char_u *)argv[0])))
2375 {
2376 argv_idx = 0;
2377 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2378 set_option_value((char_u *)"window", n, NULL, 0);
2379 argv_idx = -1;
2380 break;
2381 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002382 /* FALLTHROUGH */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002383 case 'W': /* "-W {scriptout}" overwrite script file */
2384 if (scriptout != NULL)
2385 goto scripterror;
2386 if ((scriptout = mch_fopen(argv[0],
2387 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2388 {
2389 mch_errmsg(_("Cannot open for script output: \""));
2390 mch_errmsg(argv[0]);
2391 mch_errmsg("\"\n");
2392 mch_exit(2);
2393 }
2394 break;
2395
2396#ifdef FEAT_GUI_W32
2397 case 'P': /* "-P {parent title}" MDI parent */
2398 gui_mch_set_parent(argv[0]);
2399 break;
2400#endif
2401 }
2402 }
2403 }
2404
2405 /*
2406 * File name argument.
2407 */
2408 else
2409 {
2410 argv_idx = -1; /* skip to next argument */
2411
2412 /* Check for only one type of editing. */
2413 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2414 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2415 parmp->edit_type = EDIT_FILE;
2416
2417#ifdef MSWIN
2418 /* Remember if the argument was a full path before changing
2419 * slashes to backslashes. */
2420 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2421 parmp->full_path = TRUE;
2422#endif
2423
2424 /* Add the file to the global argument list. */
2425 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2426 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2427 mch_exit(2);
2428#ifdef FEAT_DIFF
2429 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2430 && !mch_isdir(alist_name(&GARGLIST[0])))
2431 {
2432 char_u *r;
2433
2434 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2435 if (r != NULL)
2436 {
2437 vim_free(p);
2438 p = r;
2439 }
2440 }
2441#endif
2442#if defined(__CYGWIN32__) && !defined(WIN32)
2443 /*
2444 * If vim is invoked by non-Cygwin tools, convert away any
2445 * DOS paths, so things like .swp files are created correctly.
2446 * Look for evidence of non-Cygwin paths before we bother.
2447 * This is only for when using the Unix files.
2448 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002449 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002450 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002451 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002452
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002453# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002454 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002455# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002456 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002457# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002458 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002459 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002460 if (p == NULL)
2461 mch_exit(2);
2462 }
2463#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002464
2465#ifdef USE_FNAME_CASE
2466 /* Make the case of the file name match the actual file. */
2467 fname_case(p, 0);
2468#endif
2469
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002470 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002471#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002472 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002473#else
2474 2 /* add buffer number now and use curbuf */
2475#endif
2476 );
2477
2478#if defined(FEAT_MBYTE) && defined(WIN32)
2479 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002480 /* Remember this argument has been added to the argument list.
2481 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002482 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002483# ifdef FEAT_DIFF
2484 parmp->diff_mode
2485# else
2486 FALSE
2487# endif
2488 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002489 }
2490#endif
2491 }
2492
2493 /*
2494 * If there are no more letters after the current "-", go to next
2495 * argument. argv_idx is set to -1 when the current argument is to be
2496 * skipped.
2497 */
2498 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2499 {
2500 --argc;
2501 ++argv;
2502 argv_idx = 1;
2503 }
2504 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002505
2506#ifdef FEAT_EVAL
2507 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2508 * one. */
2509 if (parmp->n_commands > 0)
2510 {
2511 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2512 if (p != NULL)
2513 {
2514 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2515 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2516 vim_free(p);
2517 }
2518 }
2519#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002520}
2521
2522/*
2523 * Print a warning if stdout is not a terminal.
2524 * When starting in Ex mode and commands come from a file, set Silent mode.
2525 */
2526 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002527check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002528{
2529 int input_isatty; /* is active input a terminal? */
2530
2531 input_isatty = mch_input_isatty();
2532 if (exmode_active)
2533 {
2534 if (!input_isatty)
2535 silent_mode = TRUE;
2536 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002537 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002538#ifdef FEAT_GUI
2539 /* don't want the delay when started from the desktop */
2540 && !gui.starting
2541#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002542 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002543 {
2544#ifdef NBDEBUG
2545 /*
2546 * This shouldn't be necessary. But if I run netbeans with the log
2547 * output coming to the console and XOpenDisplay fails, I get vim
2548 * trying to start with input/output to my console tty. This fills my
2549 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002550 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002551 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002552 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002553 {
2554 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2555 exit(1);
2556 }
2557#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002558#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2559 if (is_cygpty_used())
2560 {
Bram Moolenaar2a027452017-09-26 19:10:37 +02002561# if defined(FEAT_MBYTE) && defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
2562 && defined(FEAT_GETTEXT)
2563 char *s, *tofree = NULL;
2564
2565 /* Set the encoding of the error message based on $LC_ALL or
2566 * other environment variables instead of 'encoding'.
2567 * Note that the message is shown on a Cygwin terminal (e.g.
2568 * mintty) which encoding is based on $LC_ALL or etc., not the
2569 * current codepage used by normal Win32 console programs. */
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002570 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002571 if (s == NULL)
2572 s = "utf-8"; /* Use "utf-8" by default. */
2573 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2574 vim_free(tofree);
2575# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002576 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2577 exit(1);
2578 }
2579#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002580 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002581 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2582 if (!input_isatty)
2583 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2584 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002585 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2586 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002587 if (scriptin[0] == NULL)
2588 ui_delay(2000L, TRUE);
2589 TIME_MSG("Warning delay");
2590 }
2591}
2592
2593/*
2594 * Read text from stdin.
2595 */
2596 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002597read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002598{
2599 int i;
2600
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002601#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002602 /* When getting the ATTENTION prompt here, use a dialog */
2603 swap_exists_action = SEA_DIALOG;
2604#endif
2605 no_wait_return = TRUE;
2606 i = msg_didany;
2607 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002608 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002609 no_wait_return = FALSE;
2610 msg_didany = i;
2611 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002612#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002613 check_swap_exists_action();
2614#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02002615#if !(defined(AMIGA) || defined(MACOS_X))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002616 /*
2617 * Close stdin and dup it from stderr. Required for GPM to work
2618 * properly, and for running external commands.
2619 * Is there any other system that cannot do this?
2620 */
2621 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002622 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002623#endif
2624}
2625
2626/*
2627 * Create the requested number of windows and edit buffers in them.
2628 * Also does recovery if "recoverymode" set.
2629 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002630 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002631create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002632{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002633 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002634 int done = 0;
2635
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002636 /*
2637 * Create the number of windows that was requested.
2638 */
2639 if (parmp->window_count == -1) /* was not set */
2640 parmp->window_count = 1;
2641 if (parmp->window_count == 0)
2642 parmp->window_count = GARGCOUNT;
2643 if (parmp->window_count > 1)
2644 {
2645 /* Don't change the windows if there was a command in .vimrc that
2646 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002647 if (parmp->window_layout == 0)
2648 parmp->window_layout = WIN_HOR;
2649 if (parmp->window_layout == WIN_TABS)
2650 {
2651 parmp->window_count = make_tabpages(parmp->window_count);
2652 TIME_MSG("making tab pages");
2653 }
2654 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002655 {
2656 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002657 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002658 TIME_MSG("making windows");
2659 }
2660 else
2661 parmp->window_count = win_count();
2662 }
2663 else
2664 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002665
2666 if (recoverymode) /* do recover */
2667 {
2668 msg_scroll = TRUE; /* scroll message up */
2669 ml_recover();
2670 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2671 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002672 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002673 }
2674 else
2675 {
2676 /*
2677 * Open a buffer for windows that don't have one yet.
2678 * Commands in the .vimrc might have loaded a file or split the window.
2679 * Watch out for autocommands that delete a window.
2680 */
2681#ifdef FEAT_AUTOCMD
2682 /*
2683 * Don't execute Win/Buf Enter/Leave autocommands here
2684 */
2685 ++autocmd_no_enter;
2686 ++autocmd_no_leave;
2687#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00002688 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002689 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002690 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002691 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002692 {
2693 if (parmp->window_layout == WIN_TABS)
2694 goto_tabpage(1);
2695 else
2696 curwin = firstwin;
2697 }
2698 else if (parmp->window_layout == WIN_TABS)
2699 {
2700 if (curtab->tp_next == NULL)
2701 break;
2702 goto_tabpage(0);
2703 }
2704 else
2705 {
2706 if (curwin->w_next == NULL)
2707 break;
2708 curwin = curwin->w_next;
2709 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002710 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002711 curbuf = curwin->w_buffer;
2712 if (curbuf->b_ml.ml_mfp == NULL)
2713 {
2714#ifdef FEAT_FOLDING
2715 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2716 if (p_fdls >= 0)
2717 curwin->w_p_fdl = p_fdls;
2718#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002719#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002720 /* When getting the ATTENTION prompt here, use a dialog */
2721 swap_exists_action = SEA_DIALOG;
2722#endif
2723 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002724
2725 /* create memfile, read file */
2726 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002727
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002728#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002729 if (swap_exists_action == SEA_QUIT)
2730 {
2731 if (got_int || only_one_window())
2732 {
2733 /* abort selected or quit and only one window */
2734 did_emsg = FALSE; /* avoid hit-enter prompt */
2735 getout(1);
2736 }
2737 /* We can't close the window, it would disturb what
2738 * happens next. Clear the file name and set the arg
2739 * index to -1 to delete it later. */
2740 setfname(curbuf, NULL, NULL, FALSE);
2741 curwin->w_arg_idx = -1;
2742 swap_exists_action = SEA_NONE;
2743 }
2744 else
2745 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002746#endif
2747#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002748 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002749#endif
2750 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002751 ui_breakcheck();
2752 if (got_int)
2753 {
2754 (void)vgetc(); /* only break the file loading, not the rest */
2755 break;
2756 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002757 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002758 if (parmp->window_layout == WIN_TABS)
2759 goto_tabpage(1);
2760 else
2761 curwin = firstwin;
2762 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002763#ifdef FEAT_AUTOCMD
2764 --autocmd_no_enter;
2765 --autocmd_no_leave;
2766#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002767 }
2768}
2769
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002770 /*
2771 * If opened more than one window, start editing files in the other
2772 * windows. make_windows() has already opened the windows.
2773 */
2774 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002775edit_buffers(
2776 mparm_T *parmp,
2777 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002778{
2779 int arg_idx; /* index in argument list */
2780 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002781 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002782 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002783
2784# ifdef FEAT_AUTOCMD
2785 /*
2786 * Don't execute Win/Buf Enter/Leave autocommands here
2787 */
2788 ++autocmd_no_enter;
2789 ++autocmd_no_leave;
2790# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002791
2792 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2793 if (curwin->w_arg_idx == -1)
2794 {
2795 win_close(curwin, TRUE);
2796 advance = FALSE;
2797 }
2798
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002799 arg_idx = 1;
2800 for (i = 1; i < parmp->window_count; ++i)
2801 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002802 if (cwd != NULL)
2803 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002804 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2805 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002806 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002807 ++arg_idx;
2808 win_close(curwin, TRUE);
2809 advance = FALSE;
2810 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002811 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002812
Bram Moolenaar84212822006-11-07 21:59:47 +00002813 if (advance)
2814 {
2815 if (parmp->window_layout == WIN_TABS)
2816 {
2817 if (curtab->tp_next == NULL) /* just checking */
2818 break;
2819 goto_tabpage(0);
2820 }
2821 else
2822 {
2823 if (curwin->w_next == NULL) /* just checking */
2824 break;
2825 win_enter(curwin->w_next, FALSE);
2826 }
2827 }
2828 advance = TRUE;
2829
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002830 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002831 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002832 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2833 {
2834 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002835 /* Edit file from arg list, if there is one. When "Quit" selected
2836 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002837# ifdef HAS_SWAP_EXISTS_ACTION
2838 swap_exists_did_quit = FALSE;
2839# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002840 (void)do_ecmd(0, arg_idx < GARGCOUNT
2841 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002842 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002843# ifdef HAS_SWAP_EXISTS_ACTION
2844 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002845 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002846 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002847 if (got_int || only_one_window())
2848 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002849 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002850 did_emsg = FALSE; /* avoid hit-enter prompt */
2851 getout(1);
2852 }
2853 win_close(curwin, TRUE);
2854 advance = FALSE;
2855 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002856# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002857 if (arg_idx == GARGCOUNT - 1)
2858 arg_had_last = TRUE;
2859 ++arg_idx;
2860 }
2861 ui_breakcheck();
2862 if (got_int)
2863 {
2864 (void)vgetc(); /* only break the file loading, not the rest */
2865 break;
2866 }
2867 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002868
2869 if (parmp->window_layout == WIN_TABS)
2870 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002871# ifdef FEAT_AUTOCMD
2872 --autocmd_no_enter;
2873# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002874
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002875 /* make the first window the current window */
2876 win = firstwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02002877#if defined(FEAT_QUICKFIX)
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002878 /* Avoid making a preview window the current window. */
2879 while (win->w_p_pvw)
2880 {
2881 win = win->w_next;
2882 if (win == NULL)
2883 {
2884 win = firstwin;
2885 break;
2886 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002887 }
2888#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002889 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002890
Bram Moolenaar4033c552017-09-16 20:54:51 +02002891#ifdef FEAT_AUTOCMD
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002892 --autocmd_no_leave;
Bram Moolenaar4033c552017-09-16 20:54:51 +02002893#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002894 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002895 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002896 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2897}
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002898
2899/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002900 * Execute the commands from --cmd arguments "cmds[cnt]".
2901 */
2902 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002903exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002904{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002905 char_u **cmds = parmp->pre_commands;
2906 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002907 int i;
2908
2909 if (cnt > 0)
2910 {
2911 curwin->w_cursor.lnum = 0; /* just in case.. */
2912 sourcing_name = (char_u *)_("pre-vimrc command line");
2913# ifdef FEAT_EVAL
2914 current_SID = SID_CMDARG;
2915# endif
2916 for (i = 0; i < cnt; ++i)
2917 do_cmdline_cmd(cmds[i]);
2918 sourcing_name = NULL;
2919# ifdef FEAT_EVAL
2920 current_SID = 0;
2921# endif
2922 TIME_MSG("--cmd commands");
2923 }
2924}
2925
2926/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002927 * Execute "+", "-c" and "-S" arguments.
2928 */
2929 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002930exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002931{
2932 int i;
2933
2934 /*
2935 * We start commands on line 0, make "vim +/pat file" match a
2936 * pattern on line 1. But don't move the cursor when an autocommand
2937 * with g`" was used.
2938 */
2939 msg_scroll = TRUE;
2940 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2941 curwin->w_cursor.lnum = 0;
2942 sourcing_name = (char_u *)"command line";
2943#ifdef FEAT_EVAL
2944 current_SID = SID_CARG;
2945#endif
2946 for (i = 0; i < parmp->n_commands; ++i)
2947 {
2948 do_cmdline_cmd(parmp->commands[i]);
2949 if (parmp->cmds_tofree[i])
2950 vim_free(parmp->commands[i]);
2951 }
2952 sourcing_name = NULL;
2953#ifdef FEAT_EVAL
2954 current_SID = 0;
2955#endif
2956 if (curwin->w_cursor.lnum == 0)
2957 curwin->w_cursor.lnum = 1;
2958
2959 if (!exmode_active)
2960 msg_scroll = FALSE;
2961
2962#ifdef FEAT_QUICKFIX
2963 /* When started with "-q errorfile" jump to first error again. */
2964 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002965 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002966#endif
2967 TIME_MSG("executing command arguments");
2968}
2969
2970/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002971 * Source startup scripts.
2972 */
2973 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002974source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002975{
2976 int i;
2977
2978 /*
2979 * For "evim" source evim.vim first of all, so that the user can overrule
2980 * any things he doesn't like.
2981 */
2982 if (parmp->evim_mode)
2983 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002984 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002985 TIME_MSG("source evim file");
2986 }
2987
2988 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002989 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002990 * nothing else.
2991 */
2992 if (parmp->use_vimrc != NULL)
2993 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002994 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
2995 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
2996 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00002997 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002998 {
2999#ifdef FEAT_GUI
3000 if (use_gvimrc == NULL) /* don't load gvimrc either */
3001 use_gvimrc = parmp->use_vimrc;
3002#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003003 }
3004 else
3005 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003006 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003007 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
3008 }
3009 }
3010 else if (!silent_mode)
3011 {
3012#ifdef AMIGA
3013 struct Process *proc = (struct Process *)FindTask(0L);
3014 APTR save_winptr = proc->pr_WindowPtr;
3015
3016 /* Avoid a requester here for a volume that doesn't exist. */
3017 proc->pr_WindowPtr = (APTR)-1L;
3018#endif
3019
3020 /*
3021 * Get system wide defaults, if the file name is defined.
3022 */
3023#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003024 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003025#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003026#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003027 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003028#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003029
3030 /*
3031 * Try to read initialization commands from the following places:
3032 * - environment variable VIMINIT
3033 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3034 * - second user vimrc file ($VIM/.vimrc for Dos)
3035 * - environment variable EXINIT
3036 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3037 * - second user exrc file ($VIM/.exrc for Dos)
3038 * The first that exists is used, the rest is ignored.
3039 */
3040 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3041 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003042 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003043#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003044 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3045 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003046#endif
3047#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003048 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3049 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003050#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003051#ifdef USR_VIMRC_FILE4
3052 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3053 DOSO_VIMRC) == FAIL
3054#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003055 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003056 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003057#ifdef USR_EXRC_FILE2
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003058 && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003059#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003060 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003061 {
3062 /* When no .vimrc file was found: source defaults.vim. */
3063 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003064 }
3065 }
3066
3067 /*
3068 * Read initialization commands from ".vimrc" or ".exrc" in current
3069 * directory. This is only done if the 'exrc' option is set.
3070 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003071 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003072 * option has been reset in environment of global ".exrc" or ".vimrc".
3073 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3074 * SYS_VIMRC_FILE.
3075 */
3076 if (p_exrc)
3077 {
3078#if defined(UNIX) || defined(VMS)
3079 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3080 if (!file_owned(VIMRC_FILE))
3081#endif
3082 secure = p_secure;
3083
3084 i = FAIL;
3085 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3086 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3087#ifdef USR_VIMRC_FILE2
3088 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3089 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3090#endif
3091#ifdef USR_VIMRC_FILE3
3092 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3093 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3094#endif
3095#ifdef SYS_VIMRC_FILE
3096 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3097 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3098#endif
3099 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003100 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003101
3102 if (i == FAIL)
3103 {
3104#if defined(UNIX) || defined(VMS)
3105 /* if ".exrc" is not owned by user set 'secure' mode */
3106 if (!file_owned(EXRC_FILE))
3107 secure = p_secure;
3108 else
3109 secure = 0;
3110#endif
3111 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3112 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3113#ifdef USR_EXRC_FILE2
3114 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3115 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3116#endif
3117 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003118 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003119 }
3120 }
3121 if (secure == 2)
3122 need_wait_return = TRUE;
3123 secure = 0;
3124#ifdef AMIGA
3125 proc->pr_WindowPtr = save_winptr;
3126#endif
3127 }
3128 TIME_MSG("sourcing vimrc file(s)");
3129}
3130
3131/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003132 * Setup to start using the GUI. Exit with an error when not available.
3133 */
3134 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003135main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003136{
3137#ifdef FEAT_GUI
3138 gui.starting = TRUE; /* start GUI a bit later */
3139#else
3140 mch_errmsg(_(e_nogvim));
3141 mch_errmsg("\n");
3142 mch_exit(2);
3143#endif
3144}
3145
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003146#endif /* NO_VIM_MAIN */
3147
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003148/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003149 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003150 * Returns FAIL if the environment variable was not executed, OK otherwise.
3151 */
3152 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003153process_env(
3154 char_u *env,
3155 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003156{
3157 char_u *initstr;
3158 char_u *save_sourcing_name;
3159 linenr_T save_sourcing_lnum;
3160#ifdef FEAT_EVAL
3161 scid_T save_sid;
3162#endif
3163
3164 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3165 {
3166 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003167 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003168 save_sourcing_name = sourcing_name;
3169 save_sourcing_lnum = sourcing_lnum;
3170 sourcing_name = env;
3171 sourcing_lnum = 0;
3172#ifdef FEAT_EVAL
3173 save_sid = current_SID;
3174 current_SID = SID_ENV;
3175#endif
3176 do_cmdline_cmd(initstr);
3177 sourcing_name = save_sourcing_name;
3178 sourcing_lnum = save_sourcing_lnum;
3179#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003180 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003181#endif
3182 return OK;
3183 }
3184 return FAIL;
3185}
3186
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003187#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003188/*
3189 * Return TRUE if we are certain the user owns the file "fname".
3190 * Used for ".vimrc" and ".exrc".
3191 * Use both stat() and lstat() for extra security.
3192 */
3193 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003194file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003195{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003196 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003197# ifdef UNIX
3198 uid_t uid = getuid();
3199# else /* VMS */
3200 uid_t uid = ((getgid() << 16) | getuid());
3201# endif
3202
3203 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3204# ifdef HAVE_LSTAT
3205 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3206# endif
3207 );
3208}
3209#endif
3210
3211/*
3212 * Give an error message main_errors["n"] and exit.
3213 */
3214 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003215mainerr(
3216 int n, /* one of the ME_ defines */
3217 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003218{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003219#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003220 reset_signals(); /* kill us with CTRL-C here, if you like */
3221#endif
3222
3223 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003224 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003225 mch_errmsg(_(main_errors[n]));
3226 if (str != NULL)
3227 {
3228 mch_errmsg(": \"");
3229 mch_errmsg((char *)str);
3230 mch_errmsg("\"");
3231 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003232 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003233
3234 mch_exit(1);
3235}
3236
3237 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003238mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003239{
3240 mainerr(ME_ARG_MISSING, str);
3241}
3242
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003243#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003244/*
3245 * print a message with three spaces prepended and '\n' appended.
3246 */
3247 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003248main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003249{
3250 mch_msg(" ");
3251 mch_msg(s);
3252 mch_msg("\n");
3253}
3254
3255/*
3256 * Print messages for "vim -h" or "vim --help" and exit.
3257 */
3258 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003259usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003260{
3261 int i;
3262 static char *(use[]) =
3263 {
3264 N_("[file ..] edit specified file(s)"),
3265 N_("- read text from stdin"),
3266 N_("-t tag edit file where tag is defined"),
3267#ifdef FEAT_QUICKFIX
3268 N_("-q [errorfile] edit file with first error")
3269#endif
3270 };
3271
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003272#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003273 reset_signals(); /* kill us with CTRL-C here, if you like */
3274#endif
3275
3276 mch_msg(longVersion);
3277 mch_msg(_("\n\nusage:"));
3278 for (i = 0; ; ++i)
3279 {
3280 mch_msg(_(" vim [arguments] "));
3281 mch_msg(_(use[i]));
3282 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3283 break;
3284 mch_msg(_("\n or:"));
3285 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003286#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003287 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003288#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003289
3290 mch_msg(_("\n\nArguments:\n"));
3291 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003292#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003293 main_msg(_("--literal\t\tDon't expand wildcards"));
3294#endif
3295#ifdef FEAT_OLE
3296 main_msg(_("-register\t\tRegister this gvim for OLE"));
3297 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3298#endif
3299#ifdef FEAT_GUI
3300 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3301 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3302#endif
3303 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3304 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003305 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003306 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3307#ifdef FEAT_DIFF
3308 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3309#endif
3310 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3311 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3312 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3313 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3314 main_msg(_("-M\t\t\tModifications in text not allowed"));
3315 main_msg(_("-b\t\t\tBinary mode"));
3316#ifdef FEAT_LISP
3317 main_msg(_("-l\t\t\tLisp mode"));
3318#endif
3319 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3320 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003321 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3322#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003323 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003324#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003325 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3326 main_msg(_("-r\t\t\tList swap files and exit"));
3327 main_msg(_("-r (with file name)\tRecover crashed session"));
3328 main_msg(_("-L\t\t\tSame as -r"));
3329#ifdef AMIGA
3330 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3331 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3332#endif
3333#ifdef FEAT_ARABIC
3334 main_msg(_("-A\t\t\tstart in Arabic mode"));
3335#endif
3336#ifdef FEAT_RIGHTLEFT
3337 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3338#endif
3339#ifdef FEAT_FKMAP
3340 main_msg(_("-F\t\t\tStart in Farsi mode"));
3341#endif
3342 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003343 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003344 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003345 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3346#ifdef FEAT_GUI
3347 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3348#endif
3349 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003350 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003351 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3352 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3353 main_msg(_("+\t\t\tStart at end of file"));
3354 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003355 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003356 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3357 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3358 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3359 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3360 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3361#ifdef FEAT_CRYPT
3362 main_msg(_("-x\t\t\tEdit encrypted files"));
3363#endif
3364#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3365# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3366 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3367# endif
3368 main_msg(_("-X\t\t\tDo not connect to X server"));
3369#endif
3370#ifdef FEAT_CLIENTSERVER
3371 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3372 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3373 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3374 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003375 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003376 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3377 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3378 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3379 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3380#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003381#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003382 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003383#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003384#ifdef FEAT_VIMINFO
3385 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3386#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003387 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003388 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3389 main_msg(_("--version\t\tPrint version information and exit"));
3390
3391#ifdef FEAT_GUI_X11
3392# ifdef FEAT_GUI_MOTIF
3393 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3394# else
3395# ifdef FEAT_GUI_ATHENA
3396# ifdef FEAT_GUI_NEXTAW
3397 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3398# else
3399 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3400# endif
3401# endif
3402# endif
3403 main_msg(_("-display <display>\tRun vim on <display>"));
3404 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003405 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3406 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3407 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3408 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3409 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3410 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3411 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3412 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3413# ifdef FEAT_GUI_ATHENA
3414 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3415# endif
3416 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3417 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3418 main_msg(_("-xrm <resource>\tSet the specified resource"));
3419#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003420#ifdef FEAT_GUI_GTK
3421 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3422 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3423 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3424 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3425 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003426 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003427 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003428 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003429#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003430#ifdef FEAT_GUI_W32
3431 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003432 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003433#endif
3434
3435#ifdef FEAT_GUI_GNOME
3436 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3437 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003438 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003439 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003440 gui.dofork = FALSE;
3441 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003442 else
3443#endif
3444 mch_exit(0);
3445}
3446
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003447#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003448/*
3449 * Check the result of the ATTENTION dialog:
3450 * When "Quit" selected, exit Vim.
3451 * When "Recover" selected, recover the file.
3452 */
3453 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003454check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003455{
3456 if (swap_exists_action == SEA_QUIT)
3457 getout(1);
3458 handle_swap_exists(NULL);
3459}
3460#endif
3461
Bram Moolenaar08cab962017-03-04 14:37:18 +01003462#endif /* NO_VIM_MAIN */
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003463
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003464#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003465static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003466
3467static struct timeval prev_timeval;
3468
Bram Moolenaar3f269672009-11-03 11:11:11 +00003469# ifdef WIN3264
3470/*
3471 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3472 */
3473 static int
3474gettimeofday(struct timeval *tv, char *dummy)
3475{
3476 long t = clock();
3477 tv->tv_sec = t / CLOCKS_PER_SEC;
3478 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3479 return 0;
3480}
3481# endif
3482
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003483/*
3484 * Save the previous time before doing something that could nest.
3485 * set "*tv_rel" to the time elapsed so far.
3486 */
3487 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003488time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003489{
3490 *((struct timeval *)tv_rel) = prev_timeval;
3491 gettimeofday(&prev_timeval, NULL);
3492 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3493 - ((struct timeval *)tv_rel)->tv_usec;
3494 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3495 - ((struct timeval *)tv_rel)->tv_sec;
3496 if (((struct timeval *)tv_rel)->tv_usec < 0)
3497 {
3498 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3499 --((struct timeval *)tv_rel)->tv_sec;
3500 }
3501 *(struct timeval *)tv_start = prev_timeval;
3502}
3503
3504/*
3505 * Compute the previous time after doing something that could nest.
3506 * Subtract "*tp" from prev_timeval;
3507 * Note: The arguments are (void *) to avoid trouble with systems that don't
3508 * have struct timeval.
3509 */
3510 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003511time_pop(
3512 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003513{
3514 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3515 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3516 if (prev_timeval.tv_usec < 0)
3517 {
3518 prev_timeval.tv_usec += 1000000;
3519 --prev_timeval.tv_sec;
3520 }
3521}
3522
3523 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003524time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003525{
3526 long usec;
3527 long msec;
3528
3529 usec = now->tv_usec - then->tv_usec;
3530 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3531 usec = usec % 1000L;
3532 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3533}
3534
3535 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003536time_msg(
3537 char *mesg,
3538 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003539 (struct timeval *) */
3540{
3541 static struct timeval start;
3542 struct timeval now;
3543
3544 if (time_fd != NULL)
3545 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003546 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003547 {
3548 gettimeofday(&start, NULL);
3549 prev_timeval = start;
3550 fprintf(time_fd, "\n\ntimes in msec\n");
3551 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3552 fprintf(time_fd, " clock elapsed: other lines\n\n");
3553 }
3554 gettimeofday(&now, NULL);
3555 time_diff(&start, &now);
3556 if (((struct timeval *)tv_start) != NULL)
3557 {
3558 fprintf(time_fd, " ");
3559 time_diff(((struct timeval *)tv_start), &now);
3560 }
3561 fprintf(time_fd, " ");
3562 time_diff(&prev_timeval, &now);
3563 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003564 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003565 }
3566}
3567
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003568#endif
3569
Bram Moolenaar595297d2017-03-04 19:11:12 +01003570#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003571 static void
3572set_progpath(char_u *argv0)
3573{
3574 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003575
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003576# if defined(WIN32)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003577 /* A relative path containing a "/" will become invalid when using ":cd",
3578 * turn it into a full path.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003579 * On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3580 * this. */
Bram Moolenaar066029e2017-03-05 15:19:32 +01003581 char_u *path = NULL;
3582
3583 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3584 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003585# else
3586 char_u buf[MAXPATHL + 1];
3587# ifdef PROC_EXE_LINK
3588 char linkbuf[MAXPATHL + 1];
3589 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003590
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003591 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3592 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003593 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003594 linkbuf[len] = NUL;
3595 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003596 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003597# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003598
3599 if (!mch_isFullName(val))
3600 {
3601 if (gettail(val) != val
3602 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3603 val = buf;
3604 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003605# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003606
Bram Moolenaar08cab962017-03-04 14:37:18 +01003607 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003608
Bram Moolenaar066029e2017-03-05 15:19:32 +01003609# ifdef WIN32
Bram Moolenaar43663192017-03-05 14:29:12 +01003610 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003611# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003612}
3613
3614#endif /* NO_VIM_MAIN */
3615
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003616#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003617
3618/*
3619 * Common code for the X command server and the Win32 command server.
3620 */
3621
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003622static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003623
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003624/*
3625 * Do the client-server stuff, unless "--servername ''" was used.
3626 */
3627 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003628exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003629{
3630 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3631 {
3632# ifdef WIN32
3633 /* Initialise the client/server messaging infrastructure. */
3634 serverInitMessaging();
3635# endif
3636
3637 /*
3638 * When a command server argument was found, execute it. This may
3639 * exit Vim when it was successful. Otherwise it's executed further
3640 * on. Remember the encoding used here in "serverStrEnc".
3641 */
3642 if (parmp->serverArg)
3643 {
3644 cmdsrv_main(&parmp->argc, parmp->argv,
3645 parmp->serverName_arg, &parmp->serverStr);
3646# ifdef FEAT_MBYTE
3647 parmp->serverStrEnc = vim_strsave(p_enc);
3648# endif
3649 }
3650
3651 /* If we're still running, get the name to register ourselves.
3652 * On Win32 can register right now, for X11 need to setup the
3653 * clipboard first, it's further down. */
3654 parmp->servername = serverMakeName(parmp->serverName_arg,
3655 parmp->argv[0]);
3656# ifdef WIN32
3657 if (parmp->servername != NULL)
3658 {
3659 serverSetName(parmp->servername);
3660 vim_free(parmp->servername);
3661 }
3662# endif
3663 }
3664}
3665
3666/*
3667 * Prepare for running as a Vim server.
3668 */
3669 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003670prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003671{
3672# if defined(FEAT_X11)
3673 /*
3674 * Register for remote command execution with :serversend and --remote
3675 * unless there was a -X or a --servername '' on the command line.
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003676 * Only register nongui-vim's with an explicit --servername argument,
3677 * or when compiling with autoservername.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003678 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003679 */
3680 if (X_DISPLAY != NULL && parmp->servername != NULL && (
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003681# if defined(FEAT_AUTOSERVERNAME) || defined(FEAT_GUI)
3682 (
3683# if defined(FEAT_AUTOSERVERNAME)
3684 1
3685# else
3686 gui.in_use
3687# endif
Bram Moolenaar5f402312006-08-15 19:40:35 +00003688# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003689 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003690# endif
3691 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003692# endif
3693 parmp->serverName_arg != NULL))
3694 {
3695 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3696 vim_free(parmp->servername);
3697 TIME_MSG("register server name");
3698 }
3699 else
3700 serverDelayedStartName = parmp->servername;
3701# endif
3702
3703 /*
3704 * Execute command ourselves if we're here because the send failed (or
3705 * else we would have exited above).
3706 */
3707 if (parmp->serverStr != NULL)
3708 {
3709 char_u *p;
3710
3711 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3712 parmp->serverStr, &p));
3713 vim_free(p);
3714 }
3715}
3716
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003717 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003718cmdsrv_main(
3719 int *argc,
3720 char **argv,
3721 char_u *serverName_arg,
3722 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003723{
3724 char_u *res;
3725 int i;
3726 char_u *sname;
3727 int ret;
3728 int didone = FALSE;
3729 int exiterr = 0;
3730 char **newArgV = argv + 1;
3731 int newArgC = 1,
3732 Argc = *argc;
3733 int argtype;
3734#define ARGTYPE_OTHER 0
3735#define ARGTYPE_EDIT 1
3736#define ARGTYPE_EDIT_WAIT 2
3737#define ARGTYPE_SEND 3
3738 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003739 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003740# ifndef FEAT_X11
3741 HWND srv;
3742# else
3743 Window srv;
3744
3745 setup_term_clip();
3746# endif
3747
3748 sname = serverMakeName(serverName_arg, argv[0]);
3749 if (sname == NULL)
3750 return;
3751
3752 /*
3753 * Execute the command server related arguments and remove them
3754 * from the argc/argv array; We may have to return into main()
3755 */
3756 for (i = 1; i < Argc; i++)
3757 {
3758 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003759 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003760 {
3761 for (; i < *argc; i++)
3762 {
3763 *newArgV++ = argv[i];
3764 newArgC++;
3765 }
3766 break;
3767 }
3768
Bram Moolenaareb94e552006-03-11 21:35:11 +00003769 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003770 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003771 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3772 {
3773 char *p = argv[i] + 8;
3774
3775 argtype = ARGTYPE_EDIT;
3776 while (*p != NUL)
3777 {
3778 if (STRNICMP(p, "-wait", 5) == 0)
3779 {
3780 argtype = ARGTYPE_EDIT_WAIT;
3781 p += 5;
3782 }
3783 else if (STRNICMP(p, "-silent", 7) == 0)
3784 {
3785 silent = TRUE;
3786 p += 7;
3787 }
3788 else if (STRNICMP(p, "-tab", 4) == 0)
3789 {
3790 tabs = TRUE;
3791 p += 4;
3792 }
3793 else
3794 {
3795 argtype = ARGTYPE_OTHER;
3796 break;
3797 }
3798 }
3799 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003800 else
3801 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003802
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003803 if (argtype != ARGTYPE_OTHER)
3804 {
3805 if (i == *argc - 1)
3806 mainerr_arg_missing((char_u *)argv[i]);
3807 if (argtype == ARGTYPE_SEND)
3808 {
3809 *serverStr = (char_u *)argv[i + 1];
3810 i++;
3811 }
3812 else
3813 {
3814 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003815 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003816 if (*serverStr == NULL)
3817 {
3818 /* Probably out of memory, exit. */
3819 didone = TRUE;
3820 exiterr = 1;
3821 break;
3822 }
3823 Argc = i;
3824 }
3825# ifdef FEAT_X11
3826 if (xterm_dpy == NULL)
3827 {
3828 mch_errmsg(_("No display"));
3829 ret = -1;
3830 }
3831 else
3832 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003833 NULL, &srv, 0, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003834# else
3835 /* Win32 always works? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003836 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003837# endif
3838 if (ret < 0)
3839 {
3840 if (argtype == ARGTYPE_SEND)
3841 {
3842 /* Failed to send, abort. */
3843 mch_errmsg(_(": Send failed.\n"));
3844 didone = TRUE;
3845 exiterr = 1;
3846 }
3847 else if (!silent)
3848 /* Let vim start normally. */
3849 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3850 break;
3851 }
3852
3853# ifdef FEAT_GUI_W32
3854 /* Guess that when the server name starts with "g" it's a GUI
3855 * server, which we can bring to the foreground here.
3856 * Foreground() in the server doesn't work very well. */
3857 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3858 SetForegroundWindow(srv);
3859# endif
3860
3861 /*
3862 * For --remote-wait: Wait until the server did edit each
3863 * file. Also detect that the server no longer runs.
3864 */
3865 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3866 {
3867 int numFiles = *argc - i - 1;
3868 int j;
3869 char_u *done = alloc(numFiles);
3870 char_u *p;
3871# ifdef FEAT_GUI_W32
3872 NOTIFYICONDATA ni;
3873 int count = 0;
3874 extern HWND message_window;
3875# endif
3876
3877 if (numFiles > 0 && argv[i + 1][0] == '+')
3878 /* Skip "+cmd" argument, don't wait for it to be edited. */
3879 --numFiles;
3880
3881# ifdef FEAT_GUI_W32
3882 ni.cbSize = sizeof(ni);
3883 ni.hWnd = message_window;
3884 ni.uID = 0;
3885 ni.uFlags = NIF_ICON|NIF_TIP;
3886 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3887 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3888 Shell_NotifyIcon(NIM_ADD, &ni);
3889# endif
3890
3891 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003892 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003893 while (memchr(done, 0, numFiles) != NULL)
3894 {
3895# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003896 p = serverGetReply(srv, NULL, TRUE, TRUE, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003897 if (p == NULL)
3898 break;
3899# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003900 if (serverReadReply(xterm_dpy, srv, &p, TRUE, -1) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003901 break;
3902# endif
3903 j = atoi((char *)p);
3904 if (j >= 0 && j < numFiles)
3905 {
3906# ifdef FEAT_GUI_W32
3907 ++count;
3908 sprintf(ni.szTip, _("%d of %d edited"),
3909 count, numFiles);
3910 Shell_NotifyIcon(NIM_MODIFY, &ni);
3911# endif
3912 done[j] = 1;
3913 }
3914 }
3915# ifdef FEAT_GUI_W32
3916 Shell_NotifyIcon(NIM_DELETE, &ni);
3917# endif
3918 }
3919 }
3920 else if (STRICMP(argv[i], "--remote-expr") == 0)
3921 {
3922 if (i == *argc - 1)
3923 mainerr_arg_missing((char_u *)argv[i]);
3924# ifdef WIN32
3925 /* Win32 always works? */
3926 if (serverSendToVim(sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003927 &res, NULL, 1, 0, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003928# else
3929 if (xterm_dpy == NULL)
3930 mch_errmsg(_("No display: Send expression failed.\n"));
3931 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003932 &res, NULL, 1, 0, 1, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003933# endif
3934 {
3935 if (res != NULL && *res != NUL)
3936 {
3937 /* Output error from remote */
3938 mch_errmsg((char *)res);
3939 vim_free(res);
3940 res = NULL;
3941 }
3942 mch_errmsg(_(": Send expression failed.\n"));
3943 }
3944 }
3945 else if (STRICMP(argv[i], "--serverlist") == 0)
3946 {
3947# ifdef WIN32
3948 /* Win32 always works? */
3949 res = serverGetVimNames();
3950# else
3951 if (xterm_dpy != NULL)
3952 res = serverGetVimNames(xterm_dpy);
3953# endif
3954 if (called_emsg)
3955 mch_errmsg("\n");
3956 }
3957 else if (STRICMP(argv[i], "--servername") == 0)
3958 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003959 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003960 i++;
3961 continue;
3962 }
3963 else
3964 {
3965 *newArgV++ = argv[i];
3966 newArgC++;
3967 continue;
3968 }
3969 didone = TRUE;
3970 if (res != NULL && *res != NUL)
3971 {
3972 mch_msg((char *)res);
3973 if (res[STRLEN(res) - 1] != '\n')
3974 mch_msg("\n");
3975 }
3976 vim_free(res);
3977 }
3978
3979 if (didone)
3980 {
3981 display_errors(); /* display any collected messages */
3982 exit(exiterr); /* Mission accomplished - get out */
3983 }
3984
3985 /* Return back into main() */
3986 *argc = newArgC;
3987 vim_free(sname);
3988}
3989
3990/*
3991 * Build a ":drop" command to send to a Vim server.
3992 */
3993 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003994build_drop_cmd(
3995 int filec,
3996 char **filev,
3997 int tabs, /* Use ":tab drop" instead of ":drop". */
3998 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003999{
4000 garray_T ga;
4001 int i;
4002 char_u *inicmd = NULL;
4003 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004004 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004005 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004006
4007 if (filec > 0 && filev[0][0] == '+')
4008 {
4009 inicmd = (char_u *)filev[0] + 1;
4010 filev++;
4011 filec--;
4012 }
4013 /* Check if we have at least one argument. */
4014 if (filec <= 0)
4015 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004016
4017 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02004018 cwd = alloc(MAXPATHL);
4019 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004020 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004021 if (mch_dirname(cwd, MAXPATHL) != OK)
4022 {
4023 vim_free(cwd);
4024 return NULL;
4025 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004026 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00004027#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004028 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00004029#else
4030 PATH_ESC_CHARS,
4031#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02004032 '\\', TRUE);
4033 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004034 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004035 return NULL;
4036 ga_init2(&ga, 1, 100);
4037 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004038 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00004039
4040 /* Call inputsave() so that a prompt for an encryption key works. */
4041 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
4042 if (tabs)
4043 ga_concat(&ga, (char_u *)"tab ");
4044 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004045 for (i = 0; i < filec; i++)
4046 {
4047 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004048 * do it again in the Vim server. On MS-Windows only escape
4049 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004050 p = vim_strsave_escaped((char_u *)filev[i],
4051#ifdef UNIX
4052 PATH_ESC_CHARS
4053#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004054 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004055#endif
4056 );
4057 if (p == NULL)
4058 {
4059 vim_free(ga.ga_data);
4060 return NULL;
4061 }
4062 ga_concat(&ga, (char_u *)" ");
4063 ga_concat(&ga, p);
4064 vim_free(p);
4065 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004066 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4067
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004068 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4069 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004070 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4071
4072 /* Switch back to the correct current directory (prior to temporary path
4073 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004074 * correct after the :drop command. With line breaks and spaces:
4075 * if !exists('+acd') || !&acd
4076 * if haslocaldir()
4077 * cd -
4078 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004079 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004080 * cd -
4081 * endif
4082 * endif
4083 */
4084 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004085 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004086 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004087 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004088 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004089
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004090 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004091 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4092 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004093 if (inicmd != NULL)
4094 {
4095 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4096 * the following commands to be inserted as text. Use a "|",
4097 * hopefully "inicmd" does allow this... */
4098 ga_concat(&ga, inicmd);
4099 ga_concat(&ga, (char_u *)"|");
4100 }
4101 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4102 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004103 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004104 ga_append(&ga, NUL);
4105 return ga.ga_data;
4106}
4107
4108/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004109 * Make our basic server name: use the specified "arg" if given, otherwise use
4110 * the tail of the command "cmd" we were started with.
4111 * Return the name in allocated memory. This doesn't include a serial number.
4112 */
4113 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004114serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004115{
4116 char_u *p;
4117
4118 if (arg != NULL && *arg != NUL)
4119 p = vim_strsave_up(arg);
4120 else
4121 {
4122 p = vim_strsave_up(gettail((char_u *)cmd));
4123 /* Remove .exe or .bat from the name. */
4124 if (p != NULL && vim_strchr(p, '.') != NULL)
4125 *vim_strchr(p, '.') = NUL;
4126 }
4127 return p;
4128}
4129#endif /* FEAT_CLIENTSERVER */
4130
4131#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4132/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004133 * Replace termcodes such as <CR> and insert as key presses if there is room.
4134 */
4135 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004136server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004137{
4138 char_u *ptr = NULL;
4139 char_u *cpo_save = p_cpo;
4140
4141 /* Set 'cpoptions' the way we want it.
4142 * B set - backslashes are *not* treated specially
4143 * k set - keycodes are *not* reverse-engineered
4144 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004145 * The last but one parameter of replace_termcodes() is TRUE so that the
4146 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004147 */
4148 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004149 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004150 p_cpo = cpo_save;
4151
4152 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4153 {
4154 /*
4155 * Add the string to the input stream.
4156 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4157 *
4158 * First clear typed characters from the typeahead buffer, there could
4159 * be half a mapping there. Then append to the existing string, so
4160 * that multiple commands from a client are concatenated.
4161 */
4162 if (typebuf.tb_maplen < typebuf.tb_len)
4163 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4164 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4165
4166 /* Let input_available() know we inserted text in the typeahead
4167 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004168 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004169 }
4170 vim_free((char_u *)ptr);
4171}
4172
4173/*
4174 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004175 */
4176 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004177eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004178{
4179 char_u *res;
4180 int save_dbl = debug_break_level;
4181 int save_ro = redir_off;
Bram Moolenaard99388b2017-10-26 14:28:32 +02004182 void *fc = NULL;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004183
4184 /* Evaluate the expression at the toplevel, don't use variables local to
Bram Moolenaard99388b2017-10-26 14:28:32 +02004185 * the calling function. Except when in debug mode. */
4186 if (!debug_mode)
4187 fc = clear_current_funccal();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004188
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004189 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4190 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004191 debug_break_level = -1;
4192 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004193 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4194 * to be typed. Do generate errors so that try/catch works. */
4195 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004196
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004197 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004198
4199 debug_break_level = save_dbl;
4200 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004201 --emsg_silent;
4202 if (emsg_silent < 0)
4203 emsg_silent = 0;
Bram Moolenaard99388b2017-10-26 14:28:32 +02004204 if (fc != NULL)
4205 restore_current_funccal(fc);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004206
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004207 /* A client can tell us to redraw, but not to display the cursor, so do
4208 * that here. */
4209 setcursor();
4210 out_flush();
4211#ifdef FEAT_GUI
4212 if (gui.in_use)
4213 gui_update_cursor(FALSE, FALSE);
4214#endif
4215
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004216 return res;
4217}
4218
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004219/*
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004220 * Evaluate a command or expression sent to ourselves.
4221 */
4222 int
4223sendToLocalVim(char_u *cmd, int asExpr, char_u **result)
4224{
4225 if (asExpr)
4226 {
4227 char_u *ret;
4228
4229 ret = eval_client_expr_to_string(cmd);
4230 if (result != NULL)
4231 {
4232 if (ret == NULL)
4233 {
4234 char *err = _(e_invexprmsg);
4235 size_t len = STRLEN(cmd) + STRLEN(err) + 5;
4236 char_u *msg;
4237
Bram Moolenaar1662ce12017-03-19 21:47:50 +01004238 msg = alloc((unsigned)len);
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004239 if (msg != NULL)
4240 vim_snprintf((char *)msg, len, "%s: \"%s\"", err, cmd);
4241 *result = msg;
4242 }
4243 else
4244 *result = ret;
4245 }
4246 else
4247 vim_free(ret);
4248 return ret == NULL ? -1 : 0;
4249 }
4250 server_to_input_buf(cmd);
4251 return 0;
4252}
4253
4254/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004255 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4256 * return an allocated string. Otherwise return "data".
4257 * "*tofree" is set to the result when it needs to be freed later.
4258 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004259 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004260serverConvert(
4261 char_u *client_enc UNUSED,
4262 char_u *data,
4263 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004264{
4265 char_u *res = data;
4266
4267 *tofree = NULL;
4268# ifdef FEAT_MBYTE
4269 if (client_enc != NULL && p_enc != NULL)
4270 {
4271 vimconv_T vimconv;
4272
4273 vimconv.vc_type = CONV_NONE;
4274 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4275 && vimconv.vc_type != CONV_NONE)
4276 {
4277 res = string_convert(&vimconv, data, NULL);
4278 if (res == NULL)
4279 res = data;
4280 else
4281 *tofree = res;
4282 }
4283 convert_setup(&vimconv, NULL, NULL);
4284 }
4285# endif
4286 return res;
4287}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004288#endif