blob: d418c13801c5eb84a9c8b6d346719587afe35de6 [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 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100263 // Remember the number of entries in the argument list. If it changes
264 // we don't react on setting 'encoding'.
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000265 set_alist_count();
266 }
267#endif
268
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000269#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000270 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000271 {
272 /*
273 * If there is one filename, fully qualified, we have very probably
274 * been invoked from explorer, so change to the file's directory.
275 * Hint: to avoid this when typing a command use a forward slash.
276 * If the cd fails, it doesn't matter.
277 */
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100278 (void)vim_chdirfile(params.fname, "drop");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200279 if (start_dir != NULL)
280 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000281 }
282#endif
283 TIME_MSG("expanding arguments");
284
285#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000286 if (params.diff_mode && params.window_count == -1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100287 params.window_count = 0; // open up to 3 windows
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000288#endif
289
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100290 // Don't redraw until much later.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000291 ++RedrawingDisabled;
292
293 /*
294 * When listing swap file names, don't do cursor positioning et. al.
295 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200296 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000297 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000298
299 /*
300 * When certain to start the GUI, don't check capabilities of terminal.
301 * For GTK we can't be sure, but when started from the desktop it doesn't
302 * make sense to try using a terminal.
303 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200304#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
305 || defined(VIMDLL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000306 if (gui.starting
307# ifdef FEAT_GUI_GTK
308 && !isatty(2)
309# endif
310 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000311 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000312#endif
313
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000314 /*
315 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100316 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000317 * Note that we may use mch_exit() before mch_init()!
318 */
319 mch_init();
320 TIME_MSG("shell init");
321
322#ifdef USE_XSMP
323 /*
324 * For want of anywhere else to do it, try to connect to xsmp here.
325 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
326 * Hijacking -X 'no X connection' to also disable XSMP connection as that
327 * has a similar delay upon failure.
328 * Only try if SESSION_MANAGER is set to something non-null.
329 */
330 if (!x_no_connect)
331 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000332 char *p = getenv("SESSION_MANAGER");
333
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000334 if (p != NULL && *p != NUL)
335 {
336 xsmp_init();
337 TIME_MSG("xsmp init");
338 }
339 }
340#endif
341
342 /*
343 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000344 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000345 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000346
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100347#ifdef _IOLBF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100348 // Ensure output works usefully without a tty: buffer lines instead of
349 // fully buffered.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100350 if (silent_mode)
351 setvbuf(stdout, NULL, _IOLBF, 0);
352#endif
353
Bram Moolenaar9404a182019-05-03 22:25:40 +0200354 // This message comes before term inits, but after setting "silent_mode"
355 // when the input is not a tty. Omit the message with --not-a-term.
356 if (GARGCOUNT > 1 && !silent_mode && !is_not_a_term())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000357 printf(_("%d files to edit\n"), GARGCOUNT);
358
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000359 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000360 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100361 termcapinit(params.term); // set terminal name and get terminal
362 // capabilities (will set full_screen)
363 screen_start(); // don't know where cursor is now
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000364 TIME_MSG("Termcap init");
365 }
366
367 /*
368 * Set the default values for the options that use Rows and Columns.
369 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100370 ui_get_shellsize(); // inits Rows and Columns
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000371 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000372#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100373 // Set the 'diff' option now, so that it can be checked for in a .vimrc
374 // file. There is no buffer yet though.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000375 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000376 diff_win_options(firstwin, FALSE);
377#endif
378
379 cmdline_row = Rows - p_ch;
380 msg_row = cmdline_row;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100381 screenalloc(FALSE); // allocate screen buffers
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000382 set_init_2();
383 TIME_MSG("inits 2");
384
385 msg_scroll = TRUE;
386 no_wait_return = TRUE;
387
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100388 init_mappings(); // set up initial mappings
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000389
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100390 init_highlight(TRUE, FALSE); // set the default highlight groups
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000391 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000392
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200393#if defined(FEAT_TERMRESPONSE)
394 init_term_props(TRUE);
395#endif
396
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000397#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100398 // Set the break level after the terminal is initialized.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000399 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000400#endif
401
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100402 // Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
403 // Allows for setting 'loadplugins' there.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200404 if (params.use_vimrc != NULL
405 && (STRCMP(params.use_vimrc, "NONE") == 0
406 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
407 p_lpl = FALSE;
408
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100409 // Execute --cmd arguments.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200410 exe_pre_commands(&params);
411
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100412 // Source startup scripts.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200413 source_startup_scripts(&params);
414
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100415#ifdef FEAT_MZSCHEME
416 /*
417 * Newer version of MzScheme (Racket) require earlier (trampolined)
418 * initialisation via scheme_main_setup.
419 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200420 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200421 * Do source startup scripts, so that 'mzschemedll' can be set.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100422 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200423 return mzscheme_main();
424#else
425 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100426#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200427}
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100428#endif // NO_VIM_MAIN
429#endif // PROTO
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100430
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200431/*
432 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
433 * things simple.
434 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
435 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100436 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200437vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100438{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100439#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000440#ifdef FEAT_EVAL
441 /*
442 * Read all the plugin files.
443 * Only when compiled with +eval, since most plugins need it.
444 */
445 if (p_lpl)
446 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200447 char_u *rtp_copy = NULL;
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100448 char_u *plugin_pattern = (char_u *)
449# if defined(VMS) || defined(AMIGA) // VMS and Amiga don't handle the "**".
450 "plugin/*.vim"
451# else
452 "plugin/**/*.vim"
453# endif
454 ;
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200455
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100456 // First add all package directories to 'runtimepath', so that their
457 // autoload directories can be found. Only if not done already with a
458 // :packloadall command.
459 // Make a copy of 'runtimepath', so that source_runtime does not use
460 // the pack directories.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200461 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200462 {
463 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200464 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200465 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200466
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100467 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy, plugin_pattern,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468 DIP_ALL | DIP_NOAFTER, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000469 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200470 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100471
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100472 // Only source "start" packages if not done already with a :packloadall
473 // command.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200474 if (!did_source_packages)
475 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100476 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200477
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100478 source_runtime(plugin_pattern, DIP_ALL | DIP_AFTER);
Bram Moolenaar66459b72016-08-06 19:01:55 +0200479 TIME_MSG("loading after plugins");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000480 }
481#endif
482
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000483#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100484 // Decide about window layout for diff mode after reading vimrc.
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000485 if (params.diff_mode && params.window_layout == 0)
486 {
487 if (diffopt_horizontal())
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100488 params.window_layout = WIN_HOR; // use horizontal split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000489 else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100490 params.window_layout = WIN_VER; // use vertical split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000491 }
492#endif
493
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000494 /*
495 * Recovery mode without a file name: List swap files.
496 * This uses the 'dir' option, therefore it must be after the
497 * initializations.
498 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200499 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000500 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200501 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000502 mch_exit(0);
503 }
504
505 /*
506 * Set a few option defaults after reading .vimrc files:
507 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
508 */
509 set_init_3();
510 TIME_MSG("inits 3");
511
512 /*
513 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
514 * Note that this overrides anything from a vimrc file.
515 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000516 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000517 p_uc = 0;
518
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000519#ifdef FEAT_GUI
520 if (gui.starting)
521 {
Bram Moolenaarc7226682019-08-17 16:33:23 +0200522# if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100523 // When something caused a message from a vimrc script, need to output
524 // an extra newline before the shell prompt.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000525 if (did_emsg || msg_didout)
526 putchar('\n');
Bram Moolenaarc7226682019-08-17 16:33:23 +0200527# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000528
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100529 gui_start(NULL); // will set full_screen to TRUE
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000530 TIME_MSG("starting GUI");
531
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100532 // When running "evim" or "gvim -y" we need the menus, exit if we
533 // don't have them.
Bram Moolenaar58d98232005-07-23 22:25:46 +0000534 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000535 mch_exit(1);
536 }
537#endif
538
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000539#ifdef FEAT_VIMINFO
540 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000541 * Read in registers, history etc, but not marks, from the viminfo file.
542 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000543 */
544 if (*p_viminfo != NUL)
545 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000546 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000547 TIME_MSG("reading viminfo");
548 }
549#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100550#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100551 // It's better to make v:oldfiles an empty list than NULL.
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100552 if (get_vim_var_list(VV_OLDFILES) == NULL)
553 set_vim_var_list(VV_OLDFILES, list_alloc());
554#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000555
556#ifdef FEAT_QUICKFIX
557 /*
558 * "-q errorfile": Load the error file now.
559 * If the error file can't be read, exit before doing anything else.
560 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000561 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000562 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100563 char_u *enc = NULL;
564
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100565 enc = p_menc;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000566 if (params.use_ef != NULL)
567 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000568 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200569 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100570 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000571 {
572 out_char('\n');
573 mch_exit(3);
574 }
575 TIME_MSG("reading errorfile");
576 }
577#endif
578
579 /*
580 * Start putting things on the screen.
581 * Scroll screen down before drawing over it
582 * Clear screen now, so file message will not be cleared.
583 */
584 starting = NO_BUFFERS;
585 no_wait_return = FALSE;
586 if (!exmode_active)
587 msg_scroll = FALSE;
588
589#ifdef FEAT_GUI
590 /*
591 * This seems to be required to make callbacks to be called now, instead
592 * of after things have been put on the screen, which then may be deleted
593 * when getting a resize callback.
594 * For the Mac this handles putting files dropped on the Vim icon to
595 * global_alist.
596 */
597 if (gui.in_use)
598 {
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100599 gui_wait_for_chars(50L, typebuf.tb_change_cnt);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000600 TIME_MSG("GUI delay");
601 }
602#endif
603
604#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
605 qnx_clip_init();
606#endif
607
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200608#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
609 clip_init(TRUE);
610#endif
611
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000612#ifdef FEAT_XCLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100613 // Start using the X clipboard, unless the GUI was started.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000614# ifdef FEAT_GUI
615 if (!gui.in_use)
616# endif
617 {
618 setup_term_clip();
619 TIME_MSG("setup clipboard");
620 }
621#endif
622
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000623#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100624 // Prepare for being a Vim server.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000625 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000626#endif
627
628 /*
629 * If "-" argument given: Read file from stdin.
630 * Do this before starting Raw mode, because it may change things that the
631 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
632 * are the same terminal: "cat | vim -".
633 * Using autocommands here may cause trouble...
634 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000635 if (params.edit_type == EDIT_STDIN && !recoverymode)
636 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000637
638#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100639 // When switching screens and something caused a message from a vimrc
640 // script, need to output an extra newline on exit.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000641 if ((did_emsg || msg_didout) && *T_TI != NUL)
642 newline_on_exit = TRUE;
643#endif
644
645 /*
K.Takataeeec2542021-06-02 13:28:16 +0200646 * When done something that is not allowed or given an error message call
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000647 * wait_return. This must be done before starttermcap(), because it may
648 * switch to another screen. It must be done after settmode(TMODE_RAW),
649 * because we want to react on a single key stroke.
650 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000651 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000652 */
653 settmode(TMODE_RAW);
654 TIME_MSG("setting raw mode");
655
656 if (need_wait_return || msg_didany)
657 {
658 wait_return(TRUE);
659 TIME_MSG("waiting for return");
660 }
661
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100662 starttermcap(); // start termcap if not done by wait_return()
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000663 TIME_MSG("start termcap");
664
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200665 setmouse(); // may start using the mouse
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000666 if (scroll_region)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100667 scroll_region_reset(); // In case Rows changed
668 scroll_start(); // may scroll the screen to the right position
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000669
Bram Moolenaar980bab42018-08-07 22:42:53 +0200670#if defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar40385db2018-08-07 22:31:44 +0200671 term_push_title(SAVE_RESTORE_BOTH);
672#endif
673
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000674 /*
675 * Don't clear the screen when starting in Ex mode, unless using the GUI.
676 */
677 if (exmode_active
678#ifdef FEAT_GUI
679 && !gui.in_use
680#endif
681 )
682 must_redraw = CLEAR;
683 else
684 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100685 screenclear(); // clear screen
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000686 TIME_MSG("clearing screen");
687 }
688
689#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000690 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000691 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100692 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200693 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000694 TIME_MSG("getting crypt key");
695 }
696#endif
697
698 no_wait_return = TRUE;
699
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000700 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000701 * Create the requested number of windows and edit buffers in them.
702 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000703 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000704 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000705 TIME_MSG("opening buffers");
706
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000707#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100708 // clear v:swapcommand
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000709 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
710#endif
711
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100712 // Ex starts at last line of the file
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000713 if (exmode_active)
714 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
715
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000716 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
717 TIME_MSG("BufEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000718 setpcmark();
719
720#ifdef FEAT_QUICKFIX
721 /*
722 * When started with "-q errorfile" jump to first error now.
723 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000724 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000725 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000726 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000727 TIME_MSG("jump to first error");
728 }
729#endif
730
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000731 /*
732 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000733 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000734 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200735 edit_buffers(&params, start_dir);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200736 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000737
738#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000739 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000740 {
741 win_T *wp;
742
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100743 // set options in each window for "vimdiff".
Bram Moolenaar29323592016-07-24 22:04:11 +0200744 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000745 diff_win_options(wp, TRUE);
746 }
747#endif
748
749 /*
750 * Shorten any of the filenames, but only when absolute.
751 */
752 shorten_fnames(FALSE);
753
754 /*
755 * Need to jump to the tag before executing the '-c command'.
756 * Makes "vim -c '/return' -t main" work.
757 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000758 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000759 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000760 swap_exists_did_quit = FALSE;
Bram Moolenaar146522e2005-12-16 21:55:46 +0000761
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000762 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000763 do_cmdline_cmd(IObuff);
764 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000765
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100766 // If the user doesn't want to edit the file then we quit here.
Bram Moolenaar146522e2005-12-16 21:55:46 +0000767 if (swap_exists_did_quit)
768 getout(1);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000769 }
770
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100771 // Execute any "+", "-c" and "-S" arguments.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000772 if (params.n_commands > 0)
773 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000774
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100775 // Must come before the may_req_ calls.
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200776 starting = 0;
777
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100778#if defined(FEAT_TERMRESPONSE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100779 // Must be done before redrawing, puts a few characters on the screen.
Bram Moolenaara45551a2020-06-09 15:57:37 +0200780 check_terminal_behavior();
Bram Moolenaar976787d2017-06-04 15:45:50 +0200781#endif
782
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000783 RedrawingDisabled = 0;
784 redraw_all_later(NOT_VALID);
785 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000786
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100787 // 'autochdir' has been postponed
Bram Moolenaar6f470022018-04-10 18:47:20 +0200788 DO_AUTOCHDIR;
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200789
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000790#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100791 // Requesting the termresponse is postponed until here, so that a "-c q"
792 // argument doesn't make it appear in the shell Vim was started from.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000793 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200794
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200795 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000796#endif
797
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100798 // start in insert mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000799 if (p_im)
800 need_start_insertmode = TRUE;
801
Bram Moolenaar14735512016-03-26 21:00:08 +0100802#ifdef FEAT_EVAL
803 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
804#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000805 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
806 TIME_MSG("VimEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000807
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200808#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100809 // Adjust default register name for "unnamed" in 'clipboard'. Can only be
810 // done after the clipboard is available and all initial commands that may
811 // modify the 'clipboard' setting have run; i.e. just before entering the
812 // main loop.
Bram Moolenaar439c0362020-06-06 15:58:03 +0200813 reset_reg_var();
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200814#endif
815
Bram Moolenaar8a3bb562018-03-04 20:14:14 +0100816#if defined(FEAT_DIFF)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100817 // When a startup script or session file setup for diff'ing and
818 // scrollbind, sync the scrollbind now.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000819 if (curwin->w_p_diff && curwin->w_p_scb)
820 {
821 update_topline();
822 check_scrollbind((linenr_T)0, 0L);
823 TIME_MSG("diff scrollbinding");
824 }
825#endif
826
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200827#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
828# ifdef VIMDLL
829 if (!gui.in_use)
830# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100831 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000832#endif
833
Bram Moolenaar4033c552017-09-16 20:54:51 +0200834#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100835 // When tab pages were created, may need to update the tab pages line and
836 // scrollbars. This is skipped while creating them.
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000837 if (first_tabpage->tp_next != NULL)
838 {
839 out_flush();
840 gui_init_which_components(NULL);
841 gui_update_scrollbars(TRUE);
842 }
843 need_mouse_correct = TRUE;
844#endif
845
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100846 // If ":startinsert" command used, stuff a dummy command to be able to
847 // call normal_cmd(), which will then start Insert mode.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000848 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000849 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000850
851#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200852 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200853 {
854# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200855# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar4f974752019-02-17 17:44:42 +0100856 && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200857 if (gui.in_use)
858 {
859 mch_errmsg(_("netbeans is not supported with this GUI\n"));
860 mch_exit(2);
861 }
862# endif
863# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100864 // Tell the client that it can start sending commands.
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200865 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200866 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000867#endif
868
Bram Moolenaare7ddf4e2020-02-03 21:29:30 +0100869 // Redraw at least once, also when 'lazyredraw' is set, to make sure the
870 // window title gets updated.
871 do_redraw = TRUE;
872
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000873 TIME_MSG("before starting main loop");
874
875 /*
876 * Call the main command loop. This never returns.
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200877 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000878 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000879
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100880#endif // NO_VIM_MAIN
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200881
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000882 return 0;
883}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000884
885/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200886 * Initialisation shared by main() and some tests.
887 */
888 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200889common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200890{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100891 estack_init();
Bram Moolenaar438d1762018-09-30 17:11:48 +0200892 cmdline_init();
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200893
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100894 (void)mb_init(); // init mb_bytelen_tab[] to ones
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200895#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100896 eval_init(); // init global variables
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200897#endif
898
899#ifdef __QNXNTO__
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100900 qnx_init(); // PhAttach() for clipboard, (and gui)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200901#endif
902
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100903 // Init the table of Normal mode commands.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200904 init_normal_cmds();
905
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200906 /*
907 * Allocate space for the generic buffers (needed for set_init_1() and
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100908 * emsg()).
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200909 */
910 if ((IObuff = alloc(IOSIZE)) == NULL
911 || (NameBuff = alloc(MAXPATHL)) == NULL)
912 mch_exit(0);
913 TIME_MSG("Allocated generic buffers");
914
915#ifdef NBDEBUG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100916 // Wait a moment for debugging NetBeans. Must be after allocating
917 // NameBuff.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200918 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
919 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
920 TIME_MSG("NetBeans debug wait");
921#endif
922
923#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
924 /*
925 * Setup to use the current locale (for ctype() and many other things).
926 * NOTE: Translated messages with encodings other than latin1 will not
927 * work until set_init_1() has been called!
928 */
929 init_locale();
930 TIME_MSG("locale set");
931#endif
932
933#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100934 gui.dofork = TRUE; // default is to use fork()
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200935#endif
936
937 /*
938 * Do a first scan of the arguments in "argv[]":
939 * -display or --display
940 * --server...
941 * --socketid
942 * --windowid
943 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200944 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200945
Bram Moolenaard0573012017-10-28 21:11:06 +0200946#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100947 // Prepare for possibly starting GUI sometime
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200948 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200949 TIME_MSG("GUI prepared");
950#endif
951
952#ifdef FEAT_CLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100953 clip_init(FALSE); // Initialise clipboard stuff
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200954 TIME_MSG("clipboard setup");
955#endif
956
957 /*
958 * Check if we have an interactive window.
959 * On the Amiga: If there is no window, we open one with a newcli command
960 * (needed for :! to * work). mch_check_win() will also handle the -d or
961 * -dev argument.
962 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100963 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200964 TIME_MSG("window checked");
965
966 /*
967 * Allocate the first window and buffer.
968 * Can't do anything without it, exit when it fails.
969 */
970 if (win_alloc_first() == FAIL)
971 mch_exit(0);
972
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100973 init_yank(); // init yank buffers
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200974
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100975 alist_init(&global_alist); // Init the argument list to empty.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200976 global_alist.id = 0;
977
978 /*
979 * Set the default values for the options.
980 * NOTE: Non-latin1 translated messages are working only after this,
981 * because this is where "has_mbyte" will be set, which is used by
982 * msg_outtrans_len_attr().
983 * First find out the home directory, needed to expand "~" in options.
984 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100985 init_homedir(); // find real value of $HOME
Bram Moolenaar07268702018-03-01 21:57:32 +0100986 set_init_1(paramp->clean);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200987 TIME_MSG("inits 1");
988
989#ifdef FEAT_EVAL
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100990 // set v:lang and v:ctype
991 set_lang_var();
992
993 // set v:argv
994 set_argv_var(paramp->argv, paramp->argc);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200995#endif
Bram Moolenaar6436cd82018-12-27 00:28:33 +0100996
997#ifdef FEAT_SIGNS
998 init_signs();
999#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001000}
1001
1002/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001003 * Return TRUE when the --not-a-term argument was found.
1004 */
1005 int
1006is_not_a_term()
1007{
1008 return params.not_a_term;
1009}
1010
Bram Moolenaar7007e312021-03-27 12:11:33 +01001011/*
1012 * Return TRUE when the --not-a-term argument was found or the GUI is in use.
1013 */
1014 static int
1015is_not_a_term_or_gui()
1016{
1017 return params.not_a_term
1018#ifdef FEAT_GUI
1019 || gui.in_use
1020#endif
1021 ;
1022}
1023
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001024
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001025// When TRUE in a safe state when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001026static int was_safe = FALSE;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001027static oparg_T *current_oap = NULL;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001028
1029/*
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001030 * Return TRUE if an operator was started but not finished yet.
1031 * Includes typing a count or a register name.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001032 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001033 int
1034op_pending(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001035{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001036 return !(current_oap != NULL
1037 && !finish_op
1038 && current_oap->prev_opcount == 0
1039 && current_oap->prev_count0 == 0
1040 && current_oap->op_type == OP_NOP
1041 && current_oap->regname == NUL);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001042}
1043
1044/*
Bram Moolenaard103ee72019-09-18 21:15:31 +02001045 * Return whether currently it is safe, assuming it was safe before (high level
1046 * state didn't change).
1047 */
1048 static int
1049is_safe_now(void)
1050{
1051 return stuff_empty()
1052 && typebuf.tb_len == 0
1053 && scriptin[curscript] == NULL
1054 && !global_busy;
1055}
1056
1057/*
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001058 * Trigger SafeState if currently in s safe state, that is "safe" is TRUE and
1059 * there is no typeahead.
1060 */
1061 void
1062may_trigger_safestate(int safe)
1063{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001064 int is_safe = safe && is_safe_now();
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001065
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001066#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001067 if (was_safe != is_safe)
1068 // Only log when the state changes, otherwise it happens at nearly
1069 // every key stroke.
Bram Moolenaard103ee72019-09-18 21:15:31 +02001070 ch_log(NULL, is_safe ? "SafeState: Start triggering"
1071 : "SafeState: Stop triggering");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001072#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001073 if (is_safe)
1074 apply_autocmds(EVENT_SAFESTATE, NULL, NULL, FALSE, curbuf);
1075 was_safe = is_safe;
1076}
1077
1078/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001079 * Something changed which causes the state possibly to be unsafe, e.g. a
1080 * character was typed. It will remain unsafe until the next call to
1081 * may_trigger_safestate().
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001082 */
1083 void
Bram Moolenaard103ee72019-09-18 21:15:31 +02001084state_no_longer_safe(char *reason UNUSED)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001085{
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001086#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001087 if (was_safe)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001088 ch_log(NULL, "SafeState: reset: %s", reason);
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001089#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001090 was_safe = FALSE;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001091}
1092
Bram Moolenaard103ee72019-09-18 21:15:31 +02001093 int
1094get_was_safe_state(void)
1095{
1096 return was_safe;
1097}
1098
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001099/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001100 * Invoked when leaving code that invokes callbacks. Then trigger
1101 * SafeStateAgain, if it was safe when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001102 */
1103 void
Bram Moolenaar37d18072019-09-17 20:28:38 +02001104may_trigger_safestateagain(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001105{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001106 if (!was_safe)
1107 {
1108 // If the safe state was reset in state_no_longer_safe(), e.g. because
1109 // of calling feedkeys(), we check if it's now safe again (all keys
1110 // were consumed).
1111 was_safe = is_safe_now();
1112#ifdef FEAT_JOB_CHANNEL
1113 if (was_safe)
1114 ch_log(NULL, "SafeState: undo reset");
1115#endif
1116 }
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001117 if (was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02001118 {
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001119#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar101e9922019-09-25 21:43:11 +02001120 // Only do this message when another message was given, otherwise we
1121 // get lots of them.
1122 if ((did_repeated_msg & REPEATED_MSG_SAFESTATE) == 0)
1123 {
1124 int did = did_repeated_msg;
1125
1126 ch_log(NULL,
1127 "SafeState: back to waiting, triggering SafeStateAgain");
1128 did_repeated_msg = did | REPEATED_MSG_SAFESTATE;
1129 }
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001130#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001131 apply_autocmds(EVENT_SAFESTATEAGAIN, NULL, NULL, FALSE, curbuf);
Bram Moolenaar37d18072019-09-17 20:28:38 +02001132 }
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001133#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001134 else
Bram Moolenaard103ee72019-09-18 21:15:31 +02001135 ch_log(NULL,
1136 "SafeState: back to waiting, not triggering SafeStateAgain");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001137#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001138}
1139
1140
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001141/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001142 * Main loop: Execute Normal mode commands until exiting Vim.
1143 * Also used to handle commands in the command-line window, until the window
1144 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 * Also used to handle ":visual" command after ":global": execute Normal mode
1146 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001147 */
1148 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001149main_loop(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001150 int cmdwin, // TRUE when working in the command-line window
1151 int noexmode) // TRUE when return on entering Ex mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001152{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001153 oparg_T oa; // operator arguments
1154 oparg_T *prev_oap; // operator arguments
1155 volatile int previous_got_int = FALSE; // "got_int" was TRUE
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001156#ifdef FEAT_CONCEAL
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001157 // these are static to avoid a compiler warning
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001158 static linenr_T conceal_old_cursor_line = 0;
1159 static linenr_T conceal_new_cursor_line = 0;
1160 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001161#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001162
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001163 prev_oap = current_oap;
1164 current_oap = &oa;
1165
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001166#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001167 // Setup to catch a terminating error from the X server. Just ignore
1168 // it, restore the state and continue. This might not always work
1169 // properly, but at least we don't exit unexpectedly when the X server
1170 // exits while Vim is running in a console.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001172 {
1173 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001174 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001175 got_int = TRUE;
1176 need_wait_return = FALSE;
1177 global_busy = FALSE;
1178 exmode_active = 0;
1179 skip_redraw = FALSE;
1180 RedrawingDisabled = 0;
1181 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001182 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001183# ifdef FEAT_EVAL
1184 emsg_skip = 0;
1185# endif
1186 emsg_off = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001187 setmouse();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001188 settmode(TMODE_RAW);
1189 starttermcap();
1190 scroll_start();
1191 redraw_later_clear();
1192 }
1193#endif
1194
1195 clear_oparg(&oa);
1196 while (!cmdwin
1197#ifdef FEAT_CMDWIN
1198 || cmdwin_result == 0
1199#endif
1200 )
1201 {
1202 if (stuff_empty())
1203 {
1204 did_check_timestamps = FALSE;
1205 if (need_check_timestamps)
1206 check_timestamps(FALSE);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001207 if (need_wait_return) // if wait_return still needed ...
1208 wait_return(FALSE); // ... call it now
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001209 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001210 {
1211 need_start_insertmode = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001212 stuffReadbuff((char_u *)"i"); // start insert mode next
1213 // skip the fileinfo message now, because it would be shown
1214 // after insert mode finishes!
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001215 need_fileinfo = FALSE;
1216 }
1217 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001218
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001219 // Reset "got_int" now that we got back to the main loop. Except when
1220 // inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1221 // the ":g" command.
1222 // For ":g/pat/vi" we reset "got_int" when used once. When used
1223 // a second time we go back to Ex mode and abort the ":g" command.
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001224 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001225 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001226 if (noexmode && global_busy && !exmode_active && previous_got_int)
1227 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001228 // Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1229 // used and keep "got_int" set, so that it aborts ":g".
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001230 exmode_active = EXMODE_NORMAL;
1231 State = NORMAL;
1232 }
1233 else if (!global_busy || !exmode_active)
1234 {
1235 if (!quit_more)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001236 (void)vgetc(); // flush all buffers
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001237 got_int = FALSE;
1238 }
1239 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001240 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001241 else
1242 previous_got_int = FALSE;
1243
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001244 if (!exmode_active)
1245 msg_scroll = FALSE;
1246 quit_more = FALSE;
1247
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001248 // it's not safe unless may_trigger_safestate_main() is called
1249 was_safe = FALSE;
1250
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001251 /*
1252 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1253 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1254 * update cursor and redraw.
1255 */
1256 if (skip_redraw || exmode_active)
1257 skip_redraw = FALSE;
1258 else if (do_redraw || stuff_empty())
1259 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001260#ifdef FEAT_GUI
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001261 // If ui_breakcheck() was used a resize may have been postponed.
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001262 gui_may_resize_shell();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001263#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001264#ifdef HAVE_DROP_FILE
1265 // If files were dropped while text was locked or the curbuf was
1266 // locked, this would be a good time to handle the drop.
1267 handle_any_postponed_drop();
1268#endif
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001269#ifdef FEAT_CONCEAL
1270 if (curwin->w_p_cole == 0)
1271 conceal_update_lines = FALSE;
1272#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001273
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001274 // Trigger CursorMoved if the cursor moved.
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001275 if (!finish_op && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001276 has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001277#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001278 || popup_visible
1279#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001280#ifdef FEAT_CONCEAL
1281 || curwin->w_p_cole > 0
1282#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001283 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001284 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001285 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001286 if (has_cursormoved())
1287 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1288 FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001289#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001290 if (popup_visible)
1291 popup_check_cursor_pos();
1292#endif
1293#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001294 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001295 {
1296 conceal_old_cursor_line = last_cursormoved.lnum;
1297 conceal_new_cursor_line = curwin->w_cursor.lnum;
1298 conceal_update_lines = TRUE;
1299 }
Bram Moolenaar3397f742019-06-02 18:40:06 +02001300#endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001301 last_cursormoved = curwin->w_cursor;
1302 }
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001303
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001304#if defined(FEAT_CONCEAL)
1305 if (conceal_update_lines
1306 && (conceal_old_cursor_line != conceal_new_cursor_line
1307 || conceal_cursor_line(curwin)
1308 || need_cursor_line_redraw))
1309 {
1310 if (conceal_old_cursor_line != conceal_new_cursor_line
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001311 && conceal_old_cursor_line != 0
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001312 && conceal_old_cursor_line
1313 <= curbuf->b_ml.ml_line_count)
1314 redrawWinline(curwin, conceal_old_cursor_line);
1315 redrawWinline(curwin, conceal_new_cursor_line);
1316 curwin->w_valid &= ~VALID_CROW;
1317 need_cursor_line_redraw = FALSE;
1318 }
1319#endif
1320
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001321 // Trigger TextChanged if b:changedtick differs.
Bram Moolenaar186628f2013-03-19 13:33:23 +01001322 if (!finish_op && has_textchanged()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001323 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001324 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001325 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL, FALSE, curbuf);
1326 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001327 }
Bram Moolenaar186628f2013-03-19 13:33:23 +01001328
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001329 // If nothing is pending and we are going to wait for the user to
1330 // type a character, trigger SafeState.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001331 may_trigger_safestate(!op_pending() && restart_edit == 0);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001332
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01001333#if defined(FEAT_DIFF)
Bram Moolenaare3521d92018-09-16 14:10:31 +02001334 // Updating diffs from changed() does not always work properly,
1335 // esp. updating folds. Do an update just before redrawing if
1336 // needed.
1337 if (curtab->tp_diff_update || curtab->tp_diff_invalid)
1338 {
1339 ex_diffupdate(NULL);
1340 curtab->tp_diff_update = FALSE;
1341 }
1342
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001343 // Scroll-binding for diff mode may have been postponed until
1344 // here. Avoids doing it for every change.
Bram Moolenaar33aec762006-01-22 23:30:12 +00001345 if (diff_need_scrollbind)
1346 {
1347 check_scrollbind((linenr_T)0, 0L);
1348 diff_need_scrollbind = FALSE;
1349 }
1350#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001351#if defined(FEAT_FOLDING)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001352 // Include a closed fold completely in the Visual area.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001353 foldAdjustVisual();
1354#endif
1355#ifdef FEAT_FOLDING
1356 /*
1357 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1358 * contain the cursor.
1359 * When 'foldopen' is "all", open the fold(s) under the cursor.
1360 * This may mark the window for redrawing.
1361 */
1362 if (hasAnyFolding(curwin) && !char_avail())
1363 {
1364 foldCheckClose();
1365 if (fdo_flags & FDO_ALL)
1366 foldOpenCursor();
1367 }
1368#endif
1369
1370 /*
1371 * Before redrawing, make sure w_topline is correct, and w_leftcol
1372 * if lines don't wrap, and w_skipcol if lines wrap.
1373 */
1374 update_topline();
1375 validate_cursor();
1376
Bram Moolenaar017ba072019-09-14 21:01:23 +02001377#ifdef FEAT_SYN_HL
Bram Moolenaar11a58af2019-10-24 22:32:31 +02001378 // Might need to update for 'cursorline'.
1379 // When 'cursorlineopt' is "screenline" need to redraw always.
1380 if (curwin->w_p_cul
1381 && (curwin->w_last_cursorline != curwin->w_cursor.lnum
1382 || (curwin->w_p_culopt_flags & CULOPT_SCRLINE))
1383 && !char_avail())
1384 redraw_later(VALID);
Bram Moolenaar017ba072019-09-14 21:01:23 +02001385#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001386 if (VIsual_active)
Bram Moolenaar017ba072019-09-14 21:01:23 +02001387 update_curbuf(INVERTED); // update inverted part
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001388 else if (must_redraw)
Bram Moolenaara338adc2018-01-31 20:51:47 +01001389 {
Bram Moolenaar017ba072019-09-14 21:01:23 +02001390 mch_disable_flush(); // Stop issuing gui_mch_flush().
Bram Moolenaar11a58af2019-10-24 22:32:31 +02001391 update_screen(0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01001392 mch_enable_flush();
1393 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001394 else if (redraw_cmdline || clear_cmdline)
1395 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001396 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001397#ifdef FEAT_TITLE
1398 if (need_maketitle)
1399 maketitle();
1400#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001401#ifdef FEAT_VIMINFO
1402 curbuf->b_last_used = vim_time();
1403#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001404 // display message after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001405 if (keep_msg != NULL)
1406 {
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001407 char_u *p = vim_strsave(keep_msg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001408
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001409 if (p != NULL)
1410 {
1411 // msg_start() will set keep_msg to NULL, make a copy
1412 // first. Don't reset keep_msg, msg_attr_keep() uses it to
1413 // check for duplicates. Never put this message in
1414 // history.
1415 msg_hist_off = TRUE;
1416 msg_attr((char *)p, keep_msg_attr);
1417 msg_hist_off = FALSE;
1418 vim_free(p);
1419 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001420 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001421 if (need_fileinfo) // show file info after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001422 {
1423 fileinfo(FALSE, TRUE, FALSE);
1424 need_fileinfo = FALSE;
1425 }
1426
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001427 emsg_on_display = FALSE; // can delete error message now
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001428 did_emsg = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001429 msg_didany = FALSE; // reset lines_left in msg_start()
1430 may_clear_sb_text(); // clear scroll-back text on next msg
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001431 showruler(FALSE);
1432
1433 setcursor();
1434 cursor_on();
1435
1436 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001437
1438#ifdef STARTUPTIME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001439 // Now that we have drawn the first screen all the startup stuff
1440 // has been done, close any file for startup messages.
Bram Moolenaar3f269672009-11-03 11:11:11 +00001441 if (time_fd != NULL)
1442 {
1443 TIME_MSG("first screen update");
1444 TIME_MSG("--- VIM STARTED ---");
1445 fclose(time_fd);
1446 time_fd = NULL;
1447 }
1448#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001449 }
1450#ifdef FEAT_GUI
1451 if (need_mouse_correct)
1452 gui_mouse_correct();
1453#endif
1454
1455 /*
1456 * Update w_curswant if w_set_curswant has been set.
1457 * Postponed until here to avoid computing w_virtcol too often.
1458 */
1459 update_curswant();
1460
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001461#ifdef FEAT_EVAL
1462 /*
1463 * May perform garbage collection when waiting for a character, but
1464 * only at the very toplevel. Otherwise we may be using a List or
1465 * Dict internally somewhere.
1466 * "may_garbage_collect" is reset in vgetc() which is invoked through
1467 * do_exmode() and normal_cmd().
1468 */
1469 may_garbage_collect = (!cmdwin && !noexmode);
1470#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001471 /*
1472 * If we're invoked as ex, do a round of ex commands.
1473 * Otherwise, get and execute a normal mode command.
1474 */
1475 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001476 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001477 if (noexmode) // End of ":global/path/visual" commands
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001478 goto theend;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001479 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001480 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001481 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001482 {
1483#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001484 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001485 && oa.op_type == OP_NOP && oa.regname == NUL
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001486 && !VIsual_active
1487 && !skip_term_loop)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001488 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001489 // If terminal_loop() returns OK we got a key that is handled
1490 // in Normal model. With FAIL we first need to position the
1491 // cursor and the screen needs to be redrawn.
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001492 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001493 normal_cmd(&oa, TRUE);
1494 }
1495 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001496#endif
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001497 {
1498#ifdef FEAT_TERMINAL
1499 skip_term_loop = FALSE;
1500#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001501 normal_cmd(&oa, TRUE);
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001502 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02001503 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001504 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001505
1506theend:
1507 current_oap = prev_oap;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001508}
1509
1510
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001511#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001512/*
1513 * Exit, but leave behind swap files for modified buffers.
1514 */
1515 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001516getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001517{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001518# if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001519 // Ignore SIGHUP, because a dropped connection causes a read error, which
1520 // makes Vim exit and then handling SIGHUP causes various reentrance
1521 // problems.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001522 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001523# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001524
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001525 ml_close_notmod(); // close all not-modified buffers
1526 ml_sync_all(FALSE, FALSE); // preserve all swap files
1527 ml_close_all(FALSE); // close all memfiles, without deleting
1528 getout(exitval); // exit Vim properly
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001529}
1530#endif
1531
1532
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001533/*
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001534 * Exit properly. This is the only way to exit Vim after startup has
1535 * succeeded. We are certain to exit here, no way to abort it.
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001536 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001537 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001538getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001539{
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001540 exiting = TRUE;
Bram Moolenaar0bd052b2018-03-22 20:33:56 +01001541#if defined(FEAT_JOB_CHANNEL)
1542 ch_log(NULL, "Exiting...");
1543#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001544
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001545 // When running in Ex mode an error causes us to exit with a non-zero exit
1546 // code. POSIX requires this, although it's not 100% clear from the
1547 // standard.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001548 if (exmode_active)
1549 exitval += ex_exitval;
1550
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001551#ifdef FEAT_EVAL
1552 set_vim_var_type(VV_EXITING, VAR_NUMBER);
1553 set_vim_var_nr(VV_EXITING, exitval);
1554#endif
1555
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001556 // Position the cursor on the last screen line, below all the text
Bram Moolenaar7007e312021-03-27 12:11:33 +01001557 if (!is_not_a_term_or_gui())
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001558 windgoto((int)Rows - 1, 0);
1559
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001560#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001561 // Optionally print hashtable efficiency.
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001562 hash_debug_results();
1563#endif
1564
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001565#ifdef FEAT_GUI
1566 msg_didany = FALSE;
1567#endif
1568
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001569 if (v_dying <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001570 {
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001571 tabpage_T *tp;
1572 tabpage_T *next_tp;
1573 buf_T *buf;
1574 win_T *wp;
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001575 int unblock = 0;
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001576
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001577 // Trigger BufWinLeave for all windows, but only once per buffer.
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001578 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001579 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001580 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001581 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001582 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001583 if (wp->w_buffer == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001584 // Autocmd must have close the buffer already, skip.
Bram Moolenaar802418d2013-01-17 14:00:11 +01001585 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001586 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001587 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001588 {
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001589 bufref_T bufref;
1590
1591 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001592 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1593 buf->b_fname, FALSE, buf);
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001594 if (bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001595 CHANGEDTICK(buf) = -1; // note we did it already
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001596
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001597 // start all over, autocommands may mess up the lists
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001598 next_tp = first_tabpage;
1599 break;
1600 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001601 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001602 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001603
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001604 // Trigger BufUnload for buffers that are loaded
Bram Moolenaar29323592016-07-24 22:04:11 +02001605 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001606 if (buf->b_ml.ml_mfp != NULL)
1607 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001608 bufref_T bufref;
1609
1610 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001611 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001612 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001613 if (!bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001614 // autocmd deleted the buffer
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001615 break;
1616 }
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001617
1618 // deathtrap() blocks autocommands, but we do want to trigger
1619 // VimLeavePre.
1620 if (is_autocmd_blocked())
1621 {
1622 unblock_autocmds();
1623 ++unblock;
1624 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001625 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001626 if (unblock)
1627 block_autocmds();
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001628 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001629
1630#ifdef FEAT_VIMINFO
1631 if (*p_viminfo != NUL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001632 // Write out the registers, history, marks etc, to the viminfo file
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001633 write_viminfo(NULL, FALSE);
1634#endif
1635
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001636 if (v_dying <= 1)
Bram Moolenaarc7226682019-08-17 16:33:23 +02001637 {
1638 int unblock = 0;
1639
1640 // deathtrap() blocks autocommands, but we do want to trigger VimLeave.
1641 if (is_autocmd_blocked())
1642 {
1643 unblock_autocmds();
1644 ++unblock;
1645 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001646 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc7226682019-08-17 16:33:23 +02001647 if (unblock)
1648 block_autocmds();
1649 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001650
Bram Moolenaar05159a02005-02-26 23:04:13 +00001651#ifdef FEAT_PROFILE
1652 profile_dump();
1653#endif
1654
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001655 if (did_emsg
1656#ifdef FEAT_GUI
1657 || (gui.in_use && msg_didany && p_verbose > 0)
1658#endif
1659 )
1660 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001661 // give the user a chance to read the (error) message
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001662 no_wait_return = FALSE;
K.Takataeeec2542021-06-02 13:28:16 +02001663// wait_return(FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001664 }
1665
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001666 // Position the cursor again, the autocommands may have moved it
Bram Moolenaar7007e312021-03-27 12:11:33 +01001667 if (!is_not_a_term_or_gui())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001668 windgoto((int)Rows - 1, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001669
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001670#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001671 job_stop_on_exit();
1672#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001673#ifdef FEAT_LUA
1674 lua_end();
1675#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001676#ifdef FEAT_MZSCHEME
1677 mzscheme_end();
1678#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001679#ifdef FEAT_TCL
1680 tcl_end();
1681#endif
1682#ifdef FEAT_RUBY
1683 ruby_end();
1684#endif
1685#ifdef FEAT_PYTHON
1686 python_end();
1687#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001688#ifdef FEAT_PYTHON3
1689 python3_end();
1690#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001691#ifdef FEAT_PERL
1692 perl_end();
1693#endif
1694#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1695 iconv_end();
1696#endif
1697#ifdef FEAT_NETBEANS_INTG
1698 netbeans_end();
1699#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001700#ifdef FEAT_CSCOPE
1701 cs_end();
1702#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001703#ifdef FEAT_EVAL
1704 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001705 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001706#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001707#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +02001708 free_cmd_argsW();
1709#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001710
1711 mch_exit(exitval);
1712}
1713
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001714/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001715 * Get the name of the display, before gui_prepare() removes it from
1716 * argv[]. Used for the xterm-clipboard display.
1717 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001718 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001719 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001720 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001721early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001722{
Bram Moolenaar03005972008-11-20 13:12:36 +00001723#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1724 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001725 int argc = parmp->argc;
1726 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001727 int i;
1728
1729 for (i = 1; i < argc; i++)
1730 {
1731 if (STRCMP(argv[i], "--") == 0)
1732 break;
1733# ifdef FEAT_XCLIPBOARD
1734 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001735# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001736 || STRICMP(argv[i], "--display") == 0
1737# endif
1738 )
1739 {
1740 if (i == argc - 1)
1741 mainerr_arg_missing((char_u *)argv[i]);
1742 xterm_display = argv[++i];
1743 }
1744# endif
1745# ifdef FEAT_CLIENTSERVER
1746 else if (STRICMP(argv[i], "--servername") == 0)
1747 {
1748 if (i == argc - 1)
1749 mainerr_arg_missing((char_u *)argv[i]);
1750 parmp->serverName_arg = (char_u *)argv[++i];
1751 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001752 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001753 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001754 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001755 {
1756 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001757# ifdef FEAT_GUI
1758 if (strstr(argv[i], "-wait") != 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001759 // don't fork() when starting the GUI to edit files ourself
Bram Moolenaareb94e552006-03-11 21:35:11 +00001760 gui.dofork = FALSE;
1761# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001762 }
1763# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001764
Bram Moolenaar4f974752019-02-17 17:44:42 +01001765# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
1766# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001767 else if (STRICMP(argv[i], "--windowid") == 0)
1768# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001769 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001770# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001771 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001772 long_u id;
1773 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001774
1775 if (i == argc - 1)
1776 mainerr_arg_missing((char_u *)argv[i]);
1777 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001778 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001779 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001780 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001781 if (count != 1)
1782 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1783 else
Bram Moolenaar4f974752019-02-17 17:44:42 +01001784# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001785 win_socket_id = id;
1786# else
1787 gtk_socket_id = id;
1788# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001789 i++;
1790 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001791# endif
1792# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001793 else if (STRICMP(argv[i], "--echo-wid") == 0)
1794 echo_wid_arg = TRUE;
1795# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001796# ifndef FEAT_NETBEANS_INTG
1797 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001798 {
1799 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1800 mch_exit(2);
1801 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001802# endif
1803
Bram Moolenaar58d98232005-07-23 22:25:46 +00001804 }
1805#endif
1806}
1807
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001808#ifndef NO_VIM_MAIN
1809/*
1810 * Get a (optional) count for a Vim argument.
1811 */
1812 static int
1813get_number_arg(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001814 char_u *p, // pointer to argument
1815 int *idx, // index in argument, is incremented
1816 int def) // default value
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001817{
1818 if (vim_isdigit(p[*idx]))
1819 {
1820 def = atoi((char *)&(p[*idx]));
1821 while (vim_isdigit(p[*idx]))
1822 *idx = *idx + 1;
1823 }
1824 return def;
1825}
1826
1827/*
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001828 * Check for: [r][e][g][vi|vim|view][diff][ex[im]] (sort of)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001829 * If the executable name starts with "r" we disable shell commands.
1830 * If the next character is "e" we run in Easy mode.
1831 * If the next character is "g" we run the GUI version.
1832 * If the next characters are "view" we start in readonly mode.
1833 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1834 * If the next characters are "ex" we start in Ex mode. If it's followed
1835 * by "im" use improved Ex mode.
1836 */
1837 static void
1838parse_command_name(mparm_T *parmp)
1839{
1840 char_u *initstr;
1841
1842 initstr = gettail((char_u *)parmp->argv[0]);
1843
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001844#ifdef FEAT_EVAL
1845 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001846 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001847#endif
1848
1849 if (TOLOWER_ASC(initstr[0]) == 'r')
1850 {
1851 restricted = TRUE;
1852 ++initstr;
1853 }
1854
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001855 // Use evim mode for "evim" and "egvim", not for "editor".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001856 if (TOLOWER_ASC(initstr[0]) == 'e'
1857 && (TOLOWER_ASC(initstr[1]) == 'v'
1858 || TOLOWER_ASC(initstr[1]) == 'g'))
1859 {
1860#ifdef FEAT_GUI
1861 gui.starting = TRUE;
1862#endif
1863 parmp->evim_mode = TRUE;
1864 ++initstr;
1865 }
1866
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001867 // "gvim" starts the GUI. Also accept "Gvim" for MS-Windows.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001868 if (TOLOWER_ASC(initstr[0]) == 'g')
1869 {
1870 main_start_gui();
1871#ifdef FEAT_GUI
1872 ++initstr;
1873#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001874#ifdef GUI_MAY_SPAWN
1875 gui.dospawn = FALSE; // No need to spawn a new process.
1876#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001877 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001878#ifdef GUI_MAY_SPAWN
1879 else
1880 gui.dospawn = TRUE; // Not "gvim". Need to spawn gvim.exe.
1881#endif
1882
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001883
1884 if (STRNICMP(initstr, "view", 4) == 0)
1885 {
1886 readonlymode = TRUE;
1887 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001888 p_uc = 10000; // don't update very often
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001889 initstr += 4;
1890 }
1891 else if (STRNICMP(initstr, "vim", 3) == 0)
1892 initstr += 3;
1893
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001894 // Catch "[r][g]vimdiff" and "[r][g]viewdiff".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001895 if (STRICMP(initstr, "diff") == 0)
1896 {
1897#ifdef FEAT_DIFF
1898 parmp->diff_mode = TRUE;
1899#else
1900 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1901 mch_errmsg("\n");
1902 mch_exit(2);
1903#endif
1904 }
1905
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001906 // Checking for "ex" here may catch some weird names, such as "vimex" or
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001907 // "viewex", we assume the user knows that.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001908 if (STRNICMP(initstr, "ex", 2) == 0)
1909 {
1910 if (STRNICMP(initstr + 2, "im", 2) == 0)
1911 exmode_active = EXMODE_VIM;
1912 else
1913 exmode_active = EXMODE_NORMAL;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001914 change_compatible(TRUE); // set 'compatible'
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001915 }
1916}
1917
Bram Moolenaar58d98232005-07-23 22:25:46 +00001918/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001919 * Scan the command line arguments.
1920 */
1921 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001922command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001923{
1924 int argc = parmp->argc;
1925 char **argv = parmp->argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001926 int argv_idx; // index in argv[n][]
1927 int had_minmin = FALSE; // found "--" argument
1928 int want_argument; // option argument with argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001929 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001930 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001931 long n;
1932
1933 --argc;
1934 ++argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001935 argv_idx = 1; // active option letter is argv[0][argv_idx]
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001936 while (argc > 0)
1937 {
1938 /*
1939 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1940 */
1941 if (argv[0][0] == '+' && !had_minmin)
1942 {
1943 if (parmp->n_commands >= MAX_ARG_CMDS)
1944 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001945 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001946 if (argv[0][1] == NUL)
1947 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1948 else
1949 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1950 }
1951
1952 /*
1953 * Optional argument.
1954 */
1955 else if (argv[0][0] == '-' && !had_minmin)
1956 {
1957 want_argument = FALSE;
1958 c = argv[0][argv_idx++];
1959#ifdef VMS
1960 /*
1961 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1962 * and "-/X" as "-X".
1963 */
1964 if (c == '/')
1965 {
1966 c = argv[0][argv_idx++];
1967 c = TOUPPER_ASC(c);
1968 }
1969 else
1970 c = TOLOWER_ASC(c);
1971#endif
1972 switch (c)
1973 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001974 case NUL: // "vim -" read from stdin
1975 // "ex -" silent mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001976 if (exmode_active)
1977 silent_mode = TRUE;
1978 else
1979 {
1980 if (parmp->edit_type != EDIT_NONE)
1981 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1982 parmp->edit_type = EDIT_STDIN;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001983 read_cmd_fd = 2; // read from stderr instead of stdin
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001984 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001985 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001986 break;
1987
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001988 case '-': // "--" don't take any more option arguments
1989 // "--help" give help message
1990 // "--version" give version message
1991 // "--clean" clean context
1992 // "--literal" take files literally
1993 // "--nofork" don't fork
1994 // "--not-a-term" don't warn for not a term
1995 // "--ttyfail" exit if not a term
1996 // "--noplugin[s]" skip plugins
1997 // "--cmd <cmd>" execute cmd before vimrc
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001998 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1999 usage();
2000 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
2001 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002002 Columns = 80; // need to init Columns
2003 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaar0bcadf12021-02-11 19:18:58 +01002004#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI)
Bram Moolenaar3b678042021-02-11 19:08:05 +01002005 gui.starting = FALSE; // not starting GUI, will exit
Bram Moolenaar0bcadf12021-02-11 19:18:58 +01002006#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002007 list_version();
2008 msg_putchar('\n');
2009 msg_didout = FALSE;
2010 mch_exit(0);
2011 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002012 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
2013 {
2014 parmp->use_vimrc = (char_u *)"DEFAULTS";
Bram Moolenaar62dd4522018-03-14 21:20:02 +01002015#ifdef FEAT_GUI
2016 use_gvimrc = (char_u *)"NONE";
2017#endif
Bram Moolenaar07268702018-03-01 21:57:32 +01002018 parmp->clean = TRUE;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002019 set_option_value((char_u *)"vif", 0L, (char_u *)"NONE", 0);
2020 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002021 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
2022 {
Bram Moolenaar53076832015-12-31 19:53:21 +01002023#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002024 parmp->literal = TRUE;
2025#endif
2026 }
2027 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
2028 {
2029#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002030 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002031#endif
2032 }
2033 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
2034 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002035 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
2036 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002037 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
2038 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002039 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
2040 {
2041 want_argument = TRUE;
2042 argv_idx += 3;
2043 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002044 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
2045 {
2046 want_argument = TRUE;
2047 argv_idx += 11;
2048 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002049#ifdef FEAT_CLIENTSERVER
2050 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002051 ; // already processed -- no arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002052 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
2053 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
2054 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002055 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002056 if (argc > 1)
2057 {
2058 --argc;
2059 ++argv;
2060 }
2061 }
2062#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002063#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002064# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002065 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002066# else
2067 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
2068# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002069 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002070 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002071 if (argc > 1)
2072 {
2073 --argc;
2074 ++argv;
2075 }
2076 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00002077#endif
2078#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002079 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
2080 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002081 // already processed, skip
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002082 }
2083#endif
2084 else
2085 {
2086 if (argv[0][argv_idx])
2087 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2088 had_minmin = TRUE;
2089 }
2090 if (!want_argument)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002091 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002092 break;
2093
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002094 case 'A': // "-A" start in Arabic mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002095#ifdef FEAT_ARABIC
2096 set_option_value((char_u *)"arabic", 1L, NULL, 0);
2097#else
2098 mch_errmsg(_(e_noarabic));
2099 mch_exit(2);
2100#endif
2101 break;
2102
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002103 case 'b': // "-b" binary mode
2104 // Needs to be effective before expanding file names, because
2105 // for Win32 this makes us edit a shortcut file itself,
2106 // instead of the file it links to.
Bram Moolenaar231334e2005-07-25 20:46:57 +00002107 set_options_bin(curbuf->b_p_bin, 1, 0);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002108 curbuf->b_p_bin = 1; // binary file I/O
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002109 break;
2110
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002111 case 'C': // "-C" Compatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002112 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02002113 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002114 break;
2115
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002116 case 'e': // "-e" Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002117 exmode_active = EXMODE_NORMAL;
2118 break;
2119
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002120 case 'E': // "-E" Improved Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002121 exmode_active = EXMODE_VIM;
2122 break;
2123
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002124 case 'f': // "-f" GUI: run in foreground. Amiga: open
2125 // window directly, not with newcli
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002126#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002127 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002128#endif
2129 break;
2130
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002131 case 'g': // "-g" start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002132 main_start_gui();
2133 break;
2134
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002135 case 'F': // "-F" was for Farsi mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002136 mch_errmsg(_(e_nofarsi));
2137 mch_exit(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002138 break;
2139
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002140 case '?': // "-?" give help message (for MS-Windows)
2141 case 'h': // "-h" give help message
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002142#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002143 // Tell usage() to exit for "gvim".
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002144 gui.starting = FALSE;
2145#endif
2146 usage();
2147 break;
2148
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002149 case 'H': // "-H" start in Hebrew mode: rl + hkmap set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002150#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002151 p_hkmap = TRUE;
2152 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002153#else
2154 mch_errmsg(_(e_nohebrew));
2155 mch_exit(2);
2156#endif
2157 break;
2158
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002159 case 'l': // "-l" lisp mode, 'lisp' and 'showmatch' on
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002160#ifdef FEAT_LISP
2161 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2162 p_sm = TRUE;
2163#endif
2164 break;
2165
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002166 case 'M': // "-M" no changes or writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002167 reset_modifiable();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002168 // FALLTHROUGH
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002169
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002170 case 'm': // "-m" no writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002171 p_write = FALSE;
2172 break;
2173
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002174 case 'y': // "-y" easy mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002175#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002176 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002177#endif
2178 parmp->evim_mode = TRUE;
2179 break;
2180
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002181 case 'N': // "-N" Nocompatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002182 change_compatible(FALSE);
2183 break;
2184
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002185 case 'n': // "-n" no swap file
Bram Moolenaar67c53842010-05-22 18:28:27 +02002186#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002187 // checking for "-nb", netbeans parameters
Bram Moolenaar67c53842010-05-22 18:28:27 +02002188 if (argv[0][argv_idx] == 'b')
2189 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002190 netbeansArg = argv[0];
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002191 argv_idx = -1; // skip to next argument
Bram Moolenaar67c53842010-05-22 18:28:27 +02002192 }
2193 else
2194#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002195 parmp->no_swap_file = TRUE;
2196 break;
2197
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002198 case 'p': // "-p[N]" open N tab pages
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002199#ifdef TARGET_API_MAC_OSX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002200 // For some reason on MacOS X, an argument like:
2201 // -psn_0_10223617 is passed in when invoke from Finder
2202 // or with the 'open' command
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002203 if (argv[0][argv_idx] == 's')
2204 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002205 argv_idx = -1; // bypass full -psn
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002206 main_start_gui();
2207 break;
2208 }
2209#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002210 // default is 0: open window for each file
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002211 parmp->window_count = get_number_arg((char_u *)argv[0],
2212 &argv_idx, 0);
2213 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002214 break;
2215
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002216 case 'o': // "-o[N]" open N horizontal split windows
2217 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002218 parmp->window_count = get_number_arg((char_u *)argv[0],
2219 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002220 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002221 break;
2222
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002223 case 'O': // "-O[N]" open N vertical split windows
2224 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002225 parmp->window_count = get_number_arg((char_u *)argv[0],
2226 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002227 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002228 break;
2229
2230#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002231 case 'q': // "-q" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002232 if (parmp->edit_type != EDIT_NONE)
2233 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2234 parmp->edit_type = EDIT_QF;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002235 if (argv[0][argv_idx]) // "-q{errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002236 {
2237 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2238 argv_idx = -1;
2239 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002240 else if (argc > 1) // "-q {errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002241 want_argument = TRUE;
2242 break;
2243#endif
2244
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002245 case 'R': // "-R" readonly mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002246 readonlymode = TRUE;
2247 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002248 p_uc = 10000; // don't update very often
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002249 break;
2250
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002251 case 'r': // "-r" recovery mode
2252 case 'L': // "-L" recovery mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002253 recoverymode = 1;
2254 break;
2255
2256 case 's':
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002257 if (exmode_active) // "-s" silent (batch) mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002258 silent_mode = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002259 else // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002260 want_argument = TRUE;
2261 break;
2262
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002263 case 't': // "-t {tag}" or "-t{tag}" jump to tag
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002264 if (parmp->edit_type != EDIT_NONE)
2265 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2266 parmp->edit_type = EDIT_TAG;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002267 if (argv[0][argv_idx]) // "-t{tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002268 {
2269 parmp->tagname = (char_u *)argv[0] + argv_idx;
2270 argv_idx = -1;
2271 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002272 else // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002273 want_argument = TRUE;
2274 break;
2275
2276#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002277 case 'D': // "-D" Debugging
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002278 parmp->use_debug_break_level = 9999;
2279 break;
2280#endif
2281#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002282 case 'd': // "-d" 'diff'
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002283# ifdef AMIGA
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002284 // check for "-dev {device}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002285 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2286 want_argument = TRUE;
2287 else
2288# endif
2289 parmp->diff_mode = TRUE;
2290 break;
2291#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002292 case 'V': // "-V{N}" Verbose level
2293 // default is 10: a little bit verbose
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002294 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2295 if (argv[0][argv_idx] != NUL)
2296 {
2297 set_option_value((char_u *)"verbosefile", 0L,
2298 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002299 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002300 }
2301 break;
2302
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002303 case 'v': // "-v" Vi-mode (as if called "vi")
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002304 exmode_active = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002305#if defined(FEAT_GUI) && !defined(VIMDLL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002306 gui.starting = FALSE; // don't start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002307#endif
2308 break;
2309
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002310 case 'w': // "-w{number}" set window height
2311 // "-w {scriptout}" write to script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002312 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2313 {
2314 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2315 set_option_value((char_u *)"window", n, NULL, 0);
2316 break;
2317 }
2318 want_argument = TRUE;
2319 break;
2320
2321#ifdef FEAT_CRYPT
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002322 case 'x': // "-x" encrypted reading/writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002323 parmp->ask_for_key = TRUE;
2324 break;
2325#endif
2326
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002327 case 'X': // "-X" don't connect to X server
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002328#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2329 x_no_connect = TRUE;
2330#endif
2331 break;
2332
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002333 case 'Z': // "-Z" restricted mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002334 restricted = TRUE;
2335 break;
2336
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002337 case 'c': // "-c{command}" or "-c {command}" execute
2338 // command
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002339 if (argv[0][argv_idx] != NUL)
2340 {
2341 if (parmp->n_commands >= MAX_ARG_CMDS)
2342 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002343 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2344 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002345 argv_idx = -1;
2346 break;
2347 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002348 // FALLTHROUGH
2349 case 'S': // "-S {file}" execute Vim script
2350 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002351#ifndef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002352 case 'd': // "-d {device}" device (for Amiga)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002353#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002354 case 'T': // "-T {terminal}" terminal name
2355 case 'u': // "-u {vimrc}" vim inits file
2356 case 'U': // "-U {gvimrc}" gvim inits file
2357 case 'W': // "-W {scriptout}" overwrite
Bram Moolenaar4f974752019-02-17 17:44:42 +01002358#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002359 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002360#endif
2361 want_argument = TRUE;
2362 break;
2363
2364 default:
2365 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2366 }
2367
2368 /*
2369 * Handle option arguments with argument.
2370 */
2371 if (want_argument)
2372 {
2373 /*
2374 * Check for garbage immediately after the option letter.
2375 */
2376 if (argv[0][argv_idx] != NUL)
2377 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2378
2379 --argc;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002380 if (argc < 1 && c != 'S') // -S has an optional argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002381 mainerr_arg_missing((char_u *)argv[0]);
2382 ++argv;
2383 argv_idx = -1;
2384
2385 switch (c)
2386 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002387 case 'c': // "-c {command}" execute command
2388 case 'S': // "-S {file}" execute Vim script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002389 if (parmp->n_commands >= MAX_ARG_CMDS)
2390 mainerr(ME_EXTRA_CMD, NULL);
2391 if (c == 'S')
2392 {
2393 char *a;
2394
2395 if (argc < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002396 // "-S" without argument: use default session file
2397 // name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002398 a = SESSION_FILE;
2399 else if (argv[0][0] == '-')
2400 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002401 // "-S" followed by another option: use default
2402 // session file name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002403 a = SESSION_FILE;
2404 ++argc;
2405 --argv;
2406 }
2407 else
2408 a = argv[0];
Bram Moolenaar964b3742019-05-24 18:54:09 +02002409 p = alloc(STRLEN(a) + 4);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002410 if (p == NULL)
2411 mch_exit(2);
2412 sprintf((char *)p, "so %s", a);
2413 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2414 parmp->commands[parmp->n_commands++] = p;
2415 }
2416 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002417 parmp->commands[parmp->n_commands++] =
2418 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002419 break;
2420
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002421 case '-':
2422 if (argv[-1][2] == 'c')
2423 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002424 // "--cmd {command}" execute command
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002425 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2426 mainerr(ME_EXTRA_CMD, NULL);
2427 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002428 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002429 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002430 // "--startuptime <file>" already handled
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002431 break;
2432
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002433 // case 'd': -d {device} is handled in mch_check_win() for the
2434 // Amiga
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002435
2436#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002437 case 'q': // "-q {errorfile}" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002438 parmp->use_ef = (char_u *)argv[0];
2439 break;
2440#endif
2441
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002442 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002443 set_option_value((char_u *)"vif", 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002444 break;
2445
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002446 case 's': // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002447 if (scriptin[0] != NULL)
2448 {
2449scripterror:
2450 mch_errmsg(_("Attempt to open script file again: \""));
2451 mch_errmsg(argv[-1]);
2452 mch_errmsg(" ");
2453 mch_errmsg(argv[0]);
2454 mch_errmsg("\"\n");
2455 mch_exit(2);
2456 }
2457 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2458 {
2459 mch_errmsg(_("Cannot open for reading: \""));
2460 mch_errmsg(argv[0]);
2461 mch_errmsg("\"\n");
2462 mch_exit(2);
2463 }
2464 if (save_typebuf() == FAIL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002465 mch_exit(2); // out of memory
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002466 break;
2467
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002468 case 't': // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002469 parmp->tagname = (char_u *)argv[0];
2470 break;
2471
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002472 case 'T': // "-T {terminal}" terminal name
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002473 /*
2474 * The -T term argument is always available and when
2475 * HAVE_TERMLIB is supported it overrides the environment
2476 * variable TERM.
2477 */
2478#ifdef FEAT_GUI
2479 if (term_is_gui((char_u *)argv[0]))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002480 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002481 else
2482#endif
2483 parmp->term = (char_u *)argv[0];
2484 break;
2485
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002486 case 'u': // "-u {vimrc}" vim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002487 parmp->use_vimrc = (char_u *)argv[0];
2488 break;
2489
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002490 case 'U': // "-U {gvimrc}" gvim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002491#ifdef FEAT_GUI
2492 use_gvimrc = (char_u *)argv[0];
2493#endif
2494 break;
2495
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002496 case 'w': // "-w {nr}" 'window' value
2497 // "-w {scriptout}" append to script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002498 if (vim_isdigit(*((char_u *)argv[0])))
2499 {
2500 argv_idx = 0;
2501 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2502 set_option_value((char_u *)"window", n, NULL, 0);
2503 argv_idx = -1;
2504 break;
2505 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002506 // FALLTHROUGH
2507 case 'W': // "-W {scriptout}" overwrite script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002508 if (scriptout != NULL)
2509 goto scripterror;
2510 if ((scriptout = mch_fopen(argv[0],
2511 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2512 {
2513 mch_errmsg(_("Cannot open for script output: \""));
2514 mch_errmsg(argv[0]);
2515 mch_errmsg("\"\n");
2516 mch_exit(2);
2517 }
2518 break;
2519
Bram Moolenaar4f974752019-02-17 17:44:42 +01002520#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002521 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002522 gui_mch_set_parent(argv[0]);
2523 break;
2524#endif
2525 }
2526 }
2527 }
2528
2529 /*
2530 * File name argument.
2531 */
2532 else
2533 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002534 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002535
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002536 // Check for only one type of editing.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002537 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2538 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2539 parmp->edit_type = EDIT_FILE;
2540
2541#ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002542 // Remember if the argument was a full path before changing
2543 // slashes to backslashes.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002544 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2545 parmp->full_path = TRUE;
2546#endif
2547
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002548 // Add the file to the global argument list.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002549 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2550 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2551 mch_exit(2);
2552#ifdef FEAT_DIFF
2553 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2554 && !mch_isdir(alist_name(&GARGLIST[0])))
2555 {
2556 char_u *r;
2557
2558 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2559 if (r != NULL)
2560 {
2561 vim_free(p);
2562 p = r;
2563 }
2564 }
2565#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002566#if defined(__CYGWIN32__) && !defined(MSWIN)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002567 /*
2568 * If vim is invoked by non-Cygwin tools, convert away any
2569 * DOS paths, so things like .swp files are created correctly.
2570 * Look for evidence of non-Cygwin paths before we bother.
2571 * This is only for when using the Unix files.
2572 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002573 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002574 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002575 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002576
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002577# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002578 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002579# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002580 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002581# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002582 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002583 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002584 if (p == NULL)
2585 mch_exit(2);
2586 }
2587#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002588
2589#ifdef USE_FNAME_CASE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002590 // Make the case of the file name match the actual file.
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002591 fname_case(p, 0);
2592#endif
2593
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002594 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002595#ifdef EXPAND_FILENAMES
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002596 parmp->literal ? 2 : 0 // add buffer nr after exp.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002597#else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002598 2 // add buffer number now and use curbuf
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002599#endif
2600 );
2601
Bram Moolenaar4f974752019-02-17 17:44:42 +01002602#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002603 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002604 // Remember this argument has been added to the argument list.
2605 // Needed when 'encoding' is changed.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002606 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002607# ifdef FEAT_DIFF
2608 parmp->diff_mode
2609# else
2610 FALSE
2611# endif
2612 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002613 }
2614#endif
2615 }
2616
2617 /*
2618 * If there are no more letters after the current "-", go to next
2619 * argument. argv_idx is set to -1 when the current argument is to be
2620 * skipped.
2621 */
2622 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2623 {
2624 --argc;
2625 ++argv;
2626 argv_idx = 1;
2627 }
2628 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002629
2630#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002631 // If there is a "+123" or "-c" command, set v:swapcommand to the first
2632 // one.
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002633 if (parmp->n_commands > 0)
2634 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002635 p = alloc(STRLEN(parmp->commands[0]) + 3);
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002636 if (p != NULL)
2637 {
2638 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2639 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2640 vim_free(p);
2641 }
2642 }
2643#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002644}
2645
2646/*
2647 * Print a warning if stdout is not a terminal.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +01002648 * When starting in Ex mode and commands come from a file, set silent_mode.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002649 */
2650 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002651check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002652{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002653 int input_isatty; // is active input a terminal?
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002654
2655 input_isatty = mch_input_isatty();
2656 if (exmode_active)
2657 {
2658 if (!input_isatty)
2659 silent_mode = TRUE;
2660 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002661 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002662#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002663 // don't want the delay when started from the desktop
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002664 && !gui.starting
2665#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002666 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002667 {
2668#ifdef NBDEBUG
2669 /*
2670 * This shouldn't be necessary. But if I run netbeans with the log
2671 * output coming to the console and XOpenDisplay fails, I get vim
2672 * trying to start with input/output to my console tty. This fills my
2673 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002674 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002675 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002676 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002677 {
2678 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2679 exit(1);
2680 }
2681#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002682#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2683 if (
2684# ifdef VIMDLL
2685 !gui.starting &&
2686# endif
2687 is_cygpty_used())
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002688 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002689# if defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
Bram Moolenaar2a027452017-09-26 19:10:37 +02002690 && defined(FEAT_GETTEXT)
2691 char *s, *tofree = NULL;
2692
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002693 // Set the encoding of the error message based on $LC_ALL or
2694 // other environment variables instead of 'encoding'.
2695 // Note that the message is shown on a Cygwin terminal (e.g.
2696 // mintty) which encoding is based on $LC_ALL or etc., not the
2697 // current codepage used by normal Win32 console programs.
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002698 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002699 if (s == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002700 s = "utf-8"; // Use "utf-8" by default.
Bram Moolenaar2a027452017-09-26 19:10:37 +02002701 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2702 vim_free(tofree);
2703# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002704 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2705 exit(1);
2706 }
2707#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002708 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002709 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2710 if (!input_isatty)
2711 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2712 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002713 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2714 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002715 if (scriptin[0] == NULL)
Bram Moolenaareda1da02019-11-17 17:06:33 +01002716 ui_delay(2005L, TRUE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002717 TIME_MSG("Warning delay");
2718 }
2719}
2720
2721/*
2722 * Read text from stdin.
2723 */
2724 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002725read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002726{
2727 int i;
2728
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002729 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002730 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002731
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002732 no_wait_return = TRUE;
2733 i = msg_didany;
2734 set_buflisted(TRUE);
Bram Moolenaar204ade62020-10-11 14:58:46 +02002735
2736 // Create memfile and read from stdin.
Bram Moolenaar204ade62020-10-11 14:58:46 +02002737 (void)open_buffer(TRUE, NULL, 0);
2738
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002739 no_wait_return = FALSE;
2740 msg_didany = i;
2741 TIME_MSG("reading stdin");
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002742
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002743 check_swap_exists_action();
Bram Moolenaar8e1cbb52020-12-08 19:36:21 +01002744
2745#if !(defined(AMIGA) || defined(MACOS_X))
2746 // Dup stdin from stderr to read commands from, so that shell commands
2747 // work.
2748 // TODO: why is this needed, even though readfile() has done this?
2749 close(0);
2750 vim_ignored = dup(2);
2751#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002752}
2753
2754/*
2755 * Create the requested number of windows and edit buffers in them.
2756 * Also does recovery if "recoverymode" set.
2757 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002758 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002759create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002760{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002761 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002762 int done = 0;
2763
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002764 /*
2765 * Create the number of windows that was requested.
2766 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002767 if (parmp->window_count == -1) // was not set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002768 parmp->window_count = 1;
2769 if (parmp->window_count == 0)
2770 parmp->window_count = GARGCOUNT;
2771 if (parmp->window_count > 1)
2772 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002773 // Don't change the windows if there was a command in .vimrc that
2774 // already split some windows
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002775 if (parmp->window_layout == 0)
2776 parmp->window_layout = WIN_HOR;
2777 if (parmp->window_layout == WIN_TABS)
2778 {
2779 parmp->window_count = make_tabpages(parmp->window_count);
2780 TIME_MSG("making tab pages");
2781 }
2782 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002783 {
2784 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002785 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002786 TIME_MSG("making windows");
2787 }
2788 else
2789 parmp->window_count = win_count();
2790 }
2791 else
2792 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002793
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002794 if (recoverymode) // do recover
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002795 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002796 msg_scroll = TRUE; // scroll message up
Bram Moolenaar99499b12019-05-23 21:35:48 +02002797 ml_recover(TRUE);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002798 if (curbuf->b_ml.ml_mfp == NULL) // failed
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002799 getout(1);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002800 do_modelines(0); // do modelines
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002801 }
2802 else
2803 {
2804 /*
2805 * Open a buffer for windows that don't have one yet.
2806 * Commands in the .vimrc might have loaded a file or split the window.
2807 * Watch out for autocommands that delete a window.
2808 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002809 /*
2810 * Don't execute Win/Buf Enter/Leave autocommands here
2811 */
2812 ++autocmd_no_enter;
2813 ++autocmd_no_leave;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002814 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002815 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002816 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002817 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002818 {
2819 if (parmp->window_layout == WIN_TABS)
2820 goto_tabpage(1);
2821 else
2822 curwin = firstwin;
2823 }
2824 else if (parmp->window_layout == WIN_TABS)
2825 {
2826 if (curtab->tp_next == NULL)
2827 break;
2828 goto_tabpage(0);
2829 }
2830 else
2831 {
2832 if (curwin->w_next == NULL)
2833 break;
2834 curwin = curwin->w_next;
2835 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002836 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002837 curbuf = curwin->w_buffer;
2838 if (curbuf->b_ml.ml_mfp == NULL)
2839 {
2840#ifdef FEAT_FOLDING
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002841 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002842 if (p_fdls >= 0)
2843 curwin->w_p_fdl = p_fdls;
2844#endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002845 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002846 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002847
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002848 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002849
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002850 // create memfile, read file
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002851 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002852
Bram Moolenaar84212822006-11-07 21:59:47 +00002853 if (swap_exists_action == SEA_QUIT)
2854 {
2855 if (got_int || only_one_window())
2856 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002857 // abort selected or quit and only one window
2858 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00002859 getout(1);
2860 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002861 // We can't close the window, it would disturb what
2862 // happens next. Clear the file name and set the arg
2863 // index to -1 to delete it later.
Bram Moolenaar84212822006-11-07 21:59:47 +00002864 setfname(curbuf, NULL, NULL, FALSE);
2865 curwin->w_arg_idx = -1;
2866 swap_exists_action = SEA_NONE;
2867 }
2868 else
2869 handle_swap_exists(NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002870 dorewind = TRUE; // start again
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002871 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002872 ui_breakcheck();
2873 if (got_int)
2874 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002875 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002876 break;
2877 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002878 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002879 if (parmp->window_layout == WIN_TABS)
2880 goto_tabpage(1);
2881 else
2882 curwin = firstwin;
2883 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002884 --autocmd_no_enter;
2885 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002886 }
2887}
2888
Bram Moolenaar9810cfb2019-12-11 21:23:00 +01002889/*
2890 * If opened more than one window, start editing files in the other
2891 * windows. make_windows() has already opened the windows.
2892 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002893 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002894edit_buffers(
2895 mparm_T *parmp,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002896 char_u *cwd) // current working dir
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002897{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002898 int arg_idx; // index in argument list
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002899 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002900 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002901 win_T *win;
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002902 char_u *p_shm_save = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002903
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002904 /*
2905 * Don't execute Win/Buf Enter/Leave autocommands here
2906 */
2907 ++autocmd_no_enter;
2908 ++autocmd_no_leave;
Bram Moolenaar84212822006-11-07 21:59:47 +00002909
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002910 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002911 if (curwin->w_arg_idx == -1)
2912 {
2913 win_close(curwin, TRUE);
2914 advance = FALSE;
2915 }
2916
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002917 arg_idx = 1;
2918 for (i = 1; i < parmp->window_count; ++i)
2919 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002920 if (cwd != NULL)
2921 mch_chdir((char *)cwd);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002922 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002923 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002924 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002925 ++arg_idx;
2926 win_close(curwin, TRUE);
2927 advance = FALSE;
2928 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002929 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002930
Bram Moolenaar84212822006-11-07 21:59:47 +00002931 if (advance)
2932 {
2933 if (parmp->window_layout == WIN_TABS)
2934 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002935 if (curtab->tp_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00002936 break;
2937 goto_tabpage(0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002938 // Temporarily reset 'shm' option to not print fileinfo when
2939 // loading the other buffers. This would overwrite the already
2940 // existing fileinfo for the first tab.
2941 if (i == 1)
2942 {
2943 char buf[100];
2944
2945 p_shm_save = vim_strsave(p_shm);
2946 vim_snprintf(buf, 100, "F%s", p_shm);
2947 set_option_value((char_u *)"shm", 0L, (char_u *)buf, 0);
2948 }
Bram Moolenaar84212822006-11-07 21:59:47 +00002949 }
2950 else
2951 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002952 if (curwin->w_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00002953 break;
2954 win_enter(curwin->w_next, FALSE);
2955 }
2956 }
2957 advance = TRUE;
2958
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002959 // Only open the file if there is no file in this window yet (that can
2960 // happen when .vimrc contains ":sall").
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002961 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2962 {
2963 curwin->w_arg_idx = arg_idx;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002964 // Edit file from arg list, if there is one. When "Quit" selected
2965 // at the ATTENTION prompt close the window.
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002966 swap_exists_did_quit = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002967 (void)do_ecmd(0, arg_idx < GARGCOUNT
2968 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002969 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002970 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002971 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002972 // abort or quit selected
Bram Moolenaar84212822006-11-07 21:59:47 +00002973 if (got_int || only_one_window())
2974 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002975 // abort selected and only one window
2976 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00002977 getout(1);
2978 }
2979 win_close(curwin, TRUE);
2980 advance = FALSE;
2981 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002982 if (arg_idx == GARGCOUNT - 1)
2983 arg_had_last = TRUE;
2984 ++arg_idx;
2985 }
2986 ui_breakcheck();
2987 if (got_int)
2988 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002989 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002990 break;
2991 }
2992 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002993
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002994 if (p_shm_save != NULL)
2995 {
2996 set_option_value((char_u *)"shm", 0L, p_shm_save, 0);
2997 vim_free(p_shm_save);
2998 }
2999
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003000 if (parmp->window_layout == WIN_TABS)
3001 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003002 --autocmd_no_enter;
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003003
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003004 // make the first window the current window
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003005 win = firstwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003006#if defined(FEAT_QUICKFIX)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003007 // Avoid making a preview window the current window.
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003008 while (win->w_p_pvw)
3009 {
3010 win = win->w_next;
3011 if (win == NULL)
3012 {
3013 win = firstwin;
3014 break;
3015 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003016 }
3017#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003018 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003019
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003020 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003021 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003022 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003023 win_equal(curwin, FALSE, 'b'); // adjust heights
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003024}
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003025
3026/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003027 * Execute the commands from --cmd arguments "cmds[cnt]".
3028 */
3029 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003030exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003031{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003032 char_u **cmds = parmp->pre_commands;
3033 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003034 int i;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003035 ESTACK_CHECK_DECLARATION
Bram Moolenaar58d98232005-07-23 22:25:46 +00003036
3037 if (cnt > 0)
3038 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003039 curwin->w_cursor.lnum = 0; // just in case..
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003040 estack_push(ETYPE_ARGS, (char_u *)_("pre-vimrc command line"), 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003041 ESTACK_CHECK_SETUP
Bram Moolenaar58d98232005-07-23 22:25:46 +00003042# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003043 current_sctx.sc_sid = SID_CMDARG;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003044# endif
3045 for (i = 0; i < cnt; ++i)
3046 do_cmdline_cmd(cmds[i]);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003047 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003048 estack_pop();
Bram Moolenaar58d98232005-07-23 22:25:46 +00003049# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003050 current_sctx.sc_sid = 0;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003051# endif
3052 TIME_MSG("--cmd commands");
3053 }
3054}
3055
3056/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003057 * Execute "+", "-c" and "-S" arguments.
3058 */
3059 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003060exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003061{
3062 int i;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003063 ESTACK_CHECK_DECLARATION
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003064
3065 /*
3066 * We start commands on line 0, make "vim +/pat file" match a
3067 * pattern on line 1. But don't move the cursor when an autocommand
3068 * with g`" was used.
3069 */
3070 msg_scroll = TRUE;
3071 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
3072 curwin->w_cursor.lnum = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003073 estack_push(ETYPE_ARGS, (char_u *)"command line", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003074 ESTACK_CHECK_SETUP
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003075#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003076 current_sctx.sc_sid = SID_CARG;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003077 current_sctx.sc_seq = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003078#endif
3079 for (i = 0; i < parmp->n_commands; ++i)
3080 {
3081 do_cmdline_cmd(parmp->commands[i]);
3082 if (parmp->cmds_tofree[i])
3083 vim_free(parmp->commands[i]);
3084 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01003085 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003086 estack_pop();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003087#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003088 current_sctx.sc_sid = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003089#endif
3090 if (curwin->w_cursor.lnum == 0)
3091 curwin->w_cursor.lnum = 1;
3092
3093 if (!exmode_active)
3094 msg_scroll = FALSE;
3095
3096#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003097 // When started with "-q errorfile" jump to first error again.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003098 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003099 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003100#endif
3101 TIME_MSG("executing command arguments");
3102}
3103
3104/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003105 * Source startup scripts.
3106 */
3107 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003108source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003109{
3110 int i;
3111
3112 /*
3113 * For "evim" source evim.vim first of all, so that the user can overrule
3114 * any things he doesn't like.
3115 */
3116 if (parmp->evim_mode)
3117 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003119 TIME_MSG("source evim file");
3120 }
3121
3122 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003123 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00003124 * nothing else.
3125 */
3126 if (parmp->use_vimrc != NULL)
3127 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003128 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003129 {
3130 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE, NULL)
3131 != OK)
3132 emsg(e_failed_to_source_defaults);
3133 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003134 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003135 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003136 {
3137#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003138 if (use_gvimrc == NULL) // don't load gvimrc either
Bram Moolenaar58d98232005-07-23 22:25:46 +00003139 use_gvimrc = parmp->use_vimrc;
3140#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003141 }
3142 else
3143 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003145 semsg(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003146 }
3147 }
3148 else if (!silent_mode)
3149 {
3150#ifdef AMIGA
3151 struct Process *proc = (struct Process *)FindTask(0L);
3152 APTR save_winptr = proc->pr_WindowPtr;
3153
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003154 // Avoid a requester here for a volume that doesn't exist.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003155 proc->pr_WindowPtr = (APTR)-1L;
3156#endif
3157
3158 /*
3159 * Get system wide defaults, if the file name is defined.
3160 */
3161#ifdef SYS_VIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003163#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003164#ifdef MACOS_X
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE,
3166 DOSO_NONE, NULL);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003167#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003168
3169 /*
3170 * Try to read initialization commands from the following places:
3171 * - environment variable VIMINIT
3172 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3173 * - second user vimrc file ($VIM/.vimrc for Dos)
3174 * - environment variable EXINIT
3175 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3176 * - second user exrc file ($VIM/.exrc for Dos)
3177 * The first that exists is used, the rest is ignored.
3178 */
3179 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3180 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 if (do_source((char_u *)USR_VIMRC_FILE, TRUE,
3182 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003183#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003184 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003186#endif
3187#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003188 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003190#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003191#ifdef USR_VIMRC_FILE4
3192 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003194#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003195 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 && do_source((char_u *)USR_EXRC_FILE, FALSE,
3197 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003198#ifdef USR_EXRC_FILE2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 && do_source((char_u *)USR_EXRC_FILE2, FALSE,
3200 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003201#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003202 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003203 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003204 // When no .vimrc file was found: source defaults.vim.
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003205 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE,
3206 NULL) == FAIL)
3207 emsg(e_failed_to_source_defaults);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003208 }
3209 }
3210
3211 /*
3212 * Read initialization commands from ".vimrc" or ".exrc" in current
3213 * directory. This is only done if the 'exrc' option is set.
3214 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003215 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003216 * option has been reset in environment of global ".exrc" or ".vimrc".
3217 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3218 * SYS_VIMRC_FILE.
3219 */
3220 if (p_exrc)
3221 {
3222#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003223 // If ".vimrc" file is not owned by user, set 'secure' mode.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003224 if (!file_owned(VIMRC_FILE))
3225#endif
3226 secure = p_secure;
3227
3228 i = FAIL;
3229 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003230 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003231#ifdef USR_VIMRC_FILE2
3232 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003233 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003234#endif
3235#ifdef USR_VIMRC_FILE3
3236 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003237 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003238#endif
3239#ifdef SYS_VIMRC_FILE
3240 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003241 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003242#endif
3243 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003245
3246 if (i == FAIL)
3247 {
3248#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003249 // if ".exrc" is not owned by user set 'secure' mode
Bram Moolenaar58d98232005-07-23 22:25:46 +00003250 if (!file_owned(EXRC_FILE))
3251 secure = p_secure;
3252 else
3253 secure = 0;
3254#endif
3255 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003256 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003257#ifdef USR_EXRC_FILE2
3258 && fullpathcmp((char_u *)USR_EXRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003259 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003260#endif
3261 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 (void)do_source((char_u *)EXRC_FILE, FALSE,
3263 DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003264 }
3265 }
3266 if (secure == 2)
3267 need_wait_return = TRUE;
3268 secure = 0;
3269#ifdef AMIGA
3270 proc->pr_WindowPtr = save_winptr;
3271#endif
3272 }
3273 TIME_MSG("sourcing vimrc file(s)");
3274}
3275
3276/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003277 * Setup to start using the GUI. Exit with an error when not available.
3278 */
3279 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003280main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003281{
3282#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003283 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003284#else
3285 mch_errmsg(_(e_nogvim));
3286 mch_errmsg("\n");
3287 mch_exit(2);
3288#endif
3289}
3290
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003291#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003292
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003293/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 * Get an environment variable and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003295 * Returns FAIL if the environment variable was not executed, OK otherwise.
3296 */
3297 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003298process_env(
3299 char_u *env,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003300 int is_viminit) // when TRUE, called for VIMINIT
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003301{
3302 char_u *initstr;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003303 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003304
Bram Moolenaare31ee862020-01-07 20:59:34 +01003305 ESTACK_CHECK_DECLARATION
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003306
3307 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3308 {
3309 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003310 vimrc_found(NULL, NULL);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003311 estack_push(ETYPE_ENV, env, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003312 ESTACK_CHECK_SETUP
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003313 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003314 current_sctx.sc_version = 1;
3315#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003316 current_sctx.sc_sid = SID_ENV;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003317 current_sctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003318 current_sctx.sc_lnum = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003319#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003320
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003321 do_cmdline_cmd(initstr);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003322
Bram Moolenaare31ee862020-01-07 20:59:34 +01003323 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003324 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003325 current_sctx = save_current_sctx;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003326 return OK;
3327 }
3328 return FAIL;
3329}
3330
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003331#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003332/*
3333 * Return TRUE if we are certain the user owns the file "fname".
3334 * Used for ".vimrc" and ".exrc".
3335 * Use both stat() and lstat() for extra security.
3336 */
3337 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003338file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003339{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003340 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003341# ifdef UNIX
3342 uid_t uid = getuid();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003343# else // VMS
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003344 uid_t uid = ((getgid() << 16) | getuid());
3345# endif
3346
3347 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3348# ifdef HAVE_LSTAT
3349 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3350# endif
3351 );
3352}
3353#endif
3354
3355/*
3356 * Give an error message main_errors["n"] and exit.
3357 */
3358 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003359mainerr(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003360 int n, // one of the ME_ defines
3361 char_u *str) // extra argument or NULL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003362{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003363#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003364 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003365#endif
3366
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003367 // If this is a Windows GUI executable, show an error dialog box.
3368#ifdef VIMDLL
3369 gui.in_use = mch_is_gui_executable();
3370#endif
3371#ifdef FEAT_GUI_MSWIN
3372 gui.starting = FALSE; // Needed to show as error.
3373#endif
3374
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003375 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003376 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003377 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003378 mch_errmsg(_(main_errors[n]));
3379 if (str != NULL)
3380 {
3381 mch_errmsg(": \"");
3382 mch_errmsg((char *)str);
3383 mch_errmsg("\"");
3384 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003385 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003386
3387 mch_exit(1);
3388}
3389
3390 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003391mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003392{
3393 mainerr(ME_ARG_MISSING, str);
3394}
3395
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003396#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003397/*
3398 * print a message with three spaces prepended and '\n' appended.
3399 */
3400 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003401main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003402{
3403 mch_msg(" ");
3404 mch_msg(s);
3405 mch_msg("\n");
3406}
3407
3408/*
3409 * Print messages for "vim -h" or "vim --help" and exit.
3410 */
3411 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003412usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003413{
3414 int i;
3415 static char *(use[]) =
3416 {
3417 N_("[file ..] edit specified file(s)"),
3418 N_("- read text from stdin"),
3419 N_("-t tag edit file where tag is defined"),
3420#ifdef FEAT_QUICKFIX
3421 N_("-q [errorfile] edit file with first error")
3422#endif
3423 };
3424
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003425#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003426 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003427#endif
3428
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003429 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003430 mch_msg(longVersion);
Bram Moolenaar32aaf5a2018-06-21 21:38:33 +02003431 mch_msg(_("\n\nUsage:"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003432 for (i = 0; ; ++i)
3433 {
3434 mch_msg(_(" vim [arguments] "));
3435 mch_msg(_(use[i]));
K.Takataeeec2542021-06-02 13:28:16 +02003436 if (i == ARRAY_LENGTH(use) - 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003437 break;
3438 mch_msg(_("\n or:"));
3439 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003440#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003441 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003442#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003443
3444 mch_msg(_("\n\nArguments:\n"));
3445 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003446#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003447 main_msg(_("--literal\t\tDon't expand wildcards"));
3448#endif
3449#ifdef FEAT_OLE
3450 main_msg(_("-register\t\tRegister this gvim for OLE"));
3451 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3452#endif
3453#ifdef FEAT_GUI
3454 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3455 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3456#endif
3457 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3458 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003459 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003460 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3461#ifdef FEAT_DIFF
3462 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3463#endif
3464 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3465 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3466 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3467 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3468 main_msg(_("-M\t\t\tModifications in text not allowed"));
3469 main_msg(_("-b\t\t\tBinary mode"));
3470#ifdef FEAT_LISP
3471 main_msg(_("-l\t\t\tLisp mode"));
3472#endif
3473 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3474 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003475 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3476#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003477 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003478#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003479 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3480 main_msg(_("-r\t\t\tList swap files and exit"));
3481 main_msg(_("-r (with file name)\tRecover crashed session"));
3482 main_msg(_("-L\t\t\tSame as -r"));
3483#ifdef AMIGA
3484 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3485 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3486#endif
3487#ifdef FEAT_ARABIC
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003488 main_msg(_("-A\t\t\tStart in Arabic mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003489#endif
3490#ifdef FEAT_RIGHTLEFT
3491 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3492#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003493 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003494 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003495 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003496 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3497#ifdef FEAT_GUI
3498 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3499#endif
3500 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003501 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003502 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3503 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3504 main_msg(_("+\t\t\tStart at end of file"));
3505 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003506 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003507 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3508 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3509 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3510 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3511 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3512#ifdef FEAT_CRYPT
3513 main_msg(_("-x\t\t\tEdit encrypted files"));
3514#endif
3515#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3516# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar86181df2020-05-11 23:14:04 +02003517 main_msg(_("-display <display>\tConnect Vim to this particular X-server"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003518# endif
3519 main_msg(_("-X\t\t\tDo not connect to X server"));
3520#endif
3521#ifdef FEAT_CLIENTSERVER
3522 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3523 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3524 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3525 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003526 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003527 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3528 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3529 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3530 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3531#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003532#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003533 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003534#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003535#ifdef FEAT_VIMINFO
3536 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3537#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003538 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003539 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3540 main_msg(_("--version\t\tPrint version information and exit"));
3541
3542#ifdef FEAT_GUI_X11
3543# ifdef FEAT_GUI_MOTIF
3544 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3545# else
3546# ifdef FEAT_GUI_ATHENA
3547# ifdef FEAT_GUI_NEXTAW
3548 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3549# else
3550 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3551# endif
3552# endif
3553# endif
Bram Moolenaar86181df2020-05-11 23:14:04 +02003554 main_msg(_("-display <display>\tRun Vim on <display>"));
Bram Moolenaar9e6ba8c2020-05-12 22:21:26 +02003555 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003556 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3557 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3558 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3559 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3560 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3561 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3562 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3563 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3564# ifdef FEAT_GUI_ATHENA
3565 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3566# endif
3567 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3568 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3569 main_msg(_("-xrm <resource>\tSet the specified resource"));
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003570#endif // FEAT_GUI_X11
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003571#ifdef FEAT_GUI_GTK
3572 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003573 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3574 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003575 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3576 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003577 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003578 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
Bram Moolenaar86181df2020-05-11 23:14:04 +02003579 main_msg(_("-display <display>\tRun Vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003580 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003581 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003582 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003583#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003584#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003585# ifdef VIMDLL
3586 if (gui.starting)
3587# endif
3588 {
3589 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
3590 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
3591 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003592#endif
3593
3594#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003595 // Gnome gives extra messages for --help if we continue, but not for -h.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003596 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003597 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003598 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003599 gui.dofork = FALSE;
3600 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003601 else
3602#endif
3603 mch_exit(0);
3604}
3605
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003606/*
3607 * Check the result of the ATTENTION dialog:
3608 * When "Quit" selected, exit Vim.
3609 * When "Recover" selected, recover the file.
3610 */
3611 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003612check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003613{
3614 if (swap_exists_action == SEA_QUIT)
3615 getout(1);
3616 handle_swap_exists(NULL);
3617}
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003618
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003619#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003620
Bram Moolenaar595297d2017-03-04 19:11:12 +01003621#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003622 static void
3623set_progpath(char_u *argv0)
3624{
3625 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003626
Bram Moolenaar4f974752019-02-17 17:44:42 +01003627# ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003628 // A relative path containing a "/" will become invalid when using ":cd",
3629 // turn it into a full path.
3630 // On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3631 // this.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003632 char_u *path = NULL;
3633
3634 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3635 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003636# else
3637 char_u buf[MAXPATHL + 1];
3638# ifdef PROC_EXE_LINK
3639 char linkbuf[MAXPATHL + 1];
3640 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003641
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003642 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3643 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003644 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003645 linkbuf[len] = NUL;
3646 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003647 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003648# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003649
3650 if (!mch_isFullName(val))
3651 {
3652 if (gettail(val) != val
3653 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3654 val = buf;
3655 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003656# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003657
Bram Moolenaar08cab962017-03-04 14:37:18 +01003658 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003659
Bram Moolenaar4f974752019-02-17 17:44:42 +01003660# ifdef MSWIN
Bram Moolenaar43663192017-03-05 14:29:12 +01003661 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003662# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003663}
3664
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003665#endif // NO_VIM_MAIN