blob: 42aa9903434750b9b4d2c08e62577af8911bb825 [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 +0000101VimMain
102# else
103main
104# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100105(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000106{
Bram Moolenaar77780b62018-03-01 23:10:45 +0100107#if defined(STARTUPTIME) || defined(CLEAN_RUNTIMEPATH)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000108 int i;
109#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000110
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000111 /*
112 * Do any system-specific initialisations. These can NOT use IObuff or
113 * NameBuff. Thus emsg2() cannot be called!
114 */
115 mch_early_init();
116
Bram Moolenaar4f974752019-02-17 17:44:42 +0100117#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +0200118 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200119 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200120 * convert when 'encoding' changes. Get the unexpanded arguments.
121 */
122 argc = get_cmd_argsW(&argv);
123#endif
124
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000125 /* Many variables are in "params" so that we can pass them to invoked
126 * functions without a lot of arguments. "argc" and "argv" are also
127 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000128 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000129 params.argc = argc;
130 params.argv = argv;
131 params.want_full_screen = TRUE;
132#ifdef FEAT_EVAL
133 params.use_debug_break_level = -1;
134#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000135 params.window_count = -1;
Bram Moolenaar58d98232005-07-23 22:25:46 +0000136
Bram Moolenaar99685e62013-05-11 13:56:18 +0200137#ifdef FEAT_RUBY
138 {
139 int ruby_stack_start;
140 vim_ruby_init((void *)&ruby_stack_start);
141 }
142#endif
143
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000144#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000145 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000146#endif
147
148#ifdef MEM_PROFILE
149 atexit(vim_mem_profile_dump);
150#endif
151
152#ifdef STARTUPTIME
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200153 /* Need to find "--startuptime" before actually parsing arguments. */
Bram Moolenaar07268702018-03-01 21:57:32 +0100154 for (i = 1; i < argc - 1; ++i)
155 if (STRICMP(argv[i], "--startuptime") == 0)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000156 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000157 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000158 TIME_MSG("--- VIM STARTING ---");
159 break;
160 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000161#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000162 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000163
Bram Moolenaar07268702018-03-01 21:57:32 +0100164#ifdef CLEAN_RUNTIMEPATH
165 /* Need to find "--clean" before actually parsing arguments. */
166 for (i = 1; i < argc; ++i)
167 if (STRICMP(argv[i], "--clean") == 0)
168 {
169 params.clean = TRUE;
170 break;
171 }
172#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200173 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000174
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200175#ifdef VIMDLL
176 // Check if the current executable file is for the GUI subsystem.
177 gui.starting = mch_is_gui_executable();
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +0200178#elif defined(FEAT_GUI_MSWIN)
179 gui.starting = TRUE;
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200180#endif
181
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000182#ifdef FEAT_CLIENTSERVER
183 /*
184 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000185 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000186 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000187 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000188#endif
189
190 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000191 * Figure out the way to work from the command name argv[0].
192 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000193 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000194 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000195
196 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000197 * Process the command line arguments. File names are put in the global
198 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000199 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000200 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000201 TIME_MSG("parsing arguments");
202
203 /*
204 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000205 * there is no terminal version, and on Windows we can't fork one off with
206 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000207 */
208#ifdef ALWAYS_USE_GUI
209 gui.starting = TRUE;
210#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000211# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000212 /*
213 * Check if the GUI can be started. Reset gui.starting if not.
214 * Don't know about other systems, stay on the safe side and don't check.
215 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000216 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000217 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000218 if (gui_init_check() == FAIL)
219 {
220 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000221
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000222 /* When running "evim" or "gvim -y" we need the menus, exit if we
223 * don't have them. */
224 if (params.evim_mode)
225 mch_exit(1);
226 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000227 }
228# endif
229#endif
230
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000231 if (GARGCOUNT > 0)
232 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100233#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000234 /*
235 * Expand wildcards in file names.
236 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000237 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000238 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200239 start_dir = alloc(MAXPATHL);
240 if (start_dir != NULL)
241 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000242 /* Temporarily add '(' and ')' to 'isfname'. These are valid
243 * filename characters but are excluded from 'isfname' to make
244 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
245 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000246 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000247 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200248 if (start_dir != NULL)
249 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000250 }
251#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200252 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000253 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000254
Bram Moolenaar4f974752019-02-17 17:44:42 +0100255#ifdef MSWIN
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000256 {
257 extern void set_alist_count(void);
258
259 /* Remember the number of entries in the argument list. If it changes
260 * we don't react on setting 'encoding'. */
261 set_alist_count();
262 }
263#endif
264
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000265#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000266 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000267 {
268 /*
269 * If there is one filename, fully qualified, we have very probably
270 * been invoked from explorer, so change to the file's directory.
271 * Hint: to avoid this when typing a command use a forward slash.
272 * If the cd fails, it doesn't matter.
273 */
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100274 (void)vim_chdirfile(params.fname, "drop");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200275 if (start_dir != NULL)
276 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000277 }
278#endif
279 TIME_MSG("expanding arguments");
280
281#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000282 if (params.diff_mode && params.window_count == -1)
283 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000284#endif
285
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000286 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000287 ++RedrawingDisabled;
288
289 /*
290 * When listing swap file names, don't do cursor positioning et. al.
291 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200292 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000293 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000294
295 /*
296 * When certain to start the GUI, don't check capabilities of terminal.
297 * For GTK we can't be sure, but when started from the desktop it doesn't
298 * make sense to try using a terminal.
299 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200300#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
301 || defined(VIMDLL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000302 if (gui.starting
303# ifdef FEAT_GUI_GTK
304 && !isatty(2)
305# endif
306 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000307 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000308#endif
309
Bram Moolenaard0573012017-10-28 21:11:06 +0200310#if defined(FEAT_GUI_MAC) && defined(MACOS_X_DARWIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000311 /* When the GUI is started from Finder, need to display messages in a
312 * message box. isatty(2) returns TRUE anyway, thus we need to check the
313 * name to know we're not started from a terminal. */
314 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000315 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000316 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000317
318 /* Avoid always using "/" as the current directory. Note that when
319 * started from Finder the arglist will be filled later in
320 * HandleODocAE() and "fname" will be NULL. */
321 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
322 && STRCMP(NameBuff, "/") == 0)
323 {
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200324 if (params.fname != NULL)
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100325 (void)vim_chdirfile(params.fname, "drop");
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000326 else
327 {
328 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
329 vim_chdir(NameBuff);
330 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200331 if (start_dir != NULL)
332 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000333 }
334 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000335#endif
336
337 /*
338 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100339 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000340 * Note that we may use mch_exit() before mch_init()!
341 */
342 mch_init();
343 TIME_MSG("shell init");
344
345#ifdef USE_XSMP
346 /*
347 * For want of anywhere else to do it, try to connect to xsmp here.
348 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
349 * Hijacking -X 'no X connection' to also disable XSMP connection as that
350 * has a similar delay upon failure.
351 * Only try if SESSION_MANAGER is set to something non-null.
352 */
353 if (!x_no_connect)
354 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000355 char *p = getenv("SESSION_MANAGER");
356
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000357 if (p != NULL && *p != NUL)
358 {
359 xsmp_init();
360 TIME_MSG("xsmp init");
361 }
362 }
363#endif
364
365 /*
366 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000367 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000368 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000369
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100370#ifdef _IOLBF
371 /* Ensure output works usefully without a tty: buffer lines instead of
372 * fully buffered. */
373 if (silent_mode)
374 setvbuf(stdout, NULL, _IOLBF, 0);
375#endif
376
Bram Moolenaar9404a182019-05-03 22:25:40 +0200377 // This message comes before term inits, but after setting "silent_mode"
378 // when the input is not a tty. Omit the message with --not-a-term.
379 if (GARGCOUNT > 1 && !silent_mode && !is_not_a_term())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000380 printf(_("%d files to edit\n"), GARGCOUNT);
381
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000382 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000383 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000384 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000385 capabilities (will set full_screen) */
386 screen_start(); /* don't know where cursor is now */
387 TIME_MSG("Termcap init");
388 }
389
390 /*
391 * Set the default values for the options that use Rows and Columns.
392 */
393 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000394 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000395#ifdef FEAT_DIFF
396 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
397 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000398 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000399 diff_win_options(firstwin, FALSE);
400#endif
401
402 cmdline_row = Rows - p_ch;
403 msg_row = cmdline_row;
404 screenalloc(FALSE); /* allocate screen buffers */
405 set_init_2();
406 TIME_MSG("inits 2");
407
408 msg_scroll = TRUE;
409 no_wait_return = TRUE;
410
411 init_mappings(); /* set up initial mappings */
412
413 init_highlight(TRUE, FALSE); /* set the default highlight groups */
414 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000415
416#ifdef FEAT_EVAL
417 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000418 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000419#endif
420
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200421 /* Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
422 * Allows for setting 'loadplugins' there. */
423 if (params.use_vimrc != NULL
424 && (STRCMP(params.use_vimrc, "NONE") == 0
425 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
426 p_lpl = FALSE;
427
428 /* Execute --cmd arguments. */
429 exe_pre_commands(&params);
430
431 /* Source startup scripts. */
432 source_startup_scripts(&params);
433
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100434#ifdef FEAT_MZSCHEME
435 /*
436 * Newer version of MzScheme (Racket) require earlier (trampolined)
437 * initialisation via scheme_main_setup.
438 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200439 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200440 * Do source startup scripts, so that 'mzschemedll' can be set.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100441 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200442 return mzscheme_main();
443#else
444 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100445#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200446}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100447#endif /* NO_VIM_MAIN */
Bram Moolenaara357e442016-08-10 20:45:07 +0200448#endif /* PROTO */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100449
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200450/*
451 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
452 * things simple.
453 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
454 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100455 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200456vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100457{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100458#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000459#ifdef FEAT_EVAL
460 /*
461 * Read all the plugin files.
462 * Only when compiled with +eval, since most plugins need it.
463 */
464 if (p_lpl)
465 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200466 char_u *rtp_copy = NULL;
467
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200468 /* First add all package directories to 'runtimepath', so that their
469 * autoload directories can be found. Only if not done already with a
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200470 * :packloadall command.
471 * Make a copy of 'runtimepath', so that source_runtime does not use
472 * the pack directories. */
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200473 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200474 {
475 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200476 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200477 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200478
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200479 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy,
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000480# ifdef VMS /* Somehow VMS doesn't handle the "**". */
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200481 (char_u *)"plugin/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000482# else
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200483 (char_u *)"plugin/**/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000484# endif
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200485 DIP_ALL | DIP_NOAFTER);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000486 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200487 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100488
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200489 /* Only source "start" packages if not done already with a :packloadall
490 * command. */
491 if (!did_source_packages)
492 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100493 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200494
495# ifdef VMS /* Somehow VMS doesn't handle the "**". */
496 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_AFTER);
497# else
498 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_AFTER);
499# endif
500 TIME_MSG("loading after plugins");
501
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000502 }
503#endif
504
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000505#ifdef FEAT_DIFF
506 /* Decide about window layout for diff mode after reading vimrc. */
507 if (params.diff_mode && params.window_layout == 0)
508 {
509 if (diffopt_horizontal())
510 params.window_layout = WIN_HOR; /* use horizontal split */
511 else
512 params.window_layout = WIN_VER; /* use vertical split */
513 }
514#endif
515
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000516 /*
517 * Recovery mode without a file name: List swap files.
518 * This uses the 'dir' option, therefore it must be after the
519 * initializations.
520 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200521 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000522 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200523 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000524 mch_exit(0);
525 }
526
527 /*
528 * Set a few option defaults after reading .vimrc files:
529 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
530 */
531 set_init_3();
532 TIME_MSG("inits 3");
533
534 /*
535 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
536 * Note that this overrides anything from a vimrc file.
537 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000538 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000539 p_uc = 0;
540
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000541#ifdef FEAT_GUI
542 if (gui.starting)
543 {
Bram Moolenaarc7226682019-08-17 16:33:23 +0200544# if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000545 /* When something caused a message from a vimrc script, need to output
546 * an extra newline before the shell prompt. */
547 if (did_emsg || msg_didout)
548 putchar('\n');
Bram Moolenaarc7226682019-08-17 16:33:23 +0200549# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000550
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200551 gui_start(NULL); /* will set full_screen to TRUE */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000552 TIME_MSG("starting GUI");
553
554 /* When running "evim" or "gvim -y" we need the menus, exit if we
555 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000556 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000557 mch_exit(1);
558 }
559#endif
560
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000561#ifdef FEAT_VIMINFO
562 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000563 * Read in registers, history etc, but not marks, from the viminfo file.
564 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000565 */
566 if (*p_viminfo != NUL)
567 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000568 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000569 TIME_MSG("reading viminfo");
570 }
571#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100572#ifdef FEAT_EVAL
573 /* It's better to make v:oldfiles an empty list than NULL. */
574 if (get_vim_var_list(VV_OLDFILES) == NULL)
575 set_vim_var_list(VV_OLDFILES, list_alloc());
576#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000577
578#ifdef FEAT_QUICKFIX
579 /*
580 * "-q errorfile": Load the error file now.
581 * If the error file can't be read, exit before doing anything else.
582 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000583 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000584 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100585 char_u *enc = NULL;
586
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100587 enc = p_menc;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000588 if (params.use_ef != NULL)
589 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000590 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200591 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100592 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000593 {
594 out_char('\n');
595 mch_exit(3);
596 }
597 TIME_MSG("reading errorfile");
598 }
599#endif
600
601 /*
602 * Start putting things on the screen.
603 * Scroll screen down before drawing over it
604 * Clear screen now, so file message will not be cleared.
605 */
606 starting = NO_BUFFERS;
607 no_wait_return = FALSE;
608 if (!exmode_active)
609 msg_scroll = FALSE;
610
611#ifdef FEAT_GUI
612 /*
613 * This seems to be required to make callbacks to be called now, instead
614 * of after things have been put on the screen, which then may be deleted
615 * when getting a resize callback.
616 * For the Mac this handles putting files dropped on the Vim icon to
617 * global_alist.
618 */
619 if (gui.in_use)
620 {
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100621 gui_wait_for_chars(50L, typebuf.tb_change_cnt);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000622 TIME_MSG("GUI delay");
623 }
624#endif
625
626#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
627 qnx_clip_init();
628#endif
629
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200630#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
631 clip_init(TRUE);
632#endif
633
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000634#ifdef FEAT_XCLIPBOARD
635 /* Start using the X clipboard, unless the GUI was started. */
636# ifdef FEAT_GUI
637 if (!gui.in_use)
638# endif
639 {
640 setup_term_clip();
641 TIME_MSG("setup clipboard");
642 }
643#endif
644
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000645#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000646 /* Prepare for being a Vim server. */
647 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000648#endif
649
650 /*
651 * If "-" argument given: Read file from stdin.
652 * Do this before starting Raw mode, because it may change things that the
653 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
654 * are the same terminal: "cat | vim -".
655 * Using autocommands here may cause trouble...
656 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000657 if (params.edit_type == EDIT_STDIN && !recoverymode)
658 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000659
660#if defined(UNIX) || defined(VMS)
661 /* When switching screens and something caused a message from a vimrc
662 * script, need to output an extra newline on exit. */
663 if ((did_emsg || msg_didout) && *T_TI != NUL)
664 newline_on_exit = TRUE;
665#endif
666
667 /*
668 * When done something that is not allowed or error message call
669 * wait_return. This must be done before starttermcap(), because it may
670 * switch to another screen. It must be done after settmode(TMODE_RAW),
671 * because we want to react on a single key stroke.
672 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000673 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000674 */
675 settmode(TMODE_RAW);
676 TIME_MSG("setting raw mode");
677
678 if (need_wait_return || msg_didany)
679 {
680 wait_return(TRUE);
681 TIME_MSG("waiting for return");
682 }
683
684 starttermcap(); /* start termcap if not done by wait_return() */
685 TIME_MSG("start termcap");
686
687#ifdef FEAT_MOUSE
688 setmouse(); /* may start using the mouse */
689#endif
690 if (scroll_region)
691 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000692 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000693
Bram Moolenaar980bab42018-08-07 22:42:53 +0200694#if defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar40385db2018-08-07 22:31:44 +0200695 term_push_title(SAVE_RESTORE_BOTH);
696#endif
697
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000698 /*
699 * Don't clear the screen when starting in Ex mode, unless using the GUI.
700 */
701 if (exmode_active
702#ifdef FEAT_GUI
703 && !gui.in_use
704#endif
705 )
706 must_redraw = CLEAR;
707 else
708 {
709 screenclear(); /* clear screen */
710 TIME_MSG("clearing screen");
711 }
712
713#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000714 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000715 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100716 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200717 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000718 TIME_MSG("getting crypt key");
719 }
720#endif
721
722 no_wait_return = TRUE;
723
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000724 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000725 * Create the requested number of windows and edit buffers in them.
726 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000727 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000728 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000729 TIME_MSG("opening buffers");
730
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000731#ifdef FEAT_EVAL
732 /* clear v:swapcommand */
733 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
734#endif
735
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000736 /* Ex starts at last line of the file */
737 if (exmode_active)
738 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
739
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000740 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
741 TIME_MSG("BufEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000742 setpcmark();
743
744#ifdef FEAT_QUICKFIX
745 /*
746 * When started with "-q errorfile" jump to first error now.
747 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000748 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000749 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000750 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000751 TIME_MSG("jump to first error");
752 }
753#endif
754
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000755 /*
756 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000757 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000758 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200759 edit_buffers(&params, start_dir);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200760 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000761
762#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000763 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000764 {
765 win_T *wp;
766
767 /* set options in each window for "vimdiff". */
Bram Moolenaar29323592016-07-24 22:04:11 +0200768 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000769 diff_win_options(wp, TRUE);
770 }
771#endif
772
773 /*
774 * Shorten any of the filenames, but only when absolute.
775 */
776 shorten_fnames(FALSE);
777
778 /*
779 * Need to jump to the tag before executing the '-c command'.
780 * Makes "vim -c '/return' -t main" work.
781 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000782 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000783 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000784 swap_exists_did_quit = FALSE;
Bram Moolenaar146522e2005-12-16 21:55:46 +0000785
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000786 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000787 do_cmdline_cmd(IObuff);
788 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000789
Bram Moolenaar146522e2005-12-16 21:55:46 +0000790 /* If the user doesn't want to edit the file then we quit here. */
791 if (swap_exists_did_quit)
792 getout(1);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000793 }
794
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000795 /* Execute any "+", "-c" and "-S" arguments. */
796 if (params.n_commands > 0)
797 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000798
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200799 /* Must come before the may_req_ calls. */
800 starting = 0;
801
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100802#if defined(FEAT_TERMRESPONSE)
Bram Moolenaar976787d2017-06-04 15:45:50 +0200803 /* Must be done before redrawing, puts a few characters on the screen. */
804 may_req_ambiguous_char_width();
805#endif
806
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000807 RedrawingDisabled = 0;
808 redraw_all_later(NOT_VALID);
809 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000810
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200811 /* 'autochdir' has been postponed */
Bram Moolenaar6f470022018-04-10 18:47:20 +0200812 DO_AUTOCHDIR;
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200813
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000814#ifdef FEAT_TERMRESPONSE
815 /* Requesting the termresponse is postponed until here, so that a "-c q"
816 * argument doesn't make it appear in the shell Vim was started from. */
817 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200818
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200819 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000820#endif
821
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000822 /* start in insert mode */
823 if (p_im)
824 need_start_insertmode = TRUE;
825
Bram Moolenaar14735512016-03-26 21:00:08 +0100826#ifdef FEAT_EVAL
827 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
828#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000829 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
830 TIME_MSG("VimEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000831
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200832#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
833 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
834 * done after the clipboard is available and all initial commands that may
835 * modify the 'clipboard' setting have run; i.e. just before entering the
836 * main loop. */
837 {
838 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100839
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200840 adjust_clip_reg(&default_regname);
841 set_reg_var(default_regname);
842 }
843#endif
844
Bram Moolenaar8a3bb562018-03-04 20:14:14 +0100845#if defined(FEAT_DIFF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000846 /* When a startup script or session file setup for diff'ing and
847 * scrollbind, sync the scrollbind now. */
848 if (curwin->w_p_diff && curwin->w_p_scb)
849 {
850 update_topline();
851 check_scrollbind((linenr_T)0, 0L);
852 TIME_MSG("diff scrollbinding");
853 }
854#endif
855
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200856#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
857# ifdef VIMDLL
858 if (!gui.in_use)
859# endif
860 mch_set_winsize_now(); /* Allow winsize changes from now on */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000861#endif
862
Bram Moolenaar4033c552017-09-16 20:54:51 +0200863#if defined(FEAT_GUI)
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000864 /* When tab pages were created, may need to update the tab pages line and
865 * scrollbars. This is skipped while creating them. */
866 if (first_tabpage->tp_next != NULL)
867 {
868 out_flush();
869 gui_init_which_components(NULL);
870 gui_update_scrollbars(TRUE);
871 }
872 need_mouse_correct = TRUE;
873#endif
874
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000875 /* If ":startinsert" command used, stuff a dummy command to be able to
876 * call normal_cmd(), which will then start Insert mode. */
877 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000878 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000879
880#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200881 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200882 {
883# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200884# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar4f974752019-02-17 17:44:42 +0100885 && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200886 if (gui.in_use)
887 {
888 mch_errmsg(_("netbeans is not supported with this GUI\n"));
889 mch_exit(2);
890 }
891# endif
892# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000893 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200894 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200895 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000896#endif
897
898 TIME_MSG("before starting main loop");
899
900 /*
901 * Call the main command loop. This never returns.
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200902 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000903 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000904
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200905#endif /* NO_VIM_MAIN */
906
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000907 return 0;
908}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000909
910/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200911 * Initialisation shared by main() and some tests.
912 */
913 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200914common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200915{
Bram Moolenaar438d1762018-09-30 17:11:48 +0200916 cmdline_init();
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200917
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200918 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200919#ifdef FEAT_EVAL
920 eval_init(); /* init global variables */
921#endif
922
923#ifdef __QNXNTO__
924 qnx_init(); /* PhAttach() for clipboard, (and gui) */
925#endif
926
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200927 /* Init the table of Normal mode commands. */
928 init_normal_cmds();
929
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200930 /*
931 * Allocate space for the generic buffers (needed for set_init_1() and
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100932 * emsg()).
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200933 */
934 if ((IObuff = alloc(IOSIZE)) == NULL
935 || (NameBuff = alloc(MAXPATHL)) == NULL)
936 mch_exit(0);
937 TIME_MSG("Allocated generic buffers");
938
939#ifdef NBDEBUG
940 /* Wait a moment for debugging NetBeans. Must be after allocating
941 * NameBuff. */
942 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
943 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
944 TIME_MSG("NetBeans debug wait");
945#endif
946
947#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
948 /*
949 * Setup to use the current locale (for ctype() and many other things).
950 * NOTE: Translated messages with encodings other than latin1 will not
951 * work until set_init_1() has been called!
952 */
953 init_locale();
954 TIME_MSG("locale set");
955#endif
956
957#ifdef FEAT_GUI
958 gui.dofork = TRUE; /* default is to use fork() */
959#endif
960
961 /*
962 * Do a first scan of the arguments in "argv[]":
963 * -display or --display
964 * --server...
965 * --socketid
966 * --windowid
967 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200968 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200969
Bram Moolenaard0573012017-10-28 21:11:06 +0200970#if defined(FEAT_GUI)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200971 /* Prepare for possibly starting GUI sometime */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200972 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200973 TIME_MSG("GUI prepared");
974#endif
975
976#ifdef FEAT_CLIPBOARD
977 clip_init(FALSE); /* Initialise clipboard stuff */
978 TIME_MSG("clipboard setup");
979#endif
980
981 /*
982 * Check if we have an interactive window.
983 * On the Amiga: If there is no window, we open one with a newcli command
984 * (needed for :! to * work). mch_check_win() will also handle the -d or
985 * -dev argument.
986 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100987 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200988 TIME_MSG("window checked");
989
990 /*
991 * Allocate the first window and buffer.
992 * Can't do anything without it, exit when it fails.
993 */
994 if (win_alloc_first() == FAIL)
995 mch_exit(0);
996
997 init_yank(); /* init yank buffers */
998
999 alist_init(&global_alist); /* Init the argument list to empty. */
1000 global_alist.id = 0;
1001
1002 /*
1003 * Set the default values for the options.
1004 * NOTE: Non-latin1 translated messages are working only after this,
1005 * because this is where "has_mbyte" will be set, which is used by
1006 * msg_outtrans_len_attr().
1007 * First find out the home directory, needed to expand "~" in options.
1008 */
1009 init_homedir(); /* find real value of $HOME */
Bram Moolenaar07268702018-03-01 21:57:32 +01001010 set_init_1(paramp->clean);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001011 TIME_MSG("inits 1");
1012
1013#ifdef FEAT_EVAL
1014 set_lang_var(); /* set v:lang and v:ctype */
1015#endif
Bram Moolenaar6436cd82018-12-27 00:28:33 +01001016
1017#ifdef FEAT_SIGNS
1018 init_signs();
1019#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001020}
1021
1022/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001023 * Return TRUE when the --not-a-term argument was found.
1024 */
1025 int
1026is_not_a_term()
1027{
1028 return params.not_a_term;
1029}
1030
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001031
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001032// When TRUE in a safe state when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001033static int was_safe = FALSE;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001034static oparg_T *current_oap = NULL;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001035
1036/*
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001037 * Return TRUE if an operator was started but not finished yet.
1038 * Includes typing a count or a register name.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001039 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001040 int
1041op_pending(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001042{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001043 return !(current_oap != NULL
1044 && !finish_op
1045 && current_oap->prev_opcount == 0
1046 && current_oap->prev_count0 == 0
1047 && current_oap->op_type == OP_NOP
1048 && current_oap->regname == NUL);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001049}
1050
1051/*
1052 * Trigger SafeState if currently in s safe state, that is "safe" is TRUE and
1053 * there is no typeahead.
1054 */
1055 void
1056may_trigger_safestate(int safe)
1057{
1058 int is_safe = safe
1059 && stuff_empty()
1060 && typebuf.tb_len == 0
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001061 && scriptin[curscript] == NULL
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001062 && !global_busy;
1063
1064 if (is_safe)
1065 apply_autocmds(EVENT_SAFESTATE, NULL, NULL, FALSE, curbuf);
1066 was_safe = is_safe;
1067}
1068
1069/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001070 * Something changed which causes the state possibly to be unsafe, e.g. a
1071 * character was typed. It will remain unsafe until the next call to
1072 * may_trigger_safestate().
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001073 */
1074 void
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001075state_no_longer_safe(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001076{
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001077 was_safe = FALSE;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001078}
1079
1080/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001081 * Invoked when leaving code that invokes callbacks. Then trigger
1082 * SafeStateAgain, if it was safe when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001083 */
1084 void
1085leave_unsafe_state(void)
1086{
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001087 if (was_safe)
1088 apply_autocmds(EVENT_SAFESTATEAGAIN, NULL, NULL, FALSE, curbuf);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001089}
1090
1091
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001092/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001093 * Main loop: Execute Normal mode commands until exiting Vim.
1094 * Also used to handle commands in the command-line window, until the window
1095 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096 * Also used to handle ":visual" command after ":global": execute Normal mode
1097 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001098 */
1099 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001100main_loop(
1101 int cmdwin, /* TRUE when working in the command-line window */
1102 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001103{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001104 oparg_T oa; // operator arguments
1105 oparg_T *prev_oap; // operator arguments
1106 volatile int previous_got_int = FALSE; // "got_int" was TRUE
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001107#ifdef FEAT_CONCEAL
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001108 // these are static to avoid a compiler warning
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001109 static linenr_T conceal_old_cursor_line = 0;
1110 static linenr_T conceal_new_cursor_line = 0;
1111 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001112#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001113
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001114 prev_oap = current_oap;
1115 current_oap = &oa;
1116
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001117#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1118 /* Setup to catch a terminating error from the X server. Just ignore
1119 * it, restore the state and continue. This might not always work
1120 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001121 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001123 {
1124 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001125 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001126 got_int = TRUE;
1127 need_wait_return = FALSE;
1128 global_busy = FALSE;
1129 exmode_active = 0;
1130 skip_redraw = FALSE;
1131 RedrawingDisabled = 0;
1132 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001133 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001134# ifdef FEAT_EVAL
1135 emsg_skip = 0;
1136# endif
1137 emsg_off = 0;
1138# ifdef FEAT_MOUSE
1139 setmouse();
1140# endif
1141 settmode(TMODE_RAW);
1142 starttermcap();
1143 scroll_start();
1144 redraw_later_clear();
1145 }
1146#endif
1147
1148 clear_oparg(&oa);
1149 while (!cmdwin
1150#ifdef FEAT_CMDWIN
1151 || cmdwin_result == 0
1152#endif
1153 )
1154 {
1155 if (stuff_empty())
1156 {
1157 did_check_timestamps = FALSE;
1158 if (need_check_timestamps)
1159 check_timestamps(FALSE);
1160 if (need_wait_return) /* if wait_return still needed ... */
1161 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001162 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001163 {
1164 need_start_insertmode = FALSE;
1165 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1166 /* skip the fileinfo message now, because it would be shown
1167 * after insert mode finishes! */
1168 need_fileinfo = FALSE;
1169 }
1170 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001171
1172 /* Reset "got_int" now that we got back to the main loop. Except when
1173 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1174 * the ":g" command.
1175 * For ":g/pat/vi" we reset "got_int" when used once. When used
1176 * a second time we go back to Ex mode and abort the ":g" command. */
1177 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001178 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001179 if (noexmode && global_busy && !exmode_active && previous_got_int)
1180 {
1181 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1182 * used and keep "got_int" set, so that it aborts ":g". */
1183 exmode_active = EXMODE_NORMAL;
1184 State = NORMAL;
1185 }
1186 else if (!global_busy || !exmode_active)
1187 {
1188 if (!quit_more)
1189 (void)vgetc(); /* flush all buffers */
1190 got_int = FALSE;
1191 }
1192 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001193 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001194 else
1195 previous_got_int = FALSE;
1196
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001197 if (!exmode_active)
1198 msg_scroll = FALSE;
1199 quit_more = FALSE;
1200
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001201 // it's not safe unless may_trigger_safestate_main() is called
1202 was_safe = FALSE;
1203
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001204 /*
1205 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1206 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1207 * update cursor and redraw.
1208 */
1209 if (skip_redraw || exmode_active)
1210 skip_redraw = FALSE;
1211 else if (do_redraw || stuff_empty())
1212 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001213#ifdef FEAT_GUI
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001214 // If ui_breakcheck() was used a resize may have been postponed.
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001215 gui_may_resize_shell();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001216#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001217#ifdef HAVE_DROP_FILE
1218 // If files were dropped while text was locked or the curbuf was
1219 // locked, this would be a good time to handle the drop.
1220 handle_any_postponed_drop();
1221#endif
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001222#ifdef FEAT_CONCEAL
1223 if (curwin->w_p_cole == 0)
1224 conceal_update_lines = FALSE;
1225#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001226
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001227 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001228 if (!finish_op && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001229 has_cursormoved()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001230#ifdef FEAT_TEXT_PROP
1231 || popup_visible
1232#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001233#ifdef FEAT_CONCEAL
1234 || curwin->w_p_cole > 0
1235#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001236 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001237 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001238 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001239 if (has_cursormoved())
1240 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1241 FALSE, curbuf);
Bram Moolenaar3397f742019-06-02 18:40:06 +02001242#ifdef FEAT_TEXT_PROP
1243 if (popup_visible)
1244 popup_check_cursor_pos();
1245#endif
1246#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001247 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001248 {
1249 conceal_old_cursor_line = last_cursormoved.lnum;
1250 conceal_new_cursor_line = curwin->w_cursor.lnum;
1251 conceal_update_lines = TRUE;
1252 }
Bram Moolenaar3397f742019-06-02 18:40:06 +02001253#endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001254 last_cursormoved = curwin->w_cursor;
1255 }
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001256
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001257#if defined(FEAT_CONCEAL)
1258 if (conceal_update_lines
1259 && (conceal_old_cursor_line != conceal_new_cursor_line
1260 || conceal_cursor_line(curwin)
1261 || need_cursor_line_redraw))
1262 {
1263 if (conceal_old_cursor_line != conceal_new_cursor_line
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001264 && conceal_old_cursor_line != 0
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001265 && conceal_old_cursor_line
1266 <= curbuf->b_ml.ml_line_count)
1267 redrawWinline(curwin, conceal_old_cursor_line);
1268 redrawWinline(curwin, conceal_new_cursor_line);
1269 curwin->w_valid &= ~VALID_CROW;
1270 need_cursor_line_redraw = FALSE;
1271 }
1272#endif
1273
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001274 /* Trigger TextChanged if b:changedtick differs. */
Bram Moolenaar186628f2013-03-19 13:33:23 +01001275 if (!finish_op && has_textchanged()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001276 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001277 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001278 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL, FALSE, curbuf);
1279 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001280 }
Bram Moolenaar186628f2013-03-19 13:33:23 +01001281
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001282 // If nothing is pending and we are going to wait for the user to
1283 // type a character, trigger SafeState.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001284 may_trigger_safestate(!op_pending() && restart_edit == 0);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001285
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01001286#if defined(FEAT_DIFF)
Bram Moolenaare3521d92018-09-16 14:10:31 +02001287 // Updating diffs from changed() does not always work properly,
1288 // esp. updating folds. Do an update just before redrawing if
1289 // needed.
1290 if (curtab->tp_diff_update || curtab->tp_diff_invalid)
1291 {
1292 ex_diffupdate(NULL);
1293 curtab->tp_diff_update = FALSE;
1294 }
1295
Bram Moolenaar33aec762006-01-22 23:30:12 +00001296 /* Scroll-binding for diff mode may have been postponed until
1297 * here. Avoids doing it for every change. */
1298 if (diff_need_scrollbind)
1299 {
1300 check_scrollbind((linenr_T)0, 0L);
1301 diff_need_scrollbind = FALSE;
1302 }
1303#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001304#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001305 /* Include a closed fold completely in the Visual area. */
1306 foldAdjustVisual();
1307#endif
1308#ifdef FEAT_FOLDING
1309 /*
1310 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1311 * contain the cursor.
1312 * When 'foldopen' is "all", open the fold(s) under the cursor.
1313 * This may mark the window for redrawing.
1314 */
1315 if (hasAnyFolding(curwin) && !char_avail())
1316 {
1317 foldCheckClose();
1318 if (fdo_flags & FDO_ALL)
1319 foldOpenCursor();
1320 }
1321#endif
1322
1323 /*
1324 * Before redrawing, make sure w_topline is correct, and w_leftcol
1325 * if lines don't wrap, and w_skipcol if lines wrap.
1326 */
1327 update_topline();
1328 validate_cursor();
1329
Bram Moolenaar017ba072019-09-14 21:01:23 +02001330#ifdef FEAT_SYN_HL
1331 if (curwin->w_p_cul && curwin->w_p_wrap
1332 && (curwin->w_p_culopt_flags & CULOPT_SCRLINE))
1333 must_redraw = NOT_VALID;
1334#endif
1335
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001336 if (VIsual_active)
Bram Moolenaar017ba072019-09-14 21:01:23 +02001337 update_curbuf(INVERTED); // update inverted part
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001338 else if (must_redraw)
Bram Moolenaara338adc2018-01-31 20:51:47 +01001339 {
Bram Moolenaar017ba072019-09-14 21:01:23 +02001340 mch_disable_flush(); // Stop issuing gui_mch_flush().
1341#ifdef FEAT_SYN_HL
1342 // Might need some more update for the cursorscreen line.
1343 // TODO: can we optimize this?
1344 if (curwin->w_p_cul
1345 && curwin->w_p_wrap
1346 && (curwin->w_p_culopt_flags & CULOPT_SCRLINE)
1347 && !char_avail())
1348 update_screen(VALID);
1349 else
1350#endif
1351 update_screen(0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01001352 mch_enable_flush();
1353 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001354 else if (redraw_cmdline || clear_cmdline)
1355 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001356 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001357#ifdef FEAT_TITLE
1358 if (need_maketitle)
1359 maketitle();
1360#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001361#ifdef FEAT_VIMINFO
1362 curbuf->b_last_used = vim_time();
1363#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001364 /* display message after redraw */
1365 if (keep_msg != NULL)
1366 {
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001367 char_u *p = vim_strsave(keep_msg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001368
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001369 if (p != NULL)
1370 {
1371 // msg_start() will set keep_msg to NULL, make a copy
1372 // first. Don't reset keep_msg, msg_attr_keep() uses it to
1373 // check for duplicates. Never put this message in
1374 // history.
1375 msg_hist_off = TRUE;
1376 msg_attr((char *)p, keep_msg_attr);
1377 msg_hist_off = FALSE;
1378 vim_free(p);
1379 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001380 }
1381 if (need_fileinfo) /* show file info after redraw */
1382 {
1383 fileinfo(FALSE, TRUE, FALSE);
1384 need_fileinfo = FALSE;
1385 }
1386
1387 emsg_on_display = FALSE; /* can delete error message now */
1388 did_emsg = FALSE;
1389 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001390 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001391 showruler(FALSE);
1392
1393 setcursor();
1394 cursor_on();
1395
1396 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001397
1398#ifdef STARTUPTIME
1399 /* Now that we have drawn the first screen all the startup stuff
1400 * has been done, close any file for startup messages. */
1401 if (time_fd != NULL)
1402 {
1403 TIME_MSG("first screen update");
1404 TIME_MSG("--- VIM STARTED ---");
1405 fclose(time_fd);
1406 time_fd = NULL;
1407 }
1408#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001409 }
1410#ifdef FEAT_GUI
1411 if (need_mouse_correct)
1412 gui_mouse_correct();
1413#endif
1414
1415 /*
1416 * Update w_curswant if w_set_curswant has been set.
1417 * Postponed until here to avoid computing w_virtcol too often.
1418 */
1419 update_curswant();
1420
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001421#ifdef FEAT_EVAL
1422 /*
1423 * May perform garbage collection when waiting for a character, but
1424 * only at the very toplevel. Otherwise we may be using a List or
1425 * Dict internally somewhere.
1426 * "may_garbage_collect" is reset in vgetc() which is invoked through
1427 * do_exmode() and normal_cmd().
1428 */
1429 may_garbage_collect = (!cmdwin && !noexmode);
1430#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001431 /*
1432 * If we're invoked as ex, do a round of ex commands.
1433 * Otherwise, get and execute a normal mode command.
1434 */
1435 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001436 {
1437 if (noexmode) /* End of ":global/path/visual" commands */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001438 goto theend;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001439 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001440 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001441 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001442 {
1443#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001444 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001445 && oa.op_type == OP_NOP && oa.regname == NUL
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001446 && !VIsual_active
1447 && !skip_term_loop)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001448 {
1449 /* If terminal_loop() returns OK we got a key that is handled
Bram Moolenaar6d819742017-08-06 14:57:49 +02001450 * in Normal model. With FAIL we first need to position the
1451 * cursor and the screen needs to be redrawn. */
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001452 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001453 normal_cmd(&oa, TRUE);
1454 }
1455 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001456#endif
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001457 {
1458#ifdef FEAT_TERMINAL
1459 skip_term_loop = FALSE;
1460#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001461 normal_cmd(&oa, TRUE);
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001462 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02001463 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001464 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001465
1466theend:
1467 current_oap = prev_oap;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001468}
1469
1470
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001471#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001472/*
1473 * Exit, but leave behind swap files for modified buffers.
1474 */
1475 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001476getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001477{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001478# if defined(SIGHUP) && defined(SIG_IGN)
1479 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1480 * makes Vim exit and then handling SIGHUP causes various reentrance
1481 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001482 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001483# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001484
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001485 ml_close_notmod(); /* close all not-modified buffers */
1486 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1487 ml_close_all(FALSE); /* close all memfiles, without deleting */
1488 getout(exitval); /* exit Vim properly */
1489}
1490#endif
1491
1492
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001493/*
1494 * Exit properly.
1495 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001496 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001497getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001498{
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001499 exiting = TRUE;
Bram Moolenaar0bd052b2018-03-22 20:33:56 +01001500#if defined(FEAT_JOB_CHANNEL)
1501 ch_log(NULL, "Exiting...");
1502#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001503
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001504 /* When running in Ex mode an error causes us to exit with a non-zero exit
1505 * code. POSIX requires this, although it's not 100% clear from the
1506 * standard. */
1507 if (exmode_active)
1508 exitval += ex_exitval;
1509
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001510 /* Position the cursor on the last screen line, below all the text */
1511#ifdef FEAT_GUI
1512 if (!gui.in_use)
1513#endif
1514 windgoto((int)Rows - 1, 0);
1515
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001516#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1517 /* Optionally print hashtable efficiency. */
1518 hash_debug_results();
1519#endif
1520
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001521#ifdef FEAT_GUI
1522 msg_didany = FALSE;
1523#endif
1524
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001525 if (v_dying <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001526 {
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001527 tabpage_T *tp;
1528 tabpage_T *next_tp;
1529 buf_T *buf;
1530 win_T *wp;
1531
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001532 /* Trigger BufWinLeave for all windows, but only once per buffer. */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001533 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001534 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001535 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001536 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001537 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001538 if (wp->w_buffer == NULL)
1539 /* Autocmd must have close the buffer already, skip. */
1540 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001541 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001542 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001543 {
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001544 bufref_T bufref;
1545
1546 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001547 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1548 buf->b_fname, FALSE, buf);
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001549 if (bufref_valid(&bufref))
1550 CHANGEDTICK(buf) = -1; /* note we did it already */
1551
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001552 /* start all over, autocommands may mess up the lists */
1553 next_tp = first_tabpage;
1554 break;
1555 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001556 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001557 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001558
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001559 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001560 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001561 if (buf->b_ml.ml_mfp != NULL)
1562 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001563 bufref_T bufref;
1564
1565 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001566 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001567 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001568 if (!bufref_valid(&bufref))
1569 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001570 break;
1571 }
1572 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1573 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001574
1575#ifdef FEAT_VIMINFO
1576 if (*p_viminfo != NUL)
1577 /* Write out the registers, history, marks etc, to the viminfo file */
1578 write_viminfo(NULL, FALSE);
1579#endif
1580
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001581 if (v_dying <= 1)
Bram Moolenaarc7226682019-08-17 16:33:23 +02001582 {
1583 int unblock = 0;
1584
1585 // deathtrap() blocks autocommands, but we do want to trigger VimLeave.
1586 if (is_autocmd_blocked())
1587 {
1588 unblock_autocmds();
1589 ++unblock;
1590 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001591 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc7226682019-08-17 16:33:23 +02001592 if (unblock)
1593 block_autocmds();
1594 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001595
Bram Moolenaar05159a02005-02-26 23:04:13 +00001596#ifdef FEAT_PROFILE
1597 profile_dump();
1598#endif
1599
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001600 if (did_emsg
1601#ifdef FEAT_GUI
1602 || (gui.in_use && msg_didany && p_verbose > 0)
1603#endif
1604 )
1605 {
1606 /* give the user a chance to read the (error) message */
1607 no_wait_return = FALSE;
1608 wait_return(FALSE);
1609 }
1610
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001611 /* Position the cursor again, the autocommands may have moved it */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001612#ifdef FEAT_GUI
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001613 if (!gui.in_use)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001614#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001615 windgoto((int)Rows - 1, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001616
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001617#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001618 job_stop_on_exit();
1619#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001620#ifdef FEAT_LUA
1621 lua_end();
1622#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001623#ifdef FEAT_MZSCHEME
1624 mzscheme_end();
1625#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001626#ifdef FEAT_TCL
1627 tcl_end();
1628#endif
1629#ifdef FEAT_RUBY
1630 ruby_end();
1631#endif
1632#ifdef FEAT_PYTHON
1633 python_end();
1634#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001635#ifdef FEAT_PYTHON3
1636 python3_end();
1637#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001638#ifdef FEAT_PERL
1639 perl_end();
1640#endif
1641#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1642 iconv_end();
1643#endif
1644#ifdef FEAT_NETBEANS_INTG
1645 netbeans_end();
1646#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001647#ifdef FEAT_CSCOPE
1648 cs_end();
1649#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001650#ifdef FEAT_EVAL
1651 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001652 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001653#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001654#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +02001655 free_cmd_argsW();
1656#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001657
1658 mch_exit(exitval);
1659}
1660
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001661#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1662/*
1663 * Setup to use the current locale (for ctype() and many other things).
1664 */
1665 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001666init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001667{
1668 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001669
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001670# ifdef FEAT_GUI_GTK
1671 /* Tell Gtk not to change our locale settings. */
1672 gtk_disable_setlocale();
1673# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001674# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1675 /* Make sure strtod() uses a decimal point, not a comma. */
1676 setlocale(LC_NUMERIC, "C");
1677# endif
1678
Bram Moolenaar4f974752019-02-17 17:44:42 +01001679# ifdef MSWIN
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001680 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1681 * text while it's expecting text in the current locale. This call avoids
1682 * that. */
1683 setlocale(LC_CTYPE, "C");
1684# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001685
1686# ifdef FEAT_GETTEXT
1687 {
1688 int mustfree = FALSE;
1689 char_u *p;
1690
1691# ifdef DYNAMIC_GETTEXT
1692 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001693 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001694# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001695 /* expand_env() doesn't work yet, because g_chartab[] is not
1696 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001697 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1698 if (p != NULL && *p != NUL)
1699 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001700 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001701 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1702 }
1703 if (mustfree)
1704 vim_free(p);
1705 textdomain(VIMPACKAGE);
1706 }
1707# endif
1708}
1709#endif
1710
1711/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001712 * Get the name of the display, before gui_prepare() removes it from
1713 * argv[]. Used for the xterm-clipboard display.
1714 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001715 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001716 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001717 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001718early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001719{
Bram Moolenaar03005972008-11-20 13:12:36 +00001720#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1721 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001722 int argc = parmp->argc;
1723 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001724 int i;
1725
1726 for (i = 1; i < argc; i++)
1727 {
1728 if (STRCMP(argv[i], "--") == 0)
1729 break;
1730# ifdef FEAT_XCLIPBOARD
1731 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001732# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001733 || STRICMP(argv[i], "--display") == 0
1734# endif
1735 )
1736 {
1737 if (i == argc - 1)
1738 mainerr_arg_missing((char_u *)argv[i]);
1739 xterm_display = argv[++i];
1740 }
1741# endif
1742# ifdef FEAT_CLIENTSERVER
1743 else if (STRICMP(argv[i], "--servername") == 0)
1744 {
1745 if (i == argc - 1)
1746 mainerr_arg_missing((char_u *)argv[i]);
1747 parmp->serverName_arg = (char_u *)argv[++i];
1748 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001749 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001750 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001751 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001752 {
1753 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001754# ifdef FEAT_GUI
1755 if (strstr(argv[i], "-wait") != 0)
1756 /* don't fork() when starting the GUI to edit files ourself */
1757 gui.dofork = FALSE;
1758# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001759 }
1760# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001761
Bram Moolenaar4f974752019-02-17 17:44:42 +01001762# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
1763# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001764 else if (STRICMP(argv[i], "--windowid") == 0)
1765# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001766 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001767# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001768 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001769 long_u id;
1770 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001771
1772 if (i == argc - 1)
1773 mainerr_arg_missing((char_u *)argv[i]);
1774 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001775 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001776 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001777 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001778 if (count != 1)
1779 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1780 else
Bram Moolenaar4f974752019-02-17 17:44:42 +01001781# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001782 win_socket_id = id;
1783# else
1784 gtk_socket_id = id;
1785# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001786 i++;
1787 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001788# endif
1789# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001790 else if (STRICMP(argv[i], "--echo-wid") == 0)
1791 echo_wid_arg = TRUE;
1792# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001793# ifndef FEAT_NETBEANS_INTG
1794 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001795 {
1796 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1797 mch_exit(2);
1798 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001799# endif
1800
Bram Moolenaar58d98232005-07-23 22:25:46 +00001801 }
1802#endif
1803}
1804
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001805#ifndef NO_VIM_MAIN
1806/*
1807 * Get a (optional) count for a Vim argument.
1808 */
1809 static int
1810get_number_arg(
1811 char_u *p, /* pointer to argument */
1812 int *idx, /* index in argument, is incremented */
1813 int def) /* default value */
1814{
1815 if (vim_isdigit(p[*idx]))
1816 {
1817 def = atoi((char *)&(p[*idx]));
1818 while (vim_isdigit(p[*idx]))
1819 *idx = *idx + 1;
1820 }
1821 return def;
1822}
1823
1824/*
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001825 * Check for: [r][e][g][vi|vim|view][diff][ex[im]] (sort of)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001826 * If the executable name starts with "r" we disable shell commands.
1827 * If the next character is "e" we run in Easy mode.
1828 * If the next character is "g" we run the GUI version.
1829 * If the next characters are "view" we start in readonly mode.
1830 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1831 * If the next characters are "ex" we start in Ex mode. If it's followed
1832 * by "im" use improved Ex mode.
1833 */
1834 static void
1835parse_command_name(mparm_T *parmp)
1836{
1837 char_u *initstr;
1838
1839 initstr = gettail((char_u *)parmp->argv[0]);
1840
Bram Moolenaard0573012017-10-28 21:11:06 +02001841#ifdef FEAT_GUI_MAC
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001842 /* An issue has been seen when launching Vim in such a way that
1843 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1844 * executable or a symbolic link of it. Until this issue is resolved
1845 * we prohibit the GUI from being used.
1846 */
1847 if (STRCMP(initstr, parmp->argv[0]) == 0)
1848 disallow_gui = TRUE;
1849
1850 /* TODO: On MacOS X default to gui if argv[0] ends in:
1851 * /Vim.app/Contents/MacOS/Vim */
1852#endif
1853
1854#ifdef FEAT_EVAL
1855 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001856 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001857#endif
1858
1859 if (TOLOWER_ASC(initstr[0]) == 'r')
1860 {
1861 restricted = TRUE;
1862 ++initstr;
1863 }
1864
1865 /* Use evim mode for "evim" and "egvim", not for "editor". */
1866 if (TOLOWER_ASC(initstr[0]) == 'e'
1867 && (TOLOWER_ASC(initstr[1]) == 'v'
1868 || TOLOWER_ASC(initstr[1]) == 'g'))
1869 {
1870#ifdef FEAT_GUI
1871 gui.starting = TRUE;
1872#endif
1873 parmp->evim_mode = TRUE;
1874 ++initstr;
1875 }
1876
1877 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1878 if (TOLOWER_ASC(initstr[0]) == 'g')
1879 {
1880 main_start_gui();
1881#ifdef FEAT_GUI
1882 ++initstr;
1883#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001884#ifdef GUI_MAY_SPAWN
1885 gui.dospawn = FALSE; // No need to spawn a new process.
1886#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001887 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001888#ifdef GUI_MAY_SPAWN
1889 else
1890 gui.dospawn = TRUE; // Not "gvim". Need to spawn gvim.exe.
1891#endif
1892
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001893
1894 if (STRNICMP(initstr, "view", 4) == 0)
1895 {
1896 readonlymode = TRUE;
1897 curbuf->b_p_ro = TRUE;
1898 p_uc = 10000; /* don't update very often */
1899 initstr += 4;
1900 }
1901 else if (STRNICMP(initstr, "vim", 3) == 0)
1902 initstr += 3;
1903
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001904 // Catch "[r][g]vimdiff" and "[r][g]viewdiff".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001905 if (STRICMP(initstr, "diff") == 0)
1906 {
1907#ifdef FEAT_DIFF
1908 parmp->diff_mode = TRUE;
1909#else
1910 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1911 mch_errmsg("\n");
1912 mch_exit(2);
1913#endif
1914 }
1915
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001916 // Checking for "ex" here may catch some weir names, such as "vimex" or
1917 // "viewex", we assume the user knows that.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001918 if (STRNICMP(initstr, "ex", 2) == 0)
1919 {
1920 if (STRNICMP(initstr + 2, "im", 2) == 0)
1921 exmode_active = EXMODE_VIM;
1922 else
1923 exmode_active = EXMODE_NORMAL;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001924 change_compatible(TRUE); // set 'compatible'
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001925 }
1926}
1927
Bram Moolenaar58d98232005-07-23 22:25:46 +00001928/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001929 * Scan the command line arguments.
1930 */
1931 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001932command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001933{
1934 int argc = parmp->argc;
1935 char **argv = parmp->argv;
1936 int argv_idx; /* index in argv[n][] */
1937 int had_minmin = FALSE; /* found "--" argument */
1938 int want_argument; /* option argument with argument */
1939 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001940 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001941 long n;
1942
1943 --argc;
1944 ++argv;
1945 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1946 while (argc > 0)
1947 {
1948 /*
1949 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1950 */
1951 if (argv[0][0] == '+' && !had_minmin)
1952 {
1953 if (parmp->n_commands >= MAX_ARG_CMDS)
1954 mainerr(ME_EXTRA_CMD, NULL);
1955 argv_idx = -1; /* skip to next argument */
1956 if (argv[0][1] == NUL)
1957 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1958 else
1959 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1960 }
1961
1962 /*
1963 * Optional argument.
1964 */
1965 else if (argv[0][0] == '-' && !had_minmin)
1966 {
1967 want_argument = FALSE;
1968 c = argv[0][argv_idx++];
1969#ifdef VMS
1970 /*
1971 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1972 * and "-/X" as "-X".
1973 */
1974 if (c == '/')
1975 {
1976 c = argv[0][argv_idx++];
1977 c = TOUPPER_ASC(c);
1978 }
1979 else
1980 c = TOLOWER_ASC(c);
1981#endif
1982 switch (c)
1983 {
1984 case NUL: /* "vim -" read from stdin */
1985 /* "ex -" silent mode */
1986 if (exmode_active)
1987 silent_mode = TRUE;
1988 else
1989 {
1990 if (parmp->edit_type != EDIT_NONE)
1991 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1992 parmp->edit_type = EDIT_STDIN;
1993 read_cmd_fd = 2; /* read from stderr instead of stdin */
1994 }
1995 argv_idx = -1; /* skip to next argument */
1996 break;
1997
1998 case '-': /* "--" don't take any more option arguments */
1999 /* "--help" give help message */
2000 /* "--version" give version message */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002001 /* "--clean" clean context */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002002 /* "--literal" take files literally */
2003 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002004 /* "--not-a-term" don't warn for not a term */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002005 /* "--ttyfail" exit if not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002006 /* "--noplugin[s]" skip plugins */
2007 /* "--cmd <cmd>" execute cmd before vimrc */
2008 if (STRICMP(argv[0] + argv_idx, "help") == 0)
2009 usage();
2010 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
2011 {
2012 Columns = 80; /* need to init Columns */
2013 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
2014 list_version();
2015 msg_putchar('\n');
2016 msg_didout = FALSE;
2017 mch_exit(0);
2018 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002019 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
2020 {
2021 parmp->use_vimrc = (char_u *)"DEFAULTS";
Bram Moolenaar62dd4522018-03-14 21:20:02 +01002022#ifdef FEAT_GUI
2023 use_gvimrc = (char_u *)"NONE";
2024#endif
Bram Moolenaar07268702018-03-01 21:57:32 +01002025 parmp->clean = TRUE;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002026 set_option_value((char_u *)"vif", 0L, (char_u *)"NONE", 0);
2027 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002028 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
2029 {
Bram Moolenaar53076832015-12-31 19:53:21 +01002030#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002031 parmp->literal = TRUE;
2032#endif
2033 }
2034 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
2035 {
2036#ifdef FEAT_GUI
2037 gui.dofork = FALSE; /* don't fork() when starting GUI */
2038#endif
2039 }
2040 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
2041 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002042 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
2043 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002044 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
2045 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002046 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
2047 {
2048 want_argument = TRUE;
2049 argv_idx += 3;
2050 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002051 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
2052 {
2053 want_argument = TRUE;
2054 argv_idx += 11;
2055 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002056#ifdef FEAT_CLIENTSERVER
2057 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
2058 ; /* already processed -- no arg */
2059 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
2060 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
2061 {
2062 /* already processed -- snatch the following arg */
2063 if (argc > 1)
2064 {
2065 --argc;
2066 ++argv;
2067 }
2068 }
2069#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002070#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002071# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002072 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002073# else
2074 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
2075# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002076 {
2077 /* already processed -- snatch the following arg */
2078 if (argc > 1)
2079 {
2080 --argc;
2081 ++argv;
2082 }
2083 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00002084#endif
2085#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002086 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
2087 {
2088 /* already processed, skip */
2089 }
2090#endif
2091 else
2092 {
2093 if (argv[0][argv_idx])
2094 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2095 had_minmin = TRUE;
2096 }
2097 if (!want_argument)
2098 argv_idx = -1; /* skip to next argument */
2099 break;
2100
2101 case 'A': /* "-A" start in Arabic mode */
2102#ifdef FEAT_ARABIC
2103 set_option_value((char_u *)"arabic", 1L, NULL, 0);
2104#else
2105 mch_errmsg(_(e_noarabic));
2106 mch_exit(2);
2107#endif
2108 break;
2109
2110 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002111 /* Needs to be effective before expanding file names, because
2112 * for Win32 this makes us edit a shortcut file itself,
2113 * instead of the file it links to. */
2114 set_options_bin(curbuf->b_p_bin, 1, 0);
2115 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002116 break;
2117
2118 case 'C': /* "-C" Compatible */
2119 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02002120 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002121 break;
2122
2123 case 'e': /* "-e" Ex mode */
2124 exmode_active = EXMODE_NORMAL;
2125 break;
2126
2127 case 'E': /* "-E" Improved Ex mode */
2128 exmode_active = EXMODE_VIM;
2129 break;
2130
2131 case 'f': /* "-f" GUI: run in foreground. Amiga: open
2132 window directly, not with newcli */
2133#ifdef FEAT_GUI
2134 gui.dofork = FALSE; /* don't fork() when starting GUI */
2135#endif
2136 break;
2137
2138 case 'g': /* "-g" start GUI */
2139 main_start_gui();
2140 break;
2141
Bram Moolenaar14184a32019-02-16 15:10:30 +01002142 case 'F': /* "-F" was for Farsi mode */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002143 mch_errmsg(_(e_nofarsi));
2144 mch_exit(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002145 break;
2146
Bram Moolenaarc3e81692018-05-05 15:09:51 +02002147 case '?': /* "-?" give help message (for MS-Windows) */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002148 case 'h': /* "-h" give help message */
2149#ifdef FEAT_GUI_GNOME
2150 /* Tell usage() to exit for "gvim". */
2151 gui.starting = FALSE;
2152#endif
2153 usage();
2154 break;
2155
2156 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2157#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002158 p_hkmap = TRUE;
2159 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002160#else
2161 mch_errmsg(_(e_nohebrew));
2162 mch_exit(2);
2163#endif
2164 break;
2165
2166 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2167#ifdef FEAT_LISP
2168 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2169 p_sm = TRUE;
2170#endif
2171 break;
2172
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002173 case 'M': /* "-M" no changes or writing of files */
2174 reset_modifiable();
2175 /* FALLTHROUGH */
2176
2177 case 'm': /* "-m" no writing of files */
2178 p_write = FALSE;
2179 break;
2180
2181 case 'y': /* "-y" easy mode */
2182#ifdef FEAT_GUI
2183 gui.starting = TRUE; /* start GUI a bit later */
2184#endif
2185 parmp->evim_mode = TRUE;
2186 break;
2187
2188 case 'N': /* "-N" Nocompatible */
2189 change_compatible(FALSE);
2190 break;
2191
2192 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002193#ifdef FEAT_NETBEANS_INTG
2194 /* checking for "-nb", netbeans parameters */
2195 if (argv[0][argv_idx] == 'b')
2196 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002197 netbeansArg = argv[0];
2198 argv_idx = -1; /* skip to next argument */
2199 }
2200 else
2201#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002202 parmp->no_swap_file = TRUE;
2203 break;
2204
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002205 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002206#ifdef TARGET_API_MAC_OSX
2207 /* For some reason on MacOS X, an argument like:
2208 -psn_0_10223617 is passed in when invoke from Finder
2209 or with the 'open' command */
2210 if (argv[0][argv_idx] == 's')
2211 {
2212 argv_idx = -1; /* bypass full -psn */
2213 main_start_gui();
2214 break;
2215 }
2216#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002217 /* default is 0: open window for each file */
2218 parmp->window_count = get_number_arg((char_u *)argv[0],
2219 &argv_idx, 0);
2220 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002221 break;
2222
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002223 case 'o': /* "-o[N]" open N horizontal split windows */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002224 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002225 parmp->window_count = get_number_arg((char_u *)argv[0],
2226 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002227 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002228 break;
2229
2230 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002231 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002232 parmp->window_count = get_number_arg((char_u *)argv[0],
2233 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002234 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002235 break;
2236
2237#ifdef FEAT_QUICKFIX
2238 case 'q': /* "-q" QuickFix mode */
2239 if (parmp->edit_type != EDIT_NONE)
2240 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2241 parmp->edit_type = EDIT_QF;
2242 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2243 {
2244 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2245 argv_idx = -1;
2246 }
2247 else if (argc > 1) /* "-q {errorfile}" */
2248 want_argument = TRUE;
2249 break;
2250#endif
2251
2252 case 'R': /* "-R" readonly mode */
2253 readonlymode = TRUE;
2254 curbuf->b_p_ro = TRUE;
2255 p_uc = 10000; /* don't update very often */
2256 break;
2257
2258 case 'r': /* "-r" recovery mode */
2259 case 'L': /* "-L" recovery mode */
2260 recoverymode = 1;
2261 break;
2262
2263 case 's':
2264 if (exmode_active) /* "-s" silent (batch) mode */
2265 silent_mode = TRUE;
2266 else /* "-s {scriptin}" read from script file */
2267 want_argument = TRUE;
2268 break;
2269
2270 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2271 if (parmp->edit_type != EDIT_NONE)
2272 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2273 parmp->edit_type = EDIT_TAG;
2274 if (argv[0][argv_idx]) /* "-t{tag}" */
2275 {
2276 parmp->tagname = (char_u *)argv[0] + argv_idx;
2277 argv_idx = -1;
2278 }
2279 else /* "-t {tag}" */
2280 want_argument = TRUE;
2281 break;
2282
2283#ifdef FEAT_EVAL
2284 case 'D': /* "-D" Debugging */
2285 parmp->use_debug_break_level = 9999;
2286 break;
2287#endif
2288#ifdef FEAT_DIFF
2289 case 'd': /* "-d" 'diff' */
2290# ifdef AMIGA
2291 /* check for "-dev {device}" */
2292 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2293 want_argument = TRUE;
2294 else
2295# endif
2296 parmp->diff_mode = TRUE;
2297 break;
2298#endif
2299 case 'V': /* "-V{N}" Verbose level */
2300 /* default is 10: a little bit verbose */
2301 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2302 if (argv[0][argv_idx] != NUL)
2303 {
2304 set_option_value((char_u *)"verbosefile", 0L,
2305 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002306 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002307 }
2308 break;
2309
2310 case 'v': /* "-v" Vi-mode (as if called "vi") */
2311 exmode_active = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002312#if defined(FEAT_GUI) && !defined(VIMDLL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002313 gui.starting = FALSE; /* don't start GUI */
2314#endif
2315 break;
2316
2317 case 'w': /* "-w{number}" set window height */
2318 /* "-w {scriptout}" write to script */
2319 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2320 {
2321 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2322 set_option_value((char_u *)"window", n, NULL, 0);
2323 break;
2324 }
2325 want_argument = TRUE;
2326 break;
2327
2328#ifdef FEAT_CRYPT
2329 case 'x': /* "-x" encrypted reading/writing of files */
2330 parmp->ask_for_key = TRUE;
2331 break;
2332#endif
2333
2334 case 'X': /* "-X" don't connect to X server */
2335#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2336 x_no_connect = TRUE;
2337#endif
2338 break;
2339
2340 case 'Z': /* "-Z" restricted mode */
2341 restricted = TRUE;
2342 break;
2343
2344 case 'c': /* "-c{command}" or "-c {command}" execute
2345 command */
2346 if (argv[0][argv_idx] != NUL)
2347 {
2348 if (parmp->n_commands >= MAX_ARG_CMDS)
2349 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002350 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2351 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002352 argv_idx = -1;
2353 break;
2354 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002355 /* FALLTHROUGH */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002356 case 'S': /* "-S {file}" execute Vim script */
2357 case 'i': /* "-i {viminfo}" use for viminfo */
2358#ifndef FEAT_DIFF
2359 case 'd': /* "-d {device}" device (for Amiga) */
2360#endif
2361 case 'T': /* "-T {terminal}" terminal name */
2362 case 'u': /* "-u {vimrc}" vim inits file */
2363 case 'U': /* "-U {gvimrc}" gvim inits file */
2364 case 'W': /* "-W {scriptout}" overwrite */
Bram Moolenaar4f974752019-02-17 17:44:42 +01002365#ifdef FEAT_GUI_MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002366 case 'P': /* "-P {parent title}" MDI parent */
2367#endif
2368 want_argument = TRUE;
2369 break;
2370
2371 default:
2372 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2373 }
2374
2375 /*
2376 * Handle option arguments with argument.
2377 */
2378 if (want_argument)
2379 {
2380 /*
2381 * Check for garbage immediately after the option letter.
2382 */
2383 if (argv[0][argv_idx] != NUL)
2384 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2385
2386 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002387 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002388 mainerr_arg_missing((char_u *)argv[0]);
2389 ++argv;
2390 argv_idx = -1;
2391
2392 switch (c)
2393 {
2394 case 'c': /* "-c {command}" execute command */
2395 case 'S': /* "-S {file}" execute Vim script */
2396 if (parmp->n_commands >= MAX_ARG_CMDS)
2397 mainerr(ME_EXTRA_CMD, NULL);
2398 if (c == 'S')
2399 {
2400 char *a;
2401
2402 if (argc < 1)
2403 /* "-S" without argument: use default session file
2404 * name. */
2405 a = SESSION_FILE;
2406 else if (argv[0][0] == '-')
2407 {
2408 /* "-S" followed by another option: use default
2409 * session file name. */
2410 a = SESSION_FILE;
2411 ++argc;
2412 --argv;
2413 }
2414 else
2415 a = argv[0];
Bram Moolenaar964b3742019-05-24 18:54:09 +02002416 p = alloc(STRLEN(a) + 4);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002417 if (p == NULL)
2418 mch_exit(2);
2419 sprintf((char *)p, "so %s", a);
2420 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2421 parmp->commands[parmp->n_commands++] = p;
2422 }
2423 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002424 parmp->commands[parmp->n_commands++] =
2425 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002426 break;
2427
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002428 case '-':
2429 if (argv[-1][2] == 'c')
2430 {
2431 /* "--cmd {command}" execute command */
2432 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2433 mainerr(ME_EXTRA_CMD, NULL);
2434 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002435 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002436 }
2437 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002438 break;
2439
2440 /* case 'd': -d {device} is handled in mch_check_win() for the
2441 * Amiga */
2442
2443#ifdef FEAT_QUICKFIX
2444 case 'q': /* "-q {errorfile}" QuickFix mode */
2445 parmp->use_ef = (char_u *)argv[0];
2446 break;
2447#endif
2448
2449 case 'i': /* "-i {viminfo}" use for viminfo */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002450 set_option_value((char_u *)"vif", 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002451 break;
2452
2453 case 's': /* "-s {scriptin}" read from script file */
2454 if (scriptin[0] != NULL)
2455 {
2456scripterror:
2457 mch_errmsg(_("Attempt to open script file again: \""));
2458 mch_errmsg(argv[-1]);
2459 mch_errmsg(" ");
2460 mch_errmsg(argv[0]);
2461 mch_errmsg("\"\n");
2462 mch_exit(2);
2463 }
2464 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2465 {
2466 mch_errmsg(_("Cannot open for reading: \""));
2467 mch_errmsg(argv[0]);
2468 mch_errmsg("\"\n");
2469 mch_exit(2);
2470 }
2471 if (save_typebuf() == FAIL)
2472 mch_exit(2); /* out of memory */
2473 break;
2474
2475 case 't': /* "-t {tag}" */
2476 parmp->tagname = (char_u *)argv[0];
2477 break;
2478
2479 case 'T': /* "-T {terminal}" terminal name */
2480 /*
2481 * The -T term argument is always available and when
2482 * HAVE_TERMLIB is supported it overrides the environment
2483 * variable TERM.
2484 */
2485#ifdef FEAT_GUI
2486 if (term_is_gui((char_u *)argv[0]))
2487 gui.starting = TRUE; /* start GUI a bit later */
2488 else
2489#endif
2490 parmp->term = (char_u *)argv[0];
2491 break;
2492
2493 case 'u': /* "-u {vimrc}" vim inits file */
2494 parmp->use_vimrc = (char_u *)argv[0];
2495 break;
2496
2497 case 'U': /* "-U {gvimrc}" gvim inits file */
2498#ifdef FEAT_GUI
2499 use_gvimrc = (char_u *)argv[0];
2500#endif
2501 break;
2502
2503 case 'w': /* "-w {nr}" 'window' value */
2504 /* "-w {scriptout}" append to script file */
2505 if (vim_isdigit(*((char_u *)argv[0])))
2506 {
2507 argv_idx = 0;
2508 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2509 set_option_value((char_u *)"window", n, NULL, 0);
2510 argv_idx = -1;
2511 break;
2512 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002513 /* FALLTHROUGH */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002514 case 'W': /* "-W {scriptout}" overwrite script file */
2515 if (scriptout != NULL)
2516 goto scripterror;
2517 if ((scriptout = mch_fopen(argv[0],
2518 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2519 {
2520 mch_errmsg(_("Cannot open for script output: \""));
2521 mch_errmsg(argv[0]);
2522 mch_errmsg("\"\n");
2523 mch_exit(2);
2524 }
2525 break;
2526
Bram Moolenaar4f974752019-02-17 17:44:42 +01002527#ifdef FEAT_GUI_MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002528 case 'P': /* "-P {parent title}" MDI parent */
2529 gui_mch_set_parent(argv[0]);
2530 break;
2531#endif
2532 }
2533 }
2534 }
2535
2536 /*
2537 * File name argument.
2538 */
2539 else
2540 {
2541 argv_idx = -1; /* skip to next argument */
2542
2543 /* Check for only one type of editing. */
2544 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2545 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2546 parmp->edit_type = EDIT_FILE;
2547
2548#ifdef MSWIN
2549 /* Remember if the argument was a full path before changing
2550 * slashes to backslashes. */
2551 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2552 parmp->full_path = TRUE;
2553#endif
2554
2555 /* Add the file to the global argument list. */
2556 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2557 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2558 mch_exit(2);
2559#ifdef FEAT_DIFF
2560 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2561 && !mch_isdir(alist_name(&GARGLIST[0])))
2562 {
2563 char_u *r;
2564
2565 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2566 if (r != NULL)
2567 {
2568 vim_free(p);
2569 p = r;
2570 }
2571 }
2572#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002573#if defined(__CYGWIN32__) && !defined(MSWIN)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002574 /*
2575 * If vim is invoked by non-Cygwin tools, convert away any
2576 * DOS paths, so things like .swp files are created correctly.
2577 * Look for evidence of non-Cygwin paths before we bother.
2578 * This is only for when using the Unix files.
2579 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002580 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002581 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002582 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002583
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002584# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002585 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002586# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002587 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002588# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002589 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002590 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002591 if (p == NULL)
2592 mch_exit(2);
2593 }
2594#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002595
2596#ifdef USE_FNAME_CASE
2597 /* Make the case of the file name match the actual file. */
2598 fname_case(p, 0);
2599#endif
2600
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002601 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002602#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002603 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002604#else
2605 2 /* add buffer number now and use curbuf */
2606#endif
2607 );
2608
Bram Moolenaar4f974752019-02-17 17:44:42 +01002609#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002610 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002611 /* Remember this argument has been added to the argument list.
2612 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002613 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002614# ifdef FEAT_DIFF
2615 parmp->diff_mode
2616# else
2617 FALSE
2618# endif
2619 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002620 }
2621#endif
2622 }
2623
2624 /*
2625 * If there are no more letters after the current "-", go to next
2626 * argument. argv_idx is set to -1 when the current argument is to be
2627 * skipped.
2628 */
2629 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2630 {
2631 --argc;
2632 ++argv;
2633 argv_idx = 1;
2634 }
2635 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002636
2637#ifdef FEAT_EVAL
2638 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2639 * one. */
2640 if (parmp->n_commands > 0)
2641 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002642 p = alloc(STRLEN(parmp->commands[0]) + 3);
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002643 if (p != NULL)
2644 {
2645 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2646 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2647 vim_free(p);
2648 }
2649 }
2650#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002651}
2652
2653/*
2654 * Print a warning if stdout is not a terminal.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +01002655 * When starting in Ex mode and commands come from a file, set silent_mode.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002656 */
2657 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002658check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002659{
2660 int input_isatty; /* is active input a terminal? */
2661
2662 input_isatty = mch_input_isatty();
2663 if (exmode_active)
2664 {
2665 if (!input_isatty)
2666 silent_mode = TRUE;
2667 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002668 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002669#ifdef FEAT_GUI
2670 /* don't want the delay when started from the desktop */
2671 && !gui.starting
2672#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002673 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002674 {
2675#ifdef NBDEBUG
2676 /*
2677 * This shouldn't be necessary. But if I run netbeans with the log
2678 * output coming to the console and XOpenDisplay fails, I get vim
2679 * trying to start with input/output to my console tty. This fills my
2680 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002681 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002682 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002683 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002684 {
2685 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2686 exit(1);
2687 }
2688#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002689#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2690 if (
2691# ifdef VIMDLL
2692 !gui.starting &&
2693# endif
2694 is_cygpty_used())
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002695 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002696# if defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
Bram Moolenaar2a027452017-09-26 19:10:37 +02002697 && defined(FEAT_GETTEXT)
2698 char *s, *tofree = NULL;
2699
2700 /* Set the encoding of the error message based on $LC_ALL or
2701 * other environment variables instead of 'encoding'.
2702 * Note that the message is shown on a Cygwin terminal (e.g.
2703 * mintty) which encoding is based on $LC_ALL or etc., not the
2704 * current codepage used by normal Win32 console programs. */
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002705 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002706 if (s == NULL)
2707 s = "utf-8"; /* Use "utf-8" by default. */
2708 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2709 vim_free(tofree);
2710# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002711 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2712 exit(1);
2713 }
2714#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002715 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002716 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2717 if (!input_isatty)
2718 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2719 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002720 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2721 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002722 if (scriptin[0] == NULL)
2723 ui_delay(2000L, TRUE);
2724 TIME_MSG("Warning delay");
2725 }
2726}
2727
2728/*
2729 * Read text from stdin.
2730 */
2731 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002732read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002733{
2734 int i;
2735
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002736 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002737 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002738
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002739 no_wait_return = TRUE;
2740 i = msg_didany;
2741 set_buflisted(TRUE);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002742 (void)open_buffer(TRUE, NULL, 0); // create memfile and read file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002743 no_wait_return = FALSE;
2744 msg_didany = i;
2745 TIME_MSG("reading stdin");
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002746
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002747 check_swap_exists_action();
Bram Moolenaard0573012017-10-28 21:11:06 +02002748#if !(defined(AMIGA) || defined(MACOS_X))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002749 /*
2750 * Close stdin and dup it from stderr. Required for GPM to work
2751 * properly, and for running external commands.
2752 * Is there any other system that cannot do this?
2753 */
2754 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002755 vim_ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002756#endif
2757}
2758
2759/*
2760 * Create the requested number of windows and edit buffers in them.
2761 * Also does recovery if "recoverymode" set.
2762 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002763 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002764create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002765{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002766 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002767 int done = 0;
2768
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002769 /*
2770 * Create the number of windows that was requested.
2771 */
2772 if (parmp->window_count == -1) /* was not set */
2773 parmp->window_count = 1;
2774 if (parmp->window_count == 0)
2775 parmp->window_count = GARGCOUNT;
2776 if (parmp->window_count > 1)
2777 {
2778 /* Don't change the windows if there was a command in .vimrc that
2779 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002780 if (parmp->window_layout == 0)
2781 parmp->window_layout = WIN_HOR;
2782 if (parmp->window_layout == WIN_TABS)
2783 {
2784 parmp->window_count = make_tabpages(parmp->window_count);
2785 TIME_MSG("making tab pages");
2786 }
2787 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002788 {
2789 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002790 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002791 TIME_MSG("making windows");
2792 }
2793 else
2794 parmp->window_count = win_count();
2795 }
2796 else
2797 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002798
2799 if (recoverymode) /* do recover */
2800 {
2801 msg_scroll = TRUE; /* scroll message up */
Bram Moolenaar99499b12019-05-23 21:35:48 +02002802 ml_recover(TRUE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002803 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2804 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002805 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002806 }
2807 else
2808 {
2809 /*
2810 * Open a buffer for windows that don't have one yet.
2811 * Commands in the .vimrc might have loaded a file or split the window.
2812 * Watch out for autocommands that delete a window.
2813 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002814 /*
2815 * Don't execute Win/Buf Enter/Leave autocommands here
2816 */
2817 ++autocmd_no_enter;
2818 ++autocmd_no_leave;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002819 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002820 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002821 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002822 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002823 {
2824 if (parmp->window_layout == WIN_TABS)
2825 goto_tabpage(1);
2826 else
2827 curwin = firstwin;
2828 }
2829 else if (parmp->window_layout == WIN_TABS)
2830 {
2831 if (curtab->tp_next == NULL)
2832 break;
2833 goto_tabpage(0);
2834 }
2835 else
2836 {
2837 if (curwin->w_next == NULL)
2838 break;
2839 curwin = curwin->w_next;
2840 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002841 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002842 curbuf = curwin->w_buffer;
2843 if (curbuf->b_ml.ml_mfp == NULL)
2844 {
2845#ifdef FEAT_FOLDING
2846 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2847 if (p_fdls >= 0)
2848 curwin->w_p_fdl = p_fdls;
2849#endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002850 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002851 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002852
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002853 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002854
2855 /* create memfile, read file */
2856 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002857
Bram Moolenaar84212822006-11-07 21:59:47 +00002858 if (swap_exists_action == SEA_QUIT)
2859 {
2860 if (got_int || only_one_window())
2861 {
2862 /* abort selected or quit and only one window */
2863 did_emsg = FALSE; /* avoid hit-enter prompt */
2864 getout(1);
2865 }
2866 /* We can't close the window, it would disturb what
2867 * happens next. Clear the file name and set the arg
2868 * index to -1 to delete it later. */
2869 setfname(curbuf, NULL, NULL, FALSE);
2870 curwin->w_arg_idx = -1;
2871 swap_exists_action = SEA_NONE;
2872 }
2873 else
2874 handle_swap_exists(NULL);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002875 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002876 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002877 ui_breakcheck();
2878 if (got_int)
2879 {
2880 (void)vgetc(); /* only break the file loading, not the rest */
2881 break;
2882 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002883 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002884 if (parmp->window_layout == WIN_TABS)
2885 goto_tabpage(1);
2886 else
2887 curwin = firstwin;
2888 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002889 --autocmd_no_enter;
2890 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002891 }
2892}
2893
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002894 /*
2895 * If opened more than one window, start editing files in the other
2896 * windows. make_windows() has already opened the windows.
2897 */
2898 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002899edit_buffers(
2900 mparm_T *parmp,
2901 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002902{
2903 int arg_idx; /* index in argument list */
2904 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002905 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002906 win_T *win;
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002907 char_u *p_shm_save = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002908
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002909 /*
2910 * Don't execute Win/Buf Enter/Leave autocommands here
2911 */
2912 ++autocmd_no_enter;
2913 ++autocmd_no_leave;
Bram Moolenaar84212822006-11-07 21:59:47 +00002914
2915 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2916 if (curwin->w_arg_idx == -1)
2917 {
2918 win_close(curwin, TRUE);
2919 advance = FALSE;
2920 }
2921
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002922 arg_idx = 1;
2923 for (i = 1; i < parmp->window_count; ++i)
2924 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002925 if (cwd != NULL)
2926 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002927 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2928 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002929 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002930 ++arg_idx;
2931 win_close(curwin, TRUE);
2932 advance = FALSE;
2933 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002934 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002935
Bram Moolenaar84212822006-11-07 21:59:47 +00002936 if (advance)
2937 {
2938 if (parmp->window_layout == WIN_TABS)
2939 {
2940 if (curtab->tp_next == NULL) /* just checking */
2941 break;
2942 goto_tabpage(0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002943 // Temporarily reset 'shm' option to not print fileinfo when
2944 // loading the other buffers. This would overwrite the already
2945 // existing fileinfo for the first tab.
2946 if (i == 1)
2947 {
2948 char buf[100];
2949
2950 p_shm_save = vim_strsave(p_shm);
2951 vim_snprintf(buf, 100, "F%s", p_shm);
2952 set_option_value((char_u *)"shm", 0L, (char_u *)buf, 0);
2953 }
Bram Moolenaar84212822006-11-07 21:59:47 +00002954 }
2955 else
2956 {
2957 if (curwin->w_next == NULL) /* just checking */
2958 break;
2959 win_enter(curwin->w_next, FALSE);
2960 }
2961 }
2962 advance = TRUE;
2963
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002964 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002965 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002966 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2967 {
2968 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002969 /* Edit file from arg list, if there is one. When "Quit" selected
2970 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002971 swap_exists_did_quit = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002972 (void)do_ecmd(0, arg_idx < GARGCOUNT
2973 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002974 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002975 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002976 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002977 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002978 if (got_int || only_one_window())
2979 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002980 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002981 did_emsg = FALSE; /* avoid hit-enter prompt */
2982 getout(1);
2983 }
2984 win_close(curwin, TRUE);
2985 advance = FALSE;
2986 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002987 if (arg_idx == GARGCOUNT - 1)
2988 arg_had_last = TRUE;
2989 ++arg_idx;
2990 }
2991 ui_breakcheck();
2992 if (got_int)
2993 {
2994 (void)vgetc(); /* only break the file loading, not the rest */
2995 break;
2996 }
2997 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002998
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002999 if (p_shm_save != NULL)
3000 {
3001 set_option_value((char_u *)"shm", 0L, p_shm_save, 0);
3002 vim_free(p_shm_save);
3003 }
3004
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003005 if (parmp->window_layout == WIN_TABS)
3006 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003007 --autocmd_no_enter;
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003008
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003009 /* make the first window the current window */
3010 win = firstwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003011#if defined(FEAT_QUICKFIX)
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003012 /* Avoid making a preview window the current window. */
3013 while (win->w_p_pvw)
3014 {
3015 win = win->w_next;
3016 if (win == NULL)
3017 {
3018 win = firstwin;
3019 break;
3020 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003021 }
3022#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003023 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003024
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003025 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003026 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003027 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003028 win_equal(curwin, FALSE, 'b'); /* adjust heights */
3029}
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003030
3031/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003032 * Execute the commands from --cmd arguments "cmds[cnt]".
3033 */
3034 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003035exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003036{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003037 char_u **cmds = parmp->pre_commands;
3038 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003039 int i;
3040
3041 if (cnt > 0)
3042 {
3043 curwin->w_cursor.lnum = 0; /* just in case.. */
3044 sourcing_name = (char_u *)_("pre-vimrc command line");
3045# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003046 current_sctx.sc_sid = SID_CMDARG;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003047# endif
3048 for (i = 0; i < cnt; ++i)
3049 do_cmdline_cmd(cmds[i]);
3050 sourcing_name = NULL;
3051# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003052 current_sctx.sc_sid = 0;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003053# endif
3054 TIME_MSG("--cmd commands");
3055 }
3056}
3057
3058/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003059 * Execute "+", "-c" and "-S" arguments.
3060 */
3061 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003062exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003063{
3064 int i;
3065
3066 /*
3067 * We start commands on line 0, make "vim +/pat file" match a
3068 * pattern on line 1. But don't move the cursor when an autocommand
3069 * with g`" was used.
3070 */
3071 msg_scroll = TRUE;
3072 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
3073 curwin->w_cursor.lnum = 0;
3074 sourcing_name = (char_u *)"command line";
3075#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003076 current_sctx.sc_sid = SID_CARG;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003077 current_sctx.sc_seq = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003078#endif
3079 for (i = 0; i < parmp->n_commands; ++i)
3080 {
3081 do_cmdline_cmd(parmp->commands[i]);
3082 if (parmp->cmds_tofree[i])
3083 vim_free(parmp->commands[i]);
3084 }
3085 sourcing_name = NULL;
3086#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003087 current_sctx.sc_sid = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003088#endif
3089 if (curwin->w_cursor.lnum == 0)
3090 curwin->w_cursor.lnum = 1;
3091
3092 if (!exmode_active)
3093 msg_scroll = FALSE;
3094
3095#ifdef FEAT_QUICKFIX
3096 /* When started with "-q errorfile" jump to first error again. */
3097 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003098 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003099#endif
3100 TIME_MSG("executing command arguments");
3101}
3102
3103/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003104 * Source startup scripts.
3105 */
3106 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003107source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003108{
3109 int i;
3110
3111 /*
3112 * For "evim" source evim.vim first of all, so that the user can overrule
3113 * any things he doesn't like.
3114 */
3115 if (parmp->evim_mode)
3116 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003117 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003118 TIME_MSG("source evim file");
3119 }
3120
3121 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003122 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00003123 * nothing else.
3124 */
3125 if (parmp->use_vimrc != NULL)
3126 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003127 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
3128 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
3129 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003130 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003131 {
3132#ifdef FEAT_GUI
3133 if (use_gvimrc == NULL) /* don't load gvimrc either */
3134 use_gvimrc = parmp->use_vimrc;
3135#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003136 }
3137 else
3138 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003139 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003140 semsg(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003141 }
3142 }
3143 else if (!silent_mode)
3144 {
3145#ifdef AMIGA
3146 struct Process *proc = (struct Process *)FindTask(0L);
3147 APTR save_winptr = proc->pr_WindowPtr;
3148
3149 /* Avoid a requester here for a volume that doesn't exist. */
3150 proc->pr_WindowPtr = (APTR)-1L;
3151#endif
3152
3153 /*
3154 * Get system wide defaults, if the file name is defined.
3155 */
3156#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003157 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003158#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003159#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003160 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003161#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003162
3163 /*
3164 * Try to read initialization commands from the following places:
3165 * - environment variable VIMINIT
3166 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3167 * - second user vimrc file ($VIM/.vimrc for Dos)
3168 * - environment variable EXINIT
3169 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3170 * - second user exrc file ($VIM/.exrc for Dos)
3171 * The first that exists is used, the rest is ignored.
3172 */
3173 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3174 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003175 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003176#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003177 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3178 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003179#endif
3180#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003181 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3182 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003183#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003184#ifdef USR_VIMRC_FILE4
3185 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3186 DOSO_VIMRC) == FAIL
3187#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003188 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003189 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003190#ifdef USR_EXRC_FILE2
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003191 && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003192#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003193 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003194 {
3195 /* When no .vimrc file was found: source defaults.vim. */
3196 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003197 }
3198 }
3199
3200 /*
3201 * Read initialization commands from ".vimrc" or ".exrc" in current
3202 * directory. This is only done if the 'exrc' option is set.
3203 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003204 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003205 * option has been reset in environment of global ".exrc" or ".vimrc".
3206 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3207 * SYS_VIMRC_FILE.
3208 */
3209 if (p_exrc)
3210 {
3211#if defined(UNIX) || defined(VMS)
3212 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3213 if (!file_owned(VIMRC_FILE))
3214#endif
3215 secure = p_secure;
3216
3217 i = FAIL;
3218 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003219 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003220#ifdef USR_VIMRC_FILE2
3221 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003222 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003223#endif
3224#ifdef USR_VIMRC_FILE3
3225 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003226 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003227#endif
3228#ifdef SYS_VIMRC_FILE
3229 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003230 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003231#endif
3232 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003233 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003234
3235 if (i == FAIL)
3236 {
3237#if defined(UNIX) || defined(VMS)
3238 /* if ".exrc" is not owned by user set 'secure' mode */
3239 if (!file_owned(EXRC_FILE))
3240 secure = p_secure;
3241 else
3242 secure = 0;
3243#endif
3244 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003245 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003246#ifdef USR_EXRC_FILE2
3247 && fullpathcmp((char_u *)USR_EXRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003248 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003249#endif
3250 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003251 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003252 }
3253 }
3254 if (secure == 2)
3255 need_wait_return = TRUE;
3256 secure = 0;
3257#ifdef AMIGA
3258 proc->pr_WindowPtr = save_winptr;
3259#endif
3260 }
3261 TIME_MSG("sourcing vimrc file(s)");
3262}
3263
3264/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003265 * Setup to start using the GUI. Exit with an error when not available.
3266 */
3267 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003268main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003269{
3270#ifdef FEAT_GUI
3271 gui.starting = TRUE; /* start GUI a bit later */
3272#else
3273 mch_errmsg(_(e_nogvim));
3274 mch_errmsg("\n");
3275 mch_exit(2);
3276#endif
3277}
3278
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003279#endif /* NO_VIM_MAIN */
3280
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003281/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003282 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003283 * Returns FAIL if the environment variable was not executed, OK otherwise.
3284 */
3285 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003286process_env(
3287 char_u *env,
3288 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003289{
3290 char_u *initstr;
3291 char_u *save_sourcing_name;
3292 linenr_T save_sourcing_lnum;
3293#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003294 sctx_T save_current_sctx;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003295#endif
3296
3297 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3298 {
3299 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003300 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003301 save_sourcing_name = sourcing_name;
3302 save_sourcing_lnum = sourcing_lnum;
3303 sourcing_name = env;
3304 sourcing_lnum = 0;
3305#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003306 save_current_sctx = current_sctx;
3307 current_sctx.sc_sid = SID_ENV;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003308 current_sctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003309 current_sctx.sc_lnum = 0;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003310 current_sctx.sc_version = 1;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003311#endif
3312 do_cmdline_cmd(initstr);
3313 sourcing_name = save_sourcing_name;
3314 sourcing_lnum = save_sourcing_lnum;
3315#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003316 current_sctx = save_current_sctx;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003317#endif
3318 return OK;
3319 }
3320 return FAIL;
3321}
3322
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003323#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003324/*
3325 * Return TRUE if we are certain the user owns the file "fname".
3326 * Used for ".vimrc" and ".exrc".
3327 * Use both stat() and lstat() for extra security.
3328 */
3329 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003330file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003331{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003332 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003333# ifdef UNIX
3334 uid_t uid = getuid();
3335# else /* VMS */
3336 uid_t uid = ((getgid() << 16) | getuid());
3337# endif
3338
3339 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3340# ifdef HAVE_LSTAT
3341 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3342# endif
3343 );
3344}
3345#endif
3346
3347/*
3348 * Give an error message main_errors["n"] and exit.
3349 */
3350 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003351mainerr(
3352 int n, /* one of the ME_ defines */
3353 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003354{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003355#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003356 reset_signals(); /* kill us with CTRL-C here, if you like */
3357#endif
3358
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003359 // If this is a Windows GUI executable, show an error dialog box.
3360#ifdef VIMDLL
3361 gui.in_use = mch_is_gui_executable();
3362#endif
3363#ifdef FEAT_GUI_MSWIN
3364 gui.starting = FALSE; // Needed to show as error.
3365#endif
3366
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003367 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003368 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003369 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003370 mch_errmsg(_(main_errors[n]));
3371 if (str != NULL)
3372 {
3373 mch_errmsg(": \"");
3374 mch_errmsg((char *)str);
3375 mch_errmsg("\"");
3376 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003377 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003378
3379 mch_exit(1);
3380}
3381
3382 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003383mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003384{
3385 mainerr(ME_ARG_MISSING, str);
3386}
3387
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003388#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003389/*
3390 * print a message with three spaces prepended and '\n' appended.
3391 */
3392 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003393main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003394{
3395 mch_msg(" ");
3396 mch_msg(s);
3397 mch_msg("\n");
3398}
3399
3400/*
3401 * Print messages for "vim -h" or "vim --help" and exit.
3402 */
3403 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003404usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003405{
3406 int i;
3407 static char *(use[]) =
3408 {
3409 N_("[file ..] edit specified file(s)"),
3410 N_("- read text from stdin"),
3411 N_("-t tag edit file where tag is defined"),
3412#ifdef FEAT_QUICKFIX
3413 N_("-q [errorfile] edit file with first error")
3414#endif
3415 };
3416
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003417#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003418 reset_signals(); /* kill us with CTRL-C here, if you like */
3419#endif
3420
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003421 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003422 mch_msg(longVersion);
Bram Moolenaar32aaf5a2018-06-21 21:38:33 +02003423 mch_msg(_("\n\nUsage:"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003424 for (i = 0; ; ++i)
3425 {
3426 mch_msg(_(" vim [arguments] "));
3427 mch_msg(_(use[i]));
3428 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3429 break;
3430 mch_msg(_("\n or:"));
3431 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003432#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003433 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003434#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003435
3436 mch_msg(_("\n\nArguments:\n"));
3437 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003438#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003439 main_msg(_("--literal\t\tDon't expand wildcards"));
3440#endif
3441#ifdef FEAT_OLE
3442 main_msg(_("-register\t\tRegister this gvim for OLE"));
3443 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3444#endif
3445#ifdef FEAT_GUI
3446 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3447 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3448#endif
3449 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3450 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003451 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003452 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3453#ifdef FEAT_DIFF
3454 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3455#endif
3456 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3457 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3458 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3459 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3460 main_msg(_("-M\t\t\tModifications in text not allowed"));
3461 main_msg(_("-b\t\t\tBinary mode"));
3462#ifdef FEAT_LISP
3463 main_msg(_("-l\t\t\tLisp mode"));
3464#endif
3465 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3466 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003467 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3468#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003469 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003470#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003471 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3472 main_msg(_("-r\t\t\tList swap files and exit"));
3473 main_msg(_("-r (with file name)\tRecover crashed session"));
3474 main_msg(_("-L\t\t\tSame as -r"));
3475#ifdef AMIGA
3476 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3477 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3478#endif
3479#ifdef FEAT_ARABIC
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003480 main_msg(_("-A\t\t\tStart in Arabic mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003481#endif
3482#ifdef FEAT_RIGHTLEFT
3483 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3484#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003485 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003486 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003487 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003488 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3489#ifdef FEAT_GUI
3490 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3491#endif
3492 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003493 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003494 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3495 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3496 main_msg(_("+\t\t\tStart at end of file"));
3497 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003498 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003499 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3500 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3501 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3502 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3503 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3504#ifdef FEAT_CRYPT
3505 main_msg(_("-x\t\t\tEdit encrypted files"));
3506#endif
3507#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3508# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3509 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3510# endif
3511 main_msg(_("-X\t\t\tDo not connect to X server"));
3512#endif
3513#ifdef FEAT_CLIENTSERVER
3514 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3515 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3516 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3517 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003518 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003519 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3520 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3521 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3522 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3523#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003524#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003525 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003526#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003527#ifdef FEAT_VIMINFO
3528 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3529#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003530 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003531 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3532 main_msg(_("--version\t\tPrint version information and exit"));
3533
3534#ifdef FEAT_GUI_X11
3535# ifdef FEAT_GUI_MOTIF
3536 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3537# else
3538# ifdef FEAT_GUI_ATHENA
3539# ifdef FEAT_GUI_NEXTAW
3540 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3541# else
3542 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3543# endif
3544# endif
3545# endif
3546 main_msg(_("-display <display>\tRun vim on <display>"));
3547 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003548 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3549 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3550 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3551 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3552 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3553 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3554 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3555 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3556# ifdef FEAT_GUI_ATHENA
3557 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3558# endif
3559 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3560 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3561 main_msg(_("-xrm <resource>\tSet the specified resource"));
3562#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003563#ifdef FEAT_GUI_GTK
3564 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3565 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3566 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3567 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3568 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003569 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003570 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003571 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003572#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003573#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003574# ifdef VIMDLL
3575 if (gui.starting)
3576# endif
3577 {
3578 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
3579 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
3580 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003581#endif
3582
3583#ifdef FEAT_GUI_GNOME
3584 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3585 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003586 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003587 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003588 gui.dofork = FALSE;
3589 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003590 else
3591#endif
3592 mch_exit(0);
3593}
3594
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003595/*
3596 * Check the result of the ATTENTION dialog:
3597 * When "Quit" selected, exit Vim.
3598 * When "Recover" selected, recover the file.
3599 */
3600 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003601check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003602{
3603 if (swap_exists_action == SEA_QUIT)
3604 getout(1);
3605 handle_swap_exists(NULL);
3606}
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003607
Bram Moolenaar08cab962017-03-04 14:37:18 +01003608#endif /* NO_VIM_MAIN */
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003609
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003610#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003611static struct timeval prev_timeval;
3612
Bram Moolenaar4f974752019-02-17 17:44:42 +01003613# ifdef MSWIN
Bram Moolenaar3f269672009-11-03 11:11:11 +00003614/*
3615 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3616 */
3617 static int
3618gettimeofday(struct timeval *tv, char *dummy)
3619{
3620 long t = clock();
3621 tv->tv_sec = t / CLOCKS_PER_SEC;
3622 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3623 return 0;
3624}
3625# endif
3626
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003627/*
3628 * Save the previous time before doing something that could nest.
3629 * set "*tv_rel" to the time elapsed so far.
3630 */
3631 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003632time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003633{
3634 *((struct timeval *)tv_rel) = prev_timeval;
3635 gettimeofday(&prev_timeval, NULL);
3636 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3637 - ((struct timeval *)tv_rel)->tv_usec;
3638 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3639 - ((struct timeval *)tv_rel)->tv_sec;
3640 if (((struct timeval *)tv_rel)->tv_usec < 0)
3641 {
3642 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3643 --((struct timeval *)tv_rel)->tv_sec;
3644 }
3645 *(struct timeval *)tv_start = prev_timeval;
3646}
3647
3648/*
3649 * Compute the previous time after doing something that could nest.
3650 * Subtract "*tp" from prev_timeval;
3651 * Note: The arguments are (void *) to avoid trouble with systems that don't
3652 * have struct timeval.
3653 */
3654 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003655time_pop(
3656 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003657{
3658 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3659 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3660 if (prev_timeval.tv_usec < 0)
3661 {
3662 prev_timeval.tv_usec += 1000000;
3663 --prev_timeval.tv_sec;
3664 }
3665}
3666
3667 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003668time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003669{
3670 long usec;
3671 long msec;
3672
3673 usec = now->tv_usec - then->tv_usec;
3674 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3675 usec = usec % 1000L;
3676 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3677}
3678
3679 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003680time_msg(
3681 char *mesg,
3682 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003683 (struct timeval *) */
3684{
3685 static struct timeval start;
3686 struct timeval now;
3687
3688 if (time_fd != NULL)
3689 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003690 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003691 {
3692 gettimeofday(&start, NULL);
3693 prev_timeval = start;
3694 fprintf(time_fd, "\n\ntimes in msec\n");
3695 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3696 fprintf(time_fd, " clock elapsed: other lines\n\n");
3697 }
3698 gettimeofday(&now, NULL);
3699 time_diff(&start, &now);
3700 if (((struct timeval *)tv_start) != NULL)
3701 {
3702 fprintf(time_fd, " ");
3703 time_diff(((struct timeval *)tv_start), &now);
3704 }
3705 fprintf(time_fd, " ");
3706 time_diff(&prev_timeval, &now);
3707 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003708 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003709 }
3710}
3711
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003712#endif
3713
Bram Moolenaar595297d2017-03-04 19:11:12 +01003714#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003715 static void
3716set_progpath(char_u *argv0)
3717{
3718 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003719
Bram Moolenaar4f974752019-02-17 17:44:42 +01003720# ifdef MSWIN
Bram Moolenaar08cab962017-03-04 14:37:18 +01003721 /* A relative path containing a "/" will become invalid when using ":cd",
3722 * turn it into a full path.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003723 * On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3724 * this. */
Bram Moolenaar066029e2017-03-05 15:19:32 +01003725 char_u *path = NULL;
3726
3727 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3728 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003729# else
3730 char_u buf[MAXPATHL + 1];
3731# ifdef PROC_EXE_LINK
3732 char linkbuf[MAXPATHL + 1];
3733 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003734
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003735 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3736 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003737 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003738 linkbuf[len] = NUL;
3739 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003740 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003741# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003742
3743 if (!mch_isFullName(val))
3744 {
3745 if (gettail(val) != val
3746 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3747 val = buf;
3748 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003749# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003750
Bram Moolenaar08cab962017-03-04 14:37:18 +01003751 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003752
Bram Moolenaar4f974752019-02-17 17:44:42 +01003753# ifdef MSWIN
Bram Moolenaar43663192017-03-05 14:29:12 +01003754 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003755# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003756}
3757
3758#endif /* NO_VIM_MAIN */
3759
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003760#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003761
3762/*
3763 * Common code for the X command server and the Win32 command server.
3764 */
3765
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003766static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003767
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003768/*
3769 * Do the client-server stuff, unless "--servername ''" was used.
3770 */
3771 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003772exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003773{
3774 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3775 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003776# ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003777 /* Initialise the client/server messaging infrastructure. */
3778 serverInitMessaging();
3779# endif
3780
3781 /*
3782 * When a command server argument was found, execute it. This may
3783 * exit Vim when it was successful. Otherwise it's executed further
3784 * on. Remember the encoding used here in "serverStrEnc".
3785 */
3786 if (parmp->serverArg)
3787 {
3788 cmdsrv_main(&parmp->argc, parmp->argv,
3789 parmp->serverName_arg, &parmp->serverStr);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003790 parmp->serverStrEnc = vim_strsave(p_enc);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003791 }
3792
3793 /* If we're still running, get the name to register ourselves.
3794 * On Win32 can register right now, for X11 need to setup the
3795 * clipboard first, it's further down. */
3796 parmp->servername = serverMakeName(parmp->serverName_arg,
3797 parmp->argv[0]);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003798# ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003799 if (parmp->servername != NULL)
3800 {
3801 serverSetName(parmp->servername);
3802 vim_free(parmp->servername);
3803 }
3804# endif
3805 }
3806}
3807
3808/*
3809 * Prepare for running as a Vim server.
3810 */
3811 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003812prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003813{
3814# if defined(FEAT_X11)
3815 /*
3816 * Register for remote command execution with :serversend and --remote
3817 * unless there was a -X or a --servername '' on the command line.
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003818 * Only register nongui-vim's with an explicit --servername argument,
3819 * or when compiling with autoservername.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003820 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003821 */
3822 if (X_DISPLAY != NULL && parmp->servername != NULL && (
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003823# if defined(FEAT_AUTOSERVERNAME) || defined(FEAT_GUI)
3824 (
3825# if defined(FEAT_AUTOSERVERNAME)
3826 1
3827# else
3828 gui.in_use
3829# endif
Bram Moolenaar5f402312006-08-15 19:40:35 +00003830# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003831 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003832# endif
3833 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003834# endif
3835 parmp->serverName_arg != NULL))
3836 {
3837 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3838 vim_free(parmp->servername);
3839 TIME_MSG("register server name");
3840 }
3841 else
3842 serverDelayedStartName = parmp->servername;
3843# endif
3844
3845 /*
3846 * Execute command ourselves if we're here because the send failed (or
3847 * else we would have exited above).
3848 */
3849 if (parmp->serverStr != NULL)
3850 {
3851 char_u *p;
3852
3853 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3854 parmp->serverStr, &p));
3855 vim_free(p);
3856 }
3857}
3858
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003859 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003860cmdsrv_main(
3861 int *argc,
3862 char **argv,
3863 char_u *serverName_arg,
3864 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003865{
3866 char_u *res;
3867 int i;
3868 char_u *sname;
3869 int ret;
3870 int didone = FALSE;
3871 int exiterr = 0;
3872 char **newArgV = argv + 1;
3873 int newArgC = 1,
3874 Argc = *argc;
3875 int argtype;
3876#define ARGTYPE_OTHER 0
3877#define ARGTYPE_EDIT 1
3878#define ARGTYPE_EDIT_WAIT 2
3879#define ARGTYPE_SEND 3
3880 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003881 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003882# ifndef FEAT_X11
3883 HWND srv;
3884# else
3885 Window srv;
3886
3887 setup_term_clip();
3888# endif
3889
3890 sname = serverMakeName(serverName_arg, argv[0]);
3891 if (sname == NULL)
3892 return;
3893
3894 /*
3895 * Execute the command server related arguments and remove them
3896 * from the argc/argv array; We may have to return into main()
3897 */
3898 for (i = 1; i < Argc; i++)
3899 {
3900 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003901 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003902 {
3903 for (; i < *argc; i++)
3904 {
3905 *newArgV++ = argv[i];
3906 newArgC++;
3907 }
3908 break;
3909 }
3910
Bram Moolenaareb94e552006-03-11 21:35:11 +00003911 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003912 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003913 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3914 {
3915 char *p = argv[i] + 8;
3916
3917 argtype = ARGTYPE_EDIT;
3918 while (*p != NUL)
3919 {
3920 if (STRNICMP(p, "-wait", 5) == 0)
3921 {
3922 argtype = ARGTYPE_EDIT_WAIT;
3923 p += 5;
3924 }
3925 else if (STRNICMP(p, "-silent", 7) == 0)
3926 {
3927 silent = TRUE;
3928 p += 7;
3929 }
3930 else if (STRNICMP(p, "-tab", 4) == 0)
3931 {
3932 tabs = TRUE;
3933 p += 4;
3934 }
3935 else
3936 {
3937 argtype = ARGTYPE_OTHER;
3938 break;
3939 }
3940 }
3941 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003942 else
3943 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003944
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003945 if (argtype != ARGTYPE_OTHER)
3946 {
3947 if (i == *argc - 1)
3948 mainerr_arg_missing((char_u *)argv[i]);
3949 if (argtype == ARGTYPE_SEND)
3950 {
3951 *serverStr = (char_u *)argv[i + 1];
3952 i++;
3953 }
3954 else
3955 {
3956 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003957 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003958 if (*serverStr == NULL)
3959 {
3960 /* Probably out of memory, exit. */
3961 didone = TRUE;
3962 exiterr = 1;
3963 break;
3964 }
3965 Argc = i;
3966 }
3967# ifdef FEAT_X11
3968 if (xterm_dpy == NULL)
3969 {
3970 mch_errmsg(_("No display"));
3971 ret = -1;
3972 }
3973 else
3974 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003975 NULL, &srv, 0, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003976# else
3977 /* Win32 always works? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003978 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003979# endif
3980 if (ret < 0)
3981 {
3982 if (argtype == ARGTYPE_SEND)
3983 {
3984 /* Failed to send, abort. */
3985 mch_errmsg(_(": Send failed.\n"));
3986 didone = TRUE;
3987 exiterr = 1;
3988 }
3989 else if (!silent)
3990 /* Let vim start normally. */
3991 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3992 break;
3993 }
3994
Bram Moolenaar4f974752019-02-17 17:44:42 +01003995# ifdef FEAT_GUI_MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003996 /* Guess that when the server name starts with "g" it's a GUI
3997 * server, which we can bring to the foreground here.
3998 * Foreground() in the server doesn't work very well. */
3999 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
4000 SetForegroundWindow(srv);
4001# endif
4002
4003 /*
4004 * For --remote-wait: Wait until the server did edit each
4005 * file. Also detect that the server no longer runs.
4006 */
4007 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
4008 {
4009 int numFiles = *argc - i - 1;
4010 int j;
4011 char_u *done = alloc(numFiles);
4012 char_u *p;
Bram Moolenaar4f974752019-02-17 17:44:42 +01004013# ifdef FEAT_GUI_MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004014 NOTIFYICONDATA ni;
4015 int count = 0;
4016 extern HWND message_window;
4017# endif
4018
4019 if (numFiles > 0 && argv[i + 1][0] == '+')
4020 /* Skip "+cmd" argument, don't wait for it to be edited. */
4021 --numFiles;
4022
Bram Moolenaar4f974752019-02-17 17:44:42 +01004023# ifdef FEAT_GUI_MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004024 ni.cbSize = sizeof(ni);
4025 ni.hWnd = message_window;
4026 ni.uID = 0;
4027 ni.uFlags = NIF_ICON|NIF_TIP;
4028 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
4029 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
4030 Shell_NotifyIcon(NIM_ADD, &ni);
4031# endif
4032
4033 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02004034 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004035 while (memchr(done, 0, numFiles) != NULL)
4036 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004037# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01004038 p = serverGetReply(srv, NULL, TRUE, TRUE, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004039 if (p == NULL)
4040 break;
4041# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01004042 if (serverReadReply(xterm_dpy, srv, &p, TRUE, -1) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004043 break;
4044# endif
4045 j = atoi((char *)p);
4046 if (j >= 0 && j < numFiles)
4047 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004048# ifdef FEAT_GUI_MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004049 ++count;
4050 sprintf(ni.szTip, _("%d of %d edited"),
4051 count, numFiles);
4052 Shell_NotifyIcon(NIM_MODIFY, &ni);
4053# endif
4054 done[j] = 1;
4055 }
4056 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01004057# ifdef FEAT_GUI_MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004058 Shell_NotifyIcon(NIM_DELETE, &ni);
4059# endif
4060 }
4061 }
4062 else if (STRICMP(argv[i], "--remote-expr") == 0)
4063 {
4064 if (i == *argc - 1)
4065 mainerr_arg_missing((char_u *)argv[i]);
Bram Moolenaar4f974752019-02-17 17:44:42 +01004066# ifdef MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004067 /* Win32 always works? */
4068 if (serverSendToVim(sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01004069 &res, NULL, 1, 0, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004070# else
4071 if (xterm_dpy == NULL)
4072 mch_errmsg(_("No display: Send expression failed.\n"));
4073 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01004074 &res, NULL, 1, 0, 1, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004075# endif
4076 {
4077 if (res != NULL && *res != NUL)
4078 {
4079 /* Output error from remote */
4080 mch_errmsg((char *)res);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004081 VIM_CLEAR(res);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004082 }
4083 mch_errmsg(_(": Send expression failed.\n"));
4084 }
4085 }
4086 else if (STRICMP(argv[i], "--serverlist") == 0)
4087 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01004088# ifdef MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004089 /* Win32 always works? */
4090 res = serverGetVimNames();
4091# else
4092 if (xterm_dpy != NULL)
4093 res = serverGetVimNames(xterm_dpy);
4094# endif
4095 if (called_emsg)
4096 mch_errmsg("\n");
4097 }
4098 else if (STRICMP(argv[i], "--servername") == 0)
4099 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00004100 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004101 i++;
4102 continue;
4103 }
4104 else
4105 {
4106 *newArgV++ = argv[i];
4107 newArgC++;
4108 continue;
4109 }
4110 didone = TRUE;
4111 if (res != NULL && *res != NUL)
4112 {
4113 mch_msg((char *)res);
4114 if (res[STRLEN(res) - 1] != '\n')
4115 mch_msg("\n");
4116 }
4117 vim_free(res);
4118 }
4119
4120 if (didone)
4121 {
4122 display_errors(); /* display any collected messages */
4123 exit(exiterr); /* Mission accomplished - get out */
4124 }
4125
4126 /* Return back into main() */
4127 *argc = newArgC;
4128 vim_free(sname);
4129}
4130
4131/*
4132 * Build a ":drop" command to send to a Vim server.
4133 */
4134 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004135build_drop_cmd(
4136 int filec,
4137 char **filev,
4138 int tabs, /* Use ":tab drop" instead of ":drop". */
4139 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004140{
4141 garray_T ga;
4142 int i;
4143 char_u *inicmd = NULL;
4144 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004145 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004146 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004147
4148 if (filec > 0 && filev[0][0] == '+')
4149 {
4150 inicmd = (char_u *)filev[0] + 1;
4151 filev++;
4152 filec--;
4153 }
4154 /* Check if we have at least one argument. */
4155 if (filec <= 0)
4156 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004157
4158 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02004159 cwd = alloc(MAXPATHL);
4160 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004161 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004162 if (mch_dirname(cwd, MAXPATHL) != OK)
4163 {
4164 vim_free(cwd);
4165 return NULL;
4166 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004167 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00004168#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004169 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00004170#else
4171 PATH_ESC_CHARS,
4172#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02004173 '\\', TRUE);
4174 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004175 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004176 return NULL;
4177 ga_init2(&ga, 1, 100);
4178 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004179 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00004180
4181 /* Call inputsave() so that a prompt for an encryption key works. */
4182 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
4183 if (tabs)
4184 ga_concat(&ga, (char_u *)"tab ");
4185 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004186 for (i = 0; i < filec; i++)
4187 {
4188 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004189 * do it again in the Vim server. On MS-Windows only escape
4190 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004191 p = vim_strsave_escaped((char_u *)filev[i],
4192#ifdef UNIX
4193 PATH_ESC_CHARS
4194#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004195 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004196#endif
4197 );
4198 if (p == NULL)
4199 {
4200 vim_free(ga.ga_data);
4201 return NULL;
4202 }
4203 ga_concat(&ga, (char_u *)" ");
4204 ga_concat(&ga, p);
4205 vim_free(p);
4206 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004207 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4208
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004209 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4210 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004211 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4212
4213 /* Switch back to the correct current directory (prior to temporary path
4214 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004215 * correct after the :drop command. With line breaks and spaces:
4216 * if !exists('+acd') || !&acd
4217 * if haslocaldir()
4218 * cd -
4219 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004220 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004221 * cd -
4222 * endif
4223 * endif
4224 */
4225 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004226 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004227 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004228 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004229 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004230
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004231 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004232 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4233 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004234 if (inicmd != NULL)
4235 {
4236 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4237 * the following commands to be inserted as text. Use a "|",
4238 * hopefully "inicmd" does allow this... */
4239 ga_concat(&ga, inicmd);
4240 ga_concat(&ga, (char_u *)"|");
4241 }
4242 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4243 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004244 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004245 ga_append(&ga, NUL);
4246 return ga.ga_data;
4247}
4248
4249/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004250 * Make our basic server name: use the specified "arg" if given, otherwise use
4251 * the tail of the command "cmd" we were started with.
4252 * Return the name in allocated memory. This doesn't include a serial number.
4253 */
4254 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004255serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004256{
4257 char_u *p;
4258
4259 if (arg != NULL && *arg != NUL)
4260 p = vim_strsave_up(arg);
4261 else
4262 {
4263 p = vim_strsave_up(gettail((char_u *)cmd));
4264 /* Remove .exe or .bat from the name. */
4265 if (p != NULL && vim_strchr(p, '.') != NULL)
4266 *vim_strchr(p, '.') = NUL;
4267 }
4268 return p;
4269}
4270#endif /* FEAT_CLIENTSERVER */
4271
4272#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4273/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004274 * Replace termcodes such as <CR> and insert as key presses if there is room.
4275 */
4276 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004277server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004278{
4279 char_u *ptr = NULL;
4280 char_u *cpo_save = p_cpo;
4281
4282 /* Set 'cpoptions' the way we want it.
4283 * B set - backslashes are *not* treated specially
4284 * k set - keycodes are *not* reverse-engineered
4285 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004286 * The last but one parameter of replace_termcodes() is TRUE so that the
4287 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004288 */
4289 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004290 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004291 p_cpo = cpo_save;
4292
4293 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4294 {
4295 /*
4296 * Add the string to the input stream.
4297 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4298 *
4299 * First clear typed characters from the typeahead buffer, there could
4300 * be half a mapping there. Then append to the existing string, so
4301 * that multiple commands from a client are concatenated.
4302 */
4303 if (typebuf.tb_maplen < typebuf.tb_len)
4304 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4305 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4306
4307 /* Let input_available() know we inserted text in the typeahead
4308 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004309 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004310 }
4311 vim_free((char_u *)ptr);
4312}
4313
4314/*
4315 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004316 */
4317 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004318eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004319{
4320 char_u *res;
4321 int save_dbl = debug_break_level;
4322 int save_ro = redir_off;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02004323 funccal_entry_T funccal_entry;
4324 int did_save_funccal = FALSE;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004325
4326 /* Evaluate the expression at the toplevel, don't use variables local to
Bram Moolenaard99388b2017-10-26 14:28:32 +02004327 * the calling function. Except when in debug mode. */
4328 if (!debug_mode)
Bram Moolenaar27e80c82018-10-14 21:41:01 +02004329 {
4330 save_funccal(&funccal_entry);
4331 did_save_funccal = TRUE;
4332 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004333
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004334 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4335 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004336 debug_break_level = -1;
4337 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004338 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4339 * to be typed. Do generate errors so that try/catch works. */
4340 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004341
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004342 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004343
4344 debug_break_level = save_dbl;
4345 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004346 --emsg_silent;
4347 if (emsg_silent < 0)
4348 emsg_silent = 0;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02004349 if (did_save_funccal)
4350 restore_funccal();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004351
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004352 /* A client can tell us to redraw, but not to display the cursor, so do
4353 * that here. */
4354 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01004355 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004356
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004357 return res;
4358}
4359
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004360/*
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004361 * Evaluate a command or expression sent to ourselves.
4362 */
4363 int
4364sendToLocalVim(char_u *cmd, int asExpr, char_u **result)
4365{
4366 if (asExpr)
4367 {
4368 char_u *ret;
4369
4370 ret = eval_client_expr_to_string(cmd);
4371 if (result != NULL)
4372 {
4373 if (ret == NULL)
4374 {
4375 char *err = _(e_invexprmsg);
4376 size_t len = STRLEN(cmd) + STRLEN(err) + 5;
4377 char_u *msg;
4378
Bram Moolenaar964b3742019-05-24 18:54:09 +02004379 msg = alloc(len);
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004380 if (msg != NULL)
4381 vim_snprintf((char *)msg, len, "%s: \"%s\"", err, cmd);
4382 *result = msg;
4383 }
4384 else
4385 *result = ret;
4386 }
4387 else
4388 vim_free(ret);
4389 return ret == NULL ? -1 : 0;
4390 }
4391 server_to_input_buf(cmd);
4392 return 0;
4393}
4394
4395/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004396 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4397 * return an allocated string. Otherwise return "data".
4398 * "*tofree" is set to the result when it needs to be freed later.
4399 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004400 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004401serverConvert(
4402 char_u *client_enc UNUSED,
4403 char_u *data,
4404 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004405{
4406 char_u *res = data;
4407
4408 *tofree = NULL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004409 if (client_enc != NULL && p_enc != NULL)
4410 {
4411 vimconv_T vimconv;
4412
4413 vimconv.vc_type = CONV_NONE;
4414 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4415 && vimconv.vc_type != CONV_NONE)
4416 {
4417 res = string_convert(&vimconv, data, NULL);
4418 if (res == NULL)
4419 res = data;
4420 else
4421 *tofree = res;
4422 }
4423 convert_setup(&vimconv, NULL, NULL);
4424 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004425 return res;
4426}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004427#endif