blob: 16a47b33332cd9061137b4e36e6b45b7db682e59 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
Bram Moolenaarb4210b32004-06-13 14:51:16 +000010#define EXTERN
11#include "vim.h"
12
Bram Moolenaarb4210b32004-06-13 14:51:16 +000013#ifdef __CYGWIN__
K.Takata972db232022-02-04 10:45:38 +000014# include <cygwin/version.h>
15# include <sys/cygwin.h> // for cygwin_conv_to_posix_path() and/or
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010016 // cygwin_conv_path()
Bram Moolenaarb4210b32004-06-13 14:51:16 +000017# include <limits.h>
18#endif
19
Bram Moolenaarafde13b2019-04-28 19:46:49 +020020#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar97ff9b92016-06-26 20:37:46 +020021# include "iscygpty.h"
22#endif
23
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010024// Values for edit_type.
25#define EDIT_NONE 0 // no edit type yet
26#define EDIT_FILE 1 // file name argument[s] given, use argument list
27#define EDIT_STDIN 2 // read file from stdin
28#define EDIT_TAG 3 // tag name argument given, use tagname
29#define EDIT_QF 4 // start in quickfix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +000030
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010031#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010032static int file_owned(char *fname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +000033#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static void mainerr(int, char_u *);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020035static void early_arg_scan(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010036#ifndef NO_VIM_MAIN
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010037static void usage(void);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void parse_command_name(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static void command_line_scan(mparm_T *parmp);
40static void check_tty(mparm_T *parmp);
41static void read_stdin(void);
42static void create_windows(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010043static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010044static void exe_pre_commands(mparm_T *parmp);
45static void exe_commands(mparm_T *parmp);
46static void source_startup_scripts(mparm_T *parmp);
47static void main_start_gui(void);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010048static void check_swap_exists_action(void);
Bram Moolenaar08cab962017-03-04 14:37:18 +010049# ifdef FEAT_EVAL
50static void set_progpath(char_u *argv0);
51# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000052#endif
53
54
Bram Moolenaarb4210b32004-06-13 14:51:16 +000055/*
56 * Different types of error messages.
57 */
58static char *(main_errors[]) =
59{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000060 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000061#define ME_UNKNOWN_OPTION 0
62 N_("Too many edit arguments"),
63#define ME_TOO_MANY_ARGS 1
64 N_("Argument missing after"),
65#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +000066 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000067#define ME_GARBAGE 3
68 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
69#define ME_EXTRA_CMD 4
70 N_("Invalid argument for"),
71#define ME_INVALID_ARG 5
72};
73
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010074#ifndef PROTO // don't want a prototype for main()
Bram Moolenaara6044292017-04-02 18:19:53 +020075
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010076// Various parameters passed between main() and other functions.
Bram Moolenaara6044292017-04-02 18:19:53 +020077static mparm_T params;
78
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010079#ifndef NO_VIM_MAIN // skip this for unittests
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020080
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010081static char_u *start_dir = NULL; // current working dir on startup
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020082
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +020083static int has_dash_c_arg = FALSE;
84
Bram Moolenaarafde13b2019-04-28 19:46:49 +020085# ifdef VIMDLL
86__declspec(dllexport)
87# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000088 int
Bram Moolenaar796cc422019-04-03 20:31:00 +020089# ifdef MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +000090VimMain
91# else
92main
93# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +010094(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +000095{
Bram Moolenaar77780b62018-03-01 23:10:45 +010096#if defined(STARTUPTIME) || defined(CLEAN_RUNTIMEPATH)
Bram Moolenaar3f269672009-11-03 11:11:11 +000097 int i;
98#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000099
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000100 /*
101 * Do any system-specific initialisations. These can NOT use IObuff or
102 * NameBuff. Thus emsg2() cannot be called!
103 */
104 mch_early_init();
105
Bram Moolenaar4f974752019-02-17 17:44:42 +0100106#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +0200107 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200108 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200109 * convert when 'encoding' changes. Get the unexpanded arguments.
110 */
111 argc = get_cmd_argsW(&argv);
112#endif
113
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100114 // Many variables are in "params" so that we can pass them to invoked
115 // functions without a lot of arguments. "argc" and "argv" are also
116 // copied, so that they can be changed.
Bram Moolenaara80faa82020-04-12 19:37:17 +0200117 CLEAR_FIELD(params);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000118 params.argc = argc;
119 params.argv = argv;
120 params.want_full_screen = TRUE;
121#ifdef FEAT_EVAL
122 params.use_debug_break_level = -1;
123#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000124 params.window_count = -1;
Bram Moolenaar58d98232005-07-23 22:25:46 +0000125
Bram Moolenaar99685e62013-05-11 13:56:18 +0200126#ifdef FEAT_RUBY
127 {
128 int ruby_stack_start;
129 vim_ruby_init((void *)&ruby_stack_start);
130 }
131#endif
132
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000133#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000134 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000135#endif
136
137#ifdef MEM_PROFILE
138 atexit(vim_mem_profile_dump);
139#endif
140
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100141#if defined(STARTUPTIME) || defined(FEAT_JOB_CHANNEL)
142 // Need to find "--startuptime" and "--log" before actually parsing
143 // arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100144 for (i = 1; i < argc - 1; ++i)
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100145 {
146# ifdef STARTUPTIME
147 if (STRICMP(argv[i], "--startuptime") == 0 && time_fd == NULL)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000148 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000149 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000150 TIME_MSG("--- VIM STARTING ---");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000151 }
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100152# endif
153# ifdef FEAT_JOB_CHANNEL
154 if (STRICMP(argv[i], "--log") == 0)
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100155 ch_logfile((char_u *)(argv[i + 1]), (char_u *)"ao");
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100156# endif
157 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000158#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000159 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000160
Bram Moolenaar07268702018-03-01 21:57:32 +0100161#ifdef CLEAN_RUNTIMEPATH
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100162 // Need to find "--clean" before actually parsing arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100163 for (i = 1; i < argc; ++i)
164 if (STRICMP(argv[i], "--clean") == 0)
165 {
166 params.clean = TRUE;
167 break;
168 }
169#endif
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200170#ifdef MSWIN
Bram Moolenaar4a228972021-05-01 22:41:39 +0200171 // Need to find "-register" and "-unregister" before loading any libraries.
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200172 for (i = 1; i < argc; ++i)
Bram Moolenaar4a228972021-05-01 22:41:39 +0200173 if ((STRICMP(argv[i] + 1, "register") == 0
174 || STRICMP(argv[i] + 1, "unregister") == 0)
175 && (argv[i][0] == '-' || argv[i][0] == '/'))
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200176 {
177 found_register_arg = TRUE;
178 break;
179 }
180#endif
181
182 /*
183 * Various initialisations shared with tests.
184 */
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200185 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000186
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200187#ifdef VIMDLL
188 // Check if the current executable file is for the GUI subsystem.
189 gui.starting = mch_is_gui_executable();
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +0200190#elif defined(FEAT_GUI_MSWIN)
191 gui.starting = TRUE;
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200192#endif
193
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000194#ifdef FEAT_CLIENTSERVER
195 /*
196 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000197 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000198 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000199 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000200#endif
201
202 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000203 * Figure out the way to work from the command name argv[0].
204 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000205 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000206 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000207
208 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000209 * Process the command line arguments. File names are put in the global
210 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000211 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000212 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000213 TIME_MSG("parsing arguments");
214
215 /*
216 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000217 * there is no terminal version, and on Windows we can't fork one off with
218 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000219 */
220#ifdef ALWAYS_USE_GUI
221 gui.starting = TRUE;
222#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000223# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000224 /*
225 * Check if the GUI can be started. Reset gui.starting if not.
226 * Don't know about other systems, stay on the safe side and don't check.
227 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000228 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000229 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000230 if (gui_init_check() == FAIL)
231 {
232 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000233
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100234 // When running "evim" or "gvim -y" we need the menus, exit if we
235 // don't have them.
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000236 if (params.evim_mode)
237 mch_exit(1);
238 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000239 }
240# endif
241#endif
242
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000243 if (GARGCOUNT > 0)
244 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100245#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000246 /*
247 * Expand wildcards in file names.
248 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000249 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000250 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200251 start_dir = alloc(MAXPATHL);
252 if (start_dir != NULL)
253 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100254 // Temporarily add '(' and ')' to 'isfname'. These are valid
255 // filename characters but are excluded from 'isfname' to make
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000256 // "gf" work on a file name in parentheses (e.g.: see vim.h).
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000257 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000258 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000259 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200260 if (start_dir != NULL)
261 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000262 }
263#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200264 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000265 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000266
Bram Moolenaar4f974752019-02-17 17:44:42 +0100267#ifdef MSWIN
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000268 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100269 // Remember the number of entries in the argument list. If it changes
270 // we don't react on setting 'encoding'.
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000271 set_alist_count();
272 }
273#endif
274
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000275#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000276 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000277 {
278 /*
279 * If there is one filename, fully qualified, we have very probably
280 * been invoked from explorer, so change to the file's directory.
281 * Hint: to avoid this when typing a command use a forward slash.
282 * If the cd fails, it doesn't matter.
283 */
Bram Moolenaar05268152021-11-18 18:53:45 +0000284 if (vim_chdirfile(params.fname, "drop") == OK)
285 last_chdir_reason = "drop";
Bram Moolenaarf6303872015-04-03 17:59:43 +0200286 if (start_dir != NULL)
287 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000288 }
289#endif
290 TIME_MSG("expanding arguments");
291
292#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000293 if (params.diff_mode && params.window_count == -1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100294 params.window_count = 0; // open up to 3 windows
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000295#endif
296
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100297 // Don't redraw until much later.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000298 ++RedrawingDisabled;
299
300 /*
301 * When listing swap file names, don't do cursor positioning et. al.
302 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200303 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000304 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000305
306 /*
307 * When certain to start the GUI, don't check capabilities of terminal.
308 * For GTK we can't be sure, but when started from the desktop it doesn't
309 * make sense to try using a terminal.
310 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200311#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
312 || defined(VIMDLL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000313 if (gui.starting
314# ifdef FEAT_GUI_GTK
315 && !isatty(2)
316# endif
317 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000318 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000319#endif
320
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000321 /*
322 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100323 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000324 * Note that we may use mch_exit() before mch_init()!
325 */
326 mch_init();
327 TIME_MSG("shell init");
328
329#ifdef USE_XSMP
330 /*
331 * For want of anywhere else to do it, try to connect to xsmp here.
332 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
333 * Hijacking -X 'no X connection' to also disable XSMP connection as that
334 * has a similar delay upon failure.
335 * Only try if SESSION_MANAGER is set to something non-null.
336 */
337 if (!x_no_connect)
338 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000339 char *p = getenv("SESSION_MANAGER");
340
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000341 if (p != NULL && *p != NUL)
342 {
343 xsmp_init();
344 TIME_MSG("xsmp init");
345 }
346 }
347#endif
348
349 /*
350 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000351 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000352 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000353
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100354#ifdef _IOLBF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100355 // Ensure output works usefully without a tty: buffer lines instead of
356 // fully buffered.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100357 if (silent_mode)
358 setvbuf(stdout, NULL, _IOLBF, 0);
359#endif
360
Bram Moolenaar9404a182019-05-03 22:25:40 +0200361 // This message comes before term inits, but after setting "silent_mode"
362 // when the input is not a tty. Omit the message with --not-a-term.
363 if (GARGCOUNT > 1 && !silent_mode && !is_not_a_term())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000364 printf(_("%d files to edit\n"), GARGCOUNT);
365
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000366 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000367 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100368 termcapinit(params.term); // set terminal name and get terminal
369 // capabilities (will set full_screen)
370 screen_start(); // don't know where cursor is now
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000371 TIME_MSG("Termcap init");
372 }
373
374 /*
375 * Set the default values for the options that use Rows and Columns.
376 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100377 ui_get_shellsize(); // inits Rows and Columns
Luuk van Baal470a1412022-09-14 01:27:23 +0100378 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000379#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100380 // Set the 'diff' option now, so that it can be checked for in a .vimrc
381 // file. There is no buffer yet though.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000382 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000383 diff_win_options(firstwin, FALSE);
384#endif
385
386 cmdline_row = Rows - p_ch;
387 msg_row = cmdline_row;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100388 screenalloc(FALSE); // allocate screen buffers
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000389 set_init_2();
390 TIME_MSG("inits 2");
391
392 msg_scroll = TRUE;
393 no_wait_return = TRUE;
394
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100395 init_mappings(); // set up initial mappings
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000396
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100397 init_highlight(TRUE, FALSE); // set the default highlight groups
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000398 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000399
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200400#if defined(FEAT_TERMRESPONSE)
401 init_term_props(TRUE);
402#endif
403
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000404#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100405 // Set the break level after the terminal is initialized.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000406 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000407#endif
408
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100409 // Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
410 // Allows for setting 'loadplugins' there.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200411 if (params.use_vimrc != NULL
412 && (STRCMP(params.use_vimrc, "NONE") == 0
413 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
414 p_lpl = FALSE;
415
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100416 // Execute --cmd arguments.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200417 exe_pre_commands(&params);
418
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100419 // Source startup scripts.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200420 source_startup_scripts(&params);
421
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100422#ifdef FEAT_MZSCHEME
423 /*
424 * Newer version of MzScheme (Racket) require earlier (trampolined)
425 * initialisation via scheme_main_setup.
426 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200427 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200428 * Do source startup scripts, so that 'mzschemedll' can be set.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100429 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200430 return mzscheme_main();
431#else
432 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100433#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200434}
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100435#endif // NO_VIM_MAIN
436#endif // PROTO
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100437
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200438/*
439 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
440 * things simple.
441 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
442 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100443 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200444vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100445{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100446#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000447#ifdef FEAT_EVAL
448 /*
449 * Read all the plugin files.
450 * Only when compiled with +eval, since most plugins need it.
451 */
452 if (p_lpl)
453 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200454 char_u *rtp_copy = NULL;
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100455 char_u *plugin_pattern = (char_u *)
456# if defined(VMS) || defined(AMIGA) // VMS and Amiga don't handle the "**".
457 "plugin/*.vim"
458# else
459 "plugin/**/*.vim"
460# endif
461 ;
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200462
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100463 // First add all package directories to 'runtimepath', so that their
464 // autoload directories can be found. Only if not done already with a
465 // :packloadall command.
466 // Make a copy of 'runtimepath', so that source_runtime does not use
467 // the pack directories.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200468 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200469 {
470 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200471 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200472 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200473
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100474 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy, plugin_pattern,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475 DIP_ALL | DIP_NOAFTER, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000476 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200477 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100478
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100479 // Only source "start" packages if not done already with a :packloadall
480 // command.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200481 if (!did_source_packages)
482 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100483 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200484
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100485 source_runtime(plugin_pattern, DIP_ALL | DIP_AFTER);
Bram Moolenaar66459b72016-08-06 19:01:55 +0200486 TIME_MSG("loading after plugins");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000487 }
488#endif
489
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000490#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100491 // Decide about window layout for diff mode after reading vimrc.
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000492 if (params.diff_mode && params.window_layout == 0)
493 {
494 if (diffopt_horizontal())
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100495 params.window_layout = WIN_HOR; // use horizontal split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000496 else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100497 params.window_layout = WIN_VER; // use vertical split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000498 }
499#endif
500
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000501 /*
502 * Recovery mode without a file name: List swap files.
503 * This uses the 'dir' option, therefore it must be after the
504 * initializations.
505 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200506 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000507 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200508 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000509 mch_exit(0);
510 }
511
512 /*
513 * Set a few option defaults after reading .vimrc files:
514 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
515 */
516 set_init_3();
517 TIME_MSG("inits 3");
518
519 /*
520 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
521 * Note that this overrides anything from a vimrc file.
522 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000523 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000524 p_uc = 0;
525
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000526#ifdef FEAT_GUI
527 if (gui.starting)
528 {
Bram Moolenaarc7226682019-08-17 16:33:23 +0200529# if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100530 // When something caused a message from a vimrc script, need to output
531 // an extra newline before the shell prompt.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000532 if (did_emsg || msg_didout)
533 putchar('\n');
Bram Moolenaarc7226682019-08-17 16:33:23 +0200534# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000535
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100536 gui_start(NULL); // will set full_screen to TRUE
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000537 TIME_MSG("starting GUI");
538
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100539 // When running "evim" or "gvim -y" we need the menus, exit if we
540 // don't have them.
Bram Moolenaar58d98232005-07-23 22:25:46 +0000541 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000542 mch_exit(1);
Luuk van Baal470a1412022-09-14 01:27:23 +0100543 firstwin->w_prev_height = firstwin->w_height; // may have changed
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000544 }
545#endif
546
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000547#ifdef FEAT_VIMINFO
548 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000549 * Read in registers, history etc, but not marks, from the viminfo file.
550 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000551 */
552 if (*p_viminfo != NUL)
553 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000554 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000555 TIME_MSG("reading viminfo");
556 }
557#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100558#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100559 // It's better to make v:oldfiles an empty list than NULL.
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100560 if (get_vim_var_list(VV_OLDFILES) == NULL)
561 set_vim_var_list(VV_OLDFILES, list_alloc());
562#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000563
564#ifdef FEAT_QUICKFIX
565 /*
566 * "-q errorfile": Load the error file now.
567 * If the error file can't be read, exit before doing anything else.
568 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000569 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000570 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100571 char_u *enc = NULL;
572
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100573 enc = p_menc;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000574 if (params.use_ef != NULL)
575 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000576 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200577 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100578 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000579 {
580 out_char('\n');
581 mch_exit(3);
582 }
583 TIME_MSG("reading errorfile");
584 }
585#endif
586
587 /*
588 * Start putting things on the screen.
589 * Scroll screen down before drawing over it
590 * Clear screen now, so file message will not be cleared.
591 */
592 starting = NO_BUFFERS;
593 no_wait_return = FALSE;
594 if (!exmode_active)
595 msg_scroll = FALSE;
596
597#ifdef FEAT_GUI
598 /*
599 * This seems to be required to make callbacks to be called now, instead
600 * of after things have been put on the screen, which then may be deleted
601 * when getting a resize callback.
602 * For the Mac this handles putting files dropped on the Vim icon to
603 * global_alist.
604 */
605 if (gui.in_use)
606 {
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100607 gui_wait_for_chars(50L, typebuf.tb_change_cnt);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000608 TIME_MSG("GUI delay");
609 }
610#endif
611
612#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
613 qnx_clip_init();
614#endif
615
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200616#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
617 clip_init(TRUE);
618#endif
619
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000620#ifdef FEAT_XCLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100621 // Start using the X clipboard, unless the GUI was started.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000622# ifdef FEAT_GUI
623 if (!gui.in_use)
624# endif
625 {
626 setup_term_clip();
627 TIME_MSG("setup clipboard");
628 }
629#endif
630
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000631#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100632 // Prepare for being a Vim server.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000633 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000634#endif
635
636 /*
637 * If "-" argument given: Read file from stdin.
638 * Do this before starting Raw mode, because it may change things that the
639 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
640 * are the same terminal: "cat | vim -".
641 * Using autocommands here may cause trouble...
642 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000643 if (params.edit_type == EDIT_STDIN && !recoverymode)
644 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000645
646#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100647 // When switching screens and something caused a message from a vimrc
648 // script, need to output an extra newline on exit.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000649 if ((did_emsg || msg_didout) && *T_TI != NUL)
650 newline_on_exit = TRUE;
651#endif
652
653 /*
K.Takataeeec2542021-06-02 13:28:16 +0200654 * When done something that is not allowed or given an error message call
Bram Moolenaar13608d82022-08-29 15:06:50 +0100655 * wait_return(). This must be done before starttermcap(), because it may
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000656 * switch to another screen. It must be done after settmode(TMODE_RAW),
657 * because we want to react on a single key stroke.
658 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000659 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000660 */
661 settmode(TMODE_RAW);
662 TIME_MSG("setting raw mode");
663
664 if (need_wait_return || msg_didany)
665 {
666 wait_return(TRUE);
667 TIME_MSG("waiting for return");
668 }
669
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100670 starttermcap(); // start termcap if not done by wait_return()
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000671 TIME_MSG("start termcap");
672
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200673 setmouse(); // may start using the mouse
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000674 if (scroll_region)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100675 scroll_region_reset(); // In case Rows changed
676 scroll_start(); // may scroll the screen to the right position
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000677
Bram Moolenaar651fca82021-11-29 20:39:38 +0000678#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar40385db2018-08-07 22:31:44 +0200679 term_push_title(SAVE_RESTORE_BOTH);
680#endif
681
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000682 /*
683 * Don't clear the screen when starting in Ex mode, unless using the GUI.
684 */
685 if (exmode_active
686#ifdef FEAT_GUI
687 && !gui.in_use
688#endif
689 )
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100690 set_must_redraw(UPD_CLEAR);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000691 else
692 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100693 screenclear(); // clear screen
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000694 TIME_MSG("clearing screen");
695 }
696
697#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000698 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000699 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100700 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200701 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000702 TIME_MSG("getting crypt key");
703 }
704#endif
705
706 no_wait_return = TRUE;
707
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000708 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000709 * Create the requested number of windows and edit buffers in them.
710 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000711 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000712 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000713 TIME_MSG("opening buffers");
714
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000715#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100716 // clear v:swapcommand
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000717 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
718#endif
719
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100720 // Ex starts at last line of the file
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000721 if (exmode_active)
722 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
723
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000724 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
725 TIME_MSG("BufEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000726 setpcmark();
727
728#ifdef FEAT_QUICKFIX
729 /*
730 * When started with "-q errorfile" jump to first error now.
731 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000732 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000733 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000734 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000735 TIME_MSG("jump to first error");
736 }
737#endif
738
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000739 /*
740 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000741 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000742 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200743 edit_buffers(&params, start_dir);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200744 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000745
746#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000747 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000748 {
749 win_T *wp;
750
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100751 // set options in each window for "vimdiff".
Bram Moolenaar29323592016-07-24 22:04:11 +0200752 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000753 diff_win_options(wp, TRUE);
754 }
755#endif
756
757 /*
758 * Shorten any of the filenames, but only when absolute.
759 */
760 shorten_fnames(FALSE);
761
762 /*
763 * Need to jump to the tag before executing the '-c command'.
764 * Makes "vim -c '/return' -t main" work.
765 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000766 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000767 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000768 swap_exists_did_quit = FALSE;
Bram Moolenaar146522e2005-12-16 21:55:46 +0000769
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000770 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000771 do_cmdline_cmd(IObuff);
772 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000773
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100774 // If the user doesn't want to edit the file then we quit here.
Bram Moolenaar146522e2005-12-16 21:55:46 +0000775 if (swap_exists_did_quit)
776 getout(1);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000777 }
778
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100779 // Execute any "+", "-c" and "-S" arguments.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000780 if (params.n_commands > 0)
781 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000782
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100783 // Must come before the may_req_ calls.
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200784 starting = 0;
785
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100786#if defined(FEAT_TERMRESPONSE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100787 // Must be done before redrawing, puts a few characters on the screen.
Bram Moolenaara45551a2020-06-09 15:57:37 +0200788 check_terminal_behavior();
Bram Moolenaar976787d2017-06-04 15:45:50 +0200789#endif
790
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000791 RedrawingDisabled = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100792 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000793 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000794
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100795 // 'autochdir' has been postponed
Bram Moolenaar6f470022018-04-10 18:47:20 +0200796 DO_AUTOCHDIR;
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200797
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000798#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100799 // Requesting the termresponse is postponed until here, so that a "-c q"
800 // argument doesn't make it appear in the shell Vim was started from.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000801 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200802
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200803 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000804#endif
805
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100806 // start in insert mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000807 if (p_im)
808 need_start_insertmode = TRUE;
809
Bram Moolenaar14735512016-03-26 21:00:08 +0100810#ifdef FEAT_EVAL
811 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
812#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000813 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
814 TIME_MSG("VimEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000815
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200816#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100817 // Adjust default register name for "unnamed" in 'clipboard'. Can only be
818 // done after the clipboard is available and all initial commands that may
819 // modify the 'clipboard' setting have run; i.e. just before entering the
820 // main loop.
Bram Moolenaar439c0362020-06-06 15:58:03 +0200821 reset_reg_var();
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200822#endif
823
Bram Moolenaar8a3bb562018-03-04 20:14:14 +0100824#if defined(FEAT_DIFF)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100825 // When a startup script or session file setup for diff'ing and
826 // scrollbind, sync the scrollbind now.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000827 if (curwin->w_p_diff && curwin->w_p_scb)
828 {
829 update_topline();
830 check_scrollbind((linenr_T)0, 0L);
831 TIME_MSG("diff scrollbinding");
832 }
833#endif
834
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200835#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
836# ifdef VIMDLL
837 if (!gui.in_use)
838# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100839 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000840#endif
841
Bram Moolenaar4033c552017-09-16 20:54:51 +0200842#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100843 // When tab pages were created, may need to update the tab pages line and
844 // scrollbars. This is skipped while creating them.
h-east6073f132021-12-24 11:57:06 +0000845 if (gui.in_use && first_tabpage->tp_next != NULL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000846 {
847 out_flush();
848 gui_init_which_components(NULL);
849 gui_update_scrollbars(TRUE);
850 }
851 need_mouse_correct = TRUE;
852#endif
853
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100854 // If ":startinsert" command used, stuff a dummy command to be able to
855 // call normal_cmd(), which will then start Insert mode.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000856 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000857 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000858
859#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200860 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200861 {
862# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200863# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar4f974752019-02-17 17:44:42 +0100864 && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200865 if (gui.in_use)
866 {
867 mch_errmsg(_("netbeans is not supported with this GUI\n"));
868 mch_exit(2);
869 }
870# endif
871# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100872 // Tell the client that it can start sending commands.
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200873 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200874 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000875#endif
876
Bram Moolenaare7ddf4e2020-02-03 21:29:30 +0100877 // Redraw at least once, also when 'lazyredraw' is set, to make sure the
878 // window title gets updated.
879 do_redraw = TRUE;
880
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000881 TIME_MSG("before starting main loop");
882
883 /*
884 * Call the main command loop. This never returns.
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200885 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000886 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000887
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100888#endif // NO_VIM_MAIN
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200889
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000890 return 0;
891}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000892
893/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200894 * Initialisation shared by main() and some tests.
895 */
896 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200897common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200898{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100899 estack_init();
Bram Moolenaar438d1762018-09-30 17:11:48 +0200900 cmdline_init();
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200901
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100902 (void)mb_init(); // init mb_bytelen_tab[] to ones
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200903#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100904 eval_init(); // init global variables
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200905#endif
906
907#ifdef __QNXNTO__
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100908 qnx_init(); // PhAttach() for clipboard, (and gui)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200909#endif
910
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200911 /*
912 * Allocate space for the generic buffers (needed for set_init_1() and
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100913 * emsg()).
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200914 */
915 if ((IObuff = alloc(IOSIZE)) == NULL
916 || (NameBuff = alloc(MAXPATHL)) == NULL)
917 mch_exit(0);
918 TIME_MSG("Allocated generic buffers");
919
920#ifdef NBDEBUG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100921 // Wait a moment for debugging NetBeans. Must be after allocating
922 // NameBuff.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200923 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
924 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
925 TIME_MSG("NetBeans debug wait");
926#endif
927
928#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
929 /*
930 * Setup to use the current locale (for ctype() and many other things).
931 * NOTE: Translated messages with encodings other than latin1 will not
932 * work until set_init_1() has been called!
933 */
934 init_locale();
935 TIME_MSG("locale set");
936#endif
937
938#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100939 gui.dofork = TRUE; // default is to use fork()
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200940#endif
941
942 /*
943 * Do a first scan of the arguments in "argv[]":
944 * -display or --display
945 * --server...
946 * --socketid
947 * --windowid
948 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200949 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200950
Bram Moolenaard0573012017-10-28 21:11:06 +0200951#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100952 // Prepare for possibly starting GUI sometime
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200953 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200954 TIME_MSG("GUI prepared");
955#endif
956
957#ifdef FEAT_CLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100958 clip_init(FALSE); // Initialise clipboard stuff
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200959 TIME_MSG("clipboard setup");
960#endif
961
962 /*
963 * Check if we have an interactive window.
964 * On the Amiga: If there is no window, we open one with a newcli command
965 * (needed for :! to * work). mch_check_win() will also handle the -d or
966 * -dev argument.
967 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100968 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200969 TIME_MSG("window checked");
970
971 /*
972 * Allocate the first window and buffer.
973 * Can't do anything without it, exit when it fails.
974 */
975 if (win_alloc_first() == FAIL)
976 mch_exit(0);
977
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100978 init_yank(); // init yank buffers
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200979
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100980 alist_init(&global_alist); // Init the argument list to empty.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200981 global_alist.id = 0;
982
983 /*
984 * Set the default values for the options.
985 * NOTE: Non-latin1 translated messages are working only after this,
986 * because this is where "has_mbyte" will be set, which is used by
987 * msg_outtrans_len_attr().
988 * First find out the home directory, needed to expand "~" in options.
989 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100990 init_homedir(); // find real value of $HOME
Bram Moolenaar07268702018-03-01 21:57:32 +0100991 set_init_1(paramp->clean);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200992 TIME_MSG("inits 1");
993
994#ifdef FEAT_EVAL
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100995 // set v:lang and v:ctype
996 set_lang_var();
997
998 // set v:argv
999 set_argv_var(paramp->argv, paramp->argc);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001000#endif
Bram Moolenaar6436cd82018-12-27 00:28:33 +01001001
1002#ifdef FEAT_SIGNS
1003 init_signs();
1004#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001005}
1006
1007/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001008 * Return TRUE when the --not-a-term argument was found.
1009 */
1010 int
Bram Moolenaarf7e73022022-09-24 13:10:04 +01001011is_not_a_term(void)
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001012{
1013 return params.not_a_term;
1014}
1015
Bram Moolenaar7007e312021-03-27 12:11:33 +01001016/*
1017 * Return TRUE when the --not-a-term argument was found or the GUI is in use.
1018 */
Bram Moolenaarf7e73022022-09-24 13:10:04 +01001019 int
1020is_not_a_term_or_gui(void)
Bram Moolenaar7007e312021-03-27 12:11:33 +01001021{
1022 return params.not_a_term
1023#ifdef FEAT_GUI
1024 || gui.in_use
1025#endif
1026 ;
1027}
1028
Bram Moolenaar2d12c252022-06-13 21:42:45 +01001029#if defined(FEAT_GUI) || defined(PROTO)
1030/*
1031 * If a --gui-dialog-file argument was given return the file name.
1032 * Otherwise return NULL.
1033 */
1034 char_u *
1035get_gui_dialog_file(void)
1036{
1037 return params.gui_dialog_file;
1038}
1039#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001040
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001041// When TRUE in a safe state when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001042static int was_safe = FALSE;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001043static oparg_T *current_oap = NULL;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001044
1045/*
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001046 * Return TRUE if an operator was started but not finished yet.
1047 * Includes typing a count or a register name.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001048 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001049 int
1050op_pending(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001051{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001052 return !(current_oap != NULL
1053 && !finish_op
1054 && current_oap->prev_opcount == 0
1055 && current_oap->prev_count0 == 0
1056 && current_oap->op_type == OP_NOP
1057 && current_oap->regname == NUL);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001058}
1059
1060/*
Bram Moolenaard103ee72019-09-18 21:15:31 +02001061 * Return whether currently it is safe, assuming it was safe before (high level
1062 * state didn't change).
1063 */
1064 static int
1065is_safe_now(void)
1066{
1067 return stuff_empty()
1068 && typebuf.tb_len == 0
1069 && scriptin[curscript] == NULL
Bram Moolenaar4fa13462022-02-05 12:39:24 +00001070#ifdef FEAT_EVAL
1071 && !debug_mode
1072#endif
1073 && !global_busy;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001074}
1075
1076/*
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001077 * Trigger SafeState if currently in s safe state, that is "safe" is TRUE and
1078 * there is no typeahead.
1079 */
1080 void
1081may_trigger_safestate(int safe)
1082{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001083 int is_safe = safe && is_safe_now();
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001084
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001085#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001086 if (was_safe != is_safe)
1087 // Only log when the state changes, otherwise it happens at nearly
1088 // every key stroke.
Bram Moolenaard103ee72019-09-18 21:15:31 +02001089 ch_log(NULL, is_safe ? "SafeState: Start triggering"
1090 : "SafeState: Stop triggering");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001091#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001092 if (is_safe)
1093 apply_autocmds(EVENT_SAFESTATE, NULL, NULL, FALSE, curbuf);
1094 was_safe = is_safe;
1095}
1096
1097/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001098 * Something changed which causes the state possibly to be unsafe, e.g. a
1099 * character was typed. It will remain unsafe until the next call to
1100 * may_trigger_safestate().
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001101 */
1102 void
Bram Moolenaard103ee72019-09-18 21:15:31 +02001103state_no_longer_safe(char *reason UNUSED)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001104{
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001105#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001106 if (was_safe)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001107 ch_log(NULL, "SafeState: reset: %s", reason);
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001108#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001109 was_safe = FALSE;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001110}
1111
Dominique Pelle748b3082022-01-08 12:41:16 +00001112#if defined(FEAT_EVAL) || defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001113 int
1114get_was_safe_state(void)
1115{
1116 return was_safe;
1117}
Dominique Pelle748b3082022-01-08 12:41:16 +00001118#endif
Bram Moolenaard103ee72019-09-18 21:15:31 +02001119
Dominique Pelle748b3082022-01-08 12:41:16 +00001120#if defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001121/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001122 * Invoked when leaving code that invokes callbacks. Then trigger
1123 * SafeStateAgain, if it was safe when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001124 */
1125 void
Bram Moolenaar37d18072019-09-17 20:28:38 +02001126may_trigger_safestateagain(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001127{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001128 if (!was_safe)
1129 {
1130 // If the safe state was reset in state_no_longer_safe(), e.g. because
1131 // of calling feedkeys(), we check if it's now safe again (all keys
1132 // were consumed).
1133 was_safe = is_safe_now();
1134#ifdef FEAT_JOB_CHANNEL
1135 if (was_safe)
1136 ch_log(NULL, "SafeState: undo reset");
1137#endif
1138 }
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001139 if (was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02001140 {
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001141#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar101e9922019-09-25 21:43:11 +02001142 // Only do this message when another message was given, otherwise we
1143 // get lots of them.
1144 if ((did_repeated_msg & REPEATED_MSG_SAFESTATE) == 0)
1145 {
1146 int did = did_repeated_msg;
1147
1148 ch_log(NULL,
1149 "SafeState: back to waiting, triggering SafeStateAgain");
1150 did_repeated_msg = did | REPEATED_MSG_SAFESTATE;
1151 }
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001152#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001153 apply_autocmds(EVENT_SAFESTATEAGAIN, NULL, NULL, FALSE, curbuf);
Bram Moolenaar37d18072019-09-17 20:28:38 +02001154 }
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001155#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001156 else
Bram Moolenaard103ee72019-09-18 21:15:31 +02001157 ch_log(NULL,
1158 "SafeState: back to waiting, not triggering SafeStateAgain");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001159#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001160}
Dominique Pelle748b3082022-01-08 12:41:16 +00001161#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001162
1163
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001164/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001165 * Main loop: Execute Normal mode commands until exiting Vim.
1166 * Also used to handle commands in the command-line window, until the window
1167 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001168 * Also used to handle ":visual" command after ":global": execute Normal mode
1169 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001170 */
1171 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001172main_loop(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001173 int cmdwin, // TRUE when working in the command-line window
1174 int noexmode) // TRUE when return on entering Ex mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001175{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001176 oparg_T oa; // operator arguments
1177 oparg_T *prev_oap; // operator arguments
1178 volatile int previous_got_int = FALSE; // "got_int" was TRUE
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001179#ifdef FEAT_CONCEAL
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001180 // these are static to avoid a compiler warning
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001181 static linenr_T conceal_old_cursor_line = 0;
1182 static linenr_T conceal_new_cursor_line = 0;
1183 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001184#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001185
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001186 prev_oap = current_oap;
1187 current_oap = &oa;
1188
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001189#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001190 // Setup to catch a terminating error from the X server. Just ignore
1191 // it, restore the state and continue. This might not always work
1192 // properly, but at least we don't exit unexpectedly when the X server
1193 // exits while Vim is running in a console.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001195 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001196 State = MODE_NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001197 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001198 got_int = TRUE;
1199 need_wait_return = FALSE;
1200 global_busy = FALSE;
1201 exmode_active = 0;
1202 skip_redraw = FALSE;
1203 RedrawingDisabled = 0;
1204 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001205 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001206# ifdef FEAT_EVAL
1207 emsg_skip = 0;
1208# endif
1209 emsg_off = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001210 setmouse();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001211 settmode(TMODE_RAW);
1212 starttermcap();
1213 scroll_start();
1214 redraw_later_clear();
1215 }
1216#endif
1217
1218 clear_oparg(&oa);
Martin Tournoij7904fa42022-10-04 16:28:45 +01001219 while (!cmdwin || cmdwin_result == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001220 {
1221 if (stuff_empty())
1222 {
1223 did_check_timestamps = FALSE;
1224 if (need_check_timestamps)
1225 check_timestamps(FALSE);
Bram Moolenaar13608d82022-08-29 15:06:50 +01001226 if (need_wait_return) // if wait_return() still needed ...
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001227 wait_return(FALSE); // ... call it now
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001228 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001229 {
1230 need_start_insertmode = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001231 stuffReadbuff((char_u *)"i"); // start insert mode next
1232 // skip the fileinfo message now, because it would be shown
1233 // after insert mode finishes!
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001234 need_fileinfo = FALSE;
1235 }
1236 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001237
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001238 // Reset "got_int" now that we got back to the main loop. Except when
1239 // inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1240 // the ":g" command.
1241 // For ":g/pat/vi" we reset "got_int" when used once. When used
1242 // a second time we go back to Ex mode and abort the ":g" command.
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001243 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001244 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001245 if (noexmode && global_busy && !exmode_active && previous_got_int)
1246 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001247 // Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1248 // used and keep "got_int" set, so that it aborts ":g".
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001249 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +01001250 State = MODE_NORMAL;
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001251 }
1252 else if (!global_busy || !exmode_active)
1253 {
1254 if (!quit_more)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001255 (void)vgetc(); // flush all buffers
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001256 got_int = FALSE;
1257 }
1258 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001259 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001260 else
1261 previous_got_int = FALSE;
1262
Bram Moolenaar069613c2022-01-15 15:23:44 +00001263#ifdef FEAT_EVAL
1264 // At the toplevel there is no exception handling. Discard any that
1265 // may be hanging around (e.g. from "interrupt" at the debug prompt).
1266 if (did_throw && !ex_normal_busy)
1267 discard_current_exception();
1268#endif
1269
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001270 if (!exmode_active)
1271 msg_scroll = FALSE;
1272 quit_more = FALSE;
1273
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001274 // it's not safe unless may_trigger_safestate_main() is called
1275 was_safe = FALSE;
1276
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001277 /*
1278 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1279 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1280 * update cursor and redraw.
1281 */
1282 if (skip_redraw || exmode_active)
1283 skip_redraw = FALSE;
1284 else if (do_redraw || stuff_empty())
1285 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001286#ifdef FEAT_GUI
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001287 // If ui_breakcheck() was used a resize may have been postponed.
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001288 gui_may_resize_shell();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001289#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001290#ifdef HAVE_DROP_FILE
1291 // If files were dropped while text was locked or the curbuf was
1292 // locked, this would be a good time to handle the drop.
1293 handle_any_postponed_drop();
1294#endif
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001295#ifdef FEAT_CONCEAL
1296 if (curwin->w_p_cole == 0)
1297 conceal_update_lines = FALSE;
1298#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001299
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001300 // Trigger CursorMoved if the cursor moved.
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001301 if (!finish_op && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001302 has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001303#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001304 || popup_visible
1305#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001306#ifdef FEAT_CONCEAL
1307 || curwin->w_p_cole > 0
1308#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001309 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001310 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001311 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001312 if (has_cursormoved())
1313 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1314 FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001315#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001316 if (popup_visible)
1317 popup_check_cursor_pos();
1318#endif
1319#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001320 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001321 {
1322 conceal_old_cursor_line = last_cursormoved.lnum;
1323 conceal_new_cursor_line = curwin->w_cursor.lnum;
1324 conceal_update_lines = TRUE;
1325 }
Bram Moolenaar3397f742019-06-02 18:40:06 +02001326#endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001327 last_cursormoved = curwin->w_cursor;
1328 }
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001329
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001330#if defined(FEAT_CONCEAL)
1331 if (conceal_update_lines
1332 && (conceal_old_cursor_line != conceal_new_cursor_line
1333 || conceal_cursor_line(curwin)
1334 || need_cursor_line_redraw))
1335 {
1336 if (conceal_old_cursor_line != conceal_new_cursor_line
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001337 && conceal_old_cursor_line != 0
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001338 && conceal_old_cursor_line
1339 <= curbuf->b_ml.ml_line_count)
1340 redrawWinline(curwin, conceal_old_cursor_line);
1341 redrawWinline(curwin, conceal_new_cursor_line);
1342 curwin->w_valid &= ~VALID_CROW;
1343 need_cursor_line_redraw = FALSE;
1344 }
1345#endif
1346
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001347 // Trigger TextChanged if b:changedtick differs.
Bram Moolenaar186628f2013-03-19 13:33:23 +01001348 if (!finish_op && has_textchanged()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001349 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001350 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001351 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL, FALSE, curbuf);
1352 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001353 }
Bram Moolenaar186628f2013-03-19 13:33:23 +01001354
LemonBoy09371822022-04-08 15:18:45 +01001355 // Ensure curwin->w_topline and curwin->w_leftcol are up to date
1356 // before triggering a WinScrolled autocommand.
1357 update_topline();
1358 validate_cursor();
1359
1360 if (!finish_op)
zeertzjqd58862d2022-04-12 11:32:48 +01001361 may_trigger_winscrolled();
LemonBoy09371822022-04-08 15:18:45 +01001362
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001363 // If nothing is pending and we are going to wait for the user to
1364 // type a character, trigger SafeState.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001365 may_trigger_safestate(!op_pending() && restart_edit == 0);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001366
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01001367#if defined(FEAT_DIFF)
Bram Moolenaare3521d92018-09-16 14:10:31 +02001368 // Updating diffs from changed() does not always work properly,
1369 // esp. updating folds. Do an update just before redrawing if
1370 // needed.
1371 if (curtab->tp_diff_update || curtab->tp_diff_invalid)
1372 {
1373 ex_diffupdate(NULL);
1374 curtab->tp_diff_update = FALSE;
1375 }
1376
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001377 // Scroll-binding for diff mode may have been postponed until
1378 // here. Avoids doing it for every change.
Bram Moolenaar33aec762006-01-22 23:30:12 +00001379 if (diff_need_scrollbind)
1380 {
1381 check_scrollbind((linenr_T)0, 0L);
1382 diff_need_scrollbind = FALSE;
1383 }
1384#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001385#if defined(FEAT_FOLDING)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001386 // Include a closed fold completely in the Visual area.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001387 foldAdjustVisual();
1388#endif
1389#ifdef FEAT_FOLDING
1390 /*
1391 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1392 * contain the cursor.
1393 * When 'foldopen' is "all", open the fold(s) under the cursor.
1394 * This may mark the window for redrawing.
1395 */
1396 if (hasAnyFolding(curwin) && !char_avail())
1397 {
1398 foldCheckClose();
1399 if (fdo_flags & FDO_ALL)
1400 foldOpenCursor();
1401 }
1402#endif
1403
1404 /*
1405 * Before redrawing, make sure w_topline is correct, and w_leftcol
1406 * if lines don't wrap, and w_skipcol if lines wrap.
1407 */
1408 update_topline();
1409 validate_cursor();
1410
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001411 if (VIsual_active)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001412 update_curbuf(UPD_INVERTED); // update inverted part
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001413 else if (must_redraw)
Bram Moolenaara338adc2018-01-31 20:51:47 +01001414 {
Bram Moolenaar017ba072019-09-14 21:01:23 +02001415 mch_disable_flush(); // Stop issuing gui_mch_flush().
Bram Moolenaar11a58af2019-10-24 22:32:31 +02001416 update_screen(0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01001417 mch_enable_flush();
1418 }
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01001419 else if (redraw_cmdline || clear_cmdline || redraw_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001420 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001421 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001422 if (need_maketitle)
1423 maketitle();
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001424#ifdef FEAT_VIMINFO
1425 curbuf->b_last_used = vim_time();
1426#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001427 // display message after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001428 if (keep_msg != NULL)
1429 {
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001430 char_u *p = vim_strsave(keep_msg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001431
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001432 if (p != NULL)
1433 {
1434 // msg_start() will set keep_msg to NULL, make a copy
1435 // first. Don't reset keep_msg, msg_attr_keep() uses it to
1436 // check for duplicates. Never put this message in
1437 // history.
1438 msg_hist_off = TRUE;
1439 msg_attr((char *)p, keep_msg_attr);
1440 msg_hist_off = FALSE;
1441 vim_free(p);
1442 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001443 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001444 if (need_fileinfo) // show file info after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001445 {
1446 fileinfo(FALSE, TRUE, FALSE);
1447 need_fileinfo = FALSE;
1448 }
1449
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001450 emsg_on_display = FALSE; // can delete error message now
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001451 did_emsg = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001452 msg_didany = FALSE; // reset lines_left in msg_start()
1453 may_clear_sb_text(); // clear scroll-back text on next msg
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001454 showruler(FALSE);
1455
1456 setcursor();
1457 cursor_on();
1458
1459 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001460
1461#ifdef STARTUPTIME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001462 // Now that we have drawn the first screen all the startup stuff
1463 // has been done, close any file for startup messages.
Bram Moolenaar3f269672009-11-03 11:11:11 +00001464 if (time_fd != NULL)
1465 {
1466 TIME_MSG("first screen update");
1467 TIME_MSG("--- VIM STARTED ---");
1468 fclose(time_fd);
1469 time_fd = NULL;
1470 }
1471#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001472 }
1473#ifdef FEAT_GUI
1474 if (need_mouse_correct)
1475 gui_mouse_correct();
1476#endif
1477
1478 /*
1479 * Update w_curswant if w_set_curswant has been set.
1480 * Postponed until here to avoid computing w_virtcol too often.
1481 */
1482 update_curswant();
1483
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001484#ifdef FEAT_EVAL
1485 /*
1486 * May perform garbage collection when waiting for a character, but
1487 * only at the very toplevel. Otherwise we may be using a List or
1488 * Dict internally somewhere.
1489 * "may_garbage_collect" is reset in vgetc() which is invoked through
1490 * do_exmode() and normal_cmd().
1491 */
1492 may_garbage_collect = (!cmdwin && !noexmode);
1493#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001494 /*
1495 * If we're invoked as ex, do a round of ex commands.
1496 * Otherwise, get and execute a normal mode command.
1497 */
1498 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001499 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001500 if (noexmode) // End of ":global/path/visual" commands
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001501 goto theend;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001502 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001503 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001504 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001505 {
1506#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001507 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001508 && oa.op_type == OP_NOP && oa.regname == NUL
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001509 && !VIsual_active
1510 && !skip_term_loop)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001511 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001512 // If terminal_loop() returns OK we got a key that is handled
1513 // in Normal model. With FAIL we first need to position the
1514 // cursor and the screen needs to be redrawn.
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001515 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001516 normal_cmd(&oa, TRUE);
1517 }
1518 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001519#endif
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001520 {
1521#ifdef FEAT_TERMINAL
1522 skip_term_loop = FALSE;
1523#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001524 normal_cmd(&oa, TRUE);
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001525 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02001526 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001527 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001528
1529theend:
1530 current_oap = prev_oap;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001531}
1532
1533
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001534#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001535/*
1536 * Exit, but leave behind swap files for modified buffers.
1537 */
1538 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001539getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001540{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001541# if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001542 // Ignore SIGHUP, because a dropped connection causes a read error, which
1543 // makes Vim exit and then handling SIGHUP causes various reentrance
1544 // problems.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001545 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001546# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001547
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001548 ml_close_notmod(); // close all not-modified buffers
1549 ml_sync_all(FALSE, FALSE); // preserve all swap files
1550 ml_close_all(FALSE); // close all memfiles, without deleting
1551 getout(exitval); // exit Vim properly
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001552}
1553#endif
1554
1555
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001556/*
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001557 * Exit properly. This is the only way to exit Vim after startup has
1558 * succeeded. We are certain to exit here, no way to abort it.
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001559 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001560 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001561getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001562{
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001563 exiting = TRUE;
Bram Moolenaar0bd052b2018-03-22 20:33:56 +01001564#if defined(FEAT_JOB_CHANNEL)
1565 ch_log(NULL, "Exiting...");
1566#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001567
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001568 // When running in Ex mode an error causes us to exit with a non-zero exit
1569 // code. POSIX requires this, although it's not 100% clear from the
1570 // standard.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001571 if (exmode_active)
1572 exitval += ex_exitval;
1573
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001574#ifdef FEAT_EVAL
1575 set_vim_var_type(VV_EXITING, VAR_NUMBER);
1576 set_vim_var_nr(VV_EXITING, exitval);
1577#endif
1578
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001579 // Position the cursor on the last screen line, below all the text
Bram Moolenaar7007e312021-03-27 12:11:33 +01001580 if (!is_not_a_term_or_gui())
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001581 windgoto((int)Rows - 1, 0);
1582
Bram Moolenaar58779852022-09-06 18:31:14 +01001583#ifdef FEAT_EVAL
1584 // Invoked all deferred functions in the function stack.
1585 invoke_all_defer();
1586#endif
1587
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001588#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001589 // Optionally print hashtable efficiency.
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001590 hash_debug_results();
1591#endif
1592
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001593#ifdef FEAT_GUI
1594 msg_didany = FALSE;
1595#endif
1596
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001597 if (v_dying <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001598 {
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001599 tabpage_T *tp;
1600 tabpage_T *next_tp;
1601 buf_T *buf;
1602 win_T *wp;
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001603 int unblock = 0;
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001604
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001605 // Trigger BufWinLeave for all windows, but only once per buffer.
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001606 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001607 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001608 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001609 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001610 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001611 if (wp->w_buffer == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001612 // Autocmd must have close the buffer already, skip.
Bram Moolenaar802418d2013-01-17 14:00:11 +01001613 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001614 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001615 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001616 {
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001617 bufref_T bufref;
1618
1619 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001620 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1621 buf->b_fname, FALSE, buf);
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001622 if (bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001623 CHANGEDTICK(buf) = -1; // note we did it already
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001624
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001625 // start all over, autocommands may mess up the lists
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001626 next_tp = first_tabpage;
1627 break;
1628 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001629 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001630 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001631
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001632 // Trigger BufUnload for buffers that are loaded
Bram Moolenaar29323592016-07-24 22:04:11 +02001633 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001634 if (buf->b_ml.ml_mfp != NULL)
1635 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001636 bufref_T bufref;
1637
1638 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001639 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001640 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001641 if (!bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001642 // autocmd deleted the buffer
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001643 break;
1644 }
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001645
1646 // deathtrap() blocks autocommands, but we do want to trigger
1647 // VimLeavePre.
1648 if (is_autocmd_blocked())
1649 {
1650 unblock_autocmds();
1651 ++unblock;
1652 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001653 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001654 if (unblock)
1655 block_autocmds();
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001656 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001657
1658#ifdef FEAT_VIMINFO
1659 if (*p_viminfo != NUL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001660 // Write out the registers, history, marks etc, to the viminfo file
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001661 write_viminfo(NULL, FALSE);
1662#endif
1663
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001664 if (v_dying <= 1)
Bram Moolenaarc7226682019-08-17 16:33:23 +02001665 {
1666 int unblock = 0;
1667
1668 // deathtrap() blocks autocommands, but we do want to trigger VimLeave.
1669 if (is_autocmd_blocked())
1670 {
1671 unblock_autocmds();
1672 ++unblock;
1673 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001674 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc7226682019-08-17 16:33:23 +02001675 if (unblock)
1676 block_autocmds();
1677 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001678
Bram Moolenaar05159a02005-02-26 23:04:13 +00001679#ifdef FEAT_PROFILE
1680 profile_dump();
1681#endif
1682
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001683 if (did_emsg
1684#ifdef FEAT_GUI
1685 || (gui.in_use && msg_didany && p_verbose > 0)
1686#endif
1687 )
1688 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001689 // give the user a chance to read the (error) message
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001690 no_wait_return = FALSE;
Volodymyr Kot7f0c4b42021-11-21 12:27:13 +00001691 wait_return(FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001692 }
1693
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001694 // Position the cursor again, the autocommands may have moved it
Bram Moolenaar7007e312021-03-27 12:11:33 +01001695 if (!is_not_a_term_or_gui())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001696 windgoto((int)Rows - 1, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001697
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001698#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001699 job_stop_on_exit();
1700#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001701#ifdef FEAT_LUA
1702 lua_end();
1703#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001704#ifdef FEAT_MZSCHEME
1705 mzscheme_end();
1706#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001707#ifdef FEAT_TCL
1708 tcl_end();
1709#endif
1710#ifdef FEAT_RUBY
1711 ruby_end();
1712#endif
1713#ifdef FEAT_PYTHON
1714 python_end();
1715#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001716#ifdef FEAT_PYTHON3
1717 python3_end();
1718#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001719#ifdef FEAT_PERL
1720 perl_end();
1721#endif
1722#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1723 iconv_end();
1724#endif
1725#ifdef FEAT_NETBEANS_INTG
1726 netbeans_end();
1727#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001728#ifdef FEAT_CSCOPE
1729 cs_end();
1730#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001731#ifdef FEAT_EVAL
1732 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001733 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001734#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001735#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +02001736 free_cmd_argsW();
1737#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001738
1739 mch_exit(exitval);
1740}
1741
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001742/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001743 * Get the name of the display, before gui_prepare() removes it from
1744 * argv[]. Used for the xterm-clipboard display.
1745 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001746 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001747 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001748 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001749early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001750{
Bram Moolenaar03005972008-11-20 13:12:36 +00001751#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1752 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001753 int argc = parmp->argc;
1754 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001755 int i;
1756
1757 for (i = 1; i < argc; i++)
1758 {
1759 if (STRCMP(argv[i], "--") == 0)
1760 break;
1761# ifdef FEAT_XCLIPBOARD
1762 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001763# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001764 || STRICMP(argv[i], "--display") == 0
1765# endif
1766 )
1767 {
1768 if (i == argc - 1)
1769 mainerr_arg_missing((char_u *)argv[i]);
1770 xterm_display = argv[++i];
1771 }
1772# endif
1773# ifdef FEAT_CLIENTSERVER
1774 else if (STRICMP(argv[i], "--servername") == 0)
1775 {
1776 if (i == argc - 1)
1777 mainerr_arg_missing((char_u *)argv[i]);
1778 parmp->serverName_arg = (char_u *)argv[++i];
1779 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001780 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001781 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001782 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001783 {
1784 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001785# ifdef FEAT_GUI
1786 if (strstr(argv[i], "-wait") != 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001787 // don't fork() when starting the GUI to edit files ourself
Bram Moolenaareb94e552006-03-11 21:35:11 +00001788 gui.dofork = FALSE;
1789# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001790 }
1791# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001792
Bram Moolenaar4f974752019-02-17 17:44:42 +01001793# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
1794# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001795 else if (STRICMP(argv[i], "--windowid") == 0)
1796# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001797 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001798# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001799 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001800 long_u id;
1801 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001802
1803 if (i == argc - 1)
1804 mainerr_arg_missing((char_u *)argv[i]);
1805 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001806 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001807 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001808 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001809 if (count != 1)
1810 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1811 else
Bram Moolenaar4f974752019-02-17 17:44:42 +01001812# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001813 win_socket_id = id;
1814# else
1815 gtk_socket_id = id;
1816# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001817 i++;
1818 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001819# endif
1820# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001821 else if (STRICMP(argv[i], "--echo-wid") == 0)
1822 echo_wid_arg = TRUE;
1823# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001824# ifndef FEAT_NETBEANS_INTG
1825 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001826 {
1827 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1828 mch_exit(2);
1829 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001830# endif
1831
Bram Moolenaar58d98232005-07-23 22:25:46 +00001832 }
1833#endif
1834}
1835
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001836#ifndef NO_VIM_MAIN
1837/*
1838 * Get a (optional) count for a Vim argument.
1839 */
1840 static int
1841get_number_arg(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001842 char_u *p, // pointer to argument
1843 int *idx, // index in argument, is incremented
1844 int def) // default value
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001845{
1846 if (vim_isdigit(p[*idx]))
1847 {
1848 def = atoi((char *)&(p[*idx]));
1849 while (vim_isdigit(p[*idx]))
1850 *idx = *idx + 1;
1851 }
1852 return def;
1853}
1854
1855/*
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001856 * Check for: [r][e][g][vi|vim|view][diff][ex[im]] (sort of)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001857 * If the executable name starts with "r" we disable shell commands.
1858 * If the next character is "e" we run in Easy mode.
1859 * If the next character is "g" we run the GUI version.
1860 * If the next characters are "view" we start in readonly mode.
1861 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1862 * If the next characters are "ex" we start in Ex mode. If it's followed
1863 * by "im" use improved Ex mode.
1864 */
1865 static void
1866parse_command_name(mparm_T *parmp)
1867{
1868 char_u *initstr;
1869
1870 initstr = gettail((char_u *)parmp->argv[0]);
1871
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001872#ifdef FEAT_EVAL
1873 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001874 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001875#endif
1876
1877 if (TOLOWER_ASC(initstr[0]) == 'r')
1878 {
1879 restricted = TRUE;
1880 ++initstr;
1881 }
1882
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001883 // Use evim mode for "evim" and "egvim", not for "editor".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001884 if (TOLOWER_ASC(initstr[0]) == 'e'
1885 && (TOLOWER_ASC(initstr[1]) == 'v'
1886 || TOLOWER_ASC(initstr[1]) == 'g'))
1887 {
1888#ifdef FEAT_GUI
1889 gui.starting = TRUE;
1890#endif
1891 parmp->evim_mode = TRUE;
1892 ++initstr;
1893 }
1894
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001895 // "gvim" starts the GUI. Also accept "Gvim" for MS-Windows.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001896 if (TOLOWER_ASC(initstr[0]) == 'g')
1897 {
1898 main_start_gui();
1899#ifdef FEAT_GUI
1900 ++initstr;
1901#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001902#ifdef GUI_MAY_SPAWN
1903 gui.dospawn = FALSE; // No need to spawn a new process.
1904#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001905 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001906#ifdef GUI_MAY_SPAWN
1907 else
1908 gui.dospawn = TRUE; // Not "gvim". Need to spawn gvim.exe.
1909#endif
1910
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001911
1912 if (STRNICMP(initstr, "view", 4) == 0)
1913 {
1914 readonlymode = TRUE;
1915 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001916 p_uc = 10000; // don't update very often
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001917 initstr += 4;
1918 }
1919 else if (STRNICMP(initstr, "vim", 3) == 0)
1920 initstr += 3;
1921
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001922 // Catch "[r][g]vimdiff" and "[r][g]viewdiff".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001923 if (STRICMP(initstr, "diff") == 0)
1924 {
1925#ifdef FEAT_DIFF
1926 parmp->diff_mode = TRUE;
1927#else
1928 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1929 mch_errmsg("\n");
1930 mch_exit(2);
1931#endif
1932 }
1933
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001934 // Checking for "ex" here may catch some weird names, such as "vimex" or
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001935 // "viewex", we assume the user knows that.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001936 if (STRNICMP(initstr, "ex", 2) == 0)
1937 {
1938 if (STRNICMP(initstr + 2, "im", 2) == 0)
1939 exmode_active = EXMODE_VIM;
1940 else
1941 exmode_active = EXMODE_NORMAL;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001942 change_compatible(TRUE); // set 'compatible'
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001943 }
1944}
1945
Bram Moolenaar58d98232005-07-23 22:25:46 +00001946/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001947 * Scan the command line arguments.
1948 */
1949 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001950command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001951{
1952 int argc = parmp->argc;
1953 char **argv = parmp->argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001954 int argv_idx; // index in argv[n][]
1955 int had_minmin = FALSE; // found "--" argument
1956 int want_argument; // option argument with argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001957 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001958 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001959 long n;
1960
1961 --argc;
1962 ++argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001963 argv_idx = 1; // active option letter is argv[0][argv_idx]
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001964 while (argc > 0)
1965 {
1966 /*
1967 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1968 */
1969 if (argv[0][0] == '+' && !had_minmin)
1970 {
1971 if (parmp->n_commands >= MAX_ARG_CMDS)
1972 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001973 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001974 if (argv[0][1] == NUL)
1975 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1976 else
1977 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1978 }
1979
1980 /*
1981 * Optional argument.
1982 */
1983 else if (argv[0][0] == '-' && !had_minmin)
1984 {
1985 want_argument = FALSE;
1986 c = argv[0][argv_idx++];
1987#ifdef VMS
1988 /*
1989 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1990 * and "-/X" as "-X".
1991 */
1992 if (c == '/')
1993 {
1994 c = argv[0][argv_idx++];
1995 c = TOUPPER_ASC(c);
1996 }
1997 else
1998 c = TOLOWER_ASC(c);
1999#endif
2000 switch (c)
2001 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002002 case NUL: // "vim -" read from stdin
2003 // "ex -" silent mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002004 if (exmode_active)
2005 silent_mode = TRUE;
2006 else
2007 {
2008 if (parmp->edit_type != EDIT_NONE)
2009 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2010 parmp->edit_type = EDIT_STDIN;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002011 read_cmd_fd = 2; // read from stderr instead of stdin
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002012 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002013 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002014 break;
2015
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002016 case '-': // "--" don't take any more option arguments
2017 // "--help" give help message
2018 // "--version" give version message
2019 // "--clean" clean context
2020 // "--literal" take files literally
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002021 // "--startuptime fname" write timing info
2022 // "--log fname" start logging early
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002023 // "--nofork" don't fork
2024 // "--not-a-term" don't warn for not a term
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002025 // "--gui-dialog-file fname" write dialog text
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002026 // "--ttyfail" exit if not a term
2027 // "--noplugin[s]" skip plugins
2028 // "--cmd <cmd>" execute cmd before vimrc
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002029 if (STRICMP(argv[0] + argv_idx, "help") == 0)
2030 usage();
2031 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
2032 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002033 Columns = 80; // need to init Columns
2034 info_message = TRUE; // use mch_msg(), not mch_errmsg()
K.Takata33b25d12022-01-13 16:06:45 +00002035#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar3b678042021-02-11 19:08:05 +01002036 gui.starting = FALSE; // not starting GUI, will exit
Bram Moolenaar0bcadf12021-02-11 19:18:58 +01002037#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002038 list_version();
2039 msg_putchar('\n');
2040 msg_didout = FALSE;
2041 mch_exit(0);
2042 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002043 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
2044 {
2045 parmp->use_vimrc = (char_u *)"DEFAULTS";
Bram Moolenaar62dd4522018-03-14 21:20:02 +01002046#ifdef FEAT_GUI
2047 use_gvimrc = (char_u *)"NONE";
2048#endif
Bram Moolenaar07268702018-03-01 21:57:32 +01002049 parmp->clean = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002050 set_option_value_give_err((char_u *)"vif",
2051 0L, (char_u *)"NONE", 0);
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002052 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002053 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
2054 {
Bram Moolenaar53076832015-12-31 19:53:21 +01002055#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002056 parmp->literal = TRUE;
2057#endif
2058 }
2059 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
2060 {
2061#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002062 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002063#endif
2064 }
2065 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
2066 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002067 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
2068 parmp->not_a_term = TRUE;
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002069 else if (STRNICMP(argv[0] + argv_idx, "gui-dialog-file", 15)
2070 == 0)
2071 {
2072 want_argument = TRUE;
2073 argv_idx += 15;
2074 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002075 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
2076 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002077 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
2078 {
2079 want_argument = TRUE;
2080 argv_idx += 3;
2081 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002082 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
2083 {
2084 want_argument = TRUE;
2085 argv_idx += 11;
2086 }
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002087 else if (STRNICMP(argv[0] + argv_idx, "log", 3) == 0)
2088 {
2089 want_argument = TRUE;
2090 argv_idx += 3;
2091 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002092#ifdef FEAT_CLIENTSERVER
2093 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002094 ; // already processed -- no arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002095 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
2096 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
2097 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002098 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002099 if (argc > 1)
2100 {
2101 --argc;
2102 ++argv;
2103 }
2104 }
2105#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002106#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002107# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002108 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002109# else
2110 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
2111# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002112 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002113 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002114 if (argc > 1)
2115 {
2116 --argc;
2117 ++argv;
2118 }
2119 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00002120#endif
2121#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002122 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
2123 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002124 // already processed, skip
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002125 }
2126#endif
2127 else
2128 {
2129 if (argv[0][argv_idx])
2130 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2131 had_minmin = TRUE;
2132 }
2133 if (!want_argument)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002134 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002135 break;
2136
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002137 case 'A': // "-A" start in Arabic mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002138#ifdef FEAT_ARABIC
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002139 set_option_value_give_err((char_u *)"arabic", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002140#else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002141 mch_errmsg(_(e_arabic_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002142 mch_exit(2);
2143#endif
2144 break;
2145
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002146 case 'b': // "-b" binary mode
2147 // Needs to be effective before expanding file names, because
2148 // for Win32 this makes us edit a shortcut file itself,
2149 // instead of the file it links to.
Bram Moolenaar231334e2005-07-25 20:46:57 +00002150 set_options_bin(curbuf->b_p_bin, 1, 0);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002151 curbuf->b_p_bin = 1; // binary file I/O
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002152 break;
2153
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002154 case 'C': // "-C" Compatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002155 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02002156 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002157 break;
2158
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002159 case 'e': // "-e" Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002160 exmode_active = EXMODE_NORMAL;
2161 break;
2162
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002163 case 'E': // "-E" Improved Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002164 exmode_active = EXMODE_VIM;
2165 break;
2166
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002167 case 'f': // "-f" GUI: run in foreground. Amiga: open
2168 // window directly, not with newcli
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002169#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002170 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002171#endif
2172 break;
2173
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002174 case 'g': // "-g" start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002175 main_start_gui();
2176 break;
2177
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002178 case 'F': // "-F" was for Farsi mode
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002179 mch_errmsg(_(e_farsi_support_has_been_removed));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002180 mch_exit(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002181 break;
2182
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002183 case '?': // "-?" give help message (for MS-Windows)
2184 case 'h': // "-h" give help message
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002185#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002186 // Tell usage() to exit for "gvim".
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002187 gui.starting = FALSE;
2188#endif
2189 usage();
2190 break;
2191
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002192 case 'H': // "-H" start in Hebrew mode: rl + hkmap set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002193#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002194 p_hkmap = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002195 set_option_value_give_err((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002196#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002197 mch_errmsg(_(e_hebrew_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002198 mch_exit(2);
2199#endif
2200 break;
2201
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002202 case 'l': // "-l" lisp mode, 'lisp' and 'showmatch' on
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002203 set_option_value_give_err((char_u *)"lisp", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002204 p_sm = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002205 break;
2206
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002207 case 'M': // "-M" no changes or writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002208 reset_modifiable();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002209 // FALLTHROUGH
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002210
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002211 case 'm': // "-m" no writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002212 p_write = FALSE;
2213 break;
2214
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002215 case 'y': // "-y" easy mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002216#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002217 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002218#endif
2219 parmp->evim_mode = TRUE;
2220 break;
2221
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002222 case 'N': // "-N" Nocompatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002223 change_compatible(FALSE);
2224 break;
2225
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002226 case 'n': // "-n" no swap file
Bram Moolenaar67c53842010-05-22 18:28:27 +02002227#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002228 // checking for "-nb", netbeans parameters
Bram Moolenaar67c53842010-05-22 18:28:27 +02002229 if (argv[0][argv_idx] == 'b')
2230 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002231 netbeansArg = argv[0];
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002232 argv_idx = -1; // skip to next argument
Bram Moolenaar67c53842010-05-22 18:28:27 +02002233 }
2234 else
2235#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002236 parmp->no_swap_file = TRUE;
2237 break;
2238
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002239 case 'p': // "-p[N]" open N tab pages
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002240#ifdef TARGET_API_MAC_OSX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002241 // For some reason on MacOS X, an argument like:
2242 // -psn_0_10223617 is passed in when invoke from Finder
2243 // or with the 'open' command
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002244 if (argv[0][argv_idx] == 's')
2245 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002246 argv_idx = -1; // bypass full -psn
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002247 main_start_gui();
2248 break;
2249 }
2250#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002251 // default is 0: open window for each file
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002252 parmp->window_count = get_number_arg((char_u *)argv[0],
2253 &argv_idx, 0);
2254 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002255 break;
2256
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002257 case 'o': // "-o[N]" open N horizontal split windows
2258 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002259 parmp->window_count = get_number_arg((char_u *)argv[0],
2260 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002261 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002262 break;
2263
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002264 case 'O': // "-O[N]" open N vertical split windows
2265 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002266 parmp->window_count = get_number_arg((char_u *)argv[0],
2267 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002268 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002269 break;
2270
2271#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002272 case 'q': // "-q" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002273 if (parmp->edit_type != EDIT_NONE)
2274 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2275 parmp->edit_type = EDIT_QF;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002276 if (argv[0][argv_idx]) // "-q{errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002277 {
2278 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2279 argv_idx = -1;
2280 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002281 else if (argc > 1) // "-q {errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002282 want_argument = TRUE;
2283 break;
2284#endif
2285
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002286 case 'R': // "-R" readonly mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002287 readonlymode = TRUE;
2288 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002289 p_uc = 10000; // don't update very often
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002290 break;
2291
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002292 case 'r': // "-r" recovery mode
2293 case 'L': // "-L" recovery mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002294 recoverymode = 1;
2295 break;
2296
2297 case 's':
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002298 if (exmode_active) // "-s" silent (batch) mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002299 silent_mode = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002300 else // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002301 want_argument = TRUE;
2302 break;
2303
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002304 case 't': // "-t {tag}" or "-t{tag}" jump to tag
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002305 if (parmp->edit_type != EDIT_NONE)
2306 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2307 parmp->edit_type = EDIT_TAG;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002308 if (argv[0][argv_idx]) // "-t{tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002309 {
2310 parmp->tagname = (char_u *)argv[0] + argv_idx;
2311 argv_idx = -1;
2312 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002313 else // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002314 want_argument = TRUE;
2315 break;
2316
2317#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002318 case 'D': // "-D" Debugging
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002319 parmp->use_debug_break_level = 9999;
2320 break;
2321#endif
2322#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002323 case 'd': // "-d" 'diff'
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002324# ifdef AMIGA
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002325 // check for "-dev {device}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002326 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2327 want_argument = TRUE;
2328 else
2329# endif
2330 parmp->diff_mode = TRUE;
2331 break;
2332#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002333 case 'V': // "-V{N}" Verbose level
2334 // default is 10: a little bit verbose
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002335 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2336 if (argv[0][argv_idx] != NUL)
2337 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002338 set_option_value_give_err((char_u *)"verbosefile",
2339 0L, (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002340 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002341 }
2342 break;
2343
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002344 case 'v': // "-v" Vi-mode (as if called "vi")
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002345 exmode_active = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002346#if defined(FEAT_GUI) && !defined(VIMDLL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002347 gui.starting = FALSE; // don't start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002348#endif
2349 break;
2350
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002351 case 'w': // "-w{number}" set window height
2352 // "-w {scriptout}" write to script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002353 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2354 {
2355 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002356 set_option_value_give_err((char_u *)"window", n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002357 break;
2358 }
2359 want_argument = TRUE;
2360 break;
2361
2362#ifdef FEAT_CRYPT
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002363 case 'x': // "-x" encrypted reading/writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002364 parmp->ask_for_key = TRUE;
2365 break;
2366#endif
2367
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002368 case 'X': // "-X" don't connect to X server
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002369#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2370 x_no_connect = TRUE;
2371#endif
2372 break;
2373
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002374 case 'Z': // "-Z" restricted mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002375 restricted = TRUE;
2376 break;
2377
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002378 case 'c': // "-c{command}" or "-c {command}" execute
2379 // command
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002380 if (argv[0][argv_idx] != NUL)
2381 {
2382 if (parmp->n_commands >= MAX_ARG_CMDS)
2383 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002384 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2385 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002386 argv_idx = -1;
2387 break;
2388 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002389 // FALLTHROUGH
2390 case 'S': // "-S {file}" execute Vim script
2391 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002392#ifndef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002393 case 'd': // "-d {device}" device (for Amiga)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002394#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002395 case 'T': // "-T {terminal}" terminal name
2396 case 'u': // "-u {vimrc}" vim inits file
2397 case 'U': // "-U {gvimrc}" gvim inits file
2398 case 'W': // "-W {scriptout}" overwrite
Bram Moolenaar4f974752019-02-17 17:44:42 +01002399#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002400 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002401#endif
2402 want_argument = TRUE;
2403 break;
2404
2405 default:
2406 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2407 }
2408
2409 /*
2410 * Handle option arguments with argument.
2411 */
2412 if (want_argument)
2413 {
2414 /*
2415 * Check for garbage immediately after the option letter.
2416 */
2417 if (argv[0][argv_idx] != NUL)
2418 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2419
2420 --argc;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002421 if (argc < 1 && c != 'S') // -S has an optional argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002422 mainerr_arg_missing((char_u *)argv[0]);
2423 ++argv;
2424 argv_idx = -1;
2425
2426 switch (c)
2427 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002428 case 'c': // "-c {command}" execute command
2429 case 'S': // "-S {file}" execute Vim script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002430 if (parmp->n_commands >= MAX_ARG_CMDS)
2431 mainerr(ME_EXTRA_CMD, NULL);
2432 if (c == 'S')
2433 {
2434 char *a;
2435
2436 if (argc < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002437 // "-S" without argument: use default session file
2438 // name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002439 a = SESSION_FILE;
2440 else if (argv[0][0] == '-')
2441 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002442 // "-S" followed by another option: use default
2443 // session file name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002444 a = SESSION_FILE;
2445 ++argc;
2446 --argv;
2447 }
2448 else
2449 a = argv[0];
Bram Moolenaar964b3742019-05-24 18:54:09 +02002450 p = alloc(STRLEN(a) + 4);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002451 if (p == NULL)
2452 mch_exit(2);
2453 sprintf((char *)p, "so %s", a);
2454 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2455 parmp->commands[parmp->n_commands++] = p;
2456 }
2457 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002458 parmp->commands[parmp->n_commands++] =
2459 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002460 break;
2461
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002462 case '-':
2463 if (argv[-1][2] == 'c')
2464 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002465 // "--cmd {command}" execute command
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002466 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2467 mainerr(ME_EXTRA_CMD, NULL);
2468 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002469 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002470 }
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002471 // --gui-dialog-file fname
2472 if (argv[-1][2] == 'g')
2473 {
2474 // without GUI ignore the argument
2475#ifdef FEAT_GUI
2476 parmp->gui_dialog_file = (char_u *)argv[0];
2477#endif
2478 }
2479
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002480 // "--startuptime <file>" already handled
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002481 // "--log <file>" already handled
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002482 break;
2483
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002484 // case 'd': -d {device} is handled in mch_check_win() for the
2485 // Amiga
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002486
2487#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002488 case 'q': // "-q {errorfile}" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002489 parmp->use_ef = (char_u *)argv[0];
2490 break;
2491#endif
2492
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002493 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002494 set_option_value_give_err((char_u *)"vif",
2495 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002496 break;
2497
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002498 case 's': // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002499 if (scriptin[0] != NULL)
2500 {
2501scripterror:
2502 mch_errmsg(_("Attempt to open script file again: \""));
2503 mch_errmsg(argv[-1]);
2504 mch_errmsg(" ");
2505 mch_errmsg(argv[0]);
2506 mch_errmsg("\"\n");
2507 mch_exit(2);
2508 }
2509 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2510 {
2511 mch_errmsg(_("Cannot open for reading: \""));
2512 mch_errmsg(argv[0]);
2513 mch_errmsg("\"\n");
2514 mch_exit(2);
2515 }
2516 if (save_typebuf() == FAIL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002517 mch_exit(2); // out of memory
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002518 break;
2519
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002520 case 't': // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002521 parmp->tagname = (char_u *)argv[0];
2522 break;
2523
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002524 case 'T': // "-T {terminal}" terminal name
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002525 /*
2526 * The -T term argument is always available and when
2527 * HAVE_TERMLIB is supported it overrides the environment
2528 * variable TERM.
2529 */
2530#ifdef FEAT_GUI
2531 if (term_is_gui((char_u *)argv[0]))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002532 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002533 else
2534#endif
2535 parmp->term = (char_u *)argv[0];
2536 break;
2537
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002538 case 'u': // "-u {vimrc}" vim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002539 parmp->use_vimrc = (char_u *)argv[0];
2540 break;
2541
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002542 case 'U': // "-U {gvimrc}" gvim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002543#ifdef FEAT_GUI
2544 use_gvimrc = (char_u *)argv[0];
2545#endif
2546 break;
2547
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002548 case 'w': // "-w {nr}" 'window' value
2549 // "-w {scriptout}" append to script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002550 if (vim_isdigit(*((char_u *)argv[0])))
2551 {
2552 argv_idx = 0;
2553 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002554 set_option_value_give_err((char_u *)"window",
2555 n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002556 argv_idx = -1;
2557 break;
2558 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002559 // FALLTHROUGH
2560 case 'W': // "-W {scriptout}" overwrite script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002561 if (scriptout != NULL)
2562 goto scripterror;
2563 if ((scriptout = mch_fopen(argv[0],
2564 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2565 {
2566 mch_errmsg(_("Cannot open for script output: \""));
2567 mch_errmsg(argv[0]);
2568 mch_errmsg("\"\n");
2569 mch_exit(2);
2570 }
2571 break;
2572
Bram Moolenaar4f974752019-02-17 17:44:42 +01002573#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002574 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002575 gui_mch_set_parent(argv[0]);
2576 break;
2577#endif
2578 }
2579 }
2580 }
2581
2582 /*
2583 * File name argument.
2584 */
2585 else
2586 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002587 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002588
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002589 // Check for only one type of editing.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002590 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2591 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2592 parmp->edit_type = EDIT_FILE;
2593
2594#ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002595 // Remember if the argument was a full path before changing
2596 // slashes to backslashes.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002597 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2598 parmp->full_path = TRUE;
2599#endif
2600
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002601 // Add the file to the global argument list.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002602 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2603 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2604 mch_exit(2);
2605#ifdef FEAT_DIFF
2606 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2607 && !mch_isdir(alist_name(&GARGLIST[0])))
2608 {
2609 char_u *r;
2610
2611 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2612 if (r != NULL)
2613 {
2614 vim_free(p);
2615 p = r;
2616 }
2617 }
2618#endif
K.Takatab247e062022-02-07 10:45:23 +00002619#ifdef __CYGWIN__
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002620 /*
2621 * If vim is invoked by non-Cygwin tools, convert away any
2622 * DOS paths, so things like .swp files are created correctly.
2623 * Look for evidence of non-Cygwin paths before we bother.
2624 * This is only for when using the Unix files.
2625 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002626 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002627 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002628 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002629
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002630# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002631 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002632# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002633 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002634# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002635 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002636 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002637 if (p == NULL)
2638 mch_exit(2);
2639 }
2640#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002641
2642#ifdef USE_FNAME_CASE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002643 // Make the case of the file name match the actual file.
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002644 fname_case(p, 0);
2645#endif
2646
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002647 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002648#ifdef EXPAND_FILENAMES
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002649 parmp->literal ? 2 : 0 // add buffer nr after exp.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002650#else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002651 2 // add buffer number now and use curbuf
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002652#endif
2653 );
2654
Bram Moolenaar4f974752019-02-17 17:44:42 +01002655#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002656 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002657 // Remember this argument has been added to the argument list.
2658 // Needed when 'encoding' is changed.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002659 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002660# ifdef FEAT_DIFF
2661 parmp->diff_mode
2662# else
2663 FALSE
2664# endif
2665 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002666 }
2667#endif
2668 }
2669
2670 /*
2671 * If there are no more letters after the current "-", go to next
2672 * argument. argv_idx is set to -1 when the current argument is to be
2673 * skipped.
2674 */
2675 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2676 {
2677 --argc;
2678 ++argv;
2679 argv_idx = 1;
2680 }
2681 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002682
2683#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002684 // If there is a "+123" or "-c" command, set v:swapcommand to the first
2685 // one.
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002686 if (parmp->n_commands > 0)
2687 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002688 p = alloc(STRLEN(parmp->commands[0]) + 3);
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002689 if (p != NULL)
2690 {
2691 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2692 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2693 vim_free(p);
2694 }
2695 }
2696#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002697}
2698
2699/*
2700 * Print a warning if stdout is not a terminal.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +01002701 * When starting in Ex mode and commands come from a file, set silent_mode.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002702 */
2703 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002704check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002705{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002706 int input_isatty; // is active input a terminal?
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002707
2708 input_isatty = mch_input_isatty();
2709 if (exmode_active)
2710 {
2711 if (!input_isatty)
2712 silent_mode = TRUE;
2713 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002714 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002715#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002716 // don't want the delay when started from the desktop
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002717 && !gui.starting
2718#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002719 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002720 {
2721#ifdef NBDEBUG
2722 /*
2723 * This shouldn't be necessary. But if I run netbeans with the log
2724 * output coming to the console and XOpenDisplay fails, I get vim
2725 * trying to start with input/output to my console tty. This fills my
2726 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002727 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002728 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002729 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002730 {
2731 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2732 exit(1);
2733 }
2734#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002735#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2736 if (
2737# ifdef VIMDLL
2738 !gui.starting &&
2739# endif
2740 is_cygpty_used())
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002741 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002742# if defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
Bram Moolenaar2a027452017-09-26 19:10:37 +02002743 && defined(FEAT_GETTEXT)
2744 char *s, *tofree = NULL;
2745
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002746 // Set the encoding of the error message based on $LC_ALL or
2747 // other environment variables instead of 'encoding'.
2748 // Note that the message is shown on a Cygwin terminal (e.g.
2749 // mintty) which encoding is based on $LC_ALL or etc., not the
2750 // current codepage used by normal Win32 console programs.
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002751 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002752 if (s == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002753 s = "utf-8"; // Use "utf-8" by default.
Bram Moolenaar2a027452017-09-26 19:10:37 +02002754 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2755 vim_free(tofree);
2756# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002757 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2758 exit(1);
2759 }
2760#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002761 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002762 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2763 if (!input_isatty)
2764 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2765 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002766 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2767 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002768 if (scriptin[0] == NULL)
Bram Moolenaareda1da02019-11-17 17:06:33 +01002769 ui_delay(2005L, TRUE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002770 TIME_MSG("Warning delay");
2771 }
2772}
2773
2774/*
2775 * Read text from stdin.
2776 */
2777 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002778read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002779{
2780 int i;
2781
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002782 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002783 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002784
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002785 no_wait_return = TRUE;
2786 i = msg_didany;
2787 set_buflisted(TRUE);
Bram Moolenaar204ade62020-10-11 14:58:46 +02002788
2789 // Create memfile and read from stdin.
Bram Moolenaar204ade62020-10-11 14:58:46 +02002790 (void)open_buffer(TRUE, NULL, 0);
2791
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002792 no_wait_return = FALSE;
2793 msg_didany = i;
2794 TIME_MSG("reading stdin");
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002795
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002796 check_swap_exists_action();
Bram Moolenaar8e1cbb52020-12-08 19:36:21 +01002797
2798#if !(defined(AMIGA) || defined(MACOS_X))
2799 // Dup stdin from stderr to read commands from, so that shell commands
2800 // work.
2801 // TODO: why is this needed, even though readfile() has done this?
2802 close(0);
2803 vim_ignored = dup(2);
2804#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002805}
2806
2807/*
2808 * Create the requested number of windows and edit buffers in them.
2809 * Also does recovery if "recoverymode" set.
2810 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002811 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002812create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002813{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002814 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002815 int done = 0;
2816
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002817 /*
2818 * Create the number of windows that was requested.
2819 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002820 if (parmp->window_count == -1) // was not set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002821 parmp->window_count = 1;
2822 if (parmp->window_count == 0)
2823 parmp->window_count = GARGCOUNT;
2824 if (parmp->window_count > 1)
2825 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002826 // Don't change the windows if there was a command in .vimrc that
2827 // already split some windows
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002828 if (parmp->window_layout == 0)
2829 parmp->window_layout = WIN_HOR;
2830 if (parmp->window_layout == WIN_TABS)
2831 {
2832 parmp->window_count = make_tabpages(parmp->window_count);
2833 TIME_MSG("making tab pages");
2834 }
2835 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002836 {
2837 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002838 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002839 TIME_MSG("making windows");
2840 }
2841 else
2842 parmp->window_count = win_count();
2843 }
2844 else
2845 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002846
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002847 if (recoverymode) // do recover
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002848 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002849 msg_scroll = TRUE; // scroll message up
Bram Moolenaar99499b12019-05-23 21:35:48 +02002850 ml_recover(TRUE);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002851 if (curbuf->b_ml.ml_mfp == NULL) // failed
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002852 getout(1);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002853 do_modelines(0); // do modelines
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002854 }
2855 else
2856 {
2857 /*
2858 * Open a buffer for windows that don't have one yet.
2859 * Commands in the .vimrc might have loaded a file or split the window.
2860 * Watch out for autocommands that delete a window.
2861 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002862 /*
2863 * Don't execute Win/Buf Enter/Leave autocommands here
2864 */
2865 ++autocmd_no_enter;
2866 ++autocmd_no_leave;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002867 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002868 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002869 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002870 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002871 {
2872 if (parmp->window_layout == WIN_TABS)
2873 goto_tabpage(1);
2874 else
2875 curwin = firstwin;
2876 }
2877 else if (parmp->window_layout == WIN_TABS)
2878 {
2879 if (curtab->tp_next == NULL)
2880 break;
2881 goto_tabpage(0);
2882 }
2883 else
2884 {
2885 if (curwin->w_next == NULL)
2886 break;
2887 curwin = curwin->w_next;
2888 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002889 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002890 curbuf = curwin->w_buffer;
2891 if (curbuf->b_ml.ml_mfp == NULL)
2892 {
2893#ifdef FEAT_FOLDING
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002894 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002895 if (p_fdls >= 0)
2896 curwin->w_p_fdl = p_fdls;
2897#endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002898 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002899 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002900
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002901 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002902
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002903 // create memfile, read file
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002904 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002905
Bram Moolenaar84212822006-11-07 21:59:47 +00002906 if (swap_exists_action == SEA_QUIT)
2907 {
2908 if (got_int || only_one_window())
2909 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002910 // abort selected or quit and only one window
2911 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00002912 getout(1);
2913 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002914 // We can't close the window, it would disturb what
2915 // happens next. Clear the file name and set the arg
2916 // index to -1 to delete it later.
Bram Moolenaar84212822006-11-07 21:59:47 +00002917 setfname(curbuf, NULL, NULL, FALSE);
2918 curwin->w_arg_idx = -1;
2919 swap_exists_action = SEA_NONE;
2920 }
2921 else
2922 handle_swap_exists(NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002923 dorewind = TRUE; // start again
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002924 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002925 ui_breakcheck();
2926 if (got_int)
2927 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002928 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002929 break;
2930 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002931 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002932 if (parmp->window_layout == WIN_TABS)
2933 goto_tabpage(1);
2934 else
2935 curwin = firstwin;
2936 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002937 --autocmd_no_enter;
2938 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002939 }
2940}
2941
Bram Moolenaar9810cfb2019-12-11 21:23:00 +01002942/*
2943 * If opened more than one window, start editing files in the other
2944 * windows. make_windows() has already opened the windows.
2945 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002946 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002947edit_buffers(
2948 mparm_T *parmp,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002949 char_u *cwd) // current working dir
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002950{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002951 int arg_idx; // index in argument list
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002952 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002953 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002954 win_T *win;
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002955 char_u *p_shm_save = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002956
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002957 /*
2958 * Don't execute Win/Buf Enter/Leave autocommands here
2959 */
2960 ++autocmd_no_enter;
2961 ++autocmd_no_leave;
Bram Moolenaar84212822006-11-07 21:59:47 +00002962
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002963 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002964 if (curwin->w_arg_idx == -1)
2965 {
2966 win_close(curwin, TRUE);
2967 advance = FALSE;
2968 }
2969
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002970 arg_idx = 1;
2971 for (i = 1; i < parmp->window_count; ++i)
2972 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002973 if (cwd != NULL)
2974 mch_chdir((char *)cwd);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002975 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002976 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002977 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002978 ++arg_idx;
2979 win_close(curwin, TRUE);
2980 advance = FALSE;
2981 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002982 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002983
Bram Moolenaar84212822006-11-07 21:59:47 +00002984 if (advance)
2985 {
2986 if (parmp->window_layout == WIN_TABS)
2987 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002988 if (curtab->tp_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00002989 break;
2990 goto_tabpage(0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002991 // Temporarily reset 'shm' option to not print fileinfo when
2992 // loading the other buffers. This would overwrite the already
2993 // existing fileinfo for the first tab.
2994 if (i == 1)
2995 {
2996 char buf[100];
2997
2998 p_shm_save = vim_strsave(p_shm);
2999 vim_snprintf(buf, 100, "F%s", p_shm);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003000 set_option_value_give_err((char_u *)"shm",
3001 0L, (char_u *)buf, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003002 }
Bram Moolenaar84212822006-11-07 21:59:47 +00003003 }
3004 else
3005 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003006 if (curwin->w_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00003007 break;
3008 win_enter(curwin->w_next, FALSE);
3009 }
3010 }
3011 advance = TRUE;
3012
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003013 // Only open the file if there is no file in this window yet (that can
3014 // happen when .vimrc contains ":sall").
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003015 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
3016 {
3017 curwin->w_arg_idx = arg_idx;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003018 // Edit file from arg list, if there is one. When "Quit" selected
3019 // at the ATTENTION prompt close the window.
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003020 swap_exists_did_quit = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003021 (void)do_ecmd(0, arg_idx < GARGCOUNT
3022 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003023 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003024 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00003025 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003026 // abort or quit selected
Bram Moolenaar84212822006-11-07 21:59:47 +00003027 if (got_int || only_one_window())
3028 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003029 // abort selected and only one window
3030 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00003031 getout(1);
3032 }
3033 win_close(curwin, TRUE);
3034 advance = FALSE;
3035 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003036 if (arg_idx == GARGCOUNT - 1)
3037 arg_had_last = TRUE;
3038 ++arg_idx;
3039 }
3040 ui_breakcheck();
3041 if (got_int)
3042 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003043 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003044 break;
3045 }
3046 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003047
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003048 if (p_shm_save != NULL)
3049 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003050 set_option_value_give_err((char_u *)"shm", 0L, p_shm_save, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003051 vim_free(p_shm_save);
3052 }
3053
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003054 if (parmp->window_layout == WIN_TABS)
3055 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003056 --autocmd_no_enter;
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003057
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003058 // make the first window the current window
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003059 win = firstwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003060#if defined(FEAT_QUICKFIX)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003061 // Avoid making a preview window the current window.
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003062 while (win->w_p_pvw)
3063 {
3064 win = win->w_next;
3065 if (win == NULL)
3066 {
3067 win = firstwin;
3068 break;
3069 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003070 }
3071#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003072 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003073
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003074 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003075 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003076 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003077 win_equal(curwin, FALSE, 'b'); // adjust heights
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003078}
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003079
3080/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003081 * Execute the commands from --cmd arguments "cmds[cnt]".
3082 */
3083 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003084exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003085{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003086 char_u **cmds = parmp->pre_commands;
3087 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003088 int i;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003089 ESTACK_CHECK_DECLARATION
Bram Moolenaar58d98232005-07-23 22:25:46 +00003090
3091 if (cnt > 0)
3092 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003093 curwin->w_cursor.lnum = 0; // just in case..
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003094 estack_push(ETYPE_ARGS, (char_u *)_("pre-vimrc command line"), 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003095 ESTACK_CHECK_SETUP
Bram Moolenaar58d98232005-07-23 22:25:46 +00003096# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003097 current_sctx.sc_sid = SID_CMDARG;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003098# endif
3099 for (i = 0; i < cnt; ++i)
3100 do_cmdline_cmd(cmds[i]);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003101 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003102 estack_pop();
Bram Moolenaar58d98232005-07-23 22:25:46 +00003103# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003104 current_sctx.sc_sid = 0;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003105# endif
3106 TIME_MSG("--cmd commands");
3107 }
3108}
3109
3110/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003111 * Execute "+", "-c" and "-S" arguments.
3112 */
3113 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003114exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003115{
3116 int i;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003117 ESTACK_CHECK_DECLARATION
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003118
3119 /*
3120 * We start commands on line 0, make "vim +/pat file" match a
3121 * pattern on line 1. But don't move the cursor when an autocommand
3122 * with g`" was used.
3123 */
3124 msg_scroll = TRUE;
3125 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
3126 curwin->w_cursor.lnum = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003127 estack_push(ETYPE_ARGS, (char_u *)"command line", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003128 ESTACK_CHECK_SETUP
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003129#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003130 current_sctx.sc_sid = SID_CARG;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003131 current_sctx.sc_seq = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003132#endif
3133 for (i = 0; i < parmp->n_commands; ++i)
3134 {
3135 do_cmdline_cmd(parmp->commands[i]);
3136 if (parmp->cmds_tofree[i])
3137 vim_free(parmp->commands[i]);
3138 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01003139 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003140 estack_pop();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003141#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003142 current_sctx.sc_sid = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003143#endif
3144 if (curwin->w_cursor.lnum == 0)
3145 curwin->w_cursor.lnum = 1;
3146
3147 if (!exmode_active)
3148 msg_scroll = FALSE;
3149
3150#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003151 // When started with "-q errorfile" jump to first error again.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003152 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003153 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003154#endif
3155 TIME_MSG("executing command arguments");
3156}
3157
3158/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003159 * Source startup scripts.
3160 */
3161 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003162source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003163{
3164 int i;
3165
3166 /*
3167 * For "evim" source evim.vim first of all, so that the user can overrule
3168 * any things he doesn't like.
3169 */
3170 if (parmp->evim_mode)
3171 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003173 TIME_MSG("source evim file");
3174 }
3175
3176 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003177 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00003178 * nothing else.
3179 */
3180 if (parmp->use_vimrc != NULL)
3181 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003182 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003183 {
3184 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE, NULL)
3185 != OK)
3186 emsg(e_failed_to_source_defaults);
3187 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003188 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003189 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003190 {
3191#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003192 if (use_gvimrc == NULL) // don't load gvimrc either
Bram Moolenaar58d98232005-07-23 22:25:46 +00003193 use_gvimrc = parmp->use_vimrc;
3194#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003195 }
3196 else
3197 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00003199 semsg(_(e_cannot_read_from_str_2), parmp->use_vimrc);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003200 }
3201 }
3202 else if (!silent_mode)
3203 {
3204#ifdef AMIGA
3205 struct Process *proc = (struct Process *)FindTask(0L);
3206 APTR save_winptr = proc->pr_WindowPtr;
3207
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003208 // Avoid a requester here for a volume that doesn't exist.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003209 proc->pr_WindowPtr = (APTR)-1L;
3210#endif
3211
3212 /*
3213 * Get system wide defaults, if the file name is defined.
3214 */
3215#ifdef SYS_VIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003217#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003218#ifdef MACOS_X
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE,
3220 DOSO_NONE, NULL);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003221#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003222
3223 /*
3224 * Try to read initialization commands from the following places:
3225 * - environment variable VIMINIT
3226 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3227 * - second user vimrc file ($VIM/.vimrc for Dos)
3228 * - environment variable EXINIT
3229 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3230 * - second user exrc file ($VIM/.exrc for Dos)
3231 * The first that exists is used, the rest is ignored.
3232 */
3233 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3234 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 if (do_source((char_u *)USR_VIMRC_FILE, TRUE,
3236 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003237#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003238 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003240#endif
3241#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003242 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003244#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003245#ifdef USR_VIMRC_FILE4
3246 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003248#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003249 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 && do_source((char_u *)USR_EXRC_FILE, FALSE,
3251 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003252#ifdef USR_EXRC_FILE2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253 && do_source((char_u *)USR_EXRC_FILE2, FALSE,
3254 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003255#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003256 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003257 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003258 // When no .vimrc file was found: source defaults.vim.
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003259 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE,
3260 NULL) == FAIL)
3261 emsg(e_failed_to_source_defaults);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003262 }
3263 }
3264
3265 /*
3266 * Read initialization commands from ".vimrc" or ".exrc" in current
3267 * directory. This is only done if the 'exrc' option is set.
3268 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003269 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003270 * option has been reset in environment of global ".exrc" or ".vimrc".
3271 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3272 * SYS_VIMRC_FILE.
3273 */
3274 if (p_exrc)
3275 {
3276#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003277 // If ".vimrc" file is not owned by user, set 'secure' mode.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003278 if (!file_owned(VIMRC_FILE))
3279#endif
3280 secure = p_secure;
3281
3282 i = FAIL;
3283 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003284 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003285#ifdef USR_VIMRC_FILE2
3286 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003287 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003288#endif
3289#ifdef USR_VIMRC_FILE3
3290 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003291 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003292#endif
3293#ifdef SYS_VIMRC_FILE
3294 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003295 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003296#endif
3297 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003299
3300 if (i == FAIL)
3301 {
3302#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003303 // if ".exrc" is not owned by user set 'secure' mode
Bram Moolenaar58d98232005-07-23 22:25:46 +00003304 if (!file_owned(EXRC_FILE))
3305 secure = p_secure;
3306 else
3307 secure = 0;
3308#endif
3309 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003310 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003311#ifdef USR_EXRC_FILE2
3312 && fullpathcmp((char_u *)USR_EXRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003313 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003314#endif
3315 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 (void)do_source((char_u *)EXRC_FILE, FALSE,
3317 DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003318 }
3319 }
3320 if (secure == 2)
3321 need_wait_return = TRUE;
3322 secure = 0;
3323#ifdef AMIGA
3324 proc->pr_WindowPtr = save_winptr;
3325#endif
3326 }
3327 TIME_MSG("sourcing vimrc file(s)");
3328}
3329
3330/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003331 * Setup to start using the GUI. Exit with an error when not available.
3332 */
3333 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003334main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003335{
3336#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003337 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003338#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003339 mch_errmsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003340 mch_errmsg("\n");
3341 mch_exit(2);
3342#endif
3343}
3344
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003345#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003346
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003347/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 * Get an environment variable and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003349 * Returns FAIL if the environment variable was not executed, OK otherwise.
3350 */
3351 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003352process_env(
3353 char_u *env,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003354 int is_viminit) // when TRUE, called for VIMINIT
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003355{
3356 char_u *initstr;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003357 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003358
Bram Moolenaare31ee862020-01-07 20:59:34 +01003359 ESTACK_CHECK_DECLARATION
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003360
3361 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3362 {
3363 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003364 vimrc_found(NULL, NULL);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003365 estack_push(ETYPE_ENV, env, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003366 ESTACK_CHECK_SETUP
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003367 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003368 current_sctx.sc_version = 1;
3369#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003370 current_sctx.sc_sid = SID_ENV;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003371 current_sctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003372 current_sctx.sc_lnum = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003373#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003374
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003375 do_cmdline_cmd(initstr);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003376
Bram Moolenaare31ee862020-01-07 20:59:34 +01003377 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003378 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003379 current_sctx = save_current_sctx;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003380 return OK;
3381 }
3382 return FAIL;
3383}
3384
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003385#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003386/*
3387 * Return TRUE if we are certain the user owns the file "fname".
3388 * Used for ".vimrc" and ".exrc".
3389 * Use both stat() and lstat() for extra security.
3390 */
3391 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003392file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003393{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003394 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003395# ifdef UNIX
3396 uid_t uid = getuid();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003397# else // VMS
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003398 uid_t uid = ((getgid() << 16) | getuid());
3399# endif
3400
3401 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3402# ifdef HAVE_LSTAT
3403 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3404# endif
3405 );
3406}
3407#endif
3408
3409/*
3410 * Give an error message main_errors["n"] and exit.
3411 */
3412 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003413mainerr(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003414 int n, // one of the ME_ defines
3415 char_u *str) // extra argument or NULL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003416{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003417#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003418 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003419#endif
3420
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003421 // If this is a Windows GUI executable, show an error dialog box.
3422#ifdef VIMDLL
3423 gui.in_use = mch_is_gui_executable();
3424#endif
3425#ifdef FEAT_GUI_MSWIN
3426 gui.starting = FALSE; // Needed to show as error.
3427#endif
3428
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003429 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003430 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003431 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003432 mch_errmsg(_(main_errors[n]));
3433 if (str != NULL)
3434 {
3435 mch_errmsg(": \"");
3436 mch_errmsg((char *)str);
3437 mch_errmsg("\"");
3438 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003439 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003440
3441 mch_exit(1);
3442}
3443
3444 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003445mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003446{
3447 mainerr(ME_ARG_MISSING, str);
3448}
3449
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003450#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003451/*
3452 * print a message with three spaces prepended and '\n' appended.
3453 */
3454 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003455main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003456{
3457 mch_msg(" ");
3458 mch_msg(s);
3459 mch_msg("\n");
3460}
3461
3462/*
3463 * Print messages for "vim -h" or "vim --help" and exit.
3464 */
3465 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003466usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003467{
3468 int i;
3469 static char *(use[]) =
3470 {
3471 N_("[file ..] edit specified file(s)"),
3472 N_("- read text from stdin"),
3473 N_("-t tag edit file where tag is defined"),
3474#ifdef FEAT_QUICKFIX
3475 N_("-q [errorfile] edit file with first error")
3476#endif
3477 };
3478
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003479#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003480 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003481#endif
3482
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003483 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003484 mch_msg(longVersion);
Bram Moolenaar32aaf5a2018-06-21 21:38:33 +02003485 mch_msg(_("\n\nUsage:"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003486 for (i = 0; ; ++i)
3487 {
3488 mch_msg(_(" vim [arguments] "));
3489 mch_msg(_(use[i]));
K.Takataeeec2542021-06-02 13:28:16 +02003490 if (i == ARRAY_LENGTH(use) - 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003491 break;
3492 mch_msg(_("\n or:"));
3493 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003494#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003495 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003496#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003497
3498 mch_msg(_("\n\nArguments:\n"));
3499 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003500#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003501 main_msg(_("--literal\t\tDon't expand wildcards"));
3502#endif
3503#ifdef FEAT_OLE
3504 main_msg(_("-register\t\tRegister this gvim for OLE"));
3505 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3506#endif
3507#ifdef FEAT_GUI
3508 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3509 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3510#endif
3511 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3512 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003513 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003514 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3515#ifdef FEAT_DIFF
3516 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3517#endif
3518 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3519 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3520 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3521 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3522 main_msg(_("-M\t\t\tModifications in text not allowed"));
3523 main_msg(_("-b\t\t\tBinary mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003524 main_msg(_("-l\t\t\tLisp mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003525 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3526 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003527 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3528#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003529 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003530#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003531 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3532 main_msg(_("-r\t\t\tList swap files and exit"));
3533 main_msg(_("-r (with file name)\tRecover crashed session"));
3534 main_msg(_("-L\t\t\tSame as -r"));
3535#ifdef AMIGA
3536 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3537 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3538#endif
3539#ifdef FEAT_ARABIC
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003540 main_msg(_("-A\t\t\tStart in Arabic mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003541#endif
3542#ifdef FEAT_RIGHTLEFT
3543 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3544#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003545 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003546 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2d12c252022-06-13 21:42:45 +01003547#ifdef FEAT_GUI
3548 main_msg(_("--gui-dialog-file {fname} For testing: write dialog text"));
3549#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003550 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003551 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3552#ifdef FEAT_GUI
3553 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3554#endif
3555 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003556 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003557 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3558 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3559 main_msg(_("+\t\t\tStart at end of file"));
3560 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003561 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003562 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3563 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3564 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3565 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3566 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3567#ifdef FEAT_CRYPT
3568 main_msg(_("-x\t\t\tEdit encrypted files"));
3569#endif
3570#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3571# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar86181df2020-05-11 23:14:04 +02003572 main_msg(_("-display <display>\tConnect Vim to this particular X-server"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003573# endif
3574 main_msg(_("-X\t\t\tDo not connect to X server"));
3575#endif
3576#ifdef FEAT_CLIENTSERVER
3577 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3578 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3579 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3580 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003581 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003582 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3583 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3584 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3585 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3586#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003587#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003588 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003589#endif
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01003590#ifdef FEAT_JOB_CHANNEL
K.Takata0fd7be72022-11-09 16:29:24 +00003591 main_msg(_("--log <file>\t\tStart logging to <file> early"));
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01003592#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003593#ifdef FEAT_VIMINFO
3594 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3595#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003596 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003597 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3598 main_msg(_("--version\t\tPrint version information and exit"));
3599
3600#ifdef FEAT_GUI_X11
3601# ifdef FEAT_GUI_MOTIF
3602 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003603# endif
Bram Moolenaar86181df2020-05-11 23:14:04 +02003604 main_msg(_("-display <display>\tRun Vim on <display>"));
Bram Moolenaar9e6ba8c2020-05-12 22:21:26 +02003605 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003606 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3607 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3608 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3609 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3610 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3611 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3612 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3613 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003614 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3615 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3616 main_msg(_("-xrm <resource>\tSet the specified resource"));
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003617#endif // FEAT_GUI_X11
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003618#ifdef FEAT_GUI_GTK
3619 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003620 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3621 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003622 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3623 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003624 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003625 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
Bram Moolenaar86181df2020-05-11 23:14:04 +02003626 main_msg(_("-display <display>\tRun Vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003627 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003628 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003629 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003630#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003631#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003632# ifdef VIMDLL
3633 if (gui.starting)
3634# endif
3635 {
3636 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
3637 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
3638 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003639#endif
3640
3641#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003642 // Gnome gives extra messages for --help if we continue, but not for -h.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003643 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003644 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003645 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003646 gui.dofork = FALSE;
3647 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003648 else
3649#endif
3650 mch_exit(0);
3651}
3652
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003653/*
3654 * Check the result of the ATTENTION dialog:
3655 * When "Quit" selected, exit Vim.
3656 * When "Recover" selected, recover the file.
3657 */
3658 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003659check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003660{
3661 if (swap_exists_action == SEA_QUIT)
3662 getout(1);
3663 handle_swap_exists(NULL);
3664}
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003665
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003666#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003667
Bram Moolenaar595297d2017-03-04 19:11:12 +01003668#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003669 static void
3670set_progpath(char_u *argv0)
3671{
3672 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003673
Bram Moolenaar4f974752019-02-17 17:44:42 +01003674# ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003675 // A relative path containing a "/" will become invalid when using ":cd",
3676 // turn it into a full path.
3677 // On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3678 // this.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003679 char_u *path = NULL;
3680
3681 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3682 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003683# else
3684 char_u buf[MAXPATHL + 1];
3685# ifdef PROC_EXE_LINK
3686 char linkbuf[MAXPATHL + 1];
3687 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003688
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003689 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3690 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003691 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003692 linkbuf[len] = NUL;
3693 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003694 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003695# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003696
3697 if (!mch_isFullName(val))
3698 {
3699 if (gettail(val) != val
3700 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3701 val = buf;
3702 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003703# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003704
Bram Moolenaar08cab962017-03-04 14:37:18 +01003705 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003706
Bram Moolenaar4f974752019-02-17 17:44:42 +01003707# ifdef MSWIN
Bram Moolenaar43663192017-03-05 14:29:12 +01003708 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003709# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003710}
3711
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003712#endif // NO_VIM_MAIN