blob: 9e23606ccb1b2a595a37ca639382d1f9c618d6f3 [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 usage(void);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010043static void parse_command_name(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010044static void command_line_scan(mparm_T *parmp);
45static void check_tty(mparm_T *parmp);
46static void read_stdin(void);
47static void create_windows(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010048static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010049static void exe_pre_commands(mparm_T *parmp);
50static void exe_commands(mparm_T *parmp);
51static void source_startup_scripts(mparm_T *parmp);
52static void main_start_gui(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010053# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010054static void check_swap_exists_action(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010055# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +010056# ifdef FEAT_EVAL
57static void set_progpath(char_u *argv0);
58# endif
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010059# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010060static void exec_on_server(mparm_T *parmp);
61static void prepare_server(mparm_T *parmp);
62static void cmdsrv_main(int *argc, char **argv, char_u *serverName_arg, char_u **serverStr);
63static char_u *serverMakeName(char_u *arg, char *cmd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010064# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000065#endif
66
67
Bram Moolenaarb4210b32004-06-13 14:51:16 +000068/*
69 * Different types of error messages.
70 */
71static char *(main_errors[]) =
72{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000073 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000074#define ME_UNKNOWN_OPTION 0
75 N_("Too many edit arguments"),
76#define ME_TOO_MANY_ARGS 1
77 N_("Argument missing after"),
78#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +000079 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000080#define ME_GARBAGE 3
81 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
82#define ME_EXTRA_CMD 4
83 N_("Invalid argument for"),
84#define ME_INVALID_ARG 5
85};
86
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010087#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaara6044292017-04-02 18:19:53 +020088
89/* Various parameters passed between main() and other functions. */
90static mparm_T params;
91
Bram Moolenaar8866d272012-11-28 15:55:42 +010092#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020093
94static char_u *start_dir = NULL; /* current working dir on startup */
95
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +020096static int has_dash_c_arg = FALSE;
97
Bram Moolenaarb4210b32004-06-13 14:51:16 +000098 int
99# ifdef VIMDLL
100_export
101# endif
102# ifdef FEAT_GUI_MSWIN
103# ifdef __BORLANDC__
104_cdecl
105# endif
106VimMain
107# else
108main
109# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100110(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000111{
Bram Moolenaar77780b62018-03-01 23:10:45 +0100112#if defined(STARTUPTIME) || defined(CLEAN_RUNTIMEPATH)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000113 int i;
114#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000115
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000116 /*
117 * Do any system-specific initialisations. These can NOT use IObuff or
118 * NameBuff. Thus emsg2() cannot be called!
119 */
120 mch_early_init();
121
Bram Moolenaar14993322014-09-09 12:25:33 +0200122#if defined(WIN32) && defined(FEAT_MBYTE)
123 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200124 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200125 * convert when 'encoding' changes. Get the unexpanded arguments.
126 */
127 argc = get_cmd_argsW(&argv);
128#endif
129
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000130 /* Many variables are in "params" so that we can pass them to invoked
131 * functions without a lot of arguments. "argc" and "argv" are also
132 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000133 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000134 params.argc = argc;
135 params.argv = argv;
136 params.want_full_screen = TRUE;
137#ifdef FEAT_EVAL
138 params.use_debug_break_level = -1;
139#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000140 params.window_count = -1;
Bram Moolenaar58d98232005-07-23 22:25:46 +0000141
Bram Moolenaar99685e62013-05-11 13:56:18 +0200142#ifdef FEAT_RUBY
143 {
144 int ruby_stack_start;
145 vim_ruby_init((void *)&ruby_stack_start);
146 }
147#endif
148
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000149#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000150 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000151#endif
152
153#ifdef MEM_PROFILE
154 atexit(vim_mem_profile_dump);
155#endif
156
157#ifdef STARTUPTIME
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200158 /* Need to find "--startuptime" before actually parsing arguments. */
Bram Moolenaar07268702018-03-01 21:57:32 +0100159 for (i = 1; i < argc - 1; ++i)
160 if (STRICMP(argv[i], "--startuptime") == 0)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000161 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000162 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000163 TIME_MSG("--- VIM STARTING ---");
164 break;
165 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000166#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000167 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000168
Bram Moolenaar07268702018-03-01 21:57:32 +0100169#ifdef CLEAN_RUNTIMEPATH
170 /* Need to find "--clean" before actually parsing arguments. */
171 for (i = 1; i < argc; ++i)
172 if (STRICMP(argv[i], "--clean") == 0)
173 {
174 params.clean = TRUE;
175 break;
176 }
177#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200178 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000179
180#ifdef FEAT_CLIENTSERVER
181 /*
182 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000183 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000184 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000185 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000186#endif
187
188 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000189 * Figure out the way to work from the command name argv[0].
190 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000191 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000192 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000193
194 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000195 * Process the command line arguments. File names are put in the global
196 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000197 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000198 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000199 TIME_MSG("parsing arguments");
200
201 /*
202 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000203 * there is no terminal version, and on Windows we can't fork one off with
204 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000205 */
206#ifdef ALWAYS_USE_GUI
207 gui.starting = TRUE;
208#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000209# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000210 /*
211 * Check if the GUI can be started. Reset gui.starting if not.
212 * Don't know about other systems, stay on the safe side and don't check.
213 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000214 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000215 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000216 if (gui_init_check() == FAIL)
217 {
218 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000219
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000220 /* When running "evim" or "gvim -y" we need the menus, exit if we
221 * don't have them. */
222 if (params.evim_mode)
223 mch_exit(1);
224 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000225 }
226# endif
227#endif
228
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000229 if (GARGCOUNT > 0)
230 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100231#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000232 /*
233 * Expand wildcards in file names.
234 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000235 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000236 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200237 start_dir = alloc(MAXPATHL);
238 if (start_dir != NULL)
239 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000240 /* Temporarily add '(' and ')' to 'isfname'. These are valid
241 * filename characters but are excluded from 'isfname' to make
242 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
243 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000244 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000245 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200246 if (start_dir != NULL)
247 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000248 }
249#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200250 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000251 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000252
253#if defined(WIN32) && defined(FEAT_MBYTE)
254 {
255 extern void set_alist_count(void);
256
257 /* Remember the number of entries in the argument list. If it changes
258 * we don't react on setting 'encoding'. */
259 set_alist_count();
260 }
261#endif
262
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000263#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000264 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000265 {
266 /*
267 * If there is one filename, fully qualified, we have very probably
268 * been invoked from explorer, so change to the file's directory.
269 * Hint: to avoid this when typing a command use a forward slash.
270 * If the cd fails, it doesn't matter.
271 */
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100272 (void)vim_chdirfile(params.fname, "drop");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200273 if (start_dir != NULL)
274 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000275 }
276#endif
277 TIME_MSG("expanding arguments");
278
279#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000280 if (params.diff_mode && params.window_count == -1)
281 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000282#endif
283
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000284 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000285 ++RedrawingDisabled;
286
287 /*
288 * When listing swap file names, don't do cursor positioning et. al.
289 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200290 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000291 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000292
293 /*
294 * When certain to start the GUI, don't check capabilities of terminal.
295 * For GTK we can't be sure, but when started from the desktop it doesn't
296 * make sense to try using a terminal.
297 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000298#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000299 if (gui.starting
300# ifdef FEAT_GUI_GTK
301 && !isatty(2)
302# endif
303 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000304 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000305#endif
306
Bram Moolenaard0573012017-10-28 21:11:06 +0200307#if defined(FEAT_GUI_MAC) && defined(MACOS_X_DARWIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000308 /* When the GUI is started from Finder, need to display messages in a
309 * message box. isatty(2) returns TRUE anyway, thus we need to check the
310 * name to know we're not started from a terminal. */
311 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000312 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000313 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000314
315 /* Avoid always using "/" as the current directory. Note that when
316 * started from Finder the arglist will be filled later in
317 * HandleODocAE() and "fname" will be NULL. */
318 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
319 && STRCMP(NameBuff, "/") == 0)
320 {
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200321 if (params.fname != NULL)
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100322 (void)vim_chdirfile(params.fname, "drop");
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000323 else
324 {
325 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
326 vim_chdir(NameBuff);
327 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200328 if (start_dir != NULL)
329 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000330 }
331 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000332#endif
333
334 /*
335 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100336 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000337 * Note that we may use mch_exit() before mch_init()!
338 */
339 mch_init();
340 TIME_MSG("shell init");
341
342#ifdef USE_XSMP
343 /*
344 * For want of anywhere else to do it, try to connect to xsmp here.
345 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
346 * Hijacking -X 'no X connection' to also disable XSMP connection as that
347 * has a similar delay upon failure.
348 * Only try if SESSION_MANAGER is set to something non-null.
349 */
350 if (!x_no_connect)
351 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000352 char *p = getenv("SESSION_MANAGER");
353
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000354 if (p != NULL && *p != NUL)
355 {
356 xsmp_init();
357 TIME_MSG("xsmp init");
358 }
359 }
360#endif
361
362 /*
363 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000364 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000365 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000366
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100367#ifdef _IOLBF
368 /* Ensure output works usefully without a tty: buffer lines instead of
369 * fully buffered. */
370 if (silent_mode)
371 setvbuf(stdout, NULL, _IOLBF, 0);
372#endif
373
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000374 /* This message comes before term inits, but after setting "silent_mode"
375 * when the input is not a tty. */
376 if (GARGCOUNT > 1 && !silent_mode)
377 printf(_("%d files to edit\n"), GARGCOUNT);
378
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000379 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000380 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000381 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000382 capabilities (will set full_screen) */
383 screen_start(); /* don't know where cursor is now */
384 TIME_MSG("Termcap init");
385 }
386
387 /*
388 * Set the default values for the options that use Rows and Columns.
389 */
390 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000391 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000392#ifdef FEAT_DIFF
393 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
394 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000395 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000396 diff_win_options(firstwin, FALSE);
397#endif
398
399 cmdline_row = Rows - p_ch;
400 msg_row = cmdline_row;
401 screenalloc(FALSE); /* allocate screen buffers */
402 set_init_2();
403 TIME_MSG("inits 2");
404
405 msg_scroll = TRUE;
406 no_wait_return = TRUE;
407
408 init_mappings(); /* set up initial mappings */
409
410 init_highlight(TRUE, FALSE); /* set the default highlight groups */
411 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000412
413#ifdef FEAT_EVAL
414 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000415 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000416#endif
417
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200418 /* Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
419 * Allows for setting 'loadplugins' there. */
420 if (params.use_vimrc != NULL
421 && (STRCMP(params.use_vimrc, "NONE") == 0
422 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
423 p_lpl = FALSE;
424
425 /* Execute --cmd arguments. */
426 exe_pre_commands(&params);
427
428 /* Source startup scripts. */
429 source_startup_scripts(&params);
430
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100431#ifdef FEAT_MZSCHEME
432 /*
433 * Newer version of MzScheme (Racket) require earlier (trampolined)
434 * initialisation via scheme_main_setup.
435 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200436 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200437 * Do source startup scripts, so that 'mzschemedll' can be set.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100438 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200439 return mzscheme_main();
440#else
441 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100442#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200443}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100444#endif /* NO_VIM_MAIN */
Bram Moolenaara357e442016-08-10 20:45:07 +0200445#endif /* PROTO */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100446
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200447/*
448 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
449 * things simple.
450 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
451 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100452 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200453vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100454{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100455#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000456#ifdef FEAT_EVAL
457 /*
458 * Read all the plugin files.
459 * Only when compiled with +eval, since most plugins need it.
460 */
461 if (p_lpl)
462 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200463 char_u *rtp_copy = NULL;
464
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200465 /* First add all package directories to 'runtimepath', so that their
466 * autoload directories can be found. Only if not done already with a
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200467 * :packloadall command.
468 * Make a copy of 'runtimepath', so that source_runtime does not use
469 * the pack directories. */
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200470 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200471 {
472 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200473 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200474 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200475
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200476 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy,
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000477# ifdef VMS /* Somehow VMS doesn't handle the "**". */
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200478 (char_u *)"plugin/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000479# else
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200480 (char_u *)"plugin/**/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000481# endif
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200482 DIP_ALL | DIP_NOAFTER);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000483 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200484 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100485
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200486 /* Only source "start" packages if not done already with a :packloadall
487 * command. */
488 if (!did_source_packages)
489 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100490 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200491
492# ifdef VMS /* Somehow VMS doesn't handle the "**". */
493 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_AFTER);
494# else
495 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_AFTER);
496# endif
497 TIME_MSG("loading after plugins");
498
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000499 }
500#endif
501
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000502#ifdef FEAT_DIFF
503 /* Decide about window layout for diff mode after reading vimrc. */
504 if (params.diff_mode && params.window_layout == 0)
505 {
506 if (diffopt_horizontal())
507 params.window_layout = WIN_HOR; /* use horizontal split */
508 else
509 params.window_layout = WIN_VER; /* use vertical split */
510 }
511#endif
512
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000513 /*
514 * Recovery mode without a file name: List swap files.
515 * This uses the 'dir' option, therefore it must be after the
516 * initializations.
517 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200518 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000519 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200520 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000521 mch_exit(0);
522 }
523
524 /*
525 * Set a few option defaults after reading .vimrc files:
526 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
527 */
528 set_init_3();
529 TIME_MSG("inits 3");
530
531 /*
532 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
533 * Note that this overrides anything from a vimrc file.
534 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000535 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000536 p_uc = 0;
537
538#ifdef FEAT_FKMAP
539 if (curwin->w_p_rl && p_altkeymap)
540 {
541 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
542# ifdef FEAT_ARABIC
543 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
544# endif
545 p_fkmap = TRUE; /* Set the Farsi keymap mode */
546 }
547#endif
548
549#ifdef FEAT_GUI
550 if (gui.starting)
551 {
552#if defined(UNIX) || defined(VMS)
553 /* When something caused a message from a vimrc script, need to output
554 * an extra newline before the shell prompt. */
555 if (did_emsg || msg_didout)
556 putchar('\n');
557#endif
558
559 gui_start(); /* will set full_screen to TRUE */
560 TIME_MSG("starting GUI");
561
562 /* When running "evim" or "gvim -y" we need the menus, exit if we
563 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000564 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000565 mch_exit(1);
566 }
567#endif
568
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000569#ifdef FEAT_VIMINFO
570 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000571 * Read in registers, history etc, but not marks, from the viminfo file.
572 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000573 */
574 if (*p_viminfo != NUL)
575 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000576 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000577 TIME_MSG("reading viminfo");
578 }
579#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100580#ifdef FEAT_EVAL
581 /* It's better to make v:oldfiles an empty list than NULL. */
582 if (get_vim_var_list(VV_OLDFILES) == NULL)
583 set_vim_var_list(VV_OLDFILES, list_alloc());
584#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000585
586#ifdef FEAT_QUICKFIX
587 /*
588 * "-q errorfile": Load the error file now.
589 * If the error file can't be read, exit before doing anything else.
590 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000591 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000592 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100593 char_u *enc = NULL;
594
595# ifdef FEAT_MBYTE
596 enc = p_menc;
597# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000598 if (params.use_ef != NULL)
599 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000600 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200601 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100602 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000603 {
604 out_char('\n');
605 mch_exit(3);
606 }
607 TIME_MSG("reading errorfile");
608 }
609#endif
610
611 /*
612 * Start putting things on the screen.
613 * Scroll screen down before drawing over it
614 * Clear screen now, so file message will not be cleared.
615 */
616 starting = NO_BUFFERS;
617 no_wait_return = FALSE;
618 if (!exmode_active)
619 msg_scroll = FALSE;
620
621#ifdef FEAT_GUI
622 /*
623 * This seems to be required to make callbacks to be called now, instead
624 * of after things have been put on the screen, which then may be deleted
625 * when getting a resize callback.
626 * For the Mac this handles putting files dropped on the Vim icon to
627 * global_alist.
628 */
629 if (gui.in_use)
630 {
631# ifdef FEAT_SUN_WORKSHOP
632 if (!usingSunWorkShop)
633# endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100634 gui_wait_for_chars(50L, typebuf.tb_change_cnt);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000635 TIME_MSG("GUI delay");
636 }
637#endif
638
639#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
640 qnx_clip_init();
641#endif
642
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200643#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
644 clip_init(TRUE);
645#endif
646
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000647#ifdef FEAT_XCLIPBOARD
648 /* Start using the X clipboard, unless the GUI was started. */
649# ifdef FEAT_GUI
650 if (!gui.in_use)
651# endif
652 {
653 setup_term_clip();
654 TIME_MSG("setup clipboard");
655 }
656#endif
657
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000658#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000659 /* Prepare for being a Vim server. */
660 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000661#endif
662
663 /*
664 * If "-" argument given: Read file from stdin.
665 * Do this before starting Raw mode, because it may change things that the
666 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
667 * are the same terminal: "cat | vim -".
668 * Using autocommands here may cause trouble...
669 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000670 if (params.edit_type == EDIT_STDIN && !recoverymode)
671 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000672
673#if defined(UNIX) || defined(VMS)
674 /* When switching screens and something caused a message from a vimrc
675 * script, need to output an extra newline on exit. */
676 if ((did_emsg || msg_didout) && *T_TI != NUL)
677 newline_on_exit = TRUE;
678#endif
679
680 /*
681 * When done something that is not allowed or error message call
682 * wait_return. This must be done before starttermcap(), because it may
683 * switch to another screen. It must be done after settmode(TMODE_RAW),
684 * because we want to react on a single key stroke.
685 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000686 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000687 */
688 settmode(TMODE_RAW);
689 TIME_MSG("setting raw mode");
690
691 if (need_wait_return || msg_didany)
692 {
693 wait_return(TRUE);
694 TIME_MSG("waiting for return");
695 }
696
697 starttermcap(); /* start termcap if not done by wait_return() */
698 TIME_MSG("start termcap");
699
700#ifdef FEAT_MOUSE
701 setmouse(); /* may start using the mouse */
702#endif
703 if (scroll_region)
704 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000705 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000706
Bram Moolenaar980bab42018-08-07 22:42:53 +0200707#if defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar40385db2018-08-07 22:31:44 +0200708 term_push_title(SAVE_RESTORE_BOTH);
709#endif
710
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000711 /*
712 * Don't clear the screen when starting in Ex mode, unless using the GUI.
713 */
714 if (exmode_active
715#ifdef FEAT_GUI
716 && !gui.in_use
717#endif
718 )
719 must_redraw = CLEAR;
720 else
721 {
722 screenclear(); /* clear screen */
723 TIME_MSG("clearing screen");
724 }
725
726#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000727 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000728 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100729 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200730 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000731 TIME_MSG("getting crypt key");
732 }
733#endif
734
735 no_wait_return = TRUE;
736
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000737 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000738 * Create the requested number of windows and edit buffers in them.
739 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000740 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000741 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000742 TIME_MSG("opening buffers");
743
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000744#ifdef FEAT_EVAL
745 /* clear v:swapcommand */
746 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
747#endif
748
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000749 /* Ex starts at last line of the file */
750 if (exmode_active)
751 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
752
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000753 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
754 TIME_MSG("BufEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000755 setpcmark();
756
757#ifdef FEAT_QUICKFIX
758 /*
759 * When started with "-q errorfile" jump to first error now.
760 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000761 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000762 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000763 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000764 TIME_MSG("jump to first error");
765 }
766#endif
767
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000768 /*
769 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000770 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000771 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200772 edit_buffers(&params, start_dir);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200773 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000774
775#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000776 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000777 {
778 win_T *wp;
779
780 /* set options in each window for "vimdiff". */
Bram Moolenaar29323592016-07-24 22:04:11 +0200781 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000782 diff_win_options(wp, TRUE);
783 }
784#endif
785
786 /*
787 * Shorten any of the filenames, but only when absolute.
788 */
789 shorten_fnames(FALSE);
790
791 /*
792 * Need to jump to the tag before executing the '-c command'.
793 * Makes "vim -c '/return' -t main" work.
794 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000795 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000796 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000797#if defined(HAS_SWAP_EXISTS_ACTION)
798 swap_exists_did_quit = FALSE;
799#endif
800
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000801 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000802 do_cmdline_cmd(IObuff);
803 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000804
805#if defined(HAS_SWAP_EXISTS_ACTION)
806 /* If the user doesn't want to edit the file then we quit here. */
807 if (swap_exists_did_quit)
808 getout(1);
809#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000810 }
811
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000812 /* Execute any "+", "-c" and "-S" arguments. */
813 if (params.n_commands > 0)
814 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000815
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200816 /* Must come before the may_req_ calls. */
817 starting = 0;
818
Bram Moolenaar976787d2017-06-04 15:45:50 +0200819#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
820 /* Must be done before redrawing, puts a few characters on the screen. */
821 may_req_ambiguous_char_width();
822#endif
823
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000824 RedrawingDisabled = 0;
825 redraw_all_later(NOT_VALID);
826 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000827
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200828 /* 'autochdir' has been postponed */
Bram Moolenaar6f470022018-04-10 18:47:20 +0200829 DO_AUTOCHDIR;
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200830
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000831#ifdef FEAT_TERMRESPONSE
832 /* Requesting the termresponse is postponed until here, so that a "-c q"
833 * argument doesn't make it appear in the shell Vim was started from. */
834 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200835
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200836 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000837#endif
838
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000839 /* start in insert mode */
840 if (p_im)
841 need_start_insertmode = TRUE;
842
Bram Moolenaar14735512016-03-26 21:00:08 +0100843#ifdef FEAT_EVAL
844 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
845#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000846 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
847 TIME_MSG("VimEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000848
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200849#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
850 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
851 * done after the clipboard is available and all initial commands that may
852 * modify the 'clipboard' setting have run; i.e. just before entering the
853 * main loop. */
854 {
855 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100856
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200857 adjust_clip_reg(&default_regname);
858 set_reg_var(default_regname);
859 }
860#endif
861
Bram Moolenaar8a3bb562018-03-04 20:14:14 +0100862#if defined(FEAT_DIFF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000863 /* When a startup script or session file setup for diff'ing and
864 * scrollbind, sync the scrollbind now. */
865 if (curwin->w_p_diff && curwin->w_p_scb)
866 {
867 update_topline();
868 check_scrollbind((linenr_T)0, 0L);
869 TIME_MSG("diff scrollbinding");
870 }
871#endif
872
873#if defined(WIN3264) && !defined(FEAT_GUI_W32)
874 mch_set_winsize_now(); /* Allow winsize changes from now on */
875#endif
876
Bram Moolenaar4033c552017-09-16 20:54:51 +0200877#if defined(FEAT_GUI)
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000878 /* When tab pages were created, may need to update the tab pages line and
879 * scrollbars. This is skipped while creating them. */
880 if (first_tabpage->tp_next != NULL)
881 {
882 out_flush();
883 gui_init_which_components(NULL);
884 gui_update_scrollbars(TRUE);
885 }
886 need_mouse_correct = TRUE;
887#endif
888
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000889 /* If ":startinsert" command used, stuff a dummy command to be able to
890 * call normal_cmd(), which will then start Insert mode. */
891 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000892 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000893
894#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200895 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200896 {
897# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200898# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200899 && !defined(FEAT_GUI_W32)
900 if (gui.in_use)
901 {
902 mch_errmsg(_("netbeans is not supported with this GUI\n"));
903 mch_exit(2);
904 }
905# endif
906# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000907 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200908 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200909 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000910#endif
911
912 TIME_MSG("before starting main loop");
913
914 /*
915 * Call the main command loop. This never returns.
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200916 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000917 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000918
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200919#endif /* NO_VIM_MAIN */
920
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000921 return 0;
922}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000923
924/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200925 * Initialisation shared by main() and some tests.
926 */
927 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200928common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200929{
Bram Moolenaar438d1762018-09-30 17:11:48 +0200930 cmdline_init();
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200931
932#ifdef FEAT_MBYTE
933 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
934#endif
935#ifdef FEAT_EVAL
936 eval_init(); /* init global variables */
937#endif
938
939#ifdef __QNXNTO__
940 qnx_init(); /* PhAttach() for clipboard, (and gui) */
941#endif
942
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200943 /* Init the table of Normal mode commands. */
944 init_normal_cmds();
945
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200946 /*
947 * Allocate space for the generic buffers (needed for set_init_1() and
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100948 * emsg()).
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200949 */
950 if ((IObuff = alloc(IOSIZE)) == NULL
951 || (NameBuff = alloc(MAXPATHL)) == NULL)
952 mch_exit(0);
953 TIME_MSG("Allocated generic buffers");
954
955#ifdef NBDEBUG
956 /* Wait a moment for debugging NetBeans. Must be after allocating
957 * NameBuff. */
958 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
959 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
960 TIME_MSG("NetBeans debug wait");
961#endif
962
963#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
964 /*
965 * Setup to use the current locale (for ctype() and many other things).
966 * NOTE: Translated messages with encodings other than latin1 will not
967 * work until set_init_1() has been called!
968 */
969 init_locale();
970 TIME_MSG("locale set");
971#endif
972
973#ifdef FEAT_GUI
974 gui.dofork = TRUE; /* default is to use fork() */
975#endif
976
977 /*
978 * Do a first scan of the arguments in "argv[]":
979 * -display or --display
980 * --server...
981 * --socketid
982 * --windowid
983 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200984 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200985
986#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200987 findYourself(paramp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200988#endif
Bram Moolenaard0573012017-10-28 21:11:06 +0200989#if defined(FEAT_GUI)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200990 /* Prepare for possibly starting GUI sometime */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200991 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200992 TIME_MSG("GUI prepared");
993#endif
994
995#ifdef FEAT_CLIPBOARD
996 clip_init(FALSE); /* Initialise clipboard stuff */
997 TIME_MSG("clipboard setup");
998#endif
999
1000 /*
1001 * Check if we have an interactive window.
1002 * On the Amiga: If there is no window, we open one with a newcli command
1003 * (needed for :! to * work). mch_check_win() will also handle the -d or
1004 * -dev argument.
1005 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001006 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001007 TIME_MSG("window checked");
1008
1009 /*
1010 * Allocate the first window and buffer.
1011 * Can't do anything without it, exit when it fails.
1012 */
1013 if (win_alloc_first() == FAIL)
1014 mch_exit(0);
1015
1016 init_yank(); /* init yank buffers */
1017
1018 alist_init(&global_alist); /* Init the argument list to empty. */
1019 global_alist.id = 0;
1020
1021 /*
1022 * Set the default values for the options.
1023 * NOTE: Non-latin1 translated messages are working only after this,
1024 * because this is where "has_mbyte" will be set, which is used by
1025 * msg_outtrans_len_attr().
1026 * First find out the home directory, needed to expand "~" in options.
1027 */
1028 init_homedir(); /* find real value of $HOME */
Bram Moolenaar07268702018-03-01 21:57:32 +01001029 set_init_1(paramp->clean);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001030 TIME_MSG("inits 1");
1031
1032#ifdef FEAT_EVAL
1033 set_lang_var(); /* set v:lang and v:ctype */
1034#endif
Bram Moolenaar6436cd82018-12-27 00:28:33 +01001035
1036#ifdef FEAT_SIGNS
1037 init_signs();
1038#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001039}
1040
1041/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001042 * Return TRUE when the --not-a-term argument was found.
1043 */
1044 int
1045is_not_a_term()
1046{
1047 return params.not_a_term;
1048}
1049
1050/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001051 * Main loop: Execute Normal mode commands until exiting Vim.
1052 * Also used to handle commands in the command-line window, until the window
1053 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001054 * Also used to handle ":visual" command after ":global": execute Normal mode
1055 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001056 */
1057 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001058main_loop(
1059 int cmdwin, /* TRUE when working in the command-line window */
1060 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001061{
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001062 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001063 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001064#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001065 /* these are static to avoid a compiler warning */
1066 static linenr_T conceal_old_cursor_line = 0;
1067 static linenr_T conceal_new_cursor_line = 0;
1068 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001069#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001070
1071#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1072 /* Setup to catch a terminating error from the X server. Just ignore
1073 * it, restore the state and continue. This might not always work
1074 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001075 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001077 {
1078 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001079 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001080 got_int = TRUE;
1081 need_wait_return = FALSE;
1082 global_busy = FALSE;
1083 exmode_active = 0;
1084 skip_redraw = FALSE;
1085 RedrawingDisabled = 0;
1086 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001087 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001088# ifdef FEAT_EVAL
1089 emsg_skip = 0;
1090# endif
1091 emsg_off = 0;
1092# ifdef FEAT_MOUSE
1093 setmouse();
1094# endif
1095 settmode(TMODE_RAW);
1096 starttermcap();
1097 scroll_start();
1098 redraw_later_clear();
1099 }
1100#endif
1101
1102 clear_oparg(&oa);
1103 while (!cmdwin
1104#ifdef FEAT_CMDWIN
1105 || cmdwin_result == 0
1106#endif
1107 )
1108 {
1109 if (stuff_empty())
1110 {
1111 did_check_timestamps = FALSE;
1112 if (need_check_timestamps)
1113 check_timestamps(FALSE);
1114 if (need_wait_return) /* if wait_return still needed ... */
1115 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001116 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001117 {
1118 need_start_insertmode = FALSE;
1119 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1120 /* skip the fileinfo message now, because it would be shown
1121 * after insert mode finishes! */
1122 need_fileinfo = FALSE;
1123 }
1124 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001125
1126 /* Reset "got_int" now that we got back to the main loop. Except when
1127 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1128 * the ":g" command.
1129 * For ":g/pat/vi" we reset "got_int" when used once. When used
1130 * a second time we go back to Ex mode and abort the ":g" command. */
1131 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001132 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001133 if (noexmode && global_busy && !exmode_active && previous_got_int)
1134 {
1135 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1136 * used and keep "got_int" set, so that it aborts ":g". */
1137 exmode_active = EXMODE_NORMAL;
1138 State = NORMAL;
1139 }
1140 else if (!global_busy || !exmode_active)
1141 {
1142 if (!quit_more)
1143 (void)vgetc(); /* flush all buffers */
1144 got_int = FALSE;
1145 }
1146 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001147 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001148 else
1149 previous_got_int = FALSE;
1150
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001151 if (!exmode_active)
1152 msg_scroll = FALSE;
1153 quit_more = FALSE;
1154
1155 /*
1156 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1157 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1158 * update cursor and redraw.
1159 */
1160 if (skip_redraw || exmode_active)
1161 skip_redraw = FALSE;
1162 else if (do_redraw || stuff_empty())
1163 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001164#ifdef FEAT_GUI
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001165 // If ui_breakcheck() was used a resize may have been postponed.
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001166 gui_may_resize_shell();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001167#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001168#ifdef HAVE_DROP_FILE
1169 // If files were dropped while text was locked or the curbuf was
1170 // locked, this would be a good time to handle the drop.
1171 handle_any_postponed_drop();
1172#endif
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001173#ifdef FEAT_CONCEAL
1174 if (curwin->w_p_cole == 0)
1175 conceal_update_lines = FALSE;
1176#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001177
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001178 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001179 if (!finish_op && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001180 has_cursormoved()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001181#ifdef FEAT_CONCEAL
1182 || curwin->w_p_cole > 0
1183#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001184 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001185 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001186 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001187 if (has_cursormoved())
1188 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1189 FALSE, curbuf);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001190# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001191 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001192 {
1193 conceal_old_cursor_line = last_cursormoved.lnum;
1194 conceal_new_cursor_line = curwin->w_cursor.lnum;
1195 conceal_update_lines = TRUE;
1196 }
1197# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001198 last_cursormoved = curwin->w_cursor;
1199 }
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001200
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001201#if defined(FEAT_CONCEAL)
1202 if (conceal_update_lines
1203 && (conceal_old_cursor_line != conceal_new_cursor_line
1204 || conceal_cursor_line(curwin)
1205 || need_cursor_line_redraw))
1206 {
1207 if (conceal_old_cursor_line != conceal_new_cursor_line
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001208 && conceal_old_cursor_line != 0
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001209 && conceal_old_cursor_line
1210 <= curbuf->b_ml.ml_line_count)
1211 redrawWinline(curwin, conceal_old_cursor_line);
1212 redrawWinline(curwin, conceal_new_cursor_line);
1213 curwin->w_valid &= ~VALID_CROW;
1214 need_cursor_line_redraw = FALSE;
1215 }
1216#endif
1217
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001218 /* Trigger TextChanged if b:changedtick differs. */
Bram Moolenaar186628f2013-03-19 13:33:23 +01001219 if (!finish_op && has_textchanged()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001220 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001221 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001222 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL, FALSE, curbuf);
1223 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001224 }
Bram Moolenaar186628f2013-03-19 13:33:23 +01001225
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01001226#if defined(FEAT_DIFF)
Bram Moolenaare3521d92018-09-16 14:10:31 +02001227 // Updating diffs from changed() does not always work properly,
1228 // esp. updating folds. Do an update just before redrawing if
1229 // needed.
1230 if (curtab->tp_diff_update || curtab->tp_diff_invalid)
1231 {
1232 ex_diffupdate(NULL);
1233 curtab->tp_diff_update = FALSE;
1234 }
1235
Bram Moolenaar33aec762006-01-22 23:30:12 +00001236 /* Scroll-binding for diff mode may have been postponed until
1237 * here. Avoids doing it for every change. */
1238 if (diff_need_scrollbind)
1239 {
1240 check_scrollbind((linenr_T)0, 0L);
1241 diff_need_scrollbind = FALSE;
1242 }
1243#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001244#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001245 /* Include a closed fold completely in the Visual area. */
1246 foldAdjustVisual();
1247#endif
1248#ifdef FEAT_FOLDING
1249 /*
1250 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1251 * contain the cursor.
1252 * When 'foldopen' is "all", open the fold(s) under the cursor.
1253 * This may mark the window for redrawing.
1254 */
1255 if (hasAnyFolding(curwin) && !char_avail())
1256 {
1257 foldCheckClose();
1258 if (fdo_flags & FDO_ALL)
1259 foldOpenCursor();
1260 }
1261#endif
1262
1263 /*
1264 * Before redrawing, make sure w_topline is correct, and w_leftcol
1265 * if lines don't wrap, and w_skipcol if lines wrap.
1266 */
1267 update_topline();
1268 validate_cursor();
1269
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001270 if (VIsual_active)
1271 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001272 else if (must_redraw)
Bram Moolenaara338adc2018-01-31 20:51:47 +01001273 {
1274 mch_disable_flush(); /* Stop issuing gui_mch_flush(). */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001275 update_screen(0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01001276 mch_enable_flush();
1277 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001278 else if (redraw_cmdline || clear_cmdline)
1279 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001280 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001281#ifdef FEAT_TITLE
1282 if (need_maketitle)
1283 maketitle();
1284#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001285#ifdef FEAT_VIMINFO
1286 curbuf->b_last_used = vim_time();
1287#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001288 /* display message after redraw */
1289 if (keep_msg != NULL)
1290 {
1291 char_u *p;
1292
1293 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001294 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1295 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001296 p = keep_msg;
1297 msg_attr(p, keep_msg_attr);
1298 vim_free(p);
1299 }
1300 if (need_fileinfo) /* show file info after redraw */
1301 {
1302 fileinfo(FALSE, TRUE, FALSE);
1303 need_fileinfo = FALSE;
1304 }
1305
1306 emsg_on_display = FALSE; /* can delete error message now */
1307 did_emsg = FALSE;
1308 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001309 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001310 showruler(FALSE);
1311
1312 setcursor();
1313 cursor_on();
1314
1315 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001316
1317#ifdef STARTUPTIME
1318 /* Now that we have drawn the first screen all the startup stuff
1319 * has been done, close any file for startup messages. */
1320 if (time_fd != NULL)
1321 {
1322 TIME_MSG("first screen update");
1323 TIME_MSG("--- VIM STARTED ---");
1324 fclose(time_fd);
1325 time_fd = NULL;
1326 }
1327#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001328 }
1329#ifdef FEAT_GUI
1330 if (need_mouse_correct)
1331 gui_mouse_correct();
1332#endif
1333
1334 /*
1335 * Update w_curswant if w_set_curswant has been set.
1336 * Postponed until here to avoid computing w_virtcol too often.
1337 */
1338 update_curswant();
1339
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001340#ifdef FEAT_EVAL
1341 /*
1342 * May perform garbage collection when waiting for a character, but
1343 * only at the very toplevel. Otherwise we may be using a List or
1344 * Dict internally somewhere.
1345 * "may_garbage_collect" is reset in vgetc() which is invoked through
1346 * do_exmode() and normal_cmd().
1347 */
1348 may_garbage_collect = (!cmdwin && !noexmode);
1349#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001350 /*
1351 * If we're invoked as ex, do a round of ex commands.
1352 * Otherwise, get and execute a normal mode command.
1353 */
1354 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001355 {
1356 if (noexmode) /* End of ":global/path/visual" commands */
1357 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001358 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001359 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001360 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001361 {
1362#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001363 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001364 && oa.op_type == OP_NOP && oa.regname == NUL
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001365 && !VIsual_active
1366 && !skip_term_loop)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001367 {
1368 /* If terminal_loop() returns OK we got a key that is handled
Bram Moolenaar6d819742017-08-06 14:57:49 +02001369 * in Normal model. With FAIL we first need to position the
1370 * cursor and the screen needs to be redrawn. */
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001371 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001372 normal_cmd(&oa, TRUE);
1373 }
1374 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001375#endif
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001376 {
1377#ifdef FEAT_TERMINAL
1378 skip_term_loop = FALSE;
1379#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001380 normal_cmd(&oa, TRUE);
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001381 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02001382 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001383 }
1384}
1385
1386
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001387#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001388/*
1389 * Exit, but leave behind swap files for modified buffers.
1390 */
1391 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001392getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001393{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001394# if defined(SIGHUP) && defined(SIG_IGN)
1395 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1396 * makes Vim exit and then handling SIGHUP causes various reentrance
1397 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001398 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001399# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001400
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001401 ml_close_notmod(); /* close all not-modified buffers */
1402 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1403 ml_close_all(FALSE); /* close all memfiles, without deleting */
1404 getout(exitval); /* exit Vim properly */
1405}
1406#endif
1407
1408
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001409/*
1410 * Exit properly.
1411 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001412 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001413getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001414{
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001415 exiting = TRUE;
Bram Moolenaar0bd052b2018-03-22 20:33:56 +01001416#if defined(FEAT_JOB_CHANNEL)
1417 ch_log(NULL, "Exiting...");
1418#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001419
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001420 /* When running in Ex mode an error causes us to exit with a non-zero exit
1421 * code. POSIX requires this, although it's not 100% clear from the
1422 * standard. */
1423 if (exmode_active)
1424 exitval += ex_exitval;
1425
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001426 /* Position the cursor on the last screen line, below all the text */
1427#ifdef FEAT_GUI
1428 if (!gui.in_use)
1429#endif
1430 windgoto((int)Rows - 1, 0);
1431
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001432#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1433 /* Optionally print hashtable efficiency. */
1434 hash_debug_results();
1435#endif
1436
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001437#ifdef FEAT_GUI
1438 msg_didany = FALSE;
1439#endif
1440
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001441 if (v_dying <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001442 {
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001443 tabpage_T *tp;
1444 tabpage_T *next_tp;
1445 buf_T *buf;
1446 win_T *wp;
1447
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001448 /* Trigger BufWinLeave for all windows, but only once per buffer. */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001449 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001450 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001451 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001452 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001453 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001454 if (wp->w_buffer == NULL)
1455 /* Autocmd must have close the buffer already, skip. */
1456 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001457 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001458 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001459 {
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001460 bufref_T bufref;
1461
1462 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001463 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1464 buf->b_fname, FALSE, buf);
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001465 if (bufref_valid(&bufref))
1466 CHANGEDTICK(buf) = -1; /* note we did it already */
1467
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001468 /* start all over, autocommands may mess up the lists */
1469 next_tp = first_tabpage;
1470 break;
1471 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001472 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001473 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001474
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001475 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001476 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001477 if (buf->b_ml.ml_mfp != NULL)
1478 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001479 bufref_T bufref;
1480
1481 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001482 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001483 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001484 if (!bufref_valid(&bufref))
1485 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001486 break;
1487 }
1488 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1489 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001490
1491#ifdef FEAT_VIMINFO
1492 if (*p_viminfo != NUL)
1493 /* Write out the registers, history, marks etc, to the viminfo file */
1494 write_viminfo(NULL, FALSE);
1495#endif
1496
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001497 if (v_dying <= 1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001498 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001499
Bram Moolenaar05159a02005-02-26 23:04:13 +00001500#ifdef FEAT_PROFILE
1501 profile_dump();
1502#endif
1503
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001504 if (did_emsg
1505#ifdef FEAT_GUI
1506 || (gui.in_use && msg_didany && p_verbose > 0)
1507#endif
1508 )
1509 {
1510 /* give the user a chance to read the (error) message */
1511 no_wait_return = FALSE;
1512 wait_return(FALSE);
1513 }
1514
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001515 /* Position the cursor again, the autocommands may have moved it */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001516#ifdef FEAT_GUI
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001517 if (!gui.in_use)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001518#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001519 windgoto((int)Rows - 1, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001520
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001521#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001522 job_stop_on_exit();
1523#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001524#ifdef FEAT_LUA
1525 lua_end();
1526#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001527#ifdef FEAT_MZSCHEME
1528 mzscheme_end();
1529#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001530#ifdef FEAT_TCL
1531 tcl_end();
1532#endif
1533#ifdef FEAT_RUBY
1534 ruby_end();
1535#endif
1536#ifdef FEAT_PYTHON
1537 python_end();
1538#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001539#ifdef FEAT_PYTHON3
1540 python3_end();
1541#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001542#ifdef FEAT_PERL
1543 perl_end();
1544#endif
1545#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1546 iconv_end();
1547#endif
1548#ifdef FEAT_NETBEANS_INTG
1549 netbeans_end();
1550#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001551#ifdef FEAT_CSCOPE
1552 cs_end();
1553#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001554#ifdef FEAT_EVAL
1555 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001556 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001557#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001558#if defined(WIN32) && defined(FEAT_MBYTE)
1559 free_cmd_argsW();
1560#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001561
1562 mch_exit(exitval);
1563}
1564
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001565#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1566/*
1567 * Setup to use the current locale (for ctype() and many other things).
1568 */
1569 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001570init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001571{
1572 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001573
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001574# ifdef FEAT_GUI_GTK
1575 /* Tell Gtk not to change our locale settings. */
1576 gtk_disable_setlocale();
1577# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001578# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1579 /* Make sure strtod() uses a decimal point, not a comma. */
1580 setlocale(LC_NUMERIC, "C");
1581# endif
1582
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001583# ifdef WIN32
1584 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1585 * text while it's expecting text in the current locale. This call avoids
1586 * that. */
1587 setlocale(LC_CTYPE, "C");
1588# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001589
1590# ifdef FEAT_GETTEXT
1591 {
1592 int mustfree = FALSE;
1593 char_u *p;
1594
1595# ifdef DYNAMIC_GETTEXT
1596 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001597 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001598# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001599 /* expand_env() doesn't work yet, because g_chartab[] is not
1600 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001601 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1602 if (p != NULL && *p != NUL)
1603 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001604 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001605 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1606 }
1607 if (mustfree)
1608 vim_free(p);
1609 textdomain(VIMPACKAGE);
1610 }
1611# endif
1612}
1613#endif
1614
1615/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001616 * Get the name of the display, before gui_prepare() removes it from
1617 * argv[]. Used for the xterm-clipboard display.
1618 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001619 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001620 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001621 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001622early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001623{
Bram Moolenaar03005972008-11-20 13:12:36 +00001624#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1625 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001626 int argc = parmp->argc;
1627 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001628 int i;
1629
1630 for (i = 1; i < argc; i++)
1631 {
1632 if (STRCMP(argv[i], "--") == 0)
1633 break;
1634# ifdef FEAT_XCLIPBOARD
1635 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001636# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001637 || STRICMP(argv[i], "--display") == 0
1638# endif
1639 )
1640 {
1641 if (i == argc - 1)
1642 mainerr_arg_missing((char_u *)argv[i]);
1643 xterm_display = argv[++i];
1644 }
1645# endif
1646# ifdef FEAT_CLIENTSERVER
1647 else if (STRICMP(argv[i], "--servername") == 0)
1648 {
1649 if (i == argc - 1)
1650 mainerr_arg_missing((char_u *)argv[i]);
1651 parmp->serverName_arg = (char_u *)argv[++i];
1652 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001653 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001654 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001655 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001656 {
1657 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001658# ifdef FEAT_GUI
1659 if (strstr(argv[i], "-wait") != 0)
1660 /* don't fork() when starting the GUI to edit files ourself */
1661 gui.dofork = FALSE;
1662# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001663 }
1664# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001665
1666# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1667# ifdef FEAT_GUI_W32
1668 else if (STRICMP(argv[i], "--windowid") == 0)
1669# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001670 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001671# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001672 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001673 long_u id;
1674 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001675
1676 if (i == argc - 1)
1677 mainerr_arg_missing((char_u *)argv[i]);
1678 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001679 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001680 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001681 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001682 if (count != 1)
1683 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1684 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001685# ifdef FEAT_GUI_W32
1686 win_socket_id = id;
1687# else
1688 gtk_socket_id = id;
1689# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001690 i++;
1691 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001692# endif
1693# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001694 else if (STRICMP(argv[i], "--echo-wid") == 0)
1695 echo_wid_arg = TRUE;
1696# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001697# ifndef FEAT_NETBEANS_INTG
1698 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001699 {
1700 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1701 mch_exit(2);
1702 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001703# endif
1704
Bram Moolenaar58d98232005-07-23 22:25:46 +00001705 }
1706#endif
1707}
1708
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001709#ifndef NO_VIM_MAIN
1710/*
1711 * Get a (optional) count for a Vim argument.
1712 */
1713 static int
1714get_number_arg(
1715 char_u *p, /* pointer to argument */
1716 int *idx, /* index in argument, is incremented */
1717 int def) /* default value */
1718{
1719 if (vim_isdigit(p[*idx]))
1720 {
1721 def = atoi((char *)&(p[*idx]));
1722 while (vim_isdigit(p[*idx]))
1723 *idx = *idx + 1;
1724 }
1725 return def;
1726}
1727
1728/*
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001729 * Check for: [r][e][g][vi|vim|view][diff][ex[im]] (sort of)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001730 * If the executable name starts with "r" we disable shell commands.
1731 * If the next character is "e" we run in Easy mode.
1732 * If the next character is "g" we run the GUI version.
1733 * If the next characters are "view" we start in readonly mode.
1734 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1735 * If the next characters are "ex" we start in Ex mode. If it's followed
1736 * by "im" use improved Ex mode.
1737 */
1738 static void
1739parse_command_name(mparm_T *parmp)
1740{
1741 char_u *initstr;
1742
1743 initstr = gettail((char_u *)parmp->argv[0]);
1744
Bram Moolenaard0573012017-10-28 21:11:06 +02001745#ifdef FEAT_GUI_MAC
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001746 /* An issue has been seen when launching Vim in such a way that
1747 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1748 * executable or a symbolic link of it. Until this issue is resolved
1749 * we prohibit the GUI from being used.
1750 */
1751 if (STRCMP(initstr, parmp->argv[0]) == 0)
1752 disallow_gui = TRUE;
1753
1754 /* TODO: On MacOS X default to gui if argv[0] ends in:
1755 * /Vim.app/Contents/MacOS/Vim */
1756#endif
1757
1758#ifdef FEAT_EVAL
1759 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001760 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001761#endif
1762
1763 if (TOLOWER_ASC(initstr[0]) == 'r')
1764 {
1765 restricted = TRUE;
1766 ++initstr;
1767 }
1768
1769 /* Use evim mode for "evim" and "egvim", not for "editor". */
1770 if (TOLOWER_ASC(initstr[0]) == 'e'
1771 && (TOLOWER_ASC(initstr[1]) == 'v'
1772 || TOLOWER_ASC(initstr[1]) == 'g'))
1773 {
1774#ifdef FEAT_GUI
1775 gui.starting = TRUE;
1776#endif
1777 parmp->evim_mode = TRUE;
1778 ++initstr;
1779 }
1780
1781 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1782 if (TOLOWER_ASC(initstr[0]) == 'g')
1783 {
1784 main_start_gui();
1785#ifdef FEAT_GUI
1786 ++initstr;
1787#endif
1788 }
1789
1790 if (STRNICMP(initstr, "view", 4) == 0)
1791 {
1792 readonlymode = TRUE;
1793 curbuf->b_p_ro = TRUE;
1794 p_uc = 10000; /* don't update very often */
1795 initstr += 4;
1796 }
1797 else if (STRNICMP(initstr, "vim", 3) == 0)
1798 initstr += 3;
1799
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001800 // Catch "[r][g]vimdiff" and "[r][g]viewdiff".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001801 if (STRICMP(initstr, "diff") == 0)
1802 {
1803#ifdef FEAT_DIFF
1804 parmp->diff_mode = TRUE;
1805#else
1806 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1807 mch_errmsg("\n");
1808 mch_exit(2);
1809#endif
1810 }
1811
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001812 // Checking for "ex" here may catch some weir names, such as "vimex" or
1813 // "viewex", we assume the user knows that.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001814 if (STRNICMP(initstr, "ex", 2) == 0)
1815 {
1816 if (STRNICMP(initstr + 2, "im", 2) == 0)
1817 exmode_active = EXMODE_VIM;
1818 else
1819 exmode_active = EXMODE_NORMAL;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001820 change_compatible(TRUE); // set 'compatible'
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001821 }
1822}
1823
Bram Moolenaar58d98232005-07-23 22:25:46 +00001824/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001825 * Scan the command line arguments.
1826 */
1827 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001828command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001829{
1830 int argc = parmp->argc;
1831 char **argv = parmp->argv;
1832 int argv_idx; /* index in argv[n][] */
1833 int had_minmin = FALSE; /* found "--" argument */
1834 int want_argument; /* option argument with argument */
1835 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001836 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001837 long n;
1838
1839 --argc;
1840 ++argv;
1841 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1842 while (argc > 0)
1843 {
1844 /*
1845 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1846 */
1847 if (argv[0][0] == '+' && !had_minmin)
1848 {
1849 if (parmp->n_commands >= MAX_ARG_CMDS)
1850 mainerr(ME_EXTRA_CMD, NULL);
1851 argv_idx = -1; /* skip to next argument */
1852 if (argv[0][1] == NUL)
1853 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1854 else
1855 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1856 }
1857
1858 /*
1859 * Optional argument.
1860 */
1861 else if (argv[0][0] == '-' && !had_minmin)
1862 {
1863 want_argument = FALSE;
1864 c = argv[0][argv_idx++];
1865#ifdef VMS
1866 /*
1867 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1868 * and "-/X" as "-X".
1869 */
1870 if (c == '/')
1871 {
1872 c = argv[0][argv_idx++];
1873 c = TOUPPER_ASC(c);
1874 }
1875 else
1876 c = TOLOWER_ASC(c);
1877#endif
1878 switch (c)
1879 {
1880 case NUL: /* "vim -" read from stdin */
1881 /* "ex -" silent mode */
1882 if (exmode_active)
1883 silent_mode = TRUE;
1884 else
1885 {
1886 if (parmp->edit_type != EDIT_NONE)
1887 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1888 parmp->edit_type = EDIT_STDIN;
1889 read_cmd_fd = 2; /* read from stderr instead of stdin */
1890 }
1891 argv_idx = -1; /* skip to next argument */
1892 break;
1893
1894 case '-': /* "--" don't take any more option arguments */
1895 /* "--help" give help message */
1896 /* "--version" give version message */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001897 /* "--clean" clean context */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001898 /* "--literal" take files literally */
1899 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001900 /* "--not-a-term" don't warn for not a term */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001901 /* "--ttyfail" exit if not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001902 /* "--noplugin[s]" skip plugins */
1903 /* "--cmd <cmd>" execute cmd before vimrc */
1904 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1905 usage();
1906 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1907 {
1908 Columns = 80; /* need to init Columns */
1909 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1910 list_version();
1911 msg_putchar('\n');
1912 msg_didout = FALSE;
1913 mch_exit(0);
1914 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001915 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
1916 {
1917 parmp->use_vimrc = (char_u *)"DEFAULTS";
Bram Moolenaar62dd4522018-03-14 21:20:02 +01001918#ifdef FEAT_GUI
1919 use_gvimrc = (char_u *)"NONE";
1920#endif
Bram Moolenaar07268702018-03-01 21:57:32 +01001921 parmp->clean = TRUE;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001922 set_option_value((char_u *)"vif", 0L, (char_u *)"NONE", 0);
1923 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001924 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1925 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001926#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001927 parmp->literal = TRUE;
1928#endif
1929 }
1930 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1931 {
1932#ifdef FEAT_GUI
1933 gui.dofork = FALSE; /* don't fork() when starting GUI */
1934#endif
1935 }
1936 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1937 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001938 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1939 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001940 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
1941 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001942 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1943 {
1944 want_argument = TRUE;
1945 argv_idx += 3;
1946 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001947 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1948 {
1949 want_argument = TRUE;
1950 argv_idx += 11;
1951 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001952#ifdef FEAT_CLIENTSERVER
1953 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1954 ; /* already processed -- no arg */
1955 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1956 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1957 {
1958 /* already processed -- snatch the following arg */
1959 if (argc > 1)
1960 {
1961 --argc;
1962 ++argv;
1963 }
1964 }
1965#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001966#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1967# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001968 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001969# else
1970 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1971# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001972 {
1973 /* already processed -- snatch the following arg */
1974 if (argc > 1)
1975 {
1976 --argc;
1977 ++argv;
1978 }
1979 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001980#endif
1981#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001982 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1983 {
1984 /* already processed, skip */
1985 }
1986#endif
1987 else
1988 {
1989 if (argv[0][argv_idx])
1990 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1991 had_minmin = TRUE;
1992 }
1993 if (!want_argument)
1994 argv_idx = -1; /* skip to next argument */
1995 break;
1996
1997 case 'A': /* "-A" start in Arabic mode */
1998#ifdef FEAT_ARABIC
1999 set_option_value((char_u *)"arabic", 1L, NULL, 0);
2000#else
2001 mch_errmsg(_(e_noarabic));
2002 mch_exit(2);
2003#endif
2004 break;
2005
2006 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002007 /* Needs to be effective before expanding file names, because
2008 * for Win32 this makes us edit a shortcut file itself,
2009 * instead of the file it links to. */
2010 set_options_bin(curbuf->b_p_bin, 1, 0);
2011 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002012 break;
2013
2014 case 'C': /* "-C" Compatible */
2015 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02002016 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002017 break;
2018
2019 case 'e': /* "-e" Ex mode */
2020 exmode_active = EXMODE_NORMAL;
2021 break;
2022
2023 case 'E': /* "-E" Improved Ex mode */
2024 exmode_active = EXMODE_VIM;
2025 break;
2026
2027 case 'f': /* "-f" GUI: run in foreground. Amiga: open
2028 window directly, not with newcli */
2029#ifdef FEAT_GUI
2030 gui.dofork = FALSE; /* don't fork() when starting GUI */
2031#endif
2032 break;
2033
2034 case 'g': /* "-g" start GUI */
2035 main_start_gui();
2036 break;
2037
2038 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
2039#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002040 p_fkmap = TRUE;
2041 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002042#else
2043 mch_errmsg(_(e_nofarsi));
2044 mch_exit(2);
2045#endif
2046 break;
2047
Bram Moolenaarc3e81692018-05-05 15:09:51 +02002048 case '?': /* "-?" give help message (for MS-Windows) */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002049 case 'h': /* "-h" give help message */
2050#ifdef FEAT_GUI_GNOME
2051 /* Tell usage() to exit for "gvim". */
2052 gui.starting = FALSE;
2053#endif
2054 usage();
2055 break;
2056
2057 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2058#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002059 p_hkmap = TRUE;
2060 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002061#else
2062 mch_errmsg(_(e_nohebrew));
2063 mch_exit(2);
2064#endif
2065 break;
2066
2067 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2068#ifdef FEAT_LISP
2069 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2070 p_sm = TRUE;
2071#endif
2072 break;
2073
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002074 case 'M': /* "-M" no changes or writing of files */
2075 reset_modifiable();
2076 /* FALLTHROUGH */
2077
2078 case 'm': /* "-m" no writing of files */
2079 p_write = FALSE;
2080 break;
2081
2082 case 'y': /* "-y" easy mode */
2083#ifdef FEAT_GUI
2084 gui.starting = TRUE; /* start GUI a bit later */
2085#endif
2086 parmp->evim_mode = TRUE;
2087 break;
2088
2089 case 'N': /* "-N" Nocompatible */
2090 change_compatible(FALSE);
2091 break;
2092
2093 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002094#ifdef FEAT_NETBEANS_INTG
2095 /* checking for "-nb", netbeans parameters */
2096 if (argv[0][argv_idx] == 'b')
2097 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002098 netbeansArg = argv[0];
2099 argv_idx = -1; /* skip to next argument */
2100 }
2101 else
2102#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002103 parmp->no_swap_file = TRUE;
2104 break;
2105
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002106 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002107#ifdef TARGET_API_MAC_OSX
2108 /* For some reason on MacOS X, an argument like:
2109 -psn_0_10223617 is passed in when invoke from Finder
2110 or with the 'open' command */
2111 if (argv[0][argv_idx] == 's')
2112 {
2113 argv_idx = -1; /* bypass full -psn */
2114 main_start_gui();
2115 break;
2116 }
2117#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002118 /* default is 0: open window for each file */
2119 parmp->window_count = get_number_arg((char_u *)argv[0],
2120 &argv_idx, 0);
2121 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002122 break;
2123
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002124 case 'o': /* "-o[N]" open N horizontal split windows */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002125 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002126 parmp->window_count = get_number_arg((char_u *)argv[0],
2127 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002128 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002129 break;
2130
2131 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002132 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002133 parmp->window_count = get_number_arg((char_u *)argv[0],
2134 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002135 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002136 break;
2137
2138#ifdef FEAT_QUICKFIX
2139 case 'q': /* "-q" QuickFix mode */
2140 if (parmp->edit_type != EDIT_NONE)
2141 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2142 parmp->edit_type = EDIT_QF;
2143 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2144 {
2145 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2146 argv_idx = -1;
2147 }
2148 else if (argc > 1) /* "-q {errorfile}" */
2149 want_argument = TRUE;
2150 break;
2151#endif
2152
2153 case 'R': /* "-R" readonly mode */
2154 readonlymode = TRUE;
2155 curbuf->b_p_ro = TRUE;
2156 p_uc = 10000; /* don't update very often */
2157 break;
2158
2159 case 'r': /* "-r" recovery mode */
2160 case 'L': /* "-L" recovery mode */
2161 recoverymode = 1;
2162 break;
2163
2164 case 's':
2165 if (exmode_active) /* "-s" silent (batch) mode */
2166 silent_mode = TRUE;
2167 else /* "-s {scriptin}" read from script file */
2168 want_argument = TRUE;
2169 break;
2170
2171 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2172 if (parmp->edit_type != EDIT_NONE)
2173 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2174 parmp->edit_type = EDIT_TAG;
2175 if (argv[0][argv_idx]) /* "-t{tag}" */
2176 {
2177 parmp->tagname = (char_u *)argv[0] + argv_idx;
2178 argv_idx = -1;
2179 }
2180 else /* "-t {tag}" */
2181 want_argument = TRUE;
2182 break;
2183
2184#ifdef FEAT_EVAL
2185 case 'D': /* "-D" Debugging */
2186 parmp->use_debug_break_level = 9999;
2187 break;
2188#endif
2189#ifdef FEAT_DIFF
2190 case 'd': /* "-d" 'diff' */
2191# ifdef AMIGA
2192 /* check for "-dev {device}" */
2193 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2194 want_argument = TRUE;
2195 else
2196# endif
2197 parmp->diff_mode = TRUE;
2198 break;
2199#endif
2200 case 'V': /* "-V{N}" Verbose level */
2201 /* default is 10: a little bit verbose */
2202 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2203 if (argv[0][argv_idx] != NUL)
2204 {
2205 set_option_value((char_u *)"verbosefile", 0L,
2206 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002207 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002208 }
2209 break;
2210
2211 case 'v': /* "-v" Vi-mode (as if called "vi") */
2212 exmode_active = 0;
2213#ifdef FEAT_GUI
2214 gui.starting = FALSE; /* don't start GUI */
2215#endif
2216 break;
2217
2218 case 'w': /* "-w{number}" set window height */
2219 /* "-w {scriptout}" write to script */
2220 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2221 {
2222 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2223 set_option_value((char_u *)"window", n, NULL, 0);
2224 break;
2225 }
2226 want_argument = TRUE;
2227 break;
2228
2229#ifdef FEAT_CRYPT
2230 case 'x': /* "-x" encrypted reading/writing of files */
2231 parmp->ask_for_key = TRUE;
2232 break;
2233#endif
2234
2235 case 'X': /* "-X" don't connect to X server */
2236#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2237 x_no_connect = TRUE;
2238#endif
2239 break;
2240
2241 case 'Z': /* "-Z" restricted mode */
2242 restricted = TRUE;
2243 break;
2244
2245 case 'c': /* "-c{command}" or "-c {command}" execute
2246 command */
2247 if (argv[0][argv_idx] != NUL)
2248 {
2249 if (parmp->n_commands >= MAX_ARG_CMDS)
2250 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002251 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2252 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002253 argv_idx = -1;
2254 break;
2255 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002256 /* FALLTHROUGH */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002257 case 'S': /* "-S {file}" execute Vim script */
2258 case 'i': /* "-i {viminfo}" use for viminfo */
2259#ifndef FEAT_DIFF
2260 case 'd': /* "-d {device}" device (for Amiga) */
2261#endif
2262 case 'T': /* "-T {terminal}" terminal name */
2263 case 'u': /* "-u {vimrc}" vim inits file */
2264 case 'U': /* "-U {gvimrc}" gvim inits file */
2265 case 'W': /* "-W {scriptout}" overwrite */
2266#ifdef FEAT_GUI_W32
2267 case 'P': /* "-P {parent title}" MDI parent */
2268#endif
2269 want_argument = TRUE;
2270 break;
2271
2272 default:
2273 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2274 }
2275
2276 /*
2277 * Handle option arguments with argument.
2278 */
2279 if (want_argument)
2280 {
2281 /*
2282 * Check for garbage immediately after the option letter.
2283 */
2284 if (argv[0][argv_idx] != NUL)
2285 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2286
2287 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002288 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002289 mainerr_arg_missing((char_u *)argv[0]);
2290 ++argv;
2291 argv_idx = -1;
2292
2293 switch (c)
2294 {
2295 case 'c': /* "-c {command}" execute command */
2296 case 'S': /* "-S {file}" execute Vim script */
2297 if (parmp->n_commands >= MAX_ARG_CMDS)
2298 mainerr(ME_EXTRA_CMD, NULL);
2299 if (c == 'S')
2300 {
2301 char *a;
2302
2303 if (argc < 1)
2304 /* "-S" without argument: use default session file
2305 * name. */
2306 a = SESSION_FILE;
2307 else if (argv[0][0] == '-')
2308 {
2309 /* "-S" followed by another option: use default
2310 * session file name. */
2311 a = SESSION_FILE;
2312 ++argc;
2313 --argv;
2314 }
2315 else
2316 a = argv[0];
2317 p = alloc((unsigned)(STRLEN(a) + 4));
2318 if (p == NULL)
2319 mch_exit(2);
2320 sprintf((char *)p, "so %s", a);
2321 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2322 parmp->commands[parmp->n_commands++] = p;
2323 }
2324 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002325 parmp->commands[parmp->n_commands++] =
2326 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002327 break;
2328
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002329 case '-':
2330 if (argv[-1][2] == 'c')
2331 {
2332 /* "--cmd {command}" execute command */
2333 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2334 mainerr(ME_EXTRA_CMD, NULL);
2335 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002336 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002337 }
2338 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002339 break;
2340
2341 /* case 'd': -d {device} is handled in mch_check_win() for the
2342 * Amiga */
2343
2344#ifdef FEAT_QUICKFIX
2345 case 'q': /* "-q {errorfile}" QuickFix mode */
2346 parmp->use_ef = (char_u *)argv[0];
2347 break;
2348#endif
2349
2350 case 'i': /* "-i {viminfo}" use for viminfo */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002351 set_option_value((char_u *)"vif", 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002352 break;
2353
2354 case 's': /* "-s {scriptin}" read from script file */
2355 if (scriptin[0] != NULL)
2356 {
2357scripterror:
2358 mch_errmsg(_("Attempt to open script file again: \""));
2359 mch_errmsg(argv[-1]);
2360 mch_errmsg(" ");
2361 mch_errmsg(argv[0]);
2362 mch_errmsg("\"\n");
2363 mch_exit(2);
2364 }
2365 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2366 {
2367 mch_errmsg(_("Cannot open for reading: \""));
2368 mch_errmsg(argv[0]);
2369 mch_errmsg("\"\n");
2370 mch_exit(2);
2371 }
2372 if (save_typebuf() == FAIL)
2373 mch_exit(2); /* out of memory */
2374 break;
2375
2376 case 't': /* "-t {tag}" */
2377 parmp->tagname = (char_u *)argv[0];
2378 break;
2379
2380 case 'T': /* "-T {terminal}" terminal name */
2381 /*
2382 * The -T term argument is always available and when
2383 * HAVE_TERMLIB is supported it overrides the environment
2384 * variable TERM.
2385 */
2386#ifdef FEAT_GUI
2387 if (term_is_gui((char_u *)argv[0]))
2388 gui.starting = TRUE; /* start GUI a bit later */
2389 else
2390#endif
2391 parmp->term = (char_u *)argv[0];
2392 break;
2393
2394 case 'u': /* "-u {vimrc}" vim inits file */
2395 parmp->use_vimrc = (char_u *)argv[0];
2396 break;
2397
2398 case 'U': /* "-U {gvimrc}" gvim inits file */
2399#ifdef FEAT_GUI
2400 use_gvimrc = (char_u *)argv[0];
2401#endif
2402 break;
2403
2404 case 'w': /* "-w {nr}" 'window' value */
2405 /* "-w {scriptout}" append to script file */
2406 if (vim_isdigit(*((char_u *)argv[0])))
2407 {
2408 argv_idx = 0;
2409 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2410 set_option_value((char_u *)"window", n, NULL, 0);
2411 argv_idx = -1;
2412 break;
2413 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002414 /* FALLTHROUGH */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002415 case 'W': /* "-W {scriptout}" overwrite script file */
2416 if (scriptout != NULL)
2417 goto scripterror;
2418 if ((scriptout = mch_fopen(argv[0],
2419 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2420 {
2421 mch_errmsg(_("Cannot open for script output: \""));
2422 mch_errmsg(argv[0]);
2423 mch_errmsg("\"\n");
2424 mch_exit(2);
2425 }
2426 break;
2427
2428#ifdef FEAT_GUI_W32
2429 case 'P': /* "-P {parent title}" MDI parent */
2430 gui_mch_set_parent(argv[0]);
2431 break;
2432#endif
2433 }
2434 }
2435 }
2436
2437 /*
2438 * File name argument.
2439 */
2440 else
2441 {
2442 argv_idx = -1; /* skip to next argument */
2443
2444 /* Check for only one type of editing. */
2445 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2446 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2447 parmp->edit_type = EDIT_FILE;
2448
2449#ifdef MSWIN
2450 /* Remember if the argument was a full path before changing
2451 * slashes to backslashes. */
2452 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2453 parmp->full_path = TRUE;
2454#endif
2455
2456 /* Add the file to the global argument list. */
2457 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2458 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2459 mch_exit(2);
2460#ifdef FEAT_DIFF
2461 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2462 && !mch_isdir(alist_name(&GARGLIST[0])))
2463 {
2464 char_u *r;
2465
2466 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2467 if (r != NULL)
2468 {
2469 vim_free(p);
2470 p = r;
2471 }
2472 }
2473#endif
2474#if defined(__CYGWIN32__) && !defined(WIN32)
2475 /*
2476 * If vim is invoked by non-Cygwin tools, convert away any
2477 * DOS paths, so things like .swp files are created correctly.
2478 * Look for evidence of non-Cygwin paths before we bother.
2479 * This is only for when using the Unix files.
2480 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002481 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002482 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002483 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002484
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002485# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002486 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002487# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002488 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002489# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002490 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002491 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002492 if (p == NULL)
2493 mch_exit(2);
2494 }
2495#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002496
2497#ifdef USE_FNAME_CASE
2498 /* Make the case of the file name match the actual file. */
2499 fname_case(p, 0);
2500#endif
2501
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002502 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002503#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002504 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002505#else
2506 2 /* add buffer number now and use curbuf */
2507#endif
2508 );
2509
2510#if defined(FEAT_MBYTE) && defined(WIN32)
2511 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002512 /* Remember this argument has been added to the argument list.
2513 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002514 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002515# ifdef FEAT_DIFF
2516 parmp->diff_mode
2517# else
2518 FALSE
2519# endif
2520 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002521 }
2522#endif
2523 }
2524
2525 /*
2526 * If there are no more letters after the current "-", go to next
2527 * argument. argv_idx is set to -1 when the current argument is to be
2528 * skipped.
2529 */
2530 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2531 {
2532 --argc;
2533 ++argv;
2534 argv_idx = 1;
2535 }
2536 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002537
2538#ifdef FEAT_EVAL
2539 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2540 * one. */
2541 if (parmp->n_commands > 0)
2542 {
2543 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2544 if (p != NULL)
2545 {
2546 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2547 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2548 vim_free(p);
2549 }
2550 }
2551#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002552}
2553
2554/*
2555 * Print a warning if stdout is not a terminal.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +01002556 * When starting in Ex mode and commands come from a file, set silent_mode.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002557 */
2558 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002559check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002560{
2561 int input_isatty; /* is active input a terminal? */
2562
2563 input_isatty = mch_input_isatty();
2564 if (exmode_active)
2565 {
2566 if (!input_isatty)
2567 silent_mode = TRUE;
2568 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002569 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002570#ifdef FEAT_GUI
2571 /* don't want the delay when started from the desktop */
2572 && !gui.starting
2573#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002574 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002575 {
2576#ifdef NBDEBUG
2577 /*
2578 * This shouldn't be necessary. But if I run netbeans with the log
2579 * output coming to the console and XOpenDisplay fails, I get vim
2580 * trying to start with input/output to my console tty. This fills my
2581 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002582 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002583 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002584 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002585 {
2586 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2587 exit(1);
2588 }
2589#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002590#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2591 if (is_cygpty_used())
2592 {
Bram Moolenaar2a027452017-09-26 19:10:37 +02002593# if defined(FEAT_MBYTE) && defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
2594 && defined(FEAT_GETTEXT)
2595 char *s, *tofree = NULL;
2596
2597 /* Set the encoding of the error message based on $LC_ALL or
2598 * other environment variables instead of 'encoding'.
2599 * Note that the message is shown on a Cygwin terminal (e.g.
2600 * mintty) which encoding is based on $LC_ALL or etc., not the
2601 * current codepage used by normal Win32 console programs. */
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002602 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002603 if (s == NULL)
2604 s = "utf-8"; /* Use "utf-8" by default. */
2605 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2606 vim_free(tofree);
2607# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002608 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2609 exit(1);
2610 }
2611#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002612 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002613 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2614 if (!input_isatty)
2615 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2616 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002617 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2618 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002619 if (scriptin[0] == NULL)
2620 ui_delay(2000L, TRUE);
2621 TIME_MSG("Warning delay");
2622 }
2623}
2624
2625/*
2626 * Read text from stdin.
2627 */
2628 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002629read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002630{
2631 int i;
2632
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002633#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002634 /* When getting the ATTENTION prompt here, use a dialog */
2635 swap_exists_action = SEA_DIALOG;
2636#endif
2637 no_wait_return = TRUE;
2638 i = msg_didany;
2639 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002640 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002641 no_wait_return = FALSE;
2642 msg_didany = i;
2643 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002644#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002645 check_swap_exists_action();
2646#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02002647#if !(defined(AMIGA) || defined(MACOS_X))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002648 /*
2649 * Close stdin and dup it from stderr. Required for GPM to work
2650 * properly, and for running external commands.
2651 * Is there any other system that cannot do this?
2652 */
2653 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002654 vim_ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002655#endif
2656}
2657
2658/*
2659 * Create the requested number of windows and edit buffers in them.
2660 * Also does recovery if "recoverymode" set.
2661 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002662 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002663create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002664{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002665 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002666 int done = 0;
2667
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002668 /*
2669 * Create the number of windows that was requested.
2670 */
2671 if (parmp->window_count == -1) /* was not set */
2672 parmp->window_count = 1;
2673 if (parmp->window_count == 0)
2674 parmp->window_count = GARGCOUNT;
2675 if (parmp->window_count > 1)
2676 {
2677 /* Don't change the windows if there was a command in .vimrc that
2678 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002679 if (parmp->window_layout == 0)
2680 parmp->window_layout = WIN_HOR;
2681 if (parmp->window_layout == WIN_TABS)
2682 {
2683 parmp->window_count = make_tabpages(parmp->window_count);
2684 TIME_MSG("making tab pages");
2685 }
2686 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002687 {
2688 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002689 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002690 TIME_MSG("making windows");
2691 }
2692 else
2693 parmp->window_count = win_count();
2694 }
2695 else
2696 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002697
2698 if (recoverymode) /* do recover */
2699 {
2700 msg_scroll = TRUE; /* scroll message up */
2701 ml_recover();
2702 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2703 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002704 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002705 }
2706 else
2707 {
2708 /*
2709 * Open a buffer for windows that don't have one yet.
2710 * Commands in the .vimrc might have loaded a file or split the window.
2711 * Watch out for autocommands that delete a window.
2712 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002713 /*
2714 * Don't execute Win/Buf Enter/Leave autocommands here
2715 */
2716 ++autocmd_no_enter;
2717 ++autocmd_no_leave;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002718 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002719 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002720 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002721 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002722 {
2723 if (parmp->window_layout == WIN_TABS)
2724 goto_tabpage(1);
2725 else
2726 curwin = firstwin;
2727 }
2728 else if (parmp->window_layout == WIN_TABS)
2729 {
2730 if (curtab->tp_next == NULL)
2731 break;
2732 goto_tabpage(0);
2733 }
2734 else
2735 {
2736 if (curwin->w_next == NULL)
2737 break;
2738 curwin = curwin->w_next;
2739 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002740 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002741 curbuf = curwin->w_buffer;
2742 if (curbuf->b_ml.ml_mfp == NULL)
2743 {
2744#ifdef FEAT_FOLDING
2745 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2746 if (p_fdls >= 0)
2747 curwin->w_p_fdl = p_fdls;
2748#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002749#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002750 /* When getting the ATTENTION prompt here, use a dialog */
2751 swap_exists_action = SEA_DIALOG;
2752#endif
2753 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002754
2755 /* create memfile, read file */
2756 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002757
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002758#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002759 if (swap_exists_action == SEA_QUIT)
2760 {
2761 if (got_int || only_one_window())
2762 {
2763 /* abort selected or quit and only one window */
2764 did_emsg = FALSE; /* avoid hit-enter prompt */
2765 getout(1);
2766 }
2767 /* We can't close the window, it would disturb what
2768 * happens next. Clear the file name and set the arg
2769 * index to -1 to delete it later. */
2770 setfname(curbuf, NULL, NULL, FALSE);
2771 curwin->w_arg_idx = -1;
2772 swap_exists_action = SEA_NONE;
2773 }
2774 else
2775 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002776#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00002777 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002778 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002779 ui_breakcheck();
2780 if (got_int)
2781 {
2782 (void)vgetc(); /* only break the file loading, not the rest */
2783 break;
2784 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002785 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002786 if (parmp->window_layout == WIN_TABS)
2787 goto_tabpage(1);
2788 else
2789 curwin = firstwin;
2790 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002791 --autocmd_no_enter;
2792 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002793 }
2794}
2795
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002796 /*
2797 * If opened more than one window, start editing files in the other
2798 * windows. make_windows() has already opened the windows.
2799 */
2800 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002801edit_buffers(
2802 mparm_T *parmp,
2803 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002804{
2805 int arg_idx; /* index in argument list */
2806 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002807 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002808 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002809
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002810 /*
2811 * Don't execute Win/Buf Enter/Leave autocommands here
2812 */
2813 ++autocmd_no_enter;
2814 ++autocmd_no_leave;
Bram Moolenaar84212822006-11-07 21:59:47 +00002815
2816 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2817 if (curwin->w_arg_idx == -1)
2818 {
2819 win_close(curwin, TRUE);
2820 advance = FALSE;
2821 }
2822
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002823 arg_idx = 1;
2824 for (i = 1; i < parmp->window_count; ++i)
2825 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002826 if (cwd != NULL)
2827 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002828 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2829 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002830 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002831 ++arg_idx;
2832 win_close(curwin, TRUE);
2833 advance = FALSE;
2834 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002835 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002836
Bram Moolenaar84212822006-11-07 21:59:47 +00002837 if (advance)
2838 {
2839 if (parmp->window_layout == WIN_TABS)
2840 {
2841 if (curtab->tp_next == NULL) /* just checking */
2842 break;
2843 goto_tabpage(0);
2844 }
2845 else
2846 {
2847 if (curwin->w_next == NULL) /* just checking */
2848 break;
2849 win_enter(curwin->w_next, FALSE);
2850 }
2851 }
2852 advance = TRUE;
2853
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002854 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002855 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002856 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2857 {
2858 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002859 /* Edit file from arg list, if there is one. When "Quit" selected
2860 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002861# ifdef HAS_SWAP_EXISTS_ACTION
2862 swap_exists_did_quit = FALSE;
2863# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002864 (void)do_ecmd(0, arg_idx < GARGCOUNT
2865 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002866 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002867# ifdef HAS_SWAP_EXISTS_ACTION
2868 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002869 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002870 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002871 if (got_int || only_one_window())
2872 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002873 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002874 did_emsg = FALSE; /* avoid hit-enter prompt */
2875 getout(1);
2876 }
2877 win_close(curwin, TRUE);
2878 advance = FALSE;
2879 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002880# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002881 if (arg_idx == GARGCOUNT - 1)
2882 arg_had_last = TRUE;
2883 ++arg_idx;
2884 }
2885 ui_breakcheck();
2886 if (got_int)
2887 {
2888 (void)vgetc(); /* only break the file loading, not the rest */
2889 break;
2890 }
2891 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002892
2893 if (parmp->window_layout == WIN_TABS)
2894 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002895 --autocmd_no_enter;
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002896
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002897 /* make the first window the current window */
2898 win = firstwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02002899#if defined(FEAT_QUICKFIX)
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002900 /* Avoid making a preview window the current window. */
2901 while (win->w_p_pvw)
2902 {
2903 win = win->w_next;
2904 if (win == NULL)
2905 {
2906 win = firstwin;
2907 break;
2908 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002909 }
2910#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002911 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002912
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002913 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002914 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002915 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002916 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2917}
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002918
2919/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002920 * Execute the commands from --cmd arguments "cmds[cnt]".
2921 */
2922 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002923exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002924{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002925 char_u **cmds = parmp->pre_commands;
2926 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002927 int i;
2928
2929 if (cnt > 0)
2930 {
2931 curwin->w_cursor.lnum = 0; /* just in case.. */
2932 sourcing_name = (char_u *)_("pre-vimrc command line");
2933# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002934 current_sctx.sc_sid = SID_CMDARG;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002935# endif
2936 for (i = 0; i < cnt; ++i)
2937 do_cmdline_cmd(cmds[i]);
2938 sourcing_name = NULL;
2939# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002940 current_sctx.sc_sid = 0;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002941# endif
2942 TIME_MSG("--cmd commands");
2943 }
2944}
2945
2946/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002947 * Execute "+", "-c" and "-S" arguments.
2948 */
2949 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002950exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002951{
2952 int i;
2953
2954 /*
2955 * We start commands on line 0, make "vim +/pat file" match a
2956 * pattern on line 1. But don't move the cursor when an autocommand
2957 * with g`" was used.
2958 */
2959 msg_scroll = TRUE;
2960 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2961 curwin->w_cursor.lnum = 0;
2962 sourcing_name = (char_u *)"command line";
2963#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002964 current_sctx.sc_sid = SID_CARG;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002965 current_sctx.sc_seq = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002966#endif
2967 for (i = 0; i < parmp->n_commands; ++i)
2968 {
2969 do_cmdline_cmd(parmp->commands[i]);
2970 if (parmp->cmds_tofree[i])
2971 vim_free(parmp->commands[i]);
2972 }
2973 sourcing_name = NULL;
2974#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002975 current_sctx.sc_sid = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002976#endif
2977 if (curwin->w_cursor.lnum == 0)
2978 curwin->w_cursor.lnum = 1;
2979
2980 if (!exmode_active)
2981 msg_scroll = FALSE;
2982
2983#ifdef FEAT_QUICKFIX
2984 /* When started with "-q errorfile" jump to first error again. */
2985 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002986 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002987#endif
2988 TIME_MSG("executing command arguments");
2989}
2990
2991/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002992 * Source startup scripts.
2993 */
2994 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002995source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002996{
2997 int i;
2998
2999 /*
3000 * For "evim" source evim.vim first of all, so that the user can overrule
3001 * any things he doesn't like.
3002 */
3003 if (parmp->evim_mode)
3004 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003005 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003006 TIME_MSG("source evim file");
3007 }
3008
3009 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003010 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00003011 * nothing else.
3012 */
3013 if (parmp->use_vimrc != NULL)
3014 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003015 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
3016 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
3017 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003018 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003019 {
3020#ifdef FEAT_GUI
3021 if (use_gvimrc == NULL) /* don't load gvimrc either */
3022 use_gvimrc = parmp->use_vimrc;
3023#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003024 }
3025 else
3026 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003027 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003028 semsg(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003029 }
3030 }
3031 else if (!silent_mode)
3032 {
3033#ifdef AMIGA
3034 struct Process *proc = (struct Process *)FindTask(0L);
3035 APTR save_winptr = proc->pr_WindowPtr;
3036
3037 /* Avoid a requester here for a volume that doesn't exist. */
3038 proc->pr_WindowPtr = (APTR)-1L;
3039#endif
3040
3041 /*
3042 * Get system wide defaults, if the file name is defined.
3043 */
3044#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003045 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003046#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003047#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003048 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003049#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003050
3051 /*
3052 * Try to read initialization commands from the following places:
3053 * - environment variable VIMINIT
3054 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3055 * - second user vimrc file ($VIM/.vimrc for Dos)
3056 * - environment variable EXINIT
3057 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3058 * - second user exrc file ($VIM/.exrc for Dos)
3059 * The first that exists is used, the rest is ignored.
3060 */
3061 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3062 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003063 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003064#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003065 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3066 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003067#endif
3068#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003069 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3070 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003071#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003072#ifdef USR_VIMRC_FILE4
3073 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3074 DOSO_VIMRC) == FAIL
3075#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003076 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003077 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003078#ifdef USR_EXRC_FILE2
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003079 && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003080#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003081 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003082 {
3083 /* When no .vimrc file was found: source defaults.vim. */
3084 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003085 }
3086 }
3087
3088 /*
3089 * Read initialization commands from ".vimrc" or ".exrc" in current
3090 * directory. This is only done if the 'exrc' option is set.
3091 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003092 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003093 * option has been reset in environment of global ".exrc" or ".vimrc".
3094 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3095 * SYS_VIMRC_FILE.
3096 */
3097 if (p_exrc)
3098 {
3099#if defined(UNIX) || defined(VMS)
3100 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3101 if (!file_owned(VIMRC_FILE))
3102#endif
3103 secure = p_secure;
3104
3105 i = FAIL;
3106 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3107 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3108#ifdef USR_VIMRC_FILE2
3109 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3110 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3111#endif
3112#ifdef USR_VIMRC_FILE3
3113 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3114 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3115#endif
3116#ifdef SYS_VIMRC_FILE
3117 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3118 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3119#endif
3120 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003121 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003122
3123 if (i == FAIL)
3124 {
3125#if defined(UNIX) || defined(VMS)
3126 /* if ".exrc" is not owned by user set 'secure' mode */
3127 if (!file_owned(EXRC_FILE))
3128 secure = p_secure;
3129 else
3130 secure = 0;
3131#endif
3132 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3133 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3134#ifdef USR_EXRC_FILE2
3135 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3136 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3137#endif
3138 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003139 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003140 }
3141 }
3142 if (secure == 2)
3143 need_wait_return = TRUE;
3144 secure = 0;
3145#ifdef AMIGA
3146 proc->pr_WindowPtr = save_winptr;
3147#endif
3148 }
3149 TIME_MSG("sourcing vimrc file(s)");
3150}
3151
3152/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003153 * Setup to start using the GUI. Exit with an error when not available.
3154 */
3155 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003156main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003157{
3158#ifdef FEAT_GUI
3159 gui.starting = TRUE; /* start GUI a bit later */
3160#else
3161 mch_errmsg(_(e_nogvim));
3162 mch_errmsg("\n");
3163 mch_exit(2);
3164#endif
3165}
3166
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003167#endif /* NO_VIM_MAIN */
3168
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003169/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003170 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003171 * Returns FAIL if the environment variable was not executed, OK otherwise.
3172 */
3173 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003174process_env(
3175 char_u *env,
3176 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003177{
3178 char_u *initstr;
3179 char_u *save_sourcing_name;
3180 linenr_T save_sourcing_lnum;
3181#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003182 sctx_T save_current_sctx;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003183#endif
3184
3185 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3186 {
3187 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003188 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003189 save_sourcing_name = sourcing_name;
3190 save_sourcing_lnum = sourcing_lnum;
3191 sourcing_name = env;
3192 sourcing_lnum = 0;
3193#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003194 save_current_sctx = current_sctx;
3195 current_sctx.sc_sid = SID_ENV;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003196 current_sctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003197 current_sctx.sc_lnum = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003198#endif
3199 do_cmdline_cmd(initstr);
3200 sourcing_name = save_sourcing_name;
3201 sourcing_lnum = save_sourcing_lnum;
3202#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003203 current_sctx = save_current_sctx;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003204#endif
3205 return OK;
3206 }
3207 return FAIL;
3208}
3209
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003210#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003211/*
3212 * Return TRUE if we are certain the user owns the file "fname".
3213 * Used for ".vimrc" and ".exrc".
3214 * Use both stat() and lstat() for extra security.
3215 */
3216 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003217file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003218{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003219 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003220# ifdef UNIX
3221 uid_t uid = getuid();
3222# else /* VMS */
3223 uid_t uid = ((getgid() << 16) | getuid());
3224# endif
3225
3226 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3227# ifdef HAVE_LSTAT
3228 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3229# endif
3230 );
3231}
3232#endif
3233
3234/*
3235 * Give an error message main_errors["n"] and exit.
3236 */
3237 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003238mainerr(
3239 int n, /* one of the ME_ defines */
3240 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003241{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003242#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003243 reset_signals(); /* kill us with CTRL-C here, if you like */
3244#endif
3245
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003246 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003247 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003248 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003249 mch_errmsg(_(main_errors[n]));
3250 if (str != NULL)
3251 {
3252 mch_errmsg(": \"");
3253 mch_errmsg((char *)str);
3254 mch_errmsg("\"");
3255 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003256 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003257
3258 mch_exit(1);
3259}
3260
3261 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003262mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003263{
3264 mainerr(ME_ARG_MISSING, str);
3265}
3266
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003267#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003268/*
3269 * print a message with three spaces prepended and '\n' appended.
3270 */
3271 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003272main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003273{
3274 mch_msg(" ");
3275 mch_msg(s);
3276 mch_msg("\n");
3277}
3278
3279/*
3280 * Print messages for "vim -h" or "vim --help" and exit.
3281 */
3282 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003283usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003284{
3285 int i;
3286 static char *(use[]) =
3287 {
3288 N_("[file ..] edit specified file(s)"),
3289 N_("- read text from stdin"),
3290 N_("-t tag edit file where tag is defined"),
3291#ifdef FEAT_QUICKFIX
3292 N_("-q [errorfile] edit file with first error")
3293#endif
3294 };
3295
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003296#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003297 reset_signals(); /* kill us with CTRL-C here, if you like */
3298#endif
3299
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003300 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003301 mch_msg(longVersion);
Bram Moolenaar32aaf5a2018-06-21 21:38:33 +02003302 mch_msg(_("\n\nUsage:"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003303 for (i = 0; ; ++i)
3304 {
3305 mch_msg(_(" vim [arguments] "));
3306 mch_msg(_(use[i]));
3307 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3308 break;
3309 mch_msg(_("\n or:"));
3310 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003311#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003312 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003313#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003314
3315 mch_msg(_("\n\nArguments:\n"));
3316 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003317#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003318 main_msg(_("--literal\t\tDon't expand wildcards"));
3319#endif
3320#ifdef FEAT_OLE
3321 main_msg(_("-register\t\tRegister this gvim for OLE"));
3322 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3323#endif
3324#ifdef FEAT_GUI
3325 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3326 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3327#endif
3328 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3329 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003330 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003331 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3332#ifdef FEAT_DIFF
3333 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3334#endif
3335 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3336 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3337 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3338 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3339 main_msg(_("-M\t\t\tModifications in text not allowed"));
3340 main_msg(_("-b\t\t\tBinary mode"));
3341#ifdef FEAT_LISP
3342 main_msg(_("-l\t\t\tLisp mode"));
3343#endif
3344 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3345 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003346 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3347#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003348 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003349#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003350 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3351 main_msg(_("-r\t\t\tList swap files and exit"));
3352 main_msg(_("-r (with file name)\tRecover crashed session"));
3353 main_msg(_("-L\t\t\tSame as -r"));
3354#ifdef AMIGA
3355 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3356 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3357#endif
3358#ifdef FEAT_ARABIC
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003359 main_msg(_("-A\t\t\tStart in Arabic mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003360#endif
3361#ifdef FEAT_RIGHTLEFT
3362 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3363#endif
3364#ifdef FEAT_FKMAP
3365 main_msg(_("-F\t\t\tStart in Farsi mode"));
3366#endif
3367 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003368 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003369 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003370 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3371#ifdef FEAT_GUI
3372 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3373#endif
3374 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003375 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003376 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3377 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3378 main_msg(_("+\t\t\tStart at end of file"));
3379 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003380 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003381 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3382 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3383 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3384 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3385 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3386#ifdef FEAT_CRYPT
3387 main_msg(_("-x\t\t\tEdit encrypted files"));
3388#endif
3389#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3390# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3391 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3392# endif
3393 main_msg(_("-X\t\t\tDo not connect to X server"));
3394#endif
3395#ifdef FEAT_CLIENTSERVER
3396 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3397 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3398 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3399 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003400 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003401 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3402 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3403 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3404 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3405#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003406#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003407 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003408#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003409#ifdef FEAT_VIMINFO
3410 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3411#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003412 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003413 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3414 main_msg(_("--version\t\tPrint version information and exit"));
3415
3416#ifdef FEAT_GUI_X11
3417# ifdef FEAT_GUI_MOTIF
3418 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3419# else
3420# ifdef FEAT_GUI_ATHENA
3421# ifdef FEAT_GUI_NEXTAW
3422 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3423# else
3424 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3425# endif
3426# endif
3427# endif
3428 main_msg(_("-display <display>\tRun vim on <display>"));
3429 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003430 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3431 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3432 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3433 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3434 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3435 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3436 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3437 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3438# ifdef FEAT_GUI_ATHENA
3439 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3440# endif
3441 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3442 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3443 main_msg(_("-xrm <resource>\tSet the specified resource"));
3444#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003445#ifdef FEAT_GUI_GTK
3446 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3447 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3448 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3449 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3450 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003451 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003452 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003453 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003454#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003455#ifdef FEAT_GUI_W32
3456 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003457 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003458#endif
3459
3460#ifdef FEAT_GUI_GNOME
3461 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3462 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003463 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003464 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003465 gui.dofork = FALSE;
3466 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003467 else
3468#endif
3469 mch_exit(0);
3470}
3471
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003472#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003473/*
3474 * Check the result of the ATTENTION dialog:
3475 * When "Quit" selected, exit Vim.
3476 * When "Recover" selected, recover the file.
3477 */
3478 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003479check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003480{
3481 if (swap_exists_action == SEA_QUIT)
3482 getout(1);
3483 handle_swap_exists(NULL);
3484}
3485#endif
3486
Bram Moolenaar08cab962017-03-04 14:37:18 +01003487#endif /* NO_VIM_MAIN */
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003488
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003489#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003490static struct timeval prev_timeval;
3491
Bram Moolenaar3f269672009-11-03 11:11:11 +00003492# ifdef WIN3264
3493/*
3494 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3495 */
3496 static int
3497gettimeofday(struct timeval *tv, char *dummy)
3498{
3499 long t = clock();
3500 tv->tv_sec = t / CLOCKS_PER_SEC;
3501 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3502 return 0;
3503}
3504# endif
3505
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003506/*
3507 * Save the previous time before doing something that could nest.
3508 * set "*tv_rel" to the time elapsed so far.
3509 */
3510 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003511time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003512{
3513 *((struct timeval *)tv_rel) = prev_timeval;
3514 gettimeofday(&prev_timeval, NULL);
3515 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3516 - ((struct timeval *)tv_rel)->tv_usec;
3517 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3518 - ((struct timeval *)tv_rel)->tv_sec;
3519 if (((struct timeval *)tv_rel)->tv_usec < 0)
3520 {
3521 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3522 --((struct timeval *)tv_rel)->tv_sec;
3523 }
3524 *(struct timeval *)tv_start = prev_timeval;
3525}
3526
3527/*
3528 * Compute the previous time after doing something that could nest.
3529 * Subtract "*tp" from prev_timeval;
3530 * Note: The arguments are (void *) to avoid trouble with systems that don't
3531 * have struct timeval.
3532 */
3533 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003534time_pop(
3535 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003536{
3537 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3538 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3539 if (prev_timeval.tv_usec < 0)
3540 {
3541 prev_timeval.tv_usec += 1000000;
3542 --prev_timeval.tv_sec;
3543 }
3544}
3545
3546 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003547time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003548{
3549 long usec;
3550 long msec;
3551
3552 usec = now->tv_usec - then->tv_usec;
3553 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3554 usec = usec % 1000L;
3555 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3556}
3557
3558 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003559time_msg(
3560 char *mesg,
3561 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003562 (struct timeval *) */
3563{
3564 static struct timeval start;
3565 struct timeval now;
3566
3567 if (time_fd != NULL)
3568 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003569 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003570 {
3571 gettimeofday(&start, NULL);
3572 prev_timeval = start;
3573 fprintf(time_fd, "\n\ntimes in msec\n");
3574 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3575 fprintf(time_fd, " clock elapsed: other lines\n\n");
3576 }
3577 gettimeofday(&now, NULL);
3578 time_diff(&start, &now);
3579 if (((struct timeval *)tv_start) != NULL)
3580 {
3581 fprintf(time_fd, " ");
3582 time_diff(((struct timeval *)tv_start), &now);
3583 }
3584 fprintf(time_fd, " ");
3585 time_diff(&prev_timeval, &now);
3586 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003587 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003588 }
3589}
3590
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003591#endif
3592
Bram Moolenaar595297d2017-03-04 19:11:12 +01003593#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003594 static void
3595set_progpath(char_u *argv0)
3596{
3597 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003598
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003599# if defined(WIN32)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003600 /* A relative path containing a "/" will become invalid when using ":cd",
3601 * turn it into a full path.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003602 * On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3603 * this. */
Bram Moolenaar066029e2017-03-05 15:19:32 +01003604 char_u *path = NULL;
3605
3606 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3607 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003608# else
3609 char_u buf[MAXPATHL + 1];
3610# ifdef PROC_EXE_LINK
3611 char linkbuf[MAXPATHL + 1];
3612 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003613
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003614 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3615 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003616 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003617 linkbuf[len] = NUL;
3618 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003619 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003620# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003621
3622 if (!mch_isFullName(val))
3623 {
3624 if (gettail(val) != val
3625 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3626 val = buf;
3627 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003628# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003629
Bram Moolenaar08cab962017-03-04 14:37:18 +01003630 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003631
Bram Moolenaar066029e2017-03-05 15:19:32 +01003632# ifdef WIN32
Bram Moolenaar43663192017-03-05 14:29:12 +01003633 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003634# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003635}
3636
3637#endif /* NO_VIM_MAIN */
3638
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003639#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003640
3641/*
3642 * Common code for the X command server and the Win32 command server.
3643 */
3644
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003645static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003646
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003647/*
3648 * Do the client-server stuff, unless "--servername ''" was used.
3649 */
3650 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003651exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003652{
3653 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3654 {
3655# ifdef WIN32
3656 /* Initialise the client/server messaging infrastructure. */
3657 serverInitMessaging();
3658# endif
3659
3660 /*
3661 * When a command server argument was found, execute it. This may
3662 * exit Vim when it was successful. Otherwise it's executed further
3663 * on. Remember the encoding used here in "serverStrEnc".
3664 */
3665 if (parmp->serverArg)
3666 {
3667 cmdsrv_main(&parmp->argc, parmp->argv,
3668 parmp->serverName_arg, &parmp->serverStr);
3669# ifdef FEAT_MBYTE
3670 parmp->serverStrEnc = vim_strsave(p_enc);
3671# endif
3672 }
3673
3674 /* If we're still running, get the name to register ourselves.
3675 * On Win32 can register right now, for X11 need to setup the
3676 * clipboard first, it's further down. */
3677 parmp->servername = serverMakeName(parmp->serverName_arg,
3678 parmp->argv[0]);
3679# ifdef WIN32
3680 if (parmp->servername != NULL)
3681 {
3682 serverSetName(parmp->servername);
3683 vim_free(parmp->servername);
3684 }
3685# endif
3686 }
3687}
3688
3689/*
3690 * Prepare for running as a Vim server.
3691 */
3692 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003693prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003694{
3695# if defined(FEAT_X11)
3696 /*
3697 * Register for remote command execution with :serversend and --remote
3698 * unless there was a -X or a --servername '' on the command line.
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003699 * Only register nongui-vim's with an explicit --servername argument,
3700 * or when compiling with autoservername.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003701 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003702 */
3703 if (X_DISPLAY != NULL && parmp->servername != NULL && (
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003704# if defined(FEAT_AUTOSERVERNAME) || defined(FEAT_GUI)
3705 (
3706# if defined(FEAT_AUTOSERVERNAME)
3707 1
3708# else
3709 gui.in_use
3710# endif
Bram Moolenaar5f402312006-08-15 19:40:35 +00003711# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003712 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003713# endif
3714 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003715# endif
3716 parmp->serverName_arg != NULL))
3717 {
3718 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3719 vim_free(parmp->servername);
3720 TIME_MSG("register server name");
3721 }
3722 else
3723 serverDelayedStartName = parmp->servername;
3724# endif
3725
3726 /*
3727 * Execute command ourselves if we're here because the send failed (or
3728 * else we would have exited above).
3729 */
3730 if (parmp->serverStr != NULL)
3731 {
3732 char_u *p;
3733
3734 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3735 parmp->serverStr, &p));
3736 vim_free(p);
3737 }
3738}
3739
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003740 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003741cmdsrv_main(
3742 int *argc,
3743 char **argv,
3744 char_u *serverName_arg,
3745 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003746{
3747 char_u *res;
3748 int i;
3749 char_u *sname;
3750 int ret;
3751 int didone = FALSE;
3752 int exiterr = 0;
3753 char **newArgV = argv + 1;
3754 int newArgC = 1,
3755 Argc = *argc;
3756 int argtype;
3757#define ARGTYPE_OTHER 0
3758#define ARGTYPE_EDIT 1
3759#define ARGTYPE_EDIT_WAIT 2
3760#define ARGTYPE_SEND 3
3761 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003762 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003763# ifndef FEAT_X11
3764 HWND srv;
3765# else
3766 Window srv;
3767
3768 setup_term_clip();
3769# endif
3770
3771 sname = serverMakeName(serverName_arg, argv[0]);
3772 if (sname == NULL)
3773 return;
3774
3775 /*
3776 * Execute the command server related arguments and remove them
3777 * from the argc/argv array; We may have to return into main()
3778 */
3779 for (i = 1; i < Argc; i++)
3780 {
3781 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003782 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003783 {
3784 for (; i < *argc; i++)
3785 {
3786 *newArgV++ = argv[i];
3787 newArgC++;
3788 }
3789 break;
3790 }
3791
Bram Moolenaareb94e552006-03-11 21:35:11 +00003792 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003793 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003794 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3795 {
3796 char *p = argv[i] + 8;
3797
3798 argtype = ARGTYPE_EDIT;
3799 while (*p != NUL)
3800 {
3801 if (STRNICMP(p, "-wait", 5) == 0)
3802 {
3803 argtype = ARGTYPE_EDIT_WAIT;
3804 p += 5;
3805 }
3806 else if (STRNICMP(p, "-silent", 7) == 0)
3807 {
3808 silent = TRUE;
3809 p += 7;
3810 }
3811 else if (STRNICMP(p, "-tab", 4) == 0)
3812 {
3813 tabs = TRUE;
3814 p += 4;
3815 }
3816 else
3817 {
3818 argtype = ARGTYPE_OTHER;
3819 break;
3820 }
3821 }
3822 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003823 else
3824 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003825
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003826 if (argtype != ARGTYPE_OTHER)
3827 {
3828 if (i == *argc - 1)
3829 mainerr_arg_missing((char_u *)argv[i]);
3830 if (argtype == ARGTYPE_SEND)
3831 {
3832 *serverStr = (char_u *)argv[i + 1];
3833 i++;
3834 }
3835 else
3836 {
3837 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003838 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003839 if (*serverStr == NULL)
3840 {
3841 /* Probably out of memory, exit. */
3842 didone = TRUE;
3843 exiterr = 1;
3844 break;
3845 }
3846 Argc = i;
3847 }
3848# ifdef FEAT_X11
3849 if (xterm_dpy == NULL)
3850 {
3851 mch_errmsg(_("No display"));
3852 ret = -1;
3853 }
3854 else
3855 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003856 NULL, &srv, 0, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003857# else
3858 /* Win32 always works? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003859 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003860# endif
3861 if (ret < 0)
3862 {
3863 if (argtype == ARGTYPE_SEND)
3864 {
3865 /* Failed to send, abort. */
3866 mch_errmsg(_(": Send failed.\n"));
3867 didone = TRUE;
3868 exiterr = 1;
3869 }
3870 else if (!silent)
3871 /* Let vim start normally. */
3872 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3873 break;
3874 }
3875
3876# ifdef FEAT_GUI_W32
3877 /* Guess that when the server name starts with "g" it's a GUI
3878 * server, which we can bring to the foreground here.
3879 * Foreground() in the server doesn't work very well. */
3880 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3881 SetForegroundWindow(srv);
3882# endif
3883
3884 /*
3885 * For --remote-wait: Wait until the server did edit each
3886 * file. Also detect that the server no longer runs.
3887 */
3888 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3889 {
3890 int numFiles = *argc - i - 1;
3891 int j;
3892 char_u *done = alloc(numFiles);
3893 char_u *p;
3894# ifdef FEAT_GUI_W32
3895 NOTIFYICONDATA ni;
3896 int count = 0;
3897 extern HWND message_window;
3898# endif
3899
3900 if (numFiles > 0 && argv[i + 1][0] == '+')
3901 /* Skip "+cmd" argument, don't wait for it to be edited. */
3902 --numFiles;
3903
3904# ifdef FEAT_GUI_W32
3905 ni.cbSize = sizeof(ni);
3906 ni.hWnd = message_window;
3907 ni.uID = 0;
3908 ni.uFlags = NIF_ICON|NIF_TIP;
3909 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3910 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3911 Shell_NotifyIcon(NIM_ADD, &ni);
3912# endif
3913
3914 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003915 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003916 while (memchr(done, 0, numFiles) != NULL)
3917 {
3918# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003919 p = serverGetReply(srv, NULL, TRUE, TRUE, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003920 if (p == NULL)
3921 break;
3922# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003923 if (serverReadReply(xterm_dpy, srv, &p, TRUE, -1) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003924 break;
3925# endif
3926 j = atoi((char *)p);
3927 if (j >= 0 && j < numFiles)
3928 {
3929# ifdef FEAT_GUI_W32
3930 ++count;
3931 sprintf(ni.szTip, _("%d of %d edited"),
3932 count, numFiles);
3933 Shell_NotifyIcon(NIM_MODIFY, &ni);
3934# endif
3935 done[j] = 1;
3936 }
3937 }
3938# ifdef FEAT_GUI_W32
3939 Shell_NotifyIcon(NIM_DELETE, &ni);
3940# endif
3941 }
3942 }
3943 else if (STRICMP(argv[i], "--remote-expr") == 0)
3944 {
3945 if (i == *argc - 1)
3946 mainerr_arg_missing((char_u *)argv[i]);
3947# ifdef WIN32
3948 /* Win32 always works? */
3949 if (serverSendToVim(sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003950 &res, NULL, 1, 0, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003951# else
3952 if (xterm_dpy == NULL)
3953 mch_errmsg(_("No display: Send expression failed.\n"));
3954 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003955 &res, NULL, 1, 0, 1, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003956# endif
3957 {
3958 if (res != NULL && *res != NUL)
3959 {
3960 /* Output error from remote */
3961 mch_errmsg((char *)res);
Bram Moolenaard23a8232018-02-10 18:45:26 +01003962 VIM_CLEAR(res);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003963 }
3964 mch_errmsg(_(": Send expression failed.\n"));
3965 }
3966 }
3967 else if (STRICMP(argv[i], "--serverlist") == 0)
3968 {
3969# ifdef WIN32
3970 /* Win32 always works? */
3971 res = serverGetVimNames();
3972# else
3973 if (xterm_dpy != NULL)
3974 res = serverGetVimNames(xterm_dpy);
3975# endif
3976 if (called_emsg)
3977 mch_errmsg("\n");
3978 }
3979 else if (STRICMP(argv[i], "--servername") == 0)
3980 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003981 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003982 i++;
3983 continue;
3984 }
3985 else
3986 {
3987 *newArgV++ = argv[i];
3988 newArgC++;
3989 continue;
3990 }
3991 didone = TRUE;
3992 if (res != NULL && *res != NUL)
3993 {
3994 mch_msg((char *)res);
3995 if (res[STRLEN(res) - 1] != '\n')
3996 mch_msg("\n");
3997 }
3998 vim_free(res);
3999 }
4000
4001 if (didone)
4002 {
4003 display_errors(); /* display any collected messages */
4004 exit(exiterr); /* Mission accomplished - get out */
4005 }
4006
4007 /* Return back into main() */
4008 *argc = newArgC;
4009 vim_free(sname);
4010}
4011
4012/*
4013 * Build a ":drop" command to send to a Vim server.
4014 */
4015 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004016build_drop_cmd(
4017 int filec,
4018 char **filev,
4019 int tabs, /* Use ":tab drop" instead of ":drop". */
4020 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004021{
4022 garray_T ga;
4023 int i;
4024 char_u *inicmd = NULL;
4025 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004026 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004027 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004028
4029 if (filec > 0 && filev[0][0] == '+')
4030 {
4031 inicmd = (char_u *)filev[0] + 1;
4032 filev++;
4033 filec--;
4034 }
4035 /* Check if we have at least one argument. */
4036 if (filec <= 0)
4037 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004038
4039 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02004040 cwd = alloc(MAXPATHL);
4041 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004042 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004043 if (mch_dirname(cwd, MAXPATHL) != OK)
4044 {
4045 vim_free(cwd);
4046 return NULL;
4047 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004048 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00004049#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004050 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00004051#else
4052 PATH_ESC_CHARS,
4053#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02004054 '\\', TRUE);
4055 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004056 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004057 return NULL;
4058 ga_init2(&ga, 1, 100);
4059 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004060 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00004061
4062 /* Call inputsave() so that a prompt for an encryption key works. */
4063 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
4064 if (tabs)
4065 ga_concat(&ga, (char_u *)"tab ");
4066 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004067 for (i = 0; i < filec; i++)
4068 {
4069 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004070 * do it again in the Vim server. On MS-Windows only escape
4071 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004072 p = vim_strsave_escaped((char_u *)filev[i],
4073#ifdef UNIX
4074 PATH_ESC_CHARS
4075#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004076 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004077#endif
4078 );
4079 if (p == NULL)
4080 {
4081 vim_free(ga.ga_data);
4082 return NULL;
4083 }
4084 ga_concat(&ga, (char_u *)" ");
4085 ga_concat(&ga, p);
4086 vim_free(p);
4087 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004088 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4089
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004090 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4091 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004092 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4093
4094 /* Switch back to the correct current directory (prior to temporary path
4095 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004096 * correct after the :drop command. With line breaks and spaces:
4097 * if !exists('+acd') || !&acd
4098 * if haslocaldir()
4099 * cd -
4100 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004101 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004102 * cd -
4103 * endif
4104 * endif
4105 */
4106 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004107 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004108 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004109 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004110 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004111
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004112 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004113 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4114 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004115 if (inicmd != NULL)
4116 {
4117 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4118 * the following commands to be inserted as text. Use a "|",
4119 * hopefully "inicmd" does allow this... */
4120 ga_concat(&ga, inicmd);
4121 ga_concat(&ga, (char_u *)"|");
4122 }
4123 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4124 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004125 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004126 ga_append(&ga, NUL);
4127 return ga.ga_data;
4128}
4129
4130/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004131 * Make our basic server name: use the specified "arg" if given, otherwise use
4132 * the tail of the command "cmd" we were started with.
4133 * Return the name in allocated memory. This doesn't include a serial number.
4134 */
4135 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004136serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004137{
4138 char_u *p;
4139
4140 if (arg != NULL && *arg != NUL)
4141 p = vim_strsave_up(arg);
4142 else
4143 {
4144 p = vim_strsave_up(gettail((char_u *)cmd));
4145 /* Remove .exe or .bat from the name. */
4146 if (p != NULL && vim_strchr(p, '.') != NULL)
4147 *vim_strchr(p, '.') = NUL;
4148 }
4149 return p;
4150}
4151#endif /* FEAT_CLIENTSERVER */
4152
4153#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4154/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004155 * Replace termcodes such as <CR> and insert as key presses if there is room.
4156 */
4157 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004158server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004159{
4160 char_u *ptr = NULL;
4161 char_u *cpo_save = p_cpo;
4162
4163 /* Set 'cpoptions' the way we want it.
4164 * B set - backslashes are *not* treated specially
4165 * k set - keycodes are *not* reverse-engineered
4166 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004167 * The last but one parameter of replace_termcodes() is TRUE so that the
4168 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004169 */
4170 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004171 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004172 p_cpo = cpo_save;
4173
4174 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4175 {
4176 /*
4177 * Add the string to the input stream.
4178 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4179 *
4180 * First clear typed characters from the typeahead buffer, there could
4181 * be half a mapping there. Then append to the existing string, so
4182 * that multiple commands from a client are concatenated.
4183 */
4184 if (typebuf.tb_maplen < typebuf.tb_len)
4185 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4186 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4187
4188 /* Let input_available() know we inserted text in the typeahead
4189 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004190 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004191 }
4192 vim_free((char_u *)ptr);
4193}
4194
4195/*
4196 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004197 */
4198 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004199eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004200{
4201 char_u *res;
4202 int save_dbl = debug_break_level;
4203 int save_ro = redir_off;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02004204 funccal_entry_T funccal_entry;
4205 int did_save_funccal = FALSE;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004206
4207 /* Evaluate the expression at the toplevel, don't use variables local to
Bram Moolenaard99388b2017-10-26 14:28:32 +02004208 * the calling function. Except when in debug mode. */
4209 if (!debug_mode)
Bram Moolenaar27e80c82018-10-14 21:41:01 +02004210 {
4211 save_funccal(&funccal_entry);
4212 did_save_funccal = TRUE;
4213 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004214
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004215 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4216 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004217 debug_break_level = -1;
4218 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004219 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4220 * to be typed. Do generate errors so that try/catch works. */
4221 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004222
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004223 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004224
4225 debug_break_level = save_dbl;
4226 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004227 --emsg_silent;
4228 if (emsg_silent < 0)
4229 emsg_silent = 0;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02004230 if (did_save_funccal)
4231 restore_funccal();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004232
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004233 /* A client can tell us to redraw, but not to display the cursor, so do
4234 * that here. */
4235 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01004236 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004237
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004238 return res;
4239}
4240
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004241/*
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004242 * Evaluate a command or expression sent to ourselves.
4243 */
4244 int
4245sendToLocalVim(char_u *cmd, int asExpr, char_u **result)
4246{
4247 if (asExpr)
4248 {
4249 char_u *ret;
4250
4251 ret = eval_client_expr_to_string(cmd);
4252 if (result != NULL)
4253 {
4254 if (ret == NULL)
4255 {
4256 char *err = _(e_invexprmsg);
4257 size_t len = STRLEN(cmd) + STRLEN(err) + 5;
4258 char_u *msg;
4259
Bram Moolenaar1662ce12017-03-19 21:47:50 +01004260 msg = alloc((unsigned)len);
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004261 if (msg != NULL)
4262 vim_snprintf((char *)msg, len, "%s: \"%s\"", err, cmd);
4263 *result = msg;
4264 }
4265 else
4266 *result = ret;
4267 }
4268 else
4269 vim_free(ret);
4270 return ret == NULL ? -1 : 0;
4271 }
4272 server_to_input_buf(cmd);
4273 return 0;
4274}
4275
4276/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004277 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4278 * return an allocated string. Otherwise return "data".
4279 * "*tofree" is set to the result when it needs to be freed later.
4280 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004281 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004282serverConvert(
4283 char_u *client_enc UNUSED,
4284 char_u *data,
4285 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004286{
4287 char_u *res = data;
4288
4289 *tofree = NULL;
4290# ifdef FEAT_MBYTE
4291 if (client_enc != NULL && p_enc != NULL)
4292 {
4293 vimconv_T vimconv;
4294
4295 vimconv.vc_type = CONV_NONE;
4296 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4297 && vimconv.vc_type != CONV_NONE)
4298 {
4299 res = string_convert(&vimconv, data, NULL);
4300 if (res == NULL)
4301 res = data;
4302 else
4303 *tofree = res;
4304 }
4305 convert_setup(&vimconv, NULL, NULL);
4306 }
4307# endif
4308 return res;
4309}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004310#endif