blob: a3caac8fb520b8eb8e665edfdcf7bfb283aca43f [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
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200452/*
453 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
454 * things simple.
455 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
456 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100457 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200458vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100459{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100460#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000461#ifdef FEAT_EVAL
462 /*
463 * Read all the plugin files.
464 * Only when compiled with +eval, since most plugins need it.
465 */
466 if (p_lpl)
467 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200468 char_u *rtp_copy = NULL;
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100469 char_u *plugin_pattern = (char_u *)
470# if defined(VMS) || defined(AMIGA) // VMS and Amiga don't handle the "**".
471 "plugin/*.vim"
472# else
473 "plugin/**/*.vim"
474# endif
475 ;
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200476
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100477 // First add all package directories to 'runtimepath', so that their
478 // autoload directories can be found. Only if not done already with a
479 // :packloadall command.
480 // Make a copy of 'runtimepath', so that source_runtime does not use
481 // the pack directories.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200482 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200483 {
484 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200485 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200486 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200487
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100488 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy, plugin_pattern,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100489 DIP_ALL | DIP_NOAFTER, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000490 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200491 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100492
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100493 // Only source "start" packages if not done already with a :packloadall
494 // command.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200495 if (!did_source_packages)
496 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100497 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200498
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100499 source_runtime(plugin_pattern, DIP_ALL | DIP_AFTER);
Bram Moolenaar66459b72016-08-06 19:01:55 +0200500 TIME_MSG("loading after plugins");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000501 }
502#endif
503
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000504#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100505 // Decide about window layout for diff mode after reading vimrc.
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000506 if (params.diff_mode && params.window_layout == 0)
507 {
508 if (diffopt_horizontal())
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100509 params.window_layout = WIN_HOR; // use horizontal split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000510 else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100511 params.window_layout = WIN_VER; // use vertical split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000512 }
513#endif
514
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000515 /*
516 * Recovery mode without a file name: List swap files.
517 * This uses the 'dir' option, therefore it must be after the
518 * initializations.
519 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200520 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000521 {
Bram Moolenaarc216a7a2022-12-05 13:50:55 +0000522 recover_names(NULL, TRUE, NULL, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000523 mch_exit(0);
524 }
525
526 /*
527 * Set a few option defaults after reading .vimrc files:
528 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
529 */
530 set_init_3();
531 TIME_MSG("inits 3");
532
533 /*
534 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
535 * Note that this overrides anything from a vimrc file.
536 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000537 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000538 p_uc = 0;
539
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000540#ifdef FEAT_GUI
541 if (gui.starting)
542 {
Bram Moolenaarc7226682019-08-17 16:33:23 +0200543# if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100544 // When something caused a message from a vimrc script, need to output
545 // an extra newline before the shell prompt.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000546 if (did_emsg || msg_didout)
547 putchar('\n');
Bram Moolenaarc7226682019-08-17 16:33:23 +0200548# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000549
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100550 gui_start(NULL); // will set full_screen to TRUE
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000551 TIME_MSG("starting GUI");
552
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100553 // When running "evim" or "gvim -y" we need the menus, exit if we
554 // don't have them.
Bram Moolenaar58d98232005-07-23 22:25:46 +0000555 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000556 mch_exit(1);
Luuk van Baal470a1412022-09-14 01:27:23 +0100557 firstwin->w_prev_height = firstwin->w_height; // may have changed
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000558 }
559#endif
560
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000561#ifdef FEAT_VIMINFO
562 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000563 * Read in registers, history etc, but not marks, from the viminfo file.
564 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000565 */
566 if (*p_viminfo != NUL)
567 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000568 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000569 TIME_MSG("reading viminfo");
570 }
571#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100572#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100573 // It's better to make v:oldfiles an empty list than NULL.
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100574 if (get_vim_var_list(VV_OLDFILES) == NULL)
575 set_vim_var_list(VV_OLDFILES, list_alloc());
576#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000577
578#ifdef FEAT_QUICKFIX
579 /*
580 * "-q errorfile": Load the error file now.
581 * If the error file can't be read, exit before doing anything else.
582 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000583 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000584 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100585 char_u *enc = NULL;
586
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100587 enc = p_menc;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000588 if (params.use_ef != NULL)
589 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000590 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200591 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100592 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000593 {
594 out_char('\n');
595 mch_exit(3);
596 }
597 TIME_MSG("reading errorfile");
598 }
599#endif
600
601 /*
602 * Start putting things on the screen.
603 * Scroll screen down before drawing over it
604 * Clear screen now, so file message will not be cleared.
605 */
606 starting = NO_BUFFERS;
607 no_wait_return = FALSE;
608 if (!exmode_active)
609 msg_scroll = FALSE;
610
611#ifdef FEAT_GUI
612 /*
613 * This seems to be required to make callbacks to be called now, instead
614 * of after things have been put on the screen, which then may be deleted
615 * when getting a resize callback.
616 * For the Mac this handles putting files dropped on the Vim icon to
617 * global_alist.
618 */
619 if (gui.in_use)
620 {
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100621 gui_wait_for_chars(50L, typebuf.tb_change_cnt);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000622 TIME_MSG("GUI delay");
623 }
624#endif
625
626#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
627 qnx_clip_init();
628#endif
629
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200630#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
631 clip_init(TRUE);
632#endif
633
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000634#ifdef FEAT_XCLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100635 // Start using the X clipboard, unless the GUI was started.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000636# ifdef FEAT_GUI
637 if (!gui.in_use)
638# endif
639 {
640 setup_term_clip();
641 TIME_MSG("setup clipboard");
642 }
643#endif
644
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000645#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100646 // Prepare for being a Vim server.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000647 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000648#endif
649
650 /*
651 * If "-" argument given: Read file from stdin.
652 * Do this before starting Raw mode, because it may change things that the
653 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
654 * are the same terminal: "cat | vim -".
655 * Using autocommands here may cause trouble...
656 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000657 if (params.edit_type == EDIT_STDIN && !recoverymode)
658 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000659
660#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100661 // When switching screens and something caused a message from a vimrc
662 // script, need to output an extra newline on exit.
Abhijit Barik221927b2025-04-06 16:12:06 +0200663 if ((did_emsg || msg_didout) && *T_TI != NUL && params.edit_type != EDIT_STDIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000664 newline_on_exit = TRUE;
665#endif
666
667 /*
K.Takataeeec2542021-06-02 13:28:16 +0200668 * When done something that is not allowed or given an error message call
Bram Moolenaar13608d82022-08-29 15:06:50 +0100669 * wait_return(). This must be done before starttermcap(), because it may
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000670 * switch to another screen. It must be done after settmode(TMODE_RAW),
671 * because we want to react on a single key stroke.
672 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000673 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000674 */
675 settmode(TMODE_RAW);
676 TIME_MSG("setting raw mode");
677
678 if (need_wait_return || msg_didany)
679 {
680 wait_return(TRUE);
681 TIME_MSG("waiting for return");
682 }
683
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100684 starttermcap(); // start termcap if not done by wait_return()
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000685 TIME_MSG("start termcap");
686
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200687 setmouse(); // may start using the mouse
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000688 if (scroll_region)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100689 scroll_region_reset(); // In case Rows changed
690 scroll_start(); // may scroll the screen to the right position
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000691
Bram Moolenaar651fca82021-11-29 20:39:38 +0000692#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar40385db2018-08-07 22:31:44 +0200693 term_push_title(SAVE_RESTORE_BOTH);
694#endif
695
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000696 /*
697 * Don't clear the screen when starting in Ex mode, unless using the GUI.
698 */
699 if (exmode_active
700#ifdef FEAT_GUI
701 && !gui.in_use
702#endif
703 )
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100704 set_must_redraw(UPD_CLEAR);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000705 else
706 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100707 screenclear(); // clear screen
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000708 TIME_MSG("clearing screen");
709 }
710
711#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000712 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000713 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100714 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200715 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000716 TIME_MSG("getting crypt key");
717 }
718#endif
719
720 no_wait_return = TRUE;
721
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000722 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000723 * Create the requested number of windows and edit buffers in them.
724 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000725 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000726 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000727 TIME_MSG("opening buffers");
728
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000729#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100730 // clear v:swapcommand
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000731 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
732#endif
733
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100734 // Ex starts at last line of the file
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000735 if (exmode_active)
736 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
737
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000738 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
739 TIME_MSG("BufEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000740 setpcmark();
741
742#ifdef FEAT_QUICKFIX
743 /*
744 * When started with "-q errorfile" jump to first error now.
745 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000746 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000747 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000748 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000749 TIME_MSG("jump to first error");
750 }
751#endif
752
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000753 /*
754 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000755 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000756 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200757 edit_buffers(&params, start_dir);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200758 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000759
760#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000761 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000762 {
763 win_T *wp;
764
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100765 // set options in each window for "vimdiff".
Bram Moolenaar29323592016-07-24 22:04:11 +0200766 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000767 diff_win_options(wp, TRUE);
768 }
769#endif
770
771 /*
772 * Shorten any of the filenames, but only when absolute.
773 */
774 shorten_fnames(FALSE);
775
776 /*
777 * Need to jump to the tag before executing the '-c command'.
778 * Makes "vim -c '/return' -t main" work.
779 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000780 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000781 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000782 swap_exists_did_quit = FALSE;
Bram Moolenaar146522e2005-12-16 21:55:46 +0000783
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000784 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000785 do_cmdline_cmd(IObuff);
786 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000787
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100788 // If the user doesn't want to edit the file then we quit here.
Bram Moolenaar146522e2005-12-16 21:55:46 +0000789 if (swap_exists_did_quit)
790 getout(1);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000791 }
792
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100793 // Execute any "+", "-c" and "-S" arguments.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000794 if (params.n_commands > 0)
795 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000796
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100797 // Must come before the may_req_ calls.
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200798 starting = 0;
799
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100800#if defined(FEAT_TERMRESPONSE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100801 // Must be done before redrawing, puts a few characters on the screen.
Bram Moolenaara45551a2020-06-09 15:57:37 +0200802 check_terminal_behavior();
Bram Moolenaar976787d2017-06-04 15:45:50 +0200803#endif
804
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000805 RedrawingDisabled = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100806 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000807 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000808
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100809 // 'autochdir' has been postponed
Bram Moolenaar6f470022018-04-10 18:47:20 +0200810 DO_AUTOCHDIR;
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200811
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000812#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100813 // Requesting the termresponse is postponed until here, so that a "-c q"
814 // argument doesn't make it appear in the shell Vim was started from.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000815 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200816
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200817 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000818#endif
819
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100820 // start in insert mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000821 if (p_im)
822 need_start_insertmode = TRUE;
823
Bram Moolenaar14735512016-03-26 21:00:08 +0100824#ifdef FEAT_EVAL
825 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
826#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000827 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
828 TIME_MSG("VimEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000829
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200830#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100831 // Adjust default register name for "unnamed" in 'clipboard'. Can only be
832 // done after the clipboard is available and all initial commands that may
833 // modify the 'clipboard' setting have run; i.e. just before entering the
834 // main loop.
Bram Moolenaar439c0362020-06-06 15:58:03 +0200835 reset_reg_var();
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200836#endif
837
Bram Moolenaar8a3bb562018-03-04 20:14:14 +0100838#if defined(FEAT_DIFF)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100839 // When a startup script or session file setup for diff'ing and
840 // scrollbind, sync the scrollbind now.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000841 if (curwin->w_p_diff && curwin->w_p_scb)
842 {
843 update_topline();
844 check_scrollbind((linenr_T)0, 0L);
845 TIME_MSG("diff scrollbinding");
846 }
847#endif
848
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200849#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
850# ifdef VIMDLL
851 if (!gui.in_use)
852# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100853 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000854#endif
855
Bram Moolenaar4033c552017-09-16 20:54:51 +0200856#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100857 // When tab pages were created, may need to update the tab pages line and
858 // scrollbars. This is skipped while creating them.
h-east6073f132021-12-24 11:57:06 +0000859 if (gui.in_use && first_tabpage->tp_next != NULL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000860 {
861 out_flush();
862 gui_init_which_components(NULL);
863 gui_update_scrollbars(TRUE);
864 }
865 need_mouse_correct = TRUE;
866#endif
867
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100868 // If ":startinsert" command used, stuff a dummy command to be able to
869 // call normal_cmd(), which will then start Insert mode.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000870 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000871 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000872
873#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200874 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200875 {
876# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200877# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar4f974752019-02-17 17:44:42 +0100878 && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200879 if (gui.in_use)
880 {
881 mch_errmsg(_("netbeans is not supported with this GUI\n"));
882 mch_exit(2);
883 }
884# endif
885# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100886 // Tell the client that it can start sending commands.
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200887 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200888 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000889#endif
890
Bram Moolenaare7ddf4e2020-02-03 21:29:30 +0100891 // Redraw at least once, also when 'lazyredraw' is set, to make sure the
892 // window title gets updated.
893 do_redraw = TRUE;
894
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000895 TIME_MSG("before starting main loop");
896
897 /*
898 * Call the main command loop. This never returns.
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200899 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000900 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000901
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100902#endif // NO_VIM_MAIN
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200903
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000904 return 0;
905}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000906
907/*
Hirohito Higashic5654b82025-02-10 20:55:17 +0100908 * Initialisation #1 shared by main() and some tests.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200909 */
910 void
Hirohito Higashic5654b82025-02-10 20:55:17 +0100911common_init_1(void)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200912{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100913 estack_init();
Bram Moolenaar438d1762018-09-30 17:11:48 +0200914 cmdline_init();
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200915
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100916 (void)mb_init(); // init mb_bytelen_tab[] to ones
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200917#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100918 eval_init(); // init global variables
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200919#endif
920
921#ifdef __QNXNTO__
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100922 qnx_init(); // PhAttach() for clipboard, (and gui)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200923#endif
924
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200925 /*
926 * Allocate space for the generic buffers (needed for set_init_1() and
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100927 * emsg()).
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200928 */
929 if ((IObuff = alloc(IOSIZE)) == NULL
930 || (NameBuff = alloc(MAXPATHL)) == NULL)
931 mch_exit(0);
932 TIME_MSG("Allocated generic buffers");
Hirohito Higashic5654b82025-02-10 20:55:17 +0100933}
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200934
Hirohito Higashic5654b82025-02-10 20:55:17 +0100935
936/*
937 * Initialisation #2 shared by main() and some tests.
938 */
939 void
940common_init_2(mparm_T *paramp)
941{
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200942#ifdef NBDEBUG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100943 // Wait a moment for debugging NetBeans. Must be after allocating
944 // NameBuff.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200945 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
946 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
947 TIME_MSG("NetBeans debug wait");
948#endif
949
950#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
951 /*
952 * Setup to use the current locale (for ctype() and many other things).
953 * NOTE: Translated messages with encodings other than latin1 will not
954 * work until set_init_1() has been called!
955 */
956 init_locale();
957 TIME_MSG("locale set");
958#endif
959
960#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100961 gui.dofork = TRUE; // default is to use fork()
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200962#endif
963
964 /*
965 * Do a first scan of the arguments in "argv[]":
966 * -display or --display
967 * --server...
968 * --socketid
969 * --windowid
970 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200971 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200972
Bram Moolenaard0573012017-10-28 21:11:06 +0200973#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100974 // Prepare for possibly starting GUI sometime
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200975 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200976 TIME_MSG("GUI prepared");
977#endif
978
979#ifdef FEAT_CLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100980 clip_init(FALSE); // Initialise clipboard stuff
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200981 TIME_MSG("clipboard setup");
982#endif
983
984 /*
985 * Check if we have an interactive window.
986 * On the Amiga: If there is no window, we open one with a newcli command
987 * (needed for :! to * work). mch_check_win() will also handle the -d or
988 * -dev argument.
989 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100990 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200991 TIME_MSG("window checked");
992
993 /*
994 * Allocate the first window and buffer.
995 * Can't do anything without it, exit when it fails.
996 */
997 if (win_alloc_first() == FAIL)
998 mch_exit(0);
999
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001000 init_yank(); // init yank buffers
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001001
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001002 alist_init(&global_alist); // Init the argument list to empty.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001003 global_alist.id = 0;
1004
1005 /*
1006 * Set the default values for the options.
1007 * NOTE: Non-latin1 translated messages are working only after this,
1008 * because this is where "has_mbyte" will be set, which is used by
1009 * msg_outtrans_len_attr().
1010 * First find out the home directory, needed to expand "~" in options.
1011 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001012 init_homedir(); // find real value of $HOME
Bram Moolenaar07268702018-03-01 21:57:32 +01001013 set_init_1(paramp->clean);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001014 TIME_MSG("inits 1");
1015
1016#ifdef FEAT_EVAL
Bram Moolenaar69bf6342019-10-29 04:16:57 +01001017 // set v:lang and v:ctype
1018 set_lang_var();
1019
1020 // set v:argv
1021 set_argv_var(paramp->argv, paramp->argc);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001022#endif
Bram Moolenaar6436cd82018-12-27 00:28:33 +01001023
1024#ifdef FEAT_SIGNS
1025 init_signs();
1026#endif
64-bitman88d41ab2025-04-06 17:20:39 +02001027
1028#ifdef FEAT_QUICKFIX
1029 // initialize global quickfix list
1030 // don't send an error message when memory allocation fails
1031 // do it when the user tries to access the quickfix list
1032 qf_init_quickfix_stack();
1033#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001034}
1035
1036/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001037 * Return TRUE when the --not-a-term argument was found.
1038 */
1039 int
Bram Moolenaarf7e73022022-09-24 13:10:04 +01001040is_not_a_term(void)
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001041{
1042 return params.not_a_term;
1043}
1044
Bram Moolenaar7007e312021-03-27 12:11:33 +01001045/*
1046 * Return TRUE when the --not-a-term argument was found or the GUI is in use.
1047 */
Bram Moolenaarf7e73022022-09-24 13:10:04 +01001048 int
1049is_not_a_term_or_gui(void)
Bram Moolenaar7007e312021-03-27 12:11:33 +01001050{
1051 return params.not_a_term
1052#ifdef FEAT_GUI
1053 || gui.in_use
1054#endif
1055 ;
1056}
1057
K.Takata3c240f62023-05-31 12:47:45 +01001058#if defined(EXITFREE) || defined(PROTO)
1059 void
1060free_vbuf(void)
1061{
1062# ifdef _IOLBF
1063 if (s_vbuf != NULL)
1064 {
1065 setvbuf(stdout, NULL, _IONBF, 0);
1066 free(s_vbuf);
1067 s_vbuf = NULL;
1068 }
1069# endif
1070}
1071#endif
1072
Bram Moolenaar2d12c252022-06-13 21:42:45 +01001073#if defined(FEAT_GUI) || defined(PROTO)
1074/*
1075 * If a --gui-dialog-file argument was given return the file name.
1076 * Otherwise return NULL.
1077 */
1078 char_u *
1079get_gui_dialog_file(void)
1080{
1081 return params.gui_dialog_file;
1082}
1083#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001084
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001085// When TRUE in a safe state when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001086static int was_safe = FALSE;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001087static oparg_T *current_oap = NULL;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001088
1089/*
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001090 * Return TRUE if an operator was started but not finished yet.
1091 * Includes typing a count or a register name.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001092 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001093 int
1094op_pending(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001095{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001096 return !(current_oap != NULL
1097 && !finish_op
1098 && current_oap->prev_opcount == 0
1099 && current_oap->prev_count0 == 0
1100 && current_oap->op_type == OP_NOP
1101 && current_oap->regname == NUL);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001102}
1103
1104/*
Bram Moolenaard103ee72019-09-18 21:15:31 +02001105 * Return whether currently it is safe, assuming it was safe before (high level
1106 * state didn't change).
1107 */
1108 static int
1109is_safe_now(void)
1110{
1111 return stuff_empty()
1112 && typebuf.tb_len == 0
1113 && scriptin[curscript] == NULL
Bram Moolenaar4fa13462022-02-05 12:39:24 +00001114#ifdef FEAT_EVAL
1115 && !debug_mode
1116#endif
1117 && !global_busy;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001118}
1119
1120/*
zeertzjqc4226622024-04-03 22:38:07 +02001121 * Trigger SafeState if currently in a safe state, that is "safe" is TRUE and
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001122 * there is no typeahead.
1123 */
1124 void
1125may_trigger_safestate(int safe)
1126{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001127 int is_safe = safe && is_safe_now();
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001128
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001129#ifdef FEAT_EVAL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001130 if (was_safe != is_safe)
1131 // Only log when the state changes, otherwise it happens at nearly
1132 // every key stroke.
Bram Moolenaard103ee72019-09-18 21:15:31 +02001133 ch_log(NULL, is_safe ? "SafeState: Start triggering"
1134 : "SafeState: Stop triggering");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001135#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001136 if (is_safe)
1137 apply_autocmds(EVENT_SAFESTATE, NULL, NULL, FALSE, curbuf);
1138 was_safe = is_safe;
1139}
1140
1141/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001142 * Something changed which causes the state possibly to be unsafe, e.g. a
1143 * character was typed. It will remain unsafe until the next call to
1144 * may_trigger_safestate().
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001145 */
1146 void
Bram Moolenaard103ee72019-09-18 21:15:31 +02001147state_no_longer_safe(char *reason UNUSED)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001148{
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001149#ifdef FEAT_EVAL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001150 if (was_safe)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001151 ch_log(NULL, "SafeState: reset: %s", reason);
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001152#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001153 was_safe = FALSE;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001154}
1155
Dominique Pelle748b3082022-01-08 12:41:16 +00001156#if defined(FEAT_EVAL) || defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001157 int
1158get_was_safe_state(void)
1159{
1160 return was_safe;
1161}
Dominique Pelle748b3082022-01-08 12:41:16 +00001162#endif
Bram Moolenaard103ee72019-09-18 21:15:31 +02001163
Dominique Pelle748b3082022-01-08 12:41:16 +00001164#if defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001165/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001166 * Invoked when leaving code that invokes callbacks. Then trigger
1167 * SafeStateAgain, if it was safe when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001168 */
1169 void
Bram Moolenaar37d18072019-09-17 20:28:38 +02001170may_trigger_safestateagain(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001171{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001172 if (!was_safe)
1173 {
1174 // If the safe state was reset in state_no_longer_safe(), e.g. because
1175 // of calling feedkeys(), we check if it's now safe again (all keys
1176 // were consumed).
1177 was_safe = is_safe_now();
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001178# ifdef FEAT_EVAL
Bram Moolenaard103ee72019-09-18 21:15:31 +02001179 if (was_safe)
1180 ch_log(NULL, "SafeState: undo reset");
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001181# endif
Bram Moolenaard103ee72019-09-18 21:15:31 +02001182 }
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001183 if (was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02001184 {
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001185# ifdef FEAT_EVAL
Bram Moolenaar101e9922019-09-25 21:43:11 +02001186 // Only do this message when another message was given, otherwise we
1187 // get lots of them.
1188 if ((did_repeated_msg & REPEATED_MSG_SAFESTATE) == 0)
1189 {
1190 int did = did_repeated_msg;
1191
1192 ch_log(NULL,
1193 "SafeState: back to waiting, triggering SafeStateAgain");
1194 did_repeated_msg = did | REPEATED_MSG_SAFESTATE;
1195 }
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001196# endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001197 apply_autocmds(EVENT_SAFESTATEAGAIN, NULL, NULL, FALSE, curbuf);
Bram Moolenaar37d18072019-09-17 20:28:38 +02001198 }
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001199# ifdef FEAT_EVAL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001200 else
Bram Moolenaard103ee72019-09-18 21:15:31 +02001201 ch_log(NULL,
1202 "SafeState: back to waiting, not triggering SafeStateAgain");
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001203# endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001204}
Dominique Pelle748b3082022-01-08 12:41:16 +00001205#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001206
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001207/*
1208 * Return TRUE if there is any typeahead, pending operator or command.
1209 */
1210 int
1211work_pending(void)
1212{
1213 return op_pending() || !is_safe_now();
1214}
1215
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001216
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001217/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001218 * Main loop: Execute Normal mode commands until exiting Vim.
1219 * Also used to handle commands in the command-line window, until the window
1220 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001221 * Also used to handle ":visual" command after ":global": execute Normal mode
1222 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001223 */
1224 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001225main_loop(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001226 int cmdwin, // TRUE when working in the command-line window
1227 int noexmode) // TRUE when return on entering Ex mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001228{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001229 oparg_T oa; // operator arguments
1230 oparg_T *prev_oap; // operator arguments
1231 volatile int previous_got_int = FALSE; // "got_int" was TRUE
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001232#ifdef FEAT_CONCEAL
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001233 // these are static to avoid a compiler warning
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001234 static linenr_T conceal_old_cursor_line = 0;
1235 static linenr_T conceal_new_cursor_line = 0;
1236 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001237#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001238
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001239 prev_oap = current_oap;
1240 current_oap = &oa;
1241
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001242#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001243 // Setup to catch a terminating error from the X server. Just ignore
1244 // it, restore the state and continue. This might not always work
1245 // properly, but at least we don't exit unexpectedly when the X server
1246 // exits while Vim is running in a console.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001247 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001248 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001249 State = MODE_NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001250 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001251 got_int = TRUE;
1252 need_wait_return = FALSE;
1253 global_busy = FALSE;
1254 exmode_active = 0;
1255 skip_redraw = FALSE;
1256 RedrawingDisabled = 0;
1257 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001258 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001259# ifdef FEAT_EVAL
1260 emsg_skip = 0;
1261# endif
1262 emsg_off = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001263 setmouse();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001264 settmode(TMODE_RAW);
1265 starttermcap();
1266 scroll_start();
1267 redraw_later_clear();
1268 }
1269#endif
1270
1271 clear_oparg(&oa);
Martin Tournoij7904fa42022-10-04 16:28:45 +01001272 while (!cmdwin || cmdwin_result == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001273 {
1274 if (stuff_empty())
1275 {
1276 did_check_timestamps = FALSE;
1277 if (need_check_timestamps)
1278 check_timestamps(FALSE);
Bram Moolenaar13608d82022-08-29 15:06:50 +01001279 if (need_wait_return) // if wait_return() still needed ...
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001280 wait_return(FALSE); // ... call it now
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001281 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001282 {
1283 need_start_insertmode = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001284 stuffReadbuff((char_u *)"i"); // start insert mode next
1285 // skip the fileinfo message now, because it would be shown
1286 // after insert mode finishes!
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001287 need_fileinfo = FALSE;
1288 }
1289 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001290
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001291 // Reset "got_int" now that we got back to the main loop. Except when
1292 // inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1293 // the ":g" command.
1294 // For ":g/pat/vi" we reset "got_int" when used once. When used
1295 // a second time we go back to Ex mode and abort the ":g" command.
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001296 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001297 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001298 if (noexmode && global_busy && !exmode_active && previous_got_int)
1299 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001300 // Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1301 // used and keep "got_int" set, so that it aborts ":g".
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001302 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +01001303 State = MODE_NORMAL;
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001304 }
1305 else if (!global_busy || !exmode_active)
1306 {
1307 if (!quit_more)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001308 (void)vgetc(); // flush all buffers
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001309 got_int = FALSE;
1310 }
1311 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001312 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001313 else
1314 previous_got_int = FALSE;
1315
Bram Moolenaar069613c2022-01-15 15:23:44 +00001316#ifdef FEAT_EVAL
1317 // At the toplevel there is no exception handling. Discard any that
1318 // may be hanging around (e.g. from "interrupt" at the debug prompt).
1319 if (did_throw && !ex_normal_busy)
1320 discard_current_exception();
1321#endif
1322
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001323 if (!exmode_active)
1324 msg_scroll = FALSE;
1325 quit_more = FALSE;
1326
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001327 // it's not safe unless may_trigger_safestate_main() is called
1328 was_safe = FALSE;
1329
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001330 /*
1331 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1332 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1333 * update cursor and redraw.
1334 */
1335 if (skip_redraw || exmode_active)
Bram Moolenaarc174c2e2023-03-25 20:06:49 +00001336 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001337 skip_redraw = FALSE;
Bram Moolenaarc174c2e2023-03-25 20:06:49 +00001338 setcursor();
1339 cursor_on();
1340 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001341 else if (do_redraw || stuff_empty())
1342 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001343#ifdef FEAT_GUI
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001344 // If ui_breakcheck() was used a resize may have been postponed.
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001345 gui_may_resize_shell();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001346#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001347#ifdef HAVE_DROP_FILE
1348 // If files were dropped while text was locked or the curbuf was
1349 // locked, this would be a good time to handle the drop.
1350 handle_any_postponed_drop();
1351#endif
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001352#ifdef FEAT_CONCEAL
1353 if (curwin->w_p_cole == 0)
1354 conceal_update_lines = FALSE;
1355#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001356
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001357 // Trigger CursorMoved if the cursor moved.
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001358 if (!finish_op && (has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001359#ifdef FEAT_PROP_POPUP
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001360 || popup_visible
Bram Moolenaar3397f742019-06-02 18:40:06 +02001361#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001362#ifdef FEAT_CONCEAL
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001363 || curwin->w_p_cole > 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001364#endif
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001365 )
1366 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001367 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001368 if (has_cursormoved())
1369 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1370 FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001371#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001372 if (popup_visible)
1373 popup_check_cursor_pos();
1374#endif
1375#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001376 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001377 {
1378 conceal_old_cursor_line = last_cursormoved.lnum;
1379 conceal_new_cursor_line = curwin->w_cursor.lnum;
1380 conceal_update_lines = TRUE;
1381 }
Bram Moolenaar3397f742019-06-02 18:40:06 +02001382#endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001383 last_cursormoved = curwin->w_cursor;
1384 }
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001385
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001386#if defined(FEAT_CONCEAL)
1387 if (conceal_update_lines
1388 && (conceal_old_cursor_line != conceal_new_cursor_line
1389 || conceal_cursor_line(curwin)
1390 || need_cursor_line_redraw))
1391 {
1392 if (conceal_old_cursor_line != conceal_new_cursor_line
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001393 && conceal_old_cursor_line != 0
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001394 && conceal_old_cursor_line
1395 <= curbuf->b_ml.ml_line_count)
1396 redrawWinline(curwin, conceal_old_cursor_line);
1397 redrawWinline(curwin, conceal_new_cursor_line);
1398 curwin->w_valid &= ~VALID_CROW;
1399 need_cursor_line_redraw = FALSE;
1400 }
1401#endif
1402
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001403 // Trigger TextChanged if b:changedtick differs.
Bram Moolenaar186628f2013-03-19 13:33:23 +01001404 if (!finish_op && has_textchanged()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001405 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001406 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001407 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL, FALSE, curbuf);
1408 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001409 }
Bram Moolenaar186628f2013-03-19 13:33:23 +01001410
LemonBoy09371822022-04-08 15:18:45 +01001411 // Ensure curwin->w_topline and curwin->w_leftcol are up to date
1412 // before triggering a WinScrolled autocommand.
1413 update_topline();
1414 validate_cursor();
1415
1416 if (!finish_op)
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001417 may_trigger_win_scrolled_resized();
LemonBoy09371822022-04-08 15:18:45 +01001418
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001419 // If nothing is pending and we are going to wait for the user to
1420 // type a character, trigger SafeState.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001421 may_trigger_safestate(!op_pending() && restart_edit == 0);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001422
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01001423#if defined(FEAT_DIFF)
Bram Moolenaare3521d92018-09-16 14:10:31 +02001424 // Updating diffs from changed() does not always work properly,
1425 // esp. updating folds. Do an update just before redrawing if
1426 // needed.
1427 if (curtab->tp_diff_update || curtab->tp_diff_invalid)
1428 {
1429 ex_diffupdate(NULL);
1430 curtab->tp_diff_update = FALSE;
1431 }
1432
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001433 // Scroll-binding for diff mode may have been postponed until
1434 // here. Avoids doing it for every change.
Bram Moolenaar33aec762006-01-22 23:30:12 +00001435 if (diff_need_scrollbind)
1436 {
1437 check_scrollbind((linenr_T)0, 0L);
1438 diff_need_scrollbind = FALSE;
1439 }
1440#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001441#if defined(FEAT_FOLDING)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001442 // Include a closed fold completely in the Visual area.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001443 foldAdjustVisual();
1444#endif
1445#ifdef FEAT_FOLDING
1446 /*
1447 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1448 * contain the cursor.
1449 * When 'foldopen' is "all", open the fold(s) under the cursor.
1450 * This may mark the window for redrawing.
1451 */
1452 if (hasAnyFolding(curwin) && !char_avail())
1453 {
1454 foldCheckClose();
1455 if (fdo_flags & FDO_ALL)
1456 foldOpenCursor();
1457 }
1458#endif
1459
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001460 // Before redrawing, make sure w_topline is correct, and w_leftcol
1461 // if lines don't wrap, and w_skipcol if lines wrap.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001462 update_topline();
1463 validate_cursor();
1464
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001465 if (VIsual_active)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001466 update_curbuf(UPD_INVERTED); // update inverted part
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001467 else if (must_redraw)
Bram Moolenaara338adc2018-01-31 20:51:47 +01001468 {
Bram Moolenaar017ba072019-09-14 21:01:23 +02001469 mch_disable_flush(); // Stop issuing gui_mch_flush().
Bram Moolenaar11a58af2019-10-24 22:32:31 +02001470 update_screen(0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01001471 mch_enable_flush();
1472 }
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01001473 else if (redraw_cmdline || clear_cmdline || redraw_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001474 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001475 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001476 if (need_maketitle)
1477 maketitle();
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001478#ifdef FEAT_VIMINFO
1479 curbuf->b_last_used = vim_time();
1480#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001481 // display message after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001482 if (keep_msg != NULL)
1483 {
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001484 char_u *p = vim_strsave(keep_msg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001485
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001486 if (p != NULL)
1487 {
1488 // msg_start() will set keep_msg to NULL, make a copy
1489 // first. Don't reset keep_msg, msg_attr_keep() uses it to
1490 // check for duplicates. Never put this message in
1491 // history.
1492 msg_hist_off = TRUE;
1493 msg_attr((char *)p, keep_msg_attr);
1494 msg_hist_off = FALSE;
1495 vim_free(p);
1496 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001497 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001498 if (need_fileinfo) // show file info after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001499 {
1500 fileinfo(FALSE, TRUE, FALSE);
1501 need_fileinfo = FALSE;
1502 }
1503
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001504 emsg_on_display = FALSE; // can delete error message now
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001505 did_emsg = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001506 msg_didany = FALSE; // reset lines_left in msg_start()
1507 may_clear_sb_text(); // clear scroll-back text on next msg
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001508 showruler(FALSE);
1509
1510 setcursor();
1511 cursor_on();
1512
1513 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001514
1515#ifdef STARTUPTIME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001516 // Now that we have drawn the first screen all the startup stuff
1517 // has been done, close any file for startup messages.
Bram Moolenaar3f269672009-11-03 11:11:11 +00001518 if (time_fd != NULL)
1519 {
1520 TIME_MSG("first screen update");
1521 TIME_MSG("--- VIM STARTED ---");
1522 fclose(time_fd);
1523 time_fd = NULL;
1524 }
1525#endif
Bram Moolenaar0a60f792022-11-19 21:18:11 +00001526 // After the first screen update may start triggering WinScrolled
1527 // autocmd events. Store all the scroll positions and sizes now.
1528 may_make_initial_scroll_size_snapshot();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001529 }
1530#ifdef FEAT_GUI
1531 if (need_mouse_correct)
1532 gui_mouse_correct();
1533#endif
1534
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001535 // May request the keyboard protocol state now.
1536 may_send_t_RK();
1537
1538 // Update w_curswant if w_set_curswant has been set.
1539 // Postponed until here to avoid computing w_virtcol too often.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001540 update_curswant();
1541
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001542#ifdef FEAT_EVAL
1543 /*
1544 * May perform garbage collection when waiting for a character, but
1545 * only at the very toplevel. Otherwise we may be using a List or
1546 * Dict internally somewhere.
1547 * "may_garbage_collect" is reset in vgetc() which is invoked through
1548 * do_exmode() and normal_cmd().
1549 */
1550 may_garbage_collect = (!cmdwin && !noexmode);
1551#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001552 /*
1553 * If we're invoked as ex, do a round of ex commands.
1554 * Otherwise, get and execute a normal mode command.
1555 */
1556 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001557 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001558 if (noexmode) // End of ":global/path/visual" commands
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001559 goto theend;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001560 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001561 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001562 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001563 {
1564#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001565 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001566 && oa.op_type == OP_NOP && oa.regname == NUL
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001567 && !VIsual_active
1568 && !skip_term_loop)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001569 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001570 // If terminal_loop() returns OK we got a key that is handled
Christian Brabandtee17b6f2023-09-09 11:23:50 +02001571 // in Normal mode. With FAIL we first need to position the
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001572 // cursor and the screen needs to be redrawn.
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001573 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001574 normal_cmd(&oa, TRUE);
1575 }
1576 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001577#endif
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001578 {
1579#ifdef FEAT_TERMINAL
1580 skip_term_loop = FALSE;
1581#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001582 normal_cmd(&oa, TRUE);
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001583 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02001584 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001585 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001586
1587theend:
1588 current_oap = prev_oap;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001589}
1590
1591
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001592#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001593/*
1594 * Exit, but leave behind swap files for modified buffers.
1595 */
1596 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001597getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001598{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001599# if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001600 // Ignore SIGHUP, because a dropped connection causes a read error, which
1601 // makes Vim exit and then handling SIGHUP causes various reentrance
1602 // problems.
ichizok378447f2023-05-11 22:25:42 +01001603 mch_signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001604# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001605
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001606 ml_close_notmod(); // close all not-modified buffers
1607 ml_sync_all(FALSE, FALSE); // preserve all swap files
1608 ml_close_all(FALSE); // close all memfiles, without deleting
1609 getout(exitval); // exit Vim properly
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001610}
1611#endif
1612
1613
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001614/*
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001615 * Exit properly. This is the only way to exit Vim after startup has
1616 * succeeded. We are certain to exit here, no way to abort it.
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001617 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001618 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001619getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001620{
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001621 exiting = TRUE;
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001622#if defined(FEAT_EVAL)
Bram Moolenaar0bd052b2018-03-22 20:33:56 +01001623 ch_log(NULL, "Exiting...");
1624#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001625
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001626 // When running in Ex mode an error causes us to exit with a non-zero exit
1627 // code. POSIX requires this, although it's not 100% clear from the
1628 // standard.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001629 if (exmode_active)
1630 exitval += ex_exitval;
1631
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001632#ifdef FEAT_EVAL
1633 set_vim_var_type(VV_EXITING, VAR_NUMBER);
1634 set_vim_var_nr(VV_EXITING, exitval);
1635#endif
1636
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001637 // Position the cursor on the last screen line, below all the text
Bram Moolenaar7007e312021-03-27 12:11:33 +01001638 if (!is_not_a_term_or_gui())
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001639 windgoto((int)Rows - 1, 0);
1640
Bram Moolenaar58779852022-09-06 18:31:14 +01001641#ifdef FEAT_EVAL
1642 // Invoked all deferred functions in the function stack.
1643 invoke_all_defer();
1644#endif
1645
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001646#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001647 // Optionally print hashtable efficiency.
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001648 hash_debug_results();
1649#endif
1650
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001651#ifdef FEAT_GUI
1652 msg_didany = FALSE;
1653#endif
1654
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001655 if (v_dying <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001656 {
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001657 tabpage_T *tp;
1658 tabpage_T *next_tp;
1659 buf_T *buf;
1660 win_T *wp;
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001661 int unblock = 0;
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001662
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001663 // Trigger BufWinLeave for all windows, but only once per buffer.
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001664 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001665 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001666 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001667 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001668 {
Christian Brabandtfc682992023-09-03 20:20:52 +02001669 if (wp->w_buffer == NULL || !buf_valid(wp->w_buffer))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001670 // Autocmd must have close the buffer already, skip.
Bram Moolenaar802418d2013-01-17 14:00:11 +01001671 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001672 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001673 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001674 {
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001675 bufref_T bufref;
1676
1677 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001678 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1679 buf->b_fname, FALSE, buf);
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001680 if (bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001681 CHANGEDTICK(buf) = -1; // note we did it already
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001682
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001683 // start all over, autocommands may mess up the lists
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001684 next_tp = first_tabpage;
1685 break;
1686 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001687 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001688 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001689
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001690 // Trigger BufUnload for buffers that are loaded
Bram Moolenaar29323592016-07-24 22:04:11 +02001691 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001692 if (buf->b_ml.ml_mfp != NULL)
1693 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001694 bufref_T bufref;
1695
1696 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001697 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001698 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001699 if (!bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001700 // autocmd deleted the buffer
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001701 break;
1702 }
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001703
1704 // deathtrap() blocks autocommands, but we do want to trigger
1705 // VimLeavePre.
1706 if (is_autocmd_blocked())
1707 {
1708 unblock_autocmds();
1709 ++unblock;
1710 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001711 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001712 if (unblock)
1713 block_autocmds();
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001714 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001715
1716#ifdef FEAT_VIMINFO
Milly6eca04e2024-10-21 22:20:51 +02001717 if (
1718# ifdef EXITFREE
1719 entered_free_all_mem == FALSE &&
1720# endif
1721 *p_viminfo != NUL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001722 // Write out the registers, history, marks etc, to the viminfo file
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001723 write_viminfo(NULL, FALSE);
1724#endif
1725
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001726 if (v_dying <= 1)
Bram Moolenaarc7226682019-08-17 16:33:23 +02001727 {
1728 int unblock = 0;
1729
1730 // deathtrap() blocks autocommands, but we do want to trigger VimLeave.
1731 if (is_autocmd_blocked())
1732 {
1733 unblock_autocmds();
1734 ++unblock;
1735 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001736 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc7226682019-08-17 16:33:23 +02001737 if (unblock)
1738 block_autocmds();
1739 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001740
Bram Moolenaar05159a02005-02-26 23:04:13 +00001741#ifdef FEAT_PROFILE
1742 profile_dump();
1743#endif
1744
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001745 if (did_emsg
1746#ifdef FEAT_GUI
1747 || (gui.in_use && msg_didany && p_verbose > 0)
1748#endif
1749 )
1750 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001751 // give the user a chance to read the (error) message
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001752 no_wait_return = FALSE;
Volodymyr Kot7f0c4b42021-11-21 12:27:13 +00001753 wait_return(FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001754 }
1755
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001756 // Position the cursor again, the autocommands may have moved it
Bram Moolenaar7007e312021-03-27 12:11:33 +01001757 if (!is_not_a_term_or_gui())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001758 windgoto((int)Rows - 1, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001759
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001760#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001761 job_stop_on_exit();
1762#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001763#ifdef FEAT_LUA
1764 lua_end();
1765#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001766#ifdef FEAT_MZSCHEME
1767 mzscheme_end();
1768#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001769#ifdef FEAT_TCL
1770 tcl_end();
1771#endif
1772#ifdef FEAT_RUBY
1773 ruby_end();
1774#endif
1775#ifdef FEAT_PYTHON
1776 python_end();
1777#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001778#ifdef FEAT_PYTHON3
1779 python3_end();
1780#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001781#ifdef FEAT_PERL
1782 perl_end();
1783#endif
1784#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1785 iconv_end();
1786#endif
1787#ifdef FEAT_NETBEANS_INTG
1788 netbeans_end();
1789#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001790#ifdef FEAT_CSCOPE
1791 cs_end();
1792#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001793#ifdef FEAT_EVAL
1794 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001795 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001796#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001797#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +02001798 free_cmd_argsW();
1799#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001800
1801 mch_exit(exitval);
1802}
1803
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001804/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001805 * Get the name of the display, before gui_prepare() removes it from
1806 * argv[]. Used for the xterm-clipboard display.
1807 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001808 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001809 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001810 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001811early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001812{
Bram Moolenaar03005972008-11-20 13:12:36 +00001813#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1814 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001815 int argc = parmp->argc;
1816 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001817 int i;
1818
1819 for (i = 1; i < argc; i++)
1820 {
1821 if (STRCMP(argv[i], "--") == 0)
1822 break;
1823# ifdef FEAT_XCLIPBOARD
1824 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001825# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001826 || STRICMP(argv[i], "--display") == 0
1827# endif
1828 )
1829 {
1830 if (i == argc - 1)
1831 mainerr_arg_missing((char_u *)argv[i]);
1832 xterm_display = argv[++i];
1833 }
1834# endif
1835# ifdef FEAT_CLIENTSERVER
1836 else if (STRICMP(argv[i], "--servername") == 0)
1837 {
1838 if (i == argc - 1)
1839 mainerr_arg_missing((char_u *)argv[i]);
1840 parmp->serverName_arg = (char_u *)argv[++i];
1841 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001842 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001843 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001844 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001845 {
1846 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001847# ifdef FEAT_GUI
1848 if (strstr(argv[i], "-wait") != 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001849 // don't fork() when starting the GUI to edit files ourself
Bram Moolenaareb94e552006-03-11 21:35:11 +00001850 gui.dofork = FALSE;
1851# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001852 }
1853# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001854
Bram Moolenaar4f974752019-02-17 17:44:42 +01001855# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
1856# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001857 else if (STRICMP(argv[i], "--windowid") == 0)
1858# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001859 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001860# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001861 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001862 long_u id;
1863 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001864
1865 if (i == argc - 1)
1866 mainerr_arg_missing((char_u *)argv[i]);
1867 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001868 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001869 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001870 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001871 if (count != 1)
1872 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1873 else
Bram Moolenaar4f974752019-02-17 17:44:42 +01001874# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001875 win_socket_id = id;
1876# else
1877 gtk_socket_id = id;
1878# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001879 i++;
1880 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001881# endif
1882# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001883 else if (STRICMP(argv[i], "--echo-wid") == 0)
1884 echo_wid_arg = TRUE;
1885# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001886# ifndef FEAT_NETBEANS_INTG
1887 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001888 {
1889 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1890 mch_exit(2);
1891 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001892# endif
1893
Bram Moolenaar58d98232005-07-23 22:25:46 +00001894 }
1895#endif
1896}
1897
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001898#ifndef NO_VIM_MAIN
1899/*
1900 * Get a (optional) count for a Vim argument.
1901 */
1902 static int
1903get_number_arg(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001904 char_u *p, // pointer to argument
1905 int *idx, // index in argument, is incremented
1906 int def) // default value
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001907{
1908 if (vim_isdigit(p[*idx]))
1909 {
1910 def = atoi((char *)&(p[*idx]));
1911 while (vim_isdigit(p[*idx]))
1912 *idx = *idx + 1;
1913 }
1914 return def;
1915}
1916
1917/*
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001918 * Check for: [r][e][g][vi|vim|view][diff][ex[im]] (sort of)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001919 * If the executable name starts with "r" we disable shell commands.
1920 * If the next character is "e" we run in Easy mode.
1921 * If the next character is "g" we run the GUI version.
1922 * If the next characters are "view" we start in readonly mode.
1923 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1924 * If the next characters are "ex" we start in Ex mode. If it's followed
1925 * by "im" use improved Ex mode.
1926 */
1927 static void
1928parse_command_name(mparm_T *parmp)
1929{
1930 char_u *initstr;
1931
1932 initstr = gettail((char_u *)parmp->argv[0]);
1933
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001934#ifdef FEAT_EVAL
1935 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001936 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001937#endif
1938
1939 if (TOLOWER_ASC(initstr[0]) == 'r')
1940 {
1941 restricted = TRUE;
1942 ++initstr;
1943 }
1944
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001945 // Use evim mode for "evim" and "egvim", not for "editor".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001946 if (TOLOWER_ASC(initstr[0]) == 'e'
1947 && (TOLOWER_ASC(initstr[1]) == 'v'
1948 || TOLOWER_ASC(initstr[1]) == 'g'))
1949 {
1950#ifdef FEAT_GUI
1951 gui.starting = TRUE;
1952#endif
1953 parmp->evim_mode = TRUE;
1954 ++initstr;
1955 }
1956
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001957 // "gvim" starts the GUI. Also accept "Gvim" for MS-Windows.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001958 if (TOLOWER_ASC(initstr[0]) == 'g')
1959 {
1960 main_start_gui();
1961#ifdef FEAT_GUI
1962 ++initstr;
1963#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001964#ifdef GUI_MAY_SPAWN
1965 gui.dospawn = FALSE; // No need to spawn a new process.
1966#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001967 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001968#ifdef GUI_MAY_SPAWN
1969 else
1970 gui.dospawn = TRUE; // Not "gvim". Need to spawn gvim.exe.
1971#endif
1972
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001973
1974 if (STRNICMP(initstr, "view", 4) == 0)
1975 {
1976 readonlymode = TRUE;
1977 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001978 p_uc = 10000; // don't update very often
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001979 initstr += 4;
1980 }
1981 else if (STRNICMP(initstr, "vim", 3) == 0)
1982 initstr += 3;
1983
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001984 // Catch "[r][g]vimdiff" and "[r][g]viewdiff".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001985 if (STRICMP(initstr, "diff") == 0)
1986 {
1987#ifdef FEAT_DIFF
1988 parmp->diff_mode = TRUE;
1989#else
1990 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1991 mch_errmsg("\n");
1992 mch_exit(2);
1993#endif
1994 }
1995
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001996 // Checking for "ex" here may catch some weird names, such as "vimex" or
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001997 // "viewex", we assume the user knows that.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001998 if (STRNICMP(initstr, "ex", 2) == 0)
1999 {
2000 if (STRNICMP(initstr + 2, "im", 2) == 0)
2001 exmode_active = EXMODE_VIM;
2002 else
2003 exmode_active = EXMODE_NORMAL;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02002004 change_compatible(TRUE); // set 'compatible'
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02002005 }
2006}
2007
Bram Moolenaar58d98232005-07-23 22:25:46 +00002008/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002009 * Scan the command line arguments.
2010 */
2011 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002012command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002013{
2014 int argc = parmp->argc;
2015 char **argv = parmp->argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002016 int argv_idx; // index in argv[n][]
2017 int had_minmin = FALSE; // found "--" argument
2018 int want_argument; // option argument with argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002019 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00002020 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002021 long n;
2022
2023 --argc;
2024 ++argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002025 argv_idx = 1; // active option letter is argv[0][argv_idx]
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002026 while (argc > 0)
2027 {
2028 /*
2029 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
2030 */
2031 if (argv[0][0] == '+' && !had_minmin)
2032 {
2033 if (parmp->n_commands >= MAX_ARG_CMDS)
2034 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002035 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002036 if (argv[0][1] == NUL)
2037 parmp->commands[parmp->n_commands++] = (char_u *)"$";
2038 else
2039 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
2040 }
2041
2042 /*
2043 * Optional argument.
2044 */
2045 else if (argv[0][0] == '-' && !had_minmin)
2046 {
2047 want_argument = FALSE;
2048 c = argv[0][argv_idx++];
2049#ifdef VMS
2050 /*
2051 * VMS only uses upper case command lines. Interpret "-X" as "-x"
2052 * and "-/X" as "-X".
2053 */
2054 if (c == '/')
2055 {
2056 c = argv[0][argv_idx++];
2057 c = TOUPPER_ASC(c);
2058 }
2059 else
2060 c = TOLOWER_ASC(c);
2061#endif
2062 switch (c)
2063 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002064 case NUL: // "vim -" read from stdin
2065 // "ex -" silent mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002066 if (exmode_active)
2067 silent_mode = TRUE;
2068 else
2069 {
2070 if (parmp->edit_type != EDIT_NONE)
2071 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2072 parmp->edit_type = EDIT_STDIN;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002073 read_cmd_fd = 2; // read from stderr instead of stdin
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002074 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002075 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002076 break;
2077
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002078 case '-': // "--" don't take any more option arguments
2079 // "--help" give help message
2080 // "--version" give version message
2081 // "--clean" clean context
2082 // "--literal" take files literally
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002083 // "--startuptime fname" write timing info
2084 // "--log fname" start logging early
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002085 // "--nofork" don't fork
2086 // "--not-a-term" don't warn for not a term
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002087 // "--gui-dialog-file fname" write dialog text
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002088 // "--ttyfail" exit if not a term
2089 // "--noplugin[s]" skip plugins
2090 // "--cmd <cmd>" execute cmd before vimrc
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002091 if (STRICMP(argv[0] + argv_idx, "help") == 0)
2092 usage();
2093 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
2094 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002095 Columns = 80; // need to init Columns
2096 info_message = TRUE; // use mch_msg(), not mch_errmsg()
K.Takata33b25d12022-01-13 16:06:45 +00002097#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar3b678042021-02-11 19:08:05 +01002098 gui.starting = FALSE; // not starting GUI, will exit
Bram Moolenaar0bcadf12021-02-11 19:18:58 +01002099#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002100 list_version();
2101 msg_putchar('\n');
2102 msg_didout = FALSE;
2103 mch_exit(0);
2104 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002105 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
2106 {
2107 parmp->use_vimrc = (char_u *)"DEFAULTS";
Bram Moolenaar62dd4522018-03-14 21:20:02 +01002108#ifdef FEAT_GUI
2109 use_gvimrc = (char_u *)"NONE";
2110#endif
Bram Moolenaar07268702018-03-01 21:57:32 +01002111 parmp->clean = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002112 set_option_value_give_err((char_u *)"vif",
2113 0L, (char_u *)"NONE", 0);
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002114 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002115 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
2116 {
Bram Moolenaar53076832015-12-31 19:53:21 +01002117#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002118 parmp->literal = TRUE;
2119#endif
2120 }
2121 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
2122 {
2123#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002124 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002125#endif
2126 }
2127 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
2128 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002129 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
2130 parmp->not_a_term = TRUE;
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002131 else if (STRNICMP(argv[0] + argv_idx, "gui-dialog-file", 15)
2132 == 0)
2133 {
2134 want_argument = TRUE;
2135 argv_idx += 15;
2136 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002137 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
2138 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002139 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
2140 {
2141 want_argument = TRUE;
2142 argv_idx += 3;
2143 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002144 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
2145 {
2146 want_argument = TRUE;
2147 argv_idx += 11;
2148 }
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002149 else if (STRNICMP(argv[0] + argv_idx, "log", 3) == 0)
2150 {
2151 want_argument = TRUE;
2152 argv_idx += 3;
2153 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002154#ifdef FEAT_CLIENTSERVER
2155 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002156 ; // already processed -- no arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002157 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
2158 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
2159 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002160 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002161 if (argc > 1)
2162 {
2163 --argc;
2164 ++argv;
2165 }
2166 }
2167#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002168#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002169# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002170 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002171# else
2172 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
2173# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002174 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002175 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002176 if (argc > 1)
2177 {
2178 --argc;
2179 ++argv;
2180 }
2181 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00002182#endif
2183#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002184 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
2185 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002186 // already processed, skip
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002187 }
2188#endif
2189 else
2190 {
2191 if (argv[0][argv_idx])
2192 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2193 had_minmin = TRUE;
2194 }
2195 if (!want_argument)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002196 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002197 break;
2198
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002199 case 'A': // "-A" start in Arabic mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002200#ifdef FEAT_ARABIC
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002201 set_option_value_give_err((char_u *)"arabic", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002202#else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002203 mch_errmsg(_(e_arabic_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002204 mch_exit(2);
2205#endif
2206 break;
2207
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002208 case 'b': // "-b" binary mode
2209 // Needs to be effective before expanding file names, because
2210 // for Win32 this makes us edit a shortcut file itself,
2211 // instead of the file it links to.
Bram Moolenaar231334e2005-07-25 20:46:57 +00002212 set_options_bin(curbuf->b_p_bin, 1, 0);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002213 curbuf->b_p_bin = 1; // binary file I/O
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002214 break;
2215
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002216 case 'C': // "-C" Compatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002217 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02002218 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002219 break;
2220
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002221 case 'e': // "-e" Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002222 exmode_active = EXMODE_NORMAL;
2223 break;
2224
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002225 case 'E': // "-E" Improved Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002226 exmode_active = EXMODE_VIM;
2227 break;
2228
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002229 case 'f': // "-f" GUI: run in foreground. Amiga: open
2230 // window directly, not with newcli
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002231#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002232 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002233#endif
2234 break;
2235
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002236 case 'g': // "-g" start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002237 main_start_gui();
2238 break;
2239
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002240 case 'F': // "-F" was for Farsi mode
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002241 mch_errmsg(_(e_farsi_support_has_been_removed));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002242 mch_exit(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002243 break;
2244
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002245 case '?': // "-?" give help message (for MS-Windows)
2246 case 'h': // "-h" give help message
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002247#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002248 // Tell usage() to exit for "gvim".
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002249 gui.starting = FALSE;
2250#endif
2251 usage();
2252 break;
2253
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002254 case 'H': // "-H" start in Hebrew mode: rl + hkmap set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002255#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002256 p_hkmap = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002257 set_option_value_give_err((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002258#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002259 mch_errmsg(_(e_hebrew_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002260 mch_exit(2);
2261#endif
2262 break;
2263
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002264 case 'l': // "-l" lisp mode, 'lisp' and 'showmatch' on
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002265 set_option_value_give_err((char_u *)"lisp", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002266 p_sm = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002267 break;
2268
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002269 case 'M': // "-M" no changes or writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002270 reset_modifiable();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002271 // FALLTHROUGH
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002272
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002273 case 'm': // "-m" no writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002274 p_write = FALSE;
2275 break;
2276
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002277 case 'y': // "-y" easy mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002278#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002279 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002280#endif
2281 parmp->evim_mode = TRUE;
2282 break;
2283
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002284 case 'N': // "-N" Nocompatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002285 change_compatible(FALSE);
2286 break;
2287
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002288 case 'n': // "-n" no swap file
Bram Moolenaar67c53842010-05-22 18:28:27 +02002289#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002290 // checking for "-nb", netbeans parameters
Bram Moolenaar67c53842010-05-22 18:28:27 +02002291 if (argv[0][argv_idx] == 'b')
2292 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002293 netbeansArg = argv[0];
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002294 argv_idx = -1; // skip to next argument
Bram Moolenaar67c53842010-05-22 18:28:27 +02002295 }
2296 else
2297#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002298 parmp->no_swap_file = TRUE;
2299 break;
2300
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002301 case 'p': // "-p[N]" open N tab pages
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002302#ifdef TARGET_API_MAC_OSX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002303 // For some reason on MacOS X, an argument like:
2304 // -psn_0_10223617 is passed in when invoke from Finder
2305 // or with the 'open' command
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002306 if (argv[0][argv_idx] == 's')
2307 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002308 argv_idx = -1; // bypass full -psn
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002309 main_start_gui();
2310 break;
2311 }
2312#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002313 // default is 0: open window for each file
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002314 parmp->window_count = get_number_arg((char_u *)argv[0],
2315 &argv_idx, 0);
2316 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002317 break;
2318
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002319 case 'o': // "-o[N]" open N horizontal split windows
2320 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002321 parmp->window_count = get_number_arg((char_u *)argv[0],
2322 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002323 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002324 break;
2325
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002326 case 'O': // "-O[N]" open N vertical split windows
2327 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002328 parmp->window_count = get_number_arg((char_u *)argv[0],
2329 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002330 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002331 break;
2332
2333#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002334 case 'q': // "-q" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002335 if (parmp->edit_type != EDIT_NONE)
2336 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2337 parmp->edit_type = EDIT_QF;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002338 if (argv[0][argv_idx]) // "-q{errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002339 {
2340 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2341 argv_idx = -1;
2342 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002343 else if (argc > 1) // "-q {errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002344 want_argument = TRUE;
2345 break;
2346#endif
2347
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002348 case 'R': // "-R" readonly mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002349 readonlymode = TRUE;
2350 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002351 p_uc = 10000; // don't update very often
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002352 break;
2353
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002354 case 'r': // "-r" recovery mode
2355 case 'L': // "-L" recovery mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002356 recoverymode = 1;
2357 break;
2358
2359 case 's':
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002360 if (exmode_active) // "-s" silent (batch) mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002361 silent_mode = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002362 else // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002363 want_argument = TRUE;
2364 break;
2365
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002366 case 't': // "-t {tag}" or "-t{tag}" jump to tag
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002367 if (parmp->edit_type != EDIT_NONE)
2368 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2369 parmp->edit_type = EDIT_TAG;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002370 if (argv[0][argv_idx]) // "-t{tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002371 {
2372 parmp->tagname = (char_u *)argv[0] + argv_idx;
2373 argv_idx = -1;
2374 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002375 else // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002376 want_argument = TRUE;
2377 break;
2378
2379#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002380 case 'D': // "-D" Debugging
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002381 parmp->use_debug_break_level = 9999;
2382 break;
2383#endif
2384#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002385 case 'd': // "-d" 'diff'
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002386# ifdef AMIGA
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002387 // check for "-dev {device}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002388 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2389 want_argument = TRUE;
2390 else
2391# endif
2392 parmp->diff_mode = TRUE;
2393 break;
2394#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002395 case 'V': // "-V{N}" Verbose level
2396 // default is 10: a little bit verbose
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002397 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2398 if (argv[0][argv_idx] != NUL)
2399 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002400 set_option_value_give_err((char_u *)"verbosefile",
2401 0L, (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002402 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002403 }
2404 break;
2405
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002406 case 'v': // "-v" Vi-mode (as if called "vi")
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002407 exmode_active = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002408#if defined(FEAT_GUI) && !defined(VIMDLL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002409 gui.starting = FALSE; // don't start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002410#endif
2411 break;
2412
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002413 case 'w': // "-w{number}" set window height
2414 // "-w {scriptout}" write to script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002415 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2416 {
2417 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002418 set_option_value_give_err((char_u *)"window", n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002419 break;
2420 }
2421 want_argument = TRUE;
2422 break;
2423
2424#ifdef FEAT_CRYPT
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002425 case 'x': // "-x" encrypted reading/writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002426 parmp->ask_for_key = TRUE;
2427 break;
2428#endif
2429
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002430 case 'X': // "-X" don't connect to X server
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002431#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2432 x_no_connect = TRUE;
2433#endif
2434 break;
2435
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002436 case 'Z': // "-Z" restricted mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002437 restricted = TRUE;
2438 break;
2439
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002440 case 'c': // "-c{command}" or "-c {command}" execute
2441 // command
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002442 if (argv[0][argv_idx] != NUL)
2443 {
2444 if (parmp->n_commands >= MAX_ARG_CMDS)
2445 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002446 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2447 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002448 argv_idx = -1;
2449 break;
2450 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002451 // FALLTHROUGH
2452 case 'S': // "-S {file}" execute Vim script
2453 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002454#ifndef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002455 case 'd': // "-d {device}" device (for Amiga)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002456#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002457 case 'T': // "-T {terminal}" terminal name
2458 case 'u': // "-u {vimrc}" vim inits file
2459 case 'U': // "-U {gvimrc}" gvim inits file
2460 case 'W': // "-W {scriptout}" overwrite
Bram Moolenaar4f974752019-02-17 17:44:42 +01002461#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002462 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002463#endif
2464 want_argument = TRUE;
2465 break;
2466
2467 default:
2468 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2469 }
2470
2471 /*
2472 * Handle option arguments with argument.
2473 */
2474 if (want_argument)
2475 {
2476 /*
2477 * Check for garbage immediately after the option letter.
2478 */
2479 if (argv[0][argv_idx] != NUL)
2480 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2481
2482 --argc;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002483 if (argc < 1 && c != 'S') // -S has an optional argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002484 mainerr_arg_missing((char_u *)argv[0]);
2485 ++argv;
2486 argv_idx = -1;
2487
2488 switch (c)
2489 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002490 case 'c': // "-c {command}" execute command
2491 case 'S': // "-S {file}" execute Vim script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002492 if (parmp->n_commands >= MAX_ARG_CMDS)
2493 mainerr(ME_EXTRA_CMD, NULL);
2494 if (c == 'S')
2495 {
2496 char *a;
2497
2498 if (argc < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002499 // "-S" without argument: use default session file
2500 // name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002501 a = SESSION_FILE;
2502 else if (argv[0][0] == '-')
2503 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002504 // "-S" followed by another option: use default
2505 // session file name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002506 a = SESSION_FILE;
2507 ++argc;
2508 --argv;
2509 }
2510 else
2511 a = argv[0];
Bram Moolenaar964b3742019-05-24 18:54:09 +02002512 p = alloc(STRLEN(a) + 4);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002513 if (p == NULL)
2514 mch_exit(2);
2515 sprintf((char *)p, "so %s", a);
2516 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2517 parmp->commands[parmp->n_commands++] = p;
2518 }
2519 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002520 parmp->commands[parmp->n_commands++] =
2521 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002522 break;
2523
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002524 case '-':
2525 if (argv[-1][2] == 'c')
2526 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002527 // "--cmd {command}" execute command
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002528 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2529 mainerr(ME_EXTRA_CMD, NULL);
2530 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002531 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002532 }
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002533 // --gui-dialog-file fname
2534 if (argv[-1][2] == 'g')
2535 {
2536 // without GUI ignore the argument
2537#ifdef FEAT_GUI
2538 parmp->gui_dialog_file = (char_u *)argv[0];
2539#endif
2540 }
2541
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002542 // "--startuptime <file>" already handled
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002543 // "--log <file>" already handled
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002544 break;
2545
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002546 // case 'd': -d {device} is handled in mch_check_win() for the
2547 // Amiga
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002548
2549#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002550 case 'q': // "-q {errorfile}" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002551 parmp->use_ef = (char_u *)argv[0];
2552 break;
2553#endif
2554
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002555 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002556 set_option_value_give_err((char_u *)"vif",
2557 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002558 break;
2559
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002560 case 's': // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002561 if (scriptin[0] != NULL)
2562 {
2563scripterror:
2564 mch_errmsg(_("Attempt to open script file again: \""));
2565 mch_errmsg(argv[-1]);
2566 mch_errmsg(" ");
2567 mch_errmsg(argv[0]);
2568 mch_errmsg("\"\n");
2569 mch_exit(2);
2570 }
2571 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2572 {
2573 mch_errmsg(_("Cannot open for reading: \""));
2574 mch_errmsg(argv[0]);
2575 mch_errmsg("\"\n");
2576 mch_exit(2);
2577 }
2578 if (save_typebuf() == FAIL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002579 mch_exit(2); // out of memory
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002580 break;
2581
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002582 case 't': // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002583 parmp->tagname = (char_u *)argv[0];
2584 break;
2585
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002586 case 'T': // "-T {terminal}" terminal name
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002587 /*
2588 * The -T term argument is always available and when
2589 * HAVE_TERMLIB is supported it overrides the environment
2590 * variable TERM.
2591 */
2592#ifdef FEAT_GUI
2593 if (term_is_gui((char_u *)argv[0]))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002594 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002595 else
2596#endif
2597 parmp->term = (char_u *)argv[0];
2598 break;
2599
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002600 case 'u': // "-u {vimrc}" vim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002601 parmp->use_vimrc = (char_u *)argv[0];
2602 break;
2603
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002604 case 'U': // "-U {gvimrc}" gvim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002605#ifdef FEAT_GUI
2606 use_gvimrc = (char_u *)argv[0];
2607#endif
2608 break;
2609
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002610 case 'w': // "-w {nr}" 'window' value
2611 // "-w {scriptout}" append to script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002612 if (vim_isdigit(*((char_u *)argv[0])))
2613 {
2614 argv_idx = 0;
2615 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002616 set_option_value_give_err((char_u *)"window",
2617 n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002618 argv_idx = -1;
2619 break;
2620 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002621 // FALLTHROUGH
2622 case 'W': // "-W {scriptout}" overwrite script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002623 if (scriptout != NULL)
2624 goto scripterror;
2625 if ((scriptout = mch_fopen(argv[0],
2626 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2627 {
2628 mch_errmsg(_("Cannot open for script output: \""));
2629 mch_errmsg(argv[0]);
2630 mch_errmsg("\"\n");
2631 mch_exit(2);
2632 }
2633 break;
2634
Bram Moolenaar4f974752019-02-17 17:44:42 +01002635#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002636 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002637 gui_mch_set_parent(argv[0]);
2638 break;
2639#endif
2640 }
2641 }
2642 }
2643
2644 /*
2645 * File name argument.
2646 */
2647 else
2648 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002649 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002650
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002651 // Check for only one type of editing.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002652 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2653 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2654 parmp->edit_type = EDIT_FILE;
2655
2656#ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002657 // Remember if the argument was a full path before changing
2658 // slashes to backslashes.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002659 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2660 parmp->full_path = TRUE;
2661#endif
2662
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002663 // Add the file to the global argument list.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002664 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2665 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2666 mch_exit(2);
2667#ifdef FEAT_DIFF
2668 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2669 && !mch_isdir(alist_name(&GARGLIST[0])))
2670 {
2671 char_u *r;
2672
2673 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2674 if (r != NULL)
2675 {
2676 vim_free(p);
2677 p = r;
2678 }
2679 }
2680#endif
K.Takatab247e062022-02-07 10:45:23 +00002681#ifdef __CYGWIN__
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002682 /*
2683 * If vim is invoked by non-Cygwin tools, convert away any
2684 * DOS paths, so things like .swp files are created correctly.
2685 * Look for evidence of non-Cygwin paths before we bother.
2686 * This is only for when using the Unix files.
2687 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002688 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002689 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002690 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002691
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002692# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002693 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002694# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002695 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002696# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002697 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002698 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002699 if (p == NULL)
2700 mch_exit(2);
2701 }
2702#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002703
2704#ifdef USE_FNAME_CASE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002705 // Make the case of the file name match the actual file.
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002706 fname_case(p, 0);
2707#endif
2708
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002709 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002710#ifdef EXPAND_FILENAMES
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002711 parmp->literal ? 2 : 0 // add buffer nr after exp.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002712#else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002713 2 // add buffer number now and use curbuf
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002714#endif
2715 );
2716
Bram Moolenaar4f974752019-02-17 17:44:42 +01002717#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002718 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002719 // Remember this argument has been added to the argument list.
2720 // Needed when 'encoding' is changed.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002721 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002722# ifdef FEAT_DIFF
2723 parmp->diff_mode
2724# else
2725 FALSE
2726# endif
2727 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002728 }
2729#endif
2730 }
2731
2732 /*
2733 * If there are no more letters after the current "-", go to next
2734 * argument. argv_idx is set to -1 when the current argument is to be
2735 * skipped.
2736 */
2737 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2738 {
2739 --argc;
2740 ++argv;
2741 argv_idx = 1;
2742 }
2743 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002744
2745#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002746 // If there is a "+123" or "-c" command, set v:swapcommand to the first
2747 // one.
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002748 if (parmp->n_commands > 0)
2749 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002750 p = alloc(STRLEN(parmp->commands[0]) + 3);
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002751 if (p != NULL)
2752 {
2753 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2754 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2755 vim_free(p);
2756 }
2757 }
2758#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002759}
2760
2761/*
2762 * Print a warning if stdout is not a terminal.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +01002763 * When starting in Ex mode and commands come from a file, set silent_mode.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002764 */
2765 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002766check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002767{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002768 int input_isatty; // is active input a terminal?
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002769
2770 input_isatty = mch_input_isatty();
2771 if (exmode_active)
2772 {
2773 if (!input_isatty)
2774 silent_mode = TRUE;
2775 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002776 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002777#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002778 // don't want the delay when started from the desktop
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002779 && !gui.starting
2780#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002781 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002782 {
2783#ifdef NBDEBUG
2784 /*
2785 * This shouldn't be necessary. But if I run netbeans with the log
2786 * output coming to the console and XOpenDisplay fails, I get vim
2787 * trying to start with input/output to my console tty. This fills my
2788 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002789 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002790 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002791 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002792 {
2793 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2794 exit(1);
2795 }
2796#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002797#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2798 if (
2799# ifdef VIMDLL
2800 !gui.starting &&
2801# endif
2802 is_cygpty_used())
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002803 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002804# if defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
Bram Moolenaar2a027452017-09-26 19:10:37 +02002805 && defined(FEAT_GETTEXT)
2806 char *s, *tofree = NULL;
2807
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002808 // Set the encoding of the error message based on $LC_ALL or
2809 // other environment variables instead of 'encoding'.
2810 // Note that the message is shown on a Cygwin terminal (e.g.
2811 // mintty) which encoding is based on $LC_ALL or etc., not the
2812 // current codepage used by normal Win32 console programs.
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002813 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002814 if (s == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002815 s = "utf-8"; // Use "utf-8" by default.
Bram Moolenaar2a027452017-09-26 19:10:37 +02002816 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2817 vim_free(tofree);
2818# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002819 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2820 exit(1);
2821 }
2822#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002823 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002824 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2825 if (!input_isatty)
2826 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2827 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002828 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2829 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002830 if (scriptin[0] == NULL)
Bram Moolenaareda1da02019-11-17 17:06:33 +01002831 ui_delay(2005L, TRUE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002832 TIME_MSG("Warning delay");
2833 }
2834}
2835
2836/*
2837 * Read text from stdin.
2838 */
2839 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002840read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002841{
2842 int i;
2843
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002844 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002845 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002846
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002847 no_wait_return = TRUE;
2848 i = msg_didany;
2849 set_buflisted(TRUE);
Bram Moolenaar204ade62020-10-11 14:58:46 +02002850
2851 // Create memfile and read from stdin.
Bram Moolenaar204ade62020-10-11 14:58:46 +02002852 (void)open_buffer(TRUE, NULL, 0);
2853
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002854 no_wait_return = FALSE;
2855 msg_didany = i;
2856 TIME_MSG("reading stdin");
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002857
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002858 check_swap_exists_action();
Bram Moolenaar8e1cbb52020-12-08 19:36:21 +01002859
2860#if !(defined(AMIGA) || defined(MACOS_X))
2861 // Dup stdin from stderr to read commands from, so that shell commands
2862 // work.
2863 // TODO: why is this needed, even though readfile() has done this?
2864 close(0);
2865 vim_ignored = dup(2);
2866#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002867}
2868
2869/*
2870 * Create the requested number of windows and edit buffers in them.
2871 * Also does recovery if "recoverymode" set.
2872 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002873 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002874create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002875{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002876 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002877 int done = 0;
2878
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002879 /*
2880 * Create the number of windows that was requested.
2881 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002882 if (parmp->window_count == -1) // was not set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002883 parmp->window_count = 1;
2884 if (parmp->window_count == 0)
2885 parmp->window_count = GARGCOUNT;
2886 if (parmp->window_count > 1)
2887 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002888 // Don't change the windows if there was a command in .vimrc that
2889 // already split some windows
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002890 if (parmp->window_layout == 0)
2891 parmp->window_layout = WIN_HOR;
2892 if (parmp->window_layout == WIN_TABS)
2893 {
2894 parmp->window_count = make_tabpages(parmp->window_count);
2895 TIME_MSG("making tab pages");
2896 }
2897 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002898 {
2899 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002900 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002901 TIME_MSG("making windows");
2902 }
2903 else
2904 parmp->window_count = win_count();
2905 }
2906 else
2907 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002908
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002909 if (recoverymode) // do recover
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002910 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002911 msg_scroll = TRUE; // scroll message up
Bram Moolenaar99499b12019-05-23 21:35:48 +02002912 ml_recover(TRUE);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002913 if (curbuf->b_ml.ml_mfp == NULL) // failed
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002914 getout(1);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002915 do_modelines(0); // do modelines
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002916 }
2917 else
2918 {
2919 /*
2920 * Open a buffer for windows that don't have one yet.
2921 * Commands in the .vimrc might have loaded a file or split the window.
2922 * Watch out for autocommands that delete a window.
2923 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002924 /*
2925 * Don't execute Win/Buf Enter/Leave autocommands here
2926 */
2927 ++autocmd_no_enter;
2928 ++autocmd_no_leave;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002929 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002930 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002931 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002932 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002933 {
2934 if (parmp->window_layout == WIN_TABS)
2935 goto_tabpage(1);
2936 else
2937 curwin = firstwin;
2938 }
2939 else if (parmp->window_layout == WIN_TABS)
2940 {
2941 if (curtab->tp_next == NULL)
2942 break;
2943 goto_tabpage(0);
2944 }
2945 else
2946 {
2947 if (curwin->w_next == NULL)
2948 break;
2949 curwin = curwin->w_next;
2950 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002951 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002952 curbuf = curwin->w_buffer;
2953 if (curbuf->b_ml.ml_mfp == NULL)
2954 {
2955#ifdef FEAT_FOLDING
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002956 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002957 if (p_fdls >= 0)
2958 curwin->w_p_fdl = p_fdls;
2959#endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002960 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002961 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002962
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002963 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002964
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002965 // create memfile, read file
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002966 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002967
Bram Moolenaar84212822006-11-07 21:59:47 +00002968 if (swap_exists_action == SEA_QUIT)
2969 {
2970 if (got_int || only_one_window())
2971 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002972 // abort selected or quit and only one window
2973 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00002974 getout(1);
2975 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002976 // We can't close the window, it would disturb what
2977 // happens next. Clear the file name and set the arg
2978 // index to -1 to delete it later.
Bram Moolenaar84212822006-11-07 21:59:47 +00002979 setfname(curbuf, NULL, NULL, FALSE);
2980 curwin->w_arg_idx = -1;
2981 swap_exists_action = SEA_NONE;
2982 }
2983 else
2984 handle_swap_exists(NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002985 dorewind = TRUE; // start again
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002986 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002987 ui_breakcheck();
2988 if (got_int)
2989 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002990 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002991 break;
2992 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002993 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002994 if (parmp->window_layout == WIN_TABS)
2995 goto_tabpage(1);
2996 else
2997 curwin = firstwin;
2998 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002999 --autocmd_no_enter;
3000 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003001 }
3002}
3003
Bram Moolenaar9810cfb2019-12-11 21:23:00 +01003004/*
3005 * If opened more than one window, start editing files in the other
3006 * windows. make_windows() has already opened the windows.
3007 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003008 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003009edit_buffers(
3010 mparm_T *parmp,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003011 char_u *cwd) // current working dir
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003012{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003013 int arg_idx; // index in argument list
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003014 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00003015 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003016 win_T *win;
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003017 char_u *p_shm_save = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003018
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003019 /*
3020 * Don't execute Win/Buf Enter/Leave autocommands here
3021 */
3022 ++autocmd_no_enter;
3023 ++autocmd_no_leave;
Bram Moolenaar84212822006-11-07 21:59:47 +00003024
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003025 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00003026 if (curwin->w_arg_idx == -1)
3027 {
3028 win_close(curwin, TRUE);
3029 advance = FALSE;
3030 }
3031
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003032 arg_idx = 1;
3033 for (i = 1; i < parmp->window_count; ++i)
3034 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02003035 if (cwd != NULL)
3036 mch_chdir((char *)cwd);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003037 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00003038 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003039 {
Bram Moolenaar84212822006-11-07 21:59:47 +00003040 ++arg_idx;
3041 win_close(curwin, TRUE);
3042 advance = FALSE;
3043 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003044 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003045
Bram Moolenaar84212822006-11-07 21:59:47 +00003046 if (advance)
3047 {
3048 if (parmp->window_layout == WIN_TABS)
3049 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003050 if (curtab->tp_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00003051 break;
3052 goto_tabpage(0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003053 // Temporarily reset 'shm' option to not print fileinfo when
3054 // loading the other buffers. This would overwrite the already
3055 // existing fileinfo for the first tab.
3056 if (i == 1)
3057 {
3058 char buf[100];
3059
3060 p_shm_save = vim_strsave(p_shm);
3061 vim_snprintf(buf, 100, "F%s", p_shm);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003062 set_option_value_give_err((char_u *)"shm",
3063 0L, (char_u *)buf, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003064 }
Bram Moolenaar84212822006-11-07 21:59:47 +00003065 }
3066 else
3067 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003068 if (curwin->w_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00003069 break;
3070 win_enter(curwin->w_next, FALSE);
3071 }
3072 }
3073 advance = TRUE;
3074
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003075 // Only open the file if there is no file in this window yet (that can
3076 // happen when .vimrc contains ":sall").
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003077 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
3078 {
3079 curwin->w_arg_idx = arg_idx;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003080 // Edit file from arg list, if there is one. When "Quit" selected
3081 // at the ATTENTION prompt close the window.
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003082 swap_exists_did_quit = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003083 (void)do_ecmd(0, arg_idx < GARGCOUNT
3084 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003085 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003086 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00003087 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003088 // abort or quit selected
Bram Moolenaar84212822006-11-07 21:59:47 +00003089 if (got_int || only_one_window())
3090 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003091 // abort selected and only one window
3092 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00003093 getout(1);
3094 }
3095 win_close(curwin, TRUE);
3096 advance = FALSE;
3097 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003098 if (arg_idx == GARGCOUNT - 1)
3099 arg_had_last = TRUE;
3100 ++arg_idx;
3101 }
3102 ui_breakcheck();
3103 if (got_int)
3104 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003105 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003106 break;
3107 }
3108 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003109
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003110 if (p_shm_save != NULL)
3111 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003112 set_option_value_give_err((char_u *)"shm", 0L, p_shm_save, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003113 vim_free(p_shm_save);
3114 }
3115
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003116 if (parmp->window_layout == WIN_TABS)
3117 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003118 --autocmd_no_enter;
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003119
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003120 // make the first window the current window
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003121 win = firstwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003122#if defined(FEAT_QUICKFIX)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003123 // Avoid making a preview window the current window.
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003124 while (win->w_p_pvw)
3125 {
3126 win = win->w_next;
3127 if (win == NULL)
3128 {
3129 win = firstwin;
3130 break;
3131 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003132 }
3133#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003134 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003135
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003136 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003137 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003138 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003139 win_equal(curwin, FALSE, 'b'); // adjust heights
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003140}
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003141
3142/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003143 * Execute the commands from --cmd arguments "cmds[cnt]".
3144 */
3145 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003146exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003147{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003148 char_u **cmds = parmp->pre_commands;
3149 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003150 int i;
ichizok7e5fe382023-04-15 13:17:50 +01003151 ESTACK_CHECK_DECLARATION;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003152
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003153 if (cnt <= 0)
3154 return;
3155
3156 curwin->w_cursor.lnum = 0; // just in case..
3157 estack_push(ETYPE_ARGS, (char_u *)_("pre-vimrc command line"), 0);
ichizok7e5fe382023-04-15 13:17:50 +01003158 ESTACK_CHECK_SETUP;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003159# ifdef FEAT_EVAL
ichizok7e5fe382023-04-15 13:17:50 +01003160 current_sctx.sc_sid = SID_CMDARG;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003161# endif
ichizok7e5fe382023-04-15 13:17:50 +01003162 for (i = 0; i < cnt; ++i)
3163 do_cmdline_cmd(cmds[i]);
3164 ESTACK_CHECK_NOW;
3165 estack_pop();
Bram Moolenaar58d98232005-07-23 22:25:46 +00003166# ifdef FEAT_EVAL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003167 current_sctx.sc_sid = 0;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003168# endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003169 TIME_MSG("--cmd commands");
Bram Moolenaar58d98232005-07-23 22:25:46 +00003170}
3171
3172/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003173 * Execute "+", "-c" and "-S" arguments.
3174 */
3175 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003176exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003177{
3178 int i;
ichizok7e5fe382023-04-15 13:17:50 +01003179 ESTACK_CHECK_DECLARATION;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003180
3181 /*
3182 * We start commands on line 0, make "vim +/pat file" match a
3183 * pattern on line 1. But don't move the cursor when an autocommand
3184 * with g`" was used.
3185 */
3186 msg_scroll = TRUE;
3187 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
3188 curwin->w_cursor.lnum = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003189 estack_push(ETYPE_ARGS, (char_u *)"command line", 0);
ichizok7e5fe382023-04-15 13:17:50 +01003190 ESTACK_CHECK_SETUP;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003191#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003192 current_sctx.sc_sid = SID_CARG;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003193 current_sctx.sc_seq = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003194#endif
3195 for (i = 0; i < parmp->n_commands; ++i)
3196 {
3197 do_cmdline_cmd(parmp->commands[i]);
3198 if (parmp->cmds_tofree[i])
3199 vim_free(parmp->commands[i]);
3200 }
ichizok7e5fe382023-04-15 13:17:50 +01003201 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003202 estack_pop();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003203#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003204 current_sctx.sc_sid = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003205#endif
3206 if (curwin->w_cursor.lnum == 0)
3207 curwin->w_cursor.lnum = 1;
3208
3209 if (!exmode_active)
3210 msg_scroll = FALSE;
3211
3212#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003213 // When started with "-q errorfile" jump to first error again.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003214 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003215 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003216#endif
3217 TIME_MSG("executing command arguments");
3218}
3219
3220/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003221 * Source startup scripts.
3222 */
3223 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003224source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003225{
3226 int i;
3227
3228 /*
3229 * For "evim" source evim.vim first of all, so that the user can overrule
3230 * any things he doesn't like.
3231 */
3232 if (parmp->evim_mode)
3233 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003235 TIME_MSG("source evim file");
3236 }
3237
3238 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003239 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00003240 * nothing else.
3241 */
3242 if (parmp->use_vimrc != NULL)
3243 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003244 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003245 {
3246 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE, NULL)
3247 != OK)
Bram Moolenaar50809a42023-05-20 16:39:07 +01003248 emsg(_(e_failed_to_source_defaults));
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003249 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003250 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003251 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003252 {
3253#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003254 if (use_gvimrc == NULL) // don't load gvimrc either
Bram Moolenaar58d98232005-07-23 22:25:46 +00003255 use_gvimrc = parmp->use_vimrc;
3256#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003257 }
3258 else
3259 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00003261 semsg(_(e_cannot_read_from_str_2), parmp->use_vimrc);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003262 }
3263 }
3264 else if (!silent_mode)
3265 {
3266#ifdef AMIGA
3267 struct Process *proc = (struct Process *)FindTask(0L);
3268 APTR save_winptr = proc->pr_WindowPtr;
3269
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003270 // Avoid a requester here for a volume that doesn't exist.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003271 proc->pr_WindowPtr = (APTR)-1L;
3272#endif
3273
3274 /*
3275 * Get system wide defaults, if the file name is defined.
3276 */
3277#ifdef SYS_VIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003279#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003280#ifdef MACOS_X
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE,
3282 DOSO_NONE, NULL);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003283#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003284
3285 /*
3286 * Try to read initialization commands from the following places:
3287 * - environment variable VIMINIT
3288 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3289 * - second user vimrc file ($VIM/.vimrc for Dos)
3290 * - environment variable EXINIT
3291 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3292 * - second user exrc file ($VIM/.exrc for Dos)
3293 * The first that exists is used, the rest is ignored.
3294 */
3295 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3296 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 if (do_source((char_u *)USR_VIMRC_FILE, TRUE,
3298 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003299#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003300 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003302#endif
Luca Saccarolac9df1fb2024-04-14 22:53:22 +02003303#ifdef XDG_VIMRC_FILE
3304 && do_source((char_u *)XDG_VIMRC_FILE, TRUE,
3305 DOSO_VIMRC, NULL) == FAIL
3306#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003307#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003308 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003310#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003311#ifdef USR_VIMRC_FILE4
3312 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003314#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003315 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 && do_source((char_u *)USR_EXRC_FILE, FALSE,
3317 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003318#ifdef USR_EXRC_FILE2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 && do_source((char_u *)USR_EXRC_FILE2, FALSE,
3320 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003321#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003322 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003323 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003324 // When no .vimrc file was found: source defaults.vim.
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003325 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE,
3326 NULL) == FAIL)
Bram Moolenaar50809a42023-05-20 16:39:07 +01003327 emsg(_(e_failed_to_source_defaults));
Bram Moolenaar58d98232005-07-23 22:25:46 +00003328 }
3329 }
3330
3331 /*
3332 * Read initialization commands from ".vimrc" or ".exrc" in current
3333 * directory. This is only done if the 'exrc' option is set.
3334 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003335 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003336 * option has been reset in environment of global ".exrc" or ".vimrc".
3337 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3338 * SYS_VIMRC_FILE.
3339 */
3340 if (p_exrc)
3341 {
3342#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003343 // If ".vimrc" file is not owned by user, set 'secure' mode.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003344 if (!file_owned(VIMRC_FILE))
3345#endif
3346 secure = p_secure;
3347
3348 i = FAIL;
3349 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003350 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003351#ifdef USR_VIMRC_FILE2
3352 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003353 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003354#endif
3355#ifdef USR_VIMRC_FILE3
3356 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003357 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003358#endif
3359#ifdef SYS_VIMRC_FILE
3360 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003361 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003362#endif
3363 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003365
3366 if (i == FAIL)
3367 {
3368#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003369 // if ".exrc" is not owned by user set 'secure' mode
Bram Moolenaar58d98232005-07-23 22:25:46 +00003370 if (!file_owned(EXRC_FILE))
3371 secure = p_secure;
3372 else
3373 secure = 0;
3374#endif
3375 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003376 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003377#ifdef USR_EXRC_FILE2
3378 && fullpathcmp((char_u *)USR_EXRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003379 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003380#endif
3381 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 (void)do_source((char_u *)EXRC_FILE, FALSE,
3383 DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003384 }
3385 }
3386 if (secure == 2)
3387 need_wait_return = TRUE;
3388 secure = 0;
3389#ifdef AMIGA
3390 proc->pr_WindowPtr = save_winptr;
3391#endif
3392 }
3393 TIME_MSG("sourcing vimrc file(s)");
3394}
3395
3396/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003397 * Setup to start using the GUI. Exit with an error when not available.
3398 */
3399 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003400main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003401{
3402#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003403 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003404#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003405 mch_errmsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003406 mch_errmsg("\n");
3407 mch_exit(2);
3408#endif
3409}
3410
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003411#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003412
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003413/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414 * Get an environment variable and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003415 * Returns FAIL if the environment variable was not executed, OK otherwise.
3416 */
3417 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003418process_env(
3419 char_u *env,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003420 int is_viminit) // when TRUE, called for VIMINIT
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003421{
3422 char_u *initstr;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003423 sctx_T save_current_sctx;
ichizok7e5fe382023-04-15 13:17:50 +01003424 ESTACK_CHECK_DECLARATION;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003425
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003426 if ((initstr = mch_getenv(env)) == NULL || *initstr == NUL)
3427 return FAIL;
3428
3429 if (is_viminit)
3430 vimrc_found(NULL, NULL);
3431 estack_push(ETYPE_ENV, env, 0);
ichizok7e5fe382023-04-15 13:17:50 +01003432 ESTACK_CHECK_SETUP;
3433 save_current_sctx = current_sctx;
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003434 current_sctx.sc_version = 1;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003435#ifdef FEAT_EVAL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003436 current_sctx.sc_sid = SID_ENV;
3437 current_sctx.sc_seq = 0;
3438 current_sctx.sc_lnum = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003439#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003440
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003441 do_cmdline_cmd(initstr);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003442
ichizok7e5fe382023-04-15 13:17:50 +01003443 ESTACK_CHECK_NOW;
3444 estack_pop();
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003445 current_sctx = save_current_sctx;
3446 return OK;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003447}
3448
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003449#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003450/*
3451 * Return TRUE if we are certain the user owns the file "fname".
3452 * Used for ".vimrc" and ".exrc".
3453 * Use both stat() and lstat() for extra security.
3454 */
3455 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003456file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003457{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003458 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003459# ifdef UNIX
3460 uid_t uid = getuid();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003461# else // VMS
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003462 uid_t uid = ((getgid() << 16) | getuid());
3463# endif
3464
3465 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3466# ifdef HAVE_LSTAT
3467 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3468# endif
3469 );
3470}
3471#endif
3472
3473/*
3474 * Give an error message main_errors["n"] and exit.
3475 */
3476 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003477mainerr(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003478 int n, // one of the ME_ defines
3479 char_u *str) // extra argument or NULL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003480{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003481#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003482 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003483#endif
3484
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003485 // If this is a Windows GUI executable, show an error dialog box.
3486#ifdef VIMDLL
3487 gui.in_use = mch_is_gui_executable();
3488#endif
3489#ifdef FEAT_GUI_MSWIN
3490 gui.starting = FALSE; // Needed to show as error.
3491#endif
3492
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003493 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003494 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003495 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003496 mch_errmsg(_(main_errors[n]));
3497 if (str != NULL)
3498 {
3499 mch_errmsg(": \"");
3500 mch_errmsg((char *)str);
3501 mch_errmsg("\"");
3502 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003503 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003504
3505 mch_exit(1);
3506}
3507
3508 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003509mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003510{
3511 mainerr(ME_ARG_MISSING, str);
3512}
3513
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003514#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003515/*
3516 * print a message with three spaces prepended and '\n' appended.
3517 */
3518 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003519main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003520{
3521 mch_msg(" ");
3522 mch_msg(s);
3523 mch_msg("\n");
3524}
3525
3526/*
3527 * Print messages for "vim -h" or "vim --help" and exit.
3528 */
3529 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003530usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003531{
3532 int i;
3533 static char *(use[]) =
3534 {
3535 N_("[file ..] edit specified file(s)"),
3536 N_("- read text from stdin"),
3537 N_("-t tag edit file where tag is defined"),
3538#ifdef FEAT_QUICKFIX
3539 N_("-q [errorfile] edit file with first error")
3540#endif
3541 };
3542
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003543#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003544 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003545#endif
3546
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003547 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003548 mch_msg(longVersion);
Bram Moolenaar32aaf5a2018-06-21 21:38:33 +02003549 mch_msg(_("\n\nUsage:"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003550 for (i = 0; ; ++i)
3551 {
3552 mch_msg(_(" vim [arguments] "));
3553 mch_msg(_(use[i]));
K.Takataeeec2542021-06-02 13:28:16 +02003554 if (i == ARRAY_LENGTH(use) - 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003555 break;
3556 mch_msg(_("\n or:"));
3557 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003558#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003559 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003560#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003561
3562 mch_msg(_("\n\nArguments:\n"));
3563 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003564#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003565 main_msg(_("--literal\t\tDon't expand wildcards"));
3566#endif
3567#ifdef FEAT_OLE
3568 main_msg(_("-register\t\tRegister this gvim for OLE"));
3569 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3570#endif
3571#ifdef FEAT_GUI
3572 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3573 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3574#endif
3575 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3576 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003577 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003578 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3579#ifdef FEAT_DIFF
3580 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3581#endif
3582 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3583 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3584 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3585 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3586 main_msg(_("-M\t\t\tModifications in text not allowed"));
3587 main_msg(_("-b\t\t\tBinary mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003588 main_msg(_("-l\t\t\tLisp mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003589 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3590 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003591 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3592#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003593 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003594#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003595 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3596 main_msg(_("-r\t\t\tList swap files and exit"));
3597 main_msg(_("-r (with file name)\tRecover crashed session"));
3598 main_msg(_("-L\t\t\tSame as -r"));
3599#ifdef AMIGA
3600 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3601 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3602#endif
3603#ifdef FEAT_ARABIC
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003604 main_msg(_("-A\t\t\tStart in Arabic mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003605#endif
3606#ifdef FEAT_RIGHTLEFT
3607 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3608#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003609 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003610 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2d12c252022-06-13 21:42:45 +01003611#ifdef FEAT_GUI
3612 main_msg(_("--gui-dialog-file {fname} For testing: write dialog text"));
3613#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003614 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003615 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3616#ifdef FEAT_GUI
3617 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3618#endif
3619 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003620 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003621 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3622 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3623 main_msg(_("+\t\t\tStart at end of file"));
3624 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003625 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003626 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3627 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3628 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3629 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3630 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3631#ifdef FEAT_CRYPT
3632 main_msg(_("-x\t\t\tEdit encrypted files"));
3633#endif
3634#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3635# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar86181df2020-05-11 23:14:04 +02003636 main_msg(_("-display <display>\tConnect Vim to this particular X-server"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003637# endif
3638 main_msg(_("-X\t\t\tDo not connect to X server"));
3639#endif
3640#ifdef FEAT_CLIENTSERVER
3641 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3642 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3643 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3644 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003645 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003646 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3647 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3648 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3649 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3650#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003651#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003652 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003653#endif
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01003654#ifdef FEAT_JOB_CHANNEL
K.Takata0fd7be72022-11-09 16:29:24 +00003655 main_msg(_("--log <file>\t\tStart logging to <file> early"));
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01003656#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003657#ifdef FEAT_VIMINFO
3658 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3659#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003660 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003661 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3662 main_msg(_("--version\t\tPrint version information and exit"));
3663
3664#ifdef FEAT_GUI_X11
3665# ifdef FEAT_GUI_MOTIF
3666 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003667# endif
Bram Moolenaar86181df2020-05-11 23:14:04 +02003668 main_msg(_("-display <display>\tRun Vim on <display>"));
Bram Moolenaar9e6ba8c2020-05-12 22:21:26 +02003669 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003670 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3671 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3672 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3673 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3674 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3675 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3676 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3677 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003678 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3679 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3680 main_msg(_("-xrm <resource>\tSet the specified resource"));
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003681#endif // FEAT_GUI_X11
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003682#ifdef FEAT_GUI_GTK
3683 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003684 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3685 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003686 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3687 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003688 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003689 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
Bram Moolenaar86181df2020-05-11 23:14:04 +02003690 main_msg(_("-display <display>\tRun Vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003691 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003692 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003693 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003694#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003695#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003696# ifdef VIMDLL
3697 if (gui.starting)
3698# endif
3699 {
3700 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
3701 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
3702 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003703#endif
3704
3705#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003706 // Gnome gives extra messages for --help if we continue, but not for -h.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003707 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003708 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003709 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003710 gui.dofork = FALSE;
3711 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003712 else
3713#endif
3714 mch_exit(0);
3715}
3716
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003717/*
3718 * Check the result of the ATTENTION dialog:
3719 * When "Quit" selected, exit Vim.
3720 * When "Recover" selected, recover the file.
3721 */
3722 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003723check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003724{
3725 if (swap_exists_action == SEA_QUIT)
3726 getout(1);
3727 handle_swap_exists(NULL);
3728}
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003729
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003730#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003731
Bram Moolenaar595297d2017-03-04 19:11:12 +01003732#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003733 static void
3734set_progpath(char_u *argv0)
3735{
3736 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003737
Bram Moolenaar4f974752019-02-17 17:44:42 +01003738# ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003739 // A relative path containing a "/" will become invalid when using ":cd",
3740 // turn it into a full path.
3741 // On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3742 // this.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003743 char_u *path = NULL;
3744
3745 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3746 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003747# else
3748 char_u buf[MAXPATHL + 1];
3749# ifdef PROC_EXE_LINK
3750 char linkbuf[MAXPATHL + 1];
3751 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003752
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003753 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3754 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003755 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003756 linkbuf[len] = NUL;
3757 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003758 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003759# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003760
3761 if (!mch_isFullName(val))
3762 {
3763 if (gettail(val) != val
3764 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3765 val = buf;
3766 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003767# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003768
Bram Moolenaar08cab962017-03-04 14:37:18 +01003769 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003770
Bram Moolenaar4f974752019-02-17 17:44:42 +01003771# ifdef MSWIN
Bram Moolenaar43663192017-03-05 14:29:12 +01003772 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003773# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003774}
3775
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003776#endif // NO_VIM_MAIN