blob: 6875ee5d38739529d4b009ad19975691c7087372 [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__
K.Takata972db232022-02-04 10:45:38 +000014# include <cygwin/version.h>
15# include <sys/cygwin.h> // for cygwin_conv_to_posix_path() and/or
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010016 // cygwin_conv_path()
Bram Moolenaarb4210b32004-06-13 14:51:16 +000017# include <limits.h>
18#endif
19
Bram Moolenaarafde13b2019-04-28 19:46:49 +020020#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar97ff9b92016-06-26 20:37:46 +020021# include "iscygpty.h"
22#endif
23
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010024// Values for edit_type.
25#define EDIT_NONE 0 // no edit type yet
26#define EDIT_FILE 1 // file name argument[s] given, use argument list
27#define EDIT_STDIN 2 // read file from stdin
28#define EDIT_TAG 3 // tag name argument given, use tagname
29#define EDIT_QF 4 // start in quickfix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +000030
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010031#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010032static int file_owned(char *fname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +000033#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static void mainerr(int, char_u *);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020035static void early_arg_scan(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010036#ifndef NO_VIM_MAIN
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010037static void usage(void);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void parse_command_name(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static void command_line_scan(mparm_T *parmp);
40static void check_tty(mparm_T *parmp);
41static void read_stdin(void);
42static void create_windows(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010043static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010044static void exe_pre_commands(mparm_T *parmp);
45static void exe_commands(mparm_T *parmp);
46static void source_startup_scripts(mparm_T *parmp);
47static void main_start_gui(void);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010048static void check_swap_exists_action(void);
Bram Moolenaar08cab962017-03-04 14:37:18 +010049# ifdef FEAT_EVAL
50static void set_progpath(char_u *argv0);
51# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000052#endif
53
54
Bram Moolenaarb4210b32004-06-13 14:51:16 +000055/*
56 * Different types of error messages.
57 */
58static char *(main_errors[]) =
59{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000060 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000061#define ME_UNKNOWN_OPTION 0
62 N_("Too many edit arguments"),
63#define ME_TOO_MANY_ARGS 1
64 N_("Argument missing after"),
65#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +000066 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000067#define ME_GARBAGE 3
68 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
69#define ME_EXTRA_CMD 4
70 N_("Invalid argument for"),
71#define ME_INVALID_ARG 5
72};
73
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010074#ifndef PROTO // don't want a prototype for main()
Bram Moolenaara6044292017-04-02 18:19:53 +020075
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010076// Various parameters passed between main() and other functions.
Bram Moolenaara6044292017-04-02 18:19:53 +020077static mparm_T params;
78
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010079#ifndef NO_VIM_MAIN // skip this for unittests
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020080
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010081static char_u *start_dir = NULL; // current working dir on startup
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020082
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +020083static int has_dash_c_arg = FALSE;
84
Bram Moolenaarafde13b2019-04-28 19:46:49 +020085# ifdef VIMDLL
86__declspec(dllexport)
87# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000088 int
Bram Moolenaar796cc422019-04-03 20:31:00 +020089# ifdef MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +000090VimMain
91# else
92main
93# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +010094(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +000095{
Bram Moolenaar77780b62018-03-01 23:10:45 +010096#if defined(STARTUPTIME) || defined(CLEAN_RUNTIMEPATH)
Bram Moolenaar3f269672009-11-03 11:11:11 +000097 int i;
98#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000099
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000100 /*
101 * Do any system-specific initialisations. These can NOT use IObuff or
102 * NameBuff. Thus emsg2() cannot be called!
103 */
104 mch_early_init();
105
Bram Moolenaar4f974752019-02-17 17:44:42 +0100106#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +0200107 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200108 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200109 * convert when 'encoding' changes. Get the unexpanded arguments.
110 */
111 argc = get_cmd_argsW(&argv);
112#endif
113
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100114 // Many variables are in "params" so that we can pass them to invoked
115 // functions without a lot of arguments. "argc" and "argv" are also
116 // copied, so that they can be changed.
Bram Moolenaara80faa82020-04-12 19:37:17 +0200117 CLEAR_FIELD(params);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000118 params.argc = argc;
119 params.argv = argv;
120 params.want_full_screen = TRUE;
121#ifdef FEAT_EVAL
122 params.use_debug_break_level = -1;
123#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000124 params.window_count = -1;
Bram Moolenaar58d98232005-07-23 22:25:46 +0000125
Bram Moolenaar99685e62013-05-11 13:56:18 +0200126#ifdef FEAT_RUBY
127 {
128 int ruby_stack_start;
129 vim_ruby_init((void *)&ruby_stack_start);
130 }
131#endif
132
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000133#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000134 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000135#endif
136
137#ifdef MEM_PROFILE
138 atexit(vim_mem_profile_dump);
139#endif
140
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100141#if defined(STARTUPTIME) || defined(FEAT_JOB_CHANNEL)
142 // Need to find "--startuptime" and "--log" before actually parsing
143 // arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100144 for (i = 1; i < argc - 1; ++i)
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100145 {
146# ifdef STARTUPTIME
147 if (STRICMP(argv[i], "--startuptime") == 0 && time_fd == NULL)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000148 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000149 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000150 TIME_MSG("--- VIM STARTING ---");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000151 }
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100152# endif
153# ifdef FEAT_JOB_CHANNEL
154 if (STRICMP(argv[i], "--log") == 0)
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100155 ch_logfile((char_u *)(argv[i + 1]), (char_u *)"ao");
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100156# endif
157 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000158#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000159 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000160
Bram Moolenaar07268702018-03-01 21:57:32 +0100161#ifdef CLEAN_RUNTIMEPATH
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100162 // Need to find "--clean" before actually parsing arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100163 for (i = 1; i < argc; ++i)
164 if (STRICMP(argv[i], "--clean") == 0)
165 {
166 params.clean = TRUE;
167 break;
168 }
169#endif
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200170#ifdef MSWIN
Bram Moolenaar4a228972021-05-01 22:41:39 +0200171 // Need to find "-register" and "-unregister" before loading any libraries.
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200172 for (i = 1; i < argc; ++i)
Bram Moolenaar4a228972021-05-01 22:41:39 +0200173 if ((STRICMP(argv[i] + 1, "register") == 0
174 || STRICMP(argv[i] + 1, "unregister") == 0)
175 && (argv[i][0] == '-' || argv[i][0] == '/'))
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200176 {
177 found_register_arg = TRUE;
178 break;
179 }
180#endif
181
182 /*
183 * Various initialisations shared with tests.
184 */
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200185 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000186
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200187#ifdef VIMDLL
188 // Check if the current executable file is for the GUI subsystem.
189 gui.starting = mch_is_gui_executable();
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +0200190#elif defined(FEAT_GUI_MSWIN)
191 gui.starting = TRUE;
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200192#endif
193
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000194#ifdef FEAT_CLIENTSERVER
195 /*
196 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000197 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000198 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000199 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000200#endif
201
202 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000203 * Figure out the way to work from the command name argv[0].
204 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000205 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000206 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000207
208 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000209 * Process the command line arguments. File names are put in the global
210 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000211 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000212 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000213 TIME_MSG("parsing arguments");
214
215 /*
216 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000217 * there is no terminal version, and on Windows we can't fork one off with
218 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000219 */
220#ifdef ALWAYS_USE_GUI
221 gui.starting = TRUE;
222#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000223# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000224 /*
225 * Check if the GUI can be started. Reset gui.starting if not.
226 * Don't know about other systems, stay on the safe side and don't check.
227 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000228 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000229 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000230 if (gui_init_check() == FAIL)
231 {
232 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000233
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100234 // When running "evim" or "gvim -y" we need the menus, exit if we
235 // don't have them.
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000236 if (params.evim_mode)
237 mch_exit(1);
238 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000239 }
240# endif
241#endif
242
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000243 if (GARGCOUNT > 0)
244 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100245#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000246 /*
247 * Expand wildcards in file names.
248 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000249 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000250 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200251 start_dir = alloc(MAXPATHL);
252 if (start_dir != NULL)
253 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100254 // Temporarily add '(' and ')' to 'isfname'. These are valid
255 // filename characters but are excluded from 'isfname' to make
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000256 // "gf" work on a file name in parentheses (e.g.: see vim.h).
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000257 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000258 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000259 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200260 if (start_dir != NULL)
261 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000262 }
263#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200264 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000265 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000266
Bram Moolenaar4f974752019-02-17 17:44:42 +0100267#ifdef MSWIN
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000268 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100269 // Remember the number of entries in the argument list. If it changes
270 // we don't react on setting 'encoding'.
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000271 set_alist_count();
272 }
273#endif
274
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000275#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000276 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000277 {
278 /*
279 * If there is one filename, fully qualified, we have very probably
280 * been invoked from explorer, so change to the file's directory.
281 * Hint: to avoid this when typing a command use a forward slash.
282 * If the cd fails, it doesn't matter.
283 */
Bram Moolenaar05268152021-11-18 18:53:45 +0000284 if (vim_chdirfile(params.fname, "drop") == OK)
285 last_chdir_reason = "drop";
Bram Moolenaarf6303872015-04-03 17:59:43 +0200286 if (start_dir != NULL)
287 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000288 }
289#endif
290 TIME_MSG("expanding arguments");
291
292#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000293 if (params.diff_mode && params.window_count == -1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100294 params.window_count = 0; // open up to 3 windows
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000295#endif
296
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100297 // Don't redraw until much later.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000298 ++RedrawingDisabled;
299
300 /*
301 * When listing swap file names, don't do cursor positioning et. al.
302 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200303 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000304 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000305
306 /*
307 * When certain to start the GUI, don't check capabilities of terminal.
308 * For GTK we can't be sure, but when started from the desktop it doesn't
309 * make sense to try using a terminal.
310 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200311#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
312 || defined(VIMDLL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000313 if (gui.starting
314# ifdef FEAT_GUI_GTK
315 && !isatty(2)
316# endif
317 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000318 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000319#endif
320
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000321 /*
322 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100323 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000324 * Note that we may use mch_exit() before mch_init()!
325 */
326 mch_init();
327 TIME_MSG("shell init");
328
329#ifdef USE_XSMP
330 /*
331 * For want of anywhere else to do it, try to connect to xsmp here.
332 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
333 * Hijacking -X 'no X connection' to also disable XSMP connection as that
334 * has a similar delay upon failure.
335 * Only try if SESSION_MANAGER is set to something non-null.
336 */
337 if (!x_no_connect)
338 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000339 char *p = getenv("SESSION_MANAGER");
340
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000341 if (p != NULL && *p != NUL)
342 {
343 xsmp_init();
344 TIME_MSG("xsmp init");
345 }
346 }
347#endif
348
349 /*
350 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000351 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000352 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000353
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100354#ifdef _IOLBF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100355 // Ensure output works usefully without a tty: buffer lines instead of
356 // fully buffered.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100357 if (silent_mode)
358 setvbuf(stdout, NULL, _IOLBF, 0);
359#endif
360
Bram Moolenaar9404a182019-05-03 22:25:40 +0200361 // This message comes before term inits, but after setting "silent_mode"
362 // when the input is not a tty. Omit the message with --not-a-term.
363 if (GARGCOUNT > 1 && !silent_mode && !is_not_a_term())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000364 printf(_("%d files to edit\n"), GARGCOUNT);
365
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000366 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000367 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100368 termcapinit(params.term); // set terminal name and get terminal
369 // capabilities (will set full_screen)
370 screen_start(); // don't know where cursor is now
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000371 TIME_MSG("Termcap init");
372 }
373
374 /*
375 * Set the default values for the options that use Rows and Columns.
376 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100377 ui_get_shellsize(); // inits Rows and Columns
Luuk van Baal470a1412022-09-14 01:27:23 +0100378 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000379#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100380 // Set the 'diff' option now, so that it can be checked for in a .vimrc
381 // file. There is no buffer yet though.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000382 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000383 diff_win_options(firstwin, FALSE);
384#endif
385
386 cmdline_row = Rows - p_ch;
387 msg_row = cmdline_row;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100388 screenalloc(FALSE); // allocate screen buffers
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000389 set_init_2();
390 TIME_MSG("inits 2");
391
392 msg_scroll = TRUE;
393 no_wait_return = TRUE;
394
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100395 init_mappings(); // set up initial mappings
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000396
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100397 init_highlight(TRUE, FALSE); // set the default highlight groups
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000398 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000399
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200400#if defined(FEAT_TERMRESPONSE)
401 init_term_props(TRUE);
402#endif
403
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000404#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100405 // Set the break level after the terminal is initialized.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000406 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000407#endif
408
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100409 // Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
410 // Allows for setting 'loadplugins' there.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200411 if (params.use_vimrc != NULL
412 && (STRCMP(params.use_vimrc, "NONE") == 0
413 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
414 p_lpl = FALSE;
415
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100416 // Execute --cmd arguments.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200417 exe_pre_commands(&params);
418
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100419 // Source startup scripts.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200420 source_startup_scripts(&params);
421
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100422#ifdef FEAT_MZSCHEME
423 /*
424 * Newer version of MzScheme (Racket) require earlier (trampolined)
425 * initialisation via scheme_main_setup.
426 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200427 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200428 * Do source startup scripts, so that 'mzschemedll' can be set.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100429 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200430 return mzscheme_main();
431#else
432 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100433#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200434}
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100435#endif // NO_VIM_MAIN
436#endif // PROTO
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100437
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200438/*
439 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
440 * things simple.
441 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
442 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100443 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200444vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100445{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100446#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000447#ifdef FEAT_EVAL
448 /*
449 * Read all the plugin files.
450 * Only when compiled with +eval, since most plugins need it.
451 */
452 if (p_lpl)
453 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200454 char_u *rtp_copy = NULL;
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100455 char_u *plugin_pattern = (char_u *)
456# if defined(VMS) || defined(AMIGA) // VMS and Amiga don't handle the "**".
457 "plugin/*.vim"
458# else
459 "plugin/**/*.vim"
460# endif
461 ;
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200462
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100463 // First add all package directories to 'runtimepath', so that their
464 // autoload directories can be found. Only if not done already with a
465 // :packloadall command.
466 // Make a copy of 'runtimepath', so that source_runtime does not use
467 // the pack directories.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200468 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200469 {
470 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200471 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200472 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200473
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100474 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy, plugin_pattern,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475 DIP_ALL | DIP_NOAFTER, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000476 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200477 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100478
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100479 // Only source "start" packages if not done already with a :packloadall
480 // command.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200481 if (!did_source_packages)
482 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100483 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200484
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100485 source_runtime(plugin_pattern, DIP_ALL | DIP_AFTER);
Bram Moolenaar66459b72016-08-06 19:01:55 +0200486 TIME_MSG("loading after plugins");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000487 }
488#endif
489
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000490#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100491 // Decide about window layout for diff mode after reading vimrc.
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000492 if (params.diff_mode && params.window_layout == 0)
493 {
494 if (diffopt_horizontal())
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100495 params.window_layout = WIN_HOR; // use horizontal split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000496 else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100497 params.window_layout = WIN_VER; // use vertical split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000498 }
499#endif
500
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000501 /*
502 * Recovery mode without a file name: List swap files.
503 * This uses the 'dir' option, therefore it must be after the
504 * initializations.
505 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200506 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000507 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200508 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000509 mch_exit(0);
510 }
511
512 /*
513 * Set a few option defaults after reading .vimrc files:
514 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
515 */
516 set_init_3();
517 TIME_MSG("inits 3");
518
519 /*
520 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
521 * Note that this overrides anything from a vimrc file.
522 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000523 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000524 p_uc = 0;
525
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000526#ifdef FEAT_GUI
527 if (gui.starting)
528 {
Bram Moolenaarc7226682019-08-17 16:33:23 +0200529# if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100530 // When something caused a message from a vimrc script, need to output
531 // an extra newline before the shell prompt.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000532 if (did_emsg || msg_didout)
533 putchar('\n');
Bram Moolenaarc7226682019-08-17 16:33:23 +0200534# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000535
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100536 gui_start(NULL); // will set full_screen to TRUE
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000537 TIME_MSG("starting GUI");
538
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100539 // When running "evim" or "gvim -y" we need the menus, exit if we
540 // don't have them.
Bram Moolenaar58d98232005-07-23 22:25:46 +0000541 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000542 mch_exit(1);
Luuk van Baal470a1412022-09-14 01:27:23 +0100543 firstwin->w_prev_height = firstwin->w_height; // may have changed
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000544 }
545#endif
546
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000547#ifdef FEAT_VIMINFO
548 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000549 * Read in registers, history etc, but not marks, from the viminfo file.
550 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000551 */
552 if (*p_viminfo != NUL)
553 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000554 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000555 TIME_MSG("reading viminfo");
556 }
557#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100558#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100559 // It's better to make v:oldfiles an empty list than NULL.
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100560 if (get_vim_var_list(VV_OLDFILES) == NULL)
561 set_vim_var_list(VV_OLDFILES, list_alloc());
562#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000563
564#ifdef FEAT_QUICKFIX
565 /*
566 * "-q errorfile": Load the error file now.
567 * If the error file can't be read, exit before doing anything else.
568 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000569 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000570 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100571 char_u *enc = NULL;
572
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100573 enc = p_menc;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000574 if (params.use_ef != NULL)
575 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000576 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200577 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100578 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000579 {
580 out_char('\n');
581 mch_exit(3);
582 }
583 TIME_MSG("reading errorfile");
584 }
585#endif
586
587 /*
588 * Start putting things on the screen.
589 * Scroll screen down before drawing over it
590 * Clear screen now, so file message will not be cleared.
591 */
592 starting = NO_BUFFERS;
593 no_wait_return = FALSE;
594 if (!exmode_active)
595 msg_scroll = FALSE;
596
597#ifdef FEAT_GUI
598 /*
599 * This seems to be required to make callbacks to be called now, instead
600 * of after things have been put on the screen, which then may be deleted
601 * when getting a resize callback.
602 * For the Mac this handles putting files dropped on the Vim icon to
603 * global_alist.
604 */
605 if (gui.in_use)
606 {
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100607 gui_wait_for_chars(50L, typebuf.tb_change_cnt);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000608 TIME_MSG("GUI delay");
609 }
610#endif
611
612#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
613 qnx_clip_init();
614#endif
615
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200616#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
617 clip_init(TRUE);
618#endif
619
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000620#ifdef FEAT_XCLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100621 // Start using the X clipboard, unless the GUI was started.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000622# ifdef FEAT_GUI
623 if (!gui.in_use)
624# endif
625 {
626 setup_term_clip();
627 TIME_MSG("setup clipboard");
628 }
629#endif
630
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000631#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100632 // Prepare for being a Vim server.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000633 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000634#endif
635
636 /*
637 * If "-" argument given: Read file from stdin.
638 * Do this before starting Raw mode, because it may change things that the
639 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
640 * are the same terminal: "cat | vim -".
641 * Using autocommands here may cause trouble...
642 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000643 if (params.edit_type == EDIT_STDIN && !recoverymode)
644 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000645
646#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100647 // When switching screens and something caused a message from a vimrc
648 // script, need to output an extra newline on exit.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000649 if ((did_emsg || msg_didout) && *T_TI != NUL)
650 newline_on_exit = TRUE;
651#endif
652
653 /*
K.Takataeeec2542021-06-02 13:28:16 +0200654 * When done something that is not allowed or given an error message call
Bram Moolenaar13608d82022-08-29 15:06:50 +0100655 * wait_return(). This must be done before starttermcap(), because it may
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000656 * switch to another screen. It must be done after settmode(TMODE_RAW),
657 * because we want to react on a single key stroke.
658 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000659 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000660 */
661 settmode(TMODE_RAW);
662 TIME_MSG("setting raw mode");
663
664 if (need_wait_return || msg_didany)
665 {
666 wait_return(TRUE);
667 TIME_MSG("waiting for return");
668 }
669
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100670 starttermcap(); // start termcap if not done by wait_return()
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000671 TIME_MSG("start termcap");
672
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200673 setmouse(); // may start using the mouse
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000674 if (scroll_region)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100675 scroll_region_reset(); // In case Rows changed
676 scroll_start(); // may scroll the screen to the right position
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000677
Bram Moolenaar651fca82021-11-29 20:39:38 +0000678#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar40385db2018-08-07 22:31:44 +0200679 term_push_title(SAVE_RESTORE_BOTH);
680#endif
681
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000682 /*
683 * Don't clear the screen when starting in Ex mode, unless using the GUI.
684 */
685 if (exmode_active
686#ifdef FEAT_GUI
687 && !gui.in_use
688#endif
689 )
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100690 set_must_redraw(UPD_CLEAR);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000691 else
692 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100693 screenclear(); // clear screen
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000694 TIME_MSG("clearing screen");
695 }
696
697#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000698 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000699 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100700 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200701 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000702 TIME_MSG("getting crypt key");
703 }
704#endif
705
706 no_wait_return = TRUE;
707
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000708 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000709 * Create the requested number of windows and edit buffers in them.
710 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000711 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000712 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000713 TIME_MSG("opening buffers");
714
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000715#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100716 // clear v:swapcommand
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000717 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
718#endif
719
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100720 // Ex starts at last line of the file
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000721 if (exmode_active)
722 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
723
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000724 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
725 TIME_MSG("BufEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000726 setpcmark();
727
728#ifdef FEAT_QUICKFIX
729 /*
730 * When started with "-q errorfile" jump to first error now.
731 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000732 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000733 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000734 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000735 TIME_MSG("jump to first error");
736 }
737#endif
738
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000739 /*
740 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000741 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000742 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200743 edit_buffers(&params, start_dir);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200744 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000745
746#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000747 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000748 {
749 win_T *wp;
750
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100751 // set options in each window for "vimdiff".
Bram Moolenaar29323592016-07-24 22:04:11 +0200752 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000753 diff_win_options(wp, TRUE);
754 }
755#endif
756
757 /*
758 * Shorten any of the filenames, but only when absolute.
759 */
760 shorten_fnames(FALSE);
761
762 /*
763 * Need to jump to the tag before executing the '-c command'.
764 * Makes "vim -c '/return' -t main" work.
765 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000766 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000767 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000768 swap_exists_did_quit = FALSE;
Bram Moolenaar146522e2005-12-16 21:55:46 +0000769
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000770 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000771 do_cmdline_cmd(IObuff);
772 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000773
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100774 // If the user doesn't want to edit the file then we quit here.
Bram Moolenaar146522e2005-12-16 21:55:46 +0000775 if (swap_exists_did_quit)
776 getout(1);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000777 }
778
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100779 // Execute any "+", "-c" and "-S" arguments.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000780 if (params.n_commands > 0)
781 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000782
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100783 // Must come before the may_req_ calls.
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200784 starting = 0;
785
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100786#if defined(FEAT_TERMRESPONSE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100787 // Must be done before redrawing, puts a few characters on the screen.
Bram Moolenaara45551a2020-06-09 15:57:37 +0200788 check_terminal_behavior();
Bram Moolenaar976787d2017-06-04 15:45:50 +0200789#endif
790
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000791 RedrawingDisabled = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100792 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000793 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000794
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100795 // 'autochdir' has been postponed
Bram Moolenaar6f470022018-04-10 18:47:20 +0200796 DO_AUTOCHDIR;
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200797
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000798#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100799 // Requesting the termresponse is postponed until here, so that a "-c q"
800 // argument doesn't make it appear in the shell Vim was started from.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000801 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200802
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200803 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000804#endif
805
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100806 // start in insert mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000807 if (p_im)
808 need_start_insertmode = TRUE;
809
Bram Moolenaar14735512016-03-26 21:00:08 +0100810#ifdef FEAT_EVAL
811 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
812#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000813 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
814 TIME_MSG("VimEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000815
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200816#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100817 // Adjust default register name for "unnamed" in 'clipboard'. Can only be
818 // done after the clipboard is available and all initial commands that may
819 // modify the 'clipboard' setting have run; i.e. just before entering the
820 // main loop.
Bram Moolenaar439c0362020-06-06 15:58:03 +0200821 reset_reg_var();
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200822#endif
823
Bram Moolenaar8a3bb562018-03-04 20:14:14 +0100824#if defined(FEAT_DIFF)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100825 // When a startup script or session file setup for diff'ing and
826 // scrollbind, sync the scrollbind now.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000827 if (curwin->w_p_diff && curwin->w_p_scb)
828 {
829 update_topline();
830 check_scrollbind((linenr_T)0, 0L);
831 TIME_MSG("diff scrollbinding");
832 }
833#endif
834
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200835#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
836# ifdef VIMDLL
837 if (!gui.in_use)
838# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100839 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000840#endif
841
Bram Moolenaar4033c552017-09-16 20:54:51 +0200842#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100843 // When tab pages were created, may need to update the tab pages line and
844 // scrollbars. This is skipped while creating them.
h-east6073f132021-12-24 11:57:06 +0000845 if (gui.in_use && first_tabpage->tp_next != NULL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000846 {
847 out_flush();
848 gui_init_which_components(NULL);
849 gui_update_scrollbars(TRUE);
850 }
851 need_mouse_correct = TRUE;
852#endif
853
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100854 // If ":startinsert" command used, stuff a dummy command to be able to
855 // call normal_cmd(), which will then start Insert mode.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000856 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000857 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000858
859#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200860 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200861 {
862# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200863# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar4f974752019-02-17 17:44:42 +0100864 && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200865 if (gui.in_use)
866 {
867 mch_errmsg(_("netbeans is not supported with this GUI\n"));
868 mch_exit(2);
869 }
870# endif
871# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100872 // Tell the client that it can start sending commands.
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200873 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200874 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000875#endif
876
Bram Moolenaare7ddf4e2020-02-03 21:29:30 +0100877 // Redraw at least once, also when 'lazyredraw' is set, to make sure the
878 // window title gets updated.
879 do_redraw = TRUE;
880
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000881 TIME_MSG("before starting main loop");
882
883 /*
884 * Call the main command loop. This never returns.
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200885 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000886 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000887
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100888#endif // NO_VIM_MAIN
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200889
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000890 return 0;
891}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000892
893/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200894 * Initialisation shared by main() and some tests.
895 */
896 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200897common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200898{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100899 estack_init();
Bram Moolenaar438d1762018-09-30 17:11:48 +0200900 cmdline_init();
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200901
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100902 (void)mb_init(); // init mb_bytelen_tab[] to ones
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200903#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100904 eval_init(); // init global variables
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200905#endif
906
907#ifdef __QNXNTO__
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100908 qnx_init(); // PhAttach() for clipboard, (and gui)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200909#endif
910
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200911 /*
912 * Allocate space for the generic buffers (needed for set_init_1() and
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100913 * emsg()).
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200914 */
915 if ((IObuff = alloc(IOSIZE)) == NULL
916 || (NameBuff = alloc(MAXPATHL)) == NULL)
917 mch_exit(0);
918 TIME_MSG("Allocated generic buffers");
919
920#ifdef NBDEBUG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100921 // Wait a moment for debugging NetBeans. Must be after allocating
922 // NameBuff.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200923 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
924 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
925 TIME_MSG("NetBeans debug wait");
926#endif
927
928#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
929 /*
930 * Setup to use the current locale (for ctype() and many other things).
931 * NOTE: Translated messages with encodings other than latin1 will not
932 * work until set_init_1() has been called!
933 */
934 init_locale();
935 TIME_MSG("locale set");
936#endif
937
938#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100939 gui.dofork = TRUE; // default is to use fork()
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200940#endif
941
942 /*
943 * Do a first scan of the arguments in "argv[]":
944 * -display or --display
945 * --server...
946 * --socketid
947 * --windowid
948 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200949 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200950
Bram Moolenaard0573012017-10-28 21:11:06 +0200951#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100952 // Prepare for possibly starting GUI sometime
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200953 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200954 TIME_MSG("GUI prepared");
955#endif
956
957#ifdef FEAT_CLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100958 clip_init(FALSE); // Initialise clipboard stuff
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200959 TIME_MSG("clipboard setup");
960#endif
961
962 /*
963 * Check if we have an interactive window.
964 * On the Amiga: If there is no window, we open one with a newcli command
965 * (needed for :! to * work). mch_check_win() will also handle the -d or
966 * -dev argument.
967 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100968 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200969 TIME_MSG("window checked");
970
971 /*
972 * Allocate the first window and buffer.
973 * Can't do anything without it, exit when it fails.
974 */
975 if (win_alloc_first() == FAIL)
976 mch_exit(0);
977
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100978 init_yank(); // init yank buffers
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200979
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100980 alist_init(&global_alist); // Init the argument list to empty.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200981 global_alist.id = 0;
982
983 /*
984 * Set the default values for the options.
985 * NOTE: Non-latin1 translated messages are working only after this,
986 * because this is where "has_mbyte" will be set, which is used by
987 * msg_outtrans_len_attr().
988 * First find out the home directory, needed to expand "~" in options.
989 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100990 init_homedir(); // find real value of $HOME
Bram Moolenaar07268702018-03-01 21:57:32 +0100991 set_init_1(paramp->clean);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200992 TIME_MSG("inits 1");
993
994#ifdef FEAT_EVAL
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100995 // set v:lang and v:ctype
996 set_lang_var();
997
998 // set v:argv
999 set_argv_var(paramp->argv, paramp->argc);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001000#endif
Bram Moolenaar6436cd82018-12-27 00:28:33 +01001001
1002#ifdef FEAT_SIGNS
1003 init_signs();
1004#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001005}
1006
1007/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001008 * Return TRUE when the --not-a-term argument was found.
1009 */
1010 int
1011is_not_a_term()
1012{
1013 return params.not_a_term;
1014}
1015
Bram Moolenaar7007e312021-03-27 12:11:33 +01001016/*
1017 * Return TRUE when the --not-a-term argument was found or the GUI is in use.
1018 */
1019 static int
1020is_not_a_term_or_gui()
1021{
1022 return params.not_a_term
1023#ifdef FEAT_GUI
1024 || gui.in_use
1025#endif
1026 ;
1027}
1028
Bram Moolenaar2d12c252022-06-13 21:42:45 +01001029#if defined(FEAT_GUI) || defined(PROTO)
1030/*
1031 * If a --gui-dialog-file argument was given return the file name.
1032 * Otherwise return NULL.
1033 */
1034 char_u *
1035get_gui_dialog_file(void)
1036{
1037 return params.gui_dialog_file;
1038}
1039#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001040
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001041// When TRUE in a safe state when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001042static int was_safe = FALSE;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001043static oparg_T *current_oap = NULL;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001044
1045/*
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001046 * Return TRUE if an operator was started but not finished yet.
1047 * Includes typing a count or a register name.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001048 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001049 int
1050op_pending(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001051{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001052 return !(current_oap != NULL
1053 && !finish_op
1054 && current_oap->prev_opcount == 0
1055 && current_oap->prev_count0 == 0
1056 && current_oap->op_type == OP_NOP
1057 && current_oap->regname == NUL);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001058}
1059
1060/*
Bram Moolenaard103ee72019-09-18 21:15:31 +02001061 * Return whether currently it is safe, assuming it was safe before (high level
1062 * state didn't change).
1063 */
1064 static int
1065is_safe_now(void)
1066{
1067 return stuff_empty()
1068 && typebuf.tb_len == 0
1069 && scriptin[curscript] == NULL
Bram Moolenaar4fa13462022-02-05 12:39:24 +00001070#ifdef FEAT_EVAL
1071 && !debug_mode
1072#endif
1073 && !global_busy;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001074}
1075
1076/*
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001077 * Trigger SafeState if currently in s safe state, that is "safe" is TRUE and
1078 * there is no typeahead.
1079 */
1080 void
1081may_trigger_safestate(int safe)
1082{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001083 int is_safe = safe && is_safe_now();
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001084
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001085#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001086 if (was_safe != is_safe)
1087 // Only log when the state changes, otherwise it happens at nearly
1088 // every key stroke.
Bram Moolenaard103ee72019-09-18 21:15:31 +02001089 ch_log(NULL, is_safe ? "SafeState: Start triggering"
1090 : "SafeState: Stop triggering");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001091#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001092 if (is_safe)
1093 apply_autocmds(EVENT_SAFESTATE, NULL, NULL, FALSE, curbuf);
1094 was_safe = is_safe;
1095}
1096
1097/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001098 * Something changed which causes the state possibly to be unsafe, e.g. a
1099 * character was typed. It will remain unsafe until the next call to
1100 * may_trigger_safestate().
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001101 */
1102 void
Bram Moolenaard103ee72019-09-18 21:15:31 +02001103state_no_longer_safe(char *reason UNUSED)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001104{
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001105#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001106 if (was_safe)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001107 ch_log(NULL, "SafeState: reset: %s", reason);
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001108#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001109 was_safe = FALSE;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001110}
1111
Dominique Pelle748b3082022-01-08 12:41:16 +00001112#if defined(FEAT_EVAL) || defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001113 int
1114get_was_safe_state(void)
1115{
1116 return was_safe;
1117}
Dominique Pelle748b3082022-01-08 12:41:16 +00001118#endif
Bram Moolenaard103ee72019-09-18 21:15:31 +02001119
Dominique Pelle748b3082022-01-08 12:41:16 +00001120#if defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001121/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001122 * Invoked when leaving code that invokes callbacks. Then trigger
1123 * SafeStateAgain, if it was safe when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001124 */
1125 void
Bram Moolenaar37d18072019-09-17 20:28:38 +02001126may_trigger_safestateagain(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001127{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001128 if (!was_safe)
1129 {
1130 // If the safe state was reset in state_no_longer_safe(), e.g. because
1131 // of calling feedkeys(), we check if it's now safe again (all keys
1132 // were consumed).
1133 was_safe = is_safe_now();
1134#ifdef FEAT_JOB_CHANNEL
1135 if (was_safe)
1136 ch_log(NULL, "SafeState: undo reset");
1137#endif
1138 }
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001139 if (was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02001140 {
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001141#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar101e9922019-09-25 21:43:11 +02001142 // Only do this message when another message was given, otherwise we
1143 // get lots of them.
1144 if ((did_repeated_msg & REPEATED_MSG_SAFESTATE) == 0)
1145 {
1146 int did = did_repeated_msg;
1147
1148 ch_log(NULL,
1149 "SafeState: back to waiting, triggering SafeStateAgain");
1150 did_repeated_msg = did | REPEATED_MSG_SAFESTATE;
1151 }
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001152#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001153 apply_autocmds(EVENT_SAFESTATEAGAIN, NULL, NULL, FALSE, curbuf);
Bram Moolenaar37d18072019-09-17 20:28:38 +02001154 }
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001155#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001156 else
Bram Moolenaard103ee72019-09-18 21:15:31 +02001157 ch_log(NULL,
1158 "SafeState: back to waiting, not triggering SafeStateAgain");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001159#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001160}
Dominique Pelle748b3082022-01-08 12:41:16 +00001161#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001162
1163
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001164/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001165 * Main loop: Execute Normal mode commands until exiting Vim.
1166 * Also used to handle commands in the command-line window, until the window
1167 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001168 * Also used to handle ":visual" command after ":global": execute Normal mode
1169 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001170 */
1171 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001172main_loop(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001173 int cmdwin, // TRUE when working in the command-line window
1174 int noexmode) // TRUE when return on entering Ex mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001175{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001176 oparg_T oa; // operator arguments
1177 oparg_T *prev_oap; // operator arguments
1178 volatile int previous_got_int = FALSE; // "got_int" was TRUE
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001179#ifdef FEAT_CONCEAL
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001180 // these are static to avoid a compiler warning
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001181 static linenr_T conceal_old_cursor_line = 0;
1182 static linenr_T conceal_new_cursor_line = 0;
1183 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001184#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001185
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001186 prev_oap = current_oap;
1187 current_oap = &oa;
1188
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001189#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001190 // Setup to catch a terminating error from the X server. Just ignore
1191 // it, restore the state and continue. This might not always work
1192 // properly, but at least we don't exit unexpectedly when the X server
1193 // exits while Vim is running in a console.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001195 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001196 State = MODE_NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001197 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001198 got_int = TRUE;
1199 need_wait_return = FALSE;
1200 global_busy = FALSE;
1201 exmode_active = 0;
1202 skip_redraw = FALSE;
1203 RedrawingDisabled = 0;
1204 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001205 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001206# ifdef FEAT_EVAL
1207 emsg_skip = 0;
1208# endif
1209 emsg_off = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001210 setmouse();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001211 settmode(TMODE_RAW);
1212 starttermcap();
1213 scroll_start();
1214 redraw_later_clear();
1215 }
1216#endif
1217
1218 clear_oparg(&oa);
1219 while (!cmdwin
1220#ifdef FEAT_CMDWIN
1221 || cmdwin_result == 0
1222#endif
1223 )
1224 {
1225 if (stuff_empty())
1226 {
1227 did_check_timestamps = FALSE;
1228 if (need_check_timestamps)
1229 check_timestamps(FALSE);
Bram Moolenaar13608d82022-08-29 15:06:50 +01001230 if (need_wait_return) // if wait_return() still needed ...
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001231 wait_return(FALSE); // ... call it now
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001232 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001233 {
1234 need_start_insertmode = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001235 stuffReadbuff((char_u *)"i"); // start insert mode next
1236 // skip the fileinfo message now, because it would be shown
1237 // after insert mode finishes!
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001238 need_fileinfo = FALSE;
1239 }
1240 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001241
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001242 // Reset "got_int" now that we got back to the main loop. Except when
1243 // inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1244 // the ":g" command.
1245 // For ":g/pat/vi" we reset "got_int" when used once. When used
1246 // a second time we go back to Ex mode and abort the ":g" command.
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001247 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001248 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001249 if (noexmode && global_busy && !exmode_active && previous_got_int)
1250 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001251 // Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1252 // used and keep "got_int" set, so that it aborts ":g".
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001253 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +01001254 State = MODE_NORMAL;
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001255 }
1256 else if (!global_busy || !exmode_active)
1257 {
1258 if (!quit_more)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001259 (void)vgetc(); // flush all buffers
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001260 got_int = FALSE;
1261 }
1262 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001263 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001264 else
1265 previous_got_int = FALSE;
1266
Bram Moolenaar069613c2022-01-15 15:23:44 +00001267#ifdef FEAT_EVAL
1268 // At the toplevel there is no exception handling. Discard any that
1269 // may be hanging around (e.g. from "interrupt" at the debug prompt).
1270 if (did_throw && !ex_normal_busy)
1271 discard_current_exception();
1272#endif
1273
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001274 if (!exmode_active)
1275 msg_scroll = FALSE;
1276 quit_more = FALSE;
1277
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001278 // it's not safe unless may_trigger_safestate_main() is called
1279 was_safe = FALSE;
1280
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001281 /*
1282 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1283 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1284 * update cursor and redraw.
1285 */
1286 if (skip_redraw || exmode_active)
1287 skip_redraw = FALSE;
1288 else if (do_redraw || stuff_empty())
1289 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001290#ifdef FEAT_GUI
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001291 // If ui_breakcheck() was used a resize may have been postponed.
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001292 gui_may_resize_shell();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001293#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001294#ifdef HAVE_DROP_FILE
1295 // If files were dropped while text was locked or the curbuf was
1296 // locked, this would be a good time to handle the drop.
1297 handle_any_postponed_drop();
1298#endif
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001299#ifdef FEAT_CONCEAL
1300 if (curwin->w_p_cole == 0)
1301 conceal_update_lines = FALSE;
1302#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001303
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001304 // Trigger CursorMoved if the cursor moved.
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001305 if (!finish_op && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001306 has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001307#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001308 || popup_visible
1309#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001310#ifdef FEAT_CONCEAL
1311 || curwin->w_p_cole > 0
1312#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001313 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001314 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001315 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001316 if (has_cursormoved())
1317 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1318 FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001319#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001320 if (popup_visible)
1321 popup_check_cursor_pos();
1322#endif
1323#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001324 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001325 {
1326 conceal_old_cursor_line = last_cursormoved.lnum;
1327 conceal_new_cursor_line = curwin->w_cursor.lnum;
1328 conceal_update_lines = TRUE;
1329 }
Bram Moolenaar3397f742019-06-02 18:40:06 +02001330#endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001331 last_cursormoved = curwin->w_cursor;
1332 }
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001333
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001334#if defined(FEAT_CONCEAL)
1335 if (conceal_update_lines
1336 && (conceal_old_cursor_line != conceal_new_cursor_line
1337 || conceal_cursor_line(curwin)
1338 || need_cursor_line_redraw))
1339 {
1340 if (conceal_old_cursor_line != conceal_new_cursor_line
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001341 && conceal_old_cursor_line != 0
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001342 && conceal_old_cursor_line
1343 <= curbuf->b_ml.ml_line_count)
1344 redrawWinline(curwin, conceal_old_cursor_line);
1345 redrawWinline(curwin, conceal_new_cursor_line);
1346 curwin->w_valid &= ~VALID_CROW;
1347 need_cursor_line_redraw = FALSE;
1348 }
1349#endif
1350
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001351 // Trigger TextChanged if b:changedtick differs.
Bram Moolenaar186628f2013-03-19 13:33:23 +01001352 if (!finish_op && has_textchanged()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001353 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001354 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001355 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL, FALSE, curbuf);
1356 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001357 }
Bram Moolenaar186628f2013-03-19 13:33:23 +01001358
LemonBoy09371822022-04-08 15:18:45 +01001359 // Ensure curwin->w_topline and curwin->w_leftcol are up to date
1360 // before triggering a WinScrolled autocommand.
1361 update_topline();
1362 validate_cursor();
1363
1364 if (!finish_op)
zeertzjqd58862d2022-04-12 11:32:48 +01001365 may_trigger_winscrolled();
LemonBoy09371822022-04-08 15:18:45 +01001366
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001367 // If nothing is pending and we are going to wait for the user to
1368 // type a character, trigger SafeState.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001369 may_trigger_safestate(!op_pending() && restart_edit == 0);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001370
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01001371#if defined(FEAT_DIFF)
Bram Moolenaare3521d92018-09-16 14:10:31 +02001372 // Updating diffs from changed() does not always work properly,
1373 // esp. updating folds. Do an update just before redrawing if
1374 // needed.
1375 if (curtab->tp_diff_update || curtab->tp_diff_invalid)
1376 {
1377 ex_diffupdate(NULL);
1378 curtab->tp_diff_update = FALSE;
1379 }
1380
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001381 // Scroll-binding for diff mode may have been postponed until
1382 // here. Avoids doing it for every change.
Bram Moolenaar33aec762006-01-22 23:30:12 +00001383 if (diff_need_scrollbind)
1384 {
1385 check_scrollbind((linenr_T)0, 0L);
1386 diff_need_scrollbind = FALSE;
1387 }
1388#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001389#if defined(FEAT_FOLDING)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001390 // Include a closed fold completely in the Visual area.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001391 foldAdjustVisual();
1392#endif
1393#ifdef FEAT_FOLDING
1394 /*
1395 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1396 * contain the cursor.
1397 * When 'foldopen' is "all", open the fold(s) under the cursor.
1398 * This may mark the window for redrawing.
1399 */
1400 if (hasAnyFolding(curwin) && !char_avail())
1401 {
1402 foldCheckClose();
1403 if (fdo_flags & FDO_ALL)
1404 foldOpenCursor();
1405 }
1406#endif
1407
1408 /*
1409 * Before redrawing, make sure w_topline is correct, and w_leftcol
1410 * if lines don't wrap, and w_skipcol if lines wrap.
1411 */
1412 update_topline();
1413 validate_cursor();
1414
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001415 if (VIsual_active)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001416 update_curbuf(UPD_INVERTED); // update inverted part
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001417 else if (must_redraw)
Bram Moolenaara338adc2018-01-31 20:51:47 +01001418 {
Bram Moolenaar017ba072019-09-14 21:01:23 +02001419 mch_disable_flush(); // Stop issuing gui_mch_flush().
Bram Moolenaar11a58af2019-10-24 22:32:31 +02001420 update_screen(0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01001421 mch_enable_flush();
1422 }
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01001423 else if (redraw_cmdline || clear_cmdline || redraw_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001424 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001425 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001426 if (need_maketitle)
1427 maketitle();
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001428#ifdef FEAT_VIMINFO
1429 curbuf->b_last_used = vim_time();
1430#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001431 // display message after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001432 if (keep_msg != NULL)
1433 {
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001434 char_u *p = vim_strsave(keep_msg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001435
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001436 if (p != NULL)
1437 {
1438 // msg_start() will set keep_msg to NULL, make a copy
1439 // first. Don't reset keep_msg, msg_attr_keep() uses it to
1440 // check for duplicates. Never put this message in
1441 // history.
1442 msg_hist_off = TRUE;
1443 msg_attr((char *)p, keep_msg_attr);
1444 msg_hist_off = FALSE;
1445 vim_free(p);
1446 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001447 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001448 if (need_fileinfo) // show file info after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001449 {
1450 fileinfo(FALSE, TRUE, FALSE);
1451 need_fileinfo = FALSE;
1452 }
1453
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001454 emsg_on_display = FALSE; // can delete error message now
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001455 did_emsg = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001456 msg_didany = FALSE; // reset lines_left in msg_start()
1457 may_clear_sb_text(); // clear scroll-back text on next msg
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001458 showruler(FALSE);
1459
1460 setcursor();
1461 cursor_on();
1462
1463 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001464
1465#ifdef STARTUPTIME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001466 // Now that we have drawn the first screen all the startup stuff
1467 // has been done, close any file for startup messages.
Bram Moolenaar3f269672009-11-03 11:11:11 +00001468 if (time_fd != NULL)
1469 {
1470 TIME_MSG("first screen update");
1471 TIME_MSG("--- VIM STARTED ---");
1472 fclose(time_fd);
1473 time_fd = NULL;
1474 }
1475#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001476 }
1477#ifdef FEAT_GUI
1478 if (need_mouse_correct)
1479 gui_mouse_correct();
1480#endif
1481
1482 /*
1483 * Update w_curswant if w_set_curswant has been set.
1484 * Postponed until here to avoid computing w_virtcol too often.
1485 */
1486 update_curswant();
1487
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001488#ifdef FEAT_EVAL
1489 /*
1490 * May perform garbage collection when waiting for a character, but
1491 * only at the very toplevel. Otherwise we may be using a List or
1492 * Dict internally somewhere.
1493 * "may_garbage_collect" is reset in vgetc() which is invoked through
1494 * do_exmode() and normal_cmd().
1495 */
1496 may_garbage_collect = (!cmdwin && !noexmode);
1497#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001498 /*
1499 * If we're invoked as ex, do a round of ex commands.
1500 * Otherwise, get and execute a normal mode command.
1501 */
1502 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001503 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001504 if (noexmode) // End of ":global/path/visual" commands
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001505 goto theend;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001506 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001507 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001508 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001509 {
1510#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001511 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001512 && oa.op_type == OP_NOP && oa.regname == NUL
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001513 && !VIsual_active
1514 && !skip_term_loop)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001515 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001516 // If terminal_loop() returns OK we got a key that is handled
1517 // in Normal model. With FAIL we first need to position the
1518 // cursor and the screen needs to be redrawn.
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001519 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001520 normal_cmd(&oa, TRUE);
1521 }
1522 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001523#endif
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001524 {
1525#ifdef FEAT_TERMINAL
1526 skip_term_loop = FALSE;
1527#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001528 normal_cmd(&oa, TRUE);
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001529 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02001530 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001531 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001532
1533theend:
1534 current_oap = prev_oap;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001535}
1536
1537
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001538#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001539/*
1540 * Exit, but leave behind swap files for modified buffers.
1541 */
1542 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001543getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001544{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001545# if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001546 // Ignore SIGHUP, because a dropped connection causes a read error, which
1547 // makes Vim exit and then handling SIGHUP causes various reentrance
1548 // problems.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001549 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001550# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001551
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001552 ml_close_notmod(); // close all not-modified buffers
1553 ml_sync_all(FALSE, FALSE); // preserve all swap files
1554 ml_close_all(FALSE); // close all memfiles, without deleting
1555 getout(exitval); // exit Vim properly
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001556}
1557#endif
1558
1559
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001560/*
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001561 * Exit properly. This is the only way to exit Vim after startup has
1562 * succeeded. We are certain to exit here, no way to abort it.
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001563 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001564 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001565getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001566{
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001567 exiting = TRUE;
Bram Moolenaar0bd052b2018-03-22 20:33:56 +01001568#if defined(FEAT_JOB_CHANNEL)
1569 ch_log(NULL, "Exiting...");
1570#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001571
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001572 // When running in Ex mode an error causes us to exit with a non-zero exit
1573 // code. POSIX requires this, although it's not 100% clear from the
1574 // standard.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001575 if (exmode_active)
1576 exitval += ex_exitval;
1577
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001578#ifdef FEAT_EVAL
1579 set_vim_var_type(VV_EXITING, VAR_NUMBER);
1580 set_vim_var_nr(VV_EXITING, exitval);
1581#endif
1582
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001583 // Position the cursor on the last screen line, below all the text
Bram Moolenaar7007e312021-03-27 12:11:33 +01001584 if (!is_not_a_term_or_gui())
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001585 windgoto((int)Rows - 1, 0);
1586
Bram Moolenaar58779852022-09-06 18:31:14 +01001587#ifdef FEAT_EVAL
1588 // Invoked all deferred functions in the function stack.
1589 invoke_all_defer();
1590#endif
1591
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001592#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001593 // Optionally print hashtable efficiency.
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001594 hash_debug_results();
1595#endif
1596
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001597#ifdef FEAT_GUI
1598 msg_didany = FALSE;
1599#endif
1600
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001601 if (v_dying <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001602 {
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001603 tabpage_T *tp;
1604 tabpage_T *next_tp;
1605 buf_T *buf;
1606 win_T *wp;
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001607 int unblock = 0;
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001608
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001609 // Trigger BufWinLeave for all windows, but only once per buffer.
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001610 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001611 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001612 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001613 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001614 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001615 if (wp->w_buffer == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001616 // Autocmd must have close the buffer already, skip.
Bram Moolenaar802418d2013-01-17 14:00:11 +01001617 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001618 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001619 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001620 {
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001621 bufref_T bufref;
1622
1623 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001624 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1625 buf->b_fname, FALSE, buf);
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001626 if (bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001627 CHANGEDTICK(buf) = -1; // note we did it already
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001628
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001629 // start all over, autocommands may mess up the lists
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001630 next_tp = first_tabpage;
1631 break;
1632 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001633 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001634 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001635
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001636 // Trigger BufUnload for buffers that are loaded
Bram Moolenaar29323592016-07-24 22:04:11 +02001637 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001638 if (buf->b_ml.ml_mfp != NULL)
1639 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001640 bufref_T bufref;
1641
1642 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001643 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001644 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001645 if (!bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001646 // autocmd deleted the buffer
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001647 break;
1648 }
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001649
1650 // deathtrap() blocks autocommands, but we do want to trigger
1651 // VimLeavePre.
1652 if (is_autocmd_blocked())
1653 {
1654 unblock_autocmds();
1655 ++unblock;
1656 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001657 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001658 if (unblock)
1659 block_autocmds();
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001660 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001661
1662#ifdef FEAT_VIMINFO
1663 if (*p_viminfo != NUL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001664 // Write out the registers, history, marks etc, to the viminfo file
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001665 write_viminfo(NULL, FALSE);
1666#endif
1667
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001668 if (v_dying <= 1)
Bram Moolenaarc7226682019-08-17 16:33:23 +02001669 {
1670 int unblock = 0;
1671
1672 // deathtrap() blocks autocommands, but we do want to trigger VimLeave.
1673 if (is_autocmd_blocked())
1674 {
1675 unblock_autocmds();
1676 ++unblock;
1677 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001678 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc7226682019-08-17 16:33:23 +02001679 if (unblock)
1680 block_autocmds();
1681 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001682
Bram Moolenaar05159a02005-02-26 23:04:13 +00001683#ifdef FEAT_PROFILE
1684 profile_dump();
1685#endif
1686
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001687 if (did_emsg
1688#ifdef FEAT_GUI
1689 || (gui.in_use && msg_didany && p_verbose > 0)
1690#endif
1691 )
1692 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001693 // give the user a chance to read the (error) message
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001694 no_wait_return = FALSE;
Volodymyr Kot7f0c4b42021-11-21 12:27:13 +00001695 wait_return(FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001696 }
1697
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001698 // Position the cursor again, the autocommands may have moved it
Bram Moolenaar7007e312021-03-27 12:11:33 +01001699 if (!is_not_a_term_or_gui())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001700 windgoto((int)Rows - 1, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001701
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001702#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001703 job_stop_on_exit();
1704#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001705#ifdef FEAT_LUA
1706 lua_end();
1707#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001708#ifdef FEAT_MZSCHEME
1709 mzscheme_end();
1710#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001711#ifdef FEAT_TCL
1712 tcl_end();
1713#endif
1714#ifdef FEAT_RUBY
1715 ruby_end();
1716#endif
1717#ifdef FEAT_PYTHON
1718 python_end();
1719#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001720#ifdef FEAT_PYTHON3
1721 python3_end();
1722#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001723#ifdef FEAT_PERL
1724 perl_end();
1725#endif
1726#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1727 iconv_end();
1728#endif
1729#ifdef FEAT_NETBEANS_INTG
1730 netbeans_end();
1731#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001732#ifdef FEAT_CSCOPE
1733 cs_end();
1734#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001735#ifdef FEAT_EVAL
1736 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001737 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001738#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001739#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +02001740 free_cmd_argsW();
1741#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001742
1743 mch_exit(exitval);
1744}
1745
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001746/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001747 * Get the name of the display, before gui_prepare() removes it from
1748 * argv[]. Used for the xterm-clipboard display.
1749 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001750 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001751 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001752 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001753early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001754{
Bram Moolenaar03005972008-11-20 13:12:36 +00001755#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1756 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001757 int argc = parmp->argc;
1758 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001759 int i;
1760
1761 for (i = 1; i < argc; i++)
1762 {
1763 if (STRCMP(argv[i], "--") == 0)
1764 break;
1765# ifdef FEAT_XCLIPBOARD
1766 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001767# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001768 || STRICMP(argv[i], "--display") == 0
1769# endif
1770 )
1771 {
1772 if (i == argc - 1)
1773 mainerr_arg_missing((char_u *)argv[i]);
1774 xterm_display = argv[++i];
1775 }
1776# endif
1777# ifdef FEAT_CLIENTSERVER
1778 else if (STRICMP(argv[i], "--servername") == 0)
1779 {
1780 if (i == argc - 1)
1781 mainerr_arg_missing((char_u *)argv[i]);
1782 parmp->serverName_arg = (char_u *)argv[++i];
1783 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001784 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001785 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001786 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001787 {
1788 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001789# ifdef FEAT_GUI
1790 if (strstr(argv[i], "-wait") != 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001791 // don't fork() when starting the GUI to edit files ourself
Bram Moolenaareb94e552006-03-11 21:35:11 +00001792 gui.dofork = FALSE;
1793# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001794 }
1795# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001796
Bram Moolenaar4f974752019-02-17 17:44:42 +01001797# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
1798# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001799 else if (STRICMP(argv[i], "--windowid") == 0)
1800# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001801 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001802# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001803 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001804 long_u id;
1805 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001806
1807 if (i == argc - 1)
1808 mainerr_arg_missing((char_u *)argv[i]);
1809 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001810 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001811 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001812 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001813 if (count != 1)
1814 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1815 else
Bram Moolenaar4f974752019-02-17 17:44:42 +01001816# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001817 win_socket_id = id;
1818# else
1819 gtk_socket_id = id;
1820# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001821 i++;
1822 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001823# endif
1824# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001825 else if (STRICMP(argv[i], "--echo-wid") == 0)
1826 echo_wid_arg = TRUE;
1827# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001828# ifndef FEAT_NETBEANS_INTG
1829 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001830 {
1831 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1832 mch_exit(2);
1833 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001834# endif
1835
Bram Moolenaar58d98232005-07-23 22:25:46 +00001836 }
1837#endif
1838}
1839
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001840#ifndef NO_VIM_MAIN
1841/*
1842 * Get a (optional) count for a Vim argument.
1843 */
1844 static int
1845get_number_arg(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001846 char_u *p, // pointer to argument
1847 int *idx, // index in argument, is incremented
1848 int def) // default value
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001849{
1850 if (vim_isdigit(p[*idx]))
1851 {
1852 def = atoi((char *)&(p[*idx]));
1853 while (vim_isdigit(p[*idx]))
1854 *idx = *idx + 1;
1855 }
1856 return def;
1857}
1858
1859/*
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001860 * Check for: [r][e][g][vi|vim|view][diff][ex[im]] (sort of)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001861 * If the executable name starts with "r" we disable shell commands.
1862 * If the next character is "e" we run in Easy mode.
1863 * If the next character is "g" we run the GUI version.
1864 * If the next characters are "view" we start in readonly mode.
1865 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1866 * If the next characters are "ex" we start in Ex mode. If it's followed
1867 * by "im" use improved Ex mode.
1868 */
1869 static void
1870parse_command_name(mparm_T *parmp)
1871{
1872 char_u *initstr;
1873
1874 initstr = gettail((char_u *)parmp->argv[0]);
1875
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001876#ifdef FEAT_EVAL
1877 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001878 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001879#endif
1880
1881 if (TOLOWER_ASC(initstr[0]) == 'r')
1882 {
1883 restricted = TRUE;
1884 ++initstr;
1885 }
1886
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001887 // Use evim mode for "evim" and "egvim", not for "editor".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001888 if (TOLOWER_ASC(initstr[0]) == 'e'
1889 && (TOLOWER_ASC(initstr[1]) == 'v'
1890 || TOLOWER_ASC(initstr[1]) == 'g'))
1891 {
1892#ifdef FEAT_GUI
1893 gui.starting = TRUE;
1894#endif
1895 parmp->evim_mode = TRUE;
1896 ++initstr;
1897 }
1898
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001899 // "gvim" starts the GUI. Also accept "Gvim" for MS-Windows.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001900 if (TOLOWER_ASC(initstr[0]) == 'g')
1901 {
1902 main_start_gui();
1903#ifdef FEAT_GUI
1904 ++initstr;
1905#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001906#ifdef GUI_MAY_SPAWN
1907 gui.dospawn = FALSE; // No need to spawn a new process.
1908#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001909 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001910#ifdef GUI_MAY_SPAWN
1911 else
1912 gui.dospawn = TRUE; // Not "gvim". Need to spawn gvim.exe.
1913#endif
1914
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001915
1916 if (STRNICMP(initstr, "view", 4) == 0)
1917 {
1918 readonlymode = TRUE;
1919 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001920 p_uc = 10000; // don't update very often
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001921 initstr += 4;
1922 }
1923 else if (STRNICMP(initstr, "vim", 3) == 0)
1924 initstr += 3;
1925
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001926 // Catch "[r][g]vimdiff" and "[r][g]viewdiff".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001927 if (STRICMP(initstr, "diff") == 0)
1928 {
1929#ifdef FEAT_DIFF
1930 parmp->diff_mode = TRUE;
1931#else
1932 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1933 mch_errmsg("\n");
1934 mch_exit(2);
1935#endif
1936 }
1937
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001938 // Checking for "ex" here may catch some weird names, such as "vimex" or
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001939 // "viewex", we assume the user knows that.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001940 if (STRNICMP(initstr, "ex", 2) == 0)
1941 {
1942 if (STRNICMP(initstr + 2, "im", 2) == 0)
1943 exmode_active = EXMODE_VIM;
1944 else
1945 exmode_active = EXMODE_NORMAL;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001946 change_compatible(TRUE); // set 'compatible'
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001947 }
1948}
1949
Bram Moolenaar58d98232005-07-23 22:25:46 +00001950/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001951 * Scan the command line arguments.
1952 */
1953 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001954command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001955{
1956 int argc = parmp->argc;
1957 char **argv = parmp->argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001958 int argv_idx; // index in argv[n][]
1959 int had_minmin = FALSE; // found "--" argument
1960 int want_argument; // option argument with argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001961 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001962 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001963 long n;
1964
1965 --argc;
1966 ++argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001967 argv_idx = 1; // active option letter is argv[0][argv_idx]
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001968 while (argc > 0)
1969 {
1970 /*
1971 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1972 */
1973 if (argv[0][0] == '+' && !had_minmin)
1974 {
1975 if (parmp->n_commands >= MAX_ARG_CMDS)
1976 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001977 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001978 if (argv[0][1] == NUL)
1979 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1980 else
1981 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1982 }
1983
1984 /*
1985 * Optional argument.
1986 */
1987 else if (argv[0][0] == '-' && !had_minmin)
1988 {
1989 want_argument = FALSE;
1990 c = argv[0][argv_idx++];
1991#ifdef VMS
1992 /*
1993 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1994 * and "-/X" as "-X".
1995 */
1996 if (c == '/')
1997 {
1998 c = argv[0][argv_idx++];
1999 c = TOUPPER_ASC(c);
2000 }
2001 else
2002 c = TOLOWER_ASC(c);
2003#endif
2004 switch (c)
2005 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002006 case NUL: // "vim -" read from stdin
2007 // "ex -" silent mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002008 if (exmode_active)
2009 silent_mode = TRUE;
2010 else
2011 {
2012 if (parmp->edit_type != EDIT_NONE)
2013 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2014 parmp->edit_type = EDIT_STDIN;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002015 read_cmd_fd = 2; // read from stderr instead of stdin
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002016 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002017 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002018 break;
2019
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002020 case '-': // "--" don't take any more option arguments
2021 // "--help" give help message
2022 // "--version" give version message
2023 // "--clean" clean context
2024 // "--literal" take files literally
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002025 // "--startuptime fname" write timing info
2026 // "--log fname" start logging early
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002027 // "--nofork" don't fork
2028 // "--not-a-term" don't warn for not a term
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002029 // "--gui-dialog-file fname" write dialog text
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002030 // "--ttyfail" exit if not a term
2031 // "--noplugin[s]" skip plugins
2032 // "--cmd <cmd>" execute cmd before vimrc
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002033 if (STRICMP(argv[0] + argv_idx, "help") == 0)
2034 usage();
2035 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
2036 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002037 Columns = 80; // need to init Columns
2038 info_message = TRUE; // use mch_msg(), not mch_errmsg()
K.Takata33b25d12022-01-13 16:06:45 +00002039#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar3b678042021-02-11 19:08:05 +01002040 gui.starting = FALSE; // not starting GUI, will exit
Bram Moolenaar0bcadf12021-02-11 19:18:58 +01002041#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002042 list_version();
2043 msg_putchar('\n');
2044 msg_didout = FALSE;
2045 mch_exit(0);
2046 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002047 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
2048 {
2049 parmp->use_vimrc = (char_u *)"DEFAULTS";
Bram Moolenaar62dd4522018-03-14 21:20:02 +01002050#ifdef FEAT_GUI
2051 use_gvimrc = (char_u *)"NONE";
2052#endif
Bram Moolenaar07268702018-03-01 21:57:32 +01002053 parmp->clean = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002054 set_option_value_give_err((char_u *)"vif",
2055 0L, (char_u *)"NONE", 0);
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002056 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002057 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
2058 {
Bram Moolenaar53076832015-12-31 19:53:21 +01002059#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002060 parmp->literal = TRUE;
2061#endif
2062 }
2063 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
2064 {
2065#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002066 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002067#endif
2068 }
2069 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
2070 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002071 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
2072 parmp->not_a_term = TRUE;
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002073 else if (STRNICMP(argv[0] + argv_idx, "gui-dialog-file", 15)
2074 == 0)
2075 {
2076 want_argument = TRUE;
2077 argv_idx += 15;
2078 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002079 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
2080 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002081 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
2082 {
2083 want_argument = TRUE;
2084 argv_idx += 3;
2085 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002086 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
2087 {
2088 want_argument = TRUE;
2089 argv_idx += 11;
2090 }
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002091 else if (STRNICMP(argv[0] + argv_idx, "log", 3) == 0)
2092 {
2093 want_argument = TRUE;
2094 argv_idx += 3;
2095 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002096#ifdef FEAT_CLIENTSERVER
2097 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002098 ; // already processed -- no arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002099 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
2100 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
2101 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002102 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002103 if (argc > 1)
2104 {
2105 --argc;
2106 ++argv;
2107 }
2108 }
2109#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002110#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002111# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002112 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002113# else
2114 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
2115# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002116 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002117 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002118 if (argc > 1)
2119 {
2120 --argc;
2121 ++argv;
2122 }
2123 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00002124#endif
2125#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002126 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
2127 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002128 // already processed, skip
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002129 }
2130#endif
2131 else
2132 {
2133 if (argv[0][argv_idx])
2134 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2135 had_minmin = TRUE;
2136 }
2137 if (!want_argument)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002138 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002139 break;
2140
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002141 case 'A': // "-A" start in Arabic mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002142#ifdef FEAT_ARABIC
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002143 set_option_value_give_err((char_u *)"arabic", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002144#else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002145 mch_errmsg(_(e_arabic_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002146 mch_exit(2);
2147#endif
2148 break;
2149
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002150 case 'b': // "-b" binary mode
2151 // Needs to be effective before expanding file names, because
2152 // for Win32 this makes us edit a shortcut file itself,
2153 // instead of the file it links to.
Bram Moolenaar231334e2005-07-25 20:46:57 +00002154 set_options_bin(curbuf->b_p_bin, 1, 0);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002155 curbuf->b_p_bin = 1; // binary file I/O
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002156 break;
2157
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002158 case 'C': // "-C" Compatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002159 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02002160 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002161 break;
2162
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002163 case 'e': // "-e" Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002164 exmode_active = EXMODE_NORMAL;
2165 break;
2166
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002167 case 'E': // "-E" Improved Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002168 exmode_active = EXMODE_VIM;
2169 break;
2170
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002171 case 'f': // "-f" GUI: run in foreground. Amiga: open
2172 // window directly, not with newcli
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002173#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002174 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002175#endif
2176 break;
2177
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002178 case 'g': // "-g" start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002179 main_start_gui();
2180 break;
2181
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002182 case 'F': // "-F" was for Farsi mode
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002183 mch_errmsg(_(e_farsi_support_has_been_removed));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002184 mch_exit(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002185 break;
2186
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002187 case '?': // "-?" give help message (for MS-Windows)
2188 case 'h': // "-h" give help message
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002189#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002190 // Tell usage() to exit for "gvim".
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002191 gui.starting = FALSE;
2192#endif
2193 usage();
2194 break;
2195
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002196 case 'H': // "-H" start in Hebrew mode: rl + hkmap set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002197#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002198 p_hkmap = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002199 set_option_value_give_err((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002200#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002201 mch_errmsg(_(e_hebrew_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002202 mch_exit(2);
2203#endif
2204 break;
2205
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002206 case 'l': // "-l" lisp mode, 'lisp' and 'showmatch' on
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002207 set_option_value_give_err((char_u *)"lisp", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002208 p_sm = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002209 break;
2210
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002211 case 'M': // "-M" no changes or writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002212 reset_modifiable();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002213 // FALLTHROUGH
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002214
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002215 case 'm': // "-m" no writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002216 p_write = FALSE;
2217 break;
2218
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002219 case 'y': // "-y" easy mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002220#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002221 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002222#endif
2223 parmp->evim_mode = TRUE;
2224 break;
2225
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002226 case 'N': // "-N" Nocompatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002227 change_compatible(FALSE);
2228 break;
2229
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002230 case 'n': // "-n" no swap file
Bram Moolenaar67c53842010-05-22 18:28:27 +02002231#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002232 // checking for "-nb", netbeans parameters
Bram Moolenaar67c53842010-05-22 18:28:27 +02002233 if (argv[0][argv_idx] == 'b')
2234 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002235 netbeansArg = argv[0];
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002236 argv_idx = -1; // skip to next argument
Bram Moolenaar67c53842010-05-22 18:28:27 +02002237 }
2238 else
2239#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002240 parmp->no_swap_file = TRUE;
2241 break;
2242
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002243 case 'p': // "-p[N]" open N tab pages
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002244#ifdef TARGET_API_MAC_OSX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002245 // For some reason on MacOS X, an argument like:
2246 // -psn_0_10223617 is passed in when invoke from Finder
2247 // or with the 'open' command
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002248 if (argv[0][argv_idx] == 's')
2249 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002250 argv_idx = -1; // bypass full -psn
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002251 main_start_gui();
2252 break;
2253 }
2254#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002255 // default is 0: open window for each file
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002256 parmp->window_count = get_number_arg((char_u *)argv[0],
2257 &argv_idx, 0);
2258 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002259 break;
2260
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002261 case 'o': // "-o[N]" open N horizontal split windows
2262 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002263 parmp->window_count = get_number_arg((char_u *)argv[0],
2264 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002265 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002266 break;
2267
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002268 case 'O': // "-O[N]" open N vertical split windows
2269 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002270 parmp->window_count = get_number_arg((char_u *)argv[0],
2271 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002272 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002273 break;
2274
2275#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002276 case 'q': // "-q" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002277 if (parmp->edit_type != EDIT_NONE)
2278 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2279 parmp->edit_type = EDIT_QF;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002280 if (argv[0][argv_idx]) // "-q{errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002281 {
2282 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2283 argv_idx = -1;
2284 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002285 else if (argc > 1) // "-q {errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002286 want_argument = TRUE;
2287 break;
2288#endif
2289
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002290 case 'R': // "-R" readonly mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002291 readonlymode = TRUE;
2292 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002293 p_uc = 10000; // don't update very often
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002294 break;
2295
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002296 case 'r': // "-r" recovery mode
2297 case 'L': // "-L" recovery mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002298 recoverymode = 1;
2299 break;
2300
2301 case 's':
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002302 if (exmode_active) // "-s" silent (batch) mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002303 silent_mode = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002304 else // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002305 want_argument = TRUE;
2306 break;
2307
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002308 case 't': // "-t {tag}" or "-t{tag}" jump to tag
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002309 if (parmp->edit_type != EDIT_NONE)
2310 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2311 parmp->edit_type = EDIT_TAG;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002312 if (argv[0][argv_idx]) // "-t{tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002313 {
2314 parmp->tagname = (char_u *)argv[0] + argv_idx;
2315 argv_idx = -1;
2316 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002317 else // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002318 want_argument = TRUE;
2319 break;
2320
2321#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002322 case 'D': // "-D" Debugging
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002323 parmp->use_debug_break_level = 9999;
2324 break;
2325#endif
2326#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002327 case 'd': // "-d" 'diff'
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002328# ifdef AMIGA
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002329 // check for "-dev {device}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002330 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2331 want_argument = TRUE;
2332 else
2333# endif
2334 parmp->diff_mode = TRUE;
2335 break;
2336#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002337 case 'V': // "-V{N}" Verbose level
2338 // default is 10: a little bit verbose
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002339 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2340 if (argv[0][argv_idx] != NUL)
2341 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002342 set_option_value_give_err((char_u *)"verbosefile",
2343 0L, (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002344 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002345 }
2346 break;
2347
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002348 case 'v': // "-v" Vi-mode (as if called "vi")
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002349 exmode_active = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002350#if defined(FEAT_GUI) && !defined(VIMDLL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002351 gui.starting = FALSE; // don't start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002352#endif
2353 break;
2354
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002355 case 'w': // "-w{number}" set window height
2356 // "-w {scriptout}" write to script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002357 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2358 {
2359 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002360 set_option_value_give_err((char_u *)"window", n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002361 break;
2362 }
2363 want_argument = TRUE;
2364 break;
2365
2366#ifdef FEAT_CRYPT
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002367 case 'x': // "-x" encrypted reading/writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002368 parmp->ask_for_key = TRUE;
2369 break;
2370#endif
2371
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002372 case 'X': // "-X" don't connect to X server
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002373#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2374 x_no_connect = TRUE;
2375#endif
2376 break;
2377
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002378 case 'Z': // "-Z" restricted mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002379 restricted = TRUE;
2380 break;
2381
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002382 case 'c': // "-c{command}" or "-c {command}" execute
2383 // command
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002384 if (argv[0][argv_idx] != NUL)
2385 {
2386 if (parmp->n_commands >= MAX_ARG_CMDS)
2387 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002388 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2389 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002390 argv_idx = -1;
2391 break;
2392 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002393 // FALLTHROUGH
2394 case 'S': // "-S {file}" execute Vim script
2395 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002396#ifndef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002397 case 'd': // "-d {device}" device (for Amiga)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002398#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002399 case 'T': // "-T {terminal}" terminal name
2400 case 'u': // "-u {vimrc}" vim inits file
2401 case 'U': // "-U {gvimrc}" gvim inits file
2402 case 'W': // "-W {scriptout}" overwrite
Bram Moolenaar4f974752019-02-17 17:44:42 +01002403#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002404 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002405#endif
2406 want_argument = TRUE;
2407 break;
2408
2409 default:
2410 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2411 }
2412
2413 /*
2414 * Handle option arguments with argument.
2415 */
2416 if (want_argument)
2417 {
2418 /*
2419 * Check for garbage immediately after the option letter.
2420 */
2421 if (argv[0][argv_idx] != NUL)
2422 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2423
2424 --argc;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002425 if (argc < 1 && c != 'S') // -S has an optional argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002426 mainerr_arg_missing((char_u *)argv[0]);
2427 ++argv;
2428 argv_idx = -1;
2429
2430 switch (c)
2431 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002432 case 'c': // "-c {command}" execute command
2433 case 'S': // "-S {file}" execute Vim script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002434 if (parmp->n_commands >= MAX_ARG_CMDS)
2435 mainerr(ME_EXTRA_CMD, NULL);
2436 if (c == 'S')
2437 {
2438 char *a;
2439
2440 if (argc < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002441 // "-S" without argument: use default session file
2442 // name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002443 a = SESSION_FILE;
2444 else if (argv[0][0] == '-')
2445 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002446 // "-S" followed by another option: use default
2447 // session file name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002448 a = SESSION_FILE;
2449 ++argc;
2450 --argv;
2451 }
2452 else
2453 a = argv[0];
Bram Moolenaar964b3742019-05-24 18:54:09 +02002454 p = alloc(STRLEN(a) + 4);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002455 if (p == NULL)
2456 mch_exit(2);
2457 sprintf((char *)p, "so %s", a);
2458 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2459 parmp->commands[parmp->n_commands++] = p;
2460 }
2461 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002462 parmp->commands[parmp->n_commands++] =
2463 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002464 break;
2465
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002466 case '-':
2467 if (argv[-1][2] == 'c')
2468 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002469 // "--cmd {command}" execute command
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002470 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2471 mainerr(ME_EXTRA_CMD, NULL);
2472 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002473 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002474 }
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002475 // --gui-dialog-file fname
2476 if (argv[-1][2] == 'g')
2477 {
2478 // without GUI ignore the argument
2479#ifdef FEAT_GUI
2480 parmp->gui_dialog_file = (char_u *)argv[0];
2481#endif
2482 }
2483
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002484 // "--startuptime <file>" already handled
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002485 // "--log <file>" already handled
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002486 break;
2487
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002488 // case 'd': -d {device} is handled in mch_check_win() for the
2489 // Amiga
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002490
2491#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002492 case 'q': // "-q {errorfile}" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002493 parmp->use_ef = (char_u *)argv[0];
2494 break;
2495#endif
2496
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002497 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002498 set_option_value_give_err((char_u *)"vif",
2499 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002500 break;
2501
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002502 case 's': // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002503 if (scriptin[0] != NULL)
2504 {
2505scripterror:
2506 mch_errmsg(_("Attempt to open script file again: \""));
2507 mch_errmsg(argv[-1]);
2508 mch_errmsg(" ");
2509 mch_errmsg(argv[0]);
2510 mch_errmsg("\"\n");
2511 mch_exit(2);
2512 }
2513 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2514 {
2515 mch_errmsg(_("Cannot open for reading: \""));
2516 mch_errmsg(argv[0]);
2517 mch_errmsg("\"\n");
2518 mch_exit(2);
2519 }
2520 if (save_typebuf() == FAIL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002521 mch_exit(2); // out of memory
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002522 break;
2523
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002524 case 't': // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002525 parmp->tagname = (char_u *)argv[0];
2526 break;
2527
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002528 case 'T': // "-T {terminal}" terminal name
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002529 /*
2530 * The -T term argument is always available and when
2531 * HAVE_TERMLIB is supported it overrides the environment
2532 * variable TERM.
2533 */
2534#ifdef FEAT_GUI
2535 if (term_is_gui((char_u *)argv[0]))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002536 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002537 else
2538#endif
2539 parmp->term = (char_u *)argv[0];
2540 break;
2541
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002542 case 'u': // "-u {vimrc}" vim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002543 parmp->use_vimrc = (char_u *)argv[0];
2544 break;
2545
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002546 case 'U': // "-U {gvimrc}" gvim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002547#ifdef FEAT_GUI
2548 use_gvimrc = (char_u *)argv[0];
2549#endif
2550 break;
2551
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002552 case 'w': // "-w {nr}" 'window' value
2553 // "-w {scriptout}" append to script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002554 if (vim_isdigit(*((char_u *)argv[0])))
2555 {
2556 argv_idx = 0;
2557 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002558 set_option_value_give_err((char_u *)"window",
2559 n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002560 argv_idx = -1;
2561 break;
2562 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002563 // FALLTHROUGH
2564 case 'W': // "-W {scriptout}" overwrite script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002565 if (scriptout != NULL)
2566 goto scripterror;
2567 if ((scriptout = mch_fopen(argv[0],
2568 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2569 {
2570 mch_errmsg(_("Cannot open for script output: \""));
2571 mch_errmsg(argv[0]);
2572 mch_errmsg("\"\n");
2573 mch_exit(2);
2574 }
2575 break;
2576
Bram Moolenaar4f974752019-02-17 17:44:42 +01002577#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002578 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002579 gui_mch_set_parent(argv[0]);
2580 break;
2581#endif
2582 }
2583 }
2584 }
2585
2586 /*
2587 * File name argument.
2588 */
2589 else
2590 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002591 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002592
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002593 // Check for only one type of editing.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002594 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2595 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2596 parmp->edit_type = EDIT_FILE;
2597
2598#ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002599 // Remember if the argument was a full path before changing
2600 // slashes to backslashes.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002601 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2602 parmp->full_path = TRUE;
2603#endif
2604
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002605 // Add the file to the global argument list.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002606 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2607 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2608 mch_exit(2);
2609#ifdef FEAT_DIFF
2610 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2611 && !mch_isdir(alist_name(&GARGLIST[0])))
2612 {
2613 char_u *r;
2614
2615 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2616 if (r != NULL)
2617 {
2618 vim_free(p);
2619 p = r;
2620 }
2621 }
2622#endif
K.Takatab247e062022-02-07 10:45:23 +00002623#ifdef __CYGWIN__
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002624 /*
2625 * If vim is invoked by non-Cygwin tools, convert away any
2626 * DOS paths, so things like .swp files are created correctly.
2627 * Look for evidence of non-Cygwin paths before we bother.
2628 * This is only for when using the Unix files.
2629 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002630 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002631 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002632 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002633
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002634# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002635 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002636# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002637 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002638# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002639 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002640 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002641 if (p == NULL)
2642 mch_exit(2);
2643 }
2644#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002645
2646#ifdef USE_FNAME_CASE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002647 // Make the case of the file name match the actual file.
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002648 fname_case(p, 0);
2649#endif
2650
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002651 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002652#ifdef EXPAND_FILENAMES
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002653 parmp->literal ? 2 : 0 // add buffer nr after exp.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002654#else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002655 2 // add buffer number now and use curbuf
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002656#endif
2657 );
2658
Bram Moolenaar4f974752019-02-17 17:44:42 +01002659#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002660 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002661 // Remember this argument has been added to the argument list.
2662 // Needed when 'encoding' is changed.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002663 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002664# ifdef FEAT_DIFF
2665 parmp->diff_mode
2666# else
2667 FALSE
2668# endif
2669 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002670 }
2671#endif
2672 }
2673
2674 /*
2675 * If there are no more letters after the current "-", go to next
2676 * argument. argv_idx is set to -1 when the current argument is to be
2677 * skipped.
2678 */
2679 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2680 {
2681 --argc;
2682 ++argv;
2683 argv_idx = 1;
2684 }
2685 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002686
2687#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002688 // If there is a "+123" or "-c" command, set v:swapcommand to the first
2689 // one.
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002690 if (parmp->n_commands > 0)
2691 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002692 p = alloc(STRLEN(parmp->commands[0]) + 3);
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002693 if (p != NULL)
2694 {
2695 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2696 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2697 vim_free(p);
2698 }
2699 }
2700#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002701}
2702
2703/*
2704 * Print a warning if stdout is not a terminal.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +01002705 * When starting in Ex mode and commands come from a file, set silent_mode.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002706 */
2707 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002708check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002709{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002710 int input_isatty; // is active input a terminal?
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002711
2712 input_isatty = mch_input_isatty();
2713 if (exmode_active)
2714 {
2715 if (!input_isatty)
2716 silent_mode = TRUE;
2717 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002718 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002719#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002720 // don't want the delay when started from the desktop
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002721 && !gui.starting
2722#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002723 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002724 {
2725#ifdef NBDEBUG
2726 /*
2727 * This shouldn't be necessary. But if I run netbeans with the log
2728 * output coming to the console and XOpenDisplay fails, I get vim
2729 * trying to start with input/output to my console tty. This fills my
2730 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002731 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002732 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002733 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002734 {
2735 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2736 exit(1);
2737 }
2738#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002739#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2740 if (
2741# ifdef VIMDLL
2742 !gui.starting &&
2743# endif
2744 is_cygpty_used())
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002745 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002746# if defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
Bram Moolenaar2a027452017-09-26 19:10:37 +02002747 && defined(FEAT_GETTEXT)
2748 char *s, *tofree = NULL;
2749
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002750 // Set the encoding of the error message based on $LC_ALL or
2751 // other environment variables instead of 'encoding'.
2752 // Note that the message is shown on a Cygwin terminal (e.g.
2753 // mintty) which encoding is based on $LC_ALL or etc., not the
2754 // current codepage used by normal Win32 console programs.
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002755 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002756 if (s == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002757 s = "utf-8"; // Use "utf-8" by default.
Bram Moolenaar2a027452017-09-26 19:10:37 +02002758 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2759 vim_free(tofree);
2760# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002761 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2762 exit(1);
2763 }
2764#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002765 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002766 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2767 if (!input_isatty)
2768 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2769 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002770 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2771 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002772 if (scriptin[0] == NULL)
Bram Moolenaareda1da02019-11-17 17:06:33 +01002773 ui_delay(2005L, TRUE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002774 TIME_MSG("Warning delay");
2775 }
2776}
2777
2778/*
2779 * Read text from stdin.
2780 */
2781 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002782read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002783{
2784 int i;
2785
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002786 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002787 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002788
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002789 no_wait_return = TRUE;
2790 i = msg_didany;
2791 set_buflisted(TRUE);
Bram Moolenaar204ade62020-10-11 14:58:46 +02002792
2793 // Create memfile and read from stdin.
Bram Moolenaar204ade62020-10-11 14:58:46 +02002794 (void)open_buffer(TRUE, NULL, 0);
2795
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002796 no_wait_return = FALSE;
2797 msg_didany = i;
2798 TIME_MSG("reading stdin");
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002799
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002800 check_swap_exists_action();
Bram Moolenaar8e1cbb52020-12-08 19:36:21 +01002801
2802#if !(defined(AMIGA) || defined(MACOS_X))
2803 // Dup stdin from stderr to read commands from, so that shell commands
2804 // work.
2805 // TODO: why is this needed, even though readfile() has done this?
2806 close(0);
2807 vim_ignored = dup(2);
2808#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002809}
2810
2811/*
2812 * Create the requested number of windows and edit buffers in them.
2813 * Also does recovery if "recoverymode" set.
2814 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002815 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002816create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002817{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002818 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002819 int done = 0;
2820
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002821 /*
2822 * Create the number of windows that was requested.
2823 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002824 if (parmp->window_count == -1) // was not set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002825 parmp->window_count = 1;
2826 if (parmp->window_count == 0)
2827 parmp->window_count = GARGCOUNT;
2828 if (parmp->window_count > 1)
2829 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002830 // Don't change the windows if there was a command in .vimrc that
2831 // already split some windows
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002832 if (parmp->window_layout == 0)
2833 parmp->window_layout = WIN_HOR;
2834 if (parmp->window_layout == WIN_TABS)
2835 {
2836 parmp->window_count = make_tabpages(parmp->window_count);
2837 TIME_MSG("making tab pages");
2838 }
2839 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002840 {
2841 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002842 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002843 TIME_MSG("making windows");
2844 }
2845 else
2846 parmp->window_count = win_count();
2847 }
2848 else
2849 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002850
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002851 if (recoverymode) // do recover
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002852 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002853 msg_scroll = TRUE; // scroll message up
Bram Moolenaar99499b12019-05-23 21:35:48 +02002854 ml_recover(TRUE);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002855 if (curbuf->b_ml.ml_mfp == NULL) // failed
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002856 getout(1);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002857 do_modelines(0); // do modelines
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002858 }
2859 else
2860 {
2861 /*
2862 * Open a buffer for windows that don't have one yet.
2863 * Commands in the .vimrc might have loaded a file or split the window.
2864 * Watch out for autocommands that delete a window.
2865 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002866 /*
2867 * Don't execute Win/Buf Enter/Leave autocommands here
2868 */
2869 ++autocmd_no_enter;
2870 ++autocmd_no_leave;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002871 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002872 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002873 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002874 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002875 {
2876 if (parmp->window_layout == WIN_TABS)
2877 goto_tabpage(1);
2878 else
2879 curwin = firstwin;
2880 }
2881 else if (parmp->window_layout == WIN_TABS)
2882 {
2883 if (curtab->tp_next == NULL)
2884 break;
2885 goto_tabpage(0);
2886 }
2887 else
2888 {
2889 if (curwin->w_next == NULL)
2890 break;
2891 curwin = curwin->w_next;
2892 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002893 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002894 curbuf = curwin->w_buffer;
2895 if (curbuf->b_ml.ml_mfp == NULL)
2896 {
2897#ifdef FEAT_FOLDING
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002898 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002899 if (p_fdls >= 0)
2900 curwin->w_p_fdl = p_fdls;
2901#endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002902 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002903 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002904
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002905 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002906
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002907 // create memfile, read file
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002908 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002909
Bram Moolenaar84212822006-11-07 21:59:47 +00002910 if (swap_exists_action == SEA_QUIT)
2911 {
2912 if (got_int || only_one_window())
2913 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002914 // abort selected or quit and only one window
2915 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00002916 getout(1);
2917 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002918 // We can't close the window, it would disturb what
2919 // happens next. Clear the file name and set the arg
2920 // index to -1 to delete it later.
Bram Moolenaar84212822006-11-07 21:59:47 +00002921 setfname(curbuf, NULL, NULL, FALSE);
2922 curwin->w_arg_idx = -1;
2923 swap_exists_action = SEA_NONE;
2924 }
2925 else
2926 handle_swap_exists(NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002927 dorewind = TRUE; // start again
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002928 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002929 ui_breakcheck();
2930 if (got_int)
2931 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002932 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002933 break;
2934 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002935 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002936 if (parmp->window_layout == WIN_TABS)
2937 goto_tabpage(1);
2938 else
2939 curwin = firstwin;
2940 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002941 --autocmd_no_enter;
2942 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002943 }
2944}
2945
Bram Moolenaar9810cfb2019-12-11 21:23:00 +01002946/*
2947 * If opened more than one window, start editing files in the other
2948 * windows. make_windows() has already opened the windows.
2949 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002950 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002951edit_buffers(
2952 mparm_T *parmp,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002953 char_u *cwd) // current working dir
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002954{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002955 int arg_idx; // index in argument list
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002956 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002957 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002958 win_T *win;
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002959 char_u *p_shm_save = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002960
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002961 /*
2962 * Don't execute Win/Buf Enter/Leave autocommands here
2963 */
2964 ++autocmd_no_enter;
2965 ++autocmd_no_leave;
Bram Moolenaar84212822006-11-07 21:59:47 +00002966
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002967 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002968 if (curwin->w_arg_idx == -1)
2969 {
2970 win_close(curwin, TRUE);
2971 advance = FALSE;
2972 }
2973
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002974 arg_idx = 1;
2975 for (i = 1; i < parmp->window_count; ++i)
2976 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002977 if (cwd != NULL)
2978 mch_chdir((char *)cwd);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002979 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002980 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002981 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002982 ++arg_idx;
2983 win_close(curwin, TRUE);
2984 advance = FALSE;
2985 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002986 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002987
Bram Moolenaar84212822006-11-07 21:59:47 +00002988 if (advance)
2989 {
2990 if (parmp->window_layout == WIN_TABS)
2991 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002992 if (curtab->tp_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00002993 break;
2994 goto_tabpage(0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002995 // Temporarily reset 'shm' option to not print fileinfo when
2996 // loading the other buffers. This would overwrite the already
2997 // existing fileinfo for the first tab.
2998 if (i == 1)
2999 {
3000 char buf[100];
3001
3002 p_shm_save = vim_strsave(p_shm);
3003 vim_snprintf(buf, 100, "F%s", p_shm);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003004 set_option_value_give_err((char_u *)"shm",
3005 0L, (char_u *)buf, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003006 }
Bram Moolenaar84212822006-11-07 21:59:47 +00003007 }
3008 else
3009 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003010 if (curwin->w_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00003011 break;
3012 win_enter(curwin->w_next, FALSE);
3013 }
3014 }
3015 advance = TRUE;
3016
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003017 // Only open the file if there is no file in this window yet (that can
3018 // happen when .vimrc contains ":sall").
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003019 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
3020 {
3021 curwin->w_arg_idx = arg_idx;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003022 // Edit file from arg list, if there is one. When "Quit" selected
3023 // at the ATTENTION prompt close the window.
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003024 swap_exists_did_quit = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003025 (void)do_ecmd(0, arg_idx < GARGCOUNT
3026 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003027 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003028 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00003029 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003030 // abort or quit selected
Bram Moolenaar84212822006-11-07 21:59:47 +00003031 if (got_int || only_one_window())
3032 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003033 // abort selected and only one window
3034 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00003035 getout(1);
3036 }
3037 win_close(curwin, TRUE);
3038 advance = FALSE;
3039 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003040 if (arg_idx == GARGCOUNT - 1)
3041 arg_had_last = TRUE;
3042 ++arg_idx;
3043 }
3044 ui_breakcheck();
3045 if (got_int)
3046 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003047 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003048 break;
3049 }
3050 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003051
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003052 if (p_shm_save != NULL)
3053 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003054 set_option_value_give_err((char_u *)"shm", 0L, p_shm_save, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003055 vim_free(p_shm_save);
3056 }
3057
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003058 if (parmp->window_layout == WIN_TABS)
3059 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003060 --autocmd_no_enter;
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003061
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003062 // make the first window the current window
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003063 win = firstwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003064#if defined(FEAT_QUICKFIX)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003065 // Avoid making a preview window the current window.
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003066 while (win->w_p_pvw)
3067 {
3068 win = win->w_next;
3069 if (win == NULL)
3070 {
3071 win = firstwin;
3072 break;
3073 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003074 }
3075#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003076 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003077
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003078 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003079 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003080 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003081 win_equal(curwin, FALSE, 'b'); // adjust heights
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003082}
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003083
3084/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003085 * Execute the commands from --cmd arguments "cmds[cnt]".
3086 */
3087 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003088exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003089{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003090 char_u **cmds = parmp->pre_commands;
3091 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003092 int i;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003093 ESTACK_CHECK_DECLARATION
Bram Moolenaar58d98232005-07-23 22:25:46 +00003094
3095 if (cnt > 0)
3096 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003097 curwin->w_cursor.lnum = 0; // just in case..
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003098 estack_push(ETYPE_ARGS, (char_u *)_("pre-vimrc command line"), 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003099 ESTACK_CHECK_SETUP
Bram Moolenaar58d98232005-07-23 22:25:46 +00003100# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003101 current_sctx.sc_sid = SID_CMDARG;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003102# endif
3103 for (i = 0; i < cnt; ++i)
3104 do_cmdline_cmd(cmds[i]);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003105 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003106 estack_pop();
Bram Moolenaar58d98232005-07-23 22:25:46 +00003107# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003108 current_sctx.sc_sid = 0;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003109# endif
3110 TIME_MSG("--cmd commands");
3111 }
3112}
3113
3114/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003115 * Execute "+", "-c" and "-S" arguments.
3116 */
3117 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003118exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003119{
3120 int i;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003121 ESTACK_CHECK_DECLARATION
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003122
3123 /*
3124 * We start commands on line 0, make "vim +/pat file" match a
3125 * pattern on line 1. But don't move the cursor when an autocommand
3126 * with g`" was used.
3127 */
3128 msg_scroll = TRUE;
3129 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
3130 curwin->w_cursor.lnum = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003131 estack_push(ETYPE_ARGS, (char_u *)"command line", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003132 ESTACK_CHECK_SETUP
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003133#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003134 current_sctx.sc_sid = SID_CARG;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003135 current_sctx.sc_seq = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003136#endif
3137 for (i = 0; i < parmp->n_commands; ++i)
3138 {
3139 do_cmdline_cmd(parmp->commands[i]);
3140 if (parmp->cmds_tofree[i])
3141 vim_free(parmp->commands[i]);
3142 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01003143 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003144 estack_pop();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003145#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003146 current_sctx.sc_sid = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003147#endif
3148 if (curwin->w_cursor.lnum == 0)
3149 curwin->w_cursor.lnum = 1;
3150
3151 if (!exmode_active)
3152 msg_scroll = FALSE;
3153
3154#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003155 // When started with "-q errorfile" jump to first error again.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003156 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003157 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003158#endif
3159 TIME_MSG("executing command arguments");
3160}
3161
3162/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003163 * Source startup scripts.
3164 */
3165 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003166source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003167{
3168 int i;
3169
3170 /*
3171 * For "evim" source evim.vim first of all, so that the user can overrule
3172 * any things he doesn't like.
3173 */
3174 if (parmp->evim_mode)
3175 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003177 TIME_MSG("source evim file");
3178 }
3179
3180 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003181 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00003182 * nothing else.
3183 */
3184 if (parmp->use_vimrc != NULL)
3185 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003186 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003187 {
3188 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE, NULL)
3189 != OK)
3190 emsg(e_failed_to_source_defaults);
3191 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003192 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003193 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003194 {
3195#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003196 if (use_gvimrc == NULL) // don't load gvimrc either
Bram Moolenaar58d98232005-07-23 22:25:46 +00003197 use_gvimrc = parmp->use_vimrc;
3198#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003199 }
3200 else
3201 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00003203 semsg(_(e_cannot_read_from_str_2), parmp->use_vimrc);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003204 }
3205 }
3206 else if (!silent_mode)
3207 {
3208#ifdef AMIGA
3209 struct Process *proc = (struct Process *)FindTask(0L);
3210 APTR save_winptr = proc->pr_WindowPtr;
3211
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003212 // Avoid a requester here for a volume that doesn't exist.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003213 proc->pr_WindowPtr = (APTR)-1L;
3214#endif
3215
3216 /*
3217 * Get system wide defaults, if the file name is defined.
3218 */
3219#ifdef SYS_VIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003221#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003222#ifdef MACOS_X
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE,
3224 DOSO_NONE, NULL);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003225#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003226
3227 /*
3228 * Try to read initialization commands from the following places:
3229 * - environment variable VIMINIT
3230 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3231 * - second user vimrc file ($VIM/.vimrc for Dos)
3232 * - environment variable EXINIT
3233 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3234 * - second user exrc file ($VIM/.exrc for Dos)
3235 * The first that exists is used, the rest is ignored.
3236 */
3237 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3238 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 if (do_source((char_u *)USR_VIMRC_FILE, TRUE,
3240 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003241#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003242 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003244#endif
3245#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003246 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003248#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003249#ifdef USR_VIMRC_FILE4
3250 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003252#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003253 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 && do_source((char_u *)USR_EXRC_FILE, FALSE,
3255 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003256#ifdef USR_EXRC_FILE2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 && do_source((char_u *)USR_EXRC_FILE2, FALSE,
3258 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003259#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003260 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003261 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003262 // When no .vimrc file was found: source defaults.vim.
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003263 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE,
3264 NULL) == FAIL)
3265 emsg(e_failed_to_source_defaults);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003266 }
3267 }
3268
3269 /*
3270 * Read initialization commands from ".vimrc" or ".exrc" in current
3271 * directory. This is only done if the 'exrc' option is set.
3272 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003273 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003274 * option has been reset in environment of global ".exrc" or ".vimrc".
3275 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3276 * SYS_VIMRC_FILE.
3277 */
3278 if (p_exrc)
3279 {
3280#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003281 // If ".vimrc" file is not owned by user, set 'secure' mode.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003282 if (!file_owned(VIMRC_FILE))
3283#endif
3284 secure = p_secure;
3285
3286 i = FAIL;
3287 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003288 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003289#ifdef USR_VIMRC_FILE2
3290 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003291 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003292#endif
3293#ifdef USR_VIMRC_FILE3
3294 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003295 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003296#endif
3297#ifdef SYS_VIMRC_FILE
3298 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003299 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003300#endif
3301 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003303
3304 if (i == FAIL)
3305 {
3306#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003307 // if ".exrc" is not owned by user set 'secure' mode
Bram Moolenaar58d98232005-07-23 22:25:46 +00003308 if (!file_owned(EXRC_FILE))
3309 secure = p_secure;
3310 else
3311 secure = 0;
3312#endif
3313 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003314 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003315#ifdef USR_EXRC_FILE2
3316 && fullpathcmp((char_u *)USR_EXRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003317 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003318#endif
3319 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 (void)do_source((char_u *)EXRC_FILE, FALSE,
3321 DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003322 }
3323 }
3324 if (secure == 2)
3325 need_wait_return = TRUE;
3326 secure = 0;
3327#ifdef AMIGA
3328 proc->pr_WindowPtr = save_winptr;
3329#endif
3330 }
3331 TIME_MSG("sourcing vimrc file(s)");
3332}
3333
3334/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003335 * Setup to start using the GUI. Exit with an error when not available.
3336 */
3337 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003338main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003339{
3340#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003341 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003342#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003343 mch_errmsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003344 mch_errmsg("\n");
3345 mch_exit(2);
3346#endif
3347}
3348
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003349#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003350
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003351/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 * Get an environment variable and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003353 * Returns FAIL if the environment variable was not executed, OK otherwise.
3354 */
3355 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003356process_env(
3357 char_u *env,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003358 int is_viminit) // when TRUE, called for VIMINIT
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003359{
3360 char_u *initstr;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003361 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003362
Bram Moolenaare31ee862020-01-07 20:59:34 +01003363 ESTACK_CHECK_DECLARATION
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003364
3365 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3366 {
3367 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003368 vimrc_found(NULL, NULL);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003369 estack_push(ETYPE_ENV, env, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003370 ESTACK_CHECK_SETUP
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003371 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003372 current_sctx.sc_version = 1;
3373#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003374 current_sctx.sc_sid = SID_ENV;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003375 current_sctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003376 current_sctx.sc_lnum = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003377#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003378
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003379 do_cmdline_cmd(initstr);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003380
Bram Moolenaare31ee862020-01-07 20:59:34 +01003381 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003382 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003383 current_sctx = save_current_sctx;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003384 return OK;
3385 }
3386 return FAIL;
3387}
3388
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003389#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003390/*
3391 * Return TRUE if we are certain the user owns the file "fname".
3392 * Used for ".vimrc" and ".exrc".
3393 * Use both stat() and lstat() for extra security.
3394 */
3395 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003396file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003397{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003398 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003399# ifdef UNIX
3400 uid_t uid = getuid();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003401# else // VMS
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003402 uid_t uid = ((getgid() << 16) | getuid());
3403# endif
3404
3405 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3406# ifdef HAVE_LSTAT
3407 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3408# endif
3409 );
3410}
3411#endif
3412
3413/*
3414 * Give an error message main_errors["n"] and exit.
3415 */
3416 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003417mainerr(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003418 int n, // one of the ME_ defines
3419 char_u *str) // extra argument or NULL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003420{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003421#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003422 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003423#endif
3424
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003425 // If this is a Windows GUI executable, show an error dialog box.
3426#ifdef VIMDLL
3427 gui.in_use = mch_is_gui_executable();
3428#endif
3429#ifdef FEAT_GUI_MSWIN
3430 gui.starting = FALSE; // Needed to show as error.
3431#endif
3432
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003433 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003434 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003435 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003436 mch_errmsg(_(main_errors[n]));
3437 if (str != NULL)
3438 {
3439 mch_errmsg(": \"");
3440 mch_errmsg((char *)str);
3441 mch_errmsg("\"");
3442 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003443 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003444
3445 mch_exit(1);
3446}
3447
3448 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003449mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003450{
3451 mainerr(ME_ARG_MISSING, str);
3452}
3453
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003454#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003455/*
3456 * print a message with three spaces prepended and '\n' appended.
3457 */
3458 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003459main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003460{
3461 mch_msg(" ");
3462 mch_msg(s);
3463 mch_msg("\n");
3464}
3465
3466/*
3467 * Print messages for "vim -h" or "vim --help" and exit.
3468 */
3469 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003470usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003471{
3472 int i;
3473 static char *(use[]) =
3474 {
3475 N_("[file ..] edit specified file(s)"),
3476 N_("- read text from stdin"),
3477 N_("-t tag edit file where tag is defined"),
3478#ifdef FEAT_QUICKFIX
3479 N_("-q [errorfile] edit file with first error")
3480#endif
3481 };
3482
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003483#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003484 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003485#endif
3486
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003487 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003488 mch_msg(longVersion);
Bram Moolenaar32aaf5a2018-06-21 21:38:33 +02003489 mch_msg(_("\n\nUsage:"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003490 for (i = 0; ; ++i)
3491 {
3492 mch_msg(_(" vim [arguments] "));
3493 mch_msg(_(use[i]));
K.Takataeeec2542021-06-02 13:28:16 +02003494 if (i == ARRAY_LENGTH(use) - 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003495 break;
3496 mch_msg(_("\n or:"));
3497 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003498#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003499 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003500#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003501
3502 mch_msg(_("\n\nArguments:\n"));
3503 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003504#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003505 main_msg(_("--literal\t\tDon't expand wildcards"));
3506#endif
3507#ifdef FEAT_OLE
3508 main_msg(_("-register\t\tRegister this gvim for OLE"));
3509 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3510#endif
3511#ifdef FEAT_GUI
3512 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3513 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3514#endif
3515 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3516 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003517 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003518 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3519#ifdef FEAT_DIFF
3520 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3521#endif
3522 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3523 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3524 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3525 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3526 main_msg(_("-M\t\t\tModifications in text not allowed"));
3527 main_msg(_("-b\t\t\tBinary mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003528 main_msg(_("-l\t\t\tLisp mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003529 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3530 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003531 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3532#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003533 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003534#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003535 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3536 main_msg(_("-r\t\t\tList swap files and exit"));
3537 main_msg(_("-r (with file name)\tRecover crashed session"));
3538 main_msg(_("-L\t\t\tSame as -r"));
3539#ifdef AMIGA
3540 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3541 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3542#endif
3543#ifdef FEAT_ARABIC
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003544 main_msg(_("-A\t\t\tStart in Arabic mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003545#endif
3546#ifdef FEAT_RIGHTLEFT
3547 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3548#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003549 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003550 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2d12c252022-06-13 21:42:45 +01003551#ifdef FEAT_GUI
3552 main_msg(_("--gui-dialog-file {fname} For testing: write dialog text"));
3553#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003554 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003555 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3556#ifdef FEAT_GUI
3557 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3558#endif
3559 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003560 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003561 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3562 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3563 main_msg(_("+\t\t\tStart at end of file"));
3564 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003565 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003566 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3567 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3568 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3569 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3570 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3571#ifdef FEAT_CRYPT
3572 main_msg(_("-x\t\t\tEdit encrypted files"));
3573#endif
3574#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3575# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar86181df2020-05-11 23:14:04 +02003576 main_msg(_("-display <display>\tConnect Vim to this particular X-server"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003577# endif
3578 main_msg(_("-X\t\t\tDo not connect to X server"));
3579#endif
3580#ifdef FEAT_CLIENTSERVER
3581 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3582 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3583 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3584 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003585 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003586 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3587 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3588 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3589 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3590#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003591#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003592 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003593#endif
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01003594#ifdef FEAT_JOB_CHANNEL
3595 main_msg(_("--log <file>\tStart logging to <file> early"));
3596#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003597#ifdef FEAT_VIMINFO
3598 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3599#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003600 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003601 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3602 main_msg(_("--version\t\tPrint version information and exit"));
3603
3604#ifdef FEAT_GUI_X11
3605# ifdef FEAT_GUI_MOTIF
3606 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003607# endif
Bram Moolenaar86181df2020-05-11 23:14:04 +02003608 main_msg(_("-display <display>\tRun Vim on <display>"));
Bram Moolenaar9e6ba8c2020-05-12 22:21:26 +02003609 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003610 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3611 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3612 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3613 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3614 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3615 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3616 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3617 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003618 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3619 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3620 main_msg(_("-xrm <resource>\tSet the specified resource"));
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003621#endif // FEAT_GUI_X11
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003622#ifdef FEAT_GUI_GTK
3623 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003624 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3625 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003626 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3627 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003628 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003629 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
Bram Moolenaar86181df2020-05-11 23:14:04 +02003630 main_msg(_("-display <display>\tRun Vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003631 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003632 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003633 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003634#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003635#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003636# ifdef VIMDLL
3637 if (gui.starting)
3638# endif
3639 {
3640 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
3641 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
3642 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003643#endif
3644
3645#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003646 // Gnome gives extra messages for --help if we continue, but not for -h.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003647 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003648 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003649 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003650 gui.dofork = FALSE;
3651 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003652 else
3653#endif
3654 mch_exit(0);
3655}
3656
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003657/*
3658 * Check the result of the ATTENTION dialog:
3659 * When "Quit" selected, exit Vim.
3660 * When "Recover" selected, recover the file.
3661 */
3662 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003663check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003664{
3665 if (swap_exists_action == SEA_QUIT)
3666 getout(1);
3667 handle_swap_exists(NULL);
3668}
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003669
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003670#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003671
Bram Moolenaar595297d2017-03-04 19:11:12 +01003672#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003673 static void
3674set_progpath(char_u *argv0)
3675{
3676 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003677
Bram Moolenaar4f974752019-02-17 17:44:42 +01003678# ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003679 // A relative path containing a "/" will become invalid when using ":cd",
3680 // turn it into a full path.
3681 // On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3682 // this.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003683 char_u *path = NULL;
3684
3685 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3686 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003687# else
3688 char_u buf[MAXPATHL + 1];
3689# ifdef PROC_EXE_LINK
3690 char linkbuf[MAXPATHL + 1];
3691 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003692
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003693 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3694 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003695 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003696 linkbuf[len] = NUL;
3697 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003698 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003699# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003700
3701 if (!mch_isFullName(val))
3702 {
3703 if (gettail(val) != val
3704 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3705 val = buf;
3706 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003707# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003708
Bram Moolenaar08cab962017-03-04 14:37:18 +01003709 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003710
Bram Moolenaar4f974752019-02-17 17:44:42 +01003711# ifdef MSWIN
Bram Moolenaar43663192017-03-05 14:29:12 +01003712 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003713# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003714}
3715
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003716#endif // NO_VIM_MAIN