blob: f4db631a310d33bb2ba5d76a9fd66f459489db0c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
Bram Moolenaarb4210b32004-06-13 14:51:16 +000010#define EXTERN
11#include "vim.h"
12
Bram Moolenaarb4210b32004-06-13 14:51:16 +000013#ifdef __CYGWIN__
K.Takata972db232022-02-04 10:45:38 +000014# include <cygwin/version.h>
15# include <sys/cygwin.h> // for cygwin_conv_to_posix_path() and/or
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010016 // cygwin_conv_path()
Bram Moolenaarb4210b32004-06-13 14:51:16 +000017# include <limits.h>
18#endif
19
Bram Moolenaarafde13b2019-04-28 19:46:49 +020020#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar97ff9b92016-06-26 20:37:46 +020021# include "iscygpty.h"
22#endif
23
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010024// Values for edit_type.
25#define EDIT_NONE 0 // no edit type yet
26#define EDIT_FILE 1 // file name argument[s] given, use argument list
27#define EDIT_STDIN 2 // read file from stdin
28#define EDIT_TAG 3 // tag name argument given, use tagname
29#define EDIT_QF 4 // start in quickfix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +000030
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010031#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010032static int file_owned(char *fname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +000033#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static void mainerr(int, char_u *);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020035static void early_arg_scan(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010036#ifndef NO_VIM_MAIN
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010037static void usage(void);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void parse_command_name(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static void command_line_scan(mparm_T *parmp);
40static void check_tty(mparm_T *parmp);
41static void read_stdin(void);
42static void create_windows(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010043static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010044static void exe_pre_commands(mparm_T *parmp);
45static void exe_commands(mparm_T *parmp);
46static void source_startup_scripts(mparm_T *parmp);
47static void main_start_gui(void);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010048static void check_swap_exists_action(void);
Bram Moolenaar08cab962017-03-04 14:37:18 +010049# ifdef FEAT_EVAL
50static void set_progpath(char_u *argv0);
51# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000052#endif
53
54
Bram Moolenaarb4210b32004-06-13 14:51:16 +000055/*
56 * Different types of error messages.
57 */
58static char *(main_errors[]) =
59{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000060 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000061#define ME_UNKNOWN_OPTION 0
62 N_("Too many edit arguments"),
63#define ME_TOO_MANY_ARGS 1
64 N_("Argument missing after"),
65#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +000066 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000067#define ME_GARBAGE 3
68 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
69#define ME_EXTRA_CMD 4
70 N_("Invalid argument for"),
71#define ME_INVALID_ARG 5
72};
73
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010074#ifndef PROTO // don't want a prototype for main()
Bram Moolenaara6044292017-04-02 18:19:53 +020075
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010076// Various parameters passed between main() and other functions.
Bram Moolenaara6044292017-04-02 18:19:53 +020077static mparm_T params;
78
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010079#ifndef NO_VIM_MAIN // skip this for unittests
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020080
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010081static char_u *start_dir = NULL; // current working dir on startup
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020082
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +020083static int has_dash_c_arg = FALSE;
84
Bram Moolenaarafde13b2019-04-28 19:46:49 +020085# ifdef VIMDLL
86__declspec(dllexport)
87# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000088 int
Bram Moolenaar796cc422019-04-03 20:31:00 +020089# ifdef MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +000090VimMain
91# else
92main
93# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +010094(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +000095{
Bram Moolenaar77780b62018-03-01 23:10:45 +010096#if defined(STARTUPTIME) || defined(CLEAN_RUNTIMEPATH)
Bram Moolenaar3f269672009-11-03 11:11:11 +000097 int i;
98#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000099
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000100 /*
101 * Do any system-specific initialisations. These can NOT use IObuff or
102 * NameBuff. Thus emsg2() cannot be called!
103 */
104 mch_early_init();
105
Bram Moolenaar4f974752019-02-17 17:44:42 +0100106#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +0200107 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200108 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200109 * convert when 'encoding' changes. Get the unexpanded arguments.
110 */
111 argc = get_cmd_argsW(&argv);
112#endif
113
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100114 // Many variables are in "params" so that we can pass them to invoked
115 // functions without a lot of arguments. "argc" and "argv" are also
116 // copied, so that they can be changed.
Bram Moolenaara80faa82020-04-12 19:37:17 +0200117 CLEAR_FIELD(params);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000118 params.argc = argc;
119 params.argv = argv;
120 params.want_full_screen = TRUE;
121#ifdef FEAT_EVAL
122 params.use_debug_break_level = -1;
123#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000124 params.window_count = -1;
Bram Moolenaar58d98232005-07-23 22:25:46 +0000125
Bram Moolenaar99685e62013-05-11 13:56:18 +0200126#ifdef FEAT_RUBY
127 {
128 int ruby_stack_start;
129 vim_ruby_init((void *)&ruby_stack_start);
130 }
131#endif
132
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000133#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000134 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000135#endif
136
137#ifdef MEM_PROFILE
138 atexit(vim_mem_profile_dump);
139#endif
140
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100141#if defined(STARTUPTIME) || defined(FEAT_JOB_CHANNEL)
142 // Need to find "--startuptime" and "--log" before actually parsing
143 // arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100144 for (i = 1; i < argc - 1; ++i)
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100145 {
146# ifdef STARTUPTIME
147 if (STRICMP(argv[i], "--startuptime") == 0 && time_fd == NULL)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000148 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000149 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000150 TIME_MSG("--- VIM STARTING ---");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000151 }
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100152# endif
153# ifdef FEAT_JOB_CHANNEL
154 if (STRICMP(argv[i], "--log") == 0)
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100155 ch_logfile((char_u *)(argv[i + 1]), (char_u *)"ao");
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100156# endif
157 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000158#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000159 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000160
Bram Moolenaar07268702018-03-01 21:57:32 +0100161#ifdef CLEAN_RUNTIMEPATH
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100162 // Need to find "--clean" before actually parsing arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100163 for (i = 1; i < argc; ++i)
164 if (STRICMP(argv[i], "--clean") == 0)
165 {
166 params.clean = TRUE;
167 break;
168 }
169#endif
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200170#ifdef MSWIN
Bram Moolenaar4a228972021-05-01 22:41:39 +0200171 // Need to find "-register" and "-unregister" before loading any libraries.
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200172 for (i = 1; i < argc; ++i)
Bram Moolenaar4a228972021-05-01 22:41:39 +0200173 if ((STRICMP(argv[i] + 1, "register") == 0
174 || STRICMP(argv[i] + 1, "unregister") == 0)
175 && (argv[i][0] == '-' || argv[i][0] == '/'))
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200176 {
177 found_register_arg = TRUE;
178 break;
179 }
180#endif
181
182 /*
183 * Various initialisations shared with tests.
184 */
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200185 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000186
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200187#ifdef VIMDLL
188 // Check if the current executable file is for the GUI subsystem.
189 gui.starting = mch_is_gui_executable();
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +0200190#elif defined(FEAT_GUI_MSWIN)
191 gui.starting = TRUE;
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200192#endif
193
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000194#ifdef FEAT_CLIENTSERVER
195 /*
196 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000197 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000198 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000199 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000200#endif
201
202 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000203 * Figure out the way to work from the command name argv[0].
204 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000205 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000206 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000207
208 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000209 * Process the command line arguments. File names are put in the global
210 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000211 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000212 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000213 TIME_MSG("parsing arguments");
214
215 /*
216 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000217 * there is no terminal version, and on Windows we can't fork one off with
218 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000219 */
220#ifdef ALWAYS_USE_GUI
221 gui.starting = TRUE;
222#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000223# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000224 /*
225 * Check if the GUI can be started. Reset gui.starting if not.
226 * Don't know about other systems, stay on the safe side and don't check.
227 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000228 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000229 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000230 if (gui_init_check() == FAIL)
231 {
232 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000233
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100234 // When running "evim" or "gvim -y" we need the menus, exit if we
235 // don't have them.
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000236 if (params.evim_mode)
237 mch_exit(1);
238 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000239 }
240# endif
241#endif
242
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000243 if (GARGCOUNT > 0)
244 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100245#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000246 /*
247 * Expand wildcards in file names.
248 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000249 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000250 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200251 start_dir = alloc(MAXPATHL);
252 if (start_dir != NULL)
253 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100254 // Temporarily add '(' and ')' to 'isfname'. These are valid
255 // filename characters but are excluded from 'isfname' to make
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000256 // "gf" work on a file name in parentheses (e.g.: see vim.h).
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000257 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000258 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000259 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200260 if (start_dir != NULL)
261 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000262 }
263#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200264 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000265 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000266
Bram Moolenaar4f974752019-02-17 17:44:42 +0100267#ifdef MSWIN
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000268 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100269 // Remember the number of entries in the argument list. If it changes
270 // we don't react on setting 'encoding'.
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000271 set_alist_count();
272 }
273#endif
274
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000275#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000276 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000277 {
278 /*
279 * If there is one filename, fully qualified, we have very probably
280 * been invoked from explorer, so change to the file's directory.
281 * Hint: to avoid this when typing a command use a forward slash.
282 * If the cd fails, it doesn't matter.
283 */
Bram Moolenaar05268152021-11-18 18:53:45 +0000284 if (vim_chdirfile(params.fname, "drop") == OK)
285 last_chdir_reason = "drop";
Bram Moolenaarf6303872015-04-03 17:59:43 +0200286 if (start_dir != NULL)
287 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000288 }
289#endif
290 TIME_MSG("expanding arguments");
291
292#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000293 if (params.diff_mode && params.window_count == -1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100294 params.window_count = 0; // open up to 3 windows
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000295#endif
296
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100297 // Don't redraw until much later.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000298 ++RedrawingDisabled;
299
300 /*
301 * When listing swap file names, don't do cursor positioning et. al.
302 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200303 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000304 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000305
306 /*
307 * When certain to start the GUI, don't check capabilities of terminal.
308 * For GTK we can't be sure, but when started from the desktop it doesn't
309 * make sense to try using a terminal.
310 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200311#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
312 || defined(VIMDLL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000313 if (gui.starting
314# ifdef FEAT_GUI_GTK
315 && !isatty(2)
316# endif
317 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000318 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000319#endif
320
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000321 /*
322 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100323 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000324 * Note that we may use mch_exit() before mch_init()!
325 */
326 mch_init();
327 TIME_MSG("shell init");
328
329#ifdef USE_XSMP
330 /*
331 * For want of anywhere else to do it, try to connect to xsmp here.
332 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
333 * Hijacking -X 'no X connection' to also disable XSMP connection as that
334 * has a similar delay upon failure.
335 * Only try if SESSION_MANAGER is set to something non-null.
336 */
337 if (!x_no_connect)
338 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000339 char *p = getenv("SESSION_MANAGER");
340
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000341 if (p != NULL && *p != NUL)
342 {
343 xsmp_init();
344 TIME_MSG("xsmp init");
345 }
346 }
347#endif
348
349 /*
350 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000351 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000352 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000353
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100354#ifdef _IOLBF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100355 // Ensure output works usefully without a tty: buffer lines instead of
356 // fully buffered.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100357 if (silent_mode)
358 setvbuf(stdout, NULL, _IOLBF, 0);
359#endif
360
Bram Moolenaar9404a182019-05-03 22:25:40 +0200361 // This message comes before term inits, but after setting "silent_mode"
362 // when the input is not a tty. Omit the message with --not-a-term.
363 if (GARGCOUNT > 1 && !silent_mode && !is_not_a_term())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000364 printf(_("%d files to edit\n"), GARGCOUNT);
365
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000366 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000367 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100368 termcapinit(params.term); // set terminal name and get terminal
369 // capabilities (will set full_screen)
370 screen_start(); // don't know where cursor is now
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000371 TIME_MSG("Termcap init");
372 }
373
374 /*
375 * Set the default values for the options that use Rows and Columns.
376 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100377 ui_get_shellsize(); // inits Rows and Columns
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000378 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000379#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100380 // Set the 'diff' option now, so that it can be checked for in a .vimrc
381 // file. There is no buffer yet though.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000382 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000383 diff_win_options(firstwin, FALSE);
384#endif
385
386 cmdline_row = Rows - p_ch;
387 msg_row = cmdline_row;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100388 screenalloc(FALSE); // allocate screen buffers
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000389 set_init_2();
390 TIME_MSG("inits 2");
391
392 msg_scroll = TRUE;
393 no_wait_return = TRUE;
394
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100395 init_mappings(); // set up initial mappings
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000396
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100397 init_highlight(TRUE, FALSE); // set the default highlight groups
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000398 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000399
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200400#if defined(FEAT_TERMRESPONSE)
401 init_term_props(TRUE);
402#endif
403
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000404#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100405 // Set the break level after the terminal is initialized.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000406 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000407#endif
408
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100409 // Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
410 // Allows for setting 'loadplugins' there.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200411 if (params.use_vimrc != NULL
412 && (STRCMP(params.use_vimrc, "NONE") == 0
413 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
414 p_lpl = FALSE;
415
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100416 // Execute --cmd arguments.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200417 exe_pre_commands(&params);
418
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100419 // Source startup scripts.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200420 source_startup_scripts(&params);
421
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100422#ifdef FEAT_MZSCHEME
423 /*
424 * Newer version of MzScheme (Racket) require earlier (trampolined)
425 * initialisation via scheme_main_setup.
426 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200427 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200428 * Do source startup scripts, so that 'mzschemedll' can be set.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100429 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200430 return mzscheme_main();
431#else
432 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100433#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200434}
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100435#endif // NO_VIM_MAIN
436#endif // PROTO
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100437
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200438/*
439 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
440 * things simple.
441 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
442 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100443 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200444vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100445{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100446#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000447#ifdef FEAT_EVAL
448 /*
449 * Read all the plugin files.
450 * Only when compiled with +eval, since most plugins need it.
451 */
452 if (p_lpl)
453 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200454 char_u *rtp_copy = NULL;
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100455 char_u *plugin_pattern = (char_u *)
456# if defined(VMS) || defined(AMIGA) // VMS and Amiga don't handle the "**".
457 "plugin/*.vim"
458# else
459 "plugin/**/*.vim"
460# endif
461 ;
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200462
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100463 // First add all package directories to 'runtimepath', so that their
464 // autoload directories can be found. Only if not done already with a
465 // :packloadall command.
466 // Make a copy of 'runtimepath', so that source_runtime does not use
467 // the pack directories.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200468 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200469 {
470 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200471 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200472 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200473
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100474 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy, plugin_pattern,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475 DIP_ALL | DIP_NOAFTER, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000476 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200477 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100478
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100479 // Only source "start" packages if not done already with a :packloadall
480 // command.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200481 if (!did_source_packages)
482 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100483 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200484
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100485 source_runtime(plugin_pattern, DIP_ALL | DIP_AFTER);
Bram Moolenaar66459b72016-08-06 19:01:55 +0200486 TIME_MSG("loading after plugins");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000487 }
488#endif
489
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000490#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100491 // Decide about window layout for diff mode after reading vimrc.
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000492 if (params.diff_mode && params.window_layout == 0)
493 {
494 if (diffopt_horizontal())
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100495 params.window_layout = WIN_HOR; // use horizontal split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000496 else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100497 params.window_layout = WIN_VER; // use vertical split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000498 }
499#endif
500
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000501 /*
502 * Recovery mode without a file name: List swap files.
503 * This uses the 'dir' option, therefore it must be after the
504 * initializations.
505 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200506 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000507 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200508 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000509 mch_exit(0);
510 }
511
512 /*
513 * Set a few option defaults after reading .vimrc files:
514 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
515 */
516 set_init_3();
517 TIME_MSG("inits 3");
518
519 /*
520 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
521 * Note that this overrides anything from a vimrc file.
522 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000523 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000524 p_uc = 0;
525
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000526#ifdef FEAT_GUI
527 if (gui.starting)
528 {
Bram Moolenaarc7226682019-08-17 16:33:23 +0200529# if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100530 // When something caused a message from a vimrc script, need to output
531 // an extra newline before the shell prompt.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000532 if (did_emsg || msg_didout)
533 putchar('\n');
Bram Moolenaarc7226682019-08-17 16:33:23 +0200534# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000535
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100536 gui_start(NULL); // will set full_screen to TRUE
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000537 TIME_MSG("starting GUI");
538
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100539 // When running "evim" or "gvim -y" we need the menus, exit if we
540 // don't have them.
Bram Moolenaar58d98232005-07-23 22:25:46 +0000541 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000542 mch_exit(1);
543 }
544#endif
545
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000546#ifdef FEAT_VIMINFO
547 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000548 * Read in registers, history etc, but not marks, from the viminfo file.
549 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000550 */
551 if (*p_viminfo != NUL)
552 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000553 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000554 TIME_MSG("reading viminfo");
555 }
556#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100557#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100558 // It's better to make v:oldfiles an empty list than NULL.
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100559 if (get_vim_var_list(VV_OLDFILES) == NULL)
560 set_vim_var_list(VV_OLDFILES, list_alloc());
561#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000562
563#ifdef FEAT_QUICKFIX
564 /*
565 * "-q errorfile": Load the error file now.
566 * If the error file can't be read, exit before doing anything else.
567 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000568 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000569 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100570 char_u *enc = NULL;
571
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100572 enc = p_menc;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000573 if (params.use_ef != NULL)
574 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000575 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200576 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100577 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000578 {
579 out_char('\n');
580 mch_exit(3);
581 }
582 TIME_MSG("reading errorfile");
583 }
584#endif
585
586 /*
587 * Start putting things on the screen.
588 * Scroll screen down before drawing over it
589 * Clear screen now, so file message will not be cleared.
590 */
591 starting = NO_BUFFERS;
592 no_wait_return = FALSE;
593 if (!exmode_active)
594 msg_scroll = FALSE;
595
596#ifdef FEAT_GUI
597 /*
598 * This seems to be required to make callbacks to be called now, instead
599 * of after things have been put on the screen, which then may be deleted
600 * when getting a resize callback.
601 * For the Mac this handles putting files dropped on the Vim icon to
602 * global_alist.
603 */
604 if (gui.in_use)
605 {
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100606 gui_wait_for_chars(50L, typebuf.tb_change_cnt);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000607 TIME_MSG("GUI delay");
608 }
609#endif
610
611#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
612 qnx_clip_init();
613#endif
614
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200615#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
616 clip_init(TRUE);
617#endif
618
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000619#ifdef FEAT_XCLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100620 // Start using the X clipboard, unless the GUI was started.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000621# ifdef FEAT_GUI
622 if (!gui.in_use)
623# endif
624 {
625 setup_term_clip();
626 TIME_MSG("setup clipboard");
627 }
628#endif
629
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000630#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100631 // Prepare for being a Vim server.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000632 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000633#endif
634
635 /*
636 * If "-" argument given: Read file from stdin.
637 * Do this before starting Raw mode, because it may change things that the
638 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
639 * are the same terminal: "cat | vim -".
640 * Using autocommands here may cause trouble...
641 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000642 if (params.edit_type == EDIT_STDIN && !recoverymode)
643 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000644
645#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100646 // When switching screens and something caused a message from a vimrc
647 // script, need to output an extra newline on exit.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000648 if ((did_emsg || msg_didout) && *T_TI != NUL)
649 newline_on_exit = TRUE;
650#endif
651
652 /*
K.Takataeeec2542021-06-02 13:28:16 +0200653 * When done something that is not allowed or given an error message call
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000654 * wait_return. This must be done before starttermcap(), because it may
655 * switch to another screen. It must be done after settmode(TMODE_RAW),
656 * because we want to react on a single key stroke.
657 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000658 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000659 */
660 settmode(TMODE_RAW);
661 TIME_MSG("setting raw mode");
662
663 if (need_wait_return || msg_didany)
664 {
665 wait_return(TRUE);
666 TIME_MSG("waiting for return");
667 }
668
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100669 starttermcap(); // start termcap if not done by wait_return()
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000670 TIME_MSG("start termcap");
671
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200672 setmouse(); // may start using the mouse
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000673 if (scroll_region)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100674 scroll_region_reset(); // In case Rows changed
675 scroll_start(); // may scroll the screen to the right position
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000676
Bram Moolenaar651fca82021-11-29 20:39:38 +0000677#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar40385db2018-08-07 22:31:44 +0200678 term_push_title(SAVE_RESTORE_BOTH);
679#endif
680
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000681 /*
682 * Don't clear the screen when starting in Ex mode, unless using the GUI.
683 */
684 if (exmode_active
685#ifdef FEAT_GUI
686 && !gui.in_use
687#endif
688 )
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100689 set_must_redraw(UPD_CLEAR);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000690 else
691 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100692 screenclear(); // clear screen
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000693 TIME_MSG("clearing screen");
694 }
695
696#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000697 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000698 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100699 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200700 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000701 TIME_MSG("getting crypt key");
702 }
703#endif
704
705 no_wait_return = TRUE;
706
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000707 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000708 * Create the requested number of windows and edit buffers in them.
709 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000710 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000711 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000712 TIME_MSG("opening buffers");
713
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000714#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100715 // clear v:swapcommand
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000716 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
717#endif
718
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100719 // Ex starts at last line of the file
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000720 if (exmode_active)
721 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
722
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000723 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
724 TIME_MSG("BufEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000725 setpcmark();
726
727#ifdef FEAT_QUICKFIX
728 /*
729 * When started with "-q errorfile" jump to first error now.
730 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000731 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000732 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000733 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000734 TIME_MSG("jump to first error");
735 }
736#endif
737
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000738 /*
739 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000740 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000741 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200742 edit_buffers(&params, start_dir);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200743 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000744
745#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000746 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000747 {
748 win_T *wp;
749
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100750 // set options in each window for "vimdiff".
Bram Moolenaar29323592016-07-24 22:04:11 +0200751 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000752 diff_win_options(wp, TRUE);
753 }
754#endif
755
756 /*
757 * Shorten any of the filenames, but only when absolute.
758 */
759 shorten_fnames(FALSE);
760
761 /*
762 * Need to jump to the tag before executing the '-c command'.
763 * Makes "vim -c '/return' -t main" work.
764 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000765 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000766 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000767 swap_exists_did_quit = FALSE;
Bram Moolenaar146522e2005-12-16 21:55:46 +0000768
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000769 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000770 do_cmdline_cmd(IObuff);
771 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000772
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100773 // If the user doesn't want to edit the file then we quit here.
Bram Moolenaar146522e2005-12-16 21:55:46 +0000774 if (swap_exists_did_quit)
775 getout(1);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000776 }
777
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100778 // Execute any "+", "-c" and "-S" arguments.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000779 if (params.n_commands > 0)
780 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000781
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100782 // Must come before the may_req_ calls.
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200783 starting = 0;
784
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100785#if defined(FEAT_TERMRESPONSE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100786 // Must be done before redrawing, puts a few characters on the screen.
Bram Moolenaara45551a2020-06-09 15:57:37 +0200787 check_terminal_behavior();
Bram Moolenaar976787d2017-06-04 15:45:50 +0200788#endif
789
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000790 RedrawingDisabled = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100791 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000792 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000793
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100794 // 'autochdir' has been postponed
Bram Moolenaar6f470022018-04-10 18:47:20 +0200795 DO_AUTOCHDIR;
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200796
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000797#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100798 // Requesting the termresponse is postponed until here, so that a "-c q"
799 // argument doesn't make it appear in the shell Vim was started from.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000800 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200801
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200802 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000803#endif
804
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100805 // start in insert mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000806 if (p_im)
807 need_start_insertmode = TRUE;
808
Bram Moolenaar14735512016-03-26 21:00:08 +0100809#ifdef FEAT_EVAL
810 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
811#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000812 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
813 TIME_MSG("VimEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000814
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200815#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100816 // Adjust default register name for "unnamed" in 'clipboard'. Can only be
817 // done after the clipboard is available and all initial commands that may
818 // modify the 'clipboard' setting have run; i.e. just before entering the
819 // main loop.
Bram Moolenaar439c0362020-06-06 15:58:03 +0200820 reset_reg_var();
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200821#endif
822
Bram Moolenaar8a3bb562018-03-04 20:14:14 +0100823#if defined(FEAT_DIFF)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100824 // When a startup script or session file setup for diff'ing and
825 // scrollbind, sync the scrollbind now.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000826 if (curwin->w_p_diff && curwin->w_p_scb)
827 {
828 update_topline();
829 check_scrollbind((linenr_T)0, 0L);
830 TIME_MSG("diff scrollbinding");
831 }
832#endif
833
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200834#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
835# ifdef VIMDLL
836 if (!gui.in_use)
837# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100838 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000839#endif
840
Bram Moolenaar4033c552017-09-16 20:54:51 +0200841#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100842 // When tab pages were created, may need to update the tab pages line and
843 // scrollbars. This is skipped while creating them.
h-east6073f132021-12-24 11:57:06 +0000844 if (gui.in_use && first_tabpage->tp_next != NULL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000845 {
846 out_flush();
847 gui_init_which_components(NULL);
848 gui_update_scrollbars(TRUE);
849 }
850 need_mouse_correct = TRUE;
851#endif
852
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100853 // If ":startinsert" command used, stuff a dummy command to be able to
854 // call normal_cmd(), which will then start Insert mode.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000855 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000856 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000857
858#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200859 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200860 {
861# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200862# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar4f974752019-02-17 17:44:42 +0100863 && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200864 if (gui.in_use)
865 {
866 mch_errmsg(_("netbeans is not supported with this GUI\n"));
867 mch_exit(2);
868 }
869# endif
870# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100871 // Tell the client that it can start sending commands.
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200872 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200873 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000874#endif
875
Bram Moolenaare7ddf4e2020-02-03 21:29:30 +0100876 // Redraw at least once, also when 'lazyredraw' is set, to make sure the
877 // window title gets updated.
878 do_redraw = TRUE;
879
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000880 TIME_MSG("before starting main loop");
881
882 /*
883 * Call the main command loop. This never returns.
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200884 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000885 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000886
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100887#endif // NO_VIM_MAIN
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200888
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000889 return 0;
890}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000891
892/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200893 * Initialisation shared by main() and some tests.
894 */
895 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200896common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200897{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100898 estack_init();
Bram Moolenaar438d1762018-09-30 17:11:48 +0200899 cmdline_init();
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200900
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100901 (void)mb_init(); // init mb_bytelen_tab[] to ones
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200902#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100903 eval_init(); // init global variables
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200904#endif
905
906#ifdef __QNXNTO__
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100907 qnx_init(); // PhAttach() for clipboard, (and gui)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200908#endif
909
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200910 /*
911 * Allocate space for the generic buffers (needed for set_init_1() and
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100912 * emsg()).
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200913 */
914 if ((IObuff = alloc(IOSIZE)) == NULL
915 || (NameBuff = alloc(MAXPATHL)) == NULL)
916 mch_exit(0);
917 TIME_MSG("Allocated generic buffers");
918
919#ifdef NBDEBUG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100920 // Wait a moment for debugging NetBeans. Must be after allocating
921 // NameBuff.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200922 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
923 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
924 TIME_MSG("NetBeans debug wait");
925#endif
926
927#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
928 /*
929 * Setup to use the current locale (for ctype() and many other things).
930 * NOTE: Translated messages with encodings other than latin1 will not
931 * work until set_init_1() has been called!
932 */
933 init_locale();
934 TIME_MSG("locale set");
935#endif
936
937#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100938 gui.dofork = TRUE; // default is to use fork()
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200939#endif
940
941 /*
942 * Do a first scan of the arguments in "argv[]":
943 * -display or --display
944 * --server...
945 * --socketid
946 * --windowid
947 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200948 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200949
Bram Moolenaard0573012017-10-28 21:11:06 +0200950#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100951 // Prepare for possibly starting GUI sometime
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200952 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200953 TIME_MSG("GUI prepared");
954#endif
955
956#ifdef FEAT_CLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100957 clip_init(FALSE); // Initialise clipboard stuff
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200958 TIME_MSG("clipboard setup");
959#endif
960
961 /*
962 * Check if we have an interactive window.
963 * On the Amiga: If there is no window, we open one with a newcli command
964 * (needed for :! to * work). mch_check_win() will also handle the -d or
965 * -dev argument.
966 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100967 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200968 TIME_MSG("window checked");
969
970 /*
971 * Allocate the first window and buffer.
972 * Can't do anything without it, exit when it fails.
973 */
974 if (win_alloc_first() == FAIL)
975 mch_exit(0);
976
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100977 init_yank(); // init yank buffers
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200978
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100979 alist_init(&global_alist); // Init the argument list to empty.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200980 global_alist.id = 0;
981
982 /*
983 * Set the default values for the options.
984 * NOTE: Non-latin1 translated messages are working only after this,
985 * because this is where "has_mbyte" will be set, which is used by
986 * msg_outtrans_len_attr().
987 * First find out the home directory, needed to expand "~" in options.
988 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100989 init_homedir(); // find real value of $HOME
Bram Moolenaar07268702018-03-01 21:57:32 +0100990 set_init_1(paramp->clean);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200991 TIME_MSG("inits 1");
992
993#ifdef FEAT_EVAL
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100994 // set v:lang and v:ctype
995 set_lang_var();
996
997 // set v:argv
998 set_argv_var(paramp->argv, paramp->argc);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200999#endif
Bram Moolenaar6436cd82018-12-27 00:28:33 +01001000
1001#ifdef FEAT_SIGNS
1002 init_signs();
1003#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001004}
1005
1006/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001007 * Return TRUE when the --not-a-term argument was found.
1008 */
1009 int
1010is_not_a_term()
1011{
1012 return params.not_a_term;
1013}
1014
Bram Moolenaar7007e312021-03-27 12:11:33 +01001015/*
1016 * Return TRUE when the --not-a-term argument was found or the GUI is in use.
1017 */
1018 static int
1019is_not_a_term_or_gui()
1020{
1021 return params.not_a_term
1022#ifdef FEAT_GUI
1023 || gui.in_use
1024#endif
1025 ;
1026}
1027
Bram Moolenaar2d12c252022-06-13 21:42:45 +01001028#if defined(FEAT_GUI) || defined(PROTO)
1029/*
1030 * If a --gui-dialog-file argument was given return the file name.
1031 * Otherwise return NULL.
1032 */
1033 char_u *
1034get_gui_dialog_file(void)
1035{
1036 return params.gui_dialog_file;
1037}
1038#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001039
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001040// When TRUE in a safe state when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001041static int was_safe = FALSE;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001042static oparg_T *current_oap = NULL;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001043
1044/*
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001045 * Return TRUE if an operator was started but not finished yet.
1046 * Includes typing a count or a register name.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001047 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001048 int
1049op_pending(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001050{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001051 return !(current_oap != NULL
1052 && !finish_op
1053 && current_oap->prev_opcount == 0
1054 && current_oap->prev_count0 == 0
1055 && current_oap->op_type == OP_NOP
1056 && current_oap->regname == NUL);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001057}
1058
1059/*
Bram Moolenaard103ee72019-09-18 21:15:31 +02001060 * Return whether currently it is safe, assuming it was safe before (high level
1061 * state didn't change).
1062 */
1063 static int
1064is_safe_now(void)
1065{
1066 return stuff_empty()
1067 && typebuf.tb_len == 0
1068 && scriptin[curscript] == NULL
Bram Moolenaar4fa13462022-02-05 12:39:24 +00001069#ifdef FEAT_EVAL
1070 && !debug_mode
1071#endif
1072 && !global_busy;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001073}
1074
1075/*
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001076 * Trigger SafeState if currently in s safe state, that is "safe" is TRUE and
1077 * there is no typeahead.
1078 */
1079 void
1080may_trigger_safestate(int safe)
1081{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001082 int is_safe = safe && is_safe_now();
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001083
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001084#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001085 if (was_safe != is_safe)
1086 // Only log when the state changes, otherwise it happens at nearly
1087 // every key stroke.
Bram Moolenaard103ee72019-09-18 21:15:31 +02001088 ch_log(NULL, is_safe ? "SafeState: Start triggering"
1089 : "SafeState: Stop triggering");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001090#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001091 if (is_safe)
1092 apply_autocmds(EVENT_SAFESTATE, NULL, NULL, FALSE, curbuf);
1093 was_safe = is_safe;
1094}
1095
1096/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001097 * Something changed which causes the state possibly to be unsafe, e.g. a
1098 * character was typed. It will remain unsafe until the next call to
1099 * may_trigger_safestate().
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001100 */
1101 void
Bram Moolenaard103ee72019-09-18 21:15:31 +02001102state_no_longer_safe(char *reason UNUSED)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001103{
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001104#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001105 if (was_safe)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001106 ch_log(NULL, "SafeState: reset: %s", reason);
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001107#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001108 was_safe = FALSE;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001109}
1110
Dominique Pelle748b3082022-01-08 12:41:16 +00001111#if defined(FEAT_EVAL) || defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001112 int
1113get_was_safe_state(void)
1114{
1115 return was_safe;
1116}
Dominique Pelle748b3082022-01-08 12:41:16 +00001117#endif
Bram Moolenaard103ee72019-09-18 21:15:31 +02001118
Dominique Pelle748b3082022-01-08 12:41:16 +00001119#if defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001120/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001121 * Invoked when leaving code that invokes callbacks. Then trigger
1122 * SafeStateAgain, if it was safe when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001123 */
1124 void
Bram Moolenaar37d18072019-09-17 20:28:38 +02001125may_trigger_safestateagain(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001126{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001127 if (!was_safe)
1128 {
1129 // If the safe state was reset in state_no_longer_safe(), e.g. because
1130 // of calling feedkeys(), we check if it's now safe again (all keys
1131 // were consumed).
1132 was_safe = is_safe_now();
1133#ifdef FEAT_JOB_CHANNEL
1134 if (was_safe)
1135 ch_log(NULL, "SafeState: undo reset");
1136#endif
1137 }
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001138 if (was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02001139 {
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001140#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar101e9922019-09-25 21:43:11 +02001141 // Only do this message when another message was given, otherwise we
1142 // get lots of them.
1143 if ((did_repeated_msg & REPEATED_MSG_SAFESTATE) == 0)
1144 {
1145 int did = did_repeated_msg;
1146
1147 ch_log(NULL,
1148 "SafeState: back to waiting, triggering SafeStateAgain");
1149 did_repeated_msg = did | REPEATED_MSG_SAFESTATE;
1150 }
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001151#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001152 apply_autocmds(EVENT_SAFESTATEAGAIN, NULL, NULL, FALSE, curbuf);
Bram Moolenaar37d18072019-09-17 20:28:38 +02001153 }
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001154#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001155 else
Bram Moolenaard103ee72019-09-18 21:15:31 +02001156 ch_log(NULL,
1157 "SafeState: back to waiting, not triggering SafeStateAgain");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001158#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001159}
Dominique Pelle748b3082022-01-08 12:41:16 +00001160#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001161
1162
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001163/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001164 * Main loop: Execute Normal mode commands until exiting Vim.
1165 * Also used to handle commands in the command-line window, until the window
1166 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167 * Also used to handle ":visual" command after ":global": execute Normal mode
1168 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001169 */
1170 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001171main_loop(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001172 int cmdwin, // TRUE when working in the command-line window
1173 int noexmode) // TRUE when return on entering Ex mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001174{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001175 oparg_T oa; // operator arguments
1176 oparg_T *prev_oap; // operator arguments
1177 volatile int previous_got_int = FALSE; // "got_int" was TRUE
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001178#ifdef FEAT_CONCEAL
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001179 // these are static to avoid a compiler warning
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001180 static linenr_T conceal_old_cursor_line = 0;
1181 static linenr_T conceal_new_cursor_line = 0;
1182 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001183#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001184
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001185 prev_oap = current_oap;
1186 current_oap = &oa;
1187
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001188#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001189 // Setup to catch a terminating error from the X server. Just ignore
1190 // it, restore the state and continue. This might not always work
1191 // properly, but at least we don't exit unexpectedly when the X server
1192 // exits while Vim is running in a console.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001193 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001194 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001195 State = MODE_NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001196 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001197 got_int = TRUE;
1198 need_wait_return = FALSE;
1199 global_busy = FALSE;
1200 exmode_active = 0;
1201 skip_redraw = FALSE;
1202 RedrawingDisabled = 0;
1203 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001204 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001205# ifdef FEAT_EVAL
1206 emsg_skip = 0;
1207# endif
1208 emsg_off = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001209 setmouse();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001210 settmode(TMODE_RAW);
1211 starttermcap();
1212 scroll_start();
1213 redraw_later_clear();
1214 }
1215#endif
1216
1217 clear_oparg(&oa);
1218 while (!cmdwin
1219#ifdef FEAT_CMDWIN
1220 || cmdwin_result == 0
1221#endif
1222 )
1223 {
1224 if (stuff_empty())
1225 {
1226 did_check_timestamps = FALSE;
1227 if (need_check_timestamps)
1228 check_timestamps(FALSE);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001229 if (need_wait_return) // if wait_return still needed ...
1230 wait_return(FALSE); // ... call it now
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001231 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001232 {
1233 need_start_insertmode = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001234 stuffReadbuff((char_u *)"i"); // start insert mode next
1235 // skip the fileinfo message now, because it would be shown
1236 // after insert mode finishes!
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001237 need_fileinfo = FALSE;
1238 }
1239 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001240
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001241 // Reset "got_int" now that we got back to the main loop. Except when
1242 // inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1243 // the ":g" command.
1244 // For ":g/pat/vi" we reset "got_int" when used once. When used
1245 // a second time we go back to Ex mode and abort the ":g" command.
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001246 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001247 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001248 if (noexmode && global_busy && !exmode_active && previous_got_int)
1249 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001250 // Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1251 // used and keep "got_int" set, so that it aborts ":g".
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001252 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +01001253 State = MODE_NORMAL;
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001254 }
1255 else if (!global_busy || !exmode_active)
1256 {
1257 if (!quit_more)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001258 (void)vgetc(); // flush all buffers
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001259 got_int = FALSE;
1260 }
1261 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001262 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001263 else
1264 previous_got_int = FALSE;
1265
Bram Moolenaar069613c2022-01-15 15:23:44 +00001266#ifdef FEAT_EVAL
1267 // At the toplevel there is no exception handling. Discard any that
1268 // may be hanging around (e.g. from "interrupt" at the debug prompt).
1269 if (did_throw && !ex_normal_busy)
1270 discard_current_exception();
1271#endif
1272
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001273 if (!exmode_active)
1274 msg_scroll = FALSE;
1275 quit_more = FALSE;
1276
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001277 // it's not safe unless may_trigger_safestate_main() is called
1278 was_safe = FALSE;
1279
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001280 /*
1281 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1282 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1283 * update cursor and redraw.
1284 */
1285 if (skip_redraw || exmode_active)
1286 skip_redraw = FALSE;
1287 else if (do_redraw || stuff_empty())
1288 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001289#ifdef FEAT_GUI
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001290 // If ui_breakcheck() was used a resize may have been postponed.
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001291 gui_may_resize_shell();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001292#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001293#ifdef HAVE_DROP_FILE
1294 // If files were dropped while text was locked or the curbuf was
1295 // locked, this would be a good time to handle the drop.
1296 handle_any_postponed_drop();
1297#endif
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001298#ifdef FEAT_CONCEAL
1299 if (curwin->w_p_cole == 0)
1300 conceal_update_lines = FALSE;
1301#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001302
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001303 // Trigger CursorMoved if the cursor moved.
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001304 if (!finish_op && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001305 has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001306#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001307 || popup_visible
1308#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001309#ifdef FEAT_CONCEAL
1310 || curwin->w_p_cole > 0
1311#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001312 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001313 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001314 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001315 if (has_cursormoved())
1316 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1317 FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001318#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001319 if (popup_visible)
1320 popup_check_cursor_pos();
1321#endif
1322#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001323 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001324 {
1325 conceal_old_cursor_line = last_cursormoved.lnum;
1326 conceal_new_cursor_line = curwin->w_cursor.lnum;
1327 conceal_update_lines = TRUE;
1328 }
Bram Moolenaar3397f742019-06-02 18:40:06 +02001329#endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001330 last_cursormoved = curwin->w_cursor;
1331 }
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001332
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001333#if defined(FEAT_CONCEAL)
1334 if (conceal_update_lines
1335 && (conceal_old_cursor_line != conceal_new_cursor_line
1336 || conceal_cursor_line(curwin)
1337 || need_cursor_line_redraw))
1338 {
1339 if (conceal_old_cursor_line != conceal_new_cursor_line
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001340 && conceal_old_cursor_line != 0
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001341 && conceal_old_cursor_line
1342 <= curbuf->b_ml.ml_line_count)
1343 redrawWinline(curwin, conceal_old_cursor_line);
1344 redrawWinline(curwin, conceal_new_cursor_line);
1345 curwin->w_valid &= ~VALID_CROW;
1346 need_cursor_line_redraw = FALSE;
1347 }
1348#endif
1349
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001350 // Trigger TextChanged if b:changedtick differs.
Bram Moolenaar186628f2013-03-19 13:33:23 +01001351 if (!finish_op && has_textchanged()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001352 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001353 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001354 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL, FALSE, curbuf);
1355 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001356 }
Bram Moolenaar186628f2013-03-19 13:33:23 +01001357
LemonBoy09371822022-04-08 15:18:45 +01001358 // Ensure curwin->w_topline and curwin->w_leftcol are up to date
1359 // before triggering a WinScrolled autocommand.
1360 update_topline();
1361 validate_cursor();
1362
1363 if (!finish_op)
zeertzjqd58862d2022-04-12 11:32:48 +01001364 may_trigger_winscrolled();
LemonBoy09371822022-04-08 15:18:45 +01001365
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001366 // If nothing is pending and we are going to wait for the user to
1367 // type a character, trigger SafeState.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001368 may_trigger_safestate(!op_pending() && restart_edit == 0);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001369
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01001370#if defined(FEAT_DIFF)
Bram Moolenaare3521d92018-09-16 14:10:31 +02001371 // Updating diffs from changed() does not always work properly,
1372 // esp. updating folds. Do an update just before redrawing if
1373 // needed.
1374 if (curtab->tp_diff_update || curtab->tp_diff_invalid)
1375 {
1376 ex_diffupdate(NULL);
1377 curtab->tp_diff_update = FALSE;
1378 }
1379
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001380 // Scroll-binding for diff mode may have been postponed until
1381 // here. Avoids doing it for every change.
Bram Moolenaar33aec762006-01-22 23:30:12 +00001382 if (diff_need_scrollbind)
1383 {
1384 check_scrollbind((linenr_T)0, 0L);
1385 diff_need_scrollbind = FALSE;
1386 }
1387#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001388#if defined(FEAT_FOLDING)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001389 // Include a closed fold completely in the Visual area.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001390 foldAdjustVisual();
1391#endif
1392#ifdef FEAT_FOLDING
1393 /*
1394 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1395 * contain the cursor.
1396 * When 'foldopen' is "all", open the fold(s) under the cursor.
1397 * This may mark the window for redrawing.
1398 */
1399 if (hasAnyFolding(curwin) && !char_avail())
1400 {
1401 foldCheckClose();
1402 if (fdo_flags & FDO_ALL)
1403 foldOpenCursor();
1404 }
1405#endif
1406
1407 /*
1408 * Before redrawing, make sure w_topline is correct, and w_leftcol
1409 * if lines don't wrap, and w_skipcol if lines wrap.
1410 */
1411 update_topline();
1412 validate_cursor();
1413
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001414 if (VIsual_active)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001415 update_curbuf(UPD_INVERTED); // update inverted part
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001416 else if (must_redraw)
Bram Moolenaara338adc2018-01-31 20:51:47 +01001417 {
Bram Moolenaar017ba072019-09-14 21:01:23 +02001418 mch_disable_flush(); // Stop issuing gui_mch_flush().
Bram Moolenaar11a58af2019-10-24 22:32:31 +02001419 update_screen(0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01001420 mch_enable_flush();
1421 }
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01001422 else if (redraw_cmdline || clear_cmdline || redraw_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001423 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001424 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001425 if (need_maketitle)
1426 maketitle();
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001427#ifdef FEAT_VIMINFO
1428 curbuf->b_last_used = vim_time();
1429#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001430 // display message after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001431 if (keep_msg != NULL)
1432 {
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001433 char_u *p = vim_strsave(keep_msg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001434
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001435 if (p != NULL)
1436 {
1437 // msg_start() will set keep_msg to NULL, make a copy
1438 // first. Don't reset keep_msg, msg_attr_keep() uses it to
1439 // check for duplicates. Never put this message in
1440 // history.
1441 msg_hist_off = TRUE;
1442 msg_attr((char *)p, keep_msg_attr);
1443 msg_hist_off = FALSE;
1444 vim_free(p);
1445 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001446 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001447 if (need_fileinfo) // show file info after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001448 {
1449 fileinfo(FALSE, TRUE, FALSE);
1450 need_fileinfo = FALSE;
1451 }
1452
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001453 emsg_on_display = FALSE; // can delete error message now
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001454 did_emsg = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001455 msg_didany = FALSE; // reset lines_left in msg_start()
1456 may_clear_sb_text(); // clear scroll-back text on next msg
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001457 showruler(FALSE);
1458
1459 setcursor();
1460 cursor_on();
1461
1462 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001463
1464#ifdef STARTUPTIME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001465 // Now that we have drawn the first screen all the startup stuff
1466 // has been done, close any file for startup messages.
Bram Moolenaar3f269672009-11-03 11:11:11 +00001467 if (time_fd != NULL)
1468 {
1469 TIME_MSG("first screen update");
1470 TIME_MSG("--- VIM STARTED ---");
1471 fclose(time_fd);
1472 time_fd = NULL;
1473 }
1474#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001475 }
1476#ifdef FEAT_GUI
1477 if (need_mouse_correct)
1478 gui_mouse_correct();
1479#endif
1480
1481 /*
1482 * Update w_curswant if w_set_curswant has been set.
1483 * Postponed until here to avoid computing w_virtcol too often.
1484 */
1485 update_curswant();
1486
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001487#ifdef FEAT_EVAL
1488 /*
1489 * May perform garbage collection when waiting for a character, but
1490 * only at the very toplevel. Otherwise we may be using a List or
1491 * Dict internally somewhere.
1492 * "may_garbage_collect" is reset in vgetc() which is invoked through
1493 * do_exmode() and normal_cmd().
1494 */
1495 may_garbage_collect = (!cmdwin && !noexmode);
1496#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001497 /*
1498 * If we're invoked as ex, do a round of ex commands.
1499 * Otherwise, get and execute a normal mode command.
1500 */
1501 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001502 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001503 if (noexmode) // End of ":global/path/visual" commands
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001504 goto theend;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001505 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001506 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001507 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001508 {
1509#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001510 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001511 && oa.op_type == OP_NOP && oa.regname == NUL
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001512 && !VIsual_active
1513 && !skip_term_loop)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001514 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001515 // If terminal_loop() returns OK we got a key that is handled
1516 // in Normal model. With FAIL we first need to position the
1517 // cursor and the screen needs to be redrawn.
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001518 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001519 normal_cmd(&oa, TRUE);
1520 }
1521 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001522#endif
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001523 {
1524#ifdef FEAT_TERMINAL
1525 skip_term_loop = FALSE;
1526#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001527 normal_cmd(&oa, TRUE);
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001528 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02001529 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001530 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001531
1532theend:
1533 current_oap = prev_oap;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001534}
1535
1536
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001537#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001538/*
1539 * Exit, but leave behind swap files for modified buffers.
1540 */
1541 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001542getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001543{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001544# if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001545 // Ignore SIGHUP, because a dropped connection causes a read error, which
1546 // makes Vim exit and then handling SIGHUP causes various reentrance
1547 // problems.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001548 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001549# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001550
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001551 ml_close_notmod(); // close all not-modified buffers
1552 ml_sync_all(FALSE, FALSE); // preserve all swap files
1553 ml_close_all(FALSE); // close all memfiles, without deleting
1554 getout(exitval); // exit Vim properly
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001555}
1556#endif
1557
1558
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001559/*
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001560 * Exit properly. This is the only way to exit Vim after startup has
1561 * succeeded. We are certain to exit here, no way to abort it.
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001562 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001563 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001564getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001565{
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001566 exiting = TRUE;
Bram Moolenaar0bd052b2018-03-22 20:33:56 +01001567#if defined(FEAT_JOB_CHANNEL)
1568 ch_log(NULL, "Exiting...");
1569#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001570
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001571 // When running in Ex mode an error causes us to exit with a non-zero exit
1572 // code. POSIX requires this, although it's not 100% clear from the
1573 // standard.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001574 if (exmode_active)
1575 exitval += ex_exitval;
1576
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001577#ifdef FEAT_EVAL
1578 set_vim_var_type(VV_EXITING, VAR_NUMBER);
1579 set_vim_var_nr(VV_EXITING, exitval);
1580#endif
1581
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001582 // Position the cursor on the last screen line, below all the text
Bram Moolenaar7007e312021-03-27 12:11:33 +01001583 if (!is_not_a_term_or_gui())
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001584 windgoto((int)Rows - 1, 0);
1585
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001586#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001587 // Optionally print hashtable efficiency.
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001588 hash_debug_results();
1589#endif
1590
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001591#ifdef FEAT_GUI
1592 msg_didany = FALSE;
1593#endif
1594
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001595 if (v_dying <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001596 {
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001597 tabpage_T *tp;
1598 tabpage_T *next_tp;
1599 buf_T *buf;
1600 win_T *wp;
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001601 int unblock = 0;
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001602
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001603 // Trigger BufWinLeave for all windows, but only once per buffer.
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001604 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001605 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001606 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001607 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001608 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001609 if (wp->w_buffer == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001610 // Autocmd must have close the buffer already, skip.
Bram Moolenaar802418d2013-01-17 14:00:11 +01001611 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001612 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001613 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001614 {
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001615 bufref_T bufref;
1616
1617 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001618 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1619 buf->b_fname, FALSE, buf);
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001620 if (bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001621 CHANGEDTICK(buf) = -1; // note we did it already
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001622
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001623 // start all over, autocommands may mess up the lists
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001624 next_tp = first_tabpage;
1625 break;
1626 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001627 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001628 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001629
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001630 // Trigger BufUnload for buffers that are loaded
Bram Moolenaar29323592016-07-24 22:04:11 +02001631 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001632 if (buf->b_ml.ml_mfp != NULL)
1633 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001634 bufref_T bufref;
1635
1636 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001637 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001638 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001639 if (!bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001640 // autocmd deleted the buffer
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001641 break;
1642 }
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001643
1644 // deathtrap() blocks autocommands, but we do want to trigger
1645 // VimLeavePre.
1646 if (is_autocmd_blocked())
1647 {
1648 unblock_autocmds();
1649 ++unblock;
1650 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001651 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001652 if (unblock)
1653 block_autocmds();
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001654 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001655
1656#ifdef FEAT_VIMINFO
1657 if (*p_viminfo != NUL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001658 // Write out the registers, history, marks etc, to the viminfo file
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001659 write_viminfo(NULL, FALSE);
1660#endif
1661
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001662 if (v_dying <= 1)
Bram Moolenaarc7226682019-08-17 16:33:23 +02001663 {
1664 int unblock = 0;
1665
1666 // deathtrap() blocks autocommands, but we do want to trigger VimLeave.
1667 if (is_autocmd_blocked())
1668 {
1669 unblock_autocmds();
1670 ++unblock;
1671 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001672 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc7226682019-08-17 16:33:23 +02001673 if (unblock)
1674 block_autocmds();
1675 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001676
Bram Moolenaar05159a02005-02-26 23:04:13 +00001677#ifdef FEAT_PROFILE
1678 profile_dump();
1679#endif
1680
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001681 if (did_emsg
1682#ifdef FEAT_GUI
1683 || (gui.in_use && msg_didany && p_verbose > 0)
1684#endif
1685 )
1686 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001687 // give the user a chance to read the (error) message
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001688 no_wait_return = FALSE;
Volodymyr Kot7f0c4b42021-11-21 12:27:13 +00001689 wait_return(FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001690 }
1691
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001692 // Position the cursor again, the autocommands may have moved it
Bram Moolenaar7007e312021-03-27 12:11:33 +01001693 if (!is_not_a_term_or_gui())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001694 windgoto((int)Rows - 1, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001695
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001696#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001697 job_stop_on_exit();
1698#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001699#ifdef FEAT_LUA
1700 lua_end();
1701#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001702#ifdef FEAT_MZSCHEME
1703 mzscheme_end();
1704#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001705#ifdef FEAT_TCL
1706 tcl_end();
1707#endif
1708#ifdef FEAT_RUBY
1709 ruby_end();
1710#endif
1711#ifdef FEAT_PYTHON
1712 python_end();
1713#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001714#ifdef FEAT_PYTHON3
1715 python3_end();
1716#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001717#ifdef FEAT_PERL
1718 perl_end();
1719#endif
1720#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1721 iconv_end();
1722#endif
1723#ifdef FEAT_NETBEANS_INTG
1724 netbeans_end();
1725#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001726#ifdef FEAT_CSCOPE
1727 cs_end();
1728#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001729#ifdef FEAT_EVAL
1730 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001731 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001732#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001733#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +02001734 free_cmd_argsW();
1735#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001736
1737 mch_exit(exitval);
1738}
1739
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001740/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001741 * Get the name of the display, before gui_prepare() removes it from
1742 * argv[]. Used for the xterm-clipboard display.
1743 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001744 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001745 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001746 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001747early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001748{
Bram Moolenaar03005972008-11-20 13:12:36 +00001749#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1750 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001751 int argc = parmp->argc;
1752 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001753 int i;
1754
1755 for (i = 1; i < argc; i++)
1756 {
1757 if (STRCMP(argv[i], "--") == 0)
1758 break;
1759# ifdef FEAT_XCLIPBOARD
1760 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001761# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001762 || STRICMP(argv[i], "--display") == 0
1763# endif
1764 )
1765 {
1766 if (i == argc - 1)
1767 mainerr_arg_missing((char_u *)argv[i]);
1768 xterm_display = argv[++i];
1769 }
1770# endif
1771# ifdef FEAT_CLIENTSERVER
1772 else if (STRICMP(argv[i], "--servername") == 0)
1773 {
1774 if (i == argc - 1)
1775 mainerr_arg_missing((char_u *)argv[i]);
1776 parmp->serverName_arg = (char_u *)argv[++i];
1777 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001778 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001779 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001780 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001781 {
1782 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001783# ifdef FEAT_GUI
1784 if (strstr(argv[i], "-wait") != 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001785 // don't fork() when starting the GUI to edit files ourself
Bram Moolenaareb94e552006-03-11 21:35:11 +00001786 gui.dofork = FALSE;
1787# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001788 }
1789# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001790
Bram Moolenaar4f974752019-02-17 17:44:42 +01001791# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
1792# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001793 else if (STRICMP(argv[i], "--windowid") == 0)
1794# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001795 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001796# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001797 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001798 long_u id;
1799 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001800
1801 if (i == argc - 1)
1802 mainerr_arg_missing((char_u *)argv[i]);
1803 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001804 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001805 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001806 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001807 if (count != 1)
1808 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1809 else
Bram Moolenaar4f974752019-02-17 17:44:42 +01001810# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001811 win_socket_id = id;
1812# else
1813 gtk_socket_id = id;
1814# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001815 i++;
1816 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001817# endif
1818# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001819 else if (STRICMP(argv[i], "--echo-wid") == 0)
1820 echo_wid_arg = TRUE;
1821# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001822# ifndef FEAT_NETBEANS_INTG
1823 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001824 {
1825 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1826 mch_exit(2);
1827 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001828# endif
1829
Bram Moolenaar58d98232005-07-23 22:25:46 +00001830 }
1831#endif
1832}
1833
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001834#ifndef NO_VIM_MAIN
1835/*
1836 * Get a (optional) count for a Vim argument.
1837 */
1838 static int
1839get_number_arg(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001840 char_u *p, // pointer to argument
1841 int *idx, // index in argument, is incremented
1842 int def) // default value
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001843{
1844 if (vim_isdigit(p[*idx]))
1845 {
1846 def = atoi((char *)&(p[*idx]));
1847 while (vim_isdigit(p[*idx]))
1848 *idx = *idx + 1;
1849 }
1850 return def;
1851}
1852
1853/*
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001854 * Check for: [r][e][g][vi|vim|view][diff][ex[im]] (sort of)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001855 * If the executable name starts with "r" we disable shell commands.
1856 * If the next character is "e" we run in Easy mode.
1857 * If the next character is "g" we run the GUI version.
1858 * If the next characters are "view" we start in readonly mode.
1859 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1860 * If the next characters are "ex" we start in Ex mode. If it's followed
1861 * by "im" use improved Ex mode.
1862 */
1863 static void
1864parse_command_name(mparm_T *parmp)
1865{
1866 char_u *initstr;
1867
1868 initstr = gettail((char_u *)parmp->argv[0]);
1869
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001870#ifdef FEAT_EVAL
1871 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001872 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001873#endif
1874
1875 if (TOLOWER_ASC(initstr[0]) == 'r')
1876 {
1877 restricted = TRUE;
1878 ++initstr;
1879 }
1880
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001881 // Use evim mode for "evim" and "egvim", not for "editor".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001882 if (TOLOWER_ASC(initstr[0]) == 'e'
1883 && (TOLOWER_ASC(initstr[1]) == 'v'
1884 || TOLOWER_ASC(initstr[1]) == 'g'))
1885 {
1886#ifdef FEAT_GUI
1887 gui.starting = TRUE;
1888#endif
1889 parmp->evim_mode = TRUE;
1890 ++initstr;
1891 }
1892
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001893 // "gvim" starts the GUI. Also accept "Gvim" for MS-Windows.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001894 if (TOLOWER_ASC(initstr[0]) == 'g')
1895 {
1896 main_start_gui();
1897#ifdef FEAT_GUI
1898 ++initstr;
1899#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001900#ifdef GUI_MAY_SPAWN
1901 gui.dospawn = FALSE; // No need to spawn a new process.
1902#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001903 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001904#ifdef GUI_MAY_SPAWN
1905 else
1906 gui.dospawn = TRUE; // Not "gvim". Need to spawn gvim.exe.
1907#endif
1908
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001909
1910 if (STRNICMP(initstr, "view", 4) == 0)
1911 {
1912 readonlymode = TRUE;
1913 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001914 p_uc = 10000; // don't update very often
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001915 initstr += 4;
1916 }
1917 else if (STRNICMP(initstr, "vim", 3) == 0)
1918 initstr += 3;
1919
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001920 // Catch "[r][g]vimdiff" and "[r][g]viewdiff".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001921 if (STRICMP(initstr, "diff") == 0)
1922 {
1923#ifdef FEAT_DIFF
1924 parmp->diff_mode = TRUE;
1925#else
1926 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1927 mch_errmsg("\n");
1928 mch_exit(2);
1929#endif
1930 }
1931
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001932 // Checking for "ex" here may catch some weird names, such as "vimex" or
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001933 // "viewex", we assume the user knows that.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001934 if (STRNICMP(initstr, "ex", 2) == 0)
1935 {
1936 if (STRNICMP(initstr + 2, "im", 2) == 0)
1937 exmode_active = EXMODE_VIM;
1938 else
1939 exmode_active = EXMODE_NORMAL;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001940 change_compatible(TRUE); // set 'compatible'
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001941 }
1942}
1943
Bram Moolenaar58d98232005-07-23 22:25:46 +00001944/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001945 * Scan the command line arguments.
1946 */
1947 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001948command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001949{
1950 int argc = parmp->argc;
1951 char **argv = parmp->argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001952 int argv_idx; // index in argv[n][]
1953 int had_minmin = FALSE; // found "--" argument
1954 int want_argument; // option argument with argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001955 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001956 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001957 long n;
1958
1959 --argc;
1960 ++argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001961 argv_idx = 1; // active option letter is argv[0][argv_idx]
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001962 while (argc > 0)
1963 {
1964 /*
1965 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1966 */
1967 if (argv[0][0] == '+' && !had_minmin)
1968 {
1969 if (parmp->n_commands >= MAX_ARG_CMDS)
1970 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001971 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001972 if (argv[0][1] == NUL)
1973 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1974 else
1975 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1976 }
1977
1978 /*
1979 * Optional argument.
1980 */
1981 else if (argv[0][0] == '-' && !had_minmin)
1982 {
1983 want_argument = FALSE;
1984 c = argv[0][argv_idx++];
1985#ifdef VMS
1986 /*
1987 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1988 * and "-/X" as "-X".
1989 */
1990 if (c == '/')
1991 {
1992 c = argv[0][argv_idx++];
1993 c = TOUPPER_ASC(c);
1994 }
1995 else
1996 c = TOLOWER_ASC(c);
1997#endif
1998 switch (c)
1999 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002000 case NUL: // "vim -" read from stdin
2001 // "ex -" silent mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002002 if (exmode_active)
2003 silent_mode = TRUE;
2004 else
2005 {
2006 if (parmp->edit_type != EDIT_NONE)
2007 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2008 parmp->edit_type = EDIT_STDIN;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002009 read_cmd_fd = 2; // read from stderr instead of stdin
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002010 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002011 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002012 break;
2013
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002014 case '-': // "--" don't take any more option arguments
2015 // "--help" give help message
2016 // "--version" give version message
2017 // "--clean" clean context
2018 // "--literal" take files literally
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002019 // "--startuptime fname" write timing info
2020 // "--log fname" start logging early
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002021 // "--nofork" don't fork
2022 // "--not-a-term" don't warn for not a term
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002023 // "--gui-dialog-file fname" write dialog text
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002024 // "--ttyfail" exit if not a term
2025 // "--noplugin[s]" skip plugins
2026 // "--cmd <cmd>" execute cmd before vimrc
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002027 if (STRICMP(argv[0] + argv_idx, "help") == 0)
2028 usage();
2029 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
2030 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002031 Columns = 80; // need to init Columns
2032 info_message = TRUE; // use mch_msg(), not mch_errmsg()
K.Takata33b25d12022-01-13 16:06:45 +00002033#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar3b678042021-02-11 19:08:05 +01002034 gui.starting = FALSE; // not starting GUI, will exit
Bram Moolenaar0bcadf12021-02-11 19:18:58 +01002035#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002036 list_version();
2037 msg_putchar('\n');
2038 msg_didout = FALSE;
2039 mch_exit(0);
2040 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002041 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
2042 {
2043 parmp->use_vimrc = (char_u *)"DEFAULTS";
Bram Moolenaar62dd4522018-03-14 21:20:02 +01002044#ifdef FEAT_GUI
2045 use_gvimrc = (char_u *)"NONE";
2046#endif
Bram Moolenaar07268702018-03-01 21:57:32 +01002047 parmp->clean = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002048 set_option_value_give_err((char_u *)"vif",
2049 0L, (char_u *)"NONE", 0);
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002050 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002051 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
2052 {
Bram Moolenaar53076832015-12-31 19:53:21 +01002053#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002054 parmp->literal = TRUE;
2055#endif
2056 }
2057 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
2058 {
2059#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002060 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002061#endif
2062 }
2063 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
2064 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002065 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
2066 parmp->not_a_term = TRUE;
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002067 else if (STRNICMP(argv[0] + argv_idx, "gui-dialog-file", 15)
2068 == 0)
2069 {
2070 want_argument = TRUE;
2071 argv_idx += 15;
2072 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002073 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
2074 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002075 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
2076 {
2077 want_argument = TRUE;
2078 argv_idx += 3;
2079 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002080 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
2081 {
2082 want_argument = TRUE;
2083 argv_idx += 11;
2084 }
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002085 else if (STRNICMP(argv[0] + argv_idx, "log", 3) == 0)
2086 {
2087 want_argument = TRUE;
2088 argv_idx += 3;
2089 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002090#ifdef FEAT_CLIENTSERVER
2091 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002092 ; // already processed -- no arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002093 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
2094 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
2095 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002096 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002097 if (argc > 1)
2098 {
2099 --argc;
2100 ++argv;
2101 }
2102 }
2103#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002104#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002105# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002106 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002107# else
2108 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
2109# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002110 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002111 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002112 if (argc > 1)
2113 {
2114 --argc;
2115 ++argv;
2116 }
2117 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00002118#endif
2119#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002120 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
2121 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002122 // already processed, skip
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002123 }
2124#endif
2125 else
2126 {
2127 if (argv[0][argv_idx])
2128 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2129 had_minmin = TRUE;
2130 }
2131 if (!want_argument)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002132 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002133 break;
2134
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002135 case 'A': // "-A" start in Arabic mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002136#ifdef FEAT_ARABIC
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002137 set_option_value_give_err((char_u *)"arabic", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002138#else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002139 mch_errmsg(_(e_arabic_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002140 mch_exit(2);
2141#endif
2142 break;
2143
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002144 case 'b': // "-b" binary mode
2145 // Needs to be effective before expanding file names, because
2146 // for Win32 this makes us edit a shortcut file itself,
2147 // instead of the file it links to.
Bram Moolenaar231334e2005-07-25 20:46:57 +00002148 set_options_bin(curbuf->b_p_bin, 1, 0);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002149 curbuf->b_p_bin = 1; // binary file I/O
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002150 break;
2151
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002152 case 'C': // "-C" Compatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002153 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02002154 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002155 break;
2156
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002157 case 'e': // "-e" Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002158 exmode_active = EXMODE_NORMAL;
2159 break;
2160
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002161 case 'E': // "-E" Improved Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002162 exmode_active = EXMODE_VIM;
2163 break;
2164
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002165 case 'f': // "-f" GUI: run in foreground. Amiga: open
2166 // window directly, not with newcli
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002167#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002168 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002169#endif
2170 break;
2171
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002172 case 'g': // "-g" start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002173 main_start_gui();
2174 break;
2175
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002176 case 'F': // "-F" was for Farsi mode
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002177 mch_errmsg(_(e_farsi_support_has_been_removed));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002178 mch_exit(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002179 break;
2180
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002181 case '?': // "-?" give help message (for MS-Windows)
2182 case 'h': // "-h" give help message
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002183#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002184 // Tell usage() to exit for "gvim".
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002185 gui.starting = FALSE;
2186#endif
2187 usage();
2188 break;
2189
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002190 case 'H': // "-H" start in Hebrew mode: rl + hkmap set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002191#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002192 p_hkmap = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002193 set_option_value_give_err((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002194#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002195 mch_errmsg(_(e_hebrew_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002196 mch_exit(2);
2197#endif
2198 break;
2199
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002200 case 'l': // "-l" lisp mode, 'lisp' and 'showmatch' on
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002201 set_option_value_give_err((char_u *)"lisp", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002202 p_sm = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002203 break;
2204
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002205 case 'M': // "-M" no changes or writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002206 reset_modifiable();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002207 // FALLTHROUGH
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002208
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002209 case 'm': // "-m" no writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002210 p_write = FALSE;
2211 break;
2212
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002213 case 'y': // "-y" easy mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002214#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002215 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002216#endif
2217 parmp->evim_mode = TRUE;
2218 break;
2219
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002220 case 'N': // "-N" Nocompatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002221 change_compatible(FALSE);
2222 break;
2223
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002224 case 'n': // "-n" no swap file
Bram Moolenaar67c53842010-05-22 18:28:27 +02002225#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002226 // checking for "-nb", netbeans parameters
Bram Moolenaar67c53842010-05-22 18:28:27 +02002227 if (argv[0][argv_idx] == 'b')
2228 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002229 netbeansArg = argv[0];
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002230 argv_idx = -1; // skip to next argument
Bram Moolenaar67c53842010-05-22 18:28:27 +02002231 }
2232 else
2233#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002234 parmp->no_swap_file = TRUE;
2235 break;
2236
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002237 case 'p': // "-p[N]" open N tab pages
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002238#ifdef TARGET_API_MAC_OSX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002239 // For some reason on MacOS X, an argument like:
2240 // -psn_0_10223617 is passed in when invoke from Finder
2241 // or with the 'open' command
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002242 if (argv[0][argv_idx] == 's')
2243 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002244 argv_idx = -1; // bypass full -psn
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002245 main_start_gui();
2246 break;
2247 }
2248#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002249 // default is 0: open window for each file
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002250 parmp->window_count = get_number_arg((char_u *)argv[0],
2251 &argv_idx, 0);
2252 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002253 break;
2254
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002255 case 'o': // "-o[N]" open N horizontal split windows
2256 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002257 parmp->window_count = get_number_arg((char_u *)argv[0],
2258 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002259 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002260 break;
2261
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002262 case 'O': // "-O[N]" open N vertical split windows
2263 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002264 parmp->window_count = get_number_arg((char_u *)argv[0],
2265 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002266 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002267 break;
2268
2269#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002270 case 'q': // "-q" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002271 if (parmp->edit_type != EDIT_NONE)
2272 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2273 parmp->edit_type = EDIT_QF;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002274 if (argv[0][argv_idx]) // "-q{errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002275 {
2276 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2277 argv_idx = -1;
2278 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002279 else if (argc > 1) // "-q {errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002280 want_argument = TRUE;
2281 break;
2282#endif
2283
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002284 case 'R': // "-R" readonly mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002285 readonlymode = TRUE;
2286 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002287 p_uc = 10000; // don't update very often
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002288 break;
2289
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002290 case 'r': // "-r" recovery mode
2291 case 'L': // "-L" recovery mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002292 recoverymode = 1;
2293 break;
2294
2295 case 's':
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002296 if (exmode_active) // "-s" silent (batch) mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002297 silent_mode = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002298 else // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002299 want_argument = TRUE;
2300 break;
2301
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002302 case 't': // "-t {tag}" or "-t{tag}" jump to tag
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002303 if (parmp->edit_type != EDIT_NONE)
2304 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2305 parmp->edit_type = EDIT_TAG;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002306 if (argv[0][argv_idx]) // "-t{tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002307 {
2308 parmp->tagname = (char_u *)argv[0] + argv_idx;
2309 argv_idx = -1;
2310 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002311 else // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002312 want_argument = TRUE;
2313 break;
2314
2315#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002316 case 'D': // "-D" Debugging
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002317 parmp->use_debug_break_level = 9999;
2318 break;
2319#endif
2320#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002321 case 'd': // "-d" 'diff'
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002322# ifdef AMIGA
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002323 // check for "-dev {device}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002324 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2325 want_argument = TRUE;
2326 else
2327# endif
2328 parmp->diff_mode = TRUE;
2329 break;
2330#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002331 case 'V': // "-V{N}" Verbose level
2332 // default is 10: a little bit verbose
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002333 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2334 if (argv[0][argv_idx] != NUL)
2335 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002336 set_option_value_give_err((char_u *)"verbosefile",
2337 0L, (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002338 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002339 }
2340 break;
2341
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002342 case 'v': // "-v" Vi-mode (as if called "vi")
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002343 exmode_active = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002344#if defined(FEAT_GUI) && !defined(VIMDLL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002345 gui.starting = FALSE; // don't start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002346#endif
2347 break;
2348
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002349 case 'w': // "-w{number}" set window height
2350 // "-w {scriptout}" write to script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002351 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2352 {
2353 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002354 set_option_value_give_err((char_u *)"window", n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002355 break;
2356 }
2357 want_argument = TRUE;
2358 break;
2359
2360#ifdef FEAT_CRYPT
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002361 case 'x': // "-x" encrypted reading/writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002362 parmp->ask_for_key = TRUE;
2363 break;
2364#endif
2365
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002366 case 'X': // "-X" don't connect to X server
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002367#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2368 x_no_connect = TRUE;
2369#endif
2370 break;
2371
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002372 case 'Z': // "-Z" restricted mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002373 restricted = TRUE;
2374 break;
2375
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002376 case 'c': // "-c{command}" or "-c {command}" execute
2377 // command
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002378 if (argv[0][argv_idx] != NUL)
2379 {
2380 if (parmp->n_commands >= MAX_ARG_CMDS)
2381 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002382 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2383 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002384 argv_idx = -1;
2385 break;
2386 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002387 // FALLTHROUGH
2388 case 'S': // "-S {file}" execute Vim script
2389 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002390#ifndef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002391 case 'd': // "-d {device}" device (for Amiga)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002392#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002393 case 'T': // "-T {terminal}" terminal name
2394 case 'u': // "-u {vimrc}" vim inits file
2395 case 'U': // "-U {gvimrc}" gvim inits file
2396 case 'W': // "-W {scriptout}" overwrite
Bram Moolenaar4f974752019-02-17 17:44:42 +01002397#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002398 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002399#endif
2400 want_argument = TRUE;
2401 break;
2402
2403 default:
2404 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2405 }
2406
2407 /*
2408 * Handle option arguments with argument.
2409 */
2410 if (want_argument)
2411 {
2412 /*
2413 * Check for garbage immediately after the option letter.
2414 */
2415 if (argv[0][argv_idx] != NUL)
2416 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2417
2418 --argc;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002419 if (argc < 1 && c != 'S') // -S has an optional argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002420 mainerr_arg_missing((char_u *)argv[0]);
2421 ++argv;
2422 argv_idx = -1;
2423
2424 switch (c)
2425 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002426 case 'c': // "-c {command}" execute command
2427 case 'S': // "-S {file}" execute Vim script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002428 if (parmp->n_commands >= MAX_ARG_CMDS)
2429 mainerr(ME_EXTRA_CMD, NULL);
2430 if (c == 'S')
2431 {
2432 char *a;
2433
2434 if (argc < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002435 // "-S" without argument: use default session file
2436 // name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002437 a = SESSION_FILE;
2438 else if (argv[0][0] == '-')
2439 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002440 // "-S" followed by another option: use default
2441 // session file name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002442 a = SESSION_FILE;
2443 ++argc;
2444 --argv;
2445 }
2446 else
2447 a = argv[0];
Bram Moolenaar964b3742019-05-24 18:54:09 +02002448 p = alloc(STRLEN(a) + 4);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002449 if (p == NULL)
2450 mch_exit(2);
2451 sprintf((char *)p, "so %s", a);
2452 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2453 parmp->commands[parmp->n_commands++] = p;
2454 }
2455 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002456 parmp->commands[parmp->n_commands++] =
2457 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002458 break;
2459
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002460 case '-':
2461 if (argv[-1][2] == 'c')
2462 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002463 // "--cmd {command}" execute command
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002464 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2465 mainerr(ME_EXTRA_CMD, NULL);
2466 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002467 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002468 }
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002469 // --gui-dialog-file fname
2470 if (argv[-1][2] == 'g')
2471 {
2472 // without GUI ignore the argument
2473#ifdef FEAT_GUI
2474 parmp->gui_dialog_file = (char_u *)argv[0];
2475#endif
2476 }
2477
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002478 // "--startuptime <file>" already handled
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002479 // "--log <file>" already handled
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002480 break;
2481
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002482 // case 'd': -d {device} is handled in mch_check_win() for the
2483 // Amiga
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002484
2485#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002486 case 'q': // "-q {errorfile}" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002487 parmp->use_ef = (char_u *)argv[0];
2488 break;
2489#endif
2490
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002491 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002492 set_option_value_give_err((char_u *)"vif",
2493 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002494 break;
2495
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002496 case 's': // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002497 if (scriptin[0] != NULL)
2498 {
2499scripterror:
2500 mch_errmsg(_("Attempt to open script file again: \""));
2501 mch_errmsg(argv[-1]);
2502 mch_errmsg(" ");
2503 mch_errmsg(argv[0]);
2504 mch_errmsg("\"\n");
2505 mch_exit(2);
2506 }
2507 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2508 {
2509 mch_errmsg(_("Cannot open for reading: \""));
2510 mch_errmsg(argv[0]);
2511 mch_errmsg("\"\n");
2512 mch_exit(2);
2513 }
2514 if (save_typebuf() == FAIL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002515 mch_exit(2); // out of memory
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002516 break;
2517
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002518 case 't': // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002519 parmp->tagname = (char_u *)argv[0];
2520 break;
2521
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002522 case 'T': // "-T {terminal}" terminal name
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002523 /*
2524 * The -T term argument is always available and when
2525 * HAVE_TERMLIB is supported it overrides the environment
2526 * variable TERM.
2527 */
2528#ifdef FEAT_GUI
2529 if (term_is_gui((char_u *)argv[0]))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002530 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002531 else
2532#endif
2533 parmp->term = (char_u *)argv[0];
2534 break;
2535
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002536 case 'u': // "-u {vimrc}" vim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002537 parmp->use_vimrc = (char_u *)argv[0];
2538 break;
2539
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002540 case 'U': // "-U {gvimrc}" gvim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002541#ifdef FEAT_GUI
2542 use_gvimrc = (char_u *)argv[0];
2543#endif
2544 break;
2545
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002546 case 'w': // "-w {nr}" 'window' value
2547 // "-w {scriptout}" append to script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002548 if (vim_isdigit(*((char_u *)argv[0])))
2549 {
2550 argv_idx = 0;
2551 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002552 set_option_value_give_err((char_u *)"window",
2553 n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002554 argv_idx = -1;
2555 break;
2556 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002557 // FALLTHROUGH
2558 case 'W': // "-W {scriptout}" overwrite script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002559 if (scriptout != NULL)
2560 goto scripterror;
2561 if ((scriptout = mch_fopen(argv[0],
2562 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2563 {
2564 mch_errmsg(_("Cannot open for script output: \""));
2565 mch_errmsg(argv[0]);
2566 mch_errmsg("\"\n");
2567 mch_exit(2);
2568 }
2569 break;
2570
Bram Moolenaar4f974752019-02-17 17:44:42 +01002571#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002572 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002573 gui_mch_set_parent(argv[0]);
2574 break;
2575#endif
2576 }
2577 }
2578 }
2579
2580 /*
2581 * File name argument.
2582 */
2583 else
2584 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002585 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002586
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002587 // Check for only one type of editing.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002588 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2589 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2590 parmp->edit_type = EDIT_FILE;
2591
2592#ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002593 // Remember if the argument was a full path before changing
2594 // slashes to backslashes.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002595 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2596 parmp->full_path = TRUE;
2597#endif
2598
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002599 // Add the file to the global argument list.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002600 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2601 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2602 mch_exit(2);
2603#ifdef FEAT_DIFF
2604 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2605 && !mch_isdir(alist_name(&GARGLIST[0])))
2606 {
2607 char_u *r;
2608
2609 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2610 if (r != NULL)
2611 {
2612 vim_free(p);
2613 p = r;
2614 }
2615 }
2616#endif
K.Takatab247e062022-02-07 10:45:23 +00002617#ifdef __CYGWIN__
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002618 /*
2619 * If vim is invoked by non-Cygwin tools, convert away any
2620 * DOS paths, so things like .swp files are created correctly.
2621 * Look for evidence of non-Cygwin paths before we bother.
2622 * This is only for when using the Unix files.
2623 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002624 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002625 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002626 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002627
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002628# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002629 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002630# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002631 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002632# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002633 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002634 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002635 if (p == NULL)
2636 mch_exit(2);
2637 }
2638#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002639
2640#ifdef USE_FNAME_CASE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002641 // Make the case of the file name match the actual file.
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002642 fname_case(p, 0);
2643#endif
2644
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002645 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002646#ifdef EXPAND_FILENAMES
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002647 parmp->literal ? 2 : 0 // add buffer nr after exp.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002648#else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002649 2 // add buffer number now and use curbuf
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002650#endif
2651 );
2652
Bram Moolenaar4f974752019-02-17 17:44:42 +01002653#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002654 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002655 // Remember this argument has been added to the argument list.
2656 // Needed when 'encoding' is changed.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002657 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002658# ifdef FEAT_DIFF
2659 parmp->diff_mode
2660# else
2661 FALSE
2662# endif
2663 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002664 }
2665#endif
2666 }
2667
2668 /*
2669 * If there are no more letters after the current "-", go to next
2670 * argument. argv_idx is set to -1 when the current argument is to be
2671 * skipped.
2672 */
2673 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2674 {
2675 --argc;
2676 ++argv;
2677 argv_idx = 1;
2678 }
2679 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002680
2681#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002682 // If there is a "+123" or "-c" command, set v:swapcommand to the first
2683 // one.
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002684 if (parmp->n_commands > 0)
2685 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002686 p = alloc(STRLEN(parmp->commands[0]) + 3);
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002687 if (p != NULL)
2688 {
2689 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2690 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2691 vim_free(p);
2692 }
2693 }
2694#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002695}
2696
2697/*
2698 * Print a warning if stdout is not a terminal.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +01002699 * When starting in Ex mode and commands come from a file, set silent_mode.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002700 */
2701 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002702check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002703{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002704 int input_isatty; // is active input a terminal?
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002705
2706 input_isatty = mch_input_isatty();
2707 if (exmode_active)
2708 {
2709 if (!input_isatty)
2710 silent_mode = TRUE;
2711 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002712 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002713#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002714 // don't want the delay when started from the desktop
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002715 && !gui.starting
2716#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002717 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002718 {
2719#ifdef NBDEBUG
2720 /*
2721 * This shouldn't be necessary. But if I run netbeans with the log
2722 * output coming to the console and XOpenDisplay fails, I get vim
2723 * trying to start with input/output to my console tty. This fills my
2724 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002725 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002726 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002727 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002728 {
2729 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2730 exit(1);
2731 }
2732#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002733#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2734 if (
2735# ifdef VIMDLL
2736 !gui.starting &&
2737# endif
2738 is_cygpty_used())
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002739 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002740# if defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
Bram Moolenaar2a027452017-09-26 19:10:37 +02002741 && defined(FEAT_GETTEXT)
2742 char *s, *tofree = NULL;
2743
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002744 // Set the encoding of the error message based on $LC_ALL or
2745 // other environment variables instead of 'encoding'.
2746 // Note that the message is shown on a Cygwin terminal (e.g.
2747 // mintty) which encoding is based on $LC_ALL or etc., not the
2748 // current codepage used by normal Win32 console programs.
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002749 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002750 if (s == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002751 s = "utf-8"; // Use "utf-8" by default.
Bram Moolenaar2a027452017-09-26 19:10:37 +02002752 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2753 vim_free(tofree);
2754# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002755 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2756 exit(1);
2757 }
2758#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002759 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002760 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2761 if (!input_isatty)
2762 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2763 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002764 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2765 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002766 if (scriptin[0] == NULL)
Bram Moolenaareda1da02019-11-17 17:06:33 +01002767 ui_delay(2005L, TRUE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002768 TIME_MSG("Warning delay");
2769 }
2770}
2771
2772/*
2773 * Read text from stdin.
2774 */
2775 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002776read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002777{
2778 int i;
2779
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002780 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002781 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002782
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002783 no_wait_return = TRUE;
2784 i = msg_didany;
2785 set_buflisted(TRUE);
Bram Moolenaar204ade62020-10-11 14:58:46 +02002786
2787 // Create memfile and read from stdin.
Bram Moolenaar204ade62020-10-11 14:58:46 +02002788 (void)open_buffer(TRUE, NULL, 0);
2789
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002790 no_wait_return = FALSE;
2791 msg_didany = i;
2792 TIME_MSG("reading stdin");
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002793
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002794 check_swap_exists_action();
Bram Moolenaar8e1cbb52020-12-08 19:36:21 +01002795
2796#if !(defined(AMIGA) || defined(MACOS_X))
2797 // Dup stdin from stderr to read commands from, so that shell commands
2798 // work.
2799 // TODO: why is this needed, even though readfile() has done this?
2800 close(0);
2801 vim_ignored = dup(2);
2802#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002803}
2804
2805/*
2806 * Create the requested number of windows and edit buffers in them.
2807 * Also does recovery if "recoverymode" set.
2808 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002809 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002810create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002811{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002812 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002813 int done = 0;
2814
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002815 /*
2816 * Create the number of windows that was requested.
2817 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002818 if (parmp->window_count == -1) // was not set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002819 parmp->window_count = 1;
2820 if (parmp->window_count == 0)
2821 parmp->window_count = GARGCOUNT;
2822 if (parmp->window_count > 1)
2823 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002824 // Don't change the windows if there was a command in .vimrc that
2825 // already split some windows
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002826 if (parmp->window_layout == 0)
2827 parmp->window_layout = WIN_HOR;
2828 if (parmp->window_layout == WIN_TABS)
2829 {
2830 parmp->window_count = make_tabpages(parmp->window_count);
2831 TIME_MSG("making tab pages");
2832 }
2833 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002834 {
2835 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002836 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002837 TIME_MSG("making windows");
2838 }
2839 else
2840 parmp->window_count = win_count();
2841 }
2842 else
2843 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002844
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002845 if (recoverymode) // do recover
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002846 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002847 msg_scroll = TRUE; // scroll message up
Bram Moolenaar99499b12019-05-23 21:35:48 +02002848 ml_recover(TRUE);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002849 if (curbuf->b_ml.ml_mfp == NULL) // failed
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002850 getout(1);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002851 do_modelines(0); // do modelines
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002852 }
2853 else
2854 {
2855 /*
2856 * Open a buffer for windows that don't have one yet.
2857 * Commands in the .vimrc might have loaded a file or split the window.
2858 * Watch out for autocommands that delete a window.
2859 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002860 /*
2861 * Don't execute Win/Buf Enter/Leave autocommands here
2862 */
2863 ++autocmd_no_enter;
2864 ++autocmd_no_leave;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002865 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002866 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002867 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002868 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002869 {
2870 if (parmp->window_layout == WIN_TABS)
2871 goto_tabpage(1);
2872 else
2873 curwin = firstwin;
2874 }
2875 else if (parmp->window_layout == WIN_TABS)
2876 {
2877 if (curtab->tp_next == NULL)
2878 break;
2879 goto_tabpage(0);
2880 }
2881 else
2882 {
2883 if (curwin->w_next == NULL)
2884 break;
2885 curwin = curwin->w_next;
2886 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002887 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002888 curbuf = curwin->w_buffer;
2889 if (curbuf->b_ml.ml_mfp == NULL)
2890 {
2891#ifdef FEAT_FOLDING
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002892 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002893 if (p_fdls >= 0)
2894 curwin->w_p_fdl = p_fdls;
2895#endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002896 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002897 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002898
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002899 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002900
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002901 // create memfile, read file
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002902 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002903
Bram Moolenaar84212822006-11-07 21:59:47 +00002904 if (swap_exists_action == SEA_QUIT)
2905 {
2906 if (got_int || only_one_window())
2907 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002908 // abort selected or quit and only one window
2909 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00002910 getout(1);
2911 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002912 // We can't close the window, it would disturb what
2913 // happens next. Clear the file name and set the arg
2914 // index to -1 to delete it later.
Bram Moolenaar84212822006-11-07 21:59:47 +00002915 setfname(curbuf, NULL, NULL, FALSE);
2916 curwin->w_arg_idx = -1;
2917 swap_exists_action = SEA_NONE;
2918 }
2919 else
2920 handle_swap_exists(NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002921 dorewind = TRUE; // start again
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002922 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002923 ui_breakcheck();
2924 if (got_int)
2925 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002926 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002927 break;
2928 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002929 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002930 if (parmp->window_layout == WIN_TABS)
2931 goto_tabpage(1);
2932 else
2933 curwin = firstwin;
2934 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002935 --autocmd_no_enter;
2936 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002937 }
2938}
2939
Bram Moolenaar9810cfb2019-12-11 21:23:00 +01002940/*
2941 * If opened more than one window, start editing files in the other
2942 * windows. make_windows() has already opened the windows.
2943 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002944 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002945edit_buffers(
2946 mparm_T *parmp,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002947 char_u *cwd) // current working dir
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002948{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002949 int arg_idx; // index in argument list
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002950 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002951 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002952 win_T *win;
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002953 char_u *p_shm_save = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002954
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002955 /*
2956 * Don't execute Win/Buf Enter/Leave autocommands here
2957 */
2958 ++autocmd_no_enter;
2959 ++autocmd_no_leave;
Bram Moolenaar84212822006-11-07 21:59:47 +00002960
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002961 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002962 if (curwin->w_arg_idx == -1)
2963 {
2964 win_close(curwin, TRUE);
2965 advance = FALSE;
2966 }
2967
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002968 arg_idx = 1;
2969 for (i = 1; i < parmp->window_count; ++i)
2970 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002971 if (cwd != NULL)
2972 mch_chdir((char *)cwd);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002973 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002974 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002975 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002976 ++arg_idx;
2977 win_close(curwin, TRUE);
2978 advance = FALSE;
2979 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002980 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002981
Bram Moolenaar84212822006-11-07 21:59:47 +00002982 if (advance)
2983 {
2984 if (parmp->window_layout == WIN_TABS)
2985 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002986 if (curtab->tp_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00002987 break;
2988 goto_tabpage(0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002989 // Temporarily reset 'shm' option to not print fileinfo when
2990 // loading the other buffers. This would overwrite the already
2991 // existing fileinfo for the first tab.
2992 if (i == 1)
2993 {
2994 char buf[100];
2995
2996 p_shm_save = vim_strsave(p_shm);
2997 vim_snprintf(buf, 100, "F%s", p_shm);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002998 set_option_value_give_err((char_u *)"shm",
2999 0L, (char_u *)buf, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003000 }
Bram Moolenaar84212822006-11-07 21:59:47 +00003001 }
3002 else
3003 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003004 if (curwin->w_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00003005 break;
3006 win_enter(curwin->w_next, FALSE);
3007 }
3008 }
3009 advance = TRUE;
3010
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003011 // Only open the file if there is no file in this window yet (that can
3012 // happen when .vimrc contains ":sall").
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003013 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
3014 {
3015 curwin->w_arg_idx = arg_idx;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003016 // Edit file from arg list, if there is one. When "Quit" selected
3017 // at the ATTENTION prompt close the window.
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003018 swap_exists_did_quit = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003019 (void)do_ecmd(0, arg_idx < GARGCOUNT
3020 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003021 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003022 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00003023 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003024 // abort or quit selected
Bram Moolenaar84212822006-11-07 21:59:47 +00003025 if (got_int || only_one_window())
3026 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003027 // abort selected and only one window
3028 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00003029 getout(1);
3030 }
3031 win_close(curwin, TRUE);
3032 advance = FALSE;
3033 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003034 if (arg_idx == GARGCOUNT - 1)
3035 arg_had_last = TRUE;
3036 ++arg_idx;
3037 }
3038 ui_breakcheck();
3039 if (got_int)
3040 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003041 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003042 break;
3043 }
3044 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003045
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003046 if (p_shm_save != NULL)
3047 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003048 set_option_value_give_err((char_u *)"shm", 0L, p_shm_save, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003049 vim_free(p_shm_save);
3050 }
3051
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003052 if (parmp->window_layout == WIN_TABS)
3053 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003054 --autocmd_no_enter;
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003055
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003056 // make the first window the current window
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003057 win = firstwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003058#if defined(FEAT_QUICKFIX)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003059 // Avoid making a preview window the current window.
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003060 while (win->w_p_pvw)
3061 {
3062 win = win->w_next;
3063 if (win == NULL)
3064 {
3065 win = firstwin;
3066 break;
3067 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003068 }
3069#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003070 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003071
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003072 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003073 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003074 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003075 win_equal(curwin, FALSE, 'b'); // adjust heights
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003076}
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003077
3078/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003079 * Execute the commands from --cmd arguments "cmds[cnt]".
3080 */
3081 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003082exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003083{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003084 char_u **cmds = parmp->pre_commands;
3085 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003086 int i;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003087 ESTACK_CHECK_DECLARATION
Bram Moolenaar58d98232005-07-23 22:25:46 +00003088
3089 if (cnt > 0)
3090 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003091 curwin->w_cursor.lnum = 0; // just in case..
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003092 estack_push(ETYPE_ARGS, (char_u *)_("pre-vimrc command line"), 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003093 ESTACK_CHECK_SETUP
Bram Moolenaar58d98232005-07-23 22:25:46 +00003094# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003095 current_sctx.sc_sid = SID_CMDARG;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003096# endif
3097 for (i = 0; i < cnt; ++i)
3098 do_cmdline_cmd(cmds[i]);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003099 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003100 estack_pop();
Bram Moolenaar58d98232005-07-23 22:25:46 +00003101# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003102 current_sctx.sc_sid = 0;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003103# endif
3104 TIME_MSG("--cmd commands");
3105 }
3106}
3107
3108/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003109 * Execute "+", "-c" and "-S" arguments.
3110 */
3111 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003112exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003113{
3114 int i;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003115 ESTACK_CHECK_DECLARATION
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003116
3117 /*
3118 * We start commands on line 0, make "vim +/pat file" match a
3119 * pattern on line 1. But don't move the cursor when an autocommand
3120 * with g`" was used.
3121 */
3122 msg_scroll = TRUE;
3123 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
3124 curwin->w_cursor.lnum = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003125 estack_push(ETYPE_ARGS, (char_u *)"command line", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003126 ESTACK_CHECK_SETUP
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003127#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003128 current_sctx.sc_sid = SID_CARG;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003129 current_sctx.sc_seq = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003130#endif
3131 for (i = 0; i < parmp->n_commands; ++i)
3132 {
3133 do_cmdline_cmd(parmp->commands[i]);
3134 if (parmp->cmds_tofree[i])
3135 vim_free(parmp->commands[i]);
3136 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01003137 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003138 estack_pop();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003139#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003140 current_sctx.sc_sid = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003141#endif
3142 if (curwin->w_cursor.lnum == 0)
3143 curwin->w_cursor.lnum = 1;
3144
3145 if (!exmode_active)
3146 msg_scroll = FALSE;
3147
3148#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003149 // When started with "-q errorfile" jump to first error again.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003150 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003151 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003152#endif
3153 TIME_MSG("executing command arguments");
3154}
3155
3156/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003157 * Source startup scripts.
3158 */
3159 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003160source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003161{
3162 int i;
3163
3164 /*
3165 * For "evim" source evim.vim first of all, so that the user can overrule
3166 * any things he doesn't like.
3167 */
3168 if (parmp->evim_mode)
3169 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003171 TIME_MSG("source evim file");
3172 }
3173
3174 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003175 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00003176 * nothing else.
3177 */
3178 if (parmp->use_vimrc != NULL)
3179 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003180 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003181 {
3182 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE, NULL)
3183 != OK)
3184 emsg(e_failed_to_source_defaults);
3185 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003186 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003187 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003188 {
3189#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003190 if (use_gvimrc == NULL) // don't load gvimrc either
Bram Moolenaar58d98232005-07-23 22:25:46 +00003191 use_gvimrc = parmp->use_vimrc;
3192#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003193 }
3194 else
3195 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00003197 semsg(_(e_cannot_read_from_str_2), parmp->use_vimrc);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003198 }
3199 }
3200 else if (!silent_mode)
3201 {
3202#ifdef AMIGA
3203 struct Process *proc = (struct Process *)FindTask(0L);
3204 APTR save_winptr = proc->pr_WindowPtr;
3205
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003206 // Avoid a requester here for a volume that doesn't exist.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003207 proc->pr_WindowPtr = (APTR)-1L;
3208#endif
3209
3210 /*
3211 * Get system wide defaults, if the file name is defined.
3212 */
3213#ifdef SYS_VIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003215#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003216#ifdef MACOS_X
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE,
3218 DOSO_NONE, NULL);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003219#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003220
3221 /*
3222 * Try to read initialization commands from the following places:
3223 * - environment variable VIMINIT
3224 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3225 * - second user vimrc file ($VIM/.vimrc for Dos)
3226 * - environment variable EXINIT
3227 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3228 * - second user exrc file ($VIM/.exrc for Dos)
3229 * The first that exists is used, the rest is ignored.
3230 */
3231 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3232 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 if (do_source((char_u *)USR_VIMRC_FILE, TRUE,
3234 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003235#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003236 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003238#endif
3239#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003240 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003242#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003243#ifdef USR_VIMRC_FILE4
3244 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003246#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003247 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 && do_source((char_u *)USR_EXRC_FILE, FALSE,
3249 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003250#ifdef USR_EXRC_FILE2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 && do_source((char_u *)USR_EXRC_FILE2, FALSE,
3252 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003253#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003254 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003255 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003256 // When no .vimrc file was found: source defaults.vim.
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003257 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE,
3258 NULL) == FAIL)
3259 emsg(e_failed_to_source_defaults);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003260 }
3261 }
3262
3263 /*
3264 * Read initialization commands from ".vimrc" or ".exrc" in current
3265 * directory. This is only done if the 'exrc' option is set.
3266 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003267 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003268 * option has been reset in environment of global ".exrc" or ".vimrc".
3269 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3270 * SYS_VIMRC_FILE.
3271 */
3272 if (p_exrc)
3273 {
3274#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003275 // If ".vimrc" file is not owned by user, set 'secure' mode.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003276 if (!file_owned(VIMRC_FILE))
3277#endif
3278 secure = p_secure;
3279
3280 i = FAIL;
3281 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003282 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003283#ifdef USR_VIMRC_FILE2
3284 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003285 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003286#endif
3287#ifdef USR_VIMRC_FILE3
3288 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003289 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003290#endif
3291#ifdef SYS_VIMRC_FILE
3292 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003293 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003294#endif
3295 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003297
3298 if (i == FAIL)
3299 {
3300#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003301 // if ".exrc" is not owned by user set 'secure' mode
Bram Moolenaar58d98232005-07-23 22:25:46 +00003302 if (!file_owned(EXRC_FILE))
3303 secure = p_secure;
3304 else
3305 secure = 0;
3306#endif
3307 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003308 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003309#ifdef USR_EXRC_FILE2
3310 && fullpathcmp((char_u *)USR_EXRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003311 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003312#endif
3313 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 (void)do_source((char_u *)EXRC_FILE, FALSE,
3315 DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003316 }
3317 }
3318 if (secure == 2)
3319 need_wait_return = TRUE;
3320 secure = 0;
3321#ifdef AMIGA
3322 proc->pr_WindowPtr = save_winptr;
3323#endif
3324 }
3325 TIME_MSG("sourcing vimrc file(s)");
3326}
3327
3328/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003329 * Setup to start using the GUI. Exit with an error when not available.
3330 */
3331 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003332main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003333{
3334#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003335 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003336#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003337 mch_errmsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003338 mch_errmsg("\n");
3339 mch_exit(2);
3340#endif
3341}
3342
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003343#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003344
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003345/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 * Get an environment variable and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003347 * Returns FAIL if the environment variable was not executed, OK otherwise.
3348 */
3349 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003350process_env(
3351 char_u *env,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003352 int is_viminit) // when TRUE, called for VIMINIT
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003353{
3354 char_u *initstr;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003355 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003356
Bram Moolenaare31ee862020-01-07 20:59:34 +01003357 ESTACK_CHECK_DECLARATION
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003358
3359 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3360 {
3361 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003362 vimrc_found(NULL, NULL);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003363 estack_push(ETYPE_ENV, env, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003364 ESTACK_CHECK_SETUP
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003365 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003366 current_sctx.sc_version = 1;
3367#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003368 current_sctx.sc_sid = SID_ENV;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003369 current_sctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003370 current_sctx.sc_lnum = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003371#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003372
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003373 do_cmdline_cmd(initstr);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003374
Bram Moolenaare31ee862020-01-07 20:59:34 +01003375 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003376 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003377 current_sctx = save_current_sctx;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003378 return OK;
3379 }
3380 return FAIL;
3381}
3382
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003383#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003384/*
3385 * Return TRUE if we are certain the user owns the file "fname".
3386 * Used for ".vimrc" and ".exrc".
3387 * Use both stat() and lstat() for extra security.
3388 */
3389 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003390file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003391{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003392 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003393# ifdef UNIX
3394 uid_t uid = getuid();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003395# else // VMS
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003396 uid_t uid = ((getgid() << 16) | getuid());
3397# endif
3398
3399 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3400# ifdef HAVE_LSTAT
3401 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3402# endif
3403 );
3404}
3405#endif
3406
3407/*
3408 * Give an error message main_errors["n"] and exit.
3409 */
3410 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003411mainerr(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003412 int n, // one of the ME_ defines
3413 char_u *str) // extra argument or NULL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003414{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003415#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003416 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003417#endif
3418
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003419 // If this is a Windows GUI executable, show an error dialog box.
3420#ifdef VIMDLL
3421 gui.in_use = mch_is_gui_executable();
3422#endif
3423#ifdef FEAT_GUI_MSWIN
3424 gui.starting = FALSE; // Needed to show as error.
3425#endif
3426
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003427 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003428 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003429 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003430 mch_errmsg(_(main_errors[n]));
3431 if (str != NULL)
3432 {
3433 mch_errmsg(": \"");
3434 mch_errmsg((char *)str);
3435 mch_errmsg("\"");
3436 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003437 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003438
3439 mch_exit(1);
3440}
3441
3442 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003443mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003444{
3445 mainerr(ME_ARG_MISSING, str);
3446}
3447
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003448#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003449/*
3450 * print a message with three spaces prepended and '\n' appended.
3451 */
3452 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003453main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003454{
3455 mch_msg(" ");
3456 mch_msg(s);
3457 mch_msg("\n");
3458}
3459
3460/*
3461 * Print messages for "vim -h" or "vim --help" and exit.
3462 */
3463 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003464usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003465{
3466 int i;
3467 static char *(use[]) =
3468 {
3469 N_("[file ..] edit specified file(s)"),
3470 N_("- read text from stdin"),
3471 N_("-t tag edit file where tag is defined"),
3472#ifdef FEAT_QUICKFIX
3473 N_("-q [errorfile] edit file with first error")
3474#endif
3475 };
3476
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003477#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003478 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003479#endif
3480
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003481 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003482 mch_msg(longVersion);
Bram Moolenaar32aaf5a2018-06-21 21:38:33 +02003483 mch_msg(_("\n\nUsage:"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003484 for (i = 0; ; ++i)
3485 {
3486 mch_msg(_(" vim [arguments] "));
3487 mch_msg(_(use[i]));
K.Takataeeec2542021-06-02 13:28:16 +02003488 if (i == ARRAY_LENGTH(use) - 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003489 break;
3490 mch_msg(_("\n or:"));
3491 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003492#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003493 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003494#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003495
3496 mch_msg(_("\n\nArguments:\n"));
3497 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003498#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003499 main_msg(_("--literal\t\tDon't expand wildcards"));
3500#endif
3501#ifdef FEAT_OLE
3502 main_msg(_("-register\t\tRegister this gvim for OLE"));
3503 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3504#endif
3505#ifdef FEAT_GUI
3506 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3507 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3508#endif
3509 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3510 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003511 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003512 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3513#ifdef FEAT_DIFF
3514 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3515#endif
3516 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3517 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3518 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3519 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3520 main_msg(_("-M\t\t\tModifications in text not allowed"));
3521 main_msg(_("-b\t\t\tBinary mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003522 main_msg(_("-l\t\t\tLisp mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003523 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3524 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003525 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3526#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003527 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003528#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003529 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3530 main_msg(_("-r\t\t\tList swap files and exit"));
3531 main_msg(_("-r (with file name)\tRecover crashed session"));
3532 main_msg(_("-L\t\t\tSame as -r"));
3533#ifdef AMIGA
3534 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3535 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3536#endif
3537#ifdef FEAT_ARABIC
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003538 main_msg(_("-A\t\t\tStart in Arabic mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003539#endif
3540#ifdef FEAT_RIGHTLEFT
3541 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3542#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003543 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003544 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2d12c252022-06-13 21:42:45 +01003545#ifdef FEAT_GUI
3546 main_msg(_("--gui-dialog-file {fname} For testing: write dialog text"));
3547#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003548 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003549 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3550#ifdef FEAT_GUI
3551 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3552#endif
3553 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003554 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003555 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3556 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3557 main_msg(_("+\t\t\tStart at end of file"));
3558 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003559 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003560 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3561 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3562 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3563 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3564 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3565#ifdef FEAT_CRYPT
3566 main_msg(_("-x\t\t\tEdit encrypted files"));
3567#endif
3568#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3569# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar86181df2020-05-11 23:14:04 +02003570 main_msg(_("-display <display>\tConnect Vim to this particular X-server"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003571# endif
3572 main_msg(_("-X\t\t\tDo not connect to X server"));
3573#endif
3574#ifdef FEAT_CLIENTSERVER
3575 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3576 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3577 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3578 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003579 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003580 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3581 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3582 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3583 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3584#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003585#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003586 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003587#endif
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01003588#ifdef FEAT_JOB_CHANNEL
3589 main_msg(_("--log <file>\tStart logging to <file> early"));
3590#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003591#ifdef FEAT_VIMINFO
3592 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3593#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003594 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003595 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3596 main_msg(_("--version\t\tPrint version information and exit"));
3597
3598#ifdef FEAT_GUI_X11
3599# ifdef FEAT_GUI_MOTIF
3600 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003601# endif
Bram Moolenaar86181df2020-05-11 23:14:04 +02003602 main_msg(_("-display <display>\tRun Vim on <display>"));
Bram Moolenaar9e6ba8c2020-05-12 22:21:26 +02003603 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003604 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3605 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3606 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3607 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3608 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3609 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3610 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3611 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003612 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3613 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3614 main_msg(_("-xrm <resource>\tSet the specified resource"));
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003615#endif // FEAT_GUI_X11
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003616#ifdef FEAT_GUI_GTK
3617 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003618 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3619 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003620 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3621 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003622 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003623 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
Bram Moolenaar86181df2020-05-11 23:14:04 +02003624 main_msg(_("-display <display>\tRun Vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003625 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003626 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003627 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003628#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003629#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003630# ifdef VIMDLL
3631 if (gui.starting)
3632# endif
3633 {
3634 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
3635 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
3636 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003637#endif
3638
3639#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003640 // Gnome gives extra messages for --help if we continue, but not for -h.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003641 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003642 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003643 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003644 gui.dofork = FALSE;
3645 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003646 else
3647#endif
3648 mch_exit(0);
3649}
3650
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003651/*
3652 * Check the result of the ATTENTION dialog:
3653 * When "Quit" selected, exit Vim.
3654 * When "Recover" selected, recover the file.
3655 */
3656 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003657check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003658{
3659 if (swap_exists_action == SEA_QUIT)
3660 getout(1);
3661 handle_swap_exists(NULL);
3662}
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003663
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003664#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003665
Bram Moolenaar595297d2017-03-04 19:11:12 +01003666#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003667 static void
3668set_progpath(char_u *argv0)
3669{
3670 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003671
Bram Moolenaar4f974752019-02-17 17:44:42 +01003672# ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003673 // A relative path containing a "/" will become invalid when using ":cd",
3674 // turn it into a full path.
3675 // On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3676 // this.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003677 char_u *path = NULL;
3678
3679 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3680 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003681# else
3682 char_u buf[MAXPATHL + 1];
3683# ifdef PROC_EXE_LINK
3684 char linkbuf[MAXPATHL + 1];
3685 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003686
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003687 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3688 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003689 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003690 linkbuf[len] = NUL;
3691 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003692 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003693# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003694
3695 if (!mch_isFullName(val))
3696 {
3697 if (gettail(val) != val
3698 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3699 val = buf;
3700 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003701# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003702
Bram Moolenaar08cab962017-03-04 14:37:18 +01003703 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003704
Bram Moolenaar4f974752019-02-17 17:44:42 +01003705# ifdef MSWIN
Bram Moolenaar43663192017-03-05 14:29:12 +01003706 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003707# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003708}
3709
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003710#endif // NO_VIM_MAIN