blob: ecc61f4d0be886ae923887a7e8adb469c79af342 [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
K.Takata3c240f62023-05-31 12:47:45 +010079#ifdef _IOLBF
80static void *s_vbuf = NULL; // buffer for setvbuf()
81#endif
82
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010083#ifndef NO_VIM_MAIN // skip this for unittests
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020084
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010085static char_u *start_dir = NULL; // current working dir on startup
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020086
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +020087static int has_dash_c_arg = FALSE;
88
Bram Moolenaarafde13b2019-04-28 19:46:49 +020089# ifdef VIMDLL
90__declspec(dllexport)
91# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000092 int
Bram Moolenaar796cc422019-04-03 20:31:00 +020093# ifdef MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +000094VimMain
95# else
96main
97# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +010098(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +000099{
Bram Moolenaar77780b62018-03-01 23:10:45 +0100100#if defined(STARTUPTIME) || defined(CLEAN_RUNTIMEPATH)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000101 int i;
102#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000103
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000104 /*
105 * Do any system-specific initialisations. These can NOT use IObuff or
106 * NameBuff. Thus emsg2() cannot be called!
107 */
108 mch_early_init();
109
Bram Moolenaar4f974752019-02-17 17:44:42 +0100110#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +0200111 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200112 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200113 * convert when 'encoding' changes. Get the unexpanded arguments.
114 */
115 argc = get_cmd_argsW(&argv);
116#endif
117
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100118 // Many variables are in "params" so that we can pass them to invoked
119 // functions without a lot of arguments. "argc" and "argv" are also
120 // copied, so that they can be changed.
Bram Moolenaara80faa82020-04-12 19:37:17 +0200121 CLEAR_FIELD(params);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000122 params.argc = argc;
123 params.argv = argv;
124 params.want_full_screen = TRUE;
125#ifdef FEAT_EVAL
126 params.use_debug_break_level = -1;
127#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000128 params.window_count = -1;
Bram Moolenaar58d98232005-07-23 22:25:46 +0000129
Bram Moolenaare76062c2022-11-28 18:51:43 +0000130 autocmd_init();
131
Bram Moolenaar99685e62013-05-11 13:56:18 +0200132#ifdef FEAT_RUBY
133 {
134 int ruby_stack_start;
135 vim_ruby_init((void *)&ruby_stack_start);
136 }
137#endif
138
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000139#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000140 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000141#endif
142
143#ifdef MEM_PROFILE
144 atexit(vim_mem_profile_dump);
145#endif
146
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100147#if defined(STARTUPTIME) || defined(FEAT_JOB_CHANNEL)
148 // Need to find "--startuptime" and "--log" before actually parsing
149 // arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100150 for (i = 1; i < argc - 1; ++i)
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100151 {
152# ifdef STARTUPTIME
153 if (STRICMP(argv[i], "--startuptime") == 0 && time_fd == NULL)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000154 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000155 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000156 TIME_MSG("--- VIM STARTING ---");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000157 }
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100158# endif
Bram Moolenaar4c5678f2022-11-30 18:12:19 +0000159# ifdef FEAT_EVAL
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100160 if (STRICMP(argv[i], "--log") == 0)
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100161 ch_logfile((char_u *)(argv[i + 1]), (char_u *)"ao");
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100162# endif
163 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000164#endif
165
Bram Moolenaar07268702018-03-01 21:57:32 +0100166#ifdef CLEAN_RUNTIMEPATH
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100167 // Need to find "--clean" before actually parsing arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100168 for (i = 1; i < argc; ++i)
169 if (STRICMP(argv[i], "--clean") == 0)
170 {
171 params.clean = TRUE;
172 break;
173 }
174#endif
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200175#ifdef MSWIN
Bram Moolenaar4a228972021-05-01 22:41:39 +0200176 // Need to find "-register" and "-unregister" before loading any libraries.
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200177 for (i = 1; i < argc; ++i)
Bram Moolenaar4a228972021-05-01 22:41:39 +0200178 if ((STRICMP(argv[i] + 1, "register") == 0
179 || STRICMP(argv[i] + 1, "unregister") == 0)
180 && (argv[i][0] == '-' || argv[i][0] == '/'))
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200181 {
182 found_register_arg = TRUE;
183 break;
184 }
185#endif
186
187 /*
188 * Various initialisations shared with tests.
189 */
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200190 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000191
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200192#ifdef VIMDLL
193 // Check if the current executable file is for the GUI subsystem.
194 gui.starting = mch_is_gui_executable();
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +0200195#elif defined(FEAT_GUI_MSWIN)
196 gui.starting = TRUE;
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200197#endif
198
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000199#ifdef FEAT_CLIENTSERVER
200 /*
201 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000202 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000203 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000204 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000205#endif
206
207 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000208 * Figure out the way to work from the command name argv[0].
209 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000210 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000211 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000212
213 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000214 * Process the command line arguments. File names are put in the global
215 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000216 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000217 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000218 TIME_MSG("parsing arguments");
219
220 /*
221 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000222 * there is no terminal version, and on Windows we can't fork one off with
223 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000224 */
225#ifdef ALWAYS_USE_GUI
226 gui.starting = TRUE;
227#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000228# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000229 /*
230 * Check if the GUI can be started. Reset gui.starting if not.
231 * Don't know about other systems, stay on the safe side and don't check.
232 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000233 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000234 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000235 if (gui_init_check() == FAIL)
236 {
237 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000238
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100239 // When running "evim" or "gvim -y" we need the menus, exit if we
240 // don't have them.
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000241 if (params.evim_mode)
242 mch_exit(1);
243 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000244 }
245# endif
246#endif
247
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000248 if (GARGCOUNT > 0)
249 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100250#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000251 /*
252 * Expand wildcards in file names.
253 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000254 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000255 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200256 start_dir = alloc(MAXPATHL);
257 if (start_dir != NULL)
258 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100259 // Temporarily add '(' and ')' to 'isfname'. These are valid
260 // filename characters but are excluded from 'isfname' to make
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000261 // "gf" work on a file name in parentheses (e.g.: see vim.h).
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000262 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000263 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000264 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200265 if (start_dir != NULL)
266 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000267 }
268#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200269 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000270 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000271
Bram Moolenaar4f974752019-02-17 17:44:42 +0100272#ifdef MSWIN
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000273 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100274 // Remember the number of entries in the argument list. If it changes
275 // we don't react on setting 'encoding'.
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000276 set_alist_count();
277 }
278#endif
279
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000280#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000281 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000282 {
283 /*
284 * If there is one filename, fully qualified, we have very probably
285 * been invoked from explorer, so change to the file's directory.
286 * Hint: to avoid this when typing a command use a forward slash.
287 * If the cd fails, it doesn't matter.
288 */
Bram Moolenaar05268152021-11-18 18:53:45 +0000289 if (vim_chdirfile(params.fname, "drop") == OK)
290 last_chdir_reason = "drop";
Bram Moolenaarf6303872015-04-03 17:59:43 +0200291 if (start_dir != NULL)
292 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000293 }
294#endif
295 TIME_MSG("expanding arguments");
296
297#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000298 if (params.diff_mode && params.window_count == -1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100299 params.window_count = 0; // open up to 3 windows
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000300#endif
301
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100302 // Don't redraw until much later.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000303 ++RedrawingDisabled;
304
305 /*
306 * When listing swap file names, don't do cursor positioning et. al.
307 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200308 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000309 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000310
311 /*
Bram Moolenaarc174c2e2023-03-25 20:06:49 +0000312 * When certain to start the GUI, don't check terminal capabilities.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000313 * For GTK we can't be sure, but when started from the desktop it doesn't
314 * make sense to try using a terminal.
315 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200316#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
317 || defined(VIMDLL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000318 if (gui.starting
319# ifdef FEAT_GUI_GTK
320 && !isatty(2)
321# endif
322 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000323 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000324#endif
325
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000326 /*
327 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100328 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000329 * Note that we may use mch_exit() before mch_init()!
330 */
331 mch_init();
332 TIME_MSG("shell init");
333
334#ifdef USE_XSMP
335 /*
336 * For want of anywhere else to do it, try to connect to xsmp here.
337 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
338 * Hijacking -X 'no X connection' to also disable XSMP connection as that
339 * has a similar delay upon failure.
340 * Only try if SESSION_MANAGER is set to something non-null.
341 */
342 if (!x_no_connect)
343 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000344 char *p = getenv("SESSION_MANAGER");
345
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000346 if (p != NULL && *p != NUL)
347 {
348 xsmp_init();
349 TIME_MSG("xsmp init");
350 }
351 }
352#endif
353
354 /*
355 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000356 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000357 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000358
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100359#ifdef _IOLBF
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100360 if (silent_mode)
K.Takata3c240f62023-05-31 12:47:45 +0100361 {
362 // Ensure output works usefully without a tty: buffer lines instead of
363 // fully buffered.
364 s_vbuf = malloc(BUFSIZ);
365 if (s_vbuf != NULL)
366 setvbuf(stdout, s_vbuf, _IOLBF, BUFSIZ);
367 }
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100368#endif
369
Bram Moolenaar9404a182019-05-03 22:25:40 +0200370 // This message comes before term inits, but after setting "silent_mode"
371 // when the input is not a tty. Omit the message with --not-a-term.
372 if (GARGCOUNT > 1 && !silent_mode && !is_not_a_term())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000373 printf(_("%d files to edit\n"), GARGCOUNT);
374
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000375 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000376 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100377 termcapinit(params.term); // set terminal name and get terminal
378 // capabilities (will set full_screen)
379 screen_start(); // don't know where cursor is now
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000380 TIME_MSG("Termcap init");
381 }
382
383 /*
384 * Set the default values for the options that use Rows and Columns.
385 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100386 ui_get_shellsize(); // inits Rows and Columns
Luuk van Baal470a1412022-09-14 01:27:23 +0100387 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000388#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100389 // Set the 'diff' option now, so that it can be checked for in a .vimrc
390 // file. There is no buffer yet though.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000391 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000392 diff_win_options(firstwin, FALSE);
393#endif
394
395 cmdline_row = Rows - p_ch;
396 msg_row = cmdline_row;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100397 screenalloc(FALSE); // allocate screen buffers
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000398 set_init_2();
399 TIME_MSG("inits 2");
400
401 msg_scroll = TRUE;
402 no_wait_return = TRUE;
403
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100404 init_mappings(); // set up initial mappings
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000405
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100406 init_highlight(TRUE, FALSE); // set the default highlight groups
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000407 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000408
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200409#if defined(FEAT_TERMRESPONSE)
410 init_term_props(TRUE);
411#endif
412
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000413#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100414 // Set the break level after the terminal is initialized.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000415 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000416#endif
417
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100418 // Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
419 // Allows for setting 'loadplugins' there.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200420 if (params.use_vimrc != NULL
421 && (STRCMP(params.use_vimrc, "NONE") == 0
422 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
423 p_lpl = FALSE;
424
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100425 // Execute --cmd arguments.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200426 exe_pre_commands(&params);
427
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100428 // Source startup scripts.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200429 source_startup_scripts(&params);
430
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100431#ifdef FEAT_MZSCHEME
432 /*
433 * Newer version of MzScheme (Racket) require earlier (trampolined)
434 * initialisation via scheme_main_setup.
435 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200436 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200437 * Do source startup scripts, so that 'mzschemedll' can be set.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100438 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200439 return mzscheme_main();
440#else
441 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100442#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200443}
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100444#endif // NO_VIM_MAIN
445#endif // PROTO
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100446
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200447/*
448 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
449 * things simple.
450 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
451 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100452 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200453vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100454{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100455#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000456#ifdef FEAT_EVAL
457 /*
458 * Read all the plugin files.
459 * Only when compiled with +eval, since most plugins need it.
460 */
461 if (p_lpl)
462 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200463 char_u *rtp_copy = NULL;
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100464 char_u *plugin_pattern = (char_u *)
465# if defined(VMS) || defined(AMIGA) // VMS and Amiga don't handle the "**".
466 "plugin/*.vim"
467# else
468 "plugin/**/*.vim"
469# endif
470 ;
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200471
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100472 // First add all package directories to 'runtimepath', so that their
473 // autoload directories can be found. Only if not done already with a
474 // :packloadall command.
475 // Make a copy of 'runtimepath', so that source_runtime does not use
476 // the pack directories.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200477 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200478 {
479 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200480 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200481 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200482
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100483 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy, plugin_pattern,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100484 DIP_ALL | DIP_NOAFTER, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000485 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200486 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100487
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100488 // Only source "start" packages if not done already with a :packloadall
489 // command.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200490 if (!did_source_packages)
491 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100492 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200493
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100494 source_runtime(plugin_pattern, DIP_ALL | DIP_AFTER);
Bram Moolenaar66459b72016-08-06 19:01:55 +0200495 TIME_MSG("loading after plugins");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000496 }
497#endif
498
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000499#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100500 // Decide about window layout for diff mode after reading vimrc.
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000501 if (params.diff_mode && params.window_layout == 0)
502 {
503 if (diffopt_horizontal())
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100504 params.window_layout = WIN_HOR; // use horizontal split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000505 else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100506 params.window_layout = WIN_VER; // use vertical split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000507 }
508#endif
509
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000510 /*
511 * Recovery mode without a file name: List swap files.
512 * This uses the 'dir' option, therefore it must be after the
513 * initializations.
514 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200515 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000516 {
Bram Moolenaarc216a7a2022-12-05 13:50:55 +0000517 recover_names(NULL, TRUE, NULL, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000518 mch_exit(0);
519 }
520
521 /*
522 * Set a few option defaults after reading .vimrc files:
523 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
524 */
525 set_init_3();
526 TIME_MSG("inits 3");
527
528 /*
529 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
530 * Note that this overrides anything from a vimrc file.
531 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000532 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000533 p_uc = 0;
534
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000535#ifdef FEAT_GUI
536 if (gui.starting)
537 {
Bram Moolenaarc7226682019-08-17 16:33:23 +0200538# if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100539 // When something caused a message from a vimrc script, need to output
540 // an extra newline before the shell prompt.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000541 if (did_emsg || msg_didout)
542 putchar('\n');
Bram Moolenaarc7226682019-08-17 16:33:23 +0200543# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000544
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100545 gui_start(NULL); // will set full_screen to TRUE
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000546 TIME_MSG("starting GUI");
547
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100548 // When running "evim" or "gvim -y" we need the menus, exit if we
549 // don't have them.
Bram Moolenaar58d98232005-07-23 22:25:46 +0000550 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000551 mch_exit(1);
Luuk van Baal470a1412022-09-14 01:27:23 +0100552 firstwin->w_prev_height = firstwin->w_height; // may have changed
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000553 }
554#endif
555
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000556#ifdef FEAT_VIMINFO
557 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000558 * Read in registers, history etc, but not marks, from the viminfo file.
559 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000560 */
561 if (*p_viminfo != NUL)
562 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000563 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000564 TIME_MSG("reading viminfo");
565 }
566#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100567#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100568 // It's better to make v:oldfiles an empty list than NULL.
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100569 if (get_vim_var_list(VV_OLDFILES) == NULL)
570 set_vim_var_list(VV_OLDFILES, list_alloc());
571#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000572
573#ifdef FEAT_QUICKFIX
574 /*
575 * "-q errorfile": Load the error file now.
576 * If the error file can't be read, exit before doing anything else.
577 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000578 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000579 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100580 char_u *enc = NULL;
581
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100582 enc = p_menc;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000583 if (params.use_ef != NULL)
584 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000585 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200586 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100587 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000588 {
589 out_char('\n');
590 mch_exit(3);
591 }
592 TIME_MSG("reading errorfile");
593 }
594#endif
595
596 /*
597 * Start putting things on the screen.
598 * Scroll screen down before drawing over it
599 * Clear screen now, so file message will not be cleared.
600 */
601 starting = NO_BUFFERS;
602 no_wait_return = FALSE;
603 if (!exmode_active)
604 msg_scroll = FALSE;
605
606#ifdef FEAT_GUI
607 /*
608 * This seems to be required to make callbacks to be called now, instead
609 * of after things have been put on the screen, which then may be deleted
610 * when getting a resize callback.
611 * For the Mac this handles putting files dropped on the Vim icon to
612 * global_alist.
613 */
614 if (gui.in_use)
615 {
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100616 gui_wait_for_chars(50L, typebuf.tb_change_cnt);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000617 TIME_MSG("GUI delay");
618 }
619#endif
620
621#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
622 qnx_clip_init();
623#endif
624
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200625#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
626 clip_init(TRUE);
627#endif
628
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000629#ifdef FEAT_XCLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100630 // Start using the X clipboard, unless the GUI was started.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000631# ifdef FEAT_GUI
632 if (!gui.in_use)
633# endif
634 {
635 setup_term_clip();
636 TIME_MSG("setup clipboard");
637 }
638#endif
639
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000640#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100641 // Prepare for being a Vim server.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000642 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000643#endif
644
645 /*
646 * If "-" argument given: Read file from stdin.
647 * Do this before starting Raw mode, because it may change things that the
648 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
649 * are the same terminal: "cat | vim -".
650 * Using autocommands here may cause trouble...
651 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000652 if (params.edit_type == EDIT_STDIN && !recoverymode)
653 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000654
655#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100656 // When switching screens and something caused a message from a vimrc
657 // script, need to output an extra newline on exit.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000658 if ((did_emsg || msg_didout) && *T_TI != NUL)
659 newline_on_exit = TRUE;
660#endif
661
662 /*
K.Takataeeec2542021-06-02 13:28:16 +0200663 * When done something that is not allowed or given an error message call
Bram Moolenaar13608d82022-08-29 15:06:50 +0100664 * wait_return(). This must be done before starttermcap(), because it may
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000665 * switch to another screen. It must be done after settmode(TMODE_RAW),
666 * because we want to react on a single key stroke.
667 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000668 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000669 */
670 settmode(TMODE_RAW);
671 TIME_MSG("setting raw mode");
672
673 if (need_wait_return || msg_didany)
674 {
675 wait_return(TRUE);
676 TIME_MSG("waiting for return");
677 }
678
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100679 starttermcap(); // start termcap if not done by wait_return()
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000680 TIME_MSG("start termcap");
681
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200682 setmouse(); // may start using the mouse
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000683 if (scroll_region)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100684 scroll_region_reset(); // In case Rows changed
685 scroll_start(); // may scroll the screen to the right position
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000686
Bram Moolenaar651fca82021-11-29 20:39:38 +0000687#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar40385db2018-08-07 22:31:44 +0200688 term_push_title(SAVE_RESTORE_BOTH);
689#endif
690
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000691 /*
692 * Don't clear the screen when starting in Ex mode, unless using the GUI.
693 */
694 if (exmode_active
695#ifdef FEAT_GUI
696 && !gui.in_use
697#endif
698 )
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100699 set_must_redraw(UPD_CLEAR);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000700 else
701 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100702 screenclear(); // clear screen
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000703 TIME_MSG("clearing screen");
704 }
705
706#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000707 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000708 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100709 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200710 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000711 TIME_MSG("getting crypt key");
712 }
713#endif
714
715 no_wait_return = TRUE;
716
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000717 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000718 * Create the requested number of windows and edit buffers in them.
719 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000720 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000721 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000722 TIME_MSG("opening buffers");
723
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000724#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100725 // clear v:swapcommand
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000726 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
727#endif
728
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100729 // Ex starts at last line of the file
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000730 if (exmode_active)
731 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
732
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000733 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
734 TIME_MSG("BufEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000735 setpcmark();
736
737#ifdef FEAT_QUICKFIX
738 /*
739 * When started with "-q errorfile" jump to first error now.
740 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000741 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000742 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000743 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000744 TIME_MSG("jump to first error");
745 }
746#endif
747
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000748 /*
749 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000750 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000751 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200752 edit_buffers(&params, start_dir);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200753 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000754
755#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000756 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000757 {
758 win_T *wp;
759
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100760 // set options in each window for "vimdiff".
Bram Moolenaar29323592016-07-24 22:04:11 +0200761 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000762 diff_win_options(wp, TRUE);
763 }
764#endif
765
766 /*
767 * Shorten any of the filenames, but only when absolute.
768 */
769 shorten_fnames(FALSE);
770
771 /*
772 * Need to jump to the tag before executing the '-c command'.
773 * Makes "vim -c '/return' -t main" work.
774 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000775 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000776 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000777 swap_exists_did_quit = FALSE;
Bram Moolenaar146522e2005-12-16 21:55:46 +0000778
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000779 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000780 do_cmdline_cmd(IObuff);
781 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000782
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100783 // If the user doesn't want to edit the file then we quit here.
Bram Moolenaar146522e2005-12-16 21:55:46 +0000784 if (swap_exists_did_quit)
785 getout(1);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000786 }
787
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100788 // Execute any "+", "-c" and "-S" arguments.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000789 if (params.n_commands > 0)
790 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000791
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100792 // Must come before the may_req_ calls.
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200793 starting = 0;
794
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100795#if defined(FEAT_TERMRESPONSE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100796 // Must be done before redrawing, puts a few characters on the screen.
Bram Moolenaara45551a2020-06-09 15:57:37 +0200797 check_terminal_behavior();
Bram Moolenaar976787d2017-06-04 15:45:50 +0200798#endif
799
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000800 RedrawingDisabled = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100801 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000802 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000803
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100804 // 'autochdir' has been postponed
Bram Moolenaar6f470022018-04-10 18:47:20 +0200805 DO_AUTOCHDIR;
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200806
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000807#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100808 // Requesting the termresponse is postponed until here, so that a "-c q"
809 // argument doesn't make it appear in the shell Vim was started from.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000810 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200811
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200812 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000813#endif
814
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100815 // start in insert mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000816 if (p_im)
817 need_start_insertmode = TRUE;
818
Bram Moolenaar14735512016-03-26 21:00:08 +0100819#ifdef FEAT_EVAL
820 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
821#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000822 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
823 TIME_MSG("VimEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000824
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200825#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100826 // Adjust default register name for "unnamed" in 'clipboard'. Can only be
827 // done after the clipboard is available and all initial commands that may
828 // modify the 'clipboard' setting have run; i.e. just before entering the
829 // main loop.
Bram Moolenaar439c0362020-06-06 15:58:03 +0200830 reset_reg_var();
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200831#endif
832
Bram Moolenaar8a3bb562018-03-04 20:14:14 +0100833#if defined(FEAT_DIFF)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100834 // When a startup script or session file setup for diff'ing and
835 // scrollbind, sync the scrollbind now.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000836 if (curwin->w_p_diff && curwin->w_p_scb)
837 {
838 update_topline();
839 check_scrollbind((linenr_T)0, 0L);
840 TIME_MSG("diff scrollbinding");
841 }
842#endif
843
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200844#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
845# ifdef VIMDLL
846 if (!gui.in_use)
847# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100848 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000849#endif
850
Bram Moolenaar4033c552017-09-16 20:54:51 +0200851#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100852 // When tab pages were created, may need to update the tab pages line and
853 // scrollbars. This is skipped while creating them.
h-east6073f132021-12-24 11:57:06 +0000854 if (gui.in_use && first_tabpage->tp_next != NULL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000855 {
856 out_flush();
857 gui_init_which_components(NULL);
858 gui_update_scrollbars(TRUE);
859 }
860 need_mouse_correct = TRUE;
861#endif
862
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100863 // If ":startinsert" command used, stuff a dummy command to be able to
864 // call normal_cmd(), which will then start Insert mode.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000865 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000866 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000867
868#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200869 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200870 {
871# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200872# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar4f974752019-02-17 17:44:42 +0100873 && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200874 if (gui.in_use)
875 {
876 mch_errmsg(_("netbeans is not supported with this GUI\n"));
877 mch_exit(2);
878 }
879# endif
880# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100881 // Tell the client that it can start sending commands.
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200882 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200883 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000884#endif
885
Bram Moolenaare7ddf4e2020-02-03 21:29:30 +0100886 // Redraw at least once, also when 'lazyredraw' is set, to make sure the
887 // window title gets updated.
888 do_redraw = TRUE;
889
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000890 TIME_MSG("before starting main loop");
891
892 /*
893 * Call the main command loop. This never returns.
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200894 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000895 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000896
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100897#endif // NO_VIM_MAIN
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200898
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000899 return 0;
900}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000901
902/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200903 * Initialisation shared by main() and some tests.
904 */
905 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200906common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200907{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100908 estack_init();
Bram Moolenaar438d1762018-09-30 17:11:48 +0200909 cmdline_init();
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200910
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100911 (void)mb_init(); // init mb_bytelen_tab[] to ones
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200912#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100913 eval_init(); // init global variables
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200914#endif
915
916#ifdef __QNXNTO__
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100917 qnx_init(); // PhAttach() for clipboard, (and gui)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200918#endif
919
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200920 /*
921 * Allocate space for the generic buffers (needed for set_init_1() and
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100922 * emsg()).
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200923 */
924 if ((IObuff = alloc(IOSIZE)) == NULL
925 || (NameBuff = alloc(MAXPATHL)) == NULL)
926 mch_exit(0);
927 TIME_MSG("Allocated generic buffers");
928
929#ifdef NBDEBUG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100930 // Wait a moment for debugging NetBeans. Must be after allocating
931 // NameBuff.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200932 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
933 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
934 TIME_MSG("NetBeans debug wait");
935#endif
936
937#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
938 /*
939 * Setup to use the current locale (for ctype() and many other things).
940 * NOTE: Translated messages with encodings other than latin1 will not
941 * work until set_init_1() has been called!
942 */
943 init_locale();
944 TIME_MSG("locale set");
945#endif
946
947#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100948 gui.dofork = TRUE; // default is to use fork()
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200949#endif
950
951 /*
952 * Do a first scan of the arguments in "argv[]":
953 * -display or --display
954 * --server...
955 * --socketid
956 * --windowid
957 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200958 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200959
Bram Moolenaard0573012017-10-28 21:11:06 +0200960#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100961 // Prepare for possibly starting GUI sometime
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200962 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200963 TIME_MSG("GUI prepared");
964#endif
965
966#ifdef FEAT_CLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100967 clip_init(FALSE); // Initialise clipboard stuff
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200968 TIME_MSG("clipboard setup");
969#endif
970
971 /*
972 * Check if we have an interactive window.
973 * On the Amiga: If there is no window, we open one with a newcli command
974 * (needed for :! to * work). mch_check_win() will also handle the -d or
975 * -dev argument.
976 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100977 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200978 TIME_MSG("window checked");
979
980 /*
981 * Allocate the first window and buffer.
982 * Can't do anything without it, exit when it fails.
983 */
984 if (win_alloc_first() == FAIL)
985 mch_exit(0);
986
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100987 init_yank(); // init yank buffers
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200988
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100989 alist_init(&global_alist); // Init the argument list to empty.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200990 global_alist.id = 0;
991
992 /*
993 * Set the default values for the options.
994 * NOTE: Non-latin1 translated messages are working only after this,
995 * because this is where "has_mbyte" will be set, which is used by
996 * msg_outtrans_len_attr().
997 * First find out the home directory, needed to expand "~" in options.
998 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100999 init_homedir(); // find real value of $HOME
Bram Moolenaar07268702018-03-01 21:57:32 +01001000 set_init_1(paramp->clean);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001001 TIME_MSG("inits 1");
1002
1003#ifdef FEAT_EVAL
Bram Moolenaar69bf6342019-10-29 04:16:57 +01001004 // set v:lang and v:ctype
1005 set_lang_var();
1006
1007 // set v:argv
1008 set_argv_var(paramp->argv, paramp->argc);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001009#endif
Bram Moolenaar6436cd82018-12-27 00:28:33 +01001010
1011#ifdef FEAT_SIGNS
1012 init_signs();
1013#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001014}
1015
1016/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001017 * Return TRUE when the --not-a-term argument was found.
1018 */
1019 int
Bram Moolenaarf7e73022022-09-24 13:10:04 +01001020is_not_a_term(void)
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001021{
1022 return params.not_a_term;
1023}
1024
Bram Moolenaar7007e312021-03-27 12:11:33 +01001025/*
1026 * Return TRUE when the --not-a-term argument was found or the GUI is in use.
1027 */
Bram Moolenaarf7e73022022-09-24 13:10:04 +01001028 int
1029is_not_a_term_or_gui(void)
Bram Moolenaar7007e312021-03-27 12:11:33 +01001030{
1031 return params.not_a_term
1032#ifdef FEAT_GUI
1033 || gui.in_use
1034#endif
1035 ;
1036}
1037
K.Takata3c240f62023-05-31 12:47:45 +01001038#if defined(EXITFREE) || defined(PROTO)
1039 void
1040free_vbuf(void)
1041{
1042# ifdef _IOLBF
1043 if (s_vbuf != NULL)
1044 {
1045 setvbuf(stdout, NULL, _IONBF, 0);
1046 free(s_vbuf);
1047 s_vbuf = NULL;
1048 }
1049# endif
1050}
1051#endif
1052
Bram Moolenaar2d12c252022-06-13 21:42:45 +01001053#if defined(FEAT_GUI) || defined(PROTO)
1054/*
1055 * If a --gui-dialog-file argument was given return the file name.
1056 * Otherwise return NULL.
1057 */
1058 char_u *
1059get_gui_dialog_file(void)
1060{
1061 return params.gui_dialog_file;
1062}
1063#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001064
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001065// When TRUE in a safe state when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001066static int was_safe = FALSE;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001067static oparg_T *current_oap = NULL;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001068
1069/*
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001070 * Return TRUE if an operator was started but not finished yet.
1071 * Includes typing a count or a register name.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001072 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001073 int
1074op_pending(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001075{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001076 return !(current_oap != NULL
1077 && !finish_op
1078 && current_oap->prev_opcount == 0
1079 && current_oap->prev_count0 == 0
1080 && current_oap->op_type == OP_NOP
1081 && current_oap->regname == NUL);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001082}
1083
1084/*
Bram Moolenaard103ee72019-09-18 21:15:31 +02001085 * Return whether currently it is safe, assuming it was safe before (high level
1086 * state didn't change).
1087 */
1088 static int
1089is_safe_now(void)
1090{
1091 return stuff_empty()
1092 && typebuf.tb_len == 0
1093 && scriptin[curscript] == NULL
Bram Moolenaar4fa13462022-02-05 12:39:24 +00001094#ifdef FEAT_EVAL
1095 && !debug_mode
1096#endif
1097 && !global_busy;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001098}
1099
1100/*
zeertzjqc4226622024-04-03 22:38:07 +02001101 * Trigger SafeState if currently in a safe state, that is "safe" is TRUE and
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001102 * there is no typeahead.
1103 */
1104 void
1105may_trigger_safestate(int safe)
1106{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001107 int is_safe = safe && is_safe_now();
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001108
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001109#ifdef FEAT_EVAL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001110 if (was_safe != is_safe)
1111 // Only log when the state changes, otherwise it happens at nearly
1112 // every key stroke.
Bram Moolenaard103ee72019-09-18 21:15:31 +02001113 ch_log(NULL, is_safe ? "SafeState: Start triggering"
1114 : "SafeState: Stop triggering");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001115#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001116 if (is_safe)
1117 apply_autocmds(EVENT_SAFESTATE, NULL, NULL, FALSE, curbuf);
1118 was_safe = is_safe;
1119}
1120
1121/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001122 * Something changed which causes the state possibly to be unsafe, e.g. a
1123 * character was typed. It will remain unsafe until the next call to
1124 * may_trigger_safestate().
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001125 */
1126 void
Bram Moolenaard103ee72019-09-18 21:15:31 +02001127state_no_longer_safe(char *reason UNUSED)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001128{
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001129#ifdef FEAT_EVAL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001130 if (was_safe)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001131 ch_log(NULL, "SafeState: reset: %s", reason);
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001132#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001133 was_safe = FALSE;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001134}
1135
Dominique Pelle748b3082022-01-08 12:41:16 +00001136#if defined(FEAT_EVAL) || defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001137 int
1138get_was_safe_state(void)
1139{
1140 return was_safe;
1141}
Dominique Pelle748b3082022-01-08 12:41:16 +00001142#endif
Bram Moolenaard103ee72019-09-18 21:15:31 +02001143
Dominique Pelle748b3082022-01-08 12:41:16 +00001144#if defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001145/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001146 * Invoked when leaving code that invokes callbacks. Then trigger
1147 * SafeStateAgain, if it was safe when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001148 */
1149 void
Bram Moolenaar37d18072019-09-17 20:28:38 +02001150may_trigger_safestateagain(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001151{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001152 if (!was_safe)
1153 {
1154 // If the safe state was reset in state_no_longer_safe(), e.g. because
1155 // of calling feedkeys(), we check if it's now safe again (all keys
1156 // were consumed).
1157 was_safe = is_safe_now();
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001158# ifdef FEAT_EVAL
Bram Moolenaard103ee72019-09-18 21:15:31 +02001159 if (was_safe)
1160 ch_log(NULL, "SafeState: undo reset");
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001161# endif
Bram Moolenaard103ee72019-09-18 21:15:31 +02001162 }
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001163 if (was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02001164 {
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001165# ifdef FEAT_EVAL
Bram Moolenaar101e9922019-09-25 21:43:11 +02001166 // Only do this message when another message was given, otherwise we
1167 // get lots of them.
1168 if ((did_repeated_msg & REPEATED_MSG_SAFESTATE) == 0)
1169 {
1170 int did = did_repeated_msg;
1171
1172 ch_log(NULL,
1173 "SafeState: back to waiting, triggering SafeStateAgain");
1174 did_repeated_msg = did | REPEATED_MSG_SAFESTATE;
1175 }
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001176# endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001177 apply_autocmds(EVENT_SAFESTATEAGAIN, NULL, NULL, FALSE, curbuf);
Bram Moolenaar37d18072019-09-17 20:28:38 +02001178 }
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001179# ifdef FEAT_EVAL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001180 else
Bram Moolenaard103ee72019-09-18 21:15:31 +02001181 ch_log(NULL,
1182 "SafeState: back to waiting, not triggering SafeStateAgain");
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001183# endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001184}
Dominique Pelle748b3082022-01-08 12:41:16 +00001185#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001186
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001187/*
1188 * Return TRUE if there is any typeahead, pending operator or command.
1189 */
1190 int
1191work_pending(void)
1192{
1193 return op_pending() || !is_safe_now();
1194}
1195
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001196
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001197/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001198 * Main loop: Execute Normal mode commands until exiting Vim.
1199 * Also used to handle commands in the command-line window, until the window
1200 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201 * Also used to handle ":visual" command after ":global": execute Normal mode
1202 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001203 */
1204 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001205main_loop(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001206 int cmdwin, // TRUE when working in the command-line window
1207 int noexmode) // TRUE when return on entering Ex mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001208{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001209 oparg_T oa; // operator arguments
1210 oparg_T *prev_oap; // operator arguments
1211 volatile int previous_got_int = FALSE; // "got_int" was TRUE
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001212#ifdef FEAT_CONCEAL
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001213 // these are static to avoid a compiler warning
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001214 static linenr_T conceal_old_cursor_line = 0;
1215 static linenr_T conceal_new_cursor_line = 0;
1216 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001217#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001218
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001219 prev_oap = current_oap;
1220 current_oap = &oa;
1221
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001222#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001223 // Setup to catch a terminating error from the X server. Just ignore
1224 // it, restore the state and continue. This might not always work
1225 // properly, but at least we don't exit unexpectedly when the X server
1226 // exits while Vim is running in a console.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001227 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001228 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001229 State = MODE_NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001230 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001231 got_int = TRUE;
1232 need_wait_return = FALSE;
1233 global_busy = FALSE;
1234 exmode_active = 0;
1235 skip_redraw = FALSE;
1236 RedrawingDisabled = 0;
1237 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001238 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001239# ifdef FEAT_EVAL
1240 emsg_skip = 0;
1241# endif
1242 emsg_off = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001243 setmouse();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001244 settmode(TMODE_RAW);
1245 starttermcap();
1246 scroll_start();
1247 redraw_later_clear();
1248 }
1249#endif
1250
1251 clear_oparg(&oa);
Martin Tournoij7904fa42022-10-04 16:28:45 +01001252 while (!cmdwin || cmdwin_result == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001253 {
1254 if (stuff_empty())
1255 {
1256 did_check_timestamps = FALSE;
1257 if (need_check_timestamps)
1258 check_timestamps(FALSE);
Bram Moolenaar13608d82022-08-29 15:06:50 +01001259 if (need_wait_return) // if wait_return() still needed ...
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001260 wait_return(FALSE); // ... call it now
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001261 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001262 {
1263 need_start_insertmode = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001264 stuffReadbuff((char_u *)"i"); // start insert mode next
1265 // skip the fileinfo message now, because it would be shown
1266 // after insert mode finishes!
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001267 need_fileinfo = FALSE;
1268 }
1269 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001270
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001271 // Reset "got_int" now that we got back to the main loop. Except when
1272 // inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1273 // the ":g" command.
1274 // For ":g/pat/vi" we reset "got_int" when used once. When used
1275 // a second time we go back to Ex mode and abort the ":g" command.
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001276 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001277 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001278 if (noexmode && global_busy && !exmode_active && previous_got_int)
1279 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001280 // Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1281 // used and keep "got_int" set, so that it aborts ":g".
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001282 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +01001283 State = MODE_NORMAL;
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001284 }
1285 else if (!global_busy || !exmode_active)
1286 {
1287 if (!quit_more)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001288 (void)vgetc(); // flush all buffers
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001289 got_int = FALSE;
1290 }
1291 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001292 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001293 else
1294 previous_got_int = FALSE;
1295
Bram Moolenaar069613c2022-01-15 15:23:44 +00001296#ifdef FEAT_EVAL
1297 // At the toplevel there is no exception handling. Discard any that
1298 // may be hanging around (e.g. from "interrupt" at the debug prompt).
1299 if (did_throw && !ex_normal_busy)
1300 discard_current_exception();
1301#endif
1302
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001303 if (!exmode_active)
1304 msg_scroll = FALSE;
1305 quit_more = FALSE;
1306
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001307 // it's not safe unless may_trigger_safestate_main() is called
1308 was_safe = FALSE;
1309
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001310 /*
1311 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1312 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1313 * update cursor and redraw.
1314 */
1315 if (skip_redraw || exmode_active)
Bram Moolenaarc174c2e2023-03-25 20:06:49 +00001316 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001317 skip_redraw = FALSE;
Bram Moolenaarc174c2e2023-03-25 20:06:49 +00001318 setcursor();
1319 cursor_on();
1320 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001321 else if (do_redraw || stuff_empty())
1322 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001323#ifdef FEAT_GUI
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001324 // If ui_breakcheck() was used a resize may have been postponed.
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001325 gui_may_resize_shell();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001326#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001327#ifdef HAVE_DROP_FILE
1328 // If files were dropped while text was locked or the curbuf was
1329 // locked, this would be a good time to handle the drop.
1330 handle_any_postponed_drop();
1331#endif
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001332#ifdef FEAT_CONCEAL
1333 if (curwin->w_p_cole == 0)
1334 conceal_update_lines = FALSE;
1335#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001336
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001337 // Trigger CursorMoved if the cursor moved.
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001338 if (!finish_op && (has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001339#ifdef FEAT_PROP_POPUP
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001340 || popup_visible
Bram Moolenaar3397f742019-06-02 18:40:06 +02001341#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001342#ifdef FEAT_CONCEAL
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001343 || curwin->w_p_cole > 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001344#endif
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001345 )
1346 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001347 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001348 if (has_cursormoved())
1349 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1350 FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001351#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001352 if (popup_visible)
1353 popup_check_cursor_pos();
1354#endif
1355#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001356 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001357 {
1358 conceal_old_cursor_line = last_cursormoved.lnum;
1359 conceal_new_cursor_line = curwin->w_cursor.lnum;
1360 conceal_update_lines = TRUE;
1361 }
Bram Moolenaar3397f742019-06-02 18:40:06 +02001362#endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001363 last_cursormoved = curwin->w_cursor;
1364 }
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001365
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001366#if defined(FEAT_CONCEAL)
1367 if (conceal_update_lines
1368 && (conceal_old_cursor_line != conceal_new_cursor_line
1369 || conceal_cursor_line(curwin)
1370 || need_cursor_line_redraw))
1371 {
1372 if (conceal_old_cursor_line != conceal_new_cursor_line
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001373 && conceal_old_cursor_line != 0
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001374 && conceal_old_cursor_line
1375 <= curbuf->b_ml.ml_line_count)
1376 redrawWinline(curwin, conceal_old_cursor_line);
1377 redrawWinline(curwin, conceal_new_cursor_line);
1378 curwin->w_valid &= ~VALID_CROW;
1379 need_cursor_line_redraw = FALSE;
1380 }
1381#endif
1382
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001383 // Trigger TextChanged if b:changedtick differs.
Bram Moolenaar186628f2013-03-19 13:33:23 +01001384 if (!finish_op && has_textchanged()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001385 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001386 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001387 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL, FALSE, curbuf);
1388 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001389 }
Bram Moolenaar186628f2013-03-19 13:33:23 +01001390
LemonBoy09371822022-04-08 15:18:45 +01001391 // Ensure curwin->w_topline and curwin->w_leftcol are up to date
1392 // before triggering a WinScrolled autocommand.
1393 update_topline();
1394 validate_cursor();
1395
1396 if (!finish_op)
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001397 may_trigger_win_scrolled_resized();
LemonBoy09371822022-04-08 15:18:45 +01001398
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001399 // If nothing is pending and we are going to wait for the user to
1400 // type a character, trigger SafeState.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001401 may_trigger_safestate(!op_pending() && restart_edit == 0);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001402
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01001403#if defined(FEAT_DIFF)
Bram Moolenaare3521d92018-09-16 14:10:31 +02001404 // Updating diffs from changed() does not always work properly,
1405 // esp. updating folds. Do an update just before redrawing if
1406 // needed.
1407 if (curtab->tp_diff_update || curtab->tp_diff_invalid)
1408 {
1409 ex_diffupdate(NULL);
1410 curtab->tp_diff_update = FALSE;
1411 }
1412
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001413 // Scroll-binding for diff mode may have been postponed until
1414 // here. Avoids doing it for every change.
Bram Moolenaar33aec762006-01-22 23:30:12 +00001415 if (diff_need_scrollbind)
1416 {
1417 check_scrollbind((linenr_T)0, 0L);
1418 diff_need_scrollbind = FALSE;
1419 }
1420#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001421#if defined(FEAT_FOLDING)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001422 // Include a closed fold completely in the Visual area.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001423 foldAdjustVisual();
1424#endif
1425#ifdef FEAT_FOLDING
1426 /*
1427 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1428 * contain the cursor.
1429 * When 'foldopen' is "all", open the fold(s) under the cursor.
1430 * This may mark the window for redrawing.
1431 */
1432 if (hasAnyFolding(curwin) && !char_avail())
1433 {
1434 foldCheckClose();
1435 if (fdo_flags & FDO_ALL)
1436 foldOpenCursor();
1437 }
1438#endif
1439
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001440 // Before redrawing, make sure w_topline is correct, and w_leftcol
1441 // if lines don't wrap, and w_skipcol if lines wrap.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001442 update_topline();
1443 validate_cursor();
1444
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001445 if (VIsual_active)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001446 update_curbuf(UPD_INVERTED); // update inverted part
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001447 else if (must_redraw)
Bram Moolenaara338adc2018-01-31 20:51:47 +01001448 {
Bram Moolenaar017ba072019-09-14 21:01:23 +02001449 mch_disable_flush(); // Stop issuing gui_mch_flush().
Bram Moolenaar11a58af2019-10-24 22:32:31 +02001450 update_screen(0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01001451 mch_enable_flush();
1452 }
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01001453 else if (redraw_cmdline || clear_cmdline || redraw_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001454 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001455 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001456 if (need_maketitle)
1457 maketitle();
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001458#ifdef FEAT_VIMINFO
1459 curbuf->b_last_used = vim_time();
1460#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001461 // display message after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001462 if (keep_msg != NULL)
1463 {
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001464 char_u *p = vim_strsave(keep_msg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001465
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001466 if (p != NULL)
1467 {
1468 // msg_start() will set keep_msg to NULL, make a copy
1469 // first. Don't reset keep_msg, msg_attr_keep() uses it to
1470 // check for duplicates. Never put this message in
1471 // history.
1472 msg_hist_off = TRUE;
1473 msg_attr((char *)p, keep_msg_attr);
1474 msg_hist_off = FALSE;
1475 vim_free(p);
1476 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001477 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001478 if (need_fileinfo) // show file info after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001479 {
1480 fileinfo(FALSE, TRUE, FALSE);
1481 need_fileinfo = FALSE;
1482 }
1483
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001484 emsg_on_display = FALSE; // can delete error message now
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001485 did_emsg = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001486 msg_didany = FALSE; // reset lines_left in msg_start()
1487 may_clear_sb_text(); // clear scroll-back text on next msg
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001488 showruler(FALSE);
1489
1490 setcursor();
1491 cursor_on();
1492
1493 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001494
1495#ifdef STARTUPTIME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001496 // Now that we have drawn the first screen all the startup stuff
1497 // has been done, close any file for startup messages.
Bram Moolenaar3f269672009-11-03 11:11:11 +00001498 if (time_fd != NULL)
1499 {
1500 TIME_MSG("first screen update");
1501 TIME_MSG("--- VIM STARTED ---");
1502 fclose(time_fd);
1503 time_fd = NULL;
1504 }
1505#endif
Bram Moolenaar0a60f792022-11-19 21:18:11 +00001506 // After the first screen update may start triggering WinScrolled
1507 // autocmd events. Store all the scroll positions and sizes now.
1508 may_make_initial_scroll_size_snapshot();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001509 }
1510#ifdef FEAT_GUI
1511 if (need_mouse_correct)
1512 gui_mouse_correct();
1513#endif
1514
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001515 // May request the keyboard protocol state now.
1516 may_send_t_RK();
1517
1518 // Update w_curswant if w_set_curswant has been set.
1519 // Postponed until here to avoid computing w_virtcol too often.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001520 update_curswant();
1521
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001522#ifdef FEAT_EVAL
1523 /*
1524 * May perform garbage collection when waiting for a character, but
1525 * only at the very toplevel. Otherwise we may be using a List or
1526 * Dict internally somewhere.
1527 * "may_garbage_collect" is reset in vgetc() which is invoked through
1528 * do_exmode() and normal_cmd().
1529 */
1530 may_garbage_collect = (!cmdwin && !noexmode);
1531#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001532 /*
1533 * If we're invoked as ex, do a round of ex commands.
1534 * Otherwise, get and execute a normal mode command.
1535 */
1536 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001537 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001538 if (noexmode) // End of ":global/path/visual" commands
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001539 goto theend;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001540 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001541 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001542 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001543 {
1544#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001545 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001546 && oa.op_type == OP_NOP && oa.regname == NUL
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001547 && !VIsual_active
1548 && !skip_term_loop)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001549 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001550 // If terminal_loop() returns OK we got a key that is handled
Christian Brabandtee17b6f2023-09-09 11:23:50 +02001551 // in Normal mode. With FAIL we first need to position the
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001552 // cursor and the screen needs to be redrawn.
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001553 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001554 normal_cmd(&oa, TRUE);
1555 }
1556 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001557#endif
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001558 {
1559#ifdef FEAT_TERMINAL
1560 skip_term_loop = FALSE;
1561#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001562 normal_cmd(&oa, TRUE);
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001563 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02001564 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001565 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001566
1567theend:
1568 current_oap = prev_oap;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001569}
1570
1571
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001572#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001573/*
1574 * Exit, but leave behind swap files for modified buffers.
1575 */
1576 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001577getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001578{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001579# if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001580 // Ignore SIGHUP, because a dropped connection causes a read error, which
1581 // makes Vim exit and then handling SIGHUP causes various reentrance
1582 // problems.
ichizok378447f2023-05-11 22:25:42 +01001583 mch_signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001584# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001585
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001586 ml_close_notmod(); // close all not-modified buffers
1587 ml_sync_all(FALSE, FALSE); // preserve all swap files
1588 ml_close_all(FALSE); // close all memfiles, without deleting
1589 getout(exitval); // exit Vim properly
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001590}
1591#endif
1592
1593
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001594/*
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001595 * Exit properly. This is the only way to exit Vim after startup has
1596 * succeeded. We are certain to exit here, no way to abort it.
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001597 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001598 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001599getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001600{
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001601 exiting = TRUE;
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001602#if defined(FEAT_EVAL)
Bram Moolenaar0bd052b2018-03-22 20:33:56 +01001603 ch_log(NULL, "Exiting...");
1604#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001605
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001606 // When running in Ex mode an error causes us to exit with a non-zero exit
1607 // code. POSIX requires this, although it's not 100% clear from the
1608 // standard.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001609 if (exmode_active)
1610 exitval += ex_exitval;
1611
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001612#ifdef FEAT_EVAL
1613 set_vim_var_type(VV_EXITING, VAR_NUMBER);
1614 set_vim_var_nr(VV_EXITING, exitval);
1615#endif
1616
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001617 // Position the cursor on the last screen line, below all the text
Bram Moolenaar7007e312021-03-27 12:11:33 +01001618 if (!is_not_a_term_or_gui())
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001619 windgoto((int)Rows - 1, 0);
1620
Bram Moolenaar58779852022-09-06 18:31:14 +01001621#ifdef FEAT_EVAL
1622 // Invoked all deferred functions in the function stack.
1623 invoke_all_defer();
1624#endif
1625
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001626#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001627 // Optionally print hashtable efficiency.
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001628 hash_debug_results();
1629#endif
1630
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001631#ifdef FEAT_GUI
1632 msg_didany = FALSE;
1633#endif
1634
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001635 if (v_dying <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001636 {
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001637 tabpage_T *tp;
1638 tabpage_T *next_tp;
1639 buf_T *buf;
1640 win_T *wp;
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001641 int unblock = 0;
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001642
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001643 // Trigger BufWinLeave for all windows, but only once per buffer.
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001644 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001645 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001646 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001647 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001648 {
Christian Brabandtfc682992023-09-03 20:20:52 +02001649 if (wp->w_buffer == NULL || !buf_valid(wp->w_buffer))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001650 // Autocmd must have close the buffer already, skip.
Bram Moolenaar802418d2013-01-17 14:00:11 +01001651 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001652 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001653 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001654 {
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001655 bufref_T bufref;
1656
1657 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001658 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1659 buf->b_fname, FALSE, buf);
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001660 if (bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001661 CHANGEDTICK(buf) = -1; // note we did it already
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001662
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001663 // start all over, autocommands may mess up the lists
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001664 next_tp = first_tabpage;
1665 break;
1666 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001667 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001668 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001669
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001670 // Trigger BufUnload for buffers that are loaded
Bram Moolenaar29323592016-07-24 22:04:11 +02001671 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001672 if (buf->b_ml.ml_mfp != NULL)
1673 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001674 bufref_T bufref;
1675
1676 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001677 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001678 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001679 if (!bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001680 // autocmd deleted the buffer
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001681 break;
1682 }
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001683
1684 // deathtrap() blocks autocommands, but we do want to trigger
1685 // VimLeavePre.
1686 if (is_autocmd_blocked())
1687 {
1688 unblock_autocmds();
1689 ++unblock;
1690 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001691 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001692 if (unblock)
1693 block_autocmds();
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001694 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001695
1696#ifdef FEAT_VIMINFO
Milly6eca04e2024-10-21 22:20:51 +02001697 if (
1698# ifdef EXITFREE
1699 entered_free_all_mem == FALSE &&
1700# endif
1701 *p_viminfo != NUL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001702 // Write out the registers, history, marks etc, to the viminfo file
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001703 write_viminfo(NULL, FALSE);
1704#endif
1705
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001706 if (v_dying <= 1)
Bram Moolenaarc7226682019-08-17 16:33:23 +02001707 {
1708 int unblock = 0;
1709
1710 // deathtrap() blocks autocommands, but we do want to trigger VimLeave.
1711 if (is_autocmd_blocked())
1712 {
1713 unblock_autocmds();
1714 ++unblock;
1715 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001716 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc7226682019-08-17 16:33:23 +02001717 if (unblock)
1718 block_autocmds();
1719 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001720
Bram Moolenaar05159a02005-02-26 23:04:13 +00001721#ifdef FEAT_PROFILE
1722 profile_dump();
1723#endif
1724
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001725 if (did_emsg
1726#ifdef FEAT_GUI
1727 || (gui.in_use && msg_didany && p_verbose > 0)
1728#endif
1729 )
1730 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001731 // give the user a chance to read the (error) message
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001732 no_wait_return = FALSE;
Volodymyr Kot7f0c4b42021-11-21 12:27:13 +00001733 wait_return(FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001734 }
1735
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001736 // Position the cursor again, the autocommands may have moved it
Bram Moolenaar7007e312021-03-27 12:11:33 +01001737 if (!is_not_a_term_or_gui())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001738 windgoto((int)Rows - 1, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001739
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001740#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001741 job_stop_on_exit();
1742#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001743#ifdef FEAT_LUA
1744 lua_end();
1745#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001746#ifdef FEAT_MZSCHEME
1747 mzscheme_end();
1748#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001749#ifdef FEAT_TCL
1750 tcl_end();
1751#endif
1752#ifdef FEAT_RUBY
1753 ruby_end();
1754#endif
1755#ifdef FEAT_PYTHON
1756 python_end();
1757#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001758#ifdef FEAT_PYTHON3
1759 python3_end();
1760#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001761#ifdef FEAT_PERL
1762 perl_end();
1763#endif
1764#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1765 iconv_end();
1766#endif
1767#ifdef FEAT_NETBEANS_INTG
1768 netbeans_end();
1769#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001770#ifdef FEAT_CSCOPE
1771 cs_end();
1772#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001773#ifdef FEAT_EVAL
1774 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001775 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001776#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001777#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +02001778 free_cmd_argsW();
1779#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001780
1781 mch_exit(exitval);
1782}
1783
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001784/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001785 * Get the name of the display, before gui_prepare() removes it from
1786 * argv[]. Used for the xterm-clipboard display.
1787 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001788 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001789 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001790 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001791early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001792{
Bram Moolenaar03005972008-11-20 13:12:36 +00001793#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1794 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001795 int argc = parmp->argc;
1796 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001797 int i;
1798
1799 for (i = 1; i < argc; i++)
1800 {
1801 if (STRCMP(argv[i], "--") == 0)
1802 break;
1803# ifdef FEAT_XCLIPBOARD
1804 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001805# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001806 || STRICMP(argv[i], "--display") == 0
1807# endif
1808 )
1809 {
1810 if (i == argc - 1)
1811 mainerr_arg_missing((char_u *)argv[i]);
1812 xterm_display = argv[++i];
1813 }
1814# endif
1815# ifdef FEAT_CLIENTSERVER
1816 else if (STRICMP(argv[i], "--servername") == 0)
1817 {
1818 if (i == argc - 1)
1819 mainerr_arg_missing((char_u *)argv[i]);
1820 parmp->serverName_arg = (char_u *)argv[++i];
1821 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001822 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001823 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001824 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001825 {
1826 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001827# ifdef FEAT_GUI
1828 if (strstr(argv[i], "-wait") != 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001829 // don't fork() when starting the GUI to edit files ourself
Bram Moolenaareb94e552006-03-11 21:35:11 +00001830 gui.dofork = FALSE;
1831# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001832 }
1833# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001834
Bram Moolenaar4f974752019-02-17 17:44:42 +01001835# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
1836# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001837 else if (STRICMP(argv[i], "--windowid") == 0)
1838# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001839 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001840# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001841 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001842 long_u id;
1843 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001844
1845 if (i == argc - 1)
1846 mainerr_arg_missing((char_u *)argv[i]);
1847 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001848 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001849 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001850 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001851 if (count != 1)
1852 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1853 else
Bram Moolenaar4f974752019-02-17 17:44:42 +01001854# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001855 win_socket_id = id;
1856# else
1857 gtk_socket_id = id;
1858# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001859 i++;
1860 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001861# endif
1862# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001863 else if (STRICMP(argv[i], "--echo-wid") == 0)
1864 echo_wid_arg = TRUE;
1865# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001866# ifndef FEAT_NETBEANS_INTG
1867 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001868 {
1869 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1870 mch_exit(2);
1871 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001872# endif
1873
Bram Moolenaar58d98232005-07-23 22:25:46 +00001874 }
1875#endif
1876}
1877
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001878#ifndef NO_VIM_MAIN
1879/*
1880 * Get a (optional) count for a Vim argument.
1881 */
1882 static int
1883get_number_arg(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001884 char_u *p, // pointer to argument
1885 int *idx, // index in argument, is incremented
1886 int def) // default value
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001887{
1888 if (vim_isdigit(p[*idx]))
1889 {
1890 def = atoi((char *)&(p[*idx]));
1891 while (vim_isdigit(p[*idx]))
1892 *idx = *idx + 1;
1893 }
1894 return def;
1895}
1896
1897/*
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001898 * Check for: [r][e][g][vi|vim|view][diff][ex[im]] (sort of)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001899 * If the executable name starts with "r" we disable shell commands.
1900 * If the next character is "e" we run in Easy mode.
1901 * If the next character is "g" we run the GUI version.
1902 * If the next characters are "view" we start in readonly mode.
1903 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1904 * If the next characters are "ex" we start in Ex mode. If it's followed
1905 * by "im" use improved Ex mode.
1906 */
1907 static void
1908parse_command_name(mparm_T *parmp)
1909{
1910 char_u *initstr;
1911
1912 initstr = gettail((char_u *)parmp->argv[0]);
1913
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001914#ifdef FEAT_EVAL
1915 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001916 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001917#endif
1918
1919 if (TOLOWER_ASC(initstr[0]) == 'r')
1920 {
1921 restricted = TRUE;
1922 ++initstr;
1923 }
1924
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001925 // Use evim mode for "evim" and "egvim", not for "editor".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001926 if (TOLOWER_ASC(initstr[0]) == 'e'
1927 && (TOLOWER_ASC(initstr[1]) == 'v'
1928 || TOLOWER_ASC(initstr[1]) == 'g'))
1929 {
1930#ifdef FEAT_GUI
1931 gui.starting = TRUE;
1932#endif
1933 parmp->evim_mode = TRUE;
1934 ++initstr;
1935 }
1936
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001937 // "gvim" starts the GUI. Also accept "Gvim" for MS-Windows.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001938 if (TOLOWER_ASC(initstr[0]) == 'g')
1939 {
1940 main_start_gui();
1941#ifdef FEAT_GUI
1942 ++initstr;
1943#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001944#ifdef GUI_MAY_SPAWN
1945 gui.dospawn = FALSE; // No need to spawn a new process.
1946#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001947 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001948#ifdef GUI_MAY_SPAWN
1949 else
1950 gui.dospawn = TRUE; // Not "gvim". Need to spawn gvim.exe.
1951#endif
1952
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001953
1954 if (STRNICMP(initstr, "view", 4) == 0)
1955 {
1956 readonlymode = TRUE;
1957 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001958 p_uc = 10000; // don't update very often
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001959 initstr += 4;
1960 }
1961 else if (STRNICMP(initstr, "vim", 3) == 0)
1962 initstr += 3;
1963
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001964 // Catch "[r][g]vimdiff" and "[r][g]viewdiff".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001965 if (STRICMP(initstr, "diff") == 0)
1966 {
1967#ifdef FEAT_DIFF
1968 parmp->diff_mode = TRUE;
1969#else
1970 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1971 mch_errmsg("\n");
1972 mch_exit(2);
1973#endif
1974 }
1975
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001976 // Checking for "ex" here may catch some weird names, such as "vimex" or
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001977 // "viewex", we assume the user knows that.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001978 if (STRNICMP(initstr, "ex", 2) == 0)
1979 {
1980 if (STRNICMP(initstr + 2, "im", 2) == 0)
1981 exmode_active = EXMODE_VIM;
1982 else
1983 exmode_active = EXMODE_NORMAL;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001984 change_compatible(TRUE); // set 'compatible'
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001985 }
1986}
1987
Bram Moolenaar58d98232005-07-23 22:25:46 +00001988/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001989 * Scan the command line arguments.
1990 */
1991 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001992command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001993{
1994 int argc = parmp->argc;
1995 char **argv = parmp->argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001996 int argv_idx; // index in argv[n][]
1997 int had_minmin = FALSE; // found "--" argument
1998 int want_argument; // option argument with argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001999 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00002000 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002001 long n;
2002
2003 --argc;
2004 ++argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002005 argv_idx = 1; // active option letter is argv[0][argv_idx]
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002006 while (argc > 0)
2007 {
2008 /*
2009 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
2010 */
2011 if (argv[0][0] == '+' && !had_minmin)
2012 {
2013 if (parmp->n_commands >= MAX_ARG_CMDS)
2014 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002015 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002016 if (argv[0][1] == NUL)
2017 parmp->commands[parmp->n_commands++] = (char_u *)"$";
2018 else
2019 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
2020 }
2021
2022 /*
2023 * Optional argument.
2024 */
2025 else if (argv[0][0] == '-' && !had_minmin)
2026 {
2027 want_argument = FALSE;
2028 c = argv[0][argv_idx++];
2029#ifdef VMS
2030 /*
2031 * VMS only uses upper case command lines. Interpret "-X" as "-x"
2032 * and "-/X" as "-X".
2033 */
2034 if (c == '/')
2035 {
2036 c = argv[0][argv_idx++];
2037 c = TOUPPER_ASC(c);
2038 }
2039 else
2040 c = TOLOWER_ASC(c);
2041#endif
2042 switch (c)
2043 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002044 case NUL: // "vim -" read from stdin
2045 // "ex -" silent mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002046 if (exmode_active)
2047 silent_mode = TRUE;
2048 else
2049 {
2050 if (parmp->edit_type != EDIT_NONE)
2051 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2052 parmp->edit_type = EDIT_STDIN;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002053 read_cmd_fd = 2; // read from stderr instead of stdin
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002054 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002055 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002056 break;
2057
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002058 case '-': // "--" don't take any more option arguments
2059 // "--help" give help message
2060 // "--version" give version message
2061 // "--clean" clean context
2062 // "--literal" take files literally
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002063 // "--startuptime fname" write timing info
2064 // "--log fname" start logging early
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002065 // "--nofork" don't fork
2066 // "--not-a-term" don't warn for not a term
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002067 // "--gui-dialog-file fname" write dialog text
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002068 // "--ttyfail" exit if not a term
2069 // "--noplugin[s]" skip plugins
2070 // "--cmd <cmd>" execute cmd before vimrc
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002071 if (STRICMP(argv[0] + argv_idx, "help") == 0)
2072 usage();
2073 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
2074 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002075 Columns = 80; // need to init Columns
2076 info_message = TRUE; // use mch_msg(), not mch_errmsg()
K.Takata33b25d12022-01-13 16:06:45 +00002077#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar3b678042021-02-11 19:08:05 +01002078 gui.starting = FALSE; // not starting GUI, will exit
Bram Moolenaar0bcadf12021-02-11 19:18:58 +01002079#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002080 list_version();
2081 msg_putchar('\n');
2082 msg_didout = FALSE;
2083 mch_exit(0);
2084 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002085 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
2086 {
2087 parmp->use_vimrc = (char_u *)"DEFAULTS";
Bram Moolenaar62dd4522018-03-14 21:20:02 +01002088#ifdef FEAT_GUI
2089 use_gvimrc = (char_u *)"NONE";
2090#endif
Bram Moolenaar07268702018-03-01 21:57:32 +01002091 parmp->clean = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002092 set_option_value_give_err((char_u *)"vif",
2093 0L, (char_u *)"NONE", 0);
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002094 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002095 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
2096 {
Bram Moolenaar53076832015-12-31 19:53:21 +01002097#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002098 parmp->literal = TRUE;
2099#endif
2100 }
2101 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
2102 {
2103#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002104 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002105#endif
2106 }
2107 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
2108 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002109 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
2110 parmp->not_a_term = TRUE;
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002111 else if (STRNICMP(argv[0] + argv_idx, "gui-dialog-file", 15)
2112 == 0)
2113 {
2114 want_argument = TRUE;
2115 argv_idx += 15;
2116 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002117 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
2118 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002119 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
2120 {
2121 want_argument = TRUE;
2122 argv_idx += 3;
2123 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002124 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
2125 {
2126 want_argument = TRUE;
2127 argv_idx += 11;
2128 }
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002129 else if (STRNICMP(argv[0] + argv_idx, "log", 3) == 0)
2130 {
2131 want_argument = TRUE;
2132 argv_idx += 3;
2133 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002134#ifdef FEAT_CLIENTSERVER
2135 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002136 ; // already processed -- no arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002137 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
2138 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
2139 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002140 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002141 if (argc > 1)
2142 {
2143 --argc;
2144 ++argv;
2145 }
2146 }
2147#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002148#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002149# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002150 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002151# else
2152 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
2153# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002154 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002155 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002156 if (argc > 1)
2157 {
2158 --argc;
2159 ++argv;
2160 }
2161 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00002162#endif
2163#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002164 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
2165 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002166 // already processed, skip
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002167 }
2168#endif
2169 else
2170 {
2171 if (argv[0][argv_idx])
2172 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2173 had_minmin = TRUE;
2174 }
2175 if (!want_argument)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002176 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002177 break;
2178
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002179 case 'A': // "-A" start in Arabic mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002180#ifdef FEAT_ARABIC
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002181 set_option_value_give_err((char_u *)"arabic", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002182#else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002183 mch_errmsg(_(e_arabic_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002184 mch_exit(2);
2185#endif
2186 break;
2187
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002188 case 'b': // "-b" binary mode
2189 // Needs to be effective before expanding file names, because
2190 // for Win32 this makes us edit a shortcut file itself,
2191 // instead of the file it links to.
Bram Moolenaar231334e2005-07-25 20:46:57 +00002192 set_options_bin(curbuf->b_p_bin, 1, 0);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002193 curbuf->b_p_bin = 1; // binary file I/O
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002194 break;
2195
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002196 case 'C': // "-C" Compatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002197 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02002198 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002199 break;
2200
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002201 case 'e': // "-e" Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002202 exmode_active = EXMODE_NORMAL;
2203 break;
2204
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002205 case 'E': // "-E" Improved Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002206 exmode_active = EXMODE_VIM;
2207 break;
2208
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002209 case 'f': // "-f" GUI: run in foreground. Amiga: open
2210 // window directly, not with newcli
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002211#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002212 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002213#endif
2214 break;
2215
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002216 case 'g': // "-g" start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002217 main_start_gui();
2218 break;
2219
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002220 case 'F': // "-F" was for Farsi mode
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002221 mch_errmsg(_(e_farsi_support_has_been_removed));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002222 mch_exit(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002223 break;
2224
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002225 case '?': // "-?" give help message (for MS-Windows)
2226 case 'h': // "-h" give help message
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002227#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002228 // Tell usage() to exit for "gvim".
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002229 gui.starting = FALSE;
2230#endif
2231 usage();
2232 break;
2233
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002234 case 'H': // "-H" start in Hebrew mode: rl + hkmap set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002235#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002236 p_hkmap = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002237 set_option_value_give_err((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002238#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002239 mch_errmsg(_(e_hebrew_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002240 mch_exit(2);
2241#endif
2242 break;
2243
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002244 case 'l': // "-l" lisp mode, 'lisp' and 'showmatch' on
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002245 set_option_value_give_err((char_u *)"lisp", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002246 p_sm = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002247 break;
2248
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002249 case 'M': // "-M" no changes or writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002250 reset_modifiable();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002251 // FALLTHROUGH
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002252
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002253 case 'm': // "-m" no writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002254 p_write = FALSE;
2255 break;
2256
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002257 case 'y': // "-y" easy mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002258#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002259 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002260#endif
2261 parmp->evim_mode = TRUE;
2262 break;
2263
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002264 case 'N': // "-N" Nocompatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002265 change_compatible(FALSE);
2266 break;
2267
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002268 case 'n': // "-n" no swap file
Bram Moolenaar67c53842010-05-22 18:28:27 +02002269#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002270 // checking for "-nb", netbeans parameters
Bram Moolenaar67c53842010-05-22 18:28:27 +02002271 if (argv[0][argv_idx] == 'b')
2272 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002273 netbeansArg = argv[0];
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002274 argv_idx = -1; // skip to next argument
Bram Moolenaar67c53842010-05-22 18:28:27 +02002275 }
2276 else
2277#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002278 parmp->no_swap_file = TRUE;
2279 break;
2280
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002281 case 'p': // "-p[N]" open N tab pages
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002282#ifdef TARGET_API_MAC_OSX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002283 // For some reason on MacOS X, an argument like:
2284 // -psn_0_10223617 is passed in when invoke from Finder
2285 // or with the 'open' command
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002286 if (argv[0][argv_idx] == 's')
2287 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002288 argv_idx = -1; // bypass full -psn
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002289 main_start_gui();
2290 break;
2291 }
2292#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002293 // default is 0: open window for each file
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002294 parmp->window_count = get_number_arg((char_u *)argv[0],
2295 &argv_idx, 0);
2296 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002297 break;
2298
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002299 case 'o': // "-o[N]" open N horizontal split windows
2300 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002301 parmp->window_count = get_number_arg((char_u *)argv[0],
2302 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002303 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002304 break;
2305
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002306 case 'O': // "-O[N]" open N vertical split windows
2307 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002308 parmp->window_count = get_number_arg((char_u *)argv[0],
2309 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002310 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002311 break;
2312
2313#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002314 case 'q': // "-q" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002315 if (parmp->edit_type != EDIT_NONE)
2316 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2317 parmp->edit_type = EDIT_QF;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002318 if (argv[0][argv_idx]) // "-q{errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002319 {
2320 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2321 argv_idx = -1;
2322 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002323 else if (argc > 1) // "-q {errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002324 want_argument = TRUE;
2325 break;
2326#endif
2327
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002328 case 'R': // "-R" readonly mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002329 readonlymode = TRUE;
2330 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002331 p_uc = 10000; // don't update very often
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002332 break;
2333
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002334 case 'r': // "-r" recovery mode
2335 case 'L': // "-L" recovery mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002336 recoverymode = 1;
2337 break;
2338
2339 case 's':
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002340 if (exmode_active) // "-s" silent (batch) mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002341 silent_mode = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002342 else // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002343 want_argument = TRUE;
2344 break;
2345
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002346 case 't': // "-t {tag}" or "-t{tag}" jump to tag
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002347 if (parmp->edit_type != EDIT_NONE)
2348 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2349 parmp->edit_type = EDIT_TAG;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002350 if (argv[0][argv_idx]) // "-t{tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002351 {
2352 parmp->tagname = (char_u *)argv[0] + argv_idx;
2353 argv_idx = -1;
2354 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002355 else // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002356 want_argument = TRUE;
2357 break;
2358
2359#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002360 case 'D': // "-D" Debugging
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002361 parmp->use_debug_break_level = 9999;
2362 break;
2363#endif
2364#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002365 case 'd': // "-d" 'diff'
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002366# ifdef AMIGA
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002367 // check for "-dev {device}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002368 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2369 want_argument = TRUE;
2370 else
2371# endif
2372 parmp->diff_mode = TRUE;
2373 break;
2374#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002375 case 'V': // "-V{N}" Verbose level
2376 // default is 10: a little bit verbose
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002377 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2378 if (argv[0][argv_idx] != NUL)
2379 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002380 set_option_value_give_err((char_u *)"verbosefile",
2381 0L, (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002382 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002383 }
2384 break;
2385
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002386 case 'v': // "-v" Vi-mode (as if called "vi")
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002387 exmode_active = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002388#if defined(FEAT_GUI) && !defined(VIMDLL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002389 gui.starting = FALSE; // don't start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002390#endif
2391 break;
2392
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002393 case 'w': // "-w{number}" set window height
2394 // "-w {scriptout}" write to script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002395 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2396 {
2397 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002398 set_option_value_give_err((char_u *)"window", n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002399 break;
2400 }
2401 want_argument = TRUE;
2402 break;
2403
2404#ifdef FEAT_CRYPT
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002405 case 'x': // "-x" encrypted reading/writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002406 parmp->ask_for_key = TRUE;
2407 break;
2408#endif
2409
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002410 case 'X': // "-X" don't connect to X server
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002411#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2412 x_no_connect = TRUE;
2413#endif
2414 break;
2415
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002416 case 'Z': // "-Z" restricted mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002417 restricted = TRUE;
2418 break;
2419
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002420 case 'c': // "-c{command}" or "-c {command}" execute
2421 // command
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002422 if (argv[0][argv_idx] != NUL)
2423 {
2424 if (parmp->n_commands >= MAX_ARG_CMDS)
2425 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002426 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2427 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002428 argv_idx = -1;
2429 break;
2430 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002431 // FALLTHROUGH
2432 case 'S': // "-S {file}" execute Vim script
2433 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002434#ifndef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002435 case 'd': // "-d {device}" device (for Amiga)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002436#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002437 case 'T': // "-T {terminal}" terminal name
2438 case 'u': // "-u {vimrc}" vim inits file
2439 case 'U': // "-U {gvimrc}" gvim inits file
2440 case 'W': // "-W {scriptout}" overwrite
Bram Moolenaar4f974752019-02-17 17:44:42 +01002441#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002442 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002443#endif
2444 want_argument = TRUE;
2445 break;
2446
2447 default:
2448 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2449 }
2450
2451 /*
2452 * Handle option arguments with argument.
2453 */
2454 if (want_argument)
2455 {
2456 /*
2457 * Check for garbage immediately after the option letter.
2458 */
2459 if (argv[0][argv_idx] != NUL)
2460 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2461
2462 --argc;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002463 if (argc < 1 && c != 'S') // -S has an optional argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002464 mainerr_arg_missing((char_u *)argv[0]);
2465 ++argv;
2466 argv_idx = -1;
2467
2468 switch (c)
2469 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002470 case 'c': // "-c {command}" execute command
2471 case 'S': // "-S {file}" execute Vim script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002472 if (parmp->n_commands >= MAX_ARG_CMDS)
2473 mainerr(ME_EXTRA_CMD, NULL);
2474 if (c == 'S')
2475 {
2476 char *a;
2477
2478 if (argc < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002479 // "-S" without argument: use default session file
2480 // name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002481 a = SESSION_FILE;
2482 else if (argv[0][0] == '-')
2483 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002484 // "-S" followed by another option: use default
2485 // session file name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002486 a = SESSION_FILE;
2487 ++argc;
2488 --argv;
2489 }
2490 else
2491 a = argv[0];
Bram Moolenaar964b3742019-05-24 18:54:09 +02002492 p = alloc(STRLEN(a) + 4);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002493 if (p == NULL)
2494 mch_exit(2);
2495 sprintf((char *)p, "so %s", a);
2496 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2497 parmp->commands[parmp->n_commands++] = p;
2498 }
2499 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002500 parmp->commands[parmp->n_commands++] =
2501 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002502 break;
2503
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002504 case '-':
2505 if (argv[-1][2] == 'c')
2506 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002507 // "--cmd {command}" execute command
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002508 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2509 mainerr(ME_EXTRA_CMD, NULL);
2510 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002511 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002512 }
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002513 // --gui-dialog-file fname
2514 if (argv[-1][2] == 'g')
2515 {
2516 // without GUI ignore the argument
2517#ifdef FEAT_GUI
2518 parmp->gui_dialog_file = (char_u *)argv[0];
2519#endif
2520 }
2521
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002522 // "--startuptime <file>" already handled
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002523 // "--log <file>" already handled
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002524 break;
2525
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002526 // case 'd': -d {device} is handled in mch_check_win() for the
2527 // Amiga
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002528
2529#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002530 case 'q': // "-q {errorfile}" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002531 parmp->use_ef = (char_u *)argv[0];
2532 break;
2533#endif
2534
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002535 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002536 set_option_value_give_err((char_u *)"vif",
2537 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002538 break;
2539
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002540 case 's': // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002541 if (scriptin[0] != NULL)
2542 {
2543scripterror:
2544 mch_errmsg(_("Attempt to open script file again: \""));
2545 mch_errmsg(argv[-1]);
2546 mch_errmsg(" ");
2547 mch_errmsg(argv[0]);
2548 mch_errmsg("\"\n");
2549 mch_exit(2);
2550 }
2551 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2552 {
2553 mch_errmsg(_("Cannot open for reading: \""));
2554 mch_errmsg(argv[0]);
2555 mch_errmsg("\"\n");
2556 mch_exit(2);
2557 }
2558 if (save_typebuf() == FAIL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002559 mch_exit(2); // out of memory
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002560 break;
2561
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002562 case 't': // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002563 parmp->tagname = (char_u *)argv[0];
2564 break;
2565
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002566 case 'T': // "-T {terminal}" terminal name
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002567 /*
2568 * The -T term argument is always available and when
2569 * HAVE_TERMLIB is supported it overrides the environment
2570 * variable TERM.
2571 */
2572#ifdef FEAT_GUI
2573 if (term_is_gui((char_u *)argv[0]))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002574 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002575 else
2576#endif
2577 parmp->term = (char_u *)argv[0];
2578 break;
2579
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002580 case 'u': // "-u {vimrc}" vim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002581 parmp->use_vimrc = (char_u *)argv[0];
2582 break;
2583
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002584 case 'U': // "-U {gvimrc}" gvim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002585#ifdef FEAT_GUI
2586 use_gvimrc = (char_u *)argv[0];
2587#endif
2588 break;
2589
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002590 case 'w': // "-w {nr}" 'window' value
2591 // "-w {scriptout}" append to script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002592 if (vim_isdigit(*((char_u *)argv[0])))
2593 {
2594 argv_idx = 0;
2595 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002596 set_option_value_give_err((char_u *)"window",
2597 n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002598 argv_idx = -1;
2599 break;
2600 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002601 // FALLTHROUGH
2602 case 'W': // "-W {scriptout}" overwrite script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002603 if (scriptout != NULL)
2604 goto scripterror;
2605 if ((scriptout = mch_fopen(argv[0],
2606 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2607 {
2608 mch_errmsg(_("Cannot open for script output: \""));
2609 mch_errmsg(argv[0]);
2610 mch_errmsg("\"\n");
2611 mch_exit(2);
2612 }
2613 break;
2614
Bram Moolenaar4f974752019-02-17 17:44:42 +01002615#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002616 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002617 gui_mch_set_parent(argv[0]);
2618 break;
2619#endif
2620 }
2621 }
2622 }
2623
2624 /*
2625 * File name argument.
2626 */
2627 else
2628 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002629 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002630
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002631 // Check for only one type of editing.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002632 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2633 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2634 parmp->edit_type = EDIT_FILE;
2635
2636#ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002637 // Remember if the argument was a full path before changing
2638 // slashes to backslashes.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002639 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2640 parmp->full_path = TRUE;
2641#endif
2642
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002643 // Add the file to the global argument list.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002644 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2645 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2646 mch_exit(2);
2647#ifdef FEAT_DIFF
2648 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2649 && !mch_isdir(alist_name(&GARGLIST[0])))
2650 {
2651 char_u *r;
2652
2653 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2654 if (r != NULL)
2655 {
2656 vim_free(p);
2657 p = r;
2658 }
2659 }
2660#endif
K.Takatab247e062022-02-07 10:45:23 +00002661#ifdef __CYGWIN__
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002662 /*
2663 * If vim is invoked by non-Cygwin tools, convert away any
2664 * DOS paths, so things like .swp files are created correctly.
2665 * Look for evidence of non-Cygwin paths before we bother.
2666 * This is only for when using the Unix files.
2667 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002668 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002669 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002670 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002671
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002672# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002673 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002674# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002675 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002676# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002677 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002678 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002679 if (p == NULL)
2680 mch_exit(2);
2681 }
2682#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002683
2684#ifdef USE_FNAME_CASE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002685 // Make the case of the file name match the actual file.
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002686 fname_case(p, 0);
2687#endif
2688
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002689 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002690#ifdef EXPAND_FILENAMES
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002691 parmp->literal ? 2 : 0 // add buffer nr after exp.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002692#else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002693 2 // add buffer number now and use curbuf
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002694#endif
2695 );
2696
Bram Moolenaar4f974752019-02-17 17:44:42 +01002697#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002698 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002699 // Remember this argument has been added to the argument list.
2700 // Needed when 'encoding' is changed.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002701 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002702# ifdef FEAT_DIFF
2703 parmp->diff_mode
2704# else
2705 FALSE
2706# endif
2707 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002708 }
2709#endif
2710 }
2711
2712 /*
2713 * If there are no more letters after the current "-", go to next
2714 * argument. argv_idx is set to -1 when the current argument is to be
2715 * skipped.
2716 */
2717 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2718 {
2719 --argc;
2720 ++argv;
2721 argv_idx = 1;
2722 }
2723 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002724
2725#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002726 // If there is a "+123" or "-c" command, set v:swapcommand to the first
2727 // one.
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002728 if (parmp->n_commands > 0)
2729 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002730 p = alloc(STRLEN(parmp->commands[0]) + 3);
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002731 if (p != NULL)
2732 {
2733 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2734 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2735 vim_free(p);
2736 }
2737 }
2738#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002739}
2740
2741/*
2742 * Print a warning if stdout is not a terminal.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +01002743 * When starting in Ex mode and commands come from a file, set silent_mode.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002744 */
2745 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002746check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002747{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002748 int input_isatty; // is active input a terminal?
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002749
2750 input_isatty = mch_input_isatty();
2751 if (exmode_active)
2752 {
2753 if (!input_isatty)
2754 silent_mode = TRUE;
2755 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002756 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002757#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002758 // don't want the delay when started from the desktop
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002759 && !gui.starting
2760#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002761 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002762 {
2763#ifdef NBDEBUG
2764 /*
2765 * This shouldn't be necessary. But if I run netbeans with the log
2766 * output coming to the console and XOpenDisplay fails, I get vim
2767 * trying to start with input/output to my console tty. This fills my
2768 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002769 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002770 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002771 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002772 {
2773 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2774 exit(1);
2775 }
2776#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002777#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2778 if (
2779# ifdef VIMDLL
2780 !gui.starting &&
2781# endif
2782 is_cygpty_used())
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002783 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002784# if defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
Bram Moolenaar2a027452017-09-26 19:10:37 +02002785 && defined(FEAT_GETTEXT)
2786 char *s, *tofree = NULL;
2787
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002788 // Set the encoding of the error message based on $LC_ALL or
2789 // other environment variables instead of 'encoding'.
2790 // Note that the message is shown on a Cygwin terminal (e.g.
2791 // mintty) which encoding is based on $LC_ALL or etc., not the
2792 // current codepage used by normal Win32 console programs.
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002793 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002794 if (s == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002795 s = "utf-8"; // Use "utf-8" by default.
Bram Moolenaar2a027452017-09-26 19:10:37 +02002796 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2797 vim_free(tofree);
2798# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002799 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2800 exit(1);
2801 }
2802#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002803 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002804 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2805 if (!input_isatty)
2806 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2807 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002808 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2809 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002810 if (scriptin[0] == NULL)
Bram Moolenaareda1da02019-11-17 17:06:33 +01002811 ui_delay(2005L, TRUE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002812 TIME_MSG("Warning delay");
2813 }
2814}
2815
2816/*
2817 * Read text from stdin.
2818 */
2819 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002820read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002821{
2822 int i;
2823
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002824 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002825 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002826
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002827 no_wait_return = TRUE;
2828 i = msg_didany;
2829 set_buflisted(TRUE);
Bram Moolenaar204ade62020-10-11 14:58:46 +02002830
2831 // Create memfile and read from stdin.
Bram Moolenaar204ade62020-10-11 14:58:46 +02002832 (void)open_buffer(TRUE, NULL, 0);
2833
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002834 no_wait_return = FALSE;
2835 msg_didany = i;
2836 TIME_MSG("reading stdin");
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002837
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002838 check_swap_exists_action();
Bram Moolenaar8e1cbb52020-12-08 19:36:21 +01002839
2840#if !(defined(AMIGA) || defined(MACOS_X))
2841 // Dup stdin from stderr to read commands from, so that shell commands
2842 // work.
2843 // TODO: why is this needed, even though readfile() has done this?
2844 close(0);
2845 vim_ignored = dup(2);
2846#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002847}
2848
2849/*
2850 * Create the requested number of windows and edit buffers in them.
2851 * Also does recovery if "recoverymode" set.
2852 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002853 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002854create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002855{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002856 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002857 int done = 0;
2858
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002859 /*
2860 * Create the number of windows that was requested.
2861 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002862 if (parmp->window_count == -1) // was not set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002863 parmp->window_count = 1;
2864 if (parmp->window_count == 0)
2865 parmp->window_count = GARGCOUNT;
2866 if (parmp->window_count > 1)
2867 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002868 // Don't change the windows if there was a command in .vimrc that
2869 // already split some windows
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002870 if (parmp->window_layout == 0)
2871 parmp->window_layout = WIN_HOR;
2872 if (parmp->window_layout == WIN_TABS)
2873 {
2874 parmp->window_count = make_tabpages(parmp->window_count);
2875 TIME_MSG("making tab pages");
2876 }
2877 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002878 {
2879 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002880 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002881 TIME_MSG("making windows");
2882 }
2883 else
2884 parmp->window_count = win_count();
2885 }
2886 else
2887 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002888
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002889 if (recoverymode) // do recover
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002890 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002891 msg_scroll = TRUE; // scroll message up
Bram Moolenaar99499b12019-05-23 21:35:48 +02002892 ml_recover(TRUE);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002893 if (curbuf->b_ml.ml_mfp == NULL) // failed
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002894 getout(1);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002895 do_modelines(0); // do modelines
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002896 }
2897 else
2898 {
2899 /*
2900 * Open a buffer for windows that don't have one yet.
2901 * Commands in the .vimrc might have loaded a file or split the window.
2902 * Watch out for autocommands that delete a window.
2903 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002904 /*
2905 * Don't execute Win/Buf Enter/Leave autocommands here
2906 */
2907 ++autocmd_no_enter;
2908 ++autocmd_no_leave;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002909 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002910 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002911 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002912 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002913 {
2914 if (parmp->window_layout == WIN_TABS)
2915 goto_tabpage(1);
2916 else
2917 curwin = firstwin;
2918 }
2919 else if (parmp->window_layout == WIN_TABS)
2920 {
2921 if (curtab->tp_next == NULL)
2922 break;
2923 goto_tabpage(0);
2924 }
2925 else
2926 {
2927 if (curwin->w_next == NULL)
2928 break;
2929 curwin = curwin->w_next;
2930 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002931 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002932 curbuf = curwin->w_buffer;
2933 if (curbuf->b_ml.ml_mfp == NULL)
2934 {
2935#ifdef FEAT_FOLDING
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002936 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002937 if (p_fdls >= 0)
2938 curwin->w_p_fdl = p_fdls;
2939#endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002940 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002941 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002942
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002943 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002944
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002945 // create memfile, read file
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002946 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002947
Bram Moolenaar84212822006-11-07 21:59:47 +00002948 if (swap_exists_action == SEA_QUIT)
2949 {
2950 if (got_int || only_one_window())
2951 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002952 // abort selected or quit and only one window
2953 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00002954 getout(1);
2955 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002956 // We can't close the window, it would disturb what
2957 // happens next. Clear the file name and set the arg
2958 // index to -1 to delete it later.
Bram Moolenaar84212822006-11-07 21:59:47 +00002959 setfname(curbuf, NULL, NULL, FALSE);
2960 curwin->w_arg_idx = -1;
2961 swap_exists_action = SEA_NONE;
2962 }
2963 else
2964 handle_swap_exists(NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002965 dorewind = TRUE; // start again
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002966 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002967 ui_breakcheck();
2968 if (got_int)
2969 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002970 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002971 break;
2972 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002973 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002974 if (parmp->window_layout == WIN_TABS)
2975 goto_tabpage(1);
2976 else
2977 curwin = firstwin;
2978 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002979 --autocmd_no_enter;
2980 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002981 }
2982}
2983
Bram Moolenaar9810cfb2019-12-11 21:23:00 +01002984/*
2985 * If opened more than one window, start editing files in the other
2986 * windows. make_windows() has already opened the windows.
2987 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002988 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002989edit_buffers(
2990 mparm_T *parmp,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002991 char_u *cwd) // current working dir
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002992{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002993 int arg_idx; // index in argument list
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002994 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002995 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002996 win_T *win;
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002997 char_u *p_shm_save = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002998
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002999 /*
3000 * Don't execute Win/Buf Enter/Leave autocommands here
3001 */
3002 ++autocmd_no_enter;
3003 ++autocmd_no_leave;
Bram Moolenaar84212822006-11-07 21:59:47 +00003004
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003005 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00003006 if (curwin->w_arg_idx == -1)
3007 {
3008 win_close(curwin, TRUE);
3009 advance = FALSE;
3010 }
3011
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003012 arg_idx = 1;
3013 for (i = 1; i < parmp->window_count; ++i)
3014 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02003015 if (cwd != NULL)
3016 mch_chdir((char *)cwd);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003017 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00003018 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003019 {
Bram Moolenaar84212822006-11-07 21:59:47 +00003020 ++arg_idx;
3021 win_close(curwin, TRUE);
3022 advance = FALSE;
3023 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003024 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003025
Bram Moolenaar84212822006-11-07 21:59:47 +00003026 if (advance)
3027 {
3028 if (parmp->window_layout == WIN_TABS)
3029 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003030 if (curtab->tp_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00003031 break;
3032 goto_tabpage(0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003033 // Temporarily reset 'shm' option to not print fileinfo when
3034 // loading the other buffers. This would overwrite the already
3035 // existing fileinfo for the first tab.
3036 if (i == 1)
3037 {
3038 char buf[100];
3039
3040 p_shm_save = vim_strsave(p_shm);
3041 vim_snprintf(buf, 100, "F%s", p_shm);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003042 set_option_value_give_err((char_u *)"shm",
3043 0L, (char_u *)buf, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003044 }
Bram Moolenaar84212822006-11-07 21:59:47 +00003045 }
3046 else
3047 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003048 if (curwin->w_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00003049 break;
3050 win_enter(curwin->w_next, FALSE);
3051 }
3052 }
3053 advance = TRUE;
3054
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003055 // Only open the file if there is no file in this window yet (that can
3056 // happen when .vimrc contains ":sall").
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003057 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
3058 {
3059 curwin->w_arg_idx = arg_idx;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003060 // Edit file from arg list, if there is one. When "Quit" selected
3061 // at the ATTENTION prompt close the window.
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003062 swap_exists_did_quit = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003063 (void)do_ecmd(0, arg_idx < GARGCOUNT
3064 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003065 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003066 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00003067 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003068 // abort or quit selected
Bram Moolenaar84212822006-11-07 21:59:47 +00003069 if (got_int || only_one_window())
3070 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003071 // abort selected and only one window
3072 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00003073 getout(1);
3074 }
3075 win_close(curwin, TRUE);
3076 advance = FALSE;
3077 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003078 if (arg_idx == GARGCOUNT - 1)
3079 arg_had_last = TRUE;
3080 ++arg_idx;
3081 }
3082 ui_breakcheck();
3083 if (got_int)
3084 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003085 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003086 break;
3087 }
3088 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003089
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003090 if (p_shm_save != NULL)
3091 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003092 set_option_value_give_err((char_u *)"shm", 0L, p_shm_save, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003093 vim_free(p_shm_save);
3094 }
3095
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003096 if (parmp->window_layout == WIN_TABS)
3097 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003098 --autocmd_no_enter;
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003099
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003100 // make the first window the current window
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003101 win = firstwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003102#if defined(FEAT_QUICKFIX)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003103 // Avoid making a preview window the current window.
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003104 while (win->w_p_pvw)
3105 {
3106 win = win->w_next;
3107 if (win == NULL)
3108 {
3109 win = firstwin;
3110 break;
3111 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003112 }
3113#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003114 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003115
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003116 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003117 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003118 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003119 win_equal(curwin, FALSE, 'b'); // adjust heights
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003120}
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003121
3122/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003123 * Execute the commands from --cmd arguments "cmds[cnt]".
3124 */
3125 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003126exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003127{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003128 char_u **cmds = parmp->pre_commands;
3129 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003130 int i;
ichizok7e5fe382023-04-15 13:17:50 +01003131 ESTACK_CHECK_DECLARATION;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003132
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003133 if (cnt <= 0)
3134 return;
3135
3136 curwin->w_cursor.lnum = 0; // just in case..
3137 estack_push(ETYPE_ARGS, (char_u *)_("pre-vimrc command line"), 0);
ichizok7e5fe382023-04-15 13:17:50 +01003138 ESTACK_CHECK_SETUP;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003139# ifdef FEAT_EVAL
ichizok7e5fe382023-04-15 13:17:50 +01003140 current_sctx.sc_sid = SID_CMDARG;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003141# endif
ichizok7e5fe382023-04-15 13:17:50 +01003142 for (i = 0; i < cnt; ++i)
3143 do_cmdline_cmd(cmds[i]);
3144 ESTACK_CHECK_NOW;
3145 estack_pop();
Bram Moolenaar58d98232005-07-23 22:25:46 +00003146# ifdef FEAT_EVAL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003147 current_sctx.sc_sid = 0;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003148# endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003149 TIME_MSG("--cmd commands");
Bram Moolenaar58d98232005-07-23 22:25:46 +00003150}
3151
3152/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003153 * Execute "+", "-c" and "-S" arguments.
3154 */
3155 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003156exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003157{
3158 int i;
ichizok7e5fe382023-04-15 13:17:50 +01003159 ESTACK_CHECK_DECLARATION;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003160
3161 /*
3162 * We start commands on line 0, make "vim +/pat file" match a
3163 * pattern on line 1. But don't move the cursor when an autocommand
3164 * with g`" was used.
3165 */
3166 msg_scroll = TRUE;
3167 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
3168 curwin->w_cursor.lnum = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003169 estack_push(ETYPE_ARGS, (char_u *)"command line", 0);
ichizok7e5fe382023-04-15 13:17:50 +01003170 ESTACK_CHECK_SETUP;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003171#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003172 current_sctx.sc_sid = SID_CARG;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003173 current_sctx.sc_seq = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003174#endif
3175 for (i = 0; i < parmp->n_commands; ++i)
3176 {
3177 do_cmdline_cmd(parmp->commands[i]);
3178 if (parmp->cmds_tofree[i])
3179 vim_free(parmp->commands[i]);
3180 }
ichizok7e5fe382023-04-15 13:17:50 +01003181 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003182 estack_pop();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003183#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003184 current_sctx.sc_sid = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003185#endif
3186 if (curwin->w_cursor.lnum == 0)
3187 curwin->w_cursor.lnum = 1;
3188
3189 if (!exmode_active)
3190 msg_scroll = FALSE;
3191
3192#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003193 // When started with "-q errorfile" jump to first error again.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003194 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003195 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003196#endif
3197 TIME_MSG("executing command arguments");
3198}
3199
3200/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003201 * Source startup scripts.
3202 */
3203 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003204source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003205{
3206 int i;
3207
3208 /*
3209 * For "evim" source evim.vim first of all, so that the user can overrule
3210 * any things he doesn't like.
3211 */
3212 if (parmp->evim_mode)
3213 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003215 TIME_MSG("source evim file");
3216 }
3217
3218 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003219 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00003220 * nothing else.
3221 */
3222 if (parmp->use_vimrc != NULL)
3223 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003224 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003225 {
3226 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE, NULL)
3227 != OK)
Bram Moolenaar50809a42023-05-20 16:39:07 +01003228 emsg(_(e_failed_to_source_defaults));
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003229 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003230 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003231 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003232 {
3233#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003234 if (use_gvimrc == NULL) // don't load gvimrc either
Bram Moolenaar58d98232005-07-23 22:25:46 +00003235 use_gvimrc = parmp->use_vimrc;
3236#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003237 }
3238 else
3239 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00003241 semsg(_(e_cannot_read_from_str_2), parmp->use_vimrc);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003242 }
3243 }
3244 else if (!silent_mode)
3245 {
3246#ifdef AMIGA
3247 struct Process *proc = (struct Process *)FindTask(0L);
3248 APTR save_winptr = proc->pr_WindowPtr;
3249
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003250 // Avoid a requester here for a volume that doesn't exist.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003251 proc->pr_WindowPtr = (APTR)-1L;
3252#endif
3253
3254 /*
3255 * Get system wide defaults, if the file name is defined.
3256 */
3257#ifdef SYS_VIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003259#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003260#ifdef MACOS_X
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE,
3262 DOSO_NONE, NULL);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003263#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003264
3265 /*
3266 * Try to read initialization commands from the following places:
3267 * - environment variable VIMINIT
3268 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3269 * - second user vimrc file ($VIM/.vimrc for Dos)
3270 * - environment variable EXINIT
3271 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3272 * - second user exrc file ($VIM/.exrc for Dos)
3273 * The first that exists is used, the rest is ignored.
3274 */
3275 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3276 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277 if (do_source((char_u *)USR_VIMRC_FILE, TRUE,
3278 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003279#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003280 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003282#endif
Luca Saccarolac9df1fb2024-04-14 22:53:22 +02003283#ifdef XDG_VIMRC_FILE
3284 && do_source((char_u *)XDG_VIMRC_FILE, TRUE,
3285 DOSO_VIMRC, NULL) == FAIL
3286#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003287#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003288 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003290#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003291#ifdef USR_VIMRC_FILE4
3292 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003294#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003295 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 && do_source((char_u *)USR_EXRC_FILE, FALSE,
3297 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003298#ifdef USR_EXRC_FILE2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 && do_source((char_u *)USR_EXRC_FILE2, FALSE,
3300 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003301#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003302 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003303 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003304 // When no .vimrc file was found: source defaults.vim.
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003305 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE,
3306 NULL) == FAIL)
Bram Moolenaar50809a42023-05-20 16:39:07 +01003307 emsg(_(e_failed_to_source_defaults));
Bram Moolenaar58d98232005-07-23 22:25:46 +00003308 }
3309 }
3310
3311 /*
3312 * Read initialization commands from ".vimrc" or ".exrc" in current
3313 * directory. This is only done if the 'exrc' option is set.
3314 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003315 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003316 * option has been reset in environment of global ".exrc" or ".vimrc".
3317 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3318 * SYS_VIMRC_FILE.
3319 */
3320 if (p_exrc)
3321 {
3322#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003323 // If ".vimrc" file is not owned by user, set 'secure' mode.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003324 if (!file_owned(VIMRC_FILE))
3325#endif
3326 secure = p_secure;
3327
3328 i = FAIL;
3329 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003330 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003331#ifdef USR_VIMRC_FILE2
3332 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003333 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003334#endif
3335#ifdef USR_VIMRC_FILE3
3336 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003337 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003338#endif
3339#ifdef SYS_VIMRC_FILE
3340 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003341 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003342#endif
3343 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003345
3346 if (i == FAIL)
3347 {
3348#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003349 // if ".exrc" is not owned by user set 'secure' mode
Bram Moolenaar58d98232005-07-23 22:25:46 +00003350 if (!file_owned(EXRC_FILE))
3351 secure = p_secure;
3352 else
3353 secure = 0;
3354#endif
3355 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003356 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003357#ifdef USR_EXRC_FILE2
3358 && fullpathcmp((char_u *)USR_EXRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003359 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003360#endif
3361 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 (void)do_source((char_u *)EXRC_FILE, FALSE,
3363 DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003364 }
3365 }
3366 if (secure == 2)
3367 need_wait_return = TRUE;
3368 secure = 0;
3369#ifdef AMIGA
3370 proc->pr_WindowPtr = save_winptr;
3371#endif
3372 }
3373 TIME_MSG("sourcing vimrc file(s)");
3374}
3375
3376/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003377 * Setup to start using the GUI. Exit with an error when not available.
3378 */
3379 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003380main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003381{
3382#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003383 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003384#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003385 mch_errmsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003386 mch_errmsg("\n");
3387 mch_exit(2);
3388#endif
3389}
3390
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003391#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003392
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003393/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 * Get an environment variable and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003395 * Returns FAIL if the environment variable was not executed, OK otherwise.
3396 */
3397 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003398process_env(
3399 char_u *env,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003400 int is_viminit) // when TRUE, called for VIMINIT
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003401{
3402 char_u *initstr;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003403 sctx_T save_current_sctx;
ichizok7e5fe382023-04-15 13:17:50 +01003404 ESTACK_CHECK_DECLARATION;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003405
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003406 if ((initstr = mch_getenv(env)) == NULL || *initstr == NUL)
3407 return FAIL;
3408
3409 if (is_viminit)
3410 vimrc_found(NULL, NULL);
3411 estack_push(ETYPE_ENV, env, 0);
ichizok7e5fe382023-04-15 13:17:50 +01003412 ESTACK_CHECK_SETUP;
3413 save_current_sctx = current_sctx;
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003414 current_sctx.sc_version = 1;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003415#ifdef FEAT_EVAL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003416 current_sctx.sc_sid = SID_ENV;
3417 current_sctx.sc_seq = 0;
3418 current_sctx.sc_lnum = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003419#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003420
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003421 do_cmdline_cmd(initstr);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003422
ichizok7e5fe382023-04-15 13:17:50 +01003423 ESTACK_CHECK_NOW;
3424 estack_pop();
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003425 current_sctx = save_current_sctx;
3426 return OK;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003427}
3428
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003429#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003430/*
3431 * Return TRUE if we are certain the user owns the file "fname".
3432 * Used for ".vimrc" and ".exrc".
3433 * Use both stat() and lstat() for extra security.
3434 */
3435 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003436file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003437{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003438 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003439# ifdef UNIX
3440 uid_t uid = getuid();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003441# else // VMS
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003442 uid_t uid = ((getgid() << 16) | getuid());
3443# endif
3444
3445 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3446# ifdef HAVE_LSTAT
3447 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3448# endif
3449 );
3450}
3451#endif
3452
3453/*
3454 * Give an error message main_errors["n"] and exit.
3455 */
3456 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003457mainerr(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003458 int n, // one of the ME_ defines
3459 char_u *str) // extra argument or NULL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003460{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003461#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003462 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003463#endif
3464
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003465 // If this is a Windows GUI executable, show an error dialog box.
3466#ifdef VIMDLL
3467 gui.in_use = mch_is_gui_executable();
3468#endif
3469#ifdef FEAT_GUI_MSWIN
3470 gui.starting = FALSE; // Needed to show as error.
3471#endif
3472
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003473 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003474 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003475 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003476 mch_errmsg(_(main_errors[n]));
3477 if (str != NULL)
3478 {
3479 mch_errmsg(": \"");
3480 mch_errmsg((char *)str);
3481 mch_errmsg("\"");
3482 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003483 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003484
3485 mch_exit(1);
3486}
3487
3488 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003489mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003490{
3491 mainerr(ME_ARG_MISSING, str);
3492}
3493
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003494#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003495/*
3496 * print a message with three spaces prepended and '\n' appended.
3497 */
3498 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003499main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003500{
3501 mch_msg(" ");
3502 mch_msg(s);
3503 mch_msg("\n");
3504}
3505
3506/*
3507 * Print messages for "vim -h" or "vim --help" and exit.
3508 */
3509 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003510usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003511{
3512 int i;
3513 static char *(use[]) =
3514 {
3515 N_("[file ..] edit specified file(s)"),
3516 N_("- read text from stdin"),
3517 N_("-t tag edit file where tag is defined"),
3518#ifdef FEAT_QUICKFIX
3519 N_("-q [errorfile] edit file with first error")
3520#endif
3521 };
3522
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003523#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003524 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003525#endif
3526
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003527 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003528 mch_msg(longVersion);
Bram Moolenaar32aaf5a2018-06-21 21:38:33 +02003529 mch_msg(_("\n\nUsage:"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003530 for (i = 0; ; ++i)
3531 {
3532 mch_msg(_(" vim [arguments] "));
3533 mch_msg(_(use[i]));
K.Takataeeec2542021-06-02 13:28:16 +02003534 if (i == ARRAY_LENGTH(use) - 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003535 break;
3536 mch_msg(_("\n or:"));
3537 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003538#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003539 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003540#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003541
3542 mch_msg(_("\n\nArguments:\n"));
3543 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003544#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003545 main_msg(_("--literal\t\tDon't expand wildcards"));
3546#endif
3547#ifdef FEAT_OLE
3548 main_msg(_("-register\t\tRegister this gvim for OLE"));
3549 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3550#endif
3551#ifdef FEAT_GUI
3552 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3553 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3554#endif
3555 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3556 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003557 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003558 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3559#ifdef FEAT_DIFF
3560 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3561#endif
3562 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3563 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3564 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3565 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3566 main_msg(_("-M\t\t\tModifications in text not allowed"));
3567 main_msg(_("-b\t\t\tBinary mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003568 main_msg(_("-l\t\t\tLisp mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003569 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3570 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003571 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3572#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003573 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003574#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003575 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3576 main_msg(_("-r\t\t\tList swap files and exit"));
3577 main_msg(_("-r (with file name)\tRecover crashed session"));
3578 main_msg(_("-L\t\t\tSame as -r"));
3579#ifdef AMIGA
3580 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3581 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3582#endif
3583#ifdef FEAT_ARABIC
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003584 main_msg(_("-A\t\t\tStart in Arabic mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003585#endif
3586#ifdef FEAT_RIGHTLEFT
3587 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3588#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003589 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003590 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2d12c252022-06-13 21:42:45 +01003591#ifdef FEAT_GUI
3592 main_msg(_("--gui-dialog-file {fname} For testing: write dialog text"));
3593#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003594 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003595 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3596#ifdef FEAT_GUI
3597 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3598#endif
3599 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003600 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003601 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3602 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3603 main_msg(_("+\t\t\tStart at end of file"));
3604 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003605 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003606 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3607 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3608 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3609 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3610 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3611#ifdef FEAT_CRYPT
3612 main_msg(_("-x\t\t\tEdit encrypted files"));
3613#endif
3614#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3615# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar86181df2020-05-11 23:14:04 +02003616 main_msg(_("-display <display>\tConnect Vim to this particular X-server"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003617# endif
3618 main_msg(_("-X\t\t\tDo not connect to X server"));
3619#endif
3620#ifdef FEAT_CLIENTSERVER
3621 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3622 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3623 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3624 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003625 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003626 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3627 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3628 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3629 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3630#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003631#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003632 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003633#endif
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01003634#ifdef FEAT_JOB_CHANNEL
K.Takata0fd7be72022-11-09 16:29:24 +00003635 main_msg(_("--log <file>\t\tStart logging to <file> early"));
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01003636#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003637#ifdef FEAT_VIMINFO
3638 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3639#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003640 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003641 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3642 main_msg(_("--version\t\tPrint version information and exit"));
3643
3644#ifdef FEAT_GUI_X11
3645# ifdef FEAT_GUI_MOTIF
3646 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003647# endif
Bram Moolenaar86181df2020-05-11 23:14:04 +02003648 main_msg(_("-display <display>\tRun Vim on <display>"));
Bram Moolenaar9e6ba8c2020-05-12 22:21:26 +02003649 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003650 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3651 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3652 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3653 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3654 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3655 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3656 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3657 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003658 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3659 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3660 main_msg(_("-xrm <resource>\tSet the specified resource"));
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003661#endif // FEAT_GUI_X11
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003662#ifdef FEAT_GUI_GTK
3663 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003664 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3665 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003666 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3667 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003668 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003669 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
Bram Moolenaar86181df2020-05-11 23:14:04 +02003670 main_msg(_("-display <display>\tRun Vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003671 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003672 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003673 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003674#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003675#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003676# ifdef VIMDLL
3677 if (gui.starting)
3678# endif
3679 {
3680 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
3681 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
3682 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003683#endif
3684
3685#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003686 // Gnome gives extra messages for --help if we continue, but not for -h.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003687 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003688 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003689 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003690 gui.dofork = FALSE;
3691 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003692 else
3693#endif
3694 mch_exit(0);
3695}
3696
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003697/*
3698 * Check the result of the ATTENTION dialog:
3699 * When "Quit" selected, exit Vim.
3700 * When "Recover" selected, recover the file.
3701 */
3702 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003703check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003704{
3705 if (swap_exists_action == SEA_QUIT)
3706 getout(1);
3707 handle_swap_exists(NULL);
3708}
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003709
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003710#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003711
Bram Moolenaar595297d2017-03-04 19:11:12 +01003712#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003713 static void
3714set_progpath(char_u *argv0)
3715{
3716 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003717
Bram Moolenaar4f974752019-02-17 17:44:42 +01003718# ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003719 // A relative path containing a "/" will become invalid when using ":cd",
3720 // turn it into a full path.
3721 // On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3722 // this.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003723 char_u *path = NULL;
3724
3725 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3726 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003727# else
3728 char_u buf[MAXPATHL + 1];
3729# ifdef PROC_EXE_LINK
3730 char linkbuf[MAXPATHL + 1];
3731 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003732
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003733 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3734 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003735 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003736 linkbuf[len] = NUL;
3737 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003738 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003739# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003740
3741 if (!mch_isFullName(val))
3742 {
3743 if (gettail(val) != val
3744 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3745 val = buf;
3746 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003747# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003748
Bram Moolenaar08cab962017-03-04 14:37:18 +01003749 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003750
Bram Moolenaar4f974752019-02-17 17:44:42 +01003751# ifdef MSWIN
Bram Moolenaar43663192017-03-05 14:29:12 +01003752 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003753# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003754}
3755
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003756#endif // NO_VIM_MAIN