blob: db8202ea7d3e6115ded2c0e3599bd6c2a07241a8 [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>
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010016# 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 Moolenaar4ba37b52019-12-04 21:57:43 +010026// 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
Bram Moolenaarc013cb62005-07-24 21:18:31 +000032
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 +020037static void early_arg_scan(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010038#ifndef NO_VIM_MAIN
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static void usage(void);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010040static void parse_command_name(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010041static void command_line_scan(mparm_T *parmp);
42static void check_tty(mparm_T *parmp);
43static void read_stdin(void);
44static void create_windows(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010046static void exe_pre_commands(mparm_T *parmp);
47static void exe_commands(mparm_T *parmp);
48static void source_startup_scripts(mparm_T *parmp);
49static void main_start_gui(void);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010050static void check_swap_exists_action(void);
Bram Moolenaar08cab962017-03-04 14:37:18 +010051# ifdef FEAT_EVAL
52static void set_progpath(char_u *argv0);
53# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000054#endif
55
56
Bram Moolenaarb4210b32004-06-13 14:51:16 +000057/*
58 * Different types of error messages.
59 */
60static char *(main_errors[]) =
61{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000062 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000063#define ME_UNKNOWN_OPTION 0
64 N_("Too many edit arguments"),
65#define ME_TOO_MANY_ARGS 1
66 N_("Argument missing after"),
67#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +000068 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000069#define ME_GARBAGE 3
70 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
71#define ME_EXTRA_CMD 4
72 N_("Invalid argument for"),
73#define ME_INVALID_ARG 5
74};
75
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010076#ifndef PROTO // don't want a prototype for main()
Bram Moolenaara6044292017-04-02 18:19:53 +020077
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010078// Various parameters passed between main() and other functions.
Bram Moolenaara6044292017-04-02 18:19:53 +020079static mparm_T params;
80
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010081#ifndef NO_VIM_MAIN // skip this for unittests
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020082
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010083static char_u *start_dir = NULL; // current working dir on startup
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020084
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +020085static int has_dash_c_arg = FALSE;
86
Bram Moolenaarafde13b2019-04-28 19:46:49 +020087# ifdef VIMDLL
88__declspec(dllexport)
89# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000090 int
Bram Moolenaar796cc422019-04-03 20:31:00 +020091# ifdef MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +000092VimMain
93# else
94main
95# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +010096(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +000097{
Bram Moolenaar77780b62018-03-01 23:10:45 +010098#if defined(STARTUPTIME) || defined(CLEAN_RUNTIMEPATH)
Bram Moolenaar3f269672009-11-03 11:11:11 +000099 int i;
100#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000101
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000102 /*
103 * Do any system-specific initialisations. These can NOT use IObuff or
104 * NameBuff. Thus emsg2() cannot be called!
105 */
106 mch_early_init();
107
Bram Moolenaar4f974752019-02-17 17:44:42 +0100108#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +0200109 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200110 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200111 * convert when 'encoding' changes. Get the unexpanded arguments.
112 */
113 argc = get_cmd_argsW(&argv);
114#endif
115
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100116 // Many variables are in "params" so that we can pass them to invoked
117 // functions without a lot of arguments. "argc" and "argv" are also
118 // copied, so that they can be changed.
Bram Moolenaara80faa82020-04-12 19:37:17 +0200119 CLEAR_FIELD(params);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000120 params.argc = argc;
121 params.argv = argv;
122 params.want_full_screen = TRUE;
123#ifdef FEAT_EVAL
124 params.use_debug_break_level = -1;
125#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000126 params.window_count = -1;
Bram Moolenaar58d98232005-07-23 22:25:46 +0000127
Bram Moolenaar99685e62013-05-11 13:56:18 +0200128#ifdef FEAT_RUBY
129 {
130 int ruby_stack_start;
131 vim_ruby_init((void *)&ruby_stack_start);
132 }
133#endif
134
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000135#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000136 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000137#endif
138
139#ifdef MEM_PROFILE
140 atexit(vim_mem_profile_dump);
141#endif
142
143#ifdef STARTUPTIME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100144 // Need to find "--startuptime" before actually parsing arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100145 for (i = 1; i < argc - 1; ++i)
146 if (STRICMP(argv[i], "--startuptime") == 0)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000147 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000148 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000149 TIME_MSG("--- VIM STARTING ---");
150 break;
151 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000152#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000153 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000154
Bram Moolenaar07268702018-03-01 21:57:32 +0100155#ifdef CLEAN_RUNTIMEPATH
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100156 // Need to find "--clean" before actually parsing arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100157 for (i = 1; i < argc; ++i)
158 if (STRICMP(argv[i], "--clean") == 0)
159 {
160 params.clean = TRUE;
161 break;
162 }
163#endif
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200164#ifdef MSWIN
Bram Moolenaar4a228972021-05-01 22:41:39 +0200165 // Need to find "-register" and "-unregister" before loading any libraries.
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200166 for (i = 1; i < argc; ++i)
Bram Moolenaar4a228972021-05-01 22:41:39 +0200167 if ((STRICMP(argv[i] + 1, "register") == 0
168 || STRICMP(argv[i] + 1, "unregister") == 0)
169 && (argv[i][0] == '-' || argv[i][0] == '/'))
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200170 {
171 found_register_arg = TRUE;
172 break;
173 }
174#endif
175
176 /*
177 * Various initialisations shared with tests.
178 */
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200179 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000180
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200181#ifdef VIMDLL
182 // Check if the current executable file is for the GUI subsystem.
183 gui.starting = mch_is_gui_executable();
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +0200184#elif defined(FEAT_GUI_MSWIN)
185 gui.starting = TRUE;
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200186#endif
187
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000188#ifdef FEAT_CLIENTSERVER
189 /*
190 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000191 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000192 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000193 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000194#endif
195
196 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000197 * Figure out the way to work from the command name argv[0].
198 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000199 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000200 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000201
202 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000203 * Process the command line arguments. File names are put in the global
204 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000205 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000206 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000207 TIME_MSG("parsing arguments");
208
209 /*
210 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000211 * there is no terminal version, and on Windows we can't fork one off with
212 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000213 */
214#ifdef ALWAYS_USE_GUI
215 gui.starting = TRUE;
216#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000217# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000218 /*
219 * Check if the GUI can be started. Reset gui.starting if not.
220 * Don't know about other systems, stay on the safe side and don't check.
221 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000222 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000223 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000224 if (gui_init_check() == FAIL)
225 {
226 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000227
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100228 // When running "evim" or "gvim -y" we need the menus, exit if we
229 // don't have them.
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000230 if (params.evim_mode)
231 mch_exit(1);
232 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000233 }
234# endif
235#endif
236
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000237 if (GARGCOUNT > 0)
238 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100239#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000240 /*
241 * Expand wildcards in file names.
242 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000243 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000244 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200245 start_dir = alloc(MAXPATHL);
246 if (start_dir != NULL)
247 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100248 // Temporarily add '(' and ')' to 'isfname'. These are valid
249 // filename characters but are excluded from 'isfname' to make
250 // "gf" work on a file name in parenthesis (e.g.: see vim.h).
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000251 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000252 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000253 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200254 if (start_dir != NULL)
255 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000256 }
257#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200258 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000259 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000260
Bram Moolenaar4f974752019-02-17 17:44:42 +0100261#ifdef MSWIN
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000262 {
263 extern void set_alist_count(void);
264
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100265 // Remember the number of entries in the argument list. If it changes
266 // we don't react on setting 'encoding'.
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000267 set_alist_count();
268 }
269#endif
270
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000271#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000272 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000273 {
274 /*
275 * If there is one filename, fully qualified, we have very probably
276 * been invoked from explorer, so change to the file's directory.
277 * Hint: to avoid this when typing a command use a forward slash.
278 * If the cd fails, it doesn't matter.
279 */
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100280 (void)vim_chdirfile(params.fname, "drop");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200281 if (start_dir != NULL)
282 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000283 }
284#endif
285 TIME_MSG("expanding arguments");
286
287#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000288 if (params.diff_mode && params.window_count == -1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100289 params.window_count = 0; // open up to 3 windows
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000290#endif
291
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100292 // Don't redraw until much later.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000293 ++RedrawingDisabled;
294
295 /*
296 * When listing swap file names, don't do cursor positioning et. al.
297 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200298 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000299 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000300
301 /*
302 * When certain to start the GUI, don't check capabilities of terminal.
303 * For GTK we can't be sure, but when started from the desktop it doesn't
304 * make sense to try using a terminal.
305 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200306#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
307 || defined(VIMDLL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000308 if (gui.starting
309# ifdef FEAT_GUI_GTK
310 && !isatty(2)
311# endif
312 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000313 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000314#endif
315
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000316 /*
317 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100318 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000319 * Note that we may use mch_exit() before mch_init()!
320 */
321 mch_init();
322 TIME_MSG("shell init");
323
324#ifdef USE_XSMP
325 /*
326 * For want of anywhere else to do it, try to connect to xsmp here.
327 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
328 * Hijacking -X 'no X connection' to also disable XSMP connection as that
329 * has a similar delay upon failure.
330 * Only try if SESSION_MANAGER is set to something non-null.
331 */
332 if (!x_no_connect)
333 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000334 char *p = getenv("SESSION_MANAGER");
335
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000336 if (p != NULL && *p != NUL)
337 {
338 xsmp_init();
339 TIME_MSG("xsmp init");
340 }
341 }
342#endif
343
344 /*
345 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000346 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000347 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000348
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100349#ifdef _IOLBF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100350 // Ensure output works usefully without a tty: buffer lines instead of
351 // fully buffered.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100352 if (silent_mode)
353 setvbuf(stdout, NULL, _IOLBF, 0);
354#endif
355
Bram Moolenaar9404a182019-05-03 22:25:40 +0200356 // This message comes before term inits, but after setting "silent_mode"
357 // when the input is not a tty. Omit the message with --not-a-term.
358 if (GARGCOUNT > 1 && !silent_mode && !is_not_a_term())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000359 printf(_("%d files to edit\n"), GARGCOUNT);
360
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000361 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000362 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100363 termcapinit(params.term); // set terminal name and get terminal
364 // capabilities (will set full_screen)
365 screen_start(); // don't know where cursor is now
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000366 TIME_MSG("Termcap init");
367 }
368
369 /*
370 * Set the default values for the options that use Rows and Columns.
371 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100372 ui_get_shellsize(); // inits Rows and Columns
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000373 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000374#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100375 // Set the 'diff' option now, so that it can be checked for in a .vimrc
376 // file. There is no buffer yet though.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000377 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000378 diff_win_options(firstwin, FALSE);
379#endif
380
381 cmdline_row = Rows - p_ch;
382 msg_row = cmdline_row;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100383 screenalloc(FALSE); // allocate screen buffers
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000384 set_init_2();
385 TIME_MSG("inits 2");
386
387 msg_scroll = TRUE;
388 no_wait_return = TRUE;
389
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100390 init_mappings(); // set up initial mappings
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000391
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100392 init_highlight(TRUE, FALSE); // set the default highlight groups
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000393 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000394
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200395#if defined(FEAT_TERMRESPONSE)
396 init_term_props(TRUE);
397#endif
398
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000399#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100400 // Set the break level after the terminal is initialized.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000401 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000402#endif
403
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100404 // Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
405 // Allows for setting 'loadplugins' there.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200406 if (params.use_vimrc != NULL
407 && (STRCMP(params.use_vimrc, "NONE") == 0
408 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
409 p_lpl = FALSE;
410
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100411 // Execute --cmd arguments.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200412 exe_pre_commands(&params);
413
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100414 // Source startup scripts.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200415 source_startup_scripts(&params);
416
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100417#ifdef FEAT_MZSCHEME
418 /*
419 * Newer version of MzScheme (Racket) require earlier (trampolined)
420 * initialisation via scheme_main_setup.
421 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200422 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200423 * Do source startup scripts, so that 'mzschemedll' can be set.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100424 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200425 return mzscheme_main();
426#else
427 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100428#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200429}
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100430#endif // NO_VIM_MAIN
431#endif // PROTO
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100432
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200433/*
434 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
435 * things simple.
436 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
437 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100438 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200439vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100440{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100441#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000442#ifdef FEAT_EVAL
443 /*
444 * Read all the plugin files.
445 * Only when compiled with +eval, since most plugins need it.
446 */
447 if (p_lpl)
448 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200449 char_u *rtp_copy = NULL;
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100450 char_u *plugin_pattern = (char_u *)
451# if defined(VMS) || defined(AMIGA) // VMS and Amiga don't handle the "**".
452 "plugin/*.vim"
453# else
454 "plugin/**/*.vim"
455# endif
456 ;
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200457
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100458 // First add all package directories to 'runtimepath', so that their
459 // autoload directories can be found. Only if not done already with a
460 // :packloadall command.
461 // Make a copy of 'runtimepath', so that source_runtime does not use
462 // the pack directories.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200463 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200464 {
465 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200466 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200467 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200468
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100469 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy, plugin_pattern,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100470 DIP_ALL | DIP_NOAFTER, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000471 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200472 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100473
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100474 // Only source "start" packages if not done already with a :packloadall
475 // command.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200476 if (!did_source_packages)
477 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100478 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200479
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100480 source_runtime(plugin_pattern, DIP_ALL | DIP_AFTER);
Bram Moolenaar66459b72016-08-06 19:01:55 +0200481 TIME_MSG("loading after plugins");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000482 }
483#endif
484
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000485#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100486 // Decide about window layout for diff mode after reading vimrc.
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000487 if (params.diff_mode && params.window_layout == 0)
488 {
489 if (diffopt_horizontal())
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100490 params.window_layout = WIN_HOR; // use horizontal split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000491 else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100492 params.window_layout = WIN_VER; // use vertical split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000493 }
494#endif
495
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000496 /*
497 * Recovery mode without a file name: List swap files.
498 * This uses the 'dir' option, therefore it must be after the
499 * initializations.
500 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200501 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000502 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200503 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000504 mch_exit(0);
505 }
506
507 /*
508 * Set a few option defaults after reading .vimrc files:
509 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
510 */
511 set_init_3();
512 TIME_MSG("inits 3");
513
514 /*
515 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
516 * Note that this overrides anything from a vimrc file.
517 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000518 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000519 p_uc = 0;
520
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000521#ifdef FEAT_GUI
522 if (gui.starting)
523 {
Bram Moolenaarc7226682019-08-17 16:33:23 +0200524# if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100525 // When something caused a message from a vimrc script, need to output
526 // an extra newline before the shell prompt.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000527 if (did_emsg || msg_didout)
528 putchar('\n');
Bram Moolenaarc7226682019-08-17 16:33:23 +0200529# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000530
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100531 gui_start(NULL); // will set full_screen to TRUE
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000532 TIME_MSG("starting GUI");
533
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100534 // When running "evim" or "gvim -y" we need the menus, exit if we
535 // don't have them.
Bram Moolenaar58d98232005-07-23 22:25:46 +0000536 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000537 mch_exit(1);
538 }
539#endif
540
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000541#ifdef FEAT_VIMINFO
542 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000543 * Read in registers, history etc, but not marks, from the viminfo file.
544 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000545 */
546 if (*p_viminfo != NUL)
547 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000548 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000549 TIME_MSG("reading viminfo");
550 }
551#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100552#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100553 // It's better to make v:oldfiles an empty list than NULL.
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100554 if (get_vim_var_list(VV_OLDFILES) == NULL)
555 set_vim_var_list(VV_OLDFILES, list_alloc());
556#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000557
558#ifdef FEAT_QUICKFIX
559 /*
560 * "-q errorfile": Load the error file now.
561 * If the error file can't be read, exit before doing anything else.
562 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000563 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000564 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100565 char_u *enc = NULL;
566
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100567 enc = p_menc;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000568 if (params.use_ef != NULL)
569 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000570 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200571 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100572 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000573 {
574 out_char('\n');
575 mch_exit(3);
576 }
577 TIME_MSG("reading errorfile");
578 }
579#endif
580
581 /*
582 * Start putting things on the screen.
583 * Scroll screen down before drawing over it
584 * Clear screen now, so file message will not be cleared.
585 */
586 starting = NO_BUFFERS;
587 no_wait_return = FALSE;
588 if (!exmode_active)
589 msg_scroll = FALSE;
590
591#ifdef FEAT_GUI
592 /*
593 * This seems to be required to make callbacks to be called now, instead
594 * of after things have been put on the screen, which then may be deleted
595 * when getting a resize callback.
596 * For the Mac this handles putting files dropped on the Vim icon to
597 * global_alist.
598 */
599 if (gui.in_use)
600 {
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100601 gui_wait_for_chars(50L, typebuf.tb_change_cnt);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000602 TIME_MSG("GUI delay");
603 }
604#endif
605
606#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
607 qnx_clip_init();
608#endif
609
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200610#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
611 clip_init(TRUE);
612#endif
613
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000614#ifdef FEAT_XCLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100615 // Start using the X clipboard, unless the GUI was started.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000616# ifdef FEAT_GUI
617 if (!gui.in_use)
618# endif
619 {
620 setup_term_clip();
621 TIME_MSG("setup clipboard");
622 }
623#endif
624
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000625#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100626 // Prepare for being a Vim server.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000627 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000628#endif
629
630 /*
631 * If "-" argument given: Read file from stdin.
632 * Do this before starting Raw mode, because it may change things that the
633 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
634 * are the same terminal: "cat | vim -".
635 * Using autocommands here may cause trouble...
636 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000637 if (params.edit_type == EDIT_STDIN && !recoverymode)
638 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000639
640#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100641 // When switching screens and something caused a message from a vimrc
642 // script, need to output an extra newline on exit.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000643 if ((did_emsg || msg_didout) && *T_TI != NUL)
644 newline_on_exit = TRUE;
645#endif
646
647 /*
648 * When done something that is not allowed or error message call
649 * wait_return. This must be done before starttermcap(), because it may
650 * switch to another screen. It must be done after settmode(TMODE_RAW),
651 * because we want to react on a single key stroke.
652 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000653 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000654 */
655 settmode(TMODE_RAW);
656 TIME_MSG("setting raw mode");
657
658 if (need_wait_return || msg_didany)
659 {
660 wait_return(TRUE);
661 TIME_MSG("waiting for return");
662 }
663
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100664 starttermcap(); // start termcap if not done by wait_return()
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000665 TIME_MSG("start termcap");
666
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200667 setmouse(); // may start using the mouse
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000668 if (scroll_region)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100669 scroll_region_reset(); // In case Rows changed
670 scroll_start(); // may scroll the screen to the right position
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000671
Bram Moolenaar980bab42018-08-07 22:42:53 +0200672#if defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar40385db2018-08-07 22:31:44 +0200673 term_push_title(SAVE_RESTORE_BOTH);
674#endif
675
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000676 /*
677 * Don't clear the screen when starting in Ex mode, unless using the GUI.
678 */
679 if (exmode_active
680#ifdef FEAT_GUI
681 && !gui.in_use
682#endif
683 )
684 must_redraw = CLEAR;
685 else
686 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100687 screenclear(); // clear screen
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000688 TIME_MSG("clearing screen");
689 }
690
691#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000692 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000693 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100694 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200695 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000696 TIME_MSG("getting crypt key");
697 }
698#endif
699
700 no_wait_return = TRUE;
701
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000702 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000703 * Create the requested number of windows and edit buffers in them.
704 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000705 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000706 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000707 TIME_MSG("opening buffers");
708
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000709#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100710 // clear v:swapcommand
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000711 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
712#endif
713
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100714 // Ex starts at last line of the file
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000715 if (exmode_active)
716 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
717
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000718 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
719 TIME_MSG("BufEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000720 setpcmark();
721
722#ifdef FEAT_QUICKFIX
723 /*
724 * When started with "-q errorfile" jump to first error now.
725 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000726 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000727 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000728 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000729 TIME_MSG("jump to first error");
730 }
731#endif
732
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000733 /*
734 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000735 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000736 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200737 edit_buffers(&params, start_dir);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200738 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000739
740#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000741 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000742 {
743 win_T *wp;
744
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100745 // set options in each window for "vimdiff".
Bram Moolenaar29323592016-07-24 22:04:11 +0200746 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000747 diff_win_options(wp, TRUE);
748 }
749#endif
750
751 /*
752 * Shorten any of the filenames, but only when absolute.
753 */
754 shorten_fnames(FALSE);
755
756 /*
757 * Need to jump to the tag before executing the '-c command'.
758 * Makes "vim -c '/return' -t main" work.
759 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000760 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000761 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000762 swap_exists_did_quit = FALSE;
Bram Moolenaar146522e2005-12-16 21:55:46 +0000763
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000764 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000765 do_cmdline_cmd(IObuff);
766 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000767
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100768 // If the user doesn't want to edit the file then we quit here.
Bram Moolenaar146522e2005-12-16 21:55:46 +0000769 if (swap_exists_did_quit)
770 getout(1);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000771 }
772
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100773 // Execute any "+", "-c" and "-S" arguments.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000774 if (params.n_commands > 0)
775 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000776
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100777 // Must come before the may_req_ calls.
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200778 starting = 0;
779
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100780#if defined(FEAT_TERMRESPONSE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100781 // Must be done before redrawing, puts a few characters on the screen.
Bram Moolenaara45551a2020-06-09 15:57:37 +0200782 check_terminal_behavior();
Bram Moolenaar976787d2017-06-04 15:45:50 +0200783#endif
784
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000785 RedrawingDisabled = 0;
786 redraw_all_later(NOT_VALID);
787 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000788
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100789 // 'autochdir' has been postponed
Bram Moolenaar6f470022018-04-10 18:47:20 +0200790 DO_AUTOCHDIR;
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200791
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000792#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100793 // Requesting the termresponse is postponed until here, so that a "-c q"
794 // argument doesn't make it appear in the shell Vim was started from.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000795 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200796
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200797 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000798#endif
799
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100800 // start in insert mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000801 if (p_im)
802 need_start_insertmode = TRUE;
803
Bram Moolenaar14735512016-03-26 21:00:08 +0100804#ifdef FEAT_EVAL
805 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
806#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000807 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
808 TIME_MSG("VimEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000809
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200810#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100811 // Adjust default register name for "unnamed" in 'clipboard'. Can only be
812 // done after the clipboard is available and all initial commands that may
813 // modify the 'clipboard' setting have run; i.e. just before entering the
814 // main loop.
Bram Moolenaar439c0362020-06-06 15:58:03 +0200815 reset_reg_var();
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200816#endif
817
Bram Moolenaar8a3bb562018-03-04 20:14:14 +0100818#if defined(FEAT_DIFF)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100819 // When a startup script or session file setup for diff'ing and
820 // scrollbind, sync the scrollbind now.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000821 if (curwin->w_p_diff && curwin->w_p_scb)
822 {
823 update_topline();
824 check_scrollbind((linenr_T)0, 0L);
825 TIME_MSG("diff scrollbinding");
826 }
827#endif
828
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200829#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
830# ifdef VIMDLL
831 if (!gui.in_use)
832# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100833 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000834#endif
835
Bram Moolenaar4033c552017-09-16 20:54:51 +0200836#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100837 // When tab pages were created, may need to update the tab pages line and
838 // scrollbars. This is skipped while creating them.
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000839 if (first_tabpage->tp_next != NULL)
840 {
841 out_flush();
842 gui_init_which_components(NULL);
843 gui_update_scrollbars(TRUE);
844 }
845 need_mouse_correct = TRUE;
846#endif
847
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100848 // If ":startinsert" command used, stuff a dummy command to be able to
849 // call normal_cmd(), which will then start Insert mode.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000850 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000851 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000852
853#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200854 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200855 {
856# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200857# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar4f974752019-02-17 17:44:42 +0100858 && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200859 if (gui.in_use)
860 {
861 mch_errmsg(_("netbeans is not supported with this GUI\n"));
862 mch_exit(2);
863 }
864# endif
865# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100866 // Tell the client that it can start sending commands.
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200867 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200868 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000869#endif
870
Bram Moolenaare7ddf4e2020-02-03 21:29:30 +0100871 // Redraw at least once, also when 'lazyredraw' is set, to make sure the
872 // window title gets updated.
873 do_redraw = TRUE;
874
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000875 TIME_MSG("before starting main loop");
876
877 /*
878 * Call the main command loop. This never returns.
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200879 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000880 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000881
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100882#endif // NO_VIM_MAIN
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200883
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000884 return 0;
885}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000886
887/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200888 * Initialisation shared by main() and some tests.
889 */
890 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200891common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200892{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100893 estack_init();
Bram Moolenaar438d1762018-09-30 17:11:48 +0200894 cmdline_init();
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200895
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100896 (void)mb_init(); // init mb_bytelen_tab[] to ones
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200897#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100898 eval_init(); // init global variables
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200899#endif
900
901#ifdef __QNXNTO__
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100902 qnx_init(); // PhAttach() for clipboard, (and gui)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200903#endif
904
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100905 // Init the table of Normal mode commands.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200906 init_normal_cmds();
907
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200908 /*
909 * Allocate space for the generic buffers (needed for set_init_1() and
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100910 * emsg()).
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200911 */
912 if ((IObuff = alloc(IOSIZE)) == NULL
913 || (NameBuff = alloc(MAXPATHL)) == NULL)
914 mch_exit(0);
915 TIME_MSG("Allocated generic buffers");
916
917#ifdef NBDEBUG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100918 // Wait a moment for debugging NetBeans. Must be after allocating
919 // NameBuff.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200920 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
921 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
922 TIME_MSG("NetBeans debug wait");
923#endif
924
925#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
926 /*
927 * Setup to use the current locale (for ctype() and many other things).
928 * NOTE: Translated messages with encodings other than latin1 will not
929 * work until set_init_1() has been called!
930 */
931 init_locale();
932 TIME_MSG("locale set");
933#endif
934
935#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100936 gui.dofork = TRUE; // default is to use fork()
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200937#endif
938
939 /*
940 * Do a first scan of the arguments in "argv[]":
941 * -display or --display
942 * --server...
943 * --socketid
944 * --windowid
945 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200946 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200947
Bram Moolenaard0573012017-10-28 21:11:06 +0200948#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100949 // Prepare for possibly starting GUI sometime
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200950 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200951 TIME_MSG("GUI prepared");
952#endif
953
954#ifdef FEAT_CLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100955 clip_init(FALSE); // Initialise clipboard stuff
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200956 TIME_MSG("clipboard setup");
957#endif
958
959 /*
960 * Check if we have an interactive window.
961 * On the Amiga: If there is no window, we open one with a newcli command
962 * (needed for :! to * work). mch_check_win() will also handle the -d or
963 * -dev argument.
964 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100965 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200966 TIME_MSG("window checked");
967
968 /*
969 * Allocate the first window and buffer.
970 * Can't do anything without it, exit when it fails.
971 */
972 if (win_alloc_first() == FAIL)
973 mch_exit(0);
974
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100975 init_yank(); // init yank buffers
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200976
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100977 alist_init(&global_alist); // Init the argument list to empty.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200978 global_alist.id = 0;
979
980 /*
981 * Set the default values for the options.
982 * NOTE: Non-latin1 translated messages are working only after this,
983 * because this is where "has_mbyte" will be set, which is used by
984 * msg_outtrans_len_attr().
985 * First find out the home directory, needed to expand "~" in options.
986 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100987 init_homedir(); // find real value of $HOME
Bram Moolenaar07268702018-03-01 21:57:32 +0100988 set_init_1(paramp->clean);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200989 TIME_MSG("inits 1");
990
991#ifdef FEAT_EVAL
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100992 // set v:lang and v:ctype
993 set_lang_var();
994
995 // set v:argv
996 set_argv_var(paramp->argv, paramp->argc);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200997#endif
Bram Moolenaar6436cd82018-12-27 00:28:33 +0100998
999#ifdef FEAT_SIGNS
1000 init_signs();
1001#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001002}
1003
1004/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001005 * Return TRUE when the --not-a-term argument was found.
1006 */
1007 int
1008is_not_a_term()
1009{
1010 return params.not_a_term;
1011}
1012
Bram Moolenaar7007e312021-03-27 12:11:33 +01001013/*
1014 * Return TRUE when the --not-a-term argument was found or the GUI is in use.
1015 */
1016 static int
1017is_not_a_term_or_gui()
1018{
1019 return params.not_a_term
1020#ifdef FEAT_GUI
1021 || gui.in_use
1022#endif
1023 ;
1024}
1025
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001026
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001027// When TRUE in a safe state when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001028static int was_safe = FALSE;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001029static oparg_T *current_oap = NULL;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001030
1031/*
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001032 * Return TRUE if an operator was started but not finished yet.
1033 * Includes typing a count or a register name.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001034 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001035 int
1036op_pending(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001037{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001038 return !(current_oap != NULL
1039 && !finish_op
1040 && current_oap->prev_opcount == 0
1041 && current_oap->prev_count0 == 0
1042 && current_oap->op_type == OP_NOP
1043 && current_oap->regname == NUL);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001044}
1045
1046/*
Bram Moolenaard103ee72019-09-18 21:15:31 +02001047 * Return whether currently it is safe, assuming it was safe before (high level
1048 * state didn't change).
1049 */
1050 static int
1051is_safe_now(void)
1052{
1053 return stuff_empty()
1054 && typebuf.tb_len == 0
1055 && scriptin[curscript] == NULL
1056 && !global_busy;
1057}
1058
1059/*
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001060 * Trigger SafeState if currently in s safe state, that is "safe" is TRUE and
1061 * there is no typeahead.
1062 */
1063 void
1064may_trigger_safestate(int safe)
1065{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001066 int is_safe = safe && is_safe_now();
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001067
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001068#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001069 if (was_safe != is_safe)
1070 // Only log when the state changes, otherwise it happens at nearly
1071 // every key stroke.
Bram Moolenaard103ee72019-09-18 21:15:31 +02001072 ch_log(NULL, is_safe ? "SafeState: Start triggering"
1073 : "SafeState: Stop triggering");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001074#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001075 if (is_safe)
1076 apply_autocmds(EVENT_SAFESTATE, NULL, NULL, FALSE, curbuf);
1077 was_safe = is_safe;
1078}
1079
1080/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001081 * Something changed which causes the state possibly to be unsafe, e.g. a
1082 * character was typed. It will remain unsafe until the next call to
1083 * may_trigger_safestate().
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001084 */
1085 void
Bram Moolenaard103ee72019-09-18 21:15:31 +02001086state_no_longer_safe(char *reason UNUSED)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001087{
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001088#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001089 if (was_safe)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001090 ch_log(NULL, "SafeState: reset: %s", reason);
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001091#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001092 was_safe = FALSE;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001093}
1094
Bram Moolenaard103ee72019-09-18 21:15:31 +02001095 int
1096get_was_safe_state(void)
1097{
1098 return was_safe;
1099}
1100
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001101/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001102 * Invoked when leaving code that invokes callbacks. Then trigger
1103 * SafeStateAgain, if it was safe when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001104 */
1105 void
Bram Moolenaar37d18072019-09-17 20:28:38 +02001106may_trigger_safestateagain(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001107{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001108 if (!was_safe)
1109 {
1110 // If the safe state was reset in state_no_longer_safe(), e.g. because
1111 // of calling feedkeys(), we check if it's now safe again (all keys
1112 // were consumed).
1113 was_safe = is_safe_now();
1114#ifdef FEAT_JOB_CHANNEL
1115 if (was_safe)
1116 ch_log(NULL, "SafeState: undo reset");
1117#endif
1118 }
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001119 if (was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02001120 {
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001121#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar101e9922019-09-25 21:43:11 +02001122 // Only do this message when another message was given, otherwise we
1123 // get lots of them.
1124 if ((did_repeated_msg & REPEATED_MSG_SAFESTATE) == 0)
1125 {
1126 int did = did_repeated_msg;
1127
1128 ch_log(NULL,
1129 "SafeState: back to waiting, triggering SafeStateAgain");
1130 did_repeated_msg = did | REPEATED_MSG_SAFESTATE;
1131 }
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001132#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001133 apply_autocmds(EVENT_SAFESTATEAGAIN, NULL, NULL, FALSE, curbuf);
Bram Moolenaar37d18072019-09-17 20:28:38 +02001134 }
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001135#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001136 else
Bram Moolenaard103ee72019-09-18 21:15:31 +02001137 ch_log(NULL,
1138 "SafeState: back to waiting, not triggering SafeStateAgain");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001139#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001140}
1141
1142
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001143/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001144 * Main loop: Execute Normal mode commands until exiting Vim.
1145 * Also used to handle commands in the command-line window, until the window
1146 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147 * Also used to handle ":visual" command after ":global": execute Normal mode
1148 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001149 */
1150 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001151main_loop(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001152 int cmdwin, // TRUE when working in the command-line window
1153 int noexmode) // TRUE when return on entering Ex mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001154{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001155 oparg_T oa; // operator arguments
1156 oparg_T *prev_oap; // operator arguments
1157 volatile int previous_got_int = FALSE; // "got_int" was TRUE
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001158#ifdef FEAT_CONCEAL
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001159 // these are static to avoid a compiler warning
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001160 static linenr_T conceal_old_cursor_line = 0;
1161 static linenr_T conceal_new_cursor_line = 0;
1162 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001163#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001164
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001165 prev_oap = current_oap;
1166 current_oap = &oa;
1167
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001168#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001169 // Setup to catch a terminating error from the X server. Just ignore
1170 // it, restore the state and continue. This might not always work
1171 // properly, but at least we don't exit unexpectedly when the X server
1172 // exits while Vim is running in a console.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001173 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001174 {
1175 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001176 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001177 got_int = TRUE;
1178 need_wait_return = FALSE;
1179 global_busy = FALSE;
1180 exmode_active = 0;
1181 skip_redraw = FALSE;
1182 RedrawingDisabled = 0;
1183 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001184 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001185# ifdef FEAT_EVAL
1186 emsg_skip = 0;
1187# endif
1188 emsg_off = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001189 setmouse();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001190 settmode(TMODE_RAW);
1191 starttermcap();
1192 scroll_start();
1193 redraw_later_clear();
1194 }
1195#endif
1196
1197 clear_oparg(&oa);
1198 while (!cmdwin
1199#ifdef FEAT_CMDWIN
1200 || cmdwin_result == 0
1201#endif
1202 )
1203 {
1204 if (stuff_empty())
1205 {
1206 did_check_timestamps = FALSE;
1207 if (need_check_timestamps)
1208 check_timestamps(FALSE);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001209 if (need_wait_return) // if wait_return still needed ...
1210 wait_return(FALSE); // ... call it now
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001211 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001212 {
1213 need_start_insertmode = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001214 stuffReadbuff((char_u *)"i"); // start insert mode next
1215 // skip the fileinfo message now, because it would be shown
1216 // after insert mode finishes!
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001217 need_fileinfo = FALSE;
1218 }
1219 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001220
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001221 // Reset "got_int" now that we got back to the main loop. Except when
1222 // inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1223 // the ":g" command.
1224 // For ":g/pat/vi" we reset "got_int" when used once. When used
1225 // a second time we go back to Ex mode and abort the ":g" command.
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001226 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001227 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001228 if (noexmode && global_busy && !exmode_active && previous_got_int)
1229 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001230 // Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1231 // used and keep "got_int" set, so that it aborts ":g".
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001232 exmode_active = EXMODE_NORMAL;
1233 State = NORMAL;
1234 }
1235 else if (!global_busy || !exmode_active)
1236 {
1237 if (!quit_more)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001238 (void)vgetc(); // flush all buffers
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001239 got_int = FALSE;
1240 }
1241 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001242 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001243 else
1244 previous_got_int = FALSE;
1245
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001246 if (!exmode_active)
1247 msg_scroll = FALSE;
1248 quit_more = FALSE;
1249
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001250 // it's not safe unless may_trigger_safestate_main() is called
1251 was_safe = FALSE;
1252
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001253 /*
1254 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1255 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1256 * update cursor and redraw.
1257 */
1258 if (skip_redraw || exmode_active)
1259 skip_redraw = FALSE;
1260 else if (do_redraw || stuff_empty())
1261 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001262#ifdef FEAT_GUI
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001263 // If ui_breakcheck() was used a resize may have been postponed.
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001264 gui_may_resize_shell();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001265#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001266#ifdef HAVE_DROP_FILE
1267 // If files were dropped while text was locked or the curbuf was
1268 // locked, this would be a good time to handle the drop.
1269 handle_any_postponed_drop();
1270#endif
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001271#ifdef FEAT_CONCEAL
1272 if (curwin->w_p_cole == 0)
1273 conceal_update_lines = FALSE;
1274#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001275
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001276 // Trigger CursorMoved if the cursor moved.
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001277 if (!finish_op && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001278 has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001279#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001280 || popup_visible
1281#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001282#ifdef FEAT_CONCEAL
1283 || curwin->w_p_cole > 0
1284#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001285 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001286 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001287 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001288 if (has_cursormoved())
1289 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1290 FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001291#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001292 if (popup_visible)
1293 popup_check_cursor_pos();
1294#endif
1295#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001296 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001297 {
1298 conceal_old_cursor_line = last_cursormoved.lnum;
1299 conceal_new_cursor_line = curwin->w_cursor.lnum;
1300 conceal_update_lines = TRUE;
1301 }
Bram Moolenaar3397f742019-06-02 18:40:06 +02001302#endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001303 last_cursormoved = curwin->w_cursor;
1304 }
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001305
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001306#if defined(FEAT_CONCEAL)
1307 if (conceal_update_lines
1308 && (conceal_old_cursor_line != conceal_new_cursor_line
1309 || conceal_cursor_line(curwin)
1310 || need_cursor_line_redraw))
1311 {
1312 if (conceal_old_cursor_line != conceal_new_cursor_line
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001313 && conceal_old_cursor_line != 0
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001314 && conceal_old_cursor_line
1315 <= curbuf->b_ml.ml_line_count)
1316 redrawWinline(curwin, conceal_old_cursor_line);
1317 redrawWinline(curwin, conceal_new_cursor_line);
1318 curwin->w_valid &= ~VALID_CROW;
1319 need_cursor_line_redraw = FALSE;
1320 }
1321#endif
1322
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001323 // Trigger TextChanged if b:changedtick differs.
Bram Moolenaar186628f2013-03-19 13:33:23 +01001324 if (!finish_op && has_textchanged()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001325 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001326 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001327 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL, FALSE, curbuf);
1328 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001329 }
Bram Moolenaar186628f2013-03-19 13:33:23 +01001330
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001331 // If nothing is pending and we are going to wait for the user to
1332 // type a character, trigger SafeState.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001333 may_trigger_safestate(!op_pending() && restart_edit == 0);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001334
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01001335#if defined(FEAT_DIFF)
Bram Moolenaare3521d92018-09-16 14:10:31 +02001336 // Updating diffs from changed() does not always work properly,
1337 // esp. updating folds. Do an update just before redrawing if
1338 // needed.
1339 if (curtab->tp_diff_update || curtab->tp_diff_invalid)
1340 {
1341 ex_diffupdate(NULL);
1342 curtab->tp_diff_update = FALSE;
1343 }
1344
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001345 // Scroll-binding for diff mode may have been postponed until
1346 // here. Avoids doing it for every change.
Bram Moolenaar33aec762006-01-22 23:30:12 +00001347 if (diff_need_scrollbind)
1348 {
1349 check_scrollbind((linenr_T)0, 0L);
1350 diff_need_scrollbind = FALSE;
1351 }
1352#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001353#if defined(FEAT_FOLDING)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001354 // Include a closed fold completely in the Visual area.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001355 foldAdjustVisual();
1356#endif
1357#ifdef FEAT_FOLDING
1358 /*
1359 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1360 * contain the cursor.
1361 * When 'foldopen' is "all", open the fold(s) under the cursor.
1362 * This may mark the window for redrawing.
1363 */
1364 if (hasAnyFolding(curwin) && !char_avail())
1365 {
1366 foldCheckClose();
1367 if (fdo_flags & FDO_ALL)
1368 foldOpenCursor();
1369 }
1370#endif
1371
1372 /*
1373 * Before redrawing, make sure w_topline is correct, and w_leftcol
1374 * if lines don't wrap, and w_skipcol if lines wrap.
1375 */
1376 update_topline();
1377 validate_cursor();
1378
Bram Moolenaar017ba072019-09-14 21:01:23 +02001379#ifdef FEAT_SYN_HL
Bram Moolenaar11a58af2019-10-24 22:32:31 +02001380 // Might need to update for 'cursorline'.
1381 // When 'cursorlineopt' is "screenline" need to redraw always.
1382 if (curwin->w_p_cul
1383 && (curwin->w_last_cursorline != curwin->w_cursor.lnum
1384 || (curwin->w_p_culopt_flags & CULOPT_SCRLINE))
1385 && !char_avail())
1386 redraw_later(VALID);
Bram Moolenaar017ba072019-09-14 21:01:23 +02001387#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001388 if (VIsual_active)
Bram Moolenaar017ba072019-09-14 21:01:23 +02001389 update_curbuf(INVERTED); // update inverted part
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001390 else if (must_redraw)
Bram Moolenaara338adc2018-01-31 20:51:47 +01001391 {
Bram Moolenaar017ba072019-09-14 21:01:23 +02001392 mch_disable_flush(); // Stop issuing gui_mch_flush().
Bram Moolenaar11a58af2019-10-24 22:32:31 +02001393 update_screen(0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01001394 mch_enable_flush();
1395 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001396 else if (redraw_cmdline || clear_cmdline)
1397 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001398 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001399#ifdef FEAT_TITLE
1400 if (need_maketitle)
1401 maketitle();
1402#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001403#ifdef FEAT_VIMINFO
1404 curbuf->b_last_used = vim_time();
1405#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001406 // display message after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001407 if (keep_msg != NULL)
1408 {
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001409 char_u *p = vim_strsave(keep_msg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001410
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001411 if (p != NULL)
1412 {
1413 // msg_start() will set keep_msg to NULL, make a copy
1414 // first. Don't reset keep_msg, msg_attr_keep() uses it to
1415 // check for duplicates. Never put this message in
1416 // history.
1417 msg_hist_off = TRUE;
1418 msg_attr((char *)p, keep_msg_attr);
1419 msg_hist_off = FALSE;
1420 vim_free(p);
1421 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001422 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001423 if (need_fileinfo) // show file info after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001424 {
1425 fileinfo(FALSE, TRUE, FALSE);
1426 need_fileinfo = FALSE;
1427 }
1428
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001429 emsg_on_display = FALSE; // can delete error message now
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001430 did_emsg = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001431 msg_didany = FALSE; // reset lines_left in msg_start()
1432 may_clear_sb_text(); // clear scroll-back text on next msg
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001433 showruler(FALSE);
1434
1435 setcursor();
1436 cursor_on();
1437
1438 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001439
1440#ifdef STARTUPTIME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001441 // Now that we have drawn the first screen all the startup stuff
1442 // has been done, close any file for startup messages.
Bram Moolenaar3f269672009-11-03 11:11:11 +00001443 if (time_fd != NULL)
1444 {
1445 TIME_MSG("first screen update");
1446 TIME_MSG("--- VIM STARTED ---");
1447 fclose(time_fd);
1448 time_fd = NULL;
1449 }
1450#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001451 }
1452#ifdef FEAT_GUI
1453 if (need_mouse_correct)
1454 gui_mouse_correct();
1455#endif
1456
1457 /*
1458 * Update w_curswant if w_set_curswant has been set.
1459 * Postponed until here to avoid computing w_virtcol too often.
1460 */
1461 update_curswant();
1462
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001463#ifdef FEAT_EVAL
1464 /*
1465 * May perform garbage collection when waiting for a character, but
1466 * only at the very toplevel. Otherwise we may be using a List or
1467 * Dict internally somewhere.
1468 * "may_garbage_collect" is reset in vgetc() which is invoked through
1469 * do_exmode() and normal_cmd().
1470 */
1471 may_garbage_collect = (!cmdwin && !noexmode);
1472#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001473 /*
1474 * If we're invoked as ex, do a round of ex commands.
1475 * Otherwise, get and execute a normal mode command.
1476 */
1477 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001478 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001479 if (noexmode) // End of ":global/path/visual" commands
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001480 goto theend;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001481 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001482 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001483 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001484 {
1485#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001486 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001487 && oa.op_type == OP_NOP && oa.regname == NUL
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001488 && !VIsual_active
1489 && !skip_term_loop)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001490 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001491 // If terminal_loop() returns OK we got a key that is handled
1492 // in Normal model. With FAIL we first need to position the
1493 // cursor and the screen needs to be redrawn.
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001494 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001495 normal_cmd(&oa, TRUE);
1496 }
1497 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001498#endif
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001499 {
1500#ifdef FEAT_TERMINAL
1501 skip_term_loop = FALSE;
1502#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001503 normal_cmd(&oa, TRUE);
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001504 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02001505 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001506 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001507
1508theend:
1509 current_oap = prev_oap;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001510}
1511
1512
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001513#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001514/*
1515 * Exit, but leave behind swap files for modified buffers.
1516 */
1517 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001518getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001519{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001520# if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001521 // Ignore SIGHUP, because a dropped connection causes a read error, which
1522 // makes Vim exit and then handling SIGHUP causes various reentrance
1523 // problems.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001524 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001525# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001526
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001527 ml_close_notmod(); // close all not-modified buffers
1528 ml_sync_all(FALSE, FALSE); // preserve all swap files
1529 ml_close_all(FALSE); // close all memfiles, without deleting
1530 getout(exitval); // exit Vim properly
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001531}
1532#endif
1533
1534
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001535/*
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001536 * Exit properly. This is the only way to exit Vim after startup has
1537 * succeeded. We are certain to exit here, no way to abort it.
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001538 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001539 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001540getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001541{
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001542 exiting = TRUE;
Bram Moolenaar0bd052b2018-03-22 20:33:56 +01001543#if defined(FEAT_JOB_CHANNEL)
1544 ch_log(NULL, "Exiting...");
1545#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001546
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001547 // When running in Ex mode an error causes us to exit with a non-zero exit
1548 // code. POSIX requires this, although it's not 100% clear from the
1549 // standard.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001550 if (exmode_active)
1551 exitval += ex_exitval;
1552
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001553#ifdef FEAT_EVAL
1554 set_vim_var_type(VV_EXITING, VAR_NUMBER);
1555 set_vim_var_nr(VV_EXITING, exitval);
1556#endif
1557
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001558 // Position the cursor on the last screen line, below all the text
Bram Moolenaar7007e312021-03-27 12:11:33 +01001559 if (!is_not_a_term_or_gui())
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001560 windgoto((int)Rows - 1, 0);
1561
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001562#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001563 // Optionally print hashtable efficiency.
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001564 hash_debug_results();
1565#endif
1566
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001567#ifdef FEAT_GUI
1568 msg_didany = FALSE;
1569#endif
1570
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001571 if (v_dying <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001572 {
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001573 tabpage_T *tp;
1574 tabpage_T *next_tp;
1575 buf_T *buf;
1576 win_T *wp;
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001577 int unblock = 0;
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001578
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001579 // Trigger BufWinLeave for all windows, but only once per buffer.
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001580 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001581 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001582 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001583 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001584 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001585 if (wp->w_buffer == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001586 // Autocmd must have close the buffer already, skip.
Bram Moolenaar802418d2013-01-17 14:00:11 +01001587 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001588 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001589 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001590 {
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001591 bufref_T bufref;
1592
1593 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001594 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1595 buf->b_fname, FALSE, buf);
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001596 if (bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001597 CHANGEDTICK(buf) = -1; // note we did it already
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001598
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001599 // start all over, autocommands may mess up the lists
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001600 next_tp = first_tabpage;
1601 break;
1602 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001603 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001604 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001605
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001606 // Trigger BufUnload for buffers that are loaded
Bram Moolenaar29323592016-07-24 22:04:11 +02001607 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001608 if (buf->b_ml.ml_mfp != NULL)
1609 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001610 bufref_T bufref;
1611
1612 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001613 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001614 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001615 if (!bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001616 // autocmd deleted the buffer
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001617 break;
1618 }
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001619
1620 // deathtrap() blocks autocommands, but we do want to trigger
1621 // VimLeavePre.
1622 if (is_autocmd_blocked())
1623 {
1624 unblock_autocmds();
1625 ++unblock;
1626 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001627 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001628 if (unblock)
1629 block_autocmds();
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001630 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001631
1632#ifdef FEAT_VIMINFO
1633 if (*p_viminfo != NUL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001634 // Write out the registers, history, marks etc, to the viminfo file
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001635 write_viminfo(NULL, FALSE);
1636#endif
1637
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001638 if (v_dying <= 1)
Bram Moolenaarc7226682019-08-17 16:33:23 +02001639 {
1640 int unblock = 0;
1641
1642 // deathtrap() blocks autocommands, but we do want to trigger VimLeave.
1643 if (is_autocmd_blocked())
1644 {
1645 unblock_autocmds();
1646 ++unblock;
1647 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001648 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc7226682019-08-17 16:33:23 +02001649 if (unblock)
1650 block_autocmds();
1651 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001652
Bram Moolenaar05159a02005-02-26 23:04:13 +00001653#ifdef FEAT_PROFILE
1654 profile_dump();
1655#endif
1656
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001657 if (did_emsg
1658#ifdef FEAT_GUI
1659 || (gui.in_use && msg_didany && p_verbose > 0)
1660#endif
1661 )
1662 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001663 // give the user a chance to read the (error) message
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001664 no_wait_return = FALSE;
1665 wait_return(FALSE);
1666 }
1667
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001668 // Position the cursor again, the autocommands may have moved it
Bram Moolenaar7007e312021-03-27 12:11:33 +01001669 if (!is_not_a_term_or_gui())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001670 windgoto((int)Rows - 1, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001671
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001672#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001673 job_stop_on_exit();
1674#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001675#ifdef FEAT_LUA
1676 lua_end();
1677#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001678#ifdef FEAT_MZSCHEME
1679 mzscheme_end();
1680#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001681#ifdef FEAT_TCL
1682 tcl_end();
1683#endif
1684#ifdef FEAT_RUBY
1685 ruby_end();
1686#endif
1687#ifdef FEAT_PYTHON
1688 python_end();
1689#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001690#ifdef FEAT_PYTHON3
1691 python3_end();
1692#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001693#ifdef FEAT_PERL
1694 perl_end();
1695#endif
1696#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1697 iconv_end();
1698#endif
1699#ifdef FEAT_NETBEANS_INTG
1700 netbeans_end();
1701#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001702#ifdef FEAT_CSCOPE
1703 cs_end();
1704#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001705#ifdef FEAT_EVAL
1706 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001707 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001708#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001709#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +02001710 free_cmd_argsW();
1711#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001712
1713 mch_exit(exitval);
1714}
1715
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001716/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001717 * Get the name of the display, before gui_prepare() removes it from
1718 * argv[]. Used for the xterm-clipboard display.
1719 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001720 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001721 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001722 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001723early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001724{
Bram Moolenaar03005972008-11-20 13:12:36 +00001725#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1726 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001727 int argc = parmp->argc;
1728 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001729 int i;
1730
1731 for (i = 1; i < argc; i++)
1732 {
1733 if (STRCMP(argv[i], "--") == 0)
1734 break;
1735# ifdef FEAT_XCLIPBOARD
1736 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001737# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001738 || STRICMP(argv[i], "--display") == 0
1739# endif
1740 )
1741 {
1742 if (i == argc - 1)
1743 mainerr_arg_missing((char_u *)argv[i]);
1744 xterm_display = argv[++i];
1745 }
1746# endif
1747# ifdef FEAT_CLIENTSERVER
1748 else if (STRICMP(argv[i], "--servername") == 0)
1749 {
1750 if (i == argc - 1)
1751 mainerr_arg_missing((char_u *)argv[i]);
1752 parmp->serverName_arg = (char_u *)argv[++i];
1753 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001754 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001755 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001756 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001757 {
1758 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001759# ifdef FEAT_GUI
1760 if (strstr(argv[i], "-wait") != 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001761 // don't fork() when starting the GUI to edit files ourself
Bram Moolenaareb94e552006-03-11 21:35:11 +00001762 gui.dofork = FALSE;
1763# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001764 }
1765# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001766
Bram Moolenaar4f974752019-02-17 17:44:42 +01001767# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
1768# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001769 else if (STRICMP(argv[i], "--windowid") == 0)
1770# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001771 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001772# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001773 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001774 long_u id;
1775 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001776
1777 if (i == argc - 1)
1778 mainerr_arg_missing((char_u *)argv[i]);
1779 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001780 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001781 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001782 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001783 if (count != 1)
1784 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1785 else
Bram Moolenaar4f974752019-02-17 17:44:42 +01001786# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001787 win_socket_id = id;
1788# else
1789 gtk_socket_id = id;
1790# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001791 i++;
1792 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001793# endif
1794# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001795 else if (STRICMP(argv[i], "--echo-wid") == 0)
1796 echo_wid_arg = TRUE;
1797# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001798# ifndef FEAT_NETBEANS_INTG
1799 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001800 {
1801 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1802 mch_exit(2);
1803 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001804# endif
1805
Bram Moolenaar58d98232005-07-23 22:25:46 +00001806 }
1807#endif
1808}
1809
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001810#ifndef NO_VIM_MAIN
1811/*
1812 * Get a (optional) count for a Vim argument.
1813 */
1814 static int
1815get_number_arg(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001816 char_u *p, // pointer to argument
1817 int *idx, // index in argument, is incremented
1818 int def) // default value
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001819{
1820 if (vim_isdigit(p[*idx]))
1821 {
1822 def = atoi((char *)&(p[*idx]));
1823 while (vim_isdigit(p[*idx]))
1824 *idx = *idx + 1;
1825 }
1826 return def;
1827}
1828
1829/*
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001830 * Check for: [r][e][g][vi|vim|view][diff][ex[im]] (sort of)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001831 * If the executable name starts with "r" we disable shell commands.
1832 * If the next character is "e" we run in Easy mode.
1833 * If the next character is "g" we run the GUI version.
1834 * If the next characters are "view" we start in readonly mode.
1835 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1836 * If the next characters are "ex" we start in Ex mode. If it's followed
1837 * by "im" use improved Ex mode.
1838 */
1839 static void
1840parse_command_name(mparm_T *parmp)
1841{
1842 char_u *initstr;
1843
1844 initstr = gettail((char_u *)parmp->argv[0]);
1845
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001846#ifdef FEAT_EVAL
1847 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001848 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001849#endif
1850
1851 if (TOLOWER_ASC(initstr[0]) == 'r')
1852 {
1853 restricted = TRUE;
1854 ++initstr;
1855 }
1856
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001857 // Use evim mode for "evim" and "egvim", not for "editor".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001858 if (TOLOWER_ASC(initstr[0]) == 'e'
1859 && (TOLOWER_ASC(initstr[1]) == 'v'
1860 || TOLOWER_ASC(initstr[1]) == 'g'))
1861 {
1862#ifdef FEAT_GUI
1863 gui.starting = TRUE;
1864#endif
1865 parmp->evim_mode = TRUE;
1866 ++initstr;
1867 }
1868
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001869 // "gvim" starts the GUI. Also accept "Gvim" for MS-Windows.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001870 if (TOLOWER_ASC(initstr[0]) == 'g')
1871 {
1872 main_start_gui();
1873#ifdef FEAT_GUI
1874 ++initstr;
1875#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001876#ifdef GUI_MAY_SPAWN
1877 gui.dospawn = FALSE; // No need to spawn a new process.
1878#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001879 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001880#ifdef GUI_MAY_SPAWN
1881 else
1882 gui.dospawn = TRUE; // Not "gvim". Need to spawn gvim.exe.
1883#endif
1884
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001885
1886 if (STRNICMP(initstr, "view", 4) == 0)
1887 {
1888 readonlymode = TRUE;
1889 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001890 p_uc = 10000; // don't update very often
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001891 initstr += 4;
1892 }
1893 else if (STRNICMP(initstr, "vim", 3) == 0)
1894 initstr += 3;
1895
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001896 // Catch "[r][g]vimdiff" and "[r][g]viewdiff".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001897 if (STRICMP(initstr, "diff") == 0)
1898 {
1899#ifdef FEAT_DIFF
1900 parmp->diff_mode = TRUE;
1901#else
1902 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1903 mch_errmsg("\n");
1904 mch_exit(2);
1905#endif
1906 }
1907
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001908 // Checking for "ex" here may catch some weird names, such as "vimex" or
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001909 // "viewex", we assume the user knows that.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001910 if (STRNICMP(initstr, "ex", 2) == 0)
1911 {
1912 if (STRNICMP(initstr + 2, "im", 2) == 0)
1913 exmode_active = EXMODE_VIM;
1914 else
1915 exmode_active = EXMODE_NORMAL;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001916 change_compatible(TRUE); // set 'compatible'
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001917 }
1918}
1919
Bram Moolenaar58d98232005-07-23 22:25:46 +00001920/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001921 * Scan the command line arguments.
1922 */
1923 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001924command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001925{
1926 int argc = parmp->argc;
1927 char **argv = parmp->argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001928 int argv_idx; // index in argv[n][]
1929 int had_minmin = FALSE; // found "--" argument
1930 int want_argument; // option argument with argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001931 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001932 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001933 long n;
1934
1935 --argc;
1936 ++argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001937 argv_idx = 1; // active option letter is argv[0][argv_idx]
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001938 while (argc > 0)
1939 {
1940 /*
1941 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1942 */
1943 if (argv[0][0] == '+' && !had_minmin)
1944 {
1945 if (parmp->n_commands >= MAX_ARG_CMDS)
1946 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001947 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001948 if (argv[0][1] == NUL)
1949 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1950 else
1951 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1952 }
1953
1954 /*
1955 * Optional argument.
1956 */
1957 else if (argv[0][0] == '-' && !had_minmin)
1958 {
1959 want_argument = FALSE;
1960 c = argv[0][argv_idx++];
1961#ifdef VMS
1962 /*
1963 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1964 * and "-/X" as "-X".
1965 */
1966 if (c == '/')
1967 {
1968 c = argv[0][argv_idx++];
1969 c = TOUPPER_ASC(c);
1970 }
1971 else
1972 c = TOLOWER_ASC(c);
1973#endif
1974 switch (c)
1975 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001976 case NUL: // "vim -" read from stdin
1977 // "ex -" silent mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001978 if (exmode_active)
1979 silent_mode = TRUE;
1980 else
1981 {
1982 if (parmp->edit_type != EDIT_NONE)
1983 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1984 parmp->edit_type = EDIT_STDIN;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001985 read_cmd_fd = 2; // read from stderr instead of stdin
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001986 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001987 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001988 break;
1989
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001990 case '-': // "--" don't take any more option arguments
1991 // "--help" give help message
1992 // "--version" give version message
1993 // "--clean" clean context
1994 // "--literal" take files literally
1995 // "--nofork" don't fork
1996 // "--not-a-term" don't warn for not a term
1997 // "--ttyfail" exit if not a term
1998 // "--noplugin[s]" skip plugins
1999 // "--cmd <cmd>" execute cmd before vimrc
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002000 if (STRICMP(argv[0] + argv_idx, "help") == 0)
2001 usage();
2002 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
2003 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002004 Columns = 80; // need to init Columns
2005 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar0bcadf12021-02-11 19:18:58 +01002006#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI)
Bram Moolenaar3b678042021-02-11 19:08:05 +01002007 gui.starting = FALSE; // not starting GUI, will exit
Bram Moolenaar0bcadf12021-02-11 19:18:58 +01002008#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002009 list_version();
2010 msg_putchar('\n');
2011 msg_didout = FALSE;
2012 mch_exit(0);
2013 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002014 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
2015 {
2016 parmp->use_vimrc = (char_u *)"DEFAULTS";
Bram Moolenaar62dd4522018-03-14 21:20:02 +01002017#ifdef FEAT_GUI
2018 use_gvimrc = (char_u *)"NONE";
2019#endif
Bram Moolenaar07268702018-03-01 21:57:32 +01002020 parmp->clean = TRUE;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002021 set_option_value((char_u *)"vif", 0L, (char_u *)"NONE", 0);
2022 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002023 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
2024 {
Bram Moolenaar53076832015-12-31 19:53:21 +01002025#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002026 parmp->literal = TRUE;
2027#endif
2028 }
2029 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
2030 {
2031#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002032 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002033#endif
2034 }
2035 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
2036 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002037 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
2038 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002039 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
2040 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002041 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
2042 {
2043 want_argument = TRUE;
2044 argv_idx += 3;
2045 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002046 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
2047 {
2048 want_argument = TRUE;
2049 argv_idx += 11;
2050 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002051#ifdef FEAT_CLIENTSERVER
2052 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002053 ; // already processed -- no arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002054 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
2055 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
2056 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002057 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002058 if (argc > 1)
2059 {
2060 --argc;
2061 ++argv;
2062 }
2063 }
2064#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002065#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002066# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002067 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002068# else
2069 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
2070# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002071 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002072 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002073 if (argc > 1)
2074 {
2075 --argc;
2076 ++argv;
2077 }
2078 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00002079#endif
2080#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002081 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
2082 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002083 // already processed, skip
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002084 }
2085#endif
2086 else
2087 {
2088 if (argv[0][argv_idx])
2089 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2090 had_minmin = TRUE;
2091 }
2092 if (!want_argument)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002093 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002094 break;
2095
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002096 case 'A': // "-A" start in Arabic mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002097#ifdef FEAT_ARABIC
2098 set_option_value((char_u *)"arabic", 1L, NULL, 0);
2099#else
2100 mch_errmsg(_(e_noarabic));
2101 mch_exit(2);
2102#endif
2103 break;
2104
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002105 case 'b': // "-b" binary mode
2106 // Needs to be effective before expanding file names, because
2107 // for Win32 this makes us edit a shortcut file itself,
2108 // instead of the file it links to.
Bram Moolenaar231334e2005-07-25 20:46:57 +00002109 set_options_bin(curbuf->b_p_bin, 1, 0);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002110 curbuf->b_p_bin = 1; // binary file I/O
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002111 break;
2112
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002113 case 'C': // "-C" Compatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002114 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02002115 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002116 break;
2117
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002118 case 'e': // "-e" Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002119 exmode_active = EXMODE_NORMAL;
2120 break;
2121
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002122 case 'E': // "-E" Improved Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002123 exmode_active = EXMODE_VIM;
2124 break;
2125
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002126 case 'f': // "-f" GUI: run in foreground. Amiga: open
2127 // window directly, not with newcli
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002128#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002129 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002130#endif
2131 break;
2132
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002133 case 'g': // "-g" start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002134 main_start_gui();
2135 break;
2136
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002137 case 'F': // "-F" was for Farsi mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002138 mch_errmsg(_(e_nofarsi));
2139 mch_exit(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002140 break;
2141
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002142 case '?': // "-?" give help message (for MS-Windows)
2143 case 'h': // "-h" give help message
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002144#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002145 // Tell usage() to exit for "gvim".
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002146 gui.starting = FALSE;
2147#endif
2148 usage();
2149 break;
2150
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002151 case 'H': // "-H" start in Hebrew mode: rl + hkmap set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002152#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002153 p_hkmap = TRUE;
2154 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002155#else
2156 mch_errmsg(_(e_nohebrew));
2157 mch_exit(2);
2158#endif
2159 break;
2160
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002161 case 'l': // "-l" lisp mode, 'lisp' and 'showmatch' on
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002162#ifdef FEAT_LISP
2163 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2164 p_sm = TRUE;
2165#endif
2166 break;
2167
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002168 case 'M': // "-M" no changes or writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002169 reset_modifiable();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002170 // FALLTHROUGH
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002171
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002172 case 'm': // "-m" no writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002173 p_write = FALSE;
2174 break;
2175
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002176 case 'y': // "-y" easy mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002177#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002178 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002179#endif
2180 parmp->evim_mode = TRUE;
2181 break;
2182
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002183 case 'N': // "-N" Nocompatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002184 change_compatible(FALSE);
2185 break;
2186
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002187 case 'n': // "-n" no swap file
Bram Moolenaar67c53842010-05-22 18:28:27 +02002188#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002189 // checking for "-nb", netbeans parameters
Bram Moolenaar67c53842010-05-22 18:28:27 +02002190 if (argv[0][argv_idx] == 'b')
2191 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002192 netbeansArg = argv[0];
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002193 argv_idx = -1; // skip to next argument
Bram Moolenaar67c53842010-05-22 18:28:27 +02002194 }
2195 else
2196#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002197 parmp->no_swap_file = TRUE;
2198 break;
2199
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002200 case 'p': // "-p[N]" open N tab pages
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002201#ifdef TARGET_API_MAC_OSX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002202 // For some reason on MacOS X, an argument like:
2203 // -psn_0_10223617 is passed in when invoke from Finder
2204 // or with the 'open' command
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002205 if (argv[0][argv_idx] == 's')
2206 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002207 argv_idx = -1; // bypass full -psn
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002208 main_start_gui();
2209 break;
2210 }
2211#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002212 // default is 0: open window for each file
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002213 parmp->window_count = get_number_arg((char_u *)argv[0],
2214 &argv_idx, 0);
2215 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002216 break;
2217
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002218 case 'o': // "-o[N]" open N horizontal split windows
2219 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002220 parmp->window_count = get_number_arg((char_u *)argv[0],
2221 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002222 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002223 break;
2224
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002225 case 'O': // "-O[N]" open N vertical split windows
2226 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002227 parmp->window_count = get_number_arg((char_u *)argv[0],
2228 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002229 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002230 break;
2231
2232#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002233 case 'q': // "-q" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002234 if (parmp->edit_type != EDIT_NONE)
2235 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2236 parmp->edit_type = EDIT_QF;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002237 if (argv[0][argv_idx]) // "-q{errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002238 {
2239 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2240 argv_idx = -1;
2241 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002242 else if (argc > 1) // "-q {errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002243 want_argument = TRUE;
2244 break;
2245#endif
2246
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002247 case 'R': // "-R" readonly mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002248 readonlymode = TRUE;
2249 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002250 p_uc = 10000; // don't update very often
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002251 break;
2252
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002253 case 'r': // "-r" recovery mode
2254 case 'L': // "-L" recovery mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002255 recoverymode = 1;
2256 break;
2257
2258 case 's':
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002259 if (exmode_active) // "-s" silent (batch) mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002260 silent_mode = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002261 else // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002262 want_argument = TRUE;
2263 break;
2264
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002265 case 't': // "-t {tag}" or "-t{tag}" jump to tag
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002266 if (parmp->edit_type != EDIT_NONE)
2267 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2268 parmp->edit_type = EDIT_TAG;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002269 if (argv[0][argv_idx]) // "-t{tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002270 {
2271 parmp->tagname = (char_u *)argv[0] + argv_idx;
2272 argv_idx = -1;
2273 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002274 else // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002275 want_argument = TRUE;
2276 break;
2277
2278#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002279 case 'D': // "-D" Debugging
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002280 parmp->use_debug_break_level = 9999;
2281 break;
2282#endif
2283#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002284 case 'd': // "-d" 'diff'
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002285# ifdef AMIGA
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002286 // check for "-dev {device}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002287 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2288 want_argument = TRUE;
2289 else
2290# endif
2291 parmp->diff_mode = TRUE;
2292 break;
2293#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002294 case 'V': // "-V{N}" Verbose level
2295 // default is 10: a little bit verbose
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002296 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2297 if (argv[0][argv_idx] != NUL)
2298 {
2299 set_option_value((char_u *)"verbosefile", 0L,
2300 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002301 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002302 }
2303 break;
2304
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002305 case 'v': // "-v" Vi-mode (as if called "vi")
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002306 exmode_active = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002307#if defined(FEAT_GUI) && !defined(VIMDLL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002308 gui.starting = FALSE; // don't start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002309#endif
2310 break;
2311
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002312 case 'w': // "-w{number}" set window height
2313 // "-w {scriptout}" write to script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002314 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2315 {
2316 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2317 set_option_value((char_u *)"window", n, NULL, 0);
2318 break;
2319 }
2320 want_argument = TRUE;
2321 break;
2322
2323#ifdef FEAT_CRYPT
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002324 case 'x': // "-x" encrypted reading/writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002325 parmp->ask_for_key = TRUE;
2326 break;
2327#endif
2328
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002329 case 'X': // "-X" don't connect to X server
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002330#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2331 x_no_connect = TRUE;
2332#endif
2333 break;
2334
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002335 case 'Z': // "-Z" restricted mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002336 restricted = TRUE;
2337 break;
2338
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002339 case 'c': // "-c{command}" or "-c {command}" execute
2340 // command
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002341 if (argv[0][argv_idx] != NUL)
2342 {
2343 if (parmp->n_commands >= MAX_ARG_CMDS)
2344 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002345 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2346 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002347 argv_idx = -1;
2348 break;
2349 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002350 // FALLTHROUGH
2351 case 'S': // "-S {file}" execute Vim script
2352 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002353#ifndef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002354 case 'd': // "-d {device}" device (for Amiga)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002355#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002356 case 'T': // "-T {terminal}" terminal name
2357 case 'u': // "-u {vimrc}" vim inits file
2358 case 'U': // "-U {gvimrc}" gvim inits file
2359 case 'W': // "-W {scriptout}" overwrite
Bram Moolenaar4f974752019-02-17 17:44:42 +01002360#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002361 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002362#endif
2363 want_argument = TRUE;
2364 break;
2365
2366 default:
2367 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2368 }
2369
2370 /*
2371 * Handle option arguments with argument.
2372 */
2373 if (want_argument)
2374 {
2375 /*
2376 * Check for garbage immediately after the option letter.
2377 */
2378 if (argv[0][argv_idx] != NUL)
2379 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2380
2381 --argc;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002382 if (argc < 1 && c != 'S') // -S has an optional argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002383 mainerr_arg_missing((char_u *)argv[0]);
2384 ++argv;
2385 argv_idx = -1;
2386
2387 switch (c)
2388 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002389 case 'c': // "-c {command}" execute command
2390 case 'S': // "-S {file}" execute Vim script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002391 if (parmp->n_commands >= MAX_ARG_CMDS)
2392 mainerr(ME_EXTRA_CMD, NULL);
2393 if (c == 'S')
2394 {
2395 char *a;
2396
2397 if (argc < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002398 // "-S" without argument: use default session file
2399 // name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002400 a = SESSION_FILE;
2401 else if (argv[0][0] == '-')
2402 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002403 // "-S" followed by another option: use default
2404 // session file name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002405 a = SESSION_FILE;
2406 ++argc;
2407 --argv;
2408 }
2409 else
2410 a = argv[0];
Bram Moolenaar964b3742019-05-24 18:54:09 +02002411 p = alloc(STRLEN(a) + 4);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002412 if (p == NULL)
2413 mch_exit(2);
2414 sprintf((char *)p, "so %s", a);
2415 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2416 parmp->commands[parmp->n_commands++] = p;
2417 }
2418 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002419 parmp->commands[parmp->n_commands++] =
2420 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002421 break;
2422
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002423 case '-':
2424 if (argv[-1][2] == 'c')
2425 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002426 // "--cmd {command}" execute command
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002427 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2428 mainerr(ME_EXTRA_CMD, NULL);
2429 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002430 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002431 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002432 // "--startuptime <file>" already handled
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002433 break;
2434
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002435 // case 'd': -d {device} is handled in mch_check_win() for the
2436 // Amiga
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002437
2438#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002439 case 'q': // "-q {errorfile}" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002440 parmp->use_ef = (char_u *)argv[0];
2441 break;
2442#endif
2443
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002444 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002445 set_option_value((char_u *)"vif", 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002446 break;
2447
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002448 case 's': // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002449 if (scriptin[0] != NULL)
2450 {
2451scripterror:
2452 mch_errmsg(_("Attempt to open script file again: \""));
2453 mch_errmsg(argv[-1]);
2454 mch_errmsg(" ");
2455 mch_errmsg(argv[0]);
2456 mch_errmsg("\"\n");
2457 mch_exit(2);
2458 }
2459 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2460 {
2461 mch_errmsg(_("Cannot open for reading: \""));
2462 mch_errmsg(argv[0]);
2463 mch_errmsg("\"\n");
2464 mch_exit(2);
2465 }
2466 if (save_typebuf() == FAIL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002467 mch_exit(2); // out of memory
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002468 break;
2469
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002470 case 't': // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002471 parmp->tagname = (char_u *)argv[0];
2472 break;
2473
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002474 case 'T': // "-T {terminal}" terminal name
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002475 /*
2476 * The -T term argument is always available and when
2477 * HAVE_TERMLIB is supported it overrides the environment
2478 * variable TERM.
2479 */
2480#ifdef FEAT_GUI
2481 if (term_is_gui((char_u *)argv[0]))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002482 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002483 else
2484#endif
2485 parmp->term = (char_u *)argv[0];
2486 break;
2487
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002488 case 'u': // "-u {vimrc}" vim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002489 parmp->use_vimrc = (char_u *)argv[0];
2490 break;
2491
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002492 case 'U': // "-U {gvimrc}" gvim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002493#ifdef FEAT_GUI
2494 use_gvimrc = (char_u *)argv[0];
2495#endif
2496 break;
2497
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002498 case 'w': // "-w {nr}" 'window' value
2499 // "-w {scriptout}" append to script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002500 if (vim_isdigit(*((char_u *)argv[0])))
2501 {
2502 argv_idx = 0;
2503 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2504 set_option_value((char_u *)"window", n, NULL, 0);
2505 argv_idx = -1;
2506 break;
2507 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002508 // FALLTHROUGH
2509 case 'W': // "-W {scriptout}" overwrite script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002510 if (scriptout != NULL)
2511 goto scripterror;
2512 if ((scriptout = mch_fopen(argv[0],
2513 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2514 {
2515 mch_errmsg(_("Cannot open for script output: \""));
2516 mch_errmsg(argv[0]);
2517 mch_errmsg("\"\n");
2518 mch_exit(2);
2519 }
2520 break;
2521
Bram Moolenaar4f974752019-02-17 17:44:42 +01002522#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002523 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002524 gui_mch_set_parent(argv[0]);
2525 break;
2526#endif
2527 }
2528 }
2529 }
2530
2531 /*
2532 * File name argument.
2533 */
2534 else
2535 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002536 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002537
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002538 // Check for only one type of editing.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002539 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2540 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2541 parmp->edit_type = EDIT_FILE;
2542
2543#ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002544 // Remember if the argument was a full path before changing
2545 // slashes to backslashes.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002546 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2547 parmp->full_path = TRUE;
2548#endif
2549
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002550 // Add the file to the global argument list.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002551 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2552 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2553 mch_exit(2);
2554#ifdef FEAT_DIFF
2555 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2556 && !mch_isdir(alist_name(&GARGLIST[0])))
2557 {
2558 char_u *r;
2559
2560 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2561 if (r != NULL)
2562 {
2563 vim_free(p);
2564 p = r;
2565 }
2566 }
2567#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002568#if defined(__CYGWIN32__) && !defined(MSWIN)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002569 /*
2570 * If vim is invoked by non-Cygwin tools, convert away any
2571 * DOS paths, so things like .swp files are created correctly.
2572 * Look for evidence of non-Cygwin paths before we bother.
2573 * This is only for when using the Unix files.
2574 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002575 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002576 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002577 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002578
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002579# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002580 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002581# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002582 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002583# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002584 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002585 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002586 if (p == NULL)
2587 mch_exit(2);
2588 }
2589#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002590
2591#ifdef USE_FNAME_CASE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002592 // Make the case of the file name match the actual file.
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002593 fname_case(p, 0);
2594#endif
2595
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002596 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002597#ifdef EXPAND_FILENAMES
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002598 parmp->literal ? 2 : 0 // add buffer nr after exp.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002599#else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002600 2 // add buffer number now and use curbuf
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002601#endif
2602 );
2603
Bram Moolenaar4f974752019-02-17 17:44:42 +01002604#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002605 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002606 // Remember this argument has been added to the argument list.
2607 // Needed when 'encoding' is changed.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002608 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002609# ifdef FEAT_DIFF
2610 parmp->diff_mode
2611# else
2612 FALSE
2613# endif
2614 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002615 }
2616#endif
2617 }
2618
2619 /*
2620 * If there are no more letters after the current "-", go to next
2621 * argument. argv_idx is set to -1 when the current argument is to be
2622 * skipped.
2623 */
2624 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2625 {
2626 --argc;
2627 ++argv;
2628 argv_idx = 1;
2629 }
2630 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002631
2632#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002633 // If there is a "+123" or "-c" command, set v:swapcommand to the first
2634 // one.
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002635 if (parmp->n_commands > 0)
2636 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002637 p = alloc(STRLEN(parmp->commands[0]) + 3);
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002638 if (p != NULL)
2639 {
2640 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2641 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2642 vim_free(p);
2643 }
2644 }
2645#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002646}
2647
2648/*
2649 * Print a warning if stdout is not a terminal.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +01002650 * When starting in Ex mode and commands come from a file, set silent_mode.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002651 */
2652 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002653check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002654{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002655 int input_isatty; // is active input a terminal?
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002656
2657 input_isatty = mch_input_isatty();
2658 if (exmode_active)
2659 {
2660 if (!input_isatty)
2661 silent_mode = TRUE;
2662 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002663 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002664#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002665 // don't want the delay when started from the desktop
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002666 && !gui.starting
2667#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002668 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002669 {
2670#ifdef NBDEBUG
2671 /*
2672 * This shouldn't be necessary. But if I run netbeans with the log
2673 * output coming to the console and XOpenDisplay fails, I get vim
2674 * trying to start with input/output to my console tty. This fills my
2675 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002676 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002677 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002678 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002679 {
2680 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2681 exit(1);
2682 }
2683#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002684#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2685 if (
2686# ifdef VIMDLL
2687 !gui.starting &&
2688# endif
2689 is_cygpty_used())
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002690 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002691# if defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
Bram Moolenaar2a027452017-09-26 19:10:37 +02002692 && defined(FEAT_GETTEXT)
2693 char *s, *tofree = NULL;
2694
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002695 // Set the encoding of the error message based on $LC_ALL or
2696 // other environment variables instead of 'encoding'.
2697 // Note that the message is shown on a Cygwin terminal (e.g.
2698 // mintty) which encoding is based on $LC_ALL or etc., not the
2699 // current codepage used by normal Win32 console programs.
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002700 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002701 if (s == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002702 s = "utf-8"; // Use "utf-8" by default.
Bram Moolenaar2a027452017-09-26 19:10:37 +02002703 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2704 vim_free(tofree);
2705# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002706 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2707 exit(1);
2708 }
2709#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002710 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002711 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2712 if (!input_isatty)
2713 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2714 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002715 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2716 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002717 if (scriptin[0] == NULL)
Bram Moolenaareda1da02019-11-17 17:06:33 +01002718 ui_delay(2005L, TRUE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002719 TIME_MSG("Warning delay");
2720 }
2721}
2722
2723/*
2724 * Read text from stdin.
2725 */
2726 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002727read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002728{
2729 int i;
2730
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002731 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002732 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002733
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002734 no_wait_return = TRUE;
2735 i = msg_didany;
2736 set_buflisted(TRUE);
Bram Moolenaar204ade62020-10-11 14:58:46 +02002737
2738 // Create memfile and read from stdin.
Bram Moolenaar204ade62020-10-11 14:58:46 +02002739 (void)open_buffer(TRUE, NULL, 0);
2740
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002741 no_wait_return = FALSE;
2742 msg_didany = i;
2743 TIME_MSG("reading stdin");
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002744
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002745 check_swap_exists_action();
Bram Moolenaar8e1cbb52020-12-08 19:36:21 +01002746
2747#if !(defined(AMIGA) || defined(MACOS_X))
2748 // Dup stdin from stderr to read commands from, so that shell commands
2749 // work.
2750 // TODO: why is this needed, even though readfile() has done this?
2751 close(0);
2752 vim_ignored = dup(2);
2753#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002754}
2755
2756/*
2757 * Create the requested number of windows and edit buffers in them.
2758 * Also does recovery if "recoverymode" set.
2759 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002760 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002761create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002762{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002763 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002764 int done = 0;
2765
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002766 /*
2767 * Create the number of windows that was requested.
2768 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002769 if (parmp->window_count == -1) // was not set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002770 parmp->window_count = 1;
2771 if (parmp->window_count == 0)
2772 parmp->window_count = GARGCOUNT;
2773 if (parmp->window_count > 1)
2774 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002775 // Don't change the windows if there was a command in .vimrc that
2776 // already split some windows
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002777 if (parmp->window_layout == 0)
2778 parmp->window_layout = WIN_HOR;
2779 if (parmp->window_layout == WIN_TABS)
2780 {
2781 parmp->window_count = make_tabpages(parmp->window_count);
2782 TIME_MSG("making tab pages");
2783 }
2784 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002785 {
2786 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002787 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002788 TIME_MSG("making windows");
2789 }
2790 else
2791 parmp->window_count = win_count();
2792 }
2793 else
2794 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002795
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002796 if (recoverymode) // do recover
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002797 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002798 msg_scroll = TRUE; // scroll message up
Bram Moolenaar99499b12019-05-23 21:35:48 +02002799 ml_recover(TRUE);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002800 if (curbuf->b_ml.ml_mfp == NULL) // failed
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002801 getout(1);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002802 do_modelines(0); // do modelines
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002803 }
2804 else
2805 {
2806 /*
2807 * Open a buffer for windows that don't have one yet.
2808 * Commands in the .vimrc might have loaded a file or split the window.
2809 * Watch out for autocommands that delete a window.
2810 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002811 /*
2812 * Don't execute Win/Buf Enter/Leave autocommands here
2813 */
2814 ++autocmd_no_enter;
2815 ++autocmd_no_leave;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002816 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002817 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002818 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002819 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002820 {
2821 if (parmp->window_layout == WIN_TABS)
2822 goto_tabpage(1);
2823 else
2824 curwin = firstwin;
2825 }
2826 else if (parmp->window_layout == WIN_TABS)
2827 {
2828 if (curtab->tp_next == NULL)
2829 break;
2830 goto_tabpage(0);
2831 }
2832 else
2833 {
2834 if (curwin->w_next == NULL)
2835 break;
2836 curwin = curwin->w_next;
2837 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002838 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002839 curbuf = curwin->w_buffer;
2840 if (curbuf->b_ml.ml_mfp == NULL)
2841 {
2842#ifdef FEAT_FOLDING
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002843 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002844 if (p_fdls >= 0)
2845 curwin->w_p_fdl = p_fdls;
2846#endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002847 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002848 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002849
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002850 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002851
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002852 // create memfile, read file
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002853 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002854
Bram Moolenaar84212822006-11-07 21:59:47 +00002855 if (swap_exists_action == SEA_QUIT)
2856 {
2857 if (got_int || only_one_window())
2858 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002859 // abort selected or quit and only one window
2860 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00002861 getout(1);
2862 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002863 // We can't close the window, it would disturb what
2864 // happens next. Clear the file name and set the arg
2865 // index to -1 to delete it later.
Bram Moolenaar84212822006-11-07 21:59:47 +00002866 setfname(curbuf, NULL, NULL, FALSE);
2867 curwin->w_arg_idx = -1;
2868 swap_exists_action = SEA_NONE;
2869 }
2870 else
2871 handle_swap_exists(NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002872 dorewind = TRUE; // start again
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002873 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002874 ui_breakcheck();
2875 if (got_int)
2876 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002877 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002878 break;
2879 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002880 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002881 if (parmp->window_layout == WIN_TABS)
2882 goto_tabpage(1);
2883 else
2884 curwin = firstwin;
2885 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002886 --autocmd_no_enter;
2887 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002888 }
2889}
2890
Bram Moolenaar9810cfb2019-12-11 21:23:00 +01002891/*
2892 * If opened more than one window, start editing files in the other
2893 * windows. make_windows() has already opened the windows.
2894 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002895 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002896edit_buffers(
2897 mparm_T *parmp,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002898 char_u *cwd) // current working dir
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002899{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002900 int arg_idx; // index in argument list
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002901 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002902 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002903 win_T *win;
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002904 char_u *p_shm_save = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002905
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002906 /*
2907 * Don't execute Win/Buf Enter/Leave autocommands here
2908 */
2909 ++autocmd_no_enter;
2910 ++autocmd_no_leave;
Bram Moolenaar84212822006-11-07 21:59:47 +00002911
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002912 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002913 if (curwin->w_arg_idx == -1)
2914 {
2915 win_close(curwin, TRUE);
2916 advance = FALSE;
2917 }
2918
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002919 arg_idx = 1;
2920 for (i = 1; i < parmp->window_count; ++i)
2921 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002922 if (cwd != NULL)
2923 mch_chdir((char *)cwd);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002924 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002925 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002926 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002927 ++arg_idx;
2928 win_close(curwin, TRUE);
2929 advance = FALSE;
2930 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002931 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002932
Bram Moolenaar84212822006-11-07 21:59:47 +00002933 if (advance)
2934 {
2935 if (parmp->window_layout == WIN_TABS)
2936 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002937 if (curtab->tp_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00002938 break;
2939 goto_tabpage(0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002940 // Temporarily reset 'shm' option to not print fileinfo when
2941 // loading the other buffers. This would overwrite the already
2942 // existing fileinfo for the first tab.
2943 if (i == 1)
2944 {
2945 char buf[100];
2946
2947 p_shm_save = vim_strsave(p_shm);
2948 vim_snprintf(buf, 100, "F%s", p_shm);
2949 set_option_value((char_u *)"shm", 0L, (char_u *)buf, 0);
2950 }
Bram Moolenaar84212822006-11-07 21:59:47 +00002951 }
2952 else
2953 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002954 if (curwin->w_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00002955 break;
2956 win_enter(curwin->w_next, FALSE);
2957 }
2958 }
2959 advance = TRUE;
2960
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002961 // Only open the file if there is no file in this window yet (that can
2962 // happen when .vimrc contains ":sall").
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002963 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2964 {
2965 curwin->w_arg_idx = arg_idx;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002966 // Edit file from arg list, if there is one. When "Quit" selected
2967 // at the ATTENTION prompt close the window.
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002968 swap_exists_did_quit = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002969 (void)do_ecmd(0, arg_idx < GARGCOUNT
2970 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002971 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002972 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002973 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002974 // abort or quit selected
Bram Moolenaar84212822006-11-07 21:59:47 +00002975 if (got_int || only_one_window())
2976 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002977 // abort selected and only one window
2978 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00002979 getout(1);
2980 }
2981 win_close(curwin, TRUE);
2982 advance = FALSE;
2983 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002984 if (arg_idx == GARGCOUNT - 1)
2985 arg_had_last = TRUE;
2986 ++arg_idx;
2987 }
2988 ui_breakcheck();
2989 if (got_int)
2990 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002991 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002992 break;
2993 }
2994 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002995
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002996 if (p_shm_save != NULL)
2997 {
2998 set_option_value((char_u *)"shm", 0L, p_shm_save, 0);
2999 vim_free(p_shm_save);
3000 }
3001
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003002 if (parmp->window_layout == WIN_TABS)
3003 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003004 --autocmd_no_enter;
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003005
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003006 // make the first window the current window
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003007 win = firstwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003008#if defined(FEAT_QUICKFIX)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003009 // Avoid making a preview window the current window.
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003010 while (win->w_p_pvw)
3011 {
3012 win = win->w_next;
3013 if (win == NULL)
3014 {
3015 win = firstwin;
3016 break;
3017 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003018 }
3019#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003020 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003021
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003022 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003023 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003024 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003025 win_equal(curwin, FALSE, 'b'); // adjust heights
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003026}
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003027
3028/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003029 * Execute the commands from --cmd arguments "cmds[cnt]".
3030 */
3031 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003032exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003033{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003034 char_u **cmds = parmp->pre_commands;
3035 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003036 int i;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003037 ESTACK_CHECK_DECLARATION
Bram Moolenaar58d98232005-07-23 22:25:46 +00003038
3039 if (cnt > 0)
3040 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003041 curwin->w_cursor.lnum = 0; // just in case..
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003042 estack_push(ETYPE_ARGS, (char_u *)_("pre-vimrc command line"), 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003043 ESTACK_CHECK_SETUP
Bram Moolenaar58d98232005-07-23 22:25:46 +00003044# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003045 current_sctx.sc_sid = SID_CMDARG;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003046# endif
3047 for (i = 0; i < cnt; ++i)
3048 do_cmdline_cmd(cmds[i]);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003049 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003050 estack_pop();
Bram Moolenaar58d98232005-07-23 22:25:46 +00003051# 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;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003065 ESTACK_CHECK_DECLARATION
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003066
3067 /*
3068 * We start commands on line 0, make "vim +/pat file" match a
3069 * pattern on line 1. But don't move the cursor when an autocommand
3070 * with g`" was used.
3071 */
3072 msg_scroll = TRUE;
3073 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
3074 curwin->w_cursor.lnum = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003075 estack_push(ETYPE_ARGS, (char_u *)"command line", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003076 ESTACK_CHECK_SETUP
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003077#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003078 current_sctx.sc_sid = SID_CARG;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003079 current_sctx.sc_seq = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003080#endif
3081 for (i = 0; i < parmp->n_commands; ++i)
3082 {
3083 do_cmdline_cmd(parmp->commands[i]);
3084 if (parmp->cmds_tofree[i])
3085 vim_free(parmp->commands[i]);
3086 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01003087 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003088 estack_pop();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003089#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003090 current_sctx.sc_sid = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003091#endif
3092 if (curwin->w_cursor.lnum == 0)
3093 curwin->w_cursor.lnum = 1;
3094
3095 if (!exmode_active)
3096 msg_scroll = FALSE;
3097
3098#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003099 // When started with "-q errorfile" jump to first error again.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003100 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003101 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003102#endif
3103 TIME_MSG("executing command arguments");
3104}
3105
3106/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003107 * Source startup scripts.
3108 */
3109 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003110source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003111{
3112 int i;
3113
3114 /*
3115 * For "evim" source evim.vim first of all, so that the user can overrule
3116 * any things he doesn't like.
3117 */
3118 if (parmp->evim_mode)
3119 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003121 TIME_MSG("source evim file");
3122 }
3123
3124 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003125 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00003126 * nothing else.
3127 */
3128 if (parmp->use_vimrc != NULL)
3129 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003130 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003131 {
3132 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE, NULL)
3133 != OK)
3134 emsg(e_failed_to_source_defaults);
3135 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003136 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003137 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003138 {
3139#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003140 if (use_gvimrc == NULL) // don't load gvimrc either
Bram Moolenaar58d98232005-07-23 22:25:46 +00003141 use_gvimrc = parmp->use_vimrc;
3142#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003143 }
3144 else
3145 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003147 semsg(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003148 }
3149 }
3150 else if (!silent_mode)
3151 {
3152#ifdef AMIGA
3153 struct Process *proc = (struct Process *)FindTask(0L);
3154 APTR save_winptr = proc->pr_WindowPtr;
3155
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003156 // Avoid a requester here for a volume that doesn't exist.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003157 proc->pr_WindowPtr = (APTR)-1L;
3158#endif
3159
3160 /*
3161 * Get system wide defaults, if the file name is defined.
3162 */
3163#ifdef SYS_VIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003165#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003166#ifdef MACOS_X
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE,
3168 DOSO_NONE, NULL);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003169#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003170
3171 /*
3172 * Try to read initialization commands from the following places:
3173 * - environment variable VIMINIT
3174 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3175 * - second user vimrc file ($VIM/.vimrc for Dos)
3176 * - environment variable EXINIT
3177 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3178 * - second user exrc file ($VIM/.exrc for Dos)
3179 * The first that exists is used, the rest is ignored.
3180 */
3181 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3182 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 if (do_source((char_u *)USR_VIMRC_FILE, TRUE,
3184 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003185#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003186 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003188#endif
3189#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003190 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003192#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003193#ifdef USR_VIMRC_FILE4
3194 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003196#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003197 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 && do_source((char_u *)USR_EXRC_FILE, FALSE,
3199 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003200#ifdef USR_EXRC_FILE2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 && do_source((char_u *)USR_EXRC_FILE2, FALSE,
3202 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003203#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003204 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003205 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003206 // When no .vimrc file was found: source defaults.vim.
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003207 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE,
3208 NULL) == FAIL)
3209 emsg(e_failed_to_source_defaults);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003210 }
3211 }
3212
3213 /*
3214 * Read initialization commands from ".vimrc" or ".exrc" in current
3215 * directory. This is only done if the 'exrc' option is set.
3216 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003217 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003218 * option has been reset in environment of global ".exrc" or ".vimrc".
3219 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3220 * SYS_VIMRC_FILE.
3221 */
3222 if (p_exrc)
3223 {
3224#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003225 // If ".vimrc" file is not owned by user, set 'secure' mode.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003226 if (!file_owned(VIMRC_FILE))
3227#endif
3228 secure = p_secure;
3229
3230 i = FAIL;
3231 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003232 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003233#ifdef USR_VIMRC_FILE2
3234 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003235 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003236#endif
3237#ifdef USR_VIMRC_FILE3
3238 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003239 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003240#endif
3241#ifdef SYS_VIMRC_FILE
3242 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003243 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003244#endif
3245 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003247
3248 if (i == FAIL)
3249 {
3250#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003251 // if ".exrc" is not owned by user set 'secure' mode
Bram Moolenaar58d98232005-07-23 22:25:46 +00003252 if (!file_owned(EXRC_FILE))
3253 secure = p_secure;
3254 else
3255 secure = 0;
3256#endif
3257 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003258 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003259#ifdef USR_EXRC_FILE2
3260 && fullpathcmp((char_u *)USR_EXRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003261 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003262#endif
3263 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 (void)do_source((char_u *)EXRC_FILE, FALSE,
3265 DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003266 }
3267 }
3268 if (secure == 2)
3269 need_wait_return = TRUE;
3270 secure = 0;
3271#ifdef AMIGA
3272 proc->pr_WindowPtr = save_winptr;
3273#endif
3274 }
3275 TIME_MSG("sourcing vimrc file(s)");
3276}
3277
3278/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003279 * Setup to start using the GUI. Exit with an error when not available.
3280 */
3281 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003282main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003283{
3284#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003285 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003286#else
3287 mch_errmsg(_(e_nogvim));
3288 mch_errmsg("\n");
3289 mch_exit(2);
3290#endif
3291}
3292
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003293#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003294
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003295/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 * Get an environment variable and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003297 * Returns FAIL if the environment variable was not executed, OK otherwise.
3298 */
3299 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003300process_env(
3301 char_u *env,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003302 int is_viminit) // when TRUE, called for VIMINIT
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003303{
3304 char_u *initstr;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003305 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003306
Bram Moolenaare31ee862020-01-07 20:59:34 +01003307 ESTACK_CHECK_DECLARATION
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003308
3309 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3310 {
3311 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003312 vimrc_found(NULL, NULL);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003313 estack_push(ETYPE_ENV, env, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003314 ESTACK_CHECK_SETUP
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003315 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003316 current_sctx.sc_version = 1;
3317#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003318 current_sctx.sc_sid = SID_ENV;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003319 current_sctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003320 current_sctx.sc_lnum = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003321#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003322
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003323 do_cmdline_cmd(initstr);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003324
Bram Moolenaare31ee862020-01-07 20:59:34 +01003325 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003326 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003327 current_sctx = save_current_sctx;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003328 return OK;
3329 }
3330 return FAIL;
3331}
3332
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003333#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003334/*
3335 * Return TRUE if we are certain the user owns the file "fname".
3336 * Used for ".vimrc" and ".exrc".
3337 * Use both stat() and lstat() for extra security.
3338 */
3339 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003340file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003341{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003342 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003343# ifdef UNIX
3344 uid_t uid = getuid();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003345# else // VMS
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003346 uid_t uid = ((getgid() << 16) | getuid());
3347# endif
3348
3349 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3350# ifdef HAVE_LSTAT
3351 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3352# endif
3353 );
3354}
3355#endif
3356
3357/*
3358 * Give an error message main_errors["n"] and exit.
3359 */
3360 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003361mainerr(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003362 int n, // one of the ME_ defines
3363 char_u *str) // extra argument or NULL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003364{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003365#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003366 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003367#endif
3368
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003369 // If this is a Windows GUI executable, show an error dialog box.
3370#ifdef VIMDLL
3371 gui.in_use = mch_is_gui_executable();
3372#endif
3373#ifdef FEAT_GUI_MSWIN
3374 gui.starting = FALSE; // Needed to show as error.
3375#endif
3376
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003377 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003378 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003379 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003380 mch_errmsg(_(main_errors[n]));
3381 if (str != NULL)
3382 {
3383 mch_errmsg(": \"");
3384 mch_errmsg((char *)str);
3385 mch_errmsg("\"");
3386 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003387 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003388
3389 mch_exit(1);
3390}
3391
3392 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003393mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003394{
3395 mainerr(ME_ARG_MISSING, str);
3396}
3397
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003398#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003399/*
3400 * print a message with three spaces prepended and '\n' appended.
3401 */
3402 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003403main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003404{
3405 mch_msg(" ");
3406 mch_msg(s);
3407 mch_msg("\n");
3408}
3409
3410/*
3411 * Print messages for "vim -h" or "vim --help" and exit.
3412 */
3413 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003414usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003415{
3416 int i;
3417 static char *(use[]) =
3418 {
3419 N_("[file ..] edit specified file(s)"),
3420 N_("- read text from stdin"),
3421 N_("-t tag edit file where tag is defined"),
3422#ifdef FEAT_QUICKFIX
3423 N_("-q [errorfile] edit file with first error")
3424#endif
3425 };
3426
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003427#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003428 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003429#endif
3430
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003431 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003432 mch_msg(longVersion);
Bram Moolenaar32aaf5a2018-06-21 21:38:33 +02003433 mch_msg(_("\n\nUsage:"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003434 for (i = 0; ; ++i)
3435 {
3436 mch_msg(_(" vim [arguments] "));
3437 mch_msg(_(use[i]));
3438 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3439 break;
3440 mch_msg(_("\n or:"));
3441 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003442#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003443 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003444#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003445
3446 mch_msg(_("\n\nArguments:\n"));
3447 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003448#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003449 main_msg(_("--literal\t\tDon't expand wildcards"));
3450#endif
3451#ifdef FEAT_OLE
3452 main_msg(_("-register\t\tRegister this gvim for OLE"));
3453 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3454#endif
3455#ifdef FEAT_GUI
3456 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3457 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3458#endif
3459 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3460 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003461 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003462 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3463#ifdef FEAT_DIFF
3464 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3465#endif
3466 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3467 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3468 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3469 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3470 main_msg(_("-M\t\t\tModifications in text not allowed"));
3471 main_msg(_("-b\t\t\tBinary mode"));
3472#ifdef FEAT_LISP
3473 main_msg(_("-l\t\t\tLisp mode"));
3474#endif
3475 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3476 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003477 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3478#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003479 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003480#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003481 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3482 main_msg(_("-r\t\t\tList swap files and exit"));
3483 main_msg(_("-r (with file name)\tRecover crashed session"));
3484 main_msg(_("-L\t\t\tSame as -r"));
3485#ifdef AMIGA
3486 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3487 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3488#endif
3489#ifdef FEAT_ARABIC
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003490 main_msg(_("-A\t\t\tStart in Arabic mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003491#endif
3492#ifdef FEAT_RIGHTLEFT
3493 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3494#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003495 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003496 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003497 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003498 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3499#ifdef FEAT_GUI
3500 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3501#endif
3502 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003503 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003504 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3505 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3506 main_msg(_("+\t\t\tStart at end of file"));
3507 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003508 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003509 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3510 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3511 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3512 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3513 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3514#ifdef FEAT_CRYPT
3515 main_msg(_("-x\t\t\tEdit encrypted files"));
3516#endif
3517#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3518# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar86181df2020-05-11 23:14:04 +02003519 main_msg(_("-display <display>\tConnect Vim to this particular X-server"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003520# endif
3521 main_msg(_("-X\t\t\tDo not connect to X server"));
3522#endif
3523#ifdef FEAT_CLIENTSERVER
3524 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3525 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3526 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3527 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003528 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003529 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3530 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3531 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3532 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3533#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003534#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003535 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003536#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003537#ifdef FEAT_VIMINFO
3538 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3539#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003540 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003541 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3542 main_msg(_("--version\t\tPrint version information and exit"));
3543
3544#ifdef FEAT_GUI_X11
3545# ifdef FEAT_GUI_MOTIF
3546 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3547# else
3548# ifdef FEAT_GUI_ATHENA
3549# ifdef FEAT_GUI_NEXTAW
3550 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3551# else
3552 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3553# endif
3554# endif
3555# endif
Bram Moolenaar86181df2020-05-11 23:14:04 +02003556 main_msg(_("-display <display>\tRun Vim on <display>"));
Bram Moolenaar9e6ba8c2020-05-12 22:21:26 +02003557 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003558 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3559 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3560 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3561 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3562 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3563 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3564 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3565 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3566# ifdef FEAT_GUI_ATHENA
3567 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3568# endif
3569 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3570 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3571 main_msg(_("-xrm <resource>\tSet the specified resource"));
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003572#endif // FEAT_GUI_X11
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003573#ifdef FEAT_GUI_GTK
3574 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003575 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3576 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003577 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3578 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003579 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003580 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
Bram Moolenaar86181df2020-05-11 23:14:04 +02003581 main_msg(_("-display <display>\tRun Vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003582 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003583 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003584 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003585#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003586#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003587# ifdef VIMDLL
3588 if (gui.starting)
3589# endif
3590 {
3591 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
3592 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
3593 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003594#endif
3595
3596#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003597 // Gnome gives extra messages for --help if we continue, but not for -h.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003598 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003599 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003600 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003601 gui.dofork = FALSE;
3602 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003603 else
3604#endif
3605 mch_exit(0);
3606}
3607
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003608/*
3609 * Check the result of the ATTENTION dialog:
3610 * When "Quit" selected, exit Vim.
3611 * When "Recover" selected, recover the file.
3612 */
3613 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003614check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003615{
3616 if (swap_exists_action == SEA_QUIT)
3617 getout(1);
3618 handle_swap_exists(NULL);
3619}
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003620
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003621#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003622
Bram Moolenaar595297d2017-03-04 19:11:12 +01003623#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003624 static void
3625set_progpath(char_u *argv0)
3626{
3627 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003628
Bram Moolenaar4f974752019-02-17 17:44:42 +01003629# ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003630 // A relative path containing a "/" will become invalid when using ":cd",
3631 // turn it into a full path.
3632 // On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3633 // this.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003634 char_u *path = NULL;
3635
3636 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3637 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003638# else
3639 char_u buf[MAXPATHL + 1];
3640# ifdef PROC_EXE_LINK
3641 char linkbuf[MAXPATHL + 1];
3642 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003643
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003644 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3645 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003646 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003647 linkbuf[len] = NUL;
3648 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003649 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003650# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003651
3652 if (!mch_isFullName(val))
3653 {
3654 if (gettail(val) != val
3655 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3656 val = buf;
3657 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003658# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003659
Bram Moolenaar08cab962017-03-04 14:37:18 +01003660 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003661
Bram Moolenaar4f974752019-02-17 17:44:42 +01003662# ifdef MSWIN
Bram Moolenaar43663192017-03-05 14:29:12 +01003663 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003664# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003665}
3666
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003667#endif // NO_VIM_MAIN