blob: 6d30143a3c6a393bb18bfd9bdcede22b2f3987bc [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__
Bram Moolenaar4f974752019-02-17 17:44:42 +010014# ifndef MSWIN
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 Moolenaarafde13b2019-04-28 19:46:49 +020022#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar97ff9b92016-06-26 20:37:46 +020023# 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 Moolenaar92b8b2d2016-01-29 22:36:45 +010053static void check_swap_exists_action(void);
Bram Moolenaar08cab962017-03-04 14:37:18 +010054# ifdef FEAT_EVAL
55static void set_progpath(char_u *argv0);
56# endif
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010057# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010058static void exec_on_server(mparm_T *parmp);
59static void prepare_server(mparm_T *parmp);
60static void cmdsrv_main(int *argc, char **argv, char_u *serverName_arg, char_u **serverStr);
61static char_u *serverMakeName(char_u *arg, char *cmd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010062# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000063#endif
64
65
Bram Moolenaarb4210b32004-06-13 14:51:16 +000066/*
67 * Different types of error messages.
68 */
69static char *(main_errors[]) =
70{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000071 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000072#define ME_UNKNOWN_OPTION 0
73 N_("Too many edit arguments"),
74#define ME_TOO_MANY_ARGS 1
75 N_("Argument missing after"),
76#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +000077 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000078#define ME_GARBAGE 3
79 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
80#define ME_EXTRA_CMD 4
81 N_("Invalid argument for"),
82#define ME_INVALID_ARG 5
83};
84
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010085#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaara6044292017-04-02 18:19:53 +020086
87/* Various parameters passed between main() and other functions. */
88static mparm_T params;
89
Bram Moolenaar8866d272012-11-28 15:55:42 +010090#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020091
92static char_u *start_dir = NULL; /* current working dir on startup */
93
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +020094static int has_dash_c_arg = FALSE;
95
Bram Moolenaarafde13b2019-04-28 19:46:49 +020096# ifdef VIMDLL
97__declspec(dllexport)
98# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000099 int
Bram Moolenaar796cc422019-04-03 20:31:00 +0200100# ifdef MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000101# ifdef __BORLANDC__
102_cdecl
103# endif
104VimMain
105# else
106main
107# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100108(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000109{
Bram Moolenaar77780b62018-03-01 23:10:45 +0100110#if defined(STARTUPTIME) || defined(CLEAN_RUNTIMEPATH)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000111 int i;
112#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000113
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000114 /*
115 * Do any system-specific initialisations. These can NOT use IObuff or
116 * NameBuff. Thus emsg2() cannot be called!
117 */
118 mch_early_init();
119
Bram Moolenaar4f974752019-02-17 17:44:42 +0100120#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +0200121 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200122 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200123 * convert when 'encoding' changes. Get the unexpanded arguments.
124 */
125 argc = get_cmd_argsW(&argv);
126#endif
127
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000128 /* Many variables are in "params" so that we can pass them to invoked
129 * functions without a lot of arguments. "argc" and "argv" are also
130 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000131 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000132 params.argc = argc;
133 params.argv = argv;
134 params.want_full_screen = TRUE;
135#ifdef FEAT_EVAL
136 params.use_debug_break_level = -1;
137#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000138 params.window_count = -1;
Bram Moolenaar58d98232005-07-23 22:25:46 +0000139
Bram Moolenaar99685e62013-05-11 13:56:18 +0200140#ifdef FEAT_RUBY
141 {
142 int ruby_stack_start;
143 vim_ruby_init((void *)&ruby_stack_start);
144 }
145#endif
146
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000147#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000148 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000149#endif
150
151#ifdef MEM_PROFILE
152 atexit(vim_mem_profile_dump);
153#endif
154
155#ifdef STARTUPTIME
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200156 /* Need to find "--startuptime" before actually parsing arguments. */
Bram Moolenaar07268702018-03-01 21:57:32 +0100157 for (i = 1; i < argc - 1; ++i)
158 if (STRICMP(argv[i], "--startuptime") == 0)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000159 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000160 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000161 TIME_MSG("--- VIM STARTING ---");
162 break;
163 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000164#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000165 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000166
Bram Moolenaar07268702018-03-01 21:57:32 +0100167#ifdef CLEAN_RUNTIMEPATH
168 /* Need to find "--clean" before actually parsing arguments. */
169 for (i = 1; i < argc; ++i)
170 if (STRICMP(argv[i], "--clean") == 0)
171 {
172 params.clean = TRUE;
173 break;
174 }
175#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200176 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000177
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200178#ifdef VIMDLL
179 // Check if the current executable file is for the GUI subsystem.
180 gui.starting = mch_is_gui_executable();
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +0200181#elif defined(FEAT_GUI_MSWIN)
182 gui.starting = TRUE;
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200183#endif
184
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000185#ifdef FEAT_CLIENTSERVER
186 /*
187 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000188 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000189 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000190 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000191#endif
192
193 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000194 * Figure out the way to work from the command name argv[0].
195 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000196 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000197 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000198
199 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000200 * Process the command line arguments. File names are put in the global
201 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000202 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000203 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000204 TIME_MSG("parsing arguments");
205
206 /*
207 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000208 * there is no terminal version, and on Windows we can't fork one off with
209 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000210 */
211#ifdef ALWAYS_USE_GUI
212 gui.starting = TRUE;
213#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000214# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000215 /*
216 * Check if the GUI can be started. Reset gui.starting if not.
217 * Don't know about other systems, stay on the safe side and don't check.
218 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000219 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000220 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000221 if (gui_init_check() == FAIL)
222 {
223 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000224
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000225 /* When running "evim" or "gvim -y" we need the menus, exit if we
226 * don't have them. */
227 if (params.evim_mode)
228 mch_exit(1);
229 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000230 }
231# endif
232#endif
233
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000234 if (GARGCOUNT > 0)
235 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100236#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000237 /*
238 * Expand wildcards in file names.
239 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000240 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000241 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200242 start_dir = alloc(MAXPATHL);
243 if (start_dir != NULL)
244 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000245 /* Temporarily add '(' and ')' to 'isfname'. These are valid
246 * filename characters but are excluded from 'isfname' to make
247 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
248 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000249 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000250 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200251 if (start_dir != NULL)
252 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000253 }
254#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200255 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000256 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000257
Bram Moolenaar4f974752019-02-17 17:44:42 +0100258#ifdef MSWIN
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000259 {
260 extern void set_alist_count(void);
261
262 /* Remember the number of entries in the argument list. If it changes
263 * we don't react on setting 'encoding'. */
264 set_alist_count();
265 }
266#endif
267
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000268#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000269 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000270 {
271 /*
272 * If there is one filename, fully qualified, we have very probably
273 * been invoked from explorer, so change to the file's directory.
274 * Hint: to avoid this when typing a command use a forward slash.
275 * If the cd fails, it doesn't matter.
276 */
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100277 (void)vim_chdirfile(params.fname, "drop");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200278 if (start_dir != NULL)
279 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000280 }
281#endif
282 TIME_MSG("expanding arguments");
283
284#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000285 if (params.diff_mode && params.window_count == -1)
286 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000287#endif
288
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000289 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000290 ++RedrawingDisabled;
291
292 /*
293 * When listing swap file names, don't do cursor positioning et. al.
294 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200295 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000296 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000297
298 /*
299 * When certain to start the GUI, don't check capabilities of terminal.
300 * For GTK we can't be sure, but when started from the desktop it doesn't
301 * make sense to try using a terminal.
302 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200303#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
304 || defined(VIMDLL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000305 if (gui.starting
306# ifdef FEAT_GUI_GTK
307 && !isatty(2)
308# endif
309 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000310 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000311#endif
312
Bram Moolenaard0573012017-10-28 21:11:06 +0200313#if defined(FEAT_GUI_MAC) && defined(MACOS_X_DARWIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000314 /* When the GUI is started from Finder, need to display messages in a
315 * message box. isatty(2) returns TRUE anyway, thus we need to check the
316 * name to know we're not started from a terminal. */
317 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000318 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000319 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000320
321 /* Avoid always using "/" as the current directory. Note that when
322 * started from Finder the arglist will be filled later in
323 * HandleODocAE() and "fname" will be NULL. */
324 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
325 && STRCMP(NameBuff, "/") == 0)
326 {
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200327 if (params.fname != NULL)
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100328 (void)vim_chdirfile(params.fname, "drop");
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000329 else
330 {
331 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
332 vim_chdir(NameBuff);
333 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200334 if (start_dir != NULL)
335 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000336 }
337 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000338#endif
339
340 /*
341 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100342 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000343 * Note that we may use mch_exit() before mch_init()!
344 */
345 mch_init();
346 TIME_MSG("shell init");
347
348#ifdef USE_XSMP
349 /*
350 * For want of anywhere else to do it, try to connect to xsmp here.
351 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
352 * Hijacking -X 'no X connection' to also disable XSMP connection as that
353 * has a similar delay upon failure.
354 * Only try if SESSION_MANAGER is set to something non-null.
355 */
356 if (!x_no_connect)
357 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000358 char *p = getenv("SESSION_MANAGER");
359
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000360 if (p != NULL && *p != NUL)
361 {
362 xsmp_init();
363 TIME_MSG("xsmp init");
364 }
365 }
366#endif
367
368 /*
369 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000370 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000371 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000372
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100373#ifdef _IOLBF
374 /* Ensure output works usefully without a tty: buffer lines instead of
375 * fully buffered. */
376 if (silent_mode)
377 setvbuf(stdout, NULL, _IOLBF, 0);
378#endif
379
Bram Moolenaar9404a182019-05-03 22:25:40 +0200380 // This message comes before term inits, but after setting "silent_mode"
381 // when the input is not a tty. Omit the message with --not-a-term.
382 if (GARGCOUNT > 1 && !silent_mode && !is_not_a_term())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000383 printf(_("%d files to edit\n"), GARGCOUNT);
384
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000385 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000386 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000387 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000388 capabilities (will set full_screen) */
389 screen_start(); /* don't know where cursor is now */
390 TIME_MSG("Termcap init");
391 }
392
393 /*
394 * Set the default values for the options that use Rows and Columns.
395 */
396 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000397 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000398#ifdef FEAT_DIFF
399 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
400 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000401 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000402 diff_win_options(firstwin, FALSE);
403#endif
404
405 cmdline_row = Rows - p_ch;
406 msg_row = cmdline_row;
407 screenalloc(FALSE); /* allocate screen buffers */
408 set_init_2();
409 TIME_MSG("inits 2");
410
411 msg_scroll = TRUE;
412 no_wait_return = TRUE;
413
414 init_mappings(); /* set up initial mappings */
415
416 init_highlight(TRUE, FALSE); /* set the default highlight groups */
417 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000418
419#ifdef FEAT_EVAL
420 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000421 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000422#endif
423
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200424 /* Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
425 * Allows for setting 'loadplugins' there. */
426 if (params.use_vimrc != NULL
427 && (STRCMP(params.use_vimrc, "NONE") == 0
428 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
429 p_lpl = FALSE;
430
431 /* Execute --cmd arguments. */
432 exe_pre_commands(&params);
433
434 /* Source startup scripts. */
435 source_startup_scripts(&params);
436
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100437#ifdef FEAT_MZSCHEME
438 /*
439 * Newer version of MzScheme (Racket) require earlier (trampolined)
440 * initialisation via scheme_main_setup.
441 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200442 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200443 * Do source startup scripts, so that 'mzschemedll' can be set.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100444 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200445 return mzscheme_main();
446#else
447 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100448#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200449}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100450#endif /* NO_VIM_MAIN */
Bram Moolenaara357e442016-08-10 20:45:07 +0200451#endif /* PROTO */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100452
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200453/*
454 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
455 * things simple.
456 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
457 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100458 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200459vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100460{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100461#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000462#ifdef FEAT_EVAL
463 /*
464 * Read all the plugin files.
465 * Only when compiled with +eval, since most plugins need it.
466 */
467 if (p_lpl)
468 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200469 char_u *rtp_copy = NULL;
470
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200471 /* First add all package directories to 'runtimepath', so that their
472 * autoload directories can be found. Only if not done already with a
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200473 * :packloadall command.
474 * Make a copy of 'runtimepath', so that source_runtime does not use
475 * the pack directories. */
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200476 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200477 {
478 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200479 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200480 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200481
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200482 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy,
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000483# ifdef VMS /* Somehow VMS doesn't handle the "**". */
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200484 (char_u *)"plugin/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000485# else
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200486 (char_u *)"plugin/**/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000487# endif
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200488 DIP_ALL | DIP_NOAFTER);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000489 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200490 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100491
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200492 /* Only source "start" packages if not done already with a :packloadall
493 * command. */
494 if (!did_source_packages)
495 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100496 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200497
498# ifdef VMS /* Somehow VMS doesn't handle the "**". */
499 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_AFTER);
500# else
501 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_AFTER);
502# endif
503 TIME_MSG("loading after plugins");
504
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000505 }
506#endif
507
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000508#ifdef FEAT_DIFF
509 /* Decide about window layout for diff mode after reading vimrc. */
510 if (params.diff_mode && params.window_layout == 0)
511 {
512 if (diffopt_horizontal())
513 params.window_layout = WIN_HOR; /* use horizontal split */
514 else
515 params.window_layout = WIN_VER; /* use vertical split */
516 }
517#endif
518
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000519 /*
520 * Recovery mode without a file name: List swap files.
521 * This uses the 'dir' option, therefore it must be after the
522 * initializations.
523 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200524 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000525 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200526 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000527 mch_exit(0);
528 }
529
530 /*
531 * Set a few option defaults after reading .vimrc files:
532 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
533 */
534 set_init_3();
535 TIME_MSG("inits 3");
536
537 /*
538 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
539 * Note that this overrides anything from a vimrc file.
540 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000541 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000542 p_uc = 0;
543
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000544#ifdef FEAT_GUI
545 if (gui.starting)
546 {
547#if defined(UNIX) || defined(VMS)
548 /* When something caused a message from a vimrc script, need to output
549 * an extra newline before the shell prompt. */
550 if (did_emsg || msg_didout)
551 putchar('\n');
552#endif
553
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200554 gui_start(NULL); /* will set full_screen to TRUE */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000555 TIME_MSG("starting GUI");
556
557 /* When running "evim" or "gvim -y" we need the menus, exit if we
558 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000559 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000560 mch_exit(1);
561 }
562#endif
563
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000564#ifdef FEAT_VIMINFO
565 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000566 * Read in registers, history etc, but not marks, from the viminfo file.
567 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000568 */
569 if (*p_viminfo != NUL)
570 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000571 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000572 TIME_MSG("reading viminfo");
573 }
574#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100575#ifdef FEAT_EVAL
576 /* It's better to make v:oldfiles an empty list than NULL. */
577 if (get_vim_var_list(VV_OLDFILES) == NULL)
578 set_vim_var_list(VV_OLDFILES, list_alloc());
579#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000580
581#ifdef FEAT_QUICKFIX
582 /*
583 * "-q errorfile": Load the error file now.
584 * If the error file can't be read, exit before doing anything else.
585 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000586 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000587 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100588 char_u *enc = NULL;
589
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100590 enc = p_menc;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000591 if (params.use_ef != NULL)
592 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000593 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200594 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100595 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000596 {
597 out_char('\n');
598 mch_exit(3);
599 }
600 TIME_MSG("reading errorfile");
601 }
602#endif
603
604 /*
605 * Start putting things on the screen.
606 * Scroll screen down before drawing over it
607 * Clear screen now, so file message will not be cleared.
608 */
609 starting = NO_BUFFERS;
610 no_wait_return = FALSE;
611 if (!exmode_active)
612 msg_scroll = FALSE;
613
614#ifdef FEAT_GUI
615 /*
616 * This seems to be required to make callbacks to be called now, instead
617 * of after things have been put on the screen, which then may be deleted
618 * when getting a resize callback.
619 * For the Mac this handles putting files dropped on the Vim icon to
620 * global_alist.
621 */
622 if (gui.in_use)
623 {
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100624 gui_wait_for_chars(50L, typebuf.tb_change_cnt);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000625 TIME_MSG("GUI delay");
626 }
627#endif
628
629#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
630 qnx_clip_init();
631#endif
632
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200633#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
634 clip_init(TRUE);
635#endif
636
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000637#ifdef FEAT_XCLIPBOARD
638 /* Start using the X clipboard, unless the GUI was started. */
639# ifdef FEAT_GUI
640 if (!gui.in_use)
641# endif
642 {
643 setup_term_clip();
644 TIME_MSG("setup clipboard");
645 }
646#endif
647
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000648#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000649 /* Prepare for being a Vim server. */
650 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000651#endif
652
653 /*
654 * If "-" argument given: Read file from stdin.
655 * Do this before starting Raw mode, because it may change things that the
656 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
657 * are the same terminal: "cat | vim -".
658 * Using autocommands here may cause trouble...
659 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000660 if (params.edit_type == EDIT_STDIN && !recoverymode)
661 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000662
663#if defined(UNIX) || defined(VMS)
664 /* When switching screens and something caused a message from a vimrc
665 * script, need to output an extra newline on exit. */
666 if ((did_emsg || msg_didout) && *T_TI != NUL)
667 newline_on_exit = TRUE;
668#endif
669
670 /*
671 * When done something that is not allowed or error message call
672 * wait_return. This must be done before starttermcap(), because it may
673 * switch to another screen. It must be done after settmode(TMODE_RAW),
674 * because we want to react on a single key stroke.
675 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000676 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000677 */
678 settmode(TMODE_RAW);
679 TIME_MSG("setting raw mode");
680
681 if (need_wait_return || msg_didany)
682 {
683 wait_return(TRUE);
684 TIME_MSG("waiting for return");
685 }
686
687 starttermcap(); /* start termcap if not done by wait_return() */
688 TIME_MSG("start termcap");
689
690#ifdef FEAT_MOUSE
691 setmouse(); /* may start using the mouse */
692#endif
693 if (scroll_region)
694 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000695 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000696
Bram Moolenaar980bab42018-08-07 22:42:53 +0200697#if defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar40385db2018-08-07 22:31:44 +0200698 term_push_title(SAVE_RESTORE_BOTH);
699#endif
700
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000701 /*
702 * Don't clear the screen when starting in Ex mode, unless using the GUI.
703 */
704 if (exmode_active
705#ifdef FEAT_GUI
706 && !gui.in_use
707#endif
708 )
709 must_redraw = CLEAR;
710 else
711 {
712 screenclear(); /* clear screen */
713 TIME_MSG("clearing screen");
714 }
715
716#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000717 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000718 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100719 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200720 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000721 TIME_MSG("getting crypt key");
722 }
723#endif
724
725 no_wait_return = TRUE;
726
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000727 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000728 * Create the requested number of windows and edit buffers in them.
729 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000730 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000731 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000732 TIME_MSG("opening buffers");
733
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000734#ifdef FEAT_EVAL
735 /* clear v:swapcommand */
736 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
737#endif
738
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000739 /* Ex starts at last line of the file */
740 if (exmode_active)
741 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
742
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000743 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
744 TIME_MSG("BufEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000745 setpcmark();
746
747#ifdef FEAT_QUICKFIX
748 /*
749 * When started with "-q errorfile" jump to first error now.
750 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000751 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000752 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000753 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000754 TIME_MSG("jump to first error");
755 }
756#endif
757
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000758 /*
759 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000760 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000761 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200762 edit_buffers(&params, start_dir);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200763 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000764
765#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000766 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000767 {
768 win_T *wp;
769
770 /* set options in each window for "vimdiff". */
Bram Moolenaar29323592016-07-24 22:04:11 +0200771 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000772 diff_win_options(wp, TRUE);
773 }
774#endif
775
776 /*
777 * Shorten any of the filenames, but only when absolute.
778 */
779 shorten_fnames(FALSE);
780
781 /*
782 * Need to jump to the tag before executing the '-c command'.
783 * Makes "vim -c '/return' -t main" work.
784 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000785 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000786 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000787 swap_exists_did_quit = FALSE;
Bram Moolenaar146522e2005-12-16 21:55:46 +0000788
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000789 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000790 do_cmdline_cmd(IObuff);
791 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000792
Bram Moolenaar146522e2005-12-16 21:55:46 +0000793 /* If the user doesn't want to edit the file then we quit here. */
794 if (swap_exists_did_quit)
795 getout(1);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000796 }
797
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000798 /* Execute any "+", "-c" and "-S" arguments. */
799 if (params.n_commands > 0)
800 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000801
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200802 /* Must come before the may_req_ calls. */
803 starting = 0;
804
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100805#if defined(FEAT_TERMRESPONSE)
Bram Moolenaar976787d2017-06-04 15:45:50 +0200806 /* Must be done before redrawing, puts a few characters on the screen. */
807 may_req_ambiguous_char_width();
808#endif
809
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000810 RedrawingDisabled = 0;
811 redraw_all_later(NOT_VALID);
812 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000813
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200814 /* 'autochdir' has been postponed */
Bram Moolenaar6f470022018-04-10 18:47:20 +0200815 DO_AUTOCHDIR;
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200816
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000817#ifdef FEAT_TERMRESPONSE
818 /* Requesting the termresponse is postponed until here, so that a "-c q"
819 * argument doesn't make it appear in the shell Vim was started from. */
820 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200821
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200822 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000823#endif
824
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000825 /* start in insert mode */
826 if (p_im)
827 need_start_insertmode = TRUE;
828
Bram Moolenaar14735512016-03-26 21:00:08 +0100829#ifdef FEAT_EVAL
830 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
831#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000832 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
833 TIME_MSG("VimEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000834
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200835#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
836 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
837 * done after the clipboard is available and all initial commands that may
838 * modify the 'clipboard' setting have run; i.e. just before entering the
839 * main loop. */
840 {
841 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100842
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200843 adjust_clip_reg(&default_regname);
844 set_reg_var(default_regname);
845 }
846#endif
847
Bram Moolenaar8a3bb562018-03-04 20:14:14 +0100848#if defined(FEAT_DIFF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000849 /* When a startup script or session file setup for diff'ing and
850 * scrollbind, sync the scrollbind now. */
851 if (curwin->w_p_diff && curwin->w_p_scb)
852 {
853 update_topline();
854 check_scrollbind((linenr_T)0, 0L);
855 TIME_MSG("diff scrollbinding");
856 }
857#endif
858
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200859#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
860# ifdef VIMDLL
861 if (!gui.in_use)
862# endif
863 mch_set_winsize_now(); /* Allow winsize changes from now on */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000864#endif
865
Bram Moolenaar4033c552017-09-16 20:54:51 +0200866#if defined(FEAT_GUI)
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000867 /* When tab pages were created, may need to update the tab pages line and
868 * scrollbars. This is skipped while creating them. */
869 if (first_tabpage->tp_next != NULL)
870 {
871 out_flush();
872 gui_init_which_components(NULL);
873 gui_update_scrollbars(TRUE);
874 }
875 need_mouse_correct = TRUE;
876#endif
877
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000878 /* If ":startinsert" command used, stuff a dummy command to be able to
879 * call normal_cmd(), which will then start Insert mode. */
880 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000881 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000882
883#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200884 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200885 {
886# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200887# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar4f974752019-02-17 17:44:42 +0100888 && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200889 if (gui.in_use)
890 {
891 mch_errmsg(_("netbeans is not supported with this GUI\n"));
892 mch_exit(2);
893 }
894# endif
895# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000896 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200897 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200898 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000899#endif
900
901 TIME_MSG("before starting main loop");
902
903 /*
904 * Call the main command loop. This never returns.
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200905 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000906 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000907
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200908#endif /* NO_VIM_MAIN */
909
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000910 return 0;
911}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000912
913/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200914 * Initialisation shared by main() and some tests.
915 */
916 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200917common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200918{
Bram Moolenaar438d1762018-09-30 17:11:48 +0200919 cmdline_init();
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200920
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200921 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200922#ifdef FEAT_EVAL
923 eval_init(); /* init global variables */
924#endif
925
926#ifdef __QNXNTO__
927 qnx_init(); /* PhAttach() for clipboard, (and gui) */
928#endif
929
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200930 /* Init the table of Normal mode commands. */
931 init_normal_cmds();
932
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200933 /*
934 * Allocate space for the generic buffers (needed for set_init_1() and
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100935 * emsg()).
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200936 */
937 if ((IObuff = alloc(IOSIZE)) == NULL
938 || (NameBuff = alloc(MAXPATHL)) == NULL)
939 mch_exit(0);
940 TIME_MSG("Allocated generic buffers");
941
942#ifdef NBDEBUG
943 /* Wait a moment for debugging NetBeans. Must be after allocating
944 * NameBuff. */
945 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
946 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
947 TIME_MSG("NetBeans debug wait");
948#endif
949
950#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
951 /*
952 * Setup to use the current locale (for ctype() and many other things).
953 * NOTE: Translated messages with encodings other than latin1 will not
954 * work until set_init_1() has been called!
955 */
956 init_locale();
957 TIME_MSG("locale set");
958#endif
959
960#ifdef FEAT_GUI
961 gui.dofork = TRUE; /* default is to use fork() */
962#endif
963
964 /*
965 * Do a first scan of the arguments in "argv[]":
966 * -display or --display
967 * --server...
968 * --socketid
969 * --windowid
970 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200971 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200972
Bram Moolenaard0573012017-10-28 21:11:06 +0200973#if defined(FEAT_GUI)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200974 /* Prepare for possibly starting GUI sometime */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200975 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200976 TIME_MSG("GUI prepared");
977#endif
978
979#ifdef FEAT_CLIPBOARD
980 clip_init(FALSE); /* Initialise clipboard stuff */
981 TIME_MSG("clipboard setup");
982#endif
983
984 /*
985 * Check if we have an interactive window.
986 * On the Amiga: If there is no window, we open one with a newcli command
987 * (needed for :! to * work). mch_check_win() will also handle the -d or
988 * -dev argument.
989 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100990 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200991 TIME_MSG("window checked");
992
993 /*
994 * Allocate the first window and buffer.
995 * Can't do anything without it, exit when it fails.
996 */
997 if (win_alloc_first() == FAIL)
998 mch_exit(0);
999
1000 init_yank(); /* init yank buffers */
1001
1002 alist_init(&global_alist); /* Init the argument list to empty. */
1003 global_alist.id = 0;
1004
1005 /*
1006 * Set the default values for the options.
1007 * NOTE: Non-latin1 translated messages are working only after this,
1008 * because this is where "has_mbyte" will be set, which is used by
1009 * msg_outtrans_len_attr().
1010 * First find out the home directory, needed to expand "~" in options.
1011 */
1012 init_homedir(); /* find real value of $HOME */
Bram Moolenaar07268702018-03-01 21:57:32 +01001013 set_init_1(paramp->clean);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001014 TIME_MSG("inits 1");
1015
1016#ifdef FEAT_EVAL
1017 set_lang_var(); /* set v:lang and v:ctype */
1018#endif
Bram Moolenaar6436cd82018-12-27 00:28:33 +01001019
1020#ifdef FEAT_SIGNS
1021 init_signs();
1022#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001023}
1024
1025/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001026 * Return TRUE when the --not-a-term argument was found.
1027 */
1028 int
1029is_not_a_term()
1030{
1031 return params.not_a_term;
1032}
1033
1034/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001035 * Main loop: Execute Normal mode commands until exiting Vim.
1036 * Also used to handle commands in the command-line window, until the window
1037 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001038 * Also used to handle ":visual" command after ":global": execute Normal mode
1039 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001040 */
1041 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001042main_loop(
1043 int cmdwin, /* TRUE when working in the command-line window */
1044 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001045{
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001046 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001047 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001048#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001049 /* these are static to avoid a compiler warning */
1050 static linenr_T conceal_old_cursor_line = 0;
1051 static linenr_T conceal_new_cursor_line = 0;
1052 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001053#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001054
1055#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1056 /* Setup to catch a terminating error from the X server. Just ignore
1057 * it, restore the state and continue. This might not always work
1058 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001059 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001061 {
1062 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001063 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001064 got_int = TRUE;
1065 need_wait_return = FALSE;
1066 global_busy = FALSE;
1067 exmode_active = 0;
1068 skip_redraw = FALSE;
1069 RedrawingDisabled = 0;
1070 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001071 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001072# ifdef FEAT_EVAL
1073 emsg_skip = 0;
1074# endif
1075 emsg_off = 0;
1076# ifdef FEAT_MOUSE
1077 setmouse();
1078# endif
1079 settmode(TMODE_RAW);
1080 starttermcap();
1081 scroll_start();
1082 redraw_later_clear();
1083 }
1084#endif
1085
1086 clear_oparg(&oa);
1087 while (!cmdwin
1088#ifdef FEAT_CMDWIN
1089 || cmdwin_result == 0
1090#endif
1091 )
1092 {
1093 if (stuff_empty())
1094 {
1095 did_check_timestamps = FALSE;
1096 if (need_check_timestamps)
1097 check_timestamps(FALSE);
1098 if (need_wait_return) /* if wait_return still needed ... */
1099 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001100 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001101 {
1102 need_start_insertmode = FALSE;
1103 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1104 /* skip the fileinfo message now, because it would be shown
1105 * after insert mode finishes! */
1106 need_fileinfo = FALSE;
1107 }
1108 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001109
1110 /* Reset "got_int" now that we got back to the main loop. Except when
1111 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1112 * the ":g" command.
1113 * For ":g/pat/vi" we reset "got_int" when used once. When used
1114 * a second time we go back to Ex mode and abort the ":g" command. */
1115 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001116 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001117 if (noexmode && global_busy && !exmode_active && previous_got_int)
1118 {
1119 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1120 * used and keep "got_int" set, so that it aborts ":g". */
1121 exmode_active = EXMODE_NORMAL;
1122 State = NORMAL;
1123 }
1124 else if (!global_busy || !exmode_active)
1125 {
1126 if (!quit_more)
1127 (void)vgetc(); /* flush all buffers */
1128 got_int = FALSE;
1129 }
1130 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001131 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001132 else
1133 previous_got_int = FALSE;
1134
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001135 if (!exmode_active)
1136 msg_scroll = FALSE;
1137 quit_more = FALSE;
1138
1139 /*
1140 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1141 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1142 * update cursor and redraw.
1143 */
1144 if (skip_redraw || exmode_active)
1145 skip_redraw = FALSE;
1146 else if (do_redraw || stuff_empty())
1147 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001148#ifdef FEAT_GUI
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001149 // If ui_breakcheck() was used a resize may have been postponed.
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001150 gui_may_resize_shell();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001151#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001152#ifdef HAVE_DROP_FILE
1153 // If files were dropped while text was locked or the curbuf was
1154 // locked, this would be a good time to handle the drop.
1155 handle_any_postponed_drop();
1156#endif
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001157#ifdef FEAT_CONCEAL
1158 if (curwin->w_p_cole == 0)
1159 conceal_update_lines = FALSE;
1160#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001161
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001162 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001163 if (!finish_op && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001164 has_cursormoved()
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001165#ifdef FEAT_CONCEAL
1166 || curwin->w_p_cole > 0
1167#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001168 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001169 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001170 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001171 if (has_cursormoved())
1172 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1173 FALSE, curbuf);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001174# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001175 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001176 {
1177 conceal_old_cursor_line = last_cursormoved.lnum;
1178 conceal_new_cursor_line = curwin->w_cursor.lnum;
1179 conceal_update_lines = TRUE;
1180 }
1181# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001182 last_cursormoved = curwin->w_cursor;
1183 }
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001184
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001185#if defined(FEAT_CONCEAL)
1186 if (conceal_update_lines
1187 && (conceal_old_cursor_line != conceal_new_cursor_line
1188 || conceal_cursor_line(curwin)
1189 || need_cursor_line_redraw))
1190 {
1191 if (conceal_old_cursor_line != conceal_new_cursor_line
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001192 && conceal_old_cursor_line != 0
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001193 && conceal_old_cursor_line
1194 <= curbuf->b_ml.ml_line_count)
1195 redrawWinline(curwin, conceal_old_cursor_line);
1196 redrawWinline(curwin, conceal_new_cursor_line);
1197 curwin->w_valid &= ~VALID_CROW;
1198 need_cursor_line_redraw = FALSE;
1199 }
1200#endif
1201
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001202 /* Trigger TextChanged if b:changedtick differs. */
Bram Moolenaar186628f2013-03-19 13:33:23 +01001203 if (!finish_op && has_textchanged()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001204 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001205 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001206 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL, FALSE, curbuf);
1207 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001208 }
Bram Moolenaar186628f2013-03-19 13:33:23 +01001209
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01001210#if defined(FEAT_DIFF)
Bram Moolenaare3521d92018-09-16 14:10:31 +02001211 // Updating diffs from changed() does not always work properly,
1212 // esp. updating folds. Do an update just before redrawing if
1213 // needed.
1214 if (curtab->tp_diff_update || curtab->tp_diff_invalid)
1215 {
1216 ex_diffupdate(NULL);
1217 curtab->tp_diff_update = FALSE;
1218 }
1219
Bram Moolenaar33aec762006-01-22 23:30:12 +00001220 /* Scroll-binding for diff mode may have been postponed until
1221 * here. Avoids doing it for every change. */
1222 if (diff_need_scrollbind)
1223 {
1224 check_scrollbind((linenr_T)0, 0L);
1225 diff_need_scrollbind = FALSE;
1226 }
1227#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001228#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001229 /* Include a closed fold completely in the Visual area. */
1230 foldAdjustVisual();
1231#endif
1232#ifdef FEAT_FOLDING
1233 /*
1234 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1235 * contain the cursor.
1236 * When 'foldopen' is "all", open the fold(s) under the cursor.
1237 * This may mark the window for redrawing.
1238 */
1239 if (hasAnyFolding(curwin) && !char_avail())
1240 {
1241 foldCheckClose();
1242 if (fdo_flags & FDO_ALL)
1243 foldOpenCursor();
1244 }
1245#endif
1246
1247 /*
1248 * Before redrawing, make sure w_topline is correct, and w_leftcol
1249 * if lines don't wrap, and w_skipcol if lines wrap.
1250 */
1251 update_topline();
1252 validate_cursor();
1253
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001254 if (VIsual_active)
1255 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001256 else if (must_redraw)
Bram Moolenaara338adc2018-01-31 20:51:47 +01001257 {
1258 mch_disable_flush(); /* Stop issuing gui_mch_flush(). */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001259 update_screen(0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01001260 mch_enable_flush();
1261 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001262 else if (redraw_cmdline || clear_cmdline)
1263 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001264 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001265#ifdef FEAT_TITLE
1266 if (need_maketitle)
1267 maketitle();
1268#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001269#ifdef FEAT_VIMINFO
1270 curbuf->b_last_used = vim_time();
1271#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001272 /* display message after redraw */
1273 if (keep_msg != NULL)
1274 {
1275 char_u *p;
1276
1277 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001278 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1279 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001280 p = keep_msg;
Bram Moolenaar63c0ccd2019-01-19 21:06:58 +01001281 msg_attr((char *)p, keep_msg_attr);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001282 vim_free(p);
1283 }
1284 if (need_fileinfo) /* show file info after redraw */
1285 {
1286 fileinfo(FALSE, TRUE, FALSE);
1287 need_fileinfo = FALSE;
1288 }
1289
1290 emsg_on_display = FALSE; /* can delete error message now */
1291 did_emsg = FALSE;
1292 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001293 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001294 showruler(FALSE);
1295
1296 setcursor();
1297 cursor_on();
1298
1299 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001300
1301#ifdef STARTUPTIME
1302 /* Now that we have drawn the first screen all the startup stuff
1303 * has been done, close any file for startup messages. */
1304 if (time_fd != NULL)
1305 {
1306 TIME_MSG("first screen update");
1307 TIME_MSG("--- VIM STARTED ---");
1308 fclose(time_fd);
1309 time_fd = NULL;
1310 }
1311#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001312 }
1313#ifdef FEAT_GUI
1314 if (need_mouse_correct)
1315 gui_mouse_correct();
1316#endif
1317
1318 /*
1319 * Update w_curswant if w_set_curswant has been set.
1320 * Postponed until here to avoid computing w_virtcol too often.
1321 */
1322 update_curswant();
1323
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001324#ifdef FEAT_EVAL
1325 /*
1326 * May perform garbage collection when waiting for a character, but
1327 * only at the very toplevel. Otherwise we may be using a List or
1328 * Dict internally somewhere.
1329 * "may_garbage_collect" is reset in vgetc() which is invoked through
1330 * do_exmode() and normal_cmd().
1331 */
1332 may_garbage_collect = (!cmdwin && !noexmode);
1333#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001334 /*
1335 * If we're invoked as ex, do a round of ex commands.
1336 * Otherwise, get and execute a normal mode command.
1337 */
1338 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001339 {
1340 if (noexmode) /* End of ":global/path/visual" commands */
1341 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001342 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001343 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001344 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001345 {
1346#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001347 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001348 && oa.op_type == OP_NOP && oa.regname == NUL
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001349 && !VIsual_active
1350 && !skip_term_loop)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001351 {
1352 /* If terminal_loop() returns OK we got a key that is handled
Bram Moolenaar6d819742017-08-06 14:57:49 +02001353 * in Normal model. With FAIL we first need to position the
1354 * cursor and the screen needs to be redrawn. */
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001355 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001356 normal_cmd(&oa, TRUE);
1357 }
1358 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001359#endif
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001360 {
1361#ifdef FEAT_TERMINAL
1362 skip_term_loop = FALSE;
1363#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001364 normal_cmd(&oa, TRUE);
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001365 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02001366 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001367 }
1368}
1369
1370
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001371#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001372/*
1373 * Exit, but leave behind swap files for modified buffers.
1374 */
1375 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001376getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001377{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001378# if defined(SIGHUP) && defined(SIG_IGN)
1379 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1380 * makes Vim exit and then handling SIGHUP causes various reentrance
1381 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001382 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001383# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001384
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001385 ml_close_notmod(); /* close all not-modified buffers */
1386 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1387 ml_close_all(FALSE); /* close all memfiles, without deleting */
1388 getout(exitval); /* exit Vim properly */
1389}
1390#endif
1391
1392
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001393/*
1394 * Exit properly.
1395 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001396 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001397getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001398{
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001399 exiting = TRUE;
Bram Moolenaar0bd052b2018-03-22 20:33:56 +01001400#if defined(FEAT_JOB_CHANNEL)
1401 ch_log(NULL, "Exiting...");
1402#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001403
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001404 /* When running in Ex mode an error causes us to exit with a non-zero exit
1405 * code. POSIX requires this, although it's not 100% clear from the
1406 * standard. */
1407 if (exmode_active)
1408 exitval += ex_exitval;
1409
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001410 /* Position the cursor on the last screen line, below all the text */
1411#ifdef FEAT_GUI
1412 if (!gui.in_use)
1413#endif
1414 windgoto((int)Rows - 1, 0);
1415
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001416#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1417 /* Optionally print hashtable efficiency. */
1418 hash_debug_results();
1419#endif
1420
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001421#ifdef FEAT_GUI
1422 msg_didany = FALSE;
1423#endif
1424
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001425 if (v_dying <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001426 {
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001427 tabpage_T *tp;
1428 tabpage_T *next_tp;
1429 buf_T *buf;
1430 win_T *wp;
1431
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001432 /* Trigger BufWinLeave for all windows, but only once per buffer. */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001433 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001434 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001435 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001436 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001437 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001438 if (wp->w_buffer == NULL)
1439 /* Autocmd must have close the buffer already, skip. */
1440 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001441 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001442 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001443 {
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001444 bufref_T bufref;
1445
1446 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001447 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1448 buf->b_fname, FALSE, buf);
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001449 if (bufref_valid(&bufref))
1450 CHANGEDTICK(buf) = -1; /* note we did it already */
1451
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001452 /* start all over, autocommands may mess up the lists */
1453 next_tp = first_tabpage;
1454 break;
1455 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001456 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001457 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001458
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001459 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001460 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001461 if (buf->b_ml.ml_mfp != NULL)
1462 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001463 bufref_T bufref;
1464
1465 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001466 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001467 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001468 if (!bufref_valid(&bufref))
1469 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001470 break;
1471 }
1472 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1473 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001474
1475#ifdef FEAT_VIMINFO
1476 if (*p_viminfo != NUL)
1477 /* Write out the registers, history, marks etc, to the viminfo file */
1478 write_viminfo(NULL, FALSE);
1479#endif
1480
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001481 if (v_dying <= 1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001482 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001483
Bram Moolenaar05159a02005-02-26 23:04:13 +00001484#ifdef FEAT_PROFILE
1485 profile_dump();
1486#endif
1487
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001488 if (did_emsg
1489#ifdef FEAT_GUI
1490 || (gui.in_use && msg_didany && p_verbose > 0)
1491#endif
1492 )
1493 {
1494 /* give the user a chance to read the (error) message */
1495 no_wait_return = FALSE;
1496 wait_return(FALSE);
1497 }
1498
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001499 /* Position the cursor again, the autocommands may have moved it */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001500#ifdef FEAT_GUI
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001501 if (!gui.in_use)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001502#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001503 windgoto((int)Rows - 1, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001504
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001505#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001506 job_stop_on_exit();
1507#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001508#ifdef FEAT_LUA
1509 lua_end();
1510#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001511#ifdef FEAT_MZSCHEME
1512 mzscheme_end();
1513#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001514#ifdef FEAT_TCL
1515 tcl_end();
1516#endif
1517#ifdef FEAT_RUBY
1518 ruby_end();
1519#endif
1520#ifdef FEAT_PYTHON
1521 python_end();
1522#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001523#ifdef FEAT_PYTHON3
1524 python3_end();
1525#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001526#ifdef FEAT_PERL
1527 perl_end();
1528#endif
1529#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1530 iconv_end();
1531#endif
1532#ifdef FEAT_NETBEANS_INTG
1533 netbeans_end();
1534#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001535#ifdef FEAT_CSCOPE
1536 cs_end();
1537#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001538#ifdef FEAT_EVAL
1539 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001540 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001541#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001542#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +02001543 free_cmd_argsW();
1544#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001545
1546 mch_exit(exitval);
1547}
1548
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001549#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1550/*
1551 * Setup to use the current locale (for ctype() and many other things).
1552 */
1553 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001554init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001555{
1556 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001557
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001558# ifdef FEAT_GUI_GTK
1559 /* Tell Gtk not to change our locale settings. */
1560 gtk_disable_setlocale();
1561# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001562# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1563 /* Make sure strtod() uses a decimal point, not a comma. */
1564 setlocale(LC_NUMERIC, "C");
1565# endif
1566
Bram Moolenaar4f974752019-02-17 17:44:42 +01001567# ifdef MSWIN
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001568 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1569 * text while it's expecting text in the current locale. This call avoids
1570 * that. */
1571 setlocale(LC_CTYPE, "C");
1572# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001573
1574# ifdef FEAT_GETTEXT
1575 {
1576 int mustfree = FALSE;
1577 char_u *p;
1578
1579# ifdef DYNAMIC_GETTEXT
1580 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001581 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001582# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001583 /* expand_env() doesn't work yet, because g_chartab[] is not
1584 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001585 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1586 if (p != NULL && *p != NUL)
1587 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001588 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001589 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1590 }
1591 if (mustfree)
1592 vim_free(p);
1593 textdomain(VIMPACKAGE);
1594 }
1595# endif
1596}
1597#endif
1598
1599/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001600 * Get the name of the display, before gui_prepare() removes it from
1601 * argv[]. Used for the xterm-clipboard display.
1602 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001603 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001604 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001605 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001606early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001607{
Bram Moolenaar03005972008-11-20 13:12:36 +00001608#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1609 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001610 int argc = parmp->argc;
1611 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001612 int i;
1613
1614 for (i = 1; i < argc; i++)
1615 {
1616 if (STRCMP(argv[i], "--") == 0)
1617 break;
1618# ifdef FEAT_XCLIPBOARD
1619 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001620# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001621 || STRICMP(argv[i], "--display") == 0
1622# endif
1623 )
1624 {
1625 if (i == argc - 1)
1626 mainerr_arg_missing((char_u *)argv[i]);
1627 xterm_display = argv[++i];
1628 }
1629# endif
1630# ifdef FEAT_CLIENTSERVER
1631 else if (STRICMP(argv[i], "--servername") == 0)
1632 {
1633 if (i == argc - 1)
1634 mainerr_arg_missing((char_u *)argv[i]);
1635 parmp->serverName_arg = (char_u *)argv[++i];
1636 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001637 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001638 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001639 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001640 {
1641 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001642# ifdef FEAT_GUI
1643 if (strstr(argv[i], "-wait") != 0)
1644 /* don't fork() when starting the GUI to edit files ourself */
1645 gui.dofork = FALSE;
1646# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001647 }
1648# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001649
Bram Moolenaar4f974752019-02-17 17:44:42 +01001650# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
1651# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001652 else if (STRICMP(argv[i], "--windowid") == 0)
1653# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001654 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001655# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001656 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001657 long_u id;
1658 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001659
1660 if (i == argc - 1)
1661 mainerr_arg_missing((char_u *)argv[i]);
1662 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001663 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001664 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001665 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001666 if (count != 1)
1667 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1668 else
Bram Moolenaar4f974752019-02-17 17:44:42 +01001669# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001670 win_socket_id = id;
1671# else
1672 gtk_socket_id = id;
1673# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001674 i++;
1675 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001676# endif
1677# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001678 else if (STRICMP(argv[i], "--echo-wid") == 0)
1679 echo_wid_arg = TRUE;
1680# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001681# ifndef FEAT_NETBEANS_INTG
1682 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001683 {
1684 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1685 mch_exit(2);
1686 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001687# endif
1688
Bram Moolenaar58d98232005-07-23 22:25:46 +00001689 }
1690#endif
1691}
1692
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001693#ifndef NO_VIM_MAIN
1694/*
1695 * Get a (optional) count for a Vim argument.
1696 */
1697 static int
1698get_number_arg(
1699 char_u *p, /* pointer to argument */
1700 int *idx, /* index in argument, is incremented */
1701 int def) /* default value */
1702{
1703 if (vim_isdigit(p[*idx]))
1704 {
1705 def = atoi((char *)&(p[*idx]));
1706 while (vim_isdigit(p[*idx]))
1707 *idx = *idx + 1;
1708 }
1709 return def;
1710}
1711
1712/*
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001713 * Check for: [r][e][g][vi|vim|view][diff][ex[im]] (sort of)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001714 * If the executable name starts with "r" we disable shell commands.
1715 * If the next character is "e" we run in Easy mode.
1716 * If the next character is "g" we run the GUI version.
1717 * If the next characters are "view" we start in readonly mode.
1718 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1719 * If the next characters are "ex" we start in Ex mode. If it's followed
1720 * by "im" use improved Ex mode.
1721 */
1722 static void
1723parse_command_name(mparm_T *parmp)
1724{
1725 char_u *initstr;
1726
1727 initstr = gettail((char_u *)parmp->argv[0]);
1728
Bram Moolenaard0573012017-10-28 21:11:06 +02001729#ifdef FEAT_GUI_MAC
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001730 /* An issue has been seen when launching Vim in such a way that
1731 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1732 * executable or a symbolic link of it. Until this issue is resolved
1733 * we prohibit the GUI from being used.
1734 */
1735 if (STRCMP(initstr, parmp->argv[0]) == 0)
1736 disallow_gui = TRUE;
1737
1738 /* TODO: On MacOS X default to gui if argv[0] ends in:
1739 * /Vim.app/Contents/MacOS/Vim */
1740#endif
1741
1742#ifdef FEAT_EVAL
1743 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001744 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001745#endif
1746
1747 if (TOLOWER_ASC(initstr[0]) == 'r')
1748 {
1749 restricted = TRUE;
1750 ++initstr;
1751 }
1752
1753 /* Use evim mode for "evim" and "egvim", not for "editor". */
1754 if (TOLOWER_ASC(initstr[0]) == 'e'
1755 && (TOLOWER_ASC(initstr[1]) == 'v'
1756 || TOLOWER_ASC(initstr[1]) == 'g'))
1757 {
1758#ifdef FEAT_GUI
1759 gui.starting = TRUE;
1760#endif
1761 parmp->evim_mode = TRUE;
1762 ++initstr;
1763 }
1764
1765 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1766 if (TOLOWER_ASC(initstr[0]) == 'g')
1767 {
1768 main_start_gui();
1769#ifdef FEAT_GUI
1770 ++initstr;
1771#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001772#ifdef GUI_MAY_SPAWN
1773 gui.dospawn = FALSE; // No need to spawn a new process.
1774#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001775 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001776#ifdef GUI_MAY_SPAWN
1777 else
1778 gui.dospawn = TRUE; // Not "gvim". Need to spawn gvim.exe.
1779#endif
1780
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001781
1782 if (STRNICMP(initstr, "view", 4) == 0)
1783 {
1784 readonlymode = TRUE;
1785 curbuf->b_p_ro = TRUE;
1786 p_uc = 10000; /* don't update very often */
1787 initstr += 4;
1788 }
1789 else if (STRNICMP(initstr, "vim", 3) == 0)
1790 initstr += 3;
1791
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001792 // Catch "[r][g]vimdiff" and "[r][g]viewdiff".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001793 if (STRICMP(initstr, "diff") == 0)
1794 {
1795#ifdef FEAT_DIFF
1796 parmp->diff_mode = TRUE;
1797#else
1798 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1799 mch_errmsg("\n");
1800 mch_exit(2);
1801#endif
1802 }
1803
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001804 // Checking for "ex" here may catch some weir names, such as "vimex" or
1805 // "viewex", we assume the user knows that.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001806 if (STRNICMP(initstr, "ex", 2) == 0)
1807 {
1808 if (STRNICMP(initstr + 2, "im", 2) == 0)
1809 exmode_active = EXMODE_VIM;
1810 else
1811 exmode_active = EXMODE_NORMAL;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001812 change_compatible(TRUE); // set 'compatible'
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001813 }
1814}
1815
Bram Moolenaar58d98232005-07-23 22:25:46 +00001816/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001817 * Scan the command line arguments.
1818 */
1819 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001820command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001821{
1822 int argc = parmp->argc;
1823 char **argv = parmp->argv;
1824 int argv_idx; /* index in argv[n][] */
1825 int had_minmin = FALSE; /* found "--" argument */
1826 int want_argument; /* option argument with argument */
1827 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001828 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001829 long n;
1830
1831 --argc;
1832 ++argv;
1833 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1834 while (argc > 0)
1835 {
1836 /*
1837 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1838 */
1839 if (argv[0][0] == '+' && !had_minmin)
1840 {
1841 if (parmp->n_commands >= MAX_ARG_CMDS)
1842 mainerr(ME_EXTRA_CMD, NULL);
1843 argv_idx = -1; /* skip to next argument */
1844 if (argv[0][1] == NUL)
1845 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1846 else
1847 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1848 }
1849
1850 /*
1851 * Optional argument.
1852 */
1853 else if (argv[0][0] == '-' && !had_minmin)
1854 {
1855 want_argument = FALSE;
1856 c = argv[0][argv_idx++];
1857#ifdef VMS
1858 /*
1859 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1860 * and "-/X" as "-X".
1861 */
1862 if (c == '/')
1863 {
1864 c = argv[0][argv_idx++];
1865 c = TOUPPER_ASC(c);
1866 }
1867 else
1868 c = TOLOWER_ASC(c);
1869#endif
1870 switch (c)
1871 {
1872 case NUL: /* "vim -" read from stdin */
1873 /* "ex -" silent mode */
1874 if (exmode_active)
1875 silent_mode = TRUE;
1876 else
1877 {
1878 if (parmp->edit_type != EDIT_NONE)
1879 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1880 parmp->edit_type = EDIT_STDIN;
1881 read_cmd_fd = 2; /* read from stderr instead of stdin */
1882 }
1883 argv_idx = -1; /* skip to next argument */
1884 break;
1885
1886 case '-': /* "--" don't take any more option arguments */
1887 /* "--help" give help message */
1888 /* "--version" give version message */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001889 /* "--clean" clean context */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001890 /* "--literal" take files literally */
1891 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001892 /* "--not-a-term" don't warn for not a term */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001893 /* "--ttyfail" exit if not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001894 /* "--noplugin[s]" skip plugins */
1895 /* "--cmd <cmd>" execute cmd before vimrc */
1896 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1897 usage();
1898 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1899 {
1900 Columns = 80; /* need to init Columns */
1901 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1902 list_version();
1903 msg_putchar('\n');
1904 msg_didout = FALSE;
1905 mch_exit(0);
1906 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001907 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
1908 {
1909 parmp->use_vimrc = (char_u *)"DEFAULTS";
Bram Moolenaar62dd4522018-03-14 21:20:02 +01001910#ifdef FEAT_GUI
1911 use_gvimrc = (char_u *)"NONE";
1912#endif
Bram Moolenaar07268702018-03-01 21:57:32 +01001913 parmp->clean = TRUE;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001914 set_option_value((char_u *)"vif", 0L, (char_u *)"NONE", 0);
1915 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001916 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1917 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001918#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001919 parmp->literal = TRUE;
1920#endif
1921 }
1922 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1923 {
1924#ifdef FEAT_GUI
1925 gui.dofork = FALSE; /* don't fork() when starting GUI */
1926#endif
1927 }
1928 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1929 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001930 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1931 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001932 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
1933 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001934 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1935 {
1936 want_argument = TRUE;
1937 argv_idx += 3;
1938 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001939 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1940 {
1941 want_argument = TRUE;
1942 argv_idx += 11;
1943 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001944#ifdef FEAT_CLIENTSERVER
1945 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1946 ; /* already processed -- no arg */
1947 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1948 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1949 {
1950 /* already processed -- snatch the following arg */
1951 if (argc > 1)
1952 {
1953 --argc;
1954 ++argv;
1955 }
1956 }
1957#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001958#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001959# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001960 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001961# else
1962 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1963# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001964 {
1965 /* already processed -- snatch the following arg */
1966 if (argc > 1)
1967 {
1968 --argc;
1969 ++argv;
1970 }
1971 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001972#endif
1973#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001974 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1975 {
1976 /* already processed, skip */
1977 }
1978#endif
1979 else
1980 {
1981 if (argv[0][argv_idx])
1982 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1983 had_minmin = TRUE;
1984 }
1985 if (!want_argument)
1986 argv_idx = -1; /* skip to next argument */
1987 break;
1988
1989 case 'A': /* "-A" start in Arabic mode */
1990#ifdef FEAT_ARABIC
1991 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1992#else
1993 mch_errmsg(_(e_noarabic));
1994 mch_exit(2);
1995#endif
1996 break;
1997
1998 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001999 /* Needs to be effective before expanding file names, because
2000 * for Win32 this makes us edit a shortcut file itself,
2001 * instead of the file it links to. */
2002 set_options_bin(curbuf->b_p_bin, 1, 0);
2003 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002004 break;
2005
2006 case 'C': /* "-C" Compatible */
2007 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02002008 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002009 break;
2010
2011 case 'e': /* "-e" Ex mode */
2012 exmode_active = EXMODE_NORMAL;
2013 break;
2014
2015 case 'E': /* "-E" Improved Ex mode */
2016 exmode_active = EXMODE_VIM;
2017 break;
2018
2019 case 'f': /* "-f" GUI: run in foreground. Amiga: open
2020 window directly, not with newcli */
2021#ifdef FEAT_GUI
2022 gui.dofork = FALSE; /* don't fork() when starting GUI */
2023#endif
2024 break;
2025
2026 case 'g': /* "-g" start GUI */
2027 main_start_gui();
2028 break;
2029
Bram Moolenaar14184a32019-02-16 15:10:30 +01002030 case 'F': /* "-F" was for Farsi mode */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002031 mch_errmsg(_(e_nofarsi));
2032 mch_exit(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002033 break;
2034
Bram Moolenaarc3e81692018-05-05 15:09:51 +02002035 case '?': /* "-?" give help message (for MS-Windows) */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002036 case 'h': /* "-h" give help message */
2037#ifdef FEAT_GUI_GNOME
2038 /* Tell usage() to exit for "gvim". */
2039 gui.starting = FALSE;
2040#endif
2041 usage();
2042 break;
2043
2044 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2045#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002046 p_hkmap = TRUE;
2047 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002048#else
2049 mch_errmsg(_(e_nohebrew));
2050 mch_exit(2);
2051#endif
2052 break;
2053
2054 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2055#ifdef FEAT_LISP
2056 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2057 p_sm = TRUE;
2058#endif
2059 break;
2060
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002061 case 'M': /* "-M" no changes or writing of files */
2062 reset_modifiable();
2063 /* FALLTHROUGH */
2064
2065 case 'm': /* "-m" no writing of files */
2066 p_write = FALSE;
2067 break;
2068
2069 case 'y': /* "-y" easy mode */
2070#ifdef FEAT_GUI
2071 gui.starting = TRUE; /* start GUI a bit later */
2072#endif
2073 parmp->evim_mode = TRUE;
2074 break;
2075
2076 case 'N': /* "-N" Nocompatible */
2077 change_compatible(FALSE);
2078 break;
2079
2080 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002081#ifdef FEAT_NETBEANS_INTG
2082 /* checking for "-nb", netbeans parameters */
2083 if (argv[0][argv_idx] == 'b')
2084 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002085 netbeansArg = argv[0];
2086 argv_idx = -1; /* skip to next argument */
2087 }
2088 else
2089#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002090 parmp->no_swap_file = TRUE;
2091 break;
2092
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002093 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002094#ifdef TARGET_API_MAC_OSX
2095 /* For some reason on MacOS X, an argument like:
2096 -psn_0_10223617 is passed in when invoke from Finder
2097 or with the 'open' command */
2098 if (argv[0][argv_idx] == 's')
2099 {
2100 argv_idx = -1; /* bypass full -psn */
2101 main_start_gui();
2102 break;
2103 }
2104#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002105 /* default is 0: open window for each file */
2106 parmp->window_count = get_number_arg((char_u *)argv[0],
2107 &argv_idx, 0);
2108 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002109 break;
2110
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002111 case 'o': /* "-o[N]" open N horizontal split windows */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002112 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002113 parmp->window_count = get_number_arg((char_u *)argv[0],
2114 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002115 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002116 break;
2117
2118 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002119 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002120 parmp->window_count = get_number_arg((char_u *)argv[0],
2121 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002122 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002123 break;
2124
2125#ifdef FEAT_QUICKFIX
2126 case 'q': /* "-q" QuickFix mode */
2127 if (parmp->edit_type != EDIT_NONE)
2128 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2129 parmp->edit_type = EDIT_QF;
2130 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2131 {
2132 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2133 argv_idx = -1;
2134 }
2135 else if (argc > 1) /* "-q {errorfile}" */
2136 want_argument = TRUE;
2137 break;
2138#endif
2139
2140 case 'R': /* "-R" readonly mode */
2141 readonlymode = TRUE;
2142 curbuf->b_p_ro = TRUE;
2143 p_uc = 10000; /* don't update very often */
2144 break;
2145
2146 case 'r': /* "-r" recovery mode */
2147 case 'L': /* "-L" recovery mode */
2148 recoverymode = 1;
2149 break;
2150
2151 case 's':
2152 if (exmode_active) /* "-s" silent (batch) mode */
2153 silent_mode = TRUE;
2154 else /* "-s {scriptin}" read from script file */
2155 want_argument = TRUE;
2156 break;
2157
2158 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2159 if (parmp->edit_type != EDIT_NONE)
2160 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2161 parmp->edit_type = EDIT_TAG;
2162 if (argv[0][argv_idx]) /* "-t{tag}" */
2163 {
2164 parmp->tagname = (char_u *)argv[0] + argv_idx;
2165 argv_idx = -1;
2166 }
2167 else /* "-t {tag}" */
2168 want_argument = TRUE;
2169 break;
2170
2171#ifdef FEAT_EVAL
2172 case 'D': /* "-D" Debugging */
2173 parmp->use_debug_break_level = 9999;
2174 break;
2175#endif
2176#ifdef FEAT_DIFF
2177 case 'd': /* "-d" 'diff' */
2178# ifdef AMIGA
2179 /* check for "-dev {device}" */
2180 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2181 want_argument = TRUE;
2182 else
2183# endif
2184 parmp->diff_mode = TRUE;
2185 break;
2186#endif
2187 case 'V': /* "-V{N}" Verbose level */
2188 /* default is 10: a little bit verbose */
2189 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2190 if (argv[0][argv_idx] != NUL)
2191 {
2192 set_option_value((char_u *)"verbosefile", 0L,
2193 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002194 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002195 }
2196 break;
2197
2198 case 'v': /* "-v" Vi-mode (as if called "vi") */
2199 exmode_active = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002200#if defined(FEAT_GUI) && !defined(VIMDLL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002201 gui.starting = FALSE; /* don't start GUI */
2202#endif
2203 break;
2204
2205 case 'w': /* "-w{number}" set window height */
2206 /* "-w {scriptout}" write to script */
2207 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2208 {
2209 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2210 set_option_value((char_u *)"window", n, NULL, 0);
2211 break;
2212 }
2213 want_argument = TRUE;
2214 break;
2215
2216#ifdef FEAT_CRYPT
2217 case 'x': /* "-x" encrypted reading/writing of files */
2218 parmp->ask_for_key = TRUE;
2219 break;
2220#endif
2221
2222 case 'X': /* "-X" don't connect to X server */
2223#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2224 x_no_connect = TRUE;
2225#endif
2226 break;
2227
2228 case 'Z': /* "-Z" restricted mode */
2229 restricted = TRUE;
2230 break;
2231
2232 case 'c': /* "-c{command}" or "-c {command}" execute
2233 command */
2234 if (argv[0][argv_idx] != NUL)
2235 {
2236 if (parmp->n_commands >= MAX_ARG_CMDS)
2237 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002238 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2239 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002240 argv_idx = -1;
2241 break;
2242 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002243 /* FALLTHROUGH */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002244 case 'S': /* "-S {file}" execute Vim script */
2245 case 'i': /* "-i {viminfo}" use for viminfo */
2246#ifndef FEAT_DIFF
2247 case 'd': /* "-d {device}" device (for Amiga) */
2248#endif
2249 case 'T': /* "-T {terminal}" terminal name */
2250 case 'u': /* "-u {vimrc}" vim inits file */
2251 case 'U': /* "-U {gvimrc}" gvim inits file */
2252 case 'W': /* "-W {scriptout}" overwrite */
Bram Moolenaar4f974752019-02-17 17:44:42 +01002253#ifdef FEAT_GUI_MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002254 case 'P': /* "-P {parent title}" MDI parent */
2255#endif
2256 want_argument = TRUE;
2257 break;
2258
2259 default:
2260 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2261 }
2262
2263 /*
2264 * Handle option arguments with argument.
2265 */
2266 if (want_argument)
2267 {
2268 /*
2269 * Check for garbage immediately after the option letter.
2270 */
2271 if (argv[0][argv_idx] != NUL)
2272 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2273
2274 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002275 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002276 mainerr_arg_missing((char_u *)argv[0]);
2277 ++argv;
2278 argv_idx = -1;
2279
2280 switch (c)
2281 {
2282 case 'c': /* "-c {command}" execute command */
2283 case 'S': /* "-S {file}" execute Vim script */
2284 if (parmp->n_commands >= MAX_ARG_CMDS)
2285 mainerr(ME_EXTRA_CMD, NULL);
2286 if (c == 'S')
2287 {
2288 char *a;
2289
2290 if (argc < 1)
2291 /* "-S" without argument: use default session file
2292 * name. */
2293 a = SESSION_FILE;
2294 else if (argv[0][0] == '-')
2295 {
2296 /* "-S" followed by another option: use default
2297 * session file name. */
2298 a = SESSION_FILE;
2299 ++argc;
2300 --argv;
2301 }
2302 else
2303 a = argv[0];
2304 p = alloc((unsigned)(STRLEN(a) + 4));
2305 if (p == NULL)
2306 mch_exit(2);
2307 sprintf((char *)p, "so %s", a);
2308 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2309 parmp->commands[parmp->n_commands++] = p;
2310 }
2311 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002312 parmp->commands[parmp->n_commands++] =
2313 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002314 break;
2315
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002316 case '-':
2317 if (argv[-1][2] == 'c')
2318 {
2319 /* "--cmd {command}" execute command */
2320 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2321 mainerr(ME_EXTRA_CMD, NULL);
2322 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002323 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002324 }
2325 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002326 break;
2327
2328 /* case 'd': -d {device} is handled in mch_check_win() for the
2329 * Amiga */
2330
2331#ifdef FEAT_QUICKFIX
2332 case 'q': /* "-q {errorfile}" QuickFix mode */
2333 parmp->use_ef = (char_u *)argv[0];
2334 break;
2335#endif
2336
2337 case 'i': /* "-i {viminfo}" use for viminfo */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002338 set_option_value((char_u *)"vif", 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002339 break;
2340
2341 case 's': /* "-s {scriptin}" read from script file */
2342 if (scriptin[0] != NULL)
2343 {
2344scripterror:
2345 mch_errmsg(_("Attempt to open script file again: \""));
2346 mch_errmsg(argv[-1]);
2347 mch_errmsg(" ");
2348 mch_errmsg(argv[0]);
2349 mch_errmsg("\"\n");
2350 mch_exit(2);
2351 }
2352 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2353 {
2354 mch_errmsg(_("Cannot open for reading: \""));
2355 mch_errmsg(argv[0]);
2356 mch_errmsg("\"\n");
2357 mch_exit(2);
2358 }
2359 if (save_typebuf() == FAIL)
2360 mch_exit(2); /* out of memory */
2361 break;
2362
2363 case 't': /* "-t {tag}" */
2364 parmp->tagname = (char_u *)argv[0];
2365 break;
2366
2367 case 'T': /* "-T {terminal}" terminal name */
2368 /*
2369 * The -T term argument is always available and when
2370 * HAVE_TERMLIB is supported it overrides the environment
2371 * variable TERM.
2372 */
2373#ifdef FEAT_GUI
2374 if (term_is_gui((char_u *)argv[0]))
2375 gui.starting = TRUE; /* start GUI a bit later */
2376 else
2377#endif
2378 parmp->term = (char_u *)argv[0];
2379 break;
2380
2381 case 'u': /* "-u {vimrc}" vim inits file */
2382 parmp->use_vimrc = (char_u *)argv[0];
2383 break;
2384
2385 case 'U': /* "-U {gvimrc}" gvim inits file */
2386#ifdef FEAT_GUI
2387 use_gvimrc = (char_u *)argv[0];
2388#endif
2389 break;
2390
2391 case 'w': /* "-w {nr}" 'window' value */
2392 /* "-w {scriptout}" append to script file */
2393 if (vim_isdigit(*((char_u *)argv[0])))
2394 {
2395 argv_idx = 0;
2396 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2397 set_option_value((char_u *)"window", n, NULL, 0);
2398 argv_idx = -1;
2399 break;
2400 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002401 /* FALLTHROUGH */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002402 case 'W': /* "-W {scriptout}" overwrite script file */
2403 if (scriptout != NULL)
2404 goto scripterror;
2405 if ((scriptout = mch_fopen(argv[0],
2406 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2407 {
2408 mch_errmsg(_("Cannot open for script output: \""));
2409 mch_errmsg(argv[0]);
2410 mch_errmsg("\"\n");
2411 mch_exit(2);
2412 }
2413 break;
2414
Bram Moolenaar4f974752019-02-17 17:44:42 +01002415#ifdef FEAT_GUI_MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002416 case 'P': /* "-P {parent title}" MDI parent */
2417 gui_mch_set_parent(argv[0]);
2418 break;
2419#endif
2420 }
2421 }
2422 }
2423
2424 /*
2425 * File name argument.
2426 */
2427 else
2428 {
2429 argv_idx = -1; /* skip to next argument */
2430
2431 /* Check for only one type of editing. */
2432 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2433 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2434 parmp->edit_type = EDIT_FILE;
2435
2436#ifdef MSWIN
2437 /* Remember if the argument was a full path before changing
2438 * slashes to backslashes. */
2439 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2440 parmp->full_path = TRUE;
2441#endif
2442
2443 /* Add the file to the global argument list. */
2444 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2445 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2446 mch_exit(2);
2447#ifdef FEAT_DIFF
2448 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2449 && !mch_isdir(alist_name(&GARGLIST[0])))
2450 {
2451 char_u *r;
2452
2453 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2454 if (r != NULL)
2455 {
2456 vim_free(p);
2457 p = r;
2458 }
2459 }
2460#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002461#if defined(__CYGWIN32__) && !defined(MSWIN)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002462 /*
2463 * If vim is invoked by non-Cygwin tools, convert away any
2464 * DOS paths, so things like .swp files are created correctly.
2465 * Look for evidence of non-Cygwin paths before we bother.
2466 * This is only for when using the Unix files.
2467 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002468 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002469 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002470 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002471
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002472# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002473 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002474# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002475 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002476# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002477 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002478 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002479 if (p == NULL)
2480 mch_exit(2);
2481 }
2482#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002483
2484#ifdef USE_FNAME_CASE
2485 /* Make the case of the file name match the actual file. */
2486 fname_case(p, 0);
2487#endif
2488
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002489 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002490#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002491 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002492#else
2493 2 /* add buffer number now and use curbuf */
2494#endif
2495 );
2496
Bram Moolenaar4f974752019-02-17 17:44:42 +01002497#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002498 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002499 /* Remember this argument has been added to the argument list.
2500 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002501 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002502# ifdef FEAT_DIFF
2503 parmp->diff_mode
2504# else
2505 FALSE
2506# endif
2507 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002508 }
2509#endif
2510 }
2511
2512 /*
2513 * If there are no more letters after the current "-", go to next
2514 * argument. argv_idx is set to -1 when the current argument is to be
2515 * skipped.
2516 */
2517 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2518 {
2519 --argc;
2520 ++argv;
2521 argv_idx = 1;
2522 }
2523 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002524
2525#ifdef FEAT_EVAL
2526 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2527 * one. */
2528 if (parmp->n_commands > 0)
2529 {
2530 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2531 if (p != NULL)
2532 {
2533 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2534 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2535 vim_free(p);
2536 }
2537 }
2538#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002539}
2540
2541/*
2542 * Print a warning if stdout is not a terminal.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +01002543 * When starting in Ex mode and commands come from a file, set silent_mode.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002544 */
2545 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002546check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002547{
2548 int input_isatty; /* is active input a terminal? */
2549
2550 input_isatty = mch_input_isatty();
2551 if (exmode_active)
2552 {
2553 if (!input_isatty)
2554 silent_mode = TRUE;
2555 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002556 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002557#ifdef FEAT_GUI
2558 /* don't want the delay when started from the desktop */
2559 && !gui.starting
2560#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002561 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002562 {
2563#ifdef NBDEBUG
2564 /*
2565 * This shouldn't be necessary. But if I run netbeans with the log
2566 * output coming to the console and XOpenDisplay fails, I get vim
2567 * trying to start with input/output to my console tty. This fills my
2568 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002569 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002570 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002571 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002572 {
2573 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2574 exit(1);
2575 }
2576#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002577#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2578 if (
2579# ifdef VIMDLL
2580 !gui.starting &&
2581# endif
2582 is_cygpty_used())
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002583 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002584# if defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
Bram Moolenaar2a027452017-09-26 19:10:37 +02002585 && defined(FEAT_GETTEXT)
2586 char *s, *tofree = NULL;
2587
2588 /* Set the encoding of the error message based on $LC_ALL or
2589 * other environment variables instead of 'encoding'.
2590 * Note that the message is shown on a Cygwin terminal (e.g.
2591 * mintty) which encoding is based on $LC_ALL or etc., not the
2592 * current codepage used by normal Win32 console programs. */
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002593 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002594 if (s == NULL)
2595 s = "utf-8"; /* Use "utf-8" by default. */
2596 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2597 vim_free(tofree);
2598# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002599 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2600 exit(1);
2601 }
2602#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002603 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002604 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2605 if (!input_isatty)
2606 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2607 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002608 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2609 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002610 if (scriptin[0] == NULL)
2611 ui_delay(2000L, TRUE);
2612 TIME_MSG("Warning delay");
2613 }
2614}
2615
2616/*
2617 * Read text from stdin.
2618 */
2619 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002620read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002621{
2622 int i;
2623
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002624 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002625 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002626
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002627 no_wait_return = TRUE;
2628 i = msg_didany;
2629 set_buflisted(TRUE);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002630 (void)open_buffer(TRUE, NULL, 0); // create memfile and read file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002631 no_wait_return = FALSE;
2632 msg_didany = i;
2633 TIME_MSG("reading stdin");
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002634
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002635 check_swap_exists_action();
Bram Moolenaard0573012017-10-28 21:11:06 +02002636#if !(defined(AMIGA) || defined(MACOS_X))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002637 /*
2638 * Close stdin and dup it from stderr. Required for GPM to work
2639 * properly, and for running external commands.
2640 * Is there any other system that cannot do this?
2641 */
2642 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002643 vim_ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002644#endif
2645}
2646
2647/*
2648 * Create the requested number of windows and edit buffers in them.
2649 * Also does recovery if "recoverymode" set.
2650 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002651 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002652create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002653{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002654 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002655 int done = 0;
2656
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002657 /*
2658 * Create the number of windows that was requested.
2659 */
2660 if (parmp->window_count == -1) /* was not set */
2661 parmp->window_count = 1;
2662 if (parmp->window_count == 0)
2663 parmp->window_count = GARGCOUNT;
2664 if (parmp->window_count > 1)
2665 {
2666 /* Don't change the windows if there was a command in .vimrc that
2667 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002668 if (parmp->window_layout == 0)
2669 parmp->window_layout = WIN_HOR;
2670 if (parmp->window_layout == WIN_TABS)
2671 {
2672 parmp->window_count = make_tabpages(parmp->window_count);
2673 TIME_MSG("making tab pages");
2674 }
2675 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002676 {
2677 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002678 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002679 TIME_MSG("making windows");
2680 }
2681 else
2682 parmp->window_count = win_count();
2683 }
2684 else
2685 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002686
2687 if (recoverymode) /* do recover */
2688 {
2689 msg_scroll = TRUE; /* scroll message up */
2690 ml_recover();
2691 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2692 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002693 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002694 }
2695 else
2696 {
2697 /*
2698 * Open a buffer for windows that don't have one yet.
2699 * Commands in the .vimrc might have loaded a file or split the window.
2700 * Watch out for autocommands that delete a window.
2701 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002702 /*
2703 * Don't execute Win/Buf Enter/Leave autocommands here
2704 */
2705 ++autocmd_no_enter;
2706 ++autocmd_no_leave;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002707 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002708 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002709 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002710 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002711 {
2712 if (parmp->window_layout == WIN_TABS)
2713 goto_tabpage(1);
2714 else
2715 curwin = firstwin;
2716 }
2717 else if (parmp->window_layout == WIN_TABS)
2718 {
2719 if (curtab->tp_next == NULL)
2720 break;
2721 goto_tabpage(0);
2722 }
2723 else
2724 {
2725 if (curwin->w_next == NULL)
2726 break;
2727 curwin = curwin->w_next;
2728 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002729 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002730 curbuf = curwin->w_buffer;
2731 if (curbuf->b_ml.ml_mfp == NULL)
2732 {
2733#ifdef FEAT_FOLDING
2734 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2735 if (p_fdls >= 0)
2736 curwin->w_p_fdl = p_fdls;
2737#endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002738 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002739 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002740
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002741 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002742
2743 /* create memfile, read file */
2744 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002745
Bram Moolenaar84212822006-11-07 21:59:47 +00002746 if (swap_exists_action == SEA_QUIT)
2747 {
2748 if (got_int || only_one_window())
2749 {
2750 /* abort selected or quit and only one window */
2751 did_emsg = FALSE; /* avoid hit-enter prompt */
2752 getout(1);
2753 }
2754 /* We can't close the window, it would disturb what
2755 * happens next. Clear the file name and set the arg
2756 * index to -1 to delete it later. */
2757 setfname(curbuf, NULL, NULL, FALSE);
2758 curwin->w_arg_idx = -1;
2759 swap_exists_action = SEA_NONE;
2760 }
2761 else
2762 handle_swap_exists(NULL);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002763 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002764 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002765 ui_breakcheck();
2766 if (got_int)
2767 {
2768 (void)vgetc(); /* only break the file loading, not the rest */
2769 break;
2770 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002771 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002772 if (parmp->window_layout == WIN_TABS)
2773 goto_tabpage(1);
2774 else
2775 curwin = firstwin;
2776 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002777 --autocmd_no_enter;
2778 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002779 }
2780}
2781
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002782 /*
2783 * If opened more than one window, start editing files in the other
2784 * windows. make_windows() has already opened the windows.
2785 */
2786 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002787edit_buffers(
2788 mparm_T *parmp,
2789 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002790{
2791 int arg_idx; /* index in argument list */
2792 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002793 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002794 win_T *win;
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002795 char_u *p_shm_save = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002796
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002797 /*
2798 * Don't execute Win/Buf Enter/Leave autocommands here
2799 */
2800 ++autocmd_no_enter;
2801 ++autocmd_no_leave;
Bram Moolenaar84212822006-11-07 21:59:47 +00002802
2803 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2804 if (curwin->w_arg_idx == -1)
2805 {
2806 win_close(curwin, TRUE);
2807 advance = FALSE;
2808 }
2809
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002810 arg_idx = 1;
2811 for (i = 1; i < parmp->window_count; ++i)
2812 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002813 if (cwd != NULL)
2814 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002815 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2816 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002817 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002818 ++arg_idx;
2819 win_close(curwin, TRUE);
2820 advance = FALSE;
2821 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002822 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002823
Bram Moolenaar84212822006-11-07 21:59:47 +00002824 if (advance)
2825 {
2826 if (parmp->window_layout == WIN_TABS)
2827 {
2828 if (curtab->tp_next == NULL) /* just checking */
2829 break;
2830 goto_tabpage(0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002831 // Temporarily reset 'shm' option to not print fileinfo when
2832 // loading the other buffers. This would overwrite the already
2833 // existing fileinfo for the first tab.
2834 if (i == 1)
2835 {
2836 char buf[100];
2837
2838 p_shm_save = vim_strsave(p_shm);
2839 vim_snprintf(buf, 100, "F%s", p_shm);
2840 set_option_value((char_u *)"shm", 0L, (char_u *)buf, 0);
2841 }
Bram Moolenaar84212822006-11-07 21:59:47 +00002842 }
2843 else
2844 {
2845 if (curwin->w_next == NULL) /* just checking */
2846 break;
2847 win_enter(curwin->w_next, FALSE);
2848 }
2849 }
2850 advance = TRUE;
2851
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002852 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002853 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002854 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2855 {
2856 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002857 /* Edit file from arg list, if there is one. When "Quit" selected
2858 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002859 swap_exists_did_quit = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002860 (void)do_ecmd(0, arg_idx < GARGCOUNT
2861 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002862 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002863 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002864 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002865 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002866 if (got_int || only_one_window())
2867 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002868 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002869 did_emsg = FALSE; /* avoid hit-enter prompt */
2870 getout(1);
2871 }
2872 win_close(curwin, TRUE);
2873 advance = FALSE;
2874 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002875 if (arg_idx == GARGCOUNT - 1)
2876 arg_had_last = TRUE;
2877 ++arg_idx;
2878 }
2879 ui_breakcheck();
2880 if (got_int)
2881 {
2882 (void)vgetc(); /* only break the file loading, not the rest */
2883 break;
2884 }
2885 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002886
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002887 if (p_shm_save != NULL)
2888 {
2889 set_option_value((char_u *)"shm", 0L, p_shm_save, 0);
2890 vim_free(p_shm_save);
2891 }
2892
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002893 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 Moolenaar558ca4a2019-04-04 18:15:38 +02003198 current_sctx.sc_version = 1;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003199#endif
3200 do_cmdline_cmd(initstr);
3201 sourcing_name = save_sourcing_name;
3202 sourcing_lnum = save_sourcing_lnum;
3203#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003204 current_sctx = save_current_sctx;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003205#endif
3206 return OK;
3207 }
3208 return FAIL;
3209}
3210
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003211#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003212/*
3213 * Return TRUE if we are certain the user owns the file "fname".
3214 * Used for ".vimrc" and ".exrc".
3215 * Use both stat() and lstat() for extra security.
3216 */
3217 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003218file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003219{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003220 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003221# ifdef UNIX
3222 uid_t uid = getuid();
3223# else /* VMS */
3224 uid_t uid = ((getgid() << 16) | getuid());
3225# endif
3226
3227 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3228# ifdef HAVE_LSTAT
3229 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3230# endif
3231 );
3232}
3233#endif
3234
3235/*
3236 * Give an error message main_errors["n"] and exit.
3237 */
3238 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003239mainerr(
3240 int n, /* one of the ME_ defines */
3241 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003242{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003243#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003244 reset_signals(); /* kill us with CTRL-C here, if you like */
3245#endif
3246
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003247 // If this is a Windows GUI executable, show an error dialog box.
3248#ifdef VIMDLL
3249 gui.in_use = mch_is_gui_executable();
3250#endif
3251#ifdef FEAT_GUI_MSWIN
3252 gui.starting = FALSE; // Needed to show as error.
3253#endif
3254
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003255 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003256 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003257 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003258 mch_errmsg(_(main_errors[n]));
3259 if (str != NULL)
3260 {
3261 mch_errmsg(": \"");
3262 mch_errmsg((char *)str);
3263 mch_errmsg("\"");
3264 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003265 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003266
3267 mch_exit(1);
3268}
3269
3270 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003271mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003272{
3273 mainerr(ME_ARG_MISSING, str);
3274}
3275
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003276#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003277/*
3278 * print a message with three spaces prepended and '\n' appended.
3279 */
3280 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003281main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003282{
3283 mch_msg(" ");
3284 mch_msg(s);
3285 mch_msg("\n");
3286}
3287
3288/*
3289 * Print messages for "vim -h" or "vim --help" and exit.
3290 */
3291 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003292usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003293{
3294 int i;
3295 static char *(use[]) =
3296 {
3297 N_("[file ..] edit specified file(s)"),
3298 N_("- read text from stdin"),
3299 N_("-t tag edit file where tag is defined"),
3300#ifdef FEAT_QUICKFIX
3301 N_("-q [errorfile] edit file with first error")
3302#endif
3303 };
3304
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003305#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003306 reset_signals(); /* kill us with CTRL-C here, if you like */
3307#endif
3308
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003309 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003310 mch_msg(longVersion);
Bram Moolenaar32aaf5a2018-06-21 21:38:33 +02003311 mch_msg(_("\n\nUsage:"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003312 for (i = 0; ; ++i)
3313 {
3314 mch_msg(_(" vim [arguments] "));
3315 mch_msg(_(use[i]));
3316 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3317 break;
3318 mch_msg(_("\n or:"));
3319 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003320#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003321 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003322#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003323
3324 mch_msg(_("\n\nArguments:\n"));
3325 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003326#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003327 main_msg(_("--literal\t\tDon't expand wildcards"));
3328#endif
3329#ifdef FEAT_OLE
3330 main_msg(_("-register\t\tRegister this gvim for OLE"));
3331 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3332#endif
3333#ifdef FEAT_GUI
3334 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3335 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3336#endif
3337 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3338 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003339 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003340 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3341#ifdef FEAT_DIFF
3342 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3343#endif
3344 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3345 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3346 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3347 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3348 main_msg(_("-M\t\t\tModifications in text not allowed"));
3349 main_msg(_("-b\t\t\tBinary mode"));
3350#ifdef FEAT_LISP
3351 main_msg(_("-l\t\t\tLisp mode"));
3352#endif
3353 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3354 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003355 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3356#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003357 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003358#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003359 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3360 main_msg(_("-r\t\t\tList swap files and exit"));
3361 main_msg(_("-r (with file name)\tRecover crashed session"));
3362 main_msg(_("-L\t\t\tSame as -r"));
3363#ifdef AMIGA
3364 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3365 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3366#endif
3367#ifdef FEAT_ARABIC
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003368 main_msg(_("-A\t\t\tStart in Arabic mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003369#endif
3370#ifdef FEAT_RIGHTLEFT
3371 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3372#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003373 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003374 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003375 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003376 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3377#ifdef FEAT_GUI
3378 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3379#endif
3380 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003381 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003382 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3383 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3384 main_msg(_("+\t\t\tStart at end of file"));
3385 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003386 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003387 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3388 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3389 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3390 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3391 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3392#ifdef FEAT_CRYPT
3393 main_msg(_("-x\t\t\tEdit encrypted files"));
3394#endif
3395#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3396# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3397 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3398# endif
3399 main_msg(_("-X\t\t\tDo not connect to X server"));
3400#endif
3401#ifdef FEAT_CLIENTSERVER
3402 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3403 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3404 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3405 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003406 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003407 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3408 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3409 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3410 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3411#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003412#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003413 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003414#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003415#ifdef FEAT_VIMINFO
3416 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3417#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003418 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003419 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3420 main_msg(_("--version\t\tPrint version information and exit"));
3421
3422#ifdef FEAT_GUI_X11
3423# ifdef FEAT_GUI_MOTIF
3424 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3425# else
3426# ifdef FEAT_GUI_ATHENA
3427# ifdef FEAT_GUI_NEXTAW
3428 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3429# else
3430 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3431# endif
3432# endif
3433# endif
3434 main_msg(_("-display <display>\tRun vim on <display>"));
3435 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003436 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3437 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3438 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3439 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3440 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3441 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3442 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3443 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3444# ifdef FEAT_GUI_ATHENA
3445 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3446# endif
3447 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3448 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3449 main_msg(_("-xrm <resource>\tSet the specified resource"));
3450#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003451#ifdef FEAT_GUI_GTK
3452 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3453 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3454 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3455 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3456 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003457 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003458 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003459 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003460#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003461#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003462# ifdef VIMDLL
3463 if (gui.starting)
3464# endif
3465 {
3466 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
3467 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
3468 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003469#endif
3470
3471#ifdef FEAT_GUI_GNOME
3472 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3473 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003474 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003475 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003476 gui.dofork = FALSE;
3477 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003478 else
3479#endif
3480 mch_exit(0);
3481}
3482
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003483/*
3484 * Check the result of the ATTENTION dialog:
3485 * When "Quit" selected, exit Vim.
3486 * When "Recover" selected, recover the file.
3487 */
3488 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003489check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003490{
3491 if (swap_exists_action == SEA_QUIT)
3492 getout(1);
3493 handle_swap_exists(NULL);
3494}
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003495
Bram Moolenaar08cab962017-03-04 14:37:18 +01003496#endif /* NO_VIM_MAIN */
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003497
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003498#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003499static struct timeval prev_timeval;
3500
Bram Moolenaar4f974752019-02-17 17:44:42 +01003501# ifdef MSWIN
Bram Moolenaar3f269672009-11-03 11:11:11 +00003502/*
3503 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3504 */
3505 static int
3506gettimeofday(struct timeval *tv, char *dummy)
3507{
3508 long t = clock();
3509 tv->tv_sec = t / CLOCKS_PER_SEC;
3510 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3511 return 0;
3512}
3513# endif
3514
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003515/*
3516 * Save the previous time before doing something that could nest.
3517 * set "*tv_rel" to the time elapsed so far.
3518 */
3519 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003520time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003521{
3522 *((struct timeval *)tv_rel) = prev_timeval;
3523 gettimeofday(&prev_timeval, NULL);
3524 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3525 - ((struct timeval *)tv_rel)->tv_usec;
3526 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3527 - ((struct timeval *)tv_rel)->tv_sec;
3528 if (((struct timeval *)tv_rel)->tv_usec < 0)
3529 {
3530 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3531 --((struct timeval *)tv_rel)->tv_sec;
3532 }
3533 *(struct timeval *)tv_start = prev_timeval;
3534}
3535
3536/*
3537 * Compute the previous time after doing something that could nest.
3538 * Subtract "*tp" from prev_timeval;
3539 * Note: The arguments are (void *) to avoid trouble with systems that don't
3540 * have struct timeval.
3541 */
3542 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003543time_pop(
3544 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003545{
3546 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3547 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3548 if (prev_timeval.tv_usec < 0)
3549 {
3550 prev_timeval.tv_usec += 1000000;
3551 --prev_timeval.tv_sec;
3552 }
3553}
3554
3555 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003556time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003557{
3558 long usec;
3559 long msec;
3560
3561 usec = now->tv_usec - then->tv_usec;
3562 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3563 usec = usec % 1000L;
3564 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3565}
3566
3567 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003568time_msg(
3569 char *mesg,
3570 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003571 (struct timeval *) */
3572{
3573 static struct timeval start;
3574 struct timeval now;
3575
3576 if (time_fd != NULL)
3577 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003578 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003579 {
3580 gettimeofday(&start, NULL);
3581 prev_timeval = start;
3582 fprintf(time_fd, "\n\ntimes in msec\n");
3583 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3584 fprintf(time_fd, " clock elapsed: other lines\n\n");
3585 }
3586 gettimeofday(&now, NULL);
3587 time_diff(&start, &now);
3588 if (((struct timeval *)tv_start) != NULL)
3589 {
3590 fprintf(time_fd, " ");
3591 time_diff(((struct timeval *)tv_start), &now);
3592 }
3593 fprintf(time_fd, " ");
3594 time_diff(&prev_timeval, &now);
3595 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003596 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003597 }
3598}
3599
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003600#endif
3601
Bram Moolenaar595297d2017-03-04 19:11:12 +01003602#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003603 static void
3604set_progpath(char_u *argv0)
3605{
3606 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003607
Bram Moolenaar4f974752019-02-17 17:44:42 +01003608# ifdef MSWIN
Bram Moolenaar08cab962017-03-04 14:37:18 +01003609 /* A relative path containing a "/" will become invalid when using ":cd",
3610 * turn it into a full path.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003611 * On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3612 * this. */
Bram Moolenaar066029e2017-03-05 15:19:32 +01003613 char_u *path = NULL;
3614
3615 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3616 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003617# else
3618 char_u buf[MAXPATHL + 1];
3619# ifdef PROC_EXE_LINK
3620 char linkbuf[MAXPATHL + 1];
3621 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003622
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003623 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3624 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003625 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003626 linkbuf[len] = NUL;
3627 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003628 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003629# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003630
3631 if (!mch_isFullName(val))
3632 {
3633 if (gettail(val) != val
3634 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3635 val = buf;
3636 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003637# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003638
Bram Moolenaar08cab962017-03-04 14:37:18 +01003639 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003640
Bram Moolenaar4f974752019-02-17 17:44:42 +01003641# ifdef MSWIN
Bram Moolenaar43663192017-03-05 14:29:12 +01003642 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003643# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003644}
3645
3646#endif /* NO_VIM_MAIN */
3647
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003648#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003649
3650/*
3651 * Common code for the X command server and the Win32 command server.
3652 */
3653
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003654static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003655
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003656/*
3657 * Do the client-server stuff, unless "--servername ''" was used.
3658 */
3659 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003660exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003661{
3662 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3663 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003664# ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003665 /* Initialise the client/server messaging infrastructure. */
3666 serverInitMessaging();
3667# endif
3668
3669 /*
3670 * When a command server argument was found, execute it. This may
3671 * exit Vim when it was successful. Otherwise it's executed further
3672 * on. Remember the encoding used here in "serverStrEnc".
3673 */
3674 if (parmp->serverArg)
3675 {
3676 cmdsrv_main(&parmp->argc, parmp->argv,
3677 parmp->serverName_arg, &parmp->serverStr);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003678 parmp->serverStrEnc = vim_strsave(p_enc);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003679 }
3680
3681 /* If we're still running, get the name to register ourselves.
3682 * On Win32 can register right now, for X11 need to setup the
3683 * clipboard first, it's further down. */
3684 parmp->servername = serverMakeName(parmp->serverName_arg,
3685 parmp->argv[0]);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003686# ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003687 if (parmp->servername != NULL)
3688 {
3689 serverSetName(parmp->servername);
3690 vim_free(parmp->servername);
3691 }
3692# endif
3693 }
3694}
3695
3696/*
3697 * Prepare for running as a Vim server.
3698 */
3699 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003700prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003701{
3702# if defined(FEAT_X11)
3703 /*
3704 * Register for remote command execution with :serversend and --remote
3705 * unless there was a -X or a --servername '' on the command line.
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003706 * Only register nongui-vim's with an explicit --servername argument,
3707 * or when compiling with autoservername.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003708 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003709 */
3710 if (X_DISPLAY != NULL && parmp->servername != NULL && (
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003711# if defined(FEAT_AUTOSERVERNAME) || defined(FEAT_GUI)
3712 (
3713# if defined(FEAT_AUTOSERVERNAME)
3714 1
3715# else
3716 gui.in_use
3717# endif
Bram Moolenaar5f402312006-08-15 19:40:35 +00003718# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003719 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003720# endif
3721 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003722# endif
3723 parmp->serverName_arg != NULL))
3724 {
3725 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3726 vim_free(parmp->servername);
3727 TIME_MSG("register server name");
3728 }
3729 else
3730 serverDelayedStartName = parmp->servername;
3731# endif
3732
3733 /*
3734 * Execute command ourselves if we're here because the send failed (or
3735 * else we would have exited above).
3736 */
3737 if (parmp->serverStr != NULL)
3738 {
3739 char_u *p;
3740
3741 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3742 parmp->serverStr, &p));
3743 vim_free(p);
3744 }
3745}
3746
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003747 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003748cmdsrv_main(
3749 int *argc,
3750 char **argv,
3751 char_u *serverName_arg,
3752 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003753{
3754 char_u *res;
3755 int i;
3756 char_u *sname;
3757 int ret;
3758 int didone = FALSE;
3759 int exiterr = 0;
3760 char **newArgV = argv + 1;
3761 int newArgC = 1,
3762 Argc = *argc;
3763 int argtype;
3764#define ARGTYPE_OTHER 0
3765#define ARGTYPE_EDIT 1
3766#define ARGTYPE_EDIT_WAIT 2
3767#define ARGTYPE_SEND 3
3768 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003769 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003770# ifndef FEAT_X11
3771 HWND srv;
3772# else
3773 Window srv;
3774
3775 setup_term_clip();
3776# endif
3777
3778 sname = serverMakeName(serverName_arg, argv[0]);
3779 if (sname == NULL)
3780 return;
3781
3782 /*
3783 * Execute the command server related arguments and remove them
3784 * from the argc/argv array; We may have to return into main()
3785 */
3786 for (i = 1; i < Argc; i++)
3787 {
3788 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003789 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003790 {
3791 for (; i < *argc; i++)
3792 {
3793 *newArgV++ = argv[i];
3794 newArgC++;
3795 }
3796 break;
3797 }
3798
Bram Moolenaareb94e552006-03-11 21:35:11 +00003799 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003800 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003801 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3802 {
3803 char *p = argv[i] + 8;
3804
3805 argtype = ARGTYPE_EDIT;
3806 while (*p != NUL)
3807 {
3808 if (STRNICMP(p, "-wait", 5) == 0)
3809 {
3810 argtype = ARGTYPE_EDIT_WAIT;
3811 p += 5;
3812 }
3813 else if (STRNICMP(p, "-silent", 7) == 0)
3814 {
3815 silent = TRUE;
3816 p += 7;
3817 }
3818 else if (STRNICMP(p, "-tab", 4) == 0)
3819 {
3820 tabs = TRUE;
3821 p += 4;
3822 }
3823 else
3824 {
3825 argtype = ARGTYPE_OTHER;
3826 break;
3827 }
3828 }
3829 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003830 else
3831 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003832
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003833 if (argtype != ARGTYPE_OTHER)
3834 {
3835 if (i == *argc - 1)
3836 mainerr_arg_missing((char_u *)argv[i]);
3837 if (argtype == ARGTYPE_SEND)
3838 {
3839 *serverStr = (char_u *)argv[i + 1];
3840 i++;
3841 }
3842 else
3843 {
3844 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003845 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003846 if (*serverStr == NULL)
3847 {
3848 /* Probably out of memory, exit. */
3849 didone = TRUE;
3850 exiterr = 1;
3851 break;
3852 }
3853 Argc = i;
3854 }
3855# ifdef FEAT_X11
3856 if (xterm_dpy == NULL)
3857 {
3858 mch_errmsg(_("No display"));
3859 ret = -1;
3860 }
3861 else
3862 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003863 NULL, &srv, 0, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003864# else
3865 /* Win32 always works? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003866 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003867# endif
3868 if (ret < 0)
3869 {
3870 if (argtype == ARGTYPE_SEND)
3871 {
3872 /* Failed to send, abort. */
3873 mch_errmsg(_(": Send failed.\n"));
3874 didone = TRUE;
3875 exiterr = 1;
3876 }
3877 else if (!silent)
3878 /* Let vim start normally. */
3879 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3880 break;
3881 }
3882
Bram Moolenaar4f974752019-02-17 17:44:42 +01003883# ifdef FEAT_GUI_MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003884 /* Guess that when the server name starts with "g" it's a GUI
3885 * server, which we can bring to the foreground here.
3886 * Foreground() in the server doesn't work very well. */
3887 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3888 SetForegroundWindow(srv);
3889# endif
3890
3891 /*
3892 * For --remote-wait: Wait until the server did edit each
3893 * file. Also detect that the server no longer runs.
3894 */
3895 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3896 {
3897 int numFiles = *argc - i - 1;
3898 int j;
3899 char_u *done = alloc(numFiles);
3900 char_u *p;
Bram Moolenaar4f974752019-02-17 17:44:42 +01003901# ifdef FEAT_GUI_MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003902 NOTIFYICONDATA ni;
3903 int count = 0;
3904 extern HWND message_window;
3905# endif
3906
3907 if (numFiles > 0 && argv[i + 1][0] == '+')
3908 /* Skip "+cmd" argument, don't wait for it to be edited. */
3909 --numFiles;
3910
Bram Moolenaar4f974752019-02-17 17:44:42 +01003911# ifdef FEAT_GUI_MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003912 ni.cbSize = sizeof(ni);
3913 ni.hWnd = message_window;
3914 ni.uID = 0;
3915 ni.uFlags = NIF_ICON|NIF_TIP;
3916 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3917 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3918 Shell_NotifyIcon(NIM_ADD, &ni);
3919# endif
3920
3921 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003922 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003923 while (memchr(done, 0, numFiles) != NULL)
3924 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003925# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003926 p = serverGetReply(srv, NULL, TRUE, TRUE, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003927 if (p == NULL)
3928 break;
3929# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003930 if (serverReadReply(xterm_dpy, srv, &p, TRUE, -1) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003931 break;
3932# endif
3933 j = atoi((char *)p);
3934 if (j >= 0 && j < numFiles)
3935 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003936# ifdef FEAT_GUI_MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003937 ++count;
3938 sprintf(ni.szTip, _("%d of %d edited"),
3939 count, numFiles);
3940 Shell_NotifyIcon(NIM_MODIFY, &ni);
3941# endif
3942 done[j] = 1;
3943 }
3944 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003945# ifdef FEAT_GUI_MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003946 Shell_NotifyIcon(NIM_DELETE, &ni);
3947# endif
3948 }
3949 }
3950 else if (STRICMP(argv[i], "--remote-expr") == 0)
3951 {
3952 if (i == *argc - 1)
3953 mainerr_arg_missing((char_u *)argv[i]);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003954# ifdef MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003955 /* Win32 always works? */
3956 if (serverSendToVim(sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003957 &res, NULL, 1, 0, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003958# else
3959 if (xterm_dpy == NULL)
3960 mch_errmsg(_("No display: Send expression failed.\n"));
3961 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003962 &res, NULL, 1, 0, 1, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003963# endif
3964 {
3965 if (res != NULL && *res != NUL)
3966 {
3967 /* Output error from remote */
3968 mch_errmsg((char *)res);
Bram Moolenaard23a8232018-02-10 18:45:26 +01003969 VIM_CLEAR(res);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003970 }
3971 mch_errmsg(_(": Send expression failed.\n"));
3972 }
3973 }
3974 else if (STRICMP(argv[i], "--serverlist") == 0)
3975 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003976# ifdef MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003977 /* Win32 always works? */
3978 res = serverGetVimNames();
3979# else
3980 if (xterm_dpy != NULL)
3981 res = serverGetVimNames(xterm_dpy);
3982# endif
3983 if (called_emsg)
3984 mch_errmsg("\n");
3985 }
3986 else if (STRICMP(argv[i], "--servername") == 0)
3987 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003988 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003989 i++;
3990 continue;
3991 }
3992 else
3993 {
3994 *newArgV++ = argv[i];
3995 newArgC++;
3996 continue;
3997 }
3998 didone = TRUE;
3999 if (res != NULL && *res != NUL)
4000 {
4001 mch_msg((char *)res);
4002 if (res[STRLEN(res) - 1] != '\n')
4003 mch_msg("\n");
4004 }
4005 vim_free(res);
4006 }
4007
4008 if (didone)
4009 {
4010 display_errors(); /* display any collected messages */
4011 exit(exiterr); /* Mission accomplished - get out */
4012 }
4013
4014 /* Return back into main() */
4015 *argc = newArgC;
4016 vim_free(sname);
4017}
4018
4019/*
4020 * Build a ":drop" command to send to a Vim server.
4021 */
4022 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004023build_drop_cmd(
4024 int filec,
4025 char **filev,
4026 int tabs, /* Use ":tab drop" instead of ":drop". */
4027 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004028{
4029 garray_T ga;
4030 int i;
4031 char_u *inicmd = NULL;
4032 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004033 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004034 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004035
4036 if (filec > 0 && filev[0][0] == '+')
4037 {
4038 inicmd = (char_u *)filev[0] + 1;
4039 filev++;
4040 filec--;
4041 }
4042 /* Check if we have at least one argument. */
4043 if (filec <= 0)
4044 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004045
4046 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02004047 cwd = alloc(MAXPATHL);
4048 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004049 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004050 if (mch_dirname(cwd, MAXPATHL) != OK)
4051 {
4052 vim_free(cwd);
4053 return NULL;
4054 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004055 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00004056#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004057 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00004058#else
4059 PATH_ESC_CHARS,
4060#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02004061 '\\', TRUE);
4062 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004063 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004064 return NULL;
4065 ga_init2(&ga, 1, 100);
4066 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004067 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00004068
4069 /* Call inputsave() so that a prompt for an encryption key works. */
4070 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
4071 if (tabs)
4072 ga_concat(&ga, (char_u *)"tab ");
4073 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004074 for (i = 0; i < filec; i++)
4075 {
4076 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004077 * do it again in the Vim server. On MS-Windows only escape
4078 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004079 p = vim_strsave_escaped((char_u *)filev[i],
4080#ifdef UNIX
4081 PATH_ESC_CHARS
4082#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004083 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004084#endif
4085 );
4086 if (p == NULL)
4087 {
4088 vim_free(ga.ga_data);
4089 return NULL;
4090 }
4091 ga_concat(&ga, (char_u *)" ");
4092 ga_concat(&ga, p);
4093 vim_free(p);
4094 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004095 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4096
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004097 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4098 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004099 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4100
4101 /* Switch back to the correct current directory (prior to temporary path
4102 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004103 * correct after the :drop command. With line breaks and spaces:
4104 * if !exists('+acd') || !&acd
4105 * if haslocaldir()
4106 * cd -
4107 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004108 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004109 * cd -
4110 * endif
4111 * endif
4112 */
4113 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004114 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004115 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004116 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004117 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004118
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004119 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004120 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4121 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004122 if (inicmd != NULL)
4123 {
4124 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4125 * the following commands to be inserted as text. Use a "|",
4126 * hopefully "inicmd" does allow this... */
4127 ga_concat(&ga, inicmd);
4128 ga_concat(&ga, (char_u *)"|");
4129 }
4130 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4131 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004132 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004133 ga_append(&ga, NUL);
4134 return ga.ga_data;
4135}
4136
4137/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004138 * Make our basic server name: use the specified "arg" if given, otherwise use
4139 * the tail of the command "cmd" we were started with.
4140 * Return the name in allocated memory. This doesn't include a serial number.
4141 */
4142 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004143serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004144{
4145 char_u *p;
4146
4147 if (arg != NULL && *arg != NUL)
4148 p = vim_strsave_up(arg);
4149 else
4150 {
4151 p = vim_strsave_up(gettail((char_u *)cmd));
4152 /* Remove .exe or .bat from the name. */
4153 if (p != NULL && vim_strchr(p, '.') != NULL)
4154 *vim_strchr(p, '.') = NUL;
4155 }
4156 return p;
4157}
4158#endif /* FEAT_CLIENTSERVER */
4159
4160#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4161/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004162 * Replace termcodes such as <CR> and insert as key presses if there is room.
4163 */
4164 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004165server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004166{
4167 char_u *ptr = NULL;
4168 char_u *cpo_save = p_cpo;
4169
4170 /* Set 'cpoptions' the way we want it.
4171 * B set - backslashes are *not* treated specially
4172 * k set - keycodes are *not* reverse-engineered
4173 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004174 * The last but one parameter of replace_termcodes() is TRUE so that the
4175 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004176 */
4177 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004178 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004179 p_cpo = cpo_save;
4180
4181 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4182 {
4183 /*
4184 * Add the string to the input stream.
4185 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4186 *
4187 * First clear typed characters from the typeahead buffer, there could
4188 * be half a mapping there. Then append to the existing string, so
4189 * that multiple commands from a client are concatenated.
4190 */
4191 if (typebuf.tb_maplen < typebuf.tb_len)
4192 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4193 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4194
4195 /* Let input_available() know we inserted text in the typeahead
4196 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004197 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004198 }
4199 vim_free((char_u *)ptr);
4200}
4201
4202/*
4203 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004204 */
4205 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004206eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004207{
4208 char_u *res;
4209 int save_dbl = debug_break_level;
4210 int save_ro = redir_off;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02004211 funccal_entry_T funccal_entry;
4212 int did_save_funccal = FALSE;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004213
4214 /* Evaluate the expression at the toplevel, don't use variables local to
Bram Moolenaard99388b2017-10-26 14:28:32 +02004215 * the calling function. Except when in debug mode. */
4216 if (!debug_mode)
Bram Moolenaar27e80c82018-10-14 21:41:01 +02004217 {
4218 save_funccal(&funccal_entry);
4219 did_save_funccal = TRUE;
4220 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004221
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004222 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4223 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004224 debug_break_level = -1;
4225 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004226 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4227 * to be typed. Do generate errors so that try/catch works. */
4228 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004229
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004230 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004231
4232 debug_break_level = save_dbl;
4233 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004234 --emsg_silent;
4235 if (emsg_silent < 0)
4236 emsg_silent = 0;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02004237 if (did_save_funccal)
4238 restore_funccal();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004239
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004240 /* A client can tell us to redraw, but not to display the cursor, so do
4241 * that here. */
4242 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01004243 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004244
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004245 return res;
4246}
4247
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004248/*
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004249 * Evaluate a command or expression sent to ourselves.
4250 */
4251 int
4252sendToLocalVim(char_u *cmd, int asExpr, char_u **result)
4253{
4254 if (asExpr)
4255 {
4256 char_u *ret;
4257
4258 ret = eval_client_expr_to_string(cmd);
4259 if (result != NULL)
4260 {
4261 if (ret == NULL)
4262 {
4263 char *err = _(e_invexprmsg);
4264 size_t len = STRLEN(cmd) + STRLEN(err) + 5;
4265 char_u *msg;
4266
Bram Moolenaar1662ce12017-03-19 21:47:50 +01004267 msg = alloc((unsigned)len);
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004268 if (msg != NULL)
4269 vim_snprintf((char *)msg, len, "%s: \"%s\"", err, cmd);
4270 *result = msg;
4271 }
4272 else
4273 *result = ret;
4274 }
4275 else
4276 vim_free(ret);
4277 return ret == NULL ? -1 : 0;
4278 }
4279 server_to_input_buf(cmd);
4280 return 0;
4281}
4282
4283/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004284 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4285 * return an allocated string. Otherwise return "data".
4286 * "*tofree" is set to the result when it needs to be freed later.
4287 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004288 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004289serverConvert(
4290 char_u *client_enc UNUSED,
4291 char_u *data,
4292 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004293{
4294 char_u *res = data;
4295
4296 *tofree = NULL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004297 if (client_enc != NULL && p_enc != NULL)
4298 {
4299 vimconv_T vimconv;
4300
4301 vimconv.vc_type = CONV_NONE;
4302 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4303 && vimconv.vc_type != CONV_NONE)
4304 {
4305 res = string_convert(&vimconv, data, NULL);
4306 if (res == NULL)
4307 res = data;
4308 else
4309 *tofree = res;
4310 }
4311 convert_setup(&vimconv, NULL, NULL);
4312 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004313 return res;
4314}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004315#endif