blob: 31494e3e8e19271a189670616be7903a02f9fe9d [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
Hirohito Higashic5654b82025-02-10 20:55:17 +0100147 /*
148 * Various initialisations #1 shared with tests.
149 */
150 common_init_1();
151
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100152#if defined(STARTUPTIME) || defined(FEAT_JOB_CHANNEL)
153 // Need to find "--startuptime" and "--log" before actually parsing
154 // arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100155 for (i = 1; i < argc - 1; ++i)
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100156 {
157# ifdef STARTUPTIME
158 if (STRICMP(argv[i], "--startuptime") == 0 && time_fd == NULL)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000159 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000160 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000161 TIME_MSG("--- VIM STARTING ---");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000162 }
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100163# endif
Bram Moolenaar4c5678f2022-11-30 18:12:19 +0000164# ifdef FEAT_EVAL
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100165 if (STRICMP(argv[i], "--log") == 0)
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100166 ch_logfile((char_u *)(argv[i + 1]), (char_u *)"ao");
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100167# endif
168 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000169#endif
170
Bram Moolenaar07268702018-03-01 21:57:32 +0100171#ifdef CLEAN_RUNTIMEPATH
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100172 // Need to find "--clean" before actually parsing arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100173 for (i = 1; i < argc; ++i)
174 if (STRICMP(argv[i], "--clean") == 0)
175 {
176 params.clean = TRUE;
177 break;
178 }
179#endif
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200180#ifdef MSWIN
Bram Moolenaar4a228972021-05-01 22:41:39 +0200181 // Need to find "-register" and "-unregister" before loading any libraries.
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200182 for (i = 1; i < argc; ++i)
Bram Moolenaar4a228972021-05-01 22:41:39 +0200183 if ((STRICMP(argv[i] + 1, "register") == 0
184 || STRICMP(argv[i] + 1, "unregister") == 0)
185 && (argv[i][0] == '-' || argv[i][0] == '/'))
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200186 {
187 found_register_arg = TRUE;
188 break;
189 }
190#endif
191
192 /*
Hirohito Higashic5654b82025-02-10 20:55:17 +0100193 * Various initialisations #2 shared with tests.
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200194 */
Hirohito Higashic5654b82025-02-10 20:55:17 +0100195 common_init_2(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000196
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200197#ifdef VIMDLL
198 // Check if the current executable file is for the GUI subsystem.
199 gui.starting = mch_is_gui_executable();
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +0200200#elif defined(FEAT_GUI_MSWIN)
201 gui.starting = TRUE;
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200202#endif
203
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000204#ifdef FEAT_CLIENTSERVER
205 /*
206 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000207 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000208 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000209 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000210#endif
211
212 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000213 * Figure out the way to work from the command name argv[0].
214 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000215 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000216 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000217
218 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000219 * Process the command line arguments. File names are put in the global
220 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000221 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000222 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000223 TIME_MSG("parsing arguments");
224
225 /*
226 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000227 * there is no terminal version, and on Windows we can't fork one off with
228 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000229 */
230#ifdef ALWAYS_USE_GUI
231 gui.starting = TRUE;
232#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000233# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000234 /*
235 * Check if the GUI can be started. Reset gui.starting if not.
236 * Don't know about other systems, stay on the safe side and don't check.
237 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000238 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000239 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000240 if (gui_init_check() == FAIL)
241 {
242 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000243
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100244 // When running "evim" or "gvim -y" we need the menus, exit if we
245 // don't have them.
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000246 if (params.evim_mode)
247 mch_exit(1);
248 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000249 }
250# endif
251#endif
252
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000253 if (GARGCOUNT > 0)
254 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100255#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000256 /*
257 * Expand wildcards in file names.
258 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000259 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000260 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200261 start_dir = alloc(MAXPATHL);
262 if (start_dir != NULL)
263 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100264 // Temporarily add '(' and ')' to 'isfname'. These are valid
265 // filename characters but are excluded from 'isfname' to make
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000266 // "gf" work on a file name in parentheses (e.g.: see vim.h).
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000267 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000268 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000269 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200270 if (start_dir != NULL)
271 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000272 }
273#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200274 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000275 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000276
Bram Moolenaar4f974752019-02-17 17:44:42 +0100277#ifdef MSWIN
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000278 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100279 // Remember the number of entries in the argument list. If it changes
280 // we don't react on setting 'encoding'.
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000281 set_alist_count();
282 }
283#endif
284
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000285#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000286 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000287 {
288 /*
289 * If there is one filename, fully qualified, we have very probably
290 * been invoked from explorer, so change to the file's directory.
291 * Hint: to avoid this when typing a command use a forward slash.
292 * If the cd fails, it doesn't matter.
293 */
Bram Moolenaar05268152021-11-18 18:53:45 +0000294 if (vim_chdirfile(params.fname, "drop") == OK)
295 last_chdir_reason = "drop";
Bram Moolenaarf6303872015-04-03 17:59:43 +0200296 if (start_dir != NULL)
297 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000298 }
299#endif
300 TIME_MSG("expanding arguments");
301
302#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000303 if (params.diff_mode && params.window_count == -1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100304 params.window_count = 0; // open up to 3 windows
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000305#endif
306
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100307 // Don't redraw until much later.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000308 ++RedrawingDisabled;
309
310 /*
311 * When listing swap file names, don't do cursor positioning et. al.
312 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200313 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000314 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000315
316 /*
Bram Moolenaarc174c2e2023-03-25 20:06:49 +0000317 * When certain to start the GUI, don't check terminal capabilities.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000318 * For GTK we can't be sure, but when started from the desktop it doesn't
319 * make sense to try using a terminal.
320 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200321#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
322 || defined(VIMDLL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000323 if (gui.starting
324# ifdef FEAT_GUI_GTK
325 && !isatty(2)
326# endif
327 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000328 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000329#endif
330
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000331 /*
332 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100333 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000334 * Note that we may use mch_exit() before mch_init()!
335 */
336 mch_init();
337 TIME_MSG("shell init");
338
339#ifdef USE_XSMP
340 /*
341 * For want of anywhere else to do it, try to connect to xsmp here.
342 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
343 * Hijacking -X 'no X connection' to also disable XSMP connection as that
344 * has a similar delay upon failure.
345 * Only try if SESSION_MANAGER is set to something non-null.
346 */
347 if (!x_no_connect)
348 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000349 char *p = getenv("SESSION_MANAGER");
350
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000351 if (p != NULL && *p != NUL)
352 {
353 xsmp_init();
354 TIME_MSG("xsmp init");
355 }
356 }
357#endif
358
359 /*
360 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000361 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000362 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000363
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100364#ifdef _IOLBF
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100365 if (silent_mode)
K.Takata3c240f62023-05-31 12:47:45 +0100366 {
367 // Ensure output works usefully without a tty: buffer lines instead of
368 // fully buffered.
369 s_vbuf = malloc(BUFSIZ);
370 if (s_vbuf != NULL)
371 setvbuf(stdout, s_vbuf, _IOLBF, BUFSIZ);
372 }
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100373#endif
374
Bram Moolenaar9404a182019-05-03 22:25:40 +0200375 // This message comes before term inits, but after setting "silent_mode"
376 // when the input is not a tty. Omit the message with --not-a-term.
377 if (GARGCOUNT > 1 && !silent_mode && !is_not_a_term())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000378 printf(_("%d files to edit\n"), GARGCOUNT);
379
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000380 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000381 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100382 termcapinit(params.term); // set terminal name and get terminal
383 // capabilities (will set full_screen)
384 screen_start(); // don't know where cursor is now
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000385 TIME_MSG("Termcap init");
386 }
387
388 /*
389 * Set the default values for the options that use Rows and Columns.
390 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100391 ui_get_shellsize(); // inits Rows and Columns
Luuk van Baal470a1412022-09-14 01:27:23 +0100392 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000393#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100394 // Set the 'diff' option now, so that it can be checked for in a .vimrc
395 // file. There is no buffer yet though.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000396 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000397 diff_win_options(firstwin, FALSE);
398#endif
399
400 cmdline_row = Rows - p_ch;
401 msg_row = cmdline_row;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100402 screenalloc(FALSE); // allocate screen buffers
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000403 set_init_2();
404 TIME_MSG("inits 2");
405
406 msg_scroll = TRUE;
407 no_wait_return = TRUE;
408
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100409 init_mappings(); // set up initial mappings
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000410
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100411 init_highlight(TRUE, FALSE); // set the default highlight groups
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000412 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000413
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200414#if defined(FEAT_TERMRESPONSE)
415 init_term_props(TRUE);
416#endif
417
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000418#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100419 // Set the break level after the terminal is initialized.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000420 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000421#endif
422
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100423 // Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
424 // Allows for setting 'loadplugins' there.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200425 if (params.use_vimrc != NULL
426 && (STRCMP(params.use_vimrc, "NONE") == 0
427 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
428 p_lpl = FALSE;
429
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100430 // Execute --cmd arguments.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200431 exe_pre_commands(&params);
432
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100433 // Source startup scripts.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200434 source_startup_scripts(&params);
435
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100436#ifdef FEAT_MZSCHEME
437 /*
438 * Newer version of MzScheme (Racket) require earlier (trampolined)
439 * initialisation via scheme_main_setup.
440 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200441 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200442 * Do source startup scripts, so that 'mzschemedll' can be set.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100443 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200444 return mzscheme_main();
445#else
446 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100447#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200448}
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100449#endif // NO_VIM_MAIN
450#endif // PROTO
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100451
Foxe Chen6924eb82025-04-19 11:25:18 +0200452#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
453/*
454 * Restore the state after a fatal X error.
455 */
456 static void
457x_restore_state(void)
458{
459 State = MODE_NORMAL;
460 VIsual_active = FALSE;
461 got_int = TRUE;
462 need_wait_return = FALSE;
463 global_busy = FALSE;
464 exmode_active = 0;
465 skip_redraw = FALSE;
466 RedrawingDisabled = 0;
467 no_wait_return = 0;
468 vgetc_busy = 0;
469# ifdef FEAT_EVAL
470 emsg_skip = 0;
471# endif
472 emsg_off = 0;
473 setmouse();
474 settmode(TMODE_RAW);
475 starttermcap();
476 scroll_start();
477 redraw_later_clear();
478}
479#endif
480
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200481/*
482 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
483 * things simple.
484 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
485 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100486 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200487vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100488{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100489#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000490#ifdef FEAT_EVAL
491 /*
492 * Read all the plugin files.
493 * Only when compiled with +eval, since most plugins need it.
494 */
495 if (p_lpl)
496 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200497 char_u *rtp_copy = NULL;
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100498 char_u *plugin_pattern = (char_u *)
499# if defined(VMS) || defined(AMIGA) // VMS and Amiga don't handle the "**".
500 "plugin/*.vim"
501# else
502 "plugin/**/*.vim"
503# endif
504 ;
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200505
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100506 // First add all package directories to 'runtimepath', so that their
507 // autoload directories can be found. Only if not done already with a
508 // :packloadall command.
509 // Make a copy of 'runtimepath', so that source_runtime does not use
510 // the pack directories.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200511 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200512 {
513 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200514 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200515 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200516
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100517 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy, plugin_pattern,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518 DIP_ALL | DIP_NOAFTER, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000519 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200520 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100521
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100522 // Only source "start" packages if not done already with a :packloadall
523 // command.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200524 if (!did_source_packages)
525 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100526 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200527
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100528 source_runtime(plugin_pattern, DIP_ALL | DIP_AFTER);
Bram Moolenaar66459b72016-08-06 19:01:55 +0200529 TIME_MSG("loading after plugins");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000530 }
531#endif
532
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000533#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100534 // Decide about window layout for diff mode after reading vimrc.
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000535 if (params.diff_mode && params.window_layout == 0)
536 {
537 if (diffopt_horizontal())
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100538 params.window_layout = WIN_HOR; // use horizontal split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000539 else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100540 params.window_layout = WIN_VER; // use vertical split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000541 }
542#endif
543
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000544 /*
545 * Recovery mode without a file name: List swap files.
546 * This uses the 'dir' option, therefore it must be after the
547 * initializations.
548 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200549 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000550 {
Bram Moolenaarc216a7a2022-12-05 13:50:55 +0000551 recover_names(NULL, TRUE, NULL, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000552 mch_exit(0);
553 }
554
555 /*
556 * Set a few option defaults after reading .vimrc files:
557 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
558 */
559 set_init_3();
560 TIME_MSG("inits 3");
561
562 /*
563 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
564 * Note that this overrides anything from a vimrc file.
565 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000566 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000567 p_uc = 0;
568
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000569#ifdef FEAT_GUI
570 if (gui.starting)
571 {
Bram Moolenaarc7226682019-08-17 16:33:23 +0200572# if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100573 // When something caused a message from a vimrc script, need to output
574 // an extra newline before the shell prompt.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000575 if (did_emsg || msg_didout)
576 putchar('\n');
Bram Moolenaarc7226682019-08-17 16:33:23 +0200577# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000578
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100579 gui_start(NULL); // will set full_screen to TRUE
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000580 TIME_MSG("starting GUI");
581
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100582 // When running "evim" or "gvim -y" we need the menus, exit if we
583 // don't have them.
Bram Moolenaar58d98232005-07-23 22:25:46 +0000584 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000585 mch_exit(1);
Luuk van Baal470a1412022-09-14 01:27:23 +0100586 firstwin->w_prev_height = firstwin->w_height; // may have changed
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000587 }
588#endif
589
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000590#ifdef FEAT_VIMINFO
591 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000592 * Read in registers, history etc, but not marks, from the viminfo file.
593 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000594 */
595 if (*p_viminfo != NUL)
596 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000597 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000598 TIME_MSG("reading viminfo");
599 }
600#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100601#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100602 // It's better to make v:oldfiles an empty list than NULL.
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100603 if (get_vim_var_list(VV_OLDFILES) == NULL)
604 set_vim_var_list(VV_OLDFILES, list_alloc());
605#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000606
607#ifdef FEAT_QUICKFIX
608 /*
609 * "-q errorfile": Load the error file now.
610 * If the error file can't be read, exit before doing anything else.
611 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000612 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000613 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100614 char_u *enc = NULL;
615
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100616 enc = p_menc;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000617 if (params.use_ef != NULL)
618 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000619 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200620 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100621 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000622 {
623 out_char('\n');
624 mch_exit(3);
625 }
626 TIME_MSG("reading errorfile");
627 }
628#endif
629
630 /*
631 * Start putting things on the screen.
632 * Scroll screen down before drawing over it
633 * Clear screen now, so file message will not be cleared.
634 */
635 starting = NO_BUFFERS;
636 no_wait_return = FALSE;
637 if (!exmode_active)
638 msg_scroll = FALSE;
639
640#ifdef FEAT_GUI
641 /*
642 * This seems to be required to make callbacks to be called now, instead
643 * of after things have been put on the screen, which then may be deleted
644 * when getting a resize callback.
645 * For the Mac this handles putting files dropped on the Vim icon to
646 * global_alist.
647 */
648 if (gui.in_use)
649 {
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100650 gui_wait_for_chars(50L, typebuf.tb_change_cnt);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000651 TIME_MSG("GUI delay");
652 }
653#endif
654
655#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
656 qnx_clip_init();
657#endif
658
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200659#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
660 clip_init(TRUE);
661#endif
662
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000663#ifdef FEAT_XCLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100664 // Start using the X clipboard, unless the GUI was started.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000665# ifdef FEAT_GUI
666 if (!gui.in_use)
667# endif
668 {
669 setup_term_clip();
670 TIME_MSG("setup clipboard");
671 }
672#endif
673
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000674#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100675 // Prepare for being a Vim server.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000676 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000677#endif
678
679 /*
680 * If "-" argument given: Read file from stdin.
681 * Do this before starting Raw mode, because it may change things that the
682 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
683 * are the same terminal: "cat | vim -".
684 * Using autocommands here may cause trouble...
685 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000686 if (params.edit_type == EDIT_STDIN && !recoverymode)
687 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000688
689#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100690 // When switching screens and something caused a message from a vimrc
691 // script, need to output an extra newline on exit.
Abhijit Barik221927b2025-04-06 16:12:06 +0200692 if ((did_emsg || msg_didout) && *T_TI != NUL && params.edit_type != EDIT_STDIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000693 newline_on_exit = TRUE;
694#endif
695
696 /*
K.Takataeeec2542021-06-02 13:28:16 +0200697 * When done something that is not allowed or given an error message call
Bram Moolenaar13608d82022-08-29 15:06:50 +0100698 * wait_return(). This must be done before starttermcap(), because it may
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000699 * switch to another screen. It must be done after settmode(TMODE_RAW),
700 * because we want to react on a single key stroke.
701 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000702 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000703 */
704 settmode(TMODE_RAW);
705 TIME_MSG("setting raw mode");
706
707 if (need_wait_return || msg_didany)
708 {
709 wait_return(TRUE);
710 TIME_MSG("waiting for return");
711 }
712
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100713 starttermcap(); // start termcap if not done by wait_return()
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000714 TIME_MSG("start termcap");
715
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200716 setmouse(); // may start using the mouse
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000717 if (scroll_region)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100718 scroll_region_reset(); // In case Rows changed
719 scroll_start(); // may scroll the screen to the right position
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000720
Bram Moolenaar651fca82021-11-29 20:39:38 +0000721#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar40385db2018-08-07 22:31:44 +0200722 term_push_title(SAVE_RESTORE_BOTH);
723#endif
724
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000725 /*
726 * Don't clear the screen when starting in Ex mode, unless using the GUI.
727 */
728 if (exmode_active
729#ifdef FEAT_GUI
730 && !gui.in_use
731#endif
732 )
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100733 set_must_redraw(UPD_CLEAR);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000734 else
735 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100736 screenclear(); // clear screen
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000737 TIME_MSG("clearing screen");
738 }
739
740#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000741 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000742 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100743 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200744 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000745 TIME_MSG("getting crypt key");
746 }
747#endif
748
749 no_wait_return = TRUE;
750
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000751 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000752 * Create the requested number of windows and edit buffers in them.
753 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000754 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000755 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000756 TIME_MSG("opening buffers");
757
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000758#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100759 // clear v:swapcommand
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000760 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
761#endif
762
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100763 // Ex starts at last line of the file
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000764 if (exmode_active)
765 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
766
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000767 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
768 TIME_MSG("BufEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000769 setpcmark();
770
771#ifdef FEAT_QUICKFIX
772 /*
773 * When started with "-q errorfile" jump to first error now.
774 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000775 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000776 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000777 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000778 TIME_MSG("jump to first error");
779 }
780#endif
781
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000782 /*
783 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000784 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000785 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200786 edit_buffers(&params, start_dir);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200787 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000788
789#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000790 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000791 {
792 win_T *wp;
793
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100794 // set options in each window for "vimdiff".
Bram Moolenaar29323592016-07-24 22:04:11 +0200795 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000796 diff_win_options(wp, TRUE);
797 }
798#endif
799
800 /*
801 * Shorten any of the filenames, but only when absolute.
802 */
803 shorten_fnames(FALSE);
804
805 /*
806 * Need to jump to the tag before executing the '-c command'.
807 * Makes "vim -c '/return' -t main" work.
808 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000809 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000810 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000811 swap_exists_did_quit = FALSE;
Bram Moolenaar146522e2005-12-16 21:55:46 +0000812
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000813 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000814 do_cmdline_cmd(IObuff);
815 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000816
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100817 // If the user doesn't want to edit the file then we quit here.
Bram Moolenaar146522e2005-12-16 21:55:46 +0000818 if (swap_exists_did_quit)
819 getout(1);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000820 }
821
Foxe Chen6924eb82025-04-19 11:25:18 +0200822#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
823 // Temporarily set x_jump_env to here in case there is an X11 IO error,
824 // because x_jump_env is only actually set in main_loop(), before
825 // exe_commands(). May not be the best solution since commands passed via
826 // the command line can be very broad like sourcing a file, in which case
827 // an X IO error results in the command being partially done. In theory we
828 // could use SETJMP in RealWaitForChar(), but the stack frame for that may
829 // possibly exit and then LONGJMP is called on it.
830 int jump_result = SETJMP(x_jump_env);
831
832 if (jump_result == 0)
833 {
834#endif
835 // Execute any "+", "-c" and "-S" arguments.
836 if (params.n_commands > 0)
837 exe_commands(&params);
838#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
839 }
840 else
841 // Restore state and continue just like what main_loop() does.
842 x_restore_state();
843#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000844
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100845 // Must come before the may_req_ calls.
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200846 starting = 0;
847
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100848#if defined(FEAT_TERMRESPONSE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100849 // Must be done before redrawing, puts a few characters on the screen.
Bram Moolenaara45551a2020-06-09 15:57:37 +0200850 check_terminal_behavior();
Bram Moolenaar976787d2017-06-04 15:45:50 +0200851#endif
852
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000853 RedrawingDisabled = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100854 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000855 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000856
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100857 // 'autochdir' has been postponed
Bram Moolenaar6f470022018-04-10 18:47:20 +0200858 DO_AUTOCHDIR;
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200859
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000860#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100861 // Requesting the termresponse is postponed until here, so that a "-c q"
862 // argument doesn't make it appear in the shell Vim was started from.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000863 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200864
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200865 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000866#endif
867
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100868 // start in insert mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000869 if (p_im)
870 need_start_insertmode = TRUE;
871
Bram Moolenaar14735512016-03-26 21:00:08 +0100872#ifdef FEAT_EVAL
873 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
874#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000875 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
876 TIME_MSG("VimEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000877
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200878#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100879 // Adjust default register name for "unnamed" in 'clipboard'. Can only be
880 // done after the clipboard is available and all initial commands that may
881 // modify the 'clipboard' setting have run; i.e. just before entering the
882 // main loop.
Bram Moolenaar439c0362020-06-06 15:58:03 +0200883 reset_reg_var();
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200884#endif
885
Bram Moolenaar8a3bb562018-03-04 20:14:14 +0100886#if defined(FEAT_DIFF)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100887 // When a startup script or session file setup for diff'ing and
888 // scrollbind, sync the scrollbind now.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000889 if (curwin->w_p_diff && curwin->w_p_scb)
890 {
891 update_topline();
892 check_scrollbind((linenr_T)0, 0L);
893 TIME_MSG("diff scrollbinding");
894 }
895#endif
896
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200897#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
898# ifdef VIMDLL
899 if (!gui.in_use)
900# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100901 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000902#endif
903
Bram Moolenaar4033c552017-09-16 20:54:51 +0200904#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100905 // When tab pages were created, may need to update the tab pages line and
906 // scrollbars. This is skipped while creating them.
h-east6073f132021-12-24 11:57:06 +0000907 if (gui.in_use && first_tabpage->tp_next != NULL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000908 {
909 out_flush();
910 gui_init_which_components(NULL);
911 gui_update_scrollbars(TRUE);
912 }
913 need_mouse_correct = TRUE;
914#endif
915
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100916 // If ":startinsert" command used, stuff a dummy command to be able to
917 // call normal_cmd(), which will then start Insert mode.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000918 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000919 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000920
921#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200922 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200923 {
924# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200925# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar4f974752019-02-17 17:44:42 +0100926 && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200927 if (gui.in_use)
928 {
929 mch_errmsg(_("netbeans is not supported with this GUI\n"));
930 mch_exit(2);
931 }
932# endif
933# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100934 // Tell the client that it can start sending commands.
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200935 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200936 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000937#endif
938
Bram Moolenaare7ddf4e2020-02-03 21:29:30 +0100939 // Redraw at least once, also when 'lazyredraw' is set, to make sure the
940 // window title gets updated.
941 do_redraw = TRUE;
942
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000943 TIME_MSG("before starting main loop");
944
945 /*
946 * Call the main command loop. This never returns.
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200947 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000948 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000949
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100950#endif // NO_VIM_MAIN
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200951
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000952 return 0;
953}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000954
955/*
Hirohito Higashic5654b82025-02-10 20:55:17 +0100956 * Initialisation #1 shared by main() and some tests.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200957 */
958 void
Hirohito Higashic5654b82025-02-10 20:55:17 +0100959common_init_1(void)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200960{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100961 estack_init();
Bram Moolenaar438d1762018-09-30 17:11:48 +0200962 cmdline_init();
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200963
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100964 (void)mb_init(); // init mb_bytelen_tab[] to ones
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200965#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100966 eval_init(); // init global variables
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200967#endif
968
969#ifdef __QNXNTO__
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100970 qnx_init(); // PhAttach() for clipboard, (and gui)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200971#endif
972
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200973 /*
974 * Allocate space for the generic buffers (needed for set_init_1() and
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100975 * emsg()).
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200976 */
977 if ((IObuff = alloc(IOSIZE)) == NULL
978 || (NameBuff = alloc(MAXPATHL)) == NULL)
979 mch_exit(0);
980 TIME_MSG("Allocated generic buffers");
Hirohito Higashic5654b82025-02-10 20:55:17 +0100981}
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200982
Hirohito Higashic5654b82025-02-10 20:55:17 +0100983
984/*
985 * Initialisation #2 shared by main() and some tests.
986 */
987 void
988common_init_2(mparm_T *paramp)
989{
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200990#ifdef NBDEBUG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100991 // Wait a moment for debugging NetBeans. Must be after allocating
992 // NameBuff.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200993 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
994 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
995 TIME_MSG("NetBeans debug wait");
996#endif
997
998#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
999 /*
1000 * Setup to use the current locale (for ctype() and many other things).
1001 * NOTE: Translated messages with encodings other than latin1 will not
1002 * work until set_init_1() has been called!
1003 */
1004 init_locale();
1005 TIME_MSG("locale set");
1006#endif
1007
1008#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001009 gui.dofork = TRUE; // default is to use fork()
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001010#endif
1011
1012 /*
1013 * Do a first scan of the arguments in "argv[]":
1014 * -display or --display
1015 * --server...
1016 * --socketid
1017 * --windowid
1018 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +02001019 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001020
Bram Moolenaard0573012017-10-28 21:11:06 +02001021#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001022 // Prepare for possibly starting GUI sometime
Bram Moolenaara8e691d2016-08-07 15:19:26 +02001023 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001024 TIME_MSG("GUI prepared");
1025#endif
1026
1027#ifdef FEAT_CLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001028 clip_init(FALSE); // Initialise clipboard stuff
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001029 TIME_MSG("clipboard setup");
1030#endif
1031
1032 /*
1033 * Check if we have an interactive window.
1034 * On the Amiga: If there is no window, we open one with a newcli command
1035 * (needed for :! to * work). mch_check_win() will also handle the -d or
1036 * -dev argument.
1037 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001038 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001039 TIME_MSG("window checked");
1040
1041 /*
1042 * Allocate the first window and buffer.
1043 * Can't do anything without it, exit when it fails.
1044 */
1045 if (win_alloc_first() == FAIL)
1046 mch_exit(0);
1047
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001048 init_yank(); // init yank buffers
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001049
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001050 alist_init(&global_alist); // Init the argument list to empty.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001051 global_alist.id = 0;
1052
1053 /*
1054 * Set the default values for the options.
1055 * NOTE: Non-latin1 translated messages are working only after this,
1056 * because this is where "has_mbyte" will be set, which is used by
1057 * msg_outtrans_len_attr().
1058 * First find out the home directory, needed to expand "~" in options.
1059 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001060 init_homedir(); // find real value of $HOME
Bram Moolenaar07268702018-03-01 21:57:32 +01001061 set_init_1(paramp->clean);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001062 TIME_MSG("inits 1");
1063
1064#ifdef FEAT_EVAL
Bram Moolenaar69bf6342019-10-29 04:16:57 +01001065 // set v:lang and v:ctype
1066 set_lang_var();
1067
1068 // set v:argv
1069 set_argv_var(paramp->argv, paramp->argc);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001070#endif
Bram Moolenaar6436cd82018-12-27 00:28:33 +01001071
1072#ifdef FEAT_SIGNS
1073 init_signs();
1074#endif
64-bitman88d41ab2025-04-06 17:20:39 +02001075
1076#ifdef FEAT_QUICKFIX
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02001077 // initialize quickfix list
64-bitman88d41ab2025-04-06 17:20:39 +02001078 // don't send an error message when memory allocation fails
1079 // do it when the user tries to access the quickfix list
Hirohito Higashiadcfb6c2025-04-07 21:19:07 +02001080 qf_init_stack();
64-bitman88d41ab2025-04-06 17:20:39 +02001081#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001082}
1083
1084/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001085 * Return TRUE when the --not-a-term argument was found.
1086 */
1087 int
Bram Moolenaarf7e73022022-09-24 13:10:04 +01001088is_not_a_term(void)
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001089{
1090 return params.not_a_term;
1091}
1092
Bram Moolenaar7007e312021-03-27 12:11:33 +01001093/*
1094 * Return TRUE when the --not-a-term argument was found or the GUI is in use.
1095 */
Bram Moolenaarf7e73022022-09-24 13:10:04 +01001096 int
1097is_not_a_term_or_gui(void)
Bram Moolenaar7007e312021-03-27 12:11:33 +01001098{
1099 return params.not_a_term
1100#ifdef FEAT_GUI
1101 || gui.in_use
1102#endif
1103 ;
1104}
1105
K.Takata3c240f62023-05-31 12:47:45 +01001106#if defined(EXITFREE) || defined(PROTO)
1107 void
1108free_vbuf(void)
1109{
1110# ifdef _IOLBF
1111 if (s_vbuf != NULL)
1112 {
1113 setvbuf(stdout, NULL, _IONBF, 0);
1114 free(s_vbuf);
1115 s_vbuf = NULL;
1116 }
1117# endif
1118}
1119#endif
1120
Bram Moolenaar2d12c252022-06-13 21:42:45 +01001121#if defined(FEAT_GUI) || defined(PROTO)
1122/*
1123 * If a --gui-dialog-file argument was given return the file name.
1124 * Otherwise return NULL.
1125 */
1126 char_u *
1127get_gui_dialog_file(void)
1128{
1129 return params.gui_dialog_file;
1130}
1131#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001132
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001133// When TRUE in a safe state when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001134static int was_safe = FALSE;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001135static oparg_T *current_oap = NULL;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001136
1137/*
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001138 * Return TRUE if an operator was started but not finished yet.
1139 * Includes typing a count or a register name.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001140 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001141 int
1142op_pending(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001143{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001144 return !(current_oap != NULL
1145 && !finish_op
1146 && current_oap->prev_opcount == 0
1147 && current_oap->prev_count0 == 0
1148 && current_oap->op_type == OP_NOP
1149 && current_oap->regname == NUL);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001150}
1151
1152/*
Bram Moolenaard103ee72019-09-18 21:15:31 +02001153 * Return whether currently it is safe, assuming it was safe before (high level
1154 * state didn't change).
1155 */
1156 static int
1157is_safe_now(void)
1158{
1159 return stuff_empty()
1160 && typebuf.tb_len == 0
1161 && scriptin[curscript] == NULL
Bram Moolenaar4fa13462022-02-05 12:39:24 +00001162#ifdef FEAT_EVAL
1163 && !debug_mode
1164#endif
1165 && !global_busy;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001166}
1167
1168/*
zeertzjqc4226622024-04-03 22:38:07 +02001169 * Trigger SafeState if currently in a safe state, that is "safe" is TRUE and
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001170 * there is no typeahead.
1171 */
1172 void
1173may_trigger_safestate(int safe)
1174{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001175 int is_safe = safe && is_safe_now();
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001176
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001177#ifdef FEAT_EVAL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001178 if (was_safe != is_safe)
1179 // Only log when the state changes, otherwise it happens at nearly
1180 // every key stroke.
Bram Moolenaard103ee72019-09-18 21:15:31 +02001181 ch_log(NULL, is_safe ? "SafeState: Start triggering"
1182 : "SafeState: Stop triggering");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001183#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001184 if (is_safe)
1185 apply_autocmds(EVENT_SAFESTATE, NULL, NULL, FALSE, curbuf);
1186 was_safe = is_safe;
1187}
1188
1189/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001190 * Something changed which causes the state possibly to be unsafe, e.g. a
1191 * character was typed. It will remain unsafe until the next call to
1192 * may_trigger_safestate().
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001193 */
1194 void
Bram Moolenaard103ee72019-09-18 21:15:31 +02001195state_no_longer_safe(char *reason UNUSED)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001196{
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001197#ifdef FEAT_EVAL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001198 if (was_safe)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001199 ch_log(NULL, "SafeState: reset: %s", reason);
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001200#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001201 was_safe = FALSE;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001202}
1203
Dominique Pelle748b3082022-01-08 12:41:16 +00001204#if defined(FEAT_EVAL) || defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001205 int
1206get_was_safe_state(void)
1207{
1208 return was_safe;
1209}
Dominique Pelle748b3082022-01-08 12:41:16 +00001210#endif
Bram Moolenaard103ee72019-09-18 21:15:31 +02001211
Dominique Pelle748b3082022-01-08 12:41:16 +00001212#if defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001213/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001214 * Invoked when leaving code that invokes callbacks. Then trigger
1215 * SafeStateAgain, if it was safe when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001216 */
1217 void
Bram Moolenaar37d18072019-09-17 20:28:38 +02001218may_trigger_safestateagain(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001219{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001220 if (!was_safe)
1221 {
1222 // If the safe state was reset in state_no_longer_safe(), e.g. because
1223 // of calling feedkeys(), we check if it's now safe again (all keys
1224 // were consumed).
1225 was_safe = is_safe_now();
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001226# ifdef FEAT_EVAL
Bram Moolenaard103ee72019-09-18 21:15:31 +02001227 if (was_safe)
1228 ch_log(NULL, "SafeState: undo reset");
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001229# endif
Bram Moolenaard103ee72019-09-18 21:15:31 +02001230 }
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001231 if (was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02001232 {
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001233# ifdef FEAT_EVAL
Bram Moolenaar101e9922019-09-25 21:43:11 +02001234 // Only do this message when another message was given, otherwise we
1235 // get lots of them.
1236 if ((did_repeated_msg & REPEATED_MSG_SAFESTATE) == 0)
1237 {
1238 int did = did_repeated_msg;
1239
1240 ch_log(NULL,
1241 "SafeState: back to waiting, triggering SafeStateAgain");
1242 did_repeated_msg = did | REPEATED_MSG_SAFESTATE;
1243 }
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001244# endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001245 apply_autocmds(EVENT_SAFESTATEAGAIN, NULL, NULL, FALSE, curbuf);
Bram Moolenaar37d18072019-09-17 20:28:38 +02001246 }
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001247# ifdef FEAT_EVAL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001248 else
Bram Moolenaard103ee72019-09-18 21:15:31 +02001249 ch_log(NULL,
1250 "SafeState: back to waiting, not triggering SafeStateAgain");
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001251# endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001252}
Dominique Pelle748b3082022-01-08 12:41:16 +00001253#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001254
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001255/*
1256 * Return TRUE if there is any typeahead, pending operator or command.
1257 */
1258 int
1259work_pending(void)
1260{
1261 return op_pending() || !is_safe_now();
1262}
1263
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001264
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001265/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001266 * Main loop: Execute Normal mode commands until exiting Vim.
1267 * Also used to handle commands in the command-line window, until the window
1268 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001269 * Also used to handle ":visual" command after ":global": execute Normal mode
1270 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001271 */
1272 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001273main_loop(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001274 int cmdwin, // TRUE when working in the command-line window
1275 int noexmode) // TRUE when return on entering Ex mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001276{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001277 oparg_T oa; // operator arguments
1278 oparg_T *prev_oap; // operator arguments
1279 volatile int previous_got_int = FALSE; // "got_int" was TRUE
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001280#ifdef FEAT_CONCEAL
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001281 // these are static to avoid a compiler warning
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001282 static linenr_T conceal_old_cursor_line = 0;
1283 static linenr_T conceal_new_cursor_line = 0;
1284 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001285#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001286
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001287 prev_oap = current_oap;
1288 current_oap = &oa;
1289
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001290#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001291 // Setup to catch a terminating error from the X server. Just ignore
1292 // it, restore the state and continue. This might not always work
Foxe Chen6924eb82025-04-19 11:25:18 +02001293 // properly, but at least we hopefully don't exit unexpectedly when the X
1294 // server exits while Vim is running in a console.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001295 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Foxe Chen6924eb82025-04-19 11:25:18 +02001296 x_restore_state();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001297#endif
1298
1299 clear_oparg(&oa);
Martin Tournoij7904fa42022-10-04 16:28:45 +01001300 while (!cmdwin || cmdwin_result == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001301 {
1302 if (stuff_empty())
1303 {
1304 did_check_timestamps = FALSE;
1305 if (need_check_timestamps)
1306 check_timestamps(FALSE);
Bram Moolenaar13608d82022-08-29 15:06:50 +01001307 if (need_wait_return) // if wait_return() still needed ...
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001308 wait_return(FALSE); // ... call it now
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001309 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001310 {
1311 need_start_insertmode = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001312 stuffReadbuff((char_u *)"i"); // start insert mode next
1313 // skip the fileinfo message now, because it would be shown
1314 // after insert mode finishes!
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001315 need_fileinfo = FALSE;
1316 }
1317 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001318
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001319 // Reset "got_int" now that we got back to the main loop. Except when
1320 // inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1321 // the ":g" command.
1322 // For ":g/pat/vi" we reset "got_int" when used once. When used
1323 // a second time we go back to Ex mode and abort the ":g" command.
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001324 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001325 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001326 if (noexmode && global_busy && !exmode_active && previous_got_int)
1327 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001328 // Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1329 // used and keep "got_int" set, so that it aborts ":g".
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001330 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +01001331 State = MODE_NORMAL;
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001332 }
1333 else if (!global_busy || !exmode_active)
1334 {
1335 if (!quit_more)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001336 (void)vgetc(); // flush all buffers
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001337 got_int = FALSE;
1338 }
1339 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001340 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001341 else
1342 previous_got_int = FALSE;
1343
Bram Moolenaar069613c2022-01-15 15:23:44 +00001344#ifdef FEAT_EVAL
1345 // At the toplevel there is no exception handling. Discard any that
1346 // may be hanging around (e.g. from "interrupt" at the debug prompt).
1347 if (did_throw && !ex_normal_busy)
1348 discard_current_exception();
1349#endif
1350
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001351 if (!exmode_active)
1352 msg_scroll = FALSE;
1353 quit_more = FALSE;
1354
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001355 // it's not safe unless may_trigger_safestate_main() is called
1356 was_safe = FALSE;
1357
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001358 /*
1359 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1360 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1361 * update cursor and redraw.
1362 */
1363 if (skip_redraw || exmode_active)
Bram Moolenaarc174c2e2023-03-25 20:06:49 +00001364 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001365 skip_redraw = FALSE;
Bram Moolenaarc174c2e2023-03-25 20:06:49 +00001366 setcursor();
1367 cursor_on();
1368 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001369 else if (do_redraw || stuff_empty())
1370 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001371#ifdef FEAT_GUI
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001372 // If ui_breakcheck() was used a resize may have been postponed.
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001373 gui_may_resize_shell();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001374#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001375#ifdef HAVE_DROP_FILE
1376 // If files were dropped while text was locked or the curbuf was
1377 // locked, this would be a good time to handle the drop.
1378 handle_any_postponed_drop();
1379#endif
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001380#ifdef FEAT_CONCEAL
1381 if (curwin->w_p_cole == 0)
1382 conceal_update_lines = FALSE;
1383#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001384
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001385 // Trigger CursorMoved if the cursor moved.
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001386 if (!finish_op && (has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001387#ifdef FEAT_PROP_POPUP
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001388 || popup_visible
Bram Moolenaar3397f742019-06-02 18:40:06 +02001389#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001390#ifdef FEAT_CONCEAL
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001391 || curwin->w_p_cole > 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001392#endif
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001393 )
1394 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001395 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001396 if (has_cursormoved())
1397 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1398 FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001399#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001400 if (popup_visible)
1401 popup_check_cursor_pos();
1402#endif
1403#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001404 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001405 {
1406 conceal_old_cursor_line = last_cursormoved.lnum;
1407 conceal_new_cursor_line = curwin->w_cursor.lnum;
1408 conceal_update_lines = TRUE;
1409 }
Bram Moolenaar3397f742019-06-02 18:40:06 +02001410#endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001411 last_cursormoved = curwin->w_cursor;
1412 }
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001413
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001414#if defined(FEAT_CONCEAL)
1415 if (conceal_update_lines
1416 && (conceal_old_cursor_line != conceal_new_cursor_line
1417 || conceal_cursor_line(curwin)
1418 || need_cursor_line_redraw))
1419 {
1420 if (conceal_old_cursor_line != conceal_new_cursor_line
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001421 && conceal_old_cursor_line != 0
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001422 && conceal_old_cursor_line
1423 <= curbuf->b_ml.ml_line_count)
1424 redrawWinline(curwin, conceal_old_cursor_line);
1425 redrawWinline(curwin, conceal_new_cursor_line);
1426 curwin->w_valid &= ~VALID_CROW;
1427 need_cursor_line_redraw = FALSE;
1428 }
1429#endif
1430
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001431 // Trigger TextChanged if b:changedtick differs.
Bram Moolenaar186628f2013-03-19 13:33:23 +01001432 if (!finish_op && has_textchanged()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001433 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001434 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001435 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL, FALSE, curbuf);
1436 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001437 }
Bram Moolenaar186628f2013-03-19 13:33:23 +01001438
LemonBoy09371822022-04-08 15:18:45 +01001439 // Ensure curwin->w_topline and curwin->w_leftcol are up to date
1440 // before triggering a WinScrolled autocommand.
1441 update_topline();
1442 validate_cursor();
1443
1444 if (!finish_op)
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001445 may_trigger_win_scrolled_resized();
LemonBoy09371822022-04-08 15:18:45 +01001446
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001447 // If nothing is pending and we are going to wait for the user to
1448 // type a character, trigger SafeState.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001449 may_trigger_safestate(!op_pending() && restart_edit == 0);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001450
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01001451#if defined(FEAT_DIFF)
Bram Moolenaare3521d92018-09-16 14:10:31 +02001452 // Updating diffs from changed() does not always work properly,
1453 // esp. updating folds. Do an update just before redrawing if
1454 // needed.
1455 if (curtab->tp_diff_update || curtab->tp_diff_invalid)
1456 {
1457 ex_diffupdate(NULL);
1458 curtab->tp_diff_update = FALSE;
1459 }
1460
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001461 // Scroll-binding for diff mode may have been postponed until
1462 // here. Avoids doing it for every change.
Bram Moolenaar33aec762006-01-22 23:30:12 +00001463 if (diff_need_scrollbind)
1464 {
1465 check_scrollbind((linenr_T)0, 0L);
1466 diff_need_scrollbind = FALSE;
1467 }
1468#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001469#if defined(FEAT_FOLDING)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001470 // Include a closed fold completely in the Visual area.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001471 foldAdjustVisual();
1472#endif
1473#ifdef FEAT_FOLDING
1474 /*
1475 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1476 * contain the cursor.
1477 * When 'foldopen' is "all", open the fold(s) under the cursor.
1478 * This may mark the window for redrawing.
1479 */
1480 if (hasAnyFolding(curwin) && !char_avail())
1481 {
1482 foldCheckClose();
1483 if (fdo_flags & FDO_ALL)
1484 foldOpenCursor();
1485 }
1486#endif
1487
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001488 // Before redrawing, make sure w_topline is correct, and w_leftcol
1489 // if lines don't wrap, and w_skipcol if lines wrap.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001490 update_topline();
1491 validate_cursor();
1492
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001493 if (VIsual_active)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001494 update_curbuf(UPD_INVERTED); // update inverted part
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001495 else if (must_redraw)
Bram Moolenaara338adc2018-01-31 20:51:47 +01001496 {
Bram Moolenaar017ba072019-09-14 21:01:23 +02001497 mch_disable_flush(); // Stop issuing gui_mch_flush().
Bram Moolenaar11a58af2019-10-24 22:32:31 +02001498 update_screen(0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01001499 mch_enable_flush();
1500 }
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01001501 else if (redraw_cmdline || clear_cmdline || redraw_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001502 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001503 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001504 if (need_maketitle)
1505 maketitle();
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001506#ifdef FEAT_VIMINFO
1507 curbuf->b_last_used = vim_time();
1508#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001509 // display message after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001510 if (keep_msg != NULL)
1511 {
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001512 char_u *p = vim_strsave(keep_msg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001513
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001514 if (p != NULL)
1515 {
1516 // msg_start() will set keep_msg to NULL, make a copy
1517 // first. Don't reset keep_msg, msg_attr_keep() uses it to
1518 // check for duplicates. Never put this message in
1519 // history.
1520 msg_hist_off = TRUE;
1521 msg_attr((char *)p, keep_msg_attr);
1522 msg_hist_off = FALSE;
1523 vim_free(p);
1524 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001525 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001526 if (need_fileinfo) // show file info after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001527 {
1528 fileinfo(FALSE, TRUE, FALSE);
1529 need_fileinfo = FALSE;
1530 }
1531
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001532 emsg_on_display = FALSE; // can delete error message now
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001533 did_emsg = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001534 msg_didany = FALSE; // reset lines_left in msg_start()
1535 may_clear_sb_text(); // clear scroll-back text on next msg
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001536 showruler(FALSE);
1537
1538 setcursor();
1539 cursor_on();
1540
1541 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001542
1543#ifdef STARTUPTIME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001544 // Now that we have drawn the first screen all the startup stuff
1545 // has been done, close any file for startup messages.
Bram Moolenaar3f269672009-11-03 11:11:11 +00001546 if (time_fd != NULL)
1547 {
1548 TIME_MSG("first screen update");
1549 TIME_MSG("--- VIM STARTED ---");
1550 fclose(time_fd);
1551 time_fd = NULL;
1552 }
1553#endif
Bram Moolenaar0a60f792022-11-19 21:18:11 +00001554 // After the first screen update may start triggering WinScrolled
1555 // autocmd events. Store all the scroll positions and sizes now.
1556 may_make_initial_scroll_size_snapshot();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001557 }
1558#ifdef FEAT_GUI
1559 if (need_mouse_correct)
1560 gui_mouse_correct();
1561#endif
1562
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001563 // May request the keyboard protocol state now.
1564 may_send_t_RK();
1565
1566 // Update w_curswant if w_set_curswant has been set.
1567 // Postponed until here to avoid computing w_virtcol too often.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001568 update_curswant();
1569
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001570#ifdef FEAT_EVAL
1571 /*
1572 * May perform garbage collection when waiting for a character, but
1573 * only at the very toplevel. Otherwise we may be using a List or
1574 * Dict internally somewhere.
1575 * "may_garbage_collect" is reset in vgetc() which is invoked through
1576 * do_exmode() and normal_cmd().
1577 */
1578 may_garbage_collect = (!cmdwin && !noexmode);
1579#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001580 /*
1581 * If we're invoked as ex, do a round of ex commands.
1582 * Otherwise, get and execute a normal mode command.
1583 */
1584 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001585 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001586 if (noexmode) // End of ":global/path/visual" commands
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001587 goto theend;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001588 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001589 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001590 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001591 {
1592#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001593 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001594 && oa.op_type == OP_NOP && oa.regname == NUL
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001595 && !VIsual_active
1596 && !skip_term_loop)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001597 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001598 // If terminal_loop() returns OK we got a key that is handled
Christian Brabandtee17b6f2023-09-09 11:23:50 +02001599 // in Normal mode. With FAIL we first need to position the
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001600 // cursor and the screen needs to be redrawn.
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001601 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001602 normal_cmd(&oa, TRUE);
1603 }
1604 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001605#endif
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001606 {
1607#ifdef FEAT_TERMINAL
1608 skip_term_loop = FALSE;
1609#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001610 normal_cmd(&oa, TRUE);
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001611 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02001612 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001613 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001614
1615theend:
1616 current_oap = prev_oap;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001617}
1618
1619
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001620#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001621/*
1622 * Exit, but leave behind swap files for modified buffers.
1623 */
1624 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001625getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001626{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001627# if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001628 // Ignore SIGHUP, because a dropped connection causes a read error, which
1629 // makes Vim exit and then handling SIGHUP causes various reentrance
1630 // problems.
ichizok378447f2023-05-11 22:25:42 +01001631 mch_signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001632# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001633
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001634 ml_close_notmod(); // close all not-modified buffers
1635 ml_sync_all(FALSE, FALSE); // preserve all swap files
1636 ml_close_all(FALSE); // close all memfiles, without deleting
1637 getout(exitval); // exit Vim properly
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001638}
1639#endif
1640
1641
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001642/*
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001643 * Exit properly. This is the only way to exit Vim after startup has
1644 * succeeded. We are certain to exit here, no way to abort it.
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001645 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001646 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001647getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001648{
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001649 exiting = TRUE;
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001650#if defined(FEAT_EVAL)
Bram Moolenaar0bd052b2018-03-22 20:33:56 +01001651 ch_log(NULL, "Exiting...");
1652#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001653
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001654 // When running in Ex mode an error causes us to exit with a non-zero exit
1655 // code. POSIX requires this, although it's not 100% clear from the
1656 // standard.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001657 if (exmode_active)
1658 exitval += ex_exitval;
1659
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001660#ifdef FEAT_EVAL
1661 set_vim_var_type(VV_EXITING, VAR_NUMBER);
1662 set_vim_var_nr(VV_EXITING, exitval);
1663#endif
1664
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001665 // Position the cursor on the last screen line, below all the text
Bram Moolenaar7007e312021-03-27 12:11:33 +01001666 if (!is_not_a_term_or_gui())
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001667 windgoto((int)Rows - 1, 0);
1668
Bram Moolenaar58779852022-09-06 18:31:14 +01001669#ifdef FEAT_EVAL
1670 // Invoked all deferred functions in the function stack.
1671 invoke_all_defer();
1672#endif
1673
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001674#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001675 // Optionally print hashtable efficiency.
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001676 hash_debug_results();
1677#endif
1678
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001679#ifdef FEAT_GUI
1680 msg_didany = FALSE;
1681#endif
1682
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001683 if (v_dying <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001684 {
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001685 tabpage_T *tp;
1686 tabpage_T *next_tp;
1687 buf_T *buf;
1688 win_T *wp;
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001689 int unblock = 0;
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001690
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001691 // Trigger BufWinLeave for all windows, but only once per buffer.
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001692 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001693 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001694 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001695 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001696 {
Christian Brabandtfc682992023-09-03 20:20:52 +02001697 if (wp->w_buffer == NULL || !buf_valid(wp->w_buffer))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001698 // Autocmd must have close the buffer already, skip.
Bram Moolenaar802418d2013-01-17 14:00:11 +01001699 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001700 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001701 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001702 {
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001703 bufref_T bufref;
1704
1705 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001706 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1707 buf->b_fname, FALSE, buf);
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001708 if (bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001709 CHANGEDTICK(buf) = -1; // note we did it already
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001710
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001711 // start all over, autocommands may mess up the lists
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001712 next_tp = first_tabpage;
1713 break;
1714 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001715 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001716 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001717
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001718 // Trigger BufUnload for buffers that are loaded
Bram Moolenaar29323592016-07-24 22:04:11 +02001719 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001720 if (buf->b_ml.ml_mfp != NULL)
1721 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001722 bufref_T bufref;
1723
1724 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001725 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001726 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001727 if (!bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001728 // autocmd deleted the buffer
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001729 break;
1730 }
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001731
1732 // deathtrap() blocks autocommands, but we do want to trigger
1733 // VimLeavePre.
1734 if (is_autocmd_blocked())
1735 {
1736 unblock_autocmds();
1737 ++unblock;
1738 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001739 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001740 if (unblock)
1741 block_autocmds();
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001742 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001743
1744#ifdef FEAT_VIMINFO
Milly6eca04e2024-10-21 22:20:51 +02001745 if (
1746# ifdef EXITFREE
1747 entered_free_all_mem == FALSE &&
1748# endif
1749 *p_viminfo != NUL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001750 // Write out the registers, history, marks etc, to the viminfo file
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001751 write_viminfo(NULL, FALSE);
1752#endif
1753
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001754 if (v_dying <= 1)
Bram Moolenaarc7226682019-08-17 16:33:23 +02001755 {
1756 int unblock = 0;
1757
1758 // deathtrap() blocks autocommands, but we do want to trigger VimLeave.
1759 if (is_autocmd_blocked())
1760 {
1761 unblock_autocmds();
1762 ++unblock;
1763 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001764 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc7226682019-08-17 16:33:23 +02001765 if (unblock)
1766 block_autocmds();
1767 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001768
Bram Moolenaar05159a02005-02-26 23:04:13 +00001769#ifdef FEAT_PROFILE
1770 profile_dump();
1771#endif
1772
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001773 if (did_emsg
1774#ifdef FEAT_GUI
1775 || (gui.in_use && msg_didany && p_verbose > 0)
1776#endif
1777 )
1778 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001779 // give the user a chance to read the (error) message
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001780 no_wait_return = FALSE;
Volodymyr Kot7f0c4b42021-11-21 12:27:13 +00001781 wait_return(FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001782 }
1783
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001784 // Position the cursor again, the autocommands may have moved it
Bram Moolenaar7007e312021-03-27 12:11:33 +01001785 if (!is_not_a_term_or_gui())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001786 windgoto((int)Rows - 1, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001787
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001788#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001789 job_stop_on_exit();
1790#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001791#ifdef FEAT_LUA
1792 lua_end();
1793#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001794#ifdef FEAT_MZSCHEME
1795 mzscheme_end();
1796#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001797#ifdef FEAT_TCL
1798 tcl_end();
1799#endif
1800#ifdef FEAT_RUBY
1801 ruby_end();
1802#endif
1803#ifdef FEAT_PYTHON
1804 python_end();
1805#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001806#ifdef FEAT_PYTHON3
1807 python3_end();
1808#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001809#ifdef FEAT_PERL
1810 perl_end();
1811#endif
1812#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1813 iconv_end();
1814#endif
1815#ifdef FEAT_NETBEANS_INTG
1816 netbeans_end();
1817#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001818#ifdef FEAT_CSCOPE
1819 cs_end();
1820#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001821#ifdef FEAT_EVAL
1822 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001823 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001824#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001825#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +02001826 free_cmd_argsW();
1827#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001828
1829 mch_exit(exitval);
1830}
1831
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001832/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001833 * Get the name of the display, before gui_prepare() removes it from
1834 * argv[]. Used for the xterm-clipboard display.
1835 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001836 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001837 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001838 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001839early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001840{
Bram Moolenaar03005972008-11-20 13:12:36 +00001841#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1842 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001843 int argc = parmp->argc;
1844 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001845 int i;
1846
1847 for (i = 1; i < argc; i++)
1848 {
1849 if (STRCMP(argv[i], "--") == 0)
1850 break;
1851# ifdef FEAT_XCLIPBOARD
1852 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001853# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001854 || STRICMP(argv[i], "--display") == 0
1855# endif
1856 )
1857 {
1858 if (i == argc - 1)
1859 mainerr_arg_missing((char_u *)argv[i]);
1860 xterm_display = argv[++i];
1861 }
1862# endif
1863# ifdef FEAT_CLIENTSERVER
1864 else if (STRICMP(argv[i], "--servername") == 0)
1865 {
1866 if (i == argc - 1)
1867 mainerr_arg_missing((char_u *)argv[i]);
1868 parmp->serverName_arg = (char_u *)argv[++i];
1869 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001870 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001871 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001872 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001873 {
1874 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001875# ifdef FEAT_GUI
1876 if (strstr(argv[i], "-wait") != 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001877 // don't fork() when starting the GUI to edit files ourself
Bram Moolenaareb94e552006-03-11 21:35:11 +00001878 gui.dofork = FALSE;
1879# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001880 }
1881# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001882
Bram Moolenaar4f974752019-02-17 17:44:42 +01001883# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
1884# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001885 else if (STRICMP(argv[i], "--windowid") == 0)
1886# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001887 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001888# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001889 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001890 long_u id;
1891 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001892
1893 if (i == argc - 1)
1894 mainerr_arg_missing((char_u *)argv[i]);
1895 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001896 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001897 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001898 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001899 if (count != 1)
1900 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1901 else
Bram Moolenaar4f974752019-02-17 17:44:42 +01001902# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001903 win_socket_id = id;
1904# else
1905 gtk_socket_id = id;
1906# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001907 i++;
1908 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001909# endif
1910# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001911 else if (STRICMP(argv[i], "--echo-wid") == 0)
1912 echo_wid_arg = TRUE;
1913# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001914# ifndef FEAT_NETBEANS_INTG
1915 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001916 {
1917 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1918 mch_exit(2);
1919 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001920# endif
1921
Bram Moolenaar58d98232005-07-23 22:25:46 +00001922 }
1923#endif
1924}
1925
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001926#ifndef NO_VIM_MAIN
1927/*
1928 * Get a (optional) count for a Vim argument.
1929 */
1930 static int
1931get_number_arg(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001932 char_u *p, // pointer to argument
1933 int *idx, // index in argument, is incremented
1934 int def) // default value
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001935{
1936 if (vim_isdigit(p[*idx]))
1937 {
1938 def = atoi((char *)&(p[*idx]));
1939 while (vim_isdigit(p[*idx]))
1940 *idx = *idx + 1;
1941 }
1942 return def;
1943}
1944
1945/*
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001946 * Check for: [r][e][g][vi|vim|view][diff][ex[im]] (sort of)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001947 * If the executable name starts with "r" we disable shell commands.
1948 * If the next character is "e" we run in Easy mode.
1949 * If the next character is "g" we run the GUI version.
1950 * If the next characters are "view" we start in readonly mode.
1951 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1952 * If the next characters are "ex" we start in Ex mode. If it's followed
1953 * by "im" use improved Ex mode.
1954 */
1955 static void
1956parse_command_name(mparm_T *parmp)
1957{
1958 char_u *initstr;
1959
1960 initstr = gettail((char_u *)parmp->argv[0]);
1961
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001962#ifdef FEAT_EVAL
1963 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001964 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001965#endif
1966
1967 if (TOLOWER_ASC(initstr[0]) == 'r')
1968 {
1969 restricted = TRUE;
1970 ++initstr;
1971 }
1972
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001973 // Use evim mode for "evim" and "egvim", not for "editor".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001974 if (TOLOWER_ASC(initstr[0]) == 'e'
1975 && (TOLOWER_ASC(initstr[1]) == 'v'
1976 || TOLOWER_ASC(initstr[1]) == 'g'))
1977 {
1978#ifdef FEAT_GUI
1979 gui.starting = TRUE;
1980#endif
1981 parmp->evim_mode = TRUE;
1982 ++initstr;
1983 }
1984
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001985 // "gvim" starts the GUI. Also accept "Gvim" for MS-Windows.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001986 if (TOLOWER_ASC(initstr[0]) == 'g')
1987 {
1988 main_start_gui();
1989#ifdef FEAT_GUI
1990 ++initstr;
1991#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001992#ifdef GUI_MAY_SPAWN
1993 gui.dospawn = FALSE; // No need to spawn a new process.
1994#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001995 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001996#ifdef GUI_MAY_SPAWN
1997 else
1998 gui.dospawn = TRUE; // Not "gvim". Need to spawn gvim.exe.
1999#endif
2000
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02002001
2002 if (STRNICMP(initstr, "view", 4) == 0)
2003 {
2004 readonlymode = TRUE;
2005 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002006 p_uc = 10000; // don't update very often
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02002007 initstr += 4;
2008 }
2009 else if (STRNICMP(initstr, "vim", 3) == 0)
2010 initstr += 3;
2011
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002012 // Catch "[r][g]vimdiff" and "[r][g]viewdiff".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02002013 if (STRICMP(initstr, "diff") == 0)
2014 {
2015#ifdef FEAT_DIFF
2016 parmp->diff_mode = TRUE;
2017#else
2018 mch_errmsg(_("This Vim was not compiled with the diff feature."));
2019 mch_errmsg("\n");
2020 mch_exit(2);
2021#endif
2022 }
2023
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02002024 // Checking for "ex" here may catch some weird names, such as "vimex" or
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002025 // "viewex", we assume the user knows that.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02002026 if (STRNICMP(initstr, "ex", 2) == 0)
2027 {
2028 if (STRNICMP(initstr + 2, "im", 2) == 0)
2029 exmode_active = EXMODE_VIM;
2030 else
2031 exmode_active = EXMODE_NORMAL;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002032 change_compatible(TRUE); // set 'compatible'
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02002033 }
2034}
2035
Bram Moolenaar58d98232005-07-23 22:25:46 +00002036/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002037 * Scan the command line arguments.
2038 */
2039 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002040command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002041{
2042 int argc = parmp->argc;
2043 char **argv = parmp->argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002044 int argv_idx; // index in argv[n][]
2045 int had_minmin = FALSE; // found "--" argument
2046 int want_argument; // option argument with argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002047 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00002048 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002049 long n;
2050
2051 --argc;
2052 ++argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002053 argv_idx = 1; // active option letter is argv[0][argv_idx]
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002054 while (argc > 0)
2055 {
2056 /*
2057 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
2058 */
2059 if (argv[0][0] == '+' && !had_minmin)
2060 {
2061 if (parmp->n_commands >= MAX_ARG_CMDS)
2062 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002063 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002064 if (argv[0][1] == NUL)
2065 parmp->commands[parmp->n_commands++] = (char_u *)"$";
2066 else
2067 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
2068 }
2069
2070 /*
2071 * Optional argument.
2072 */
2073 else if (argv[0][0] == '-' && !had_minmin)
2074 {
2075 want_argument = FALSE;
2076 c = argv[0][argv_idx++];
2077#ifdef VMS
2078 /*
2079 * VMS only uses upper case command lines. Interpret "-X" as "-x"
2080 * and "-/X" as "-X".
2081 */
2082 if (c == '/')
2083 {
2084 c = argv[0][argv_idx++];
2085 c = TOUPPER_ASC(c);
2086 }
2087 else
2088 c = TOLOWER_ASC(c);
2089#endif
2090 switch (c)
2091 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002092 case NUL: // "vim -" read from stdin
2093 // "ex -" silent mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002094 if (exmode_active)
2095 silent_mode = TRUE;
2096 else
2097 {
2098 if (parmp->edit_type != EDIT_NONE)
2099 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2100 parmp->edit_type = EDIT_STDIN;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002101 read_cmd_fd = 2; // read from stderr instead of stdin
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002102 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002103 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002104 break;
2105
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002106 case '-': // "--" don't take any more option arguments
2107 // "--help" give help message
2108 // "--version" give version message
2109 // "--clean" clean context
2110 // "--literal" take files literally
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002111 // "--startuptime fname" write timing info
2112 // "--log fname" start logging early
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002113 // "--nofork" don't fork
2114 // "--not-a-term" don't warn for not a term
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002115 // "--gui-dialog-file fname" write dialog text
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002116 // "--ttyfail" exit if not a term
2117 // "--noplugin[s]" skip plugins
2118 // "--cmd <cmd>" execute cmd before vimrc
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002119 if (STRICMP(argv[0] + argv_idx, "help") == 0)
2120 usage();
2121 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
2122 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002123 Columns = 80; // need to init Columns
2124 info_message = TRUE; // use mch_msg(), not mch_errmsg()
K.Takata33b25d12022-01-13 16:06:45 +00002125#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar3b678042021-02-11 19:08:05 +01002126 gui.starting = FALSE; // not starting GUI, will exit
Bram Moolenaar0bcadf12021-02-11 19:18:58 +01002127#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002128 list_version();
2129 msg_putchar('\n');
2130 msg_didout = FALSE;
2131 mch_exit(0);
2132 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002133 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
2134 {
2135 parmp->use_vimrc = (char_u *)"DEFAULTS";
Bram Moolenaar62dd4522018-03-14 21:20:02 +01002136#ifdef FEAT_GUI
2137 use_gvimrc = (char_u *)"NONE";
2138#endif
Bram Moolenaar07268702018-03-01 21:57:32 +01002139 parmp->clean = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002140 set_option_value_give_err((char_u *)"vif",
2141 0L, (char_u *)"NONE", 0);
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002142 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002143 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
2144 {
Bram Moolenaar53076832015-12-31 19:53:21 +01002145#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002146 parmp->literal = TRUE;
2147#endif
2148 }
2149 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
2150 {
2151#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002152 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002153#endif
2154 }
2155 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
2156 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002157 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
2158 parmp->not_a_term = TRUE;
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002159 else if (STRNICMP(argv[0] + argv_idx, "gui-dialog-file", 15)
2160 == 0)
2161 {
2162 want_argument = TRUE;
2163 argv_idx += 15;
2164 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002165 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
2166 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002167 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
2168 {
2169 want_argument = TRUE;
2170 argv_idx += 3;
2171 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002172 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
2173 {
2174 want_argument = TRUE;
2175 argv_idx += 11;
2176 }
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002177 else if (STRNICMP(argv[0] + argv_idx, "log", 3) == 0)
2178 {
2179 want_argument = TRUE;
2180 argv_idx += 3;
2181 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002182#ifdef FEAT_CLIENTSERVER
2183 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002184 ; // already processed -- no arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002185 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
2186 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
2187 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002188 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002189 if (argc > 1)
2190 {
2191 --argc;
2192 ++argv;
2193 }
2194 }
2195#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002196#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002197# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002198 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002199# else
2200 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
2201# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002202 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002203 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002204 if (argc > 1)
2205 {
2206 --argc;
2207 ++argv;
2208 }
2209 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00002210#endif
2211#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002212 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
2213 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002214 // already processed, skip
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002215 }
2216#endif
2217 else
2218 {
2219 if (argv[0][argv_idx])
2220 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2221 had_minmin = TRUE;
2222 }
2223 if (!want_argument)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002224 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002225 break;
2226
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002227 case 'A': // "-A" start in Arabic mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002228#ifdef FEAT_ARABIC
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002229 set_option_value_give_err((char_u *)"arabic", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002230#else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002231 mch_errmsg(_(e_arabic_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002232 mch_exit(2);
2233#endif
2234 break;
2235
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002236 case 'b': // "-b" binary mode
2237 // Needs to be effective before expanding file names, because
2238 // for Win32 this makes us edit a shortcut file itself,
2239 // instead of the file it links to.
Bram Moolenaar231334e2005-07-25 20:46:57 +00002240 set_options_bin(curbuf->b_p_bin, 1, 0);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002241 curbuf->b_p_bin = 1; // binary file I/O
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002242 break;
2243
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002244 case 'C': // "-C" Compatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002245 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02002246 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002247 break;
2248
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002249 case 'e': // "-e" Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002250 exmode_active = EXMODE_NORMAL;
2251 break;
2252
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002253 case 'E': // "-E" Improved Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002254 exmode_active = EXMODE_VIM;
2255 break;
2256
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002257 case 'f': // "-f" GUI: run in foreground. Amiga: open
2258 // window directly, not with newcli
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002259#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002260 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002261#endif
2262 break;
2263
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002264 case 'g': // "-g" start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002265 main_start_gui();
2266 break;
2267
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002268 case 'F': // "-F" was for Farsi mode
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002269 mch_errmsg(_(e_farsi_support_has_been_removed));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002270 mch_exit(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002271 break;
2272
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002273 case '?': // "-?" give help message (for MS-Windows)
2274 case 'h': // "-h" give help message
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002275#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002276 // Tell usage() to exit for "gvim".
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002277 gui.starting = FALSE;
2278#endif
2279 usage();
2280 break;
2281
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002282 case 'H': // "-H" start in Hebrew mode: rl + hkmap set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002283#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002284 p_hkmap = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002285 set_option_value_give_err((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002286#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002287 mch_errmsg(_(e_hebrew_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002288 mch_exit(2);
2289#endif
2290 break;
2291
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002292 case 'l': // "-l" lisp mode, 'lisp' and 'showmatch' on
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002293 set_option_value_give_err((char_u *)"lisp", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002294 p_sm = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002295 break;
2296
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002297 case 'M': // "-M" no changes or writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002298 reset_modifiable();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002299 // FALLTHROUGH
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002300
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002301 case 'm': // "-m" no writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002302 p_write = FALSE;
2303 break;
2304
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002305 case 'y': // "-y" easy mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002306#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002307 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002308#endif
2309 parmp->evim_mode = TRUE;
2310 break;
2311
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002312 case 'N': // "-N" Nocompatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002313 change_compatible(FALSE);
2314 break;
2315
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002316 case 'n': // "-n" no swap file
Bram Moolenaar67c53842010-05-22 18:28:27 +02002317#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002318 // checking for "-nb", netbeans parameters
Bram Moolenaar67c53842010-05-22 18:28:27 +02002319 if (argv[0][argv_idx] == 'b')
2320 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002321 netbeansArg = argv[0];
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002322 argv_idx = -1; // skip to next argument
Bram Moolenaar67c53842010-05-22 18:28:27 +02002323 }
2324 else
2325#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002326 parmp->no_swap_file = TRUE;
2327 break;
2328
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002329 case 'p': // "-p[N]" open N tab pages
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002330#ifdef TARGET_API_MAC_OSX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002331 // For some reason on MacOS X, an argument like:
2332 // -psn_0_10223617 is passed in when invoke from Finder
2333 // or with the 'open' command
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002334 if (argv[0][argv_idx] == 's')
2335 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002336 argv_idx = -1; // bypass full -psn
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002337 main_start_gui();
2338 break;
2339 }
2340#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002341 // default is 0: open window for each file
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002342 parmp->window_count = get_number_arg((char_u *)argv[0],
2343 &argv_idx, 0);
2344 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002345 break;
2346
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002347 case 'o': // "-o[N]" open N horizontal split windows
2348 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002349 parmp->window_count = get_number_arg((char_u *)argv[0],
2350 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002351 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002352 break;
2353
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002354 case 'O': // "-O[N]" open N vertical split windows
2355 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002356 parmp->window_count = get_number_arg((char_u *)argv[0],
2357 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002358 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002359 break;
2360
2361#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002362 case 'q': // "-q" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002363 if (parmp->edit_type != EDIT_NONE)
2364 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2365 parmp->edit_type = EDIT_QF;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002366 if (argv[0][argv_idx]) // "-q{errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002367 {
2368 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2369 argv_idx = -1;
2370 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002371 else if (argc > 1) // "-q {errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002372 want_argument = TRUE;
2373 break;
2374#endif
2375
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002376 case 'R': // "-R" readonly mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002377 readonlymode = TRUE;
2378 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002379 p_uc = 10000; // don't update very often
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002380 break;
2381
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002382 case 'r': // "-r" recovery mode
2383 case 'L': // "-L" recovery mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002384 recoverymode = 1;
2385 break;
2386
2387 case 's':
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002388 if (exmode_active) // "-s" silent (batch) mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002389 silent_mode = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002390 else // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002391 want_argument = TRUE;
2392 break;
2393
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002394 case 't': // "-t {tag}" or "-t{tag}" jump to tag
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002395 if (parmp->edit_type != EDIT_NONE)
2396 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2397 parmp->edit_type = EDIT_TAG;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002398 if (argv[0][argv_idx]) // "-t{tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002399 {
2400 parmp->tagname = (char_u *)argv[0] + argv_idx;
2401 argv_idx = -1;
2402 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002403 else // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002404 want_argument = TRUE;
2405 break;
2406
2407#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002408 case 'D': // "-D" Debugging
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002409 parmp->use_debug_break_level = 9999;
2410 break;
2411#endif
2412#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002413 case 'd': // "-d" 'diff'
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002414# ifdef AMIGA
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002415 // check for "-dev {device}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002416 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2417 want_argument = TRUE;
2418 else
2419# endif
2420 parmp->diff_mode = TRUE;
2421 break;
2422#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002423 case 'V': // "-V{N}" Verbose level
2424 // default is 10: a little bit verbose
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002425 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2426 if (argv[0][argv_idx] != NUL)
2427 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002428 set_option_value_give_err((char_u *)"verbosefile",
2429 0L, (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002430 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002431 }
2432 break;
2433
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002434 case 'v': // "-v" Vi-mode (as if called "vi")
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002435 exmode_active = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002436#if defined(FEAT_GUI) && !defined(VIMDLL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002437 gui.starting = FALSE; // don't start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002438#endif
2439 break;
2440
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002441 case 'w': // "-w{number}" set window height
2442 // "-w {scriptout}" write to script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002443 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2444 {
2445 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002446 set_option_value_give_err((char_u *)"window", n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002447 break;
2448 }
2449 want_argument = TRUE;
2450 break;
2451
2452#ifdef FEAT_CRYPT
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002453 case 'x': // "-x" encrypted reading/writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002454 parmp->ask_for_key = TRUE;
2455 break;
2456#endif
2457
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002458 case 'X': // "-X" don't connect to X server
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002459#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2460 x_no_connect = TRUE;
2461#endif
2462 break;
2463
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002464 case 'Z': // "-Z" restricted mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002465 restricted = TRUE;
2466 break;
2467
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002468 case 'c': // "-c{command}" or "-c {command}" execute
2469 // command
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002470 if (argv[0][argv_idx] != NUL)
2471 {
2472 if (parmp->n_commands >= MAX_ARG_CMDS)
2473 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002474 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2475 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002476 argv_idx = -1;
2477 break;
2478 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002479 // FALLTHROUGH
2480 case 'S': // "-S {file}" execute Vim script
2481 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002482#ifndef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002483 case 'd': // "-d {device}" device (for Amiga)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002484#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002485 case 'T': // "-T {terminal}" terminal name
2486 case 'u': // "-u {vimrc}" vim inits file
2487 case 'U': // "-U {gvimrc}" gvim inits file
2488 case 'W': // "-W {scriptout}" overwrite
Bram Moolenaar4f974752019-02-17 17:44:42 +01002489#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002490 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002491#endif
2492 want_argument = TRUE;
2493 break;
2494
2495 default:
2496 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2497 }
2498
2499 /*
2500 * Handle option arguments with argument.
2501 */
2502 if (want_argument)
2503 {
2504 /*
2505 * Check for garbage immediately after the option letter.
2506 */
2507 if (argv[0][argv_idx] != NUL)
2508 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2509
2510 --argc;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002511 if (argc < 1 && c != 'S') // -S has an optional argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002512 mainerr_arg_missing((char_u *)argv[0]);
2513 ++argv;
2514 argv_idx = -1;
2515
2516 switch (c)
2517 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002518 case 'c': // "-c {command}" execute command
2519 case 'S': // "-S {file}" execute Vim script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002520 if (parmp->n_commands >= MAX_ARG_CMDS)
2521 mainerr(ME_EXTRA_CMD, NULL);
2522 if (c == 'S')
2523 {
2524 char *a;
2525
2526 if (argc < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002527 // "-S" without argument: use default session file
2528 // name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002529 a = SESSION_FILE;
2530 else if (argv[0][0] == '-')
2531 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002532 // "-S" followed by another option: use default
2533 // session file name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002534 a = SESSION_FILE;
2535 ++argc;
2536 --argv;
2537 }
2538 else
2539 a = argv[0];
Bram Moolenaar964b3742019-05-24 18:54:09 +02002540 p = alloc(STRLEN(a) + 4);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002541 if (p == NULL)
2542 mch_exit(2);
2543 sprintf((char *)p, "so %s", a);
2544 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2545 parmp->commands[parmp->n_commands++] = p;
2546 }
2547 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002548 parmp->commands[parmp->n_commands++] =
2549 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002550 break;
2551
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002552 case '-':
2553 if (argv[-1][2] == 'c')
2554 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002555 // "--cmd {command}" execute command
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002556 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2557 mainerr(ME_EXTRA_CMD, NULL);
2558 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002559 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002560 }
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002561 // --gui-dialog-file fname
2562 if (argv[-1][2] == 'g')
2563 {
2564 // without GUI ignore the argument
2565#ifdef FEAT_GUI
2566 parmp->gui_dialog_file = (char_u *)argv[0];
2567#endif
2568 }
2569
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002570 // "--startuptime <file>" already handled
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002571 // "--log <file>" already handled
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002572 break;
2573
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002574 // case 'd': -d {device} is handled in mch_check_win() for the
2575 // Amiga
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002576
2577#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002578 case 'q': // "-q {errorfile}" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002579 parmp->use_ef = (char_u *)argv[0];
2580 break;
2581#endif
2582
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002583 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002584 set_option_value_give_err((char_u *)"vif",
2585 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002586 break;
2587
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002588 case 's': // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002589 if (scriptin[0] != NULL)
2590 {
2591scripterror:
2592 mch_errmsg(_("Attempt to open script file again: \""));
2593 mch_errmsg(argv[-1]);
2594 mch_errmsg(" ");
2595 mch_errmsg(argv[0]);
2596 mch_errmsg("\"\n");
2597 mch_exit(2);
2598 }
2599 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2600 {
2601 mch_errmsg(_("Cannot open for reading: \""));
2602 mch_errmsg(argv[0]);
2603 mch_errmsg("\"\n");
2604 mch_exit(2);
2605 }
2606 if (save_typebuf() == FAIL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002607 mch_exit(2); // out of memory
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002608 break;
2609
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002610 case 't': // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002611 parmp->tagname = (char_u *)argv[0];
2612 break;
2613
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002614 case 'T': // "-T {terminal}" terminal name
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002615 /*
2616 * The -T term argument is always available and when
2617 * HAVE_TERMLIB is supported it overrides the environment
2618 * variable TERM.
2619 */
2620#ifdef FEAT_GUI
2621 if (term_is_gui((char_u *)argv[0]))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002622 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002623 else
2624#endif
2625 parmp->term = (char_u *)argv[0];
2626 break;
2627
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002628 case 'u': // "-u {vimrc}" vim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002629 parmp->use_vimrc = (char_u *)argv[0];
2630 break;
2631
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002632 case 'U': // "-U {gvimrc}" gvim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002633#ifdef FEAT_GUI
2634 use_gvimrc = (char_u *)argv[0];
2635#endif
2636 break;
2637
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002638 case 'w': // "-w {nr}" 'window' value
2639 // "-w {scriptout}" append to script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002640 if (vim_isdigit(*((char_u *)argv[0])))
2641 {
2642 argv_idx = 0;
2643 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002644 set_option_value_give_err((char_u *)"window",
2645 n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002646 argv_idx = -1;
2647 break;
2648 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002649 // FALLTHROUGH
2650 case 'W': // "-W {scriptout}" overwrite script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002651 if (scriptout != NULL)
2652 goto scripterror;
2653 if ((scriptout = mch_fopen(argv[0],
2654 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2655 {
2656 mch_errmsg(_("Cannot open for script output: \""));
2657 mch_errmsg(argv[0]);
2658 mch_errmsg("\"\n");
2659 mch_exit(2);
2660 }
2661 break;
2662
Bram Moolenaar4f974752019-02-17 17:44:42 +01002663#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002664 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002665 gui_mch_set_parent(argv[0]);
2666 break;
2667#endif
2668 }
2669 }
2670 }
2671
2672 /*
2673 * File name argument.
2674 */
2675 else
2676 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002677 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002678
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002679 // Check for only one type of editing.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002680 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2681 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2682 parmp->edit_type = EDIT_FILE;
2683
2684#ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002685 // Remember if the argument was a full path before changing
2686 // slashes to backslashes.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002687 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2688 parmp->full_path = TRUE;
2689#endif
2690
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002691 // Add the file to the global argument list.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002692 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2693 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2694 mch_exit(2);
2695#ifdef FEAT_DIFF
2696 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2697 && !mch_isdir(alist_name(&GARGLIST[0])))
2698 {
2699 char_u *r;
2700
2701 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2702 if (r != NULL)
2703 {
2704 vim_free(p);
2705 p = r;
2706 }
2707 }
2708#endif
K.Takatab247e062022-02-07 10:45:23 +00002709#ifdef __CYGWIN__
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002710 /*
2711 * If vim is invoked by non-Cygwin tools, convert away any
2712 * DOS paths, so things like .swp files are created correctly.
2713 * Look for evidence of non-Cygwin paths before we bother.
2714 * This is only for when using the Unix files.
2715 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002716 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002717 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002718 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002719
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002720# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002721 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002722# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002723 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002724# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002725 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002726 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002727 if (p == NULL)
2728 mch_exit(2);
2729 }
2730#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002731
2732#ifdef USE_FNAME_CASE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002733 // Make the case of the file name match the actual file.
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002734 fname_case(p, 0);
2735#endif
2736
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002737 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002738#ifdef EXPAND_FILENAMES
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002739 parmp->literal ? 2 : 0 // add buffer nr after exp.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002740#else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002741 2 // add buffer number now and use curbuf
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002742#endif
2743 );
2744
Bram Moolenaar4f974752019-02-17 17:44:42 +01002745#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002746 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002747 // Remember this argument has been added to the argument list.
2748 // Needed when 'encoding' is changed.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002749 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002750# ifdef FEAT_DIFF
2751 parmp->diff_mode
2752# else
2753 FALSE
2754# endif
2755 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002756 }
2757#endif
2758 }
2759
2760 /*
2761 * If there are no more letters after the current "-", go to next
2762 * argument. argv_idx is set to -1 when the current argument is to be
2763 * skipped.
2764 */
2765 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2766 {
2767 --argc;
2768 ++argv;
2769 argv_idx = 1;
2770 }
2771 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002772
2773#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002774 // If there is a "+123" or "-c" command, set v:swapcommand to the first
2775 // one.
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002776 if (parmp->n_commands > 0)
2777 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002778 p = alloc(STRLEN(parmp->commands[0]) + 3);
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002779 if (p != NULL)
2780 {
2781 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2782 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2783 vim_free(p);
2784 }
2785 }
2786#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002787}
2788
2789/*
2790 * Print a warning if stdout is not a terminal.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +01002791 * When starting in Ex mode and commands come from a file, set silent_mode.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002792 */
2793 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002794check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002795{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002796 int input_isatty; // is active input a terminal?
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002797
2798 input_isatty = mch_input_isatty();
2799 if (exmode_active)
2800 {
2801 if (!input_isatty)
2802 silent_mode = TRUE;
2803 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002804 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002805#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002806 // don't want the delay when started from the desktop
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002807 && !gui.starting
2808#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002809 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002810 {
2811#ifdef NBDEBUG
2812 /*
2813 * This shouldn't be necessary. But if I run netbeans with the log
2814 * output coming to the console and XOpenDisplay fails, I get vim
2815 * trying to start with input/output to my console tty. This fills my
2816 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002817 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002818 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002819 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002820 {
2821 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2822 exit(1);
2823 }
2824#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002825#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2826 if (
2827# ifdef VIMDLL
2828 !gui.starting &&
2829# endif
2830 is_cygpty_used())
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002831 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002832# if defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
Bram Moolenaar2a027452017-09-26 19:10:37 +02002833 && defined(FEAT_GETTEXT)
2834 char *s, *tofree = NULL;
2835
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002836 // Set the encoding of the error message based on $LC_ALL or
2837 // other environment variables instead of 'encoding'.
2838 // Note that the message is shown on a Cygwin terminal (e.g.
2839 // mintty) which encoding is based on $LC_ALL or etc., not the
2840 // current codepage used by normal Win32 console programs.
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002841 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002842 if (s == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002843 s = "utf-8"; // Use "utf-8" by default.
Bram Moolenaar2a027452017-09-26 19:10:37 +02002844 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2845 vim_free(tofree);
2846# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002847 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2848 exit(1);
2849 }
2850#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002851 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002852 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2853 if (!input_isatty)
2854 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2855 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002856 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2857 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002858 if (scriptin[0] == NULL)
Bram Moolenaareda1da02019-11-17 17:06:33 +01002859 ui_delay(2005L, TRUE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002860 TIME_MSG("Warning delay");
2861 }
2862}
2863
2864/*
2865 * Read text from stdin.
2866 */
2867 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002868read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002869{
2870 int i;
2871
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002872 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002873 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002874
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002875 no_wait_return = TRUE;
2876 i = msg_didany;
2877 set_buflisted(TRUE);
Bram Moolenaar204ade62020-10-11 14:58:46 +02002878
2879 // Create memfile and read from stdin.
Bram Moolenaar204ade62020-10-11 14:58:46 +02002880 (void)open_buffer(TRUE, NULL, 0);
2881
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002882 no_wait_return = FALSE;
2883 msg_didany = i;
2884 TIME_MSG("reading stdin");
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002885
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002886 check_swap_exists_action();
Bram Moolenaar8e1cbb52020-12-08 19:36:21 +01002887
2888#if !(defined(AMIGA) || defined(MACOS_X))
2889 // Dup stdin from stderr to read commands from, so that shell commands
2890 // work.
2891 // TODO: why is this needed, even though readfile() has done this?
2892 close(0);
2893 vim_ignored = dup(2);
2894#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002895}
2896
2897/*
2898 * Create the requested number of windows and edit buffers in them.
2899 * Also does recovery if "recoverymode" set.
2900 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002901 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002902create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002903{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002904 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002905 int done = 0;
2906
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002907 /*
2908 * Create the number of windows that was requested.
2909 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002910 if (parmp->window_count == -1) // was not set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002911 parmp->window_count = 1;
2912 if (parmp->window_count == 0)
2913 parmp->window_count = GARGCOUNT;
2914 if (parmp->window_count > 1)
2915 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002916 // Don't change the windows if there was a command in .vimrc that
2917 // already split some windows
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002918 if (parmp->window_layout == 0)
2919 parmp->window_layout = WIN_HOR;
2920 if (parmp->window_layout == WIN_TABS)
2921 {
2922 parmp->window_count = make_tabpages(parmp->window_count);
2923 TIME_MSG("making tab pages");
2924 }
2925 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002926 {
2927 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002928 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002929 TIME_MSG("making windows");
2930 }
2931 else
2932 parmp->window_count = win_count();
2933 }
2934 else
2935 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002936
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002937 if (recoverymode) // do recover
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002938 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002939 msg_scroll = TRUE; // scroll message up
Bram Moolenaar99499b12019-05-23 21:35:48 +02002940 ml_recover(TRUE);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002941 if (curbuf->b_ml.ml_mfp == NULL) // failed
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002942 getout(1);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002943 do_modelines(0); // do modelines
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002944 }
2945 else
2946 {
2947 /*
2948 * Open a buffer for windows that don't have one yet.
2949 * Commands in the .vimrc might have loaded a file or split the window.
2950 * Watch out for autocommands that delete a window.
2951 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002952 /*
2953 * Don't execute Win/Buf Enter/Leave autocommands here
2954 */
2955 ++autocmd_no_enter;
2956 ++autocmd_no_leave;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002957 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002958 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002959 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002960 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002961 {
2962 if (parmp->window_layout == WIN_TABS)
2963 goto_tabpage(1);
2964 else
2965 curwin = firstwin;
2966 }
2967 else if (parmp->window_layout == WIN_TABS)
2968 {
2969 if (curtab->tp_next == NULL)
2970 break;
2971 goto_tabpage(0);
2972 }
2973 else
2974 {
2975 if (curwin->w_next == NULL)
2976 break;
2977 curwin = curwin->w_next;
2978 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002979 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002980 curbuf = curwin->w_buffer;
2981 if (curbuf->b_ml.ml_mfp == NULL)
2982 {
2983#ifdef FEAT_FOLDING
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002984 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002985 if (p_fdls >= 0)
2986 curwin->w_p_fdl = p_fdls;
2987#endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002988 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002989 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002990
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002991 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002992
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002993 // create memfile, read file
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002994 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002995
Bram Moolenaar84212822006-11-07 21:59:47 +00002996 if (swap_exists_action == SEA_QUIT)
2997 {
2998 if (got_int || only_one_window())
2999 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003000 // abort selected or quit and only one window
3001 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00003002 getout(1);
3003 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003004 // We can't close the window, it would disturb what
3005 // happens next. Clear the file name and set the arg
3006 // index to -1 to delete it later.
Bram Moolenaar84212822006-11-07 21:59:47 +00003007 setfname(curbuf, NULL, NULL, FALSE);
3008 curwin->w_arg_idx = -1;
3009 swap_exists_action = SEA_NONE;
3010 }
3011 else
3012 handle_swap_exists(NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003013 dorewind = TRUE; // start again
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003014 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003015 ui_breakcheck();
3016 if (got_int)
3017 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003018 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003019 break;
3020 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003021 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003022 if (parmp->window_layout == WIN_TABS)
3023 goto_tabpage(1);
3024 else
3025 curwin = firstwin;
3026 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003027 --autocmd_no_enter;
3028 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003029 }
3030}
3031
Bram Moolenaar9810cfb2019-12-11 21:23:00 +01003032/*
3033 * If opened more than one window, start editing files in the other
3034 * windows. make_windows() has already opened the windows.
3035 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003036 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003037edit_buffers(
3038 mparm_T *parmp,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003039 char_u *cwd) // current working dir
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003040{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003041 int arg_idx; // index in argument list
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003042 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00003043 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003044 win_T *win;
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003045 char_u *p_shm_save = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003046
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003047 /*
3048 * Don't execute Win/Buf Enter/Leave autocommands here
3049 */
3050 ++autocmd_no_enter;
3051 ++autocmd_no_leave;
Bram Moolenaar84212822006-11-07 21:59:47 +00003052
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003053 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00003054 if (curwin->w_arg_idx == -1)
3055 {
3056 win_close(curwin, TRUE);
3057 advance = FALSE;
3058 }
3059
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003060 arg_idx = 1;
3061 for (i = 1; i < parmp->window_count; ++i)
3062 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02003063 if (cwd != NULL)
3064 mch_chdir((char *)cwd);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003065 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00003066 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003067 {
Bram Moolenaar84212822006-11-07 21:59:47 +00003068 ++arg_idx;
3069 win_close(curwin, TRUE);
3070 advance = FALSE;
3071 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003072 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003073
Bram Moolenaar84212822006-11-07 21:59:47 +00003074 if (advance)
3075 {
3076 if (parmp->window_layout == WIN_TABS)
3077 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003078 if (curtab->tp_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00003079 break;
3080 goto_tabpage(0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003081 // Temporarily reset 'shm' option to not print fileinfo when
3082 // loading the other buffers. This would overwrite the already
3083 // existing fileinfo for the first tab.
3084 if (i == 1)
3085 {
3086 char buf[100];
3087
3088 p_shm_save = vim_strsave(p_shm);
3089 vim_snprintf(buf, 100, "F%s", p_shm);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003090 set_option_value_give_err((char_u *)"shm",
3091 0L, (char_u *)buf, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003092 }
Bram Moolenaar84212822006-11-07 21:59:47 +00003093 }
3094 else
3095 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003096 if (curwin->w_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00003097 break;
3098 win_enter(curwin->w_next, FALSE);
3099 }
3100 }
3101 advance = TRUE;
3102
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003103 // Only open the file if there is no file in this window yet (that can
3104 // happen when .vimrc contains ":sall").
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003105 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
3106 {
3107 curwin->w_arg_idx = arg_idx;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003108 // Edit file from arg list, if there is one. When "Quit" selected
3109 // at the ATTENTION prompt close the window.
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003110 swap_exists_did_quit = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003111 (void)do_ecmd(0, arg_idx < GARGCOUNT
3112 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003113 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003114 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00003115 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003116 // abort or quit selected
Bram Moolenaar84212822006-11-07 21:59:47 +00003117 if (got_int || only_one_window())
3118 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003119 // abort selected and only one window
3120 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00003121 getout(1);
3122 }
3123 win_close(curwin, TRUE);
3124 advance = FALSE;
3125 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003126 if (arg_idx == GARGCOUNT - 1)
3127 arg_had_last = TRUE;
3128 ++arg_idx;
3129 }
3130 ui_breakcheck();
3131 if (got_int)
3132 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003133 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003134 break;
3135 }
3136 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003137
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003138 if (p_shm_save != NULL)
3139 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003140 set_option_value_give_err((char_u *)"shm", 0L, p_shm_save, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003141 vim_free(p_shm_save);
3142 }
3143
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003144 if (parmp->window_layout == WIN_TABS)
3145 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003146 --autocmd_no_enter;
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003147
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003148 // make the first window the current window
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003149 win = firstwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003150#if defined(FEAT_QUICKFIX)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003151 // Avoid making a preview window the current window.
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003152 while (win->w_p_pvw)
3153 {
3154 win = win->w_next;
3155 if (win == NULL)
3156 {
3157 win = firstwin;
3158 break;
3159 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003160 }
3161#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003162 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003163
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003164 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003165 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003166 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003167 win_equal(curwin, FALSE, 'b'); // adjust heights
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003168}
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003169
3170/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003171 * Execute the commands from --cmd arguments "cmds[cnt]".
3172 */
3173 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003174exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003175{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003176 char_u **cmds = parmp->pre_commands;
3177 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003178 int i;
ichizok7e5fe382023-04-15 13:17:50 +01003179 ESTACK_CHECK_DECLARATION;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003180
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003181 if (cnt <= 0)
3182 return;
3183
3184 curwin->w_cursor.lnum = 0; // just in case..
3185 estack_push(ETYPE_ARGS, (char_u *)_("pre-vimrc command line"), 0);
ichizok7e5fe382023-04-15 13:17:50 +01003186 ESTACK_CHECK_SETUP;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003187# ifdef FEAT_EVAL
ichizok7e5fe382023-04-15 13:17:50 +01003188 current_sctx.sc_sid = SID_CMDARG;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003189# endif
ichizok7e5fe382023-04-15 13:17:50 +01003190 for (i = 0; i < cnt; ++i)
3191 do_cmdline_cmd(cmds[i]);
3192 ESTACK_CHECK_NOW;
3193 estack_pop();
Bram Moolenaar58d98232005-07-23 22:25:46 +00003194# ifdef FEAT_EVAL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003195 current_sctx.sc_sid = 0;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003196# endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003197 TIME_MSG("--cmd commands");
Bram Moolenaar58d98232005-07-23 22:25:46 +00003198}
3199
3200/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003201 * Execute "+", "-c" and "-S" arguments.
3202 */
3203 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003204exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003205{
3206 int i;
ichizok7e5fe382023-04-15 13:17:50 +01003207 ESTACK_CHECK_DECLARATION;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003208
3209 /*
3210 * We start commands on line 0, make "vim +/pat file" match a
3211 * pattern on line 1. But don't move the cursor when an autocommand
3212 * with g`" was used.
3213 */
3214 msg_scroll = TRUE;
3215 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
3216 curwin->w_cursor.lnum = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003217 estack_push(ETYPE_ARGS, (char_u *)"command line", 0);
ichizok7e5fe382023-04-15 13:17:50 +01003218 ESTACK_CHECK_SETUP;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003219#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003220 current_sctx.sc_sid = SID_CARG;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003221 current_sctx.sc_seq = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003222#endif
3223 for (i = 0; i < parmp->n_commands; ++i)
3224 {
3225 do_cmdline_cmd(parmp->commands[i]);
3226 if (parmp->cmds_tofree[i])
3227 vim_free(parmp->commands[i]);
3228 }
ichizok7e5fe382023-04-15 13:17:50 +01003229 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003230 estack_pop();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003231#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003232 current_sctx.sc_sid = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003233#endif
3234 if (curwin->w_cursor.lnum == 0)
3235 curwin->w_cursor.lnum = 1;
3236
3237 if (!exmode_active)
3238 msg_scroll = FALSE;
3239
3240#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003241 // When started with "-q errorfile" jump to first error again.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003242 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003243 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003244#endif
3245 TIME_MSG("executing command arguments");
3246}
3247
3248/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003249 * Source startup scripts.
3250 */
3251 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003252source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003253{
3254 int i;
3255
3256 /*
3257 * For "evim" source evim.vim first of all, so that the user can overrule
3258 * any things he doesn't like.
3259 */
3260 if (parmp->evim_mode)
3261 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003263 TIME_MSG("source evim file");
3264 }
3265
3266 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003267 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00003268 * nothing else.
3269 */
3270 if (parmp->use_vimrc != NULL)
3271 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003272 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003273 {
3274 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE, NULL)
3275 != OK)
Bram Moolenaar50809a42023-05-20 16:39:07 +01003276 emsg(_(e_failed_to_source_defaults));
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003277 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003278 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003279 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003280 {
3281#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003282 if (use_gvimrc == NULL) // don't load gvimrc either
Bram Moolenaar58d98232005-07-23 22:25:46 +00003283 use_gvimrc = parmp->use_vimrc;
3284#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003285 }
3286 else
3287 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00003289 semsg(_(e_cannot_read_from_str_2), parmp->use_vimrc);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003290 }
3291 }
3292 else if (!silent_mode)
3293 {
3294#ifdef AMIGA
3295 struct Process *proc = (struct Process *)FindTask(0L);
3296 APTR save_winptr = proc->pr_WindowPtr;
3297
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003298 // Avoid a requester here for a volume that doesn't exist.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003299 proc->pr_WindowPtr = (APTR)-1L;
3300#endif
3301
3302 /*
3303 * Get system wide defaults, if the file name is defined.
3304 */
3305#ifdef SYS_VIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003307#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003308#ifdef MACOS_X
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE,
3310 DOSO_NONE, NULL);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003311#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003312
3313 /*
3314 * Try to read initialization commands from the following places:
3315 * - environment variable VIMINIT
3316 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3317 * - second user vimrc file ($VIM/.vimrc for Dos)
3318 * - environment variable EXINIT
3319 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3320 * - second user exrc file ($VIM/.exrc for Dos)
3321 * The first that exists is used, the rest is ignored.
3322 */
3323 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3324 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003325 if (do_source((char_u *)USR_VIMRC_FILE, TRUE,
3326 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003327#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003328 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003330#endif
Luca Saccarolac9df1fb2024-04-14 22:53:22 +02003331#ifdef XDG_VIMRC_FILE
3332 && do_source((char_u *)XDG_VIMRC_FILE, TRUE,
3333 DOSO_VIMRC, NULL) == FAIL
3334#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003335#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003336 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003338#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003339#ifdef USR_VIMRC_FILE4
3340 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003342#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003343 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344 && do_source((char_u *)USR_EXRC_FILE, FALSE,
3345 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003346#ifdef USR_EXRC_FILE2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 && do_source((char_u *)USR_EXRC_FILE2, FALSE,
3348 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003349#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003350 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003351 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003352 // When no .vimrc file was found: source defaults.vim.
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003353 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE,
3354 NULL) == FAIL)
Bram Moolenaar50809a42023-05-20 16:39:07 +01003355 emsg(_(e_failed_to_source_defaults));
Bram Moolenaar58d98232005-07-23 22:25:46 +00003356 }
3357 }
3358
3359 /*
3360 * Read initialization commands from ".vimrc" or ".exrc" in current
3361 * directory. This is only done if the 'exrc' option is set.
3362 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003363 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003364 * option has been reset in environment of global ".exrc" or ".vimrc".
3365 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3366 * SYS_VIMRC_FILE.
3367 */
3368 if (p_exrc)
3369 {
3370#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003371 // If ".vimrc" file is not owned by user, set 'secure' mode.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003372 if (!file_owned(VIMRC_FILE))
3373#endif
3374 secure = p_secure;
3375
3376 i = FAIL;
3377 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003378 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003379#ifdef USR_VIMRC_FILE2
3380 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003381 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003382#endif
3383#ifdef USR_VIMRC_FILE3
3384 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003385 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003386#endif
3387#ifdef SYS_VIMRC_FILE
3388 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003389 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003390#endif
3391 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003393
3394 if (i == FAIL)
3395 {
3396#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003397 // if ".exrc" is not owned by user set 'secure' mode
Bram Moolenaar58d98232005-07-23 22:25:46 +00003398 if (!file_owned(EXRC_FILE))
3399 secure = p_secure;
3400 else
3401 secure = 0;
3402#endif
3403 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003404 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003405#ifdef USR_EXRC_FILE2
3406 && fullpathcmp((char_u *)USR_EXRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003407 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003408#endif
3409 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 (void)do_source((char_u *)EXRC_FILE, FALSE,
3411 DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003412 }
3413 }
3414 if (secure == 2)
3415 need_wait_return = TRUE;
3416 secure = 0;
3417#ifdef AMIGA
3418 proc->pr_WindowPtr = save_winptr;
3419#endif
3420 }
3421 TIME_MSG("sourcing vimrc file(s)");
3422}
3423
3424/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003425 * Setup to start using the GUI. Exit with an error when not available.
3426 */
3427 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003428main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003429{
3430#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003431 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003432#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003433 mch_errmsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003434 mch_errmsg("\n");
3435 mch_exit(2);
3436#endif
3437}
3438
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003439#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003440
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003441/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 * Get an environment variable and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003443 * Returns FAIL if the environment variable was not executed, OK otherwise.
3444 */
3445 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003446process_env(
3447 char_u *env,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003448 int is_viminit) // when TRUE, called for VIMINIT
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003449{
3450 char_u *initstr;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003451 sctx_T save_current_sctx;
ichizok7e5fe382023-04-15 13:17:50 +01003452 ESTACK_CHECK_DECLARATION;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003453
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003454 if ((initstr = mch_getenv(env)) == NULL || *initstr == NUL)
3455 return FAIL;
3456
3457 if (is_viminit)
3458 vimrc_found(NULL, NULL);
3459 estack_push(ETYPE_ENV, env, 0);
ichizok7e5fe382023-04-15 13:17:50 +01003460 ESTACK_CHECK_SETUP;
3461 save_current_sctx = current_sctx;
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003462 current_sctx.sc_version = 1;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003463#ifdef FEAT_EVAL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003464 current_sctx.sc_sid = SID_ENV;
3465 current_sctx.sc_seq = 0;
3466 current_sctx.sc_lnum = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003467#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003468
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003469 do_cmdline_cmd(initstr);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003470
ichizok7e5fe382023-04-15 13:17:50 +01003471 ESTACK_CHECK_NOW;
3472 estack_pop();
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003473 current_sctx = save_current_sctx;
3474 return OK;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003475}
3476
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003477#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003478/*
3479 * Return TRUE if we are certain the user owns the file "fname".
3480 * Used for ".vimrc" and ".exrc".
3481 * Use both stat() and lstat() for extra security.
3482 */
3483 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003484file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003485{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003486 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003487# ifdef UNIX
3488 uid_t uid = getuid();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003489# else // VMS
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003490 uid_t uid = ((getgid() << 16) | getuid());
3491# endif
3492
3493 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3494# ifdef HAVE_LSTAT
3495 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3496# endif
3497 );
3498}
3499#endif
3500
3501/*
3502 * Give an error message main_errors["n"] and exit.
3503 */
3504 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003505mainerr(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003506 int n, // one of the ME_ defines
3507 char_u *str) // extra argument or NULL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003508{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003509#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003510 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003511#endif
3512
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003513 // If this is a Windows GUI executable, show an error dialog box.
3514#ifdef VIMDLL
3515 gui.in_use = mch_is_gui_executable();
3516#endif
3517#ifdef FEAT_GUI_MSWIN
3518 gui.starting = FALSE; // Needed to show as error.
3519#endif
3520
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003521 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003522 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003523 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003524 mch_errmsg(_(main_errors[n]));
3525 if (str != NULL)
3526 {
3527 mch_errmsg(": \"");
3528 mch_errmsg((char *)str);
3529 mch_errmsg("\"");
3530 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003531 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003532
3533 mch_exit(1);
3534}
3535
3536 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003537mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003538{
3539 mainerr(ME_ARG_MISSING, str);
3540}
3541
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003542#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003543/*
3544 * print a message with three spaces prepended and '\n' appended.
3545 */
3546 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003547main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003548{
3549 mch_msg(" ");
3550 mch_msg(s);
3551 mch_msg("\n");
3552}
3553
3554/*
3555 * Print messages for "vim -h" or "vim --help" and exit.
3556 */
3557 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003558usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003559{
3560 int i;
3561 static char *(use[]) =
3562 {
3563 N_("[file ..] edit specified file(s)"),
3564 N_("- read text from stdin"),
3565 N_("-t tag edit file where tag is defined"),
3566#ifdef FEAT_QUICKFIX
3567 N_("-q [errorfile] edit file with first error")
3568#endif
3569 };
3570
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003571#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003572 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003573#endif
3574
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003575 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003576 mch_msg(longVersion);
Bram Moolenaar32aaf5a2018-06-21 21:38:33 +02003577 mch_msg(_("\n\nUsage:"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003578 for (i = 0; ; ++i)
3579 {
3580 mch_msg(_(" vim [arguments] "));
3581 mch_msg(_(use[i]));
K.Takataeeec2542021-06-02 13:28:16 +02003582 if (i == ARRAY_LENGTH(use) - 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003583 break;
3584 mch_msg(_("\n or:"));
3585 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003586#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003587 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003588#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003589
3590 mch_msg(_("\n\nArguments:\n"));
3591 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003592#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003593 main_msg(_("--literal\t\tDon't expand wildcards"));
3594#endif
3595#ifdef FEAT_OLE
3596 main_msg(_("-register\t\tRegister this gvim for OLE"));
3597 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3598#endif
3599#ifdef FEAT_GUI
3600 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3601 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3602#endif
3603 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3604 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003605 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003606 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3607#ifdef FEAT_DIFF
3608 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3609#endif
3610 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3611 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3612 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3613 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3614 main_msg(_("-M\t\t\tModifications in text not allowed"));
3615 main_msg(_("-b\t\t\tBinary mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003616 main_msg(_("-l\t\t\tLisp mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003617 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3618 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003619 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3620#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003621 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003622#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003623 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3624 main_msg(_("-r\t\t\tList swap files and exit"));
3625 main_msg(_("-r (with file name)\tRecover crashed session"));
3626 main_msg(_("-L\t\t\tSame as -r"));
3627#ifdef AMIGA
3628 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3629 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3630#endif
3631#ifdef FEAT_ARABIC
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003632 main_msg(_("-A\t\t\tStart in Arabic mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003633#endif
3634#ifdef FEAT_RIGHTLEFT
3635 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3636#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003637 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003638 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2d12c252022-06-13 21:42:45 +01003639#ifdef FEAT_GUI
3640 main_msg(_("--gui-dialog-file {fname} For testing: write dialog text"));
3641#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003642 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003643 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3644#ifdef FEAT_GUI
3645 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3646#endif
3647 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003648 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003649 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3650 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3651 main_msg(_("+\t\t\tStart at end of file"));
3652 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003653 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003654 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3655 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3656 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3657 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3658 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3659#ifdef FEAT_CRYPT
3660 main_msg(_("-x\t\t\tEdit encrypted files"));
3661#endif
3662#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3663# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar86181df2020-05-11 23:14:04 +02003664 main_msg(_("-display <display>\tConnect Vim to this particular X-server"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003665# endif
3666 main_msg(_("-X\t\t\tDo not connect to X server"));
3667#endif
3668#ifdef FEAT_CLIENTSERVER
3669 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3670 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3671 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3672 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003673 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003674 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3675 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3676 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3677 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3678#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003679#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003680 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003681#endif
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01003682#ifdef FEAT_JOB_CHANNEL
K.Takata0fd7be72022-11-09 16:29:24 +00003683 main_msg(_("--log <file>\t\tStart logging to <file> early"));
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01003684#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003685#ifdef FEAT_VIMINFO
3686 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3687#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003688 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003689 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3690 main_msg(_("--version\t\tPrint version information and exit"));
3691
3692#ifdef FEAT_GUI_X11
3693# ifdef FEAT_GUI_MOTIF
3694 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003695# endif
Bram Moolenaar86181df2020-05-11 23:14:04 +02003696 main_msg(_("-display <display>\tRun Vim on <display>"));
Bram Moolenaar9e6ba8c2020-05-12 22:21:26 +02003697 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003698 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3699 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3700 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3701 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3702 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3703 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3704 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3705 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003706 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3707 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3708 main_msg(_("-xrm <resource>\tSet the specified resource"));
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003709#endif // FEAT_GUI_X11
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003710#ifdef FEAT_GUI_GTK
3711 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003712 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3713 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003714 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3715 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003716 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003717 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
Bram Moolenaar86181df2020-05-11 23:14:04 +02003718 main_msg(_("-display <display>\tRun Vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003719 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003720 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003721 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003722#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003723#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003724# ifdef VIMDLL
3725 if (gui.starting)
3726# endif
3727 {
3728 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
3729 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
3730 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003731#endif
3732
3733#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003734 // Gnome gives extra messages for --help if we continue, but not for -h.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003735 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003736 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003737 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003738 gui.dofork = FALSE;
3739 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003740 else
3741#endif
3742 mch_exit(0);
3743}
3744
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003745/*
3746 * Check the result of the ATTENTION dialog:
3747 * When "Quit" selected, exit Vim.
3748 * When "Recover" selected, recover the file.
3749 */
3750 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003751check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003752{
3753 if (swap_exists_action == SEA_QUIT)
3754 getout(1);
3755 handle_swap_exists(NULL);
3756}
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003757
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003758#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003759
Bram Moolenaar595297d2017-03-04 19:11:12 +01003760#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003761 static void
3762set_progpath(char_u *argv0)
3763{
3764 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003765
Bram Moolenaar4f974752019-02-17 17:44:42 +01003766# ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003767 // A relative path containing a "/" will become invalid when using ":cd",
3768 // turn it into a full path.
3769 // On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3770 // this.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003771 char_u *path = NULL;
3772
3773 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3774 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003775# else
3776 char_u buf[MAXPATHL + 1];
3777# ifdef PROC_EXE_LINK
3778 char linkbuf[MAXPATHL + 1];
3779 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003780
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003781 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3782 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003783 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003784 linkbuf[len] = NUL;
3785 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003786 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003787# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003788
3789 if (!mch_isFullName(val))
3790 {
3791 if (gettail(val) != val
3792 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3793 val = buf;
3794 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003795# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003796
Bram Moolenaar08cab962017-03-04 14:37:18 +01003797 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003798
Bram Moolenaar4f974752019-02-17 17:44:42 +01003799# ifdef MSWIN
Bram Moolenaar43663192017-03-05 14:29:12 +01003800 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003801# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003802}
3803
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003804#endif // NO_VIM_MAIN